compiler: only build thunk struct type when it is needed
[official-gcc.git] / gcc / range-op.h
blobb2f063afb07475337e7fe9f6c4aa7ab51d9a4069
1 /* Header file for range operator class.
2 Copyright (C) 2017-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_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
30 // expression.
32 // 2 - Performs range algebra on the expression such that a range can be
33 // adjusted in terms of one of the operands:
35 // def = op1 + op2
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
46 // purposes.
47 // ie 0 = op1 - op2 implies op2 has the same range as op1.
49 class range_operator
51 public:
52 // Perform an operation between 2 ranges and return it.
53 virtual bool fold_range (irange &r, tree type,
54 const irange &lh,
55 const irange &rh,
56 relation_kind rel = VREL_VARYING) const;
58 // Return the range for op[12] in the general case. LHS is the range for
59 // the LHS of the expression, OP[12]is the range for the other
61 // The operand and the result is returned in R.
63 // TYPE is the expected type of the range.
65 // Return TRUE if the operation is performed and a valid range is available.
67 // i.e. [LHS] = ??? + OP2
68 // is re-formed as R = [LHS] - OP2.
69 virtual bool op1_range (irange &r, tree type,
70 const irange &lhs,
71 const irange &op2,
72 relation_kind rel = VREL_VARYING) const;
73 virtual bool op2_range (irange &r, tree type,
74 const irange &lhs,
75 const irange &op1,
76 relation_kind rel = VREL_VARYING) const;
78 // The following routines are used to represent relations between the
79 // various operations. If the caller knows where the symbolics are,
80 // it can query for relationships between them given known ranges.
81 // the optional relation passed in is the relation between op1 and op2.
82 virtual relation_kind lhs_op1_relation (const irange &lhs,
83 const irange &op1,
84 const irange &op2,
85 relation_kind = VREL_VARYING) const;
86 virtual relation_kind lhs_op2_relation (const irange &lhs,
87 const irange &op1,
88 const irange &op2,
89 relation_kind = VREL_VARYING) const;
90 virtual relation_kind op1_op2_relation (const irange &lhs) const;
91 protected:
92 // Perform an integral operation between 2 sub-ranges and return it.
93 virtual void wi_fold (irange &r, tree type,
94 const wide_int &lh_lb,
95 const wide_int &lh_ub,
96 const wide_int &rh_lb,
97 const wide_int &rh_ub) const;
98 // Effect of relation for generic fold_range clients.
99 virtual bool op1_op2_relation_effect (irange &lhs_range, tree type,
100 const irange &op1_range,
101 const irange &op2_range,
102 relation_kind rel) const;
103 // Called by fold range to split small subranges into parts.
104 void wi_fold_in_parts (irange &r, tree type,
105 const wide_int &lh_lb,
106 const wide_int &lh_ub,
107 const wide_int &rh_lb,
108 const wide_int &rh_ub) const;
111 // Like range_operator above, but for floating point operators.
113 class range_operator_float
115 public:
116 virtual bool fold_range (frange &r, tree type,
117 const frange &lh,
118 const frange &rh,
119 relation_kind rel = VREL_VARYING) const;
120 // Unary operations have the range of the LHS as op2.
121 virtual bool fold_range (irange &r, tree type,
122 const frange &lh,
123 const irange &rh,
124 relation_kind rel = VREL_VARYING) const;
125 virtual bool fold_range (irange &r, tree type,
126 const frange &lh,
127 const frange &rh,
128 relation_kind rel = VREL_VARYING) const;
129 virtual bool op1_range (frange &r, tree type,
130 const frange &lhs,
131 const frange &op2,
132 relation_kind rel = VREL_VARYING) const;
133 virtual bool op1_range (frange &r, tree type,
134 const irange &lhs,
135 const frange &op2,
136 relation_kind rel = VREL_VARYING) const;
137 virtual bool op2_range (frange &r, tree type,
138 const frange &lhs,
139 const frange &op1,
140 relation_kind rel = VREL_VARYING) const;
141 virtual bool op2_range (frange &r, tree type,
142 const irange &lhs,
143 const frange &op1,
144 relation_kind rel = VREL_VARYING) const;
146 virtual relation_kind lhs_op1_relation (const frange &lhs,
147 const frange &op1,
148 const frange &op2,
149 relation_kind = VREL_VARYING) const;
150 virtual relation_kind lhs_op1_relation (const irange &lhs,
151 const frange &op1,
152 const frange &op2,
153 relation_kind = VREL_VARYING) const;
154 virtual relation_kind lhs_op2_relation (const frange &lhs,
155 const frange &op1,
156 const frange &op2,
157 relation_kind = VREL_VARYING) const;
158 virtual relation_kind lhs_op2_relation (const irange &lhs,
159 const frange &op1,
160 const frange &op2,
161 relation_kind = VREL_VARYING) const;
162 virtual relation_kind op1_op2_relation (const irange &lhs) const;
165 class range_op_handler
167 public:
168 range_op_handler ();
169 range_op_handler (enum tree_code code, tree type);
170 inline operator bool () const { return m_valid; }
172 bool fold_range (vrange &r, tree type,
173 const vrange &lh,
174 const vrange &rh,
175 relation_kind rel = VREL_VARYING) const;
176 bool op1_range (vrange &r, tree type,
177 const vrange &lhs,
178 const vrange &op2,
179 relation_kind rel = VREL_VARYING) const;
180 bool op2_range (vrange &r, tree type,
181 const vrange &lhs,
182 const vrange &op1,
183 relation_kind rel = VREL_VARYING) const;
184 relation_kind lhs_op1_relation (const vrange &lhs,
185 const vrange &op1,
186 const vrange &op2,
187 relation_kind = VREL_VARYING) const;
188 relation_kind lhs_op2_relation (const vrange &lhs,
189 const vrange &op1,
190 const vrange &op2,
191 relation_kind = VREL_VARYING) const;
192 relation_kind op1_op2_relation (const vrange &lhs) const;
193 protected:
194 void set_op_handler (enum tree_code code, tree type);
195 bool m_valid;
196 range_operator *m_int;
197 range_operator_float *m_float;
200 extern bool range_cast (vrange &, tree type);
201 extern void wi_set_zero_nonzero_bits (tree type,
202 const wide_int &, const wide_int &,
203 wide_int &maybe_nonzero,
204 wide_int &mustbe_nonzero);
206 // op1_op2_relation methods that are the same across irange and frange.
207 relation_kind equal_op1_op2_relation (const irange &lhs);
208 relation_kind not_equal_op1_op2_relation (const irange &lhs);
209 relation_kind lt_op1_op2_relation (const irange &lhs);
210 relation_kind le_op1_op2_relation (const irange &lhs);
211 relation_kind gt_op1_op2_relation (const irange &lhs);
212 relation_kind ge_op1_op2_relation (const irange &lhs);
214 enum bool_range_state { BRS_FALSE, BRS_TRUE, BRS_EMPTY, BRS_FULL };
215 bool_range_state get_bool_state (vrange &r, const vrange &lhs, tree val_type);
217 // If the range of either op1 or op2 is undefined, set the result to
218 // varying and return TRUE. If the caller truely cares about a result,
219 // they should pass in a varying if it has an undefined that it wants
220 // treated as a varying.
222 inline bool
223 empty_range_varying (vrange &r, tree type,
224 const vrange &op1, const vrange & op2)
226 if (op1.undefined_p () || op2.undefined_p ())
228 r.set_varying (type);
229 return true;
231 else
232 return false;
235 // For relation opcodes, first try to see if the supplied relation
236 // forces a true or false result, and return that.
237 // Then check for undefined operands. If none of this applies,
238 // return false.
240 inline bool
241 relop_early_resolve (irange &r, tree type, const vrange &op1,
242 const vrange &op2, relation_kind rel,
243 relation_kind my_rel)
245 // If known relation is a complete subset of this relation, always true.
246 if (relation_union (rel, my_rel) == my_rel)
248 r = range_true (type);
249 return true;
252 // If known relation has no subset of this relation, always false.
253 if (relation_intersect (rel, my_rel) == VREL_UNDEFINED)
255 r = range_false (type);
256 return true;
259 // If either operand is undefined, return VARYING.
260 if (empty_range_varying (r, type, op1, op2))
261 return true;
263 return false;
266 // This implements the range operator tables as local objects.
268 class range_op_table
270 public:
271 range_operator *operator[] (enum tree_code code);
272 protected:
273 void set (enum tree_code code, range_operator &op);
274 private:
275 range_operator *m_range_tree[MAX_TREE_CODES];
278 // Like above, but for floating point operators.
280 class floating_op_table
282 public:
283 floating_op_table ();
284 range_operator_float *operator[] (enum tree_code code);
285 private:
286 void set (enum tree_code code, range_operator_float &op);
287 range_operator_float *m_range_tree[MAX_TREE_CODES];
290 // This holds the range op table for floating point operations.
291 extern floating_op_table *floating_tree_table;
293 #endif // GCC_RANGE_OP_H