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"
39 #include "hard-reg-set.h"
42 #include "basic-block.h"
43 #include "tree-ssa-alias.h"
44 #include "internal-fn.h"
45 #include "gimple-expr.h"
48 #include "gimple-ssa.h"
49 #include "tree-ssanames.h"
50 #include "gimple-fold.h"
51 #include "gimple-iterator.h"
54 #include "statistics.h"
56 #include "fixed-value.h"
57 #include "insn-config.h"
68 #include "tree-phinodes.h"
69 #include "ssa-iterators.h"
71 #include "gimple-match.h"
74 /* Forward declarations of the private auto-generated matchers.
75 They expect valueized operands in canonical order and do not
76 perform simplification of all-constant operands. */
77 static bool gimple_simplify (code_helper
*, tree
*,
78 gimple_seq
*, tree (*)(tree
),
79 code_helper
, tree
, tree
);
80 static bool gimple_simplify (code_helper
*, tree
*,
81 gimple_seq
*, tree (*)(tree
),
82 code_helper
, tree
, tree
, tree
);
83 static bool gimple_simplify (code_helper
*, tree
*,
84 gimple_seq
*, tree (*)(tree
),
85 code_helper
, tree
, tree
, tree
, tree
);
88 /* Return whether T is a constant that we'll dispatch to fold to
89 evaluate fully constant expressions. */
92 constant_for_folding (tree t
)
94 return (CONSTANT_CLASS_P (t
)
95 /* The following is only interesting to string builtins. */
96 || (TREE_CODE (t
) == ADDR_EXPR
97 && TREE_CODE (TREE_OPERAND (t
, 0)) == STRING_CST
));
101 /* Helper that matches and simplifies the toplevel result from
102 a gimple_simplify run (where we don't want to build
103 a stmt in case it's used in in-place folding). Replaces
104 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
105 result and returns whether any change was made. */
108 gimple_resimplify1 (gimple_seq
*seq
,
109 code_helper
*res_code
, tree type
, tree
*res_ops
,
110 tree (*valueize
)(tree
))
112 if (constant_for_folding (res_ops
[0]))
114 tree tem
= NULL_TREE
;
115 if (res_code
->is_tree_code ())
116 tem
= const_unop (*res_code
, type
, res_ops
[0]);
119 tree decl
= builtin_decl_implicit (*res_code
);
122 tem
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, res_ops
, 1, false);
125 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
127 tem
= fold_convert (type
, tem
);
132 && CONSTANT_CLASS_P (tem
))
135 res_ops
[1] = NULL_TREE
;
136 res_ops
[2] = NULL_TREE
;
137 *res_code
= TREE_CODE (res_ops
[0]);
142 code_helper res_code2
;
143 tree res_ops2
[3] = {};
144 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
145 *res_code
, type
, res_ops
[0]))
147 *res_code
= res_code2
;
148 res_ops
[0] = res_ops2
[0];
149 res_ops
[1] = res_ops2
[1];
150 res_ops
[2] = res_ops2
[2];
157 /* Helper that matches and simplifies the toplevel result from
158 a gimple_simplify run (where we don't want to build
159 a stmt in case it's used in in-place folding). Replaces
160 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
161 result and returns whether any change was made. */
164 gimple_resimplify2 (gimple_seq
*seq
,
165 code_helper
*res_code
, tree type
, tree
*res_ops
,
166 tree (*valueize
)(tree
))
168 if (constant_for_folding (res_ops
[0]) && constant_for_folding (res_ops
[1]))
170 tree tem
= NULL_TREE
;
171 if (res_code
->is_tree_code ())
172 tem
= const_binop (*res_code
, type
, res_ops
[0], res_ops
[1]);
175 tree decl
= builtin_decl_implicit (*res_code
);
178 tem
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, res_ops
, 2, false);
181 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
183 tem
= fold_convert (type
, tem
);
188 && CONSTANT_CLASS_P (tem
))
191 res_ops
[1] = NULL_TREE
;
192 res_ops
[2] = NULL_TREE
;
193 *res_code
= TREE_CODE (res_ops
[0]);
198 /* Canonicalize operand order. */
199 bool canonicalized
= false;
200 if (res_code
->is_tree_code ()
201 && (TREE_CODE_CLASS ((enum tree_code
) *res_code
) == tcc_comparison
202 || commutative_tree_code (*res_code
))
203 && tree_swap_operands_p (res_ops
[0], res_ops
[1], false))
205 tree tem
= res_ops
[0];
206 res_ops
[0] = res_ops
[1];
208 if (TREE_CODE_CLASS ((enum tree_code
) *res_code
) == tcc_comparison
)
209 *res_code
= swap_tree_comparison (*res_code
);
210 canonicalized
= true;
213 code_helper res_code2
;
214 tree res_ops2
[3] = {};
215 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
216 *res_code
, type
, res_ops
[0], res_ops
[1]))
218 *res_code
= res_code2
;
219 res_ops
[0] = res_ops2
[0];
220 res_ops
[1] = res_ops2
[1];
221 res_ops
[2] = res_ops2
[2];
225 return canonicalized
;
228 /* Helper that matches and simplifies the toplevel result from
229 a gimple_simplify run (where we don't want to build
230 a stmt in case it's used in in-place folding). Replaces
231 *RES_CODE and *RES_OPS with a simplified and/or canonicalized
232 result and returns whether any change was made. */
235 gimple_resimplify3 (gimple_seq
*seq
,
236 code_helper
*res_code
, tree type
, tree
*res_ops
,
237 tree (*valueize
)(tree
))
239 if (constant_for_folding (res_ops
[0]) && constant_for_folding (res_ops
[1])
240 && constant_for_folding (res_ops
[2]))
242 tree tem
= NULL_TREE
;
243 if (res_code
->is_tree_code ())
244 tem
= fold_ternary
/*_to_constant*/ (*res_code
, type
, res_ops
[0],
245 res_ops
[1], res_ops
[2]);
248 tree decl
= builtin_decl_implicit (*res_code
);
251 tem
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, res_ops
, 3, false);
254 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
256 tem
= fold_convert (type
, tem
);
261 && CONSTANT_CLASS_P (tem
))
264 res_ops
[1] = NULL_TREE
;
265 res_ops
[2] = NULL_TREE
;
266 *res_code
= TREE_CODE (res_ops
[0]);
271 /* Canonicalize operand order. */
272 bool canonicalized
= false;
273 if (res_code
->is_tree_code ()
274 && commutative_ternary_tree_code (*res_code
)
275 && tree_swap_operands_p (res_ops
[0], res_ops
[1], false))
277 tree tem
= res_ops
[0];
278 res_ops
[0] = res_ops
[1];
280 canonicalized
= true;
283 code_helper res_code2
;
284 tree res_ops2
[3] = {};
285 if (gimple_simplify (&res_code2
, res_ops2
, seq
, valueize
,
287 res_ops
[0], res_ops
[1], res_ops
[2]))
289 *res_code
= res_code2
;
290 res_ops
[0] = res_ops2
[0];
291 res_ops
[1] = res_ops2
[1];
292 res_ops
[2] = res_ops2
[2];
296 return canonicalized
;
300 /* If in GIMPLE expressions with CODE go as single-rhs build
301 a GENERIC tree for that expression into *OP0. */
304 maybe_build_generic_op (enum tree_code code
, tree type
,
305 tree
*op0
, tree op1
, tree op2
)
311 case VIEW_CONVERT_EXPR
:
312 *op0
= build1 (code
, type
, *op0
);
315 *op0
= build3 (code
, type
, *op0
, op1
, op2
);
321 /* Push the exploded expression described by RCODE, TYPE and OPS
322 as a statement to SEQ if necessary and return a gimple value
323 denoting the value of the expression. If RES is not NULL
324 then the result will be always RES and even gimple values are
328 maybe_push_res_to_seq (code_helper rcode
, tree type
, tree
*ops
,
329 gimple_seq
*seq
, tree res
)
331 if (rcode
.is_tree_code ())
334 && (TREE_CODE_LENGTH ((tree_code
) rcode
) == 0
335 || ((tree_code
) rcode
) == ADDR_EXPR
)
336 && is_gimple_val (ops
[0]))
340 /* Play safe and do not allow abnormals to be mentioned in
341 newly created statements. */
342 if ((TREE_CODE (ops
[0]) == SSA_NAME
343 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[0]))
345 && TREE_CODE (ops
[1]) == SSA_NAME
346 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[1]))
348 && TREE_CODE (ops
[2]) == SSA_NAME
349 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[2])))
352 res
= make_ssa_name (type
);
353 maybe_build_generic_op (rcode
, type
, &ops
[0], ops
[1], ops
[2]);
354 gimple new_stmt
= gimple_build_assign (res
, rcode
,
355 ops
[0], ops
[1], ops
[2]);
356 gimple_seq_add_stmt_without_update (seq
, new_stmt
);
363 tree decl
= builtin_decl_implicit (rcode
);
366 unsigned nargs
= type_num_arguments (TREE_TYPE (decl
));
367 gcc_assert (nargs
<= 3);
368 /* Play safe and do not allow abnormals to be mentioned in
369 newly created statements. */
370 if ((TREE_CODE (ops
[0]) == SSA_NAME
371 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[0]))
373 && TREE_CODE (ops
[1]) == SSA_NAME
374 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[1]))
376 && TREE_CODE (ops
[2]) == SSA_NAME
377 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[2])))
380 res
= make_ssa_name (type
);
381 gimple new_stmt
= gimple_build_call (decl
, nargs
, ops
[0], ops
[1], ops
[2]);
382 gimple_call_set_lhs (new_stmt
, res
);
383 gimple_seq_add_stmt_without_update (seq
, new_stmt
);
389 /* Public API overloads follow for operation being tree_code or
390 built_in_function and for one to three operands or arguments.
391 They return NULL_TREE if nothing could be simplified or
392 the resulting simplified value with parts pushed to SEQ.
393 If SEQ is NULL then if the simplification needs to create
394 new stmts it will fail. If VALUEIZE is non-NULL then all
395 SSA names will be valueized using that hook prior to
396 applying simplifications. */
401 gimple_simplify (enum tree_code code
, tree type
,
403 gimple_seq
*seq
, tree (*valueize
)(tree
))
405 if (constant_for_folding (op0
))
407 tree res
= const_unop (code
, type
, op0
);
409 && CONSTANT_CLASS_P (res
))
415 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
418 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
424 gimple_simplify (enum tree_code code
, tree type
,
426 gimple_seq
*seq
, tree (*valueize
)(tree
))
428 if (constant_for_folding (op0
) && constant_for_folding (op1
))
430 tree res
= const_binop (code
, type
, op0
, op1
);
432 && CONSTANT_CLASS_P (res
))
436 /* Canonicalize operand order both for matching and fallback stmt
438 if ((commutative_tree_code (code
)
439 || TREE_CODE_CLASS (code
) == tcc_comparison
)
440 && tree_swap_operands_p (op0
, op1
, false))
445 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
446 code
= swap_tree_comparison (code
);
451 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
452 code
, type
, op0
, op1
))
454 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
460 gimple_simplify (enum tree_code code
, tree type
,
461 tree op0
, tree op1
, tree op2
,
462 gimple_seq
*seq
, tree (*valueize
)(tree
))
464 if (constant_for_folding (op0
) && constant_for_folding (op1
)
465 && constant_for_folding (op2
))
467 tree res
= fold_ternary
/*_to_constant */ (code
, type
, op0
, op1
, op2
);
469 && CONSTANT_CLASS_P (res
))
473 /* Canonicalize operand order both for matching and fallback stmt
475 if (commutative_ternary_tree_code (code
)
476 && tree_swap_operands_p (op0
, op1
, false))
485 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
486 code
, type
, op0
, op1
, op2
))
488 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
491 /* Builtin function with one argument. */
494 gimple_simplify (enum built_in_function fn
, tree type
,
496 gimple_seq
*seq
, tree (*valueize
)(tree
))
498 if (constant_for_folding (arg0
))
500 tree decl
= builtin_decl_implicit (fn
);
503 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, &arg0
, 1, false);
506 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
508 res
= fold_convert (type
, res
);
509 if (CONSTANT_CLASS_P (res
))
517 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
520 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
523 /* Builtin function with two arguments. */
526 gimple_simplify (enum built_in_function fn
, tree type
,
527 tree arg0
, tree arg1
,
528 gimple_seq
*seq
, tree (*valueize
)(tree
))
530 if (constant_for_folding (arg0
)
531 && constant_for_folding (arg1
))
533 tree decl
= builtin_decl_implicit (fn
);
539 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, args
, 2, false);
542 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
544 res
= fold_convert (type
, res
);
545 if (CONSTANT_CLASS_P (res
))
553 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
554 fn
, type
, arg0
, arg1
))
556 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
559 /* Builtin function with three arguments. */
562 gimple_simplify (enum built_in_function fn
, tree type
,
563 tree arg0
, tree arg1
, tree arg2
,
564 gimple_seq
*seq
, tree (*valueize
)(tree
))
566 if (constant_for_folding (arg0
)
567 && constant_for_folding (arg1
)
568 && constant_for_folding (arg2
))
570 tree decl
= builtin_decl_implicit (fn
);
577 tree res
= fold_builtin_n (UNKNOWN_LOCATION
, decl
, args
, 3, false);
580 /* fold_builtin_n wraps the result inside a NOP_EXPR. */
582 res
= fold_convert (type
, res
);
583 if (CONSTANT_CLASS_P (res
))
591 if (!gimple_simplify (&rcode
, ops
, seq
, valueize
,
592 fn
, type
, arg0
, arg1
, arg2
))
594 return maybe_push_res_to_seq (rcode
, type
, ops
, seq
);
598 /* The main STMT based simplification entry. It is used by the fold_stmt
599 and the fold_stmt_to_constant APIs. */
602 gimple_simplify (gimple stmt
,
603 code_helper
*rcode
, tree
*ops
,
605 tree (*valueize
)(tree
), tree (*top_valueize
)(tree
))
607 switch (gimple_code (stmt
))
611 enum tree_code code
= gimple_assign_rhs_code (stmt
);
612 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
613 switch (gimple_assign_rhs_class (stmt
))
615 case GIMPLE_SINGLE_RHS
:
616 if (code
== REALPART_EXPR
617 || code
== IMAGPART_EXPR
618 || code
== VIEW_CONVERT_EXPR
)
620 tree op0
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
621 if (top_valueize
&& TREE_CODE (op0
) == SSA_NAME
)
623 tree tem
= top_valueize (op0
);
629 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
631 else if (code
== BIT_FIELD_REF
)
633 tree rhs1
= gimple_assign_rhs1 (stmt
);
634 tree op0
= TREE_OPERAND (rhs1
, 0);
635 if (top_valueize
&& TREE_CODE (op0
) == SSA_NAME
)
637 tree tem
= top_valueize (op0
);
643 ops
[1] = TREE_OPERAND (rhs1
, 1);
644 ops
[2] = TREE_OPERAND (rhs1
, 2);
645 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
647 else if (code
== SSA_NAME
650 tree op0
= gimple_assign_rhs1 (stmt
);
651 tree valueized
= top_valueize (op0
);
652 if (!valueized
|| op0
== valueized
)
655 *rcode
= TREE_CODE (op0
);
659 case GIMPLE_UNARY_RHS
:
661 tree rhs1
= gimple_assign_rhs1 (stmt
);
662 if (top_valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
664 tree tem
= top_valueize (rhs1
);
670 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
672 case GIMPLE_BINARY_RHS
:
674 tree rhs1
= gimple_assign_rhs1 (stmt
);
675 if (top_valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
677 tree tem
= top_valueize (rhs1
);
681 tree rhs2
= gimple_assign_rhs2 (stmt
);
682 if (top_valueize
&& TREE_CODE (rhs2
) == SSA_NAME
)
684 tree tem
= top_valueize (rhs2
);
691 return gimple_resimplify2 (seq
, rcode
, type
, ops
, valueize
);
693 case GIMPLE_TERNARY_RHS
:
695 tree rhs1
= gimple_assign_rhs1 (stmt
);
696 if (top_valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
698 tree tem
= top_valueize (rhs1
);
702 tree rhs2
= gimple_assign_rhs2 (stmt
);
703 if (top_valueize
&& TREE_CODE (rhs2
) == SSA_NAME
)
705 tree tem
= top_valueize (rhs2
);
709 tree rhs3
= gimple_assign_rhs3 (stmt
);
710 if (top_valueize
&& TREE_CODE (rhs3
) == SSA_NAME
)
712 tree tem
= top_valueize (rhs3
);
720 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
729 /* ??? This way we can't simplify calls with side-effects. */
730 if (gimple_call_lhs (stmt
) != NULL_TREE
)
732 tree fn
= gimple_call_fn (stmt
);
733 /* ??? Internal function support missing. */
736 if (top_valueize
&& TREE_CODE (fn
) == SSA_NAME
)
738 tree tem
= top_valueize (fn
);
743 || TREE_CODE (fn
) != ADDR_EXPR
744 || TREE_CODE (TREE_OPERAND (fn
, 0)) != FUNCTION_DECL
745 || DECL_BUILT_IN_CLASS (TREE_OPERAND (fn
, 0)) != BUILT_IN_NORMAL
746 || !builtin_decl_implicit (DECL_FUNCTION_CODE (TREE_OPERAND (fn
, 0)))
747 || !gimple_builtin_call_types_compatible_p (stmt
,
748 TREE_OPERAND (fn
, 0)))
751 tree decl
= TREE_OPERAND (fn
, 0);
752 tree type
= TREE_TYPE (gimple_call_lhs (stmt
));
753 switch (gimple_call_num_args (stmt
))
757 tree arg1
= gimple_call_arg (stmt
, 0);
758 if (top_valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
760 tree tem
= top_valueize (arg1
);
764 *rcode
= DECL_FUNCTION_CODE (decl
);
766 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
770 tree arg1
= gimple_call_arg (stmt
, 0);
771 if (top_valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
773 tree tem
= top_valueize (arg1
);
777 tree arg2
= gimple_call_arg (stmt
, 1);
778 if (top_valueize
&& TREE_CODE (arg2
) == SSA_NAME
)
780 tree tem
= top_valueize (arg2
);
784 *rcode
= DECL_FUNCTION_CODE (decl
);
787 return gimple_resimplify2 (seq
, rcode
, type
, ops
, valueize
);
791 tree arg1
= gimple_call_arg (stmt
, 0);
792 if (top_valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
794 tree tem
= top_valueize (arg1
);
798 tree arg2
= gimple_call_arg (stmt
, 1);
799 if (top_valueize
&& TREE_CODE (arg2
) == SSA_NAME
)
801 tree tem
= top_valueize (arg2
);
805 tree arg3
= gimple_call_arg (stmt
, 2);
806 if (top_valueize
&& TREE_CODE (arg3
) == SSA_NAME
)
808 tree tem
= top_valueize (arg3
);
812 *rcode
= DECL_FUNCTION_CODE (decl
);
816 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
826 tree lhs
= gimple_cond_lhs (stmt
);
827 if (top_valueize
&& TREE_CODE (lhs
) == SSA_NAME
)
829 tree tem
= top_valueize (lhs
);
833 tree rhs
= gimple_cond_rhs (stmt
);
834 if (top_valueize
&& TREE_CODE (rhs
) == SSA_NAME
)
836 tree tem
= top_valueize (rhs
);
840 *rcode
= gimple_cond_code (stmt
);
843 return gimple_resimplify2 (seq
, rcode
, boolean_type_node
, ops
, valueize
);
854 /* Helper for the autogenerated code, valueize OP. */
857 do_valueize (tree (*valueize
)(tree
), tree op
)
859 if (valueize
&& TREE_CODE (op
) == SSA_NAME
)
860 return valueize (op
);
864 /* Routine to determine if the types T1 and T2 are effectively
865 the same for GIMPLE. If T1 or T2 is not a type, the test
866 applies to their TREE_TYPE. */
869 types_match (tree t1
, tree t2
)
876 return types_compatible_p (t1
, t2
);
879 /* Return if T has a single use. For GIMPLE, we also allow any
880 non-SSA_NAME (ie constants) and zero uses to cope with uses
881 that aren't linked up yet. */
886 return TREE_CODE (t
) != SSA_NAME
|| has_zero_uses (t
) || has_single_use (t
);