1 /* Preamble and helpers for the autogenerated gimple-match.c file.
2 Copyright (C) 2014-2019 Free Software Foundation, Inc.
4 This file is part of GCC.
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
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
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/>. */
22 #include "coretypes.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-fold.h"
38 #include "gimple-match.h"
39 #include "tree-pass.h"
40 #include "internal-fn.h"
41 #include "case-cfn-macros.h"
43 #include "optabs-tree.h"
47 /* Forward declarations of the private auto-generated matchers.
48 They expect valueized operands in canonical order and do not
49 perform simplification of all-constant operands. */
50 static bool gimple_simplify (gimple_match_op
*, gimple_seq
*, tree (*)(tree
),
51 code_helper
, tree
, tree
);
52 static bool gimple_simplify (gimple_match_op
*, gimple_seq
*, tree (*)(tree
),
53 code_helper
, tree
, tree
, tree
);
54 static bool gimple_simplify (gimple_match_op
*, gimple_seq
*, tree (*)(tree
),
55 code_helper
, tree
, tree
, tree
, tree
);
56 static bool gimple_simplify (gimple_match_op
*, gimple_seq
*, tree (*)(tree
),
57 code_helper
, tree
, tree
, tree
, tree
, tree
);
58 static bool gimple_simplify (gimple_match_op
*, gimple_seq
*, tree (*)(tree
),
59 code_helper
, tree
, tree
, tree
, tree
, tree
, tree
);
60 static bool gimple_resimplify1 (gimple_seq
*, gimple_match_op
*,
62 static bool gimple_resimplify2 (gimple_seq
*, gimple_match_op
*,
64 static bool gimple_resimplify3 (gimple_seq
*, gimple_match_op
*,
66 static bool gimple_resimplify4 (gimple_seq
*, gimple_match_op
*,
68 static bool gimple_resimplify5 (gimple_seq
*, gimple_match_op
*,
71 const unsigned int gimple_match_op::MAX_NUM_OPS
;
73 /* Return whether T is a constant that we'll dispatch to fold to
74 evaluate fully constant expressions. */
77 constant_for_folding (tree t
)
79 return (CONSTANT_CLASS_P (t
)
80 /* The following is only interesting to string builtins. */
81 || (TREE_CODE (t
) == ADDR_EXPR
82 && TREE_CODE (TREE_OPERAND (t
, 0)) == STRING_CST
));
85 /* Try to convert conditional operation ORIG_OP into an IFN_COND_*
86 operation. Return true on success, storing the new operation in NEW_OP. */
89 convert_conditional_op (gimple_match_op
*orig_op
,
90 gimple_match_op
*new_op
)
93 if (orig_op
->code
.is_tree_code ())
94 ifn
= get_conditional_internal_fn ((tree_code
) orig_op
->code
);
97 combined_fn cfn
= orig_op
->code
;
98 if (!internal_fn_p (cfn
))
100 ifn
= get_conditional_internal_fn (as_internal_fn (cfn
));
104 unsigned int num_ops
= orig_op
->num_ops
;
105 new_op
->set_op (as_combined_fn (ifn
), orig_op
->type
, num_ops
+ 2);
106 new_op
->ops
[0] = orig_op
->cond
.cond
;
107 for (unsigned int i
= 0; i
< num_ops
; ++i
)
108 new_op
->ops
[i
+ 1] = orig_op
->ops
[i
];
109 tree else_value
= orig_op
->cond
.else_value
;
111 else_value
= targetm
.preferred_else_value (ifn
, orig_op
->type
,
112 num_ops
, orig_op
->ops
);
113 new_op
->ops
[num_ops
+ 1] = else_value
;
117 /* RES_OP is the result of a simplification. If it is conditional,
118 try to replace it with the equivalent UNCOND form, such as an
119 IFN_COND_* call or a VEC_COND_EXPR. Also try to resimplify the
120 result of the replacement if appropriate, adding any new statements to
121 SEQ and using VALUEIZE as the valueization function. Return true if
122 this resimplification occurred and resulted in at least one change. */
125 maybe_resimplify_conditional_op (gimple_seq
*seq
, gimple_match_op
*res_op
,
126 tree (*valueize
) (tree
))
128 if (!res_op
->cond
.cond
)
131 if (!res_op
->cond
.else_value
132 && res_op
->code
.is_tree_code ())
134 /* The "else" value doesn't matter. If the "then" value is a
135 gimple value, just use it unconditionally. This isn't a
136 simplification in itself, since there was no operation to
137 build in the first place. */
138 if (gimple_simplified_result_is_gimple_val (res_op
))
140 res_op
->cond
.cond
= NULL_TREE
;
144 /* Likewise if the operation would not trap. */
145 bool honor_trapv
= (INTEGRAL_TYPE_P (res_op
->type
)
146 && TYPE_OVERFLOW_TRAPS (res_op
->type
));
147 if (!operation_could_trap_p ((tree_code
) res_op
->code
,
148 FLOAT_TYPE_P (res_op
->type
),
149 honor_trapv
, res_op
->op_or_null (1)))
151 res_op
->cond
.cond
= NULL_TREE
;
156 /* If the "then" value is a gimple value and the "else" value matters,
157 create a VEC_COND_EXPR between them, then see if it can be further
159 gimple_match_op new_op
;
160 if (res_op
->cond
.else_value
161 && VECTOR_TYPE_P (res_op
->type
)
162 && gimple_simplified_result_is_gimple_val (res_op
))
164 new_op
.set_op (VEC_COND_EXPR
, res_op
->type
,
165 res_op
->cond
.cond
, res_op
->ops
[0],
166 res_op
->cond
.else_value
);
168 return gimple_resimplify3 (seq
, res_op
, valueize
);
171 /* Otherwise try rewriting the operation as an IFN_COND_* call.
172 Again, this isn't a simplification in itself, since it's what
173 RES_OP already described. */
174 if (convert_conditional_op (res_op
, &new_op
))
180 /* Helper that matches and simplifies the toplevel result from
181 a gimple_simplify run (where we don't want to build
182 a stmt in case it's used in in-place folding). Replaces
183 RES_OP with a simplified and/or canonicalized result and
184 returns whether any change was made. */
187 gimple_resimplify1 (gimple_seq
*seq
, gimple_match_op
*res_op
,
188 tree (*valueize
)(tree
))
190 if (constant_for_folding (res_op
->ops
[0]))
192 tree tem
= NULL_TREE
;
193 if (res_op
->code
.is_tree_code ())
195 tree_code code
= res_op
->code
;
196 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
))
197 && TREE_CODE_LENGTH (code
) == 1)
198 tem
= const_unop (res_op
->code
, res_op
->type
, res_op
->ops
[0]);
201 tem
= fold_const_call (combined_fn (res_op
->code
), res_op
->type
,
204 && CONSTANT_CLASS_P (tem
))
206 if (TREE_OVERFLOW_P (tem
))
207 tem
= drop_tree_overflow (tem
);
208 res_op
->set_value (tem
);
209 maybe_resimplify_conditional_op (seq
, res_op
, valueize
);
214 /* Limit recursion, there are cases like PR80887 and others, for
215 example when value-numbering presents us with unfolded expressions
216 that we are really not prepared to handle without eventual
217 oscillation like ((_50 + 0) + 8) where _50 gets mapped to _50
218 itself as available expression. */
219 static unsigned depth
;
222 if (dump_file
&& (dump_flags
& TDF_FOLDING
))
223 fprintf (dump_file
, "Aborting expression simplification due to "
229 gimple_match_op
res_op2 (*res_op
);
230 if (gimple_simplify (&res_op2
, seq
, valueize
,
231 res_op
->code
, res_op
->type
, res_op
->ops
[0]))
239 if (maybe_resimplify_conditional_op (seq
, res_op
, valueize
))
245 /* Helper that matches and simplifies the toplevel result from
246 a gimple_simplify run (where we don't want to build
247 a stmt in case it's used in in-place folding). Replaces
248 RES_OP with a simplified and/or canonicalized result and
249 returns whether any change was made. */
252 gimple_resimplify2 (gimple_seq
*seq
, gimple_match_op
*res_op
,
253 tree (*valueize
)(tree
))
255 if (constant_for_folding (res_op
->ops
[0])
256 && constant_for_folding (res_op
->ops
[1]))
258 tree tem
= NULL_TREE
;
259 if (res_op
->code
.is_tree_code ())
261 tree_code code
= res_op
->code
;
262 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
))
263 && TREE_CODE_LENGTH (code
) == 2)
264 tem
= const_binop (res_op
->code
, res_op
->type
,
265 res_op
->ops
[0], res_op
->ops
[1]);
268 tem
= fold_const_call (combined_fn (res_op
->code
), res_op
->type
,
269 res_op
->ops
[0], res_op
->ops
[1]);
271 && CONSTANT_CLASS_P (tem
))
273 if (TREE_OVERFLOW_P (tem
))
274 tem
= drop_tree_overflow (tem
);
275 res_op
->set_value (tem
);
276 maybe_resimplify_conditional_op (seq
, res_op
, valueize
);
281 /* Canonicalize operand order. */
282 bool canonicalized
= false;
283 if (res_op
->code
.is_tree_code ()
284 && (TREE_CODE_CLASS ((enum tree_code
) res_op
->code
) == tcc_comparison
285 || commutative_tree_code (res_op
->code
))
286 && tree_swap_operands_p (res_op
->ops
[0], res_op
->ops
[1]))
288 std::swap (res_op
->ops
[0], res_op
->ops
[1]);
289 if (TREE_CODE_CLASS ((enum tree_code
) res_op
->code
) == tcc_comparison
)
290 res_op
->code
= swap_tree_comparison (res_op
->code
);
291 canonicalized
= true;
294 /* Limit recursion, see gimple_resimplify1. */
295 static unsigned depth
;
298 if (dump_file
&& (dump_flags
& TDF_FOLDING
))
299 fprintf (dump_file
, "Aborting expression simplification due to "
305 gimple_match_op
res_op2 (*res_op
);
306 if (gimple_simplify (&res_op2
, seq
, valueize
,
307 res_op
->code
, res_op
->type
,
308 res_op
->ops
[0], res_op
->ops
[1]))
316 if (maybe_resimplify_conditional_op (seq
, res_op
, valueize
))
319 return canonicalized
;
322 /* Helper that matches and simplifies the toplevel result from
323 a gimple_simplify run (where we don't want to build
324 a stmt in case it's used in in-place folding). Replaces
325 RES_OP with a simplified and/or canonicalized result and
326 returns whether any change was made. */
329 gimple_resimplify3 (gimple_seq
*seq
, gimple_match_op
*res_op
,
330 tree (*valueize
)(tree
))
332 if (constant_for_folding (res_op
->ops
[0])
333 && constant_for_folding (res_op
->ops
[1])
334 && constant_for_folding (res_op
->ops
[2]))
336 tree tem
= NULL_TREE
;
337 if (res_op
->code
.is_tree_code ())
339 tree_code code
= res_op
->code
;
340 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
))
341 && TREE_CODE_LENGTH (code
) == 3)
342 tem
= fold_ternary
/*_to_constant*/ (res_op
->code
, res_op
->type
,
343 res_op
->ops
[0], res_op
->ops
[1],
347 tem
= fold_const_call (combined_fn (res_op
->code
), res_op
->type
,
348 res_op
->ops
[0], res_op
->ops
[1], res_op
->ops
[2]);
350 && CONSTANT_CLASS_P (tem
))
352 if (TREE_OVERFLOW_P (tem
))
353 tem
= drop_tree_overflow (tem
);
354 res_op
->set_value (tem
);
355 maybe_resimplify_conditional_op (seq
, res_op
, valueize
);
360 /* Canonicalize operand order. */
361 bool canonicalized
= false;
362 if (res_op
->code
.is_tree_code ()
363 && commutative_ternary_tree_code (res_op
->code
)
364 && tree_swap_operands_p (res_op
->ops
[0], res_op
->ops
[1]))
366 std::swap (res_op
->ops
[0], res_op
->ops
[1]);
367 canonicalized
= true;
370 /* Limit recursion, see gimple_resimplify1. */
371 static unsigned depth
;
374 if (dump_file
&& (dump_flags
& TDF_FOLDING
))
375 fprintf (dump_file
, "Aborting expression simplification due to "
381 gimple_match_op
res_op2 (*res_op
);
382 if (gimple_simplify (&res_op2
, seq
, valueize
,
383 res_op
->code
, res_op
->type
,
384 res_op
->ops
[0], res_op
->ops
[1], res_op
->ops
[2]))
392 if (maybe_resimplify_conditional_op (seq
, res_op
, valueize
))
395 return canonicalized
;
398 /* Helper that matches and simplifies the toplevel result from
399 a gimple_simplify run (where we don't want to build
400 a stmt in case it's used in in-place folding). Replaces
401 RES_OP with a simplified and/or canonicalized result and
402 returns whether any change was made. */
405 gimple_resimplify4 (gimple_seq
*seq
, gimple_match_op
*res_op
,
406 tree (*valueize
)(tree
))
408 /* No constant folding is defined for four-operand functions. */
410 /* Limit recursion, see gimple_resimplify1. */
411 static unsigned depth
;
414 if (dump_file
&& (dump_flags
& TDF_FOLDING
))
415 fprintf (dump_file
, "Aborting expression simplification due to "
421 gimple_match_op
res_op2 (*res_op
);
422 if (gimple_simplify (&res_op2
, seq
, valueize
,
423 res_op
->code
, res_op
->type
,
424 res_op
->ops
[0], res_op
->ops
[1], res_op
->ops
[2],
433 if (maybe_resimplify_conditional_op (seq
, res_op
, valueize
))
439 /* Helper that matches and simplifies the toplevel result from
440 a gimple_simplify run (where we don't want to build
441 a stmt in case it's used in in-place folding). Replaces
442 RES_OP with a simplified and/or canonicalized result and
443 returns whether any change was made. */
446 gimple_resimplify5 (gimple_seq
*seq
, gimple_match_op
*res_op
,
447 tree (*valueize
)(tree
))
449 /* No constant folding is defined for five-operand functions. */
451 gimple_match_op
res_op2 (*res_op
);
452 if (gimple_simplify (&res_op2
, seq
, valueize
,
453 res_op
->code
, res_op
->type
,
454 res_op
->ops
[0], res_op
->ops
[1], res_op
->ops
[2],
455 res_op
->ops
[3], res_op
->ops
[4]))
461 if (maybe_resimplify_conditional_op (seq
, res_op
, valueize
))
467 /* Match and simplify the toplevel valueized operation THIS.
468 Replaces THIS with a simplified and/or canonicalized result and
469 returns whether any change was made. */
472 gimple_match_op::resimplify (gimple_seq
*seq
, tree (*valueize
)(tree
))
477 return gimple_resimplify1 (seq
, this, valueize
);
479 return gimple_resimplify2 (seq
, this, valueize
);
481 return gimple_resimplify3 (seq
, this, valueize
);
483 return gimple_resimplify4 (seq
, this, valueize
);
485 return gimple_resimplify5 (seq
, this, valueize
);
491 /* If in GIMPLE the operation described by RES_OP should be single-rhs,
492 build a GENERIC tree for that expression and update RES_OP accordingly. */
495 maybe_build_generic_op (gimple_match_op
*res_op
)
497 tree_code code
= (tree_code
) res_op
->code
;
503 case VIEW_CONVERT_EXPR
:
504 val
= build1 (code
, res_op
->type
, res_op
->ops
[0]);
505 res_op
->set_value (val
);
508 val
= build3 (code
, res_op
->type
, res_op
->ops
[0], res_op
->ops
[1],
510 REF_REVERSE_STORAGE_ORDER (val
) = res_op
->reverse
;
511 res_op
->set_value (val
);
517 tree (*mprts_hook
) (gimple_match_op
*);
519 /* Try to build RES_OP, which is known to be a call to FN. Return null
520 if the target doesn't support the function. */
523 build_call_internal (internal_fn fn
, gimple_match_op
*res_op
)
525 if (direct_internal_fn_p (fn
))
527 tree_pair types
= direct_internal_fn_types (fn
, res_op
->type
,
529 if (!direct_internal_fn_supported_p (fn
, types
, OPTIMIZE_FOR_BOTH
))
532 return gimple_build_call_internal (fn
, res_op
->num_ops
,
533 res_op
->op_or_null (0),
534 res_op
->op_or_null (1),
535 res_op
->op_or_null (2),
536 res_op
->op_or_null (3),
537 res_op
->op_or_null (4));
540 /* Push the exploded expression described by RES_OP as a statement to
541 SEQ if necessary and return a gimple value denoting the value of the
542 expression. If RES is not NULL then the result will be always RES
543 and even gimple values are pushed to SEQ. */
546 maybe_push_res_to_seq (gimple_match_op
*res_op
, gimple_seq
*seq
, tree res
)
548 tree
*ops
= res_op
->ops
;
549 unsigned num_ops
= res_op
->num_ops
;
551 /* The caller should have converted conditional operations into an UNCOND
552 form and resimplified as appropriate. The conditional form only
553 survives this far if that conversion failed. */
554 if (res_op
->cond
.cond
)
557 if (res_op
->code
.is_tree_code ())
560 && gimple_simplified_result_is_gimple_val (res_op
))
564 tree tem
= mprts_hook (res_op
);
573 /* Play safe and do not allow abnormals to be mentioned in
574 newly created statements. */
575 for (unsigned int i
= 0; i
< num_ops
; ++i
)
576 if (TREE_CODE (ops
[i
]) == SSA_NAME
577 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[i
]))
580 if (num_ops
> 0 && COMPARISON_CLASS_P (ops
[0]))
581 for (unsigned int i
= 0; i
< 2; ++i
)
582 if (TREE_CODE (TREE_OPERAND (ops
[0], i
)) == SSA_NAME
583 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops
[0], i
)))
586 if (res_op
->code
.is_tree_code ())
590 if (gimple_in_ssa_p (cfun
))
591 res
= make_ssa_name (res_op
->type
);
593 res
= create_tmp_reg (res_op
->type
);
595 maybe_build_generic_op (res_op
);
596 gimple
*new_stmt
= gimple_build_assign (res
, res_op
->code
,
597 res_op
->op_or_null (0),
598 res_op
->op_or_null (1),
599 res_op
->op_or_null (2));
600 gimple_seq_add_stmt_without_update (seq
, new_stmt
);
605 gcc_assert (num_ops
!= 0);
606 combined_fn fn
= res_op
->code
;
607 gcall
*new_stmt
= NULL
;
608 if (internal_fn_p (fn
))
610 /* Generate the given function if we can. */
611 internal_fn ifn
= as_internal_fn (fn
);
612 new_stmt
= build_call_internal (ifn
, res_op
);
618 /* Find the function we want to call. */
619 tree decl
= builtin_decl_implicit (as_builtin_fn (fn
));
623 /* We can't and should not emit calls to non-const functions. */
624 if (!(flags_from_decl_or_type (decl
) & ECF_CONST
))
627 new_stmt
= gimple_build_call (decl
, num_ops
,
628 res_op
->op_or_null (0),
629 res_op
->op_or_null (1),
630 res_op
->op_or_null (2),
631 res_op
->op_or_null (3),
632 res_op
->op_or_null (4));
636 if (gimple_in_ssa_p (cfun
))
637 res
= make_ssa_name (res_op
->type
);
639 res
= create_tmp_reg (res_op
->type
);
641 gimple_call_set_lhs (new_stmt
, res
);
642 gimple_seq_add_stmt_without_update (seq
, new_stmt
);
648 /* Public API overloads follow for operation being tree_code or
649 built_in_function and for one to three operands or arguments.
650 They return NULL_TREE if nothing could be simplified or
651 the resulting simplified value with parts pushed to SEQ.
652 If SEQ is NULL then if the simplification needs to create
653 new stmts it will fail. If VALUEIZE is non-NULL then all
654 SSA names will be valueized using that hook prior to
655 applying simplifications. */
660 gimple_simplify (enum tree_code code
, tree type
,
662 gimple_seq
*seq
, tree (*valueize
)(tree
))
664 if (constant_for_folding (op0
))
666 tree res
= const_unop (code
, type
, op0
);
668 && CONSTANT_CLASS_P (res
))
672 gimple_match_op res_op
;
673 if (!gimple_simplify (&res_op
, seq
, valueize
, code
, type
, op0
))
675 return maybe_push_res_to_seq (&res_op
, seq
);
681 gimple_simplify (enum tree_code code
, tree type
,
683 gimple_seq
*seq
, tree (*valueize
)(tree
))
685 if (constant_for_folding (op0
) && constant_for_folding (op1
))
687 tree res
= const_binop (code
, type
, op0
, op1
);
689 && CONSTANT_CLASS_P (res
))
693 /* Canonicalize operand order both for matching and fallback stmt
695 if ((commutative_tree_code (code
)
696 || TREE_CODE_CLASS (code
) == tcc_comparison
)
697 && tree_swap_operands_p (op0
, op1
))
699 std::swap (op0
, op1
);
700 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
701 code
= swap_tree_comparison (code
);
704 gimple_match_op res_op
;
705 if (!gimple_simplify (&res_op
, seq
, valueize
, code
, type
, op0
, op1
))
707 return maybe_push_res_to_seq (&res_op
, seq
);
713 gimple_simplify (enum tree_code code
, tree type
,
714 tree op0
, tree op1
, tree op2
,
715 gimple_seq
*seq
, tree (*valueize
)(tree
))
717 if (constant_for_folding (op0
) && constant_for_folding (op1
)
718 && constant_for_folding (op2
))
720 tree res
= fold_ternary
/*_to_constant */ (code
, type
, op0
, op1
, op2
);
722 && CONSTANT_CLASS_P (res
))
726 /* Canonicalize operand order both for matching and fallback stmt
728 if (commutative_ternary_tree_code (code
)
729 && tree_swap_operands_p (op0
, op1
))
730 std::swap (op0
, op1
);
732 gimple_match_op res_op
;
733 if (!gimple_simplify (&res_op
, seq
, valueize
, code
, type
, op0
, op1
, op2
))
735 return maybe_push_res_to_seq (&res_op
, seq
);
738 /* Builtin or internal function with one argument. */
741 gimple_simplify (combined_fn fn
, tree type
,
743 gimple_seq
*seq
, tree (*valueize
)(tree
))
745 if (constant_for_folding (arg0
))
747 tree res
= fold_const_call (fn
, type
, arg0
);
748 if (res
&& CONSTANT_CLASS_P (res
))
752 gimple_match_op res_op
;
753 if (!gimple_simplify (&res_op
, seq
, valueize
, fn
, type
, arg0
))
755 return maybe_push_res_to_seq (&res_op
, seq
);
758 /* Builtin or internal function with two arguments. */
761 gimple_simplify (combined_fn fn
, tree type
,
762 tree arg0
, tree arg1
,
763 gimple_seq
*seq
, tree (*valueize
)(tree
))
765 if (constant_for_folding (arg0
)
766 && constant_for_folding (arg1
))
768 tree res
= fold_const_call (fn
, type
, arg0
, arg1
);
769 if (res
&& CONSTANT_CLASS_P (res
))
773 gimple_match_op res_op
;
774 if (!gimple_simplify (&res_op
, seq
, valueize
, fn
, type
, arg0
, arg1
))
776 return maybe_push_res_to_seq (&res_op
, seq
);
779 /* Builtin or internal function with three arguments. */
782 gimple_simplify (combined_fn fn
, tree type
,
783 tree arg0
, tree arg1
, tree arg2
,
784 gimple_seq
*seq
, tree (*valueize
)(tree
))
786 if (constant_for_folding (arg0
)
787 && constant_for_folding (arg1
)
788 && constant_for_folding (arg2
))
790 tree res
= fold_const_call (fn
, type
, arg0
, arg1
, arg2
);
791 if (res
&& CONSTANT_CLASS_P (res
))
795 gimple_match_op res_op
;
796 if (!gimple_simplify (&res_op
, seq
, valueize
, fn
, type
, arg0
, arg1
, arg2
))
798 return maybe_push_res_to_seq (&res_op
, seq
);
801 /* Helper for gimple_simplify valueizing OP using VALUEIZE and setting
802 VALUEIZED to true if valueization changed OP. */
805 do_valueize (tree op
, tree (*valueize
)(tree
), bool &valueized
)
807 if (valueize
&& TREE_CODE (op
) == SSA_NAME
)
809 tree tem
= valueize (op
);
810 if (tem
&& tem
!= op
)
819 /* If RES_OP is a call to a conditional internal function, try simplifying
820 the associated unconditional operation and using the result to build
821 a new conditional operation. For example, if RES_OP is:
823 IFN_COND_ADD (COND, A, B, ELSE)
825 try simplifying (plus A B) and using the result to build a replacement
826 for the whole IFN_COND_ADD.
828 Return true if this approach led to a simplification, otherwise leave
829 RES_OP unchanged (and so suitable for other simplifications). When
830 returning true, add any new statements to SEQ and use VALUEIZE as the
831 valueization function.
833 RES_OP is known to be a call to IFN. */
836 try_conditional_simplification (internal_fn ifn
, gimple_match_op
*res_op
,
837 gimple_seq
*seq
, tree (*valueize
) (tree
))
840 tree_code code
= conditional_internal_fn_code (ifn
);
841 if (code
!= ERROR_MARK
)
845 ifn
= get_unconditional_internal_fn (ifn
);
848 op
= as_combined_fn (ifn
);
851 unsigned int num_ops
= res_op
->num_ops
;
852 gimple_match_op
cond_op (gimple_match_cond (res_op
->ops
[0],
853 res_op
->ops
[num_ops
- 1]),
854 op
, res_op
->type
, num_ops
- 2);
856 memcpy (cond_op
.ops
, res_op
->ops
+ 1, (num_ops
- 1) * sizeof *cond_op
.ops
);
860 if (!gimple_resimplify2 (seq
, &cond_op
, valueize
))
864 if (!gimple_resimplify3 (seq
, &cond_op
, valueize
))
871 maybe_resimplify_conditional_op (seq
, res_op
, valueize
);
875 /* The main STMT based simplification entry. It is used by the fold_stmt
876 and the fold_stmt_to_constant APIs. */
879 gimple_simplify (gimple
*stmt
, gimple_match_op
*res_op
, gimple_seq
*seq
,
880 tree (*valueize
)(tree
), tree (*top_valueize
)(tree
))
882 switch (gimple_code (stmt
))
886 enum tree_code code
= gimple_assign_rhs_code (stmt
);
887 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
888 switch (gimple_assign_rhs_class (stmt
))
890 case GIMPLE_SINGLE_RHS
:
891 if (code
== REALPART_EXPR
892 || code
== IMAGPART_EXPR
893 || code
== VIEW_CONVERT_EXPR
)
895 tree op0
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
896 bool valueized
= false;
897 op0
= do_valueize (op0
, top_valueize
, valueized
);
898 res_op
->set_op (code
, type
, op0
);
899 return (gimple_resimplify1 (seq
, res_op
, valueize
)
902 else if (code
== BIT_FIELD_REF
)
904 tree rhs1
= gimple_assign_rhs1 (stmt
);
905 tree op0
= TREE_OPERAND (rhs1
, 0);
906 bool valueized
= false;
907 op0
= do_valueize (op0
, top_valueize
, valueized
);
908 res_op
->set_op (code
, type
, op0
,
909 TREE_OPERAND (rhs1
, 1),
910 TREE_OPERAND (rhs1
, 2),
911 REF_REVERSE_STORAGE_ORDER (rhs1
));
914 return (gimple_resimplify3 (seq
, res_op
, valueize
)
917 else if (code
== SSA_NAME
920 tree op0
= gimple_assign_rhs1 (stmt
);
921 tree valueized
= top_valueize (op0
);
922 if (!valueized
|| op0
== valueized
)
924 res_op
->set_op (TREE_CODE (op0
), type
, valueized
);
928 case GIMPLE_UNARY_RHS
:
930 tree rhs1
= gimple_assign_rhs1 (stmt
);
931 bool valueized
= false;
932 rhs1
= do_valueize (rhs1
, top_valueize
, valueized
);
933 res_op
->set_op (code
, type
, rhs1
);
934 return (gimple_resimplify1 (seq
, res_op
, valueize
)
937 case GIMPLE_BINARY_RHS
:
939 tree rhs1
= gimple_assign_rhs1 (stmt
);
940 tree rhs2
= gimple_assign_rhs2 (stmt
);
941 bool valueized
= false;
942 rhs1
= do_valueize (rhs1
, top_valueize
, valueized
);
943 rhs2
= do_valueize (rhs2
, top_valueize
, valueized
);
944 res_op
->set_op (code
, type
, rhs1
, rhs2
);
945 return (gimple_resimplify2 (seq
, res_op
, valueize
)
948 case GIMPLE_TERNARY_RHS
:
950 bool valueized
= false;
951 tree rhs1
= gimple_assign_rhs1 (stmt
);
952 /* If this is a [VEC_]COND_EXPR first try to simplify an
953 embedded GENERIC condition. */
954 if (code
== COND_EXPR
955 || code
== VEC_COND_EXPR
)
957 if (COMPARISON_CLASS_P (rhs1
))
959 tree lhs
= TREE_OPERAND (rhs1
, 0);
960 tree rhs
= TREE_OPERAND (rhs1
, 1);
961 lhs
= do_valueize (lhs
, top_valueize
, valueized
);
962 rhs
= do_valueize (rhs
, top_valueize
, valueized
);
963 gimple_match_op
res_op2 (res_op
->cond
, TREE_CODE (rhs1
),
964 TREE_TYPE (rhs1
), lhs
, rhs
);
965 if ((gimple_resimplify2 (seq
, &res_op2
, valueize
)
967 && res_op2
.code
.is_tree_code ())
970 if (TREE_CODE_CLASS ((enum tree_code
) res_op2
.code
)
972 rhs1
= build2 (res_op2
.code
, TREE_TYPE (rhs1
),
973 res_op2
.ops
[0], res_op2
.ops
[1]);
974 else if (res_op2
.code
== SSA_NAME
975 || res_op2
.code
== INTEGER_CST
976 || res_op2
.code
== VECTOR_CST
)
977 rhs1
= res_op2
.ops
[0];
983 tree rhs2
= gimple_assign_rhs2 (stmt
);
984 tree rhs3
= gimple_assign_rhs3 (stmt
);
985 rhs1
= do_valueize (rhs1
, top_valueize
, valueized
);
986 rhs2
= do_valueize (rhs2
, top_valueize
, valueized
);
987 rhs3
= do_valueize (rhs3
, top_valueize
, valueized
);
988 res_op
->set_op (code
, type
, rhs1
, rhs2
, rhs3
);
989 return (gimple_resimplify3 (seq
, res_op
, valueize
)
999 /* ??? This way we can't simplify calls with side-effects. */
1000 if (gimple_call_lhs (stmt
) != NULL_TREE
1001 && gimple_call_num_args (stmt
) >= 1
1002 && gimple_call_num_args (stmt
) <= 5)
1004 bool valueized
= false;
1006 if (gimple_call_internal_p (stmt
))
1007 cfn
= as_combined_fn (gimple_call_internal_fn (stmt
));
1010 tree fn
= gimple_call_fn (stmt
);
1014 fn
= do_valueize (fn
, top_valueize
, valueized
);
1015 if (TREE_CODE (fn
) != ADDR_EXPR
1016 || TREE_CODE (TREE_OPERAND (fn
, 0)) != FUNCTION_DECL
)
1019 tree decl
= TREE_OPERAND (fn
, 0);
1020 if (DECL_BUILT_IN_CLASS (decl
) != BUILT_IN_NORMAL
1021 || !gimple_builtin_call_types_compatible_p (stmt
, decl
))
1024 cfn
= as_combined_fn (DECL_FUNCTION_CODE (decl
));
1027 unsigned int num_args
= gimple_call_num_args (stmt
);
1028 res_op
->set_op (cfn
, TREE_TYPE (gimple_call_lhs (stmt
)), num_args
);
1029 for (unsigned i
= 0; i
< num_args
; ++i
)
1031 tree arg
= gimple_call_arg (stmt
, i
);
1032 res_op
->ops
[i
] = do_valueize (arg
, top_valueize
, valueized
);
1034 if (internal_fn_p (cfn
)
1035 && try_conditional_simplification (as_internal_fn (cfn
),
1036 res_op
, seq
, valueize
))
1041 return (gimple_resimplify1 (seq
, res_op
, valueize
)
1044 return (gimple_resimplify2 (seq
, res_op
, valueize
)
1047 return (gimple_resimplify3 (seq
, res_op
, valueize
)
1050 return (gimple_resimplify4 (seq
, res_op
, valueize
)
1053 return (gimple_resimplify5 (seq
, res_op
, valueize
)
1063 tree lhs
= gimple_cond_lhs (stmt
);
1064 tree rhs
= gimple_cond_rhs (stmt
);
1065 bool valueized
= false;
1066 lhs
= do_valueize (lhs
, top_valueize
, valueized
);
1067 rhs
= do_valueize (rhs
, top_valueize
, valueized
);
1068 res_op
->set_op (gimple_cond_code (stmt
), boolean_type_node
, lhs
, rhs
);
1069 return (gimple_resimplify2 (seq
, res_op
, valueize
)
1081 /* Helper for the autogenerated code, valueize OP. */
1084 do_valueize (tree (*valueize
)(tree
), tree op
)
1086 if (valueize
&& TREE_CODE (op
) == SSA_NAME
)
1088 tree tem
= valueize (op
);
1095 /* Helper for the autogenerated code, get at the definition of NAME when
1096 VALUEIZE allows that. */
1099 get_def (tree (*valueize
)(tree
), tree name
)
1101 if (valueize
&& ! valueize (name
))
1103 return SSA_NAME_DEF_STMT (name
);
1106 /* Routine to determine if the types T1 and T2 are effectively
1107 the same for GIMPLE. If T1 or T2 is not a type, the test
1108 applies to their TREE_TYPE. */
1111 types_match (tree t1
, tree t2
)
1114 t1
= TREE_TYPE (t1
);
1116 t2
= TREE_TYPE (t2
);
1118 return types_compatible_p (t1
, t2
);
1121 /* Return if T has a single use. For GIMPLE, we also allow any
1122 non-SSA_NAME (ie constants) and zero uses to cope with uses
1123 that aren't linked up yet. */
1128 return TREE_CODE (t
) != SSA_NAME
|| has_zero_uses (t
) || has_single_use (t
);
1131 /* Return true if math operations should be canonicalized,
1132 e.g. sqrt(sqrt(x)) -> pow(x, 0.25). */
1135 canonicalize_math_p ()
1137 return !cfun
|| (cfun
->curr_properties
& PROP_gimple_opt_math
) == 0;
1140 /* Return true if math operations that are beneficial only after
1141 vectorization should be canonicalized. */
1144 canonicalize_math_after_vectorization_p ()
1146 return !cfun
|| (cfun
->curr_properties
& PROP_gimple_lvec
) != 0;
1149 /* Return true if pow(cst, x) should be optimized into exp(log(cst) * x).
1150 As a workaround for SPEC CPU2017 628.pop2_s, don't do it if arg0
1151 is an exact integer, arg1 = phi_res +/- cst1 and phi_res = PHI <cst2, ...>
1152 where cst2 +/- cst1 is an exact integer, because then pow (arg0, arg1)
1153 will likely be exact, while exp (log (arg0) * arg1) might be not.
1154 Also don't do it if arg1 is phi_res above and cst2 is an exact integer. */
1157 optimize_pow_to_exp (tree arg0
, tree arg1
)
1159 gcc_assert (TREE_CODE (arg0
) == REAL_CST
);
1160 if (!real_isinteger (TREE_REAL_CST_PTR (arg0
), TYPE_MODE (TREE_TYPE (arg0
))))
1163 if (TREE_CODE (arg1
) != SSA_NAME
)
1166 gimple
*def
= SSA_NAME_DEF_STMT (arg1
);
1167 gphi
*phi
= dyn_cast
<gphi
*> (def
);
1168 tree cst1
= NULL_TREE
;
1169 enum tree_code code
= ERROR_MARK
;
1172 if (!is_gimple_assign (def
))
1174 code
= gimple_assign_rhs_code (def
);
1183 if (TREE_CODE (gimple_assign_rhs1 (def
)) != SSA_NAME
1184 || TREE_CODE (gimple_assign_rhs2 (def
)) != REAL_CST
)
1187 cst1
= gimple_assign_rhs2 (def
);
1189 phi
= dyn_cast
<gphi
*> (SSA_NAME_DEF_STMT (gimple_assign_rhs1 (def
)));
1194 tree cst2
= NULL_TREE
;
1195 int n
= gimple_phi_num_args (phi
);
1196 for (int i
= 0; i
< n
; i
++)
1198 tree arg
= PHI_ARG_DEF (phi
, i
);
1199 if (TREE_CODE (arg
) != REAL_CST
)
1201 else if (cst2
== NULL_TREE
)
1203 else if (!operand_equal_p (cst2
, arg
, 0))
1208 cst2
= const_binop (code
, TREE_TYPE (cst2
), cst2
, cst1
);
1210 && TREE_CODE (cst2
) == REAL_CST
1211 && real_isinteger (TREE_REAL_CST_PTR (cst2
),
1212 TYPE_MODE (TREE_TYPE (cst2
))))
1217 /* Return true if a division INNER_DIV / DIVISOR where INNER_DIV
1218 is another division can be optimized. Don't optimize if INNER_DIV
1219 is used in a TRUNC_MOD_EXPR with DIVISOR as second operand. */
1222 optimize_successive_divisions_p (tree divisor
, tree inner_div
)
1224 if (!gimple_in_ssa_p (cfun
))
1227 imm_use_iterator imm_iter
;
1228 use_operand_p use_p
;
1229 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, inner_div
)
1231 gimple
*use_stmt
= USE_STMT (use_p
);
1232 if (!is_gimple_assign (use_stmt
)
1233 || gimple_assign_rhs_code (use_stmt
) != TRUNC_MOD_EXPR
1234 || !operand_equal_p (gimple_assign_rhs2 (use_stmt
), divisor
, 0))