1 /* Statement simplification on GIMPLE.
2 Copyright (C) 2010-2022 Free Software Foundation, Inc.
3 Split out from tree-ssa-ccp.cc.
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-access.h"
34 #include "gimple-ssa-warn-restrict.h"
35 #include "fold-const.h"
38 #include "stor-layout.h"
40 #include "gimple-fold.h"
42 #include "gimple-iterator.h"
43 #include "tree-into-ssa.h"
45 #include "tree-object-size.h"
47 #include "tree-ssa-propagate.h"
48 #include "ipa-utils.h"
49 #include "tree-ssa-address.h"
50 #include "langhooks.h"
51 #include "gimplify-me.h"
55 #include "gimple-match.h"
56 #include "gomp-constants.h"
57 #include "optabs-query.h"
58 #include "omp-general.h"
60 #include "fold-const-call.h"
61 #include "stringpool.h"
64 #include "diagnostic-core.h"
67 #include "tree-vector-builder.h"
68 #include "tree-ssa-strlen.h"
72 #include "internal-fn.h"
74 enum strlen_range_kind
{
75 /* Compute the exact constant string length. */
77 /* Compute the maximum constant string length. */
79 /* Compute a range of string lengths bounded by object sizes. When
80 the length of a string cannot be determined, consider as the upper
81 bound the size of the enclosing object the string may be a member
82 or element of. Also determine the size of the largest character
83 array the string may refer to. */
85 /* Determine the integer value of the argument (not string length). */
90 get_range_strlen (tree
, bitmap
, strlen_range_kind
, c_strlen_data
*, unsigned);
92 /* Return true when DECL can be referenced from current unit.
93 FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
94 We can get declarations that are not possible to reference for various
97 1) When analyzing C++ virtual tables.
98 C++ virtual tables do have known constructors even
99 when they are keyed to other compilation unit.
100 Those tables can contain pointers to methods and vars
101 in other units. Those methods have both STATIC and EXTERNAL
103 2) In WHOPR mode devirtualization might lead to reference
104 to method that was partitioned elsehwere.
105 In this case we have static VAR_DECL or FUNCTION_DECL
106 that has no corresponding callgraph/varpool node
108 3) COMDAT functions referred by external vtables that
109 we devirtualize only during final compilation stage.
110 At this time we already decided that we will not output
111 the function body and thus we can't reference the symbol
115 can_refer_decl_in_current_unit_p (tree decl
, tree from_decl
)
118 struct cgraph_node
*node
;
121 if (DECL_ABSTRACT_P (decl
))
124 /* We are concerned only about static/external vars and functions. */
125 if ((!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
126 || !VAR_OR_FUNCTION_DECL_P (decl
))
129 /* Static objects can be referred only if they are defined and not optimized
131 if (!TREE_PUBLIC (decl
))
133 if (DECL_EXTERNAL (decl
))
135 /* Before we start optimizing unreachable code we can be sure all
136 static objects are defined. */
137 if (symtab
->function_flags_ready
)
139 snode
= symtab_node::get (decl
);
140 if (!snode
|| !snode
->definition
)
142 node
= dyn_cast
<cgraph_node
*> (snode
);
143 return !node
|| !node
->inlined_to
;
146 /* We will later output the initializer, so we can refer to it.
147 So we are concerned only when DECL comes from initializer of
148 external var or var that has been optimized out. */
150 || !VAR_P (from_decl
)
151 || (!DECL_EXTERNAL (from_decl
)
152 && (vnode
= varpool_node::get (from_decl
)) != NULL
153 && vnode
->definition
)
155 && (vnode
= varpool_node::get (from_decl
)) != NULL
156 && vnode
->in_other_partition
))
158 /* We are folding reference from external vtable. The vtable may reffer
159 to a symbol keyed to other compilation unit. The other compilation
160 unit may be in separate DSO and the symbol may be hidden. */
161 if (DECL_VISIBILITY_SPECIFIED (decl
)
162 && DECL_EXTERNAL (decl
)
163 && DECL_VISIBILITY (decl
) != VISIBILITY_DEFAULT
164 && (!(snode
= symtab_node::get (decl
)) || !snode
->in_other_partition
))
166 /* When function is public, we always can introduce new reference.
167 Exception are the COMDAT functions where introducing a direct
168 reference imply need to include function body in the curren tunit. */
169 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
))
171 /* We have COMDAT. We are going to check if we still have definition
172 or if the definition is going to be output in other partition.
173 Bypass this when gimplifying; all needed functions will be produced.
175 As observed in PR20991 for already optimized out comdat virtual functions
176 it may be tempting to not necessarily give up because the copy will be
177 output elsewhere when corresponding vtable is output.
178 This is however not possible - ABI specify that COMDATs are output in
179 units where they are used and when the other unit was compiled with LTO
180 it is possible that vtable was kept public while the function itself
182 if (!symtab
->function_flags_ready
)
185 snode
= symtab_node::get (decl
);
187 || ((!snode
->definition
|| DECL_EXTERNAL (decl
))
188 && (!snode
->in_other_partition
189 || (!snode
->forced_by_abi
&& !snode
->force_output
))))
191 node
= dyn_cast
<cgraph_node
*> (snode
);
192 return !node
|| !node
->inlined_to
;
195 /* Create a temporary for TYPE for a statement STMT. If the current function
196 is in SSA form, a SSA name is created. Otherwise a temporary register
200 create_tmp_reg_or_ssa_name (tree type
, gimple
*stmt
)
202 if (gimple_in_ssa_p (cfun
))
203 return make_ssa_name (type
, stmt
);
205 return create_tmp_reg (type
);
208 /* CVAL is value taken from DECL_INITIAL of variable. Try to transform it into
209 acceptable form for is_gimple_min_invariant.
210 FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL. */
213 canonicalize_constructor_val (tree cval
, tree from_decl
)
215 if (CONSTANT_CLASS_P (cval
))
218 tree orig_cval
= cval
;
220 if (TREE_CODE (cval
) == POINTER_PLUS_EXPR
221 && TREE_CODE (TREE_OPERAND (cval
, 1)) == INTEGER_CST
)
223 tree ptr
= TREE_OPERAND (cval
, 0);
224 if (is_gimple_min_invariant (ptr
))
225 cval
= build1_loc (EXPR_LOCATION (cval
),
226 ADDR_EXPR
, TREE_TYPE (ptr
),
227 fold_build2 (MEM_REF
, TREE_TYPE (TREE_TYPE (ptr
)),
229 fold_convert (ptr_type_node
,
230 TREE_OPERAND (cval
, 1))));
232 if (TREE_CODE (cval
) == ADDR_EXPR
)
234 tree base
= NULL_TREE
;
235 if (TREE_CODE (TREE_OPERAND (cval
, 0)) == COMPOUND_LITERAL_EXPR
)
237 base
= COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval
, 0));
239 TREE_OPERAND (cval
, 0) = base
;
242 base
= get_base_address (TREE_OPERAND (cval
, 0));
246 if (VAR_OR_FUNCTION_DECL_P (base
)
247 && !can_refer_decl_in_current_unit_p (base
, from_decl
))
249 if (TREE_TYPE (base
) == error_mark_node
)
252 /* ??? We should be able to assert that TREE_ADDRESSABLE is set,
253 but since the use can be in a debug stmt we can't. */
255 else if (TREE_CODE (base
) == FUNCTION_DECL
)
257 /* Make sure we create a cgraph node for functions we'll reference.
258 They can be non-existent if the reference comes from an entry
259 of an external vtable for example. */
260 cgraph_node::get_create (base
);
262 /* Fixup types in global initializers. */
263 if (TREE_TYPE (TREE_TYPE (cval
)) != TREE_TYPE (TREE_OPERAND (cval
, 0)))
264 cval
= build_fold_addr_expr (TREE_OPERAND (cval
, 0));
266 if (!useless_type_conversion_p (TREE_TYPE (orig_cval
), TREE_TYPE (cval
)))
267 cval
= fold_convert (TREE_TYPE (orig_cval
), cval
);
270 /* In CONSTRUCTORs we may see unfolded constants like (int (*) ()) 0. */
271 if (TREE_CODE (cval
) == INTEGER_CST
)
273 if (TREE_OVERFLOW_P (cval
))
274 cval
= drop_tree_overflow (cval
);
275 if (!useless_type_conversion_p (TREE_TYPE (orig_cval
), TREE_TYPE (cval
)))
276 cval
= fold_convert (TREE_TYPE (orig_cval
), cval
);
282 /* If SYM is a constant variable with known value, return the value.
283 NULL_TREE is returned otherwise. */
286 get_symbol_constant_value (tree sym
)
288 tree val
= ctor_for_folding (sym
);
289 if (val
!= error_mark_node
)
293 val
= canonicalize_constructor_val (unshare_expr (val
), sym
);
295 && is_gimple_min_invariant (val
)
296 && useless_type_conversion_p (TREE_TYPE (sym
), TREE_TYPE (val
)))
301 /* Variables declared 'const' without an initializer
302 have zero as the initializer if they may not be
303 overridden at link or run time. */
305 && is_gimple_reg_type (TREE_TYPE (sym
)))
306 return build_zero_cst (TREE_TYPE (sym
));
314 /* Subroutine of fold_stmt. We perform constant folding of the
315 memory reference tree EXPR. */
318 maybe_fold_reference (tree expr
)
320 tree result
= NULL_TREE
;
322 if ((TREE_CODE (expr
) == VIEW_CONVERT_EXPR
323 || TREE_CODE (expr
) == REALPART_EXPR
324 || TREE_CODE (expr
) == IMAGPART_EXPR
)
325 && CONSTANT_CLASS_P (TREE_OPERAND (expr
, 0)))
326 result
= fold_unary_loc (EXPR_LOCATION (expr
),
329 TREE_OPERAND (expr
, 0));
330 else if (TREE_CODE (expr
) == BIT_FIELD_REF
331 && CONSTANT_CLASS_P (TREE_OPERAND (expr
, 0)))
332 result
= fold_ternary_loc (EXPR_LOCATION (expr
),
335 TREE_OPERAND (expr
, 0),
336 TREE_OPERAND (expr
, 1),
337 TREE_OPERAND (expr
, 2));
339 result
= fold_const_aggregate_ref (expr
);
341 if (result
&& is_gimple_min_invariant (result
))
347 /* Return true if EXPR is an acceptable right-hand-side for a
348 GIMPLE assignment. We validate the entire tree, not just
349 the root node, thus catching expressions that embed complex
350 operands that are not permitted in GIMPLE. This function
351 is needed because the folding routines in fold-const.cc
352 may return such expressions in some cases, e.g., an array
353 access with an embedded index addition. It may make more
354 sense to have folding routines that are sensitive to the
355 constraints on GIMPLE operands, rather than abandoning any
356 any attempt to fold if the usual folding turns out to be too
360 valid_gimple_rhs_p (tree expr
)
362 enum tree_code code
= TREE_CODE (expr
);
364 switch (TREE_CODE_CLASS (code
))
366 case tcc_declaration
:
367 if (!is_gimple_variable (expr
))
372 /* All constants are ok. */
376 /* GENERIC allows comparisons with non-boolean types, reject
377 those for GIMPLE. Let vector-typed comparisons pass - rules
378 for GENERIC and GIMPLE are the same here. */
379 if (!(INTEGRAL_TYPE_P (TREE_TYPE (expr
))
380 && (TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
381 || TYPE_PRECISION (TREE_TYPE (expr
)) == 1))
382 && ! VECTOR_TYPE_P (TREE_TYPE (expr
)))
387 if (!is_gimple_val (TREE_OPERAND (expr
, 0))
388 || !is_gimple_val (TREE_OPERAND (expr
, 1)))
393 if (!is_gimple_val (TREE_OPERAND (expr
, 0)))
403 if (is_gimple_min_invariant (expr
))
405 t
= TREE_OPERAND (expr
, 0);
406 while (handled_component_p (t
))
408 /* ??? More checks needed, see the GIMPLE verifier. */
409 if ((TREE_CODE (t
) == ARRAY_REF
410 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
411 && !is_gimple_val (TREE_OPERAND (t
, 1)))
413 t
= TREE_OPERAND (t
, 0);
415 if (!is_gimple_id (t
))
421 if (get_gimple_rhs_class (code
) == GIMPLE_TERNARY_RHS
)
423 if ((code
== COND_EXPR
424 ? !is_gimple_condexpr (TREE_OPERAND (expr
, 0))
425 : !is_gimple_val (TREE_OPERAND (expr
, 0)))
426 || !is_gimple_val (TREE_OPERAND (expr
, 1))
427 || !is_gimple_val (TREE_OPERAND (expr
, 2)))
438 case tcc_exceptional
:
439 if (code
== CONSTRUCTOR
)
443 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), i
, elt
)
444 if (!is_gimple_val (elt
))
448 if (code
!= SSA_NAME
)
453 if (code
== BIT_FIELD_REF
)
454 return is_gimple_val (TREE_OPERAND (expr
, 0));
465 /* Attempt to fold an assignment statement pointed-to by SI. Returns a
466 replacement rhs for the statement or NULL_TREE if no simplification
467 could be made. It is assumed that the operands have been previously
471 fold_gimple_assign (gimple_stmt_iterator
*si
)
473 gimple
*stmt
= gsi_stmt (*si
);
474 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
475 location_t loc
= gimple_location (stmt
);
477 tree result
= NULL_TREE
;
479 switch (get_gimple_rhs_class (subcode
))
481 case GIMPLE_SINGLE_RHS
:
483 tree rhs
= gimple_assign_rhs1 (stmt
);
485 if (TREE_CLOBBER_P (rhs
))
488 if (REFERENCE_CLASS_P (rhs
))
489 return maybe_fold_reference (rhs
);
491 else if (TREE_CODE (rhs
) == OBJ_TYPE_REF
)
493 tree val
= OBJ_TYPE_REF_EXPR (rhs
);
494 if (is_gimple_min_invariant (val
))
496 else if (flag_devirtualize
&& virtual_method_call_p (rhs
))
499 vec
<cgraph_node
*>targets
500 = possible_polymorphic_call_targets (rhs
, stmt
, &final
);
501 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
503 if (dump_enabled_p ())
505 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, stmt
,
506 "resolving virtual function address "
507 "reference to function %s\n",
508 targets
.length () == 1
509 ? targets
[0]->name ()
512 if (targets
.length () == 1)
514 val
= fold_convert (TREE_TYPE (val
),
515 build_fold_addr_expr_loc
516 (loc
, targets
[0]->decl
));
517 STRIP_USELESS_TYPE_CONVERSION (val
);
520 /* We cannot use __builtin_unreachable here because it
521 cannot have address taken. */
522 val
= build_int_cst (TREE_TYPE (val
), 0);
528 else if (TREE_CODE (rhs
) == ADDR_EXPR
)
530 tree ref
= TREE_OPERAND (rhs
, 0);
531 if (TREE_CODE (ref
) == MEM_REF
532 && integer_zerop (TREE_OPERAND (ref
, 1)))
534 result
= TREE_OPERAND (ref
, 0);
535 if (!useless_type_conversion_p (TREE_TYPE (rhs
),
537 result
= build1 (NOP_EXPR
, TREE_TYPE (rhs
), result
);
542 else if (TREE_CODE (rhs
) == CONSTRUCTOR
543 && TREE_CODE (TREE_TYPE (rhs
)) == VECTOR_TYPE
)
545 /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */
549 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs
), i
, val
)
550 if (! CONSTANT_CLASS_P (val
))
553 return build_vector_from_ctor (TREE_TYPE (rhs
),
554 CONSTRUCTOR_ELTS (rhs
));
557 else if (DECL_P (rhs
)
558 && is_gimple_reg_type (TREE_TYPE (rhs
)))
559 return get_symbol_constant_value (rhs
);
563 case GIMPLE_UNARY_RHS
:
566 case GIMPLE_BINARY_RHS
:
569 case GIMPLE_TERNARY_RHS
:
570 result
= fold_ternary_loc (loc
, subcode
,
571 TREE_TYPE (gimple_assign_lhs (stmt
)),
572 gimple_assign_rhs1 (stmt
),
573 gimple_assign_rhs2 (stmt
),
574 gimple_assign_rhs3 (stmt
));
578 STRIP_USELESS_TYPE_CONVERSION (result
);
579 if (valid_gimple_rhs_p (result
))
584 case GIMPLE_INVALID_RHS
:
592 /* Replace a statement at *SI_P with a sequence of statements in STMTS,
593 adjusting the replacement stmts location and virtual operands.
594 If the statement has a lhs the last stmt in the sequence is expected
595 to assign to that lhs. */
598 gsi_replace_with_seq_vops (gimple_stmt_iterator
*si_p
, gimple_seq stmts
)
600 gimple
*stmt
= gsi_stmt (*si_p
);
602 if (gimple_has_location (stmt
))
603 annotate_all_with_location (stmts
, gimple_location (stmt
));
605 /* First iterate over the replacement statements backward, assigning
606 virtual operands to their defining statements. */
607 gimple
*laststore
= NULL
;
608 for (gimple_stmt_iterator i
= gsi_last (stmts
);
609 !gsi_end_p (i
); gsi_prev (&i
))
611 gimple
*new_stmt
= gsi_stmt (i
);
612 if ((gimple_assign_single_p (new_stmt
)
613 && !is_gimple_reg (gimple_assign_lhs (new_stmt
)))
614 || (is_gimple_call (new_stmt
)
615 && (gimple_call_flags (new_stmt
)
616 & (ECF_NOVOPS
| ECF_PURE
| ECF_CONST
| ECF_NORETURN
)) == 0))
620 vdef
= gimple_vdef (stmt
);
622 vdef
= make_ssa_name (gimple_vop (cfun
), new_stmt
);
623 gimple_set_vdef (new_stmt
, vdef
);
624 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
625 SSA_NAME_DEF_STMT (vdef
) = new_stmt
;
626 laststore
= new_stmt
;
630 /* Second iterate over the statements forward, assigning virtual
631 operands to their uses. */
632 tree reaching_vuse
= gimple_vuse (stmt
);
633 for (gimple_stmt_iterator i
= gsi_start (stmts
);
634 !gsi_end_p (i
); gsi_next (&i
))
636 gimple
*new_stmt
= gsi_stmt (i
);
637 /* If the new statement possibly has a VUSE, update it with exact SSA
638 name we know will reach this one. */
639 if (gimple_has_mem_ops (new_stmt
))
640 gimple_set_vuse (new_stmt
, reaching_vuse
);
641 gimple_set_modified (new_stmt
, true);
642 if (gimple_vdef (new_stmt
))
643 reaching_vuse
= gimple_vdef (new_stmt
);
646 /* If the new sequence does not do a store release the virtual
647 definition of the original statement. */
649 && reaching_vuse
== gimple_vuse (stmt
))
651 tree vdef
= gimple_vdef (stmt
);
653 && TREE_CODE (vdef
) == SSA_NAME
)
655 unlink_stmt_vdef (stmt
);
656 release_ssa_name (vdef
);
660 /* Finally replace the original statement with the sequence. */
661 gsi_replace_with_seq (si_p
, stmts
, false);
664 /* Helper function for update_gimple_call and
665 gimplify_and_update_call_from_tree. A GIMPLE_CALL STMT is being replaced
666 with GIMPLE_CALL NEW_STMT. */
669 finish_update_gimple_call (gimple_stmt_iterator
*si_p
, gimple
*new_stmt
,
672 tree lhs
= gimple_call_lhs (stmt
);
673 gimple_call_set_lhs (new_stmt
, lhs
);
674 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
675 SSA_NAME_DEF_STMT (lhs
) = new_stmt
;
676 gimple_move_vops (new_stmt
, stmt
);
677 gimple_set_location (new_stmt
, gimple_location (stmt
));
678 if (gimple_block (new_stmt
) == NULL_TREE
)
679 gimple_set_block (new_stmt
, gimple_block (stmt
));
680 gsi_replace (si_p
, new_stmt
, false);
683 /* Update a GIMPLE_CALL statement at iterator *SI_P to call to FN
684 with number of arguments NARGS, where the arguments in GIMPLE form
685 follow NARGS argument. */
688 update_gimple_call (gimple_stmt_iterator
*si_p
, tree fn
, int nargs
, ...)
691 gcall
*new_stmt
, *stmt
= as_a
<gcall
*> (gsi_stmt (*si_p
));
693 gcc_assert (is_gimple_call (stmt
));
694 va_start (ap
, nargs
);
695 new_stmt
= gimple_build_call_valist (fn
, nargs
, ap
);
696 finish_update_gimple_call (si_p
, new_stmt
, stmt
);
701 /* Return true if EXPR is a CALL_EXPR suitable for representation
702 as a single GIMPLE_CALL statement. If the arguments require
703 further gimplification, return false. */
706 valid_gimple_call_p (tree expr
)
710 if (TREE_CODE (expr
) != CALL_EXPR
)
713 nargs
= call_expr_nargs (expr
);
714 for (i
= 0; i
< nargs
; i
++)
716 tree arg
= CALL_EXPR_ARG (expr
, i
);
717 if (is_gimple_reg_type (TREE_TYPE (arg
)))
719 if (!is_gimple_val (arg
))
723 if (!is_gimple_lvalue (arg
))
730 /* Convert EXPR into a GIMPLE value suitable for substitution on the
731 RHS of an assignment. Insert the necessary statements before
732 iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL
733 is replaced. If the call is expected to produces a result, then it
734 is replaced by an assignment of the new RHS to the result variable.
735 If the result is to be ignored, then the call is replaced by a
736 GIMPLE_NOP. A proper VDEF chain is retained by making the first
737 VUSE and the last VDEF of the whole sequence be the same as the replaced
738 statement and using new SSA names for stores in between. */
741 gimplify_and_update_call_from_tree (gimple_stmt_iterator
*si_p
, tree expr
)
744 gimple
*stmt
, *new_stmt
;
745 gimple_stmt_iterator i
;
746 gimple_seq stmts
= NULL
;
748 stmt
= gsi_stmt (*si_p
);
750 gcc_assert (is_gimple_call (stmt
));
752 if (valid_gimple_call_p (expr
))
754 /* The call has simplified to another call. */
755 tree fn
= CALL_EXPR_FN (expr
);
757 unsigned nargs
= call_expr_nargs (expr
);
758 vec
<tree
> args
= vNULL
;
764 args
.safe_grow_cleared (nargs
, true);
766 for (i
= 0; i
< nargs
; i
++)
767 args
[i
] = CALL_EXPR_ARG (expr
, i
);
770 new_stmt
= gimple_build_call_vec (fn
, args
);
771 finish_update_gimple_call (si_p
, new_stmt
, stmt
);
776 lhs
= gimple_call_lhs (stmt
);
777 if (lhs
== NULL_TREE
)
779 push_gimplify_context (gimple_in_ssa_p (cfun
));
780 gimplify_and_add (expr
, &stmts
);
781 pop_gimplify_context (NULL
);
783 /* We can end up with folding a memcpy of an empty class assignment
784 which gets optimized away by C++ gimplification. */
785 if (gimple_seq_empty_p (stmts
))
787 if (gimple_in_ssa_p (cfun
))
789 unlink_stmt_vdef (stmt
);
792 gsi_replace (si_p
, gimple_build_nop (), false);
798 tree tmp
= force_gimple_operand (expr
, &stmts
, false, NULL_TREE
);
799 new_stmt
= gimple_build_assign (lhs
, tmp
);
800 i
= gsi_last (stmts
);
801 gsi_insert_after_without_update (&i
, new_stmt
,
802 GSI_CONTINUE_LINKING
);
805 gsi_replace_with_seq_vops (si_p
, stmts
);
809 /* Replace the call at *GSI with the gimple value VAL. */
812 replace_call_with_value (gimple_stmt_iterator
*gsi
, tree val
)
814 gimple
*stmt
= gsi_stmt (*gsi
);
815 tree lhs
= gimple_call_lhs (stmt
);
819 if (!useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (val
)))
820 val
= fold_convert (TREE_TYPE (lhs
), val
);
821 repl
= gimple_build_assign (lhs
, val
);
824 repl
= gimple_build_nop ();
825 tree vdef
= gimple_vdef (stmt
);
826 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
828 unlink_stmt_vdef (stmt
);
829 release_ssa_name (vdef
);
831 gsi_replace (gsi
, repl
, false);
834 /* Replace the call at *GSI with the new call REPL and fold that
838 replace_call_with_call_and_fold (gimple_stmt_iterator
*gsi
, gimple
*repl
)
840 gimple
*stmt
= gsi_stmt (*gsi
);
841 gimple_call_set_lhs (repl
, gimple_call_lhs (stmt
));
842 gimple_set_location (repl
, gimple_location (stmt
));
843 gimple_move_vops (repl
, stmt
);
844 gsi_replace (gsi
, repl
, false);
848 /* Return true if VAR is a VAR_DECL or a component thereof. */
851 var_decl_component_p (tree var
)
854 while (handled_component_p (inner
))
855 inner
= TREE_OPERAND (inner
, 0);
856 return (DECL_P (inner
)
857 || (TREE_CODE (inner
) == MEM_REF
858 && TREE_CODE (TREE_OPERAND (inner
, 0)) == ADDR_EXPR
));
861 /* Return TRUE if the SIZE argument, representing the size of an
862 object, is in a range of values of which exactly zero is valid. */
865 size_must_be_zero_p (tree size
)
867 if (integer_zerop (size
))
870 if (TREE_CODE (size
) != SSA_NAME
|| !INTEGRAL_TYPE_P (TREE_TYPE (size
)))
873 tree type
= TREE_TYPE (size
);
874 int prec
= TYPE_PRECISION (type
);
876 /* Compute the value of SSIZE_MAX, the largest positive value that
877 can be stored in ssize_t, the signed counterpart of size_t. */
878 wide_int ssize_max
= wi::lshift (wi::one (prec
), prec
- 1) - 1;
879 value_range
valid_range (build_int_cst (type
, 0),
880 wide_int_to_tree (type
, ssize_max
));
883 get_range_query (cfun
)->range_of_expr (vr
, size
);
885 get_global_range_query ()->range_of_expr (vr
, size
);
886 if (vr
.undefined_p ())
887 vr
.set_varying (TREE_TYPE (size
));
888 vr
.intersect (&valid_range
);
892 /* Fold function call to builtin mem{{,p}cpy,move}. Try to detect and
893 diagnose (otherwise undefined) overlapping copies without preventing
894 folding. When folded, GCC guarantees that overlapping memcpy has
895 the same semantics as memmove. Call to the library memcpy need not
896 provide the same guarantee. Return false if no simplification can
900 gimple_fold_builtin_memory_op (gimple_stmt_iterator
*gsi
,
901 tree dest
, tree src
, enum built_in_function code
)
903 gimple
*stmt
= gsi_stmt (*gsi
);
904 tree lhs
= gimple_call_lhs (stmt
);
905 tree len
= gimple_call_arg (stmt
, 2);
906 location_t loc
= gimple_location (stmt
);
908 /* If the LEN parameter is a constant zero or in range where
909 the only valid value is zero, return DEST. */
910 if (size_must_be_zero_p (len
))
913 if (gimple_call_lhs (stmt
))
914 repl
= gimple_build_assign (gimple_call_lhs (stmt
), dest
);
916 repl
= gimple_build_nop ();
917 tree vdef
= gimple_vdef (stmt
);
918 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
920 unlink_stmt_vdef (stmt
);
921 release_ssa_name (vdef
);
923 gsi_replace (gsi
, repl
, false);
927 /* If SRC and DEST are the same (and not volatile), return
928 DEST{,+LEN,+LEN-1}. */
929 if (operand_equal_p (src
, dest
, 0))
931 /* Avoid diagnosing exact overlap in calls to __builtin_memcpy.
932 It's safe and may even be emitted by GCC itself (see bug
934 unlink_stmt_vdef (stmt
);
935 if (gimple_vdef (stmt
) && TREE_CODE (gimple_vdef (stmt
)) == SSA_NAME
)
936 release_ssa_name (gimple_vdef (stmt
));
939 gsi_replace (gsi
, gimple_build_nop (), false);
946 /* We cannot (easily) change the type of the copy if it is a storage
947 order barrier, i.e. is equivalent to a VIEW_CONVERT_EXPR that can
948 modify the storage order of objects (see storage_order_barrier_p). */
950 = POINTER_TYPE_P (TREE_TYPE (src
))
951 ? TREE_TYPE (TREE_TYPE (src
)) : NULL_TREE
;
953 = POINTER_TYPE_P (TREE_TYPE (dest
))
954 ? TREE_TYPE (TREE_TYPE (dest
)) : NULL_TREE
;
955 tree destvar
, srcvar
, srcoff
;
956 unsigned int src_align
, dest_align
;
957 unsigned HOST_WIDE_INT tmp_len
;
960 /* Build accesses at offset zero with a ref-all character type. */
962 = build_int_cst (build_pointer_type_for_mode (char_type_node
,
965 /* If we can perform the copy efficiently with first doing all loads and
966 then all stores inline it that way. Currently efficiently means that
967 we can load all the memory with a single set operation and that the
968 total size is less than MOVE_MAX * MOVE_RATIO. */
969 src_align
= get_pointer_alignment (src
);
970 dest_align
= get_pointer_alignment (dest
);
971 if (tree_fits_uhwi_p (len
)
974 * MOVE_RATIO (optimize_function_for_size_p (cfun
))))
976 /* FIXME: Don't transform copies from strings with known length.
977 Until GCC 9 this prevented a case in gcc.dg/strlenopt-8.c
978 from being handled, and the case was XFAILed for that reason.
979 Now that it is handled and the XFAIL removed, as soon as other
980 strlenopt tests that rely on it for passing are adjusted, this
981 hack can be removed. */
982 && !c_strlen (src
, 1)
983 && !((tmp_str
= getbyterep (src
, &tmp_len
)) != NULL
984 && memchr (tmp_str
, 0, tmp_len
) == NULL
)
986 && AGGREGATE_TYPE_P (srctype
)
987 && TYPE_REVERSE_STORAGE_ORDER (srctype
))
989 && AGGREGATE_TYPE_P (desttype
)
990 && TYPE_REVERSE_STORAGE_ORDER (desttype
)))
992 unsigned ilen
= tree_to_uhwi (len
);
993 if (pow2p_hwi (ilen
))
995 /* Detect out-of-bounds accesses without issuing warnings.
996 Avoid folding out-of-bounds copies but to avoid false
997 positives for unreachable code defer warning until after
998 DCE has worked its magic.
999 -Wrestrict is still diagnosed. */
1000 if (int warning
= check_bounds_or_overlap (as_a
<gcall
*>(stmt
),
1001 dest
, src
, len
, len
,
1003 if (warning
!= OPT_Wrestrict
)
1006 scalar_int_mode mode
;
1007 if (int_mode_for_size (ilen
* 8, 0).exists (&mode
)
1008 && GET_MODE_SIZE (mode
) * BITS_PER_UNIT
== ilen
* 8
1009 && have_insn_for (SET
, mode
)
1010 /* If the destination pointer is not aligned we must be able
1011 to emit an unaligned store. */
1012 && (dest_align
>= GET_MODE_ALIGNMENT (mode
)
1013 || !targetm
.slow_unaligned_access (mode
, dest_align
)
1014 || (optab_handler (movmisalign_optab
, mode
)
1015 != CODE_FOR_nothing
)))
1017 tree type
= build_nonstandard_integer_type (ilen
* 8, 1);
1018 tree srctype
= type
;
1019 tree desttype
= type
;
1020 if (src_align
< GET_MODE_ALIGNMENT (mode
))
1021 srctype
= build_aligned_type (type
, src_align
);
1022 tree srcmem
= fold_build2 (MEM_REF
, srctype
, src
, off0
);
1023 tree tem
= fold_const_aggregate_ref (srcmem
);
1026 else if (src_align
< GET_MODE_ALIGNMENT (mode
)
1027 && targetm
.slow_unaligned_access (mode
, src_align
)
1028 && (optab_handler (movmisalign_optab
, mode
)
1029 == CODE_FOR_nothing
))
1034 if (is_gimple_reg_type (TREE_TYPE (srcmem
)))
1036 new_stmt
= gimple_build_assign (NULL_TREE
, srcmem
);
1038 = create_tmp_reg_or_ssa_name (TREE_TYPE (srcmem
),
1040 gimple_assign_set_lhs (new_stmt
, srcmem
);
1041 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
1042 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
1044 if (dest_align
< GET_MODE_ALIGNMENT (mode
))
1045 desttype
= build_aligned_type (type
, dest_align
);
1047 = gimple_build_assign (fold_build2 (MEM_REF
, desttype
,
1050 gimple_move_vops (new_stmt
, stmt
);
1053 gsi_replace (gsi
, new_stmt
, false);
1056 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
1063 if (code
== BUILT_IN_MEMMOVE
)
1065 /* Both DEST and SRC must be pointer types.
1066 ??? This is what old code did. Is the testing for pointer types
1069 If either SRC is readonly or length is 1, we can use memcpy. */
1070 if (!dest_align
|| !src_align
)
1072 if (readonly_data_expr (src
)
1073 || (tree_fits_uhwi_p (len
)
1074 && (MIN (src_align
, dest_align
) / BITS_PER_UNIT
1075 >= tree_to_uhwi (len
))))
1077 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
1080 gimple_call_set_fndecl (stmt
, fn
);
1081 gimple_call_set_arg (stmt
, 0, dest
);
1082 gimple_call_set_arg (stmt
, 1, src
);
1087 /* If *src and *dest can't overlap, optimize into memcpy as well. */
1088 if (TREE_CODE (src
) == ADDR_EXPR
1089 && TREE_CODE (dest
) == ADDR_EXPR
)
1091 tree src_base
, dest_base
, fn
;
1092 poly_int64 src_offset
= 0, dest_offset
= 0;
1093 poly_uint64 maxsize
;
1095 srcvar
= TREE_OPERAND (src
, 0);
1096 src_base
= get_addr_base_and_unit_offset (srcvar
, &src_offset
);
1097 if (src_base
== NULL
)
1099 destvar
= TREE_OPERAND (dest
, 0);
1100 dest_base
= get_addr_base_and_unit_offset (destvar
,
1102 if (dest_base
== NULL
)
1103 dest_base
= destvar
;
1104 if (!poly_int_tree_p (len
, &maxsize
))
1106 if (SSA_VAR_P (src_base
)
1107 && SSA_VAR_P (dest_base
))
1109 if (operand_equal_p (src_base
, dest_base
, 0)
1110 && ranges_maybe_overlap_p (src_offset
, maxsize
,
1111 dest_offset
, maxsize
))
1114 else if (TREE_CODE (src_base
) == MEM_REF
1115 && TREE_CODE (dest_base
) == MEM_REF
)
1117 if (! operand_equal_p (TREE_OPERAND (src_base
, 0),
1118 TREE_OPERAND (dest_base
, 0), 0))
1120 poly_offset_int full_src_offset
1121 = mem_ref_offset (src_base
) + src_offset
;
1122 poly_offset_int full_dest_offset
1123 = mem_ref_offset (dest_base
) + dest_offset
;
1124 if (ranges_maybe_overlap_p (full_src_offset
, maxsize
,
1125 full_dest_offset
, maxsize
))
1131 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
1134 gimple_call_set_fndecl (stmt
, fn
);
1135 gimple_call_set_arg (stmt
, 0, dest
);
1136 gimple_call_set_arg (stmt
, 1, src
);
1141 /* If the destination and source do not alias optimize into
1143 if ((is_gimple_min_invariant (dest
)
1144 || TREE_CODE (dest
) == SSA_NAME
)
1145 && (is_gimple_min_invariant (src
)
1146 || TREE_CODE (src
) == SSA_NAME
))
1149 ao_ref_init_from_ptr_and_size (&destr
, dest
, len
);
1150 ao_ref_init_from_ptr_and_size (&srcr
, src
, len
);
1151 if (!refs_may_alias_p_1 (&destr
, &srcr
, false))
1154 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
1157 gimple_call_set_fndecl (stmt
, fn
);
1158 gimple_call_set_arg (stmt
, 0, dest
);
1159 gimple_call_set_arg (stmt
, 1, src
);
1168 if (!tree_fits_shwi_p (len
))
1171 || (AGGREGATE_TYPE_P (srctype
)
1172 && TYPE_REVERSE_STORAGE_ORDER (srctype
)))
1175 || (AGGREGATE_TYPE_P (desttype
)
1176 && TYPE_REVERSE_STORAGE_ORDER (desttype
)))
1178 /* In the following try to find a type that is most natural to be
1179 used for the memcpy source and destination and that allows
1180 the most optimization when memcpy is turned into a plain assignment
1181 using that type. In theory we could always use a char[len] type
1182 but that only gains us that the destination and source possibly
1183 no longer will have their address taken. */
1184 if (TREE_CODE (srctype
) == ARRAY_TYPE
1185 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype
), len
))
1186 srctype
= TREE_TYPE (srctype
);
1187 if (TREE_CODE (desttype
) == ARRAY_TYPE
1188 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype
), len
))
1189 desttype
= TREE_TYPE (desttype
);
1190 if (TREE_ADDRESSABLE (srctype
)
1191 || TREE_ADDRESSABLE (desttype
))
1194 /* Make sure we are not copying using a floating-point mode or
1195 a type whose size possibly does not match its precision. */
1196 if (FLOAT_MODE_P (TYPE_MODE (desttype
))
1197 || TREE_CODE (desttype
) == BOOLEAN_TYPE
1198 || TREE_CODE (desttype
) == ENUMERAL_TYPE
)
1199 desttype
= bitwise_type_for_mode (TYPE_MODE (desttype
));
1200 if (FLOAT_MODE_P (TYPE_MODE (srctype
))
1201 || TREE_CODE (srctype
) == BOOLEAN_TYPE
1202 || TREE_CODE (srctype
) == ENUMERAL_TYPE
)
1203 srctype
= bitwise_type_for_mode (TYPE_MODE (srctype
));
1211 src_align
= get_pointer_alignment (src
);
1212 dest_align
= get_pointer_alignment (dest
);
1214 /* Choose between src and destination type for the access based
1215 on alignment, whether the access constitutes a register access
1216 and whether it may actually expose a declaration for SSA rewrite
1217 or SRA decomposition. Also try to expose a string constant, we
1218 might be able to concatenate several of them later into a single
1220 destvar
= NULL_TREE
;
1222 if (TREE_CODE (dest
) == ADDR_EXPR
1223 && var_decl_component_p (TREE_OPERAND (dest
, 0))
1224 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype
), len
)
1225 && dest_align
>= TYPE_ALIGN (desttype
)
1226 && (is_gimple_reg_type (desttype
)
1227 || src_align
>= TYPE_ALIGN (desttype
)))
1228 destvar
= fold_build2 (MEM_REF
, desttype
, dest
, off0
);
1229 else if (TREE_CODE (src
) == ADDR_EXPR
1230 && var_decl_component_p (TREE_OPERAND (src
, 0))
1231 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype
), len
)
1232 && src_align
>= TYPE_ALIGN (srctype
)
1233 && (is_gimple_reg_type (srctype
)
1234 || dest_align
>= TYPE_ALIGN (srctype
)))
1235 srcvar
= fold_build2 (MEM_REF
, srctype
, src
, off0
);
1236 /* FIXME: Don't transform copies from strings with known original length.
1237 As soon as strlenopt tests that rely on it for passing are adjusted,
1238 this hack can be removed. */
1239 else if (gimple_call_alloca_for_var_p (stmt
)
1240 && (srcvar
= string_constant (src
, &srcoff
, NULL
, NULL
))
1241 && integer_zerop (srcoff
)
1242 && tree_int_cst_equal (TYPE_SIZE_UNIT (TREE_TYPE (srcvar
)), len
)
1243 && dest_align
>= TYPE_ALIGN (TREE_TYPE (srcvar
)))
1244 srctype
= TREE_TYPE (srcvar
);
1248 /* Now that we chose an access type express the other side in
1249 terms of it if the target allows that with respect to alignment
1251 if (srcvar
== NULL_TREE
)
1253 if (src_align
>= TYPE_ALIGN (desttype
))
1254 srcvar
= fold_build2 (MEM_REF
, desttype
, src
, off0
);
1257 if (STRICT_ALIGNMENT
)
1259 srctype
= build_aligned_type (TYPE_MAIN_VARIANT (desttype
),
1261 srcvar
= fold_build2 (MEM_REF
, srctype
, src
, off0
);
1264 else if (destvar
== NULL_TREE
)
1266 if (dest_align
>= TYPE_ALIGN (srctype
))
1267 destvar
= fold_build2 (MEM_REF
, srctype
, dest
, off0
);
1270 if (STRICT_ALIGNMENT
)
1272 desttype
= build_aligned_type (TYPE_MAIN_VARIANT (srctype
),
1274 destvar
= fold_build2 (MEM_REF
, desttype
, dest
, off0
);
1278 /* Same as above, detect out-of-bounds accesses without issuing
1279 warnings. Avoid folding out-of-bounds copies but to avoid
1280 false positives for unreachable code defer warning until
1281 after DCE has worked its magic.
1282 -Wrestrict is still diagnosed. */
1283 if (int warning
= check_bounds_or_overlap (as_a
<gcall
*>(stmt
),
1284 dest
, src
, len
, len
,
1286 if (warning
!= OPT_Wrestrict
)
1290 if (is_gimple_reg_type (TREE_TYPE (srcvar
)))
1292 tree tem
= fold_const_aggregate_ref (srcvar
);
1295 if (! is_gimple_min_invariant (srcvar
))
1297 new_stmt
= gimple_build_assign (NULL_TREE
, srcvar
);
1298 srcvar
= create_tmp_reg_or_ssa_name (TREE_TYPE (srcvar
),
1300 gimple_assign_set_lhs (new_stmt
, srcvar
);
1301 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
1302 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
1304 new_stmt
= gimple_build_assign (destvar
, srcvar
);
1305 goto set_vop_and_replace
;
1308 /* We get an aggregate copy. If the source is a STRING_CST, then
1309 directly use its type to perform the copy. */
1310 if (TREE_CODE (srcvar
) == STRING_CST
)
1313 /* Or else, use an unsigned char[] type to perform the copy in order
1314 to preserve padding and to avoid any issues with TREE_ADDRESSABLE
1315 types or float modes behavior on copying. */
1318 desttype
= build_array_type_nelts (unsigned_char_type_node
,
1319 tree_to_uhwi (len
));
1321 if (src_align
> TYPE_ALIGN (srctype
))
1322 srctype
= build_aligned_type (srctype
, src_align
);
1323 srcvar
= fold_build2 (MEM_REF
, srctype
, src
, off0
);
1326 if (dest_align
> TYPE_ALIGN (desttype
))
1327 desttype
= build_aligned_type (desttype
, dest_align
);
1328 destvar
= fold_build2 (MEM_REF
, desttype
, dest
, off0
);
1329 new_stmt
= gimple_build_assign (destvar
, srcvar
);
1331 set_vop_and_replace
:
1332 gimple_move_vops (new_stmt
, stmt
);
1335 gsi_replace (gsi
, new_stmt
, false);
1338 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
1342 gimple_seq stmts
= NULL
;
1343 if (code
== BUILT_IN_MEMCPY
|| code
== BUILT_IN_MEMMOVE
)
1345 else if (code
== BUILT_IN_MEMPCPY
)
1347 len
= gimple_convert_to_ptrofftype (&stmts
, loc
, len
);
1348 dest
= gimple_build (&stmts
, loc
, POINTER_PLUS_EXPR
,
1349 TREE_TYPE (dest
), dest
, len
);
1354 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1355 gimple
*repl
= gimple_build_assign (lhs
, dest
);
1356 gsi_replace (gsi
, repl
, false);
1360 /* Transform a call to built-in bcmp(a, b, len) at *GSI into one
1361 to built-in memcmp (a, b, len). */
1364 gimple_fold_builtin_bcmp (gimple_stmt_iterator
*gsi
)
1366 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCMP
);
1371 /* Transform bcmp (a, b, len) into memcmp (a, b, len). */
1373 gimple
*stmt
= gsi_stmt (*gsi
);
1374 tree a
= gimple_call_arg (stmt
, 0);
1375 tree b
= gimple_call_arg (stmt
, 1);
1376 tree len
= gimple_call_arg (stmt
, 2);
1378 gimple
*repl
= gimple_build_call (fn
, 3, a
, b
, len
);
1379 replace_call_with_call_and_fold (gsi
, repl
);
1384 /* Transform a call to built-in bcopy (src, dest, len) at *GSI into one
1385 to built-in memmove (dest, src, len). */
1388 gimple_fold_builtin_bcopy (gimple_stmt_iterator
*gsi
)
1390 tree fn
= builtin_decl_implicit (BUILT_IN_MEMMOVE
);
1395 /* bcopy has been removed from POSIX in Issue 7 but Issue 6 specifies
1396 it's quivalent to memmove (not memcpy). Transform bcopy (src, dest,
1397 len) into memmove (dest, src, len). */
1399 gimple
*stmt
= gsi_stmt (*gsi
);
1400 tree src
= gimple_call_arg (stmt
, 0);
1401 tree dest
= gimple_call_arg (stmt
, 1);
1402 tree len
= gimple_call_arg (stmt
, 2);
1404 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1405 gimple_call_set_fntype (as_a
<gcall
*> (stmt
), TREE_TYPE (fn
));
1406 replace_call_with_call_and_fold (gsi
, repl
);
1411 /* Transform a call to built-in bzero (dest, len) at *GSI into one
1412 to built-in memset (dest, 0, len). */
1415 gimple_fold_builtin_bzero (gimple_stmt_iterator
*gsi
)
1417 tree fn
= builtin_decl_implicit (BUILT_IN_MEMSET
);
1422 /* Transform bzero (dest, len) into memset (dest, 0, len). */
1424 gimple
*stmt
= gsi_stmt (*gsi
);
1425 tree dest
= gimple_call_arg (stmt
, 0);
1426 tree len
= gimple_call_arg (stmt
, 1);
1428 gimple_seq seq
= NULL
;
1429 gimple
*repl
= gimple_build_call (fn
, 3, dest
, integer_zero_node
, len
);
1430 gimple_seq_add_stmt_without_update (&seq
, repl
);
1431 gsi_replace_with_seq_vops (gsi
, seq
);
1437 /* Fold function call to builtin memset or bzero at *GSI setting the
1438 memory of size LEN to VAL. Return whether a simplification was made. */
1441 gimple_fold_builtin_memset (gimple_stmt_iterator
*gsi
, tree c
, tree len
)
1443 gimple
*stmt
= gsi_stmt (*gsi
);
1445 unsigned HOST_WIDE_INT length
, cval
;
1447 /* If the LEN parameter is zero, return DEST. */
1448 if (integer_zerop (len
))
1450 replace_call_with_value (gsi
, gimple_call_arg (stmt
, 0));
1454 if (! tree_fits_uhwi_p (len
))
1457 if (TREE_CODE (c
) != INTEGER_CST
)
1460 tree dest
= gimple_call_arg (stmt
, 0);
1462 if (TREE_CODE (var
) != ADDR_EXPR
)
1465 var
= TREE_OPERAND (var
, 0);
1466 if (TREE_THIS_VOLATILE (var
))
1469 etype
= TREE_TYPE (var
);
1470 if (TREE_CODE (etype
) == ARRAY_TYPE
)
1471 etype
= TREE_TYPE (etype
);
1473 if (!INTEGRAL_TYPE_P (etype
)
1474 && !POINTER_TYPE_P (etype
))
1477 if (! var_decl_component_p (var
))
1480 length
= tree_to_uhwi (len
);
1481 if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (etype
)) != length
1482 || (GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (etype
))
1483 != GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (etype
)))
1484 || get_pointer_alignment (dest
) / BITS_PER_UNIT
< length
)
1487 if (length
> HOST_BITS_PER_WIDE_INT
/ BITS_PER_UNIT
)
1490 if (!type_has_mode_precision_p (etype
))
1491 etype
= lang_hooks
.types
.type_for_mode (SCALAR_INT_TYPE_MODE (etype
),
1492 TYPE_UNSIGNED (etype
));
1494 if (integer_zerop (c
))
1498 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8 || HOST_BITS_PER_WIDE_INT
> 64)
1501 cval
= TREE_INT_CST_LOW (c
);
1505 cval
|= (cval
<< 31) << 1;
1508 var
= fold_build2 (MEM_REF
, etype
, dest
, build_int_cst (ptr_type_node
, 0));
1509 gimple
*store
= gimple_build_assign (var
, build_int_cst_type (etype
, cval
));
1510 gimple_move_vops (store
, stmt
);
1511 gimple_set_location (store
, gimple_location (stmt
));
1512 gsi_insert_before (gsi
, store
, GSI_SAME_STMT
);
1513 if (gimple_call_lhs (stmt
))
1515 gimple
*asgn
= gimple_build_assign (gimple_call_lhs (stmt
), dest
);
1516 gsi_replace (gsi
, asgn
, false);
1520 gimple_stmt_iterator gsi2
= *gsi
;
1522 gsi_remove (&gsi2
, true);
1528 /* Helper of get_range_strlen for ARG that is not an SSA_NAME. */
1531 get_range_strlen_tree (tree arg
, bitmap visited
, strlen_range_kind rkind
,
1532 c_strlen_data
*pdata
, unsigned eltsize
)
1534 gcc_assert (TREE_CODE (arg
) != SSA_NAME
);
1536 /* The length computed by this invocation of the function. */
1537 tree val
= NULL_TREE
;
1539 /* True if VAL is an optimistic (tight) bound determined from
1540 the size of the character array in which the string may be
1541 stored. In that case, the computed VAL is used to set
1543 bool tight_bound
= false;
1545 /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */
1546 if (TREE_CODE (arg
) == ADDR_EXPR
1547 && TREE_CODE (TREE_OPERAND (arg
, 0)) == ARRAY_REF
)
1549 tree op
= TREE_OPERAND (arg
, 0);
1550 if (integer_zerop (TREE_OPERAND (op
, 1)))
1552 tree aop0
= TREE_OPERAND (op
, 0);
1553 if (TREE_CODE (aop0
) == INDIRECT_REF
1554 && TREE_CODE (TREE_OPERAND (aop0
, 0)) == SSA_NAME
)
1555 return get_range_strlen (TREE_OPERAND (aop0
, 0), visited
, rkind
,
1558 else if (TREE_CODE (TREE_OPERAND (op
, 0)) == COMPONENT_REF
1559 && rkind
== SRK_LENRANGE
)
1561 /* Fail if an array is the last member of a struct object
1562 since it could be treated as a (fake) flexible array
1564 tree idx
= TREE_OPERAND (op
, 1);
1566 arg
= TREE_OPERAND (op
, 0);
1567 tree optype
= TREE_TYPE (arg
);
1568 if (tree dom
= TYPE_DOMAIN (optype
))
1569 if (tree bound
= TYPE_MAX_VALUE (dom
))
1570 if (TREE_CODE (bound
) == INTEGER_CST
1571 && TREE_CODE (idx
) == INTEGER_CST
1572 && tree_int_cst_lt (bound
, idx
))
1577 if (rkind
== SRK_INT_VALUE
)
1579 /* We are computing the maximum value (not string length). */
1581 if (TREE_CODE (val
) != INTEGER_CST
1582 || tree_int_cst_sgn (val
) < 0)
1587 c_strlen_data lendata
= { };
1588 val
= c_strlen (arg
, 1, &lendata
, eltsize
);
1590 if (!val
&& lendata
.decl
)
1592 /* ARG refers to an unterminated const character array.
1593 DATA.DECL with size DATA.LEN. */
1594 val
= lendata
.minlen
;
1595 pdata
->decl
= lendata
.decl
;
1599 /* Set if VAL represents the maximum length based on array size (set
1600 when exact length cannot be determined). */
1601 bool maxbound
= false;
1603 if (!val
&& rkind
== SRK_LENRANGE
)
1605 if (TREE_CODE (arg
) == ADDR_EXPR
)
1606 return get_range_strlen (TREE_OPERAND (arg
, 0), visited
, rkind
,
1609 if (TREE_CODE (arg
) == ARRAY_REF
)
1611 tree optype
= TREE_TYPE (TREE_OPERAND (arg
, 0));
1613 /* Determine the "innermost" array type. */
1614 while (TREE_CODE (optype
) == ARRAY_TYPE
1615 && TREE_CODE (TREE_TYPE (optype
)) == ARRAY_TYPE
)
1616 optype
= TREE_TYPE (optype
);
1618 /* Avoid arrays of pointers. */
1619 tree eltype
= TREE_TYPE (optype
);
1620 if (TREE_CODE (optype
) != ARRAY_TYPE
1621 || !INTEGRAL_TYPE_P (eltype
))
1624 /* Fail when the array bound is unknown or zero. */
1625 val
= TYPE_SIZE_UNIT (optype
);
1627 || TREE_CODE (val
) != INTEGER_CST
1628 || integer_zerop (val
))
1631 val
= fold_build2 (MINUS_EXPR
, TREE_TYPE (val
), val
,
1634 /* Set the minimum size to zero since the string in
1635 the array could have zero length. */
1636 pdata
->minlen
= ssize_int (0);
1640 else if (TREE_CODE (arg
) == COMPONENT_REF
1641 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (arg
, 1)))
1644 /* Use the type of the member array to determine the upper
1645 bound on the length of the array. This may be overly
1646 optimistic if the array itself isn't NUL-terminated and
1647 the caller relies on the subsequent member to contain
1648 the NUL but that would only be considered valid if
1649 the array were the last member of a struct. */
1651 tree fld
= TREE_OPERAND (arg
, 1);
1653 tree optype
= TREE_TYPE (fld
);
1655 /* Determine the "innermost" array type. */
1656 while (TREE_CODE (optype
) == ARRAY_TYPE
1657 && TREE_CODE (TREE_TYPE (optype
)) == ARRAY_TYPE
)
1658 optype
= TREE_TYPE (optype
);
1660 /* Fail when the array bound is unknown or zero. */
1661 val
= TYPE_SIZE_UNIT (optype
);
1663 || TREE_CODE (val
) != INTEGER_CST
1664 || integer_zerop (val
))
1666 val
= fold_build2 (MINUS_EXPR
, TREE_TYPE (val
), val
,
1669 /* Set the minimum size to zero since the string in
1670 the array could have zero length. */
1671 pdata
->minlen
= ssize_int (0);
1673 /* The array size determined above is an optimistic bound
1674 on the length. If the array isn't nul-terminated the
1675 length computed by the library function would be greater.
1676 Even though using strlen to cross the subobject boundary
1677 is undefined, avoid drawing conclusions from the member
1678 type about the length here. */
1681 else if (TREE_CODE (arg
) == MEM_REF
1682 && TREE_CODE (TREE_TYPE (arg
)) == ARRAY_TYPE
1683 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) == INTEGER_TYPE
1684 && TREE_CODE (TREE_OPERAND (arg
, 0)) == ADDR_EXPR
)
1686 /* Handle a MEM_REF into a DECL accessing an array of integers,
1687 being conservative about references to extern structures with
1688 flexible array members that can be initialized to arbitrary
1689 numbers of elements as an extension (static structs are okay).
1690 FIXME: Make this less conservative -- see
1691 component_ref_size in tree.cc. */
1692 tree ref
= TREE_OPERAND (TREE_OPERAND (arg
, 0), 0);
1693 if ((TREE_CODE (ref
) == PARM_DECL
|| VAR_P (ref
))
1694 && (decl_binds_to_current_def_p (ref
)
1695 || !array_at_struct_end_p (arg
)))
1697 /* Fail if the offset is out of bounds. Such accesses
1698 should be diagnosed at some point. */
1699 val
= DECL_SIZE_UNIT (ref
);
1701 || TREE_CODE (val
) != INTEGER_CST
1702 || integer_zerop (val
))
1705 poly_offset_int psiz
= wi::to_offset (val
);
1706 poly_offset_int poff
= mem_ref_offset (arg
);
1707 if (known_le (psiz
, poff
))
1710 pdata
->minlen
= ssize_int (0);
1712 /* Subtract the offset and one for the terminating nul. */
1715 val
= wide_int_to_tree (TREE_TYPE (val
), psiz
);
1716 /* Since VAL reflects the size of a declared object
1717 rather the type of the access it is not a tight bound. */
1720 else if (TREE_CODE (arg
) == PARM_DECL
|| VAR_P (arg
))
1722 /* Avoid handling pointers to arrays. GCC might misuse
1723 a pointer to an array of one bound to point to an array
1724 object of a greater bound. */
1725 tree argtype
= TREE_TYPE (arg
);
1726 if (TREE_CODE (argtype
) == ARRAY_TYPE
)
1728 val
= TYPE_SIZE_UNIT (argtype
);
1730 || TREE_CODE (val
) != INTEGER_CST
1731 || integer_zerop (val
))
1733 val
= wide_int_to_tree (TREE_TYPE (val
),
1734 wi::sub (wi::to_wide (val
), 1));
1736 /* Set the minimum size to zero since the string in
1737 the array could have zero length. */
1738 pdata
->minlen
= ssize_int (0);
1747 /* Adjust the lower bound on the string length as necessary. */
1749 || (rkind
!= SRK_STRLEN
1750 && TREE_CODE (pdata
->minlen
) == INTEGER_CST
1751 && TREE_CODE (val
) == INTEGER_CST
1752 && tree_int_cst_lt (val
, pdata
->minlen
)))
1753 pdata
->minlen
= val
;
1755 if (pdata
->maxbound
&& TREE_CODE (pdata
->maxbound
) == INTEGER_CST
)
1757 /* Adjust the tighter (more optimistic) string length bound
1758 if necessary and proceed to adjust the more conservative
1760 if (TREE_CODE (val
) == INTEGER_CST
)
1762 if (tree_int_cst_lt (pdata
->maxbound
, val
))
1763 pdata
->maxbound
= val
;
1766 pdata
->maxbound
= val
;
1768 else if (pdata
->maxbound
|| maxbound
)
1769 /* Set PDATA->MAXBOUND only if it either isn't INTEGER_CST or
1770 if VAL corresponds to the maximum length determined based
1771 on the type of the object. */
1772 pdata
->maxbound
= val
;
1776 /* VAL computed above represents an optimistically tight bound
1777 on the length of the string based on the referenced object's
1778 or subobject's type. Determine the conservative upper bound
1779 based on the enclosing object's size if possible. */
1780 if (rkind
== SRK_LENRANGE
)
1783 tree base
= get_addr_base_and_unit_offset (arg
, &offset
);
1786 /* When the call above fails due to a non-constant offset
1787 assume the offset is zero and use the size of the whole
1788 enclosing object instead. */
1789 base
= get_base_address (arg
);
1792 /* If the base object is a pointer no upper bound on the length
1793 can be determined. Otherwise the maximum length is equal to
1794 the size of the enclosing object minus the offset of
1795 the referenced subobject minus 1 (for the terminating nul). */
1796 tree type
= TREE_TYPE (base
);
1797 if (TREE_CODE (type
) == POINTER_TYPE
1798 || (TREE_CODE (base
) != PARM_DECL
&& !VAR_P (base
))
1799 || !(val
= DECL_SIZE_UNIT (base
)))
1800 val
= build_all_ones_cst (size_type_node
);
1803 val
= DECL_SIZE_UNIT (base
);
1804 val
= fold_build2 (MINUS_EXPR
, TREE_TYPE (val
), val
,
1805 size_int (offset
+ 1));
1814 /* Adjust the more conservative bound if possible/necessary
1815 and fail otherwise. */
1816 if (rkind
!= SRK_STRLEN
)
1818 if (TREE_CODE (pdata
->maxlen
) != INTEGER_CST
1819 || TREE_CODE (val
) != INTEGER_CST
)
1822 if (tree_int_cst_lt (pdata
->maxlen
, val
))
1823 pdata
->maxlen
= val
;
1826 else if (simple_cst_equal (val
, pdata
->maxlen
) != 1)
1828 /* Fail if the length of this ARG is different from that
1829 previously determined from another ARG. */
1834 pdata
->maxlen
= val
;
1835 return rkind
== SRK_LENRANGE
|| !integer_all_onesp (val
);
1838 /* For an ARG referencing one or more strings, try to obtain the range
1839 of their lengths, or the size of the largest array ARG referes to if
1840 the range of lengths cannot be determined, and store all in *PDATA.
1841 For an integer ARG (when RKIND == SRK_INT_VALUE), try to determine
1842 the maximum constant value.
1843 If ARG is an SSA_NAME, follow its use-def chains. When RKIND ==
1844 SRK_STRLEN, then if PDATA->MAXLEN is not equal to the determined
1845 length or if we are unable to determine the length, return false.
1846 VISITED is a bitmap of visited variables.
1847 RKIND determines the kind of value or range to obtain (see
1849 Set PDATA->DECL if ARG refers to an unterminated constant array.
1850 On input, set ELTSIZE to 1 for normal single byte character strings,
1851 and either 2 or 4 for wide characer strings (the size of wchar_t).
1852 Return true if *PDATA was successfully populated and false otherwise. */
1855 get_range_strlen (tree arg
, bitmap visited
,
1856 strlen_range_kind rkind
,
1857 c_strlen_data
*pdata
, unsigned eltsize
)
1860 if (TREE_CODE (arg
) != SSA_NAME
)
1861 return get_range_strlen_tree (arg
, visited
, rkind
, pdata
, eltsize
);
1863 /* If ARG is registered for SSA update we cannot look at its defining
1865 if (name_registered_for_update_p (arg
))
1868 /* If we were already here, break the infinite cycle. */
1869 if (!bitmap_set_bit (visited
, SSA_NAME_VERSION (arg
)))
1873 gimple
*def_stmt
= SSA_NAME_DEF_STMT (var
);
1875 switch (gimple_code (def_stmt
))
1878 /* The RHS of the statement defining VAR must either have a
1879 constant length or come from another SSA_NAME with a constant
1881 if (gimple_assign_single_p (def_stmt
)
1882 || gimple_assign_unary_nop_p (def_stmt
))
1884 tree rhs
= gimple_assign_rhs1 (def_stmt
);
1885 return get_range_strlen (rhs
, visited
, rkind
, pdata
, eltsize
);
1887 else if (gimple_assign_rhs_code (def_stmt
) == COND_EXPR
)
1889 tree ops
[2] = { gimple_assign_rhs2 (def_stmt
),
1890 gimple_assign_rhs3 (def_stmt
) };
1892 for (unsigned int i
= 0; i
< 2; i
++)
1893 if (!get_range_strlen (ops
[i
], visited
, rkind
, pdata
, eltsize
))
1895 if (rkind
!= SRK_LENRANGE
)
1897 /* Set the upper bound to the maximum to prevent
1898 it from being adjusted in the next iteration but
1899 leave MINLEN and the more conservative MAXBOUND
1900 determined so far alone (or leave them null if
1901 they haven't been set yet). That the MINLEN is
1902 in fact zero can be determined from MAXLEN being
1903 unbounded but the discovered minimum is used for
1905 pdata
->maxlen
= build_all_ones_cst (size_type_node
);
1912 /* Unless RKIND == SRK_LENRANGE, all arguments of the PHI node
1913 must have a constant length. */
1914 for (unsigned i
= 0; i
< gimple_phi_num_args (def_stmt
); i
++)
1916 tree arg
= gimple_phi_arg (def_stmt
, i
)->def
;
1918 /* If this PHI has itself as an argument, we cannot
1919 determine the string length of this argument. However,
1920 if we can find a constant string length for the other
1921 PHI args then we can still be sure that this is a
1922 constant string length. So be optimistic and just
1923 continue with the next argument. */
1924 if (arg
== gimple_phi_result (def_stmt
))
1927 if (!get_range_strlen (arg
, visited
, rkind
, pdata
, eltsize
))
1929 if (rkind
!= SRK_LENRANGE
)
1931 /* Set the upper bound to the maximum to prevent
1932 it from being adjusted in the next iteration but
1933 leave MINLEN and the more conservative MAXBOUND
1934 determined so far alone (or leave them null if
1935 they haven't been set yet). That the MINLEN is
1936 in fact zero can be determined from MAXLEN being
1937 unbounded but the discovered minimum is used for
1939 pdata
->maxlen
= build_all_ones_cst (size_type_node
);
1949 /* Try to obtain the range of the lengths of the string(s) referenced
1950 by ARG, or the size of the largest array ARG refers to if the range
1951 of lengths cannot be determined, and store all in *PDATA which must
1952 be zero-initialized on input except PDATA->MAXBOUND may be set to
1953 a non-null tree node other than INTEGER_CST to request to have it
1954 set to the length of the longest string in a PHI. ELTSIZE is
1955 the expected size of the string element in bytes: 1 for char and
1956 some power of 2 for wide characters.
1957 Return true if the range [PDATA->MINLEN, PDATA->MAXLEN] is suitable
1958 for optimization. Returning false means that a nonzero PDATA->MINLEN
1959 doesn't reflect the true lower bound of the range when PDATA->MAXLEN
1960 is -1 (in that case, the actual range is indeterminate, i.e.,
1961 [0, PTRDIFF_MAX - 2]. */
1964 get_range_strlen (tree arg
, c_strlen_data
*pdata
, unsigned eltsize
)
1966 auto_bitmap visited
;
1967 tree maxbound
= pdata
->maxbound
;
1969 if (!get_range_strlen (arg
, visited
, SRK_LENRANGE
, pdata
, eltsize
))
1971 /* On failure extend the length range to an impossible maximum
1972 (a valid MAXLEN must be less than PTRDIFF_MAX - 1). Other
1973 members can stay unchanged regardless. */
1974 pdata
->minlen
= ssize_int (0);
1975 pdata
->maxlen
= build_all_ones_cst (size_type_node
);
1977 else if (!pdata
->minlen
)
1978 pdata
->minlen
= ssize_int (0);
1980 /* If it's unchanged from it initial non-null value, set the conservative
1981 MAXBOUND to SIZE_MAX. Otherwise leave it null (if it is null). */
1982 if (maxbound
&& pdata
->maxbound
== maxbound
)
1983 pdata
->maxbound
= build_all_ones_cst (size_type_node
);
1985 return !integer_all_onesp (pdata
->maxlen
);
1988 /* Return the maximum value for ARG given RKIND (see strlen_range_kind).
1989 For ARG of pointer types, NONSTR indicates if the caller is prepared
1990 to handle unterminated strings. For integer ARG and when RKIND ==
1991 SRK_INT_VALUE, NONSTR must be null.
1993 If an unterminated array is discovered and our caller handles
1994 unterminated arrays, then bubble up the offending DECL and
1995 return the maximum size. Otherwise return NULL. */
1998 get_maxval_strlen (tree arg
, strlen_range_kind rkind
, tree
*nonstr
= NULL
)
2000 /* A non-null NONSTR is meaningless when determining the maximum
2001 value of an integer ARG. */
2002 gcc_assert (rkind
!= SRK_INT_VALUE
|| nonstr
== NULL
);
2003 /* ARG must have an integral type when RKIND says so. */
2004 gcc_assert (rkind
!= SRK_INT_VALUE
|| INTEGRAL_TYPE_P (TREE_TYPE (arg
)));
2006 auto_bitmap visited
;
2008 /* Reset DATA.MAXLEN if the call fails or when DATA.MAXLEN
2010 c_strlen_data lendata
= { };
2011 if (!get_range_strlen (arg
, visited
, rkind
, &lendata
, /* eltsize = */1))
2012 lendata
.maxlen
= NULL_TREE
;
2013 else if (lendata
.maxlen
&& integer_all_onesp (lendata
.maxlen
))
2014 lendata
.maxlen
= NULL_TREE
;
2018 /* For callers prepared to handle unterminated arrays set
2019 *NONSTR to point to the declaration of the array and return
2020 the maximum length/size. */
2021 *nonstr
= lendata
.decl
;
2022 return lendata
.maxlen
;
2025 /* Fail if the constant array isn't nul-terminated. */
2026 return lendata
.decl
? NULL_TREE
: lendata
.maxlen
;
2029 /* Return true if LEN is known to be less than or equal to (or if STRICT is
2030 true, strictly less than) the lower bound of SIZE at compile time and false
2034 known_lower (gimple
*stmt
, tree len
, tree size
, bool strict
= false)
2036 if (len
== NULL_TREE
)
2039 wide_int size_range
[2];
2040 wide_int len_range
[2];
2041 if (get_range (len
, stmt
, len_range
) && get_range (size
, stmt
, size_range
))
2044 return wi::ltu_p (len_range
[1], size_range
[0]);
2046 return wi::leu_p (len_range
[1], size_range
[0]);
2052 /* Fold function call to builtin strcpy with arguments DEST and SRC.
2053 If LEN is not NULL, it represents the length of the string to be
2054 copied. Return NULL_TREE if no simplification can be made. */
2057 gimple_fold_builtin_strcpy (gimple_stmt_iterator
*gsi
,
2058 tree dest
, tree src
)
2060 gimple
*stmt
= gsi_stmt (*gsi
);
2061 location_t loc
= gimple_location (stmt
);
2064 /* If SRC and DEST are the same (and not volatile), return DEST. */
2065 if (operand_equal_p (src
, dest
, 0))
2067 /* Issue -Wrestrict unless the pointers are null (those do
2068 not point to objects and so do not indicate an overlap;
2069 such calls could be the result of sanitization and jump
2071 if (!integer_zerop (dest
) && !warning_suppressed_p (stmt
, OPT_Wrestrict
))
2073 tree func
= gimple_call_fndecl (stmt
);
2075 warning_at (loc
, OPT_Wrestrict
,
2076 "%qD source argument is the same as destination",
2080 replace_call_with_value (gsi
, dest
);
2084 if (optimize_function_for_size_p (cfun
))
2087 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
2091 /* Set to non-null if ARG refers to an unterminated array. */
2093 tree len
= get_maxval_strlen (src
, SRK_STRLEN
, &nonstr
);
2097 /* Avoid folding calls with unterminated arrays. */
2098 if (!warning_suppressed_p (stmt
, OPT_Wstringop_overread
))
2099 warn_string_no_nul (loc
, stmt
, "strcpy", src
, nonstr
);
2100 suppress_warning (stmt
, OPT_Wstringop_overread
);
2107 len
= fold_convert_loc (loc
, size_type_node
, len
);
2108 len
= size_binop_loc (loc
, PLUS_EXPR
, len
, build_int_cst (size_type_node
, 1));
2109 len
= force_gimple_operand_gsi (gsi
, len
, true,
2110 NULL_TREE
, true, GSI_SAME_STMT
);
2111 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
2112 replace_call_with_call_and_fold (gsi
, repl
);
2116 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
2117 If SLEN is not NULL, it represents the length of the source string.
2118 Return NULL_TREE if no simplification can be made. */
2121 gimple_fold_builtin_strncpy (gimple_stmt_iterator
*gsi
,
2122 tree dest
, tree src
, tree len
)
2124 gimple
*stmt
= gsi_stmt (*gsi
);
2125 location_t loc
= gimple_location (stmt
);
2126 bool nonstring
= get_attr_nonstring_decl (dest
) != NULL_TREE
;
2128 /* If the LEN parameter is zero, return DEST. */
2129 if (integer_zerop (len
))
2131 /* Avoid warning if the destination refers to an array/pointer
2132 decorate with attribute nonstring. */
2135 tree fndecl
= gimple_call_fndecl (stmt
);
2137 /* Warn about the lack of nul termination: the result is not
2138 a (nul-terminated) string. */
2139 tree slen
= get_maxval_strlen (src
, SRK_STRLEN
);
2140 if (slen
&& !integer_zerop (slen
))
2141 warning_at (loc
, OPT_Wstringop_truncation
,
2142 "%qD destination unchanged after copying no bytes "
2143 "from a string of length %E",
2146 warning_at (loc
, OPT_Wstringop_truncation
,
2147 "%qD destination unchanged after copying no bytes",
2151 replace_call_with_value (gsi
, dest
);
2155 /* We can't compare slen with len as constants below if len is not a
2157 if (TREE_CODE (len
) != INTEGER_CST
)
2160 /* Now, we must be passed a constant src ptr parameter. */
2161 tree slen
= get_maxval_strlen (src
, SRK_STRLEN
);
2162 if (!slen
|| TREE_CODE (slen
) != INTEGER_CST
)
2165 /* The size of the source string including the terminating nul. */
2166 tree ssize
= size_binop_loc (loc
, PLUS_EXPR
, slen
, ssize_int (1));
2168 /* We do not support simplification of this case, though we do
2169 support it when expanding trees into RTL. */
2170 /* FIXME: generate a call to __builtin_memset. */
2171 if (tree_int_cst_lt (ssize
, len
))
2174 /* Diagnose truncation that leaves the copy unterminated. */
2175 maybe_diag_stxncpy_trunc (*gsi
, src
, len
);
2177 /* OK transform into builtin memcpy. */
2178 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
2182 len
= fold_convert_loc (loc
, size_type_node
, len
);
2183 len
= force_gimple_operand_gsi (gsi
, len
, true,
2184 NULL_TREE
, true, GSI_SAME_STMT
);
2185 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
2186 replace_call_with_call_and_fold (gsi
, repl
);
2191 /* Fold function call to builtin strchr or strrchr.
2192 If both arguments are constant, evaluate and fold the result,
2193 otherwise simplify str(r)chr (str, 0) into str + strlen (str).
2194 In general strlen is significantly faster than strchr
2195 due to being a simpler operation. */
2197 gimple_fold_builtin_strchr (gimple_stmt_iterator
*gsi
, bool is_strrchr
)
2199 gimple
*stmt
= gsi_stmt (*gsi
);
2200 tree str
= gimple_call_arg (stmt
, 0);
2201 tree c
= gimple_call_arg (stmt
, 1);
2202 location_t loc
= gimple_location (stmt
);
2206 if (!gimple_call_lhs (stmt
))
2209 /* Avoid folding if the first argument is not a nul-terminated array.
2210 Defer warning until later. */
2211 if (!check_nul_terminated_array (NULL_TREE
, str
))
2214 if ((p
= c_getstr (str
)) && target_char_cst_p (c
, &ch
))
2216 const char *p1
= is_strrchr
? strrchr (p
, ch
) : strchr (p
, ch
);
2220 replace_call_with_value (gsi
, integer_zero_node
);
2224 tree len
= build_int_cst (size_type_node
, p1
- p
);
2225 gimple_seq stmts
= NULL
;
2226 gimple
*new_stmt
= gimple_build_assign (gimple_call_lhs (stmt
),
2227 POINTER_PLUS_EXPR
, str
, len
);
2228 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
2229 gsi_replace_with_seq_vops (gsi
, stmts
);
2233 if (!integer_zerop (c
))
2236 /* Transform strrchr (s, 0) to strchr (s, 0) when optimizing for size. */
2237 if (is_strrchr
&& optimize_function_for_size_p (cfun
))
2239 tree strchr_fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
2243 gimple
*repl
= gimple_build_call (strchr_fn
, 2, str
, c
);
2244 replace_call_with_call_and_fold (gsi
, repl
);
2252 tree strlen_fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
2257 /* Create newstr = strlen (str). */
2258 gimple_seq stmts
= NULL
;
2259 gimple
*new_stmt
= gimple_build_call (strlen_fn
, 1, str
);
2260 gimple_set_location (new_stmt
, loc
);
2261 len
= create_tmp_reg_or_ssa_name (size_type_node
);
2262 gimple_call_set_lhs (new_stmt
, len
);
2263 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
2265 /* Create (str p+ strlen (str)). */
2266 new_stmt
= gimple_build_assign (gimple_call_lhs (stmt
),
2267 POINTER_PLUS_EXPR
, str
, len
);
2268 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
2269 gsi_replace_with_seq_vops (gsi
, stmts
);
2270 /* gsi now points at the assignment to the lhs, get a
2271 stmt iterator to the strlen.
2272 ??? We can't use gsi_for_stmt as that doesn't work when the
2273 CFG isn't built yet. */
2274 gimple_stmt_iterator gsi2
= *gsi
;
2280 /* Fold function call to builtin strstr.
2281 If both arguments are constant, evaluate and fold the result,
2282 additionally fold strstr (x, "") into x and strstr (x, "c")
2283 into strchr (x, 'c'). */
2285 gimple_fold_builtin_strstr (gimple_stmt_iterator
*gsi
)
2287 gimple
*stmt
= gsi_stmt (*gsi
);
2288 if (!gimple_call_lhs (stmt
))
2291 tree haystack
= gimple_call_arg (stmt
, 0);
2292 tree needle
= gimple_call_arg (stmt
, 1);
2294 /* Avoid folding if either argument is not a nul-terminated array.
2295 Defer warning until later. */
2296 if (!check_nul_terminated_array (NULL_TREE
, haystack
)
2297 || !check_nul_terminated_array (NULL_TREE
, needle
))
2300 const char *q
= c_getstr (needle
);
2304 if (const char *p
= c_getstr (haystack
))
2306 const char *r
= strstr (p
, q
);
2310 replace_call_with_value (gsi
, integer_zero_node
);
2314 tree len
= build_int_cst (size_type_node
, r
- p
);
2315 gimple_seq stmts
= NULL
;
2317 = gimple_build_assign (gimple_call_lhs (stmt
), POINTER_PLUS_EXPR
,
2319 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
2320 gsi_replace_with_seq_vops (gsi
, stmts
);
2324 /* For strstr (x, "") return x. */
2327 replace_call_with_value (gsi
, haystack
);
2331 /* Transform strstr (x, "c") into strchr (x, 'c'). */
2334 tree strchr_fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
2337 tree c
= build_int_cst (integer_type_node
, q
[0]);
2338 gimple
*repl
= gimple_build_call (strchr_fn
, 2, haystack
, c
);
2339 replace_call_with_call_and_fold (gsi
, repl
);
2347 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
2350 Return NULL_TREE if no simplification was possible, otherwise return the
2351 simplified form of the call as a tree.
2353 The simplified form may be a constant or other expression which
2354 computes the same value, but in a more efficient manner (including
2355 calls to other builtin functions).
2357 The call may contain arguments which need to be evaluated, but
2358 which are not useful to determine the result of the call. In
2359 this case we return a chain of COMPOUND_EXPRs. The LHS of each
2360 COMPOUND_EXPR will be an argument which must be evaluated.
2361 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
2362 COMPOUND_EXPR in the chain will contain the tree for the simplified
2363 form of the builtin function call. */
2366 gimple_fold_builtin_strcat (gimple_stmt_iterator
*gsi
, tree dst
, tree src
)
2368 gimple
*stmt
= gsi_stmt (*gsi
);
2369 location_t loc
= gimple_location (stmt
);
2371 const char *p
= c_getstr (src
);
2373 /* If the string length is zero, return the dst parameter. */
2374 if (p
&& *p
== '\0')
2376 replace_call_with_value (gsi
, dst
);
2380 if (!optimize_bb_for_speed_p (gimple_bb (stmt
)))
2383 /* See if we can store by pieces into (dst + strlen(dst)). */
2385 tree strlen_fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
2386 tree memcpy_fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
2388 if (!strlen_fn
|| !memcpy_fn
)
2391 /* If the length of the source string isn't computable don't
2392 split strcat into strlen and memcpy. */
2393 tree len
= get_maxval_strlen (src
, SRK_STRLEN
);
2397 /* Create strlen (dst). */
2398 gimple_seq stmts
= NULL
, stmts2
;
2399 gimple
*repl
= gimple_build_call (strlen_fn
, 1, dst
);
2400 gimple_set_location (repl
, loc
);
2401 newdst
= create_tmp_reg_or_ssa_name (size_type_node
);
2402 gimple_call_set_lhs (repl
, newdst
);
2403 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2405 /* Create (dst p+ strlen (dst)). */
2406 newdst
= fold_build_pointer_plus_loc (loc
, dst
, newdst
);
2407 newdst
= force_gimple_operand (newdst
, &stmts2
, true, NULL_TREE
);
2408 gimple_seq_add_seq_without_update (&stmts
, stmts2
);
2410 len
= fold_convert_loc (loc
, size_type_node
, len
);
2411 len
= size_binop_loc (loc
, PLUS_EXPR
, len
,
2412 build_int_cst (size_type_node
, 1));
2413 len
= force_gimple_operand (len
, &stmts2
, true, NULL_TREE
);
2414 gimple_seq_add_seq_without_update (&stmts
, stmts2
);
2416 repl
= gimple_build_call (memcpy_fn
, 3, newdst
, src
, len
);
2417 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2418 if (gimple_call_lhs (stmt
))
2420 repl
= gimple_build_assign (gimple_call_lhs (stmt
), dst
);
2421 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2422 gsi_replace_with_seq_vops (gsi
, stmts
);
2423 /* gsi now points at the assignment to the lhs, get a
2424 stmt iterator to the memcpy call.
2425 ??? We can't use gsi_for_stmt as that doesn't work when the
2426 CFG isn't built yet. */
2427 gimple_stmt_iterator gsi2
= *gsi
;
2433 gsi_replace_with_seq_vops (gsi
, stmts
);
2439 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
2440 are the arguments to the call. */
2443 gimple_fold_builtin_strcat_chk (gimple_stmt_iterator
*gsi
)
2445 gimple
*stmt
= gsi_stmt (*gsi
);
2446 tree dest
= gimple_call_arg (stmt
, 0);
2447 tree src
= gimple_call_arg (stmt
, 1);
2448 tree size
= gimple_call_arg (stmt
, 2);
2454 /* If the SRC parameter is "", return DEST. */
2455 if (p
&& *p
== '\0')
2457 replace_call_with_value (gsi
, dest
);
2461 if (! tree_fits_uhwi_p (size
) || ! integer_all_onesp (size
))
2464 /* If __builtin_strcat_chk is used, assume strcat is available. */
2465 fn
= builtin_decl_explicit (BUILT_IN_STRCAT
);
2469 gimple
*repl
= gimple_build_call (fn
, 2, dest
, src
);
2470 replace_call_with_call_and_fold (gsi
, repl
);
2474 /* Simplify a call to the strncat builtin. */
2477 gimple_fold_builtin_strncat (gimple_stmt_iterator
*gsi
)
2479 gimple
*stmt
= gsi_stmt (*gsi
);
2480 tree dst
= gimple_call_arg (stmt
, 0);
2481 tree src
= gimple_call_arg (stmt
, 1);
2482 tree len
= gimple_call_arg (stmt
, 2);
2483 tree src_len
= c_strlen (src
, 1);
2485 /* If the requested length is zero, or the src parameter string
2486 length is zero, return the dst parameter. */
2487 if (integer_zerop (len
) || (src_len
&& integer_zerop (src_len
)))
2489 replace_call_with_value (gsi
, dst
);
2493 /* Return early if the requested len is less than the string length.
2494 Warnings will be issued elsewhere later. */
2495 if (!src_len
|| known_lower (stmt
, len
, src_len
, true))
2498 /* Warn on constant LEN. */
2499 if (TREE_CODE (len
) == INTEGER_CST
)
2501 bool nowarn
= warning_suppressed_p (stmt
, OPT_Wstringop_overflow_
);
2504 if (!nowarn
&& compute_builtin_object_size (dst
, 1, &dstsize
)
2505 && TREE_CODE (dstsize
) == INTEGER_CST
)
2507 int cmpdst
= tree_int_cst_compare (len
, dstsize
);
2511 tree fndecl
= gimple_call_fndecl (stmt
);
2513 /* Strncat copies (at most) LEN bytes and always appends
2514 the terminating NUL so the specified bound should never
2515 be equal to (or greater than) the size of the destination.
2516 If it is, the copy could overflow. */
2517 location_t loc
= gimple_location (stmt
);
2518 nowarn
= warning_at (loc
, OPT_Wstringop_overflow_
,
2520 ? G_("%qD specified bound %E equals "
2522 : G_("%qD specified bound %E exceeds "
2523 "destination size %E"),
2524 fndecl
, len
, dstsize
);
2526 suppress_warning (stmt
, OPT_Wstringop_overflow_
);
2530 if (!nowarn
&& TREE_CODE (src_len
) == INTEGER_CST
2531 && tree_int_cst_compare (src_len
, len
) == 0)
2533 tree fndecl
= gimple_call_fndecl (stmt
);
2534 location_t loc
= gimple_location (stmt
);
2536 /* To avoid possible overflow the specified bound should also
2537 not be equal to the length of the source, even when the size
2538 of the destination is unknown (it's not an uncommon mistake
2539 to specify as the bound to strncpy the length of the source). */
2540 if (warning_at (loc
, OPT_Wstringop_overflow_
,
2541 "%qD specified bound %E equals source length",
2543 suppress_warning (stmt
, OPT_Wstringop_overflow_
);
2547 if (!known_lower (stmt
, src_len
, len
))
2550 tree fn
= builtin_decl_implicit (BUILT_IN_STRCAT
);
2552 /* If the replacement _DECL isn't initialized, don't do the
2557 /* Otherwise, emit a call to strcat. */
2558 gcall
*repl
= gimple_build_call (fn
, 2, dst
, src
);
2559 replace_call_with_call_and_fold (gsi
, repl
);
2563 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
2567 gimple_fold_builtin_strncat_chk (gimple_stmt_iterator
*gsi
)
2569 gimple
*stmt
= gsi_stmt (*gsi
);
2570 tree dest
= gimple_call_arg (stmt
, 0);
2571 tree src
= gimple_call_arg (stmt
, 1);
2572 tree len
= gimple_call_arg (stmt
, 2);
2573 tree size
= gimple_call_arg (stmt
, 3);
2578 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
2579 if ((p
&& *p
== '\0')
2580 || integer_zerop (len
))
2582 replace_call_with_value (gsi
, dest
);
2586 if (! integer_all_onesp (size
))
2588 tree src_len
= c_strlen (src
, 1);
2589 if (known_lower (stmt
, src_len
, len
))
2591 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
2592 fn
= builtin_decl_explicit (BUILT_IN_STRCAT_CHK
);
2596 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, size
);
2597 replace_call_with_call_and_fold (gsi
, repl
);
2603 /* If __builtin_strncat_chk is used, assume strncat is available. */
2604 fn
= builtin_decl_explicit (BUILT_IN_STRNCAT
);
2608 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
2609 replace_call_with_call_and_fold (gsi
, repl
);
2613 /* Build and append gimple statements to STMTS that would load a first
2614 character of a memory location identified by STR. LOC is location
2615 of the statement. */
2618 gimple_load_first_char (location_t loc
, tree str
, gimple_seq
*stmts
)
2622 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
2623 tree cst_uchar_ptr_node
2624 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
2625 tree off0
= build_int_cst (cst_uchar_ptr_node
, 0);
2627 tree temp
= fold_build2_loc (loc
, MEM_REF
, cst_uchar_node
, str
, off0
);
2628 gassign
*stmt
= gimple_build_assign (NULL_TREE
, temp
);
2629 var
= create_tmp_reg_or_ssa_name (cst_uchar_node
, stmt
);
2631 gimple_assign_set_lhs (stmt
, var
);
2632 gimple_seq_add_stmt_without_update (stmts
, stmt
);
2637 /* Fold a call to the str{n}{case}cmp builtin pointed by GSI iterator. */
2640 gimple_fold_builtin_string_compare (gimple_stmt_iterator
*gsi
)
2642 gimple
*stmt
= gsi_stmt (*gsi
);
2643 tree callee
= gimple_call_fndecl (stmt
);
2644 enum built_in_function fcode
= DECL_FUNCTION_CODE (callee
);
2646 tree type
= integer_type_node
;
2647 tree str1
= gimple_call_arg (stmt
, 0);
2648 tree str2
= gimple_call_arg (stmt
, 1);
2649 tree lhs
= gimple_call_lhs (stmt
);
2651 tree bound_node
= NULL_TREE
;
2652 unsigned HOST_WIDE_INT bound
= HOST_WIDE_INT_M1U
;
2654 /* Handle strncmp and strncasecmp functions. */
2655 if (gimple_call_num_args (stmt
) == 3)
2657 bound_node
= gimple_call_arg (stmt
, 2);
2658 if (tree_fits_uhwi_p (bound_node
))
2659 bound
= tree_to_uhwi (bound_node
);
2662 /* If the BOUND parameter is zero, return zero. */
2665 replace_call_with_value (gsi
, integer_zero_node
);
2669 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
2670 if (operand_equal_p (str1
, str2
, 0))
2672 replace_call_with_value (gsi
, integer_zero_node
);
2676 /* Initially set to the number of characters, including the terminating
2677 nul if each array has one. LENx == strnlen (Sx, LENx) implies that
2678 the array Sx is not terminated by a nul.
2679 For nul-terminated strings then adjusted to their length so that
2680 LENx == NULPOSx holds. */
2681 unsigned HOST_WIDE_INT len1
= HOST_WIDE_INT_MAX
, len2
= len1
;
2682 const char *p1
= getbyterep (str1
, &len1
);
2683 const char *p2
= getbyterep (str2
, &len2
);
2685 /* The position of the terminating nul character if one exists, otherwise
2686 a value greater than LENx. */
2687 unsigned HOST_WIDE_INT nulpos1
= HOST_WIDE_INT_MAX
, nulpos2
= nulpos1
;
2691 size_t n
= strnlen (p1
, len1
);
2698 size_t n
= strnlen (p2
, len2
);
2703 /* For known strings, return an immediate value. */
2707 bool known_result
= false;
2711 case BUILT_IN_STRCMP
:
2712 case BUILT_IN_STRCMP_EQ
:
2713 if (len1
!= nulpos1
|| len2
!= nulpos2
)
2716 r
= strcmp (p1
, p2
);
2717 known_result
= true;
2720 case BUILT_IN_STRNCMP
:
2721 case BUILT_IN_STRNCMP_EQ
:
2723 if (bound
== HOST_WIDE_INT_M1U
)
2726 /* Reduce the bound to be no more than the length
2727 of the shorter of the two strings, or the sizes
2728 of the unterminated arrays. */
2729 unsigned HOST_WIDE_INT n
= bound
;
2731 if (len1
== nulpos1
&& len1
< n
)
2733 if (len2
== nulpos2
&& len2
< n
)
2736 if (MIN (nulpos1
, nulpos2
) + 1 < n
)
2739 r
= strncmp (p1
, p2
, n
);
2740 known_result
= true;
2743 /* Only handleable situation is where the string are equal (result 0),
2744 which is already handled by operand_equal_p case. */
2745 case BUILT_IN_STRCASECMP
:
2747 case BUILT_IN_STRNCASECMP
:
2749 if (bound
== HOST_WIDE_INT_M1U
)
2751 r
= strncmp (p1
, p2
, bound
);
2753 known_result
= true;
2762 replace_call_with_value (gsi
, build_cmp_result (type
, r
));
2767 bool nonzero_bound
= (bound
>= 1 && bound
< HOST_WIDE_INT_M1U
)
2768 || fcode
== BUILT_IN_STRCMP
2769 || fcode
== BUILT_IN_STRCMP_EQ
2770 || fcode
== BUILT_IN_STRCASECMP
;
2772 location_t loc
= gimple_location (stmt
);
2774 /* If the second arg is "", return *(const unsigned char*)arg1. */
2775 if (p2
&& *p2
== '\0' && nonzero_bound
)
2777 gimple_seq stmts
= NULL
;
2778 tree var
= gimple_load_first_char (loc
, str1
, &stmts
);
2781 stmt
= gimple_build_assign (lhs
, NOP_EXPR
, var
);
2782 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2785 gsi_replace_with_seq_vops (gsi
, stmts
);
2789 /* If the first arg is "", return -*(const unsigned char*)arg2. */
2790 if (p1
&& *p1
== '\0' && nonzero_bound
)
2792 gimple_seq stmts
= NULL
;
2793 tree var
= gimple_load_first_char (loc
, str2
, &stmts
);
2797 tree c
= create_tmp_reg_or_ssa_name (integer_type_node
);
2798 stmt
= gimple_build_assign (c
, NOP_EXPR
, var
);
2799 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2801 stmt
= gimple_build_assign (lhs
, NEGATE_EXPR
, c
);
2802 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2805 gsi_replace_with_seq_vops (gsi
, stmts
);
2809 /* If BOUND is one, return an expression corresponding to
2810 (*(const unsigned char*)arg2 - *(const unsigned char*)arg1). */
2811 if (fcode
== BUILT_IN_STRNCMP
&& bound
== 1)
2813 gimple_seq stmts
= NULL
;
2814 tree temp1
= gimple_load_first_char (loc
, str1
, &stmts
);
2815 tree temp2
= gimple_load_first_char (loc
, str2
, &stmts
);
2819 tree c1
= create_tmp_reg_or_ssa_name (integer_type_node
);
2820 gassign
*convert1
= gimple_build_assign (c1
, NOP_EXPR
, temp1
);
2821 gimple_seq_add_stmt_without_update (&stmts
, convert1
);
2823 tree c2
= create_tmp_reg_or_ssa_name (integer_type_node
);
2824 gassign
*convert2
= gimple_build_assign (c2
, NOP_EXPR
, temp2
);
2825 gimple_seq_add_stmt_without_update (&stmts
, convert2
);
2827 stmt
= gimple_build_assign (lhs
, MINUS_EXPR
, c1
, c2
);
2828 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2831 gsi_replace_with_seq_vops (gsi
, stmts
);
2835 /* If BOUND is greater than the length of one constant string,
2836 and the other argument is also a nul-terminated string, replace
2837 strncmp with strcmp. */
2838 if (fcode
== BUILT_IN_STRNCMP
2839 && bound
> 0 && bound
< HOST_WIDE_INT_M1U
2840 && ((p2
&& len2
< bound
&& len2
== nulpos2
)
2841 || (p1
&& len1
< bound
&& len1
== nulpos1
)))
2843 tree fn
= builtin_decl_implicit (BUILT_IN_STRCMP
);
2846 gimple
*repl
= gimple_build_call (fn
, 2, str1
, str2
);
2847 replace_call_with_call_and_fold (gsi
, repl
);
2854 /* Fold a call to the memchr pointed by GSI iterator. */
2857 gimple_fold_builtin_memchr (gimple_stmt_iterator
*gsi
)
2859 gimple
*stmt
= gsi_stmt (*gsi
);
2860 tree lhs
= gimple_call_lhs (stmt
);
2861 tree arg1
= gimple_call_arg (stmt
, 0);
2862 tree arg2
= gimple_call_arg (stmt
, 1);
2863 tree len
= gimple_call_arg (stmt
, 2);
2865 /* If the LEN parameter is zero, return zero. */
2866 if (integer_zerop (len
))
2868 replace_call_with_value (gsi
, build_int_cst (ptr_type_node
, 0));
2873 if (TREE_CODE (arg2
) != INTEGER_CST
2874 || !tree_fits_uhwi_p (len
)
2875 || !target_char_cst_p (arg2
, &c
))
2878 unsigned HOST_WIDE_INT length
= tree_to_uhwi (len
);
2879 unsigned HOST_WIDE_INT string_length
;
2880 const char *p1
= getbyterep (arg1
, &string_length
);
2884 const char *r
= (const char *)memchr (p1
, c
, MIN (length
, string_length
));
2887 tree mem_size
, offset_node
;
2888 byte_representation (arg1
, &offset_node
, &mem_size
, NULL
);
2889 unsigned HOST_WIDE_INT offset
= (offset_node
== NULL_TREE
)
2890 ? 0 : tree_to_uhwi (offset_node
);
2891 /* MEM_SIZE is the size of the array the string literal
2893 unsigned HOST_WIDE_INT string_size
= tree_to_uhwi (mem_size
) - offset
;
2894 gcc_checking_assert (string_length
<= string_size
);
2895 if (length
<= string_size
)
2897 replace_call_with_value (gsi
, build_int_cst (ptr_type_node
, 0));
2903 unsigned HOST_WIDE_INT offset
= r
- p1
;
2904 gimple_seq stmts
= NULL
;
2905 if (lhs
!= NULL_TREE
)
2907 tree offset_cst
= build_int_cst (sizetype
, offset
);
2908 gassign
*stmt
= gimple_build_assign (lhs
, POINTER_PLUS_EXPR
,
2910 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2913 gimple_seq_add_stmt_without_update (&stmts
,
2914 gimple_build_nop ());
2916 gsi_replace_with_seq_vops (gsi
, stmts
);
2924 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
2925 to the call. IGNORE is true if the value returned
2926 by the builtin will be ignored. UNLOCKED is true is true if this
2927 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
2928 the known length of the string. Return NULL_TREE if no simplification
2932 gimple_fold_builtin_fputs (gimple_stmt_iterator
*gsi
,
2933 tree arg0
, tree arg1
,
2936 gimple
*stmt
= gsi_stmt (*gsi
);
2938 /* If we're using an unlocked function, assume the other unlocked
2939 functions exist explicitly. */
2940 tree
const fn_fputc
= (unlocked
2941 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED
)
2942 : builtin_decl_implicit (BUILT_IN_FPUTC
));
2943 tree
const fn_fwrite
= (unlocked
2944 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED
)
2945 : builtin_decl_implicit (BUILT_IN_FWRITE
));
2947 /* If the return value is used, don't do the transformation. */
2948 if (gimple_call_lhs (stmt
))
2951 /* Get the length of the string passed to fputs. If the length
2952 can't be determined, punt. */
2953 tree len
= get_maxval_strlen (arg0
, SRK_STRLEN
);
2955 || TREE_CODE (len
) != INTEGER_CST
)
2958 switch (compare_tree_int (len
, 1))
2960 case -1: /* length is 0, delete the call entirely . */
2961 replace_call_with_value (gsi
, integer_zero_node
);
2964 case 0: /* length is 1, call fputc. */
2966 const char *p
= c_getstr (arg0
);
2972 gimple
*repl
= gimple_build_call (fn_fputc
, 2,
2974 (integer_type_node
, p
[0]), arg1
);
2975 replace_call_with_call_and_fold (gsi
, repl
);
2980 case 1: /* length is greater than 1, call fwrite. */
2982 /* If optimizing for size keep fputs. */
2983 if (optimize_function_for_size_p (cfun
))
2985 /* New argument list transforming fputs(string, stream) to
2986 fwrite(string, 1, len, stream). */
2990 gimple
*repl
= gimple_build_call (fn_fwrite
, 4, arg0
,
2991 size_one_node
, len
, arg1
);
2992 replace_call_with_call_and_fold (gsi
, repl
);
3000 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
3001 DEST, SRC, LEN, and SIZE are the arguments to the call.
3002 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
3003 code of the builtin. If MAXLEN is not NULL, it is maximum length
3004 passed as third argument. */
3007 gimple_fold_builtin_memory_chk (gimple_stmt_iterator
*gsi
,
3008 tree dest
, tree src
, tree len
, tree size
,
3009 enum built_in_function fcode
)
3011 gimple
*stmt
= gsi_stmt (*gsi
);
3012 location_t loc
= gimple_location (stmt
);
3013 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
3016 /* If SRC and DEST are the same (and not volatile), return DEST
3017 (resp. DEST+LEN for __mempcpy_chk). */
3018 if (fcode
!= BUILT_IN_MEMSET_CHK
&& operand_equal_p (src
, dest
, 0))
3020 if (fcode
!= BUILT_IN_MEMPCPY_CHK
)
3022 replace_call_with_value (gsi
, dest
);
3027 gimple_seq stmts
= NULL
;
3028 len
= gimple_convert_to_ptrofftype (&stmts
, loc
, len
);
3029 tree temp
= gimple_build (&stmts
, loc
, POINTER_PLUS_EXPR
,
3030 TREE_TYPE (dest
), dest
, len
);
3031 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
3032 replace_call_with_value (gsi
, temp
);
3037 tree maxlen
= get_maxval_strlen (len
, SRK_INT_VALUE
);
3038 if (! integer_all_onesp (size
)
3039 && !known_lower (stmt
, len
, size
)
3040 && !known_lower (stmt
, maxlen
, size
))
3042 /* MAXLEN and LEN both cannot be proved to be less than SIZE, at
3043 least try to optimize (void) __mempcpy_chk () into
3044 (void) __memcpy_chk () */
3045 if (fcode
== BUILT_IN_MEMPCPY_CHK
&& ignore
)
3047 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
3051 gimple
*repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
3052 replace_call_with_call_and_fold (gsi
, repl
);
3059 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
3060 mem{cpy,pcpy,move,set} is available. */
3063 case BUILT_IN_MEMCPY_CHK
:
3064 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY
);
3066 case BUILT_IN_MEMPCPY_CHK
:
3067 fn
= builtin_decl_explicit (BUILT_IN_MEMPCPY
);
3069 case BUILT_IN_MEMMOVE_CHK
:
3070 fn
= builtin_decl_explicit (BUILT_IN_MEMMOVE
);
3072 case BUILT_IN_MEMSET_CHK
:
3073 fn
= builtin_decl_explicit (BUILT_IN_MEMSET
);
3082 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
3083 replace_call_with_call_and_fold (gsi
, repl
);
3087 /* Print a message in the dump file recording transformation of FROM to TO. */
3090 dump_transformation (gcall
*from
, gcall
*to
)
3092 if (dump_enabled_p ())
3093 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, from
, "simplified %T to %T\n",
3094 gimple_call_fn (from
), gimple_call_fn (to
));
3097 /* Fold a call to the __st[rp]cpy_chk builtin.
3098 DEST, SRC, and SIZE are the arguments to the call.
3099 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
3100 code of the builtin. If MAXLEN is not NULL, it is maximum length of
3101 strings passed as second argument. */
3104 gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator
*gsi
,
3106 tree src
, tree size
,
3107 enum built_in_function fcode
)
3109 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3110 location_t loc
= gimple_location (stmt
);
3111 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
3114 /* If SRC and DEST are the same (and not volatile), return DEST. */
3115 if (fcode
== BUILT_IN_STRCPY_CHK
&& operand_equal_p (src
, dest
, 0))
3117 /* Issue -Wrestrict unless the pointers are null (those do
3118 not point to objects and so do not indicate an overlap;
3119 such calls could be the result of sanitization and jump
3121 if (!integer_zerop (dest
)
3122 && !warning_suppressed_p (stmt
, OPT_Wrestrict
))
3124 tree func
= gimple_call_fndecl (stmt
);
3126 warning_at (loc
, OPT_Wrestrict
,
3127 "%qD source argument is the same as destination",
3131 replace_call_with_value (gsi
, dest
);
3135 tree maxlen
= get_maxval_strlen (src
, SRK_STRLENMAX
);
3136 if (! integer_all_onesp (size
))
3138 len
= c_strlen (src
, 1);
3139 if (!known_lower (stmt
, len
, size
, true)
3140 && !known_lower (stmt
, maxlen
, size
, true))
3142 if (fcode
== BUILT_IN_STPCPY_CHK
)
3147 /* If return value of __stpcpy_chk is ignored,
3148 optimize into __strcpy_chk. */
3149 fn
= builtin_decl_explicit (BUILT_IN_STRCPY_CHK
);
3153 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, size
);
3154 replace_call_with_call_and_fold (gsi
, repl
);
3158 if (! len
|| TREE_SIDE_EFFECTS (len
))
3161 /* If c_strlen returned something, but not provably less than size,
3162 transform __strcpy_chk into __memcpy_chk. */
3163 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
3167 gimple_seq stmts
= NULL
;
3168 len
= force_gimple_operand (len
, &stmts
, true, NULL_TREE
);
3169 len
= gimple_convert (&stmts
, loc
, size_type_node
, len
);
3170 len
= gimple_build (&stmts
, loc
, PLUS_EXPR
, size_type_node
, len
,
3171 build_int_cst (size_type_node
, 1));
3172 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
3173 gimple
*repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
3174 replace_call_with_call_and_fold (gsi
, repl
);
3179 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
3180 fn
= builtin_decl_explicit (fcode
== BUILT_IN_STPCPY_CHK
&& !ignore
3181 ? BUILT_IN_STPCPY
: BUILT_IN_STRCPY
);
3185 gcall
*repl
= gimple_build_call (fn
, 2, dest
, src
);
3186 dump_transformation (stmt
, repl
);
3187 replace_call_with_call_and_fold (gsi
, repl
);
3191 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
3192 are the arguments to the call. If MAXLEN is not NULL, it is maximum
3193 length passed as third argument. IGNORE is true if return value can be
3194 ignored. FCODE is the BUILT_IN_* code of the builtin. */
3197 gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator
*gsi
,
3198 tree dest
, tree src
,
3199 tree len
, tree size
,
3200 enum built_in_function fcode
)
3202 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3203 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
3206 tree maxlen
= get_maxval_strlen (len
, SRK_INT_VALUE
);
3207 if (! integer_all_onesp (size
)
3208 && !known_lower (stmt
, len
, size
) && !known_lower (stmt
, maxlen
, size
))
3210 if (fcode
== BUILT_IN_STPNCPY_CHK
&& ignore
)
3212 /* If return value of __stpncpy_chk is ignored,
3213 optimize into __strncpy_chk. */
3214 fn
= builtin_decl_explicit (BUILT_IN_STRNCPY_CHK
);
3217 gimple
*repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
3218 replace_call_with_call_and_fold (gsi
, repl
);
3225 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
3226 fn
= builtin_decl_explicit (fcode
== BUILT_IN_STPNCPY_CHK
&& !ignore
3227 ? BUILT_IN_STPNCPY
: BUILT_IN_STRNCPY
);
3231 gcall
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
3232 dump_transformation (stmt
, repl
);
3233 replace_call_with_call_and_fold (gsi
, repl
);
3237 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
3238 Return NULL_TREE if no simplification can be made. */
3241 gimple_fold_builtin_stpcpy (gimple_stmt_iterator
*gsi
)
3243 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3244 location_t loc
= gimple_location (stmt
);
3245 tree dest
= gimple_call_arg (stmt
, 0);
3246 tree src
= gimple_call_arg (stmt
, 1);
3249 /* If the result is unused, replace stpcpy with strcpy. */
3250 if (gimple_call_lhs (stmt
) == NULL_TREE
)
3252 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3255 gimple_call_set_fndecl (stmt
, fn
);
3260 /* Set to non-null if ARG refers to an unterminated array. */
3261 c_strlen_data data
= { };
3262 /* The size of the unterminated array if SRC referes to one. */
3264 /* True if the size is exact/constant, false if it's the lower bound
3267 tree len
= c_strlen (src
, 1, &data
, 1);
3269 || TREE_CODE (len
) != INTEGER_CST
)
3271 data
.decl
= unterminated_array (src
, &size
, &exact
);
3278 /* Avoid folding calls with unterminated arrays. */
3279 if (!warning_suppressed_p (stmt
, OPT_Wstringop_overread
))
3280 warn_string_no_nul (loc
, stmt
, "stpcpy", src
, data
.decl
, size
,
3282 suppress_warning (stmt
, OPT_Wstringop_overread
);
3286 if (optimize_function_for_size_p (cfun
)
3287 /* If length is zero it's small enough. */
3288 && !integer_zerop (len
))
3291 /* If the source has a known length replace stpcpy with memcpy. */
3292 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
3296 gimple_seq stmts
= NULL
;
3297 tree tem
= gimple_convert (&stmts
, loc
, size_type_node
, len
);
3298 lenp1
= gimple_build (&stmts
, loc
, PLUS_EXPR
, size_type_node
,
3299 tem
, build_int_cst (size_type_node
, 1));
3300 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
3301 gcall
*repl
= gimple_build_call (fn
, 3, dest
, src
, lenp1
);
3302 gimple_move_vops (repl
, stmt
);
3303 gsi_insert_before (gsi
, repl
, GSI_SAME_STMT
);
3304 /* Replace the result with dest + len. */
3306 tem
= gimple_convert (&stmts
, loc
, sizetype
, len
);
3307 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
3308 gassign
*ret
= gimple_build_assign (gimple_call_lhs (stmt
),
3309 POINTER_PLUS_EXPR
, dest
, tem
);
3310 gsi_replace (gsi
, ret
, false);
3311 /* Finally fold the memcpy call. */
3312 gimple_stmt_iterator gsi2
= *gsi
;
3318 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
3319 NULL_TREE if a normal call should be emitted rather than expanding
3320 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
3321 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
3322 passed as second argument. */
3325 gimple_fold_builtin_snprintf_chk (gimple_stmt_iterator
*gsi
,
3326 enum built_in_function fcode
)
3328 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3329 tree dest
, size
, len
, fn
, fmt
, flag
;
3330 const char *fmt_str
;
3332 /* Verify the required arguments in the original call. */
3333 if (gimple_call_num_args (stmt
) < 5)
3336 dest
= gimple_call_arg (stmt
, 0);
3337 len
= gimple_call_arg (stmt
, 1);
3338 flag
= gimple_call_arg (stmt
, 2);
3339 size
= gimple_call_arg (stmt
, 3);
3340 fmt
= gimple_call_arg (stmt
, 4);
3342 tree maxlen
= get_maxval_strlen (len
, SRK_INT_VALUE
);
3343 if (! integer_all_onesp (size
)
3344 && !known_lower (stmt
, len
, size
) && !known_lower (stmt
, maxlen
, size
))
3347 if (!init_target_chars ())
3350 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
3351 or if format doesn't contain % chars or is "%s". */
3352 if (! integer_zerop (flag
))
3354 fmt_str
= c_getstr (fmt
);
3355 if (fmt_str
== NULL
)
3357 if (strchr (fmt_str
, target_percent
) != NULL
3358 && strcmp (fmt_str
, target_percent_s
))
3362 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
3364 fn
= builtin_decl_explicit (fcode
== BUILT_IN_VSNPRINTF_CHK
3365 ? BUILT_IN_VSNPRINTF
: BUILT_IN_SNPRINTF
);
3369 /* Replace the called function and the first 5 argument by 3 retaining
3370 trailing varargs. */
3371 gimple_call_set_fndecl (stmt
, fn
);
3372 gimple_call_set_fntype (stmt
, TREE_TYPE (fn
));
3373 gimple_call_set_arg (stmt
, 0, dest
);
3374 gimple_call_set_arg (stmt
, 1, len
);
3375 gimple_call_set_arg (stmt
, 2, fmt
);
3376 for (unsigned i
= 3; i
< gimple_call_num_args (stmt
) - 2; ++i
)
3377 gimple_call_set_arg (stmt
, i
, gimple_call_arg (stmt
, i
+ 2));
3378 gimple_set_num_ops (stmt
, gimple_num_ops (stmt
) - 2);
3383 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
3384 Return NULL_TREE if a normal call should be emitted rather than
3385 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
3386 or BUILT_IN_VSPRINTF_CHK. */
3389 gimple_fold_builtin_sprintf_chk (gimple_stmt_iterator
*gsi
,
3390 enum built_in_function fcode
)
3392 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3393 tree dest
, size
, len
, fn
, fmt
, flag
;
3394 const char *fmt_str
;
3395 unsigned nargs
= gimple_call_num_args (stmt
);
3397 /* Verify the required arguments in the original call. */
3400 dest
= gimple_call_arg (stmt
, 0);
3401 flag
= gimple_call_arg (stmt
, 1);
3402 size
= gimple_call_arg (stmt
, 2);
3403 fmt
= gimple_call_arg (stmt
, 3);
3407 if (!init_target_chars ())
3410 /* Check whether the format is a literal string constant. */
3411 fmt_str
= c_getstr (fmt
);
3412 if (fmt_str
!= NULL
)
3414 /* If the format doesn't contain % args or %%, we know the size. */
3415 if (strchr (fmt_str
, target_percent
) == 0)
3417 if (fcode
!= BUILT_IN_SPRINTF_CHK
|| nargs
== 4)
3418 len
= build_int_cstu (size_type_node
, strlen (fmt_str
));
3420 /* If the format is "%s" and first ... argument is a string literal,
3421 we know the size too. */
3422 else if (fcode
== BUILT_IN_SPRINTF_CHK
3423 && strcmp (fmt_str
, target_percent_s
) == 0)
3429 arg
= gimple_call_arg (stmt
, 4);
3430 if (POINTER_TYPE_P (TREE_TYPE (arg
)))
3431 len
= c_strlen (arg
, 1);
3436 if (! integer_all_onesp (size
) && !known_lower (stmt
, len
, size
, true))
3439 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
3440 or if format doesn't contain % chars or is "%s". */
3441 if (! integer_zerop (flag
))
3443 if (fmt_str
== NULL
)
3445 if (strchr (fmt_str
, target_percent
) != NULL
3446 && strcmp (fmt_str
, target_percent_s
))
3450 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
3451 fn
= builtin_decl_explicit (fcode
== BUILT_IN_VSPRINTF_CHK
3452 ? BUILT_IN_VSPRINTF
: BUILT_IN_SPRINTF
);
3456 /* Replace the called function and the first 4 argument by 2 retaining
3457 trailing varargs. */
3458 gimple_call_set_fndecl (stmt
, fn
);
3459 gimple_call_set_fntype (stmt
, TREE_TYPE (fn
));
3460 gimple_call_set_arg (stmt
, 0, dest
);
3461 gimple_call_set_arg (stmt
, 1, fmt
);
3462 for (unsigned i
= 2; i
< gimple_call_num_args (stmt
) - 2; ++i
)
3463 gimple_call_set_arg (stmt
, i
, gimple_call_arg (stmt
, i
+ 2));
3464 gimple_set_num_ops (stmt
, gimple_num_ops (stmt
) - 2);
3469 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
3470 ORIG may be null if this is a 2-argument call. We don't attempt to
3471 simplify calls with more than 3 arguments.
3473 Return true if simplification was possible, otherwise false. */
3476 gimple_fold_builtin_sprintf (gimple_stmt_iterator
*gsi
)
3478 gimple
*stmt
= gsi_stmt (*gsi
);
3480 /* Verify the required arguments in the original call. We deal with two
3481 types of sprintf() calls: 'sprintf (str, fmt)' and
3482 'sprintf (dest, "%s", orig)'. */
3483 if (gimple_call_num_args (stmt
) > 3)
3486 tree orig
= NULL_TREE
;
3487 if (gimple_call_num_args (stmt
) == 3)
3488 orig
= gimple_call_arg (stmt
, 2);
3490 /* Check whether the format is a literal string constant. */
3491 tree fmt
= gimple_call_arg (stmt
, 1);
3492 const char *fmt_str
= c_getstr (fmt
);
3493 if (fmt_str
== NULL
)
3496 tree dest
= gimple_call_arg (stmt
, 0);
3498 if (!init_target_chars ())
3501 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3505 /* If the format doesn't contain % args or %%, use strcpy. */
3506 if (strchr (fmt_str
, target_percent
) == NULL
)
3508 /* Don't optimize sprintf (buf, "abc", ptr++). */
3512 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
3513 'format' is known to contain no % formats. */
3514 gimple_seq stmts
= NULL
;
3515 gimple
*repl
= gimple_build_call (fn
, 2, dest
, fmt
);
3517 /* Propagate the NO_WARNING bit to avoid issuing the same
3518 warning more than once. */
3519 copy_warning (repl
, stmt
);
3521 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3522 if (tree lhs
= gimple_call_lhs (stmt
))
3524 repl
= gimple_build_assign (lhs
, build_int_cst (TREE_TYPE (lhs
),
3526 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3527 gsi_replace_with_seq_vops (gsi
, stmts
);
3528 /* gsi now points at the assignment to the lhs, get a
3529 stmt iterator to the memcpy call.
3530 ??? We can't use gsi_for_stmt as that doesn't work when the
3531 CFG isn't built yet. */
3532 gimple_stmt_iterator gsi2
= *gsi
;
3538 gsi_replace_with_seq_vops (gsi
, stmts
);
3544 /* If the format is "%s", use strcpy if the result isn't used. */
3545 else if (fmt_str
&& strcmp (fmt_str
, target_percent_s
) == 0)
3547 /* Don't crash on sprintf (str1, "%s"). */
3551 /* Don't fold calls with source arguments of invalid (nonpointer)
3553 if (!POINTER_TYPE_P (TREE_TYPE (orig
)))
3556 tree orig_len
= NULL_TREE
;
3557 if (gimple_call_lhs (stmt
))
3559 orig_len
= get_maxval_strlen (orig
, SRK_STRLEN
);
3564 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
3565 gimple_seq stmts
= NULL
;
3566 gimple
*repl
= gimple_build_call (fn
, 2, dest
, orig
);
3568 /* Propagate the NO_WARNING bit to avoid issuing the same
3569 warning more than once. */
3570 copy_warning (repl
, stmt
);
3572 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3573 if (tree lhs
= gimple_call_lhs (stmt
))
3575 if (!useless_type_conversion_p (TREE_TYPE (lhs
),
3576 TREE_TYPE (orig_len
)))
3577 orig_len
= fold_convert (TREE_TYPE (lhs
), orig_len
);
3578 repl
= gimple_build_assign (lhs
, orig_len
);
3579 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3580 gsi_replace_with_seq_vops (gsi
, stmts
);
3581 /* gsi now points at the assignment to the lhs, get a
3582 stmt iterator to the memcpy call.
3583 ??? We can't use gsi_for_stmt as that doesn't work when the
3584 CFG isn't built yet. */
3585 gimple_stmt_iterator gsi2
= *gsi
;
3591 gsi_replace_with_seq_vops (gsi
, stmts
);
3599 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
3600 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
3601 attempt to simplify calls with more than 4 arguments.
3603 Return true if simplification was possible, otherwise false. */
3606 gimple_fold_builtin_snprintf (gimple_stmt_iterator
*gsi
)
3608 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3609 tree dest
= gimple_call_arg (stmt
, 0);
3610 tree destsize
= gimple_call_arg (stmt
, 1);
3611 tree fmt
= gimple_call_arg (stmt
, 2);
3612 tree orig
= NULL_TREE
;
3613 const char *fmt_str
= NULL
;
3615 if (gimple_call_num_args (stmt
) > 4)
3618 if (gimple_call_num_args (stmt
) == 4)
3619 orig
= gimple_call_arg (stmt
, 3);
3621 /* Check whether the format is a literal string constant. */
3622 fmt_str
= c_getstr (fmt
);
3623 if (fmt_str
== NULL
)
3626 if (!init_target_chars ())
3629 /* If the format doesn't contain % args or %%, use strcpy. */
3630 if (strchr (fmt_str
, target_percent
) == NULL
)
3632 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3636 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
3640 tree len
= build_int_cstu (TREE_TYPE (destsize
), strlen (fmt_str
));
3642 /* We could expand this as
3643 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
3645 memcpy (str, fmt_with_nul_at_cstm1, cst);
3646 but in the former case that might increase code size
3647 and in the latter case grow .rodata section too much.
3649 if (!known_lower (stmt
, len
, destsize
, true))
3652 gimple_seq stmts
= NULL
;
3653 gimple
*repl
= gimple_build_call (fn
, 2, dest
, fmt
);
3654 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3655 if (tree lhs
= gimple_call_lhs (stmt
))
3657 repl
= gimple_build_assign (lhs
,
3658 fold_convert (TREE_TYPE (lhs
), len
));
3659 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3660 gsi_replace_with_seq_vops (gsi
, stmts
);
3661 /* gsi now points at the assignment to the lhs, get a
3662 stmt iterator to the memcpy call.
3663 ??? We can't use gsi_for_stmt as that doesn't work when the
3664 CFG isn't built yet. */
3665 gimple_stmt_iterator gsi2
= *gsi
;
3671 gsi_replace_with_seq_vops (gsi
, stmts
);
3677 /* If the format is "%s", use strcpy if the result isn't used. */
3678 else if (fmt_str
&& strcmp (fmt_str
, target_percent_s
) == 0)
3680 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3684 /* Don't crash on snprintf (str1, cst, "%s"). */
3688 tree orig_len
= get_maxval_strlen (orig
, SRK_STRLEN
);
3690 /* We could expand this as
3691 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
3693 memcpy (str1, str2_with_nul_at_cstm1, cst);
3694 but in the former case that might increase code size
3695 and in the latter case grow .rodata section too much.
3697 if (!known_lower (stmt
, orig_len
, destsize
, true))
3700 /* Convert snprintf (str1, cst, "%s", str2) into
3701 strcpy (str1, str2) if strlen (str2) < cst. */
3702 gimple_seq stmts
= NULL
;
3703 gimple
*repl
= gimple_build_call (fn
, 2, dest
, orig
);
3704 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3705 if (tree lhs
= gimple_call_lhs (stmt
))
3707 if (!useless_type_conversion_p (TREE_TYPE (lhs
),
3708 TREE_TYPE (orig_len
)))
3709 orig_len
= fold_convert (TREE_TYPE (lhs
), orig_len
);
3710 repl
= gimple_build_assign (lhs
, orig_len
);
3711 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3712 gsi_replace_with_seq_vops (gsi
, stmts
);
3713 /* gsi now points at the assignment to the lhs, get a
3714 stmt iterator to the memcpy call.
3715 ??? We can't use gsi_for_stmt as that doesn't work when the
3716 CFG isn't built yet. */
3717 gimple_stmt_iterator gsi2
= *gsi
;
3723 gsi_replace_with_seq_vops (gsi
, stmts
);
3731 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
3732 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
3733 more than 3 arguments, and ARG may be null in the 2-argument case.
3735 Return NULL_TREE if no simplification was possible, otherwise return the
3736 simplified form of the call as a tree. FCODE is the BUILT_IN_*
3737 code of the function to be simplified. */
3740 gimple_fold_builtin_fprintf (gimple_stmt_iterator
*gsi
,
3741 tree fp
, tree fmt
, tree arg
,
3742 enum built_in_function fcode
)
3744 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3745 tree fn_fputc
, fn_fputs
;
3746 const char *fmt_str
= NULL
;
3748 /* If the return value is used, don't do the transformation. */
3749 if (gimple_call_lhs (stmt
) != NULL_TREE
)
3752 /* Check whether the format is a literal string constant. */
3753 fmt_str
= c_getstr (fmt
);
3754 if (fmt_str
== NULL
)
3757 if (fcode
== BUILT_IN_FPRINTF_UNLOCKED
)
3759 /* If we're using an unlocked function, assume the other
3760 unlocked functions exist explicitly. */
3761 fn_fputc
= builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED
);
3762 fn_fputs
= builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED
);
3766 fn_fputc
= builtin_decl_implicit (BUILT_IN_FPUTC
);
3767 fn_fputs
= builtin_decl_implicit (BUILT_IN_FPUTS
);
3770 if (!init_target_chars ())
3773 /* If the format doesn't contain % args or %%, use strcpy. */
3774 if (strchr (fmt_str
, target_percent
) == NULL
)
3776 if (fcode
!= BUILT_IN_VFPRINTF
&& fcode
!= BUILT_IN_VFPRINTF_CHK
3780 /* If the format specifier was "", fprintf does nothing. */
3781 if (fmt_str
[0] == '\0')
3783 replace_call_with_value (gsi
, NULL_TREE
);
3787 /* When "string" doesn't contain %, replace all cases of
3788 fprintf (fp, string) with fputs (string, fp). The fputs
3789 builtin will take care of special cases like length == 1. */
3792 gcall
*repl
= gimple_build_call (fn_fputs
, 2, fmt
, fp
);
3793 replace_call_with_call_and_fold (gsi
, repl
);
3798 /* The other optimizations can be done only on the non-va_list variants. */
3799 else if (fcode
== BUILT_IN_VFPRINTF
|| fcode
== BUILT_IN_VFPRINTF_CHK
)
3802 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
3803 else if (strcmp (fmt_str
, target_percent_s
) == 0)
3805 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
3809 gcall
*repl
= gimple_build_call (fn_fputs
, 2, arg
, fp
);
3810 replace_call_with_call_and_fold (gsi
, repl
);
3815 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
3816 else if (strcmp (fmt_str
, target_percent_c
) == 0)
3819 || ! useless_type_conversion_p (integer_type_node
, TREE_TYPE (arg
)))
3823 gcall
*repl
= gimple_build_call (fn_fputc
, 2, arg
, fp
);
3824 replace_call_with_call_and_fold (gsi
, repl
);
3832 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
3833 FMT and ARG are the arguments to the call; we don't fold cases with
3834 more than 2 arguments, and ARG may be null if this is a 1-argument case.
3836 Return NULL_TREE if no simplification was possible, otherwise return the
3837 simplified form of the call as a tree. FCODE is the BUILT_IN_*
3838 code of the function to be simplified. */
3841 gimple_fold_builtin_printf (gimple_stmt_iterator
*gsi
, tree fmt
,
3842 tree arg
, enum built_in_function fcode
)
3844 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3845 tree fn_putchar
, fn_puts
, newarg
;
3846 const char *fmt_str
= NULL
;
3848 /* If the return value is used, don't do the transformation. */
3849 if (gimple_call_lhs (stmt
) != NULL_TREE
)
3852 /* Check whether the format is a literal string constant. */
3853 fmt_str
= c_getstr (fmt
);
3854 if (fmt_str
== NULL
)
3857 if (fcode
== BUILT_IN_PRINTF_UNLOCKED
)
3859 /* If we're using an unlocked function, assume the other
3860 unlocked functions exist explicitly. */
3861 fn_putchar
= builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED
);
3862 fn_puts
= builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED
);
3866 fn_putchar
= builtin_decl_implicit (BUILT_IN_PUTCHAR
);
3867 fn_puts
= builtin_decl_implicit (BUILT_IN_PUTS
);
3870 if (!init_target_chars ())
3873 if (strcmp (fmt_str
, target_percent_s
) == 0
3874 || strchr (fmt_str
, target_percent
) == NULL
)
3878 if (strcmp (fmt_str
, target_percent_s
) == 0)
3880 if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
3883 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
3886 str
= c_getstr (arg
);
3892 /* The format specifier doesn't contain any '%' characters. */
3893 if (fcode
!= BUILT_IN_VPRINTF
&& fcode
!= BUILT_IN_VPRINTF_CHK
3899 /* If the string was "", printf does nothing. */
3902 replace_call_with_value (gsi
, NULL_TREE
);
3906 /* If the string has length of 1, call putchar. */
3909 /* Given printf("c"), (where c is any one character,)
3910 convert "c"[0] to an int and pass that to the replacement
3912 newarg
= build_int_cst (integer_type_node
, str
[0]);
3915 gcall
*repl
= gimple_build_call (fn_putchar
, 1, newarg
);
3916 replace_call_with_call_and_fold (gsi
, repl
);
3922 /* If the string was "string\n", call puts("string"). */
3923 size_t len
= strlen (str
);
3924 if ((unsigned char)str
[len
- 1] == target_newline
3925 && (size_t) (int) len
== len
3930 /* Create a NUL-terminated string that's one char shorter
3931 than the original, stripping off the trailing '\n'. */
3932 newstr
= xstrdup (str
);
3933 newstr
[len
- 1] = '\0';
3934 newarg
= build_string_literal (len
, newstr
);
3938 gcall
*repl
= gimple_build_call (fn_puts
, 1, newarg
);
3939 replace_call_with_call_and_fold (gsi
, repl
);
3944 /* We'd like to arrange to call fputs(string,stdout) here,
3945 but we need stdout and don't have a way to get it yet. */
3950 /* The other optimizations can be done only on the non-va_list variants. */
3951 else if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
3954 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
3955 else if (strcmp (fmt_str
, target_percent_s_newline
) == 0)
3957 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
3961 gcall
*repl
= gimple_build_call (fn_puts
, 1, arg
);
3962 replace_call_with_call_and_fold (gsi
, repl
);
3967 /* If the format specifier was "%c", call __builtin_putchar(arg). */
3968 else if (strcmp (fmt_str
, target_percent_c
) == 0)
3970 if (!arg
|| ! useless_type_conversion_p (integer_type_node
,
3975 gcall
*repl
= gimple_build_call (fn_putchar
, 1, arg
);
3976 replace_call_with_call_and_fold (gsi
, repl
);
3986 /* Fold a call to __builtin_strlen with known length LEN. */
3989 gimple_fold_builtin_strlen (gimple_stmt_iterator
*gsi
)
3991 gimple
*stmt
= gsi_stmt (*gsi
);
3992 tree arg
= gimple_call_arg (stmt
, 0);
3997 c_strlen_data lendata
= { };
3998 if (get_range_strlen (arg
, &lendata
, /* eltsize = */ 1)
4000 && lendata
.minlen
&& TREE_CODE (lendata
.minlen
) == INTEGER_CST
4001 && lendata
.maxlen
&& TREE_CODE (lendata
.maxlen
) == INTEGER_CST
)
4003 /* The range of lengths refers to either a single constant
4004 string or to the longest and shortest constant string
4005 referenced by the argument of the strlen() call, or to
4006 the strings that can possibly be stored in the arrays
4007 the argument refers to. */
4008 minlen
= wi::to_wide (lendata
.minlen
);
4009 maxlen
= wi::to_wide (lendata
.maxlen
);
4013 unsigned prec
= TYPE_PRECISION (sizetype
);
4015 minlen
= wi::shwi (0, prec
);
4016 maxlen
= wi::to_wide (max_object_size (), prec
) - 2;
4019 if (minlen
== maxlen
)
4021 /* Fold the strlen call to a constant. */
4022 tree type
= TREE_TYPE (lendata
.minlen
);
4023 tree len
= force_gimple_operand_gsi (gsi
,
4024 wide_int_to_tree (type
, minlen
),
4025 true, NULL
, true, GSI_SAME_STMT
);
4026 replace_call_with_value (gsi
, len
);
4030 /* Set the strlen() range to [0, MAXLEN]. */
4031 if (tree lhs
= gimple_call_lhs (stmt
))
4032 set_strlen_range (lhs
, minlen
, maxlen
);
4037 /* Fold a call to __builtin_acc_on_device. */
4040 gimple_fold_builtin_acc_on_device (gimple_stmt_iterator
*gsi
, tree arg0
)
4042 /* Defer folding until we know which compiler we're in. */
4043 if (symtab
->state
!= EXPANSION
)
4046 unsigned val_host
= GOMP_DEVICE_HOST
;
4047 unsigned val_dev
= GOMP_DEVICE_NONE
;
4049 #ifdef ACCEL_COMPILER
4050 val_host
= GOMP_DEVICE_NOT_HOST
;
4051 val_dev
= ACCEL_COMPILER_acc_device
;
4054 location_t loc
= gimple_location (gsi_stmt (*gsi
));
4056 tree host_eq
= make_ssa_name (boolean_type_node
);
4057 gimple
*host_ass
= gimple_build_assign
4058 (host_eq
, EQ_EXPR
, arg0
, build_int_cst (TREE_TYPE (arg0
), val_host
));
4059 gimple_set_location (host_ass
, loc
);
4060 gsi_insert_before (gsi
, host_ass
, GSI_SAME_STMT
);
4062 tree dev_eq
= make_ssa_name (boolean_type_node
);
4063 gimple
*dev_ass
= gimple_build_assign
4064 (dev_eq
, EQ_EXPR
, arg0
, build_int_cst (TREE_TYPE (arg0
), val_dev
));
4065 gimple_set_location (dev_ass
, loc
);
4066 gsi_insert_before (gsi
, dev_ass
, GSI_SAME_STMT
);
4068 tree result
= make_ssa_name (boolean_type_node
);
4069 gimple
*result_ass
= gimple_build_assign
4070 (result
, BIT_IOR_EXPR
, host_eq
, dev_eq
);
4071 gimple_set_location (result_ass
, loc
);
4072 gsi_insert_before (gsi
, result_ass
, GSI_SAME_STMT
);
4074 replace_call_with_value (gsi
, result
);
4079 /* Fold realloc (0, n) -> malloc (n). */
4082 gimple_fold_builtin_realloc (gimple_stmt_iterator
*gsi
)
4084 gimple
*stmt
= gsi_stmt (*gsi
);
4085 tree arg
= gimple_call_arg (stmt
, 0);
4086 tree size
= gimple_call_arg (stmt
, 1);
4088 if (operand_equal_p (arg
, null_pointer_node
, 0))
4090 tree fn_malloc
= builtin_decl_implicit (BUILT_IN_MALLOC
);
4093 gcall
*repl
= gimple_build_call (fn_malloc
, 1, size
);
4094 replace_call_with_call_and_fold (gsi
, repl
);
4101 /* Number of bytes into which any type but aggregate or vector types
4103 static constexpr size_t clear_padding_unit
4104 = MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
;
4105 /* Buffer size on which __builtin_clear_padding folding code works. */
4106 static const size_t clear_padding_buf_size
= 32 * clear_padding_unit
;
4108 /* Data passed through __builtin_clear_padding folding. */
4109 struct clear_padding_struct
{
4111 /* 0 during __builtin_clear_padding folding, nonzero during
4112 clear_type_padding_in_mask. In that case, instead of clearing the
4113 non-padding bits in union_ptr array clear the padding bits in there. */
4117 gimple_stmt_iterator
*gsi
;
4118 /* Alignment of buf->base + 0. */
4120 /* Offset from buf->base. Should be always a multiple of UNITS_PER_WORD. */
4122 /* Number of padding bytes before buf->off that don't have padding clear
4123 code emitted yet. */
4124 HOST_WIDE_INT padding_bytes
;
4125 /* The size of the whole object. Never emit code to touch
4126 buf->base + buf->sz or following bytes. */
4128 /* Number of bytes recorded in buf->buf. */
4130 /* When inside union, instead of emitting code we and bits inside of
4131 the union_ptr array. */
4132 unsigned char *union_ptr
;
4133 /* Set bits mean padding bits that need to be cleared by the builtin. */
4134 unsigned char buf
[clear_padding_buf_size
+ clear_padding_unit
];
4137 /* Emit code to clear padding requested in BUF->buf - set bits
4138 in there stand for padding that should be cleared. FULL is true
4139 if everything from the buffer should be flushed, otherwise
4140 it can leave up to 2 * clear_padding_unit bytes for further
4144 clear_padding_flush (clear_padding_struct
*buf
, bool full
)
4146 gcc_assert ((clear_padding_unit
% UNITS_PER_WORD
) == 0);
4147 if (!full
&& buf
->size
< 2 * clear_padding_unit
)
4149 gcc_assert ((buf
->off
% UNITS_PER_WORD
) == 0);
4150 size_t end
= buf
->size
;
4152 end
= ((end
- clear_padding_unit
- 1) / clear_padding_unit
4153 * clear_padding_unit
);
4154 size_t padding_bytes
= buf
->padding_bytes
;
4157 if (buf
->clear_in_mask
)
4159 /* During clear_type_padding_in_mask, clear the padding
4160 bits set in buf->buf in the buf->union_ptr mask. */
4161 for (size_t i
= 0; i
< end
; i
++)
4163 if (buf
->buf
[i
] == (unsigned char) ~0)
4167 memset (&buf
->union_ptr
[buf
->off
+ i
- padding_bytes
],
4170 buf
->union_ptr
[buf
->off
+ i
] &= ~buf
->buf
[i
];
4175 memset (&buf
->union_ptr
[buf
->off
+ end
- padding_bytes
],
4179 buf
->padding_bytes
= 0;
4183 memmove (buf
->buf
, buf
->buf
+ end
, buf
->size
- end
);
4186 buf
->padding_bytes
= padding_bytes
;
4190 /* Inside of a union, instead of emitting any code, instead
4191 clear all bits in the union_ptr buffer that are clear
4192 in buf. Whole padding bytes don't clear anything. */
4193 for (size_t i
= 0; i
< end
; i
++)
4195 if (buf
->buf
[i
] == (unsigned char) ~0)
4200 buf
->union_ptr
[buf
->off
+ i
] &= buf
->buf
[i
];
4207 buf
->padding_bytes
= 0;
4211 memmove (buf
->buf
, buf
->buf
+ end
, buf
->size
- end
);
4214 buf
->padding_bytes
= padding_bytes
;
4218 size_t wordsize
= UNITS_PER_WORD
;
4219 for (size_t i
= 0; i
< end
; i
+= wordsize
)
4221 size_t nonzero_first
= wordsize
;
4222 size_t nonzero_last
= 0;
4223 size_t zero_first
= wordsize
;
4224 size_t zero_last
= 0;
4225 bool all_ones
= true, bytes_only
= true;
4226 if ((unsigned HOST_WIDE_INT
) (buf
->off
+ i
+ wordsize
)
4227 > (unsigned HOST_WIDE_INT
) buf
->sz
)
4229 gcc_assert (wordsize
> 1);
4234 for (size_t j
= i
; j
< i
+ wordsize
&& j
< end
; j
++)
4238 if (nonzero_first
== wordsize
)
4240 nonzero_first
= j
- i
;
4241 nonzero_last
= j
- i
;
4243 if (nonzero_last
!= j
- i
)
4245 nonzero_last
= j
+ 1 - i
;
4249 if (zero_first
== wordsize
)
4251 zero_last
= j
+ 1 - i
;
4253 if (buf
->buf
[j
] != 0 && buf
->buf
[j
] != (unsigned char) ~0)
4259 size_t padding_end
= i
;
4262 if (nonzero_first
== 0
4263 && nonzero_last
== wordsize
4266 /* All bits are padding and we had some padding
4267 before too. Just extend it. */
4268 padding_bytes
+= wordsize
;
4271 if (all_ones
&& nonzero_first
== 0)
4273 padding_bytes
+= nonzero_last
;
4274 padding_end
+= nonzero_last
;
4275 nonzero_first
= wordsize
;
4278 else if (bytes_only
&& nonzero_first
== 0)
4280 gcc_assert (zero_first
&& zero_first
!= wordsize
);
4281 padding_bytes
+= zero_first
;
4282 padding_end
+= zero_first
;
4285 if (padding_bytes
== 1)
4287 atype
= char_type_node
;
4288 src
= build_zero_cst (char_type_node
);
4292 atype
= build_array_type_nelts (char_type_node
, padding_bytes
);
4293 src
= build_constructor (atype
, NULL
);
4295 tree dst
= build2_loc (buf
->loc
, MEM_REF
, atype
, buf
->base
,
4296 build_int_cst (buf
->alias_type
,
4297 buf
->off
+ padding_end
4299 gimple
*g
= gimple_build_assign (dst
, src
);
4300 gimple_set_location (g
, buf
->loc
);
4301 gsi_insert_before (buf
->gsi
, g
, GSI_SAME_STMT
);
4303 buf
->padding_bytes
= 0;
4305 if (nonzero_first
== wordsize
)
4306 /* All bits in a word are 0, there are no padding bits. */
4308 if (all_ones
&& nonzero_last
== wordsize
)
4310 /* All bits between nonzero_first and end of word are padding
4311 bits, start counting padding_bytes. */
4312 padding_bytes
= nonzero_last
- nonzero_first
;
4317 /* If bitfields aren't involved in this word, prefer storing
4318 individual bytes or groups of them over performing a RMW
4319 operation on the whole word. */
4320 gcc_assert (i
+ zero_last
<= end
);
4321 for (size_t j
= padding_end
; j
< i
+ zero_last
; j
++)
4326 for (k
= j
; k
< i
+ zero_last
; k
++)
4327 if (buf
->buf
[k
] == 0)
4329 HOST_WIDE_INT off
= buf
->off
+ j
;
4333 atype
= char_type_node
;
4334 src
= build_zero_cst (char_type_node
);
4338 atype
= build_array_type_nelts (char_type_node
, k
- j
);
4339 src
= build_constructor (atype
, NULL
);
4341 tree dst
= build2_loc (buf
->loc
, MEM_REF
, atype
,
4343 build_int_cst (buf
->alias_type
, off
));
4344 gimple
*g
= gimple_build_assign (dst
, src
);
4345 gimple_set_location (g
, buf
->loc
);
4346 gsi_insert_before (buf
->gsi
, g
, GSI_SAME_STMT
);
4350 if (nonzero_last
== wordsize
)
4351 padding_bytes
= nonzero_last
- zero_last
;
4354 for (size_t eltsz
= 1; eltsz
<= wordsize
; eltsz
<<= 1)
4356 if (nonzero_last
- nonzero_first
<= eltsz
4357 && ((nonzero_first
& ~(eltsz
- 1))
4358 == ((nonzero_last
- 1) & ~(eltsz
- 1))))
4362 type
= char_type_node
;
4364 type
= lang_hooks
.types
.type_for_size (eltsz
* BITS_PER_UNIT
,
4366 size_t start
= nonzero_first
& ~(eltsz
- 1);
4367 HOST_WIDE_INT off
= buf
->off
+ i
+ start
;
4369 if (eltsz
> 1 && buf
->align
< TYPE_ALIGN (type
))
4370 atype
= build_aligned_type (type
, buf
->align
);
4371 tree dst
= build2_loc (buf
->loc
, MEM_REF
, atype
, buf
->base
,
4372 build_int_cst (buf
->alias_type
, off
));
4376 && nonzero_first
== start
4377 && nonzero_last
== start
+ eltsz
)
4378 src
= build_zero_cst (type
);
4381 src
= make_ssa_name (type
);
4382 tree tmp_dst
= unshare_expr (dst
);
4383 /* The folding introduces a read from the tmp_dst, we should
4384 prevent uninitialized warning analysis from issuing warning
4385 for such fake read. In order to suppress warning only for
4386 this expr, we should set the location of tmp_dst to
4387 UNKNOWN_LOCATION first, then suppress_warning will call
4388 set_no_warning_bit to set the no_warning flag only for
4390 SET_EXPR_LOCATION (tmp_dst
, UNKNOWN_LOCATION
);
4391 suppress_warning (tmp_dst
, OPT_Wuninitialized
);
4392 g
= gimple_build_assign (src
, tmp_dst
);
4393 gimple_set_location (g
, buf
->loc
);
4394 gsi_insert_before (buf
->gsi
, g
, GSI_SAME_STMT
);
4395 tree mask
= native_interpret_expr (type
,
4396 buf
->buf
+ i
+ start
,
4398 gcc_assert (mask
&& TREE_CODE (mask
) == INTEGER_CST
);
4399 mask
= fold_build1 (BIT_NOT_EXPR
, type
, mask
);
4400 tree src_masked
= make_ssa_name (type
);
4401 g
= gimple_build_assign (src_masked
, BIT_AND_EXPR
,
4403 gimple_set_location (g
, buf
->loc
);
4404 gsi_insert_before (buf
->gsi
, g
, GSI_SAME_STMT
);
4407 g
= gimple_build_assign (dst
, src
);
4408 gimple_set_location (g
, buf
->loc
);
4409 gsi_insert_before (buf
->gsi
, g
, GSI_SAME_STMT
);
4419 if (padding_bytes
== 1)
4421 atype
= char_type_node
;
4422 src
= build_zero_cst (char_type_node
);
4426 atype
= build_array_type_nelts (char_type_node
, padding_bytes
);
4427 src
= build_constructor (atype
, NULL
);
4429 tree dst
= build2_loc (buf
->loc
, MEM_REF
, atype
, buf
->base
,
4430 build_int_cst (buf
->alias_type
,
4433 gimple
*g
= gimple_build_assign (dst
, src
);
4434 gimple_set_location (g
, buf
->loc
);
4435 gsi_insert_before (buf
->gsi
, g
, GSI_SAME_STMT
);
4437 size_t end_rem
= end
% UNITS_PER_WORD
;
4438 buf
->off
+= end
- end_rem
;
4439 buf
->size
= end_rem
;
4440 memset (buf
->buf
, 0, buf
->size
);
4441 buf
->padding_bytes
= 0;
4445 memmove (buf
->buf
, buf
->buf
+ end
, buf
->size
- end
);
4448 buf
->padding_bytes
= padding_bytes
;
4452 /* Append PADDING_BYTES padding bytes. */
4455 clear_padding_add_padding (clear_padding_struct
*buf
,
4456 HOST_WIDE_INT padding_bytes
)
4458 if (padding_bytes
== 0)
4460 if ((unsigned HOST_WIDE_INT
) padding_bytes
+ buf
->size
4461 > (unsigned HOST_WIDE_INT
) clear_padding_buf_size
)
4462 clear_padding_flush (buf
, false);
4463 if ((unsigned HOST_WIDE_INT
) padding_bytes
+ buf
->size
4464 > (unsigned HOST_WIDE_INT
) clear_padding_buf_size
)
4466 memset (buf
->buf
+ buf
->size
, ~0, clear_padding_buf_size
- buf
->size
);
4467 padding_bytes
-= clear_padding_buf_size
- buf
->size
;
4468 buf
->size
= clear_padding_buf_size
;
4469 clear_padding_flush (buf
, false);
4470 gcc_assert (buf
->padding_bytes
);
4471 /* At this point buf->buf[0] through buf->buf[buf->size - 1]
4472 is guaranteed to be all ones. */
4473 padding_bytes
+= buf
->size
;
4474 buf
->size
= padding_bytes
% UNITS_PER_WORD
;
4475 memset (buf
->buf
, ~0, buf
->size
);
4476 buf
->off
+= padding_bytes
- buf
->size
;
4477 buf
->padding_bytes
+= padding_bytes
- buf
->size
;
4481 memset (buf
->buf
+ buf
->size
, ~0, padding_bytes
);
4482 buf
->size
+= padding_bytes
;
4486 static void clear_padding_type (clear_padding_struct
*, tree
,
4487 HOST_WIDE_INT
, bool);
4489 /* Clear padding bits of union type TYPE. */
4492 clear_padding_union (clear_padding_struct
*buf
, tree type
,
4493 HOST_WIDE_INT sz
, bool for_auto_init
)
4495 clear_padding_struct
*union_buf
;
4496 HOST_WIDE_INT start_off
= 0, next_off
= 0;
4497 size_t start_size
= 0;
4500 start_off
= buf
->off
+ buf
->size
;
4501 next_off
= start_off
+ sz
;
4502 start_size
= start_off
% UNITS_PER_WORD
;
4503 start_off
-= start_size
;
4504 clear_padding_flush (buf
, true);
4509 if (sz
+ buf
->size
> clear_padding_buf_size
)
4510 clear_padding_flush (buf
, false);
4511 union_buf
= XALLOCA (clear_padding_struct
);
4512 union_buf
->loc
= buf
->loc
;
4513 union_buf
->clear_in_mask
= buf
->clear_in_mask
;
4514 union_buf
->base
= NULL_TREE
;
4515 union_buf
->alias_type
= NULL_TREE
;
4516 union_buf
->gsi
= NULL
;
4517 union_buf
->align
= 0;
4519 union_buf
->padding_bytes
= 0;
4521 union_buf
->size
= 0;
4522 if (sz
+ buf
->size
<= clear_padding_buf_size
)
4523 union_buf
->union_ptr
= buf
->buf
+ buf
->size
;
4525 union_buf
->union_ptr
= XNEWVEC (unsigned char, sz
);
4526 memset (union_buf
->union_ptr
, ~0, sz
);
4529 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
4530 if (TREE_CODE (field
) == FIELD_DECL
&& !DECL_PADDING_P (field
))
4532 if (DECL_SIZE_UNIT (field
) == NULL_TREE
)
4534 if (TREE_TYPE (field
) == error_mark_node
)
4536 gcc_assert (TREE_CODE (TREE_TYPE (field
)) == ARRAY_TYPE
4537 && !COMPLETE_TYPE_P (TREE_TYPE (field
)));
4538 if (!buf
->clear_in_mask
&& !for_auto_init
)
4539 error_at (buf
->loc
, "flexible array member %qD does not have "
4540 "well defined padding bits for %qs",
4541 field
, "__builtin_clear_padding");
4544 HOST_WIDE_INT fldsz
= tree_to_shwi (DECL_SIZE_UNIT (field
));
4545 gcc_assert (union_buf
->size
== 0);
4546 union_buf
->off
= start_off
;
4547 union_buf
->size
= start_size
;
4548 memset (union_buf
->buf
, ~0, start_size
);
4549 clear_padding_type (union_buf
, TREE_TYPE (field
), fldsz
, for_auto_init
);
4550 clear_padding_add_padding (union_buf
, sz
- fldsz
);
4551 clear_padding_flush (union_buf
, true);
4554 if (buf
== union_buf
)
4556 buf
->off
= next_off
;
4557 buf
->size
= next_off
% UNITS_PER_WORD
;
4558 buf
->off
-= buf
->size
;
4559 memset (buf
->buf
, ~0, buf
->size
);
4561 else if (sz
+ buf
->size
<= clear_padding_buf_size
)
4565 unsigned char *union_ptr
= union_buf
->union_ptr
;
4568 clear_padding_flush (buf
, false);
4569 HOST_WIDE_INT this_sz
4570 = MIN ((unsigned HOST_WIDE_INT
) sz
,
4571 clear_padding_buf_size
- buf
->size
);
4572 memcpy (buf
->buf
+ buf
->size
, union_ptr
, this_sz
);
4573 buf
->size
+= this_sz
;
4574 union_ptr
+= this_sz
;
4577 XDELETE (union_buf
->union_ptr
);
4581 /* The only known floating point formats with padding bits are the
4582 IEEE extended ones. */
4585 clear_padding_real_needs_padding_p (tree type
)
4587 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
4589 && fmt
->signbit_ro
== fmt
->signbit_rw
4590 && (fmt
->signbit_ro
== 79 || fmt
->signbit_ro
== 95));
4593 /* Return true if TYPE might contain any padding bits. */
4596 clear_padding_type_may_have_padding_p (tree type
)
4598 switch (TREE_CODE (type
))
4606 return clear_padding_type_may_have_padding_p (TREE_TYPE (type
));
4608 return clear_padding_real_needs_padding_p (type
);
4614 /* Emit a runtime loop:
4615 for (; buf.base != end; buf.base += sz)
4616 __builtin_clear_padding (buf.base); */
4619 clear_padding_emit_loop (clear_padding_struct
*buf
, tree type
,
4620 tree end
, bool for_auto_init
)
4622 tree l1
= create_artificial_label (buf
->loc
);
4623 tree l2
= create_artificial_label (buf
->loc
);
4624 tree l3
= create_artificial_label (buf
->loc
);
4625 gimple
*g
= gimple_build_goto (l2
);
4626 gimple_set_location (g
, buf
->loc
);
4627 gsi_insert_before (buf
->gsi
, g
, GSI_SAME_STMT
);
4628 g
= gimple_build_label (l1
);
4629 gimple_set_location (g
, buf
->loc
);
4630 gsi_insert_before (buf
->gsi
, g
, GSI_SAME_STMT
);
4631 clear_padding_type (buf
, type
, buf
->sz
, for_auto_init
);
4632 clear_padding_flush (buf
, true);
4633 g
= gimple_build_assign (buf
->base
, POINTER_PLUS_EXPR
, buf
->base
,
4634 size_int (buf
->sz
));
4635 gimple_set_location (g
, buf
->loc
);
4636 gsi_insert_before (buf
->gsi
, g
, GSI_SAME_STMT
);
4637 g
= gimple_build_label (l2
);
4638 gimple_set_location (g
, buf
->loc
);
4639 gsi_insert_before (buf
->gsi
, g
, GSI_SAME_STMT
);
4640 g
= gimple_build_cond (NE_EXPR
, buf
->base
, end
, l1
, l3
);
4641 gimple_set_location (g
, buf
->loc
);
4642 gsi_insert_before (buf
->gsi
, g
, GSI_SAME_STMT
);
4643 g
= gimple_build_label (l3
);
4644 gimple_set_location (g
, buf
->loc
);
4645 gsi_insert_before (buf
->gsi
, g
, GSI_SAME_STMT
);
4648 /* Clear padding bits for TYPE. Called recursively from
4649 gimple_fold_builtin_clear_padding. If FOR_AUTO_INIT is true,
4650 the __builtin_clear_padding is not called by the end user,
4651 instead, it's inserted by the compiler to initialize the
4652 paddings of automatic variable. Therefore, we should not
4653 emit the error messages for flexible array members to confuse
4657 clear_padding_type (clear_padding_struct
*buf
, tree type
,
4658 HOST_WIDE_INT sz
, bool for_auto_init
)
4660 switch (TREE_CODE (type
))
4663 HOST_WIDE_INT cur_pos
;
4665 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
4666 if (TREE_CODE (field
) == FIELD_DECL
&& !DECL_PADDING_P (field
))
4668 tree ftype
= TREE_TYPE (field
);
4669 if (DECL_BIT_FIELD (field
))
4671 HOST_WIDE_INT fldsz
= TYPE_PRECISION (ftype
);
4674 HOST_WIDE_INT pos
= int_byte_position (field
);
4678 = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field
));
4679 bpos
%= BITS_PER_UNIT
;
4681 = ROUND_UP (bpos
+ fldsz
, BITS_PER_UNIT
) / BITS_PER_UNIT
;
4682 if (pos
+ end
> cur_pos
)
4684 clear_padding_add_padding (buf
, pos
+ end
- cur_pos
);
4685 cur_pos
= pos
+ end
;
4687 gcc_assert (cur_pos
> pos
4688 && ((unsigned HOST_WIDE_INT
) buf
->size
4689 >= (unsigned HOST_WIDE_INT
) cur_pos
- pos
));
4690 unsigned char *p
= buf
->buf
+ buf
->size
- (cur_pos
- pos
);
4691 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
)
4692 sorry_at (buf
->loc
, "PDP11 bit-field handling unsupported"
4693 " in %qs", "__builtin_clear_padding");
4694 else if (BYTES_BIG_ENDIAN
)
4697 if (bpos
+ fldsz
<= BITS_PER_UNIT
)
4698 *p
&= ~(((1 << fldsz
) - 1)
4699 << (BITS_PER_UNIT
- bpos
- fldsz
));
4704 *p
&= ~(((1U << BITS_PER_UNIT
) - 1) >> bpos
);
4706 fldsz
-= BITS_PER_UNIT
- bpos
;
4708 memset (p
, 0, fldsz
/ BITS_PER_UNIT
);
4709 p
+= fldsz
/ BITS_PER_UNIT
;
4710 fldsz
%= BITS_PER_UNIT
;
4712 *p
&= ((1U << BITS_PER_UNIT
) - 1) >> fldsz
;
4717 /* Little endian. */
4718 if (bpos
+ fldsz
<= BITS_PER_UNIT
)
4719 *p
&= ~(((1 << fldsz
) - 1) << bpos
);
4724 *p
&= ~(((1 << BITS_PER_UNIT
) - 1) << bpos
);
4726 fldsz
-= BITS_PER_UNIT
- bpos
;
4728 memset (p
, 0, fldsz
/ BITS_PER_UNIT
);
4729 p
+= fldsz
/ BITS_PER_UNIT
;
4730 fldsz
%= BITS_PER_UNIT
;
4732 *p
&= ~((1 << fldsz
) - 1);
4736 else if (DECL_SIZE_UNIT (field
) == NULL_TREE
)
4738 if (ftype
== error_mark_node
)
4740 gcc_assert (TREE_CODE (ftype
) == ARRAY_TYPE
4741 && !COMPLETE_TYPE_P (ftype
));
4742 if (!buf
->clear_in_mask
&& !for_auto_init
)
4743 error_at (buf
->loc
, "flexible array member %qD does not "
4744 "have well defined padding bits for %qs",
4745 field
, "__builtin_clear_padding");
4747 else if (is_empty_type (TREE_TYPE (field
)))
4751 HOST_WIDE_INT pos
= int_byte_position (field
);
4754 HOST_WIDE_INT fldsz
= tree_to_shwi (DECL_SIZE_UNIT (field
));
4755 gcc_assert (pos
>= 0 && fldsz
>= 0 && pos
>= cur_pos
);
4756 clear_padding_add_padding (buf
, pos
- cur_pos
);
4758 clear_padding_type (buf
, TREE_TYPE (field
),
4759 fldsz
, for_auto_init
);
4763 gcc_assert (sz
>= cur_pos
);
4764 clear_padding_add_padding (buf
, sz
- cur_pos
);
4767 HOST_WIDE_INT nelts
, fldsz
;
4768 fldsz
= int_size_in_bytes (TREE_TYPE (type
));
4773 && sz
> 8 * UNITS_PER_WORD
4774 && buf
->union_ptr
== NULL
4775 && clear_padding_type_may_have_padding_p (TREE_TYPE (type
)))
4777 /* For sufficiently large array of more than one elements,
4778 emit a runtime loop to keep code size manageable. */
4779 tree base
= buf
->base
;
4780 unsigned int prev_align
= buf
->align
;
4781 HOST_WIDE_INT off
= buf
->off
+ buf
->size
;
4782 HOST_WIDE_INT prev_sz
= buf
->sz
;
4783 clear_padding_flush (buf
, true);
4784 tree elttype
= TREE_TYPE (type
);
4785 buf
->base
= create_tmp_var (build_pointer_type (elttype
));
4786 tree end
= make_ssa_name (TREE_TYPE (buf
->base
));
4787 gimple
*g
= gimple_build_assign (buf
->base
, POINTER_PLUS_EXPR
,
4788 base
, size_int (off
));
4789 gimple_set_location (g
, buf
->loc
);
4790 gsi_insert_before (buf
->gsi
, g
, GSI_SAME_STMT
);
4791 g
= gimple_build_assign (end
, POINTER_PLUS_EXPR
, buf
->base
,
4793 gimple_set_location (g
, buf
->loc
);
4794 gsi_insert_before (buf
->gsi
, g
, GSI_SAME_STMT
);
4796 buf
->align
= TYPE_ALIGN (elttype
);
4799 clear_padding_emit_loop (buf
, elttype
, end
, for_auto_init
);
4802 buf
->align
= prev_align
;
4803 buf
->size
= off
% UNITS_PER_WORD
;
4804 buf
->off
= off
- buf
->size
;
4805 memset (buf
->buf
, 0, buf
->size
);
4808 for (HOST_WIDE_INT i
= 0; i
< nelts
; i
++)
4809 clear_padding_type (buf
, TREE_TYPE (type
), fldsz
, for_auto_init
);
4812 clear_padding_union (buf
, type
, sz
, for_auto_init
);
4815 gcc_assert ((size_t) sz
<= clear_padding_unit
);
4816 if ((unsigned HOST_WIDE_INT
) sz
+ buf
->size
> clear_padding_buf_size
)
4817 clear_padding_flush (buf
, false);
4818 if (clear_padding_real_needs_padding_p (type
))
4820 /* Use native_interpret_real + native_encode_expr to figure out
4821 which bits are padding. */
4822 memset (buf
->buf
+ buf
->size
, ~0, sz
);
4823 tree cst
= native_interpret_real (type
, buf
->buf
+ buf
->size
, sz
);
4824 gcc_assert (cst
&& TREE_CODE (cst
) == REAL_CST
);
4825 int len
= native_encode_expr (cst
, buf
->buf
+ buf
->size
, sz
);
4826 gcc_assert (len
> 0 && (size_t) len
== (size_t) sz
);
4827 for (size_t i
= 0; i
< (size_t) sz
; i
++)
4828 buf
->buf
[buf
->size
+ i
] ^= ~0;
4831 memset (buf
->buf
+ buf
->size
, 0, sz
);
4835 fldsz
= int_size_in_bytes (TREE_TYPE (type
));
4836 clear_padding_type (buf
, TREE_TYPE (type
), fldsz
, for_auto_init
);
4837 clear_padding_type (buf
, TREE_TYPE (type
), fldsz
, for_auto_init
);
4840 nelts
= TYPE_VECTOR_SUBPARTS (type
).to_constant ();
4841 fldsz
= int_size_in_bytes (TREE_TYPE (type
));
4842 for (HOST_WIDE_INT i
= 0; i
< nelts
; i
++)
4843 clear_padding_type (buf
, TREE_TYPE (type
), fldsz
, for_auto_init
);
4846 gcc_assert ((size_t) sz
<= clear_padding_unit
);
4847 if ((unsigned HOST_WIDE_INT
) sz
+ buf
->size
> clear_padding_buf_size
)
4848 clear_padding_flush (buf
, false);
4849 memset (buf
->buf
+ buf
->size
, ~0, sz
);
4853 gcc_assert ((size_t) sz
<= clear_padding_unit
);
4854 if ((unsigned HOST_WIDE_INT
) sz
+ buf
->size
> clear_padding_buf_size
)
4855 clear_padding_flush (buf
, false);
4856 memset (buf
->buf
+ buf
->size
, 0, sz
);
4862 /* Clear padding bits of TYPE in MASK. */
4865 clear_type_padding_in_mask (tree type
, unsigned char *mask
)
4867 clear_padding_struct buf
;
4868 buf
.loc
= UNKNOWN_LOCATION
;
4869 buf
.clear_in_mask
= true;
4870 buf
.base
= NULL_TREE
;
4871 buf
.alias_type
= NULL_TREE
;
4875 buf
.padding_bytes
= 0;
4876 buf
.sz
= int_size_in_bytes (type
);
4878 buf
.union_ptr
= mask
;
4879 clear_padding_type (&buf
, type
, buf
.sz
, false);
4880 clear_padding_flush (&buf
, true);
4883 /* Fold __builtin_clear_padding builtin. */
4886 gimple_fold_builtin_clear_padding (gimple_stmt_iterator
*gsi
)
4888 gimple
*stmt
= gsi_stmt (*gsi
);
4889 gcc_assert (gimple_call_num_args (stmt
) == 2);
4890 tree ptr
= gimple_call_arg (stmt
, 0);
4891 tree typearg
= gimple_call_arg (stmt
, 1);
4892 /* The 2nd argument of __builtin_clear_padding's value is used to
4893 distinguish whether this call is made by the user or by the compiler
4894 for automatic variable initialization. */
4895 bool for_auto_init
= (bool) TREE_INT_CST_LOW (typearg
);
4896 tree type
= TREE_TYPE (TREE_TYPE (typearg
));
4897 location_t loc
= gimple_location (stmt
);
4898 clear_padding_struct buf
;
4899 gimple_stmt_iterator gsiprev
= *gsi
;
4900 /* This should be folded during the lower pass. */
4901 gcc_assert (!gimple_in_ssa_p (cfun
) && cfun
->cfg
== NULL
);
4902 gcc_assert (COMPLETE_TYPE_P (type
));
4903 gsi_prev (&gsiprev
);
4906 buf
.clear_in_mask
= false;
4908 buf
.alias_type
= NULL_TREE
;
4910 buf
.align
= get_pointer_alignment (ptr
);
4911 unsigned int talign
= min_align_of_type (type
) * BITS_PER_UNIT
;
4912 buf
.align
= MAX (buf
.align
, talign
);
4914 buf
.padding_bytes
= 0;
4916 buf
.sz
= int_size_in_bytes (type
);
4917 buf
.union_ptr
= NULL
;
4918 if (buf
.sz
< 0 && int_size_in_bytes (strip_array_types (type
)) < 0)
4919 sorry_at (loc
, "%s not supported for variable length aggregates",
4920 "__builtin_clear_padding");
4921 /* The implementation currently assumes 8-bit host and target
4922 chars which is the case for all currently supported targets
4923 and hosts and is required e.g. for native_{encode,interpret}* APIs. */
4924 else if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8)
4925 sorry_at (loc
, "%s not supported on this target",
4926 "__builtin_clear_padding");
4927 else if (!clear_padding_type_may_have_padding_p (type
))
4929 else if (TREE_CODE (type
) == ARRAY_TYPE
&& buf
.sz
< 0)
4931 tree sz
= TYPE_SIZE_UNIT (type
);
4932 tree elttype
= type
;
4933 /* Only supports C/C++ VLAs and flattens all the VLA levels. */
4934 while (TREE_CODE (elttype
) == ARRAY_TYPE
4935 && int_size_in_bytes (elttype
) < 0)
4936 elttype
= TREE_TYPE (elttype
);
4937 HOST_WIDE_INT eltsz
= int_size_in_bytes (elttype
);
4938 gcc_assert (eltsz
>= 0);
4941 buf
.base
= create_tmp_var (build_pointer_type (elttype
));
4942 tree end
= make_ssa_name (TREE_TYPE (buf
.base
));
4943 gimple
*g
= gimple_build_assign (buf
.base
, ptr
);
4944 gimple_set_location (g
, loc
);
4945 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4946 g
= gimple_build_assign (end
, POINTER_PLUS_EXPR
, buf
.base
, sz
);
4947 gimple_set_location (g
, loc
);
4948 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4950 buf
.align
= TYPE_ALIGN (elttype
);
4951 buf
.alias_type
= build_pointer_type (elttype
);
4952 clear_padding_emit_loop (&buf
, elttype
, end
, for_auto_init
);
4957 if (!is_gimple_mem_ref_addr (buf
.base
))
4959 buf
.base
= make_ssa_name (TREE_TYPE (ptr
));
4960 gimple
*g
= gimple_build_assign (buf
.base
, ptr
);
4961 gimple_set_location (g
, loc
);
4962 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4964 buf
.alias_type
= build_pointer_type (type
);
4965 clear_padding_type (&buf
, type
, buf
.sz
, for_auto_init
);
4966 clear_padding_flush (&buf
, true);
4969 gimple_stmt_iterator gsiprev2
= *gsi
;
4970 gsi_prev (&gsiprev2
);
4971 if (gsi_stmt (gsiprev
) == gsi_stmt (gsiprev2
))
4972 gsi_replace (gsi
, gimple_build_nop (), true);
4975 gsi_remove (gsi
, true);
4981 /* Fold the non-target builtin at *GSI and return whether any simplification
4985 gimple_fold_builtin (gimple_stmt_iterator
*gsi
)
4987 gcall
*stmt
= as_a
<gcall
*>(gsi_stmt (*gsi
));
4988 tree callee
= gimple_call_fndecl (stmt
);
4990 /* Give up for always_inline inline builtins until they are
4992 if (avoid_folding_inline_builtin (callee
))
4995 unsigned n
= gimple_call_num_args (stmt
);
4996 enum built_in_function fcode
= DECL_FUNCTION_CODE (callee
);
5000 return gimple_fold_builtin_bcmp (gsi
);
5001 case BUILT_IN_BCOPY
:
5002 return gimple_fold_builtin_bcopy (gsi
);
5003 case BUILT_IN_BZERO
:
5004 return gimple_fold_builtin_bzero (gsi
);
5006 case BUILT_IN_MEMSET
:
5007 return gimple_fold_builtin_memset (gsi
,
5008 gimple_call_arg (stmt
, 1),
5009 gimple_call_arg (stmt
, 2));
5010 case BUILT_IN_MEMCPY
:
5011 case BUILT_IN_MEMPCPY
:
5012 case BUILT_IN_MEMMOVE
:
5013 return gimple_fold_builtin_memory_op (gsi
, gimple_call_arg (stmt
, 0),
5014 gimple_call_arg (stmt
, 1), fcode
);
5015 case BUILT_IN_SPRINTF_CHK
:
5016 case BUILT_IN_VSPRINTF_CHK
:
5017 return gimple_fold_builtin_sprintf_chk (gsi
, fcode
);
5018 case BUILT_IN_STRCAT_CHK
:
5019 return gimple_fold_builtin_strcat_chk (gsi
);
5020 case BUILT_IN_STRNCAT_CHK
:
5021 return gimple_fold_builtin_strncat_chk (gsi
);
5022 case BUILT_IN_STRLEN
:
5023 return gimple_fold_builtin_strlen (gsi
);
5024 case BUILT_IN_STRCPY
:
5025 return gimple_fold_builtin_strcpy (gsi
,
5026 gimple_call_arg (stmt
, 0),
5027 gimple_call_arg (stmt
, 1));
5028 case BUILT_IN_STRNCPY
:
5029 return gimple_fold_builtin_strncpy (gsi
,
5030 gimple_call_arg (stmt
, 0),
5031 gimple_call_arg (stmt
, 1),
5032 gimple_call_arg (stmt
, 2));
5033 case BUILT_IN_STRCAT
:
5034 return gimple_fold_builtin_strcat (gsi
, gimple_call_arg (stmt
, 0),
5035 gimple_call_arg (stmt
, 1));
5036 case BUILT_IN_STRNCAT
:
5037 return gimple_fold_builtin_strncat (gsi
);
5038 case BUILT_IN_INDEX
:
5039 case BUILT_IN_STRCHR
:
5040 return gimple_fold_builtin_strchr (gsi
, false);
5041 case BUILT_IN_RINDEX
:
5042 case BUILT_IN_STRRCHR
:
5043 return gimple_fold_builtin_strchr (gsi
, true);
5044 case BUILT_IN_STRSTR
:
5045 return gimple_fold_builtin_strstr (gsi
);
5046 case BUILT_IN_STRCMP
:
5047 case BUILT_IN_STRCMP_EQ
:
5048 case BUILT_IN_STRCASECMP
:
5049 case BUILT_IN_STRNCMP
:
5050 case BUILT_IN_STRNCMP_EQ
:
5051 case BUILT_IN_STRNCASECMP
:
5052 return gimple_fold_builtin_string_compare (gsi
);
5053 case BUILT_IN_MEMCHR
:
5054 return gimple_fold_builtin_memchr (gsi
);
5055 case BUILT_IN_FPUTS
:
5056 return gimple_fold_builtin_fputs (gsi
, gimple_call_arg (stmt
, 0),
5057 gimple_call_arg (stmt
, 1), false);
5058 case BUILT_IN_FPUTS_UNLOCKED
:
5059 return gimple_fold_builtin_fputs (gsi
, gimple_call_arg (stmt
, 0),
5060 gimple_call_arg (stmt
, 1), true);
5061 case BUILT_IN_MEMCPY_CHK
:
5062 case BUILT_IN_MEMPCPY_CHK
:
5063 case BUILT_IN_MEMMOVE_CHK
:
5064 case BUILT_IN_MEMSET_CHK
:
5065 return gimple_fold_builtin_memory_chk (gsi
,
5066 gimple_call_arg (stmt
, 0),
5067 gimple_call_arg (stmt
, 1),
5068 gimple_call_arg (stmt
, 2),
5069 gimple_call_arg (stmt
, 3),
5071 case BUILT_IN_STPCPY
:
5072 return gimple_fold_builtin_stpcpy (gsi
);
5073 case BUILT_IN_STRCPY_CHK
:
5074 case BUILT_IN_STPCPY_CHK
:
5075 return gimple_fold_builtin_stxcpy_chk (gsi
,
5076 gimple_call_arg (stmt
, 0),
5077 gimple_call_arg (stmt
, 1),
5078 gimple_call_arg (stmt
, 2),
5080 case BUILT_IN_STRNCPY_CHK
:
5081 case BUILT_IN_STPNCPY_CHK
:
5082 return gimple_fold_builtin_stxncpy_chk (gsi
,
5083 gimple_call_arg (stmt
, 0),
5084 gimple_call_arg (stmt
, 1),
5085 gimple_call_arg (stmt
, 2),
5086 gimple_call_arg (stmt
, 3),
5088 case BUILT_IN_SNPRINTF_CHK
:
5089 case BUILT_IN_VSNPRINTF_CHK
:
5090 return gimple_fold_builtin_snprintf_chk (gsi
, fcode
);
5092 case BUILT_IN_FPRINTF
:
5093 case BUILT_IN_FPRINTF_UNLOCKED
:
5094 case BUILT_IN_VFPRINTF
:
5095 if (n
== 2 || n
== 3)
5096 return gimple_fold_builtin_fprintf (gsi
,
5097 gimple_call_arg (stmt
, 0),
5098 gimple_call_arg (stmt
, 1),
5100 ? gimple_call_arg (stmt
, 2)
5104 case BUILT_IN_FPRINTF_CHK
:
5105 case BUILT_IN_VFPRINTF_CHK
:
5106 if (n
== 3 || n
== 4)
5107 return gimple_fold_builtin_fprintf (gsi
,
5108 gimple_call_arg (stmt
, 0),
5109 gimple_call_arg (stmt
, 2),
5111 ? gimple_call_arg (stmt
, 3)
5115 case BUILT_IN_PRINTF
:
5116 case BUILT_IN_PRINTF_UNLOCKED
:
5117 case BUILT_IN_VPRINTF
:
5118 if (n
== 1 || n
== 2)
5119 return gimple_fold_builtin_printf (gsi
, gimple_call_arg (stmt
, 0),
5121 ? gimple_call_arg (stmt
, 1)
5122 : NULL_TREE
, fcode
);
5124 case BUILT_IN_PRINTF_CHK
:
5125 case BUILT_IN_VPRINTF_CHK
:
5126 if (n
== 2 || n
== 3)
5127 return gimple_fold_builtin_printf (gsi
, gimple_call_arg (stmt
, 1),
5129 ? gimple_call_arg (stmt
, 2)
5130 : NULL_TREE
, fcode
);
5132 case BUILT_IN_ACC_ON_DEVICE
:
5133 return gimple_fold_builtin_acc_on_device (gsi
,
5134 gimple_call_arg (stmt
, 0));
5135 case BUILT_IN_REALLOC
:
5136 return gimple_fold_builtin_realloc (gsi
);
5138 case BUILT_IN_CLEAR_PADDING
:
5139 return gimple_fold_builtin_clear_padding (gsi
);
5144 /* Try the generic builtin folder. */
5145 bool ignore
= (gimple_call_lhs (stmt
) == NULL
);
5146 tree result
= fold_call_stmt (stmt
, ignore
);
5150 STRIP_NOPS (result
);
5152 result
= fold_convert (gimple_call_return_type (stmt
), result
);
5153 gimplify_and_update_call_from_tree (gsi
, result
);
5160 /* Transform IFN_GOACC_DIM_SIZE and IFN_GOACC_DIM_POS internal
5161 function calls to constants, where possible. */
5164 fold_internal_goacc_dim (const gimple
*call
)
5166 int axis
= oacc_get_ifn_dim_arg (call
);
5167 int size
= oacc_get_fn_dim_size (current_function_decl
, axis
);
5168 tree result
= NULL_TREE
;
5169 tree type
= TREE_TYPE (gimple_call_lhs (call
));
5171 switch (gimple_call_internal_fn (call
))
5173 case IFN_GOACC_DIM_POS
:
5174 /* If the size is 1, we know the answer. */
5176 result
= build_int_cst (type
, 0);
5178 case IFN_GOACC_DIM_SIZE
:
5179 /* If the size is not dynamic, we know the answer. */
5181 result
= build_int_cst (type
, size
);
5190 /* Return true if stmt is __atomic_compare_exchange_N call which is suitable
5191 for conversion into ATOMIC_COMPARE_EXCHANGE if the second argument is
5192 &var where var is only addressable because of such calls. */
5195 optimize_atomic_compare_exchange_p (gimple
*stmt
)
5197 if (gimple_call_num_args (stmt
) != 6
5198 || !flag_inline_atomics
5200 || sanitize_flags_p (SANITIZE_THREAD
| SANITIZE_ADDRESS
)
5201 || !gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
)
5202 || !gimple_vdef (stmt
)
5203 || !gimple_vuse (stmt
))
5206 tree fndecl
= gimple_call_fndecl (stmt
);
5207 switch (DECL_FUNCTION_CODE (fndecl
))
5209 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
:
5210 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2
:
5211 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4
:
5212 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8
:
5213 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16
:
5219 tree expected
= gimple_call_arg (stmt
, 1);
5220 if (TREE_CODE (expected
) != ADDR_EXPR
5221 || !SSA_VAR_P (TREE_OPERAND (expected
, 0)))
5224 tree etype
= TREE_TYPE (TREE_OPERAND (expected
, 0));
5225 if (!is_gimple_reg_type (etype
)
5226 || !auto_var_in_fn_p (TREE_OPERAND (expected
, 0), current_function_decl
)
5227 || TREE_THIS_VOLATILE (etype
)
5228 || VECTOR_TYPE_P (etype
)
5229 || TREE_CODE (etype
) == COMPLEX_TYPE
5230 /* Don't optimize floating point expected vars, VIEW_CONVERT_EXPRs
5231 might not preserve all the bits. See PR71716. */
5232 || SCALAR_FLOAT_TYPE_P (etype
)
5233 || maybe_ne (TYPE_PRECISION (etype
),
5234 GET_MODE_BITSIZE (TYPE_MODE (etype
))))
5237 tree weak
= gimple_call_arg (stmt
, 3);
5238 if (!integer_zerop (weak
) && !integer_onep (weak
))
5241 tree parmt
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
5242 tree itype
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt
)));
5243 machine_mode mode
= TYPE_MODE (itype
);
5245 if (direct_optab_handler (atomic_compare_and_swap_optab
, mode
)
5247 && optab_handler (sync_compare_and_swap_optab
, mode
) == CODE_FOR_nothing
)
5250 if (maybe_ne (int_size_in_bytes (etype
), GET_MODE_SIZE (mode
)))
5257 r = __atomic_compare_exchange_N (p, &e, d, w, s, f);
5259 _Complex uintN_t t = ATOMIC_COMPARE_EXCHANGE (p, e, d, w * 256 + N, s, f);
5260 i = IMAGPART_EXPR <t>;
5262 e = REALPART_EXPR <t>; */
5265 fold_builtin_atomic_compare_exchange (gimple_stmt_iterator
*gsi
)
5267 gimple
*stmt
= gsi_stmt (*gsi
);
5268 tree fndecl
= gimple_call_fndecl (stmt
);
5269 tree parmt
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
5270 tree itype
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt
)));
5271 tree ctype
= build_complex_type (itype
);
5272 tree expected
= TREE_OPERAND (gimple_call_arg (stmt
, 1), 0);
5273 bool throws
= false;
5275 gimple
*g
= gimple_build_assign (make_ssa_name (TREE_TYPE (expected
)),
5277 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
5278 gimple_stmt_iterator gsiret
= gsi_for_stmt (g
);
5279 if (!useless_type_conversion_p (itype
, TREE_TYPE (expected
)))
5281 g
= gimple_build_assign (make_ssa_name (itype
), VIEW_CONVERT_EXPR
,
5282 build1 (VIEW_CONVERT_EXPR
, itype
,
5283 gimple_assign_lhs (g
)));
5284 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
5286 int flag
= (integer_onep (gimple_call_arg (stmt
, 3)) ? 256 : 0)
5287 + int_size_in_bytes (itype
);
5288 g
= gimple_build_call_internal (IFN_ATOMIC_COMPARE_EXCHANGE
, 6,
5289 gimple_call_arg (stmt
, 0),
5290 gimple_assign_lhs (g
),
5291 gimple_call_arg (stmt
, 2),
5292 build_int_cst (integer_type_node
, flag
),
5293 gimple_call_arg (stmt
, 4),
5294 gimple_call_arg (stmt
, 5));
5295 tree lhs
= make_ssa_name (ctype
);
5296 gimple_call_set_lhs (g
, lhs
);
5297 gimple_move_vops (g
, stmt
);
5298 tree oldlhs
= gimple_call_lhs (stmt
);
5299 if (stmt_can_throw_internal (cfun
, stmt
))
5302 e
= find_fallthru_edge (gsi_bb (*gsi
)->succs
);
5304 gimple_call_set_nothrow (as_a
<gcall
*> (g
),
5305 gimple_call_nothrow_p (as_a
<gcall
*> (stmt
)));
5306 gimple_call_set_lhs (stmt
, NULL_TREE
);
5307 gsi_replace (gsi
, g
, true);
5310 g
= gimple_build_assign (make_ssa_name (itype
), IMAGPART_EXPR
,
5311 build1 (IMAGPART_EXPR
, itype
, lhs
));
5314 gsi_insert_on_edge_immediate (e
, g
);
5315 *gsi
= gsi_for_stmt (g
);
5318 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
5319 g
= gimple_build_assign (oldlhs
, NOP_EXPR
, gimple_assign_lhs (g
));
5320 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
5322 g
= gimple_build_assign (make_ssa_name (itype
), REALPART_EXPR
,
5323 build1 (REALPART_EXPR
, itype
, lhs
));
5324 if (throws
&& oldlhs
== NULL_TREE
)
5326 gsi_insert_on_edge_immediate (e
, g
);
5327 *gsi
= gsi_for_stmt (g
);
5330 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
5331 if (!useless_type_conversion_p (TREE_TYPE (expected
), itype
))
5333 g
= gimple_build_assign (make_ssa_name (TREE_TYPE (expected
)),
5335 build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (expected
),
5336 gimple_assign_lhs (g
)));
5337 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
5339 g
= gimple_build_assign (expected
, SSA_NAME
, gimple_assign_lhs (g
));
5340 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
5344 /* Return true if ARG0 CODE ARG1 in infinite signed precision operation
5345 doesn't fit into TYPE. The test for overflow should be regardless of
5346 -fwrapv, and even for unsigned types. */
5349 arith_overflowed_p (enum tree_code code
, const_tree type
,
5350 const_tree arg0
, const_tree arg1
)
5352 widest2_int warg0
= widest2_int_cst (arg0
);
5353 widest2_int warg1
= widest2_int_cst (arg1
);
5357 case PLUS_EXPR
: wres
= wi::add (warg0
, warg1
); break;
5358 case MINUS_EXPR
: wres
= wi::sub (warg0
, warg1
); break;
5359 case MULT_EXPR
: wres
= wi::mul (warg0
, warg1
); break;
5360 default: gcc_unreachable ();
5362 signop sign
= TYPE_SIGN (type
);
5363 if (sign
== UNSIGNED
&& wi::neg_p (wres
))
5365 return wi::min_precision (wres
, sign
) > TYPE_PRECISION (type
);
5368 /* If IFN_MASK_LOAD/STORE call CALL is unconditional, return a MEM_REF
5369 for the memory it references, otherwise return null. VECTYPE is the
5370 type of the memory vector. */
5373 gimple_fold_mask_load_store_mem_ref (gcall
*call
, tree vectype
)
5375 tree ptr
= gimple_call_arg (call
, 0);
5376 tree alias_align
= gimple_call_arg (call
, 1);
5377 tree mask
= gimple_call_arg (call
, 2);
5378 if (!tree_fits_uhwi_p (alias_align
) || !integer_all_onesp (mask
))
5381 unsigned HOST_WIDE_INT align
= tree_to_uhwi (alias_align
);
5382 if (TYPE_ALIGN (vectype
) != align
)
5383 vectype
= build_aligned_type (vectype
, align
);
5384 tree offset
= build_zero_cst (TREE_TYPE (alias_align
));
5385 return fold_build2 (MEM_REF
, vectype
, ptr
, offset
);
5388 /* Try to fold IFN_MASK_LOAD call CALL. Return true on success. */
5391 gimple_fold_mask_load (gimple_stmt_iterator
*gsi
, gcall
*call
)
5393 tree lhs
= gimple_call_lhs (call
);
5397 if (tree rhs
= gimple_fold_mask_load_store_mem_ref (call
, TREE_TYPE (lhs
)))
5399 gassign
*new_stmt
= gimple_build_assign (lhs
, rhs
);
5400 gimple_set_location (new_stmt
, gimple_location (call
));
5401 gimple_move_vops (new_stmt
, call
);
5402 gsi_replace (gsi
, new_stmt
, false);
5408 /* Try to fold IFN_MASK_STORE call CALL. Return true on success. */
5411 gimple_fold_mask_store (gimple_stmt_iterator
*gsi
, gcall
*call
)
5413 tree rhs
= gimple_call_arg (call
, 3);
5414 if (tree lhs
= gimple_fold_mask_load_store_mem_ref (call
, TREE_TYPE (rhs
)))
5416 gassign
*new_stmt
= gimple_build_assign (lhs
, rhs
);
5417 gimple_set_location (new_stmt
, gimple_location (call
));
5418 gimple_move_vops (new_stmt
, call
);
5419 gsi_replace (gsi
, new_stmt
, false);
5425 /* Attempt to fold a call statement referenced by the statement iterator GSI.
5426 The statement may be replaced by another statement, e.g., if the call
5427 simplifies to a constant value. Return true if any changes were made.
5428 It is assumed that the operands have been previously folded. */
5431 gimple_fold_call (gimple_stmt_iterator
*gsi
, bool inplace
)
5433 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
5435 bool changed
= false;
5437 /* Check for virtual calls that became direct calls. */
5438 callee
= gimple_call_fn (stmt
);
5439 if (callee
&& TREE_CODE (callee
) == OBJ_TYPE_REF
)
5441 if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee
)) != NULL_TREE
)
5443 if (dump_file
&& virtual_method_call_p (callee
)
5444 && !possible_polymorphic_call_target_p
5445 (callee
, stmt
, cgraph_node::get (gimple_call_addr_fndecl
5446 (OBJ_TYPE_REF_EXPR (callee
)))))
5449 "Type inheritance inconsistent devirtualization of ");
5450 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
5451 fprintf (dump_file
, " to ");
5452 print_generic_expr (dump_file
, callee
, TDF_SLIM
);
5453 fprintf (dump_file
, "\n");
5456 gimple_call_set_fn (stmt
, OBJ_TYPE_REF_EXPR (callee
));
5459 else if (flag_devirtualize
&& !inplace
&& virtual_method_call_p (callee
))
5462 vec
<cgraph_node
*>targets
5463 = possible_polymorphic_call_targets (callee
, stmt
, &final
);
5464 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
5466 tree lhs
= gimple_call_lhs (stmt
);
5467 if (dump_enabled_p ())
5469 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, stmt
,
5470 "folding virtual function call to %s\n",
5471 targets
.length () == 1
5472 ? targets
[0]->name ()
5473 : "__builtin_unreachable");
5475 if (targets
.length () == 1)
5477 tree fndecl
= targets
[0]->decl
;
5478 gimple_call_set_fndecl (stmt
, fndecl
);
5480 /* If changing the call to __cxa_pure_virtual
5481 or similar noreturn function, adjust gimple_call_fntype
5483 if (gimple_call_noreturn_p (stmt
)
5484 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl
)))
5485 && TYPE_ARG_TYPES (TREE_TYPE (fndecl
))
5486 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)))
5488 gimple_call_set_fntype (stmt
, TREE_TYPE (fndecl
));
5489 /* If the call becomes noreturn, remove the lhs. */
5491 && gimple_call_noreturn_p (stmt
)
5492 && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (stmt
)))
5493 || should_remove_lhs_p (lhs
)))
5495 if (TREE_CODE (lhs
) == SSA_NAME
)
5497 tree var
= create_tmp_var (TREE_TYPE (lhs
));
5498 tree def
= get_or_create_ssa_default_def (cfun
, var
);
5499 gimple
*new_stmt
= gimple_build_assign (lhs
, def
);
5500 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
5502 gimple_call_set_lhs (stmt
, NULL_TREE
);
5504 maybe_remove_unused_call_args (cfun
, stmt
);
5508 tree fndecl
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
5509 gimple
*new_stmt
= gimple_build_call (fndecl
, 0);
5510 gimple_set_location (new_stmt
, gimple_location (stmt
));
5511 /* If the call had a SSA name as lhs morph that into
5512 an uninitialized value. */
5513 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
5515 tree var
= create_tmp_var (TREE_TYPE (lhs
));
5516 SET_SSA_NAME_VAR_OR_IDENTIFIER (lhs
, var
);
5517 SSA_NAME_DEF_STMT (lhs
) = gimple_build_nop ();
5518 set_ssa_default_def (cfun
, var
, lhs
);
5520 gimple_move_vops (new_stmt
, stmt
);
5521 gsi_replace (gsi
, new_stmt
, false);
5528 /* Check for indirect calls that became direct calls, and then
5529 no longer require a static chain. */
5530 if (gimple_call_chain (stmt
))
5532 tree fn
= gimple_call_fndecl (stmt
);
5533 if (fn
&& !DECL_STATIC_CHAIN (fn
))
5535 gimple_call_set_chain (stmt
, NULL
);
5543 /* Check for builtins that CCP can handle using information not
5544 available in the generic fold routines. */
5545 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
5547 if (gimple_fold_builtin (gsi
))
5550 else if (gimple_call_builtin_p (stmt
, BUILT_IN_MD
))
5552 changed
|= targetm
.gimple_fold_builtin (gsi
);
5554 else if (gimple_call_internal_p (stmt
))
5556 enum tree_code subcode
= ERROR_MARK
;
5557 tree result
= NULL_TREE
;
5558 bool cplx_result
= false;
5559 tree overflow
= NULL_TREE
;
5560 switch (gimple_call_internal_fn (stmt
))
5562 case IFN_BUILTIN_EXPECT
:
5563 result
= fold_builtin_expect (gimple_location (stmt
),
5564 gimple_call_arg (stmt
, 0),
5565 gimple_call_arg (stmt
, 1),
5566 gimple_call_arg (stmt
, 2),
5569 case IFN_UBSAN_OBJECT_SIZE
:
5571 tree offset
= gimple_call_arg (stmt
, 1);
5572 tree objsize
= gimple_call_arg (stmt
, 2);
5573 if (integer_all_onesp (objsize
)
5574 || (TREE_CODE (offset
) == INTEGER_CST
5575 && TREE_CODE (objsize
) == INTEGER_CST
5576 && tree_int_cst_le (offset
, objsize
)))
5578 replace_call_with_value (gsi
, NULL_TREE
);
5584 if (integer_zerop (gimple_call_arg (stmt
, 1)))
5586 replace_call_with_value (gsi
, NULL_TREE
);
5590 case IFN_UBSAN_BOUNDS
:
5592 tree index
= gimple_call_arg (stmt
, 1);
5593 tree bound
= gimple_call_arg (stmt
, 2);
5594 if (TREE_CODE (index
) == INTEGER_CST
5595 && TREE_CODE (bound
) == INTEGER_CST
)
5597 index
= fold_convert (TREE_TYPE (bound
), index
);
5598 if (TREE_CODE (index
) == INTEGER_CST
5599 && tree_int_cst_le (index
, bound
))
5601 replace_call_with_value (gsi
, NULL_TREE
);
5607 case IFN_GOACC_DIM_SIZE
:
5608 case IFN_GOACC_DIM_POS
:
5609 result
= fold_internal_goacc_dim (stmt
);
5611 case IFN_UBSAN_CHECK_ADD
:
5612 subcode
= PLUS_EXPR
;
5614 case IFN_UBSAN_CHECK_SUB
:
5615 subcode
= MINUS_EXPR
;
5617 case IFN_UBSAN_CHECK_MUL
:
5618 subcode
= MULT_EXPR
;
5620 case IFN_ADD_OVERFLOW
:
5621 subcode
= PLUS_EXPR
;
5624 case IFN_SUB_OVERFLOW
:
5625 subcode
= MINUS_EXPR
;
5628 case IFN_MUL_OVERFLOW
:
5629 subcode
= MULT_EXPR
;
5633 changed
|= gimple_fold_mask_load (gsi
, stmt
);
5635 case IFN_MASK_STORE
:
5636 changed
|= gimple_fold_mask_store (gsi
, stmt
);
5641 if (subcode
!= ERROR_MARK
)
5643 tree arg0
= gimple_call_arg (stmt
, 0);
5644 tree arg1
= gimple_call_arg (stmt
, 1);
5645 tree type
= TREE_TYPE (arg0
);
5648 tree lhs
= gimple_call_lhs (stmt
);
5649 if (lhs
== NULL_TREE
)
5652 type
= TREE_TYPE (TREE_TYPE (lhs
));
5654 if (type
== NULL_TREE
)
5656 /* x = y + 0; x = y - 0; x = y * 0; */
5657 else if (integer_zerop (arg1
))
5658 result
= subcode
== MULT_EXPR
? integer_zero_node
: arg0
;
5659 /* x = 0 + y; x = 0 * y; */
5660 else if (subcode
!= MINUS_EXPR
&& integer_zerop (arg0
))
5661 result
= subcode
== MULT_EXPR
? integer_zero_node
: arg1
;
5663 else if (subcode
== MINUS_EXPR
&& operand_equal_p (arg0
, arg1
, 0))
5664 result
= integer_zero_node
;
5665 /* x = y * 1; x = 1 * y; */
5666 else if (subcode
== MULT_EXPR
&& integer_onep (arg1
))
5668 else if (subcode
== MULT_EXPR
&& integer_onep (arg0
))
5670 else if (TREE_CODE (arg0
) == INTEGER_CST
5671 && TREE_CODE (arg1
) == INTEGER_CST
)
5674 result
= int_const_binop (subcode
, fold_convert (type
, arg0
),
5675 fold_convert (type
, arg1
));
5677 result
= int_const_binop (subcode
, arg0
, arg1
);
5678 if (result
&& arith_overflowed_p (subcode
, type
, arg0
, arg1
))
5681 overflow
= build_one_cst (type
);
5688 if (result
== integer_zero_node
)
5689 result
= build_zero_cst (type
);
5690 else if (cplx_result
&& TREE_TYPE (result
) != type
)
5692 if (TREE_CODE (result
) == INTEGER_CST
)
5694 if (arith_overflowed_p (PLUS_EXPR
, type
, result
,
5696 overflow
= build_one_cst (type
);
5698 else if ((!TYPE_UNSIGNED (TREE_TYPE (result
))
5699 && TYPE_UNSIGNED (type
))
5700 || (TYPE_PRECISION (type
)
5701 < (TYPE_PRECISION (TREE_TYPE (result
))
5702 + (TYPE_UNSIGNED (TREE_TYPE (result
))
5703 && !TYPE_UNSIGNED (type
)))))
5706 result
= fold_convert (type
, result
);
5713 if (TREE_CODE (result
) == INTEGER_CST
&& TREE_OVERFLOW (result
))
5714 result
= drop_tree_overflow (result
);
5717 if (overflow
== NULL_TREE
)
5718 overflow
= build_zero_cst (TREE_TYPE (result
));
5719 tree ctype
= build_complex_type (TREE_TYPE (result
));
5720 if (TREE_CODE (result
) == INTEGER_CST
5721 && TREE_CODE (overflow
) == INTEGER_CST
)
5722 result
= build_complex (ctype
, result
, overflow
);
5724 result
= build2_loc (gimple_location (stmt
), COMPLEX_EXPR
,
5725 ctype
, result
, overflow
);
5727 gimplify_and_update_call_from_tree (gsi
, result
);
5736 /* Return true whether NAME has a use on STMT. */
5739 has_use_on_stmt (tree name
, gimple
*stmt
)
5741 imm_use_iterator iter
;
5742 use_operand_p use_p
;
5743 FOR_EACH_IMM_USE_FAST (use_p
, iter
, name
)
5744 if (USE_STMT (use_p
) == stmt
)
5749 /* Worker for fold_stmt_1 dispatch to pattern based folding with
5752 Replaces *GSI with the simplification result in RCODE and OPS
5753 and the associated statements in *SEQ. Does the replacement
5754 according to INPLACE and returns true if the operation succeeded. */
5757 replace_stmt_with_simplification (gimple_stmt_iterator
*gsi
,
5758 gimple_match_op
*res_op
,
5759 gimple_seq
*seq
, bool inplace
)
5761 gimple
*stmt
= gsi_stmt (*gsi
);
5762 tree
*ops
= res_op
->ops
;
5763 unsigned int num_ops
= res_op
->num_ops
;
5765 /* Play safe and do not allow abnormals to be mentioned in
5766 newly created statements. See also maybe_push_res_to_seq.
5767 As an exception allow such uses if there was a use of the
5768 same SSA name on the old stmt. */
5769 for (unsigned int i
= 0; i
< num_ops
; ++i
)
5770 if (TREE_CODE (ops
[i
]) == SSA_NAME
5771 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[i
])
5772 && !has_use_on_stmt (ops
[i
], stmt
))
5775 if (num_ops
> 0 && COMPARISON_CLASS_P (ops
[0]))
5776 for (unsigned int i
= 0; i
< 2; ++i
)
5777 if (TREE_CODE (TREE_OPERAND (ops
[0], i
)) == SSA_NAME
5778 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops
[0], i
))
5779 && !has_use_on_stmt (TREE_OPERAND (ops
[0], i
), stmt
))
5782 /* Don't insert new statements when INPLACE is true, even if we could
5783 reuse STMT for the final statement. */
5784 if (inplace
&& !gimple_seq_empty_p (*seq
))
5787 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
5789 gcc_assert (res_op
->code
.is_tree_code ());
5790 auto code
= tree_code (res_op
->code
);
5791 if (TREE_CODE_CLASS (code
) == tcc_comparison
5792 /* GIMPLE_CONDs condition may not throw. */
5793 && (!flag_exceptions
5794 || !cfun
->can_throw_non_call_exceptions
5795 || !operation_could_trap_p (code
,
5796 FLOAT_TYPE_P (TREE_TYPE (ops
[0])),
5798 gimple_cond_set_condition (cond_stmt
, code
, ops
[0], ops
[1]);
5799 else if (code
== SSA_NAME
)
5800 gimple_cond_set_condition (cond_stmt
, NE_EXPR
, ops
[0],
5801 build_zero_cst (TREE_TYPE (ops
[0])));
5802 else if (code
== INTEGER_CST
)
5804 if (integer_zerop (ops
[0]))
5805 gimple_cond_make_false (cond_stmt
);
5807 gimple_cond_make_true (cond_stmt
);
5811 tree res
= maybe_push_res_to_seq (res_op
, seq
);
5814 gimple_cond_set_condition (cond_stmt
, NE_EXPR
, res
,
5815 build_zero_cst (TREE_TYPE (res
)));
5819 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5821 fprintf (dump_file
, "gimple_simplified to ");
5822 if (!gimple_seq_empty_p (*seq
))
5823 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
5824 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
),
5827 gsi_insert_seq_before (gsi
, *seq
, GSI_SAME_STMT
);
5830 else if (is_gimple_assign (stmt
)
5831 && res_op
->code
.is_tree_code ())
5833 auto code
= tree_code (res_op
->code
);
5835 || gimple_num_ops (stmt
) > get_gimple_rhs_num_ops (code
))
5837 maybe_build_generic_op (res_op
);
5838 gimple_assign_set_rhs_with_ops (gsi
, code
,
5839 res_op
->op_or_null (0),
5840 res_op
->op_or_null (1),
5841 res_op
->op_or_null (2));
5842 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5844 fprintf (dump_file
, "gimple_simplified to ");
5845 if (!gimple_seq_empty_p (*seq
))
5846 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
5847 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
),
5850 gsi_insert_seq_before (gsi
, *seq
, GSI_SAME_STMT
);
5854 else if (res_op
->code
.is_fn_code ()
5855 && gimple_call_combined_fn (stmt
) == combined_fn (res_op
->code
))
5857 gcc_assert (num_ops
== gimple_call_num_args (stmt
));
5858 for (unsigned int i
= 0; i
< num_ops
; ++i
)
5859 gimple_call_set_arg (stmt
, i
, ops
[i
]);
5860 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5862 fprintf (dump_file
, "gimple_simplified to ");
5863 if (!gimple_seq_empty_p (*seq
))
5864 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
5865 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
), 0, TDF_SLIM
);
5867 gsi_insert_seq_before (gsi
, *seq
, GSI_SAME_STMT
);
5872 if (gimple_has_lhs (stmt
))
5874 tree lhs
= gimple_get_lhs (stmt
);
5875 if (!maybe_push_res_to_seq (res_op
, seq
, lhs
))
5877 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5879 fprintf (dump_file
, "gimple_simplified to ");
5880 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
5882 gsi_replace_with_seq_vops (gsi
, *seq
);
5892 /* Canonicalize MEM_REFs invariant address operand after propagation. */
5895 maybe_canonicalize_mem_ref_addr (tree
*t
, bool is_debug
= false)
5900 if (TREE_CODE (*t
) == ADDR_EXPR
)
5901 t
= &TREE_OPERAND (*t
, 0);
5903 /* The C and C++ frontends use an ARRAY_REF for indexing with their
5904 generic vector extension. The actual vector referenced is
5905 view-converted to an array type for this purpose. If the index
5906 is constant the canonical representation in the middle-end is a
5907 BIT_FIELD_REF so re-write the former to the latter here. */
5908 if (TREE_CODE (*t
) == ARRAY_REF
5909 && TREE_CODE (TREE_OPERAND (*t
, 0)) == VIEW_CONVERT_EXPR
5910 && TREE_CODE (TREE_OPERAND (*t
, 1)) == INTEGER_CST
5911 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t
, 0), 0))))
5913 tree vtype
= TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t
, 0), 0));
5914 if (VECTOR_TYPE_P (vtype
))
5916 tree low
= array_ref_low_bound (*t
);
5917 if (TREE_CODE (low
) == INTEGER_CST
)
5919 if (tree_int_cst_le (low
, TREE_OPERAND (*t
, 1)))
5921 widest_int idx
= wi::sub (wi::to_widest (TREE_OPERAND (*t
, 1)),
5922 wi::to_widest (low
));
5923 idx
= wi::mul (idx
, wi::to_widest
5924 (TYPE_SIZE (TREE_TYPE (*t
))));
5926 = wi::add (idx
, wi::to_widest (TYPE_SIZE (TREE_TYPE (*t
))));
5927 if (wi::les_p (ext
, wi::to_widest (TYPE_SIZE (vtype
))))
5929 *t
= build3_loc (EXPR_LOCATION (*t
), BIT_FIELD_REF
,
5931 TREE_OPERAND (TREE_OPERAND (*t
, 0), 0),
5932 TYPE_SIZE (TREE_TYPE (*t
)),
5933 wide_int_to_tree (bitsizetype
, idx
));
5941 while (handled_component_p (*t
))
5942 t
= &TREE_OPERAND (*t
, 0);
5944 /* Canonicalize MEM [&foo.bar, 0] which appears after propagating
5945 of invariant addresses into a SSA name MEM_REF address. */
5946 if (TREE_CODE (*t
) == MEM_REF
5947 || TREE_CODE (*t
) == TARGET_MEM_REF
)
5949 tree addr
= TREE_OPERAND (*t
, 0);
5950 if (TREE_CODE (addr
) == ADDR_EXPR
5951 && (TREE_CODE (TREE_OPERAND (addr
, 0)) == MEM_REF
5952 || handled_component_p (TREE_OPERAND (addr
, 0))))
5956 base
= get_addr_base_and_unit_offset (TREE_OPERAND (addr
, 0),
5965 TREE_OPERAND (*t
, 0) = build_fold_addr_expr (base
);
5966 TREE_OPERAND (*t
, 1) = int_const_binop (PLUS_EXPR
,
5967 TREE_OPERAND (*t
, 1),
5968 size_int (coffset
));
5971 gcc_checking_assert (TREE_CODE (TREE_OPERAND (*t
, 0)) == DEBUG_EXPR_DECL
5972 || is_gimple_mem_ref_addr (TREE_OPERAND (*t
, 0)));
5975 /* Canonicalize back MEM_REFs to plain reference trees if the object
5976 accessed is a decl that has the same access semantics as the MEM_REF. */
5977 if (TREE_CODE (*t
) == MEM_REF
5978 && TREE_CODE (TREE_OPERAND (*t
, 0)) == ADDR_EXPR
5979 && integer_zerop (TREE_OPERAND (*t
, 1))
5980 && MR_DEPENDENCE_CLIQUE (*t
) == 0)
5982 tree decl
= TREE_OPERAND (TREE_OPERAND (*t
, 0), 0);
5983 tree alias_type
= TREE_TYPE (TREE_OPERAND (*t
, 1));
5984 if (/* Same volatile qualification. */
5985 TREE_THIS_VOLATILE (*t
) == TREE_THIS_VOLATILE (decl
)
5986 /* Same TBAA behavior with -fstrict-aliasing. */
5987 && !TYPE_REF_CAN_ALIAS_ALL (alias_type
)
5988 && (TYPE_MAIN_VARIANT (TREE_TYPE (decl
))
5989 == TYPE_MAIN_VARIANT (TREE_TYPE (alias_type
)))
5990 /* Same alignment. */
5991 && TYPE_ALIGN (TREE_TYPE (decl
)) == TYPE_ALIGN (TREE_TYPE (*t
))
5992 /* We have to look out here to not drop a required conversion
5993 from the rhs to the lhs if *t appears on the lhs or vice-versa
5994 if it appears on the rhs. Thus require strict type
5996 && types_compatible_p (TREE_TYPE (*t
), TREE_TYPE (decl
)))
5998 *t
= TREE_OPERAND (TREE_OPERAND (*t
, 0), 0);
6003 else if (TREE_CODE (*orig_t
) == ADDR_EXPR
6004 && TREE_CODE (*t
) == MEM_REF
6005 && TREE_CODE (TREE_OPERAND (*t
, 0)) == INTEGER_CST
)
6009 base
= get_addr_base_and_unit_offset (TREE_OPERAND (*orig_t
, 0),
6013 gcc_assert (TREE_CODE (base
) == MEM_REF
);
6015 if (mem_ref_offset (base
).to_shwi (&moffset
))
6018 if (wi::to_poly_wide (TREE_OPERAND (base
, 0)).to_shwi (&moffset
))
6021 *orig_t
= build_int_cst (TREE_TYPE (*orig_t
), coffset
);
6028 /* Canonicalize TARGET_MEM_REF in particular with respect to
6029 the indexes becoming constant. */
6030 else if (TREE_CODE (*t
) == TARGET_MEM_REF
)
6032 tree tem
= maybe_fold_tmr (*t
);
6036 if (TREE_CODE (*orig_t
) == ADDR_EXPR
)
6037 recompute_tree_invariant_for_addr_expr (*orig_t
);
6045 /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument
6046 distinguishes both cases. */
6049 fold_stmt_1 (gimple_stmt_iterator
*gsi
, bool inplace
, tree (*valueize
) (tree
))
6051 bool changed
= false;
6052 gimple
*stmt
= gsi_stmt (*gsi
);
6053 bool nowarning
= warning_suppressed_p (stmt
, OPT_Wstrict_overflow
);
6055 fold_defer_overflow_warnings ();
6057 /* First do required canonicalization of [TARGET_]MEM_REF addresses
6059 ??? This shouldn't be done in generic folding but in the
6060 propagation helpers which also know whether an address was
6062 Also canonicalize operand order. */
6063 switch (gimple_code (stmt
))
6066 if (gimple_assign_rhs_class (stmt
) == GIMPLE_SINGLE_RHS
)
6068 tree
*rhs
= gimple_assign_rhs1_ptr (stmt
);
6069 if ((REFERENCE_CLASS_P (*rhs
)
6070 || TREE_CODE (*rhs
) == ADDR_EXPR
)
6071 && maybe_canonicalize_mem_ref_addr (rhs
))
6073 tree
*lhs
= gimple_assign_lhs_ptr (stmt
);
6074 if (REFERENCE_CLASS_P (*lhs
)
6075 && maybe_canonicalize_mem_ref_addr (lhs
))
6077 /* Canonicalize &MEM[ssa_n, CST] to ssa_n p+ CST.
6078 This cannot be done in maybe_canonicalize_mem_ref_addr
6079 as the gimple now has two operands rather than one.
6080 The same reason why this can't be done in
6081 maybe_canonicalize_mem_ref_addr is the same reason why
6082 this can't be done inplace. */
6083 if (!inplace
&& TREE_CODE (*rhs
) == ADDR_EXPR
)
6085 tree inner
= TREE_OPERAND (*rhs
, 0);
6086 if (TREE_CODE (inner
) == MEM_REF
6087 && TREE_CODE (TREE_OPERAND (inner
, 0)) == SSA_NAME
6088 && TREE_CODE (TREE_OPERAND (inner
, 1)) == INTEGER_CST
)
6090 tree ptr
= TREE_OPERAND (inner
, 0);
6091 tree addon
= TREE_OPERAND (inner
, 1);
6092 addon
= fold_convert (sizetype
, addon
);
6093 gimple_assign_set_rhs_with_ops (gsi
, POINTER_PLUS_EXPR
,
6096 stmt
= gsi_stmt (*gsi
);
6102 /* Canonicalize operand order. */
6103 enum tree_code code
= gimple_assign_rhs_code (stmt
);
6104 if (TREE_CODE_CLASS (code
) == tcc_comparison
6105 || commutative_tree_code (code
)
6106 || commutative_ternary_tree_code (code
))
6108 tree rhs1
= gimple_assign_rhs1 (stmt
);
6109 tree rhs2
= gimple_assign_rhs2 (stmt
);
6110 if (tree_swap_operands_p (rhs1
, rhs2
))
6112 gimple_assign_set_rhs1 (stmt
, rhs2
);
6113 gimple_assign_set_rhs2 (stmt
, rhs1
);
6114 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
6115 gimple_assign_set_rhs_code (stmt
,
6116 swap_tree_comparison (code
));
6124 gcall
*call
= as_a
<gcall
*> (stmt
);
6125 for (i
= 0; i
< gimple_call_num_args (call
); ++i
)
6127 tree
*arg
= gimple_call_arg_ptr (call
, i
);
6128 if (REFERENCE_CLASS_P (*arg
)
6129 && maybe_canonicalize_mem_ref_addr (arg
))
6132 tree
*lhs
= gimple_call_lhs_ptr (call
);
6134 && REFERENCE_CLASS_P (*lhs
)
6135 && maybe_canonicalize_mem_ref_addr (lhs
))
6139 combined_fn cfn
= gimple_call_combined_fn (call
);
6140 internal_fn ifn
= associated_internal_fn (cfn
, TREE_TYPE (*lhs
));
6141 int opno
= first_commutative_argument (ifn
);
6144 tree arg1
= gimple_call_arg (call
, opno
);
6145 tree arg2
= gimple_call_arg (call
, opno
+ 1);
6146 if (tree_swap_operands_p (arg1
, arg2
))
6148 gimple_call_set_arg (call
, opno
, arg2
);
6149 gimple_call_set_arg (call
, opno
+ 1, arg1
);
6158 gasm
*asm_stmt
= as_a
<gasm
*> (stmt
);
6159 for (i
= 0; i
< gimple_asm_noutputs (asm_stmt
); ++i
)
6161 tree link
= gimple_asm_output_op (asm_stmt
, i
);
6162 tree op
= TREE_VALUE (link
);
6163 if (REFERENCE_CLASS_P (op
)
6164 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link
)))
6167 for (i
= 0; i
< gimple_asm_ninputs (asm_stmt
); ++i
)
6169 tree link
= gimple_asm_input_op (asm_stmt
, i
);
6170 tree op
= TREE_VALUE (link
);
6171 if ((REFERENCE_CLASS_P (op
)
6172 || TREE_CODE (op
) == ADDR_EXPR
)
6173 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link
)))
6179 if (gimple_debug_bind_p (stmt
))
6181 tree
*val
= gimple_debug_bind_get_value_ptr (stmt
);
6183 && (REFERENCE_CLASS_P (*val
)
6184 || TREE_CODE (*val
) == ADDR_EXPR
)
6185 && maybe_canonicalize_mem_ref_addr (val
, true))
6191 /* Canonicalize operand order. */
6192 tree lhs
= gimple_cond_lhs (stmt
);
6193 tree rhs
= gimple_cond_rhs (stmt
);
6194 if (tree_swap_operands_p (lhs
, rhs
))
6196 gcond
*gc
= as_a
<gcond
*> (stmt
);
6197 gimple_cond_set_lhs (gc
, rhs
);
6198 gimple_cond_set_rhs (gc
, lhs
);
6199 gimple_cond_set_code (gc
,
6200 swap_tree_comparison (gimple_cond_code (gc
)));
6207 /* Dispatch to pattern-based folding. */
6209 || is_gimple_assign (stmt
)
6210 || gimple_code (stmt
) == GIMPLE_COND
)
6212 gimple_seq seq
= NULL
;
6213 gimple_match_op res_op
;
6214 if (gimple_simplify (stmt
, &res_op
, inplace
? NULL
: &seq
,
6215 valueize
, valueize
))
6217 if (replace_stmt_with_simplification (gsi
, &res_op
, &seq
, inplace
))
6220 gimple_seq_discard (seq
);
6224 stmt
= gsi_stmt (*gsi
);
6226 /* Fold the main computation performed by the statement. */
6227 switch (gimple_code (stmt
))
6231 /* Try to canonicalize for boolean-typed X the comparisons
6232 X == 0, X == 1, X != 0, and X != 1. */
6233 if (gimple_assign_rhs_code (stmt
) == EQ_EXPR
6234 || gimple_assign_rhs_code (stmt
) == NE_EXPR
)
6236 tree lhs
= gimple_assign_lhs (stmt
);
6237 tree op1
= gimple_assign_rhs1 (stmt
);
6238 tree op2
= gimple_assign_rhs2 (stmt
);
6239 tree type
= TREE_TYPE (op1
);
6241 /* Check whether the comparison operands are of the same boolean
6242 type as the result type is.
6243 Check that second operand is an integer-constant with value
6245 if (TREE_CODE (op2
) == INTEGER_CST
6246 && (integer_zerop (op2
) || integer_onep (op2
))
6247 && useless_type_conversion_p (TREE_TYPE (lhs
), type
))
6249 enum tree_code cmp_code
= gimple_assign_rhs_code (stmt
);
6250 bool is_logical_not
= false;
6252 /* X == 0 and X != 1 is a logical-not.of X
6253 X == 1 and X != 0 is X */
6254 if ((cmp_code
== EQ_EXPR
&& integer_zerop (op2
))
6255 || (cmp_code
== NE_EXPR
&& integer_onep (op2
)))
6256 is_logical_not
= true;
6258 if (is_logical_not
== false)
6259 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op1
), op1
);
6260 /* Only for one-bit precision typed X the transformation
6261 !X -> ~X is valied. */
6262 else if (TYPE_PRECISION (type
) == 1)
6263 gimple_assign_set_rhs_with_ops (gsi
, BIT_NOT_EXPR
, op1
);
6264 /* Otherwise we use !X -> X ^ 1. */
6266 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op1
,
6267 build_int_cst (type
, 1));
6273 unsigned old_num_ops
= gimple_num_ops (stmt
);
6274 tree lhs
= gimple_assign_lhs (stmt
);
6275 tree new_rhs
= fold_gimple_assign (gsi
);
6277 && !useless_type_conversion_p (TREE_TYPE (lhs
),
6278 TREE_TYPE (new_rhs
)))
6279 new_rhs
= fold_convert (TREE_TYPE (lhs
), new_rhs
);
6282 || get_gimple_rhs_num_ops (TREE_CODE (new_rhs
)) < old_num_ops
))
6284 gimple_assign_set_rhs_from_tree (gsi
, new_rhs
);
6291 changed
|= gimple_fold_call (gsi
, inplace
);
6295 if (gimple_debug_bind_p (stmt
))
6297 tree val
= gimple_debug_bind_get_value (stmt
);
6298 if (val
&& REFERENCE_CLASS_P (val
))
6300 tree tem
= maybe_fold_reference (val
);
6303 gimple_debug_bind_set_value (stmt
, tem
);
6312 greturn
*ret_stmt
= as_a
<greturn
*> (stmt
);
6313 tree ret
= gimple_return_retval(ret_stmt
);
6315 if (ret
&& TREE_CODE (ret
) == SSA_NAME
&& valueize
)
6317 tree val
= valueize (ret
);
6318 if (val
&& val
!= ret
6319 && may_propagate_copy (ret
, val
))
6321 gimple_return_set_retval (ret_stmt
, val
);
6331 stmt
= gsi_stmt (*gsi
);
6333 fold_undefer_overflow_warnings (changed
&& !nowarning
, stmt
, 0);
6337 /* Valueziation callback that ends up not following SSA edges. */
6340 no_follow_ssa_edges (tree
)
6345 /* Valueization callback that ends up following single-use SSA edges only. */
6348 follow_single_use_edges (tree val
)
6350 if (TREE_CODE (val
) == SSA_NAME
6351 && !has_single_use (val
))
6356 /* Valueization callback that follows all SSA edges. */
6359 follow_all_ssa_edges (tree val
)
6364 /* Fold the statement pointed to by GSI. In some cases, this function may
6365 replace the whole statement with a new one. Returns true iff folding
6367 The statement pointed to by GSI should be in valid gimple form but may
6368 be in unfolded state as resulting from for example constant propagation
6369 which can produce *&x = 0. */
6372 fold_stmt (gimple_stmt_iterator
*gsi
)
6374 return fold_stmt_1 (gsi
, false, no_follow_ssa_edges
);
6378 fold_stmt (gimple_stmt_iterator
*gsi
, tree (*valueize
) (tree
))
6380 return fold_stmt_1 (gsi
, false, valueize
);
6383 /* Perform the minimal folding on statement *GSI. Only operations like
6384 *&x created by constant propagation are handled. The statement cannot
6385 be replaced with a new one. Return true if the statement was
6386 changed, false otherwise.
6387 The statement *GSI should be in valid gimple form but may
6388 be in unfolded state as resulting from for example constant propagation
6389 which can produce *&x = 0. */
6392 fold_stmt_inplace (gimple_stmt_iterator
*gsi
)
6394 gimple
*stmt
= gsi_stmt (*gsi
);
6395 bool changed
= fold_stmt_1 (gsi
, true, no_follow_ssa_edges
);
6396 gcc_assert (gsi_stmt (*gsi
) == stmt
);
6400 /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
6401 if EXPR is null or we don't know how.
6402 If non-null, the result always has boolean type. */
6405 canonicalize_bool (tree expr
, bool invert
)
6411 if (integer_nonzerop (expr
))
6412 return boolean_false_node
;
6413 else if (integer_zerop (expr
))
6414 return boolean_true_node
;
6415 else if (TREE_CODE (expr
) == SSA_NAME
)
6416 return fold_build2 (EQ_EXPR
, boolean_type_node
, expr
,
6417 build_int_cst (TREE_TYPE (expr
), 0));
6418 else if (COMPARISON_CLASS_P (expr
))
6419 return fold_build2 (invert_tree_comparison (TREE_CODE (expr
), false),
6421 TREE_OPERAND (expr
, 0),
6422 TREE_OPERAND (expr
, 1));
6428 if (TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
)
6430 if (integer_nonzerop (expr
))
6431 return boolean_true_node
;
6432 else if (integer_zerop (expr
))
6433 return boolean_false_node
;
6434 else if (TREE_CODE (expr
) == SSA_NAME
)
6435 return fold_build2 (NE_EXPR
, boolean_type_node
, expr
,
6436 build_int_cst (TREE_TYPE (expr
), 0));
6437 else if (COMPARISON_CLASS_P (expr
))
6438 return fold_build2 (TREE_CODE (expr
),
6440 TREE_OPERAND (expr
, 0),
6441 TREE_OPERAND (expr
, 1));
6447 /* Check to see if a boolean expression EXPR is logically equivalent to the
6448 comparison (OP1 CODE OP2). Check for various identities involving
6452 same_bool_comparison_p (const_tree expr
, enum tree_code code
,
6453 const_tree op1
, const_tree op2
)
6457 /* The obvious case. */
6458 if (TREE_CODE (expr
) == code
6459 && operand_equal_p (TREE_OPERAND (expr
, 0), op1
, 0)
6460 && operand_equal_p (TREE_OPERAND (expr
, 1), op2
, 0))
6463 /* Check for comparing (name, name != 0) and the case where expr
6464 is an SSA_NAME with a definition matching the comparison. */
6465 if (TREE_CODE (expr
) == SSA_NAME
6466 && TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
)
6468 if (operand_equal_p (expr
, op1
, 0))
6469 return ((code
== NE_EXPR
&& integer_zerop (op2
))
6470 || (code
== EQ_EXPR
&& integer_nonzerop (op2
)));
6471 s
= SSA_NAME_DEF_STMT (expr
);
6472 if (is_gimple_assign (s
)
6473 && gimple_assign_rhs_code (s
) == code
6474 && operand_equal_p (gimple_assign_rhs1 (s
), op1
, 0)
6475 && operand_equal_p (gimple_assign_rhs2 (s
), op2
, 0))
6479 /* If op1 is of the form (name != 0) or (name == 0), and the definition
6480 of name is a comparison, recurse. */
6481 if (TREE_CODE (op1
) == SSA_NAME
6482 && TREE_CODE (TREE_TYPE (op1
)) == BOOLEAN_TYPE
)
6484 s
= SSA_NAME_DEF_STMT (op1
);
6485 if (is_gimple_assign (s
)
6486 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
)
6488 enum tree_code c
= gimple_assign_rhs_code (s
);
6489 if ((c
== NE_EXPR
&& integer_zerop (op2
))
6490 || (c
== EQ_EXPR
&& integer_nonzerop (op2
)))
6491 return same_bool_comparison_p (expr
, c
,
6492 gimple_assign_rhs1 (s
),
6493 gimple_assign_rhs2 (s
));
6494 if ((c
== EQ_EXPR
&& integer_zerop (op2
))
6495 || (c
== NE_EXPR
&& integer_nonzerop (op2
)))
6496 return same_bool_comparison_p (expr
,
6497 invert_tree_comparison (c
, false),
6498 gimple_assign_rhs1 (s
),
6499 gimple_assign_rhs2 (s
));
6505 /* Check to see if two boolean expressions OP1 and OP2 are logically
6509 same_bool_result_p (const_tree op1
, const_tree op2
)
6511 /* Simple cases first. */
6512 if (operand_equal_p (op1
, op2
, 0))
6515 /* Check the cases where at least one of the operands is a comparison.
6516 These are a bit smarter than operand_equal_p in that they apply some
6517 identifies on SSA_NAMEs. */
6518 if (COMPARISON_CLASS_P (op2
)
6519 && same_bool_comparison_p (op1
, TREE_CODE (op2
),
6520 TREE_OPERAND (op2
, 0),
6521 TREE_OPERAND (op2
, 1)))
6523 if (COMPARISON_CLASS_P (op1
)
6524 && same_bool_comparison_p (op2
, TREE_CODE (op1
),
6525 TREE_OPERAND (op1
, 0),
6526 TREE_OPERAND (op1
, 1)))
6533 /* Forward declarations for some mutually recursive functions. */
6536 and_comparisons_1 (tree type
, enum tree_code code1
, tree op1a
, tree op1b
,
6537 enum tree_code code2
, tree op2a
, tree op2b
);
6539 and_var_with_comparison (tree type
, tree var
, bool invert
,
6540 enum tree_code code2
, tree op2a
, tree op2b
);
6542 and_var_with_comparison_1 (tree type
, gimple
*stmt
,
6543 enum tree_code code2
, tree op2a
, tree op2b
);
6545 or_comparisons_1 (tree
, enum tree_code code1
, tree op1a
, tree op1b
,
6546 enum tree_code code2
, tree op2a
, tree op2b
);
6548 or_var_with_comparison (tree
, tree var
, bool invert
,
6549 enum tree_code code2
, tree op2a
, tree op2b
);
6551 or_var_with_comparison_1 (tree
, gimple
*stmt
,
6552 enum tree_code code2
, tree op2a
, tree op2b
);
6554 /* Helper function for and_comparisons_1: try to simplify the AND of the
6555 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
6556 If INVERT is true, invert the value of the VAR before doing the AND.
6557 Return NULL_EXPR if we can't simplify this to a single expression. */
6560 and_var_with_comparison (tree type
, tree var
, bool invert
,
6561 enum tree_code code2
, tree op2a
, tree op2b
)
6564 gimple
*stmt
= SSA_NAME_DEF_STMT (var
);
6566 /* We can only deal with variables whose definitions are assignments. */
6567 if (!is_gimple_assign (stmt
))
6570 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
6571 !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
6572 Then we only have to consider the simpler non-inverted cases. */
6574 t
= or_var_with_comparison_1 (type
, stmt
,
6575 invert_tree_comparison (code2
, false),
6578 t
= and_var_with_comparison_1 (type
, stmt
, code2
, op2a
, op2b
);
6579 return canonicalize_bool (t
, invert
);
6582 /* Try to simplify the AND of the ssa variable defined by the assignment
6583 STMT with the comparison specified by (OP2A CODE2 OP2B).
6584 Return NULL_EXPR if we can't simplify this to a single expression. */
6587 and_var_with_comparison_1 (tree type
, gimple
*stmt
,
6588 enum tree_code code2
, tree op2a
, tree op2b
)
6590 tree var
= gimple_assign_lhs (stmt
);
6591 tree true_test_var
= NULL_TREE
;
6592 tree false_test_var
= NULL_TREE
;
6593 enum tree_code innercode
= gimple_assign_rhs_code (stmt
);
6595 /* Check for identities like (var AND (var == 0)) => false. */
6596 if (TREE_CODE (op2a
) == SSA_NAME
6597 && TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
)
6599 if ((code2
== NE_EXPR
&& integer_zerop (op2b
))
6600 || (code2
== EQ_EXPR
&& integer_nonzerop (op2b
)))
6602 true_test_var
= op2a
;
6603 if (var
== true_test_var
)
6606 else if ((code2
== EQ_EXPR
&& integer_zerop (op2b
))
6607 || (code2
== NE_EXPR
&& integer_nonzerop (op2b
)))
6609 false_test_var
= op2a
;
6610 if (var
== false_test_var
)
6611 return boolean_false_node
;
6615 /* If the definition is a comparison, recurse on it. */
6616 if (TREE_CODE_CLASS (innercode
) == tcc_comparison
)
6618 tree t
= and_comparisons_1 (type
, innercode
,
6619 gimple_assign_rhs1 (stmt
),
6620 gimple_assign_rhs2 (stmt
),
6628 /* If the definition is an AND or OR expression, we may be able to
6629 simplify by reassociating. */
6630 if (TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
6631 && (innercode
== BIT_AND_EXPR
|| innercode
== BIT_IOR_EXPR
))
6633 tree inner1
= gimple_assign_rhs1 (stmt
);
6634 tree inner2
= gimple_assign_rhs2 (stmt
);
6637 tree partial
= NULL_TREE
;
6638 bool is_and
= (innercode
== BIT_AND_EXPR
);
6640 /* Check for boolean identities that don't require recursive examination
6642 inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
6643 inner1 AND (inner1 OR inner2) => inner1
6644 !inner1 AND (inner1 AND inner2) => false
6645 !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
6646 Likewise for similar cases involving inner2. */
6647 if (inner1
== true_test_var
)
6648 return (is_and
? var
: inner1
);
6649 else if (inner2
== true_test_var
)
6650 return (is_and
? var
: inner2
);
6651 else if (inner1
== false_test_var
)
6653 ? boolean_false_node
6654 : and_var_with_comparison (type
, inner2
, false, code2
, op2a
,
6656 else if (inner2
== false_test_var
)
6658 ? boolean_false_node
6659 : and_var_with_comparison (type
, inner1
, false, code2
, op2a
,
6662 /* Next, redistribute/reassociate the AND across the inner tests.
6663 Compute the first partial result, (inner1 AND (op2a code op2b)) */
6664 if (TREE_CODE (inner1
) == SSA_NAME
6665 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner1
))
6666 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
6667 && (t
= maybe_fold_and_comparisons (type
, gimple_assign_rhs_code (s
),
6668 gimple_assign_rhs1 (s
),
6669 gimple_assign_rhs2 (s
),
6670 code2
, op2a
, op2b
)))
6672 /* Handle the AND case, where we are reassociating:
6673 (inner1 AND inner2) AND (op2a code2 op2b)
6675 If the partial result t is a constant, we win. Otherwise
6676 continue on to try reassociating with the other inner test. */
6679 if (integer_onep (t
))
6681 else if (integer_zerop (t
))
6682 return boolean_false_node
;
6685 /* Handle the OR case, where we are redistributing:
6686 (inner1 OR inner2) AND (op2a code2 op2b)
6687 => (t OR (inner2 AND (op2a code2 op2b))) */
6688 else if (integer_onep (t
))
6689 return boolean_true_node
;
6691 /* Save partial result for later. */
6695 /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
6696 if (TREE_CODE (inner2
) == SSA_NAME
6697 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner2
))
6698 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
6699 && (t
= maybe_fold_and_comparisons (type
, gimple_assign_rhs_code (s
),
6700 gimple_assign_rhs1 (s
),
6701 gimple_assign_rhs2 (s
),
6702 code2
, op2a
, op2b
)))
6704 /* Handle the AND case, where we are reassociating:
6705 (inner1 AND inner2) AND (op2a code2 op2b)
6706 => (inner1 AND t) */
6709 if (integer_onep (t
))
6711 else if (integer_zerop (t
))
6712 return boolean_false_node
;
6713 /* If both are the same, we can apply the identity
6715 else if (partial
&& same_bool_result_p (t
, partial
))
6719 /* Handle the OR case. where we are redistributing:
6720 (inner1 OR inner2) AND (op2a code2 op2b)
6721 => (t OR (inner1 AND (op2a code2 op2b)))
6722 => (t OR partial) */
6725 if (integer_onep (t
))
6726 return boolean_true_node
;
6729 /* We already got a simplification for the other
6730 operand to the redistributed OR expression. The
6731 interesting case is when at least one is false.
6732 Or, if both are the same, we can apply the identity
6734 if (integer_zerop (partial
))
6736 else if (integer_zerop (t
))
6738 else if (same_bool_result_p (t
, partial
))
6747 /* Try to simplify the AND of two comparisons defined by
6748 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
6749 If this can be done without constructing an intermediate value,
6750 return the resulting tree; otherwise NULL_TREE is returned.
6751 This function is deliberately asymmetric as it recurses on SSA_DEFs
6752 in the first comparison but not the second. */
6755 and_comparisons_1 (tree type
, enum tree_code code1
, tree op1a
, tree op1b
,
6756 enum tree_code code2
, tree op2a
, tree op2b
)
6758 tree truth_type
= truth_type_for (TREE_TYPE (op1a
));
6760 /* First check for ((x CODE1 y) AND (x CODE2 y)). */
6761 if (operand_equal_p (op1a
, op2a
, 0)
6762 && operand_equal_p (op1b
, op2b
, 0))
6764 /* Result will be either NULL_TREE, or a combined comparison. */
6765 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
6766 TRUTH_ANDIF_EXPR
, code1
, code2
,
6767 truth_type
, op1a
, op1b
);
6772 /* Likewise the swapped case of the above. */
6773 if (operand_equal_p (op1a
, op2b
, 0)
6774 && operand_equal_p (op1b
, op2a
, 0))
6776 /* Result will be either NULL_TREE, or a combined comparison. */
6777 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
6778 TRUTH_ANDIF_EXPR
, code1
,
6779 swap_tree_comparison (code2
),
6780 truth_type
, op1a
, op1b
);
6785 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
6786 NAME's definition is a truth value. See if there are any simplifications
6787 that can be done against the NAME's definition. */
6788 if (TREE_CODE (op1a
) == SSA_NAME
6789 && (code1
== NE_EXPR
|| code1
== EQ_EXPR
)
6790 && (integer_zerop (op1b
) || integer_onep (op1b
)))
6792 bool invert
= ((code1
== EQ_EXPR
&& integer_zerop (op1b
))
6793 || (code1
== NE_EXPR
&& integer_onep (op1b
)));
6794 gimple
*stmt
= SSA_NAME_DEF_STMT (op1a
);
6795 switch (gimple_code (stmt
))
6798 /* Try to simplify by copy-propagating the definition. */
6799 return and_var_with_comparison (type
, op1a
, invert
, code2
, op2a
,
6803 /* If every argument to the PHI produces the same result when
6804 ANDed with the second comparison, we win.
6805 Do not do this unless the type is bool since we need a bool
6806 result here anyway. */
6807 if (TREE_CODE (TREE_TYPE (op1a
)) == BOOLEAN_TYPE
)
6809 tree result
= NULL_TREE
;
6811 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
6813 tree arg
= gimple_phi_arg_def (stmt
, i
);
6815 /* If this PHI has itself as an argument, ignore it.
6816 If all the other args produce the same result,
6818 if (arg
== gimple_phi_result (stmt
))
6820 else if (TREE_CODE (arg
) == INTEGER_CST
)
6822 if (invert
? integer_nonzerop (arg
) : integer_zerop (arg
))
6825 result
= boolean_false_node
;
6826 else if (!integer_zerop (result
))
6830 result
= fold_build2 (code2
, boolean_type_node
,
6832 else if (!same_bool_comparison_p (result
,
6836 else if (TREE_CODE (arg
) == SSA_NAME
6837 && !SSA_NAME_IS_DEFAULT_DEF (arg
))
6840 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
6841 /* In simple cases we can look through PHI nodes,
6842 but we have to be careful with loops.
6844 if (! dom_info_available_p (CDI_DOMINATORS
)
6845 || gimple_bb (def_stmt
) == gimple_bb (stmt
)
6846 || dominated_by_p (CDI_DOMINATORS
,
6847 gimple_bb (def_stmt
),
6850 temp
= and_var_with_comparison (type
, arg
, invert
, code2
,
6856 else if (!same_bool_result_p (result
, temp
))
6872 /* Helper function for maybe_fold_and_comparisons and maybe_fold_or_comparisons
6873 : try to simplify the AND/OR of the ssa variable VAR with the comparison
6874 specified by (OP2A CODE2 OP2B) from match.pd. Return NULL_EXPR if we can't
6875 simplify this to a single expression. As we are going to lower the cost
6876 of building SSA names / gimple stmts significantly, we need to allocate
6877 them ont the stack. This will cause the code to be a bit ugly. */
6880 maybe_fold_comparisons_from_match_pd (tree type
, enum tree_code code
,
6881 enum tree_code code1
,
6882 tree op1a
, tree op1b
,
6883 enum tree_code code2
, tree op2a
,
6886 /* Allocate gimple stmt1 on the stack. */
6888 = (gassign
*) XALLOCAVEC (char, gimple_size (GIMPLE_ASSIGN
, 3));
6889 gimple_init (stmt1
, GIMPLE_ASSIGN
, 3);
6890 gimple_assign_set_rhs_code (stmt1
, code1
);
6891 gimple_assign_set_rhs1 (stmt1
, op1a
);
6892 gimple_assign_set_rhs2 (stmt1
, op1b
);
6894 /* Allocate gimple stmt2 on the stack. */
6896 = (gassign
*) XALLOCAVEC (char, gimple_size (GIMPLE_ASSIGN
, 3));
6897 gimple_init (stmt2
, GIMPLE_ASSIGN
, 3);
6898 gimple_assign_set_rhs_code (stmt2
, code2
);
6899 gimple_assign_set_rhs1 (stmt2
, op2a
);
6900 gimple_assign_set_rhs2 (stmt2
, op2b
);
6902 /* Allocate SSA names(lhs1) on the stack. */
6903 tree lhs1
= (tree
)XALLOCA (tree_ssa_name
);
6904 memset (lhs1
, 0, sizeof (tree_ssa_name
));
6905 TREE_SET_CODE (lhs1
, SSA_NAME
);
6906 TREE_TYPE (lhs1
) = type
;
6907 init_ssa_name_imm_use (lhs1
);
6909 /* Allocate SSA names(lhs2) on the stack. */
6910 tree lhs2
= (tree
)XALLOCA (tree_ssa_name
);
6911 memset (lhs2
, 0, sizeof (tree_ssa_name
));
6912 TREE_SET_CODE (lhs2
, SSA_NAME
);
6913 TREE_TYPE (lhs2
) = type
;
6914 init_ssa_name_imm_use (lhs2
);
6916 gimple_assign_set_lhs (stmt1
, lhs1
);
6917 gimple_assign_set_lhs (stmt2
, lhs2
);
6919 gimple_match_op
op (gimple_match_cond::UNCOND
, code
,
6920 type
, gimple_assign_lhs (stmt1
),
6921 gimple_assign_lhs (stmt2
));
6922 if (op
.resimplify (NULL
, follow_all_ssa_edges
))
6924 if (gimple_simplified_result_is_gimple_val (&op
))
6926 tree res
= op
.ops
[0];
6928 return build2 (code1
, type
, op1a
, op1b
);
6929 else if (res
== lhs2
)
6930 return build2 (code2
, type
, op2a
, op2b
);
6934 else if (op
.code
.is_tree_code ()
6935 && TREE_CODE_CLASS ((tree_code
)op
.code
) == tcc_comparison
)
6937 tree op0
= op
.ops
[0];
6938 tree op1
= op
.ops
[1];
6939 if (op0
== lhs1
|| op0
== lhs2
|| op1
== lhs1
|| op1
== lhs2
)
6940 return NULL_TREE
; /* not simple */
6942 return build2 ((enum tree_code
)op
.code
, op
.type
, op0
, op1
);
6949 /* Try to simplify the AND of two comparisons, specified by
6950 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
6951 If this can be simplified to a single expression (without requiring
6952 introducing more SSA variables to hold intermediate values),
6953 return the resulting tree. Otherwise return NULL_TREE.
6954 If the result expression is non-null, it has boolean type. */
6957 maybe_fold_and_comparisons (tree type
,
6958 enum tree_code code1
, tree op1a
, tree op1b
,
6959 enum tree_code code2
, tree op2a
, tree op2b
)
6961 if (tree t
= and_comparisons_1 (type
, code1
, op1a
, op1b
, code2
, op2a
, op2b
))
6964 if (tree t
= and_comparisons_1 (type
, code2
, op2a
, op2b
, code1
, op1a
, op1b
))
6967 if (tree t
= maybe_fold_comparisons_from_match_pd (type
, BIT_AND_EXPR
, code1
,
6968 op1a
, op1b
, code2
, op2a
,
6975 /* Helper function for or_comparisons_1: try to simplify the OR of the
6976 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
6977 If INVERT is true, invert the value of VAR before doing the OR.
6978 Return NULL_EXPR if we can't simplify this to a single expression. */
6981 or_var_with_comparison (tree type
, tree var
, bool invert
,
6982 enum tree_code code2
, tree op2a
, tree op2b
)
6985 gimple
*stmt
= SSA_NAME_DEF_STMT (var
);
6987 /* We can only deal with variables whose definitions are assignments. */
6988 if (!is_gimple_assign (stmt
))
6991 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
6992 !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
6993 Then we only have to consider the simpler non-inverted cases. */
6995 t
= and_var_with_comparison_1 (type
, stmt
,
6996 invert_tree_comparison (code2
, false),
6999 t
= or_var_with_comparison_1 (type
, stmt
, code2
, op2a
, op2b
);
7000 return canonicalize_bool (t
, invert
);
7003 /* Try to simplify the OR of the ssa variable defined by the assignment
7004 STMT with the comparison specified by (OP2A CODE2 OP2B).
7005 Return NULL_EXPR if we can't simplify this to a single expression. */
7008 or_var_with_comparison_1 (tree type
, gimple
*stmt
,
7009 enum tree_code code2
, tree op2a
, tree op2b
)
7011 tree var
= gimple_assign_lhs (stmt
);
7012 tree true_test_var
= NULL_TREE
;
7013 tree false_test_var
= NULL_TREE
;
7014 enum tree_code innercode
= gimple_assign_rhs_code (stmt
);
7016 /* Check for identities like (var OR (var != 0)) => true . */
7017 if (TREE_CODE (op2a
) == SSA_NAME
7018 && TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
)
7020 if ((code2
== NE_EXPR
&& integer_zerop (op2b
))
7021 || (code2
== EQ_EXPR
&& integer_nonzerop (op2b
)))
7023 true_test_var
= op2a
;
7024 if (var
== true_test_var
)
7027 else if ((code2
== EQ_EXPR
&& integer_zerop (op2b
))
7028 || (code2
== NE_EXPR
&& integer_nonzerop (op2b
)))
7030 false_test_var
= op2a
;
7031 if (var
== false_test_var
)
7032 return boolean_true_node
;
7036 /* If the definition is a comparison, recurse on it. */
7037 if (TREE_CODE_CLASS (innercode
) == tcc_comparison
)
7039 tree t
= or_comparisons_1 (type
, innercode
,
7040 gimple_assign_rhs1 (stmt
),
7041 gimple_assign_rhs2 (stmt
),
7049 /* If the definition is an AND or OR expression, we may be able to
7050 simplify by reassociating. */
7051 if (TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
7052 && (innercode
== BIT_AND_EXPR
|| innercode
== BIT_IOR_EXPR
))
7054 tree inner1
= gimple_assign_rhs1 (stmt
);
7055 tree inner2
= gimple_assign_rhs2 (stmt
);
7058 tree partial
= NULL_TREE
;
7059 bool is_or
= (innercode
== BIT_IOR_EXPR
);
7061 /* Check for boolean identities that don't require recursive examination
7063 inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
7064 inner1 OR (inner1 AND inner2) => inner1
7065 !inner1 OR (inner1 OR inner2) => true
7066 !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
7068 if (inner1
== true_test_var
)
7069 return (is_or
? var
: inner1
);
7070 else if (inner2
== true_test_var
)
7071 return (is_or
? var
: inner2
);
7072 else if (inner1
== false_test_var
)
7075 : or_var_with_comparison (type
, inner2
, false, code2
, op2a
,
7077 else if (inner2
== false_test_var
)
7080 : or_var_with_comparison (type
, inner1
, false, code2
, op2a
,
7083 /* Next, redistribute/reassociate the OR across the inner tests.
7084 Compute the first partial result, (inner1 OR (op2a code op2b)) */
7085 if (TREE_CODE (inner1
) == SSA_NAME
7086 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner1
))
7087 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
7088 && (t
= maybe_fold_or_comparisons (type
, gimple_assign_rhs_code (s
),
7089 gimple_assign_rhs1 (s
),
7090 gimple_assign_rhs2 (s
),
7091 code2
, op2a
, op2b
)))
7093 /* Handle the OR case, where we are reassociating:
7094 (inner1 OR inner2) OR (op2a code2 op2b)
7096 If the partial result t is a constant, we win. Otherwise
7097 continue on to try reassociating with the other inner test. */
7100 if (integer_onep (t
))
7101 return boolean_true_node
;
7102 else if (integer_zerop (t
))
7106 /* Handle the AND case, where we are redistributing:
7107 (inner1 AND inner2) OR (op2a code2 op2b)
7108 => (t AND (inner2 OR (op2a code op2b))) */
7109 else if (integer_zerop (t
))
7110 return boolean_false_node
;
7112 /* Save partial result for later. */
7116 /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
7117 if (TREE_CODE (inner2
) == SSA_NAME
7118 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner2
))
7119 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
7120 && (t
= maybe_fold_or_comparisons (type
, gimple_assign_rhs_code (s
),
7121 gimple_assign_rhs1 (s
),
7122 gimple_assign_rhs2 (s
),
7123 code2
, op2a
, op2b
)))
7125 /* Handle the OR case, where we are reassociating:
7126 (inner1 OR inner2) OR (op2a code2 op2b)
7128 => (t OR partial) */
7131 if (integer_zerop (t
))
7133 else if (integer_onep (t
))
7134 return boolean_true_node
;
7135 /* If both are the same, we can apply the identity
7137 else if (partial
&& same_bool_result_p (t
, partial
))
7141 /* Handle the AND case, where we are redistributing:
7142 (inner1 AND inner2) OR (op2a code2 op2b)
7143 => (t AND (inner1 OR (op2a code2 op2b)))
7144 => (t AND partial) */
7147 if (integer_zerop (t
))
7148 return boolean_false_node
;
7151 /* We already got a simplification for the other
7152 operand to the redistributed AND expression. The
7153 interesting case is when at least one is true.
7154 Or, if both are the same, we can apply the identity
7156 if (integer_onep (partial
))
7158 else if (integer_onep (t
))
7160 else if (same_bool_result_p (t
, partial
))
7169 /* Try to simplify the OR of two comparisons defined by
7170 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
7171 If this can be done without constructing an intermediate value,
7172 return the resulting tree; otherwise NULL_TREE is returned.
7173 This function is deliberately asymmetric as it recurses on SSA_DEFs
7174 in the first comparison but not the second. */
7177 or_comparisons_1 (tree type
, enum tree_code code1
, tree op1a
, tree op1b
,
7178 enum tree_code code2
, tree op2a
, tree op2b
)
7180 tree truth_type
= truth_type_for (TREE_TYPE (op1a
));
7182 /* First check for ((x CODE1 y) OR (x CODE2 y)). */
7183 if (operand_equal_p (op1a
, op2a
, 0)
7184 && operand_equal_p (op1b
, op2b
, 0))
7186 /* Result will be either NULL_TREE, or a combined comparison. */
7187 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
7188 TRUTH_ORIF_EXPR
, code1
, code2
,
7189 truth_type
, op1a
, op1b
);
7194 /* Likewise the swapped case of the above. */
7195 if (operand_equal_p (op1a
, op2b
, 0)
7196 && operand_equal_p (op1b
, op2a
, 0))
7198 /* Result will be either NULL_TREE, or a combined comparison. */
7199 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
7200 TRUTH_ORIF_EXPR
, code1
,
7201 swap_tree_comparison (code2
),
7202 truth_type
, op1a
, op1b
);
7207 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
7208 NAME's definition is a truth value. See if there are any simplifications
7209 that can be done against the NAME's definition. */
7210 if (TREE_CODE (op1a
) == SSA_NAME
7211 && (code1
== NE_EXPR
|| code1
== EQ_EXPR
)
7212 && (integer_zerop (op1b
) || integer_onep (op1b
)))
7214 bool invert
= ((code1
== EQ_EXPR
&& integer_zerop (op1b
))
7215 || (code1
== NE_EXPR
&& integer_onep (op1b
)));
7216 gimple
*stmt
= SSA_NAME_DEF_STMT (op1a
);
7217 switch (gimple_code (stmt
))
7220 /* Try to simplify by copy-propagating the definition. */
7221 return or_var_with_comparison (type
, op1a
, invert
, code2
, op2a
,
7225 /* If every argument to the PHI produces the same result when
7226 ORed with the second comparison, we win.
7227 Do not do this unless the type is bool since we need a bool
7228 result here anyway. */
7229 if (TREE_CODE (TREE_TYPE (op1a
)) == BOOLEAN_TYPE
)
7231 tree result
= NULL_TREE
;
7233 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
7235 tree arg
= gimple_phi_arg_def (stmt
, i
);
7237 /* If this PHI has itself as an argument, ignore it.
7238 If all the other args produce the same result,
7240 if (arg
== gimple_phi_result (stmt
))
7242 else if (TREE_CODE (arg
) == INTEGER_CST
)
7244 if (invert
? integer_zerop (arg
) : integer_nonzerop (arg
))
7247 result
= boolean_true_node
;
7248 else if (!integer_onep (result
))
7252 result
= fold_build2 (code2
, boolean_type_node
,
7254 else if (!same_bool_comparison_p (result
,
7258 else if (TREE_CODE (arg
) == SSA_NAME
7259 && !SSA_NAME_IS_DEFAULT_DEF (arg
))
7262 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
7263 /* In simple cases we can look through PHI nodes,
7264 but we have to be careful with loops.
7266 if (! dom_info_available_p (CDI_DOMINATORS
)
7267 || gimple_bb (def_stmt
) == gimple_bb (stmt
)
7268 || dominated_by_p (CDI_DOMINATORS
,
7269 gimple_bb (def_stmt
),
7272 temp
= or_var_with_comparison (type
, arg
, invert
, code2
,
7278 else if (!same_bool_result_p (result
, temp
))
7294 /* Try to simplify the OR of two comparisons, specified by
7295 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
7296 If this can be simplified to a single expression (without requiring
7297 introducing more SSA variables to hold intermediate values),
7298 return the resulting tree. Otherwise return NULL_TREE.
7299 If the result expression is non-null, it has boolean type. */
7302 maybe_fold_or_comparisons (tree type
,
7303 enum tree_code code1
, tree op1a
, tree op1b
,
7304 enum tree_code code2
, tree op2a
, tree op2b
)
7306 if (tree t
= or_comparisons_1 (type
, code1
, op1a
, op1b
, code2
, op2a
, op2b
))
7309 if (tree t
= or_comparisons_1 (type
, code2
, op2a
, op2b
, code1
, op1a
, op1b
))
7312 if (tree t
= maybe_fold_comparisons_from_match_pd (type
, BIT_IOR_EXPR
, code1
,
7313 op1a
, op1b
, code2
, op2a
,
7320 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
7322 Either NULL_TREE, a simplified but non-constant or a constant
7325 ??? This should go into a gimple-fold-inline.h file to be eventually
7326 privatized with the single valueize function used in the various TUs
7327 to avoid the indirect function call overhead. */
7330 gimple_fold_stmt_to_constant_1 (gimple
*stmt
, tree (*valueize
) (tree
),
7331 tree (*gvalueize
) (tree
))
7333 gimple_match_op res_op
;
7334 /* ??? The SSA propagators do not correctly deal with following SSA use-def
7335 edges if there are intermediate VARYING defs. For this reason
7336 do not follow SSA edges here even though SCCVN can technically
7337 just deal fine with that. */
7338 if (gimple_simplify (stmt
, &res_op
, NULL
, gvalueize
, valueize
))
7340 tree res
= NULL_TREE
;
7341 if (gimple_simplified_result_is_gimple_val (&res_op
))
7342 res
= res_op
.ops
[0];
7343 else if (mprts_hook
)
7344 res
= mprts_hook (&res_op
);
7347 if (dump_file
&& dump_flags
& TDF_DETAILS
)
7349 fprintf (dump_file
, "Match-and-simplified ");
7350 print_gimple_expr (dump_file
, stmt
, 0, TDF_SLIM
);
7351 fprintf (dump_file
, " to ");
7352 print_generic_expr (dump_file
, res
);
7353 fprintf (dump_file
, "\n");
7359 location_t loc
= gimple_location (stmt
);
7360 switch (gimple_code (stmt
))
7364 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
7366 switch (get_gimple_rhs_class (subcode
))
7368 case GIMPLE_SINGLE_RHS
:
7370 tree rhs
= gimple_assign_rhs1 (stmt
);
7371 enum tree_code_class kind
= TREE_CODE_CLASS (subcode
);
7373 if (TREE_CODE (rhs
) == SSA_NAME
)
7375 /* If the RHS is an SSA_NAME, return its known constant value,
7377 return (*valueize
) (rhs
);
7379 /* Handle propagating invariant addresses into address
7381 else if (TREE_CODE (rhs
) == ADDR_EXPR
7382 && !is_gimple_min_invariant (rhs
))
7384 poly_int64 offset
= 0;
7386 base
= get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs
, 0),
7390 && (CONSTANT_CLASS_P (base
)
7391 || decl_address_invariant_p (base
)))
7392 return build_invariant_address (TREE_TYPE (rhs
),
7395 else if (TREE_CODE (rhs
) == CONSTRUCTOR
7396 && TREE_CODE (TREE_TYPE (rhs
)) == VECTOR_TYPE
7397 && known_eq (CONSTRUCTOR_NELTS (rhs
),
7398 TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs
))))
7403 nelts
= CONSTRUCTOR_NELTS (rhs
);
7404 tree_vector_builder
vec (TREE_TYPE (rhs
), nelts
, 1);
7405 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs
), i
, val
)
7407 val
= (*valueize
) (val
);
7408 if (TREE_CODE (val
) == INTEGER_CST
7409 || TREE_CODE (val
) == REAL_CST
7410 || TREE_CODE (val
) == FIXED_CST
)
7411 vec
.quick_push (val
);
7416 return vec
.build ();
7418 if (subcode
== OBJ_TYPE_REF
)
7420 tree val
= (*valueize
) (OBJ_TYPE_REF_EXPR (rhs
));
7421 /* If callee is constant, we can fold away the wrapper. */
7422 if (is_gimple_min_invariant (val
))
7426 if (kind
== tcc_reference
)
7428 if ((TREE_CODE (rhs
) == VIEW_CONVERT_EXPR
7429 || TREE_CODE (rhs
) == REALPART_EXPR
7430 || TREE_CODE (rhs
) == IMAGPART_EXPR
)
7431 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
7433 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
7434 return fold_unary_loc (EXPR_LOCATION (rhs
),
7436 TREE_TYPE (rhs
), val
);
7438 else if (TREE_CODE (rhs
) == BIT_FIELD_REF
7439 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
7441 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
7442 return fold_ternary_loc (EXPR_LOCATION (rhs
),
7444 TREE_TYPE (rhs
), val
,
7445 TREE_OPERAND (rhs
, 1),
7446 TREE_OPERAND (rhs
, 2));
7448 else if (TREE_CODE (rhs
) == MEM_REF
7449 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
7451 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
7452 if (TREE_CODE (val
) == ADDR_EXPR
7453 && is_gimple_min_invariant (val
))
7455 tree tem
= fold_build2 (MEM_REF
, TREE_TYPE (rhs
),
7457 TREE_OPERAND (rhs
, 1));
7462 return fold_const_aggregate_ref_1 (rhs
, valueize
);
7464 else if (kind
== tcc_declaration
)
7465 return get_symbol_constant_value (rhs
);
7469 case GIMPLE_UNARY_RHS
:
7472 case GIMPLE_BINARY_RHS
:
7473 /* Translate &x + CST into an invariant form suitable for
7474 further propagation. */
7475 if (subcode
== POINTER_PLUS_EXPR
)
7477 tree op0
= (*valueize
) (gimple_assign_rhs1 (stmt
));
7478 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
7479 if (TREE_CODE (op0
) == ADDR_EXPR
7480 && TREE_CODE (op1
) == INTEGER_CST
)
7482 tree off
= fold_convert (ptr_type_node
, op1
);
7484 (loc
, ADDR_EXPR
, TREE_TYPE (op0
),
7485 fold_build2 (MEM_REF
,
7486 TREE_TYPE (TREE_TYPE (op0
)),
7487 unshare_expr (op0
), off
));
7490 /* Canonicalize bool != 0 and bool == 0 appearing after
7491 valueization. While gimple_simplify handles this
7492 it can get confused by the ~X == 1 -> X == 0 transform
7493 which we cant reduce to a SSA name or a constant
7494 (and we have no way to tell gimple_simplify to not
7495 consider those transforms in the first place). */
7496 else if (subcode
== EQ_EXPR
7497 || subcode
== NE_EXPR
)
7499 tree lhs
= gimple_assign_lhs (stmt
);
7500 tree op0
= gimple_assign_rhs1 (stmt
);
7501 if (useless_type_conversion_p (TREE_TYPE (lhs
),
7504 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
7505 op0
= (*valueize
) (op0
);
7506 if (TREE_CODE (op0
) == INTEGER_CST
)
7507 std::swap (op0
, op1
);
7508 if (TREE_CODE (op1
) == INTEGER_CST
7509 && ((subcode
== NE_EXPR
&& integer_zerop (op1
))
7510 || (subcode
== EQ_EXPR
&& integer_onep (op1
))))
7516 case GIMPLE_TERNARY_RHS
:
7518 /* Handle ternary operators that can appear in GIMPLE form. */
7519 tree op0
= (*valueize
) (gimple_assign_rhs1 (stmt
));
7520 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
7521 tree op2
= (*valueize
) (gimple_assign_rhs3 (stmt
));
7522 return fold_ternary_loc (loc
, subcode
,
7523 TREE_TYPE (gimple_assign_lhs (stmt
)),
7535 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
7537 if (gimple_call_internal_p (stmt
))
7539 enum tree_code subcode
= ERROR_MARK
;
7540 switch (gimple_call_internal_fn (stmt
))
7542 case IFN_UBSAN_CHECK_ADD
:
7543 subcode
= PLUS_EXPR
;
7545 case IFN_UBSAN_CHECK_SUB
:
7546 subcode
= MINUS_EXPR
;
7548 case IFN_UBSAN_CHECK_MUL
:
7549 subcode
= MULT_EXPR
;
7551 case IFN_BUILTIN_EXPECT
:
7553 tree arg0
= gimple_call_arg (stmt
, 0);
7554 tree op0
= (*valueize
) (arg0
);
7555 if (TREE_CODE (op0
) == INTEGER_CST
)
7562 tree arg0
= gimple_call_arg (stmt
, 0);
7563 tree arg1
= gimple_call_arg (stmt
, 1);
7564 tree op0
= (*valueize
) (arg0
);
7565 tree op1
= (*valueize
) (arg1
);
7567 if (TREE_CODE (op0
) != INTEGER_CST
7568 || TREE_CODE (op1
) != INTEGER_CST
)
7573 /* x * 0 = 0 * x = 0 without overflow. */
7574 if (integer_zerop (op0
) || integer_zerop (op1
))
7575 return build_zero_cst (TREE_TYPE (arg0
));
7578 /* y - y = 0 without overflow. */
7579 if (operand_equal_p (op0
, op1
, 0))
7580 return build_zero_cst (TREE_TYPE (arg0
));
7587 = fold_binary_loc (loc
, subcode
, TREE_TYPE (arg0
), op0
, op1
);
7589 && TREE_CODE (res
) == INTEGER_CST
7590 && !TREE_OVERFLOW (res
))
7595 fn
= (*valueize
) (gimple_call_fn (stmt
));
7596 if (TREE_CODE (fn
) == ADDR_EXPR
7597 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
7598 && fndecl_built_in_p (TREE_OPERAND (fn
, 0))
7599 && gimple_builtin_call_types_compatible_p (stmt
,
7600 TREE_OPERAND (fn
, 0)))
7602 tree
*args
= XALLOCAVEC (tree
, gimple_call_num_args (stmt
));
7605 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
7606 args
[i
] = (*valueize
) (gimple_call_arg (stmt
, i
));
7607 retval
= fold_builtin_call_array (loc
,
7608 gimple_call_return_type (call_stmt
),
7609 fn
, gimple_call_num_args (stmt
), args
);
7612 /* fold_call_expr wraps the result inside a NOP_EXPR. */
7613 STRIP_NOPS (retval
);
7614 retval
= fold_convert (gimple_call_return_type (call_stmt
),
7627 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
7628 Returns NULL_TREE if folding to a constant is not possible, otherwise
7629 returns a constant according to is_gimple_min_invariant. */
7632 gimple_fold_stmt_to_constant (gimple
*stmt
, tree (*valueize
) (tree
))
7634 tree res
= gimple_fold_stmt_to_constant_1 (stmt
, valueize
);
7635 if (res
&& is_gimple_min_invariant (res
))
7641 /* The following set of functions are supposed to fold references using
7642 their constant initializers. */
7644 /* See if we can find constructor defining value of BASE.
7645 When we know the consructor with constant offset (such as
7646 base is array[40] and we do know constructor of array), then
7647 BIT_OFFSET is adjusted accordingly.
7649 As a special case, return error_mark_node when constructor
7650 is not explicitly available, but it is known to be zero
7651 such as 'static const int a;'. */
7653 get_base_constructor (tree base
, poly_int64_pod
*bit_offset
,
7654 tree (*valueize
)(tree
))
7656 poly_int64 bit_offset2
, size
, max_size
;
7659 if (TREE_CODE (base
) == MEM_REF
)
7661 poly_offset_int boff
= *bit_offset
+ mem_ref_offset (base
) * BITS_PER_UNIT
;
7662 if (!boff
.to_shwi (bit_offset
))
7666 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
7667 base
= valueize (TREE_OPERAND (base
, 0));
7668 if (!base
|| TREE_CODE (base
) != ADDR_EXPR
)
7670 base
= TREE_OPERAND (base
, 0);
7673 && TREE_CODE (base
) == SSA_NAME
)
7674 base
= valueize (base
);
7676 /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
7677 DECL_INITIAL. If BASE is a nested reference into another
7678 ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
7679 the inner reference. */
7680 switch (TREE_CODE (base
))
7685 tree init
= ctor_for_folding (base
);
7687 /* Our semantic is exact opposite of ctor_for_folding;
7688 NULL means unknown, while error_mark_node is 0. */
7689 if (init
== error_mark_node
)
7692 return error_mark_node
;
7696 case VIEW_CONVERT_EXPR
:
7697 return get_base_constructor (TREE_OPERAND (base
, 0),
7698 bit_offset
, valueize
);
7702 base
= get_ref_base_and_extent (base
, &bit_offset2
, &size
, &max_size
,
7704 if (!known_size_p (max_size
) || maybe_ne (size
, max_size
))
7706 *bit_offset
+= bit_offset2
;
7707 return get_base_constructor (base
, bit_offset
, valueize
);
7713 if (CONSTANT_CLASS_P (base
))
7720 /* CTOR is CONSTRUCTOR of an array type. Fold a reference of SIZE bits
7721 to the memory at bit OFFSET. When non-null, TYPE is the expected
7722 type of the reference; otherwise the type of the referenced element
7723 is used instead. When SIZE is zero, attempt to fold a reference to
7724 the entire element which OFFSET refers to. Increment *SUBOFF by
7725 the bit offset of the accessed element. */
7728 fold_array_ctor_reference (tree type
, tree ctor
,
7729 unsigned HOST_WIDE_INT offset
,
7730 unsigned HOST_WIDE_INT size
,
7732 unsigned HOST_WIDE_INT
*suboff
)
7734 offset_int low_bound
;
7735 offset_int elt_size
;
7736 offset_int access_index
;
7737 tree domain_type
= NULL_TREE
;
7738 HOST_WIDE_INT inner_offset
;
7740 /* Compute low bound and elt size. */
7741 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
)
7742 domain_type
= TYPE_DOMAIN (TREE_TYPE (ctor
));
7743 if (domain_type
&& TYPE_MIN_VALUE (domain_type
))
7745 /* Static constructors for variably sized objects make no sense. */
7746 if (TREE_CODE (TYPE_MIN_VALUE (domain_type
)) != INTEGER_CST
)
7748 low_bound
= wi::to_offset (TYPE_MIN_VALUE (domain_type
));
7752 /* Static constructors for variably sized objects make no sense. */
7753 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor
)))) != INTEGER_CST
)
7755 elt_size
= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor
))));
7757 /* When TYPE is non-null, verify that it specifies a constant-sized
7758 access of a multiple of the array element size. Avoid division
7759 by zero below when ELT_SIZE is zero, such as with the result of
7760 an initializer for a zero-length array or an empty struct. */
7763 && (!TYPE_SIZE_UNIT (type
)
7764 || TREE_CODE (TYPE_SIZE_UNIT (type
)) != INTEGER_CST
)))
7767 /* Compute the array index we look for. */
7768 access_index
= wi::udiv_trunc (offset_int (offset
/ BITS_PER_UNIT
),
7770 access_index
+= low_bound
;
7772 /* And offset within the access. */
7773 inner_offset
= offset
% (elt_size
.to_uhwi () * BITS_PER_UNIT
);
7775 unsigned HOST_WIDE_INT elt_sz
= elt_size
.to_uhwi ();
7776 if (size
> elt_sz
* BITS_PER_UNIT
)
7778 /* native_encode_expr constraints. */
7779 if (size
> MAX_BITSIZE_MODE_ANY_MODE
7780 || size
% BITS_PER_UNIT
!= 0
7781 || inner_offset
% BITS_PER_UNIT
!= 0
7782 || elt_sz
> MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
)
7786 tree val
= get_array_ctor_element_at_index (ctor
, access_index
,
7788 if (!val
&& ctor_idx
>= CONSTRUCTOR_NELTS (ctor
))
7789 return build_zero_cst (type
);
7791 /* native-encode adjacent ctor elements. */
7792 unsigned char buf
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
7793 unsigned bufoff
= 0;
7794 offset_int index
= 0;
7795 offset_int max_index
= access_index
;
7796 constructor_elt
*elt
= CONSTRUCTOR_ELT (ctor
, ctor_idx
);
7798 val
= build_zero_cst (TREE_TYPE (TREE_TYPE (ctor
)));
7799 else if (!CONSTANT_CLASS_P (val
))
7803 else if (TREE_CODE (elt
->index
) == RANGE_EXPR
)
7805 index
= wi::to_offset (TREE_OPERAND (elt
->index
, 0));
7806 max_index
= wi::to_offset (TREE_OPERAND (elt
->index
, 1));
7809 index
= max_index
= wi::to_offset (elt
->index
);
7810 index
= wi::umax (index
, access_index
);
7813 if (bufoff
+ elt_sz
> sizeof (buf
))
7814 elt_sz
= sizeof (buf
) - bufoff
;
7815 int len
= native_encode_expr (val
, buf
+ bufoff
, elt_sz
,
7816 inner_offset
/ BITS_PER_UNIT
);
7817 if (len
!= (int) elt_sz
- inner_offset
/ BITS_PER_UNIT
)
7823 if (wi::cmpu (access_index
, index
) == 0)
7825 else if (wi::cmpu (access_index
, max_index
) > 0)
7828 if (ctor_idx
>= CONSTRUCTOR_NELTS (ctor
))
7830 val
= build_zero_cst (TREE_TYPE (TREE_TYPE (ctor
)));
7835 elt
= CONSTRUCTOR_ELT (ctor
, ctor_idx
);
7837 max_index
= access_index
;
7840 else if (TREE_CODE (elt
->index
) == RANGE_EXPR
)
7842 index
= wi::to_offset (TREE_OPERAND (elt
->index
, 0));
7843 max_index
= wi::to_offset (TREE_OPERAND (elt
->index
, 1));
7846 index
= max_index
= wi::to_offset (elt
->index
);
7847 index
= wi::umax (index
, access_index
);
7848 if (wi::cmpu (access_index
, index
) == 0)
7851 val
= build_zero_cst (TREE_TYPE (TREE_TYPE (ctor
)));
7855 while (bufoff
< size
/ BITS_PER_UNIT
);
7857 return native_interpret_expr (type
, buf
, size
/ BITS_PER_UNIT
);
7860 if (tree val
= get_array_ctor_element_at_index (ctor
, access_index
))
7862 if (!size
&& TREE_CODE (val
) != CONSTRUCTOR
)
7864 /* For the final reference to the entire accessed element
7865 (SIZE is zero), reset INNER_OFFSET, disegard TYPE (which
7866 may be null) in favor of the type of the element, and set
7867 SIZE to the size of the accessed element. */
7869 type
= TREE_TYPE (val
);
7870 size
= elt_sz
* BITS_PER_UNIT
;
7872 else if (size
&& access_index
< CONSTRUCTOR_NELTS (ctor
) - 1
7873 && TREE_CODE (val
) == CONSTRUCTOR
7874 && (elt_sz
* BITS_PER_UNIT
- inner_offset
) < size
)
7875 /* If this isn't the last element in the CTOR and a CTOR itself
7876 and it does not cover the whole object we are requesting give up
7877 since we're not set up for combining from multiple CTORs. */
7880 *suboff
+= access_index
.to_uhwi () * elt_sz
* BITS_PER_UNIT
;
7881 return fold_ctor_reference (type
, val
, inner_offset
, size
, from_decl
,
7885 /* Memory not explicitly mentioned in constructor is 0 (or
7886 the reference is out of range). */
7887 return type
? build_zero_cst (type
) : NULL_TREE
;
7890 /* CTOR is CONSTRUCTOR of an aggregate or vector. Fold a reference
7891 of SIZE bits to the memory at bit OFFSET. When non-null, TYPE
7892 is the expected type of the reference; otherwise the type of
7893 the referenced member is used instead. When SIZE is zero,
7894 attempt to fold a reference to the entire member which OFFSET
7895 refers to; in this case. Increment *SUBOFF by the bit offset
7896 of the accessed member. */
7899 fold_nonarray_ctor_reference (tree type
, tree ctor
,
7900 unsigned HOST_WIDE_INT offset
,
7901 unsigned HOST_WIDE_INT size
,
7903 unsigned HOST_WIDE_INT
*suboff
)
7905 unsigned HOST_WIDE_INT cnt
;
7908 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
,
7911 tree byte_offset
= DECL_FIELD_OFFSET (cfield
);
7912 tree field_offset
= DECL_FIELD_BIT_OFFSET (cfield
);
7913 tree field_size
= DECL_SIZE (cfield
);
7917 /* Determine the size of the flexible array member from
7918 the size of the initializer provided for it. */
7919 field_size
= TYPE_SIZE (TREE_TYPE (cval
));
7922 /* Variable sized objects in static constructors makes no sense,
7923 but field_size can be NULL for flexible array members. */
7924 gcc_assert (TREE_CODE (field_offset
) == INTEGER_CST
7925 && TREE_CODE (byte_offset
) == INTEGER_CST
7926 && (field_size
!= NULL_TREE
7927 ? TREE_CODE (field_size
) == INTEGER_CST
7928 : TREE_CODE (TREE_TYPE (cfield
)) == ARRAY_TYPE
));
7930 /* Compute bit offset of the field. */
7931 offset_int bitoffset
7932 = (wi::to_offset (field_offset
)
7933 + (wi::to_offset (byte_offset
) << LOG2_BITS_PER_UNIT
));
7934 /* Compute bit offset where the field ends. */
7935 offset_int bitoffset_end
;
7936 if (field_size
!= NULL_TREE
)
7937 bitoffset_end
= bitoffset
+ wi::to_offset (field_size
);
7941 /* Compute the bit offset of the end of the desired access.
7942 As a special case, if the size of the desired access is
7943 zero, assume the access is to the entire field (and let
7944 the caller make any necessary adjustments by storing
7945 the actual bounds of the field in FIELDBOUNDS). */
7946 offset_int access_end
= offset_int (offset
);
7950 access_end
= bitoffset_end
;
7952 /* Is there any overlap between the desired access at
7953 [OFFSET, OFFSET+SIZE) and the offset of the field within
7954 the object at [BITOFFSET, BITOFFSET_END)? */
7955 if (wi::cmps (access_end
, bitoffset
) > 0
7956 && (field_size
== NULL_TREE
7957 || wi::lts_p (offset
, bitoffset_end
)))
7959 *suboff
+= bitoffset
.to_uhwi ();
7961 if (!size
&& TREE_CODE (cval
) != CONSTRUCTOR
)
7963 /* For the final reference to the entire accessed member
7964 (SIZE is zero), reset OFFSET, disegard TYPE (which may
7965 be null) in favor of the type of the member, and set
7966 SIZE to the size of the accessed member. */
7967 offset
= bitoffset
.to_uhwi ();
7968 type
= TREE_TYPE (cval
);
7969 size
= (bitoffset_end
- bitoffset
).to_uhwi ();
7972 /* We do have overlap. Now see if the field is large enough
7973 to cover the access. Give up for accesses that extend
7974 beyond the end of the object or that span multiple fields. */
7975 if (wi::cmps (access_end
, bitoffset_end
) > 0)
7977 if (offset
< bitoffset
)
7980 offset_int inner_offset
= offset_int (offset
) - bitoffset
;
7981 return fold_ctor_reference (type
, cval
,
7982 inner_offset
.to_uhwi (), size
,
7990 return build_zero_cst (type
);
7993 /* CTOR is value initializing memory. Fold a reference of TYPE and
7994 bit size POLY_SIZE to the memory at bit POLY_OFFSET. When POLY_SIZE
7995 is zero, attempt to fold a reference to the entire subobject
7996 which OFFSET refers to. This is used when folding accesses to
7997 string members of aggregates. When non-null, set *SUBOFF to
7998 the bit offset of the accessed subobject. */
8001 fold_ctor_reference (tree type
, tree ctor
, const poly_uint64
&poly_offset
,
8002 const poly_uint64
&poly_size
, tree from_decl
,
8003 unsigned HOST_WIDE_INT
*suboff
/* = NULL */)
8007 /* We found the field with exact match. */
8009 && useless_type_conversion_p (type
, TREE_TYPE (ctor
))
8010 && known_eq (poly_offset
, 0U))
8011 return canonicalize_constructor_val (unshare_expr (ctor
), from_decl
);
8013 /* The remaining optimizations need a constant size and offset. */
8014 unsigned HOST_WIDE_INT size
, offset
;
8015 if (!poly_size
.is_constant (&size
) || !poly_offset
.is_constant (&offset
))
8018 /* We are at the end of walk, see if we can view convert the
8020 if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor
)) && !offset
8021 /* VIEW_CONVERT_EXPR is defined only for matching sizes. */
8022 && !compare_tree_int (TYPE_SIZE (type
), size
)
8023 && !compare_tree_int (TYPE_SIZE (TREE_TYPE (ctor
)), size
))
8025 ret
= canonicalize_constructor_val (unshare_expr (ctor
), from_decl
);
8028 ret
= fold_unary (VIEW_CONVERT_EXPR
, type
, ret
);
8030 STRIP_USELESS_TYPE_CONVERSION (ret
);
8034 /* For constants and byte-aligned/sized reads try to go through
8035 native_encode/interpret. */
8036 if (CONSTANT_CLASS_P (ctor
)
8037 && BITS_PER_UNIT
== 8
8038 && offset
% BITS_PER_UNIT
== 0
8039 && offset
/ BITS_PER_UNIT
<= INT_MAX
8040 && size
% BITS_PER_UNIT
== 0
8041 && size
<= MAX_BITSIZE_MODE_ANY_MODE
8042 && can_native_interpret_type_p (type
))
8044 unsigned char buf
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
8045 int len
= native_encode_expr (ctor
, buf
, size
/ BITS_PER_UNIT
,
8046 offset
/ BITS_PER_UNIT
);
8048 return native_interpret_expr (type
, buf
, len
);
8050 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
8052 unsigned HOST_WIDE_INT dummy
= 0;
8057 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
8058 || TREE_CODE (TREE_TYPE (ctor
)) == VECTOR_TYPE
)
8059 ret
= fold_array_ctor_reference (type
, ctor
, offset
, size
,
8062 ret
= fold_nonarray_ctor_reference (type
, ctor
, offset
, size
,
8065 /* Fall back to native_encode_initializer. Needs to be done
8066 only in the outermost fold_ctor_reference call (because it itself
8067 recurses into CONSTRUCTORs) and doesn't update suboff. */
8068 if (ret
== NULL_TREE
8070 && BITS_PER_UNIT
== 8
8071 && offset
% BITS_PER_UNIT
== 0
8072 && offset
/ BITS_PER_UNIT
<= INT_MAX
8073 && size
% BITS_PER_UNIT
== 0
8074 && size
<= MAX_BITSIZE_MODE_ANY_MODE
8075 && can_native_interpret_type_p (type
))
8077 unsigned char buf
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
8078 int len
= native_encode_initializer (ctor
, buf
, size
/ BITS_PER_UNIT
,
8079 offset
/ BITS_PER_UNIT
);
8081 return native_interpret_expr (type
, buf
, len
);
8090 /* Return the tree representing the element referenced by T if T is an
8091 ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
8092 names using VALUEIZE. Return NULL_TREE otherwise. */
8095 fold_const_aggregate_ref_1 (tree t
, tree (*valueize
) (tree
))
8097 tree ctor
, idx
, base
;
8098 poly_int64 offset
, size
, max_size
;
8102 if (TREE_THIS_VOLATILE (t
))
8106 return get_symbol_constant_value (t
);
8108 tem
= fold_read_from_constant_string (t
);
8112 switch (TREE_CODE (t
))
8115 case ARRAY_RANGE_REF
:
8116 /* Constant indexes are handled well by get_base_constructor.
8117 Only special case variable offsets.
8118 FIXME: This code can't handle nested references with variable indexes
8119 (they will be handled only by iteration of ccp). Perhaps we can bring
8120 get_ref_base_and_extent here and make it use a valueize callback. */
8121 if (TREE_CODE (TREE_OPERAND (t
, 1)) == SSA_NAME
8123 && (idx
= (*valueize
) (TREE_OPERAND (t
, 1)))
8124 && poly_int_tree_p (idx
))
8126 tree low_bound
, unit_size
;
8128 /* If the resulting bit-offset is constant, track it. */
8129 if ((low_bound
= array_ref_low_bound (t
),
8130 poly_int_tree_p (low_bound
))
8131 && (unit_size
= array_ref_element_size (t
),
8132 tree_fits_uhwi_p (unit_size
)))
8134 poly_offset_int woffset
8135 = wi::sext (wi::to_poly_offset (idx
)
8136 - wi::to_poly_offset (low_bound
),
8137 TYPE_PRECISION (sizetype
));
8138 woffset
*= tree_to_uhwi (unit_size
);
8139 woffset
*= BITS_PER_UNIT
;
8140 if (woffset
.to_shwi (&offset
))
8142 base
= TREE_OPERAND (t
, 0);
8143 ctor
= get_base_constructor (base
, &offset
, valueize
);
8144 /* Empty constructor. Always fold to 0. */
8145 if (ctor
== error_mark_node
)
8146 return build_zero_cst (TREE_TYPE (t
));
8147 /* Out of bound array access. Value is undefined,
8149 if (maybe_lt (offset
, 0))
8151 /* We cannot determine ctor. */
8154 return fold_ctor_reference (TREE_TYPE (t
), ctor
, offset
,
8155 tree_to_uhwi (unit_size
)
8165 case TARGET_MEM_REF
:
8167 base
= get_ref_base_and_extent (t
, &offset
, &size
, &max_size
, &reverse
);
8168 ctor
= get_base_constructor (base
, &offset
, valueize
);
8170 /* Empty constructor. Always fold to 0. */
8171 if (ctor
== error_mark_node
)
8172 return build_zero_cst (TREE_TYPE (t
));
8173 /* We do not know precise address. */
8174 if (!known_size_p (max_size
) || maybe_ne (max_size
, size
))
8176 /* We cannot determine ctor. */
8180 /* Out of bound array access. Value is undefined, but don't fold. */
8181 if (maybe_lt (offset
, 0))
8184 tem
= fold_ctor_reference (TREE_TYPE (t
), ctor
, offset
, size
, base
);
8188 /* For bit field reads try to read the representative and
8190 if (TREE_CODE (t
) == COMPONENT_REF
8191 && DECL_BIT_FIELD (TREE_OPERAND (t
, 1))
8192 && DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (t
, 1)))
8194 HOST_WIDE_INT csize
, coffset
;
8195 tree field
= TREE_OPERAND (t
, 1);
8196 tree repr
= DECL_BIT_FIELD_REPRESENTATIVE (field
);
8197 if (INTEGRAL_TYPE_P (TREE_TYPE (repr
))
8198 && size
.is_constant (&csize
)
8199 && offset
.is_constant (&coffset
)
8200 && (coffset
% BITS_PER_UNIT
!= 0
8201 || csize
% BITS_PER_UNIT
!= 0)
8203 && BYTES_BIG_ENDIAN
== WORDS_BIG_ENDIAN
)
8205 poly_int64 bitoffset
;
8206 poly_uint64 field_offset
, repr_offset
;
8207 if (poly_int_tree_p (DECL_FIELD_OFFSET (field
), &field_offset
)
8208 && poly_int_tree_p (DECL_FIELD_OFFSET (repr
), &repr_offset
))
8209 bitoffset
= (field_offset
- repr_offset
) * BITS_PER_UNIT
;
8212 bitoffset
+= (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field
))
8213 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr
)));
8214 HOST_WIDE_INT bitoff
;
8215 int diff
= (TYPE_PRECISION (TREE_TYPE (repr
))
8216 - TYPE_PRECISION (TREE_TYPE (field
)));
8217 if (bitoffset
.is_constant (&bitoff
)
8222 size
= tree_to_uhwi (DECL_SIZE (repr
));
8224 tem
= fold_ctor_reference (TREE_TYPE (repr
), ctor
, offset
,
8226 if (tem
&& TREE_CODE (tem
) == INTEGER_CST
)
8228 if (!BYTES_BIG_ENDIAN
)
8229 tem
= wide_int_to_tree (TREE_TYPE (field
),
8230 wi::lrshift (wi::to_wide (tem
),
8233 tem
= wide_int_to_tree (TREE_TYPE (field
),
8234 wi::lrshift (wi::to_wide (tem
),
8246 tree c
= fold_const_aggregate_ref_1 (TREE_OPERAND (t
, 0), valueize
);
8247 if (c
&& TREE_CODE (c
) == COMPLEX_CST
)
8248 return fold_build1_loc (EXPR_LOCATION (t
),
8249 TREE_CODE (t
), TREE_TYPE (t
), c
);
8261 fold_const_aggregate_ref (tree t
)
8263 return fold_const_aggregate_ref_1 (t
, NULL
);
8266 /* Lookup virtual method with index TOKEN in a virtual table V
8268 Set CAN_REFER if non-NULL to false if method
8269 is not referable or if the virtual table is ill-formed (such as rewriten
8270 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
8273 gimple_get_virt_method_for_vtable (HOST_WIDE_INT token
,
8275 unsigned HOST_WIDE_INT offset
,
8278 tree vtable
= v
, init
, fn
;
8279 unsigned HOST_WIDE_INT size
;
8280 unsigned HOST_WIDE_INT elt_size
, access_index
;
8286 /* First of all double check we have virtual table. */
8287 if (!VAR_P (v
) || !DECL_VIRTUAL_P (v
))
8289 /* Pass down that we lost track of the target. */
8295 init
= ctor_for_folding (v
);
8297 /* The virtual tables should always be born with constructors
8298 and we always should assume that they are avaialble for
8299 folding. At the moment we do not stream them in all cases,
8300 but it should never happen that ctor seem unreachable. */
8302 if (init
== error_mark_node
)
8304 /* Pass down that we lost track of the target. */
8309 gcc_checking_assert (TREE_CODE (TREE_TYPE (v
)) == ARRAY_TYPE
);
8310 size
= tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v
))));
8311 offset
*= BITS_PER_UNIT
;
8312 offset
+= token
* size
;
8314 /* Lookup the value in the constructor that is assumed to be array.
8315 This is equivalent to
8316 fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
8317 offset, size, NULL);
8318 but in a constant time. We expect that frontend produced a simple
8319 array without indexed initializers. */
8321 gcc_checking_assert (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
);
8322 domain_type
= TYPE_DOMAIN (TREE_TYPE (init
));
8323 gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type
)));
8324 elt_size
= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init
))));
8326 access_index
= offset
/ BITS_PER_UNIT
/ elt_size
;
8327 gcc_checking_assert (offset
% (elt_size
* BITS_PER_UNIT
) == 0);
8329 /* The C++ FE can now produce indexed fields, and we check if the indexes
8331 if (access_index
< CONSTRUCTOR_NELTS (init
))
8333 fn
= CONSTRUCTOR_ELT (init
, access_index
)->value
;
8334 tree idx
= CONSTRUCTOR_ELT (init
, access_index
)->index
;
8335 gcc_checking_assert (!idx
|| tree_to_uhwi (idx
) == access_index
);
8341 /* For type inconsistent program we may end up looking up virtual method
8342 in virtual table that does not contain TOKEN entries. We may overrun
8343 the virtual table and pick up a constant or RTTI info pointer.
8344 In any case the call is undefined. */
8346 || (TREE_CODE (fn
) != ADDR_EXPR
&& TREE_CODE (fn
) != FDESC_EXPR
)
8347 || TREE_CODE (TREE_OPERAND (fn
, 0)) != FUNCTION_DECL
)
8348 fn
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
8351 fn
= TREE_OPERAND (fn
, 0);
8353 /* When cgraph node is missing and function is not public, we cannot
8354 devirtualize. This can happen in WHOPR when the actual method
8355 ends up in other partition, because we found devirtualization
8356 possibility too late. */
8357 if (!can_refer_decl_in_current_unit_p (fn
, vtable
))
8368 /* Make sure we create a cgraph node for functions we'll reference.
8369 They can be non-existent if the reference comes from an entry
8370 of an external vtable for example. */
8371 cgraph_node::get_create (fn
);
8376 /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
8377 is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
8378 KNOWN_BINFO carries the binfo describing the true type of
8379 OBJ_TYPE_REF_OBJECT(REF).
8380 Set CAN_REFER if non-NULL to false if method
8381 is not referable or if the virtual table is ill-formed (such as rewriten
8382 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
8385 gimple_get_virt_method_for_binfo (HOST_WIDE_INT token
, tree known_binfo
,
8388 unsigned HOST_WIDE_INT offset
;
8391 v
= BINFO_VTABLE (known_binfo
);
8392 /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */
8396 if (!vtable_pointer_value_to_vtable (v
, &v
, &offset
))
8402 return gimple_get_virt_method_for_vtable (token
, v
, offset
, can_refer
);
8405 /* Given a pointer value T, return a simplified version of an
8406 indirection through T, or NULL_TREE if no simplification is
8407 possible. Note that the resulting type may be different from
8408 the type pointed to in the sense that it is still compatible
8409 from the langhooks point of view. */
8412 gimple_fold_indirect_ref (tree t
)
8414 tree ptype
= TREE_TYPE (t
), type
= TREE_TYPE (ptype
);
8419 subtype
= TREE_TYPE (sub
);
8420 if (!POINTER_TYPE_P (subtype
)
8421 || TYPE_REF_CAN_ALIAS_ALL (ptype
))
8424 if (TREE_CODE (sub
) == ADDR_EXPR
)
8426 tree op
= TREE_OPERAND (sub
, 0);
8427 tree optype
= TREE_TYPE (op
);
8429 if (useless_type_conversion_p (type
, optype
))
8432 /* *(foo *)&fooarray => fooarray[0] */
8433 if (TREE_CODE (optype
) == ARRAY_TYPE
8434 && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype
))) == INTEGER_CST
8435 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
8437 tree type_domain
= TYPE_DOMAIN (optype
);
8438 tree min_val
= size_zero_node
;
8439 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
8440 min_val
= TYPE_MIN_VALUE (type_domain
);
8441 if (TREE_CODE (min_val
) == INTEGER_CST
)
8442 return build4 (ARRAY_REF
, type
, op
, min_val
, NULL_TREE
, NULL_TREE
);
8444 /* *(foo *)&complexfoo => __real__ complexfoo */
8445 else if (TREE_CODE (optype
) == COMPLEX_TYPE
8446 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
8447 return fold_build1 (REALPART_EXPR
, type
, op
);
8448 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
8449 else if (TREE_CODE (optype
) == VECTOR_TYPE
8450 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
8452 tree part_width
= TYPE_SIZE (type
);
8453 tree index
= bitsize_int (0);
8454 return fold_build3 (BIT_FIELD_REF
, type
, op
, part_width
, index
);
8458 /* *(p + CST) -> ... */
8459 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
8460 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
8462 tree addr
= TREE_OPERAND (sub
, 0);
8463 tree off
= TREE_OPERAND (sub
, 1);
8467 addrtype
= TREE_TYPE (addr
);
8469 /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
8470 if (TREE_CODE (addr
) == ADDR_EXPR
8471 && TREE_CODE (TREE_TYPE (addrtype
)) == VECTOR_TYPE
8472 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (addrtype
)))
8473 && tree_fits_uhwi_p (off
))
8475 unsigned HOST_WIDE_INT offset
= tree_to_uhwi (off
);
8476 tree part_width
= TYPE_SIZE (type
);
8477 unsigned HOST_WIDE_INT part_widthi
8478 = tree_to_shwi (part_width
) / BITS_PER_UNIT
;
8479 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
8480 tree index
= bitsize_int (indexi
);
8481 if (known_lt (offset
/ part_widthi
,
8482 TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype
))))
8483 return fold_build3 (BIT_FIELD_REF
, type
, TREE_OPERAND (addr
, 0),
8487 /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
8488 if (TREE_CODE (addr
) == ADDR_EXPR
8489 && TREE_CODE (TREE_TYPE (addrtype
)) == COMPLEX_TYPE
8490 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (addrtype
))))
8492 tree size
= TYPE_SIZE_UNIT (type
);
8493 if (tree_int_cst_equal (size
, off
))
8494 return fold_build1 (IMAGPART_EXPR
, type
, TREE_OPERAND (addr
, 0));
8497 /* *(p + CST) -> MEM_REF <p, CST>. */
8498 if (TREE_CODE (addr
) != ADDR_EXPR
8499 || DECL_P (TREE_OPERAND (addr
, 0)))
8500 return fold_build2 (MEM_REF
, type
,
8502 wide_int_to_tree (ptype
, wi::to_wide (off
)));
8505 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
8506 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
8507 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype
)))) == INTEGER_CST
8508 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (subtype
))))
8511 tree min_val
= size_zero_node
;
8513 sub
= gimple_fold_indirect_ref (sub
);
8515 sub
= build1 (INDIRECT_REF
, TREE_TYPE (subtype
), osub
);
8516 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
8517 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
8518 min_val
= TYPE_MIN_VALUE (type_domain
);
8519 if (TREE_CODE (min_val
) == INTEGER_CST
)
8520 return build4 (ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
, NULL_TREE
);
8526 /* Return true if CODE is an operation that when operating on signed
8527 integer types involves undefined behavior on overflow and the
8528 operation can be expressed with unsigned arithmetic. */
8531 arith_code_with_undefined_signed_overflow (tree_code code
)
8540 case POINTER_PLUS_EXPR
:
8547 /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic
8548 operation that can be transformed to unsigned arithmetic by converting
8549 its operand, carrying out the operation in the corresponding unsigned
8550 type and converting the result back to the original type.
8552 If IN_PLACE is true, adjust the stmt in place and return NULL.
8553 Otherwise returns a sequence of statements that replace STMT and also
8554 contain a modified form of STMT itself. */
8557 rewrite_to_defined_overflow (gimple
*stmt
, bool in_place
/* = false */)
8559 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8561 fprintf (dump_file
, "rewriting stmt with undefined signed "
8563 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
8566 tree lhs
= gimple_assign_lhs (stmt
);
8567 tree type
= unsigned_type_for (TREE_TYPE (lhs
));
8568 gimple_seq stmts
= NULL
;
8569 if (gimple_assign_rhs_code (stmt
) == ABS_EXPR
)
8570 gimple_assign_set_rhs_code (stmt
, ABSU_EXPR
);
8572 for (unsigned i
= 1; i
< gimple_num_ops (stmt
); ++i
)
8574 tree op
= gimple_op (stmt
, i
);
8575 op
= gimple_convert (&stmts
, type
, op
);
8576 gimple_set_op (stmt
, i
, op
);
8578 gimple_assign_set_lhs (stmt
, make_ssa_name (type
, stmt
));
8579 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
8580 gimple_assign_set_rhs_code (stmt
, PLUS_EXPR
);
8581 gimple_set_modified (stmt
, true);
8584 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
8586 gsi_insert_seq_before (&gsi
, stmts
, GSI_SAME_STMT
);
8590 gimple_seq_add_stmt (&stmts
, stmt
);
8591 gimple
*cvt
= gimple_build_assign (lhs
, NOP_EXPR
, gimple_assign_lhs (stmt
));
8594 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
8595 gsi_insert_after (&gsi
, cvt
, GSI_SAME_STMT
);
8599 gimple_seq_add_stmt (&stmts
, cvt
);
8605 /* The valueization hook we use for the gimple_build API simplification.
8606 This makes us match fold_buildN behavior by only combining with
8607 statements in the sequence(s) we are currently building. */
8610 gimple_build_valueize (tree op
)
8612 if (gimple_bb (SSA_NAME_DEF_STMT (op
)) == NULL
)
8617 /* Build the expression CODE OP0 of type TYPE with location LOC,
8618 simplifying it first if possible. Returns the built
8619 expression value and appends statements possibly defining it
8623 gimple_build (gimple_seq
*seq
, location_t loc
,
8624 enum tree_code code
, tree type
, tree op0
)
8626 tree res
= gimple_simplify (code
, type
, op0
, seq
, gimple_build_valueize
);
8629 res
= create_tmp_reg_or_ssa_name (type
);
8631 if (code
== REALPART_EXPR
8632 || code
== IMAGPART_EXPR
8633 || code
== VIEW_CONVERT_EXPR
)
8634 stmt
= gimple_build_assign (res
, code
, build1 (code
, type
, op0
));
8636 stmt
= gimple_build_assign (res
, code
, op0
);
8637 gimple_set_location (stmt
, loc
);
8638 gimple_seq_add_stmt_without_update (seq
, stmt
);
8643 /* Build the expression OP0 CODE OP1 of type TYPE with location LOC,
8644 simplifying it first if possible. Returns the built
8645 expression value and appends statements possibly defining it
8649 gimple_build (gimple_seq
*seq
, location_t loc
,
8650 enum tree_code code
, tree type
, tree op0
, tree op1
)
8652 tree res
= gimple_simplify (code
, type
, op0
, op1
, seq
, gimple_build_valueize
);
8655 res
= create_tmp_reg_or_ssa_name (type
);
8656 gimple
*stmt
= gimple_build_assign (res
, code
, op0
, op1
);
8657 gimple_set_location (stmt
, loc
);
8658 gimple_seq_add_stmt_without_update (seq
, stmt
);
8663 /* Build the expression (CODE OP0 OP1 OP2) of type TYPE with location LOC,
8664 simplifying it first if possible. Returns the built
8665 expression value and appends statements possibly defining it
8669 gimple_build (gimple_seq
*seq
, location_t loc
,
8670 enum tree_code code
, tree type
, tree op0
, tree op1
, tree op2
)
8672 tree res
= gimple_simplify (code
, type
, op0
, op1
, op2
,
8673 seq
, gimple_build_valueize
);
8676 res
= create_tmp_reg_or_ssa_name (type
);
8678 if (code
== BIT_FIELD_REF
)
8679 stmt
= gimple_build_assign (res
, code
,
8680 build3 (code
, type
, op0
, op1
, op2
));
8682 stmt
= gimple_build_assign (res
, code
, op0
, op1
, op2
);
8683 gimple_set_location (stmt
, loc
);
8684 gimple_seq_add_stmt_without_update (seq
, stmt
);
8689 /* Build the call FN () with a result of type TYPE (or no result if TYPE is
8690 void) with a location LOC. Returns the built expression value (or NULL_TREE
8691 if TYPE is void) and appends statements possibly defining it to SEQ. */
8694 gimple_build (gimple_seq
*seq
, location_t loc
, combined_fn fn
, tree type
)
8696 tree res
= NULL_TREE
;
8698 if (internal_fn_p (fn
))
8699 stmt
= gimple_build_call_internal (as_internal_fn (fn
), 0);
8702 tree decl
= builtin_decl_implicit (as_builtin_fn (fn
));
8703 stmt
= gimple_build_call (decl
, 0);
8705 if (!VOID_TYPE_P (type
))
8707 res
= create_tmp_reg_or_ssa_name (type
);
8708 gimple_call_set_lhs (stmt
, res
);
8710 gimple_set_location (stmt
, loc
);
8711 gimple_seq_add_stmt_without_update (seq
, stmt
);
8715 /* Build the call FN (ARG0) with a result of type TYPE
8716 (or no result if TYPE is void) with location LOC,
8717 simplifying it first if possible. Returns the built
8718 expression value (or NULL_TREE if TYPE is void) and appends
8719 statements possibly defining it to SEQ. */
8722 gimple_build (gimple_seq
*seq
, location_t loc
, combined_fn fn
,
8723 tree type
, tree arg0
)
8725 tree res
= gimple_simplify (fn
, type
, arg0
, seq
, gimple_build_valueize
);
8729 if (internal_fn_p (fn
))
8730 stmt
= gimple_build_call_internal (as_internal_fn (fn
), 1, arg0
);
8733 tree decl
= builtin_decl_implicit (as_builtin_fn (fn
));
8734 stmt
= gimple_build_call (decl
, 1, arg0
);
8736 if (!VOID_TYPE_P (type
))
8738 res
= create_tmp_reg_or_ssa_name (type
);
8739 gimple_call_set_lhs (stmt
, res
);
8741 gimple_set_location (stmt
, loc
);
8742 gimple_seq_add_stmt_without_update (seq
, stmt
);
8747 /* Build the call FN (ARG0, ARG1) with a result of type TYPE
8748 (or no result if TYPE is void) with location LOC,
8749 simplifying it first if possible. Returns the built
8750 expression value (or NULL_TREE if TYPE is void) and appends
8751 statements possibly defining it to SEQ. */
8754 gimple_build (gimple_seq
*seq
, location_t loc
, combined_fn fn
,
8755 tree type
, tree arg0
, tree arg1
)
8757 tree res
= gimple_simplify (fn
, type
, arg0
, arg1
, seq
, gimple_build_valueize
);
8761 if (internal_fn_p (fn
))
8762 stmt
= gimple_build_call_internal (as_internal_fn (fn
), 2, arg0
, arg1
);
8765 tree decl
= builtin_decl_implicit (as_builtin_fn (fn
));
8766 stmt
= gimple_build_call (decl
, 2, arg0
, arg1
);
8768 if (!VOID_TYPE_P (type
))
8770 res
= create_tmp_reg_or_ssa_name (type
);
8771 gimple_call_set_lhs (stmt
, res
);
8773 gimple_set_location (stmt
, loc
);
8774 gimple_seq_add_stmt_without_update (seq
, stmt
);
8779 /* Build the call FN (ARG0, ARG1, ARG2) with a result of type TYPE
8780 (or no result if TYPE is void) with location LOC,
8781 simplifying it first if possible. Returns the built
8782 expression value (or NULL_TREE if TYPE is void) and appends
8783 statements possibly defining it to SEQ. */
8786 gimple_build (gimple_seq
*seq
, location_t loc
, combined_fn fn
,
8787 tree type
, tree arg0
, tree arg1
, tree arg2
)
8789 tree res
= gimple_simplify (fn
, type
, arg0
, arg1
, arg2
,
8790 seq
, gimple_build_valueize
);
8794 if (internal_fn_p (fn
))
8795 stmt
= gimple_build_call_internal (as_internal_fn (fn
),
8796 3, arg0
, arg1
, arg2
);
8799 tree decl
= builtin_decl_implicit (as_builtin_fn (fn
));
8800 stmt
= gimple_build_call (decl
, 3, arg0
, arg1
, arg2
);
8802 if (!VOID_TYPE_P (type
))
8804 res
= create_tmp_reg_or_ssa_name (type
);
8805 gimple_call_set_lhs (stmt
, res
);
8807 gimple_set_location (stmt
, loc
);
8808 gimple_seq_add_stmt_without_update (seq
, stmt
);
8813 /* Build CODE (OP0) with a result of type TYPE (or no result if TYPE is
8814 void) with location LOC, simplifying it first if possible. Returns the
8815 built expression value (or NULL_TREE if TYPE is void) and appends
8816 statements possibly defining it to SEQ. */
8819 gimple_build (gimple_seq
*seq
, location_t loc
, code_helper code
,
8820 tree type
, tree op0
)
8822 if (code
.is_tree_code ())
8823 return gimple_build (seq
, loc
, tree_code (code
), type
, op0
);
8824 return gimple_build (seq
, loc
, combined_fn (code
), type
, op0
);
8827 /* Build CODE (OP0, OP1) with a result of type TYPE (or no result if TYPE is
8828 void) with location LOC, simplifying it first if possible. Returns the
8829 built expression value (or NULL_TREE if TYPE is void) and appends
8830 statements possibly defining it to SEQ. */
8833 gimple_build (gimple_seq
*seq
, location_t loc
, code_helper code
,
8834 tree type
, tree op0
, tree op1
)
8836 if (code
.is_tree_code ())
8837 return gimple_build (seq
, loc
, tree_code (code
), type
, op0
, op1
);
8838 return gimple_build (seq
, loc
, combined_fn (code
), type
, op0
, op1
);
8841 /* Build CODE (OP0, OP1, OP2) with a result of type TYPE (or no result if TYPE
8842 is void) with location LOC, simplifying it first if possible. Returns the
8843 built expression value (or NULL_TREE if TYPE is void) and appends statements
8844 possibly defining it to SEQ. */
8847 gimple_build (gimple_seq
*seq
, location_t loc
, code_helper code
,
8848 tree type
, tree op0
, tree op1
, tree op2
)
8850 if (code
.is_tree_code ())
8851 return gimple_build (seq
, loc
, tree_code (code
), type
, op0
, op1
, op2
);
8852 return gimple_build (seq
, loc
, combined_fn (code
), type
, op0
, op1
, op2
);
8855 /* Build the conversion (TYPE) OP with a result of type TYPE
8856 with location LOC if such conversion is neccesary in GIMPLE,
8857 simplifying it first.
8858 Returns the built expression value and appends
8859 statements possibly defining it to SEQ. */
8862 gimple_convert (gimple_seq
*seq
, location_t loc
, tree type
, tree op
)
8864 if (useless_type_conversion_p (type
, TREE_TYPE (op
)))
8866 return gimple_build (seq
, loc
, NOP_EXPR
, type
, op
);
8869 /* Build the conversion (ptrofftype) OP with a result of a type
8870 compatible with ptrofftype with location LOC if such conversion
8871 is neccesary in GIMPLE, simplifying it first.
8872 Returns the built expression value and appends
8873 statements possibly defining it to SEQ. */
8876 gimple_convert_to_ptrofftype (gimple_seq
*seq
, location_t loc
, tree op
)
8878 if (ptrofftype_p (TREE_TYPE (op
)))
8880 return gimple_convert (seq
, loc
, sizetype
, op
);
8883 /* Build a vector of type TYPE in which each element has the value OP.
8884 Return a gimple value for the result, appending any new statements
8888 gimple_build_vector_from_val (gimple_seq
*seq
, location_t loc
, tree type
,
8891 if (!TYPE_VECTOR_SUBPARTS (type
).is_constant ()
8892 && !CONSTANT_CLASS_P (op
))
8893 return gimple_build (seq
, loc
, VEC_DUPLICATE_EXPR
, type
, op
);
8895 tree res
, vec
= build_vector_from_val (type
, op
);
8896 if (is_gimple_val (vec
))
8898 if (gimple_in_ssa_p (cfun
))
8899 res
= make_ssa_name (type
);
8901 res
= create_tmp_reg (type
);
8902 gimple
*stmt
= gimple_build_assign (res
, vec
);
8903 gimple_set_location (stmt
, loc
);
8904 gimple_seq_add_stmt_without_update (seq
, stmt
);
8908 /* Build a vector from BUILDER, handling the case in which some elements
8909 are non-constant. Return a gimple value for the result, appending any
8910 new instructions to SEQ.
8912 BUILDER must not have a stepped encoding on entry. This is because
8913 the function is not geared up to handle the arithmetic that would
8914 be needed in the variable case, and any code building a vector that
8915 is known to be constant should use BUILDER->build () directly. */
8918 gimple_build_vector (gimple_seq
*seq
, location_t loc
,
8919 tree_vector_builder
*builder
)
8921 gcc_assert (builder
->nelts_per_pattern () <= 2);
8922 unsigned int encoded_nelts
= builder
->encoded_nelts ();
8923 for (unsigned int i
= 0; i
< encoded_nelts
; ++i
)
8924 if (!CONSTANT_CLASS_P ((*builder
)[i
]))
8926 tree type
= builder
->type ();
8927 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
).to_constant ();
8928 vec
<constructor_elt
, va_gc
> *v
;
8929 vec_alloc (v
, nelts
);
8930 for (i
= 0; i
< nelts
; ++i
)
8931 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, builder
->elt (i
));
8934 if (gimple_in_ssa_p (cfun
))
8935 res
= make_ssa_name (type
);
8937 res
= create_tmp_reg (type
);
8938 gimple
*stmt
= gimple_build_assign (res
, build_constructor (type
, v
));
8939 gimple_set_location (stmt
, loc
);
8940 gimple_seq_add_stmt_without_update (seq
, stmt
);
8943 return builder
->build ();
8946 /* Emit gimple statements into &stmts that take a value given in OLD_SIZE
8947 and generate a value guaranteed to be rounded upwards to ALIGN.
8949 Return the tree node representing this size, it is of TREE_TYPE TYPE. */
8952 gimple_build_round_up (gimple_seq
*seq
, location_t loc
, tree type
,
8953 tree old_size
, unsigned HOST_WIDE_INT align
)
8955 unsigned HOST_WIDE_INT tg_mask
= align
- 1;
8956 /* tree new_size = (old_size + tg_mask) & ~tg_mask; */
8957 gcc_assert (INTEGRAL_TYPE_P (type
));
8958 tree tree_mask
= build_int_cst (type
, tg_mask
);
8959 tree oversize
= gimple_build (seq
, loc
, PLUS_EXPR
, type
, old_size
,
8962 tree mask
= build_int_cst (type
, -align
);
8963 return gimple_build (seq
, loc
, BIT_AND_EXPR
, type
, oversize
, mask
);
8966 /* Return true if the result of assignment STMT is known to be non-negative.
8967 If the return value is based on the assumption that signed overflow is
8968 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
8969 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
8972 gimple_assign_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
8975 enum tree_code code
= gimple_assign_rhs_code (stmt
);
8976 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
8977 switch (get_gimple_rhs_class (code
))
8979 case GIMPLE_UNARY_RHS
:
8980 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
8982 gimple_assign_rhs1 (stmt
),
8983 strict_overflow_p
, depth
);
8984 case GIMPLE_BINARY_RHS
:
8985 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
8987 gimple_assign_rhs1 (stmt
),
8988 gimple_assign_rhs2 (stmt
),
8989 strict_overflow_p
, depth
);
8990 case GIMPLE_TERNARY_RHS
:
8992 case GIMPLE_SINGLE_RHS
:
8993 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
8994 strict_overflow_p
, depth
);
8995 case GIMPLE_INVALID_RHS
:
9001 /* Return true if return value of call STMT is known to be non-negative.
9002 If the return value is based on the assumption that signed overflow is
9003 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
9004 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
9007 gimple_call_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
9010 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
9011 gimple_call_arg (stmt
, 0) : NULL_TREE
;
9012 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
9013 gimple_call_arg (stmt
, 1) : NULL_TREE
;
9014 tree lhs
= gimple_call_lhs (stmt
);
9016 && tree_call_nonnegative_warnv_p (TREE_TYPE (lhs
),
9017 gimple_call_combined_fn (stmt
),
9019 strict_overflow_p
, depth
));
9022 /* Return true if return value of call STMT is known to be non-negative.
9023 If the return value is based on the assumption that signed overflow is
9024 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
9025 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
9028 gimple_phi_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
9031 for (unsigned i
= 0; i
< gimple_phi_num_args (stmt
); ++i
)
9033 tree arg
= gimple_phi_arg_def (stmt
, i
);
9034 if (!tree_single_nonnegative_warnv_p (arg
, strict_overflow_p
, depth
+ 1))
9040 /* Return true if STMT is known to compute a non-negative value.
9041 If the return value is based on the assumption that signed overflow is
9042 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
9043 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
9046 gimple_stmt_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
9049 switch (gimple_code (stmt
))
9052 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
,
9055 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
,
9058 return gimple_phi_nonnegative_warnv_p (stmt
, strict_overflow_p
,
9065 /* Return true if the floating-point value computed by assignment STMT
9066 is known to have an integer value. We also allow +Inf, -Inf and NaN
9067 to be considered integer values. Return false for signaling NaN.
9069 DEPTH is the current nesting depth of the query. */
9072 gimple_assign_integer_valued_real_p (gimple
*stmt
, int depth
)
9074 enum tree_code code
= gimple_assign_rhs_code (stmt
);
9075 switch (get_gimple_rhs_class (code
))
9077 case GIMPLE_UNARY_RHS
:
9078 return integer_valued_real_unary_p (gimple_assign_rhs_code (stmt
),
9079 gimple_assign_rhs1 (stmt
), depth
);
9080 case GIMPLE_BINARY_RHS
:
9081 return integer_valued_real_binary_p (gimple_assign_rhs_code (stmt
),
9082 gimple_assign_rhs1 (stmt
),
9083 gimple_assign_rhs2 (stmt
), depth
);
9084 case GIMPLE_TERNARY_RHS
:
9086 case GIMPLE_SINGLE_RHS
:
9087 return integer_valued_real_single_p (gimple_assign_rhs1 (stmt
), depth
);
9088 case GIMPLE_INVALID_RHS
:
9094 /* Return true if the floating-point value computed by call STMT is known
9095 to have an integer value. We also allow +Inf, -Inf and NaN to be
9096 considered integer values. Return false for signaling NaN.
9098 DEPTH is the current nesting depth of the query. */
9101 gimple_call_integer_valued_real_p (gimple
*stmt
, int depth
)
9103 tree arg0
= (gimple_call_num_args (stmt
) > 0
9104 ? gimple_call_arg (stmt
, 0)
9106 tree arg1
= (gimple_call_num_args (stmt
) > 1
9107 ? gimple_call_arg (stmt
, 1)
9109 return integer_valued_real_call_p (gimple_call_combined_fn (stmt
),
9113 /* Return true if the floating-point result of phi STMT is known to have
9114 an integer value. We also allow +Inf, -Inf and NaN to be considered
9115 integer values. Return false for signaling NaN.
9117 DEPTH is the current nesting depth of the query. */
9120 gimple_phi_integer_valued_real_p (gimple
*stmt
, int depth
)
9122 for (unsigned i
= 0; i
< gimple_phi_num_args (stmt
); ++i
)
9124 tree arg
= gimple_phi_arg_def (stmt
, i
);
9125 if (!integer_valued_real_single_p (arg
, depth
+ 1))
9131 /* Return true if the floating-point value computed by STMT is known
9132 to have an integer value. We also allow +Inf, -Inf and NaN to be
9133 considered integer values. Return false for signaling NaN.
9135 DEPTH is the current nesting depth of the query. */
9138 gimple_stmt_integer_valued_real_p (gimple
*stmt
, int depth
)
9140 switch (gimple_code (stmt
))
9143 return gimple_assign_integer_valued_real_p (stmt
, depth
);
9145 return gimple_call_integer_valued_real_p (stmt
, depth
);
9147 return gimple_phi_integer_valued_real_p (stmt
, depth
);