1 /* Statement simplification on GIMPLE.
2 Copyright (C) 2010-2018 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 /* Return true when DECL can be referenced from current unit.
70 FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
71 We can get declarations that are not possible to reference for various
74 1) When analyzing C++ virtual tables.
75 C++ virtual tables do have known constructors even
76 when they are keyed to other compilation unit.
77 Those tables can contain pointers to methods and vars
78 in other units. Those methods have both STATIC and EXTERNAL
80 2) In WHOPR mode devirtualization might lead to reference
81 to method that was partitioned elsehwere.
82 In this case we have static VAR_DECL or FUNCTION_DECL
83 that has no corresponding callgraph/varpool node
85 3) COMDAT functions referred by external vtables that
86 we devirtualize only during final compilation stage.
87 At this time we already decided that we will not output
88 the function body and thus we can't reference the symbol
92 can_refer_decl_in_current_unit_p (tree decl
, tree from_decl
)
95 struct cgraph_node
*node
;
98 if (DECL_ABSTRACT_P (decl
))
101 /* We are concerned only about static/external vars and functions. */
102 if ((!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
103 || !VAR_OR_FUNCTION_DECL_P (decl
))
106 /* Static objects can be referred only if they was not optimized out yet. */
107 if (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
109 /* Before we start optimizing unreachable code we can be sure all
110 static objects are defined. */
111 if (symtab
->function_flags_ready
)
113 snode
= symtab_node::get (decl
);
114 if (!snode
|| !snode
->definition
)
116 node
= dyn_cast
<cgraph_node
*> (snode
);
117 return !node
|| !node
->global
.inlined_to
;
120 /* We will later output the initializer, so we can refer to it.
121 So we are concerned only when DECL comes from initializer of
122 external var or var that has been optimized out. */
124 || !VAR_P (from_decl
)
125 || (!DECL_EXTERNAL (from_decl
)
126 && (vnode
= varpool_node::get (from_decl
)) != NULL
127 && vnode
->definition
)
129 && (vnode
= varpool_node::get (from_decl
)) != NULL
130 && vnode
->in_other_partition
))
132 /* We are folding reference from external vtable. The vtable may reffer
133 to a symbol keyed to other compilation unit. The other compilation
134 unit may be in separate DSO and the symbol may be hidden. */
135 if (DECL_VISIBILITY_SPECIFIED (decl
)
136 && DECL_EXTERNAL (decl
)
137 && DECL_VISIBILITY (decl
) != VISIBILITY_DEFAULT
138 && (!(snode
= symtab_node::get (decl
)) || !snode
->in_other_partition
))
140 /* When function is public, we always can introduce new reference.
141 Exception are the COMDAT functions where introducing a direct
142 reference imply need to include function body in the curren tunit. */
143 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
))
145 /* We have COMDAT. We are going to check if we still have definition
146 or if the definition is going to be output in other partition.
147 Bypass this when gimplifying; all needed functions will be produced.
149 As observed in PR20991 for already optimized out comdat virtual functions
150 it may be tempting to not necessarily give up because the copy will be
151 output elsewhere when corresponding vtable is output.
152 This is however not possible - ABI specify that COMDATs are output in
153 units where they are used and when the other unit was compiled with LTO
154 it is possible that vtable was kept public while the function itself
156 if (!symtab
->function_flags_ready
)
159 snode
= symtab_node::get (decl
);
161 || ((!snode
->definition
|| DECL_EXTERNAL (decl
))
162 && (!snode
->in_other_partition
163 || (!snode
->forced_by_abi
&& !snode
->force_output
))))
165 node
= dyn_cast
<cgraph_node
*> (snode
);
166 return !node
|| !node
->global
.inlined_to
;
169 /* Create a temporary for TYPE for a statement STMT. If the current function
170 is in SSA form, a SSA name is created. Otherwise a temporary register
174 create_tmp_reg_or_ssa_name (tree type
, gimple
*stmt
)
176 if (gimple_in_ssa_p (cfun
))
177 return make_ssa_name (type
, stmt
);
179 return create_tmp_reg (type
);
182 /* CVAL is value taken from DECL_INITIAL of variable. Try to transform it into
183 acceptable form for is_gimple_min_invariant.
184 FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL. */
187 canonicalize_constructor_val (tree cval
, tree from_decl
)
189 tree orig_cval
= cval
;
191 if (TREE_CODE (cval
) == POINTER_PLUS_EXPR
192 && TREE_CODE (TREE_OPERAND (cval
, 1)) == INTEGER_CST
)
194 tree ptr
= TREE_OPERAND (cval
, 0);
195 if (is_gimple_min_invariant (ptr
))
196 cval
= build1_loc (EXPR_LOCATION (cval
),
197 ADDR_EXPR
, TREE_TYPE (ptr
),
198 fold_build2 (MEM_REF
, TREE_TYPE (TREE_TYPE (ptr
)),
200 fold_convert (ptr_type_node
,
201 TREE_OPERAND (cval
, 1))));
203 if (TREE_CODE (cval
) == ADDR_EXPR
)
205 tree base
= NULL_TREE
;
206 if (TREE_CODE (TREE_OPERAND (cval
, 0)) == COMPOUND_LITERAL_EXPR
)
208 base
= COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval
, 0));
210 TREE_OPERAND (cval
, 0) = base
;
213 base
= get_base_address (TREE_OPERAND (cval
, 0));
217 if (VAR_OR_FUNCTION_DECL_P (base
)
218 && !can_refer_decl_in_current_unit_p (base
, from_decl
))
220 if (TREE_TYPE (base
) == error_mark_node
)
223 TREE_ADDRESSABLE (base
) = 1;
224 else if (TREE_CODE (base
) == FUNCTION_DECL
)
226 /* Make sure we create a cgraph node for functions we'll reference.
227 They can be non-existent if the reference comes from an entry
228 of an external vtable for example. */
229 cgraph_node::get_create (base
);
231 /* Fixup types in global initializers. */
232 if (TREE_TYPE (TREE_TYPE (cval
)) != TREE_TYPE (TREE_OPERAND (cval
, 0)))
233 cval
= build_fold_addr_expr (TREE_OPERAND (cval
, 0));
235 if (!useless_type_conversion_p (TREE_TYPE (orig_cval
), TREE_TYPE (cval
)))
236 cval
= fold_convert (TREE_TYPE (orig_cval
), cval
);
239 if (TREE_OVERFLOW_P (cval
))
240 return drop_tree_overflow (cval
);
244 /* If SYM is a constant variable with known value, return the value.
245 NULL_TREE is returned otherwise. */
248 get_symbol_constant_value (tree sym
)
250 tree val
= ctor_for_folding (sym
);
251 if (val
!= error_mark_node
)
255 val
= canonicalize_constructor_val (unshare_expr (val
), sym
);
256 if (val
&& is_gimple_min_invariant (val
))
261 /* Variables declared 'const' without an initializer
262 have zero as the initializer if they may not be
263 overridden at link or run time. */
265 && is_gimple_reg_type (TREE_TYPE (sym
)))
266 return build_zero_cst (TREE_TYPE (sym
));
274 /* Subroutine of fold_stmt. We perform several simplifications of the
275 memory reference tree EXPR and make sure to re-gimplify them properly
276 after propagation of constant addresses. IS_LHS is true if the
277 reference is supposed to be an lvalue. */
280 maybe_fold_reference (tree expr
, bool is_lhs
)
284 if ((TREE_CODE (expr
) == VIEW_CONVERT_EXPR
285 || TREE_CODE (expr
) == REALPART_EXPR
286 || TREE_CODE (expr
) == IMAGPART_EXPR
)
287 && CONSTANT_CLASS_P (TREE_OPERAND (expr
, 0)))
288 return fold_unary_loc (EXPR_LOCATION (expr
),
291 TREE_OPERAND (expr
, 0));
292 else if (TREE_CODE (expr
) == BIT_FIELD_REF
293 && CONSTANT_CLASS_P (TREE_OPERAND (expr
, 0)))
294 return fold_ternary_loc (EXPR_LOCATION (expr
),
297 TREE_OPERAND (expr
, 0),
298 TREE_OPERAND (expr
, 1),
299 TREE_OPERAND (expr
, 2));
302 && (result
= fold_const_aggregate_ref (expr
))
303 && is_gimple_min_invariant (result
))
310 /* Attempt to fold an assignment statement pointed-to by SI. Returns a
311 replacement rhs for the statement or NULL_TREE if no simplification
312 could be made. It is assumed that the operands have been previously
316 fold_gimple_assign (gimple_stmt_iterator
*si
)
318 gimple
*stmt
= gsi_stmt (*si
);
319 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
320 location_t loc
= gimple_location (stmt
);
322 tree result
= NULL_TREE
;
324 switch (get_gimple_rhs_class (subcode
))
326 case GIMPLE_SINGLE_RHS
:
328 tree rhs
= gimple_assign_rhs1 (stmt
);
330 if (TREE_CLOBBER_P (rhs
))
333 if (REFERENCE_CLASS_P (rhs
))
334 return maybe_fold_reference (rhs
, false);
336 else if (TREE_CODE (rhs
) == OBJ_TYPE_REF
)
338 tree val
= OBJ_TYPE_REF_EXPR (rhs
);
339 if (is_gimple_min_invariant (val
))
341 else if (flag_devirtualize
&& virtual_method_call_p (rhs
))
344 vec
<cgraph_node
*>targets
345 = possible_polymorphic_call_targets (rhs
, stmt
, &final
);
346 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
348 if (dump_enabled_p ())
350 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, stmt
,
351 "resolving virtual function address "
352 "reference to function %s\n",
353 targets
.length () == 1
354 ? targets
[0]->name ()
357 if (targets
.length () == 1)
359 val
= fold_convert (TREE_TYPE (val
),
360 build_fold_addr_expr_loc
361 (loc
, targets
[0]->decl
));
362 STRIP_USELESS_TYPE_CONVERSION (val
);
365 /* We can not use __builtin_unreachable here because it
366 can not have address taken. */
367 val
= build_int_cst (TREE_TYPE (val
), 0);
373 else if (TREE_CODE (rhs
) == ADDR_EXPR
)
375 tree ref
= TREE_OPERAND (rhs
, 0);
376 tree tem
= maybe_fold_reference (ref
, true);
378 && TREE_CODE (tem
) == MEM_REF
379 && integer_zerop (TREE_OPERAND (tem
, 1)))
380 result
= fold_convert (TREE_TYPE (rhs
), TREE_OPERAND (tem
, 0));
382 result
= fold_convert (TREE_TYPE (rhs
),
383 build_fold_addr_expr_loc (loc
, tem
));
384 else if (TREE_CODE (ref
) == MEM_REF
385 && integer_zerop (TREE_OPERAND (ref
, 1)))
386 result
= fold_convert (TREE_TYPE (rhs
), TREE_OPERAND (ref
, 0));
390 /* Strip away useless type conversions. Both the
391 NON_LVALUE_EXPR that may have been added by fold, and
392 "useless" type conversions that might now be apparent
393 due to propagation. */
394 STRIP_USELESS_TYPE_CONVERSION (result
);
396 if (result
!= rhs
&& valid_gimple_rhs_p (result
))
401 else if (TREE_CODE (rhs
) == CONSTRUCTOR
402 && TREE_CODE (TREE_TYPE (rhs
)) == VECTOR_TYPE
)
404 /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */
408 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs
), i
, val
)
409 if (! CONSTANT_CLASS_P (val
))
412 return build_vector_from_ctor (TREE_TYPE (rhs
),
413 CONSTRUCTOR_ELTS (rhs
));
416 else if (DECL_P (rhs
))
417 return get_symbol_constant_value (rhs
);
421 case GIMPLE_UNARY_RHS
:
424 case GIMPLE_BINARY_RHS
:
427 case GIMPLE_TERNARY_RHS
:
428 result
= fold_ternary_loc (loc
, subcode
,
429 TREE_TYPE (gimple_assign_lhs (stmt
)),
430 gimple_assign_rhs1 (stmt
),
431 gimple_assign_rhs2 (stmt
),
432 gimple_assign_rhs3 (stmt
));
436 STRIP_USELESS_TYPE_CONVERSION (result
);
437 if (valid_gimple_rhs_p (result
))
442 case GIMPLE_INVALID_RHS
:
450 /* Replace a statement at *SI_P with a sequence of statements in STMTS,
451 adjusting the replacement stmts location and virtual operands.
452 If the statement has a lhs the last stmt in the sequence is expected
453 to assign to that lhs. */
456 gsi_replace_with_seq_vops (gimple_stmt_iterator
*si_p
, gimple_seq stmts
)
458 gimple
*stmt
= gsi_stmt (*si_p
);
460 if (gimple_has_location (stmt
))
461 annotate_all_with_location (stmts
, gimple_location (stmt
));
463 /* First iterate over the replacement statements backward, assigning
464 virtual operands to their defining statements. */
465 gimple
*laststore
= NULL
;
466 for (gimple_stmt_iterator i
= gsi_last (stmts
);
467 !gsi_end_p (i
); gsi_prev (&i
))
469 gimple
*new_stmt
= gsi_stmt (i
);
470 if ((gimple_assign_single_p (new_stmt
)
471 && !is_gimple_reg (gimple_assign_lhs (new_stmt
)))
472 || (is_gimple_call (new_stmt
)
473 && (gimple_call_flags (new_stmt
)
474 & (ECF_NOVOPS
| ECF_PURE
| ECF_CONST
| ECF_NORETURN
)) == 0))
478 vdef
= gimple_vdef (stmt
);
480 vdef
= make_ssa_name (gimple_vop (cfun
), new_stmt
);
481 gimple_set_vdef (new_stmt
, vdef
);
482 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
483 SSA_NAME_DEF_STMT (vdef
) = new_stmt
;
484 laststore
= new_stmt
;
488 /* Second iterate over the statements forward, assigning virtual
489 operands to their uses. */
490 tree reaching_vuse
= gimple_vuse (stmt
);
491 for (gimple_stmt_iterator i
= gsi_start (stmts
);
492 !gsi_end_p (i
); gsi_next (&i
))
494 gimple
*new_stmt
= gsi_stmt (i
);
495 /* If the new statement possibly has a VUSE, update it with exact SSA
496 name we know will reach this one. */
497 if (gimple_has_mem_ops (new_stmt
))
498 gimple_set_vuse (new_stmt
, reaching_vuse
);
499 gimple_set_modified (new_stmt
, true);
500 if (gimple_vdef (new_stmt
))
501 reaching_vuse
= gimple_vdef (new_stmt
);
504 /* If the new sequence does not do a store release the virtual
505 definition of the original statement. */
507 && reaching_vuse
== gimple_vuse (stmt
))
509 tree vdef
= gimple_vdef (stmt
);
511 && TREE_CODE (vdef
) == SSA_NAME
)
513 unlink_stmt_vdef (stmt
);
514 release_ssa_name (vdef
);
518 /* Finally replace the original statement with the sequence. */
519 gsi_replace_with_seq (si_p
, stmts
, false);
522 /* Convert EXPR into a GIMPLE value suitable for substitution on the
523 RHS of an assignment. Insert the necessary statements before
524 iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL
525 is replaced. If the call is expected to produces a result, then it
526 is replaced by an assignment of the new RHS to the result variable.
527 If the result is to be ignored, then the call is replaced by a
528 GIMPLE_NOP. A proper VDEF chain is retained by making the first
529 VUSE and the last VDEF of the whole sequence be the same as the replaced
530 statement and using new SSA names for stores in between. */
533 gimplify_and_update_call_from_tree (gimple_stmt_iterator
*si_p
, tree expr
)
536 gimple
*stmt
, *new_stmt
;
537 gimple_stmt_iterator i
;
538 gimple_seq stmts
= NULL
;
540 stmt
= gsi_stmt (*si_p
);
542 gcc_assert (is_gimple_call (stmt
));
544 push_gimplify_context (gimple_in_ssa_p (cfun
));
546 lhs
= gimple_call_lhs (stmt
);
547 if (lhs
== NULL_TREE
)
549 gimplify_and_add (expr
, &stmts
);
550 /* We can end up with folding a memcpy of an empty class assignment
551 which gets optimized away by C++ gimplification. */
552 if (gimple_seq_empty_p (stmts
))
554 pop_gimplify_context (NULL
);
555 if (gimple_in_ssa_p (cfun
))
557 unlink_stmt_vdef (stmt
);
560 gsi_replace (si_p
, gimple_build_nop (), false);
566 tree tmp
= force_gimple_operand (expr
, &stmts
, false, NULL_TREE
);
567 new_stmt
= gimple_build_assign (lhs
, tmp
);
568 i
= gsi_last (stmts
);
569 gsi_insert_after_without_update (&i
, new_stmt
,
570 GSI_CONTINUE_LINKING
);
573 pop_gimplify_context (NULL
);
575 gsi_replace_with_seq_vops (si_p
, stmts
);
579 /* Replace the call at *GSI with the gimple value VAL. */
582 replace_call_with_value (gimple_stmt_iterator
*gsi
, tree val
)
584 gimple
*stmt
= gsi_stmt (*gsi
);
585 tree lhs
= gimple_call_lhs (stmt
);
589 if (!useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (val
)))
590 val
= fold_convert (TREE_TYPE (lhs
), val
);
591 repl
= gimple_build_assign (lhs
, val
);
594 repl
= gimple_build_nop ();
595 tree vdef
= gimple_vdef (stmt
);
596 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
598 unlink_stmt_vdef (stmt
);
599 release_ssa_name (vdef
);
601 gsi_replace (gsi
, repl
, false);
604 /* Replace the call at *GSI with the new call REPL and fold that
608 replace_call_with_call_and_fold (gimple_stmt_iterator
*gsi
, gimple
*repl
)
610 gimple
*stmt
= gsi_stmt (*gsi
);
611 gimple_call_set_lhs (repl
, gimple_call_lhs (stmt
));
612 gimple_set_location (repl
, gimple_location (stmt
));
613 if (gimple_vdef (stmt
)
614 && TREE_CODE (gimple_vdef (stmt
)) == SSA_NAME
)
616 gimple_set_vdef (repl
, gimple_vdef (stmt
));
617 SSA_NAME_DEF_STMT (gimple_vdef (repl
)) = repl
;
619 if (gimple_vuse (stmt
))
620 gimple_set_vuse (repl
, gimple_vuse (stmt
));
621 gsi_replace (gsi
, repl
, false);
625 /* Return true if VAR is a VAR_DECL or a component thereof. */
628 var_decl_component_p (tree var
)
631 while (handled_component_p (inner
))
632 inner
= TREE_OPERAND (inner
, 0);
633 return (DECL_P (inner
)
634 || (TREE_CODE (inner
) == MEM_REF
635 && TREE_CODE (TREE_OPERAND (inner
, 0)) == ADDR_EXPR
));
638 /* If the SIZE argument representing the size of an object is in a range
639 of values of which exactly one is valid (and that is zero), return
640 true, otherwise false. */
643 size_must_be_zero_p (tree size
)
645 if (integer_zerop (size
))
648 if (TREE_CODE (size
) != SSA_NAME
|| !INTEGRAL_TYPE_P (TREE_TYPE (size
)))
652 enum value_range_type rtype
= get_range_info (size
, &min
, &max
);
653 if (rtype
!= VR_ANTI_RANGE
)
656 tree type
= TREE_TYPE (size
);
657 int prec
= TYPE_PRECISION (type
);
659 wide_int wone
= wi::one (prec
);
661 /* Compute the value of SSIZE_MAX, the largest positive value that
662 can be stored in ssize_t, the signed counterpart of size_t. */
663 wide_int ssize_max
= wi::lshift (wi::one (prec
), prec
- 1) - 1;
665 return wi::eq_p (min
, wone
) && wi::geu_p (max
, ssize_max
);
668 /* Fold function call to builtin mem{{,p}cpy,move}. Try to detect and
669 diagnose (otherwise undefined) overlapping copies without preventing
670 folding. When folded, GCC guarantees that overlapping memcpy has
671 the same semantics as memmove. Call to the library memcpy need not
672 provide the same guarantee. Return false if no simplification can
676 gimple_fold_builtin_memory_op (gimple_stmt_iterator
*gsi
,
677 tree dest
, tree src
, int endp
)
679 gimple
*stmt
= gsi_stmt (*gsi
);
680 tree lhs
= gimple_call_lhs (stmt
);
681 tree len
= gimple_call_arg (stmt
, 2);
682 tree destvar
, srcvar
;
683 location_t loc
= gimple_location (stmt
);
685 bool nowarn
= gimple_no_warning_p (stmt
);
687 /* If the LEN parameter is a constant zero or in range where
688 the only valid value is zero, return DEST. */
689 if (size_must_be_zero_p (len
))
692 if (gimple_call_lhs (stmt
))
693 repl
= gimple_build_assign (gimple_call_lhs (stmt
), dest
);
695 repl
= gimple_build_nop ();
696 tree vdef
= gimple_vdef (stmt
);
697 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
699 unlink_stmt_vdef (stmt
);
700 release_ssa_name (vdef
);
702 gsi_replace (gsi
, repl
, false);
706 /* If SRC and DEST are the same (and not volatile), return
707 DEST{,+LEN,+LEN-1}. */
708 if (operand_equal_p (src
, dest
, 0))
710 /* Avoid diagnosing exact overlap in calls to __builtin_memcpy.
711 It's safe and may even be emitted by GCC itself (see bug
713 unlink_stmt_vdef (stmt
);
714 if (gimple_vdef (stmt
) && TREE_CODE (gimple_vdef (stmt
)) == SSA_NAME
)
715 release_ssa_name (gimple_vdef (stmt
));
718 gsi_replace (gsi
, gimple_build_nop (), false);
725 tree srctype
, desttype
;
726 unsigned int src_align
, dest_align
;
729 /* Build accesses at offset zero with a ref-all character type. */
730 off0
= build_int_cst (build_pointer_type_for_mode (char_type_node
,
733 /* If we can perform the copy efficiently with first doing all loads
734 and then all stores inline it that way. Currently efficiently
735 means that we can load all the memory into a single integer
736 register which is what MOVE_MAX gives us. */
737 src_align
= get_pointer_alignment (src
);
738 dest_align
= get_pointer_alignment (dest
);
739 if (tree_fits_uhwi_p (len
)
740 && compare_tree_int (len
, MOVE_MAX
) <= 0
741 /* ??? Don't transform copies from strings with known length this
742 confuses the tree-ssa-strlen.c. This doesn't handle
743 the case in gcc.dg/strlenopt-8.c which is XFAILed for that
745 && !c_strlen (src
, 2))
747 unsigned ilen
= tree_to_uhwi (len
);
748 if (pow2p_hwi (ilen
))
750 /* Detect invalid bounds and overlapping copies and issue
751 either -Warray-bounds or -Wrestrict. */
753 && check_bounds_or_overlap (as_a
<gcall
*>(stmt
),
754 dest
, src
, len
, len
))
755 gimple_set_no_warning (stmt
, true);
757 scalar_int_mode mode
;
758 tree type
= lang_hooks
.types
.type_for_size (ilen
* 8, 1);
760 && is_a
<scalar_int_mode
> (TYPE_MODE (type
), &mode
)
761 && GET_MODE_SIZE (mode
) * BITS_PER_UNIT
== ilen
* 8
762 /* If the destination pointer is not aligned we must be able
763 to emit an unaligned store. */
764 && (dest_align
>= GET_MODE_ALIGNMENT (mode
)
765 || !targetm
.slow_unaligned_access (mode
, dest_align
)
766 || (optab_handler (movmisalign_optab
, mode
)
767 != CODE_FOR_nothing
)))
770 tree desttype
= type
;
771 if (src_align
< GET_MODE_ALIGNMENT (mode
))
772 srctype
= build_aligned_type (type
, src_align
);
773 tree srcmem
= fold_build2 (MEM_REF
, srctype
, src
, off0
);
774 tree tem
= fold_const_aggregate_ref (srcmem
);
777 else if (src_align
< GET_MODE_ALIGNMENT (mode
)
778 && targetm
.slow_unaligned_access (mode
, src_align
)
779 && (optab_handler (movmisalign_optab
, mode
)
780 == CODE_FOR_nothing
))
785 if (is_gimple_reg_type (TREE_TYPE (srcmem
)))
787 new_stmt
= gimple_build_assign (NULL_TREE
, srcmem
);
789 = create_tmp_reg_or_ssa_name (TREE_TYPE (srcmem
),
791 gimple_assign_set_lhs (new_stmt
, srcmem
);
792 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
793 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
795 if (dest_align
< GET_MODE_ALIGNMENT (mode
))
796 desttype
= build_aligned_type (type
, dest_align
);
798 = gimple_build_assign (fold_build2 (MEM_REF
, desttype
,
801 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
802 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
803 if (gimple_vdef (new_stmt
)
804 && TREE_CODE (gimple_vdef (new_stmt
)) == SSA_NAME
)
805 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt
)) = new_stmt
;
808 gsi_replace (gsi
, new_stmt
, false);
811 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
820 /* Both DEST and SRC must be pointer types.
821 ??? This is what old code did. Is the testing for pointer types
824 If either SRC is readonly or length is 1, we can use memcpy. */
825 if (!dest_align
|| !src_align
)
827 if (readonly_data_expr (src
)
828 || (tree_fits_uhwi_p (len
)
829 && (MIN (src_align
, dest_align
) / BITS_PER_UNIT
830 >= tree_to_uhwi (len
))))
832 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
835 gimple_call_set_fndecl (stmt
, fn
);
836 gimple_call_set_arg (stmt
, 0, dest
);
837 gimple_call_set_arg (stmt
, 1, src
);
842 /* If *src and *dest can't overlap, optimize into memcpy as well. */
843 if (TREE_CODE (src
) == ADDR_EXPR
844 && TREE_CODE (dest
) == ADDR_EXPR
)
846 tree src_base
, dest_base
, fn
;
847 poly_int64 src_offset
= 0, dest_offset
= 0;
850 srcvar
= TREE_OPERAND (src
, 0);
851 src_base
= get_addr_base_and_unit_offset (srcvar
, &src_offset
);
852 if (src_base
== NULL
)
854 destvar
= TREE_OPERAND (dest
, 0);
855 dest_base
= get_addr_base_and_unit_offset (destvar
,
857 if (dest_base
== NULL
)
859 if (!poly_int_tree_p (len
, &maxsize
))
861 if (SSA_VAR_P (src_base
)
862 && SSA_VAR_P (dest_base
))
864 if (operand_equal_p (src_base
, dest_base
, 0)
865 && ranges_maybe_overlap_p (src_offset
, maxsize
,
866 dest_offset
, maxsize
))
869 else if (TREE_CODE (src_base
) == MEM_REF
870 && TREE_CODE (dest_base
) == MEM_REF
)
872 if (! operand_equal_p (TREE_OPERAND (src_base
, 0),
873 TREE_OPERAND (dest_base
, 0), 0))
875 poly_offset_int full_src_offset
876 = mem_ref_offset (src_base
) + src_offset
;
877 poly_offset_int full_dest_offset
878 = mem_ref_offset (dest_base
) + dest_offset
;
879 if (ranges_maybe_overlap_p (full_src_offset
, maxsize
,
880 full_dest_offset
, maxsize
))
886 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
889 gimple_call_set_fndecl (stmt
, fn
);
890 gimple_call_set_arg (stmt
, 0, dest
);
891 gimple_call_set_arg (stmt
, 1, src
);
896 /* If the destination and source do not alias optimize into
898 if ((is_gimple_min_invariant (dest
)
899 || TREE_CODE (dest
) == SSA_NAME
)
900 && (is_gimple_min_invariant (src
)
901 || TREE_CODE (src
) == SSA_NAME
))
904 ao_ref_init_from_ptr_and_size (&destr
, dest
, len
);
905 ao_ref_init_from_ptr_and_size (&srcr
, src
, len
);
906 if (!refs_may_alias_p_1 (&destr
, &srcr
, false))
909 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
912 gimple_call_set_fndecl (stmt
, fn
);
913 gimple_call_set_arg (stmt
, 0, dest
);
914 gimple_call_set_arg (stmt
, 1, src
);
923 if (!tree_fits_shwi_p (len
))
925 if (!POINTER_TYPE_P (TREE_TYPE (src
))
926 || !POINTER_TYPE_P (TREE_TYPE (dest
)))
928 /* In the following try to find a type that is most natural to be
929 used for the memcpy source and destination and that allows
930 the most optimization when memcpy is turned into a plain assignment
931 using that type. In theory we could always use a char[len] type
932 but that only gains us that the destination and source possibly
933 no longer will have their address taken. */
934 srctype
= TREE_TYPE (TREE_TYPE (src
));
935 if (TREE_CODE (srctype
) == ARRAY_TYPE
936 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype
), len
))
937 srctype
= TREE_TYPE (srctype
);
938 desttype
= TREE_TYPE (TREE_TYPE (dest
));
939 if (TREE_CODE (desttype
) == ARRAY_TYPE
940 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype
), len
))
941 desttype
= TREE_TYPE (desttype
);
942 if (TREE_ADDRESSABLE (srctype
)
943 || TREE_ADDRESSABLE (desttype
))
946 /* Make sure we are not copying using a floating-point mode or
947 a type whose size possibly does not match its precision. */
948 if (FLOAT_MODE_P (TYPE_MODE (desttype
))
949 || TREE_CODE (desttype
) == BOOLEAN_TYPE
950 || TREE_CODE (desttype
) == ENUMERAL_TYPE
)
951 desttype
= bitwise_type_for_mode (TYPE_MODE (desttype
));
952 if (FLOAT_MODE_P (TYPE_MODE (srctype
))
953 || TREE_CODE (srctype
) == BOOLEAN_TYPE
954 || TREE_CODE (srctype
) == ENUMERAL_TYPE
)
955 srctype
= bitwise_type_for_mode (TYPE_MODE (srctype
));
963 src_align
= get_pointer_alignment (src
);
964 dest_align
= get_pointer_alignment (dest
);
965 if (dest_align
< TYPE_ALIGN (desttype
)
966 || src_align
< TYPE_ALIGN (srctype
))
970 if (TREE_CODE (dest
) == ADDR_EXPR
971 && var_decl_component_p (TREE_OPERAND (dest
, 0))
972 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype
), len
))
973 destvar
= fold_build2 (MEM_REF
, desttype
, dest
, off0
);
976 if (TREE_CODE (src
) == ADDR_EXPR
977 && var_decl_component_p (TREE_OPERAND (src
, 0))
978 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype
), len
))
981 || src_align
>= TYPE_ALIGN (desttype
))
982 srcvar
= fold_build2 (MEM_REF
, destvar
? desttype
: srctype
,
984 else if (!STRICT_ALIGNMENT
)
986 srctype
= build_aligned_type (TYPE_MAIN_VARIANT (desttype
),
988 srcvar
= fold_build2 (MEM_REF
, srctype
, src
, off0
);
992 if (srcvar
== NULL_TREE
&& destvar
== NULL_TREE
)
995 if (srcvar
== NULL_TREE
)
997 if (src_align
>= TYPE_ALIGN (desttype
))
998 srcvar
= fold_build2 (MEM_REF
, desttype
, src
, off0
);
1001 if (STRICT_ALIGNMENT
)
1003 srctype
= build_aligned_type (TYPE_MAIN_VARIANT (desttype
),
1005 srcvar
= fold_build2 (MEM_REF
, srctype
, src
, off0
);
1008 else if (destvar
== NULL_TREE
)
1010 if (dest_align
>= TYPE_ALIGN (srctype
))
1011 destvar
= fold_build2 (MEM_REF
, srctype
, dest
, off0
);
1014 if (STRICT_ALIGNMENT
)
1016 desttype
= build_aligned_type (TYPE_MAIN_VARIANT (srctype
),
1018 destvar
= fold_build2 (MEM_REF
, desttype
, dest
, off0
);
1022 /* Detect invalid bounds and overlapping copies and issue either
1023 -Warray-bounds or -Wrestrict. */
1025 check_bounds_or_overlap (as_a
<gcall
*>(stmt
), dest
, src
, len
, len
);
1028 if (is_gimple_reg_type (TREE_TYPE (srcvar
)))
1030 tree tem
= fold_const_aggregate_ref (srcvar
);
1033 if (! is_gimple_min_invariant (srcvar
))
1035 new_stmt
= gimple_build_assign (NULL_TREE
, srcvar
);
1036 srcvar
= create_tmp_reg_or_ssa_name (TREE_TYPE (srcvar
),
1038 gimple_assign_set_lhs (new_stmt
, srcvar
);
1039 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
1040 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
1042 new_stmt
= gimple_build_assign (destvar
, srcvar
);
1043 goto set_vop_and_replace
;
1046 /* We get an aggregate copy. Use an unsigned char[] type to
1047 perform the copying to preserve padding and to avoid any issues
1048 with TREE_ADDRESSABLE types or float modes behavior on copying. */
1049 desttype
= build_array_type_nelts (unsigned_char_type_node
,
1050 tree_to_uhwi (len
));
1052 if (src_align
> TYPE_ALIGN (srctype
))
1053 srctype
= build_aligned_type (srctype
, src_align
);
1054 if (dest_align
> TYPE_ALIGN (desttype
))
1055 desttype
= build_aligned_type (desttype
, dest_align
);
1057 = gimple_build_assign (fold_build2 (MEM_REF
, desttype
, dest
, off0
),
1058 fold_build2 (MEM_REF
, srctype
, src
, off0
));
1059 set_vop_and_replace
:
1060 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
1061 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
1062 if (gimple_vdef (new_stmt
)
1063 && TREE_CODE (gimple_vdef (new_stmt
)) == SSA_NAME
)
1064 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt
)) = new_stmt
;
1067 gsi_replace (gsi
, new_stmt
, false);
1070 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
1074 gimple_seq stmts
= NULL
;
1075 if (endp
== 0 || endp
== 3)
1078 len
= gimple_build (&stmts
, loc
, MINUS_EXPR
, TREE_TYPE (len
), len
,
1080 if (endp
== 2 || endp
== 1)
1082 len
= gimple_convert_to_ptrofftype (&stmts
, loc
, len
);
1083 dest
= gimple_build (&stmts
, loc
, POINTER_PLUS_EXPR
,
1084 TREE_TYPE (dest
), dest
, len
);
1087 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1088 gimple
*repl
= gimple_build_assign (lhs
, dest
);
1089 gsi_replace (gsi
, repl
, false);
1093 /* Transform a call to built-in bcmp(a, b, len) at *GSI into one
1094 to built-in memcmp (a, b, len). */
1097 gimple_fold_builtin_bcmp (gimple_stmt_iterator
*gsi
)
1099 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCMP
);
1104 /* Transform bcmp (a, b, len) into memcmp (a, b, len). */
1106 gimple
*stmt
= gsi_stmt (*gsi
);
1107 tree a
= gimple_call_arg (stmt
, 0);
1108 tree b
= gimple_call_arg (stmt
, 1);
1109 tree len
= gimple_call_arg (stmt
, 2);
1111 gimple
*repl
= gimple_build_call (fn
, 3, a
, b
, len
);
1112 replace_call_with_call_and_fold (gsi
, repl
);
1117 /* Transform a call to built-in bcopy (src, dest, len) at *GSI into one
1118 to built-in memmove (dest, src, len). */
1121 gimple_fold_builtin_bcopy (gimple_stmt_iterator
*gsi
)
1123 tree fn
= builtin_decl_implicit (BUILT_IN_MEMMOVE
);
1128 /* bcopy has been removed from POSIX in Issue 7 but Issue 6 specifies
1129 it's quivalent to memmove (not memcpy). Transform bcopy (src, dest,
1130 len) into memmove (dest, src, len). */
1132 gimple
*stmt
= gsi_stmt (*gsi
);
1133 tree src
= gimple_call_arg (stmt
, 0);
1134 tree dest
= gimple_call_arg (stmt
, 1);
1135 tree len
= gimple_call_arg (stmt
, 2);
1137 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1138 gimple_call_set_fntype (as_a
<gcall
*> (stmt
), TREE_TYPE (fn
));
1139 replace_call_with_call_and_fold (gsi
, repl
);
1144 /* Transform a call to built-in bzero (dest, len) at *GSI into one
1145 to built-in memset (dest, 0, len). */
1148 gimple_fold_builtin_bzero (gimple_stmt_iterator
*gsi
)
1150 tree fn
= builtin_decl_implicit (BUILT_IN_MEMSET
);
1155 /* Transform bzero (dest, len) into memset (dest, 0, len). */
1157 gimple
*stmt
= gsi_stmt (*gsi
);
1158 tree dest
= gimple_call_arg (stmt
, 0);
1159 tree len
= gimple_call_arg (stmt
, 1);
1161 gimple_seq seq
= NULL
;
1162 gimple
*repl
= gimple_build_call (fn
, 3, dest
, integer_zero_node
, len
);
1163 gimple_seq_add_stmt_without_update (&seq
, repl
);
1164 gsi_replace_with_seq_vops (gsi
, seq
);
1170 /* Fold function call to builtin memset or bzero at *GSI setting the
1171 memory of size LEN to VAL. Return whether a simplification was made. */
1174 gimple_fold_builtin_memset (gimple_stmt_iterator
*gsi
, tree c
, tree len
)
1176 gimple
*stmt
= gsi_stmt (*gsi
);
1178 unsigned HOST_WIDE_INT length
, cval
;
1180 /* If the LEN parameter is zero, return DEST. */
1181 if (integer_zerop (len
))
1183 replace_call_with_value (gsi
, gimple_call_arg (stmt
, 0));
1187 if (! tree_fits_uhwi_p (len
))
1190 if (TREE_CODE (c
) != INTEGER_CST
)
1193 tree dest
= gimple_call_arg (stmt
, 0);
1195 if (TREE_CODE (var
) != ADDR_EXPR
)
1198 var
= TREE_OPERAND (var
, 0);
1199 if (TREE_THIS_VOLATILE (var
))
1202 etype
= TREE_TYPE (var
);
1203 if (TREE_CODE (etype
) == ARRAY_TYPE
)
1204 etype
= TREE_TYPE (etype
);
1206 if (!INTEGRAL_TYPE_P (etype
)
1207 && !POINTER_TYPE_P (etype
))
1210 if (! var_decl_component_p (var
))
1213 length
= tree_to_uhwi (len
);
1214 if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (etype
)) != length
1215 || get_pointer_alignment (dest
) / BITS_PER_UNIT
< length
)
1218 if (length
> HOST_BITS_PER_WIDE_INT
/ BITS_PER_UNIT
)
1221 if (integer_zerop (c
))
1225 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8 || HOST_BITS_PER_WIDE_INT
> 64)
1228 cval
= TREE_INT_CST_LOW (c
);
1232 cval
|= (cval
<< 31) << 1;
1235 var
= fold_build2 (MEM_REF
, etype
, dest
, build_int_cst (ptr_type_node
, 0));
1236 gimple
*store
= gimple_build_assign (var
, build_int_cst_type (etype
, cval
));
1237 gimple_set_vuse (store
, gimple_vuse (stmt
));
1238 tree vdef
= gimple_vdef (stmt
);
1239 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
1241 gimple_set_vdef (store
, gimple_vdef (stmt
));
1242 SSA_NAME_DEF_STMT (gimple_vdef (stmt
)) = store
;
1244 gsi_insert_before (gsi
, store
, GSI_SAME_STMT
);
1245 if (gimple_call_lhs (stmt
))
1247 gimple
*asgn
= gimple_build_assign (gimple_call_lhs (stmt
), dest
);
1248 gsi_replace (gsi
, asgn
, false);
1252 gimple_stmt_iterator gsi2
= *gsi
;
1254 gsi_remove (&gsi2
, true);
1261 /* Obtain the minimum and maximum string length or minimum and maximum
1262 value of ARG in LENGTH[0] and LENGTH[1], respectively.
1263 If ARG is an SSA name variable, follow its use-def chains. When
1264 TYPE == 0, if LENGTH[1] is not equal to the length we determine or
1265 if we are unable to determine the length or value, return false.
1266 VISITED is a bitmap of visited variables.
1267 TYPE is 0 if string length should be obtained, 1 for maximum string
1268 length and 2 for maximum value ARG can have.
1269 When FUZZY is non-zero and the length of a string cannot be determined,
1270 the function instead considers as the maximum possible length the
1271 size of a character array it may refer to. If FUZZY is 2, it will handle
1272 PHIs and COND_EXPRs optimistically, if we can determine string length
1273 minimum and maximum, it will use the minimum from the ones where it
1275 Set *FLEXP to true if the range of the string lengths has been
1276 obtained from the upper bound of an array at the end of a struct.
1277 Such an array may hold a string that's longer than its upper bound
1278 due to it being used as a poor-man's flexible array member. */
1281 get_range_strlen (tree arg
, tree length
[2], bitmap
*visited
, int type
,
1282 int fuzzy
, bool *flexp
)
1284 tree var
, val
= NULL_TREE
;
1287 /* The minimum and maximum length. */
1288 tree
*const minlen
= length
;
1289 tree
*const maxlen
= length
+ 1;
1291 if (TREE_CODE (arg
) != SSA_NAME
)
1293 /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */
1294 if (TREE_CODE (arg
) == ADDR_EXPR
1295 && TREE_CODE (TREE_OPERAND (arg
, 0)) == ARRAY_REF
)
1297 tree op
= TREE_OPERAND (arg
, 0);
1298 if (integer_zerop (TREE_OPERAND (op
, 1)))
1300 tree aop0
= TREE_OPERAND (op
, 0);
1301 if (TREE_CODE (aop0
) == INDIRECT_REF
1302 && TREE_CODE (TREE_OPERAND (aop0
, 0)) == SSA_NAME
)
1303 return get_range_strlen (TREE_OPERAND (aop0
, 0),
1304 length
, visited
, type
, fuzzy
, flexp
);
1306 else if (TREE_CODE (TREE_OPERAND (op
, 0)) == COMPONENT_REF
&& fuzzy
)
1308 /* Fail if an array is the last member of a struct object
1309 since it could be treated as a (fake) flexible array
1311 tree idx
= TREE_OPERAND (op
, 1);
1313 arg
= TREE_OPERAND (op
, 0);
1314 tree optype
= TREE_TYPE (arg
);
1315 if (tree dom
= TYPE_DOMAIN (optype
))
1316 if (tree bound
= TYPE_MAX_VALUE (dom
))
1317 if (TREE_CODE (bound
) == INTEGER_CST
1318 && TREE_CODE (idx
) == INTEGER_CST
1319 && tree_int_cst_lt (bound
, idx
))
1327 if (TREE_CODE (val
) != INTEGER_CST
1328 || tree_int_cst_sgn (val
) < 0)
1332 val
= c_strlen (arg
, 1);
1336 if (TREE_CODE (arg
) == ADDR_EXPR
)
1337 return get_range_strlen (TREE_OPERAND (arg
, 0), length
,
1338 visited
, type
, fuzzy
, flexp
);
1340 if (TREE_CODE (arg
) == ARRAY_REF
)
1342 tree type
= TREE_TYPE (TREE_OPERAND (arg
, 0));
1344 /* Determine the "innermost" array type. */
1345 while (TREE_CODE (type
) == ARRAY_TYPE
1346 && TREE_CODE (TREE_TYPE (type
)) == ARRAY_TYPE
)
1347 type
= TREE_TYPE (type
);
1349 /* Avoid arrays of pointers. */
1350 tree eltype
= TREE_TYPE (type
);
1351 if (TREE_CODE (type
) != ARRAY_TYPE
1352 || !INTEGRAL_TYPE_P (eltype
))
1355 val
= TYPE_SIZE_UNIT (type
);
1356 if (!val
|| integer_zerop (val
))
1359 val
= fold_build2 (MINUS_EXPR
, TREE_TYPE (val
), val
,
1361 /* Set the minimum size to zero since the string in
1362 the array could have zero length. */
1363 *minlen
= ssize_int (0);
1365 if (TREE_CODE (TREE_OPERAND (arg
, 0)) == COMPONENT_REF
1366 && type
== TREE_TYPE (TREE_OPERAND (arg
, 0))
1367 && array_at_struct_end_p (TREE_OPERAND (arg
, 0)))
1370 else if (TREE_CODE (arg
) == COMPONENT_REF
1371 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (arg
, 1)))
1374 /* Use the type of the member array to determine the upper
1375 bound on the length of the array. This may be overly
1376 optimistic if the array itself isn't NUL-terminated and
1377 the caller relies on the subsequent member to contain
1378 the NUL but that would only be considered valid if
1379 the array were the last member of a struct.
1380 Set *FLEXP to true if the array whose bound is being
1381 used is at the end of a struct. */
1382 if (array_at_struct_end_p (arg
))
1385 arg
= TREE_OPERAND (arg
, 1);
1387 tree type
= TREE_TYPE (arg
);
1389 while (TREE_CODE (type
) == ARRAY_TYPE
1390 && TREE_CODE (TREE_TYPE (type
)) == ARRAY_TYPE
)
1391 type
= TREE_TYPE (type
);
1393 /* Fail when the array bound is unknown or zero. */
1394 val
= TYPE_SIZE_UNIT (type
);
1395 if (!val
|| integer_zerop (val
))
1397 val
= fold_build2 (MINUS_EXPR
, TREE_TYPE (val
), val
,
1399 /* Set the minimum size to zero since the string in
1400 the array could have zero length. */
1401 *minlen
= ssize_int (0);
1406 tree type
= TREE_TYPE (arg
);
1407 if (POINTER_TYPE_P (type
))
1408 type
= TREE_TYPE (type
);
1410 if (TREE_CODE (type
) == ARRAY_TYPE
)
1412 val
= TYPE_SIZE_UNIT (type
);
1414 || TREE_CODE (val
) != INTEGER_CST
1415 || integer_zerop (val
))
1417 val
= wide_int_to_tree (TREE_TYPE (val
),
1418 wi::sub (wi::to_wide (val
), 1));
1419 /* Set the minimum size to zero since the string in
1420 the array could have zero length. */
1421 *minlen
= ssize_int (0);
1431 && TREE_CODE (*minlen
) == INTEGER_CST
1432 && TREE_CODE (val
) == INTEGER_CST
1433 && tree_int_cst_lt (val
, *minlen
)))
1440 if (TREE_CODE (*maxlen
) != INTEGER_CST
1441 || TREE_CODE (val
) != INTEGER_CST
)
1444 if (tree_int_cst_lt (*maxlen
, val
))
1448 else if (simple_cst_equal (val
, *maxlen
) != 1)
1456 /* If ARG is registered for SSA update we cannot look at its defining
1458 if (name_registered_for_update_p (arg
))
1461 /* If we were already here, break the infinite cycle. */
1463 *visited
= BITMAP_ALLOC (NULL
);
1464 if (!bitmap_set_bit (*visited
, SSA_NAME_VERSION (arg
)))
1468 def_stmt
= SSA_NAME_DEF_STMT (var
);
1470 switch (gimple_code (def_stmt
))
1473 /* The RHS of the statement defining VAR must either have a
1474 constant length or come from another SSA_NAME with a constant
1476 if (gimple_assign_single_p (def_stmt
)
1477 || gimple_assign_unary_nop_p (def_stmt
))
1479 tree rhs
= gimple_assign_rhs1 (def_stmt
);
1480 return get_range_strlen (rhs
, length
, visited
, type
, fuzzy
, flexp
);
1482 else if (gimple_assign_rhs_code (def_stmt
) == COND_EXPR
)
1484 tree ops
[2] = { gimple_assign_rhs2 (def_stmt
),
1485 gimple_assign_rhs3 (def_stmt
) };
1487 for (unsigned int i
= 0; i
< 2; i
++)
1488 if (!get_range_strlen (ops
[i
], length
, visited
, type
, fuzzy
,
1492 *maxlen
= build_all_ones_cst (size_type_node
);
1501 /* All the arguments of the PHI node must have the same constant
1503 for (unsigned i
= 0; i
< gimple_phi_num_args (def_stmt
); i
++)
1505 tree arg
= gimple_phi_arg (def_stmt
, i
)->def
;
1507 /* If this PHI has itself as an argument, we cannot
1508 determine the string length of this argument. However,
1509 if we can find a constant string length for the other
1510 PHI args then we can still be sure that this is a
1511 constant string length. So be optimistic and just
1512 continue with the next argument. */
1513 if (arg
== gimple_phi_result (def_stmt
))
1516 if (!get_range_strlen (arg
, length
, visited
, type
, fuzzy
, flexp
))
1519 *maxlen
= build_all_ones_cst (size_type_node
);
1531 /* Determine the minimum and maximum value or string length that ARG
1532 refers to and store each in the first two elements of MINMAXLEN.
1533 For expressions that point to strings of unknown lengths that are
1534 character arrays, use the upper bound of the array as the maximum
1535 length. For example, given an expression like 'x ? array : "xyz"'
1536 and array declared as 'char array[8]', MINMAXLEN[0] will be set
1537 to 0 and MINMAXLEN[1] to 7, the longest string that could be
1539 Return true if the range of the string lengths has been obtained
1540 from the upper bound of an array at the end of a struct. Such
1541 an array may hold a string that's longer than its upper bound
1542 due to it being used as a poor-man's flexible array member.
1544 STRICT is true if it will handle PHIs and COND_EXPRs conservatively
1545 and false if PHIs and COND_EXPRs are to be handled optimistically,
1546 if we can determine string length minimum and maximum; it will use
1547 the minimum from the ones where it can be determined.
1548 STRICT false should be only used for warning code. */
1551 get_range_strlen (tree arg
, tree minmaxlen
[2], bool strict
)
1553 bitmap visited
= NULL
;
1555 minmaxlen
[0] = NULL_TREE
;
1556 minmaxlen
[1] = NULL_TREE
;
1558 bool flexarray
= false;
1559 if (!get_range_strlen (arg
, minmaxlen
, &visited
, 1, strict
? 1 : 2,
1562 minmaxlen
[0] = NULL_TREE
;
1563 minmaxlen
[1] = NULL_TREE
;
1567 BITMAP_FREE (visited
);
1573 get_maxval_strlen (tree arg
, int type
)
1575 bitmap visited
= NULL
;
1576 tree len
[2] = { NULL_TREE
, NULL_TREE
};
1579 if (!get_range_strlen (arg
, len
, &visited
, type
, 0, &dummy
))
1582 BITMAP_FREE (visited
);
1588 /* Fold function call to builtin strcpy with arguments DEST and SRC.
1589 If LEN is not NULL, it represents the length of the string to be
1590 copied. Return NULL_TREE if no simplification can be made. */
1593 gimple_fold_builtin_strcpy (gimple_stmt_iterator
*gsi
,
1594 tree dest
, tree src
)
1596 gimple
*stmt
= gsi_stmt (*gsi
);
1597 location_t loc
= gimple_location (stmt
);
1600 /* If SRC and DEST are the same (and not volatile), return DEST. */
1601 if (operand_equal_p (src
, dest
, 0))
1603 /* Issue -Wrestrict unless the pointers are null (those do
1604 not point to objects and so do not indicate an overlap;
1605 such calls could be the result of sanitization and jump
1607 if (!integer_zerop (dest
) && !gimple_no_warning_p (stmt
))
1609 tree func
= gimple_call_fndecl (stmt
);
1611 warning_at (loc
, OPT_Wrestrict
,
1612 "%qD source argument is the same as destination",
1616 replace_call_with_value (gsi
, dest
);
1620 if (optimize_function_for_size_p (cfun
))
1623 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
1627 tree len
= get_maxval_strlen (src
, 0);
1631 len
= fold_convert_loc (loc
, size_type_node
, len
);
1632 len
= size_binop_loc (loc
, PLUS_EXPR
, len
, build_int_cst (size_type_node
, 1));
1633 len
= force_gimple_operand_gsi (gsi
, len
, true,
1634 NULL_TREE
, true, GSI_SAME_STMT
);
1635 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1636 replace_call_with_call_and_fold (gsi
, repl
);
1640 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
1641 If SLEN is not NULL, it represents the length of the source string.
1642 Return NULL_TREE if no simplification can be made. */
1645 gimple_fold_builtin_strncpy (gimple_stmt_iterator
*gsi
,
1646 tree dest
, tree src
, tree len
)
1648 gimple
*stmt
= gsi_stmt (*gsi
);
1649 location_t loc
= gimple_location (stmt
);
1650 bool nonstring
= get_attr_nonstring_decl (dest
) != NULL_TREE
;
1652 /* If the LEN parameter is zero, return DEST. */
1653 if (integer_zerop (len
))
1655 /* Avoid warning if the destination refers to a an array/pointer
1656 decorate with attribute nonstring. */
1659 tree fndecl
= gimple_call_fndecl (stmt
);
1660 gcall
*call
= as_a
<gcall
*> (stmt
);
1662 /* Warn about the lack of nul termination: the result is not
1663 a (nul-terminated) string. */
1664 tree slen
= get_maxval_strlen (src
, 0);
1665 if (slen
&& !integer_zerop (slen
))
1666 warning_at (loc
, OPT_Wstringop_truncation
,
1667 "%G%qD destination unchanged after copying no bytes "
1668 "from a string of length %E",
1669 call
, fndecl
, slen
);
1671 warning_at (loc
, OPT_Wstringop_truncation
,
1672 "%G%qD destination unchanged after copying no bytes",
1676 replace_call_with_value (gsi
, dest
);
1680 /* We can't compare slen with len as constants below if len is not a
1682 if (TREE_CODE (len
) != INTEGER_CST
)
1685 /* Now, we must be passed a constant src ptr parameter. */
1686 tree slen
= get_maxval_strlen (src
, 0);
1687 if (!slen
|| TREE_CODE (slen
) != INTEGER_CST
)
1690 /* The size of the source string including the terminating nul. */
1691 tree ssize
= size_binop_loc (loc
, PLUS_EXPR
, slen
, ssize_int (1));
1693 /* We do not support simplification of this case, though we do
1694 support it when expanding trees into RTL. */
1695 /* FIXME: generate a call to __builtin_memset. */
1696 if (tree_int_cst_lt (ssize
, len
))
1699 /* Diagnose truncation that leaves the copy unterminated. */
1700 maybe_diag_stxncpy_trunc (*gsi
, src
, len
);
1702 /* OK transform into builtin memcpy. */
1703 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
1707 len
= fold_convert_loc (loc
, size_type_node
, len
);
1708 len
= force_gimple_operand_gsi (gsi
, len
, true,
1709 NULL_TREE
, true, GSI_SAME_STMT
);
1710 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1711 replace_call_with_call_and_fold (gsi
, repl
);
1716 /* Fold function call to builtin strchr or strrchr.
1717 If both arguments are constant, evaluate and fold the result,
1718 otherwise simplify str(r)chr (str, 0) into str + strlen (str).
1719 In general strlen is significantly faster than strchr
1720 due to being a simpler operation. */
1722 gimple_fold_builtin_strchr (gimple_stmt_iterator
*gsi
, bool is_strrchr
)
1724 gimple
*stmt
= gsi_stmt (*gsi
);
1725 tree str
= gimple_call_arg (stmt
, 0);
1726 tree c
= gimple_call_arg (stmt
, 1);
1727 location_t loc
= gimple_location (stmt
);
1731 if (!gimple_call_lhs (stmt
))
1734 if ((p
= c_getstr (str
)) && target_char_cst_p (c
, &ch
))
1736 const char *p1
= is_strrchr
? strrchr (p
, ch
) : strchr (p
, ch
);
1740 replace_call_with_value (gsi
, integer_zero_node
);
1744 tree len
= build_int_cst (size_type_node
, p1
- p
);
1745 gimple_seq stmts
= NULL
;
1746 gimple
*new_stmt
= gimple_build_assign (gimple_call_lhs (stmt
),
1747 POINTER_PLUS_EXPR
, str
, len
);
1748 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
1749 gsi_replace_with_seq_vops (gsi
, stmts
);
1753 if (!integer_zerop (c
))
1756 /* Transform strrchr (s, 0) to strchr (s, 0) when optimizing for size. */
1757 if (is_strrchr
&& optimize_function_for_size_p (cfun
))
1759 tree strchr_fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
1763 gimple
*repl
= gimple_build_call (strchr_fn
, 2, str
, c
);
1764 replace_call_with_call_and_fold (gsi
, repl
);
1772 tree strlen_fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
1777 /* Create newstr = strlen (str). */
1778 gimple_seq stmts
= NULL
;
1779 gimple
*new_stmt
= gimple_build_call (strlen_fn
, 1, str
);
1780 gimple_set_location (new_stmt
, loc
);
1781 len
= create_tmp_reg_or_ssa_name (size_type_node
);
1782 gimple_call_set_lhs (new_stmt
, len
);
1783 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
1785 /* Create (str p+ strlen (str)). */
1786 new_stmt
= gimple_build_assign (gimple_call_lhs (stmt
),
1787 POINTER_PLUS_EXPR
, str
, len
);
1788 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
1789 gsi_replace_with_seq_vops (gsi
, stmts
);
1790 /* gsi now points at the assignment to the lhs, get a
1791 stmt iterator to the strlen.
1792 ??? We can't use gsi_for_stmt as that doesn't work when the
1793 CFG isn't built yet. */
1794 gimple_stmt_iterator gsi2
= *gsi
;
1800 /* Fold function call to builtin strstr.
1801 If both arguments are constant, evaluate and fold the result,
1802 additionally fold strstr (x, "") into x and strstr (x, "c")
1803 into strchr (x, 'c'). */
1805 gimple_fold_builtin_strstr (gimple_stmt_iterator
*gsi
)
1807 gimple
*stmt
= gsi_stmt (*gsi
);
1808 tree haystack
= gimple_call_arg (stmt
, 0);
1809 tree needle
= gimple_call_arg (stmt
, 1);
1812 if (!gimple_call_lhs (stmt
))
1815 q
= c_getstr (needle
);
1819 if ((p
= c_getstr (haystack
)))
1821 const char *r
= strstr (p
, q
);
1825 replace_call_with_value (gsi
, integer_zero_node
);
1829 tree len
= build_int_cst (size_type_node
, r
- p
);
1830 gimple_seq stmts
= NULL
;
1832 = gimple_build_assign (gimple_call_lhs (stmt
), POINTER_PLUS_EXPR
,
1834 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
1835 gsi_replace_with_seq_vops (gsi
, stmts
);
1839 /* For strstr (x, "") return x. */
1842 replace_call_with_value (gsi
, haystack
);
1846 /* Transform strstr (x, "c") into strchr (x, 'c'). */
1849 tree strchr_fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
1852 tree c
= build_int_cst (integer_type_node
, q
[0]);
1853 gimple
*repl
= gimple_build_call (strchr_fn
, 2, haystack
, c
);
1854 replace_call_with_call_and_fold (gsi
, repl
);
1862 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
1865 Return NULL_TREE if no simplification was possible, otherwise return the
1866 simplified form of the call as a tree.
1868 The simplified form may be a constant or other expression which
1869 computes the same value, but in a more efficient manner (including
1870 calls to other builtin functions).
1872 The call may contain arguments which need to be evaluated, but
1873 which are not useful to determine the result of the call. In
1874 this case we return a chain of COMPOUND_EXPRs. The LHS of each
1875 COMPOUND_EXPR will be an argument which must be evaluated.
1876 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
1877 COMPOUND_EXPR in the chain will contain the tree for the simplified
1878 form of the builtin function call. */
1881 gimple_fold_builtin_strcat (gimple_stmt_iterator
*gsi
, tree dst
, tree src
)
1883 gimple
*stmt
= gsi_stmt (*gsi
);
1884 location_t loc
= gimple_location (stmt
);
1886 const char *p
= c_getstr (src
);
1888 /* If the string length is zero, return the dst parameter. */
1889 if (p
&& *p
== '\0')
1891 replace_call_with_value (gsi
, dst
);
1895 if (!optimize_bb_for_speed_p (gimple_bb (stmt
)))
1898 /* See if we can store by pieces into (dst + strlen(dst)). */
1900 tree strlen_fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
1901 tree memcpy_fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
1903 if (!strlen_fn
|| !memcpy_fn
)
1906 /* If the length of the source string isn't computable don't
1907 split strcat into strlen and memcpy. */
1908 tree len
= get_maxval_strlen (src
, 0);
1912 /* Create strlen (dst). */
1913 gimple_seq stmts
= NULL
, stmts2
;
1914 gimple
*repl
= gimple_build_call (strlen_fn
, 1, dst
);
1915 gimple_set_location (repl
, loc
);
1916 newdst
= create_tmp_reg_or_ssa_name (size_type_node
);
1917 gimple_call_set_lhs (repl
, newdst
);
1918 gimple_seq_add_stmt_without_update (&stmts
, repl
);
1920 /* Create (dst p+ strlen (dst)). */
1921 newdst
= fold_build_pointer_plus_loc (loc
, dst
, newdst
);
1922 newdst
= force_gimple_operand (newdst
, &stmts2
, true, NULL_TREE
);
1923 gimple_seq_add_seq_without_update (&stmts
, stmts2
);
1925 len
= fold_convert_loc (loc
, size_type_node
, len
);
1926 len
= size_binop_loc (loc
, PLUS_EXPR
, len
,
1927 build_int_cst (size_type_node
, 1));
1928 len
= force_gimple_operand (len
, &stmts2
, true, NULL_TREE
);
1929 gimple_seq_add_seq_without_update (&stmts
, stmts2
);
1931 repl
= gimple_build_call (memcpy_fn
, 3, newdst
, src
, len
);
1932 gimple_seq_add_stmt_without_update (&stmts
, repl
);
1933 if (gimple_call_lhs (stmt
))
1935 repl
= gimple_build_assign (gimple_call_lhs (stmt
), dst
);
1936 gimple_seq_add_stmt_without_update (&stmts
, repl
);
1937 gsi_replace_with_seq_vops (gsi
, stmts
);
1938 /* gsi now points at the assignment to the lhs, get a
1939 stmt iterator to the memcpy call.
1940 ??? We can't use gsi_for_stmt as that doesn't work when the
1941 CFG isn't built yet. */
1942 gimple_stmt_iterator gsi2
= *gsi
;
1948 gsi_replace_with_seq_vops (gsi
, stmts
);
1954 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
1955 are the arguments to the call. */
1958 gimple_fold_builtin_strcat_chk (gimple_stmt_iterator
*gsi
)
1960 gimple
*stmt
= gsi_stmt (*gsi
);
1961 tree dest
= gimple_call_arg (stmt
, 0);
1962 tree src
= gimple_call_arg (stmt
, 1);
1963 tree size
= gimple_call_arg (stmt
, 2);
1969 /* If the SRC parameter is "", return DEST. */
1970 if (p
&& *p
== '\0')
1972 replace_call_with_value (gsi
, dest
);
1976 if (! tree_fits_uhwi_p (size
) || ! integer_all_onesp (size
))
1979 /* If __builtin_strcat_chk is used, assume strcat is available. */
1980 fn
= builtin_decl_explicit (BUILT_IN_STRCAT
);
1984 gimple
*repl
= gimple_build_call (fn
, 2, dest
, src
);
1985 replace_call_with_call_and_fold (gsi
, repl
);
1989 /* Simplify a call to the strncat builtin. */
1992 gimple_fold_builtin_strncat (gimple_stmt_iterator
*gsi
)
1994 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
1995 tree dst
= gimple_call_arg (stmt
, 0);
1996 tree src
= gimple_call_arg (stmt
, 1);
1997 tree len
= gimple_call_arg (stmt
, 2);
1999 const char *p
= c_getstr (src
);
2001 /* If the requested length is zero, or the src parameter string
2002 length is zero, return the dst parameter. */
2003 if (integer_zerop (len
) || (p
&& *p
== '\0'))
2005 replace_call_with_value (gsi
, dst
);
2009 if (TREE_CODE (len
) != INTEGER_CST
|| !p
)
2012 unsigned srclen
= strlen (p
);
2014 int cmpsrc
= compare_tree_int (len
, srclen
);
2016 /* Return early if the requested len is less than the string length.
2017 Warnings will be issued elsewhere later. */
2021 unsigned HOST_WIDE_INT dstsize
;
2023 bool nowarn
= gimple_no_warning_p (stmt
);
2025 if (!nowarn
&& compute_builtin_object_size (dst
, 1, &dstsize
))
2027 int cmpdst
= compare_tree_int (len
, dstsize
);
2031 tree fndecl
= gimple_call_fndecl (stmt
);
2033 /* Strncat copies (at most) LEN bytes and always appends
2034 the terminating NUL so the specified bound should never
2035 be equal to (or greater than) the size of the destination.
2036 If it is, the copy could overflow. */
2037 location_t loc
= gimple_location (stmt
);
2038 nowarn
= warning_at (loc
, OPT_Wstringop_overflow_
,
2040 ? G_("%G%qD specified bound %E equals "
2042 : G_("%G%qD specified bound %E exceeds "
2043 "destination size %wu"),
2044 stmt
, fndecl
, len
, dstsize
);
2046 gimple_set_no_warning (stmt
, true);
2050 if (!nowarn
&& cmpsrc
== 0)
2052 tree fndecl
= gimple_call_fndecl (stmt
);
2053 location_t loc
= gimple_location (stmt
);
2055 /* To avoid possible overflow the specified bound should also
2056 not be equal to the length of the source, even when the size
2057 of the destination is unknown (it's not an uncommon mistake
2058 to specify as the bound to strncpy the length of the source). */
2059 if (warning_at (loc
, OPT_Wstringop_overflow_
,
2060 "%G%qD specified bound %E equals source length",
2062 gimple_set_no_warning (stmt
, true);
2065 tree fn
= builtin_decl_implicit (BUILT_IN_STRCAT
);
2067 /* If the replacement _DECL isn't initialized, don't do the
2072 /* Otherwise, emit a call to strcat. */
2073 gcall
*repl
= gimple_build_call (fn
, 2, dst
, src
);
2074 replace_call_with_call_and_fold (gsi
, repl
);
2078 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
2082 gimple_fold_builtin_strncat_chk (gimple_stmt_iterator
*gsi
)
2084 gimple
*stmt
= gsi_stmt (*gsi
);
2085 tree dest
= gimple_call_arg (stmt
, 0);
2086 tree src
= gimple_call_arg (stmt
, 1);
2087 tree len
= gimple_call_arg (stmt
, 2);
2088 tree size
= gimple_call_arg (stmt
, 3);
2093 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
2094 if ((p
&& *p
== '\0')
2095 || integer_zerop (len
))
2097 replace_call_with_value (gsi
, dest
);
2101 if (! tree_fits_uhwi_p (size
))
2104 if (! integer_all_onesp (size
))
2106 tree src_len
= c_strlen (src
, 1);
2108 && tree_fits_uhwi_p (src_len
)
2109 && tree_fits_uhwi_p (len
)
2110 && ! tree_int_cst_lt (len
, src_len
))
2112 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
2113 fn
= builtin_decl_explicit (BUILT_IN_STRCAT_CHK
);
2117 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, size
);
2118 replace_call_with_call_and_fold (gsi
, repl
);
2124 /* If __builtin_strncat_chk is used, assume strncat is available. */
2125 fn
= builtin_decl_explicit (BUILT_IN_STRNCAT
);
2129 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
2130 replace_call_with_call_and_fold (gsi
, repl
);
2134 /* Build and append gimple statements to STMTS that would load a first
2135 character of a memory location identified by STR. LOC is location
2136 of the statement. */
2139 gimple_load_first_char (location_t loc
, tree str
, gimple_seq
*stmts
)
2143 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
2144 tree cst_uchar_ptr_node
2145 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
2146 tree off0
= build_int_cst (cst_uchar_ptr_node
, 0);
2148 tree temp
= fold_build2_loc (loc
, MEM_REF
, cst_uchar_node
, str
, off0
);
2149 gassign
*stmt
= gimple_build_assign (NULL_TREE
, temp
);
2150 var
= create_tmp_reg_or_ssa_name (cst_uchar_node
, stmt
);
2152 gimple_assign_set_lhs (stmt
, var
);
2153 gimple_seq_add_stmt_without_update (stmts
, stmt
);
2158 /* Fold a call to the str{n}{case}cmp builtin pointed by GSI iterator.
2159 FCODE is the name of the builtin. */
2162 gimple_fold_builtin_string_compare (gimple_stmt_iterator
*gsi
)
2164 gimple
*stmt
= gsi_stmt (*gsi
);
2165 tree callee
= gimple_call_fndecl (stmt
);
2166 enum built_in_function fcode
= DECL_FUNCTION_CODE (callee
);
2168 tree type
= integer_type_node
;
2169 tree str1
= gimple_call_arg (stmt
, 0);
2170 tree str2
= gimple_call_arg (stmt
, 1);
2171 tree lhs
= gimple_call_lhs (stmt
);
2172 HOST_WIDE_INT length
= -1;
2174 /* Handle strncmp and strncasecmp functions. */
2175 if (gimple_call_num_args (stmt
) == 3)
2177 tree len
= gimple_call_arg (stmt
, 2);
2178 if (tree_fits_uhwi_p (len
))
2179 length
= tree_to_uhwi (len
);
2182 /* If the LEN parameter is zero, return zero. */
2185 replace_call_with_value (gsi
, integer_zero_node
);
2189 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
2190 if (operand_equal_p (str1
, str2
, 0))
2192 replace_call_with_value (gsi
, integer_zero_node
);
2196 const char *p1
= c_getstr (str1
);
2197 const char *p2
= c_getstr (str2
);
2199 /* For known strings, return an immediate value. */
2203 bool known_result
= false;
2207 case BUILT_IN_STRCMP
:
2208 case BUILT_IN_STRCMP_EQ
:
2210 r
= strcmp (p1
, p2
);
2211 known_result
= true;
2214 case BUILT_IN_STRNCMP
:
2215 case BUILT_IN_STRNCMP_EQ
:
2219 r
= strncmp (p1
, p2
, length
);
2220 known_result
= true;
2223 /* Only handleable situation is where the string are equal (result 0),
2224 which is already handled by operand_equal_p case. */
2225 case BUILT_IN_STRCASECMP
:
2227 case BUILT_IN_STRNCASECMP
:
2231 r
= strncmp (p1
, p2
, length
);
2233 known_result
= true;
2242 replace_call_with_value (gsi
, build_cmp_result (type
, r
));
2247 bool nonzero_length
= length
>= 1
2248 || fcode
== BUILT_IN_STRCMP
2249 || fcode
== BUILT_IN_STRCMP_EQ
2250 || fcode
== BUILT_IN_STRCASECMP
;
2252 location_t loc
= gimple_location (stmt
);
2254 /* If the second arg is "", return *(const unsigned char*)arg1. */
2255 if (p2
&& *p2
== '\0' && nonzero_length
)
2257 gimple_seq stmts
= NULL
;
2258 tree var
= gimple_load_first_char (loc
, str1
, &stmts
);
2261 stmt
= gimple_build_assign (lhs
, NOP_EXPR
, var
);
2262 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2265 gsi_replace_with_seq_vops (gsi
, stmts
);
2269 /* If the first arg is "", return -*(const unsigned char*)arg2. */
2270 if (p1
&& *p1
== '\0' && nonzero_length
)
2272 gimple_seq stmts
= NULL
;
2273 tree var
= gimple_load_first_char (loc
, str2
, &stmts
);
2277 tree c
= create_tmp_reg_or_ssa_name (integer_type_node
);
2278 stmt
= gimple_build_assign (c
, NOP_EXPR
, var
);
2279 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2281 stmt
= gimple_build_assign (lhs
, NEGATE_EXPR
, c
);
2282 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2285 gsi_replace_with_seq_vops (gsi
, stmts
);
2289 /* If len parameter is one, return an expression corresponding to
2290 (*(const unsigned char*)arg2 - *(const unsigned char*)arg1). */
2291 if (fcode
== BUILT_IN_STRNCMP
&& length
== 1)
2293 gimple_seq stmts
= NULL
;
2294 tree temp1
= gimple_load_first_char (loc
, str1
, &stmts
);
2295 tree temp2
= gimple_load_first_char (loc
, str2
, &stmts
);
2299 tree c1
= create_tmp_reg_or_ssa_name (integer_type_node
);
2300 gassign
*convert1
= gimple_build_assign (c1
, NOP_EXPR
, temp1
);
2301 gimple_seq_add_stmt_without_update (&stmts
, convert1
);
2303 tree c2
= create_tmp_reg_or_ssa_name (integer_type_node
);
2304 gassign
*convert2
= gimple_build_assign (c2
, NOP_EXPR
, temp2
);
2305 gimple_seq_add_stmt_without_update (&stmts
, convert2
);
2307 stmt
= gimple_build_assign (lhs
, MINUS_EXPR
, c1
, c2
);
2308 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2311 gsi_replace_with_seq_vops (gsi
, stmts
);
2315 /* If length is larger than the length of one constant string,
2316 replace strncmp with corresponding strcmp */
2317 if (fcode
== BUILT_IN_STRNCMP
2319 && ((p2
&& (size_t) length
> strlen (p2
))
2320 || (p1
&& (size_t) length
> strlen (p1
))))
2322 tree fn
= builtin_decl_implicit (BUILT_IN_STRCMP
);
2325 gimple
*repl
= gimple_build_call (fn
, 2, str1
, str2
);
2326 replace_call_with_call_and_fold (gsi
, repl
);
2333 /* Fold a call to the memchr pointed by GSI iterator. */
2336 gimple_fold_builtin_memchr (gimple_stmt_iterator
*gsi
)
2338 gimple
*stmt
= gsi_stmt (*gsi
);
2339 tree lhs
= gimple_call_lhs (stmt
);
2340 tree arg1
= gimple_call_arg (stmt
, 0);
2341 tree arg2
= gimple_call_arg (stmt
, 1);
2342 tree len
= gimple_call_arg (stmt
, 2);
2344 /* If the LEN parameter is zero, return zero. */
2345 if (integer_zerop (len
))
2347 replace_call_with_value (gsi
, build_int_cst (ptr_type_node
, 0));
2352 if (TREE_CODE (arg2
) != INTEGER_CST
2353 || !tree_fits_uhwi_p (len
)
2354 || !target_char_cst_p (arg2
, &c
))
2357 unsigned HOST_WIDE_INT length
= tree_to_uhwi (len
);
2358 unsigned HOST_WIDE_INT string_length
;
2359 const char *p1
= c_getstr (arg1
, &string_length
);
2363 const char *r
= (const char *)memchr (p1
, c
, MIN (length
, string_length
));
2366 if (length
<= string_length
)
2368 replace_call_with_value (gsi
, build_int_cst (ptr_type_node
, 0));
2374 unsigned HOST_WIDE_INT offset
= r
- p1
;
2375 gimple_seq stmts
= NULL
;
2376 if (lhs
!= NULL_TREE
)
2378 tree offset_cst
= build_int_cst (TREE_TYPE (len
), offset
);
2379 gassign
*stmt
= gimple_build_assign (lhs
, POINTER_PLUS_EXPR
,
2381 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2384 gimple_seq_add_stmt_without_update (&stmts
,
2385 gimple_build_nop ());
2387 gsi_replace_with_seq_vops (gsi
, stmts
);
2395 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
2396 to the call. IGNORE is true if the value returned
2397 by the builtin will be ignored. UNLOCKED is true is true if this
2398 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
2399 the known length of the string. Return NULL_TREE if no simplification
2403 gimple_fold_builtin_fputs (gimple_stmt_iterator
*gsi
,
2404 tree arg0
, tree arg1
,
2407 gimple
*stmt
= gsi_stmt (*gsi
);
2409 /* If we're using an unlocked function, assume the other unlocked
2410 functions exist explicitly. */
2411 tree
const fn_fputc
= (unlocked
2412 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED
)
2413 : builtin_decl_implicit (BUILT_IN_FPUTC
));
2414 tree
const fn_fwrite
= (unlocked
2415 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED
)
2416 : builtin_decl_implicit (BUILT_IN_FWRITE
));
2418 /* If the return value is used, don't do the transformation. */
2419 if (gimple_call_lhs (stmt
))
2422 /* Get the length of the string passed to fputs. If the length
2423 can't be determined, punt. */
2424 tree len
= get_maxval_strlen (arg0
, 0);
2426 || TREE_CODE (len
) != INTEGER_CST
)
2429 switch (compare_tree_int (len
, 1))
2431 case -1: /* length is 0, delete the call entirely . */
2432 replace_call_with_value (gsi
, integer_zero_node
);
2435 case 0: /* length is 1, call fputc. */
2437 const char *p
= c_getstr (arg0
);
2443 gimple
*repl
= gimple_build_call (fn_fputc
, 2,
2445 (integer_type_node
, p
[0]), arg1
);
2446 replace_call_with_call_and_fold (gsi
, repl
);
2451 case 1: /* length is greater than 1, call fwrite. */
2453 /* If optimizing for size keep fputs. */
2454 if (optimize_function_for_size_p (cfun
))
2456 /* New argument list transforming fputs(string, stream) to
2457 fwrite(string, 1, len, stream). */
2461 gimple
*repl
= gimple_build_call (fn_fwrite
, 4, arg0
,
2462 size_one_node
, len
, arg1
);
2463 replace_call_with_call_and_fold (gsi
, repl
);
2472 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
2473 DEST, SRC, LEN, and SIZE are the arguments to the call.
2474 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
2475 code of the builtin. If MAXLEN is not NULL, it is maximum length
2476 passed as third argument. */
2479 gimple_fold_builtin_memory_chk (gimple_stmt_iterator
*gsi
,
2480 tree dest
, tree src
, tree len
, tree size
,
2481 enum built_in_function fcode
)
2483 gimple
*stmt
= gsi_stmt (*gsi
);
2484 location_t loc
= gimple_location (stmt
);
2485 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
2488 /* If SRC and DEST are the same (and not volatile), return DEST
2489 (resp. DEST+LEN for __mempcpy_chk). */
2490 if (fcode
!= BUILT_IN_MEMSET_CHK
&& operand_equal_p (src
, dest
, 0))
2492 if (fcode
!= BUILT_IN_MEMPCPY_CHK
)
2494 replace_call_with_value (gsi
, dest
);
2499 gimple_seq stmts
= NULL
;
2500 len
= gimple_convert_to_ptrofftype (&stmts
, loc
, len
);
2501 tree temp
= gimple_build (&stmts
, loc
, POINTER_PLUS_EXPR
,
2502 TREE_TYPE (dest
), dest
, len
);
2503 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2504 replace_call_with_value (gsi
, temp
);
2509 if (! tree_fits_uhwi_p (size
))
2512 tree maxlen
= get_maxval_strlen (len
, 2);
2513 if (! integer_all_onesp (size
))
2515 if (! tree_fits_uhwi_p (len
))
2517 /* If LEN is not constant, try MAXLEN too.
2518 For MAXLEN only allow optimizing into non-_ocs function
2519 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2520 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
2522 if (fcode
== BUILT_IN_MEMPCPY_CHK
&& ignore
)
2524 /* (void) __mempcpy_chk () can be optimized into
2525 (void) __memcpy_chk (). */
2526 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
2530 gimple
*repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
2531 replace_call_with_call_and_fold (gsi
, repl
);
2540 if (tree_int_cst_lt (size
, maxlen
))
2545 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
2546 mem{cpy,pcpy,move,set} is available. */
2549 case BUILT_IN_MEMCPY_CHK
:
2550 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY
);
2552 case BUILT_IN_MEMPCPY_CHK
:
2553 fn
= builtin_decl_explicit (BUILT_IN_MEMPCPY
);
2555 case BUILT_IN_MEMMOVE_CHK
:
2556 fn
= builtin_decl_explicit (BUILT_IN_MEMMOVE
);
2558 case BUILT_IN_MEMSET_CHK
:
2559 fn
= builtin_decl_explicit (BUILT_IN_MEMSET
);
2568 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
2569 replace_call_with_call_and_fold (gsi
, repl
);
2573 /* Fold a call to the __st[rp]cpy_chk builtin.
2574 DEST, SRC, and SIZE are the arguments to the call.
2575 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
2576 code of the builtin. If MAXLEN is not NULL, it is maximum length of
2577 strings passed as second argument. */
2580 gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator
*gsi
,
2582 tree src
, tree size
,
2583 enum built_in_function fcode
)
2585 gimple
*stmt
= gsi_stmt (*gsi
);
2586 location_t loc
= gimple_location (stmt
);
2587 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
2590 /* If SRC and DEST are the same (and not volatile), return DEST. */
2591 if (fcode
== BUILT_IN_STRCPY_CHK
&& operand_equal_p (src
, dest
, 0))
2593 /* Issue -Wrestrict unless the pointers are null (those do
2594 not point to objects and so do not indicate an overlap;
2595 such calls could be the result of sanitization and jump
2597 if (!integer_zerop (dest
) && !gimple_no_warning_p (stmt
))
2599 tree func
= gimple_call_fndecl (stmt
);
2601 warning_at (loc
, OPT_Wrestrict
,
2602 "%qD source argument is the same as destination",
2606 replace_call_with_value (gsi
, dest
);
2610 if (! tree_fits_uhwi_p (size
))
2613 tree maxlen
= get_maxval_strlen (src
, 1);
2614 if (! integer_all_onesp (size
))
2616 len
= c_strlen (src
, 1);
2617 if (! len
|| ! tree_fits_uhwi_p (len
))
2619 /* If LEN is not constant, try MAXLEN too.
2620 For MAXLEN only allow optimizing into non-_ocs function
2621 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2622 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
2624 if (fcode
== BUILT_IN_STPCPY_CHK
)
2629 /* If return value of __stpcpy_chk is ignored,
2630 optimize into __strcpy_chk. */
2631 fn
= builtin_decl_explicit (BUILT_IN_STRCPY_CHK
);
2635 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, size
);
2636 replace_call_with_call_and_fold (gsi
, repl
);
2640 if (! len
|| TREE_SIDE_EFFECTS (len
))
2643 /* If c_strlen returned something, but not a constant,
2644 transform __strcpy_chk into __memcpy_chk. */
2645 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
2649 gimple_seq stmts
= NULL
;
2650 len
= gimple_convert (&stmts
, loc
, size_type_node
, len
);
2651 len
= gimple_build (&stmts
, loc
, PLUS_EXPR
, size_type_node
, len
,
2652 build_int_cst (size_type_node
, 1));
2653 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2654 gimple
*repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
2655 replace_call_with_call_and_fold (gsi
, repl
);
2662 if (! tree_int_cst_lt (maxlen
, size
))
2666 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
2667 fn
= builtin_decl_explicit (fcode
== BUILT_IN_STPCPY_CHK
2668 ? BUILT_IN_STPCPY
: BUILT_IN_STRCPY
);
2672 gimple
*repl
= gimple_build_call (fn
, 2, dest
, src
);
2673 replace_call_with_call_and_fold (gsi
, repl
);
2677 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
2678 are the arguments to the call. If MAXLEN is not NULL, it is maximum
2679 length passed as third argument. IGNORE is true if return value can be
2680 ignored. FCODE is the BUILT_IN_* code of the builtin. */
2683 gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator
*gsi
,
2684 tree dest
, tree src
,
2685 tree len
, tree size
,
2686 enum built_in_function fcode
)
2688 gimple
*stmt
= gsi_stmt (*gsi
);
2689 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
2692 if (fcode
== BUILT_IN_STPNCPY_CHK
&& ignore
)
2694 /* If return value of __stpncpy_chk is ignored,
2695 optimize into __strncpy_chk. */
2696 fn
= builtin_decl_explicit (BUILT_IN_STRNCPY_CHK
);
2699 gimple
*repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
2700 replace_call_with_call_and_fold (gsi
, repl
);
2705 if (! tree_fits_uhwi_p (size
))
2708 tree maxlen
= get_maxval_strlen (len
, 2);
2709 if (! integer_all_onesp (size
))
2711 if (! tree_fits_uhwi_p (len
))
2713 /* If LEN is not constant, try MAXLEN too.
2714 For MAXLEN only allow optimizing into non-_ocs function
2715 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2716 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
2722 if (tree_int_cst_lt (size
, maxlen
))
2726 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
2727 fn
= builtin_decl_explicit (fcode
== BUILT_IN_STPNCPY_CHK
2728 ? BUILT_IN_STPNCPY
: BUILT_IN_STRNCPY
);
2732 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
2733 replace_call_with_call_and_fold (gsi
, repl
);
2737 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
2738 Return NULL_TREE if no simplification can be made. */
2741 gimple_fold_builtin_stpcpy (gimple_stmt_iterator
*gsi
)
2743 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
2744 location_t loc
= gimple_location (stmt
);
2745 tree dest
= gimple_call_arg (stmt
, 0);
2746 tree src
= gimple_call_arg (stmt
, 1);
2747 tree fn
, len
, lenp1
;
2749 /* If the result is unused, replace stpcpy with strcpy. */
2750 if (gimple_call_lhs (stmt
) == NULL_TREE
)
2752 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
2755 gimple_call_set_fndecl (stmt
, fn
);
2760 len
= c_strlen (src
, 1);
2762 || TREE_CODE (len
) != INTEGER_CST
)
2765 if (optimize_function_for_size_p (cfun
)
2766 /* If length is zero it's small enough. */
2767 && !integer_zerop (len
))
2770 /* If the source has a known length replace stpcpy with memcpy. */
2771 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
2775 gimple_seq stmts
= NULL
;
2776 tree tem
= gimple_convert (&stmts
, loc
, size_type_node
, len
);
2777 lenp1
= gimple_build (&stmts
, loc
, PLUS_EXPR
, size_type_node
,
2778 tem
, build_int_cst (size_type_node
, 1));
2779 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2780 gcall
*repl
= gimple_build_call (fn
, 3, dest
, src
, lenp1
);
2781 gimple_set_vuse (repl
, gimple_vuse (stmt
));
2782 gimple_set_vdef (repl
, gimple_vdef (stmt
));
2783 if (gimple_vdef (repl
)
2784 && TREE_CODE (gimple_vdef (repl
)) == SSA_NAME
)
2785 SSA_NAME_DEF_STMT (gimple_vdef (repl
)) = repl
;
2786 gsi_insert_before (gsi
, repl
, GSI_SAME_STMT
);
2787 /* Replace the result with dest + len. */
2789 tem
= gimple_convert (&stmts
, loc
, sizetype
, len
);
2790 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2791 gassign
*ret
= gimple_build_assign (gimple_call_lhs (stmt
),
2792 POINTER_PLUS_EXPR
, dest
, tem
);
2793 gsi_replace (gsi
, ret
, false);
2794 /* Finally fold the memcpy call. */
2795 gimple_stmt_iterator gsi2
= *gsi
;
2801 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
2802 NULL_TREE if a normal call should be emitted rather than expanding
2803 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
2804 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
2805 passed as second argument. */
2808 gimple_fold_builtin_snprintf_chk (gimple_stmt_iterator
*gsi
,
2809 enum built_in_function fcode
)
2811 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
2812 tree dest
, size
, len
, fn
, fmt
, flag
;
2813 const char *fmt_str
;
2815 /* Verify the required arguments in the original call. */
2816 if (gimple_call_num_args (stmt
) < 5)
2819 dest
= gimple_call_arg (stmt
, 0);
2820 len
= gimple_call_arg (stmt
, 1);
2821 flag
= gimple_call_arg (stmt
, 2);
2822 size
= gimple_call_arg (stmt
, 3);
2823 fmt
= gimple_call_arg (stmt
, 4);
2825 if (! tree_fits_uhwi_p (size
))
2828 if (! integer_all_onesp (size
))
2830 tree maxlen
= get_maxval_strlen (len
, 2);
2831 if (! 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
))
2842 if (tree_int_cst_lt (size
, maxlen
))
2846 if (!init_target_chars ())
2849 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
2850 or if format doesn't contain % chars or is "%s". */
2851 if (! integer_zerop (flag
))
2853 fmt_str
= c_getstr (fmt
);
2854 if (fmt_str
== NULL
)
2856 if (strchr (fmt_str
, target_percent
) != NULL
2857 && strcmp (fmt_str
, target_percent_s
))
2861 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
2863 fn
= builtin_decl_explicit (fcode
== BUILT_IN_VSNPRINTF_CHK
2864 ? BUILT_IN_VSNPRINTF
: BUILT_IN_SNPRINTF
);
2868 /* Replace the called function and the first 5 argument by 3 retaining
2869 trailing varargs. */
2870 gimple_call_set_fndecl (stmt
, fn
);
2871 gimple_call_set_fntype (stmt
, TREE_TYPE (fn
));
2872 gimple_call_set_arg (stmt
, 0, dest
);
2873 gimple_call_set_arg (stmt
, 1, len
);
2874 gimple_call_set_arg (stmt
, 2, fmt
);
2875 for (unsigned i
= 3; i
< gimple_call_num_args (stmt
) - 2; ++i
)
2876 gimple_call_set_arg (stmt
, i
, gimple_call_arg (stmt
, i
+ 2));
2877 gimple_set_num_ops (stmt
, gimple_num_ops (stmt
) - 2);
2882 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
2883 Return NULL_TREE if a normal call should be emitted rather than
2884 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
2885 or BUILT_IN_VSPRINTF_CHK. */
2888 gimple_fold_builtin_sprintf_chk (gimple_stmt_iterator
*gsi
,
2889 enum built_in_function fcode
)
2891 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
2892 tree dest
, size
, len
, fn
, fmt
, flag
;
2893 const char *fmt_str
;
2894 unsigned nargs
= gimple_call_num_args (stmt
);
2896 /* Verify the required arguments in the original call. */
2899 dest
= gimple_call_arg (stmt
, 0);
2900 flag
= gimple_call_arg (stmt
, 1);
2901 size
= gimple_call_arg (stmt
, 2);
2902 fmt
= gimple_call_arg (stmt
, 3);
2904 if (! tree_fits_uhwi_p (size
))
2909 if (!init_target_chars ())
2912 /* Check whether the format is a literal string constant. */
2913 fmt_str
= c_getstr (fmt
);
2914 if (fmt_str
!= NULL
)
2916 /* If the format doesn't contain % args or %%, we know the size. */
2917 if (strchr (fmt_str
, target_percent
) == 0)
2919 if (fcode
!= BUILT_IN_SPRINTF_CHK
|| nargs
== 4)
2920 len
= build_int_cstu (size_type_node
, strlen (fmt_str
));
2922 /* If the format is "%s" and first ... argument is a string literal,
2923 we know the size too. */
2924 else if (fcode
== BUILT_IN_SPRINTF_CHK
2925 && strcmp (fmt_str
, target_percent_s
) == 0)
2931 arg
= gimple_call_arg (stmt
, 4);
2932 if (POINTER_TYPE_P (TREE_TYPE (arg
)))
2934 len
= c_strlen (arg
, 1);
2935 if (! len
|| ! tree_fits_uhwi_p (len
))
2942 if (! integer_all_onesp (size
))
2944 if (! len
|| ! tree_int_cst_lt (len
, size
))
2948 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
2949 or if format doesn't contain % chars or is "%s". */
2950 if (! integer_zerop (flag
))
2952 if (fmt_str
== NULL
)
2954 if (strchr (fmt_str
, target_percent
) != NULL
2955 && strcmp (fmt_str
, target_percent_s
))
2959 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
2960 fn
= builtin_decl_explicit (fcode
== BUILT_IN_VSPRINTF_CHK
2961 ? BUILT_IN_VSPRINTF
: BUILT_IN_SPRINTF
);
2965 /* Replace the called function and the first 4 argument by 2 retaining
2966 trailing varargs. */
2967 gimple_call_set_fndecl (stmt
, fn
);
2968 gimple_call_set_fntype (stmt
, TREE_TYPE (fn
));
2969 gimple_call_set_arg (stmt
, 0, dest
);
2970 gimple_call_set_arg (stmt
, 1, fmt
);
2971 for (unsigned i
= 2; i
< gimple_call_num_args (stmt
) - 2; ++i
)
2972 gimple_call_set_arg (stmt
, i
, gimple_call_arg (stmt
, i
+ 2));
2973 gimple_set_num_ops (stmt
, gimple_num_ops (stmt
) - 2);
2978 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
2979 ORIG may be null if this is a 2-argument call. We don't attempt to
2980 simplify calls with more than 3 arguments.
2982 Return true if simplification was possible, otherwise false. */
2985 gimple_fold_builtin_sprintf (gimple_stmt_iterator
*gsi
)
2987 gimple
*stmt
= gsi_stmt (*gsi
);
2988 tree dest
= gimple_call_arg (stmt
, 0);
2989 tree fmt
= gimple_call_arg (stmt
, 1);
2990 tree orig
= NULL_TREE
;
2991 const char *fmt_str
= NULL
;
2993 /* Verify the required arguments in the original call. We deal with two
2994 types of sprintf() calls: 'sprintf (str, fmt)' and
2995 'sprintf (dest, "%s", orig)'. */
2996 if (gimple_call_num_args (stmt
) > 3)
2999 if (gimple_call_num_args (stmt
) == 3)
3000 orig
= gimple_call_arg (stmt
, 2);
3002 /* Check whether the format is a literal string constant. */
3003 fmt_str
= c_getstr (fmt
);
3004 if (fmt_str
== NULL
)
3007 if (!init_target_chars ())
3010 /* If the format doesn't contain % args or %%, use strcpy. */
3011 if (strchr (fmt_str
, target_percent
) == NULL
)
3013 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3018 /* Don't optimize sprintf (buf, "abc", ptr++). */
3022 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
3023 'format' is known to contain no % formats. */
3024 gimple_seq stmts
= NULL
;
3025 gimple
*repl
= gimple_build_call (fn
, 2, dest
, fmt
);
3026 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3027 if (gimple_call_lhs (stmt
))
3029 repl
= gimple_build_assign (gimple_call_lhs (stmt
),
3030 build_int_cst (integer_type_node
,
3032 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3033 gsi_replace_with_seq_vops (gsi
, stmts
);
3034 /* gsi now points at the assignment to the lhs, get a
3035 stmt iterator to the memcpy call.
3036 ??? We can't use gsi_for_stmt as that doesn't work when the
3037 CFG isn't built yet. */
3038 gimple_stmt_iterator gsi2
= *gsi
;
3044 gsi_replace_with_seq_vops (gsi
, stmts
);
3050 /* If the format is "%s", use strcpy if the result isn't used. */
3051 else if (fmt_str
&& strcmp (fmt_str
, target_percent_s
) == 0)
3054 fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3059 /* Don't crash on sprintf (str1, "%s"). */
3063 tree orig_len
= NULL_TREE
;
3064 if (gimple_call_lhs (stmt
))
3066 orig_len
= get_maxval_strlen (orig
, 0);
3071 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
3072 gimple_seq stmts
= NULL
;
3073 gimple
*repl
= gimple_build_call (fn
, 2, dest
, orig
);
3074 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3075 if (gimple_call_lhs (stmt
))
3077 if (!useless_type_conversion_p (integer_type_node
,
3078 TREE_TYPE (orig_len
)))
3079 orig_len
= fold_convert (integer_type_node
, orig_len
);
3080 repl
= gimple_build_assign (gimple_call_lhs (stmt
), orig_len
);
3081 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3082 gsi_replace_with_seq_vops (gsi
, stmts
);
3083 /* gsi now points at the assignment to the lhs, get a
3084 stmt iterator to the memcpy call.
3085 ??? We can't use gsi_for_stmt as that doesn't work when the
3086 CFG isn't built yet. */
3087 gimple_stmt_iterator gsi2
= *gsi
;
3093 gsi_replace_with_seq_vops (gsi
, stmts
);
3101 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
3102 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
3103 attempt to simplify calls with more than 4 arguments.
3105 Return true if simplification was possible, otherwise false. */
3108 gimple_fold_builtin_snprintf (gimple_stmt_iterator
*gsi
)
3110 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3111 tree dest
= gimple_call_arg (stmt
, 0);
3112 tree destsize
= gimple_call_arg (stmt
, 1);
3113 tree fmt
= gimple_call_arg (stmt
, 2);
3114 tree orig
= NULL_TREE
;
3115 const char *fmt_str
= NULL
;
3117 if (gimple_call_num_args (stmt
) > 4)
3120 if (gimple_call_num_args (stmt
) == 4)
3121 orig
= gimple_call_arg (stmt
, 3);
3123 if (!tree_fits_uhwi_p (destsize
))
3125 unsigned HOST_WIDE_INT destlen
= tree_to_uhwi (destsize
);
3127 /* Check whether the format is a literal string constant. */
3128 fmt_str
= c_getstr (fmt
);
3129 if (fmt_str
== NULL
)
3132 if (!init_target_chars ())
3135 /* If the format doesn't contain % args or %%, use strcpy. */
3136 if (strchr (fmt_str
, target_percent
) == NULL
)
3138 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3142 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
3146 /* We could expand this as
3147 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
3149 memcpy (str, fmt_with_nul_at_cstm1, cst);
3150 but in the former case that might increase code size
3151 and in the latter case grow .rodata section too much.
3153 size_t len
= strlen (fmt_str
);
3157 gimple_seq stmts
= NULL
;
3158 gimple
*repl
= gimple_build_call (fn
, 2, dest
, fmt
);
3159 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3160 if (gimple_call_lhs (stmt
))
3162 repl
= gimple_build_assign (gimple_call_lhs (stmt
),
3163 build_int_cst (integer_type_node
, len
));
3164 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3165 gsi_replace_with_seq_vops (gsi
, stmts
);
3166 /* gsi now points at the assignment to the lhs, get a
3167 stmt iterator to the memcpy call.
3168 ??? We can't use gsi_for_stmt as that doesn't work when the
3169 CFG isn't built yet. */
3170 gimple_stmt_iterator gsi2
= *gsi
;
3176 gsi_replace_with_seq_vops (gsi
, stmts
);
3182 /* If the format is "%s", use strcpy if the result isn't used. */
3183 else if (fmt_str
&& strcmp (fmt_str
, target_percent_s
) == 0)
3185 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3189 /* Don't crash on snprintf (str1, cst, "%s"). */
3193 tree orig_len
= get_maxval_strlen (orig
, 0);
3194 if (!orig_len
|| TREE_CODE (orig_len
) != INTEGER_CST
)
3197 /* We could expand this as
3198 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
3200 memcpy (str1, str2_with_nul_at_cstm1, cst);
3201 but in the former case that might increase code size
3202 and in the latter case grow .rodata section too much.
3204 if (compare_tree_int (orig_len
, destlen
) >= 0)
3207 /* Convert snprintf (str1, cst, "%s", str2) into
3208 strcpy (str1, str2) if strlen (str2) < cst. */
3209 gimple_seq stmts
= NULL
;
3210 gimple
*repl
= gimple_build_call (fn
, 2, dest
, orig
);
3211 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3212 if (gimple_call_lhs (stmt
))
3214 if (!useless_type_conversion_p (integer_type_node
,
3215 TREE_TYPE (orig_len
)))
3216 orig_len
= fold_convert (integer_type_node
, orig_len
);
3217 repl
= gimple_build_assign (gimple_call_lhs (stmt
), orig_len
);
3218 gimple_seq_add_stmt_without_update (&stmts
, repl
);
3219 gsi_replace_with_seq_vops (gsi
, stmts
);
3220 /* gsi now points at the assignment to the lhs, get a
3221 stmt iterator to the memcpy call.
3222 ??? We can't use gsi_for_stmt as that doesn't work when the
3223 CFG isn't built yet. */
3224 gimple_stmt_iterator gsi2
= *gsi
;
3230 gsi_replace_with_seq_vops (gsi
, stmts
);
3238 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
3239 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
3240 more than 3 arguments, and ARG may be null in the 2-argument case.
3242 Return NULL_TREE if no simplification was possible, otherwise return the
3243 simplified form of the call as a tree. FCODE is the BUILT_IN_*
3244 code of the function to be simplified. */
3247 gimple_fold_builtin_fprintf (gimple_stmt_iterator
*gsi
,
3248 tree fp
, tree fmt
, tree arg
,
3249 enum built_in_function fcode
)
3251 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3252 tree fn_fputc
, fn_fputs
;
3253 const char *fmt_str
= NULL
;
3255 /* If the return value is used, don't do the transformation. */
3256 if (gimple_call_lhs (stmt
) != NULL_TREE
)
3259 /* Check whether the format is a literal string constant. */
3260 fmt_str
= c_getstr (fmt
);
3261 if (fmt_str
== NULL
)
3264 if (fcode
== BUILT_IN_FPRINTF_UNLOCKED
)
3266 /* If we're using an unlocked function, assume the other
3267 unlocked functions exist explicitly. */
3268 fn_fputc
= builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED
);
3269 fn_fputs
= builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED
);
3273 fn_fputc
= builtin_decl_implicit (BUILT_IN_FPUTC
);
3274 fn_fputs
= builtin_decl_implicit (BUILT_IN_FPUTS
);
3277 if (!init_target_chars ())
3280 /* If the format doesn't contain % args or %%, use strcpy. */
3281 if (strchr (fmt_str
, target_percent
) == NULL
)
3283 if (fcode
!= BUILT_IN_VFPRINTF
&& fcode
!= BUILT_IN_VFPRINTF_CHK
3287 /* If the format specifier was "", fprintf does nothing. */
3288 if (fmt_str
[0] == '\0')
3290 replace_call_with_value (gsi
, NULL_TREE
);
3294 /* When "string" doesn't contain %, replace all cases of
3295 fprintf (fp, string) with fputs (string, fp). The fputs
3296 builtin will take care of special cases like length == 1. */
3299 gcall
*repl
= gimple_build_call (fn_fputs
, 2, fmt
, fp
);
3300 replace_call_with_call_and_fold (gsi
, repl
);
3305 /* The other optimizations can be done only on the non-va_list variants. */
3306 else if (fcode
== BUILT_IN_VFPRINTF
|| fcode
== BUILT_IN_VFPRINTF_CHK
)
3309 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
3310 else if (strcmp (fmt_str
, target_percent_s
) == 0)
3312 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
3316 gcall
*repl
= gimple_build_call (fn_fputs
, 2, arg
, fp
);
3317 replace_call_with_call_and_fold (gsi
, repl
);
3322 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
3323 else if (strcmp (fmt_str
, target_percent_c
) == 0)
3326 || ! useless_type_conversion_p (integer_type_node
, TREE_TYPE (arg
)))
3330 gcall
*repl
= gimple_build_call (fn_fputc
, 2, arg
, fp
);
3331 replace_call_with_call_and_fold (gsi
, repl
);
3339 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
3340 FMT and ARG are the arguments to the call; we don't fold cases with
3341 more than 2 arguments, and ARG may be null if this is a 1-argument case.
3343 Return NULL_TREE if no simplification was possible, otherwise return the
3344 simplified form of the call as a tree. FCODE is the BUILT_IN_*
3345 code of the function to be simplified. */
3348 gimple_fold_builtin_printf (gimple_stmt_iterator
*gsi
, tree fmt
,
3349 tree arg
, enum built_in_function fcode
)
3351 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3352 tree fn_putchar
, fn_puts
, newarg
;
3353 const char *fmt_str
= NULL
;
3355 /* If the return value is used, don't do the transformation. */
3356 if (gimple_call_lhs (stmt
) != NULL_TREE
)
3359 /* Check whether the format is a literal string constant. */
3360 fmt_str
= c_getstr (fmt
);
3361 if (fmt_str
== NULL
)
3364 if (fcode
== BUILT_IN_PRINTF_UNLOCKED
)
3366 /* If we're using an unlocked function, assume the other
3367 unlocked functions exist explicitly. */
3368 fn_putchar
= builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED
);
3369 fn_puts
= builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED
);
3373 fn_putchar
= builtin_decl_implicit (BUILT_IN_PUTCHAR
);
3374 fn_puts
= builtin_decl_implicit (BUILT_IN_PUTS
);
3377 if (!init_target_chars ())
3380 if (strcmp (fmt_str
, target_percent_s
) == 0
3381 || strchr (fmt_str
, target_percent
) == NULL
)
3385 if (strcmp (fmt_str
, target_percent_s
) == 0)
3387 if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
3390 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
3393 str
= c_getstr (arg
);
3399 /* The format specifier doesn't contain any '%' characters. */
3400 if (fcode
!= BUILT_IN_VPRINTF
&& fcode
!= BUILT_IN_VPRINTF_CHK
3406 /* If the string was "", printf does nothing. */
3409 replace_call_with_value (gsi
, NULL_TREE
);
3413 /* If the string has length of 1, call putchar. */
3416 /* Given printf("c"), (where c is any one character,)
3417 convert "c"[0] to an int and pass that to the replacement
3419 newarg
= build_int_cst (integer_type_node
, str
[0]);
3422 gcall
*repl
= gimple_build_call (fn_putchar
, 1, newarg
);
3423 replace_call_with_call_and_fold (gsi
, repl
);
3429 /* If the string was "string\n", call puts("string"). */
3430 size_t len
= strlen (str
);
3431 if ((unsigned char)str
[len
- 1] == target_newline
3432 && (size_t) (int) len
== len
3436 tree offset_node
, string_cst
;
3438 /* Create a NUL-terminated string that's one char shorter
3439 than the original, stripping off the trailing '\n'. */
3440 newarg
= build_string_literal (len
, str
);
3441 string_cst
= string_constant (newarg
, &offset_node
);
3442 gcc_checking_assert (string_cst
3443 && (TREE_STRING_LENGTH (string_cst
)
3445 && integer_zerop (offset_node
)
3447 TREE_STRING_POINTER (string_cst
)[len
- 1]
3449 /* build_string_literal creates a new STRING_CST,
3450 modify it in place to avoid double copying. */
3451 newstr
= CONST_CAST (char *, TREE_STRING_POINTER (string_cst
));
3452 newstr
[len
- 1] = '\0';
3455 gcall
*repl
= gimple_build_call (fn_puts
, 1, newarg
);
3456 replace_call_with_call_and_fold (gsi
, repl
);
3461 /* We'd like to arrange to call fputs(string,stdout) here,
3462 but we need stdout and don't have a way to get it yet. */
3467 /* The other optimizations can be done only on the non-va_list variants. */
3468 else if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
3471 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
3472 else if (strcmp (fmt_str
, target_percent_s_newline
) == 0)
3474 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
3478 gcall
*repl
= gimple_build_call (fn_puts
, 1, arg
);
3479 replace_call_with_call_and_fold (gsi
, repl
);
3484 /* If the format specifier was "%c", call __builtin_putchar(arg). */
3485 else if (strcmp (fmt_str
, target_percent_c
) == 0)
3487 if (!arg
|| ! useless_type_conversion_p (integer_type_node
,
3492 gcall
*repl
= gimple_build_call (fn_putchar
, 1, arg
);
3493 replace_call_with_call_and_fold (gsi
, repl
);
3503 /* Fold a call to __builtin_strlen with known length LEN. */
3506 gimple_fold_builtin_strlen (gimple_stmt_iterator
*gsi
)
3508 gimple
*stmt
= gsi_stmt (*gsi
);
3514 if (!get_range_strlen (gimple_call_arg (stmt
, 0), lenrange
, true)
3515 && lenrange
[0] && TREE_CODE (lenrange
[0]) == INTEGER_CST
3516 && lenrange
[1] && TREE_CODE (lenrange
[1]) == INTEGER_CST
)
3518 /* The range of lengths refers to either a single constant
3519 string or to the longest and shortest constant string
3520 referenced by the argument of the strlen() call, or to
3521 the strings that can possibly be stored in the arrays
3522 the argument refers to. */
3523 minlen
= wi::to_wide (lenrange
[0]);
3524 maxlen
= wi::to_wide (lenrange
[1]);
3528 unsigned prec
= TYPE_PRECISION (sizetype
);
3530 minlen
= wi::shwi (0, prec
);
3531 maxlen
= wi::to_wide (max_object_size (), prec
) - 2;
3534 if (minlen
== maxlen
)
3536 lenrange
[0] = force_gimple_operand_gsi (gsi
, lenrange
[0], true, NULL
,
3537 true, GSI_SAME_STMT
);
3538 replace_call_with_value (gsi
, lenrange
[0]);
3542 if (tree lhs
= gimple_call_lhs (stmt
))
3543 if (TREE_CODE (lhs
) == SSA_NAME
3544 && INTEGRAL_TYPE_P (TREE_TYPE (lhs
)))
3545 set_range_info (lhs
, VR_RANGE
, minlen
, maxlen
);
3550 /* Fold a call to __builtin_acc_on_device. */
3553 gimple_fold_builtin_acc_on_device (gimple_stmt_iterator
*gsi
, tree arg0
)
3555 /* Defer folding until we know which compiler we're in. */
3556 if (symtab
->state
!= EXPANSION
)
3559 unsigned val_host
= GOMP_DEVICE_HOST
;
3560 unsigned val_dev
= GOMP_DEVICE_NONE
;
3562 #ifdef ACCEL_COMPILER
3563 val_host
= GOMP_DEVICE_NOT_HOST
;
3564 val_dev
= ACCEL_COMPILER_acc_device
;
3567 location_t loc
= gimple_location (gsi_stmt (*gsi
));
3569 tree host_eq
= make_ssa_name (boolean_type_node
);
3570 gimple
*host_ass
= gimple_build_assign
3571 (host_eq
, EQ_EXPR
, arg0
, build_int_cst (TREE_TYPE (arg0
), val_host
));
3572 gimple_set_location (host_ass
, loc
);
3573 gsi_insert_before (gsi
, host_ass
, GSI_SAME_STMT
);
3575 tree dev_eq
= make_ssa_name (boolean_type_node
);
3576 gimple
*dev_ass
= gimple_build_assign
3577 (dev_eq
, EQ_EXPR
, arg0
, build_int_cst (TREE_TYPE (arg0
), val_dev
));
3578 gimple_set_location (dev_ass
, loc
);
3579 gsi_insert_before (gsi
, dev_ass
, GSI_SAME_STMT
);
3581 tree result
= make_ssa_name (boolean_type_node
);
3582 gimple
*result_ass
= gimple_build_assign
3583 (result
, BIT_IOR_EXPR
, host_eq
, dev_eq
);
3584 gimple_set_location (result_ass
, loc
);
3585 gsi_insert_before (gsi
, result_ass
, GSI_SAME_STMT
);
3587 replace_call_with_value (gsi
, result
);
3592 /* Fold realloc (0, n) -> malloc (n). */
3595 gimple_fold_builtin_realloc (gimple_stmt_iterator
*gsi
)
3597 gimple
*stmt
= gsi_stmt (*gsi
);
3598 tree arg
= gimple_call_arg (stmt
, 0);
3599 tree size
= gimple_call_arg (stmt
, 1);
3601 if (operand_equal_p (arg
, null_pointer_node
, 0))
3603 tree fn_malloc
= builtin_decl_implicit (BUILT_IN_MALLOC
);
3606 gcall
*repl
= gimple_build_call (fn_malloc
, 1, size
);
3607 replace_call_with_call_and_fold (gsi
, repl
);
3614 /* Fold the non-target builtin at *GSI and return whether any simplification
3618 gimple_fold_builtin (gimple_stmt_iterator
*gsi
)
3620 gcall
*stmt
= as_a
<gcall
*>(gsi_stmt (*gsi
));
3621 tree callee
= gimple_call_fndecl (stmt
);
3623 /* Give up for always_inline inline builtins until they are
3625 if (avoid_folding_inline_builtin (callee
))
3628 unsigned n
= gimple_call_num_args (stmt
);
3629 enum built_in_function fcode
= DECL_FUNCTION_CODE (callee
);
3633 return gimple_fold_builtin_bcmp (gsi
);
3634 case BUILT_IN_BCOPY
:
3635 return gimple_fold_builtin_bcopy (gsi
);
3636 case BUILT_IN_BZERO
:
3637 return gimple_fold_builtin_bzero (gsi
);
3639 case BUILT_IN_MEMSET
:
3640 return gimple_fold_builtin_memset (gsi
,
3641 gimple_call_arg (stmt
, 1),
3642 gimple_call_arg (stmt
, 2));
3643 case BUILT_IN_MEMCPY
:
3644 return gimple_fold_builtin_memory_op (gsi
, gimple_call_arg (stmt
, 0),
3645 gimple_call_arg (stmt
, 1), 0);
3646 case BUILT_IN_MEMPCPY
:
3647 return gimple_fold_builtin_memory_op (gsi
, gimple_call_arg (stmt
, 0),
3648 gimple_call_arg (stmt
, 1), 1);
3649 case BUILT_IN_MEMMOVE
:
3650 return gimple_fold_builtin_memory_op (gsi
, gimple_call_arg (stmt
, 0),
3651 gimple_call_arg (stmt
, 1), 3);
3652 case BUILT_IN_SPRINTF_CHK
:
3653 case BUILT_IN_VSPRINTF_CHK
:
3654 return gimple_fold_builtin_sprintf_chk (gsi
, fcode
);
3655 case BUILT_IN_STRCAT_CHK
:
3656 return gimple_fold_builtin_strcat_chk (gsi
);
3657 case BUILT_IN_STRNCAT_CHK
:
3658 return gimple_fold_builtin_strncat_chk (gsi
);
3659 case BUILT_IN_STRLEN
:
3660 return gimple_fold_builtin_strlen (gsi
);
3661 case BUILT_IN_STRCPY
:
3662 return gimple_fold_builtin_strcpy (gsi
,
3663 gimple_call_arg (stmt
, 0),
3664 gimple_call_arg (stmt
, 1));
3665 case BUILT_IN_STRNCPY
:
3666 return gimple_fold_builtin_strncpy (gsi
,
3667 gimple_call_arg (stmt
, 0),
3668 gimple_call_arg (stmt
, 1),
3669 gimple_call_arg (stmt
, 2));
3670 case BUILT_IN_STRCAT
:
3671 return gimple_fold_builtin_strcat (gsi
, gimple_call_arg (stmt
, 0),
3672 gimple_call_arg (stmt
, 1));
3673 case BUILT_IN_STRNCAT
:
3674 return gimple_fold_builtin_strncat (gsi
);
3675 case BUILT_IN_INDEX
:
3676 case BUILT_IN_STRCHR
:
3677 return gimple_fold_builtin_strchr (gsi
, false);
3678 case BUILT_IN_RINDEX
:
3679 case BUILT_IN_STRRCHR
:
3680 return gimple_fold_builtin_strchr (gsi
, true);
3681 case BUILT_IN_STRSTR
:
3682 return gimple_fold_builtin_strstr (gsi
);
3683 case BUILT_IN_STRCMP
:
3684 case BUILT_IN_STRCMP_EQ
:
3685 case BUILT_IN_STRCASECMP
:
3686 case BUILT_IN_STRNCMP
:
3687 case BUILT_IN_STRNCMP_EQ
:
3688 case BUILT_IN_STRNCASECMP
:
3689 return gimple_fold_builtin_string_compare (gsi
);
3690 case BUILT_IN_MEMCHR
:
3691 return gimple_fold_builtin_memchr (gsi
);
3692 case BUILT_IN_FPUTS
:
3693 return gimple_fold_builtin_fputs (gsi
, gimple_call_arg (stmt
, 0),
3694 gimple_call_arg (stmt
, 1), false);
3695 case BUILT_IN_FPUTS_UNLOCKED
:
3696 return gimple_fold_builtin_fputs (gsi
, gimple_call_arg (stmt
, 0),
3697 gimple_call_arg (stmt
, 1), true);
3698 case BUILT_IN_MEMCPY_CHK
:
3699 case BUILT_IN_MEMPCPY_CHK
:
3700 case BUILT_IN_MEMMOVE_CHK
:
3701 case BUILT_IN_MEMSET_CHK
:
3702 return gimple_fold_builtin_memory_chk (gsi
,
3703 gimple_call_arg (stmt
, 0),
3704 gimple_call_arg (stmt
, 1),
3705 gimple_call_arg (stmt
, 2),
3706 gimple_call_arg (stmt
, 3),
3708 case BUILT_IN_STPCPY
:
3709 return gimple_fold_builtin_stpcpy (gsi
);
3710 case BUILT_IN_STRCPY_CHK
:
3711 case BUILT_IN_STPCPY_CHK
:
3712 return gimple_fold_builtin_stxcpy_chk (gsi
,
3713 gimple_call_arg (stmt
, 0),
3714 gimple_call_arg (stmt
, 1),
3715 gimple_call_arg (stmt
, 2),
3717 case BUILT_IN_STRNCPY_CHK
:
3718 case BUILT_IN_STPNCPY_CHK
:
3719 return gimple_fold_builtin_stxncpy_chk (gsi
,
3720 gimple_call_arg (stmt
, 0),
3721 gimple_call_arg (stmt
, 1),
3722 gimple_call_arg (stmt
, 2),
3723 gimple_call_arg (stmt
, 3),
3725 case BUILT_IN_SNPRINTF_CHK
:
3726 case BUILT_IN_VSNPRINTF_CHK
:
3727 return gimple_fold_builtin_snprintf_chk (gsi
, fcode
);
3729 case BUILT_IN_FPRINTF
:
3730 case BUILT_IN_FPRINTF_UNLOCKED
:
3731 case BUILT_IN_VFPRINTF
:
3732 if (n
== 2 || n
== 3)
3733 return gimple_fold_builtin_fprintf (gsi
,
3734 gimple_call_arg (stmt
, 0),
3735 gimple_call_arg (stmt
, 1),
3737 ? gimple_call_arg (stmt
, 2)
3741 case BUILT_IN_FPRINTF_CHK
:
3742 case BUILT_IN_VFPRINTF_CHK
:
3743 if (n
== 3 || n
== 4)
3744 return gimple_fold_builtin_fprintf (gsi
,
3745 gimple_call_arg (stmt
, 0),
3746 gimple_call_arg (stmt
, 2),
3748 ? gimple_call_arg (stmt
, 3)
3752 case BUILT_IN_PRINTF
:
3753 case BUILT_IN_PRINTF_UNLOCKED
:
3754 case BUILT_IN_VPRINTF
:
3755 if (n
== 1 || n
== 2)
3756 return gimple_fold_builtin_printf (gsi
, gimple_call_arg (stmt
, 0),
3758 ? gimple_call_arg (stmt
, 1)
3759 : NULL_TREE
, fcode
);
3761 case BUILT_IN_PRINTF_CHK
:
3762 case BUILT_IN_VPRINTF_CHK
:
3763 if (n
== 2 || n
== 3)
3764 return gimple_fold_builtin_printf (gsi
, gimple_call_arg (stmt
, 1),
3766 ? gimple_call_arg (stmt
, 2)
3767 : NULL_TREE
, fcode
);
3769 case BUILT_IN_ACC_ON_DEVICE
:
3770 return gimple_fold_builtin_acc_on_device (gsi
,
3771 gimple_call_arg (stmt
, 0));
3772 case BUILT_IN_REALLOC
:
3773 return gimple_fold_builtin_realloc (gsi
);
3778 /* Try the generic builtin folder. */
3779 bool ignore
= (gimple_call_lhs (stmt
) == NULL
);
3780 tree result
= fold_call_stmt (stmt
, ignore
);
3784 STRIP_NOPS (result
);
3786 result
= fold_convert (gimple_call_return_type (stmt
), result
);
3787 if (!update_call_from_tree (gsi
, result
))
3788 gimplify_and_update_call_from_tree (gsi
, result
);
3795 /* Transform IFN_GOACC_DIM_SIZE and IFN_GOACC_DIM_POS internal
3796 function calls to constants, where possible. */
3799 fold_internal_goacc_dim (const gimple
*call
)
3801 int axis
= oacc_get_ifn_dim_arg (call
);
3802 int size
= oacc_get_fn_dim_size (current_function_decl
, axis
);
3803 tree result
= NULL_TREE
;
3804 tree type
= TREE_TYPE (gimple_call_lhs (call
));
3806 switch (gimple_call_internal_fn (call
))
3808 case IFN_GOACC_DIM_POS
:
3809 /* If the size is 1, we know the answer. */
3811 result
= build_int_cst (type
, 0);
3813 case IFN_GOACC_DIM_SIZE
:
3814 /* If the size is not dynamic, we know the answer. */
3816 result
= build_int_cst (type
, size
);
3825 /* Return true if stmt is __atomic_compare_exchange_N call which is suitable
3826 for conversion into ATOMIC_COMPARE_EXCHANGE if the second argument is
3827 &var where var is only addressable because of such calls. */
3830 optimize_atomic_compare_exchange_p (gimple
*stmt
)
3832 if (gimple_call_num_args (stmt
) != 6
3833 || !flag_inline_atomics
3835 || sanitize_flags_p (SANITIZE_THREAD
| SANITIZE_ADDRESS
)
3836 || !gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
)
3837 || !gimple_vdef (stmt
)
3838 || !gimple_vuse (stmt
))
3841 tree fndecl
= gimple_call_fndecl (stmt
);
3842 switch (DECL_FUNCTION_CODE (fndecl
))
3844 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
:
3845 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2
:
3846 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4
:
3847 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8
:
3848 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16
:
3854 tree expected
= gimple_call_arg (stmt
, 1);
3855 if (TREE_CODE (expected
) != ADDR_EXPR
3856 || !SSA_VAR_P (TREE_OPERAND (expected
, 0)))
3859 tree etype
= TREE_TYPE (TREE_OPERAND (expected
, 0));
3860 if (!is_gimple_reg_type (etype
)
3861 || !auto_var_in_fn_p (TREE_OPERAND (expected
, 0), current_function_decl
)
3862 || TREE_THIS_VOLATILE (etype
)
3863 || VECTOR_TYPE_P (etype
)
3864 || TREE_CODE (etype
) == COMPLEX_TYPE
3865 /* Don't optimize floating point expected vars, VIEW_CONVERT_EXPRs
3866 might not preserve all the bits. See PR71716. */
3867 || SCALAR_FLOAT_TYPE_P (etype
)
3868 || maybe_ne (TYPE_PRECISION (etype
),
3869 GET_MODE_BITSIZE (TYPE_MODE (etype
))))
3872 tree weak
= gimple_call_arg (stmt
, 3);
3873 if (!integer_zerop (weak
) && !integer_onep (weak
))
3876 tree parmt
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
3877 tree itype
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt
)));
3878 machine_mode mode
= TYPE_MODE (itype
);
3880 if (direct_optab_handler (atomic_compare_and_swap_optab
, mode
)
3882 && optab_handler (sync_compare_and_swap_optab
, mode
) == CODE_FOR_nothing
)
3885 if (maybe_ne (int_size_in_bytes (etype
), GET_MODE_SIZE (mode
)))
3892 r = __atomic_compare_exchange_N (p, &e, d, w, s, f);
3894 _Complex uintN_t t = ATOMIC_COMPARE_EXCHANGE (p, e, d, w * 256 + N, s, f);
3895 i = IMAGPART_EXPR <t>;
3897 e = REALPART_EXPR <t>; */
3900 fold_builtin_atomic_compare_exchange (gimple_stmt_iterator
*gsi
)
3902 gimple
*stmt
= gsi_stmt (*gsi
);
3903 tree fndecl
= gimple_call_fndecl (stmt
);
3904 tree parmt
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
3905 tree itype
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt
)));
3906 tree ctype
= build_complex_type (itype
);
3907 tree expected
= TREE_OPERAND (gimple_call_arg (stmt
, 1), 0);
3908 bool throws
= false;
3910 gimple
*g
= gimple_build_assign (make_ssa_name (TREE_TYPE (expected
)),
3912 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
3913 gimple_stmt_iterator gsiret
= gsi_for_stmt (g
);
3914 if (!useless_type_conversion_p (itype
, TREE_TYPE (expected
)))
3916 g
= gimple_build_assign (make_ssa_name (itype
), VIEW_CONVERT_EXPR
,
3917 build1 (VIEW_CONVERT_EXPR
, itype
,
3918 gimple_assign_lhs (g
)));
3919 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
3921 int flag
= (integer_onep (gimple_call_arg (stmt
, 3)) ? 256 : 0)
3922 + int_size_in_bytes (itype
);
3923 g
= gimple_build_call_internal (IFN_ATOMIC_COMPARE_EXCHANGE
, 6,
3924 gimple_call_arg (stmt
, 0),
3925 gimple_assign_lhs (g
),
3926 gimple_call_arg (stmt
, 2),
3927 build_int_cst (integer_type_node
, flag
),
3928 gimple_call_arg (stmt
, 4),
3929 gimple_call_arg (stmt
, 5));
3930 tree lhs
= make_ssa_name (ctype
);
3931 gimple_call_set_lhs (g
, lhs
);
3932 gimple_set_vdef (g
, gimple_vdef (stmt
));
3933 gimple_set_vuse (g
, gimple_vuse (stmt
));
3934 SSA_NAME_DEF_STMT (gimple_vdef (g
)) = g
;
3935 tree oldlhs
= gimple_call_lhs (stmt
);
3936 if (stmt_can_throw_internal (stmt
))
3939 e
= find_fallthru_edge (gsi_bb (*gsi
)->succs
);
3941 gimple_call_set_nothrow (as_a
<gcall
*> (g
),
3942 gimple_call_nothrow_p (as_a
<gcall
*> (stmt
)));
3943 gimple_call_set_lhs (stmt
, NULL_TREE
);
3944 gsi_replace (gsi
, g
, true);
3947 g
= gimple_build_assign (make_ssa_name (itype
), IMAGPART_EXPR
,
3948 build1 (IMAGPART_EXPR
, itype
, lhs
));
3951 gsi_insert_on_edge_immediate (e
, g
);
3952 *gsi
= gsi_for_stmt (g
);
3955 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
3956 g
= gimple_build_assign (oldlhs
, NOP_EXPR
, gimple_assign_lhs (g
));
3957 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
3959 g
= gimple_build_assign (make_ssa_name (itype
), REALPART_EXPR
,
3960 build1 (REALPART_EXPR
, itype
, lhs
));
3961 if (throws
&& oldlhs
== NULL_TREE
)
3963 gsi_insert_on_edge_immediate (e
, g
);
3964 *gsi
= gsi_for_stmt (g
);
3967 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
3968 if (!useless_type_conversion_p (TREE_TYPE (expected
), itype
))
3970 g
= gimple_build_assign (make_ssa_name (TREE_TYPE (expected
)),
3972 build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (expected
),
3973 gimple_assign_lhs (g
)));
3974 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
3976 g
= gimple_build_assign (expected
, SSA_NAME
, gimple_assign_lhs (g
));
3977 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
3981 /* Return true if ARG0 CODE ARG1 in infinite signed precision operation
3982 doesn't fit into TYPE. The test for overflow should be regardless of
3983 -fwrapv, and even for unsigned types. */
3986 arith_overflowed_p (enum tree_code code
, const_tree type
,
3987 const_tree arg0
, const_tree arg1
)
3989 widest2_int warg0
= widest2_int_cst (arg0
);
3990 widest2_int warg1
= widest2_int_cst (arg1
);
3994 case PLUS_EXPR
: wres
= wi::add (warg0
, warg1
); break;
3995 case MINUS_EXPR
: wres
= wi::sub (warg0
, warg1
); break;
3996 case MULT_EXPR
: wres
= wi::mul (warg0
, warg1
); break;
3997 default: gcc_unreachable ();
3999 signop sign
= TYPE_SIGN (type
);
4000 if (sign
== UNSIGNED
&& wi::neg_p (wres
))
4002 return wi::min_precision (wres
, sign
) > TYPE_PRECISION (type
);
4005 /* Attempt to fold a call statement referenced by the statement iterator GSI.
4006 The statement may be replaced by another statement, e.g., if the call
4007 simplifies to a constant value. Return true if any changes were made.
4008 It is assumed that the operands have been previously folded. */
4011 gimple_fold_call (gimple_stmt_iterator
*gsi
, bool inplace
)
4013 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
4015 bool changed
= false;
4018 /* Fold *& in call arguments. */
4019 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
4020 if (REFERENCE_CLASS_P (gimple_call_arg (stmt
, i
)))
4022 tree tmp
= maybe_fold_reference (gimple_call_arg (stmt
, i
), false);
4025 gimple_call_set_arg (stmt
, i
, tmp
);
4030 /* Check for virtual calls that became direct calls. */
4031 callee
= gimple_call_fn (stmt
);
4032 if (callee
&& TREE_CODE (callee
) == OBJ_TYPE_REF
)
4034 if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee
)) != NULL_TREE
)
4036 if (dump_file
&& virtual_method_call_p (callee
)
4037 && !possible_polymorphic_call_target_p
4038 (callee
, stmt
, cgraph_node::get (gimple_call_addr_fndecl
4039 (OBJ_TYPE_REF_EXPR (callee
)))))
4042 "Type inheritance inconsistent devirtualization of ");
4043 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
4044 fprintf (dump_file
, " to ");
4045 print_generic_expr (dump_file
, callee
, TDF_SLIM
);
4046 fprintf (dump_file
, "\n");
4049 gimple_call_set_fn (stmt
, OBJ_TYPE_REF_EXPR (callee
));
4052 else if (flag_devirtualize
&& !inplace
&& virtual_method_call_p (callee
))
4055 vec
<cgraph_node
*>targets
4056 = possible_polymorphic_call_targets (callee
, stmt
, &final
);
4057 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
4059 tree lhs
= gimple_call_lhs (stmt
);
4060 if (dump_enabled_p ())
4062 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, stmt
,
4063 "folding virtual function call to %s\n",
4064 targets
.length () == 1
4065 ? targets
[0]->name ()
4066 : "__builtin_unreachable");
4068 if (targets
.length () == 1)
4070 tree fndecl
= targets
[0]->decl
;
4071 gimple_call_set_fndecl (stmt
, fndecl
);
4073 /* If changing the call to __cxa_pure_virtual
4074 or similar noreturn function, adjust gimple_call_fntype
4076 if (gimple_call_noreturn_p (stmt
)
4077 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl
)))
4078 && TYPE_ARG_TYPES (TREE_TYPE (fndecl
))
4079 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)))
4081 gimple_call_set_fntype (stmt
, TREE_TYPE (fndecl
));
4082 /* If the call becomes noreturn, remove the lhs. */
4084 && gimple_call_noreturn_p (stmt
)
4085 && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (stmt
)))
4086 || should_remove_lhs_p (lhs
)))
4088 if (TREE_CODE (lhs
) == SSA_NAME
)
4090 tree var
= create_tmp_var (TREE_TYPE (lhs
));
4091 tree def
= get_or_create_ssa_default_def (cfun
, var
);
4092 gimple
*new_stmt
= gimple_build_assign (lhs
, def
);
4093 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
4095 gimple_call_set_lhs (stmt
, NULL_TREE
);
4097 maybe_remove_unused_call_args (cfun
, stmt
);
4101 tree fndecl
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
4102 gimple
*new_stmt
= gimple_build_call (fndecl
, 0);
4103 gimple_set_location (new_stmt
, gimple_location (stmt
));
4104 /* If the call had a SSA name as lhs morph that into
4105 an uninitialized value. */
4106 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
4108 tree var
= create_tmp_var (TREE_TYPE (lhs
));
4109 SET_SSA_NAME_VAR_OR_IDENTIFIER (lhs
, var
);
4110 SSA_NAME_DEF_STMT (lhs
) = gimple_build_nop ();
4111 set_ssa_default_def (cfun
, var
, lhs
);
4113 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
4114 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
4115 gsi_replace (gsi
, new_stmt
, false);
4122 /* Check for indirect calls that became direct calls, and then
4123 no longer require a static chain. */
4124 if (gimple_call_chain (stmt
))
4126 tree fn
= gimple_call_fndecl (stmt
);
4127 if (fn
&& !DECL_STATIC_CHAIN (fn
))
4129 gimple_call_set_chain (stmt
, NULL
);
4134 tree tmp
= maybe_fold_reference (gimple_call_chain (stmt
), false);
4137 gimple_call_set_chain (stmt
, tmp
);
4146 /* Check for builtins that CCP can handle using information not
4147 available in the generic fold routines. */
4148 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
4150 if (gimple_fold_builtin (gsi
))
4153 else if (gimple_call_builtin_p (stmt
, BUILT_IN_MD
))
4155 changed
|= targetm
.gimple_fold_builtin (gsi
);
4157 else if (gimple_call_internal_p (stmt
))
4159 enum tree_code subcode
= ERROR_MARK
;
4160 tree result
= NULL_TREE
;
4161 bool cplx_result
= false;
4162 tree overflow
= NULL_TREE
;
4163 switch (gimple_call_internal_fn (stmt
))
4165 case IFN_BUILTIN_EXPECT
:
4166 result
= fold_builtin_expect (gimple_location (stmt
),
4167 gimple_call_arg (stmt
, 0),
4168 gimple_call_arg (stmt
, 1),
4169 gimple_call_arg (stmt
, 2));
4171 case IFN_UBSAN_OBJECT_SIZE
:
4173 tree offset
= gimple_call_arg (stmt
, 1);
4174 tree objsize
= gimple_call_arg (stmt
, 2);
4175 if (integer_all_onesp (objsize
)
4176 || (TREE_CODE (offset
) == INTEGER_CST
4177 && TREE_CODE (objsize
) == INTEGER_CST
4178 && tree_int_cst_le (offset
, objsize
)))
4180 replace_call_with_value (gsi
, NULL_TREE
);
4186 if (integer_zerop (gimple_call_arg (stmt
, 1)))
4188 replace_call_with_value (gsi
, NULL_TREE
);
4192 case IFN_UBSAN_BOUNDS
:
4194 tree index
= gimple_call_arg (stmt
, 1);
4195 tree bound
= gimple_call_arg (stmt
, 2);
4196 if (TREE_CODE (index
) == INTEGER_CST
4197 && TREE_CODE (bound
) == INTEGER_CST
)
4199 index
= fold_convert (TREE_TYPE (bound
), index
);
4200 if (TREE_CODE (index
) == INTEGER_CST
4201 && tree_int_cst_le (index
, bound
))
4203 replace_call_with_value (gsi
, NULL_TREE
);
4209 case IFN_GOACC_DIM_SIZE
:
4210 case IFN_GOACC_DIM_POS
:
4211 result
= fold_internal_goacc_dim (stmt
);
4213 case IFN_UBSAN_CHECK_ADD
:
4214 subcode
= PLUS_EXPR
;
4216 case IFN_UBSAN_CHECK_SUB
:
4217 subcode
= MINUS_EXPR
;
4219 case IFN_UBSAN_CHECK_MUL
:
4220 subcode
= MULT_EXPR
;
4222 case IFN_ADD_OVERFLOW
:
4223 subcode
= PLUS_EXPR
;
4226 case IFN_SUB_OVERFLOW
:
4227 subcode
= MINUS_EXPR
;
4230 case IFN_MUL_OVERFLOW
:
4231 subcode
= MULT_EXPR
;
4237 if (subcode
!= ERROR_MARK
)
4239 tree arg0
= gimple_call_arg (stmt
, 0);
4240 tree arg1
= gimple_call_arg (stmt
, 1);
4241 tree type
= TREE_TYPE (arg0
);
4244 tree lhs
= gimple_call_lhs (stmt
);
4245 if (lhs
== NULL_TREE
)
4248 type
= TREE_TYPE (TREE_TYPE (lhs
));
4250 if (type
== NULL_TREE
)
4252 /* x = y + 0; x = y - 0; x = y * 0; */
4253 else if (integer_zerop (arg1
))
4254 result
= subcode
== MULT_EXPR
? integer_zero_node
: arg0
;
4255 /* x = 0 + y; x = 0 * y; */
4256 else if (subcode
!= MINUS_EXPR
&& integer_zerop (arg0
))
4257 result
= subcode
== MULT_EXPR
? integer_zero_node
: arg1
;
4259 else if (subcode
== MINUS_EXPR
&& operand_equal_p (arg0
, arg1
, 0))
4260 result
= integer_zero_node
;
4261 /* x = y * 1; x = 1 * y; */
4262 else if (subcode
== MULT_EXPR
&& integer_onep (arg1
))
4264 else if (subcode
== MULT_EXPR
&& integer_onep (arg0
))
4266 else if (TREE_CODE (arg0
) == INTEGER_CST
4267 && TREE_CODE (arg1
) == INTEGER_CST
)
4270 result
= int_const_binop (subcode
, fold_convert (type
, arg0
),
4271 fold_convert (type
, arg1
));
4273 result
= int_const_binop (subcode
, arg0
, arg1
);
4274 if (result
&& arith_overflowed_p (subcode
, type
, arg0
, arg1
))
4277 overflow
= build_one_cst (type
);
4284 if (result
== integer_zero_node
)
4285 result
= build_zero_cst (type
);
4286 else if (cplx_result
&& TREE_TYPE (result
) != type
)
4288 if (TREE_CODE (result
) == INTEGER_CST
)
4290 if (arith_overflowed_p (PLUS_EXPR
, type
, result
,
4292 overflow
= build_one_cst (type
);
4294 else if ((!TYPE_UNSIGNED (TREE_TYPE (result
))
4295 && TYPE_UNSIGNED (type
))
4296 || (TYPE_PRECISION (type
)
4297 < (TYPE_PRECISION (TREE_TYPE (result
))
4298 + (TYPE_UNSIGNED (TREE_TYPE (result
))
4299 && !TYPE_UNSIGNED (type
)))))
4302 result
= fold_convert (type
, result
);
4309 if (TREE_CODE (result
) == INTEGER_CST
&& TREE_OVERFLOW (result
))
4310 result
= drop_tree_overflow (result
);
4313 if (overflow
== NULL_TREE
)
4314 overflow
= build_zero_cst (TREE_TYPE (result
));
4315 tree ctype
= build_complex_type (TREE_TYPE (result
));
4316 if (TREE_CODE (result
) == INTEGER_CST
4317 && TREE_CODE (overflow
) == INTEGER_CST
)
4318 result
= build_complex (ctype
, result
, overflow
);
4320 result
= build2_loc (gimple_location (stmt
), COMPLEX_EXPR
,
4321 ctype
, result
, overflow
);
4323 if (!update_call_from_tree (gsi
, result
))
4324 gimplify_and_update_call_from_tree (gsi
, result
);
4333 /* Return true whether NAME has a use on STMT. */
4336 has_use_on_stmt (tree name
, gimple
*stmt
)
4338 imm_use_iterator iter
;
4339 use_operand_p use_p
;
4340 FOR_EACH_IMM_USE_FAST (use_p
, iter
, name
)
4341 if (USE_STMT (use_p
) == stmt
)
4346 /* Worker for fold_stmt_1 dispatch to pattern based folding with
4349 Replaces *GSI with the simplification result in RCODE and OPS
4350 and the associated statements in *SEQ. Does the replacement
4351 according to INPLACE and returns true if the operation succeeded. */
4354 replace_stmt_with_simplification (gimple_stmt_iterator
*gsi
,
4355 gimple_match_op
*res_op
,
4356 gimple_seq
*seq
, bool inplace
)
4358 gimple
*stmt
= gsi_stmt (*gsi
);
4359 tree
*ops
= res_op
->ops
;
4360 unsigned int num_ops
= res_op
->num_ops
;
4362 /* Play safe and do not allow abnormals to be mentioned in
4363 newly created statements. See also maybe_push_res_to_seq.
4364 As an exception allow such uses if there was a use of the
4365 same SSA name on the old stmt. */
4366 for (unsigned int i
= 0; i
< num_ops
; ++i
)
4367 if (TREE_CODE (ops
[i
]) == SSA_NAME
4368 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[i
])
4369 && !has_use_on_stmt (ops
[i
], stmt
))
4372 if (num_ops
> 0 && COMPARISON_CLASS_P (ops
[0]))
4373 for (unsigned int i
= 0; i
< 2; ++i
)
4374 if (TREE_CODE (TREE_OPERAND (ops
[0], i
)) == SSA_NAME
4375 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops
[0], i
))
4376 && !has_use_on_stmt (TREE_OPERAND (ops
[0], i
), stmt
))
4379 /* Don't insert new statements when INPLACE is true, even if we could
4380 reuse STMT for the final statement. */
4381 if (inplace
&& !gimple_seq_empty_p (*seq
))
4384 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
4386 gcc_assert (res_op
->code
.is_tree_code ());
4387 if (TREE_CODE_CLASS ((enum tree_code
) res_op
->code
) == tcc_comparison
4388 /* GIMPLE_CONDs condition may not throw. */
4389 && (!flag_exceptions
4390 || !cfun
->can_throw_non_call_exceptions
4391 || !operation_could_trap_p (res_op
->code
,
4392 FLOAT_TYPE_P (TREE_TYPE (ops
[0])),
4394 gimple_cond_set_condition (cond_stmt
, res_op
->code
, ops
[0], ops
[1]);
4395 else if (res_op
->code
== SSA_NAME
)
4396 gimple_cond_set_condition (cond_stmt
, NE_EXPR
, ops
[0],
4397 build_zero_cst (TREE_TYPE (ops
[0])));
4398 else if (res_op
->code
== INTEGER_CST
)
4400 if (integer_zerop (ops
[0]))
4401 gimple_cond_make_false (cond_stmt
);
4403 gimple_cond_make_true (cond_stmt
);
4407 tree res
= maybe_push_res_to_seq (res_op
, seq
);
4410 gimple_cond_set_condition (cond_stmt
, NE_EXPR
, res
,
4411 build_zero_cst (TREE_TYPE (res
)));
4415 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4417 fprintf (dump_file
, "gimple_simplified to ");
4418 if (!gimple_seq_empty_p (*seq
))
4419 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
4420 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
),
4423 gsi_insert_seq_before (gsi
, *seq
, GSI_SAME_STMT
);
4426 else if (is_gimple_assign (stmt
)
4427 && res_op
->code
.is_tree_code ())
4430 || gimple_num_ops (stmt
) > get_gimple_rhs_num_ops (res_op
->code
))
4432 maybe_build_generic_op (res_op
);
4433 gimple_assign_set_rhs_with_ops (gsi
, res_op
->code
,
4434 res_op
->op_or_null (0),
4435 res_op
->op_or_null (1),
4436 res_op
->op_or_null (2));
4437 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4439 fprintf (dump_file
, "gimple_simplified to ");
4440 if (!gimple_seq_empty_p (*seq
))
4441 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
4442 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
),
4445 gsi_insert_seq_before (gsi
, *seq
, GSI_SAME_STMT
);
4449 else if (res_op
->code
.is_fn_code ()
4450 && gimple_call_combined_fn (stmt
) == res_op
->code
)
4452 gcc_assert (num_ops
== gimple_call_num_args (stmt
));
4453 for (unsigned int i
= 0; i
< num_ops
; ++i
)
4454 gimple_call_set_arg (stmt
, i
, ops
[i
]);
4455 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4457 fprintf (dump_file
, "gimple_simplified to ");
4458 if (!gimple_seq_empty_p (*seq
))
4459 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
4460 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
), 0, TDF_SLIM
);
4462 gsi_insert_seq_before (gsi
, *seq
, GSI_SAME_STMT
);
4467 if (gimple_has_lhs (stmt
))
4469 tree lhs
= gimple_get_lhs (stmt
);
4470 if (!maybe_push_res_to_seq (res_op
, seq
, lhs
))
4472 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4474 fprintf (dump_file
, "gimple_simplified to ");
4475 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
4477 gsi_replace_with_seq_vops (gsi
, *seq
);
4487 /* Canonicalize MEM_REFs invariant address operand after propagation. */
4490 maybe_canonicalize_mem_ref_addr (tree
*t
)
4494 if (TREE_CODE (*t
) == ADDR_EXPR
)
4495 t
= &TREE_OPERAND (*t
, 0);
4497 /* The C and C++ frontends use an ARRAY_REF for indexing with their
4498 generic vector extension. The actual vector referenced is
4499 view-converted to an array type for this purpose. If the index
4500 is constant the canonical representation in the middle-end is a
4501 BIT_FIELD_REF so re-write the former to the latter here. */
4502 if (TREE_CODE (*t
) == ARRAY_REF
4503 && TREE_CODE (TREE_OPERAND (*t
, 0)) == VIEW_CONVERT_EXPR
4504 && TREE_CODE (TREE_OPERAND (*t
, 1)) == INTEGER_CST
4505 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t
, 0), 0))))
4507 tree vtype
= TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t
, 0), 0));
4508 if (VECTOR_TYPE_P (vtype
))
4510 tree low
= array_ref_low_bound (*t
);
4511 if (TREE_CODE (low
) == INTEGER_CST
)
4513 if (tree_int_cst_le (low
, TREE_OPERAND (*t
, 1)))
4515 widest_int idx
= wi::sub (wi::to_widest (TREE_OPERAND (*t
, 1)),
4516 wi::to_widest (low
));
4517 idx
= wi::mul (idx
, wi::to_widest
4518 (TYPE_SIZE (TREE_TYPE (*t
))));
4520 = wi::add (idx
, wi::to_widest (TYPE_SIZE (TREE_TYPE (*t
))));
4521 if (wi::les_p (ext
, wi::to_widest (TYPE_SIZE (vtype
))))
4523 *t
= build3_loc (EXPR_LOCATION (*t
), BIT_FIELD_REF
,
4525 TREE_OPERAND (TREE_OPERAND (*t
, 0), 0),
4526 TYPE_SIZE (TREE_TYPE (*t
)),
4527 wide_int_to_tree (bitsizetype
, idx
));
4535 while (handled_component_p (*t
))
4536 t
= &TREE_OPERAND (*t
, 0);
4538 /* Canonicalize MEM [&foo.bar, 0] which appears after propagating
4539 of invariant addresses into a SSA name MEM_REF address. */
4540 if (TREE_CODE (*t
) == MEM_REF
4541 || TREE_CODE (*t
) == TARGET_MEM_REF
)
4543 tree addr
= TREE_OPERAND (*t
, 0);
4544 if (TREE_CODE (addr
) == ADDR_EXPR
4545 && (TREE_CODE (TREE_OPERAND (addr
, 0)) == MEM_REF
4546 || handled_component_p (TREE_OPERAND (addr
, 0))))
4550 base
= get_addr_base_and_unit_offset (TREE_OPERAND (addr
, 0),
4555 TREE_OPERAND (*t
, 0) = build_fold_addr_expr (base
);
4556 TREE_OPERAND (*t
, 1) = int_const_binop (PLUS_EXPR
,
4557 TREE_OPERAND (*t
, 1),
4558 size_int (coffset
));
4561 gcc_checking_assert (TREE_CODE (TREE_OPERAND (*t
, 0)) == DEBUG_EXPR_DECL
4562 || is_gimple_mem_ref_addr (TREE_OPERAND (*t
, 0)));
4565 /* Canonicalize back MEM_REFs to plain reference trees if the object
4566 accessed is a decl that has the same access semantics as the MEM_REF. */
4567 if (TREE_CODE (*t
) == MEM_REF
4568 && TREE_CODE (TREE_OPERAND (*t
, 0)) == ADDR_EXPR
4569 && integer_zerop (TREE_OPERAND (*t
, 1))
4570 && MR_DEPENDENCE_CLIQUE (*t
) == 0)
4572 tree decl
= TREE_OPERAND (TREE_OPERAND (*t
, 0), 0);
4573 tree alias_type
= TREE_TYPE (TREE_OPERAND (*t
, 1));
4574 if (/* Same volatile qualification. */
4575 TREE_THIS_VOLATILE (*t
) == TREE_THIS_VOLATILE (decl
)
4576 /* Same TBAA behavior with -fstrict-aliasing. */
4577 && !TYPE_REF_CAN_ALIAS_ALL (alias_type
)
4578 && (TYPE_MAIN_VARIANT (TREE_TYPE (decl
))
4579 == TYPE_MAIN_VARIANT (TREE_TYPE (alias_type
)))
4580 /* Same alignment. */
4581 && TYPE_ALIGN (TREE_TYPE (decl
)) == TYPE_ALIGN (TREE_TYPE (*t
))
4582 /* We have to look out here to not drop a required conversion
4583 from the rhs to the lhs if *t appears on the lhs or vice-versa
4584 if it appears on the rhs. Thus require strict type
4586 && types_compatible_p (TREE_TYPE (*t
), TREE_TYPE (decl
)))
4588 *t
= TREE_OPERAND (TREE_OPERAND (*t
, 0), 0);
4593 /* Canonicalize TARGET_MEM_REF in particular with respect to
4594 the indexes becoming constant. */
4595 else if (TREE_CODE (*t
) == TARGET_MEM_REF
)
4597 tree tem
= maybe_fold_tmr (*t
);
4608 /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument
4609 distinguishes both cases. */
4612 fold_stmt_1 (gimple_stmt_iterator
*gsi
, bool inplace
, tree (*valueize
) (tree
))
4614 bool changed
= false;
4615 gimple
*stmt
= gsi_stmt (*gsi
);
4616 bool nowarning
= gimple_no_warning_p (stmt
);
4618 fold_defer_overflow_warnings ();
4620 /* First do required canonicalization of [TARGET_]MEM_REF addresses
4622 ??? This shouldn't be done in generic folding but in the
4623 propagation helpers which also know whether an address was
4625 Also canonicalize operand order. */
4626 switch (gimple_code (stmt
))
4629 if (gimple_assign_rhs_class (stmt
) == GIMPLE_SINGLE_RHS
)
4631 tree
*rhs
= gimple_assign_rhs1_ptr (stmt
);
4632 if ((REFERENCE_CLASS_P (*rhs
)
4633 || TREE_CODE (*rhs
) == ADDR_EXPR
)
4634 && maybe_canonicalize_mem_ref_addr (rhs
))
4636 tree
*lhs
= gimple_assign_lhs_ptr (stmt
);
4637 if (REFERENCE_CLASS_P (*lhs
)
4638 && maybe_canonicalize_mem_ref_addr (lhs
))
4643 /* Canonicalize operand order. */
4644 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4645 if (TREE_CODE_CLASS (code
) == tcc_comparison
4646 || commutative_tree_code (code
)
4647 || commutative_ternary_tree_code (code
))
4649 tree rhs1
= gimple_assign_rhs1 (stmt
);
4650 tree rhs2
= gimple_assign_rhs2 (stmt
);
4651 if (tree_swap_operands_p (rhs1
, rhs2
))
4653 gimple_assign_set_rhs1 (stmt
, rhs2
);
4654 gimple_assign_set_rhs2 (stmt
, rhs1
);
4655 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4656 gimple_assign_set_rhs_code (stmt
,
4657 swap_tree_comparison (code
));
4665 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
4667 tree
*arg
= gimple_call_arg_ptr (stmt
, i
);
4668 if (REFERENCE_CLASS_P (*arg
)
4669 && maybe_canonicalize_mem_ref_addr (arg
))
4672 tree
*lhs
= gimple_call_lhs_ptr (stmt
);
4674 && REFERENCE_CLASS_P (*lhs
)
4675 && maybe_canonicalize_mem_ref_addr (lhs
))
4681 gasm
*asm_stmt
= as_a
<gasm
*> (stmt
);
4682 for (i
= 0; i
< gimple_asm_noutputs (asm_stmt
); ++i
)
4684 tree link
= gimple_asm_output_op (asm_stmt
, i
);
4685 tree op
= TREE_VALUE (link
);
4686 if (REFERENCE_CLASS_P (op
)
4687 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link
)))
4690 for (i
= 0; i
< gimple_asm_ninputs (asm_stmt
); ++i
)
4692 tree link
= gimple_asm_input_op (asm_stmt
, i
);
4693 tree op
= TREE_VALUE (link
);
4694 if ((REFERENCE_CLASS_P (op
)
4695 || TREE_CODE (op
) == ADDR_EXPR
)
4696 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link
)))
4702 if (gimple_debug_bind_p (stmt
))
4704 tree
*val
= gimple_debug_bind_get_value_ptr (stmt
);
4706 && (REFERENCE_CLASS_P (*val
)
4707 || TREE_CODE (*val
) == ADDR_EXPR
)
4708 && maybe_canonicalize_mem_ref_addr (val
))
4714 /* Canonicalize operand order. */
4715 tree lhs
= gimple_cond_lhs (stmt
);
4716 tree rhs
= gimple_cond_rhs (stmt
);
4717 if (tree_swap_operands_p (lhs
, rhs
))
4719 gcond
*gc
= as_a
<gcond
*> (stmt
);
4720 gimple_cond_set_lhs (gc
, rhs
);
4721 gimple_cond_set_rhs (gc
, lhs
);
4722 gimple_cond_set_code (gc
,
4723 swap_tree_comparison (gimple_cond_code (gc
)));
4730 /* Dispatch to pattern-based folding. */
4732 || is_gimple_assign (stmt
)
4733 || gimple_code (stmt
) == GIMPLE_COND
)
4735 gimple_seq seq
= NULL
;
4736 gimple_match_op res_op
;
4737 if (gimple_simplify (stmt
, &res_op
, inplace
? NULL
: &seq
,
4738 valueize
, valueize
))
4740 if (replace_stmt_with_simplification (gsi
, &res_op
, &seq
, inplace
))
4743 gimple_seq_discard (seq
);
4747 stmt
= gsi_stmt (*gsi
);
4749 /* Fold the main computation performed by the statement. */
4750 switch (gimple_code (stmt
))
4754 /* Try to canonicalize for boolean-typed X the comparisons
4755 X == 0, X == 1, X != 0, and X != 1. */
4756 if (gimple_assign_rhs_code (stmt
) == EQ_EXPR
4757 || gimple_assign_rhs_code (stmt
) == NE_EXPR
)
4759 tree lhs
= gimple_assign_lhs (stmt
);
4760 tree op1
= gimple_assign_rhs1 (stmt
);
4761 tree op2
= gimple_assign_rhs2 (stmt
);
4762 tree type
= TREE_TYPE (op1
);
4764 /* Check whether the comparison operands are of the same boolean
4765 type as the result type is.
4766 Check that second operand is an integer-constant with value
4768 if (TREE_CODE (op2
) == INTEGER_CST
4769 && (integer_zerop (op2
) || integer_onep (op2
))
4770 && useless_type_conversion_p (TREE_TYPE (lhs
), type
))
4772 enum tree_code cmp_code
= gimple_assign_rhs_code (stmt
);
4773 bool is_logical_not
= false;
4775 /* X == 0 and X != 1 is a logical-not.of X
4776 X == 1 and X != 0 is X */
4777 if ((cmp_code
== EQ_EXPR
&& integer_zerop (op2
))
4778 || (cmp_code
== NE_EXPR
&& integer_onep (op2
)))
4779 is_logical_not
= true;
4781 if (is_logical_not
== false)
4782 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op1
), op1
);
4783 /* Only for one-bit precision typed X the transformation
4784 !X -> ~X is valied. */
4785 else if (TYPE_PRECISION (type
) == 1)
4786 gimple_assign_set_rhs_with_ops (gsi
, BIT_NOT_EXPR
, op1
);
4787 /* Otherwise we use !X -> X ^ 1. */
4789 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op1
,
4790 build_int_cst (type
, 1));
4796 unsigned old_num_ops
= gimple_num_ops (stmt
);
4797 tree lhs
= gimple_assign_lhs (stmt
);
4798 tree new_rhs
= fold_gimple_assign (gsi
);
4800 && !useless_type_conversion_p (TREE_TYPE (lhs
),
4801 TREE_TYPE (new_rhs
)))
4802 new_rhs
= fold_convert (TREE_TYPE (lhs
), new_rhs
);
4805 || get_gimple_rhs_num_ops (TREE_CODE (new_rhs
)) < old_num_ops
))
4807 gimple_assign_set_rhs_from_tree (gsi
, new_rhs
);
4814 changed
|= gimple_fold_call (gsi
, inplace
);
4818 /* Fold *& in asm operands. */
4820 gasm
*asm_stmt
= as_a
<gasm
*> (stmt
);
4822 const char **oconstraints
;
4823 const char *constraint
;
4824 bool allows_mem
, allows_reg
;
4826 noutputs
= gimple_asm_noutputs (asm_stmt
);
4827 oconstraints
= XALLOCAVEC (const char *, noutputs
);
4829 for (i
= 0; i
< gimple_asm_noutputs (asm_stmt
); ++i
)
4831 tree link
= gimple_asm_output_op (asm_stmt
, i
);
4832 tree op
= TREE_VALUE (link
);
4834 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
4835 if (REFERENCE_CLASS_P (op
)
4836 && (op
= maybe_fold_reference (op
, true)) != NULL_TREE
)
4838 TREE_VALUE (link
) = op
;
4842 for (i
= 0; i
< gimple_asm_ninputs (asm_stmt
); ++i
)
4844 tree link
= gimple_asm_input_op (asm_stmt
, i
);
4845 tree op
= TREE_VALUE (link
);
4847 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
4848 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0,
4849 oconstraints
, &allows_mem
, &allows_reg
);
4850 if (REFERENCE_CLASS_P (op
)
4851 && (op
= maybe_fold_reference (op
, !allows_reg
&& allows_mem
))
4854 TREE_VALUE (link
) = op
;
4862 if (gimple_debug_bind_p (stmt
))
4864 tree val
= gimple_debug_bind_get_value (stmt
);
4866 && REFERENCE_CLASS_P (val
))
4868 tree tem
= maybe_fold_reference (val
, false);
4871 gimple_debug_bind_set_value (stmt
, tem
);
4876 && TREE_CODE (val
) == ADDR_EXPR
)
4878 tree ref
= TREE_OPERAND (val
, 0);
4879 tree tem
= maybe_fold_reference (ref
, false);
4882 tem
= build_fold_addr_expr_with_type (tem
, TREE_TYPE (val
));
4883 gimple_debug_bind_set_value (stmt
, tem
);
4892 greturn
*ret_stmt
= as_a
<greturn
*> (stmt
);
4893 tree ret
= gimple_return_retval(ret_stmt
);
4895 if (ret
&& TREE_CODE (ret
) == SSA_NAME
&& valueize
)
4897 tree val
= valueize (ret
);
4898 if (val
&& val
!= ret
4899 && may_propagate_copy (ret
, val
))
4901 gimple_return_set_retval (ret_stmt
, val
);
4911 stmt
= gsi_stmt (*gsi
);
4913 /* Fold *& on the lhs. */
4914 if (gimple_has_lhs (stmt
))
4916 tree lhs
= gimple_get_lhs (stmt
);
4917 if (lhs
&& REFERENCE_CLASS_P (lhs
))
4919 tree new_lhs
= maybe_fold_reference (lhs
, true);
4922 gimple_set_lhs (stmt
, new_lhs
);
4928 fold_undefer_overflow_warnings (changed
&& !nowarning
, stmt
, 0);
4932 /* Valueziation callback that ends up not following SSA edges. */
4935 no_follow_ssa_edges (tree
)
4940 /* Valueization callback that ends up following single-use SSA edges only. */
4943 follow_single_use_edges (tree val
)
4945 if (TREE_CODE (val
) == SSA_NAME
4946 && !has_single_use (val
))
4951 /* Valueization callback that follows all SSA edges. */
4954 follow_all_ssa_edges (tree val
)
4959 /* Fold the statement pointed to by GSI. In some cases, this function may
4960 replace the whole statement with a new one. Returns true iff folding
4962 The statement pointed to by GSI should be in valid gimple form but may
4963 be in unfolded state as resulting from for example constant propagation
4964 which can produce *&x = 0. */
4967 fold_stmt (gimple_stmt_iterator
*gsi
)
4969 return fold_stmt_1 (gsi
, false, no_follow_ssa_edges
);
4973 fold_stmt (gimple_stmt_iterator
*gsi
, tree (*valueize
) (tree
))
4975 return fold_stmt_1 (gsi
, false, valueize
);
4978 /* Perform the minimal folding on statement *GSI. Only operations like
4979 *&x created by constant propagation are handled. The statement cannot
4980 be replaced with a new one. Return true if the statement was
4981 changed, false otherwise.
4982 The statement *GSI should be in valid gimple form but may
4983 be in unfolded state as resulting from for example constant propagation
4984 which can produce *&x = 0. */
4987 fold_stmt_inplace (gimple_stmt_iterator
*gsi
)
4989 gimple
*stmt
= gsi_stmt (*gsi
);
4990 bool changed
= fold_stmt_1 (gsi
, true, no_follow_ssa_edges
);
4991 gcc_assert (gsi_stmt (*gsi
) == stmt
);
4995 /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
4996 if EXPR is null or we don't know how.
4997 If non-null, the result always has boolean type. */
5000 canonicalize_bool (tree expr
, bool invert
)
5006 if (integer_nonzerop (expr
))
5007 return boolean_false_node
;
5008 else if (integer_zerop (expr
))
5009 return boolean_true_node
;
5010 else if (TREE_CODE (expr
) == SSA_NAME
)
5011 return fold_build2 (EQ_EXPR
, boolean_type_node
, expr
,
5012 build_int_cst (TREE_TYPE (expr
), 0));
5013 else if (COMPARISON_CLASS_P (expr
))
5014 return fold_build2 (invert_tree_comparison (TREE_CODE (expr
), false),
5016 TREE_OPERAND (expr
, 0),
5017 TREE_OPERAND (expr
, 1));
5023 if (TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
)
5025 if (integer_nonzerop (expr
))
5026 return boolean_true_node
;
5027 else if (integer_zerop (expr
))
5028 return boolean_false_node
;
5029 else if (TREE_CODE (expr
) == SSA_NAME
)
5030 return fold_build2 (NE_EXPR
, boolean_type_node
, expr
,
5031 build_int_cst (TREE_TYPE (expr
), 0));
5032 else if (COMPARISON_CLASS_P (expr
))
5033 return fold_build2 (TREE_CODE (expr
),
5035 TREE_OPERAND (expr
, 0),
5036 TREE_OPERAND (expr
, 1));
5042 /* Check to see if a boolean expression EXPR is logically equivalent to the
5043 comparison (OP1 CODE OP2). Check for various identities involving
5047 same_bool_comparison_p (const_tree expr
, enum tree_code code
,
5048 const_tree op1
, const_tree op2
)
5052 /* The obvious case. */
5053 if (TREE_CODE (expr
) == code
5054 && operand_equal_p (TREE_OPERAND (expr
, 0), op1
, 0)
5055 && operand_equal_p (TREE_OPERAND (expr
, 1), op2
, 0))
5058 /* Check for comparing (name, name != 0) and the case where expr
5059 is an SSA_NAME with a definition matching the comparison. */
5060 if (TREE_CODE (expr
) == SSA_NAME
5061 && TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
)
5063 if (operand_equal_p (expr
, op1
, 0))
5064 return ((code
== NE_EXPR
&& integer_zerop (op2
))
5065 || (code
== EQ_EXPR
&& integer_nonzerop (op2
)));
5066 s
= SSA_NAME_DEF_STMT (expr
);
5067 if (is_gimple_assign (s
)
5068 && gimple_assign_rhs_code (s
) == code
5069 && operand_equal_p (gimple_assign_rhs1 (s
), op1
, 0)
5070 && operand_equal_p (gimple_assign_rhs2 (s
), op2
, 0))
5074 /* If op1 is of the form (name != 0) or (name == 0), and the definition
5075 of name is a comparison, recurse. */
5076 if (TREE_CODE (op1
) == SSA_NAME
5077 && TREE_CODE (TREE_TYPE (op1
)) == BOOLEAN_TYPE
)
5079 s
= SSA_NAME_DEF_STMT (op1
);
5080 if (is_gimple_assign (s
)
5081 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
)
5083 enum tree_code c
= gimple_assign_rhs_code (s
);
5084 if ((c
== NE_EXPR
&& integer_zerop (op2
))
5085 || (c
== EQ_EXPR
&& integer_nonzerop (op2
)))
5086 return same_bool_comparison_p (expr
, c
,
5087 gimple_assign_rhs1 (s
),
5088 gimple_assign_rhs2 (s
));
5089 if ((c
== EQ_EXPR
&& integer_zerop (op2
))
5090 || (c
== NE_EXPR
&& integer_nonzerop (op2
)))
5091 return same_bool_comparison_p (expr
,
5092 invert_tree_comparison (c
, false),
5093 gimple_assign_rhs1 (s
),
5094 gimple_assign_rhs2 (s
));
5100 /* Check to see if two boolean expressions OP1 and OP2 are logically
5104 same_bool_result_p (const_tree op1
, const_tree op2
)
5106 /* Simple cases first. */
5107 if (operand_equal_p (op1
, op2
, 0))
5110 /* Check the cases where at least one of the operands is a comparison.
5111 These are a bit smarter than operand_equal_p in that they apply some
5112 identifies on SSA_NAMEs. */
5113 if (COMPARISON_CLASS_P (op2
)
5114 && same_bool_comparison_p (op1
, TREE_CODE (op2
),
5115 TREE_OPERAND (op2
, 0),
5116 TREE_OPERAND (op2
, 1)))
5118 if (COMPARISON_CLASS_P (op1
)
5119 && same_bool_comparison_p (op2
, TREE_CODE (op1
),
5120 TREE_OPERAND (op1
, 0),
5121 TREE_OPERAND (op1
, 1)))
5128 /* Forward declarations for some mutually recursive functions. */
5131 and_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
5132 enum tree_code code2
, tree op2a
, tree op2b
);
5134 and_var_with_comparison (tree var
, bool invert
,
5135 enum tree_code code2
, tree op2a
, tree op2b
);
5137 and_var_with_comparison_1 (gimple
*stmt
,
5138 enum tree_code code2
, tree op2a
, tree op2b
);
5140 or_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
5141 enum tree_code code2
, tree op2a
, tree op2b
);
5143 or_var_with_comparison (tree var
, bool invert
,
5144 enum tree_code code2
, tree op2a
, tree op2b
);
5146 or_var_with_comparison_1 (gimple
*stmt
,
5147 enum tree_code code2
, tree op2a
, tree op2b
);
5149 /* Helper function for and_comparisons_1: try to simplify the AND of the
5150 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
5151 If INVERT is true, invert the value of the VAR before doing the AND.
5152 Return NULL_EXPR if we can't simplify this to a single expression. */
5155 and_var_with_comparison (tree var
, bool invert
,
5156 enum tree_code code2
, tree op2a
, tree op2b
)
5159 gimple
*stmt
= SSA_NAME_DEF_STMT (var
);
5161 /* We can only deal with variables whose definitions are assignments. */
5162 if (!is_gimple_assign (stmt
))
5165 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
5166 !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
5167 Then we only have to consider the simpler non-inverted cases. */
5169 t
= or_var_with_comparison_1 (stmt
,
5170 invert_tree_comparison (code2
, false),
5173 t
= and_var_with_comparison_1 (stmt
, code2
, op2a
, op2b
);
5174 return canonicalize_bool (t
, invert
);
5177 /* Try to simplify the AND of the ssa variable defined by the assignment
5178 STMT with the comparison specified by (OP2A CODE2 OP2B).
5179 Return NULL_EXPR if we can't simplify this to a single expression. */
5182 and_var_with_comparison_1 (gimple
*stmt
,
5183 enum tree_code code2
, tree op2a
, tree op2b
)
5185 tree var
= gimple_assign_lhs (stmt
);
5186 tree true_test_var
= NULL_TREE
;
5187 tree false_test_var
= NULL_TREE
;
5188 enum tree_code innercode
= gimple_assign_rhs_code (stmt
);
5190 /* Check for identities like (var AND (var == 0)) => false. */
5191 if (TREE_CODE (op2a
) == SSA_NAME
5192 && TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
)
5194 if ((code2
== NE_EXPR
&& integer_zerop (op2b
))
5195 || (code2
== EQ_EXPR
&& integer_nonzerop (op2b
)))
5197 true_test_var
= op2a
;
5198 if (var
== true_test_var
)
5201 else if ((code2
== EQ_EXPR
&& integer_zerop (op2b
))
5202 || (code2
== NE_EXPR
&& integer_nonzerop (op2b
)))
5204 false_test_var
= op2a
;
5205 if (var
== false_test_var
)
5206 return boolean_false_node
;
5210 /* If the definition is a comparison, recurse on it. */
5211 if (TREE_CODE_CLASS (innercode
) == tcc_comparison
)
5213 tree t
= and_comparisons_1 (innercode
,
5214 gimple_assign_rhs1 (stmt
),
5215 gimple_assign_rhs2 (stmt
),
5223 /* If the definition is an AND or OR expression, we may be able to
5224 simplify by reassociating. */
5225 if (TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
5226 && (innercode
== BIT_AND_EXPR
|| innercode
== BIT_IOR_EXPR
))
5228 tree inner1
= gimple_assign_rhs1 (stmt
);
5229 tree inner2
= gimple_assign_rhs2 (stmt
);
5232 tree partial
= NULL_TREE
;
5233 bool is_and
= (innercode
== BIT_AND_EXPR
);
5235 /* Check for boolean identities that don't require recursive examination
5237 inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
5238 inner1 AND (inner1 OR inner2) => inner1
5239 !inner1 AND (inner1 AND inner2) => false
5240 !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
5241 Likewise for similar cases involving inner2. */
5242 if (inner1
== true_test_var
)
5243 return (is_and
? var
: inner1
);
5244 else if (inner2
== true_test_var
)
5245 return (is_and
? var
: inner2
);
5246 else if (inner1
== false_test_var
)
5248 ? boolean_false_node
5249 : and_var_with_comparison (inner2
, false, code2
, op2a
, op2b
));
5250 else if (inner2
== false_test_var
)
5252 ? boolean_false_node
5253 : and_var_with_comparison (inner1
, false, code2
, op2a
, op2b
));
5255 /* Next, redistribute/reassociate the AND across the inner tests.
5256 Compute the first partial result, (inner1 AND (op2a code op2b)) */
5257 if (TREE_CODE (inner1
) == SSA_NAME
5258 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner1
))
5259 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
5260 && (t
= maybe_fold_and_comparisons (gimple_assign_rhs_code (s
),
5261 gimple_assign_rhs1 (s
),
5262 gimple_assign_rhs2 (s
),
5263 code2
, op2a
, op2b
)))
5265 /* Handle the AND case, where we are reassociating:
5266 (inner1 AND inner2) AND (op2a code2 op2b)
5268 If the partial result t is a constant, we win. Otherwise
5269 continue on to try reassociating with the other inner test. */
5272 if (integer_onep (t
))
5274 else if (integer_zerop (t
))
5275 return boolean_false_node
;
5278 /* Handle the OR case, where we are redistributing:
5279 (inner1 OR inner2) AND (op2a code2 op2b)
5280 => (t OR (inner2 AND (op2a code2 op2b))) */
5281 else if (integer_onep (t
))
5282 return boolean_true_node
;
5284 /* Save partial result for later. */
5288 /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
5289 if (TREE_CODE (inner2
) == SSA_NAME
5290 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner2
))
5291 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
5292 && (t
= maybe_fold_and_comparisons (gimple_assign_rhs_code (s
),
5293 gimple_assign_rhs1 (s
),
5294 gimple_assign_rhs2 (s
),
5295 code2
, op2a
, op2b
)))
5297 /* Handle the AND case, where we are reassociating:
5298 (inner1 AND inner2) AND (op2a code2 op2b)
5299 => (inner1 AND t) */
5302 if (integer_onep (t
))
5304 else if (integer_zerop (t
))
5305 return boolean_false_node
;
5306 /* If both are the same, we can apply the identity
5308 else if (partial
&& same_bool_result_p (t
, partial
))
5312 /* Handle the OR case. where we are redistributing:
5313 (inner1 OR inner2) AND (op2a code2 op2b)
5314 => (t OR (inner1 AND (op2a code2 op2b)))
5315 => (t OR partial) */
5318 if (integer_onep (t
))
5319 return boolean_true_node
;
5322 /* We already got a simplification for the other
5323 operand to the redistributed OR expression. The
5324 interesting case is when at least one is false.
5325 Or, if both are the same, we can apply the identity
5327 if (integer_zerop (partial
))
5329 else if (integer_zerop (t
))
5331 else if (same_bool_result_p (t
, partial
))
5340 /* Try to simplify the AND of two comparisons defined by
5341 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
5342 If this can be done without constructing an intermediate value,
5343 return the resulting tree; otherwise NULL_TREE is returned.
5344 This function is deliberately asymmetric as it recurses on SSA_DEFs
5345 in the first comparison but not the second. */
5348 and_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
5349 enum tree_code code2
, tree op2a
, tree op2b
)
5351 tree truth_type
= truth_type_for (TREE_TYPE (op1a
));
5353 /* First check for ((x CODE1 y) AND (x CODE2 y)). */
5354 if (operand_equal_p (op1a
, op2a
, 0)
5355 && operand_equal_p (op1b
, op2b
, 0))
5357 /* Result will be either NULL_TREE, or a combined comparison. */
5358 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
5359 TRUTH_ANDIF_EXPR
, code1
, code2
,
5360 truth_type
, op1a
, op1b
);
5365 /* Likewise the swapped case of the above. */
5366 if (operand_equal_p (op1a
, op2b
, 0)
5367 && operand_equal_p (op1b
, op2a
, 0))
5369 /* Result will be either NULL_TREE, or a combined comparison. */
5370 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
5371 TRUTH_ANDIF_EXPR
, code1
,
5372 swap_tree_comparison (code2
),
5373 truth_type
, op1a
, op1b
);
5378 /* If both comparisons are of the same value against constants, we might
5379 be able to merge them. */
5380 if (operand_equal_p (op1a
, op2a
, 0)
5381 && TREE_CODE (op1b
) == INTEGER_CST
5382 && TREE_CODE (op2b
) == INTEGER_CST
)
5384 int cmp
= tree_int_cst_compare (op1b
, op2b
);
5386 /* If we have (op1a == op1b), we should either be able to
5387 return that or FALSE, depending on whether the constant op1b
5388 also satisfies the other comparison against op2b. */
5389 if (code1
== EQ_EXPR
)
5395 case EQ_EXPR
: val
= (cmp
== 0); break;
5396 case NE_EXPR
: val
= (cmp
!= 0); break;
5397 case LT_EXPR
: val
= (cmp
< 0); break;
5398 case GT_EXPR
: val
= (cmp
> 0); break;
5399 case LE_EXPR
: val
= (cmp
<= 0); break;
5400 case GE_EXPR
: val
= (cmp
>= 0); break;
5401 default: done
= false;
5406 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5408 return boolean_false_node
;
5411 /* Likewise if the second comparison is an == comparison. */
5412 else if (code2
== EQ_EXPR
)
5418 case EQ_EXPR
: val
= (cmp
== 0); break;
5419 case NE_EXPR
: val
= (cmp
!= 0); break;
5420 case LT_EXPR
: val
= (cmp
> 0); break;
5421 case GT_EXPR
: val
= (cmp
< 0); break;
5422 case LE_EXPR
: val
= (cmp
>= 0); break;
5423 case GE_EXPR
: val
= (cmp
<= 0); break;
5424 default: done
= false;
5429 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5431 return boolean_false_node
;
5435 /* Same business with inequality tests. */
5436 else if (code1
== NE_EXPR
)
5441 case EQ_EXPR
: val
= (cmp
!= 0); break;
5442 case NE_EXPR
: val
= (cmp
== 0); break;
5443 case LT_EXPR
: val
= (cmp
>= 0); break;
5444 case GT_EXPR
: val
= (cmp
<= 0); break;
5445 case LE_EXPR
: val
= (cmp
> 0); break;
5446 case GE_EXPR
: val
= (cmp
< 0); break;
5451 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5453 else if (code2
== NE_EXPR
)
5458 case EQ_EXPR
: val
= (cmp
== 0); break;
5459 case NE_EXPR
: val
= (cmp
!= 0); break;
5460 case LT_EXPR
: val
= (cmp
<= 0); break;
5461 case GT_EXPR
: val
= (cmp
>= 0); break;
5462 case LE_EXPR
: val
= (cmp
< 0); break;
5463 case GE_EXPR
: val
= (cmp
> 0); break;
5468 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5471 /* Chose the more restrictive of two < or <= comparisons. */
5472 else if ((code1
== LT_EXPR
|| code1
== LE_EXPR
)
5473 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
5475 if ((cmp
< 0) || (cmp
== 0 && code1
== LT_EXPR
))
5476 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5478 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5481 /* Likewise chose the more restrictive of two > or >= comparisons. */
5482 else if ((code1
== GT_EXPR
|| code1
== GE_EXPR
)
5483 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
5485 if ((cmp
> 0) || (cmp
== 0 && code1
== GT_EXPR
))
5486 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5488 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5491 /* Check for singleton ranges. */
5493 && ((code1
== LE_EXPR
&& code2
== GE_EXPR
)
5494 || (code1
== GE_EXPR
&& code2
== LE_EXPR
)))
5495 return fold_build2 (EQ_EXPR
, boolean_type_node
, op1a
, op2b
);
5497 /* Check for disjoint ranges. */
5499 && (code1
== LT_EXPR
|| code1
== LE_EXPR
)
5500 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
5501 return boolean_false_node
;
5503 && (code1
== GT_EXPR
|| code1
== GE_EXPR
)
5504 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
5505 return boolean_false_node
;
5508 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
5509 NAME's definition is a truth value. See if there are any simplifications
5510 that can be done against the NAME's definition. */
5511 if (TREE_CODE (op1a
) == SSA_NAME
5512 && (code1
== NE_EXPR
|| code1
== EQ_EXPR
)
5513 && (integer_zerop (op1b
) || integer_onep (op1b
)))
5515 bool invert
= ((code1
== EQ_EXPR
&& integer_zerop (op1b
))
5516 || (code1
== NE_EXPR
&& integer_onep (op1b
)));
5517 gimple
*stmt
= SSA_NAME_DEF_STMT (op1a
);
5518 switch (gimple_code (stmt
))
5521 /* Try to simplify by copy-propagating the definition. */
5522 return and_var_with_comparison (op1a
, invert
, code2
, op2a
, op2b
);
5525 /* If every argument to the PHI produces the same result when
5526 ANDed with the second comparison, we win.
5527 Do not do this unless the type is bool since we need a bool
5528 result here anyway. */
5529 if (TREE_CODE (TREE_TYPE (op1a
)) == BOOLEAN_TYPE
)
5531 tree result
= NULL_TREE
;
5533 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
5535 tree arg
= gimple_phi_arg_def (stmt
, i
);
5537 /* If this PHI has itself as an argument, ignore it.
5538 If all the other args produce the same result,
5540 if (arg
== gimple_phi_result (stmt
))
5542 else if (TREE_CODE (arg
) == INTEGER_CST
)
5544 if (invert
? integer_nonzerop (arg
) : integer_zerop (arg
))
5547 result
= boolean_false_node
;
5548 else if (!integer_zerop (result
))
5552 result
= fold_build2 (code2
, boolean_type_node
,
5554 else if (!same_bool_comparison_p (result
,
5558 else if (TREE_CODE (arg
) == SSA_NAME
5559 && !SSA_NAME_IS_DEFAULT_DEF (arg
))
5562 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
5563 /* In simple cases we can look through PHI nodes,
5564 but we have to be careful with loops.
5566 if (! dom_info_available_p (CDI_DOMINATORS
)
5567 || gimple_bb (def_stmt
) == gimple_bb (stmt
)
5568 || dominated_by_p (CDI_DOMINATORS
,
5569 gimple_bb (def_stmt
),
5572 temp
= and_var_with_comparison (arg
, invert
, code2
,
5578 else if (!same_bool_result_p (result
, temp
))
5594 /* Try to simplify the AND of two comparisons, specified by
5595 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
5596 If this can be simplified to a single expression (without requiring
5597 introducing more SSA variables to hold intermediate values),
5598 return the resulting tree. Otherwise return NULL_TREE.
5599 If the result expression is non-null, it has boolean type. */
5602 maybe_fold_and_comparisons (enum tree_code code1
, tree op1a
, tree op1b
,
5603 enum tree_code code2
, tree op2a
, tree op2b
)
5605 tree t
= and_comparisons_1 (code1
, op1a
, op1b
, code2
, op2a
, op2b
);
5609 return and_comparisons_1 (code2
, op2a
, op2b
, code1
, op1a
, op1b
);
5612 /* Helper function for or_comparisons_1: try to simplify the OR of the
5613 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
5614 If INVERT is true, invert the value of VAR before doing the OR.
5615 Return NULL_EXPR if we can't simplify this to a single expression. */
5618 or_var_with_comparison (tree var
, bool invert
,
5619 enum tree_code code2
, tree op2a
, tree op2b
)
5622 gimple
*stmt
= SSA_NAME_DEF_STMT (var
);
5624 /* We can only deal with variables whose definitions are assignments. */
5625 if (!is_gimple_assign (stmt
))
5628 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
5629 !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
5630 Then we only have to consider the simpler non-inverted cases. */
5632 t
= and_var_with_comparison_1 (stmt
,
5633 invert_tree_comparison (code2
, false),
5636 t
= or_var_with_comparison_1 (stmt
, code2
, op2a
, op2b
);
5637 return canonicalize_bool (t
, invert
);
5640 /* Try to simplify the OR of the ssa variable defined by the assignment
5641 STMT with the comparison specified by (OP2A CODE2 OP2B).
5642 Return NULL_EXPR if we can't simplify this to a single expression. */
5645 or_var_with_comparison_1 (gimple
*stmt
,
5646 enum tree_code code2
, tree op2a
, tree op2b
)
5648 tree var
= gimple_assign_lhs (stmt
);
5649 tree true_test_var
= NULL_TREE
;
5650 tree false_test_var
= NULL_TREE
;
5651 enum tree_code innercode
= gimple_assign_rhs_code (stmt
);
5653 /* Check for identities like (var OR (var != 0)) => true . */
5654 if (TREE_CODE (op2a
) == SSA_NAME
5655 && TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
)
5657 if ((code2
== NE_EXPR
&& integer_zerop (op2b
))
5658 || (code2
== EQ_EXPR
&& integer_nonzerop (op2b
)))
5660 true_test_var
= op2a
;
5661 if (var
== true_test_var
)
5664 else if ((code2
== EQ_EXPR
&& integer_zerop (op2b
))
5665 || (code2
== NE_EXPR
&& integer_nonzerop (op2b
)))
5667 false_test_var
= op2a
;
5668 if (var
== false_test_var
)
5669 return boolean_true_node
;
5673 /* If the definition is a comparison, recurse on it. */
5674 if (TREE_CODE_CLASS (innercode
) == tcc_comparison
)
5676 tree t
= or_comparisons_1 (innercode
,
5677 gimple_assign_rhs1 (stmt
),
5678 gimple_assign_rhs2 (stmt
),
5686 /* If the definition is an AND or OR expression, we may be able to
5687 simplify by reassociating. */
5688 if (TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
5689 && (innercode
== BIT_AND_EXPR
|| innercode
== BIT_IOR_EXPR
))
5691 tree inner1
= gimple_assign_rhs1 (stmt
);
5692 tree inner2
= gimple_assign_rhs2 (stmt
);
5695 tree partial
= NULL_TREE
;
5696 bool is_or
= (innercode
== BIT_IOR_EXPR
);
5698 /* Check for boolean identities that don't require recursive examination
5700 inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
5701 inner1 OR (inner1 AND inner2) => inner1
5702 !inner1 OR (inner1 OR inner2) => true
5703 !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
5705 if (inner1
== true_test_var
)
5706 return (is_or
? var
: inner1
);
5707 else if (inner2
== true_test_var
)
5708 return (is_or
? var
: inner2
);
5709 else if (inner1
== false_test_var
)
5712 : or_var_with_comparison (inner2
, false, code2
, op2a
, op2b
));
5713 else if (inner2
== false_test_var
)
5716 : or_var_with_comparison (inner1
, false, code2
, op2a
, op2b
));
5718 /* Next, redistribute/reassociate the OR across the inner tests.
5719 Compute the first partial result, (inner1 OR (op2a code op2b)) */
5720 if (TREE_CODE (inner1
) == SSA_NAME
5721 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner1
))
5722 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
5723 && (t
= maybe_fold_or_comparisons (gimple_assign_rhs_code (s
),
5724 gimple_assign_rhs1 (s
),
5725 gimple_assign_rhs2 (s
),
5726 code2
, op2a
, op2b
)))
5728 /* Handle the OR case, where we are reassociating:
5729 (inner1 OR inner2) OR (op2a code2 op2b)
5731 If the partial result t is a constant, we win. Otherwise
5732 continue on to try reassociating with the other inner test. */
5735 if (integer_onep (t
))
5736 return boolean_true_node
;
5737 else if (integer_zerop (t
))
5741 /* Handle the AND case, where we are redistributing:
5742 (inner1 AND inner2) OR (op2a code2 op2b)
5743 => (t AND (inner2 OR (op2a code op2b))) */
5744 else if (integer_zerop (t
))
5745 return boolean_false_node
;
5747 /* Save partial result for later. */
5751 /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
5752 if (TREE_CODE (inner2
) == SSA_NAME
5753 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner2
))
5754 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
5755 && (t
= maybe_fold_or_comparisons (gimple_assign_rhs_code (s
),
5756 gimple_assign_rhs1 (s
),
5757 gimple_assign_rhs2 (s
),
5758 code2
, op2a
, op2b
)))
5760 /* Handle the OR case, where we are reassociating:
5761 (inner1 OR inner2) OR (op2a code2 op2b)
5763 => (t OR partial) */
5766 if (integer_zerop (t
))
5768 else if (integer_onep (t
))
5769 return boolean_true_node
;
5770 /* If both are the same, we can apply the identity
5772 else if (partial
&& same_bool_result_p (t
, partial
))
5776 /* Handle the AND case, where we are redistributing:
5777 (inner1 AND inner2) OR (op2a code2 op2b)
5778 => (t AND (inner1 OR (op2a code2 op2b)))
5779 => (t AND partial) */
5782 if (integer_zerop (t
))
5783 return boolean_false_node
;
5786 /* We already got a simplification for the other
5787 operand to the redistributed AND expression. The
5788 interesting case is when at least one is true.
5789 Or, if both are the same, we can apply the identity
5791 if (integer_onep (partial
))
5793 else if (integer_onep (t
))
5795 else if (same_bool_result_p (t
, partial
))
5804 /* Try to simplify the OR of two comparisons defined by
5805 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
5806 If this can be done without constructing an intermediate value,
5807 return the resulting tree; otherwise NULL_TREE is returned.
5808 This function is deliberately asymmetric as it recurses on SSA_DEFs
5809 in the first comparison but not the second. */
5812 or_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
5813 enum tree_code code2
, tree op2a
, tree op2b
)
5815 tree truth_type
= truth_type_for (TREE_TYPE (op1a
));
5817 /* First check for ((x CODE1 y) OR (x CODE2 y)). */
5818 if (operand_equal_p (op1a
, op2a
, 0)
5819 && operand_equal_p (op1b
, op2b
, 0))
5821 /* Result will be either NULL_TREE, or a combined comparison. */
5822 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
5823 TRUTH_ORIF_EXPR
, code1
, code2
,
5824 truth_type
, op1a
, op1b
);
5829 /* Likewise the swapped case of the above. */
5830 if (operand_equal_p (op1a
, op2b
, 0)
5831 && operand_equal_p (op1b
, op2a
, 0))
5833 /* Result will be either NULL_TREE, or a combined comparison. */
5834 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
5835 TRUTH_ORIF_EXPR
, code1
,
5836 swap_tree_comparison (code2
),
5837 truth_type
, op1a
, op1b
);
5842 /* If both comparisons are of the same value against constants, we might
5843 be able to merge them. */
5844 if (operand_equal_p (op1a
, op2a
, 0)
5845 && TREE_CODE (op1b
) == INTEGER_CST
5846 && TREE_CODE (op2b
) == INTEGER_CST
)
5848 int cmp
= tree_int_cst_compare (op1b
, op2b
);
5850 /* If we have (op1a != op1b), we should either be able to
5851 return that or TRUE, depending on whether the constant op1b
5852 also satisfies the other comparison against op2b. */
5853 if (code1
== NE_EXPR
)
5859 case EQ_EXPR
: val
= (cmp
== 0); break;
5860 case NE_EXPR
: val
= (cmp
!= 0); break;
5861 case LT_EXPR
: val
= (cmp
< 0); break;
5862 case GT_EXPR
: val
= (cmp
> 0); break;
5863 case LE_EXPR
: val
= (cmp
<= 0); break;
5864 case GE_EXPR
: val
= (cmp
>= 0); break;
5865 default: done
= false;
5870 return boolean_true_node
;
5872 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5875 /* Likewise if the second comparison is a != comparison. */
5876 else if (code2
== NE_EXPR
)
5882 case EQ_EXPR
: val
= (cmp
== 0); break;
5883 case NE_EXPR
: val
= (cmp
!= 0); break;
5884 case LT_EXPR
: val
= (cmp
> 0); break;
5885 case GT_EXPR
: val
= (cmp
< 0); break;
5886 case LE_EXPR
: val
= (cmp
>= 0); break;
5887 case GE_EXPR
: val
= (cmp
<= 0); break;
5888 default: done
= false;
5893 return boolean_true_node
;
5895 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5899 /* See if an equality test is redundant with the other comparison. */
5900 else if (code1
== EQ_EXPR
)
5905 case EQ_EXPR
: val
= (cmp
== 0); break;
5906 case NE_EXPR
: val
= (cmp
!= 0); break;
5907 case LT_EXPR
: val
= (cmp
< 0); break;
5908 case GT_EXPR
: val
= (cmp
> 0); break;
5909 case LE_EXPR
: val
= (cmp
<= 0); break;
5910 case GE_EXPR
: val
= (cmp
>= 0); break;
5915 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5917 else if (code2
== EQ_EXPR
)
5922 case EQ_EXPR
: val
= (cmp
== 0); break;
5923 case NE_EXPR
: val
= (cmp
!= 0); break;
5924 case LT_EXPR
: val
= (cmp
> 0); break;
5925 case GT_EXPR
: val
= (cmp
< 0); break;
5926 case LE_EXPR
: val
= (cmp
>= 0); break;
5927 case GE_EXPR
: val
= (cmp
<= 0); break;
5932 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5935 /* Chose the less restrictive of two < or <= comparisons. */
5936 else if ((code1
== LT_EXPR
|| code1
== LE_EXPR
)
5937 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
5939 if ((cmp
< 0) || (cmp
== 0 && code1
== LT_EXPR
))
5940 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5942 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5945 /* Likewise chose the less restrictive of two > or >= comparisons. */
5946 else if ((code1
== GT_EXPR
|| code1
== GE_EXPR
)
5947 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
5949 if ((cmp
> 0) || (cmp
== 0 && code1
== GT_EXPR
))
5950 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5952 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5955 /* Check for singleton ranges. */
5957 && ((code1
== LT_EXPR
&& code2
== GT_EXPR
)
5958 || (code1
== GT_EXPR
&& code2
== LT_EXPR
)))
5959 return fold_build2 (NE_EXPR
, boolean_type_node
, op1a
, op2b
);
5961 /* Check for less/greater pairs that don't restrict the range at all. */
5963 && (code1
== LT_EXPR
|| code1
== LE_EXPR
)
5964 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
5965 return boolean_true_node
;
5967 && (code1
== GT_EXPR
|| code1
== GE_EXPR
)
5968 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
5969 return boolean_true_node
;
5972 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
5973 NAME's definition is a truth value. See if there are any simplifications
5974 that can be done against the NAME's definition. */
5975 if (TREE_CODE (op1a
) == SSA_NAME
5976 && (code1
== NE_EXPR
|| code1
== EQ_EXPR
)
5977 && (integer_zerop (op1b
) || integer_onep (op1b
)))
5979 bool invert
= ((code1
== EQ_EXPR
&& integer_zerop (op1b
))
5980 || (code1
== NE_EXPR
&& integer_onep (op1b
)));
5981 gimple
*stmt
= SSA_NAME_DEF_STMT (op1a
);
5982 switch (gimple_code (stmt
))
5985 /* Try to simplify by copy-propagating the definition. */
5986 return or_var_with_comparison (op1a
, invert
, code2
, op2a
, op2b
);
5989 /* If every argument to the PHI produces the same result when
5990 ORed with the second comparison, we win.
5991 Do not do this unless the type is bool since we need a bool
5992 result here anyway. */
5993 if (TREE_CODE (TREE_TYPE (op1a
)) == BOOLEAN_TYPE
)
5995 tree result
= NULL_TREE
;
5997 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
5999 tree arg
= gimple_phi_arg_def (stmt
, i
);
6001 /* If this PHI has itself as an argument, ignore it.
6002 If all the other args produce the same result,
6004 if (arg
== gimple_phi_result (stmt
))
6006 else if (TREE_CODE (arg
) == INTEGER_CST
)
6008 if (invert
? integer_zerop (arg
) : integer_nonzerop (arg
))
6011 result
= boolean_true_node
;
6012 else if (!integer_onep (result
))
6016 result
= fold_build2 (code2
, boolean_type_node
,
6018 else if (!same_bool_comparison_p (result
,
6022 else if (TREE_CODE (arg
) == SSA_NAME
6023 && !SSA_NAME_IS_DEFAULT_DEF (arg
))
6026 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
6027 /* In simple cases we can look through PHI nodes,
6028 but we have to be careful with loops.
6030 if (! dom_info_available_p (CDI_DOMINATORS
)
6031 || gimple_bb (def_stmt
) == gimple_bb (stmt
)
6032 || dominated_by_p (CDI_DOMINATORS
,
6033 gimple_bb (def_stmt
),
6036 temp
= or_var_with_comparison (arg
, invert
, code2
,
6042 else if (!same_bool_result_p (result
, temp
))
6058 /* Try to simplify the OR of two comparisons, specified by
6059 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
6060 If this can be simplified to a single expression (without requiring
6061 introducing more SSA variables to hold intermediate values),
6062 return the resulting tree. Otherwise return NULL_TREE.
6063 If the result expression is non-null, it has boolean type. */
6066 maybe_fold_or_comparisons (enum tree_code code1
, tree op1a
, tree op1b
,
6067 enum tree_code code2
, tree op2a
, tree op2b
)
6069 tree t
= or_comparisons_1 (code1
, op1a
, op1b
, code2
, op2a
, op2b
);
6073 return or_comparisons_1 (code2
, op2a
, op2b
, code1
, op1a
, op1b
);
6077 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
6079 Either NULL_TREE, a simplified but non-constant or a constant
6082 ??? This should go into a gimple-fold-inline.h file to be eventually
6083 privatized with the single valueize function used in the various TUs
6084 to avoid the indirect function call overhead. */
6087 gimple_fold_stmt_to_constant_1 (gimple
*stmt
, tree (*valueize
) (tree
),
6088 tree (*gvalueize
) (tree
))
6090 gimple_match_op res_op
;
6091 /* ??? The SSA propagators do not correctly deal with following SSA use-def
6092 edges if there are intermediate VARYING defs. For this reason
6093 do not follow SSA edges here even though SCCVN can technically
6094 just deal fine with that. */
6095 if (gimple_simplify (stmt
, &res_op
, NULL
, gvalueize
, valueize
))
6097 tree res
= NULL_TREE
;
6098 if (gimple_simplified_result_is_gimple_val (&res_op
))
6099 res
= res_op
.ops
[0];
6100 else if (mprts_hook
)
6101 res
= mprts_hook (&res_op
);
6104 if (dump_file
&& dump_flags
& TDF_DETAILS
)
6106 fprintf (dump_file
, "Match-and-simplified ");
6107 print_gimple_expr (dump_file
, stmt
, 0, TDF_SLIM
);
6108 fprintf (dump_file
, " to ");
6109 print_generic_expr (dump_file
, res
);
6110 fprintf (dump_file
, "\n");
6116 location_t loc
= gimple_location (stmt
);
6117 switch (gimple_code (stmt
))
6121 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
6123 switch (get_gimple_rhs_class (subcode
))
6125 case GIMPLE_SINGLE_RHS
:
6127 tree rhs
= gimple_assign_rhs1 (stmt
);
6128 enum tree_code_class kind
= TREE_CODE_CLASS (subcode
);
6130 if (TREE_CODE (rhs
) == SSA_NAME
)
6132 /* If the RHS is an SSA_NAME, return its known constant value,
6134 return (*valueize
) (rhs
);
6136 /* Handle propagating invariant addresses into address
6138 else if (TREE_CODE (rhs
) == ADDR_EXPR
6139 && !is_gimple_min_invariant (rhs
))
6141 poly_int64 offset
= 0;
6143 base
= get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs
, 0),
6147 && (CONSTANT_CLASS_P (base
)
6148 || decl_address_invariant_p (base
)))
6149 return build_invariant_address (TREE_TYPE (rhs
),
6152 else if (TREE_CODE (rhs
) == CONSTRUCTOR
6153 && TREE_CODE (TREE_TYPE (rhs
)) == VECTOR_TYPE
6154 && known_eq (CONSTRUCTOR_NELTS (rhs
),
6155 TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs
))))
6160 nelts
= CONSTRUCTOR_NELTS (rhs
);
6161 tree_vector_builder
vec (TREE_TYPE (rhs
), nelts
, 1);
6162 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs
), i
, val
)
6164 val
= (*valueize
) (val
);
6165 if (TREE_CODE (val
) == INTEGER_CST
6166 || TREE_CODE (val
) == REAL_CST
6167 || TREE_CODE (val
) == FIXED_CST
)
6168 vec
.quick_push (val
);
6173 return vec
.build ();
6175 if (subcode
== OBJ_TYPE_REF
)
6177 tree val
= (*valueize
) (OBJ_TYPE_REF_EXPR (rhs
));
6178 /* If callee is constant, we can fold away the wrapper. */
6179 if (is_gimple_min_invariant (val
))
6183 if (kind
== tcc_reference
)
6185 if ((TREE_CODE (rhs
) == VIEW_CONVERT_EXPR
6186 || TREE_CODE (rhs
) == REALPART_EXPR
6187 || TREE_CODE (rhs
) == IMAGPART_EXPR
)
6188 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
6190 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
6191 return fold_unary_loc (EXPR_LOCATION (rhs
),
6193 TREE_TYPE (rhs
), val
);
6195 else if (TREE_CODE (rhs
) == BIT_FIELD_REF
6196 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
6198 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
6199 return fold_ternary_loc (EXPR_LOCATION (rhs
),
6201 TREE_TYPE (rhs
), val
,
6202 TREE_OPERAND (rhs
, 1),
6203 TREE_OPERAND (rhs
, 2));
6205 else if (TREE_CODE (rhs
) == MEM_REF
6206 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
6208 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
6209 if (TREE_CODE (val
) == ADDR_EXPR
6210 && is_gimple_min_invariant (val
))
6212 tree tem
= fold_build2 (MEM_REF
, TREE_TYPE (rhs
),
6214 TREE_OPERAND (rhs
, 1));
6219 return fold_const_aggregate_ref_1 (rhs
, valueize
);
6221 else if (kind
== tcc_declaration
)
6222 return get_symbol_constant_value (rhs
);
6226 case GIMPLE_UNARY_RHS
:
6229 case GIMPLE_BINARY_RHS
:
6230 /* Translate &x + CST into an invariant form suitable for
6231 further propagation. */
6232 if (subcode
== POINTER_PLUS_EXPR
)
6234 tree op0
= (*valueize
) (gimple_assign_rhs1 (stmt
));
6235 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
6236 if (TREE_CODE (op0
) == ADDR_EXPR
6237 && TREE_CODE (op1
) == INTEGER_CST
)
6239 tree off
= fold_convert (ptr_type_node
, op1
);
6240 return build_fold_addr_expr_loc
6242 fold_build2 (MEM_REF
,
6243 TREE_TYPE (TREE_TYPE (op0
)),
6244 unshare_expr (op0
), off
));
6247 /* Canonicalize bool != 0 and bool == 0 appearing after
6248 valueization. While gimple_simplify handles this
6249 it can get confused by the ~X == 1 -> X == 0 transform
6250 which we cant reduce to a SSA name or a constant
6251 (and we have no way to tell gimple_simplify to not
6252 consider those transforms in the first place). */
6253 else if (subcode
== EQ_EXPR
6254 || subcode
== NE_EXPR
)
6256 tree lhs
= gimple_assign_lhs (stmt
);
6257 tree op0
= gimple_assign_rhs1 (stmt
);
6258 if (useless_type_conversion_p (TREE_TYPE (lhs
),
6261 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
6262 op0
= (*valueize
) (op0
);
6263 if (TREE_CODE (op0
) == INTEGER_CST
)
6264 std::swap (op0
, op1
);
6265 if (TREE_CODE (op1
) == INTEGER_CST
6266 && ((subcode
== NE_EXPR
&& integer_zerop (op1
))
6267 || (subcode
== EQ_EXPR
&& integer_onep (op1
))))
6273 case GIMPLE_TERNARY_RHS
:
6275 /* Handle ternary operators that can appear in GIMPLE form. */
6276 tree op0
= (*valueize
) (gimple_assign_rhs1 (stmt
));
6277 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
6278 tree op2
= (*valueize
) (gimple_assign_rhs3 (stmt
));
6279 return fold_ternary_loc (loc
, subcode
,
6280 gimple_expr_type (stmt
), op0
, op1
, op2
);
6291 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
6293 if (gimple_call_internal_p (stmt
))
6295 enum tree_code subcode
= ERROR_MARK
;
6296 switch (gimple_call_internal_fn (stmt
))
6298 case IFN_UBSAN_CHECK_ADD
:
6299 subcode
= PLUS_EXPR
;
6301 case IFN_UBSAN_CHECK_SUB
:
6302 subcode
= MINUS_EXPR
;
6304 case IFN_UBSAN_CHECK_MUL
:
6305 subcode
= MULT_EXPR
;
6307 case IFN_BUILTIN_EXPECT
:
6309 tree arg0
= gimple_call_arg (stmt
, 0);
6310 tree op0
= (*valueize
) (arg0
);
6311 if (TREE_CODE (op0
) == INTEGER_CST
)
6318 tree arg0
= gimple_call_arg (stmt
, 0);
6319 tree arg1
= gimple_call_arg (stmt
, 1);
6320 tree op0
= (*valueize
) (arg0
);
6321 tree op1
= (*valueize
) (arg1
);
6323 if (TREE_CODE (op0
) != INTEGER_CST
6324 || TREE_CODE (op1
) != INTEGER_CST
)
6329 /* x * 0 = 0 * x = 0 without overflow. */
6330 if (integer_zerop (op0
) || integer_zerop (op1
))
6331 return build_zero_cst (TREE_TYPE (arg0
));
6334 /* y - y = 0 without overflow. */
6335 if (operand_equal_p (op0
, op1
, 0))
6336 return build_zero_cst (TREE_TYPE (arg0
));
6343 = fold_binary_loc (loc
, subcode
, TREE_TYPE (arg0
), op0
, op1
);
6345 && TREE_CODE (res
) == INTEGER_CST
6346 && !TREE_OVERFLOW (res
))
6351 fn
= (*valueize
) (gimple_call_fn (stmt
));
6352 if (TREE_CODE (fn
) == ADDR_EXPR
6353 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
6354 && DECL_BUILT_IN (TREE_OPERAND (fn
, 0))
6355 && gimple_builtin_call_types_compatible_p (stmt
,
6356 TREE_OPERAND (fn
, 0)))
6358 tree
*args
= XALLOCAVEC (tree
, gimple_call_num_args (stmt
));
6361 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
6362 args
[i
] = (*valueize
) (gimple_call_arg (stmt
, i
));
6363 retval
= fold_builtin_call_array (loc
,
6364 gimple_call_return_type (call_stmt
),
6365 fn
, gimple_call_num_args (stmt
), args
);
6368 /* fold_call_expr wraps the result inside a NOP_EXPR. */
6369 STRIP_NOPS (retval
);
6370 retval
= fold_convert (gimple_call_return_type (call_stmt
),
6383 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
6384 Returns NULL_TREE if folding to a constant is not possible, otherwise
6385 returns a constant according to is_gimple_min_invariant. */
6388 gimple_fold_stmt_to_constant (gimple
*stmt
, tree (*valueize
) (tree
))
6390 tree res
= gimple_fold_stmt_to_constant_1 (stmt
, valueize
);
6391 if (res
&& is_gimple_min_invariant (res
))
6397 /* The following set of functions are supposed to fold references using
6398 their constant initializers. */
6400 /* See if we can find constructor defining value of BASE.
6401 When we know the consructor with constant offset (such as
6402 base is array[40] and we do know constructor of array), then
6403 BIT_OFFSET is adjusted accordingly.
6405 As a special case, return error_mark_node when constructor
6406 is not explicitly available, but it is known to be zero
6407 such as 'static const int a;'. */
6409 get_base_constructor (tree base
, poly_int64_pod
*bit_offset
,
6410 tree (*valueize
)(tree
))
6412 poly_int64 bit_offset2
, size
, max_size
;
6415 if (TREE_CODE (base
) == MEM_REF
)
6417 poly_offset_int boff
= *bit_offset
+ mem_ref_offset (base
) * BITS_PER_UNIT
;
6418 if (!boff
.to_shwi (bit_offset
))
6422 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
6423 base
= valueize (TREE_OPERAND (base
, 0));
6424 if (!base
|| TREE_CODE (base
) != ADDR_EXPR
)
6426 base
= TREE_OPERAND (base
, 0);
6429 && TREE_CODE (base
) == SSA_NAME
)
6430 base
= valueize (base
);
6432 /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
6433 DECL_INITIAL. If BASE is a nested reference into another
6434 ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
6435 the inner reference. */
6436 switch (TREE_CODE (base
))
6441 tree init
= ctor_for_folding (base
);
6443 /* Our semantic is exact opposite of ctor_for_folding;
6444 NULL means unknown, while error_mark_node is 0. */
6445 if (init
== error_mark_node
)
6448 return error_mark_node
;
6452 case VIEW_CONVERT_EXPR
:
6453 return get_base_constructor (TREE_OPERAND (base
, 0),
6454 bit_offset
, valueize
);
6458 base
= get_ref_base_and_extent (base
, &bit_offset2
, &size
, &max_size
,
6460 if (!known_size_p (max_size
) || maybe_ne (size
, max_size
))
6462 *bit_offset
+= bit_offset2
;
6463 return get_base_constructor (base
, bit_offset
, valueize
);
6469 if (CONSTANT_CLASS_P (base
))
6476 /* CTOR is CONSTRUCTOR of an array type. Fold a reference of SIZE bits
6477 to the memory at bit OFFSET. When non-null, TYPE is the expected
6478 type of the reference; otherwise the type of the referenced element
6479 is used instead. When SIZE is zero, attempt to fold a reference to
6480 the entire element which OFFSET refers to. Increment *SUBOFF by
6481 the bit offset of the accessed element. */
6484 fold_array_ctor_reference (tree type
, tree ctor
,
6485 unsigned HOST_WIDE_INT offset
,
6486 unsigned HOST_WIDE_INT size
,
6488 unsigned HOST_WIDE_INT
*suboff
)
6490 offset_int low_bound
;
6491 offset_int elt_size
;
6492 offset_int access_index
;
6493 tree domain_type
= NULL_TREE
;
6494 HOST_WIDE_INT inner_offset
;
6496 /* Compute low bound and elt size. */
6497 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
)
6498 domain_type
= TYPE_DOMAIN (TREE_TYPE (ctor
));
6499 if (domain_type
&& TYPE_MIN_VALUE (domain_type
))
6501 /* Static constructors for variably sized objects makes no sense. */
6502 if (TREE_CODE (TYPE_MIN_VALUE (domain_type
)) != INTEGER_CST
)
6504 low_bound
= wi::to_offset (TYPE_MIN_VALUE (domain_type
));
6508 /* Static constructors for variably sized objects makes no sense. */
6509 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor
)))) != INTEGER_CST
)
6511 elt_size
= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor
))));
6513 /* When TYPE is non-null, verify that it specifies a constant-sized
6514 accessed not larger than size of array element. */
6516 && (!TYPE_SIZE_UNIT (type
)
6517 || TREE_CODE (TYPE_SIZE_UNIT (type
)) != INTEGER_CST
6518 || elt_size
< wi::to_offset (TYPE_SIZE_UNIT (type
))
6522 /* Compute the array index we look for. */
6523 access_index
= wi::udiv_trunc (offset_int (offset
/ BITS_PER_UNIT
),
6525 access_index
+= low_bound
;
6527 /* And offset within the access. */
6528 inner_offset
= offset
% (elt_size
.to_uhwi () * BITS_PER_UNIT
);
6530 /* See if the array field is large enough to span whole access. We do not
6531 care to fold accesses spanning multiple array indexes. */
6532 if (inner_offset
+ size
> elt_size
.to_uhwi () * BITS_PER_UNIT
)
6534 if (tree val
= get_array_ctor_element_at_index (ctor
, access_index
))
6536 if (!size
&& TREE_CODE (val
) != CONSTRUCTOR
)
6538 /* For the final reference to the entire accessed element
6539 (SIZE is zero), reset INNER_OFFSET, disegard TYPE (which
6540 may be null) in favor of the type of the element, and set
6541 SIZE to the size of the accessed element. */
6543 type
= TREE_TYPE (val
);
6544 size
= elt_size
.to_uhwi () * BITS_PER_UNIT
;
6547 *suboff
+= (access_index
* elt_size
* BITS_PER_UNIT
).to_uhwi ();
6548 return fold_ctor_reference (type
, val
, inner_offset
, size
, from_decl
,
6552 /* Memory not explicitly mentioned in constructor is 0 (or
6553 the reference is out of range). */
6554 return type
? build_zero_cst (type
) : NULL_TREE
;
6557 /* CTOR is CONSTRUCTOR of an aggregate or vector. Fold a reference
6558 of SIZE bits to the memory at bit OFFSET. When non-null, TYPE
6559 is the expected type of the reference; otherwise the type of
6560 the referenced member is used instead. When SIZE is zero,
6561 attempt to fold a reference to the entire member which OFFSET
6562 refers to; in this case. Increment *SUBOFF by the bit offset
6563 of the accessed member. */
6566 fold_nonarray_ctor_reference (tree type
, tree ctor
,
6567 unsigned HOST_WIDE_INT offset
,
6568 unsigned HOST_WIDE_INT size
,
6570 unsigned HOST_WIDE_INT
*suboff
)
6572 unsigned HOST_WIDE_INT cnt
;
6575 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
,
6578 tree byte_offset
= DECL_FIELD_OFFSET (cfield
);
6579 tree field_offset
= DECL_FIELD_BIT_OFFSET (cfield
);
6580 tree field_size
= DECL_SIZE (cfield
);
6584 /* Determine the size of the flexible array member from
6585 the size of the initializer provided for it. */
6586 field_size
= TYPE_SIZE (TREE_TYPE (cval
));
6589 /* Variable sized objects in static constructors makes no sense,
6590 but field_size can be NULL for flexible array members. */
6591 gcc_assert (TREE_CODE (field_offset
) == INTEGER_CST
6592 && TREE_CODE (byte_offset
) == INTEGER_CST
6593 && (field_size
!= NULL_TREE
6594 ? TREE_CODE (field_size
) == INTEGER_CST
6595 : TREE_CODE (TREE_TYPE (cfield
)) == ARRAY_TYPE
));
6597 /* Compute bit offset of the field. */
6598 offset_int bitoffset
6599 = (wi::to_offset (field_offset
)
6600 + (wi::to_offset (byte_offset
) << LOG2_BITS_PER_UNIT
));
6601 /* Compute bit offset where the field ends. */
6602 offset_int bitoffset_end
;
6603 if (field_size
!= NULL_TREE
)
6604 bitoffset_end
= bitoffset
+ wi::to_offset (field_size
);
6608 /* Compute the bit offset of the end of the desired access.
6609 As a special case, if the size of the desired access is
6610 zero, assume the access is to the entire field (and let
6611 the caller make any necessary adjustments by storing
6612 the actual bounds of the field in FIELDBOUNDS). */
6613 offset_int access_end
= offset_int (offset
);
6617 access_end
= bitoffset_end
;
6619 /* Is there any overlap between the desired access at
6620 [OFFSET, OFFSET+SIZE) and the offset of the field within
6621 the object at [BITOFFSET, BITOFFSET_END)? */
6622 if (wi::cmps (access_end
, bitoffset
) > 0
6623 && (field_size
== NULL_TREE
6624 || wi::lts_p (offset
, bitoffset_end
)))
6626 *suboff
+= bitoffset
.to_uhwi ();
6628 if (!size
&& TREE_CODE (cval
) != CONSTRUCTOR
)
6630 /* For the final reference to the entire accessed member
6631 (SIZE is zero), reset OFFSET, disegard TYPE (which may
6632 be null) in favor of the type of the member, and set
6633 SIZE to the size of the accessed member. */
6634 offset
= bitoffset
.to_uhwi ();
6635 type
= TREE_TYPE (cval
);
6636 size
= (bitoffset_end
- bitoffset
).to_uhwi ();
6639 /* We do have overlap. Now see if the field is large enough
6640 to cover the access. Give up for accesses that extend
6641 beyond the end of the object or that span multiple fields. */
6642 if (wi::cmps (access_end
, bitoffset_end
) > 0)
6644 if (offset
< bitoffset
)
6647 offset_int inner_offset
= offset_int (offset
) - bitoffset
;
6648 return fold_ctor_reference (type
, cval
,
6649 inner_offset
.to_uhwi (), size
,
6653 /* Memory not explicitly mentioned in constructor is 0. */
6654 return type
? build_zero_cst (type
) : NULL_TREE
;
6657 /* CTOR is value initializing memory. Fold a reference of TYPE and
6658 bit size POLY_SIZE to the memory at bit POLY_OFFSET. When SIZE
6659 is zero, attempt to fold a reference to the entire subobject
6660 which OFFSET refers to. This is used when folding accesses to
6661 string members of aggregates. When non-null, set *SUBOFF to
6662 the bit offset of the accessed subobject. */
6665 fold_ctor_reference (tree type
, tree ctor
, const poly_uint64
&poly_offset
,
6666 const poly_uint64
&poly_size
, tree from_decl
,
6667 unsigned HOST_WIDE_INT
*suboff
/* = NULL */)
6671 /* We found the field with exact match. */
6673 && useless_type_conversion_p (type
, TREE_TYPE (ctor
))
6674 && known_eq (poly_offset
, 0U))
6675 return canonicalize_constructor_val (unshare_expr (ctor
), from_decl
);
6677 /* The remaining optimizations need a constant size and offset. */
6678 unsigned HOST_WIDE_INT size
, offset
;
6679 if (!poly_size
.is_constant (&size
) || !poly_offset
.is_constant (&offset
))
6682 /* We are at the end of walk, see if we can view convert the
6684 if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor
)) && !offset
6685 /* VIEW_CONVERT_EXPR is defined only for matching sizes. */
6686 && !compare_tree_int (TYPE_SIZE (type
), size
)
6687 && !compare_tree_int (TYPE_SIZE (TREE_TYPE (ctor
)), size
))
6689 ret
= canonicalize_constructor_val (unshare_expr (ctor
), from_decl
);
6692 ret
= fold_unary (VIEW_CONVERT_EXPR
, type
, ret
);
6694 STRIP_USELESS_TYPE_CONVERSION (ret
);
6698 /* For constants and byte-aligned/sized reads try to go through
6699 native_encode/interpret. */
6700 if (CONSTANT_CLASS_P (ctor
)
6701 && BITS_PER_UNIT
== 8
6702 && offset
% BITS_PER_UNIT
== 0
6703 && size
% BITS_PER_UNIT
== 0
6704 && size
<= MAX_BITSIZE_MODE_ANY_MODE
)
6706 unsigned char buf
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
6707 int len
= native_encode_expr (ctor
, buf
, size
/ BITS_PER_UNIT
,
6708 offset
/ BITS_PER_UNIT
);
6710 return native_interpret_expr (type
, buf
, len
);
6712 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
6714 unsigned HOST_WIDE_INT dummy
= 0;
6718 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
6719 || TREE_CODE (TREE_TYPE (ctor
)) == VECTOR_TYPE
)
6720 return fold_array_ctor_reference (type
, ctor
, offset
, size
,
6723 return fold_nonarray_ctor_reference (type
, ctor
, offset
, size
,
6730 /* Return the tree representing the element referenced by T if T is an
6731 ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
6732 names using VALUEIZE. Return NULL_TREE otherwise. */
6735 fold_const_aggregate_ref_1 (tree t
, tree (*valueize
) (tree
))
6737 tree ctor
, idx
, base
;
6738 poly_int64 offset
, size
, max_size
;
6742 if (TREE_THIS_VOLATILE (t
))
6746 return get_symbol_constant_value (t
);
6748 tem
= fold_read_from_constant_string (t
);
6752 switch (TREE_CODE (t
))
6755 case ARRAY_RANGE_REF
:
6756 /* Constant indexes are handled well by get_base_constructor.
6757 Only special case variable offsets.
6758 FIXME: This code can't handle nested references with variable indexes
6759 (they will be handled only by iteration of ccp). Perhaps we can bring
6760 get_ref_base_and_extent here and make it use a valueize callback. */
6761 if (TREE_CODE (TREE_OPERAND (t
, 1)) == SSA_NAME
6763 && (idx
= (*valueize
) (TREE_OPERAND (t
, 1)))
6764 && poly_int_tree_p (idx
))
6766 tree low_bound
, unit_size
;
6768 /* If the resulting bit-offset is constant, track it. */
6769 if ((low_bound
= array_ref_low_bound (t
),
6770 poly_int_tree_p (low_bound
))
6771 && (unit_size
= array_ref_element_size (t
),
6772 tree_fits_uhwi_p (unit_size
)))
6774 poly_offset_int woffset
6775 = wi::sext (wi::to_poly_offset (idx
)
6776 - wi::to_poly_offset (low_bound
),
6777 TYPE_PRECISION (TREE_TYPE (idx
)));
6779 if (woffset
.to_shwi (&offset
))
6781 /* TODO: This code seems wrong, multiply then check
6782 to see if it fits. */
6783 offset
*= tree_to_uhwi (unit_size
);
6784 offset
*= BITS_PER_UNIT
;
6786 base
= TREE_OPERAND (t
, 0);
6787 ctor
= get_base_constructor (base
, &offset
, valueize
);
6788 /* Empty constructor. Always fold to 0. */
6789 if (ctor
== error_mark_node
)
6790 return build_zero_cst (TREE_TYPE (t
));
6791 /* Out of bound array access. Value is undefined,
6793 if (maybe_lt (offset
, 0))
6795 /* We can not determine ctor. */
6798 return fold_ctor_reference (TREE_TYPE (t
), ctor
, offset
,
6799 tree_to_uhwi (unit_size
)
6809 case TARGET_MEM_REF
:
6811 base
= get_ref_base_and_extent (t
, &offset
, &size
, &max_size
, &reverse
);
6812 ctor
= get_base_constructor (base
, &offset
, valueize
);
6814 /* Empty constructor. Always fold to 0. */
6815 if (ctor
== error_mark_node
)
6816 return build_zero_cst (TREE_TYPE (t
));
6817 /* We do not know precise address. */
6818 if (!known_size_p (max_size
) || maybe_ne (max_size
, size
))
6820 /* We can not determine ctor. */
6824 /* Out of bound array access. Value is undefined, but don't fold. */
6825 if (maybe_lt (offset
, 0))
6828 return fold_ctor_reference (TREE_TYPE (t
), ctor
, offset
, size
,
6834 tree c
= fold_const_aggregate_ref_1 (TREE_OPERAND (t
, 0), valueize
);
6835 if (c
&& TREE_CODE (c
) == COMPLEX_CST
)
6836 return fold_build1_loc (EXPR_LOCATION (t
),
6837 TREE_CODE (t
), TREE_TYPE (t
), c
);
6849 fold_const_aggregate_ref (tree t
)
6851 return fold_const_aggregate_ref_1 (t
, NULL
);
6854 /* Lookup virtual method with index TOKEN in a virtual table V
6856 Set CAN_REFER if non-NULL to false if method
6857 is not referable or if the virtual table is ill-formed (such as rewriten
6858 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
6861 gimple_get_virt_method_for_vtable (HOST_WIDE_INT token
,
6863 unsigned HOST_WIDE_INT offset
,
6866 tree vtable
= v
, init
, fn
;
6867 unsigned HOST_WIDE_INT size
;
6868 unsigned HOST_WIDE_INT elt_size
, access_index
;
6874 /* First of all double check we have virtual table. */
6875 if (!VAR_P (v
) || !DECL_VIRTUAL_P (v
))
6877 /* Pass down that we lost track of the target. */
6883 init
= ctor_for_folding (v
);
6885 /* The virtual tables should always be born with constructors
6886 and we always should assume that they are avaialble for
6887 folding. At the moment we do not stream them in all cases,
6888 but it should never happen that ctor seem unreachable. */
6890 if (init
== error_mark_node
)
6892 /* Pass down that we lost track of the target. */
6897 gcc_checking_assert (TREE_CODE (TREE_TYPE (v
)) == ARRAY_TYPE
);
6898 size
= tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v
))));
6899 offset
*= BITS_PER_UNIT
;
6900 offset
+= token
* size
;
6902 /* Lookup the value in the constructor that is assumed to be array.
6903 This is equivalent to
6904 fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
6905 offset, size, NULL);
6906 but in a constant time. We expect that frontend produced a simple
6907 array without indexed initializers. */
6909 gcc_checking_assert (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
);
6910 domain_type
= TYPE_DOMAIN (TREE_TYPE (init
));
6911 gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type
)));
6912 elt_size
= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init
))));
6914 access_index
= offset
/ BITS_PER_UNIT
/ elt_size
;
6915 gcc_checking_assert (offset
% (elt_size
* BITS_PER_UNIT
) == 0);
6917 /* This code makes an assumption that there are no
6918 indexed fileds produced by C++ FE, so we can directly index the array. */
6919 if (access_index
< CONSTRUCTOR_NELTS (init
))
6921 fn
= CONSTRUCTOR_ELT (init
, access_index
)->value
;
6922 gcc_checking_assert (!CONSTRUCTOR_ELT (init
, access_index
)->index
);
6928 /* For type inconsistent program we may end up looking up virtual method
6929 in virtual table that does not contain TOKEN entries. We may overrun
6930 the virtual table and pick up a constant or RTTI info pointer.
6931 In any case the call is undefined. */
6933 || (TREE_CODE (fn
) != ADDR_EXPR
&& TREE_CODE (fn
) != FDESC_EXPR
)
6934 || TREE_CODE (TREE_OPERAND (fn
, 0)) != FUNCTION_DECL
)
6935 fn
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
6938 fn
= TREE_OPERAND (fn
, 0);
6940 /* When cgraph node is missing and function is not public, we cannot
6941 devirtualize. This can happen in WHOPR when the actual method
6942 ends up in other partition, because we found devirtualization
6943 possibility too late. */
6944 if (!can_refer_decl_in_current_unit_p (fn
, vtable
))
6955 /* Make sure we create a cgraph node for functions we'll reference.
6956 They can be non-existent if the reference comes from an entry
6957 of an external vtable for example. */
6958 cgraph_node::get_create (fn
);
6963 /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
6964 is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
6965 KNOWN_BINFO carries the binfo describing the true type of
6966 OBJ_TYPE_REF_OBJECT(REF).
6967 Set CAN_REFER if non-NULL to false if method
6968 is not referable or if the virtual table is ill-formed (such as rewriten
6969 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
6972 gimple_get_virt_method_for_binfo (HOST_WIDE_INT token
, tree known_binfo
,
6975 unsigned HOST_WIDE_INT offset
;
6978 v
= BINFO_VTABLE (known_binfo
);
6979 /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */
6983 if (!vtable_pointer_value_to_vtable (v
, &v
, &offset
))
6989 return gimple_get_virt_method_for_vtable (token
, v
, offset
, can_refer
);
6992 /* Given a pointer value T, return a simplified version of an
6993 indirection through T, or NULL_TREE if no simplification is
6994 possible. Note that the resulting type may be different from
6995 the type pointed to in the sense that it is still compatible
6996 from the langhooks point of view. */
6999 gimple_fold_indirect_ref (tree t
)
7001 tree ptype
= TREE_TYPE (t
), type
= TREE_TYPE (ptype
);
7006 subtype
= TREE_TYPE (sub
);
7007 if (!POINTER_TYPE_P (subtype
)
7008 || TYPE_REF_CAN_ALIAS_ALL (ptype
))
7011 if (TREE_CODE (sub
) == ADDR_EXPR
)
7013 tree op
= TREE_OPERAND (sub
, 0);
7014 tree optype
= TREE_TYPE (op
);
7016 if (useless_type_conversion_p (type
, optype
))
7019 /* *(foo *)&fooarray => fooarray[0] */
7020 if (TREE_CODE (optype
) == ARRAY_TYPE
7021 && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype
))) == INTEGER_CST
7022 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
7024 tree type_domain
= TYPE_DOMAIN (optype
);
7025 tree min_val
= size_zero_node
;
7026 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
7027 min_val
= TYPE_MIN_VALUE (type_domain
);
7028 if (TREE_CODE (min_val
) == INTEGER_CST
)
7029 return build4 (ARRAY_REF
, type
, op
, min_val
, NULL_TREE
, NULL_TREE
);
7031 /* *(foo *)&complexfoo => __real__ complexfoo */
7032 else if (TREE_CODE (optype
) == COMPLEX_TYPE
7033 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
7034 return fold_build1 (REALPART_EXPR
, type
, op
);
7035 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
7036 else if (TREE_CODE (optype
) == VECTOR_TYPE
7037 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
7039 tree part_width
= TYPE_SIZE (type
);
7040 tree index
= bitsize_int (0);
7041 return fold_build3 (BIT_FIELD_REF
, type
, op
, part_width
, index
);
7045 /* *(p + CST) -> ... */
7046 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
7047 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
7049 tree addr
= TREE_OPERAND (sub
, 0);
7050 tree off
= TREE_OPERAND (sub
, 1);
7054 addrtype
= TREE_TYPE (addr
);
7056 /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
7057 if (TREE_CODE (addr
) == ADDR_EXPR
7058 && TREE_CODE (TREE_TYPE (addrtype
)) == VECTOR_TYPE
7059 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (addrtype
)))
7060 && tree_fits_uhwi_p (off
))
7062 unsigned HOST_WIDE_INT offset
= tree_to_uhwi (off
);
7063 tree part_width
= TYPE_SIZE (type
);
7064 unsigned HOST_WIDE_INT part_widthi
7065 = tree_to_shwi (part_width
) / BITS_PER_UNIT
;
7066 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
7067 tree index
= bitsize_int (indexi
);
7068 if (known_lt (offset
/ part_widthi
,
7069 TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype
))))
7070 return fold_build3 (BIT_FIELD_REF
, type
, TREE_OPERAND (addr
, 0),
7074 /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
7075 if (TREE_CODE (addr
) == ADDR_EXPR
7076 && TREE_CODE (TREE_TYPE (addrtype
)) == COMPLEX_TYPE
7077 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (addrtype
))))
7079 tree size
= TYPE_SIZE_UNIT (type
);
7080 if (tree_int_cst_equal (size
, off
))
7081 return fold_build1 (IMAGPART_EXPR
, type
, TREE_OPERAND (addr
, 0));
7084 /* *(p + CST) -> MEM_REF <p, CST>. */
7085 if (TREE_CODE (addr
) != ADDR_EXPR
7086 || DECL_P (TREE_OPERAND (addr
, 0)))
7087 return fold_build2 (MEM_REF
, type
,
7089 wide_int_to_tree (ptype
, wi::to_wide (off
)));
7092 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
7093 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
7094 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype
)))) == INTEGER_CST
7095 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (subtype
))))
7098 tree min_val
= size_zero_node
;
7100 sub
= gimple_fold_indirect_ref (sub
);
7102 sub
= build1 (INDIRECT_REF
, TREE_TYPE (subtype
), osub
);
7103 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
7104 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
7105 min_val
= TYPE_MIN_VALUE (type_domain
);
7106 if (TREE_CODE (min_val
) == INTEGER_CST
)
7107 return build4 (ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
, NULL_TREE
);
7113 /* Return true if CODE is an operation that when operating on signed
7114 integer types involves undefined behavior on overflow and the
7115 operation can be expressed with unsigned arithmetic. */
7118 arith_code_with_undefined_signed_overflow (tree_code code
)
7126 case POINTER_PLUS_EXPR
:
7133 /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic
7134 operation that can be transformed to unsigned arithmetic by converting
7135 its operand, carrying out the operation in the corresponding unsigned
7136 type and converting the result back to the original type.
7138 Returns a sequence of statements that replace STMT and also contain
7139 a modified form of STMT itself. */
7142 rewrite_to_defined_overflow (gimple
*stmt
)
7144 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7146 fprintf (dump_file
, "rewriting stmt with undefined signed "
7148 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
7151 tree lhs
= gimple_assign_lhs (stmt
);
7152 tree type
= unsigned_type_for (TREE_TYPE (lhs
));
7153 gimple_seq stmts
= NULL
;
7154 for (unsigned i
= 1; i
< gimple_num_ops (stmt
); ++i
)
7156 tree op
= gimple_op (stmt
, i
);
7157 op
= gimple_convert (&stmts
, type
, op
);
7158 gimple_set_op (stmt
, i
, op
);
7160 gimple_assign_set_lhs (stmt
, make_ssa_name (type
, stmt
));
7161 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
7162 gimple_assign_set_rhs_code (stmt
, PLUS_EXPR
);
7163 gimple_seq_add_stmt (&stmts
, stmt
);
7164 gimple
*cvt
= gimple_build_assign (lhs
, NOP_EXPR
, gimple_assign_lhs (stmt
));
7165 gimple_seq_add_stmt (&stmts
, cvt
);
7171 /* The valueization hook we use for the gimple_build API simplification.
7172 This makes us match fold_buildN behavior by only combining with
7173 statements in the sequence(s) we are currently building. */
7176 gimple_build_valueize (tree op
)
7178 if (gimple_bb (SSA_NAME_DEF_STMT (op
)) == NULL
)
7183 /* Build the expression CODE OP0 of type TYPE with location LOC,
7184 simplifying it first if possible. Returns the built
7185 expression value and appends statements possibly defining it
7189 gimple_build (gimple_seq
*seq
, location_t loc
,
7190 enum tree_code code
, tree type
, tree op0
)
7192 tree res
= gimple_simplify (code
, type
, op0
, seq
, gimple_build_valueize
);
7195 res
= create_tmp_reg_or_ssa_name (type
);
7197 if (code
== REALPART_EXPR
7198 || code
== IMAGPART_EXPR
7199 || code
== VIEW_CONVERT_EXPR
)
7200 stmt
= gimple_build_assign (res
, code
, build1 (code
, type
, op0
));
7202 stmt
= gimple_build_assign (res
, code
, op0
);
7203 gimple_set_location (stmt
, loc
);
7204 gimple_seq_add_stmt_without_update (seq
, stmt
);
7209 /* Build the expression OP0 CODE OP1 of type TYPE with location LOC,
7210 simplifying it first if possible. Returns the built
7211 expression value and appends statements possibly defining it
7215 gimple_build (gimple_seq
*seq
, location_t loc
,
7216 enum tree_code code
, tree type
, tree op0
, tree op1
)
7218 tree res
= gimple_simplify (code
, type
, op0
, op1
, seq
, gimple_build_valueize
);
7221 res
= create_tmp_reg_or_ssa_name (type
);
7222 gimple
*stmt
= gimple_build_assign (res
, code
, op0
, op1
);
7223 gimple_set_location (stmt
, loc
);
7224 gimple_seq_add_stmt_without_update (seq
, stmt
);
7229 /* Build the expression (CODE OP0 OP1 OP2) of type TYPE with location LOC,
7230 simplifying it first if possible. Returns the built
7231 expression value and appends statements possibly defining it
7235 gimple_build (gimple_seq
*seq
, location_t loc
,
7236 enum tree_code code
, tree type
, tree op0
, tree op1
, tree op2
)
7238 tree res
= gimple_simplify (code
, type
, op0
, op1
, op2
,
7239 seq
, gimple_build_valueize
);
7242 res
= create_tmp_reg_or_ssa_name (type
);
7244 if (code
== BIT_FIELD_REF
)
7245 stmt
= gimple_build_assign (res
, code
,
7246 build3 (code
, type
, op0
, op1
, op2
));
7248 stmt
= gimple_build_assign (res
, code
, op0
, op1
, op2
);
7249 gimple_set_location (stmt
, loc
);
7250 gimple_seq_add_stmt_without_update (seq
, stmt
);
7255 /* Build the call FN (ARG0) with a result of type TYPE
7256 (or no result if TYPE is void) with location LOC,
7257 simplifying it first if possible. Returns the built
7258 expression value (or NULL_TREE if TYPE is void) and appends
7259 statements possibly defining it to SEQ. */
7262 gimple_build (gimple_seq
*seq
, location_t loc
, combined_fn fn
,
7263 tree type
, tree arg0
)
7265 tree res
= gimple_simplify (fn
, type
, arg0
, seq
, gimple_build_valueize
);
7269 if (internal_fn_p (fn
))
7270 stmt
= gimple_build_call_internal (as_internal_fn (fn
), 1, arg0
);
7273 tree decl
= builtin_decl_implicit (as_builtin_fn (fn
));
7274 stmt
= gimple_build_call (decl
, 1, arg0
);
7276 if (!VOID_TYPE_P (type
))
7278 res
= create_tmp_reg_or_ssa_name (type
);
7279 gimple_call_set_lhs (stmt
, res
);
7281 gimple_set_location (stmt
, loc
);
7282 gimple_seq_add_stmt_without_update (seq
, stmt
);
7287 /* Build the call FN (ARG0, ARG1) with a result of type TYPE
7288 (or no result if TYPE is void) with location LOC,
7289 simplifying it first if possible. Returns the built
7290 expression value (or NULL_TREE if TYPE is void) and appends
7291 statements possibly defining it to SEQ. */
7294 gimple_build (gimple_seq
*seq
, location_t loc
, combined_fn fn
,
7295 tree type
, tree arg0
, tree arg1
)
7297 tree res
= gimple_simplify (fn
, type
, arg0
, arg1
, seq
, gimple_build_valueize
);
7301 if (internal_fn_p (fn
))
7302 stmt
= gimple_build_call_internal (as_internal_fn (fn
), 2, arg0
, arg1
);
7305 tree decl
= builtin_decl_implicit (as_builtin_fn (fn
));
7306 stmt
= gimple_build_call (decl
, 2, arg0
, arg1
);
7308 if (!VOID_TYPE_P (type
))
7310 res
= create_tmp_reg_or_ssa_name (type
);
7311 gimple_call_set_lhs (stmt
, res
);
7313 gimple_set_location (stmt
, loc
);
7314 gimple_seq_add_stmt_without_update (seq
, stmt
);
7319 /* Build the call FN (ARG0, ARG1, ARG2) with a result of type TYPE
7320 (or no result if TYPE is void) with location LOC,
7321 simplifying it first if possible. Returns the built
7322 expression value (or NULL_TREE if TYPE is void) and appends
7323 statements possibly defining it to SEQ. */
7326 gimple_build (gimple_seq
*seq
, location_t loc
, combined_fn fn
,
7327 tree type
, tree arg0
, tree arg1
, tree arg2
)
7329 tree res
= gimple_simplify (fn
, type
, arg0
, arg1
, arg2
,
7330 seq
, gimple_build_valueize
);
7334 if (internal_fn_p (fn
))
7335 stmt
= gimple_build_call_internal (as_internal_fn (fn
),
7336 3, arg0
, arg1
, arg2
);
7339 tree decl
= builtin_decl_implicit (as_builtin_fn (fn
));
7340 stmt
= gimple_build_call (decl
, 3, arg0
, arg1
, arg2
);
7342 if (!VOID_TYPE_P (type
))
7344 res
= create_tmp_reg_or_ssa_name (type
);
7345 gimple_call_set_lhs (stmt
, res
);
7347 gimple_set_location (stmt
, loc
);
7348 gimple_seq_add_stmt_without_update (seq
, stmt
);
7353 /* Build the conversion (TYPE) OP with a result of type TYPE
7354 with location LOC if such conversion is neccesary in GIMPLE,
7355 simplifying it first.
7356 Returns the built expression value and appends
7357 statements possibly defining it to SEQ. */
7360 gimple_convert (gimple_seq
*seq
, location_t loc
, tree type
, tree op
)
7362 if (useless_type_conversion_p (type
, TREE_TYPE (op
)))
7364 return gimple_build (seq
, loc
, NOP_EXPR
, type
, op
);
7367 /* Build the conversion (ptrofftype) OP with a result of a type
7368 compatible with ptrofftype with location LOC if such conversion
7369 is neccesary in GIMPLE, simplifying it first.
7370 Returns the built expression value and appends
7371 statements possibly defining it to SEQ. */
7374 gimple_convert_to_ptrofftype (gimple_seq
*seq
, location_t loc
, tree op
)
7376 if (ptrofftype_p (TREE_TYPE (op
)))
7378 return gimple_convert (seq
, loc
, sizetype
, op
);
7381 /* Build a vector of type TYPE in which each element has the value OP.
7382 Return a gimple value for the result, appending any new statements
7386 gimple_build_vector_from_val (gimple_seq
*seq
, location_t loc
, tree type
,
7389 if (!TYPE_VECTOR_SUBPARTS (type
).is_constant ()
7390 && !CONSTANT_CLASS_P (op
))
7391 return gimple_build (seq
, loc
, VEC_DUPLICATE_EXPR
, type
, op
);
7393 tree res
, vec
= build_vector_from_val (type
, op
);
7394 if (is_gimple_val (vec
))
7396 if (gimple_in_ssa_p (cfun
))
7397 res
= make_ssa_name (type
);
7399 res
= create_tmp_reg (type
);
7400 gimple
*stmt
= gimple_build_assign (res
, vec
);
7401 gimple_set_location (stmt
, loc
);
7402 gimple_seq_add_stmt_without_update (seq
, stmt
);
7406 /* Build a vector from BUILDER, handling the case in which some elements
7407 are non-constant. Return a gimple value for the result, appending any
7408 new instructions to SEQ.
7410 BUILDER must not have a stepped encoding on entry. This is because
7411 the function is not geared up to handle the arithmetic that would
7412 be needed in the variable case, and any code building a vector that
7413 is known to be constant should use BUILDER->build () directly. */
7416 gimple_build_vector (gimple_seq
*seq
, location_t loc
,
7417 tree_vector_builder
*builder
)
7419 gcc_assert (builder
->nelts_per_pattern () <= 2);
7420 unsigned int encoded_nelts
= builder
->encoded_nelts ();
7421 for (unsigned int i
= 0; i
< encoded_nelts
; ++i
)
7422 if (!TREE_CONSTANT ((*builder
)[i
]))
7424 tree type
= builder
->type ();
7425 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
).to_constant ();
7426 vec
<constructor_elt
, va_gc
> *v
;
7427 vec_alloc (v
, nelts
);
7428 for (i
= 0; i
< nelts
; ++i
)
7429 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, builder
->elt (i
));
7432 if (gimple_in_ssa_p (cfun
))
7433 res
= make_ssa_name (type
);
7435 res
= create_tmp_reg (type
);
7436 gimple
*stmt
= gimple_build_assign (res
, build_constructor (type
, v
));
7437 gimple_set_location (stmt
, loc
);
7438 gimple_seq_add_stmt_without_update (seq
, stmt
);
7441 return builder
->build ();
7444 /* Return true if the result of assignment STMT is known to be non-negative.
7445 If the return value is based on the assumption that signed overflow is
7446 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7447 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7450 gimple_assign_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
7453 enum tree_code code
= gimple_assign_rhs_code (stmt
);
7454 switch (get_gimple_rhs_class (code
))
7456 case GIMPLE_UNARY_RHS
:
7457 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
7458 gimple_expr_type (stmt
),
7459 gimple_assign_rhs1 (stmt
),
7460 strict_overflow_p
, depth
);
7461 case GIMPLE_BINARY_RHS
:
7462 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
7463 gimple_expr_type (stmt
),
7464 gimple_assign_rhs1 (stmt
),
7465 gimple_assign_rhs2 (stmt
),
7466 strict_overflow_p
, depth
);
7467 case GIMPLE_TERNARY_RHS
:
7469 case GIMPLE_SINGLE_RHS
:
7470 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
7471 strict_overflow_p
, depth
);
7472 case GIMPLE_INVALID_RHS
:
7478 /* Return true if return value of call STMT is known to be non-negative.
7479 If the return value is based on the assumption that signed overflow is
7480 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7481 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7484 gimple_call_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
7487 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
7488 gimple_call_arg (stmt
, 0) : NULL_TREE
;
7489 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
7490 gimple_call_arg (stmt
, 1) : NULL_TREE
;
7492 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
7493 gimple_call_combined_fn (stmt
),
7496 strict_overflow_p
, depth
);
7499 /* Return true if return value of call STMT is known to be non-negative.
7500 If the return value is based on the assumption that signed overflow is
7501 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7502 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7505 gimple_phi_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
7508 for (unsigned i
= 0; i
< gimple_phi_num_args (stmt
); ++i
)
7510 tree arg
= gimple_phi_arg_def (stmt
, i
);
7511 if (!tree_single_nonnegative_warnv_p (arg
, strict_overflow_p
, depth
+ 1))
7517 /* Return true if STMT is known to compute a non-negative value.
7518 If the return value is based on the assumption that signed overflow is
7519 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7520 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7523 gimple_stmt_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
7526 switch (gimple_code (stmt
))
7529 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
,
7532 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
,
7535 return gimple_phi_nonnegative_warnv_p (stmt
, strict_overflow_p
,
7542 /* Return true if the floating-point value computed by assignment STMT
7543 is known to have an integer value. We also allow +Inf, -Inf and NaN
7544 to be considered integer values. Return false for signaling NaN.
7546 DEPTH is the current nesting depth of the query. */
7549 gimple_assign_integer_valued_real_p (gimple
*stmt
, int depth
)
7551 enum tree_code code
= gimple_assign_rhs_code (stmt
);
7552 switch (get_gimple_rhs_class (code
))
7554 case GIMPLE_UNARY_RHS
:
7555 return integer_valued_real_unary_p (gimple_assign_rhs_code (stmt
),
7556 gimple_assign_rhs1 (stmt
), depth
);
7557 case GIMPLE_BINARY_RHS
:
7558 return integer_valued_real_binary_p (gimple_assign_rhs_code (stmt
),
7559 gimple_assign_rhs1 (stmt
),
7560 gimple_assign_rhs2 (stmt
), depth
);
7561 case GIMPLE_TERNARY_RHS
:
7563 case GIMPLE_SINGLE_RHS
:
7564 return integer_valued_real_single_p (gimple_assign_rhs1 (stmt
), depth
);
7565 case GIMPLE_INVALID_RHS
:
7571 /* Return true if the floating-point value computed by call STMT is known
7572 to have an integer value. We also allow +Inf, -Inf and NaN to be
7573 considered integer values. Return false for signaling NaN.
7575 DEPTH is the current nesting depth of the query. */
7578 gimple_call_integer_valued_real_p (gimple
*stmt
, int depth
)
7580 tree arg0
= (gimple_call_num_args (stmt
) > 0
7581 ? gimple_call_arg (stmt
, 0)
7583 tree arg1
= (gimple_call_num_args (stmt
) > 1
7584 ? gimple_call_arg (stmt
, 1)
7586 return integer_valued_real_call_p (gimple_call_combined_fn (stmt
),
7590 /* Return true if the floating-point result of phi STMT is known to have
7591 an integer value. We also allow +Inf, -Inf and NaN to be considered
7592 integer values. Return false for signaling NaN.
7594 DEPTH is the current nesting depth of the query. */
7597 gimple_phi_integer_valued_real_p (gimple
*stmt
, int depth
)
7599 for (unsigned i
= 0; i
< gimple_phi_num_args (stmt
); ++i
)
7601 tree arg
= gimple_phi_arg_def (stmt
, i
);
7602 if (!integer_valued_real_single_p (arg
, depth
+ 1))
7608 /* Return true if the floating-point value computed by STMT is known
7609 to have an integer value. We also allow +Inf, -Inf and NaN to be
7610 considered integer values. Return false for signaling NaN.
7612 DEPTH is the current nesting depth of the query. */
7615 gimple_stmt_integer_valued_real_p (gimple
*stmt
, int depth
)
7617 switch (gimple_code (stmt
))
7620 return gimple_assign_integer_valued_real_p (stmt
, depth
);
7622 return gimple_call_integer_valued_real_p (stmt
, depth
);
7624 return gimple_phi_integer_valued_real_p (stmt
, depth
);