1 /* Statement simplification on GIMPLE.
2 Copyright (C) 2010-2015 Free Software Foundation, Inc.
3 Split out from tree-ssa-ccp.c.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
31 #include "fold-const.h"
33 #include "insn-config.h"
42 #include "stor-layout.h"
44 #include "dominance.h"
45 #include "internal-fn.h"
46 #include "gimple-fold.h"
48 #include "gimple-iterator.h"
49 #include "tree-into-ssa.h"
52 #include "tree-ssa-propagate.h"
55 #include "ipa-utils.h"
56 #include "gimple-pretty-print.h"
57 #include "tree-ssa-address.h"
58 #include "langhooks.h"
59 #include "gimplify-me.h"
64 #include "gimple-match.h"
66 /* Return true when DECL can be referenced from current unit.
67 FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
68 We can get declarations that are not possible to reference for various
71 1) When analyzing C++ virtual tables.
72 C++ virtual tables do have known constructors even
73 when they are keyed to other compilation unit.
74 Those tables can contain pointers to methods and vars
75 in other units. Those methods have both STATIC and EXTERNAL
77 2) In WHOPR mode devirtualization might lead to reference
78 to method that was partitioned elsehwere.
79 In this case we have static VAR_DECL or FUNCTION_DECL
80 that has no corresponding callgraph/varpool node
82 3) COMDAT functions referred by external vtables that
83 we devirtualize only during final compilation stage.
84 At this time we already decided that we will not output
85 the function body and thus we can't reference the symbol
89 can_refer_decl_in_current_unit_p (tree decl
, tree from_decl
)
92 struct cgraph_node
*node
;
95 if (DECL_ABSTRACT_P (decl
))
98 /* We are concerned only about static/external vars and functions. */
99 if ((!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
100 || (TREE_CODE (decl
) != VAR_DECL
&& TREE_CODE (decl
) != FUNCTION_DECL
))
103 /* Static objects can be referred only if they was not optimized out yet. */
104 if (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
106 /* Before we start optimizing unreachable code we can be sure all
107 static objects are defined. */
108 if (symtab
->function_flags_ready
)
110 snode
= symtab_node::get (decl
);
111 if (!snode
|| !snode
->definition
)
113 node
= dyn_cast
<cgraph_node
*> (snode
);
114 return !node
|| !node
->global
.inlined_to
;
117 /* We will later output the initializer, so we can refer to it.
118 So we are concerned only when DECL comes from initializer of
119 external var or var that has been optimized out. */
121 || TREE_CODE (from_decl
) != VAR_DECL
122 || (!DECL_EXTERNAL (from_decl
)
123 && (vnode
= varpool_node::get (from_decl
)) != NULL
124 && vnode
->definition
)
126 && (vnode
= varpool_node::get (from_decl
)) != NULL
127 && vnode
->in_other_partition
))
129 /* We are folding reference from external vtable. The vtable may reffer
130 to a symbol keyed to other compilation unit. The other compilation
131 unit may be in separate DSO and the symbol may be hidden. */
132 if (DECL_VISIBILITY_SPECIFIED (decl
)
133 && DECL_EXTERNAL (decl
)
134 && DECL_VISIBILITY (decl
) != VISIBILITY_DEFAULT
135 && (!(snode
= symtab_node::get (decl
)) || !snode
->in_other_partition
))
137 /* When function is public, we always can introduce new reference.
138 Exception are the COMDAT functions where introducing a direct
139 reference imply need to include function body in the curren tunit. */
140 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
))
142 /* We have COMDAT. We are going to check if we still have definition
143 or if the definition is going to be output in other partition.
144 Bypass this when gimplifying; all needed functions will be produced.
146 As observed in PR20991 for already optimized out comdat virtual functions
147 it may be tempting to not necessarily give up because the copy will be
148 output elsewhere when corresponding vtable is output.
149 This is however not possible - ABI specify that COMDATs are output in
150 units where they are used and when the other unit was compiled with LTO
151 it is possible that vtable was kept public while the function itself
153 if (!symtab
->function_flags_ready
)
156 snode
= symtab_node::get (decl
);
158 || ((!snode
->definition
|| DECL_EXTERNAL (decl
))
159 && (!snode
->in_other_partition
160 || (!snode
->forced_by_abi
&& !snode
->force_output
))))
162 node
= dyn_cast
<cgraph_node
*> (snode
);
163 return !node
|| !node
->global
.inlined_to
;
166 /* CVAL is value taken from DECL_INITIAL of variable. Try to transform it into
167 acceptable form for is_gimple_min_invariant.
168 FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL. */
171 canonicalize_constructor_val (tree cval
, tree from_decl
)
173 tree orig_cval
= cval
;
175 if (TREE_CODE (cval
) == POINTER_PLUS_EXPR
176 && TREE_CODE (TREE_OPERAND (cval
, 1)) == INTEGER_CST
)
178 tree ptr
= TREE_OPERAND (cval
, 0);
179 if (is_gimple_min_invariant (ptr
))
180 cval
= build1_loc (EXPR_LOCATION (cval
),
181 ADDR_EXPR
, TREE_TYPE (ptr
),
182 fold_build2 (MEM_REF
, TREE_TYPE (TREE_TYPE (ptr
)),
184 fold_convert (ptr_type_node
,
185 TREE_OPERAND (cval
, 1))));
187 if (TREE_CODE (cval
) == ADDR_EXPR
)
189 tree base
= NULL_TREE
;
190 if (TREE_CODE (TREE_OPERAND (cval
, 0)) == COMPOUND_LITERAL_EXPR
)
192 base
= COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval
, 0));
194 TREE_OPERAND (cval
, 0) = base
;
197 base
= get_base_address (TREE_OPERAND (cval
, 0));
201 if ((TREE_CODE (base
) == VAR_DECL
202 || TREE_CODE (base
) == FUNCTION_DECL
)
203 && !can_refer_decl_in_current_unit_p (base
, from_decl
))
205 if (TREE_CODE (base
) == VAR_DECL
)
206 TREE_ADDRESSABLE (base
) = 1;
207 else if (TREE_CODE (base
) == FUNCTION_DECL
)
209 /* Make sure we create a cgraph node for functions we'll reference.
210 They can be non-existent if the reference comes from an entry
211 of an external vtable for example. */
212 cgraph_node::get_create (base
);
214 /* Fixup types in global initializers. */
215 if (TREE_TYPE (TREE_TYPE (cval
)) != TREE_TYPE (TREE_OPERAND (cval
, 0)))
216 cval
= build_fold_addr_expr (TREE_OPERAND (cval
, 0));
218 if (!useless_type_conversion_p (TREE_TYPE (orig_cval
), TREE_TYPE (cval
)))
219 cval
= fold_convert (TREE_TYPE (orig_cval
), cval
);
222 if (TREE_OVERFLOW_P (cval
))
223 return drop_tree_overflow (cval
);
227 /* If SYM is a constant variable with known value, return the value.
228 NULL_TREE is returned otherwise. */
231 get_symbol_constant_value (tree sym
)
233 tree val
= ctor_for_folding (sym
);
234 if (val
!= error_mark_node
)
238 val
= canonicalize_constructor_val (unshare_expr (val
), sym
);
239 if (val
&& is_gimple_min_invariant (val
))
244 /* Variables declared 'const' without an initializer
245 have zero as the initializer if they may not be
246 overridden at link or run time. */
248 && is_gimple_reg_type (TREE_TYPE (sym
)))
249 return build_zero_cst (TREE_TYPE (sym
));
257 /* Subroutine of fold_stmt. We perform several simplifications of the
258 memory reference tree EXPR and make sure to re-gimplify them properly
259 after propagation of constant addresses. IS_LHS is true if the
260 reference is supposed to be an lvalue. */
263 maybe_fold_reference (tree expr
, bool is_lhs
)
267 if ((TREE_CODE (expr
) == VIEW_CONVERT_EXPR
268 || TREE_CODE (expr
) == REALPART_EXPR
269 || TREE_CODE (expr
) == IMAGPART_EXPR
)
270 && CONSTANT_CLASS_P (TREE_OPERAND (expr
, 0)))
271 return fold_unary_loc (EXPR_LOCATION (expr
),
274 TREE_OPERAND (expr
, 0));
275 else if (TREE_CODE (expr
) == BIT_FIELD_REF
276 && CONSTANT_CLASS_P (TREE_OPERAND (expr
, 0)))
277 return fold_ternary_loc (EXPR_LOCATION (expr
),
280 TREE_OPERAND (expr
, 0),
281 TREE_OPERAND (expr
, 1),
282 TREE_OPERAND (expr
, 2));
285 && (result
= fold_const_aggregate_ref (expr
))
286 && is_gimple_min_invariant (result
))
293 /* Attempt to fold an assignment statement pointed-to by SI. Returns a
294 replacement rhs for the statement or NULL_TREE if no simplification
295 could be made. It is assumed that the operands have been previously
299 fold_gimple_assign (gimple_stmt_iterator
*si
)
301 gimple stmt
= gsi_stmt (*si
);
302 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
303 location_t loc
= gimple_location (stmt
);
305 tree result
= NULL_TREE
;
307 switch (get_gimple_rhs_class (subcode
))
309 case GIMPLE_SINGLE_RHS
:
311 tree rhs
= gimple_assign_rhs1 (stmt
);
313 if (TREE_CLOBBER_P (rhs
))
316 if (REFERENCE_CLASS_P (rhs
))
317 return maybe_fold_reference (rhs
, false);
319 else if (TREE_CODE (rhs
) == OBJ_TYPE_REF
)
321 tree val
= OBJ_TYPE_REF_EXPR (rhs
);
322 if (is_gimple_min_invariant (val
))
324 else if (flag_devirtualize
&& virtual_method_call_p (rhs
))
327 vec
<cgraph_node
*>targets
328 = possible_polymorphic_call_targets (rhs
, stmt
, &final
);
329 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
331 if (dump_enabled_p ())
333 location_t loc
= gimple_location_safe (stmt
);
334 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, loc
,
335 "resolving virtual function address "
336 "reference to function %s\n",
337 targets
.length () == 1
338 ? targets
[0]->name ()
341 if (targets
.length () == 1)
343 val
= fold_convert (TREE_TYPE (val
),
344 build_fold_addr_expr_loc
345 (loc
, targets
[0]->decl
));
346 STRIP_USELESS_TYPE_CONVERSION (val
);
349 /* We can not use __builtin_unreachable here because it
350 can not have address taken. */
351 val
= build_int_cst (TREE_TYPE (val
), 0);
357 else if (TREE_CODE (rhs
) == ADDR_EXPR
)
359 tree ref
= TREE_OPERAND (rhs
, 0);
360 tree tem
= maybe_fold_reference (ref
, true);
362 && TREE_CODE (tem
) == MEM_REF
363 && integer_zerop (TREE_OPERAND (tem
, 1)))
364 result
= fold_convert (TREE_TYPE (rhs
), TREE_OPERAND (tem
, 0));
366 result
= fold_convert (TREE_TYPE (rhs
),
367 build_fold_addr_expr_loc (loc
, tem
));
368 else if (TREE_CODE (ref
) == MEM_REF
369 && integer_zerop (TREE_OPERAND (ref
, 1)))
370 result
= fold_convert (TREE_TYPE (rhs
), TREE_OPERAND (ref
, 0));
373 else if (TREE_CODE (rhs
) == CONSTRUCTOR
374 && TREE_CODE (TREE_TYPE (rhs
)) == VECTOR_TYPE
375 && (CONSTRUCTOR_NELTS (rhs
)
376 == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs
))))
378 /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */
382 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs
), i
, val
)
383 if (TREE_CODE (val
) != INTEGER_CST
384 && TREE_CODE (val
) != REAL_CST
385 && TREE_CODE (val
) != FIXED_CST
)
388 return build_vector_from_ctor (TREE_TYPE (rhs
),
389 CONSTRUCTOR_ELTS (rhs
));
392 else if (DECL_P (rhs
))
393 return get_symbol_constant_value (rhs
);
395 /* If we couldn't fold the RHS, hand over to the generic
397 if (result
== NULL_TREE
)
400 /* Strip away useless type conversions. Both the NON_LVALUE_EXPR
401 that may have been added by fold, and "useless" type
402 conversions that might now be apparent due to propagation. */
403 STRIP_USELESS_TYPE_CONVERSION (result
);
405 if (result
!= rhs
&& valid_gimple_rhs_p (result
))
412 case GIMPLE_UNARY_RHS
:
415 case GIMPLE_BINARY_RHS
:
418 case GIMPLE_TERNARY_RHS
:
419 /* Try to fold a conditional expression. */
420 if (gimple_assign_rhs_code (stmt
) == COND_EXPR
)
422 tree op0
= gimple_assign_rhs1 (stmt
);
425 location_t cond_loc
= gimple_location (stmt
);
427 if (COMPARISON_CLASS_P (op0
))
429 fold_defer_overflow_warnings ();
430 tem
= fold_binary_loc (cond_loc
,
431 TREE_CODE (op0
), TREE_TYPE (op0
),
432 TREE_OPERAND (op0
, 0),
433 TREE_OPERAND (op0
, 1));
434 /* This is actually a conditional expression, not a GIMPLE
435 conditional statement, however, the valid_gimple_rhs_p
436 test still applies. */
437 set
= (tem
&& is_gimple_condexpr (tem
)
438 && valid_gimple_rhs_p (tem
));
439 fold_undefer_overflow_warnings (set
, stmt
, 0);
441 else if (is_gimple_min_invariant (op0
))
450 result
= fold_build3_loc (cond_loc
, COND_EXPR
,
451 TREE_TYPE (gimple_assign_lhs (stmt
)), tem
,
452 gimple_assign_rhs2 (stmt
),
453 gimple_assign_rhs3 (stmt
));
457 result
= fold_ternary_loc (loc
, subcode
,
458 TREE_TYPE (gimple_assign_lhs (stmt
)),
459 gimple_assign_rhs1 (stmt
),
460 gimple_assign_rhs2 (stmt
),
461 gimple_assign_rhs3 (stmt
));
465 STRIP_USELESS_TYPE_CONVERSION (result
);
466 if (valid_gimple_rhs_p (result
))
471 case GIMPLE_INVALID_RHS
:
479 /* Replace a statement at *SI_P with a sequence of statements in STMTS,
480 adjusting the replacement stmts location and virtual operands.
481 If the statement has a lhs the last stmt in the sequence is expected
482 to assign to that lhs. */
485 gsi_replace_with_seq_vops (gimple_stmt_iterator
*si_p
, gimple_seq stmts
)
487 gimple stmt
= gsi_stmt (*si_p
);
489 if (gimple_has_location (stmt
))
490 annotate_all_with_location (stmts
, gimple_location (stmt
));
492 /* First iterate over the replacement statements backward, assigning
493 virtual operands to their defining statements. */
494 gimple laststore
= NULL
;
495 for (gimple_stmt_iterator i
= gsi_last (stmts
);
496 !gsi_end_p (i
); gsi_prev (&i
))
498 gimple new_stmt
= gsi_stmt (i
);
499 if ((gimple_assign_single_p (new_stmt
)
500 && !is_gimple_reg (gimple_assign_lhs (new_stmt
)))
501 || (is_gimple_call (new_stmt
)
502 && (gimple_call_flags (new_stmt
)
503 & (ECF_NOVOPS
| ECF_PURE
| ECF_CONST
| ECF_NORETURN
)) == 0))
507 vdef
= gimple_vdef (stmt
);
509 vdef
= make_ssa_name (gimple_vop (cfun
), new_stmt
);
510 gimple_set_vdef (new_stmt
, vdef
);
511 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
512 SSA_NAME_DEF_STMT (vdef
) = new_stmt
;
513 laststore
= new_stmt
;
517 /* Second iterate over the statements forward, assigning virtual
518 operands to their uses. */
519 tree reaching_vuse
= gimple_vuse (stmt
);
520 for (gimple_stmt_iterator i
= gsi_start (stmts
);
521 !gsi_end_p (i
); gsi_next (&i
))
523 gimple new_stmt
= gsi_stmt (i
);
524 /* If the new statement possibly has a VUSE, update it with exact SSA
525 name we know will reach this one. */
526 if (gimple_has_mem_ops (new_stmt
))
527 gimple_set_vuse (new_stmt
, reaching_vuse
);
528 gimple_set_modified (new_stmt
, true);
529 if (gimple_vdef (new_stmt
))
530 reaching_vuse
= gimple_vdef (new_stmt
);
533 /* If the new sequence does not do a store release the virtual
534 definition of the original statement. */
536 && reaching_vuse
== gimple_vuse (stmt
))
538 tree vdef
= gimple_vdef (stmt
);
540 && TREE_CODE (vdef
) == SSA_NAME
)
542 unlink_stmt_vdef (stmt
);
543 release_ssa_name (vdef
);
547 /* Finally replace the original statement with the sequence. */
548 gsi_replace_with_seq (si_p
, stmts
, false);
551 /* Convert EXPR into a GIMPLE value suitable for substitution on the
552 RHS of an assignment. Insert the necessary statements before
553 iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL
554 is replaced. If the call is expected to produces a result, then it
555 is replaced by an assignment of the new RHS to the result variable.
556 If the result is to be ignored, then the call is replaced by a
557 GIMPLE_NOP. A proper VDEF chain is retained by making the first
558 VUSE and the last VDEF of the whole sequence be the same as the replaced
559 statement and using new SSA names for stores in between. */
562 gimplify_and_update_call_from_tree (gimple_stmt_iterator
*si_p
, tree expr
)
565 gimple stmt
, new_stmt
;
566 gimple_stmt_iterator i
;
567 gimple_seq stmts
= NULL
;
569 stmt
= gsi_stmt (*si_p
);
571 gcc_assert (is_gimple_call (stmt
));
573 push_gimplify_context (gimple_in_ssa_p (cfun
));
575 lhs
= gimple_call_lhs (stmt
);
576 if (lhs
== NULL_TREE
)
578 gimplify_and_add (expr
, &stmts
);
579 /* We can end up with folding a memcpy of an empty class assignment
580 which gets optimized away by C++ gimplification. */
581 if (gimple_seq_empty_p (stmts
))
583 pop_gimplify_context (NULL
);
584 if (gimple_in_ssa_p (cfun
))
586 unlink_stmt_vdef (stmt
);
589 gsi_replace (si_p
, gimple_build_nop (), true);
595 tree tmp
= get_initialized_tmp_var (expr
, &stmts
, NULL
);
596 new_stmt
= gimple_build_assign (lhs
, tmp
);
597 i
= gsi_last (stmts
);
598 gsi_insert_after_without_update (&i
, new_stmt
,
599 GSI_CONTINUE_LINKING
);
602 pop_gimplify_context (NULL
);
604 gsi_replace_with_seq_vops (si_p
, stmts
);
608 /* Replace the call at *GSI with the gimple value VAL. */
611 replace_call_with_value (gimple_stmt_iterator
*gsi
, tree val
)
613 gimple stmt
= gsi_stmt (*gsi
);
614 tree lhs
= gimple_call_lhs (stmt
);
618 if (!useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (val
)))
619 val
= fold_convert (TREE_TYPE (lhs
), val
);
620 repl
= gimple_build_assign (lhs
, val
);
623 repl
= gimple_build_nop ();
624 tree vdef
= gimple_vdef (stmt
);
625 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
627 unlink_stmt_vdef (stmt
);
628 release_ssa_name (vdef
);
630 gsi_replace (gsi
, repl
, true);
633 /* Replace the call at *GSI with the new call REPL and fold that
637 replace_call_with_call_and_fold (gimple_stmt_iterator
*gsi
, gimple repl
)
639 gimple stmt
= gsi_stmt (*gsi
);
640 gimple_call_set_lhs (repl
, gimple_call_lhs (stmt
));
641 gimple_set_location (repl
, gimple_location (stmt
));
642 if (gimple_vdef (stmt
)
643 && TREE_CODE (gimple_vdef (stmt
)) == SSA_NAME
)
645 gimple_set_vdef (repl
, gimple_vdef (stmt
));
646 gimple_set_vuse (repl
, gimple_vuse (stmt
));
647 SSA_NAME_DEF_STMT (gimple_vdef (repl
)) = repl
;
649 gsi_replace (gsi
, repl
, true);
653 /* Return true if VAR is a VAR_DECL or a component thereof. */
656 var_decl_component_p (tree var
)
659 while (handled_component_p (inner
))
660 inner
= TREE_OPERAND (inner
, 0);
661 return SSA_VAR_P (inner
);
664 /* Fold function call to builtin mem{{,p}cpy,move}. Return
665 false if no simplification can be made.
666 If ENDP is 0, return DEST (like memcpy).
667 If ENDP is 1, return DEST+LEN (like mempcpy).
668 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
669 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
673 gimple_fold_builtin_memory_op (gimple_stmt_iterator
*gsi
,
674 tree dest
, tree src
, int endp
)
676 gimple stmt
= gsi_stmt (*gsi
);
677 tree lhs
= gimple_call_lhs (stmt
);
678 tree len
= gimple_call_arg (stmt
, 2);
679 tree destvar
, srcvar
;
680 location_t loc
= gimple_location (stmt
);
682 /* If the LEN parameter is zero, return DEST. */
683 if (integer_zerop (len
))
686 if (gimple_call_lhs (stmt
))
687 repl
= gimple_build_assign (gimple_call_lhs (stmt
), dest
);
689 repl
= gimple_build_nop ();
690 tree vdef
= gimple_vdef (stmt
);
691 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
693 unlink_stmt_vdef (stmt
);
694 release_ssa_name (vdef
);
696 gsi_replace (gsi
, repl
, true);
700 /* If SRC and DEST are the same (and not volatile), return
701 DEST{,+LEN,+LEN-1}. */
702 if (operand_equal_p (src
, dest
, 0))
704 unlink_stmt_vdef (stmt
);
705 if (gimple_vdef (stmt
) && TREE_CODE (gimple_vdef (stmt
)) == SSA_NAME
)
706 release_ssa_name (gimple_vdef (stmt
));
709 gsi_replace (gsi
, gimple_build_nop (), true);
716 tree srctype
, desttype
;
717 unsigned int src_align
, dest_align
;
720 /* Build accesses at offset zero with a ref-all character type. */
721 off0
= build_int_cst (build_pointer_type_for_mode (char_type_node
,
724 /* If we can perform the copy efficiently with first doing all loads
725 and then all stores inline it that way. Currently efficiently
726 means that we can load all the memory into a single integer
727 register which is what MOVE_MAX gives us. */
728 src_align
= get_pointer_alignment (src
);
729 dest_align
= get_pointer_alignment (dest
);
730 if (tree_fits_uhwi_p (len
)
731 && compare_tree_int (len
, MOVE_MAX
) <= 0
732 /* ??? Don't transform copies from strings with known length this
733 confuses the tree-ssa-strlen.c. This doesn't handle
734 the case in gcc.dg/strlenopt-8.c which is XFAILed for that
736 && !c_strlen (src
, 2))
738 unsigned ilen
= tree_to_uhwi (len
);
739 if (exact_log2 (ilen
) != -1)
741 tree type
= lang_hooks
.types
.type_for_size (ilen
* 8, 1);
743 && TYPE_MODE (type
) != BLKmode
744 && (GET_MODE_SIZE (TYPE_MODE (type
)) * BITS_PER_UNIT
746 /* If the destination pointer is not aligned we must be able
747 to emit an unaligned store. */
748 && (dest_align
>= GET_MODE_ALIGNMENT (TYPE_MODE (type
))
749 || !SLOW_UNALIGNED_ACCESS (TYPE_MODE (type
), dest_align
)))
752 tree desttype
= type
;
753 if (src_align
< GET_MODE_ALIGNMENT (TYPE_MODE (type
)))
754 srctype
= build_aligned_type (type
, src_align
);
755 tree srcmem
= fold_build2 (MEM_REF
, srctype
, src
, off0
);
756 tree tem
= fold_const_aggregate_ref (srcmem
);
759 else if (src_align
< GET_MODE_ALIGNMENT (TYPE_MODE (type
))
760 && SLOW_UNALIGNED_ACCESS (TYPE_MODE (type
),
766 if (is_gimple_reg_type (TREE_TYPE (srcmem
)))
768 new_stmt
= gimple_build_assign (NULL_TREE
, srcmem
);
769 if (gimple_in_ssa_p (cfun
))
770 srcmem
= make_ssa_name (TREE_TYPE (srcmem
),
773 srcmem
= create_tmp_reg (TREE_TYPE (srcmem
));
774 gimple_assign_set_lhs (new_stmt
, srcmem
);
775 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
776 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
778 if (dest_align
< GET_MODE_ALIGNMENT (TYPE_MODE (type
)))
779 desttype
= build_aligned_type (type
, dest_align
);
781 = gimple_build_assign (fold_build2 (MEM_REF
, desttype
,
784 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
785 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
786 if (gimple_vdef (new_stmt
)
787 && TREE_CODE (gimple_vdef (new_stmt
)) == SSA_NAME
)
788 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt
)) = new_stmt
;
791 gsi_replace (gsi
, new_stmt
, true);
794 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
803 /* Both DEST and SRC must be pointer types.
804 ??? This is what old code did. Is the testing for pointer types
807 If either SRC is readonly or length is 1, we can use memcpy. */
808 if (!dest_align
|| !src_align
)
810 if (readonly_data_expr (src
)
811 || (tree_fits_uhwi_p (len
)
812 && (MIN (src_align
, dest_align
) / BITS_PER_UNIT
813 >= tree_to_uhwi (len
))))
815 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
818 gimple_call_set_fndecl (stmt
, fn
);
819 gimple_call_set_arg (stmt
, 0, dest
);
820 gimple_call_set_arg (stmt
, 1, src
);
825 /* If *src and *dest can't overlap, optimize into memcpy as well. */
826 if (TREE_CODE (src
) == ADDR_EXPR
827 && TREE_CODE (dest
) == ADDR_EXPR
)
829 tree src_base
, dest_base
, fn
;
830 HOST_WIDE_INT src_offset
= 0, dest_offset
= 0;
831 HOST_WIDE_INT size
= -1;
832 HOST_WIDE_INT maxsize
= -1;
834 srcvar
= TREE_OPERAND (src
, 0);
835 src_base
= get_ref_base_and_extent (srcvar
, &src_offset
,
837 destvar
= TREE_OPERAND (dest
, 0);
838 dest_base
= get_ref_base_and_extent (destvar
, &dest_offset
,
840 if (tree_fits_uhwi_p (len
))
841 maxsize
= tree_to_uhwi (len
);
844 src_offset
/= BITS_PER_UNIT
;
845 dest_offset
/= BITS_PER_UNIT
;
846 if (SSA_VAR_P (src_base
)
847 && SSA_VAR_P (dest_base
))
849 if (operand_equal_p (src_base
, dest_base
, 0)
850 && ranges_overlap_p (src_offset
, maxsize
,
851 dest_offset
, maxsize
))
854 else if (TREE_CODE (src_base
) == MEM_REF
855 && TREE_CODE (dest_base
) == MEM_REF
)
857 if (! operand_equal_p (TREE_OPERAND (src_base
, 0),
858 TREE_OPERAND (dest_base
, 0), 0))
860 offset_int off
= mem_ref_offset (src_base
) + src_offset
;
861 if (!wi::fits_shwi_p (off
))
863 src_offset
= off
.to_shwi ();
865 off
= mem_ref_offset (dest_base
) + dest_offset
;
866 if (!wi::fits_shwi_p (off
))
868 dest_offset
= off
.to_shwi ();
869 if (ranges_overlap_p (src_offset
, maxsize
,
870 dest_offset
, maxsize
))
876 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
879 gimple_call_set_fndecl (stmt
, fn
);
880 gimple_call_set_arg (stmt
, 0, dest
);
881 gimple_call_set_arg (stmt
, 1, src
);
886 /* If the destination and source do not alias optimize into
888 if ((is_gimple_min_invariant (dest
)
889 || TREE_CODE (dest
) == SSA_NAME
)
890 && (is_gimple_min_invariant (src
)
891 || TREE_CODE (src
) == SSA_NAME
))
894 ao_ref_init_from_ptr_and_size (&destr
, dest
, len
);
895 ao_ref_init_from_ptr_and_size (&srcr
, src
, len
);
896 if (!refs_may_alias_p_1 (&destr
, &srcr
, false))
899 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
902 gimple_call_set_fndecl (stmt
, fn
);
903 gimple_call_set_arg (stmt
, 0, dest
);
904 gimple_call_set_arg (stmt
, 1, src
);
913 if (!tree_fits_shwi_p (len
))
916 This logic lose for arguments like (type *)malloc (sizeof (type)),
917 since we strip the casts of up to VOID return value from malloc.
918 Perhaps we ought to inherit type from non-VOID argument here? */
921 if (!POINTER_TYPE_P (TREE_TYPE (src
))
922 || !POINTER_TYPE_P (TREE_TYPE (dest
)))
924 /* In the following try to find a type that is most natural to be
925 used for the memcpy source and destination and that allows
926 the most optimization when memcpy is turned into a plain assignment
927 using that type. In theory we could always use a char[len] type
928 but that only gains us that the destination and source possibly
929 no longer will have their address taken. */
930 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
931 if (TREE_CODE (src
) == POINTER_PLUS_EXPR
)
933 tree tem
= TREE_OPERAND (src
, 0);
935 if (tem
!= TREE_OPERAND (src
, 0))
936 src
= build1 (NOP_EXPR
, TREE_TYPE (tem
), src
);
938 if (TREE_CODE (dest
) == POINTER_PLUS_EXPR
)
940 tree tem
= TREE_OPERAND (dest
, 0);
942 if (tem
!= TREE_OPERAND (dest
, 0))
943 dest
= build1 (NOP_EXPR
, TREE_TYPE (tem
), dest
);
945 srctype
= TREE_TYPE (TREE_TYPE (src
));
946 if (TREE_CODE (srctype
) == ARRAY_TYPE
947 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype
), len
))
949 srctype
= TREE_TYPE (srctype
);
951 src
= build1 (NOP_EXPR
, build_pointer_type (srctype
), src
);
953 desttype
= TREE_TYPE (TREE_TYPE (dest
));
954 if (TREE_CODE (desttype
) == ARRAY_TYPE
955 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype
), len
))
957 desttype
= TREE_TYPE (desttype
);
959 dest
= build1 (NOP_EXPR
, build_pointer_type (desttype
), dest
);
961 if (TREE_ADDRESSABLE (srctype
)
962 || TREE_ADDRESSABLE (desttype
))
965 /* Make sure we are not copying using a floating-point mode or
966 a type whose size possibly does not match its precision. */
967 if (FLOAT_MODE_P (TYPE_MODE (desttype
))
968 || TREE_CODE (desttype
) == BOOLEAN_TYPE
969 || TREE_CODE (desttype
) == ENUMERAL_TYPE
)
970 desttype
= bitwise_type_for_mode (TYPE_MODE (desttype
));
971 if (FLOAT_MODE_P (TYPE_MODE (srctype
))
972 || TREE_CODE (srctype
) == BOOLEAN_TYPE
973 || TREE_CODE (srctype
) == ENUMERAL_TYPE
)
974 srctype
= bitwise_type_for_mode (TYPE_MODE (srctype
));
982 src_align
= get_pointer_alignment (src
);
983 dest_align
= get_pointer_alignment (dest
);
984 if (dest_align
< TYPE_ALIGN (desttype
)
985 || src_align
< TYPE_ALIGN (srctype
))
989 STRIP_NOPS (destvar
);
990 if (TREE_CODE (destvar
) == ADDR_EXPR
991 && var_decl_component_p (TREE_OPERAND (destvar
, 0))
992 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype
), len
))
993 destvar
= fold_build2 (MEM_REF
, desttype
, destvar
, off0
);
999 if (TREE_CODE (srcvar
) == ADDR_EXPR
1000 && var_decl_component_p (TREE_OPERAND (srcvar
, 0))
1001 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype
), len
))
1004 || src_align
>= TYPE_ALIGN (desttype
))
1005 srcvar
= fold_build2 (MEM_REF
, destvar
? desttype
: srctype
,
1007 else if (!STRICT_ALIGNMENT
)
1009 srctype
= build_aligned_type (TYPE_MAIN_VARIANT (desttype
),
1011 srcvar
= fold_build2 (MEM_REF
, srctype
, srcvar
, off0
);
1019 if (srcvar
== NULL_TREE
&& destvar
== NULL_TREE
)
1022 if (srcvar
== NULL_TREE
)
1025 if (src_align
>= TYPE_ALIGN (desttype
))
1026 srcvar
= fold_build2 (MEM_REF
, desttype
, src
, off0
);
1029 if (STRICT_ALIGNMENT
)
1031 srctype
= build_aligned_type (TYPE_MAIN_VARIANT (desttype
),
1033 srcvar
= fold_build2 (MEM_REF
, srctype
, src
, off0
);
1036 else if (destvar
== NULL_TREE
)
1039 if (dest_align
>= TYPE_ALIGN (srctype
))
1040 destvar
= fold_build2 (MEM_REF
, srctype
, dest
, off0
);
1043 if (STRICT_ALIGNMENT
)
1045 desttype
= build_aligned_type (TYPE_MAIN_VARIANT (srctype
),
1047 destvar
= fold_build2 (MEM_REF
, desttype
, dest
, off0
);
1052 if (is_gimple_reg_type (TREE_TYPE (srcvar
)))
1054 new_stmt
= gimple_build_assign (NULL_TREE
, srcvar
);
1055 if (gimple_in_ssa_p (cfun
))
1056 srcvar
= make_ssa_name (TREE_TYPE (srcvar
), new_stmt
);
1058 srcvar
= create_tmp_reg (TREE_TYPE (srcvar
));
1059 gimple_assign_set_lhs (new_stmt
, srcvar
);
1060 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
1061 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
1063 new_stmt
= gimple_build_assign (destvar
, srcvar
);
1064 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
1065 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
1066 if (gimple_vdef (new_stmt
)
1067 && TREE_CODE (gimple_vdef (new_stmt
)) == SSA_NAME
)
1068 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt
)) = new_stmt
;
1071 gsi_replace (gsi
, new_stmt
, true);
1074 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
1078 if (endp
== 0 || endp
== 3)
1081 len
= fold_build2_loc (loc
, MINUS_EXPR
, TREE_TYPE (len
), len
,
1083 if (endp
== 2 || endp
== 1)
1084 dest
= fold_build_pointer_plus_loc (loc
, dest
, len
);
1086 dest
= force_gimple_operand_gsi (gsi
, dest
, false, NULL_TREE
, true,
1088 gimple repl
= gimple_build_assign (lhs
, dest
);
1089 gsi_replace (gsi
, repl
, true);
1093 /* Fold function call to builtin memset or bzero at *GSI setting the
1094 memory of size LEN to VAL. Return whether a simplification was made. */
1097 gimple_fold_builtin_memset (gimple_stmt_iterator
*gsi
, tree c
, tree len
)
1099 gimple stmt
= gsi_stmt (*gsi
);
1101 unsigned HOST_WIDE_INT length
, cval
;
1103 /* If the LEN parameter is zero, return DEST. */
1104 if (integer_zerop (len
))
1106 replace_call_with_value (gsi
, gimple_call_arg (stmt
, 0));
1110 if (! tree_fits_uhwi_p (len
))
1113 if (TREE_CODE (c
) != INTEGER_CST
)
1116 tree dest
= gimple_call_arg (stmt
, 0);
1118 if (TREE_CODE (var
) != ADDR_EXPR
)
1121 var
= TREE_OPERAND (var
, 0);
1122 if (TREE_THIS_VOLATILE (var
))
1125 etype
= TREE_TYPE (var
);
1126 if (TREE_CODE (etype
) == ARRAY_TYPE
)
1127 etype
= TREE_TYPE (etype
);
1129 if (!INTEGRAL_TYPE_P (etype
)
1130 && !POINTER_TYPE_P (etype
))
1133 if (! var_decl_component_p (var
))
1136 length
= tree_to_uhwi (len
);
1137 if (GET_MODE_SIZE (TYPE_MODE (etype
)) != length
1138 || get_pointer_alignment (dest
) / BITS_PER_UNIT
< length
)
1141 if (length
> HOST_BITS_PER_WIDE_INT
/ BITS_PER_UNIT
)
1144 if (integer_zerop (c
))
1148 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8 || HOST_BITS_PER_WIDE_INT
> 64)
1151 cval
= TREE_INT_CST_LOW (c
);
1155 cval
|= (cval
<< 31) << 1;
1158 var
= fold_build2 (MEM_REF
, etype
, dest
, build_int_cst (ptr_type_node
, 0));
1159 gimple store
= gimple_build_assign (var
, build_int_cst_type (etype
, cval
));
1160 gimple_set_vuse (store
, gimple_vuse (stmt
));
1161 tree vdef
= gimple_vdef (stmt
);
1162 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
1164 gimple_set_vdef (store
, gimple_vdef (stmt
));
1165 SSA_NAME_DEF_STMT (gimple_vdef (stmt
)) = store
;
1167 gsi_insert_before (gsi
, store
, GSI_SAME_STMT
);
1168 if (gimple_call_lhs (stmt
))
1170 gimple asgn
= gimple_build_assign (gimple_call_lhs (stmt
), dest
);
1171 gsi_replace (gsi
, asgn
, true);
1175 gimple_stmt_iterator gsi2
= *gsi
;
1177 gsi_remove (&gsi2
, true);
1184 /* Return the string length, maximum string length or maximum value of
1186 If ARG is an SSA name variable, follow its use-def chains. If LENGTH
1187 is not NULL and, for TYPE == 0, its value is not equal to the length
1188 we determine or if we are unable to determine the length or value,
1189 return false. VISITED is a bitmap of visited variables.
1190 TYPE is 0 if string length should be returned, 1 for maximum string
1191 length and 2 for maximum value ARG can have. */
1194 get_maxval_strlen (tree arg
, tree
*length
, bitmap
*visited
, int type
)
1199 if (TREE_CODE (arg
) != SSA_NAME
)
1201 /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */
1202 if (TREE_CODE (arg
) == ADDR_EXPR
1203 && TREE_CODE (TREE_OPERAND (arg
, 0)) == ARRAY_REF
1204 && integer_zerop (TREE_OPERAND (TREE_OPERAND (arg
, 0), 1)))
1206 tree aop0
= TREE_OPERAND (TREE_OPERAND (arg
, 0), 0);
1207 if (TREE_CODE (aop0
) == INDIRECT_REF
1208 && TREE_CODE (TREE_OPERAND (aop0
, 0)) == SSA_NAME
)
1209 return get_maxval_strlen (TREE_OPERAND (aop0
, 0),
1210 length
, visited
, type
);
1216 if (TREE_CODE (val
) != INTEGER_CST
1217 || tree_int_cst_sgn (val
) < 0)
1221 val
= c_strlen (arg
, 1);
1229 if (TREE_CODE (*length
) != INTEGER_CST
1230 || TREE_CODE (val
) != INTEGER_CST
)
1233 if (tree_int_cst_lt (*length
, val
))
1237 else if (simple_cst_equal (val
, *length
) != 1)
1245 /* If ARG is registered for SSA update we cannot look at its defining
1247 if (name_registered_for_update_p (arg
))
1250 /* If we were already here, break the infinite cycle. */
1252 *visited
= BITMAP_ALLOC (NULL
);
1253 if (!bitmap_set_bit (*visited
, SSA_NAME_VERSION (arg
)))
1257 def_stmt
= SSA_NAME_DEF_STMT (var
);
1259 switch (gimple_code (def_stmt
))
1262 /* The RHS of the statement defining VAR must either have a
1263 constant length or come from another SSA_NAME with a constant
1265 if (gimple_assign_single_p (def_stmt
)
1266 || gimple_assign_unary_nop_p (def_stmt
))
1268 tree rhs
= gimple_assign_rhs1 (def_stmt
);
1269 return get_maxval_strlen (rhs
, length
, visited
, type
);
1271 else if (gimple_assign_rhs_code (def_stmt
) == COND_EXPR
)
1273 tree op2
= gimple_assign_rhs2 (def_stmt
);
1274 tree op3
= gimple_assign_rhs3 (def_stmt
);
1275 return get_maxval_strlen (op2
, length
, visited
, type
)
1276 && get_maxval_strlen (op3
, length
, visited
, type
);
1282 /* All the arguments of the PHI node must have the same constant
1286 for (i
= 0; i
< gimple_phi_num_args (def_stmt
); i
++)
1288 tree arg
= gimple_phi_arg (def_stmt
, i
)->def
;
1290 /* If this PHI has itself as an argument, we cannot
1291 determine the string length of this argument. However,
1292 if we can find a constant string length for the other
1293 PHI args then we can still be sure that this is a
1294 constant string length. So be optimistic and just
1295 continue with the next argument. */
1296 if (arg
== gimple_phi_result (def_stmt
))
1299 if (!get_maxval_strlen (arg
, length
, visited
, type
))
1311 get_maxval_strlen (tree arg
, int type
)
1313 bitmap visited
= NULL
;
1314 tree len
= NULL_TREE
;
1315 if (!get_maxval_strlen (arg
, &len
, &visited
, type
))
1318 BITMAP_FREE (visited
);
1324 /* Fold function call to builtin strcpy with arguments DEST and SRC.
1325 If LEN is not NULL, it represents the length of the string to be
1326 copied. Return NULL_TREE if no simplification can be made. */
1329 gimple_fold_builtin_strcpy (gimple_stmt_iterator
*gsi
,
1330 tree dest
, tree src
)
1332 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1335 /* If SRC and DEST are the same (and not volatile), return DEST. */
1336 if (operand_equal_p (src
, dest
, 0))
1338 replace_call_with_value (gsi
, dest
);
1342 if (optimize_function_for_size_p (cfun
))
1345 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
1349 tree len
= get_maxval_strlen (src
, 0);
1353 len
= fold_convert_loc (loc
, size_type_node
, len
);
1354 len
= size_binop_loc (loc
, PLUS_EXPR
, len
, build_int_cst (size_type_node
, 1));
1355 len
= force_gimple_operand_gsi (gsi
, len
, true,
1356 NULL_TREE
, true, GSI_SAME_STMT
);
1357 gimple repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1358 replace_call_with_call_and_fold (gsi
, repl
);
1362 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
1363 If SLEN is not NULL, it represents the length of the source string.
1364 Return NULL_TREE if no simplification can be made. */
1367 gimple_fold_builtin_strncpy (gimple_stmt_iterator
*gsi
,
1368 tree dest
, tree src
, tree len
)
1370 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1373 /* If the LEN parameter is zero, return DEST. */
1374 if (integer_zerop (len
))
1376 replace_call_with_value (gsi
, dest
);
1380 /* We can't compare slen with len as constants below if len is not a
1382 if (TREE_CODE (len
) != INTEGER_CST
)
1385 /* Now, we must be passed a constant src ptr parameter. */
1386 tree slen
= get_maxval_strlen (src
, 0);
1387 if (!slen
|| TREE_CODE (slen
) != INTEGER_CST
)
1390 slen
= size_binop_loc (loc
, PLUS_EXPR
, slen
, ssize_int (1));
1392 /* We do not support simplification of this case, though we do
1393 support it when expanding trees into RTL. */
1394 /* FIXME: generate a call to __builtin_memset. */
1395 if (tree_int_cst_lt (slen
, len
))
1398 /* OK transform into builtin memcpy. */
1399 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
1403 len
= fold_convert_loc (loc
, size_type_node
, len
);
1404 len
= force_gimple_operand_gsi (gsi
, len
, true,
1405 NULL_TREE
, true, GSI_SAME_STMT
);
1406 gimple repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1407 replace_call_with_call_and_fold (gsi
, repl
);
1411 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
1414 Return NULL_TREE if no simplification was possible, otherwise return the
1415 simplified form of the call as a tree.
1417 The simplified form may be a constant or other expression which
1418 computes the same value, but in a more efficient manner (including
1419 calls to other builtin functions).
1421 The call may contain arguments which need to be evaluated, but
1422 which are not useful to determine the result of the call. In
1423 this case we return a chain of COMPOUND_EXPRs. The LHS of each
1424 COMPOUND_EXPR will be an argument which must be evaluated.
1425 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
1426 COMPOUND_EXPR in the chain will contain the tree for the simplified
1427 form of the builtin function call. */
1430 gimple_fold_builtin_strcat (gimple_stmt_iterator
*gsi
, tree dst
, tree src
)
1432 gimple stmt
= gsi_stmt (*gsi
);
1433 location_t loc
= gimple_location (stmt
);
1435 const char *p
= c_getstr (src
);
1437 /* If the string length is zero, return the dst parameter. */
1438 if (p
&& *p
== '\0')
1440 replace_call_with_value (gsi
, dst
);
1444 if (!optimize_bb_for_speed_p (gimple_bb (stmt
)))
1447 /* See if we can store by pieces into (dst + strlen(dst)). */
1449 tree strlen_fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
1450 tree memcpy_fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
1452 if (!strlen_fn
|| !memcpy_fn
)
1455 /* If the length of the source string isn't computable don't
1456 split strcat into strlen and memcpy. */
1457 tree len
= get_maxval_strlen (src
, 0);
1461 /* Create strlen (dst). */
1462 gimple_seq stmts
= NULL
, stmts2
;
1463 gimple repl
= gimple_build_call (strlen_fn
, 1, dst
);
1464 gimple_set_location (repl
, loc
);
1465 if (gimple_in_ssa_p (cfun
))
1466 newdst
= make_ssa_name (size_type_node
);
1468 newdst
= create_tmp_reg (size_type_node
);
1469 gimple_call_set_lhs (repl
, newdst
);
1470 gimple_seq_add_stmt_without_update (&stmts
, repl
);
1472 /* Create (dst p+ strlen (dst)). */
1473 newdst
= fold_build_pointer_plus_loc (loc
, dst
, newdst
);
1474 newdst
= force_gimple_operand (newdst
, &stmts2
, true, NULL_TREE
);
1475 gimple_seq_add_seq_without_update (&stmts
, stmts2
);
1477 len
= fold_convert_loc (loc
, size_type_node
, len
);
1478 len
= size_binop_loc (loc
, PLUS_EXPR
, len
,
1479 build_int_cst (size_type_node
, 1));
1480 len
= force_gimple_operand (len
, &stmts2
, true, NULL_TREE
);
1481 gimple_seq_add_seq_without_update (&stmts
, stmts2
);
1483 repl
= gimple_build_call (memcpy_fn
, 3, newdst
, src
, len
);
1484 gimple_seq_add_stmt_without_update (&stmts
, repl
);
1485 if (gimple_call_lhs (stmt
))
1487 repl
= gimple_build_assign (gimple_call_lhs (stmt
), dst
);
1488 gimple_seq_add_stmt_without_update (&stmts
, repl
);
1489 gsi_replace_with_seq_vops (gsi
, stmts
);
1490 /* gsi now points at the assignment to the lhs, get a
1491 stmt iterator to the memcpy call.
1492 ??? We can't use gsi_for_stmt as that doesn't work when the
1493 CFG isn't built yet. */
1494 gimple_stmt_iterator gsi2
= *gsi
;
1500 gsi_replace_with_seq_vops (gsi
, stmts
);
1506 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
1507 are the arguments to the call. */
1510 gimple_fold_builtin_strcat_chk (gimple_stmt_iterator
*gsi
)
1512 gimple stmt
= gsi_stmt (*gsi
);
1513 tree dest
= gimple_call_arg (stmt
, 0);
1514 tree src
= gimple_call_arg (stmt
, 1);
1515 tree size
= gimple_call_arg (stmt
, 2);
1521 /* If the SRC parameter is "", return DEST. */
1522 if (p
&& *p
== '\0')
1524 replace_call_with_value (gsi
, dest
);
1528 if (! tree_fits_uhwi_p (size
) || ! integer_all_onesp (size
))
1531 /* If __builtin_strcat_chk is used, assume strcat is available. */
1532 fn
= builtin_decl_explicit (BUILT_IN_STRCAT
);
1536 gimple repl
= gimple_build_call (fn
, 2, dest
, src
);
1537 replace_call_with_call_and_fold (gsi
, repl
);
1541 /* Simplify a call to the strncat builtin. */
1544 gimple_fold_builtin_strncat (gimple_stmt_iterator
*gsi
)
1546 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
1547 tree dst
= gimple_call_arg (stmt
, 0);
1548 tree src
= gimple_call_arg (stmt
, 1);
1549 tree len
= gimple_call_arg (stmt
, 2);
1551 const char *p
= c_getstr (src
);
1553 /* If the requested length is zero, or the src parameter string
1554 length is zero, return the dst parameter. */
1555 if (integer_zerop (len
) || (p
&& *p
== '\0'))
1557 replace_call_with_value (gsi
, dst
);
1561 /* If the requested len is greater than or equal to the string
1562 length, call strcat. */
1563 if (TREE_CODE (len
) == INTEGER_CST
&& p
1564 && compare_tree_int (len
, strlen (p
)) >= 0)
1566 tree fn
= builtin_decl_implicit (BUILT_IN_STRCAT
);
1568 /* If the replacement _DECL isn't initialized, don't do the
1573 gcall
*repl
= gimple_build_call (fn
, 2, dst
, src
);
1574 replace_call_with_call_and_fold (gsi
, repl
);
1581 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
1585 gimple_fold_builtin_strncat_chk (gimple_stmt_iterator
*gsi
)
1587 gimple stmt
= gsi_stmt (*gsi
);
1588 tree dest
= gimple_call_arg (stmt
, 0);
1589 tree src
= gimple_call_arg (stmt
, 1);
1590 tree len
= gimple_call_arg (stmt
, 2);
1591 tree size
= gimple_call_arg (stmt
, 3);
1596 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
1597 if ((p
&& *p
== '\0')
1598 || integer_zerop (len
))
1600 replace_call_with_value (gsi
, dest
);
1604 if (! tree_fits_uhwi_p (size
))
1607 if (! integer_all_onesp (size
))
1609 tree src_len
= c_strlen (src
, 1);
1611 && tree_fits_uhwi_p (src_len
)
1612 && tree_fits_uhwi_p (len
)
1613 && ! tree_int_cst_lt (len
, src_len
))
1615 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
1616 fn
= builtin_decl_explicit (BUILT_IN_STRCAT_CHK
);
1620 gimple repl
= gimple_build_call (fn
, 3, dest
, src
, size
);
1621 replace_call_with_call_and_fold (gsi
, repl
);
1627 /* If __builtin_strncat_chk is used, assume strncat is available. */
1628 fn
= builtin_decl_explicit (BUILT_IN_STRNCAT
);
1632 gimple repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1633 replace_call_with_call_and_fold (gsi
, repl
);
1637 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
1638 to the call. IGNORE is true if the value returned
1639 by the builtin will be ignored. UNLOCKED is true is true if this
1640 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
1641 the known length of the string. Return NULL_TREE if no simplification
1645 gimple_fold_builtin_fputs (gimple_stmt_iterator
*gsi
,
1646 tree arg0
, tree arg1
,
1649 gimple stmt
= gsi_stmt (*gsi
);
1651 /* If we're using an unlocked function, assume the other unlocked
1652 functions exist explicitly. */
1653 tree
const fn_fputc
= (unlocked
1654 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED
)
1655 : builtin_decl_implicit (BUILT_IN_FPUTC
));
1656 tree
const fn_fwrite
= (unlocked
1657 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED
)
1658 : builtin_decl_implicit (BUILT_IN_FWRITE
));
1660 /* If the return value is used, don't do the transformation. */
1661 if (gimple_call_lhs (stmt
))
1664 /* Get the length of the string passed to fputs. If the length
1665 can't be determined, punt. */
1666 tree len
= get_maxval_strlen (arg0
, 0);
1668 || TREE_CODE (len
) != INTEGER_CST
)
1671 switch (compare_tree_int (len
, 1))
1673 case -1: /* length is 0, delete the call entirely . */
1674 replace_call_with_value (gsi
, integer_zero_node
);
1677 case 0: /* length is 1, call fputc. */
1679 const char *p
= c_getstr (arg0
);
1685 gimple repl
= gimple_build_call (fn_fputc
, 2,
1687 (integer_type_node
, p
[0]), arg1
);
1688 replace_call_with_call_and_fold (gsi
, repl
);
1693 case 1: /* length is greater than 1, call fwrite. */
1695 /* If optimizing for size keep fputs. */
1696 if (optimize_function_for_size_p (cfun
))
1698 /* New argument list transforming fputs(string, stream) to
1699 fwrite(string, 1, len, stream). */
1703 gimple repl
= gimple_build_call (fn_fwrite
, 4, arg0
,
1704 size_one_node
, len
, arg1
);
1705 replace_call_with_call_and_fold (gsi
, repl
);
1714 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
1715 DEST, SRC, LEN, and SIZE are the arguments to the call.
1716 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
1717 code of the builtin. If MAXLEN is not NULL, it is maximum length
1718 passed as third argument. */
1721 gimple_fold_builtin_memory_chk (gimple_stmt_iterator
*gsi
,
1722 tree dest
, tree src
, tree len
, tree size
,
1723 enum built_in_function fcode
)
1725 gimple stmt
= gsi_stmt (*gsi
);
1726 location_t loc
= gimple_location (stmt
);
1727 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
1730 /* If SRC and DEST are the same (and not volatile), return DEST
1731 (resp. DEST+LEN for __mempcpy_chk). */
1732 if (fcode
!= BUILT_IN_MEMSET_CHK
&& operand_equal_p (src
, dest
, 0))
1734 if (fcode
!= BUILT_IN_MEMPCPY_CHK
)
1736 replace_call_with_value (gsi
, dest
);
1741 tree temp
= fold_build_pointer_plus_loc (loc
, dest
, len
);
1742 temp
= force_gimple_operand_gsi (gsi
, temp
,
1743 false, NULL_TREE
, true,
1745 replace_call_with_value (gsi
, temp
);
1750 if (! tree_fits_uhwi_p (size
))
1753 tree maxlen
= get_maxval_strlen (len
, 2);
1754 if (! integer_all_onesp (size
))
1756 if (! tree_fits_uhwi_p (len
))
1758 /* If LEN is not constant, try MAXLEN too.
1759 For MAXLEN only allow optimizing into non-_ocs function
1760 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
1761 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
1763 if (fcode
== BUILT_IN_MEMPCPY_CHK
&& ignore
)
1765 /* (void) __mempcpy_chk () can be optimized into
1766 (void) __memcpy_chk (). */
1767 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
1771 gimple repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
1772 replace_call_with_call_and_fold (gsi
, repl
);
1781 if (tree_int_cst_lt (size
, maxlen
))
1786 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
1787 mem{cpy,pcpy,move,set} is available. */
1790 case BUILT_IN_MEMCPY_CHK
:
1791 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY
);
1793 case BUILT_IN_MEMPCPY_CHK
:
1794 fn
= builtin_decl_explicit (BUILT_IN_MEMPCPY
);
1796 case BUILT_IN_MEMMOVE_CHK
:
1797 fn
= builtin_decl_explicit (BUILT_IN_MEMMOVE
);
1799 case BUILT_IN_MEMSET_CHK
:
1800 fn
= builtin_decl_explicit (BUILT_IN_MEMSET
);
1809 gimple repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1810 replace_call_with_call_and_fold (gsi
, repl
);
1814 /* Fold a call to the __st[rp]cpy_chk builtin.
1815 DEST, SRC, and SIZE are the arguments to the call.
1816 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
1817 code of the builtin. If MAXLEN is not NULL, it is maximum length of
1818 strings passed as second argument. */
1821 gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator
*gsi
,
1823 tree src
, tree size
,
1824 enum built_in_function fcode
)
1826 gimple stmt
= gsi_stmt (*gsi
);
1827 location_t loc
= gimple_location (stmt
);
1828 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
1831 /* If SRC and DEST are the same (and not volatile), return DEST. */
1832 if (fcode
== BUILT_IN_STRCPY_CHK
&& operand_equal_p (src
, dest
, 0))
1834 replace_call_with_value (gsi
, dest
);
1838 if (! tree_fits_uhwi_p (size
))
1841 tree maxlen
= get_maxval_strlen (src
, 1);
1842 if (! integer_all_onesp (size
))
1844 len
= c_strlen (src
, 1);
1845 if (! len
|| ! tree_fits_uhwi_p (len
))
1847 /* If LEN is not constant, try MAXLEN too.
1848 For MAXLEN only allow optimizing into non-_ocs function
1849 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
1850 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
1852 if (fcode
== BUILT_IN_STPCPY_CHK
)
1857 /* If return value of __stpcpy_chk is ignored,
1858 optimize into __strcpy_chk. */
1859 fn
= builtin_decl_explicit (BUILT_IN_STRCPY_CHK
);
1863 gimple repl
= gimple_build_call (fn
, 3, dest
, src
, size
);
1864 replace_call_with_call_and_fold (gsi
, repl
);
1868 if (! len
|| TREE_SIDE_EFFECTS (len
))
1871 /* If c_strlen returned something, but not a constant,
1872 transform __strcpy_chk into __memcpy_chk. */
1873 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
1877 len
= fold_convert_loc (loc
, size_type_node
, len
);
1878 len
= size_binop_loc (loc
, PLUS_EXPR
, len
,
1879 build_int_cst (size_type_node
, 1));
1880 len
= force_gimple_operand_gsi (gsi
, len
, true, NULL_TREE
,
1881 true, GSI_SAME_STMT
);
1882 gimple repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
1883 replace_call_with_call_and_fold (gsi
, repl
);
1890 if (! tree_int_cst_lt (maxlen
, size
))
1894 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
1895 fn
= builtin_decl_explicit (fcode
== BUILT_IN_STPCPY_CHK
1896 ? BUILT_IN_STPCPY
: BUILT_IN_STRCPY
);
1900 gimple repl
= gimple_build_call (fn
, 2, dest
, src
);
1901 replace_call_with_call_and_fold (gsi
, repl
);
1905 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
1906 are the arguments to the call. If MAXLEN is not NULL, it is maximum
1907 length passed as third argument. IGNORE is true if return value can be
1908 ignored. FCODE is the BUILT_IN_* code of the builtin. */
1911 gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator
*gsi
,
1912 tree dest
, tree src
,
1913 tree len
, tree size
,
1914 enum built_in_function fcode
)
1916 gimple stmt
= gsi_stmt (*gsi
);
1917 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
1920 if (fcode
== BUILT_IN_STPNCPY_CHK
&& ignore
)
1922 /* If return value of __stpncpy_chk is ignored,
1923 optimize into __strncpy_chk. */
1924 fn
= builtin_decl_explicit (BUILT_IN_STRNCPY_CHK
);
1927 gimple repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
1928 replace_call_with_call_and_fold (gsi
, repl
);
1933 if (! tree_fits_uhwi_p (size
))
1936 tree maxlen
= get_maxval_strlen (len
, 2);
1937 if (! integer_all_onesp (size
))
1939 if (! tree_fits_uhwi_p (len
))
1941 /* If LEN is not constant, try MAXLEN too.
1942 For MAXLEN only allow optimizing into non-_ocs function
1943 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
1944 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
1950 if (tree_int_cst_lt (size
, maxlen
))
1954 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
1955 fn
= builtin_decl_explicit (fcode
== BUILT_IN_STPNCPY_CHK
1956 ? BUILT_IN_STPNCPY
: BUILT_IN_STRNCPY
);
1960 gimple repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1961 replace_call_with_call_and_fold (gsi
, repl
);
1965 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
1966 Return NULL_TREE if no simplification can be made. */
1969 gimple_fold_builtin_stpcpy (gimple_stmt_iterator
*gsi
)
1971 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
1972 location_t loc
= gimple_location (stmt
);
1973 tree dest
= gimple_call_arg (stmt
, 0);
1974 tree src
= gimple_call_arg (stmt
, 1);
1975 tree fn
, len
, lenp1
;
1977 /* If the result is unused, replace stpcpy with strcpy. */
1978 if (gimple_call_lhs (stmt
) == NULL_TREE
)
1980 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
1983 gimple_call_set_fndecl (stmt
, fn
);
1988 len
= c_strlen (src
, 1);
1990 || TREE_CODE (len
) != INTEGER_CST
)
1993 if (optimize_function_for_size_p (cfun
)
1994 /* If length is zero it's small enough. */
1995 && !integer_zerop (len
))
1998 /* If the source has a known length replace stpcpy with memcpy. */
1999 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
2003 gimple_seq stmts
= NULL
;
2004 tree tem
= gimple_convert (&stmts
, loc
, size_type_node
, len
);
2005 lenp1
= gimple_build (&stmts
, loc
, PLUS_EXPR
, size_type_node
,
2006 tem
, build_int_cst (size_type_node
, 1));
2007 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2008 gcall
*repl
= gimple_build_call (fn
, 3, dest
, src
, lenp1
);
2009 gimple_set_vuse (repl
, gimple_vuse (stmt
));
2010 gimple_set_vdef (repl
, gimple_vdef (stmt
));
2011 if (gimple_vdef (repl
)
2012 && TREE_CODE (gimple_vdef (repl
)) == SSA_NAME
)
2013 SSA_NAME_DEF_STMT (gimple_vdef (repl
)) = repl
;
2014 gsi_insert_before (gsi
, repl
, GSI_SAME_STMT
);
2015 /* Replace the result with dest + len. */
2017 tem
= gimple_convert (&stmts
, loc
, sizetype
, len
);
2018 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2019 gassign
*ret
= gimple_build_assign (gimple_call_lhs (stmt
),
2020 POINTER_PLUS_EXPR
, dest
, tem
);
2021 gsi_replace (gsi
, ret
, true);
2022 /* Finally fold the memcpy call. */
2023 gimple_stmt_iterator gsi2
= *gsi
;
2029 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
2030 NULL_TREE if a normal call should be emitted rather than expanding
2031 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
2032 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
2033 passed as second argument. */
2036 gimple_fold_builtin_snprintf_chk (gimple_stmt_iterator
*gsi
,
2037 enum built_in_function fcode
)
2039 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
2040 tree dest
, size
, len
, fn
, fmt
, flag
;
2041 const char *fmt_str
;
2043 /* Verify the required arguments in the original call. */
2044 if (gimple_call_num_args (stmt
) < 5)
2047 dest
= gimple_call_arg (stmt
, 0);
2048 len
= gimple_call_arg (stmt
, 1);
2049 flag
= gimple_call_arg (stmt
, 2);
2050 size
= gimple_call_arg (stmt
, 3);
2051 fmt
= gimple_call_arg (stmt
, 4);
2053 if (! tree_fits_uhwi_p (size
))
2056 if (! integer_all_onesp (size
))
2058 tree maxlen
= get_maxval_strlen (len
, 2);
2059 if (! tree_fits_uhwi_p (len
))
2061 /* If LEN is not constant, try MAXLEN too.
2062 For MAXLEN only allow optimizing into non-_ocs function
2063 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2064 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
2070 if (tree_int_cst_lt (size
, maxlen
))
2074 if (!init_target_chars ())
2077 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
2078 or if format doesn't contain % chars or is "%s". */
2079 if (! integer_zerop (flag
))
2081 fmt_str
= c_getstr (fmt
);
2082 if (fmt_str
== NULL
)
2084 if (strchr (fmt_str
, target_percent
) != NULL
2085 && strcmp (fmt_str
, target_percent_s
))
2089 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
2091 fn
= builtin_decl_explicit (fcode
== BUILT_IN_VSNPRINTF_CHK
2092 ? BUILT_IN_VSNPRINTF
: BUILT_IN_SNPRINTF
);
2096 /* Replace the called function and the first 5 argument by 3 retaining
2097 trailing varargs. */
2098 gimple_call_set_fndecl (stmt
, fn
);
2099 gimple_call_set_fntype (stmt
, TREE_TYPE (fn
));
2100 gimple_call_set_arg (stmt
, 0, dest
);
2101 gimple_call_set_arg (stmt
, 1, len
);
2102 gimple_call_set_arg (stmt
, 2, fmt
);
2103 for (unsigned i
= 3; i
< gimple_call_num_args (stmt
) - 2; ++i
)
2104 gimple_call_set_arg (stmt
, i
, gimple_call_arg (stmt
, i
+ 2));
2105 gimple_set_num_ops (stmt
, gimple_num_ops (stmt
) - 2);
2110 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
2111 Return NULL_TREE if a normal call should be emitted rather than
2112 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
2113 or BUILT_IN_VSPRINTF_CHK. */
2116 gimple_fold_builtin_sprintf_chk (gimple_stmt_iterator
*gsi
,
2117 enum built_in_function fcode
)
2119 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
2120 tree dest
, size
, len
, fn
, fmt
, flag
;
2121 const char *fmt_str
;
2122 unsigned nargs
= gimple_call_num_args (stmt
);
2124 /* Verify the required arguments in the original call. */
2127 dest
= gimple_call_arg (stmt
, 0);
2128 flag
= gimple_call_arg (stmt
, 1);
2129 size
= gimple_call_arg (stmt
, 2);
2130 fmt
= gimple_call_arg (stmt
, 3);
2132 if (! tree_fits_uhwi_p (size
))
2137 if (!init_target_chars ())
2140 /* Check whether the format is a literal string constant. */
2141 fmt_str
= c_getstr (fmt
);
2142 if (fmt_str
!= NULL
)
2144 /* If the format doesn't contain % args or %%, we know the size. */
2145 if (strchr (fmt_str
, target_percent
) == 0)
2147 if (fcode
!= BUILT_IN_SPRINTF_CHK
|| nargs
== 4)
2148 len
= build_int_cstu (size_type_node
, strlen (fmt_str
));
2150 /* If the format is "%s" and first ... argument is a string literal,
2151 we know the size too. */
2152 else if (fcode
== BUILT_IN_SPRINTF_CHK
2153 && strcmp (fmt_str
, target_percent_s
) == 0)
2159 arg
= gimple_call_arg (stmt
, 4);
2160 if (POINTER_TYPE_P (TREE_TYPE (arg
)))
2162 len
= c_strlen (arg
, 1);
2163 if (! len
|| ! tree_fits_uhwi_p (len
))
2170 if (! integer_all_onesp (size
))
2172 if (! len
|| ! tree_int_cst_lt (len
, size
))
2176 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
2177 or if format doesn't contain % chars or is "%s". */
2178 if (! integer_zerop (flag
))
2180 if (fmt_str
== NULL
)
2182 if (strchr (fmt_str
, target_percent
) != NULL
2183 && strcmp (fmt_str
, target_percent_s
))
2187 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
2188 fn
= builtin_decl_explicit (fcode
== BUILT_IN_VSPRINTF_CHK
2189 ? BUILT_IN_VSPRINTF
: BUILT_IN_SPRINTF
);
2193 /* Replace the called function and the first 4 argument by 2 retaining
2194 trailing varargs. */
2195 gimple_call_set_fndecl (stmt
, fn
);
2196 gimple_call_set_fntype (stmt
, TREE_TYPE (fn
));
2197 gimple_call_set_arg (stmt
, 0, dest
);
2198 gimple_call_set_arg (stmt
, 1, fmt
);
2199 for (unsigned i
= 2; i
< gimple_call_num_args (stmt
) - 2; ++i
)
2200 gimple_call_set_arg (stmt
, i
, gimple_call_arg (stmt
, i
+ 2));
2201 gimple_set_num_ops (stmt
, gimple_num_ops (stmt
) - 2);
2206 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
2207 ORIG may be null if this is a 2-argument call. We don't attempt to
2208 simplify calls with more than 3 arguments.
2210 Return NULL_TREE if no simplification was possible, otherwise return the
2211 simplified form of the call as a tree. If IGNORED is true, it means that
2212 the caller does not use the returned value of the function. */
2215 gimple_fold_builtin_sprintf (gimple_stmt_iterator
*gsi
)
2217 gimple stmt
= gsi_stmt (*gsi
);
2218 tree dest
= gimple_call_arg (stmt
, 0);
2219 tree fmt
= gimple_call_arg (stmt
, 1);
2220 tree orig
= NULL_TREE
;
2221 const char *fmt_str
= NULL
;
2223 /* Verify the required arguments in the original call. We deal with two
2224 types of sprintf() calls: 'sprintf (str, fmt)' and
2225 'sprintf (dest, "%s", orig)'. */
2226 if (gimple_call_num_args (stmt
) > 3)
2229 if (gimple_call_num_args (stmt
) == 3)
2230 orig
= gimple_call_arg (stmt
, 2);
2232 /* Check whether the format is a literal string constant. */
2233 fmt_str
= c_getstr (fmt
);
2234 if (fmt_str
== NULL
)
2237 if (!init_target_chars ())
2240 /* If the format doesn't contain % args or %%, use strcpy. */
2241 if (strchr (fmt_str
, target_percent
) == NULL
)
2243 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
2248 /* Don't optimize sprintf (buf, "abc", ptr++). */
2252 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
2253 'format' is known to contain no % formats. */
2254 gimple_seq stmts
= NULL
;
2255 gimple repl
= gimple_build_call (fn
, 2, dest
, fmt
);
2256 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2257 if (gimple_call_lhs (stmt
))
2259 repl
= gimple_build_assign (gimple_call_lhs (stmt
),
2260 build_int_cst (integer_type_node
,
2262 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2263 gsi_replace_with_seq_vops (gsi
, stmts
);
2264 /* gsi now points at the assignment to the lhs, get a
2265 stmt iterator to the memcpy call.
2266 ??? We can't use gsi_for_stmt as that doesn't work when the
2267 CFG isn't built yet. */
2268 gimple_stmt_iterator gsi2
= *gsi
;
2274 gsi_replace_with_seq_vops (gsi
, stmts
);
2280 /* If the format is "%s", use strcpy if the result isn't used. */
2281 else if (fmt_str
&& strcmp (fmt_str
, target_percent_s
) == 0)
2284 fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
2289 /* Don't crash on sprintf (str1, "%s"). */
2293 tree orig_len
= NULL_TREE
;
2294 if (gimple_call_lhs (stmt
))
2296 orig_len
= get_maxval_strlen (orig
, 0);
2301 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
2302 gimple_seq stmts
= NULL
;
2303 gimple repl
= gimple_build_call (fn
, 2, dest
, orig
);
2304 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2305 if (gimple_call_lhs (stmt
))
2307 if (!useless_type_conversion_p (integer_type_node
,
2308 TREE_TYPE (orig_len
)))
2309 orig_len
= fold_convert (integer_type_node
, orig_len
);
2310 repl
= gimple_build_assign (gimple_call_lhs (stmt
), orig_len
);
2311 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2312 gsi_replace_with_seq_vops (gsi
, stmts
);
2313 /* gsi now points at the assignment to the lhs, get a
2314 stmt iterator to the memcpy call.
2315 ??? We can't use gsi_for_stmt as that doesn't work when the
2316 CFG isn't built yet. */
2317 gimple_stmt_iterator gsi2
= *gsi
;
2323 gsi_replace_with_seq_vops (gsi
, stmts
);
2331 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
2332 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
2333 attempt to simplify calls with more than 4 arguments.
2335 Return NULL_TREE if no simplification was possible, otherwise return the
2336 simplified form of the call as a tree. If IGNORED is true, it means that
2337 the caller does not use the returned value of the function. */
2340 gimple_fold_builtin_snprintf (gimple_stmt_iterator
*gsi
)
2342 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
2343 tree dest
= gimple_call_arg (stmt
, 0);
2344 tree destsize
= gimple_call_arg (stmt
, 1);
2345 tree fmt
= gimple_call_arg (stmt
, 2);
2346 tree orig
= NULL_TREE
;
2347 const char *fmt_str
= NULL
;
2349 if (gimple_call_num_args (stmt
) > 4)
2352 if (gimple_call_num_args (stmt
) == 4)
2353 orig
= gimple_call_arg (stmt
, 3);
2355 if (!tree_fits_uhwi_p (destsize
))
2357 unsigned HOST_WIDE_INT destlen
= tree_to_uhwi (destsize
);
2359 /* Check whether the format is a literal string constant. */
2360 fmt_str
= c_getstr (fmt
);
2361 if (fmt_str
== NULL
)
2364 if (!init_target_chars ())
2367 /* If the format doesn't contain % args or %%, use strcpy. */
2368 if (strchr (fmt_str
, target_percent
) == NULL
)
2370 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
2374 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
2378 /* We could expand this as
2379 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
2381 memcpy (str, fmt_with_nul_at_cstm1, cst);
2382 but in the former case that might increase code size
2383 and in the latter case grow .rodata section too much.
2385 size_t len
= strlen (fmt_str
);
2389 gimple_seq stmts
= NULL
;
2390 gimple repl
= gimple_build_call (fn
, 2, dest
, fmt
);
2391 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2392 if (gimple_call_lhs (stmt
))
2394 repl
= gimple_build_assign (gimple_call_lhs (stmt
),
2395 build_int_cst (integer_type_node
, len
));
2396 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2397 gsi_replace_with_seq_vops (gsi
, stmts
);
2398 /* gsi now points at the assignment to the lhs, get a
2399 stmt iterator to the memcpy call.
2400 ??? We can't use gsi_for_stmt as that doesn't work when the
2401 CFG isn't built yet. */
2402 gimple_stmt_iterator gsi2
= *gsi
;
2408 gsi_replace_with_seq_vops (gsi
, stmts
);
2414 /* If the format is "%s", use strcpy if the result isn't used. */
2415 else if (fmt_str
&& strcmp (fmt_str
, target_percent_s
) == 0)
2417 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
2421 /* Don't crash on snprintf (str1, cst, "%s"). */
2425 tree orig_len
= get_maxval_strlen (orig
, 0);
2426 if (!orig_len
|| TREE_CODE (orig_len
) != INTEGER_CST
)
2429 /* We could expand this as
2430 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
2432 memcpy (str1, str2_with_nul_at_cstm1, cst);
2433 but in the former case that might increase code size
2434 and in the latter case grow .rodata section too much.
2436 if (compare_tree_int (orig_len
, destlen
) >= 0)
2439 /* Convert snprintf (str1, cst, "%s", str2) into
2440 strcpy (str1, str2) if strlen (str2) < cst. */
2441 gimple_seq stmts
= NULL
;
2442 gimple repl
= gimple_build_call (fn
, 2, dest
, orig
);
2443 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2444 if (gimple_call_lhs (stmt
))
2446 if (!useless_type_conversion_p (integer_type_node
,
2447 TREE_TYPE (orig_len
)))
2448 orig_len
= fold_convert (integer_type_node
, orig_len
);
2449 repl
= gimple_build_assign (gimple_call_lhs (stmt
), orig_len
);
2450 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2451 gsi_replace_with_seq_vops (gsi
, stmts
);
2452 /* gsi now points at the assignment to the lhs, get a
2453 stmt iterator to the memcpy call.
2454 ??? We can't use gsi_for_stmt as that doesn't work when the
2455 CFG isn't built yet. */
2456 gimple_stmt_iterator gsi2
= *gsi
;
2462 gsi_replace_with_seq_vops (gsi
, stmts
);
2470 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
2471 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
2472 more than 3 arguments, and ARG may be null in the 2-argument case.
2474 Return NULL_TREE if no simplification was possible, otherwise return the
2475 simplified form of the call as a tree. FCODE is the BUILT_IN_*
2476 code of the function to be simplified. */
2479 gimple_fold_builtin_fprintf (gimple_stmt_iterator
*gsi
,
2480 tree fp
, tree fmt
, tree arg
,
2481 enum built_in_function fcode
)
2483 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
2484 tree fn_fputc
, fn_fputs
;
2485 const char *fmt_str
= NULL
;
2487 /* If the return value is used, don't do the transformation. */
2488 if (gimple_call_lhs (stmt
) != NULL_TREE
)
2491 /* Check whether the format is a literal string constant. */
2492 fmt_str
= c_getstr (fmt
);
2493 if (fmt_str
== NULL
)
2496 if (fcode
== BUILT_IN_FPRINTF_UNLOCKED
)
2498 /* If we're using an unlocked function, assume the other
2499 unlocked functions exist explicitly. */
2500 fn_fputc
= builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED
);
2501 fn_fputs
= builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED
);
2505 fn_fputc
= builtin_decl_implicit (BUILT_IN_FPUTC
);
2506 fn_fputs
= builtin_decl_implicit (BUILT_IN_FPUTS
);
2509 if (!init_target_chars ())
2512 /* If the format doesn't contain % args or %%, use strcpy. */
2513 if (strchr (fmt_str
, target_percent
) == NULL
)
2515 if (fcode
!= BUILT_IN_VFPRINTF
&& fcode
!= BUILT_IN_VFPRINTF_CHK
2519 /* If the format specifier was "", fprintf does nothing. */
2520 if (fmt_str
[0] == '\0')
2522 replace_call_with_value (gsi
, NULL_TREE
);
2526 /* When "string" doesn't contain %, replace all cases of
2527 fprintf (fp, string) with fputs (string, fp). The fputs
2528 builtin will take care of special cases like length == 1. */
2531 gcall
*repl
= gimple_build_call (fn_fputs
, 2, fmt
, fp
);
2532 replace_call_with_call_and_fold (gsi
, repl
);
2537 /* The other optimizations can be done only on the non-va_list variants. */
2538 else if (fcode
== BUILT_IN_VFPRINTF
|| fcode
== BUILT_IN_VFPRINTF_CHK
)
2541 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
2542 else if (strcmp (fmt_str
, target_percent_s
) == 0)
2544 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
2548 gcall
*repl
= gimple_build_call (fn_fputs
, 2, arg
, fp
);
2549 replace_call_with_call_and_fold (gsi
, repl
);
2554 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
2555 else if (strcmp (fmt_str
, target_percent_c
) == 0)
2558 || ! useless_type_conversion_p (integer_type_node
, TREE_TYPE (arg
)))
2562 gcall
*repl
= gimple_build_call (fn_fputc
, 2, arg
, fp
);
2563 replace_call_with_call_and_fold (gsi
, repl
);
2571 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
2572 FMT and ARG are the arguments to the call; we don't fold cases with
2573 more than 2 arguments, and ARG may be null if this is a 1-argument case.
2575 Return NULL_TREE if no simplification was possible, otherwise return the
2576 simplified form of the call as a tree. FCODE is the BUILT_IN_*
2577 code of the function to be simplified. */
2580 gimple_fold_builtin_printf (gimple_stmt_iterator
*gsi
, tree fmt
,
2581 tree arg
, enum built_in_function fcode
)
2583 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
2584 tree fn_putchar
, fn_puts
, newarg
;
2585 const char *fmt_str
= NULL
;
2587 /* If the return value is used, don't do the transformation. */
2588 if (gimple_call_lhs (stmt
) != NULL_TREE
)
2591 /* Check whether the format is a literal string constant. */
2592 fmt_str
= c_getstr (fmt
);
2593 if (fmt_str
== NULL
)
2596 if (fcode
== BUILT_IN_PRINTF_UNLOCKED
)
2598 /* If we're using an unlocked function, assume the other
2599 unlocked functions exist explicitly. */
2600 fn_putchar
= builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED
);
2601 fn_puts
= builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED
);
2605 fn_putchar
= builtin_decl_implicit (BUILT_IN_PUTCHAR
);
2606 fn_puts
= builtin_decl_implicit (BUILT_IN_PUTS
);
2609 if (!init_target_chars ())
2612 if (strcmp (fmt_str
, target_percent_s
) == 0
2613 || strchr (fmt_str
, target_percent
) == NULL
)
2617 if (strcmp (fmt_str
, target_percent_s
) == 0)
2619 if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
2622 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
2625 str
= c_getstr (arg
);
2631 /* The format specifier doesn't contain any '%' characters. */
2632 if (fcode
!= BUILT_IN_VPRINTF
&& fcode
!= BUILT_IN_VPRINTF_CHK
2638 /* If the string was "", printf does nothing. */
2641 replace_call_with_value (gsi
, NULL_TREE
);
2645 /* If the string has length of 1, call putchar. */
2648 /* Given printf("c"), (where c is any one character,)
2649 convert "c"[0] to an int and pass that to the replacement
2651 newarg
= build_int_cst (integer_type_node
, str
[0]);
2654 gcall
*repl
= gimple_build_call (fn_putchar
, 1, newarg
);
2655 replace_call_with_call_and_fold (gsi
, repl
);
2661 /* If the string was "string\n", call puts("string"). */
2662 size_t len
= strlen (str
);
2663 if ((unsigned char)str
[len
- 1] == target_newline
2664 && (size_t) (int) len
== len
2668 tree offset_node
, string_cst
;
2670 /* Create a NUL-terminated string that's one char shorter
2671 than the original, stripping off the trailing '\n'. */
2672 newarg
= build_string_literal (len
, str
);
2673 string_cst
= string_constant (newarg
, &offset_node
);
2674 gcc_checking_assert (string_cst
2675 && (TREE_STRING_LENGTH (string_cst
)
2677 && integer_zerop (offset_node
)
2679 TREE_STRING_POINTER (string_cst
)[len
- 1]
2681 /* build_string_literal creates a new STRING_CST,
2682 modify it in place to avoid double copying. */
2683 newstr
= CONST_CAST (char *, TREE_STRING_POINTER (string_cst
));
2684 newstr
[len
- 1] = '\0';
2687 gcall
*repl
= gimple_build_call (fn_puts
, 1, newarg
);
2688 replace_call_with_call_and_fold (gsi
, repl
);
2693 /* We'd like to arrange to call fputs(string,stdout) here,
2694 but we need stdout and don't have a way to get it yet. */
2699 /* The other optimizations can be done only on the non-va_list variants. */
2700 else if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
2703 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
2704 else if (strcmp (fmt_str
, target_percent_s_newline
) == 0)
2706 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
2710 gcall
*repl
= gimple_build_call (fn_puts
, 1, arg
);
2711 replace_call_with_call_and_fold (gsi
, repl
);
2716 /* If the format specifier was "%c", call __builtin_putchar(arg). */
2717 else if (strcmp (fmt_str
, target_percent_c
) == 0)
2719 if (!arg
|| ! useless_type_conversion_p (integer_type_node
,
2724 gcall
*repl
= gimple_build_call (fn_putchar
, 1, arg
);
2725 replace_call_with_call_and_fold (gsi
, repl
);
2735 /* Fold a call to __builtin_strlen with known length LEN. */
2738 gimple_fold_builtin_strlen (gimple_stmt_iterator
*gsi
)
2740 gimple stmt
= gsi_stmt (*gsi
);
2741 tree len
= get_maxval_strlen (gimple_call_arg (stmt
, 0), 0);
2744 len
= force_gimple_operand_gsi (gsi
, len
, true, NULL
, true, GSI_SAME_STMT
);
2745 replace_call_with_value (gsi
, len
);
2750 /* Fold the non-target builtin at *GSI and return whether any simplification
2754 gimple_fold_builtin (gimple_stmt_iterator
*gsi
)
2756 gcall
*stmt
= as_a
<gcall
*>(gsi_stmt (*gsi
));
2757 tree callee
= gimple_call_fndecl (stmt
);
2759 /* Give up for always_inline inline builtins until they are
2761 if (avoid_folding_inline_builtin (callee
))
2764 unsigned n
= gimple_call_num_args (stmt
);
2765 enum built_in_function fcode
= DECL_FUNCTION_CODE (callee
);
2768 case BUILT_IN_BZERO
:
2769 return gimple_fold_builtin_memset (gsi
, integer_zero_node
,
2770 gimple_call_arg (stmt
, 1));
2771 case BUILT_IN_MEMSET
:
2772 return gimple_fold_builtin_memset (gsi
,
2773 gimple_call_arg (stmt
, 1),
2774 gimple_call_arg (stmt
, 2));
2775 case BUILT_IN_BCOPY
:
2776 return gimple_fold_builtin_memory_op (gsi
, gimple_call_arg (stmt
, 1),
2777 gimple_call_arg (stmt
, 0), 3);
2778 case BUILT_IN_MEMCPY
:
2779 return gimple_fold_builtin_memory_op (gsi
, gimple_call_arg (stmt
, 0),
2780 gimple_call_arg (stmt
, 1), 0);
2781 case BUILT_IN_MEMPCPY
:
2782 return gimple_fold_builtin_memory_op (gsi
, gimple_call_arg (stmt
, 0),
2783 gimple_call_arg (stmt
, 1), 1);
2784 case BUILT_IN_MEMMOVE
:
2785 return gimple_fold_builtin_memory_op (gsi
, gimple_call_arg (stmt
, 0),
2786 gimple_call_arg (stmt
, 1), 3);
2787 case BUILT_IN_SPRINTF_CHK
:
2788 case BUILT_IN_VSPRINTF_CHK
:
2789 return gimple_fold_builtin_sprintf_chk (gsi
, fcode
);
2790 case BUILT_IN_STRCAT_CHK
:
2791 return gimple_fold_builtin_strcat_chk (gsi
);
2792 case BUILT_IN_STRNCAT_CHK
:
2793 return gimple_fold_builtin_strncat_chk (gsi
);
2794 case BUILT_IN_STRLEN
:
2795 return gimple_fold_builtin_strlen (gsi
);
2796 case BUILT_IN_STRCPY
:
2797 return gimple_fold_builtin_strcpy (gsi
,
2798 gimple_call_arg (stmt
, 0),
2799 gimple_call_arg (stmt
, 1));
2800 case BUILT_IN_STRNCPY
:
2801 return gimple_fold_builtin_strncpy (gsi
,
2802 gimple_call_arg (stmt
, 0),
2803 gimple_call_arg (stmt
, 1),
2804 gimple_call_arg (stmt
, 2));
2805 case BUILT_IN_STRCAT
:
2806 return gimple_fold_builtin_strcat (gsi
, gimple_call_arg (stmt
, 0),
2807 gimple_call_arg (stmt
, 1));
2808 case BUILT_IN_STRNCAT
:
2809 return gimple_fold_builtin_strncat (gsi
);
2810 case BUILT_IN_FPUTS
:
2811 return gimple_fold_builtin_fputs (gsi
, gimple_call_arg (stmt
, 0),
2812 gimple_call_arg (stmt
, 1), false);
2813 case BUILT_IN_FPUTS_UNLOCKED
:
2814 return gimple_fold_builtin_fputs (gsi
, gimple_call_arg (stmt
, 0),
2815 gimple_call_arg (stmt
, 1), true);
2816 case BUILT_IN_MEMCPY_CHK
:
2817 case BUILT_IN_MEMPCPY_CHK
:
2818 case BUILT_IN_MEMMOVE_CHK
:
2819 case BUILT_IN_MEMSET_CHK
:
2820 return gimple_fold_builtin_memory_chk (gsi
,
2821 gimple_call_arg (stmt
, 0),
2822 gimple_call_arg (stmt
, 1),
2823 gimple_call_arg (stmt
, 2),
2824 gimple_call_arg (stmt
, 3),
2826 case BUILT_IN_STPCPY
:
2827 return gimple_fold_builtin_stpcpy (gsi
);
2828 case BUILT_IN_STRCPY_CHK
:
2829 case BUILT_IN_STPCPY_CHK
:
2830 return gimple_fold_builtin_stxcpy_chk (gsi
,
2831 gimple_call_arg (stmt
, 0),
2832 gimple_call_arg (stmt
, 1),
2833 gimple_call_arg (stmt
, 2),
2835 case BUILT_IN_STRNCPY_CHK
:
2836 case BUILT_IN_STPNCPY_CHK
:
2837 return gimple_fold_builtin_stxncpy_chk (gsi
,
2838 gimple_call_arg (stmt
, 0),
2839 gimple_call_arg (stmt
, 1),
2840 gimple_call_arg (stmt
, 2),
2841 gimple_call_arg (stmt
, 3),
2843 case BUILT_IN_SNPRINTF_CHK
:
2844 case BUILT_IN_VSNPRINTF_CHK
:
2845 return gimple_fold_builtin_snprintf_chk (gsi
, fcode
);
2846 case BUILT_IN_SNPRINTF
:
2847 return gimple_fold_builtin_snprintf (gsi
);
2848 case BUILT_IN_SPRINTF
:
2849 return gimple_fold_builtin_sprintf (gsi
);
2850 case BUILT_IN_FPRINTF
:
2851 case BUILT_IN_FPRINTF_UNLOCKED
:
2852 case BUILT_IN_VFPRINTF
:
2853 if (n
== 2 || n
== 3)
2854 return gimple_fold_builtin_fprintf (gsi
,
2855 gimple_call_arg (stmt
, 0),
2856 gimple_call_arg (stmt
, 1),
2858 ? gimple_call_arg (stmt
, 2)
2862 case BUILT_IN_FPRINTF_CHK
:
2863 case BUILT_IN_VFPRINTF_CHK
:
2864 if (n
== 3 || n
== 4)
2865 return gimple_fold_builtin_fprintf (gsi
,
2866 gimple_call_arg (stmt
, 0),
2867 gimple_call_arg (stmt
, 2),
2869 ? gimple_call_arg (stmt
, 3)
2873 case BUILT_IN_PRINTF
:
2874 case BUILT_IN_PRINTF_UNLOCKED
:
2875 case BUILT_IN_VPRINTF
:
2876 if (n
== 1 || n
== 2)
2877 return gimple_fold_builtin_printf (gsi
, gimple_call_arg (stmt
, 0),
2879 ? gimple_call_arg (stmt
, 1)
2880 : NULL_TREE
, fcode
);
2882 case BUILT_IN_PRINTF_CHK
:
2883 case BUILT_IN_VPRINTF_CHK
:
2884 if (n
== 2 || n
== 3)
2885 return gimple_fold_builtin_printf (gsi
, gimple_call_arg (stmt
, 1),
2887 ? gimple_call_arg (stmt
, 2)
2888 : NULL_TREE
, fcode
);
2892 /* Try the generic builtin folder. */
2893 bool ignore
= (gimple_call_lhs (stmt
) == NULL
);
2894 tree result
= fold_call_stmt (stmt
, ignore
);
2898 STRIP_NOPS (result
);
2900 result
= fold_convert (gimple_call_return_type (stmt
), result
);
2901 if (!update_call_from_tree (gsi
, result
))
2902 gimplify_and_update_call_from_tree (gsi
, result
);
2909 /* Return true if ARG0 CODE ARG1 in infinite signed precision operation
2910 doesn't fit into TYPE. The test for overflow should be regardless of
2911 -fwrapv, and even for unsigned types. */
2914 arith_overflowed_p (enum tree_code code
, const_tree type
,
2915 const_tree arg0
, const_tree arg1
)
2917 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
* 2) widest2_int
;
2918 typedef generic_wide_int
<wi::extended_tree
<WIDE_INT_MAX_PRECISION
* 2> >
2920 widest2_int warg0
= widest2_int_cst (arg0
);
2921 widest2_int warg1
= widest2_int_cst (arg1
);
2925 case PLUS_EXPR
: wres
= wi::add (warg0
, warg1
); break;
2926 case MINUS_EXPR
: wres
= wi::sub (warg0
, warg1
); break;
2927 case MULT_EXPR
: wres
= wi::mul (warg0
, warg1
); break;
2928 default: gcc_unreachable ();
2930 signop sign
= TYPE_SIGN (type
);
2931 if (sign
== UNSIGNED
&& wi::neg_p (wres
))
2933 return wi::min_precision (wres
, sign
) > TYPE_PRECISION (type
);
2936 /* Attempt to fold a call statement referenced by the statement iterator GSI.
2937 The statement may be replaced by another statement, e.g., if the call
2938 simplifies to a constant value. Return true if any changes were made.
2939 It is assumed that the operands have been previously folded. */
2942 gimple_fold_call (gimple_stmt_iterator
*gsi
, bool inplace
)
2944 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
2946 bool changed
= false;
2949 /* Fold *& in call arguments. */
2950 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
2951 if (REFERENCE_CLASS_P (gimple_call_arg (stmt
, i
)))
2953 tree tmp
= maybe_fold_reference (gimple_call_arg (stmt
, i
), false);
2956 gimple_call_set_arg (stmt
, i
, tmp
);
2961 /* Check for virtual calls that became direct calls. */
2962 callee
= gimple_call_fn (stmt
);
2963 if (callee
&& TREE_CODE (callee
) == OBJ_TYPE_REF
)
2965 if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee
)) != NULL_TREE
)
2967 if (dump_file
&& virtual_method_call_p (callee
)
2968 && !possible_polymorphic_call_target_p
2969 (callee
, stmt
, cgraph_node::get (gimple_call_addr_fndecl
2970 (OBJ_TYPE_REF_EXPR (callee
)))))
2973 "Type inheritance inconsistent devirtualization of ");
2974 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
2975 fprintf (dump_file
, " to ");
2976 print_generic_expr (dump_file
, callee
, TDF_SLIM
);
2977 fprintf (dump_file
, "\n");
2980 gimple_call_set_fn (stmt
, OBJ_TYPE_REF_EXPR (callee
));
2983 else if (flag_devirtualize
&& !inplace
&& virtual_method_call_p (callee
))
2986 vec
<cgraph_node
*>targets
2987 = possible_polymorphic_call_targets (callee
, stmt
, &final
);
2988 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
2990 tree lhs
= gimple_call_lhs (stmt
);
2991 if (dump_enabled_p ())
2993 location_t loc
= gimple_location_safe (stmt
);
2994 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, loc
,
2995 "folding virtual function call to %s\n",
2996 targets
.length () == 1
2997 ? targets
[0]->name ()
2998 : "__builtin_unreachable");
3000 if (targets
.length () == 1)
3002 gimple_call_set_fndecl (stmt
, targets
[0]->decl
);
3004 /* If the call becomes noreturn, remove the lhs. */
3005 if (lhs
&& (gimple_call_flags (stmt
) & ECF_NORETURN
))
3007 if (TREE_CODE (lhs
) == SSA_NAME
)
3009 tree var
= create_tmp_var (TREE_TYPE (lhs
));
3010 tree def
= get_or_create_ssa_default_def (cfun
, var
);
3011 gimple new_stmt
= gimple_build_assign (lhs
, def
);
3012 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
3014 gimple_call_set_lhs (stmt
, NULL_TREE
);
3016 maybe_remove_unused_call_args (cfun
, stmt
);
3020 tree fndecl
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
3021 gimple new_stmt
= gimple_build_call (fndecl
, 0);
3022 gimple_set_location (new_stmt
, gimple_location (stmt
));
3023 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
3025 tree var
= create_tmp_var (TREE_TYPE (lhs
));
3026 tree def
= get_or_create_ssa_default_def (cfun
, var
);
3028 /* To satisfy condition for
3029 cgraph_update_edges_for_call_stmt_node,
3030 we need to preserve GIMPLE_CALL statement
3031 at position of GSI iterator. */
3032 update_call_from_tree (gsi
, def
);
3033 gsi_insert_before (gsi
, new_stmt
, GSI_NEW_STMT
);
3037 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
3038 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
3039 gsi_replace (gsi
, new_stmt
, false);
3047 /* Check for indirect calls that became direct calls, and then
3048 no longer require a static chain. */
3049 if (gimple_call_chain (stmt
))
3051 tree fn
= gimple_call_fndecl (stmt
);
3052 if (fn
&& !DECL_STATIC_CHAIN (fn
))
3054 gimple_call_set_chain (stmt
, NULL
);
3059 tree tmp
= maybe_fold_reference (gimple_call_chain (stmt
), false);
3062 gimple_call_set_chain (stmt
, tmp
);
3071 /* Check for builtins that CCP can handle using information not
3072 available in the generic fold routines. */
3073 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
3075 if (gimple_fold_builtin (gsi
))
3078 else if (gimple_call_builtin_p (stmt
, BUILT_IN_MD
))
3080 changed
|= targetm
.gimple_fold_builtin (gsi
);
3082 else if (gimple_call_internal_p (stmt
))
3084 enum tree_code subcode
= ERROR_MARK
;
3085 tree result
= NULL_TREE
;
3086 bool cplx_result
= false;
3087 tree overflow
= NULL_TREE
;
3088 switch (gimple_call_internal_fn (stmt
))
3090 case IFN_BUILTIN_EXPECT
:
3091 result
= fold_builtin_expect (gimple_location (stmt
),
3092 gimple_call_arg (stmt
, 0),
3093 gimple_call_arg (stmt
, 1),
3094 gimple_call_arg (stmt
, 2));
3096 case IFN_UBSAN_OBJECT_SIZE
:
3097 if (integer_all_onesp (gimple_call_arg (stmt
, 2))
3098 || (TREE_CODE (gimple_call_arg (stmt
, 1)) == INTEGER_CST
3099 && TREE_CODE (gimple_call_arg (stmt
, 2)) == INTEGER_CST
3100 && tree_int_cst_le (gimple_call_arg (stmt
, 1),
3101 gimple_call_arg (stmt
, 2))))
3103 gsi_replace (gsi
, gimple_build_nop (), true);
3104 unlink_stmt_vdef (stmt
);
3105 release_defs (stmt
);
3109 case IFN_UBSAN_CHECK_ADD
:
3110 subcode
= PLUS_EXPR
;
3112 case IFN_UBSAN_CHECK_SUB
:
3113 subcode
= MINUS_EXPR
;
3115 case IFN_UBSAN_CHECK_MUL
:
3116 subcode
= MULT_EXPR
;
3118 case IFN_ADD_OVERFLOW
:
3119 subcode
= PLUS_EXPR
;
3122 case IFN_SUB_OVERFLOW
:
3123 subcode
= MINUS_EXPR
;
3126 case IFN_MUL_OVERFLOW
:
3127 subcode
= MULT_EXPR
;
3133 if (subcode
!= ERROR_MARK
)
3135 tree arg0
= gimple_call_arg (stmt
, 0);
3136 tree arg1
= gimple_call_arg (stmt
, 1);
3137 tree type
= TREE_TYPE (arg0
);
3140 tree lhs
= gimple_call_lhs (stmt
);
3141 if (lhs
== NULL_TREE
)
3144 type
= TREE_TYPE (TREE_TYPE (lhs
));
3146 if (type
== NULL_TREE
)
3148 /* x = y + 0; x = y - 0; x = y * 0; */
3149 else if (integer_zerop (arg1
))
3150 result
= subcode
== MULT_EXPR
? integer_zero_node
: arg0
;
3151 /* x = 0 + y; x = 0 * y; */
3152 else if (subcode
!= MINUS_EXPR
&& integer_zerop (arg0
))
3153 result
= subcode
== MULT_EXPR
? integer_zero_node
: arg1
;
3155 else if (subcode
== MINUS_EXPR
&& operand_equal_p (arg0
, arg1
, 0))
3156 result
= integer_zero_node
;
3157 /* x = y * 1; x = 1 * y; */
3158 else if (subcode
== MULT_EXPR
&& integer_onep (arg1
))
3160 else if (subcode
== MULT_EXPR
&& integer_onep (arg0
))
3162 else if (TREE_CODE (arg0
) == INTEGER_CST
3163 && TREE_CODE (arg1
) == INTEGER_CST
)
3166 result
= int_const_binop (subcode
, fold_convert (type
, arg0
),
3167 fold_convert (type
, arg1
));
3169 result
= int_const_binop (subcode
, arg0
, arg1
);
3170 if (result
&& arith_overflowed_p (subcode
, type
, arg0
, arg1
))
3173 overflow
= build_one_cst (type
);
3180 if (result
== integer_zero_node
)
3181 result
= build_zero_cst (type
);
3182 else if (cplx_result
&& TREE_TYPE (result
) != type
)
3184 if (TREE_CODE (result
) == INTEGER_CST
)
3186 if (arith_overflowed_p (PLUS_EXPR
, type
, result
,
3188 overflow
= build_one_cst (type
);
3190 else if ((!TYPE_UNSIGNED (TREE_TYPE (result
))
3191 && TYPE_UNSIGNED (type
))
3192 || (TYPE_PRECISION (type
)
3193 < (TYPE_PRECISION (TREE_TYPE (result
))
3194 + (TYPE_UNSIGNED (TREE_TYPE (result
))
3195 && !TYPE_UNSIGNED (type
)))))
3198 result
= fold_convert (type
, result
);
3205 if (TREE_CODE (result
) == INTEGER_CST
&& TREE_OVERFLOW (result
))
3206 result
= drop_tree_overflow (result
);
3209 if (overflow
== NULL_TREE
)
3210 overflow
= build_zero_cst (TREE_TYPE (result
));
3211 tree ctype
= build_complex_type (TREE_TYPE (result
));
3212 if (TREE_CODE (result
) == INTEGER_CST
3213 && TREE_CODE (overflow
) == INTEGER_CST
)
3214 result
= build_complex (ctype
, result
, overflow
);
3216 result
= build2_loc (gimple_location (stmt
), COMPLEX_EXPR
,
3217 ctype
, result
, overflow
);
3219 if (!update_call_from_tree (gsi
, result
))
3220 gimplify_and_update_call_from_tree (gsi
, result
);
3229 /* Return true whether NAME has a use on STMT. */
3232 has_use_on_stmt (tree name
, gimple stmt
)
3234 imm_use_iterator iter
;
3235 use_operand_p use_p
;
3236 FOR_EACH_IMM_USE_FAST (use_p
, iter
, name
)
3237 if (USE_STMT (use_p
) == stmt
)
3242 /* Worker for fold_stmt_1 dispatch to pattern based folding with
3245 Replaces *GSI with the simplification result in RCODE and OPS
3246 and the associated statements in *SEQ. Does the replacement
3247 according to INPLACE and returns true if the operation succeeded. */
3250 replace_stmt_with_simplification (gimple_stmt_iterator
*gsi
,
3251 code_helper rcode
, tree
*ops
,
3252 gimple_seq
*seq
, bool inplace
)
3254 gimple stmt
= gsi_stmt (*gsi
);
3256 /* Play safe and do not allow abnormals to be mentioned in
3257 newly created statements. See also maybe_push_res_to_seq.
3258 As an exception allow such uses if there was a use of the
3259 same SSA name on the old stmt. */
3260 if ((TREE_CODE (ops
[0]) == SSA_NAME
3261 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[0])
3262 && !has_use_on_stmt (ops
[0], stmt
))
3264 && TREE_CODE (ops
[1]) == SSA_NAME
3265 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[1])
3266 && !has_use_on_stmt (ops
[1], stmt
))
3268 && TREE_CODE (ops
[2]) == SSA_NAME
3269 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[2])
3270 && !has_use_on_stmt (ops
[2], stmt
)))
3273 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
3275 gcc_assert (rcode
.is_tree_code ());
3276 if (TREE_CODE_CLASS ((enum tree_code
)rcode
) == tcc_comparison
3277 /* GIMPLE_CONDs condition may not throw. */
3278 && (!flag_exceptions
3279 || !cfun
->can_throw_non_call_exceptions
3280 || !operation_could_trap_p (rcode
,
3281 FLOAT_TYPE_P (TREE_TYPE (ops
[0])),
3283 gimple_cond_set_condition (cond_stmt
, rcode
, ops
[0], ops
[1]);
3284 else if (rcode
== SSA_NAME
)
3285 gimple_cond_set_condition (cond_stmt
, NE_EXPR
, ops
[0],
3286 build_zero_cst (TREE_TYPE (ops
[0])));
3287 else if (rcode
== INTEGER_CST
)
3289 if (integer_zerop (ops
[0]))
3290 gimple_cond_make_false (cond_stmt
);
3292 gimple_cond_make_true (cond_stmt
);
3296 tree res
= maybe_push_res_to_seq (rcode
, boolean_type_node
,
3300 gimple_cond_set_condition (cond_stmt
, NE_EXPR
, res
,
3301 build_zero_cst (TREE_TYPE (res
)));
3305 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3307 fprintf (dump_file
, "gimple_simplified to ");
3308 if (!gimple_seq_empty_p (*seq
))
3309 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
3310 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
),
3313 gsi_insert_seq_before (gsi
, *seq
, GSI_SAME_STMT
);
3316 else if (is_gimple_assign (stmt
)
3317 && rcode
.is_tree_code ())
3320 || gimple_num_ops (stmt
) > get_gimple_rhs_num_ops (rcode
))
3322 maybe_build_generic_op (rcode
,
3323 TREE_TYPE (gimple_assign_lhs (stmt
)),
3324 &ops
[0], ops
[1], ops
[2]);
3325 gimple_assign_set_rhs_with_ops (gsi
, rcode
, ops
[0], ops
[1], ops
[2]);
3326 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3328 fprintf (dump_file
, "gimple_simplified to ");
3329 if (!gimple_seq_empty_p (*seq
))
3330 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
3331 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
),
3334 gsi_insert_seq_before (gsi
, *seq
, GSI_SAME_STMT
);
3338 else if (rcode
.is_fn_code ()
3339 && gimple_call_builtin_p (stmt
, rcode
))
3342 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
3344 gcc_assert (ops
[i
] != NULL_TREE
);
3345 gimple_call_set_arg (stmt
, i
, ops
[i
]);
3348 gcc_assert (ops
[i
] == NULL_TREE
);
3353 if (gimple_has_lhs (stmt
))
3355 tree lhs
= gimple_get_lhs (stmt
);
3356 if (!maybe_push_res_to_seq (rcode
, TREE_TYPE (lhs
),
3359 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3361 fprintf (dump_file
, "gimple_simplified to ");
3362 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
3364 gsi_replace_with_seq_vops (gsi
, *seq
);
3374 /* Canonicalize MEM_REFs invariant address operand after propagation. */
3377 maybe_canonicalize_mem_ref_addr (tree
*t
)
3381 if (TREE_CODE (*t
) == ADDR_EXPR
)
3382 t
= &TREE_OPERAND (*t
, 0);
3384 while (handled_component_p (*t
))
3385 t
= &TREE_OPERAND (*t
, 0);
3387 /* Canonicalize MEM [&foo.bar, 0] which appears after propagating
3388 of invariant addresses into a SSA name MEM_REF address. */
3389 if (TREE_CODE (*t
) == MEM_REF
3390 || TREE_CODE (*t
) == TARGET_MEM_REF
)
3392 tree addr
= TREE_OPERAND (*t
, 0);
3393 if (TREE_CODE (addr
) == ADDR_EXPR
3394 && (TREE_CODE (TREE_OPERAND (addr
, 0)) == MEM_REF
3395 || handled_component_p (TREE_OPERAND (addr
, 0))))
3398 HOST_WIDE_INT coffset
;
3399 base
= get_addr_base_and_unit_offset (TREE_OPERAND (addr
, 0),
3404 TREE_OPERAND (*t
, 0) = build_fold_addr_expr (base
);
3405 TREE_OPERAND (*t
, 1) = int_const_binop (PLUS_EXPR
,
3406 TREE_OPERAND (*t
, 1),
3407 size_int (coffset
));
3410 gcc_checking_assert (TREE_CODE (TREE_OPERAND (*t
, 0)) == DEBUG_EXPR_DECL
3411 || is_gimple_mem_ref_addr (TREE_OPERAND (*t
, 0)));
3414 /* Canonicalize back MEM_REFs to plain reference trees if the object
3415 accessed is a decl that has the same access semantics as the MEM_REF. */
3416 if (TREE_CODE (*t
) == MEM_REF
3417 && TREE_CODE (TREE_OPERAND (*t
, 0)) == ADDR_EXPR
3418 && integer_zerop (TREE_OPERAND (*t
, 1))
3419 && MR_DEPENDENCE_CLIQUE (*t
) == 0)
3421 tree decl
= TREE_OPERAND (TREE_OPERAND (*t
, 0), 0);
3422 tree alias_type
= TREE_TYPE (TREE_OPERAND (*t
, 1));
3423 if (/* Same volatile qualification. */
3424 TREE_THIS_VOLATILE (*t
) == TREE_THIS_VOLATILE (decl
)
3425 /* Same TBAA behavior with -fstrict-aliasing. */
3426 && !TYPE_REF_CAN_ALIAS_ALL (alias_type
)
3427 && (TYPE_MAIN_VARIANT (TREE_TYPE (decl
))
3428 == TYPE_MAIN_VARIANT (TREE_TYPE (alias_type
)))
3429 /* Same alignment. */
3430 && TYPE_ALIGN (TREE_TYPE (decl
)) == TYPE_ALIGN (TREE_TYPE (*t
))
3431 /* We have to look out here to not drop a required conversion
3432 from the rhs to the lhs if *t appears on the lhs or vice-versa
3433 if it appears on the rhs. Thus require strict type
3435 && types_compatible_p (TREE_TYPE (*t
), TREE_TYPE (decl
)))
3437 *t
= TREE_OPERAND (TREE_OPERAND (*t
, 0), 0);
3442 /* Canonicalize TARGET_MEM_REF in particular with respect to
3443 the indexes becoming constant. */
3444 else if (TREE_CODE (*t
) == TARGET_MEM_REF
)
3446 tree tem
= maybe_fold_tmr (*t
);
3457 /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument
3458 distinguishes both cases. */
3461 fold_stmt_1 (gimple_stmt_iterator
*gsi
, bool inplace
, tree (*valueize
) (tree
))
3463 bool changed
= false;
3464 gimple stmt
= gsi_stmt (*gsi
);
3467 /* First do required canonicalization of [TARGET_]MEM_REF addresses
3469 ??? This shouldn't be done in generic folding but in the
3470 propagation helpers which also know whether an address was
3472 Also canonicalize operand order. */
3473 switch (gimple_code (stmt
))
3476 if (gimple_assign_rhs_class (stmt
) == GIMPLE_SINGLE_RHS
)
3478 tree
*rhs
= gimple_assign_rhs1_ptr (stmt
);
3479 if ((REFERENCE_CLASS_P (*rhs
)
3480 || TREE_CODE (*rhs
) == ADDR_EXPR
)
3481 && maybe_canonicalize_mem_ref_addr (rhs
))
3483 tree
*lhs
= gimple_assign_lhs_ptr (stmt
);
3484 if (REFERENCE_CLASS_P (*lhs
)
3485 && maybe_canonicalize_mem_ref_addr (lhs
))
3490 /* Canonicalize operand order. */
3491 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3492 if (TREE_CODE_CLASS (code
) == tcc_comparison
3493 || commutative_tree_code (code
)
3494 || commutative_ternary_tree_code (code
))
3496 tree rhs1
= gimple_assign_rhs1 (stmt
);
3497 tree rhs2
= gimple_assign_rhs2 (stmt
);
3498 if (tree_swap_operands_p (rhs1
, rhs2
, false))
3500 gimple_assign_set_rhs1 (stmt
, rhs2
);
3501 gimple_assign_set_rhs2 (stmt
, rhs1
);
3502 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3503 gimple_assign_set_rhs_code (stmt
,
3504 swap_tree_comparison (code
));
3512 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
3514 tree
*arg
= gimple_call_arg_ptr (stmt
, i
);
3515 if (REFERENCE_CLASS_P (*arg
)
3516 && maybe_canonicalize_mem_ref_addr (arg
))
3519 tree
*lhs
= gimple_call_lhs_ptr (stmt
);
3521 && REFERENCE_CLASS_P (*lhs
)
3522 && maybe_canonicalize_mem_ref_addr (lhs
))
3528 gasm
*asm_stmt
= as_a
<gasm
*> (stmt
);
3529 for (i
= 0; i
< gimple_asm_noutputs (asm_stmt
); ++i
)
3531 tree link
= gimple_asm_output_op (asm_stmt
, i
);
3532 tree op
= TREE_VALUE (link
);
3533 if (REFERENCE_CLASS_P (op
)
3534 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link
)))
3537 for (i
= 0; i
< gimple_asm_ninputs (asm_stmt
); ++i
)
3539 tree link
= gimple_asm_input_op (asm_stmt
, i
);
3540 tree op
= TREE_VALUE (link
);
3541 if ((REFERENCE_CLASS_P (op
)
3542 || TREE_CODE (op
) == ADDR_EXPR
)
3543 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link
)))
3549 if (gimple_debug_bind_p (stmt
))
3551 tree
*val
= gimple_debug_bind_get_value_ptr (stmt
);
3553 && (REFERENCE_CLASS_P (*val
)
3554 || TREE_CODE (*val
) == ADDR_EXPR
)
3555 && maybe_canonicalize_mem_ref_addr (val
))
3561 /* Canonicalize operand order. */
3562 tree lhs
= gimple_cond_lhs (stmt
);
3563 tree rhs
= gimple_cond_rhs (stmt
);
3564 if (tree_swap_operands_p (lhs
, rhs
, false))
3566 gcond
*gc
= as_a
<gcond
*> (stmt
);
3567 gimple_cond_set_lhs (gc
, rhs
);
3568 gimple_cond_set_rhs (gc
, lhs
);
3569 gimple_cond_set_code (gc
,
3570 swap_tree_comparison (gimple_cond_code (gc
)));
3577 /* Dispatch to pattern-based folding. */
3579 || is_gimple_assign (stmt
)
3580 || gimple_code (stmt
) == GIMPLE_COND
)
3582 gimple_seq seq
= NULL
;
3585 if (gimple_simplify (stmt
, &rcode
, ops
, inplace
? NULL
: &seq
,
3586 valueize
, valueize
))
3588 if (replace_stmt_with_simplification (gsi
, rcode
, ops
, &seq
, inplace
))
3591 gimple_seq_discard (seq
);
3595 stmt
= gsi_stmt (*gsi
);
3597 /* Fold the main computation performed by the statement. */
3598 switch (gimple_code (stmt
))
3602 /* Try to canonicalize for boolean-typed X the comparisons
3603 X == 0, X == 1, X != 0, and X != 1. */
3604 if (gimple_assign_rhs_code (stmt
) == EQ_EXPR
3605 || gimple_assign_rhs_code (stmt
) == NE_EXPR
)
3607 tree lhs
= gimple_assign_lhs (stmt
);
3608 tree op1
= gimple_assign_rhs1 (stmt
);
3609 tree op2
= gimple_assign_rhs2 (stmt
);
3610 tree type
= TREE_TYPE (op1
);
3612 /* Check whether the comparison operands are of the same boolean
3613 type as the result type is.
3614 Check that second operand is an integer-constant with value
3616 if (TREE_CODE (op2
) == INTEGER_CST
3617 && (integer_zerop (op2
) || integer_onep (op2
))
3618 && useless_type_conversion_p (TREE_TYPE (lhs
), type
))
3620 enum tree_code cmp_code
= gimple_assign_rhs_code (stmt
);
3621 bool is_logical_not
= false;
3623 /* X == 0 and X != 1 is a logical-not.of X
3624 X == 1 and X != 0 is X */
3625 if ((cmp_code
== EQ_EXPR
&& integer_zerop (op2
))
3626 || (cmp_code
== NE_EXPR
&& integer_onep (op2
)))
3627 is_logical_not
= true;
3629 if (is_logical_not
== false)
3630 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op1
), op1
);
3631 /* Only for one-bit precision typed X the transformation
3632 !X -> ~X is valied. */
3633 else if (TYPE_PRECISION (type
) == 1)
3634 gimple_assign_set_rhs_with_ops (gsi
, BIT_NOT_EXPR
, op1
);
3635 /* Otherwise we use !X -> X ^ 1. */
3637 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op1
,
3638 build_int_cst (type
, 1));
3644 unsigned old_num_ops
= gimple_num_ops (stmt
);
3645 tree lhs
= gimple_assign_lhs (stmt
);
3646 tree new_rhs
= fold_gimple_assign (gsi
);
3648 && !useless_type_conversion_p (TREE_TYPE (lhs
),
3649 TREE_TYPE (new_rhs
)))
3650 new_rhs
= fold_convert (TREE_TYPE (lhs
), new_rhs
);
3653 || get_gimple_rhs_num_ops (TREE_CODE (new_rhs
)) < old_num_ops
))
3655 gimple_assign_set_rhs_from_tree (gsi
, new_rhs
);
3662 changed
|= gimple_fold_call (gsi
, inplace
);
3666 /* Fold *& in asm operands. */
3668 gasm
*asm_stmt
= as_a
<gasm
*> (stmt
);
3670 const char **oconstraints
;
3671 const char *constraint
;
3672 bool allows_mem
, allows_reg
;
3674 noutputs
= gimple_asm_noutputs (asm_stmt
);
3675 oconstraints
= XALLOCAVEC (const char *, noutputs
);
3677 for (i
= 0; i
< gimple_asm_noutputs (asm_stmt
); ++i
)
3679 tree link
= gimple_asm_output_op (asm_stmt
, i
);
3680 tree op
= TREE_VALUE (link
);
3682 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
3683 if (REFERENCE_CLASS_P (op
)
3684 && (op
= maybe_fold_reference (op
, true)) != NULL_TREE
)
3686 TREE_VALUE (link
) = op
;
3690 for (i
= 0; i
< gimple_asm_ninputs (asm_stmt
); ++i
)
3692 tree link
= gimple_asm_input_op (asm_stmt
, i
);
3693 tree op
= TREE_VALUE (link
);
3695 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
3696 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0,
3697 oconstraints
, &allows_mem
, &allows_reg
);
3698 if (REFERENCE_CLASS_P (op
)
3699 && (op
= maybe_fold_reference (op
, !allows_reg
&& allows_mem
))
3702 TREE_VALUE (link
) = op
;
3710 if (gimple_debug_bind_p (stmt
))
3712 tree val
= gimple_debug_bind_get_value (stmt
);
3714 && REFERENCE_CLASS_P (val
))
3716 tree tem
= maybe_fold_reference (val
, false);
3719 gimple_debug_bind_set_value (stmt
, tem
);
3724 && TREE_CODE (val
) == ADDR_EXPR
)
3726 tree ref
= TREE_OPERAND (val
, 0);
3727 tree tem
= maybe_fold_reference (ref
, false);
3730 tem
= build_fold_addr_expr_with_type (tem
, TREE_TYPE (val
));
3731 gimple_debug_bind_set_value (stmt
, tem
);
3741 stmt
= gsi_stmt (*gsi
);
3743 /* Fold *& on the lhs. */
3744 if (gimple_has_lhs (stmt
))
3746 tree lhs
= gimple_get_lhs (stmt
);
3747 if (lhs
&& REFERENCE_CLASS_P (lhs
))
3749 tree new_lhs
= maybe_fold_reference (lhs
, true);
3752 gimple_set_lhs (stmt
, new_lhs
);
3761 /* Valueziation callback that ends up not following SSA edges. */
3764 no_follow_ssa_edges (tree
)
3769 /* Valueization callback that ends up following single-use SSA edges only. */
3772 follow_single_use_edges (tree val
)
3774 if (TREE_CODE (val
) == SSA_NAME
3775 && !has_single_use (val
))
3780 /* Fold the statement pointed to by GSI. In some cases, this function may
3781 replace the whole statement with a new one. Returns true iff folding
3783 The statement pointed to by GSI should be in valid gimple form but may
3784 be in unfolded state as resulting from for example constant propagation
3785 which can produce *&x = 0. */
3788 fold_stmt (gimple_stmt_iterator
*gsi
)
3790 return fold_stmt_1 (gsi
, false, no_follow_ssa_edges
);
3794 fold_stmt (gimple_stmt_iterator
*gsi
, tree (*valueize
) (tree
))
3796 return fold_stmt_1 (gsi
, false, valueize
);
3799 /* Perform the minimal folding on statement *GSI. Only operations like
3800 *&x created by constant propagation are handled. The statement cannot
3801 be replaced with a new one. Return true if the statement was
3802 changed, false otherwise.
3803 The statement *GSI should be in valid gimple form but may
3804 be in unfolded state as resulting from for example constant propagation
3805 which can produce *&x = 0. */
3808 fold_stmt_inplace (gimple_stmt_iterator
*gsi
)
3810 gimple stmt
= gsi_stmt (*gsi
);
3811 bool changed
= fold_stmt_1 (gsi
, true, no_follow_ssa_edges
);
3812 gcc_assert (gsi_stmt (*gsi
) == stmt
);
3816 /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
3817 if EXPR is null or we don't know how.
3818 If non-null, the result always has boolean type. */
3821 canonicalize_bool (tree expr
, bool invert
)
3827 if (integer_nonzerop (expr
))
3828 return boolean_false_node
;
3829 else if (integer_zerop (expr
))
3830 return boolean_true_node
;
3831 else if (TREE_CODE (expr
) == SSA_NAME
)
3832 return fold_build2 (EQ_EXPR
, boolean_type_node
, expr
,
3833 build_int_cst (TREE_TYPE (expr
), 0));
3834 else if (COMPARISON_CLASS_P (expr
))
3835 return fold_build2 (invert_tree_comparison (TREE_CODE (expr
), false),
3837 TREE_OPERAND (expr
, 0),
3838 TREE_OPERAND (expr
, 1));
3844 if (TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
)
3846 if (integer_nonzerop (expr
))
3847 return boolean_true_node
;
3848 else if (integer_zerop (expr
))
3849 return boolean_false_node
;
3850 else if (TREE_CODE (expr
) == SSA_NAME
)
3851 return fold_build2 (NE_EXPR
, boolean_type_node
, expr
,
3852 build_int_cst (TREE_TYPE (expr
), 0));
3853 else if (COMPARISON_CLASS_P (expr
))
3854 return fold_build2 (TREE_CODE (expr
),
3856 TREE_OPERAND (expr
, 0),
3857 TREE_OPERAND (expr
, 1));
3863 /* Check to see if a boolean expression EXPR is logically equivalent to the
3864 comparison (OP1 CODE OP2). Check for various identities involving
3868 same_bool_comparison_p (const_tree expr
, enum tree_code code
,
3869 const_tree op1
, const_tree op2
)
3873 /* The obvious case. */
3874 if (TREE_CODE (expr
) == code
3875 && operand_equal_p (TREE_OPERAND (expr
, 0), op1
, 0)
3876 && operand_equal_p (TREE_OPERAND (expr
, 1), op2
, 0))
3879 /* Check for comparing (name, name != 0) and the case where expr
3880 is an SSA_NAME with a definition matching the comparison. */
3881 if (TREE_CODE (expr
) == SSA_NAME
3882 && TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
)
3884 if (operand_equal_p (expr
, op1
, 0))
3885 return ((code
== NE_EXPR
&& integer_zerop (op2
))
3886 || (code
== EQ_EXPR
&& integer_nonzerop (op2
)));
3887 s
= SSA_NAME_DEF_STMT (expr
);
3888 if (is_gimple_assign (s
)
3889 && gimple_assign_rhs_code (s
) == code
3890 && operand_equal_p (gimple_assign_rhs1 (s
), op1
, 0)
3891 && operand_equal_p (gimple_assign_rhs2 (s
), op2
, 0))
3895 /* If op1 is of the form (name != 0) or (name == 0), and the definition
3896 of name is a comparison, recurse. */
3897 if (TREE_CODE (op1
) == SSA_NAME
3898 && TREE_CODE (TREE_TYPE (op1
)) == BOOLEAN_TYPE
)
3900 s
= SSA_NAME_DEF_STMT (op1
);
3901 if (is_gimple_assign (s
)
3902 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
)
3904 enum tree_code c
= gimple_assign_rhs_code (s
);
3905 if ((c
== NE_EXPR
&& integer_zerop (op2
))
3906 || (c
== EQ_EXPR
&& integer_nonzerop (op2
)))
3907 return same_bool_comparison_p (expr
, c
,
3908 gimple_assign_rhs1 (s
),
3909 gimple_assign_rhs2 (s
));
3910 if ((c
== EQ_EXPR
&& integer_zerop (op2
))
3911 || (c
== NE_EXPR
&& integer_nonzerop (op2
)))
3912 return same_bool_comparison_p (expr
,
3913 invert_tree_comparison (c
, false),
3914 gimple_assign_rhs1 (s
),
3915 gimple_assign_rhs2 (s
));
3921 /* Check to see if two boolean expressions OP1 and OP2 are logically
3925 same_bool_result_p (const_tree op1
, const_tree op2
)
3927 /* Simple cases first. */
3928 if (operand_equal_p (op1
, op2
, 0))
3931 /* Check the cases where at least one of the operands is a comparison.
3932 These are a bit smarter than operand_equal_p in that they apply some
3933 identifies on SSA_NAMEs. */
3934 if (COMPARISON_CLASS_P (op2
)
3935 && same_bool_comparison_p (op1
, TREE_CODE (op2
),
3936 TREE_OPERAND (op2
, 0),
3937 TREE_OPERAND (op2
, 1)))
3939 if (COMPARISON_CLASS_P (op1
)
3940 && same_bool_comparison_p (op2
, TREE_CODE (op1
),
3941 TREE_OPERAND (op1
, 0),
3942 TREE_OPERAND (op1
, 1)))
3949 /* Forward declarations for some mutually recursive functions. */
3952 and_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
3953 enum tree_code code2
, tree op2a
, tree op2b
);
3955 and_var_with_comparison (tree var
, bool invert
,
3956 enum tree_code code2
, tree op2a
, tree op2b
);
3958 and_var_with_comparison_1 (gimple stmt
,
3959 enum tree_code code2
, tree op2a
, tree op2b
);
3961 or_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
3962 enum tree_code code2
, tree op2a
, tree op2b
);
3964 or_var_with_comparison (tree var
, bool invert
,
3965 enum tree_code code2
, tree op2a
, tree op2b
);
3967 or_var_with_comparison_1 (gimple stmt
,
3968 enum tree_code code2
, tree op2a
, tree op2b
);
3970 /* Helper function for and_comparisons_1: try to simplify the AND of the
3971 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
3972 If INVERT is true, invert the value of the VAR before doing the AND.
3973 Return NULL_EXPR if we can't simplify this to a single expression. */
3976 and_var_with_comparison (tree var
, bool invert
,
3977 enum tree_code code2
, tree op2a
, tree op2b
)
3980 gimple stmt
= SSA_NAME_DEF_STMT (var
);
3982 /* We can only deal with variables whose definitions are assignments. */
3983 if (!is_gimple_assign (stmt
))
3986 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
3987 !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
3988 Then we only have to consider the simpler non-inverted cases. */
3990 t
= or_var_with_comparison_1 (stmt
,
3991 invert_tree_comparison (code2
, false),
3994 t
= and_var_with_comparison_1 (stmt
, code2
, op2a
, op2b
);
3995 return canonicalize_bool (t
, invert
);
3998 /* Try to simplify the AND of the ssa variable defined by the assignment
3999 STMT with the comparison specified by (OP2A CODE2 OP2B).
4000 Return NULL_EXPR if we can't simplify this to a single expression. */
4003 and_var_with_comparison_1 (gimple stmt
,
4004 enum tree_code code2
, tree op2a
, tree op2b
)
4006 tree var
= gimple_assign_lhs (stmt
);
4007 tree true_test_var
= NULL_TREE
;
4008 tree false_test_var
= NULL_TREE
;
4009 enum tree_code innercode
= gimple_assign_rhs_code (stmt
);
4011 /* Check for identities like (var AND (var == 0)) => false. */
4012 if (TREE_CODE (op2a
) == SSA_NAME
4013 && TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
)
4015 if ((code2
== NE_EXPR
&& integer_zerop (op2b
))
4016 || (code2
== EQ_EXPR
&& integer_nonzerop (op2b
)))
4018 true_test_var
= op2a
;
4019 if (var
== true_test_var
)
4022 else if ((code2
== EQ_EXPR
&& integer_zerop (op2b
))
4023 || (code2
== NE_EXPR
&& integer_nonzerop (op2b
)))
4025 false_test_var
= op2a
;
4026 if (var
== false_test_var
)
4027 return boolean_false_node
;
4031 /* If the definition is a comparison, recurse on it. */
4032 if (TREE_CODE_CLASS (innercode
) == tcc_comparison
)
4034 tree t
= and_comparisons_1 (innercode
,
4035 gimple_assign_rhs1 (stmt
),
4036 gimple_assign_rhs2 (stmt
),
4044 /* If the definition is an AND or OR expression, we may be able to
4045 simplify by reassociating. */
4046 if (TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
4047 && (innercode
== BIT_AND_EXPR
|| innercode
== BIT_IOR_EXPR
))
4049 tree inner1
= gimple_assign_rhs1 (stmt
);
4050 tree inner2
= gimple_assign_rhs2 (stmt
);
4053 tree partial
= NULL_TREE
;
4054 bool is_and
= (innercode
== BIT_AND_EXPR
);
4056 /* Check for boolean identities that don't require recursive examination
4058 inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
4059 inner1 AND (inner1 OR inner2) => inner1
4060 !inner1 AND (inner1 AND inner2) => false
4061 !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
4062 Likewise for similar cases involving inner2. */
4063 if (inner1
== true_test_var
)
4064 return (is_and
? var
: inner1
);
4065 else if (inner2
== true_test_var
)
4066 return (is_and
? var
: inner2
);
4067 else if (inner1
== false_test_var
)
4069 ? boolean_false_node
4070 : and_var_with_comparison (inner2
, false, code2
, op2a
, op2b
));
4071 else if (inner2
== false_test_var
)
4073 ? boolean_false_node
4074 : and_var_with_comparison (inner1
, false, code2
, op2a
, op2b
));
4076 /* Next, redistribute/reassociate the AND across the inner tests.
4077 Compute the first partial result, (inner1 AND (op2a code op2b)) */
4078 if (TREE_CODE (inner1
) == SSA_NAME
4079 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner1
))
4080 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
4081 && (t
= maybe_fold_and_comparisons (gimple_assign_rhs_code (s
),
4082 gimple_assign_rhs1 (s
),
4083 gimple_assign_rhs2 (s
),
4084 code2
, op2a
, op2b
)))
4086 /* Handle the AND case, where we are reassociating:
4087 (inner1 AND inner2) AND (op2a code2 op2b)
4089 If the partial result t is a constant, we win. Otherwise
4090 continue on to try reassociating with the other inner test. */
4093 if (integer_onep (t
))
4095 else if (integer_zerop (t
))
4096 return boolean_false_node
;
4099 /* Handle the OR case, where we are redistributing:
4100 (inner1 OR inner2) AND (op2a code2 op2b)
4101 => (t OR (inner2 AND (op2a code2 op2b))) */
4102 else if (integer_onep (t
))
4103 return boolean_true_node
;
4105 /* Save partial result for later. */
4109 /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
4110 if (TREE_CODE (inner2
) == SSA_NAME
4111 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner2
))
4112 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
4113 && (t
= maybe_fold_and_comparisons (gimple_assign_rhs_code (s
),
4114 gimple_assign_rhs1 (s
),
4115 gimple_assign_rhs2 (s
),
4116 code2
, op2a
, op2b
)))
4118 /* Handle the AND case, where we are reassociating:
4119 (inner1 AND inner2) AND (op2a code2 op2b)
4120 => (inner1 AND t) */
4123 if (integer_onep (t
))
4125 else if (integer_zerop (t
))
4126 return boolean_false_node
;
4127 /* If both are the same, we can apply the identity
4129 else if (partial
&& same_bool_result_p (t
, partial
))
4133 /* Handle the OR case. where we are redistributing:
4134 (inner1 OR inner2) AND (op2a code2 op2b)
4135 => (t OR (inner1 AND (op2a code2 op2b)))
4136 => (t OR partial) */
4139 if (integer_onep (t
))
4140 return boolean_true_node
;
4143 /* We already got a simplification for the other
4144 operand to the redistributed OR expression. The
4145 interesting case is when at least one is false.
4146 Or, if both are the same, we can apply the identity
4148 if (integer_zerop (partial
))
4150 else if (integer_zerop (t
))
4152 else if (same_bool_result_p (t
, partial
))
4161 /* Try to simplify the AND of two comparisons defined by
4162 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
4163 If this can be done without constructing an intermediate value,
4164 return the resulting tree; otherwise NULL_TREE is returned.
4165 This function is deliberately asymmetric as it recurses on SSA_DEFs
4166 in the first comparison but not the second. */
4169 and_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
4170 enum tree_code code2
, tree op2a
, tree op2b
)
4172 tree truth_type
= truth_type_for (TREE_TYPE (op1a
));
4174 /* First check for ((x CODE1 y) AND (x CODE2 y)). */
4175 if (operand_equal_p (op1a
, op2a
, 0)
4176 && operand_equal_p (op1b
, op2b
, 0))
4178 /* Result will be either NULL_TREE, or a combined comparison. */
4179 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
4180 TRUTH_ANDIF_EXPR
, code1
, code2
,
4181 truth_type
, op1a
, op1b
);
4186 /* Likewise the swapped case of the above. */
4187 if (operand_equal_p (op1a
, op2b
, 0)
4188 && operand_equal_p (op1b
, op2a
, 0))
4190 /* Result will be either NULL_TREE, or a combined comparison. */
4191 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
4192 TRUTH_ANDIF_EXPR
, code1
,
4193 swap_tree_comparison (code2
),
4194 truth_type
, op1a
, op1b
);
4199 /* If both comparisons are of the same value against constants, we might
4200 be able to merge them. */
4201 if (operand_equal_p (op1a
, op2a
, 0)
4202 && TREE_CODE (op1b
) == INTEGER_CST
4203 && TREE_CODE (op2b
) == INTEGER_CST
)
4205 int cmp
= tree_int_cst_compare (op1b
, op2b
);
4207 /* If we have (op1a == op1b), we should either be able to
4208 return that or FALSE, depending on whether the constant op1b
4209 also satisfies the other comparison against op2b. */
4210 if (code1
== EQ_EXPR
)
4216 case EQ_EXPR
: val
= (cmp
== 0); break;
4217 case NE_EXPR
: val
= (cmp
!= 0); break;
4218 case LT_EXPR
: val
= (cmp
< 0); break;
4219 case GT_EXPR
: val
= (cmp
> 0); break;
4220 case LE_EXPR
: val
= (cmp
<= 0); break;
4221 case GE_EXPR
: val
= (cmp
>= 0); break;
4222 default: done
= false;
4227 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
4229 return boolean_false_node
;
4232 /* Likewise if the second comparison is an == comparison. */
4233 else if (code2
== EQ_EXPR
)
4239 case EQ_EXPR
: val
= (cmp
== 0); break;
4240 case NE_EXPR
: val
= (cmp
!= 0); break;
4241 case LT_EXPR
: val
= (cmp
> 0); break;
4242 case GT_EXPR
: val
= (cmp
< 0); break;
4243 case LE_EXPR
: val
= (cmp
>= 0); break;
4244 case GE_EXPR
: val
= (cmp
<= 0); break;
4245 default: done
= false;
4250 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
4252 return boolean_false_node
;
4256 /* Same business with inequality tests. */
4257 else if (code1
== NE_EXPR
)
4262 case EQ_EXPR
: val
= (cmp
!= 0); break;
4263 case NE_EXPR
: val
= (cmp
== 0); break;
4264 case LT_EXPR
: val
= (cmp
>= 0); break;
4265 case GT_EXPR
: val
= (cmp
<= 0); break;
4266 case LE_EXPR
: val
= (cmp
> 0); break;
4267 case GE_EXPR
: val
= (cmp
< 0); break;
4272 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
4274 else if (code2
== NE_EXPR
)
4279 case EQ_EXPR
: val
= (cmp
== 0); break;
4280 case NE_EXPR
: val
= (cmp
!= 0); break;
4281 case LT_EXPR
: val
= (cmp
<= 0); break;
4282 case GT_EXPR
: val
= (cmp
>= 0); break;
4283 case LE_EXPR
: val
= (cmp
< 0); break;
4284 case GE_EXPR
: val
= (cmp
> 0); break;
4289 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
4292 /* Chose the more restrictive of two < or <= comparisons. */
4293 else if ((code1
== LT_EXPR
|| code1
== LE_EXPR
)
4294 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
4296 if ((cmp
< 0) || (cmp
== 0 && code1
== LT_EXPR
))
4297 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
4299 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
4302 /* Likewise chose the more restrictive of two > or >= comparisons. */
4303 else if ((code1
== GT_EXPR
|| code1
== GE_EXPR
)
4304 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
4306 if ((cmp
> 0) || (cmp
== 0 && code1
== GT_EXPR
))
4307 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
4309 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
4312 /* Check for singleton ranges. */
4314 && ((code1
== LE_EXPR
&& code2
== GE_EXPR
)
4315 || (code1
== GE_EXPR
&& code2
== LE_EXPR
)))
4316 return fold_build2 (EQ_EXPR
, boolean_type_node
, op1a
, op2b
);
4318 /* Check for disjoint ranges. */
4320 && (code1
== LT_EXPR
|| code1
== LE_EXPR
)
4321 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
4322 return boolean_false_node
;
4324 && (code1
== GT_EXPR
|| code1
== GE_EXPR
)
4325 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
4326 return boolean_false_node
;
4329 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
4330 NAME's definition is a truth value. See if there are any simplifications
4331 that can be done against the NAME's definition. */
4332 if (TREE_CODE (op1a
) == SSA_NAME
4333 && (code1
== NE_EXPR
|| code1
== EQ_EXPR
)
4334 && (integer_zerop (op1b
) || integer_onep (op1b
)))
4336 bool invert
= ((code1
== EQ_EXPR
&& integer_zerop (op1b
))
4337 || (code1
== NE_EXPR
&& integer_onep (op1b
)));
4338 gimple stmt
= SSA_NAME_DEF_STMT (op1a
);
4339 switch (gimple_code (stmt
))
4342 /* Try to simplify by copy-propagating the definition. */
4343 return and_var_with_comparison (op1a
, invert
, code2
, op2a
, op2b
);
4346 /* If every argument to the PHI produces the same result when
4347 ANDed with the second comparison, we win.
4348 Do not do this unless the type is bool since we need a bool
4349 result here anyway. */
4350 if (TREE_CODE (TREE_TYPE (op1a
)) == BOOLEAN_TYPE
)
4352 tree result
= NULL_TREE
;
4354 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
4356 tree arg
= gimple_phi_arg_def (stmt
, i
);
4358 /* If this PHI has itself as an argument, ignore it.
4359 If all the other args produce the same result,
4361 if (arg
== gimple_phi_result (stmt
))
4363 else if (TREE_CODE (arg
) == INTEGER_CST
)
4365 if (invert
? integer_nonzerop (arg
) : integer_zerop (arg
))
4368 result
= boolean_false_node
;
4369 else if (!integer_zerop (result
))
4373 result
= fold_build2 (code2
, boolean_type_node
,
4375 else if (!same_bool_comparison_p (result
,
4379 else if (TREE_CODE (arg
) == SSA_NAME
4380 && !SSA_NAME_IS_DEFAULT_DEF (arg
))
4383 gimple def_stmt
= SSA_NAME_DEF_STMT (arg
);
4384 /* In simple cases we can look through PHI nodes,
4385 but we have to be careful with loops.
4387 if (! dom_info_available_p (CDI_DOMINATORS
)
4388 || gimple_bb (def_stmt
) == gimple_bb (stmt
)
4389 || dominated_by_p (CDI_DOMINATORS
,
4390 gimple_bb (def_stmt
),
4393 temp
= and_var_with_comparison (arg
, invert
, code2
,
4399 else if (!same_bool_result_p (result
, temp
))
4415 /* Try to simplify the AND of two comparisons, specified by
4416 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
4417 If this can be simplified to a single expression (without requiring
4418 introducing more SSA variables to hold intermediate values),
4419 return the resulting tree. Otherwise return NULL_TREE.
4420 If the result expression is non-null, it has boolean type. */
4423 maybe_fold_and_comparisons (enum tree_code code1
, tree op1a
, tree op1b
,
4424 enum tree_code code2
, tree op2a
, tree op2b
)
4426 tree t
= and_comparisons_1 (code1
, op1a
, op1b
, code2
, op2a
, op2b
);
4430 return and_comparisons_1 (code2
, op2a
, op2b
, code1
, op1a
, op1b
);
4433 /* Helper function for or_comparisons_1: try to simplify the OR of the
4434 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
4435 If INVERT is true, invert the value of VAR before doing the OR.
4436 Return NULL_EXPR if we can't simplify this to a single expression. */
4439 or_var_with_comparison (tree var
, bool invert
,
4440 enum tree_code code2
, tree op2a
, tree op2b
)
4443 gimple stmt
= SSA_NAME_DEF_STMT (var
);
4445 /* We can only deal with variables whose definitions are assignments. */
4446 if (!is_gimple_assign (stmt
))
4449 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
4450 !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
4451 Then we only have to consider the simpler non-inverted cases. */
4453 t
= and_var_with_comparison_1 (stmt
,
4454 invert_tree_comparison (code2
, false),
4457 t
= or_var_with_comparison_1 (stmt
, code2
, op2a
, op2b
);
4458 return canonicalize_bool (t
, invert
);
4461 /* Try to simplify the OR of the ssa variable defined by the assignment
4462 STMT with the comparison specified by (OP2A CODE2 OP2B).
4463 Return NULL_EXPR if we can't simplify this to a single expression. */
4466 or_var_with_comparison_1 (gimple stmt
,
4467 enum tree_code code2
, tree op2a
, tree op2b
)
4469 tree var
= gimple_assign_lhs (stmt
);
4470 tree true_test_var
= NULL_TREE
;
4471 tree false_test_var
= NULL_TREE
;
4472 enum tree_code innercode
= gimple_assign_rhs_code (stmt
);
4474 /* Check for identities like (var OR (var != 0)) => true . */
4475 if (TREE_CODE (op2a
) == SSA_NAME
4476 && TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
)
4478 if ((code2
== NE_EXPR
&& integer_zerop (op2b
))
4479 || (code2
== EQ_EXPR
&& integer_nonzerop (op2b
)))
4481 true_test_var
= op2a
;
4482 if (var
== true_test_var
)
4485 else if ((code2
== EQ_EXPR
&& integer_zerop (op2b
))
4486 || (code2
== NE_EXPR
&& integer_nonzerop (op2b
)))
4488 false_test_var
= op2a
;
4489 if (var
== false_test_var
)
4490 return boolean_true_node
;
4494 /* If the definition is a comparison, recurse on it. */
4495 if (TREE_CODE_CLASS (innercode
) == tcc_comparison
)
4497 tree t
= or_comparisons_1 (innercode
,
4498 gimple_assign_rhs1 (stmt
),
4499 gimple_assign_rhs2 (stmt
),
4507 /* If the definition is an AND or OR expression, we may be able to
4508 simplify by reassociating. */
4509 if (TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
4510 && (innercode
== BIT_AND_EXPR
|| innercode
== BIT_IOR_EXPR
))
4512 tree inner1
= gimple_assign_rhs1 (stmt
);
4513 tree inner2
= gimple_assign_rhs2 (stmt
);
4516 tree partial
= NULL_TREE
;
4517 bool is_or
= (innercode
== BIT_IOR_EXPR
);
4519 /* Check for boolean identities that don't require recursive examination
4521 inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
4522 inner1 OR (inner1 AND inner2) => inner1
4523 !inner1 OR (inner1 OR inner2) => true
4524 !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
4526 if (inner1
== true_test_var
)
4527 return (is_or
? var
: inner1
);
4528 else if (inner2
== true_test_var
)
4529 return (is_or
? var
: inner2
);
4530 else if (inner1
== false_test_var
)
4533 : or_var_with_comparison (inner2
, false, code2
, op2a
, op2b
));
4534 else if (inner2
== false_test_var
)
4537 : or_var_with_comparison (inner1
, false, code2
, op2a
, op2b
));
4539 /* Next, redistribute/reassociate the OR across the inner tests.
4540 Compute the first partial result, (inner1 OR (op2a code op2b)) */
4541 if (TREE_CODE (inner1
) == SSA_NAME
4542 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner1
))
4543 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
4544 && (t
= maybe_fold_or_comparisons (gimple_assign_rhs_code (s
),
4545 gimple_assign_rhs1 (s
),
4546 gimple_assign_rhs2 (s
),
4547 code2
, op2a
, op2b
)))
4549 /* Handle the OR case, where we are reassociating:
4550 (inner1 OR inner2) OR (op2a code2 op2b)
4552 If the partial result t is a constant, we win. Otherwise
4553 continue on to try reassociating with the other inner test. */
4556 if (integer_onep (t
))
4557 return boolean_true_node
;
4558 else if (integer_zerop (t
))
4562 /* Handle the AND case, where we are redistributing:
4563 (inner1 AND inner2) OR (op2a code2 op2b)
4564 => (t AND (inner2 OR (op2a code op2b))) */
4565 else if (integer_zerop (t
))
4566 return boolean_false_node
;
4568 /* Save partial result for later. */
4572 /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
4573 if (TREE_CODE (inner2
) == SSA_NAME
4574 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner2
))
4575 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
4576 && (t
= maybe_fold_or_comparisons (gimple_assign_rhs_code (s
),
4577 gimple_assign_rhs1 (s
),
4578 gimple_assign_rhs2 (s
),
4579 code2
, op2a
, op2b
)))
4581 /* Handle the OR case, where we are reassociating:
4582 (inner1 OR inner2) OR (op2a code2 op2b)
4584 => (t OR partial) */
4587 if (integer_zerop (t
))
4589 else if (integer_onep (t
))
4590 return boolean_true_node
;
4591 /* If both are the same, we can apply the identity
4593 else if (partial
&& same_bool_result_p (t
, partial
))
4597 /* Handle the AND case, where we are redistributing:
4598 (inner1 AND inner2) OR (op2a code2 op2b)
4599 => (t AND (inner1 OR (op2a code2 op2b)))
4600 => (t AND partial) */
4603 if (integer_zerop (t
))
4604 return boolean_false_node
;
4607 /* We already got a simplification for the other
4608 operand to the redistributed AND expression. The
4609 interesting case is when at least one is true.
4610 Or, if both are the same, we can apply the identity
4612 if (integer_onep (partial
))
4614 else if (integer_onep (t
))
4616 else if (same_bool_result_p (t
, partial
))
4625 /* Try to simplify the OR of two comparisons defined by
4626 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
4627 If this can be done without constructing an intermediate value,
4628 return the resulting tree; otherwise NULL_TREE is returned.
4629 This function is deliberately asymmetric as it recurses on SSA_DEFs
4630 in the first comparison but not the second. */
4633 or_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
4634 enum tree_code code2
, tree op2a
, tree op2b
)
4636 tree truth_type
= truth_type_for (TREE_TYPE (op1a
));
4638 /* First check for ((x CODE1 y) OR (x CODE2 y)). */
4639 if (operand_equal_p (op1a
, op2a
, 0)
4640 && operand_equal_p (op1b
, op2b
, 0))
4642 /* Result will be either NULL_TREE, or a combined comparison. */
4643 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
4644 TRUTH_ORIF_EXPR
, code1
, code2
,
4645 truth_type
, op1a
, op1b
);
4650 /* Likewise the swapped case of the above. */
4651 if (operand_equal_p (op1a
, op2b
, 0)
4652 && operand_equal_p (op1b
, op2a
, 0))
4654 /* Result will be either NULL_TREE, or a combined comparison. */
4655 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
4656 TRUTH_ORIF_EXPR
, code1
,
4657 swap_tree_comparison (code2
),
4658 truth_type
, op1a
, op1b
);
4663 /* If both comparisons are of the same value against constants, we might
4664 be able to merge them. */
4665 if (operand_equal_p (op1a
, op2a
, 0)
4666 && TREE_CODE (op1b
) == INTEGER_CST
4667 && TREE_CODE (op2b
) == INTEGER_CST
)
4669 int cmp
= tree_int_cst_compare (op1b
, op2b
);
4671 /* If we have (op1a != op1b), we should either be able to
4672 return that or TRUE, depending on whether the constant op1b
4673 also satisfies the other comparison against op2b. */
4674 if (code1
== NE_EXPR
)
4680 case EQ_EXPR
: val
= (cmp
== 0); break;
4681 case NE_EXPR
: val
= (cmp
!= 0); break;
4682 case LT_EXPR
: val
= (cmp
< 0); break;
4683 case GT_EXPR
: val
= (cmp
> 0); break;
4684 case LE_EXPR
: val
= (cmp
<= 0); break;
4685 case GE_EXPR
: val
= (cmp
>= 0); break;
4686 default: done
= false;
4691 return boolean_true_node
;
4693 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
4696 /* Likewise if the second comparison is a != comparison. */
4697 else if (code2
== NE_EXPR
)
4703 case EQ_EXPR
: val
= (cmp
== 0); break;
4704 case NE_EXPR
: val
= (cmp
!= 0); break;
4705 case LT_EXPR
: val
= (cmp
> 0); break;
4706 case GT_EXPR
: val
= (cmp
< 0); break;
4707 case LE_EXPR
: val
= (cmp
>= 0); break;
4708 case GE_EXPR
: val
= (cmp
<= 0); break;
4709 default: done
= false;
4714 return boolean_true_node
;
4716 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
4720 /* See if an equality test is redundant with the other comparison. */
4721 else if (code1
== EQ_EXPR
)
4726 case EQ_EXPR
: val
= (cmp
== 0); break;
4727 case NE_EXPR
: val
= (cmp
!= 0); break;
4728 case LT_EXPR
: val
= (cmp
< 0); break;
4729 case GT_EXPR
: val
= (cmp
> 0); break;
4730 case LE_EXPR
: val
= (cmp
<= 0); break;
4731 case GE_EXPR
: val
= (cmp
>= 0); break;
4736 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
4738 else if (code2
== EQ_EXPR
)
4743 case EQ_EXPR
: val
= (cmp
== 0); break;
4744 case NE_EXPR
: val
= (cmp
!= 0); break;
4745 case LT_EXPR
: val
= (cmp
> 0); break;
4746 case GT_EXPR
: val
= (cmp
< 0); break;
4747 case LE_EXPR
: val
= (cmp
>= 0); break;
4748 case GE_EXPR
: val
= (cmp
<= 0); break;
4753 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
4756 /* Chose the less restrictive of two < or <= comparisons. */
4757 else if ((code1
== LT_EXPR
|| code1
== LE_EXPR
)
4758 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
4760 if ((cmp
< 0) || (cmp
== 0 && code1
== LT_EXPR
))
4761 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
4763 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
4766 /* Likewise chose the less restrictive of two > or >= comparisons. */
4767 else if ((code1
== GT_EXPR
|| code1
== GE_EXPR
)
4768 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
4770 if ((cmp
> 0) || (cmp
== 0 && code1
== GT_EXPR
))
4771 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
4773 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
4776 /* Check for singleton ranges. */
4778 && ((code1
== LT_EXPR
&& code2
== GT_EXPR
)
4779 || (code1
== GT_EXPR
&& code2
== LT_EXPR
)))
4780 return fold_build2 (NE_EXPR
, boolean_type_node
, op1a
, op2b
);
4782 /* Check for less/greater pairs that don't restrict the range at all. */
4784 && (code1
== LT_EXPR
|| code1
== LE_EXPR
)
4785 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
4786 return boolean_true_node
;
4788 && (code1
== GT_EXPR
|| code1
== GE_EXPR
)
4789 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
4790 return boolean_true_node
;
4793 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
4794 NAME's definition is a truth value. See if there are any simplifications
4795 that can be done against the NAME's definition. */
4796 if (TREE_CODE (op1a
) == SSA_NAME
4797 && (code1
== NE_EXPR
|| code1
== EQ_EXPR
)
4798 && (integer_zerop (op1b
) || integer_onep (op1b
)))
4800 bool invert
= ((code1
== EQ_EXPR
&& integer_zerop (op1b
))
4801 || (code1
== NE_EXPR
&& integer_onep (op1b
)));
4802 gimple stmt
= SSA_NAME_DEF_STMT (op1a
);
4803 switch (gimple_code (stmt
))
4806 /* Try to simplify by copy-propagating the definition. */
4807 return or_var_with_comparison (op1a
, invert
, code2
, op2a
, op2b
);
4810 /* If every argument to the PHI produces the same result when
4811 ORed with the second comparison, we win.
4812 Do not do this unless the type is bool since we need a bool
4813 result here anyway. */
4814 if (TREE_CODE (TREE_TYPE (op1a
)) == BOOLEAN_TYPE
)
4816 tree result
= NULL_TREE
;
4818 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
4820 tree arg
= gimple_phi_arg_def (stmt
, i
);
4822 /* If this PHI has itself as an argument, ignore it.
4823 If all the other args produce the same result,
4825 if (arg
== gimple_phi_result (stmt
))
4827 else if (TREE_CODE (arg
) == INTEGER_CST
)
4829 if (invert
? integer_zerop (arg
) : integer_nonzerop (arg
))
4832 result
= boolean_true_node
;
4833 else if (!integer_onep (result
))
4837 result
= fold_build2 (code2
, boolean_type_node
,
4839 else if (!same_bool_comparison_p (result
,
4843 else if (TREE_CODE (arg
) == SSA_NAME
4844 && !SSA_NAME_IS_DEFAULT_DEF (arg
))
4847 gimple def_stmt
= SSA_NAME_DEF_STMT (arg
);
4848 /* In simple cases we can look through PHI nodes,
4849 but we have to be careful with loops.
4851 if (! dom_info_available_p (CDI_DOMINATORS
)
4852 || gimple_bb (def_stmt
) == gimple_bb (stmt
)
4853 || dominated_by_p (CDI_DOMINATORS
,
4854 gimple_bb (def_stmt
),
4857 temp
= or_var_with_comparison (arg
, invert
, code2
,
4863 else if (!same_bool_result_p (result
, temp
))
4879 /* Try to simplify the OR of two comparisons, specified by
4880 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
4881 If this can be simplified to a single expression (without requiring
4882 introducing more SSA variables to hold intermediate values),
4883 return the resulting tree. Otherwise return NULL_TREE.
4884 If the result expression is non-null, it has boolean type. */
4887 maybe_fold_or_comparisons (enum tree_code code1
, tree op1a
, tree op1b
,
4888 enum tree_code code2
, tree op2a
, tree op2b
)
4890 tree t
= or_comparisons_1 (code1
, op1a
, op1b
, code2
, op2a
, op2b
);
4894 return or_comparisons_1 (code2
, op2a
, op2b
, code1
, op1a
, op1b
);
4898 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
4900 Either NULL_TREE, a simplified but non-constant or a constant
4903 ??? This should go into a gimple-fold-inline.h file to be eventually
4904 privatized with the single valueize function used in the various TUs
4905 to avoid the indirect function call overhead. */
4908 gimple_fold_stmt_to_constant_1 (gimple stmt
, tree (*valueize
) (tree
),
4909 tree (*gvalueize
) (tree
))
4913 /* ??? The SSA propagators do not correctly deal with following SSA use-def
4914 edges if there are intermediate VARYING defs. For this reason
4915 do not follow SSA edges here even though SCCVN can technically
4916 just deal fine with that. */
4917 if (gimple_simplify (stmt
, &rcode
, ops
, NULL
, gvalueize
, valueize
)
4918 && rcode
.is_tree_code ()
4919 && (TREE_CODE_LENGTH ((tree_code
) rcode
) == 0
4920 || ((tree_code
) rcode
) == ADDR_EXPR
)
4921 && is_gimple_val (ops
[0]))
4924 if (dump_file
&& dump_flags
& TDF_DETAILS
)
4926 fprintf (dump_file
, "Match-and-simplified ");
4927 print_gimple_expr (dump_file
, stmt
, 0, TDF_SLIM
);
4928 fprintf (dump_file
, " to ");
4929 print_generic_expr (dump_file
, res
, 0);
4930 fprintf (dump_file
, "\n");
4935 location_t loc
= gimple_location (stmt
);
4936 switch (gimple_code (stmt
))
4940 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
4942 switch (get_gimple_rhs_class (subcode
))
4944 case GIMPLE_SINGLE_RHS
:
4946 tree rhs
= gimple_assign_rhs1 (stmt
);
4947 enum tree_code_class kind
= TREE_CODE_CLASS (subcode
);
4949 if (TREE_CODE (rhs
) == SSA_NAME
)
4951 /* If the RHS is an SSA_NAME, return its known constant value,
4953 return (*valueize
) (rhs
);
4955 /* Handle propagating invariant addresses into address
4957 else if (TREE_CODE (rhs
) == ADDR_EXPR
4958 && !is_gimple_min_invariant (rhs
))
4960 HOST_WIDE_INT offset
= 0;
4962 base
= get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs
, 0),
4966 && (CONSTANT_CLASS_P (base
)
4967 || decl_address_invariant_p (base
)))
4968 return build_invariant_address (TREE_TYPE (rhs
),
4971 else if (TREE_CODE (rhs
) == CONSTRUCTOR
4972 && TREE_CODE (TREE_TYPE (rhs
)) == VECTOR_TYPE
4973 && (CONSTRUCTOR_NELTS (rhs
)
4974 == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs
))))
4979 vec
= XALLOCAVEC (tree
,
4980 TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs
)));
4981 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs
), i
, val
)
4983 val
= (*valueize
) (val
);
4984 if (TREE_CODE (val
) == INTEGER_CST
4985 || TREE_CODE (val
) == REAL_CST
4986 || TREE_CODE (val
) == FIXED_CST
)
4992 return build_vector (TREE_TYPE (rhs
), vec
);
4994 if (subcode
== OBJ_TYPE_REF
)
4996 tree val
= (*valueize
) (OBJ_TYPE_REF_EXPR (rhs
));
4997 /* If callee is constant, we can fold away the wrapper. */
4998 if (is_gimple_min_invariant (val
))
5002 if (kind
== tcc_reference
)
5004 if ((TREE_CODE (rhs
) == VIEW_CONVERT_EXPR
5005 || TREE_CODE (rhs
) == REALPART_EXPR
5006 || TREE_CODE (rhs
) == IMAGPART_EXPR
)
5007 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
5009 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
5010 return fold_unary_loc (EXPR_LOCATION (rhs
),
5012 TREE_TYPE (rhs
), val
);
5014 else if (TREE_CODE (rhs
) == BIT_FIELD_REF
5015 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
5017 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
5018 return fold_ternary_loc (EXPR_LOCATION (rhs
),
5020 TREE_TYPE (rhs
), val
,
5021 TREE_OPERAND (rhs
, 1),
5022 TREE_OPERAND (rhs
, 2));
5024 else if (TREE_CODE (rhs
) == MEM_REF
5025 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
5027 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
5028 if (TREE_CODE (val
) == ADDR_EXPR
5029 && is_gimple_min_invariant (val
))
5031 tree tem
= fold_build2 (MEM_REF
, TREE_TYPE (rhs
),
5033 TREE_OPERAND (rhs
, 1));
5038 return fold_const_aggregate_ref_1 (rhs
, valueize
);
5040 else if (kind
== tcc_declaration
)
5041 return get_symbol_constant_value (rhs
);
5045 case GIMPLE_UNARY_RHS
:
5048 case GIMPLE_BINARY_RHS
:
5050 /* Handle binary operators that can appear in GIMPLE form. */
5051 tree op0
= (*valueize
) (gimple_assign_rhs1 (stmt
));
5052 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
5054 /* Translate &x + CST into an invariant form suitable for
5055 further propagation. */
5056 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
5057 && TREE_CODE (op0
) == ADDR_EXPR
5058 && TREE_CODE (op1
) == INTEGER_CST
)
5060 tree off
= fold_convert (ptr_type_node
, op1
);
5061 return build_fold_addr_expr_loc
5063 fold_build2 (MEM_REF
,
5064 TREE_TYPE (TREE_TYPE (op0
)),
5065 unshare_expr (op0
), off
));
5068 return fold_binary_loc (loc
, subcode
,
5069 gimple_expr_type (stmt
), op0
, op1
);
5072 case GIMPLE_TERNARY_RHS
:
5074 /* Handle ternary operators that can appear in GIMPLE form. */
5075 tree op0
= (*valueize
) (gimple_assign_rhs1 (stmt
));
5076 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
5077 tree op2
= (*valueize
) (gimple_assign_rhs3 (stmt
));
5079 /* Fold embedded expressions in ternary codes. */
5080 if ((subcode
== COND_EXPR
5081 || subcode
== VEC_COND_EXPR
)
5082 && COMPARISON_CLASS_P (op0
))
5084 tree op00
= (*valueize
) (TREE_OPERAND (op0
, 0));
5085 tree op01
= (*valueize
) (TREE_OPERAND (op0
, 1));
5086 tree tem
= fold_binary_loc (loc
, TREE_CODE (op0
),
5087 TREE_TYPE (op0
), op00
, op01
);
5092 return fold_ternary_loc (loc
, subcode
,
5093 gimple_expr_type (stmt
), op0
, op1
, op2
);
5104 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
5106 if (gimple_call_internal_p (stmt
))
5108 enum tree_code subcode
= ERROR_MARK
;
5109 switch (gimple_call_internal_fn (stmt
))
5111 case IFN_UBSAN_CHECK_ADD
:
5112 subcode
= PLUS_EXPR
;
5114 case IFN_UBSAN_CHECK_SUB
:
5115 subcode
= MINUS_EXPR
;
5117 case IFN_UBSAN_CHECK_MUL
:
5118 subcode
= MULT_EXPR
;
5123 tree arg0
= gimple_call_arg (stmt
, 0);
5124 tree arg1
= gimple_call_arg (stmt
, 1);
5125 tree op0
= (*valueize
) (arg0
);
5126 tree op1
= (*valueize
) (arg1
);
5128 if (TREE_CODE (op0
) != INTEGER_CST
5129 || TREE_CODE (op1
) != INTEGER_CST
)
5134 /* x * 0 = 0 * x = 0 without overflow. */
5135 if (integer_zerop (op0
) || integer_zerop (op1
))
5136 return build_zero_cst (TREE_TYPE (arg0
));
5139 /* y - y = 0 without overflow. */
5140 if (operand_equal_p (op0
, op1
, 0))
5141 return build_zero_cst (TREE_TYPE (arg0
));
5148 = fold_binary_loc (loc
, subcode
, TREE_TYPE (arg0
), op0
, op1
);
5150 && TREE_CODE (res
) == INTEGER_CST
5151 && !TREE_OVERFLOW (res
))
5156 fn
= (*valueize
) (gimple_call_fn (stmt
));
5157 if (TREE_CODE (fn
) == ADDR_EXPR
5158 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
5159 && DECL_BUILT_IN (TREE_OPERAND (fn
, 0))
5160 && gimple_builtin_call_types_compatible_p (stmt
,
5161 TREE_OPERAND (fn
, 0)))
5163 tree
*args
= XALLOCAVEC (tree
, gimple_call_num_args (stmt
));
5166 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
5167 args
[i
] = (*valueize
) (gimple_call_arg (stmt
, i
));
5168 retval
= fold_builtin_call_array (loc
,
5169 gimple_call_return_type (call_stmt
),
5170 fn
, gimple_call_num_args (stmt
), args
);
5173 /* fold_call_expr wraps the result inside a NOP_EXPR. */
5174 STRIP_NOPS (retval
);
5175 retval
= fold_convert (gimple_call_return_type (call_stmt
),
5188 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
5189 Returns NULL_TREE if folding to a constant is not possible, otherwise
5190 returns a constant according to is_gimple_min_invariant. */
5193 gimple_fold_stmt_to_constant (gimple stmt
, tree (*valueize
) (tree
))
5195 tree res
= gimple_fold_stmt_to_constant_1 (stmt
, valueize
);
5196 if (res
&& is_gimple_min_invariant (res
))
5202 /* The following set of functions are supposed to fold references using
5203 their constant initializers. */
5205 /* See if we can find constructor defining value of BASE.
5206 When we know the consructor with constant offset (such as
5207 base is array[40] and we do know constructor of array), then
5208 BIT_OFFSET is adjusted accordingly.
5210 As a special case, return error_mark_node when constructor
5211 is not explicitly available, but it is known to be zero
5212 such as 'static const int a;'. */
5214 get_base_constructor (tree base
, HOST_WIDE_INT
*bit_offset
,
5215 tree (*valueize
)(tree
))
5217 HOST_WIDE_INT bit_offset2
, size
, max_size
;
5218 if (TREE_CODE (base
) == MEM_REF
)
5220 if (!integer_zerop (TREE_OPERAND (base
, 1)))
5222 if (!tree_fits_shwi_p (TREE_OPERAND (base
, 1)))
5224 *bit_offset
+= (mem_ref_offset (base
).to_short_addr ()
5229 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
5230 base
= valueize (TREE_OPERAND (base
, 0));
5231 if (!base
|| TREE_CODE (base
) != ADDR_EXPR
)
5233 base
= TREE_OPERAND (base
, 0);
5236 /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
5237 DECL_INITIAL. If BASE is a nested reference into another
5238 ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
5239 the inner reference. */
5240 switch (TREE_CODE (base
))
5245 tree init
= ctor_for_folding (base
);
5247 /* Our semantic is exact opposite of ctor_for_folding;
5248 NULL means unknown, while error_mark_node is 0. */
5249 if (init
== error_mark_node
)
5252 return error_mark_node
;
5258 base
= get_ref_base_and_extent (base
, &bit_offset2
, &size
, &max_size
);
5259 if (max_size
== -1 || size
!= max_size
)
5261 *bit_offset
+= bit_offset2
;
5262 return get_base_constructor (base
, bit_offset
, valueize
);
5273 /* CTOR is CONSTRUCTOR of an array type. Fold reference of type TYPE and size
5274 SIZE to the memory at bit OFFSET. */
5277 fold_array_ctor_reference (tree type
, tree ctor
,
5278 unsigned HOST_WIDE_INT offset
,
5279 unsigned HOST_WIDE_INT size
,
5282 unsigned HOST_WIDE_INT cnt
;
5284 offset_int low_bound
;
5285 offset_int elt_size
;
5286 offset_int index
, max_index
;
5287 offset_int access_index
;
5288 tree domain_type
= NULL_TREE
, index_type
= NULL_TREE
;
5289 HOST_WIDE_INT inner_offset
;
5291 /* Compute low bound and elt size. */
5292 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
)
5293 domain_type
= TYPE_DOMAIN (TREE_TYPE (ctor
));
5294 if (domain_type
&& TYPE_MIN_VALUE (domain_type
))
5296 /* Static constructors for variably sized objects makes no sense. */
5297 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type
)) == INTEGER_CST
);
5298 index_type
= TREE_TYPE (TYPE_MIN_VALUE (domain_type
));
5299 low_bound
= wi::to_offset (TYPE_MIN_VALUE (domain_type
));
5303 /* Static constructors for variably sized objects makes no sense. */
5304 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor
))))
5306 elt_size
= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor
))));
5308 /* We can handle only constantly sized accesses that are known to not
5309 be larger than size of array element. */
5310 if (!TYPE_SIZE_UNIT (type
)
5311 || TREE_CODE (TYPE_SIZE_UNIT (type
)) != INTEGER_CST
5312 || wi::lts_p (elt_size
, wi::to_offset (TYPE_SIZE_UNIT (type
)))
5316 /* Compute the array index we look for. */
5317 access_index
= wi::udiv_trunc (offset_int (offset
/ BITS_PER_UNIT
),
5319 access_index
+= low_bound
;
5321 access_index
= wi::ext (access_index
, TYPE_PRECISION (index_type
),
5322 TYPE_SIGN (index_type
));
5324 /* And offset within the access. */
5325 inner_offset
= offset
% (elt_size
.to_uhwi () * BITS_PER_UNIT
);
5327 /* See if the array field is large enough to span whole access. We do not
5328 care to fold accesses spanning multiple array indexes. */
5329 if (inner_offset
+ size
> elt_size
.to_uhwi () * BITS_PER_UNIT
)
5332 index
= low_bound
- 1;
5334 index
= wi::ext (index
, TYPE_PRECISION (index_type
),
5335 TYPE_SIGN (index_type
));
5337 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
, cval
)
5339 /* Array constructor might explicitely set index, or specify range
5340 or leave index NULL meaning that it is next index after previous
5344 if (TREE_CODE (cfield
) == INTEGER_CST
)
5345 max_index
= index
= wi::to_offset (cfield
);
5348 gcc_assert (TREE_CODE (cfield
) == RANGE_EXPR
);
5349 index
= wi::to_offset (TREE_OPERAND (cfield
, 0));
5350 max_index
= wi::to_offset (TREE_OPERAND (cfield
, 1));
5357 index
= wi::ext (index
, TYPE_PRECISION (index_type
),
5358 TYPE_SIGN (index_type
));
5362 /* Do we have match? */
5363 if (wi::cmpu (access_index
, index
) >= 0
5364 && wi::cmpu (access_index
, max_index
) <= 0)
5365 return fold_ctor_reference (type
, cval
, inner_offset
, size
,
5368 /* When memory is not explicitely mentioned in constructor,
5369 it is 0 (or out of range). */
5370 return build_zero_cst (type
);
5373 /* CTOR is CONSTRUCTOR of an aggregate or vector.
5374 Fold reference of type TYPE and size SIZE to the memory at bit OFFSET. */
5377 fold_nonarray_ctor_reference (tree type
, tree ctor
,
5378 unsigned HOST_WIDE_INT offset
,
5379 unsigned HOST_WIDE_INT size
,
5382 unsigned HOST_WIDE_INT cnt
;
5385 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
,
5388 tree byte_offset
= DECL_FIELD_OFFSET (cfield
);
5389 tree field_offset
= DECL_FIELD_BIT_OFFSET (cfield
);
5390 tree field_size
= DECL_SIZE (cfield
);
5391 offset_int bitoffset
;
5392 offset_int bitoffset_end
, access_end
;
5394 /* Variable sized objects in static constructors makes no sense,
5395 but field_size can be NULL for flexible array members. */
5396 gcc_assert (TREE_CODE (field_offset
) == INTEGER_CST
5397 && TREE_CODE (byte_offset
) == INTEGER_CST
5398 && (field_size
!= NULL_TREE
5399 ? TREE_CODE (field_size
) == INTEGER_CST
5400 : TREE_CODE (TREE_TYPE (cfield
)) == ARRAY_TYPE
));
5402 /* Compute bit offset of the field. */
5403 bitoffset
= (wi::to_offset (field_offset
)
5404 + wi::lshift (wi::to_offset (byte_offset
),
5405 LOG2_BITS_PER_UNIT
));
5406 /* Compute bit offset where the field ends. */
5407 if (field_size
!= NULL_TREE
)
5408 bitoffset_end
= bitoffset
+ wi::to_offset (field_size
);
5412 access_end
= offset_int (offset
) + size
;
5414 /* Is there any overlap between [OFFSET, OFFSET+SIZE) and
5415 [BITOFFSET, BITOFFSET_END)? */
5416 if (wi::cmps (access_end
, bitoffset
) > 0
5417 && (field_size
== NULL_TREE
5418 || wi::lts_p (offset
, bitoffset_end
)))
5420 offset_int inner_offset
= offset_int (offset
) - bitoffset
;
5421 /* We do have overlap. Now see if field is large enough to
5422 cover the access. Give up for accesses spanning multiple
5424 if (wi::cmps (access_end
, bitoffset_end
) > 0)
5426 if (wi::lts_p (offset
, bitoffset
))
5428 return fold_ctor_reference (type
, cval
,
5429 inner_offset
.to_uhwi (), size
,
5433 /* When memory is not explicitely mentioned in constructor, it is 0. */
5434 return build_zero_cst (type
);
5437 /* CTOR is value initializing memory, fold reference of type TYPE and size SIZE
5438 to the memory at bit OFFSET. */
5441 fold_ctor_reference (tree type
, tree ctor
, unsigned HOST_WIDE_INT offset
,
5442 unsigned HOST_WIDE_INT size
, tree from_decl
)
5446 /* We found the field with exact match. */
5447 if (useless_type_conversion_p (type
, TREE_TYPE (ctor
))
5449 return canonicalize_constructor_val (unshare_expr (ctor
), from_decl
);
5451 /* We are at the end of walk, see if we can view convert the
5453 if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor
)) && !offset
5454 /* VIEW_CONVERT_EXPR is defined only for matching sizes. */
5455 && !compare_tree_int (TYPE_SIZE (type
), size
)
5456 && !compare_tree_int (TYPE_SIZE (TREE_TYPE (ctor
)), size
))
5458 ret
= canonicalize_constructor_val (unshare_expr (ctor
), from_decl
);
5459 ret
= fold_unary (VIEW_CONVERT_EXPR
, type
, ret
);
5461 STRIP_USELESS_TYPE_CONVERSION (ret
);
5464 /* For constants and byte-aligned/sized reads try to go through
5465 native_encode/interpret. */
5466 if (CONSTANT_CLASS_P (ctor
)
5467 && BITS_PER_UNIT
== 8
5468 && offset
% BITS_PER_UNIT
== 0
5469 && size
% BITS_PER_UNIT
== 0
5470 && size
<= MAX_BITSIZE_MODE_ANY_MODE
)
5472 unsigned char buf
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
5473 if (native_encode_expr (ctor
, buf
, size
/ BITS_PER_UNIT
,
5474 offset
/ BITS_PER_UNIT
) > 0)
5475 return native_interpret_expr (type
, buf
, size
/ BITS_PER_UNIT
);
5477 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
5480 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
5481 || TREE_CODE (TREE_TYPE (ctor
)) == VECTOR_TYPE
)
5482 return fold_array_ctor_reference (type
, ctor
, offset
, size
,
5485 return fold_nonarray_ctor_reference (type
, ctor
, offset
, size
,
5492 /* Return the tree representing the element referenced by T if T is an
5493 ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
5494 names using VALUEIZE. Return NULL_TREE otherwise. */
5497 fold_const_aggregate_ref_1 (tree t
, tree (*valueize
) (tree
))
5499 tree ctor
, idx
, base
;
5500 HOST_WIDE_INT offset
, size
, max_size
;
5503 if (TREE_THIS_VOLATILE (t
))
5507 return get_symbol_constant_value (t
);
5509 tem
= fold_read_from_constant_string (t
);
5513 switch (TREE_CODE (t
))
5516 case ARRAY_RANGE_REF
:
5517 /* Constant indexes are handled well by get_base_constructor.
5518 Only special case variable offsets.
5519 FIXME: This code can't handle nested references with variable indexes
5520 (they will be handled only by iteration of ccp). Perhaps we can bring
5521 get_ref_base_and_extent here and make it use a valueize callback. */
5522 if (TREE_CODE (TREE_OPERAND (t
, 1)) == SSA_NAME
5524 && (idx
= (*valueize
) (TREE_OPERAND (t
, 1)))
5525 && TREE_CODE (idx
) == INTEGER_CST
)
5527 tree low_bound
, unit_size
;
5529 /* If the resulting bit-offset is constant, track it. */
5530 if ((low_bound
= array_ref_low_bound (t
),
5531 TREE_CODE (low_bound
) == INTEGER_CST
)
5532 && (unit_size
= array_ref_element_size (t
),
5533 tree_fits_uhwi_p (unit_size
)))
5536 = wi::sext (wi::to_offset (idx
) - wi::to_offset (low_bound
),
5537 TYPE_PRECISION (TREE_TYPE (idx
)));
5539 if (wi::fits_shwi_p (woffset
))
5541 offset
= woffset
.to_shwi ();
5542 /* TODO: This code seems wrong, multiply then check
5543 to see if it fits. */
5544 offset
*= tree_to_uhwi (unit_size
);
5545 offset
*= BITS_PER_UNIT
;
5547 base
= TREE_OPERAND (t
, 0);
5548 ctor
= get_base_constructor (base
, &offset
, valueize
);
5549 /* Empty constructor. Always fold to 0. */
5550 if (ctor
== error_mark_node
)
5551 return build_zero_cst (TREE_TYPE (t
));
5552 /* Out of bound array access. Value is undefined,
5556 /* We can not determine ctor. */
5559 return fold_ctor_reference (TREE_TYPE (t
), ctor
, offset
,
5560 tree_to_uhwi (unit_size
)
5570 case TARGET_MEM_REF
:
5572 base
= get_ref_base_and_extent (t
, &offset
, &size
, &max_size
);
5573 ctor
= get_base_constructor (base
, &offset
, valueize
);
5575 /* Empty constructor. Always fold to 0. */
5576 if (ctor
== error_mark_node
)
5577 return build_zero_cst (TREE_TYPE (t
));
5578 /* We do not know precise address. */
5579 if (max_size
== -1 || max_size
!= size
)
5581 /* We can not determine ctor. */
5585 /* Out of bound array access. Value is undefined, but don't fold. */
5589 return fold_ctor_reference (TREE_TYPE (t
), ctor
, offset
, size
,
5595 tree c
= fold_const_aggregate_ref_1 (TREE_OPERAND (t
, 0), valueize
);
5596 if (c
&& TREE_CODE (c
) == COMPLEX_CST
)
5597 return fold_build1_loc (EXPR_LOCATION (t
),
5598 TREE_CODE (t
), TREE_TYPE (t
), c
);
5610 fold_const_aggregate_ref (tree t
)
5612 return fold_const_aggregate_ref_1 (t
, NULL
);
5615 /* Lookup virtual method with index TOKEN in a virtual table V
5617 Set CAN_REFER if non-NULL to false if method
5618 is not referable or if the virtual table is ill-formed (such as rewriten
5619 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
5622 gimple_get_virt_method_for_vtable (HOST_WIDE_INT token
,
5624 unsigned HOST_WIDE_INT offset
,
5627 tree vtable
= v
, init
, fn
;
5628 unsigned HOST_WIDE_INT size
;
5629 unsigned HOST_WIDE_INT elt_size
, access_index
;
5635 /* First of all double check we have virtual table. */
5636 if (TREE_CODE (v
) != VAR_DECL
5637 || !DECL_VIRTUAL_P (v
))
5639 /* Pass down that we lost track of the target. */
5645 init
= ctor_for_folding (v
);
5647 /* The virtual tables should always be born with constructors
5648 and we always should assume that they are avaialble for
5649 folding. At the moment we do not stream them in all cases,
5650 but it should never happen that ctor seem unreachable. */
5652 if (init
== error_mark_node
)
5654 gcc_assert (in_lto_p
);
5655 /* Pass down that we lost track of the target. */
5660 gcc_checking_assert (TREE_CODE (TREE_TYPE (v
)) == ARRAY_TYPE
);
5661 size
= tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v
))));
5662 offset
*= BITS_PER_UNIT
;
5663 offset
+= token
* size
;
5665 /* Lookup the value in the constructor that is assumed to be array.
5666 This is equivalent to
5667 fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
5668 offset, size, NULL);
5669 but in a constant time. We expect that frontend produced a simple
5670 array without indexed initializers. */
5672 gcc_checking_assert (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
);
5673 domain_type
= TYPE_DOMAIN (TREE_TYPE (init
));
5674 gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type
)));
5675 elt_size
= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init
))));
5677 access_index
= offset
/ BITS_PER_UNIT
/ elt_size
;
5678 gcc_checking_assert (offset
% (elt_size
* BITS_PER_UNIT
) == 0);
5680 /* This code makes an assumption that there are no
5681 indexed fileds produced by C++ FE, so we can directly index the array. */
5682 if (access_index
< CONSTRUCTOR_NELTS (init
))
5684 fn
= CONSTRUCTOR_ELT (init
, access_index
)->value
;
5685 gcc_checking_assert (!CONSTRUCTOR_ELT (init
, access_index
)->index
);
5691 /* For type inconsistent program we may end up looking up virtual method
5692 in virtual table that does not contain TOKEN entries. We may overrun
5693 the virtual table and pick up a constant or RTTI info pointer.
5694 In any case the call is undefined. */
5696 || (TREE_CODE (fn
) != ADDR_EXPR
&& TREE_CODE (fn
) != FDESC_EXPR
)
5697 || TREE_CODE (TREE_OPERAND (fn
, 0)) != FUNCTION_DECL
)
5698 fn
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
5701 fn
= TREE_OPERAND (fn
, 0);
5703 /* When cgraph node is missing and function is not public, we cannot
5704 devirtualize. This can happen in WHOPR when the actual method
5705 ends up in other partition, because we found devirtualization
5706 possibility too late. */
5707 if (!can_refer_decl_in_current_unit_p (fn
, vtable
))
5718 /* Make sure we create a cgraph node for functions we'll reference.
5719 They can be non-existent if the reference comes from an entry
5720 of an external vtable for example. */
5721 cgraph_node::get_create (fn
);
5726 /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
5727 is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
5728 KNOWN_BINFO carries the binfo describing the true type of
5729 OBJ_TYPE_REF_OBJECT(REF).
5730 Set CAN_REFER if non-NULL to false if method
5731 is not referable or if the virtual table is ill-formed (such as rewriten
5732 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
5735 gimple_get_virt_method_for_binfo (HOST_WIDE_INT token
, tree known_binfo
,
5738 unsigned HOST_WIDE_INT offset
;
5741 v
= BINFO_VTABLE (known_binfo
);
5742 /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */
5746 if (!vtable_pointer_value_to_vtable (v
, &v
, &offset
))
5752 return gimple_get_virt_method_for_vtable (token
, v
, offset
, can_refer
);
5755 /* Return true iff VAL is a gimple expression that is known to be
5756 non-negative. Restricted to floating-point inputs. */
5759 gimple_val_nonnegative_real_p (tree val
)
5763 gcc_assert (val
&& SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)));
5765 /* Use existing logic for non-gimple trees. */
5766 if (tree_expr_nonnegative_p (val
))
5769 if (TREE_CODE (val
) != SSA_NAME
)
5772 /* Currently we look only at the immediately defining statement
5773 to make this determination, since recursion on defining
5774 statements of operands can lead to quadratic behavior in the
5775 worst case. This is expected to catch almost all occurrences
5776 in practice. It would be possible to implement limited-depth
5777 recursion if important cases are lost. Alternatively, passes
5778 that need this information (such as the pow/powi lowering code
5779 in the cse_sincos pass) could be revised to provide it through
5780 dataflow propagation. */
5782 def_stmt
= SSA_NAME_DEF_STMT (val
);
5784 if (is_gimple_assign (def_stmt
))
5788 /* See fold-const.c:tree_expr_nonnegative_p for additional
5789 cases that could be handled with recursion. */
5791 switch (gimple_assign_rhs_code (def_stmt
))
5794 /* Always true for floating-point operands. */
5798 /* True if the two operands are identical (since we are
5799 restricted to floating-point inputs). */
5800 op0
= gimple_assign_rhs1 (def_stmt
);
5801 op1
= gimple_assign_rhs2 (def_stmt
);
5804 || operand_equal_p (op0
, op1
, 0))
5811 else if (is_gimple_call (def_stmt
))
5813 tree fndecl
= gimple_call_fndecl (def_stmt
);
5815 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
5819 switch (DECL_FUNCTION_CODE (fndecl
))
5821 CASE_FLT_FN (BUILT_IN_ACOS
):
5822 CASE_FLT_FN (BUILT_IN_ACOSH
):
5823 CASE_FLT_FN (BUILT_IN_CABS
):
5824 CASE_FLT_FN (BUILT_IN_COSH
):
5825 CASE_FLT_FN (BUILT_IN_ERFC
):
5826 CASE_FLT_FN (BUILT_IN_EXP
):
5827 CASE_FLT_FN (BUILT_IN_EXP10
):
5828 CASE_FLT_FN (BUILT_IN_EXP2
):
5829 CASE_FLT_FN (BUILT_IN_FABS
):
5830 CASE_FLT_FN (BUILT_IN_FDIM
):
5831 CASE_FLT_FN (BUILT_IN_HYPOT
):
5832 CASE_FLT_FN (BUILT_IN_POW10
):
5835 CASE_FLT_FN (BUILT_IN_SQRT
):
5836 /* sqrt(-0.0) is -0.0, and sqrt is not defined over other
5837 nonnegative inputs. */
5838 if (!HONOR_SIGNED_ZEROS (val
))
5843 CASE_FLT_FN (BUILT_IN_POWI
):
5844 /* True if the second argument is an even integer. */
5845 arg1
= gimple_call_arg (def_stmt
, 1);
5847 if (TREE_CODE (arg1
) == INTEGER_CST
5848 && (TREE_INT_CST_LOW (arg1
) & 1) == 0)
5853 CASE_FLT_FN (BUILT_IN_POW
):
5854 /* True if the second argument is an even integer-valued
5856 arg1
= gimple_call_arg (def_stmt
, 1);
5858 if (TREE_CODE (arg1
) == REAL_CST
)
5863 c
= TREE_REAL_CST (arg1
);
5864 n
= real_to_integer (&c
);
5868 REAL_VALUE_TYPE cint
;
5869 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
5870 if (real_identical (&c
, &cint
))
5886 /* Given a pointer value OP0, return a simplified version of an
5887 indirection through OP0, or NULL_TREE if no simplification is
5888 possible. Note that the resulting type may be different from
5889 the type pointed to in the sense that it is still compatible
5890 from the langhooks point of view. */
5893 gimple_fold_indirect_ref (tree t
)
5895 tree ptype
= TREE_TYPE (t
), type
= TREE_TYPE (ptype
);
5900 subtype
= TREE_TYPE (sub
);
5901 if (!POINTER_TYPE_P (subtype
))
5904 if (TREE_CODE (sub
) == ADDR_EXPR
)
5906 tree op
= TREE_OPERAND (sub
, 0);
5907 tree optype
= TREE_TYPE (op
);
5909 if (useless_type_conversion_p (type
, optype
))
5912 /* *(foo *)&fooarray => fooarray[0] */
5913 if (TREE_CODE (optype
) == ARRAY_TYPE
5914 && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype
))) == INTEGER_CST
5915 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
5917 tree type_domain
= TYPE_DOMAIN (optype
);
5918 tree min_val
= size_zero_node
;
5919 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
5920 min_val
= TYPE_MIN_VALUE (type_domain
);
5921 if (TREE_CODE (min_val
) == INTEGER_CST
)
5922 return build4 (ARRAY_REF
, type
, op
, min_val
, NULL_TREE
, NULL_TREE
);
5924 /* *(foo *)&complexfoo => __real__ complexfoo */
5925 else if (TREE_CODE (optype
) == COMPLEX_TYPE
5926 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
5927 return fold_build1 (REALPART_EXPR
, type
, op
);
5928 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
5929 else if (TREE_CODE (optype
) == VECTOR_TYPE
5930 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
5932 tree part_width
= TYPE_SIZE (type
);
5933 tree index
= bitsize_int (0);
5934 return fold_build3 (BIT_FIELD_REF
, type
, op
, part_width
, index
);
5938 /* *(p + CST) -> ... */
5939 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
5940 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
5942 tree addr
= TREE_OPERAND (sub
, 0);
5943 tree off
= TREE_OPERAND (sub
, 1);
5947 addrtype
= TREE_TYPE (addr
);
5949 /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
5950 if (TREE_CODE (addr
) == ADDR_EXPR
5951 && TREE_CODE (TREE_TYPE (addrtype
)) == VECTOR_TYPE
5952 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (addrtype
)))
5953 && tree_fits_uhwi_p (off
))
5955 unsigned HOST_WIDE_INT offset
= tree_to_uhwi (off
);
5956 tree part_width
= TYPE_SIZE (type
);
5957 unsigned HOST_WIDE_INT part_widthi
5958 = tree_to_shwi (part_width
) / BITS_PER_UNIT
;
5959 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
5960 tree index
= bitsize_int (indexi
);
5961 if (offset
/ part_widthi
5962 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype
)))
5963 return fold_build3 (BIT_FIELD_REF
, type
, TREE_OPERAND (addr
, 0),
5967 /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
5968 if (TREE_CODE (addr
) == ADDR_EXPR
5969 && TREE_CODE (TREE_TYPE (addrtype
)) == COMPLEX_TYPE
5970 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (addrtype
))))
5972 tree size
= TYPE_SIZE_UNIT (type
);
5973 if (tree_int_cst_equal (size
, off
))
5974 return fold_build1 (IMAGPART_EXPR
, type
, TREE_OPERAND (addr
, 0));
5977 /* *(p + CST) -> MEM_REF <p, CST>. */
5978 if (TREE_CODE (addr
) != ADDR_EXPR
5979 || DECL_P (TREE_OPERAND (addr
, 0)))
5980 return fold_build2 (MEM_REF
, type
,
5982 wide_int_to_tree (ptype
, off
));
5985 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
5986 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
5987 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype
)))) == INTEGER_CST
5988 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (subtype
))))
5991 tree min_val
= size_zero_node
;
5993 sub
= gimple_fold_indirect_ref (sub
);
5995 sub
= build1 (INDIRECT_REF
, TREE_TYPE (subtype
), osub
);
5996 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
5997 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
5998 min_val
= TYPE_MIN_VALUE (type_domain
);
5999 if (TREE_CODE (min_val
) == INTEGER_CST
)
6000 return build4 (ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
, NULL_TREE
);
6006 /* Return true if CODE is an operation that when operating on signed
6007 integer types involves undefined behavior on overflow and the
6008 operation can be expressed with unsigned arithmetic. */
6011 arith_code_with_undefined_signed_overflow (tree_code code
)
6019 case POINTER_PLUS_EXPR
:
6026 /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic
6027 operation that can be transformed to unsigned arithmetic by converting
6028 its operand, carrying out the operation in the corresponding unsigned
6029 type and converting the result back to the original type.
6031 Returns a sequence of statements that replace STMT and also contain
6032 a modified form of STMT itself. */
6035 rewrite_to_defined_overflow (gimple stmt
)
6037 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6039 fprintf (dump_file
, "rewriting stmt with undefined signed "
6041 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
6044 tree lhs
= gimple_assign_lhs (stmt
);
6045 tree type
= unsigned_type_for (TREE_TYPE (lhs
));
6046 gimple_seq stmts
= NULL
;
6047 for (unsigned i
= 1; i
< gimple_num_ops (stmt
); ++i
)
6049 gimple_seq stmts2
= NULL
;
6050 gimple_set_op (stmt
, i
,
6051 force_gimple_operand (fold_convert (type
,
6052 gimple_op (stmt
, i
)),
6053 &stmts2
, true, NULL_TREE
));
6054 gimple_seq_add_seq (&stmts
, stmts2
);
6056 gimple_assign_set_lhs (stmt
, make_ssa_name (type
, stmt
));
6057 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
6058 gimple_assign_set_rhs_code (stmt
, PLUS_EXPR
);
6059 gimple_seq_add_stmt (&stmts
, stmt
);
6060 gimple cvt
= gimple_build_assign (lhs
, NOP_EXPR
, gimple_assign_lhs (stmt
));
6061 gimple_seq_add_stmt (&stmts
, cvt
);
6067 /* The valueization hook we use for the gimple_build API simplification.
6068 This makes us match fold_buildN behavior by only combining with
6069 statements in the sequence(s) we are currently building. */
6072 gimple_build_valueize (tree op
)
6074 if (gimple_bb (SSA_NAME_DEF_STMT (op
)) == NULL
)
6079 /* Build the expression CODE OP0 of type TYPE with location LOC,
6080 simplifying it first if possible. Returns the built
6081 expression value and appends statements possibly defining it
6085 gimple_build (gimple_seq
*seq
, location_t loc
,
6086 enum tree_code code
, tree type
, tree op0
)
6088 tree res
= gimple_simplify (code
, type
, op0
, seq
, gimple_build_valueize
);
6091 if (gimple_in_ssa_p (cfun
))
6092 res
= make_ssa_name (type
);
6094 res
= create_tmp_reg (type
);
6096 if (code
== REALPART_EXPR
6097 || code
== IMAGPART_EXPR
6098 || code
== VIEW_CONVERT_EXPR
)
6099 stmt
= gimple_build_assign (res
, code
, build1 (code
, type
, op0
));
6101 stmt
= gimple_build_assign (res
, code
, op0
);
6102 gimple_set_location (stmt
, loc
);
6103 gimple_seq_add_stmt_without_update (seq
, stmt
);
6108 /* Build the expression OP0 CODE OP1 of type TYPE with location LOC,
6109 simplifying it first if possible. Returns the built
6110 expression value and appends statements possibly defining it
6114 gimple_build (gimple_seq
*seq
, location_t loc
,
6115 enum tree_code code
, tree type
, tree op0
, tree op1
)
6117 tree res
= gimple_simplify (code
, type
, op0
, op1
, seq
, gimple_build_valueize
);
6120 if (gimple_in_ssa_p (cfun
))
6121 res
= make_ssa_name (type
);
6123 res
= create_tmp_reg (type
);
6124 gimple stmt
= gimple_build_assign (res
, code
, op0
, op1
);
6125 gimple_set_location (stmt
, loc
);
6126 gimple_seq_add_stmt_without_update (seq
, stmt
);
6131 /* Build the expression (CODE OP0 OP1 OP2) of type TYPE with location LOC,
6132 simplifying it first if possible. Returns the built
6133 expression value and appends statements possibly defining it
6137 gimple_build (gimple_seq
*seq
, location_t loc
,
6138 enum tree_code code
, tree type
, tree op0
, tree op1
, tree op2
)
6140 tree res
= gimple_simplify (code
, type
, op0
, op1
, op2
,
6141 seq
, gimple_build_valueize
);
6144 if (gimple_in_ssa_p (cfun
))
6145 res
= make_ssa_name (type
);
6147 res
= create_tmp_reg (type
);
6149 if (code
== BIT_FIELD_REF
)
6150 stmt
= gimple_build_assign (res
, code
,
6151 build3 (code
, type
, op0
, op1
, op2
));
6153 stmt
= gimple_build_assign (res
, code
, op0
, op1
, op2
);
6154 gimple_set_location (stmt
, loc
);
6155 gimple_seq_add_stmt_without_update (seq
, stmt
);
6160 /* Build the call FN (ARG0) with a result of type TYPE
6161 (or no result if TYPE is void) with location LOC,
6162 simplifying it first if possible. Returns the built
6163 expression value (or NULL_TREE if TYPE is void) and appends
6164 statements possibly defining it to SEQ. */
6167 gimple_build (gimple_seq
*seq
, location_t loc
,
6168 enum built_in_function fn
, tree type
, tree arg0
)
6170 tree res
= gimple_simplify (fn
, type
, arg0
, seq
, gimple_build_valueize
);
6173 tree decl
= builtin_decl_implicit (fn
);
6174 gimple stmt
= gimple_build_call (decl
, 1, arg0
);
6175 if (!VOID_TYPE_P (type
))
6177 if (gimple_in_ssa_p (cfun
))
6178 res
= make_ssa_name (type
);
6180 res
= create_tmp_reg (type
);
6181 gimple_call_set_lhs (stmt
, res
);
6183 gimple_set_location (stmt
, loc
);
6184 gimple_seq_add_stmt_without_update (seq
, stmt
);
6189 /* Build the call FN (ARG0, ARG1) with a result of type TYPE
6190 (or no result if TYPE is void) with location LOC,
6191 simplifying it first if possible. Returns the built
6192 expression value (or NULL_TREE if TYPE is void) and appends
6193 statements possibly defining it to SEQ. */
6196 gimple_build (gimple_seq
*seq
, location_t loc
,
6197 enum built_in_function fn
, tree type
, tree arg0
, tree arg1
)
6199 tree res
= gimple_simplify (fn
, type
, arg0
, arg1
, seq
, gimple_build_valueize
);
6202 tree decl
= builtin_decl_implicit (fn
);
6203 gimple stmt
= gimple_build_call (decl
, 2, arg0
, arg1
);
6204 if (!VOID_TYPE_P (type
))
6206 if (gimple_in_ssa_p (cfun
))
6207 res
= make_ssa_name (type
);
6209 res
= create_tmp_reg (type
);
6210 gimple_call_set_lhs (stmt
, res
);
6212 gimple_set_location (stmt
, loc
);
6213 gimple_seq_add_stmt_without_update (seq
, stmt
);
6218 /* Build the call FN (ARG0, ARG1, ARG2) with a result of type TYPE
6219 (or no result if TYPE is void) with location LOC,
6220 simplifying it first if possible. Returns the built
6221 expression value (or NULL_TREE if TYPE is void) and appends
6222 statements possibly defining it to SEQ. */
6225 gimple_build (gimple_seq
*seq
, location_t loc
,
6226 enum built_in_function fn
, tree type
,
6227 tree arg0
, tree arg1
, tree arg2
)
6229 tree res
= gimple_simplify (fn
, type
, arg0
, arg1
, arg2
,
6230 seq
, gimple_build_valueize
);
6233 tree decl
= builtin_decl_implicit (fn
);
6234 gimple stmt
= gimple_build_call (decl
, 3, arg0
, arg1
, arg2
);
6235 if (!VOID_TYPE_P (type
))
6237 if (gimple_in_ssa_p (cfun
))
6238 res
= make_ssa_name (type
);
6240 res
= create_tmp_reg (type
);
6241 gimple_call_set_lhs (stmt
, res
);
6243 gimple_set_location (stmt
, loc
);
6244 gimple_seq_add_stmt_without_update (seq
, stmt
);
6249 /* Build the conversion (TYPE) OP with a result of type TYPE
6250 with location LOC if such conversion is neccesary in GIMPLE,
6251 simplifying it first.
6252 Returns the built expression value and appends
6253 statements possibly defining it to SEQ. */
6256 gimple_convert (gimple_seq
*seq
, location_t loc
, tree type
, tree op
)
6258 if (useless_type_conversion_p (type
, TREE_TYPE (op
)))
6260 return gimple_build (seq
, loc
, NOP_EXPR
, type
, op
);