| 1 |
/* A class to encapsulate decisions about how the analysis should happen. |
| 2 |
Copyright (C) 2019-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by David Malcolm <[email protected]>. |
| 4 |
|
| 5 |
This file is part of GCC. |
| 6 |
|
| 7 |
GCC is free software; you can redistribute it and/or modify it |
| 8 |
under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 3, or (at your option) |
| 10 |
any later version. |
| 11 |
|
| 12 |
GCC is distributed in the hope that it will be useful, but |
| 13 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along 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 |
#include "tree.h" |
| 25 |
#include "options.h" |
| 26 |
#include "cgraph.h" |
| 27 |
#include "timevar.h" |
| 28 |
#include "ipa-utils.h" |
| 29 |
#include "function.h" |
| 30 |
#include "analyzer/analyzer.h" |
| 31 |
#include "diagnostic-core.h" |
| 32 |
#include "analyzer/analyzer-logging.h" |
| 33 |
#include "analyzer/analysis-plan.h" |
| 34 |
#include "ordered-hash-map.h" |
| 35 |
#include "options.h" |
| 36 |
#include "cgraph.h" |
| 37 |
#include "cfg.h" |
| 38 |
#include "basic-block.h" |
| 39 |
#include "gimple.h" |
| 40 |
#include "gimple-iterator.h" |
| 41 |
#include "digraph.h" |
| 42 |
#include "analyzer/supergraph.h" |
| 43 |
|
| 44 |
#if ENABLE_ANALYZER |
| 45 |
|
| 46 |
/* class analysis_plan. */ |
| 47 |
|
| 48 |
/* analysis_plan's ctor. */ |
| 49 |
|
| 50 |
analysis_plan::analysis_plan (const supergraph &sg, logger *logger) |
| 51 |
: log_user (logger), m_sg (sg), |
| 52 |
m_cgraph_node_postorder (XCNEWVEC (struct cgraph_node *, |
| 53 |
symtab->cgraph_count)), |
| 54 |
m_index_by_uid (symtab->cgraph_max_uid) |
| 55 |
{ |
| 56 |
LOG_SCOPE (logger); |
| 57 |
auto_timevar time (TV_ANALYZER_PLAN); |
| 58 |
|
| 59 |
m_num_cgraph_nodes = ipa_reverse_postorder (m_cgraph_node_postorder); |
| 60 |
gcc_assert (m_num_cgraph_nodes == symtab->cgraph_count); |
| 61 |
if (get_logger_file ()) |
| 62 |
ipa_print_order (get_logger_file (), |
| 63 |
"analysis_plan", m_cgraph_node_postorder, |
| 64 |
m_num_cgraph_nodes); |
| 65 |
|
| 66 |
/* Populate m_index_by_uid. */ |
| 67 |
for (int i = 0; i < symtab->cgraph_max_uid; i++) |
| 68 |
m_index_by_uid.quick_push (-1); |
| 69 |
for (int i = 0; i < m_num_cgraph_nodes; i++) |
| 70 |
{ |
| 71 |
gcc_assert (m_cgraph_node_postorder[i]->get_uid () |
| 72 |
< symtab->cgraph_max_uid); |
| 73 |
m_index_by_uid[m_cgraph_node_postorder[i]->get_uid ()] = i; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/* analysis_plan's dtor. */ |
| 78 |
|
| 79 |
analysis_plan::~analysis_plan () |
| 80 |
{ |
| 81 |
free (m_cgraph_node_postorder); |
| 82 |
} |
| 83 |
|
| 84 |
/* Comparator for use by the exploded_graph's worklist, to order FUN_A |
| 85 |
and FUN_B so that functions that are to be summarized are visited |
| 86 |
before the summary is needed (based on a sort of the callgraph). */ |
| 87 |
|
| 88 |
int |
| 89 |
analysis_plan::cmp_function (function *fun_a, function *fun_b) const |
| 90 |
{ |
| 91 |
cgraph_node *node_a = cgraph_node::get (fun_a->decl); |
| 92 |
cgraph_node *node_b = cgraph_node::get (fun_b->decl); |
| 93 |
|
| 94 |
int idx_a = m_index_by_uid[node_a->get_uid ()]; |
| 95 |
int idx_b = m_index_by_uid[node_b->get_uid ()]; |
| 96 |
|
| 97 |
return idx_b - idx_a; |
| 98 |
} |
| 99 |
|
| 100 |
/* Return true if the call EDGE should be analyzed using a call summary. |
| 101 |
Return false if it should be analyzed using a full call and return. */ |
| 102 |
|
| 103 |
bool |
| 104 |
analysis_plan::use_summary_p (const cgraph_edge *edge) const |
| 105 |
{ |
| 106 |
/* Don't use call summaries if -fno-analyzer-call-summaries. */ |
| 107 |
if (!flag_analyzer_call_summaries) |
| 108 |
return false; |
| 109 |
|
| 110 |
/* Don't use call summaries if there is no callgraph edge */ |
| 111 |
if (!edge || !edge->callee) |
| 112 |
return false; |
| 113 |
|
| 114 |
/* TODO: don't count callsites each time. */ |
| 115 |
int num_call_sites = 0; |
| 116 |
const cgraph_node *callee = edge->callee; |
| 117 |
for (cgraph_edge *edge = callee->callers; edge; edge = edge->next_caller) |
| 118 |
++num_call_sites; |
| 119 |
|
| 120 |
/* Don't use a call summary if there's only one call site. */ |
| 121 |
if (num_call_sites <= 1) |
| 122 |
return false; |
| 123 |
|
| 124 |
/* Require the callee to be sufficiently complex to be worth |
| 125 |
summarizing. */ |
| 126 |
const function *fun |
| 127 |
= const_cast <cgraph_node *> (callee)->ultimate_alias_target ()->get_fun (); |
| 128 |
/* TODO(stage1): can ultimate_alias_target be made const? */ |
| 129 |
|
| 130 |
if ((int)m_sg.get_num_snodes (fun) |
| 131 |
< param_analyzer_min_snodes_for_call_summary) |
| 132 |
return false; |
| 133 |
|
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
#endif /* #if ENABLE_ANALYZER */ |