Commit b84e3bde by David Malcolm Committed by David Malcolm

dumpfile.c: eliminate special-casing of dump_file/alt_dump_file

With the addition of optinfo, the various dump_* calls had three parts:
- optionally print to dump_file
- optionally print to alt_dump_file
- optionally make an optinfo_item and add it to the pending optinfo,
  creating it for dump_*_loc calls.

However, this split makes it difficult to implement the formatted dumps
later in patch kit, so as enabling work towards that, this patch removes
the above split, so that all dumping within the dump_* API goes through
optinfo_item.

In order to ensure that the dumps to dump_file and alt_dump_file are
processed immediately (rather than being buffered within the pending
optinfo for consolidation), this patch introduces the idea of "immediate"
optinfo_item destinations vs "non-immediate" destinations.

The patch also adds selftest coverage of what's printed, and of scopes.

This adds two allocations per dump_* call when dumping is enabled.
I'm assuming that this isn't a problem, as dump_enabled_p is normally
false.  There are ways of optimizing it if it is an issue (by making
optinfo_item instances become temporaries that borrow the underlying
buffer), but they require nontrivial changes, so I'd prefer to leave
that for another patch kit, if it becomes necessary.

gcc/ChangeLog:
	* dump-context.h: Include "pretty-print.h".
	(dump_context::refresh_dumps_are_enabled): New decl.
	(dump_context::emit_item): New decl.
	(class dump_context): Add fields "m_test_pp" and
	"m_test_pp_flags".
	(temp_dump_context::temp_dump_context): Add param "test_pp_flags".
	(temp_dump_context::get_dumped_text): New decl.
	(class temp_dump_context): Add field "m_pp".
	* dumpfile.c (refresh_dumps_are_enabled): Convert to...
	(dump_context::refresh_dumps_are_enabled): ...and add a test for
	m_test_pp.
	(set_dump_file): Update for above change.
	(set_alt_dump_file): Likewise.
	(dump_loc): New overload, taking a pretty_printer *.
	(dump_context::dump_loc): Call end_any_optinfo.  Dump the location
	to any test pretty-printer.
	(make_item_for_dump_gimple_stmt): New function, adapted from
	optinfo::add_gimple_stmt.
	(dump_context::dump_gimple_stmt): Call it, and use the result,
	eliminating the direct usage of dump_file and alt_dump_file in
	favor of indirectly using them via emit_item.
	(make_item_for_dump_gimple_expr): New function, adapted from
	optinfo::add_gimple_expr.
	(dump_context::dump_gimple_expr): Call it, and use the result,
	eliminating the direct usage of dump_file and alt_dump_file in
	favor of indirectly using them via emit_item.
	(make_item_for_dump_generic_expr): New function, adapted from
	optinfo::add_tree.
	(dump_context::dump_generic_expr): Call it, and use the result,
	eliminating the direct usage of dump_file and alt_dump_file in
	favor of indirectly using them via emit_item.
	(make_item_for_dump_printf_va): New function, adapted from
	optinfo::add_printf_va.
	(make_item_for_dump_printf): New function.
	(dump_context::dump_printf_va): Call make_item_for_dump_printf_va,
	and use the result, eliminating the direct usage of dump_file and
	alt_dump_file in favor of indirectly using them via emit_item.
	(make_item_for_dump_dec): New function.
	(dump_context::dump_dec): Call it, and use the result,
	eliminating the direct usage of dump_file and alt_dump_file in
	favor of indirectly using them via emit_item.
	(make_item_for_dump_symtab_node): New function, adapted from
	optinfo::add_symtab_node.
	(dump_context::dump_symtab_node): Call it, and use the result,
	eliminating the direct usage of dump_file and alt_dump_file in
	favor of indirectly using them via emit_item.
	(dump_context::begin_scope): Reimplement, avoiding direct usage
	of dump_file and alt_dump_file in favor of indirectly using them
	via emit_item.
	(dump_context::emit_item): New member function.
	(temp_dump_context::temp_dump_context): Add param "test_pp_flags".
	Set up test pretty-printer on the underlying context.  Call
	refresh_dumps_are_enabled.
	(temp_dump_context::~temp_dump_context): Call
	refresh_dumps_are_enabled.
	(temp_dump_context::get_dumped_text): New member function.
	(selftest::verify_dumped_text): New function.
	(ASSERT_DUMPED_TEXT_EQ): New macro.
	(selftest::test_capture_of_dump_calls): Run all tests twice, with
	and then without optinfo enabled.  Add uses of
	ASSERT_DUMPED_TEXT_EQ to all tests.  Add test of nested scopes.
	* dumpfile.h: Update comment for the dump_* API.
	* optinfo-emit-json.cc
	(selftest::test_building_json_from_dump_calls): Update for new
	param for temp_dump_context ctor.
	* optinfo.cc (optinfo_item::optinfo_item): Remove "owned" param
	and "m_owned" field.
	(optinfo_item::~optinfo_item): Likewise.
	(optinfo::add_item): New member function.
	(optinfo::emit): Update comment.
	(optinfo::add_string): Delete.
	(optinfo::add_printf): Delete.
	(optinfo::add_printf_va): Delete.
	(optinfo::add_gimple_stmt): Delete.
	(optinfo::add_gimple_expr): Delete.
	(optinfo::add_tree): Delete.
	(optinfo::add_symtab_node): Delete.
	(optinfo::add_dec): Delete.
	* optinfo.h (class dump_context): New forward decl.
	(optinfo::add_item): New decl.
	(optinfo::add_string): Delete.
	(optinfo::add_printf): Delete.
	(optinfo::add_printf_va): Delete.
	(optinfo::add_gimple_stmt): Delete.
	(optinfo::add_gimple_expr): Delete.
	(optinfo::add_tree): Delete.
	(optinfo::add_symtab_node): Delete.
	(optinfo::add_dec): Delete.
	(optinfo::add_poly_int): Delete.
	(optinfo_item::optinfo_item): Remove "owned" param.
	(class optinfo_item): Remove field "m_owned".

From-SVN: r263178
parent b06e1dce
2018-07-31 David Malcolm <dmalcolm@redhat.com>
* dump-context.h: Include "pretty-print.h".
(dump_context::refresh_dumps_are_enabled): New decl.
(dump_context::emit_item): New decl.
(class dump_context): Add fields "m_test_pp" and
"m_test_pp_flags".
(temp_dump_context::temp_dump_context): Add param "test_pp_flags".
(temp_dump_context::get_dumped_text): New decl.
(class temp_dump_context): Add field "m_pp".
* dumpfile.c (refresh_dumps_are_enabled): Convert to...
(dump_context::refresh_dumps_are_enabled): ...and add a test for
m_test_pp.
(set_dump_file): Update for above change.
(set_alt_dump_file): Likewise.
(dump_loc): New overload, taking a pretty_printer *.
(dump_context::dump_loc): Call end_any_optinfo. Dump the location
to any test pretty-printer.
(make_item_for_dump_gimple_stmt): New function, adapted from
optinfo::add_gimple_stmt.
(dump_context::dump_gimple_stmt): Call it, and use the result,
eliminating the direct usage of dump_file and alt_dump_file in
favor of indirectly using them via emit_item.
(make_item_for_dump_gimple_expr): New function, adapted from
optinfo::add_gimple_expr.
(dump_context::dump_gimple_expr): Call it, and use the result,
eliminating the direct usage of dump_file and alt_dump_file in
favor of indirectly using them via emit_item.
(make_item_for_dump_generic_expr): New function, adapted from
optinfo::add_tree.
(dump_context::dump_generic_expr): Call it, and use the result,
eliminating the direct usage of dump_file and alt_dump_file in
favor of indirectly using them via emit_item.
(make_item_for_dump_printf_va): New function, adapted from
optinfo::add_printf_va.
(make_item_for_dump_printf): New function.
(dump_context::dump_printf_va): Call make_item_for_dump_printf_va,
and use the result, eliminating the direct usage of dump_file and
alt_dump_file in favor of indirectly using them via emit_item.
(make_item_for_dump_dec): New function.
(dump_context::dump_dec): Call it, and use the result,
eliminating the direct usage of dump_file and alt_dump_file in
favor of indirectly using them via emit_item.
(make_item_for_dump_symtab_node): New function, adapted from
optinfo::add_symtab_node.
(dump_context::dump_symtab_node): Call it, and use the result,
eliminating the direct usage of dump_file and alt_dump_file in
favor of indirectly using them via emit_item.
(dump_context::begin_scope): Reimplement, avoiding direct usage
of dump_file and alt_dump_file in favor of indirectly using them
via emit_item.
(dump_context::emit_item): New member function.
(temp_dump_context::temp_dump_context): Add param "test_pp_flags".
Set up test pretty-printer on the underlying context. Call
refresh_dumps_are_enabled.
(temp_dump_context::~temp_dump_context): Call
refresh_dumps_are_enabled.
(temp_dump_context::get_dumped_text): New member function.
(selftest::verify_dumped_text): New function.
(ASSERT_DUMPED_TEXT_EQ): New macro.
(selftest::test_capture_of_dump_calls): Run all tests twice, with
and then without optinfo enabled. Add uses of
ASSERT_DUMPED_TEXT_EQ to all tests. Add test of nested scopes.
* dumpfile.h: Update comment for the dump_* API.
* optinfo-emit-json.cc
(selftest::test_building_json_from_dump_calls): Update for new
param for temp_dump_context ctor.
* optinfo.cc (optinfo_item::optinfo_item): Remove "owned" param
and "m_owned" field.
(optinfo_item::~optinfo_item): Likewise.
(optinfo::add_item): New member function.
(optinfo::emit): Update comment.
(optinfo::add_string): Delete.
(optinfo::add_printf): Delete.
(optinfo::add_printf_va): Delete.
(optinfo::add_gimple_stmt): Delete.
(optinfo::add_gimple_expr): Delete.
(optinfo::add_tree): Delete.
(optinfo::add_symtab_node): Delete.
(optinfo::add_dec): Delete.
* optinfo.h (class dump_context): New forward decl.
(optinfo::add_item): New decl.
(optinfo::add_string): Delete.
(optinfo::add_printf): Delete.
(optinfo::add_printf_va): Delete.
(optinfo::add_gimple_stmt): Delete.
(optinfo::add_gimple_expr): Delete.
(optinfo::add_tree): Delete.
(optinfo::add_symtab_node): Delete.
(optinfo::add_dec): Delete.
(optinfo::add_poly_int): Delete.
(optinfo_item::optinfo_item): Remove "owned" param.
(class optinfo_item): Remove field "m_owned".
2018-07-31 Jozef Lawrynowicz <jozef.l@mittosystems.com>
PR middle-end/86705
......
......@@ -22,6 +22,8 @@ along with GCC; see the file COPYING3. If not see
#ifndef GCC_DUMP_CONTEXT_H
#define GCC_DUMP_CONTEXT_H 1
#include "pretty-print.h"
/* A class for handling the various dump_* calls.
In particular, this class has responsibility for consolidating
......@@ -39,6 +41,8 @@ class dump_context
~dump_context ();
void refresh_dumps_are_enabled ();
void dump_loc (dump_flags_t dump_kind, const dump_location_t &loc);
void dump_gimple_stmt (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
......@@ -93,6 +97,8 @@ class dump_context
void end_any_optinfo ();
void emit_item (optinfo_item *item, dump_flags_t dump_kind);
private:
optinfo &ensure_pending_optinfo ();
optinfo &begin_next_optinfo (const dump_location_t &loc);
......@@ -108,6 +114,11 @@ class dump_context
if any. */
optinfo *m_pending;
/* For use in selftests: if non-NULL, then items are to be printed
to this, using the given flags. */
pretty_printer *m_test_pp;
dump_flags_t m_test_pp_flags;
/* The currently active dump_context, for use by the dump_* API calls. */
static dump_context *s_current;
......@@ -123,13 +134,16 @@ class dump_context
class temp_dump_context
{
public:
temp_dump_context (bool forcibly_enable_optinfo);
temp_dump_context (bool forcibly_enable_optinfo,
dump_flags_t test_pp_flags);
~temp_dump_context ();
/* Support for selftests. */
optinfo *get_pending_optinfo () const { return m_context.m_pending; }
const char *get_dumped_text ();
private:
pretty_printer m_pp;
dump_context m_context;
dump_context *m_saved;
bool m_saved_flag_remarks;
......
......@@ -442,19 +442,27 @@ dump_enabled_p (void)
}
/* The following API calls (which *don't* take a "FILE *")
write the output to zero or more locations:
(a) the active dump_file, if any
(b) the -fopt-info destination, if any
(c) to the "optinfo" destinations, if any:
(c.1) as optimization records
dump_* (MSG_*) --> dumpfile.c --+--> (a) dump_file
|
+--> (b) alt_dump_file
|
`--> (c) optinfo
`---> optinfo destinations
(c.1) optimization records
write the output to zero or more locations.
Some destinations are written to immediately as dump_* calls
are made; for others, the output is consolidated into an "optinfo"
instance (with its own metadata), and only emitted once the optinfo
is complete.
The destinations are:
(a) the "immediate" destinations:
(a.1) the active dump_file, if any
(a.2) the -fopt-info destination, if any
(b) the "optinfo" destinations, if any:
(b.1) as optimization records
dump_* (MSG_*) --> dumpfile.c --> items --> (a.1) dump_file
| `-> (a.2) alt_dump_file
|
`--> (b) optinfo
`---> optinfo destinations
(b.1) optimization records
For optinfos, the dump_*_loc mark the beginning of an optinfo
instance: all subsequent dump_* calls are consolidated into
......
......@@ -537,7 +537,7 @@ namespace selftest {
static void
test_building_json_from_dump_calls ()
{
temp_dump_context tmp (true);
temp_dump_context tmp (true, MSG_NOTE);
dump_location_t loc;
dump_printf_loc (MSG_NOTE, loc, "test of tree: ");
dump_generic_expr (MSG_NOTE, TDF_SLIM, integer_zero_node);
......
......@@ -34,11 +34,11 @@ along with GCC; see the file COPYING3. If not see
#include "cgraph.h"
#include "selftest.h"
/* optinfo_item's ctor. */
/* optinfo_item's ctor. Takes ownership of TEXT. */
optinfo_item::optinfo_item (enum optinfo_item_kind kind, location_t location,
char *text, bool owned)
: m_kind (kind), m_location (location), m_text (text), m_owned (owned)
char *text)
: m_kind (kind), m_location (location), m_text (text)
{
}
......@@ -46,8 +46,7 @@ optinfo_item::optinfo_item (enum optinfo_item_kind kind, location_t location,
optinfo_item::~optinfo_item ()
{
if (m_owned)
free (m_text);
free (m_text);
}
/* Get a string from KIND. */
......@@ -81,7 +80,17 @@ optinfo::~optinfo ()
delete item;
}
/* Emit the optinfo to all of the active destinations. */
/* Add ITEM to this optinfo. */
void
optinfo::add_item (optinfo_item *item)
{
gcc_assert (item);
m_items.safe_push (item);
}
/* Emit the optinfo to all of the "non-immediate" destinations
(emission to "immediate" destinations is done by emit_item). */
void
optinfo::emit ()
......@@ -103,120 +112,6 @@ optinfo::handle_dump_file_kind (dump_flags_t dump_kind)
m_kind = OPTINFO_KIND_NOTE;
}
/* Append a string literal to this optinfo. */
void
optinfo::add_string (const char *str)
{
optinfo_item *item
= new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
const_cast <char *> (str), false);
m_items.safe_push (item);
}
/* Append printf-formatted text to this optinfo. */
void
optinfo::add_printf (const char *format, ...)
{
va_list ap;
va_start (ap, format);
add_printf_va (format, ap);
va_end (ap);
}
/* Append printf-formatted text to this optinfo. */
void
optinfo::add_printf_va (const char *format, va_list ap)
{
char *formatted_text = xvasprintf (format, ap);
optinfo_item *item
= new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
formatted_text, true);
m_items.safe_push (item);
}
/* Append a gimple statement to this optinfo, equivalent to
print_gimple_stmt. */
void
optinfo::add_gimple_stmt (gimple *stmt, int spc, dump_flags_t dump_flags)
{
pretty_printer pp;
pp_needs_newline (&pp) = true;
pp_gimple_stmt_1 (&pp, stmt, spc, dump_flags);
pp_newline (&pp);
optinfo_item *item
= new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE, gimple_location (stmt),
xstrdup (pp_formatted_text (&pp)), true);
m_items.safe_push (item);
}
/* Append a gimple statement to this optinfo, equivalent to
print_gimple_expr. */
void
optinfo::add_gimple_expr (gimple *stmt, int spc, dump_flags_t dump_flags)
{
dump_flags |= TDF_RHS_ONLY;
pretty_printer pp;
pp_needs_newline (&pp) = true;
pp_gimple_stmt_1 (&pp, stmt, spc, dump_flags);
optinfo_item *item
= new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE, gimple_location (stmt),
xstrdup (pp_formatted_text (&pp)), true);
m_items.safe_push (item);
}
/* Append a tree node to this optinfo, equivalent to print_generic_expr. */
void
optinfo::add_tree (tree node, dump_flags_t dump_flags)
{
pretty_printer pp;
pp_needs_newline (&pp) = true;
pp_translate_identifiers (&pp) = false;
dump_generic_node (&pp, node, 0, dump_flags, false);
location_t loc = UNKNOWN_LOCATION;
if (EXPR_HAS_LOCATION (node))
loc = EXPR_LOCATION (node);
optinfo_item *item
= new optinfo_item (OPTINFO_ITEM_KIND_TREE, loc,
xstrdup (pp_formatted_text (&pp)), true);
m_items.safe_push (item);
}
/* Append a symbol table node to this optinfo. */
void
optinfo::add_symtab_node (symtab_node *node)
{
location_t loc = DECL_SOURCE_LOCATION (node->decl);
optinfo_item *item
= new optinfo_item (OPTINFO_ITEM_KIND_SYMTAB_NODE, loc,
xstrdup (node->dump_name ()), true);
m_items.safe_push (item);
}
/* Append the decimal represenation of a wide_int_ref to this
optinfo. */
void
optinfo::add_dec (const wide_int_ref &wi, signop sgn)
{
char buf[WIDE_INT_PRINT_BUFFER_SIZE];
print_dec (wi, buf, sgn);
optinfo_item *item
= new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
xstrdup (buf), true);
m_items.safe_push (item);
}
/* Should optinfo instances be created?
All creation of optinfos should be guarded by this predicate.
Return true if any optinfo destinations are active. */
......
......@@ -92,6 +92,8 @@ enum optinfo_kind
extern const char *optinfo_kind_to_string (enum optinfo_kind kind);
class dump_context;
/* A bundle of information describing part of an optimization. */
class optinfo
......@@ -120,41 +122,14 @@ class optinfo
location_t get_location_t () const { return m_loc.get_location_t (); }
profile_count get_count () const { return m_loc.get_count (); }
void add_item (optinfo_item *item);
private:
void emit ();
/* Pre-canned ways of manipulating the optinfo, for use by friend class
dump_context. */
void handle_dump_file_kind (dump_flags_t);
void add_string (const char *str);
void add_printf (const char *format, ...) ATTRIBUTE_PRINTF_2;
void add_printf_va (const char *format, va_list ap) ATTRIBUTE_PRINTF (2, 0);
void add_gimple_stmt (gimple *stmt, int spc, dump_flags_t dump_flags);
void add_gimple_expr (gimple *stmt, int spc, dump_flags_t dump_flags);
void add_tree (tree node, dump_flags_t dump_flags);
void add_symtab_node (symtab_node *node);
void add_dec (const wide_int_ref &wi, signop sgn);
template<unsigned int N, typename C>
void add_poly_int (const poly_int<N, C> &value)
{
/* Compare with dump_dec (MSG_NOTE, ). */
STATIC_ASSERT (poly_coeff_traits<C>::signedness >= 0);
signop sgn = poly_coeff_traits<C>::signedness ? SIGNED : UNSIGNED;
if (value.is_constant ())
add_dec (value.coeffs[0], sgn);
else
{
add_string ("[");
for (unsigned int i = 0; i < N; ++i)
{
add_dec (value.coeffs[i], sgn);
add_string (i == N - 1 ? "]" : ",");
}
}
}
private:
dump_location_t m_loc;
......@@ -179,7 +154,7 @@ class optinfo_item
{
public:
optinfo_item (enum optinfo_item_kind kind, location_t location,
char *text, bool owned);
char *text);
~optinfo_item ();
enum optinfo_item_kind get_kind () const { return m_kind; }
......@@ -191,9 +166,8 @@ class optinfo_item
enum optinfo_item_kind m_kind;
location_t m_location;
/* The textual form of the item. */
/* The textual form of the item, owned by the item. */
char *m_text;
bool m_owned;
};
#endif /* #ifndef GCC_OPTINFO_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment