1 /* Statement simplification on GIMPLE.
2 Copyright (C) 2010-2019 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"
32 #include "gimple-pretty-print.h"
33 #include "gimple-ssa-warn-restrict.h"
34 #include "fold-const.h"
37 #include "stor-layout.h"
39 #include "gimple-fold.h"
41 #include "gimple-iterator.h"
42 #include "tree-into-ssa.h"
44 #include "tree-object-size.h"
46 #include "tree-ssa-propagate.h"
47 #include "ipa-utils.h"
48 #include "tree-ssa-address.h"
49 #include "langhooks.h"
50 #include "gimplify-me.h"
54 #include "gimple-match.h"
55 #include "gomp-constants.h"
56 #include "optabs-query.h"
57 #include "omp-general.h"
59 #include "fold-const-call.h"
60 #include "stringpool.h"
63 #include "diagnostic-core.h"
66 #include "tree-vector-builder.h"
67 #include "tree-ssa-strlen.h"
69 enum strlen_range_kind
{
70 /* Compute the exact constant string length. */
72 /* Compute the maximum constant string length. */
74 /* Compute a range of string lengths bounded by object sizes. When
75 the length of a string cannot be determined, consider as the upper
76 bound the size of the enclosing object the string may be a member
77 or element of. Also determine the size of the largest character
78 array the string may refer to. */
80 /* Determine the integer value of the argument (not string length). */
85 get_range_strlen (tree
, bitmap
*, strlen_range_kind
, c_strlen_data
*, unsigned);
87 /* Return true when DECL can be referenced from current unit.
88 FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
89 We can get declarations that are not possible to reference for various
92 1) When analyzing C++ virtual tables.
93 C++ virtual tables do have known constructors even
94 when they are keyed to other compilation unit.
95 Those tables can contain pointers to methods and vars
96 in other units. Those methods have both STATIC and EXTERNAL
98 2) In WHOPR mode devirtualization might lead to reference
99 to method that was partitioned elsehwere.
100 In this case we have static VAR_DECL or FUNCTION_DECL
101 that has no corresponding callgraph/varpool node
103 3) COMDAT functions referred by external vtables that
104 we devirtualize only during final compilation stage.
105 At this time we already decided that we will not output
106 the function body and thus we can't reference the symbol
110 can_refer_decl_in_current_unit_p (tree decl
, tree from_decl
)
113 struct cgraph_node
*node
;
116 if (DECL_ABSTRACT_P (decl
))
119 /* We are concerned only about static/external vars and functions. */
120 if ((!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
121 || !VAR_OR_FUNCTION_DECL_P (decl
))
124 /* Static objects can be referred only if they are defined and not optimized
126 if (!TREE_PUBLIC (decl
))
128 if (DECL_EXTERNAL (decl
))
130 /* Before we start optimizing unreachable code we can be sure all
131 static objects are defined. */
132 if (symtab
->function_flags_ready
)
134 snode
= symtab_node::get (decl
);
135 if (!snode
|| !snode
->definition
)
137 node
= dyn_cast
<cgraph_node
*> (snode
);
138 return !node
|| !node
->global
.inlined_to
;
141 /* We will later output the initializer, so we can refer to it.
142 So we are concerned only when DECL comes from initializer of
143 external var or var that has been optimized out. */
145 || !VAR_P (from_decl
)
146 || (!DECL_EXTERNAL (from_decl
)
147 && (vnode
= varpool_node::get (from_decl
)) != NULL
148 && vnode
->definition
)
150 && (vnode
= varpool_node::get (from_decl
)) != NULL
151 && vnode
->in_other_partition
))
153 /* We are folding reference from external vtable. The vtable may reffer
154 to a symbol keyed to other compilation unit. The other compilation
155 unit may be in separate DSO and the symbol may be hidden. */
156 if (DECL_VISIBILITY_SPECIFIED (decl
)
157 && DECL_EXTERNAL (decl
)
158 && DECL_VISIBILITY (decl
) != VISIBILITY_DEFAULT
159 && (!(snode
= symtab_node::get (decl
)) || !snode
->in_other_partition
))
161 /* When function is public, we always can introduce new reference.
162 Exception are the COMDAT functions where introducing a direct
163 reference imply need to include function body in the curren tunit. */
164 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
))
166 /* We have COMDAT. We are going to check if we still have definition
167 or if the definition is going to be output in other partition.
168 Bypass this when gimplifying; all needed functions will be produced.
170 As observed in PR20991 for already optimized out comdat virtual functions
171 it may be tempting to not necessarily give up because the copy will be
172 output elsewhere when corresponding vtable is output.
173 This is however not possible - ABI specify that COMDATs are output in
174 units where they are used and when the other unit was compiled with LTO
175 it is possible that vtable was kept public while the function itself
177 if (!symtab
->function_flags_ready
)
180 snode
= symtab_node::get (decl
);
182 || ((!snode
->definition
|| DECL_EXTERNAL (decl
))
183 && (!snode
->in_other_partition
184 || (!snode
->forced_by_abi
&& !snode
->force_output
))))
186 node
= dyn_cast
<cgraph_node
*> (snode
);
187 return !node
|| !node
->global
.inlined_to
;
190 /* Create a temporary for TYPE for a statement STMT. If the current function
191 is in SSA form, a SSA name is created. Otherwise a temporary register
195 create_tmp_reg_or_ssa_name (tree type
, gimple
*stmt
)
197 if (gimple_in_ssa_p (cfun
))
198 return make_ssa_name (type
, stmt
);
200 return create_tmp_reg (type
);
203 /* CVAL is value taken from DECL_INITIAL of variable. Try to transform it into
204 acceptable form for is_gimple_min_invariant.
205 FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL. */
208 canonicalize_constructor_val (tree cval
, tree from_decl
)
210 tree orig_cval
= cval
;
212 if (TREE_CODE (cval
) == POINTER_PLUS_EXPR
213 && TREE_CODE (TREE_OPERAND (cval
, 1)) == INTEGER_CST
)
215 tree ptr
= TREE_OPERAND (cval
, 0);
216 if (is_gimple_min_invariant (ptr
))
217 cval
= build1_loc (EXPR_LOCATION (cval
),
218 ADDR_EXPR
, TREE_TYPE (ptr
),
219 fold_build2 (MEM_REF
, TREE_TYPE (TREE_TYPE (ptr
)),
221 fold_convert (ptr_type_node
,
222 TREE_OPERAND (cval
, 1))));
224 if (TREE_CODE (cval
) == ADDR_EXPR
)
226 tree base
= NULL_TREE
;
227 if (TREE_CODE (TREE_OPERAND (cval
, 0)) == COMPOUND_LITERAL_EXPR
)
229 base
= COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval
, 0));
231 TREE_OPERAND (cval
, 0) = base
;
234 base
= get_base_address (TREE_OPERAND (cval
, 0));
238 if (VAR_OR_FUNCTION_DECL_P (base
)
239 && !can_refer_decl_in_current_unit_p (base
, from_decl
))
241 if (TREE_TYPE (base
) == error_mark_node
)
244 TREE_ADDRESSABLE (base
) = 1;
245 else if (TREE_CODE (base
) == FUNCTION_DECL
)
247 /* Make sure we create a cgraph node for functions we'll reference.
248 They can be non-existent if the reference comes from an entry
249 of an external vtable for example. */
250 cgraph_node::get_create (base
);
252 /* Fixup types in global initializers. */
253 if (TREE_TYPE (TREE_TYPE (cval
)) != TREE_TYPE (TREE_OPERAND (cval
, 0)))
254 cval
= build_fold_addr_expr (TREE_OPERAND (cval
, 0));
256 if (!useless_type_conversion_p (TREE_TYPE (orig_cval
), TREE_TYPE (cval
)))
257 cval
= fold_convert (TREE_TYPE (orig_cval
), cval
);
260 if (TREE_OVERFLOW_P (cval
))
261 return drop_tree_overflow (cval
);
265 /* If SYM is a constant variable with known value, return the value.
266 NULL_TREE is returned otherwise. */
269 get_symbol_constant_value (tree sym
)
271 tree val
= ctor_for_folding (sym
);
272 if (val
!= error_mark_node
)
276 val
= canonicalize_constructor_val (unshare_expr (val
), sym
);
277 if (val
&& is_gimple_min_invariant (val
))
282 /* Variables declared 'const' without an initializer
283 have zero as the initializer if they may not be
284 overridden at link or run time. */
286 && is_gimple_reg_type (TREE_TYPE (sym
)))
287 return build_zero_cst (TREE_TYPE (sym
));
295 /* Subroutine of fold_stmt. We perform several simplifications of the
296 memory reference tree EXPR and make sure to re-gimplify them properly
297 after propagation of constant addresses. IS_LHS is true if the
298 reference is supposed to be an lvalue. */
301 maybe_fold_reference (tree expr
, bool is_lhs
)
305 if ((TREE_CODE (expr
) == VIEW_CONVERT_EXPR
306 || TREE_CODE (expr
) == REALPART_EXPR
307 || TREE_CODE (expr
) == IMAGPART_EXPR
)
308 && CONSTANT_CLASS_P (TREE_OPERAND (expr
, 0)))
309 return fold_unary_loc (EXPR_LOCATION (expr
),
312 TREE_OPERAND (expr
, 0));
313 else if (TREE_CODE (expr
) == BIT_FIELD_REF
314 && CONSTANT_CLASS_P (TREE_OPERAND (expr
, 0)))
315 return fold_ternary_loc (EXPR_LOCATION (expr
),
318 TREE_OPERAND (expr
, 0),
319 TREE_OPERAND (expr
, 1),
320 TREE_OPERAND (expr
, 2));
323 && (result
= fold_const_aggregate_ref (expr
))
324 && is_gimple_min_invariant (result
))
331 /* Attempt to fold an assignment statement pointed-to by SI. Returns a
332 replacement rhs for the statement or NULL_TREE if no simplification
333 could be made. It is assumed that the operands have been previously
337 fold_gimple_assign (gimple_stmt_iterator
*si
)
339 gimple
*stmt
= gsi_stmt (*si
);
340 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
341 location_t loc
= gimple_location (stmt
);
343 tree result
= NULL_TREE
;
345 switch (get_gimple_rhs_class (subcode
))
347 case GIMPLE_SINGLE_RHS
:
349 tree rhs
= gimple_assign_rhs1 (stmt
);
351 if (TREE_CLOBBER_P (rhs
))
354 if (REFERENCE_CLASS_P (rhs
))
355 return maybe_fold_reference (rhs
, false);
357 else if (TREE_CODE (rhs
) == OBJ_TYPE_REF
)
359 tree val
= OBJ_TYPE_REF_EXPR (rhs
);
360 if (is_gimple_min_invariant (val
))
362 else if (flag_devirtualize
&& virtual_method_call_p (rhs
))
365 vec
<cgraph_node
*>targets
366 = possible_polymorphic_call_targets (rhs
, stmt
, &final
);
367 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
369 if (dump_enabled_p ())
371 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, stmt
,
372 "resolving virtual function address "
373 "reference to function %s\n",
374 targets
.length () == 1
375 ? targets
[0]->name ()
378 if (targets
.length () == 1)
380 val
= fold_convert (TREE_TYPE (val
),
381 build_fold_addr_expr_loc
382 (loc
, targets
[0]->decl
));
383 STRIP_USELESS_TYPE_CONVERSION (val
);
386 /* We cannot use __builtin_unreachable here because it
387 cannot have address taken. */
388 val
= build_int_cst (TREE_TYPE (val
), 0);
394 else if (TREE_CODE (rhs
) == ADDR_EXPR
)
396 tree ref
= TREE_OPERAND (rhs
, 0);
397 tree tem
= maybe_fold_reference (ref
, true);
399 && TREE_CODE (tem
) == MEM_REF
400 && integer_zerop (TREE_OPERAND (tem
, 1)))
401 result
= fold_convert (TREE_TYPE (rhs
), TREE_OPERAND (tem
, 0));
403 result
= fold_convert (TREE_TYPE (rhs
),
404 build_fold_addr_expr_loc (loc
, tem
));
405 else if (TREE_CODE (ref
) == MEM_REF
406 && integer_zerop (TREE_OPERAND (ref
, 1)))
407 result
= fold_convert (TREE_TYPE (rhs
), TREE_OPERAND (ref
, 0));
411 /* Strip away useless type conversions. Both the
412 NON_LVALUE_EXPR that may have been added by fold, and
413 "useless" type conversions that might now be apparent
414 due to propagation. */
415 STRIP_USELESS_TYPE_CONVERSION (result
);
417 if (result
!= rhs
&& valid_gimple_rhs_p (result
))
422 else if (TREE_CODE (rhs
) == CONSTRUCTOR
423 && TREE_CODE (TREE_TYPE (rhs
)) == VECTOR_TYPE
)
425 /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */
429 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs
), i
, val
)
430 if (! CONSTANT_CLASS_P (val
))
433 return build_vector_from_ctor (TREE_TYPE (rhs
),
434 CONSTRUCTOR_ELTS (rhs
));
437 else if (DECL_P (rhs
))
438 return get_symbol_constant_value (rhs
);
442 case GIMPLE_UNARY_RHS
:
445 case GIMPLE_BINARY_RHS
:
448 case GIMPLE_TERNARY_RHS
:
449 result
= fold_ternary_loc (loc
, subcode
,
450 TREE_TYPE (gimple_assign_lhs (stmt
)),
451 gimple_assign_rhs1 (stmt
),
452 gimple_assign_rhs2 (stmt
),
453 gimple_assign_rhs3 (stmt
));
457 STRIP_USELESS_TYPE_CONVERSION (result
);
458 if (valid_gimple_rhs_p (result
))
463 case GIMPLE_INVALID_RHS
:
471 /* Replace a statement at *SI_P with a sequence of statements in STMTS,
472 adjusting the replacement stmts location and virtual operands.
473 If the statement has a lhs the last stmt in the sequence is expected
474 to assign to that lhs. */
477 gsi_replace_with_seq_vops (gimple_stmt_iterator
*si_p
, gimple_seq stmts
)
479 gimple
*stmt
= gsi_stmt (*si_p
);
481 if (gimple_has_location (stmt
))
482 annotate_all_with_location (stmts
, gimple_location (stmt
));
484 /* First iterate over the replacement statements backward, assigning
485 virtual operands to their defining statements. */
486 gimple
*laststore
= NULL
;
487 for (gimple_stmt_iterator i
= gsi_last (stmts
);
488 !gsi_end_p (i
); gsi_prev (&i
))
490 gimple
*new_stmt
= gsi_stmt (i
);
491 if ((gimple_assign_single_p (new_stmt
)
492 && !is_gimple_reg (gimple_assign_lhs (new_stmt
)))
493 || (is_gimple_call (new_stmt
)
494 && (gimple_call_flags (new_stmt
)
495 & (ECF_NOVOPS
| ECF_PURE
| ECF_CONST
| ECF_NORETURN
)) == 0))
499 vdef
= gimple_vdef (stmt
);
501 vdef
= make_ssa_name (gimple_vop (cfun
), new_stmt
);
502 gimple_set_vdef (new_stmt
, vdef
);
503 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
504 SSA_NAME_DEF_STMT (vdef
) = new_stmt
;
505 laststore
= new_stmt
;
509 /* Second iterate over the statements forward, assigning virtual
510 operands to their uses. */
511 tree reaching_vuse
= gimple_vuse (stmt
);
512 for (gimple_stmt_iterator i
= gsi_start (stmts
);
513 !gsi_end_p (i
); gsi_next (&i
))
515 gimple
*new_stmt
= gsi_stmt (i
);
516 /* If the new statement possibly has a VUSE, update it with exact SSA
517 name we know will reach this one. */
518 if (gimple_has_mem_ops (new_stmt
))
519 gimple_set_vuse (new_stmt
, reaching_vuse
);
520 gimple_set_modified (new_stmt
, true);
521 if (gimple_vdef (new_stmt
))
522 reaching_vuse
= gimple_vdef (new_stmt
);
525 /* If the new sequence does not do a store release the virtual
526 definition of the original statement. */
528 && reaching_vuse
== gimple_vuse (stmt
))
530 tree vdef
= gimple_vdef (stmt
);
532 && TREE_CODE (vdef
) == SSA_NAME
)
534 unlink_stmt_vdef (stmt
);
535 release_ssa_name (vdef
);
539 /* Finally replace the original statement with the sequence. */
540 gsi_replace_with_seq (si_p
, stmts
, false);
543 /* Convert EXPR into a GIMPLE value suitable for substitution on the
544 RHS of an assignment. Insert the necessary statements before
545 iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL
546 is replaced. If the call is expected to produces a result, then it
547 is replaced by an assignment of the new RHS to the result variable.
548 If the result is to be ignored, then the call is replaced by a
549 GIMPLE_NOP. A proper VDEF chain is retained by making the first
550 VUSE and the last VDEF of the whole sequence be the same as the replaced
551 statement and using new SSA names for stores in between. */
554 gimplify_and_update_call_from_tree (gimple_stmt_iterator
*si_p
, tree expr
)
557 gimple
*stmt
, *new_stmt
;
558 gimple_stmt_iterator i
;
559 gimple_seq stmts
= NULL
;
561 stmt
= gsi_stmt (*si_p
);
563 gcc_assert (is_gimple_call (stmt
));
565 push_gimplify_context (gimple_in_ssa_p (cfun
));
567 lhs
= gimple_call_lhs (stmt
);
568 if (lhs
== NULL_TREE
)
570 gimplify_and_add (expr
, &stmts
);
571 /* We can end up with folding a memcpy of an empty class assignment
572 which gets optimized away by C++ gimplification. */
573 if (gimple_seq_empty_p (stmts
))
575 pop_gimplify_context (NULL
);
576 if (gimple_in_ssa_p (cfun
))
578 unlink_stmt_vdef (stmt
);
581 gsi_replace (si_p
, gimple_build_nop (), false);
587 tree tmp
= force_gimple_operand (expr
, &stmts
, false, NULL_TREE
);
588 new_stmt
= gimple_build_assign (lhs
, tmp
);
589 i
= gsi_last (stmts
);
590 gsi_insert_after_without_update (&i
, new_stmt
,
591 GSI_CONTINUE_LINKING
);
594 pop_gimplify_context (NULL
);
596 gsi_replace_with_seq_vops (si_p
, stmts
);
600 /* Replace the call at *GSI with the gimple value VAL. */
603 replace_call_with_value (gimple_stmt_iterator
*gsi
, tree val
)
605 gimple
*stmt
= gsi_stmt (*gsi
);
606 tree lhs
= gimple_call_lhs (stmt
);
610 if (!useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (val
)))
611 val
= fold_convert (TREE_TYPE (lhs
), val
);
612 repl
= gimple_build_assign (lhs
, val
);
615 repl
= gimple_build_nop ();
616 tree vdef
= gimple_vdef (stmt
);
617 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
619 unlink_stmt_vdef (stmt
);
620 release_ssa_name (vdef
);
622 gsi_replace (gsi
, repl
, false);
625 /* Replace the call at *GSI with the new call REPL and fold that
629 replace_call_with_call_and_fold (gimple_stmt_iterator
*gsi
, gimple
*repl
)
631 gimple
*stmt
= gsi_stmt (*gsi
);
632 gimple_call_set_lhs (repl
, gimple_call_lhs (stmt
));
633 gimple_set_location (repl
, gimple_location (stmt
));
634 if (gimple_vdef (stmt
)
635 && TREE_CODE (gimple_vdef (stmt
)) == SSA_NAME
)
637 gimple_set_vdef (repl
, gimple_vdef (stmt
));
638 SSA_NAME_DEF_STMT (gimple_vdef (repl
)) = repl
;
640 if (gimple_vuse (stmt
))
641 gimple_set_vuse (repl
, gimple_vuse (stmt
));
642 gsi_replace (gsi
, repl
, false);
646 /* Return true if VAR is a VAR_DECL or a component thereof. */
649 var_decl_component_p (tree var
)
652 while (handled_component_p (inner
))
653 inner
= TREE_OPERAND (inner
, 0);
654 return (DECL_P (inner
)
655 || (TREE_CODE (inner
) == MEM_REF
656 && TREE_CODE (TREE_OPERAND (inner
, 0)) == ADDR_EXPR
));
659 /* Return TRUE if the SIZE argument, representing the size of an
660 object, is in a range of values of which exactly zero is valid. */
663 size_must_be_zero_p (tree size
)
665 if (integer_zerop (size
))
668 if (TREE_CODE (size
) != SSA_NAME
|| !INTEGRAL_TYPE_P (TREE_TYPE (size
)))
671 tree type
= TREE_TYPE (size
);
672 int prec
= TYPE_PRECISION (type
);
674 /* Compute the value of SSIZE_MAX, the largest positive value that
675 can be stored in ssize_t, the signed counterpart of size_t. */
676 wide_int ssize_max
= wi::lshift (wi::one (prec
), prec
- 1) - 1;
677 value_range
valid_range (VR_RANGE
,
678 build_int_cst (type
, 0),
679 wide_int_to_tree (type
, ssize_max
));
681 get_range_info (size
, vr
);
682 vr
.intersect (&valid_range
);
686 /* Fold function call to builtin mem{{,p}cpy,move}. Try to detect and
687 diagnose (otherwise undefined) overlapping copies without preventing
688 folding. When folded, GCC guarantees that overlapping memcpy has
689 the same semantics as memmove. Call to the library memcpy need not
690 provide the same guarantee. Return false if no simplification can
694 gimple_fold_builtin_memory_op (gimple_stmt_iterator
*gsi
,
695 tree dest
, tree src
, int endp
)
697 gimple
*stmt
= gsi_stmt (*gsi
);
698 tree lhs
= gimple_call_lhs (stmt
);
699 tree len
= gimple_call_arg (stmt
, 2);
700 tree destvar
, srcvar
;
701 location_t loc
= gimple_location (stmt
);
703 /* If the LEN parameter is a constant zero or in range where
704 the only valid value is zero, return DEST. */
705 if (size_must_be_zero_p (len
))
708 if (gimple_call_lhs (stmt
))
709 repl
= gimple_build_assign (gimple_call_lhs (stmt
), dest
);
711 repl
= gimple_build_nop ();
712 tree vdef
= gimple_vdef (stmt
);
713 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
715 unlink_stmt_vdef (stmt
);
716 release_ssa_name (vdef
);
718 gsi_replace (gsi
, repl
, false);
722 /* If SRC and DEST are the same (and not volatile), return
723 DEST{,+LEN,+LEN-1}. */
724 if (operand_equal_p (src
, dest
, 0))
726 /* Avoid diagnosing exact overlap in calls to __builtin_memcpy.
727 It's safe and may even be emitted by GCC itself (see bug
729 unlink_stmt_vdef (stmt
);
730 if (gimple_vdef (stmt
) && TREE_CODE (gimple_vdef (stmt
)) == SSA_NAME
)
731 release_ssa_name (gimple_vdef (stmt
));
734 gsi_replace (gsi
, gimple_build_nop (), false);
741 tree srctype
, desttype
;
742 unsigned int src_align
, dest_align
;
745 unsigned HOST_WIDE_INT tmp_len
;
747 /* Build accesses at offset zero with a ref-all character type. */
748 off0
= build_int_cst (build_pointer_type_for_mode (char_type_node
,
751 /* If we can perform the copy efficiently with first doing all loads
752 and then all stores inline it that way. Currently efficiently
753 means that we can load all the memory into a single integer
754 register which is what MOVE_MAX gives us. */
755 src_align
= get_pointer_alignment (src
);
756 dest_align
= get_pointer_alignment (dest
);
757 if (tree_fits_uhwi_p (len
)
758 && compare_tree_int (len
, MOVE_MAX
) <= 0
759 /* ??? Don't transform copies from strings with known length this
760 confuses the tree-ssa-strlen.c. This doesn't handle
761 the case in gcc.dg/strlenopt-8.c which is XFAILed for that
763 && !c_strlen (src
, 2)
764 && !((tmp_str
= c_getstr (src
, &tmp_len
)) != NULL
765 && memchr (tmp_str
, 0, tmp_len
) == NULL
))
767 unsigned ilen
= tree_to_uhwi (len
);
768 if (pow2p_hwi (ilen
))
770 /* Detect out-of-bounds accesses without issuing warnings.
771 Avoid folding out-of-bounds copies but to avoid false
772 positives for unreachable code defer warning until after
773 DCE has worked its magic.
774 -Wrestrict is still diagnosed. */
775 if (int warning
= check_bounds_or_overlap (as_a
<gcall
*>(stmt
),
778 if (warning
!= OPT_Wrestrict
)
781 scalar_int_mode mode
;
782 tree type
= lang_hooks
.types
.type_for_size (ilen
* 8, 1);
784 && is_a
<scalar_int_mode
> (TYPE_MODE (type
), &mode
)
785 && GET_MODE_SIZE (mode
) * BITS_PER_UNIT
== ilen
* 8
786 /* If the destination pointer is not aligned we must be able
787 to emit an unaligned store. */
788 && (dest_align
>= GET_MODE_ALIGNMENT (mode
)
789 || !targetm
.slow_unaligned_access (mode
, dest_align
)
790 || (optab_handler (movmisalign_optab
, mode
)
791 != CODE_FOR_nothing
)))
794 tree desttype
= type
;
795 if (src_align
< GET_MODE_ALIGNMENT (mode
))
796 srctype
= build_aligned_type (type
, src_align
);
797 tree srcmem
= fold_build2 (MEM_REF
, srctype
, src
, off0
);
798 tree tem
= fold_const_aggregate_ref (srcmem
);
801 else if (src_align
< GET_MODE_ALIGNMENT (mode
)
802 && targetm
.slow_unaligned_access (mode
, src_align
)
803 && (optab_handler (movmisalign_optab
, mode
)
804 == CODE_FOR_nothing
))
809 if (is_gimple_reg_type (TREE_TYPE (srcmem
)))
811 new_stmt
= gimple_build_assign (NULL_TREE
, srcmem
);
813 = create_tmp_reg_or_ssa_name (TREE_TYPE (srcmem
),
815 gimple_assign_set_lhs (new_stmt
, srcmem
);
816 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
817 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
819 if (dest_align
< GET_MODE_ALIGNMENT (mode
))
820 desttype
= build_aligned_type (type
, dest_align
);
822 = gimple_build_assign (fold_build2 (MEM_REF
, desttype
,
825 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
826 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
827 if (gimple_vdef (new_stmt
)
828 && TREE_CODE (gimple_vdef (new_stmt
)) == SSA_NAME
)
829 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt
)) = new_stmt
;
832 gsi_replace (gsi
, new_stmt
, false);
835 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
844 /* Both DEST and SRC must be pointer types.
845 ??? This is what old code did. Is the testing for pointer types
848 If either SRC is readonly or length is 1, we can use memcpy. */
849 if (!dest_align
|| !src_align
)
851 if (readonly_data_expr (src
)
852 || (tree_fits_uhwi_p (len
)
853 && (MIN (src_align
, dest_align
) / BITS_PER_UNIT
854 >= tree_to_uhwi (len
))))
856 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
859 gimple_call_set_fndecl (stmt
, fn
);
860 gimple_call_set_arg (stmt
, 0, dest
);
861 gimple_call_set_arg (stmt
, 1, src
);
866 /* If *src and *dest can't overlap, optimize into memcpy as well. */
867 if (TREE_CODE (src
) == ADDR_EXPR
868 && TREE_CODE (dest
) == ADDR_EXPR
)
870 tree src_base
, dest_base
, fn
;
871 poly_int64 src_offset
= 0, dest_offset
= 0;
874 srcvar
= TREE_OPERAND (src
, 0);
875 src_base
= get_addr_base_and_unit_offset (srcvar
, &src_offset
);
876 if (src_base
== NULL
)
878 destvar
= TREE_OPERAND (dest
, 0);
879 dest_base
= get_addr_base_and_unit_offset (destvar
,
881 if (dest_base
== NULL
)
883 if (!poly_int_tree_p (len
, &maxsize
))
885 if (SSA_VAR_P (src_base
)
886 && SSA_VAR_P (dest_base
))
888 if (operand_equal_p (src_base
, dest_base
, 0)
889 && ranges_maybe_overlap_p (src_offset
, maxsize
,
890 dest_offset
, maxsize
))
893 else if (TREE_CODE (src_base
) == MEM_REF
894 && TREE_CODE (dest_base
) == MEM_REF
)
896 if (! operand_equal_p (TREE_OPERAND (src_base
, 0),
897 TREE_OPERAND (dest_base
, 0), 0))
899 poly_offset_int full_src_offset
900 = mem_ref_offset (src_base
) + src_offset
;
901 poly_offset_int full_dest_offset
902 = mem_ref_offset (dest_base
) + dest_offset
;
903 if (ranges_maybe_overlap_p (full_src_offset
, maxsize
,
904 full_dest_offset
, maxsize
))
910 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
913 gimple_call_set_fndecl (stmt
, fn
);
914 gimple_call_set_arg (stmt
, 0, dest
);
915 gimple_call_set_arg (stmt
, 1, src
);
920 /* If the destination and source do not alias optimize into
922 if ((is_gimple_min_invariant (dest
)
923 || TREE_CODE (dest
) == SSA_NAME
)
924 && (is_gimple_min_invariant (src
)
925 || TREE_CODE (src
) == SSA_NAME
))
928 ao_ref_init_from_ptr_and_size (&destr
, dest
, len
);
929 ao_ref_init_from_ptr_and_size (&srcr
, src
, len
);
930 if (!refs_may_alias_p_1 (&destr
, &srcr
, false))
933 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
936 gimple_call_set_fndecl (stmt
, fn
);
937 gimple_call_set_arg (stmt
, 0, dest
);
938 gimple_call_set_arg (stmt
, 1, src
);
947 if (!tree_fits_shwi_p (len
))
949 if (!POINTER_TYPE_P (TREE_TYPE (src
))
950 || !POINTER_TYPE_P (TREE_TYPE (dest
)))
952 /* In the following try to find a type that is most natural to be
953 used for the memcpy source and destination and that allows
954 the most optimization when memcpy is turned into a plain assignment
955 using that type. In theory we could always use a char[len] type
956 but that only gains us that the destination and source possibly
957 no longer will have their address taken. */
958 srctype
= TREE_TYPE (TREE_TYPE (src
));
959 if (TREE_CODE (srctype
) == ARRAY_TYPE
960 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype
), len
))
961 srctype
= TREE_TYPE (srctype
);
962 desttype
= TREE_TYPE (TREE_TYPE (dest
));
963 if (TREE_CODE (desttype
) == ARRAY_TYPE
964 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype
), len
))
965 desttype
= TREE_TYPE (desttype
);
966 if (TREE_ADDRESSABLE (srctype
)
967 || TREE_ADDRESSABLE (desttype
))
970 /* Make sure we are not copying using a floating-point mode or
971 a type whose size possibly does not match its precision. */
972 if (FLOAT_MODE_P (TYPE_MODE (desttype
))
973 || TREE_CODE (desttype
) == BOOLEAN_TYPE
974 || TREE_CODE (desttype
) == ENUMERAL_TYPE
)
975 desttype
= bitwise_type_for_mode (TYPE_MODE (desttype
));
976 if (FLOAT_MODE_P (TYPE_MODE (srctype
))
977 || TREE_CODE (srctype
) == BOOLEAN_TYPE
978 || TREE_CODE (srctype
) == ENUMERAL_TYPE
)
979 srctype
= bitwise_type_for_mode (TYPE_MODE (srctype
));
987 src_align
= get_pointer_alignment (src
);
988 dest_align
= get_pointer_alignment (dest
);
989 if (dest_align
< TYPE_ALIGN (desttype
)
990 || src_align
< TYPE_ALIGN (srctype
))
994 if (TREE_CODE (dest
) == ADDR_EXPR
995 && var_decl_component_p (TREE_OPERAND (dest
, 0))
996 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype
), len
))
997 destvar
= fold_build2 (MEM_REF
, desttype
, dest
, off0
);
1000 if (TREE_CODE (src
) == ADDR_EXPR
1001 && var_decl_component_p (TREE_OPERAND (src
, 0))
1002 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype
), len
))
1005 || src_align
>= TYPE_ALIGN (desttype
))
1006 srcvar
= fold_build2 (MEM_REF
, destvar
? desttype
: srctype
,
1008 else if (!STRICT_ALIGNMENT
)
1010 srctype
= build_aligned_type (TYPE_MAIN_VARIANT (desttype
),
1012 srcvar
= fold_build2 (MEM_REF
, srctype
, src
, off0
);
1016 if (srcvar
== NULL_TREE
&& destvar
== NULL_TREE
)
1019 if (srcvar
== NULL_TREE
)
1021 if (src_align
>= TYPE_ALIGN (desttype
))
1022 srcvar
= fold_build2 (MEM_REF
, desttype
, src
, off0
);
1025 if (STRICT_ALIGNMENT
)
1027 srctype
= build_aligned_type (TYPE_MAIN_VARIANT (desttype
),
1029 srcvar
= fold_build2 (MEM_REF
, srctype
, src
, off0
);
1032 else if (destvar
== NULL_TREE
)
1034 if (dest_align
>= TYPE_ALIGN (srctype
))
1035 destvar
= fold_build2 (MEM_REF
, srctype
, dest
, off0
);
1038 if (STRICT_ALIGNMENT
)
1040 desttype
= build_aligned_type (TYPE_MAIN_VARIANT (srctype
),
1042 destvar
= fold_build2 (MEM_REF
, desttype
, dest
, off0
);
1046 /* Same as above, detect out-of-bounds accesses without issuing
1047 warnings. Avoid folding out-of-bounds copies but to avoid
1048 false positives for unreachable code defer warning until
1049 after DCE has worked its magic.
1050 -Wrestrict is still diagnosed. */
1051 if (int warning
= check_bounds_or_overlap (as_a
<gcall
*>(stmt
),
1052 dest
, src
, len
, len
,
1054 if (warning
!= OPT_Wrestrict
)
1058 if (is_gimple_reg_type (TREE_TYPE (srcvar
)))
1060 tree tem
= fold_const_aggregate_ref (srcvar
);
1063 if (! is_gimple_min_invariant (srcvar
))
1065 new_stmt
= gimple_build_assign (NULL_TREE
, srcvar
);
1066 srcvar
= create_tmp_reg_or_ssa_name (TREE_TYPE (srcvar
),
1068 gimple_assign_set_lhs (new_stmt
, srcvar
);
1069 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
1070 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
1072 new_stmt
= gimple_build_assign (destvar
, srcvar
);
1073 goto set_vop_and_replace
;
1076 /* We get an aggregate copy. Use an unsigned char[] type to
1077 perform the copying to preserve padding and to avoid any issues
1078 with TREE_ADDRESSABLE types or float modes behavior on copying. */
1079 desttype
= build_array_type_nelts (unsigned_char_type_node
,
1080 tree_to_uhwi (len
));
1082 if (src_align
> TYPE_ALIGN (srctype
))
1083 srctype
= build_aligned_type (srctype
, src_align
);
1084 if (dest_align
> TYPE_ALIGN (desttype
))
1085 desttype
= build_aligned_type (desttype
, dest_align
);
1087 = gimple_build_assign (fold_build2 (MEM_REF
, desttype
, dest
, off0
),
1088 fold_build2 (MEM_REF
, srctype
, src
, off0
));
1089 set_vop_and_replace
:
1090 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
1091 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
1092 if (gimple_vdef (new_stmt
)
1093 && TREE_CODE (gimple_vdef (new_stmt
)) == SSA_NAME
)
1094 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt
)) = new_stmt
;
1097 gsi_replace (gsi
, new_stmt
, false);
1100 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
1104 gimple_seq stmts
= NULL
;
1105 if (endp
== 0 || endp
== 3)
1108 len
= gimple_build (&stmts
, loc
, MINUS_EXPR
, TREE_TYPE (len
), len
,
1110 if (endp
== 2 || endp
== 1)
1112 len
= gimple_convert_to_ptrofftype (&stmts
, loc
, len
);
1113 dest
= gimple_build (&stmts
, loc
, POINTER_PLUS_EXPR
,
1114 TREE_TYPE (dest
), dest
, len
);
1117 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1118 gimple
*repl
= gimple_build_assign (lhs
, dest
);
1119 gsi_replace (gsi
, repl
, false);
1123 /* Transform a call to built-in bcmp(a, b, len) at *GSI into one
1124 to built-in memcmp (a, b, len). */
1127 gimple_fold_builtin_bcmp (gimple_stmt_iterator
*gsi
)
1129 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCMP
);
1134 /* Transform bcmp (a, b, len) into memcmp (a, b, len). */
1136 gimple
*stmt
= gsi_stmt (*gsi
);
1137 tree a
= gimple_call_arg (stmt
, 0);
1138 tree b
= gimple_call_arg (stmt
, 1);
1139 tree len
= gimple_call_arg (stmt
, 2);
1141 gimple
*repl
= gimple_build_call (fn
, 3, a
, b
, len
);
1142 replace_call_with_call_and_fold (gsi
, repl
);
1147 /* Transform a call to built-in bcopy (src, dest, len) at *GSI into one
1148 to built-in memmove (dest, src, len). */
1151 gimple_fold_builtin_bcopy (gimple_stmt_iterator
*gsi
)
1153 tree fn
= builtin_decl_implicit (BUILT_IN_MEMMOVE
);
1158 /* bcopy has been removed from POSIX in Issue 7 but Issue 6 specifies
1159 it's quivalent to memmove (not memcpy). Transform bcopy (src, dest,
1160 len) into memmove (dest, src, len). */
1162 gimple
*stmt
= gsi_stmt (*gsi
);
1163 tree src
= gimple_call_arg (stmt
, 0);
1164 tree dest
= gimple_call_arg (stmt
, 1);
1165 tree len
= gimple_call_arg (stmt
, 2);
1167 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1168 gimple_call_set_fntype (as_a
<gcall
*> (stmt
), TREE_TYPE (fn
));
1169 replace_call_with_call_and_fold (gsi
, repl
);
1174 /* Transform a call to built-in bzero (dest, len) at *GSI into one
1175 to built-in memset (dest, 0, len). */
1178 gimple_fold_builtin_bzero (gimple_stmt_iterator
*gsi
)
1180 tree fn
= builtin_decl_implicit (BUILT_IN_MEMSET
);
1185 /* Transform bzero (dest, len) into memset (dest, 0, len). */
1187 gimple
*stmt
= gsi_stmt (*gsi
);
1188 tree dest
= gimple_call_arg (stmt
, 0);
1189 tree len
= gimple_call_arg (stmt
, 1);
1191 gimple_seq seq
= NULL
;
1192 gimple
*repl
= gimple_build_call (fn
, 3, dest
, integer_zero_node
, len
);
1193 gimple_seq_add_stmt_without_update (&seq
, repl
);
1194 gsi_replace_with_seq_vops (gsi
, seq
);
1200 /* Fold function call to builtin memset or bzero at *GSI setting the
1201 memory of size LEN to VAL. Return whether a simplification was made. */
1204 gimple_fold_builtin_memset (gimple_stmt_iterator
*gsi
, tree c
, tree len
)
1206 gimple
*stmt
= gsi_stmt (*gsi
);
1208 unsigned HOST_WIDE_INT length
, cval
;
1210 /* If the LEN parameter is zero, return DEST. */
1211 if (integer_zerop (len
))
1213 replace_call_with_value (gsi
, gimple_call_arg (stmt
, 0));
1217 if (! tree_fits_uhwi_p (len
))
1220 if (TREE_CODE (c
) != INTEGER_CST
)
1223 tree dest
= gimple_call_arg (stmt
, 0);
1225 if (TREE_CODE (var
) != ADDR_EXPR
)
1228 var
= TREE_OPERAND (var
, 0);
1229 if (TREE_THIS_VOLATILE (var
))
1232 etype
= TREE_TYPE (var
);
1233 if (TREE_CODE (etype
) == ARRAY_TYPE
)
1234 etype
= TREE_TYPE (etype
);
1236 if (!INTEGRAL_TYPE_P (etype
)
1237 && !POINTER_TYPE_P (etype
))
1240 if (! var_decl_component_p (var
))
1243 length
= tree_to_uhwi (len
);
1244 if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (etype
)) != length
1245 || get_pointer_alignment (dest
) / BITS_PER_UNIT
< length
)
1248 if (length
> HOST_BITS_PER_WIDE_INT
/ BITS_PER_UNIT
)
1251 if (integer_zerop (c
))
1255 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8 || HOST_BITS_PER_WIDE_INT
> 64)
1258 cval
= TREE_INT_CST_LOW (c
);
1262 cval
|= (cval
<< 31) << 1;
1265 var
= fold_build2 (MEM_REF
, etype
, dest
, build_int_cst (ptr_type_node
, 0));
1266 gimple
*store
= gimple_build_assign (var
, build_int_cst_type (etype
, cval
));
1267 gimple_set_vuse (store
, gimple_vuse (stmt
));
1268 tree vdef
= gimple_vdef (stmt
);
1269 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
1271 gimple_set_vdef (store
, gimple_vdef (stmt
));
1272 SSA_NAME_DEF_STMT (gimple_vdef (stmt
)) = store
;
1274 gsi_insert_before (gsi
, store
, GSI_SAME_STMT
);
1275 if (gimple_call_lhs (stmt
))
1277 gimple
*asgn
= gimple_build_assign (gimple_call_lhs (stmt
), dest
);
1278 gsi_replace (gsi
, asgn
, false);
1282 gimple_stmt_iterator gsi2
= *gsi
;
1284 gsi_remove (&gsi2
, true);
1290 /* Helper of get_range_strlen for ARG that is not an SSA_NAME. */
1293 get_range_strlen_tree (tree arg
, bitmap
*visited
, strlen_range_kind rkind
,
1294 c_strlen_data
*pdata
, unsigned eltsize
)
1296 gcc_assert (TREE_CODE (arg
) != SSA_NAME
);
1298 /* The length computed by this invocation of the function. */
1299 tree val
= NULL_TREE
;
1301 /* True if VAL is an optimistic (tight) bound determined from
1302 the size of the character array in which the string may be
1303 stored. In that case, the computed VAL is used to set
1305 bool tight_bound
= false;
1307 /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */
1308 if (TREE_CODE (arg
) == ADDR_EXPR
1309 && TREE_CODE (TREE_OPERAND (arg
, 0)) == ARRAY_REF
)
1311 tree op
= TREE_OPERAND (arg
, 0);
1312 if (integer_zerop (TREE_OPERAND (op
, 1)))
1314 tree aop0
= TREE_OPERAND (op
, 0);
1315 if (TREE_CODE (aop0
) == INDIRECT_REF
1316 && TREE_CODE (TREE_OPERAND (aop0
, 0)) == SSA_NAME
)
1317 return get_range_strlen (TREE_OPERAND (aop0
, 0), visited
, rkind
,
1320 else if (TREE_CODE (TREE_OPERAND (op
, 0)) == COMPONENT_REF
1321 && rkind
== SRK_LENRANGE
)
1323 /* Fail if an array is the last member of a struct object
1324 since it could be treated as a (fake) flexible array
1326 tree idx
= TREE_OPERAND (op
, 1);
1328 arg
= TREE_OPERAND (op
, 0);
1329 tree optype
= TREE_TYPE (arg
);
1330 if (tree dom
= TYPE_DOMAIN (optype
))
1331 if (tree bound
= TYPE_MAX_VALUE (dom
))
1332 if (TREE_CODE (bound
) == INTEGER_CST
1333 && TREE_CODE (idx
) == INTEGER_CST
1334 && tree_int_cst_lt (bound
, idx
))
1339 if (rkind
== SRK_INT_VALUE
)
1341 /* We are computing the maximum value (not string length). */
1343 if (TREE_CODE (val
) != INTEGER_CST
1344 || tree_int_cst_sgn (val
) < 0)
1349 c_strlen_data lendata
= { };
1350 val
= c_strlen (arg
, 1, &lendata
, eltsize
);
1352 if (!val
&& lendata
.decl
)
1354 /* ARG refers to an unterminated const character array.
1355 DATA.DECL with size DATA.LEN. */
1356 val
= lendata
.minlen
;
1357 pdata
->decl
= lendata
.decl
;
1361 if (!val
&& rkind
== SRK_LENRANGE
)
1363 if (TREE_CODE (arg
) == ADDR_EXPR
)
1364 return get_range_strlen (TREE_OPERAND (arg
, 0), visited
, rkind
,
1367 if (TREE_CODE (arg
) == ARRAY_REF
)
1369 tree optype
= TREE_TYPE (TREE_OPERAND (arg
, 0));
1371 /* Determine the "innermost" array type. */
1372 while (TREE_CODE (optype
) == ARRAY_TYPE
1373 && TREE_CODE (TREE_TYPE (optype
)) == ARRAY_TYPE
)
1374 optype
= TREE_TYPE (optype
);
1376 /* Avoid arrays of pointers. */
1377 tree eltype
= TREE_TYPE (optype
);
1378 if (TREE_CODE (optype
) != ARRAY_TYPE
1379 || !INTEGRAL_TYPE_P (eltype
))
1382 /* Fail when the array bound is unknown or zero. */
1383 val
= TYPE_SIZE_UNIT (optype
);
1384 if (!val
|| integer_zerop (val
))
1387 val
= fold_build2 (MINUS_EXPR
, TREE_TYPE (val
), val
,
1390 /* Set the minimum size to zero since the string in
1391 the array could have zero length. */
1392 pdata
->minlen
= ssize_int (0);
1396 else if (TREE_CODE (arg
) == COMPONENT_REF
1397 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (arg
, 1)))
1400 /* Use the type of the member array to determine the upper
1401 bound on the length of the array. This may be overly
1402 optimistic if the array itself isn't NUL-terminated and
1403 the caller relies on the subsequent member to contain
1404 the NUL but that would only be considered valid if
1405 the array were the last member of a struct. */
1407 tree fld
= TREE_OPERAND (arg
, 1);
1409 tree optype
= TREE_TYPE (fld
);
1411 /* Determine the "innermost" array type. */
1412 while (TREE_CODE (optype
) == ARRAY_TYPE
1413 && TREE_CODE (TREE_TYPE (optype
)) == ARRAY_TYPE
)
1414 optype
= TREE_TYPE (optype
);
1416 /* Fail when the array bound is unknown or zero. */
1417 val
= TYPE_SIZE_UNIT (optype
);
1418 if (!val
|| integer_zerop (val
))
1420 val
= fold_build2 (MINUS_EXPR
, TREE_TYPE (val
), val
,
1423 /* Set the minimum size to zero since the string in
1424 the array could have zero length. */
1425 pdata
->minlen
= ssize_int (0);
1427 /* The array size determined above is an optimistic bound
1428 on the length. If the array isn't nul-terminated the
1429 length computed by the library function would be greater.
1430 Even though using strlen to cross the subobject boundary
1431 is undefined, avoid drawing conclusions from the member
1432 type about the length here. */
1435 else if (VAR_P (arg
))
1437 /* Avoid handling pointers to arrays. GCC might misuse
1438 a pointer to an array of one bound to point to an array
1439 object of a greater bound. */
1440 tree argtype
= TREE_TYPE (arg
);
1441 if (TREE_CODE (argtype
) == ARRAY_TYPE
)
1443 val
= TYPE_SIZE_UNIT (argtype
);
1445 || TREE_CODE (val
) != INTEGER_CST
1446 || integer_zerop (val
))
1448 val
= wide_int_to_tree (TREE_TYPE (val
),
1449 wi::sub (wi::to_wide (val
), 1));
1451 /* Set the minimum size to zero since the string in
1452 the array could have zero length. */
1453 pdata
->minlen
= ssize_int (0);
1461 /* Adjust the lower bound on the string length as necessary. */
1463 || (rkind
!= SRK_STRLEN
1464 && TREE_CODE (pdata
->minlen
) == INTEGER_CST
1465 && TREE_CODE (val
) == INTEGER_CST
1466 && tree_int_cst_lt (val
, pdata
->minlen
)))
1467 pdata
->minlen
= val
;
1469 if (pdata
->maxbound
)
1471 /* Adjust the tighter (more optimistic) string length bound
1472 if necessary and proceed to adjust the more conservative
1474 if (TREE_CODE (val
) == INTEGER_CST
)
1476 if (TREE_CODE (pdata
->maxbound
) == INTEGER_CST
)
1478 if (tree_int_cst_lt (pdata
->maxbound
, val
))
1479 pdata
->maxbound
= val
;
1482 pdata
->maxbound
= build_all_ones_cst (size_type_node
);
1485 pdata
->maxbound
= val
;
1488 pdata
->maxbound
= val
;
1492 /* VAL computed above represents an optimistically tight bound
1493 on the length of the string based on the referenced object's
1494 or subobject's type. Determine the conservative upper bound
1495 based on the enclosing object's size if possible. */
1496 if (rkind
== SRK_LENRANGE
)
1499 tree base
= get_addr_base_and_unit_offset (arg
, &offset
);
1502 /* When the call above fails due to a non-constant offset
1503 assume the offset is zero and use the size of the whole
1504 enclosing object instead. */
1505 base
= get_base_address (arg
);
1508 /* If the base object is a pointer no upper bound on the length
1509 can be determined. Otherwise the maximum length is equal to
1510 the size of the enclosing object minus the offset of
1511 the referenced subobject minus 1 (for the terminating nul). */
1512 tree type
= TREE_TYPE (base
);
1513 if (TREE_CODE (type
) == POINTER_TYPE
1514 || !VAR_P (base
) || !(val
= DECL_SIZE_UNIT (base
)))
1515 val
= build_all_ones_cst (size_type_node
);
1518 val
= DECL_SIZE_UNIT (base
);
1519 val
= fold_build2 (MINUS_EXPR
, TREE_TYPE (val
), val
,
1520 size_int (offset
+ 1));
1529 /* Adjust the more conservative bound if possible/necessary
1530 and fail otherwise. */
1531 if (rkind
!= SRK_STRLEN
)
1533 if (TREE_CODE (pdata
->maxlen
) != INTEGER_CST
1534 || TREE_CODE (val
) != INTEGER_CST
)
1537 if (tree_int_cst_lt (pdata
->maxlen
, val
))
1538 pdata
->maxlen
= val
;
1541 else if (simple_cst_equal (val
, pdata
->maxlen
) != 1)
1543 /* Fail if the length of this ARG is different from that
1544 previously determined from another ARG. */
1549 pdata
->maxlen
= val
;
1550 return rkind
== SRK_LENRANGE
|| !integer_all_onesp (val
);
1553 /* For an ARG referencing one or more strings, try to obtain the range
1554 of their lengths, or the size of the largest array ARG referes to if
1555 the range of lengths cannot be determined, and store all in *PDATA.
1556 For an integer ARG (when RKIND == SRK_INT_VALUE), try to determine
1557 the maximum constant value.
1558 If ARG is an SSA_NAME, follow its use-def chains. When RKIND ==
1559 SRK_STRLEN, then if PDATA->MAXLEN is not equal to the determined
1560 length or if we are unable to determine the length, return false.
1561 VISITED is a bitmap of visited variables.
1562 RKIND determines the kind of value or range to obtain (see
1564 Set PDATA->DECL if ARG refers to an unterminated constant array.
1565 On input, set ELTSIZE to 1 for normal single byte character strings,
1566 and either 2 or 4 for wide characer strings (the size of wchar_t).
1567 Return true if *PDATA was successfully populated and false otherwise. */
1570 get_range_strlen (tree arg
, bitmap
*visited
,
1571 strlen_range_kind rkind
,
1572 c_strlen_data
*pdata
, unsigned eltsize
)
1575 if (TREE_CODE (arg
) != SSA_NAME
)
1576 return get_range_strlen_tree (arg
, visited
, rkind
, pdata
, eltsize
);
1578 /* If ARG is registered for SSA update we cannot look at its defining
1580 if (name_registered_for_update_p (arg
))
1583 /* If we were already here, break the infinite cycle. */
1585 *visited
= BITMAP_ALLOC (NULL
);
1586 if (!bitmap_set_bit (*visited
, SSA_NAME_VERSION (arg
)))
1590 gimple
*def_stmt
= SSA_NAME_DEF_STMT (var
);
1592 switch (gimple_code (def_stmt
))
1595 /* The RHS of the statement defining VAR must either have a
1596 constant length or come from another SSA_NAME with a constant
1598 if (gimple_assign_single_p (def_stmt
)
1599 || gimple_assign_unary_nop_p (def_stmt
))
1601 tree rhs
= gimple_assign_rhs1 (def_stmt
);
1602 return get_range_strlen (rhs
, visited
, rkind
, pdata
, eltsize
);
1604 else if (gimple_assign_rhs_code (def_stmt
) == COND_EXPR
)
1606 tree ops
[2] = { gimple_assign_rhs2 (def_stmt
),
1607 gimple_assign_rhs3 (def_stmt
) };
1609 for (unsigned int i
= 0; i
< 2; i
++)
1610 if (!get_range_strlen (ops
[i
], visited
, rkind
, pdata
, eltsize
))
1612 if (rkind
!= SRK_LENRANGE
)
1614 /* Set the upper bound to the maximum to prevent
1615 it from being adjusted in the next iteration but
1616 leave MINLEN and the more conservative MAXBOUND
1617 determined so far alone (or leave them null if
1618 they haven't been set yet). That the MINLEN is
1619 in fact zero can be determined from MAXLEN being
1620 unbounded but the discovered minimum is used for
1622 pdata
->maxlen
= build_all_ones_cst (size_type_node
);
1629 /* Unless RKIND == SRK_LENRANGE, all arguments of the PHI node
1630 must have a constant length. */
1631 for (unsigned i
= 0; i
< gimple_phi_num_args (def_stmt
); i
++)
1633 tree arg
= gimple_phi_arg (def_stmt
, i
)->def
;
1635 /* If this PHI has itself as an argument, we cannot
1636 determine the string length of this argument. However,
1637 if we can find a constant string length for the other
1638 PHI args then we can still be sure that this is a
1639 constant string length. So be optimistic and just
1640 continue with the next argument. */
1641 if (arg
== gimple_phi_result (def_stmt
))
1644 if (!get_range_strlen (arg
, visited
, rkind
, pdata
, eltsize
))
1646 if (rkind
!= SRK_LENRANGE
)
1648 /* Set the upper bound to the maximum to prevent
1649 it from being adjusted in the next iteration but
1650 leave MINLEN and the more conservative MAXBOUND
1651 determined so far alone (or leave them null if
1652 they haven't been set yet). That the MINLEN is
1653 in fact zero can be determined from MAXLEN being
1654 unbounded but the discovered minimum is used for
1656 pdata
->maxlen
= build_all_ones_cst (size_type_node
);
1666 /* Determine the minimum and maximum value or string length that ARG
1667 refers to and store each in the first two elements of MINMAXLEN.
1668 For expressions that point to strings of unknown lengths that are
1669 character arrays, use the upper bound of the array as the maximum
1670 length. For example, given an expression like 'x ? array : "xyz"'
1671 and array declared as 'char array[8]', MINMAXLEN[0] will be set
1672 to 0 and MINMAXLEN[1] to 7, the longest string that could be
1674 Return true if the range of the string lengths has been obtained
1675 from the upper bound of an array at the end of a struct. Such
1676 an array may hold a string that's longer than its upper bound
1677 due to it being used as a poor-man's flexible array member.
1679 STRICT is true if it will handle PHIs and COND_EXPRs conservatively
1680 and false if PHIs and COND_EXPRs are to be handled optimistically,
1681 if we can determine string length minimum and maximum; it will use
1682 the minimum from the ones where it can be determined.
1683 STRICT false should be only used for warning code.
1684 When non-null, clear *NONSTR if ARG refers to a constant array
1685 that is known not be nul-terminated. Otherwise set it to
1686 the declaration of the constant non-terminated array.
1688 ELTSIZE is 1 for normal single byte character strings, and 2 or
1689 4 for wide characer strings. ELTSIZE is by default 1. */
1692 get_range_strlen (tree arg
, c_strlen_data
*pdata
, unsigned eltsize
)
1694 bitmap visited
= NULL
;
1696 if (!get_range_strlen (arg
, &visited
, SRK_LENRANGE
, pdata
, eltsize
))
1698 /* On failure extend the length range to an impossible maximum
1699 (a valid MAXLEN must be less than PTRDIFF_MAX - 1). Other
1700 members can stay unchanged regardless. */
1701 pdata
->minlen
= ssize_int (0);
1702 pdata
->maxlen
= build_all_ones_cst (size_type_node
);
1704 else if (!pdata
->minlen
)
1705 pdata
->minlen
= ssize_int (0);
1707 /* Unless its null, leave the more conservative MAXBOUND unchanged. */
1708 if (!pdata
->maxbound
)
1709 pdata
->maxbound
= pdata
->maxlen
;
1712 BITMAP_FREE (visited
);
1714 return !integer_all_onesp (pdata
->maxlen
);
1717 /* Return the maximum value for ARG given RKIND (see strlen_range_kind).
1718 For ARG of pointer types, NONSTR indicates if the caller is prepared
1719 to handle unterminated strings. For integer ARG and when RKIND ==
1720 SRK_INT_VALUE, NONSTR must be null.
1722 If an unterminated array is discovered and our caller handles
1723 unterminated arrays, then bubble up the offending DECL and
1724 return the maximum size. Otherwise return NULL. */
1727 get_maxval_strlen (tree arg
, strlen_range_kind rkind
, tree
*nonstr
= NULL
)
1729 /* A non-null NONSTR is meaningless when determining the maximum
1730 value of an integer ARG. */
1731 gcc_assert (rkind
!= SRK_INT_VALUE
|| nonstr
== NULL
);
1732 /* ARG must have an integral type when RKIND says so. */
1733 gcc_assert (rkind
!= SRK_INT_VALUE
|| INTEGRAL_TYPE_P (TREE_TYPE (arg
)));
1735 bitmap visited
= NULL
;
1737 /* Reset DATA.MAXLEN if the call fails or when DATA.MAXLEN
1739 c_strlen_data lendata
= { };
1740 if (!get_range_strlen (arg
, &visited
, rkind
, &lendata
, /* eltsize = */1))
1741 lendata
.maxlen
= NULL_TREE
;
1742 else if (lendata
.maxlen
&& integer_all_onesp (lendata
.maxlen
))
1743 lendata
.maxlen
= NULL_TREE
;
1746 BITMAP_FREE (visited
);
1750 /* For callers prepared to handle unterminated arrays set
1751 *NONSTR to point to the declaration of the array and return
1752 the maximum length/size. */
1753 *nonstr
= lendata
.decl
;
1754 return lendata
.maxlen
;
1757 /* Fail if the constant array isn't nul-terminated. */
1758 return lendata
.decl
? NULL_TREE
: lendata
.maxlen
;
1762 /* Fold function call to builtin strcpy with arguments DEST and SRC.
1763 If LEN is not NULL, it represents the length of the string to be
1764 copied. Return NULL_TREE if no simplification can be made. */
1767 gimple_fold_builtin_strcpy (gimple_stmt_iterator
*gsi
,
1768 tree dest
, tree src
)
1770 gimple
*stmt
= gsi_stmt (*gsi
);
1771 location_t loc
= gimple_location (stmt
);
1774 /* If SRC and DEST are the same (and not volatile), return DEST. */
1775 if (operand_equal_p (src
, dest
, 0))
1777 /* Issue -Wrestrict unless the pointers are null (those do
1778 not point to objects and so do not indicate an overlap;
1779 such calls could be the result of sanitization and jump
1781 if (!integer_zerop (dest
) && !gimple_no_warning_p (stmt
))
1783 tree func
= gimple_call_fndecl (stmt
);
1785 warning_at (loc
, OPT_Wrestrict
,
1786 "%qD source argument is the same as destination",
1790 replace_call_with_value (gsi
, dest
);
1794 if (optimize_function_for_size_p (cfun
))
1797 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
1801 /* Set to non-null if ARG refers to an unterminated array. */
1803 tree len
= get_maxval_strlen (src
, SRK_STRLEN
, &nonstr
);
1807 /* Avoid folding calls with unterminated arrays. */
1808 if (!gimple_no_warning_p (stmt
))
1809 warn_string_no_nul (loc
, "strcpy", src
, nonstr
);
1810 gimple_set_no_warning (stmt
, true);
1817 len
= fold_convert_loc (loc
, size_type_node
, len
);
1818 len
= size_binop_loc (loc
, PLUS_EXPR
, len
, build_int_cst (size_type_node
, 1));
1819 len
= force_gimple_operand_gsi (gsi
, len
, true,
1820 NULL_TREE
, true, GSI_SAME_STMT
);
1821 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1822 replace_call_with_call_and_fold (gsi
, repl
);
1826 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
1827 If SLEN is not NULL, it represents the length of the source string.
1828 Return NULL_TREE if no simplification can be made. */
1831 gimple_fold_builtin_strncpy (gimple_stmt_iterator
*gsi
,
1832 tree dest
, tree src
, tree len
)
1834 gimple
*stmt
= gsi_stmt (*gsi
);
1835 location_t loc
= gimple_location (stmt
);
1836 bool nonstring
= get_attr_nonstring_decl (dest
) != NULL_TREE
;
1838 /* If the LEN parameter is zero, return DEST. */
1839 if (integer_zerop (len
))
1841 /* Avoid warning if the destination refers to a an array/pointer
1842 decorate with attribute nonstring. */
1845 tree fndecl
= gimple_call_fndecl (stmt
);
1847 /* Warn about the lack of nul termination: the result is not
1848 a (nul-terminated) string. */
1849 tree slen
= get_maxval_strlen (src
, SRK_STRLEN
);
1850 if (slen
&& !integer_zerop (slen
))
1851 warning_at (loc
, OPT_Wstringop_truncation
,
1852 "%G%qD destination unchanged after copying no bytes "
1853 "from a string of length %E",
1854 stmt
, fndecl
, slen
);
1856 warning_at (loc
, OPT_Wstringop_truncation
,
1857 "%G%qD destination unchanged after copying no bytes",
1861 replace_call_with_value (gsi
, dest
);
1865 /* We can't compare slen with len as constants below if len is not a
1867 if (TREE_CODE (len
) != INTEGER_CST
)
1870 /* Now, we must be passed a constant src ptr parameter. */
1871 tree slen
= get_maxval_strlen (src
, SRK_STRLEN
);
1872 if (!slen
|| TREE_CODE (slen
) != INTEGER_CST
)
1875 /* The size of the source string including the terminating nul. */
1876 tree ssize
= size_binop_loc (loc
, PLUS_EXPR
, slen
, ssize_int (1));
1878 /* We do not support simplification of this case, though we do
1879 support it when expanding trees into RTL. */
1880 /* FIXME: generate a call to __builtin_memset. */
1881 if (tree_int_cst_lt (ssize
, len
))
1884 /* Diagnose truncation that leaves the copy unterminated. */
1885 maybe_diag_stxncpy_trunc (*gsi
, src
, len
);
1887 /* OK transform into builtin memcpy. */
1888 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
1892 len
= fold_convert_loc (loc
, size_type_node
, len
);
1893 len
= force_gimple_operand_gsi (gsi
, len
, true,
1894 NULL_TREE
, true, GSI_SAME_STMT
);
1895 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1896 replace_call_with_call_and_fold (gsi
, repl
);
1901 /* Fold function call to builtin strchr or strrchr.
1902 If both arguments are constant, evaluate and fold the result,
1903 otherwise simplify str(r)chr (str, 0) into str + strlen (str).
1904 In general strlen is significantly faster than strchr
1905 due to being a simpler operation. */
1907 gimple_fold_builtin_strchr (gimple_stmt_iterator
*gsi
, bool is_strrchr
)
1909 gimple
*stmt
= gsi_stmt (*gsi
);
1910 tree str
= gimple_call_arg (stmt
, 0);
1911 tree c
= gimple_call_arg (stmt
, 1);
1912 location_t loc
= gimple_location (stmt
);
1916 if (!gimple_call_lhs (stmt
))
1919 if ((p
= c_getstr (str
)) && target_char_cst_p (c
, &ch
))
1921 const char *p1
= is_strrchr
? strrchr (p
, ch
) : strchr (p
, ch
);
1925 replace_call_with_value (gsi
, integer_zero_node
);
1929 tree len
= build_int_cst (size_type_node
, p1
- p
);
1930 gimple_seq stmts
= NULL
;
1931 gimple
*new_stmt
= gimple_build_assign (gimple_call_lhs (stmt
),
1932 POINTER_PLUS_EXPR
, str
, len
);
1933 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
1934 gsi_replace_with_seq_vops (gsi
, stmts
);
1938 if (!integer_zerop (c
))
1941 /* Transform strrchr (s, 0) to strchr (s, 0) when optimizing for size. */
1942 if (is_strrchr
&& optimize_function_for_size_p (cfun
))
1944 tree strchr_fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
1948 gimple
*repl
= gimple_build_call (strchr_fn
, 2, str
, c
);
1949 replace_call_with_call_and_fold (gsi
, repl
);
1957 tree strlen_fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
1962 /* Create newstr = strlen (str). */
1963 gimple_seq stmts
= NULL
;
1964 gimple
*new_stmt
= gimple_build_call (strlen_fn
, 1, str
);
1965 gimple_set_location (new_stmt
, loc
);
1966 len
= create_tmp_reg_or_ssa_name (size_type_node
);
1967 gimple_call_set_lhs (new_stmt
, len
);
1968 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
1970 /* Create (str p+ strlen (str)). */
1971 new_stmt
= gimple_build_assign (gimple_call_lhs (stmt
),
1972 POINTER_PLUS_EXPR
, str
, len
);
1973 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
1974 gsi_replace_with_seq_vops (gsi
, stmts
);
1975 /* gsi now points at the assignment to the lhs, get a
1976 stmt iterator to the strlen.
1977 ??? We can't use gsi_for_stmt as that doesn't work when the
1978 CFG isn't built yet. */
1979 gimple_stmt_iterator gsi2
= *gsi
;
1985 /* Fold function call to builtin strstr.
1986 If both arguments are constant, evaluate and fold the result,
1987 additionally fold strstr (x, "") into x and strstr (x, "c")
1988 into strchr (x, 'c'). */
1990 gimple_fold_builtin_strstr (gimple_stmt_iterator
*gsi
)
1992 gimple
*stmt
= gsi_stmt (*gsi
);
1993 tree haystack
= gimple_call_arg (stmt
, 0);
1994 tree needle
= gimple_call_arg (stmt
, 1);
1997 if (!gimple_call_lhs (stmt
))
2000 q
= c_getstr (needle
);
2004 if ((p
= c_getstr (haystack
)))
2006 const char *r
= strstr (p
, q
);
2010 replace_call_with_value (gsi
, integer_zero_node
);
2014 tree len
= build_int_cst (size_type_node
, r
- p
);
2015 gimple_seq stmts
= NULL
;
2017 = gimple_build_assign (gimple_call_lhs (stmt
), POINTER_PLUS_EXPR
,
2019 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
2020 gsi_replace_with_seq_vops (gsi
, stmts
);
2024 /* For strstr (x, "") return x. */
2027 replace_call_with_value (gsi
, haystack
);
2031 /* Transform strstr (x, "c") into strchr (x, 'c'). */
2034 tree strchr_fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
2037 tree c
= build_int_cst (integer_type_node
, q
[0]);
2038 gimple
*repl
= gimple_build_call (strchr_fn
, 2, haystack
, c
);
2039 replace_call_with_call_and_fold (gsi
, repl
);
2047 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
2050 Return NULL_TREE if no simplification was possible, otherwise return the
2051 simplified form of the call as a tree.
2053 The simplified form may be a constant or other expression which
2054 computes the same value, but in a more efficient manner (including
2055 calls to other builtin functions).
2057 The call may contain arguments which need to be evaluated, but
2058 which are not useful to determine the result of the call. In
2059 this case we return a chain of COMPOUND_EXPRs. The LHS of each
2060 COMPOUND_EXPR will be an argument which must be evaluated.
2061 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
2062 COMPOUND_EXPR in the chain will contain the tree for the simplified
2063 form of the builtin function call. */
2066 gimple_fold_builtin_strcat (gimple_stmt_iterator
*gsi
, tree dst
, tree src
)
2068 gimple
*stmt
= gsi_stmt (*gsi
);
2069 location_t loc
= gimple_location (stmt
);
2071 const char *p
= c_getstr (src
);
2073 /* If the string length is zero, return the dst parameter. */
2074 if (p
&& *p
== '\0')
2076 replace_call_with_value (gsi
, dst
);
2080 if (!optimize_bb_for_speed_p (gimple_bb (stmt
)))
2083 /* See if we can store by pieces into (dst + strlen(dst)). */
2085 tree strlen_fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
2086 tree memcpy_fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
2088 if (!strlen_fn
|| !memcpy_fn
)
2091 /* If the length of the source string isn't computable don't
2092 split strcat into strlen and memcpy. */
2093 tree len
= get_maxval_strlen (src
, SRK_STRLEN
);
2097 /* Create strlen (dst). */
2098 gimple_seq stmts
= NULL
, stmts2
;
2099 gimple
*repl
= gimple_build_call (strlen_fn
, 1, dst
);
2100 gimple_set_location (repl
, loc
);
2101 newdst
= create_tmp_reg_or_ssa_name (size_type_node
);
2102 gimple_call_set_lhs (repl
, newdst
);
2103 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2105 /* Create (dst p+ strlen (dst)). */
2106 newdst
= fold_build_pointer_plus_loc (loc
, dst
, newdst
);
2107 newdst
= force_gimple_operand (newdst
, &stmts2
, true, NULL_TREE
);
2108 gimple_seq_add_seq_without_update (&stmts
, stmts2
);
2110 len
= fold_convert_loc (loc
, size_type_node
, len
);
2111 len
= size_binop_loc (loc
, PLUS_EXPR
, len
,
2112 build_int_cst (size_type_node
, 1));
2113 len
= force_gimple_operand (len
, &stmts2
, true, NULL_TREE
);
2114 gimple_seq_add_seq_without_update (&stmts
, stmts2
);
2116 repl
= gimple_build_call (memcpy_fn
, 3, newdst
, src
, len
);
2117 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2118 if (gimple_call_lhs (stmt
))
2120 repl
= gimple_build_assign (gimple_call_lhs (stmt
), dst
);
2121 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2122 gsi_replace_with_seq_vops (gsi
, stmts
);
2123 /* gsi now points at the assignment to the lhs, get a
2124 stmt iterator to the memcpy call.
2125 ??? We can't use gsi_for_stmt as that doesn't work when the
2126 CFG isn't built yet. */
2127 gimple_stmt_iterator gsi2
= *gsi
;
2133 gsi_replace_with_seq_vops (gsi
, stmts
);
2139 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
2140 are the arguments to the call. */
2143 gimple_fold_builtin_strcat_chk (gimple_stmt_iterator
*gsi
)
2145 gimple
*stmt
= gsi_stmt (*gsi
);
2146 tree dest
= gimple_call_arg (stmt
, 0);
2147 tree src
= gimple_call_arg (stmt
, 1);
2148 tree size
= gimple_call_arg (stmt
, 2);
2154 /* If the SRC parameter is "", return DEST. */
2155 if (p
&& *p
== '\0')
2157 replace_call_with_value (gsi
, dest
);
2161 if (! tree_fits_uhwi_p (size
) || ! integer_all_onesp (size
))
2164 /* If __builtin_strcat_chk is used, assume strcat is available. */
2165 fn
= builtin_decl_explicit (BUILT_IN_STRCAT
);
2169 gimple
*repl
= gimple_build_call (fn
, 2, dest
, src
);
2170 replace_call_with_call_and_fold (gsi
, repl
);
2174 /* Simplify a call to the strncat builtin. */
2177 gimple_fold_builtin_strncat (gimple_stmt_iterator
*gsi
)
2179 gimple
*stmt
= gsi_stmt (*gsi
);
2180 tree dst
= gimple_call_arg (stmt
, 0);
2181 tree src
= gimple_call_arg (stmt
, 1);
2182 tree len
= gimple_call_arg (stmt
, 2);
2184 const char *p
= c_getstr (src
);
2186 /* If the requested length is zero, or the src parameter string
2187 length is zero, return the dst parameter. */
2188 if (integer_zerop (len
) || (p
&& *p
== '\0'))
2190 replace_call_with_value (gsi
, dst
);
2194 if (TREE_CODE (len
) != INTEGER_CST
|| !p
)
2197 unsigned srclen
= strlen (p
);
2199 int cmpsrc
= compare_tree_int (len
, srclen
);
2201 /* Return early if the requested len is less than the string length.
2202 Warnings will be issued elsewhere later. */
2206 unsigned HOST_WIDE_INT dstsize
;
2208 bool nowarn
= gimple_no_warning_p (stmt
);
2210 if (!nowarn
&& compute_builtin_object_size (dst
, 1, &dstsize
))
2212 int cmpdst
= compare_tree_int (len
, dstsize
);
2216 tree fndecl
= gimple_call_fndecl (stmt
);
2218 /* Strncat copies (at most) LEN bytes and always appends
2219 the terminating NUL so the specified bound should never
2220 be equal to (or greater than) the size of the destination.
2221 If it is, the copy could overflow. */
2222 location_t loc
= gimple_location (stmt
);
2223 nowarn
= warning_at (loc
, OPT_Wstringop_overflow_
,
2225 ? G_("%G%qD specified bound %E equals "
2227 : G_("%G%qD specified bound %E exceeds "
2228 "destination size %wu"),
2229 stmt
, fndecl
, len
, dstsize
);
2231 gimple_set_no_warning (stmt
, true);
2235 if (!nowarn
&& cmpsrc
== 0)
2237 tree fndecl
= gimple_call_fndecl (stmt
);
2238 location_t loc
= gimple_location (stmt
);
2240 /* To avoid possible overflow the specified bound should also
2241 not be equal to the length of the source, even when the size
2242 of the destination is unknown (it's not an uncommon mistake
2243 to specify as the bound to strncpy the length of the source). */
2244 if (warning_at (loc
, OPT_Wstringop_overflow_
,
2245 "%G%qD specified bound %E equals source length",
2247 gimple_set_no_warning (stmt
, true);
2250 tree fn
= builtin_decl_implicit (BUILT_IN_STRCAT
);
2252 /* If the replacement _DECL isn't initialized, don't do the
2257 /* Otherwise, emit a call to strcat. */
2258 gcall
*repl
= gimple_build_call (fn
, 2, dst
, src
);
2259 replace_call_with_call_and_fold (gsi
, repl
);
2263 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
2267 gimple_fold_builtin_strncat_chk (gimple_stmt_iterator
*gsi
)
2269 gimple
*stmt
= gsi_stmt (*gsi
);
2270 tree dest
= gimple_call_arg (stmt
, 0);
2271 tree src
= gimple_call_arg (stmt
, 1);
2272 tree len
= gimple_call_arg (stmt
, 2);
2273 tree size
= gimple_call_arg (stmt
, 3);
2278 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
2279 if ((p
&& *p
== '\0')
2280 || integer_zerop (len
))
2282 replace_call_with_value (gsi
, dest
);
2286 if (! tree_fits_uhwi_p (size
))
2289 if (! integer_all_onesp (size
))
2291 tree src_len
= c_strlen (src
, 1);
2293 && tree_fits_uhwi_p (src_len
)
2294 && tree_fits_uhwi_p (len
)
2295 && ! tree_int_cst_lt (len
, src_len
))
2297 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
2298 fn
= builtin_decl_explicit (BUILT_IN_STRCAT_CHK
);
2302 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, size
);
2303 replace_call_with_call_and_fold (gsi
, repl
);
2309 /* If __builtin_strncat_chk is used, assume strncat is available. */
2310 fn
= builtin_decl_explicit (BUILT_IN_STRNCAT
);
2314 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
2315 replace_call_with_call_and_fold (gsi
, repl
);
2319 /* Build and append gimple statements to STMTS that would load a first
2320 character of a memory location identified by STR. LOC is location
2321 of the statement. */
2324 gimple_load_first_char (location_t loc
, tree str
, gimple_seq
*stmts
)
2328 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
2329 tree cst_uchar_ptr_node
2330 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
2331 tree off0
= build_int_cst (cst_uchar_ptr_node
, 0);
2333 tree temp
= fold_build2_loc (loc
, MEM_REF
, cst_uchar_node
, str
, off0
);
2334 gassign
*stmt
= gimple_build_assign (NULL_TREE
, temp
);
2335 var
= create_tmp_reg_or_ssa_name (cst_uchar_node
, stmt
);
2337 gimple_assign_set_lhs (stmt
, var
);
2338 gimple_seq_add_stmt_without_update (stmts
, stmt
);
2343 /* Fold a call to the str{n}{case}cmp builtin pointed by GSI iterator.
2344 FCODE is the name of the builtin. */
2347 gimple_fold_builtin_string_compare (gimple_stmt_iterator
*gsi
)
2349 gimple
*stmt
= gsi_stmt (*gsi
);
2350 tree callee
= gimple_call_fndecl (stmt
);
2351 enum built_in_function fcode
= DECL_FUNCTION_CODE (callee
);
2353 tree type
= integer_type_node
;
2354 tree str1
= gimple_call_arg (stmt
, 0);
2355 tree str2
= gimple_call_arg (stmt
, 1);
2356 tree lhs
= gimple_call_lhs (stmt
);
2357 HOST_WIDE_INT length
= -1;
2359 /* Handle strncmp and strncasecmp functions. */
2360 if (gimple_call_num_args (stmt
) == 3)
2362 tree len
= gimple_call_arg (stmt
, 2);
2363 if (tree_fits_uhwi_p (len
))
2364 length
= tree_to_uhwi (len
);
2367 /* If the LEN parameter is zero, return zero. */
2370 replace_call_with_value (gsi
, integer_zero_node
);
2374 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
2375 if (operand_equal_p (str1
, str2
, 0))
2377 replace_call_with_value (gsi
, integer_zero_node
);
2381 const char *p1
= c_getstr (str1
);
2382 const char *p2
= c_getstr (str2
);
2384 /* For known strings, return an immediate value. */
2388 bool known_result
= false;
2392 case BUILT_IN_STRCMP
:
2393 case BUILT_IN_STRCMP_EQ
:
2395 r
= strcmp (p1
, p2
);
2396 known_result
= true;
2399 case BUILT_IN_STRNCMP
:
2400 case BUILT_IN_STRNCMP_EQ
:
2404 r
= strncmp (p1
, p2
, length
);
2405 known_result
= true;
2408 /* Only handleable situation is where the string are equal (result 0),
2409 which is already handled by operand_equal_p case. */
2410 case BUILT_IN_STRCASECMP
:
2412 case BUILT_IN_STRNCASECMP
:
2416 r
= strncmp (p1
, p2
, length
);
2418 known_result
= true;
2427 replace_call_with_value (gsi
, build_cmp_result (type
, r
));
2432 bool nonzero_length
= length
>= 1
2433 || fcode
== BUILT_IN_STRCMP
2434 || fcode
== BUILT_IN_STRCMP_EQ
2435 || fcode
== BUILT_IN_STRCASECMP
;
2437 location_t loc
= gimple_location (stmt
);
2439 /* If the second arg is "", return *(const unsigned char*)arg1. */
2440 if (p2
&& *p2
== '\0' && nonzero_length
)
2442 gimple_seq stmts
= NULL
;
2443 tree var
= gimple_load_first_char (loc
, str1
, &stmts
);
2446 stmt
= gimple_build_assign (lhs
, NOP_EXPR
, var
);
2447 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2450 gsi_replace_with_seq_vops (gsi
, stmts
);
2454 /* If the first arg is "", return -*(const unsigned char*)arg2. */
2455 if (p1
&& *p1
== '\0' && nonzero_length
)
2457 gimple_seq stmts
= NULL
;
2458 tree var
= gimple_load_first_char (loc
, str2
, &stmts
);
2462 tree c
= create_tmp_reg_or_ssa_name (integer_type_node
);
2463 stmt
= gimple_build_assign (c
, NOP_EXPR
, var
);
2464 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2466 stmt
= gimple_build_assign (lhs
, NEGATE_EXPR
, c
);
2467 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2470 gsi_replace_with_seq_vops (gsi
, stmts
);
2474 /* If len parameter is one, return an expression corresponding to
2475 (*(const unsigned char*)arg2 - *(const unsigned char*)arg1). */
2476 if (fcode
== BUILT_IN_STRNCMP
&& length
== 1)
2478 gimple_seq stmts
= NULL
;
2479 tree temp1
= gimple_load_first_char (loc
, str1
, &stmts
);
2480 tree temp2
= gimple_load_first_char (loc
, str2
, &stmts
);
2484 tree c1
= create_tmp_reg_or_ssa_name (integer_type_node
);
2485 gassign
*convert1
= gimple_build_assign (c1
, NOP_EXPR
, temp1
);
2486 gimple_seq_add_stmt_without_update (&stmts
, convert1
);
2488 tree c2
= create_tmp_reg_or_ssa_name (integer_type_node
);
2489 gassign
*convert2
= gimple_build_assign (c2
, NOP_EXPR
, temp2
);
2490 gimple_seq_add_stmt_without_update (&stmts
, convert2
);
2492 stmt
= gimple_build_assign (lhs
, MINUS_EXPR
, c1
, c2
);
2493 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2496 gsi_replace_with_seq_vops (gsi
, stmts
);
2500 /* If length is larger than the length of one constant string,
2501 replace strncmp with corresponding strcmp */
2502 if (fcode
== BUILT_IN_STRNCMP
2504 && ((p2
&& (size_t) length
> strlen (p2
))
2505 || (p1
&& (size_t) length
> strlen (p1
))))
2507 tree fn
= builtin_decl_implicit (BUILT_IN_STRCMP
);
2510 gimple
*repl
= gimple_build_call (fn
, 2, str1
, str2
);
2511 replace_call_with_call_and_fold (gsi
, repl
);
2518 /* Fold a call to the memchr pointed by GSI iterator. */
2521 gimple_fold_builtin_memchr (gimple_stmt_iterator
*gsi
)
2523 gimple
*stmt
= gsi_stmt (*gsi
);
2524 tree lhs
= gimple_call_lhs (stmt
);
2525 tree arg1
= gimple_call_arg (stmt
, 0);
2526 tree arg2
= gimple_call_arg (stmt
, 1);
2527 tree len
= gimple_call_arg (stmt
, 2);
2529 /* If the LEN parameter is zero, return zero. */
2530 if (integer_zerop (len
))
2532 replace_call_with_value (gsi
, build_int_cst (ptr_type_node
, 0));
2537 if (TREE_CODE (arg2
) != INTEGER_CST
2538 || !tree_fits_uhwi_p (len
)
2539 || !target_char_cst_p (arg2
, &c
))
2542 unsigned HOST_WIDE_INT length
= tree_to_uhwi (len
);
2543 unsigned HOST_WIDE_INT string_length
;
2544 const char *p1
= c_getstr (arg1
, &string_length
);
2548 const char *r
= (const char *)memchr (p1
, c
, MIN (length
, string_length
));
2551 if (length
<= string_length
)
2553 replace_call_with_value (gsi
, build_int_cst (ptr_type_node
, 0));
2559 unsigned HOST_WIDE_INT offset
= r
- p1
;
2560 gimple_seq stmts
= NULL
;
2561 if (lhs
!= NULL_TREE
)
2563 tree offset_cst
= build_int_cst (TREE_TYPE (len
), offset
);
2564 gassign
*stmt
= gimple_build_assign (lhs
, POINTER_PLUS_EXPR
,
2566 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2569 gimple_seq_add_stmt_without_update (&stmts
,
2570 gimple_build_nop ());
2572 gsi_replace_with_seq_vops (gsi
, stmts
);
2580 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
2581 to the call. IGNORE is true if the value returned
2582 by the builtin will be ignored. UNLOCKED is true is true if this
2583 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
2584 the known length of the string. Return NULL_TREE if no simplification
2588 gimple_fold_builtin_fputs (gimple_stmt_iterator
*gsi
,
2589 tree arg0
, tree arg1
,
2592 gimple
*stmt
= gsi_stmt (*gsi
);
2594 /* If we're using an unlocked function, assume the other unlocked
2595 functions exist explicitly. */
2596 tree
const fn_fputc
= (unlocked
2597 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED
)
2598 : builtin_decl_implicit (BUILT_IN_FPUTC
));
2599 tree
const fn_fwrite
= (unlocked
2600 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED
)
2601 : builtin_decl_implicit (BUILT_IN_FWRITE
));
2603 /* If the return value is used, don't do the transformation. */
2604 if (gimple_call_lhs (stmt
))
2607 /* Get the length of the string passed to fputs. If the length
2608 can't be determined, punt. */
2609 tree len
= get_maxval_strlen (arg0
, SRK_STRLEN
);
2611 || TREE_CODE (len
) != INTEGER_CST
)
2614 switch (compare_tree_int (len
, 1))
2616 case -1: /* length is 0, delete the call entirely . */
2617 replace_call_with_value (gsi
, integer_zero_node
);
2620 case 0: /* length is 1, call fputc. */
2622 const char *p
= c_getstr (arg0
);
2628 gimple
*repl
= gimple_build_call (fn_fputc
, 2,
2630 (integer_type_node
, p
[0]), arg1
);
2631 replace_call_with_call_and_fold (gsi
, repl
);
2636 case 1: /* length is greater than 1, call fwrite. */
2638 /* If optimizing for size keep fputs. */
2639 if (optimize_function_for_size_p (cfun
))
2641 /* New argument list transforming fputs(string, stream) to
2642 fwrite(string, 1, len, stream). */
2646 gimple
*repl
= gimple_build_call (fn_fwrite
, 4, arg0
,
2647 size_one_node
, len
, arg1
);
2648 replace_call_with_call_and_fold (gsi
, repl
);
2657 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
2658 DEST, SRC, LEN, and SIZE are the arguments to the call.
2659 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
2660 code of the builtin. If MAXLEN is not NULL, it is maximum length
2661 passed as third argument. */
2664 gimple_fold_builtin_memory_chk (gimple_stmt_iterator
*gsi
,
2665 tree dest
, tree src
, tree len
, tree size
,
2666 enum built_in_function fcode
)
2668 gimple
*stmt
= gsi_stmt (*gsi
);
2669 location_t loc
= gimple_location (stmt
);
2670 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
2673 /* If SRC and DEST are the same (and not volatile), return DEST
2674 (resp. DEST+LEN for __mempcpy_chk). */
2675 if (fcode
!= BUILT_IN_MEMSET_CHK
&& operand_equal_p (src
, dest
, 0))
2677 if (fcode
!= BUILT_IN_MEMPCPY_CHK
)
2679 replace_call_with_value (gsi
, dest
);
2684 gimple_seq stmts
= NULL
;
2685 len
= gimple_convert_to_ptrofftype (&stmts
, loc
, len
);
2686 tree temp
= gimple_build (&stmts
, loc
, POINTER_PLUS_EXPR
,
2687 TREE_TYPE (dest
), dest
, len
);
2688 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2689 replace_call_with_value (gsi
, temp
);
2694 if (! tree_fits_uhwi_p (size
))
2697 tree maxlen
= get_maxval_strlen (len
, SRK_INT_VALUE
);
2698 if (! integer_all_onesp (size
))
2700 if (! tree_fits_uhwi_p (len
))
2702 /* If LEN is not constant, try MAXLEN too.
2703 For MAXLEN only allow optimizing into non-_ocs function
2704 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2705 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
2707 if (fcode
== BUILT_IN_MEMPCPY_CHK
&& ignore
)
2709 /* (void) __mempcpy_chk () can be optimized into
2710 (void) __memcpy_chk (). */
2711 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
2715 gimple
*repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
2716 replace_call_with_call_and_fold (gsi
, repl
);
2725 if (tree_int_cst_lt (size
, maxlen
))
2730 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
2731 mem{cpy,pcpy,move,set} is available. */
2734 case BUILT_IN_MEMCPY_CHK
:
2735 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY
);
2737 case BUILT_IN_MEMPCPY_CHK
:
2738 fn
= builtin_decl_explicit (BUILT_IN_MEMPCPY
);
2740 case BUILT_IN_MEMMOVE_CHK
:
2741 fn
= builtin_decl_explicit (BUILT_IN_MEMMOVE
);
2743 case BUILT_IN_MEMSET_CHK
:
2744 fn
= builtin_decl_explicit (BUILT_IN_MEMSET
);
2753 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
2754 replace_call_with_call_and_fold (gsi
, repl
);
2758 /* Fold a call to the __st[rp]cpy_chk builtin.
2759 DEST, SRC, and SIZE are the arguments to the call.
2760 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
2761 code of the builtin. If MAXLEN is not NULL, it is maximum length of
2762 strings passed as second argument. */
2765 gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator
*gsi
,
2767 tree src
, tree size
,
2768 enum built_in_function fcode
)
2770 gimple
*stmt
= gsi_stmt (*gsi
);
2771 location_t loc
= gimple_location (stmt
);
2772 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
2775 /* If SRC and DEST are the same (and not volatile), return DEST. */
2776 if (fcode
== BUILT_IN_STRCPY_CHK
&& operand_equal_p (src
, dest
, 0))
2778 /* Issue -Wrestrict unless the pointers are null (those do
2779 not point to objects and so do not indicate an overlap;
2780 such calls could be the result of sanitization and jump
2782 if (!integer_zerop (dest
) && !gimple_no_warning_p (stmt
))
2784 tree func
= gimple_call_fndecl (stmt
);
2786 warning_at (loc
, OPT_Wrestrict
,
2787 "%qD source argument is the same as destination",
2791 replace_call_with_value (gsi
, dest
);
2795 if (! tree_fits_uhwi_p (size
))
2798 tree maxlen
= get_maxval_strlen (src
, SRK_STRLENMAX
);
2799 if (! integer_all_onesp (size
))
2801 len
= c_strlen (src
, 1);
2802 if (! len
|| ! tree_fits_uhwi_p (len
))
2804 /* If LEN is not constant, try MAXLEN too.
2805 For MAXLEN only allow optimizing into non-_ocs function
2806 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2807 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
2809 if (fcode
== BUILT_IN_STPCPY_CHK
)
2814 /* If return value of __stpcpy_chk is ignored,
2815 optimize into __strcpy_chk. */
2816 fn
= builtin_decl_explicit (BUILT_IN_STRCPY_CHK
);
2820 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, size
);
2821 replace_call_with_call_and_fold (gsi
, repl
);
2825 if (! len
|| TREE_SIDE_EFFECTS (len
))
2828 /* If c_strlen returned something, but not a constant,
2829 transform __strcpy_chk into __memcpy_chk. */
2830 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
2834 gimple_seq stmts
= NULL
;
2835 len
= force_gimple_operand (len
, &stmts
, true, NULL_TREE
);
2836 len
= gimple_convert (&stmts
, loc
, size_type_node
, len
);
2837 len
= gimple_build (&stmts
, loc
, PLUS_EXPR
, size_type_node
, len
,
2838 build_int_cst (size_type_node
, 1));
2839 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2840 gimple
*repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
2841 replace_call_with_call_and_fold (gsi
, repl
);
2848 if (! tree_int_cst_lt (maxlen
, size
))
2852 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
2853 fn
= builtin_decl_explicit (fcode
== BUILT_IN_STPCPY_CHK
2854 ? BUILT_IN_STPCPY
: BUILT_IN_STRCPY
);
2858 gimple
*repl
= gimple_build_call (fn
, 2, dest
, src
);
2859 replace_call_with_call_and_fold (gsi
, repl
);
2863 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
2864 are the arguments to the call. If MAXLEN is not NULL, it is maximum
2865 length passed as third argument. IGNORE is true if return value can be
2866 ignored. FCODE is the BUILT_IN_* code of the builtin. */
2869 gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator
*gsi
,
2870 tree dest
, tree src
,
2871 tree len
, tree size
,
2872 enum built_in_function fcode
)
2874 gimple
*stmt
= gsi_stmt (*gsi
);
2875 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
2878 if (fcode
== BUILT_IN_STPNCPY_CHK
&& ignore
)
2880 /* If return value of __stpncpy_chk is ignored,
2881 optimize into __strncpy_chk. */
2882 fn
= builtin_decl_explicit (BUILT_IN_STRNCPY_CHK
);
2885 gimple
*repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
2886 replace_call_with_call_and_fold (gsi
, repl
);
2891 if (! tree_fits_uhwi_p (size
))
2894 tree maxlen
= get_maxval_strlen (len
, SRK_INT_VALUE
);
2895 if (! integer_all_onesp (size
))
2897 if (! tree_fits_uhwi_p (len
))
2899 /* If LEN is not constant, try MAXLEN too.
2900 For MAXLEN only allow optimizing into non-_ocs function
2901 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2902 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
2908 if (tree_int_cst_lt (size
, maxlen
))
2912 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
2913 fn
= builtin_decl_explicit (fcode
== BUILT_IN_STPNCPY_CHK
2914 ? BUILT_IN_STPNCPY
: BUILT_IN_STRNCPY
);
2918 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
2919 replace_call_with_call_and_fold (gsi
, repl
);
2923 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
2924 Return NULL_TREE if no simplification can be made. */
2927 gimple_fold_builtin_stpcpy (gimple_stmt_iterator
*gsi
)
2929 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
2930 location_t loc
= gimple_location (stmt
);
2931 tree dest
= gimple_call_arg (stmt
, 0);
2932 tree src
= gimple_call_arg (stmt
, 1);
2935 /* If the result is unused, replace stpcpy with strcpy. */
2936 if (gimple_call_lhs (stmt
) == NULL_TREE
)
2938 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
2941 gimple_call_set_fndecl (stmt
, fn
);
2946 /* Set to non-null if ARG refers to an unterminated array. */
2947 c_strlen_data data
= { };
2948 tree len
= c_strlen (src
, 1, &data
, 1);
2950 || TREE_CODE (len
) != INTEGER_CST
)
2952 data
.decl
= unterminated_array (src
);
2959 /* Avoid folding calls with unterminated arrays. */
2960 if (!gimple_no_warning_p (stmt
))
2961 warn_string_no_nul (loc
, "stpcpy", src
, data
.decl
);
2962 gimple_set_no_warning (stmt
, true);
2966 if (optimize_function_for_size_p (cfun
)
2967 /* If length is zero it's small enough. */
2968 && !integer_zerop (len
))
2971 /* If the source has a known length replace stpcpy with memcpy. */
2972 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
2976 gimple_seq stmts
= NULL
;
2977 tree tem
= gimple_convert (&stmts
, loc
, size_type_node
, len
);
2978 lenp1
= gimple_build (&stmts
, loc
, PLUS_EXPR
, size_type_node
,
2979 tem
, build_int_cst (size_type_node
, 1));
2980 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2981 gcall
*repl
= gimple_build_call (fn
, 3, dest
, src
, lenp1
);
2982 gimple_set_vuse (repl
, gimple_vuse (stmt
));
2983 gimple_set_vdef (repl
, gimple_vdef (stmt
));
2984 if (gimple_vdef (repl
)
2985 && TREE_CODE (gimple_vdef (repl
)) == SSA_NAME
)
2986 SSA_NAME_DEF_STMT (gimple_vdef (repl
)) = repl
;
2987 gsi_insert_before (gsi
, repl
, GSI_SAME_STMT
);
2988 /* Replace the result with dest + len. */
2990 tem
= gimple_convert (&stmts
, loc
, sizetype
, len
);
2991 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2992 gassign
*ret
= gimple_build_assign (gimple_call_lhs (stmt
),
2993 POINTER_PLUS_EXPR
, dest
, tem
);
2994 gsi_replace (gsi
, ret
, false);
2995 /* Finally fold the memcpy call. */
2996 gimple_stmt_iterator gsi2
= *gsi
;
3002 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
3003 NULL_TREE if a normal call should be emitted rather than expanding
3004 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
3005 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
3006 passed as second argument. */
3009 gimple_fold_builtin_snprintf_chk (gimple_stmt_iterator
*gsi
,
3010 enum built_in_function fcode
)
3012 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3013 tree dest
, size
, len
, fn
, fmt
, flag
;
3014 const char *fmt_str
;
3016 /* Verify the required arguments in the original call. */
3017 if (gimple_call_num_args (stmt
) < 5)
3020 dest
= gimple_call_arg (stmt
, 0);
3021 len
= gimple_call_arg (stmt
, 1);
3022 flag
= gimple_call_arg (stmt
, 2);
3023 size
= gimple_call_arg (stmt
, 3);
3024 fmt
= gimple_call_arg (stmt
, 4);
3026 if (! tree_fits_uhwi_p (size
))
3029 if (! integer_all_onesp (size
))
3031 tree maxlen
= get_maxval_strlen (len
, SRK_INT_VALUE
);
3032 if (! tree_fits_uhwi_p (len
))
3034 /* If LEN is not constant, try MAXLEN too.
3035 For MAXLEN only allow optimizing into non-_ocs function
3036 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
3037 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
3043 if (tree_int_cst_lt (size
, maxlen
))
3047 if (!init_target_chars ())
3050 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
3051 or if format doesn't contain % chars or is "%s". */
3052 if (! integer_zerop (flag
))
3054 fmt_str
= c_getstr (fmt
);
3055 if (fmt_str
== NULL
)
3057 if (strchr (fmt_str
, target_percent
) != NULL
3058 && strcmp (fmt_str
, target_percent_s
))
3062 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
3064 fn
= builtin_decl_explicit (fcode
== BUILT_IN_VSNPRINTF_CHK
3065 ? BUILT_IN_VSNPRINTF
: BUILT_IN_SNPRINTF
);
3069 /* Replace the called function and the first 5 argument by 3 retaining
3070 trailing varargs. */
3071 gimple_call_set_fndecl (stmt
, fn
);
3072 gimple_call_set_fntype (stmt
, TREE_TYPE (fn
));
3073 gimple_call_set_arg (stmt
, 0, dest
);
3074 gimple_call_set_arg (stmt
, 1, len
);
3075 gimple_call_set_arg (stmt
, 2, fmt
);
3076 for (unsigned i
= 3; i
< gimple_call_num_args (stmt
) - 2; ++i
)
3077 gimple_call_set_arg (stmt
, i
, gimple_call_arg (stmt
, i
+ 2));
3078 gimple_set_num_ops (stmt
, gimple_num_ops (stmt
) - 2);
3083 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
3084 Return NULL_TREE if a normal call should be emitted rather than
3085 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
3086 or BUILT_IN_VSPRINTF_CHK. */
3089 gimple_fold_builtin_sprintf_chk (gimple_stmt_iterator
*gsi
,
3090 enum built_in_function fcode
)
3092 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3093 tree dest
, size
, len
, fn
, fmt
, flag
;
3094 const char *fmt_str
;
3095 unsigned nargs
= gimple_call_num_args (stmt
);
3097 /* Verify the required arguments in the original call. */
3100 dest
= gimple_call_arg (stmt
, 0);
3101 flag
= gimple_call_arg (stmt
, 1);
3102 size
= gimple_call_arg (stmt
, 2);
3103 fmt
= gimple_call_arg (stmt
, 3);
3105 if (! tree_fits_uhwi_p (size
))
3110 if (!init_target_chars ())
3113 /* Check whether the format is a literal string constant. */
3114 fmt_str
= c_getstr (fmt
);
3115 if (fmt_str
!= NULL
)
3117 /* If the format doesn't contain % args or %%, we know the size. */
3118 if (strchr (fmt_str
, target_percent
) == 0)
3120 if (fcode
!= BUILT_IN_SPRINTF_CHK
|| nargs
== 4)
3121 len
= build_int_cstu (size_type_node
, strlen (fmt_str
));
3123 /* If the format is "%s" and first ... argument is a string literal,
3124 we know the size too. */
3125 else if (fcode
== BUILT_IN_SPRINTF_CHK
3126 && strcmp (fmt_str
, target_percent_s
) == 0)
3132 arg
= gimple_call_arg (stmt
, 4);
3133 if (POINTER_TYPE_P (TREE_TYPE (arg
)))
3135 len
= c_strlen (arg
, 1);
3136 if (! len
|| ! tree_fits_uhwi_p (len
))
3143 if (! integer_all_onesp (size
))
3145 if (! len
|| ! tree_int_cst_lt (len
, size
))
3149 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
3150 or if format doesn't contain % chars or is "%s". */
3151 if (! integer_zerop (flag
))
3153 if (fmt_str
== NULL
)
3155 if (strchr (fmt_str
, target_percent
) != NULL
3156 && strcmp (fmt_str
, target_percent_s
))
3160 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
3161 fn
= builtin_decl_explicit (fcode
== BUILT_IN_VSPRINTF_CHK
3162 ? BUILT_IN_VSPRINTF
: BUILT_IN_SPRINTF
);
3166 /* Replace the called function and the first 4 argument by 2 retaining
3167 trailing varargs. */
3168 gimple_call_set_fndecl (stmt
, fn
);
3169 gimple_call_set_fntype (stmt
, TREE_TYPE (fn
));
3170 gimple_call_set_arg (stmt
, 0, dest
);
3171 gimple_call_set_arg (stmt
, 1, fmt
);
3172 for (unsigned i
= 2; i
< gimple_call_num_args (stmt
) - 2; ++i
)
3173 gimple_call_set_arg (stmt
, i
, gimple_call_arg (stmt
, i
+ 2));
3174 gimple_set_num_ops (stmt
, gimple_num_ops (stmt
) - 2);
3179 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
3180 ORIG may be null if this is a 2-argument call. We don't attempt to
3181 simplify calls with more than 3 arguments.
3183 Return true if simplification was possible, otherwise false. */
3186 gimple_fold_builtin_sprintf (gimple_stmt_iterator
*gsi
)
3188 gimple
*stmt
= gsi_stmt (*gsi
);
3189 tree dest
= gimple_call_arg (stmt
, 0);
3190 tree fmt
= gimple_call_arg (stmt
, 1);
3191 tree orig
= NULL_TREE
;
3192 const char *fmt_str
= NULL
;
3194 /* Verify the required arguments in the original call. We deal with two
3195 types of sprintf() calls: 'sprintf (str, fmt)' and
3196 'sprintf (dest, "%s", orig)'. */
3197 if (gimple_call_num_args (stmt
) > 3)
3200 if (gimple_call_num_args (stmt
) == 3)
3201 orig
= gimple_call_arg (stmt
, 2);
3203 /* Check whether the format is a literal string constant. */
3204 fmt_str
= c_getstr (fmt
);
3205 if (fmt_str
== NULL
)
3208 if (!init_target_chars ())
3211 /* If the format doesn't contain % args or %%, use strcpy. */
3212 if (strchr (fmt_str
, target_percent
) == NULL
)
3214 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3219 /* Don't optimize sprintf (buf, "abc", ptr++). */
3223 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
3224 'format' is known to contain no % formats. */
3225 gimple_seq stmts
= NULL
;
3226 gimple
*repl
= gimple_build_call (fn
, 2, dest
, fmt
);
3228 /* Propagate the NO_WARNING bit to avoid issuing the same
3229 warning more than once. */
3230 if (gimple_no_warning_p (stmt
))
3231 gimple_set_no_warning (repl
, true);
3233 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3234 if (tree lhs
= gimple_call_lhs (stmt
))
3236 repl
= gimple_build_assign (lhs
, build_int_cst (TREE_TYPE (lhs
),
3238 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3239 gsi_replace_with_seq_vops (gsi
, stmts
);
3240 /* gsi now points at the assignment to the lhs, get a
3241 stmt iterator to the memcpy call.
3242 ??? We can't use gsi_for_stmt as that doesn't work when the
3243 CFG isn't built yet. */
3244 gimple_stmt_iterator gsi2
= *gsi
;
3250 gsi_replace_with_seq_vops (gsi
, stmts
);
3256 /* If the format is "%s", use strcpy if the result isn't used. */
3257 else if (fmt_str
&& strcmp (fmt_str
, target_percent_s
) == 0)
3260 fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3265 /* Don't crash on sprintf (str1, "%s"). */
3269 tree orig_len
= NULL_TREE
;
3270 if (gimple_call_lhs (stmt
))
3272 orig_len
= get_maxval_strlen (orig
, SRK_STRLEN
);
3277 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
3278 gimple_seq stmts
= NULL
;
3279 gimple
*repl
= gimple_build_call (fn
, 2, dest
, orig
);
3281 /* Propagate the NO_WARNING bit to avoid issuing the same
3282 warning more than once. */
3283 if (gimple_no_warning_p (stmt
))
3284 gimple_set_no_warning (repl
, true);
3286 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3287 if (tree lhs
= gimple_call_lhs (stmt
))
3289 if (!useless_type_conversion_p (TREE_TYPE (lhs
),
3290 TREE_TYPE (orig_len
)))
3291 orig_len
= fold_convert (TREE_TYPE (lhs
), orig_len
);
3292 repl
= gimple_build_assign (lhs
, orig_len
);
3293 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3294 gsi_replace_with_seq_vops (gsi
, stmts
);
3295 /* gsi now points at the assignment to the lhs, get a
3296 stmt iterator to the memcpy call.
3297 ??? We can't use gsi_for_stmt as that doesn't work when the
3298 CFG isn't built yet. */
3299 gimple_stmt_iterator gsi2
= *gsi
;
3305 gsi_replace_with_seq_vops (gsi
, stmts
);
3313 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
3314 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
3315 attempt to simplify calls with more than 4 arguments.
3317 Return true if simplification was possible, otherwise false. */
3320 gimple_fold_builtin_snprintf (gimple_stmt_iterator
*gsi
)
3322 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3323 tree dest
= gimple_call_arg (stmt
, 0);
3324 tree destsize
= gimple_call_arg (stmt
, 1);
3325 tree fmt
= gimple_call_arg (stmt
, 2);
3326 tree orig
= NULL_TREE
;
3327 const char *fmt_str
= NULL
;
3329 if (gimple_call_num_args (stmt
) > 4)
3332 if (gimple_call_num_args (stmt
) == 4)
3333 orig
= gimple_call_arg (stmt
, 3);
3335 if (!tree_fits_uhwi_p (destsize
))
3337 unsigned HOST_WIDE_INT destlen
= tree_to_uhwi (destsize
);
3339 /* Check whether the format is a literal string constant. */
3340 fmt_str
= c_getstr (fmt
);
3341 if (fmt_str
== NULL
)
3344 if (!init_target_chars ())
3347 /* If the format doesn't contain % args or %%, use strcpy. */
3348 if (strchr (fmt_str
, target_percent
) == NULL
)
3350 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3354 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
3358 /* We could expand this as
3359 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
3361 memcpy (str, fmt_with_nul_at_cstm1, cst);
3362 but in the former case that might increase code size
3363 and in the latter case grow .rodata section too much.
3365 size_t len
= strlen (fmt_str
);
3369 gimple_seq stmts
= NULL
;
3370 gimple
*repl
= gimple_build_call (fn
, 2, dest
, fmt
);
3371 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3372 if (tree lhs
= gimple_call_lhs (stmt
))
3374 repl
= gimple_build_assign (lhs
,
3375 build_int_cst (TREE_TYPE (lhs
), len
));
3376 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3377 gsi_replace_with_seq_vops (gsi
, stmts
);
3378 /* gsi now points at the assignment to the lhs, get a
3379 stmt iterator to the memcpy call.
3380 ??? We can't use gsi_for_stmt as that doesn't work when the
3381 CFG isn't built yet. */
3382 gimple_stmt_iterator gsi2
= *gsi
;
3388 gsi_replace_with_seq_vops (gsi
, stmts
);
3394 /* If the format is "%s", use strcpy if the result isn't used. */
3395 else if (fmt_str
&& strcmp (fmt_str
, target_percent_s
) == 0)
3397 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3401 /* Don't crash on snprintf (str1, cst, "%s"). */
3405 tree orig_len
= get_maxval_strlen (orig
, SRK_STRLEN
);
3406 if (!orig_len
|| TREE_CODE (orig_len
) != INTEGER_CST
)
3409 /* We could expand this as
3410 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
3412 memcpy (str1, str2_with_nul_at_cstm1, cst);
3413 but in the former case that might increase code size
3414 and in the latter case grow .rodata section too much.
3416 if (compare_tree_int (orig_len
, destlen
) >= 0)
3419 /* Convert snprintf (str1, cst, "%s", str2) into
3420 strcpy (str1, str2) if strlen (str2) < cst. */
3421 gimple_seq stmts
= NULL
;
3422 gimple
*repl
= gimple_build_call (fn
, 2, dest
, orig
);
3423 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3424 if (tree lhs
= gimple_call_lhs (stmt
))
3426 if (!useless_type_conversion_p (TREE_TYPE (lhs
),
3427 TREE_TYPE (orig_len
)))
3428 orig_len
= fold_convert (TREE_TYPE (lhs
), orig_len
);
3429 repl
= gimple_build_assign (lhs
, orig_len
);
3430 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3431 gsi_replace_with_seq_vops (gsi
, stmts
);
3432 /* gsi now points at the assignment to the lhs, get a
3433 stmt iterator to the memcpy call.
3434 ??? We can't use gsi_for_stmt as that doesn't work when the
3435 CFG isn't built yet. */
3436 gimple_stmt_iterator gsi2
= *gsi
;
3442 gsi_replace_with_seq_vops (gsi
, stmts
);
3450 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
3451 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
3452 more than 3 arguments, and ARG may be null in the 2-argument case.
3454 Return NULL_TREE if no simplification was possible, otherwise return the
3455 simplified form of the call as a tree. FCODE is the BUILT_IN_*
3456 code of the function to be simplified. */
3459 gimple_fold_builtin_fprintf (gimple_stmt_iterator
*gsi
,
3460 tree fp
, tree fmt
, tree arg
,
3461 enum built_in_function fcode
)
3463 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3464 tree fn_fputc
, fn_fputs
;
3465 const char *fmt_str
= NULL
;
3467 /* If the return value is used, don't do the transformation. */
3468 if (gimple_call_lhs (stmt
) != NULL_TREE
)
3471 /* Check whether the format is a literal string constant. */
3472 fmt_str
= c_getstr (fmt
);
3473 if (fmt_str
== NULL
)
3476 if (fcode
== BUILT_IN_FPRINTF_UNLOCKED
)
3478 /* If we're using an unlocked function, assume the other
3479 unlocked functions exist explicitly. */
3480 fn_fputc
= builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED
);
3481 fn_fputs
= builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED
);
3485 fn_fputc
= builtin_decl_implicit (BUILT_IN_FPUTC
);
3486 fn_fputs
= builtin_decl_implicit (BUILT_IN_FPUTS
);
3489 if (!init_target_chars ())
3492 /* If the format doesn't contain % args or %%, use strcpy. */
3493 if (strchr (fmt_str
, target_percent
) == NULL
)
3495 if (fcode
!= BUILT_IN_VFPRINTF
&& fcode
!= BUILT_IN_VFPRINTF_CHK
3499 /* If the format specifier was "", fprintf does nothing. */
3500 if (fmt_str
[0] == '\0')
3502 replace_call_with_value (gsi
, NULL_TREE
);
3506 /* When "string" doesn't contain %, replace all cases of
3507 fprintf (fp, string) with fputs (string, fp). The fputs
3508 builtin will take care of special cases like length == 1. */
3511 gcall
*repl
= gimple_build_call (fn_fputs
, 2, fmt
, fp
);
3512 replace_call_with_call_and_fold (gsi
, repl
);
3517 /* The other optimizations can be done only on the non-va_list variants. */
3518 else if (fcode
== BUILT_IN_VFPRINTF
|| fcode
== BUILT_IN_VFPRINTF_CHK
)
3521 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
3522 else if (strcmp (fmt_str
, target_percent_s
) == 0)
3524 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
3528 gcall
*repl
= gimple_build_call (fn_fputs
, 2, arg
, fp
);
3529 replace_call_with_call_and_fold (gsi
, repl
);
3534 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
3535 else if (strcmp (fmt_str
, target_percent_c
) == 0)
3538 || ! useless_type_conversion_p (integer_type_node
, TREE_TYPE (arg
)))
3542 gcall
*repl
= gimple_build_call (fn_fputc
, 2, arg
, fp
);
3543 replace_call_with_call_and_fold (gsi
, repl
);
3551 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
3552 FMT and ARG are the arguments to the call; we don't fold cases with
3553 more than 2 arguments, and ARG may be null if this is a 1-argument case.
3555 Return NULL_TREE if no simplification was possible, otherwise return the
3556 simplified form of the call as a tree. FCODE is the BUILT_IN_*
3557 code of the function to be simplified. */
3560 gimple_fold_builtin_printf (gimple_stmt_iterator
*gsi
, tree fmt
,
3561 tree arg
, enum built_in_function fcode
)
3563 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3564 tree fn_putchar
, fn_puts
, newarg
;
3565 const char *fmt_str
= NULL
;
3567 /* If the return value is used, don't do the transformation. */
3568 if (gimple_call_lhs (stmt
) != NULL_TREE
)
3571 /* Check whether the format is a literal string constant. */
3572 fmt_str
= c_getstr (fmt
);
3573 if (fmt_str
== NULL
)
3576 if (fcode
== BUILT_IN_PRINTF_UNLOCKED
)
3578 /* If we're using an unlocked function, assume the other
3579 unlocked functions exist explicitly. */
3580 fn_putchar
= builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED
);
3581 fn_puts
= builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED
);
3585 fn_putchar
= builtin_decl_implicit (BUILT_IN_PUTCHAR
);
3586 fn_puts
= builtin_decl_implicit (BUILT_IN_PUTS
);
3589 if (!init_target_chars ())
3592 if (strcmp (fmt_str
, target_percent_s
) == 0
3593 || strchr (fmt_str
, target_percent
) == NULL
)
3597 if (strcmp (fmt_str
, target_percent_s
) == 0)
3599 if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
3602 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
3605 str
= c_getstr (arg
);
3611 /* The format specifier doesn't contain any '%' characters. */
3612 if (fcode
!= BUILT_IN_VPRINTF
&& fcode
!= BUILT_IN_VPRINTF_CHK
3618 /* If the string was "", printf does nothing. */
3621 replace_call_with_value (gsi
, NULL_TREE
);
3625 /* If the string has length of 1, call putchar. */
3628 /* Given printf("c"), (where c is any one character,)
3629 convert "c"[0] to an int and pass that to the replacement
3631 newarg
= build_int_cst (integer_type_node
, str
[0]);
3634 gcall
*repl
= gimple_build_call (fn_putchar
, 1, newarg
);
3635 replace_call_with_call_and_fold (gsi
, repl
);
3641 /* If the string was "string\n", call puts("string"). */
3642 size_t len
= strlen (str
);
3643 if ((unsigned char)str
[len
- 1] == target_newline
3644 && (size_t) (int) len
== len
3649 /* Create a NUL-terminated string that's one char shorter
3650 than the original, stripping off the trailing '\n'. */
3651 newstr
= xstrdup (str
);
3652 newstr
[len
- 1] = '\0';
3653 newarg
= build_string_literal (len
, newstr
);
3657 gcall
*repl
= gimple_build_call (fn_puts
, 1, newarg
);
3658 replace_call_with_call_and_fold (gsi
, repl
);
3663 /* We'd like to arrange to call fputs(string,stdout) here,
3664 but we need stdout and don't have a way to get it yet. */
3669 /* The other optimizations can be done only on the non-va_list variants. */
3670 else if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
3673 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
3674 else if (strcmp (fmt_str
, target_percent_s_newline
) == 0)
3676 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
3680 gcall
*repl
= gimple_build_call (fn_puts
, 1, arg
);
3681 replace_call_with_call_and_fold (gsi
, repl
);
3686 /* If the format specifier was "%c", call __builtin_putchar(arg). */
3687 else if (strcmp (fmt_str
, target_percent_c
) == 0)
3689 if (!arg
|| ! useless_type_conversion_p (integer_type_node
,
3694 gcall
*repl
= gimple_build_call (fn_putchar
, 1, arg
);
3695 replace_call_with_call_and_fold (gsi
, repl
);
3705 /* Fold a call to __builtin_strlen with known length LEN. */
3708 gimple_fold_builtin_strlen (gimple_stmt_iterator
*gsi
)
3710 gimple
*stmt
= gsi_stmt (*gsi
);
3711 tree arg
= gimple_call_arg (stmt
, 0);
3716 c_strlen_data lendata
= { };
3717 if (get_range_strlen (arg
, &lendata
, /* eltsize = */ 1)
3719 && lendata
.minlen
&& TREE_CODE (lendata
.minlen
) == INTEGER_CST
3720 && lendata
.maxlen
&& TREE_CODE (lendata
.maxlen
) == INTEGER_CST
)
3722 /* The range of lengths refers to either a single constant
3723 string or to the longest and shortest constant string
3724 referenced by the argument of the strlen() call, or to
3725 the strings that can possibly be stored in the arrays
3726 the argument refers to. */
3727 minlen
= wi::to_wide (lendata
.minlen
);
3728 maxlen
= wi::to_wide (lendata
.maxlen
);
3732 unsigned prec
= TYPE_PRECISION (sizetype
);
3734 minlen
= wi::shwi (0, prec
);
3735 maxlen
= wi::to_wide (max_object_size (), prec
) - 2;
3738 if (minlen
== maxlen
)
3740 /* Fold the strlen call to a constant. */
3741 tree type
= TREE_TYPE (lendata
.minlen
);
3742 tree len
= force_gimple_operand_gsi (gsi
,
3743 wide_int_to_tree (type
, minlen
),
3744 true, NULL
, true, GSI_SAME_STMT
);
3745 replace_call_with_value (gsi
, len
);
3749 /* Set the strlen() range to [0, MAXLEN]. */
3750 if (tree lhs
= gimple_call_lhs (stmt
))
3751 set_strlen_range (lhs
, maxlen
);
3756 /* Fold a call to __builtin_acc_on_device. */
3759 gimple_fold_builtin_acc_on_device (gimple_stmt_iterator
*gsi
, tree arg0
)
3761 /* Defer folding until we know which compiler we're in. */
3762 if (symtab
->state
!= EXPANSION
)
3765 unsigned val_host
= GOMP_DEVICE_HOST
;
3766 unsigned val_dev
= GOMP_DEVICE_NONE
;
3768 #ifdef ACCEL_COMPILER
3769 val_host
= GOMP_DEVICE_NOT_HOST
;
3770 val_dev
= ACCEL_COMPILER_acc_device
;
3773 location_t loc
= gimple_location (gsi_stmt (*gsi
));
3775 tree host_eq
= make_ssa_name (boolean_type_node
);
3776 gimple
*host_ass
= gimple_build_assign
3777 (host_eq
, EQ_EXPR
, arg0
, build_int_cst (TREE_TYPE (arg0
), val_host
));
3778 gimple_set_location (host_ass
, loc
);
3779 gsi_insert_before (gsi
, host_ass
, GSI_SAME_STMT
);
3781 tree dev_eq
= make_ssa_name (boolean_type_node
);
3782 gimple
*dev_ass
= gimple_build_assign
3783 (dev_eq
, EQ_EXPR
, arg0
, build_int_cst (TREE_TYPE (arg0
), val_dev
));
3784 gimple_set_location (dev_ass
, loc
);
3785 gsi_insert_before (gsi
, dev_ass
, GSI_SAME_STMT
);
3787 tree result
= make_ssa_name (boolean_type_node
);
3788 gimple
*result_ass
= gimple_build_assign
3789 (result
, BIT_IOR_EXPR
, host_eq
, dev_eq
);
3790 gimple_set_location (result_ass
, loc
);
3791 gsi_insert_before (gsi
, result_ass
, GSI_SAME_STMT
);
3793 replace_call_with_value (gsi
, result
);
3798 /* Fold realloc (0, n) -> malloc (n). */
3801 gimple_fold_builtin_realloc (gimple_stmt_iterator
*gsi
)
3803 gimple
*stmt
= gsi_stmt (*gsi
);
3804 tree arg
= gimple_call_arg (stmt
, 0);
3805 tree size
= gimple_call_arg (stmt
, 1);
3807 if (operand_equal_p (arg
, null_pointer_node
, 0))
3809 tree fn_malloc
= builtin_decl_implicit (BUILT_IN_MALLOC
);
3812 gcall
*repl
= gimple_build_call (fn_malloc
, 1, size
);
3813 replace_call_with_call_and_fold (gsi
, repl
);
3820 /* Fold the non-target builtin at *GSI and return whether any simplification
3824 gimple_fold_builtin (gimple_stmt_iterator
*gsi
)
3826 gcall
*stmt
= as_a
<gcall
*>(gsi_stmt (*gsi
));
3827 tree callee
= gimple_call_fndecl (stmt
);
3829 /* Give up for always_inline inline builtins until they are
3831 if (avoid_folding_inline_builtin (callee
))
3834 unsigned n
= gimple_call_num_args (stmt
);
3835 enum built_in_function fcode
= DECL_FUNCTION_CODE (callee
);
3839 return gimple_fold_builtin_bcmp (gsi
);
3840 case BUILT_IN_BCOPY
:
3841 return gimple_fold_builtin_bcopy (gsi
);
3842 case BUILT_IN_BZERO
:
3843 return gimple_fold_builtin_bzero (gsi
);
3845 case BUILT_IN_MEMSET
:
3846 return gimple_fold_builtin_memset (gsi
,
3847 gimple_call_arg (stmt
, 1),
3848 gimple_call_arg (stmt
, 2));
3849 case BUILT_IN_MEMCPY
:
3850 return gimple_fold_builtin_memory_op (gsi
, gimple_call_arg (stmt
, 0),
3851 gimple_call_arg (stmt
, 1), 0);
3852 case BUILT_IN_MEMPCPY
:
3853 return gimple_fold_builtin_memory_op (gsi
, gimple_call_arg (stmt
, 0),
3854 gimple_call_arg (stmt
, 1), 1);
3855 case BUILT_IN_MEMMOVE
:
3856 return gimple_fold_builtin_memory_op (gsi
, gimple_call_arg (stmt
, 0),
3857 gimple_call_arg (stmt
, 1), 3);
3858 case BUILT_IN_SPRINTF_CHK
:
3859 case BUILT_IN_VSPRINTF_CHK
:
3860 return gimple_fold_builtin_sprintf_chk (gsi
, fcode
);
3861 case BUILT_IN_STRCAT_CHK
:
3862 return gimple_fold_builtin_strcat_chk (gsi
);
3863 case BUILT_IN_STRNCAT_CHK
:
3864 return gimple_fold_builtin_strncat_chk (gsi
);
3865 case BUILT_IN_STRLEN
:
3866 return gimple_fold_builtin_strlen (gsi
);
3867 case BUILT_IN_STRCPY
:
3868 return gimple_fold_builtin_strcpy (gsi
,
3869 gimple_call_arg (stmt
, 0),
3870 gimple_call_arg (stmt
, 1));
3871 case BUILT_IN_STRNCPY
:
3872 return gimple_fold_builtin_strncpy (gsi
,
3873 gimple_call_arg (stmt
, 0),
3874 gimple_call_arg (stmt
, 1),
3875 gimple_call_arg (stmt
, 2));
3876 case BUILT_IN_STRCAT
:
3877 return gimple_fold_builtin_strcat (gsi
, gimple_call_arg (stmt
, 0),
3878 gimple_call_arg (stmt
, 1));
3879 case BUILT_IN_STRNCAT
:
3880 return gimple_fold_builtin_strncat (gsi
);
3881 case BUILT_IN_INDEX
:
3882 case BUILT_IN_STRCHR
:
3883 return gimple_fold_builtin_strchr (gsi
, false);
3884 case BUILT_IN_RINDEX
:
3885 case BUILT_IN_STRRCHR
:
3886 return gimple_fold_builtin_strchr (gsi
, true);
3887 case BUILT_IN_STRSTR
:
3888 return gimple_fold_builtin_strstr (gsi
);
3889 case BUILT_IN_STRCMP
:
3890 case BUILT_IN_STRCMP_EQ
:
3891 case BUILT_IN_STRCASECMP
:
3892 case BUILT_IN_STRNCMP
:
3893 case BUILT_IN_STRNCMP_EQ
:
3894 case BUILT_IN_STRNCASECMP
:
3895 return gimple_fold_builtin_string_compare (gsi
);
3896 case BUILT_IN_MEMCHR
:
3897 return gimple_fold_builtin_memchr (gsi
);
3898 case BUILT_IN_FPUTS
:
3899 return gimple_fold_builtin_fputs (gsi
, gimple_call_arg (stmt
, 0),
3900 gimple_call_arg (stmt
, 1), false);
3901 case BUILT_IN_FPUTS_UNLOCKED
:
3902 return gimple_fold_builtin_fputs (gsi
, gimple_call_arg (stmt
, 0),
3903 gimple_call_arg (stmt
, 1), true);
3904 case BUILT_IN_MEMCPY_CHK
:
3905 case BUILT_IN_MEMPCPY_CHK
:
3906 case BUILT_IN_MEMMOVE_CHK
:
3907 case BUILT_IN_MEMSET_CHK
:
3908 return gimple_fold_builtin_memory_chk (gsi
,
3909 gimple_call_arg (stmt
, 0),
3910 gimple_call_arg (stmt
, 1),
3911 gimple_call_arg (stmt
, 2),
3912 gimple_call_arg (stmt
, 3),
3914 case BUILT_IN_STPCPY
:
3915 return gimple_fold_builtin_stpcpy (gsi
);
3916 case BUILT_IN_STRCPY_CHK
:
3917 case BUILT_IN_STPCPY_CHK
:
3918 return gimple_fold_builtin_stxcpy_chk (gsi
,
3919 gimple_call_arg (stmt
, 0),
3920 gimple_call_arg (stmt
, 1),
3921 gimple_call_arg (stmt
, 2),
3923 case BUILT_IN_STRNCPY_CHK
:
3924 case BUILT_IN_STPNCPY_CHK
:
3925 return gimple_fold_builtin_stxncpy_chk (gsi
,
3926 gimple_call_arg (stmt
, 0),
3927 gimple_call_arg (stmt
, 1),
3928 gimple_call_arg (stmt
, 2),
3929 gimple_call_arg (stmt
, 3),
3931 case BUILT_IN_SNPRINTF_CHK
:
3932 case BUILT_IN_VSNPRINTF_CHK
:
3933 return gimple_fold_builtin_snprintf_chk (gsi
, fcode
);
3935 case BUILT_IN_FPRINTF
:
3936 case BUILT_IN_FPRINTF_UNLOCKED
:
3937 case BUILT_IN_VFPRINTF
:
3938 if (n
== 2 || n
== 3)
3939 return gimple_fold_builtin_fprintf (gsi
,
3940 gimple_call_arg (stmt
, 0),
3941 gimple_call_arg (stmt
, 1),
3943 ? gimple_call_arg (stmt
, 2)
3947 case BUILT_IN_FPRINTF_CHK
:
3948 case BUILT_IN_VFPRINTF_CHK
:
3949 if (n
== 3 || n
== 4)
3950 return gimple_fold_builtin_fprintf (gsi
,
3951 gimple_call_arg (stmt
, 0),
3952 gimple_call_arg (stmt
, 2),
3954 ? gimple_call_arg (stmt
, 3)
3958 case BUILT_IN_PRINTF
:
3959 case BUILT_IN_PRINTF_UNLOCKED
:
3960 case BUILT_IN_VPRINTF
:
3961 if (n
== 1 || n
== 2)
3962 return gimple_fold_builtin_printf (gsi
, gimple_call_arg (stmt
, 0),
3964 ? gimple_call_arg (stmt
, 1)
3965 : NULL_TREE
, fcode
);
3967 case BUILT_IN_PRINTF_CHK
:
3968 case BUILT_IN_VPRINTF_CHK
:
3969 if (n
== 2 || n
== 3)
3970 return gimple_fold_builtin_printf (gsi
, gimple_call_arg (stmt
, 1),
3972 ? gimple_call_arg (stmt
, 2)
3973 : NULL_TREE
, fcode
);
3975 case BUILT_IN_ACC_ON_DEVICE
:
3976 return gimple_fold_builtin_acc_on_device (gsi
,
3977 gimple_call_arg (stmt
, 0));
3978 case BUILT_IN_REALLOC
:
3979 return gimple_fold_builtin_realloc (gsi
);
3984 /* Try the generic builtin folder. */
3985 bool ignore
= (gimple_call_lhs (stmt
) == NULL
);
3986 tree result
= fold_call_stmt (stmt
, ignore
);
3990 STRIP_NOPS (result
);
3992 result
= fold_convert (gimple_call_return_type (stmt
), result
);
3993 if (!update_call_from_tree (gsi
, result
))
3994 gimplify_and_update_call_from_tree (gsi
, result
);
4001 /* Transform IFN_GOACC_DIM_SIZE and IFN_GOACC_DIM_POS internal
4002 function calls to constants, where possible. */
4005 fold_internal_goacc_dim (const gimple
*call
)
4007 int axis
= oacc_get_ifn_dim_arg (call
);
4008 int size
= oacc_get_fn_dim_size (current_function_decl
, axis
);
4009 tree result
= NULL_TREE
;
4010 tree type
= TREE_TYPE (gimple_call_lhs (call
));
4012 switch (gimple_call_internal_fn (call
))
4014 case IFN_GOACC_DIM_POS
:
4015 /* If the size is 1, we know the answer. */
4017 result
= build_int_cst (type
, 0);
4019 case IFN_GOACC_DIM_SIZE
:
4020 /* If the size is not dynamic, we know the answer. */
4022 result
= build_int_cst (type
, size
);
4031 /* Return true if stmt is __atomic_compare_exchange_N call which is suitable
4032 for conversion into ATOMIC_COMPARE_EXCHANGE if the second argument is
4033 &var where var is only addressable because of such calls. */
4036 optimize_atomic_compare_exchange_p (gimple
*stmt
)
4038 if (gimple_call_num_args (stmt
) != 6
4039 || !flag_inline_atomics
4041 || sanitize_flags_p (SANITIZE_THREAD
| SANITIZE_ADDRESS
)
4042 || !gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
)
4043 || !gimple_vdef (stmt
)
4044 || !gimple_vuse (stmt
))
4047 tree fndecl
= gimple_call_fndecl (stmt
);
4048 switch (DECL_FUNCTION_CODE (fndecl
))
4050 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
:
4051 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2
:
4052 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4
:
4053 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8
:
4054 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16
:
4060 tree expected
= gimple_call_arg (stmt
, 1);
4061 if (TREE_CODE (expected
) != ADDR_EXPR
4062 || !SSA_VAR_P (TREE_OPERAND (expected
, 0)))
4065 tree etype
= TREE_TYPE (TREE_OPERAND (expected
, 0));
4066 if (!is_gimple_reg_type (etype
)
4067 || !auto_var_in_fn_p (TREE_OPERAND (expected
, 0), current_function_decl
)
4068 || TREE_THIS_VOLATILE (etype
)
4069 || VECTOR_TYPE_P (etype
)
4070 || TREE_CODE (etype
) == COMPLEX_TYPE
4071 /* Don't optimize floating point expected vars, VIEW_CONVERT_EXPRs
4072 might not preserve all the bits. See PR71716. */
4073 || SCALAR_FLOAT_TYPE_P (etype
)
4074 || maybe_ne (TYPE_PRECISION (etype
),
4075 GET_MODE_BITSIZE (TYPE_MODE (etype
))))
4078 tree weak
= gimple_call_arg (stmt
, 3);
4079 if (!integer_zerop (weak
) && !integer_onep (weak
))
4082 tree parmt
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
4083 tree itype
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt
)));
4084 machine_mode mode
= TYPE_MODE (itype
);
4086 if (direct_optab_handler (atomic_compare_and_swap_optab
, mode
)
4088 && optab_handler (sync_compare_and_swap_optab
, mode
) == CODE_FOR_nothing
)
4091 if (maybe_ne (int_size_in_bytes (etype
), GET_MODE_SIZE (mode
)))
4098 r = __atomic_compare_exchange_N (p, &e, d, w, s, f);
4100 _Complex uintN_t t = ATOMIC_COMPARE_EXCHANGE (p, e, d, w * 256 + N, s, f);
4101 i = IMAGPART_EXPR <t>;
4103 e = REALPART_EXPR <t>; */
4106 fold_builtin_atomic_compare_exchange (gimple_stmt_iterator
*gsi
)
4108 gimple
*stmt
= gsi_stmt (*gsi
);
4109 tree fndecl
= gimple_call_fndecl (stmt
);
4110 tree parmt
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
4111 tree itype
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt
)));
4112 tree ctype
= build_complex_type (itype
);
4113 tree expected
= TREE_OPERAND (gimple_call_arg (stmt
, 1), 0);
4114 bool throws
= false;
4116 gimple
*g
= gimple_build_assign (make_ssa_name (TREE_TYPE (expected
)),
4118 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4119 gimple_stmt_iterator gsiret
= gsi_for_stmt (g
);
4120 if (!useless_type_conversion_p (itype
, TREE_TYPE (expected
)))
4122 g
= gimple_build_assign (make_ssa_name (itype
), VIEW_CONVERT_EXPR
,
4123 build1 (VIEW_CONVERT_EXPR
, itype
,
4124 gimple_assign_lhs (g
)));
4125 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4127 int flag
= (integer_onep (gimple_call_arg (stmt
, 3)) ? 256 : 0)
4128 + int_size_in_bytes (itype
);
4129 g
= gimple_build_call_internal (IFN_ATOMIC_COMPARE_EXCHANGE
, 6,
4130 gimple_call_arg (stmt
, 0),
4131 gimple_assign_lhs (g
),
4132 gimple_call_arg (stmt
, 2),
4133 build_int_cst (integer_type_node
, flag
),
4134 gimple_call_arg (stmt
, 4),
4135 gimple_call_arg (stmt
, 5));
4136 tree lhs
= make_ssa_name (ctype
);
4137 gimple_call_set_lhs (g
, lhs
);
4138 gimple_set_vdef (g
, gimple_vdef (stmt
));
4139 gimple_set_vuse (g
, gimple_vuse (stmt
));
4140 SSA_NAME_DEF_STMT (gimple_vdef (g
)) = g
;
4141 tree oldlhs
= gimple_call_lhs (stmt
);
4142 if (stmt_can_throw_internal (cfun
, stmt
))
4145 e
= find_fallthru_edge (gsi_bb (*gsi
)->succs
);
4147 gimple_call_set_nothrow (as_a
<gcall
*> (g
),
4148 gimple_call_nothrow_p (as_a
<gcall
*> (stmt
)));
4149 gimple_call_set_lhs (stmt
, NULL_TREE
);
4150 gsi_replace (gsi
, g
, true);
4153 g
= gimple_build_assign (make_ssa_name (itype
), IMAGPART_EXPR
,
4154 build1 (IMAGPART_EXPR
, itype
, lhs
));
4157 gsi_insert_on_edge_immediate (e
, g
);
4158 *gsi
= gsi_for_stmt (g
);
4161 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
4162 g
= gimple_build_assign (oldlhs
, NOP_EXPR
, gimple_assign_lhs (g
));
4163 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
4165 g
= gimple_build_assign (make_ssa_name (itype
), REALPART_EXPR
,
4166 build1 (REALPART_EXPR
, itype
, lhs
));
4167 if (throws
&& oldlhs
== NULL_TREE
)
4169 gsi_insert_on_edge_immediate (e
, g
);
4170 *gsi
= gsi_for_stmt (g
);
4173 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
4174 if (!useless_type_conversion_p (TREE_TYPE (expected
), itype
))
4176 g
= gimple_build_assign (make_ssa_name (TREE_TYPE (expected
)),
4178 build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (expected
),
4179 gimple_assign_lhs (g
)));
4180 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
4182 g
= gimple_build_assign (expected
, SSA_NAME
, gimple_assign_lhs (g
));
4183 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
4187 /* Return true if ARG0 CODE ARG1 in infinite signed precision operation
4188 doesn't fit into TYPE. The test for overflow should be regardless of
4189 -fwrapv, and even for unsigned types. */
4192 arith_overflowed_p (enum tree_code code
, const_tree type
,
4193 const_tree arg0
, const_tree arg1
)
4195 widest2_int warg0
= widest2_int_cst (arg0
);
4196 widest2_int warg1
= widest2_int_cst (arg1
);
4200 case PLUS_EXPR
: wres
= wi::add (warg0
, warg1
); break;
4201 case MINUS_EXPR
: wres
= wi::sub (warg0
, warg1
); break;
4202 case MULT_EXPR
: wres
= wi::mul (warg0
, warg1
); break;
4203 default: gcc_unreachable ();
4205 signop sign
= TYPE_SIGN (type
);
4206 if (sign
== UNSIGNED
&& wi::neg_p (wres
))
4208 return wi::min_precision (wres
, sign
) > TYPE_PRECISION (type
);
4211 /* Attempt to fold a call statement referenced by the statement iterator GSI.
4212 The statement may be replaced by another statement, e.g., if the call
4213 simplifies to a constant value. Return true if any changes were made.
4214 It is assumed that the operands have been previously folded. */
4217 gimple_fold_call (gimple_stmt_iterator
*gsi
, bool inplace
)
4219 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
4221 bool changed
= false;
4224 /* Fold *& in call arguments. */
4225 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
4226 if (REFERENCE_CLASS_P (gimple_call_arg (stmt
, i
)))
4228 tree tmp
= maybe_fold_reference (gimple_call_arg (stmt
, i
), false);
4231 gimple_call_set_arg (stmt
, i
, tmp
);
4236 /* Check for virtual calls that became direct calls. */
4237 callee
= gimple_call_fn (stmt
);
4238 if (callee
&& TREE_CODE (callee
) == OBJ_TYPE_REF
)
4240 if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee
)) != NULL_TREE
)
4242 if (dump_file
&& virtual_method_call_p (callee
)
4243 && !possible_polymorphic_call_target_p
4244 (callee
, stmt
, cgraph_node::get (gimple_call_addr_fndecl
4245 (OBJ_TYPE_REF_EXPR (callee
)))))
4248 "Type inheritance inconsistent devirtualization of ");
4249 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
4250 fprintf (dump_file
, " to ");
4251 print_generic_expr (dump_file
, callee
, TDF_SLIM
);
4252 fprintf (dump_file
, "\n");
4255 gimple_call_set_fn (stmt
, OBJ_TYPE_REF_EXPR (callee
));
4258 else if (flag_devirtualize
&& !inplace
&& virtual_method_call_p (callee
))
4261 vec
<cgraph_node
*>targets
4262 = possible_polymorphic_call_targets (callee
, stmt
, &final
);
4263 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
4265 tree lhs
= gimple_call_lhs (stmt
);
4266 if (dump_enabled_p ())
4268 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, stmt
,
4269 "folding virtual function call to %s\n",
4270 targets
.length () == 1
4271 ? targets
[0]->name ()
4272 : "__builtin_unreachable");
4274 if (targets
.length () == 1)
4276 tree fndecl
= targets
[0]->decl
;
4277 gimple_call_set_fndecl (stmt
, fndecl
);
4279 /* If changing the call to __cxa_pure_virtual
4280 or similar noreturn function, adjust gimple_call_fntype
4282 if (gimple_call_noreturn_p (stmt
)
4283 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl
)))
4284 && TYPE_ARG_TYPES (TREE_TYPE (fndecl
))
4285 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)))
4287 gimple_call_set_fntype (stmt
, TREE_TYPE (fndecl
));
4288 /* If the call becomes noreturn, remove the lhs. */
4290 && gimple_call_noreturn_p (stmt
)
4291 && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (stmt
)))
4292 || should_remove_lhs_p (lhs
)))
4294 if (TREE_CODE (lhs
) == SSA_NAME
)
4296 tree var
= create_tmp_var (TREE_TYPE (lhs
));
4297 tree def
= get_or_create_ssa_default_def (cfun
, var
);
4298 gimple
*new_stmt
= gimple_build_assign (lhs
, def
);
4299 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
4301 gimple_call_set_lhs (stmt
, NULL_TREE
);
4303 maybe_remove_unused_call_args (cfun
, stmt
);
4307 tree fndecl
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
4308 gimple
*new_stmt
= gimple_build_call (fndecl
, 0);
4309 gimple_set_location (new_stmt
, gimple_location (stmt
));
4310 /* If the call had a SSA name as lhs morph that into
4311 an uninitialized value. */
4312 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
4314 tree var
= create_tmp_var (TREE_TYPE (lhs
));
4315 SET_SSA_NAME_VAR_OR_IDENTIFIER (lhs
, var
);
4316 SSA_NAME_DEF_STMT (lhs
) = gimple_build_nop ();
4317 set_ssa_default_def (cfun
, var
, lhs
);
4319 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
4320 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
4321 gsi_replace (gsi
, new_stmt
, false);
4328 /* Check for indirect calls that became direct calls, and then
4329 no longer require a static chain. */
4330 if (gimple_call_chain (stmt
))
4332 tree fn
= gimple_call_fndecl (stmt
);
4333 if (fn
&& !DECL_STATIC_CHAIN (fn
))
4335 gimple_call_set_chain (stmt
, NULL
);
4340 tree tmp
= maybe_fold_reference (gimple_call_chain (stmt
), false);
4343 gimple_call_set_chain (stmt
, tmp
);
4352 /* Check for builtins that CCP can handle using information not
4353 available in the generic fold routines. */
4354 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
4356 if (gimple_fold_builtin (gsi
))
4359 else if (gimple_call_builtin_p (stmt
, BUILT_IN_MD
))
4361 changed
|= targetm
.gimple_fold_builtin (gsi
);
4363 else if (gimple_call_internal_p (stmt
))
4365 enum tree_code subcode
= ERROR_MARK
;
4366 tree result
= NULL_TREE
;
4367 bool cplx_result
= false;
4368 tree overflow
= NULL_TREE
;
4369 switch (gimple_call_internal_fn (stmt
))
4371 case IFN_BUILTIN_EXPECT
:
4372 result
= fold_builtin_expect (gimple_location (stmt
),
4373 gimple_call_arg (stmt
, 0),
4374 gimple_call_arg (stmt
, 1),
4375 gimple_call_arg (stmt
, 2),
4378 case IFN_UBSAN_OBJECT_SIZE
:
4380 tree offset
= gimple_call_arg (stmt
, 1);
4381 tree objsize
= gimple_call_arg (stmt
, 2);
4382 if (integer_all_onesp (objsize
)
4383 || (TREE_CODE (offset
) == INTEGER_CST
4384 && TREE_CODE (objsize
) == INTEGER_CST
4385 && tree_int_cst_le (offset
, objsize
)))
4387 replace_call_with_value (gsi
, NULL_TREE
);
4393 if (integer_zerop (gimple_call_arg (stmt
, 1)))
4395 replace_call_with_value (gsi
, NULL_TREE
);
4399 case IFN_UBSAN_BOUNDS
:
4401 tree index
= gimple_call_arg (stmt
, 1);
4402 tree bound
= gimple_call_arg (stmt
, 2);
4403 if (TREE_CODE (index
) == INTEGER_CST
4404 && TREE_CODE (bound
) == INTEGER_CST
)
4406 index
= fold_convert (TREE_TYPE (bound
), index
);
4407 if (TREE_CODE (index
) == INTEGER_CST
4408 && tree_int_cst_le (index
, bound
))
4410 replace_call_with_value (gsi
, NULL_TREE
);
4416 case IFN_GOACC_DIM_SIZE
:
4417 case IFN_GOACC_DIM_POS
:
4418 result
= fold_internal_goacc_dim (stmt
);
4420 case IFN_UBSAN_CHECK_ADD
:
4421 subcode
= PLUS_EXPR
;
4423 case IFN_UBSAN_CHECK_SUB
:
4424 subcode
= MINUS_EXPR
;
4426 case IFN_UBSAN_CHECK_MUL
:
4427 subcode
= MULT_EXPR
;
4429 case IFN_ADD_OVERFLOW
:
4430 subcode
= PLUS_EXPR
;
4433 case IFN_SUB_OVERFLOW
:
4434 subcode
= MINUS_EXPR
;
4437 case IFN_MUL_OVERFLOW
:
4438 subcode
= MULT_EXPR
;
4444 if (subcode
!= ERROR_MARK
)
4446 tree arg0
= gimple_call_arg (stmt
, 0);
4447 tree arg1
= gimple_call_arg (stmt
, 1);
4448 tree type
= TREE_TYPE (arg0
);
4451 tree lhs
= gimple_call_lhs (stmt
);
4452 if (lhs
== NULL_TREE
)
4455 type
= TREE_TYPE (TREE_TYPE (lhs
));
4457 if (type
== NULL_TREE
)
4459 /* x = y + 0; x = y - 0; x = y * 0; */
4460 else if (integer_zerop (arg1
))
4461 result
= subcode
== MULT_EXPR
? integer_zero_node
: arg0
;
4462 /* x = 0 + y; x = 0 * y; */
4463 else if (subcode
!= MINUS_EXPR
&& integer_zerop (arg0
))
4464 result
= subcode
== MULT_EXPR
? integer_zero_node
: arg1
;
4466 else if (subcode
== MINUS_EXPR
&& operand_equal_p (arg0
, arg1
, 0))
4467 result
= integer_zero_node
;
4468 /* x = y * 1; x = 1 * y; */
4469 else if (subcode
== MULT_EXPR
&& integer_onep (arg1
))
4471 else if (subcode
== MULT_EXPR
&& integer_onep (arg0
))
4473 else if (TREE_CODE (arg0
) == INTEGER_CST
4474 && TREE_CODE (arg1
) == INTEGER_CST
)
4477 result
= int_const_binop (subcode
, fold_convert (type
, arg0
),
4478 fold_convert (type
, arg1
));
4480 result
= int_const_binop (subcode
, arg0
, arg1
);
4481 if (result
&& arith_overflowed_p (subcode
, type
, arg0
, arg1
))
4484 overflow
= build_one_cst (type
);
4491 if (result
== integer_zero_node
)
4492 result
= build_zero_cst (type
);
4493 else if (cplx_result
&& TREE_TYPE (result
) != type
)
4495 if (TREE_CODE (result
) == INTEGER_CST
)
4497 if (arith_overflowed_p (PLUS_EXPR
, type
, result
,
4499 overflow
= build_one_cst (type
);
4501 else if ((!TYPE_UNSIGNED (TREE_TYPE (result
))
4502 && TYPE_UNSIGNED (type
))
4503 || (TYPE_PRECISION (type
)
4504 < (TYPE_PRECISION (TREE_TYPE (result
))
4505 + (TYPE_UNSIGNED (TREE_TYPE (result
))
4506 && !TYPE_UNSIGNED (type
)))))
4509 result
= fold_convert (type
, result
);
4516 if (TREE_CODE (result
) == INTEGER_CST
&& TREE_OVERFLOW (result
))
4517 result
= drop_tree_overflow (result
);
4520 if (overflow
== NULL_TREE
)
4521 overflow
= build_zero_cst (TREE_TYPE (result
));
4522 tree ctype
= build_complex_type (TREE_TYPE (result
));
4523 if (TREE_CODE (result
) == INTEGER_CST
4524 && TREE_CODE (overflow
) == INTEGER_CST
)
4525 result
= build_complex (ctype
, result
, overflow
);
4527 result
= build2_loc (gimple_location (stmt
), COMPLEX_EXPR
,
4528 ctype
, result
, overflow
);
4530 if (!update_call_from_tree (gsi
, result
))
4531 gimplify_and_update_call_from_tree (gsi
, result
);
4540 /* Return true whether NAME has a use on STMT. */
4543 has_use_on_stmt (tree name
, gimple
*stmt
)
4545 imm_use_iterator iter
;
4546 use_operand_p use_p
;
4547 FOR_EACH_IMM_USE_FAST (use_p
, iter
, name
)
4548 if (USE_STMT (use_p
) == stmt
)
4553 /* Worker for fold_stmt_1 dispatch to pattern based folding with
4556 Replaces *GSI with the simplification result in RCODE and OPS
4557 and the associated statements in *SEQ. Does the replacement
4558 according to INPLACE and returns true if the operation succeeded. */
4561 replace_stmt_with_simplification (gimple_stmt_iterator
*gsi
,
4562 gimple_match_op
*res_op
,
4563 gimple_seq
*seq
, bool inplace
)
4565 gimple
*stmt
= gsi_stmt (*gsi
);
4566 tree
*ops
= res_op
->ops
;
4567 unsigned int num_ops
= res_op
->num_ops
;
4569 /* Play safe and do not allow abnormals to be mentioned in
4570 newly created statements. See also maybe_push_res_to_seq.
4571 As an exception allow such uses if there was a use of the
4572 same SSA name on the old stmt. */
4573 for (unsigned int i
= 0; i
< num_ops
; ++i
)
4574 if (TREE_CODE (ops
[i
]) == SSA_NAME
4575 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[i
])
4576 && !has_use_on_stmt (ops
[i
], stmt
))
4579 if (num_ops
> 0 && COMPARISON_CLASS_P (ops
[0]))
4580 for (unsigned int i
= 0; i
< 2; ++i
)
4581 if (TREE_CODE (TREE_OPERAND (ops
[0], i
)) == SSA_NAME
4582 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops
[0], i
))
4583 && !has_use_on_stmt (TREE_OPERAND (ops
[0], i
), stmt
))
4586 /* Don't insert new statements when INPLACE is true, even if we could
4587 reuse STMT for the final statement. */
4588 if (inplace
&& !gimple_seq_empty_p (*seq
))
4591 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
4593 gcc_assert (res_op
->code
.is_tree_code ());
4594 if (TREE_CODE_CLASS ((enum tree_code
) res_op
->code
) == tcc_comparison
4595 /* GIMPLE_CONDs condition may not throw. */
4596 && (!flag_exceptions
4597 || !cfun
->can_throw_non_call_exceptions
4598 || !operation_could_trap_p (res_op
->code
,
4599 FLOAT_TYPE_P (TREE_TYPE (ops
[0])),
4601 gimple_cond_set_condition (cond_stmt
, res_op
->code
, ops
[0], ops
[1]);
4602 else if (res_op
->code
== SSA_NAME
)
4603 gimple_cond_set_condition (cond_stmt
, NE_EXPR
, ops
[0],
4604 build_zero_cst (TREE_TYPE (ops
[0])));
4605 else if (res_op
->code
== INTEGER_CST
)
4607 if (integer_zerop (ops
[0]))
4608 gimple_cond_make_false (cond_stmt
);
4610 gimple_cond_make_true (cond_stmt
);
4614 tree res
= maybe_push_res_to_seq (res_op
, seq
);
4617 gimple_cond_set_condition (cond_stmt
, NE_EXPR
, res
,
4618 build_zero_cst (TREE_TYPE (res
)));
4622 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4624 fprintf (dump_file
, "gimple_simplified to ");
4625 if (!gimple_seq_empty_p (*seq
))
4626 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
4627 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
),
4630 gsi_insert_seq_before (gsi
, *seq
, GSI_SAME_STMT
);
4633 else if (is_gimple_assign (stmt
)
4634 && res_op
->code
.is_tree_code ())
4637 || gimple_num_ops (stmt
) > get_gimple_rhs_num_ops (res_op
->code
))
4639 maybe_build_generic_op (res_op
);
4640 gimple_assign_set_rhs_with_ops (gsi
, res_op
->code
,
4641 res_op
->op_or_null (0),
4642 res_op
->op_or_null (1),
4643 res_op
->op_or_null (2));
4644 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4646 fprintf (dump_file
, "gimple_simplified to ");
4647 if (!gimple_seq_empty_p (*seq
))
4648 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
4649 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
),
4652 gsi_insert_seq_before (gsi
, *seq
, GSI_SAME_STMT
);
4656 else if (res_op
->code
.is_fn_code ()
4657 && gimple_call_combined_fn (stmt
) == res_op
->code
)
4659 gcc_assert (num_ops
== gimple_call_num_args (stmt
));
4660 for (unsigned int i
= 0; i
< num_ops
; ++i
)
4661 gimple_call_set_arg (stmt
, i
, ops
[i
]);
4662 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4664 fprintf (dump_file
, "gimple_simplified to ");
4665 if (!gimple_seq_empty_p (*seq
))
4666 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
4667 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
), 0, TDF_SLIM
);
4669 gsi_insert_seq_before (gsi
, *seq
, GSI_SAME_STMT
);
4674 if (gimple_has_lhs (stmt
))
4676 tree lhs
= gimple_get_lhs (stmt
);
4677 if (!maybe_push_res_to_seq (res_op
, seq
, lhs
))
4679 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4681 fprintf (dump_file
, "gimple_simplified to ");
4682 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
4684 gsi_replace_with_seq_vops (gsi
, *seq
);
4694 /* Canonicalize MEM_REFs invariant address operand after propagation. */
4697 maybe_canonicalize_mem_ref_addr (tree
*t
)
4701 if (TREE_CODE (*t
) == ADDR_EXPR
)
4702 t
= &TREE_OPERAND (*t
, 0);
4704 /* The C and C++ frontends use an ARRAY_REF for indexing with their
4705 generic vector extension. The actual vector referenced is
4706 view-converted to an array type for this purpose. If the index
4707 is constant the canonical representation in the middle-end is a
4708 BIT_FIELD_REF so re-write the former to the latter here. */
4709 if (TREE_CODE (*t
) == ARRAY_REF
4710 && TREE_CODE (TREE_OPERAND (*t
, 0)) == VIEW_CONVERT_EXPR
4711 && TREE_CODE (TREE_OPERAND (*t
, 1)) == INTEGER_CST
4712 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t
, 0), 0))))
4714 tree vtype
= TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t
, 0), 0));
4715 if (VECTOR_TYPE_P (vtype
))
4717 tree low
= array_ref_low_bound (*t
);
4718 if (TREE_CODE (low
) == INTEGER_CST
)
4720 if (tree_int_cst_le (low
, TREE_OPERAND (*t
, 1)))
4722 widest_int idx
= wi::sub (wi::to_widest (TREE_OPERAND (*t
, 1)),
4723 wi::to_widest (low
));
4724 idx
= wi::mul (idx
, wi::to_widest
4725 (TYPE_SIZE (TREE_TYPE (*t
))));
4727 = wi::add (idx
, wi::to_widest (TYPE_SIZE (TREE_TYPE (*t
))));
4728 if (wi::les_p (ext
, wi::to_widest (TYPE_SIZE (vtype
))))
4730 *t
= build3_loc (EXPR_LOCATION (*t
), BIT_FIELD_REF
,
4732 TREE_OPERAND (TREE_OPERAND (*t
, 0), 0),
4733 TYPE_SIZE (TREE_TYPE (*t
)),
4734 wide_int_to_tree (bitsizetype
, idx
));
4742 while (handled_component_p (*t
))
4743 t
= &TREE_OPERAND (*t
, 0);
4745 /* Canonicalize MEM [&foo.bar, 0] which appears after propagating
4746 of invariant addresses into a SSA name MEM_REF address. */
4747 if (TREE_CODE (*t
) == MEM_REF
4748 || TREE_CODE (*t
) == TARGET_MEM_REF
)
4750 tree addr
= TREE_OPERAND (*t
, 0);
4751 if (TREE_CODE (addr
) == ADDR_EXPR
4752 && (TREE_CODE (TREE_OPERAND (addr
, 0)) == MEM_REF
4753 || handled_component_p (TREE_OPERAND (addr
, 0))))
4757 base
= get_addr_base_and_unit_offset (TREE_OPERAND (addr
, 0),
4762 TREE_OPERAND (*t
, 0) = build_fold_addr_expr (base
);
4763 TREE_OPERAND (*t
, 1) = int_const_binop (PLUS_EXPR
,
4764 TREE_OPERAND (*t
, 1),
4765 size_int (coffset
));
4768 gcc_checking_assert (TREE_CODE (TREE_OPERAND (*t
, 0)) == DEBUG_EXPR_DECL
4769 || is_gimple_mem_ref_addr (TREE_OPERAND (*t
, 0)));
4772 /* Canonicalize back MEM_REFs to plain reference trees if the object
4773 accessed is a decl that has the same access semantics as the MEM_REF. */
4774 if (TREE_CODE (*t
) == MEM_REF
4775 && TREE_CODE (TREE_OPERAND (*t
, 0)) == ADDR_EXPR
4776 && integer_zerop (TREE_OPERAND (*t
, 1))
4777 && MR_DEPENDENCE_CLIQUE (*t
) == 0)
4779 tree decl
= TREE_OPERAND (TREE_OPERAND (*t
, 0), 0);
4780 tree alias_type
= TREE_TYPE (TREE_OPERAND (*t
, 1));
4781 if (/* Same volatile qualification. */
4782 TREE_THIS_VOLATILE (*t
) == TREE_THIS_VOLATILE (decl
)
4783 /* Same TBAA behavior with -fstrict-aliasing. */
4784 && !TYPE_REF_CAN_ALIAS_ALL (alias_type
)
4785 && (TYPE_MAIN_VARIANT (TREE_TYPE (decl
))
4786 == TYPE_MAIN_VARIANT (TREE_TYPE (alias_type
)))
4787 /* Same alignment. */
4788 && TYPE_ALIGN (TREE_TYPE (decl
)) == TYPE_ALIGN (TREE_TYPE (*t
))
4789 /* We have to look out here to not drop a required conversion
4790 from the rhs to the lhs if *t appears on the lhs or vice-versa
4791 if it appears on the rhs. Thus require strict type
4793 && types_compatible_p (TREE_TYPE (*t
), TREE_TYPE (decl
)))
4795 *t
= TREE_OPERAND (TREE_OPERAND (*t
, 0), 0);
4800 /* Canonicalize TARGET_MEM_REF in particular with respect to
4801 the indexes becoming constant. */
4802 else if (TREE_CODE (*t
) == TARGET_MEM_REF
)
4804 tree tem
= maybe_fold_tmr (*t
);
4815 /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument
4816 distinguishes both cases. */
4819 fold_stmt_1 (gimple_stmt_iterator
*gsi
, bool inplace
, tree (*valueize
) (tree
))
4821 bool changed
= false;
4822 gimple
*stmt
= gsi_stmt (*gsi
);
4823 bool nowarning
= gimple_no_warning_p (stmt
);
4825 fold_defer_overflow_warnings ();
4827 /* First do required canonicalization of [TARGET_]MEM_REF addresses
4829 ??? This shouldn't be done in generic folding but in the
4830 propagation helpers which also know whether an address was
4832 Also canonicalize operand order. */
4833 switch (gimple_code (stmt
))
4836 if (gimple_assign_rhs_class (stmt
) == GIMPLE_SINGLE_RHS
)
4838 tree
*rhs
= gimple_assign_rhs1_ptr (stmt
);
4839 if ((REFERENCE_CLASS_P (*rhs
)
4840 || TREE_CODE (*rhs
) == ADDR_EXPR
)
4841 && maybe_canonicalize_mem_ref_addr (rhs
))
4843 tree
*lhs
= gimple_assign_lhs_ptr (stmt
);
4844 if (REFERENCE_CLASS_P (*lhs
)
4845 && maybe_canonicalize_mem_ref_addr (lhs
))
4850 /* Canonicalize operand order. */
4851 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4852 if (TREE_CODE_CLASS (code
) == tcc_comparison
4853 || commutative_tree_code (code
)
4854 || commutative_ternary_tree_code (code
))
4856 tree rhs1
= gimple_assign_rhs1 (stmt
);
4857 tree rhs2
= gimple_assign_rhs2 (stmt
);
4858 if (tree_swap_operands_p (rhs1
, rhs2
))
4860 gimple_assign_set_rhs1 (stmt
, rhs2
);
4861 gimple_assign_set_rhs2 (stmt
, rhs1
);
4862 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4863 gimple_assign_set_rhs_code (stmt
,
4864 swap_tree_comparison (code
));
4872 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
4874 tree
*arg
= gimple_call_arg_ptr (stmt
, i
);
4875 if (REFERENCE_CLASS_P (*arg
)
4876 && maybe_canonicalize_mem_ref_addr (arg
))
4879 tree
*lhs
= gimple_call_lhs_ptr (stmt
);
4881 && REFERENCE_CLASS_P (*lhs
)
4882 && maybe_canonicalize_mem_ref_addr (lhs
))
4888 gasm
*asm_stmt
= as_a
<gasm
*> (stmt
);
4889 for (i
= 0; i
< gimple_asm_noutputs (asm_stmt
); ++i
)
4891 tree link
= gimple_asm_output_op (asm_stmt
, i
);
4892 tree op
= TREE_VALUE (link
);
4893 if (REFERENCE_CLASS_P (op
)
4894 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link
)))
4897 for (i
= 0; i
< gimple_asm_ninputs (asm_stmt
); ++i
)
4899 tree link
= gimple_asm_input_op (asm_stmt
, i
);
4900 tree op
= TREE_VALUE (link
);
4901 if ((REFERENCE_CLASS_P (op
)
4902 || TREE_CODE (op
) == ADDR_EXPR
)
4903 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link
)))
4909 if (gimple_debug_bind_p (stmt
))
4911 tree
*val
= gimple_debug_bind_get_value_ptr (stmt
);
4913 && (REFERENCE_CLASS_P (*val
)
4914 || TREE_CODE (*val
) == ADDR_EXPR
)
4915 && maybe_canonicalize_mem_ref_addr (val
))
4921 /* Canonicalize operand order. */
4922 tree lhs
= gimple_cond_lhs (stmt
);
4923 tree rhs
= gimple_cond_rhs (stmt
);
4924 if (tree_swap_operands_p (lhs
, rhs
))
4926 gcond
*gc
= as_a
<gcond
*> (stmt
);
4927 gimple_cond_set_lhs (gc
, rhs
);
4928 gimple_cond_set_rhs (gc
, lhs
);
4929 gimple_cond_set_code (gc
,
4930 swap_tree_comparison (gimple_cond_code (gc
)));
4937 /* Dispatch to pattern-based folding. */
4939 || is_gimple_assign (stmt
)
4940 || gimple_code (stmt
) == GIMPLE_COND
)
4942 gimple_seq seq
= NULL
;
4943 gimple_match_op res_op
;
4944 if (gimple_simplify (stmt
, &res_op
, inplace
? NULL
: &seq
,
4945 valueize
, valueize
))
4947 if (replace_stmt_with_simplification (gsi
, &res_op
, &seq
, inplace
))
4950 gimple_seq_discard (seq
);
4954 stmt
= gsi_stmt (*gsi
);
4956 /* Fold the main computation performed by the statement. */
4957 switch (gimple_code (stmt
))
4961 /* Try to canonicalize for boolean-typed X the comparisons
4962 X == 0, X == 1, X != 0, and X != 1. */
4963 if (gimple_assign_rhs_code (stmt
) == EQ_EXPR
4964 || gimple_assign_rhs_code (stmt
) == NE_EXPR
)
4966 tree lhs
= gimple_assign_lhs (stmt
);
4967 tree op1
= gimple_assign_rhs1 (stmt
);
4968 tree op2
= gimple_assign_rhs2 (stmt
);
4969 tree type
= TREE_TYPE (op1
);
4971 /* Check whether the comparison operands are of the same boolean
4972 type as the result type is.
4973 Check that second operand is an integer-constant with value
4975 if (TREE_CODE (op2
) == INTEGER_CST
4976 && (integer_zerop (op2
) || integer_onep (op2
))
4977 && useless_type_conversion_p (TREE_TYPE (lhs
), type
))
4979 enum tree_code cmp_code
= gimple_assign_rhs_code (stmt
);
4980 bool is_logical_not
= false;
4982 /* X == 0 and X != 1 is a logical-not.of X
4983 X == 1 and X != 0 is X */
4984 if ((cmp_code
== EQ_EXPR
&& integer_zerop (op2
))
4985 || (cmp_code
== NE_EXPR
&& integer_onep (op2
)))
4986 is_logical_not
= true;
4988 if (is_logical_not
== false)
4989 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op1
), op1
);
4990 /* Only for one-bit precision typed X the transformation
4991 !X -> ~X is valied. */
4992 else if (TYPE_PRECISION (type
) == 1)
4993 gimple_assign_set_rhs_with_ops (gsi
, BIT_NOT_EXPR
, op1
);
4994 /* Otherwise we use !X -> X ^ 1. */
4996 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op1
,
4997 build_int_cst (type
, 1));
5003 unsigned old_num_ops
= gimple_num_ops (stmt
);
5004 tree lhs
= gimple_assign_lhs (stmt
);
5005 tree new_rhs
= fold_gimple_assign (gsi
);
5007 && !useless_type_conversion_p (TREE_TYPE (lhs
),
5008 TREE_TYPE (new_rhs
)))
5009 new_rhs
= fold_convert (TREE_TYPE (lhs
), new_rhs
);
5012 || get_gimple_rhs_num_ops (TREE_CODE (new_rhs
)) < old_num_ops
))
5014 gimple_assign_set_rhs_from_tree (gsi
, new_rhs
);
5021 changed
|= gimple_fold_call (gsi
, inplace
);
5025 /* Fold *& in asm operands. */
5027 gasm
*asm_stmt
= as_a
<gasm
*> (stmt
);
5029 const char **oconstraints
;
5030 const char *constraint
;
5031 bool allows_mem
, allows_reg
;
5033 noutputs
= gimple_asm_noutputs (asm_stmt
);
5034 oconstraints
= XALLOCAVEC (const char *, noutputs
);
5036 for (i
= 0; i
< gimple_asm_noutputs (asm_stmt
); ++i
)
5038 tree link
= gimple_asm_output_op (asm_stmt
, i
);
5039 tree op
= TREE_VALUE (link
);
5041 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
5042 if (REFERENCE_CLASS_P (op
)
5043 && (op
= maybe_fold_reference (op
, true)) != NULL_TREE
)
5045 TREE_VALUE (link
) = op
;
5049 for (i
= 0; i
< gimple_asm_ninputs (asm_stmt
); ++i
)
5051 tree link
= gimple_asm_input_op (asm_stmt
, i
);
5052 tree op
= TREE_VALUE (link
);
5054 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
5055 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0,
5056 oconstraints
, &allows_mem
, &allows_reg
);
5057 if (REFERENCE_CLASS_P (op
)
5058 && (op
= maybe_fold_reference (op
, !allows_reg
&& allows_mem
))
5061 TREE_VALUE (link
) = op
;
5069 if (gimple_debug_bind_p (stmt
))
5071 tree val
= gimple_debug_bind_get_value (stmt
);
5073 && REFERENCE_CLASS_P (val
))
5075 tree tem
= maybe_fold_reference (val
, false);
5078 gimple_debug_bind_set_value (stmt
, tem
);
5083 && TREE_CODE (val
) == ADDR_EXPR
)
5085 tree ref
= TREE_OPERAND (val
, 0);
5086 tree tem
= maybe_fold_reference (ref
, false);
5089 tem
= build_fold_addr_expr_with_type (tem
, TREE_TYPE (val
));
5090 gimple_debug_bind_set_value (stmt
, tem
);
5099 greturn
*ret_stmt
= as_a
<greturn
*> (stmt
);
5100 tree ret
= gimple_return_retval(ret_stmt
);
5102 if (ret
&& TREE_CODE (ret
) == SSA_NAME
&& valueize
)
5104 tree val
= valueize (ret
);
5105 if (val
&& val
!= ret
5106 && may_propagate_copy (ret
, val
))
5108 gimple_return_set_retval (ret_stmt
, val
);
5118 stmt
= gsi_stmt (*gsi
);
5120 /* Fold *& on the lhs. */
5121 if (gimple_has_lhs (stmt
))
5123 tree lhs
= gimple_get_lhs (stmt
);
5124 if (lhs
&& REFERENCE_CLASS_P (lhs
))
5126 tree new_lhs
= maybe_fold_reference (lhs
, true);
5129 gimple_set_lhs (stmt
, new_lhs
);
5135 fold_undefer_overflow_warnings (changed
&& !nowarning
, stmt
, 0);
5139 /* Valueziation callback that ends up not following SSA edges. */
5142 no_follow_ssa_edges (tree
)
5147 /* Valueization callback that ends up following single-use SSA edges only. */
5150 follow_single_use_edges (tree val
)
5152 if (TREE_CODE (val
) == SSA_NAME
5153 && !has_single_use (val
))
5158 /* Valueization callback that follows all SSA edges. */
5161 follow_all_ssa_edges (tree val
)
5166 /* Fold the statement pointed to by GSI. In some cases, this function may
5167 replace the whole statement with a new one. Returns true iff folding
5169 The statement pointed to by GSI should be in valid gimple form but may
5170 be in unfolded state as resulting from for example constant propagation
5171 which can produce *&x = 0. */
5174 fold_stmt (gimple_stmt_iterator
*gsi
)
5176 return fold_stmt_1 (gsi
, false, no_follow_ssa_edges
);
5180 fold_stmt (gimple_stmt_iterator
*gsi
, tree (*valueize
) (tree
))
5182 return fold_stmt_1 (gsi
, false, valueize
);
5185 /* Perform the minimal folding on statement *GSI. Only operations like
5186 *&x created by constant propagation are handled. The statement cannot
5187 be replaced with a new one. Return true if the statement was
5188 changed, false otherwise.
5189 The statement *GSI should be in valid gimple form but may
5190 be in unfolded state as resulting from for example constant propagation
5191 which can produce *&x = 0. */
5194 fold_stmt_inplace (gimple_stmt_iterator
*gsi
)
5196 gimple
*stmt
= gsi_stmt (*gsi
);
5197 bool changed
= fold_stmt_1 (gsi
, true, no_follow_ssa_edges
);
5198 gcc_assert (gsi_stmt (*gsi
) == stmt
);
5202 /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
5203 if EXPR is null or we don't know how.
5204 If non-null, the result always has boolean type. */
5207 canonicalize_bool (tree expr
, bool invert
)
5213 if (integer_nonzerop (expr
))
5214 return boolean_false_node
;
5215 else if (integer_zerop (expr
))
5216 return boolean_true_node
;
5217 else if (TREE_CODE (expr
) == SSA_NAME
)
5218 return fold_build2 (EQ_EXPR
, boolean_type_node
, expr
,
5219 build_int_cst (TREE_TYPE (expr
), 0));
5220 else if (COMPARISON_CLASS_P (expr
))
5221 return fold_build2 (invert_tree_comparison (TREE_CODE (expr
), false),
5223 TREE_OPERAND (expr
, 0),
5224 TREE_OPERAND (expr
, 1));
5230 if (TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
)
5232 if (integer_nonzerop (expr
))
5233 return boolean_true_node
;
5234 else if (integer_zerop (expr
))
5235 return boolean_false_node
;
5236 else if (TREE_CODE (expr
) == SSA_NAME
)
5237 return fold_build2 (NE_EXPR
, boolean_type_node
, expr
,
5238 build_int_cst (TREE_TYPE (expr
), 0));
5239 else if (COMPARISON_CLASS_P (expr
))
5240 return fold_build2 (TREE_CODE (expr
),
5242 TREE_OPERAND (expr
, 0),
5243 TREE_OPERAND (expr
, 1));
5249 /* Check to see if a boolean expression EXPR is logically equivalent to the
5250 comparison (OP1 CODE OP2). Check for various identities involving
5254 same_bool_comparison_p (const_tree expr
, enum tree_code code
,
5255 const_tree op1
, const_tree op2
)
5259 /* The obvious case. */
5260 if (TREE_CODE (expr
) == code
5261 && operand_equal_p (TREE_OPERAND (expr
, 0), op1
, 0)
5262 && operand_equal_p (TREE_OPERAND (expr
, 1), op2
, 0))
5265 /* Check for comparing (name, name != 0) and the case where expr
5266 is an SSA_NAME with a definition matching the comparison. */
5267 if (TREE_CODE (expr
) == SSA_NAME
5268 && TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
)
5270 if (operand_equal_p (expr
, op1
, 0))
5271 return ((code
== NE_EXPR
&& integer_zerop (op2
))
5272 || (code
== EQ_EXPR
&& integer_nonzerop (op2
)));
5273 s
= SSA_NAME_DEF_STMT (expr
);
5274 if (is_gimple_assign (s
)
5275 && gimple_assign_rhs_code (s
) == code
5276 && operand_equal_p (gimple_assign_rhs1 (s
), op1
, 0)
5277 && operand_equal_p (gimple_assign_rhs2 (s
), op2
, 0))
5281 /* If op1 is of the form (name != 0) or (name == 0), and the definition
5282 of name is a comparison, recurse. */
5283 if (TREE_CODE (op1
) == SSA_NAME
5284 && TREE_CODE (TREE_TYPE (op1
)) == BOOLEAN_TYPE
)
5286 s
= SSA_NAME_DEF_STMT (op1
);
5287 if (is_gimple_assign (s
)
5288 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
)
5290 enum tree_code c
= gimple_assign_rhs_code (s
);
5291 if ((c
== NE_EXPR
&& integer_zerop (op2
))
5292 || (c
== EQ_EXPR
&& integer_nonzerop (op2
)))
5293 return same_bool_comparison_p (expr
, c
,
5294 gimple_assign_rhs1 (s
),
5295 gimple_assign_rhs2 (s
));
5296 if ((c
== EQ_EXPR
&& integer_zerop (op2
))
5297 || (c
== NE_EXPR
&& integer_nonzerop (op2
)))
5298 return same_bool_comparison_p (expr
,
5299 invert_tree_comparison (c
, false),
5300 gimple_assign_rhs1 (s
),
5301 gimple_assign_rhs2 (s
));
5307 /* Check to see if two boolean expressions OP1 and OP2 are logically
5311 same_bool_result_p (const_tree op1
, const_tree op2
)
5313 /* Simple cases first. */
5314 if (operand_equal_p (op1
, op2
, 0))
5317 /* Check the cases where at least one of the operands is a comparison.
5318 These are a bit smarter than operand_equal_p in that they apply some
5319 identifies on SSA_NAMEs. */
5320 if (COMPARISON_CLASS_P (op2
)
5321 && same_bool_comparison_p (op1
, TREE_CODE (op2
),
5322 TREE_OPERAND (op2
, 0),
5323 TREE_OPERAND (op2
, 1)))
5325 if (COMPARISON_CLASS_P (op1
)
5326 && same_bool_comparison_p (op2
, TREE_CODE (op1
),
5327 TREE_OPERAND (op1
, 0),
5328 TREE_OPERAND (op1
, 1)))
5335 /* Forward declarations for some mutually recursive functions. */
5338 and_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
5339 enum tree_code code2
, tree op2a
, tree op2b
);
5341 and_var_with_comparison (tree var
, bool invert
,
5342 enum tree_code code2
, tree op2a
, tree op2b
);
5344 and_var_with_comparison_1 (gimple
*stmt
,
5345 enum tree_code code2
, tree op2a
, tree op2b
);
5347 or_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
5348 enum tree_code code2
, tree op2a
, tree op2b
);
5350 or_var_with_comparison (tree var
, bool invert
,
5351 enum tree_code code2
, tree op2a
, tree op2b
);
5353 or_var_with_comparison_1 (gimple
*stmt
,
5354 enum tree_code code2
, tree op2a
, tree op2b
);
5356 /* Helper function for and_comparisons_1: try to simplify the AND of the
5357 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
5358 If INVERT is true, invert the value of the VAR before doing the AND.
5359 Return NULL_EXPR if we can't simplify this to a single expression. */
5362 and_var_with_comparison (tree var
, bool invert
,
5363 enum tree_code code2
, tree op2a
, tree op2b
)
5366 gimple
*stmt
= SSA_NAME_DEF_STMT (var
);
5368 /* We can only deal with variables whose definitions are assignments. */
5369 if (!is_gimple_assign (stmt
))
5372 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
5373 !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
5374 Then we only have to consider the simpler non-inverted cases. */
5376 t
= or_var_with_comparison_1 (stmt
,
5377 invert_tree_comparison (code2
, false),
5380 t
= and_var_with_comparison_1 (stmt
, code2
, op2a
, op2b
);
5381 return canonicalize_bool (t
, invert
);
5384 /* Try to simplify the AND of the ssa variable defined by the assignment
5385 STMT with the comparison specified by (OP2A CODE2 OP2B).
5386 Return NULL_EXPR if we can't simplify this to a single expression. */
5389 and_var_with_comparison_1 (gimple
*stmt
,
5390 enum tree_code code2
, tree op2a
, tree op2b
)
5392 tree var
= gimple_assign_lhs (stmt
);
5393 tree true_test_var
= NULL_TREE
;
5394 tree false_test_var
= NULL_TREE
;
5395 enum tree_code innercode
= gimple_assign_rhs_code (stmt
);
5397 /* Check for identities like (var AND (var == 0)) => false. */
5398 if (TREE_CODE (op2a
) == SSA_NAME
5399 && TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
)
5401 if ((code2
== NE_EXPR
&& integer_zerop (op2b
))
5402 || (code2
== EQ_EXPR
&& integer_nonzerop (op2b
)))
5404 true_test_var
= op2a
;
5405 if (var
== true_test_var
)
5408 else if ((code2
== EQ_EXPR
&& integer_zerop (op2b
))
5409 || (code2
== NE_EXPR
&& integer_nonzerop (op2b
)))
5411 false_test_var
= op2a
;
5412 if (var
== false_test_var
)
5413 return boolean_false_node
;
5417 /* If the definition is a comparison, recurse on it. */
5418 if (TREE_CODE_CLASS (innercode
) == tcc_comparison
)
5420 tree t
= and_comparisons_1 (innercode
,
5421 gimple_assign_rhs1 (stmt
),
5422 gimple_assign_rhs2 (stmt
),
5430 /* If the definition is an AND or OR expression, we may be able to
5431 simplify by reassociating. */
5432 if (TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
5433 && (innercode
== BIT_AND_EXPR
|| innercode
== BIT_IOR_EXPR
))
5435 tree inner1
= gimple_assign_rhs1 (stmt
);
5436 tree inner2
= gimple_assign_rhs2 (stmt
);
5439 tree partial
= NULL_TREE
;
5440 bool is_and
= (innercode
== BIT_AND_EXPR
);
5442 /* Check for boolean identities that don't require recursive examination
5444 inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
5445 inner1 AND (inner1 OR inner2) => inner1
5446 !inner1 AND (inner1 AND inner2) => false
5447 !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
5448 Likewise for similar cases involving inner2. */
5449 if (inner1
== true_test_var
)
5450 return (is_and
? var
: inner1
);
5451 else if (inner2
== true_test_var
)
5452 return (is_and
? var
: inner2
);
5453 else if (inner1
== false_test_var
)
5455 ? boolean_false_node
5456 : and_var_with_comparison (inner2
, false, code2
, op2a
, op2b
));
5457 else if (inner2
== false_test_var
)
5459 ? boolean_false_node
5460 : and_var_with_comparison (inner1
, false, code2
, op2a
, op2b
));
5462 /* Next, redistribute/reassociate the AND across the inner tests.
5463 Compute the first partial result, (inner1 AND (op2a code op2b)) */
5464 if (TREE_CODE (inner1
) == SSA_NAME
5465 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner1
))
5466 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
5467 && (t
= maybe_fold_and_comparisons (gimple_assign_rhs_code (s
),
5468 gimple_assign_rhs1 (s
),
5469 gimple_assign_rhs2 (s
),
5470 code2
, op2a
, op2b
)))
5472 /* Handle the AND case, where we are reassociating:
5473 (inner1 AND inner2) AND (op2a code2 op2b)
5475 If the partial result t is a constant, we win. Otherwise
5476 continue on to try reassociating with the other inner test. */
5479 if (integer_onep (t
))
5481 else if (integer_zerop (t
))
5482 return boolean_false_node
;
5485 /* Handle the OR case, where we are redistributing:
5486 (inner1 OR inner2) AND (op2a code2 op2b)
5487 => (t OR (inner2 AND (op2a code2 op2b))) */
5488 else if (integer_onep (t
))
5489 return boolean_true_node
;
5491 /* Save partial result for later. */
5495 /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
5496 if (TREE_CODE (inner2
) == SSA_NAME
5497 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner2
))
5498 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
5499 && (t
= maybe_fold_and_comparisons (gimple_assign_rhs_code (s
),
5500 gimple_assign_rhs1 (s
),
5501 gimple_assign_rhs2 (s
),
5502 code2
, op2a
, op2b
)))
5504 /* Handle the AND case, where we are reassociating:
5505 (inner1 AND inner2) AND (op2a code2 op2b)
5506 => (inner1 AND t) */
5509 if (integer_onep (t
))
5511 else if (integer_zerop (t
))
5512 return boolean_false_node
;
5513 /* If both are the same, we can apply the identity
5515 else if (partial
&& same_bool_result_p (t
, partial
))
5519 /* Handle the OR case. where we are redistributing:
5520 (inner1 OR inner2) AND (op2a code2 op2b)
5521 => (t OR (inner1 AND (op2a code2 op2b)))
5522 => (t OR partial) */
5525 if (integer_onep (t
))
5526 return boolean_true_node
;
5529 /* We already got a simplification for the other
5530 operand to the redistributed OR expression. The
5531 interesting case is when at least one is false.
5532 Or, if both are the same, we can apply the identity
5534 if (integer_zerop (partial
))
5536 else if (integer_zerop (t
))
5538 else if (same_bool_result_p (t
, partial
))
5547 /* Try to simplify the AND of two comparisons defined by
5548 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
5549 If this can be done without constructing an intermediate value,
5550 return the resulting tree; otherwise NULL_TREE is returned.
5551 This function is deliberately asymmetric as it recurses on SSA_DEFs
5552 in the first comparison but not the second. */
5555 and_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
5556 enum tree_code code2
, tree op2a
, tree op2b
)
5558 tree truth_type
= truth_type_for (TREE_TYPE (op1a
));
5560 /* First check for ((x CODE1 y) AND (x CODE2 y)). */
5561 if (operand_equal_p (op1a
, op2a
, 0)
5562 && operand_equal_p (op1b
, op2b
, 0))
5564 /* Result will be either NULL_TREE, or a combined comparison. */
5565 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
5566 TRUTH_ANDIF_EXPR
, code1
, code2
,
5567 truth_type
, op1a
, op1b
);
5572 /* Likewise the swapped case of the above. */
5573 if (operand_equal_p (op1a
, op2b
, 0)
5574 && operand_equal_p (op1b
, op2a
, 0))
5576 /* Result will be either NULL_TREE, or a combined comparison. */
5577 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
5578 TRUTH_ANDIF_EXPR
, code1
,
5579 swap_tree_comparison (code2
),
5580 truth_type
, op1a
, op1b
);
5585 /* If both comparisons are of the same value against constants, we might
5586 be able to merge them. */
5587 if (operand_equal_p (op1a
, op2a
, 0)
5588 && TREE_CODE (op1b
) == INTEGER_CST
5589 && TREE_CODE (op2b
) == INTEGER_CST
)
5591 int cmp
= tree_int_cst_compare (op1b
, op2b
);
5593 /* If we have (op1a == op1b), we should either be able to
5594 return that or FALSE, depending on whether the constant op1b
5595 also satisfies the other comparison against op2b. */
5596 if (code1
== EQ_EXPR
)
5602 case EQ_EXPR
: val
= (cmp
== 0); break;
5603 case NE_EXPR
: val
= (cmp
!= 0); break;
5604 case LT_EXPR
: val
= (cmp
< 0); break;
5605 case GT_EXPR
: val
= (cmp
> 0); break;
5606 case LE_EXPR
: val
= (cmp
<= 0); break;
5607 case GE_EXPR
: val
= (cmp
>= 0); break;
5608 default: done
= false;
5613 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5615 return boolean_false_node
;
5618 /* Likewise if the second comparison is an == comparison. */
5619 else if (code2
== EQ_EXPR
)
5625 case EQ_EXPR
: val
= (cmp
== 0); break;
5626 case NE_EXPR
: val
= (cmp
!= 0); break;
5627 case LT_EXPR
: val
= (cmp
> 0); break;
5628 case GT_EXPR
: val
= (cmp
< 0); break;
5629 case LE_EXPR
: val
= (cmp
>= 0); break;
5630 case GE_EXPR
: val
= (cmp
<= 0); break;
5631 default: done
= false;
5636 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5638 return boolean_false_node
;
5642 /* Same business with inequality tests. */
5643 else if (code1
== NE_EXPR
)
5648 case EQ_EXPR
: val
= (cmp
!= 0); break;
5649 case NE_EXPR
: val
= (cmp
== 0); break;
5650 case LT_EXPR
: val
= (cmp
>= 0); break;
5651 case GT_EXPR
: val
= (cmp
<= 0); break;
5652 case LE_EXPR
: val
= (cmp
> 0); break;
5653 case GE_EXPR
: val
= (cmp
< 0); break;
5658 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5660 else if (code2
== NE_EXPR
)
5665 case EQ_EXPR
: val
= (cmp
== 0); break;
5666 case NE_EXPR
: val
= (cmp
!= 0); break;
5667 case LT_EXPR
: val
= (cmp
<= 0); break;
5668 case GT_EXPR
: val
= (cmp
>= 0); break;
5669 case LE_EXPR
: val
= (cmp
< 0); break;
5670 case GE_EXPR
: val
= (cmp
> 0); break;
5675 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5678 /* Chose the more restrictive of two < or <= comparisons. */
5679 else if ((code1
== LT_EXPR
|| code1
== LE_EXPR
)
5680 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
5682 if ((cmp
< 0) || (cmp
== 0 && code1
== LT_EXPR
))
5683 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5685 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5688 /* Likewise chose the more restrictive of two > or >= comparisons. */
5689 else if ((code1
== GT_EXPR
|| code1
== GE_EXPR
)
5690 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
5692 if ((cmp
> 0) || (cmp
== 0 && code1
== GT_EXPR
))
5693 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5695 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5698 /* Check for singleton ranges. */
5700 && ((code1
== LE_EXPR
&& code2
== GE_EXPR
)
5701 || (code1
== GE_EXPR
&& code2
== LE_EXPR
)))
5702 return fold_build2 (EQ_EXPR
, boolean_type_node
, op1a
, op2b
);
5704 /* Check for disjoint ranges. */
5706 && (code1
== LT_EXPR
|| code1
== LE_EXPR
)
5707 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
5708 return boolean_false_node
;
5710 && (code1
== GT_EXPR
|| code1
== GE_EXPR
)
5711 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
5712 return boolean_false_node
;
5715 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
5716 NAME's definition is a truth value. See if there are any simplifications
5717 that can be done against the NAME's definition. */
5718 if (TREE_CODE (op1a
) == SSA_NAME
5719 && (code1
== NE_EXPR
|| code1
== EQ_EXPR
)
5720 && (integer_zerop (op1b
) || integer_onep (op1b
)))
5722 bool invert
= ((code1
== EQ_EXPR
&& integer_zerop (op1b
))
5723 || (code1
== NE_EXPR
&& integer_onep (op1b
)));
5724 gimple
*stmt
= SSA_NAME_DEF_STMT (op1a
);
5725 switch (gimple_code (stmt
))
5728 /* Try to simplify by copy-propagating the definition. */
5729 return and_var_with_comparison (op1a
, invert
, code2
, op2a
, op2b
);
5732 /* If every argument to the PHI produces the same result when
5733 ANDed with the second comparison, we win.
5734 Do not do this unless the type is bool since we need a bool
5735 result here anyway. */
5736 if (TREE_CODE (TREE_TYPE (op1a
)) == BOOLEAN_TYPE
)
5738 tree result
= NULL_TREE
;
5740 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
5742 tree arg
= gimple_phi_arg_def (stmt
, i
);
5744 /* If this PHI has itself as an argument, ignore it.
5745 If all the other args produce the same result,
5747 if (arg
== gimple_phi_result (stmt
))
5749 else if (TREE_CODE (arg
) == INTEGER_CST
)
5751 if (invert
? integer_nonzerop (arg
) : integer_zerop (arg
))
5754 result
= boolean_false_node
;
5755 else if (!integer_zerop (result
))
5759 result
= fold_build2 (code2
, boolean_type_node
,
5761 else if (!same_bool_comparison_p (result
,
5765 else if (TREE_CODE (arg
) == SSA_NAME
5766 && !SSA_NAME_IS_DEFAULT_DEF (arg
))
5769 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
5770 /* In simple cases we can look through PHI nodes,
5771 but we have to be careful with loops.
5773 if (! dom_info_available_p (CDI_DOMINATORS
)
5774 || gimple_bb (def_stmt
) == gimple_bb (stmt
)
5775 || dominated_by_p (CDI_DOMINATORS
,
5776 gimple_bb (def_stmt
),
5779 temp
= and_var_with_comparison (arg
, invert
, code2
,
5785 else if (!same_bool_result_p (result
, temp
))
5801 /* Try to simplify the AND of two comparisons, specified by
5802 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
5803 If this can be simplified to a single expression (without requiring
5804 introducing more SSA variables to hold intermediate values),
5805 return the resulting tree. Otherwise return NULL_TREE.
5806 If the result expression is non-null, it has boolean type. */
5809 maybe_fold_and_comparisons (enum tree_code code1
, tree op1a
, tree op1b
,
5810 enum tree_code code2
, tree op2a
, tree op2b
)
5812 tree t
= and_comparisons_1 (code1
, op1a
, op1b
, code2
, op2a
, op2b
);
5816 return and_comparisons_1 (code2
, op2a
, op2b
, code1
, op1a
, op1b
);
5819 /* Helper function for or_comparisons_1: try to simplify the OR of the
5820 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
5821 If INVERT is true, invert the value of VAR before doing the OR.
5822 Return NULL_EXPR if we can't simplify this to a single expression. */
5825 or_var_with_comparison (tree var
, bool invert
,
5826 enum tree_code code2
, tree op2a
, tree op2b
)
5829 gimple
*stmt
= SSA_NAME_DEF_STMT (var
);
5831 /* We can only deal with variables whose definitions are assignments. */
5832 if (!is_gimple_assign (stmt
))
5835 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
5836 !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
5837 Then we only have to consider the simpler non-inverted cases. */
5839 t
= and_var_with_comparison_1 (stmt
,
5840 invert_tree_comparison (code2
, false),
5843 t
= or_var_with_comparison_1 (stmt
, code2
, op2a
, op2b
);
5844 return canonicalize_bool (t
, invert
);
5847 /* Try to simplify the OR of the ssa variable defined by the assignment
5848 STMT with the comparison specified by (OP2A CODE2 OP2B).
5849 Return NULL_EXPR if we can't simplify this to a single expression. */
5852 or_var_with_comparison_1 (gimple
*stmt
,
5853 enum tree_code code2
, tree op2a
, tree op2b
)
5855 tree var
= gimple_assign_lhs (stmt
);
5856 tree true_test_var
= NULL_TREE
;
5857 tree false_test_var
= NULL_TREE
;
5858 enum tree_code innercode
= gimple_assign_rhs_code (stmt
);
5860 /* Check for identities like (var OR (var != 0)) => true . */
5861 if (TREE_CODE (op2a
) == SSA_NAME
5862 && TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
)
5864 if ((code2
== NE_EXPR
&& integer_zerop (op2b
))
5865 || (code2
== EQ_EXPR
&& integer_nonzerop (op2b
)))
5867 true_test_var
= op2a
;
5868 if (var
== true_test_var
)
5871 else if ((code2
== EQ_EXPR
&& integer_zerop (op2b
))
5872 || (code2
== NE_EXPR
&& integer_nonzerop (op2b
)))
5874 false_test_var
= op2a
;
5875 if (var
== false_test_var
)
5876 return boolean_true_node
;
5880 /* If the definition is a comparison, recurse on it. */
5881 if (TREE_CODE_CLASS (innercode
) == tcc_comparison
)
5883 tree t
= or_comparisons_1 (innercode
,
5884 gimple_assign_rhs1 (stmt
),
5885 gimple_assign_rhs2 (stmt
),
5893 /* If the definition is an AND or OR expression, we may be able to
5894 simplify by reassociating. */
5895 if (TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
5896 && (innercode
== BIT_AND_EXPR
|| innercode
== BIT_IOR_EXPR
))
5898 tree inner1
= gimple_assign_rhs1 (stmt
);
5899 tree inner2
= gimple_assign_rhs2 (stmt
);
5902 tree partial
= NULL_TREE
;
5903 bool is_or
= (innercode
== BIT_IOR_EXPR
);
5905 /* Check for boolean identities that don't require recursive examination
5907 inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
5908 inner1 OR (inner1 AND inner2) => inner1
5909 !inner1 OR (inner1 OR inner2) => true
5910 !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
5912 if (inner1
== true_test_var
)
5913 return (is_or
? var
: inner1
);
5914 else if (inner2
== true_test_var
)
5915 return (is_or
? var
: inner2
);
5916 else if (inner1
== false_test_var
)
5919 : or_var_with_comparison (inner2
, false, code2
, op2a
, op2b
));
5920 else if (inner2
== false_test_var
)
5923 : or_var_with_comparison (inner1
, false, code2
, op2a
, op2b
));
5925 /* Next, redistribute/reassociate the OR across the inner tests.
5926 Compute the first partial result, (inner1 OR (op2a code op2b)) */
5927 if (TREE_CODE (inner1
) == SSA_NAME
5928 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner1
))
5929 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
5930 && (t
= maybe_fold_or_comparisons (gimple_assign_rhs_code (s
),
5931 gimple_assign_rhs1 (s
),
5932 gimple_assign_rhs2 (s
),
5933 code2
, op2a
, op2b
)))
5935 /* Handle the OR case, where we are reassociating:
5936 (inner1 OR inner2) OR (op2a code2 op2b)
5938 If the partial result t is a constant, we win. Otherwise
5939 continue on to try reassociating with the other inner test. */
5942 if (integer_onep (t
))
5943 return boolean_true_node
;
5944 else if (integer_zerop (t
))
5948 /* Handle the AND case, where we are redistributing:
5949 (inner1 AND inner2) OR (op2a code2 op2b)
5950 => (t AND (inner2 OR (op2a code op2b))) */
5951 else if (integer_zerop (t
))
5952 return boolean_false_node
;
5954 /* Save partial result for later. */
5958 /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
5959 if (TREE_CODE (inner2
) == SSA_NAME
5960 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner2
))
5961 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
5962 && (t
= maybe_fold_or_comparisons (gimple_assign_rhs_code (s
),
5963 gimple_assign_rhs1 (s
),
5964 gimple_assign_rhs2 (s
),
5965 code2
, op2a
, op2b
)))
5967 /* Handle the OR case, where we are reassociating:
5968 (inner1 OR inner2) OR (op2a code2 op2b)
5970 => (t OR partial) */
5973 if (integer_zerop (t
))
5975 else if (integer_onep (t
))
5976 return boolean_true_node
;
5977 /* If both are the same, we can apply the identity
5979 else if (partial
&& same_bool_result_p (t
, partial
))
5983 /* Handle the AND case, where we are redistributing:
5984 (inner1 AND inner2) OR (op2a code2 op2b)
5985 => (t AND (inner1 OR (op2a code2 op2b)))
5986 => (t AND partial) */
5989 if (integer_zerop (t
))
5990 return boolean_false_node
;
5993 /* We already got a simplification for the other
5994 operand to the redistributed AND expression. The
5995 interesting case is when at least one is true.
5996 Or, if both are the same, we can apply the identity
5998 if (integer_onep (partial
))
6000 else if (integer_onep (t
))
6002 else if (same_bool_result_p (t
, partial
))
6011 /* Try to simplify the OR of two comparisons defined by
6012 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
6013 If this can be done without constructing an intermediate value,
6014 return the resulting tree; otherwise NULL_TREE is returned.
6015 This function is deliberately asymmetric as it recurses on SSA_DEFs
6016 in the first comparison but not the second. */
6019 or_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
6020 enum tree_code code2
, tree op2a
, tree op2b
)
6022 tree truth_type
= truth_type_for (TREE_TYPE (op1a
));
6024 /* First check for ((x CODE1 y) OR (x CODE2 y)). */
6025 if (operand_equal_p (op1a
, op2a
, 0)
6026 && operand_equal_p (op1b
, op2b
, 0))
6028 /* Result will be either NULL_TREE, or a combined comparison. */
6029 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
6030 TRUTH_ORIF_EXPR
, code1
, code2
,
6031 truth_type
, op1a
, op1b
);
6036 /* Likewise the swapped case of the above. */
6037 if (operand_equal_p (op1a
, op2b
, 0)
6038 && operand_equal_p (op1b
, op2a
, 0))
6040 /* Result will be either NULL_TREE, or a combined comparison. */
6041 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
6042 TRUTH_ORIF_EXPR
, code1
,
6043 swap_tree_comparison (code2
),
6044 truth_type
, op1a
, op1b
);
6049 /* If both comparisons are of the same value against constants, we might
6050 be able to merge them. */
6051 if (operand_equal_p (op1a
, op2a
, 0)
6052 && TREE_CODE (op1b
) == INTEGER_CST
6053 && TREE_CODE (op2b
) == INTEGER_CST
)
6055 int cmp
= tree_int_cst_compare (op1b
, op2b
);
6057 /* If we have (op1a != op1b), we should either be able to
6058 return that or TRUE, depending on whether the constant op1b
6059 also satisfies the other comparison against op2b. */
6060 if (code1
== NE_EXPR
)
6066 case EQ_EXPR
: val
= (cmp
== 0); break;
6067 case NE_EXPR
: val
= (cmp
!= 0); break;
6068 case LT_EXPR
: val
= (cmp
< 0); break;
6069 case GT_EXPR
: val
= (cmp
> 0); break;
6070 case LE_EXPR
: val
= (cmp
<= 0); break;
6071 case GE_EXPR
: val
= (cmp
>= 0); break;
6072 default: done
= false;
6077 return boolean_true_node
;
6079 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
6082 /* Likewise if the second comparison is a != comparison. */
6083 else if (code2
== NE_EXPR
)
6089 case EQ_EXPR
: val
= (cmp
== 0); break;
6090 case NE_EXPR
: val
= (cmp
!= 0); break;
6091 case LT_EXPR
: val
= (cmp
> 0); break;
6092 case GT_EXPR
: val
= (cmp
< 0); break;
6093 case LE_EXPR
: val
= (cmp
>= 0); break;
6094 case GE_EXPR
: val
= (cmp
<= 0); break;
6095 default: done
= false;
6100 return boolean_true_node
;
6102 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
6106 /* See if an equality test is redundant with the other comparison. */
6107 else if (code1
== EQ_EXPR
)
6112 case EQ_EXPR
: val
= (cmp
== 0); break;
6113 case NE_EXPR
: val
= (cmp
!= 0); break;
6114 case LT_EXPR
: val
= (cmp
< 0); break;
6115 case GT_EXPR
: val
= (cmp
> 0); break;
6116 case LE_EXPR
: val
= (cmp
<= 0); break;
6117 case GE_EXPR
: val
= (cmp
>= 0); break;
6122 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
6124 else if (code2
== EQ_EXPR
)
6129 case EQ_EXPR
: val
= (cmp
== 0); break;
6130 case NE_EXPR
: val
= (cmp
!= 0); break;
6131 case LT_EXPR
: val
= (cmp
> 0); break;
6132 case GT_EXPR
: val
= (cmp
< 0); break;
6133 case LE_EXPR
: val
= (cmp
>= 0); break;
6134 case GE_EXPR
: val
= (cmp
<= 0); break;
6139 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
6142 /* Chose the less restrictive of two < or <= comparisons. */
6143 else if ((code1
== LT_EXPR
|| code1
== LE_EXPR
)
6144 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
6146 if ((cmp
< 0) || (cmp
== 0 && code1
== LT_EXPR
))
6147 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
6149 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
6152 /* Likewise chose the less restrictive of two > or >= comparisons. */
6153 else if ((code1
== GT_EXPR
|| code1
== GE_EXPR
)
6154 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
6156 if ((cmp
> 0) || (cmp
== 0 && code1
== GT_EXPR
))
6157 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
6159 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
6162 /* Check for singleton ranges. */
6164 && ((code1
== LT_EXPR
&& code2
== GT_EXPR
)
6165 || (code1
== GT_EXPR
&& code2
== LT_EXPR
)))
6166 return fold_build2 (NE_EXPR
, boolean_type_node
, op1a
, op2b
);
6168 /* Check for less/greater pairs that don't restrict the range at all. */
6170 && (code1
== LT_EXPR
|| code1
== LE_EXPR
)
6171 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
6172 return boolean_true_node
;
6174 && (code1
== GT_EXPR
|| code1
== GE_EXPR
)
6175 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
6176 return boolean_true_node
;
6179 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
6180 NAME's definition is a truth value. See if there are any simplifications
6181 that can be done against the NAME's definition. */
6182 if (TREE_CODE (op1a
) == SSA_NAME
6183 && (code1
== NE_EXPR
|| code1
== EQ_EXPR
)
6184 && (integer_zerop (op1b
) || integer_onep (op1b
)))
6186 bool invert
= ((code1
== EQ_EXPR
&& integer_zerop (op1b
))
6187 || (code1
== NE_EXPR
&& integer_onep (op1b
)));
6188 gimple
*stmt
= SSA_NAME_DEF_STMT (op1a
);
6189 switch (gimple_code (stmt
))
6192 /* Try to simplify by copy-propagating the definition. */
6193 return or_var_with_comparison (op1a
, invert
, code2
, op2a
, op2b
);
6196 /* If every argument to the PHI produces the same result when
6197 ORed with the second comparison, we win.
6198 Do not do this unless the type is bool since we need a bool
6199 result here anyway. */
6200 if (TREE_CODE (TREE_TYPE (op1a
)) == BOOLEAN_TYPE
)
6202 tree result
= NULL_TREE
;
6204 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
6206 tree arg
= gimple_phi_arg_def (stmt
, i
);
6208 /* If this PHI has itself as an argument, ignore it.
6209 If all the other args produce the same result,
6211 if (arg
== gimple_phi_result (stmt
))
6213 else if (TREE_CODE (arg
) == INTEGER_CST
)
6215 if (invert
? integer_zerop (arg
) : integer_nonzerop (arg
))
6218 result
= boolean_true_node
;
6219 else if (!integer_onep (result
))
6223 result
= fold_build2 (code2
, boolean_type_node
,
6225 else if (!same_bool_comparison_p (result
,
6229 else if (TREE_CODE (arg
) == SSA_NAME
6230 && !SSA_NAME_IS_DEFAULT_DEF (arg
))
6233 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
6234 /* In simple cases we can look through PHI nodes,
6235 but we have to be careful with loops.
6237 if (! dom_info_available_p (CDI_DOMINATORS
)
6238 || gimple_bb (def_stmt
) == gimple_bb (stmt
)
6239 || dominated_by_p (CDI_DOMINATORS
,
6240 gimple_bb (def_stmt
),
6243 temp
= or_var_with_comparison (arg
, invert
, code2
,
6249 else if (!same_bool_result_p (result
, temp
))
6265 /* Try to simplify the OR of two comparisons, specified by
6266 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
6267 If this can be simplified to a single expression (without requiring
6268 introducing more SSA variables to hold intermediate values),
6269 return the resulting tree. Otherwise return NULL_TREE.
6270 If the result expression is non-null, it has boolean type. */
6273 maybe_fold_or_comparisons (enum tree_code code1
, tree op1a
, tree op1b
,
6274 enum tree_code code2
, tree op2a
, tree op2b
)
6276 tree t
= or_comparisons_1 (code1
, op1a
, op1b
, code2
, op2a
, op2b
);
6280 return or_comparisons_1 (code2
, op2a
, op2b
, code1
, op1a
, op1b
);
6284 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
6286 Either NULL_TREE, a simplified but non-constant or a constant
6289 ??? This should go into a gimple-fold-inline.h file to be eventually
6290 privatized with the single valueize function used in the various TUs
6291 to avoid the indirect function call overhead. */
6294 gimple_fold_stmt_to_constant_1 (gimple
*stmt
, tree (*valueize
) (tree
),
6295 tree (*gvalueize
) (tree
))
6297 gimple_match_op res_op
;
6298 /* ??? The SSA propagators do not correctly deal with following SSA use-def
6299 edges if there are intermediate VARYING defs. For this reason
6300 do not follow SSA edges here even though SCCVN can technically
6301 just deal fine with that. */
6302 if (gimple_simplify (stmt
, &res_op
, NULL
, gvalueize
, valueize
))
6304 tree res
= NULL_TREE
;
6305 if (gimple_simplified_result_is_gimple_val (&res_op
))
6306 res
= res_op
.ops
[0];
6307 else if (mprts_hook
)
6308 res
= mprts_hook (&res_op
);
6311 if (dump_file
&& dump_flags
& TDF_DETAILS
)
6313 fprintf (dump_file
, "Match-and-simplified ");
6314 print_gimple_expr (dump_file
, stmt
, 0, TDF_SLIM
);
6315 fprintf (dump_file
, " to ");
6316 print_generic_expr (dump_file
, res
);
6317 fprintf (dump_file
, "\n");
6323 location_t loc
= gimple_location (stmt
);
6324 switch (gimple_code (stmt
))
6328 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
6330 switch (get_gimple_rhs_class (subcode
))
6332 case GIMPLE_SINGLE_RHS
:
6334 tree rhs
= gimple_assign_rhs1 (stmt
);
6335 enum tree_code_class kind
= TREE_CODE_CLASS (subcode
);
6337 if (TREE_CODE (rhs
) == SSA_NAME
)
6339 /* If the RHS is an SSA_NAME, return its known constant value,
6341 return (*valueize
) (rhs
);
6343 /* Handle propagating invariant addresses into address
6345 else if (TREE_CODE (rhs
) == ADDR_EXPR
6346 && !is_gimple_min_invariant (rhs
))
6348 poly_int64 offset
= 0;
6350 base
= get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs
, 0),
6354 && (CONSTANT_CLASS_P (base
)
6355 || decl_address_invariant_p (base
)))
6356 return build_invariant_address (TREE_TYPE (rhs
),
6359 else if (TREE_CODE (rhs
) == CONSTRUCTOR
6360 && TREE_CODE (TREE_TYPE (rhs
)) == VECTOR_TYPE
6361 && known_eq (CONSTRUCTOR_NELTS (rhs
),
6362 TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs
))))
6367 nelts
= CONSTRUCTOR_NELTS (rhs
);
6368 tree_vector_builder
vec (TREE_TYPE (rhs
), nelts
, 1);
6369 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs
), i
, val
)
6371 val
= (*valueize
) (val
);
6372 if (TREE_CODE (val
) == INTEGER_CST
6373 || TREE_CODE (val
) == REAL_CST
6374 || TREE_CODE (val
) == FIXED_CST
)
6375 vec
.quick_push (val
);
6380 return vec
.build ();
6382 if (subcode
== OBJ_TYPE_REF
)
6384 tree val
= (*valueize
) (OBJ_TYPE_REF_EXPR (rhs
));
6385 /* If callee is constant, we can fold away the wrapper. */
6386 if (is_gimple_min_invariant (val
))
6390 if (kind
== tcc_reference
)
6392 if ((TREE_CODE (rhs
) == VIEW_CONVERT_EXPR
6393 || TREE_CODE (rhs
) == REALPART_EXPR
6394 || TREE_CODE (rhs
) == IMAGPART_EXPR
)
6395 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
6397 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
6398 return fold_unary_loc (EXPR_LOCATION (rhs
),
6400 TREE_TYPE (rhs
), val
);
6402 else if (TREE_CODE (rhs
) == BIT_FIELD_REF
6403 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
6405 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
6406 return fold_ternary_loc (EXPR_LOCATION (rhs
),
6408 TREE_TYPE (rhs
), val
,
6409 TREE_OPERAND (rhs
, 1),
6410 TREE_OPERAND (rhs
, 2));
6412 else if (TREE_CODE (rhs
) == MEM_REF
6413 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
6415 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
6416 if (TREE_CODE (val
) == ADDR_EXPR
6417 && is_gimple_min_invariant (val
))
6419 tree tem
= fold_build2 (MEM_REF
, TREE_TYPE (rhs
),
6421 TREE_OPERAND (rhs
, 1));
6426 return fold_const_aggregate_ref_1 (rhs
, valueize
);
6428 else if (kind
== tcc_declaration
)
6429 return get_symbol_constant_value (rhs
);
6433 case GIMPLE_UNARY_RHS
:
6436 case GIMPLE_BINARY_RHS
:
6437 /* Translate &x + CST into an invariant form suitable for
6438 further propagation. */
6439 if (subcode
== POINTER_PLUS_EXPR
)
6441 tree op0
= (*valueize
) (gimple_assign_rhs1 (stmt
));
6442 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
6443 if (TREE_CODE (op0
) == ADDR_EXPR
6444 && TREE_CODE (op1
) == INTEGER_CST
)
6446 tree off
= fold_convert (ptr_type_node
, op1
);
6447 return build_fold_addr_expr_loc
6449 fold_build2 (MEM_REF
,
6450 TREE_TYPE (TREE_TYPE (op0
)),
6451 unshare_expr (op0
), off
));
6454 /* Canonicalize bool != 0 and bool == 0 appearing after
6455 valueization. While gimple_simplify handles this
6456 it can get confused by the ~X == 1 -> X == 0 transform
6457 which we cant reduce to a SSA name or a constant
6458 (and we have no way to tell gimple_simplify to not
6459 consider those transforms in the first place). */
6460 else if (subcode
== EQ_EXPR
6461 || subcode
== NE_EXPR
)
6463 tree lhs
= gimple_assign_lhs (stmt
);
6464 tree op0
= gimple_assign_rhs1 (stmt
);
6465 if (useless_type_conversion_p (TREE_TYPE (lhs
),
6468 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
6469 op0
= (*valueize
) (op0
);
6470 if (TREE_CODE (op0
) == INTEGER_CST
)
6471 std::swap (op0
, op1
);
6472 if (TREE_CODE (op1
) == INTEGER_CST
6473 && ((subcode
== NE_EXPR
&& integer_zerop (op1
))
6474 || (subcode
== EQ_EXPR
&& integer_onep (op1
))))
6480 case GIMPLE_TERNARY_RHS
:
6482 /* Handle ternary operators that can appear in GIMPLE form. */
6483 tree op0
= (*valueize
) (gimple_assign_rhs1 (stmt
));
6484 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
6485 tree op2
= (*valueize
) (gimple_assign_rhs3 (stmt
));
6486 return fold_ternary_loc (loc
, subcode
,
6487 gimple_expr_type (stmt
), op0
, op1
, op2
);
6498 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
6500 if (gimple_call_internal_p (stmt
))
6502 enum tree_code subcode
= ERROR_MARK
;
6503 switch (gimple_call_internal_fn (stmt
))
6505 case IFN_UBSAN_CHECK_ADD
:
6506 subcode
= PLUS_EXPR
;
6508 case IFN_UBSAN_CHECK_SUB
:
6509 subcode
= MINUS_EXPR
;
6511 case IFN_UBSAN_CHECK_MUL
:
6512 subcode
= MULT_EXPR
;
6514 case IFN_BUILTIN_EXPECT
:
6516 tree arg0
= gimple_call_arg (stmt
, 0);
6517 tree op0
= (*valueize
) (arg0
);
6518 if (TREE_CODE (op0
) == INTEGER_CST
)
6525 tree arg0
= gimple_call_arg (stmt
, 0);
6526 tree arg1
= gimple_call_arg (stmt
, 1);
6527 tree op0
= (*valueize
) (arg0
);
6528 tree op1
= (*valueize
) (arg1
);
6530 if (TREE_CODE (op0
) != INTEGER_CST
6531 || TREE_CODE (op1
) != INTEGER_CST
)
6536 /* x * 0 = 0 * x = 0 without overflow. */
6537 if (integer_zerop (op0
) || integer_zerop (op1
))
6538 return build_zero_cst (TREE_TYPE (arg0
));
6541 /* y - y = 0 without overflow. */
6542 if (operand_equal_p (op0
, op1
, 0))
6543 return build_zero_cst (TREE_TYPE (arg0
));
6550 = fold_binary_loc (loc
, subcode
, TREE_TYPE (arg0
), op0
, op1
);
6552 && TREE_CODE (res
) == INTEGER_CST
6553 && !TREE_OVERFLOW (res
))
6558 fn
= (*valueize
) (gimple_call_fn (stmt
));
6559 if (TREE_CODE (fn
) == ADDR_EXPR
6560 && fndecl_built_in_p (TREE_OPERAND (fn
, 0))
6561 && gimple_builtin_call_types_compatible_p (stmt
,
6562 TREE_OPERAND (fn
, 0)))
6564 tree
*args
= XALLOCAVEC (tree
, gimple_call_num_args (stmt
));
6567 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
6568 args
[i
] = (*valueize
) (gimple_call_arg (stmt
, i
));
6569 retval
= fold_builtin_call_array (loc
,
6570 gimple_call_return_type (call_stmt
),
6571 fn
, gimple_call_num_args (stmt
), args
);
6574 /* fold_call_expr wraps the result inside a NOP_EXPR. */
6575 STRIP_NOPS (retval
);
6576 retval
= fold_convert (gimple_call_return_type (call_stmt
),
6589 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
6590 Returns NULL_TREE if folding to a constant is not possible, otherwise
6591 returns a constant according to is_gimple_min_invariant. */
6594 gimple_fold_stmt_to_constant (gimple
*stmt
, tree (*valueize
) (tree
))
6596 tree res
= gimple_fold_stmt_to_constant_1 (stmt
, valueize
);
6597 if (res
&& is_gimple_min_invariant (res
))
6603 /* The following set of functions are supposed to fold references using
6604 their constant initializers. */
6606 /* See if we can find constructor defining value of BASE.
6607 When we know the consructor with constant offset (such as
6608 base is array[40] and we do know constructor of array), then
6609 BIT_OFFSET is adjusted accordingly.
6611 As a special case, return error_mark_node when constructor
6612 is not explicitly available, but it is known to be zero
6613 such as 'static const int a;'. */
6615 get_base_constructor (tree base
, poly_int64_pod
*bit_offset
,
6616 tree (*valueize
)(tree
))
6618 poly_int64 bit_offset2
, size
, max_size
;
6621 if (TREE_CODE (base
) == MEM_REF
)
6623 poly_offset_int boff
= *bit_offset
+ mem_ref_offset (base
) * BITS_PER_UNIT
;
6624 if (!boff
.to_shwi (bit_offset
))
6628 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
6629 base
= valueize (TREE_OPERAND (base
, 0));
6630 if (!base
|| TREE_CODE (base
) != ADDR_EXPR
)
6632 base
= TREE_OPERAND (base
, 0);
6635 && TREE_CODE (base
) == SSA_NAME
)
6636 base
= valueize (base
);
6638 /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
6639 DECL_INITIAL. If BASE is a nested reference into another
6640 ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
6641 the inner reference. */
6642 switch (TREE_CODE (base
))
6647 tree init
= ctor_for_folding (base
);
6649 /* Our semantic is exact opposite of ctor_for_folding;
6650 NULL means unknown, while error_mark_node is 0. */
6651 if (init
== error_mark_node
)
6654 return error_mark_node
;
6658 case VIEW_CONVERT_EXPR
:
6659 return get_base_constructor (TREE_OPERAND (base
, 0),
6660 bit_offset
, valueize
);
6664 base
= get_ref_base_and_extent (base
, &bit_offset2
, &size
, &max_size
,
6666 if (!known_size_p (max_size
) || maybe_ne (size
, max_size
))
6668 *bit_offset
+= bit_offset2
;
6669 return get_base_constructor (base
, bit_offset
, valueize
);
6675 if (CONSTANT_CLASS_P (base
))
6682 /* CTOR is CONSTRUCTOR of an array type. Fold a reference of SIZE bits
6683 to the memory at bit OFFSET. When non-null, TYPE is the expected
6684 type of the reference; otherwise the type of the referenced element
6685 is used instead. When SIZE is zero, attempt to fold a reference to
6686 the entire element which OFFSET refers to. Increment *SUBOFF by
6687 the bit offset of the accessed element. */
6690 fold_array_ctor_reference (tree type
, tree ctor
,
6691 unsigned HOST_WIDE_INT offset
,
6692 unsigned HOST_WIDE_INT size
,
6694 unsigned HOST_WIDE_INT
*suboff
)
6696 offset_int low_bound
;
6697 offset_int elt_size
;
6698 offset_int access_index
;
6699 tree domain_type
= NULL_TREE
;
6700 HOST_WIDE_INT inner_offset
;
6702 /* Compute low bound and elt size. */
6703 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
)
6704 domain_type
= TYPE_DOMAIN (TREE_TYPE (ctor
));
6705 if (domain_type
&& TYPE_MIN_VALUE (domain_type
))
6707 /* Static constructors for variably sized objects make no sense. */
6708 if (TREE_CODE (TYPE_MIN_VALUE (domain_type
)) != INTEGER_CST
)
6710 low_bound
= wi::to_offset (TYPE_MIN_VALUE (domain_type
));
6714 /* Static constructors for variably sized objects make no sense. */
6715 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor
)))) != INTEGER_CST
)
6717 elt_size
= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor
))));
6719 /* When TYPE is non-null, verify that it specifies a constant-sized
6720 accessed not larger than size of array element. Avoid division
6721 by zero below when ELT_SIZE is zero, such as with the result of
6722 an initializer for a zero-length array or an empty struct. */
6725 && (!TYPE_SIZE_UNIT (type
)
6726 || TREE_CODE (TYPE_SIZE_UNIT (type
)) != INTEGER_CST
6727 || elt_size
< wi::to_offset (TYPE_SIZE_UNIT (type
)))))
6730 /* Compute the array index we look for. */
6731 access_index
= wi::udiv_trunc (offset_int (offset
/ BITS_PER_UNIT
),
6733 access_index
+= low_bound
;
6735 /* And offset within the access. */
6736 inner_offset
= offset
% (elt_size
.to_uhwi () * BITS_PER_UNIT
);
6738 /* See if the array field is large enough to span whole access. We do not
6739 care to fold accesses spanning multiple array indexes. */
6740 if (inner_offset
+ size
> elt_size
.to_uhwi () * BITS_PER_UNIT
)
6742 if (tree val
= get_array_ctor_element_at_index (ctor
, access_index
))
6744 if (!size
&& TREE_CODE (val
) != CONSTRUCTOR
)
6746 /* For the final reference to the entire accessed element
6747 (SIZE is zero), reset INNER_OFFSET, disegard TYPE (which
6748 may be null) in favor of the type of the element, and set
6749 SIZE to the size of the accessed element. */
6751 type
= TREE_TYPE (val
);
6752 size
= elt_size
.to_uhwi () * BITS_PER_UNIT
;
6755 *suboff
+= (access_index
* elt_size
* BITS_PER_UNIT
).to_uhwi ();
6756 return fold_ctor_reference (type
, val
, inner_offset
, size
, from_decl
,
6760 /* Memory not explicitly mentioned in constructor is 0 (or
6761 the reference is out of range). */
6762 return type
? build_zero_cst (type
) : NULL_TREE
;
6765 /* CTOR is CONSTRUCTOR of an aggregate or vector. Fold a reference
6766 of SIZE bits to the memory at bit OFFSET. When non-null, TYPE
6767 is the expected type of the reference; otherwise the type of
6768 the referenced member is used instead. When SIZE is zero,
6769 attempt to fold a reference to the entire member which OFFSET
6770 refers to; in this case. Increment *SUBOFF by the bit offset
6771 of the accessed member. */
6774 fold_nonarray_ctor_reference (tree type
, tree ctor
,
6775 unsigned HOST_WIDE_INT offset
,
6776 unsigned HOST_WIDE_INT size
,
6778 unsigned HOST_WIDE_INT
*suboff
)
6780 unsigned HOST_WIDE_INT cnt
;
6783 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
,
6786 tree byte_offset
= DECL_FIELD_OFFSET (cfield
);
6787 tree field_offset
= DECL_FIELD_BIT_OFFSET (cfield
);
6788 tree field_size
= DECL_SIZE (cfield
);
6792 /* Determine the size of the flexible array member from
6793 the size of the initializer provided for it. */
6794 field_size
= TYPE_SIZE (TREE_TYPE (cval
));
6797 /* Variable sized objects in static constructors makes no sense,
6798 but field_size can be NULL for flexible array members. */
6799 gcc_assert (TREE_CODE (field_offset
) == INTEGER_CST
6800 && TREE_CODE (byte_offset
) == INTEGER_CST
6801 && (field_size
!= NULL_TREE
6802 ? TREE_CODE (field_size
) == INTEGER_CST
6803 : TREE_CODE (TREE_TYPE (cfield
)) == ARRAY_TYPE
));
6805 /* Compute bit offset of the field. */
6806 offset_int bitoffset
6807 = (wi::to_offset (field_offset
)
6808 + (wi::to_offset (byte_offset
) << LOG2_BITS_PER_UNIT
));
6809 /* Compute bit offset where the field ends. */
6810 offset_int bitoffset_end
;
6811 if (field_size
!= NULL_TREE
)
6812 bitoffset_end
= bitoffset
+ wi::to_offset (field_size
);
6816 /* Compute the bit offset of the end of the desired access.
6817 As a special case, if the size of the desired access is
6818 zero, assume the access is to the entire field (and let
6819 the caller make any necessary adjustments by storing
6820 the actual bounds of the field in FIELDBOUNDS). */
6821 offset_int access_end
= offset_int (offset
);
6825 access_end
= bitoffset_end
;
6827 /* Is there any overlap between the desired access at
6828 [OFFSET, OFFSET+SIZE) and the offset of the field within
6829 the object at [BITOFFSET, BITOFFSET_END)? */
6830 if (wi::cmps (access_end
, bitoffset
) > 0
6831 && (field_size
== NULL_TREE
6832 || wi::lts_p (offset
, bitoffset_end
)))
6834 *suboff
+= bitoffset
.to_uhwi ();
6836 if (!size
&& TREE_CODE (cval
) != CONSTRUCTOR
)
6838 /* For the final reference to the entire accessed member
6839 (SIZE is zero), reset OFFSET, disegard TYPE (which may
6840 be null) in favor of the type of the member, and set
6841 SIZE to the size of the accessed member. */
6842 offset
= bitoffset
.to_uhwi ();
6843 type
= TREE_TYPE (cval
);
6844 size
= (bitoffset_end
- bitoffset
).to_uhwi ();
6847 /* We do have overlap. Now see if the field is large enough
6848 to cover the access. Give up for accesses that extend
6849 beyond the end of the object or that span multiple fields. */
6850 if (wi::cmps (access_end
, bitoffset_end
) > 0)
6852 if (offset
< bitoffset
)
6855 offset_int inner_offset
= offset_int (offset
) - bitoffset
;
6856 return fold_ctor_reference (type
, cval
,
6857 inner_offset
.to_uhwi (), size
,
6861 /* Memory not explicitly mentioned in constructor is 0. */
6862 return type
? build_zero_cst (type
) : NULL_TREE
;
6865 /* CTOR is value initializing memory. Fold a reference of TYPE and
6866 bit size POLY_SIZE to the memory at bit POLY_OFFSET. When SIZE
6867 is zero, attempt to fold a reference to the entire subobject
6868 which OFFSET refers to. This is used when folding accesses to
6869 string members of aggregates. When non-null, set *SUBOFF to
6870 the bit offset of the accessed subobject. */
6873 fold_ctor_reference (tree type
, tree ctor
, const poly_uint64
&poly_offset
,
6874 const poly_uint64
&poly_size
, tree from_decl
,
6875 unsigned HOST_WIDE_INT
*suboff
/* = NULL */)
6879 /* We found the field with exact match. */
6881 && useless_type_conversion_p (type
, TREE_TYPE (ctor
))
6882 && known_eq (poly_offset
, 0U))
6883 return canonicalize_constructor_val (unshare_expr (ctor
), from_decl
);
6885 /* The remaining optimizations need a constant size and offset. */
6886 unsigned HOST_WIDE_INT size
, offset
;
6887 if (!poly_size
.is_constant (&size
) || !poly_offset
.is_constant (&offset
))
6890 /* We are at the end of walk, see if we can view convert the
6892 if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor
)) && !offset
6893 /* VIEW_CONVERT_EXPR is defined only for matching sizes. */
6894 && !compare_tree_int (TYPE_SIZE (type
), size
)
6895 && !compare_tree_int (TYPE_SIZE (TREE_TYPE (ctor
)), size
))
6897 ret
= canonicalize_constructor_val (unshare_expr (ctor
), from_decl
);
6900 ret
= fold_unary (VIEW_CONVERT_EXPR
, type
, ret
);
6902 STRIP_USELESS_TYPE_CONVERSION (ret
);
6906 /* For constants and byte-aligned/sized reads try to go through
6907 native_encode/interpret. */
6908 if (CONSTANT_CLASS_P (ctor
)
6909 && BITS_PER_UNIT
== 8
6910 && offset
% BITS_PER_UNIT
== 0
6911 && size
% BITS_PER_UNIT
== 0
6912 && size
<= MAX_BITSIZE_MODE_ANY_MODE
)
6914 unsigned char buf
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
6915 int len
= native_encode_expr (ctor
, buf
, size
/ BITS_PER_UNIT
,
6916 offset
/ BITS_PER_UNIT
);
6918 return native_interpret_expr (type
, buf
, len
);
6920 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
6922 unsigned HOST_WIDE_INT dummy
= 0;
6926 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
6927 || TREE_CODE (TREE_TYPE (ctor
)) == VECTOR_TYPE
)
6928 return fold_array_ctor_reference (type
, ctor
, offset
, size
,
6931 return fold_nonarray_ctor_reference (type
, ctor
, offset
, size
,
6938 /* Return the tree representing the element referenced by T if T is an
6939 ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
6940 names using VALUEIZE. Return NULL_TREE otherwise. */
6943 fold_const_aggregate_ref_1 (tree t
, tree (*valueize
) (tree
))
6945 tree ctor
, idx
, base
;
6946 poly_int64 offset
, size
, max_size
;
6950 if (TREE_THIS_VOLATILE (t
))
6954 return get_symbol_constant_value (t
);
6956 tem
= fold_read_from_constant_string (t
);
6960 switch (TREE_CODE (t
))
6963 case ARRAY_RANGE_REF
:
6964 /* Constant indexes are handled well by get_base_constructor.
6965 Only special case variable offsets.
6966 FIXME: This code can't handle nested references with variable indexes
6967 (they will be handled only by iteration of ccp). Perhaps we can bring
6968 get_ref_base_and_extent here and make it use a valueize callback. */
6969 if (TREE_CODE (TREE_OPERAND (t
, 1)) == SSA_NAME
6971 && (idx
= (*valueize
) (TREE_OPERAND (t
, 1)))
6972 && poly_int_tree_p (idx
))
6974 tree low_bound
, unit_size
;
6976 /* If the resulting bit-offset is constant, track it. */
6977 if ((low_bound
= array_ref_low_bound (t
),
6978 poly_int_tree_p (low_bound
))
6979 && (unit_size
= array_ref_element_size (t
),
6980 tree_fits_uhwi_p (unit_size
)))
6982 poly_offset_int woffset
6983 = wi::sext (wi::to_poly_offset (idx
)
6984 - wi::to_poly_offset (low_bound
),
6985 TYPE_PRECISION (TREE_TYPE (idx
)));
6987 if (woffset
.to_shwi (&offset
))
6989 /* TODO: This code seems wrong, multiply then check
6990 to see if it fits. */
6991 offset
*= tree_to_uhwi (unit_size
);
6992 offset
*= BITS_PER_UNIT
;
6994 base
= TREE_OPERAND (t
, 0);
6995 ctor
= get_base_constructor (base
, &offset
, valueize
);
6996 /* Empty constructor. Always fold to 0. */
6997 if (ctor
== error_mark_node
)
6998 return build_zero_cst (TREE_TYPE (t
));
6999 /* Out of bound array access. Value is undefined,
7001 if (maybe_lt (offset
, 0))
7003 /* We cannot determine ctor. */
7006 return fold_ctor_reference (TREE_TYPE (t
), ctor
, offset
,
7007 tree_to_uhwi (unit_size
)
7017 case TARGET_MEM_REF
:
7019 base
= get_ref_base_and_extent (t
, &offset
, &size
, &max_size
, &reverse
);
7020 ctor
= get_base_constructor (base
, &offset
, valueize
);
7022 /* Empty constructor. Always fold to 0. */
7023 if (ctor
== error_mark_node
)
7024 return build_zero_cst (TREE_TYPE (t
));
7025 /* We do not know precise address. */
7026 if (!known_size_p (max_size
) || maybe_ne (max_size
, size
))
7028 /* We cannot determine ctor. */
7032 /* Out of bound array access. Value is undefined, but don't fold. */
7033 if (maybe_lt (offset
, 0))
7036 return fold_ctor_reference (TREE_TYPE (t
), ctor
, offset
, size
,
7042 tree c
= fold_const_aggregate_ref_1 (TREE_OPERAND (t
, 0), valueize
);
7043 if (c
&& TREE_CODE (c
) == COMPLEX_CST
)
7044 return fold_build1_loc (EXPR_LOCATION (t
),
7045 TREE_CODE (t
), TREE_TYPE (t
), c
);
7057 fold_const_aggregate_ref (tree t
)
7059 return fold_const_aggregate_ref_1 (t
, NULL
);
7062 /* Lookup virtual method with index TOKEN in a virtual table V
7064 Set CAN_REFER if non-NULL to false if method
7065 is not referable or if the virtual table is ill-formed (such as rewriten
7066 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
7069 gimple_get_virt_method_for_vtable (HOST_WIDE_INT token
,
7071 unsigned HOST_WIDE_INT offset
,
7074 tree vtable
= v
, init
, fn
;
7075 unsigned HOST_WIDE_INT size
;
7076 unsigned HOST_WIDE_INT elt_size
, access_index
;
7082 /* First of all double check we have virtual table. */
7083 if (!VAR_P (v
) || !DECL_VIRTUAL_P (v
))
7085 /* Pass down that we lost track of the target. */
7091 init
= ctor_for_folding (v
);
7093 /* The virtual tables should always be born with constructors
7094 and we always should assume that they are avaialble for
7095 folding. At the moment we do not stream them in all cases,
7096 but it should never happen that ctor seem unreachable. */
7098 if (init
== error_mark_node
)
7100 /* Pass down that we lost track of the target. */
7105 gcc_checking_assert (TREE_CODE (TREE_TYPE (v
)) == ARRAY_TYPE
);
7106 size
= tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v
))));
7107 offset
*= BITS_PER_UNIT
;
7108 offset
+= token
* size
;
7110 /* Lookup the value in the constructor that is assumed to be array.
7111 This is equivalent to
7112 fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
7113 offset, size, NULL);
7114 but in a constant time. We expect that frontend produced a simple
7115 array without indexed initializers. */
7117 gcc_checking_assert (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
);
7118 domain_type
= TYPE_DOMAIN (TREE_TYPE (init
));
7119 gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type
)));
7120 elt_size
= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init
))));
7122 access_index
= offset
/ BITS_PER_UNIT
/ elt_size
;
7123 gcc_checking_assert (offset
% (elt_size
* BITS_PER_UNIT
) == 0);
7125 /* The C++ FE can now produce indexed fields, and we check if the indexes
7127 if (access_index
< CONSTRUCTOR_NELTS (init
))
7129 fn
= CONSTRUCTOR_ELT (init
, access_index
)->value
;
7130 tree idx
= CONSTRUCTOR_ELT (init
, access_index
)->index
;
7131 gcc_checking_assert (!idx
|| tree_to_uhwi (idx
) == access_index
);
7137 /* For type inconsistent program we may end up looking up virtual method
7138 in virtual table that does not contain TOKEN entries. We may overrun
7139 the virtual table and pick up a constant or RTTI info pointer.
7140 In any case the call is undefined. */
7142 || (TREE_CODE (fn
) != ADDR_EXPR
&& TREE_CODE (fn
) != FDESC_EXPR
)
7143 || TREE_CODE (TREE_OPERAND (fn
, 0)) != FUNCTION_DECL
)
7144 fn
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
7147 fn
= TREE_OPERAND (fn
, 0);
7149 /* When cgraph node is missing and function is not public, we cannot
7150 devirtualize. This can happen in WHOPR when the actual method
7151 ends up in other partition, because we found devirtualization
7152 possibility too late. */
7153 if (!can_refer_decl_in_current_unit_p (fn
, vtable
))
7164 /* Make sure we create a cgraph node for functions we'll reference.
7165 They can be non-existent if the reference comes from an entry
7166 of an external vtable for example. */
7167 cgraph_node::get_create (fn
);
7172 /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
7173 is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
7174 KNOWN_BINFO carries the binfo describing the true type of
7175 OBJ_TYPE_REF_OBJECT(REF).
7176 Set CAN_REFER if non-NULL to false if method
7177 is not referable or if the virtual table is ill-formed (such as rewriten
7178 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
7181 gimple_get_virt_method_for_binfo (HOST_WIDE_INT token
, tree known_binfo
,
7184 unsigned HOST_WIDE_INT offset
;
7187 v
= BINFO_VTABLE (known_binfo
);
7188 /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */
7192 if (!vtable_pointer_value_to_vtable (v
, &v
, &offset
))
7198 return gimple_get_virt_method_for_vtable (token
, v
, offset
, can_refer
);
7201 /* Given a pointer value T, return a simplified version of an
7202 indirection through T, or NULL_TREE if no simplification is
7203 possible. Note that the resulting type may be different from
7204 the type pointed to in the sense that it is still compatible
7205 from the langhooks point of view. */
7208 gimple_fold_indirect_ref (tree t
)
7210 tree ptype
= TREE_TYPE (t
), type
= TREE_TYPE (ptype
);
7215 subtype
= TREE_TYPE (sub
);
7216 if (!POINTER_TYPE_P (subtype
)
7217 || TYPE_REF_CAN_ALIAS_ALL (ptype
))
7220 if (TREE_CODE (sub
) == ADDR_EXPR
)
7222 tree op
= TREE_OPERAND (sub
, 0);
7223 tree optype
= TREE_TYPE (op
);
7225 if (useless_type_conversion_p (type
, optype
))
7228 /* *(foo *)&fooarray => fooarray[0] */
7229 if (TREE_CODE (optype
) == ARRAY_TYPE
7230 && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype
))) == INTEGER_CST
7231 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
7233 tree type_domain
= TYPE_DOMAIN (optype
);
7234 tree min_val
= size_zero_node
;
7235 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
7236 min_val
= TYPE_MIN_VALUE (type_domain
);
7237 if (TREE_CODE (min_val
) == INTEGER_CST
)
7238 return build4 (ARRAY_REF
, type
, op
, min_val
, NULL_TREE
, NULL_TREE
);
7240 /* *(foo *)&complexfoo => __real__ complexfoo */
7241 else if (TREE_CODE (optype
) == COMPLEX_TYPE
7242 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
7243 return fold_build1 (REALPART_EXPR
, type
, op
);
7244 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
7245 else if (TREE_CODE (optype
) == VECTOR_TYPE
7246 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
7248 tree part_width
= TYPE_SIZE (type
);
7249 tree index
= bitsize_int (0);
7250 return fold_build3 (BIT_FIELD_REF
, type
, op
, part_width
, index
);
7254 /* *(p + CST) -> ... */
7255 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
7256 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
7258 tree addr
= TREE_OPERAND (sub
, 0);
7259 tree off
= TREE_OPERAND (sub
, 1);
7263 addrtype
= TREE_TYPE (addr
);
7265 /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
7266 if (TREE_CODE (addr
) == ADDR_EXPR
7267 && TREE_CODE (TREE_TYPE (addrtype
)) == VECTOR_TYPE
7268 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (addrtype
)))
7269 && tree_fits_uhwi_p (off
))
7271 unsigned HOST_WIDE_INT offset
= tree_to_uhwi (off
);
7272 tree part_width
= TYPE_SIZE (type
);
7273 unsigned HOST_WIDE_INT part_widthi
7274 = tree_to_shwi (part_width
) / BITS_PER_UNIT
;
7275 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
7276 tree index
= bitsize_int (indexi
);
7277 if (known_lt (offset
/ part_widthi
,
7278 TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype
))))
7279 return fold_build3 (BIT_FIELD_REF
, type
, TREE_OPERAND (addr
, 0),
7283 /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
7284 if (TREE_CODE (addr
) == ADDR_EXPR
7285 && TREE_CODE (TREE_TYPE (addrtype
)) == COMPLEX_TYPE
7286 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (addrtype
))))
7288 tree size
= TYPE_SIZE_UNIT (type
);
7289 if (tree_int_cst_equal (size
, off
))
7290 return fold_build1 (IMAGPART_EXPR
, type
, TREE_OPERAND (addr
, 0));
7293 /* *(p + CST) -> MEM_REF <p, CST>. */
7294 if (TREE_CODE (addr
) != ADDR_EXPR
7295 || DECL_P (TREE_OPERAND (addr
, 0)))
7296 return fold_build2 (MEM_REF
, type
,
7298 wide_int_to_tree (ptype
, wi::to_wide (off
)));
7301 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
7302 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
7303 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype
)))) == INTEGER_CST
7304 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (subtype
))))
7307 tree min_val
= size_zero_node
;
7309 sub
= gimple_fold_indirect_ref (sub
);
7311 sub
= build1 (INDIRECT_REF
, TREE_TYPE (subtype
), osub
);
7312 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
7313 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
7314 min_val
= TYPE_MIN_VALUE (type_domain
);
7315 if (TREE_CODE (min_val
) == INTEGER_CST
)
7316 return build4 (ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
, NULL_TREE
);
7322 /* Return true if CODE is an operation that when operating on signed
7323 integer types involves undefined behavior on overflow and the
7324 operation can be expressed with unsigned arithmetic. */
7327 arith_code_with_undefined_signed_overflow (tree_code code
)
7335 case POINTER_PLUS_EXPR
:
7342 /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic
7343 operation that can be transformed to unsigned arithmetic by converting
7344 its operand, carrying out the operation in the corresponding unsigned
7345 type and converting the result back to the original type.
7347 Returns a sequence of statements that replace STMT and also contain
7348 a modified form of STMT itself. */
7351 rewrite_to_defined_overflow (gimple
*stmt
)
7353 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7355 fprintf (dump_file
, "rewriting stmt with undefined signed "
7357 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
7360 tree lhs
= gimple_assign_lhs (stmt
);
7361 tree type
= unsigned_type_for (TREE_TYPE (lhs
));
7362 gimple_seq stmts
= NULL
;
7363 for (unsigned i
= 1; i
< gimple_num_ops (stmt
); ++i
)
7365 tree op
= gimple_op (stmt
, i
);
7366 op
= gimple_convert (&stmts
, type
, op
);
7367 gimple_set_op (stmt
, i
, op
);
7369 gimple_assign_set_lhs (stmt
, make_ssa_name (type
, stmt
));
7370 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
7371 gimple_assign_set_rhs_code (stmt
, PLUS_EXPR
);
7372 gimple_seq_add_stmt (&stmts
, stmt
);
7373 gimple
*cvt
= gimple_build_assign (lhs
, NOP_EXPR
, gimple_assign_lhs (stmt
));
7374 gimple_seq_add_stmt (&stmts
, cvt
);
7380 /* The valueization hook we use for the gimple_build API simplification.
7381 This makes us match fold_buildN behavior by only combining with
7382 statements in the sequence(s) we are currently building. */
7385 gimple_build_valueize (tree op
)
7387 if (gimple_bb (SSA_NAME_DEF_STMT (op
)) == NULL
)
7392 /* Build the expression CODE OP0 of type TYPE with location LOC,
7393 simplifying it first if possible. Returns the built
7394 expression value and appends statements possibly defining it
7398 gimple_build (gimple_seq
*seq
, location_t loc
,
7399 enum tree_code code
, tree type
, tree op0
)
7401 tree res
= gimple_simplify (code
, type
, op0
, seq
, gimple_build_valueize
);
7404 res
= create_tmp_reg_or_ssa_name (type
);
7406 if (code
== REALPART_EXPR
7407 || code
== IMAGPART_EXPR
7408 || code
== VIEW_CONVERT_EXPR
)
7409 stmt
= gimple_build_assign (res
, code
, build1 (code
, type
, op0
));
7411 stmt
= gimple_build_assign (res
, code
, op0
);
7412 gimple_set_location (stmt
, loc
);
7413 gimple_seq_add_stmt_without_update (seq
, stmt
);
7418 /* Build the expression OP0 CODE OP1 of type TYPE with location LOC,
7419 simplifying it first if possible. Returns the built
7420 expression value and appends statements possibly defining it
7424 gimple_build (gimple_seq
*seq
, location_t loc
,
7425 enum tree_code code
, tree type
, tree op0
, tree op1
)
7427 tree res
= gimple_simplify (code
, type
, op0
, op1
, seq
, gimple_build_valueize
);
7430 res
= create_tmp_reg_or_ssa_name (type
);
7431 gimple
*stmt
= gimple_build_assign (res
, code
, op0
, op1
);
7432 gimple_set_location (stmt
, loc
);
7433 gimple_seq_add_stmt_without_update (seq
, stmt
);
7438 /* Build the expression (CODE OP0 OP1 OP2) of type TYPE with location LOC,
7439 simplifying it first if possible. Returns the built
7440 expression value and appends statements possibly defining it
7444 gimple_build (gimple_seq
*seq
, location_t loc
,
7445 enum tree_code code
, tree type
, tree op0
, tree op1
, tree op2
)
7447 tree res
= gimple_simplify (code
, type
, op0
, op1
, op2
,
7448 seq
, gimple_build_valueize
);
7451 res
= create_tmp_reg_or_ssa_name (type
);
7453 if (code
== BIT_FIELD_REF
)
7454 stmt
= gimple_build_assign (res
, code
,
7455 build3 (code
, type
, op0
, op1
, op2
));
7457 stmt
= gimple_build_assign (res
, code
, op0
, op1
, op2
);
7458 gimple_set_location (stmt
, loc
);
7459 gimple_seq_add_stmt_without_update (seq
, stmt
);
7464 /* Build the call FN (ARG0) with a result of type TYPE
7465 (or no result if TYPE is void) with location LOC,
7466 simplifying it first if possible. Returns the built
7467 expression value (or NULL_TREE if TYPE is void) and appends
7468 statements possibly defining it to SEQ. */
7471 gimple_build (gimple_seq
*seq
, location_t loc
, combined_fn fn
,
7472 tree type
, tree arg0
)
7474 tree res
= gimple_simplify (fn
, type
, arg0
, seq
, gimple_build_valueize
);
7478 if (internal_fn_p (fn
))
7479 stmt
= gimple_build_call_internal (as_internal_fn (fn
), 1, arg0
);
7482 tree decl
= builtin_decl_implicit (as_builtin_fn (fn
));
7483 stmt
= gimple_build_call (decl
, 1, arg0
);
7485 if (!VOID_TYPE_P (type
))
7487 res
= create_tmp_reg_or_ssa_name (type
);
7488 gimple_call_set_lhs (stmt
, res
);
7490 gimple_set_location (stmt
, loc
);
7491 gimple_seq_add_stmt_without_update (seq
, stmt
);
7496 /* Build the call FN (ARG0, ARG1) with a result of type TYPE
7497 (or no result if TYPE is void) with location LOC,
7498 simplifying it first if possible. Returns the built
7499 expression value (or NULL_TREE if TYPE is void) and appends
7500 statements possibly defining it to SEQ. */
7503 gimple_build (gimple_seq
*seq
, location_t loc
, combined_fn fn
,
7504 tree type
, tree arg0
, tree arg1
)
7506 tree res
= gimple_simplify (fn
, type
, arg0
, arg1
, seq
, gimple_build_valueize
);
7510 if (internal_fn_p (fn
))
7511 stmt
= gimple_build_call_internal (as_internal_fn (fn
), 2, arg0
, arg1
);
7514 tree decl
= builtin_decl_implicit (as_builtin_fn (fn
));
7515 stmt
= gimple_build_call (decl
, 2, arg0
, arg1
);
7517 if (!VOID_TYPE_P (type
))
7519 res
= create_tmp_reg_or_ssa_name (type
);
7520 gimple_call_set_lhs (stmt
, res
);
7522 gimple_set_location (stmt
, loc
);
7523 gimple_seq_add_stmt_without_update (seq
, stmt
);
7528 /* Build the call FN (ARG0, ARG1, ARG2) with a result of type TYPE
7529 (or no result if TYPE is void) with location LOC,
7530 simplifying it first if possible. Returns the built
7531 expression value (or NULL_TREE if TYPE is void) and appends
7532 statements possibly defining it to SEQ. */
7535 gimple_build (gimple_seq
*seq
, location_t loc
, combined_fn fn
,
7536 tree type
, tree arg0
, tree arg1
, tree arg2
)
7538 tree res
= gimple_simplify (fn
, type
, arg0
, arg1
, arg2
,
7539 seq
, gimple_build_valueize
);
7543 if (internal_fn_p (fn
))
7544 stmt
= gimple_build_call_internal (as_internal_fn (fn
),
7545 3, arg0
, arg1
, arg2
);
7548 tree decl
= builtin_decl_implicit (as_builtin_fn (fn
));
7549 stmt
= gimple_build_call (decl
, 3, arg0
, arg1
, arg2
);
7551 if (!VOID_TYPE_P (type
))
7553 res
= create_tmp_reg_or_ssa_name (type
);
7554 gimple_call_set_lhs (stmt
, res
);
7556 gimple_set_location (stmt
, loc
);
7557 gimple_seq_add_stmt_without_update (seq
, stmt
);
7562 /* Build the conversion (TYPE) OP with a result of type TYPE
7563 with location LOC if such conversion is neccesary in GIMPLE,
7564 simplifying it first.
7565 Returns the built expression value and appends
7566 statements possibly defining it to SEQ. */
7569 gimple_convert (gimple_seq
*seq
, location_t loc
, tree type
, tree op
)
7571 if (useless_type_conversion_p (type
, TREE_TYPE (op
)))
7573 return gimple_build (seq
, loc
, NOP_EXPR
, type
, op
);
7576 /* Build the conversion (ptrofftype) OP with a result of a type
7577 compatible with ptrofftype with location LOC if such conversion
7578 is neccesary in GIMPLE, simplifying it first.
7579 Returns the built expression value and appends
7580 statements possibly defining it to SEQ. */
7583 gimple_convert_to_ptrofftype (gimple_seq
*seq
, location_t loc
, tree op
)
7585 if (ptrofftype_p (TREE_TYPE (op
)))
7587 return gimple_convert (seq
, loc
, sizetype
, op
);
7590 /* Build a vector of type TYPE in which each element has the value OP.
7591 Return a gimple value for the result, appending any new statements
7595 gimple_build_vector_from_val (gimple_seq
*seq
, location_t loc
, tree type
,
7598 if (!TYPE_VECTOR_SUBPARTS (type
).is_constant ()
7599 && !CONSTANT_CLASS_P (op
))
7600 return gimple_build (seq
, loc
, VEC_DUPLICATE_EXPR
, type
, op
);
7602 tree res
, vec
= build_vector_from_val (type
, op
);
7603 if (is_gimple_val (vec
))
7605 if (gimple_in_ssa_p (cfun
))
7606 res
= make_ssa_name (type
);
7608 res
= create_tmp_reg (type
);
7609 gimple
*stmt
= gimple_build_assign (res
, vec
);
7610 gimple_set_location (stmt
, loc
);
7611 gimple_seq_add_stmt_without_update (seq
, stmt
);
7615 /* Build a vector from BUILDER, handling the case in which some elements
7616 are non-constant. Return a gimple value for the result, appending any
7617 new instructions to SEQ.
7619 BUILDER must not have a stepped encoding on entry. This is because
7620 the function is not geared up to handle the arithmetic that would
7621 be needed in the variable case, and any code building a vector that
7622 is known to be constant should use BUILDER->build () directly. */
7625 gimple_build_vector (gimple_seq
*seq
, location_t loc
,
7626 tree_vector_builder
*builder
)
7628 gcc_assert (builder
->nelts_per_pattern () <= 2);
7629 unsigned int encoded_nelts
= builder
->encoded_nelts ();
7630 for (unsigned int i
= 0; i
< encoded_nelts
; ++i
)
7631 if (!TREE_CONSTANT ((*builder
)[i
]))
7633 tree type
= builder
->type ();
7634 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
).to_constant ();
7635 vec
<constructor_elt
, va_gc
> *v
;
7636 vec_alloc (v
, nelts
);
7637 for (i
= 0; i
< nelts
; ++i
)
7638 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, builder
->elt (i
));
7641 if (gimple_in_ssa_p (cfun
))
7642 res
= make_ssa_name (type
);
7644 res
= create_tmp_reg (type
);
7645 gimple
*stmt
= gimple_build_assign (res
, build_constructor (type
, v
));
7646 gimple_set_location (stmt
, loc
);
7647 gimple_seq_add_stmt_without_update (seq
, stmt
);
7650 return builder
->build ();
7653 /* Return true if the result of assignment STMT is known to be non-negative.
7654 If the return value is based on the assumption that signed overflow is
7655 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7656 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7659 gimple_assign_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
7662 enum tree_code code
= gimple_assign_rhs_code (stmt
);
7663 switch (get_gimple_rhs_class (code
))
7665 case GIMPLE_UNARY_RHS
:
7666 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
7667 gimple_expr_type (stmt
),
7668 gimple_assign_rhs1 (stmt
),
7669 strict_overflow_p
, depth
);
7670 case GIMPLE_BINARY_RHS
:
7671 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
7672 gimple_expr_type (stmt
),
7673 gimple_assign_rhs1 (stmt
),
7674 gimple_assign_rhs2 (stmt
),
7675 strict_overflow_p
, depth
);
7676 case GIMPLE_TERNARY_RHS
:
7678 case GIMPLE_SINGLE_RHS
:
7679 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
7680 strict_overflow_p
, depth
);
7681 case GIMPLE_INVALID_RHS
:
7687 /* Return true if return value of call STMT is known to be non-negative.
7688 If the return value is based on the assumption that signed overflow is
7689 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7690 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7693 gimple_call_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
7696 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
7697 gimple_call_arg (stmt
, 0) : NULL_TREE
;
7698 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
7699 gimple_call_arg (stmt
, 1) : NULL_TREE
;
7701 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
7702 gimple_call_combined_fn (stmt
),
7705 strict_overflow_p
, depth
);
7708 /* Return true if return value of call STMT is known to be non-negative.
7709 If the return value is based on the assumption that signed overflow is
7710 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7711 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7714 gimple_phi_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
7717 for (unsigned i
= 0; i
< gimple_phi_num_args (stmt
); ++i
)
7719 tree arg
= gimple_phi_arg_def (stmt
, i
);
7720 if (!tree_single_nonnegative_warnv_p (arg
, strict_overflow_p
, depth
+ 1))
7726 /* Return true if STMT is known to compute a non-negative value.
7727 If the return value is based on the assumption that signed overflow is
7728 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7729 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7732 gimple_stmt_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
7735 switch (gimple_code (stmt
))
7738 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
,
7741 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
,
7744 return gimple_phi_nonnegative_warnv_p (stmt
, strict_overflow_p
,
7751 /* Return true if the floating-point value computed by assignment STMT
7752 is known to have an integer value. We also allow +Inf, -Inf and NaN
7753 to be considered integer values. Return false for signaling NaN.
7755 DEPTH is the current nesting depth of the query. */
7758 gimple_assign_integer_valued_real_p (gimple
*stmt
, int depth
)
7760 enum tree_code code
= gimple_assign_rhs_code (stmt
);
7761 switch (get_gimple_rhs_class (code
))
7763 case GIMPLE_UNARY_RHS
:
7764 return integer_valued_real_unary_p (gimple_assign_rhs_code (stmt
),
7765 gimple_assign_rhs1 (stmt
), depth
);
7766 case GIMPLE_BINARY_RHS
:
7767 return integer_valued_real_binary_p (gimple_assign_rhs_code (stmt
),
7768 gimple_assign_rhs1 (stmt
),
7769 gimple_assign_rhs2 (stmt
), depth
);
7770 case GIMPLE_TERNARY_RHS
:
7772 case GIMPLE_SINGLE_RHS
:
7773 return integer_valued_real_single_p (gimple_assign_rhs1 (stmt
), depth
);
7774 case GIMPLE_INVALID_RHS
:
7780 /* Return true if the floating-point value computed by call STMT is known
7781 to have an integer value. We also allow +Inf, -Inf and NaN to be
7782 considered integer values. Return false for signaling NaN.
7784 DEPTH is the current nesting depth of the query. */
7787 gimple_call_integer_valued_real_p (gimple
*stmt
, int depth
)
7789 tree arg0
= (gimple_call_num_args (stmt
) > 0
7790 ? gimple_call_arg (stmt
, 0)
7792 tree arg1
= (gimple_call_num_args (stmt
) > 1
7793 ? gimple_call_arg (stmt
, 1)
7795 return integer_valued_real_call_p (gimple_call_combined_fn (stmt
),
7799 /* Return true if the floating-point result of phi STMT is known to have
7800 an integer value. We also allow +Inf, -Inf and NaN to be considered
7801 integer values. Return false for signaling NaN.
7803 DEPTH is the current nesting depth of the query. */
7806 gimple_phi_integer_valued_real_p (gimple
*stmt
, int depth
)
7808 for (unsigned i
= 0; i
< gimple_phi_num_args (stmt
); ++i
)
7810 tree arg
= gimple_phi_arg_def (stmt
, i
);
7811 if (!integer_valued_real_single_p (arg
, depth
+ 1))
7817 /* Return true if the floating-point value computed by STMT is known
7818 to have an integer value. We also allow +Inf, -Inf and NaN to be
7819 considered integer values. Return false for signaling NaN.
7821 DEPTH is the current nesting depth of the query. */
7824 gimple_stmt_integer_valued_real_p (gimple
*stmt
, int depth
)
7826 switch (gimple_code (stmt
))
7829 return gimple_assign_integer_valued_real_p (stmt
, depth
);
7831 return gimple_call_integer_valued_real_p (stmt
, depth
);
7833 return gimple_phi_integer_valued_real_p (stmt
, depth
);