From 30a60b383c5bb8b07ab178ad81d60fd81432059d Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Wed, 16 May 2018 13:08:21 -0400 Subject: [PATCH 59/83] Cleanups to optinfo.h/.cc --- gcc/optinfo.cc | 24 +++++++----- gcc/optinfo.h | 114 ++++++++++++++++++++++++++++++++++----------------------- 2 files changed, 82 insertions(+), 56 deletions(-) diff --git a/gcc/optinfo.cc b/gcc/optinfo.cc index a0b468b..d39ad66 100644 --- a/gcc/optinfo.cc +++ b/gcc/optinfo.cc @@ -1,4 +1,4 @@ -/* FIXME. +/* Optimization information. Copyright (C) 2018 Free Software Foundation, Inc. Contributed by David Malcolm . @@ -52,7 +52,7 @@ along with GCC; see the file COPYING3. If not see #include "diagnostic.h" #include "diagnostic-color.h" -/* FIXME. */ +/* Constructing an optinfo_location from a loop. */ optinfo_location::optinfo_location (struct loop *loop) : m_stmt (NULL) @@ -76,7 +76,7 @@ optinfo_location::optinfo_location (struct loop *loop) } } -/* FIXME. */ +/* Get a string from KIND. */ const char * optinfo_kind_to_string (enum optinfo_kind kind) @@ -110,7 +110,7 @@ optinfo::~optinfo () delete item; } -/* FIXME. */ +/* Get a location_t from an optinfo. */ location_t optinfo::get_location_t () const @@ -121,14 +121,18 @@ optinfo::get_location_t () const return UNKNOWN_LOCATION; } -/* FIXME. */ +/* Emit the optinfo to any/all of the active destinations. */ void optinfo::emit () const { /* -fsave-optimization-record. */ optimization_records_maybe_record_optinfo (this); + + /* FIXME. */ emit_optinfo_as_diagnostic_remark (this); + + /* -fopt-info. */ emit_via_fopt_info (); } @@ -265,7 +269,7 @@ pending_optinfo::pending_optinfo (const location &impl_location, { } -/* FIXME. */ +/* Append a string literal to this pending_optinfo. */ pending_optinfo& pending_optinfo::operator<< (const char *msg) @@ -276,7 +280,7 @@ pending_optinfo::operator<< (const char *msg) return *this; } -/* FIXME. */ +/* Append a tree node with flags to this pending_optinfo. */ pending_optinfo& pending_optinfo::operator<< (optinfo_tree node_with_flags) @@ -285,7 +289,7 @@ pending_optinfo::operator<< (optinfo_tree node_with_flags) return *this; } -/* FIXME. */ +/* Append a gimple statement to this pending_optinfo. */ pending_optinfo& pending_optinfo::operator<< (gimple *stmt) @@ -294,12 +298,12 @@ pending_optinfo::operator<< (gimple *stmt) return *this; } -/* FIXME. */ +/* Append printf-formatted text to this pending_optinfo. */ pending_optinfo& pending_optinfo::operator<< (const optinfo_printf &p) { - // FIXME: some double-copies going on here + // FIXME: there are some double-copies going on here optinfo_item *item = new optinfo_item_text (xstrdup (p.get_formatted_text ()), true); m_optinfo->m_items.safe_push (item); diff --git a/gcc/optinfo.h b/gcc/optinfo.h index a391966..e5c2eda 100644 --- a/gcc/optinfo.h +++ b/gcc/optinfo.h @@ -1,4 +1,4 @@ -/* FIXME. +/* Optimization information. Copyright (C) 2018 Free Software Foundation, Inc. Contributed by David Malcolm . @@ -25,15 +25,46 @@ along with GCC; see the file COPYING3. If not see /* FIXME: this is flexible, but probably inefficient. */ +/* An "optinfo" is a bundle of information describing part of an + optimization, which can be emitted to zero or more of several + destinations: + + * the "-fopt-info" destination(s) + + * as a "remark" through the diagnostics subsystem + + * saved to a file via "-fsave-optimization-record" + + Building optinfo instances is non-trivial, so all usage should be + guarded by + + if (optinfo_enabled_p ()) + + which is off by default. + + They're intended to be be short-lived; in particular, there's no + interaction with GTY: it's assumed that no GC happens during the + lifetime of an optinfo. */ + +/* Forward decls. */ +class pending_optinfo; + +/* Multiple dispatch: there are various kinds of items within an optinfo, + and various destinations to send optinfo to. + + Handling this for now by exposing all of the item subclasses, + and having the destinations handle them with "switch" statements. */ + +/* An enum for discriminating between optinfo_item subclasses. */ + enum optinfo_item_kind { OPTINFO_ITEM_KIND_TEXT, OPTINFO_ITEM_KIND_TREE, - OPTINFO_ITEM_KIND_GIMPLE, - //OPTINFO_ITEM_KIND_POLY_INT, + OPTINFO_ITEM_KIND_GIMPLE }; -/* FIXME. */ +/* Base class for items within an optinfo. */ class optinfo_item { @@ -45,7 +76,8 @@ class optinfo_item /* optinfo_item subclasses. */ -/* FIXME. */ +/* Item within an optinfo: text, either owned by the item + (for optinfo_printf), or borrowed (for string literals). */ class optinfo_item_text : public optinfo_item { @@ -72,13 +104,14 @@ class optinfo_item_text : public optinfo_item }; /* Packaging up a tree with dump flags. */ +// FIXME: should this be GTY-marked? struct optinfo_tree { optinfo_tree (tree node, dump_flags_t flags) : m_node (node), m_flags (flags) { - // only support TDF_* flags. + /* Only the TDF_* flags are allowed. */ gcc_assert ((m_flags & MSG_ALL) == 0); // FIXME: what happened to TDF_KIND_MASK, mentioned in dumpfile.h ? } @@ -97,7 +130,9 @@ static inline optinfo_tree details (tree node) return optinfo_tree (node, TDF_DETAILS); } -/* FIXME. */ +/* Item within an optinfo: a tree, with dump flags. + Note that this is not GTY-marked, as it's assumed that instances + are short-lived. */ class optinfo_item_tree : public optinfo_item { @@ -117,7 +152,9 @@ class optinfo_item_tree : public optinfo_item optinfo_tree m_node_with_flags; }; -/* FIXME. */ +/* Item within an optinfo: a gimple statement. + Note that this is not GTY-marked, as it's assumed that instances + are short-lived. */ class optinfo_item_gimple : public optinfo_item { @@ -134,30 +171,12 @@ class optinfo_item_gimple : public optinfo_item gimple *m_stmt; }; -#if 0 -class optinfo_item_poly_int : public optinfo_item -{ - public: - template - optinfo_item_poly_int (const poly_int &val) - { - print_dec (val, m_digit_buffer, - poly_coeff_traits::signedness ? SIGNED : UNSIGNED); - } - - enum optinfo_item_kind get_kind () const FINAL OVERRIDE - { - return OPTINFO_ITEM_KIND_POLY_INT; - } - - private: - /* This must be large enough to hold any printed integer or - floating-point value. */ - char m_digit_buffer[128]; -}; -#endif - -/* FIXME. */ +/* A class for describing a source-code location for an optinfo. + In particular, this lets us associate optinfo instances + with hotness information (e.g. from PGO), allowing them to + be prioritized by code hotness. + Note that this is not GTY-marked, as it's assumed that instances + are short-lived. */ class optinfo_location { @@ -180,8 +199,6 @@ class optinfo_location #endif }; -class pending_optinfo; - /* A way to identify where in the compiler source that optimization information is being emitted from. */ /* FIXME: taken from selftest::location. */ @@ -201,7 +218,7 @@ struct location #define OPTINFO_IMPL_LOCATION \ (location (__FILE__, __LINE__, __FUNCTION__)) -/* FIXME. */ +/* The various kinds of optinfo. */ enum optinfo_kind { @@ -211,10 +228,12 @@ enum optinfo_kind OPTINFO_KIND_SCOPE }; -/* FIXME. */ extern const char *optinfo_kind_to_string (enum optinfo_kind kind); -/* FIXME. */ +/* A bundle of information describing part of an optimization. + + Note that this is not GTY-marked, as it's assumed that instances + are short-lived. */ class optinfo { @@ -246,7 +265,7 @@ class optinfo auto_vec m_items; }; -/* FIXME. */ +/* A class for adding printf-style formatted strings to an optinfo. */ class optinfo_printf { @@ -304,11 +323,11 @@ class pending_optinfo } private: - // FIXME: are we allowed this yet? or do we need a gnu::shared_ptr + // FIXME: are we allowed this yet? or do we need a gnu::shared_ptr? std::shared_ptr m_optinfo; }; -/* FIXME. */ +/* Emit optimization information at LOC of the given KIND. */ #define OPTINFO(KIND, LOC) \ pending_optinfo ((OPTINFO_IMPL_LOCATION), (KIND), (LOC)) @@ -332,10 +351,10 @@ extern void push_optinfo_scope (); extern void pop_optinfo_scope (); extern unsigned int get_optinfo_scope_depth (); -/* FIXME. */ -// RAII-style class intended to make -// it easy to emit optinfo remarks about entering and exiting the body of a -// given function +/* Implementation detail of the OPTINFO_SCOPE macro below. + + A RAII-style class intended to make it easy to emit optinfo remarks + about entering and exiting the body of a given function. */ class optinfo_scope { @@ -362,11 +381,14 @@ class optinfo_scope optinfo_location m_loc; }; -/* FIXME. */ +/* A macro for emitting an optinfo note about entering a scope, + pushing and popping the scope, so that all optinfos "within" + the scope are nested within it. */ + #define OPTINFO_SCOPE(NAME, LOC) \ optinfo_scope scope (NAME, LOC, (OPTINFO_IMPL_LOCATION)) -/* FIXME. */ +/* Implementation detail. */ extern void emit_optinfo_as_diagnostic_remark (const optinfo *); -- 1.8.5.3