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 std::swap (res_ops
[0], res_ops
[1]);
194 if (TREE_CODE_CLASS ((enum tree_code
) *res_code
) == tcc_comparison
)
195 *res_code
= swap_tree_comparison (*res_code
);
196 canonicalized
= true;
199 code_helper res_code2
;
200 tree res_ops2
[3] = {};
201 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
202 *res_code
, type
, res_ops
[0], res_ops
[1]))
204 *res_code
= res_code2
;
205 res_ops
[0] = res_ops2
[0];
206 res_ops
[1] = res_ops2
[1];
207 res_ops
[2] = res_ops2
[2];
211 return canonicalized
;
214 /* Helper that matches and simplifies the toplevel result from
215 a gimple_simplify run (where we don't want to build
216 a stmt in case it's used in in-place folding). Replaces
217 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
218 result and returns whether any change was made. */
221 gimple_resimplify3 (gimple_seq
*seq
,
222 code_helper
*res_code
, tree type
, tree
*res_ops
,
223 tree (*valueize
)(tree
))
225 if (constant_for_folding (res_ops
[0]) && constant_for_folding (res_ops
[1])
226 && constant_for_folding (res_ops
[2]))
228 tree tem
= NULL_TREE
;
229 if (res_code
->is_tree_code ())
230 tem
= fold_ternary
/*_to_constant*/ (*res_code
, type
, res_ops
[0],
231 res_ops
[1], res_ops
[2]);
234 tree decl
= builtin_decl_implicit (*res_code
);
237 tem
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, res_ops
, 3, false);
240 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
242 tem
= fold_convert (type
, tem
);
247 && CONSTANT_CLASS_P (tem
))
250 res_ops
[1] = NULL_TREE
;
251 res_ops
[2] = NULL_TREE
;
252 *res_code
= TREE_CODE (res_ops
[0]);
257 /* Canonicalize operand order. */
258 bool canonicalized
= false;
259 if (res_code
->is_tree_code ()
260 && commutative_ternary_tree_code (*res_code
)
261 && tree_swap_operands_p (res_ops
[0], res_ops
[1], false))
263 std::swap (res_ops
[0], res_ops
[1]);
264 canonicalized
= true;
267 code_helper res_code2
;
268 tree res_ops2
[3] = {};
269 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
271 res_ops
[0], res_ops
[1], res_ops
[2]))
273 *res_code
= res_code2
;
274 res_ops
[0] = res_ops2
[0];
275 res_ops
[1] = res_ops2
[1];
276 res_ops
[2] = res_ops2
[2];
280 return canonicalized
;
284 /* If in GIMPLE expressions with CODE go as single-rhs build
285 a GENERIC tree for that expression into *OP0. */
288 maybe_build_generic_op (enum tree_code code
, tree type
,
289 tree
*op0
, tree op1
, tree op2
)
295 case VIEW_CONVERT_EXPR
:
296 *op0
= build1 (code
, type
, *op0
);
299 *op0
= build3 (code
, type
, *op0
, op1
, op2
);
305 /* Push the exploded expression described by RCODE, TYPE and OPS
306 as a statement to SEQ if necessary and return a gimple value
307 denoting the value of the expression. If RES is not NULL
308 then the result will be always RES and even gimple values are
312 maybe_push_res_to_seq (code_helper rcode
, tree type
, tree
*ops
,
313 gimple_seq
*seq
, tree res
)
315 if (rcode
.is_tree_code ())
318 && (TREE_CODE_LENGTH ((tree_code
) rcode
) == 0
319 || ((tree_code
) rcode
) == ADDR_EXPR
)
320 && is_gimple_val (ops
[0]))
324 /* Play safe and do not allow abnormals to be mentioned in
325 newly created statements. */
326 if ((TREE_CODE (ops
[0]) == SSA_NAME
327 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[0]))
329 && TREE_CODE (ops
[1]) == SSA_NAME
330 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[1]))
332 && TREE_CODE (ops
[2]) == SSA_NAME
333 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[2])))
336 res
= make_ssa_name (type
);
337 maybe_build_generic_op (rcode
, type
, &ops
[0], ops
[1], ops
[2]);
338 gimple new_stmt
= gimple_build_assign (res
, rcode
,
339 ops
[0], ops
[1], ops
[2]);
340 gimple_seq_add_stmt_without_update (seq
, new_stmt
);
347 tree decl
= builtin_decl_implicit (rcode
);
350 unsigned nargs
= type_num_arguments (TREE_TYPE (decl
));
351 gcc_assert (nargs
<= 3);
352 /* Play safe and do not allow abnormals to be mentioned in
353 newly created statements. */
354 if ((TREE_CODE (ops
[0]) == SSA_NAME
355 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[0]))
357 && TREE_CODE (ops
[1]) == SSA_NAME
358 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[1]))
360 && TREE_CODE (ops
[2]) == SSA_NAME
361 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[2])))
364 res
= make_ssa_name (type
);
365 gimple new_stmt
= gimple_build_call (decl
, nargs
, ops
[0], ops
[1], ops
[2]);
366 gimple_call_set_lhs (new_stmt
, res
);
367 gimple_seq_add_stmt_without_update (seq
, new_stmt
);
373 /* Public API overloads follow for operation being tree_code or
374 built_in_function and for one to three operands or arguments.
375 They return NULL_TREE if nothing could be simplified or
376 the resulting simplified value with parts pushed to SEQ.
377 If SEQ is NULL then if the simplification needs to create
378 new stmts it will fail. If VALUEIZE is non-NULL then all
379 SSA names will be valueized using that hook prior to
380 applying simplifications. */
385 gimple_simplify (enum tree_code code
, tree type
,
387 gimple_seq
*seq
, tree (*valueize
)(tree
))
389 if (constant_for_folding (op0
))
391 tree res
= const_unop (code
, type
, op0
);
393 && CONSTANT_CLASS_P (res
))
399 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
402 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
408 gimple_simplify (enum tree_code code
, tree type
,
410 gimple_seq
*seq
, tree (*valueize
)(tree
))
412 if (constant_for_folding (op0
) && constant_for_folding (op1
))
414 tree res
= const_binop (code
, type
, op0
, op1
);
416 && CONSTANT_CLASS_P (res
))
420 /* Canonicalize operand order both for matching and fallback stmt
422 if ((commutative_tree_code (code
)
423 || TREE_CODE_CLASS (code
) == tcc_comparison
)
424 && tree_swap_operands_p (op0
, op1
, false))
426 std::swap (op0
, op1
);
427 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
428 code
= swap_tree_comparison (code
);
433 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
434 code
, type
, op0
, op1
))
436 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
442 gimple_simplify (enum tree_code code
, tree type
,
443 tree op0
, tree op1
, tree op2
,
444 gimple_seq
*seq
, tree (*valueize
)(tree
))
446 if (constant_for_folding (op0
) && constant_for_folding (op1
)
447 && constant_for_folding (op2
))
449 tree res
= fold_ternary
/*_to_constant */ (code
, type
, op0
, op1
, op2
);
451 && CONSTANT_CLASS_P (res
))
455 /* Canonicalize operand order both for matching and fallback stmt
457 if (commutative_ternary_tree_code (code
)
458 && tree_swap_operands_p (op0
, op1
, false))
459 std::swap (op0
, op1
);
463 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
464 code
, type
, op0
, op1
, op2
))
466 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
469 /* Builtin function with one argument. */
472 gimple_simplify (enum built_in_function fn
, tree type
,
474 gimple_seq
*seq
, tree (*valueize
)(tree
))
476 if (constant_for_folding (arg0
))
478 tree decl
= builtin_decl_implicit (fn
);
481 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, &arg0
, 1, false);
484 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
486 res
= fold_convert (type
, res
);
487 if (CONSTANT_CLASS_P (res
))
495 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
498 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
501 /* Builtin function with two arguments. */
504 gimple_simplify (enum built_in_function fn
, tree type
,
505 tree arg0
, tree arg1
,
506 gimple_seq
*seq
, tree (*valueize
)(tree
))
508 if (constant_for_folding (arg0
)
509 && constant_for_folding (arg1
))
511 tree decl
= builtin_decl_implicit (fn
);
517 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, args
, 2, false);
520 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
522 res
= fold_convert (type
, res
);
523 if (CONSTANT_CLASS_P (res
))
531 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
532 fn
, type
, arg0
, arg1
))
534 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
537 /* Builtin function with three arguments. */
540 gimple_simplify (enum built_in_function fn
, tree type
,
541 tree arg0
, tree arg1
, tree arg2
,
542 gimple_seq
*seq
, tree (*valueize
)(tree
))
544 if (constant_for_folding (arg0
)
545 && constant_for_folding (arg1
)
546 && constant_for_folding (arg2
))
548 tree decl
= builtin_decl_implicit (fn
);
555 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, args
, 3, false);
558 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
560 res
= fold_convert (type
, res
);
561 if (CONSTANT_CLASS_P (res
))
569 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
570 fn
, type
, arg0
, arg1
, arg2
))
572 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
576 /* The main STMT based simplification entry. It is used by the fold_stmt
577 and the fold_stmt_to_constant APIs. */
580 gimple_simplify (gimple stmt
,
581 code_helper
*rcode
, tree
*ops
,
583 tree (*valueize
)(tree
), tree (*top_valueize
)(tree
))
585 switch (gimple_code (stmt
))
589 enum tree_code code
= gimple_assign_rhs_code (stmt
);
590 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
591 switch (gimple_assign_rhs_class (stmt
))
593 case GIMPLE_SINGLE_RHS
:
594 if (code
== REALPART_EXPR
595 || code
== IMAGPART_EXPR
596 || code
== VIEW_CONVERT_EXPR
)
598 tree op0
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
599 if (top_valueize
&& TREE_CODE (op0
) == SSA_NAME
)
601 tree tem
= top_valueize (op0
);
607 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
609 else if (code
== BIT_FIELD_REF
)
611 tree rhs1
= gimple_assign_rhs1 (stmt
);
612 tree op0
= TREE_OPERAND (rhs1
, 0);
613 if (top_valueize
&& TREE_CODE (op0
) == SSA_NAME
)
615 tree tem
= top_valueize (op0
);
621 ops
[1] = TREE_OPERAND (rhs1
, 1);
622 ops
[2] = TREE_OPERAND (rhs1
, 2);
623 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
625 else if (code
== SSA_NAME
628 tree op0
= gimple_assign_rhs1 (stmt
);
629 tree valueized
= top_valueize (op0
);
630 if (!valueized
|| op0
== valueized
)
633 *rcode
= TREE_CODE (op0
);
637 case GIMPLE_UNARY_RHS
:
639 tree rhs1
= gimple_assign_rhs1 (stmt
);
640 if (top_valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
642 tree tem
= top_valueize (rhs1
);
648 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
650 case GIMPLE_BINARY_RHS
:
652 tree rhs1
= gimple_assign_rhs1 (stmt
);
653 if (top_valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
655 tree tem
= top_valueize (rhs1
);
659 tree rhs2
= gimple_assign_rhs2 (stmt
);
660 if (top_valueize
&& TREE_CODE (rhs2
) == SSA_NAME
)
662 tree tem
= top_valueize (rhs2
);
669 return gimple_resimplify2 (seq
, rcode
, type
, ops
, valueize
);
671 case GIMPLE_TERNARY_RHS
:
673 tree rhs1
= gimple_assign_rhs1 (stmt
);
674 if (top_valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
676 tree tem
= top_valueize (rhs1
);
680 tree rhs2
= gimple_assign_rhs2 (stmt
);
681 if (top_valueize
&& TREE_CODE (rhs2
) == SSA_NAME
)
683 tree tem
= top_valueize (rhs2
);
687 tree rhs3
= gimple_assign_rhs3 (stmt
);
688 if (top_valueize
&& TREE_CODE (rhs3
) == SSA_NAME
)
690 tree tem
= top_valueize (rhs3
);
698 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
707 /* ??? This way we can't simplify calls with side-effects. */
708 if (gimple_call_lhs (stmt
) != NULL_TREE
)
710 tree fn
= gimple_call_fn (stmt
);
711 /* ??? Internal function support missing. */
714 if (top_valueize
&& TREE_CODE (fn
) == SSA_NAME
)
716 tree tem
= top_valueize (fn
);
721 || TREE_CODE (fn
) != ADDR_EXPR
722 || TREE_CODE (TREE_OPERAND (fn
, 0)) != FUNCTION_DECL
723 || DECL_BUILT_IN_CLASS (TREE_OPERAND (fn
, 0)) != BUILT_IN_NORMAL
724 || !builtin_decl_implicit (DECL_FUNCTION_CODE (TREE_OPERAND (fn
, 0)))
725 || !gimple_builtin_call_types_compatible_p (stmt
,
726 TREE_OPERAND (fn
, 0)))
729 tree decl
= TREE_OPERAND (fn
, 0);
730 tree type
= TREE_TYPE (gimple_call_lhs (stmt
));
731 switch (gimple_call_num_args (stmt
))
735 tree arg1
= gimple_call_arg (stmt
, 0);
736 if (top_valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
738 tree tem
= top_valueize (arg1
);
742 *rcode
= DECL_FUNCTION_CODE (decl
);
744 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
748 tree arg1
= gimple_call_arg (stmt
, 0);
749 if (top_valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
751 tree tem
= top_valueize (arg1
);
755 tree arg2
= gimple_call_arg (stmt
, 1);
756 if (top_valueize
&& TREE_CODE (arg2
) == SSA_NAME
)
758 tree tem
= top_valueize (arg2
);
762 *rcode
= DECL_FUNCTION_CODE (decl
);
765 return gimple_resimplify2 (seq
, rcode
, type
, ops
, valueize
);
769 tree arg1
= gimple_call_arg (stmt
, 0);
770 if (top_valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
772 tree tem
= top_valueize (arg1
);
776 tree arg2
= gimple_call_arg (stmt
, 1);
777 if (top_valueize
&& TREE_CODE (arg2
) == SSA_NAME
)
779 tree tem
= top_valueize (arg2
);
783 tree arg3
= gimple_call_arg (stmt
, 2);
784 if (top_valueize
&& TREE_CODE (arg3
) == SSA_NAME
)
786 tree tem
= top_valueize (arg3
);
790 *rcode
= DECL_FUNCTION_CODE (decl
);
794 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
804 tree lhs
= gimple_cond_lhs (stmt
);
805 if (top_valueize
&& TREE_CODE (lhs
) == SSA_NAME
)
807 tree tem
= top_valueize (lhs
);
811 tree rhs
= gimple_cond_rhs (stmt
);
812 if (top_valueize
&& TREE_CODE (rhs
) == SSA_NAME
)
814 tree tem
= top_valueize (rhs
);
818 *rcode
= gimple_cond_code (stmt
);
821 return gimple_resimplify2 (seq
, rcode
, boolean_type_node
, ops
, valueize
);
832 /* Helper for the autogenerated code, valueize OP. */
835 do_valueize (tree (*valueize
)(tree
), tree op
)
837 if (valueize
&& TREE_CODE (op
) == SSA_NAME
)
838 return valueize (op
);
842 /* Routine to determine if the types T1 and T2 are effectively
843 the same for GIMPLE. If T1 or T2 is not a type, the test
844 applies to their TREE_TYPE. */
847 types_match (tree t1
, tree t2
)
854 return types_compatible_p (t1
, t2
);
857 /* Return if T has a single use. For GIMPLE, we also allow any
858 non-SSA_NAME (ie constants) and zero uses to cope with uses
859 that aren't linked up yet. */
864 return TREE_CODE (t
) != SSA_NAME
|| has_zero_uses (t
) || has_single_use (t
);