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"
32 #include "fold-const.h"
33 #include "stringpool.h"
34 #include "stor-layout.h"
36 #include "hard-reg-set.h"
39 #include "basic-block.h"
40 #include "tree-ssa-alias.h"
41 #include "internal-fn.h"
42 #include "gimple-expr.h"
45 #include "gimple-ssa.h"
46 #include "tree-ssanames.h"
47 #include "gimple-fold.h"
48 #include "gimple-iterator.h"
51 #include "statistics.h"
52 #include "insn-config.h"
63 #include "tree-phinodes.h"
64 #include "ssa-iterators.h"
66 #include "gimple-match.h"
69 /* Forward declarations of the private auto-generated matchers.
70 They expect valueized operands in canonical order and do not
71 perform simplification of all-constant operands. */
72 static bool gimple_simplify (code_helper
*, tree
*,
73 gimple_seq
*, tree (*)(tree
),
74 code_helper
, tree
, tree
);
75 static bool gimple_simplify (code_helper
*, tree
*,
76 gimple_seq
*, tree (*)(tree
),
77 code_helper
, tree
, tree
, tree
);
78 static bool gimple_simplify (code_helper
*, tree
*,
79 gimple_seq
*, tree (*)(tree
),
80 code_helper
, tree
, tree
, tree
, tree
);
83 /* Return whether T is a constant that we'll dispatch to fold to
84 evaluate fully constant expressions. */
87 constant_for_folding (tree t
)
89 return (CONSTANT_CLASS_P (t
)
90 /* The following is only interesting to string builtins. */
91 || (TREE_CODE (t
) == ADDR_EXPR
92 && TREE_CODE (TREE_OPERAND (t
, 0)) == STRING_CST
));
96 /* Helper that matches and simplifies the toplevel result from
97 a gimple_simplify run (where we don't want to build
98 a stmt in case it's used in in-place folding). Replaces
99 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
100 result and returns whether any change was made. */
103 gimple_resimplify1 (gimple_seq
*seq
,
104 code_helper
*res_code
, tree type
, tree
*res_ops
,
105 tree (*valueize
)(tree
))
107 if (constant_for_folding (res_ops
[0]))
109 tree tem
= NULL_TREE
;
110 if (res_code
->is_tree_code ())
111 tem
= const_unop (*res_code
, type
, res_ops
[0]);
114 tree decl
= builtin_decl_implicit (*res_code
);
117 tem
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, res_ops
, 1, false);
120 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
122 tem
= fold_convert (type
, tem
);
127 && CONSTANT_CLASS_P (tem
))
130 res_ops
[1] = NULL_TREE
;
131 res_ops
[2] = NULL_TREE
;
132 *res_code
= TREE_CODE (res_ops
[0]);
137 code_helper res_code2
;
138 tree res_ops2
[3] = {};
139 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
140 *res_code
, type
, res_ops
[0]))
142 *res_code
= res_code2
;
143 res_ops
[0] = res_ops2
[0];
144 res_ops
[1] = res_ops2
[1];
145 res_ops
[2] = res_ops2
[2];
152 /* Helper that matches and simplifies the toplevel result from
153 a gimple_simplify run (where we don't want to build
154 a stmt in case it's used in in-place folding). Replaces
155 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
156 result and returns whether any change was made. */
159 gimple_resimplify2 (gimple_seq
*seq
,
160 code_helper
*res_code
, tree type
, tree
*res_ops
,
161 tree (*valueize
)(tree
))
163 if (constant_for_folding (res_ops
[0]) && constant_for_folding (res_ops
[1]))
165 tree tem
= NULL_TREE
;
166 if (res_code
->is_tree_code ())
167 tem
= const_binop (*res_code
, type
, res_ops
[0], res_ops
[1]);
170 tree decl
= builtin_decl_implicit (*res_code
);
173 tem
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, res_ops
, 2, false);
176 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
178 tem
= fold_convert (type
, tem
);
183 && CONSTANT_CLASS_P (tem
))
186 res_ops
[1] = NULL_TREE
;
187 res_ops
[2] = NULL_TREE
;
188 *res_code
= TREE_CODE (res_ops
[0]);
193 /* Canonicalize operand order. */
194 bool canonicalized
= false;
195 if (res_code
->is_tree_code ()
196 && (TREE_CODE_CLASS ((enum tree_code
) *res_code
) == tcc_comparison
197 || commutative_tree_code (*res_code
))
198 && tree_swap_operands_p (res_ops
[0], res_ops
[1], false))
200 tree tem
= res_ops
[0];
201 res_ops
[0] = res_ops
[1];
203 if (TREE_CODE_CLASS ((enum tree_code
) *res_code
) == tcc_comparison
)
204 *res_code
= swap_tree_comparison (*res_code
);
205 canonicalized
= true;
208 code_helper res_code2
;
209 tree res_ops2
[3] = {};
210 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
211 *res_code
, type
, res_ops
[0], res_ops
[1]))
213 *res_code
= res_code2
;
214 res_ops
[0] = res_ops2
[0];
215 res_ops
[1] = res_ops2
[1];
216 res_ops
[2] = res_ops2
[2];
220 return canonicalized
;
223 /* Helper that matches and simplifies the toplevel result from
224 a gimple_simplify run (where we don't want to build
225 a stmt in case it's used in in-place folding). Replaces
226 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
227 result and returns whether any change was made. */
230 gimple_resimplify3 (gimple_seq
*seq
,
231 code_helper
*res_code
, tree type
, tree
*res_ops
,
232 tree (*valueize
)(tree
))
234 if (constant_for_folding (res_ops
[0]) && constant_for_folding (res_ops
[1])
235 && constant_for_folding (res_ops
[2]))
237 tree tem
= NULL_TREE
;
238 if (res_code
->is_tree_code ())
239 tem
= fold_ternary
/*_to_constant*/ (*res_code
, type
, res_ops
[0],
240 res_ops
[1], res_ops
[2]);
243 tree decl
= builtin_decl_implicit (*res_code
);
246 tem
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, res_ops
, 3, false);
249 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
251 tem
= fold_convert (type
, tem
);
256 && CONSTANT_CLASS_P (tem
))
259 res_ops
[1] = NULL_TREE
;
260 res_ops
[2] = NULL_TREE
;
261 *res_code
= TREE_CODE (res_ops
[0]);
266 /* Canonicalize operand order. */
267 bool canonicalized
= false;
268 if (res_code
->is_tree_code ()
269 && commutative_ternary_tree_code (*res_code
)
270 && tree_swap_operands_p (res_ops
[0], res_ops
[1], false))
272 tree tem
= res_ops
[0];
273 res_ops
[0] = res_ops
[1];
275 canonicalized
= true;
278 code_helper res_code2
;
279 tree res_ops2
[3] = {};
280 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
282 res_ops
[0], res_ops
[1], res_ops
[2]))
284 *res_code
= res_code2
;
285 res_ops
[0] = res_ops2
[0];
286 res_ops
[1] = res_ops2
[1];
287 res_ops
[2] = res_ops2
[2];
291 return canonicalized
;
295 /* If in GIMPLE expressions with CODE go as single-rhs build
296 a GENERIC tree for that expression into *OP0. */
299 maybe_build_generic_op (enum tree_code code
, tree type
,
300 tree
*op0
, tree op1
, tree op2
)
306 case VIEW_CONVERT_EXPR
:
307 *op0
= build1 (code
, type
, *op0
);
310 *op0
= build3 (code
, type
, *op0
, op1
, op2
);
316 /* Push the exploded expression described by RCODE, TYPE and OPS
317 as a statement to SEQ if necessary and return a gimple value
318 denoting the value of the expression. If RES is not NULL
319 then the result will be always RES and even gimple values are
323 maybe_push_res_to_seq (code_helper rcode
, tree type
, tree
*ops
,
324 gimple_seq
*seq
, tree res
)
326 if (rcode
.is_tree_code ())
329 && (TREE_CODE_LENGTH ((tree_code
) rcode
) == 0
330 || ((tree_code
) rcode
) == ADDR_EXPR
)
331 && is_gimple_val (ops
[0]))
335 /* Play safe and do not allow abnormals to be mentioned in
336 newly created statements. */
337 if ((TREE_CODE (ops
[0]) == SSA_NAME
338 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[0]))
340 && TREE_CODE (ops
[1]) == SSA_NAME
341 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[1]))
343 && TREE_CODE (ops
[2]) == SSA_NAME
344 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[2])))
347 res
= make_ssa_name (type
);
348 maybe_build_generic_op (rcode
, type
, &ops
[0], ops
[1], ops
[2]);
349 gimple new_stmt
= gimple_build_assign (res
, rcode
,
350 ops
[0], ops
[1], ops
[2]);
351 gimple_seq_add_stmt_without_update (seq
, new_stmt
);
358 tree decl
= builtin_decl_implicit (rcode
);
361 unsigned nargs
= type_num_arguments (TREE_TYPE (decl
));
362 gcc_assert (nargs
<= 3);
363 /* Play safe and do not allow abnormals to be mentioned in
364 newly created statements. */
365 if ((TREE_CODE (ops
[0]) == SSA_NAME
366 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[0]))
368 && TREE_CODE (ops
[1]) == SSA_NAME
369 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[1]))
371 && TREE_CODE (ops
[2]) == SSA_NAME
372 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[2])))
375 res
= make_ssa_name (type
);
376 gimple new_stmt
= gimple_build_call (decl
, nargs
, ops
[0], ops
[1], ops
[2]);
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
, false))
440 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
441 code
= swap_tree_comparison (code
);
446 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
447 code
, type
, op0
, op1
))
449 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
455 gimple_simplify (enum tree_code code
, tree type
,
456 tree op0
, tree op1
, tree op2
,
457 gimple_seq
*seq
, tree (*valueize
)(tree
))
459 if (constant_for_folding (op0
) && constant_for_folding (op1
)
460 && constant_for_folding (op2
))
462 tree res
= fold_ternary
/*_to_constant */ (code
, type
, op0
, op1
, op2
);
464 && CONSTANT_CLASS_P (res
))
468 /* Canonicalize operand order both for matching and fallback stmt
470 if (commutative_ternary_tree_code (code
)
471 && tree_swap_operands_p (op0
, op1
, false))
480 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
481 code
, type
, op0
, op1
, op2
))
483 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
486 /* Builtin function with one argument. */
489 gimple_simplify (enum built_in_function fn
, tree type
,
491 gimple_seq
*seq
, tree (*valueize
)(tree
))
493 if (constant_for_folding (arg0
))
495 tree decl
= builtin_decl_implicit (fn
);
498 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, &arg0
, 1, false);
501 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
503 res
= fold_convert (type
, res
);
504 if (CONSTANT_CLASS_P (res
))
512 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
515 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
518 /* Builtin function with two arguments. */
521 gimple_simplify (enum built_in_function fn
, tree type
,
522 tree arg0
, tree arg1
,
523 gimple_seq
*seq
, tree (*valueize
)(tree
))
525 if (constant_for_folding (arg0
)
526 && constant_for_folding (arg1
))
528 tree decl
= builtin_decl_implicit (fn
);
534 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, args
, 2, false);
537 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
539 res
= fold_convert (type
, res
);
540 if (CONSTANT_CLASS_P (res
))
548 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
549 fn
, type
, arg0
, arg1
))
551 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
554 /* Builtin function with three arguments. */
557 gimple_simplify (enum built_in_function fn
, tree type
,
558 tree arg0
, tree arg1
, tree arg2
,
559 gimple_seq
*seq
, tree (*valueize
)(tree
))
561 if (constant_for_folding (arg0
)
562 && constant_for_folding (arg1
)
563 && constant_for_folding (arg2
))
565 tree decl
= builtin_decl_implicit (fn
);
572 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, args
, 3, false);
575 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
577 res
= fold_convert (type
, res
);
578 if (CONSTANT_CLASS_P (res
))
586 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
587 fn
, type
, arg0
, arg1
, arg2
))
589 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
593 /* The main STMT based simplification entry. It is used by the fold_stmt
594 and the fold_stmt_to_constant APIs. */
597 gimple_simplify (gimple stmt
,
598 code_helper
*rcode
, tree
*ops
,
600 tree (*valueize
)(tree
), tree (*top_valueize
)(tree
))
602 switch (gimple_code (stmt
))
606 enum tree_code code
= gimple_assign_rhs_code (stmt
);
607 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
608 switch (gimple_assign_rhs_class (stmt
))
610 case GIMPLE_SINGLE_RHS
:
611 if (code
== REALPART_EXPR
612 || code
== IMAGPART_EXPR
613 || code
== VIEW_CONVERT_EXPR
)
615 tree op0
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
616 if (top_valueize
&& TREE_CODE (op0
) == SSA_NAME
)
618 tree tem
= top_valueize (op0
);
624 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
626 else if (code
== BIT_FIELD_REF
)
628 tree rhs1
= gimple_assign_rhs1 (stmt
);
629 tree op0
= TREE_OPERAND (rhs1
, 0);
630 if (top_valueize
&& TREE_CODE (op0
) == SSA_NAME
)
632 tree tem
= top_valueize (op0
);
638 ops
[1] = TREE_OPERAND (rhs1
, 1);
639 ops
[2] = TREE_OPERAND (rhs1
, 2);
640 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
642 else if (code
== SSA_NAME
645 tree op0
= gimple_assign_rhs1 (stmt
);
646 tree valueized
= top_valueize (op0
);
647 if (!valueized
|| op0
== valueized
)
650 *rcode
= TREE_CODE (op0
);
654 case GIMPLE_UNARY_RHS
:
656 tree rhs1
= gimple_assign_rhs1 (stmt
);
657 if (top_valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
659 tree tem
= top_valueize (rhs1
);
665 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
667 case GIMPLE_BINARY_RHS
:
669 tree rhs1
= gimple_assign_rhs1 (stmt
);
670 if (top_valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
672 tree tem
= top_valueize (rhs1
);
676 tree rhs2
= gimple_assign_rhs2 (stmt
);
677 if (top_valueize
&& TREE_CODE (rhs2
) == SSA_NAME
)
679 tree tem
= top_valueize (rhs2
);
686 return gimple_resimplify2 (seq
, rcode
, type
, ops
, valueize
);
688 case GIMPLE_TERNARY_RHS
:
690 tree rhs1
= gimple_assign_rhs1 (stmt
);
691 if (top_valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
693 tree tem
= top_valueize (rhs1
);
697 tree rhs2
= gimple_assign_rhs2 (stmt
);
698 if (top_valueize
&& TREE_CODE (rhs2
) == SSA_NAME
)
700 tree tem
= top_valueize (rhs2
);
704 tree rhs3
= gimple_assign_rhs3 (stmt
);
705 if (top_valueize
&& TREE_CODE (rhs3
) == SSA_NAME
)
707 tree tem
= top_valueize (rhs3
);
715 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
724 /* ??? This way we can't simplify calls with side-effects. */
725 if (gimple_call_lhs (stmt
) != NULL_TREE
)
727 tree fn
= gimple_call_fn (stmt
);
728 /* ??? Internal function support missing. */
731 if (top_valueize
&& TREE_CODE (fn
) == SSA_NAME
)
733 tree tem
= top_valueize (fn
);
738 || TREE_CODE (fn
) != ADDR_EXPR
739 || TREE_CODE (TREE_OPERAND (fn
, 0)) != FUNCTION_DECL
740 || DECL_BUILT_IN_CLASS (TREE_OPERAND (fn
, 0)) != BUILT_IN_NORMAL
741 || !builtin_decl_implicit (DECL_FUNCTION_CODE (TREE_OPERAND (fn
, 0)))
742 || !gimple_builtin_call_types_compatible_p (stmt
,
743 TREE_OPERAND (fn
, 0)))
746 tree decl
= TREE_OPERAND (fn
, 0);
747 tree type
= TREE_TYPE (gimple_call_lhs (stmt
));
748 switch (gimple_call_num_args (stmt
))
752 tree arg1
= gimple_call_arg (stmt
, 0);
753 if (top_valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
755 tree tem
= top_valueize (arg1
);
759 *rcode
= DECL_FUNCTION_CODE (decl
);
761 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
765 tree arg1
= gimple_call_arg (stmt
, 0);
766 if (top_valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
768 tree tem
= top_valueize (arg1
);
772 tree arg2
= gimple_call_arg (stmt
, 1);
773 if (top_valueize
&& TREE_CODE (arg2
) == SSA_NAME
)
775 tree tem
= top_valueize (arg2
);
779 *rcode
= DECL_FUNCTION_CODE (decl
);
782 return gimple_resimplify2 (seq
, rcode
, type
, ops
, valueize
);
786 tree arg1
= gimple_call_arg (stmt
, 0);
787 if (top_valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
789 tree tem
= top_valueize (arg1
);
793 tree arg2
= gimple_call_arg (stmt
, 1);
794 if (top_valueize
&& TREE_CODE (arg2
) == SSA_NAME
)
796 tree tem
= top_valueize (arg2
);
800 tree arg3
= gimple_call_arg (stmt
, 2);
801 if (top_valueize
&& TREE_CODE (arg3
) == SSA_NAME
)
803 tree tem
= top_valueize (arg3
);
807 *rcode
= DECL_FUNCTION_CODE (decl
);
811 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
821 tree lhs
= gimple_cond_lhs (stmt
);
822 if (top_valueize
&& TREE_CODE (lhs
) == SSA_NAME
)
824 tree tem
= top_valueize (lhs
);
828 tree rhs
= gimple_cond_rhs (stmt
);
829 if (top_valueize
&& TREE_CODE (rhs
) == SSA_NAME
)
831 tree tem
= top_valueize (rhs
);
835 *rcode
= gimple_cond_code (stmt
);
838 return gimple_resimplify2 (seq
, rcode
, boolean_type_node
, ops
, valueize
);
849 /* Helper for the autogenerated code, valueize OP. */
852 do_valueize (tree (*valueize
)(tree
), tree op
)
854 if (valueize
&& TREE_CODE (op
) == SSA_NAME
)
855 return valueize (op
);
859 /* Routine to determine if the types T1 and T2 are effectively
860 the same for GIMPLE. If T1 or T2 is not a type, the test
861 applies to their TREE_TYPE. */
864 types_match (tree t1
, tree t2
)
871 return types_compatible_p (t1
, t2
);
874 /* Return if T has a single use. For GIMPLE, we also allow any
875 non-SSA_NAME (ie constants) and zero uses to cope with uses
876 that aren't linked up yet. */
881 return TREE_CODE (t
) != SSA_NAME
|| has_zero_uses (t
) || has_single_use (t
);