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"
30 #include "fold-const.h"
31 #include "stor-layout.h"
33 #include "internal-fn.h"
34 #include "gimple-fold.h"
35 #include "gimple-iterator.h"
36 #include "insn-config.h"
49 #include "gimple-match.h"
52 /* Forward declarations of the private auto-generated matchers.
53 They expect valueized operands in canonical order and do not
54 perform simplification of all-constant operands. */
55 static bool gimple_simplify (code_helper
*, tree
*,
56 gimple_seq
*, tree (*)(tree
),
57 code_helper
, tree
, tree
);
58 static bool gimple_simplify (code_helper
*, tree
*,
59 gimple_seq
*, tree (*)(tree
),
60 code_helper
, tree
, tree
, tree
);
61 static bool gimple_simplify (code_helper
*, tree
*,
62 gimple_seq
*, tree (*)(tree
),
63 code_helper
, tree
, tree
, tree
, tree
);
66 /* Return whether T is a constant that we'll dispatch to fold to
67 evaluate fully constant expressions. */
70 constant_for_folding (tree t
)
72 return (CONSTANT_CLASS_P (t
)
73 /* The following is only interesting to string builtins. */
74 || (TREE_CODE (t
) == ADDR_EXPR
75 && TREE_CODE (TREE_OPERAND (t
, 0)) == STRING_CST
));
79 /* Helper that matches and simplifies the toplevel result from
80 a gimple_simplify run (where we don't want to build
81 a stmt in case it's used in in-place folding). Replaces
82 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
83 result and returns whether any change was made. */
86 gimple_resimplify1 (gimple_seq
*seq
,
87 code_helper
*res_code
, tree type
, tree
*res_ops
,
88 tree (*valueize
)(tree
))
90 if (constant_for_folding (res_ops
[0]))
93 if (res_code
->is_tree_code ())
94 tem
= const_unop (*res_code
, type
, res_ops
[0]);
97 tree decl
= builtin_decl_implicit (*res_code
);
100 tem
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, res_ops
, 1, false);
103 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
105 tem
= fold_convert (type
, tem
);
110 && CONSTANT_CLASS_P (tem
))
113 res_ops
[1] = NULL_TREE
;
114 res_ops
[2] = NULL_TREE
;
115 *res_code
= TREE_CODE (res_ops
[0]);
120 code_helper res_code2
;
121 tree res_ops2
[3] = {};
122 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
123 *res_code
, type
, res_ops
[0]))
125 *res_code
= res_code2
;
126 res_ops
[0] = res_ops2
[0];
127 res_ops
[1] = res_ops2
[1];
128 res_ops
[2] = res_ops2
[2];
135 /* Helper that matches and simplifies the toplevel result from
136 a gimple_simplify run (where we don't want to build
137 a stmt in case it's used in in-place folding). Replaces
138 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
139 result and returns whether any change was made. */
142 gimple_resimplify2 (gimple_seq
*seq
,
143 code_helper
*res_code
, tree type
, tree
*res_ops
,
144 tree (*valueize
)(tree
))
146 if (constant_for_folding (res_ops
[0]) && constant_for_folding (res_ops
[1]))
148 tree tem
= NULL_TREE
;
149 if (res_code
->is_tree_code ())
150 tem
= const_binop (*res_code
, type
, res_ops
[0], res_ops
[1]);
153 tree decl
= builtin_decl_implicit (*res_code
);
156 tem
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, res_ops
, 2, false);
159 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
161 tem
= fold_convert (type
, tem
);
166 && CONSTANT_CLASS_P (tem
))
169 res_ops
[1] = NULL_TREE
;
170 res_ops
[2] = NULL_TREE
;
171 *res_code
= TREE_CODE (res_ops
[0]);
176 /* Canonicalize operand order. */
177 bool canonicalized
= false;
178 if (res_code
->is_tree_code ()
179 && (TREE_CODE_CLASS ((enum tree_code
) *res_code
) == tcc_comparison
180 || commutative_tree_code (*res_code
))
181 && tree_swap_operands_p (res_ops
[0], res_ops
[1], false))
183 std::swap (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 std::swap (res_ops
[0], res_ops
[1]);
254 canonicalized
= true;
257 code_helper res_code2
;
258 tree res_ops2
[3] = {};
259 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
261 res_ops
[0], res_ops
[1], res_ops
[2]))
263 *res_code
= res_code2
;
264 res_ops
[0] = res_ops2
[0];
265 res_ops
[1] = res_ops2
[1];
266 res_ops
[2] = res_ops2
[2];
270 return canonicalized
;
274 /* If in GIMPLE expressions with CODE go as single-rhs build
275 a GENERIC tree for that expression into *OP0. */
278 maybe_build_generic_op (enum tree_code code
, tree type
,
279 tree
*op0
, tree op1
, tree op2
)
285 case VIEW_CONVERT_EXPR
:
286 *op0
= build1 (code
, type
, *op0
);
289 *op0
= build3 (code
, type
, *op0
, op1
, op2
);
295 /* Push the exploded expression described by RCODE, TYPE and OPS
296 as a statement to SEQ if necessary and return a gimple value
297 denoting the value of the expression. If RES is not NULL
298 then the result will be always RES and even gimple values are
302 maybe_push_res_to_seq (code_helper rcode
, tree type
, tree
*ops
,
303 gimple_seq
*seq
, tree res
)
305 if (rcode
.is_tree_code ())
308 && (TREE_CODE_LENGTH ((tree_code
) rcode
) == 0
309 || ((tree_code
) rcode
) == ADDR_EXPR
)
310 && is_gimple_val (ops
[0]))
314 /* Play safe and do not allow abnormals to be mentioned in
315 newly created statements. */
316 if ((TREE_CODE (ops
[0]) == SSA_NAME
317 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[0]))
319 && TREE_CODE (ops
[1]) == SSA_NAME
320 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[1]))
322 && TREE_CODE (ops
[2]) == SSA_NAME
323 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[2])))
326 res
= make_ssa_name (type
);
327 maybe_build_generic_op (rcode
, type
, &ops
[0], ops
[1], ops
[2]);
328 gimple new_stmt
= gimple_build_assign (res
, rcode
,
329 ops
[0], ops
[1], ops
[2]);
330 gimple_seq_add_stmt_without_update (seq
, new_stmt
);
337 tree decl
= builtin_decl_implicit (rcode
);
340 unsigned nargs
= type_num_arguments (TREE_TYPE (decl
));
341 gcc_assert (nargs
<= 3);
342 /* Play safe and do not allow abnormals to be mentioned in
343 newly created statements. */
344 if ((TREE_CODE (ops
[0]) == SSA_NAME
345 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[0]))
347 && TREE_CODE (ops
[1]) == SSA_NAME
348 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[1]))
350 && TREE_CODE (ops
[2]) == SSA_NAME
351 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[2])))
354 res
= make_ssa_name (type
);
355 gimple new_stmt
= gimple_build_call (decl
, nargs
, ops
[0], ops
[1], ops
[2]);
356 gimple_call_set_lhs (new_stmt
, res
);
357 gimple_seq_add_stmt_without_update (seq
, new_stmt
);
363 /* Public API overloads follow for operation being tree_code or
364 built_in_function and for one to three operands or arguments.
365 They return NULL_TREE if nothing could be simplified or
366 the resulting simplified value with parts pushed to SEQ.
367 If SEQ is NULL then if the simplification needs to create
368 new stmts it will fail. If VALUEIZE is non-NULL then all
369 SSA names will be valueized using that hook prior to
370 applying simplifications. */
375 gimple_simplify (enum tree_code code
, tree type
,
377 gimple_seq
*seq
, tree (*valueize
)(tree
))
379 if (constant_for_folding (op0
))
381 tree res
= const_unop (code
, type
, op0
);
383 && CONSTANT_CLASS_P (res
))
389 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
392 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
398 gimple_simplify (enum tree_code code
, tree type
,
400 gimple_seq
*seq
, tree (*valueize
)(tree
))
402 if (constant_for_folding (op0
) && constant_for_folding (op1
))
404 tree res
= const_binop (code
, type
, op0
, op1
);
406 && CONSTANT_CLASS_P (res
))
410 /* Canonicalize operand order both for matching and fallback stmt
412 if ((commutative_tree_code (code
)
413 || TREE_CODE_CLASS (code
) == tcc_comparison
)
414 && tree_swap_operands_p (op0
, op1
, false))
416 std::swap (op0
, op1
);
417 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
418 code
= swap_tree_comparison (code
);
423 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
424 code
, type
, op0
, op1
))
426 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
432 gimple_simplify (enum tree_code code
, tree type
,
433 tree op0
, tree op1
, tree op2
,
434 gimple_seq
*seq
, tree (*valueize
)(tree
))
436 if (constant_for_folding (op0
) && constant_for_folding (op1
)
437 && constant_for_folding (op2
))
439 tree res
= fold_ternary
/*_to_constant */ (code
, type
, op0
, op1
, op2
);
441 && CONSTANT_CLASS_P (res
))
445 /* Canonicalize operand order both for matching and fallback stmt
447 if (commutative_ternary_tree_code (code
)
448 && tree_swap_operands_p (op0
, op1
, false))
449 std::swap (op0
, op1
);
453 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
454 code
, type
, op0
, op1
, op2
))
456 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
459 /* Builtin function with one argument. */
462 gimple_simplify (enum built_in_function fn
, tree type
,
464 gimple_seq
*seq
, tree (*valueize
)(tree
))
466 if (constant_for_folding (arg0
))
468 tree decl
= builtin_decl_implicit (fn
);
471 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, &arg0
, 1, false);
474 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
476 res
= fold_convert (type
, res
);
477 if (CONSTANT_CLASS_P (res
))
485 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
488 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
491 /* Builtin function with two arguments. */
494 gimple_simplify (enum built_in_function fn
, tree type
,
495 tree arg0
, tree arg1
,
496 gimple_seq
*seq
, tree (*valueize
)(tree
))
498 if (constant_for_folding (arg0
)
499 && constant_for_folding (arg1
))
501 tree decl
= builtin_decl_implicit (fn
);
507 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, args
, 2, false);
510 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
512 res
= fold_convert (type
, res
);
513 if (CONSTANT_CLASS_P (res
))
521 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
522 fn
, type
, arg0
, arg1
))
524 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
527 /* Builtin function with three arguments. */
530 gimple_simplify (enum built_in_function fn
, tree type
,
531 tree arg0
, tree arg1
, tree arg2
,
532 gimple_seq
*seq
, tree (*valueize
)(tree
))
534 if (constant_for_folding (arg0
)
535 && constant_for_folding (arg1
)
536 && constant_for_folding (arg2
))
538 tree decl
= builtin_decl_implicit (fn
);
545 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, args
, 3, false);
548 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
550 res
= fold_convert (type
, res
);
551 if (CONSTANT_CLASS_P (res
))
559 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
560 fn
, type
, arg0
, arg1
, arg2
))
562 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
566 /* The main STMT based simplification entry. It is used by the fold_stmt
567 and the fold_stmt_to_constant APIs. */
570 gimple_simplify (gimple stmt
,
571 code_helper
*rcode
, tree
*ops
,
573 tree (*valueize
)(tree
), tree (*top_valueize
)(tree
))
575 switch (gimple_code (stmt
))
579 enum tree_code code
= gimple_assign_rhs_code (stmt
);
580 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
581 switch (gimple_assign_rhs_class (stmt
))
583 case GIMPLE_SINGLE_RHS
:
584 if (code
== REALPART_EXPR
585 || code
== IMAGPART_EXPR
586 || code
== VIEW_CONVERT_EXPR
)
588 tree op0
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
589 if (top_valueize
&& TREE_CODE (op0
) == SSA_NAME
)
591 tree tem
= top_valueize (op0
);
597 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
599 else if (code
== BIT_FIELD_REF
)
601 tree rhs1
= gimple_assign_rhs1 (stmt
);
602 tree op0
= TREE_OPERAND (rhs1
, 0);
603 if (top_valueize
&& TREE_CODE (op0
) == SSA_NAME
)
605 tree tem
= top_valueize (op0
);
611 ops
[1] = TREE_OPERAND (rhs1
, 1);
612 ops
[2] = TREE_OPERAND (rhs1
, 2);
613 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
615 else if (code
== SSA_NAME
618 tree op0
= gimple_assign_rhs1 (stmt
);
619 tree valueized
= top_valueize (op0
);
620 if (!valueized
|| op0
== valueized
)
623 *rcode
= TREE_CODE (op0
);
627 case GIMPLE_UNARY_RHS
:
629 tree rhs1
= gimple_assign_rhs1 (stmt
);
630 if (top_valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
632 tree tem
= top_valueize (rhs1
);
638 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
640 case GIMPLE_BINARY_RHS
:
642 tree rhs1
= gimple_assign_rhs1 (stmt
);
643 if (top_valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
645 tree tem
= top_valueize (rhs1
);
649 tree rhs2
= gimple_assign_rhs2 (stmt
);
650 if (top_valueize
&& TREE_CODE (rhs2
) == SSA_NAME
)
652 tree tem
= top_valueize (rhs2
);
659 return gimple_resimplify2 (seq
, rcode
, type
, ops
, valueize
);
661 case GIMPLE_TERNARY_RHS
:
663 tree rhs1
= gimple_assign_rhs1 (stmt
);
664 if (top_valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
666 tree tem
= top_valueize (rhs1
);
670 tree rhs2
= gimple_assign_rhs2 (stmt
);
671 if (top_valueize
&& TREE_CODE (rhs2
) == SSA_NAME
)
673 tree tem
= top_valueize (rhs2
);
677 tree rhs3
= gimple_assign_rhs3 (stmt
);
678 if (top_valueize
&& TREE_CODE (rhs3
) == SSA_NAME
)
680 tree tem
= top_valueize (rhs3
);
688 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
697 /* ??? This way we can't simplify calls with side-effects. */
698 if (gimple_call_lhs (stmt
) != NULL_TREE
)
700 tree fn
= gimple_call_fn (stmt
);
701 /* ??? Internal function support missing. */
704 if (top_valueize
&& TREE_CODE (fn
) == SSA_NAME
)
706 tree tem
= top_valueize (fn
);
711 || TREE_CODE (fn
) != ADDR_EXPR
712 || TREE_CODE (TREE_OPERAND (fn
, 0)) != FUNCTION_DECL
713 || DECL_BUILT_IN_CLASS (TREE_OPERAND (fn
, 0)) != BUILT_IN_NORMAL
714 || !builtin_decl_implicit (DECL_FUNCTION_CODE (TREE_OPERAND (fn
, 0)))
715 || !gimple_builtin_call_types_compatible_p (stmt
,
716 TREE_OPERAND (fn
, 0)))
719 tree decl
= TREE_OPERAND (fn
, 0);
720 tree type
= TREE_TYPE (gimple_call_lhs (stmt
));
721 switch (gimple_call_num_args (stmt
))
725 tree arg1
= gimple_call_arg (stmt
, 0);
726 if (top_valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
728 tree tem
= top_valueize (arg1
);
732 *rcode
= DECL_FUNCTION_CODE (decl
);
734 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
738 tree arg1
= gimple_call_arg (stmt
, 0);
739 if (top_valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
741 tree tem
= top_valueize (arg1
);
745 tree arg2
= gimple_call_arg (stmt
, 1);
746 if (top_valueize
&& TREE_CODE (arg2
) == SSA_NAME
)
748 tree tem
= top_valueize (arg2
);
752 *rcode
= DECL_FUNCTION_CODE (decl
);
755 return gimple_resimplify2 (seq
, rcode
, type
, ops
, valueize
);
759 tree arg1
= gimple_call_arg (stmt
, 0);
760 if (top_valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
762 tree tem
= top_valueize (arg1
);
766 tree arg2
= gimple_call_arg (stmt
, 1);
767 if (top_valueize
&& TREE_CODE (arg2
) == SSA_NAME
)
769 tree tem
= top_valueize (arg2
);
773 tree arg3
= gimple_call_arg (stmt
, 2);
774 if (top_valueize
&& TREE_CODE (arg3
) == SSA_NAME
)
776 tree tem
= top_valueize (arg3
);
780 *rcode
= DECL_FUNCTION_CODE (decl
);
784 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
794 tree lhs
= gimple_cond_lhs (stmt
);
795 if (top_valueize
&& TREE_CODE (lhs
) == SSA_NAME
)
797 tree tem
= top_valueize (lhs
);
801 tree rhs
= gimple_cond_rhs (stmt
);
802 if (top_valueize
&& TREE_CODE (rhs
) == SSA_NAME
)
804 tree tem
= top_valueize (rhs
);
808 *rcode
= gimple_cond_code (stmt
);
811 return gimple_resimplify2 (seq
, rcode
, boolean_type_node
, ops
, valueize
);
822 /* Helper for the autogenerated code, valueize OP. */
825 do_valueize (tree (*valueize
)(tree
), tree op
)
827 if (valueize
&& TREE_CODE (op
) == SSA_NAME
)
828 return valueize (op
);
832 /* Routine to determine if the types T1 and T2 are effectively
833 the same for GIMPLE. If T1 or T2 is not a type, the test
834 applies to their TREE_TYPE. */
837 types_match (tree t1
, tree t2
)
844 return types_compatible_p (t1
, t2
);
847 /* Return if T has a single use. For GIMPLE, we also allow any
848 non-SSA_NAME (ie constants) and zero uses to cope with uses
849 that aren't linked up yet. */
854 return TREE_CODE (t
) != SSA_NAME
|| has_zero_uses (t
) || has_single_use (t
);