/* Optimization information.
- Copyright (C) 2018 Free Software Foundation, Inc.
+ Copyright (C) 2018-2020 Free Software Foundation, Inc.
Contributed by David Malcolm <dmalcolm@redhat.com>.
This file is part of GCC.
#include "gimple.h"
#include "optinfo.h"
+#include "optinfo-emit-json.h"
#include "dump-context.h"
#include "pretty-print.h"
#include "gimple-pretty-print.h"
#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)
{
}
optinfo_item::~optinfo_item ()
{
- if (m_owned)
- free (m_text);
+ free (m_text);
}
/* Get a string from KIND. */
delete item;
}
-/* Emit the optinfo to all of the active destinations. */
+/* Add ITEM to this optinfo. */
void
-optinfo::emit ()
+optinfo::add_item (optinfo_item *item)
{
- /* currently this is a no-op. */
-}
-
-/* Update the optinfo's kind based on DUMP_KIND. */
-
-void
-optinfo::handle_dump_file_kind (dump_flags_t dump_kind)
-{
- if (dump_kind & MSG_OPTIMIZED_LOCATIONS)
- m_kind = OPTINFO_KIND_SUCCESS;
- else if (dump_kind & MSG_MISSED_OPTIMIZATION)
- m_kind = OPTINFO_KIND_FAILURE;
- else if (dump_kind & MSG_NOTE)
- 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);
+ gcc_assert (item);
m_items.safe_push (item);
}
-/* Append a gimple statement to this optinfo, equivalent to
- print_gimple_expr. */
+/* Get MSG_* flags corresponding to KIND. */
-void
-optinfo::add_gimple_expr (gimple *stmt, int spc, dump_flags_t dump_flags)
+static dump_flags_t
+optinfo_kind_to_dump_flag (enum optinfo_kind kind)
{
- 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);
+ switch (kind)
+ {
+ default:
+ gcc_unreachable ();
+ case OPTINFO_KIND_SUCCESS:
+ return MSG_OPTIMIZED_LOCATIONS;
+ case OPTINFO_KIND_FAILURE:
+ return MSG_MISSED_OPTIMIZATION;
+ case OPTINFO_KIND_NOTE:
+ case OPTINFO_KIND_SCOPE:
+ return MSG_NOTE;
+ }
}
-/* Append a tree node to this optinfo, equivalent to print_generic_expr. */
+/* Re-emit this optinfo, both to the "non-immediate" destinations,
+ *and* to the "immediate" destinations. */
void
-optinfo::add_tree (tree node, dump_flags_t dump_flags)
+optinfo::emit_for_opt_problem () const
{
- 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);
-}
+ dump_flags_t dump_kind = optinfo_kind_to_dump_flag (get_kind ());
+ dump_kind |= MSG_PRIORITY_REEMITTED;
-/* Append a symbol table node to this optinfo. */
+ /* Re-emit to "immediate" destinations, without creating a new optinfo. */
+ dump_context::get ().dump_loc_immediate (dump_kind, get_user_location ());
+ unsigned i;
+ optinfo_item *item;
+ FOR_EACH_VEC_ELT (m_items, i, item)
+ dump_context::get ().emit_item (item, dump_kind);
-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);
+ /* Re-emit to "non-immediate" destinations. */
+ dump_context::get ().emit_optinfo (this);
}
-/* Append the decimal represenation of a wide_int_ref to this
- optinfo. */
+/* Update the optinfo's kind based on DUMP_KIND. */
void
-optinfo::add_dec (const wide_int_ref &wi, signop sgn)
+optinfo::handle_dump_file_kind (dump_flags_t dump_kind)
{
- 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);
-}
+ /* Any optinfo for a "scope" should have been emitted separately. */
+ gcc_assert (m_kind != OPTINFO_KIND_SCOPE);
-/* Should optinfo instances be created?
- All creation of optinfos should be guarded by this predicate.
- Return true if any optinfo destinations are active. */
-
-bool optinfo_enabled_p ()
-{
- /* Currently no destinations are implemented, just a hook for
- selftests. */
- return dump_context::get ().forcibly_enable_optinfo_p ();
+ if (dump_kind & MSG_OPTIMIZED_LOCATIONS)
+ m_kind = OPTINFO_KIND_SUCCESS;
+ else if (dump_kind & MSG_MISSED_OPTIMIZATION)
+ m_kind = OPTINFO_KIND_FAILURE;
+ else if (dump_kind & MSG_NOTE)
+ m_kind = OPTINFO_KIND_NOTE;
}
/* Return true if any of the active optinfo destinations make use
bool optinfo_wants_inlining_info_p ()
{
- return false;
+ return dump_context::get ().optimization_records_enabled_p ();
}