1 /* Statement simplification on GIMPLE.
2 Copyright (C) 2010-2020 Free Software Foundation, Inc.
3 Split out from tree-ssa-ccp.c.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
32 #include "gimple-pretty-print.h"
33 #include "gimple-ssa-warn-restrict.h"
34 #include "fold-const.h"
37 #include "stor-layout.h"
39 #include "gimple-fold.h"
41 #include "gimple-iterator.h"
42 #include "tree-into-ssa.h"
44 #include "tree-object-size.h"
46 #include "tree-ssa-propagate.h"
47 #include "ipa-utils.h"
48 #include "tree-ssa-address.h"
49 #include "langhooks.h"
50 #include "gimplify-me.h"
54 #include "gimple-match.h"
55 #include "gomp-constants.h"
56 #include "optabs-query.h"
57 #include "omp-general.h"
59 #include "fold-const-call.h"
60 #include "stringpool.h"
63 #include "diagnostic-core.h"
66 #include "tree-vector-builder.h"
67 #include "tree-ssa-strlen.h"
69 enum strlen_range_kind
{
70 /* Compute the exact constant string length. */
72 /* Compute the maximum constant string length. */
74 /* Compute a range of string lengths bounded by object sizes. When
75 the length of a string cannot be determined, consider as the upper
76 bound the size of the enclosing object the string may be a member
77 or element of. Also determine the size of the largest character
78 array the string may refer to. */
80 /* Determine the integer value of the argument (not string length). */
85 get_range_strlen (tree
, bitmap
*, strlen_range_kind
, c_strlen_data
*, unsigned);
87 /* Return true when DECL can be referenced from current unit.
88 FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
89 We can get declarations that are not possible to reference for various
92 1) When analyzing C++ virtual tables.
93 C++ virtual tables do have known constructors even
94 when they are keyed to other compilation unit.
95 Those tables can contain pointers to methods and vars
96 in other units. Those methods have both STATIC and EXTERNAL
98 2) In WHOPR mode devirtualization might lead to reference
99 to method that was partitioned elsehwere.
100 In this case we have static VAR_DECL or FUNCTION_DECL
101 that has no corresponding callgraph/varpool node
103 3) COMDAT functions referred by external vtables that
104 we devirtualize only during final compilation stage.
105 At this time we already decided that we will not output
106 the function body and thus we can't reference the symbol
110 can_refer_decl_in_current_unit_p (tree decl
, tree from_decl
)
113 struct cgraph_node
*node
;
116 if (DECL_ABSTRACT_P (decl
))
119 /* We are concerned only about static/external vars and functions. */
120 if ((!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
121 || !VAR_OR_FUNCTION_DECL_P (decl
))
124 /* Static objects can be referred only if they are defined and not optimized
126 if (!TREE_PUBLIC (decl
))
128 if (DECL_EXTERNAL (decl
))
130 /* Before we start optimizing unreachable code we can be sure all
131 static objects are defined. */
132 if (symtab
->function_flags_ready
)
134 snode
= symtab_node::get (decl
);
135 if (!snode
|| !snode
->definition
)
137 node
= dyn_cast
<cgraph_node
*> (snode
);
138 return !node
|| !node
->inlined_to
;
141 /* We will later output the initializer, so we can refer to it.
142 So we are concerned only when DECL comes from initializer of
143 external var or var that has been optimized out. */
145 || !VAR_P (from_decl
)
146 || (!DECL_EXTERNAL (from_decl
)
147 && (vnode
= varpool_node::get (from_decl
)) != NULL
148 && vnode
->definition
)
150 && (vnode
= varpool_node::get (from_decl
)) != NULL
151 && vnode
->in_other_partition
))
153 /* We are folding reference from external vtable. The vtable may reffer
154 to a symbol keyed to other compilation unit. The other compilation
155 unit may be in separate DSO and the symbol may be hidden. */
156 if (DECL_VISIBILITY_SPECIFIED (decl
)
157 && DECL_EXTERNAL (decl
)
158 && DECL_VISIBILITY (decl
) != VISIBILITY_DEFAULT
159 && (!(snode
= symtab_node::get (decl
)) || !snode
->in_other_partition
))
161 /* When function is public, we always can introduce new reference.
162 Exception are the COMDAT functions where introducing a direct
163 reference imply need to include function body in the curren tunit. */
164 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
))
166 /* We have COMDAT. We are going to check if we still have definition
167 or if the definition is going to be output in other partition.
168 Bypass this when gimplifying; all needed functions will be produced.
170 As observed in PR20991 for already optimized out comdat virtual functions
171 it may be tempting to not necessarily give up because the copy will be
172 output elsewhere when corresponding vtable is output.
173 This is however not possible - ABI specify that COMDATs are output in
174 units where they are used and when the other unit was compiled with LTO
175 it is possible that vtable was kept public while the function itself
177 if (!symtab
->function_flags_ready
)
180 snode
= symtab_node::get (decl
);
182 || ((!snode
->definition
|| DECL_EXTERNAL (decl
))
183 && (!snode
->in_other_partition
184 || (!snode
->forced_by_abi
&& !snode
->force_output
))))
186 node
= dyn_cast
<cgraph_node
*> (snode
);
187 return !node
|| !node
->inlined_to
;
190 /* Create a temporary for TYPE for a statement STMT. If the current function
191 is in SSA form, a SSA name is created. Otherwise a temporary register
195 create_tmp_reg_or_ssa_name (tree type
, gimple
*stmt
)
197 if (gimple_in_ssa_p (cfun
))
198 return make_ssa_name (type
, stmt
);
200 return create_tmp_reg (type
);
203 /* CVAL is value taken from DECL_INITIAL of variable. Try to transform it into
204 acceptable form for is_gimple_min_invariant.
205 FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL. */
208 canonicalize_constructor_val (tree cval
, tree from_decl
)
210 if (CONSTANT_CLASS_P (cval
))
213 tree orig_cval
= cval
;
215 if (TREE_CODE (cval
) == POINTER_PLUS_EXPR
216 && TREE_CODE (TREE_OPERAND (cval
, 1)) == INTEGER_CST
)
218 tree ptr
= TREE_OPERAND (cval
, 0);
219 if (is_gimple_min_invariant (ptr
))
220 cval
= build1_loc (EXPR_LOCATION (cval
),
221 ADDR_EXPR
, TREE_TYPE (ptr
),
222 fold_build2 (MEM_REF
, TREE_TYPE (TREE_TYPE (ptr
)),
224 fold_convert (ptr_type_node
,
225 TREE_OPERAND (cval
, 1))));
227 if (TREE_CODE (cval
) == ADDR_EXPR
)
229 tree base
= NULL_TREE
;
230 if (TREE_CODE (TREE_OPERAND (cval
, 0)) == COMPOUND_LITERAL_EXPR
)
232 base
= COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval
, 0));
234 TREE_OPERAND (cval
, 0) = base
;
237 base
= get_base_address (TREE_OPERAND (cval
, 0));
241 if (VAR_OR_FUNCTION_DECL_P (base
)
242 && !can_refer_decl_in_current_unit_p (base
, from_decl
))
244 if (TREE_TYPE (base
) == error_mark_node
)
247 TREE_ADDRESSABLE (base
) = 1;
248 else if (TREE_CODE (base
) == FUNCTION_DECL
)
250 /* Make sure we create a cgraph node for functions we'll reference.
251 They can be non-existent if the reference comes from an entry
252 of an external vtable for example. */
253 cgraph_node::get_create (base
);
255 /* Fixup types in global initializers. */
256 if (TREE_TYPE (TREE_TYPE (cval
)) != TREE_TYPE (TREE_OPERAND (cval
, 0)))
257 cval
= build_fold_addr_expr (TREE_OPERAND (cval
, 0));
259 if (!useless_type_conversion_p (TREE_TYPE (orig_cval
), TREE_TYPE (cval
)))
260 cval
= fold_convert (TREE_TYPE (orig_cval
), cval
);
263 /* In CONSTRUCTORs we may see unfolded constants like (int (*) ()) 0. */
264 if (TREE_CODE (cval
) == INTEGER_CST
)
266 if (TREE_OVERFLOW_P (cval
))
267 cval
= drop_tree_overflow (cval
);
268 if (!useless_type_conversion_p (TREE_TYPE (orig_cval
), TREE_TYPE (cval
)))
269 cval
= fold_convert (TREE_TYPE (orig_cval
), cval
);
275 /* If SYM is a constant variable with known value, return the value.
276 NULL_TREE is returned otherwise. */
279 get_symbol_constant_value (tree sym
)
281 tree val
= ctor_for_folding (sym
);
282 if (val
!= error_mark_node
)
286 val
= canonicalize_constructor_val (unshare_expr (val
), sym
);
287 if (val
&& is_gimple_min_invariant (val
))
292 /* Variables declared 'const' without an initializer
293 have zero as the initializer if they may not be
294 overridden at link or run time. */
296 && is_gimple_reg_type (TREE_TYPE (sym
)))
297 return build_zero_cst (TREE_TYPE (sym
));
305 /* Subroutine of fold_stmt. We perform several simplifications of the
306 memory reference tree EXPR and make sure to re-gimplify them properly
307 after propagation of constant addresses. IS_LHS is true if the
308 reference is supposed to be an lvalue. */
311 maybe_fold_reference (tree expr
, bool is_lhs
)
315 if ((TREE_CODE (expr
) == VIEW_CONVERT_EXPR
316 || TREE_CODE (expr
) == REALPART_EXPR
317 || TREE_CODE (expr
) == IMAGPART_EXPR
)
318 && CONSTANT_CLASS_P (TREE_OPERAND (expr
, 0)))
319 return fold_unary_loc (EXPR_LOCATION (expr
),
322 TREE_OPERAND (expr
, 0));
323 else if (TREE_CODE (expr
) == BIT_FIELD_REF
324 && CONSTANT_CLASS_P (TREE_OPERAND (expr
, 0)))
325 return fold_ternary_loc (EXPR_LOCATION (expr
),
328 TREE_OPERAND (expr
, 0),
329 TREE_OPERAND (expr
, 1),
330 TREE_OPERAND (expr
, 2));
333 && (result
= fold_const_aggregate_ref (expr
))
334 && is_gimple_min_invariant (result
))
341 /* Attempt to fold an assignment statement pointed-to by SI. Returns a
342 replacement rhs for the statement or NULL_TREE if no simplification
343 could be made. It is assumed that the operands have been previously
347 fold_gimple_assign (gimple_stmt_iterator
*si
)
349 gimple
*stmt
= gsi_stmt (*si
);
350 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
351 location_t loc
= gimple_location (stmt
);
353 tree result
= NULL_TREE
;
355 switch (get_gimple_rhs_class (subcode
))
357 case GIMPLE_SINGLE_RHS
:
359 tree rhs
= gimple_assign_rhs1 (stmt
);
361 if (TREE_CLOBBER_P (rhs
))
364 if (REFERENCE_CLASS_P (rhs
))
365 return maybe_fold_reference (rhs
, false);
367 else if (TREE_CODE (rhs
) == OBJ_TYPE_REF
)
369 tree val
= OBJ_TYPE_REF_EXPR (rhs
);
370 if (is_gimple_min_invariant (val
))
372 else if (flag_devirtualize
&& virtual_method_call_p (rhs
))
375 vec
<cgraph_node
*>targets
376 = possible_polymorphic_call_targets (rhs
, stmt
, &final
);
377 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
379 if (dump_enabled_p ())
381 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, stmt
,
382 "resolving virtual function address "
383 "reference to function %s\n",
384 targets
.length () == 1
385 ? targets
[0]->name ()
388 if (targets
.length () == 1)
390 val
= fold_convert (TREE_TYPE (val
),
391 build_fold_addr_expr_loc
392 (loc
, targets
[0]->decl
));
393 STRIP_USELESS_TYPE_CONVERSION (val
);
396 /* We cannot use __builtin_unreachable here because it
397 cannot have address taken. */
398 val
= build_int_cst (TREE_TYPE (val
), 0);
404 else if (TREE_CODE (rhs
) == ADDR_EXPR
)
406 tree ref
= TREE_OPERAND (rhs
, 0);
407 tree tem
= maybe_fold_reference (ref
, true);
409 && TREE_CODE (tem
) == MEM_REF
410 && integer_zerop (TREE_OPERAND (tem
, 1)))
411 result
= fold_convert (TREE_TYPE (rhs
), TREE_OPERAND (tem
, 0));
413 result
= fold_convert (TREE_TYPE (rhs
),
414 build_fold_addr_expr_loc (loc
, tem
));
415 else if (TREE_CODE (ref
) == MEM_REF
416 && integer_zerop (TREE_OPERAND (ref
, 1)))
417 result
= fold_convert (TREE_TYPE (rhs
), TREE_OPERAND (ref
, 0));
421 /* Strip away useless type conversions. Both the
422 NON_LVALUE_EXPR that may have been added by fold, and
423 "useless" type conversions that might now be apparent
424 due to propagation. */
425 STRIP_USELESS_TYPE_CONVERSION (result
);
427 if (result
!= rhs
&& valid_gimple_rhs_p (result
))
432 else if (TREE_CODE (rhs
) == CONSTRUCTOR
433 && TREE_CODE (TREE_TYPE (rhs
)) == VECTOR_TYPE
)
435 /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */
439 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs
), i
, val
)
440 if (! CONSTANT_CLASS_P (val
))
443 return build_vector_from_ctor (TREE_TYPE (rhs
),
444 CONSTRUCTOR_ELTS (rhs
));
447 else if (DECL_P (rhs
))
448 return get_symbol_constant_value (rhs
);
452 case GIMPLE_UNARY_RHS
:
455 case GIMPLE_BINARY_RHS
:
458 case GIMPLE_TERNARY_RHS
:
459 result
= fold_ternary_loc (loc
, subcode
,
460 TREE_TYPE (gimple_assign_lhs (stmt
)),
461 gimple_assign_rhs1 (stmt
),
462 gimple_assign_rhs2 (stmt
),
463 gimple_assign_rhs3 (stmt
));
467 STRIP_USELESS_TYPE_CONVERSION (result
);
468 if (valid_gimple_rhs_p (result
))
473 case GIMPLE_INVALID_RHS
:
481 /* Replace a statement at *SI_P with a sequence of statements in STMTS,
482 adjusting the replacement stmts location and virtual operands.
483 If the statement has a lhs the last stmt in the sequence is expected
484 to assign to that lhs. */
487 gsi_replace_with_seq_vops (gimple_stmt_iterator
*si_p
, gimple_seq stmts
)
489 gimple
*stmt
= gsi_stmt (*si_p
);
491 if (gimple_has_location (stmt
))
492 annotate_all_with_location (stmts
, gimple_location (stmt
));
494 /* First iterate over the replacement statements backward, assigning
495 virtual operands to their defining statements. */
496 gimple
*laststore
= NULL
;
497 for (gimple_stmt_iterator i
= gsi_last (stmts
);
498 !gsi_end_p (i
); gsi_prev (&i
))
500 gimple
*new_stmt
= gsi_stmt (i
);
501 if ((gimple_assign_single_p (new_stmt
)
502 && !is_gimple_reg (gimple_assign_lhs (new_stmt
)))
503 || (is_gimple_call (new_stmt
)
504 && (gimple_call_flags (new_stmt
)
505 & (ECF_NOVOPS
| ECF_PURE
| ECF_CONST
| ECF_NORETURN
)) == 0))
509 vdef
= gimple_vdef (stmt
);
511 vdef
= make_ssa_name (gimple_vop (cfun
), new_stmt
);
512 gimple_set_vdef (new_stmt
, vdef
);
513 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
514 SSA_NAME_DEF_STMT (vdef
) = new_stmt
;
515 laststore
= new_stmt
;
519 /* Second iterate over the statements forward, assigning virtual
520 operands to their uses. */
521 tree reaching_vuse
= gimple_vuse (stmt
);
522 for (gimple_stmt_iterator i
= gsi_start (stmts
);
523 !gsi_end_p (i
); gsi_next (&i
))
525 gimple
*new_stmt
= gsi_stmt (i
);
526 /* If the new statement possibly has a VUSE, update it with exact SSA
527 name we know will reach this one. */
528 if (gimple_has_mem_ops (new_stmt
))
529 gimple_set_vuse (new_stmt
, reaching_vuse
);
530 gimple_set_modified (new_stmt
, true);
531 if (gimple_vdef (new_stmt
))
532 reaching_vuse
= gimple_vdef (new_stmt
);
535 /* If the new sequence does not do a store release the virtual
536 definition of the original statement. */
538 && reaching_vuse
== gimple_vuse (stmt
))
540 tree vdef
= gimple_vdef (stmt
);
542 && TREE_CODE (vdef
) == SSA_NAME
)
544 unlink_stmt_vdef (stmt
);
545 release_ssa_name (vdef
);
549 /* Finally replace the original statement with the sequence. */
550 gsi_replace_with_seq (si_p
, stmts
, false);
553 /* Convert EXPR into a GIMPLE value suitable for substitution on the
554 RHS of an assignment. Insert the necessary statements before
555 iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL
556 is replaced. If the call is expected to produces a result, then it
557 is replaced by an assignment of the new RHS to the result variable.
558 If the result is to be ignored, then the call is replaced by a
559 GIMPLE_NOP. A proper VDEF chain is retained by making the first
560 VUSE and the last VDEF of the whole sequence be the same as the replaced
561 statement and using new SSA names for stores in between. */
564 gimplify_and_update_call_from_tree (gimple_stmt_iterator
*si_p
, tree expr
)
567 gimple
*stmt
, *new_stmt
;
568 gimple_stmt_iterator i
;
569 gimple_seq stmts
= NULL
;
571 stmt
= gsi_stmt (*si_p
);
573 gcc_assert (is_gimple_call (stmt
));
575 push_gimplify_context (gimple_in_ssa_p (cfun
));
577 lhs
= gimple_call_lhs (stmt
);
578 if (lhs
== NULL_TREE
)
580 gimplify_and_add (expr
, &stmts
);
581 /* We can end up with folding a memcpy of an empty class assignment
582 which gets optimized away by C++ gimplification. */
583 if (gimple_seq_empty_p (stmts
))
585 pop_gimplify_context (NULL
);
586 if (gimple_in_ssa_p (cfun
))
588 unlink_stmt_vdef (stmt
);
591 gsi_replace (si_p
, gimple_build_nop (), false);
597 tree tmp
= force_gimple_operand (expr
, &stmts
, false, NULL_TREE
);
598 new_stmt
= gimple_build_assign (lhs
, tmp
);
599 i
= gsi_last (stmts
);
600 gsi_insert_after_without_update (&i
, new_stmt
,
601 GSI_CONTINUE_LINKING
);
604 pop_gimplify_context (NULL
);
606 gsi_replace_with_seq_vops (si_p
, stmts
);
610 /* Replace the call at *GSI with the gimple value VAL. */
613 replace_call_with_value (gimple_stmt_iterator
*gsi
, tree val
)
615 gimple
*stmt
= gsi_stmt (*gsi
);
616 tree lhs
= gimple_call_lhs (stmt
);
620 if (!useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (val
)))
621 val
= fold_convert (TREE_TYPE (lhs
), val
);
622 repl
= gimple_build_assign (lhs
, val
);
625 repl
= gimple_build_nop ();
626 tree vdef
= gimple_vdef (stmt
);
627 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
629 unlink_stmt_vdef (stmt
);
630 release_ssa_name (vdef
);
632 gsi_replace (gsi
, repl
, false);
635 /* Replace the call at *GSI with the new call REPL and fold that
639 replace_call_with_call_and_fold (gimple_stmt_iterator
*gsi
, gimple
*repl
)
641 gimple
*stmt
= gsi_stmt (*gsi
);
642 gimple_call_set_lhs (repl
, gimple_call_lhs (stmt
));
643 gimple_set_location (repl
, gimple_location (stmt
));
644 gimple_move_vops (repl
, stmt
);
645 gsi_replace (gsi
, repl
, false);
649 /* Return true if VAR is a VAR_DECL or a component thereof. */
652 var_decl_component_p (tree var
)
655 while (handled_component_p (inner
))
656 inner
= TREE_OPERAND (inner
, 0);
657 return (DECL_P (inner
)
658 || (TREE_CODE (inner
) == MEM_REF
659 && TREE_CODE (TREE_OPERAND (inner
, 0)) == ADDR_EXPR
));
662 /* Return TRUE if the SIZE argument, representing the size of an
663 object, is in a range of values of which exactly zero is valid. */
666 size_must_be_zero_p (tree size
)
668 if (integer_zerop (size
))
671 if (TREE_CODE (size
) != SSA_NAME
|| !INTEGRAL_TYPE_P (TREE_TYPE (size
)))
674 tree type
= TREE_TYPE (size
);
675 int prec
= TYPE_PRECISION (type
);
677 /* Compute the value of SSIZE_MAX, the largest positive value that
678 can be stored in ssize_t, the signed counterpart of size_t. */
679 wide_int ssize_max
= wi::lshift (wi::one (prec
), prec
- 1) - 1;
680 value_range
valid_range (build_int_cst (type
, 0),
681 wide_int_to_tree (type
, ssize_max
));
683 get_range_info (size
, vr
);
684 vr
.intersect (&valid_range
);
688 /* Fold function call to builtin mem{{,p}cpy,move}. Try to detect and
689 diagnose (otherwise undefined) overlapping copies without preventing
690 folding. When folded, GCC guarantees that overlapping memcpy has
691 the same semantics as memmove. Call to the library memcpy need not
692 provide the same guarantee. Return false if no simplification can
696 gimple_fold_builtin_memory_op (gimple_stmt_iterator
*gsi
,
697 tree dest
, tree src
, enum built_in_function code
)
699 gimple
*stmt
= gsi_stmt (*gsi
);
700 tree lhs
= gimple_call_lhs (stmt
);
701 tree len
= gimple_call_arg (stmt
, 2);
702 tree destvar
, srcvar
;
703 location_t loc
= gimple_location (stmt
);
705 /* If the LEN parameter is a constant zero or in range where
706 the only valid value is zero, return DEST. */
707 if (size_must_be_zero_p (len
))
710 if (gimple_call_lhs (stmt
))
711 repl
= gimple_build_assign (gimple_call_lhs (stmt
), dest
);
713 repl
= gimple_build_nop ();
714 tree vdef
= gimple_vdef (stmt
);
715 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
717 unlink_stmt_vdef (stmt
);
718 release_ssa_name (vdef
);
720 gsi_replace (gsi
, repl
, false);
724 /* If SRC and DEST are the same (and not volatile), return
725 DEST{,+LEN,+LEN-1}. */
726 if (operand_equal_p (src
, dest
, 0))
728 /* Avoid diagnosing exact overlap in calls to __builtin_memcpy.
729 It's safe and may even be emitted by GCC itself (see bug
731 unlink_stmt_vdef (stmt
);
732 if (gimple_vdef (stmt
) && TREE_CODE (gimple_vdef (stmt
)) == SSA_NAME
)
733 release_ssa_name (gimple_vdef (stmt
));
736 gsi_replace (gsi
, gimple_build_nop (), false);
743 tree srctype
, desttype
;
744 unsigned int src_align
, dest_align
;
747 unsigned HOST_WIDE_INT tmp_len
;
749 /* Build accesses at offset zero with a ref-all character type. */
750 off0
= build_int_cst (build_pointer_type_for_mode (char_type_node
,
753 /* If we can perform the copy efficiently with first doing all loads
754 and then all stores inline it that way. Currently efficiently
755 means that we can load all the memory into a single integer
756 register which is what MOVE_MAX gives us. */
757 src_align
= get_pointer_alignment (src
);
758 dest_align
= get_pointer_alignment (dest
);
759 if (tree_fits_uhwi_p (len
)
760 && compare_tree_int (len
, MOVE_MAX
) <= 0
761 /* FIXME: Don't transform copies from strings with known length.
762 Until GCC 9 this prevented a case in gcc.dg/strlenopt-8.c
763 from being handled, and the case was XFAILed for that reason.
764 Now that it is handled and the XFAIL removed, as soon as other
765 strlenopt tests that rely on it for passing are adjusted, this
766 hack can be removed. */
767 && !c_strlen (src
, 1)
768 && !((tmp_str
= c_getstr (src
, &tmp_len
)) != NULL
769 && memchr (tmp_str
, 0, tmp_len
) == NULL
))
771 unsigned ilen
= tree_to_uhwi (len
);
772 if (pow2p_hwi (ilen
))
774 /* Detect out-of-bounds accesses without issuing warnings.
775 Avoid folding out-of-bounds copies but to avoid false
776 positives for unreachable code defer warning until after
777 DCE has worked its magic.
778 -Wrestrict is still diagnosed. */
779 if (int warning
= check_bounds_or_overlap (as_a
<gcall
*>(stmt
),
782 if (warning
!= OPT_Wrestrict
)
785 scalar_int_mode mode
;
786 tree type
= lang_hooks
.types
.type_for_size (ilen
* 8, 1);
788 && is_a
<scalar_int_mode
> (TYPE_MODE (type
), &mode
)
789 && GET_MODE_SIZE (mode
) * BITS_PER_UNIT
== ilen
* 8
790 /* If the destination pointer is not aligned we must be able
791 to emit an unaligned store. */
792 && (dest_align
>= GET_MODE_ALIGNMENT (mode
)
793 || !targetm
.slow_unaligned_access (mode
, dest_align
)
794 || (optab_handler (movmisalign_optab
, mode
)
795 != CODE_FOR_nothing
)))
798 tree desttype
= type
;
799 if (src_align
< GET_MODE_ALIGNMENT (mode
))
800 srctype
= build_aligned_type (type
, src_align
);
801 tree srcmem
= fold_build2 (MEM_REF
, srctype
, src
, off0
);
802 tree tem
= fold_const_aggregate_ref (srcmem
);
805 else if (src_align
< GET_MODE_ALIGNMENT (mode
)
806 && targetm
.slow_unaligned_access (mode
, src_align
)
807 && (optab_handler (movmisalign_optab
, mode
)
808 == CODE_FOR_nothing
))
813 if (is_gimple_reg_type (TREE_TYPE (srcmem
)))
815 new_stmt
= gimple_build_assign (NULL_TREE
, srcmem
);
817 = create_tmp_reg_or_ssa_name (TREE_TYPE (srcmem
),
819 gimple_assign_set_lhs (new_stmt
, srcmem
);
820 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
821 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
823 if (dest_align
< GET_MODE_ALIGNMENT (mode
))
824 desttype
= build_aligned_type (type
, dest_align
);
826 = gimple_build_assign (fold_build2 (MEM_REF
, desttype
,
829 gimple_move_vops (new_stmt
, stmt
);
832 gsi_replace (gsi
, new_stmt
, false);
835 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
842 if (code
== BUILT_IN_MEMMOVE
)
844 /* Both DEST and SRC must be pointer types.
845 ??? This is what old code did. Is the testing for pointer types
848 If either SRC is readonly or length is 1, we can use memcpy. */
849 if (!dest_align
|| !src_align
)
851 if (readonly_data_expr (src
)
852 || (tree_fits_uhwi_p (len
)
853 && (MIN (src_align
, dest_align
) / BITS_PER_UNIT
854 >= tree_to_uhwi (len
))))
856 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
859 gimple_call_set_fndecl (stmt
, fn
);
860 gimple_call_set_arg (stmt
, 0, dest
);
861 gimple_call_set_arg (stmt
, 1, src
);
866 /* If *src and *dest can't overlap, optimize into memcpy as well. */
867 if (TREE_CODE (src
) == ADDR_EXPR
868 && TREE_CODE (dest
) == ADDR_EXPR
)
870 tree src_base
, dest_base
, fn
;
871 poly_int64 src_offset
= 0, dest_offset
= 0;
874 srcvar
= TREE_OPERAND (src
, 0);
875 src_base
= get_addr_base_and_unit_offset (srcvar
, &src_offset
);
876 if (src_base
== NULL
)
878 destvar
= TREE_OPERAND (dest
, 0);
879 dest_base
= get_addr_base_and_unit_offset (destvar
,
881 if (dest_base
== NULL
)
883 if (!poly_int_tree_p (len
, &maxsize
))
885 if (SSA_VAR_P (src_base
)
886 && SSA_VAR_P (dest_base
))
888 if (operand_equal_p (src_base
, dest_base
, 0)
889 && ranges_maybe_overlap_p (src_offset
, maxsize
,
890 dest_offset
, maxsize
))
893 else if (TREE_CODE (src_base
) == MEM_REF
894 && TREE_CODE (dest_base
) == MEM_REF
)
896 if (! operand_equal_p (TREE_OPERAND (src_base
, 0),
897 TREE_OPERAND (dest_base
, 0), 0))
899 poly_offset_int full_src_offset
900 = mem_ref_offset (src_base
) + src_offset
;
901 poly_offset_int full_dest_offset
902 = mem_ref_offset (dest_base
) + dest_offset
;
903 if (ranges_maybe_overlap_p (full_src_offset
, maxsize
,
904 full_dest_offset
, maxsize
))
910 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
913 gimple_call_set_fndecl (stmt
, fn
);
914 gimple_call_set_arg (stmt
, 0, dest
);
915 gimple_call_set_arg (stmt
, 1, src
);
920 /* If the destination and source do not alias optimize into
922 if ((is_gimple_min_invariant (dest
)
923 || TREE_CODE (dest
) == SSA_NAME
)
924 && (is_gimple_min_invariant (src
)
925 || TREE_CODE (src
) == SSA_NAME
))
928 ao_ref_init_from_ptr_and_size (&destr
, dest
, len
);
929 ao_ref_init_from_ptr_and_size (&srcr
, src
, len
);
930 if (!refs_may_alias_p_1 (&destr
, &srcr
, false))
933 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
936 gimple_call_set_fndecl (stmt
, fn
);
937 gimple_call_set_arg (stmt
, 0, dest
);
938 gimple_call_set_arg (stmt
, 1, src
);
947 if (!tree_fits_shwi_p (len
))
949 if (!POINTER_TYPE_P (TREE_TYPE (src
))
950 || !POINTER_TYPE_P (TREE_TYPE (dest
)))
952 /* In the following try to find a type that is most natural to be
953 used for the memcpy source and destination and that allows
954 the most optimization when memcpy is turned into a plain assignment
955 using that type. In theory we could always use a char[len] type
956 but that only gains us that the destination and source possibly
957 no longer will have their address taken. */
958 srctype
= TREE_TYPE (TREE_TYPE (src
));
959 if (TREE_CODE (srctype
) == ARRAY_TYPE
960 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype
), len
))
961 srctype
= TREE_TYPE (srctype
);
962 desttype
= TREE_TYPE (TREE_TYPE (dest
));
963 if (TREE_CODE (desttype
) == ARRAY_TYPE
964 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype
), len
))
965 desttype
= TREE_TYPE (desttype
);
966 if (TREE_ADDRESSABLE (srctype
)
967 || TREE_ADDRESSABLE (desttype
))
970 /* Make sure we are not copying using a floating-point mode or
971 a type whose size possibly does not match its precision. */
972 if (FLOAT_MODE_P (TYPE_MODE (desttype
))
973 || TREE_CODE (desttype
) == BOOLEAN_TYPE
974 || TREE_CODE (desttype
) == ENUMERAL_TYPE
)
975 desttype
= bitwise_type_for_mode (TYPE_MODE (desttype
));
976 if (FLOAT_MODE_P (TYPE_MODE (srctype
))
977 || TREE_CODE (srctype
) == BOOLEAN_TYPE
978 || TREE_CODE (srctype
) == ENUMERAL_TYPE
)
979 srctype
= bitwise_type_for_mode (TYPE_MODE (srctype
));
987 src_align
= get_pointer_alignment (src
);
988 dest_align
= get_pointer_alignment (dest
);
990 /* Choose between src and destination type for the access based
991 on alignment, whether the access constitutes a register access
992 and whether it may actually expose a declaration for SSA rewrite
993 or SRA decomposition. */
996 if (TREE_CODE (dest
) == ADDR_EXPR
997 && var_decl_component_p (TREE_OPERAND (dest
, 0))
998 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype
), len
)
999 && dest_align
>= TYPE_ALIGN (desttype
)
1000 && (is_gimple_reg_type (desttype
)
1001 || src_align
>= TYPE_ALIGN (desttype
)))
1002 destvar
= fold_build2 (MEM_REF
, desttype
, dest
, off0
);
1003 else if (TREE_CODE (src
) == ADDR_EXPR
1004 && var_decl_component_p (TREE_OPERAND (src
, 0))
1005 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype
), len
)
1006 && src_align
>= TYPE_ALIGN (srctype
)
1007 && (is_gimple_reg_type (srctype
)
1008 || dest_align
>= TYPE_ALIGN (srctype
)))
1009 srcvar
= fold_build2 (MEM_REF
, srctype
, src
, off0
);
1010 if (srcvar
== NULL_TREE
&& destvar
== NULL_TREE
)
1013 /* Now that we chose an access type express the other side in
1014 terms of it if the target allows that with respect to alignment
1016 if (srcvar
== NULL_TREE
)
1018 if (src_align
>= TYPE_ALIGN (desttype
))
1019 srcvar
= fold_build2 (MEM_REF
, desttype
, src
, off0
);
1022 if (STRICT_ALIGNMENT
)
1024 srctype
= build_aligned_type (TYPE_MAIN_VARIANT (desttype
),
1026 srcvar
= fold_build2 (MEM_REF
, srctype
, src
, off0
);
1029 else if (destvar
== NULL_TREE
)
1031 if (dest_align
>= TYPE_ALIGN (srctype
))
1032 destvar
= fold_build2 (MEM_REF
, srctype
, dest
, off0
);
1035 if (STRICT_ALIGNMENT
)
1037 desttype
= build_aligned_type (TYPE_MAIN_VARIANT (srctype
),
1039 destvar
= fold_build2 (MEM_REF
, desttype
, dest
, off0
);
1043 /* Same as above, detect out-of-bounds accesses without issuing
1044 warnings. Avoid folding out-of-bounds copies but to avoid
1045 false positives for unreachable code defer warning until
1046 after DCE has worked its magic.
1047 -Wrestrict is still diagnosed. */
1048 if (int warning
= check_bounds_or_overlap (as_a
<gcall
*>(stmt
),
1049 dest
, src
, len
, len
,
1051 if (warning
!= OPT_Wrestrict
)
1055 if (is_gimple_reg_type (TREE_TYPE (srcvar
)))
1057 tree tem
= fold_const_aggregate_ref (srcvar
);
1060 if (! is_gimple_min_invariant (srcvar
))
1062 new_stmt
= gimple_build_assign (NULL_TREE
, srcvar
);
1063 srcvar
= create_tmp_reg_or_ssa_name (TREE_TYPE (srcvar
),
1065 gimple_assign_set_lhs (new_stmt
, srcvar
);
1066 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
1067 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
1069 new_stmt
= gimple_build_assign (destvar
, srcvar
);
1070 goto set_vop_and_replace
;
1073 /* We get an aggregate copy. Use an unsigned char[] type to
1074 perform the copying to preserve padding and to avoid any issues
1075 with TREE_ADDRESSABLE types or float modes behavior on copying. */
1076 desttype
= build_array_type_nelts (unsigned_char_type_node
,
1077 tree_to_uhwi (len
));
1079 if (src_align
> TYPE_ALIGN (srctype
))
1080 srctype
= build_aligned_type (srctype
, src_align
);
1081 if (dest_align
> TYPE_ALIGN (desttype
))
1082 desttype
= build_aligned_type (desttype
, dest_align
);
1084 = gimple_build_assign (fold_build2 (MEM_REF
, desttype
, dest
, off0
),
1085 fold_build2 (MEM_REF
, srctype
, src
, off0
));
1086 set_vop_and_replace
:
1087 gimple_move_vops (new_stmt
, stmt
);
1090 gsi_replace (gsi
, new_stmt
, false);
1093 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
1097 gimple_seq stmts
= NULL
;
1098 if (code
== BUILT_IN_MEMCPY
|| code
== BUILT_IN_MEMMOVE
)
1100 else if (code
== BUILT_IN_MEMPCPY
)
1102 len
= gimple_convert_to_ptrofftype (&stmts
, loc
, len
);
1103 dest
= gimple_build (&stmts
, loc
, POINTER_PLUS_EXPR
,
1104 TREE_TYPE (dest
), dest
, len
);
1109 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1110 gimple
*repl
= gimple_build_assign (lhs
, dest
);
1111 gsi_replace (gsi
, repl
, false);
1115 /* Transform a call to built-in bcmp(a, b, len) at *GSI into one
1116 to built-in memcmp (a, b, len). */
1119 gimple_fold_builtin_bcmp (gimple_stmt_iterator
*gsi
)
1121 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCMP
);
1126 /* Transform bcmp (a, b, len) into memcmp (a, b, len). */
1128 gimple
*stmt
= gsi_stmt (*gsi
);
1129 tree a
= gimple_call_arg (stmt
, 0);
1130 tree b
= gimple_call_arg (stmt
, 1);
1131 tree len
= gimple_call_arg (stmt
, 2);
1133 gimple
*repl
= gimple_build_call (fn
, 3, a
, b
, len
);
1134 replace_call_with_call_and_fold (gsi
, repl
);
1139 /* Transform a call to built-in bcopy (src, dest, len) at *GSI into one
1140 to built-in memmove (dest, src, len). */
1143 gimple_fold_builtin_bcopy (gimple_stmt_iterator
*gsi
)
1145 tree fn
= builtin_decl_implicit (BUILT_IN_MEMMOVE
);
1150 /* bcopy has been removed from POSIX in Issue 7 but Issue 6 specifies
1151 it's quivalent to memmove (not memcpy). Transform bcopy (src, dest,
1152 len) into memmove (dest, src, len). */
1154 gimple
*stmt
= gsi_stmt (*gsi
);
1155 tree src
= gimple_call_arg (stmt
, 0);
1156 tree dest
= gimple_call_arg (stmt
, 1);
1157 tree len
= gimple_call_arg (stmt
, 2);
1159 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1160 gimple_call_set_fntype (as_a
<gcall
*> (stmt
), TREE_TYPE (fn
));
1161 replace_call_with_call_and_fold (gsi
, repl
);
1166 /* Transform a call to built-in bzero (dest, len) at *GSI into one
1167 to built-in memset (dest, 0, len). */
1170 gimple_fold_builtin_bzero (gimple_stmt_iterator
*gsi
)
1172 tree fn
= builtin_decl_implicit (BUILT_IN_MEMSET
);
1177 /* Transform bzero (dest, len) into memset (dest, 0, len). */
1179 gimple
*stmt
= gsi_stmt (*gsi
);
1180 tree dest
= gimple_call_arg (stmt
, 0);
1181 tree len
= gimple_call_arg (stmt
, 1);
1183 gimple_seq seq
= NULL
;
1184 gimple
*repl
= gimple_build_call (fn
, 3, dest
, integer_zero_node
, len
);
1185 gimple_seq_add_stmt_without_update (&seq
, repl
);
1186 gsi_replace_with_seq_vops (gsi
, seq
);
1192 /* Fold function call to builtin memset or bzero at *GSI setting the
1193 memory of size LEN to VAL. Return whether a simplification was made. */
1196 gimple_fold_builtin_memset (gimple_stmt_iterator
*gsi
, tree c
, tree len
)
1198 gimple
*stmt
= gsi_stmt (*gsi
);
1200 unsigned HOST_WIDE_INT length
, cval
;
1202 /* If the LEN parameter is zero, return DEST. */
1203 if (integer_zerop (len
))
1205 replace_call_with_value (gsi
, gimple_call_arg (stmt
, 0));
1209 if (! tree_fits_uhwi_p (len
))
1212 if (TREE_CODE (c
) != INTEGER_CST
)
1215 tree dest
= gimple_call_arg (stmt
, 0);
1217 if (TREE_CODE (var
) != ADDR_EXPR
)
1220 var
= TREE_OPERAND (var
, 0);
1221 if (TREE_THIS_VOLATILE (var
))
1224 etype
= TREE_TYPE (var
);
1225 if (TREE_CODE (etype
) == ARRAY_TYPE
)
1226 etype
= TREE_TYPE (etype
);
1228 if (!INTEGRAL_TYPE_P (etype
)
1229 && !POINTER_TYPE_P (etype
))
1232 if (! var_decl_component_p (var
))
1235 length
= tree_to_uhwi (len
);
1236 if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (etype
)) != length
1237 || get_pointer_alignment (dest
) / BITS_PER_UNIT
< length
)
1240 if (length
> HOST_BITS_PER_WIDE_INT
/ BITS_PER_UNIT
)
1243 if (integer_zerop (c
))
1247 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8 || HOST_BITS_PER_WIDE_INT
> 64)
1250 cval
= TREE_INT_CST_LOW (c
);
1254 cval
|= (cval
<< 31) << 1;
1257 var
= fold_build2 (MEM_REF
, etype
, dest
, build_int_cst (ptr_type_node
, 0));
1258 gimple
*store
= gimple_build_assign (var
, build_int_cst_type (etype
, cval
));
1259 gimple_move_vops (store
, stmt
);
1260 gsi_insert_before (gsi
, store
, GSI_SAME_STMT
);
1261 if (gimple_call_lhs (stmt
))
1263 gimple
*asgn
= gimple_build_assign (gimple_call_lhs (stmt
), dest
);
1264 gsi_replace (gsi
, asgn
, false);
1268 gimple_stmt_iterator gsi2
= *gsi
;
1270 gsi_remove (&gsi2
, true);
1276 /* Helper of get_range_strlen for ARG that is not an SSA_NAME. */
1279 get_range_strlen_tree (tree arg
, bitmap
*visited
, strlen_range_kind rkind
,
1280 c_strlen_data
*pdata
, unsigned eltsize
)
1282 gcc_assert (TREE_CODE (arg
) != SSA_NAME
);
1284 /* The length computed by this invocation of the function. */
1285 tree val
= NULL_TREE
;
1287 /* True if VAL is an optimistic (tight) bound determined from
1288 the size of the character array in which the string may be
1289 stored. In that case, the computed VAL is used to set
1291 bool tight_bound
= false;
1293 /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */
1294 if (TREE_CODE (arg
) == ADDR_EXPR
1295 && TREE_CODE (TREE_OPERAND (arg
, 0)) == ARRAY_REF
)
1297 tree op
= TREE_OPERAND (arg
, 0);
1298 if (integer_zerop (TREE_OPERAND (op
, 1)))
1300 tree aop0
= TREE_OPERAND (op
, 0);
1301 if (TREE_CODE (aop0
) == INDIRECT_REF
1302 && TREE_CODE (TREE_OPERAND (aop0
, 0)) == SSA_NAME
)
1303 return get_range_strlen (TREE_OPERAND (aop0
, 0), visited
, rkind
,
1306 else if (TREE_CODE (TREE_OPERAND (op
, 0)) == COMPONENT_REF
1307 && rkind
== SRK_LENRANGE
)
1309 /* Fail if an array is the last member of a struct object
1310 since it could be treated as a (fake) flexible array
1312 tree idx
= TREE_OPERAND (op
, 1);
1314 arg
= TREE_OPERAND (op
, 0);
1315 tree optype
= TREE_TYPE (arg
);
1316 if (tree dom
= TYPE_DOMAIN (optype
))
1317 if (tree bound
= TYPE_MAX_VALUE (dom
))
1318 if (TREE_CODE (bound
) == INTEGER_CST
1319 && TREE_CODE (idx
) == INTEGER_CST
1320 && tree_int_cst_lt (bound
, idx
))
1325 if (rkind
== SRK_INT_VALUE
)
1327 /* We are computing the maximum value (not string length). */
1329 if (TREE_CODE (val
) != INTEGER_CST
1330 || tree_int_cst_sgn (val
) < 0)
1335 c_strlen_data lendata
= { };
1336 val
= c_strlen (arg
, 1, &lendata
, eltsize
);
1338 if (!val
&& lendata
.decl
)
1340 /* ARG refers to an unterminated const character array.
1341 DATA.DECL with size DATA.LEN. */
1342 val
= lendata
.minlen
;
1343 pdata
->decl
= lendata
.decl
;
1347 /* Set if VAL represents the maximum length based on array size (set
1348 when exact length cannot be determined). */
1349 bool maxbound
= false;
1351 if (!val
&& rkind
== SRK_LENRANGE
)
1353 if (TREE_CODE (arg
) == ADDR_EXPR
)
1354 return get_range_strlen (TREE_OPERAND (arg
, 0), visited
, rkind
,
1357 if (TREE_CODE (arg
) == ARRAY_REF
)
1359 tree optype
= TREE_TYPE (TREE_OPERAND (arg
, 0));
1361 /* Determine the "innermost" array type. */
1362 while (TREE_CODE (optype
) == ARRAY_TYPE
1363 && TREE_CODE (TREE_TYPE (optype
)) == ARRAY_TYPE
)
1364 optype
= TREE_TYPE (optype
);
1366 /* Avoid arrays of pointers. */
1367 tree eltype
= TREE_TYPE (optype
);
1368 if (TREE_CODE (optype
) != ARRAY_TYPE
1369 || !INTEGRAL_TYPE_P (eltype
))
1372 /* Fail when the array bound is unknown or zero. */
1373 val
= TYPE_SIZE_UNIT (optype
);
1374 if (!val
|| integer_zerop (val
))
1377 val
= fold_build2 (MINUS_EXPR
, TREE_TYPE (val
), val
,
1380 /* Set the minimum size to zero since the string in
1381 the array could have zero length. */
1382 pdata
->minlen
= ssize_int (0);
1386 else if (TREE_CODE (arg
) == COMPONENT_REF
1387 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (arg
, 1)))
1390 /* Use the type of the member array to determine the upper
1391 bound on the length of the array. This may be overly
1392 optimistic if the array itself isn't NUL-terminated and
1393 the caller relies on the subsequent member to contain
1394 the NUL but that would only be considered valid if
1395 the array were the last member of a struct. */
1397 tree fld
= TREE_OPERAND (arg
, 1);
1399 tree optype
= TREE_TYPE (fld
);
1401 /* Determine the "innermost" array type. */
1402 while (TREE_CODE (optype
) == ARRAY_TYPE
1403 && TREE_CODE (TREE_TYPE (optype
)) == ARRAY_TYPE
)
1404 optype
= TREE_TYPE (optype
);
1406 /* Fail when the array bound is unknown or zero. */
1407 val
= TYPE_SIZE_UNIT (optype
);
1408 if (!val
|| integer_zerop (val
))
1410 val
= fold_build2 (MINUS_EXPR
, TREE_TYPE (val
), val
,
1413 /* Set the minimum size to zero since the string in
1414 the array could have zero length. */
1415 pdata
->minlen
= ssize_int (0);
1417 /* The array size determined above is an optimistic bound
1418 on the length. If the array isn't nul-terminated the
1419 length computed by the library function would be greater.
1420 Even though using strlen to cross the subobject boundary
1421 is undefined, avoid drawing conclusions from the member
1422 type about the length here. */
1425 else if (VAR_P (arg
))
1427 /* Avoid handling pointers to arrays. GCC might misuse
1428 a pointer to an array of one bound to point to an array
1429 object of a greater bound. */
1430 tree argtype
= TREE_TYPE (arg
);
1431 if (TREE_CODE (argtype
) == ARRAY_TYPE
)
1433 val
= TYPE_SIZE_UNIT (argtype
);
1435 || TREE_CODE (val
) != INTEGER_CST
1436 || integer_zerop (val
))
1438 val
= wide_int_to_tree (TREE_TYPE (val
),
1439 wi::sub (wi::to_wide (val
), 1));
1441 /* Set the minimum size to zero since the string in
1442 the array could have zero length. */
1443 pdata
->minlen
= ssize_int (0);
1452 /* Adjust the lower bound on the string length as necessary. */
1454 || (rkind
!= SRK_STRLEN
1455 && TREE_CODE (pdata
->minlen
) == INTEGER_CST
1456 && TREE_CODE (val
) == INTEGER_CST
1457 && tree_int_cst_lt (val
, pdata
->minlen
)))
1458 pdata
->minlen
= val
;
1460 if (pdata
->maxbound
&& TREE_CODE (pdata
->maxbound
) == INTEGER_CST
)
1462 /* Adjust the tighter (more optimistic) string length bound
1463 if necessary and proceed to adjust the more conservative
1465 if (TREE_CODE (val
) == INTEGER_CST
)
1467 if (tree_int_cst_lt (pdata
->maxbound
, val
))
1468 pdata
->maxbound
= val
;
1471 pdata
->maxbound
= val
;
1473 else if (pdata
->maxbound
|| maxbound
)
1474 /* Set PDATA->MAXBOUND only if it either isn't INTEGER_CST or
1475 if VAL corresponds to the maximum length determined based
1476 on the type of the object. */
1477 pdata
->maxbound
= val
;
1481 /* VAL computed above represents an optimistically tight bound
1482 on the length of the string based on the referenced object's
1483 or subobject's type. Determine the conservative upper bound
1484 based on the enclosing object's size if possible. */
1485 if (rkind
== SRK_LENRANGE
)
1488 tree base
= get_addr_base_and_unit_offset (arg
, &offset
);
1491 /* When the call above fails due to a non-constant offset
1492 assume the offset is zero and use the size of the whole
1493 enclosing object instead. */
1494 base
= get_base_address (arg
);
1497 /* If the base object is a pointer no upper bound on the length
1498 can be determined. Otherwise the maximum length is equal to
1499 the size of the enclosing object minus the offset of
1500 the referenced subobject minus 1 (for the terminating nul). */
1501 tree type
= TREE_TYPE (base
);
1502 if (TREE_CODE (type
) == POINTER_TYPE
1503 || !VAR_P (base
) || !(val
= DECL_SIZE_UNIT (base
)))
1504 val
= build_all_ones_cst (size_type_node
);
1507 val
= DECL_SIZE_UNIT (base
);
1508 val
= fold_build2 (MINUS_EXPR
, TREE_TYPE (val
), val
,
1509 size_int (offset
+ 1));
1518 /* Adjust the more conservative bound if possible/necessary
1519 and fail otherwise. */
1520 if (rkind
!= SRK_STRLEN
)
1522 if (TREE_CODE (pdata
->maxlen
) != INTEGER_CST
1523 || TREE_CODE (val
) != INTEGER_CST
)
1526 if (tree_int_cst_lt (pdata
->maxlen
, val
))
1527 pdata
->maxlen
= val
;
1530 else if (simple_cst_equal (val
, pdata
->maxlen
) != 1)
1532 /* Fail if the length of this ARG is different from that
1533 previously determined from another ARG. */
1538 pdata
->maxlen
= val
;
1539 return rkind
== SRK_LENRANGE
|| !integer_all_onesp (val
);
1542 /* For an ARG referencing one or more strings, try to obtain the range
1543 of their lengths, or the size of the largest array ARG referes to if
1544 the range of lengths cannot be determined, and store all in *PDATA.
1545 For an integer ARG (when RKIND == SRK_INT_VALUE), try to determine
1546 the maximum constant value.
1547 If ARG is an SSA_NAME, follow its use-def chains. When RKIND ==
1548 SRK_STRLEN, then if PDATA->MAXLEN is not equal to the determined
1549 length or if we are unable to determine the length, return false.
1550 VISITED is a bitmap of visited variables.
1551 RKIND determines the kind of value or range to obtain (see
1553 Set PDATA->DECL if ARG refers to an unterminated constant array.
1554 On input, set ELTSIZE to 1 for normal single byte character strings,
1555 and either 2 or 4 for wide characer strings (the size of wchar_t).
1556 Return true if *PDATA was successfully populated and false otherwise. */
1559 get_range_strlen (tree arg
, bitmap
*visited
,
1560 strlen_range_kind rkind
,
1561 c_strlen_data
*pdata
, unsigned eltsize
)
1564 if (TREE_CODE (arg
) != SSA_NAME
)
1565 return get_range_strlen_tree (arg
, visited
, rkind
, pdata
, eltsize
);
1567 /* If ARG is registered for SSA update we cannot look at its defining
1569 if (name_registered_for_update_p (arg
))
1572 /* If we were already here, break the infinite cycle. */
1574 *visited
= BITMAP_ALLOC (NULL
);
1575 if (!bitmap_set_bit (*visited
, SSA_NAME_VERSION (arg
)))
1579 gimple
*def_stmt
= SSA_NAME_DEF_STMT (var
);
1581 switch (gimple_code (def_stmt
))
1584 /* The RHS of the statement defining VAR must either have a
1585 constant length or come from another SSA_NAME with a constant
1587 if (gimple_assign_single_p (def_stmt
)
1588 || gimple_assign_unary_nop_p (def_stmt
))
1590 tree rhs
= gimple_assign_rhs1 (def_stmt
);
1591 return get_range_strlen (rhs
, visited
, rkind
, pdata
, eltsize
);
1593 else if (gimple_assign_rhs_code (def_stmt
) == COND_EXPR
)
1595 tree ops
[2] = { gimple_assign_rhs2 (def_stmt
),
1596 gimple_assign_rhs3 (def_stmt
) };
1598 for (unsigned int i
= 0; i
< 2; i
++)
1599 if (!get_range_strlen (ops
[i
], visited
, rkind
, pdata
, eltsize
))
1601 if (rkind
!= SRK_LENRANGE
)
1603 /* Set the upper bound to the maximum to prevent
1604 it from being adjusted in the next iteration but
1605 leave MINLEN and the more conservative MAXBOUND
1606 determined so far alone (or leave them null if
1607 they haven't been set yet). That the MINLEN is
1608 in fact zero can be determined from MAXLEN being
1609 unbounded but the discovered minimum is used for
1611 pdata
->maxlen
= build_all_ones_cst (size_type_node
);
1618 /* Unless RKIND == SRK_LENRANGE, all arguments of the PHI node
1619 must have a constant length. */
1620 for (unsigned i
= 0; i
< gimple_phi_num_args (def_stmt
); i
++)
1622 tree arg
= gimple_phi_arg (def_stmt
, i
)->def
;
1624 /* If this PHI has itself as an argument, we cannot
1625 determine the string length of this argument. However,
1626 if we can find a constant string length for the other
1627 PHI args then we can still be sure that this is a
1628 constant string length. So be optimistic and just
1629 continue with the next argument. */
1630 if (arg
== gimple_phi_result (def_stmt
))
1633 if (!get_range_strlen (arg
, visited
, rkind
, pdata
, eltsize
))
1635 if (rkind
!= SRK_LENRANGE
)
1637 /* Set the upper bound to the maximum to prevent
1638 it from being adjusted in the next iteration but
1639 leave MINLEN and the more conservative MAXBOUND
1640 determined so far alone (or leave them null if
1641 they haven't been set yet). That the MINLEN is
1642 in fact zero can be determined from MAXLEN being
1643 unbounded but the discovered minimum is used for
1645 pdata
->maxlen
= build_all_ones_cst (size_type_node
);
1655 /* Try to obtain the range of the lengths of the string(s) referenced
1656 by ARG, or the size of the largest array ARG refers to if the range
1657 of lengths cannot be determined, and store all in *PDATA which must
1658 be zero-initialized on input except PDATA->MAXBOUND may be set to
1659 a non-null tree node other than INTEGER_CST to request to have it
1660 set to the length of the longest string in a PHI. ELTSIZE is
1661 the expected size of the string element in bytes: 1 for char and
1662 some power of 2 for wide characters.
1663 Return true if the range [PDATA->MINLEN, PDATA->MAXLEN] is suitable
1664 for optimization. Returning false means that a nonzero PDATA->MINLEN
1665 doesn't reflect the true lower bound of the range when PDATA->MAXLEN
1666 is -1 (in that case, the actual range is indeterminate, i.e.,
1667 [0, PTRDIFF_MAX - 2]. */
1670 get_range_strlen (tree arg
, c_strlen_data
*pdata
, unsigned eltsize
)
1672 bitmap visited
= NULL
;
1673 tree maxbound
= pdata
->maxbound
;
1675 if (!get_range_strlen (arg
, &visited
, SRK_LENRANGE
, pdata
, eltsize
))
1677 /* On failure extend the length range to an impossible maximum
1678 (a valid MAXLEN must be less than PTRDIFF_MAX - 1). Other
1679 members can stay unchanged regardless. */
1680 pdata
->minlen
= ssize_int (0);
1681 pdata
->maxlen
= build_all_ones_cst (size_type_node
);
1683 else if (!pdata
->minlen
)
1684 pdata
->minlen
= ssize_int (0);
1686 /* If it's unchanged from it initial non-null value, set the conservative
1687 MAXBOUND to SIZE_MAX. Otherwise leave it null (if it is null). */
1688 if (maxbound
&& pdata
->maxbound
== maxbound
)
1689 pdata
->maxbound
= build_all_ones_cst (size_type_node
);
1692 BITMAP_FREE (visited
);
1694 return !integer_all_onesp (pdata
->maxlen
);
1697 /* Return the maximum value for ARG given RKIND (see strlen_range_kind).
1698 For ARG of pointer types, NONSTR indicates if the caller is prepared
1699 to handle unterminated strings. For integer ARG and when RKIND ==
1700 SRK_INT_VALUE, NONSTR must be null.
1702 If an unterminated array is discovered and our caller handles
1703 unterminated arrays, then bubble up the offending DECL and
1704 return the maximum size. Otherwise return NULL. */
1707 get_maxval_strlen (tree arg
, strlen_range_kind rkind
, tree
*nonstr
= NULL
)
1709 /* A non-null NONSTR is meaningless when determining the maximum
1710 value of an integer ARG. */
1711 gcc_assert (rkind
!= SRK_INT_VALUE
|| nonstr
== NULL
);
1712 /* ARG must have an integral type when RKIND says so. */
1713 gcc_assert (rkind
!= SRK_INT_VALUE
|| INTEGRAL_TYPE_P (TREE_TYPE (arg
)));
1715 bitmap visited
= NULL
;
1717 /* Reset DATA.MAXLEN if the call fails or when DATA.MAXLEN
1719 c_strlen_data lendata
= { };
1720 if (!get_range_strlen (arg
, &visited
, rkind
, &lendata
, /* eltsize = */1))
1721 lendata
.maxlen
= NULL_TREE
;
1722 else if (lendata
.maxlen
&& integer_all_onesp (lendata
.maxlen
))
1723 lendata
.maxlen
= NULL_TREE
;
1726 BITMAP_FREE (visited
);
1730 /* For callers prepared to handle unterminated arrays set
1731 *NONSTR to point to the declaration of the array and return
1732 the maximum length/size. */
1733 *nonstr
= lendata
.decl
;
1734 return lendata
.maxlen
;
1737 /* Fail if the constant array isn't nul-terminated. */
1738 return lendata
.decl
? NULL_TREE
: lendata
.maxlen
;
1742 /* Fold function call to builtin strcpy with arguments DEST and SRC.
1743 If LEN is not NULL, it represents the length of the string to be
1744 copied. Return NULL_TREE if no simplification can be made. */
1747 gimple_fold_builtin_strcpy (gimple_stmt_iterator
*gsi
,
1748 tree dest
, tree src
)
1750 gimple
*stmt
= gsi_stmt (*gsi
);
1751 location_t loc
= gimple_location (stmt
);
1754 /* If SRC and DEST are the same (and not volatile), return DEST. */
1755 if (operand_equal_p (src
, dest
, 0))
1757 /* Issue -Wrestrict unless the pointers are null (those do
1758 not point to objects and so do not indicate an overlap;
1759 such calls could be the result of sanitization and jump
1761 if (!integer_zerop (dest
) && !gimple_no_warning_p (stmt
))
1763 tree func
= gimple_call_fndecl (stmt
);
1765 warning_at (loc
, OPT_Wrestrict
,
1766 "%qD source argument is the same as destination",
1770 replace_call_with_value (gsi
, dest
);
1774 if (optimize_function_for_size_p (cfun
))
1777 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
1781 /* Set to non-null if ARG refers to an unterminated array. */
1783 tree len
= get_maxval_strlen (src
, SRK_STRLEN
, &nonstr
);
1787 /* Avoid folding calls with unterminated arrays. */
1788 if (!gimple_no_warning_p (stmt
))
1789 warn_string_no_nul (loc
, "strcpy", src
, nonstr
);
1790 gimple_set_no_warning (stmt
, true);
1797 len
= fold_convert_loc (loc
, size_type_node
, len
);
1798 len
= size_binop_loc (loc
, PLUS_EXPR
, len
, build_int_cst (size_type_node
, 1));
1799 len
= force_gimple_operand_gsi (gsi
, len
, true,
1800 NULL_TREE
, true, GSI_SAME_STMT
);
1801 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1802 replace_call_with_call_and_fold (gsi
, repl
);
1806 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
1807 If SLEN is not NULL, it represents the length of the source string.
1808 Return NULL_TREE if no simplification can be made. */
1811 gimple_fold_builtin_strncpy (gimple_stmt_iterator
*gsi
,
1812 tree dest
, tree src
, tree len
)
1814 gimple
*stmt
= gsi_stmt (*gsi
);
1815 location_t loc
= gimple_location (stmt
);
1816 bool nonstring
= get_attr_nonstring_decl (dest
) != NULL_TREE
;
1818 /* If the LEN parameter is zero, return DEST. */
1819 if (integer_zerop (len
))
1821 /* Avoid warning if the destination refers to a an array/pointer
1822 decorate with attribute nonstring. */
1825 tree fndecl
= gimple_call_fndecl (stmt
);
1827 /* Warn about the lack of nul termination: the result is not
1828 a (nul-terminated) string. */
1829 tree slen
= get_maxval_strlen (src
, SRK_STRLEN
);
1830 if (slen
&& !integer_zerop (slen
))
1831 warning_at (loc
, OPT_Wstringop_truncation
,
1832 "%G%qD destination unchanged after copying no bytes "
1833 "from a string of length %E",
1834 stmt
, fndecl
, slen
);
1836 warning_at (loc
, OPT_Wstringop_truncation
,
1837 "%G%qD destination unchanged after copying no bytes",
1841 replace_call_with_value (gsi
, dest
);
1845 /* We can't compare slen with len as constants below if len is not a
1847 if (TREE_CODE (len
) != INTEGER_CST
)
1850 /* Now, we must be passed a constant src ptr parameter. */
1851 tree slen
= get_maxval_strlen (src
, SRK_STRLEN
);
1852 if (!slen
|| TREE_CODE (slen
) != INTEGER_CST
)
1855 /* The size of the source string including the terminating nul. */
1856 tree ssize
= size_binop_loc (loc
, PLUS_EXPR
, slen
, ssize_int (1));
1858 /* We do not support simplification of this case, though we do
1859 support it when expanding trees into RTL. */
1860 /* FIXME: generate a call to __builtin_memset. */
1861 if (tree_int_cst_lt (ssize
, len
))
1864 /* Diagnose truncation that leaves the copy unterminated. */
1865 maybe_diag_stxncpy_trunc (*gsi
, src
, len
);
1867 /* OK transform into builtin memcpy. */
1868 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
1872 len
= fold_convert_loc (loc
, size_type_node
, len
);
1873 len
= force_gimple_operand_gsi (gsi
, len
, true,
1874 NULL_TREE
, true, GSI_SAME_STMT
);
1875 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1876 replace_call_with_call_and_fold (gsi
, repl
);
1881 /* Fold function call to builtin strchr or strrchr.
1882 If both arguments are constant, evaluate and fold the result,
1883 otherwise simplify str(r)chr (str, 0) into str + strlen (str).
1884 In general strlen is significantly faster than strchr
1885 due to being a simpler operation. */
1887 gimple_fold_builtin_strchr (gimple_stmt_iterator
*gsi
, bool is_strrchr
)
1889 gimple
*stmt
= gsi_stmt (*gsi
);
1890 tree str
= gimple_call_arg (stmt
, 0);
1891 tree c
= gimple_call_arg (stmt
, 1);
1892 location_t loc
= gimple_location (stmt
);
1896 if (!gimple_call_lhs (stmt
))
1899 /* Avoid folding if the first argument is not a nul-terminated array.
1900 Defer warning until later. */
1901 if (!check_nul_terminated_array (NULL_TREE
, str
))
1904 if ((p
= c_getstr (str
)) && target_char_cst_p (c
, &ch
))
1906 const char *p1
= is_strrchr
? strrchr (p
, ch
) : strchr (p
, ch
);
1910 replace_call_with_value (gsi
, integer_zero_node
);
1914 tree len
= build_int_cst (size_type_node
, p1
- p
);
1915 gimple_seq stmts
= NULL
;
1916 gimple
*new_stmt
= gimple_build_assign (gimple_call_lhs (stmt
),
1917 POINTER_PLUS_EXPR
, str
, len
);
1918 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
1919 gsi_replace_with_seq_vops (gsi
, stmts
);
1923 if (!integer_zerop (c
))
1926 /* Transform strrchr (s, 0) to strchr (s, 0) when optimizing for size. */
1927 if (is_strrchr
&& optimize_function_for_size_p (cfun
))
1929 tree strchr_fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
1933 gimple
*repl
= gimple_build_call (strchr_fn
, 2, str
, c
);
1934 replace_call_with_call_and_fold (gsi
, repl
);
1942 tree strlen_fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
1947 /* Create newstr = strlen (str). */
1948 gimple_seq stmts
= NULL
;
1949 gimple
*new_stmt
= gimple_build_call (strlen_fn
, 1, str
);
1950 gimple_set_location (new_stmt
, loc
);
1951 len
= create_tmp_reg_or_ssa_name (size_type_node
);
1952 gimple_call_set_lhs (new_stmt
, len
);
1953 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
1955 /* Create (str p+ strlen (str)). */
1956 new_stmt
= gimple_build_assign (gimple_call_lhs (stmt
),
1957 POINTER_PLUS_EXPR
, str
, len
);
1958 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
1959 gsi_replace_with_seq_vops (gsi
, stmts
);
1960 /* gsi now points at the assignment to the lhs, get a
1961 stmt iterator to the strlen.
1962 ??? We can't use gsi_for_stmt as that doesn't work when the
1963 CFG isn't built yet. */
1964 gimple_stmt_iterator gsi2
= *gsi
;
1970 /* Fold function call to builtin strstr.
1971 If both arguments are constant, evaluate and fold the result,
1972 additionally fold strstr (x, "") into x and strstr (x, "c")
1973 into strchr (x, 'c'). */
1975 gimple_fold_builtin_strstr (gimple_stmt_iterator
*gsi
)
1977 gimple
*stmt
= gsi_stmt (*gsi
);
1978 if (!gimple_call_lhs (stmt
))
1981 tree haystack
= gimple_call_arg (stmt
, 0);
1982 tree needle
= gimple_call_arg (stmt
, 1);
1984 /* Avoid folding if either argument is not a nul-terminated array.
1985 Defer warning until later. */
1986 if (!check_nul_terminated_array (NULL_TREE
, haystack
)
1987 || !check_nul_terminated_array (NULL_TREE
, needle
))
1990 const char *q
= c_getstr (needle
);
1994 if (const char *p
= c_getstr (haystack
))
1996 const char *r
= strstr (p
, q
);
2000 replace_call_with_value (gsi
, integer_zero_node
);
2004 tree len
= build_int_cst (size_type_node
, r
- p
);
2005 gimple_seq stmts
= NULL
;
2007 = gimple_build_assign (gimple_call_lhs (stmt
), POINTER_PLUS_EXPR
,
2009 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
2010 gsi_replace_with_seq_vops (gsi
, stmts
);
2014 /* For strstr (x, "") return x. */
2017 replace_call_with_value (gsi
, haystack
);
2021 /* Transform strstr (x, "c") into strchr (x, 'c'). */
2024 tree strchr_fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
2027 tree c
= build_int_cst (integer_type_node
, q
[0]);
2028 gimple
*repl
= gimple_build_call (strchr_fn
, 2, haystack
, c
);
2029 replace_call_with_call_and_fold (gsi
, repl
);
2037 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
2040 Return NULL_TREE if no simplification was possible, otherwise return the
2041 simplified form of the call as a tree.
2043 The simplified form may be a constant or other expression which
2044 computes the same value, but in a more efficient manner (including
2045 calls to other builtin functions).
2047 The call may contain arguments which need to be evaluated, but
2048 which are not useful to determine the result of the call. In
2049 this case we return a chain of COMPOUND_EXPRs. The LHS of each
2050 COMPOUND_EXPR will be an argument which must be evaluated.
2051 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
2052 COMPOUND_EXPR in the chain will contain the tree for the simplified
2053 form of the builtin function call. */
2056 gimple_fold_builtin_strcat (gimple_stmt_iterator
*gsi
, tree dst
, tree src
)
2058 gimple
*stmt
= gsi_stmt (*gsi
);
2059 location_t loc
= gimple_location (stmt
);
2061 const char *p
= c_getstr (src
);
2063 /* If the string length is zero, return the dst parameter. */
2064 if (p
&& *p
== '\0')
2066 replace_call_with_value (gsi
, dst
);
2070 if (!optimize_bb_for_speed_p (gimple_bb (stmt
)))
2073 /* See if we can store by pieces into (dst + strlen(dst)). */
2075 tree strlen_fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
2076 tree memcpy_fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
2078 if (!strlen_fn
|| !memcpy_fn
)
2081 /* If the length of the source string isn't computable don't
2082 split strcat into strlen and memcpy. */
2083 tree len
= get_maxval_strlen (src
, SRK_STRLEN
);
2087 /* Create strlen (dst). */
2088 gimple_seq stmts
= NULL
, stmts2
;
2089 gimple
*repl
= gimple_build_call (strlen_fn
, 1, dst
);
2090 gimple_set_location (repl
, loc
);
2091 newdst
= create_tmp_reg_or_ssa_name (size_type_node
);
2092 gimple_call_set_lhs (repl
, newdst
);
2093 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2095 /* Create (dst p+ strlen (dst)). */
2096 newdst
= fold_build_pointer_plus_loc (loc
, dst
, newdst
);
2097 newdst
= force_gimple_operand (newdst
, &stmts2
, true, NULL_TREE
);
2098 gimple_seq_add_seq_without_update (&stmts
, stmts2
);
2100 len
= fold_convert_loc (loc
, size_type_node
, len
);
2101 len
= size_binop_loc (loc
, PLUS_EXPR
, len
,
2102 build_int_cst (size_type_node
, 1));
2103 len
= force_gimple_operand (len
, &stmts2
, true, NULL_TREE
);
2104 gimple_seq_add_seq_without_update (&stmts
, stmts2
);
2106 repl
= gimple_build_call (memcpy_fn
, 3, newdst
, src
, len
);
2107 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2108 if (gimple_call_lhs (stmt
))
2110 repl
= gimple_build_assign (gimple_call_lhs (stmt
), dst
);
2111 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2112 gsi_replace_with_seq_vops (gsi
, stmts
);
2113 /* gsi now points at the assignment to the lhs, get a
2114 stmt iterator to the memcpy call.
2115 ??? We can't use gsi_for_stmt as that doesn't work when the
2116 CFG isn't built yet. */
2117 gimple_stmt_iterator gsi2
= *gsi
;
2123 gsi_replace_with_seq_vops (gsi
, stmts
);
2129 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
2130 are the arguments to the call. */
2133 gimple_fold_builtin_strcat_chk (gimple_stmt_iterator
*gsi
)
2135 gimple
*stmt
= gsi_stmt (*gsi
);
2136 tree dest
= gimple_call_arg (stmt
, 0);
2137 tree src
= gimple_call_arg (stmt
, 1);
2138 tree size
= gimple_call_arg (stmt
, 2);
2144 /* If the SRC parameter is "", return DEST. */
2145 if (p
&& *p
== '\0')
2147 replace_call_with_value (gsi
, dest
);
2151 if (! tree_fits_uhwi_p (size
) || ! integer_all_onesp (size
))
2154 /* If __builtin_strcat_chk is used, assume strcat is available. */
2155 fn
= builtin_decl_explicit (BUILT_IN_STRCAT
);
2159 gimple
*repl
= gimple_build_call (fn
, 2, dest
, src
);
2160 replace_call_with_call_and_fold (gsi
, repl
);
2164 /* Simplify a call to the strncat builtin. */
2167 gimple_fold_builtin_strncat (gimple_stmt_iterator
*gsi
)
2169 gimple
*stmt
= gsi_stmt (*gsi
);
2170 tree dst
= gimple_call_arg (stmt
, 0);
2171 tree src
= gimple_call_arg (stmt
, 1);
2172 tree len
= gimple_call_arg (stmt
, 2);
2174 const char *p
= c_getstr (src
);
2176 /* If the requested length is zero, or the src parameter string
2177 length is zero, return the dst parameter. */
2178 if (integer_zerop (len
) || (p
&& *p
== '\0'))
2180 replace_call_with_value (gsi
, dst
);
2184 if (TREE_CODE (len
) != INTEGER_CST
|| !p
)
2187 unsigned srclen
= strlen (p
);
2189 int cmpsrc
= compare_tree_int (len
, srclen
);
2191 /* Return early if the requested len is less than the string length.
2192 Warnings will be issued elsewhere later. */
2196 unsigned HOST_WIDE_INT dstsize
;
2198 bool nowarn
= gimple_no_warning_p (stmt
);
2200 if (!nowarn
&& compute_builtin_object_size (dst
, 1, &dstsize
))
2202 int cmpdst
= compare_tree_int (len
, dstsize
);
2206 tree fndecl
= gimple_call_fndecl (stmt
);
2208 /* Strncat copies (at most) LEN bytes and always appends
2209 the terminating NUL so the specified bound should never
2210 be equal to (or greater than) the size of the destination.
2211 If it is, the copy could overflow. */
2212 location_t loc
= gimple_location (stmt
);
2213 nowarn
= warning_at (loc
, OPT_Wstringop_overflow_
,
2215 ? G_("%G%qD specified bound %E equals "
2217 : G_("%G%qD specified bound %E exceeds "
2218 "destination size %wu"),
2219 stmt
, fndecl
, len
, dstsize
);
2221 gimple_set_no_warning (stmt
, true);
2225 if (!nowarn
&& cmpsrc
== 0)
2227 tree fndecl
= gimple_call_fndecl (stmt
);
2228 location_t loc
= gimple_location (stmt
);
2230 /* To avoid possible overflow the specified bound should also
2231 not be equal to the length of the source, even when the size
2232 of the destination is unknown (it's not an uncommon mistake
2233 to specify as the bound to strncpy the length of the source). */
2234 if (warning_at (loc
, OPT_Wstringop_overflow_
,
2235 "%G%qD specified bound %E equals source length",
2237 gimple_set_no_warning (stmt
, true);
2240 tree fn
= builtin_decl_implicit (BUILT_IN_STRCAT
);
2242 /* If the replacement _DECL isn't initialized, don't do the
2247 /* Otherwise, emit a call to strcat. */
2248 gcall
*repl
= gimple_build_call (fn
, 2, dst
, src
);
2249 replace_call_with_call_and_fold (gsi
, repl
);
2253 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
2257 gimple_fold_builtin_strncat_chk (gimple_stmt_iterator
*gsi
)
2259 gimple
*stmt
= gsi_stmt (*gsi
);
2260 tree dest
= gimple_call_arg (stmt
, 0);
2261 tree src
= gimple_call_arg (stmt
, 1);
2262 tree len
= gimple_call_arg (stmt
, 2);
2263 tree size
= gimple_call_arg (stmt
, 3);
2268 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
2269 if ((p
&& *p
== '\0')
2270 || integer_zerop (len
))
2272 replace_call_with_value (gsi
, dest
);
2276 if (! tree_fits_uhwi_p (size
))
2279 if (! integer_all_onesp (size
))
2281 tree src_len
= c_strlen (src
, 1);
2283 && tree_fits_uhwi_p (src_len
)
2284 && tree_fits_uhwi_p (len
)
2285 && ! tree_int_cst_lt (len
, src_len
))
2287 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
2288 fn
= builtin_decl_explicit (BUILT_IN_STRCAT_CHK
);
2292 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, size
);
2293 replace_call_with_call_and_fold (gsi
, repl
);
2299 /* If __builtin_strncat_chk is used, assume strncat is available. */
2300 fn
= builtin_decl_explicit (BUILT_IN_STRNCAT
);
2304 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
2305 replace_call_with_call_and_fold (gsi
, repl
);
2309 /* Build and append gimple statements to STMTS that would load a first
2310 character of a memory location identified by STR. LOC is location
2311 of the statement. */
2314 gimple_load_first_char (location_t loc
, tree str
, gimple_seq
*stmts
)
2318 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
2319 tree cst_uchar_ptr_node
2320 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
2321 tree off0
= build_int_cst (cst_uchar_ptr_node
, 0);
2323 tree temp
= fold_build2_loc (loc
, MEM_REF
, cst_uchar_node
, str
, off0
);
2324 gassign
*stmt
= gimple_build_assign (NULL_TREE
, temp
);
2325 var
= create_tmp_reg_or_ssa_name (cst_uchar_node
, stmt
);
2327 gimple_assign_set_lhs (stmt
, var
);
2328 gimple_seq_add_stmt_without_update (stmts
, stmt
);
2333 /* Fold a call to the str{n}{case}cmp builtin pointed by GSI iterator. */
2336 gimple_fold_builtin_string_compare (gimple_stmt_iterator
*gsi
)
2338 gimple
*stmt
= gsi_stmt (*gsi
);
2339 tree callee
= gimple_call_fndecl (stmt
);
2340 enum built_in_function fcode
= DECL_FUNCTION_CODE (callee
);
2342 tree type
= integer_type_node
;
2343 tree str1
= gimple_call_arg (stmt
, 0);
2344 tree str2
= gimple_call_arg (stmt
, 1);
2345 tree lhs
= gimple_call_lhs (stmt
);
2347 tree bound_node
= NULL_TREE
;
2348 unsigned HOST_WIDE_INT bound
= HOST_WIDE_INT_M1U
;
2350 /* Handle strncmp and strncasecmp functions. */
2351 if (gimple_call_num_args (stmt
) == 3)
2353 bound_node
= gimple_call_arg (stmt
, 2);
2354 if (tree_fits_uhwi_p (bound_node
))
2355 bound
= tree_to_uhwi (bound_node
);
2358 /* If the BOUND parameter is zero, return zero. */
2361 replace_call_with_value (gsi
, integer_zero_node
);
2365 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
2366 if (operand_equal_p (str1
, str2
, 0))
2368 replace_call_with_value (gsi
, integer_zero_node
);
2372 /* Initially set to the number of characters, including the terminating
2373 nul if each array has one. LENx == strnlen (Sx, LENx) implies that
2374 the array Sx is not terminated by a nul.
2375 For nul-terminated strings then adjusted to their length so that
2376 LENx == NULPOSx holds. */
2377 unsigned HOST_WIDE_INT len1
= HOST_WIDE_INT_MAX
, len2
= len1
;
2378 const char *p1
= c_getstr (str1
, &len1
);
2379 const char *p2
= c_getstr (str2
, &len2
);
2381 /* The position of the terminating nul character if one exists, otherwise
2382 a value greater than LENx. */
2383 unsigned HOST_WIDE_INT nulpos1
= HOST_WIDE_INT_MAX
, nulpos2
= nulpos1
;
2387 size_t n
= strnlen (p1
, len1
);
2394 size_t n
= strnlen (p2
, len2
);
2399 /* For known strings, return an immediate value. */
2403 bool known_result
= false;
2407 case BUILT_IN_STRCMP
:
2408 case BUILT_IN_STRCMP_EQ
:
2409 if (len1
!= nulpos1
|| len2
!= nulpos2
)
2412 r
= strcmp (p1
, p2
);
2413 known_result
= true;
2416 case BUILT_IN_STRNCMP
:
2417 case BUILT_IN_STRNCMP_EQ
:
2419 if (bound
== HOST_WIDE_INT_M1U
)
2422 /* Reduce the bound to be no more than the length
2423 of the shorter of the two strings, or the sizes
2424 of the unterminated arrays. */
2425 unsigned HOST_WIDE_INT n
= bound
;
2427 if (len1
== nulpos1
&& len1
< n
)
2429 if (len2
== nulpos2
&& len2
< n
)
2432 if (MIN (nulpos1
, nulpos2
) + 1 < n
)
2435 r
= strncmp (p1
, p2
, n
);
2436 known_result
= true;
2439 /* Only handleable situation is where the string are equal (result 0),
2440 which is already handled by operand_equal_p case. */
2441 case BUILT_IN_STRCASECMP
:
2443 case BUILT_IN_STRNCASECMP
:
2445 if (bound
== HOST_WIDE_INT_M1U
)
2447 r
= strncmp (p1
, p2
, bound
);
2449 known_result
= true;
2458 replace_call_with_value (gsi
, build_cmp_result (type
, r
));
2463 bool nonzero_bound
= (bound
>= 1 && bound
< HOST_WIDE_INT_M1U
)
2464 || fcode
== BUILT_IN_STRCMP
2465 || fcode
== BUILT_IN_STRCMP_EQ
2466 || fcode
== BUILT_IN_STRCASECMP
;
2468 location_t loc
= gimple_location (stmt
);
2470 /* If the second arg is "", return *(const unsigned char*)arg1. */
2471 if (p2
&& *p2
== '\0' && nonzero_bound
)
2473 gimple_seq stmts
= NULL
;
2474 tree var
= gimple_load_first_char (loc
, str1
, &stmts
);
2477 stmt
= gimple_build_assign (lhs
, NOP_EXPR
, var
);
2478 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2481 gsi_replace_with_seq_vops (gsi
, stmts
);
2485 /* If the first arg is "", return -*(const unsigned char*)arg2. */
2486 if (p1
&& *p1
== '\0' && nonzero_bound
)
2488 gimple_seq stmts
= NULL
;
2489 tree var
= gimple_load_first_char (loc
, str2
, &stmts
);
2493 tree c
= create_tmp_reg_or_ssa_name (integer_type_node
);
2494 stmt
= gimple_build_assign (c
, NOP_EXPR
, var
);
2495 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2497 stmt
= gimple_build_assign (lhs
, NEGATE_EXPR
, c
);
2498 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2501 gsi_replace_with_seq_vops (gsi
, stmts
);
2505 /* If BOUND is one, return an expression corresponding to
2506 (*(const unsigned char*)arg2 - *(const unsigned char*)arg1). */
2507 if (fcode
== BUILT_IN_STRNCMP
&& bound
== 1)
2509 gimple_seq stmts
= NULL
;
2510 tree temp1
= gimple_load_first_char (loc
, str1
, &stmts
);
2511 tree temp2
= gimple_load_first_char (loc
, str2
, &stmts
);
2515 tree c1
= create_tmp_reg_or_ssa_name (integer_type_node
);
2516 gassign
*convert1
= gimple_build_assign (c1
, NOP_EXPR
, temp1
);
2517 gimple_seq_add_stmt_without_update (&stmts
, convert1
);
2519 tree c2
= create_tmp_reg_or_ssa_name (integer_type_node
);
2520 gassign
*convert2
= gimple_build_assign (c2
, NOP_EXPR
, temp2
);
2521 gimple_seq_add_stmt_without_update (&stmts
, convert2
);
2523 stmt
= gimple_build_assign (lhs
, MINUS_EXPR
, c1
, c2
);
2524 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2527 gsi_replace_with_seq_vops (gsi
, stmts
);
2531 /* If BOUND is greater than the length of one constant string,
2532 and the other argument is also a nul-terminated string, replace
2533 strncmp with strcmp. */
2534 if (fcode
== BUILT_IN_STRNCMP
2535 && bound
> 0 && bound
< HOST_WIDE_INT_M1U
2536 && ((p2
&& len2
< bound
&& len2
== nulpos2
)
2537 || (p1
&& len1
< bound
&& len1
== nulpos1
)))
2539 tree fn
= builtin_decl_implicit (BUILT_IN_STRCMP
);
2542 gimple
*repl
= gimple_build_call (fn
, 2, str1
, str2
);
2543 replace_call_with_call_and_fold (gsi
, repl
);
2550 /* Fold a call to the memchr pointed by GSI iterator. */
2553 gimple_fold_builtin_memchr (gimple_stmt_iterator
*gsi
)
2555 gimple
*stmt
= gsi_stmt (*gsi
);
2556 tree lhs
= gimple_call_lhs (stmt
);
2557 tree arg1
= gimple_call_arg (stmt
, 0);
2558 tree arg2
= gimple_call_arg (stmt
, 1);
2559 tree len
= gimple_call_arg (stmt
, 2);
2561 /* If the LEN parameter is zero, return zero. */
2562 if (integer_zerop (len
))
2564 replace_call_with_value (gsi
, build_int_cst (ptr_type_node
, 0));
2569 if (TREE_CODE (arg2
) != INTEGER_CST
2570 || !tree_fits_uhwi_p (len
)
2571 || !target_char_cst_p (arg2
, &c
))
2574 unsigned HOST_WIDE_INT length
= tree_to_uhwi (len
);
2575 unsigned HOST_WIDE_INT string_length
;
2576 const char *p1
= c_getstr (arg1
, &string_length
);
2580 const char *r
= (const char *)memchr (p1
, c
, MIN (length
, string_length
));
2583 tree mem_size
, offset_node
;
2584 string_constant (arg1
, &offset_node
, &mem_size
, NULL
);
2585 unsigned HOST_WIDE_INT offset
= (offset_node
== NULL_TREE
)
2586 ? 0 : tree_to_uhwi (offset_node
);
2587 /* MEM_SIZE is the size of the array the string literal
2589 unsigned HOST_WIDE_INT string_size
= tree_to_uhwi (mem_size
) - offset
;
2590 gcc_checking_assert (string_length
<= string_size
);
2591 if (length
<= string_size
)
2593 replace_call_with_value (gsi
, build_int_cst (ptr_type_node
, 0));
2599 unsigned HOST_WIDE_INT offset
= r
- p1
;
2600 gimple_seq stmts
= NULL
;
2601 if (lhs
!= NULL_TREE
)
2603 tree offset_cst
= build_int_cst (TREE_TYPE (len
), offset
);
2604 gassign
*stmt
= gimple_build_assign (lhs
, POINTER_PLUS_EXPR
,
2606 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2609 gimple_seq_add_stmt_without_update (&stmts
,
2610 gimple_build_nop ());
2612 gsi_replace_with_seq_vops (gsi
, stmts
);
2620 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
2621 to the call. IGNORE is true if the value returned
2622 by the builtin will be ignored. UNLOCKED is true is true if this
2623 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
2624 the known length of the string. Return NULL_TREE if no simplification
2628 gimple_fold_builtin_fputs (gimple_stmt_iterator
*gsi
,
2629 tree arg0
, tree arg1
,
2632 gimple
*stmt
= gsi_stmt (*gsi
);
2634 /* If we're using an unlocked function, assume the other unlocked
2635 functions exist explicitly. */
2636 tree
const fn_fputc
= (unlocked
2637 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED
)
2638 : builtin_decl_implicit (BUILT_IN_FPUTC
));
2639 tree
const fn_fwrite
= (unlocked
2640 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED
)
2641 : builtin_decl_implicit (BUILT_IN_FWRITE
));
2643 /* If the return value is used, don't do the transformation. */
2644 if (gimple_call_lhs (stmt
))
2647 /* Get the length of the string passed to fputs. If the length
2648 can't be determined, punt. */
2649 tree len
= get_maxval_strlen (arg0
, SRK_STRLEN
);
2651 || TREE_CODE (len
) != INTEGER_CST
)
2654 switch (compare_tree_int (len
, 1))
2656 case -1: /* length is 0, delete the call entirely . */
2657 replace_call_with_value (gsi
, integer_zero_node
);
2660 case 0: /* length is 1, call fputc. */
2662 const char *p
= c_getstr (arg0
);
2668 gimple
*repl
= gimple_build_call (fn_fputc
, 2,
2670 (integer_type_node
, p
[0]), arg1
);
2671 replace_call_with_call_and_fold (gsi
, repl
);
2676 case 1: /* length is greater than 1, call fwrite. */
2678 /* If optimizing for size keep fputs. */
2679 if (optimize_function_for_size_p (cfun
))
2681 /* New argument list transforming fputs(string, stream) to
2682 fwrite(string, 1, len, stream). */
2686 gimple
*repl
= gimple_build_call (fn_fwrite
, 4, arg0
,
2687 size_one_node
, len
, arg1
);
2688 replace_call_with_call_and_fold (gsi
, repl
);
2697 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
2698 DEST, SRC, LEN, and SIZE are the arguments to the call.
2699 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
2700 code of the builtin. If MAXLEN is not NULL, it is maximum length
2701 passed as third argument. */
2704 gimple_fold_builtin_memory_chk (gimple_stmt_iterator
*gsi
,
2705 tree dest
, tree src
, tree len
, tree size
,
2706 enum built_in_function fcode
)
2708 gimple
*stmt
= gsi_stmt (*gsi
);
2709 location_t loc
= gimple_location (stmt
);
2710 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
2713 /* If SRC and DEST are the same (and not volatile), return DEST
2714 (resp. DEST+LEN for __mempcpy_chk). */
2715 if (fcode
!= BUILT_IN_MEMSET_CHK
&& operand_equal_p (src
, dest
, 0))
2717 if (fcode
!= BUILT_IN_MEMPCPY_CHK
)
2719 replace_call_with_value (gsi
, dest
);
2724 gimple_seq stmts
= NULL
;
2725 len
= gimple_convert_to_ptrofftype (&stmts
, loc
, len
);
2726 tree temp
= gimple_build (&stmts
, loc
, POINTER_PLUS_EXPR
,
2727 TREE_TYPE (dest
), dest
, len
);
2728 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2729 replace_call_with_value (gsi
, temp
);
2734 if (! tree_fits_uhwi_p (size
))
2737 tree maxlen
= get_maxval_strlen (len
, SRK_INT_VALUE
);
2738 if (! integer_all_onesp (size
))
2740 if (! tree_fits_uhwi_p (len
))
2742 /* If LEN is not constant, try MAXLEN too.
2743 For MAXLEN only allow optimizing into non-_ocs function
2744 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2745 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
2747 if (fcode
== BUILT_IN_MEMPCPY_CHK
&& ignore
)
2749 /* (void) __mempcpy_chk () can be optimized into
2750 (void) __memcpy_chk (). */
2751 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
2755 gimple
*repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
2756 replace_call_with_call_and_fold (gsi
, repl
);
2765 if (tree_int_cst_lt (size
, maxlen
))
2770 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
2771 mem{cpy,pcpy,move,set} is available. */
2774 case BUILT_IN_MEMCPY_CHK
:
2775 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY
);
2777 case BUILT_IN_MEMPCPY_CHK
:
2778 fn
= builtin_decl_explicit (BUILT_IN_MEMPCPY
);
2780 case BUILT_IN_MEMMOVE_CHK
:
2781 fn
= builtin_decl_explicit (BUILT_IN_MEMMOVE
);
2783 case BUILT_IN_MEMSET_CHK
:
2784 fn
= builtin_decl_explicit (BUILT_IN_MEMSET
);
2793 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
2794 replace_call_with_call_and_fold (gsi
, repl
);
2798 /* Fold a call to the __st[rp]cpy_chk builtin.
2799 DEST, SRC, and SIZE are the arguments to the call.
2800 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
2801 code of the builtin. If MAXLEN is not NULL, it is maximum length of
2802 strings passed as second argument. */
2805 gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator
*gsi
,
2807 tree src
, tree size
,
2808 enum built_in_function fcode
)
2810 gimple
*stmt
= gsi_stmt (*gsi
);
2811 location_t loc
= gimple_location (stmt
);
2812 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
2815 /* If SRC and DEST are the same (and not volatile), return DEST. */
2816 if (fcode
== BUILT_IN_STRCPY_CHK
&& operand_equal_p (src
, dest
, 0))
2818 /* Issue -Wrestrict unless the pointers are null (those do
2819 not point to objects and so do not indicate an overlap;
2820 such calls could be the result of sanitization and jump
2822 if (!integer_zerop (dest
) && !gimple_no_warning_p (stmt
))
2824 tree func
= gimple_call_fndecl (stmt
);
2826 warning_at (loc
, OPT_Wrestrict
,
2827 "%qD source argument is the same as destination",
2831 replace_call_with_value (gsi
, dest
);
2835 if (! tree_fits_uhwi_p (size
))
2838 tree maxlen
= get_maxval_strlen (src
, SRK_STRLENMAX
);
2839 if (! integer_all_onesp (size
))
2841 len
= c_strlen (src
, 1);
2842 if (! len
|| ! tree_fits_uhwi_p (len
))
2844 /* If LEN is not constant, try MAXLEN too.
2845 For MAXLEN only allow optimizing into non-_ocs function
2846 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2847 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
2849 if (fcode
== BUILT_IN_STPCPY_CHK
)
2854 /* If return value of __stpcpy_chk is ignored,
2855 optimize into __strcpy_chk. */
2856 fn
= builtin_decl_explicit (BUILT_IN_STRCPY_CHK
);
2860 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, size
);
2861 replace_call_with_call_and_fold (gsi
, repl
);
2865 if (! len
|| TREE_SIDE_EFFECTS (len
))
2868 /* If c_strlen returned something, but not a constant,
2869 transform __strcpy_chk into __memcpy_chk. */
2870 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
2874 gimple_seq stmts
= NULL
;
2875 len
= force_gimple_operand (len
, &stmts
, true, NULL_TREE
);
2876 len
= gimple_convert (&stmts
, loc
, size_type_node
, len
);
2877 len
= gimple_build (&stmts
, loc
, PLUS_EXPR
, size_type_node
, len
,
2878 build_int_cst (size_type_node
, 1));
2879 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2880 gimple
*repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
2881 replace_call_with_call_and_fold (gsi
, repl
);
2888 if (! tree_int_cst_lt (maxlen
, size
))
2892 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
2893 fn
= builtin_decl_explicit (fcode
== BUILT_IN_STPCPY_CHK
2894 ? BUILT_IN_STPCPY
: BUILT_IN_STRCPY
);
2898 gimple
*repl
= gimple_build_call (fn
, 2, dest
, src
);
2899 replace_call_with_call_and_fold (gsi
, repl
);
2903 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
2904 are the arguments to the call. If MAXLEN is not NULL, it is maximum
2905 length passed as third argument. IGNORE is true if return value can be
2906 ignored. FCODE is the BUILT_IN_* code of the builtin. */
2909 gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator
*gsi
,
2910 tree dest
, tree src
,
2911 tree len
, tree size
,
2912 enum built_in_function fcode
)
2914 gimple
*stmt
= gsi_stmt (*gsi
);
2915 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
2918 if (fcode
== BUILT_IN_STPNCPY_CHK
&& ignore
)
2920 /* If return value of __stpncpy_chk is ignored,
2921 optimize into __strncpy_chk. */
2922 fn
= builtin_decl_explicit (BUILT_IN_STRNCPY_CHK
);
2925 gimple
*repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
2926 replace_call_with_call_and_fold (gsi
, repl
);
2931 if (! tree_fits_uhwi_p (size
))
2934 tree maxlen
= get_maxval_strlen (len
, SRK_INT_VALUE
);
2935 if (! integer_all_onesp (size
))
2937 if (! tree_fits_uhwi_p (len
))
2939 /* If LEN is not constant, try MAXLEN too.
2940 For MAXLEN only allow optimizing into non-_ocs function
2941 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2942 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
2948 if (tree_int_cst_lt (size
, maxlen
))
2952 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
2953 fn
= builtin_decl_explicit (fcode
== BUILT_IN_STPNCPY_CHK
2954 ? BUILT_IN_STPNCPY
: BUILT_IN_STRNCPY
);
2958 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
2959 replace_call_with_call_and_fold (gsi
, repl
);
2963 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
2964 Return NULL_TREE if no simplification can be made. */
2967 gimple_fold_builtin_stpcpy (gimple_stmt_iterator
*gsi
)
2969 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
2970 location_t loc
= gimple_location (stmt
);
2971 tree dest
= gimple_call_arg (stmt
, 0);
2972 tree src
= gimple_call_arg (stmt
, 1);
2975 /* If the result is unused, replace stpcpy with strcpy. */
2976 if (gimple_call_lhs (stmt
) == NULL_TREE
)
2978 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
2981 gimple_call_set_fndecl (stmt
, fn
);
2986 /* Set to non-null if ARG refers to an unterminated array. */
2987 c_strlen_data data
= { };
2988 tree len
= c_strlen (src
, 1, &data
, 1);
2990 || TREE_CODE (len
) != INTEGER_CST
)
2992 data
.decl
= unterminated_array (src
);
2999 /* Avoid folding calls with unterminated arrays. */
3000 if (!gimple_no_warning_p (stmt
))
3001 warn_string_no_nul (loc
, "stpcpy", src
, data
.decl
);
3002 gimple_set_no_warning (stmt
, true);
3006 if (optimize_function_for_size_p (cfun
)
3007 /* If length is zero it's small enough. */
3008 && !integer_zerop (len
))
3011 /* If the source has a known length replace stpcpy with memcpy. */
3012 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
3016 gimple_seq stmts
= NULL
;
3017 tree tem
= gimple_convert (&stmts
, loc
, size_type_node
, len
);
3018 lenp1
= gimple_build (&stmts
, loc
, PLUS_EXPR
, size_type_node
,
3019 tem
, build_int_cst (size_type_node
, 1));
3020 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
3021 gcall
*repl
= gimple_build_call (fn
, 3, dest
, src
, lenp1
);
3022 gimple_move_vops (repl
, stmt
);
3023 gsi_insert_before (gsi
, repl
, GSI_SAME_STMT
);
3024 /* Replace the result with dest + len. */
3026 tem
= gimple_convert (&stmts
, loc
, sizetype
, len
);
3027 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
3028 gassign
*ret
= gimple_build_assign (gimple_call_lhs (stmt
),
3029 POINTER_PLUS_EXPR
, dest
, tem
);
3030 gsi_replace (gsi
, ret
, false);
3031 /* Finally fold the memcpy call. */
3032 gimple_stmt_iterator gsi2
= *gsi
;
3038 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
3039 NULL_TREE if a normal call should be emitted rather than expanding
3040 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
3041 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
3042 passed as second argument. */
3045 gimple_fold_builtin_snprintf_chk (gimple_stmt_iterator
*gsi
,
3046 enum built_in_function fcode
)
3048 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3049 tree dest
, size
, len
, fn
, fmt
, flag
;
3050 const char *fmt_str
;
3052 /* Verify the required arguments in the original call. */
3053 if (gimple_call_num_args (stmt
) < 5)
3056 dest
= gimple_call_arg (stmt
, 0);
3057 len
= gimple_call_arg (stmt
, 1);
3058 flag
= gimple_call_arg (stmt
, 2);
3059 size
= gimple_call_arg (stmt
, 3);
3060 fmt
= gimple_call_arg (stmt
, 4);
3062 if (! tree_fits_uhwi_p (size
))
3065 if (! integer_all_onesp (size
))
3067 tree maxlen
= get_maxval_strlen (len
, SRK_INT_VALUE
);
3068 if (! tree_fits_uhwi_p (len
))
3070 /* If LEN is not constant, try MAXLEN too.
3071 For MAXLEN only allow optimizing into non-_ocs function
3072 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
3073 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
3079 if (tree_int_cst_lt (size
, maxlen
))
3083 if (!init_target_chars ())
3086 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
3087 or if format doesn't contain % chars or is "%s". */
3088 if (! integer_zerop (flag
))
3090 fmt_str
= c_getstr (fmt
);
3091 if (fmt_str
== NULL
)
3093 if (strchr (fmt_str
, target_percent
) != NULL
3094 && strcmp (fmt_str
, target_percent_s
))
3098 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
3100 fn
= builtin_decl_explicit (fcode
== BUILT_IN_VSNPRINTF_CHK
3101 ? BUILT_IN_VSNPRINTF
: BUILT_IN_SNPRINTF
);
3105 /* Replace the called function and the first 5 argument by 3 retaining
3106 trailing varargs. */
3107 gimple_call_set_fndecl (stmt
, fn
);
3108 gimple_call_set_fntype (stmt
, TREE_TYPE (fn
));
3109 gimple_call_set_arg (stmt
, 0, dest
);
3110 gimple_call_set_arg (stmt
, 1, len
);
3111 gimple_call_set_arg (stmt
, 2, fmt
);
3112 for (unsigned i
= 3; i
< gimple_call_num_args (stmt
) - 2; ++i
)
3113 gimple_call_set_arg (stmt
, i
, gimple_call_arg (stmt
, i
+ 2));
3114 gimple_set_num_ops (stmt
, gimple_num_ops (stmt
) - 2);
3119 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
3120 Return NULL_TREE if a normal call should be emitted rather than
3121 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
3122 or BUILT_IN_VSPRINTF_CHK. */
3125 gimple_fold_builtin_sprintf_chk (gimple_stmt_iterator
*gsi
,
3126 enum built_in_function fcode
)
3128 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3129 tree dest
, size
, len
, fn
, fmt
, flag
;
3130 const char *fmt_str
;
3131 unsigned nargs
= gimple_call_num_args (stmt
);
3133 /* Verify the required arguments in the original call. */
3136 dest
= gimple_call_arg (stmt
, 0);
3137 flag
= gimple_call_arg (stmt
, 1);
3138 size
= gimple_call_arg (stmt
, 2);
3139 fmt
= gimple_call_arg (stmt
, 3);
3141 if (! tree_fits_uhwi_p (size
))
3146 if (!init_target_chars ())
3149 /* Check whether the format is a literal string constant. */
3150 fmt_str
= c_getstr (fmt
);
3151 if (fmt_str
!= NULL
)
3153 /* If the format doesn't contain % args or %%, we know the size. */
3154 if (strchr (fmt_str
, target_percent
) == 0)
3156 if (fcode
!= BUILT_IN_SPRINTF_CHK
|| nargs
== 4)
3157 len
= build_int_cstu (size_type_node
, strlen (fmt_str
));
3159 /* If the format is "%s" and first ... argument is a string literal,
3160 we know the size too. */
3161 else if (fcode
== BUILT_IN_SPRINTF_CHK
3162 && strcmp (fmt_str
, target_percent_s
) == 0)
3168 arg
= gimple_call_arg (stmt
, 4);
3169 if (POINTER_TYPE_P (TREE_TYPE (arg
)))
3171 len
= c_strlen (arg
, 1);
3172 if (! len
|| ! tree_fits_uhwi_p (len
))
3179 if (! integer_all_onesp (size
))
3181 if (! len
|| ! tree_int_cst_lt (len
, size
))
3185 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
3186 or if format doesn't contain % chars or is "%s". */
3187 if (! integer_zerop (flag
))
3189 if (fmt_str
== NULL
)
3191 if (strchr (fmt_str
, target_percent
) != NULL
3192 && strcmp (fmt_str
, target_percent_s
))
3196 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
3197 fn
= builtin_decl_explicit (fcode
== BUILT_IN_VSPRINTF_CHK
3198 ? BUILT_IN_VSPRINTF
: BUILT_IN_SPRINTF
);
3202 /* Replace the called function and the first 4 argument by 2 retaining
3203 trailing varargs. */
3204 gimple_call_set_fndecl (stmt
, fn
);
3205 gimple_call_set_fntype (stmt
, TREE_TYPE (fn
));
3206 gimple_call_set_arg (stmt
, 0, dest
);
3207 gimple_call_set_arg (stmt
, 1, fmt
);
3208 for (unsigned i
= 2; i
< gimple_call_num_args (stmt
) - 2; ++i
)
3209 gimple_call_set_arg (stmt
, i
, gimple_call_arg (stmt
, i
+ 2));
3210 gimple_set_num_ops (stmt
, gimple_num_ops (stmt
) - 2);
3215 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
3216 ORIG may be null if this is a 2-argument call. We don't attempt to
3217 simplify calls with more than 3 arguments.
3219 Return true if simplification was possible, otherwise false. */
3222 gimple_fold_builtin_sprintf (gimple_stmt_iterator
*gsi
)
3224 gimple
*stmt
= gsi_stmt (*gsi
);
3225 tree dest
= gimple_call_arg (stmt
, 0);
3226 tree fmt
= gimple_call_arg (stmt
, 1);
3227 tree orig
= NULL_TREE
;
3228 const char *fmt_str
= NULL
;
3230 /* Verify the required arguments in the original call. We deal with two
3231 types of sprintf() calls: 'sprintf (str, fmt)' and
3232 'sprintf (dest, "%s", orig)'. */
3233 if (gimple_call_num_args (stmt
) > 3)
3236 if (gimple_call_num_args (stmt
) == 3)
3237 orig
= gimple_call_arg (stmt
, 2);
3239 /* Check whether the format is a literal string constant. */
3240 fmt_str
= c_getstr (fmt
);
3241 if (fmt_str
== NULL
)
3244 if (!init_target_chars ())
3247 /* If the format doesn't contain % args or %%, use strcpy. */
3248 if (strchr (fmt_str
, target_percent
) == NULL
)
3250 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3255 /* Don't optimize sprintf (buf, "abc", ptr++). */
3259 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
3260 'format' is known to contain no % formats. */
3261 gimple_seq stmts
= NULL
;
3262 gimple
*repl
= gimple_build_call (fn
, 2, dest
, fmt
);
3264 /* Propagate the NO_WARNING bit to avoid issuing the same
3265 warning more than once. */
3266 if (gimple_no_warning_p (stmt
))
3267 gimple_set_no_warning (repl
, true);
3269 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3270 if (tree lhs
= gimple_call_lhs (stmt
))
3272 repl
= gimple_build_assign (lhs
, build_int_cst (TREE_TYPE (lhs
),
3274 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3275 gsi_replace_with_seq_vops (gsi
, stmts
);
3276 /* gsi now points at the assignment to the lhs, get a
3277 stmt iterator to the memcpy call.
3278 ??? We can't use gsi_for_stmt as that doesn't work when the
3279 CFG isn't built yet. */
3280 gimple_stmt_iterator gsi2
= *gsi
;
3286 gsi_replace_with_seq_vops (gsi
, stmts
);
3292 /* If the format is "%s", use strcpy if the result isn't used. */
3293 else if (fmt_str
&& strcmp (fmt_str
, target_percent_s
) == 0)
3296 fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3301 /* Don't crash on sprintf (str1, "%s"). */
3305 tree orig_len
= NULL_TREE
;
3306 if (gimple_call_lhs (stmt
))
3308 orig_len
= get_maxval_strlen (orig
, SRK_STRLEN
);
3313 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
3314 gimple_seq stmts
= NULL
;
3315 gimple
*repl
= gimple_build_call (fn
, 2, dest
, orig
);
3317 /* Propagate the NO_WARNING bit to avoid issuing the same
3318 warning more than once. */
3319 if (gimple_no_warning_p (stmt
))
3320 gimple_set_no_warning (repl
, true);
3322 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3323 if (tree lhs
= gimple_call_lhs (stmt
))
3325 if (!useless_type_conversion_p (TREE_TYPE (lhs
),
3326 TREE_TYPE (orig_len
)))
3327 orig_len
= fold_convert (TREE_TYPE (lhs
), orig_len
);
3328 repl
= gimple_build_assign (lhs
, orig_len
);
3329 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3330 gsi_replace_with_seq_vops (gsi
, stmts
);
3331 /* gsi now points at the assignment to the lhs, get a
3332 stmt iterator to the memcpy call.
3333 ??? We can't use gsi_for_stmt as that doesn't work when the
3334 CFG isn't built yet. */
3335 gimple_stmt_iterator gsi2
= *gsi
;
3341 gsi_replace_with_seq_vops (gsi
, stmts
);
3349 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
3350 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
3351 attempt to simplify calls with more than 4 arguments.
3353 Return true if simplification was possible, otherwise false. */
3356 gimple_fold_builtin_snprintf (gimple_stmt_iterator
*gsi
)
3358 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3359 tree dest
= gimple_call_arg (stmt
, 0);
3360 tree destsize
= gimple_call_arg (stmt
, 1);
3361 tree fmt
= gimple_call_arg (stmt
, 2);
3362 tree orig
= NULL_TREE
;
3363 const char *fmt_str
= NULL
;
3365 if (gimple_call_num_args (stmt
) > 4)
3368 if (gimple_call_num_args (stmt
) == 4)
3369 orig
= gimple_call_arg (stmt
, 3);
3371 if (!tree_fits_uhwi_p (destsize
))
3373 unsigned HOST_WIDE_INT destlen
= tree_to_uhwi (destsize
);
3375 /* Check whether the format is a literal string constant. */
3376 fmt_str
= c_getstr (fmt
);
3377 if (fmt_str
== NULL
)
3380 if (!init_target_chars ())
3383 /* If the format doesn't contain % args or %%, use strcpy. */
3384 if (strchr (fmt_str
, target_percent
) == NULL
)
3386 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3390 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
3394 /* We could expand this as
3395 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
3397 memcpy (str, fmt_with_nul_at_cstm1, cst);
3398 but in the former case that might increase code size
3399 and in the latter case grow .rodata section too much.
3401 size_t len
= strlen (fmt_str
);
3405 gimple_seq stmts
= NULL
;
3406 gimple
*repl
= gimple_build_call (fn
, 2, dest
, fmt
);
3407 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3408 if (tree lhs
= gimple_call_lhs (stmt
))
3410 repl
= gimple_build_assign (lhs
,
3411 build_int_cst (TREE_TYPE (lhs
), len
));
3412 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3413 gsi_replace_with_seq_vops (gsi
, stmts
);
3414 /* gsi now points at the assignment to the lhs, get a
3415 stmt iterator to the memcpy call.
3416 ??? We can't use gsi_for_stmt as that doesn't work when the
3417 CFG isn't built yet. */
3418 gimple_stmt_iterator gsi2
= *gsi
;
3424 gsi_replace_with_seq_vops (gsi
, stmts
);
3430 /* If the format is "%s", use strcpy if the result isn't used. */
3431 else if (fmt_str
&& strcmp (fmt_str
, target_percent_s
) == 0)
3433 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3437 /* Don't crash on snprintf (str1, cst, "%s"). */
3441 tree orig_len
= get_maxval_strlen (orig
, SRK_STRLEN
);
3442 if (!orig_len
|| TREE_CODE (orig_len
) != INTEGER_CST
)
3445 /* We could expand this as
3446 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
3448 memcpy (str1, str2_with_nul_at_cstm1, cst);
3449 but in the former case that might increase code size
3450 and in the latter case grow .rodata section too much.
3452 if (compare_tree_int (orig_len
, destlen
) >= 0)
3455 /* Convert snprintf (str1, cst, "%s", str2) into
3456 strcpy (str1, str2) if strlen (str2) < cst. */
3457 gimple_seq stmts
= NULL
;
3458 gimple
*repl
= gimple_build_call (fn
, 2, dest
, orig
);
3459 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3460 if (tree lhs
= gimple_call_lhs (stmt
))
3462 if (!useless_type_conversion_p (TREE_TYPE (lhs
),
3463 TREE_TYPE (orig_len
)))
3464 orig_len
= fold_convert (TREE_TYPE (lhs
), orig_len
);
3465 repl
= gimple_build_assign (lhs
, orig_len
);
3466 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3467 gsi_replace_with_seq_vops (gsi
, stmts
);
3468 /* gsi now points at the assignment to the lhs, get a
3469 stmt iterator to the memcpy call.
3470 ??? We can't use gsi_for_stmt as that doesn't work when the
3471 CFG isn't built yet. */
3472 gimple_stmt_iterator gsi2
= *gsi
;
3478 gsi_replace_with_seq_vops (gsi
, stmts
);
3486 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
3487 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
3488 more than 3 arguments, and ARG may be null in the 2-argument case.
3490 Return NULL_TREE if no simplification was possible, otherwise return the
3491 simplified form of the call as a tree. FCODE is the BUILT_IN_*
3492 code of the function to be simplified. */
3495 gimple_fold_builtin_fprintf (gimple_stmt_iterator
*gsi
,
3496 tree fp
, tree fmt
, tree arg
,
3497 enum built_in_function fcode
)
3499 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3500 tree fn_fputc
, fn_fputs
;
3501 const char *fmt_str
= NULL
;
3503 /* If the return value is used, don't do the transformation. */
3504 if (gimple_call_lhs (stmt
) != NULL_TREE
)
3507 /* Check whether the format is a literal string constant. */
3508 fmt_str
= c_getstr (fmt
);
3509 if (fmt_str
== NULL
)
3512 if (fcode
== BUILT_IN_FPRINTF_UNLOCKED
)
3514 /* If we're using an unlocked function, assume the other
3515 unlocked functions exist explicitly. */
3516 fn_fputc
= builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED
);
3517 fn_fputs
= builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED
);
3521 fn_fputc
= builtin_decl_implicit (BUILT_IN_FPUTC
);
3522 fn_fputs
= builtin_decl_implicit (BUILT_IN_FPUTS
);
3525 if (!init_target_chars ())
3528 /* If the format doesn't contain % args or %%, use strcpy. */
3529 if (strchr (fmt_str
, target_percent
) == NULL
)
3531 if (fcode
!= BUILT_IN_VFPRINTF
&& fcode
!= BUILT_IN_VFPRINTF_CHK
3535 /* If the format specifier was "", fprintf does nothing. */
3536 if (fmt_str
[0] == '\0')
3538 replace_call_with_value (gsi
, NULL_TREE
);
3542 /* When "string" doesn't contain %, replace all cases of
3543 fprintf (fp, string) with fputs (string, fp). The fputs
3544 builtin will take care of special cases like length == 1. */
3547 gcall
*repl
= gimple_build_call (fn_fputs
, 2, fmt
, fp
);
3548 replace_call_with_call_and_fold (gsi
, repl
);
3553 /* The other optimizations can be done only on the non-va_list variants. */
3554 else if (fcode
== BUILT_IN_VFPRINTF
|| fcode
== BUILT_IN_VFPRINTF_CHK
)
3557 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
3558 else if (strcmp (fmt_str
, target_percent_s
) == 0)
3560 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
3564 gcall
*repl
= gimple_build_call (fn_fputs
, 2, arg
, fp
);
3565 replace_call_with_call_and_fold (gsi
, repl
);
3570 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
3571 else if (strcmp (fmt_str
, target_percent_c
) == 0)
3574 || ! useless_type_conversion_p (integer_type_node
, TREE_TYPE (arg
)))
3578 gcall
*repl
= gimple_build_call (fn_fputc
, 2, arg
, fp
);
3579 replace_call_with_call_and_fold (gsi
, repl
);
3587 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
3588 FMT and ARG are the arguments to the call; we don't fold cases with
3589 more than 2 arguments, and ARG may be null if this is a 1-argument case.
3591 Return NULL_TREE if no simplification was possible, otherwise return the
3592 simplified form of the call as a tree. FCODE is the BUILT_IN_*
3593 code of the function to be simplified. */
3596 gimple_fold_builtin_printf (gimple_stmt_iterator
*gsi
, tree fmt
,
3597 tree arg
, enum built_in_function fcode
)
3599 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3600 tree fn_putchar
, fn_puts
, newarg
;
3601 const char *fmt_str
= NULL
;
3603 /* If the return value is used, don't do the transformation. */
3604 if (gimple_call_lhs (stmt
) != NULL_TREE
)
3607 /* Check whether the format is a literal string constant. */
3608 fmt_str
= c_getstr (fmt
);
3609 if (fmt_str
== NULL
)
3612 if (fcode
== BUILT_IN_PRINTF_UNLOCKED
)
3614 /* If we're using an unlocked function, assume the other
3615 unlocked functions exist explicitly. */
3616 fn_putchar
= builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED
);
3617 fn_puts
= builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED
);
3621 fn_putchar
= builtin_decl_implicit (BUILT_IN_PUTCHAR
);
3622 fn_puts
= builtin_decl_implicit (BUILT_IN_PUTS
);
3625 if (!init_target_chars ())
3628 if (strcmp (fmt_str
, target_percent_s
) == 0
3629 || strchr (fmt_str
, target_percent
) == NULL
)
3633 if (strcmp (fmt_str
, target_percent_s
) == 0)
3635 if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
3638 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
3641 str
= c_getstr (arg
);
3647 /* The format specifier doesn't contain any '%' characters. */
3648 if (fcode
!= BUILT_IN_VPRINTF
&& fcode
!= BUILT_IN_VPRINTF_CHK
3654 /* If the string was "", printf does nothing. */
3657 replace_call_with_value (gsi
, NULL_TREE
);
3661 /* If the string has length of 1, call putchar. */
3664 /* Given printf("c"), (where c is any one character,)
3665 convert "c"[0] to an int and pass that to the replacement
3667 newarg
= build_int_cst (integer_type_node
, str
[0]);
3670 gcall
*repl
= gimple_build_call (fn_putchar
, 1, newarg
);
3671 replace_call_with_call_and_fold (gsi
, repl
);
3677 /* If the string was "string\n", call puts("string"). */
3678 size_t len
= strlen (str
);
3679 if ((unsigned char)str
[len
- 1] == target_newline
3680 && (size_t) (int) len
== len
3685 /* Create a NUL-terminated string that's one char shorter
3686 than the original, stripping off the trailing '\n'. */
3687 newstr
= xstrdup (str
);
3688 newstr
[len
- 1] = '\0';
3689 newarg
= build_string_literal (len
, newstr
);
3693 gcall
*repl
= gimple_build_call (fn_puts
, 1, newarg
);
3694 replace_call_with_call_and_fold (gsi
, repl
);
3699 /* We'd like to arrange to call fputs(string,stdout) here,
3700 but we need stdout and don't have a way to get it yet. */
3705 /* The other optimizations can be done only on the non-va_list variants. */
3706 else if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
3709 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
3710 else if (strcmp (fmt_str
, target_percent_s_newline
) == 0)
3712 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
3716 gcall
*repl
= gimple_build_call (fn_puts
, 1, arg
);
3717 replace_call_with_call_and_fold (gsi
, repl
);
3722 /* If the format specifier was "%c", call __builtin_putchar(arg). */
3723 else if (strcmp (fmt_str
, target_percent_c
) == 0)
3725 if (!arg
|| ! useless_type_conversion_p (integer_type_node
,
3730 gcall
*repl
= gimple_build_call (fn_putchar
, 1, arg
);
3731 replace_call_with_call_and_fold (gsi
, repl
);
3741 /* Fold a call to __builtin_strlen with known length LEN. */
3744 gimple_fold_builtin_strlen (gimple_stmt_iterator
*gsi
)
3746 gimple
*stmt
= gsi_stmt (*gsi
);
3747 tree arg
= gimple_call_arg (stmt
, 0);
3752 c_strlen_data lendata
= { };
3753 if (get_range_strlen (arg
, &lendata
, /* eltsize = */ 1)
3755 && lendata
.minlen
&& TREE_CODE (lendata
.minlen
) == INTEGER_CST
3756 && lendata
.maxlen
&& TREE_CODE (lendata
.maxlen
) == INTEGER_CST
)
3758 /* The range of lengths refers to either a single constant
3759 string or to the longest and shortest constant string
3760 referenced by the argument of the strlen() call, or to
3761 the strings that can possibly be stored in the arrays
3762 the argument refers to. */
3763 minlen
= wi::to_wide (lendata
.minlen
);
3764 maxlen
= wi::to_wide (lendata
.maxlen
);
3768 unsigned prec
= TYPE_PRECISION (sizetype
);
3770 minlen
= wi::shwi (0, prec
);
3771 maxlen
= wi::to_wide (max_object_size (), prec
) - 2;
3774 if (minlen
== maxlen
)
3776 /* Fold the strlen call to a constant. */
3777 tree type
= TREE_TYPE (lendata
.minlen
);
3778 tree len
= force_gimple_operand_gsi (gsi
,
3779 wide_int_to_tree (type
, minlen
),
3780 true, NULL
, true, GSI_SAME_STMT
);
3781 replace_call_with_value (gsi
, len
);
3785 /* Set the strlen() range to [0, MAXLEN]. */
3786 if (tree lhs
= gimple_call_lhs (stmt
))
3787 set_strlen_range (lhs
, minlen
, maxlen
);
3792 /* Fold a call to __builtin_acc_on_device. */
3795 gimple_fold_builtin_acc_on_device (gimple_stmt_iterator
*gsi
, tree arg0
)
3797 /* Defer folding until we know which compiler we're in. */
3798 if (symtab
->state
!= EXPANSION
)
3801 unsigned val_host
= GOMP_DEVICE_HOST
;
3802 unsigned val_dev
= GOMP_DEVICE_NONE
;
3804 #ifdef ACCEL_COMPILER
3805 val_host
= GOMP_DEVICE_NOT_HOST
;
3806 val_dev
= ACCEL_COMPILER_acc_device
;
3809 location_t loc
= gimple_location (gsi_stmt (*gsi
));
3811 tree host_eq
= make_ssa_name (boolean_type_node
);
3812 gimple
*host_ass
= gimple_build_assign
3813 (host_eq
, EQ_EXPR
, arg0
, build_int_cst (TREE_TYPE (arg0
), val_host
));
3814 gimple_set_location (host_ass
, loc
);
3815 gsi_insert_before (gsi
, host_ass
, GSI_SAME_STMT
);
3817 tree dev_eq
= make_ssa_name (boolean_type_node
);
3818 gimple
*dev_ass
= gimple_build_assign
3819 (dev_eq
, EQ_EXPR
, arg0
, build_int_cst (TREE_TYPE (arg0
), val_dev
));
3820 gimple_set_location (dev_ass
, loc
);
3821 gsi_insert_before (gsi
, dev_ass
, GSI_SAME_STMT
);
3823 tree result
= make_ssa_name (boolean_type_node
);
3824 gimple
*result_ass
= gimple_build_assign
3825 (result
, BIT_IOR_EXPR
, host_eq
, dev_eq
);
3826 gimple_set_location (result_ass
, loc
);
3827 gsi_insert_before (gsi
, result_ass
, GSI_SAME_STMT
);
3829 replace_call_with_value (gsi
, result
);
3834 /* Fold realloc (0, n) -> malloc (n). */
3837 gimple_fold_builtin_realloc (gimple_stmt_iterator
*gsi
)
3839 gimple
*stmt
= gsi_stmt (*gsi
);
3840 tree arg
= gimple_call_arg (stmt
, 0);
3841 tree size
= gimple_call_arg (stmt
, 1);
3843 if (operand_equal_p (arg
, null_pointer_node
, 0))
3845 tree fn_malloc
= builtin_decl_implicit (BUILT_IN_MALLOC
);
3848 gcall
*repl
= gimple_build_call (fn_malloc
, 1, size
);
3849 replace_call_with_call_and_fold (gsi
, repl
);
3856 /* Fold the non-target builtin at *GSI and return whether any simplification
3860 gimple_fold_builtin (gimple_stmt_iterator
*gsi
)
3862 gcall
*stmt
= as_a
<gcall
*>(gsi_stmt (*gsi
));
3863 tree callee
= gimple_call_fndecl (stmt
);
3865 /* Give up for always_inline inline builtins until they are
3867 if (avoid_folding_inline_builtin (callee
))
3870 unsigned n
= gimple_call_num_args (stmt
);
3871 enum built_in_function fcode
= DECL_FUNCTION_CODE (callee
);
3875 return gimple_fold_builtin_bcmp (gsi
);
3876 case BUILT_IN_BCOPY
:
3877 return gimple_fold_builtin_bcopy (gsi
);
3878 case BUILT_IN_BZERO
:
3879 return gimple_fold_builtin_bzero (gsi
);
3881 case BUILT_IN_MEMSET
:
3882 return gimple_fold_builtin_memset (gsi
,
3883 gimple_call_arg (stmt
, 1),
3884 gimple_call_arg (stmt
, 2));
3885 case BUILT_IN_MEMCPY
:
3886 case BUILT_IN_MEMPCPY
:
3887 case BUILT_IN_MEMMOVE
:
3888 return gimple_fold_builtin_memory_op (gsi
, gimple_call_arg (stmt
, 0),
3889 gimple_call_arg (stmt
, 1), fcode
);
3890 case BUILT_IN_SPRINTF_CHK
:
3891 case BUILT_IN_VSPRINTF_CHK
:
3892 return gimple_fold_builtin_sprintf_chk (gsi
, fcode
);
3893 case BUILT_IN_STRCAT_CHK
:
3894 return gimple_fold_builtin_strcat_chk (gsi
);
3895 case BUILT_IN_STRNCAT_CHK
:
3896 return gimple_fold_builtin_strncat_chk (gsi
);
3897 case BUILT_IN_STRLEN
:
3898 return gimple_fold_builtin_strlen (gsi
);
3899 case BUILT_IN_STRCPY
:
3900 return gimple_fold_builtin_strcpy (gsi
,
3901 gimple_call_arg (stmt
, 0),
3902 gimple_call_arg (stmt
, 1));
3903 case BUILT_IN_STRNCPY
:
3904 return gimple_fold_builtin_strncpy (gsi
,
3905 gimple_call_arg (stmt
, 0),
3906 gimple_call_arg (stmt
, 1),
3907 gimple_call_arg (stmt
, 2));
3908 case BUILT_IN_STRCAT
:
3909 return gimple_fold_builtin_strcat (gsi
, gimple_call_arg (stmt
, 0),
3910 gimple_call_arg (stmt
, 1));
3911 case BUILT_IN_STRNCAT
:
3912 return gimple_fold_builtin_strncat (gsi
);
3913 case BUILT_IN_INDEX
:
3914 case BUILT_IN_STRCHR
:
3915 return gimple_fold_builtin_strchr (gsi
, false);
3916 case BUILT_IN_RINDEX
:
3917 case BUILT_IN_STRRCHR
:
3918 return gimple_fold_builtin_strchr (gsi
, true);
3919 case BUILT_IN_STRSTR
:
3920 return gimple_fold_builtin_strstr (gsi
);
3921 case BUILT_IN_STRCMP
:
3922 case BUILT_IN_STRCMP_EQ
:
3923 case BUILT_IN_STRCASECMP
:
3924 case BUILT_IN_STRNCMP
:
3925 case BUILT_IN_STRNCMP_EQ
:
3926 case BUILT_IN_STRNCASECMP
:
3927 return gimple_fold_builtin_string_compare (gsi
);
3928 case BUILT_IN_MEMCHR
:
3929 return gimple_fold_builtin_memchr (gsi
);
3930 case BUILT_IN_FPUTS
:
3931 return gimple_fold_builtin_fputs (gsi
, gimple_call_arg (stmt
, 0),
3932 gimple_call_arg (stmt
, 1), false);
3933 case BUILT_IN_FPUTS_UNLOCKED
:
3934 return gimple_fold_builtin_fputs (gsi
, gimple_call_arg (stmt
, 0),
3935 gimple_call_arg (stmt
, 1), true);
3936 case BUILT_IN_MEMCPY_CHK
:
3937 case BUILT_IN_MEMPCPY_CHK
:
3938 case BUILT_IN_MEMMOVE_CHK
:
3939 case BUILT_IN_MEMSET_CHK
:
3940 return gimple_fold_builtin_memory_chk (gsi
,
3941 gimple_call_arg (stmt
, 0),
3942 gimple_call_arg (stmt
, 1),
3943 gimple_call_arg (stmt
, 2),
3944 gimple_call_arg (stmt
, 3),
3946 case BUILT_IN_STPCPY
:
3947 return gimple_fold_builtin_stpcpy (gsi
);
3948 case BUILT_IN_STRCPY_CHK
:
3949 case BUILT_IN_STPCPY_CHK
:
3950 return gimple_fold_builtin_stxcpy_chk (gsi
,
3951 gimple_call_arg (stmt
, 0),
3952 gimple_call_arg (stmt
, 1),
3953 gimple_call_arg (stmt
, 2),
3955 case BUILT_IN_STRNCPY_CHK
:
3956 case BUILT_IN_STPNCPY_CHK
:
3957 return gimple_fold_builtin_stxncpy_chk (gsi
,
3958 gimple_call_arg (stmt
, 0),
3959 gimple_call_arg (stmt
, 1),
3960 gimple_call_arg (stmt
, 2),
3961 gimple_call_arg (stmt
, 3),
3963 case BUILT_IN_SNPRINTF_CHK
:
3964 case BUILT_IN_VSNPRINTF_CHK
:
3965 return gimple_fold_builtin_snprintf_chk (gsi
, fcode
);
3967 case BUILT_IN_FPRINTF
:
3968 case BUILT_IN_FPRINTF_UNLOCKED
:
3969 case BUILT_IN_VFPRINTF
:
3970 if (n
== 2 || n
== 3)
3971 return gimple_fold_builtin_fprintf (gsi
,
3972 gimple_call_arg (stmt
, 0),
3973 gimple_call_arg (stmt
, 1),
3975 ? gimple_call_arg (stmt
, 2)
3979 case BUILT_IN_FPRINTF_CHK
:
3980 case BUILT_IN_VFPRINTF_CHK
:
3981 if (n
== 3 || n
== 4)
3982 return gimple_fold_builtin_fprintf (gsi
,
3983 gimple_call_arg (stmt
, 0),
3984 gimple_call_arg (stmt
, 2),
3986 ? gimple_call_arg (stmt
, 3)
3990 case BUILT_IN_PRINTF
:
3991 case BUILT_IN_PRINTF_UNLOCKED
:
3992 case BUILT_IN_VPRINTF
:
3993 if (n
== 1 || n
== 2)
3994 return gimple_fold_builtin_printf (gsi
, gimple_call_arg (stmt
, 0),
3996 ? gimple_call_arg (stmt
, 1)
3997 : NULL_TREE
, fcode
);
3999 case BUILT_IN_PRINTF_CHK
:
4000 case BUILT_IN_VPRINTF_CHK
:
4001 if (n
== 2 || n
== 3)
4002 return gimple_fold_builtin_printf (gsi
, gimple_call_arg (stmt
, 1),
4004 ? gimple_call_arg (stmt
, 2)
4005 : NULL_TREE
, fcode
);
4007 case BUILT_IN_ACC_ON_DEVICE
:
4008 return gimple_fold_builtin_acc_on_device (gsi
,
4009 gimple_call_arg (stmt
, 0));
4010 case BUILT_IN_REALLOC
:
4011 return gimple_fold_builtin_realloc (gsi
);
4016 /* Try the generic builtin folder. */
4017 bool ignore
= (gimple_call_lhs (stmt
) == NULL
);
4018 tree result
= fold_call_stmt (stmt
, ignore
);
4022 STRIP_NOPS (result
);
4024 result
= fold_convert (gimple_call_return_type (stmt
), result
);
4025 if (!update_call_from_tree (gsi
, result
))
4026 gimplify_and_update_call_from_tree (gsi
, result
);
4033 /* Transform IFN_GOACC_DIM_SIZE and IFN_GOACC_DIM_POS internal
4034 function calls to constants, where possible. */
4037 fold_internal_goacc_dim (const gimple
*call
)
4039 int axis
= oacc_get_ifn_dim_arg (call
);
4040 int size
= oacc_get_fn_dim_size (current_function_decl
, axis
);
4041 tree result
= NULL_TREE
;
4042 tree type
= TREE_TYPE (gimple_call_lhs (call
));
4044 switch (gimple_call_internal_fn (call
))
4046 case IFN_GOACC_DIM_POS
:
4047 /* If the size is 1, we know the answer. */
4049 result
= build_int_cst (type
, 0);
4051 case IFN_GOACC_DIM_SIZE
:
4052 /* If the size is not dynamic, we know the answer. */
4054 result
= build_int_cst (type
, size
);
4063 /* Return true if stmt is __atomic_compare_exchange_N call which is suitable
4064 for conversion into ATOMIC_COMPARE_EXCHANGE if the second argument is
4065 &var where var is only addressable because of such calls. */
4068 optimize_atomic_compare_exchange_p (gimple
*stmt
)
4070 if (gimple_call_num_args (stmt
) != 6
4071 || !flag_inline_atomics
4073 || sanitize_flags_p (SANITIZE_THREAD
| SANITIZE_ADDRESS
)
4074 || !gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
)
4075 || !gimple_vdef (stmt
)
4076 || !gimple_vuse (stmt
))
4079 tree fndecl
= gimple_call_fndecl (stmt
);
4080 switch (DECL_FUNCTION_CODE (fndecl
))
4082 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
:
4083 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2
:
4084 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4
:
4085 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8
:
4086 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16
:
4092 tree expected
= gimple_call_arg (stmt
, 1);
4093 if (TREE_CODE (expected
) != ADDR_EXPR
4094 || !SSA_VAR_P (TREE_OPERAND (expected
, 0)))
4097 tree etype
= TREE_TYPE (TREE_OPERAND (expected
, 0));
4098 if (!is_gimple_reg_type (etype
)
4099 || !auto_var_in_fn_p (TREE_OPERAND (expected
, 0), current_function_decl
)
4100 || TREE_THIS_VOLATILE (etype
)
4101 || VECTOR_TYPE_P (etype
)
4102 || TREE_CODE (etype
) == COMPLEX_TYPE
4103 /* Don't optimize floating point expected vars, VIEW_CONVERT_EXPRs
4104 might not preserve all the bits. See PR71716. */
4105 || SCALAR_FLOAT_TYPE_P (etype
)
4106 || maybe_ne (TYPE_PRECISION (etype
),
4107 GET_MODE_BITSIZE (TYPE_MODE (etype
))))
4110 tree weak
= gimple_call_arg (stmt
, 3);
4111 if (!integer_zerop (weak
) && !integer_onep (weak
))
4114 tree parmt
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
4115 tree itype
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt
)));
4116 machine_mode mode
= TYPE_MODE (itype
);
4118 if (direct_optab_handler (atomic_compare_and_swap_optab
, mode
)
4120 && optab_handler (sync_compare_and_swap_optab
, mode
) == CODE_FOR_nothing
)
4123 if (maybe_ne (int_size_in_bytes (etype
), GET_MODE_SIZE (mode
)))
4130 r = __atomic_compare_exchange_N (p, &e, d, w, s, f);
4132 _Complex uintN_t t = ATOMIC_COMPARE_EXCHANGE (p, e, d, w * 256 + N, s, f);
4133 i = IMAGPART_EXPR <t>;
4135 e = REALPART_EXPR <t>; */
4138 fold_builtin_atomic_compare_exchange (gimple_stmt_iterator
*gsi
)
4140 gimple
*stmt
= gsi_stmt (*gsi
);
4141 tree fndecl
= gimple_call_fndecl (stmt
);
4142 tree parmt
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
4143 tree itype
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt
)));
4144 tree ctype
= build_complex_type (itype
);
4145 tree expected
= TREE_OPERAND (gimple_call_arg (stmt
, 1), 0);
4146 bool throws
= false;
4148 gimple
*g
= gimple_build_assign (make_ssa_name (TREE_TYPE (expected
)),
4150 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4151 gimple_stmt_iterator gsiret
= gsi_for_stmt (g
);
4152 if (!useless_type_conversion_p (itype
, TREE_TYPE (expected
)))
4154 g
= gimple_build_assign (make_ssa_name (itype
), VIEW_CONVERT_EXPR
,
4155 build1 (VIEW_CONVERT_EXPR
, itype
,
4156 gimple_assign_lhs (g
)));
4157 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4159 int flag
= (integer_onep (gimple_call_arg (stmt
, 3)) ? 256 : 0)
4160 + int_size_in_bytes (itype
);
4161 g
= gimple_build_call_internal (IFN_ATOMIC_COMPARE_EXCHANGE
, 6,
4162 gimple_call_arg (stmt
, 0),
4163 gimple_assign_lhs (g
),
4164 gimple_call_arg (stmt
, 2),
4165 build_int_cst (integer_type_node
, flag
),
4166 gimple_call_arg (stmt
, 4),
4167 gimple_call_arg (stmt
, 5));
4168 tree lhs
= make_ssa_name (ctype
);
4169 gimple_call_set_lhs (g
, lhs
);
4170 gimple_move_vops (g
, stmt
);
4171 tree oldlhs
= gimple_call_lhs (stmt
);
4172 if (stmt_can_throw_internal (cfun
, stmt
))
4175 e
= find_fallthru_edge (gsi_bb (*gsi
)->succs
);
4177 gimple_call_set_nothrow (as_a
<gcall
*> (g
),
4178 gimple_call_nothrow_p (as_a
<gcall
*> (stmt
)));
4179 gimple_call_set_lhs (stmt
, NULL_TREE
);
4180 gsi_replace (gsi
, g
, true);
4183 g
= gimple_build_assign (make_ssa_name (itype
), IMAGPART_EXPR
,
4184 build1 (IMAGPART_EXPR
, itype
, lhs
));
4187 gsi_insert_on_edge_immediate (e
, g
);
4188 *gsi
= gsi_for_stmt (g
);
4191 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
4192 g
= gimple_build_assign (oldlhs
, NOP_EXPR
, gimple_assign_lhs (g
));
4193 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
4195 g
= gimple_build_assign (make_ssa_name (itype
), REALPART_EXPR
,
4196 build1 (REALPART_EXPR
, itype
, lhs
));
4197 if (throws
&& oldlhs
== NULL_TREE
)
4199 gsi_insert_on_edge_immediate (e
, g
);
4200 *gsi
= gsi_for_stmt (g
);
4203 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
4204 if (!useless_type_conversion_p (TREE_TYPE (expected
), itype
))
4206 g
= gimple_build_assign (make_ssa_name (TREE_TYPE (expected
)),
4208 build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (expected
),
4209 gimple_assign_lhs (g
)));
4210 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
4212 g
= gimple_build_assign (expected
, SSA_NAME
, gimple_assign_lhs (g
));
4213 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
4217 /* Return true if ARG0 CODE ARG1 in infinite signed precision operation
4218 doesn't fit into TYPE. The test for overflow should be regardless of
4219 -fwrapv, and even for unsigned types. */
4222 arith_overflowed_p (enum tree_code code
, const_tree type
,
4223 const_tree arg0
, const_tree arg1
)
4225 widest2_int warg0
= widest2_int_cst (arg0
);
4226 widest2_int warg1
= widest2_int_cst (arg1
);
4230 case PLUS_EXPR
: wres
= wi::add (warg0
, warg1
); break;
4231 case MINUS_EXPR
: wres
= wi::sub (warg0
, warg1
); break;
4232 case MULT_EXPR
: wres
= wi::mul (warg0
, warg1
); break;
4233 default: gcc_unreachable ();
4235 signop sign
= TYPE_SIGN (type
);
4236 if (sign
== UNSIGNED
&& wi::neg_p (wres
))
4238 return wi::min_precision (wres
, sign
) > TYPE_PRECISION (type
);
4241 /* If IFN_MASK_LOAD/STORE call CALL is unconditional, return a MEM_REF
4242 for the memory it references, otherwise return null. VECTYPE is the
4243 type of the memory vector. */
4246 gimple_fold_mask_load_store_mem_ref (gcall
*call
, tree vectype
)
4248 tree ptr
= gimple_call_arg (call
, 0);
4249 tree alias_align
= gimple_call_arg (call
, 1);
4250 tree mask
= gimple_call_arg (call
, 2);
4251 if (!tree_fits_uhwi_p (alias_align
) || !integer_all_onesp (mask
))
4254 unsigned HOST_WIDE_INT align
= tree_to_uhwi (alias_align
) * BITS_PER_UNIT
;
4255 if (TYPE_ALIGN (vectype
) != align
)
4256 vectype
= build_aligned_type (vectype
, align
);
4257 tree offset
= build_zero_cst (TREE_TYPE (alias_align
));
4258 return fold_build2 (MEM_REF
, vectype
, ptr
, offset
);
4261 /* Try to fold IFN_MASK_LOAD call CALL. Return true on success. */
4264 gimple_fold_mask_load (gimple_stmt_iterator
*gsi
, gcall
*call
)
4266 tree lhs
= gimple_call_lhs (call
);
4270 if (tree rhs
= gimple_fold_mask_load_store_mem_ref (call
, TREE_TYPE (lhs
)))
4272 gassign
*new_stmt
= gimple_build_assign (lhs
, rhs
);
4273 gimple_set_location (new_stmt
, gimple_location (call
));
4274 gimple_move_vops (new_stmt
, call
);
4275 gsi_replace (gsi
, new_stmt
, false);
4281 /* Try to fold IFN_MASK_STORE call CALL. Return true on success. */
4284 gimple_fold_mask_store (gimple_stmt_iterator
*gsi
, gcall
*call
)
4286 tree rhs
= gimple_call_arg (call
, 3);
4287 if (tree lhs
= gimple_fold_mask_load_store_mem_ref (call
, TREE_TYPE (rhs
)))
4289 gassign
*new_stmt
= gimple_build_assign (lhs
, rhs
);
4290 gimple_set_location (new_stmt
, gimple_location (call
));
4291 gimple_move_vops (new_stmt
, call
);
4292 gsi_replace (gsi
, new_stmt
, false);
4298 /* Attempt to fold a call statement referenced by the statement iterator GSI.
4299 The statement may be replaced by another statement, e.g., if the call
4300 simplifies to a constant value. Return true if any changes were made.
4301 It is assumed that the operands have been previously folded. */
4304 gimple_fold_call (gimple_stmt_iterator
*gsi
, bool inplace
)
4306 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
4308 bool changed
= false;
4311 /* Fold *& in call arguments. */
4312 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
4313 if (REFERENCE_CLASS_P (gimple_call_arg (stmt
, i
)))
4315 tree tmp
= maybe_fold_reference (gimple_call_arg (stmt
, i
), false);
4318 gimple_call_set_arg (stmt
, i
, tmp
);
4323 /* Check for virtual calls that became direct calls. */
4324 callee
= gimple_call_fn (stmt
);
4325 if (callee
&& TREE_CODE (callee
) == OBJ_TYPE_REF
)
4327 if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee
)) != NULL_TREE
)
4329 if (dump_file
&& virtual_method_call_p (callee
)
4330 && !possible_polymorphic_call_target_p
4331 (callee
, stmt
, cgraph_node::get (gimple_call_addr_fndecl
4332 (OBJ_TYPE_REF_EXPR (callee
)))))
4335 "Type inheritance inconsistent devirtualization of ");
4336 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
4337 fprintf (dump_file
, " to ");
4338 print_generic_expr (dump_file
, callee
, TDF_SLIM
);
4339 fprintf (dump_file
, "\n");
4342 gimple_call_set_fn (stmt
, OBJ_TYPE_REF_EXPR (callee
));
4345 else if (flag_devirtualize
&& !inplace
&& virtual_method_call_p (callee
))
4348 vec
<cgraph_node
*>targets
4349 = possible_polymorphic_call_targets (callee
, stmt
, &final
);
4350 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
4352 tree lhs
= gimple_call_lhs (stmt
);
4353 if (dump_enabled_p ())
4355 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, stmt
,
4356 "folding virtual function call to %s\n",
4357 targets
.length () == 1
4358 ? targets
[0]->name ()
4359 : "__builtin_unreachable");
4361 if (targets
.length () == 1)
4363 tree fndecl
= targets
[0]->decl
;
4364 gimple_call_set_fndecl (stmt
, fndecl
);
4366 /* If changing the call to __cxa_pure_virtual
4367 or similar noreturn function, adjust gimple_call_fntype
4369 if (gimple_call_noreturn_p (stmt
)
4370 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl
)))
4371 && TYPE_ARG_TYPES (TREE_TYPE (fndecl
))
4372 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)))
4374 gimple_call_set_fntype (stmt
, TREE_TYPE (fndecl
));
4375 /* If the call becomes noreturn, remove the lhs. */
4377 && gimple_call_noreturn_p (stmt
)
4378 && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (stmt
)))
4379 || should_remove_lhs_p (lhs
)))
4381 if (TREE_CODE (lhs
) == SSA_NAME
)
4383 tree var
= create_tmp_var (TREE_TYPE (lhs
));
4384 tree def
= get_or_create_ssa_default_def (cfun
, var
);
4385 gimple
*new_stmt
= gimple_build_assign (lhs
, def
);
4386 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
4388 gimple_call_set_lhs (stmt
, NULL_TREE
);
4390 maybe_remove_unused_call_args (cfun
, stmt
);
4394 tree fndecl
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
4395 gimple
*new_stmt
= gimple_build_call (fndecl
, 0);
4396 gimple_set_location (new_stmt
, gimple_location (stmt
));
4397 /* If the call had a SSA name as lhs morph that into
4398 an uninitialized value. */
4399 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
4401 tree var
= create_tmp_var (TREE_TYPE (lhs
));
4402 SET_SSA_NAME_VAR_OR_IDENTIFIER (lhs
, var
);
4403 SSA_NAME_DEF_STMT (lhs
) = gimple_build_nop ();
4404 set_ssa_default_def (cfun
, var
, lhs
);
4406 gimple_move_vops (new_stmt
, stmt
);
4407 gsi_replace (gsi
, new_stmt
, false);
4414 /* Check for indirect calls that became direct calls, and then
4415 no longer require a static chain. */
4416 if (gimple_call_chain (stmt
))
4418 tree fn
= gimple_call_fndecl (stmt
);
4419 if (fn
&& !DECL_STATIC_CHAIN (fn
))
4421 gimple_call_set_chain (stmt
, NULL
);
4426 tree tmp
= maybe_fold_reference (gimple_call_chain (stmt
), false);
4429 gimple_call_set_chain (stmt
, tmp
);
4438 /* Check for builtins that CCP can handle using information not
4439 available in the generic fold routines. */
4440 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
4442 if (gimple_fold_builtin (gsi
))
4445 else if (gimple_call_builtin_p (stmt
, BUILT_IN_MD
))
4447 changed
|= targetm
.gimple_fold_builtin (gsi
);
4449 else if (gimple_call_internal_p (stmt
))
4451 enum tree_code subcode
= ERROR_MARK
;
4452 tree result
= NULL_TREE
;
4453 bool cplx_result
= false;
4454 tree overflow
= NULL_TREE
;
4455 switch (gimple_call_internal_fn (stmt
))
4457 case IFN_BUILTIN_EXPECT
:
4458 result
= fold_builtin_expect (gimple_location (stmt
),
4459 gimple_call_arg (stmt
, 0),
4460 gimple_call_arg (stmt
, 1),
4461 gimple_call_arg (stmt
, 2),
4464 case IFN_UBSAN_OBJECT_SIZE
:
4466 tree offset
= gimple_call_arg (stmt
, 1);
4467 tree objsize
= gimple_call_arg (stmt
, 2);
4468 if (integer_all_onesp (objsize
)
4469 || (TREE_CODE (offset
) == INTEGER_CST
4470 && TREE_CODE (objsize
) == INTEGER_CST
4471 && tree_int_cst_le (offset
, objsize
)))
4473 replace_call_with_value (gsi
, NULL_TREE
);
4479 if (integer_zerop (gimple_call_arg (stmt
, 1)))
4481 replace_call_with_value (gsi
, NULL_TREE
);
4485 case IFN_UBSAN_BOUNDS
:
4487 tree index
= gimple_call_arg (stmt
, 1);
4488 tree bound
= gimple_call_arg (stmt
, 2);
4489 if (TREE_CODE (index
) == INTEGER_CST
4490 && TREE_CODE (bound
) == INTEGER_CST
)
4492 index
= fold_convert (TREE_TYPE (bound
), index
);
4493 if (TREE_CODE (index
) == INTEGER_CST
4494 && tree_int_cst_le (index
, bound
))
4496 replace_call_with_value (gsi
, NULL_TREE
);
4502 case IFN_GOACC_DIM_SIZE
:
4503 case IFN_GOACC_DIM_POS
:
4504 result
= fold_internal_goacc_dim (stmt
);
4506 case IFN_UBSAN_CHECK_ADD
:
4507 subcode
= PLUS_EXPR
;
4509 case IFN_UBSAN_CHECK_SUB
:
4510 subcode
= MINUS_EXPR
;
4512 case IFN_UBSAN_CHECK_MUL
:
4513 subcode
= MULT_EXPR
;
4515 case IFN_ADD_OVERFLOW
:
4516 subcode
= PLUS_EXPR
;
4519 case IFN_SUB_OVERFLOW
:
4520 subcode
= MINUS_EXPR
;
4523 case IFN_MUL_OVERFLOW
:
4524 subcode
= MULT_EXPR
;
4528 changed
|= gimple_fold_mask_load (gsi
, stmt
);
4530 case IFN_MASK_STORE
:
4531 changed
|= gimple_fold_mask_store (gsi
, stmt
);
4536 if (subcode
!= ERROR_MARK
)
4538 tree arg0
= gimple_call_arg (stmt
, 0);
4539 tree arg1
= gimple_call_arg (stmt
, 1);
4540 tree type
= TREE_TYPE (arg0
);
4543 tree lhs
= gimple_call_lhs (stmt
);
4544 if (lhs
== NULL_TREE
)
4547 type
= TREE_TYPE (TREE_TYPE (lhs
));
4549 if (type
== NULL_TREE
)
4551 /* x = y + 0; x = y - 0; x = y * 0; */
4552 else if (integer_zerop (arg1
))
4553 result
= subcode
== MULT_EXPR
? integer_zero_node
: arg0
;
4554 /* x = 0 + y; x = 0 * y; */
4555 else if (subcode
!= MINUS_EXPR
&& integer_zerop (arg0
))
4556 result
= subcode
== MULT_EXPR
? integer_zero_node
: arg1
;
4558 else if (subcode
== MINUS_EXPR
&& operand_equal_p (arg0
, arg1
, 0))
4559 result
= integer_zero_node
;
4560 /* x = y * 1; x = 1 * y; */
4561 else if (subcode
== MULT_EXPR
&& integer_onep (arg1
))
4563 else if (subcode
== MULT_EXPR
&& integer_onep (arg0
))
4565 else if (TREE_CODE (arg0
) == INTEGER_CST
4566 && TREE_CODE (arg1
) == INTEGER_CST
)
4569 result
= int_const_binop (subcode
, fold_convert (type
, arg0
),
4570 fold_convert (type
, arg1
));
4572 result
= int_const_binop (subcode
, arg0
, arg1
);
4573 if (result
&& arith_overflowed_p (subcode
, type
, arg0
, arg1
))
4576 overflow
= build_one_cst (type
);
4583 if (result
== integer_zero_node
)
4584 result
= build_zero_cst (type
);
4585 else if (cplx_result
&& TREE_TYPE (result
) != type
)
4587 if (TREE_CODE (result
) == INTEGER_CST
)
4589 if (arith_overflowed_p (PLUS_EXPR
, type
, result
,
4591 overflow
= build_one_cst (type
);
4593 else if ((!TYPE_UNSIGNED (TREE_TYPE (result
))
4594 && TYPE_UNSIGNED (type
))
4595 || (TYPE_PRECISION (type
)
4596 < (TYPE_PRECISION (TREE_TYPE (result
))
4597 + (TYPE_UNSIGNED (TREE_TYPE (result
))
4598 && !TYPE_UNSIGNED (type
)))))
4601 result
= fold_convert (type
, result
);
4608 if (TREE_CODE (result
) == INTEGER_CST
&& TREE_OVERFLOW (result
))
4609 result
= drop_tree_overflow (result
);
4612 if (overflow
== NULL_TREE
)
4613 overflow
= build_zero_cst (TREE_TYPE (result
));
4614 tree ctype
= build_complex_type (TREE_TYPE (result
));
4615 if (TREE_CODE (result
) == INTEGER_CST
4616 && TREE_CODE (overflow
) == INTEGER_CST
)
4617 result
= build_complex (ctype
, result
, overflow
);
4619 result
= build2_loc (gimple_location (stmt
), COMPLEX_EXPR
,
4620 ctype
, result
, overflow
);
4622 if (!update_call_from_tree (gsi
, result
))
4623 gimplify_and_update_call_from_tree (gsi
, result
);
4632 /* Return true whether NAME has a use on STMT. */
4635 has_use_on_stmt (tree name
, gimple
*stmt
)
4637 imm_use_iterator iter
;
4638 use_operand_p use_p
;
4639 FOR_EACH_IMM_USE_FAST (use_p
, iter
, name
)
4640 if (USE_STMT (use_p
) == stmt
)
4645 /* Worker for fold_stmt_1 dispatch to pattern based folding with
4648 Replaces *GSI with the simplification result in RCODE and OPS
4649 and the associated statements in *SEQ. Does the replacement
4650 according to INPLACE and returns true if the operation succeeded. */
4653 replace_stmt_with_simplification (gimple_stmt_iterator
*gsi
,
4654 gimple_match_op
*res_op
,
4655 gimple_seq
*seq
, bool inplace
)
4657 gimple
*stmt
= gsi_stmt (*gsi
);
4658 tree
*ops
= res_op
->ops
;
4659 unsigned int num_ops
= res_op
->num_ops
;
4661 /* Play safe and do not allow abnormals to be mentioned in
4662 newly created statements. See also maybe_push_res_to_seq.
4663 As an exception allow such uses if there was a use of the
4664 same SSA name on the old stmt. */
4665 for (unsigned int i
= 0; i
< num_ops
; ++i
)
4666 if (TREE_CODE (ops
[i
]) == SSA_NAME
4667 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[i
])
4668 && !has_use_on_stmt (ops
[i
], stmt
))
4671 if (num_ops
> 0 && COMPARISON_CLASS_P (ops
[0]))
4672 for (unsigned int i
= 0; i
< 2; ++i
)
4673 if (TREE_CODE (TREE_OPERAND (ops
[0], i
)) == SSA_NAME
4674 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops
[0], i
))
4675 && !has_use_on_stmt (TREE_OPERAND (ops
[0], i
), stmt
))
4678 /* Don't insert new statements when INPLACE is true, even if we could
4679 reuse STMT for the final statement. */
4680 if (inplace
&& !gimple_seq_empty_p (*seq
))
4683 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
4685 gcc_assert (res_op
->code
.is_tree_code ());
4686 if (TREE_CODE_CLASS ((enum tree_code
) res_op
->code
) == tcc_comparison
4687 /* GIMPLE_CONDs condition may not throw. */
4688 && (!flag_exceptions
4689 || !cfun
->can_throw_non_call_exceptions
4690 || !operation_could_trap_p (res_op
->code
,
4691 FLOAT_TYPE_P (TREE_TYPE (ops
[0])),
4693 gimple_cond_set_condition (cond_stmt
, res_op
->code
, ops
[0], ops
[1]);
4694 else if (res_op
->code
== SSA_NAME
)
4695 gimple_cond_set_condition (cond_stmt
, NE_EXPR
, ops
[0],
4696 build_zero_cst (TREE_TYPE (ops
[0])));
4697 else if (res_op
->code
== INTEGER_CST
)
4699 if (integer_zerop (ops
[0]))
4700 gimple_cond_make_false (cond_stmt
);
4702 gimple_cond_make_true (cond_stmt
);
4706 tree res
= maybe_push_res_to_seq (res_op
, seq
);
4709 gimple_cond_set_condition (cond_stmt
, NE_EXPR
, res
,
4710 build_zero_cst (TREE_TYPE (res
)));
4714 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4716 fprintf (dump_file
, "gimple_simplified to ");
4717 if (!gimple_seq_empty_p (*seq
))
4718 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
4719 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
),
4722 gsi_insert_seq_before (gsi
, *seq
, GSI_SAME_STMT
);
4725 else if (is_gimple_assign (stmt
)
4726 && res_op
->code
.is_tree_code ())
4729 || gimple_num_ops (stmt
) > get_gimple_rhs_num_ops (res_op
->code
))
4731 maybe_build_generic_op (res_op
);
4732 gimple_assign_set_rhs_with_ops (gsi
, res_op
->code
,
4733 res_op
->op_or_null (0),
4734 res_op
->op_or_null (1),
4735 res_op
->op_or_null (2));
4736 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4738 fprintf (dump_file
, "gimple_simplified to ");
4739 if (!gimple_seq_empty_p (*seq
))
4740 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
4741 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
),
4744 gsi_insert_seq_before (gsi
, *seq
, GSI_SAME_STMT
);
4748 else if (res_op
->code
.is_fn_code ()
4749 && gimple_call_combined_fn (stmt
) == res_op
->code
)
4751 gcc_assert (num_ops
== gimple_call_num_args (stmt
));
4752 for (unsigned int i
= 0; i
< num_ops
; ++i
)
4753 gimple_call_set_arg (stmt
, i
, ops
[i
]);
4754 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4756 fprintf (dump_file
, "gimple_simplified to ");
4757 if (!gimple_seq_empty_p (*seq
))
4758 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
4759 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
), 0, TDF_SLIM
);
4761 gsi_insert_seq_before (gsi
, *seq
, GSI_SAME_STMT
);
4766 if (gimple_has_lhs (stmt
))
4768 tree lhs
= gimple_get_lhs (stmt
);
4769 if (!maybe_push_res_to_seq (res_op
, seq
, lhs
))
4771 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4773 fprintf (dump_file
, "gimple_simplified to ");
4774 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
4776 gsi_replace_with_seq_vops (gsi
, *seq
);
4786 /* Canonicalize MEM_REFs invariant address operand after propagation. */
4789 maybe_canonicalize_mem_ref_addr (tree
*t
)
4793 if (TREE_CODE (*t
) == ADDR_EXPR
)
4794 t
= &TREE_OPERAND (*t
, 0);
4796 /* The C and C++ frontends use an ARRAY_REF for indexing with their
4797 generic vector extension. The actual vector referenced is
4798 view-converted to an array type for this purpose. If the index
4799 is constant the canonical representation in the middle-end is a
4800 BIT_FIELD_REF so re-write the former to the latter here. */
4801 if (TREE_CODE (*t
) == ARRAY_REF
4802 && TREE_CODE (TREE_OPERAND (*t
, 0)) == VIEW_CONVERT_EXPR
4803 && TREE_CODE (TREE_OPERAND (*t
, 1)) == INTEGER_CST
4804 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t
, 0), 0))))
4806 tree vtype
= TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t
, 0), 0));
4807 if (VECTOR_TYPE_P (vtype
))
4809 tree low
= array_ref_low_bound (*t
);
4810 if (TREE_CODE (low
) == INTEGER_CST
)
4812 if (tree_int_cst_le (low
, TREE_OPERAND (*t
, 1)))
4814 widest_int idx
= wi::sub (wi::to_widest (TREE_OPERAND (*t
, 1)),
4815 wi::to_widest (low
));
4816 idx
= wi::mul (idx
, wi::to_widest
4817 (TYPE_SIZE (TREE_TYPE (*t
))));
4819 = wi::add (idx
, wi::to_widest (TYPE_SIZE (TREE_TYPE (*t
))));
4820 if (wi::les_p (ext
, wi::to_widest (TYPE_SIZE (vtype
))))
4822 *t
= build3_loc (EXPR_LOCATION (*t
), BIT_FIELD_REF
,
4824 TREE_OPERAND (TREE_OPERAND (*t
, 0), 0),
4825 TYPE_SIZE (TREE_TYPE (*t
)),
4826 wide_int_to_tree (bitsizetype
, idx
));
4834 while (handled_component_p (*t
))
4835 t
= &TREE_OPERAND (*t
, 0);
4837 /* Canonicalize MEM [&foo.bar, 0] which appears after propagating
4838 of invariant addresses into a SSA name MEM_REF address. */
4839 if (TREE_CODE (*t
) == MEM_REF
4840 || TREE_CODE (*t
) == TARGET_MEM_REF
)
4842 tree addr
= TREE_OPERAND (*t
, 0);
4843 if (TREE_CODE (addr
) == ADDR_EXPR
4844 && (TREE_CODE (TREE_OPERAND (addr
, 0)) == MEM_REF
4845 || handled_component_p (TREE_OPERAND (addr
, 0))))
4849 base
= get_addr_base_and_unit_offset (TREE_OPERAND (addr
, 0),
4854 TREE_OPERAND (*t
, 0) = build_fold_addr_expr (base
);
4855 TREE_OPERAND (*t
, 1) = int_const_binop (PLUS_EXPR
,
4856 TREE_OPERAND (*t
, 1),
4857 size_int (coffset
));
4860 gcc_checking_assert (TREE_CODE (TREE_OPERAND (*t
, 0)) == DEBUG_EXPR_DECL
4861 || is_gimple_mem_ref_addr (TREE_OPERAND (*t
, 0)));
4864 /* Canonicalize back MEM_REFs to plain reference trees if the object
4865 accessed is a decl that has the same access semantics as the MEM_REF. */
4866 if (TREE_CODE (*t
) == MEM_REF
4867 && TREE_CODE (TREE_OPERAND (*t
, 0)) == ADDR_EXPR
4868 && integer_zerop (TREE_OPERAND (*t
, 1))
4869 && MR_DEPENDENCE_CLIQUE (*t
) == 0)
4871 tree decl
= TREE_OPERAND (TREE_OPERAND (*t
, 0), 0);
4872 tree alias_type
= TREE_TYPE (TREE_OPERAND (*t
, 1));
4873 if (/* Same volatile qualification. */
4874 TREE_THIS_VOLATILE (*t
) == TREE_THIS_VOLATILE (decl
)
4875 /* Same TBAA behavior with -fstrict-aliasing. */
4876 && !TYPE_REF_CAN_ALIAS_ALL (alias_type
)
4877 && (TYPE_MAIN_VARIANT (TREE_TYPE (decl
))
4878 == TYPE_MAIN_VARIANT (TREE_TYPE (alias_type
)))
4879 /* Same alignment. */
4880 && TYPE_ALIGN (TREE_TYPE (decl
)) == TYPE_ALIGN (TREE_TYPE (*t
))
4881 /* We have to look out here to not drop a required conversion
4882 from the rhs to the lhs if *t appears on the lhs or vice-versa
4883 if it appears on the rhs. Thus require strict type
4885 && types_compatible_p (TREE_TYPE (*t
), TREE_TYPE (decl
)))
4887 *t
= TREE_OPERAND (TREE_OPERAND (*t
, 0), 0);
4892 /* Canonicalize TARGET_MEM_REF in particular with respect to
4893 the indexes becoming constant. */
4894 else if (TREE_CODE (*t
) == TARGET_MEM_REF
)
4896 tree tem
= maybe_fold_tmr (*t
);
4907 /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument
4908 distinguishes both cases. */
4911 fold_stmt_1 (gimple_stmt_iterator
*gsi
, bool inplace
, tree (*valueize
) (tree
))
4913 bool changed
= false;
4914 gimple
*stmt
= gsi_stmt (*gsi
);
4915 bool nowarning
= gimple_no_warning_p (stmt
);
4917 fold_defer_overflow_warnings ();
4919 /* First do required canonicalization of [TARGET_]MEM_REF addresses
4921 ??? This shouldn't be done in generic folding but in the
4922 propagation helpers which also know whether an address was
4924 Also canonicalize operand order. */
4925 switch (gimple_code (stmt
))
4928 if (gimple_assign_rhs_class (stmt
) == GIMPLE_SINGLE_RHS
)
4930 tree
*rhs
= gimple_assign_rhs1_ptr (stmt
);
4931 if ((REFERENCE_CLASS_P (*rhs
)
4932 || TREE_CODE (*rhs
) == ADDR_EXPR
)
4933 && maybe_canonicalize_mem_ref_addr (rhs
))
4935 tree
*lhs
= gimple_assign_lhs_ptr (stmt
);
4936 if (REFERENCE_CLASS_P (*lhs
)
4937 && maybe_canonicalize_mem_ref_addr (lhs
))
4942 /* Canonicalize operand order. */
4943 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4944 if (TREE_CODE_CLASS (code
) == tcc_comparison
4945 || commutative_tree_code (code
)
4946 || commutative_ternary_tree_code (code
))
4948 tree rhs1
= gimple_assign_rhs1 (stmt
);
4949 tree rhs2
= gimple_assign_rhs2 (stmt
);
4950 if (tree_swap_operands_p (rhs1
, rhs2
))
4952 gimple_assign_set_rhs1 (stmt
, rhs2
);
4953 gimple_assign_set_rhs2 (stmt
, rhs1
);
4954 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4955 gimple_assign_set_rhs_code (stmt
,
4956 swap_tree_comparison (code
));
4964 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
4966 tree
*arg
= gimple_call_arg_ptr (stmt
, i
);
4967 if (REFERENCE_CLASS_P (*arg
)
4968 && maybe_canonicalize_mem_ref_addr (arg
))
4971 tree
*lhs
= gimple_call_lhs_ptr (stmt
);
4973 && REFERENCE_CLASS_P (*lhs
)
4974 && maybe_canonicalize_mem_ref_addr (lhs
))
4980 gasm
*asm_stmt
= as_a
<gasm
*> (stmt
);
4981 for (i
= 0; i
< gimple_asm_noutputs (asm_stmt
); ++i
)
4983 tree link
= gimple_asm_output_op (asm_stmt
, i
);
4984 tree op
= TREE_VALUE (link
);
4985 if (REFERENCE_CLASS_P (op
)
4986 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link
)))
4989 for (i
= 0; i
< gimple_asm_ninputs (asm_stmt
); ++i
)
4991 tree link
= gimple_asm_input_op (asm_stmt
, i
);
4992 tree op
= TREE_VALUE (link
);
4993 if ((REFERENCE_CLASS_P (op
)
4994 || TREE_CODE (op
) == ADDR_EXPR
)
4995 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link
)))
5001 if (gimple_debug_bind_p (stmt
))
5003 tree
*val
= gimple_debug_bind_get_value_ptr (stmt
);
5005 && (REFERENCE_CLASS_P (*val
)
5006 || TREE_CODE (*val
) == ADDR_EXPR
)
5007 && maybe_canonicalize_mem_ref_addr (val
))
5013 /* Canonicalize operand order. */
5014 tree lhs
= gimple_cond_lhs (stmt
);
5015 tree rhs
= gimple_cond_rhs (stmt
);
5016 if (tree_swap_operands_p (lhs
, rhs
))
5018 gcond
*gc
= as_a
<gcond
*> (stmt
);
5019 gimple_cond_set_lhs (gc
, rhs
);
5020 gimple_cond_set_rhs (gc
, lhs
);
5021 gimple_cond_set_code (gc
,
5022 swap_tree_comparison (gimple_cond_code (gc
)));
5029 /* Dispatch to pattern-based folding. */
5031 || is_gimple_assign (stmt
)
5032 || gimple_code (stmt
) == GIMPLE_COND
)
5034 gimple_seq seq
= NULL
;
5035 gimple_match_op res_op
;
5036 if (gimple_simplify (stmt
, &res_op
, inplace
? NULL
: &seq
,
5037 valueize
, valueize
))
5039 if (replace_stmt_with_simplification (gsi
, &res_op
, &seq
, inplace
))
5042 gimple_seq_discard (seq
);
5046 stmt
= gsi_stmt (*gsi
);
5048 /* Fold the main computation performed by the statement. */
5049 switch (gimple_code (stmt
))
5053 /* Try to canonicalize for boolean-typed X the comparisons
5054 X == 0, X == 1, X != 0, and X != 1. */
5055 if (gimple_assign_rhs_code (stmt
) == EQ_EXPR
5056 || gimple_assign_rhs_code (stmt
) == NE_EXPR
)
5058 tree lhs
= gimple_assign_lhs (stmt
);
5059 tree op1
= gimple_assign_rhs1 (stmt
);
5060 tree op2
= gimple_assign_rhs2 (stmt
);
5061 tree type
= TREE_TYPE (op1
);
5063 /* Check whether the comparison operands are of the same boolean
5064 type as the result type is.
5065 Check that second operand is an integer-constant with value
5067 if (TREE_CODE (op2
) == INTEGER_CST
5068 && (integer_zerop (op2
) || integer_onep (op2
))
5069 && useless_type_conversion_p (TREE_TYPE (lhs
), type
))
5071 enum tree_code cmp_code
= gimple_assign_rhs_code (stmt
);
5072 bool is_logical_not
= false;
5074 /* X == 0 and X != 1 is a logical-not.of X
5075 X == 1 and X != 0 is X */
5076 if ((cmp_code
== EQ_EXPR
&& integer_zerop (op2
))
5077 || (cmp_code
== NE_EXPR
&& integer_onep (op2
)))
5078 is_logical_not
= true;
5080 if (is_logical_not
== false)
5081 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op1
), op1
);
5082 /* Only for one-bit precision typed X the transformation
5083 !X -> ~X is valied. */
5084 else if (TYPE_PRECISION (type
) == 1)
5085 gimple_assign_set_rhs_with_ops (gsi
, BIT_NOT_EXPR
, op1
);
5086 /* Otherwise we use !X -> X ^ 1. */
5088 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op1
,
5089 build_int_cst (type
, 1));
5095 unsigned old_num_ops
= gimple_num_ops (stmt
);
5096 tree lhs
= gimple_assign_lhs (stmt
);
5097 tree new_rhs
= fold_gimple_assign (gsi
);
5099 && !useless_type_conversion_p (TREE_TYPE (lhs
),
5100 TREE_TYPE (new_rhs
)))
5101 new_rhs
= fold_convert (TREE_TYPE (lhs
), new_rhs
);
5104 || get_gimple_rhs_num_ops (TREE_CODE (new_rhs
)) < old_num_ops
))
5106 gimple_assign_set_rhs_from_tree (gsi
, new_rhs
);
5113 changed
|= gimple_fold_call (gsi
, inplace
);
5117 /* Fold *& in asm operands. */
5119 gasm
*asm_stmt
= as_a
<gasm
*> (stmt
);
5121 const char **oconstraints
;
5122 const char *constraint
;
5123 bool allows_mem
, allows_reg
;
5125 noutputs
= gimple_asm_noutputs (asm_stmt
);
5126 oconstraints
= XALLOCAVEC (const char *, noutputs
);
5128 for (i
= 0; i
< gimple_asm_noutputs (asm_stmt
); ++i
)
5130 tree link
= gimple_asm_output_op (asm_stmt
, i
);
5131 tree op
= TREE_VALUE (link
);
5133 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
5134 if (REFERENCE_CLASS_P (op
)
5135 && (op
= maybe_fold_reference (op
, true)) != NULL_TREE
)
5137 TREE_VALUE (link
) = op
;
5141 for (i
= 0; i
< gimple_asm_ninputs (asm_stmt
); ++i
)
5143 tree link
= gimple_asm_input_op (asm_stmt
, i
);
5144 tree op
= TREE_VALUE (link
);
5146 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
5147 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0,
5148 oconstraints
, &allows_mem
, &allows_reg
);
5149 if (REFERENCE_CLASS_P (op
)
5150 && (op
= maybe_fold_reference (op
, !allows_reg
&& allows_mem
))
5153 TREE_VALUE (link
) = op
;
5161 if (gimple_debug_bind_p (stmt
))
5163 tree val
= gimple_debug_bind_get_value (stmt
);
5165 && REFERENCE_CLASS_P (val
))
5167 tree tem
= maybe_fold_reference (val
, false);
5170 gimple_debug_bind_set_value (stmt
, tem
);
5175 && TREE_CODE (val
) == ADDR_EXPR
)
5177 tree ref
= TREE_OPERAND (val
, 0);
5178 tree tem
= maybe_fold_reference (ref
, false);
5181 tem
= build_fold_addr_expr_with_type (tem
, TREE_TYPE (val
));
5182 gimple_debug_bind_set_value (stmt
, tem
);
5191 greturn
*ret_stmt
= as_a
<greturn
*> (stmt
);
5192 tree ret
= gimple_return_retval(ret_stmt
);
5194 if (ret
&& TREE_CODE (ret
) == SSA_NAME
&& valueize
)
5196 tree val
= valueize (ret
);
5197 if (val
&& val
!= ret
5198 && may_propagate_copy (ret
, val
))
5200 gimple_return_set_retval (ret_stmt
, val
);
5210 stmt
= gsi_stmt (*gsi
);
5212 /* Fold *& on the lhs. */
5213 if (gimple_has_lhs (stmt
))
5215 tree lhs
= gimple_get_lhs (stmt
);
5216 if (lhs
&& REFERENCE_CLASS_P (lhs
))
5218 tree new_lhs
= maybe_fold_reference (lhs
, true);
5221 gimple_set_lhs (stmt
, new_lhs
);
5227 fold_undefer_overflow_warnings (changed
&& !nowarning
, stmt
, 0);
5231 /* Valueziation callback that ends up not following SSA edges. */
5234 no_follow_ssa_edges (tree
)
5239 /* Valueization callback that ends up following single-use SSA edges only. */
5242 follow_single_use_edges (tree val
)
5244 if (TREE_CODE (val
) == SSA_NAME
5245 && !has_single_use (val
))
5250 /* Valueization callback that follows all SSA edges. */
5253 follow_all_ssa_edges (tree val
)
5258 /* Fold the statement pointed to by GSI. In some cases, this function may
5259 replace the whole statement with a new one. Returns true iff folding
5261 The statement pointed to by GSI should be in valid gimple form but may
5262 be in unfolded state as resulting from for example constant propagation
5263 which can produce *&x = 0. */
5266 fold_stmt (gimple_stmt_iterator
*gsi
)
5268 return fold_stmt_1 (gsi
, false, no_follow_ssa_edges
);
5272 fold_stmt (gimple_stmt_iterator
*gsi
, tree (*valueize
) (tree
))
5274 return fold_stmt_1 (gsi
, false, valueize
);
5277 /* Perform the minimal folding on statement *GSI. Only operations like
5278 *&x created by constant propagation are handled. The statement cannot
5279 be replaced with a new one. Return true if the statement was
5280 changed, false otherwise.
5281 The statement *GSI should be in valid gimple form but may
5282 be in unfolded state as resulting from for example constant propagation
5283 which can produce *&x = 0. */
5286 fold_stmt_inplace (gimple_stmt_iterator
*gsi
)
5288 gimple
*stmt
= gsi_stmt (*gsi
);
5289 bool changed
= fold_stmt_1 (gsi
, true, no_follow_ssa_edges
);
5290 gcc_assert (gsi_stmt (*gsi
) == stmt
);
5294 /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
5295 if EXPR is null or we don't know how.
5296 If non-null, the result always has boolean type. */
5299 canonicalize_bool (tree expr
, bool invert
)
5305 if (integer_nonzerop (expr
))
5306 return boolean_false_node
;
5307 else if (integer_zerop (expr
))
5308 return boolean_true_node
;
5309 else if (TREE_CODE (expr
) == SSA_NAME
)
5310 return fold_build2 (EQ_EXPR
, boolean_type_node
, expr
,
5311 build_int_cst (TREE_TYPE (expr
), 0));
5312 else if (COMPARISON_CLASS_P (expr
))
5313 return fold_build2 (invert_tree_comparison (TREE_CODE (expr
), false),
5315 TREE_OPERAND (expr
, 0),
5316 TREE_OPERAND (expr
, 1));
5322 if (TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
)
5324 if (integer_nonzerop (expr
))
5325 return boolean_true_node
;
5326 else if (integer_zerop (expr
))
5327 return boolean_false_node
;
5328 else if (TREE_CODE (expr
) == SSA_NAME
)
5329 return fold_build2 (NE_EXPR
, boolean_type_node
, expr
,
5330 build_int_cst (TREE_TYPE (expr
), 0));
5331 else if (COMPARISON_CLASS_P (expr
))
5332 return fold_build2 (TREE_CODE (expr
),
5334 TREE_OPERAND (expr
, 0),
5335 TREE_OPERAND (expr
, 1));
5341 /* Check to see if a boolean expression EXPR is logically equivalent to the
5342 comparison (OP1 CODE OP2). Check for various identities involving
5346 same_bool_comparison_p (const_tree expr
, enum tree_code code
,
5347 const_tree op1
, const_tree op2
)
5351 /* The obvious case. */
5352 if (TREE_CODE (expr
) == code
5353 && operand_equal_p (TREE_OPERAND (expr
, 0), op1
, 0)
5354 && operand_equal_p (TREE_OPERAND (expr
, 1), op2
, 0))
5357 /* Check for comparing (name, name != 0) and the case where expr
5358 is an SSA_NAME with a definition matching the comparison. */
5359 if (TREE_CODE (expr
) == SSA_NAME
5360 && TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
)
5362 if (operand_equal_p (expr
, op1
, 0))
5363 return ((code
== NE_EXPR
&& integer_zerop (op2
))
5364 || (code
== EQ_EXPR
&& integer_nonzerop (op2
)));
5365 s
= SSA_NAME_DEF_STMT (expr
);
5366 if (is_gimple_assign (s
)
5367 && gimple_assign_rhs_code (s
) == code
5368 && operand_equal_p (gimple_assign_rhs1 (s
), op1
, 0)
5369 && operand_equal_p (gimple_assign_rhs2 (s
), op2
, 0))
5373 /* If op1 is of the form (name != 0) or (name == 0), and the definition
5374 of name is a comparison, recurse. */
5375 if (TREE_CODE (op1
) == SSA_NAME
5376 && TREE_CODE (TREE_TYPE (op1
)) == BOOLEAN_TYPE
)
5378 s
= SSA_NAME_DEF_STMT (op1
);
5379 if (is_gimple_assign (s
)
5380 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
)
5382 enum tree_code c
= gimple_assign_rhs_code (s
);
5383 if ((c
== NE_EXPR
&& integer_zerop (op2
))
5384 || (c
== EQ_EXPR
&& integer_nonzerop (op2
)))
5385 return same_bool_comparison_p (expr
, c
,
5386 gimple_assign_rhs1 (s
),
5387 gimple_assign_rhs2 (s
));
5388 if ((c
== EQ_EXPR
&& integer_zerop (op2
))
5389 || (c
== NE_EXPR
&& integer_nonzerop (op2
)))
5390 return same_bool_comparison_p (expr
,
5391 invert_tree_comparison (c
, false),
5392 gimple_assign_rhs1 (s
),
5393 gimple_assign_rhs2 (s
));
5399 /* Check to see if two boolean expressions OP1 and OP2 are logically
5403 same_bool_result_p (const_tree op1
, const_tree op2
)
5405 /* Simple cases first. */
5406 if (operand_equal_p (op1
, op2
, 0))
5409 /* Check the cases where at least one of the operands is a comparison.
5410 These are a bit smarter than operand_equal_p in that they apply some
5411 identifies on SSA_NAMEs. */
5412 if (COMPARISON_CLASS_P (op2
)
5413 && same_bool_comparison_p (op1
, TREE_CODE (op2
),
5414 TREE_OPERAND (op2
, 0),
5415 TREE_OPERAND (op2
, 1)))
5417 if (COMPARISON_CLASS_P (op1
)
5418 && same_bool_comparison_p (op2
, TREE_CODE (op1
),
5419 TREE_OPERAND (op1
, 0),
5420 TREE_OPERAND (op1
, 1)))
5427 /* Forward declarations for some mutually recursive functions. */
5430 and_comparisons_1 (tree type
, enum tree_code code1
, tree op1a
, tree op1b
,
5431 enum tree_code code2
, tree op2a
, tree op2b
);
5433 and_var_with_comparison (tree type
, tree var
, bool invert
,
5434 enum tree_code code2
, tree op2a
, tree op2b
);
5436 and_var_with_comparison_1 (tree type
, gimple
*stmt
,
5437 enum tree_code code2
, tree op2a
, tree op2b
);
5439 or_comparisons_1 (tree
, enum tree_code code1
, tree op1a
, tree op1b
,
5440 enum tree_code code2
, tree op2a
, tree op2b
);
5442 or_var_with_comparison (tree
, tree var
, bool invert
,
5443 enum tree_code code2
, tree op2a
, tree op2b
);
5445 or_var_with_comparison_1 (tree
, gimple
*stmt
,
5446 enum tree_code code2
, tree op2a
, tree op2b
);
5448 /* Helper function for and_comparisons_1: try to simplify the AND of the
5449 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
5450 If INVERT is true, invert the value of the VAR before doing the AND.
5451 Return NULL_EXPR if we can't simplify this to a single expression. */
5454 and_var_with_comparison (tree type
, tree var
, bool invert
,
5455 enum tree_code code2
, tree op2a
, tree op2b
)
5458 gimple
*stmt
= SSA_NAME_DEF_STMT (var
);
5460 /* We can only deal with variables whose definitions are assignments. */
5461 if (!is_gimple_assign (stmt
))
5464 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
5465 !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
5466 Then we only have to consider the simpler non-inverted cases. */
5468 t
= or_var_with_comparison_1 (type
, stmt
,
5469 invert_tree_comparison (code2
, false),
5472 t
= and_var_with_comparison_1 (type
, stmt
, code2
, op2a
, op2b
);
5473 return canonicalize_bool (t
, invert
);
5476 /* Try to simplify the AND of the ssa variable defined by the assignment
5477 STMT with the comparison specified by (OP2A CODE2 OP2B).
5478 Return NULL_EXPR if we can't simplify this to a single expression. */
5481 and_var_with_comparison_1 (tree type
, gimple
*stmt
,
5482 enum tree_code code2
, tree op2a
, tree op2b
)
5484 tree var
= gimple_assign_lhs (stmt
);
5485 tree true_test_var
= NULL_TREE
;
5486 tree false_test_var
= NULL_TREE
;
5487 enum tree_code innercode
= gimple_assign_rhs_code (stmt
);
5489 /* Check for identities like (var AND (var == 0)) => false. */
5490 if (TREE_CODE (op2a
) == SSA_NAME
5491 && TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
)
5493 if ((code2
== NE_EXPR
&& integer_zerop (op2b
))
5494 || (code2
== EQ_EXPR
&& integer_nonzerop (op2b
)))
5496 true_test_var
= op2a
;
5497 if (var
== true_test_var
)
5500 else if ((code2
== EQ_EXPR
&& integer_zerop (op2b
))
5501 || (code2
== NE_EXPR
&& integer_nonzerop (op2b
)))
5503 false_test_var
= op2a
;
5504 if (var
== false_test_var
)
5505 return boolean_false_node
;
5509 /* If the definition is a comparison, recurse on it. */
5510 if (TREE_CODE_CLASS (innercode
) == tcc_comparison
)
5512 tree t
= and_comparisons_1 (type
, innercode
,
5513 gimple_assign_rhs1 (stmt
),
5514 gimple_assign_rhs2 (stmt
),
5522 /* If the definition is an AND or OR expression, we may be able to
5523 simplify by reassociating. */
5524 if (TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
5525 && (innercode
== BIT_AND_EXPR
|| innercode
== BIT_IOR_EXPR
))
5527 tree inner1
= gimple_assign_rhs1 (stmt
);
5528 tree inner2
= gimple_assign_rhs2 (stmt
);
5531 tree partial
= NULL_TREE
;
5532 bool is_and
= (innercode
== BIT_AND_EXPR
);
5534 /* Check for boolean identities that don't require recursive examination
5536 inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
5537 inner1 AND (inner1 OR inner2) => inner1
5538 !inner1 AND (inner1 AND inner2) => false
5539 !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
5540 Likewise for similar cases involving inner2. */
5541 if (inner1
== true_test_var
)
5542 return (is_and
? var
: inner1
);
5543 else if (inner2
== true_test_var
)
5544 return (is_and
? var
: inner2
);
5545 else if (inner1
== false_test_var
)
5547 ? boolean_false_node
5548 : and_var_with_comparison (type
, inner2
, false, code2
, op2a
,
5550 else if (inner2
== false_test_var
)
5552 ? boolean_false_node
5553 : and_var_with_comparison (type
, inner1
, false, code2
, op2a
,
5556 /* Next, redistribute/reassociate the AND across the inner tests.
5557 Compute the first partial result, (inner1 AND (op2a code op2b)) */
5558 if (TREE_CODE (inner1
) == SSA_NAME
5559 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner1
))
5560 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
5561 && (t
= maybe_fold_and_comparisons (type
, gimple_assign_rhs_code (s
),
5562 gimple_assign_rhs1 (s
),
5563 gimple_assign_rhs2 (s
),
5564 code2
, op2a
, op2b
)))
5566 /* Handle the AND case, where we are reassociating:
5567 (inner1 AND inner2) AND (op2a code2 op2b)
5569 If the partial result t is a constant, we win. Otherwise
5570 continue on to try reassociating with the other inner test. */
5573 if (integer_onep (t
))
5575 else if (integer_zerop (t
))
5576 return boolean_false_node
;
5579 /* Handle the OR case, where we are redistributing:
5580 (inner1 OR inner2) AND (op2a code2 op2b)
5581 => (t OR (inner2 AND (op2a code2 op2b))) */
5582 else if (integer_onep (t
))
5583 return boolean_true_node
;
5585 /* Save partial result for later. */
5589 /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
5590 if (TREE_CODE (inner2
) == SSA_NAME
5591 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner2
))
5592 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
5593 && (t
= maybe_fold_and_comparisons (type
, gimple_assign_rhs_code (s
),
5594 gimple_assign_rhs1 (s
),
5595 gimple_assign_rhs2 (s
),
5596 code2
, op2a
, op2b
)))
5598 /* Handle the AND case, where we are reassociating:
5599 (inner1 AND inner2) AND (op2a code2 op2b)
5600 => (inner1 AND t) */
5603 if (integer_onep (t
))
5605 else if (integer_zerop (t
))
5606 return boolean_false_node
;
5607 /* If both are the same, we can apply the identity
5609 else if (partial
&& same_bool_result_p (t
, partial
))
5613 /* Handle the OR case. where we are redistributing:
5614 (inner1 OR inner2) AND (op2a code2 op2b)
5615 => (t OR (inner1 AND (op2a code2 op2b)))
5616 => (t OR partial) */
5619 if (integer_onep (t
))
5620 return boolean_true_node
;
5623 /* We already got a simplification for the other
5624 operand to the redistributed OR expression. The
5625 interesting case is when at least one is false.
5626 Or, if both are the same, we can apply the identity
5628 if (integer_zerop (partial
))
5630 else if (integer_zerop (t
))
5632 else if (same_bool_result_p (t
, partial
))
5641 /* Try to simplify the AND of two comparisons defined by
5642 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
5643 If this can be done without constructing an intermediate value,
5644 return the resulting tree; otherwise NULL_TREE is returned.
5645 This function is deliberately asymmetric as it recurses on SSA_DEFs
5646 in the first comparison but not the second. */
5649 and_comparisons_1 (tree type
, enum tree_code code1
, tree op1a
, tree op1b
,
5650 enum tree_code code2
, tree op2a
, tree op2b
)
5652 tree truth_type
= truth_type_for (TREE_TYPE (op1a
));
5654 /* First check for ((x CODE1 y) AND (x CODE2 y)). */
5655 if (operand_equal_p (op1a
, op2a
, 0)
5656 && operand_equal_p (op1b
, op2b
, 0))
5658 /* Result will be either NULL_TREE, or a combined comparison. */
5659 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
5660 TRUTH_ANDIF_EXPR
, code1
, code2
,
5661 truth_type
, op1a
, op1b
);
5666 /* Likewise the swapped case of the above. */
5667 if (operand_equal_p (op1a
, op2b
, 0)
5668 && operand_equal_p (op1b
, op2a
, 0))
5670 /* Result will be either NULL_TREE, or a combined comparison. */
5671 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
5672 TRUTH_ANDIF_EXPR
, code1
,
5673 swap_tree_comparison (code2
),
5674 truth_type
, op1a
, op1b
);
5679 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
5680 NAME's definition is a truth value. See if there are any simplifications
5681 that can be done against the NAME's definition. */
5682 if (TREE_CODE (op1a
) == SSA_NAME
5683 && (code1
== NE_EXPR
|| code1
== EQ_EXPR
)
5684 && (integer_zerop (op1b
) || integer_onep (op1b
)))
5686 bool invert
= ((code1
== EQ_EXPR
&& integer_zerop (op1b
))
5687 || (code1
== NE_EXPR
&& integer_onep (op1b
)));
5688 gimple
*stmt
= SSA_NAME_DEF_STMT (op1a
);
5689 switch (gimple_code (stmt
))
5692 /* Try to simplify by copy-propagating the definition. */
5693 return and_var_with_comparison (type
, op1a
, invert
, code2
, op2a
,
5697 /* If every argument to the PHI produces the same result when
5698 ANDed with the second comparison, we win.
5699 Do not do this unless the type is bool since we need a bool
5700 result here anyway. */
5701 if (TREE_CODE (TREE_TYPE (op1a
)) == BOOLEAN_TYPE
)
5703 tree result
= NULL_TREE
;
5705 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
5707 tree arg
= gimple_phi_arg_def (stmt
, i
);
5709 /* If this PHI has itself as an argument, ignore it.
5710 If all the other args produce the same result,
5712 if (arg
== gimple_phi_result (stmt
))
5714 else if (TREE_CODE (arg
) == INTEGER_CST
)
5716 if (invert
? integer_nonzerop (arg
) : integer_zerop (arg
))
5719 result
= boolean_false_node
;
5720 else if (!integer_zerop (result
))
5724 result
= fold_build2 (code2
, boolean_type_node
,
5726 else if (!same_bool_comparison_p (result
,
5730 else if (TREE_CODE (arg
) == SSA_NAME
5731 && !SSA_NAME_IS_DEFAULT_DEF (arg
))
5734 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
5735 /* In simple cases we can look through PHI nodes,
5736 but we have to be careful with loops.
5738 if (! dom_info_available_p (CDI_DOMINATORS
)
5739 || gimple_bb (def_stmt
) == gimple_bb (stmt
)
5740 || dominated_by_p (CDI_DOMINATORS
,
5741 gimple_bb (def_stmt
),
5744 temp
= and_var_with_comparison (type
, arg
, invert
, code2
,
5750 else if (!same_bool_result_p (result
, temp
))
5766 /* Helper function for maybe_fold_and_comparisons and maybe_fold_or_comparisons
5767 : try to simplify the AND/OR of the ssa variable VAR with the comparison
5768 specified by (OP2A CODE2 OP2B) from match.pd. Return NULL_EXPR if we can't
5769 simplify this to a single expression. As we are going to lower the cost
5770 of building SSA names / gimple stmts significantly, we need to allocate
5771 them ont the stack. This will cause the code to be a bit ugly. */
5774 maybe_fold_comparisons_from_match_pd (tree type
, enum tree_code code
,
5775 enum tree_code code1
,
5776 tree op1a
, tree op1b
,
5777 enum tree_code code2
, tree op2a
,
5780 /* Allocate gimple stmt1 on the stack. */
5782 = (gassign
*) XALLOCAVEC (char, gimple_size (GIMPLE_ASSIGN
, 3));
5783 gimple_init (stmt1
, GIMPLE_ASSIGN
, 3);
5784 gimple_assign_set_rhs_code (stmt1
, code1
);
5785 gimple_assign_set_rhs1 (stmt1
, op1a
);
5786 gimple_assign_set_rhs2 (stmt1
, op1b
);
5788 /* Allocate gimple stmt2 on the stack. */
5790 = (gassign
*) XALLOCAVEC (char, gimple_size (GIMPLE_ASSIGN
, 3));
5791 gimple_init (stmt2
, GIMPLE_ASSIGN
, 3);
5792 gimple_assign_set_rhs_code (stmt2
, code2
);
5793 gimple_assign_set_rhs1 (stmt2
, op2a
);
5794 gimple_assign_set_rhs2 (stmt2
, op2b
);
5796 /* Allocate SSA names(lhs1) on the stack. */
5797 tree lhs1
= (tree
)XALLOCA (tree_ssa_name
);
5798 memset (lhs1
, 0, sizeof (tree_ssa_name
));
5799 TREE_SET_CODE (lhs1
, SSA_NAME
);
5800 TREE_TYPE (lhs1
) = type
;
5801 init_ssa_name_imm_use (lhs1
);
5803 /* Allocate SSA names(lhs2) on the stack. */
5804 tree lhs2
= (tree
)XALLOCA (tree_ssa_name
);
5805 memset (lhs2
, 0, sizeof (tree_ssa_name
));
5806 TREE_SET_CODE (lhs2
, SSA_NAME
);
5807 TREE_TYPE (lhs2
) = type
;
5808 init_ssa_name_imm_use (lhs2
);
5810 gimple_assign_set_lhs (stmt1
, lhs1
);
5811 gimple_assign_set_lhs (stmt2
, lhs2
);
5813 gimple_match_op
op (gimple_match_cond::UNCOND
, code
,
5814 type
, gimple_assign_lhs (stmt1
),
5815 gimple_assign_lhs (stmt2
));
5816 if (op
.resimplify (NULL
, follow_all_ssa_edges
))
5818 if (gimple_simplified_result_is_gimple_val (&op
))
5820 tree res
= op
.ops
[0];
5822 return build2 (code1
, type
, op1a
, op1b
);
5823 else if (res
== lhs2
)
5824 return build2 (code2
, type
, op2a
, op2b
);
5828 else if (op
.code
.is_tree_code ()
5829 && TREE_CODE_CLASS ((tree_code
)op
.code
) == tcc_comparison
)
5831 tree op0
= op
.ops
[0];
5832 tree op1
= op
.ops
[1];
5833 if (op0
== lhs1
|| op0
== lhs2
|| op1
== lhs1
|| op1
== lhs2
)
5834 return NULL_TREE
; /* not simple */
5836 return build2 ((enum tree_code
)op
.code
, op
.type
, op0
, op1
);
5843 /* Try to simplify the AND of two comparisons, specified by
5844 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
5845 If this can be simplified to a single expression (without requiring
5846 introducing more SSA variables to hold intermediate values),
5847 return the resulting tree. Otherwise return NULL_TREE.
5848 If the result expression is non-null, it has boolean type. */
5851 maybe_fold_and_comparisons (tree type
,
5852 enum tree_code code1
, tree op1a
, tree op1b
,
5853 enum tree_code code2
, tree op2a
, tree op2b
)
5855 if (tree t
= and_comparisons_1 (type
, code1
, op1a
, op1b
, code2
, op2a
, op2b
))
5858 if (tree t
= and_comparisons_1 (type
, code2
, op2a
, op2b
, code1
, op1a
, op1b
))
5861 if (tree t
= maybe_fold_comparisons_from_match_pd (type
, BIT_AND_EXPR
, code1
,
5862 op1a
, op1b
, code2
, op2a
,
5869 /* Helper function for or_comparisons_1: try to simplify the OR of the
5870 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
5871 If INVERT is true, invert the value of VAR before doing the OR.
5872 Return NULL_EXPR if we can't simplify this to a single expression. */
5875 or_var_with_comparison (tree type
, tree var
, bool invert
,
5876 enum tree_code code2
, tree op2a
, tree op2b
)
5879 gimple
*stmt
= SSA_NAME_DEF_STMT (var
);
5881 /* We can only deal with variables whose definitions are assignments. */
5882 if (!is_gimple_assign (stmt
))
5885 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
5886 !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
5887 Then we only have to consider the simpler non-inverted cases. */
5889 t
= and_var_with_comparison_1 (type
, stmt
,
5890 invert_tree_comparison (code2
, false),
5893 t
= or_var_with_comparison_1 (type
, stmt
, code2
, op2a
, op2b
);
5894 return canonicalize_bool (t
, invert
);
5897 /* Try to simplify the OR of the ssa variable defined by the assignment
5898 STMT with the comparison specified by (OP2A CODE2 OP2B).
5899 Return NULL_EXPR if we can't simplify this to a single expression. */
5902 or_var_with_comparison_1 (tree type
, gimple
*stmt
,
5903 enum tree_code code2
, tree op2a
, tree op2b
)
5905 tree var
= gimple_assign_lhs (stmt
);
5906 tree true_test_var
= NULL_TREE
;
5907 tree false_test_var
= NULL_TREE
;
5908 enum tree_code innercode
= gimple_assign_rhs_code (stmt
);
5910 /* Check for identities like (var OR (var != 0)) => true . */
5911 if (TREE_CODE (op2a
) == SSA_NAME
5912 && TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
)
5914 if ((code2
== NE_EXPR
&& integer_zerop (op2b
))
5915 || (code2
== EQ_EXPR
&& integer_nonzerop (op2b
)))
5917 true_test_var
= op2a
;
5918 if (var
== true_test_var
)
5921 else if ((code2
== EQ_EXPR
&& integer_zerop (op2b
))
5922 || (code2
== NE_EXPR
&& integer_nonzerop (op2b
)))
5924 false_test_var
= op2a
;
5925 if (var
== false_test_var
)
5926 return boolean_true_node
;
5930 /* If the definition is a comparison, recurse on it. */
5931 if (TREE_CODE_CLASS (innercode
) == tcc_comparison
)
5933 tree t
= or_comparisons_1 (type
, innercode
,
5934 gimple_assign_rhs1 (stmt
),
5935 gimple_assign_rhs2 (stmt
),
5943 /* If the definition is an AND or OR expression, we may be able to
5944 simplify by reassociating. */
5945 if (TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
5946 && (innercode
== BIT_AND_EXPR
|| innercode
== BIT_IOR_EXPR
))
5948 tree inner1
= gimple_assign_rhs1 (stmt
);
5949 tree inner2
= gimple_assign_rhs2 (stmt
);
5952 tree partial
= NULL_TREE
;
5953 bool is_or
= (innercode
== BIT_IOR_EXPR
);
5955 /* Check for boolean identities that don't require recursive examination
5957 inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
5958 inner1 OR (inner1 AND inner2) => inner1
5959 !inner1 OR (inner1 OR inner2) => true
5960 !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
5962 if (inner1
== true_test_var
)
5963 return (is_or
? var
: inner1
);
5964 else if (inner2
== true_test_var
)
5965 return (is_or
? var
: inner2
);
5966 else if (inner1
== false_test_var
)
5969 : or_var_with_comparison (type
, inner2
, false, code2
, op2a
,
5971 else if (inner2
== false_test_var
)
5974 : or_var_with_comparison (type
, inner1
, false, code2
, op2a
,
5977 /* Next, redistribute/reassociate the OR across the inner tests.
5978 Compute the first partial result, (inner1 OR (op2a code op2b)) */
5979 if (TREE_CODE (inner1
) == SSA_NAME
5980 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner1
))
5981 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
5982 && (t
= maybe_fold_or_comparisons (type
, gimple_assign_rhs_code (s
),
5983 gimple_assign_rhs1 (s
),
5984 gimple_assign_rhs2 (s
),
5985 code2
, op2a
, op2b
)))
5987 /* Handle the OR case, where we are reassociating:
5988 (inner1 OR inner2) OR (op2a code2 op2b)
5990 If the partial result t is a constant, we win. Otherwise
5991 continue on to try reassociating with the other inner test. */
5994 if (integer_onep (t
))
5995 return boolean_true_node
;
5996 else if (integer_zerop (t
))
6000 /* Handle the AND case, where we are redistributing:
6001 (inner1 AND inner2) OR (op2a code2 op2b)
6002 => (t AND (inner2 OR (op2a code op2b))) */
6003 else if (integer_zerop (t
))
6004 return boolean_false_node
;
6006 /* Save partial result for later. */
6010 /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
6011 if (TREE_CODE (inner2
) == SSA_NAME
6012 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner2
))
6013 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
6014 && (t
= maybe_fold_or_comparisons (type
, gimple_assign_rhs_code (s
),
6015 gimple_assign_rhs1 (s
),
6016 gimple_assign_rhs2 (s
),
6017 code2
, op2a
, op2b
)))
6019 /* Handle the OR case, where we are reassociating:
6020 (inner1 OR inner2) OR (op2a code2 op2b)
6022 => (t OR partial) */
6025 if (integer_zerop (t
))
6027 else if (integer_onep (t
))
6028 return boolean_true_node
;
6029 /* If both are the same, we can apply the identity
6031 else if (partial
&& same_bool_result_p (t
, partial
))
6035 /* Handle the AND case, where we are redistributing:
6036 (inner1 AND inner2) OR (op2a code2 op2b)
6037 => (t AND (inner1 OR (op2a code2 op2b)))
6038 => (t AND partial) */
6041 if (integer_zerop (t
))
6042 return boolean_false_node
;
6045 /* We already got a simplification for the other
6046 operand to the redistributed AND expression. The
6047 interesting case is when at least one is true.
6048 Or, if both are the same, we can apply the identity
6050 if (integer_onep (partial
))
6052 else if (integer_onep (t
))
6054 else if (same_bool_result_p (t
, partial
))
6063 /* Try to simplify the OR of two comparisons defined by
6064 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
6065 If this can be done without constructing an intermediate value,
6066 return the resulting tree; otherwise NULL_TREE is returned.
6067 This function is deliberately asymmetric as it recurses on SSA_DEFs
6068 in the first comparison but not the second. */
6071 or_comparisons_1 (tree type
, enum tree_code code1
, tree op1a
, tree op1b
,
6072 enum tree_code code2
, tree op2a
, tree op2b
)
6074 tree truth_type
= truth_type_for (TREE_TYPE (op1a
));
6076 /* First check for ((x CODE1 y) OR (x CODE2 y)). */
6077 if (operand_equal_p (op1a
, op2a
, 0)
6078 && operand_equal_p (op1b
, op2b
, 0))
6080 /* Result will be either NULL_TREE, or a combined comparison. */
6081 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
6082 TRUTH_ORIF_EXPR
, code1
, code2
,
6083 truth_type
, op1a
, op1b
);
6088 /* Likewise the swapped case of the above. */
6089 if (operand_equal_p (op1a
, op2b
, 0)
6090 && operand_equal_p (op1b
, op2a
, 0))
6092 /* Result will be either NULL_TREE, or a combined comparison. */
6093 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
6094 TRUTH_ORIF_EXPR
, code1
,
6095 swap_tree_comparison (code2
),
6096 truth_type
, op1a
, op1b
);
6101 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
6102 NAME's definition is a truth value. See if there are any simplifications
6103 that can be done against the NAME's definition. */
6104 if (TREE_CODE (op1a
) == SSA_NAME
6105 && (code1
== NE_EXPR
|| code1
== EQ_EXPR
)
6106 && (integer_zerop (op1b
) || integer_onep (op1b
)))
6108 bool invert
= ((code1
== EQ_EXPR
&& integer_zerop (op1b
))
6109 || (code1
== NE_EXPR
&& integer_onep (op1b
)));
6110 gimple
*stmt
= SSA_NAME_DEF_STMT (op1a
);
6111 switch (gimple_code (stmt
))
6114 /* Try to simplify by copy-propagating the definition. */
6115 return or_var_with_comparison (type
, op1a
, invert
, code2
, op2a
,
6119 /* If every argument to the PHI produces the same result when
6120 ORed with the second comparison, we win.
6121 Do not do this unless the type is bool since we need a bool
6122 result here anyway. */
6123 if (TREE_CODE (TREE_TYPE (op1a
)) == BOOLEAN_TYPE
)
6125 tree result
= NULL_TREE
;
6127 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
6129 tree arg
= gimple_phi_arg_def (stmt
, i
);
6131 /* If this PHI has itself as an argument, ignore it.
6132 If all the other args produce the same result,
6134 if (arg
== gimple_phi_result (stmt
))
6136 else if (TREE_CODE (arg
) == INTEGER_CST
)
6138 if (invert
? integer_zerop (arg
) : integer_nonzerop (arg
))
6141 result
= boolean_true_node
;
6142 else if (!integer_onep (result
))
6146 result
= fold_build2 (code2
, boolean_type_node
,
6148 else if (!same_bool_comparison_p (result
,
6152 else if (TREE_CODE (arg
) == SSA_NAME
6153 && !SSA_NAME_IS_DEFAULT_DEF (arg
))
6156 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
6157 /* In simple cases we can look through PHI nodes,
6158 but we have to be careful with loops.
6160 if (! dom_info_available_p (CDI_DOMINATORS
)
6161 || gimple_bb (def_stmt
) == gimple_bb (stmt
)
6162 || dominated_by_p (CDI_DOMINATORS
,
6163 gimple_bb (def_stmt
),
6166 temp
= or_var_with_comparison (type
, arg
, invert
, code2
,
6172 else if (!same_bool_result_p (result
, temp
))
6188 /* Try to simplify the OR of two comparisons, specified by
6189 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
6190 If this can be simplified to a single expression (without requiring
6191 introducing more SSA variables to hold intermediate values),
6192 return the resulting tree. Otherwise return NULL_TREE.
6193 If the result expression is non-null, it has boolean type. */
6196 maybe_fold_or_comparisons (tree type
,
6197 enum tree_code code1
, tree op1a
, tree op1b
,
6198 enum tree_code code2
, tree op2a
, tree op2b
)
6200 if (tree t
= or_comparisons_1 (type
, code1
, op1a
, op1b
, code2
, op2a
, op2b
))
6203 if (tree t
= or_comparisons_1 (type
, code2
, op2a
, op2b
, code1
, op1a
, op1b
))
6206 if (tree t
= maybe_fold_comparisons_from_match_pd (type
, BIT_IOR_EXPR
, code1
,
6207 op1a
, op1b
, code2
, op2a
,
6214 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
6216 Either NULL_TREE, a simplified but non-constant or a constant
6219 ??? This should go into a gimple-fold-inline.h file to be eventually
6220 privatized with the single valueize function used in the various TUs
6221 to avoid the indirect function call overhead. */
6224 gimple_fold_stmt_to_constant_1 (gimple
*stmt
, tree (*valueize
) (tree
),
6225 tree (*gvalueize
) (tree
))
6227 gimple_match_op res_op
;
6228 /* ??? The SSA propagators do not correctly deal with following SSA use-def
6229 edges if there are intermediate VARYING defs. For this reason
6230 do not follow SSA edges here even though SCCVN can technically
6231 just deal fine with that. */
6232 if (gimple_simplify (stmt
, &res_op
, NULL
, gvalueize
, valueize
))
6234 tree res
= NULL_TREE
;
6235 if (gimple_simplified_result_is_gimple_val (&res_op
))
6236 res
= res_op
.ops
[0];
6237 else if (mprts_hook
)
6238 res
= mprts_hook (&res_op
);
6241 if (dump_file
&& dump_flags
& TDF_DETAILS
)
6243 fprintf (dump_file
, "Match-and-simplified ");
6244 print_gimple_expr (dump_file
, stmt
, 0, TDF_SLIM
);
6245 fprintf (dump_file
, " to ");
6246 print_generic_expr (dump_file
, res
);
6247 fprintf (dump_file
, "\n");
6253 location_t loc
= gimple_location (stmt
);
6254 switch (gimple_code (stmt
))
6258 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
6260 switch (get_gimple_rhs_class (subcode
))
6262 case GIMPLE_SINGLE_RHS
:
6264 tree rhs
= gimple_assign_rhs1 (stmt
);
6265 enum tree_code_class kind
= TREE_CODE_CLASS (subcode
);
6267 if (TREE_CODE (rhs
) == SSA_NAME
)
6269 /* If the RHS is an SSA_NAME, return its known constant value,
6271 return (*valueize
) (rhs
);
6273 /* Handle propagating invariant addresses into address
6275 else if (TREE_CODE (rhs
) == ADDR_EXPR
6276 && !is_gimple_min_invariant (rhs
))
6278 poly_int64 offset
= 0;
6280 base
= get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs
, 0),
6284 && (CONSTANT_CLASS_P (base
)
6285 || decl_address_invariant_p (base
)))
6286 return build_invariant_address (TREE_TYPE (rhs
),
6289 else if (TREE_CODE (rhs
) == CONSTRUCTOR
6290 && TREE_CODE (TREE_TYPE (rhs
)) == VECTOR_TYPE
6291 && known_eq (CONSTRUCTOR_NELTS (rhs
),
6292 TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs
))))
6297 nelts
= CONSTRUCTOR_NELTS (rhs
);
6298 tree_vector_builder
vec (TREE_TYPE (rhs
), nelts
, 1);
6299 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs
), i
, val
)
6301 val
= (*valueize
) (val
);
6302 if (TREE_CODE (val
) == INTEGER_CST
6303 || TREE_CODE (val
) == REAL_CST
6304 || TREE_CODE (val
) == FIXED_CST
)
6305 vec
.quick_push (val
);
6310 return vec
.build ();
6312 if (subcode
== OBJ_TYPE_REF
)
6314 tree val
= (*valueize
) (OBJ_TYPE_REF_EXPR (rhs
));
6315 /* If callee is constant, we can fold away the wrapper. */
6316 if (is_gimple_min_invariant (val
))
6320 if (kind
== tcc_reference
)
6322 if ((TREE_CODE (rhs
) == VIEW_CONVERT_EXPR
6323 || TREE_CODE (rhs
) == REALPART_EXPR
6324 || TREE_CODE (rhs
) == IMAGPART_EXPR
)
6325 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
6327 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
6328 return fold_unary_loc (EXPR_LOCATION (rhs
),
6330 TREE_TYPE (rhs
), val
);
6332 else if (TREE_CODE (rhs
) == BIT_FIELD_REF
6333 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
6335 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
6336 return fold_ternary_loc (EXPR_LOCATION (rhs
),
6338 TREE_TYPE (rhs
), val
,
6339 TREE_OPERAND (rhs
, 1),
6340 TREE_OPERAND (rhs
, 2));
6342 else if (TREE_CODE (rhs
) == MEM_REF
6343 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
6345 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
6346 if (TREE_CODE (val
) == ADDR_EXPR
6347 && is_gimple_min_invariant (val
))
6349 tree tem
= fold_build2 (MEM_REF
, TREE_TYPE (rhs
),
6351 TREE_OPERAND (rhs
, 1));
6356 return fold_const_aggregate_ref_1 (rhs
, valueize
);
6358 else if (kind
== tcc_declaration
)
6359 return get_symbol_constant_value (rhs
);
6363 case GIMPLE_UNARY_RHS
:
6366 case GIMPLE_BINARY_RHS
:
6367 /* Translate &x + CST into an invariant form suitable for
6368 further propagation. */
6369 if (subcode
== POINTER_PLUS_EXPR
)
6371 tree op0
= (*valueize
) (gimple_assign_rhs1 (stmt
));
6372 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
6373 if (TREE_CODE (op0
) == ADDR_EXPR
6374 && TREE_CODE (op1
) == INTEGER_CST
)
6376 tree off
= fold_convert (ptr_type_node
, op1
);
6377 return build_fold_addr_expr_loc
6379 fold_build2 (MEM_REF
,
6380 TREE_TYPE (TREE_TYPE (op0
)),
6381 unshare_expr (op0
), off
));
6384 /* Canonicalize bool != 0 and bool == 0 appearing after
6385 valueization. While gimple_simplify handles this
6386 it can get confused by the ~X == 1 -> X == 0 transform
6387 which we cant reduce to a SSA name or a constant
6388 (and we have no way to tell gimple_simplify to not
6389 consider those transforms in the first place). */
6390 else if (subcode
== EQ_EXPR
6391 || subcode
== NE_EXPR
)
6393 tree lhs
= gimple_assign_lhs (stmt
);
6394 tree op0
= gimple_assign_rhs1 (stmt
);
6395 if (useless_type_conversion_p (TREE_TYPE (lhs
),
6398 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
6399 op0
= (*valueize
) (op0
);
6400 if (TREE_CODE (op0
) == INTEGER_CST
)
6401 std::swap (op0
, op1
);
6402 if (TREE_CODE (op1
) == INTEGER_CST
6403 && ((subcode
== NE_EXPR
&& integer_zerop (op1
))
6404 || (subcode
== EQ_EXPR
&& integer_onep (op1
))))
6410 case GIMPLE_TERNARY_RHS
:
6412 /* Handle ternary operators that can appear in GIMPLE form. */
6413 tree op0
= (*valueize
) (gimple_assign_rhs1 (stmt
));
6414 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
6415 tree op2
= (*valueize
) (gimple_assign_rhs3 (stmt
));
6416 return fold_ternary_loc (loc
, subcode
,
6417 gimple_expr_type (stmt
), op0
, op1
, op2
);
6428 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
6430 if (gimple_call_internal_p (stmt
))
6432 enum tree_code subcode
= ERROR_MARK
;
6433 switch (gimple_call_internal_fn (stmt
))
6435 case IFN_UBSAN_CHECK_ADD
:
6436 subcode
= PLUS_EXPR
;
6438 case IFN_UBSAN_CHECK_SUB
:
6439 subcode
= MINUS_EXPR
;
6441 case IFN_UBSAN_CHECK_MUL
:
6442 subcode
= MULT_EXPR
;
6444 case IFN_BUILTIN_EXPECT
:
6446 tree arg0
= gimple_call_arg (stmt
, 0);
6447 tree op0
= (*valueize
) (arg0
);
6448 if (TREE_CODE (op0
) == INTEGER_CST
)
6455 tree arg0
= gimple_call_arg (stmt
, 0);
6456 tree arg1
= gimple_call_arg (stmt
, 1);
6457 tree op0
= (*valueize
) (arg0
);
6458 tree op1
= (*valueize
) (arg1
);
6460 if (TREE_CODE (op0
) != INTEGER_CST
6461 || TREE_CODE (op1
) != INTEGER_CST
)
6466 /* x * 0 = 0 * x = 0 without overflow. */
6467 if (integer_zerop (op0
) || integer_zerop (op1
))
6468 return build_zero_cst (TREE_TYPE (arg0
));
6471 /* y - y = 0 without overflow. */
6472 if (operand_equal_p (op0
, op1
, 0))
6473 return build_zero_cst (TREE_TYPE (arg0
));
6480 = fold_binary_loc (loc
, subcode
, TREE_TYPE (arg0
), op0
, op1
);
6482 && TREE_CODE (res
) == INTEGER_CST
6483 && !TREE_OVERFLOW (res
))
6488 fn
= (*valueize
) (gimple_call_fn (stmt
));
6489 if (TREE_CODE (fn
) == ADDR_EXPR
6490 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
6491 && fndecl_built_in_p (TREE_OPERAND (fn
, 0))
6492 && gimple_builtin_call_types_compatible_p (stmt
,
6493 TREE_OPERAND (fn
, 0)))
6495 tree
*args
= XALLOCAVEC (tree
, gimple_call_num_args (stmt
));
6498 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
6499 args
[i
] = (*valueize
) (gimple_call_arg (stmt
, i
));
6500 retval
= fold_builtin_call_array (loc
,
6501 gimple_call_return_type (call_stmt
),
6502 fn
, gimple_call_num_args (stmt
), args
);
6505 /* fold_call_expr wraps the result inside a NOP_EXPR. */
6506 STRIP_NOPS (retval
);
6507 retval
= fold_convert (gimple_call_return_type (call_stmt
),
6520 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
6521 Returns NULL_TREE if folding to a constant is not possible, otherwise
6522 returns a constant according to is_gimple_min_invariant. */
6525 gimple_fold_stmt_to_constant (gimple
*stmt
, tree (*valueize
) (tree
))
6527 tree res
= gimple_fold_stmt_to_constant_1 (stmt
, valueize
);
6528 if (res
&& is_gimple_min_invariant (res
))
6534 /* The following set of functions are supposed to fold references using
6535 their constant initializers. */
6537 /* See if we can find constructor defining value of BASE.
6538 When we know the consructor with constant offset (such as
6539 base is array[40] and we do know constructor of array), then
6540 BIT_OFFSET is adjusted accordingly.
6542 As a special case, return error_mark_node when constructor
6543 is not explicitly available, but it is known to be zero
6544 such as 'static const int a;'. */
6546 get_base_constructor (tree base
, poly_int64_pod
*bit_offset
,
6547 tree (*valueize
)(tree
))
6549 poly_int64 bit_offset2
, size
, max_size
;
6552 if (TREE_CODE (base
) == MEM_REF
)
6554 poly_offset_int boff
= *bit_offset
+ mem_ref_offset (base
) * BITS_PER_UNIT
;
6555 if (!boff
.to_shwi (bit_offset
))
6559 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
6560 base
= valueize (TREE_OPERAND (base
, 0));
6561 if (!base
|| TREE_CODE (base
) != ADDR_EXPR
)
6563 base
= TREE_OPERAND (base
, 0);
6566 && TREE_CODE (base
) == SSA_NAME
)
6567 base
= valueize (base
);
6569 /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
6570 DECL_INITIAL. If BASE is a nested reference into another
6571 ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
6572 the inner reference. */
6573 switch (TREE_CODE (base
))
6578 tree init
= ctor_for_folding (base
);
6580 /* Our semantic is exact opposite of ctor_for_folding;
6581 NULL means unknown, while error_mark_node is 0. */
6582 if (init
== error_mark_node
)
6585 return error_mark_node
;
6589 case VIEW_CONVERT_EXPR
:
6590 return get_base_constructor (TREE_OPERAND (base
, 0),
6591 bit_offset
, valueize
);
6595 base
= get_ref_base_and_extent (base
, &bit_offset2
, &size
, &max_size
,
6597 if (!known_size_p (max_size
) || maybe_ne (size
, max_size
))
6599 *bit_offset
+= bit_offset2
;
6600 return get_base_constructor (base
, bit_offset
, valueize
);
6606 if (CONSTANT_CLASS_P (base
))
6613 /* CTOR is CONSTRUCTOR of an array type. Fold a reference of SIZE bits
6614 to the memory at bit OFFSET. When non-null, TYPE is the expected
6615 type of the reference; otherwise the type of the referenced element
6616 is used instead. When SIZE is zero, attempt to fold a reference to
6617 the entire element which OFFSET refers to. Increment *SUBOFF by
6618 the bit offset of the accessed element. */
6621 fold_array_ctor_reference (tree type
, tree ctor
,
6622 unsigned HOST_WIDE_INT offset
,
6623 unsigned HOST_WIDE_INT size
,
6625 unsigned HOST_WIDE_INT
*suboff
)
6627 offset_int low_bound
;
6628 offset_int elt_size
;
6629 offset_int access_index
;
6630 tree domain_type
= NULL_TREE
;
6631 HOST_WIDE_INT inner_offset
;
6633 /* Compute low bound and elt size. */
6634 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
)
6635 domain_type
= TYPE_DOMAIN (TREE_TYPE (ctor
));
6636 if (domain_type
&& TYPE_MIN_VALUE (domain_type
))
6638 /* Static constructors for variably sized objects make no sense. */
6639 if (TREE_CODE (TYPE_MIN_VALUE (domain_type
)) != INTEGER_CST
)
6641 low_bound
= wi::to_offset (TYPE_MIN_VALUE (domain_type
));
6645 /* Static constructors for variably sized objects make no sense. */
6646 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor
)))) != INTEGER_CST
)
6648 elt_size
= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor
))));
6650 /* When TYPE is non-null, verify that it specifies a constant-sized
6651 access of a multiple of the array element size. Avoid division
6652 by zero below when ELT_SIZE is zero, such as with the result of
6653 an initializer for a zero-length array or an empty struct. */
6656 && (!TYPE_SIZE_UNIT (type
)
6657 || TREE_CODE (TYPE_SIZE_UNIT (type
)) != INTEGER_CST
)))
6660 /* Compute the array index we look for. */
6661 access_index
= wi::udiv_trunc (offset_int (offset
/ BITS_PER_UNIT
),
6663 access_index
+= low_bound
;
6665 /* And offset within the access. */
6666 inner_offset
= offset
% (elt_size
.to_uhwi () * BITS_PER_UNIT
);
6668 if (size
> elt_size
.to_uhwi () * BITS_PER_UNIT
)
6670 /* native_encode_expr constraints. */
6671 if (size
> MAX_BITSIZE_MODE_ANY_MODE
6672 || size
% BITS_PER_UNIT
!= 0
6673 || inner_offset
% BITS_PER_UNIT
!= 0)
6677 tree val
= get_array_ctor_element_at_index (ctor
, access_index
,
6679 if (!val
&& ctor_idx
>= CONSTRUCTOR_NELTS (ctor
))
6680 return build_zero_cst (type
);
6682 /* native-encode adjacent ctor elements. */
6683 unsigned char buf
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
6684 unsigned bufoff
= 0;
6685 offset_int index
= 0;
6686 offset_int max_index
= access_index
;
6687 constructor_elt
*elt
= CONSTRUCTOR_ELT (ctor
, ctor_idx
);
6689 val
= build_zero_cst (TREE_TYPE (TREE_TYPE (ctor
)));
6690 else if (!CONSTANT_CLASS_P (val
))
6694 else if (TREE_CODE (elt
->index
) == RANGE_EXPR
)
6696 index
= wi::to_offset (TREE_OPERAND (elt
->index
, 0));
6697 max_index
= wi::to_offset (TREE_OPERAND (elt
->index
, 1));
6700 index
= max_index
= wi::to_offset (elt
->index
);
6701 index
= wi::umax (index
, access_index
);
6704 int len
= native_encode_expr (val
, buf
+ bufoff
,
6705 elt_size
.to_uhwi (),
6706 inner_offset
/ BITS_PER_UNIT
);
6707 if (len
!= elt_size
- inner_offset
/ BITS_PER_UNIT
)
6713 if (wi::cmpu (access_index
, index
) == 0)
6715 else if (wi::cmpu (access_index
, max_index
) > 0)
6718 if (ctor_idx
>= CONSTRUCTOR_NELTS (ctor
))
6720 val
= build_zero_cst (TREE_TYPE (TREE_TYPE (ctor
)));
6725 elt
= CONSTRUCTOR_ELT (ctor
, ctor_idx
);
6727 max_index
= access_index
;
6730 else if (TREE_CODE (elt
->index
) == RANGE_EXPR
)
6732 index
= wi::to_offset (TREE_OPERAND (elt
->index
, 0));
6733 max_index
= wi::to_offset (TREE_OPERAND (elt
->index
, 1));
6736 index
= max_index
= wi::to_offset (elt
->index
);
6737 index
= wi::umax (index
, access_index
);
6738 if (wi::cmpu (access_index
, index
) == 0)
6741 val
= build_zero_cst (TREE_TYPE (TREE_TYPE (ctor
)));
6745 while (bufoff
< size
/ BITS_PER_UNIT
);
6747 return native_interpret_expr (type
, buf
, size
/ BITS_PER_UNIT
);
6750 if (tree val
= get_array_ctor_element_at_index (ctor
, access_index
))
6752 if (!size
&& TREE_CODE (val
) != CONSTRUCTOR
)
6754 /* For the final reference to the entire accessed element
6755 (SIZE is zero), reset INNER_OFFSET, disegard TYPE (which
6756 may be null) in favor of the type of the element, and set
6757 SIZE to the size of the accessed element. */
6759 type
= TREE_TYPE (val
);
6760 size
= elt_size
.to_uhwi () * BITS_PER_UNIT
;
6763 *suboff
+= (access_index
* elt_size
* BITS_PER_UNIT
).to_uhwi ();
6764 return fold_ctor_reference (type
, val
, inner_offset
, size
, from_decl
,
6768 /* Memory not explicitly mentioned in constructor is 0 (or
6769 the reference is out of range). */
6770 return type
? build_zero_cst (type
) : NULL_TREE
;
6773 /* CTOR is CONSTRUCTOR of an aggregate or vector. Fold a reference
6774 of SIZE bits to the memory at bit OFFSET. When non-null, TYPE
6775 is the expected type of the reference; otherwise the type of
6776 the referenced member is used instead. When SIZE is zero,
6777 attempt to fold a reference to the entire member which OFFSET
6778 refers to; in this case. Increment *SUBOFF by the bit offset
6779 of the accessed member. */
6782 fold_nonarray_ctor_reference (tree type
, tree ctor
,
6783 unsigned HOST_WIDE_INT offset
,
6784 unsigned HOST_WIDE_INT size
,
6786 unsigned HOST_WIDE_INT
*suboff
)
6788 unsigned HOST_WIDE_INT cnt
;
6791 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
,
6794 tree byte_offset
= DECL_FIELD_OFFSET (cfield
);
6795 tree field_offset
= DECL_FIELD_BIT_OFFSET (cfield
);
6796 tree field_size
= DECL_SIZE (cfield
);
6800 /* Determine the size of the flexible array member from
6801 the size of the initializer provided for it. */
6802 field_size
= TYPE_SIZE (TREE_TYPE (cval
));
6805 /* Variable sized objects in static constructors makes no sense,
6806 but field_size can be NULL for flexible array members. */
6807 gcc_assert (TREE_CODE (field_offset
) == INTEGER_CST
6808 && TREE_CODE (byte_offset
) == INTEGER_CST
6809 && (field_size
!= NULL_TREE
6810 ? TREE_CODE (field_size
) == INTEGER_CST
6811 : TREE_CODE (TREE_TYPE (cfield
)) == ARRAY_TYPE
));
6813 /* Compute bit offset of the field. */
6814 offset_int bitoffset
6815 = (wi::to_offset (field_offset
)
6816 + (wi::to_offset (byte_offset
) << LOG2_BITS_PER_UNIT
));
6817 /* Compute bit offset where the field ends. */
6818 offset_int bitoffset_end
;
6819 if (field_size
!= NULL_TREE
)
6820 bitoffset_end
= bitoffset
+ wi::to_offset (field_size
);
6824 /* Compute the bit offset of the end of the desired access.
6825 As a special case, if the size of the desired access is
6826 zero, assume the access is to the entire field (and let
6827 the caller make any necessary adjustments by storing
6828 the actual bounds of the field in FIELDBOUNDS). */
6829 offset_int access_end
= offset_int (offset
);
6833 access_end
= bitoffset_end
;
6835 /* Is there any overlap between the desired access at
6836 [OFFSET, OFFSET+SIZE) and the offset of the field within
6837 the object at [BITOFFSET, BITOFFSET_END)? */
6838 if (wi::cmps (access_end
, bitoffset
) > 0
6839 && (field_size
== NULL_TREE
6840 || wi::lts_p (offset
, bitoffset_end
)))
6842 *suboff
+= bitoffset
.to_uhwi ();
6844 if (!size
&& TREE_CODE (cval
) != CONSTRUCTOR
)
6846 /* For the final reference to the entire accessed member
6847 (SIZE is zero), reset OFFSET, disegard TYPE (which may
6848 be null) in favor of the type of the member, and set
6849 SIZE to the size of the accessed member. */
6850 offset
= bitoffset
.to_uhwi ();
6851 type
= TREE_TYPE (cval
);
6852 size
= (bitoffset_end
- bitoffset
).to_uhwi ();
6855 /* We do have overlap. Now see if the field is large enough
6856 to cover the access. Give up for accesses that extend
6857 beyond the end of the object or that span multiple fields. */
6858 if (wi::cmps (access_end
, bitoffset_end
) > 0)
6860 if (offset
< bitoffset
)
6863 offset_int inner_offset
= offset_int (offset
) - bitoffset
;
6864 return fold_ctor_reference (type
, cval
,
6865 inner_offset
.to_uhwi (), size
,
6873 return build_zero_cst (type
);
6876 /* CTOR is value initializing memory. Fold a reference of TYPE and
6877 bit size POLY_SIZE to the memory at bit POLY_OFFSET. When POLY_SIZE
6878 is zero, attempt to fold a reference to the entire subobject
6879 which OFFSET refers to. This is used when folding accesses to
6880 string members of aggregates. When non-null, set *SUBOFF to
6881 the bit offset of the accessed subobject. */
6884 fold_ctor_reference (tree type
, tree ctor
, const poly_uint64
&poly_offset
,
6885 const poly_uint64
&poly_size
, tree from_decl
,
6886 unsigned HOST_WIDE_INT
*suboff
/* = NULL */)
6890 /* We found the field with exact match. */
6892 && useless_type_conversion_p (type
, TREE_TYPE (ctor
))
6893 && known_eq (poly_offset
, 0U))
6894 return canonicalize_constructor_val (unshare_expr (ctor
), from_decl
);
6896 /* The remaining optimizations need a constant size and offset. */
6897 unsigned HOST_WIDE_INT size
, offset
;
6898 if (!poly_size
.is_constant (&size
) || !poly_offset
.is_constant (&offset
))
6901 /* We are at the end of walk, see if we can view convert the
6903 if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor
)) && !offset
6904 /* VIEW_CONVERT_EXPR is defined only for matching sizes. */
6905 && !compare_tree_int (TYPE_SIZE (type
), size
)
6906 && !compare_tree_int (TYPE_SIZE (TREE_TYPE (ctor
)), size
))
6908 ret
= canonicalize_constructor_val (unshare_expr (ctor
), from_decl
);
6911 ret
= fold_unary (VIEW_CONVERT_EXPR
, type
, ret
);
6913 STRIP_USELESS_TYPE_CONVERSION (ret
);
6917 /* For constants and byte-aligned/sized reads try to go through
6918 native_encode/interpret. */
6919 if (CONSTANT_CLASS_P (ctor
)
6920 && BITS_PER_UNIT
== 8
6921 && offset
% BITS_PER_UNIT
== 0
6922 && offset
/ BITS_PER_UNIT
<= INT_MAX
6923 && size
% BITS_PER_UNIT
== 0
6924 && size
<= MAX_BITSIZE_MODE_ANY_MODE
6925 && can_native_interpret_type_p (type
))
6927 unsigned char buf
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
6928 int len
= native_encode_expr (ctor
, buf
, size
/ BITS_PER_UNIT
,
6929 offset
/ BITS_PER_UNIT
);
6931 return native_interpret_expr (type
, buf
, len
);
6933 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
6935 unsigned HOST_WIDE_INT dummy
= 0;
6940 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
6941 || TREE_CODE (TREE_TYPE (ctor
)) == VECTOR_TYPE
)
6942 ret
= fold_array_ctor_reference (type
, ctor
, offset
, size
,
6945 ret
= fold_nonarray_ctor_reference (type
, ctor
, offset
, size
,
6948 /* Fall back to native_encode_initializer. Needs to be done
6949 only in the outermost fold_ctor_reference call (because it itself
6950 recurses into CONSTRUCTORs) and doesn't update suboff. */
6951 if (ret
== NULL_TREE
6953 && BITS_PER_UNIT
== 8
6954 && offset
% BITS_PER_UNIT
== 0
6955 && offset
/ BITS_PER_UNIT
<= INT_MAX
6956 && size
% BITS_PER_UNIT
== 0
6957 && size
<= MAX_BITSIZE_MODE_ANY_MODE
6958 && can_native_interpret_type_p (type
))
6960 unsigned char buf
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
6961 int len
= native_encode_initializer (ctor
, buf
, size
/ BITS_PER_UNIT
,
6962 offset
/ BITS_PER_UNIT
);
6964 return native_interpret_expr (type
, buf
, len
);
6973 /* Return the tree representing the element referenced by T if T is an
6974 ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
6975 names using VALUEIZE. Return NULL_TREE otherwise. */
6978 fold_const_aggregate_ref_1 (tree t
, tree (*valueize
) (tree
))
6980 tree ctor
, idx
, base
;
6981 poly_int64 offset
, size
, max_size
;
6985 if (TREE_THIS_VOLATILE (t
))
6989 return get_symbol_constant_value (t
);
6991 tem
= fold_read_from_constant_string (t
);
6995 switch (TREE_CODE (t
))
6998 case ARRAY_RANGE_REF
:
6999 /* Constant indexes are handled well by get_base_constructor.
7000 Only special case variable offsets.
7001 FIXME: This code can't handle nested references with variable indexes
7002 (they will be handled only by iteration of ccp). Perhaps we can bring
7003 get_ref_base_and_extent here and make it use a valueize callback. */
7004 if (TREE_CODE (TREE_OPERAND (t
, 1)) == SSA_NAME
7006 && (idx
= (*valueize
) (TREE_OPERAND (t
, 1)))
7007 && poly_int_tree_p (idx
))
7009 tree low_bound
, unit_size
;
7011 /* If the resulting bit-offset is constant, track it. */
7012 if ((low_bound
= array_ref_low_bound (t
),
7013 poly_int_tree_p (low_bound
))
7014 && (unit_size
= array_ref_element_size (t
),
7015 tree_fits_uhwi_p (unit_size
)))
7017 poly_offset_int woffset
7018 = wi::sext (wi::to_poly_offset (idx
)
7019 - wi::to_poly_offset (low_bound
),
7020 TYPE_PRECISION (TREE_TYPE (idx
)));
7021 woffset
*= tree_to_uhwi (unit_size
);
7022 woffset
*= BITS_PER_UNIT
;
7023 if (woffset
.to_shwi (&offset
))
7025 base
= TREE_OPERAND (t
, 0);
7026 ctor
= get_base_constructor (base
, &offset
, valueize
);
7027 /* Empty constructor. Always fold to 0. */
7028 if (ctor
== error_mark_node
)
7029 return build_zero_cst (TREE_TYPE (t
));
7030 /* Out of bound array access. Value is undefined,
7032 if (maybe_lt (offset
, 0))
7034 /* We cannot determine ctor. */
7037 return fold_ctor_reference (TREE_TYPE (t
), ctor
, offset
,
7038 tree_to_uhwi (unit_size
)
7048 case TARGET_MEM_REF
:
7050 base
= get_ref_base_and_extent (t
, &offset
, &size
, &max_size
, &reverse
);
7051 ctor
= get_base_constructor (base
, &offset
, valueize
);
7053 /* Empty constructor. Always fold to 0. */
7054 if (ctor
== error_mark_node
)
7055 return build_zero_cst (TREE_TYPE (t
));
7056 /* We do not know precise address. */
7057 if (!known_size_p (max_size
) || maybe_ne (max_size
, size
))
7059 /* We cannot determine ctor. */
7063 /* Out of bound array access. Value is undefined, but don't fold. */
7064 if (maybe_lt (offset
, 0))
7067 return fold_ctor_reference (TREE_TYPE (t
), ctor
, offset
, size
,
7073 tree c
= fold_const_aggregate_ref_1 (TREE_OPERAND (t
, 0), valueize
);
7074 if (c
&& TREE_CODE (c
) == COMPLEX_CST
)
7075 return fold_build1_loc (EXPR_LOCATION (t
),
7076 TREE_CODE (t
), TREE_TYPE (t
), c
);
7088 fold_const_aggregate_ref (tree t
)
7090 return fold_const_aggregate_ref_1 (t
, NULL
);
7093 /* Lookup virtual method with index TOKEN in a virtual table V
7095 Set CAN_REFER if non-NULL to false if method
7096 is not referable or if the virtual table is ill-formed (such as rewriten
7097 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
7100 gimple_get_virt_method_for_vtable (HOST_WIDE_INT token
,
7102 unsigned HOST_WIDE_INT offset
,
7105 tree vtable
= v
, init
, fn
;
7106 unsigned HOST_WIDE_INT size
;
7107 unsigned HOST_WIDE_INT elt_size
, access_index
;
7113 /* First of all double check we have virtual table. */
7114 if (!VAR_P (v
) || !DECL_VIRTUAL_P (v
))
7116 /* Pass down that we lost track of the target. */
7122 init
= ctor_for_folding (v
);
7124 /* The virtual tables should always be born with constructors
7125 and we always should assume that they are avaialble for
7126 folding. At the moment we do not stream them in all cases,
7127 but it should never happen that ctor seem unreachable. */
7129 if (init
== error_mark_node
)
7131 /* Pass down that we lost track of the target. */
7136 gcc_checking_assert (TREE_CODE (TREE_TYPE (v
)) == ARRAY_TYPE
);
7137 size
= tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v
))));
7138 offset
*= BITS_PER_UNIT
;
7139 offset
+= token
* size
;
7141 /* Lookup the value in the constructor that is assumed to be array.
7142 This is equivalent to
7143 fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
7144 offset, size, NULL);
7145 but in a constant time. We expect that frontend produced a simple
7146 array without indexed initializers. */
7148 gcc_checking_assert (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
);
7149 domain_type
= TYPE_DOMAIN (TREE_TYPE (init
));
7150 gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type
)));
7151 elt_size
= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init
))));
7153 access_index
= offset
/ BITS_PER_UNIT
/ elt_size
;
7154 gcc_checking_assert (offset
% (elt_size
* BITS_PER_UNIT
) == 0);
7156 /* The C++ FE can now produce indexed fields, and we check if the indexes
7158 if (access_index
< CONSTRUCTOR_NELTS (init
))
7160 fn
= CONSTRUCTOR_ELT (init
, access_index
)->value
;
7161 tree idx
= CONSTRUCTOR_ELT (init
, access_index
)->index
;
7162 gcc_checking_assert (!idx
|| tree_to_uhwi (idx
) == access_index
);
7168 /* For type inconsistent program we may end up looking up virtual method
7169 in virtual table that does not contain TOKEN entries. We may overrun
7170 the virtual table and pick up a constant or RTTI info pointer.
7171 In any case the call is undefined. */
7173 || (TREE_CODE (fn
) != ADDR_EXPR
&& TREE_CODE (fn
) != FDESC_EXPR
)
7174 || TREE_CODE (TREE_OPERAND (fn
, 0)) != FUNCTION_DECL
)
7175 fn
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
7178 fn
= TREE_OPERAND (fn
, 0);
7180 /* When cgraph node is missing and function is not public, we cannot
7181 devirtualize. This can happen in WHOPR when the actual method
7182 ends up in other partition, because we found devirtualization
7183 possibility too late. */
7184 if (!can_refer_decl_in_current_unit_p (fn
, vtable
))
7195 /* Make sure we create a cgraph node for functions we'll reference.
7196 They can be non-existent if the reference comes from an entry
7197 of an external vtable for example. */
7198 cgraph_node::get_create (fn
);
7203 /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
7204 is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
7205 KNOWN_BINFO carries the binfo describing the true type of
7206 OBJ_TYPE_REF_OBJECT(REF).
7207 Set CAN_REFER if non-NULL to false if method
7208 is not referable or if the virtual table is ill-formed (such as rewriten
7209 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
7212 gimple_get_virt_method_for_binfo (HOST_WIDE_INT token
, tree known_binfo
,
7215 unsigned HOST_WIDE_INT offset
;
7218 v
= BINFO_VTABLE (known_binfo
);
7219 /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */
7223 if (!vtable_pointer_value_to_vtable (v
, &v
, &offset
))
7229 return gimple_get_virt_method_for_vtable (token
, v
, offset
, can_refer
);
7232 /* Given a pointer value T, return a simplified version of an
7233 indirection through T, or NULL_TREE if no simplification is
7234 possible. Note that the resulting type may be different from
7235 the type pointed to in the sense that it is still compatible
7236 from the langhooks point of view. */
7239 gimple_fold_indirect_ref (tree t
)
7241 tree ptype
= TREE_TYPE (t
), type
= TREE_TYPE (ptype
);
7246 subtype
= TREE_TYPE (sub
);
7247 if (!POINTER_TYPE_P (subtype
)
7248 || TYPE_REF_CAN_ALIAS_ALL (ptype
))
7251 if (TREE_CODE (sub
) == ADDR_EXPR
)
7253 tree op
= TREE_OPERAND (sub
, 0);
7254 tree optype
= TREE_TYPE (op
);
7256 if (useless_type_conversion_p (type
, optype
))
7259 /* *(foo *)&fooarray => fooarray[0] */
7260 if (TREE_CODE (optype
) == ARRAY_TYPE
7261 && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype
))) == INTEGER_CST
7262 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
7264 tree type_domain
= TYPE_DOMAIN (optype
);
7265 tree min_val
= size_zero_node
;
7266 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
7267 min_val
= TYPE_MIN_VALUE (type_domain
);
7268 if (TREE_CODE (min_val
) == INTEGER_CST
)
7269 return build4 (ARRAY_REF
, type
, op
, min_val
, NULL_TREE
, NULL_TREE
);
7271 /* *(foo *)&complexfoo => __real__ complexfoo */
7272 else if (TREE_CODE (optype
) == COMPLEX_TYPE
7273 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
7274 return fold_build1 (REALPART_EXPR
, type
, op
);
7275 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
7276 else if (TREE_CODE (optype
) == VECTOR_TYPE
7277 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
7279 tree part_width
= TYPE_SIZE (type
);
7280 tree index
= bitsize_int (0);
7281 return fold_build3 (BIT_FIELD_REF
, type
, op
, part_width
, index
);
7285 /* *(p + CST) -> ... */
7286 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
7287 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
7289 tree addr
= TREE_OPERAND (sub
, 0);
7290 tree off
= TREE_OPERAND (sub
, 1);
7294 addrtype
= TREE_TYPE (addr
);
7296 /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
7297 if (TREE_CODE (addr
) == ADDR_EXPR
7298 && TREE_CODE (TREE_TYPE (addrtype
)) == VECTOR_TYPE
7299 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (addrtype
)))
7300 && tree_fits_uhwi_p (off
))
7302 unsigned HOST_WIDE_INT offset
= tree_to_uhwi (off
);
7303 tree part_width
= TYPE_SIZE (type
);
7304 unsigned HOST_WIDE_INT part_widthi
7305 = tree_to_shwi (part_width
) / BITS_PER_UNIT
;
7306 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
7307 tree index
= bitsize_int (indexi
);
7308 if (known_lt (offset
/ part_widthi
,
7309 TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype
))))
7310 return fold_build3 (BIT_FIELD_REF
, type
, TREE_OPERAND (addr
, 0),
7314 /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
7315 if (TREE_CODE (addr
) == ADDR_EXPR
7316 && TREE_CODE (TREE_TYPE (addrtype
)) == COMPLEX_TYPE
7317 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (addrtype
))))
7319 tree size
= TYPE_SIZE_UNIT (type
);
7320 if (tree_int_cst_equal (size
, off
))
7321 return fold_build1 (IMAGPART_EXPR
, type
, TREE_OPERAND (addr
, 0));
7324 /* *(p + CST) -> MEM_REF <p, CST>. */
7325 if (TREE_CODE (addr
) != ADDR_EXPR
7326 || DECL_P (TREE_OPERAND (addr
, 0)))
7327 return fold_build2 (MEM_REF
, type
,
7329 wide_int_to_tree (ptype
, wi::to_wide (off
)));
7332 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
7333 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
7334 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype
)))) == INTEGER_CST
7335 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (subtype
))))
7338 tree min_val
= size_zero_node
;
7340 sub
= gimple_fold_indirect_ref (sub
);
7342 sub
= build1 (INDIRECT_REF
, TREE_TYPE (subtype
), osub
);
7343 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
7344 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
7345 min_val
= TYPE_MIN_VALUE (type_domain
);
7346 if (TREE_CODE (min_val
) == INTEGER_CST
)
7347 return build4 (ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
, NULL_TREE
);
7353 /* Return true if CODE is an operation that when operating on signed
7354 integer types involves undefined behavior on overflow and the
7355 operation can be expressed with unsigned arithmetic. */
7358 arith_code_with_undefined_signed_overflow (tree_code code
)
7367 case POINTER_PLUS_EXPR
:
7374 /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic
7375 operation that can be transformed to unsigned arithmetic by converting
7376 its operand, carrying out the operation in the corresponding unsigned
7377 type and converting the result back to the original type.
7379 Returns a sequence of statements that replace STMT and also contain
7380 a modified form of STMT itself. */
7383 rewrite_to_defined_overflow (gimple
*stmt
)
7385 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7387 fprintf (dump_file
, "rewriting stmt with undefined signed "
7389 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
7392 tree lhs
= gimple_assign_lhs (stmt
);
7393 tree type
= unsigned_type_for (TREE_TYPE (lhs
));
7394 gimple_seq stmts
= NULL
;
7395 if (gimple_assign_rhs_code (stmt
) == ABS_EXPR
)
7396 gimple_assign_set_rhs_code (stmt
, ABSU_EXPR
);
7398 for (unsigned i
= 1; i
< gimple_num_ops (stmt
); ++i
)
7400 tree op
= gimple_op (stmt
, i
);
7401 op
= gimple_convert (&stmts
, type
, op
);
7402 gimple_set_op (stmt
, i
, op
);
7404 gimple_assign_set_lhs (stmt
, make_ssa_name (type
, stmt
));
7405 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
7406 gimple_assign_set_rhs_code (stmt
, PLUS_EXPR
);
7407 gimple_set_modified (stmt
, true);
7408 gimple_seq_add_stmt (&stmts
, stmt
);
7409 gimple
*cvt
= gimple_build_assign (lhs
, NOP_EXPR
, gimple_assign_lhs (stmt
));
7410 gimple_seq_add_stmt (&stmts
, cvt
);
7416 /* The valueization hook we use for the gimple_build API simplification.
7417 This makes us match fold_buildN behavior by only combining with
7418 statements in the sequence(s) we are currently building. */
7421 gimple_build_valueize (tree op
)
7423 if (gimple_bb (SSA_NAME_DEF_STMT (op
)) == NULL
)
7428 /* Build the expression CODE OP0 of type TYPE with location LOC,
7429 simplifying it first if possible. Returns the built
7430 expression value and appends statements possibly defining it
7434 gimple_build (gimple_seq
*seq
, location_t loc
,
7435 enum tree_code code
, tree type
, tree op0
)
7437 tree res
= gimple_simplify (code
, type
, op0
, seq
, gimple_build_valueize
);
7440 res
= create_tmp_reg_or_ssa_name (type
);
7442 if (code
== REALPART_EXPR
7443 || code
== IMAGPART_EXPR
7444 || code
== VIEW_CONVERT_EXPR
)
7445 stmt
= gimple_build_assign (res
, code
, build1 (code
, type
, op0
));
7447 stmt
= gimple_build_assign (res
, code
, op0
);
7448 gimple_set_location (stmt
, loc
);
7449 gimple_seq_add_stmt_without_update (seq
, stmt
);
7454 /* Build the expression OP0 CODE OP1 of type TYPE with location LOC,
7455 simplifying it first if possible. Returns the built
7456 expression value and appends statements possibly defining it
7460 gimple_build (gimple_seq
*seq
, location_t loc
,
7461 enum tree_code code
, tree type
, tree op0
, tree op1
)
7463 tree res
= gimple_simplify (code
, type
, op0
, op1
, seq
, gimple_build_valueize
);
7466 res
= create_tmp_reg_or_ssa_name (type
);
7467 gimple
*stmt
= gimple_build_assign (res
, code
, op0
, op1
);
7468 gimple_set_location (stmt
, loc
);
7469 gimple_seq_add_stmt_without_update (seq
, stmt
);
7474 /* Build the expression (CODE OP0 OP1 OP2) of type TYPE with location LOC,
7475 simplifying it first if possible. Returns the built
7476 expression value and appends statements possibly defining it
7480 gimple_build (gimple_seq
*seq
, location_t loc
,
7481 enum tree_code code
, tree type
, tree op0
, tree op1
, tree op2
)
7483 tree res
= gimple_simplify (code
, type
, op0
, op1
, op2
,
7484 seq
, gimple_build_valueize
);
7487 res
= create_tmp_reg_or_ssa_name (type
);
7489 if (code
== BIT_FIELD_REF
)
7490 stmt
= gimple_build_assign (res
, code
,
7491 build3 (code
, type
, op0
, op1
, op2
));
7493 stmt
= gimple_build_assign (res
, code
, op0
, op1
, op2
);
7494 gimple_set_location (stmt
, loc
);
7495 gimple_seq_add_stmt_without_update (seq
, stmt
);
7500 /* Build the call FN (ARG0) with a result of type TYPE
7501 (or no result if TYPE is void) with location LOC,
7502 simplifying it first if possible. Returns the built
7503 expression value (or NULL_TREE if TYPE is void) and appends
7504 statements possibly defining it to SEQ. */
7507 gimple_build (gimple_seq
*seq
, location_t loc
, combined_fn fn
,
7508 tree type
, tree arg0
)
7510 tree res
= gimple_simplify (fn
, type
, arg0
, seq
, gimple_build_valueize
);
7514 if (internal_fn_p (fn
))
7515 stmt
= gimple_build_call_internal (as_internal_fn (fn
), 1, arg0
);
7518 tree decl
= builtin_decl_implicit (as_builtin_fn (fn
));
7519 stmt
= gimple_build_call (decl
, 1, arg0
);
7521 if (!VOID_TYPE_P (type
))
7523 res
= create_tmp_reg_or_ssa_name (type
);
7524 gimple_call_set_lhs (stmt
, res
);
7526 gimple_set_location (stmt
, loc
);
7527 gimple_seq_add_stmt_without_update (seq
, stmt
);
7532 /* Build the call FN (ARG0, ARG1) with a result of type TYPE
7533 (or no result if TYPE is void) with location LOC,
7534 simplifying it first if possible. Returns the built
7535 expression value (or NULL_TREE if TYPE is void) and appends
7536 statements possibly defining it to SEQ. */
7539 gimple_build (gimple_seq
*seq
, location_t loc
, combined_fn fn
,
7540 tree type
, tree arg0
, tree arg1
)
7542 tree res
= gimple_simplify (fn
, type
, arg0
, arg1
, seq
, gimple_build_valueize
);
7546 if (internal_fn_p (fn
))
7547 stmt
= gimple_build_call_internal (as_internal_fn (fn
), 2, arg0
, arg1
);
7550 tree decl
= builtin_decl_implicit (as_builtin_fn (fn
));
7551 stmt
= gimple_build_call (decl
, 2, arg0
, arg1
);
7553 if (!VOID_TYPE_P (type
))
7555 res
= create_tmp_reg_or_ssa_name (type
);
7556 gimple_call_set_lhs (stmt
, res
);
7558 gimple_set_location (stmt
, loc
);
7559 gimple_seq_add_stmt_without_update (seq
, stmt
);
7564 /* Build the call FN (ARG0, ARG1, ARG2) with a result of type TYPE
7565 (or no result if TYPE is void) with location LOC,
7566 simplifying it first if possible. Returns the built
7567 expression value (or NULL_TREE if TYPE is void) and appends
7568 statements possibly defining it to SEQ. */
7571 gimple_build (gimple_seq
*seq
, location_t loc
, combined_fn fn
,
7572 tree type
, tree arg0
, tree arg1
, tree arg2
)
7574 tree res
= gimple_simplify (fn
, type
, arg0
, arg1
, arg2
,
7575 seq
, gimple_build_valueize
);
7579 if (internal_fn_p (fn
))
7580 stmt
= gimple_build_call_internal (as_internal_fn (fn
),
7581 3, arg0
, arg1
, arg2
);
7584 tree decl
= builtin_decl_implicit (as_builtin_fn (fn
));
7585 stmt
= gimple_build_call (decl
, 3, arg0
, arg1
, arg2
);
7587 if (!VOID_TYPE_P (type
))
7589 res
= create_tmp_reg_or_ssa_name (type
);
7590 gimple_call_set_lhs (stmt
, res
);
7592 gimple_set_location (stmt
, loc
);
7593 gimple_seq_add_stmt_without_update (seq
, stmt
);
7598 /* Build the conversion (TYPE) OP with a result of type TYPE
7599 with location LOC if such conversion is neccesary in GIMPLE,
7600 simplifying it first.
7601 Returns the built expression value and appends
7602 statements possibly defining it to SEQ. */
7605 gimple_convert (gimple_seq
*seq
, location_t loc
, tree type
, tree op
)
7607 if (useless_type_conversion_p (type
, TREE_TYPE (op
)))
7609 return gimple_build (seq
, loc
, NOP_EXPR
, type
, op
);
7612 /* Build the conversion (ptrofftype) OP with a result of a type
7613 compatible with ptrofftype with location LOC if such conversion
7614 is neccesary in GIMPLE, simplifying it first.
7615 Returns the built expression value and appends
7616 statements possibly defining it to SEQ. */
7619 gimple_convert_to_ptrofftype (gimple_seq
*seq
, location_t loc
, tree op
)
7621 if (ptrofftype_p (TREE_TYPE (op
)))
7623 return gimple_convert (seq
, loc
, sizetype
, op
);
7626 /* Build a vector of type TYPE in which each element has the value OP.
7627 Return a gimple value for the result, appending any new statements
7631 gimple_build_vector_from_val (gimple_seq
*seq
, location_t loc
, tree type
,
7634 if (!TYPE_VECTOR_SUBPARTS (type
).is_constant ()
7635 && !CONSTANT_CLASS_P (op
))
7636 return gimple_build (seq
, loc
, VEC_DUPLICATE_EXPR
, type
, op
);
7638 tree res
, vec
= build_vector_from_val (type
, op
);
7639 if (is_gimple_val (vec
))
7641 if (gimple_in_ssa_p (cfun
))
7642 res
= make_ssa_name (type
);
7644 res
= create_tmp_reg (type
);
7645 gimple
*stmt
= gimple_build_assign (res
, vec
);
7646 gimple_set_location (stmt
, loc
);
7647 gimple_seq_add_stmt_without_update (seq
, stmt
);
7651 /* Build a vector from BUILDER, handling the case in which some elements
7652 are non-constant. Return a gimple value for the result, appending any
7653 new instructions to SEQ.
7655 BUILDER must not have a stepped encoding on entry. This is because
7656 the function is not geared up to handle the arithmetic that would
7657 be needed in the variable case, and any code building a vector that
7658 is known to be constant should use BUILDER->build () directly. */
7661 gimple_build_vector (gimple_seq
*seq
, location_t loc
,
7662 tree_vector_builder
*builder
)
7664 gcc_assert (builder
->nelts_per_pattern () <= 2);
7665 unsigned int encoded_nelts
= builder
->encoded_nelts ();
7666 for (unsigned int i
= 0; i
< encoded_nelts
; ++i
)
7667 if (!TREE_CONSTANT ((*builder
)[i
]))
7669 tree type
= builder
->type ();
7670 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
).to_constant ();
7671 vec
<constructor_elt
, va_gc
> *v
;
7672 vec_alloc (v
, nelts
);
7673 for (i
= 0; i
< nelts
; ++i
)
7674 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, builder
->elt (i
));
7677 if (gimple_in_ssa_p (cfun
))
7678 res
= make_ssa_name (type
);
7680 res
= create_tmp_reg (type
);
7681 gimple
*stmt
= gimple_build_assign (res
, build_constructor (type
, v
));
7682 gimple_set_location (stmt
, loc
);
7683 gimple_seq_add_stmt_without_update (seq
, stmt
);
7686 return builder
->build ();
7689 /* Return true if the result of assignment STMT is known to be non-negative.
7690 If the return value is based on the assumption that signed overflow is
7691 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7692 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7695 gimple_assign_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
7698 enum tree_code code
= gimple_assign_rhs_code (stmt
);
7699 switch (get_gimple_rhs_class (code
))
7701 case GIMPLE_UNARY_RHS
:
7702 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
7703 gimple_expr_type (stmt
),
7704 gimple_assign_rhs1 (stmt
),
7705 strict_overflow_p
, depth
);
7706 case GIMPLE_BINARY_RHS
:
7707 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
7708 gimple_expr_type (stmt
),
7709 gimple_assign_rhs1 (stmt
),
7710 gimple_assign_rhs2 (stmt
),
7711 strict_overflow_p
, depth
);
7712 case GIMPLE_TERNARY_RHS
:
7714 case GIMPLE_SINGLE_RHS
:
7715 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
7716 strict_overflow_p
, depth
);
7717 case GIMPLE_INVALID_RHS
:
7723 /* Return true if return value of call STMT is known to be non-negative.
7724 If the return value is based on the assumption that signed overflow is
7725 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7726 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7729 gimple_call_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
7732 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
7733 gimple_call_arg (stmt
, 0) : NULL_TREE
;
7734 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
7735 gimple_call_arg (stmt
, 1) : NULL_TREE
;
7737 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
7738 gimple_call_combined_fn (stmt
),
7741 strict_overflow_p
, depth
);
7744 /* Return true if return value of call STMT is known to be non-negative.
7745 If the return value is based on the assumption that signed overflow is
7746 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7747 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7750 gimple_phi_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
7753 for (unsigned i
= 0; i
< gimple_phi_num_args (stmt
); ++i
)
7755 tree arg
= gimple_phi_arg_def (stmt
, i
);
7756 if (!tree_single_nonnegative_warnv_p (arg
, strict_overflow_p
, depth
+ 1))
7762 /* Return true if STMT is known to compute a non-negative value.
7763 If the return value is based on the assumption that signed overflow is
7764 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7765 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7768 gimple_stmt_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
7771 switch (gimple_code (stmt
))
7774 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
,
7777 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
,
7780 return gimple_phi_nonnegative_warnv_p (stmt
, strict_overflow_p
,
7787 /* Return true if the floating-point value computed by assignment STMT
7788 is known to have an integer value. We also allow +Inf, -Inf and NaN
7789 to be considered integer values. Return false for signaling NaN.
7791 DEPTH is the current nesting depth of the query. */
7794 gimple_assign_integer_valued_real_p (gimple
*stmt
, int depth
)
7796 enum tree_code code
= gimple_assign_rhs_code (stmt
);
7797 switch (get_gimple_rhs_class (code
))
7799 case GIMPLE_UNARY_RHS
:
7800 return integer_valued_real_unary_p (gimple_assign_rhs_code (stmt
),
7801 gimple_assign_rhs1 (stmt
), depth
);
7802 case GIMPLE_BINARY_RHS
:
7803 return integer_valued_real_binary_p (gimple_assign_rhs_code (stmt
),
7804 gimple_assign_rhs1 (stmt
),
7805 gimple_assign_rhs2 (stmt
), depth
);
7806 case GIMPLE_TERNARY_RHS
:
7808 case GIMPLE_SINGLE_RHS
:
7809 return integer_valued_real_single_p (gimple_assign_rhs1 (stmt
), depth
);
7810 case GIMPLE_INVALID_RHS
:
7816 /* Return true if the floating-point value computed by call STMT is known
7817 to have an integer value. We also allow +Inf, -Inf and NaN to be
7818 considered integer values. Return false for signaling NaN.
7820 DEPTH is the current nesting depth of the query. */
7823 gimple_call_integer_valued_real_p (gimple
*stmt
, int depth
)
7825 tree arg0
= (gimple_call_num_args (stmt
) > 0
7826 ? gimple_call_arg (stmt
, 0)
7828 tree arg1
= (gimple_call_num_args (stmt
) > 1
7829 ? gimple_call_arg (stmt
, 1)
7831 return integer_valued_real_call_p (gimple_call_combined_fn (stmt
),
7835 /* Return true if the floating-point result of phi STMT is known to have
7836 an integer value. We also allow +Inf, -Inf and NaN to be considered
7837 integer values. Return false for signaling NaN.
7839 DEPTH is the current nesting depth of the query. */
7842 gimple_phi_integer_valued_real_p (gimple
*stmt
, int depth
)
7844 for (unsigned i
= 0; i
< gimple_phi_num_args (stmt
); ++i
)
7846 tree arg
= gimple_phi_arg_def (stmt
, i
);
7847 if (!integer_valued_real_single_p (arg
, depth
+ 1))
7853 /* Return true if the floating-point value computed by STMT is known
7854 to have an integer value. We also allow +Inf, -Inf and NaN to be
7855 considered integer values. Return false for signaling NaN.
7857 DEPTH is the current nesting depth of the query. */
7860 gimple_stmt_integer_valued_real_p (gimple
*stmt
, int depth
)
7862 switch (gimple_code (stmt
))
7865 return gimple_assign_integer_valued_real_p (stmt
, depth
);
7867 return gimple_call_integer_valued_real_p (stmt
, depth
);
7869 return gimple_phi_integer_valued_real_p (stmt
, depth
);