GCC Middle and Back End API Reference
gimple-range-gori.h
Go to the documentation of this file.
1/* Header file for gimple range GORI structures.
2 Copyright (C) 2017-2026 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
4 and Aldy Hernandez <aldyh@redhat.com>.
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 3, or (at your option) any later
11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
21
22#ifndef GCC_GIMPLE_RANGE_GORI_H
23#define GCC_GIMPLE_RANGE_GORI_H
24
25// RANGE_DEF_CHAIN is used to determine which SSA names in a block can
26// have range information calculated for them, and what the
27// dependencies on each other are.
28
30{
31public:
34 tree depend1 (tree name) const;
35 tree depend2 (tree name) const;
36 bool in_chain_p (tree name, tree def);
37 bool chain_import_p (tree name, tree import);
38 void register_dependency (tree name, tree ssa1, basic_block bb = NULL);
39 void clear (tree name);
40 void dump (FILE *f, basic_block bb, const char *prefix = NULL);
41protected:
42 bool has_def_chain (tree name);
46 bitmap get_imports (tree name);
48private:
49 struct rdc {
50 unsigned int ssa1; // First direct dependency
51 unsigned int ssa2; // Second direct dependency
52 bitmap bm; // All dependencies
54 };
55 vec<rdc> m_def_chain; // SSA_NAME : def chain components.
56 void set_import (struct rdc &data, tree imp, bitmap b);
58};
59
60// Return the first direct dependency for NAME, if there is one.
61// Direct dependencies are those which occur on the definition statement.
62// Only the first 2 such names are cached.
63
64inline tree
66{
67 unsigned v = SSA_NAME_VERSION (name);
68 if (v >= m_def_chain.length ())
69 return NULL_TREE;
70 unsigned v1 = m_def_chain[v].ssa1;
71 if (!v1)
72 return NULL_TREE;
73 return ssa_name (v1);
74}
75
76// Return the second direct dependency for NAME, if there is one.
77
78inline tree
80{
81 unsigned v = SSA_NAME_VERSION (name);
82 if (v >= m_def_chain.length ())
83 return NULL_TREE;
84 unsigned v2 = m_def_chain[v].ssa2;
85 if (!v2)
86 return NULL_TREE;
87 return ssa_name (v2);
88}
89
90// GORI_MAP is used to accumulate what SSA names in a block can
91// generate range information, and provides tools for the block ranger
92// to enable it to efficiently calculate these ranges.
93
95{
96public:
97 gori_map ();
98 ~gori_map ();
99
100 bool is_export_p (tree name, basic_block bb = NULL);
101 bool is_import_p (tree name, basic_block bb);
105 void set_range_invariant (tree name, bool invariant = true);
106
107 void dump (FILE *f);
108 void dump (FILE *f, basic_block bb, bool verbose = true);
109private:
110 vec<bitmap> m_outgoing; // BB: Outgoing ranges calculable on edges
111 vec<bitmap> m_incoming; // BB: Incoming ranges which can affect exports.
112 bitmap m_maybe_variant; // Names which might have outgoing ranges.
113 void maybe_add_gori (tree name, basic_block bb);
114 void calculate_gori (basic_block bb);
115};
116
117
118// This class is used to determine which SSA_NAMES can have ranges
119// calculated for them on outgoing edges from basic blocks. This represents
120// ONLY the effect of the basic block edge->src on a range.
121//
122// There are 2 primary entry points:
123//
124// has_edge_range_p (tree name, edge e)
125// returns true if the outgoing edge *may* be able to produce range
126// information for ssa_name NAME on edge E.
127// FALSE is returned if this edge does not affect the range of NAME.
128// if no edge is specified, return TRUE if name may have a value calculated
129// on *ANY* edge that has been seen. FALSE indicates that the global value
130// is applicable everywhere that has been processed.
131//
132// edge_range_p (vrange &range, edge e, tree name)
133// Actually does the calculation of RANGE for name on E
134// This represents application of whatever static range effect edge E
135// may have on NAME, not any cumulative effect.
136
137// There are also some internal APIs
138//
139// ssa_range_in_bb () is an internal routine which is used to start any
140// calculation chain using SSA_NAMES which come from outside the block. ie
141// a_2 = b_4 - 8
142// if (a_2 < 30)
143// on the true edge, a_2 is known to be [0, 29]
144// b_4 can be calculated as [8, 37]
145// during this calculation, b_4 is considered an "import" and ssa_range_in_bb
146// is queried for a starting range which is used in the calculation.
147// A default value of VARYING provides the raw static info for the edge.
148//
149// If there is any known range for b_4 coming into this block, it can refine
150// the results. This allows for cascading results to be propagated.
151// if b_4 is [100, 200] on entry to the block, feeds into the calculation
152// of a_2 = [92, 192], and finally on the true edge the range would be
153// an empty range [] because it is not possible for the true edge to be taken.
154//
155// expr_range_in_bb is simply a wrapper which calls ssa_range_in_bb for
156// SSA_NAMES and otherwise simply calculates the range of the expression.
157//
158// The constructor takes a flag value to use on edges to check for the
159// NON_EXECUTABLE_EDGE property. The zero default means no flag is checked.
160// All value requests from NON_EXECUTABLE_EDGE edges are returned UNDEFINED.
161//
162// The remaining routines are internal use only.
163
164class value_relation;
165
167{
168public:
169 gori_compute (gori_map &map, int not_executable_flag = 0,
170 int max_sw_edges = 0);
171 virtual ~gori_compute ();
172 bool edge_range_p (vrange &r, edge e, tree name, range_query &q);
173 bool has_edge_range_p (tree name, basic_block bb = NULL);
174 bool has_edge_range_p (tree name, edge e);
175 void dump (FILE *f);
176 bool compute_operand_range (vrange &r, gimple *stmt, const vrange &lhs,
177 tree name, class fur_source &src,
178 value_relation *rel = NULL);
179private:
182 tree op2, vrange &op2_range,
183 fur_source &src, relation_kind k);
184 bool may_recompute_p (tree name, edge e, int depth = -1);
185 bool may_recompute_p (tree name, basic_block bb = NULL, int depth = -1);
186 bool compute_operand_range_switch (vrange &r, gswitch *s, const vrange &lhs,
187 tree name, fur_source &src);
189 const vrange &lhs, fur_source &src,
190 value_relation *rel = NULL);
192 const vrange &lhs, fur_source &src,
193 value_relation *rel = NULL);
196 const vrange &lhs, tree name,
197 fur_source &src,
198 value_relation *rel = NULL);
199 void compute_logical_operands (vrange &true_range, vrange &false_range,
201 const irange &lhs, tree name, fur_source &src,
202 tree op, bool op_in_chain);
203 bool logical_combine (vrange &r, enum tree_code code, const irange &lhs,
204 const vrange &op1_true, const vrange &op1_false,
205 const vrange &op2_true, const vrange &op2_false);
206 int_range<2> m_bool_zero; // Boolean false cached.
207 int_range<2> m_bool_one; // Boolean true cached.
208
212};
213
214// These APIs are used to query GORI if there are ranges generated on an edge.
215// GORI_ON_EDGE is used to get all the ranges at once (returned in an
216// ssa_cache structure).
217// GORI_NAME_ON_EDGE is used to simply ask if NAME has a range on edge E
218
219// Fill ssa-cache R with any outgoing ranges on edge E, using QUERY.
220bool gori_on_edge (class ssa_cache &r, edge e, range_query *query = NULL);
221
222// Query if NAME has an outgoing range on edge E, and return it in R if so.
223// Note this doesnt use ranger, its a static GORI analysis of the range in
224// block e->src and is based on any branch at the exit of that block.
225bool gori_name_on_edge (vrange &r, tree name, edge e, range_query *q = NULL);
226
227// For each name that is an import into BB's exports..
228#define FOR_EACH_GORI_IMPORT_NAME(gorimap, bb, name) \
229 for (gori_export_iterator iter ((gorimap)->imports ((bb))); \
230 ((name) = iter.get_name ()); \
231 iter.next ())
232
233// For each name possibly exported from block BB.
234#define FOR_EACH_GORI_EXPORT_NAME(gorimap, bb, name) \
235 for (gori_export_iterator iter ((gorimap)->exports ((bb))); \
236 ((name) = iter.get_name ()); \
237 iter.next ())
238
239// For each name and all their dependencies possibly exported from block BB.
240#define FOR_EACH_GORI_EXPORT_AND_DEP_NAME(gorimap, bb, name, bm) \
241 for (gori_export_iterator iter ((gorimap)->exports_and_deps ((bb),(bm))); \
242 ((name) = iter.get_name ()); \
243 iter.next ())
244
245// Used to assist with iterating over the GORI export list in various ways
247public:
249 void next ();
250 tree get_name ();
251protected:
254 unsigned y;
255};
256
257#endif // GCC_GIMPLE_RANGE_GORI_H
Definition genoutput.cc:150
Definition gimple-range-fold.h:104
gimple_outgoing_range(int max_sw_edges=0)
Definition gimple-range-edge.cc:71
Definition gimple-range-op.h:29
bool edge_range_p(vrange &r, edge e, tree name, range_query &q)
Definition gimple-range-gori.cc:1426
virtual ~gori_compute()
Definition gimple-range-gori.cc:616
int_range< 2 > m_bool_zero
Definition gimple-range-gori.h:206
void dump(FILE *f)
Definition gimple-range-gori.cc:1496
int_range< 2 > m_bool_one
Definition gimple-range-gori.h:207
bool compute_operand1_and_operand2_range(vrange &r, gimple_range_op_handler &handler, const vrange &lhs, tree name, fur_source &src, value_relation *rel=NULL)
Definition gimple-range-gori.cc:1315
bool may_recompute_p(tree name, edge e, int depth=-1)
Definition gimple-range-gori.cc:1391
gori_map & m_map
Definition gimple-range-gori.h:180
gori_compute(gori_map &map, int not_executable_flag=0, int max_sw_edges=0)
Definition gimple-range-gori.cc:597
int m_not_executable_flag
Definition gimple-range-gori.h:210
bool compute_operand1_range(vrange &r, gimple_range_op_handler &handler, const vrange &lhs, fur_source &src, value_relation *rel=NULL)
Definition gimple-range-gori.cc:1167
bool compute_operand_range_switch(vrange &r, gswitch *s, const vrange &lhs, tree name, fur_source &src)
Definition gimple-range-gori.cc:625
void compute_logical_operands(vrange &true_range, vrange &false_range, gimple_range_op_handler &handler, const irange &lhs, tree name, fur_source &src, tree op, bool op_in_chain)
Definition gimple-range-gori.cc:999
bool refine_using_relation(tree op1, vrange &op1_range, tree op2, vrange &op2_range, fur_source &src, relation_kind k)
Definition gimple-range-gori.cc:1059
bool compute_operand_range(vrange &r, gimple *stmt, const vrange &lhs, tree name, class fur_source &src, value_relation *rel=NULL)
Definition gimple-range-gori.cc:653
int m_recompute_depth
Definition gimple-range-gori.h:211
bool has_edge_range_p(tree name, basic_block bb=NULL)
Definition gimple-range-gori.cc:1402
bool compute_operand2_range(vrange &r, gimple_range_op_handler &handler, const vrange &lhs, fur_source &src, value_relation *rel=NULL)
Definition gimple-range-gori.cc:1248
range_tracer tracer
Definition gimple-range-gori.h:209
bool logical_combine(vrange &r, enum tree_code code, const irange &lhs, const vrange &op1_true, const vrange &op1_false, const vrange &op2_true, const vrange &op2_false)
Definition gimple-range-gori.cc:832
bitmap_iterator bi
Definition gimple-range-gori.h:253
tree get_name()
Definition gimple-range-gori.cc:1528
gori_export_iterator(bitmap b)
Definition gimple-range-gori.cc:1507
void next()
Definition gimple-range-gori.cc:1518
bitmap bm
Definition gimple-range-gori.h:252
unsigned y
Definition gimple-range-gori.h:254
Definition gimple-range-gori.h:95
void maybe_add_gori(tree name, basic_block bb)
Definition gimple-range-gori.cc:470
bitmap m_maybe_variant
Definition gimple-range-gori.h:112
vec< bitmap > m_incoming
Definition gimple-range-gori.h:111
bool is_import_p(tree name, basic_block bb)
Definition gimple-range-gori.cc:460
bitmap exports_and_deps(basic_block bb, bitmap tmpbit)
Definition gimple-range-gori.cc:405
gori_map()
Definition gimple-range-gori.cc:374
~gori_map()
Definition gimple-range-gori.cc:385
vec< bitmap > m_outgoing
Definition gimple-range-gori.h:110
void dump(FILE *f)
Definition gimple-range-gori.cc:580
bitmap imports(basic_block bb)
Definition gimple-range-gori.cc:426
bitmap exports(basic_block bb)
Definition gimple-range-gori.cc:394
void calculate_gori(basic_block bb)
Definition gimple-range-gori.cc:494
void set_range_invariant(tree name, bool invariant=true)
Definition gimple-range-gori.cc:449
bool is_export_p(tree name, basic_block bb=NULL)
Definition gimple-range-gori.cc:437
Definition value-range.h:388
Definition value-range.h:291
tree depend1(tree name) const
Definition gimple-range-gori.h:65
bool in_chain_p(tree name, tree def)
Definition gimple-range-gori.cc:116
void dump(FILE *f, basic_block bb, const char *prefix=NULL)
Definition gimple-range-gori.cc:316
void set_import(struct rdc &data, tree imp, bitmap b)
Definition gimple-range-gori.cc:132
void register_dependency(tree name, tree ssa1, basic_block bb=NULL)
Definition gimple-range-gori.cc:170
bitmap get_def_chain(tree name)
Definition gimple-range-gori.cc:255
bool has_def_chain(tree name)
Definition gimple-range-gori.cc:238
vec< rdc > m_def_chain
Definition gimple-range-gori.h:55
range_def_chain()
Definition gimple-range-gori.cc:96
bool def_chain_in_bitmap_p(tree name, bitmap b)
Definition gimple-range-gori.cc:218
~range_def_chain()
Definition gimple-range-gori.cc:106
bool chain_import_p(tree name, tree import)
Definition gimple-range-gori.cc:159
bitmap get_imports(tree name)
Definition gimple-range-gori.cc:148
void clear(tree name)
Definition gimple-range-gori.cc:301
bitmap_obstack m_bitmaps
Definition gimple-range-gori.h:47
void add_def_chain_to_bitmap(bitmap b, tree name)
Definition gimple-range-gori.cc:227
int m_logical_depth
Definition gimple-range-gori.h:57
tree depend2(tree name) const
Definition gimple-range-gori.h:79
Definition value-query.h:55
Definition gimple-range-trace.h:30
Definition gimple-range-cache.h:58
Definition value-relation.h:456
Definition value-range.h:88
bool verbose
Definition collect-utils.cc:35
struct basic_block_def * basic_block
Definition coretypes.h:351
class edge_def * edge
Definition coretypes.h:348
class bitmap_head * bitmap
Definition coretypes.h:51
union tree_node * tree
Definition coretypes.h:97
static struct string2counter_map map[debug_counter_number_of_counters]
Definition dbgcnt.cc:39
tree_code
Definition genmatch.cc:1002
bool op2_range(vrange &r, gimple *s, const vrange &lhs, range_query *q)
Definition gimple-range-fold.cc:375
bool op1_range(vrange &r, gimple *s, const vrange &lhs, range_query *q)
Definition gimple-range-fold.cc:338
bool gori_name_on_edge(vrange &r, tree name, edge e, range_query *q=NULL)
Definition gimple-range-gori.cc:1749
bool gori_on_edge(class ssa_cache &r, edge e, range_query *query=NULL)
Definition gimple-range-gori.cc:1677
poly_int< N, C > r
Definition poly-int.h:774
Ca const poly_int< N, Cb > & b
Definition poly-int.h:771
Definition bitmap.h:519
Definition bitmap.h:293
Definition loop-invariant.cc:88
Definition gimple.h:224
Definition gimple.h:901
Definition loop-invariant.cc:101
Definition gimple-range-gori.h:49
bitmap m_import
Definition gimple-range-gori.h:53
unsigned int ssa1
Definition gimple-range-gori.h:50
unsigned int ssa2
Definition gimple-range-gori.h:51
bitmap bm
Definition gimple-range-gori.h:52
Definition vec.h:450
#define NULL
Definition system.h:58
#define ssa_name(i)
Definition tree-ssanames.h:53
#define SSA_NAME_VERSION(NODE)
Definition tree.h:2290
#define NULL_TREE
Definition tree.h:318
enum relation_kind_t relation_kind