1 /* Preamble and helpers for the autogenerated gimple-match.c file.
2 Copyright (C) 2014-2015 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"
29 #include "fold-const.h"
30 #include "stringpool.h"
31 #include "stor-layout.h"
33 #include "hard-reg-set.h"
36 #include "basic-block.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "gimple-expr.h"
42 #include "gimple-ssa.h"
43 #include "tree-ssanames.h"
44 #include "gimple-fold.h"
45 #include "gimple-iterator.h"
47 #include "insn-config.h"
58 #include "tree-phinodes.h"
59 #include "ssa-iterators.h"
61 #include "gimple-match.h"
64 /* Forward declarations of the private auto-generated matchers.
65 They expect valueized operands in canonical order and do not
66 perform simplification of all-constant operands. */
67 static bool gimple_simplify (code_helper
*, tree
*,
68 gimple_seq
*, tree (*)(tree
),
69 code_helper
, tree
, tree
);
70 static bool gimple_simplify (code_helper
*, tree
*,
71 gimple_seq
*, tree (*)(tree
),
72 code_helper
, tree
, tree
, tree
);
73 static bool gimple_simplify (code_helper
*, tree
*,
74 gimple_seq
*, tree (*)(tree
),
75 code_helper
, tree
, tree
, tree
, tree
);
78 /* Return whether T is a constant that we'll dispatch to fold to
79 evaluate fully constant expressions. */
82 constant_for_folding (tree t
)
84 return (CONSTANT_CLASS_P (t
)
85 /* The following is only interesting to string builtins. */
86 || (TREE_CODE (t
) == ADDR_EXPR
87 && TREE_CODE (TREE_OPERAND (t
, 0)) == STRING_CST
));
91 /* Helper that matches and simplifies the toplevel result from
92 a gimple_simplify run (where we don't want to build
93 a stmt in case it's used in in-place folding). Replaces
94 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
95 result and returns whether any change was made. */
98 gimple_resimplify1 (gimple_seq
*seq
,
99 code_helper
*res_code
, tree type
, tree
*res_ops
,
100 tree (*valueize
)(tree
))
102 if (constant_for_folding (res_ops
[0]))
104 tree tem
= NULL_TREE
;
105 if (res_code
->is_tree_code ())
106 tem
= const_unop (*res_code
, type
, res_ops
[0]);
109 tree decl
= builtin_decl_implicit (*res_code
);
112 tem
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, res_ops
, 1, false);
115 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
117 tem
= fold_convert (type
, tem
);
122 && CONSTANT_CLASS_P (tem
))
125 res_ops
[1] = NULL_TREE
;
126 res_ops
[2] = NULL_TREE
;
127 *res_code
= TREE_CODE (res_ops
[0]);
132 code_helper res_code2
;
133 tree res_ops2
[3] = {};
134 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
135 *res_code
, type
, res_ops
[0]))
137 *res_code
= res_code2
;
138 res_ops
[0] = res_ops2
[0];
139 res_ops
[1] = res_ops2
[1];
140 res_ops
[2] = res_ops2
[2];
147 /* Helper that matches and simplifies the toplevel result from
148 a gimple_simplify run (where we don't want to build
149 a stmt in case it's used in in-place folding). Replaces
150 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
151 result and returns whether any change was made. */
154 gimple_resimplify2 (gimple_seq
*seq
,
155 code_helper
*res_code
, tree type
, tree
*res_ops
,
156 tree (*valueize
)(tree
))
158 if (constant_for_folding (res_ops
[0]) && constant_for_folding (res_ops
[1]))
160 tree tem
= NULL_TREE
;
161 if (res_code
->is_tree_code ())
162 tem
= const_binop (*res_code
, type
, res_ops
[0], res_ops
[1]);
165 tree decl
= builtin_decl_implicit (*res_code
);
168 tem
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, res_ops
, 2, false);
171 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
173 tem
= fold_convert (type
, tem
);
178 && CONSTANT_CLASS_P (tem
))
181 res_ops
[1] = NULL_TREE
;
182 res_ops
[2] = NULL_TREE
;
183 *res_code
= TREE_CODE (res_ops
[0]);
188 /* Canonicalize operand order. */
189 bool canonicalized
= false;
190 if (res_code
->is_tree_code ()
191 && (TREE_CODE_CLASS ((enum tree_code
) *res_code
) == tcc_comparison
192 || commutative_tree_code (*res_code
))
193 && tree_swap_operands_p (res_ops
[0], res_ops
[1], false))
195 tree tem
= res_ops
[0];
196 res_ops
[0] = res_ops
[1];
198 if (TREE_CODE_CLASS ((enum tree_code
) *res_code
) == tcc_comparison
)
199 *res_code
= swap_tree_comparison (*res_code
);
200 canonicalized
= true;
203 code_helper res_code2
;
204 tree res_ops2
[3] = {};
205 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
206 *res_code
, type
, res_ops
[0], res_ops
[1]))
208 *res_code
= res_code2
;
209 res_ops
[0] = res_ops2
[0];
210 res_ops
[1] = res_ops2
[1];
211 res_ops
[2] = res_ops2
[2];
215 return canonicalized
;
218 /* Helper that matches and simplifies the toplevel result from
219 a gimple_simplify run (where we don't want to build
220 a stmt in case it's used in in-place folding). Replaces
221 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
222 result and returns whether any change was made. */
225 gimple_resimplify3 (gimple_seq
*seq
,
226 code_helper
*res_code
, tree type
, tree
*res_ops
,
227 tree (*valueize
)(tree
))
229 if (constant_for_folding (res_ops
[0]) && constant_for_folding (res_ops
[1])
230 && constant_for_folding (res_ops
[2]))
232 tree tem
= NULL_TREE
;
233 if (res_code
->is_tree_code ())
234 tem
= fold_ternary
/*_to_constant*/ (*res_code
, type
, res_ops
[0],
235 res_ops
[1], res_ops
[2]);
238 tree decl
= builtin_decl_implicit (*res_code
);
241 tem
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, res_ops
, 3, false);
244 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
246 tem
= fold_convert (type
, tem
);
251 && CONSTANT_CLASS_P (tem
))
254 res_ops
[1] = NULL_TREE
;
255 res_ops
[2] = NULL_TREE
;
256 *res_code
= TREE_CODE (res_ops
[0]);
261 /* Canonicalize operand order. */
262 bool canonicalized
= false;
263 if (res_code
->is_tree_code ()
264 && commutative_ternary_tree_code (*res_code
)
265 && tree_swap_operands_p (res_ops
[0], res_ops
[1], false))
267 tree tem
= res_ops
[0];
268 res_ops
[0] = res_ops
[1];
270 canonicalized
= true;
273 code_helper res_code2
;
274 tree res_ops2
[3] = {};
275 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
277 res_ops
[0], res_ops
[1], res_ops
[2]))
279 *res_code
= res_code2
;
280 res_ops
[0] = res_ops2
[0];
281 res_ops
[1] = res_ops2
[1];
282 res_ops
[2] = res_ops2
[2];
286 return canonicalized
;
290 /* If in GIMPLE expressions with CODE go as single-rhs build
291 a GENERIC tree for that expression into *OP0. */
294 maybe_build_generic_op (enum tree_code code
, tree type
,
295 tree
*op0
, tree op1
, tree op2
)
301 case VIEW_CONVERT_EXPR
:
302 *op0
= build1 (code
, type
, *op0
);
305 *op0
= build3 (code
, type
, *op0
, op1
, op2
);
311 /* Push the exploded expression described by RCODE, TYPE and OPS
312 as a statement to SEQ if necessary and return a gimple value
313 denoting the value of the expression. If RES is not NULL
314 then the result will be always RES and even gimple values are
318 maybe_push_res_to_seq (code_helper rcode
, tree type
, tree
*ops
,
319 gimple_seq
*seq
, tree res
)
321 if (rcode
.is_tree_code ())
324 && (TREE_CODE_LENGTH ((tree_code
) rcode
) == 0
325 || ((tree_code
) rcode
) == ADDR_EXPR
)
326 && is_gimple_val (ops
[0]))
330 /* Play safe and do not allow abnormals to be mentioned in
331 newly created statements. */
332 if ((TREE_CODE (ops
[0]) == SSA_NAME
333 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[0]))
335 && TREE_CODE (ops
[1]) == SSA_NAME
336 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[1]))
338 && TREE_CODE (ops
[2]) == SSA_NAME
339 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[2])))
342 res
= make_ssa_name (type
);
343 maybe_build_generic_op (rcode
, type
, &ops
[0], ops
[1], ops
[2]);
344 gimple new_stmt
= gimple_build_assign (res
, rcode
,
345 ops
[0], ops
[1], ops
[2]);
346 gimple_seq_add_stmt_without_update (seq
, new_stmt
);
353 tree decl
= builtin_decl_implicit (rcode
);
356 unsigned nargs
= type_num_arguments (TREE_TYPE (decl
));
357 gcc_assert (nargs
<= 3);
358 /* Play safe and do not allow abnormals to be mentioned in
359 newly created statements. */
360 if ((TREE_CODE (ops
[0]) == SSA_NAME
361 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[0]))
363 && TREE_CODE (ops
[1]) == SSA_NAME
364 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[1]))
366 && TREE_CODE (ops
[2]) == SSA_NAME
367 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[2])))
370 res
= make_ssa_name (type
);
371 gimple new_stmt
= gimple_build_call (decl
, nargs
, ops
[0], ops
[1], ops
[2]);
372 gimple_call_set_lhs (new_stmt
, res
);
373 gimple_seq_add_stmt_without_update (seq
, new_stmt
);
379 /* Public API overloads follow for operation being tree_code or
380 built_in_function and for one to three operands or arguments.
381 They return NULL_TREE if nothing could be simplified or
382 the resulting simplified value with parts pushed to SEQ.
383 If SEQ is NULL then if the simplification needs to create
384 new stmts it will fail. If VALUEIZE is non-NULL then all
385 SSA names will be valueized using that hook prior to
386 applying simplifications. */
391 gimple_simplify (enum tree_code code
, tree type
,
393 gimple_seq
*seq
, tree (*valueize
)(tree
))
395 if (constant_for_folding (op0
))
397 tree res
= const_unop (code
, type
, op0
);
399 && CONSTANT_CLASS_P (res
))
405 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
408 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
414 gimple_simplify (enum tree_code code
, tree type
,
416 gimple_seq
*seq
, tree (*valueize
)(tree
))
418 if (constant_for_folding (op0
) && constant_for_folding (op1
))
420 tree res
= const_binop (code
, type
, op0
, op1
);
422 && CONSTANT_CLASS_P (res
))
426 /* Canonicalize operand order both for matching and fallback stmt
428 if ((commutative_tree_code (code
)
429 || TREE_CODE_CLASS (code
) == tcc_comparison
)
430 && tree_swap_operands_p (op0
, op1
, false))
435 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
436 code
= swap_tree_comparison (code
);
441 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
442 code
, type
, op0
, op1
))
444 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
450 gimple_simplify (enum tree_code code
, tree type
,
451 tree op0
, tree op1
, tree op2
,
452 gimple_seq
*seq
, tree (*valueize
)(tree
))
454 if (constant_for_folding (op0
) && constant_for_folding (op1
)
455 && constant_for_folding (op2
))
457 tree res
= fold_ternary
/*_to_constant */ (code
, type
, op0
, op1
, op2
);
459 && CONSTANT_CLASS_P (res
))
463 /* Canonicalize operand order both for matching and fallback stmt
465 if (commutative_ternary_tree_code (code
)
466 && tree_swap_operands_p (op0
, op1
, false))
475 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
476 code
, type
, op0
, op1
, op2
))
478 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
481 /* Builtin function with one argument. */
484 gimple_simplify (enum built_in_function fn
, tree type
,
486 gimple_seq
*seq
, tree (*valueize
)(tree
))
488 if (constant_for_folding (arg0
))
490 tree decl
= builtin_decl_implicit (fn
);
493 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, &arg0
, 1, false);
496 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
498 res
= fold_convert (type
, res
);
499 if (CONSTANT_CLASS_P (res
))
507 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
510 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
513 /* Builtin function with two arguments. */
516 gimple_simplify (enum built_in_function fn
, tree type
,
517 tree arg0
, tree arg1
,
518 gimple_seq
*seq
, tree (*valueize
)(tree
))
520 if (constant_for_folding (arg0
)
521 && constant_for_folding (arg1
))
523 tree decl
= builtin_decl_implicit (fn
);
529 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, args
, 2, false);
532 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
534 res
= fold_convert (type
, res
);
535 if (CONSTANT_CLASS_P (res
))
543 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
544 fn
, type
, arg0
, arg1
))
546 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
549 /* Builtin function with three arguments. */
552 gimple_simplify (enum built_in_function fn
, tree type
,
553 tree arg0
, tree arg1
, tree arg2
,
554 gimple_seq
*seq
, tree (*valueize
)(tree
))
556 if (constant_for_folding (arg0
)
557 && constant_for_folding (arg1
)
558 && constant_for_folding (arg2
))
560 tree decl
= builtin_decl_implicit (fn
);
567 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, args
, 3, false);
570 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
572 res
= fold_convert (type
, res
);
573 if (CONSTANT_CLASS_P (res
))
581 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
582 fn
, type
, arg0
, arg1
, arg2
))
584 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
588 /* The main STMT based simplification entry. It is used by the fold_stmt
589 and the fold_stmt_to_constant APIs. */
592 gimple_simplify (gimple stmt
,
593 code_helper
*rcode
, tree
*ops
,
595 tree (*valueize
)(tree
), tree (*top_valueize
)(tree
))
597 switch (gimple_code (stmt
))
601 enum tree_code code
= gimple_assign_rhs_code (stmt
);
602 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
603 switch (gimple_assign_rhs_class (stmt
))
605 case GIMPLE_SINGLE_RHS
:
606 if (code
== REALPART_EXPR
607 || code
== IMAGPART_EXPR
608 || code
== VIEW_CONVERT_EXPR
)
610 tree op0
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
611 if (top_valueize
&& TREE_CODE (op0
) == SSA_NAME
)
613 tree tem
= top_valueize (op0
);
619 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
621 else if (code
== BIT_FIELD_REF
)
623 tree rhs1
= gimple_assign_rhs1 (stmt
);
624 tree op0
= TREE_OPERAND (rhs1
, 0);
625 if (top_valueize
&& TREE_CODE (op0
) == SSA_NAME
)
627 tree tem
= top_valueize (op0
);
633 ops
[1] = TREE_OPERAND (rhs1
, 1);
634 ops
[2] = TREE_OPERAND (rhs1
, 2);
635 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
637 else if (code
== SSA_NAME
640 tree op0
= gimple_assign_rhs1 (stmt
);
641 tree valueized
= top_valueize (op0
);
642 if (!valueized
|| op0
== valueized
)
645 *rcode
= TREE_CODE (op0
);
649 case GIMPLE_UNARY_RHS
:
651 tree rhs1
= gimple_assign_rhs1 (stmt
);
652 if (top_valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
654 tree tem
= top_valueize (rhs1
);
660 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
662 case GIMPLE_BINARY_RHS
:
664 tree rhs1
= gimple_assign_rhs1 (stmt
);
665 if (top_valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
667 tree tem
= top_valueize (rhs1
);
671 tree rhs2
= gimple_assign_rhs2 (stmt
);
672 if (top_valueize
&& TREE_CODE (rhs2
) == SSA_NAME
)
674 tree tem
= top_valueize (rhs2
);
681 return gimple_resimplify2 (seq
, rcode
, type
, ops
, valueize
);
683 case GIMPLE_TERNARY_RHS
:
685 tree rhs1
= gimple_assign_rhs1 (stmt
);
686 if (top_valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
688 tree tem
= top_valueize (rhs1
);
692 tree rhs2
= gimple_assign_rhs2 (stmt
);
693 if (top_valueize
&& TREE_CODE (rhs2
) == SSA_NAME
)
695 tree tem
= top_valueize (rhs2
);
699 tree rhs3
= gimple_assign_rhs3 (stmt
);
700 if (top_valueize
&& TREE_CODE (rhs3
) == SSA_NAME
)
702 tree tem
= top_valueize (rhs3
);
710 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
719 /* ??? This way we can't simplify calls with side-effects. */
720 if (gimple_call_lhs (stmt
) != NULL_TREE
)
722 tree fn
= gimple_call_fn (stmt
);
723 /* ??? Internal function support missing. */
726 if (top_valueize
&& TREE_CODE (fn
) == SSA_NAME
)
728 tree tem
= top_valueize (fn
);
733 || TREE_CODE (fn
) != ADDR_EXPR
734 || TREE_CODE (TREE_OPERAND (fn
, 0)) != FUNCTION_DECL
735 || DECL_BUILT_IN_CLASS (TREE_OPERAND (fn
, 0)) != BUILT_IN_NORMAL
736 || !builtin_decl_implicit (DECL_FUNCTION_CODE (TREE_OPERAND (fn
, 0)))
737 || !gimple_builtin_call_types_compatible_p (stmt
,
738 TREE_OPERAND (fn
, 0)))
741 tree decl
= TREE_OPERAND (fn
, 0);
742 tree type
= TREE_TYPE (gimple_call_lhs (stmt
));
743 switch (gimple_call_num_args (stmt
))
747 tree arg1
= gimple_call_arg (stmt
, 0);
748 if (top_valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
750 tree tem
= top_valueize (arg1
);
754 *rcode
= DECL_FUNCTION_CODE (decl
);
756 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
760 tree arg1
= gimple_call_arg (stmt
, 0);
761 if (top_valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
763 tree tem
= top_valueize (arg1
);
767 tree arg2
= gimple_call_arg (stmt
, 1);
768 if (top_valueize
&& TREE_CODE (arg2
) == SSA_NAME
)
770 tree tem
= top_valueize (arg2
);
774 *rcode
= DECL_FUNCTION_CODE (decl
);
777 return gimple_resimplify2 (seq
, rcode
, type
, ops
, valueize
);
781 tree arg1
= gimple_call_arg (stmt
, 0);
782 if (top_valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
784 tree tem
= top_valueize (arg1
);
788 tree arg2
= gimple_call_arg (stmt
, 1);
789 if (top_valueize
&& TREE_CODE (arg2
) == SSA_NAME
)
791 tree tem
= top_valueize (arg2
);
795 tree arg3
= gimple_call_arg (stmt
, 2);
796 if (top_valueize
&& TREE_CODE (arg3
) == SSA_NAME
)
798 tree tem
= top_valueize (arg3
);
802 *rcode
= DECL_FUNCTION_CODE (decl
);
806 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
816 tree lhs
= gimple_cond_lhs (stmt
);
817 if (top_valueize
&& TREE_CODE (lhs
) == SSA_NAME
)
819 tree tem
= top_valueize (lhs
);
823 tree rhs
= gimple_cond_rhs (stmt
);
824 if (top_valueize
&& TREE_CODE (rhs
) == SSA_NAME
)
826 tree tem
= top_valueize (rhs
);
830 *rcode
= gimple_cond_code (stmt
);
833 return gimple_resimplify2 (seq
, rcode
, boolean_type_node
, ops
, valueize
);
844 /* Helper for the autogenerated code, valueize OP. */
847 do_valueize (tree (*valueize
)(tree
), tree op
)
849 if (valueize
&& TREE_CODE (op
) == SSA_NAME
)
850 return valueize (op
);
854 /* Routine to determine if the types T1 and T2 are effectively
855 the same for GIMPLE. If T1 or T2 is not a type, the test
856 applies to their TREE_TYPE. */
859 types_match (tree t1
, tree t2
)
866 return types_compatible_p (t1
, t2
);
869 /* Return if T has a single use. For GIMPLE, we also allow any
870 non-SSA_NAME (ie constants) and zero uses to cope with uses
871 that aren't linked up yet. */
876 return TREE_CODE (t
) != SSA_NAME
|| has_zero_uses (t
) || has_single_use (t
);