1/* Optimization information.
2   Copyright (C) 2018-2020 Free Software Foundation, Inc.
3   Contributed by David Malcolm <dmalcolm@redhat.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3.  If not see
19<http://www.gnu.org/licenses/>.  */
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
24
25#include "backend.h"
26#include "tree.h"
27#include "gimple.h"
28
29#include "optinfo.h"
30#include "optinfo-emit-json.h"
31#include "dump-context.h"
32#include "pretty-print.h"
33#include "gimple-pretty-print.h"
34#include "cgraph.h"
35#include "selftest.h"
36
37/* optinfo_item's ctor.  Takes ownership of TEXT.  */
38
39optinfo_item::optinfo_item (enum optinfo_item_kind kind, location_t location,
40			    char *text)
41: m_kind (kind), m_location (location), m_text (text)
42{
43}
44
45/* optinfo_item's dtor.  */
46
47optinfo_item::~optinfo_item ()
48{
49  free (m_text);
50}
51
52/* Get a string from KIND.  */
53
54const char *
55optinfo_kind_to_string (enum optinfo_kind kind)
56{
57  switch (kind)
58    {
59    default:
60      gcc_unreachable ();
61    case OPTINFO_KIND_SUCCESS:
62      return "success";
63    case OPTINFO_KIND_FAILURE:
64      return "failure";
65    case OPTINFO_KIND_NOTE:
66      return "note";
67    case OPTINFO_KIND_SCOPE:
68      return "scope";
69    }
70}
71
72/* optinfo's dtor.  */
73
74optinfo::~optinfo ()
75{
76  /* Cleanup.  */
77  unsigned i;
78  optinfo_item *item;
79  FOR_EACH_VEC_ELT (m_items, i, item)
80    delete item;
81}
82
83/* Add ITEM to this optinfo.  */
84
85void
86optinfo::add_item (optinfo_item *item)
87{
88  gcc_assert (item);
89  m_items.safe_push (item);
90}
91
92/* Get MSG_* flags corresponding to KIND.  */
93
94static dump_flags_t
95optinfo_kind_to_dump_flag (enum optinfo_kind kind)
96{
97  switch (kind)
98    {
99    default:
100      gcc_unreachable ();
101    case OPTINFO_KIND_SUCCESS:
102      return MSG_OPTIMIZED_LOCATIONS;
103    case OPTINFO_KIND_FAILURE:
104      return MSG_MISSED_OPTIMIZATION;
105    case OPTINFO_KIND_NOTE:
106    case OPTINFO_KIND_SCOPE:
107      return MSG_NOTE;
108    }
109}
110
111/* Re-emit this optinfo, both to the "non-immediate" destinations,
112   *and* to the "immediate" destinations.  */
113
114void
115optinfo::emit_for_opt_problem () const
116{
117  dump_flags_t dump_kind = optinfo_kind_to_dump_flag (get_kind ());
118  dump_kind |= MSG_PRIORITY_REEMITTED;
119
120  /* Re-emit to "immediate" destinations, without creating a new optinfo.  */
121  dump_context::get ().dump_loc_immediate (dump_kind, get_user_location ());
122  unsigned i;
123  optinfo_item *item;
124  FOR_EACH_VEC_ELT (m_items, i, item)
125    dump_context::get ().emit_item (item, dump_kind);
126
127  /* Re-emit to "non-immediate" destinations.  */
128  dump_context::get ().emit_optinfo (this);
129}
130
131/* Update the optinfo's kind based on DUMP_KIND.  */
132
133void
134optinfo::handle_dump_file_kind (dump_flags_t dump_kind)
135{
136  /* Any optinfo for a "scope" should have been emitted separately.  */
137  gcc_assert (m_kind != OPTINFO_KIND_SCOPE);
138
139  if (dump_kind & MSG_OPTIMIZED_LOCATIONS)
140    m_kind = OPTINFO_KIND_SUCCESS;
141  else if (dump_kind & MSG_MISSED_OPTIMIZATION)
142    m_kind = OPTINFO_KIND_FAILURE;
143  else if (dump_kind & MSG_NOTE)
144    m_kind = OPTINFO_KIND_NOTE;
145}
146
147/* Return true if any of the active optinfo destinations make use
148   of inlining information.
149   (if true, then the information is preserved).  */
150
151bool optinfo_wants_inlining_info_p ()
152{
153  return dump_context::get ().optimization_records_enabled_p ();
154}
155