d: Merge upstream dmd 56589f0f4, druntime 651389b5, phobos 1516ecad9.
[official-gcc.git] / gcc / gimple-range-fold.h
blobfbf66275f741bea384529c9cfe087b197ecd10e4
1 /* Header file for the GIMPLE fold_using_range interface.
2 Copyright (C) 2019-2022 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
4 and Aldy Hernandez <aldyh@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #ifndef GCC_GIMPLE_RANGE_FOLD_H
23 #define GCC_GIMPLE_RANGE_FOLD_H
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.
33 // Fold stmt S into range R using range query Q.
34 bool 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.
36 bool fold_range (vrange &v, gimple *s, edge on_edge, range_query *q = NULL);
38 // These routines the operands to be specified when manually folding.
39 // Any excess queries will be drawn from the current range_query.
40 bool fold_range (vrange &r, gimple *s, vrange &r1);
41 bool fold_range (vrange &r, gimple *s, vrange &r1, vrange &r2);
42 bool fold_range (vrange &r, gimple *s, unsigned num_elements, vrange **vector);
44 // Return the type of range which statement S calculates. If the type is
45 // unsupported or no type can be determined, return NULL_TREE.
47 static inline tree
48 gimple_range_type (const gimple *s)
50 tree lhs = gimple_get_lhs (s);
51 tree type = NULL_TREE;
52 if (lhs)
53 type = TREE_TYPE (lhs);
54 else
56 enum gimple_code code = gimple_code (s);
57 if (code == GIMPLE_COND)
58 type = boolean_type_node;
59 else if (code == GIMPLE_PHI)
60 type = TREE_TYPE (gimple_phi_result (s));
61 else if (code == GIMPLE_CALL)
63 type = gimple_call_fntype (s);
64 // If it has a type, get the return type.
65 if (type)
66 type = TREE_TYPE (type);
69 if (type && Value_Range::supports_type_p (type))
70 return type;
71 return NULL_TREE;
74 // Return EXP if it is an SSA_NAME with a type supported by gimple ranges.
76 static inline tree
77 gimple_range_ssa_p (tree exp)
79 if (exp && TREE_CODE (exp) == SSA_NAME &&
80 !SSA_NAME_IS_VIRTUAL_OPERAND (exp) &&
81 !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (exp) &&
82 Value_Range::supports_type_p (TREE_TYPE (exp)))
83 return exp;
84 return NULL_TREE;
87 // Return true if TYPE1 and TYPE2 are compatible range types.
89 static inline bool
90 range_compatible_p (tree type1, tree type2)
92 // types_compatible_p requires conversion in both directions to be useless.
93 // GIMPLE only requires a cast one way in order to be compatible.
94 // Ranges really only need the sign and precision to be the same.
95 return (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
96 && TYPE_SIGN (type1) == TYPE_SIGN (type2));
100 // Source of all operands for fold_using_range and gori_compute.
101 // It abstracts out the source of an operand so it can come from a stmt or
102 // and edge or anywhere a derived class of fur_source wants.
103 // The default simply picks up ranges from the current range_query.
105 class fur_source
107 public:
108 fur_source (range_query *q = NULL);
109 inline range_query *query () { return m_query; }
110 inline class gori_compute *gori () { return m_gori; };
111 virtual bool get_operand (vrange &r, tree expr);
112 virtual bool get_phi_operand (vrange &r, tree expr, edge e);
113 virtual relation_kind query_relation (tree op1, tree op2);
114 virtual void register_relation (gimple *stmt, relation_kind k, tree op1,
115 tree op2);
116 virtual void register_relation (edge e, relation_kind k, tree op1,
117 tree op2);
118 void register_outgoing_edges (gcond *, irange &lhs_range, edge e0, edge e1);
119 protected:
120 range_query *m_query;
121 gori_compute *m_gori;
124 // fur_stmt is the specification for drawing an operand from range_query Q
125 // via a range_of_Expr call on stmt S.
127 class fur_stmt : public fur_source
129 public:
130 fur_stmt (gimple *s, range_query *q = NULL);
131 virtual bool get_operand (vrange &r, tree expr) override;
132 virtual bool get_phi_operand (vrange &r, tree expr, edge e) override;
133 virtual relation_kind query_relation (tree op1, tree op2) override;
134 private:
135 gimple *m_stmt;
138 // This version of fur_source will pick a range from a stmt, and also register
139 // dependencies via a gori_compute object. This is mostly an internal API.
141 class fur_depend : public fur_stmt
143 public:
144 fur_depend (gimple *s, gori_compute *gori, range_query *q = NULL);
145 virtual void register_relation (gimple *stmt, relation_kind k, tree op1,
146 tree op2) override;
147 virtual void register_relation (edge e, relation_kind k, tree op1,
148 tree op2) override;
149 protected:
150 relation_oracle *m_oracle;
153 extern tree gimple_range_operand1 (const gimple *s);
154 extern tree gimple_range_operand2 (const gimple *s);
156 // This class uses ranges to fold a gimple statement producinf a range for
157 // the LHS. The source of all operands is supplied via the fur_source class
158 // which provides a range_query as well as a source location and any other
159 // required information.
161 class fold_using_range
163 public:
164 bool fold_stmt (vrange &r, gimple *s, class fur_source &src,
165 tree name = NULL_TREE);
166 protected:
167 bool range_of_range_op (vrange &r, gimple *s, fur_source &src);
168 bool range_of_call (vrange &r, gcall *call, fur_source &src);
169 bool range_of_cond_expr (vrange &r, gassign* cond, fur_source &src);
170 bool range_of_address (irange &r, gimple *s, fur_source &src);
171 bool range_of_builtin_call (vrange &r, gcall *call, fur_source &src);
172 bool range_of_builtin_int_call (irange &r, gcall *call, fur_source &src);
173 void range_of_builtin_ubsan_call (irange &r, gcall *call, tree_code code,
174 fur_source &src);
175 bool range_of_phi (vrange &r, gphi *phi, fur_source &src);
176 void range_of_ssa_name_with_loop_info (irange &, tree, class loop *, gphi *,
177 fur_source &src);
178 void relation_fold_and_or (irange& lhs_range, gimple *s, fur_source &src);
180 #endif // GCC_GIMPLE_RANGE_FOLD_H