Line data Source code
1 : /* Preamble and helpers for the autogenerated gimple-match.cc file.
2 : Copyright (C) 2014-2026 Free Software Foundation, Inc.
3 :
4 : This file is part of GCC.
5 :
6 : GCC is free software; you can redistribute it and/or modify it under
7 : the terms of the GNU General Public License as published by the Free
8 : Software Foundation; either version 3, or (at your option) any later
9 : version.
10 :
11 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with GCC; see the file COPYING3. If not see
18 : <http://www.gnu.org/licenses/>. */
19 :
20 : #include "config.h"
21 : #include "system.h"
22 : #include "coretypes.h"
23 : #include "backend.h"
24 : #include "target.h"
25 : #include "rtl.h"
26 : #include "tree.h"
27 : #include "gimple.h"
28 : #include "ssa.h"
29 : #include "cgraph.h"
30 : #include "vec-perm-indices.h"
31 : #include "fold-const.h"
32 : #include "fold-const-call.h"
33 : #include "stor-layout.h"
34 : #include "gimple-iterator.h"
35 : #include "gimple-fold.h"
36 : #include "calls.h"
37 : #include "tree-dfa.h"
38 : #include "builtins.h"
39 : #include "gimple-match.h"
40 : #include "tree-pass.h"
41 : #include "internal-fn.h"
42 : #include "case-cfn-macros.h"
43 : #include "gimplify.h"
44 : #include "memmodel.h"
45 : #include "optabs.h"
46 : #include "optabs-tree.h"
47 : #include "tree-eh.h"
48 : #include "dbgcnt.h"
49 : #include "tm.h"
50 : #include "gimple-range.h"
51 : #include "langhooks.h"
52 : #include "attribs.h"
53 : #include "asan.h"
54 : #include "vr-values.h"
55 :
56 : tree do_valueize (tree, tree (*)(tree), bool &);
57 : tree do_valueize (tree (*)(tree), tree);
58 :
59 : /* Helper for the autogenerated code, get at the definition of NAME when
60 : VALUEIZE allows that. */
61 :
62 : inline gimple *
63 15119773277 : get_def (tree (*valueize)(tree), tree name)
64 : {
65 15119773277 : if (valueize && ! valueize (name))
66 : return NULL;
67 6216543614 : return SSA_NAME_DEF_STMT (name);
68 : }
69 :
70 : /* Routine to determine if the types T1 and T2 are effectively
71 : the same for GIMPLE. If T1 or T2 is not a type, the test
72 : applies to their TREE_TYPE. */
73 :
74 : static inline bool
75 218596790 : types_match (tree t1, tree t2)
76 : {
77 218596790 : if (!TYPE_P (t1))
78 1588350 : t1 = TREE_TYPE (t1);
79 218596790 : if (!TYPE_P (t2))
80 2268945 : t2 = TREE_TYPE (t2);
81 :
82 218596790 : return types_compatible_p (t1, t2);
83 : }
84 :
85 : /* Routine to determine if the types T1, T2 and T3 are effectively
86 : the same for GIMPLE. If T1, T2 or T2 is not a type, the test
87 : applies to their TREE_TYPE. */
88 :
89 : static inline bool
90 8701 : types_match (tree t1, tree t2, tree t3)
91 : {
92 8701 : return types_match (t1, t2) && types_match (t2, t3);
93 : }
94 :
95 : /* Return if T has uses in a single stmt. For GIMPLE, we also allow any
96 : non-SSA_NAME (ie constants) and zero uses to cope with uses
97 : that aren't linked up yet. */
98 :
99 : static bool
100 : single_use (const_tree) ATTRIBUTE_PURE;
101 :
102 : static bool
103 12180620 : single_use (const_tree t)
104 : {
105 12180620 : if (TREE_CODE (t) != SSA_NAME)
106 : return true;
107 :
108 : /* Inline return has_zero_uses (t) || has_single_use (t); */
109 12180620 : const ssa_use_operand_t *const head = &(SSA_NAME_IMM_USE_NODE (t));
110 12180620 : const ssa_use_operand_t *ptr;
111 12180620 : gimple *single = NULL;
112 :
113 26516045 : for (ptr = head->next; ptr != head; ptr = ptr->next)
114 20982373 : if (USE_STMT (ptr) && !is_gimple_debug (USE_STMT (ptr)))
115 : {
116 18865633 : if (single && single != USE_STMT (ptr))
117 : return false;
118 : single = USE_STMT (ptr);
119 : }
120 : return true;
121 : }
122 :
123 : /* Return true if math operations should be canonicalized,
124 : e.g. sqrt(sqrt(x)) -> pow(x, 0.25). */
125 :
126 : static inline bool
127 16691 : canonicalize_math_p ()
128 : {
129 16691 : return !cfun || (cfun->curr_properties & PROP_gimple_opt_math) == 0;
130 : }
131 :
132 : /* Return true if math operations that are beneficial only after
133 : vectorization should be canonicalized. */
134 :
135 : static inline bool
136 48734 : canonicalize_math_after_vectorization_p ()
137 : {
138 48734 : return !cfun || (cfun->curr_properties & PROP_gimple_lvec) != 0;
139 : }
140 :
141 : /* Return true if we can still perform transformations that may introduce
142 : vector operations that are not supported by the target. Vector lowering
143 : normally handles those, but after that pass, it becomes unsafe. */
144 :
145 : static inline bool
146 5092 : optimize_vectors_before_lowering_p ()
147 : {
148 5092 : return !cfun || (cfun->curr_properties & PROP_gimple_lvec) == 0;
149 : }
150 :
151 : /* Returns true if the expression T has no side effects
152 : including not trapping. */
153 : static inline bool
154 6132 : expr_no_side_effects_p (tree t)
155 : {
156 : /* For gimple, there should only be gimple val's here. */
157 6132 : gcc_assert (is_gimple_val (t));
158 6132 : return true;
159 : }
160 :
161 : /* Return true if pow(cst, x) should be optimized into exp(log(cst) * x).
162 : As a workaround for SPEC CPU2017 628.pop2_s, don't do it if arg0
163 : is an exact integer, arg1 = phi_res +/- cst1 and phi_res = PHI <cst2, ...>
164 : where cst2 +/- cst1 is an exact integer, because then pow (arg0, arg1)
165 : will likely be exact, while exp (log (arg0) * arg1) might be not.
166 : Also don't do it if arg1 is phi_res above and cst2 is an exact integer. */
167 :
168 : static bool
169 4 : optimize_pow_to_exp (tree arg0, tree arg1)
170 : {
171 4 : gcc_assert (TREE_CODE (arg0) == REAL_CST);
172 4 : if (!real_isinteger (TREE_REAL_CST_PTR (arg0), TYPE_MODE (TREE_TYPE (arg0))))
173 : return true;
174 :
175 4 : if (TREE_CODE (arg1) != SSA_NAME)
176 : return true;
177 :
178 4 : gimple *def = SSA_NAME_DEF_STMT (arg1);
179 4 : gphi *phi = dyn_cast <gphi *> (def);
180 4 : tree cst1 = NULL_TREE;
181 4 : enum tree_code code = ERROR_MARK;
182 4 : if (!phi)
183 : {
184 4 : gassign *assign = dyn_cast<gassign *> (def);
185 4 : if (!assign)
186 : return true;
187 4 : code = gimple_assign_rhs_code (assign);
188 4 : switch (code)
189 : {
190 4 : case PLUS_EXPR:
191 4 : case MINUS_EXPR:
192 4 : break;
193 : default:
194 : return true;
195 : }
196 4 : if (TREE_CODE (gimple_assign_rhs1 (assign)) != SSA_NAME
197 8 : || TREE_CODE (gimple_assign_rhs2 (assign)) != REAL_CST)
198 : return true;
199 :
200 4 : cst1 = gimple_assign_rhs2 (assign);
201 :
202 4 : phi = dyn_cast <gphi *> (SSA_NAME_DEF_STMT (gimple_assign_rhs1 (assign)));
203 : if (!phi)
204 : return true;
205 : }
206 :
207 4 : tree cst2 = NULL_TREE;
208 4 : int n = gimple_phi_num_args (phi);
209 12 : for (int i = 0; i < n; i++)
210 : {
211 8 : tree arg = PHI_ARG_DEF (phi, i);
212 8 : if (TREE_CODE (arg) != REAL_CST)
213 4 : continue;
214 4 : else if (cst2 == NULL_TREE)
215 : cst2 = arg;
216 0 : else if (!operand_equal_p (cst2, arg, 0))
217 : return true;
218 : }
219 :
220 4 : if (cst1 && cst2)
221 4 : cst2 = const_binop (code, TREE_TYPE (cst2), cst2, cst1);
222 4 : if (cst2
223 4 : && TREE_CODE (cst2) == REAL_CST
224 8 : && real_isinteger (TREE_REAL_CST_PTR (cst2),
225 4 : TYPE_MODE (TREE_TYPE (cst2))))
226 4 : return false;
227 : return true;
228 : }
229 :
230 : /* Return true if a division INNER_DIV / DIVISOR where INNER_DIV
231 : is another division can be optimized. Don't optimize if INNER_DIV
232 : is used in a TRUNC_MOD_EXPR with DIVISOR as second operand. */
233 :
234 : static bool
235 15753 : optimize_successive_divisions_p (tree divisor, tree inner_div)
236 : {
237 15753 : if (!gimple_in_ssa_p (cfun))
238 : return false;
239 :
240 15753 : imm_use_iterator imm_iter;
241 15753 : use_operand_p use_p;
242 33913 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter, inner_div)
243 : {
244 31459 : gimple *use_stmt = USE_STMT (use_p);
245 31459 : if (!is_gimple_assign (use_stmt)
246 25940 : || gimple_assign_rhs_code (use_stmt) != TRUNC_MOD_EXPR
247 45221 : || !operand_equal_p (gimple_assign_rhs2 (use_stmt), divisor, 0))
248 18160 : continue;
249 13299 : return false;
250 13299 : }
251 2454 : return true;
252 : }
253 :
254 : /* Return true if EXPR1 and EXPR2 have the same value, but not necessarily
255 : same type. The types can differ through nop conversions. */
256 : #define bitwise_equal_p(expr1, expr2) \
257 : gimple_bitwise_equal_p (expr1, expr2, valueize)
258 :
259 : bool gimple_nop_convert (tree, tree *, tree (*) (tree));
260 : bool gimple_maybe_truncate (tree, tree *, tree (*) (tree));
261 :
262 : /* Helper function for bitwise_equal_p macro. */
263 :
264 : static inline bool
265 2291695 : gimple_bitwise_equal_p (tree expr1, tree expr2, tree (*valueize) (tree))
266 : {
267 2291695 : if (expr1 == expr2)
268 : return true;
269 2288302 : if (!tree_nop_conversion_p (TREE_TYPE (expr1), TREE_TYPE (expr2)))
270 : return false;
271 2270134 : if (TREE_CODE (expr1) == INTEGER_CST && TREE_CODE (expr2) == INTEGER_CST)
272 75 : return wi::to_wide (expr1) == wi::to_wide (expr2);
273 2270059 : if (operand_equal_p (expr1, expr2, 0))
274 : return true;
275 2270059 : tree expr3, expr4;
276 2270059 : if (!gimple_nop_convert (expr1, &expr3, valueize))
277 2255565 : expr3 = expr1;
278 2270059 : if (!gimple_nop_convert (expr2, &expr4, valueize))
279 2258814 : expr4 = expr2;
280 2270059 : if (expr1 != expr3)
281 : {
282 14494 : if (operand_equal_p (expr3, expr2, 0))
283 : return true;
284 14239 : if (expr2 != expr4 && operand_equal_p (expr3, expr4, 0))
285 : return true;
286 : }
287 2269790 : if (expr2 != expr4 && operand_equal_p (expr1, expr4, 0))
288 : return true;
289 2269397 : if (gimple_maybe_truncate (expr3, &expr3, valueize)
290 56720 : && gimple_maybe_truncate (expr4, &expr4, valueize)
291 2272464 : && operand_equal_p (expr3, expr4, 0))
292 : return true;
293 : return false;
294 : }
295 :
296 : /* Return true if EXPR1 and EXPR2 have the bitwise opposite value,
297 : but not necessarily same type.
298 : The types can differ through nop conversions. */
299 : #define bitwise_inverted_equal_p(expr1, expr2, wascmp) \
300 : gimple_bitwise_inverted_equal_p (expr1, expr2, wascmp, valueize)
301 :
302 :
303 : bool gimple_bit_not_with_nop (tree, tree *, tree (*) (tree));
304 : bool gimple_maybe_cmp (tree, tree *, tree (*) (tree));
305 : bool gimple_bit_xor_cst (tree, tree *, tree (*) (tree));
306 :
307 : /* Helper function for bitwise_inverted_equal_p macro. */
308 :
309 : static inline bool
310 254543335 : gimple_bitwise_inverted_equal_p (tree expr1, tree expr2, bool &wascmp, tree (*valueize) (tree))
311 : {
312 254543335 : wascmp = false;
313 254543335 : if (expr1 == expr2)
314 : return false;
315 254499016 : if (!tree_nop_conversion_p (TREE_TYPE (expr1), TREE_TYPE (expr2)))
316 : return false;
317 254499016 : tree cst1 = uniform_integer_cst_p (expr1);
318 254499016 : tree cst2 = uniform_integer_cst_p (expr2);
319 254499016 : if (cst1 && cst2)
320 325680 : return wi::to_wide (cst1) == ~wi::to_wide (cst2);
321 254173340 : if (operand_equal_p (expr1, expr2, 0))
322 : return false;
323 :
324 254173340 : tree xor1[2];
325 254173340 : tree xor2[2];
326 : /* `X ^ CST` and `X ^ ~CST` match for ~. */
327 254173340 : if (gimple_bit_xor_cst (expr1, xor1, valueize)
328 254173340 : && gimple_bit_xor_cst (expr2, xor2, valueize))
329 : {
330 530 : if (operand_equal_p (xor1[0], xor2[0], 0)
331 730 : && (wi::to_wide (uniform_integer_cst_p (xor1[1]))
332 730 : == ~wi::to_wide (uniform_integer_cst_p (xor2[1]))))
333 : return true;
334 : }
335 :
336 254173307 : tree other;
337 : /* Try if EXPR1 was defined as ~EXPR2. */
338 254173307 : if (gimple_bit_not_with_nop (expr1, &other, valueize))
339 : {
340 760529 : if (gimple_bitwise_equal_p (other, expr2, valueize))
341 : return true;
342 : }
343 : /* Try if EXPR2 was defined as ~EXPR1. */
344 254172355 : if (gimple_bit_not_with_nop (expr2, &other, valueize))
345 : {
346 623346 : if (gimple_bitwise_equal_p (other, expr1, valueize))
347 : return true;
348 : }
349 :
350 : /* If neither are defined by BIT_NOT, try to see if
351 : both are defined by comparisons and see if they are
352 : complementary (inversion) of each other. */
353 254170783 : tree newexpr1, newexpr2;
354 254170783 : if (!gimple_maybe_cmp (expr1, &newexpr1, valueize))
355 : return false;
356 6397964 : if (!gimple_maybe_cmp (expr2, &newexpr2, valueize))
357 : return false;
358 :
359 5759566 : gimple *d1 = get_def (valueize, newexpr1);
360 5759566 : gassign *a1 = dyn_cast <gassign *> (d1);
361 5759566 : gimple *d2 = get_def (valueize, newexpr2);
362 5759566 : gassign *a2 = dyn_cast <gassign *> (d2);
363 5759566 : tree op10 = do_valueize (valueize, gimple_assign_rhs1 (a1));
364 5759566 : tree op20 = do_valueize (valueize, gimple_assign_rhs1 (a2));
365 5759566 : if (!operand_equal_p (op10, op20))
366 : return false;
367 3232186 : tree op11 = do_valueize (valueize, gimple_assign_rhs2 (a1));
368 3232186 : tree op21 = do_valueize (valueize, gimple_assign_rhs2 (a2));
369 1616093 : if (!operand_equal_p (op11, op21))
370 : return false;
371 43708 : wascmp = true;
372 43708 : tree_code ac1 = gimple_assign_rhs_code (a1);
373 43708 : tree_code ac2 = gimple_assign_rhs_code (a2);
374 : /* Match `^` against `==` but this should only
375 : happen when the type is a 1bit precision integer. */
376 43708 : if (ac1 == BIT_XOR_EXPR)
377 : {
378 41 : tree type = TREE_TYPE (newexpr1);
379 41 : gcc_assert (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1);
380 41 : return ac2 == EQ_EXPR;
381 : }
382 43667 : if (ac2 == BIT_XOR_EXPR)
383 : {
384 16 : tree type = TREE_TYPE (newexpr1);
385 16 : gcc_assert (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1);
386 16 : return ac1 == EQ_EXPR;
387 : }
388 43651 : if (invert_tree_comparison (ac1, HONOR_NANS (op10)) == ac2)
389 4103 : return true;
390 : return false;
391 : }
392 :
393 : /*
394 : * Return the relevant gcond * of the given phi, as well as the true
395 : * and false TREE args of the phi. Or return nullptr.
396 : *
397 : * If matched the gcond *, the output argument TREE true_arg and false_arg
398 : * will be updated to the relevant args of phi.
399 : *
400 : * If failed to match, nullptr gcond * will be returned, as well as the output
401 : * arguments will be set to NULL_TREE.
402 : */
403 :
404 : static inline gcond *
405 28530650 : match_cond_with_binary_phi (gphi *phi, tree *true_arg, tree *false_arg)
406 : {
407 28530650 : *true_arg = *false_arg = NULL_TREE;
408 :
409 28530650 : if (gimple_phi_num_args (phi) != 2)
410 : return nullptr;
411 :
412 27999491 : basic_block pred_b0 = EDGE_PRED (gimple_bb (phi), 0)->src;
413 27999491 : basic_block pred_b1 = EDGE_PRED (gimple_bb (phi), 1)->src;
414 27999491 : edge edge_for_pred_0 = nullptr;
415 :
416 27999491 : if (EDGE_COUNT (pred_b0->succs) == 2
417 17326605 : && EDGE_COUNT (pred_b1->succs) == 1
418 11189920 : && EDGE_COUNT (pred_b1->preds) == 1
419 36905508 : && pred_b0 == EDGE_PRED (pred_b1, 0)->src)
420 : /*
421 : * +------+
422 : * | b0: |
423 : * | def | +-----+
424 : * | ... | | b1: |
425 : * | cond |------>| def |
426 : * +------+ | ... |
427 : * | +-----+
428 : * # |
429 : * | |
430 : * v |
431 : * +-----+ |
432 : * | b2: | |
433 : * | def |<----------+
434 : * +-----+
435 : * #: edge_for_pred_0.
436 : */
437 : edge_for_pred_0 = EDGE_PRED (gimple_bb (phi), 0);
438 25127168 : else if (EDGE_COUNT (pred_b1->succs) == 2
439 9770012 : && EDGE_COUNT (pred_b0->succs) == 1
440 3632709 : && EDGE_COUNT (pred_b0->preds) == 1
441 27950107 : && pred_b1 == EDGE_PRED (pred_b0, 0)->src)
442 : /*
443 : * +------+
444 : * | b1: |
445 : * +-----+ | def |
446 : * | b0: | | ... |
447 : * | def |<---#---| cond |
448 : * | ... | +------+
449 : * +-----+ |
450 : * | |
451 : * | |
452 : * | |
453 : * v |
454 : * +-----+ |
455 : * | b2: | |
456 : * | def |<----------+
457 : * +-----+
458 : * #: edge_for_pred_0.
459 : */
460 : edge_for_pred_0 = EDGE_PRED (pred_b0, 0);
461 24175994 : else if (EDGE_COUNT (pred_b0->succs) == 1
462 9658363 : && EDGE_COUNT (pred_b1->succs) == 1
463 6972099 : && EDGE_COUNT (pred_b0->preds) == 1
464 5646306 : && EDGE_COUNT (pred_b1->preds) == 1
465 4251797 : && EDGE_COUNT (EDGE_PRED (pred_b0, 0)->src->succs) == 2
466 28213508 : && EDGE_PRED (pred_b0, 0)->src == EDGE_PRED (pred_b1, 0)->src)
467 : /* +------+
468 : * | b0: |
469 : * | ... | +-----+
470 : * | cond |------>| b2: |
471 : * +------+ | ... |
472 : * | +-----+
473 : * # |
474 : * | |
475 : * v |
476 : * +-----+ |
477 : * | b1: | |
478 : * | ... | |
479 : * +-----+ |
480 : * | |
481 : * | |
482 : * v |
483 : * +-----+ |
484 : * | b3: |<----------+
485 : * | ... |
486 : * +-----+
487 : * #: edge_for_pred_0.
488 : */
489 : edge_for_pred_0 = EDGE_PRED (pred_b0, 0);
490 :
491 5700271 : if (!edge_for_pred_0)
492 : return nullptr;
493 :
494 11400542 : gcond *cond = safe_dyn_cast <gcond *> (*gsi_last_bb (edge_for_pred_0->src));
495 :
496 5690101 : if (!cond)
497 : return nullptr;
498 :
499 5690101 : if (edge_for_pred_0->flags & EDGE_TRUE_VALUE)
500 : {
501 3289228 : *true_arg = gimple_phi_arg_def (phi, 0);
502 3289228 : *false_arg = gimple_phi_arg_def (phi, 1);
503 : }
504 : else /* Aka edge_for_pred_0->flags & EDGE_FALSE_VALUE */
505 : {
506 2400873 : *false_arg = gimple_phi_arg_def (phi, 0);
507 2400873 : *true_arg = gimple_phi_arg_def (phi, 1);
508 : }
509 :
510 : return cond;
511 : }
512 :
513 : /* If OP is a SSA_NAME with SSA_NAME_DEF_STMT in the IL, return that
514 : stmt, otherwise NULL. For use in range_of_expr calls. */
515 :
516 : static inline gimple *
517 1286151 : gimple_match_ctx (tree op)
518 : {
519 1286151 : if (TREE_CODE (op) == SSA_NAME
520 1227016 : && SSA_NAME_DEF_STMT (op)
521 2513167 : && gimple_bb (SSA_NAME_DEF_STMT (op)))
522 : return SSA_NAME_DEF_STMT (op);
523 : return NULL;
524 : }
525 :
526 : /* Helper to shorten range queries in match.pd. R is the range to
527 : be queried, OP tree on which it should be queried and CTX is some
528 : capture on which gimple_match_ctx should be called, or NULL for
529 : global range. */
530 :
531 : static inline bool
532 1278758 : gimple_match_range_of_expr (vrange &r, tree op, tree ctx = NULL_TREE)
533 : {
534 3836274 : if (!get_range_query (cfun)->range_of_expr (r, op,
535 1278758 : ctx ? gimple_match_ctx (ctx)
536 : : NULL))
537 : return false;
538 1278758 : return !r.undefined_p ();
539 : }
540 :
541 : /* Return true if CODE applied to OP0 and OP1 in TYPE cannot overflow,
542 : either because TYPE says so or because their ranges prove it. CTX is
543 : a capture passed on to gimple_match_range_of_expr. */
544 :
545 : static inline bool
546 272 : gimple_match_no_overflow_p (tree_code code, tree type, tree op0, tree op1,
547 : tree ctx)
548 : {
549 272 : if (TYPE_OVERFLOW_UNDEFINED (type))
550 : return true;
551 :
552 104 : int_range_max vr0, vr1;
553 104 : return (gimple_match_range_of_expr (vr0, op0, ctx)
554 104 : && gimple_match_range_of_expr (vr1, op1, ctx)
555 208 : && range_op_handler (code).overflow_free_p (vr0, vr1));
556 104 : }
|