GCC Middle and Back End API Reference
ipa-predicate.h
Go to the documentation of this file.
1/* IPA predicates.
2 Copyright (C) 2003-2026 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
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/* 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. */
34struct GTY(()) expr_eval_op
35{
36 /* Result type of expression. */
38 /* Constant operands in expression, there are at most two. */
40 /* Index of parameter operand in expression. */
41 unsigned index : 2;
42 /* Operation code of expression. */
44};
45
47
48struct 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). */
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. */
72};
73
74/* Information kept about parameter of call site. */
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. */
87 bool equal_to (const inline_param_summary &other) const
88 {
89 return change_prob == other.change_prob
91 == other.points_to_local_or_readonly_memory
93 == other.points_to_possible_sra_candidate;
94 }
101};
102
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
122typedef uint32_t clause_t;
124{
125public:
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 inline ipa_predicate (bool p = true)
154 {
155 if (p)
156 /* True predicate. */
157 m_clause[0] = 0;
158 else
159 /* False predicate. */
161 }
162
163 /* Sanity check that we do not mix pointers to predicates with predicates. */
165 {
167 }
168
169 /* Return predicate testing condition I. */
171 {
174 return p;
175 }
176
177 /* Return predicate testing that function was not inlined. */
179 {
182 return p;
183 }
184
185 /* Compute logical and of ipa_predicates. */
188 {
189 ipa_predicate ret = *this;
190 ret &= p;
191 return ret;
192 }
193
194 /* Compute logical or of ipa_predicates. This is not operator because
195 extra parameter CONDITIONS is needed */
197
198 /* Return true if ipa_predicates are known to be equal. */
199 inline bool operator==(const ipa_predicate &p2) const
200 {
201 int i;
202 for (i = 0; m_clause[i]; i++)
203 {
207 || p2.m_clause[i] > p2.m_clause[i + 1]);
208 if (m_clause[i] != p2.m_clause[i])
209 return false;
210 }
211 return !p2.m_clause[i];
212 }
213
214 /* Return true if predicates are known to be true or false depending
215 on COND. */
216 inline bool operator==(const bool cond) const
217 {
218 if (cond)
219 return !m_clause[0];
220 if (m_clause[0] == (1 << false_condition))
221 {
223 && m_clause[0] == 1
224 << false_condition);
225 return true;
226 }
227 return false;
228 }
229
230 inline bool operator!=(const ipa_predicate &p2) const
231 {
232 return !(*this == p2);
233 }
234
235 inline bool operator!=(const bool cond) const
236 {
237 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. */
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. */
253
254 /* Return ipa_predicate equal to THIS after inlining. */
256 ipa_node_params *params_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
266private:
267 static const int max_clauses = 8;
269
270 /* Initialize predicate to one testing single condition number COND. */
271 inline void set_to_cond (int cond)
272 {
273 m_clause[0] = 1 << cond;
274 m_clause[1] = 0;
275 }
276
278};
279
280void dump_condition (FILE *f, conditions conditions, int cond);
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);
Definition ipa-fnsummary.h:124
Definition ipa-prop.h:641
Definition ipa-predicate.h:124
ipa_predicate & operator&=(const ipa_predicate &)
Definition ipa-predicate.cc:181
static const int max_clauses
Definition ipa-predicate.h:267
bool operator==(const bool cond) const
Definition ipa-predicate.h:216
void dump(FILE *f, conditions, bool nl=true) const
Definition ipa-predicate.cc:474
ipa_predicate remap_after_inlining(class ipa_fn_summary *, ipa_node_params *params_summary, ipa_fn_summary *, const vec< int > &, const vec< HOST_WIDE_INT > &, const vec< bool > &, clause_t, const ipa_predicate &)
Definition ipa-predicate.cc:530
void stream_in(lto_input_block *)
Definition ipa-predicate.cc:622
void add_clause(conditions conditions, clause_t)
Definition ipa-predicate.cc:74
void stream_out(output_block *)
Definition ipa-predicate.cc:643
bool evaluate(clause_t) const
Definition ipa-predicate.cc:240
static ipa_predicate not_inlined(void)
Definition ipa-predicate.h:178
bool operator!=(const ipa_predicate &p2) const
Definition ipa-predicate.h:230
clause_t m_clause[max_clauses+1]
Definition ipa-predicate.h:268
static const tree_code not_sra_candidate
Definition ipa-predicate.h:149
bool operator==(const ipa_predicate &p2) const
Definition ipa-predicate.h:199
ipa_predicate(bool p=true)
Definition ipa-predicate.h:153
ipa_predicate or_with(conditions, const ipa_predicate &) const
Definition ipa-predicate.cc:214
ipa_predicate remap_after_duplication(clause_t)
Definition ipa-predicate.cc:503
int probability(conditions, clause_t, vec< inline_param_summary >) const
Definition ipa-predicate.cc:264
predicate_conditions
Definition ipa-predicate.h:127
@ first_dynamic_condition
Definition ipa-predicate.h:130
@ false_condition
Definition ipa-predicate.h:128
@ not_inlined_condition
Definition ipa-predicate.h:129
static ipa_predicate predicate_testing_cond(int i)
Definition ipa-predicate.h:170
static const tree_code changed
Definition ipa-predicate.h:145
static const int num_conditions
Definition ipa-predicate.h:135
void set_to_cond(int cond)
Definition ipa-predicate.h:271
static const tree_code is_not_constant
Definition ipa-predicate.h:139
bool operator!=(const bool cond) const
Definition ipa-predicate.h:235
ipa_predicate operator&(const ipa_predicate &p) const
Definition ipa-predicate.h:187
ipa_predicate(ipa_predicate *)
Definition ipa-predicate.h:164
Definition lto-streamer.h:349
bool debug
Definition collect-utils.cc:34
#define GTY(x)
Definition coretypes.h:41
union tree_node * tree
Definition coretypes.h:97
tree_code
Definition genmatch.cc:1011
ipa_predicate add_condition(class ipa_fn_summary *summary, class ipa_node_params *params_summary, int operand_num, tree type, struct agg_position_info *aggpos, enum tree_code code, tree val, expr_eval_ops param_ops)
Definition ipa-predicate.cc:662
void dump_condition(FILE *f, conditions conditions, int cond)
Definition ipa-predicate.cc:327
uint32_t clause_t
Definition ipa-predicate.h:122
vec< expr_eval_op, va_gc > * expr_eval_ops
Definition ipa-predicate.h:46
vec< condition, va_gc > * conditions
Definition ipa-predicate.h:103
i
Definition poly-int.h:776
#define REG_BR_PROB_BASE
Definition profile-count.h:75
Definition ipa-fnsummary.h:66
Definition ipa-predicate.h:49
unsigned unknown_offset
Definition ipa-predicate.h:68
tree val
Definition ipa-predicate.h:55
unsigned maybe_modified
Definition ipa-predicate.h:66
expr_eval_ops param_ops
Definition ipa-predicate.h:71
int operand_num
Definition ipa-predicate.h:56
unsigned agg_contents
Definition ipa-predicate.h:60
tree type
Definition ipa-predicate.h:54
unsigned by_ref
Definition ipa-predicate.h:63
tree_code code
Definition ipa-predicate.h:57
HOST_WIDE_INT offset
Definition ipa-predicate.h:52
Definition ipa-predicate.h:35
tree val[2]
Definition ipa-predicate.h:39
tree_code code
Definition ipa-predicate.h:43
tree type
Definition ipa-predicate.h:37
unsigned index
Definition ipa-predicate.h:41
Definition ipa-predicate.h:76
unsigned points_to_possible_sra_candidate
Definition ipa-predicate.h:86
bool equal_to(const inline_param_summary &other) const
Definition ipa-predicate.h:87
bool useless_p(void) const
Definition ipa-predicate.h:95
unsigned points_to_local_or_readonly_memory
Definition ipa-predicate.h:85
short change_prob
Definition ipa-predicate.h:84
Definition lto-streamer.h:793
Definition gengtype.h:252
Definition vec.h:450
#define NULL
Definition system.h:55
#define gcc_unreachable()
Definition system.h:785
#define DEBUG_FUNCTION
Definition system.h:1131
#define gcc_checking_assert(EXPR)
Definition system.h:765