1 /* Preamble and helpers for the autogenerated gimple-match.c file.
2 Copyright (C) 2014 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"
24 #include "stringpool.h"
25 #include "stor-layout.h"
28 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "tree-ssa-alias.h"
32 #include "internal-fn.h"
33 #include "gimple-expr.h"
36 #include "gimple-ssa.h"
37 #include "tree-ssanames.h"
38 #include "gimple-fold.h"
39 #include "gimple-iterator.h"
43 #include "tree-phinodes.h"
44 #include "ssa-iterators.h"
46 #include "gimple-match.h"
49 /* Forward declarations of the private auto-generated matchers.
50 They expect valueized operands in canonical order and do not
51 perform simplification of all-constant operands. */
52 static bool gimple_simplify (code_helper
*, tree
*,
53 gimple_seq
*, tree (*)(tree
),
54 code_helper
, tree
, tree
);
55 static bool gimple_simplify (code_helper
*, tree
*,
56 gimple_seq
*, tree (*)(tree
),
57 code_helper
, tree
, tree
, tree
);
58 static bool gimple_simplify (code_helper
*, tree
*,
59 gimple_seq
*, tree (*)(tree
),
60 code_helper
, tree
, tree
, tree
, tree
);
63 /* Return whether T is a constant that we'll dispatch to fold to
64 evaluate fully constant expressions. */
67 constant_for_folding (tree t
)
69 return (CONSTANT_CLASS_P (t
)
70 /* The following is only interesting to string builtins. */
71 || (TREE_CODE (t
) == ADDR_EXPR
72 && TREE_CODE (TREE_OPERAND (t
, 0)) == STRING_CST
));
76 /* Helper that matches and simplifies the toplevel result from
77 a gimple_simplify run (where we don't want to build
78 a stmt in case it's used in in-place folding). Replaces
79 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
80 result and returns whether any change was made. */
83 gimple_resimplify1 (gimple_seq
*seq
,
84 code_helper
*res_code
, tree type
, tree
*res_ops
,
85 tree (*valueize
)(tree
))
87 if (constant_for_folding (res_ops
[0]))
90 if (res_code
->is_tree_code ())
91 tem
= fold_unary_to_constant (*res_code
, type
, res_ops
[0]);
94 tree decl
= builtin_decl_implicit (*res_code
);
97 tem
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, res_ops
, 1, false);
100 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
102 tem
= fold_convert (type
, tem
);
107 && CONSTANT_CLASS_P (tem
))
110 res_ops
[1] = NULL_TREE
;
111 res_ops
[2] = NULL_TREE
;
112 *res_code
= TREE_CODE (res_ops
[0]);
117 code_helper res_code2
;
118 tree res_ops2
[3] = {};
119 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
120 *res_code
, type
, res_ops
[0]))
122 *res_code
= res_code2
;
123 res_ops
[0] = res_ops2
[0];
124 res_ops
[1] = res_ops2
[1];
125 res_ops
[2] = res_ops2
[2];
132 /* Helper that matches and simplifies the toplevel result from
133 a gimple_simplify run (where we don't want to build
134 a stmt in case it's used in in-place folding). Replaces
135 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
136 result and returns whether any change was made. */
139 gimple_resimplify2 (gimple_seq
*seq
,
140 code_helper
*res_code
, tree type
, tree
*res_ops
,
141 tree (*valueize
)(tree
))
143 if (constant_for_folding (res_ops
[0]) && constant_for_folding (res_ops
[1]))
145 tree tem
= NULL_TREE
;
146 if (res_code
->is_tree_code ())
147 tem
= fold_binary_to_constant (*res_code
, type
,
148 res_ops
[0], res_ops
[1]);
151 tree decl
= builtin_decl_implicit (*res_code
);
154 tem
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, res_ops
, 2, false);
157 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
159 tem
= fold_convert (type
, tem
);
164 && CONSTANT_CLASS_P (tem
))
167 res_ops
[1] = NULL_TREE
;
168 res_ops
[2] = NULL_TREE
;
169 *res_code
= TREE_CODE (res_ops
[0]);
174 /* Canonicalize operand order. */
175 bool canonicalized
= false;
176 if (res_code
->is_tree_code ()
177 && (TREE_CODE_CLASS ((enum tree_code
) *res_code
) == tcc_comparison
178 || commutative_tree_code (*res_code
))
179 && tree_swap_operands_p (res_ops
[0], res_ops
[1], false))
181 tree tem
= res_ops
[0];
182 res_ops
[0] = res_ops
[1];
184 if (TREE_CODE_CLASS ((enum tree_code
) *res_code
) == tcc_comparison
)
185 *res_code
= swap_tree_comparison (*res_code
);
186 canonicalized
= true;
189 code_helper res_code2
;
190 tree res_ops2
[3] = {};
191 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
192 *res_code
, type
, res_ops
[0], res_ops
[1]))
194 *res_code
= res_code2
;
195 res_ops
[0] = res_ops2
[0];
196 res_ops
[1] = res_ops2
[1];
197 res_ops
[2] = res_ops2
[2];
201 return canonicalized
;
204 /* Helper that matches and simplifies the toplevel result from
205 a gimple_simplify run (where we don't want to build
206 a stmt in case it's used in in-place folding). Replaces
207 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
208 result and returns whether any change was made. */
211 gimple_resimplify3 (gimple_seq
*seq
,
212 code_helper
*res_code
, tree type
, tree
*res_ops
,
213 tree (*valueize
)(tree
))
215 if (constant_for_folding (res_ops
[0]) && constant_for_folding (res_ops
[1])
216 && constant_for_folding (res_ops
[2]))
218 tree tem
= NULL_TREE
;
219 if (res_code
->is_tree_code ())
220 tem
= fold_ternary
/*_to_constant*/ (*res_code
, type
, res_ops
[0],
221 res_ops
[1], res_ops
[2]);
224 tree decl
= builtin_decl_implicit (*res_code
);
227 tem
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, res_ops
, 3, false);
230 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
232 tem
= fold_convert (type
, tem
);
237 && CONSTANT_CLASS_P (tem
))
240 res_ops
[1] = NULL_TREE
;
241 res_ops
[2] = NULL_TREE
;
242 *res_code
= TREE_CODE (res_ops
[0]);
247 /* Canonicalize operand order. */
248 bool canonicalized
= false;
249 if (res_code
->is_tree_code ()
250 && commutative_ternary_tree_code (*res_code
)
251 && tree_swap_operands_p (res_ops
[0], res_ops
[1], false))
253 tree tem
= res_ops
[0];
254 res_ops
[0] = res_ops
[1];
256 canonicalized
= true;
259 code_helper res_code2
;
260 tree res_ops2
[3] = {};
261 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
263 res_ops
[0], res_ops
[1], res_ops
[2]))
265 *res_code
= res_code2
;
266 res_ops
[0] = res_ops2
[0];
267 res_ops
[1] = res_ops2
[1];
268 res_ops
[2] = res_ops2
[2];
272 return canonicalized
;
276 /* If in GIMPLE expressions with CODE go as single-rhs build
277 a GENERIC tree for that expression into *OP0. */
280 maybe_build_generic_op (enum tree_code code
, tree type
,
281 tree
*op0
, tree op1
, tree op2
)
287 case VIEW_CONVERT_EXPR
:
288 *op0
= build1 (code
, type
, *op0
);
291 *op0
= build3 (code
, type
, *op0
, op1
, op2
);
297 /* Push the exploded expression described by RCODE, TYPE and OPS
298 as a statement to SEQ if necessary and return a gimple value
299 denoting the value of the expression. If RES is not NULL
300 then the result will be always RES and even gimple values are
304 maybe_push_res_to_seq (code_helper rcode
, tree type
, tree
*ops
,
305 gimple_seq
*seq
, tree res
)
307 if (rcode
.is_tree_code ())
310 && (TREE_CODE_LENGTH ((tree_code
) rcode
) == 0
311 || ((tree_code
) rcode
) == ADDR_EXPR
)
312 && is_gimple_val (ops
[0]))
316 /* Play safe and do not allow abnormals to be mentioned in
317 newly created statements. */
318 if ((TREE_CODE (ops
[0]) == SSA_NAME
319 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[0]))
321 && TREE_CODE (ops
[1]) == SSA_NAME
322 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[1]))
324 && TREE_CODE (ops
[2]) == SSA_NAME
325 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[2])))
328 res
= make_ssa_name (type
, NULL
);
329 maybe_build_generic_op (rcode
, type
, &ops
[0], ops
[1], ops
[2]);
330 gimple new_stmt
= gimple_build_assign_with_ops (rcode
, res
,
331 ops
[0], ops
[1], ops
[2]);
332 gimple_seq_add_stmt_without_update (seq
, new_stmt
);
339 tree decl
= builtin_decl_implicit (rcode
);
342 unsigned nargs
= type_num_arguments (TREE_TYPE (decl
));
343 gcc_assert (nargs
<= 3);
344 /* Play safe and do not allow abnormals to be mentioned in
345 newly created statements. */
346 if ((TREE_CODE (ops
[0]) == SSA_NAME
347 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[0]))
349 && TREE_CODE (ops
[1]) == SSA_NAME
350 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[1]))
352 && TREE_CODE (ops
[2]) == SSA_NAME
353 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[2])))
356 res
= make_ssa_name (type
, NULL
);
357 gimple new_stmt
= gimple_build_call (decl
, nargs
, ops
[0], ops
[1], ops
[2]);
358 gimple_call_set_lhs (new_stmt
, res
);
359 gimple_seq_add_stmt_without_update (seq
, new_stmt
);
365 /* Public API overloads follow for operation being tree_code or
366 built_in_function and for one to three operands or arguments.
367 They return NULL_TREE if nothing could be simplified or
368 the resulting simplified value with parts pushed to SEQ.
369 If SEQ is NULL then if the simplification needs to create
370 new stmts it will fail. If VALUEIZE is non-NULL then all
371 SSA names will be valueized using that hook prior to
372 applying simplifications. */
377 gimple_simplify (enum tree_code code
, tree type
,
379 gimple_seq
*seq
, tree (*valueize
)(tree
))
381 if (constant_for_folding (op0
))
383 tree res
= fold_unary_to_constant (code
, type
, op0
);
385 && CONSTANT_CLASS_P (res
))
391 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
394 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
400 gimple_simplify (enum tree_code code
, tree type
,
402 gimple_seq
*seq
, tree (*valueize
)(tree
))
404 if (constant_for_folding (op0
) && constant_for_folding (op1
))
406 tree res
= fold_binary_to_constant (code
, type
, op0
, op1
);
408 && CONSTANT_CLASS_P (res
))
412 /* Canonicalize operand order both for matching and fallback stmt
414 if ((commutative_tree_code (code
)
415 || TREE_CODE_CLASS (code
) == tcc_comparison
)
416 && tree_swap_operands_p (op0
, op1
, false))
421 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
422 code
= swap_tree_comparison (code
);
427 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
428 code
, type
, op0
, op1
))
430 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
436 gimple_simplify (enum tree_code code
, tree type
,
437 tree op0
, tree op1
, tree op2
,
438 gimple_seq
*seq
, tree (*valueize
)(tree
))
440 if (constant_for_folding (op0
) && constant_for_folding (op1
)
441 && constant_for_folding (op2
))
443 tree res
= fold_ternary
/*_to_constant */ (code
, type
, op0
, op1
, op2
);
445 && CONSTANT_CLASS_P (res
))
449 /* Canonicalize operand order both for matching and fallback stmt
451 if (commutative_ternary_tree_code (code
)
452 && tree_swap_operands_p (op0
, op1
, false))
461 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
462 code
, type
, op0
, op1
, op2
))
464 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
467 /* Builtin function with one argument. */
470 gimple_simplify (enum built_in_function fn
, tree type
,
472 gimple_seq
*seq
, tree (*valueize
)(tree
))
474 if (constant_for_folding (arg0
))
476 tree decl
= builtin_decl_implicit (fn
);
479 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, &arg0
, 1, false);
482 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
484 res
= fold_convert (type
, res
);
485 if (CONSTANT_CLASS_P (res
))
493 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
496 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
499 /* Builtin function with two arguments. */
502 gimple_simplify (enum built_in_function fn
, tree type
,
503 tree arg0
, tree arg1
,
504 gimple_seq
*seq
, tree (*valueize
)(tree
))
506 if (constant_for_folding (arg0
)
507 && constant_for_folding (arg1
))
509 tree decl
= builtin_decl_implicit (fn
);
515 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, args
, 2, false);
518 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
520 res
= fold_convert (type
, res
);
521 if (CONSTANT_CLASS_P (res
))
529 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
530 fn
, type
, arg0
, arg1
))
532 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
535 /* Builtin function with three arguments. */
538 gimple_simplify (enum built_in_function fn
, tree type
,
539 tree arg0
, tree arg1
, tree arg2
,
540 gimple_seq
*seq
, tree (*valueize
)(tree
))
542 if (constant_for_folding (arg0
)
543 && constant_for_folding (arg1
)
544 && constant_for_folding (arg2
))
546 tree decl
= builtin_decl_implicit (fn
);
553 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, args
, 3, false);
556 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
558 res
= fold_convert (type
, res
);
559 if (CONSTANT_CLASS_P (res
))
567 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
568 fn
, type
, arg0
, arg1
, arg2
))
570 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
574 /* The main STMT based simplification entry. It is used by the fold_stmt
575 and the fold_stmt_to_constant APIs. */
578 gimple_simplify (gimple stmt
,
579 code_helper
*rcode
, tree
*ops
,
580 gimple_seq
*seq
, tree (*valueize
)(tree
))
582 switch (gimple_code (stmt
))
586 enum tree_code code
= gimple_assign_rhs_code (stmt
);
587 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
588 switch (gimple_assign_rhs_class (stmt
))
590 case GIMPLE_SINGLE_RHS
:
591 if (code
== REALPART_EXPR
592 || code
== IMAGPART_EXPR
593 || code
== VIEW_CONVERT_EXPR
)
595 tree op0
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
596 if (valueize
&& TREE_CODE (op0
) == SSA_NAME
)
598 tree tem
= valueize (op0
);
604 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
606 else if (code
== BIT_FIELD_REF
)
608 tree rhs1
= gimple_assign_rhs1 (stmt
);
609 tree op0
= TREE_OPERAND (rhs1
, 0);
610 if (valueize
&& TREE_CODE (op0
) == SSA_NAME
)
612 tree tem
= valueize (op0
);
618 ops
[1] = TREE_OPERAND (rhs1
, 1);
619 ops
[2] = TREE_OPERAND (rhs1
, 2);
620 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
622 else if (code
== SSA_NAME
625 tree op0
= gimple_assign_rhs1 (stmt
);
626 tree valueized
= valueize (op0
);
627 if (!valueized
|| op0
== valueized
)
630 *rcode
= TREE_CODE (op0
);
634 case GIMPLE_UNARY_RHS
:
636 tree rhs1
= gimple_assign_rhs1 (stmt
);
637 if (valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
639 tree tem
= valueize (rhs1
);
645 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
647 case GIMPLE_BINARY_RHS
:
649 tree rhs1
= gimple_assign_rhs1 (stmt
);
650 if (valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
652 tree tem
= valueize (rhs1
);
656 tree rhs2
= gimple_assign_rhs2 (stmt
);
657 if (valueize
&& TREE_CODE (rhs2
) == SSA_NAME
)
659 tree tem
= valueize (rhs2
);
666 return gimple_resimplify2 (seq
, rcode
, type
, ops
, valueize
);
668 case GIMPLE_TERNARY_RHS
:
670 tree rhs1
= gimple_assign_rhs1 (stmt
);
671 if (valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
673 tree tem
= valueize (rhs1
);
677 tree rhs2
= gimple_assign_rhs2 (stmt
);
678 if (valueize
&& TREE_CODE (rhs2
) == SSA_NAME
)
680 tree tem
= valueize (rhs2
);
684 tree rhs3
= gimple_assign_rhs3 (stmt
);
685 if (valueize
&& TREE_CODE (rhs3
) == SSA_NAME
)
687 tree tem
= valueize (rhs3
);
695 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
704 /* ??? This way we can't simplify calls with side-effects. */
705 if (gimple_call_lhs (stmt
) != NULL_TREE
)
707 tree fn
= gimple_call_fn (stmt
);
708 /* ??? Internal function support missing. */
711 if (valueize
&& TREE_CODE (fn
) == SSA_NAME
)
713 tree tem
= valueize (fn
);
718 || TREE_CODE (fn
) != ADDR_EXPR
719 || TREE_CODE (TREE_OPERAND (fn
, 0)) != FUNCTION_DECL
720 || DECL_BUILT_IN_CLASS (TREE_OPERAND (fn
, 0)) != BUILT_IN_NORMAL
721 || !builtin_decl_implicit (DECL_FUNCTION_CODE (TREE_OPERAND (fn
, 0)))
722 || !gimple_builtin_call_types_compatible_p (stmt
,
723 TREE_OPERAND (fn
, 0)))
726 tree decl
= TREE_OPERAND (fn
, 0);
727 tree type
= TREE_TYPE (gimple_call_lhs (stmt
));
728 switch (gimple_call_num_args (stmt
))
732 tree arg1
= gimple_call_arg (stmt
, 0);
733 if (valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
735 tree tem
= valueize (arg1
);
739 *rcode
= DECL_FUNCTION_CODE (decl
);
741 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
745 tree arg1
= gimple_call_arg (stmt
, 0);
746 if (valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
748 tree tem
= valueize (arg1
);
752 tree arg2
= gimple_call_arg (stmt
, 1);
753 if (valueize
&& TREE_CODE (arg2
) == SSA_NAME
)
755 tree tem
= valueize (arg2
);
759 *rcode
= DECL_FUNCTION_CODE (decl
);
762 return gimple_resimplify2 (seq
, rcode
, type
, ops
, valueize
);
766 tree arg1
= gimple_call_arg (stmt
, 0);
767 if (valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
769 tree tem
= valueize (arg1
);
773 tree arg2
= gimple_call_arg (stmt
, 1);
774 if (valueize
&& TREE_CODE (arg2
) == SSA_NAME
)
776 tree tem
= valueize (arg2
);
780 tree arg3
= gimple_call_arg (stmt
, 2);
781 if (valueize
&& TREE_CODE (arg3
) == SSA_NAME
)
783 tree tem
= valueize (arg3
);
787 *rcode
= DECL_FUNCTION_CODE (decl
);
791 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
801 tree lhs
= gimple_cond_lhs (stmt
);
802 if (valueize
&& TREE_CODE (lhs
) == SSA_NAME
)
804 tree tem
= valueize (lhs
);
808 tree rhs
= gimple_cond_rhs (stmt
);
809 if (valueize
&& TREE_CODE (rhs
) == SSA_NAME
)
811 tree tem
= valueize (rhs
);
815 *rcode
= gimple_cond_code (stmt
);
818 return gimple_resimplify2 (seq
, rcode
, boolean_type_node
, ops
, valueize
);
829 /* Helper for the autogenerated code, valueize OP. */
832 do_valueize (tree (*valueize
)(tree
), tree op
)
834 if (valueize
&& TREE_CODE (op
) == SSA_NAME
)
835 return valueize (op
);