1 /* Statement simplification on GIMPLE.
2 Copyright (C) 2010-2014 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"
26 #include "stringpool.h"
29 #include "stor-layout.h"
34 #include "basic-block.h"
35 #include "tree-ssa-alias.h"
36 #include "internal-fn.h"
37 #include "gimple-fold.h"
38 #include "gimple-expr.h"
42 #include "gimple-iterator.h"
43 #include "gimple-ssa.h"
44 #include "tree-ssanames.h"
45 #include "tree-into-ssa.h"
48 #include "tree-ssa-propagate.h"
50 #include "ipa-utils.h"
51 #include "gimple-pretty-print.h"
52 #include "tree-ssa-address.h"
53 #include "langhooks.h"
54 #include "gimplify-me.h"
58 /* Return true when DECL can be referenced from current unit.
59 FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
60 We can get declarations that are not possible to reference for various
63 1) When analyzing C++ virtual tables.
64 C++ virtual tables do have known constructors even
65 when they are keyed to other compilation unit.
66 Those tables can contain pointers to methods and vars
67 in other units. Those methods have both STATIC and EXTERNAL
69 2) In WHOPR mode devirtualization might lead to reference
70 to method that was partitioned elsehwere.
71 In this case we have static VAR_DECL or FUNCTION_DECL
72 that has no corresponding callgraph/varpool node
74 3) COMDAT functions referred by external vtables that
75 we devirtualize only during final compilation stage.
76 At this time we already decided that we will not output
77 the function body and thus we can't reference the symbol
81 can_refer_decl_in_current_unit_p (tree decl
, tree from_decl
)
84 struct cgraph_node
*node
;
87 if (DECL_ABSTRACT (decl
))
90 /* We are concerned only about static/external vars and functions. */
91 if ((!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
92 || (TREE_CODE (decl
) != VAR_DECL
&& TREE_CODE (decl
) != FUNCTION_DECL
))
95 /* Static objects can be referred only if they was not optimized out yet. */
96 if (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
98 /* Before we start optimizing unreachable code we can be sure all
99 static objects are defined. */
100 if (cgraph_function_flags_ready
)
102 snode
= symtab_get_node (decl
);
103 if (!snode
|| !snode
->definition
)
105 node
= dyn_cast
<cgraph_node
*> (snode
);
106 return !node
|| !node
->global
.inlined_to
;
109 /* We will later output the initializer, so we can refer to it.
110 So we are concerned only when DECL comes from initializer of
111 external var or var that has been optimized out. */
113 || TREE_CODE (from_decl
) != VAR_DECL
114 || (!DECL_EXTERNAL (from_decl
)
115 && (vnode
= varpool_get_node (from_decl
)) != NULL
116 && vnode
->definition
)
118 && (vnode
= varpool_get_node (from_decl
)) != NULL
119 && vnode
->in_other_partition
))
121 /* We are folding reference from external vtable. The vtable may reffer
122 to a symbol keyed to other compilation unit. The other compilation
123 unit may be in separate DSO and the symbol may be hidden. */
124 if (DECL_VISIBILITY_SPECIFIED (decl
)
125 && DECL_EXTERNAL (decl
)
126 && DECL_VISIBILITY (decl
) != VISIBILITY_DEFAULT
127 && (!(snode
= symtab_get_node (decl
)) || !snode
->in_other_partition
))
129 /* When function is public, we always can introduce new reference.
130 Exception are the COMDAT functions where introducing a direct
131 reference imply need to include function body in the curren tunit. */
132 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
))
134 /* We have COMDAT. We are going to check if we still have definition
135 or if the definition is going to be output in other partition.
136 Bypass this when gimplifying; all needed functions will be produced.
138 As observed in PR20991 for already optimized out comdat virtual functions
139 it may be tempting to not necessarily give up because the copy will be
140 output elsewhere when corresponding vtable is output.
141 This is however not possible - ABI specify that COMDATs are output in
142 units where they are used and when the other unit was compiled with LTO
143 it is possible that vtable was kept public while the function itself
145 if (!cgraph_function_flags_ready
)
148 snode
= symtab_get_node (decl
);
150 || ((!snode
->definition
|| DECL_EXTERNAL (decl
))
151 && (!snode
->in_other_partition
152 || (!snode
->forced_by_abi
&& !snode
->force_output
))))
154 node
= dyn_cast
<cgraph_node
*> (snode
);
155 return !node
|| !node
->global
.inlined_to
;
158 /* CVAL is value taken from DECL_INITIAL of variable. Try to transform it into
159 acceptable form for is_gimple_min_invariant.
160 FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL. */
163 canonicalize_constructor_val (tree cval
, tree from_decl
)
165 tree orig_cval
= cval
;
167 if (TREE_CODE (cval
) == POINTER_PLUS_EXPR
168 && TREE_CODE (TREE_OPERAND (cval
, 1)) == INTEGER_CST
)
170 tree ptr
= TREE_OPERAND (cval
, 0);
171 if (is_gimple_min_invariant (ptr
))
172 cval
= build1_loc (EXPR_LOCATION (cval
),
173 ADDR_EXPR
, TREE_TYPE (ptr
),
174 fold_build2 (MEM_REF
, TREE_TYPE (TREE_TYPE (ptr
)),
176 fold_convert (ptr_type_node
,
177 TREE_OPERAND (cval
, 1))));
179 if (TREE_CODE (cval
) == ADDR_EXPR
)
181 tree base
= NULL_TREE
;
182 if (TREE_CODE (TREE_OPERAND (cval
, 0)) == COMPOUND_LITERAL_EXPR
)
184 base
= COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval
, 0));
186 TREE_OPERAND (cval
, 0) = base
;
189 base
= get_base_address (TREE_OPERAND (cval
, 0));
193 if ((TREE_CODE (base
) == VAR_DECL
194 || TREE_CODE (base
) == FUNCTION_DECL
)
195 && !can_refer_decl_in_current_unit_p (base
, from_decl
))
197 if (TREE_CODE (base
) == VAR_DECL
)
198 TREE_ADDRESSABLE (base
) = 1;
199 else if (TREE_CODE (base
) == FUNCTION_DECL
)
201 /* Make sure we create a cgraph node for functions we'll reference.
202 They can be non-existent if the reference comes from an entry
203 of an external vtable for example. */
204 cgraph_get_create_node (base
);
206 /* Fixup types in global initializers. */
207 if (TREE_TYPE (TREE_TYPE (cval
)) != TREE_TYPE (TREE_OPERAND (cval
, 0)))
208 cval
= build_fold_addr_expr (TREE_OPERAND (cval
, 0));
210 if (!useless_type_conversion_p (TREE_TYPE (orig_cval
), TREE_TYPE (cval
)))
211 cval
= fold_convert (TREE_TYPE (orig_cval
), cval
);
214 if (TREE_OVERFLOW_P (cval
))
215 return drop_tree_overflow (cval
);
219 /* If SYM is a constant variable with known value, return the value.
220 NULL_TREE is returned otherwise. */
223 get_symbol_constant_value (tree sym
)
225 tree val
= ctor_for_folding (sym
);
226 if (val
!= error_mark_node
)
230 val
= canonicalize_constructor_val (unshare_expr (val
), sym
);
231 if (val
&& is_gimple_min_invariant (val
))
236 /* Variables declared 'const' without an initializer
237 have zero as the initializer if they may not be
238 overridden at link or run time. */
240 && (INTEGRAL_TYPE_P (TREE_TYPE (sym
))
241 || SCALAR_FLOAT_TYPE_P (TREE_TYPE (sym
))))
242 return build_zero_cst (TREE_TYPE (sym
));
250 /* Subroutine of fold_stmt. We perform several simplifications of the
251 memory reference tree EXPR and make sure to re-gimplify them properly
252 after propagation of constant addresses. IS_LHS is true if the
253 reference is supposed to be an lvalue. */
256 maybe_fold_reference (tree expr
, bool is_lhs
)
261 if ((TREE_CODE (expr
) == VIEW_CONVERT_EXPR
262 || TREE_CODE (expr
) == REALPART_EXPR
263 || TREE_CODE (expr
) == IMAGPART_EXPR
)
264 && CONSTANT_CLASS_P (TREE_OPERAND (expr
, 0)))
265 return fold_unary_loc (EXPR_LOCATION (expr
),
268 TREE_OPERAND (expr
, 0));
269 else if (TREE_CODE (expr
) == BIT_FIELD_REF
270 && CONSTANT_CLASS_P (TREE_OPERAND (expr
, 0)))
271 return fold_ternary_loc (EXPR_LOCATION (expr
),
274 TREE_OPERAND (expr
, 0),
275 TREE_OPERAND (expr
, 1),
276 TREE_OPERAND (expr
, 2));
278 while (handled_component_p (*t
))
279 t
= &TREE_OPERAND (*t
, 0);
281 /* Canonicalize MEM_REFs invariant address operand. Do this first
282 to avoid feeding non-canonical MEM_REFs elsewhere. */
283 if (TREE_CODE (*t
) == MEM_REF
284 && !is_gimple_mem_ref_addr (TREE_OPERAND (*t
, 0)))
286 bool volatile_p
= TREE_THIS_VOLATILE (*t
);
287 tree tem
= fold_binary (MEM_REF
, TREE_TYPE (*t
),
288 TREE_OPERAND (*t
, 0),
289 TREE_OPERAND (*t
, 1));
292 TREE_THIS_VOLATILE (tem
) = volatile_p
;
294 tem
= maybe_fold_reference (expr
, is_lhs
);
302 && (result
= fold_const_aggregate_ref (expr
))
303 && is_gimple_min_invariant (result
))
306 /* Fold back MEM_REFs to reference trees. */
307 if (TREE_CODE (*t
) == MEM_REF
308 && TREE_CODE (TREE_OPERAND (*t
, 0)) == ADDR_EXPR
309 && integer_zerop (TREE_OPERAND (*t
, 1))
310 && (TREE_THIS_VOLATILE (*t
)
311 == TREE_THIS_VOLATILE (TREE_OPERAND (TREE_OPERAND (*t
, 0), 0)))
312 && !TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (TREE_OPERAND (*t
, 1)))
313 && (TYPE_MAIN_VARIANT (TREE_TYPE (*t
))
314 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (*t
, 1)))))
315 /* We have to look out here to not drop a required conversion
316 from the rhs to the lhs if is_lhs, but we don't have the
317 rhs here to verify that. Thus require strict type
319 && types_compatible_p (TREE_TYPE (*t
),
320 TREE_TYPE (TREE_OPERAND
321 (TREE_OPERAND (*t
, 0), 0))))
324 *t
= TREE_OPERAND (TREE_OPERAND (*t
, 0), 0);
325 tem
= maybe_fold_reference (expr
, is_lhs
);
330 else if (TREE_CODE (*t
) == TARGET_MEM_REF
)
332 tree tem
= maybe_fold_tmr (*t
);
336 tem
= maybe_fold_reference (expr
, is_lhs
);
347 /* Attempt to fold an assignment statement pointed-to by SI. Returns a
348 replacement rhs for the statement or NULL_TREE if no simplification
349 could be made. It is assumed that the operands have been previously
353 fold_gimple_assign (gimple_stmt_iterator
*si
)
355 gimple stmt
= gsi_stmt (*si
);
356 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
357 location_t loc
= gimple_location (stmt
);
359 tree result
= NULL_TREE
;
361 switch (get_gimple_rhs_class (subcode
))
363 case GIMPLE_SINGLE_RHS
:
365 tree rhs
= gimple_assign_rhs1 (stmt
);
367 if (REFERENCE_CLASS_P (rhs
))
368 return maybe_fold_reference (rhs
, false);
370 else if (TREE_CODE (rhs
) == OBJ_TYPE_REF
)
372 tree val
= OBJ_TYPE_REF_EXPR (rhs
);
373 if (is_gimple_min_invariant (val
))
375 else if (flag_devirtualize
&& virtual_method_call_p (val
))
378 vec
<cgraph_node
*>targets
379 = possible_polymorphic_call_targets (val
, stmt
, &final
);
380 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
384 if (targets
.length () == 1)
385 fndecl
= targets
[0]->decl
;
387 fndecl
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
388 if (dump_enabled_p ())
390 location_t loc
= gimple_location_safe (stmt
);
391 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, loc
,
392 "resolving virtual function address "
393 "reference to function %s\n",
394 targets
.length () == 1
395 ? targets
[0]->name ()
396 : "__builtin_unreachable");
398 val
= fold_convert (TREE_TYPE (val
), fndecl
);
399 STRIP_USELESS_TYPE_CONVERSION (val
);
405 else if (TREE_CODE (rhs
) == ADDR_EXPR
)
407 tree ref
= TREE_OPERAND (rhs
, 0);
408 tree tem
= maybe_fold_reference (ref
, true);
410 && TREE_CODE (tem
) == MEM_REF
411 && integer_zerop (TREE_OPERAND (tem
, 1)))
412 result
= fold_convert (TREE_TYPE (rhs
), TREE_OPERAND (tem
, 0));
414 result
= fold_convert (TREE_TYPE (rhs
),
415 build_fold_addr_expr_loc (loc
, tem
));
416 else if (TREE_CODE (ref
) == MEM_REF
417 && integer_zerop (TREE_OPERAND (ref
, 1)))
418 result
= fold_convert (TREE_TYPE (rhs
), TREE_OPERAND (ref
, 0));
421 else if (TREE_CODE (rhs
) == CONSTRUCTOR
422 && TREE_CODE (TREE_TYPE (rhs
)) == VECTOR_TYPE
423 && (CONSTRUCTOR_NELTS (rhs
)
424 == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs
))))
426 /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */
430 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs
), i
, val
)
431 if (TREE_CODE (val
) != INTEGER_CST
432 && TREE_CODE (val
) != REAL_CST
433 && TREE_CODE (val
) != FIXED_CST
)
436 return build_vector_from_ctor (TREE_TYPE (rhs
),
437 CONSTRUCTOR_ELTS (rhs
));
440 else if (DECL_P (rhs
))
441 return get_symbol_constant_value (rhs
);
443 /* If we couldn't fold the RHS, hand over to the generic
445 if (result
== NULL_TREE
)
448 /* Strip away useless type conversions. Both the NON_LVALUE_EXPR
449 that may have been added by fold, and "useless" type
450 conversions that might now be apparent due to propagation. */
451 STRIP_USELESS_TYPE_CONVERSION (result
);
453 if (result
!= rhs
&& valid_gimple_rhs_p (result
))
460 case GIMPLE_UNARY_RHS
:
462 tree rhs
= gimple_assign_rhs1 (stmt
);
464 result
= fold_unary_loc (loc
, subcode
, gimple_expr_type (stmt
), rhs
);
467 /* If the operation was a conversion do _not_ mark a
468 resulting constant with TREE_OVERFLOW if the original
469 constant was not. These conversions have implementation
470 defined behavior and retaining the TREE_OVERFLOW flag
471 here would confuse later passes such as VRP. */
472 if (CONVERT_EXPR_CODE_P (subcode
)
473 && TREE_CODE (result
) == INTEGER_CST
474 && TREE_CODE (rhs
) == INTEGER_CST
)
475 TREE_OVERFLOW (result
) = TREE_OVERFLOW (rhs
);
477 STRIP_USELESS_TYPE_CONVERSION (result
);
478 if (valid_gimple_rhs_p (result
))
484 case GIMPLE_BINARY_RHS
:
485 /* Try to canonicalize for boolean-typed X the comparisons
486 X == 0, X == 1, X != 0, and X != 1. */
487 if (gimple_assign_rhs_code (stmt
) == EQ_EXPR
488 || gimple_assign_rhs_code (stmt
) == NE_EXPR
)
490 tree lhs
= gimple_assign_lhs (stmt
);
491 tree op1
= gimple_assign_rhs1 (stmt
);
492 tree op2
= gimple_assign_rhs2 (stmt
);
493 tree type
= TREE_TYPE (op1
);
495 /* Check whether the comparison operands are of the same boolean
496 type as the result type is.
497 Check that second operand is an integer-constant with value
499 if (TREE_CODE (op2
) == INTEGER_CST
500 && (integer_zerop (op2
) || integer_onep (op2
))
501 && useless_type_conversion_p (TREE_TYPE (lhs
), type
))
503 enum tree_code cmp_code
= gimple_assign_rhs_code (stmt
);
504 bool is_logical_not
= false;
506 /* X == 0 and X != 1 is a logical-not.of X
507 X == 1 and X != 0 is X */
508 if ((cmp_code
== EQ_EXPR
&& integer_zerop (op2
))
509 || (cmp_code
== NE_EXPR
&& integer_onep (op2
)))
510 is_logical_not
= true;
512 if (is_logical_not
== false)
514 /* Only for one-bit precision typed X the transformation
515 !X -> ~X is valied. */
516 else if (TYPE_PRECISION (type
) == 1)
517 result
= build1_loc (gimple_location (stmt
), BIT_NOT_EXPR
,
519 /* Otherwise we use !X -> X ^ 1. */
521 result
= build2_loc (gimple_location (stmt
), BIT_XOR_EXPR
,
522 type
, op1
, build_int_cst (type
, 1));
528 result
= fold_binary_loc (loc
, subcode
,
529 TREE_TYPE (gimple_assign_lhs (stmt
)),
530 gimple_assign_rhs1 (stmt
),
531 gimple_assign_rhs2 (stmt
));
535 STRIP_USELESS_TYPE_CONVERSION (result
);
536 if (valid_gimple_rhs_p (result
))
541 case GIMPLE_TERNARY_RHS
:
542 /* Try to fold a conditional expression. */
543 if (gimple_assign_rhs_code (stmt
) == COND_EXPR
)
545 tree op0
= gimple_assign_rhs1 (stmt
);
548 location_t cond_loc
= gimple_location (stmt
);
550 if (COMPARISON_CLASS_P (op0
))
552 fold_defer_overflow_warnings ();
553 tem
= fold_binary_loc (cond_loc
,
554 TREE_CODE (op0
), TREE_TYPE (op0
),
555 TREE_OPERAND (op0
, 0),
556 TREE_OPERAND (op0
, 1));
557 /* This is actually a conditional expression, not a GIMPLE
558 conditional statement, however, the valid_gimple_rhs_p
559 test still applies. */
560 set
= (tem
&& is_gimple_condexpr (tem
)
561 && valid_gimple_rhs_p (tem
));
562 fold_undefer_overflow_warnings (set
, stmt
, 0);
564 else if (is_gimple_min_invariant (op0
))
573 result
= fold_build3_loc (cond_loc
, COND_EXPR
,
574 TREE_TYPE (gimple_assign_lhs (stmt
)), tem
,
575 gimple_assign_rhs2 (stmt
),
576 gimple_assign_rhs3 (stmt
));
580 result
= fold_ternary_loc (loc
, subcode
,
581 TREE_TYPE (gimple_assign_lhs (stmt
)),
582 gimple_assign_rhs1 (stmt
),
583 gimple_assign_rhs2 (stmt
),
584 gimple_assign_rhs3 (stmt
));
588 STRIP_USELESS_TYPE_CONVERSION (result
);
589 if (valid_gimple_rhs_p (result
))
594 case GIMPLE_INVALID_RHS
:
601 /* Attempt to fold a conditional statement. Return true if any changes were
602 made. We only attempt to fold the condition expression, and do not perform
603 any transformation that would require alteration of the cfg. It is
604 assumed that the operands have been previously folded. */
607 fold_gimple_cond (gimple stmt
)
609 tree result
= fold_binary_loc (gimple_location (stmt
),
610 gimple_cond_code (stmt
),
612 gimple_cond_lhs (stmt
),
613 gimple_cond_rhs (stmt
));
617 STRIP_USELESS_TYPE_CONVERSION (result
);
618 if (is_gimple_condexpr (result
) && valid_gimple_rhs_p (result
))
620 gimple_cond_set_condition_from_tree (stmt
, result
);
628 /* Convert EXPR into a GIMPLE value suitable for substitution on the
629 RHS of an assignment. Insert the necessary statements before
630 iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL
631 is replaced. If the call is expected to produces a result, then it
632 is replaced by an assignment of the new RHS to the result variable.
633 If the result is to be ignored, then the call is replaced by a
634 GIMPLE_NOP. A proper VDEF chain is retained by making the first
635 VUSE and the last VDEF of the whole sequence be the same as the replaced
636 statement and using new SSA names for stores in between. */
639 gimplify_and_update_call_from_tree (gimple_stmt_iterator
*si_p
, tree expr
)
642 gimple stmt
, new_stmt
;
643 gimple_stmt_iterator i
;
644 gimple_seq stmts
= NULL
;
648 stmt
= gsi_stmt (*si_p
);
650 gcc_assert (is_gimple_call (stmt
));
652 push_gimplify_context (gimple_in_ssa_p (cfun
));
654 lhs
= gimple_call_lhs (stmt
);
655 if (lhs
== NULL_TREE
)
657 gimplify_and_add (expr
, &stmts
);
658 /* We can end up with folding a memcpy of an empty class assignment
659 which gets optimized away by C++ gimplification. */
660 if (gimple_seq_empty_p (stmts
))
662 pop_gimplify_context (NULL
);
663 if (gimple_in_ssa_p (cfun
))
665 unlink_stmt_vdef (stmt
);
668 gsi_replace (si_p
, gimple_build_nop (), true);
674 tree tmp
= get_initialized_tmp_var (expr
, &stmts
, NULL
);
675 new_stmt
= gimple_build_assign (lhs
, tmp
);
676 i
= gsi_last (stmts
);
677 gsi_insert_after_without_update (&i
, new_stmt
,
678 GSI_CONTINUE_LINKING
);
681 pop_gimplify_context (NULL
);
683 if (gimple_has_location (stmt
))
684 annotate_all_with_location (stmts
, gimple_location (stmt
));
686 /* First iterate over the replacement statements backward, assigning
687 virtual operands to their defining statements. */
689 for (i
= gsi_last (stmts
); !gsi_end_p (i
); gsi_prev (&i
))
691 new_stmt
= gsi_stmt (i
);
692 if ((gimple_assign_single_p (new_stmt
)
693 && !is_gimple_reg (gimple_assign_lhs (new_stmt
)))
694 || (is_gimple_call (new_stmt
)
695 && (gimple_call_flags (new_stmt
)
696 & (ECF_NOVOPS
| ECF_PURE
| ECF_CONST
| ECF_NORETURN
)) == 0))
700 vdef
= gimple_vdef (stmt
);
702 vdef
= make_ssa_name (gimple_vop (cfun
), new_stmt
);
703 gimple_set_vdef (new_stmt
, vdef
);
704 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
705 SSA_NAME_DEF_STMT (vdef
) = new_stmt
;
706 laststore
= new_stmt
;
710 /* Second iterate over the statements forward, assigning virtual
711 operands to their uses. */
712 reaching_vuse
= gimple_vuse (stmt
);
713 for (i
= gsi_start (stmts
); !gsi_end_p (i
); gsi_next (&i
))
715 new_stmt
= gsi_stmt (i
);
716 /* If the new statement possibly has a VUSE, update it with exact SSA
717 name we know will reach this one. */
718 if (gimple_has_mem_ops (new_stmt
))
719 gimple_set_vuse (new_stmt
, reaching_vuse
);
720 gimple_set_modified (new_stmt
, true);
721 if (gimple_vdef (new_stmt
))
722 reaching_vuse
= gimple_vdef (new_stmt
);
725 /* If the new sequence does not do a store release the virtual
726 definition of the original statement. */
728 && reaching_vuse
== gimple_vuse (stmt
))
730 tree vdef
= gimple_vdef (stmt
);
732 && TREE_CODE (vdef
) == SSA_NAME
)
734 unlink_stmt_vdef (stmt
);
735 release_ssa_name (vdef
);
739 /* Finally replace the original statement with the sequence. */
740 gsi_replace_with_seq (si_p
, stmts
, false);
743 /* Return the string length, maximum string length or maximum value of
745 If ARG is an SSA name variable, follow its use-def chains. If LENGTH
746 is not NULL and, for TYPE == 0, its value is not equal to the length
747 we determine or if we are unable to determine the length or value,
748 return false. VISITED is a bitmap of visited variables.
749 TYPE is 0 if string length should be returned, 1 for maximum string
750 length and 2 for maximum value ARG can have. */
753 get_maxval_strlen (tree arg
, tree
*length
, bitmap visited
, int type
)
758 if (TREE_CODE (arg
) != SSA_NAME
)
760 /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */
761 if (TREE_CODE (arg
) == ADDR_EXPR
762 && TREE_CODE (TREE_OPERAND (arg
, 0)) == ARRAY_REF
763 && integer_zerop (TREE_OPERAND (TREE_OPERAND (arg
, 0), 1)))
765 tree aop0
= TREE_OPERAND (TREE_OPERAND (arg
, 0), 0);
766 if (TREE_CODE (aop0
) == INDIRECT_REF
767 && TREE_CODE (TREE_OPERAND (aop0
, 0)) == SSA_NAME
)
768 return get_maxval_strlen (TREE_OPERAND (aop0
, 0),
769 length
, visited
, type
);
775 if (TREE_CODE (val
) != INTEGER_CST
776 || tree_int_cst_sgn (val
) < 0)
780 val
= c_strlen (arg
, 1);
788 if (TREE_CODE (*length
) != INTEGER_CST
789 || TREE_CODE (val
) != INTEGER_CST
)
792 if (tree_int_cst_lt (*length
, val
))
796 else if (simple_cst_equal (val
, *length
) != 1)
804 /* If ARG is registered for SSA update we cannot look at its defining
806 if (name_registered_for_update_p (arg
))
809 /* If we were already here, break the infinite cycle. */
810 if (!bitmap_set_bit (visited
, SSA_NAME_VERSION (arg
)))
814 def_stmt
= SSA_NAME_DEF_STMT (var
);
816 switch (gimple_code (def_stmt
))
819 /* The RHS of the statement defining VAR must either have a
820 constant length or come from another SSA_NAME with a constant
822 if (gimple_assign_single_p (def_stmt
)
823 || gimple_assign_unary_nop_p (def_stmt
))
825 tree rhs
= gimple_assign_rhs1 (def_stmt
);
826 return get_maxval_strlen (rhs
, length
, visited
, type
);
828 else if (gimple_assign_rhs_code (def_stmt
) == COND_EXPR
)
830 tree op2
= gimple_assign_rhs2 (def_stmt
);
831 tree op3
= gimple_assign_rhs3 (def_stmt
);
832 return get_maxval_strlen (op2
, length
, visited
, type
)
833 && get_maxval_strlen (op3
, length
, visited
, type
);
839 /* All the arguments of the PHI node must have the same constant
843 for (i
= 0; i
< gimple_phi_num_args (def_stmt
); i
++)
845 tree arg
= gimple_phi_arg (def_stmt
, i
)->def
;
847 /* If this PHI has itself as an argument, we cannot
848 determine the string length of this argument. However,
849 if we can find a constant string length for the other
850 PHI args then we can still be sure that this is a
851 constant string length. So be optimistic and just
852 continue with the next argument. */
853 if (arg
== gimple_phi_result (def_stmt
))
856 if (!get_maxval_strlen (arg
, length
, visited
, type
))
868 /* Fold builtin call in statement STMT. Returns a simplified tree.
869 We may return a non-constant expression, including another call
870 to a different function and with different arguments, e.g.,
871 substituting memcpy for strcpy when the string length is known.
872 Note that some builtins expand into inline code that may not
873 be valid in GIMPLE. Callers must take care. */
876 gimple_fold_builtin (gimple stmt
)
884 location_t loc
= gimple_location (stmt
);
886 ignore
= (gimple_call_lhs (stmt
) == NULL
);
888 /* First try the generic builtin folder. If that succeeds, return the
890 result
= fold_call_stmt (stmt
, ignore
);
896 result
= fold_convert (gimple_call_return_type (stmt
), result
);
900 /* Ignore MD builtins. */
901 callee
= gimple_call_fndecl (stmt
);
902 if (DECL_BUILT_IN_CLASS (callee
) == BUILT_IN_MD
)
905 /* Give up for always_inline inline builtins until they are
907 if (avoid_folding_inline_builtin (callee
))
910 /* If the builtin could not be folded, and it has no argument list,
912 nargs
= gimple_call_num_args (stmt
);
916 /* Limit the work only for builtins we know how to simplify. */
917 switch (DECL_FUNCTION_CODE (callee
))
919 case BUILT_IN_STRLEN
:
921 case BUILT_IN_FPUTS_UNLOCKED
:
925 case BUILT_IN_STRCPY
:
926 case BUILT_IN_STRNCPY
:
927 case BUILT_IN_STRCAT
:
931 case BUILT_IN_MEMCPY_CHK
:
932 case BUILT_IN_MEMPCPY_CHK
:
933 case BUILT_IN_MEMMOVE_CHK
:
934 case BUILT_IN_MEMSET_CHK
:
935 case BUILT_IN_STRNCPY_CHK
:
936 case BUILT_IN_STPNCPY_CHK
:
940 case BUILT_IN_STRCPY_CHK
:
941 case BUILT_IN_STPCPY_CHK
:
945 case BUILT_IN_SNPRINTF_CHK
:
946 case BUILT_IN_VSNPRINTF_CHK
:
954 if (arg_idx
>= nargs
)
957 /* Try to use the dataflow information gathered by the CCP process. */
958 visited
= BITMAP_ALLOC (NULL
);
959 bitmap_clear (visited
);
961 memset (val
, 0, sizeof (val
));
962 a
= gimple_call_arg (stmt
, arg_idx
);
963 if (!get_maxval_strlen (a
, &val
[arg_idx
], visited
, type
))
964 val
[arg_idx
] = NULL_TREE
;
966 BITMAP_FREE (visited
);
969 switch (DECL_FUNCTION_CODE (callee
))
971 case BUILT_IN_STRLEN
:
972 if (val
[0] && nargs
== 1)
975 fold_convert (TREE_TYPE (gimple_call_lhs (stmt
)), val
[0]);
977 /* If the result is not a valid gimple value, or not a cast
978 of a valid gimple value, then we cannot use the result. */
979 if (is_gimple_val (new_val
)
980 || (CONVERT_EXPR_P (new_val
)
981 && is_gimple_val (TREE_OPERAND (new_val
, 0))))
986 case BUILT_IN_STRCPY
:
987 if (val
[1] && is_gimple_val (val
[1]) && nargs
== 2)
988 result
= fold_builtin_strcpy (loc
, callee
,
989 gimple_call_arg (stmt
, 0),
990 gimple_call_arg (stmt
, 1),
994 case BUILT_IN_STRNCPY
:
995 if (val
[1] && is_gimple_val (val
[1]) && nargs
== 3)
996 result
= fold_builtin_strncpy (loc
, callee
,
997 gimple_call_arg (stmt
, 0),
998 gimple_call_arg (stmt
, 1),
999 gimple_call_arg (stmt
, 2),
1003 case BUILT_IN_STRCAT
:
1004 if (val
[1] && is_gimple_val (val
[1]) && nargs
== 2)
1005 result
= fold_builtin_strcat (loc
, gimple_call_arg (stmt
, 0),
1006 gimple_call_arg (stmt
, 1),
1010 case BUILT_IN_FPUTS
:
1012 result
= fold_builtin_fputs (loc
, gimple_call_arg (stmt
, 0),
1013 gimple_call_arg (stmt
, 1),
1014 ignore
, false, val
[0]);
1017 case BUILT_IN_FPUTS_UNLOCKED
:
1019 result
= fold_builtin_fputs (loc
, gimple_call_arg (stmt
, 0),
1020 gimple_call_arg (stmt
, 1),
1021 ignore
, true, val
[0]);
1024 case BUILT_IN_MEMCPY_CHK
:
1025 case BUILT_IN_MEMPCPY_CHK
:
1026 case BUILT_IN_MEMMOVE_CHK
:
1027 case BUILT_IN_MEMSET_CHK
:
1028 if (val
[2] && is_gimple_val (val
[2]) && nargs
== 4)
1029 result
= fold_builtin_memory_chk (loc
, callee
,
1030 gimple_call_arg (stmt
, 0),
1031 gimple_call_arg (stmt
, 1),
1032 gimple_call_arg (stmt
, 2),
1033 gimple_call_arg (stmt
, 3),
1035 DECL_FUNCTION_CODE (callee
));
1038 case BUILT_IN_STRCPY_CHK
:
1039 case BUILT_IN_STPCPY_CHK
:
1040 if (val
[1] && is_gimple_val (val
[1]) && nargs
== 3)
1041 result
= fold_builtin_stxcpy_chk (loc
, callee
,
1042 gimple_call_arg (stmt
, 0),
1043 gimple_call_arg (stmt
, 1),
1044 gimple_call_arg (stmt
, 2),
1046 DECL_FUNCTION_CODE (callee
));
1049 case BUILT_IN_STRNCPY_CHK
:
1050 case BUILT_IN_STPNCPY_CHK
:
1051 if (val
[2] && is_gimple_val (val
[2]) && nargs
== 4)
1052 result
= fold_builtin_stxncpy_chk (loc
, gimple_call_arg (stmt
, 0),
1053 gimple_call_arg (stmt
, 1),
1054 gimple_call_arg (stmt
, 2),
1055 gimple_call_arg (stmt
, 3),
1057 DECL_FUNCTION_CODE (callee
));
1060 case BUILT_IN_SNPRINTF_CHK
:
1061 case BUILT_IN_VSNPRINTF_CHK
:
1062 if (val
[1] && is_gimple_val (val
[1]))
1063 result
= gimple_fold_builtin_snprintf_chk (stmt
, val
[1],
1064 DECL_FUNCTION_CODE (callee
));
1071 if (result
&& ignore
)
1072 result
= fold_ignored_result (result
);
1077 /* Attempt to fold a call statement referenced by the statement iterator GSI.
1078 The statement may be replaced by another statement, e.g., if the call
1079 simplifies to a constant value. Return true if any changes were made.
1080 It is assumed that the operands have been previously folded. */
1083 gimple_fold_call (gimple_stmt_iterator
*gsi
, bool inplace
)
1085 gimple stmt
= gsi_stmt (*gsi
);
1087 bool changed
= false;
1090 /* Fold *& in call arguments. */
1091 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
1092 if (REFERENCE_CLASS_P (gimple_call_arg (stmt
, i
)))
1094 tree tmp
= maybe_fold_reference (gimple_call_arg (stmt
, i
), false);
1097 gimple_call_set_arg (stmt
, i
, tmp
);
1102 /* Check for virtual calls that became direct calls. */
1103 callee
= gimple_call_fn (stmt
);
1104 if (callee
&& TREE_CODE (callee
) == OBJ_TYPE_REF
)
1106 if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee
)) != NULL_TREE
)
1108 if (dump_file
&& virtual_method_call_p (callee
)
1109 && !possible_polymorphic_call_target_p
1110 (callee
, cgraph_get_node (gimple_call_addr_fndecl
1111 (OBJ_TYPE_REF_EXPR (callee
)))))
1114 "Type inheritance inconsistent devirtualization of ");
1115 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1116 fprintf (dump_file
, " to ");
1117 print_generic_expr (dump_file
, callee
, TDF_SLIM
);
1118 fprintf (dump_file
, "\n");
1121 gimple_call_set_fn (stmt
, OBJ_TYPE_REF_EXPR (callee
));
1124 else if (flag_devirtualize
&& !inplace
&& virtual_method_call_p (callee
))
1127 vec
<cgraph_node
*>targets
1128 = possible_polymorphic_call_targets (callee
, stmt
, &final
);
1129 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
1131 tree lhs
= gimple_call_lhs (stmt
);
1132 if (dump_enabled_p ())
1134 location_t loc
= gimple_location_safe (stmt
);
1135 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, loc
,
1136 "folding virtual function call to %s\n",
1137 targets
.length () == 1
1138 ? targets
[0]->name ()
1139 : "__builtin_unreachable");
1141 if (targets
.length () == 1)
1143 gimple_call_set_fndecl (stmt
, targets
[0]->decl
);
1145 /* If the call becomes noreturn, remove the lhs. */
1146 if (lhs
&& (gimple_call_flags (stmt
) & ECF_NORETURN
))
1148 if (TREE_CODE (lhs
) == SSA_NAME
)
1150 tree var
= create_tmp_var (TREE_TYPE (lhs
), NULL
);
1151 tree def
= get_or_create_ssa_default_def (cfun
, var
);
1152 gimple new_stmt
= gimple_build_assign (lhs
, def
);
1153 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
1155 gimple_call_set_lhs (stmt
, NULL_TREE
);
1160 tree fndecl
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
1161 gimple new_stmt
= gimple_build_call (fndecl
, 0);
1162 gimple_set_location (new_stmt
, gimple_location (stmt
));
1163 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
1165 tree var
= create_tmp_var (TREE_TYPE (lhs
), NULL
);
1166 tree def
= get_or_create_ssa_default_def (cfun
, var
);
1168 /* To satisfy condition for
1169 cgraph_update_edges_for_call_stmt_node,
1170 we need to preserve GIMPLE_CALL statement
1171 at position of GSI iterator. */
1172 update_call_from_tree (gsi
, def
);
1173 gsi_insert_before (gsi
, new_stmt
, GSI_NEW_STMT
);
1176 gsi_replace (gsi
, new_stmt
, true);
1186 /* Check for builtins that CCP can handle using information not
1187 available in the generic fold routines. */
1188 if (gimple_call_builtin_p (stmt
))
1190 tree result
= gimple_fold_builtin (stmt
);
1193 if (!update_call_from_tree (gsi
, result
))
1194 gimplify_and_update_call_from_tree (gsi
, result
);
1197 else if (gimple_call_builtin_p (stmt
, BUILT_IN_MD
))
1198 changed
|= targetm
.gimple_fold_builtin (gsi
);
1200 else if (gimple_call_internal_p (stmt
))
1202 enum tree_code subcode
= ERROR_MARK
;
1203 tree result
= NULL_TREE
;
1204 switch (gimple_call_internal_fn (stmt
))
1206 case IFN_BUILTIN_EXPECT
:
1207 result
= fold_builtin_expect (gimple_location (stmt
),
1208 gimple_call_arg (stmt
, 0),
1209 gimple_call_arg (stmt
, 1),
1210 gimple_call_arg (stmt
, 2));
1212 case IFN_UBSAN_CHECK_ADD
:
1213 subcode
= PLUS_EXPR
;
1215 case IFN_UBSAN_CHECK_SUB
:
1216 subcode
= MINUS_EXPR
;
1218 case IFN_UBSAN_CHECK_MUL
:
1219 subcode
= MULT_EXPR
;
1224 if (subcode
!= ERROR_MARK
)
1226 tree arg0
= gimple_call_arg (stmt
, 0);
1227 tree arg1
= gimple_call_arg (stmt
, 1);
1228 /* x = y + 0; x = y - 0; x = y * 0; */
1229 if (integer_zerop (arg1
))
1230 result
= subcode
== MULT_EXPR
1231 ? build_zero_cst (TREE_TYPE (arg0
))
1233 /* x = 0 + y; x = 0 * y; */
1234 else if (subcode
!= MINUS_EXPR
&& integer_zerop (arg0
))
1235 result
= subcode
== MULT_EXPR
1236 ? build_zero_cst (TREE_TYPE (arg0
))
1239 else if (subcode
== MINUS_EXPR
&& operand_equal_p (arg0
, arg1
, 0))
1240 result
= build_zero_cst (TREE_TYPE (arg0
));
1241 /* x = y * 1; x = 1 * y; */
1242 else if (subcode
== MULT_EXPR
)
1244 if (integer_onep (arg1
))
1246 else if (integer_onep (arg0
))
1252 if (!update_call_from_tree (gsi
, result
))
1253 gimplify_and_update_call_from_tree (gsi
, result
);
1261 /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument
1262 distinguishes both cases. */
1265 fold_stmt_1 (gimple_stmt_iterator
*gsi
, bool inplace
)
1267 bool changed
= false;
1268 gimple stmt
= gsi_stmt (*gsi
);
1271 /* Fold the main computation performed by the statement. */
1272 switch (gimple_code (stmt
))
1276 unsigned old_num_ops
= gimple_num_ops (stmt
);
1277 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
1278 tree lhs
= gimple_assign_lhs (stmt
);
1280 /* First canonicalize operand order. This avoids building new
1281 trees if this is the only thing fold would later do. */
1282 if ((commutative_tree_code (subcode
)
1283 || commutative_ternary_tree_code (subcode
))
1284 && tree_swap_operands_p (gimple_assign_rhs1 (stmt
),
1285 gimple_assign_rhs2 (stmt
), false))
1287 tree tem
= gimple_assign_rhs1 (stmt
);
1288 gimple_assign_set_rhs1 (stmt
, gimple_assign_rhs2 (stmt
));
1289 gimple_assign_set_rhs2 (stmt
, tem
);
1292 new_rhs
= fold_gimple_assign (gsi
);
1294 && !useless_type_conversion_p (TREE_TYPE (lhs
),
1295 TREE_TYPE (new_rhs
)))
1296 new_rhs
= fold_convert (TREE_TYPE (lhs
), new_rhs
);
1299 || get_gimple_rhs_num_ops (TREE_CODE (new_rhs
)) < old_num_ops
))
1301 gimple_assign_set_rhs_from_tree (gsi
, new_rhs
);
1308 changed
|= fold_gimple_cond (stmt
);
1312 changed
|= gimple_fold_call (gsi
, inplace
);
1316 /* Fold *& in asm operands. */
1319 const char **oconstraints
;
1320 const char *constraint
;
1321 bool allows_mem
, allows_reg
;
1323 noutputs
= gimple_asm_noutputs (stmt
);
1324 oconstraints
= XALLOCAVEC (const char *, noutputs
);
1326 for (i
= 0; i
< gimple_asm_noutputs (stmt
); ++i
)
1328 tree link
= gimple_asm_output_op (stmt
, i
);
1329 tree op
= TREE_VALUE (link
);
1331 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
1332 if (REFERENCE_CLASS_P (op
)
1333 && (op
= maybe_fold_reference (op
, true)) != NULL_TREE
)
1335 TREE_VALUE (link
) = op
;
1339 for (i
= 0; i
< gimple_asm_ninputs (stmt
); ++i
)
1341 tree link
= gimple_asm_input_op (stmt
, i
);
1342 tree op
= TREE_VALUE (link
);
1344 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
1345 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0,
1346 oconstraints
, &allows_mem
, &allows_reg
);
1347 if (REFERENCE_CLASS_P (op
)
1348 && (op
= maybe_fold_reference (op
, !allows_reg
&& allows_mem
))
1351 TREE_VALUE (link
) = op
;
1359 if (gimple_debug_bind_p (stmt
))
1361 tree val
= gimple_debug_bind_get_value (stmt
);
1363 && REFERENCE_CLASS_P (val
))
1365 tree tem
= maybe_fold_reference (val
, false);
1368 gimple_debug_bind_set_value (stmt
, tem
);
1373 && TREE_CODE (val
) == ADDR_EXPR
)
1375 tree ref
= TREE_OPERAND (val
, 0);
1376 tree tem
= maybe_fold_reference (ref
, false);
1379 tem
= build_fold_addr_expr_with_type (tem
, TREE_TYPE (val
));
1380 gimple_debug_bind_set_value (stmt
, tem
);
1390 stmt
= gsi_stmt (*gsi
);
1392 /* Fold *& on the lhs. */
1393 if (gimple_has_lhs (stmt
))
1395 tree lhs
= gimple_get_lhs (stmt
);
1396 if (lhs
&& REFERENCE_CLASS_P (lhs
))
1398 tree new_lhs
= maybe_fold_reference (lhs
, true);
1401 gimple_set_lhs (stmt
, new_lhs
);
1410 /* Fold the statement pointed to by GSI. In some cases, this function may
1411 replace the whole statement with a new one. Returns true iff folding
1413 The statement pointed to by GSI should be in valid gimple form but may
1414 be in unfolded state as resulting from for example constant propagation
1415 which can produce *&x = 0. */
1418 fold_stmt (gimple_stmt_iterator
*gsi
)
1420 return fold_stmt_1 (gsi
, false);
1423 /* Perform the minimal folding on statement *GSI. Only operations like
1424 *&x created by constant propagation are handled. The statement cannot
1425 be replaced with a new one. Return true if the statement was
1426 changed, false otherwise.
1427 The statement *GSI should be in valid gimple form but may
1428 be in unfolded state as resulting from for example constant propagation
1429 which can produce *&x = 0. */
1432 fold_stmt_inplace (gimple_stmt_iterator
*gsi
)
1434 gimple stmt
= gsi_stmt (*gsi
);
1435 bool changed
= fold_stmt_1 (gsi
, true);
1436 gcc_assert (gsi_stmt (*gsi
) == stmt
);
1440 /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
1441 if EXPR is null or we don't know how.
1442 If non-null, the result always has boolean type. */
1445 canonicalize_bool (tree expr
, bool invert
)
1451 if (integer_nonzerop (expr
))
1452 return boolean_false_node
;
1453 else if (integer_zerop (expr
))
1454 return boolean_true_node
;
1455 else if (TREE_CODE (expr
) == SSA_NAME
)
1456 return fold_build2 (EQ_EXPR
, boolean_type_node
, expr
,
1457 build_int_cst (TREE_TYPE (expr
), 0));
1458 else if (TREE_CODE_CLASS (TREE_CODE (expr
)) == tcc_comparison
)
1459 return fold_build2 (invert_tree_comparison (TREE_CODE (expr
), false),
1461 TREE_OPERAND (expr
, 0),
1462 TREE_OPERAND (expr
, 1));
1468 if (TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
)
1470 if (integer_nonzerop (expr
))
1471 return boolean_true_node
;
1472 else if (integer_zerop (expr
))
1473 return boolean_false_node
;
1474 else if (TREE_CODE (expr
) == SSA_NAME
)
1475 return fold_build2 (NE_EXPR
, boolean_type_node
, expr
,
1476 build_int_cst (TREE_TYPE (expr
), 0));
1477 else if (TREE_CODE_CLASS (TREE_CODE (expr
)) == tcc_comparison
)
1478 return fold_build2 (TREE_CODE (expr
),
1480 TREE_OPERAND (expr
, 0),
1481 TREE_OPERAND (expr
, 1));
1487 /* Check to see if a boolean expression EXPR is logically equivalent to the
1488 comparison (OP1 CODE OP2). Check for various identities involving
1492 same_bool_comparison_p (const_tree expr
, enum tree_code code
,
1493 const_tree op1
, const_tree op2
)
1497 /* The obvious case. */
1498 if (TREE_CODE (expr
) == code
1499 && operand_equal_p (TREE_OPERAND (expr
, 0), op1
, 0)
1500 && operand_equal_p (TREE_OPERAND (expr
, 1), op2
, 0))
1503 /* Check for comparing (name, name != 0) and the case where expr
1504 is an SSA_NAME with a definition matching the comparison. */
1505 if (TREE_CODE (expr
) == SSA_NAME
1506 && TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
)
1508 if (operand_equal_p (expr
, op1
, 0))
1509 return ((code
== NE_EXPR
&& integer_zerop (op2
))
1510 || (code
== EQ_EXPR
&& integer_nonzerop (op2
)));
1511 s
= SSA_NAME_DEF_STMT (expr
);
1512 if (is_gimple_assign (s
)
1513 && gimple_assign_rhs_code (s
) == code
1514 && operand_equal_p (gimple_assign_rhs1 (s
), op1
, 0)
1515 && operand_equal_p (gimple_assign_rhs2 (s
), op2
, 0))
1519 /* If op1 is of the form (name != 0) or (name == 0), and the definition
1520 of name is a comparison, recurse. */
1521 if (TREE_CODE (op1
) == SSA_NAME
1522 && TREE_CODE (TREE_TYPE (op1
)) == BOOLEAN_TYPE
)
1524 s
= SSA_NAME_DEF_STMT (op1
);
1525 if (is_gimple_assign (s
)
1526 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
)
1528 enum tree_code c
= gimple_assign_rhs_code (s
);
1529 if ((c
== NE_EXPR
&& integer_zerop (op2
))
1530 || (c
== EQ_EXPR
&& integer_nonzerop (op2
)))
1531 return same_bool_comparison_p (expr
, c
,
1532 gimple_assign_rhs1 (s
),
1533 gimple_assign_rhs2 (s
));
1534 if ((c
== EQ_EXPR
&& integer_zerop (op2
))
1535 || (c
== NE_EXPR
&& integer_nonzerop (op2
)))
1536 return same_bool_comparison_p (expr
,
1537 invert_tree_comparison (c
, false),
1538 gimple_assign_rhs1 (s
),
1539 gimple_assign_rhs2 (s
));
1545 /* Check to see if two boolean expressions OP1 and OP2 are logically
1549 same_bool_result_p (const_tree op1
, const_tree op2
)
1551 /* Simple cases first. */
1552 if (operand_equal_p (op1
, op2
, 0))
1555 /* Check the cases where at least one of the operands is a comparison.
1556 These are a bit smarter than operand_equal_p in that they apply some
1557 identifies on SSA_NAMEs. */
1558 if (TREE_CODE_CLASS (TREE_CODE (op2
)) == tcc_comparison
1559 && same_bool_comparison_p (op1
, TREE_CODE (op2
),
1560 TREE_OPERAND (op2
, 0),
1561 TREE_OPERAND (op2
, 1)))
1563 if (TREE_CODE_CLASS (TREE_CODE (op1
)) == tcc_comparison
1564 && same_bool_comparison_p (op2
, TREE_CODE (op1
),
1565 TREE_OPERAND (op1
, 0),
1566 TREE_OPERAND (op1
, 1)))
1573 /* Forward declarations for some mutually recursive functions. */
1576 and_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
1577 enum tree_code code2
, tree op2a
, tree op2b
);
1579 and_var_with_comparison (tree var
, bool invert
,
1580 enum tree_code code2
, tree op2a
, tree op2b
);
1582 and_var_with_comparison_1 (gimple stmt
,
1583 enum tree_code code2
, tree op2a
, tree op2b
);
1585 or_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
1586 enum tree_code code2
, tree op2a
, tree op2b
);
1588 or_var_with_comparison (tree var
, bool invert
,
1589 enum tree_code code2
, tree op2a
, tree op2b
);
1591 or_var_with_comparison_1 (gimple stmt
,
1592 enum tree_code code2
, tree op2a
, tree op2b
);
1594 /* Helper function for and_comparisons_1: try to simplify the AND of the
1595 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
1596 If INVERT is true, invert the value of the VAR before doing the AND.
1597 Return NULL_EXPR if we can't simplify this to a single expression. */
1600 and_var_with_comparison (tree var
, bool invert
,
1601 enum tree_code code2
, tree op2a
, tree op2b
)
1604 gimple stmt
= SSA_NAME_DEF_STMT (var
);
1606 /* We can only deal with variables whose definitions are assignments. */
1607 if (!is_gimple_assign (stmt
))
1610 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
1611 !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
1612 Then we only have to consider the simpler non-inverted cases. */
1614 t
= or_var_with_comparison_1 (stmt
,
1615 invert_tree_comparison (code2
, false),
1618 t
= and_var_with_comparison_1 (stmt
, code2
, op2a
, op2b
);
1619 return canonicalize_bool (t
, invert
);
1622 /* Try to simplify the AND of the ssa variable defined by the assignment
1623 STMT with the comparison specified by (OP2A CODE2 OP2B).
1624 Return NULL_EXPR if we can't simplify this to a single expression. */
1627 and_var_with_comparison_1 (gimple stmt
,
1628 enum tree_code code2
, tree op2a
, tree op2b
)
1630 tree var
= gimple_assign_lhs (stmt
);
1631 tree true_test_var
= NULL_TREE
;
1632 tree false_test_var
= NULL_TREE
;
1633 enum tree_code innercode
= gimple_assign_rhs_code (stmt
);
1635 /* Check for identities like (var AND (var == 0)) => false. */
1636 if (TREE_CODE (op2a
) == SSA_NAME
1637 && TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
)
1639 if ((code2
== NE_EXPR
&& integer_zerop (op2b
))
1640 || (code2
== EQ_EXPR
&& integer_nonzerop (op2b
)))
1642 true_test_var
= op2a
;
1643 if (var
== true_test_var
)
1646 else if ((code2
== EQ_EXPR
&& integer_zerop (op2b
))
1647 || (code2
== NE_EXPR
&& integer_nonzerop (op2b
)))
1649 false_test_var
= op2a
;
1650 if (var
== false_test_var
)
1651 return boolean_false_node
;
1655 /* If the definition is a comparison, recurse on it. */
1656 if (TREE_CODE_CLASS (innercode
) == tcc_comparison
)
1658 tree t
= and_comparisons_1 (innercode
,
1659 gimple_assign_rhs1 (stmt
),
1660 gimple_assign_rhs2 (stmt
),
1668 /* If the definition is an AND or OR expression, we may be able to
1669 simplify by reassociating. */
1670 if (TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
1671 && (innercode
== BIT_AND_EXPR
|| innercode
== BIT_IOR_EXPR
))
1673 tree inner1
= gimple_assign_rhs1 (stmt
);
1674 tree inner2
= gimple_assign_rhs2 (stmt
);
1677 tree partial
= NULL_TREE
;
1678 bool is_and
= (innercode
== BIT_AND_EXPR
);
1680 /* Check for boolean identities that don't require recursive examination
1682 inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
1683 inner1 AND (inner1 OR inner2) => inner1
1684 !inner1 AND (inner1 AND inner2) => false
1685 !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
1686 Likewise for similar cases involving inner2. */
1687 if (inner1
== true_test_var
)
1688 return (is_and
? var
: inner1
);
1689 else if (inner2
== true_test_var
)
1690 return (is_and
? var
: inner2
);
1691 else if (inner1
== false_test_var
)
1693 ? boolean_false_node
1694 : and_var_with_comparison (inner2
, false, code2
, op2a
, op2b
));
1695 else if (inner2
== false_test_var
)
1697 ? boolean_false_node
1698 : and_var_with_comparison (inner1
, false, code2
, op2a
, op2b
));
1700 /* Next, redistribute/reassociate the AND across the inner tests.
1701 Compute the first partial result, (inner1 AND (op2a code op2b)) */
1702 if (TREE_CODE (inner1
) == SSA_NAME
1703 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner1
))
1704 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
1705 && (t
= maybe_fold_and_comparisons (gimple_assign_rhs_code (s
),
1706 gimple_assign_rhs1 (s
),
1707 gimple_assign_rhs2 (s
),
1708 code2
, op2a
, op2b
)))
1710 /* Handle the AND case, where we are reassociating:
1711 (inner1 AND inner2) AND (op2a code2 op2b)
1713 If the partial result t is a constant, we win. Otherwise
1714 continue on to try reassociating with the other inner test. */
1717 if (integer_onep (t
))
1719 else if (integer_zerop (t
))
1720 return boolean_false_node
;
1723 /* Handle the OR case, where we are redistributing:
1724 (inner1 OR inner2) AND (op2a code2 op2b)
1725 => (t OR (inner2 AND (op2a code2 op2b))) */
1726 else if (integer_onep (t
))
1727 return boolean_true_node
;
1729 /* Save partial result for later. */
1733 /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
1734 if (TREE_CODE (inner2
) == SSA_NAME
1735 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner2
))
1736 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
1737 && (t
= maybe_fold_and_comparisons (gimple_assign_rhs_code (s
),
1738 gimple_assign_rhs1 (s
),
1739 gimple_assign_rhs2 (s
),
1740 code2
, op2a
, op2b
)))
1742 /* Handle the AND case, where we are reassociating:
1743 (inner1 AND inner2) AND (op2a code2 op2b)
1744 => (inner1 AND t) */
1747 if (integer_onep (t
))
1749 else if (integer_zerop (t
))
1750 return boolean_false_node
;
1751 /* If both are the same, we can apply the identity
1753 else if (partial
&& same_bool_result_p (t
, partial
))
1757 /* Handle the OR case. where we are redistributing:
1758 (inner1 OR inner2) AND (op2a code2 op2b)
1759 => (t OR (inner1 AND (op2a code2 op2b)))
1760 => (t OR partial) */
1763 if (integer_onep (t
))
1764 return boolean_true_node
;
1767 /* We already got a simplification for the other
1768 operand to the redistributed OR expression. The
1769 interesting case is when at least one is false.
1770 Or, if both are the same, we can apply the identity
1772 if (integer_zerop (partial
))
1774 else if (integer_zerop (t
))
1776 else if (same_bool_result_p (t
, partial
))
1785 /* Try to simplify the AND of two comparisons defined by
1786 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
1787 If this can be done without constructing an intermediate value,
1788 return the resulting tree; otherwise NULL_TREE is returned.
1789 This function is deliberately asymmetric as it recurses on SSA_DEFs
1790 in the first comparison but not the second. */
1793 and_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
1794 enum tree_code code2
, tree op2a
, tree op2b
)
1796 tree truth_type
= truth_type_for (TREE_TYPE (op1a
));
1798 /* First check for ((x CODE1 y) AND (x CODE2 y)). */
1799 if (operand_equal_p (op1a
, op2a
, 0)
1800 && operand_equal_p (op1b
, op2b
, 0))
1802 /* Result will be either NULL_TREE, or a combined comparison. */
1803 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
1804 TRUTH_ANDIF_EXPR
, code1
, code2
,
1805 truth_type
, op1a
, op1b
);
1810 /* Likewise the swapped case of the above. */
1811 if (operand_equal_p (op1a
, op2b
, 0)
1812 && operand_equal_p (op1b
, op2a
, 0))
1814 /* Result will be either NULL_TREE, or a combined comparison. */
1815 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
1816 TRUTH_ANDIF_EXPR
, code1
,
1817 swap_tree_comparison (code2
),
1818 truth_type
, op1a
, op1b
);
1823 /* If both comparisons are of the same value against constants, we might
1824 be able to merge them. */
1825 if (operand_equal_p (op1a
, op2a
, 0)
1826 && TREE_CODE (op1b
) == INTEGER_CST
1827 && TREE_CODE (op2b
) == INTEGER_CST
)
1829 int cmp
= tree_int_cst_compare (op1b
, op2b
);
1831 /* If we have (op1a == op1b), we should either be able to
1832 return that or FALSE, depending on whether the constant op1b
1833 also satisfies the other comparison against op2b. */
1834 if (code1
== EQ_EXPR
)
1840 case EQ_EXPR
: val
= (cmp
== 0); break;
1841 case NE_EXPR
: val
= (cmp
!= 0); break;
1842 case LT_EXPR
: val
= (cmp
< 0); break;
1843 case GT_EXPR
: val
= (cmp
> 0); break;
1844 case LE_EXPR
: val
= (cmp
<= 0); break;
1845 case GE_EXPR
: val
= (cmp
>= 0); break;
1846 default: done
= false;
1851 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
1853 return boolean_false_node
;
1856 /* Likewise if the second comparison is an == comparison. */
1857 else if (code2
== EQ_EXPR
)
1863 case EQ_EXPR
: val
= (cmp
== 0); break;
1864 case NE_EXPR
: val
= (cmp
!= 0); break;
1865 case LT_EXPR
: val
= (cmp
> 0); break;
1866 case GT_EXPR
: val
= (cmp
< 0); break;
1867 case LE_EXPR
: val
= (cmp
>= 0); break;
1868 case GE_EXPR
: val
= (cmp
<= 0); break;
1869 default: done
= false;
1874 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
1876 return boolean_false_node
;
1880 /* Same business with inequality tests. */
1881 else if (code1
== NE_EXPR
)
1886 case EQ_EXPR
: val
= (cmp
!= 0); break;
1887 case NE_EXPR
: val
= (cmp
== 0); break;
1888 case LT_EXPR
: val
= (cmp
>= 0); break;
1889 case GT_EXPR
: val
= (cmp
<= 0); break;
1890 case LE_EXPR
: val
= (cmp
> 0); break;
1891 case GE_EXPR
: val
= (cmp
< 0); break;
1896 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
1898 else if (code2
== NE_EXPR
)
1903 case EQ_EXPR
: val
= (cmp
== 0); break;
1904 case NE_EXPR
: val
= (cmp
!= 0); break;
1905 case LT_EXPR
: val
= (cmp
<= 0); break;
1906 case GT_EXPR
: val
= (cmp
>= 0); break;
1907 case LE_EXPR
: val
= (cmp
< 0); break;
1908 case GE_EXPR
: val
= (cmp
> 0); break;
1913 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
1916 /* Chose the more restrictive of two < or <= comparisons. */
1917 else if ((code1
== LT_EXPR
|| code1
== LE_EXPR
)
1918 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
1920 if ((cmp
< 0) || (cmp
== 0 && code1
== LT_EXPR
))
1921 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
1923 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
1926 /* Likewise chose the more restrictive of two > or >= comparisons. */
1927 else if ((code1
== GT_EXPR
|| code1
== GE_EXPR
)
1928 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
1930 if ((cmp
> 0) || (cmp
== 0 && code1
== GT_EXPR
))
1931 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
1933 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
1936 /* Check for singleton ranges. */
1938 && ((code1
== LE_EXPR
&& code2
== GE_EXPR
)
1939 || (code1
== GE_EXPR
&& code2
== LE_EXPR
)))
1940 return fold_build2 (EQ_EXPR
, boolean_type_node
, op1a
, op2b
);
1942 /* Check for disjoint ranges. */
1944 && (code1
== LT_EXPR
|| code1
== LE_EXPR
)
1945 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
1946 return boolean_false_node
;
1948 && (code1
== GT_EXPR
|| code1
== GE_EXPR
)
1949 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
1950 return boolean_false_node
;
1953 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
1954 NAME's definition is a truth value. See if there are any simplifications
1955 that can be done against the NAME's definition. */
1956 if (TREE_CODE (op1a
) == SSA_NAME
1957 && (code1
== NE_EXPR
|| code1
== EQ_EXPR
)
1958 && (integer_zerop (op1b
) || integer_onep (op1b
)))
1960 bool invert
= ((code1
== EQ_EXPR
&& integer_zerop (op1b
))
1961 || (code1
== NE_EXPR
&& integer_onep (op1b
)));
1962 gimple stmt
= SSA_NAME_DEF_STMT (op1a
);
1963 switch (gimple_code (stmt
))
1966 /* Try to simplify by copy-propagating the definition. */
1967 return and_var_with_comparison (op1a
, invert
, code2
, op2a
, op2b
);
1970 /* If every argument to the PHI produces the same result when
1971 ANDed with the second comparison, we win.
1972 Do not do this unless the type is bool since we need a bool
1973 result here anyway. */
1974 if (TREE_CODE (TREE_TYPE (op1a
)) == BOOLEAN_TYPE
)
1976 tree result
= NULL_TREE
;
1978 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
1980 tree arg
= gimple_phi_arg_def (stmt
, i
);
1982 /* If this PHI has itself as an argument, ignore it.
1983 If all the other args produce the same result,
1985 if (arg
== gimple_phi_result (stmt
))
1987 else if (TREE_CODE (arg
) == INTEGER_CST
)
1989 if (invert
? integer_nonzerop (arg
) : integer_zerop (arg
))
1992 result
= boolean_false_node
;
1993 else if (!integer_zerop (result
))
1997 result
= fold_build2 (code2
, boolean_type_node
,
1999 else if (!same_bool_comparison_p (result
,
2003 else if (TREE_CODE (arg
) == SSA_NAME
2004 && !SSA_NAME_IS_DEFAULT_DEF (arg
))
2007 gimple def_stmt
= SSA_NAME_DEF_STMT (arg
);
2008 /* In simple cases we can look through PHI nodes,
2009 but we have to be careful with loops.
2011 if (! dom_info_available_p (CDI_DOMINATORS
)
2012 || gimple_bb (def_stmt
) == gimple_bb (stmt
)
2013 || dominated_by_p (CDI_DOMINATORS
,
2014 gimple_bb (def_stmt
),
2017 temp
= and_var_with_comparison (arg
, invert
, code2
,
2023 else if (!same_bool_result_p (result
, temp
))
2039 /* Try to simplify the AND of two comparisons, specified by
2040 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
2041 If this can be simplified to a single expression (without requiring
2042 introducing more SSA variables to hold intermediate values),
2043 return the resulting tree. Otherwise return NULL_TREE.
2044 If the result expression is non-null, it has boolean type. */
2047 maybe_fold_and_comparisons (enum tree_code code1
, tree op1a
, tree op1b
,
2048 enum tree_code code2
, tree op2a
, tree op2b
)
2050 tree t
= and_comparisons_1 (code1
, op1a
, op1b
, code2
, op2a
, op2b
);
2054 return and_comparisons_1 (code2
, op2a
, op2b
, code1
, op1a
, op1b
);
2057 /* Helper function for or_comparisons_1: try to simplify the OR of the
2058 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
2059 If INVERT is true, invert the value of VAR before doing the OR.
2060 Return NULL_EXPR if we can't simplify this to a single expression. */
2063 or_var_with_comparison (tree var
, bool invert
,
2064 enum tree_code code2
, tree op2a
, tree op2b
)
2067 gimple stmt
= SSA_NAME_DEF_STMT (var
);
2069 /* We can only deal with variables whose definitions are assignments. */
2070 if (!is_gimple_assign (stmt
))
2073 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
2074 !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
2075 Then we only have to consider the simpler non-inverted cases. */
2077 t
= and_var_with_comparison_1 (stmt
,
2078 invert_tree_comparison (code2
, false),
2081 t
= or_var_with_comparison_1 (stmt
, code2
, op2a
, op2b
);
2082 return canonicalize_bool (t
, invert
);
2085 /* Try to simplify the OR of the ssa variable defined by the assignment
2086 STMT with the comparison specified by (OP2A CODE2 OP2B).
2087 Return NULL_EXPR if we can't simplify this to a single expression. */
2090 or_var_with_comparison_1 (gimple stmt
,
2091 enum tree_code code2
, tree op2a
, tree op2b
)
2093 tree var
= gimple_assign_lhs (stmt
);
2094 tree true_test_var
= NULL_TREE
;
2095 tree false_test_var
= NULL_TREE
;
2096 enum tree_code innercode
= gimple_assign_rhs_code (stmt
);
2098 /* Check for identities like (var OR (var != 0)) => true . */
2099 if (TREE_CODE (op2a
) == SSA_NAME
2100 && TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
)
2102 if ((code2
== NE_EXPR
&& integer_zerop (op2b
))
2103 || (code2
== EQ_EXPR
&& integer_nonzerop (op2b
)))
2105 true_test_var
= op2a
;
2106 if (var
== true_test_var
)
2109 else if ((code2
== EQ_EXPR
&& integer_zerop (op2b
))
2110 || (code2
== NE_EXPR
&& integer_nonzerop (op2b
)))
2112 false_test_var
= op2a
;
2113 if (var
== false_test_var
)
2114 return boolean_true_node
;
2118 /* If the definition is a comparison, recurse on it. */
2119 if (TREE_CODE_CLASS (innercode
) == tcc_comparison
)
2121 tree t
= or_comparisons_1 (innercode
,
2122 gimple_assign_rhs1 (stmt
),
2123 gimple_assign_rhs2 (stmt
),
2131 /* If the definition is an AND or OR expression, we may be able to
2132 simplify by reassociating. */
2133 if (TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
2134 && (innercode
== BIT_AND_EXPR
|| innercode
== BIT_IOR_EXPR
))
2136 tree inner1
= gimple_assign_rhs1 (stmt
);
2137 tree inner2
= gimple_assign_rhs2 (stmt
);
2140 tree partial
= NULL_TREE
;
2141 bool is_or
= (innercode
== BIT_IOR_EXPR
);
2143 /* Check for boolean identities that don't require recursive examination
2145 inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
2146 inner1 OR (inner1 AND inner2) => inner1
2147 !inner1 OR (inner1 OR inner2) => true
2148 !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
2150 if (inner1
== true_test_var
)
2151 return (is_or
? var
: inner1
);
2152 else if (inner2
== true_test_var
)
2153 return (is_or
? var
: inner2
);
2154 else if (inner1
== false_test_var
)
2157 : or_var_with_comparison (inner2
, false, code2
, op2a
, op2b
));
2158 else if (inner2
== false_test_var
)
2161 : or_var_with_comparison (inner1
, false, code2
, op2a
, op2b
));
2163 /* Next, redistribute/reassociate the OR across the inner tests.
2164 Compute the first partial result, (inner1 OR (op2a code op2b)) */
2165 if (TREE_CODE (inner1
) == SSA_NAME
2166 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner1
))
2167 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
2168 && (t
= maybe_fold_or_comparisons (gimple_assign_rhs_code (s
),
2169 gimple_assign_rhs1 (s
),
2170 gimple_assign_rhs2 (s
),
2171 code2
, op2a
, op2b
)))
2173 /* Handle the OR case, where we are reassociating:
2174 (inner1 OR inner2) OR (op2a code2 op2b)
2176 If the partial result t is a constant, we win. Otherwise
2177 continue on to try reassociating with the other inner test. */
2180 if (integer_onep (t
))
2181 return boolean_true_node
;
2182 else if (integer_zerop (t
))
2186 /* Handle the AND case, where we are redistributing:
2187 (inner1 AND inner2) OR (op2a code2 op2b)
2188 => (t AND (inner2 OR (op2a code op2b))) */
2189 else if (integer_zerop (t
))
2190 return boolean_false_node
;
2192 /* Save partial result for later. */
2196 /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
2197 if (TREE_CODE (inner2
) == SSA_NAME
2198 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner2
))
2199 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
2200 && (t
= maybe_fold_or_comparisons (gimple_assign_rhs_code (s
),
2201 gimple_assign_rhs1 (s
),
2202 gimple_assign_rhs2 (s
),
2203 code2
, op2a
, op2b
)))
2205 /* Handle the OR case, where we are reassociating:
2206 (inner1 OR inner2) OR (op2a code2 op2b)
2208 => (t OR partial) */
2211 if (integer_zerop (t
))
2213 else if (integer_onep (t
))
2214 return boolean_true_node
;
2215 /* If both are the same, we can apply the identity
2217 else if (partial
&& same_bool_result_p (t
, partial
))
2221 /* Handle the AND case, where we are redistributing:
2222 (inner1 AND inner2) OR (op2a code2 op2b)
2223 => (t AND (inner1 OR (op2a code2 op2b)))
2224 => (t AND partial) */
2227 if (integer_zerop (t
))
2228 return boolean_false_node
;
2231 /* We already got a simplification for the other
2232 operand to the redistributed AND expression. The
2233 interesting case is when at least one is true.
2234 Or, if both are the same, we can apply the identity
2236 if (integer_onep (partial
))
2238 else if (integer_onep (t
))
2240 else if (same_bool_result_p (t
, partial
))
2249 /* Try to simplify the OR of two comparisons defined by
2250 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
2251 If this can be done without constructing an intermediate value,
2252 return the resulting tree; otherwise NULL_TREE is returned.
2253 This function is deliberately asymmetric as it recurses on SSA_DEFs
2254 in the first comparison but not the second. */
2257 or_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
2258 enum tree_code code2
, tree op2a
, tree op2b
)
2260 tree truth_type
= truth_type_for (TREE_TYPE (op1a
));
2262 /* First check for ((x CODE1 y) OR (x CODE2 y)). */
2263 if (operand_equal_p (op1a
, op2a
, 0)
2264 && operand_equal_p (op1b
, op2b
, 0))
2266 /* Result will be either NULL_TREE, or a combined comparison. */
2267 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
2268 TRUTH_ORIF_EXPR
, code1
, code2
,
2269 truth_type
, op1a
, op1b
);
2274 /* Likewise the swapped case of the above. */
2275 if (operand_equal_p (op1a
, op2b
, 0)
2276 && operand_equal_p (op1b
, op2a
, 0))
2278 /* Result will be either NULL_TREE, or a combined comparison. */
2279 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
2280 TRUTH_ORIF_EXPR
, code1
,
2281 swap_tree_comparison (code2
),
2282 truth_type
, op1a
, op1b
);
2287 /* If both comparisons are of the same value against constants, we might
2288 be able to merge them. */
2289 if (operand_equal_p (op1a
, op2a
, 0)
2290 && TREE_CODE (op1b
) == INTEGER_CST
2291 && TREE_CODE (op2b
) == INTEGER_CST
)
2293 int cmp
= tree_int_cst_compare (op1b
, op2b
);
2295 /* If we have (op1a != op1b), we should either be able to
2296 return that or TRUE, depending on whether the constant op1b
2297 also satisfies the other comparison against op2b. */
2298 if (code1
== NE_EXPR
)
2304 case EQ_EXPR
: val
= (cmp
== 0); break;
2305 case NE_EXPR
: val
= (cmp
!= 0); break;
2306 case LT_EXPR
: val
= (cmp
< 0); break;
2307 case GT_EXPR
: val
= (cmp
> 0); break;
2308 case LE_EXPR
: val
= (cmp
<= 0); break;
2309 case GE_EXPR
: val
= (cmp
>= 0); break;
2310 default: done
= false;
2315 return boolean_true_node
;
2317 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
2320 /* Likewise if the second comparison is a != comparison. */
2321 else if (code2
== NE_EXPR
)
2327 case EQ_EXPR
: val
= (cmp
== 0); break;
2328 case NE_EXPR
: val
= (cmp
!= 0); break;
2329 case LT_EXPR
: val
= (cmp
> 0); break;
2330 case GT_EXPR
: val
= (cmp
< 0); break;
2331 case LE_EXPR
: val
= (cmp
>= 0); break;
2332 case GE_EXPR
: val
= (cmp
<= 0); break;
2333 default: done
= false;
2338 return boolean_true_node
;
2340 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
2344 /* See if an equality test is redundant with the other comparison. */
2345 else if (code1
== EQ_EXPR
)
2350 case EQ_EXPR
: val
= (cmp
== 0); break;
2351 case NE_EXPR
: val
= (cmp
!= 0); break;
2352 case LT_EXPR
: val
= (cmp
< 0); break;
2353 case GT_EXPR
: val
= (cmp
> 0); break;
2354 case LE_EXPR
: val
= (cmp
<= 0); break;
2355 case GE_EXPR
: val
= (cmp
>= 0); break;
2360 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
2362 else if (code2
== EQ_EXPR
)
2367 case EQ_EXPR
: val
= (cmp
== 0); break;
2368 case NE_EXPR
: val
= (cmp
!= 0); break;
2369 case LT_EXPR
: val
= (cmp
> 0); break;
2370 case GT_EXPR
: val
= (cmp
< 0); break;
2371 case LE_EXPR
: val
= (cmp
>= 0); break;
2372 case GE_EXPR
: val
= (cmp
<= 0); break;
2377 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
2380 /* Chose the less restrictive of two < or <= comparisons. */
2381 else if ((code1
== LT_EXPR
|| code1
== LE_EXPR
)
2382 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
2384 if ((cmp
< 0) || (cmp
== 0 && code1
== LT_EXPR
))
2385 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
2387 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
2390 /* Likewise chose the less restrictive of two > or >= comparisons. */
2391 else if ((code1
== GT_EXPR
|| code1
== GE_EXPR
)
2392 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
2394 if ((cmp
> 0) || (cmp
== 0 && code1
== GT_EXPR
))
2395 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
2397 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
2400 /* Check for singleton ranges. */
2402 && ((code1
== LT_EXPR
&& code2
== GT_EXPR
)
2403 || (code1
== GT_EXPR
&& code2
== LT_EXPR
)))
2404 return fold_build2 (NE_EXPR
, boolean_type_node
, op1a
, op2b
);
2406 /* Check for less/greater pairs that don't restrict the range at all. */
2408 && (code1
== LT_EXPR
|| code1
== LE_EXPR
)
2409 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
2410 return boolean_true_node
;
2412 && (code1
== GT_EXPR
|| code1
== GE_EXPR
)
2413 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
2414 return boolean_true_node
;
2417 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
2418 NAME's definition is a truth value. See if there are any simplifications
2419 that can be done against the NAME's definition. */
2420 if (TREE_CODE (op1a
) == SSA_NAME
2421 && (code1
== NE_EXPR
|| code1
== EQ_EXPR
)
2422 && (integer_zerop (op1b
) || integer_onep (op1b
)))
2424 bool invert
= ((code1
== EQ_EXPR
&& integer_zerop (op1b
))
2425 || (code1
== NE_EXPR
&& integer_onep (op1b
)));
2426 gimple stmt
= SSA_NAME_DEF_STMT (op1a
);
2427 switch (gimple_code (stmt
))
2430 /* Try to simplify by copy-propagating the definition. */
2431 return or_var_with_comparison (op1a
, invert
, code2
, op2a
, op2b
);
2434 /* If every argument to the PHI produces the same result when
2435 ORed with the second comparison, we win.
2436 Do not do this unless the type is bool since we need a bool
2437 result here anyway. */
2438 if (TREE_CODE (TREE_TYPE (op1a
)) == BOOLEAN_TYPE
)
2440 tree result
= NULL_TREE
;
2442 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
2444 tree arg
= gimple_phi_arg_def (stmt
, i
);
2446 /* If this PHI has itself as an argument, ignore it.
2447 If all the other args produce the same result,
2449 if (arg
== gimple_phi_result (stmt
))
2451 else if (TREE_CODE (arg
) == INTEGER_CST
)
2453 if (invert
? integer_zerop (arg
) : integer_nonzerop (arg
))
2456 result
= boolean_true_node
;
2457 else if (!integer_onep (result
))
2461 result
= fold_build2 (code2
, boolean_type_node
,
2463 else if (!same_bool_comparison_p (result
,
2467 else if (TREE_CODE (arg
) == SSA_NAME
2468 && !SSA_NAME_IS_DEFAULT_DEF (arg
))
2471 gimple def_stmt
= SSA_NAME_DEF_STMT (arg
);
2472 /* In simple cases we can look through PHI nodes,
2473 but we have to be careful with loops.
2475 if (! dom_info_available_p (CDI_DOMINATORS
)
2476 || gimple_bb (def_stmt
) == gimple_bb (stmt
)
2477 || dominated_by_p (CDI_DOMINATORS
,
2478 gimple_bb (def_stmt
),
2481 temp
= or_var_with_comparison (arg
, invert
, code2
,
2487 else if (!same_bool_result_p (result
, temp
))
2503 /* Try to simplify the OR of two comparisons, specified by
2504 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
2505 If this can be simplified to a single expression (without requiring
2506 introducing more SSA variables to hold intermediate values),
2507 return the resulting tree. Otherwise return NULL_TREE.
2508 If the result expression is non-null, it has boolean type. */
2511 maybe_fold_or_comparisons (enum tree_code code1
, tree op1a
, tree op1b
,
2512 enum tree_code code2
, tree op2a
, tree op2b
)
2514 tree t
= or_comparisons_1 (code1
, op1a
, op1b
, code2
, op2a
, op2b
);
2518 return or_comparisons_1 (code2
, op2a
, op2b
, code1
, op1a
, op1b
);
2522 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
2524 Either NULL_TREE, a simplified but non-constant or a constant
2527 ??? This should go into a gimple-fold-inline.h file to be eventually
2528 privatized with the single valueize function used in the various TUs
2529 to avoid the indirect function call overhead. */
2532 gimple_fold_stmt_to_constant_1 (gimple stmt
, tree (*valueize
) (tree
))
2534 location_t loc
= gimple_location (stmt
);
2535 switch (gimple_code (stmt
))
2539 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
2541 switch (get_gimple_rhs_class (subcode
))
2543 case GIMPLE_SINGLE_RHS
:
2545 tree rhs
= gimple_assign_rhs1 (stmt
);
2546 enum tree_code_class kind
= TREE_CODE_CLASS (subcode
);
2548 if (TREE_CODE (rhs
) == SSA_NAME
)
2550 /* If the RHS is an SSA_NAME, return its known constant value,
2552 return (*valueize
) (rhs
);
2554 /* Handle propagating invariant addresses into address
2556 else if (TREE_CODE (rhs
) == ADDR_EXPR
2557 && !is_gimple_min_invariant (rhs
))
2559 HOST_WIDE_INT offset
= 0;
2561 base
= get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs
, 0),
2565 && (CONSTANT_CLASS_P (base
)
2566 || decl_address_invariant_p (base
)))
2567 return build_invariant_address (TREE_TYPE (rhs
),
2570 else if (TREE_CODE (rhs
) == CONSTRUCTOR
2571 && TREE_CODE (TREE_TYPE (rhs
)) == VECTOR_TYPE
2572 && (CONSTRUCTOR_NELTS (rhs
)
2573 == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs
))))
2578 vec
= XALLOCAVEC (tree
,
2579 TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs
)));
2580 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs
), i
, val
)
2582 val
= (*valueize
) (val
);
2583 if (TREE_CODE (val
) == INTEGER_CST
2584 || TREE_CODE (val
) == REAL_CST
2585 || TREE_CODE (val
) == FIXED_CST
)
2591 return build_vector (TREE_TYPE (rhs
), vec
);
2593 if (subcode
== OBJ_TYPE_REF
)
2595 tree val
= (*valueize
) (OBJ_TYPE_REF_EXPR (rhs
));
2596 /* If callee is constant, we can fold away the wrapper. */
2597 if (is_gimple_min_invariant (val
))
2601 if (kind
== tcc_reference
)
2603 if ((TREE_CODE (rhs
) == VIEW_CONVERT_EXPR
2604 || TREE_CODE (rhs
) == REALPART_EXPR
2605 || TREE_CODE (rhs
) == IMAGPART_EXPR
)
2606 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
2608 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
2609 return fold_unary_loc (EXPR_LOCATION (rhs
),
2611 TREE_TYPE (rhs
), val
);
2613 else if (TREE_CODE (rhs
) == BIT_FIELD_REF
2614 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
2616 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
2617 return fold_ternary_loc (EXPR_LOCATION (rhs
),
2619 TREE_TYPE (rhs
), val
,
2620 TREE_OPERAND (rhs
, 1),
2621 TREE_OPERAND (rhs
, 2));
2623 else if (TREE_CODE (rhs
) == MEM_REF
2624 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
2626 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
2627 if (TREE_CODE (val
) == ADDR_EXPR
2628 && is_gimple_min_invariant (val
))
2630 tree tem
= fold_build2 (MEM_REF
, TREE_TYPE (rhs
),
2632 TREE_OPERAND (rhs
, 1));
2637 return fold_const_aggregate_ref_1 (rhs
, valueize
);
2639 else if (kind
== tcc_declaration
)
2640 return get_symbol_constant_value (rhs
);
2644 case GIMPLE_UNARY_RHS
:
2646 /* Handle unary operators that can appear in GIMPLE form.
2647 Note that we know the single operand must be a constant,
2648 so this should almost always return a simplified RHS. */
2649 tree op0
= (*valueize
) (gimple_assign_rhs1 (stmt
));
2652 fold_unary_ignore_overflow_loc (loc
, subcode
,
2653 gimple_expr_type (stmt
), op0
);
2656 case GIMPLE_BINARY_RHS
:
2658 /* Handle binary operators that can appear in GIMPLE form. */
2659 tree op0
= (*valueize
) (gimple_assign_rhs1 (stmt
));
2660 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
2662 /* Translate &x + CST into an invariant form suitable for
2663 further propagation. */
2664 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
2665 && TREE_CODE (op0
) == ADDR_EXPR
2666 && TREE_CODE (op1
) == INTEGER_CST
)
2668 tree off
= fold_convert (ptr_type_node
, op1
);
2669 return build_fold_addr_expr_loc
2671 fold_build2 (MEM_REF
,
2672 TREE_TYPE (TREE_TYPE (op0
)),
2673 unshare_expr (op0
), off
));
2676 return fold_binary_loc (loc
, subcode
,
2677 gimple_expr_type (stmt
), op0
, op1
);
2680 case GIMPLE_TERNARY_RHS
:
2682 /* Handle ternary operators that can appear in GIMPLE form. */
2683 tree op0
= (*valueize
) (gimple_assign_rhs1 (stmt
));
2684 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
2685 tree op2
= (*valueize
) (gimple_assign_rhs3 (stmt
));
2687 /* Fold embedded expressions in ternary codes. */
2688 if ((subcode
== COND_EXPR
2689 || subcode
== VEC_COND_EXPR
)
2690 && COMPARISON_CLASS_P (op0
))
2692 tree op00
= (*valueize
) (TREE_OPERAND (op0
, 0));
2693 tree op01
= (*valueize
) (TREE_OPERAND (op0
, 1));
2694 tree tem
= fold_binary_loc (loc
, TREE_CODE (op0
),
2695 TREE_TYPE (op0
), op00
, op01
);
2700 return fold_ternary_loc (loc
, subcode
,
2701 gimple_expr_type (stmt
), op0
, op1
, op2
);
2713 if (gimple_call_internal_p (stmt
))
2715 enum tree_code subcode
= ERROR_MARK
;
2716 switch (gimple_call_internal_fn (stmt
))
2718 case IFN_UBSAN_CHECK_ADD
:
2719 subcode
= PLUS_EXPR
;
2721 case IFN_UBSAN_CHECK_SUB
:
2722 subcode
= MINUS_EXPR
;
2724 case IFN_UBSAN_CHECK_MUL
:
2725 subcode
= MULT_EXPR
;
2730 tree arg0
= gimple_call_arg (stmt
, 0);
2731 tree arg1
= gimple_call_arg (stmt
, 1);
2732 tree op0
= (*valueize
) (arg0
);
2733 tree op1
= (*valueize
) (arg1
);
2735 if (TREE_CODE (op0
) != INTEGER_CST
2736 || TREE_CODE (op1
) != INTEGER_CST
)
2741 /* x * 0 = 0 * x = 0 without overflow. */
2742 if (integer_zerop (op0
) || integer_zerop (op1
))
2743 return build_zero_cst (TREE_TYPE (arg0
));
2746 /* y - y = 0 without overflow. */
2747 if (operand_equal_p (op0
, op1
, 0))
2748 return build_zero_cst (TREE_TYPE (arg0
));
2755 = fold_binary_loc (loc
, subcode
, TREE_TYPE (arg0
), op0
, op1
);
2757 && TREE_CODE (res
) == INTEGER_CST
2758 && !TREE_OVERFLOW (res
))
2763 fn
= (*valueize
) (gimple_call_fn (stmt
));
2764 if (TREE_CODE (fn
) == ADDR_EXPR
2765 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
2766 && DECL_BUILT_IN (TREE_OPERAND (fn
, 0))
2767 && gimple_builtin_call_types_compatible_p (stmt
,
2768 TREE_OPERAND (fn
, 0)))
2770 tree
*args
= XALLOCAVEC (tree
, gimple_call_num_args (stmt
));
2773 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
2774 args
[i
] = (*valueize
) (gimple_call_arg (stmt
, i
));
2775 call
= build_call_array_loc (loc
,
2776 gimple_call_return_type (stmt
),
2777 fn
, gimple_call_num_args (stmt
), args
);
2778 retval
= fold_call_expr (EXPR_LOCATION (call
), call
, false);
2781 /* fold_call_expr wraps the result inside a NOP_EXPR. */
2782 STRIP_NOPS (retval
);
2783 retval
= fold_convert (gimple_call_return_type (stmt
), retval
);
2795 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
2796 Returns NULL_TREE if folding to a constant is not possible, otherwise
2797 returns a constant according to is_gimple_min_invariant. */
2800 gimple_fold_stmt_to_constant (gimple stmt
, tree (*valueize
) (tree
))
2802 tree res
= gimple_fold_stmt_to_constant_1 (stmt
, valueize
);
2803 if (res
&& is_gimple_min_invariant (res
))
2809 /* The following set of functions are supposed to fold references using
2810 their constant initializers. */
2812 static tree
fold_ctor_reference (tree type
, tree ctor
,
2813 unsigned HOST_WIDE_INT offset
,
2814 unsigned HOST_WIDE_INT size
, tree
);
2816 /* See if we can find constructor defining value of BASE.
2817 When we know the consructor with constant offset (such as
2818 base is array[40] and we do know constructor of array), then
2819 BIT_OFFSET is adjusted accordingly.
2821 As a special case, return error_mark_node when constructor
2822 is not explicitly available, but it is known to be zero
2823 such as 'static const int a;'. */
2825 get_base_constructor (tree base
, HOST_WIDE_INT
*bit_offset
,
2826 tree (*valueize
)(tree
))
2828 HOST_WIDE_INT bit_offset2
, size
, max_size
;
2829 if (TREE_CODE (base
) == MEM_REF
)
2831 if (!integer_zerop (TREE_OPERAND (base
, 1)))
2833 if (!tree_fits_shwi_p (TREE_OPERAND (base
, 1)))
2835 *bit_offset
+= (mem_ref_offset (base
).to_short_addr ()
2840 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
2841 base
= valueize (TREE_OPERAND (base
, 0));
2842 if (!base
|| TREE_CODE (base
) != ADDR_EXPR
)
2844 base
= TREE_OPERAND (base
, 0);
2847 /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
2848 DECL_INITIAL. If BASE is a nested reference into another
2849 ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
2850 the inner reference. */
2851 switch (TREE_CODE (base
))
2856 tree init
= ctor_for_folding (base
);
2858 /* Our semantic is exact opposite of ctor_for_folding;
2859 NULL means unknown, while error_mark_node is 0. */
2860 if (init
== error_mark_node
)
2863 return error_mark_node
;
2869 base
= get_ref_base_and_extent (base
, &bit_offset2
, &size
, &max_size
);
2870 if (max_size
== -1 || size
!= max_size
)
2872 *bit_offset
+= bit_offset2
;
2873 return get_base_constructor (base
, bit_offset
, valueize
);
2884 /* CTOR is STRING_CST. Fold reference of type TYPE and size SIZE
2885 to the memory at bit OFFSET.
2887 We do only simple job of folding byte accesses. */
2890 fold_string_cst_ctor_reference (tree type
, tree ctor
,
2891 unsigned HOST_WIDE_INT offset
,
2892 unsigned HOST_WIDE_INT size
)
2894 if (INTEGRAL_TYPE_P (type
)
2895 && (TYPE_MODE (type
)
2896 == TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor
))))
2897 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor
))))
2899 && GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor
)))) == 1
2900 && size
== BITS_PER_UNIT
2901 && !(offset
% BITS_PER_UNIT
))
2903 offset
/= BITS_PER_UNIT
;
2904 if (offset
< (unsigned HOST_WIDE_INT
) TREE_STRING_LENGTH (ctor
))
2905 return build_int_cst_type (type
, (TREE_STRING_POINTER (ctor
)
2908 const char a[20]="hello";
2911 might lead to offset greater than string length. In this case we
2912 know value is either initialized to 0 or out of bounds. Return 0
2914 return build_zero_cst (type
);
2919 /* CTOR is CONSTRUCTOR of an array type. Fold reference of type TYPE and size
2920 SIZE to the memory at bit OFFSET. */
2923 fold_array_ctor_reference (tree type
, tree ctor
,
2924 unsigned HOST_WIDE_INT offset
,
2925 unsigned HOST_WIDE_INT size
,
2928 unsigned HOST_WIDE_INT cnt
;
2930 offset_int low_bound
;
2931 offset_int elt_size
;
2932 offset_int index
, max_index
;
2933 offset_int access_index
;
2934 tree domain_type
= NULL_TREE
, index_type
= NULL_TREE
;
2935 HOST_WIDE_INT inner_offset
;
2937 /* Compute low bound and elt size. */
2938 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
)
2939 domain_type
= TYPE_DOMAIN (TREE_TYPE (ctor
));
2940 if (domain_type
&& TYPE_MIN_VALUE (domain_type
))
2942 /* Static constructors for variably sized objects makes no sense. */
2943 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type
)) == INTEGER_CST
);
2944 index_type
= TREE_TYPE (TYPE_MIN_VALUE (domain_type
));
2945 low_bound
= wi::to_offset (TYPE_MIN_VALUE (domain_type
));
2949 /* Static constructors for variably sized objects makes no sense. */
2950 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor
))))
2952 elt_size
= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor
))));
2954 /* We can handle only constantly sized accesses that are known to not
2955 be larger than size of array element. */
2956 if (!TYPE_SIZE_UNIT (type
)
2957 || TREE_CODE (TYPE_SIZE_UNIT (type
)) != INTEGER_CST
2958 || wi::lts_p (elt_size
, wi::to_offset (TYPE_SIZE_UNIT (type
)))
2962 /* Compute the array index we look for. */
2963 access_index
= wi::udiv_trunc (offset_int (offset
/ BITS_PER_UNIT
),
2965 access_index
+= low_bound
;
2967 access_index
= wi::ext (access_index
, TYPE_PRECISION (index_type
),
2968 TYPE_SIGN (index_type
));
2970 /* And offset within the access. */
2971 inner_offset
= offset
% (elt_size
.to_uhwi () * BITS_PER_UNIT
);
2973 /* See if the array field is large enough to span whole access. We do not
2974 care to fold accesses spanning multiple array indexes. */
2975 if (inner_offset
+ size
> elt_size
.to_uhwi () * BITS_PER_UNIT
)
2978 index
= low_bound
- 1;
2980 index
= wi::ext (index
, TYPE_PRECISION (index_type
),
2981 TYPE_SIGN (index_type
));
2983 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
, cval
)
2985 /* Array constructor might explicitely set index, or specify range
2986 or leave index NULL meaning that it is next index after previous
2990 if (TREE_CODE (cfield
) == INTEGER_CST
)
2991 max_index
= index
= wi::to_offset (cfield
);
2994 gcc_assert (TREE_CODE (cfield
) == RANGE_EXPR
);
2995 index
= wi::to_offset (TREE_OPERAND (cfield
, 0));
2996 max_index
= wi::to_offset (TREE_OPERAND (cfield
, 1));
3003 index
= wi::ext (index
, TYPE_PRECISION (index_type
),
3004 TYPE_SIGN (index_type
));
3008 /* Do we have match? */
3009 if (wi::cmpu (access_index
, index
) >= 0
3010 && wi::cmpu (access_index
, max_index
) <= 0)
3011 return fold_ctor_reference (type
, cval
, inner_offset
, size
,
3014 /* When memory is not explicitely mentioned in constructor,
3015 it is 0 (or out of range). */
3016 return build_zero_cst (type
);
3019 /* CTOR is CONSTRUCTOR of an aggregate or vector.
3020 Fold reference of type TYPE and size SIZE to the memory at bit OFFSET. */
3023 fold_nonarray_ctor_reference (tree type
, tree ctor
,
3024 unsigned HOST_WIDE_INT offset
,
3025 unsigned HOST_WIDE_INT size
,
3028 unsigned HOST_WIDE_INT cnt
;
3031 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
,
3034 tree byte_offset
= DECL_FIELD_OFFSET (cfield
);
3035 tree field_offset
= DECL_FIELD_BIT_OFFSET (cfield
);
3036 tree field_size
= DECL_SIZE (cfield
);
3037 offset_int bitoffset
;
3038 offset_int bitoffset_end
, access_end
;
3040 /* Variable sized objects in static constructors makes no sense,
3041 but field_size can be NULL for flexible array members. */
3042 gcc_assert (TREE_CODE (field_offset
) == INTEGER_CST
3043 && TREE_CODE (byte_offset
) == INTEGER_CST
3044 && (field_size
!= NULL_TREE
3045 ? TREE_CODE (field_size
) == INTEGER_CST
3046 : TREE_CODE (TREE_TYPE (cfield
)) == ARRAY_TYPE
));
3048 /* Compute bit offset of the field. */
3049 bitoffset
= (wi::to_offset (field_offset
)
3050 + wi::lshift (wi::to_offset (byte_offset
),
3051 LOG2_BITS_PER_UNIT
));
3052 /* Compute bit offset where the field ends. */
3053 if (field_size
!= NULL_TREE
)
3054 bitoffset_end
= bitoffset
+ wi::to_offset (field_size
);
3058 access_end
= offset_int (offset
) + size
;
3060 /* Is there any overlap between [OFFSET, OFFSET+SIZE) and
3061 [BITOFFSET, BITOFFSET_END)? */
3062 if (wi::cmps (access_end
, bitoffset
) > 0
3063 && (field_size
== NULL_TREE
3064 || wi::lts_p (offset
, bitoffset_end
)))
3066 offset_int inner_offset
= offset_int (offset
) - bitoffset
;
3067 /* We do have overlap. Now see if field is large enough to
3068 cover the access. Give up for accesses spanning multiple
3070 if (wi::cmps (access_end
, bitoffset_end
) > 0)
3072 if (wi::lts_p (offset
, bitoffset
))
3074 return fold_ctor_reference (type
, cval
,
3075 inner_offset
.to_uhwi (), size
,
3079 /* When memory is not explicitely mentioned in constructor, it is 0. */
3080 return build_zero_cst (type
);
3083 /* CTOR is value initializing memory, fold reference of type TYPE and size SIZE
3084 to the memory at bit OFFSET. */
3087 fold_ctor_reference (tree type
, tree ctor
, unsigned HOST_WIDE_INT offset
,
3088 unsigned HOST_WIDE_INT size
, tree from_decl
)
3092 /* We found the field with exact match. */
3093 if (useless_type_conversion_p (type
, TREE_TYPE (ctor
))
3095 return canonicalize_constructor_val (unshare_expr (ctor
), from_decl
);
3097 /* We are at the end of walk, see if we can view convert the
3099 if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor
)) && !offset
3100 /* VIEW_CONVERT_EXPR is defined only for matching sizes. */
3101 && operand_equal_p (TYPE_SIZE (type
),
3102 TYPE_SIZE (TREE_TYPE (ctor
)), 0))
3104 ret
= canonicalize_constructor_val (unshare_expr (ctor
), from_decl
);
3105 ret
= fold_unary (VIEW_CONVERT_EXPR
, type
, ret
);
3110 if (TREE_CODE (ctor
) == STRING_CST
)
3111 return fold_string_cst_ctor_reference (type
, ctor
, offset
, size
);
3112 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
3115 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
3116 || TREE_CODE (TREE_TYPE (ctor
)) == VECTOR_TYPE
)
3117 return fold_array_ctor_reference (type
, ctor
, offset
, size
,
3120 return fold_nonarray_ctor_reference (type
, ctor
, offset
, size
,
3127 /* Return the tree representing the element referenced by T if T is an
3128 ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
3129 names using VALUEIZE. Return NULL_TREE otherwise. */
3132 fold_const_aggregate_ref_1 (tree t
, tree (*valueize
) (tree
))
3134 tree ctor
, idx
, base
;
3135 HOST_WIDE_INT offset
, size
, max_size
;
3138 if (TREE_THIS_VOLATILE (t
))
3141 if (TREE_CODE_CLASS (TREE_CODE (t
)) == tcc_declaration
)
3142 return get_symbol_constant_value (t
);
3144 tem
= fold_read_from_constant_string (t
);
3148 switch (TREE_CODE (t
))
3151 case ARRAY_RANGE_REF
:
3152 /* Constant indexes are handled well by get_base_constructor.
3153 Only special case variable offsets.
3154 FIXME: This code can't handle nested references with variable indexes
3155 (they will be handled only by iteration of ccp). Perhaps we can bring
3156 get_ref_base_and_extent here and make it use a valueize callback. */
3157 if (TREE_CODE (TREE_OPERAND (t
, 1)) == SSA_NAME
3159 && (idx
= (*valueize
) (TREE_OPERAND (t
, 1)))
3160 && TREE_CODE (idx
) == INTEGER_CST
)
3162 tree low_bound
, unit_size
;
3164 /* If the resulting bit-offset is constant, track it. */
3165 if ((low_bound
= array_ref_low_bound (t
),
3166 TREE_CODE (low_bound
) == INTEGER_CST
)
3167 && (unit_size
= array_ref_element_size (t
),
3168 tree_fits_uhwi_p (unit_size
)))
3171 = wi::sext (wi::to_offset (idx
) - wi::to_offset (low_bound
),
3172 TYPE_PRECISION (TREE_TYPE (idx
)));
3174 if (wi::fits_shwi_p (woffset
))
3176 offset
= woffset
.to_shwi ();
3177 /* TODO: This code seems wrong, multiply then check
3178 to see if it fits. */
3179 offset
*= tree_to_uhwi (unit_size
);
3180 offset
*= BITS_PER_UNIT
;
3182 base
= TREE_OPERAND (t
, 0);
3183 ctor
= get_base_constructor (base
, &offset
, valueize
);
3184 /* Empty constructor. Always fold to 0. */
3185 if (ctor
== error_mark_node
)
3186 return build_zero_cst (TREE_TYPE (t
));
3187 /* Out of bound array access. Value is undefined,
3191 /* We can not determine ctor. */
3194 return fold_ctor_reference (TREE_TYPE (t
), ctor
, offset
,
3195 tree_to_uhwi (unit_size
)
3205 case TARGET_MEM_REF
:
3207 base
= get_ref_base_and_extent (t
, &offset
, &size
, &max_size
);
3208 ctor
= get_base_constructor (base
, &offset
, valueize
);
3210 /* Empty constructor. Always fold to 0. */
3211 if (ctor
== error_mark_node
)
3212 return build_zero_cst (TREE_TYPE (t
));
3213 /* We do not know precise address. */
3214 if (max_size
== -1 || max_size
!= size
)
3216 /* We can not determine ctor. */
3220 /* Out of bound array access. Value is undefined, but don't fold. */
3224 return fold_ctor_reference (TREE_TYPE (t
), ctor
, offset
, size
,
3230 tree c
= fold_const_aggregate_ref_1 (TREE_OPERAND (t
, 0), valueize
);
3231 if (c
&& TREE_CODE (c
) == COMPLEX_CST
)
3232 return fold_build1_loc (EXPR_LOCATION (t
),
3233 TREE_CODE (t
), TREE_TYPE (t
), c
);
3245 fold_const_aggregate_ref (tree t
)
3247 return fold_const_aggregate_ref_1 (t
, NULL
);
3250 /* Lookup virtual method with index TOKEN in a virtual table V
3252 Set CAN_REFER if non-NULL to false if method
3253 is not referable or if the virtual table is ill-formed (such as rewriten
3254 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
3257 gimple_get_virt_method_for_vtable (HOST_WIDE_INT token
,
3259 unsigned HOST_WIDE_INT offset
,
3262 tree vtable
= v
, init
, fn
;
3263 unsigned HOST_WIDE_INT size
;
3264 unsigned HOST_WIDE_INT elt_size
, access_index
;
3270 /* First of all double check we have virtual table. */
3271 if (TREE_CODE (v
) != VAR_DECL
3272 || !DECL_VIRTUAL_P (v
))
3274 gcc_assert (in_lto_p
);
3275 /* Pass down that we lost track of the target. */
3281 init
= ctor_for_folding (v
);
3283 /* The virtual tables should always be born with constructors
3284 and we always should assume that they are avaialble for
3285 folding. At the moment we do not stream them in all cases,
3286 but it should never happen that ctor seem unreachable. */
3288 if (init
== error_mark_node
)
3290 gcc_assert (in_lto_p
);
3291 /* Pass down that we lost track of the target. */
3296 gcc_checking_assert (TREE_CODE (TREE_TYPE (v
)) == ARRAY_TYPE
);
3297 size
= tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v
))));
3298 offset
*= BITS_PER_UNIT
;
3299 offset
+= token
* size
;
3301 /* Lookup the value in the constructor that is assumed to be array.
3302 This is equivalent to
3303 fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
3304 offset, size, NULL);
3305 but in a constant time. We expect that frontend produced a simple
3306 array without indexed initializers. */
3308 gcc_checking_assert (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
);
3309 domain_type
= TYPE_DOMAIN (TREE_TYPE (init
));
3310 gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type
)));
3311 elt_size
= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init
))));
3313 access_index
= offset
/ BITS_PER_UNIT
/ elt_size
;
3314 gcc_checking_assert (offset
% (elt_size
* BITS_PER_UNIT
) == 0);
3316 /* This code makes an assumption that there are no
3317 indexed fileds produced by C++ FE, so we can directly index the array. */
3318 if (access_index
< CONSTRUCTOR_NELTS (init
))
3320 fn
= CONSTRUCTOR_ELT (init
, access_index
)->value
;
3321 gcc_checking_assert (!CONSTRUCTOR_ELT (init
, access_index
)->index
);
3327 /* For type inconsistent program we may end up looking up virtual method
3328 in virtual table that does not contain TOKEN entries. We may overrun
3329 the virtual table and pick up a constant or RTTI info pointer.
3330 In any case the call is undefined. */
3332 || (TREE_CODE (fn
) != ADDR_EXPR
&& TREE_CODE (fn
) != FDESC_EXPR
)
3333 || TREE_CODE (TREE_OPERAND (fn
, 0)) != FUNCTION_DECL
)
3334 fn
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
3337 fn
= TREE_OPERAND (fn
, 0);
3339 /* When cgraph node is missing and function is not public, we cannot
3340 devirtualize. This can happen in WHOPR when the actual method
3341 ends up in other partition, because we found devirtualization
3342 possibility too late. */
3343 if (!can_refer_decl_in_current_unit_p (fn
, vtable
))
3354 /* Make sure we create a cgraph node for functions we'll reference.
3355 They can be non-existent if the reference comes from an entry
3356 of an external vtable for example. */
3357 cgraph_get_create_node (fn
);
3362 /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
3363 is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
3364 KNOWN_BINFO carries the binfo describing the true type of
3365 OBJ_TYPE_REF_OBJECT(REF).
3366 Set CAN_REFER if non-NULL to false if method
3367 is not referable or if the virtual table is ill-formed (such as rewriten
3368 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
3371 gimple_get_virt_method_for_binfo (HOST_WIDE_INT token
, tree known_binfo
,
3374 unsigned HOST_WIDE_INT offset
;
3377 v
= BINFO_VTABLE (known_binfo
);
3378 /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */
3382 if (!vtable_pointer_value_to_vtable (v
, &v
, &offset
))
3388 return gimple_get_virt_method_for_vtable (token
, v
, offset
, can_refer
);
3391 /* Return true iff VAL is a gimple expression that is known to be
3392 non-negative. Restricted to floating-point inputs. */
3395 gimple_val_nonnegative_real_p (tree val
)
3399 gcc_assert (val
&& SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)));
3401 /* Use existing logic for non-gimple trees. */
3402 if (tree_expr_nonnegative_p (val
))
3405 if (TREE_CODE (val
) != SSA_NAME
)
3408 /* Currently we look only at the immediately defining statement
3409 to make this determination, since recursion on defining
3410 statements of operands can lead to quadratic behavior in the
3411 worst case. This is expected to catch almost all occurrences
3412 in practice. It would be possible to implement limited-depth
3413 recursion if important cases are lost. Alternatively, passes
3414 that need this information (such as the pow/powi lowering code
3415 in the cse_sincos pass) could be revised to provide it through
3416 dataflow propagation. */
3418 def_stmt
= SSA_NAME_DEF_STMT (val
);
3420 if (is_gimple_assign (def_stmt
))
3424 /* See fold-const.c:tree_expr_nonnegative_p for additional
3425 cases that could be handled with recursion. */
3427 switch (gimple_assign_rhs_code (def_stmt
))
3430 /* Always true for floating-point operands. */
3434 /* True if the two operands are identical (since we are
3435 restricted to floating-point inputs). */
3436 op0
= gimple_assign_rhs1 (def_stmt
);
3437 op1
= gimple_assign_rhs2 (def_stmt
);
3440 || operand_equal_p (op0
, op1
, 0))
3447 else if (is_gimple_call (def_stmt
))
3449 tree fndecl
= gimple_call_fndecl (def_stmt
);
3451 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
3455 switch (DECL_FUNCTION_CODE (fndecl
))
3457 CASE_FLT_FN (BUILT_IN_ACOS
):
3458 CASE_FLT_FN (BUILT_IN_ACOSH
):
3459 CASE_FLT_FN (BUILT_IN_CABS
):
3460 CASE_FLT_FN (BUILT_IN_COSH
):
3461 CASE_FLT_FN (BUILT_IN_ERFC
):
3462 CASE_FLT_FN (BUILT_IN_EXP
):
3463 CASE_FLT_FN (BUILT_IN_EXP10
):
3464 CASE_FLT_FN (BUILT_IN_EXP2
):
3465 CASE_FLT_FN (BUILT_IN_FABS
):
3466 CASE_FLT_FN (BUILT_IN_FDIM
):
3467 CASE_FLT_FN (BUILT_IN_HYPOT
):
3468 CASE_FLT_FN (BUILT_IN_POW10
):
3471 CASE_FLT_FN (BUILT_IN_SQRT
):
3472 /* sqrt(-0.0) is -0.0, and sqrt is not defined over other
3473 nonnegative inputs. */
3474 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (val
))))
3479 CASE_FLT_FN (BUILT_IN_POWI
):
3480 /* True if the second argument is an even integer. */
3481 arg1
= gimple_call_arg (def_stmt
, 1);
3483 if (TREE_CODE (arg1
) == INTEGER_CST
3484 && (TREE_INT_CST_LOW (arg1
) & 1) == 0)
3489 CASE_FLT_FN (BUILT_IN_POW
):
3490 /* True if the second argument is an even integer-valued
3492 arg1
= gimple_call_arg (def_stmt
, 1);
3494 if (TREE_CODE (arg1
) == REAL_CST
)
3499 c
= TREE_REAL_CST (arg1
);
3500 n
= real_to_integer (&c
);
3504 REAL_VALUE_TYPE cint
;
3505 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
3506 if (real_identical (&c
, &cint
))
3522 /* Given a pointer value OP0, return a simplified version of an
3523 indirection through OP0, or NULL_TREE if no simplification is
3524 possible. Note that the resulting type may be different from
3525 the type pointed to in the sense that it is still compatible
3526 from the langhooks point of view. */
3529 gimple_fold_indirect_ref (tree t
)
3531 tree ptype
= TREE_TYPE (t
), type
= TREE_TYPE (ptype
);
3536 subtype
= TREE_TYPE (sub
);
3537 if (!POINTER_TYPE_P (subtype
))
3540 if (TREE_CODE (sub
) == ADDR_EXPR
)
3542 tree op
= TREE_OPERAND (sub
, 0);
3543 tree optype
= TREE_TYPE (op
);
3545 if (useless_type_conversion_p (type
, optype
))
3548 /* *(foo *)&fooarray => fooarray[0] */
3549 if (TREE_CODE (optype
) == ARRAY_TYPE
3550 && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype
))) == INTEGER_CST
3551 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
3553 tree type_domain
= TYPE_DOMAIN (optype
);
3554 tree min_val
= size_zero_node
;
3555 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
3556 min_val
= TYPE_MIN_VALUE (type_domain
);
3557 if (TREE_CODE (min_val
) == INTEGER_CST
)
3558 return build4 (ARRAY_REF
, type
, op
, min_val
, NULL_TREE
, NULL_TREE
);
3560 /* *(foo *)&complexfoo => __real__ complexfoo */
3561 else if (TREE_CODE (optype
) == COMPLEX_TYPE
3562 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
3563 return fold_build1 (REALPART_EXPR
, type
, op
);
3564 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
3565 else if (TREE_CODE (optype
) == VECTOR_TYPE
3566 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
3568 tree part_width
= TYPE_SIZE (type
);
3569 tree index
= bitsize_int (0);
3570 return fold_build3 (BIT_FIELD_REF
, type
, op
, part_width
, index
);
3574 /* *(p + CST) -> ... */
3575 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
3576 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
3578 tree addr
= TREE_OPERAND (sub
, 0);
3579 tree off
= TREE_OPERAND (sub
, 1);
3583 addrtype
= TREE_TYPE (addr
);
3585 /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
3586 if (TREE_CODE (addr
) == ADDR_EXPR
3587 && TREE_CODE (TREE_TYPE (addrtype
)) == VECTOR_TYPE
3588 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (addrtype
)))
3589 && tree_fits_uhwi_p (off
))
3591 unsigned HOST_WIDE_INT offset
= tree_to_uhwi (off
);
3592 tree part_width
= TYPE_SIZE (type
);
3593 unsigned HOST_WIDE_INT part_widthi
3594 = tree_to_shwi (part_width
) / BITS_PER_UNIT
;
3595 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
3596 tree index
= bitsize_int (indexi
);
3597 if (offset
/ part_widthi
3598 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype
)))
3599 return fold_build3 (BIT_FIELD_REF
, type
, TREE_OPERAND (addr
, 0),
3603 /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
3604 if (TREE_CODE (addr
) == ADDR_EXPR
3605 && TREE_CODE (TREE_TYPE (addrtype
)) == COMPLEX_TYPE
3606 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (addrtype
))))
3608 tree size
= TYPE_SIZE_UNIT (type
);
3609 if (tree_int_cst_equal (size
, off
))
3610 return fold_build1 (IMAGPART_EXPR
, type
, TREE_OPERAND (addr
, 0));
3613 /* *(p + CST) -> MEM_REF <p, CST>. */
3614 if (TREE_CODE (addr
) != ADDR_EXPR
3615 || DECL_P (TREE_OPERAND (addr
, 0)))
3616 return fold_build2 (MEM_REF
, type
,
3618 wide_int_to_tree (ptype
, off
));
3621 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
3622 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
3623 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype
)))) == INTEGER_CST
3624 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (subtype
))))
3627 tree min_val
= size_zero_node
;
3629 sub
= gimple_fold_indirect_ref (sub
);
3631 sub
= build1 (INDIRECT_REF
, TREE_TYPE (subtype
), osub
);
3632 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
3633 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
3634 min_val
= TYPE_MIN_VALUE (type_domain
);
3635 if (TREE_CODE (min_val
) == INTEGER_CST
)
3636 return build4 (ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
, NULL_TREE
);
3642 /* Return true if CODE is an operation that when operating on signed
3643 integer types involves undefined behavior on overflow and the
3644 operation can be expressed with unsigned arithmetic. */
3647 arith_code_with_undefined_signed_overflow (tree_code code
)
3655 case POINTER_PLUS_EXPR
:
3662 /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic
3663 operation that can be transformed to unsigned arithmetic by converting
3664 its operand, carrying out the operation in the corresponding unsigned
3665 type and converting the result back to the original type.
3667 Returns a sequence of statements that replace STMT and also contain
3668 a modified form of STMT itself. */
3671 rewrite_to_defined_overflow (gimple stmt
)
3673 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3675 fprintf (dump_file
, "rewriting stmt with undefined signed "
3677 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
3680 tree lhs
= gimple_assign_lhs (stmt
);
3681 tree type
= unsigned_type_for (TREE_TYPE (lhs
));
3682 gimple_seq stmts
= NULL
;
3683 for (unsigned i
= 1; i
< gimple_num_ops (stmt
); ++i
)
3685 gimple_seq stmts2
= NULL
;
3686 gimple_set_op (stmt
, i
,
3687 force_gimple_operand (fold_convert (type
,
3688 gimple_op (stmt
, i
)),
3689 &stmts2
, true, NULL_TREE
));
3690 gimple_seq_add_seq (&stmts
, stmts2
);
3692 gimple_assign_set_lhs (stmt
, make_ssa_name (type
, stmt
));
3693 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
3694 gimple_assign_set_rhs_code (stmt
, PLUS_EXPR
);
3695 gimple_seq_add_stmt (&stmts
, stmt
);
3696 gimple cvt
= gimple_build_assign_with_ops
3697 (NOP_EXPR
, lhs
, gimple_assign_lhs (stmt
), NULL_TREE
);
3698 gimple_seq_add_stmt (&stmts
, cvt
);