GCC Middle and Back End API Reference
gimple-range-fold.h
Go to the documentation of this file.
1/* Header file for the GIMPLE fold_using_range interface.
2 Copyright (C) 2019-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_FOLD_H
23#define GCC_GIMPLE_RANGE_FOLD_H
24
25// This file is the main include point for gimple range folding.
26// These routines will fold stmt S into the result range R.
27// Any ssa_names on the stmt will be calculated using the range_query
28// parameter via a call to range_of_expr.
29// If no range_query is provided, current global range info will be used.
30// The second variation specifies an edge, and stmt S is recalculated as if
31// it appeared on that edge.
32
33// Fold stmt S into range R using range query Q.
34bool fold_range (vrange &r, gimple *s, range_query *q = NULL);
35// Recalculate stmt S into R using range query Q as if it were on edge ON_EDGE.
36bool fold_range (vrange &v, gimple *s, edge on_edge, range_query *q = NULL);
37
38// These routines the operands to be specified when manually folding.
39// Any excess queries will be drawn from the current range_query.
40bool fold_range (vrange &r, gimple *s, vrange &r1, range_query *q = NULL);
41bool fold_range (vrange &r, gimple *s, vrange &r1, vrange &r2,
42 range_query *q = NULL);
43bool fold_range (vrange &r, gimple *s, unsigned num_elements, vrange **vector,
44 range_query *q = NULL);
45
46// Calculate op1 on stmt S.
47bool op1_range (vrange &, gimple *s, range_query *q = NULL);
48bool op1_range (vrange &, gimple *s, const vrange &lhs, range_query *q = NULL);
49// Calculate op2 on stmt S.
50bool op2_range (vrange &, gimple *s, range_query *q = NULL);
51bool op2_range (vrange &, gimple *s, const vrange &lhs, range_query *q = NULL);
52
53// This routine will return a relation trio for stmt S.
55
56// Return the type of range which statement S calculates. If the type is
57// unsupported or no type can be determined, return NULL_TREE.
58
59inline tree
61{
62 tree lhs = gimple_get_lhs (s);
64 if (lhs)
65 type = TREE_TYPE (lhs);
66 else
67 {
68 enum gimple_code code = gimple_code (s);
69 if (code == GIMPLE_COND)
71 else if (code == GIMPLE_PHI)
73 else if (code == GIMPLE_CALL)
74 {
76 // If it has a type, get the return type.
77 if (type)
79 }
80 }
82 return type;
83 return NULL_TREE;
84}
85
86// Return EXP if it is an SSA_NAME with a type supported by gimple ranges.
87
88inline tree
90{
91 if (exp && TREE_CODE (exp) == SSA_NAME &&
94 return exp;
95 return NULL_TREE;
96}
97
98// Source of all operands for fold_using_range and gori_compute.
99// It abstracts out the source of an operand so it can come from a stmt or
100// and edge or anywhere a derived class of fur_source wants.
101// The default simply picks up ranges from the current range_query.
102
104{
105public:
107 inline range_query *query () const { return m_query; }
108 inline gori_map *gori_ssa () const
109 { return (m_depend_p && m_query) ? m_query->gori_ssa () : NULL; }
111 { return m_depend_p ? &(m_query->gori ()) : NULL; }
112 virtual bool get_operand (vrange &r, tree expr);
113 virtual bool get_phi_operand (vrange &r, tree expr, edge e);
114 virtual relation_kind query_relation (tree op1, tree op2);
115 virtual bool register_relation (gimple *stmt, relation_kind k, tree op1,
116 tree op2);
117 virtual bool register_relation (edge e, relation_kind k, tree op1,
118 tree op2);
119 void register_outgoing_edges (gcond *, irange &lhs_range, edge e0, edge e1);
120protected:
123};
124
125// fur_stmt is the specification for drawing an operand from range_query Q
126// via a range_of_Expr call on stmt S.
127
128class fur_stmt : public fur_source
129{
130public:
131 fur_stmt (gimple *s, range_query *q = NULL);
132 virtual bool get_operand (vrange &r, tree expr) override;
133 virtual bool get_phi_operand (vrange &r, tree expr, edge e) override;
134 virtual relation_kind query_relation (tree op1, tree op2) override;
135private:
137};
138
139// This version of fur_source will pick a range from a stmt, and also register
140// dependencies via a gori_compute object. This is mostly an internal API.
141
142class fur_depend : public fur_stmt
143{
144public:
145 fur_depend (gimple *s, range_query *q = NULL, class ranger_cache *c = NULL);
146 virtual bool register_relation (gimple *stmt, relation_kind k, tree op1,
147 tree op2) override;
148 virtual bool register_relation (edge e, relation_kind k, tree op1,
149 tree op2) override;
150private:
152};
153
154
155// This version of fur_source will pick a range up off an edge.
156
157class fur_edge : public fur_source
158{
159public:
161 { m_edge = e; }
162 virtual bool get_operand (vrange &r, tree expr) override;
163 virtual bool get_phi_operand (vrange &r, tree expr, edge e) override;
164private:
166};
167
168// This class uses ranges to fold a gimple statement producing a range for
169// the LHS. The source of all operands is supplied via the fur_source class
170// which provides a range_query as well as a source location and any other
171// required information.
172
174{
175public:
176 bool fold_stmt (vrange &r, gimple *s, class fur_source &src,
177 tree name = NULL_TREE);
178protected:
180 fur_source &src);
181 bool range_of_call (vrange &r, gcall *call, fur_source &src);
182 bool range_of_cond_expr (vrange &r, gassign* cond, fur_source &src);
183 bool range_of_address (prange &r, gimple *s, fur_source &src);
184 bool range_from_readonly_var (vrange &r, gimple *stmt);
185 bool range_of_phi (vrange &r, gphi *phi, fur_source &src);
187 fur_source &src);
188 void relation_fold_and_or (irange& lhs_range, gimple *s, fur_source &src,
189 vrange &op1, vrange &op2);
190 bool condexpr_adjust (vrange &r1, vrange &r2, gimple *, tree cond, tree op1,
191 tree op2, fur_source &src);
192};
193#endif // GCC_GIMPLE_RANGE_FOLD_H
Definition genmatch.cc:1507
Definition gimple-range-fold.h:174
bool range_from_readonly_var(vrange &r, gimple *stmt)
Definition gimple-range-fold.cc:1114
bool range_of_call(vrange &r, gcall *call, fur_source &src)
Definition gimple-range-fold.cc:1333
bool condexpr_adjust(vrange &r1, vrange &r2, gimple *, tree cond, tree op1, tree op2, fur_source &src)
Definition gimple-range-fold.cc:1386
bool range_of_cond_expr(vrange &r, gassign *cond, fur_source &src)
Definition gimple-range-fold.cc:1483
bool range_of_address(prange &r, gimple *s, fur_source &src)
Definition gimple-range-fold.cc:909
bool range_of_range_op(vrange &r, gimple_range_op_handler &handler, fur_source &src)
Definition gimple-range-fold.cc:791
void range_of_ssa_name_with_loop_info(vrange &, tree, class loop *, gphi *, fur_source &src)
Definition gimple-range-fold.cc:1535
void relation_fold_and_or(irange &lhs_range, gimple *s, fur_source &src, vrange &op1, vrange &op2)
Definition gimple-range-fold.cc:1576
bool fold_stmt(vrange &r, gimple *s, class fur_source &src, tree name=NULL_TREE)
Definition gimple-range-fold.cc:679
bool range_of_phi(vrange &r, gphi *phi, fur_source &src)
Definition gimple-range-fold.cc:1184
fur_depend(gimple *s, range_query *q=NULL, class ranger_cache *c=NULL)
Definition gimple-range-fold.cc:169
ranger_cache * m_cache
Definition gimple-range-fold.h:151
virtual bool register_relation(gimple *stmt, relation_kind k, tree op1, tree op2) override
Definition gimple-range-fold.cc:179
virtual bool get_operand(vrange &r, tree expr) override
Definition gimple-range-fold.cc:117
virtual bool get_phi_operand(vrange &r, tree expr, edge e) override
Definition gimple-range-fold.cc:126
edge m_edge
Definition gimple-range-fold.h:165
fur_edge(edge e, range_query *q=NULL)
Definition gimple-range-fold.h:160
Definition gimple-range-fold.h:104
fur_source(range_query *q=NULL)
Definition gimple-range-fold.cc:57
bool m_depend_p
Definition gimple-range-fold.h:122
void register_outgoing_edges(gcond *, irange &lhs_range, edge e0, edge e1)
Definition gimple-range-fold.cc:1675
virtual bool get_operand(vrange &r, tree expr)
Definition gimple-range-fold.cc:69
virtual bool get_phi_operand(vrange &r, tree expr, edge e)
Definition gimple-range-fold.cc:78
range_query * m_query
Definition gimple-range-fold.h:121
class gimple_outgoing_range * gori()
Definition gimple-range-fold.h:110
virtual bool register_relation(gimple *stmt, relation_kind k, tree op1, tree op2)
Definition gimple-range-fold.cc:95
gori_map * gori_ssa() const
Definition gimple-range-fold.h:108
range_query * query() const
Definition gimple-range-fold.h:107
virtual relation_kind query_relation(tree op1, tree op2)
Definition gimple-range-fold.cc:86
gimple * m_stmt
Definition gimple-range-fold.h:136
virtual bool get_phi_operand(vrange &r, tree expr, edge e) override
Definition gimple-range-fold.cc:152
fur_stmt(gimple *s, range_query *q=NULL)
Definition gimple-range-fold.cc:135
virtual bool get_operand(vrange &r, tree expr) override
Definition gimple-range-fold.cc:143
virtual relation_kind query_relation(tree op1, tree op2) override
Definition gimple-range-fold.cc:162
Definition gimple-range-edge.h:48
Definition gimple-range-op.h:29
Definition gimple-range-gori.h:95
Definition value-range.h:291
Definition cfgloop.h:120
Definition value-range.h:405
Definition value-query.h:55
Definition gimple-range-cache.h:103
Definition value-relation.h:364
static bool supports_type_p(const_tree type)
Definition value-range.h:1064
Definition value-range.h:88
class edge_def * edge
Definition coretypes.h:348
union tree_node * tree
Definition coretypes.h:97
double exp(double)
tree gimple_range_ssa_p(tree exp)
Definition gimple-range-fold.h:89
bool op1_range(vrange &, gimple *s, range_query *q=NULL)
Definition gimple-range-fold.cc:361
tree gimple_range_type(const gimple *s)
Definition gimple-range-fold.h:60
bool fold_range(vrange &r, gimple *s, range_query *q=NULL)
Definition gimple-range-fold.cc:317
bool op2_range(vrange &, gimple *s, range_query *q=NULL)
Definition gimple-range-fold.cc:395
relation_trio fold_relations(gimple *s, range_query *q=NULL)
Definition gimple-range-fold.cc:498
tree gimple_get_lhs(const gimple *stmt)
Definition gimple.cc:2007
gimple_code
Definition gimple.h:33
tree gimple_phi_result(const gphi *gs)
Definition gimple.h:4611
tree gimple_call_fntype(const gcall *gs)
Definition gimple.h:3139
poly_int< N, C > r
Definition poly-int.h:774
Definition gimple.h:910
Definition gimple.h:355
Definition gimple.h:864
Definition gimple.h:224
Definition gimple.h:464
Definition gengtype.h:252
#define NULL
Definition system.h:58
#define TREE_CODE(NODE)
Definition tree.h:325
#define boolean_type_node
Definition tree.h:4720
#define TREE_TYPE(NODE)
Definition tree.h:513
#define SSA_NAME_IS_VIRTUAL_OPERAND(NODE)
Definition tree.h:2253
#define NULL_TREE
Definition tree.h:318
enum relation_kind_t relation_kind