1 /* Statement simplification on GIMPLE.
2 Copyright (C) 2010-2013 Free Software Foundation, Inc.
3 Split out from tree-ssa-ccp.c.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
31 #include "gimple-ssa.h"
32 #include "tree-ssanames.h"
33 #include "tree-into-ssa.h"
36 #include "tree-ssa-propagate.h"
38 #include "ipa-utils.h"
39 #include "gimple-pretty-print.h"
40 #include "tree-ssa-address.h"
41 #include "langhooks.h"
43 /* Return true when DECL can be referenced from current unit.
44 FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
45 We can get declarations that are not possible to reference for various
48 1) When analyzing C++ virtual tables.
49 C++ virtual tables do have known constructors even
50 when they are keyed to other compilation unit.
51 Those tables can contain pointers to methods and vars
52 in other units. Those methods have both STATIC and EXTERNAL
54 2) In WHOPR mode devirtualization might lead to reference
55 to method that was partitioned elsehwere.
56 In this case we have static VAR_DECL or FUNCTION_DECL
57 that has no corresponding callgraph/varpool node
59 3) COMDAT functions referred by external vtables that
60 we devirtualize only during final compilation stage.
61 At this time we already decided that we will not output
62 the function body and thus we can't reference the symbol
66 can_refer_decl_in_current_unit_p (tree decl
, tree from_decl
)
68 struct varpool_node
*vnode
;
69 struct cgraph_node
*node
;
72 if (DECL_ABSTRACT (decl
))
75 /* We are concerned only about static/external vars and functions. */
76 if ((!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
77 || (TREE_CODE (decl
) != VAR_DECL
&& TREE_CODE (decl
) != FUNCTION_DECL
))
80 /* Static objects can be referred only if they was not optimized out yet. */
81 if (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
83 snode
= symtab_get_node (decl
);
86 node
= dyn_cast
<cgraph_node
> (snode
);
87 return !node
|| !node
->global
.inlined_to
;
90 /* We will later output the initializer, so we can refer to it.
91 So we are concerned only when DECL comes from initializer of
94 || TREE_CODE (from_decl
) != VAR_DECL
95 || !DECL_EXTERNAL (from_decl
)
97 && symtab_get_node (from_decl
)->in_other_partition
))
99 /* We are folding reference from external vtable. The vtable may reffer
100 to a symbol keyed to other compilation unit. The other compilation
101 unit may be in separate DSO and the symbol may be hidden. */
102 if (DECL_VISIBILITY_SPECIFIED (decl
)
103 && DECL_EXTERNAL (decl
)
104 && (!(snode
= symtab_get_node (decl
)) || !snode
->in_other_partition
))
106 /* When function is public, we always can introduce new reference.
107 Exception are the COMDAT functions where introducing a direct
108 reference imply need to include function body in the curren tunit. */
109 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
))
111 /* We are not at ltrans stage; so don't worry about WHOPR.
112 Also when still gimplifying all referred comdat functions will be
115 As observed in PR20991 for already optimized out comdat virtual functions
116 it may be tempting to not necessarily give up because the copy will be
117 output elsewhere when corresponding vtable is output.
118 This is however not possible - ABI specify that COMDATs are output in
119 units where they are used and when the other unit was compiled with LTO
120 it is possible that vtable was kept public while the function itself
122 if (!flag_ltrans
&& (!DECL_COMDAT (decl
) || !cgraph_function_flags_ready
))
125 /* OK we are seeing either COMDAT or static variable. In this case we must
126 check that the definition is still around so we can refer it. */
127 if (TREE_CODE (decl
) == FUNCTION_DECL
)
129 node
= cgraph_get_node (decl
);
130 /* Check that we still have function body and that we didn't took
131 the decision to eliminate offline copy of the function yet.
132 The second is important when devirtualization happens during final
133 compilation stage when making a new reference no longer makes callee
135 if (!node
|| !node
->definition
|| node
->global
.inlined_to
)
137 gcc_checking_assert (!TREE_ASM_WRITTEN (decl
));
141 else if (TREE_CODE (decl
) == VAR_DECL
)
143 vnode
= varpool_get_node (decl
);
144 if (!vnode
|| !vnode
->definition
)
146 gcc_checking_assert (!TREE_ASM_WRITTEN (decl
));
153 /* CVAL is value taken from DECL_INITIAL of variable. Try to transform it into
154 acceptable form for is_gimple_min_invariant.
155 FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL. */
158 canonicalize_constructor_val (tree cval
, tree from_decl
)
160 tree orig_cval
= cval
;
162 if (TREE_CODE (cval
) == POINTER_PLUS_EXPR
163 && TREE_CODE (TREE_OPERAND (cval
, 1)) == INTEGER_CST
)
165 tree ptr
= TREE_OPERAND (cval
, 0);
166 if (is_gimple_min_invariant (ptr
))
167 cval
= build1_loc (EXPR_LOCATION (cval
),
168 ADDR_EXPR
, TREE_TYPE (ptr
),
169 fold_build2 (MEM_REF
, TREE_TYPE (TREE_TYPE (ptr
)),
171 fold_convert (ptr_type_node
,
172 TREE_OPERAND (cval
, 1))));
174 if (TREE_CODE (cval
) == ADDR_EXPR
)
176 tree base
= NULL_TREE
;
177 if (TREE_CODE (TREE_OPERAND (cval
, 0)) == COMPOUND_LITERAL_EXPR
)
179 base
= COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval
, 0));
181 TREE_OPERAND (cval
, 0) = base
;
184 base
= get_base_address (TREE_OPERAND (cval
, 0));
188 if ((TREE_CODE (base
) == VAR_DECL
189 || TREE_CODE (base
) == FUNCTION_DECL
)
190 && !can_refer_decl_in_current_unit_p (base
, from_decl
))
192 if (TREE_CODE (base
) == VAR_DECL
)
193 TREE_ADDRESSABLE (base
) = 1;
194 else if (TREE_CODE (base
) == FUNCTION_DECL
)
196 /* Make sure we create a cgraph node for functions we'll reference.
197 They can be non-existent if the reference comes from an entry
198 of an external vtable for example. */
199 cgraph_get_create_real_symbol_node (base
);
201 /* Fixup types in global initializers. */
202 if (TREE_TYPE (TREE_TYPE (cval
)) != TREE_TYPE (TREE_OPERAND (cval
, 0)))
203 cval
= build_fold_addr_expr (TREE_OPERAND (cval
, 0));
205 if (!useless_type_conversion_p (TREE_TYPE (orig_cval
), TREE_TYPE (cval
)))
206 cval
= fold_convert (TREE_TYPE (orig_cval
), cval
);
209 if (TREE_OVERFLOW_P (cval
))
210 return drop_tree_overflow (cval
);
214 /* If SYM is a constant variable with known value, return the value.
215 NULL_TREE is returned otherwise. */
218 get_symbol_constant_value (tree sym
)
220 tree val
= ctor_for_folding (sym
);
221 if (val
!= error_mark_node
)
225 val
= canonicalize_constructor_val (unshare_expr (val
), sym
);
226 if (val
&& is_gimple_min_invariant (val
))
231 /* Variables declared 'const' without an initializer
232 have zero as the initializer if they may not be
233 overridden at link or run time. */
235 && (INTEGRAL_TYPE_P (TREE_TYPE (sym
))
236 || SCALAR_FLOAT_TYPE_P (TREE_TYPE (sym
))))
237 return build_zero_cst (TREE_TYPE (sym
));
245 /* Subroutine of fold_stmt. We perform several simplifications of the
246 memory reference tree EXPR and make sure to re-gimplify them properly
247 after propagation of constant addresses. IS_LHS is true if the
248 reference is supposed to be an lvalue. */
251 maybe_fold_reference (tree expr
, bool is_lhs
)
256 if ((TREE_CODE (expr
) == VIEW_CONVERT_EXPR
257 || TREE_CODE (expr
) == REALPART_EXPR
258 || TREE_CODE (expr
) == IMAGPART_EXPR
)
259 && CONSTANT_CLASS_P (TREE_OPERAND (expr
, 0)))
260 return fold_unary_loc (EXPR_LOCATION (expr
),
263 TREE_OPERAND (expr
, 0));
264 else if (TREE_CODE (expr
) == BIT_FIELD_REF
265 && CONSTANT_CLASS_P (TREE_OPERAND (expr
, 0)))
266 return fold_ternary_loc (EXPR_LOCATION (expr
),
269 TREE_OPERAND (expr
, 0),
270 TREE_OPERAND (expr
, 1),
271 TREE_OPERAND (expr
, 2));
273 while (handled_component_p (*t
))
274 t
= &TREE_OPERAND (*t
, 0);
276 /* Canonicalize MEM_REFs invariant address operand. Do this first
277 to avoid feeding non-canonical MEM_REFs elsewhere. */
278 if (TREE_CODE (*t
) == MEM_REF
279 && !is_gimple_mem_ref_addr (TREE_OPERAND (*t
, 0)))
281 bool volatile_p
= TREE_THIS_VOLATILE (*t
);
282 tree tem
= fold_binary (MEM_REF
, TREE_TYPE (*t
),
283 TREE_OPERAND (*t
, 0),
284 TREE_OPERAND (*t
, 1));
287 TREE_THIS_VOLATILE (tem
) = volatile_p
;
289 tem
= maybe_fold_reference (expr
, is_lhs
);
297 && (result
= fold_const_aggregate_ref (expr
))
298 && is_gimple_min_invariant (result
))
301 /* Fold back MEM_REFs to reference trees. */
302 if (TREE_CODE (*t
) == MEM_REF
303 && TREE_CODE (TREE_OPERAND (*t
, 0)) == ADDR_EXPR
304 && integer_zerop (TREE_OPERAND (*t
, 1))
305 && (TREE_THIS_VOLATILE (*t
)
306 == TREE_THIS_VOLATILE (TREE_OPERAND (TREE_OPERAND (*t
, 0), 0)))
307 && !TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (TREE_OPERAND (*t
, 1)))
308 && (TYPE_MAIN_VARIANT (TREE_TYPE (*t
))
309 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (*t
, 1)))))
310 /* We have to look out here to not drop a required conversion
311 from the rhs to the lhs if is_lhs, but we don't have the
312 rhs here to verify that. Thus require strict type
314 && types_compatible_p (TREE_TYPE (*t
),
315 TREE_TYPE (TREE_OPERAND
316 (TREE_OPERAND (*t
, 0), 0))))
319 *t
= TREE_OPERAND (TREE_OPERAND (*t
, 0), 0);
320 tem
= maybe_fold_reference (expr
, is_lhs
);
325 else if (TREE_CODE (*t
) == TARGET_MEM_REF
)
327 tree tem
= maybe_fold_tmr (*t
);
331 tem
= maybe_fold_reference (expr
, is_lhs
);
342 /* Attempt to fold an assignment statement pointed-to by SI. Returns a
343 replacement rhs for the statement or NULL_TREE if no simplification
344 could be made. It is assumed that the operands have been previously
348 fold_gimple_assign (gimple_stmt_iterator
*si
)
350 gimple stmt
= gsi_stmt (*si
);
351 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
352 location_t loc
= gimple_location (stmt
);
354 tree result
= NULL_TREE
;
356 switch (get_gimple_rhs_class (subcode
))
358 case GIMPLE_SINGLE_RHS
:
360 tree rhs
= gimple_assign_rhs1 (stmt
);
362 if (REFERENCE_CLASS_P (rhs
))
363 return maybe_fold_reference (rhs
, false);
365 else if (TREE_CODE (rhs
) == ADDR_EXPR
)
367 tree ref
= TREE_OPERAND (rhs
, 0);
368 tree tem
= maybe_fold_reference (ref
, true);
370 && TREE_CODE (tem
) == MEM_REF
371 && integer_zerop (TREE_OPERAND (tem
, 1)))
372 result
= fold_convert (TREE_TYPE (rhs
), TREE_OPERAND (tem
, 0));
374 result
= fold_convert (TREE_TYPE (rhs
),
375 build_fold_addr_expr_loc (loc
, tem
));
376 else if (TREE_CODE (ref
) == MEM_REF
377 && integer_zerop (TREE_OPERAND (ref
, 1)))
378 result
= fold_convert (TREE_TYPE (rhs
), TREE_OPERAND (ref
, 0));
381 else if (TREE_CODE (rhs
) == CONSTRUCTOR
382 && TREE_CODE (TREE_TYPE (rhs
)) == VECTOR_TYPE
383 && (CONSTRUCTOR_NELTS (rhs
)
384 == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs
))))
386 /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */
390 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs
), i
, val
)
391 if (TREE_CODE (val
) != INTEGER_CST
392 && TREE_CODE (val
) != REAL_CST
393 && TREE_CODE (val
) != FIXED_CST
)
396 return build_vector_from_ctor (TREE_TYPE (rhs
),
397 CONSTRUCTOR_ELTS (rhs
));
400 else if (DECL_P (rhs
))
401 return get_symbol_constant_value (rhs
);
403 /* If we couldn't fold the RHS, hand over to the generic
405 if (result
== NULL_TREE
)
408 /* Strip away useless type conversions. Both the NON_LVALUE_EXPR
409 that may have been added by fold, and "useless" type
410 conversions that might now be apparent due to propagation. */
411 STRIP_USELESS_TYPE_CONVERSION (result
);
413 if (result
!= rhs
&& valid_gimple_rhs_p (result
))
420 case GIMPLE_UNARY_RHS
:
422 tree rhs
= gimple_assign_rhs1 (stmt
);
424 result
= fold_unary_loc (loc
, subcode
, gimple_expr_type (stmt
), rhs
);
427 /* If the operation was a conversion do _not_ mark a
428 resulting constant with TREE_OVERFLOW if the original
429 constant was not. These conversions have implementation
430 defined behavior and retaining the TREE_OVERFLOW flag
431 here would confuse later passes such as VRP. */
432 if (CONVERT_EXPR_CODE_P (subcode
)
433 && TREE_CODE (result
) == INTEGER_CST
434 && TREE_CODE (rhs
) == INTEGER_CST
)
435 TREE_OVERFLOW (result
) = TREE_OVERFLOW (rhs
);
437 STRIP_USELESS_TYPE_CONVERSION (result
);
438 if (valid_gimple_rhs_p (result
))
444 case GIMPLE_BINARY_RHS
:
445 /* Try to canonicalize for boolean-typed X the comparisons
446 X == 0, X == 1, X != 0, and X != 1. */
447 if (gimple_assign_rhs_code (stmt
) == EQ_EXPR
448 || gimple_assign_rhs_code (stmt
) == NE_EXPR
)
450 tree lhs
= gimple_assign_lhs (stmt
);
451 tree op1
= gimple_assign_rhs1 (stmt
);
452 tree op2
= gimple_assign_rhs2 (stmt
);
453 tree type
= TREE_TYPE (op1
);
455 /* Check whether the comparison operands are of the same boolean
456 type as the result type is.
457 Check that second operand is an integer-constant with value
459 if (TREE_CODE (op2
) == INTEGER_CST
460 && (integer_zerop (op2
) || integer_onep (op2
))
461 && useless_type_conversion_p (TREE_TYPE (lhs
), type
))
463 enum tree_code cmp_code
= gimple_assign_rhs_code (stmt
);
464 bool is_logical_not
= false;
466 /* X == 0 and X != 1 is a logical-not.of X
467 X == 1 and X != 0 is X */
468 if ((cmp_code
== EQ_EXPR
&& integer_zerop (op2
))
469 || (cmp_code
== NE_EXPR
&& integer_onep (op2
)))
470 is_logical_not
= true;
472 if (is_logical_not
== false)
474 /* Only for one-bit precision typed X the transformation
475 !X -> ~X is valied. */
476 else if (TYPE_PRECISION (type
) == 1)
477 result
= build1_loc (gimple_location (stmt
), BIT_NOT_EXPR
,
479 /* Otherwise we use !X -> X ^ 1. */
481 result
= build2_loc (gimple_location (stmt
), BIT_XOR_EXPR
,
482 type
, op1
, build_int_cst (type
, 1));
488 result
= fold_binary_loc (loc
, subcode
,
489 TREE_TYPE (gimple_assign_lhs (stmt
)),
490 gimple_assign_rhs1 (stmt
),
491 gimple_assign_rhs2 (stmt
));
495 STRIP_USELESS_TYPE_CONVERSION (result
);
496 if (valid_gimple_rhs_p (result
))
501 case GIMPLE_TERNARY_RHS
:
502 /* Try to fold a conditional expression. */
503 if (gimple_assign_rhs_code (stmt
) == COND_EXPR
)
505 tree op0
= gimple_assign_rhs1 (stmt
);
508 location_t cond_loc
= gimple_location (stmt
);
510 if (COMPARISON_CLASS_P (op0
))
512 fold_defer_overflow_warnings ();
513 tem
= fold_binary_loc (cond_loc
,
514 TREE_CODE (op0
), TREE_TYPE (op0
),
515 TREE_OPERAND (op0
, 0),
516 TREE_OPERAND (op0
, 1));
517 /* This is actually a conditional expression, not a GIMPLE
518 conditional statement, however, the valid_gimple_rhs_p
519 test still applies. */
520 set
= (tem
&& is_gimple_condexpr (tem
)
521 && valid_gimple_rhs_p (tem
));
522 fold_undefer_overflow_warnings (set
, stmt
, 0);
524 else if (is_gimple_min_invariant (op0
))
533 result
= fold_build3_loc (cond_loc
, COND_EXPR
,
534 TREE_TYPE (gimple_assign_lhs (stmt
)), tem
,
535 gimple_assign_rhs2 (stmt
),
536 gimple_assign_rhs3 (stmt
));
540 result
= fold_ternary_loc (loc
, subcode
,
541 TREE_TYPE (gimple_assign_lhs (stmt
)),
542 gimple_assign_rhs1 (stmt
),
543 gimple_assign_rhs2 (stmt
),
544 gimple_assign_rhs3 (stmt
));
548 STRIP_USELESS_TYPE_CONVERSION (result
);
549 if (valid_gimple_rhs_p (result
))
554 case GIMPLE_INVALID_RHS
:
561 /* Attempt to fold a conditional statement. Return true if any changes were
562 made. We only attempt to fold the condition expression, and do not perform
563 any transformation that would require alteration of the cfg. It is
564 assumed that the operands have been previously folded. */
567 fold_gimple_cond (gimple stmt
)
569 tree result
= fold_binary_loc (gimple_location (stmt
),
570 gimple_cond_code (stmt
),
572 gimple_cond_lhs (stmt
),
573 gimple_cond_rhs (stmt
));
577 STRIP_USELESS_TYPE_CONVERSION (result
);
578 if (is_gimple_condexpr (result
) && valid_gimple_rhs_p (result
))
580 gimple_cond_set_condition_from_tree (stmt
, result
);
588 /* Convert EXPR into a GIMPLE value suitable for substitution on the
589 RHS of an assignment. Insert the necessary statements before
590 iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL
591 is replaced. If the call is expected to produces a result, then it
592 is replaced by an assignment of the new RHS to the result variable.
593 If the result is to be ignored, then the call is replaced by a
594 GIMPLE_NOP. A proper VDEF chain is retained by making the first
595 VUSE and the last VDEF of the whole sequence be the same as the replaced
596 statement and using new SSA names for stores in between. */
599 gimplify_and_update_call_from_tree (gimple_stmt_iterator
*si_p
, tree expr
)
602 gimple stmt
, new_stmt
;
603 gimple_stmt_iterator i
;
604 gimple_seq stmts
= NULL
;
605 struct gimplify_ctx gctx
;
609 stmt
= gsi_stmt (*si_p
);
611 gcc_assert (is_gimple_call (stmt
));
613 push_gimplify_context (&gctx
);
614 gctx
.into_ssa
= gimple_in_ssa_p (cfun
);
616 lhs
= gimple_call_lhs (stmt
);
617 if (lhs
== NULL_TREE
)
619 gimplify_and_add (expr
, &stmts
);
620 /* We can end up with folding a memcpy of an empty class assignment
621 which gets optimized away by C++ gimplification. */
622 if (gimple_seq_empty_p (stmts
))
624 pop_gimplify_context (NULL
);
625 if (gimple_in_ssa_p (cfun
))
627 unlink_stmt_vdef (stmt
);
630 gsi_replace (si_p
, gimple_build_nop (), true);
636 tree tmp
= get_initialized_tmp_var (expr
, &stmts
, NULL
);
637 new_stmt
= gimple_build_assign (lhs
, tmp
);
638 i
= gsi_last (stmts
);
639 gsi_insert_after_without_update (&i
, new_stmt
,
640 GSI_CONTINUE_LINKING
);
643 pop_gimplify_context (NULL
);
645 if (gimple_has_location (stmt
))
646 annotate_all_with_location (stmts
, gimple_location (stmt
));
648 /* First iterate over the replacement statements backward, assigning
649 virtual operands to their defining statements. */
651 for (i
= gsi_last (stmts
); !gsi_end_p (i
); gsi_prev (&i
))
653 new_stmt
= gsi_stmt (i
);
654 if ((gimple_assign_single_p (new_stmt
)
655 && !is_gimple_reg (gimple_assign_lhs (new_stmt
)))
656 || (is_gimple_call (new_stmt
)
657 && (gimple_call_flags (new_stmt
)
658 & (ECF_NOVOPS
| ECF_PURE
| ECF_CONST
| ECF_NORETURN
)) == 0))
662 vdef
= gimple_vdef (stmt
);
664 vdef
= make_ssa_name (gimple_vop (cfun
), new_stmt
);
665 gimple_set_vdef (new_stmt
, vdef
);
666 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
667 SSA_NAME_DEF_STMT (vdef
) = new_stmt
;
668 laststore
= new_stmt
;
672 /* Second iterate over the statements forward, assigning virtual
673 operands to their uses. */
674 reaching_vuse
= gimple_vuse (stmt
);
675 for (i
= gsi_start (stmts
); !gsi_end_p (i
); gsi_next (&i
))
677 new_stmt
= gsi_stmt (i
);
678 /* If the new statement possibly has a VUSE, update it with exact SSA
679 name we know will reach this one. */
680 if (gimple_has_mem_ops (new_stmt
))
681 gimple_set_vuse (new_stmt
, reaching_vuse
);
682 gimple_set_modified (new_stmt
, true);
683 if (gimple_vdef (new_stmt
))
684 reaching_vuse
= gimple_vdef (new_stmt
);
687 /* If the new sequence does not do a store release the virtual
688 definition of the original statement. */
690 && reaching_vuse
== gimple_vuse (stmt
))
692 tree vdef
= gimple_vdef (stmt
);
694 && TREE_CODE (vdef
) == SSA_NAME
)
696 unlink_stmt_vdef (stmt
);
697 release_ssa_name (vdef
);
701 /* Finally replace the original statement with the sequence. */
702 gsi_replace_with_seq (si_p
, stmts
, false);
705 /* Return the string length, maximum string length or maximum value of
707 If ARG is an SSA name variable, follow its use-def chains. If LENGTH
708 is not NULL and, for TYPE == 0, its value is not equal to the length
709 we determine or if we are unable to determine the length or value,
710 return false. VISITED is a bitmap of visited variables.
711 TYPE is 0 if string length should be returned, 1 for maximum string
712 length and 2 for maximum value ARG can have. */
715 get_maxval_strlen (tree arg
, tree
*length
, bitmap visited
, int type
)
720 if (TREE_CODE (arg
) != SSA_NAME
)
722 /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */
723 if (TREE_CODE (arg
) == ADDR_EXPR
724 && TREE_CODE (TREE_OPERAND (arg
, 0)) == ARRAY_REF
725 && integer_zerop (TREE_OPERAND (TREE_OPERAND (arg
, 0), 1)))
727 tree aop0
= TREE_OPERAND (TREE_OPERAND (arg
, 0), 0);
728 if (TREE_CODE (aop0
) == INDIRECT_REF
729 && TREE_CODE (TREE_OPERAND (aop0
, 0)) == SSA_NAME
)
730 return get_maxval_strlen (TREE_OPERAND (aop0
, 0),
731 length
, visited
, type
);
737 if (TREE_CODE (val
) != INTEGER_CST
738 || tree_int_cst_sgn (val
) < 0)
742 val
= c_strlen (arg
, 1);
750 if (TREE_CODE (*length
) != INTEGER_CST
751 || TREE_CODE (val
) != INTEGER_CST
)
754 if (tree_int_cst_lt (*length
, val
))
758 else if (simple_cst_equal (val
, *length
) != 1)
766 /* If ARG is registered for SSA update we cannot look at its defining
768 if (name_registered_for_update_p (arg
))
771 /* If we were already here, break the infinite cycle. */
772 if (!bitmap_set_bit (visited
, SSA_NAME_VERSION (arg
)))
776 def_stmt
= SSA_NAME_DEF_STMT (var
);
778 switch (gimple_code (def_stmt
))
781 /* The RHS of the statement defining VAR must either have a
782 constant length or come from another SSA_NAME with a constant
784 if (gimple_assign_single_p (def_stmt
)
785 || gimple_assign_unary_nop_p (def_stmt
))
787 tree rhs
= gimple_assign_rhs1 (def_stmt
);
788 return get_maxval_strlen (rhs
, length
, visited
, type
);
790 else if (gimple_assign_rhs_code (def_stmt
) == COND_EXPR
)
792 tree op2
= gimple_assign_rhs2 (def_stmt
);
793 tree op3
= gimple_assign_rhs3 (def_stmt
);
794 return get_maxval_strlen (op2
, length
, visited
, type
)
795 && get_maxval_strlen (op3
, length
, visited
, type
);
801 /* All the arguments of the PHI node must have the same constant
805 for (i
= 0; i
< gimple_phi_num_args (def_stmt
); i
++)
807 tree arg
= gimple_phi_arg (def_stmt
, i
)->def
;
809 /* If this PHI has itself as an argument, we cannot
810 determine the string length of this argument. However,
811 if we can find a constant string length for the other
812 PHI args then we can still be sure that this is a
813 constant string length. So be optimistic and just
814 continue with the next argument. */
815 if (arg
== gimple_phi_result (def_stmt
))
818 if (!get_maxval_strlen (arg
, length
, visited
, type
))
830 /* Fold builtin call in statement STMT. Returns a simplified tree.
831 We may return a non-constant expression, including another call
832 to a different function and with different arguments, e.g.,
833 substituting memcpy for strcpy when the string length is known.
834 Note that some builtins expand into inline code that may not
835 be valid in GIMPLE. Callers must take care. */
838 gimple_fold_builtin (gimple stmt
)
846 location_t loc
= gimple_location (stmt
);
848 gcc_assert (is_gimple_call (stmt
));
850 ignore
= (gimple_call_lhs (stmt
) == NULL
);
852 /* First try the generic builtin folder. If that succeeds, return the
854 result
= fold_call_stmt (stmt
, ignore
);
862 /* Ignore MD builtins. */
863 callee
= gimple_call_fndecl (stmt
);
864 if (DECL_BUILT_IN_CLASS (callee
) == BUILT_IN_MD
)
867 /* Give up for always_inline inline builtins until they are
869 if (avoid_folding_inline_builtin (callee
))
872 /* If the builtin could not be folded, and it has no argument list,
874 nargs
= gimple_call_num_args (stmt
);
878 /* Limit the work only for builtins we know how to simplify. */
879 switch (DECL_FUNCTION_CODE (callee
))
881 case BUILT_IN_STRLEN
:
883 case BUILT_IN_FPUTS_UNLOCKED
:
887 case BUILT_IN_STRCPY
:
888 case BUILT_IN_STRNCPY
:
892 case BUILT_IN_MEMCPY_CHK
:
893 case BUILT_IN_MEMPCPY_CHK
:
894 case BUILT_IN_MEMMOVE_CHK
:
895 case BUILT_IN_MEMSET_CHK
:
896 case BUILT_IN_STRNCPY_CHK
:
897 case BUILT_IN_STPNCPY_CHK
:
901 case BUILT_IN_STRCPY_CHK
:
902 case BUILT_IN_STPCPY_CHK
:
906 case BUILT_IN_SNPRINTF_CHK
:
907 case BUILT_IN_VSNPRINTF_CHK
:
915 if (arg_idx
>= nargs
)
918 /* Try to use the dataflow information gathered by the CCP process. */
919 visited
= BITMAP_ALLOC (NULL
);
920 bitmap_clear (visited
);
922 memset (val
, 0, sizeof (val
));
923 a
= gimple_call_arg (stmt
, arg_idx
);
924 if (!get_maxval_strlen (a
, &val
[arg_idx
], visited
, type
))
925 val
[arg_idx
] = NULL_TREE
;
927 BITMAP_FREE (visited
);
930 switch (DECL_FUNCTION_CODE (callee
))
932 case BUILT_IN_STRLEN
:
933 if (val
[0] && nargs
== 1)
936 fold_convert (TREE_TYPE (gimple_call_lhs (stmt
)), val
[0]);
938 /* If the result is not a valid gimple value, or not a cast
939 of a valid gimple value, then we cannot use the result. */
940 if (is_gimple_val (new_val
)
941 || (CONVERT_EXPR_P (new_val
)
942 && is_gimple_val (TREE_OPERAND (new_val
, 0))))
947 case BUILT_IN_STRCPY
:
948 if (val
[1] && is_gimple_val (val
[1]) && nargs
== 2)
949 result
= fold_builtin_strcpy (loc
, callee
,
950 gimple_call_arg (stmt
, 0),
951 gimple_call_arg (stmt
, 1),
955 case BUILT_IN_STRNCPY
:
956 if (val
[1] && is_gimple_val (val
[1]) && nargs
== 3)
957 result
= fold_builtin_strncpy (loc
, callee
,
958 gimple_call_arg (stmt
, 0),
959 gimple_call_arg (stmt
, 1),
960 gimple_call_arg (stmt
, 2),
966 result
= fold_builtin_fputs (loc
, gimple_call_arg (stmt
, 0),
967 gimple_call_arg (stmt
, 1),
968 ignore
, false, val
[0]);
971 case BUILT_IN_FPUTS_UNLOCKED
:
973 result
= fold_builtin_fputs (loc
, gimple_call_arg (stmt
, 0),
974 gimple_call_arg (stmt
, 1),
975 ignore
, true, val
[0]);
978 case BUILT_IN_MEMCPY_CHK
:
979 case BUILT_IN_MEMPCPY_CHK
:
980 case BUILT_IN_MEMMOVE_CHK
:
981 case BUILT_IN_MEMSET_CHK
:
982 if (val
[2] && is_gimple_val (val
[2]) && nargs
== 4)
983 result
= fold_builtin_memory_chk (loc
, callee
,
984 gimple_call_arg (stmt
, 0),
985 gimple_call_arg (stmt
, 1),
986 gimple_call_arg (stmt
, 2),
987 gimple_call_arg (stmt
, 3),
989 DECL_FUNCTION_CODE (callee
));
992 case BUILT_IN_STRCPY_CHK
:
993 case BUILT_IN_STPCPY_CHK
:
994 if (val
[1] && is_gimple_val (val
[1]) && nargs
== 3)
995 result
= fold_builtin_stxcpy_chk (loc
, callee
,
996 gimple_call_arg (stmt
, 0),
997 gimple_call_arg (stmt
, 1),
998 gimple_call_arg (stmt
, 2),
1000 DECL_FUNCTION_CODE (callee
));
1003 case BUILT_IN_STRNCPY_CHK
:
1004 case BUILT_IN_STPNCPY_CHK
:
1005 if (val
[2] && is_gimple_val (val
[2]) && nargs
== 4)
1006 result
= fold_builtin_stxncpy_chk (loc
, gimple_call_arg (stmt
, 0),
1007 gimple_call_arg (stmt
, 1),
1008 gimple_call_arg (stmt
, 2),
1009 gimple_call_arg (stmt
, 3),
1011 DECL_FUNCTION_CODE (callee
));
1014 case BUILT_IN_SNPRINTF_CHK
:
1015 case BUILT_IN_VSNPRINTF_CHK
:
1016 if (val
[1] && is_gimple_val (val
[1]))
1017 result
= gimple_fold_builtin_snprintf_chk (stmt
, val
[1],
1018 DECL_FUNCTION_CODE (callee
));
1025 if (result
&& ignore
)
1026 result
= fold_ignored_result (result
);
1031 /* Return a binfo to be used for devirtualization of calls based on an object
1032 represented by a declaration (i.e. a global or automatically allocated one)
1033 or NULL if it cannot be found or is not safe. CST is expected to be an
1034 ADDR_EXPR of such object or the function will return NULL. Currently it is
1035 safe to use such binfo only if it has no base binfo (i.e. no ancestors)
1036 EXPECTED_TYPE is type of the class virtual belongs to. */
1039 gimple_extract_devirt_binfo_from_cst (tree cst
, tree expected_type
)
1041 HOST_WIDE_INT offset
, size
, max_size
;
1042 tree base
, type
, binfo
;
1043 bool last_artificial
= false;
1045 if (!flag_devirtualize
1046 || TREE_CODE (cst
) != ADDR_EXPR
1047 || TREE_CODE (TREE_TYPE (TREE_TYPE (cst
))) != RECORD_TYPE
)
1050 cst
= TREE_OPERAND (cst
, 0);
1051 base
= get_ref_base_and_extent (cst
, &offset
, &size
, &max_size
);
1052 type
= TREE_TYPE (base
);
1056 || TREE_CODE (type
) != RECORD_TYPE
)
1059 /* Find the sub-object the constant actually refers to and mark whether it is
1060 an artificial one (as opposed to a user-defined one). */
1063 HOST_WIDE_INT pos
, size
;
1066 if (types_same_for_odr (type
, expected_type
))
1071 for (fld
= TYPE_FIELDS (type
); fld
; fld
= DECL_CHAIN (fld
))
1073 if (TREE_CODE (fld
) != FIELD_DECL
)
1076 pos
= int_bit_position (fld
);
1077 size
= tree_low_cst (DECL_SIZE (fld
), 1);
1078 if (pos
<= offset
&& (pos
+ size
) > offset
)
1081 if (!fld
|| TREE_CODE (TREE_TYPE (fld
)) != RECORD_TYPE
)
1084 last_artificial
= DECL_ARTIFICIAL (fld
);
1085 type
= TREE_TYPE (fld
);
1088 /* Artificial sub-objects are ancestors, we do not want to use them for
1089 devirtualization, at least not here. */
1090 if (last_artificial
)
1092 binfo
= TYPE_BINFO (type
);
1093 if (!binfo
|| BINFO_N_BASE_BINFOS (binfo
) > 0)
1099 /* Attempt to fold a call statement referenced by the statement iterator GSI.
1100 The statement may be replaced by another statement, e.g., if the call
1101 simplifies to a constant value. Return true if any changes were made.
1102 It is assumed that the operands have been previously folded. */
1105 gimple_fold_call (gimple_stmt_iterator
*gsi
, bool inplace
)
1107 gimple stmt
= gsi_stmt (*gsi
);
1109 bool changed
= false;
1112 /* Fold *& in call arguments. */
1113 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
1114 if (REFERENCE_CLASS_P (gimple_call_arg (stmt
, i
)))
1116 tree tmp
= maybe_fold_reference (gimple_call_arg (stmt
, i
), false);
1119 gimple_call_set_arg (stmt
, i
, tmp
);
1124 /* Check for virtual calls that became direct calls. */
1125 callee
= gimple_call_fn (stmt
);
1126 if (callee
&& TREE_CODE (callee
) == OBJ_TYPE_REF
)
1128 if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee
)) != NULL_TREE
)
1130 if (dump_file
&& virtual_method_call_p (callee
)
1131 && !possible_polymorphic_call_target_p
1132 (callee
, cgraph_get_node (gimple_call_addr_fndecl
1133 (OBJ_TYPE_REF_EXPR (callee
)))))
1136 "Type inheritnace inconsistent devirtualization of ");
1137 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1138 fprintf (dump_file
, " to ");
1139 print_generic_expr (dump_file
, callee
, TDF_SLIM
);
1140 fprintf (dump_file
, "\n");
1143 gimple_call_set_fn (stmt
, OBJ_TYPE_REF_EXPR (callee
));
1146 else if (virtual_method_call_p (callee
))
1148 tree obj
= OBJ_TYPE_REF_OBJECT (callee
);
1149 tree binfo
= gimple_extract_devirt_binfo_from_cst
1150 (obj
, obj_type_ref_class (callee
));
1154 = TREE_INT_CST_LOW (OBJ_TYPE_REF_TOKEN (callee
));
1155 tree fndecl
= gimple_get_virt_method_for_binfo (token
, binfo
);
1158 #ifdef ENABLE_CHECKING
1159 gcc_assert (possible_polymorphic_call_target_p
1160 (callee
, cgraph_get_node (fndecl
)));
1163 gimple_call_set_fndecl (stmt
, fndecl
);
1173 /* Check for builtins that CCP can handle using information not
1174 available in the generic fold routines. */
1175 callee
= gimple_call_fndecl (stmt
);
1176 if (callee
&& DECL_BUILT_IN (callee
))
1178 tree result
= gimple_fold_builtin (stmt
);
1181 if (!update_call_from_tree (gsi
, result
))
1182 gimplify_and_update_call_from_tree (gsi
, result
);
1185 else if (DECL_BUILT_IN_CLASS (callee
) == BUILT_IN_MD
)
1186 changed
|= targetm
.gimple_fold_builtin (gsi
);
1192 /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument
1193 distinguishes both cases. */
1196 fold_stmt_1 (gimple_stmt_iterator
*gsi
, bool inplace
)
1198 bool changed
= false;
1199 gimple stmt
= gsi_stmt (*gsi
);
1202 /* Fold the main computation performed by the statement. */
1203 switch (gimple_code (stmt
))
1207 unsigned old_num_ops
= gimple_num_ops (stmt
);
1208 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
1209 tree lhs
= gimple_assign_lhs (stmt
);
1211 /* First canonicalize operand order. This avoids building new
1212 trees if this is the only thing fold would later do. */
1213 if ((commutative_tree_code (subcode
)
1214 || commutative_ternary_tree_code (subcode
))
1215 && tree_swap_operands_p (gimple_assign_rhs1 (stmt
),
1216 gimple_assign_rhs2 (stmt
), false))
1218 tree tem
= gimple_assign_rhs1 (stmt
);
1219 gimple_assign_set_rhs1 (stmt
, gimple_assign_rhs2 (stmt
));
1220 gimple_assign_set_rhs2 (stmt
, tem
);
1223 new_rhs
= fold_gimple_assign (gsi
);
1225 && !useless_type_conversion_p (TREE_TYPE (lhs
),
1226 TREE_TYPE (new_rhs
)))
1227 new_rhs
= fold_convert (TREE_TYPE (lhs
), new_rhs
);
1230 || get_gimple_rhs_num_ops (TREE_CODE (new_rhs
)) < old_num_ops
))
1232 gimple_assign_set_rhs_from_tree (gsi
, new_rhs
);
1239 changed
|= fold_gimple_cond (stmt
);
1243 changed
|= gimple_fold_call (gsi
, inplace
);
1247 /* Fold *& in asm operands. */
1250 const char **oconstraints
;
1251 const char *constraint
;
1252 bool allows_mem
, allows_reg
;
1254 noutputs
= gimple_asm_noutputs (stmt
);
1255 oconstraints
= XALLOCAVEC (const char *, noutputs
);
1257 for (i
= 0; i
< gimple_asm_noutputs (stmt
); ++i
)
1259 tree link
= gimple_asm_output_op (stmt
, i
);
1260 tree op
= TREE_VALUE (link
);
1262 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
1263 if (REFERENCE_CLASS_P (op
)
1264 && (op
= maybe_fold_reference (op
, true)) != NULL_TREE
)
1266 TREE_VALUE (link
) = op
;
1270 for (i
= 0; i
< gimple_asm_ninputs (stmt
); ++i
)
1272 tree link
= gimple_asm_input_op (stmt
, i
);
1273 tree op
= TREE_VALUE (link
);
1275 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
1276 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0,
1277 oconstraints
, &allows_mem
, &allows_reg
);
1278 if (REFERENCE_CLASS_P (op
)
1279 && (op
= maybe_fold_reference (op
, !allows_reg
&& allows_mem
))
1282 TREE_VALUE (link
) = op
;
1290 if (gimple_debug_bind_p (stmt
))
1292 tree val
= gimple_debug_bind_get_value (stmt
);
1294 && REFERENCE_CLASS_P (val
))
1296 tree tem
= maybe_fold_reference (val
, false);
1299 gimple_debug_bind_set_value (stmt
, tem
);
1304 && TREE_CODE (val
) == ADDR_EXPR
)
1306 tree ref
= TREE_OPERAND (val
, 0);
1307 tree tem
= maybe_fold_reference (ref
, false);
1310 tem
= build_fold_addr_expr_with_type (tem
, TREE_TYPE (val
));
1311 gimple_debug_bind_set_value (stmt
, tem
);
1321 stmt
= gsi_stmt (*gsi
);
1323 /* Fold *& on the lhs. */
1324 if (gimple_has_lhs (stmt
))
1326 tree lhs
= gimple_get_lhs (stmt
);
1327 if (lhs
&& REFERENCE_CLASS_P (lhs
))
1329 tree new_lhs
= maybe_fold_reference (lhs
, true);
1332 gimple_set_lhs (stmt
, new_lhs
);
1341 /* Fold the statement pointed to by GSI. In some cases, this function may
1342 replace the whole statement with a new one. Returns true iff folding
1344 The statement pointed to by GSI should be in valid gimple form but may
1345 be in unfolded state as resulting from for example constant propagation
1346 which can produce *&x = 0. */
1349 fold_stmt (gimple_stmt_iterator
*gsi
)
1351 return fold_stmt_1 (gsi
, false);
1354 /* Perform the minimal folding on statement *GSI. Only operations like
1355 *&x created by constant propagation are handled. The statement cannot
1356 be replaced with a new one. Return true if the statement was
1357 changed, false otherwise.
1358 The statement *GSI should be in valid gimple form but may
1359 be in unfolded state as resulting from for example constant propagation
1360 which can produce *&x = 0. */
1363 fold_stmt_inplace (gimple_stmt_iterator
*gsi
)
1365 gimple stmt
= gsi_stmt (*gsi
);
1366 bool changed
= fold_stmt_1 (gsi
, true);
1367 gcc_assert (gsi_stmt (*gsi
) == stmt
);
1371 /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
1372 if EXPR is null or we don't know how.
1373 If non-null, the result always has boolean type. */
1376 canonicalize_bool (tree expr
, bool invert
)
1382 if (integer_nonzerop (expr
))
1383 return boolean_false_node
;
1384 else if (integer_zerop (expr
))
1385 return boolean_true_node
;
1386 else if (TREE_CODE (expr
) == SSA_NAME
)
1387 return fold_build2 (EQ_EXPR
, boolean_type_node
, expr
,
1388 build_int_cst (TREE_TYPE (expr
), 0));
1389 else if (TREE_CODE_CLASS (TREE_CODE (expr
)) == tcc_comparison
)
1390 return fold_build2 (invert_tree_comparison (TREE_CODE (expr
), false),
1392 TREE_OPERAND (expr
, 0),
1393 TREE_OPERAND (expr
, 1));
1399 if (TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
)
1401 if (integer_nonzerop (expr
))
1402 return boolean_true_node
;
1403 else if (integer_zerop (expr
))
1404 return boolean_false_node
;
1405 else if (TREE_CODE (expr
) == SSA_NAME
)
1406 return fold_build2 (NE_EXPR
, boolean_type_node
, expr
,
1407 build_int_cst (TREE_TYPE (expr
), 0));
1408 else if (TREE_CODE_CLASS (TREE_CODE (expr
)) == tcc_comparison
)
1409 return fold_build2 (TREE_CODE (expr
),
1411 TREE_OPERAND (expr
, 0),
1412 TREE_OPERAND (expr
, 1));
1418 /* Check to see if a boolean expression EXPR is logically equivalent to the
1419 comparison (OP1 CODE OP2). Check for various identities involving
1423 same_bool_comparison_p (const_tree expr
, enum tree_code code
,
1424 const_tree op1
, const_tree op2
)
1428 /* The obvious case. */
1429 if (TREE_CODE (expr
) == code
1430 && operand_equal_p (TREE_OPERAND (expr
, 0), op1
, 0)
1431 && operand_equal_p (TREE_OPERAND (expr
, 1), op2
, 0))
1434 /* Check for comparing (name, name != 0) and the case where expr
1435 is an SSA_NAME with a definition matching the comparison. */
1436 if (TREE_CODE (expr
) == SSA_NAME
1437 && TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
)
1439 if (operand_equal_p (expr
, op1
, 0))
1440 return ((code
== NE_EXPR
&& integer_zerop (op2
))
1441 || (code
== EQ_EXPR
&& integer_nonzerop (op2
)));
1442 s
= SSA_NAME_DEF_STMT (expr
);
1443 if (is_gimple_assign (s
)
1444 && gimple_assign_rhs_code (s
) == code
1445 && operand_equal_p (gimple_assign_rhs1 (s
), op1
, 0)
1446 && operand_equal_p (gimple_assign_rhs2 (s
), op2
, 0))
1450 /* If op1 is of the form (name != 0) or (name == 0), and the definition
1451 of name is a comparison, recurse. */
1452 if (TREE_CODE (op1
) == SSA_NAME
1453 && TREE_CODE (TREE_TYPE (op1
)) == BOOLEAN_TYPE
)
1455 s
= SSA_NAME_DEF_STMT (op1
);
1456 if (is_gimple_assign (s
)
1457 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
)
1459 enum tree_code c
= gimple_assign_rhs_code (s
);
1460 if ((c
== NE_EXPR
&& integer_zerop (op2
))
1461 || (c
== EQ_EXPR
&& integer_nonzerop (op2
)))
1462 return same_bool_comparison_p (expr
, c
,
1463 gimple_assign_rhs1 (s
),
1464 gimple_assign_rhs2 (s
));
1465 if ((c
== EQ_EXPR
&& integer_zerop (op2
))
1466 || (c
== NE_EXPR
&& integer_nonzerop (op2
)))
1467 return same_bool_comparison_p (expr
,
1468 invert_tree_comparison (c
, false),
1469 gimple_assign_rhs1 (s
),
1470 gimple_assign_rhs2 (s
));
1476 /* Check to see if two boolean expressions OP1 and OP2 are logically
1480 same_bool_result_p (const_tree op1
, const_tree op2
)
1482 /* Simple cases first. */
1483 if (operand_equal_p (op1
, op2
, 0))
1486 /* Check the cases where at least one of the operands is a comparison.
1487 These are a bit smarter than operand_equal_p in that they apply some
1488 identifies on SSA_NAMEs. */
1489 if (TREE_CODE_CLASS (TREE_CODE (op2
)) == tcc_comparison
1490 && same_bool_comparison_p (op1
, TREE_CODE (op2
),
1491 TREE_OPERAND (op2
, 0),
1492 TREE_OPERAND (op2
, 1)))
1494 if (TREE_CODE_CLASS (TREE_CODE (op1
)) == tcc_comparison
1495 && same_bool_comparison_p (op2
, TREE_CODE (op1
),
1496 TREE_OPERAND (op1
, 0),
1497 TREE_OPERAND (op1
, 1)))
1504 /* Forward declarations for some mutually recursive functions. */
1507 and_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
1508 enum tree_code code2
, tree op2a
, tree op2b
);
1510 and_var_with_comparison (tree var
, bool invert
,
1511 enum tree_code code2
, tree op2a
, tree op2b
);
1513 and_var_with_comparison_1 (gimple stmt
,
1514 enum tree_code code2
, tree op2a
, tree op2b
);
1516 or_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
1517 enum tree_code code2
, tree op2a
, tree op2b
);
1519 or_var_with_comparison (tree var
, bool invert
,
1520 enum tree_code code2
, tree op2a
, tree op2b
);
1522 or_var_with_comparison_1 (gimple stmt
,
1523 enum tree_code code2
, tree op2a
, tree op2b
);
1525 /* Helper function for and_comparisons_1: try to simplify the AND of the
1526 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
1527 If INVERT is true, invert the value of the VAR before doing the AND.
1528 Return NULL_EXPR if we can't simplify this to a single expression. */
1531 and_var_with_comparison (tree var
, bool invert
,
1532 enum tree_code code2
, tree op2a
, tree op2b
)
1535 gimple stmt
= SSA_NAME_DEF_STMT (var
);
1537 /* We can only deal with variables whose definitions are assignments. */
1538 if (!is_gimple_assign (stmt
))
1541 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
1542 !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
1543 Then we only have to consider the simpler non-inverted cases. */
1545 t
= or_var_with_comparison_1 (stmt
,
1546 invert_tree_comparison (code2
, false),
1549 t
= and_var_with_comparison_1 (stmt
, code2
, op2a
, op2b
);
1550 return canonicalize_bool (t
, invert
);
1553 /* Try to simplify the AND of the ssa variable defined by the assignment
1554 STMT with the comparison specified by (OP2A CODE2 OP2B).
1555 Return NULL_EXPR if we can't simplify this to a single expression. */
1558 and_var_with_comparison_1 (gimple stmt
,
1559 enum tree_code code2
, tree op2a
, tree op2b
)
1561 tree var
= gimple_assign_lhs (stmt
);
1562 tree true_test_var
= NULL_TREE
;
1563 tree false_test_var
= NULL_TREE
;
1564 enum tree_code innercode
= gimple_assign_rhs_code (stmt
);
1566 /* Check for identities like (var AND (var == 0)) => false. */
1567 if (TREE_CODE (op2a
) == SSA_NAME
1568 && TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
)
1570 if ((code2
== NE_EXPR
&& integer_zerop (op2b
))
1571 || (code2
== EQ_EXPR
&& integer_nonzerop (op2b
)))
1573 true_test_var
= op2a
;
1574 if (var
== true_test_var
)
1577 else if ((code2
== EQ_EXPR
&& integer_zerop (op2b
))
1578 || (code2
== NE_EXPR
&& integer_nonzerop (op2b
)))
1580 false_test_var
= op2a
;
1581 if (var
== false_test_var
)
1582 return boolean_false_node
;
1586 /* If the definition is a comparison, recurse on it. */
1587 if (TREE_CODE_CLASS (innercode
) == tcc_comparison
)
1589 tree t
= and_comparisons_1 (innercode
,
1590 gimple_assign_rhs1 (stmt
),
1591 gimple_assign_rhs2 (stmt
),
1599 /* If the definition is an AND or OR expression, we may be able to
1600 simplify by reassociating. */
1601 if (TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
1602 && (innercode
== BIT_AND_EXPR
|| innercode
== BIT_IOR_EXPR
))
1604 tree inner1
= gimple_assign_rhs1 (stmt
);
1605 tree inner2
= gimple_assign_rhs2 (stmt
);
1608 tree partial
= NULL_TREE
;
1609 bool is_and
= (innercode
== BIT_AND_EXPR
);
1611 /* Check for boolean identities that don't require recursive examination
1613 inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
1614 inner1 AND (inner1 OR inner2) => inner1
1615 !inner1 AND (inner1 AND inner2) => false
1616 !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
1617 Likewise for similar cases involving inner2. */
1618 if (inner1
== true_test_var
)
1619 return (is_and
? var
: inner1
);
1620 else if (inner2
== true_test_var
)
1621 return (is_and
? var
: inner2
);
1622 else if (inner1
== false_test_var
)
1624 ? boolean_false_node
1625 : and_var_with_comparison (inner2
, false, code2
, op2a
, op2b
));
1626 else if (inner2
== false_test_var
)
1628 ? boolean_false_node
1629 : and_var_with_comparison (inner1
, false, code2
, op2a
, op2b
));
1631 /* Next, redistribute/reassociate the AND across the inner tests.
1632 Compute the first partial result, (inner1 AND (op2a code op2b)) */
1633 if (TREE_CODE (inner1
) == SSA_NAME
1634 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner1
))
1635 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
1636 && (t
= maybe_fold_and_comparisons (gimple_assign_rhs_code (s
),
1637 gimple_assign_rhs1 (s
),
1638 gimple_assign_rhs2 (s
),
1639 code2
, op2a
, op2b
)))
1641 /* Handle the AND case, where we are reassociating:
1642 (inner1 AND inner2) AND (op2a code2 op2b)
1644 If the partial result t is a constant, we win. Otherwise
1645 continue on to try reassociating with the other inner test. */
1648 if (integer_onep (t
))
1650 else if (integer_zerop (t
))
1651 return boolean_false_node
;
1654 /* Handle the OR case, where we are redistributing:
1655 (inner1 OR inner2) AND (op2a code2 op2b)
1656 => (t OR (inner2 AND (op2a code2 op2b))) */
1657 else if (integer_onep (t
))
1658 return boolean_true_node
;
1660 /* Save partial result for later. */
1664 /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
1665 if (TREE_CODE (inner2
) == SSA_NAME
1666 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner2
))
1667 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
1668 && (t
= maybe_fold_and_comparisons (gimple_assign_rhs_code (s
),
1669 gimple_assign_rhs1 (s
),
1670 gimple_assign_rhs2 (s
),
1671 code2
, op2a
, op2b
)))
1673 /* Handle the AND case, where we are reassociating:
1674 (inner1 AND inner2) AND (op2a code2 op2b)
1675 => (inner1 AND t) */
1678 if (integer_onep (t
))
1680 else if (integer_zerop (t
))
1681 return boolean_false_node
;
1682 /* If both are the same, we can apply the identity
1684 else if (partial
&& same_bool_result_p (t
, partial
))
1688 /* Handle the OR case. where we are redistributing:
1689 (inner1 OR inner2) AND (op2a code2 op2b)
1690 => (t OR (inner1 AND (op2a code2 op2b)))
1691 => (t OR partial) */
1694 if (integer_onep (t
))
1695 return boolean_true_node
;
1698 /* We already got a simplification for the other
1699 operand to the redistributed OR expression. The
1700 interesting case is when at least one is false.
1701 Or, if both are the same, we can apply the identity
1703 if (integer_zerop (partial
))
1705 else if (integer_zerop (t
))
1707 else if (same_bool_result_p (t
, partial
))
1716 /* Try to simplify the AND of two comparisons defined by
1717 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
1718 If this can be done without constructing an intermediate value,
1719 return the resulting tree; otherwise NULL_TREE is returned.
1720 This function is deliberately asymmetric as it recurses on SSA_DEFs
1721 in the first comparison but not the second. */
1724 and_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
1725 enum tree_code code2
, tree op2a
, tree op2b
)
1727 tree truth_type
= truth_type_for (TREE_TYPE (op1a
));
1729 /* First check for ((x CODE1 y) AND (x CODE2 y)). */
1730 if (operand_equal_p (op1a
, op2a
, 0)
1731 && operand_equal_p (op1b
, op2b
, 0))
1733 /* Result will be either NULL_TREE, or a combined comparison. */
1734 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
1735 TRUTH_ANDIF_EXPR
, code1
, code2
,
1736 truth_type
, op1a
, op1b
);
1741 /* Likewise the swapped case of the above. */
1742 if (operand_equal_p (op1a
, op2b
, 0)
1743 && operand_equal_p (op1b
, op2a
, 0))
1745 /* Result will be either NULL_TREE, or a combined comparison. */
1746 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
1747 TRUTH_ANDIF_EXPR
, code1
,
1748 swap_tree_comparison (code2
),
1749 truth_type
, op1a
, op1b
);
1754 /* If both comparisons are of the same value against constants, we might
1755 be able to merge them. */
1756 if (operand_equal_p (op1a
, op2a
, 0)
1757 && TREE_CODE (op1b
) == INTEGER_CST
1758 && TREE_CODE (op2b
) == INTEGER_CST
)
1760 int cmp
= tree_int_cst_compare (op1b
, op2b
);
1762 /* If we have (op1a == op1b), we should either be able to
1763 return that or FALSE, depending on whether the constant op1b
1764 also satisfies the other comparison against op2b. */
1765 if (code1
== EQ_EXPR
)
1771 case EQ_EXPR
: val
= (cmp
== 0); break;
1772 case NE_EXPR
: val
= (cmp
!= 0); break;
1773 case LT_EXPR
: val
= (cmp
< 0); break;
1774 case GT_EXPR
: val
= (cmp
> 0); break;
1775 case LE_EXPR
: val
= (cmp
<= 0); break;
1776 case GE_EXPR
: val
= (cmp
>= 0); break;
1777 default: done
= false;
1782 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
1784 return boolean_false_node
;
1787 /* Likewise if the second comparison is an == comparison. */
1788 else if (code2
== EQ_EXPR
)
1794 case EQ_EXPR
: val
= (cmp
== 0); break;
1795 case NE_EXPR
: val
= (cmp
!= 0); break;
1796 case LT_EXPR
: val
= (cmp
> 0); break;
1797 case GT_EXPR
: val
= (cmp
< 0); break;
1798 case LE_EXPR
: val
= (cmp
>= 0); break;
1799 case GE_EXPR
: val
= (cmp
<= 0); break;
1800 default: done
= false;
1805 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
1807 return boolean_false_node
;
1811 /* Same business with inequality tests. */
1812 else if (code1
== NE_EXPR
)
1817 case EQ_EXPR
: val
= (cmp
!= 0); break;
1818 case NE_EXPR
: val
= (cmp
== 0); break;
1819 case LT_EXPR
: val
= (cmp
>= 0); break;
1820 case GT_EXPR
: val
= (cmp
<= 0); break;
1821 case LE_EXPR
: val
= (cmp
> 0); break;
1822 case GE_EXPR
: val
= (cmp
< 0); break;
1827 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
1829 else if (code2
== NE_EXPR
)
1834 case EQ_EXPR
: val
= (cmp
== 0); break;
1835 case NE_EXPR
: val
= (cmp
!= 0); break;
1836 case LT_EXPR
: val
= (cmp
<= 0); break;
1837 case GT_EXPR
: val
= (cmp
>= 0); break;
1838 case LE_EXPR
: val
= (cmp
< 0); break;
1839 case GE_EXPR
: val
= (cmp
> 0); break;
1844 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
1847 /* Chose the more restrictive of two < or <= comparisons. */
1848 else if ((code1
== LT_EXPR
|| code1
== LE_EXPR
)
1849 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
1851 if ((cmp
< 0) || (cmp
== 0 && code1
== LT_EXPR
))
1852 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
1854 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
1857 /* Likewise chose the more restrictive of two > or >= comparisons. */
1858 else if ((code1
== GT_EXPR
|| code1
== GE_EXPR
)
1859 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
1861 if ((cmp
> 0) || (cmp
== 0 && code1
== GT_EXPR
))
1862 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
1864 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
1867 /* Check for singleton ranges. */
1869 && ((code1
== LE_EXPR
&& code2
== GE_EXPR
)
1870 || (code1
== GE_EXPR
&& code2
== LE_EXPR
)))
1871 return fold_build2 (EQ_EXPR
, boolean_type_node
, op1a
, op2b
);
1873 /* Check for disjoint ranges. */
1875 && (code1
== LT_EXPR
|| code1
== LE_EXPR
)
1876 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
1877 return boolean_false_node
;
1879 && (code1
== GT_EXPR
|| code1
== GE_EXPR
)
1880 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
1881 return boolean_false_node
;
1884 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
1885 NAME's definition is a truth value. See if there are any simplifications
1886 that can be done against the NAME's definition. */
1887 if (TREE_CODE (op1a
) == SSA_NAME
1888 && (code1
== NE_EXPR
|| code1
== EQ_EXPR
)
1889 && (integer_zerop (op1b
) || integer_onep (op1b
)))
1891 bool invert
= ((code1
== EQ_EXPR
&& integer_zerop (op1b
))
1892 || (code1
== NE_EXPR
&& integer_onep (op1b
)));
1893 gimple stmt
= SSA_NAME_DEF_STMT (op1a
);
1894 switch (gimple_code (stmt
))
1897 /* Try to simplify by copy-propagating the definition. */
1898 return and_var_with_comparison (op1a
, invert
, code2
, op2a
, op2b
);
1901 /* If every argument to the PHI produces the same result when
1902 ANDed with the second comparison, we win.
1903 Do not do this unless the type is bool since we need a bool
1904 result here anyway. */
1905 if (TREE_CODE (TREE_TYPE (op1a
)) == BOOLEAN_TYPE
)
1907 tree result
= NULL_TREE
;
1909 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
1911 tree arg
= gimple_phi_arg_def (stmt
, i
);
1913 /* If this PHI has itself as an argument, ignore it.
1914 If all the other args produce the same result,
1916 if (arg
== gimple_phi_result (stmt
))
1918 else if (TREE_CODE (arg
) == INTEGER_CST
)
1920 if (invert
? integer_nonzerop (arg
) : integer_zerop (arg
))
1923 result
= boolean_false_node
;
1924 else if (!integer_zerop (result
))
1928 result
= fold_build2 (code2
, boolean_type_node
,
1930 else if (!same_bool_comparison_p (result
,
1934 else if (TREE_CODE (arg
) == SSA_NAME
1935 && !SSA_NAME_IS_DEFAULT_DEF (arg
))
1938 gimple def_stmt
= SSA_NAME_DEF_STMT (arg
);
1939 /* In simple cases we can look through PHI nodes,
1940 but we have to be careful with loops.
1942 if (! dom_info_available_p (CDI_DOMINATORS
)
1943 || gimple_bb (def_stmt
) == gimple_bb (stmt
)
1944 || dominated_by_p (CDI_DOMINATORS
,
1945 gimple_bb (def_stmt
),
1948 temp
= and_var_with_comparison (arg
, invert
, code2
,
1954 else if (!same_bool_result_p (result
, temp
))
1970 /* Try to simplify the AND of two comparisons, specified by
1971 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
1972 If this can be simplified to a single expression (without requiring
1973 introducing more SSA variables to hold intermediate values),
1974 return the resulting tree. Otherwise return NULL_TREE.
1975 If the result expression is non-null, it has boolean type. */
1978 maybe_fold_and_comparisons (enum tree_code code1
, tree op1a
, tree op1b
,
1979 enum tree_code code2
, tree op2a
, tree op2b
)
1981 tree t
= and_comparisons_1 (code1
, op1a
, op1b
, code2
, op2a
, op2b
);
1985 return and_comparisons_1 (code2
, op2a
, op2b
, code1
, op1a
, op1b
);
1988 /* Helper function for or_comparisons_1: try to simplify the OR of the
1989 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
1990 If INVERT is true, invert the value of VAR before doing the OR.
1991 Return NULL_EXPR if we can't simplify this to a single expression. */
1994 or_var_with_comparison (tree var
, bool invert
,
1995 enum tree_code code2
, tree op2a
, tree op2b
)
1998 gimple stmt
= SSA_NAME_DEF_STMT (var
);
2000 /* We can only deal with variables whose definitions are assignments. */
2001 if (!is_gimple_assign (stmt
))
2004 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
2005 !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
2006 Then we only have to consider the simpler non-inverted cases. */
2008 t
= and_var_with_comparison_1 (stmt
,
2009 invert_tree_comparison (code2
, false),
2012 t
= or_var_with_comparison_1 (stmt
, code2
, op2a
, op2b
);
2013 return canonicalize_bool (t
, invert
);
2016 /* Try to simplify the OR of the ssa variable defined by the assignment
2017 STMT with the comparison specified by (OP2A CODE2 OP2B).
2018 Return NULL_EXPR if we can't simplify this to a single expression. */
2021 or_var_with_comparison_1 (gimple stmt
,
2022 enum tree_code code2
, tree op2a
, tree op2b
)
2024 tree var
= gimple_assign_lhs (stmt
);
2025 tree true_test_var
= NULL_TREE
;
2026 tree false_test_var
= NULL_TREE
;
2027 enum tree_code innercode
= gimple_assign_rhs_code (stmt
);
2029 /* Check for identities like (var OR (var != 0)) => true . */
2030 if (TREE_CODE (op2a
) == SSA_NAME
2031 && TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
)
2033 if ((code2
== NE_EXPR
&& integer_zerop (op2b
))
2034 || (code2
== EQ_EXPR
&& integer_nonzerop (op2b
)))
2036 true_test_var
= op2a
;
2037 if (var
== true_test_var
)
2040 else if ((code2
== EQ_EXPR
&& integer_zerop (op2b
))
2041 || (code2
== NE_EXPR
&& integer_nonzerop (op2b
)))
2043 false_test_var
= op2a
;
2044 if (var
== false_test_var
)
2045 return boolean_true_node
;
2049 /* If the definition is a comparison, recurse on it. */
2050 if (TREE_CODE_CLASS (innercode
) == tcc_comparison
)
2052 tree t
= or_comparisons_1 (innercode
,
2053 gimple_assign_rhs1 (stmt
),
2054 gimple_assign_rhs2 (stmt
),
2062 /* If the definition is an AND or OR expression, we may be able to
2063 simplify by reassociating. */
2064 if (TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
2065 && (innercode
== BIT_AND_EXPR
|| innercode
== BIT_IOR_EXPR
))
2067 tree inner1
= gimple_assign_rhs1 (stmt
);
2068 tree inner2
= gimple_assign_rhs2 (stmt
);
2071 tree partial
= NULL_TREE
;
2072 bool is_or
= (innercode
== BIT_IOR_EXPR
);
2074 /* Check for boolean identities that don't require recursive examination
2076 inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
2077 inner1 OR (inner1 AND inner2) => inner1
2078 !inner1 OR (inner1 OR inner2) => true
2079 !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
2081 if (inner1
== true_test_var
)
2082 return (is_or
? var
: inner1
);
2083 else if (inner2
== true_test_var
)
2084 return (is_or
? var
: inner2
);
2085 else if (inner1
== false_test_var
)
2088 : or_var_with_comparison (inner2
, false, code2
, op2a
, op2b
));
2089 else if (inner2
== false_test_var
)
2092 : or_var_with_comparison (inner1
, false, code2
, op2a
, op2b
));
2094 /* Next, redistribute/reassociate the OR across the inner tests.
2095 Compute the first partial result, (inner1 OR (op2a code op2b)) */
2096 if (TREE_CODE (inner1
) == SSA_NAME
2097 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner1
))
2098 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
2099 && (t
= maybe_fold_or_comparisons (gimple_assign_rhs_code (s
),
2100 gimple_assign_rhs1 (s
),
2101 gimple_assign_rhs2 (s
),
2102 code2
, op2a
, op2b
)))
2104 /* Handle the OR case, where we are reassociating:
2105 (inner1 OR inner2) OR (op2a code2 op2b)
2107 If the partial result t is a constant, we win. Otherwise
2108 continue on to try reassociating with the other inner test. */
2111 if (integer_onep (t
))
2112 return boolean_true_node
;
2113 else if (integer_zerop (t
))
2117 /* Handle the AND case, where we are redistributing:
2118 (inner1 AND inner2) OR (op2a code2 op2b)
2119 => (t AND (inner2 OR (op2a code op2b))) */
2120 else if (integer_zerop (t
))
2121 return boolean_false_node
;
2123 /* Save partial result for later. */
2127 /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
2128 if (TREE_CODE (inner2
) == SSA_NAME
2129 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner2
))
2130 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
2131 && (t
= maybe_fold_or_comparisons (gimple_assign_rhs_code (s
),
2132 gimple_assign_rhs1 (s
),
2133 gimple_assign_rhs2 (s
),
2134 code2
, op2a
, op2b
)))
2136 /* Handle the OR case, where we are reassociating:
2137 (inner1 OR inner2) OR (op2a code2 op2b)
2139 => (t OR partial) */
2142 if (integer_zerop (t
))
2144 else if (integer_onep (t
))
2145 return boolean_true_node
;
2146 /* If both are the same, we can apply the identity
2148 else if (partial
&& same_bool_result_p (t
, partial
))
2152 /* Handle the AND case, where we are redistributing:
2153 (inner1 AND inner2) OR (op2a code2 op2b)
2154 => (t AND (inner1 OR (op2a code2 op2b)))
2155 => (t AND partial) */
2158 if (integer_zerop (t
))
2159 return boolean_false_node
;
2162 /* We already got a simplification for the other
2163 operand to the redistributed AND expression. The
2164 interesting case is when at least one is true.
2165 Or, if both are the same, we can apply the identity
2167 if (integer_onep (partial
))
2169 else if (integer_onep (t
))
2171 else if (same_bool_result_p (t
, partial
))
2180 /* Try to simplify the OR of two comparisons defined by
2181 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
2182 If this can be done without constructing an intermediate value,
2183 return the resulting tree; otherwise NULL_TREE is returned.
2184 This function is deliberately asymmetric as it recurses on SSA_DEFs
2185 in the first comparison but not the second. */
2188 or_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
2189 enum tree_code code2
, tree op2a
, tree op2b
)
2191 tree truth_type
= truth_type_for (TREE_TYPE (op1a
));
2193 /* First check for ((x CODE1 y) OR (x CODE2 y)). */
2194 if (operand_equal_p (op1a
, op2a
, 0)
2195 && operand_equal_p (op1b
, op2b
, 0))
2197 /* Result will be either NULL_TREE, or a combined comparison. */
2198 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
2199 TRUTH_ORIF_EXPR
, code1
, code2
,
2200 truth_type
, op1a
, op1b
);
2205 /* Likewise the swapped case of the above. */
2206 if (operand_equal_p (op1a
, op2b
, 0)
2207 && operand_equal_p (op1b
, op2a
, 0))
2209 /* Result will be either NULL_TREE, or a combined comparison. */
2210 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
2211 TRUTH_ORIF_EXPR
, code1
,
2212 swap_tree_comparison (code2
),
2213 truth_type
, op1a
, op1b
);
2218 /* If both comparisons are of the same value against constants, we might
2219 be able to merge them. */
2220 if (operand_equal_p (op1a
, op2a
, 0)
2221 && TREE_CODE (op1b
) == INTEGER_CST
2222 && TREE_CODE (op2b
) == INTEGER_CST
)
2224 int cmp
= tree_int_cst_compare (op1b
, op2b
);
2226 /* If we have (op1a != op1b), we should either be able to
2227 return that or TRUE, depending on whether the constant op1b
2228 also satisfies the other comparison against op2b. */
2229 if (code1
== NE_EXPR
)
2235 case EQ_EXPR
: val
= (cmp
== 0); break;
2236 case NE_EXPR
: val
= (cmp
!= 0); break;
2237 case LT_EXPR
: val
= (cmp
< 0); break;
2238 case GT_EXPR
: val
= (cmp
> 0); break;
2239 case LE_EXPR
: val
= (cmp
<= 0); break;
2240 case GE_EXPR
: val
= (cmp
>= 0); break;
2241 default: done
= false;
2246 return boolean_true_node
;
2248 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
2251 /* Likewise if the second comparison is a != comparison. */
2252 else if (code2
== NE_EXPR
)
2258 case EQ_EXPR
: val
= (cmp
== 0); break;
2259 case NE_EXPR
: val
= (cmp
!= 0); break;
2260 case LT_EXPR
: val
= (cmp
> 0); break;
2261 case GT_EXPR
: val
= (cmp
< 0); break;
2262 case LE_EXPR
: val
= (cmp
>= 0); break;
2263 case GE_EXPR
: val
= (cmp
<= 0); break;
2264 default: done
= false;
2269 return boolean_true_node
;
2271 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
2275 /* See if an equality test is redundant with the other comparison. */
2276 else if (code1
== EQ_EXPR
)
2281 case EQ_EXPR
: val
= (cmp
== 0); break;
2282 case NE_EXPR
: val
= (cmp
!= 0); break;
2283 case LT_EXPR
: val
= (cmp
< 0); break;
2284 case GT_EXPR
: val
= (cmp
> 0); break;
2285 case LE_EXPR
: val
= (cmp
<= 0); break;
2286 case GE_EXPR
: val
= (cmp
>= 0); break;
2291 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
2293 else if (code2
== EQ_EXPR
)
2298 case EQ_EXPR
: val
= (cmp
== 0); break;
2299 case NE_EXPR
: val
= (cmp
!= 0); break;
2300 case LT_EXPR
: val
= (cmp
> 0); break;
2301 case GT_EXPR
: val
= (cmp
< 0); break;
2302 case LE_EXPR
: val
= (cmp
>= 0); break;
2303 case GE_EXPR
: val
= (cmp
<= 0); break;
2308 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
2311 /* Chose the less restrictive of two < or <= comparisons. */
2312 else if ((code1
== LT_EXPR
|| code1
== LE_EXPR
)
2313 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
2315 if ((cmp
< 0) || (cmp
== 0 && code1
== LT_EXPR
))
2316 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
2318 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
2321 /* Likewise chose the less restrictive of two > or >= comparisons. */
2322 else if ((code1
== GT_EXPR
|| code1
== GE_EXPR
)
2323 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
2325 if ((cmp
> 0) || (cmp
== 0 && code1
== GT_EXPR
))
2326 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
2328 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
2331 /* Check for singleton ranges. */
2333 && ((code1
== LT_EXPR
&& code2
== GT_EXPR
)
2334 || (code1
== GT_EXPR
&& code2
== LT_EXPR
)))
2335 return fold_build2 (NE_EXPR
, boolean_type_node
, op1a
, op2b
);
2337 /* Check for less/greater pairs that don't restrict the range at all. */
2339 && (code1
== LT_EXPR
|| code1
== LE_EXPR
)
2340 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
2341 return boolean_true_node
;
2343 && (code1
== GT_EXPR
|| code1
== GE_EXPR
)
2344 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
2345 return boolean_true_node
;
2348 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
2349 NAME's definition is a truth value. See if there are any simplifications
2350 that can be done against the NAME's definition. */
2351 if (TREE_CODE (op1a
) == SSA_NAME
2352 && (code1
== NE_EXPR
|| code1
== EQ_EXPR
)
2353 && (integer_zerop (op1b
) || integer_onep (op1b
)))
2355 bool invert
= ((code1
== EQ_EXPR
&& integer_zerop (op1b
))
2356 || (code1
== NE_EXPR
&& integer_onep (op1b
)));
2357 gimple stmt
= SSA_NAME_DEF_STMT (op1a
);
2358 switch (gimple_code (stmt
))
2361 /* Try to simplify by copy-propagating the definition. */
2362 return or_var_with_comparison (op1a
, invert
, code2
, op2a
, op2b
);
2365 /* If every argument to the PHI produces the same result when
2366 ORed with the second comparison, we win.
2367 Do not do this unless the type is bool since we need a bool
2368 result here anyway. */
2369 if (TREE_CODE (TREE_TYPE (op1a
)) == BOOLEAN_TYPE
)
2371 tree result
= NULL_TREE
;
2373 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
2375 tree arg
= gimple_phi_arg_def (stmt
, i
);
2377 /* If this PHI has itself as an argument, ignore it.
2378 If all the other args produce the same result,
2380 if (arg
== gimple_phi_result (stmt
))
2382 else if (TREE_CODE (arg
) == INTEGER_CST
)
2384 if (invert
? integer_zerop (arg
) : integer_nonzerop (arg
))
2387 result
= boolean_true_node
;
2388 else if (!integer_onep (result
))
2392 result
= fold_build2 (code2
, boolean_type_node
,
2394 else if (!same_bool_comparison_p (result
,
2398 else if (TREE_CODE (arg
) == SSA_NAME
2399 && !SSA_NAME_IS_DEFAULT_DEF (arg
))
2402 gimple def_stmt
= SSA_NAME_DEF_STMT (arg
);
2403 /* In simple cases we can look through PHI nodes,
2404 but we have to be careful with loops.
2406 if (! dom_info_available_p (CDI_DOMINATORS
)
2407 || gimple_bb (def_stmt
) == gimple_bb (stmt
)
2408 || dominated_by_p (CDI_DOMINATORS
,
2409 gimple_bb (def_stmt
),
2412 temp
= or_var_with_comparison (arg
, invert
, code2
,
2418 else if (!same_bool_result_p (result
, temp
))
2434 /* Try to simplify the OR of two comparisons, specified by
2435 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
2436 If this can be simplified to a single expression (without requiring
2437 introducing more SSA variables to hold intermediate values),
2438 return the resulting tree. Otherwise return NULL_TREE.
2439 If the result expression is non-null, it has boolean type. */
2442 maybe_fold_or_comparisons (enum tree_code code1
, tree op1a
, tree op1b
,
2443 enum tree_code code2
, tree op2a
, tree op2b
)
2445 tree t
= or_comparisons_1 (code1
, op1a
, op1b
, code2
, op2a
, op2b
);
2449 return or_comparisons_1 (code2
, op2a
, op2b
, code1
, op1a
, op1b
);
2453 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
2455 Either NULL_TREE, a simplified but non-constant or a constant
2458 ??? This should go into a gimple-fold-inline.h file to be eventually
2459 privatized with the single valueize function used in the various TUs
2460 to avoid the indirect function call overhead. */
2463 gimple_fold_stmt_to_constant_1 (gimple stmt
, tree (*valueize
) (tree
))
2465 location_t loc
= gimple_location (stmt
);
2466 switch (gimple_code (stmt
))
2470 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
2472 switch (get_gimple_rhs_class (subcode
))
2474 case GIMPLE_SINGLE_RHS
:
2476 tree rhs
= gimple_assign_rhs1 (stmt
);
2477 enum tree_code_class kind
= TREE_CODE_CLASS (subcode
);
2479 if (TREE_CODE (rhs
) == SSA_NAME
)
2481 /* If the RHS is an SSA_NAME, return its known constant value,
2483 return (*valueize
) (rhs
);
2485 /* Handle propagating invariant addresses into address
2487 else if (TREE_CODE (rhs
) == ADDR_EXPR
2488 && !is_gimple_min_invariant (rhs
))
2490 HOST_WIDE_INT offset
= 0;
2492 base
= get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs
, 0),
2496 && (CONSTANT_CLASS_P (base
)
2497 || decl_address_invariant_p (base
)))
2498 return build_invariant_address (TREE_TYPE (rhs
),
2501 else if (TREE_CODE (rhs
) == CONSTRUCTOR
2502 && TREE_CODE (TREE_TYPE (rhs
)) == VECTOR_TYPE
2503 && (CONSTRUCTOR_NELTS (rhs
)
2504 == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs
))))
2509 vec
= XALLOCAVEC (tree
,
2510 TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs
)));
2511 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs
), i
, val
)
2513 val
= (*valueize
) (val
);
2514 if (TREE_CODE (val
) == INTEGER_CST
2515 || TREE_CODE (val
) == REAL_CST
2516 || TREE_CODE (val
) == FIXED_CST
)
2522 return build_vector (TREE_TYPE (rhs
), vec
);
2525 if (kind
== tcc_reference
)
2527 if ((TREE_CODE (rhs
) == VIEW_CONVERT_EXPR
2528 || TREE_CODE (rhs
) == REALPART_EXPR
2529 || TREE_CODE (rhs
) == IMAGPART_EXPR
)
2530 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
2532 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
2533 return fold_unary_loc (EXPR_LOCATION (rhs
),
2535 TREE_TYPE (rhs
), val
);
2537 else if (TREE_CODE (rhs
) == BIT_FIELD_REF
2538 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
2540 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
2541 return fold_ternary_loc (EXPR_LOCATION (rhs
),
2543 TREE_TYPE (rhs
), val
,
2544 TREE_OPERAND (rhs
, 1),
2545 TREE_OPERAND (rhs
, 2));
2547 else if (TREE_CODE (rhs
) == MEM_REF
2548 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
2550 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
2551 if (TREE_CODE (val
) == ADDR_EXPR
2552 && is_gimple_min_invariant (val
))
2554 tree tem
= fold_build2 (MEM_REF
, TREE_TYPE (rhs
),
2556 TREE_OPERAND (rhs
, 1));
2561 return fold_const_aggregate_ref_1 (rhs
, valueize
);
2563 else if (kind
== tcc_declaration
)
2564 return get_symbol_constant_value (rhs
);
2568 case GIMPLE_UNARY_RHS
:
2570 /* Handle unary operators that can appear in GIMPLE form.
2571 Note that we know the single operand must be a constant,
2572 so this should almost always return a simplified RHS. */
2573 tree lhs
= gimple_assign_lhs (stmt
);
2574 tree op0
= (*valueize
) (gimple_assign_rhs1 (stmt
));
2576 /* Conversions are useless for CCP purposes if they are
2577 value-preserving. Thus the restrictions that
2578 useless_type_conversion_p places for restrict qualification
2579 of pointer types should not apply here.
2580 Substitution later will only substitute to allowed places. */
2581 if (CONVERT_EXPR_CODE_P (subcode
)
2582 && POINTER_TYPE_P (TREE_TYPE (lhs
))
2583 && POINTER_TYPE_P (TREE_TYPE (op0
))
2584 && TYPE_ADDR_SPACE (TREE_TYPE (lhs
))
2585 == TYPE_ADDR_SPACE (TREE_TYPE (op0
))
2586 && TYPE_MODE (TREE_TYPE (lhs
))
2587 == TYPE_MODE (TREE_TYPE (op0
)))
2591 fold_unary_ignore_overflow_loc (loc
, subcode
,
2592 gimple_expr_type (stmt
), op0
);
2595 case GIMPLE_BINARY_RHS
:
2597 /* Handle binary operators that can appear in GIMPLE form. */
2598 tree op0
= (*valueize
) (gimple_assign_rhs1 (stmt
));
2599 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
2601 /* Translate &x + CST into an invariant form suitable for
2602 further propagation. */
2603 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
2604 && TREE_CODE (op0
) == ADDR_EXPR
2605 && TREE_CODE (op1
) == INTEGER_CST
)
2607 tree off
= fold_convert (ptr_type_node
, op1
);
2608 return build_fold_addr_expr_loc
2610 fold_build2 (MEM_REF
,
2611 TREE_TYPE (TREE_TYPE (op0
)),
2612 unshare_expr (op0
), off
));
2615 return fold_binary_loc (loc
, subcode
,
2616 gimple_expr_type (stmt
), op0
, op1
);
2619 case GIMPLE_TERNARY_RHS
:
2621 /* Handle ternary operators that can appear in GIMPLE form. */
2622 tree op0
= (*valueize
) (gimple_assign_rhs1 (stmt
));
2623 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
2624 tree op2
= (*valueize
) (gimple_assign_rhs3 (stmt
));
2626 /* Fold embedded expressions in ternary codes. */
2627 if ((subcode
== COND_EXPR
2628 || subcode
== VEC_COND_EXPR
)
2629 && COMPARISON_CLASS_P (op0
))
2631 tree op00
= (*valueize
) (TREE_OPERAND (op0
, 0));
2632 tree op01
= (*valueize
) (TREE_OPERAND (op0
, 1));
2633 tree tem
= fold_binary_loc (loc
, TREE_CODE (op0
),
2634 TREE_TYPE (op0
), op00
, op01
);
2639 return fold_ternary_loc (loc
, subcode
,
2640 gimple_expr_type (stmt
), op0
, op1
, op2
);
2652 if (gimple_call_internal_p (stmt
))
2653 /* No folding yet for these functions. */
2656 fn
= (*valueize
) (gimple_call_fn (stmt
));
2657 if (TREE_CODE (fn
) == ADDR_EXPR
2658 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
2659 && DECL_BUILT_IN (TREE_OPERAND (fn
, 0)))
2661 tree
*args
= XALLOCAVEC (tree
, gimple_call_num_args (stmt
));
2664 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
2665 args
[i
] = (*valueize
) (gimple_call_arg (stmt
, i
));
2666 call
= build_call_array_loc (loc
,
2667 gimple_call_return_type (stmt
),
2668 fn
, gimple_call_num_args (stmt
), args
);
2669 retval
= fold_call_expr (EXPR_LOCATION (call
), call
, false);
2671 /* fold_call_expr wraps the result inside a NOP_EXPR. */
2672 STRIP_NOPS (retval
);
2683 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
2684 Returns NULL_TREE if folding to a constant is not possible, otherwise
2685 returns a constant according to is_gimple_min_invariant. */
2688 gimple_fold_stmt_to_constant (gimple stmt
, tree (*valueize
) (tree
))
2690 tree res
= gimple_fold_stmt_to_constant_1 (stmt
, valueize
);
2691 if (res
&& is_gimple_min_invariant (res
))
2697 /* The following set of functions are supposed to fold references using
2698 their constant initializers. */
2700 static tree
fold_ctor_reference (tree type
, tree ctor
,
2701 unsigned HOST_WIDE_INT offset
,
2702 unsigned HOST_WIDE_INT size
, tree
);
2704 /* See if we can find constructor defining value of BASE.
2705 When we know the consructor with constant offset (such as
2706 base is array[40] and we do know constructor of array), then
2707 BIT_OFFSET is adjusted accordingly.
2709 As a special case, return error_mark_node when constructor
2710 is not explicitly available, but it is known to be zero
2711 such as 'static const int a;'. */
2713 get_base_constructor (tree base
, HOST_WIDE_INT
*bit_offset
,
2714 tree (*valueize
)(tree
))
2716 HOST_WIDE_INT bit_offset2
, size
, max_size
;
2717 if (TREE_CODE (base
) == MEM_REF
)
2719 if (!integer_zerop (TREE_OPERAND (base
, 1)))
2721 if (!host_integerp (TREE_OPERAND (base
, 1), 0))
2723 *bit_offset
+= (mem_ref_offset (base
).low
2728 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
2729 base
= valueize (TREE_OPERAND (base
, 0));
2730 if (!base
|| TREE_CODE (base
) != ADDR_EXPR
)
2732 base
= TREE_OPERAND (base
, 0);
2735 /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
2736 DECL_INITIAL. If BASE is a nested reference into another
2737 ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
2738 the inner reference. */
2739 switch (TREE_CODE (base
))
2744 tree init
= ctor_for_folding (base
);
2746 /* Our semantic is exact opposite of ctor_for_folding;
2747 NULL means unknown, while error_mark_node is 0. */
2748 if (init
== error_mark_node
)
2751 return error_mark_node
;
2757 base
= get_ref_base_and_extent (base
, &bit_offset2
, &size
, &max_size
);
2758 if (max_size
== -1 || size
!= max_size
)
2760 *bit_offset
+= bit_offset2
;
2761 return get_base_constructor (base
, bit_offset
, valueize
);
2772 /* CTOR is STRING_CST. Fold reference of type TYPE and size SIZE
2773 to the memory at bit OFFSET.
2775 We do only simple job of folding byte accesses. */
2778 fold_string_cst_ctor_reference (tree type
, tree ctor
,
2779 unsigned HOST_WIDE_INT offset
,
2780 unsigned HOST_WIDE_INT size
)
2782 if (INTEGRAL_TYPE_P (type
)
2783 && (TYPE_MODE (type
)
2784 == TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor
))))
2785 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor
))))
2787 && GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor
)))) == 1
2788 && size
== BITS_PER_UNIT
2789 && !(offset
% BITS_PER_UNIT
))
2791 offset
/= BITS_PER_UNIT
;
2792 if (offset
< (unsigned HOST_WIDE_INT
) TREE_STRING_LENGTH (ctor
))
2793 return build_int_cst_type (type
, (TREE_STRING_POINTER (ctor
)
2796 const char a[20]="hello";
2799 might lead to offset greater than string length. In this case we
2800 know value is either initialized to 0 or out of bounds. Return 0
2802 return build_zero_cst (type
);
2807 /* CTOR is CONSTRUCTOR of an array type. Fold reference of type TYPE and size
2808 SIZE to the memory at bit OFFSET. */
2811 fold_array_ctor_reference (tree type
, tree ctor
,
2812 unsigned HOST_WIDE_INT offset
,
2813 unsigned HOST_WIDE_INT size
,
2816 unsigned HOST_WIDE_INT cnt
;
2818 double_int low_bound
, elt_size
;
2819 double_int index
, max_index
;
2820 double_int access_index
;
2821 tree domain_type
= NULL_TREE
, index_type
= NULL_TREE
;
2822 HOST_WIDE_INT inner_offset
;
2824 /* Compute low bound and elt size. */
2825 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
)
2826 domain_type
= TYPE_DOMAIN (TREE_TYPE (ctor
));
2827 if (domain_type
&& TYPE_MIN_VALUE (domain_type
))
2829 /* Static constructors for variably sized objects makes no sense. */
2830 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type
)) == INTEGER_CST
);
2831 index_type
= TREE_TYPE (TYPE_MIN_VALUE (domain_type
));
2832 low_bound
= tree_to_double_int (TYPE_MIN_VALUE (domain_type
));
2835 low_bound
= double_int_zero
;
2836 /* Static constructors for variably sized objects makes no sense. */
2837 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor
))))
2840 tree_to_double_int (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor
))));
2843 /* We can handle only constantly sized accesses that are known to not
2844 be larger than size of array element. */
2845 if (!TYPE_SIZE_UNIT (type
)
2846 || TREE_CODE (TYPE_SIZE_UNIT (type
)) != INTEGER_CST
2847 || elt_size
.slt (tree_to_double_int (TYPE_SIZE_UNIT (type
))))
2850 /* Compute the array index we look for. */
2851 access_index
= double_int::from_uhwi (offset
/ BITS_PER_UNIT
)
2852 .udiv (elt_size
, TRUNC_DIV_EXPR
);
2853 access_index
+= low_bound
;
2855 access_index
= access_index
.ext (TYPE_PRECISION (index_type
),
2856 TYPE_UNSIGNED (index_type
));
2858 /* And offset within the access. */
2859 inner_offset
= offset
% (elt_size
.to_uhwi () * BITS_PER_UNIT
);
2861 /* See if the array field is large enough to span whole access. We do not
2862 care to fold accesses spanning multiple array indexes. */
2863 if (inner_offset
+ size
> elt_size
.to_uhwi () * BITS_PER_UNIT
)
2866 index
= low_bound
- double_int_one
;
2868 index
= index
.ext (TYPE_PRECISION (index_type
), TYPE_UNSIGNED (index_type
));
2870 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
, cval
)
2872 /* Array constructor might explicitely set index, or specify range
2873 or leave index NULL meaning that it is next index after previous
2877 if (TREE_CODE (cfield
) == INTEGER_CST
)
2878 max_index
= index
= tree_to_double_int (cfield
);
2881 gcc_assert (TREE_CODE (cfield
) == RANGE_EXPR
);
2882 index
= tree_to_double_int (TREE_OPERAND (cfield
, 0));
2883 max_index
= tree_to_double_int (TREE_OPERAND (cfield
, 1));
2888 index
+= double_int_one
;
2890 index
= index
.ext (TYPE_PRECISION (index_type
),
2891 TYPE_UNSIGNED (index_type
));
2895 /* Do we have match? */
2896 if (access_index
.cmp (index
, 1) >= 0
2897 && access_index
.cmp (max_index
, 1) <= 0)
2898 return fold_ctor_reference (type
, cval
, inner_offset
, size
,
2901 /* When memory is not explicitely mentioned in constructor,
2902 it is 0 (or out of range). */
2903 return build_zero_cst (type
);
2906 /* CTOR is CONSTRUCTOR of an aggregate or vector.
2907 Fold reference of type TYPE and size SIZE to the memory at bit OFFSET. */
2910 fold_nonarray_ctor_reference (tree type
, tree ctor
,
2911 unsigned HOST_WIDE_INT offset
,
2912 unsigned HOST_WIDE_INT size
,
2915 unsigned HOST_WIDE_INT cnt
;
2918 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
,
2921 tree byte_offset
= DECL_FIELD_OFFSET (cfield
);
2922 tree field_offset
= DECL_FIELD_BIT_OFFSET (cfield
);
2923 tree field_size
= DECL_SIZE (cfield
);
2924 double_int bitoffset
;
2925 double_int byte_offset_cst
= tree_to_double_int (byte_offset
);
2926 double_int bits_per_unit_cst
= double_int::from_uhwi (BITS_PER_UNIT
);
2927 double_int bitoffset_end
, access_end
;
2929 /* Variable sized objects in static constructors makes no sense,
2930 but field_size can be NULL for flexible array members. */
2931 gcc_assert (TREE_CODE (field_offset
) == INTEGER_CST
2932 && TREE_CODE (byte_offset
) == INTEGER_CST
2933 && (field_size
!= NULL_TREE
2934 ? TREE_CODE (field_size
) == INTEGER_CST
2935 : TREE_CODE (TREE_TYPE (cfield
)) == ARRAY_TYPE
));
2937 /* Compute bit offset of the field. */
2938 bitoffset
= tree_to_double_int (field_offset
)
2939 + byte_offset_cst
* bits_per_unit_cst
;
2940 /* Compute bit offset where the field ends. */
2941 if (field_size
!= NULL_TREE
)
2942 bitoffset_end
= bitoffset
+ tree_to_double_int (field_size
);
2944 bitoffset_end
= double_int_zero
;
2946 access_end
= double_int::from_uhwi (offset
)
2947 + double_int::from_uhwi (size
);
2949 /* Is there any overlap between [OFFSET, OFFSET+SIZE) and
2950 [BITOFFSET, BITOFFSET_END)? */
2951 if (access_end
.cmp (bitoffset
, 0) > 0
2952 && (field_size
== NULL_TREE
2953 || double_int::from_uhwi (offset
).slt (bitoffset_end
)))
2955 double_int inner_offset
= double_int::from_uhwi (offset
) - bitoffset
;
2956 /* We do have overlap. Now see if field is large enough to
2957 cover the access. Give up for accesses spanning multiple
2959 if (access_end
.cmp (bitoffset_end
, 0) > 0)
2961 if (double_int::from_uhwi (offset
).slt (bitoffset
))
2963 return fold_ctor_reference (type
, cval
,
2964 inner_offset
.to_uhwi (), size
,
2968 /* When memory is not explicitely mentioned in constructor, it is 0. */
2969 return build_zero_cst (type
);
2972 /* CTOR is value initializing memory, fold reference of type TYPE and size SIZE
2973 to the memory at bit OFFSET. */
2976 fold_ctor_reference (tree type
, tree ctor
, unsigned HOST_WIDE_INT offset
,
2977 unsigned HOST_WIDE_INT size
, tree from_decl
)
2981 /* We found the field with exact match. */
2982 if (useless_type_conversion_p (type
, TREE_TYPE (ctor
))
2984 return canonicalize_constructor_val (unshare_expr (ctor
), from_decl
);
2986 /* We are at the end of walk, see if we can view convert the
2988 if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor
)) && !offset
2989 /* VIEW_CONVERT_EXPR is defined only for matching sizes. */
2990 && operand_equal_p (TYPE_SIZE (type
),
2991 TYPE_SIZE (TREE_TYPE (ctor
)), 0))
2993 ret
= canonicalize_constructor_val (unshare_expr (ctor
), from_decl
);
2994 ret
= fold_unary (VIEW_CONVERT_EXPR
, type
, ret
);
2999 if (TREE_CODE (ctor
) == STRING_CST
)
3000 return fold_string_cst_ctor_reference (type
, ctor
, offset
, size
);
3001 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
3004 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
3005 || TREE_CODE (TREE_TYPE (ctor
)) == VECTOR_TYPE
)
3006 return fold_array_ctor_reference (type
, ctor
, offset
, size
,
3009 return fold_nonarray_ctor_reference (type
, ctor
, offset
, size
,
3016 /* Return the tree representing the element referenced by T if T is an
3017 ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
3018 names using VALUEIZE. Return NULL_TREE otherwise. */
3021 fold_const_aggregate_ref_1 (tree t
, tree (*valueize
) (tree
))
3023 tree ctor
, idx
, base
;
3024 HOST_WIDE_INT offset
, size
, max_size
;
3027 if (TREE_THIS_VOLATILE (t
))
3030 if (TREE_CODE_CLASS (TREE_CODE (t
)) == tcc_declaration
)
3031 return get_symbol_constant_value (t
);
3033 tem
= fold_read_from_constant_string (t
);
3037 switch (TREE_CODE (t
))
3040 case ARRAY_RANGE_REF
:
3041 /* Constant indexes are handled well by get_base_constructor.
3042 Only special case variable offsets.
3043 FIXME: This code can't handle nested references with variable indexes
3044 (they will be handled only by iteration of ccp). Perhaps we can bring
3045 get_ref_base_and_extent here and make it use a valueize callback. */
3046 if (TREE_CODE (TREE_OPERAND (t
, 1)) == SSA_NAME
3048 && (idx
= (*valueize
) (TREE_OPERAND (t
, 1)))
3049 && TREE_CODE (idx
) == INTEGER_CST
)
3051 tree low_bound
, unit_size
;
3054 /* If the resulting bit-offset is constant, track it. */
3055 if ((low_bound
= array_ref_low_bound (t
),
3056 TREE_CODE (low_bound
) == INTEGER_CST
)
3057 && (unit_size
= array_ref_element_size (t
),
3058 host_integerp (unit_size
, 1))
3059 && (doffset
= (TREE_INT_CST (idx
) - TREE_INT_CST (low_bound
))
3060 .sext (TYPE_PRECISION (TREE_TYPE (idx
))),
3061 doffset
.fits_shwi ()))
3063 offset
= doffset
.to_shwi ();
3064 offset
*= TREE_INT_CST_LOW (unit_size
);
3065 offset
*= BITS_PER_UNIT
;
3067 base
= TREE_OPERAND (t
, 0);
3068 ctor
= get_base_constructor (base
, &offset
, valueize
);
3069 /* Empty constructor. Always fold to 0. */
3070 if (ctor
== error_mark_node
)
3071 return build_zero_cst (TREE_TYPE (t
));
3072 /* Out of bound array access. Value is undefined,
3076 /* We can not determine ctor. */
3079 return fold_ctor_reference (TREE_TYPE (t
), ctor
, offset
,
3080 TREE_INT_CST_LOW (unit_size
)
3089 case TARGET_MEM_REF
:
3091 base
= get_ref_base_and_extent (t
, &offset
, &size
, &max_size
);
3092 ctor
= get_base_constructor (base
, &offset
, valueize
);
3094 /* Empty constructor. Always fold to 0. */
3095 if (ctor
== error_mark_node
)
3096 return build_zero_cst (TREE_TYPE (t
));
3097 /* We do not know precise address. */
3098 if (max_size
== -1 || max_size
!= size
)
3100 /* We can not determine ctor. */
3104 /* Out of bound array access. Value is undefined, but don't fold. */
3108 return fold_ctor_reference (TREE_TYPE (t
), ctor
, offset
, size
,
3114 tree c
= fold_const_aggregate_ref_1 (TREE_OPERAND (t
, 0), valueize
);
3115 if (c
&& TREE_CODE (c
) == COMPLEX_CST
)
3116 return fold_build1_loc (EXPR_LOCATION (t
),
3117 TREE_CODE (t
), TREE_TYPE (t
), c
);
3129 fold_const_aggregate_ref (tree t
)
3131 return fold_const_aggregate_ref_1 (t
, NULL
);
3134 /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
3135 is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
3136 KNOWN_BINFO carries the binfo describing the true type of
3137 OBJ_TYPE_REF_OBJECT(REF). */
3140 gimple_get_virt_method_for_binfo (HOST_WIDE_INT token
, tree known_binfo
)
3142 unsigned HOST_WIDE_INT offset
, size
;
3143 tree v
, fn
, vtable
, init
;
3145 vtable
= v
= BINFO_VTABLE (known_binfo
);
3146 /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */
3150 if (TREE_CODE (v
) == POINTER_PLUS_EXPR
)
3152 offset
= tree_low_cst (TREE_OPERAND (v
, 1), 1) * BITS_PER_UNIT
;
3153 v
= TREE_OPERAND (v
, 0);
3158 if (TREE_CODE (v
) != ADDR_EXPR
)
3160 v
= TREE_OPERAND (v
, 0);
3162 if (TREE_CODE (v
) != VAR_DECL
3163 || !DECL_VIRTUAL_P (v
))
3165 init
= ctor_for_folding (v
);
3167 /* The virtual tables should always be born with constructors.
3168 and we always should assume that they are avaialble for
3169 folding. At the moment we do not stream them in all cases,
3170 but it should never happen that ctor seem unreachable. */
3172 if (init
== error_mark_node
)
3174 gcc_assert (in_lto_p
);
3177 gcc_checking_assert (TREE_CODE (TREE_TYPE (v
)) == ARRAY_TYPE
);
3178 size
= tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v
))), 1);
3179 offset
+= token
* size
;
3180 fn
= fold_ctor_reference (TREE_TYPE (TREE_TYPE (v
)), init
,
3182 if (!fn
|| integer_zerop (fn
))
3184 gcc_assert (TREE_CODE (fn
) == ADDR_EXPR
3185 || TREE_CODE (fn
) == FDESC_EXPR
);
3186 fn
= TREE_OPERAND (fn
, 0);
3187 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
3189 /* When cgraph node is missing and function is not public, we cannot
3190 devirtualize. This can happen in WHOPR when the actual method
3191 ends up in other partition, because we found devirtualization
3192 possibility too late. */
3193 if (!can_refer_decl_in_current_unit_p (fn
, vtable
))
3196 /* Make sure we create a cgraph node for functions we'll reference.
3197 They can be non-existent if the reference comes from an entry
3198 of an external vtable for example. */
3199 cgraph_get_create_node (fn
);
3204 /* Return true iff VAL is a gimple expression that is known to be
3205 non-negative. Restricted to floating-point inputs. */
3208 gimple_val_nonnegative_real_p (tree val
)
3212 gcc_assert (val
&& SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)));
3214 /* Use existing logic for non-gimple trees. */
3215 if (tree_expr_nonnegative_p (val
))
3218 if (TREE_CODE (val
) != SSA_NAME
)
3221 /* Currently we look only at the immediately defining statement
3222 to make this determination, since recursion on defining
3223 statements of operands can lead to quadratic behavior in the
3224 worst case. This is expected to catch almost all occurrences
3225 in practice. It would be possible to implement limited-depth
3226 recursion if important cases are lost. Alternatively, passes
3227 that need this information (such as the pow/powi lowering code
3228 in the cse_sincos pass) could be revised to provide it through
3229 dataflow propagation. */
3231 def_stmt
= SSA_NAME_DEF_STMT (val
);
3233 if (is_gimple_assign (def_stmt
))
3237 /* See fold-const.c:tree_expr_nonnegative_p for additional
3238 cases that could be handled with recursion. */
3240 switch (gimple_assign_rhs_code (def_stmt
))
3243 /* Always true for floating-point operands. */
3247 /* True if the two operands are identical (since we are
3248 restricted to floating-point inputs). */
3249 op0
= gimple_assign_rhs1 (def_stmt
);
3250 op1
= gimple_assign_rhs2 (def_stmt
);
3253 || operand_equal_p (op0
, op1
, 0))
3260 else if (is_gimple_call (def_stmt
))
3262 tree fndecl
= gimple_call_fndecl (def_stmt
);
3264 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
3268 switch (DECL_FUNCTION_CODE (fndecl
))
3270 CASE_FLT_FN (BUILT_IN_ACOS
):
3271 CASE_FLT_FN (BUILT_IN_ACOSH
):
3272 CASE_FLT_FN (BUILT_IN_CABS
):
3273 CASE_FLT_FN (BUILT_IN_COSH
):
3274 CASE_FLT_FN (BUILT_IN_ERFC
):
3275 CASE_FLT_FN (BUILT_IN_EXP
):
3276 CASE_FLT_FN (BUILT_IN_EXP10
):
3277 CASE_FLT_FN (BUILT_IN_EXP2
):
3278 CASE_FLT_FN (BUILT_IN_FABS
):
3279 CASE_FLT_FN (BUILT_IN_FDIM
):
3280 CASE_FLT_FN (BUILT_IN_HYPOT
):
3281 CASE_FLT_FN (BUILT_IN_POW10
):
3284 CASE_FLT_FN (BUILT_IN_SQRT
):
3285 /* sqrt(-0.0) is -0.0, and sqrt is not defined over other
3286 nonnegative inputs. */
3287 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (val
))))
3292 CASE_FLT_FN (BUILT_IN_POWI
):
3293 /* True if the second argument is an even integer. */
3294 arg1
= gimple_call_arg (def_stmt
, 1);
3296 if (TREE_CODE (arg1
) == INTEGER_CST
3297 && (TREE_INT_CST_LOW (arg1
) & 1) == 0)
3302 CASE_FLT_FN (BUILT_IN_POW
):
3303 /* True if the second argument is an even integer-valued
3305 arg1
= gimple_call_arg (def_stmt
, 1);
3307 if (TREE_CODE (arg1
) == REAL_CST
)
3312 c
= TREE_REAL_CST (arg1
);
3313 n
= real_to_integer (&c
);
3317 REAL_VALUE_TYPE cint
;
3318 real_from_integer (&cint
, VOIDmode
, n
, n
< 0 ? -1 : 0, 0);
3319 if (real_identical (&c
, &cint
))
3335 /* Given a pointer value OP0, return a simplified version of an
3336 indirection through OP0, or NULL_TREE if no simplification is
3337 possible. Note that the resulting type may be different from
3338 the type pointed to in the sense that it is still compatible
3339 from the langhooks point of view. */
3342 gimple_fold_indirect_ref (tree t
)
3344 tree ptype
= TREE_TYPE (t
), type
= TREE_TYPE (ptype
);
3349 subtype
= TREE_TYPE (sub
);
3350 if (!POINTER_TYPE_P (subtype
))
3353 if (TREE_CODE (sub
) == ADDR_EXPR
)
3355 tree op
= TREE_OPERAND (sub
, 0);
3356 tree optype
= TREE_TYPE (op
);
3358 if (useless_type_conversion_p (type
, optype
))
3361 /* *(foo *)&fooarray => fooarray[0] */
3362 if (TREE_CODE (optype
) == ARRAY_TYPE
3363 && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype
))) == INTEGER_CST
3364 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
3366 tree type_domain
= TYPE_DOMAIN (optype
);
3367 tree min_val
= size_zero_node
;
3368 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
3369 min_val
= TYPE_MIN_VALUE (type_domain
);
3370 if (TREE_CODE (min_val
) == INTEGER_CST
)
3371 return build4 (ARRAY_REF
, type
, op
, min_val
, NULL_TREE
, NULL_TREE
);
3373 /* *(foo *)&complexfoo => __real__ complexfoo */
3374 else if (TREE_CODE (optype
) == COMPLEX_TYPE
3375 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
3376 return fold_build1 (REALPART_EXPR
, type
, op
);
3377 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
3378 else if (TREE_CODE (optype
) == VECTOR_TYPE
3379 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
3381 tree part_width
= TYPE_SIZE (type
);
3382 tree index
= bitsize_int (0);
3383 return fold_build3 (BIT_FIELD_REF
, type
, op
, part_width
, index
);
3387 /* *(p + CST) -> ... */
3388 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
3389 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
3391 tree addr
= TREE_OPERAND (sub
, 0);
3392 tree off
= TREE_OPERAND (sub
, 1);
3396 addrtype
= TREE_TYPE (addr
);
3398 /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
3399 if (TREE_CODE (addr
) == ADDR_EXPR
3400 && TREE_CODE (TREE_TYPE (addrtype
)) == VECTOR_TYPE
3401 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (addrtype
)))
3402 && host_integerp (off
, 1))
3404 unsigned HOST_WIDE_INT offset
= tree_low_cst (off
, 1);
3405 tree part_width
= TYPE_SIZE (type
);
3406 unsigned HOST_WIDE_INT part_widthi
3407 = tree_low_cst (part_width
, 0) / BITS_PER_UNIT
;
3408 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
3409 tree index
= bitsize_int (indexi
);
3410 if (offset
/ part_widthi
3411 <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype
)))
3412 return fold_build3 (BIT_FIELD_REF
, type
, TREE_OPERAND (addr
, 0),
3416 /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
3417 if (TREE_CODE (addr
) == ADDR_EXPR
3418 && TREE_CODE (TREE_TYPE (addrtype
)) == COMPLEX_TYPE
3419 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (addrtype
))))
3421 tree size
= TYPE_SIZE_UNIT (type
);
3422 if (tree_int_cst_equal (size
, off
))
3423 return fold_build1 (IMAGPART_EXPR
, type
, TREE_OPERAND (addr
, 0));
3426 /* *(p + CST) -> MEM_REF <p, CST>. */
3427 if (TREE_CODE (addr
) != ADDR_EXPR
3428 || DECL_P (TREE_OPERAND (addr
, 0)))
3429 return fold_build2 (MEM_REF
, type
,
3431 build_int_cst_wide (ptype
,
3432 TREE_INT_CST_LOW (off
),
3433 TREE_INT_CST_HIGH (off
)));
3436 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
3437 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
3438 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype
)))) == INTEGER_CST
3439 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (subtype
))))
3442 tree min_val
= size_zero_node
;
3444 sub
= gimple_fold_indirect_ref (sub
);
3446 sub
= build1 (INDIRECT_REF
, TREE_TYPE (subtype
), osub
);
3447 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
3448 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
3449 min_val
= TYPE_MIN_VALUE (type_domain
);
3450 if (TREE_CODE (min_val
) == INTEGER_CST
)
3451 return build4 (ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
, NULL_TREE
);