]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/range-op.h
Provide interface for non-standard operators.
[thirdparty/gcc.git] / gcc / range-op.h
CommitLineData
38a73435 1/* Header file for range operator class.
aeee4812 2 Copyright (C) 2017-2023 Free Software Foundation, Inc.
38a73435
AH
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_RANGE_OP_H
23#define GCC_RANGE_OP_H
24
25// This class is implemented for each kind of operator supported by
26// the range generator. It serves various purposes.
27//
28// 1 - Generates range information for the specific operation between
29// two ranges. This provides the ability to fold ranges for an
30// expression.
31//
32// 2 - Performs range algebra on the expression such that a range can be
33// adjusted in terms of one of the operands:
34//
35// def = op1 + op2
36//
37// Given a range for def, we can adjust the range so that it is in
38// terms of either operand.
39//
40// op1_range (def_range, op2) will adjust the range in place so it
41// is in terms of op1. Since op1 = def - op2, it will subtract
42// op2 from each element of the range.
43//
44// 3 - Creates a range for an operand based on whether the result is 0 or
45// non-zero. This is mostly for logical true false, but can serve other
46// purposes.
47// ie 0 = op1 - op2 implies op2 has the same range as op1.
9c0fed50
AM
48//
49// 4 - All supported range combinations are explicitly specified.
50// Any desired combinations should be implemented for each operator.
51// When new range classes are added, new matching prototypes should be
52// added.
38a73435
AH
53
54class range_operator
55{
17586bdc 56 friend class range_op_table;
38a73435
AH
57public:
58 // Perform an operation between 2 ranges and return it.
4ba9fb0a
AH
59 virtual bool fold_range (irange &r, tree type,
60 const irange &lh,
80dd13f5 61 const irange &rh,
b565ac19 62 relation_trio = TRIO_VARYING) const;
9c0fed50
AM
63 virtual bool fold_range (frange &r, tree type,
64 const frange &lh,
65 const frange &rh,
66 relation_trio = TRIO_VARYING) const;
67 virtual bool fold_range (irange &r, tree type,
68 const frange &lh,
69 const irange &rh,
70 relation_trio = TRIO_VARYING) const;
71 virtual bool fold_range (irange &r, tree type,
72 const frange &lh,
73 const frange &rh,
74 relation_trio = TRIO_VARYING) const;
38a73435
AH
75
76 // Return the range for op[12] in the general case. LHS is the range for
77 // the LHS of the expression, OP[12]is the range for the other
78 //
79 // The operand and the result is returned in R.
80 //
81 // TYPE is the expected type of the range.
82 //
83 // Return TRUE if the operation is performed and a valid range is available.
84 //
85 // i.e. [LHS] = ??? + OP2
86 // is re-formed as R = [LHS] - OP2.
4ba9fb0a
AH
87 virtual bool op1_range (irange &r, tree type,
88 const irange &lhs,
80dd13f5 89 const irange &op2,
b565ac19 90 relation_trio = TRIO_VARYING) const;
9c0fed50
AM
91 virtual bool op1_range (frange &r, tree type,
92 const frange &lhs,
93 const frange &op2,
94 relation_trio = TRIO_VARYING) const;
95 virtual bool op1_range (frange &r, tree type,
96 const irange &lhs,
97 const frange &op2,
98 relation_trio = TRIO_VARYING) const;
99
100
4ba9fb0a
AH
101 virtual bool op2_range (irange &r, tree type,
102 const irange &lhs,
80dd13f5 103 const irange &op1,
b565ac19 104 relation_trio = TRIO_VARYING) const;
9c0fed50
AM
105 virtual bool op2_range (frange &r, tree type,
106 const frange &lhs,
107 const frange &op1,
108 relation_trio = TRIO_VARYING) const;
109 virtual bool op2_range (frange &r, tree type,
110 const irange &lhs,
111 const frange &op1,
112 relation_trio = TRIO_VARYING) const;
38a73435 113
80dd13f5
AM
114 // The following routines are used to represent relations between the
115 // various operations. If the caller knows where the symbolics are,
116 // it can query for relationships between them given known ranges.
cf2141a0 117 // the optional relation passed in is the relation between op1 and op2.
ade5531c
AM
118 virtual relation_kind lhs_op1_relation (const irange &lhs,
119 const irange &op1,
120 const irange &op2,
121 relation_kind = VREL_VARYING) const;
9c0fed50
AM
122 virtual relation_kind lhs_op1_relation (const frange &lhs,
123 const frange &op1,
124 const frange &op2,
125 relation_kind = VREL_VARYING) const;
126 virtual relation_kind lhs_op1_relation (const irange &lhs,
127 const frange &op1,
128 const frange &op2,
129 relation_kind = VREL_VARYING) const;
130
ade5531c
AM
131 virtual relation_kind lhs_op2_relation (const irange &lhs,
132 const irange &op1,
133 const irange &op2,
134 relation_kind = VREL_VARYING) const;
9c0fed50
AM
135 virtual relation_kind lhs_op2_relation (const frange &lhs,
136 const frange &op1,
137 const frange &op2,
138 relation_kind = VREL_VARYING) const;
139 virtual relation_kind lhs_op2_relation (const irange &lhs,
140 const frange &op1,
141 const frange &op2,
142 relation_kind = VREL_VARYING) const;
143
ade5531c 144 virtual relation_kind op1_op2_relation (const irange &lhs) const;
9c0fed50 145 virtual relation_kind op1_op2_relation (const frange &lhs) const;
38a73435 146protected:
f674b4a7 147 // Perform an integral operation between 2 sub-ranges and return it.
4ba9fb0a 148 virtual void wi_fold (irange &r, tree type,
bb74ef9e
AM
149 const wide_int &lh_lb,
150 const wide_int &lh_ub,
151 const wide_int &rh_lb,
152 const wide_int &rh_ub) const;
156d7d8d 153 // Effect of relation for generic fold_range clients.
80dd13f5
AM
154 virtual bool op1_op2_relation_effect (irange &lhs_range, tree type,
155 const irange &op1_range,
156 const irange &op2_range,
157 relation_kind rel) const;
704e8a82
AM
158 // Called by fold range to split small subranges into parts.
159 void wi_fold_in_parts (irange &r, tree type,
160 const wide_int &lh_lb,
161 const wide_int &lh_ub,
162 const wide_int &rh_lb,
163 const wide_int &rh_ub) const;
809d661a
AM
164
165 // Called by fold range to split small subranges into parts when op1 == op2
166 void wi_fold_in_parts_equiv (irange &r, tree type,
167 const wide_int &lb,
168 const wide_int &ub,
169 unsigned limit) const;
cd4b7e8b
AM
170 // Apply any bitmasks implied by these ranges.
171 virtual void update_bitmask (irange &, const irange &, const irange &) const;
a6efab5f 172
9c0fed50 173 // Perform an float operation between 2 ranges and return it.
0ef5649e
AH
174 virtual void rv_fold (REAL_VALUE_TYPE &lb, REAL_VALUE_TYPE &ub,
175 bool &maybe_nan,
176 tree type,
177 const REAL_VALUE_TYPE &lh_lb,
178 const REAL_VALUE_TYPE &lh_ub,
179 const REAL_VALUE_TYPE &rh_lb,
2f7f9edd
JJ
180 const REAL_VALUE_TYPE &rh_ub,
181 relation_kind) const;
a6efab5f
AH
182};
183
cf5bea76
AH
184class range_op_handler
185{
186public:
51ce0638 187 range_op_handler ();
5410b07a 188 range_op_handler (unsigned);
1b1de36a
AM
189 operator bool () const;
190 range_operator *range_op () const;
cf5bea76
AH
191
192 bool fold_range (vrange &r, tree type,
193 const vrange &lh,
194 const vrange &rh,
b565ac19 195 relation_trio = TRIO_VARYING) const;
cf5bea76
AH
196 bool op1_range (vrange &r, tree type,
197 const vrange &lhs,
198 const vrange &op2,
b565ac19 199 relation_trio = TRIO_VARYING) const;
cf5bea76
AH
200 bool op2_range (vrange &r, tree type,
201 const vrange &lhs,
202 const vrange &op1,
b565ac19 203 relation_trio = TRIO_VARYING) const;
cf5bea76
AH
204 relation_kind lhs_op1_relation (const vrange &lhs,
205 const vrange &op1,
206 const vrange &op2,
207 relation_kind = VREL_VARYING) const;
208 relation_kind lhs_op2_relation (const vrange &lhs,
209 const vrange &op1,
210 const vrange &op2,
211 relation_kind = VREL_VARYING) const;
212 relation_kind op1_op2_relation (const vrange &lhs) const;
51ce0638 213protected:
cd9c7f89
AM
214 unsigned dispatch_kind (const vrange &lhs, const vrange &op1,
215 const vrange& op2) const;
cd9c7f89 216 range_operator *m_operator;
cf5bea76
AH
217};
218
c570818b
AM
219// Cast the range in R to TYPE if R supports TYPE.
220
221inline bool
222range_cast (vrange &r, tree type)
223{
224 gcc_checking_assert (r.supports_type_p (type));
225 Value_Range tmp (r);
226 Value_Range varying (type);
227 varying.set_varying (type);
c570818b 228 // Call op_convert, if it fails, the result is varying.
2eb50117 229 if (!range_op_handler (CONVERT_EXPR).fold_range (r, type, tmp, varying))
c570818b
AM
230 {
231 r.set_varying (type);
232 return false;
233 }
234 return true;
235}
236
237// Range cast which is capable of switching range kinds.
238// ie for float to int.
239
240inline bool
241range_cast (Value_Range &r, tree type)
242{
243 Value_Range tmp (r);
244 Value_Range varying (type);
245 varying.set_varying (type);
246
247 // Ensure we are in the correct mode for the call to fold.
248 r.set_type (type);
249
c570818b 250 // Call op_convert, if it fails, the result is varying.
2eb50117 251 if (!range_op_handler (CONVERT_EXPR).fold_range (r, type, tmp, varying))
c570818b
AM
252 {
253 r.set_varying (type);
254 return false;
255 }
256 return true;
257}
258
259
8f119c55
AH
260extern void wi_set_zero_nonzero_bits (tree type,
261 const wide_int &, const wide_int &,
262 wide_int &maybe_nonzero,
263 wide_int &mustbe_nonzero);
38a73435 264
5410b07a
AM
265// These are extra operators that do not fit in the normal scheme of things.
266// Add them to the end of the tree-code vector, and provide a name for
267// each allowing for easy access when required.
268
269#define OP_WIDEN_MULT_SIGNED ((unsigned) MAX_TREE_CODES)
270#define OP_WIDEN_MULT_UNSIGNED ((unsigned) MAX_TREE_CODES + 1)
271#define OP_WIDEN_PLUS_SIGNED ((unsigned) MAX_TREE_CODES + 2)
272#define OP_WIDEN_PLUS_UNSIGNED ((unsigned) MAX_TREE_CODES + 3)
273#define RANGE_OP_TABLE_SIZE ((unsigned) MAX_TREE_CODES + 4)
274
9eb38e88
AH
275// This implements the range operator tables as local objects.
276
277class range_op_table
278{
279public:
1c0aae69 280 range_op_table ();
5410b07a 281 inline range_operator *operator[] (unsigned code)
1c0aae69 282 {
5410b07a 283 gcc_checking_assert (code < RANGE_OP_TABLE_SIZE);
1c0aae69
AM
284 return m_range_tree[code];
285 }
07767389 286protected:
5410b07a 287 inline void set (unsigned code, range_operator &op)
1c0aae69 288 {
5410b07a 289 gcc_checking_assert (code < RANGE_OP_TABLE_SIZE);
1c0aae69
AM
290 gcc_checking_assert (m_range_tree[code] == NULL);
291 m_range_tree[code] = &op;
292 }
5410b07a 293 range_operator *m_range_tree[RANGE_OP_TABLE_SIZE];
07767389
AM
294 void initialize_integral_ops ();
295 void initialize_pointer_ops ();
296 void initialize_float_ops ();
9eb38e88 297};
38a73435 298#endif // GCC_RANGE_OP_H