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
,
604 gimple_seq
*seq
, tree (*valueize
)(tree
))
606 switch (gimple_code (stmt
))
610 enum tree_code code
= gimple_assign_rhs_code (stmt
);
611 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
612 switch (gimple_assign_rhs_class (stmt
))
614 case GIMPLE_SINGLE_RHS
:
615 if (code
== REALPART_EXPR
616 || code
== IMAGPART_EXPR
617 || code
== VIEW_CONVERT_EXPR
)
619 tree op0
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
620 if (valueize
&& TREE_CODE (op0
) == SSA_NAME
)
622 tree tem
= valueize (op0
);
628 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
630 else if (code
== BIT_FIELD_REF
)
632 tree rhs1
= gimple_assign_rhs1 (stmt
);
633 tree op0
= TREE_OPERAND (rhs1
, 0);
634 if (valueize
&& TREE_CODE (op0
) == SSA_NAME
)
636 tree tem
= valueize (op0
);
642 ops
[1] = TREE_OPERAND (rhs1
, 1);
643 ops
[2] = TREE_OPERAND (rhs1
, 2);
644 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
646 else if (code
== SSA_NAME
649 tree op0
= gimple_assign_rhs1 (stmt
);
650 tree valueized
= valueize (op0
);
651 if (!valueized
|| op0
== valueized
)
654 *rcode
= TREE_CODE (op0
);
658 case GIMPLE_UNARY_RHS
:
660 tree rhs1
= gimple_assign_rhs1 (stmt
);
661 if (valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
663 tree tem
= valueize (rhs1
);
669 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
671 case GIMPLE_BINARY_RHS
:
673 tree rhs1
= gimple_assign_rhs1 (stmt
);
674 if (valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
676 tree tem
= valueize (rhs1
);
680 tree rhs2
= gimple_assign_rhs2 (stmt
);
681 if (valueize
&& TREE_CODE (rhs2
) == SSA_NAME
)
683 tree tem
= valueize (rhs2
);
690 return gimple_resimplify2 (seq
, rcode
, type
, ops
, valueize
);
692 case GIMPLE_TERNARY_RHS
:
694 tree rhs1
= gimple_assign_rhs1 (stmt
);
695 if (valueize
&& TREE_CODE (rhs1
) == SSA_NAME
)
697 tree tem
= valueize (rhs1
);
701 tree rhs2
= gimple_assign_rhs2 (stmt
);
702 if (valueize
&& TREE_CODE (rhs2
) == SSA_NAME
)
704 tree tem
= valueize (rhs2
);
708 tree rhs3
= gimple_assign_rhs3 (stmt
);
709 if (valueize
&& TREE_CODE (rhs3
) == SSA_NAME
)
711 tree tem
= valueize (rhs3
);
719 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
728 /* ??? This way we can't simplify calls with side-effects. */
729 if (gimple_call_lhs (stmt
) != NULL_TREE
)
731 tree fn
= gimple_call_fn (stmt
);
732 /* ??? Internal function support missing. */
735 if (valueize
&& TREE_CODE (fn
) == SSA_NAME
)
737 tree tem
= valueize (fn
);
742 || TREE_CODE (fn
) != ADDR_EXPR
743 || TREE_CODE (TREE_OPERAND (fn
, 0)) != FUNCTION_DECL
744 || DECL_BUILT_IN_CLASS (TREE_OPERAND (fn
, 0)) != BUILT_IN_NORMAL
745 || !builtin_decl_implicit (DECL_FUNCTION_CODE (TREE_OPERAND (fn
, 0)))
746 || !gimple_builtin_call_types_compatible_p (stmt
,
747 TREE_OPERAND (fn
, 0)))
750 tree decl
= TREE_OPERAND (fn
, 0);
751 tree type
= TREE_TYPE (gimple_call_lhs (stmt
));
752 switch (gimple_call_num_args (stmt
))
756 tree arg1
= gimple_call_arg (stmt
, 0);
757 if (valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
759 tree tem
= valueize (arg1
);
763 *rcode
= DECL_FUNCTION_CODE (decl
);
765 return gimple_resimplify1 (seq
, rcode
, type
, ops
, valueize
);
769 tree arg1
= gimple_call_arg (stmt
, 0);
770 if (valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
772 tree tem
= valueize (arg1
);
776 tree arg2
= gimple_call_arg (stmt
, 1);
777 if (valueize
&& TREE_CODE (arg2
) == SSA_NAME
)
779 tree tem
= valueize (arg2
);
783 *rcode
= DECL_FUNCTION_CODE (decl
);
786 return gimple_resimplify2 (seq
, rcode
, type
, ops
, valueize
);
790 tree arg1
= gimple_call_arg (stmt
, 0);
791 if (valueize
&& TREE_CODE (arg1
) == SSA_NAME
)
793 tree tem
= valueize (arg1
);
797 tree arg2
= gimple_call_arg (stmt
, 1);
798 if (valueize
&& TREE_CODE (arg2
) == SSA_NAME
)
800 tree tem
= valueize (arg2
);
804 tree arg3
= gimple_call_arg (stmt
, 2);
805 if (valueize
&& TREE_CODE (arg3
) == SSA_NAME
)
807 tree tem
= valueize (arg3
);
811 *rcode
= DECL_FUNCTION_CODE (decl
);
815 return gimple_resimplify3 (seq
, rcode
, type
, ops
, valueize
);
825 tree lhs
= gimple_cond_lhs (stmt
);
826 if (valueize
&& TREE_CODE (lhs
) == SSA_NAME
)
828 tree tem
= valueize (lhs
);
832 tree rhs
= gimple_cond_rhs (stmt
);
833 if (valueize
&& TREE_CODE (rhs
) == SSA_NAME
)
835 tree tem
= valueize (rhs
);
839 *rcode
= gimple_cond_code (stmt
);
842 return gimple_resimplify2 (seq
, rcode
, boolean_type_node
, ops
, valueize
);
853 /* Helper for the autogenerated code, valueize OP. */
856 do_valueize (tree (*valueize
)(tree
), tree op
)
858 if (valueize
&& TREE_CODE (op
) == SSA_NAME
)
859 return valueize (op
);