1 /* Header file for range operator class.
2 Copyright (C) 2017-2023 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
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
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_RANGE_OP_H
23 #define GCC_RANGE_OP_H
25 // This class is implemented for each kind of operator supported by
26 // the range generator. It serves various purposes.
28 // 1 - Generates range information for the specific operation between
29 // two ranges. This provides the ability to fold ranges for an
32 // 2 - Performs range algebra on the expression such that a range can be
33 // adjusted in terms of one of the operands:
37 // Given a range for def, we can adjust the range so that it is in
38 // terms of either operand.
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.
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
47 // ie 0 = op1 - op2 implies op2 has the same range as op1.
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
56 friend class range_op_table
;
58 // Perform an operation between 2 ranges and return it.
59 virtual bool fold_range (irange
&r
, tree type
,
62 relation_trio
= TRIO_VARYING
) const;
63 virtual bool fold_range (frange
&r
, tree type
,
66 relation_trio
= TRIO_VARYING
) const;
67 virtual bool fold_range (irange
&r
, tree type
,
70 relation_trio
= TRIO_VARYING
) const;
71 virtual bool fold_range (irange
&r
, tree type
,
74 relation_trio
= TRIO_VARYING
) const;
75 virtual bool fold_range (frange
&r
, tree type
,
78 relation_trio
= TRIO_VARYING
) const;
80 // Return the range for op[12] in the general case. LHS is the range for
81 // the LHS of the expression, OP[12]is the range for the other
83 // The operand and the result is returned in R.
85 // TYPE is the expected type of the range.
87 // Return TRUE if the operation is performed and a valid range is available.
89 // i.e. [LHS] = ??? + OP2
90 // is re-formed as R = [LHS] - OP2.
91 virtual bool op1_range (irange
&r
, tree type
,
94 relation_trio
= TRIO_VARYING
) const;
95 virtual bool op1_range (frange
&r
, tree type
,
98 relation_trio
= TRIO_VARYING
) const;
99 virtual bool op1_range (frange
&r
, tree type
,
102 relation_trio
= TRIO_VARYING
) const;
105 virtual bool op2_range (irange
&r
, tree type
,
108 relation_trio
= TRIO_VARYING
) const;
109 virtual bool op2_range (frange
&r
, tree type
,
112 relation_trio
= TRIO_VARYING
) const;
113 virtual bool op2_range (frange
&r
, tree type
,
116 relation_trio
= TRIO_VARYING
) const;
118 // The following routines are used to represent relations between the
119 // various operations. If the caller knows where the symbolics are,
120 // it can query for relationships between them given known ranges.
121 // the optional relation passed in is the relation between op1 and op2.
122 virtual relation_kind
lhs_op1_relation (const irange
&lhs
,
125 relation_kind
= VREL_VARYING
) const;
126 virtual relation_kind
lhs_op1_relation (const frange
&lhs
,
129 relation_kind
= VREL_VARYING
) const;
130 virtual relation_kind
lhs_op1_relation (const irange
&lhs
,
133 relation_kind
= VREL_VARYING
) const;
135 virtual relation_kind
lhs_op2_relation (const irange
&lhs
,
138 relation_kind
= VREL_VARYING
) const;
139 virtual relation_kind
lhs_op2_relation (const frange
&lhs
,
142 relation_kind
= VREL_VARYING
) const;
143 virtual relation_kind
lhs_op2_relation (const irange
&lhs
,
146 relation_kind
= VREL_VARYING
) const;
148 virtual relation_kind
op1_op2_relation (const irange
&lhs
,
150 const irange
&op2
) const;
151 virtual relation_kind
op1_op2_relation (const irange
&lhs
,
153 const frange
&op2
) const;
154 virtual relation_kind
op1_op2_relation (const frange
&lhs
,
156 const frange
&op2
) const;
158 // Perform an integral operation between 2 sub-ranges and return it.
159 virtual void wi_fold (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;
164 // Effect of relation for generic fold_range clients.
165 virtual bool op1_op2_relation_effect (irange
&lhs_range
, tree type
,
166 const irange
&op1_range
,
167 const irange
&op2_range
,
168 relation_kind rel
) const;
169 // Called by fold range to split small subranges into parts.
170 void wi_fold_in_parts (irange
&r
, tree type
,
171 const wide_int
&lh_lb
,
172 const wide_int
&lh_ub
,
173 const wide_int
&rh_lb
,
174 const wide_int
&rh_ub
) const;
176 // Called by fold range to split small subranges into parts when op1 == op2
177 void wi_fold_in_parts_equiv (irange
&r
, tree type
,
180 unsigned limit
) const;
181 // Apply any bitmasks implied by these ranges.
182 virtual void update_bitmask (irange
&, const irange
&, const irange
&) const;
184 // Perform an float operation between 2 ranges and return it.
185 virtual void rv_fold (REAL_VALUE_TYPE
&lb
, REAL_VALUE_TYPE
&ub
,
188 const REAL_VALUE_TYPE
&lh_lb
,
189 const REAL_VALUE_TYPE
&lh_ub
,
190 const REAL_VALUE_TYPE
&rh_lb
,
191 const REAL_VALUE_TYPE
&rh_ub
,
192 relation_kind
) const;
195 class range_op_handler
199 range_op_handler (unsigned);
200 operator bool () const;
201 range_operator
*range_op () const;
203 bool fold_range (vrange
&r
, tree type
,
206 relation_trio
= TRIO_VARYING
) const;
207 bool op1_range (vrange
&r
, tree type
,
210 relation_trio
= TRIO_VARYING
) const;
211 bool op2_range (vrange
&r
, tree type
,
214 relation_trio
= TRIO_VARYING
) const;
215 relation_kind
lhs_op1_relation (const vrange
&lhs
,
218 relation_kind
= VREL_VARYING
) const;
219 relation_kind
lhs_op2_relation (const vrange
&lhs
,
222 relation_kind
= VREL_VARYING
) const;
223 relation_kind
op1_op2_relation (const vrange
&lhs
,
225 const vrange
&op2
) const;
227 unsigned dispatch_kind (const vrange
&lhs
, const vrange
&op1
,
228 const vrange
& op2
) const;
229 range_operator
*m_operator
;
232 // Cast the range in R to TYPE if R supports TYPE.
235 range_cast (vrange
&r
, tree type
)
237 gcc_checking_assert (r
.supports_type_p (type
));
239 Value_Range
varying (type
);
240 varying
.set_varying (type
);
241 // Call op_convert, if it fails, the result is varying.
242 if (!range_op_handler (CONVERT_EXPR
).fold_range (r
, type
, tmp
, varying
))
244 r
.set_varying (type
);
250 // Range cast which is capable of switching range kinds.
251 // ie for float to int.
254 range_cast (Value_Range
&r
, tree type
)
257 Value_Range
varying (type
);
258 varying
.set_varying (type
);
260 // Ensure we are in the correct mode for the call to fold.
263 // Call op_convert, if it fails, the result is varying.
264 if (!range_op_handler (CONVERT_EXPR
).fold_range (r
, type
, tmp
, varying
))
266 r
.set_varying (type
);
273 extern void wi_set_zero_nonzero_bits (tree type
,
274 const wide_int
&, const wide_int
&,
275 wide_int
&maybe_nonzero
,
276 wide_int
&mustbe_nonzero
);
278 // These are extra operators that do not fit in the normal scheme of things.
279 // Add them to the end of the tree-code vector, and provide a name for
280 // each allowing for easy access when required.
282 #define OP_WIDEN_MULT_SIGNED ((unsigned) MAX_TREE_CODES)
283 #define OP_WIDEN_MULT_UNSIGNED ((unsigned) MAX_TREE_CODES + 1)
284 #define OP_WIDEN_PLUS_SIGNED ((unsigned) MAX_TREE_CODES + 2)
285 #define OP_WIDEN_PLUS_UNSIGNED ((unsigned) MAX_TREE_CODES + 3)
286 #define RANGE_OP_TABLE_SIZE ((unsigned) MAX_TREE_CODES + 4)
288 // This implements the range operator tables as local objects.
294 inline range_operator
*operator[] (unsigned code
)
296 gcc_checking_assert (code
< RANGE_OP_TABLE_SIZE
);
297 return m_range_tree
[code
];
300 inline void set (unsigned code
, range_operator
&op
)
302 gcc_checking_assert (code
< RANGE_OP_TABLE_SIZE
);
303 gcc_checking_assert (m_range_tree
[code
] == NULL
);
304 m_range_tree
[code
] = &op
;
306 range_operator
*m_range_tree
[RANGE_OP_TABLE_SIZE
];
307 void initialize_integral_ops ();
308 void initialize_pointer_ops ();
309 void initialize_float_ops ();
311 #endif // GCC_RANGE_OP_H