From e68c8280fa2e1b7071378cfdd876155c73ec944f Mon Sep 17 00:00:00 2001 From: Andrew MacLeod Date: Fri, 30 Jul 2021 15:15:29 -0400 Subject: Abstract tracing routines into a class. Generalize range tracing into a class and integrae it with gimple_ranger. Remove the old derived trace_ranger class. * Makefile.in (OBJS): Add gimple-range-trace.o. * gimple-range-cache.h (enable_new_values): Remove unused prototype. * gimple-range-fold.cc: Adjust headers. * gimple-range-trace.cc: New. * gimple-range-trace.h: New. * gimple-range.cc (gimple_ranger::gimple_ranger): Enable tracer. (gimple_ranger::range_of_expr): Add tracing. (gimple_ranger::range_on_entry): Ditto. (gimple_ranger::range_on_exit): Ditto. (gimple_ranger::range_on_edge): Ditto. (gimple_ranger::fold_range_internal): Ditto. (gimple_ranger::dump_bb): Do not calculate edge range twice. (trace_ranger::*): Remove. (enable_ranger): Never create a trace_ranger. (debug_seed_ranger): Move to gimple-range-trace.cc. (dump_ranger): Ditto. (debug_ranger): Ditto. * gimple-range.h: Include gimple-range-trace.h. (range_on_entry, range_on_exit): No longer virtual. (class trace_ranger): Remove. (DEBUG_RANGE_CACHE): Move to gimple-range-trace.h. --- gcc/gimple-range-trace.cc | 206 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 gcc/gimple-range-trace.cc (limited to 'gcc/gimple-range-trace.cc') diff --git a/gcc/gimple-range-trace.cc b/gcc/gimple-range-trace.cc new file mode 100644 index 0000000..1feb978 --- /dev/null +++ b/gcc/gimple-range-trace.cc @@ -0,0 +1,206 @@ +/* Code for GIMPLE range trace and debugging related routines. + Copyright (C) 2019-2021 Free Software Foundation, Inc. + Contributed by Andrew MacLeod + and Aldy Hernandez . + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 3, or (at your option) +any later version. + +GCC is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +. */ + +#include "config.h" +#include "system.h" +#include "coretypes.h" +#include "backend.h" +#include "tree.h" +#include "gimple.h" +#include "ssa.h" +#include "gimple-pretty-print.h" +#include "gimple-iterator.h" +#include "tree-cfg.h" +#include "fold-const.h" +#include "tree-cfg.h" +#include "cfgloop.h" +#include "tree-scalar-evolution.h" +#include "gimple-range.h" + + +// Breakpoint to trap at a specific index. From GDB, this provides a simple +// place to put a breakpoint to stop at a given trace line. +// ie. b range_tracer::breakpoint if index == 45678 + +void +range_tracer::breakpoint (unsigned index ATTRIBUTE_UNUSED) +{ +} + +// Construct a range_tracer with component NAME. + +range_tracer::range_tracer (const char *name) +{ + gcc_checking_assert (strlen(name) < name_len -1); + strcpy (component, name); + indent = 0; + tracing = false; +} + +// This routine does the initial line spacing/indenting for a trace. +// If BLANKS is false, then IDX is printed, otherwise spaces. + +void +range_tracer::print_prefix (unsigned idx, bool blanks) +{ + // Print counter index as well as INDENT spaces. + if (!blanks) + fprintf (dump_file, "%-7u ", idx); + else + fprintf (dump_file, " "); + fprintf (dump_file, "%s ", component); + unsigned x; + for (x = 0; x< indent; x++) + fputc (' ', dump_file); + +} +// If dumping, return the next call index and print the prefix for the next +// output line. If not, retrurn 0. +// Counter is static to monotonically increase across the compilation unit. + +unsigned +range_tracer::do_header (const char *str) +{ + static unsigned trace_count = 0; + + unsigned idx = ++trace_count; + print_prefix (idx, false); + fprintf (dump_file, "%s", str); + indent += bump; + breakpoint (idx); + return idx; +} + +// Print a line without starting or ending a trace. + +void +range_tracer::print (unsigned counter, const char *str) +{ + print_prefix (counter, true); + fprintf (dump_file, "%s", str); +} + +// End a trace and print the CALLER, NAME, and RESULT and range R, + +void +range_tracer::trailer (unsigned counter, const char *caller, bool result, + tree name, const irange &r) +{ + gcc_checking_assert (tracing && counter != 0); + + indent -= bump; + print_prefix (counter, true); + fputs(result ? "TRUE : " : "FALSE : ", dump_file); + fprintf (dump_file, "(%u) ", counter); + fputs (caller, dump_file); + fputs (" (",dump_file); + if (name) + print_generic_expr (dump_file, name, TDF_SLIM); + fputs (") ",dump_file); + if (result) + { + r.dump (dump_file); + fputc('\n', dump_file); + } + else + fputc('\n', dump_file); +} + +// ========================================= +// Debugging helpers. +// ========================================= + +// Query all statements in the IL to precalculate computable ranges in RANGER. + +static DEBUG_FUNCTION void +debug_seed_ranger (gimple_ranger &ranger) +{ + // Recalculate SCEV to make sure the dump lists everything. + if (scev_initialized_p ()) + { + scev_finalize (); + scev_initialize (); + } + + basic_block bb; + int_range_max r; + gimple_stmt_iterator gsi; + FOR_EACH_BB_FN (bb, cfun) + for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) + { + gimple *stmt = gsi_stmt (gsi); + + if (is_gimple_debug (stmt)) + continue; + + ranger.range_of_stmt (r, stmt); + } +} + +// Dump all that ranger knows for the current function. + +DEBUG_FUNCTION void +dump_ranger (FILE *out) +{ + gimple_ranger ranger; + debug_seed_ranger (ranger); + ranger.dump (out); +} + +DEBUG_FUNCTION void +debug_ranger () +{ + dump_ranger (stderr); +} + +// Dump all that ranger knows on a path of BBs. +// +// Note that the blocks are in reverse order, thus the exit block is +// path[0]. + +DEBUG_FUNCTION void +dump_ranger (FILE *dump_file, const vec &path) +{ + if (path.length () == 0) + { + fprintf (dump_file, "empty\n"); + return; + } + + gimple_ranger ranger; + debug_seed_ranger (ranger); + + unsigned i = path.length (); + do + { + i--; + ranger.dump_bb (dump_file, path[i]); + } + while (i > 0); +} + +DEBUG_FUNCTION void +debug_ranger (const vec &path) +{ + dump_ranger (stderr, path); +} + +#include "gimple-range-tests.cc" -- cgit v1.1