Line data Source code
1 : /* IPA predicates.
2 : Copyright (C) 2003-2026 Free Software Foundation, Inc.
3 : Contributed by Jan Hubicka
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
10 : version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : 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 : /* Representation of inline parameters that do depend on context function is
22 : inlined into (i.e. known constant values of function parameters.
23 :
24 : Conditions that are interesting for function body are collected into CONDS
25 : vector. They are of simple as kind of a mathematical transformation on
26 : function parameter, T(function_param), in which the parameter occurs only
27 : once, and other operands are IPA invariant. The conditions are then
28 : referred by predicates. */
29 :
30 :
31 : /* A simplified representation of tree node, for unary, binary and ternary
32 : operation. Computations on parameter are decomposed to a series of this
33 : kind of structure. */
34 : struct GTY(()) expr_eval_op
35 : {
36 : /* Result type of expression. */
37 : tree type;
38 : /* Constant operands in expression, there are at most two. */
39 : tree val[2];
40 : /* Index of parameter operand in expression. */
41 : unsigned index : 2;
42 : /* Operation code of expression. */
43 : tree_code code : 16;
44 : };
45 :
46 : typedef vec<expr_eval_op, va_gc> *expr_eval_ops;
47 :
48 : struct GTY(()) condition
49 : {
50 : /* If agg_contents is set, this is the offset from which the used data was
51 : loaded, or -1 if unknown_offset is set. */
52 : HOST_WIDE_INT offset;
53 : /* Type of the access reading the data (or the PARM_DECL SSA_NAME). */
54 : tree type;
55 : tree val;
56 : int operand_num;
57 : tree_code code : 16;
58 : /* Set if the used data were loaded from an aggregate parameter or from
59 : data received by reference. */
60 : unsigned agg_contents : 1;
61 : /* If agg_contents is set, this differentiates between loads from data
62 : passed by reference and by value. */
63 : unsigned by_ref : 1;
64 : /* If by_ref is set, this is set when we cannot prove that the value
65 : pointed to is unmodified from the beginning of the function to use. */
66 : unsigned maybe_modified : 1;
67 : /* If agg_contents is set, this is set when the offset is unknown. */
68 : unsigned unknown_offset : 1;
69 : /* A set of sequential operations on the parameter, which can be seen as
70 : a mathematical function on the parameter. */
71 : expr_eval_ops param_ops;
72 : };
73 :
74 : /* Information kept about parameter of call site. */
75 : struct inline_param_summary
76 : {
77 : /* REG_BR_PROB_BASE based probability that parameter will change in between
78 : two invocation of the calls.
79 : I.e. loop invariant parameters
80 : REG_BR_PROB_BASE/estimated_iterations and regular
81 : parameters REG_BR_PROB_BASE.
82 :
83 : Value 0 is reserved for compile time invariants. */
84 : short change_prob;
85 : unsigned points_to_local_or_readonly_memory : 1;
86 : unsigned points_to_possible_sra_candidate : 1;
87 3865983 : bool equal_to (const inline_param_summary &other) const
88 : {
89 3865983 : return change_prob == other.change_prob
90 3844586 : && points_to_local_or_readonly_memory
91 3844586 : == other.points_to_local_or_readonly_memory
92 3865983 : && points_to_possible_sra_candidate
93 3835309 : == other.points_to_possible_sra_candidate;
94 : }
95 14503046 : bool useless_p (void) const
96 : {
97 14503046 : return change_prob == REG_BR_PROB_BASE
98 5753540 : && !points_to_local_or_readonly_memory
99 20065935 : && !points_to_possible_sra_candidate;
100 : }
101 : };
102 :
103 : typedef vec<condition, va_gc> *conditions;
104 :
105 : /* Predicates are used to represent function parameters (such as runtime)
106 : which depend on a context function is called in.
107 :
108 : Predicates are logical formulas in conjunctive-disjunctive form consisting
109 : of clauses which are bitmaps specifying a set of condition that must
110 : be true for a clause to be satisfied. Physically they are represented as
111 : array of clauses terminated by 0.
112 :
113 : In order to make predicate (possibly) true, all of its clauses must
114 : be (possibly) true. To make clause (possibly) true, one of conditions
115 : it mentions must be (possibly) true.
116 :
117 : There are fixed bounds on number of clauses and conditions and all the
118 : manipulation functions are conservative in positive direction. I.e. we
119 : may lose precision by thinking that predicate may be true even when it
120 : is not. */
121 :
122 : typedef uint32_t clause_t;
123 : class ipa_predicate
124 : {
125 : public:
126 : enum predicate_conditions
127 : {
128 : false_condition = 0,
129 : not_inlined_condition = 1,
130 : first_dynamic_condition = 2
131 : };
132 :
133 : /* Maximal number of conditions predicate can refer to. This is limited
134 : by using clause_t to be 32bit. */
135 : static const int num_conditions = 32;
136 :
137 : /* Special condition code we use to represent test that operand is compile
138 : time constant. */
139 : static const tree_code is_not_constant = ERROR_MARK;
140 :
141 : /* Special condition code we use to represent test that operand is not changed
142 : across invocation of the function. When operand IS_NOT_CONSTANT it is
143 : always CHANGED, however i.e. loop invariants can be NOT_CHANGED given
144 : percentage of executions even when they are not compile time constants. */
145 : static const tree_code changed = IDENTIFIER_NODE;
146 :
147 : /* Special condition code we use to check that the parameter is likely SRA
148 : candidate. */
149 : static const tree_code not_sra_candidate = TREE_LIST;
150 :
151 :
152 : /* Initialize predicate either to true of false depending on P. */
153 1187914967 : inline ipa_predicate (bool p = true)
154 : {
155 1187914967 : if (p)
156 : /* True predicate. */
157 973725730 : m_clause[0] = 0;
158 : else
159 : /* False predicate. */
160 145476846 : set_to_cond (false_condition);
161 : }
162 :
163 : /* Sanity check that we do not mix pointers to predicates with predicates. */
164 : inline ipa_predicate (ipa_predicate *)
165 : {
166 : gcc_unreachable ();
167 : }
168 :
169 : /* Return predicate testing condition I. */
170 26303124 : static inline ipa_predicate predicate_testing_cond (int i)
171 : {
172 26303124 : ipa_predicate p;
173 26303124 : p.set_to_cond (i + first_dynamic_condition);
174 26303124 : return p;
175 : }
176 :
177 : /* Return predicate testing that function was not inlined. */
178 22959716 : static ipa_predicate not_inlined (void)
179 : {
180 22959716 : ipa_predicate p;
181 22959716 : p.set_to_cond (not_inlined_condition);
182 7479821 : return p;
183 : }
184 :
185 : /* Compute logical and of ipa_predicates. */
186 : ipa_predicate & operator &= (const ipa_predicate &);
187 374012859 : inline ipa_predicate operator &(const ipa_predicate &p) const
188 : {
189 374012859 : ipa_predicate ret = *this;
190 358532964 : ret &= p;
191 358532964 : return ret;
192 : }
193 :
194 : /* Compute logical or of ipa_predicates. This is not operator because
195 : extra parameter CONDITIONS is needed */
196 : ipa_predicate or_with (conditions, const ipa_predicate &) const;
197 :
198 : /* Return true if ipa_predicates are known to be equal. */
199 858890799 : inline bool operator==(const ipa_predicate &p2) const
200 : {
201 858890799 : int i;
202 1040054697 : for (i = 0; m_clause[i]; i++)
203 : {
204 630563144 : gcc_checking_assert (i < max_clauses);
205 630563144 : gcc_checking_assert (m_clause[i] > m_clause[i + 1]);
206 630563144 : gcc_checking_assert (!p2.m_clause[i]
207 : || p2.m_clause[i] > p2.m_clause[i + 1]);
208 630563144 : if (m_clause[i] != p2.m_clause[i])
209 : return false;
210 : }
211 409491553 : return !p2.m_clause[i];
212 : }
213 :
214 : /* Return true if predicates are known to be true or false depending
215 : on COND. */
216 2726169035 : inline bool operator==(const bool cond) const
217 : {
218 2726169035 : if (cond)
219 1262168629 : return !m_clause[0];
220 1464000406 : if (m_clause[0] == (1 << false_condition))
221 : {
222 179038227 : gcc_checking_assert (!m_clause[1]
223 : && m_clause[0] == 1
224 : << false_condition);
225 : return true;
226 : }
227 : return false;
228 : }
229 :
230 84349368 : inline bool operator!=(const ipa_predicate &p2) const
231 : {
232 84349368 : return !(*this == p2);
233 : }
234 :
235 299411268 : inline bool operator!=(const bool cond) const
236 : {
237 299411268 : return !(*this == cond);
238 : }
239 :
240 : /* Evaluate if predicate is known to be false given the clause of possible
241 : truths. */
242 : bool evaluate (clause_t) const;
243 :
244 : /* Estimate probability that predicate will be true in a given context. */
245 : int probability (conditions, clause_t, vec<inline_param_summary>) const;
246 :
247 : /* Dump predicate to F. Output newline if nl. */
248 : void dump (FILE *f, conditions, bool nl=true) const;
249 : void DEBUG_FUNCTION debug (conditions) const;
250 :
251 : /* Return ipa_predicate equal to THIS after duplication. */
252 : ipa_predicate remap_after_duplication (clause_t);
253 :
254 : /* Return ipa_predicate equal to THIS after inlining. */
255 : ipa_predicate remap_after_inlining (class ipa_fn_summary *,
256 : ipa_node_params *params_summary,
257 : ipa_fn_summary *,
258 : const vec<int> &,
259 : const vec<HOST_WIDE_INT> &,
260 : const vec<bool> &,
261 : clause_t, const ipa_predicate &);
262 :
263 : void stream_in (lto_input_block *);
264 : void stream_out (output_block *);
265 :
266 : private:
267 : static const int max_clauses = 8;
268 : clause_t m_clause[max_clauses + 1];
269 :
270 : /* Initialize predicate to one testing single condition number COND. */
271 187259865 : inline void set_to_cond (int cond)
272 : {
273 187259865 : m_clause[0] = 1 << cond;
274 187259865 : m_clause[1] = 0;
275 : }
276 :
277 : void add_clause (conditions conditions, clause_t);
278 : };
279 :
280 : void dump_condition (FILE *f, conditions conditions, int cond);
281 : ipa_predicate add_condition (ipa_fn_summary *summary,
282 : ipa_node_params *params_summary,
283 : int operand_num,
284 : tree type, struct agg_position_info *aggpos,
285 : enum tree_code code, tree val,
286 : expr_eval_ops param_ops = NULL);
|