1/* Gimple simplify definitions.
2
3 Copyright (C) 2011-2025 Free Software Foundation, Inc.
4 Contributed by Richard Guenther <rguenther@suse.de>
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
16for 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_MATCH_H
23#define GCC_GIMPLE_MATCH_H
24
25
26/* Represents the condition under which an operation should happen,
27 and the value to use otherwise. The condition applies elementwise
28 (as for VEC_COND_EXPR) if the values are vectors. */
29class gimple_match_cond
30{
31public:
32 enum uncond { UNCOND };
33
34 /* Build an unconditional op. */
35 gimple_match_cond (uncond) : cond (NULL_TREE), else_value (NULL_TREE), len
36 (NULL_TREE), bias (NULL_TREE) {}
37 gimple_match_cond (tree, tree);
38 gimple_match_cond (tree, tree, tree, tree);
39
40 gimple_match_cond any_else () const;
41
42 /* The condition under which the operation occurs, or NULL_TREE
43 if the operation is unconditional. */
44 tree cond;
45
46 /* The value to use when the condition is false. This is NULL_TREE if
47 the operation is unconditional or if the value doesn't matter. */
48 tree else_value;
49
50 /* The length and bias parameters to be applied to a vector operation,
51 so that the condition is forced to false when the element index is
52 >= LEN + BIAS. These are NULL_TREE if the operation isn't applied
53 to vectors, or if no such length limit is in use. */
54 tree len;
55 tree bias;
56};
57
58inline
59gimple_match_cond::gimple_match_cond (tree cond_in, tree else_value_in)
60 : cond (cond_in), else_value (else_value_in), len (NULL_TREE),
61 bias (NULL_TREE)
62{
63}
64
65inline
66gimple_match_cond::gimple_match_cond (tree cond_in, tree else_value_in,
67 tree len_in, tree bias_in)
68 : cond (cond_in), else_value (else_value_in), len (len_in), bias (bias_in)
69{}
70
71/* Return a gimple_match_cond with the same condition but with an
72 arbitrary ELSE_VALUE. */
73
74inline gimple_match_cond
75gimple_match_cond::any_else () const
76{
77 return gimple_match_cond (cond, NULL_TREE);
78}
79
80/* Represents an operation to be simplified, or the result of the
81 simplification. */
82class gimple_match_op
83{
84public:
85 gimple_match_op ();
86 gimple_match_op (const gimple_match_cond &, code_helper, tree, unsigned int);
87 gimple_match_op (const gimple_match_cond &,
88 code_helper, tree, tree);
89 gimple_match_op (const gimple_match_cond &,
90 code_helper, tree, tree, tree);
91 gimple_match_op (const gimple_match_cond &,
92 code_helper, tree, tree, tree, tree);
93 gimple_match_op (const gimple_match_cond &,
94 code_helper, tree, tree, tree, tree, tree);
95 gimple_match_op (const gimple_match_cond &,
96 code_helper, tree, tree, tree, tree, tree, tree);
97 gimple_match_op (const gimple_match_cond &,
98 code_helper, tree, tree, tree, tree, tree, tree, tree);
99 gimple_match_op (const gimple_match_cond &,
100 code_helper, tree, tree, tree, tree, tree, tree, tree, tree);
101
102 void set_op (code_helper, tree, unsigned int);
103 void set_op (code_helper, tree, tree);
104 void set_op (code_helper, tree, tree, tree);
105 void set_op (code_helper, tree, tree, tree, tree);
106 void set_op (code_helper, tree, tree, tree, tree, bool);
107 void set_op (code_helper, tree, tree, tree, tree, tree);
108 void set_op (code_helper, tree, tree, tree, tree, tree, tree);
109 void set_op (code_helper, tree, tree, tree, tree, tree, tree, tree);
110 void set_op (code_helper, tree, tree, tree, tree, tree, tree, tree, tree);
111 void set_value (tree);
112
113 tree op_or_null (unsigned int) const;
114
115 bool resimplify (gimple_seq *, tree (*)(tree));
116
117 /* The maximum value of NUM_OPS. */
118 static const unsigned int MAX_NUM_OPS = 7;
119
120 /* The conditions under which the operation is performed, and the value to
121 use as a fallback. */
122 gimple_match_cond cond;
123
124 /* The operation being performed. */
125 code_helper code;
126
127 /* The type of the result. */
128 tree type;
129
130 /* For a BIT_FIELD_REF, whether the group of bits is stored in reverse order
131 from the target order. */
132 bool reverse;
133
134 /* The number of operands to CODE. */
135 unsigned int num_ops;
136
137 /* The operands to CODE. Only the first NUM_OPS entries are meaningful. */
138 tree ops[MAX_NUM_OPS];
139
140 bool operands_occurs_in_abnormal_phi() const;
141};
142
143inline
144gimple_match_op::gimple_match_op ()
145 : cond (gimple_match_cond::UNCOND), type (NULL_TREE), reverse (false),
146 num_ops (0)
147{
148}
149
150/* Constructor that takes the condition, code, type and number of
151 operands, but leaves the caller to fill in the operands. */
152
153inline
154gimple_match_op::gimple_match_op (const gimple_match_cond &cond_in,
155 code_helper code_in, tree type_in,
156 unsigned int num_ops_in)
157 : cond (cond_in), code (code_in), type (type_in), reverse (false),
158 num_ops (num_ops_in)
159{
160}
161
162/* Constructors for various numbers of operands. */
163
164inline
165gimple_match_op::gimple_match_op (const gimple_match_cond &cond_in,
166 code_helper code_in, tree type_in,
167 tree op0)
168 : cond (cond_in), code (code_in), type (type_in), reverse (false),
169 num_ops (1)
170{
171 ops[0] = op0;
172}
173
174inline
175gimple_match_op::gimple_match_op (const gimple_match_cond &cond_in,
176 code_helper code_in, tree type_in,
177 tree op0, tree op1)
178 : cond (cond_in), code (code_in), type (type_in), reverse (false),
179 num_ops (2)
180{
181 ops[0] = op0;
182 ops[1] = op1;
183}
184
185inline
186gimple_match_op::gimple_match_op (const gimple_match_cond &cond_in,
187 code_helper code_in, tree type_in,
188 tree op0, tree op1, tree op2)
189 : cond (cond_in), code (code_in), type (type_in), reverse (false),
190 num_ops (3)
191{
192 ops[0] = op0;
193 ops[1] = op1;
194 ops[2] = op2;
195}
196
197inline
198gimple_match_op::gimple_match_op (const gimple_match_cond &cond_in,
199 code_helper code_in, tree type_in,
200 tree op0, tree op1, tree op2, tree op3)
201 : cond (cond_in), code (code_in), type (type_in), reverse (false),
202 num_ops (4)
203{
204 ops[0] = op0;
205 ops[1] = op1;
206 ops[2] = op2;
207 ops[3] = op3;
208}
209
210inline
211gimple_match_op::gimple_match_op (const gimple_match_cond &cond_in,
212 code_helper code_in, tree type_in,
213 tree op0, tree op1, tree op2, tree op3,
214 tree op4)
215 : cond (cond_in), code (code_in), type (type_in), reverse (false),
216 num_ops (5)
217{
218 ops[0] = op0;
219 ops[1] = op1;
220 ops[2] = op2;
221 ops[3] = op3;
222 ops[4] = op4;
223}
224
225inline
226gimple_match_op::gimple_match_op (const gimple_match_cond &cond_in,
227 code_helper code_in, tree type_in,
228 tree op0, tree op1, tree op2, tree op3,
229 tree op4, tree op5)
230 : cond (cond_in), code (code_in), type (type_in), reverse (false),
231 num_ops (6)
232{
233 ops[0] = op0;
234 ops[1] = op1;
235 ops[2] = op2;
236 ops[3] = op3;
237 ops[4] = op4;
238 ops[5] = op5;
239}
240
241inline
242gimple_match_op::gimple_match_op (const gimple_match_cond &cond_in,
243 code_helper code_in, tree type_in,
244 tree op0, tree op1, tree op2, tree op3,
245 tree op4, tree op5, tree op6)
246 : cond (cond_in), code (code_in), type (type_in), reverse (false),
247 num_ops (7)
248{
249 ops[0] = op0;
250 ops[1] = op1;
251 ops[2] = op2;
252 ops[3] = op3;
253 ops[4] = op4;
254 ops[5] = op5;
255 ops[6] = op6;
256}
257
258/* Change the operation performed to CODE_IN, the type of the result to
259 TYPE_IN, and the number of operands to NUM_OPS_IN. The caller needs
260 to set the operands itself. */
261
262inline void
263gimple_match_op::set_op (code_helper code_in, tree type_in,
264 unsigned int num_ops_in)
265{
266 code = code_in;
267 type = type_in;
268 num_ops = num_ops_in;
269}
270
271/* Functions for changing the operation performed, for various numbers
272 of operands. */
273
274inline void
275gimple_match_op::set_op (code_helper code_in, tree type_in, tree op0)
276{
277 code = code_in;
278 type = type_in;
279 num_ops = 1;
280 ops[0] = op0;
281}
282
283inline void
284gimple_match_op::set_op (code_helper code_in, tree type_in, tree op0, tree op1)
285{
286 code = code_in;
287 type = type_in;
288 num_ops = 2;
289 ops[0] = op0;
290 ops[1] = op1;
291}
292
293inline void
294gimple_match_op::set_op (code_helper code_in, tree type_in,
295 tree op0, tree op1, tree op2)
296{
297 code = code_in;
298 type = type_in;
299 num_ops = 3;
300 ops[0] = op0;
301 ops[1] = op1;
302 ops[2] = op2;
303}
304
305inline void
306gimple_match_op::set_op (code_helper code_in, tree type_in,
307 tree op0, tree op1, tree op2, bool reverse_in)
308{
309 code = code_in;
310 type = type_in;
311 reverse = reverse_in;
312 num_ops = 3;
313 ops[0] = op0;
314 ops[1] = op1;
315 ops[2] = op2;
316}
317
318inline void
319gimple_match_op::set_op (code_helper code_in, tree type_in,
320 tree op0, tree op1, tree op2, tree op3)
321{
322 code = code_in;
323 type = type_in;
324 num_ops = 4;
325 ops[0] = op0;
326 ops[1] = op1;
327 ops[2] = op2;
328 ops[3] = op3;
329}
330
331inline void
332gimple_match_op::set_op (code_helper code_in, tree type_in,
333 tree op0, tree op1, tree op2, tree op3, tree op4)
334{
335 code = code_in;
336 type = type_in;
337 num_ops = 5;
338 ops[0] = op0;
339 ops[1] = op1;
340 ops[2] = op2;
341 ops[3] = op3;
342 ops[4] = op4;
343}
344
345inline void
346gimple_match_op::set_op (code_helper code_in, tree type_in,
347 tree op0, tree op1, tree op2, tree op3, tree op4,
348 tree op5)
349{
350 code = code_in;
351 type = type_in;
352 num_ops = 6;
353 ops[0] = op0;
354 ops[1] = op1;
355 ops[2] = op2;
356 ops[3] = op3;
357 ops[4] = op4;
358 ops[5] = op5;
359}
360
361inline void
362gimple_match_op::set_op (code_helper code_in, tree type_in,
363 tree op0, tree op1, tree op2, tree op3, tree op4,
364 tree op5, tree op6)
365{
366 code = code_in;
367 type = type_in;
368 num_ops = 7;
369 ops[0] = op0;
370 ops[1] = op1;
371 ops[2] = op2;
372 ops[3] = op3;
373 ops[4] = op4;
374 ops[5] = op5;
375 ops[6] = op6;
376}
377
378/* Set the "operation" to be the single value VALUE, such as a constant
379 or SSA_NAME. */
380
381inline void
382gimple_match_op::set_value (tree value)
383{
384 set_op (TREE_CODE (value), TREE_TYPE (value), op0: value);
385}
386
387/* Return the value of operand I, or null if there aren't that many
388 operands. */
389
390inline tree
391gimple_match_op::op_or_null (unsigned int i) const
392{
393 return i < num_ops ? ops[i] : NULL_TREE;
394}
395
396/* Return whether OP is a non-expression result and a gimple value. */
397
398inline bool
399gimple_simplified_result_is_gimple_val (const gimple_match_op *op)
400{
401 return (op->code.is_tree_code ()
402 && (TREE_CODE_LENGTH ((tree_code) op->code) == 0
403 || ((tree_code) op->code) == ADDR_EXPR)
404 && is_gimple_val (op->ops[0]));
405}
406
407extern tree (*mprts_hook) (gimple_match_op *);
408
409bool gimple_extract_op (gimple *, gimple_match_op *);
410bool gimple_simplify (gimple *, gimple_match_op *, gimple_seq *,
411 tree (*)(tree), tree (*)(tree));
412tree maybe_push_res_to_seq (gimple_match_op *, gimple_seq *,
413 tree res = NULL_TREE);
414void maybe_build_generic_op (gimple_match_op *);
415
416bool commutative_binary_op_p (code_helper, tree);
417bool commutative_ternary_op_p (code_helper, tree);
418int first_commutative_argument (code_helper, tree);
419bool associative_binary_op_p (code_helper, tree);
420code_helper canonicalize_code (code_helper, tree);
421
422#ifdef GCC_OPTABS_TREE_H
423bool directly_supported_p (code_helper, tree, optab_subtype = optab_default);
424bool directly_supported_p (code_helper, tree, tree,
425 optab_subtype = optab_default);
426#endif
427
428internal_fn get_conditional_internal_fn (code_helper, tree);
429
430#endif /* GCC_GIMPLE_MATCH_H */
431

source code of gcc/gimple-match.h