1*38fd1498Szrj /* IPA function body analysis.
2*38fd1498Szrj Copyright (C) 2003-2018 Free Software Foundation, Inc.
3*38fd1498Szrj Contributed by Jan Hubicka
4*38fd1498Szrj
5*38fd1498Szrj This file is part of GCC.
6*38fd1498Szrj
7*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
8*38fd1498Szrj the terms of the GNU General Public License as published by the Free
9*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
10*38fd1498Szrj version.
11*38fd1498Szrj
12*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15*38fd1498Szrj for more details.
16*38fd1498Szrj
17*38fd1498Szrj You should have received a copy of the GNU General Public License
18*38fd1498Szrj along with GCC; see the file COPYING3. If not see
19*38fd1498Szrj <http://www.gnu.org/licenses/>. */
20*38fd1498Szrj
21*38fd1498Szrj #ifndef GCC_IPA_SUMMARY_H
22*38fd1498Szrj #define GCC_IPA_SUMMARY_H
23*38fd1498Szrj
24*38fd1498Szrj #include "sreal.h"
25*38fd1498Szrj #include "ipa-predicate.h"
26*38fd1498Szrj
27*38fd1498Szrj
28*38fd1498Szrj /* Hints are reasons why IPA heuristics should preffer specializing given
29*38fd1498Szrj function. They are represtented as bitmap of the following values. */
30*38fd1498Szrj enum ipa_hints_vals {
31*38fd1498Szrj /* When specialization turns indirect call into a direct call,
32*38fd1498Szrj it is good idea to do so. */
33*38fd1498Szrj INLINE_HINT_indirect_call = 1,
34*38fd1498Szrj /* Inlining may make loop iterations or loop stride known. It is good idea
35*38fd1498Szrj to do so because it enables loop optimizatoins. */
36*38fd1498Szrj INLINE_HINT_loop_iterations = 2,
37*38fd1498Szrj INLINE_HINT_loop_stride = 4,
38*38fd1498Szrj /* Inlining within same strongly connected component of callgraph is often
39*38fd1498Szrj a loss due to increased stack frame usage and prologue setup costs. */
40*38fd1498Szrj INLINE_HINT_same_scc = 8,
41*38fd1498Szrj /* Inlining functions in strongly connected component is not such a great
42*38fd1498Szrj win. */
43*38fd1498Szrj INLINE_HINT_in_scc = 16,
44*38fd1498Szrj /* If function is declared inline by user, it may be good idea to inline
45*38fd1498Szrj it. Set by simple_edge_hints in ipa-inline-analysis.c. */
46*38fd1498Szrj INLINE_HINT_declared_inline = 32,
47*38fd1498Szrj /* Programs are usually still organized for non-LTO compilation and thus
48*38fd1498Szrj if functions are in different modules, inlining may not be so important.
49*38fd1498Szrj Set by simple_edge_hints in ipa-inline-analysis.c. */
50*38fd1498Szrj INLINE_HINT_cross_module = 64,
51*38fd1498Szrj /* If array indexes of loads/stores become known there may be room for
52*38fd1498Szrj further optimization. */
53*38fd1498Szrj INLINE_HINT_array_index = 128,
54*38fd1498Szrj /* We know that the callee is hot by profile. */
55*38fd1498Szrj INLINE_HINT_known_hot = 256
56*38fd1498Szrj };
57*38fd1498Szrj
58*38fd1498Szrj typedef int ipa_hints;
59*38fd1498Szrj
60*38fd1498Szrj /* Simple description of whether a memory load or a condition refers to a load
61*38fd1498Szrj from an aggregate and if so, how and where from in the aggregate.
62*38fd1498Szrj Individual fields have the same meaning like fields with the same name in
63*38fd1498Szrj struct condition. */
64*38fd1498Szrj
65*38fd1498Szrj struct agg_position_info
66*38fd1498Szrj {
67*38fd1498Szrj HOST_WIDE_INT offset;
68*38fd1498Szrj bool agg_contents;
69*38fd1498Szrj bool by_ref;
70*38fd1498Szrj };
71*38fd1498Szrj
72*38fd1498Szrj /* Representation of function body size and time depending on the call
73*38fd1498Szrj context. We keep simple array of record, every containing of predicate
74*38fd1498Szrj and time/size to account. */
75*38fd1498Szrj struct GTY(()) size_time_entry
76*38fd1498Szrj {
77*38fd1498Szrj /* Predicate for code to be executed. */
78*38fd1498Szrj predicate exec_predicate;
79*38fd1498Szrj /* Predicate for value to be constant and optimized out in a specialized copy.
80*38fd1498Szrj When deciding on specialization this makes it possible to see how much
81*38fd1498Szrj the executed code paths will simplify. */
82*38fd1498Szrj predicate nonconst_predicate;
83*38fd1498Szrj int size;
84*38fd1498Szrj sreal GTY((skip)) time;
85*38fd1498Szrj };
86*38fd1498Szrj
87*38fd1498Szrj /* Function inlining information. */
88*38fd1498Szrj struct GTY(()) ipa_fn_summary
89*38fd1498Szrj {
90*38fd1498Szrj /* Information about the function body itself. */
91*38fd1498Szrj
92*38fd1498Szrj /* Estimated stack frame consumption by the function. */
93*38fd1498Szrj HOST_WIDE_INT estimated_self_stack_size;
94*38fd1498Szrj /* Size of the function body. */
95*38fd1498Szrj int self_size;
96*38fd1498Szrj /* Minimal size increase after inlining. */
97*38fd1498Szrj int min_size;
98*38fd1498Szrj
99*38fd1498Szrj /* False when there something makes inlining impossible (such as va_arg). */
100*38fd1498Szrj unsigned inlinable : 1;
101*38fd1498Szrj /* True wen there is only one caller of the function before small function
102*38fd1498Szrj inlining. */
103*38fd1498Szrj unsigned int single_caller : 1;
104*38fd1498Szrj /* True if function contains any floating point expressions. */
105*38fd1498Szrj unsigned int fp_expressions : 1;
106*38fd1498Szrj
107*38fd1498Szrj /* Information about function that will result after applying all the
108*38fd1498Szrj inline decisions present in the callgraph. Generally kept up to
109*38fd1498Szrj date only for functions that are not inline clones. */
110*38fd1498Szrj
111*38fd1498Szrj /* Estimated stack frame consumption by the function. */
112*38fd1498Szrj HOST_WIDE_INT estimated_stack_size;
113*38fd1498Szrj /* Expected offset of the stack frame of function. */
114*38fd1498Szrj HOST_WIDE_INT stack_frame_offset;
115*38fd1498Szrj /* Estimated size of the function after inlining. */
116*38fd1498Szrj sreal GTY((skip)) time;
117*38fd1498Szrj int size;
118*38fd1498Szrj
119*38fd1498Szrj /* Conditional size/time information. The summaries are being
120*38fd1498Szrj merged during inlining. */
121*38fd1498Szrj conditions conds;
122*38fd1498Szrj vec<size_time_entry, va_gc> *size_time_table;
123*38fd1498Szrj
124*38fd1498Szrj /* Predicate on when some loop in the function becomes to have known
125*38fd1498Szrj bounds. */
126*38fd1498Szrj predicate * GTY((skip)) loop_iterations;
127*38fd1498Szrj /* Predicate on when some loop in the function becomes to have known
128*38fd1498Szrj stride. */
129*38fd1498Szrj predicate * GTY((skip)) loop_stride;
130*38fd1498Szrj /* Predicate on when some array indexes become constants. */
131*38fd1498Szrj predicate * GTY((skip)) array_index;
132*38fd1498Szrj /* Estimated growth for inlining all copies of the function before start
133*38fd1498Szrj of small functions inlining.
134*38fd1498Szrj This value will get out of date as the callers are duplicated, but
135*38fd1498Szrj using up-to-date value in the badness metric mean a lot of extra
136*38fd1498Szrj expenses. */
137*38fd1498Szrj int growth;
138*38fd1498Szrj /* Number of SCC on the beginning of inlining process. */
139*38fd1498Szrj int scc_no;
140*38fd1498Szrj
141*38fd1498Szrj /* Keep all field empty so summary dumping works during its computation.
142*38fd1498Szrj This is useful for debugging. */
ipa_fn_summaryipa_fn_summary143*38fd1498Szrj ipa_fn_summary ()
144*38fd1498Szrj : estimated_self_stack_size (0), self_size (0), min_size (0),
145*38fd1498Szrj inlinable (false), single_caller (false),
146*38fd1498Szrj fp_expressions (false), estimated_stack_size (false),
147*38fd1498Szrj stack_frame_offset (false), time (0), size (0), conds (NULL),
148*38fd1498Szrj size_time_table (NULL), loop_iterations (NULL), loop_stride (NULL),
149*38fd1498Szrj array_index (NULL), growth (0), scc_no (0)
150*38fd1498Szrj {
151*38fd1498Szrj }
152*38fd1498Szrj
153*38fd1498Szrj /* Record time and size under given predicates. */
154*38fd1498Szrj void account_size_time (int, sreal, const predicate &, const predicate &);
155*38fd1498Szrj
156*38fd1498Szrj /* Reset summary to empty state. */
157*38fd1498Szrj void reset (struct cgraph_node *node);
158*38fd1498Szrj
159*38fd1498Szrj /* We keep values scaled up, so fractional sizes can be accounted. */
160*38fd1498Szrj static const int size_scale = 2;
161*38fd1498Szrj };
162*38fd1498Szrj
class(user)163*38fd1498Szrj class GTY((user)) ipa_fn_summary_t: public function_summary <ipa_fn_summary *>
164*38fd1498Szrj {
165*38fd1498Szrj public:
166*38fd1498Szrj ipa_fn_summary_t (symbol_table *symtab, bool ggc):
167*38fd1498Szrj function_summary <ipa_fn_summary *> (symtab, ggc) {}
168*38fd1498Szrj
169*38fd1498Szrj static ipa_fn_summary_t *create_ggc (symbol_table *symtab)
170*38fd1498Szrj {
171*38fd1498Szrj struct ipa_fn_summary_t *summary = new (ggc_alloc <ipa_fn_summary_t> ())
172*38fd1498Szrj ipa_fn_summary_t(symtab, true);
173*38fd1498Szrj summary->disable_insertion_hook ();
174*38fd1498Szrj return summary;
175*38fd1498Szrj }
176*38fd1498Szrj
177*38fd1498Szrj
178*38fd1498Szrj virtual void insert (cgraph_node *, ipa_fn_summary *);
179*38fd1498Szrj virtual void remove (cgraph_node *node, ipa_fn_summary *);
180*38fd1498Szrj virtual void duplicate (cgraph_node *src, cgraph_node *dst,
181*38fd1498Szrj ipa_fn_summary *src_data, ipa_fn_summary *dst_data);
182*38fd1498Szrj };
183*38fd1498Szrj
184*38fd1498Szrj extern GTY(()) function_summary <ipa_fn_summary *> *ipa_fn_summaries;
185*38fd1498Szrj
186*38fd1498Szrj /* Information kept about callgraph edges. */
187*38fd1498Szrj struct ipa_call_summary
188*38fd1498Szrj {
189*38fd1498Szrj class predicate *predicate;
190*38fd1498Szrj /* Vector indexed by parameters. */
191*38fd1498Szrj vec<inline_param_summary> param;
192*38fd1498Szrj /* Estimated size and time of the call statement. */
193*38fd1498Szrj int call_stmt_size;
194*38fd1498Szrj int call_stmt_time;
195*38fd1498Szrj /* Depth of loop nest, 0 means no nesting. */
196*38fd1498Szrj unsigned int loop_depth;
197*38fd1498Szrj /* Indicates whether the caller returns the value of it's callee. */
198*38fd1498Szrj bool is_return_callee_uncaptured;
199*38fd1498Szrj
200*38fd1498Szrj /* Keep all field empty so summary dumping works during its computation.
201*38fd1498Szrj This is useful for debugging. */
ipa_call_summaryipa_call_summary202*38fd1498Szrj ipa_call_summary ()
203*38fd1498Szrj : predicate (NULL), param (vNULL), call_stmt_size (0), call_stmt_time (0),
204*38fd1498Szrj loop_depth (0)
205*38fd1498Szrj {
206*38fd1498Szrj }
207*38fd1498Szrj
208*38fd1498Szrj /* Reset inline summary to empty state. */
209*38fd1498Szrj void reset ();
210*38fd1498Szrj };
211*38fd1498Szrj
212*38fd1498Szrj class ipa_call_summary_t: public call_summary <ipa_call_summary *>
213*38fd1498Szrj {
214*38fd1498Szrj public:
ipa_call_summary_t(symbol_table * symtab,bool ggc)215*38fd1498Szrj ipa_call_summary_t (symbol_table *symtab, bool ggc):
216*38fd1498Szrj call_summary <ipa_call_summary *> (symtab, ggc) {}
217*38fd1498Szrj
218*38fd1498Szrj /* Hook that is called by summary when an edge is duplicated. */
219*38fd1498Szrj virtual void remove (cgraph_edge *cs, ipa_call_summary *);
220*38fd1498Szrj /* Hook that is called by summary when an edge is duplicated. */
221*38fd1498Szrj virtual void duplicate (cgraph_edge *src, cgraph_edge *dst,
222*38fd1498Szrj ipa_call_summary *src_data,
223*38fd1498Szrj ipa_call_summary *dst_data);
224*38fd1498Szrj };
225*38fd1498Szrj
226*38fd1498Szrj extern call_summary <ipa_call_summary *> *ipa_call_summaries;
227*38fd1498Szrj
228*38fd1498Szrj /* In ipa-fnsummary.c */
229*38fd1498Szrj void ipa_debug_fn_summary (struct cgraph_node *);
230*38fd1498Szrj void ipa_dump_fn_summaries (FILE *f);
231*38fd1498Szrj void ipa_dump_fn_summary (FILE *f, struct cgraph_node *node);
232*38fd1498Szrj void ipa_dump_hints (FILE *f, ipa_hints);
233*38fd1498Szrj void ipa_free_fn_summary (void);
234*38fd1498Szrj void inline_analyze_function (struct cgraph_node *node);
235*38fd1498Szrj void estimate_ipcp_clone_size_and_time (struct cgraph_node *,
236*38fd1498Szrj vec<tree>,
237*38fd1498Szrj vec<ipa_polymorphic_call_context>,
238*38fd1498Szrj vec<ipa_agg_jump_function_p>,
239*38fd1498Szrj int *, sreal *, sreal *,
240*38fd1498Szrj ipa_hints *);
241*38fd1498Szrj void ipa_merge_fn_summary_after_inlining (struct cgraph_edge *edge);
242*38fd1498Szrj void ipa_update_overall_fn_summary (struct cgraph_node *node);
243*38fd1498Szrj void compute_fn_summary (struct cgraph_node *, bool);
244*38fd1498Szrj
245*38fd1498Szrj
246*38fd1498Szrj void evaluate_properties_for_edge (struct cgraph_edge *e, bool inline_p,
247*38fd1498Szrj clause_t *clause_ptr,
248*38fd1498Szrj clause_t *nonspec_clause_ptr,
249*38fd1498Szrj vec<tree> *known_vals_ptr,
250*38fd1498Szrj vec<ipa_polymorphic_call_context>
251*38fd1498Szrj *known_contexts_ptr,
252*38fd1498Szrj vec<ipa_agg_jump_function_p> *);
253*38fd1498Szrj void estimate_node_size_and_time (struct cgraph_node *node,
254*38fd1498Szrj clause_t possible_truths,
255*38fd1498Szrj clause_t nonspec_possible_truths,
256*38fd1498Szrj vec<tree> known_vals,
257*38fd1498Szrj vec<ipa_polymorphic_call_context>,
258*38fd1498Szrj vec<ipa_agg_jump_function_p> known_aggs,
259*38fd1498Szrj int *ret_size, int *ret_min_size,
260*38fd1498Szrj sreal *ret_time,
261*38fd1498Szrj sreal *ret_nonspecialized_time,
262*38fd1498Szrj ipa_hints *ret_hints,
263*38fd1498Szrj vec<inline_param_summary>
264*38fd1498Szrj inline_param_summary);
265*38fd1498Szrj
266*38fd1498Szrj void ipa_fnsummary_c_finalize (void);
267*38fd1498Szrj
268*38fd1498Szrj #endif /* GCC_IPA_FNSUMMARY_H */
269