1 /* Statement simplification on GIMPLE.
2 Copyright (C) 2010-2019 Free Software Foundation, Inc.
3 Split out from tree-ssa-ccp.c.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
32 #include "gimple-pretty-print.h"
33 #include "gimple-ssa-warn-restrict.h"
34 #include "fold-const.h"
37 #include "stor-layout.h"
39 #include "gimple-fold.h"
41 #include "gimple-iterator.h"
42 #include "tree-into-ssa.h"
44 #include "tree-object-size.h"
46 #include "tree-ssa-propagate.h"
47 #include "ipa-utils.h"
48 #include "tree-ssa-address.h"
49 #include "langhooks.h"
50 #include "gimplify-me.h"
54 #include "gimple-match.h"
55 #include "gomp-constants.h"
56 #include "optabs-query.h"
57 #include "omp-general.h"
59 #include "fold-const-call.h"
60 #include "stringpool.h"
63 #include "diagnostic-core.h"
66 #include "tree-vector-builder.h"
67 #include "tree-ssa-strlen.h"
69 enum strlen_range_kind
{
70 /* Compute the exact constant string length. */
72 /* Compute the maximum constant string length. */
74 /* Compute a range of string lengths bounded by object sizes. When
75 the length of a string cannot be determined, consider as the upper
76 bound the size of the enclosing object the string may be a member
77 or element of. Also determine the size of the largest character
78 array the string may refer to. */
80 /* Determine the integer value of the argument (not string length). */
85 get_range_strlen (tree
, bitmap
*, strlen_range_kind
, c_strlen_data
*, unsigned);
87 /* Return true when DECL can be referenced from current unit.
88 FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
89 We can get declarations that are not possible to reference for various
92 1) When analyzing C++ virtual tables.
93 C++ virtual tables do have known constructors even
94 when they are keyed to other compilation unit.
95 Those tables can contain pointers to methods and vars
96 in other units. Those methods have both STATIC and EXTERNAL
98 2) In WHOPR mode devirtualization might lead to reference
99 to method that was partitioned elsehwere.
100 In this case we have static VAR_DECL or FUNCTION_DECL
101 that has no corresponding callgraph/varpool node
103 3) COMDAT functions referred by external vtables that
104 we devirtualize only during final compilation stage.
105 At this time we already decided that we will not output
106 the function body and thus we can't reference the symbol
110 can_refer_decl_in_current_unit_p (tree decl
, tree from_decl
)
113 struct cgraph_node
*node
;
116 if (DECL_ABSTRACT_P (decl
))
119 /* We are concerned only about static/external vars and functions. */
120 if ((!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
121 || !VAR_OR_FUNCTION_DECL_P (decl
))
124 /* Static objects can be referred only if they are defined and not optimized
126 if (!TREE_PUBLIC (decl
))
128 if (DECL_EXTERNAL (decl
))
130 /* Before we start optimizing unreachable code we can be sure all
131 static objects are defined. */
132 if (symtab
->function_flags_ready
)
134 snode
= symtab_node::get (decl
);
135 if (!snode
|| !snode
->definition
)
137 node
= dyn_cast
<cgraph_node
*> (snode
);
138 return !node
|| !node
->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
);
989 if (dest_align
< TYPE_ALIGN (desttype
)
990 || src_align
< TYPE_ALIGN (srctype
))
994 if (TREE_CODE (dest
) == ADDR_EXPR
995 && var_decl_component_p (TREE_OPERAND (dest
, 0))
996 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype
), len
))
997 destvar
= fold_build2 (MEM_REF
, desttype
, dest
, off0
);
1000 if (TREE_CODE (src
) == ADDR_EXPR
1001 && var_decl_component_p (TREE_OPERAND (src
, 0))
1002 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype
), len
))
1005 || src_align
>= TYPE_ALIGN (desttype
))
1006 srcvar
= fold_build2 (MEM_REF
, destvar
? desttype
: srctype
,
1008 else if (!STRICT_ALIGNMENT
)
1010 srctype
= build_aligned_type (TYPE_MAIN_VARIANT (desttype
),
1012 srcvar
= fold_build2 (MEM_REF
, srctype
, src
, off0
);
1016 if (srcvar
== NULL_TREE
&& destvar
== NULL_TREE
)
1019 if (srcvar
== NULL_TREE
)
1021 if (src_align
>= TYPE_ALIGN (desttype
))
1022 srcvar
= fold_build2 (MEM_REF
, desttype
, src
, off0
);
1025 if (STRICT_ALIGNMENT
)
1027 srctype
= build_aligned_type (TYPE_MAIN_VARIANT (desttype
),
1029 srcvar
= fold_build2 (MEM_REF
, srctype
, src
, off0
);
1032 else if (destvar
== NULL_TREE
)
1034 if (dest_align
>= TYPE_ALIGN (srctype
))
1035 destvar
= fold_build2 (MEM_REF
, srctype
, dest
, off0
);
1038 if (STRICT_ALIGNMENT
)
1040 desttype
= build_aligned_type (TYPE_MAIN_VARIANT (srctype
),
1042 destvar
= fold_build2 (MEM_REF
, desttype
, dest
, off0
);
1046 /* Same as above, detect out-of-bounds accesses without issuing
1047 warnings. Avoid folding out-of-bounds copies but to avoid
1048 false positives for unreachable code defer warning until
1049 after DCE has worked its magic.
1050 -Wrestrict is still diagnosed. */
1051 if (int warning
= check_bounds_or_overlap (as_a
<gcall
*>(stmt
),
1052 dest
, src
, len
, len
,
1054 if (warning
!= OPT_Wrestrict
)
1058 if (is_gimple_reg_type (TREE_TYPE (srcvar
)))
1060 tree tem
= fold_const_aggregate_ref (srcvar
);
1063 if (! is_gimple_min_invariant (srcvar
))
1065 new_stmt
= gimple_build_assign (NULL_TREE
, srcvar
);
1066 srcvar
= create_tmp_reg_or_ssa_name (TREE_TYPE (srcvar
),
1068 gimple_assign_set_lhs (new_stmt
, srcvar
);
1069 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
1070 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
1072 new_stmt
= gimple_build_assign (destvar
, srcvar
);
1073 goto set_vop_and_replace
;
1076 /* We get an aggregate copy. Use an unsigned char[] type to
1077 perform the copying to preserve padding and to avoid any issues
1078 with TREE_ADDRESSABLE types or float modes behavior on copying. */
1079 desttype
= build_array_type_nelts (unsigned_char_type_node
,
1080 tree_to_uhwi (len
));
1082 if (src_align
> TYPE_ALIGN (srctype
))
1083 srctype
= build_aligned_type (srctype
, src_align
);
1084 if (dest_align
> TYPE_ALIGN (desttype
))
1085 desttype
= build_aligned_type (desttype
, dest_align
);
1087 = gimple_build_assign (fold_build2 (MEM_REF
, desttype
, dest
, off0
),
1088 fold_build2 (MEM_REF
, srctype
, src
, off0
));
1089 set_vop_and_replace
:
1090 gimple_move_vops (new_stmt
, stmt
);
1093 gsi_replace (gsi
, new_stmt
, false);
1096 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
1100 gimple_seq stmts
= NULL
;
1101 if (code
== BUILT_IN_MEMCPY
|| code
== BUILT_IN_MEMMOVE
)
1103 else if (code
== BUILT_IN_MEMPCPY
)
1105 len
= gimple_convert_to_ptrofftype (&stmts
, loc
, len
);
1106 dest
= gimple_build (&stmts
, loc
, POINTER_PLUS_EXPR
,
1107 TREE_TYPE (dest
), dest
, len
);
1112 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1113 gimple
*repl
= gimple_build_assign (lhs
, dest
);
1114 gsi_replace (gsi
, repl
, false);
1118 /* Transform a call to built-in bcmp(a, b, len) at *GSI into one
1119 to built-in memcmp (a, b, len). */
1122 gimple_fold_builtin_bcmp (gimple_stmt_iterator
*gsi
)
1124 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCMP
);
1129 /* Transform bcmp (a, b, len) into memcmp (a, b, len). */
1131 gimple
*stmt
= gsi_stmt (*gsi
);
1132 tree a
= gimple_call_arg (stmt
, 0);
1133 tree b
= gimple_call_arg (stmt
, 1);
1134 tree len
= gimple_call_arg (stmt
, 2);
1136 gimple
*repl
= gimple_build_call (fn
, 3, a
, b
, len
);
1137 replace_call_with_call_and_fold (gsi
, repl
);
1142 /* Transform a call to built-in bcopy (src, dest, len) at *GSI into one
1143 to built-in memmove (dest, src, len). */
1146 gimple_fold_builtin_bcopy (gimple_stmt_iterator
*gsi
)
1148 tree fn
= builtin_decl_implicit (BUILT_IN_MEMMOVE
);
1153 /* bcopy has been removed from POSIX in Issue 7 but Issue 6 specifies
1154 it's quivalent to memmove (not memcpy). Transform bcopy (src, dest,
1155 len) into memmove (dest, src, len). */
1157 gimple
*stmt
= gsi_stmt (*gsi
);
1158 tree src
= gimple_call_arg (stmt
, 0);
1159 tree dest
= gimple_call_arg (stmt
, 1);
1160 tree len
= gimple_call_arg (stmt
, 2);
1162 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1163 gimple_call_set_fntype (as_a
<gcall
*> (stmt
), TREE_TYPE (fn
));
1164 replace_call_with_call_and_fold (gsi
, repl
);
1169 /* Transform a call to built-in bzero (dest, len) at *GSI into one
1170 to built-in memset (dest, 0, len). */
1173 gimple_fold_builtin_bzero (gimple_stmt_iterator
*gsi
)
1175 tree fn
= builtin_decl_implicit (BUILT_IN_MEMSET
);
1180 /* Transform bzero (dest, len) into memset (dest, 0, len). */
1182 gimple
*stmt
= gsi_stmt (*gsi
);
1183 tree dest
= gimple_call_arg (stmt
, 0);
1184 tree len
= gimple_call_arg (stmt
, 1);
1186 gimple_seq seq
= NULL
;
1187 gimple
*repl
= gimple_build_call (fn
, 3, dest
, integer_zero_node
, len
);
1188 gimple_seq_add_stmt_without_update (&seq
, repl
);
1189 gsi_replace_with_seq_vops (gsi
, seq
);
1195 /* Fold function call to builtin memset or bzero at *GSI setting the
1196 memory of size LEN to VAL. Return whether a simplification was made. */
1199 gimple_fold_builtin_memset (gimple_stmt_iterator
*gsi
, tree c
, tree len
)
1201 gimple
*stmt
= gsi_stmt (*gsi
);
1203 unsigned HOST_WIDE_INT length
, cval
;
1205 /* If the LEN parameter is zero, return DEST. */
1206 if (integer_zerop (len
))
1208 replace_call_with_value (gsi
, gimple_call_arg (stmt
, 0));
1212 if (! tree_fits_uhwi_p (len
))
1215 if (TREE_CODE (c
) != INTEGER_CST
)
1218 tree dest
= gimple_call_arg (stmt
, 0);
1220 if (TREE_CODE (var
) != ADDR_EXPR
)
1223 var
= TREE_OPERAND (var
, 0);
1224 if (TREE_THIS_VOLATILE (var
))
1227 etype
= TREE_TYPE (var
);
1228 if (TREE_CODE (etype
) == ARRAY_TYPE
)
1229 etype
= TREE_TYPE (etype
);
1231 if (!INTEGRAL_TYPE_P (etype
)
1232 && !POINTER_TYPE_P (etype
))
1235 if (! var_decl_component_p (var
))
1238 length
= tree_to_uhwi (len
);
1239 if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (etype
)) != length
1240 || get_pointer_alignment (dest
) / BITS_PER_UNIT
< length
)
1243 if (length
> HOST_BITS_PER_WIDE_INT
/ BITS_PER_UNIT
)
1246 if (integer_zerop (c
))
1250 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8 || HOST_BITS_PER_WIDE_INT
> 64)
1253 cval
= TREE_INT_CST_LOW (c
);
1257 cval
|= (cval
<< 31) << 1;
1260 var
= fold_build2 (MEM_REF
, etype
, dest
, build_int_cst (ptr_type_node
, 0));
1261 gimple
*store
= gimple_build_assign (var
, build_int_cst_type (etype
, cval
));
1262 gimple_move_vops (store
, stmt
);
1263 gsi_insert_before (gsi
, store
, GSI_SAME_STMT
);
1264 if (gimple_call_lhs (stmt
))
1266 gimple
*asgn
= gimple_build_assign (gimple_call_lhs (stmt
), dest
);
1267 gsi_replace (gsi
, asgn
, false);
1271 gimple_stmt_iterator gsi2
= *gsi
;
1273 gsi_remove (&gsi2
, true);
1279 /* Helper of get_range_strlen for ARG that is not an SSA_NAME. */
1282 get_range_strlen_tree (tree arg
, bitmap
*visited
, strlen_range_kind rkind
,
1283 c_strlen_data
*pdata
, unsigned eltsize
)
1285 gcc_assert (TREE_CODE (arg
) != SSA_NAME
);
1287 /* The length computed by this invocation of the function. */
1288 tree val
= NULL_TREE
;
1290 /* True if VAL is an optimistic (tight) bound determined from
1291 the size of the character array in which the string may be
1292 stored. In that case, the computed VAL is used to set
1294 bool tight_bound
= false;
1296 /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */
1297 if (TREE_CODE (arg
) == ADDR_EXPR
1298 && TREE_CODE (TREE_OPERAND (arg
, 0)) == ARRAY_REF
)
1300 tree op
= TREE_OPERAND (arg
, 0);
1301 if (integer_zerop (TREE_OPERAND (op
, 1)))
1303 tree aop0
= TREE_OPERAND (op
, 0);
1304 if (TREE_CODE (aop0
) == INDIRECT_REF
1305 && TREE_CODE (TREE_OPERAND (aop0
, 0)) == SSA_NAME
)
1306 return get_range_strlen (TREE_OPERAND (aop0
, 0), visited
, rkind
,
1309 else if (TREE_CODE (TREE_OPERAND (op
, 0)) == COMPONENT_REF
1310 && rkind
== SRK_LENRANGE
)
1312 /* Fail if an array is the last member of a struct object
1313 since it could be treated as a (fake) flexible array
1315 tree idx
= TREE_OPERAND (op
, 1);
1317 arg
= TREE_OPERAND (op
, 0);
1318 tree optype
= TREE_TYPE (arg
);
1319 if (tree dom
= TYPE_DOMAIN (optype
))
1320 if (tree bound
= TYPE_MAX_VALUE (dom
))
1321 if (TREE_CODE (bound
) == INTEGER_CST
1322 && TREE_CODE (idx
) == INTEGER_CST
1323 && tree_int_cst_lt (bound
, idx
))
1328 if (rkind
== SRK_INT_VALUE
)
1330 /* We are computing the maximum value (not string length). */
1332 if (TREE_CODE (val
) != INTEGER_CST
1333 || tree_int_cst_sgn (val
) < 0)
1338 c_strlen_data lendata
= { };
1339 val
= c_strlen (arg
, 1, &lendata
, eltsize
);
1341 if (!val
&& lendata
.decl
)
1343 /* ARG refers to an unterminated const character array.
1344 DATA.DECL with size DATA.LEN. */
1345 val
= lendata
.minlen
;
1346 pdata
->decl
= lendata
.decl
;
1350 /* Set if VAL represents the maximum length based on array size (set
1351 when exact length cannot be determined). */
1352 bool maxbound
= false;
1354 if (!val
&& rkind
== SRK_LENRANGE
)
1356 if (TREE_CODE (arg
) == ADDR_EXPR
)
1357 return get_range_strlen (TREE_OPERAND (arg
, 0), visited
, rkind
,
1360 if (TREE_CODE (arg
) == ARRAY_REF
)
1362 tree optype
= TREE_TYPE (TREE_OPERAND (arg
, 0));
1364 /* Determine the "innermost" array type. */
1365 while (TREE_CODE (optype
) == ARRAY_TYPE
1366 && TREE_CODE (TREE_TYPE (optype
)) == ARRAY_TYPE
)
1367 optype
= TREE_TYPE (optype
);
1369 /* Avoid arrays of pointers. */
1370 tree eltype
= TREE_TYPE (optype
);
1371 if (TREE_CODE (optype
) != ARRAY_TYPE
1372 || !INTEGRAL_TYPE_P (eltype
))
1375 /* Fail when the array bound is unknown or zero. */
1376 val
= TYPE_SIZE_UNIT (optype
);
1377 if (!val
|| integer_zerop (val
))
1380 val
= fold_build2 (MINUS_EXPR
, TREE_TYPE (val
), val
,
1383 /* Set the minimum size to zero since the string in
1384 the array could have zero length. */
1385 pdata
->minlen
= ssize_int (0);
1389 else if (TREE_CODE (arg
) == COMPONENT_REF
1390 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (arg
, 1)))
1393 /* Use the type of the member array to determine the upper
1394 bound on the length of the array. This may be overly
1395 optimistic if the array itself isn't NUL-terminated and
1396 the caller relies on the subsequent member to contain
1397 the NUL but that would only be considered valid if
1398 the array were the last member of a struct. */
1400 tree fld
= TREE_OPERAND (arg
, 1);
1402 tree optype
= TREE_TYPE (fld
);
1404 /* Determine the "innermost" array type. */
1405 while (TREE_CODE (optype
) == ARRAY_TYPE
1406 && TREE_CODE (TREE_TYPE (optype
)) == ARRAY_TYPE
)
1407 optype
= TREE_TYPE (optype
);
1409 /* Fail when the array bound is unknown or zero. */
1410 val
= TYPE_SIZE_UNIT (optype
);
1411 if (!val
|| integer_zerop (val
))
1413 val
= fold_build2 (MINUS_EXPR
, TREE_TYPE (val
), val
,
1416 /* Set the minimum size to zero since the string in
1417 the array could have zero length. */
1418 pdata
->minlen
= ssize_int (0);
1420 /* The array size determined above is an optimistic bound
1421 on the length. If the array isn't nul-terminated the
1422 length computed by the library function would be greater.
1423 Even though using strlen to cross the subobject boundary
1424 is undefined, avoid drawing conclusions from the member
1425 type about the length here. */
1428 else if (VAR_P (arg
))
1430 /* Avoid handling pointers to arrays. GCC might misuse
1431 a pointer to an array of one bound to point to an array
1432 object of a greater bound. */
1433 tree argtype
= TREE_TYPE (arg
);
1434 if (TREE_CODE (argtype
) == ARRAY_TYPE
)
1436 val
= TYPE_SIZE_UNIT (argtype
);
1438 || TREE_CODE (val
) != INTEGER_CST
1439 || integer_zerop (val
))
1441 val
= wide_int_to_tree (TREE_TYPE (val
),
1442 wi::sub (wi::to_wide (val
), 1));
1444 /* Set the minimum size to zero since the string in
1445 the array could have zero length. */
1446 pdata
->minlen
= ssize_int (0);
1455 /* Adjust the lower bound on the string length as necessary. */
1457 || (rkind
!= SRK_STRLEN
1458 && TREE_CODE (pdata
->minlen
) == INTEGER_CST
1459 && TREE_CODE (val
) == INTEGER_CST
1460 && tree_int_cst_lt (val
, pdata
->minlen
)))
1461 pdata
->minlen
= val
;
1463 if (pdata
->maxbound
&& TREE_CODE (pdata
->maxbound
) == INTEGER_CST
)
1465 /* Adjust the tighter (more optimistic) string length bound
1466 if necessary and proceed to adjust the more conservative
1468 if (TREE_CODE (val
) == INTEGER_CST
)
1470 if (tree_int_cst_lt (pdata
->maxbound
, val
))
1471 pdata
->maxbound
= val
;
1474 pdata
->maxbound
= val
;
1476 else if (pdata
->maxbound
|| maxbound
)
1477 /* Set PDATA->MAXBOUND only if it either isn't INTEGER_CST or
1478 if VAL corresponds to the maximum length determined based
1479 on the type of the object. */
1480 pdata
->maxbound
= val
;
1484 /* VAL computed above represents an optimistically tight bound
1485 on the length of the string based on the referenced object's
1486 or subobject's type. Determine the conservative upper bound
1487 based on the enclosing object's size if possible. */
1488 if (rkind
== SRK_LENRANGE
)
1491 tree base
= get_addr_base_and_unit_offset (arg
, &offset
);
1494 /* When the call above fails due to a non-constant offset
1495 assume the offset is zero and use the size of the whole
1496 enclosing object instead. */
1497 base
= get_base_address (arg
);
1500 /* If the base object is a pointer no upper bound on the length
1501 can be determined. Otherwise the maximum length is equal to
1502 the size of the enclosing object minus the offset of
1503 the referenced subobject minus 1 (for the terminating nul). */
1504 tree type
= TREE_TYPE (base
);
1505 if (TREE_CODE (type
) == POINTER_TYPE
1506 || !VAR_P (base
) || !(val
= DECL_SIZE_UNIT (base
)))
1507 val
= build_all_ones_cst (size_type_node
);
1510 val
= DECL_SIZE_UNIT (base
);
1511 val
= fold_build2 (MINUS_EXPR
, TREE_TYPE (val
), val
,
1512 size_int (offset
+ 1));
1521 /* Adjust the more conservative bound if possible/necessary
1522 and fail otherwise. */
1523 if (rkind
!= SRK_STRLEN
)
1525 if (TREE_CODE (pdata
->maxlen
) != INTEGER_CST
1526 || TREE_CODE (val
) != INTEGER_CST
)
1529 if (tree_int_cst_lt (pdata
->maxlen
, val
))
1530 pdata
->maxlen
= val
;
1533 else if (simple_cst_equal (val
, pdata
->maxlen
) != 1)
1535 /* Fail if the length of this ARG is different from that
1536 previously determined from another ARG. */
1541 pdata
->maxlen
= val
;
1542 return rkind
== SRK_LENRANGE
|| !integer_all_onesp (val
);
1545 /* For an ARG referencing one or more strings, try to obtain the range
1546 of their lengths, or the size of the largest array ARG referes to if
1547 the range of lengths cannot be determined, and store all in *PDATA.
1548 For an integer ARG (when RKIND == SRK_INT_VALUE), try to determine
1549 the maximum constant value.
1550 If ARG is an SSA_NAME, follow its use-def chains. When RKIND ==
1551 SRK_STRLEN, then if PDATA->MAXLEN is not equal to the determined
1552 length or if we are unable to determine the length, return false.
1553 VISITED is a bitmap of visited variables.
1554 RKIND determines the kind of value or range to obtain (see
1556 Set PDATA->DECL if ARG refers to an unterminated constant array.
1557 On input, set ELTSIZE to 1 for normal single byte character strings,
1558 and either 2 or 4 for wide characer strings (the size of wchar_t).
1559 Return true if *PDATA was successfully populated and false otherwise. */
1562 get_range_strlen (tree arg
, bitmap
*visited
,
1563 strlen_range_kind rkind
,
1564 c_strlen_data
*pdata
, unsigned eltsize
)
1567 if (TREE_CODE (arg
) != SSA_NAME
)
1568 return get_range_strlen_tree (arg
, visited
, rkind
, pdata
, eltsize
);
1570 /* If ARG is registered for SSA update we cannot look at its defining
1572 if (name_registered_for_update_p (arg
))
1575 /* If we were already here, break the infinite cycle. */
1577 *visited
= BITMAP_ALLOC (NULL
);
1578 if (!bitmap_set_bit (*visited
, SSA_NAME_VERSION (arg
)))
1582 gimple
*def_stmt
= SSA_NAME_DEF_STMT (var
);
1584 switch (gimple_code (def_stmt
))
1587 /* The RHS of the statement defining VAR must either have a
1588 constant length or come from another SSA_NAME with a constant
1590 if (gimple_assign_single_p (def_stmt
)
1591 || gimple_assign_unary_nop_p (def_stmt
))
1593 tree rhs
= gimple_assign_rhs1 (def_stmt
);
1594 return get_range_strlen (rhs
, visited
, rkind
, pdata
, eltsize
);
1596 else if (gimple_assign_rhs_code (def_stmt
) == COND_EXPR
)
1598 tree ops
[2] = { gimple_assign_rhs2 (def_stmt
),
1599 gimple_assign_rhs3 (def_stmt
) };
1601 for (unsigned int i
= 0; i
< 2; i
++)
1602 if (!get_range_strlen (ops
[i
], visited
, rkind
, pdata
, eltsize
))
1604 if (rkind
!= SRK_LENRANGE
)
1606 /* Set the upper bound to the maximum to prevent
1607 it from being adjusted in the next iteration but
1608 leave MINLEN and the more conservative MAXBOUND
1609 determined so far alone (or leave them null if
1610 they haven't been set yet). That the MINLEN is
1611 in fact zero can be determined from MAXLEN being
1612 unbounded but the discovered minimum is used for
1614 pdata
->maxlen
= build_all_ones_cst (size_type_node
);
1621 /* Unless RKIND == SRK_LENRANGE, all arguments of the PHI node
1622 must have a constant length. */
1623 for (unsigned i
= 0; i
< gimple_phi_num_args (def_stmt
); i
++)
1625 tree arg
= gimple_phi_arg (def_stmt
, i
)->def
;
1627 /* If this PHI has itself as an argument, we cannot
1628 determine the string length of this argument. However,
1629 if we can find a constant string length for the other
1630 PHI args then we can still be sure that this is a
1631 constant string length. So be optimistic and just
1632 continue with the next argument. */
1633 if (arg
== gimple_phi_result (def_stmt
))
1636 if (!get_range_strlen (arg
, visited
, rkind
, pdata
, eltsize
))
1638 if (rkind
!= SRK_LENRANGE
)
1640 /* Set the upper bound to the maximum to prevent
1641 it from being adjusted in the next iteration but
1642 leave MINLEN and the more conservative MAXBOUND
1643 determined so far alone (or leave them null if
1644 they haven't been set yet). That the MINLEN is
1645 in fact zero can be determined from MAXLEN being
1646 unbounded but the discovered minimum is used for
1648 pdata
->maxlen
= build_all_ones_cst (size_type_node
);
1658 /* Try to obtain the range of the lengths of the string(s) referenced
1659 by ARG, or the size of the largest array ARG refers to if the range
1660 of lengths cannot be determined, and store all in *PDATA which must
1661 be zero-initialized on input except PDATA->MAXBOUND may be set to
1662 a non-null tree node other than INTEGER_CST to request to have it
1663 set to the length of the longest string in a PHI. ELTSIZE is
1664 the expected size of the string element in bytes: 1 for char and
1665 some power of 2 for wide characters.
1666 Return true if the range [PDATA->MINLEN, PDATA->MAXLEN] is suitable
1667 for optimization. Returning false means that a nonzero PDATA->MINLEN
1668 doesn't reflect the true lower bound of the range when PDATA->MAXLEN
1669 is -1 (in that case, the actual range is indeterminate, i.e.,
1670 [0, PTRDIFF_MAX - 2]. */
1673 get_range_strlen (tree arg
, c_strlen_data
*pdata
, unsigned eltsize
)
1675 bitmap visited
= NULL
;
1676 tree maxbound
= pdata
->maxbound
;
1678 if (!get_range_strlen (arg
, &visited
, SRK_LENRANGE
, pdata
, eltsize
))
1680 /* On failure extend the length range to an impossible maximum
1681 (a valid MAXLEN must be less than PTRDIFF_MAX - 1). Other
1682 members can stay unchanged regardless. */
1683 pdata
->minlen
= ssize_int (0);
1684 pdata
->maxlen
= build_all_ones_cst (size_type_node
);
1686 else if (!pdata
->minlen
)
1687 pdata
->minlen
= ssize_int (0);
1689 /* If it's unchanged from it initial non-null value, set the conservative
1690 MAXBOUND to SIZE_MAX. Otherwise leave it null (if it is null). */
1691 if (maxbound
&& pdata
->maxbound
== maxbound
)
1692 pdata
->maxbound
= build_all_ones_cst (size_type_node
);
1695 BITMAP_FREE (visited
);
1697 return !integer_all_onesp (pdata
->maxlen
);
1700 /* Return the maximum value for ARG given RKIND (see strlen_range_kind).
1701 For ARG of pointer types, NONSTR indicates if the caller is prepared
1702 to handle unterminated strings. For integer ARG and when RKIND ==
1703 SRK_INT_VALUE, NONSTR must be null.
1705 If an unterminated array is discovered and our caller handles
1706 unterminated arrays, then bubble up the offending DECL and
1707 return the maximum size. Otherwise return NULL. */
1710 get_maxval_strlen (tree arg
, strlen_range_kind rkind
, tree
*nonstr
= NULL
)
1712 /* A non-null NONSTR is meaningless when determining the maximum
1713 value of an integer ARG. */
1714 gcc_assert (rkind
!= SRK_INT_VALUE
|| nonstr
== NULL
);
1715 /* ARG must have an integral type when RKIND says so. */
1716 gcc_assert (rkind
!= SRK_INT_VALUE
|| INTEGRAL_TYPE_P (TREE_TYPE (arg
)));
1718 bitmap visited
= NULL
;
1720 /* Reset DATA.MAXLEN if the call fails or when DATA.MAXLEN
1722 c_strlen_data lendata
= { };
1723 if (!get_range_strlen (arg
, &visited
, rkind
, &lendata
, /* eltsize = */1))
1724 lendata
.maxlen
= NULL_TREE
;
1725 else if (lendata
.maxlen
&& integer_all_onesp (lendata
.maxlen
))
1726 lendata
.maxlen
= NULL_TREE
;
1729 BITMAP_FREE (visited
);
1733 /* For callers prepared to handle unterminated arrays set
1734 *NONSTR to point to the declaration of the array and return
1735 the maximum length/size. */
1736 *nonstr
= lendata
.decl
;
1737 return lendata
.maxlen
;
1740 /* Fail if the constant array isn't nul-terminated. */
1741 return lendata
.decl
? NULL_TREE
: lendata
.maxlen
;
1745 /* Fold function call to builtin strcpy with arguments DEST and SRC.
1746 If LEN is not NULL, it represents the length of the string to be
1747 copied. Return NULL_TREE if no simplification can be made. */
1750 gimple_fold_builtin_strcpy (gimple_stmt_iterator
*gsi
,
1751 tree dest
, tree src
)
1753 gimple
*stmt
= gsi_stmt (*gsi
);
1754 location_t loc
= gimple_location (stmt
);
1757 /* If SRC and DEST are the same (and not volatile), return DEST. */
1758 if (operand_equal_p (src
, dest
, 0))
1760 /* Issue -Wrestrict unless the pointers are null (those do
1761 not point to objects and so do not indicate an overlap;
1762 such calls could be the result of sanitization and jump
1764 if (!integer_zerop (dest
) && !gimple_no_warning_p (stmt
))
1766 tree func
= gimple_call_fndecl (stmt
);
1768 warning_at (loc
, OPT_Wrestrict
,
1769 "%qD source argument is the same as destination",
1773 replace_call_with_value (gsi
, dest
);
1777 if (optimize_function_for_size_p (cfun
))
1780 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
1784 /* Set to non-null if ARG refers to an unterminated array. */
1786 tree len
= get_maxval_strlen (src
, SRK_STRLEN
, &nonstr
);
1790 /* Avoid folding calls with unterminated arrays. */
1791 if (!gimple_no_warning_p (stmt
))
1792 warn_string_no_nul (loc
, "strcpy", src
, nonstr
);
1793 gimple_set_no_warning (stmt
, true);
1800 len
= fold_convert_loc (loc
, size_type_node
, len
);
1801 len
= size_binop_loc (loc
, PLUS_EXPR
, len
, build_int_cst (size_type_node
, 1));
1802 len
= force_gimple_operand_gsi (gsi
, len
, true,
1803 NULL_TREE
, true, GSI_SAME_STMT
);
1804 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1805 replace_call_with_call_and_fold (gsi
, repl
);
1809 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
1810 If SLEN is not NULL, it represents the length of the source string.
1811 Return NULL_TREE if no simplification can be made. */
1814 gimple_fold_builtin_strncpy (gimple_stmt_iterator
*gsi
,
1815 tree dest
, tree src
, tree len
)
1817 gimple
*stmt
= gsi_stmt (*gsi
);
1818 location_t loc
= gimple_location (stmt
);
1819 bool nonstring
= get_attr_nonstring_decl (dest
) != NULL_TREE
;
1821 /* If the LEN parameter is zero, return DEST. */
1822 if (integer_zerop (len
))
1824 /* Avoid warning if the destination refers to a an array/pointer
1825 decorate with attribute nonstring. */
1828 tree fndecl
= gimple_call_fndecl (stmt
);
1830 /* Warn about the lack of nul termination: the result is not
1831 a (nul-terminated) string. */
1832 tree slen
= get_maxval_strlen (src
, SRK_STRLEN
);
1833 if (slen
&& !integer_zerop (slen
))
1834 warning_at (loc
, OPT_Wstringop_truncation
,
1835 "%G%qD destination unchanged after copying no bytes "
1836 "from a string of length %E",
1837 stmt
, fndecl
, slen
);
1839 warning_at (loc
, OPT_Wstringop_truncation
,
1840 "%G%qD destination unchanged after copying no bytes",
1844 replace_call_with_value (gsi
, dest
);
1848 /* We can't compare slen with len as constants below if len is not a
1850 if (TREE_CODE (len
) != INTEGER_CST
)
1853 /* Now, we must be passed a constant src ptr parameter. */
1854 tree slen
= get_maxval_strlen (src
, SRK_STRLEN
);
1855 if (!slen
|| TREE_CODE (slen
) != INTEGER_CST
)
1858 /* The size of the source string including the terminating nul. */
1859 tree ssize
= size_binop_loc (loc
, PLUS_EXPR
, slen
, ssize_int (1));
1861 /* We do not support simplification of this case, though we do
1862 support it when expanding trees into RTL. */
1863 /* FIXME: generate a call to __builtin_memset. */
1864 if (tree_int_cst_lt (ssize
, len
))
1867 /* Diagnose truncation that leaves the copy unterminated. */
1868 maybe_diag_stxncpy_trunc (*gsi
, src
, len
);
1870 /* OK transform into builtin memcpy. */
1871 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
1875 len
= fold_convert_loc (loc
, size_type_node
, len
);
1876 len
= force_gimple_operand_gsi (gsi
, len
, true,
1877 NULL_TREE
, true, GSI_SAME_STMT
);
1878 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1879 replace_call_with_call_and_fold (gsi
, repl
);
1884 /* Fold function call to builtin strchr or strrchr.
1885 If both arguments are constant, evaluate and fold the result,
1886 otherwise simplify str(r)chr (str, 0) into str + strlen (str).
1887 In general strlen is significantly faster than strchr
1888 due to being a simpler operation. */
1890 gimple_fold_builtin_strchr (gimple_stmt_iterator
*gsi
, bool is_strrchr
)
1892 gimple
*stmt
= gsi_stmt (*gsi
);
1893 tree str
= gimple_call_arg (stmt
, 0);
1894 tree c
= gimple_call_arg (stmt
, 1);
1895 location_t loc
= gimple_location (stmt
);
1899 if (!gimple_call_lhs (stmt
))
1902 if ((p
= c_getstr (str
)) && target_char_cst_p (c
, &ch
))
1904 const char *p1
= is_strrchr
? strrchr (p
, ch
) : strchr (p
, ch
);
1908 replace_call_with_value (gsi
, integer_zero_node
);
1912 tree len
= build_int_cst (size_type_node
, p1
- p
);
1913 gimple_seq stmts
= NULL
;
1914 gimple
*new_stmt
= gimple_build_assign (gimple_call_lhs (stmt
),
1915 POINTER_PLUS_EXPR
, str
, len
);
1916 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
1917 gsi_replace_with_seq_vops (gsi
, stmts
);
1921 if (!integer_zerop (c
))
1924 /* Transform strrchr (s, 0) to strchr (s, 0) when optimizing for size. */
1925 if (is_strrchr
&& optimize_function_for_size_p (cfun
))
1927 tree strchr_fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
1931 gimple
*repl
= gimple_build_call (strchr_fn
, 2, str
, c
);
1932 replace_call_with_call_and_fold (gsi
, repl
);
1940 tree strlen_fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
1945 /* Create newstr = strlen (str). */
1946 gimple_seq stmts
= NULL
;
1947 gimple
*new_stmt
= gimple_build_call (strlen_fn
, 1, str
);
1948 gimple_set_location (new_stmt
, loc
);
1949 len
= create_tmp_reg_or_ssa_name (size_type_node
);
1950 gimple_call_set_lhs (new_stmt
, len
);
1951 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
1953 /* Create (str p+ strlen (str)). */
1954 new_stmt
= gimple_build_assign (gimple_call_lhs (stmt
),
1955 POINTER_PLUS_EXPR
, str
, len
);
1956 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
1957 gsi_replace_with_seq_vops (gsi
, stmts
);
1958 /* gsi now points at the assignment to the lhs, get a
1959 stmt iterator to the strlen.
1960 ??? We can't use gsi_for_stmt as that doesn't work when the
1961 CFG isn't built yet. */
1962 gimple_stmt_iterator gsi2
= *gsi
;
1968 /* Fold function call to builtin strstr.
1969 If both arguments are constant, evaluate and fold the result,
1970 additionally fold strstr (x, "") into x and strstr (x, "c")
1971 into strchr (x, 'c'). */
1973 gimple_fold_builtin_strstr (gimple_stmt_iterator
*gsi
)
1975 gimple
*stmt
= gsi_stmt (*gsi
);
1976 tree haystack
= gimple_call_arg (stmt
, 0);
1977 tree needle
= gimple_call_arg (stmt
, 1);
1980 if (!gimple_call_lhs (stmt
))
1983 q
= c_getstr (needle
);
1987 if ((p
= c_getstr (haystack
)))
1989 const char *r
= strstr (p
, q
);
1993 replace_call_with_value (gsi
, integer_zero_node
);
1997 tree len
= build_int_cst (size_type_node
, r
- p
);
1998 gimple_seq stmts
= NULL
;
2000 = gimple_build_assign (gimple_call_lhs (stmt
), POINTER_PLUS_EXPR
,
2002 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
2003 gsi_replace_with_seq_vops (gsi
, stmts
);
2007 /* For strstr (x, "") return x. */
2010 replace_call_with_value (gsi
, haystack
);
2014 /* Transform strstr (x, "c") into strchr (x, 'c'). */
2017 tree strchr_fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
2020 tree c
= build_int_cst (integer_type_node
, q
[0]);
2021 gimple
*repl
= gimple_build_call (strchr_fn
, 2, haystack
, c
);
2022 replace_call_with_call_and_fold (gsi
, repl
);
2030 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
2033 Return NULL_TREE if no simplification was possible, otherwise return the
2034 simplified form of the call as a tree.
2036 The simplified form may be a constant or other expression which
2037 computes the same value, but in a more efficient manner (including
2038 calls to other builtin functions).
2040 The call may contain arguments which need to be evaluated, but
2041 which are not useful to determine the result of the call. In
2042 this case we return a chain of COMPOUND_EXPRs. The LHS of each
2043 COMPOUND_EXPR will be an argument which must be evaluated.
2044 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
2045 COMPOUND_EXPR in the chain will contain the tree for the simplified
2046 form of the builtin function call. */
2049 gimple_fold_builtin_strcat (gimple_stmt_iterator
*gsi
, tree dst
, tree src
)
2051 gimple
*stmt
= gsi_stmt (*gsi
);
2052 location_t loc
= gimple_location (stmt
);
2054 const char *p
= c_getstr (src
);
2056 /* If the string length is zero, return the dst parameter. */
2057 if (p
&& *p
== '\0')
2059 replace_call_with_value (gsi
, dst
);
2063 if (!optimize_bb_for_speed_p (gimple_bb (stmt
)))
2066 /* See if we can store by pieces into (dst + strlen(dst)). */
2068 tree strlen_fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
2069 tree memcpy_fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
2071 if (!strlen_fn
|| !memcpy_fn
)
2074 /* If the length of the source string isn't computable don't
2075 split strcat into strlen and memcpy. */
2076 tree len
= get_maxval_strlen (src
, SRK_STRLEN
);
2080 /* Create strlen (dst). */
2081 gimple_seq stmts
= NULL
, stmts2
;
2082 gimple
*repl
= gimple_build_call (strlen_fn
, 1, dst
);
2083 gimple_set_location (repl
, loc
);
2084 newdst
= create_tmp_reg_or_ssa_name (size_type_node
);
2085 gimple_call_set_lhs (repl
, newdst
);
2086 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2088 /* Create (dst p+ strlen (dst)). */
2089 newdst
= fold_build_pointer_plus_loc (loc
, dst
, newdst
);
2090 newdst
= force_gimple_operand (newdst
, &stmts2
, true, NULL_TREE
);
2091 gimple_seq_add_seq_without_update (&stmts
, stmts2
);
2093 len
= fold_convert_loc (loc
, size_type_node
, len
);
2094 len
= size_binop_loc (loc
, PLUS_EXPR
, len
,
2095 build_int_cst (size_type_node
, 1));
2096 len
= force_gimple_operand (len
, &stmts2
, true, NULL_TREE
);
2097 gimple_seq_add_seq_without_update (&stmts
, stmts2
);
2099 repl
= gimple_build_call (memcpy_fn
, 3, newdst
, src
, len
);
2100 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2101 if (gimple_call_lhs (stmt
))
2103 repl
= gimple_build_assign (gimple_call_lhs (stmt
), dst
);
2104 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2105 gsi_replace_with_seq_vops (gsi
, stmts
);
2106 /* gsi now points at the assignment to the lhs, get a
2107 stmt iterator to the memcpy call.
2108 ??? We can't use gsi_for_stmt as that doesn't work when the
2109 CFG isn't built yet. */
2110 gimple_stmt_iterator gsi2
= *gsi
;
2116 gsi_replace_with_seq_vops (gsi
, stmts
);
2122 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
2123 are the arguments to the call. */
2126 gimple_fold_builtin_strcat_chk (gimple_stmt_iterator
*gsi
)
2128 gimple
*stmt
= gsi_stmt (*gsi
);
2129 tree dest
= gimple_call_arg (stmt
, 0);
2130 tree src
= gimple_call_arg (stmt
, 1);
2131 tree size
= gimple_call_arg (stmt
, 2);
2137 /* If the SRC parameter is "", return DEST. */
2138 if (p
&& *p
== '\0')
2140 replace_call_with_value (gsi
, dest
);
2144 if (! tree_fits_uhwi_p (size
) || ! integer_all_onesp (size
))
2147 /* If __builtin_strcat_chk is used, assume strcat is available. */
2148 fn
= builtin_decl_explicit (BUILT_IN_STRCAT
);
2152 gimple
*repl
= gimple_build_call (fn
, 2, dest
, src
);
2153 replace_call_with_call_and_fold (gsi
, repl
);
2157 /* Simplify a call to the strncat builtin. */
2160 gimple_fold_builtin_strncat (gimple_stmt_iterator
*gsi
)
2162 gimple
*stmt
= gsi_stmt (*gsi
);
2163 tree dst
= gimple_call_arg (stmt
, 0);
2164 tree src
= gimple_call_arg (stmt
, 1);
2165 tree len
= gimple_call_arg (stmt
, 2);
2167 const char *p
= c_getstr (src
);
2169 /* If the requested length is zero, or the src parameter string
2170 length is zero, return the dst parameter. */
2171 if (integer_zerop (len
) || (p
&& *p
== '\0'))
2173 replace_call_with_value (gsi
, dst
);
2177 if (TREE_CODE (len
) != INTEGER_CST
|| !p
)
2180 unsigned srclen
= strlen (p
);
2182 int cmpsrc
= compare_tree_int (len
, srclen
);
2184 /* Return early if the requested len is less than the string length.
2185 Warnings will be issued elsewhere later. */
2189 unsigned HOST_WIDE_INT dstsize
;
2191 bool nowarn
= gimple_no_warning_p (stmt
);
2193 if (!nowarn
&& compute_builtin_object_size (dst
, 1, &dstsize
))
2195 int cmpdst
= compare_tree_int (len
, dstsize
);
2199 tree fndecl
= gimple_call_fndecl (stmt
);
2201 /* Strncat copies (at most) LEN bytes and always appends
2202 the terminating NUL so the specified bound should never
2203 be equal to (or greater than) the size of the destination.
2204 If it is, the copy could overflow. */
2205 location_t loc
= gimple_location (stmt
);
2206 nowarn
= warning_at (loc
, OPT_Wstringop_overflow_
,
2208 ? G_("%G%qD specified bound %E equals "
2210 : G_("%G%qD specified bound %E exceeds "
2211 "destination size %wu"),
2212 stmt
, fndecl
, len
, dstsize
);
2214 gimple_set_no_warning (stmt
, true);
2218 if (!nowarn
&& cmpsrc
== 0)
2220 tree fndecl
= gimple_call_fndecl (stmt
);
2221 location_t loc
= gimple_location (stmt
);
2223 /* To avoid possible overflow the specified bound should also
2224 not be equal to the length of the source, even when the size
2225 of the destination is unknown (it's not an uncommon mistake
2226 to specify as the bound to strncpy the length of the source). */
2227 if (warning_at (loc
, OPT_Wstringop_overflow_
,
2228 "%G%qD specified bound %E equals source length",
2230 gimple_set_no_warning (stmt
, true);
2233 tree fn
= builtin_decl_implicit (BUILT_IN_STRCAT
);
2235 /* If the replacement _DECL isn't initialized, don't do the
2240 /* Otherwise, emit a call to strcat. */
2241 gcall
*repl
= gimple_build_call (fn
, 2, dst
, src
);
2242 replace_call_with_call_and_fold (gsi
, repl
);
2246 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
2250 gimple_fold_builtin_strncat_chk (gimple_stmt_iterator
*gsi
)
2252 gimple
*stmt
= gsi_stmt (*gsi
);
2253 tree dest
= gimple_call_arg (stmt
, 0);
2254 tree src
= gimple_call_arg (stmt
, 1);
2255 tree len
= gimple_call_arg (stmt
, 2);
2256 tree size
= gimple_call_arg (stmt
, 3);
2261 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
2262 if ((p
&& *p
== '\0')
2263 || integer_zerop (len
))
2265 replace_call_with_value (gsi
, dest
);
2269 if (! tree_fits_uhwi_p (size
))
2272 if (! integer_all_onesp (size
))
2274 tree src_len
= c_strlen (src
, 1);
2276 && tree_fits_uhwi_p (src_len
)
2277 && tree_fits_uhwi_p (len
)
2278 && ! tree_int_cst_lt (len
, src_len
))
2280 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
2281 fn
= builtin_decl_explicit (BUILT_IN_STRCAT_CHK
);
2285 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, size
);
2286 replace_call_with_call_and_fold (gsi
, repl
);
2292 /* If __builtin_strncat_chk is used, assume strncat is available. */
2293 fn
= builtin_decl_explicit (BUILT_IN_STRNCAT
);
2297 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
2298 replace_call_with_call_and_fold (gsi
, repl
);
2302 /* Build and append gimple statements to STMTS that would load a first
2303 character of a memory location identified by STR. LOC is location
2304 of the statement. */
2307 gimple_load_first_char (location_t loc
, tree str
, gimple_seq
*stmts
)
2311 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
2312 tree cst_uchar_ptr_node
2313 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
2314 tree off0
= build_int_cst (cst_uchar_ptr_node
, 0);
2316 tree temp
= fold_build2_loc (loc
, MEM_REF
, cst_uchar_node
, str
, off0
);
2317 gassign
*stmt
= gimple_build_assign (NULL_TREE
, temp
);
2318 var
= create_tmp_reg_or_ssa_name (cst_uchar_node
, stmt
);
2320 gimple_assign_set_lhs (stmt
, var
);
2321 gimple_seq_add_stmt_without_update (stmts
, stmt
);
2326 /* Fold a call to the str{n}{case}cmp builtin pointed by GSI iterator. */
2329 gimple_fold_builtin_string_compare (gimple_stmt_iterator
*gsi
)
2331 gimple
*stmt
= gsi_stmt (*gsi
);
2332 tree callee
= gimple_call_fndecl (stmt
);
2333 enum built_in_function fcode
= DECL_FUNCTION_CODE (callee
);
2335 tree type
= integer_type_node
;
2336 tree str1
= gimple_call_arg (stmt
, 0);
2337 tree str2
= gimple_call_arg (stmt
, 1);
2338 tree lhs
= gimple_call_lhs (stmt
);
2339 tree len
= NULL_TREE
;
2340 unsigned HOST_WIDE_INT bound
= HOST_WIDE_INT_M1U
;
2342 /* Handle strncmp and strncasecmp functions. */
2343 if (gimple_call_num_args (stmt
) == 3)
2345 len
= gimple_call_arg (stmt
, 2);
2346 if (tree_fits_uhwi_p (len
))
2347 bound
= tree_to_uhwi (len
);
2350 /* If the LEN parameter is zero, return zero. */
2353 replace_call_with_value (gsi
, integer_zero_node
);
2357 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
2358 if (operand_equal_p (str1
, str2
, 0))
2360 replace_call_with_value (gsi
, integer_zero_node
);
2364 /* Initially set to the number of characters, including the terminating
2365 nul if each array has one. LENx == strnlen (Sx, LENx) implies that
2366 the array Sx is not terminated by a nul.
2367 For nul-terminated strings then adjusted to their length so that
2368 LENx == NULPOSx holds. */
2369 unsigned HOST_WIDE_INT len1
= HOST_WIDE_INT_MAX
, len2
= len1
;
2370 const char *p1
= c_getstr (str1
, &len1
);
2371 const char *p2
= c_getstr (str2
, &len2
);
2373 /* The position of the terminating nul character if one exists, otherwise
2374 a value greater than LENx. */
2375 unsigned HOST_WIDE_INT nulpos1
= HOST_WIDE_INT_MAX
, nulpos2
= nulpos1
;
2379 size_t n
= strnlen (p1
, len1
);
2386 size_t n
= strnlen (p2
, len2
);
2391 /* For known strings, return an immediate value. */
2395 bool known_result
= false;
2399 case BUILT_IN_STRCMP
:
2400 case BUILT_IN_STRCMP_EQ
:
2401 if (len1
!= nulpos1
|| len2
!= nulpos2
)
2404 r
= strcmp (p1
, p2
);
2405 known_result
= true;
2408 case BUILT_IN_STRNCMP
:
2409 case BUILT_IN_STRNCMP_EQ
:
2411 /* Reduce the bound to be no more than the length
2412 of the shorter of the two strings, or the sizes
2413 of the unterminated arrays. */
2414 unsigned HOST_WIDE_INT n
= bound
;
2416 if (len1
== nulpos1
&& len1
< n
)
2418 if (len2
== nulpos2
&& len2
< n
)
2421 if (MIN (nulpos1
, nulpos2
) + 1 < n
)
2424 r
= strncmp (p1
, p2
, n
);
2425 known_result
= true;
2428 /* Only handleable situation is where the string are equal (result 0),
2429 which is already handled by operand_equal_p case. */
2430 case BUILT_IN_STRCASECMP
:
2432 case BUILT_IN_STRNCASECMP
:
2434 if (bound
== HOST_WIDE_INT_M1U
)
2436 r
= strncmp (p1
, p2
, bound
);
2438 known_result
= true;
2447 replace_call_with_value (gsi
, build_cmp_result (type
, r
));
2452 bool nonzero_bound
= (bound
>= 1 && bound
< HOST_WIDE_INT_M1U
)
2453 || fcode
== BUILT_IN_STRCMP
2454 || fcode
== BUILT_IN_STRCMP_EQ
2455 || fcode
== BUILT_IN_STRCASECMP
;
2457 location_t loc
= gimple_location (stmt
);
2459 /* If the second arg is "", return *(const unsigned char*)arg1. */
2460 if (p2
&& *p2
== '\0' && nonzero_bound
)
2462 gimple_seq stmts
= NULL
;
2463 tree var
= gimple_load_first_char (loc
, str1
, &stmts
);
2466 stmt
= gimple_build_assign (lhs
, NOP_EXPR
, var
);
2467 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2470 gsi_replace_with_seq_vops (gsi
, stmts
);
2474 /* If the first arg is "", return -*(const unsigned char*)arg2. */
2475 if (p1
&& *p1
== '\0' && nonzero_bound
)
2477 gimple_seq stmts
= NULL
;
2478 tree var
= gimple_load_first_char (loc
, str2
, &stmts
);
2482 tree c
= create_tmp_reg_or_ssa_name (integer_type_node
);
2483 stmt
= gimple_build_assign (c
, NOP_EXPR
, var
);
2484 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2486 stmt
= gimple_build_assign (lhs
, NEGATE_EXPR
, c
);
2487 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2490 gsi_replace_with_seq_vops (gsi
, stmts
);
2494 /* If BOUND is one, return an expression corresponding to
2495 (*(const unsigned char*)arg2 - *(const unsigned char*)arg1). */
2496 if (fcode
== BUILT_IN_STRNCMP
&& bound
== 1)
2498 gimple_seq stmts
= NULL
;
2499 tree temp1
= gimple_load_first_char (loc
, str1
, &stmts
);
2500 tree temp2
= gimple_load_first_char (loc
, str2
, &stmts
);
2504 tree c1
= create_tmp_reg_or_ssa_name (integer_type_node
);
2505 gassign
*convert1
= gimple_build_assign (c1
, NOP_EXPR
, temp1
);
2506 gimple_seq_add_stmt_without_update (&stmts
, convert1
);
2508 tree c2
= create_tmp_reg_or_ssa_name (integer_type_node
);
2509 gassign
*convert2
= gimple_build_assign (c2
, NOP_EXPR
, temp2
);
2510 gimple_seq_add_stmt_without_update (&stmts
, convert2
);
2512 stmt
= gimple_build_assign (lhs
, MINUS_EXPR
, c1
, c2
);
2513 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2516 gsi_replace_with_seq_vops (gsi
, stmts
);
2520 /* If BOUND is greater than the length of one constant string,
2521 and the other argument is also a nul-terminated string, replace
2522 strncmp with strcmp. */
2523 if (fcode
== BUILT_IN_STRNCMP
2524 && bound
> 0 && bound
< HOST_WIDE_INT_M1U
2525 && ((p2
&& len2
< bound
&& len2
== nulpos2
)
2526 || (p1
&& len1
< bound
&& len1
== nulpos1
)))
2528 tree fn
= builtin_decl_implicit (BUILT_IN_STRCMP
);
2531 gimple
*repl
= gimple_build_call (fn
, 2, str1
, str2
);
2532 replace_call_with_call_and_fold (gsi
, repl
);
2539 /* Fold a call to the memchr pointed by GSI iterator. */
2542 gimple_fold_builtin_memchr (gimple_stmt_iterator
*gsi
)
2544 gimple
*stmt
= gsi_stmt (*gsi
);
2545 tree lhs
= gimple_call_lhs (stmt
);
2546 tree arg1
= gimple_call_arg (stmt
, 0);
2547 tree arg2
= gimple_call_arg (stmt
, 1);
2548 tree len
= gimple_call_arg (stmt
, 2);
2550 /* If the LEN parameter is zero, return zero. */
2551 if (integer_zerop (len
))
2553 replace_call_with_value (gsi
, build_int_cst (ptr_type_node
, 0));
2558 if (TREE_CODE (arg2
) != INTEGER_CST
2559 || !tree_fits_uhwi_p (len
)
2560 || !target_char_cst_p (arg2
, &c
))
2563 unsigned HOST_WIDE_INT length
= tree_to_uhwi (len
);
2564 unsigned HOST_WIDE_INT string_length
;
2565 const char *p1
= c_getstr (arg1
, &string_length
);
2569 const char *r
= (const char *)memchr (p1
, c
, MIN (length
, string_length
));
2572 tree mem_size
, offset_node
;
2573 string_constant (arg1
, &offset_node
, &mem_size
, NULL
);
2574 unsigned HOST_WIDE_INT offset
= (offset_node
== NULL_TREE
)
2575 ? 0 : tree_to_uhwi (offset_node
);
2576 /* MEM_SIZE is the size of the array the string literal
2578 unsigned HOST_WIDE_INT string_size
= tree_to_uhwi (mem_size
) - offset
;
2579 gcc_checking_assert (string_length
<= string_size
);
2580 if (length
<= string_size
)
2582 replace_call_with_value (gsi
, build_int_cst (ptr_type_node
, 0));
2588 unsigned HOST_WIDE_INT offset
= r
- p1
;
2589 gimple_seq stmts
= NULL
;
2590 if (lhs
!= NULL_TREE
)
2592 tree offset_cst
= build_int_cst (TREE_TYPE (len
), offset
);
2593 gassign
*stmt
= gimple_build_assign (lhs
, POINTER_PLUS_EXPR
,
2595 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2598 gimple_seq_add_stmt_without_update (&stmts
,
2599 gimple_build_nop ());
2601 gsi_replace_with_seq_vops (gsi
, stmts
);
2609 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
2610 to the call. IGNORE is true if the value returned
2611 by the builtin will be ignored. UNLOCKED is true is true if this
2612 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
2613 the known length of the string. Return NULL_TREE if no simplification
2617 gimple_fold_builtin_fputs (gimple_stmt_iterator
*gsi
,
2618 tree arg0
, tree arg1
,
2621 gimple
*stmt
= gsi_stmt (*gsi
);
2623 /* If we're using an unlocked function, assume the other unlocked
2624 functions exist explicitly. */
2625 tree
const fn_fputc
= (unlocked
2626 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED
)
2627 : builtin_decl_implicit (BUILT_IN_FPUTC
));
2628 tree
const fn_fwrite
= (unlocked
2629 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED
)
2630 : builtin_decl_implicit (BUILT_IN_FWRITE
));
2632 /* If the return value is used, don't do the transformation. */
2633 if (gimple_call_lhs (stmt
))
2636 /* Get the length of the string passed to fputs. If the length
2637 can't be determined, punt. */
2638 tree len
= get_maxval_strlen (arg0
, SRK_STRLEN
);
2640 || TREE_CODE (len
) != INTEGER_CST
)
2643 switch (compare_tree_int (len
, 1))
2645 case -1: /* length is 0, delete the call entirely . */
2646 replace_call_with_value (gsi
, integer_zero_node
);
2649 case 0: /* length is 1, call fputc. */
2651 const char *p
= c_getstr (arg0
);
2657 gimple
*repl
= gimple_build_call (fn_fputc
, 2,
2659 (integer_type_node
, p
[0]), arg1
);
2660 replace_call_with_call_and_fold (gsi
, repl
);
2665 case 1: /* length is greater than 1, call fwrite. */
2667 /* If optimizing for size keep fputs. */
2668 if (optimize_function_for_size_p (cfun
))
2670 /* New argument list transforming fputs(string, stream) to
2671 fwrite(string, 1, len, stream). */
2675 gimple
*repl
= gimple_build_call (fn_fwrite
, 4, arg0
,
2676 size_one_node
, len
, arg1
);
2677 replace_call_with_call_and_fold (gsi
, repl
);
2686 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
2687 DEST, SRC, LEN, and SIZE are the arguments to the call.
2688 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
2689 code of the builtin. If MAXLEN is not NULL, it is maximum length
2690 passed as third argument. */
2693 gimple_fold_builtin_memory_chk (gimple_stmt_iterator
*gsi
,
2694 tree dest
, tree src
, tree len
, tree size
,
2695 enum built_in_function fcode
)
2697 gimple
*stmt
= gsi_stmt (*gsi
);
2698 location_t loc
= gimple_location (stmt
);
2699 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
2702 /* If SRC and DEST are the same (and not volatile), return DEST
2703 (resp. DEST+LEN for __mempcpy_chk). */
2704 if (fcode
!= BUILT_IN_MEMSET_CHK
&& operand_equal_p (src
, dest
, 0))
2706 if (fcode
!= BUILT_IN_MEMPCPY_CHK
)
2708 replace_call_with_value (gsi
, dest
);
2713 gimple_seq stmts
= NULL
;
2714 len
= gimple_convert_to_ptrofftype (&stmts
, loc
, len
);
2715 tree temp
= gimple_build (&stmts
, loc
, POINTER_PLUS_EXPR
,
2716 TREE_TYPE (dest
), dest
, len
);
2717 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2718 replace_call_with_value (gsi
, temp
);
2723 if (! tree_fits_uhwi_p (size
))
2726 tree maxlen
= get_maxval_strlen (len
, SRK_INT_VALUE
);
2727 if (! integer_all_onesp (size
))
2729 if (! tree_fits_uhwi_p (len
))
2731 /* If LEN is not constant, try MAXLEN too.
2732 For MAXLEN only allow optimizing into non-_ocs function
2733 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2734 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
2736 if (fcode
== BUILT_IN_MEMPCPY_CHK
&& ignore
)
2738 /* (void) __mempcpy_chk () can be optimized into
2739 (void) __memcpy_chk (). */
2740 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
2744 gimple
*repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
2745 replace_call_with_call_and_fold (gsi
, repl
);
2754 if (tree_int_cst_lt (size
, maxlen
))
2759 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
2760 mem{cpy,pcpy,move,set} is available. */
2763 case BUILT_IN_MEMCPY_CHK
:
2764 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY
);
2766 case BUILT_IN_MEMPCPY_CHK
:
2767 fn
= builtin_decl_explicit (BUILT_IN_MEMPCPY
);
2769 case BUILT_IN_MEMMOVE_CHK
:
2770 fn
= builtin_decl_explicit (BUILT_IN_MEMMOVE
);
2772 case BUILT_IN_MEMSET_CHK
:
2773 fn
= builtin_decl_explicit (BUILT_IN_MEMSET
);
2782 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
2783 replace_call_with_call_and_fold (gsi
, repl
);
2787 /* Fold a call to the __st[rp]cpy_chk builtin.
2788 DEST, SRC, and SIZE are the arguments to the call.
2789 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
2790 code of the builtin. If MAXLEN is not NULL, it is maximum length of
2791 strings passed as second argument. */
2794 gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator
*gsi
,
2796 tree src
, tree size
,
2797 enum built_in_function fcode
)
2799 gimple
*stmt
= gsi_stmt (*gsi
);
2800 location_t loc
= gimple_location (stmt
);
2801 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
2804 /* If SRC and DEST are the same (and not volatile), return DEST. */
2805 if (fcode
== BUILT_IN_STRCPY_CHK
&& operand_equal_p (src
, dest
, 0))
2807 /* Issue -Wrestrict unless the pointers are null (those do
2808 not point to objects and so do not indicate an overlap;
2809 such calls could be the result of sanitization and jump
2811 if (!integer_zerop (dest
) && !gimple_no_warning_p (stmt
))
2813 tree func
= gimple_call_fndecl (stmt
);
2815 warning_at (loc
, OPT_Wrestrict
,
2816 "%qD source argument is the same as destination",
2820 replace_call_with_value (gsi
, dest
);
2824 if (! tree_fits_uhwi_p (size
))
2827 tree maxlen
= get_maxval_strlen (src
, SRK_STRLENMAX
);
2828 if (! integer_all_onesp (size
))
2830 len
= c_strlen (src
, 1);
2831 if (! len
|| ! tree_fits_uhwi_p (len
))
2833 /* If LEN is not constant, try MAXLEN too.
2834 For MAXLEN only allow optimizing into non-_ocs function
2835 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2836 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
2838 if (fcode
== BUILT_IN_STPCPY_CHK
)
2843 /* If return value of __stpcpy_chk is ignored,
2844 optimize into __strcpy_chk. */
2845 fn
= builtin_decl_explicit (BUILT_IN_STRCPY_CHK
);
2849 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, size
);
2850 replace_call_with_call_and_fold (gsi
, repl
);
2854 if (! len
|| TREE_SIDE_EFFECTS (len
))
2857 /* If c_strlen returned something, but not a constant,
2858 transform __strcpy_chk into __memcpy_chk. */
2859 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
2863 gimple_seq stmts
= NULL
;
2864 len
= force_gimple_operand (len
, &stmts
, true, NULL_TREE
);
2865 len
= gimple_convert (&stmts
, loc
, size_type_node
, len
);
2866 len
= gimple_build (&stmts
, loc
, PLUS_EXPR
, size_type_node
, len
,
2867 build_int_cst (size_type_node
, 1));
2868 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2869 gimple
*repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
2870 replace_call_with_call_and_fold (gsi
, repl
);
2877 if (! tree_int_cst_lt (maxlen
, size
))
2881 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
2882 fn
= builtin_decl_explicit (fcode
== BUILT_IN_STPCPY_CHK
2883 ? BUILT_IN_STPCPY
: BUILT_IN_STRCPY
);
2887 gimple
*repl
= gimple_build_call (fn
, 2, dest
, src
);
2888 replace_call_with_call_and_fold (gsi
, repl
);
2892 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
2893 are the arguments to the call. If MAXLEN is not NULL, it is maximum
2894 length passed as third argument. IGNORE is true if return value can be
2895 ignored. FCODE is the BUILT_IN_* code of the builtin. */
2898 gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator
*gsi
,
2899 tree dest
, tree src
,
2900 tree len
, tree size
,
2901 enum built_in_function fcode
)
2903 gimple
*stmt
= gsi_stmt (*gsi
);
2904 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
2907 if (fcode
== BUILT_IN_STPNCPY_CHK
&& ignore
)
2909 /* If return value of __stpncpy_chk is ignored,
2910 optimize into __strncpy_chk. */
2911 fn
= builtin_decl_explicit (BUILT_IN_STRNCPY_CHK
);
2914 gimple
*repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
2915 replace_call_with_call_and_fold (gsi
, repl
);
2920 if (! tree_fits_uhwi_p (size
))
2923 tree maxlen
= get_maxval_strlen (len
, SRK_INT_VALUE
);
2924 if (! integer_all_onesp (size
))
2926 if (! tree_fits_uhwi_p (len
))
2928 /* If LEN is not constant, try MAXLEN too.
2929 For MAXLEN only allow optimizing into non-_ocs function
2930 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2931 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
2937 if (tree_int_cst_lt (size
, maxlen
))
2941 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
2942 fn
= builtin_decl_explicit (fcode
== BUILT_IN_STPNCPY_CHK
2943 ? BUILT_IN_STPNCPY
: BUILT_IN_STRNCPY
);
2947 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
2948 replace_call_with_call_and_fold (gsi
, repl
);
2952 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
2953 Return NULL_TREE if no simplification can be made. */
2956 gimple_fold_builtin_stpcpy (gimple_stmt_iterator
*gsi
)
2958 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
2959 location_t loc
= gimple_location (stmt
);
2960 tree dest
= gimple_call_arg (stmt
, 0);
2961 tree src
= gimple_call_arg (stmt
, 1);
2964 /* If the result is unused, replace stpcpy with strcpy. */
2965 if (gimple_call_lhs (stmt
) == NULL_TREE
)
2967 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
2970 gimple_call_set_fndecl (stmt
, fn
);
2975 /* Set to non-null if ARG refers to an unterminated array. */
2976 c_strlen_data data
= { };
2977 tree len
= c_strlen (src
, 1, &data
, 1);
2979 || TREE_CODE (len
) != INTEGER_CST
)
2981 data
.decl
= unterminated_array (src
);
2988 /* Avoid folding calls with unterminated arrays. */
2989 if (!gimple_no_warning_p (stmt
))
2990 warn_string_no_nul (loc
, "stpcpy", src
, data
.decl
);
2991 gimple_set_no_warning (stmt
, true);
2995 if (optimize_function_for_size_p (cfun
)
2996 /* If length is zero it's small enough. */
2997 && !integer_zerop (len
))
3000 /* If the source has a known length replace stpcpy with memcpy. */
3001 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
3005 gimple_seq stmts
= NULL
;
3006 tree tem
= gimple_convert (&stmts
, loc
, size_type_node
, len
);
3007 lenp1
= gimple_build (&stmts
, loc
, PLUS_EXPR
, size_type_node
,
3008 tem
, build_int_cst (size_type_node
, 1));
3009 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
3010 gcall
*repl
= gimple_build_call (fn
, 3, dest
, src
, lenp1
);
3011 gimple_move_vops (repl
, stmt
);
3012 gsi_insert_before (gsi
, repl
, GSI_SAME_STMT
);
3013 /* Replace the result with dest + len. */
3015 tem
= gimple_convert (&stmts
, loc
, sizetype
, len
);
3016 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
3017 gassign
*ret
= gimple_build_assign (gimple_call_lhs (stmt
),
3018 POINTER_PLUS_EXPR
, dest
, tem
);
3019 gsi_replace (gsi
, ret
, false);
3020 /* Finally fold the memcpy call. */
3021 gimple_stmt_iterator gsi2
= *gsi
;
3027 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
3028 NULL_TREE if a normal call should be emitted rather than expanding
3029 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
3030 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
3031 passed as second argument. */
3034 gimple_fold_builtin_snprintf_chk (gimple_stmt_iterator
*gsi
,
3035 enum built_in_function fcode
)
3037 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3038 tree dest
, size
, len
, fn
, fmt
, flag
;
3039 const char *fmt_str
;
3041 /* Verify the required arguments in the original call. */
3042 if (gimple_call_num_args (stmt
) < 5)
3045 dest
= gimple_call_arg (stmt
, 0);
3046 len
= gimple_call_arg (stmt
, 1);
3047 flag
= gimple_call_arg (stmt
, 2);
3048 size
= gimple_call_arg (stmt
, 3);
3049 fmt
= gimple_call_arg (stmt
, 4);
3051 if (! tree_fits_uhwi_p (size
))
3054 if (! integer_all_onesp (size
))
3056 tree maxlen
= get_maxval_strlen (len
, SRK_INT_VALUE
);
3057 if (! tree_fits_uhwi_p (len
))
3059 /* If LEN is not constant, try MAXLEN too.
3060 For MAXLEN only allow optimizing into non-_ocs function
3061 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
3062 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
3068 if (tree_int_cst_lt (size
, maxlen
))
3072 if (!init_target_chars ())
3075 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
3076 or if format doesn't contain % chars or is "%s". */
3077 if (! integer_zerop (flag
))
3079 fmt_str
= c_getstr (fmt
);
3080 if (fmt_str
== NULL
)
3082 if (strchr (fmt_str
, target_percent
) != NULL
3083 && strcmp (fmt_str
, target_percent_s
))
3087 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
3089 fn
= builtin_decl_explicit (fcode
== BUILT_IN_VSNPRINTF_CHK
3090 ? BUILT_IN_VSNPRINTF
: BUILT_IN_SNPRINTF
);
3094 /* Replace the called function and the first 5 argument by 3 retaining
3095 trailing varargs. */
3096 gimple_call_set_fndecl (stmt
, fn
);
3097 gimple_call_set_fntype (stmt
, TREE_TYPE (fn
));
3098 gimple_call_set_arg (stmt
, 0, dest
);
3099 gimple_call_set_arg (stmt
, 1, len
);
3100 gimple_call_set_arg (stmt
, 2, fmt
);
3101 for (unsigned i
= 3; i
< gimple_call_num_args (stmt
) - 2; ++i
)
3102 gimple_call_set_arg (stmt
, i
, gimple_call_arg (stmt
, i
+ 2));
3103 gimple_set_num_ops (stmt
, gimple_num_ops (stmt
) - 2);
3108 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
3109 Return NULL_TREE if a normal call should be emitted rather than
3110 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
3111 or BUILT_IN_VSPRINTF_CHK. */
3114 gimple_fold_builtin_sprintf_chk (gimple_stmt_iterator
*gsi
,
3115 enum built_in_function fcode
)
3117 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3118 tree dest
, size
, len
, fn
, fmt
, flag
;
3119 const char *fmt_str
;
3120 unsigned nargs
= gimple_call_num_args (stmt
);
3122 /* Verify the required arguments in the original call. */
3125 dest
= gimple_call_arg (stmt
, 0);
3126 flag
= gimple_call_arg (stmt
, 1);
3127 size
= gimple_call_arg (stmt
, 2);
3128 fmt
= gimple_call_arg (stmt
, 3);
3130 if (! tree_fits_uhwi_p (size
))
3135 if (!init_target_chars ())
3138 /* Check whether the format is a literal string constant. */
3139 fmt_str
= c_getstr (fmt
);
3140 if (fmt_str
!= NULL
)
3142 /* If the format doesn't contain % args or %%, we know the size. */
3143 if (strchr (fmt_str
, target_percent
) == 0)
3145 if (fcode
!= BUILT_IN_SPRINTF_CHK
|| nargs
== 4)
3146 len
= build_int_cstu (size_type_node
, strlen (fmt_str
));
3148 /* If the format is "%s" and first ... argument is a string literal,
3149 we know the size too. */
3150 else if (fcode
== BUILT_IN_SPRINTF_CHK
3151 && strcmp (fmt_str
, target_percent_s
) == 0)
3157 arg
= gimple_call_arg (stmt
, 4);
3158 if (POINTER_TYPE_P (TREE_TYPE (arg
)))
3160 len
= c_strlen (arg
, 1);
3161 if (! len
|| ! tree_fits_uhwi_p (len
))
3168 if (! integer_all_onesp (size
))
3170 if (! len
|| ! tree_int_cst_lt (len
, size
))
3174 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
3175 or if format doesn't contain % chars or is "%s". */
3176 if (! integer_zerop (flag
))
3178 if (fmt_str
== NULL
)
3180 if (strchr (fmt_str
, target_percent
) != NULL
3181 && strcmp (fmt_str
, target_percent_s
))
3185 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
3186 fn
= builtin_decl_explicit (fcode
== BUILT_IN_VSPRINTF_CHK
3187 ? BUILT_IN_VSPRINTF
: BUILT_IN_SPRINTF
);
3191 /* Replace the called function and the first 4 argument by 2 retaining
3192 trailing varargs. */
3193 gimple_call_set_fndecl (stmt
, fn
);
3194 gimple_call_set_fntype (stmt
, TREE_TYPE (fn
));
3195 gimple_call_set_arg (stmt
, 0, dest
);
3196 gimple_call_set_arg (stmt
, 1, fmt
);
3197 for (unsigned i
= 2; i
< gimple_call_num_args (stmt
) - 2; ++i
)
3198 gimple_call_set_arg (stmt
, i
, gimple_call_arg (stmt
, i
+ 2));
3199 gimple_set_num_ops (stmt
, gimple_num_ops (stmt
) - 2);
3204 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
3205 ORIG may be null if this is a 2-argument call. We don't attempt to
3206 simplify calls with more than 3 arguments.
3208 Return true if simplification was possible, otherwise false. */
3211 gimple_fold_builtin_sprintf (gimple_stmt_iterator
*gsi
)
3213 gimple
*stmt
= gsi_stmt (*gsi
);
3214 tree dest
= gimple_call_arg (stmt
, 0);
3215 tree fmt
= gimple_call_arg (stmt
, 1);
3216 tree orig
= NULL_TREE
;
3217 const char *fmt_str
= NULL
;
3219 /* Verify the required arguments in the original call. We deal with two
3220 types of sprintf() calls: 'sprintf (str, fmt)' and
3221 'sprintf (dest, "%s", orig)'. */
3222 if (gimple_call_num_args (stmt
) > 3)
3225 if (gimple_call_num_args (stmt
) == 3)
3226 orig
= gimple_call_arg (stmt
, 2);
3228 /* Check whether the format is a literal string constant. */
3229 fmt_str
= c_getstr (fmt
);
3230 if (fmt_str
== NULL
)
3233 if (!init_target_chars ())
3236 /* If the format doesn't contain % args or %%, use strcpy. */
3237 if (strchr (fmt_str
, target_percent
) == NULL
)
3239 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3244 /* Don't optimize sprintf (buf, "abc", ptr++). */
3248 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
3249 'format' is known to contain no % formats. */
3250 gimple_seq stmts
= NULL
;
3251 gimple
*repl
= gimple_build_call (fn
, 2, dest
, fmt
);
3253 /* Propagate the NO_WARNING bit to avoid issuing the same
3254 warning more than once. */
3255 if (gimple_no_warning_p (stmt
))
3256 gimple_set_no_warning (repl
, true);
3258 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3259 if (tree lhs
= gimple_call_lhs (stmt
))
3261 repl
= gimple_build_assign (lhs
, build_int_cst (TREE_TYPE (lhs
),
3263 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3264 gsi_replace_with_seq_vops (gsi
, stmts
);
3265 /* gsi now points at the assignment to the lhs, get a
3266 stmt iterator to the memcpy call.
3267 ??? We can't use gsi_for_stmt as that doesn't work when the
3268 CFG isn't built yet. */
3269 gimple_stmt_iterator gsi2
= *gsi
;
3275 gsi_replace_with_seq_vops (gsi
, stmts
);
3281 /* If the format is "%s", use strcpy if the result isn't used. */
3282 else if (fmt_str
&& strcmp (fmt_str
, target_percent_s
) == 0)
3285 fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3290 /* Don't crash on sprintf (str1, "%s"). */
3294 tree orig_len
= NULL_TREE
;
3295 if (gimple_call_lhs (stmt
))
3297 orig_len
= get_maxval_strlen (orig
, SRK_STRLEN
);
3302 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
3303 gimple_seq stmts
= NULL
;
3304 gimple
*repl
= gimple_build_call (fn
, 2, dest
, orig
);
3306 /* Propagate the NO_WARNING bit to avoid issuing the same
3307 warning more than once. */
3308 if (gimple_no_warning_p (stmt
))
3309 gimple_set_no_warning (repl
, true);
3311 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3312 if (tree lhs
= gimple_call_lhs (stmt
))
3314 if (!useless_type_conversion_p (TREE_TYPE (lhs
),
3315 TREE_TYPE (orig_len
)))
3316 orig_len
= fold_convert (TREE_TYPE (lhs
), orig_len
);
3317 repl
= gimple_build_assign (lhs
, orig_len
);
3318 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3319 gsi_replace_with_seq_vops (gsi
, stmts
);
3320 /* gsi now points at the assignment to the lhs, get a
3321 stmt iterator to the memcpy call.
3322 ??? We can't use gsi_for_stmt as that doesn't work when the
3323 CFG isn't built yet. */
3324 gimple_stmt_iterator gsi2
= *gsi
;
3330 gsi_replace_with_seq_vops (gsi
, stmts
);
3338 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
3339 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
3340 attempt to simplify calls with more than 4 arguments.
3342 Return true if simplification was possible, otherwise false. */
3345 gimple_fold_builtin_snprintf (gimple_stmt_iterator
*gsi
)
3347 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3348 tree dest
= gimple_call_arg (stmt
, 0);
3349 tree destsize
= gimple_call_arg (stmt
, 1);
3350 tree fmt
= gimple_call_arg (stmt
, 2);
3351 tree orig
= NULL_TREE
;
3352 const char *fmt_str
= NULL
;
3354 if (gimple_call_num_args (stmt
) > 4)
3357 if (gimple_call_num_args (stmt
) == 4)
3358 orig
= gimple_call_arg (stmt
, 3);
3360 if (!tree_fits_uhwi_p (destsize
))
3362 unsigned HOST_WIDE_INT destlen
= tree_to_uhwi (destsize
);
3364 /* Check whether the format is a literal string constant. */
3365 fmt_str
= c_getstr (fmt
);
3366 if (fmt_str
== NULL
)
3369 if (!init_target_chars ())
3372 /* If the format doesn't contain % args or %%, use strcpy. */
3373 if (strchr (fmt_str
, target_percent
) == NULL
)
3375 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3379 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
3383 /* We could expand this as
3384 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
3386 memcpy (str, fmt_with_nul_at_cstm1, cst);
3387 but in the former case that might increase code size
3388 and in the latter case grow .rodata section too much.
3390 size_t len
= strlen (fmt_str
);
3394 gimple_seq stmts
= NULL
;
3395 gimple
*repl
= gimple_build_call (fn
, 2, dest
, fmt
);
3396 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3397 if (tree lhs
= gimple_call_lhs (stmt
))
3399 repl
= gimple_build_assign (lhs
,
3400 build_int_cst (TREE_TYPE (lhs
), len
));
3401 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3402 gsi_replace_with_seq_vops (gsi
, stmts
);
3403 /* gsi now points at the assignment to the lhs, get a
3404 stmt iterator to the memcpy call.
3405 ??? We can't use gsi_for_stmt as that doesn't work when the
3406 CFG isn't built yet. */
3407 gimple_stmt_iterator gsi2
= *gsi
;
3413 gsi_replace_with_seq_vops (gsi
, stmts
);
3419 /* If the format is "%s", use strcpy if the result isn't used. */
3420 else if (fmt_str
&& strcmp (fmt_str
, target_percent_s
) == 0)
3422 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3426 /* Don't crash on snprintf (str1, cst, "%s"). */
3430 tree orig_len
= get_maxval_strlen (orig
, SRK_STRLEN
);
3431 if (!orig_len
|| TREE_CODE (orig_len
) != INTEGER_CST
)
3434 /* We could expand this as
3435 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
3437 memcpy (str1, str2_with_nul_at_cstm1, cst);
3438 but in the former case that might increase code size
3439 and in the latter case grow .rodata section too much.
3441 if (compare_tree_int (orig_len
, destlen
) >= 0)
3444 /* Convert snprintf (str1, cst, "%s", str2) into
3445 strcpy (str1, str2) if strlen (str2) < cst. */
3446 gimple_seq stmts
= NULL
;
3447 gimple
*repl
= gimple_build_call (fn
, 2, dest
, orig
);
3448 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3449 if (tree lhs
= gimple_call_lhs (stmt
))
3451 if (!useless_type_conversion_p (TREE_TYPE (lhs
),
3452 TREE_TYPE (orig_len
)))
3453 orig_len
= fold_convert (TREE_TYPE (lhs
), orig_len
);
3454 repl
= gimple_build_assign (lhs
, orig_len
);
3455 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3456 gsi_replace_with_seq_vops (gsi
, stmts
);
3457 /* gsi now points at the assignment to the lhs, get a
3458 stmt iterator to the memcpy call.
3459 ??? We can't use gsi_for_stmt as that doesn't work when the
3460 CFG isn't built yet. */
3461 gimple_stmt_iterator gsi2
= *gsi
;
3467 gsi_replace_with_seq_vops (gsi
, stmts
);
3475 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
3476 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
3477 more than 3 arguments, and ARG may be null in the 2-argument case.
3479 Return NULL_TREE if no simplification was possible, otherwise return the
3480 simplified form of the call as a tree. FCODE is the BUILT_IN_*
3481 code of the function to be simplified. */
3484 gimple_fold_builtin_fprintf (gimple_stmt_iterator
*gsi
,
3485 tree fp
, tree fmt
, tree arg
,
3486 enum built_in_function fcode
)
3488 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3489 tree fn_fputc
, fn_fputs
;
3490 const char *fmt_str
= NULL
;
3492 /* If the return value is used, don't do the transformation. */
3493 if (gimple_call_lhs (stmt
) != NULL_TREE
)
3496 /* Check whether the format is a literal string constant. */
3497 fmt_str
= c_getstr (fmt
);
3498 if (fmt_str
== NULL
)
3501 if (fcode
== BUILT_IN_FPRINTF_UNLOCKED
)
3503 /* If we're using an unlocked function, assume the other
3504 unlocked functions exist explicitly. */
3505 fn_fputc
= builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED
);
3506 fn_fputs
= builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED
);
3510 fn_fputc
= builtin_decl_implicit (BUILT_IN_FPUTC
);
3511 fn_fputs
= builtin_decl_implicit (BUILT_IN_FPUTS
);
3514 if (!init_target_chars ())
3517 /* If the format doesn't contain % args or %%, use strcpy. */
3518 if (strchr (fmt_str
, target_percent
) == NULL
)
3520 if (fcode
!= BUILT_IN_VFPRINTF
&& fcode
!= BUILT_IN_VFPRINTF_CHK
3524 /* If the format specifier was "", fprintf does nothing. */
3525 if (fmt_str
[0] == '\0')
3527 replace_call_with_value (gsi
, NULL_TREE
);
3531 /* When "string" doesn't contain %, replace all cases of
3532 fprintf (fp, string) with fputs (string, fp). The fputs
3533 builtin will take care of special cases like length == 1. */
3536 gcall
*repl
= gimple_build_call (fn_fputs
, 2, fmt
, fp
);
3537 replace_call_with_call_and_fold (gsi
, repl
);
3542 /* The other optimizations can be done only on the non-va_list variants. */
3543 else if (fcode
== BUILT_IN_VFPRINTF
|| fcode
== BUILT_IN_VFPRINTF_CHK
)
3546 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
3547 else if (strcmp (fmt_str
, target_percent_s
) == 0)
3549 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
3553 gcall
*repl
= gimple_build_call (fn_fputs
, 2, arg
, fp
);
3554 replace_call_with_call_and_fold (gsi
, repl
);
3559 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
3560 else if (strcmp (fmt_str
, target_percent_c
) == 0)
3563 || ! useless_type_conversion_p (integer_type_node
, TREE_TYPE (arg
)))
3567 gcall
*repl
= gimple_build_call (fn_fputc
, 2, arg
, fp
);
3568 replace_call_with_call_and_fold (gsi
, repl
);
3576 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
3577 FMT and ARG are the arguments to the call; we don't fold cases with
3578 more than 2 arguments, and ARG may be null if this is a 1-argument case.
3580 Return NULL_TREE if no simplification was possible, otherwise return the
3581 simplified form of the call as a tree. FCODE is the BUILT_IN_*
3582 code of the function to be simplified. */
3585 gimple_fold_builtin_printf (gimple_stmt_iterator
*gsi
, tree fmt
,
3586 tree arg
, enum built_in_function fcode
)
3588 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3589 tree fn_putchar
, fn_puts
, newarg
;
3590 const char *fmt_str
= NULL
;
3592 /* If the return value is used, don't do the transformation. */
3593 if (gimple_call_lhs (stmt
) != NULL_TREE
)
3596 /* Check whether the format is a literal string constant. */
3597 fmt_str
= c_getstr (fmt
);
3598 if (fmt_str
== NULL
)
3601 if (fcode
== BUILT_IN_PRINTF_UNLOCKED
)
3603 /* If we're using an unlocked function, assume the other
3604 unlocked functions exist explicitly. */
3605 fn_putchar
= builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED
);
3606 fn_puts
= builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED
);
3610 fn_putchar
= builtin_decl_implicit (BUILT_IN_PUTCHAR
);
3611 fn_puts
= builtin_decl_implicit (BUILT_IN_PUTS
);
3614 if (!init_target_chars ())
3617 if (strcmp (fmt_str
, target_percent_s
) == 0
3618 || strchr (fmt_str
, target_percent
) == NULL
)
3622 if (strcmp (fmt_str
, target_percent_s
) == 0)
3624 if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
3627 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
3630 str
= c_getstr (arg
);
3636 /* The format specifier doesn't contain any '%' characters. */
3637 if (fcode
!= BUILT_IN_VPRINTF
&& fcode
!= BUILT_IN_VPRINTF_CHK
3643 /* If the string was "", printf does nothing. */
3646 replace_call_with_value (gsi
, NULL_TREE
);
3650 /* If the string has length of 1, call putchar. */
3653 /* Given printf("c"), (where c is any one character,)
3654 convert "c"[0] to an int and pass that to the replacement
3656 newarg
= build_int_cst (integer_type_node
, str
[0]);
3659 gcall
*repl
= gimple_build_call (fn_putchar
, 1, newarg
);
3660 replace_call_with_call_and_fold (gsi
, repl
);
3666 /* If the string was "string\n", call puts("string"). */
3667 size_t len
= strlen (str
);
3668 if ((unsigned char)str
[len
- 1] == target_newline
3669 && (size_t) (int) len
== len
3674 /* Create a NUL-terminated string that's one char shorter
3675 than the original, stripping off the trailing '\n'. */
3676 newstr
= xstrdup (str
);
3677 newstr
[len
- 1] = '\0';
3678 newarg
= build_string_literal (len
, newstr
);
3682 gcall
*repl
= gimple_build_call (fn_puts
, 1, newarg
);
3683 replace_call_with_call_and_fold (gsi
, repl
);
3688 /* We'd like to arrange to call fputs(string,stdout) here,
3689 but we need stdout and don't have a way to get it yet. */
3694 /* The other optimizations can be done only on the non-va_list variants. */
3695 else if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
3698 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
3699 else if (strcmp (fmt_str
, target_percent_s_newline
) == 0)
3701 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
3705 gcall
*repl
= gimple_build_call (fn_puts
, 1, arg
);
3706 replace_call_with_call_and_fold (gsi
, repl
);
3711 /* If the format specifier was "%c", call __builtin_putchar(arg). */
3712 else if (strcmp (fmt_str
, target_percent_c
) == 0)
3714 if (!arg
|| ! useless_type_conversion_p (integer_type_node
,
3719 gcall
*repl
= gimple_build_call (fn_putchar
, 1, arg
);
3720 replace_call_with_call_and_fold (gsi
, repl
);
3730 /* Fold a call to __builtin_strlen with known length LEN. */
3733 gimple_fold_builtin_strlen (gimple_stmt_iterator
*gsi
)
3735 gimple
*stmt
= gsi_stmt (*gsi
);
3736 tree arg
= gimple_call_arg (stmt
, 0);
3741 c_strlen_data lendata
= { };
3742 if (get_range_strlen (arg
, &lendata
, /* eltsize = */ 1)
3744 && lendata
.minlen
&& TREE_CODE (lendata
.minlen
) == INTEGER_CST
3745 && lendata
.maxlen
&& TREE_CODE (lendata
.maxlen
) == INTEGER_CST
)
3747 /* The range of lengths refers to either a single constant
3748 string or to the longest and shortest constant string
3749 referenced by the argument of the strlen() call, or to
3750 the strings that can possibly be stored in the arrays
3751 the argument refers to. */
3752 minlen
= wi::to_wide (lendata
.minlen
);
3753 maxlen
= wi::to_wide (lendata
.maxlen
);
3757 unsigned prec
= TYPE_PRECISION (sizetype
);
3759 minlen
= wi::shwi (0, prec
);
3760 maxlen
= wi::to_wide (max_object_size (), prec
) - 2;
3763 if (minlen
== maxlen
)
3765 /* Fold the strlen call to a constant. */
3766 tree type
= TREE_TYPE (lendata
.minlen
);
3767 tree len
= force_gimple_operand_gsi (gsi
,
3768 wide_int_to_tree (type
, minlen
),
3769 true, NULL
, true, GSI_SAME_STMT
);
3770 replace_call_with_value (gsi
, len
);
3774 /* Set the strlen() range to [0, MAXLEN]. */
3775 if (tree lhs
= gimple_call_lhs (stmt
))
3776 set_strlen_range (lhs
, minlen
, maxlen
);
3781 /* Fold a call to __builtin_acc_on_device. */
3784 gimple_fold_builtin_acc_on_device (gimple_stmt_iterator
*gsi
, tree arg0
)
3786 /* Defer folding until we know which compiler we're in. */
3787 if (symtab
->state
!= EXPANSION
)
3790 unsigned val_host
= GOMP_DEVICE_HOST
;
3791 unsigned val_dev
= GOMP_DEVICE_NONE
;
3793 #ifdef ACCEL_COMPILER
3794 val_host
= GOMP_DEVICE_NOT_HOST
;
3795 val_dev
= ACCEL_COMPILER_acc_device
;
3798 location_t loc
= gimple_location (gsi_stmt (*gsi
));
3800 tree host_eq
= make_ssa_name (boolean_type_node
);
3801 gimple
*host_ass
= gimple_build_assign
3802 (host_eq
, EQ_EXPR
, arg0
, build_int_cst (TREE_TYPE (arg0
), val_host
));
3803 gimple_set_location (host_ass
, loc
);
3804 gsi_insert_before (gsi
, host_ass
, GSI_SAME_STMT
);
3806 tree dev_eq
= make_ssa_name (boolean_type_node
);
3807 gimple
*dev_ass
= gimple_build_assign
3808 (dev_eq
, EQ_EXPR
, arg0
, build_int_cst (TREE_TYPE (arg0
), val_dev
));
3809 gimple_set_location (dev_ass
, loc
);
3810 gsi_insert_before (gsi
, dev_ass
, GSI_SAME_STMT
);
3812 tree result
= make_ssa_name (boolean_type_node
);
3813 gimple
*result_ass
= gimple_build_assign
3814 (result
, BIT_IOR_EXPR
, host_eq
, dev_eq
);
3815 gimple_set_location (result_ass
, loc
);
3816 gsi_insert_before (gsi
, result_ass
, GSI_SAME_STMT
);
3818 replace_call_with_value (gsi
, result
);
3823 /* Fold realloc (0, n) -> malloc (n). */
3826 gimple_fold_builtin_realloc (gimple_stmt_iterator
*gsi
)
3828 gimple
*stmt
= gsi_stmt (*gsi
);
3829 tree arg
= gimple_call_arg (stmt
, 0);
3830 tree size
= gimple_call_arg (stmt
, 1);
3832 if (operand_equal_p (arg
, null_pointer_node
, 0))
3834 tree fn_malloc
= builtin_decl_implicit (BUILT_IN_MALLOC
);
3837 gcall
*repl
= gimple_build_call (fn_malloc
, 1, size
);
3838 replace_call_with_call_and_fold (gsi
, repl
);
3845 /* Fold the non-target builtin at *GSI and return whether any simplification
3849 gimple_fold_builtin (gimple_stmt_iterator
*gsi
)
3851 gcall
*stmt
= as_a
<gcall
*>(gsi_stmt (*gsi
));
3852 tree callee
= gimple_call_fndecl (stmt
);
3854 /* Give up for always_inline inline builtins until they are
3856 if (avoid_folding_inline_builtin (callee
))
3859 unsigned n
= gimple_call_num_args (stmt
);
3860 enum built_in_function fcode
= DECL_FUNCTION_CODE (callee
);
3864 return gimple_fold_builtin_bcmp (gsi
);
3865 case BUILT_IN_BCOPY
:
3866 return gimple_fold_builtin_bcopy (gsi
);
3867 case BUILT_IN_BZERO
:
3868 return gimple_fold_builtin_bzero (gsi
);
3870 case BUILT_IN_MEMSET
:
3871 return gimple_fold_builtin_memset (gsi
,
3872 gimple_call_arg (stmt
, 1),
3873 gimple_call_arg (stmt
, 2));
3874 case BUILT_IN_MEMCPY
:
3875 case BUILT_IN_MEMPCPY
:
3876 case BUILT_IN_MEMMOVE
:
3877 return gimple_fold_builtin_memory_op (gsi
, gimple_call_arg (stmt
, 0),
3878 gimple_call_arg (stmt
, 1), fcode
);
3879 case BUILT_IN_SPRINTF_CHK
:
3880 case BUILT_IN_VSPRINTF_CHK
:
3881 return gimple_fold_builtin_sprintf_chk (gsi
, fcode
);
3882 case BUILT_IN_STRCAT_CHK
:
3883 return gimple_fold_builtin_strcat_chk (gsi
);
3884 case BUILT_IN_STRNCAT_CHK
:
3885 return gimple_fold_builtin_strncat_chk (gsi
);
3886 case BUILT_IN_STRLEN
:
3887 return gimple_fold_builtin_strlen (gsi
);
3888 case BUILT_IN_STRCPY
:
3889 return gimple_fold_builtin_strcpy (gsi
,
3890 gimple_call_arg (stmt
, 0),
3891 gimple_call_arg (stmt
, 1));
3892 case BUILT_IN_STRNCPY
:
3893 return gimple_fold_builtin_strncpy (gsi
,
3894 gimple_call_arg (stmt
, 0),
3895 gimple_call_arg (stmt
, 1),
3896 gimple_call_arg (stmt
, 2));
3897 case BUILT_IN_STRCAT
:
3898 return gimple_fold_builtin_strcat (gsi
, gimple_call_arg (stmt
, 0),
3899 gimple_call_arg (stmt
, 1));
3900 case BUILT_IN_STRNCAT
:
3901 return gimple_fold_builtin_strncat (gsi
);
3902 case BUILT_IN_INDEX
:
3903 case BUILT_IN_STRCHR
:
3904 return gimple_fold_builtin_strchr (gsi
, false);
3905 case BUILT_IN_RINDEX
:
3906 case BUILT_IN_STRRCHR
:
3907 return gimple_fold_builtin_strchr (gsi
, true);
3908 case BUILT_IN_STRSTR
:
3909 return gimple_fold_builtin_strstr (gsi
);
3910 case BUILT_IN_STRCMP
:
3911 case BUILT_IN_STRCMP_EQ
:
3912 case BUILT_IN_STRCASECMP
:
3913 case BUILT_IN_STRNCMP
:
3914 case BUILT_IN_STRNCMP_EQ
:
3915 case BUILT_IN_STRNCASECMP
:
3916 return gimple_fold_builtin_string_compare (gsi
);
3917 case BUILT_IN_MEMCHR
:
3918 return gimple_fold_builtin_memchr (gsi
);
3919 case BUILT_IN_FPUTS
:
3920 return gimple_fold_builtin_fputs (gsi
, gimple_call_arg (stmt
, 0),
3921 gimple_call_arg (stmt
, 1), false);
3922 case BUILT_IN_FPUTS_UNLOCKED
:
3923 return gimple_fold_builtin_fputs (gsi
, gimple_call_arg (stmt
, 0),
3924 gimple_call_arg (stmt
, 1), true);
3925 case BUILT_IN_MEMCPY_CHK
:
3926 case BUILT_IN_MEMPCPY_CHK
:
3927 case BUILT_IN_MEMMOVE_CHK
:
3928 case BUILT_IN_MEMSET_CHK
:
3929 return gimple_fold_builtin_memory_chk (gsi
,
3930 gimple_call_arg (stmt
, 0),
3931 gimple_call_arg (stmt
, 1),
3932 gimple_call_arg (stmt
, 2),
3933 gimple_call_arg (stmt
, 3),
3935 case BUILT_IN_STPCPY
:
3936 return gimple_fold_builtin_stpcpy (gsi
);
3937 case BUILT_IN_STRCPY_CHK
:
3938 case BUILT_IN_STPCPY_CHK
:
3939 return gimple_fold_builtin_stxcpy_chk (gsi
,
3940 gimple_call_arg (stmt
, 0),
3941 gimple_call_arg (stmt
, 1),
3942 gimple_call_arg (stmt
, 2),
3944 case BUILT_IN_STRNCPY_CHK
:
3945 case BUILT_IN_STPNCPY_CHK
:
3946 return gimple_fold_builtin_stxncpy_chk (gsi
,
3947 gimple_call_arg (stmt
, 0),
3948 gimple_call_arg (stmt
, 1),
3949 gimple_call_arg (stmt
, 2),
3950 gimple_call_arg (stmt
, 3),
3952 case BUILT_IN_SNPRINTF_CHK
:
3953 case BUILT_IN_VSNPRINTF_CHK
:
3954 return gimple_fold_builtin_snprintf_chk (gsi
, fcode
);
3956 case BUILT_IN_FPRINTF
:
3957 case BUILT_IN_FPRINTF_UNLOCKED
:
3958 case BUILT_IN_VFPRINTF
:
3959 if (n
== 2 || n
== 3)
3960 return gimple_fold_builtin_fprintf (gsi
,
3961 gimple_call_arg (stmt
, 0),
3962 gimple_call_arg (stmt
, 1),
3964 ? gimple_call_arg (stmt
, 2)
3968 case BUILT_IN_FPRINTF_CHK
:
3969 case BUILT_IN_VFPRINTF_CHK
:
3970 if (n
== 3 || n
== 4)
3971 return gimple_fold_builtin_fprintf (gsi
,
3972 gimple_call_arg (stmt
, 0),
3973 gimple_call_arg (stmt
, 2),
3975 ? gimple_call_arg (stmt
, 3)
3979 case BUILT_IN_PRINTF
:
3980 case BUILT_IN_PRINTF_UNLOCKED
:
3981 case BUILT_IN_VPRINTF
:
3982 if (n
== 1 || n
== 2)
3983 return gimple_fold_builtin_printf (gsi
, gimple_call_arg (stmt
, 0),
3985 ? gimple_call_arg (stmt
, 1)
3986 : NULL_TREE
, fcode
);
3988 case BUILT_IN_PRINTF_CHK
:
3989 case BUILT_IN_VPRINTF_CHK
:
3990 if (n
== 2 || n
== 3)
3991 return gimple_fold_builtin_printf (gsi
, gimple_call_arg (stmt
, 1),
3993 ? gimple_call_arg (stmt
, 2)
3994 : NULL_TREE
, fcode
);
3996 case BUILT_IN_ACC_ON_DEVICE
:
3997 return gimple_fold_builtin_acc_on_device (gsi
,
3998 gimple_call_arg (stmt
, 0));
3999 case BUILT_IN_REALLOC
:
4000 return gimple_fold_builtin_realloc (gsi
);
4005 /* Try the generic builtin folder. */
4006 bool ignore
= (gimple_call_lhs (stmt
) == NULL
);
4007 tree result
= fold_call_stmt (stmt
, ignore
);
4011 STRIP_NOPS (result
);
4013 result
= fold_convert (gimple_call_return_type (stmt
), result
);
4014 if (!update_call_from_tree (gsi
, result
))
4015 gimplify_and_update_call_from_tree (gsi
, result
);
4022 /* Transform IFN_GOACC_DIM_SIZE and IFN_GOACC_DIM_POS internal
4023 function calls to constants, where possible. */
4026 fold_internal_goacc_dim (const gimple
*call
)
4028 int axis
= oacc_get_ifn_dim_arg (call
);
4029 int size
= oacc_get_fn_dim_size (current_function_decl
, axis
);
4030 tree result
= NULL_TREE
;
4031 tree type
= TREE_TYPE (gimple_call_lhs (call
));
4033 switch (gimple_call_internal_fn (call
))
4035 case IFN_GOACC_DIM_POS
:
4036 /* If the size is 1, we know the answer. */
4038 result
= build_int_cst (type
, 0);
4040 case IFN_GOACC_DIM_SIZE
:
4041 /* If the size is not dynamic, we know the answer. */
4043 result
= build_int_cst (type
, size
);
4052 /* Return true if stmt is __atomic_compare_exchange_N call which is suitable
4053 for conversion into ATOMIC_COMPARE_EXCHANGE if the second argument is
4054 &var where var is only addressable because of such calls. */
4057 optimize_atomic_compare_exchange_p (gimple
*stmt
)
4059 if (gimple_call_num_args (stmt
) != 6
4060 || !flag_inline_atomics
4062 || sanitize_flags_p (SANITIZE_THREAD
| SANITIZE_ADDRESS
)
4063 || !gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
)
4064 || !gimple_vdef (stmt
)
4065 || !gimple_vuse (stmt
))
4068 tree fndecl
= gimple_call_fndecl (stmt
);
4069 switch (DECL_FUNCTION_CODE (fndecl
))
4071 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
:
4072 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2
:
4073 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4
:
4074 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8
:
4075 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16
:
4081 tree expected
= gimple_call_arg (stmt
, 1);
4082 if (TREE_CODE (expected
) != ADDR_EXPR
4083 || !SSA_VAR_P (TREE_OPERAND (expected
, 0)))
4086 tree etype
= TREE_TYPE (TREE_OPERAND (expected
, 0));
4087 if (!is_gimple_reg_type (etype
)
4088 || !auto_var_in_fn_p (TREE_OPERAND (expected
, 0), current_function_decl
)
4089 || TREE_THIS_VOLATILE (etype
)
4090 || VECTOR_TYPE_P (etype
)
4091 || TREE_CODE (etype
) == COMPLEX_TYPE
4092 /* Don't optimize floating point expected vars, VIEW_CONVERT_EXPRs
4093 might not preserve all the bits. See PR71716. */
4094 || SCALAR_FLOAT_TYPE_P (etype
)
4095 || maybe_ne (TYPE_PRECISION (etype
),
4096 GET_MODE_BITSIZE (TYPE_MODE (etype
))))
4099 tree weak
= gimple_call_arg (stmt
, 3);
4100 if (!integer_zerop (weak
) && !integer_onep (weak
))
4103 tree parmt
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
4104 tree itype
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt
)));
4105 machine_mode mode
= TYPE_MODE (itype
);
4107 if (direct_optab_handler (atomic_compare_and_swap_optab
, mode
)
4109 && optab_handler (sync_compare_and_swap_optab
, mode
) == CODE_FOR_nothing
)
4112 if (maybe_ne (int_size_in_bytes (etype
), GET_MODE_SIZE (mode
)))
4119 r = __atomic_compare_exchange_N (p, &e, d, w, s, f);
4121 _Complex uintN_t t = ATOMIC_COMPARE_EXCHANGE (p, e, d, w * 256 + N, s, f);
4122 i = IMAGPART_EXPR <t>;
4124 e = REALPART_EXPR <t>; */
4127 fold_builtin_atomic_compare_exchange (gimple_stmt_iterator
*gsi
)
4129 gimple
*stmt
= gsi_stmt (*gsi
);
4130 tree fndecl
= gimple_call_fndecl (stmt
);
4131 tree parmt
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
4132 tree itype
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt
)));
4133 tree ctype
= build_complex_type (itype
);
4134 tree expected
= TREE_OPERAND (gimple_call_arg (stmt
, 1), 0);
4135 bool throws
= false;
4137 gimple
*g
= gimple_build_assign (make_ssa_name (TREE_TYPE (expected
)),
4139 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4140 gimple_stmt_iterator gsiret
= gsi_for_stmt (g
);
4141 if (!useless_type_conversion_p (itype
, TREE_TYPE (expected
)))
4143 g
= gimple_build_assign (make_ssa_name (itype
), VIEW_CONVERT_EXPR
,
4144 build1 (VIEW_CONVERT_EXPR
, itype
,
4145 gimple_assign_lhs (g
)));
4146 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4148 int flag
= (integer_onep (gimple_call_arg (stmt
, 3)) ? 256 : 0)
4149 + int_size_in_bytes (itype
);
4150 g
= gimple_build_call_internal (IFN_ATOMIC_COMPARE_EXCHANGE
, 6,
4151 gimple_call_arg (stmt
, 0),
4152 gimple_assign_lhs (g
),
4153 gimple_call_arg (stmt
, 2),
4154 build_int_cst (integer_type_node
, flag
),
4155 gimple_call_arg (stmt
, 4),
4156 gimple_call_arg (stmt
, 5));
4157 tree lhs
= make_ssa_name (ctype
);
4158 gimple_call_set_lhs (g
, lhs
);
4159 gimple_move_vops (g
, stmt
);
4160 tree oldlhs
= gimple_call_lhs (stmt
);
4161 if (stmt_can_throw_internal (cfun
, stmt
))
4164 e
= find_fallthru_edge (gsi_bb (*gsi
)->succs
);
4166 gimple_call_set_nothrow (as_a
<gcall
*> (g
),
4167 gimple_call_nothrow_p (as_a
<gcall
*> (stmt
)));
4168 gimple_call_set_lhs (stmt
, NULL_TREE
);
4169 gsi_replace (gsi
, g
, true);
4172 g
= gimple_build_assign (make_ssa_name (itype
), IMAGPART_EXPR
,
4173 build1 (IMAGPART_EXPR
, itype
, lhs
));
4176 gsi_insert_on_edge_immediate (e
, g
);
4177 *gsi
= gsi_for_stmt (g
);
4180 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
4181 g
= gimple_build_assign (oldlhs
, NOP_EXPR
, gimple_assign_lhs (g
));
4182 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
4184 g
= gimple_build_assign (make_ssa_name (itype
), REALPART_EXPR
,
4185 build1 (REALPART_EXPR
, itype
, lhs
));
4186 if (throws
&& oldlhs
== NULL_TREE
)
4188 gsi_insert_on_edge_immediate (e
, g
);
4189 *gsi
= gsi_for_stmt (g
);
4192 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
4193 if (!useless_type_conversion_p (TREE_TYPE (expected
), itype
))
4195 g
= gimple_build_assign (make_ssa_name (TREE_TYPE (expected
)),
4197 build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (expected
),
4198 gimple_assign_lhs (g
)));
4199 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
4201 g
= gimple_build_assign (expected
, SSA_NAME
, gimple_assign_lhs (g
));
4202 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
4206 /* Return true if ARG0 CODE ARG1 in infinite signed precision operation
4207 doesn't fit into TYPE. The test for overflow should be regardless of
4208 -fwrapv, and even for unsigned types. */
4211 arith_overflowed_p (enum tree_code code
, const_tree type
,
4212 const_tree arg0
, const_tree arg1
)
4214 widest2_int warg0
= widest2_int_cst (arg0
);
4215 widest2_int warg1
= widest2_int_cst (arg1
);
4219 case PLUS_EXPR
: wres
= wi::add (warg0
, warg1
); break;
4220 case MINUS_EXPR
: wres
= wi::sub (warg0
, warg1
); break;
4221 case MULT_EXPR
: wres
= wi::mul (warg0
, warg1
); break;
4222 default: gcc_unreachable ();
4224 signop sign
= TYPE_SIGN (type
);
4225 if (sign
== UNSIGNED
&& wi::neg_p (wres
))
4227 return wi::min_precision (wres
, sign
) > TYPE_PRECISION (type
);
4230 /* If IFN_MASK_LOAD/STORE call CALL is unconditional, return a MEM_REF
4231 for the memory it references, otherwise return null. VECTYPE is the
4232 type of the memory vector. */
4235 gimple_fold_mask_load_store_mem_ref (gcall
*call
, tree vectype
)
4237 tree ptr
= gimple_call_arg (call
, 0);
4238 tree alias_align
= gimple_call_arg (call
, 1);
4239 tree mask
= gimple_call_arg (call
, 2);
4240 if (!tree_fits_uhwi_p (alias_align
) || !integer_all_onesp (mask
))
4243 unsigned HOST_WIDE_INT align
= tree_to_uhwi (alias_align
) * BITS_PER_UNIT
;
4244 if (TYPE_ALIGN (vectype
) != align
)
4245 vectype
= build_aligned_type (vectype
, align
);
4246 tree offset
= build_zero_cst (TREE_TYPE (alias_align
));
4247 return fold_build2 (MEM_REF
, vectype
, ptr
, offset
);
4250 /* Try to fold IFN_MASK_LOAD call CALL. Return true on success. */
4253 gimple_fold_mask_load (gimple_stmt_iterator
*gsi
, gcall
*call
)
4255 tree lhs
= gimple_call_lhs (call
);
4259 if (tree rhs
= gimple_fold_mask_load_store_mem_ref (call
, TREE_TYPE (lhs
)))
4261 gassign
*new_stmt
= gimple_build_assign (lhs
, rhs
);
4262 gimple_set_location (new_stmt
, gimple_location (call
));
4263 gimple_move_vops (new_stmt
, call
);
4264 gsi_replace (gsi
, new_stmt
, false);
4270 /* Try to fold IFN_MASK_STORE call CALL. Return true on success. */
4273 gimple_fold_mask_store (gimple_stmt_iterator
*gsi
, gcall
*call
)
4275 tree rhs
= gimple_call_arg (call
, 3);
4276 if (tree lhs
= gimple_fold_mask_load_store_mem_ref (call
, TREE_TYPE (rhs
)))
4278 gassign
*new_stmt
= gimple_build_assign (lhs
, rhs
);
4279 gimple_set_location (new_stmt
, gimple_location (call
));
4280 gimple_move_vops (new_stmt
, call
);
4281 gsi_replace (gsi
, new_stmt
, false);
4287 /* Attempt to fold a call statement referenced by the statement iterator GSI.
4288 The statement may be replaced by another statement, e.g., if the call
4289 simplifies to a constant value. Return true if any changes were made.
4290 It is assumed that the operands have been previously folded. */
4293 gimple_fold_call (gimple_stmt_iterator
*gsi
, bool inplace
)
4295 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
4297 bool changed
= false;
4300 /* Fold *& in call arguments. */
4301 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
4302 if (REFERENCE_CLASS_P (gimple_call_arg (stmt
, i
)))
4304 tree tmp
= maybe_fold_reference (gimple_call_arg (stmt
, i
), false);
4307 gimple_call_set_arg (stmt
, i
, tmp
);
4312 /* Check for virtual calls that became direct calls. */
4313 callee
= gimple_call_fn (stmt
);
4314 if (callee
&& TREE_CODE (callee
) == OBJ_TYPE_REF
)
4316 if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee
)) != NULL_TREE
)
4318 if (dump_file
&& virtual_method_call_p (callee
)
4319 && !possible_polymorphic_call_target_p
4320 (callee
, stmt
, cgraph_node::get (gimple_call_addr_fndecl
4321 (OBJ_TYPE_REF_EXPR (callee
)))))
4324 "Type inheritance inconsistent devirtualization of ");
4325 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
4326 fprintf (dump_file
, " to ");
4327 print_generic_expr (dump_file
, callee
, TDF_SLIM
);
4328 fprintf (dump_file
, "\n");
4331 gimple_call_set_fn (stmt
, OBJ_TYPE_REF_EXPR (callee
));
4334 else if (flag_devirtualize
&& !inplace
&& virtual_method_call_p (callee
))
4337 vec
<cgraph_node
*>targets
4338 = possible_polymorphic_call_targets (callee
, stmt
, &final
);
4339 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
4341 tree lhs
= gimple_call_lhs (stmt
);
4342 if (dump_enabled_p ())
4344 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, stmt
,
4345 "folding virtual function call to %s\n",
4346 targets
.length () == 1
4347 ? targets
[0]->name ()
4348 : "__builtin_unreachable");
4350 if (targets
.length () == 1)
4352 tree fndecl
= targets
[0]->decl
;
4353 gimple_call_set_fndecl (stmt
, fndecl
);
4355 /* If changing the call to __cxa_pure_virtual
4356 or similar noreturn function, adjust gimple_call_fntype
4358 if (gimple_call_noreturn_p (stmt
)
4359 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl
)))
4360 && TYPE_ARG_TYPES (TREE_TYPE (fndecl
))
4361 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)))
4363 gimple_call_set_fntype (stmt
, TREE_TYPE (fndecl
));
4364 /* If the call becomes noreturn, remove the lhs. */
4366 && gimple_call_noreturn_p (stmt
)
4367 && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (stmt
)))
4368 || should_remove_lhs_p (lhs
)))
4370 if (TREE_CODE (lhs
) == SSA_NAME
)
4372 tree var
= create_tmp_var (TREE_TYPE (lhs
));
4373 tree def
= get_or_create_ssa_default_def (cfun
, var
);
4374 gimple
*new_stmt
= gimple_build_assign (lhs
, def
);
4375 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
4377 gimple_call_set_lhs (stmt
, NULL_TREE
);
4379 maybe_remove_unused_call_args (cfun
, stmt
);
4383 tree fndecl
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
4384 gimple
*new_stmt
= gimple_build_call (fndecl
, 0);
4385 gimple_set_location (new_stmt
, gimple_location (stmt
));
4386 /* If the call had a SSA name as lhs morph that into
4387 an uninitialized value. */
4388 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
4390 tree var
= create_tmp_var (TREE_TYPE (lhs
));
4391 SET_SSA_NAME_VAR_OR_IDENTIFIER (lhs
, var
);
4392 SSA_NAME_DEF_STMT (lhs
) = gimple_build_nop ();
4393 set_ssa_default_def (cfun
, var
, lhs
);
4395 gimple_move_vops (new_stmt
, stmt
);
4396 gsi_replace (gsi
, new_stmt
, false);
4403 /* Check for indirect calls that became direct calls, and then
4404 no longer require a static chain. */
4405 if (gimple_call_chain (stmt
))
4407 tree fn
= gimple_call_fndecl (stmt
);
4408 if (fn
&& !DECL_STATIC_CHAIN (fn
))
4410 gimple_call_set_chain (stmt
, NULL
);
4415 tree tmp
= maybe_fold_reference (gimple_call_chain (stmt
), false);
4418 gimple_call_set_chain (stmt
, tmp
);
4427 /* Check for builtins that CCP can handle using information not
4428 available in the generic fold routines. */
4429 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
4431 if (gimple_fold_builtin (gsi
))
4434 else if (gimple_call_builtin_p (stmt
, BUILT_IN_MD
))
4436 changed
|= targetm
.gimple_fold_builtin (gsi
);
4438 else if (gimple_call_internal_p (stmt
))
4440 enum tree_code subcode
= ERROR_MARK
;
4441 tree result
= NULL_TREE
;
4442 bool cplx_result
= false;
4443 tree overflow
= NULL_TREE
;
4444 switch (gimple_call_internal_fn (stmt
))
4446 case IFN_BUILTIN_EXPECT
:
4447 result
= fold_builtin_expect (gimple_location (stmt
),
4448 gimple_call_arg (stmt
, 0),
4449 gimple_call_arg (stmt
, 1),
4450 gimple_call_arg (stmt
, 2),
4453 case IFN_UBSAN_OBJECT_SIZE
:
4455 tree offset
= gimple_call_arg (stmt
, 1);
4456 tree objsize
= gimple_call_arg (stmt
, 2);
4457 if (integer_all_onesp (objsize
)
4458 || (TREE_CODE (offset
) == INTEGER_CST
4459 && TREE_CODE (objsize
) == INTEGER_CST
4460 && tree_int_cst_le (offset
, objsize
)))
4462 replace_call_with_value (gsi
, NULL_TREE
);
4468 if (integer_zerop (gimple_call_arg (stmt
, 1)))
4470 replace_call_with_value (gsi
, NULL_TREE
);
4474 case IFN_UBSAN_BOUNDS
:
4476 tree index
= gimple_call_arg (stmt
, 1);
4477 tree bound
= gimple_call_arg (stmt
, 2);
4478 if (TREE_CODE (index
) == INTEGER_CST
4479 && TREE_CODE (bound
) == INTEGER_CST
)
4481 index
= fold_convert (TREE_TYPE (bound
), index
);
4482 if (TREE_CODE (index
) == INTEGER_CST
4483 && tree_int_cst_le (index
, bound
))
4485 replace_call_with_value (gsi
, NULL_TREE
);
4491 case IFN_GOACC_DIM_SIZE
:
4492 case IFN_GOACC_DIM_POS
:
4493 result
= fold_internal_goacc_dim (stmt
);
4495 case IFN_UBSAN_CHECK_ADD
:
4496 subcode
= PLUS_EXPR
;
4498 case IFN_UBSAN_CHECK_SUB
:
4499 subcode
= MINUS_EXPR
;
4501 case IFN_UBSAN_CHECK_MUL
:
4502 subcode
= MULT_EXPR
;
4504 case IFN_ADD_OVERFLOW
:
4505 subcode
= PLUS_EXPR
;
4508 case IFN_SUB_OVERFLOW
:
4509 subcode
= MINUS_EXPR
;
4512 case IFN_MUL_OVERFLOW
:
4513 subcode
= MULT_EXPR
;
4517 changed
|= gimple_fold_mask_load (gsi
, stmt
);
4519 case IFN_MASK_STORE
:
4520 changed
|= gimple_fold_mask_store (gsi
, stmt
);
4525 if (subcode
!= ERROR_MARK
)
4527 tree arg0
= gimple_call_arg (stmt
, 0);
4528 tree arg1
= gimple_call_arg (stmt
, 1);
4529 tree type
= TREE_TYPE (arg0
);
4532 tree lhs
= gimple_call_lhs (stmt
);
4533 if (lhs
== NULL_TREE
)
4536 type
= TREE_TYPE (TREE_TYPE (lhs
));
4538 if (type
== NULL_TREE
)
4540 /* x = y + 0; x = y - 0; x = y * 0; */
4541 else if (integer_zerop (arg1
))
4542 result
= subcode
== MULT_EXPR
? integer_zero_node
: arg0
;
4543 /* x = 0 + y; x = 0 * y; */
4544 else if (subcode
!= MINUS_EXPR
&& integer_zerop (arg0
))
4545 result
= subcode
== MULT_EXPR
? integer_zero_node
: arg1
;
4547 else if (subcode
== MINUS_EXPR
&& operand_equal_p (arg0
, arg1
, 0))
4548 result
= integer_zero_node
;
4549 /* x = y * 1; x = 1 * y; */
4550 else if (subcode
== MULT_EXPR
&& integer_onep (arg1
))
4552 else if (subcode
== MULT_EXPR
&& integer_onep (arg0
))
4554 else if (TREE_CODE (arg0
) == INTEGER_CST
4555 && TREE_CODE (arg1
) == INTEGER_CST
)
4558 result
= int_const_binop (subcode
, fold_convert (type
, arg0
),
4559 fold_convert (type
, arg1
));
4561 result
= int_const_binop (subcode
, arg0
, arg1
);
4562 if (result
&& arith_overflowed_p (subcode
, type
, arg0
, arg1
))
4565 overflow
= build_one_cst (type
);
4572 if (result
== integer_zero_node
)
4573 result
= build_zero_cst (type
);
4574 else if (cplx_result
&& TREE_TYPE (result
) != type
)
4576 if (TREE_CODE (result
) == INTEGER_CST
)
4578 if (arith_overflowed_p (PLUS_EXPR
, type
, result
,
4580 overflow
= build_one_cst (type
);
4582 else if ((!TYPE_UNSIGNED (TREE_TYPE (result
))
4583 && TYPE_UNSIGNED (type
))
4584 || (TYPE_PRECISION (type
)
4585 < (TYPE_PRECISION (TREE_TYPE (result
))
4586 + (TYPE_UNSIGNED (TREE_TYPE (result
))
4587 && !TYPE_UNSIGNED (type
)))))
4590 result
= fold_convert (type
, result
);
4597 if (TREE_CODE (result
) == INTEGER_CST
&& TREE_OVERFLOW (result
))
4598 result
= drop_tree_overflow (result
);
4601 if (overflow
== NULL_TREE
)
4602 overflow
= build_zero_cst (TREE_TYPE (result
));
4603 tree ctype
= build_complex_type (TREE_TYPE (result
));
4604 if (TREE_CODE (result
) == INTEGER_CST
4605 && TREE_CODE (overflow
) == INTEGER_CST
)
4606 result
= build_complex (ctype
, result
, overflow
);
4608 result
= build2_loc (gimple_location (stmt
), COMPLEX_EXPR
,
4609 ctype
, result
, overflow
);
4611 if (!update_call_from_tree (gsi
, result
))
4612 gimplify_and_update_call_from_tree (gsi
, result
);
4621 /* Return true whether NAME has a use on STMT. */
4624 has_use_on_stmt (tree name
, gimple
*stmt
)
4626 imm_use_iterator iter
;
4627 use_operand_p use_p
;
4628 FOR_EACH_IMM_USE_FAST (use_p
, iter
, name
)
4629 if (USE_STMT (use_p
) == stmt
)
4634 /* Worker for fold_stmt_1 dispatch to pattern based folding with
4637 Replaces *GSI with the simplification result in RCODE and OPS
4638 and the associated statements in *SEQ. Does the replacement
4639 according to INPLACE and returns true if the operation succeeded. */
4642 replace_stmt_with_simplification (gimple_stmt_iterator
*gsi
,
4643 gimple_match_op
*res_op
,
4644 gimple_seq
*seq
, bool inplace
)
4646 gimple
*stmt
= gsi_stmt (*gsi
);
4647 tree
*ops
= res_op
->ops
;
4648 unsigned int num_ops
= res_op
->num_ops
;
4650 /* Play safe and do not allow abnormals to be mentioned in
4651 newly created statements. See also maybe_push_res_to_seq.
4652 As an exception allow such uses if there was a use of the
4653 same SSA name on the old stmt. */
4654 for (unsigned int i
= 0; i
< num_ops
; ++i
)
4655 if (TREE_CODE (ops
[i
]) == SSA_NAME
4656 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[i
])
4657 && !has_use_on_stmt (ops
[i
], stmt
))
4660 if (num_ops
> 0 && COMPARISON_CLASS_P (ops
[0]))
4661 for (unsigned int i
= 0; i
< 2; ++i
)
4662 if (TREE_CODE (TREE_OPERAND (ops
[0], i
)) == SSA_NAME
4663 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops
[0], i
))
4664 && !has_use_on_stmt (TREE_OPERAND (ops
[0], i
), stmt
))
4667 /* Don't insert new statements when INPLACE is true, even if we could
4668 reuse STMT for the final statement. */
4669 if (inplace
&& !gimple_seq_empty_p (*seq
))
4672 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
4674 gcc_assert (res_op
->code
.is_tree_code ());
4675 if (TREE_CODE_CLASS ((enum tree_code
) res_op
->code
) == tcc_comparison
4676 /* GIMPLE_CONDs condition may not throw. */
4677 && (!flag_exceptions
4678 || !cfun
->can_throw_non_call_exceptions
4679 || !operation_could_trap_p (res_op
->code
,
4680 FLOAT_TYPE_P (TREE_TYPE (ops
[0])),
4682 gimple_cond_set_condition (cond_stmt
, res_op
->code
, ops
[0], ops
[1]);
4683 else if (res_op
->code
== SSA_NAME
)
4684 gimple_cond_set_condition (cond_stmt
, NE_EXPR
, ops
[0],
4685 build_zero_cst (TREE_TYPE (ops
[0])));
4686 else if (res_op
->code
== INTEGER_CST
)
4688 if (integer_zerop (ops
[0]))
4689 gimple_cond_make_false (cond_stmt
);
4691 gimple_cond_make_true (cond_stmt
);
4695 tree res
= maybe_push_res_to_seq (res_op
, seq
);
4698 gimple_cond_set_condition (cond_stmt
, NE_EXPR
, res
,
4699 build_zero_cst (TREE_TYPE (res
)));
4703 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4705 fprintf (dump_file
, "gimple_simplified to ");
4706 if (!gimple_seq_empty_p (*seq
))
4707 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
4708 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
),
4711 gsi_insert_seq_before (gsi
, *seq
, GSI_SAME_STMT
);
4714 else if (is_gimple_assign (stmt
)
4715 && res_op
->code
.is_tree_code ())
4718 || gimple_num_ops (stmt
) > get_gimple_rhs_num_ops (res_op
->code
))
4720 maybe_build_generic_op (res_op
);
4721 gimple_assign_set_rhs_with_ops (gsi
, res_op
->code
,
4722 res_op
->op_or_null (0),
4723 res_op
->op_or_null (1),
4724 res_op
->op_or_null (2));
4725 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4727 fprintf (dump_file
, "gimple_simplified to ");
4728 if (!gimple_seq_empty_p (*seq
))
4729 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
4730 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
),
4733 gsi_insert_seq_before (gsi
, *seq
, GSI_SAME_STMT
);
4737 else if (res_op
->code
.is_fn_code ()
4738 && gimple_call_combined_fn (stmt
) == res_op
->code
)
4740 gcc_assert (num_ops
== gimple_call_num_args (stmt
));
4741 for (unsigned int i
= 0; i
< num_ops
; ++i
)
4742 gimple_call_set_arg (stmt
, i
, ops
[i
]);
4743 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4745 fprintf (dump_file
, "gimple_simplified to ");
4746 if (!gimple_seq_empty_p (*seq
))
4747 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
4748 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
), 0, TDF_SLIM
);
4750 gsi_insert_seq_before (gsi
, *seq
, GSI_SAME_STMT
);
4755 if (gimple_has_lhs (stmt
))
4757 tree lhs
= gimple_get_lhs (stmt
);
4758 if (!maybe_push_res_to_seq (res_op
, seq
, lhs
))
4760 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4762 fprintf (dump_file
, "gimple_simplified to ");
4763 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
4765 gsi_replace_with_seq_vops (gsi
, *seq
);
4775 /* Canonicalize MEM_REFs invariant address operand after propagation. */
4778 maybe_canonicalize_mem_ref_addr (tree
*t
)
4782 if (TREE_CODE (*t
) == ADDR_EXPR
)
4783 t
= &TREE_OPERAND (*t
, 0);
4785 /* The C and C++ frontends use an ARRAY_REF for indexing with their
4786 generic vector extension. The actual vector referenced is
4787 view-converted to an array type for this purpose. If the index
4788 is constant the canonical representation in the middle-end is a
4789 BIT_FIELD_REF so re-write the former to the latter here. */
4790 if (TREE_CODE (*t
) == ARRAY_REF
4791 && TREE_CODE (TREE_OPERAND (*t
, 0)) == VIEW_CONVERT_EXPR
4792 && TREE_CODE (TREE_OPERAND (*t
, 1)) == INTEGER_CST
4793 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t
, 0), 0))))
4795 tree vtype
= TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t
, 0), 0));
4796 if (VECTOR_TYPE_P (vtype
))
4798 tree low
= array_ref_low_bound (*t
);
4799 if (TREE_CODE (low
) == INTEGER_CST
)
4801 if (tree_int_cst_le (low
, TREE_OPERAND (*t
, 1)))
4803 widest_int idx
= wi::sub (wi::to_widest (TREE_OPERAND (*t
, 1)),
4804 wi::to_widest (low
));
4805 idx
= wi::mul (idx
, wi::to_widest
4806 (TYPE_SIZE (TREE_TYPE (*t
))));
4808 = wi::add (idx
, wi::to_widest (TYPE_SIZE (TREE_TYPE (*t
))));
4809 if (wi::les_p (ext
, wi::to_widest (TYPE_SIZE (vtype
))))
4811 *t
= build3_loc (EXPR_LOCATION (*t
), BIT_FIELD_REF
,
4813 TREE_OPERAND (TREE_OPERAND (*t
, 0), 0),
4814 TYPE_SIZE (TREE_TYPE (*t
)),
4815 wide_int_to_tree (bitsizetype
, idx
));
4823 while (handled_component_p (*t
))
4824 t
= &TREE_OPERAND (*t
, 0);
4826 /* Canonicalize MEM [&foo.bar, 0] which appears after propagating
4827 of invariant addresses into a SSA name MEM_REF address. */
4828 if (TREE_CODE (*t
) == MEM_REF
4829 || TREE_CODE (*t
) == TARGET_MEM_REF
)
4831 tree addr
= TREE_OPERAND (*t
, 0);
4832 if (TREE_CODE (addr
) == ADDR_EXPR
4833 && (TREE_CODE (TREE_OPERAND (addr
, 0)) == MEM_REF
4834 || handled_component_p (TREE_OPERAND (addr
, 0))))
4838 base
= get_addr_base_and_unit_offset (TREE_OPERAND (addr
, 0),
4843 TREE_OPERAND (*t
, 0) = build_fold_addr_expr (base
);
4844 TREE_OPERAND (*t
, 1) = int_const_binop (PLUS_EXPR
,
4845 TREE_OPERAND (*t
, 1),
4846 size_int (coffset
));
4849 gcc_checking_assert (TREE_CODE (TREE_OPERAND (*t
, 0)) == DEBUG_EXPR_DECL
4850 || is_gimple_mem_ref_addr (TREE_OPERAND (*t
, 0)));
4853 /* Canonicalize back MEM_REFs to plain reference trees if the object
4854 accessed is a decl that has the same access semantics as the MEM_REF. */
4855 if (TREE_CODE (*t
) == MEM_REF
4856 && TREE_CODE (TREE_OPERAND (*t
, 0)) == ADDR_EXPR
4857 && integer_zerop (TREE_OPERAND (*t
, 1))
4858 && MR_DEPENDENCE_CLIQUE (*t
) == 0)
4860 tree decl
= TREE_OPERAND (TREE_OPERAND (*t
, 0), 0);
4861 tree alias_type
= TREE_TYPE (TREE_OPERAND (*t
, 1));
4862 if (/* Same volatile qualification. */
4863 TREE_THIS_VOLATILE (*t
) == TREE_THIS_VOLATILE (decl
)
4864 /* Same TBAA behavior with -fstrict-aliasing. */
4865 && !TYPE_REF_CAN_ALIAS_ALL (alias_type
)
4866 && (TYPE_MAIN_VARIANT (TREE_TYPE (decl
))
4867 == TYPE_MAIN_VARIANT (TREE_TYPE (alias_type
)))
4868 /* Same alignment. */
4869 && TYPE_ALIGN (TREE_TYPE (decl
)) == TYPE_ALIGN (TREE_TYPE (*t
))
4870 /* We have to look out here to not drop a required conversion
4871 from the rhs to the lhs if *t appears on the lhs or vice-versa
4872 if it appears on the rhs. Thus require strict type
4874 && types_compatible_p (TREE_TYPE (*t
), TREE_TYPE (decl
)))
4876 *t
= TREE_OPERAND (TREE_OPERAND (*t
, 0), 0);
4881 /* Canonicalize TARGET_MEM_REF in particular with respect to
4882 the indexes becoming constant. */
4883 else if (TREE_CODE (*t
) == TARGET_MEM_REF
)
4885 tree tem
= maybe_fold_tmr (*t
);
4896 /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument
4897 distinguishes both cases. */
4900 fold_stmt_1 (gimple_stmt_iterator
*gsi
, bool inplace
, tree (*valueize
) (tree
))
4902 bool changed
= false;
4903 gimple
*stmt
= gsi_stmt (*gsi
);
4904 bool nowarning
= gimple_no_warning_p (stmt
);
4906 fold_defer_overflow_warnings ();
4908 /* First do required canonicalization of [TARGET_]MEM_REF addresses
4910 ??? This shouldn't be done in generic folding but in the
4911 propagation helpers which also know whether an address was
4913 Also canonicalize operand order. */
4914 switch (gimple_code (stmt
))
4917 if (gimple_assign_rhs_class (stmt
) == GIMPLE_SINGLE_RHS
)
4919 tree
*rhs
= gimple_assign_rhs1_ptr (stmt
);
4920 if ((REFERENCE_CLASS_P (*rhs
)
4921 || TREE_CODE (*rhs
) == ADDR_EXPR
)
4922 && maybe_canonicalize_mem_ref_addr (rhs
))
4924 tree
*lhs
= gimple_assign_lhs_ptr (stmt
);
4925 if (REFERENCE_CLASS_P (*lhs
)
4926 && maybe_canonicalize_mem_ref_addr (lhs
))
4931 /* Canonicalize operand order. */
4932 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4933 if (TREE_CODE_CLASS (code
) == tcc_comparison
4934 || commutative_tree_code (code
)
4935 || commutative_ternary_tree_code (code
))
4937 tree rhs1
= gimple_assign_rhs1 (stmt
);
4938 tree rhs2
= gimple_assign_rhs2 (stmt
);
4939 if (tree_swap_operands_p (rhs1
, rhs2
))
4941 gimple_assign_set_rhs1 (stmt
, rhs2
);
4942 gimple_assign_set_rhs2 (stmt
, rhs1
);
4943 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4944 gimple_assign_set_rhs_code (stmt
,
4945 swap_tree_comparison (code
));
4953 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
4955 tree
*arg
= gimple_call_arg_ptr (stmt
, i
);
4956 if (REFERENCE_CLASS_P (*arg
)
4957 && maybe_canonicalize_mem_ref_addr (arg
))
4960 tree
*lhs
= gimple_call_lhs_ptr (stmt
);
4962 && REFERENCE_CLASS_P (*lhs
)
4963 && maybe_canonicalize_mem_ref_addr (lhs
))
4969 gasm
*asm_stmt
= as_a
<gasm
*> (stmt
);
4970 for (i
= 0; i
< gimple_asm_noutputs (asm_stmt
); ++i
)
4972 tree link
= gimple_asm_output_op (asm_stmt
, i
);
4973 tree op
= TREE_VALUE (link
);
4974 if (REFERENCE_CLASS_P (op
)
4975 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link
)))
4978 for (i
= 0; i
< gimple_asm_ninputs (asm_stmt
); ++i
)
4980 tree link
= gimple_asm_input_op (asm_stmt
, i
);
4981 tree op
= TREE_VALUE (link
);
4982 if ((REFERENCE_CLASS_P (op
)
4983 || TREE_CODE (op
) == ADDR_EXPR
)
4984 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link
)))
4990 if (gimple_debug_bind_p (stmt
))
4992 tree
*val
= gimple_debug_bind_get_value_ptr (stmt
);
4994 && (REFERENCE_CLASS_P (*val
)
4995 || TREE_CODE (*val
) == ADDR_EXPR
)
4996 && maybe_canonicalize_mem_ref_addr (val
))
5002 /* Canonicalize operand order. */
5003 tree lhs
= gimple_cond_lhs (stmt
);
5004 tree rhs
= gimple_cond_rhs (stmt
);
5005 if (tree_swap_operands_p (lhs
, rhs
))
5007 gcond
*gc
= as_a
<gcond
*> (stmt
);
5008 gimple_cond_set_lhs (gc
, rhs
);
5009 gimple_cond_set_rhs (gc
, lhs
);
5010 gimple_cond_set_code (gc
,
5011 swap_tree_comparison (gimple_cond_code (gc
)));
5018 /* Dispatch to pattern-based folding. */
5020 || is_gimple_assign (stmt
)
5021 || gimple_code (stmt
) == GIMPLE_COND
)
5023 gimple_seq seq
= NULL
;
5024 gimple_match_op res_op
;
5025 if (gimple_simplify (stmt
, &res_op
, inplace
? NULL
: &seq
,
5026 valueize
, valueize
))
5028 if (replace_stmt_with_simplification (gsi
, &res_op
, &seq
, inplace
))
5031 gimple_seq_discard (seq
);
5035 stmt
= gsi_stmt (*gsi
);
5037 /* Fold the main computation performed by the statement. */
5038 switch (gimple_code (stmt
))
5042 /* Try to canonicalize for boolean-typed X the comparisons
5043 X == 0, X == 1, X != 0, and X != 1. */
5044 if (gimple_assign_rhs_code (stmt
) == EQ_EXPR
5045 || gimple_assign_rhs_code (stmt
) == NE_EXPR
)
5047 tree lhs
= gimple_assign_lhs (stmt
);
5048 tree op1
= gimple_assign_rhs1 (stmt
);
5049 tree op2
= gimple_assign_rhs2 (stmt
);
5050 tree type
= TREE_TYPE (op1
);
5052 /* Check whether the comparison operands are of the same boolean
5053 type as the result type is.
5054 Check that second operand is an integer-constant with value
5056 if (TREE_CODE (op2
) == INTEGER_CST
5057 && (integer_zerop (op2
) || integer_onep (op2
))
5058 && useless_type_conversion_p (TREE_TYPE (lhs
), type
))
5060 enum tree_code cmp_code
= gimple_assign_rhs_code (stmt
);
5061 bool is_logical_not
= false;
5063 /* X == 0 and X != 1 is a logical-not.of X
5064 X == 1 and X != 0 is X */
5065 if ((cmp_code
== EQ_EXPR
&& integer_zerop (op2
))
5066 || (cmp_code
== NE_EXPR
&& integer_onep (op2
)))
5067 is_logical_not
= true;
5069 if (is_logical_not
== false)
5070 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op1
), op1
);
5071 /* Only for one-bit precision typed X the transformation
5072 !X -> ~X is valied. */
5073 else if (TYPE_PRECISION (type
) == 1)
5074 gimple_assign_set_rhs_with_ops (gsi
, BIT_NOT_EXPR
, op1
);
5075 /* Otherwise we use !X -> X ^ 1. */
5077 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op1
,
5078 build_int_cst (type
, 1));
5084 unsigned old_num_ops
= gimple_num_ops (stmt
);
5085 tree lhs
= gimple_assign_lhs (stmt
);
5086 tree new_rhs
= fold_gimple_assign (gsi
);
5088 && !useless_type_conversion_p (TREE_TYPE (lhs
),
5089 TREE_TYPE (new_rhs
)))
5090 new_rhs
= fold_convert (TREE_TYPE (lhs
), new_rhs
);
5093 || get_gimple_rhs_num_ops (TREE_CODE (new_rhs
)) < old_num_ops
))
5095 gimple_assign_set_rhs_from_tree (gsi
, new_rhs
);
5102 changed
|= gimple_fold_call (gsi
, inplace
);
5106 /* Fold *& in asm operands. */
5108 gasm
*asm_stmt
= as_a
<gasm
*> (stmt
);
5110 const char **oconstraints
;
5111 const char *constraint
;
5112 bool allows_mem
, allows_reg
;
5114 noutputs
= gimple_asm_noutputs (asm_stmt
);
5115 oconstraints
= XALLOCAVEC (const char *, noutputs
);
5117 for (i
= 0; i
< gimple_asm_noutputs (asm_stmt
); ++i
)
5119 tree link
= gimple_asm_output_op (asm_stmt
, i
);
5120 tree op
= TREE_VALUE (link
);
5122 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
5123 if (REFERENCE_CLASS_P (op
)
5124 && (op
= maybe_fold_reference (op
, true)) != NULL_TREE
)
5126 TREE_VALUE (link
) = op
;
5130 for (i
= 0; i
< gimple_asm_ninputs (asm_stmt
); ++i
)
5132 tree link
= gimple_asm_input_op (asm_stmt
, i
);
5133 tree op
= TREE_VALUE (link
);
5135 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
5136 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0,
5137 oconstraints
, &allows_mem
, &allows_reg
);
5138 if (REFERENCE_CLASS_P (op
)
5139 && (op
= maybe_fold_reference (op
, !allows_reg
&& allows_mem
))
5142 TREE_VALUE (link
) = op
;
5150 if (gimple_debug_bind_p (stmt
))
5152 tree val
= gimple_debug_bind_get_value (stmt
);
5154 && REFERENCE_CLASS_P (val
))
5156 tree tem
= maybe_fold_reference (val
, false);
5159 gimple_debug_bind_set_value (stmt
, tem
);
5164 && TREE_CODE (val
) == ADDR_EXPR
)
5166 tree ref
= TREE_OPERAND (val
, 0);
5167 tree tem
= maybe_fold_reference (ref
, false);
5170 tem
= build_fold_addr_expr_with_type (tem
, TREE_TYPE (val
));
5171 gimple_debug_bind_set_value (stmt
, tem
);
5180 greturn
*ret_stmt
= as_a
<greturn
*> (stmt
);
5181 tree ret
= gimple_return_retval(ret_stmt
);
5183 if (ret
&& TREE_CODE (ret
) == SSA_NAME
&& valueize
)
5185 tree val
= valueize (ret
);
5186 if (val
&& val
!= ret
5187 && may_propagate_copy (ret
, val
))
5189 gimple_return_set_retval (ret_stmt
, val
);
5199 stmt
= gsi_stmt (*gsi
);
5201 /* Fold *& on the lhs. */
5202 if (gimple_has_lhs (stmt
))
5204 tree lhs
= gimple_get_lhs (stmt
);
5205 if (lhs
&& REFERENCE_CLASS_P (lhs
))
5207 tree new_lhs
= maybe_fold_reference (lhs
, true);
5210 gimple_set_lhs (stmt
, new_lhs
);
5216 fold_undefer_overflow_warnings (changed
&& !nowarning
, stmt
, 0);
5220 /* Valueziation callback that ends up not following SSA edges. */
5223 no_follow_ssa_edges (tree
)
5228 /* Valueization callback that ends up following single-use SSA edges only. */
5231 follow_single_use_edges (tree val
)
5233 if (TREE_CODE (val
) == SSA_NAME
5234 && !has_single_use (val
))
5239 /* Valueization callback that follows all SSA edges. */
5242 follow_all_ssa_edges (tree val
)
5247 /* Fold the statement pointed to by GSI. In some cases, this function may
5248 replace the whole statement with a new one. Returns true iff folding
5250 The statement pointed to by GSI should be in valid gimple form but may
5251 be in unfolded state as resulting from for example constant propagation
5252 which can produce *&x = 0. */
5255 fold_stmt (gimple_stmt_iterator
*gsi
)
5257 return fold_stmt_1 (gsi
, false, no_follow_ssa_edges
);
5261 fold_stmt (gimple_stmt_iterator
*gsi
, tree (*valueize
) (tree
))
5263 return fold_stmt_1 (gsi
, false, valueize
);
5266 /* Perform the minimal folding on statement *GSI. Only operations like
5267 *&x created by constant propagation are handled. The statement cannot
5268 be replaced with a new one. Return true if the statement was
5269 changed, false otherwise.
5270 The statement *GSI should be in valid gimple form but may
5271 be in unfolded state as resulting from for example constant propagation
5272 which can produce *&x = 0. */
5275 fold_stmt_inplace (gimple_stmt_iterator
*gsi
)
5277 gimple
*stmt
= gsi_stmt (*gsi
);
5278 bool changed
= fold_stmt_1 (gsi
, true, no_follow_ssa_edges
);
5279 gcc_assert (gsi_stmt (*gsi
) == stmt
);
5283 /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
5284 if EXPR is null or we don't know how.
5285 If non-null, the result always has boolean type. */
5288 canonicalize_bool (tree expr
, bool invert
)
5294 if (integer_nonzerop (expr
))
5295 return boolean_false_node
;
5296 else if (integer_zerop (expr
))
5297 return boolean_true_node
;
5298 else if (TREE_CODE (expr
) == SSA_NAME
)
5299 return fold_build2 (EQ_EXPR
, boolean_type_node
, expr
,
5300 build_int_cst (TREE_TYPE (expr
), 0));
5301 else if (COMPARISON_CLASS_P (expr
))
5302 return fold_build2 (invert_tree_comparison (TREE_CODE (expr
), false),
5304 TREE_OPERAND (expr
, 0),
5305 TREE_OPERAND (expr
, 1));
5311 if (TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
)
5313 if (integer_nonzerop (expr
))
5314 return boolean_true_node
;
5315 else if (integer_zerop (expr
))
5316 return boolean_false_node
;
5317 else if (TREE_CODE (expr
) == SSA_NAME
)
5318 return fold_build2 (NE_EXPR
, boolean_type_node
, expr
,
5319 build_int_cst (TREE_TYPE (expr
), 0));
5320 else if (COMPARISON_CLASS_P (expr
))
5321 return fold_build2 (TREE_CODE (expr
),
5323 TREE_OPERAND (expr
, 0),
5324 TREE_OPERAND (expr
, 1));
5330 /* Check to see if a boolean expression EXPR is logically equivalent to the
5331 comparison (OP1 CODE OP2). Check for various identities involving
5335 same_bool_comparison_p (const_tree expr
, enum tree_code code
,
5336 const_tree op1
, const_tree op2
)
5340 /* The obvious case. */
5341 if (TREE_CODE (expr
) == code
5342 && operand_equal_p (TREE_OPERAND (expr
, 0), op1
, 0)
5343 && operand_equal_p (TREE_OPERAND (expr
, 1), op2
, 0))
5346 /* Check for comparing (name, name != 0) and the case where expr
5347 is an SSA_NAME with a definition matching the comparison. */
5348 if (TREE_CODE (expr
) == SSA_NAME
5349 && TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
)
5351 if (operand_equal_p (expr
, op1
, 0))
5352 return ((code
== NE_EXPR
&& integer_zerop (op2
))
5353 || (code
== EQ_EXPR
&& integer_nonzerop (op2
)));
5354 s
= SSA_NAME_DEF_STMT (expr
);
5355 if (is_gimple_assign (s
)
5356 && gimple_assign_rhs_code (s
) == code
5357 && operand_equal_p (gimple_assign_rhs1 (s
), op1
, 0)
5358 && operand_equal_p (gimple_assign_rhs2 (s
), op2
, 0))
5362 /* If op1 is of the form (name != 0) or (name == 0), and the definition
5363 of name is a comparison, recurse. */
5364 if (TREE_CODE (op1
) == SSA_NAME
5365 && TREE_CODE (TREE_TYPE (op1
)) == BOOLEAN_TYPE
)
5367 s
= SSA_NAME_DEF_STMT (op1
);
5368 if (is_gimple_assign (s
)
5369 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
)
5371 enum tree_code c
= gimple_assign_rhs_code (s
);
5372 if ((c
== NE_EXPR
&& integer_zerop (op2
))
5373 || (c
== EQ_EXPR
&& integer_nonzerop (op2
)))
5374 return same_bool_comparison_p (expr
, c
,
5375 gimple_assign_rhs1 (s
),
5376 gimple_assign_rhs2 (s
));
5377 if ((c
== EQ_EXPR
&& integer_zerop (op2
))
5378 || (c
== NE_EXPR
&& integer_nonzerop (op2
)))
5379 return same_bool_comparison_p (expr
,
5380 invert_tree_comparison (c
, false),
5381 gimple_assign_rhs1 (s
),
5382 gimple_assign_rhs2 (s
));
5388 /* Check to see if two boolean expressions OP1 and OP2 are logically
5392 same_bool_result_p (const_tree op1
, const_tree op2
)
5394 /* Simple cases first. */
5395 if (operand_equal_p (op1
, op2
, 0))
5398 /* Check the cases where at least one of the operands is a comparison.
5399 These are a bit smarter than operand_equal_p in that they apply some
5400 identifies on SSA_NAMEs. */
5401 if (COMPARISON_CLASS_P (op2
)
5402 && same_bool_comparison_p (op1
, TREE_CODE (op2
),
5403 TREE_OPERAND (op2
, 0),
5404 TREE_OPERAND (op2
, 1)))
5406 if (COMPARISON_CLASS_P (op1
)
5407 && same_bool_comparison_p (op2
, TREE_CODE (op1
),
5408 TREE_OPERAND (op1
, 0),
5409 TREE_OPERAND (op1
, 1)))
5416 /* Forward declarations for some mutually recursive functions. */
5419 and_comparisons_1 (tree type
, enum tree_code code1
, tree op1a
, tree op1b
,
5420 enum tree_code code2
, tree op2a
, tree op2b
);
5422 and_var_with_comparison (tree type
, tree var
, bool invert
,
5423 enum tree_code code2
, tree op2a
, tree op2b
);
5425 and_var_with_comparison_1 (tree type
, gimple
*stmt
,
5426 enum tree_code code2
, tree op2a
, tree op2b
);
5428 or_comparisons_1 (tree
, enum tree_code code1
, tree op1a
, tree op1b
,
5429 enum tree_code code2
, tree op2a
, tree op2b
);
5431 or_var_with_comparison (tree
, tree var
, bool invert
,
5432 enum tree_code code2
, tree op2a
, tree op2b
);
5434 or_var_with_comparison_1 (tree
, gimple
*stmt
,
5435 enum tree_code code2
, tree op2a
, tree op2b
);
5437 /* Helper function for and_comparisons_1: try to simplify the AND of the
5438 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
5439 If INVERT is true, invert the value of the VAR before doing the AND.
5440 Return NULL_EXPR if we can't simplify this to a single expression. */
5443 and_var_with_comparison (tree type
, tree var
, bool invert
,
5444 enum tree_code code2
, tree op2a
, tree op2b
)
5447 gimple
*stmt
= SSA_NAME_DEF_STMT (var
);
5449 /* We can only deal with variables whose definitions are assignments. */
5450 if (!is_gimple_assign (stmt
))
5453 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
5454 !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
5455 Then we only have to consider the simpler non-inverted cases. */
5457 t
= or_var_with_comparison_1 (type
, stmt
,
5458 invert_tree_comparison (code2
, false),
5461 t
= and_var_with_comparison_1 (type
, stmt
, code2
, op2a
, op2b
);
5462 return canonicalize_bool (t
, invert
);
5465 /* Try to simplify the AND of the ssa variable defined by the assignment
5466 STMT with the comparison specified by (OP2A CODE2 OP2B).
5467 Return NULL_EXPR if we can't simplify this to a single expression. */
5470 and_var_with_comparison_1 (tree type
, gimple
*stmt
,
5471 enum tree_code code2
, tree op2a
, tree op2b
)
5473 tree var
= gimple_assign_lhs (stmt
);
5474 tree true_test_var
= NULL_TREE
;
5475 tree false_test_var
= NULL_TREE
;
5476 enum tree_code innercode
= gimple_assign_rhs_code (stmt
);
5478 /* Check for identities like (var AND (var == 0)) => false. */
5479 if (TREE_CODE (op2a
) == SSA_NAME
5480 && TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
)
5482 if ((code2
== NE_EXPR
&& integer_zerop (op2b
))
5483 || (code2
== EQ_EXPR
&& integer_nonzerop (op2b
)))
5485 true_test_var
= op2a
;
5486 if (var
== true_test_var
)
5489 else if ((code2
== EQ_EXPR
&& integer_zerop (op2b
))
5490 || (code2
== NE_EXPR
&& integer_nonzerop (op2b
)))
5492 false_test_var
= op2a
;
5493 if (var
== false_test_var
)
5494 return boolean_false_node
;
5498 /* If the definition is a comparison, recurse on it. */
5499 if (TREE_CODE_CLASS (innercode
) == tcc_comparison
)
5501 tree t
= and_comparisons_1 (type
, innercode
,
5502 gimple_assign_rhs1 (stmt
),
5503 gimple_assign_rhs2 (stmt
),
5511 /* If the definition is an AND or OR expression, we may be able to
5512 simplify by reassociating. */
5513 if (TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
5514 && (innercode
== BIT_AND_EXPR
|| innercode
== BIT_IOR_EXPR
))
5516 tree inner1
= gimple_assign_rhs1 (stmt
);
5517 tree inner2
= gimple_assign_rhs2 (stmt
);
5520 tree partial
= NULL_TREE
;
5521 bool is_and
= (innercode
== BIT_AND_EXPR
);
5523 /* Check for boolean identities that don't require recursive examination
5525 inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
5526 inner1 AND (inner1 OR inner2) => inner1
5527 !inner1 AND (inner1 AND inner2) => false
5528 !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
5529 Likewise for similar cases involving inner2. */
5530 if (inner1
== true_test_var
)
5531 return (is_and
? var
: inner1
);
5532 else if (inner2
== true_test_var
)
5533 return (is_and
? var
: inner2
);
5534 else if (inner1
== false_test_var
)
5536 ? boolean_false_node
5537 : and_var_with_comparison (type
, inner2
, false, code2
, op2a
,
5539 else if (inner2
== false_test_var
)
5541 ? boolean_false_node
5542 : and_var_with_comparison (type
, inner1
, false, code2
, op2a
,
5545 /* Next, redistribute/reassociate the AND across the inner tests.
5546 Compute the first partial result, (inner1 AND (op2a code op2b)) */
5547 if (TREE_CODE (inner1
) == SSA_NAME
5548 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner1
))
5549 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
5550 && (t
= maybe_fold_and_comparisons (type
, gimple_assign_rhs_code (s
),
5551 gimple_assign_rhs1 (s
),
5552 gimple_assign_rhs2 (s
),
5553 code2
, op2a
, op2b
)))
5555 /* Handle the AND case, where we are reassociating:
5556 (inner1 AND inner2) AND (op2a code2 op2b)
5558 If the partial result t is a constant, we win. Otherwise
5559 continue on to try reassociating with the other inner test. */
5562 if (integer_onep (t
))
5564 else if (integer_zerop (t
))
5565 return boolean_false_node
;
5568 /* Handle the OR case, where we are redistributing:
5569 (inner1 OR inner2) AND (op2a code2 op2b)
5570 => (t OR (inner2 AND (op2a code2 op2b))) */
5571 else if (integer_onep (t
))
5572 return boolean_true_node
;
5574 /* Save partial result for later. */
5578 /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
5579 if (TREE_CODE (inner2
) == SSA_NAME
5580 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner2
))
5581 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
5582 && (t
= maybe_fold_and_comparisons (type
, gimple_assign_rhs_code (s
),
5583 gimple_assign_rhs1 (s
),
5584 gimple_assign_rhs2 (s
),
5585 code2
, op2a
, op2b
)))
5587 /* Handle the AND case, where we are reassociating:
5588 (inner1 AND inner2) AND (op2a code2 op2b)
5589 => (inner1 AND t) */
5592 if (integer_onep (t
))
5594 else if (integer_zerop (t
))
5595 return boolean_false_node
;
5596 /* If both are the same, we can apply the identity
5598 else if (partial
&& same_bool_result_p (t
, partial
))
5602 /* Handle the OR case. where we are redistributing:
5603 (inner1 OR inner2) AND (op2a code2 op2b)
5604 => (t OR (inner1 AND (op2a code2 op2b)))
5605 => (t OR partial) */
5608 if (integer_onep (t
))
5609 return boolean_true_node
;
5612 /* We already got a simplification for the other
5613 operand to the redistributed OR expression. The
5614 interesting case is when at least one is false.
5615 Or, if both are the same, we can apply the identity
5617 if (integer_zerop (partial
))
5619 else if (integer_zerop (t
))
5621 else if (same_bool_result_p (t
, partial
))
5630 /* Try to simplify the AND of two comparisons defined by
5631 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
5632 If this can be done without constructing an intermediate value,
5633 return the resulting tree; otherwise NULL_TREE is returned.
5634 This function is deliberately asymmetric as it recurses on SSA_DEFs
5635 in the first comparison but not the second. */
5638 and_comparisons_1 (tree type
, enum tree_code code1
, tree op1a
, tree op1b
,
5639 enum tree_code code2
, tree op2a
, tree op2b
)
5641 tree truth_type
= truth_type_for (TREE_TYPE (op1a
));
5643 /* First check for ((x CODE1 y) AND (x CODE2 y)). */
5644 if (operand_equal_p (op1a
, op2a
, 0)
5645 && operand_equal_p (op1b
, op2b
, 0))
5647 /* Result will be either NULL_TREE, or a combined comparison. */
5648 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
5649 TRUTH_ANDIF_EXPR
, code1
, code2
,
5650 truth_type
, op1a
, op1b
);
5655 /* Likewise the swapped case of the above. */
5656 if (operand_equal_p (op1a
, op2b
, 0)
5657 && operand_equal_p (op1b
, op2a
, 0))
5659 /* Result will be either NULL_TREE, or a combined comparison. */
5660 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
5661 TRUTH_ANDIF_EXPR
, code1
,
5662 swap_tree_comparison (code2
),
5663 truth_type
, op1a
, op1b
);
5668 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
5669 NAME's definition is a truth value. See if there are any simplifications
5670 that can be done against the NAME's definition. */
5671 if (TREE_CODE (op1a
) == SSA_NAME
5672 && (code1
== NE_EXPR
|| code1
== EQ_EXPR
)
5673 && (integer_zerop (op1b
) || integer_onep (op1b
)))
5675 bool invert
= ((code1
== EQ_EXPR
&& integer_zerop (op1b
))
5676 || (code1
== NE_EXPR
&& integer_onep (op1b
)));
5677 gimple
*stmt
= SSA_NAME_DEF_STMT (op1a
);
5678 switch (gimple_code (stmt
))
5681 /* Try to simplify by copy-propagating the definition. */
5682 return and_var_with_comparison (type
, op1a
, invert
, code2
, op2a
,
5686 /* If every argument to the PHI produces the same result when
5687 ANDed with the second comparison, we win.
5688 Do not do this unless the type is bool since we need a bool
5689 result here anyway. */
5690 if (TREE_CODE (TREE_TYPE (op1a
)) == BOOLEAN_TYPE
)
5692 tree result
= NULL_TREE
;
5694 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
5696 tree arg
= gimple_phi_arg_def (stmt
, i
);
5698 /* If this PHI has itself as an argument, ignore it.
5699 If all the other args produce the same result,
5701 if (arg
== gimple_phi_result (stmt
))
5703 else if (TREE_CODE (arg
) == INTEGER_CST
)
5705 if (invert
? integer_nonzerop (arg
) : integer_zerop (arg
))
5708 result
= boolean_false_node
;
5709 else if (!integer_zerop (result
))
5713 result
= fold_build2 (code2
, boolean_type_node
,
5715 else if (!same_bool_comparison_p (result
,
5719 else if (TREE_CODE (arg
) == SSA_NAME
5720 && !SSA_NAME_IS_DEFAULT_DEF (arg
))
5723 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
5724 /* In simple cases we can look through PHI nodes,
5725 but we have to be careful with loops.
5727 if (! dom_info_available_p (CDI_DOMINATORS
)
5728 || gimple_bb (def_stmt
) == gimple_bb (stmt
)
5729 || dominated_by_p (CDI_DOMINATORS
,
5730 gimple_bb (def_stmt
),
5733 temp
= and_var_with_comparison (type
, arg
, invert
, code2
,
5739 else if (!same_bool_result_p (result
, temp
))
5755 /* Helper function for maybe_fold_and_comparisons and maybe_fold_or_comparisons
5756 : try to simplify the AND/OR of the ssa variable VAR with the comparison
5757 specified by (OP2A CODE2 OP2B) from match.pd. Return NULL_EXPR if we can't
5758 simplify this to a single expression. As we are going to lower the cost
5759 of building SSA names / gimple stmts significantly, we need to allocate
5760 them ont the stack. This will cause the code to be a bit ugly. */
5763 maybe_fold_comparisons_from_match_pd (tree type
, enum tree_code code
,
5764 enum tree_code code1
,
5765 tree op1a
, tree op1b
,
5766 enum tree_code code2
, tree op2a
,
5769 /* Allocate gimple stmt1 on the stack. */
5771 = (gassign
*) XALLOCAVEC (char, gimple_size (GIMPLE_ASSIGN
, 3));
5772 gimple_init (stmt1
, GIMPLE_ASSIGN
, 3);
5773 gimple_assign_set_rhs_code (stmt1
, code1
);
5774 gimple_assign_set_rhs1 (stmt1
, op1a
);
5775 gimple_assign_set_rhs2 (stmt1
, op1b
);
5777 /* Allocate gimple stmt2 on the stack. */
5779 = (gassign
*) XALLOCAVEC (char, gimple_size (GIMPLE_ASSIGN
, 3));
5780 gimple_init (stmt2
, GIMPLE_ASSIGN
, 3);
5781 gimple_assign_set_rhs_code (stmt2
, code2
);
5782 gimple_assign_set_rhs1 (stmt2
, op2a
);
5783 gimple_assign_set_rhs2 (stmt2
, op2b
);
5785 /* Allocate SSA names(lhs1) on the stack. */
5786 tree lhs1
= (tree
)XALLOCA (tree_ssa_name
);
5787 memset (lhs1
, 0, sizeof (tree_ssa_name
));
5788 TREE_SET_CODE (lhs1
, SSA_NAME
);
5789 TREE_TYPE (lhs1
) = type
;
5790 init_ssa_name_imm_use (lhs1
);
5792 /* Allocate SSA names(lhs2) on the stack. */
5793 tree lhs2
= (tree
)XALLOCA (tree_ssa_name
);
5794 memset (lhs2
, 0, sizeof (tree_ssa_name
));
5795 TREE_SET_CODE (lhs2
, SSA_NAME
);
5796 TREE_TYPE (lhs2
) = type
;
5797 init_ssa_name_imm_use (lhs2
);
5799 gimple_assign_set_lhs (stmt1
, lhs1
);
5800 gimple_assign_set_lhs (stmt2
, lhs2
);
5802 gimple_match_op
op (gimple_match_cond::UNCOND
, code
,
5803 type
, gimple_assign_lhs (stmt1
),
5804 gimple_assign_lhs (stmt2
));
5805 if (op
.resimplify (NULL
, follow_all_ssa_edges
))
5807 if (gimple_simplified_result_is_gimple_val (&op
))
5809 tree res
= op
.ops
[0];
5811 return build2 (code1
, type
, op1a
, op1b
);
5812 else if (res
== lhs2
)
5813 return build2 (code2
, type
, op2a
, op2b
);
5817 else if (op
.code
.is_tree_code ()
5818 && TREE_CODE_CLASS ((tree_code
)op
.code
) == tcc_comparison
)
5820 tree op0
= op
.ops
[0];
5821 tree op1
= op
.ops
[1];
5822 if (op0
== lhs1
|| op0
== lhs2
|| op1
== lhs1
|| op1
== lhs2
)
5823 return NULL_TREE
; /* not simple */
5825 return build2 ((enum tree_code
)op
.code
, op
.type
, op0
, op1
);
5832 /* Try to simplify the AND of two comparisons, specified by
5833 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
5834 If this can be simplified to a single expression (without requiring
5835 introducing more SSA variables to hold intermediate values),
5836 return the resulting tree. Otherwise return NULL_TREE.
5837 If the result expression is non-null, it has boolean type. */
5840 maybe_fold_and_comparisons (tree type
,
5841 enum tree_code code1
, tree op1a
, tree op1b
,
5842 enum tree_code code2
, tree op2a
, tree op2b
)
5844 if (tree t
= and_comparisons_1 (type
, code1
, op1a
, op1b
, code2
, op2a
, op2b
))
5847 if (tree t
= and_comparisons_1 (type
, code2
, op2a
, op2b
, code1
, op1a
, op1b
))
5850 if (tree t
= maybe_fold_comparisons_from_match_pd (type
, BIT_AND_EXPR
, code1
,
5851 op1a
, op1b
, code2
, op2a
,
5858 /* Helper function for or_comparisons_1: try to simplify the OR of the
5859 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
5860 If INVERT is true, invert the value of VAR before doing the OR.
5861 Return NULL_EXPR if we can't simplify this to a single expression. */
5864 or_var_with_comparison (tree type
, tree var
, bool invert
,
5865 enum tree_code code2
, tree op2a
, tree op2b
)
5868 gimple
*stmt
= SSA_NAME_DEF_STMT (var
);
5870 /* We can only deal with variables whose definitions are assignments. */
5871 if (!is_gimple_assign (stmt
))
5874 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
5875 !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
5876 Then we only have to consider the simpler non-inverted cases. */
5878 t
= and_var_with_comparison_1 (type
, stmt
,
5879 invert_tree_comparison (code2
, false),
5882 t
= or_var_with_comparison_1 (type
, stmt
, code2
, op2a
, op2b
);
5883 return canonicalize_bool (t
, invert
);
5886 /* Try to simplify the OR of the ssa variable defined by the assignment
5887 STMT with the comparison specified by (OP2A CODE2 OP2B).
5888 Return NULL_EXPR if we can't simplify this to a single expression. */
5891 or_var_with_comparison_1 (tree type
, gimple
*stmt
,
5892 enum tree_code code2
, tree op2a
, tree op2b
)
5894 tree var
= gimple_assign_lhs (stmt
);
5895 tree true_test_var
= NULL_TREE
;
5896 tree false_test_var
= NULL_TREE
;
5897 enum tree_code innercode
= gimple_assign_rhs_code (stmt
);
5899 /* Check for identities like (var OR (var != 0)) => true . */
5900 if (TREE_CODE (op2a
) == SSA_NAME
5901 && TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
)
5903 if ((code2
== NE_EXPR
&& integer_zerop (op2b
))
5904 || (code2
== EQ_EXPR
&& integer_nonzerop (op2b
)))
5906 true_test_var
= op2a
;
5907 if (var
== true_test_var
)
5910 else if ((code2
== EQ_EXPR
&& integer_zerop (op2b
))
5911 || (code2
== NE_EXPR
&& integer_nonzerop (op2b
)))
5913 false_test_var
= op2a
;
5914 if (var
== false_test_var
)
5915 return boolean_true_node
;
5919 /* If the definition is a comparison, recurse on it. */
5920 if (TREE_CODE_CLASS (innercode
) == tcc_comparison
)
5922 tree t
= or_comparisons_1 (type
, innercode
,
5923 gimple_assign_rhs1 (stmt
),
5924 gimple_assign_rhs2 (stmt
),
5932 /* If the definition is an AND or OR expression, we may be able to
5933 simplify by reassociating. */
5934 if (TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
5935 && (innercode
== BIT_AND_EXPR
|| innercode
== BIT_IOR_EXPR
))
5937 tree inner1
= gimple_assign_rhs1 (stmt
);
5938 tree inner2
= gimple_assign_rhs2 (stmt
);
5941 tree partial
= NULL_TREE
;
5942 bool is_or
= (innercode
== BIT_IOR_EXPR
);
5944 /* Check for boolean identities that don't require recursive examination
5946 inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
5947 inner1 OR (inner1 AND inner2) => inner1
5948 !inner1 OR (inner1 OR inner2) => true
5949 !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
5951 if (inner1
== true_test_var
)
5952 return (is_or
? var
: inner1
);
5953 else if (inner2
== true_test_var
)
5954 return (is_or
? var
: inner2
);
5955 else if (inner1
== false_test_var
)
5958 : or_var_with_comparison (type
, inner2
, false, code2
, op2a
,
5960 else if (inner2
== false_test_var
)
5963 : or_var_with_comparison (type
, inner1
, false, code2
, op2a
,
5966 /* Next, redistribute/reassociate the OR across the inner tests.
5967 Compute the first partial result, (inner1 OR (op2a code op2b)) */
5968 if (TREE_CODE (inner1
) == SSA_NAME
5969 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner1
))
5970 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
5971 && (t
= maybe_fold_or_comparisons (type
, gimple_assign_rhs_code (s
),
5972 gimple_assign_rhs1 (s
),
5973 gimple_assign_rhs2 (s
),
5974 code2
, op2a
, op2b
)))
5976 /* Handle the OR case, where we are reassociating:
5977 (inner1 OR inner2) OR (op2a code2 op2b)
5979 If the partial result t is a constant, we win. Otherwise
5980 continue on to try reassociating with the other inner test. */
5983 if (integer_onep (t
))
5984 return boolean_true_node
;
5985 else if (integer_zerop (t
))
5989 /* Handle the AND case, where we are redistributing:
5990 (inner1 AND inner2) OR (op2a code2 op2b)
5991 => (t AND (inner2 OR (op2a code op2b))) */
5992 else if (integer_zerop (t
))
5993 return boolean_false_node
;
5995 /* Save partial result for later. */
5999 /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
6000 if (TREE_CODE (inner2
) == SSA_NAME
6001 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner2
))
6002 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
6003 && (t
= maybe_fold_or_comparisons (type
, gimple_assign_rhs_code (s
),
6004 gimple_assign_rhs1 (s
),
6005 gimple_assign_rhs2 (s
),
6006 code2
, op2a
, op2b
)))
6008 /* Handle the OR case, where we are reassociating:
6009 (inner1 OR inner2) OR (op2a code2 op2b)
6011 => (t OR partial) */
6014 if (integer_zerop (t
))
6016 else if (integer_onep (t
))
6017 return boolean_true_node
;
6018 /* If both are the same, we can apply the identity
6020 else if (partial
&& same_bool_result_p (t
, partial
))
6024 /* Handle the AND case, where we are redistributing:
6025 (inner1 AND inner2) OR (op2a code2 op2b)
6026 => (t AND (inner1 OR (op2a code2 op2b)))
6027 => (t AND partial) */
6030 if (integer_zerop (t
))
6031 return boolean_false_node
;
6034 /* We already got a simplification for the other
6035 operand to the redistributed AND expression. The
6036 interesting case is when at least one is true.
6037 Or, if both are the same, we can apply the identity
6039 if (integer_onep (partial
))
6041 else if (integer_onep (t
))
6043 else if (same_bool_result_p (t
, partial
))
6052 /* Try to simplify the OR of two comparisons defined by
6053 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
6054 If this can be done without constructing an intermediate value,
6055 return the resulting tree; otherwise NULL_TREE is returned.
6056 This function is deliberately asymmetric as it recurses on SSA_DEFs
6057 in the first comparison but not the second. */
6060 or_comparisons_1 (tree type
, enum tree_code code1
, tree op1a
, tree op1b
,
6061 enum tree_code code2
, tree op2a
, tree op2b
)
6063 tree truth_type
= truth_type_for (TREE_TYPE (op1a
));
6065 /* First check for ((x CODE1 y) OR (x CODE2 y)). */
6066 if (operand_equal_p (op1a
, op2a
, 0)
6067 && operand_equal_p (op1b
, op2b
, 0))
6069 /* Result will be either NULL_TREE, or a combined comparison. */
6070 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
6071 TRUTH_ORIF_EXPR
, code1
, code2
,
6072 truth_type
, op1a
, op1b
);
6077 /* Likewise the swapped case of the above. */
6078 if (operand_equal_p (op1a
, op2b
, 0)
6079 && operand_equal_p (op1b
, op2a
, 0))
6081 /* Result will be either NULL_TREE, or a combined comparison. */
6082 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
6083 TRUTH_ORIF_EXPR
, code1
,
6084 swap_tree_comparison (code2
),
6085 truth_type
, op1a
, op1b
);
6090 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
6091 NAME's definition is a truth value. See if there are any simplifications
6092 that can be done against the NAME's definition. */
6093 if (TREE_CODE (op1a
) == SSA_NAME
6094 && (code1
== NE_EXPR
|| code1
== EQ_EXPR
)
6095 && (integer_zerop (op1b
) || integer_onep (op1b
)))
6097 bool invert
= ((code1
== EQ_EXPR
&& integer_zerop (op1b
))
6098 || (code1
== NE_EXPR
&& integer_onep (op1b
)));
6099 gimple
*stmt
= SSA_NAME_DEF_STMT (op1a
);
6100 switch (gimple_code (stmt
))
6103 /* Try to simplify by copy-propagating the definition. */
6104 return or_var_with_comparison (type
, op1a
, invert
, code2
, op2a
,
6108 /* If every argument to the PHI produces the same result when
6109 ORed with the second comparison, we win.
6110 Do not do this unless the type is bool since we need a bool
6111 result here anyway. */
6112 if (TREE_CODE (TREE_TYPE (op1a
)) == BOOLEAN_TYPE
)
6114 tree result
= NULL_TREE
;
6116 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
6118 tree arg
= gimple_phi_arg_def (stmt
, i
);
6120 /* If this PHI has itself as an argument, ignore it.
6121 If all the other args produce the same result,
6123 if (arg
== gimple_phi_result (stmt
))
6125 else if (TREE_CODE (arg
) == INTEGER_CST
)
6127 if (invert
? integer_zerop (arg
) : integer_nonzerop (arg
))
6130 result
= boolean_true_node
;
6131 else if (!integer_onep (result
))
6135 result
= fold_build2 (code2
, boolean_type_node
,
6137 else if (!same_bool_comparison_p (result
,
6141 else if (TREE_CODE (arg
) == SSA_NAME
6142 && !SSA_NAME_IS_DEFAULT_DEF (arg
))
6145 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
6146 /* In simple cases we can look through PHI nodes,
6147 but we have to be careful with loops.
6149 if (! dom_info_available_p (CDI_DOMINATORS
)
6150 || gimple_bb (def_stmt
) == gimple_bb (stmt
)
6151 || dominated_by_p (CDI_DOMINATORS
,
6152 gimple_bb (def_stmt
),
6155 temp
= or_var_with_comparison (type
, arg
, invert
, code2
,
6161 else if (!same_bool_result_p (result
, temp
))
6177 /* Try to simplify the OR of two comparisons, specified by
6178 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
6179 If this can be simplified to a single expression (without requiring
6180 introducing more SSA variables to hold intermediate values),
6181 return the resulting tree. Otherwise return NULL_TREE.
6182 If the result expression is non-null, it has boolean type. */
6185 maybe_fold_or_comparisons (tree type
,
6186 enum tree_code code1
, tree op1a
, tree op1b
,
6187 enum tree_code code2
, tree op2a
, tree op2b
)
6189 if (tree t
= or_comparisons_1 (type
, code1
, op1a
, op1b
, code2
, op2a
, op2b
))
6192 if (tree t
= or_comparisons_1 (type
, code2
, op2a
, op2b
, code1
, op1a
, op1b
))
6195 if (tree t
= maybe_fold_comparisons_from_match_pd (type
, BIT_IOR_EXPR
, code1
,
6196 op1a
, op1b
, code2
, op2a
,
6203 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
6205 Either NULL_TREE, a simplified but non-constant or a constant
6208 ??? This should go into a gimple-fold-inline.h file to be eventually
6209 privatized with the single valueize function used in the various TUs
6210 to avoid the indirect function call overhead. */
6213 gimple_fold_stmt_to_constant_1 (gimple
*stmt
, tree (*valueize
) (tree
),
6214 tree (*gvalueize
) (tree
))
6216 gimple_match_op res_op
;
6217 /* ??? The SSA propagators do not correctly deal with following SSA use-def
6218 edges if there are intermediate VARYING defs. For this reason
6219 do not follow SSA edges here even though SCCVN can technically
6220 just deal fine with that. */
6221 if (gimple_simplify (stmt
, &res_op
, NULL
, gvalueize
, valueize
))
6223 tree res
= NULL_TREE
;
6224 if (gimple_simplified_result_is_gimple_val (&res_op
))
6225 res
= res_op
.ops
[0];
6226 else if (mprts_hook
)
6227 res
= mprts_hook (&res_op
);
6230 if (dump_file
&& dump_flags
& TDF_DETAILS
)
6232 fprintf (dump_file
, "Match-and-simplified ");
6233 print_gimple_expr (dump_file
, stmt
, 0, TDF_SLIM
);
6234 fprintf (dump_file
, " to ");
6235 print_generic_expr (dump_file
, res
);
6236 fprintf (dump_file
, "\n");
6242 location_t loc
= gimple_location (stmt
);
6243 switch (gimple_code (stmt
))
6247 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
6249 switch (get_gimple_rhs_class (subcode
))
6251 case GIMPLE_SINGLE_RHS
:
6253 tree rhs
= gimple_assign_rhs1 (stmt
);
6254 enum tree_code_class kind
= TREE_CODE_CLASS (subcode
);
6256 if (TREE_CODE (rhs
) == SSA_NAME
)
6258 /* If the RHS is an SSA_NAME, return its known constant value,
6260 return (*valueize
) (rhs
);
6262 /* Handle propagating invariant addresses into address
6264 else if (TREE_CODE (rhs
) == ADDR_EXPR
6265 && !is_gimple_min_invariant (rhs
))
6267 poly_int64 offset
= 0;
6269 base
= get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs
, 0),
6273 && (CONSTANT_CLASS_P (base
)
6274 || decl_address_invariant_p (base
)))
6275 return build_invariant_address (TREE_TYPE (rhs
),
6278 else if (TREE_CODE (rhs
) == CONSTRUCTOR
6279 && TREE_CODE (TREE_TYPE (rhs
)) == VECTOR_TYPE
6280 && known_eq (CONSTRUCTOR_NELTS (rhs
),
6281 TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs
))))
6286 nelts
= CONSTRUCTOR_NELTS (rhs
);
6287 tree_vector_builder
vec (TREE_TYPE (rhs
), nelts
, 1);
6288 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs
), i
, val
)
6290 val
= (*valueize
) (val
);
6291 if (TREE_CODE (val
) == INTEGER_CST
6292 || TREE_CODE (val
) == REAL_CST
6293 || TREE_CODE (val
) == FIXED_CST
)
6294 vec
.quick_push (val
);
6299 return vec
.build ();
6301 if (subcode
== OBJ_TYPE_REF
)
6303 tree val
= (*valueize
) (OBJ_TYPE_REF_EXPR (rhs
));
6304 /* If callee is constant, we can fold away the wrapper. */
6305 if (is_gimple_min_invariant (val
))
6309 if (kind
== tcc_reference
)
6311 if ((TREE_CODE (rhs
) == VIEW_CONVERT_EXPR
6312 || TREE_CODE (rhs
) == REALPART_EXPR
6313 || TREE_CODE (rhs
) == IMAGPART_EXPR
)
6314 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
6316 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
6317 return fold_unary_loc (EXPR_LOCATION (rhs
),
6319 TREE_TYPE (rhs
), val
);
6321 else if (TREE_CODE (rhs
) == BIT_FIELD_REF
6322 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
6324 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
6325 return fold_ternary_loc (EXPR_LOCATION (rhs
),
6327 TREE_TYPE (rhs
), val
,
6328 TREE_OPERAND (rhs
, 1),
6329 TREE_OPERAND (rhs
, 2));
6331 else if (TREE_CODE (rhs
) == MEM_REF
6332 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
6334 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
6335 if (TREE_CODE (val
) == ADDR_EXPR
6336 && is_gimple_min_invariant (val
))
6338 tree tem
= fold_build2 (MEM_REF
, TREE_TYPE (rhs
),
6340 TREE_OPERAND (rhs
, 1));
6345 return fold_const_aggregate_ref_1 (rhs
, valueize
);
6347 else if (kind
== tcc_declaration
)
6348 return get_symbol_constant_value (rhs
);
6352 case GIMPLE_UNARY_RHS
:
6355 case GIMPLE_BINARY_RHS
:
6356 /* Translate &x + CST into an invariant form suitable for
6357 further propagation. */
6358 if (subcode
== POINTER_PLUS_EXPR
)
6360 tree op0
= (*valueize
) (gimple_assign_rhs1 (stmt
));
6361 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
6362 if (TREE_CODE (op0
) == ADDR_EXPR
6363 && TREE_CODE (op1
) == INTEGER_CST
)
6365 tree off
= fold_convert (ptr_type_node
, op1
);
6366 return build_fold_addr_expr_loc
6368 fold_build2 (MEM_REF
,
6369 TREE_TYPE (TREE_TYPE (op0
)),
6370 unshare_expr (op0
), off
));
6373 /* Canonicalize bool != 0 and bool == 0 appearing after
6374 valueization. While gimple_simplify handles this
6375 it can get confused by the ~X == 1 -> X == 0 transform
6376 which we cant reduce to a SSA name or a constant
6377 (and we have no way to tell gimple_simplify to not
6378 consider those transforms in the first place). */
6379 else if (subcode
== EQ_EXPR
6380 || subcode
== NE_EXPR
)
6382 tree lhs
= gimple_assign_lhs (stmt
);
6383 tree op0
= gimple_assign_rhs1 (stmt
);
6384 if (useless_type_conversion_p (TREE_TYPE (lhs
),
6387 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
6388 op0
= (*valueize
) (op0
);
6389 if (TREE_CODE (op0
) == INTEGER_CST
)
6390 std::swap (op0
, op1
);
6391 if (TREE_CODE (op1
) == INTEGER_CST
6392 && ((subcode
== NE_EXPR
&& integer_zerop (op1
))
6393 || (subcode
== EQ_EXPR
&& integer_onep (op1
))))
6399 case GIMPLE_TERNARY_RHS
:
6401 /* Handle ternary operators that can appear in GIMPLE form. */
6402 tree op0
= (*valueize
) (gimple_assign_rhs1 (stmt
));
6403 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
6404 tree op2
= (*valueize
) (gimple_assign_rhs3 (stmt
));
6405 return fold_ternary_loc (loc
, subcode
,
6406 gimple_expr_type (stmt
), op0
, op1
, op2
);
6417 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
6419 if (gimple_call_internal_p (stmt
))
6421 enum tree_code subcode
= ERROR_MARK
;
6422 switch (gimple_call_internal_fn (stmt
))
6424 case IFN_UBSAN_CHECK_ADD
:
6425 subcode
= PLUS_EXPR
;
6427 case IFN_UBSAN_CHECK_SUB
:
6428 subcode
= MINUS_EXPR
;
6430 case IFN_UBSAN_CHECK_MUL
:
6431 subcode
= MULT_EXPR
;
6433 case IFN_BUILTIN_EXPECT
:
6435 tree arg0
= gimple_call_arg (stmt
, 0);
6436 tree op0
= (*valueize
) (arg0
);
6437 if (TREE_CODE (op0
) == INTEGER_CST
)
6444 tree arg0
= gimple_call_arg (stmt
, 0);
6445 tree arg1
= gimple_call_arg (stmt
, 1);
6446 tree op0
= (*valueize
) (arg0
);
6447 tree op1
= (*valueize
) (arg1
);
6449 if (TREE_CODE (op0
) != INTEGER_CST
6450 || TREE_CODE (op1
) != INTEGER_CST
)
6455 /* x * 0 = 0 * x = 0 without overflow. */
6456 if (integer_zerop (op0
) || integer_zerop (op1
))
6457 return build_zero_cst (TREE_TYPE (arg0
));
6460 /* y - y = 0 without overflow. */
6461 if (operand_equal_p (op0
, op1
, 0))
6462 return build_zero_cst (TREE_TYPE (arg0
));
6469 = fold_binary_loc (loc
, subcode
, TREE_TYPE (arg0
), op0
, op1
);
6471 && TREE_CODE (res
) == INTEGER_CST
6472 && !TREE_OVERFLOW (res
))
6477 fn
= (*valueize
) (gimple_call_fn (stmt
));
6478 if (TREE_CODE (fn
) == ADDR_EXPR
6479 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
6480 && fndecl_built_in_p (TREE_OPERAND (fn
, 0))
6481 && gimple_builtin_call_types_compatible_p (stmt
,
6482 TREE_OPERAND (fn
, 0)))
6484 tree
*args
= XALLOCAVEC (tree
, gimple_call_num_args (stmt
));
6487 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
6488 args
[i
] = (*valueize
) (gimple_call_arg (stmt
, i
));
6489 retval
= fold_builtin_call_array (loc
,
6490 gimple_call_return_type (call_stmt
),
6491 fn
, gimple_call_num_args (stmt
), args
);
6494 /* fold_call_expr wraps the result inside a NOP_EXPR. */
6495 STRIP_NOPS (retval
);
6496 retval
= fold_convert (gimple_call_return_type (call_stmt
),
6509 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
6510 Returns NULL_TREE if folding to a constant is not possible, otherwise
6511 returns a constant according to is_gimple_min_invariant. */
6514 gimple_fold_stmt_to_constant (gimple
*stmt
, tree (*valueize
) (tree
))
6516 tree res
= gimple_fold_stmt_to_constant_1 (stmt
, valueize
);
6517 if (res
&& is_gimple_min_invariant (res
))
6523 /* The following set of functions are supposed to fold references using
6524 their constant initializers. */
6526 /* See if we can find constructor defining value of BASE.
6527 When we know the consructor with constant offset (such as
6528 base is array[40] and we do know constructor of array), then
6529 BIT_OFFSET is adjusted accordingly.
6531 As a special case, return error_mark_node when constructor
6532 is not explicitly available, but it is known to be zero
6533 such as 'static const int a;'. */
6535 get_base_constructor (tree base
, poly_int64_pod
*bit_offset
,
6536 tree (*valueize
)(tree
))
6538 poly_int64 bit_offset2
, size
, max_size
;
6541 if (TREE_CODE (base
) == MEM_REF
)
6543 poly_offset_int boff
= *bit_offset
+ mem_ref_offset (base
) * BITS_PER_UNIT
;
6544 if (!boff
.to_shwi (bit_offset
))
6548 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
6549 base
= valueize (TREE_OPERAND (base
, 0));
6550 if (!base
|| TREE_CODE (base
) != ADDR_EXPR
)
6552 base
= TREE_OPERAND (base
, 0);
6555 && TREE_CODE (base
) == SSA_NAME
)
6556 base
= valueize (base
);
6558 /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
6559 DECL_INITIAL. If BASE is a nested reference into another
6560 ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
6561 the inner reference. */
6562 switch (TREE_CODE (base
))
6567 tree init
= ctor_for_folding (base
);
6569 /* Our semantic is exact opposite of ctor_for_folding;
6570 NULL means unknown, while error_mark_node is 0. */
6571 if (init
== error_mark_node
)
6574 return error_mark_node
;
6578 case VIEW_CONVERT_EXPR
:
6579 return get_base_constructor (TREE_OPERAND (base
, 0),
6580 bit_offset
, valueize
);
6584 base
= get_ref_base_and_extent (base
, &bit_offset2
, &size
, &max_size
,
6586 if (!known_size_p (max_size
) || maybe_ne (size
, max_size
))
6588 *bit_offset
+= bit_offset2
;
6589 return get_base_constructor (base
, bit_offset
, valueize
);
6595 if (CONSTANT_CLASS_P (base
))
6602 /* CTOR is CONSTRUCTOR of an array type. Fold a reference of SIZE bits
6603 to the memory at bit OFFSET. When non-null, TYPE is the expected
6604 type of the reference; otherwise the type of the referenced element
6605 is used instead. When SIZE is zero, attempt to fold a reference to
6606 the entire element which OFFSET refers to. Increment *SUBOFF by
6607 the bit offset of the accessed element. */
6610 fold_array_ctor_reference (tree type
, tree ctor
,
6611 unsigned HOST_WIDE_INT offset
,
6612 unsigned HOST_WIDE_INT size
,
6614 unsigned HOST_WIDE_INT
*suboff
)
6616 offset_int low_bound
;
6617 offset_int elt_size
;
6618 offset_int access_index
;
6619 tree domain_type
= NULL_TREE
;
6620 HOST_WIDE_INT inner_offset
;
6622 /* Compute low bound and elt size. */
6623 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
)
6624 domain_type
= TYPE_DOMAIN (TREE_TYPE (ctor
));
6625 if (domain_type
&& TYPE_MIN_VALUE (domain_type
))
6627 /* Static constructors for variably sized objects make no sense. */
6628 if (TREE_CODE (TYPE_MIN_VALUE (domain_type
)) != INTEGER_CST
)
6630 low_bound
= wi::to_offset (TYPE_MIN_VALUE (domain_type
));
6634 /* Static constructors for variably sized objects make no sense. */
6635 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor
)))) != INTEGER_CST
)
6637 elt_size
= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor
))));
6639 /* When TYPE is non-null, verify that it specifies a constant-sized
6640 access of a multiple of the array element size. Avoid division
6641 by zero below when ELT_SIZE is zero, such as with the result of
6642 an initializer for a zero-length array or an empty struct. */
6645 && (!TYPE_SIZE_UNIT (type
)
6646 || TREE_CODE (TYPE_SIZE_UNIT (type
)) != INTEGER_CST
)))
6649 /* Compute the array index we look for. */
6650 access_index
= wi::udiv_trunc (offset_int (offset
/ BITS_PER_UNIT
),
6652 access_index
+= low_bound
;
6654 /* And offset within the access. */
6655 inner_offset
= offset
% (elt_size
.to_uhwi () * BITS_PER_UNIT
);
6657 if (size
> elt_size
.to_uhwi () * BITS_PER_UNIT
)
6659 /* native_encode_expr constraints. */
6660 if (size
> MAX_BITSIZE_MODE_ANY_MODE
6661 || size
% BITS_PER_UNIT
!= 0
6662 || inner_offset
% BITS_PER_UNIT
!= 0)
6666 tree val
= get_array_ctor_element_at_index (ctor
, access_index
,
6668 if (!val
&& ctor_idx
>= CONSTRUCTOR_NELTS (ctor
))
6669 return build_zero_cst (type
);
6671 /* native-encode adjacent ctor elements. */
6672 unsigned char buf
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
6673 unsigned bufoff
= 0;
6674 offset_int index
= 0;
6675 offset_int max_index
= access_index
;
6676 constructor_elt
*elt
= CONSTRUCTOR_ELT (ctor
, ctor_idx
);
6678 val
= build_zero_cst (TREE_TYPE (TREE_TYPE (ctor
)));
6679 else if (!CONSTANT_CLASS_P (val
))
6683 else if (TREE_CODE (elt
->index
) == RANGE_EXPR
)
6685 index
= wi::to_offset (TREE_OPERAND (elt
->index
, 0));
6686 max_index
= wi::to_offset (TREE_OPERAND (elt
->index
, 1));
6689 index
= max_index
= wi::to_offset (elt
->index
);
6690 index
= wi::umax (index
, access_index
);
6693 int len
= native_encode_expr (val
, buf
+ bufoff
,
6694 elt_size
.to_uhwi (),
6695 inner_offset
/ BITS_PER_UNIT
);
6696 if (len
!= elt_size
- inner_offset
/ BITS_PER_UNIT
)
6702 if (wi::cmpu (access_index
, index
) == 0)
6704 else if (wi::cmpu (access_index
, max_index
) > 0)
6707 if (ctor_idx
>= CONSTRUCTOR_NELTS (ctor
))
6709 val
= build_zero_cst (TREE_TYPE (TREE_TYPE (ctor
)));
6714 elt
= CONSTRUCTOR_ELT (ctor
, ctor_idx
);
6716 max_index
= access_index
;
6719 else if (TREE_CODE (elt
->index
) == RANGE_EXPR
)
6721 index
= wi::to_offset (TREE_OPERAND (elt
->index
, 0));
6722 max_index
= wi::to_offset (TREE_OPERAND (elt
->index
, 1));
6725 index
= max_index
= wi::to_offset (elt
->index
);
6726 index
= wi::umax (index
, access_index
);
6727 if (wi::cmpu (access_index
, index
) == 0)
6730 val
= build_zero_cst (TREE_TYPE (TREE_TYPE (ctor
)));
6734 while (bufoff
< size
/ BITS_PER_UNIT
);
6736 return native_interpret_expr (type
, buf
, size
/ BITS_PER_UNIT
);
6739 if (tree val
= get_array_ctor_element_at_index (ctor
, access_index
))
6741 if (!size
&& TREE_CODE (val
) != CONSTRUCTOR
)
6743 /* For the final reference to the entire accessed element
6744 (SIZE is zero), reset INNER_OFFSET, disegard TYPE (which
6745 may be null) in favor of the type of the element, and set
6746 SIZE to the size of the accessed element. */
6748 type
= TREE_TYPE (val
);
6749 size
= elt_size
.to_uhwi () * BITS_PER_UNIT
;
6752 *suboff
+= (access_index
* elt_size
* BITS_PER_UNIT
).to_uhwi ();
6753 return fold_ctor_reference (type
, val
, inner_offset
, size
, from_decl
,
6757 /* Memory not explicitly mentioned in constructor is 0 (or
6758 the reference is out of range). */
6759 return type
? build_zero_cst (type
) : NULL_TREE
;
6762 /* CTOR is CONSTRUCTOR of an aggregate or vector. Fold a reference
6763 of SIZE bits to the memory at bit OFFSET. When non-null, TYPE
6764 is the expected type of the reference; otherwise the type of
6765 the referenced member is used instead. When SIZE is zero,
6766 attempt to fold a reference to the entire member which OFFSET
6767 refers to; in this case. Increment *SUBOFF by the bit offset
6768 of the accessed member. */
6771 fold_nonarray_ctor_reference (tree type
, tree ctor
,
6772 unsigned HOST_WIDE_INT offset
,
6773 unsigned HOST_WIDE_INT size
,
6775 unsigned HOST_WIDE_INT
*suboff
)
6777 unsigned HOST_WIDE_INT cnt
;
6780 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
,
6783 tree byte_offset
= DECL_FIELD_OFFSET (cfield
);
6784 tree field_offset
= DECL_FIELD_BIT_OFFSET (cfield
);
6785 tree field_size
= DECL_SIZE (cfield
);
6789 /* Determine the size of the flexible array member from
6790 the size of the initializer provided for it. */
6791 field_size
= TYPE_SIZE (TREE_TYPE (cval
));
6794 /* Variable sized objects in static constructors makes no sense,
6795 but field_size can be NULL for flexible array members. */
6796 gcc_assert (TREE_CODE (field_offset
) == INTEGER_CST
6797 && TREE_CODE (byte_offset
) == INTEGER_CST
6798 && (field_size
!= NULL_TREE
6799 ? TREE_CODE (field_size
) == INTEGER_CST
6800 : TREE_CODE (TREE_TYPE (cfield
)) == ARRAY_TYPE
));
6802 /* Compute bit offset of the field. */
6803 offset_int bitoffset
6804 = (wi::to_offset (field_offset
)
6805 + (wi::to_offset (byte_offset
) << LOG2_BITS_PER_UNIT
));
6806 /* Compute bit offset where the field ends. */
6807 offset_int bitoffset_end
;
6808 if (field_size
!= NULL_TREE
)
6809 bitoffset_end
= bitoffset
+ wi::to_offset (field_size
);
6813 /* Compute the bit offset of the end of the desired access.
6814 As a special case, if the size of the desired access is
6815 zero, assume the access is to the entire field (and let
6816 the caller make any necessary adjustments by storing
6817 the actual bounds of the field in FIELDBOUNDS). */
6818 offset_int access_end
= offset_int (offset
);
6822 access_end
= bitoffset_end
;
6824 /* Is there any overlap between the desired access at
6825 [OFFSET, OFFSET+SIZE) and the offset of the field within
6826 the object at [BITOFFSET, BITOFFSET_END)? */
6827 if (wi::cmps (access_end
, bitoffset
) > 0
6828 && (field_size
== NULL_TREE
6829 || wi::lts_p (offset
, bitoffset_end
)))
6831 *suboff
+= bitoffset
.to_uhwi ();
6833 if (!size
&& TREE_CODE (cval
) != CONSTRUCTOR
)
6835 /* For the final reference to the entire accessed member
6836 (SIZE is zero), reset OFFSET, disegard TYPE (which may
6837 be null) in favor of the type of the member, and set
6838 SIZE to the size of the accessed member. */
6839 offset
= bitoffset
.to_uhwi ();
6840 type
= TREE_TYPE (cval
);
6841 size
= (bitoffset_end
- bitoffset
).to_uhwi ();
6844 /* We do have overlap. Now see if the field is large enough
6845 to cover the access. Give up for accesses that extend
6846 beyond the end of the object or that span multiple fields. */
6847 if (wi::cmps (access_end
, bitoffset_end
) > 0)
6849 if (offset
< bitoffset
)
6852 offset_int inner_offset
= offset_int (offset
) - bitoffset
;
6853 return fold_ctor_reference (type
, cval
,
6854 inner_offset
.to_uhwi (), size
,
6862 return build_zero_cst (type
);
6865 /* CTOR is value initializing memory. Fold a reference of TYPE and
6866 bit size POLY_SIZE to the memory at bit POLY_OFFSET. When POLY_SIZE
6867 is zero, attempt to fold a reference to the entire subobject
6868 which OFFSET refers to. This is used when folding accesses to
6869 string members of aggregates. When non-null, set *SUBOFF to
6870 the bit offset of the accessed subobject. */
6873 fold_ctor_reference (tree type
, tree ctor
, const poly_uint64
&poly_offset
,
6874 const poly_uint64
&poly_size
, tree from_decl
,
6875 unsigned HOST_WIDE_INT
*suboff
/* = NULL */)
6879 /* We found the field with exact match. */
6881 && useless_type_conversion_p (type
, TREE_TYPE (ctor
))
6882 && known_eq (poly_offset
, 0U))
6883 return canonicalize_constructor_val (unshare_expr (ctor
), from_decl
);
6885 /* The remaining optimizations need a constant size and offset. */
6886 unsigned HOST_WIDE_INT size
, offset
;
6887 if (!poly_size
.is_constant (&size
) || !poly_offset
.is_constant (&offset
))
6890 /* We are at the end of walk, see if we can view convert the
6892 if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor
)) && !offset
6893 /* VIEW_CONVERT_EXPR is defined only for matching sizes. */
6894 && !compare_tree_int (TYPE_SIZE (type
), size
)
6895 && !compare_tree_int (TYPE_SIZE (TREE_TYPE (ctor
)), size
))
6897 ret
= canonicalize_constructor_val (unshare_expr (ctor
), from_decl
);
6900 ret
= fold_unary (VIEW_CONVERT_EXPR
, type
, ret
);
6902 STRIP_USELESS_TYPE_CONVERSION (ret
);
6906 /* For constants and byte-aligned/sized reads try to go through
6907 native_encode/interpret. */
6908 if (CONSTANT_CLASS_P (ctor
)
6909 && BITS_PER_UNIT
== 8
6910 && offset
% BITS_PER_UNIT
== 0
6911 && size
% BITS_PER_UNIT
== 0
6912 && size
<= MAX_BITSIZE_MODE_ANY_MODE
)
6914 unsigned char buf
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
6915 int len
= native_encode_expr (ctor
, buf
, size
/ BITS_PER_UNIT
,
6916 offset
/ BITS_PER_UNIT
);
6918 return native_interpret_expr (type
, buf
, len
);
6920 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
6922 unsigned HOST_WIDE_INT dummy
= 0;
6926 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
6927 || TREE_CODE (TREE_TYPE (ctor
)) == VECTOR_TYPE
)
6928 return fold_array_ctor_reference (type
, ctor
, offset
, size
,
6931 return fold_nonarray_ctor_reference (type
, ctor
, offset
, size
,
6938 /* Return the tree representing the element referenced by T if T is an
6939 ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
6940 names using VALUEIZE. Return NULL_TREE otherwise. */
6943 fold_const_aggregate_ref_1 (tree t
, tree (*valueize
) (tree
))
6945 tree ctor
, idx
, base
;
6946 poly_int64 offset
, size
, max_size
;
6950 if (TREE_THIS_VOLATILE (t
))
6954 return get_symbol_constant_value (t
);
6956 tem
= fold_read_from_constant_string (t
);
6960 switch (TREE_CODE (t
))
6963 case ARRAY_RANGE_REF
:
6964 /* Constant indexes are handled well by get_base_constructor.
6965 Only special case variable offsets.
6966 FIXME: This code can't handle nested references with variable indexes
6967 (they will be handled only by iteration of ccp). Perhaps we can bring
6968 get_ref_base_and_extent here and make it use a valueize callback. */
6969 if (TREE_CODE (TREE_OPERAND (t
, 1)) == SSA_NAME
6971 && (idx
= (*valueize
) (TREE_OPERAND (t
, 1)))
6972 && poly_int_tree_p (idx
))
6974 tree low_bound
, unit_size
;
6976 /* If the resulting bit-offset is constant, track it. */
6977 if ((low_bound
= array_ref_low_bound (t
),
6978 poly_int_tree_p (low_bound
))
6979 && (unit_size
= array_ref_element_size (t
),
6980 tree_fits_uhwi_p (unit_size
)))
6982 poly_offset_int woffset
6983 = wi::sext (wi::to_poly_offset (idx
)
6984 - wi::to_poly_offset (low_bound
),
6985 TYPE_PRECISION (TREE_TYPE (idx
)));
6986 woffset
*= tree_to_uhwi (unit_size
);
6987 woffset
*= BITS_PER_UNIT
;
6988 if (woffset
.to_shwi (&offset
))
6990 base
= TREE_OPERAND (t
, 0);
6991 ctor
= get_base_constructor (base
, &offset
, valueize
);
6992 /* Empty constructor. Always fold to 0. */
6993 if (ctor
== error_mark_node
)
6994 return build_zero_cst (TREE_TYPE (t
));
6995 /* Out of bound array access. Value is undefined,
6997 if (maybe_lt (offset
, 0))
6999 /* We cannot determine ctor. */
7002 return fold_ctor_reference (TREE_TYPE (t
), ctor
, offset
,
7003 tree_to_uhwi (unit_size
)
7013 case TARGET_MEM_REF
:
7015 base
= get_ref_base_and_extent (t
, &offset
, &size
, &max_size
, &reverse
);
7016 ctor
= get_base_constructor (base
, &offset
, valueize
);
7018 /* Empty constructor. Always fold to 0. */
7019 if (ctor
== error_mark_node
)
7020 return build_zero_cst (TREE_TYPE (t
));
7021 /* We do not know precise address. */
7022 if (!known_size_p (max_size
) || maybe_ne (max_size
, size
))
7024 /* We cannot determine ctor. */
7028 /* Out of bound array access. Value is undefined, but don't fold. */
7029 if (maybe_lt (offset
, 0))
7032 return fold_ctor_reference (TREE_TYPE (t
), ctor
, offset
, size
,
7038 tree c
= fold_const_aggregate_ref_1 (TREE_OPERAND (t
, 0), valueize
);
7039 if (c
&& TREE_CODE (c
) == COMPLEX_CST
)
7040 return fold_build1_loc (EXPR_LOCATION (t
),
7041 TREE_CODE (t
), TREE_TYPE (t
), c
);
7053 fold_const_aggregate_ref (tree t
)
7055 return fold_const_aggregate_ref_1 (t
, NULL
);
7058 /* Lookup virtual method with index TOKEN in a virtual table V
7060 Set CAN_REFER if non-NULL to false if method
7061 is not referable or if the virtual table is ill-formed (such as rewriten
7062 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
7065 gimple_get_virt_method_for_vtable (HOST_WIDE_INT token
,
7067 unsigned HOST_WIDE_INT offset
,
7070 tree vtable
= v
, init
, fn
;
7071 unsigned HOST_WIDE_INT size
;
7072 unsigned HOST_WIDE_INT elt_size
, access_index
;
7078 /* First of all double check we have virtual table. */
7079 if (!VAR_P (v
) || !DECL_VIRTUAL_P (v
))
7081 /* Pass down that we lost track of the target. */
7087 init
= ctor_for_folding (v
);
7089 /* The virtual tables should always be born with constructors
7090 and we always should assume that they are avaialble for
7091 folding. At the moment we do not stream them in all cases,
7092 but it should never happen that ctor seem unreachable. */
7094 if (init
== error_mark_node
)
7096 /* Pass down that we lost track of the target. */
7101 gcc_checking_assert (TREE_CODE (TREE_TYPE (v
)) == ARRAY_TYPE
);
7102 size
= tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v
))));
7103 offset
*= BITS_PER_UNIT
;
7104 offset
+= token
* size
;
7106 /* Lookup the value in the constructor that is assumed to be array.
7107 This is equivalent to
7108 fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
7109 offset, size, NULL);
7110 but in a constant time. We expect that frontend produced a simple
7111 array without indexed initializers. */
7113 gcc_checking_assert (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
);
7114 domain_type
= TYPE_DOMAIN (TREE_TYPE (init
));
7115 gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type
)));
7116 elt_size
= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init
))));
7118 access_index
= offset
/ BITS_PER_UNIT
/ elt_size
;
7119 gcc_checking_assert (offset
% (elt_size
* BITS_PER_UNIT
) == 0);
7121 /* The C++ FE can now produce indexed fields, and we check if the indexes
7123 if (access_index
< CONSTRUCTOR_NELTS (init
))
7125 fn
= CONSTRUCTOR_ELT (init
, access_index
)->value
;
7126 tree idx
= CONSTRUCTOR_ELT (init
, access_index
)->index
;
7127 gcc_checking_assert (!idx
|| tree_to_uhwi (idx
) == access_index
);
7133 /* For type inconsistent program we may end up looking up virtual method
7134 in virtual table that does not contain TOKEN entries. We may overrun
7135 the virtual table and pick up a constant or RTTI info pointer.
7136 In any case the call is undefined. */
7138 || (TREE_CODE (fn
) != ADDR_EXPR
&& TREE_CODE (fn
) != FDESC_EXPR
)
7139 || TREE_CODE (TREE_OPERAND (fn
, 0)) != FUNCTION_DECL
)
7140 fn
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
7143 fn
= TREE_OPERAND (fn
, 0);
7145 /* When cgraph node is missing and function is not public, we cannot
7146 devirtualize. This can happen in WHOPR when the actual method
7147 ends up in other partition, because we found devirtualization
7148 possibility too late. */
7149 if (!can_refer_decl_in_current_unit_p (fn
, vtable
))
7160 /* Make sure we create a cgraph node for functions we'll reference.
7161 They can be non-existent if the reference comes from an entry
7162 of an external vtable for example. */
7163 cgraph_node::get_create (fn
);
7168 /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
7169 is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
7170 KNOWN_BINFO carries the binfo describing the true type of
7171 OBJ_TYPE_REF_OBJECT(REF).
7172 Set CAN_REFER if non-NULL to false if method
7173 is not referable or if the virtual table is ill-formed (such as rewriten
7174 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
7177 gimple_get_virt_method_for_binfo (HOST_WIDE_INT token
, tree known_binfo
,
7180 unsigned HOST_WIDE_INT offset
;
7183 v
= BINFO_VTABLE (known_binfo
);
7184 /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */
7188 if (!vtable_pointer_value_to_vtable (v
, &v
, &offset
))
7194 return gimple_get_virt_method_for_vtable (token
, v
, offset
, can_refer
);
7197 /* Given a pointer value T, return a simplified version of an
7198 indirection through T, or NULL_TREE if no simplification is
7199 possible. Note that the resulting type may be different from
7200 the type pointed to in the sense that it is still compatible
7201 from the langhooks point of view. */
7204 gimple_fold_indirect_ref (tree t
)
7206 tree ptype
= TREE_TYPE (t
), type
= TREE_TYPE (ptype
);
7211 subtype
= TREE_TYPE (sub
);
7212 if (!POINTER_TYPE_P (subtype
)
7213 || TYPE_REF_CAN_ALIAS_ALL (ptype
))
7216 if (TREE_CODE (sub
) == ADDR_EXPR
)
7218 tree op
= TREE_OPERAND (sub
, 0);
7219 tree optype
= TREE_TYPE (op
);
7221 if (useless_type_conversion_p (type
, optype
))
7224 /* *(foo *)&fooarray => fooarray[0] */
7225 if (TREE_CODE (optype
) == ARRAY_TYPE
7226 && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype
))) == INTEGER_CST
7227 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
7229 tree type_domain
= TYPE_DOMAIN (optype
);
7230 tree min_val
= size_zero_node
;
7231 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
7232 min_val
= TYPE_MIN_VALUE (type_domain
);
7233 if (TREE_CODE (min_val
) == INTEGER_CST
)
7234 return build4 (ARRAY_REF
, type
, op
, min_val
, NULL_TREE
, NULL_TREE
);
7236 /* *(foo *)&complexfoo => __real__ complexfoo */
7237 else if (TREE_CODE (optype
) == COMPLEX_TYPE
7238 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
7239 return fold_build1 (REALPART_EXPR
, type
, op
);
7240 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
7241 else if (TREE_CODE (optype
) == VECTOR_TYPE
7242 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
7244 tree part_width
= TYPE_SIZE (type
);
7245 tree index
= bitsize_int (0);
7246 return fold_build3 (BIT_FIELD_REF
, type
, op
, part_width
, index
);
7250 /* *(p + CST) -> ... */
7251 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
7252 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
7254 tree addr
= TREE_OPERAND (sub
, 0);
7255 tree off
= TREE_OPERAND (sub
, 1);
7259 addrtype
= TREE_TYPE (addr
);
7261 /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
7262 if (TREE_CODE (addr
) == ADDR_EXPR
7263 && TREE_CODE (TREE_TYPE (addrtype
)) == VECTOR_TYPE
7264 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (addrtype
)))
7265 && tree_fits_uhwi_p (off
))
7267 unsigned HOST_WIDE_INT offset
= tree_to_uhwi (off
);
7268 tree part_width
= TYPE_SIZE (type
);
7269 unsigned HOST_WIDE_INT part_widthi
7270 = tree_to_shwi (part_width
) / BITS_PER_UNIT
;
7271 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
7272 tree index
= bitsize_int (indexi
);
7273 if (known_lt (offset
/ part_widthi
,
7274 TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype
))))
7275 return fold_build3 (BIT_FIELD_REF
, type
, TREE_OPERAND (addr
, 0),
7279 /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
7280 if (TREE_CODE (addr
) == ADDR_EXPR
7281 && TREE_CODE (TREE_TYPE (addrtype
)) == COMPLEX_TYPE
7282 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (addrtype
))))
7284 tree size
= TYPE_SIZE_UNIT (type
);
7285 if (tree_int_cst_equal (size
, off
))
7286 return fold_build1 (IMAGPART_EXPR
, type
, TREE_OPERAND (addr
, 0));
7289 /* *(p + CST) -> MEM_REF <p, CST>. */
7290 if (TREE_CODE (addr
) != ADDR_EXPR
7291 || DECL_P (TREE_OPERAND (addr
, 0)))
7292 return fold_build2 (MEM_REF
, type
,
7294 wide_int_to_tree (ptype
, wi::to_wide (off
)));
7297 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
7298 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
7299 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype
)))) == INTEGER_CST
7300 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (subtype
))))
7303 tree min_val
= size_zero_node
;
7305 sub
= gimple_fold_indirect_ref (sub
);
7307 sub
= build1 (INDIRECT_REF
, TREE_TYPE (subtype
), osub
);
7308 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
7309 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
7310 min_val
= TYPE_MIN_VALUE (type_domain
);
7311 if (TREE_CODE (min_val
) == INTEGER_CST
)
7312 return build4 (ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
, NULL_TREE
);
7318 /* Return true if CODE is an operation that when operating on signed
7319 integer types involves undefined behavior on overflow and the
7320 operation can be expressed with unsigned arithmetic. */
7323 arith_code_with_undefined_signed_overflow (tree_code code
)
7332 case POINTER_PLUS_EXPR
:
7339 /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic
7340 operation that can be transformed to unsigned arithmetic by converting
7341 its operand, carrying out the operation in the corresponding unsigned
7342 type and converting the result back to the original type.
7344 Returns a sequence of statements that replace STMT and also contain
7345 a modified form of STMT itself. */
7348 rewrite_to_defined_overflow (gimple
*stmt
)
7350 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7352 fprintf (dump_file
, "rewriting stmt with undefined signed "
7354 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
7357 tree lhs
= gimple_assign_lhs (stmt
);
7358 tree type
= unsigned_type_for (TREE_TYPE (lhs
));
7359 gimple_seq stmts
= NULL
;
7360 if (gimple_assign_rhs_code (stmt
) == ABS_EXPR
)
7361 gimple_assign_set_rhs_code (stmt
, ABSU_EXPR
);
7363 for (unsigned i
= 1; i
< gimple_num_ops (stmt
); ++i
)
7365 tree op
= gimple_op (stmt
, i
);
7366 op
= gimple_convert (&stmts
, type
, op
);
7367 gimple_set_op (stmt
, i
, op
);
7369 gimple_assign_set_lhs (stmt
, make_ssa_name (type
, stmt
));
7370 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
7371 gimple_assign_set_rhs_code (stmt
, PLUS_EXPR
);
7372 gimple_seq_add_stmt (&stmts
, stmt
);
7373 gimple
*cvt
= gimple_build_assign (lhs
, NOP_EXPR
, gimple_assign_lhs (stmt
));
7374 gimple_seq_add_stmt (&stmts
, cvt
);
7380 /* The valueization hook we use for the gimple_build API simplification.
7381 This makes us match fold_buildN behavior by only combining with
7382 statements in the sequence(s) we are currently building. */
7385 gimple_build_valueize (tree op
)
7387 if (gimple_bb (SSA_NAME_DEF_STMT (op
)) == NULL
)
7392 /* Build the expression CODE OP0 of type TYPE with location LOC,
7393 simplifying it first if possible. Returns the built
7394 expression value and appends statements possibly defining it
7398 gimple_build (gimple_seq
*seq
, location_t loc
,
7399 enum tree_code code
, tree type
, tree op0
)
7401 tree res
= gimple_simplify (code
, type
, op0
, seq
, gimple_build_valueize
);
7404 res
= create_tmp_reg_or_ssa_name (type
);
7406 if (code
== REALPART_EXPR
7407 || code
== IMAGPART_EXPR
7408 || code
== VIEW_CONVERT_EXPR
)
7409 stmt
= gimple_build_assign (res
, code
, build1 (code
, type
, op0
));
7411 stmt
= gimple_build_assign (res
, code
, op0
);
7412 gimple_set_location (stmt
, loc
);
7413 gimple_seq_add_stmt_without_update (seq
, stmt
);
7418 /* Build the expression OP0 CODE OP1 of type TYPE with location LOC,
7419 simplifying it first if possible. Returns the built
7420 expression value and appends statements possibly defining it
7424 gimple_build (gimple_seq
*seq
, location_t loc
,
7425 enum tree_code code
, tree type
, tree op0
, tree op1
)
7427 tree res
= gimple_simplify (code
, type
, op0
, op1
, seq
, gimple_build_valueize
);
7430 res
= create_tmp_reg_or_ssa_name (type
);
7431 gimple
*stmt
= gimple_build_assign (res
, code
, op0
, op1
);
7432 gimple_set_location (stmt
, loc
);
7433 gimple_seq_add_stmt_without_update (seq
, stmt
);
7438 /* Build the expression (CODE OP0 OP1 OP2) of type TYPE with location LOC,
7439 simplifying it first if possible. Returns the built
7440 expression value and appends statements possibly defining it
7444 gimple_build (gimple_seq
*seq
, location_t loc
,
7445 enum tree_code code
, tree type
, tree op0
, tree op1
, tree op2
)
7447 tree res
= gimple_simplify (code
, type
, op0
, op1
, op2
,
7448 seq
, gimple_build_valueize
);
7451 res
= create_tmp_reg_or_ssa_name (type
);
7453 if (code
== BIT_FIELD_REF
)
7454 stmt
= gimple_build_assign (res
, code
,
7455 build3 (code
, type
, op0
, op1
, op2
));
7457 stmt
= gimple_build_assign (res
, code
, op0
, op1
, op2
);
7458 gimple_set_location (stmt
, loc
);
7459 gimple_seq_add_stmt_without_update (seq
, stmt
);
7464 /* Build the call FN (ARG0) with a result of type TYPE
7465 (or no result if TYPE is void) with location LOC,
7466 simplifying it first if possible. Returns the built
7467 expression value (or NULL_TREE if TYPE is void) and appends
7468 statements possibly defining it to SEQ. */
7471 gimple_build (gimple_seq
*seq
, location_t loc
, combined_fn fn
,
7472 tree type
, tree arg0
)
7474 tree res
= gimple_simplify (fn
, type
, arg0
, seq
, gimple_build_valueize
);
7478 if (internal_fn_p (fn
))
7479 stmt
= gimple_build_call_internal (as_internal_fn (fn
), 1, arg0
);
7482 tree decl
= builtin_decl_implicit (as_builtin_fn (fn
));
7483 stmt
= gimple_build_call (decl
, 1, arg0
);
7485 if (!VOID_TYPE_P (type
))
7487 res
= create_tmp_reg_or_ssa_name (type
);
7488 gimple_call_set_lhs (stmt
, res
);
7490 gimple_set_location (stmt
, loc
);
7491 gimple_seq_add_stmt_without_update (seq
, stmt
);
7496 /* Build the call FN (ARG0, ARG1) with a result of type TYPE
7497 (or no result if TYPE is void) with location LOC,
7498 simplifying it first if possible. Returns the built
7499 expression value (or NULL_TREE if TYPE is void) and appends
7500 statements possibly defining it to SEQ. */
7503 gimple_build (gimple_seq
*seq
, location_t loc
, combined_fn fn
,
7504 tree type
, tree arg0
, tree arg1
)
7506 tree res
= gimple_simplify (fn
, type
, arg0
, arg1
, seq
, gimple_build_valueize
);
7510 if (internal_fn_p (fn
))
7511 stmt
= gimple_build_call_internal (as_internal_fn (fn
), 2, arg0
, arg1
);
7514 tree decl
= builtin_decl_implicit (as_builtin_fn (fn
));
7515 stmt
= gimple_build_call (decl
, 2, arg0
, arg1
);
7517 if (!VOID_TYPE_P (type
))
7519 res
= create_tmp_reg_or_ssa_name (type
);
7520 gimple_call_set_lhs (stmt
, res
);
7522 gimple_set_location (stmt
, loc
);
7523 gimple_seq_add_stmt_without_update (seq
, stmt
);
7528 /* Build the call FN (ARG0, ARG1, ARG2) with a result of type TYPE
7529 (or no result if TYPE is void) with location LOC,
7530 simplifying it first if possible. Returns the built
7531 expression value (or NULL_TREE if TYPE is void) and appends
7532 statements possibly defining it to SEQ. */
7535 gimple_build (gimple_seq
*seq
, location_t loc
, combined_fn fn
,
7536 tree type
, tree arg0
, tree arg1
, tree arg2
)
7538 tree res
= gimple_simplify (fn
, type
, arg0
, arg1
, arg2
,
7539 seq
, gimple_build_valueize
);
7543 if (internal_fn_p (fn
))
7544 stmt
= gimple_build_call_internal (as_internal_fn (fn
),
7545 3, arg0
, arg1
, arg2
);
7548 tree decl
= builtin_decl_implicit (as_builtin_fn (fn
));
7549 stmt
= gimple_build_call (decl
, 3, arg0
, arg1
, arg2
);
7551 if (!VOID_TYPE_P (type
))
7553 res
= create_tmp_reg_or_ssa_name (type
);
7554 gimple_call_set_lhs (stmt
, res
);
7556 gimple_set_location (stmt
, loc
);
7557 gimple_seq_add_stmt_without_update (seq
, stmt
);
7562 /* Build the conversion (TYPE) OP with a result of type TYPE
7563 with location LOC if such conversion is neccesary in GIMPLE,
7564 simplifying it first.
7565 Returns the built expression value and appends
7566 statements possibly defining it to SEQ. */
7569 gimple_convert (gimple_seq
*seq
, location_t loc
, tree type
, tree op
)
7571 if (useless_type_conversion_p (type
, TREE_TYPE (op
)))
7573 return gimple_build (seq
, loc
, NOP_EXPR
, type
, op
);
7576 /* Build the conversion (ptrofftype) OP with a result of a type
7577 compatible with ptrofftype with location LOC if such conversion
7578 is neccesary in GIMPLE, simplifying it first.
7579 Returns the built expression value and appends
7580 statements possibly defining it to SEQ. */
7583 gimple_convert_to_ptrofftype (gimple_seq
*seq
, location_t loc
, tree op
)
7585 if (ptrofftype_p (TREE_TYPE (op
)))
7587 return gimple_convert (seq
, loc
, sizetype
, op
);
7590 /* Build a vector of type TYPE in which each element has the value OP.
7591 Return a gimple value for the result, appending any new statements
7595 gimple_build_vector_from_val (gimple_seq
*seq
, location_t loc
, tree type
,
7598 if (!TYPE_VECTOR_SUBPARTS (type
).is_constant ()
7599 && !CONSTANT_CLASS_P (op
))
7600 return gimple_build (seq
, loc
, VEC_DUPLICATE_EXPR
, type
, op
);
7602 tree res
, vec
= build_vector_from_val (type
, op
);
7603 if (is_gimple_val (vec
))
7605 if (gimple_in_ssa_p (cfun
))
7606 res
= make_ssa_name (type
);
7608 res
= create_tmp_reg (type
);
7609 gimple
*stmt
= gimple_build_assign (res
, vec
);
7610 gimple_set_location (stmt
, loc
);
7611 gimple_seq_add_stmt_without_update (seq
, stmt
);
7615 /* Build a vector from BUILDER, handling the case in which some elements
7616 are non-constant. Return a gimple value for the result, appending any
7617 new instructions to SEQ.
7619 BUILDER must not have a stepped encoding on entry. This is because
7620 the function is not geared up to handle the arithmetic that would
7621 be needed in the variable case, and any code building a vector that
7622 is known to be constant should use BUILDER->build () directly. */
7625 gimple_build_vector (gimple_seq
*seq
, location_t loc
,
7626 tree_vector_builder
*builder
)
7628 gcc_assert (builder
->nelts_per_pattern () <= 2);
7629 unsigned int encoded_nelts
= builder
->encoded_nelts ();
7630 for (unsigned int i
= 0; i
< encoded_nelts
; ++i
)
7631 if (!TREE_CONSTANT ((*builder
)[i
]))
7633 tree type
= builder
->type ();
7634 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
).to_constant ();
7635 vec
<constructor_elt
, va_gc
> *v
;
7636 vec_alloc (v
, nelts
);
7637 for (i
= 0; i
< nelts
; ++i
)
7638 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, builder
->elt (i
));
7641 if (gimple_in_ssa_p (cfun
))
7642 res
= make_ssa_name (type
);
7644 res
= create_tmp_reg (type
);
7645 gimple
*stmt
= gimple_build_assign (res
, build_constructor (type
, v
));
7646 gimple_set_location (stmt
, loc
);
7647 gimple_seq_add_stmt_without_update (seq
, stmt
);
7650 return builder
->build ();
7653 /* Return true if the result of assignment STMT is known to be non-negative.
7654 If the return value is based on the assumption that signed overflow is
7655 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7656 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7659 gimple_assign_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
7662 enum tree_code code
= gimple_assign_rhs_code (stmt
);
7663 switch (get_gimple_rhs_class (code
))
7665 case GIMPLE_UNARY_RHS
:
7666 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
7667 gimple_expr_type (stmt
),
7668 gimple_assign_rhs1 (stmt
),
7669 strict_overflow_p
, depth
);
7670 case GIMPLE_BINARY_RHS
:
7671 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
7672 gimple_expr_type (stmt
),
7673 gimple_assign_rhs1 (stmt
),
7674 gimple_assign_rhs2 (stmt
),
7675 strict_overflow_p
, depth
);
7676 case GIMPLE_TERNARY_RHS
:
7678 case GIMPLE_SINGLE_RHS
:
7679 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
7680 strict_overflow_p
, depth
);
7681 case GIMPLE_INVALID_RHS
:
7687 /* Return true if return value of call STMT is known to be non-negative.
7688 If the return value is based on the assumption that signed overflow is
7689 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7690 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7693 gimple_call_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
7696 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
7697 gimple_call_arg (stmt
, 0) : NULL_TREE
;
7698 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
7699 gimple_call_arg (stmt
, 1) : NULL_TREE
;
7701 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
7702 gimple_call_combined_fn (stmt
),
7705 strict_overflow_p
, depth
);
7708 /* Return true if return value of call STMT is known to be non-negative.
7709 If the return value is based on the assumption that signed overflow is
7710 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7711 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7714 gimple_phi_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
7717 for (unsigned i
= 0; i
< gimple_phi_num_args (stmt
); ++i
)
7719 tree arg
= gimple_phi_arg_def (stmt
, i
);
7720 if (!tree_single_nonnegative_warnv_p (arg
, strict_overflow_p
, depth
+ 1))
7726 /* Return true if STMT is known to compute a non-negative value.
7727 If the return value is based on the assumption that signed overflow is
7728 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7729 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7732 gimple_stmt_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
7735 switch (gimple_code (stmt
))
7738 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
,
7741 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
,
7744 return gimple_phi_nonnegative_warnv_p (stmt
, strict_overflow_p
,
7751 /* Return true if the floating-point value computed by assignment STMT
7752 is known to have an integer value. We also allow +Inf, -Inf and NaN
7753 to be considered integer values. Return false for signaling NaN.
7755 DEPTH is the current nesting depth of the query. */
7758 gimple_assign_integer_valued_real_p (gimple
*stmt
, int depth
)
7760 enum tree_code code
= gimple_assign_rhs_code (stmt
);
7761 switch (get_gimple_rhs_class (code
))
7763 case GIMPLE_UNARY_RHS
:
7764 return integer_valued_real_unary_p (gimple_assign_rhs_code (stmt
),
7765 gimple_assign_rhs1 (stmt
), depth
);
7766 case GIMPLE_BINARY_RHS
:
7767 return integer_valued_real_binary_p (gimple_assign_rhs_code (stmt
),
7768 gimple_assign_rhs1 (stmt
),
7769 gimple_assign_rhs2 (stmt
), depth
);
7770 case GIMPLE_TERNARY_RHS
:
7772 case GIMPLE_SINGLE_RHS
:
7773 return integer_valued_real_single_p (gimple_assign_rhs1 (stmt
), depth
);
7774 case GIMPLE_INVALID_RHS
:
7780 /* Return true if the floating-point value computed by call STMT is known
7781 to have an integer value. We also allow +Inf, -Inf and NaN to be
7782 considered integer values. Return false for signaling NaN.
7784 DEPTH is the current nesting depth of the query. */
7787 gimple_call_integer_valued_real_p (gimple
*stmt
, int depth
)
7789 tree arg0
= (gimple_call_num_args (stmt
) > 0
7790 ? gimple_call_arg (stmt
, 0)
7792 tree arg1
= (gimple_call_num_args (stmt
) > 1
7793 ? gimple_call_arg (stmt
, 1)
7795 return integer_valued_real_call_p (gimple_call_combined_fn (stmt
),
7799 /* Return true if the floating-point result of phi STMT is known to have
7800 an integer value. We also allow +Inf, -Inf and NaN to be considered
7801 integer values. Return false for signaling NaN.
7803 DEPTH is the current nesting depth of the query. */
7806 gimple_phi_integer_valued_real_p (gimple
*stmt
, int depth
)
7808 for (unsigned i
= 0; i
< gimple_phi_num_args (stmt
); ++i
)
7810 tree arg
= gimple_phi_arg_def (stmt
, i
);
7811 if (!integer_valued_real_single_p (arg
, depth
+ 1))
7817 /* Return true if the floating-point value computed by STMT is known
7818 to have an integer value. We also allow +Inf, -Inf and NaN to be
7819 considered integer values. Return false for signaling NaN.
7821 DEPTH is the current nesting depth of the query. */
7824 gimple_stmt_integer_valued_real_p (gimple
*stmt
, int depth
)
7826 switch (gimple_code (stmt
))
7829 return gimple_assign_integer_valued_real_p (stmt
, depth
);
7831 return gimple_call_integer_valued_real_p (stmt
, depth
);
7833 return gimple_phi_integer_valued_real_p (stmt
, depth
);