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"
27 #include "double-int.h"
35 #include "fold-const.h"
36 #include "stringpool.h"
37 #include "stor-layout.h"
40 #include "hard-reg-set.h"
44 #include "basic-block.h"
45 #include "tree-ssa-alias.h"
46 #include "internal-fn.h"
47 #include "gimple-expr.h"
50 #include "gimple-ssa.h"
51 #include "tree-ssanames.h"
52 #include "gimple-fold.h"
53 #include "gimple-iterator.h"
57 #include "tree-phinodes.h"
58 #include "ssa-iterators.h"
60 #include "gimple-match.h"
63 /* Forward declarations of the private auto-generated matchers.
64 They expect valueized operands in canonical order and do not
65 perform simplification of all-constant operands. */
66 static bool gimple_simplify (code_helper
*, tree
*,
67 gimple_seq
*, tree (*)(tree
),
68 code_helper
, tree
, tree
);
69 static bool gimple_simplify (code_helper
*, tree
*,
70 gimple_seq
*, tree (*)(tree
),
71 code_helper
, tree
, tree
, tree
);
72 static bool gimple_simplify (code_helper
*, tree
*,
73 gimple_seq
*, tree (*)(tree
),
74 code_helper
, tree
, tree
, tree
, tree
);
77 /* Return whether T is a constant that we'll dispatch to fold to
78 evaluate fully constant expressions. */
81 constant_for_folding (tree t
)
83 return (CONSTANT_CLASS_P (t
)
84 /* The following is only interesting to string builtins. */
85 || (TREE_CODE (t
) == ADDR_EXPR
86 && TREE_CODE (TREE_OPERAND (t
, 0)) == STRING_CST
));
90 /* Helper that matches and simplifies the toplevel result from
91 a gimple_simplify run (where we don't want to build
92 a stmt in case it's used in in-place folding). Replaces
93 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
94 result and returns whether any change was made. */
97 gimple_resimplify1 (gimple_seq
*seq
,
98 code_helper
*res_code
, tree type
, tree
*res_ops
,
99 tree (*valueize
)(tree
))
101 if (constant_for_folding (res_ops
[0]))
103 tree tem
= NULL_TREE
;
104 if (res_code
->is_tree_code ())
105 tem
= const_unop (*res_code
, type
, res_ops
[0]);
108 tree decl
= builtin_decl_implicit (*res_code
);
111 tem
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, res_ops
, 1, false);
114 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
116 tem
= fold_convert (type
, tem
);
121 && CONSTANT_CLASS_P (tem
))
124 res_ops
[1] = NULL_TREE
;
125 res_ops
[2] = NULL_TREE
;
126 *res_code
= TREE_CODE (res_ops
[0]);
131 code_helper res_code2
;
132 tree res_ops2
[3] = {};
133 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
134 *res_code
, type
, res_ops
[0]))
136 *res_code
= res_code2
;
137 res_ops
[0] = res_ops2
[0];
138 res_ops
[1] = res_ops2
[1];
139 res_ops
[2] = res_ops2
[2];
146 /* Helper that matches and simplifies the toplevel result from
147 a gimple_simplify run (where we don't want to build
148 a stmt in case it's used in in-place folding). Replaces
149 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
150 result and returns whether any change was made. */
153 gimple_resimplify2 (gimple_seq
*seq
,
154 code_helper
*res_code
, tree type
, tree
*res_ops
,
155 tree (*valueize
)(tree
))
157 if (constant_for_folding (res_ops
[0]) && constant_for_folding (res_ops
[1]))
159 tree tem
= NULL_TREE
;
160 if (res_code
->is_tree_code ())
161 tem
= const_binop (*res_code
, type
, res_ops
[0], res_ops
[1]);
164 tree decl
= builtin_decl_implicit (*res_code
);
167 tem
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, res_ops
, 2, false);
170 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
172 tem
= fold_convert (type
, tem
);
177 && CONSTANT_CLASS_P (tem
))
180 res_ops
[1] = NULL_TREE
;
181 res_ops
[2] = NULL_TREE
;
182 *res_code
= TREE_CODE (res_ops
[0]);
187 /* Canonicalize operand order. */
188 bool canonicalized
= false;
189 if (res_code
->is_tree_code ()
190 && (TREE_CODE_CLASS ((enum tree_code
) *res_code
) == tcc_comparison
191 || commutative_tree_code (*res_code
))
192 && tree_swap_operands_p (res_ops
[0], res_ops
[1], false))
194 tree tem
= res_ops
[0];
195 res_ops
[0] = res_ops
[1];
197 if (TREE_CODE_CLASS ((enum tree_code
) *res_code
) == tcc_comparison
)
198 *res_code
= swap_tree_comparison (*res_code
);
199 canonicalized
= true;
202 code_helper res_code2
;
203 tree res_ops2
[3] = {};
204 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
205 *res_code
, type
, res_ops
[0], res_ops
[1]))
207 *res_code
= res_code2
;
208 res_ops
[0] = res_ops2
[0];
209 res_ops
[1] = res_ops2
[1];
210 res_ops
[2] = res_ops2
[2];
214 return canonicalized
;
217 /* Helper that matches and simplifies the toplevel result from
218 a gimple_simplify run (where we don't want to build
219 a stmt in case it's used in in-place folding). Replaces
220 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
221 result and returns whether any change was made. */
224 gimple_resimplify3 (gimple_seq
*seq
,
225 code_helper
*res_code
, tree type
, tree
*res_ops
,
226 tree (*valueize
)(tree
))
228 if (constant_for_folding (res_ops
[0]) && constant_for_folding (res_ops
[1])
229 && constant_for_folding (res_ops
[2]))
231 tree tem
= NULL_TREE
;
232 if (res_code
->is_tree_code ())
233 tem
= fold_ternary
/*_to_constant*/ (*res_code
, type
, res_ops
[0],
234 res_ops
[1], res_ops
[2]);
237 tree decl
= builtin_decl_implicit (*res_code
);
240 tem
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, res_ops
, 3, false);
243 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
245 tem
= fold_convert (type
, tem
);
250 && CONSTANT_CLASS_P (tem
))
253 res_ops
[1] = NULL_TREE
;
254 res_ops
[2] = NULL_TREE
;
255 *res_code
= TREE_CODE (res_ops
[0]);
260 /* Canonicalize operand order. */
261 bool canonicalized
= false;
262 if (res_code
->is_tree_code ()
263 && commutative_ternary_tree_code (*res_code
)
264 && tree_swap_operands_p (res_ops
[0], res_ops
[1], false))
266 tree tem
= res_ops
[0];
267 res_ops
[0] = res_ops
[1];
269 canonicalized
= true;
272 code_helper res_code2
;
273 tree res_ops2
[3] = {};
274 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
276 res_ops
[0], res_ops
[1], res_ops
[2]))
278 *res_code
= res_code2
;
279 res_ops
[0] = res_ops2
[0];
280 res_ops
[1] = res_ops2
[1];
281 res_ops
[2] = res_ops2
[2];
285 return canonicalized
;
289 /* If in GIMPLE expressions with CODE go as single-rhs build
290 a GENERIC tree for that expression into *OP0. */
293 maybe_build_generic_op (enum tree_code code
, tree type
,
294 tree
*op0
, tree op1
, tree op2
)
300 case VIEW_CONVERT_EXPR
:
301 *op0
= build1 (code
, type
, *op0
);
304 *op0
= build3 (code
, type
, *op0
, op1
, op2
);
310 /* Push the exploded expression described by RCODE, TYPE and OPS
311 as a statement to SEQ if necessary and return a gimple value
312 denoting the value of the expression. If RES is not NULL
313 then the result will be always RES and even gimple values are
317 maybe_push_res_to_seq (code_helper rcode
, tree type
, tree
*ops
,
318 gimple_seq
*seq
, tree res
)
320 if (rcode
.is_tree_code ())
323 && (TREE_CODE_LENGTH ((tree_code
) rcode
) == 0
324 || ((tree_code
) rcode
) == ADDR_EXPR
)
325 && is_gimple_val (ops
[0]))
329 /* Play safe and do not allow abnormals to be mentioned in
330 newly created statements. */
331 if ((TREE_CODE (ops
[0]) == SSA_NAME
332 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[0]))
334 && TREE_CODE (ops
[1]) == SSA_NAME
335 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[1]))
337 && TREE_CODE (ops
[2]) == SSA_NAME
338 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[2])))
341 res
= make_ssa_name (type
);
342 maybe_build_generic_op (rcode
, type
, &ops
[0], ops
[1], ops
[2]);
343 gimple new_stmt
= gimple_build_assign (res
, rcode
,
344 ops
[0], ops
[1], ops
[2]);
345 gimple_seq_add_stmt_without_update (seq
, new_stmt
);
352 tree decl
= builtin_decl_implicit (rcode
);
355 unsigned nargs
= type_num_arguments (TREE_TYPE (decl
));
356 gcc_assert (nargs
<= 3);
357 /* Play safe and do not allow abnormals to be mentioned in
358 newly created statements. */
359 if ((TREE_CODE (ops
[0]) == SSA_NAME
360 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[0]))
362 && TREE_CODE (ops
[1]) == SSA_NAME
363 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[1]))
365 && TREE_CODE (ops
[2]) == SSA_NAME
366 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[2])))
369 res
= make_ssa_name (type
);
370 gimple new_stmt
= gimple_build_call (decl
, nargs
, ops
[0], ops
[1], ops
[2]);
371 gimple_call_set_lhs (new_stmt
, res
);
372 gimple_seq_add_stmt_without_update (seq
, new_stmt
);
378 /* Public API overloads follow for operation being tree_code or
379 built_in_function and for one to three operands or arguments.
380 They return NULL_TREE if nothing could be simplified or
381 the resulting simplified value with parts pushed to SEQ.
382 If SEQ is NULL then if the simplification needs to create
383 new stmts it will fail. If VALUEIZE is non-NULL then all
384 SSA names will be valueized using that hook prior to
385 applying simplifications. */
390 gimple_simplify (enum tree_code code
, tree type
,
392 gimple_seq
*seq
, tree (*valueize
)(tree
))
394 if (constant_for_folding (op0
))
396 tree res
= const_unop (code
, type
, op0
);
398 && CONSTANT_CLASS_P (res
))
404 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
407 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
413 gimple_simplify (enum tree_code code
, tree type
,
415 gimple_seq
*seq
, tree (*valueize
)(tree
))
417 if (constant_for_folding (op0
) && constant_for_folding (op1
))
419 tree res
= const_binop (code
, type
, op0
, op1
);
421 && CONSTANT_CLASS_P (res
))
425 /* Canonicalize operand order both for matching and fallback stmt
427 if ((commutative_tree_code (code
)
428 || TREE_CODE_CLASS (code
) == tcc_comparison
)
429 && tree_swap_operands_p (op0
, op1
, false))
434 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
435 code
= swap_tree_comparison (code
);
440 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
441 code
, type
, op0
, op1
))
443 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
449 gimple_simplify (enum tree_code code
, tree type
,
450 tree op0
, tree op1
, tree op2
,
451 gimple_seq
*seq
, tree (*valueize
)(tree
))
453 if (constant_for_folding (op0
) && constant_for_folding (op1
)
454 && constant_for_folding (op2
))
456 tree res
= fold_ternary
/*_to_constant */ (code
, type
, op0
, op1
, op2
);
458 && CONSTANT_CLASS_P (res
))
462 /* Canonicalize operand order both for matching and fallback stmt
464 if (commutative_ternary_tree_code (code
)
465 && tree_swap_operands_p (op0
, op1
, false))
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 decl
= builtin_decl_implicit (fn
);
492 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, &arg0
, 1, false);
495 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
497 res
= fold_convert (type
, res
);
498 if (CONSTANT_CLASS_P (res
))
506 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
509 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
512 /* Builtin function with two arguments. */
515 gimple_simplify (enum built_in_function fn
, tree type
,
516 tree arg0
, tree arg1
,
517 gimple_seq
*seq
, tree (*valueize
)(tree
))
519 if (constant_for_folding (arg0
)
520 && constant_for_folding (arg1
))
522 tree decl
= builtin_decl_implicit (fn
);
528 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, args
, 2, false);
531 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
533 res
= fold_convert (type
, res
);
534 if (CONSTANT_CLASS_P (res
))
542 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
543 fn
, type
, arg0
, arg1
))
545 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
548 /* Builtin function with three arguments. */
551 gimple_simplify (enum built_in_function fn
, tree type
,
552 tree arg0
, tree arg1
, tree arg2
,
553 gimple_seq
*seq
, tree (*valueize
)(tree
))
555 if (constant_for_folding (arg0
)
556 && constant_for_folding (arg1
)
557 && constant_for_folding (arg2
))
559 tree decl
= builtin_decl_implicit (fn
);
566 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, args
, 3, false);
569 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
571 res
= fold_convert (type
, res
);
572 if (CONSTANT_CLASS_P (res
))
580 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
581 fn
, type
, arg0
, arg1
, arg2
))
583 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
587 /* The main STMT based simplification entry. It is used by the fold_stmt
588 and the fold_stmt_to_constant APIs. */
591 gimple_simplify (gimple stmt
,
592 code_helper
*rcode
, tree
*ops
,
593 gimple_seq
*seq
, tree (*valueize
)(tree
))
595 switch (gimple_code (stmt
))
599 enum tree_code code
= gimple_assign_rhs_code (stmt
);
600 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
601 switch (gimple_assign_rhs_class (stmt
))
603 case GIMPLE_SINGLE_RHS
:
604 if (code
== REALPART_EXPR
605 || code
== IMAGPART_EXPR
606 || code
== VIEW_CONVERT_EXPR
)
608 tree op0
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
609 if (valueize
&& TREE_CODE (op0
) == SSA_NAME
)
611 tree tem
= valueize (op0
);
617 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
619 else if (code
== BIT_FIELD_REF
)
621 tree rhs1
= gimple_assign_rhs1 (stmt
);
622 tree op0
= TREE_OPERAND (rhs1
, 0);
623 if (valueize
&& TREE_CODE (op0
) == SSA_NAME
)
625 tree tem
= valueize (op0
);
631 ops
[1] = TREE_OPERAND (rhs1
, 1);
632 ops
[2] = TREE_OPERAND (rhs1
, 2);
633 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
635 else if (code
== SSA_NAME
638 tree op0
= gimple_assign_rhs1 (stmt
);
639 tree valueized
= valueize (op0
);
640 if (!valueized
|| op0
== valueized
)
643 *rcode
= TREE_CODE (op0
);
647 case GIMPLE_UNARY_RHS
:
649 tree rhs1
= gimple_assign_rhs1 (stmt
);
650 if (valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
652 tree tem
= valueize (rhs1
);
658 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
660 case GIMPLE_BINARY_RHS
:
662 tree rhs1
= gimple_assign_rhs1 (stmt
);
663 if (valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
665 tree tem
= valueize (rhs1
);
669 tree rhs2
= gimple_assign_rhs2 (stmt
);
670 if (valueize
&& TREE_CODE (rhs2
) == SSA_NAME
)
672 tree tem
= valueize (rhs2
);
679 return gimple_resimplify2 (seq
, rcode
, type
, ops
, valueize
);
681 case GIMPLE_TERNARY_RHS
:
683 tree rhs1
= gimple_assign_rhs1 (stmt
);
684 if (valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
686 tree tem
= valueize (rhs1
);
690 tree rhs2
= gimple_assign_rhs2 (stmt
);
691 if (valueize
&& TREE_CODE (rhs2
) == SSA_NAME
)
693 tree tem
= valueize (rhs2
);
697 tree rhs3
= gimple_assign_rhs3 (stmt
);
698 if (valueize
&& TREE_CODE (rhs3
) == SSA_NAME
)
700 tree tem
= valueize (rhs3
);
708 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
717 /* ??? This way we can't simplify calls with side-effects. */
718 if (gimple_call_lhs (stmt
) != NULL_TREE
)
720 tree fn
= gimple_call_fn (stmt
);
721 /* ??? Internal function support missing. */
724 if (valueize
&& TREE_CODE (fn
) == SSA_NAME
)
726 tree tem
= valueize (fn
);
731 || TREE_CODE (fn
) != ADDR_EXPR
732 || TREE_CODE (TREE_OPERAND (fn
, 0)) != FUNCTION_DECL
733 || DECL_BUILT_IN_CLASS (TREE_OPERAND (fn
, 0)) != BUILT_IN_NORMAL
734 || !builtin_decl_implicit (DECL_FUNCTION_CODE (TREE_OPERAND (fn
, 0)))
735 || !gimple_builtin_call_types_compatible_p (stmt
,
736 TREE_OPERAND (fn
, 0)))
739 tree decl
= TREE_OPERAND (fn
, 0);
740 tree type
= TREE_TYPE (gimple_call_lhs (stmt
));
741 switch (gimple_call_num_args (stmt
))
745 tree arg1
= gimple_call_arg (stmt
, 0);
746 if (valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
748 tree tem
= valueize (arg1
);
752 *rcode
= DECL_FUNCTION_CODE (decl
);
754 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
758 tree arg1
= gimple_call_arg (stmt
, 0);
759 if (valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
761 tree tem
= valueize (arg1
);
765 tree arg2
= gimple_call_arg (stmt
, 1);
766 if (valueize
&& TREE_CODE (arg2
) == SSA_NAME
)
768 tree tem
= valueize (arg2
);
772 *rcode
= DECL_FUNCTION_CODE (decl
);
775 return gimple_resimplify2 (seq
, rcode
, type
, ops
, valueize
);
779 tree arg1
= gimple_call_arg (stmt
, 0);
780 if (valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
782 tree tem
= valueize (arg1
);
786 tree arg2
= gimple_call_arg (stmt
, 1);
787 if (valueize
&& TREE_CODE (arg2
) == SSA_NAME
)
789 tree tem
= valueize (arg2
);
793 tree arg3
= gimple_call_arg (stmt
, 2);
794 if (valueize
&& TREE_CODE (arg3
) == SSA_NAME
)
796 tree tem
= valueize (arg3
);
800 *rcode
= DECL_FUNCTION_CODE (decl
);
804 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
814 tree lhs
= gimple_cond_lhs (stmt
);
815 if (valueize
&& TREE_CODE (lhs
) == SSA_NAME
)
817 tree tem
= valueize (lhs
);
821 tree rhs
= gimple_cond_rhs (stmt
);
822 if (valueize
&& TREE_CODE (rhs
) == SSA_NAME
)
824 tree tem
= valueize (rhs
);
828 *rcode
= gimple_cond_code (stmt
);
831 return gimple_resimplify2 (seq
, rcode
, boolean_type_node
, ops
, valueize
);
842 /* Helper for the autogenerated code, valueize OP. */
845 do_valueize (tree (*valueize
)(tree
), tree op
)
847 if (valueize
&& TREE_CODE (op
) == SSA_NAME
)
848 return valueize (op
);