]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/gimple-match-head.c
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"
28 #include "fold-const.h"
29 #include "stringpool.h"
30 #include "stor-layout.h"
32 #include "hard-reg-set.h"
35 #include "basic-block.h"
36 #include "tree-ssa-alias.h"
37 #include "internal-fn.h"
38 #include "gimple-expr.h"
40 #include "gimple-ssa.h"
41 #include "tree-ssanames.h"
42 #include "gimple-fold.h"
43 #include "gimple-iterator.h"
45 #include "insn-config.h"
56 #include "tree-phinodes.h"
57 #include "ssa-iterators.h"
59 #include "gimple-match.h"
62 /* Forward declarations of the private auto-generated matchers.
63 They expect valueized operands in canonical order and do not
64 perform simplification of all-constant operands. */
65 static bool gimple_simplify (code_helper
*, tree
*,
66 gimple_seq
*, tree (*)(tree
),
67 code_helper
, tree
, tree
);
68 static bool gimple_simplify (code_helper
*, tree
*,
69 gimple_seq
*, tree (*)(tree
),
70 code_helper
, tree
, tree
, tree
);
71 static bool gimple_simplify (code_helper
*, tree
*,
72 gimple_seq
*, tree (*)(tree
),
73 code_helper
, tree
, tree
, tree
, tree
);
76 /* Return whether T is a constant that we'll dispatch to fold to
77 evaluate fully constant expressions. */
80 constant_for_folding (tree t
)
82 return (CONSTANT_CLASS_P (t
)
83 /* The following is only interesting to string builtins. */
84 || (TREE_CODE (t
) == ADDR_EXPR
85 && TREE_CODE (TREE_OPERAND (t
, 0)) == STRING_CST
));
89 /* Helper that matches and simplifies the toplevel result from
90 a gimple_simplify run (where we don't want to build
91 a stmt in case it's used in in-place folding). Replaces
92 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
93 result and returns whether any change was made. */
96 gimple_resimplify1 (gimple_seq
*seq
,
97 code_helper
*res_code
, tree type
, tree
*res_ops
,
98 tree (*valueize
)(tree
))
100 if (constant_for_folding (res_ops
[0]))
102 tree tem
= NULL_TREE
;
103 if (res_code
->is_tree_code ())
104 tem
= const_unop (*res_code
, type
, res_ops
[0]);
107 tree decl
= builtin_decl_implicit (*res_code
);
110 tem
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, res_ops
, 1, false);
113 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
115 tem
= fold_convert (type
, tem
);
120 && CONSTANT_CLASS_P (tem
))
123 res_ops
[1] = NULL_TREE
;
124 res_ops
[2] = NULL_TREE
;
125 *res_code
= TREE_CODE (res_ops
[0]);
130 code_helper res_code2
;
131 tree res_ops2
[3] = {};
132 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
133 *res_code
, type
, res_ops
[0]))
135 *res_code
= res_code2
;
136 res_ops
[0] = res_ops2
[0];
137 res_ops
[1] = res_ops2
[1];
138 res_ops
[2] = res_ops2
[2];
145 /* Helper that matches and simplifies the toplevel result from
146 a gimple_simplify run (where we don't want to build
147 a stmt in case it's used in in-place folding). Replaces
148 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
149 result and returns whether any change was made. */
152 gimple_resimplify2 (gimple_seq
*seq
,
153 code_helper
*res_code
, tree type
, tree
*res_ops
,
154 tree (*valueize
)(tree
))
156 if (constant_for_folding (res_ops
[0]) && constant_for_folding (res_ops
[1]))
158 tree tem
= NULL_TREE
;
159 if (res_code
->is_tree_code ())
160 tem
= const_binop (*res_code
, type
, res_ops
[0], res_ops
[1]);
163 tree decl
= builtin_decl_implicit (*res_code
);
166 tem
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, res_ops
, 2, false);
169 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
171 tem
= fold_convert (type
, tem
);
176 && CONSTANT_CLASS_P (tem
))
179 res_ops
[1] = NULL_TREE
;
180 res_ops
[2] = NULL_TREE
;
181 *res_code
= TREE_CODE (res_ops
[0]);
186 /* Canonicalize operand order. */
187 bool canonicalized
= false;
188 if (res_code
->is_tree_code ()
189 && (TREE_CODE_CLASS ((enum tree_code
) *res_code
) == tcc_comparison
190 || commutative_tree_code (*res_code
))
191 && tree_swap_operands_p (res_ops
[0], res_ops
[1], false))
193 tree tem
= res_ops
[0];
194 res_ops
[0] = res_ops
[1];
196 if (TREE_CODE_CLASS ((enum tree_code
) *res_code
) == tcc_comparison
)
197 *res_code
= swap_tree_comparison (*res_code
);
198 canonicalized
= true;
201 code_helper res_code2
;
202 tree res_ops2
[3] = {};
203 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
204 *res_code
, type
, res_ops
[0], res_ops
[1]))
206 *res_code
= res_code2
;
207 res_ops
[0] = res_ops2
[0];
208 res_ops
[1] = res_ops2
[1];
209 res_ops
[2] = res_ops2
[2];
213 return canonicalized
;
216 /* Helper that matches and simplifies the toplevel result from
217 a gimple_simplify run (where we don't want to build
218 a stmt in case it's used in in-place folding). Replaces
219 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
220 result and returns whether any change was made. */
223 gimple_resimplify3 (gimple_seq
*seq
,
224 code_helper
*res_code
, tree type
, tree
*res_ops
,
225 tree (*valueize
)(tree
))
227 if (constant_for_folding (res_ops
[0]) && constant_for_folding (res_ops
[1])
228 && constant_for_folding (res_ops
[2]))
230 tree tem
= NULL_TREE
;
231 if (res_code
->is_tree_code ())
232 tem
= fold_ternary
/*_to_constant*/ (*res_code
, type
, res_ops
[0],
233 res_ops
[1], res_ops
[2]);
236 tree decl
= builtin_decl_implicit (*res_code
);
239 tem
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, res_ops
, 3, false);
242 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
244 tem
= fold_convert (type
, tem
);
249 && CONSTANT_CLASS_P (tem
))
252 res_ops
[1] = NULL_TREE
;
253 res_ops
[2] = NULL_TREE
;
254 *res_code
= TREE_CODE (res_ops
[0]);
259 /* Canonicalize operand order. */
260 bool canonicalized
= false;
261 if (res_code
->is_tree_code ()
262 && commutative_ternary_tree_code (*res_code
)
263 && tree_swap_operands_p (res_ops
[0], res_ops
[1], false))
265 tree tem
= res_ops
[0];
266 res_ops
[0] = res_ops
[1];
268 canonicalized
= true;
271 code_helper res_code2
;
272 tree res_ops2
[3] = {};
273 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
275 res_ops
[0], res_ops
[1], res_ops
[2]))
277 *res_code
= res_code2
;
278 res_ops
[0] = res_ops2
[0];
279 res_ops
[1] = res_ops2
[1];
280 res_ops
[2] = res_ops2
[2];
284 return canonicalized
;
288 /* If in GIMPLE expressions with CODE go as single-rhs build
289 a GENERIC tree for that expression into *OP0. */
292 maybe_build_generic_op (enum tree_code code
, tree type
,
293 tree
*op0
, tree op1
, tree op2
)
299 case VIEW_CONVERT_EXPR
:
300 *op0
= build1 (code
, type
, *op0
);
303 *op0
= build3 (code
, type
, *op0
, op1
, op2
);
309 /* Push the exploded expression described by RCODE, TYPE and OPS
310 as a statement to SEQ if necessary and return a gimple value
311 denoting the value of the expression. If RES is not NULL
312 then the result will be always RES and even gimple values are
316 maybe_push_res_to_seq (code_helper rcode
, tree type
, tree
*ops
,
317 gimple_seq
*seq
, tree res
)
319 if (rcode
.is_tree_code ())
322 && (TREE_CODE_LENGTH ((tree_code
) rcode
) == 0
323 || ((tree_code
) rcode
) == ADDR_EXPR
)
324 && is_gimple_val (ops
[0]))
328 /* Play safe and do not allow abnormals to be mentioned in
329 newly created statements. */
330 if ((TREE_CODE (ops
[0]) == SSA_NAME
331 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[0]))
333 && TREE_CODE (ops
[1]) == SSA_NAME
334 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[1]))
336 && TREE_CODE (ops
[2]) == SSA_NAME
337 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[2])))
340 res
= make_ssa_name (type
);
341 maybe_build_generic_op (rcode
, type
, &ops
[0], ops
[1], ops
[2]);
342 gimple new_stmt
= gimple_build_assign (res
, rcode
,
343 ops
[0], ops
[1], ops
[2]);
344 gimple_seq_add_stmt_without_update (seq
, new_stmt
);
351 tree decl
= builtin_decl_implicit (rcode
);
354 unsigned nargs
= type_num_arguments (TREE_TYPE (decl
));
355 gcc_assert (nargs
<= 3);
356 /* Play safe and do not allow abnormals to be mentioned in
357 newly created statements. */
358 if ((TREE_CODE (ops
[0]) == SSA_NAME
359 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[0]))
361 && TREE_CODE (ops
[1]) == SSA_NAME
362 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[1]))
364 && TREE_CODE (ops
[2]) == SSA_NAME
365 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[2])))
368 res
= make_ssa_name (type
);
369 gimple new_stmt
= gimple_build_call (decl
, nargs
, ops
[0], ops
[1], ops
[2]);
370 gimple_call_set_lhs (new_stmt
, res
);
371 gimple_seq_add_stmt_without_update (seq
, new_stmt
);
377 /* Public API overloads follow for operation being tree_code or
378 built_in_function and for one to three operands or arguments.
379 They return NULL_TREE if nothing could be simplified or
380 the resulting simplified value with parts pushed to SEQ.
381 If SEQ is NULL then if the simplification needs to create
382 new stmts it will fail. If VALUEIZE is non-NULL then all
383 SSA names will be valueized using that hook prior to
384 applying simplifications. */
389 gimple_simplify (enum tree_code code
, tree type
,
391 gimple_seq
*seq
, tree (*valueize
)(tree
))
393 if (constant_for_folding (op0
))
395 tree res
= const_unop (code
, type
, op0
);
397 && CONSTANT_CLASS_P (res
))
403 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
406 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
412 gimple_simplify (enum tree_code code
, tree type
,
414 gimple_seq
*seq
, tree (*valueize
)(tree
))
416 if (constant_for_folding (op0
) && constant_for_folding (op1
))
418 tree res
= const_binop (code
, type
, op0
, op1
);
420 && CONSTANT_CLASS_P (res
))
424 /* Canonicalize operand order both for matching and fallback stmt
426 if ((commutative_tree_code (code
)
427 || TREE_CODE_CLASS (code
) == tcc_comparison
)
428 && tree_swap_operands_p (op0
, op1
, false))
433 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
434 code
= swap_tree_comparison (code
);
439 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
440 code
, type
, op0
, op1
))
442 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
448 gimple_simplify (enum tree_code code
, tree type
,
449 tree op0
, tree op1
, tree op2
,
450 gimple_seq
*seq
, tree (*valueize
)(tree
))
452 if (constant_for_folding (op0
) && constant_for_folding (op1
)
453 && constant_for_folding (op2
))
455 tree res
= fold_ternary
/*_to_constant */ (code
, type
, op0
, op1
, op2
);
457 && CONSTANT_CLASS_P (res
))
461 /* Canonicalize operand order both for matching and fallback stmt
463 if (commutative_ternary_tree_code (code
)
464 && tree_swap_operands_p (op0
, op1
, false))
473 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
474 code
, type
, op0
, op1
, op2
))
476 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
479 /* Builtin function with one argument. */
482 gimple_simplify (enum built_in_function fn
, tree type
,
484 gimple_seq
*seq
, tree (*valueize
)(tree
))
486 if (constant_for_folding (arg0
))
488 tree decl
= builtin_decl_implicit (fn
);
491 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, &arg0
, 1, false);
494 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
496 res
= fold_convert (type
, res
);
497 if (CONSTANT_CLASS_P (res
))
505 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
508 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
511 /* Builtin function with two arguments. */
514 gimple_simplify (enum built_in_function fn
, tree type
,
515 tree arg0
, tree arg1
,
516 gimple_seq
*seq
, tree (*valueize
)(tree
))
518 if (constant_for_folding (arg0
)
519 && constant_for_folding (arg1
))
521 tree decl
= builtin_decl_implicit (fn
);
527 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, args
, 2, false);
530 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
532 res
= fold_convert (type
, res
);
533 if (CONSTANT_CLASS_P (res
))
541 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
542 fn
, type
, arg0
, arg1
))
544 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
547 /* Builtin function with three arguments. */
550 gimple_simplify (enum built_in_function fn
, tree type
,
551 tree arg0
, tree arg1
, tree arg2
,
552 gimple_seq
*seq
, tree (*valueize
)(tree
))
554 if (constant_for_folding (arg0
)
555 && constant_for_folding (arg1
)
556 && constant_for_folding (arg2
))
558 tree decl
= builtin_decl_implicit (fn
);
565 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, args
, 3, false);
568 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
570 res
= fold_convert (type
, res
);
571 if (CONSTANT_CLASS_P (res
))
579 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
580 fn
, type
, arg0
, arg1
, arg2
))
582 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
586 /* The main STMT based simplification entry. It is used by the fold_stmt
587 and the fold_stmt_to_constant APIs. */
590 gimple_simplify (gimple stmt
,
591 code_helper
*rcode
, tree
*ops
,
593 tree (*valueize
)(tree
), tree (*top_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 (top_valueize
&& TREE_CODE (op0
) == SSA_NAME
)
611 tree tem
= top_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 (top_valueize
&& TREE_CODE (op0
) == SSA_NAME
)
625 tree tem
= top_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
= top_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 (top_valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
652 tree tem
= top_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 (top_valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
665 tree tem
= top_valueize (rhs1
);
669 tree rhs2
= gimple_assign_rhs2 (stmt
);
670 if (top_valueize
&& TREE_CODE (rhs2
) == SSA_NAME
)
672 tree tem
= top_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 (top_valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
686 tree tem
= top_valueize (rhs1
);
690 tree rhs2
= gimple_assign_rhs2 (stmt
);
691 if (top_valueize
&& TREE_CODE (rhs2
) == SSA_NAME
)
693 tree tem
= top_valueize (rhs2
);
697 tree rhs3
= gimple_assign_rhs3 (stmt
);
698 if (top_valueize
&& TREE_CODE (rhs3
) == SSA_NAME
)
700 tree tem
= top_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 (top_valueize
&& TREE_CODE (fn
) == SSA_NAME
)
726 tree tem
= top_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 (top_valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
748 tree tem
= top_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 (top_valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
761 tree tem
= top_valueize (arg1
);
765 tree arg2
= gimple_call_arg (stmt
, 1);
766 if (top_valueize
&& TREE_CODE (arg2
) == SSA_NAME
)
768 tree tem
= top_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 (top_valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
782 tree tem
= top_valueize (arg1
);
786 tree arg2
= gimple_call_arg (stmt
, 1);
787 if (top_valueize
&& TREE_CODE (arg2
) == SSA_NAME
)
789 tree tem
= top_valueize (arg2
);
793 tree arg3
= gimple_call_arg (stmt
, 2);
794 if (top_valueize
&& TREE_CODE (arg3
) == SSA_NAME
)
796 tree tem
= top_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 (top_valueize
&& TREE_CODE (lhs
) == SSA_NAME
)
817 tree tem
= top_valueize (lhs
);
821 tree rhs
= gimple_cond_rhs (stmt
);
822 if (top_valueize
&& TREE_CODE (rhs
) == SSA_NAME
)
824 tree tem
= top_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
);
852 /* Routine to determine if the types T1 and T2 are effectively
853 the same for GIMPLE. If T1 or T2 is not a type, the test
854 applies to their TREE_TYPE. */
857 types_match (tree t1
, tree t2
)
864 return types_compatible_p (t1
, t2
);
867 /* Return if T has a single use. For GIMPLE, we also allow any
868 non-SSA_NAME (ie constants) and zero uses to cope with uses
869 that aren't linked up yet. */
874 return TREE_CODE (t
) != SSA_NAME
|| has_zero_uses (t
) || has_single_use (t
);