1 /* Preamble and helpers for the autogenerated gimple-match.c file.
2 Copyright (C) 2014-2017 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 "fold-const.h"
31 #include "fold-const-call.h"
32 #include "stor-layout.h"
33 #include "gimple-fold.h"
37 #include "gimple-match.h"
38 #include "tree-pass.h"
39 #include "internal-fn.h"
40 #include "case-cfn-macros.h"
44 /* Forward declarations of the private auto-generated matchers.
45 They expect valueized operands in canonical order and do not
46 perform simplification of all-constant operands. */
47 static bool gimple_simplify (code_helper
*, tree
*,
48 gimple_seq
*, tree (*)(tree
),
49 code_helper
, tree
, tree
);
50 static bool gimple_simplify (code_helper
*, tree
*,
51 gimple_seq
*, tree (*)(tree
),
52 code_helper
, tree
, tree
, tree
);
53 static bool gimple_simplify (code_helper
*, tree
*,
54 gimple_seq
*, tree (*)(tree
),
55 code_helper
, tree
, tree
, tree
, tree
);
58 /* Return whether T is a constant that we'll dispatch to fold to
59 evaluate fully constant expressions. */
62 constant_for_folding (tree t
)
64 return (CONSTANT_CLASS_P (t
)
65 /* The following is only interesting to string builtins. */
66 || (TREE_CODE (t
) == ADDR_EXPR
67 && TREE_CODE (TREE_OPERAND (t
, 0)) == STRING_CST
));
71 /* Helper that matches and simplifies the toplevel result from
72 a gimple_simplify run (where we don't want to build
73 a stmt in case it's used in in-place folding). Replaces
74 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
75 result and returns whether any change was made. */
78 gimple_resimplify1 (gimple_seq
*seq
,
79 code_helper
*res_code
, tree type
, tree
*res_ops
,
80 tree (*valueize
)(tree
))
82 if (constant_for_folding (res_ops
[0]))
85 if (res_code
->is_tree_code ())
86 tem
= const_unop (*res_code
, type
, res_ops
[0]);
88 tem
= fold_const_call (combined_fn (*res_code
), type
, res_ops
[0]);
90 && CONSTANT_CLASS_P (tem
))
92 if (TREE_OVERFLOW_P (tem
))
93 tem
= drop_tree_overflow (tem
);
95 res_ops
[1] = NULL_TREE
;
96 res_ops
[2] = NULL_TREE
;
97 *res_code
= TREE_CODE (res_ops
[0]);
102 code_helper res_code2
;
103 tree res_ops2
[3] = {};
104 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
105 *res_code
, type
, res_ops
[0]))
107 *res_code
= res_code2
;
108 res_ops
[0] = res_ops2
[0];
109 res_ops
[1] = res_ops2
[1];
110 res_ops
[2] = res_ops2
[2];
117 /* Helper that matches and simplifies the toplevel result from
118 a gimple_simplify run (where we don't want to build
119 a stmt in case it's used in in-place folding). Replaces
120 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
121 result and returns whether any change was made. */
124 gimple_resimplify2 (gimple_seq
*seq
,
125 code_helper
*res_code
, tree type
, tree
*res_ops
,
126 tree (*valueize
)(tree
))
128 if (constant_for_folding (res_ops
[0]) && constant_for_folding (res_ops
[1]))
130 tree tem
= NULL_TREE
;
131 if (res_code
->is_tree_code ())
132 tem
= const_binop (*res_code
, type
, res_ops
[0], res_ops
[1]);
134 tem
= fold_const_call (combined_fn (*res_code
), type
,
135 res_ops
[0], res_ops
[1]);
137 && CONSTANT_CLASS_P (tem
))
139 if (TREE_OVERFLOW_P (tem
))
140 tem
= drop_tree_overflow (tem
);
142 res_ops
[1] = NULL_TREE
;
143 res_ops
[2] = NULL_TREE
;
144 *res_code
= TREE_CODE (res_ops
[0]);
149 /* Canonicalize operand order. */
150 bool canonicalized
= false;
151 if (res_code
->is_tree_code ()
152 && (TREE_CODE_CLASS ((enum tree_code
) *res_code
) == tcc_comparison
153 || commutative_tree_code (*res_code
))
154 && tree_swap_operands_p (res_ops
[0], res_ops
[1]))
156 std::swap (res_ops
[0], res_ops
[1]);
157 if (TREE_CODE_CLASS ((enum tree_code
) *res_code
) == tcc_comparison
)
158 *res_code
= swap_tree_comparison (*res_code
);
159 canonicalized
= true;
162 code_helper res_code2
;
163 tree res_ops2
[3] = {};
164 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
165 *res_code
, type
, res_ops
[0], res_ops
[1]))
167 *res_code
= res_code2
;
168 res_ops
[0] = res_ops2
[0];
169 res_ops
[1] = res_ops2
[1];
170 res_ops
[2] = res_ops2
[2];
174 return canonicalized
;
177 /* Helper that matches and simplifies the toplevel result from
178 a gimple_simplify run (where we don't want to build
179 a stmt in case it's used in in-place folding). Replaces
180 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
181 result and returns whether any change was made. */
184 gimple_resimplify3 (gimple_seq
*seq
,
185 code_helper
*res_code
, tree type
, tree
*res_ops
,
186 tree (*valueize
)(tree
))
188 if (constant_for_folding (res_ops
[0]) && constant_for_folding (res_ops
[1])
189 && constant_for_folding (res_ops
[2]))
191 tree tem
= NULL_TREE
;
192 if (res_code
->is_tree_code ())
193 tem
= fold_ternary
/*_to_constant*/ (*res_code
, type
, res_ops
[0],
194 res_ops
[1], res_ops
[2]);
196 tem
= fold_const_call (combined_fn (*res_code
), type
,
197 res_ops
[0], res_ops
[1], res_ops
[2]);
199 && CONSTANT_CLASS_P (tem
))
201 if (TREE_OVERFLOW_P (tem
))
202 tem
= drop_tree_overflow (tem
);
204 res_ops
[1] = NULL_TREE
;
205 res_ops
[2] = NULL_TREE
;
206 *res_code
= TREE_CODE (res_ops
[0]);
211 /* Canonicalize operand order. */
212 bool canonicalized
= false;
213 if (res_code
->is_tree_code ()
214 && commutative_ternary_tree_code (*res_code
)
215 && tree_swap_operands_p (res_ops
[0], res_ops
[1]))
217 std::swap (res_ops
[0], res_ops
[1]);
218 canonicalized
= true;
221 code_helper res_code2
;
222 tree res_ops2
[3] = {};
223 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
225 res_ops
[0], res_ops
[1], res_ops
[2]))
227 *res_code
= res_code2
;
228 res_ops
[0] = res_ops2
[0];
229 res_ops
[1] = res_ops2
[1];
230 res_ops
[2] = res_ops2
[2];
234 return canonicalized
;
238 /* If in GIMPLE expressions with CODE go as single-rhs build
239 a GENERIC tree for that expression into *OP0. */
242 maybe_build_generic_op (enum tree_code code
, tree type
, tree
*ops
)
248 case VIEW_CONVERT_EXPR
:
249 ops
[0] = build1 (code
, type
, ops
[0]);
252 ops
[0] = build3 (code
, type
, ops
[0], ops
[1], ops
[2]);
253 ops
[1] = ops
[2] = NULL_TREE
;
259 tree (*mprts_hook
) (code_helper
, tree
, tree
*);
261 /* Try to build a call to FN with return type TYPE and the NARGS
262 arguments given in OPS. Return null if the target doesn't support
266 build_call_internal (internal_fn fn
, tree type
, unsigned int nargs
, tree
*ops
)
268 if (direct_internal_fn_p (fn
))
270 tree_pair types
= direct_internal_fn_types (fn
, type
, ops
);
271 if (!direct_internal_fn_supported_p (fn
, types
, OPTIMIZE_FOR_BOTH
))
274 return gimple_build_call_internal (fn
, nargs
, ops
[0], ops
[1], ops
[2]);
277 /* Push the exploded expression described by RCODE, TYPE and OPS
278 as a statement to SEQ if necessary and return a gimple value
279 denoting the value of the expression. If RES is not NULL
280 then the result will be always RES and even gimple values are
284 maybe_push_res_to_seq (code_helper rcode
, tree type
, tree
*ops
,
285 gimple_seq
*seq
, tree res
)
287 if (rcode
.is_tree_code ())
290 && gimple_simplified_result_is_gimple_val (rcode
, ops
))
294 tree tem
= mprts_hook (rcode
, type
, ops
);
300 /* Play safe and do not allow abnormals to be mentioned in
301 newly created statements. */
302 if ((TREE_CODE (ops
[0]) == SSA_NAME
303 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[0]))
305 && TREE_CODE (ops
[1]) == SSA_NAME
306 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[1]))
308 && TREE_CODE (ops
[2]) == SSA_NAME
309 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[2]))
310 || (COMPARISON_CLASS_P (ops
[0])
311 && ((TREE_CODE (TREE_OPERAND (ops
[0], 0)) == SSA_NAME
312 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops
[0],
314 || (TREE_CODE (TREE_OPERAND (ops
[0], 1)) == SSA_NAME
315 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops
[0],
320 if (gimple_in_ssa_p (cfun
))
321 res
= make_ssa_name (type
);
323 res
= create_tmp_reg (type
);
325 maybe_build_generic_op (rcode
, type
, ops
);
326 gimple
*new_stmt
= gimple_build_assign (res
, rcode
,
327 ops
[0], ops
[1], ops
[2]);
328 gimple_seq_add_stmt_without_update (seq
, new_stmt
);
335 combined_fn fn
= rcode
;
336 /* Play safe and do not allow abnormals to be mentioned in
337 newly created statements. */
339 for (nargs
= 0; nargs
< 3; ++nargs
)
343 if (TREE_CODE (ops
[nargs
]) == SSA_NAME
344 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[nargs
]))
347 gcc_assert (nargs
!= 0);
348 gcall
*new_stmt
= NULL
;
349 if (internal_fn_p (fn
))
351 /* Generate the given function if we can. */
352 internal_fn ifn
= as_internal_fn (fn
);
353 new_stmt
= build_call_internal (ifn
, type
, nargs
, ops
);
359 /* Find the function we want to call. */
360 tree decl
= builtin_decl_implicit (as_builtin_fn (fn
));
364 /* We can't and should not emit calls to non-const functions. */
365 if (!(flags_from_decl_or_type (decl
) & ECF_CONST
))
368 new_stmt
= gimple_build_call (decl
, nargs
, ops
[0], ops
[1], ops
[2]);
372 if (gimple_in_ssa_p (cfun
))
373 res
= make_ssa_name (type
);
375 res
= create_tmp_reg (type
);
377 gimple_call_set_lhs (new_stmt
, res
);
378 gimple_seq_add_stmt_without_update (seq
, new_stmt
);
384 /* Public API overloads follow for operation being tree_code or
385 built_in_function and for one to three operands or arguments.
386 They return NULL_TREE if nothing could be simplified or
387 the resulting simplified value with parts pushed to SEQ.
388 If SEQ is NULL then if the simplification needs to create
389 new stmts it will fail. If VALUEIZE is non-NULL then all
390 SSA names will be valueized using that hook prior to
391 applying simplifications. */
396 gimple_simplify (enum tree_code code
, tree type
,
398 gimple_seq
*seq
, tree (*valueize
)(tree
))
400 if (constant_for_folding (op0
))
402 tree res
= const_unop (code
, type
, op0
);
404 && CONSTANT_CLASS_P (res
))
410 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
413 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
419 gimple_simplify (enum tree_code code
, tree type
,
421 gimple_seq
*seq
, tree (*valueize
)(tree
))
423 if (constant_for_folding (op0
) && constant_for_folding (op1
))
425 tree res
= const_binop (code
, type
, op0
, op1
);
427 && CONSTANT_CLASS_P (res
))
431 /* Canonicalize operand order both for matching and fallback stmt
433 if ((commutative_tree_code (code
)
434 || TREE_CODE_CLASS (code
) == tcc_comparison
)
435 && tree_swap_operands_p (op0
, op1
))
437 std::swap (op0
, op1
);
438 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
439 code
= swap_tree_comparison (code
);
444 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
445 code
, type
, op0
, op1
))
447 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
453 gimple_simplify (enum tree_code code
, tree type
,
454 tree op0
, tree op1
, tree op2
,
455 gimple_seq
*seq
, tree (*valueize
)(tree
))
457 if (constant_for_folding (op0
) && constant_for_folding (op1
)
458 && constant_for_folding (op2
))
460 tree res
= fold_ternary
/*_to_constant */ (code
, type
, op0
, op1
, op2
);
462 && CONSTANT_CLASS_P (res
))
466 /* Canonicalize operand order both for matching and fallback stmt
468 if (commutative_ternary_tree_code (code
)
469 && tree_swap_operands_p (op0
, op1
))
470 std::swap (op0
, op1
);
474 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
475 code
, type
, op0
, op1
, op2
))
477 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
480 /* Builtin function with one argument. */
483 gimple_simplify (enum built_in_function fn
, tree type
,
485 gimple_seq
*seq
, tree (*valueize
)(tree
))
487 if (constant_for_folding (arg0
))
489 tree res
= fold_const_call (as_combined_fn (fn
), type
, arg0
);
490 if (res
&& CONSTANT_CLASS_P (res
))
496 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
497 as_combined_fn (fn
), type
, arg0
))
499 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
502 /* Builtin function with two arguments. */
505 gimple_simplify (enum built_in_function fn
, tree type
,
506 tree arg0
, tree arg1
,
507 gimple_seq
*seq
, tree (*valueize
)(tree
))
509 if (constant_for_folding (arg0
)
510 && constant_for_folding (arg1
))
512 tree res
= fold_const_call (as_combined_fn (fn
), type
, arg0
, arg1
);
513 if (res
&& CONSTANT_CLASS_P (res
))
519 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
520 as_combined_fn (fn
), type
, arg0
, arg1
))
522 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
525 /* Builtin function with three arguments. */
528 gimple_simplify (enum built_in_function fn
, tree type
,
529 tree arg0
, tree arg1
, tree arg2
,
530 gimple_seq
*seq
, tree (*valueize
)(tree
))
532 if (constant_for_folding (arg0
)
533 && constant_for_folding (arg1
)
534 && constant_for_folding (arg2
))
536 tree res
= fold_const_call (as_combined_fn (fn
), type
, arg0
, arg1
, arg2
);
537 if (res
&& CONSTANT_CLASS_P (res
))
543 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
544 as_combined_fn (fn
), type
, arg0
, arg1
, arg2
))
546 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
549 /* Helper for gimple_simplify valueizing OP using VALUEIZE and setting
550 VALUEIZED to true if valueization changed OP. */
553 do_valueize (tree op
, tree (*valueize
)(tree
), bool &valueized
)
555 if (valueize
&& TREE_CODE (op
) == SSA_NAME
)
557 tree tem
= valueize (op
);
558 if (tem
&& tem
!= op
)
567 /* The main STMT based simplification entry. It is used by the fold_stmt
568 and the fold_stmt_to_constant APIs. */
571 gimple_simplify (gimple
*stmt
,
572 code_helper
*rcode
, tree
*ops
,
574 tree (*valueize
)(tree
), tree (*top_valueize
)(tree
))
576 switch (gimple_code (stmt
))
580 enum tree_code code
= gimple_assign_rhs_code (stmt
);
581 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
582 switch (gimple_assign_rhs_class (stmt
))
584 case GIMPLE_SINGLE_RHS
:
585 if (code
== REALPART_EXPR
586 || code
== IMAGPART_EXPR
587 || code
== VIEW_CONVERT_EXPR
)
589 tree op0
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
590 bool valueized
= false;
591 op0
= do_valueize (op0
, top_valueize
, valueized
);
594 return (gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
)
597 else if (code
== BIT_FIELD_REF
)
599 tree rhs1
= gimple_assign_rhs1 (stmt
);
600 tree op0
= TREE_OPERAND (rhs1
, 0);
601 bool valueized
= false;
602 op0
= do_valueize (op0
, top_valueize
, valueized
);
605 ops
[1] = TREE_OPERAND (rhs1
, 1);
606 ops
[2] = TREE_OPERAND (rhs1
, 2);
607 return (gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
)
610 else if (code
== SSA_NAME
613 tree op0
= gimple_assign_rhs1 (stmt
);
614 tree valueized
= top_valueize (op0
);
615 if (!valueized
|| op0
== valueized
)
618 *rcode
= TREE_CODE (op0
);
622 case GIMPLE_UNARY_RHS
:
624 tree rhs1
= gimple_assign_rhs1 (stmt
);
625 bool valueized
= false;
626 rhs1
= do_valueize (rhs1
, top_valueize
, valueized
);
629 return (gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
)
632 case GIMPLE_BINARY_RHS
:
634 tree rhs1
= gimple_assign_rhs1 (stmt
);
635 tree rhs2
= gimple_assign_rhs2 (stmt
);
636 bool valueized
= false;
637 rhs1
= do_valueize (rhs1
, top_valueize
, valueized
);
638 rhs2
= do_valueize (rhs2
, top_valueize
, valueized
);
642 return (gimple_resimplify2 (seq
, rcode
, type
, ops
, valueize
)
645 case GIMPLE_TERNARY_RHS
:
647 bool valueized
= false;
648 tree rhs1
= gimple_assign_rhs1 (stmt
);
649 /* If this is a [VEC_]COND_EXPR first try to simplify an
650 embedded GENERIC condition. */
651 if (code
== COND_EXPR
652 || code
== VEC_COND_EXPR
)
654 if (COMPARISON_CLASS_P (rhs1
))
656 tree lhs
= TREE_OPERAND (rhs1
, 0);
657 tree rhs
= TREE_OPERAND (rhs1
, 1);
658 lhs
= do_valueize (lhs
, top_valueize
, valueized
);
659 rhs
= do_valueize (rhs
, top_valueize
, valueized
);
660 code_helper rcode2
= TREE_CODE (rhs1
);
664 if ((gimple_resimplify2 (seq
, &rcode2
, TREE_TYPE (rhs1
),
667 && rcode2
.is_tree_code ())
670 if (TREE_CODE_CLASS ((enum tree_code
)rcode2
)
672 rhs1
= build2 (rcode2
, TREE_TYPE (rhs1
),
674 else if (rcode2
== SSA_NAME
675 || rcode2
== INTEGER_CST
676 || rcode2
== VECTOR_CST
)
683 tree rhs2
= gimple_assign_rhs2 (stmt
);
684 tree rhs3
= gimple_assign_rhs3 (stmt
);
685 rhs1
= do_valueize (rhs1
, top_valueize
, valueized
);
686 rhs2
= do_valueize (rhs2
, top_valueize
, valueized
);
687 rhs3
= do_valueize (rhs3
, top_valueize
, valueized
);
692 return (gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
)
702 /* ??? This way we can't simplify calls with side-effects. */
703 if (gimple_call_lhs (stmt
) != NULL_TREE
704 && gimple_call_num_args (stmt
) >= 1
705 && gimple_call_num_args (stmt
) <= 3)
707 bool valueized
= false;
708 if (gimple_call_internal_p (stmt
))
709 *rcode
= as_combined_fn (gimple_call_internal_fn (stmt
));
712 tree fn
= gimple_call_fn (stmt
);
716 fn
= do_valueize (fn
, top_valueize
, valueized
);
717 if (TREE_CODE (fn
) != ADDR_EXPR
718 || TREE_CODE (TREE_OPERAND (fn
, 0)) != FUNCTION_DECL
)
721 tree decl
= TREE_OPERAND (fn
, 0);
722 if (DECL_BUILT_IN_CLASS (decl
) != BUILT_IN_NORMAL
723 || !gimple_builtin_call_types_compatible_p (stmt
, decl
))
726 *rcode
= as_combined_fn (DECL_FUNCTION_CODE (decl
));
729 tree type
= TREE_TYPE (gimple_call_lhs (stmt
));
730 for (unsigned i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
732 tree arg
= gimple_call_arg (stmt
, i
);
733 ops
[i
] = do_valueize (arg
, top_valueize
, valueized
);
735 switch (gimple_call_num_args (stmt
))
738 return (gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
)
741 return (gimple_resimplify2 (seq
, rcode
, type
, ops
, valueize
)
744 return (gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
)
754 tree lhs
= gimple_cond_lhs (stmt
);
755 tree rhs
= gimple_cond_rhs (stmt
);
756 bool valueized
= false;
757 lhs
= do_valueize (lhs
, top_valueize
, valueized
);
758 rhs
= do_valueize (rhs
, top_valueize
, valueized
);
759 *rcode
= gimple_cond_code (stmt
);
762 return (gimple_resimplify2 (seq
, rcode
,
763 boolean_type_node
, ops
, valueize
)
775 /* Helper for the autogenerated code, valueize OP. */
778 do_valueize (tree (*valueize
)(tree
), tree op
)
780 if (valueize
&& TREE_CODE (op
) == SSA_NAME
)
781 return valueize (op
);
785 /* Routine to determine if the types T1 and T2 are effectively
786 the same for GIMPLE. If T1 or T2 is not a type, the test
787 applies to their TREE_TYPE. */
790 types_match (tree t1
, tree t2
)
797 return types_compatible_p (t1
, t2
);
800 /* Return if T has a single use. For GIMPLE, we also allow any
801 non-SSA_NAME (ie constants) and zero uses to cope with uses
802 that aren't linked up yet. */
807 return TREE_CODE (t
) != SSA_NAME
|| has_zero_uses (t
) || has_single_use (t
);
810 /* Return true if math operations should be canonicalized,
811 e.g. sqrt(sqrt(x)) -> pow(x, 0.25). */
814 canonicalize_math_p ()
816 return !cfun
|| (cfun
->curr_properties
& PROP_gimple_opt_math
) == 0;