1 /* Statement simplification on GIMPLE.
2 Copyright (C) 2010-2016 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 "fold-const.h"
36 #include "stor-layout.h"
38 #include "gimple-fold.h"
40 #include "gimple-iterator.h"
41 #include "tree-into-ssa.h"
44 #include "tree-ssa-propagate.h"
45 #include "ipa-utils.h"
46 #include "tree-ssa-address.h"
47 #include "langhooks.h"
48 #include "gimplify-me.h"
52 #include "gimple-match.h"
53 #include "gomp-constants.h"
54 #include "optabs-query.h"
58 #include "fold-const-call.h"
60 /* Return true when DECL can be referenced from current unit.
61 FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
62 We can get declarations that are not possible to reference for various
65 1) When analyzing C++ virtual tables.
66 C++ virtual tables do have known constructors even
67 when they are keyed to other compilation unit.
68 Those tables can contain pointers to methods and vars
69 in other units. Those methods have both STATIC and EXTERNAL
71 2) In WHOPR mode devirtualization might lead to reference
72 to method that was partitioned elsehwere.
73 In this case we have static VAR_DECL or FUNCTION_DECL
74 that has no corresponding callgraph/varpool node
76 3) COMDAT functions referred by external vtables that
77 we devirtualize only during final compilation stage.
78 At this time we already decided that we will not output
79 the function body and thus we can't reference the symbol
83 can_refer_decl_in_current_unit_p (tree decl
, tree from_decl
)
86 struct cgraph_node
*node
;
89 if (DECL_ABSTRACT_P (decl
))
92 /* We are concerned only about static/external vars and functions. */
93 if ((!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
94 || !VAR_OR_FUNCTION_DECL_P (decl
))
97 /* Static objects can be referred only if they was not optimized out yet. */
98 if (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
100 /* Before we start optimizing unreachable code we can be sure all
101 static objects are defined. */
102 if (symtab
->function_flags_ready
)
104 snode
= symtab_node::get (decl
);
105 if (!snode
|| !snode
->definition
)
107 node
= dyn_cast
<cgraph_node
*> (snode
);
108 return !node
|| !node
->global
.inlined_to
;
111 /* We will later output the initializer, so we can refer to it.
112 So we are concerned only when DECL comes from initializer of
113 external var or var that has been optimized out. */
115 || !VAR_P (from_decl
)
116 || (!DECL_EXTERNAL (from_decl
)
117 && (vnode
= varpool_node::get (from_decl
)) != NULL
118 && vnode
->definition
)
120 && (vnode
= varpool_node::get (from_decl
)) != NULL
121 && vnode
->in_other_partition
))
123 /* We are folding reference from external vtable. The vtable may reffer
124 to a symbol keyed to other compilation unit. The other compilation
125 unit may be in separate DSO and the symbol may be hidden. */
126 if (DECL_VISIBILITY_SPECIFIED (decl
)
127 && DECL_EXTERNAL (decl
)
128 && DECL_VISIBILITY (decl
) != VISIBILITY_DEFAULT
129 && (!(snode
= symtab_node::get (decl
)) || !snode
->in_other_partition
))
131 /* When function is public, we always can introduce new reference.
132 Exception are the COMDAT functions where introducing a direct
133 reference imply need to include function body in the curren tunit. */
134 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
))
136 /* We have COMDAT. We are going to check if we still have definition
137 or if the definition is going to be output in other partition.
138 Bypass this when gimplifying; all needed functions will be produced.
140 As observed in PR20991 for already optimized out comdat virtual functions
141 it may be tempting to not necessarily give up because the copy will be
142 output elsewhere when corresponding vtable is output.
143 This is however not possible - ABI specify that COMDATs are output in
144 units where they are used and when the other unit was compiled with LTO
145 it is possible that vtable was kept public while the function itself
147 if (!symtab
->function_flags_ready
)
150 snode
= symtab_node::get (decl
);
152 || ((!snode
->definition
|| DECL_EXTERNAL (decl
))
153 && (!snode
->in_other_partition
154 || (!snode
->forced_by_abi
&& !snode
->force_output
))))
156 node
= dyn_cast
<cgraph_node
*> (snode
);
157 return !node
|| !node
->global
.inlined_to
;
160 /* Create a temporary for TYPE for a statement STMT. If the current function
161 is in SSA form, a SSA name is created. Otherwise a temporary register
165 create_tmp_reg_or_ssa_name (tree type
, gimple
*stmt
= NULL
)
167 if (gimple_in_ssa_p (cfun
))
168 return make_ssa_name (type
, stmt
);
170 return create_tmp_reg (type
);
173 /* CVAL is value taken from DECL_INITIAL of variable. Try to transform it into
174 acceptable form for is_gimple_min_invariant.
175 FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL. */
178 canonicalize_constructor_val (tree cval
, tree from_decl
)
180 tree orig_cval
= cval
;
182 if (TREE_CODE (cval
) == POINTER_PLUS_EXPR
183 && TREE_CODE (TREE_OPERAND (cval
, 1)) == INTEGER_CST
)
185 tree ptr
= TREE_OPERAND (cval
, 0);
186 if (is_gimple_min_invariant (ptr
))
187 cval
= build1_loc (EXPR_LOCATION (cval
),
188 ADDR_EXPR
, TREE_TYPE (ptr
),
189 fold_build2 (MEM_REF
, TREE_TYPE (TREE_TYPE (ptr
)),
191 fold_convert (ptr_type_node
,
192 TREE_OPERAND (cval
, 1))));
194 if (TREE_CODE (cval
) == ADDR_EXPR
)
196 tree base
= NULL_TREE
;
197 if (TREE_CODE (TREE_OPERAND (cval
, 0)) == COMPOUND_LITERAL_EXPR
)
199 base
= COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval
, 0));
201 TREE_OPERAND (cval
, 0) = base
;
204 base
= get_base_address (TREE_OPERAND (cval
, 0));
208 if (VAR_OR_FUNCTION_DECL_P (base
)
209 && !can_refer_decl_in_current_unit_p (base
, from_decl
))
211 if (TREE_TYPE (base
) == error_mark_node
)
214 TREE_ADDRESSABLE (base
) = 1;
215 else if (TREE_CODE (base
) == FUNCTION_DECL
)
217 /* Make sure we create a cgraph node for functions we'll reference.
218 They can be non-existent if the reference comes from an entry
219 of an external vtable for example. */
220 cgraph_node::get_create (base
);
222 /* Fixup types in global initializers. */
223 if (TREE_TYPE (TREE_TYPE (cval
)) != TREE_TYPE (TREE_OPERAND (cval
, 0)))
224 cval
= build_fold_addr_expr (TREE_OPERAND (cval
, 0));
226 if (!useless_type_conversion_p (TREE_TYPE (orig_cval
), TREE_TYPE (cval
)))
227 cval
= fold_convert (TREE_TYPE (orig_cval
), cval
);
230 if (TREE_OVERFLOW_P (cval
))
231 return drop_tree_overflow (cval
);
235 /* If SYM is a constant variable with known value, return the value.
236 NULL_TREE is returned otherwise. */
239 get_symbol_constant_value (tree sym
)
241 tree val
= ctor_for_folding (sym
);
242 if (val
!= error_mark_node
)
246 val
= canonicalize_constructor_val (unshare_expr (val
), sym
);
247 if (val
&& is_gimple_min_invariant (val
))
252 /* Variables declared 'const' without an initializer
253 have zero as the initializer if they may not be
254 overridden at link or run time. */
256 && is_gimple_reg_type (TREE_TYPE (sym
)))
257 return build_zero_cst (TREE_TYPE (sym
));
265 /* Subroutine of fold_stmt. We perform several simplifications of the
266 memory reference tree EXPR and make sure to re-gimplify them properly
267 after propagation of constant addresses. IS_LHS is true if the
268 reference is supposed to be an lvalue. */
271 maybe_fold_reference (tree expr
, bool is_lhs
)
275 if ((TREE_CODE (expr
) == VIEW_CONVERT_EXPR
276 || TREE_CODE (expr
) == REALPART_EXPR
277 || TREE_CODE (expr
) == IMAGPART_EXPR
)
278 && CONSTANT_CLASS_P (TREE_OPERAND (expr
, 0)))
279 return fold_unary_loc (EXPR_LOCATION (expr
),
282 TREE_OPERAND (expr
, 0));
283 else if (TREE_CODE (expr
) == BIT_FIELD_REF
284 && CONSTANT_CLASS_P (TREE_OPERAND (expr
, 0)))
285 return fold_ternary_loc (EXPR_LOCATION (expr
),
288 TREE_OPERAND (expr
, 0),
289 TREE_OPERAND (expr
, 1),
290 TREE_OPERAND (expr
, 2));
293 && (result
= fold_const_aggregate_ref (expr
))
294 && is_gimple_min_invariant (result
))
301 /* Attempt to fold an assignment statement pointed-to by SI. Returns a
302 replacement rhs for the statement or NULL_TREE if no simplification
303 could be made. It is assumed that the operands have been previously
307 fold_gimple_assign (gimple_stmt_iterator
*si
)
309 gimple
*stmt
= gsi_stmt (*si
);
310 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
311 location_t loc
= gimple_location (stmt
);
313 tree result
= NULL_TREE
;
315 switch (get_gimple_rhs_class (subcode
))
317 case GIMPLE_SINGLE_RHS
:
319 tree rhs
= gimple_assign_rhs1 (stmt
);
321 if (TREE_CLOBBER_P (rhs
))
324 if (REFERENCE_CLASS_P (rhs
))
325 return maybe_fold_reference (rhs
, false);
327 else if (TREE_CODE (rhs
) == OBJ_TYPE_REF
)
329 tree val
= OBJ_TYPE_REF_EXPR (rhs
);
330 if (is_gimple_min_invariant (val
))
332 else if (flag_devirtualize
&& virtual_method_call_p (rhs
))
335 vec
<cgraph_node
*>targets
336 = possible_polymorphic_call_targets (rhs
, stmt
, &final
);
337 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
339 if (dump_enabled_p ())
341 location_t loc
= gimple_location_safe (stmt
);
342 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, loc
,
343 "resolving virtual function address "
344 "reference to function %s\n",
345 targets
.length () == 1
346 ? targets
[0]->name ()
349 if (targets
.length () == 1)
351 val
= fold_convert (TREE_TYPE (val
),
352 build_fold_addr_expr_loc
353 (loc
, targets
[0]->decl
));
354 STRIP_USELESS_TYPE_CONVERSION (val
);
357 /* We can not use __builtin_unreachable here because it
358 can not have address taken. */
359 val
= build_int_cst (TREE_TYPE (val
), 0);
365 else if (TREE_CODE (rhs
) == ADDR_EXPR
)
367 tree ref
= TREE_OPERAND (rhs
, 0);
368 tree tem
= maybe_fold_reference (ref
, true);
370 && TREE_CODE (tem
) == MEM_REF
371 && integer_zerop (TREE_OPERAND (tem
, 1)))
372 result
= fold_convert (TREE_TYPE (rhs
), TREE_OPERAND (tem
, 0));
374 result
= fold_convert (TREE_TYPE (rhs
),
375 build_fold_addr_expr_loc (loc
, tem
));
376 else if (TREE_CODE (ref
) == MEM_REF
377 && integer_zerop (TREE_OPERAND (ref
, 1)))
378 result
= fold_convert (TREE_TYPE (rhs
), TREE_OPERAND (ref
, 0));
382 /* Strip away useless type conversions. Both the
383 NON_LVALUE_EXPR that may have been added by fold, and
384 "useless" type conversions that might now be apparent
385 due to propagation. */
386 STRIP_USELESS_TYPE_CONVERSION (result
);
388 if (result
!= rhs
&& valid_gimple_rhs_p (result
))
393 else if (TREE_CODE (rhs
) == CONSTRUCTOR
394 && TREE_CODE (TREE_TYPE (rhs
)) == VECTOR_TYPE
)
396 /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */
400 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs
), i
, val
)
401 if (! CONSTANT_CLASS_P (val
))
404 return build_vector_from_ctor (TREE_TYPE (rhs
),
405 CONSTRUCTOR_ELTS (rhs
));
408 else if (DECL_P (rhs
))
409 return get_symbol_constant_value (rhs
);
413 case GIMPLE_UNARY_RHS
:
416 case GIMPLE_BINARY_RHS
:
419 case GIMPLE_TERNARY_RHS
:
420 result
= fold_ternary_loc (loc
, subcode
,
421 TREE_TYPE (gimple_assign_lhs (stmt
)),
422 gimple_assign_rhs1 (stmt
),
423 gimple_assign_rhs2 (stmt
),
424 gimple_assign_rhs3 (stmt
));
428 STRIP_USELESS_TYPE_CONVERSION (result
);
429 if (valid_gimple_rhs_p (result
))
434 case GIMPLE_INVALID_RHS
:
442 /* Replace a statement at *SI_P with a sequence of statements in STMTS,
443 adjusting the replacement stmts location and virtual operands.
444 If the statement has a lhs the last stmt in the sequence is expected
445 to assign to that lhs. */
448 gsi_replace_with_seq_vops (gimple_stmt_iterator
*si_p
, gimple_seq stmts
)
450 gimple
*stmt
= gsi_stmt (*si_p
);
452 if (gimple_has_location (stmt
))
453 annotate_all_with_location (stmts
, gimple_location (stmt
));
455 /* First iterate over the replacement statements backward, assigning
456 virtual operands to their defining statements. */
457 gimple
*laststore
= NULL
;
458 for (gimple_stmt_iterator i
= gsi_last (stmts
);
459 !gsi_end_p (i
); gsi_prev (&i
))
461 gimple
*new_stmt
= gsi_stmt (i
);
462 if ((gimple_assign_single_p (new_stmt
)
463 && !is_gimple_reg (gimple_assign_lhs (new_stmt
)))
464 || (is_gimple_call (new_stmt
)
465 && (gimple_call_flags (new_stmt
)
466 & (ECF_NOVOPS
| ECF_PURE
| ECF_CONST
| ECF_NORETURN
)) == 0))
470 vdef
= gimple_vdef (stmt
);
472 vdef
= make_ssa_name (gimple_vop (cfun
), new_stmt
);
473 gimple_set_vdef (new_stmt
, vdef
);
474 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
475 SSA_NAME_DEF_STMT (vdef
) = new_stmt
;
476 laststore
= new_stmt
;
480 /* Second iterate over the statements forward, assigning virtual
481 operands to their uses. */
482 tree reaching_vuse
= gimple_vuse (stmt
);
483 for (gimple_stmt_iterator i
= gsi_start (stmts
);
484 !gsi_end_p (i
); gsi_next (&i
))
486 gimple
*new_stmt
= gsi_stmt (i
);
487 /* If the new statement possibly has a VUSE, update it with exact SSA
488 name we know will reach this one. */
489 if (gimple_has_mem_ops (new_stmt
))
490 gimple_set_vuse (new_stmt
, reaching_vuse
);
491 gimple_set_modified (new_stmt
, true);
492 if (gimple_vdef (new_stmt
))
493 reaching_vuse
= gimple_vdef (new_stmt
);
496 /* If the new sequence does not do a store release the virtual
497 definition of the original statement. */
499 && reaching_vuse
== gimple_vuse (stmt
))
501 tree vdef
= gimple_vdef (stmt
);
503 && TREE_CODE (vdef
) == SSA_NAME
)
505 unlink_stmt_vdef (stmt
);
506 release_ssa_name (vdef
);
510 /* Finally replace the original statement with the sequence. */
511 gsi_replace_with_seq (si_p
, stmts
, false);
514 /* Convert EXPR into a GIMPLE value suitable for substitution on the
515 RHS of an assignment. Insert the necessary statements before
516 iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL
517 is replaced. If the call is expected to produces a result, then it
518 is replaced by an assignment of the new RHS to the result variable.
519 If the result is to be ignored, then the call is replaced by a
520 GIMPLE_NOP. A proper VDEF chain is retained by making the first
521 VUSE and the last VDEF of the whole sequence be the same as the replaced
522 statement and using new SSA names for stores in between. */
525 gimplify_and_update_call_from_tree (gimple_stmt_iterator
*si_p
, tree expr
)
528 gimple
*stmt
, *new_stmt
;
529 gimple_stmt_iterator i
;
530 gimple_seq stmts
= NULL
;
532 stmt
= gsi_stmt (*si_p
);
534 gcc_assert (is_gimple_call (stmt
));
536 push_gimplify_context (gimple_in_ssa_p (cfun
));
538 lhs
= gimple_call_lhs (stmt
);
539 if (lhs
== NULL_TREE
)
541 gimplify_and_add (expr
, &stmts
);
542 /* We can end up with folding a memcpy of an empty class assignment
543 which gets optimized away by C++ gimplification. */
544 if (gimple_seq_empty_p (stmts
))
546 pop_gimplify_context (NULL
);
547 if (gimple_in_ssa_p (cfun
))
549 unlink_stmt_vdef (stmt
);
552 gsi_replace (si_p
, gimple_build_nop (), false);
558 tree tmp
= force_gimple_operand (expr
, &stmts
, false, NULL_TREE
);
559 new_stmt
= gimple_build_assign (lhs
, tmp
);
560 i
= gsi_last (stmts
);
561 gsi_insert_after_without_update (&i
, new_stmt
,
562 GSI_CONTINUE_LINKING
);
565 pop_gimplify_context (NULL
);
567 gsi_replace_with_seq_vops (si_p
, stmts
);
571 /* Replace the call at *GSI with the gimple value VAL. */
574 replace_call_with_value (gimple_stmt_iterator
*gsi
, tree val
)
576 gimple
*stmt
= gsi_stmt (*gsi
);
577 tree lhs
= gimple_call_lhs (stmt
);
581 if (!useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (val
)))
582 val
= fold_convert (TREE_TYPE (lhs
), val
);
583 repl
= gimple_build_assign (lhs
, val
);
586 repl
= gimple_build_nop ();
587 tree vdef
= gimple_vdef (stmt
);
588 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
590 unlink_stmt_vdef (stmt
);
591 release_ssa_name (vdef
);
593 gsi_replace (gsi
, repl
, false);
596 /* Replace the call at *GSI with the new call REPL and fold that
600 replace_call_with_call_and_fold (gimple_stmt_iterator
*gsi
, gimple
*repl
)
602 gimple
*stmt
= gsi_stmt (*gsi
);
603 gimple_call_set_lhs (repl
, gimple_call_lhs (stmt
));
604 gimple_set_location (repl
, gimple_location (stmt
));
605 if (gimple_vdef (stmt
)
606 && TREE_CODE (gimple_vdef (stmt
)) == SSA_NAME
)
608 gimple_set_vdef (repl
, gimple_vdef (stmt
));
609 gimple_set_vuse (repl
, gimple_vuse (stmt
));
610 SSA_NAME_DEF_STMT (gimple_vdef (repl
)) = repl
;
612 gsi_replace (gsi
, repl
, false);
616 /* Return true if VAR is a VAR_DECL or a component thereof. */
619 var_decl_component_p (tree var
)
622 while (handled_component_p (inner
))
623 inner
= TREE_OPERAND (inner
, 0);
624 return SSA_VAR_P (inner
);
627 /* Fold function call to builtin mem{{,p}cpy,move}. Return
628 false if no simplification can be made.
629 If ENDP is 0, return DEST (like memcpy).
630 If ENDP is 1, return DEST+LEN (like mempcpy).
631 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
632 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
636 gimple_fold_builtin_memory_op (gimple_stmt_iterator
*gsi
,
637 tree dest
, tree src
, int endp
)
639 gimple
*stmt
= gsi_stmt (*gsi
);
640 tree lhs
= gimple_call_lhs (stmt
);
641 tree len
= gimple_call_arg (stmt
, 2);
642 tree destvar
, srcvar
;
643 location_t loc
= gimple_location (stmt
);
645 /* If the LEN parameter is zero, return DEST. */
646 if (integer_zerop (len
))
649 if (gimple_call_lhs (stmt
))
650 repl
= gimple_build_assign (gimple_call_lhs (stmt
), dest
);
652 repl
= gimple_build_nop ();
653 tree vdef
= gimple_vdef (stmt
);
654 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
656 unlink_stmt_vdef (stmt
);
657 release_ssa_name (vdef
);
659 gsi_replace (gsi
, repl
, false);
663 /* If SRC and DEST are the same (and not volatile), return
664 DEST{,+LEN,+LEN-1}. */
665 if (operand_equal_p (src
, dest
, 0))
667 unlink_stmt_vdef (stmt
);
668 if (gimple_vdef (stmt
) && TREE_CODE (gimple_vdef (stmt
)) == SSA_NAME
)
669 release_ssa_name (gimple_vdef (stmt
));
672 gsi_replace (gsi
, gimple_build_nop (), false);
679 tree srctype
, desttype
;
680 unsigned int src_align
, dest_align
;
683 /* Inlining of memcpy/memmove may cause bounds lost (if we copy
684 pointers as wide integer) and also may result in huge function
685 size because of inlined bounds copy. Thus don't inline for
686 functions we want to instrument. */
687 if (flag_check_pointer_bounds
688 && chkp_instrumentable_p (cfun
->decl
)
689 /* Even if data may contain pointers we can inline if copy
690 less than a pointer size. */
691 && (!tree_fits_uhwi_p (len
)
692 || compare_tree_int (len
, POINTER_SIZE_UNITS
) >= 0))
695 /* Build accesses at offset zero with a ref-all character type. */
696 off0
= build_int_cst (build_pointer_type_for_mode (char_type_node
,
699 /* If we can perform the copy efficiently with first doing all loads
700 and then all stores inline it that way. Currently efficiently
701 means that we can load all the memory into a single integer
702 register which is what MOVE_MAX gives us. */
703 src_align
= get_pointer_alignment (src
);
704 dest_align
= get_pointer_alignment (dest
);
705 if (tree_fits_uhwi_p (len
)
706 && compare_tree_int (len
, MOVE_MAX
) <= 0
707 /* ??? Don't transform copies from strings with known length this
708 confuses the tree-ssa-strlen.c. This doesn't handle
709 the case in gcc.dg/strlenopt-8.c which is XFAILed for that
711 && !c_strlen (src
, 2))
713 unsigned ilen
= tree_to_uhwi (len
);
714 if (pow2p_hwi (ilen
))
716 tree type
= lang_hooks
.types
.type_for_size (ilen
* 8, 1);
718 && TYPE_MODE (type
) != BLKmode
719 && (GET_MODE_SIZE (TYPE_MODE (type
)) * BITS_PER_UNIT
721 /* If the destination pointer is not aligned we must be able
722 to emit an unaligned store. */
723 && (dest_align
>= GET_MODE_ALIGNMENT (TYPE_MODE (type
))
724 || !SLOW_UNALIGNED_ACCESS (TYPE_MODE (type
), dest_align
)
725 || (optab_handler (movmisalign_optab
, TYPE_MODE (type
))
726 != CODE_FOR_nothing
)))
729 tree desttype
= type
;
730 if (src_align
< GET_MODE_ALIGNMENT (TYPE_MODE (type
)))
731 srctype
= build_aligned_type (type
, src_align
);
732 tree srcmem
= fold_build2 (MEM_REF
, srctype
, src
, off0
);
733 tree tem
= fold_const_aggregate_ref (srcmem
);
736 else if (src_align
< GET_MODE_ALIGNMENT (TYPE_MODE (type
))
737 && SLOW_UNALIGNED_ACCESS (TYPE_MODE (type
),
739 && (optab_handler (movmisalign_optab
,
741 == CODE_FOR_nothing
))
746 if (is_gimple_reg_type (TREE_TYPE (srcmem
)))
748 new_stmt
= gimple_build_assign (NULL_TREE
, srcmem
);
750 = create_tmp_reg_or_ssa_name (TREE_TYPE (srcmem
),
752 gimple_assign_set_lhs (new_stmt
, srcmem
);
753 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
754 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
756 if (dest_align
< GET_MODE_ALIGNMENT (TYPE_MODE (type
)))
757 desttype
= build_aligned_type (type
, dest_align
);
759 = gimple_build_assign (fold_build2 (MEM_REF
, desttype
,
762 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
763 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
764 if (gimple_vdef (new_stmt
)
765 && TREE_CODE (gimple_vdef (new_stmt
)) == SSA_NAME
)
766 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt
)) = new_stmt
;
769 gsi_replace (gsi
, new_stmt
, false);
772 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
781 /* Both DEST and SRC must be pointer types.
782 ??? This is what old code did. Is the testing for pointer types
785 If either SRC is readonly or length is 1, we can use memcpy. */
786 if (!dest_align
|| !src_align
)
788 if (readonly_data_expr (src
)
789 || (tree_fits_uhwi_p (len
)
790 && (MIN (src_align
, dest_align
) / BITS_PER_UNIT
791 >= tree_to_uhwi (len
))))
793 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
796 gimple_call_set_fndecl (stmt
, fn
);
797 gimple_call_set_arg (stmt
, 0, dest
);
798 gimple_call_set_arg (stmt
, 1, src
);
803 /* If *src and *dest can't overlap, optimize into memcpy as well. */
804 if (TREE_CODE (src
) == ADDR_EXPR
805 && TREE_CODE (dest
) == ADDR_EXPR
)
807 tree src_base
, dest_base
, fn
;
808 HOST_WIDE_INT src_offset
= 0, dest_offset
= 0;
809 HOST_WIDE_INT maxsize
;
811 srcvar
= TREE_OPERAND (src
, 0);
812 src_base
= get_addr_base_and_unit_offset (srcvar
, &src_offset
);
813 if (src_base
== NULL
)
815 destvar
= TREE_OPERAND (dest
, 0);
816 dest_base
= get_addr_base_and_unit_offset (destvar
,
818 if (dest_base
== NULL
)
820 if (tree_fits_uhwi_p (len
))
821 maxsize
= tree_to_uhwi (len
);
824 if (SSA_VAR_P (src_base
)
825 && SSA_VAR_P (dest_base
))
827 if (operand_equal_p (src_base
, dest_base
, 0)
828 && ranges_overlap_p (src_offset
, maxsize
,
829 dest_offset
, maxsize
))
832 else if (TREE_CODE (src_base
) == MEM_REF
833 && TREE_CODE (dest_base
) == MEM_REF
)
835 if (! operand_equal_p (TREE_OPERAND (src_base
, 0),
836 TREE_OPERAND (dest_base
, 0), 0))
838 offset_int off
= mem_ref_offset (src_base
) + src_offset
;
839 if (!wi::fits_shwi_p (off
))
841 src_offset
= off
.to_shwi ();
843 off
= mem_ref_offset (dest_base
) + dest_offset
;
844 if (!wi::fits_shwi_p (off
))
846 dest_offset
= off
.to_shwi ();
847 if (ranges_overlap_p (src_offset
, maxsize
,
848 dest_offset
, maxsize
))
854 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
857 gimple_call_set_fndecl (stmt
, fn
);
858 gimple_call_set_arg (stmt
, 0, dest
);
859 gimple_call_set_arg (stmt
, 1, src
);
864 /* If the destination and source do not alias optimize into
866 if ((is_gimple_min_invariant (dest
)
867 || TREE_CODE (dest
) == SSA_NAME
)
868 && (is_gimple_min_invariant (src
)
869 || TREE_CODE (src
) == SSA_NAME
))
872 ao_ref_init_from_ptr_and_size (&destr
, dest
, len
);
873 ao_ref_init_from_ptr_and_size (&srcr
, src
, len
);
874 if (!refs_may_alias_p_1 (&destr
, &srcr
, false))
877 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
880 gimple_call_set_fndecl (stmt
, fn
);
881 gimple_call_set_arg (stmt
, 0, dest
);
882 gimple_call_set_arg (stmt
, 1, src
);
891 if (!tree_fits_shwi_p (len
))
894 This logic lose for arguments like (type *)malloc (sizeof (type)),
895 since we strip the casts of up to VOID return value from malloc.
896 Perhaps we ought to inherit type from non-VOID argument here? */
899 if (!POINTER_TYPE_P (TREE_TYPE (src
))
900 || !POINTER_TYPE_P (TREE_TYPE (dest
)))
902 /* In the following try to find a type that is most natural to be
903 used for the memcpy source and destination and that allows
904 the most optimization when memcpy is turned into a plain assignment
905 using that type. In theory we could always use a char[len] type
906 but that only gains us that the destination and source possibly
907 no longer will have their address taken. */
908 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
909 if (TREE_CODE (src
) == POINTER_PLUS_EXPR
)
911 tree tem
= TREE_OPERAND (src
, 0);
913 if (tem
!= TREE_OPERAND (src
, 0))
914 src
= build1 (NOP_EXPR
, TREE_TYPE (tem
), src
);
916 if (TREE_CODE (dest
) == POINTER_PLUS_EXPR
)
918 tree tem
= TREE_OPERAND (dest
, 0);
920 if (tem
!= TREE_OPERAND (dest
, 0))
921 dest
= build1 (NOP_EXPR
, TREE_TYPE (tem
), dest
);
923 srctype
= TREE_TYPE (TREE_TYPE (src
));
924 if (TREE_CODE (srctype
) == ARRAY_TYPE
925 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype
), len
))
927 srctype
= TREE_TYPE (srctype
);
929 src
= build1 (NOP_EXPR
, build_pointer_type (srctype
), src
);
931 desttype
= TREE_TYPE (TREE_TYPE (dest
));
932 if (TREE_CODE (desttype
) == ARRAY_TYPE
933 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype
), len
))
935 desttype
= TREE_TYPE (desttype
);
937 dest
= build1 (NOP_EXPR
, build_pointer_type (desttype
), dest
);
939 if (TREE_ADDRESSABLE (srctype
)
940 || TREE_ADDRESSABLE (desttype
))
943 /* Make sure we are not copying using a floating-point mode or
944 a type whose size possibly does not match its precision. */
945 if (FLOAT_MODE_P (TYPE_MODE (desttype
))
946 || TREE_CODE (desttype
) == BOOLEAN_TYPE
947 || TREE_CODE (desttype
) == ENUMERAL_TYPE
)
948 desttype
= bitwise_type_for_mode (TYPE_MODE (desttype
));
949 if (FLOAT_MODE_P (TYPE_MODE (srctype
))
950 || TREE_CODE (srctype
) == BOOLEAN_TYPE
951 || TREE_CODE (srctype
) == ENUMERAL_TYPE
)
952 srctype
= bitwise_type_for_mode (TYPE_MODE (srctype
));
960 src_align
= get_pointer_alignment (src
);
961 dest_align
= get_pointer_alignment (dest
);
962 if (dest_align
< TYPE_ALIGN (desttype
)
963 || src_align
< TYPE_ALIGN (srctype
))
967 STRIP_NOPS (destvar
);
968 if (TREE_CODE (destvar
) == ADDR_EXPR
969 && var_decl_component_p (TREE_OPERAND (destvar
, 0))
970 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype
), len
))
971 destvar
= fold_build2 (MEM_REF
, desttype
, destvar
, off0
);
977 if (TREE_CODE (srcvar
) == ADDR_EXPR
978 && var_decl_component_p (TREE_OPERAND (srcvar
, 0))
979 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype
), len
))
982 || src_align
>= TYPE_ALIGN (desttype
))
983 srcvar
= fold_build2 (MEM_REF
, destvar
? desttype
: srctype
,
985 else if (!STRICT_ALIGNMENT
)
987 srctype
= build_aligned_type (TYPE_MAIN_VARIANT (desttype
),
989 srcvar
= fold_build2 (MEM_REF
, srctype
, srcvar
, off0
);
997 if (srcvar
== NULL_TREE
&& destvar
== NULL_TREE
)
1000 if (srcvar
== NULL_TREE
)
1003 if (src_align
>= TYPE_ALIGN (desttype
))
1004 srcvar
= fold_build2 (MEM_REF
, desttype
, src
, off0
);
1007 if (STRICT_ALIGNMENT
)
1009 srctype
= build_aligned_type (TYPE_MAIN_VARIANT (desttype
),
1011 srcvar
= fold_build2 (MEM_REF
, srctype
, src
, off0
);
1014 else if (destvar
== NULL_TREE
)
1017 if (dest_align
>= TYPE_ALIGN (srctype
))
1018 destvar
= fold_build2 (MEM_REF
, srctype
, dest
, off0
);
1021 if (STRICT_ALIGNMENT
)
1023 desttype
= build_aligned_type (TYPE_MAIN_VARIANT (srctype
),
1025 destvar
= fold_build2 (MEM_REF
, desttype
, dest
, off0
);
1030 if (is_gimple_reg_type (TREE_TYPE (srcvar
)))
1032 tree tem
= fold_const_aggregate_ref (srcvar
);
1035 if (! is_gimple_min_invariant (srcvar
))
1037 new_stmt
= gimple_build_assign (NULL_TREE
, srcvar
);
1038 srcvar
= create_tmp_reg_or_ssa_name (TREE_TYPE (srcvar
),
1040 gimple_assign_set_lhs (new_stmt
, srcvar
);
1041 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
1042 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
1045 new_stmt
= gimple_build_assign (destvar
, srcvar
);
1046 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
1047 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
1048 if (gimple_vdef (new_stmt
)
1049 && TREE_CODE (gimple_vdef (new_stmt
)) == SSA_NAME
)
1050 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt
)) = new_stmt
;
1053 gsi_replace (gsi
, new_stmt
, false);
1056 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
1060 gimple_seq stmts
= NULL
;
1061 if (endp
== 0 || endp
== 3)
1064 len
= gimple_build (&stmts
, loc
, MINUS_EXPR
, TREE_TYPE (len
), len
,
1066 if (endp
== 2 || endp
== 1)
1068 len
= gimple_convert_to_ptrofftype (&stmts
, loc
, len
);
1069 dest
= gimple_build (&stmts
, loc
, POINTER_PLUS_EXPR
,
1070 TREE_TYPE (dest
), dest
, len
);
1073 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1074 gimple
*repl
= gimple_build_assign (lhs
, dest
);
1075 gsi_replace (gsi
, repl
, false);
1079 /* Fold function call to builtin memset or bzero at *GSI setting the
1080 memory of size LEN to VAL. Return whether a simplification was made. */
1083 gimple_fold_builtin_memset (gimple_stmt_iterator
*gsi
, tree c
, tree len
)
1085 gimple
*stmt
= gsi_stmt (*gsi
);
1087 unsigned HOST_WIDE_INT length
, cval
;
1089 /* If the LEN parameter is zero, return DEST. */
1090 if (integer_zerop (len
))
1092 replace_call_with_value (gsi
, gimple_call_arg (stmt
, 0));
1096 if (! tree_fits_uhwi_p (len
))
1099 if (TREE_CODE (c
) != INTEGER_CST
)
1102 tree dest
= gimple_call_arg (stmt
, 0);
1104 if (TREE_CODE (var
) != ADDR_EXPR
)
1107 var
= TREE_OPERAND (var
, 0);
1108 if (TREE_THIS_VOLATILE (var
))
1111 etype
= TREE_TYPE (var
);
1112 if (TREE_CODE (etype
) == ARRAY_TYPE
)
1113 etype
= TREE_TYPE (etype
);
1115 if (!INTEGRAL_TYPE_P (etype
)
1116 && !POINTER_TYPE_P (etype
))
1119 if (! var_decl_component_p (var
))
1122 length
= tree_to_uhwi (len
);
1123 if (GET_MODE_SIZE (TYPE_MODE (etype
)) != length
1124 || get_pointer_alignment (dest
) / BITS_PER_UNIT
< length
)
1127 if (length
> HOST_BITS_PER_WIDE_INT
/ BITS_PER_UNIT
)
1130 if (integer_zerop (c
))
1134 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8 || HOST_BITS_PER_WIDE_INT
> 64)
1137 cval
= TREE_INT_CST_LOW (c
);
1141 cval
|= (cval
<< 31) << 1;
1144 var
= fold_build2 (MEM_REF
, etype
, dest
, build_int_cst (ptr_type_node
, 0));
1145 gimple
*store
= gimple_build_assign (var
, build_int_cst_type (etype
, cval
));
1146 gimple_set_vuse (store
, gimple_vuse (stmt
));
1147 tree vdef
= gimple_vdef (stmt
);
1148 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
1150 gimple_set_vdef (store
, gimple_vdef (stmt
));
1151 SSA_NAME_DEF_STMT (gimple_vdef (stmt
)) = store
;
1153 gsi_insert_before (gsi
, store
, GSI_SAME_STMT
);
1154 if (gimple_call_lhs (stmt
))
1156 gimple
*asgn
= gimple_build_assign (gimple_call_lhs (stmt
), dest
);
1157 gsi_replace (gsi
, asgn
, false);
1161 gimple_stmt_iterator gsi2
= *gsi
;
1163 gsi_remove (&gsi2
, true);
1170 /* Obtain the minimum and maximum string length or minimum and maximum
1171 value of ARG in LENGTH[0] and LENGTH[1], respectively.
1172 If ARG is an SSA name variable, follow its use-def chains. When
1173 TYPE == 0, if LENGTH[1] is not equal to the length we determine or
1174 if we are unable to determine the length or value, return False.
1175 VISITED is a bitmap of visited variables.
1176 TYPE is 0 if string length should be obtained, 1 for maximum string
1177 length and 2 for maximum value ARG can have.
1178 When FUZZY is set and the length of a string cannot be determined,
1179 the function instead considers as the maximum possible length the
1180 size of a character array it may refer to. */
1183 get_range_strlen (tree arg
, tree length
[2], bitmap
*visited
, int type
,
1189 /* The minimum and maximum length. The MAXLEN pointer stays unchanged
1190 but MINLEN may be cleared during the execution of the function. */
1191 tree
*minlen
= length
;
1192 tree
*const maxlen
= length
+ 1;
1194 if (TREE_CODE (arg
) != SSA_NAME
)
1196 /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */
1197 if (TREE_CODE (arg
) == ADDR_EXPR
1198 && TREE_CODE (TREE_OPERAND (arg
, 0)) == ARRAY_REF
1199 && integer_zerop (TREE_OPERAND (TREE_OPERAND (arg
, 0), 1)))
1201 tree aop0
= TREE_OPERAND (TREE_OPERAND (arg
, 0), 0);
1202 if (TREE_CODE (aop0
) == INDIRECT_REF
1203 && TREE_CODE (TREE_OPERAND (aop0
, 0)) == SSA_NAME
)
1204 return get_range_strlen (TREE_OPERAND (aop0
, 0),
1205 length
, visited
, type
, fuzzy
);
1211 if (TREE_CODE (val
) != INTEGER_CST
1212 || tree_int_cst_sgn (val
) < 0)
1216 val
= c_strlen (arg
, 1);
1220 if (TREE_CODE (arg
) == ADDR_EXPR
)
1221 return get_range_strlen (TREE_OPERAND (arg
, 0), length
,
1222 visited
, type
, fuzzy
);
1224 if (TREE_CODE (arg
) == COMPONENT_REF
1225 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg
, 1))) == ARRAY_TYPE
)
1227 /* Use the type of the member array to determine the upper
1228 bound on the length of the array. This may be overly
1229 optimistic if the array itself isn't NUL-terminated and
1230 the caller relies on the subsequent member to contain
1232 arg
= TREE_OPERAND (arg
, 1);
1233 val
= TYPE_SIZE_UNIT (TREE_TYPE (arg
));
1234 if (!val
|| integer_zerop (val
))
1236 val
= fold_build2 (MINUS_EXPR
, TREE_TYPE (val
), val
,
1238 /* Avoid using the array size as the minimum. */
1249 && TREE_CODE (*minlen
) == INTEGER_CST
1250 && TREE_CODE (val
) == INTEGER_CST
1251 && tree_int_cst_lt (val
, *minlen
))))
1258 if (TREE_CODE (*maxlen
) != INTEGER_CST
1259 || TREE_CODE (val
) != INTEGER_CST
)
1262 if (tree_int_cst_lt (*maxlen
, val
))
1266 else if (simple_cst_equal (val
, *maxlen
) != 1)
1274 /* If ARG is registered for SSA update we cannot look at its defining
1276 if (name_registered_for_update_p (arg
))
1279 /* If we were already here, break the infinite cycle. */
1281 *visited
= BITMAP_ALLOC (NULL
);
1282 if (!bitmap_set_bit (*visited
, SSA_NAME_VERSION (arg
)))
1286 def_stmt
= SSA_NAME_DEF_STMT (var
);
1288 switch (gimple_code (def_stmt
))
1291 /* The RHS of the statement defining VAR must either have a
1292 constant length or come from another SSA_NAME with a constant
1294 if (gimple_assign_single_p (def_stmt
)
1295 || gimple_assign_unary_nop_p (def_stmt
))
1297 tree rhs
= gimple_assign_rhs1 (def_stmt
);
1298 return get_range_strlen (rhs
, length
, visited
, type
, fuzzy
);
1300 else if (gimple_assign_rhs_code (def_stmt
) == COND_EXPR
)
1302 tree op2
= gimple_assign_rhs2 (def_stmt
);
1303 tree op3
= gimple_assign_rhs3 (def_stmt
);
1304 return get_range_strlen (op2
, length
, visited
, type
, fuzzy
)
1305 && get_range_strlen (op3
, length
, visited
, type
, fuzzy
);
1311 /* All the arguments of the PHI node must have the same constant
1315 for (i
= 0; i
< gimple_phi_num_args (def_stmt
); i
++)
1317 tree arg
= gimple_phi_arg (def_stmt
, i
)->def
;
1319 /* If this PHI has itself as an argument, we cannot
1320 determine the string length of this argument. However,
1321 if we can find a constant string length for the other
1322 PHI args then we can still be sure that this is a
1323 constant string length. So be optimistic and just
1324 continue with the next argument. */
1325 if (arg
== gimple_phi_result (def_stmt
))
1328 if (!get_range_strlen (arg
, length
, visited
, type
, fuzzy
))
1331 *maxlen
= build_all_ones_cst (size_type_node
);
1344 /* Determine the minimum and maximum value or string length that ARG
1345 refers to and store each in the first two elements of MINMAXLEN.
1346 For expressions that point to strings of unknown lengths that are
1347 character arrays, use the upper bound of the array as the maximum
1348 length. For example, given an expression like 'x ? array : "xyz"'
1349 and array declared as 'char array[8]', MINMAXLEN[0] will be set
1350 to 3 and MINMAXLEN[1] to 7, the longest string that could be
1354 void get_range_strlen (tree arg
, tree minmaxlen
[2])
1356 bitmap visited
= NULL
;
1358 minmaxlen
[0] = NULL_TREE
;
1359 minmaxlen
[1] = NULL_TREE
;
1361 get_range_strlen (arg
, minmaxlen
, &visited
, 1, true);
1364 BITMAP_FREE (visited
);
1368 get_maxval_strlen (tree arg
, int type
)
1370 bitmap visited
= NULL
;
1371 tree len
[2] = { NULL_TREE
, NULL_TREE
};
1372 if (!get_range_strlen (arg
, len
, &visited
, type
, false))
1375 BITMAP_FREE (visited
);
1381 /* Fold function call to builtin strcpy with arguments DEST and SRC.
1382 If LEN is not NULL, it represents the length of the string to be
1383 copied. Return NULL_TREE if no simplification can be made. */
1386 gimple_fold_builtin_strcpy (gimple_stmt_iterator
*gsi
,
1387 tree dest
, tree src
)
1389 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1392 /* If SRC and DEST are the same (and not volatile), return DEST. */
1393 if (operand_equal_p (src
, dest
, 0))
1395 replace_call_with_value (gsi
, dest
);
1399 if (optimize_function_for_size_p (cfun
))
1402 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
1406 tree len
= get_maxval_strlen (src
, 0);
1410 len
= fold_convert_loc (loc
, size_type_node
, len
);
1411 len
= size_binop_loc (loc
, PLUS_EXPR
, len
, build_int_cst (size_type_node
, 1));
1412 len
= force_gimple_operand_gsi (gsi
, len
, true,
1413 NULL_TREE
, true, GSI_SAME_STMT
);
1414 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1415 replace_call_with_call_and_fold (gsi
, repl
);
1419 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
1420 If SLEN is not NULL, it represents the length of the source string.
1421 Return NULL_TREE if no simplification can be made. */
1424 gimple_fold_builtin_strncpy (gimple_stmt_iterator
*gsi
,
1425 tree dest
, tree src
, tree len
)
1427 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1430 /* If the LEN parameter is zero, return DEST. */
1431 if (integer_zerop (len
))
1433 replace_call_with_value (gsi
, dest
);
1437 /* We can't compare slen with len as constants below if len is not a
1439 if (TREE_CODE (len
) != INTEGER_CST
)
1442 /* Now, we must be passed a constant src ptr parameter. */
1443 tree slen
= get_maxval_strlen (src
, 0);
1444 if (!slen
|| TREE_CODE (slen
) != INTEGER_CST
)
1447 slen
= size_binop_loc (loc
, PLUS_EXPR
, slen
, ssize_int (1));
1449 /* We do not support simplification of this case, though we do
1450 support it when expanding trees into RTL. */
1451 /* FIXME: generate a call to __builtin_memset. */
1452 if (tree_int_cst_lt (slen
, len
))
1455 /* OK transform into builtin memcpy. */
1456 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
1460 len
= fold_convert_loc (loc
, size_type_node
, len
);
1461 len
= force_gimple_operand_gsi (gsi
, len
, true,
1462 NULL_TREE
, true, GSI_SAME_STMT
);
1463 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1464 replace_call_with_call_and_fold (gsi
, repl
);
1468 /* Fold function call to builtin strchr or strrchr.
1469 If both arguments are constant, evaluate and fold the result,
1470 otherwise simplify str(r)chr (str, 0) into str + strlen (str).
1471 In general strlen is significantly faster than strchr
1472 due to being a simpler operation. */
1474 gimple_fold_builtin_strchr (gimple_stmt_iterator
*gsi
, bool is_strrchr
)
1476 gimple
*stmt
= gsi_stmt (*gsi
);
1477 tree str
= gimple_call_arg (stmt
, 0);
1478 tree c
= gimple_call_arg (stmt
, 1);
1479 location_t loc
= gimple_location (stmt
);
1483 if (!gimple_call_lhs (stmt
))
1486 if ((p
= c_getstr (str
)) && target_char_cst_p (c
, &ch
))
1488 const char *p1
= is_strrchr
? strrchr (p
, ch
) : strchr (p
, ch
);
1492 replace_call_with_value (gsi
, integer_zero_node
);
1496 tree len
= build_int_cst (size_type_node
, p1
- p
);
1497 gimple_seq stmts
= NULL
;
1498 gimple
*new_stmt
= gimple_build_assign (gimple_call_lhs (stmt
),
1499 POINTER_PLUS_EXPR
, str
, len
);
1500 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
1501 gsi_replace_with_seq_vops (gsi
, stmts
);
1505 if (!integer_zerop (c
))
1508 /* Transform strrchr (s, 0) to strchr (s, 0) when optimizing for size. */
1509 if (is_strrchr
&& optimize_function_for_size_p (cfun
))
1511 tree strchr_fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
1515 gimple
*repl
= gimple_build_call (strchr_fn
, 2, str
, c
);
1516 replace_call_with_call_and_fold (gsi
, repl
);
1524 tree strlen_fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
1529 /* Create newstr = strlen (str). */
1530 gimple_seq stmts
= NULL
;
1531 gimple
*new_stmt
= gimple_build_call (strlen_fn
, 1, str
);
1532 gimple_set_location (new_stmt
, loc
);
1533 len
= create_tmp_reg_or_ssa_name (size_type_node
);
1534 gimple_call_set_lhs (new_stmt
, len
);
1535 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
1537 /* Create (str p+ strlen (str)). */
1538 new_stmt
= gimple_build_assign (gimple_call_lhs (stmt
),
1539 POINTER_PLUS_EXPR
, str
, len
);
1540 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
1541 gsi_replace_with_seq_vops (gsi
, stmts
);
1542 /* gsi now points at the assignment to the lhs, get a
1543 stmt iterator to the strlen.
1544 ??? We can't use gsi_for_stmt as that doesn't work when the
1545 CFG isn't built yet. */
1546 gimple_stmt_iterator gsi2
= *gsi
;
1552 /* Fold function call to builtin strstr.
1553 If both arguments are constant, evaluate and fold the result,
1554 additionally fold strstr (x, "") into x and strstr (x, "c")
1555 into strchr (x, 'c'). */
1557 gimple_fold_builtin_strstr (gimple_stmt_iterator
*gsi
)
1559 gimple
*stmt
= gsi_stmt (*gsi
);
1560 tree haystack
= gimple_call_arg (stmt
, 0);
1561 tree needle
= gimple_call_arg (stmt
, 1);
1564 if (!gimple_call_lhs (stmt
))
1567 q
= c_getstr (needle
);
1571 if ((p
= c_getstr (haystack
)))
1573 const char *r
= strstr (p
, q
);
1577 replace_call_with_value (gsi
, integer_zero_node
);
1581 tree len
= build_int_cst (size_type_node
, r
- p
);
1582 gimple_seq stmts
= NULL
;
1584 = gimple_build_assign (gimple_call_lhs (stmt
), POINTER_PLUS_EXPR
,
1586 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
1587 gsi_replace_with_seq_vops (gsi
, stmts
);
1591 /* For strstr (x, "") return x. */
1594 replace_call_with_value (gsi
, haystack
);
1598 /* Transform strstr (x, "c") into strchr (x, 'c'). */
1601 tree strchr_fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
1604 tree c
= build_int_cst (integer_type_node
, q
[0]);
1605 gimple
*repl
= gimple_build_call (strchr_fn
, 2, haystack
, c
);
1606 replace_call_with_call_and_fold (gsi
, repl
);
1614 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
1617 Return NULL_TREE if no simplification was possible, otherwise return the
1618 simplified form of the call as a tree.
1620 The simplified form may be a constant or other expression which
1621 computes the same value, but in a more efficient manner (including
1622 calls to other builtin functions).
1624 The call may contain arguments which need to be evaluated, but
1625 which are not useful to determine the result of the call. In
1626 this case we return a chain of COMPOUND_EXPRs. The LHS of each
1627 COMPOUND_EXPR will be an argument which must be evaluated.
1628 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
1629 COMPOUND_EXPR in the chain will contain the tree for the simplified
1630 form of the builtin function call. */
1633 gimple_fold_builtin_strcat (gimple_stmt_iterator
*gsi
, tree dst
, tree src
)
1635 gimple
*stmt
= gsi_stmt (*gsi
);
1636 location_t loc
= gimple_location (stmt
);
1638 const char *p
= c_getstr (src
);
1640 /* If the string length is zero, return the dst parameter. */
1641 if (p
&& *p
== '\0')
1643 replace_call_with_value (gsi
, dst
);
1647 if (!optimize_bb_for_speed_p (gimple_bb (stmt
)))
1650 /* See if we can store by pieces into (dst + strlen(dst)). */
1652 tree strlen_fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
1653 tree memcpy_fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
1655 if (!strlen_fn
|| !memcpy_fn
)
1658 /* If the length of the source string isn't computable don't
1659 split strcat into strlen and memcpy. */
1660 tree len
= get_maxval_strlen (src
, 0);
1664 /* Create strlen (dst). */
1665 gimple_seq stmts
= NULL
, stmts2
;
1666 gimple
*repl
= gimple_build_call (strlen_fn
, 1, dst
);
1667 gimple_set_location (repl
, loc
);
1668 newdst
= create_tmp_reg_or_ssa_name (size_type_node
);
1669 gimple_call_set_lhs (repl
, newdst
);
1670 gimple_seq_add_stmt_without_update (&stmts
, repl
);
1672 /* Create (dst p+ strlen (dst)). */
1673 newdst
= fold_build_pointer_plus_loc (loc
, dst
, newdst
);
1674 newdst
= force_gimple_operand (newdst
, &stmts2
, true, NULL_TREE
);
1675 gimple_seq_add_seq_without_update (&stmts
, stmts2
);
1677 len
= fold_convert_loc (loc
, size_type_node
, len
);
1678 len
= size_binop_loc (loc
, PLUS_EXPR
, len
,
1679 build_int_cst (size_type_node
, 1));
1680 len
= force_gimple_operand (len
, &stmts2
, true, NULL_TREE
);
1681 gimple_seq_add_seq_without_update (&stmts
, stmts2
);
1683 repl
= gimple_build_call (memcpy_fn
, 3, newdst
, src
, len
);
1684 gimple_seq_add_stmt_without_update (&stmts
, repl
);
1685 if (gimple_call_lhs (stmt
))
1687 repl
= gimple_build_assign (gimple_call_lhs (stmt
), dst
);
1688 gimple_seq_add_stmt_without_update (&stmts
, repl
);
1689 gsi_replace_with_seq_vops (gsi
, stmts
);
1690 /* gsi now points at the assignment to the lhs, get a
1691 stmt iterator to the memcpy call.
1692 ??? We can't use gsi_for_stmt as that doesn't work when the
1693 CFG isn't built yet. */
1694 gimple_stmt_iterator gsi2
= *gsi
;
1700 gsi_replace_with_seq_vops (gsi
, stmts
);
1706 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
1707 are the arguments to the call. */
1710 gimple_fold_builtin_strcat_chk (gimple_stmt_iterator
*gsi
)
1712 gimple
*stmt
= gsi_stmt (*gsi
);
1713 tree dest
= gimple_call_arg (stmt
, 0);
1714 tree src
= gimple_call_arg (stmt
, 1);
1715 tree size
= gimple_call_arg (stmt
, 2);
1721 /* If the SRC parameter is "", return DEST. */
1722 if (p
&& *p
== '\0')
1724 replace_call_with_value (gsi
, dest
);
1728 if (! tree_fits_uhwi_p (size
) || ! integer_all_onesp (size
))
1731 /* If __builtin_strcat_chk is used, assume strcat is available. */
1732 fn
= builtin_decl_explicit (BUILT_IN_STRCAT
);
1736 gimple
*repl
= gimple_build_call (fn
, 2, dest
, src
);
1737 replace_call_with_call_and_fold (gsi
, repl
);
1741 /* Simplify a call to the strncat builtin. */
1744 gimple_fold_builtin_strncat (gimple_stmt_iterator
*gsi
)
1746 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
1747 tree dst
= gimple_call_arg (stmt
, 0);
1748 tree src
= gimple_call_arg (stmt
, 1);
1749 tree len
= gimple_call_arg (stmt
, 2);
1751 const char *p
= c_getstr (src
);
1753 /* If the requested length is zero, or the src parameter string
1754 length is zero, return the dst parameter. */
1755 if (integer_zerop (len
) || (p
&& *p
== '\0'))
1757 replace_call_with_value (gsi
, dst
);
1761 /* If the requested len is greater than or equal to the string
1762 length, call strcat. */
1763 if (TREE_CODE (len
) == INTEGER_CST
&& p
1764 && compare_tree_int (len
, strlen (p
)) >= 0)
1766 tree fn
= builtin_decl_implicit (BUILT_IN_STRCAT
);
1768 /* If the replacement _DECL isn't initialized, don't do the
1773 gcall
*repl
= gimple_build_call (fn
, 2, dst
, src
);
1774 replace_call_with_call_and_fold (gsi
, repl
);
1781 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
1785 gimple_fold_builtin_strncat_chk (gimple_stmt_iterator
*gsi
)
1787 gimple
*stmt
= gsi_stmt (*gsi
);
1788 tree dest
= gimple_call_arg (stmt
, 0);
1789 tree src
= gimple_call_arg (stmt
, 1);
1790 tree len
= gimple_call_arg (stmt
, 2);
1791 tree size
= gimple_call_arg (stmt
, 3);
1796 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
1797 if ((p
&& *p
== '\0')
1798 || integer_zerop (len
))
1800 replace_call_with_value (gsi
, dest
);
1804 if (! tree_fits_uhwi_p (size
))
1807 if (! integer_all_onesp (size
))
1809 tree src_len
= c_strlen (src
, 1);
1811 && tree_fits_uhwi_p (src_len
)
1812 && tree_fits_uhwi_p (len
)
1813 && ! tree_int_cst_lt (len
, src_len
))
1815 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
1816 fn
= builtin_decl_explicit (BUILT_IN_STRCAT_CHK
);
1820 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, size
);
1821 replace_call_with_call_and_fold (gsi
, repl
);
1827 /* If __builtin_strncat_chk is used, assume strncat is available. */
1828 fn
= builtin_decl_explicit (BUILT_IN_STRNCAT
);
1832 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1833 replace_call_with_call_and_fold (gsi
, repl
);
1837 /* Build and append gimple statements to STMTS that would load a first
1838 character of a memory location identified by STR. LOC is location
1839 of the statement. */
1842 gimple_load_first_char (location_t loc
, tree str
, gimple_seq
*stmts
)
1846 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
1847 tree cst_uchar_ptr_node
1848 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
1849 tree off0
= build_int_cst (cst_uchar_ptr_node
, 0);
1851 tree temp
= fold_build2_loc (loc
, MEM_REF
, cst_uchar_node
, str
, off0
);
1852 gassign
*stmt
= gimple_build_assign (NULL_TREE
, temp
);
1853 var
= create_tmp_reg_or_ssa_name (cst_uchar_node
, stmt
);
1855 gimple_assign_set_lhs (stmt
, var
);
1856 gimple_seq_add_stmt_without_update (stmts
, stmt
);
1861 /* Fold a call to the str{n}{case}cmp builtin pointed by GSI iterator.
1862 FCODE is the name of the builtin. */
1865 gimple_fold_builtin_string_compare (gimple_stmt_iterator
*gsi
)
1867 gimple
*stmt
= gsi_stmt (*gsi
);
1868 tree callee
= gimple_call_fndecl (stmt
);
1869 enum built_in_function fcode
= DECL_FUNCTION_CODE (callee
);
1871 tree type
= integer_type_node
;
1872 tree str1
= gimple_call_arg (stmt
, 0);
1873 tree str2
= gimple_call_arg (stmt
, 1);
1874 tree lhs
= gimple_call_lhs (stmt
);
1875 HOST_WIDE_INT length
= -1;
1877 /* Handle strncmp and strncasecmp functions. */
1878 if (gimple_call_num_args (stmt
) == 3)
1880 tree len
= gimple_call_arg (stmt
, 2);
1881 if (tree_fits_uhwi_p (len
))
1882 length
= tree_to_uhwi (len
);
1885 /* If the LEN parameter is zero, return zero. */
1888 replace_call_with_value (gsi
, integer_zero_node
);
1892 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
1893 if (operand_equal_p (str1
, str2
, 0))
1895 replace_call_with_value (gsi
, integer_zero_node
);
1899 const char *p1
= c_getstr (str1
);
1900 const char *p2
= c_getstr (str2
);
1902 /* For known strings, return an immediate value. */
1906 bool known_result
= false;
1910 case BUILT_IN_STRCMP
:
1912 r
= strcmp (p1
, p2
);
1913 known_result
= true;
1916 case BUILT_IN_STRNCMP
:
1920 r
= strncmp (p1
, p2
, length
);
1921 known_result
= true;
1924 /* Only handleable situation is where the string are equal (result 0),
1925 which is already handled by operand_equal_p case. */
1926 case BUILT_IN_STRCASECMP
:
1928 case BUILT_IN_STRNCASECMP
:
1932 r
= strncmp (p1
, p2
, length
);
1934 known_result
= true;
1943 replace_call_with_value (gsi
, build_cmp_result (type
, r
));
1948 bool nonzero_length
= length
>= 1
1949 || fcode
== BUILT_IN_STRCMP
1950 || fcode
== BUILT_IN_STRCASECMP
;
1952 location_t loc
= gimple_location (stmt
);
1954 /* If the second arg is "", return *(const unsigned char*)arg1. */
1955 if (p2
&& *p2
== '\0' && nonzero_length
)
1957 gimple_seq stmts
= NULL
;
1958 tree var
= gimple_load_first_char (loc
, str1
, &stmts
);
1961 stmt
= gimple_build_assign (lhs
, NOP_EXPR
, var
);
1962 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
1965 gsi_replace_with_seq_vops (gsi
, stmts
);
1969 /* If the first arg is "", return -*(const unsigned char*)arg2. */
1970 if (p1
&& *p1
== '\0' && nonzero_length
)
1972 gimple_seq stmts
= NULL
;
1973 tree var
= gimple_load_first_char (loc
, str2
, &stmts
);
1977 tree c
= create_tmp_reg_or_ssa_name (integer_type_node
);
1978 stmt
= gimple_build_assign (c
, NOP_EXPR
, var
);
1979 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
1981 stmt
= gimple_build_assign (lhs
, NEGATE_EXPR
, c
);
1982 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
1985 gsi_replace_with_seq_vops (gsi
, stmts
);
1989 /* If len parameter is one, return an expression corresponding to
1990 (*(const unsigned char*)arg2 - *(const unsigned char*)arg1). */
1991 if (fcode
== BUILT_IN_STRNCMP
&& length
== 1)
1993 gimple_seq stmts
= NULL
;
1994 tree temp1
= gimple_load_first_char (loc
, str1
, &stmts
);
1995 tree temp2
= gimple_load_first_char (loc
, str2
, &stmts
);
1999 tree c1
= create_tmp_reg_or_ssa_name (integer_type_node
);
2000 gassign
*convert1
= gimple_build_assign (c1
, NOP_EXPR
, temp1
);
2001 gimple_seq_add_stmt_without_update (&stmts
, convert1
);
2003 tree c2
= create_tmp_reg_or_ssa_name (integer_type_node
);
2004 gassign
*convert2
= gimple_build_assign (c2
, NOP_EXPR
, temp2
);
2005 gimple_seq_add_stmt_without_update (&stmts
, convert2
);
2007 stmt
= gimple_build_assign (lhs
, MINUS_EXPR
, c1
, c2
);
2008 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2011 gsi_replace_with_seq_vops (gsi
, stmts
);
2018 /* Fold a call to the memchr pointed by GSI iterator. */
2021 gimple_fold_builtin_memchr (gimple_stmt_iterator
*gsi
)
2023 gimple
*stmt
= gsi_stmt (*gsi
);
2024 tree lhs
= gimple_call_lhs (stmt
);
2025 tree arg1
= gimple_call_arg (stmt
, 0);
2026 tree arg2
= gimple_call_arg (stmt
, 1);
2027 tree len
= gimple_call_arg (stmt
, 2);
2029 /* If the LEN parameter is zero, return zero. */
2030 if (integer_zerop (len
))
2032 replace_call_with_value (gsi
, build_int_cst (ptr_type_node
, 0));
2037 if (TREE_CODE (arg2
) != INTEGER_CST
2038 || !tree_fits_uhwi_p (len
)
2039 || !target_char_cst_p (arg2
, &c
))
2042 unsigned HOST_WIDE_INT length
= tree_to_uhwi (len
);
2043 unsigned HOST_WIDE_INT string_length
;
2044 const char *p1
= c_getstr (arg1
, &string_length
);
2048 const char *r
= (const char *)memchr (p1
, c
, MIN (length
, string_length
));
2051 if (length
<= string_length
)
2053 replace_call_with_value (gsi
, build_int_cst (ptr_type_node
, 0));
2059 unsigned HOST_WIDE_INT offset
= r
- p1
;
2060 gimple_seq stmts
= NULL
;
2061 if (lhs
!= NULL_TREE
)
2063 tree offset_cst
= build_int_cst (TREE_TYPE (len
), offset
);
2064 gassign
*stmt
= gimple_build_assign (lhs
, POINTER_PLUS_EXPR
,
2066 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2069 gimple_seq_add_stmt_without_update (&stmts
,
2070 gimple_build_nop ());
2072 gsi_replace_with_seq_vops (gsi
, stmts
);
2080 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
2081 to the call. IGNORE is true if the value returned
2082 by the builtin will be ignored. UNLOCKED is true is true if this
2083 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
2084 the known length of the string. Return NULL_TREE if no simplification
2088 gimple_fold_builtin_fputs (gimple_stmt_iterator
*gsi
,
2089 tree arg0
, tree arg1
,
2092 gimple
*stmt
= gsi_stmt (*gsi
);
2094 /* If we're using an unlocked function, assume the other unlocked
2095 functions exist explicitly. */
2096 tree
const fn_fputc
= (unlocked
2097 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED
)
2098 : builtin_decl_implicit (BUILT_IN_FPUTC
));
2099 tree
const fn_fwrite
= (unlocked
2100 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED
)
2101 : builtin_decl_implicit (BUILT_IN_FWRITE
));
2103 /* If the return value is used, don't do the transformation. */
2104 if (gimple_call_lhs (stmt
))
2107 /* Get the length of the string passed to fputs. If the length
2108 can't be determined, punt. */
2109 tree len
= get_maxval_strlen (arg0
, 0);
2111 || TREE_CODE (len
) != INTEGER_CST
)
2114 switch (compare_tree_int (len
, 1))
2116 case -1: /* length is 0, delete the call entirely . */
2117 replace_call_with_value (gsi
, integer_zero_node
);
2120 case 0: /* length is 1, call fputc. */
2122 const char *p
= c_getstr (arg0
);
2128 gimple
*repl
= gimple_build_call (fn_fputc
, 2,
2130 (integer_type_node
, p
[0]), arg1
);
2131 replace_call_with_call_and_fold (gsi
, repl
);
2136 case 1: /* length is greater than 1, call fwrite. */
2138 /* If optimizing for size keep fputs. */
2139 if (optimize_function_for_size_p (cfun
))
2141 /* New argument list transforming fputs(string, stream) to
2142 fwrite(string, 1, len, stream). */
2146 gimple
*repl
= gimple_build_call (fn_fwrite
, 4, arg0
,
2147 size_one_node
, len
, arg1
);
2148 replace_call_with_call_and_fold (gsi
, repl
);
2157 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
2158 DEST, SRC, LEN, and SIZE are the arguments to the call.
2159 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
2160 code of the builtin. If MAXLEN is not NULL, it is maximum length
2161 passed as third argument. */
2164 gimple_fold_builtin_memory_chk (gimple_stmt_iterator
*gsi
,
2165 tree dest
, tree src
, tree len
, tree size
,
2166 enum built_in_function fcode
)
2168 gimple
*stmt
= gsi_stmt (*gsi
);
2169 location_t loc
= gimple_location (stmt
);
2170 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
2173 /* If SRC and DEST are the same (and not volatile), return DEST
2174 (resp. DEST+LEN for __mempcpy_chk). */
2175 if (fcode
!= BUILT_IN_MEMSET_CHK
&& operand_equal_p (src
, dest
, 0))
2177 if (fcode
!= BUILT_IN_MEMPCPY_CHK
)
2179 replace_call_with_value (gsi
, dest
);
2184 gimple_seq stmts
= NULL
;
2185 len
= gimple_convert_to_ptrofftype (&stmts
, loc
, len
);
2186 tree temp
= gimple_build (&stmts
, loc
, POINTER_PLUS_EXPR
,
2187 TREE_TYPE (dest
), dest
, len
);
2188 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2189 replace_call_with_value (gsi
, temp
);
2194 if (! tree_fits_uhwi_p (size
))
2197 tree maxlen
= get_maxval_strlen (len
, 2);
2198 if (! integer_all_onesp (size
))
2200 if (! tree_fits_uhwi_p (len
))
2202 /* If LEN is not constant, try MAXLEN too.
2203 For MAXLEN only allow optimizing into non-_ocs function
2204 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2205 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
2207 if (fcode
== BUILT_IN_MEMPCPY_CHK
&& ignore
)
2209 /* (void) __mempcpy_chk () can be optimized into
2210 (void) __memcpy_chk (). */
2211 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
2215 gimple
*repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
2216 replace_call_with_call_and_fold (gsi
, repl
);
2225 if (tree_int_cst_lt (size
, maxlen
))
2230 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
2231 mem{cpy,pcpy,move,set} is available. */
2234 case BUILT_IN_MEMCPY_CHK
:
2235 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY
);
2237 case BUILT_IN_MEMPCPY_CHK
:
2238 fn
= builtin_decl_explicit (BUILT_IN_MEMPCPY
);
2240 case BUILT_IN_MEMMOVE_CHK
:
2241 fn
= builtin_decl_explicit (BUILT_IN_MEMMOVE
);
2243 case BUILT_IN_MEMSET_CHK
:
2244 fn
= builtin_decl_explicit (BUILT_IN_MEMSET
);
2253 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
2254 replace_call_with_call_and_fold (gsi
, repl
);
2258 /* Fold a call to the __st[rp]cpy_chk builtin.
2259 DEST, SRC, and SIZE are the arguments to the call.
2260 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
2261 code of the builtin. If MAXLEN is not NULL, it is maximum length of
2262 strings passed as second argument. */
2265 gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator
*gsi
,
2267 tree src
, tree size
,
2268 enum built_in_function fcode
)
2270 gimple
*stmt
= gsi_stmt (*gsi
);
2271 location_t loc
= gimple_location (stmt
);
2272 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
2275 /* If SRC and DEST are the same (and not volatile), return DEST. */
2276 if (fcode
== BUILT_IN_STRCPY_CHK
&& operand_equal_p (src
, dest
, 0))
2278 replace_call_with_value (gsi
, dest
);
2282 if (! tree_fits_uhwi_p (size
))
2285 tree maxlen
= get_maxval_strlen (src
, 1);
2286 if (! integer_all_onesp (size
))
2288 len
= c_strlen (src
, 1);
2289 if (! len
|| ! tree_fits_uhwi_p (len
))
2291 /* If LEN is not constant, try MAXLEN too.
2292 For MAXLEN only allow optimizing into non-_ocs function
2293 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2294 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
2296 if (fcode
== BUILT_IN_STPCPY_CHK
)
2301 /* If return value of __stpcpy_chk is ignored,
2302 optimize into __strcpy_chk. */
2303 fn
= builtin_decl_explicit (BUILT_IN_STRCPY_CHK
);
2307 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, size
);
2308 replace_call_with_call_and_fold (gsi
, repl
);
2312 if (! len
|| TREE_SIDE_EFFECTS (len
))
2315 /* If c_strlen returned something, but not a constant,
2316 transform __strcpy_chk into __memcpy_chk. */
2317 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
2321 gimple_seq stmts
= NULL
;
2322 len
= gimple_convert (&stmts
, loc
, size_type_node
, len
);
2323 len
= gimple_build (&stmts
, loc
, PLUS_EXPR
, size_type_node
, len
,
2324 build_int_cst (size_type_node
, 1));
2325 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2326 gimple
*repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
2327 replace_call_with_call_and_fold (gsi
, repl
);
2334 if (! tree_int_cst_lt (maxlen
, size
))
2338 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
2339 fn
= builtin_decl_explicit (fcode
== BUILT_IN_STPCPY_CHK
2340 ? BUILT_IN_STPCPY
: BUILT_IN_STRCPY
);
2344 gimple
*repl
= gimple_build_call (fn
, 2, dest
, src
);
2345 replace_call_with_call_and_fold (gsi
, repl
);
2349 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
2350 are the arguments to the call. If MAXLEN is not NULL, it is maximum
2351 length passed as third argument. IGNORE is true if return value can be
2352 ignored. FCODE is the BUILT_IN_* code of the builtin. */
2355 gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator
*gsi
,
2356 tree dest
, tree src
,
2357 tree len
, tree size
,
2358 enum built_in_function fcode
)
2360 gimple
*stmt
= gsi_stmt (*gsi
);
2361 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
2364 if (fcode
== BUILT_IN_STPNCPY_CHK
&& ignore
)
2366 /* If return value of __stpncpy_chk is ignored,
2367 optimize into __strncpy_chk. */
2368 fn
= builtin_decl_explicit (BUILT_IN_STRNCPY_CHK
);
2371 gimple
*repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
2372 replace_call_with_call_and_fold (gsi
, repl
);
2377 if (! tree_fits_uhwi_p (size
))
2380 tree maxlen
= get_maxval_strlen (len
, 2);
2381 if (! integer_all_onesp (size
))
2383 if (! tree_fits_uhwi_p (len
))
2385 /* If LEN is not constant, try MAXLEN too.
2386 For MAXLEN only allow optimizing into non-_ocs function
2387 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2388 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
2394 if (tree_int_cst_lt (size
, maxlen
))
2398 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
2399 fn
= builtin_decl_explicit (fcode
== BUILT_IN_STPNCPY_CHK
2400 ? BUILT_IN_STPNCPY
: BUILT_IN_STRNCPY
);
2404 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
2405 replace_call_with_call_and_fold (gsi
, repl
);
2409 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
2410 Return NULL_TREE if no simplification can be made. */
2413 gimple_fold_builtin_stpcpy (gimple_stmt_iterator
*gsi
)
2415 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
2416 location_t loc
= gimple_location (stmt
);
2417 tree dest
= gimple_call_arg (stmt
, 0);
2418 tree src
= gimple_call_arg (stmt
, 1);
2419 tree fn
, len
, lenp1
;
2421 /* If the result is unused, replace stpcpy with strcpy. */
2422 if (gimple_call_lhs (stmt
) == NULL_TREE
)
2424 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
2427 gimple_call_set_fndecl (stmt
, fn
);
2432 len
= c_strlen (src
, 1);
2434 || TREE_CODE (len
) != INTEGER_CST
)
2437 if (optimize_function_for_size_p (cfun
)
2438 /* If length is zero it's small enough. */
2439 && !integer_zerop (len
))
2442 /* If the source has a known length replace stpcpy with memcpy. */
2443 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
2447 gimple_seq stmts
= NULL
;
2448 tree tem
= gimple_convert (&stmts
, loc
, size_type_node
, len
);
2449 lenp1
= gimple_build (&stmts
, loc
, PLUS_EXPR
, size_type_node
,
2450 tem
, build_int_cst (size_type_node
, 1));
2451 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2452 gcall
*repl
= gimple_build_call (fn
, 3, dest
, src
, lenp1
);
2453 gimple_set_vuse (repl
, gimple_vuse (stmt
));
2454 gimple_set_vdef (repl
, gimple_vdef (stmt
));
2455 if (gimple_vdef (repl
)
2456 && TREE_CODE (gimple_vdef (repl
)) == SSA_NAME
)
2457 SSA_NAME_DEF_STMT (gimple_vdef (repl
)) = repl
;
2458 gsi_insert_before (gsi
, repl
, GSI_SAME_STMT
);
2459 /* Replace the result with dest + len. */
2461 tem
= gimple_convert (&stmts
, loc
, sizetype
, len
);
2462 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2463 gassign
*ret
= gimple_build_assign (gimple_call_lhs (stmt
),
2464 POINTER_PLUS_EXPR
, dest
, tem
);
2465 gsi_replace (gsi
, ret
, false);
2466 /* Finally fold the memcpy call. */
2467 gimple_stmt_iterator gsi2
= *gsi
;
2473 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
2474 NULL_TREE if a normal call should be emitted rather than expanding
2475 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
2476 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
2477 passed as second argument. */
2480 gimple_fold_builtin_snprintf_chk (gimple_stmt_iterator
*gsi
,
2481 enum built_in_function fcode
)
2483 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
2484 tree dest
, size
, len
, fn
, fmt
, flag
;
2485 const char *fmt_str
;
2487 /* Verify the required arguments in the original call. */
2488 if (gimple_call_num_args (stmt
) < 5)
2491 dest
= gimple_call_arg (stmt
, 0);
2492 len
= gimple_call_arg (stmt
, 1);
2493 flag
= gimple_call_arg (stmt
, 2);
2494 size
= gimple_call_arg (stmt
, 3);
2495 fmt
= gimple_call_arg (stmt
, 4);
2497 if (! tree_fits_uhwi_p (size
))
2500 if (! integer_all_onesp (size
))
2502 tree maxlen
= get_maxval_strlen (len
, 2);
2503 if (! tree_fits_uhwi_p (len
))
2505 /* If LEN is not constant, try MAXLEN too.
2506 For MAXLEN only allow optimizing into non-_ocs function
2507 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2508 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
2514 if (tree_int_cst_lt (size
, maxlen
))
2518 if (!init_target_chars ())
2521 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
2522 or if format doesn't contain % chars or is "%s". */
2523 if (! integer_zerop (flag
))
2525 fmt_str
= c_getstr (fmt
);
2526 if (fmt_str
== NULL
)
2528 if (strchr (fmt_str
, target_percent
) != NULL
2529 && strcmp (fmt_str
, target_percent_s
))
2533 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
2535 fn
= builtin_decl_explicit (fcode
== BUILT_IN_VSNPRINTF_CHK
2536 ? BUILT_IN_VSNPRINTF
: BUILT_IN_SNPRINTF
);
2540 /* Replace the called function and the first 5 argument by 3 retaining
2541 trailing varargs. */
2542 gimple_call_set_fndecl (stmt
, fn
);
2543 gimple_call_set_fntype (stmt
, TREE_TYPE (fn
));
2544 gimple_call_set_arg (stmt
, 0, dest
);
2545 gimple_call_set_arg (stmt
, 1, len
);
2546 gimple_call_set_arg (stmt
, 2, fmt
);
2547 for (unsigned i
= 3; i
< gimple_call_num_args (stmt
) - 2; ++i
)
2548 gimple_call_set_arg (stmt
, i
, gimple_call_arg (stmt
, i
+ 2));
2549 gimple_set_num_ops (stmt
, gimple_num_ops (stmt
) - 2);
2554 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
2555 Return NULL_TREE if a normal call should be emitted rather than
2556 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
2557 or BUILT_IN_VSPRINTF_CHK. */
2560 gimple_fold_builtin_sprintf_chk (gimple_stmt_iterator
*gsi
,
2561 enum built_in_function fcode
)
2563 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
2564 tree dest
, size
, len
, fn
, fmt
, flag
;
2565 const char *fmt_str
;
2566 unsigned nargs
= gimple_call_num_args (stmt
);
2568 /* Verify the required arguments in the original call. */
2571 dest
= gimple_call_arg (stmt
, 0);
2572 flag
= gimple_call_arg (stmt
, 1);
2573 size
= gimple_call_arg (stmt
, 2);
2574 fmt
= gimple_call_arg (stmt
, 3);
2576 if (! tree_fits_uhwi_p (size
))
2581 if (!init_target_chars ())
2584 /* Check whether the format is a literal string constant. */
2585 fmt_str
= c_getstr (fmt
);
2586 if (fmt_str
!= NULL
)
2588 /* If the format doesn't contain % args or %%, we know the size. */
2589 if (strchr (fmt_str
, target_percent
) == 0)
2591 if (fcode
!= BUILT_IN_SPRINTF_CHK
|| nargs
== 4)
2592 len
= build_int_cstu (size_type_node
, strlen (fmt_str
));
2594 /* If the format is "%s" and first ... argument is a string literal,
2595 we know the size too. */
2596 else if (fcode
== BUILT_IN_SPRINTF_CHK
2597 && strcmp (fmt_str
, target_percent_s
) == 0)
2603 arg
= gimple_call_arg (stmt
, 4);
2604 if (POINTER_TYPE_P (TREE_TYPE (arg
)))
2606 len
= c_strlen (arg
, 1);
2607 if (! len
|| ! tree_fits_uhwi_p (len
))
2614 if (! integer_all_onesp (size
))
2616 if (! len
|| ! tree_int_cst_lt (len
, size
))
2620 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
2621 or if format doesn't contain % chars or is "%s". */
2622 if (! integer_zerop (flag
))
2624 if (fmt_str
== NULL
)
2626 if (strchr (fmt_str
, target_percent
) != NULL
2627 && strcmp (fmt_str
, target_percent_s
))
2631 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
2632 fn
= builtin_decl_explicit (fcode
== BUILT_IN_VSPRINTF_CHK
2633 ? BUILT_IN_VSPRINTF
: BUILT_IN_SPRINTF
);
2637 /* Replace the called function and the first 4 argument by 2 retaining
2638 trailing varargs. */
2639 gimple_call_set_fndecl (stmt
, fn
);
2640 gimple_call_set_fntype (stmt
, TREE_TYPE (fn
));
2641 gimple_call_set_arg (stmt
, 0, dest
);
2642 gimple_call_set_arg (stmt
, 1, fmt
);
2643 for (unsigned i
= 2; i
< gimple_call_num_args (stmt
) - 2; ++i
)
2644 gimple_call_set_arg (stmt
, i
, gimple_call_arg (stmt
, i
+ 2));
2645 gimple_set_num_ops (stmt
, gimple_num_ops (stmt
) - 2);
2650 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
2651 ORIG may be null if this is a 2-argument call. We don't attempt to
2652 simplify calls with more than 3 arguments.
2654 Return NULL_TREE if no simplification was possible, otherwise return the
2655 simplified form of the call as a tree. If IGNORED is true, it means that
2656 the caller does not use the returned value of the function. */
2659 gimple_fold_builtin_sprintf (gimple_stmt_iterator
*gsi
)
2661 gimple
*stmt
= gsi_stmt (*gsi
);
2662 tree dest
= gimple_call_arg (stmt
, 0);
2663 tree fmt
= gimple_call_arg (stmt
, 1);
2664 tree orig
= NULL_TREE
;
2665 const char *fmt_str
= NULL
;
2667 /* Verify the required arguments in the original call. We deal with two
2668 types of sprintf() calls: 'sprintf (str, fmt)' and
2669 'sprintf (dest, "%s", orig)'. */
2670 if (gimple_call_num_args (stmt
) > 3)
2673 if (gimple_call_num_args (stmt
) == 3)
2674 orig
= gimple_call_arg (stmt
, 2);
2676 /* Check whether the format is a literal string constant. */
2677 fmt_str
= c_getstr (fmt
);
2678 if (fmt_str
== NULL
)
2681 if (!init_target_chars ())
2684 /* If the format doesn't contain % args or %%, use strcpy. */
2685 if (strchr (fmt_str
, target_percent
) == NULL
)
2687 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
2692 /* Don't optimize sprintf (buf, "abc", ptr++). */
2696 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
2697 'format' is known to contain no % formats. */
2698 gimple_seq stmts
= NULL
;
2699 gimple
*repl
= gimple_build_call (fn
, 2, dest
, fmt
);
2700 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2701 if (gimple_call_lhs (stmt
))
2703 repl
= gimple_build_assign (gimple_call_lhs (stmt
),
2704 build_int_cst (integer_type_node
,
2706 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2707 gsi_replace_with_seq_vops (gsi
, stmts
);
2708 /* gsi now points at the assignment to the lhs, get a
2709 stmt iterator to the memcpy call.
2710 ??? We can't use gsi_for_stmt as that doesn't work when the
2711 CFG isn't built yet. */
2712 gimple_stmt_iterator gsi2
= *gsi
;
2718 gsi_replace_with_seq_vops (gsi
, stmts
);
2724 /* If the format is "%s", use strcpy if the result isn't used. */
2725 else if (fmt_str
&& strcmp (fmt_str
, target_percent_s
) == 0)
2728 fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
2733 /* Don't crash on sprintf (str1, "%s"). */
2737 tree orig_len
= NULL_TREE
;
2738 if (gimple_call_lhs (stmt
))
2740 orig_len
= get_maxval_strlen (orig
, 0);
2745 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
2746 gimple_seq stmts
= NULL
;
2747 gimple
*repl
= gimple_build_call (fn
, 2, dest
, orig
);
2748 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2749 if (gimple_call_lhs (stmt
))
2751 if (!useless_type_conversion_p (integer_type_node
,
2752 TREE_TYPE (orig_len
)))
2753 orig_len
= fold_convert (integer_type_node
, orig_len
);
2754 repl
= gimple_build_assign (gimple_call_lhs (stmt
), orig_len
);
2755 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2756 gsi_replace_with_seq_vops (gsi
, stmts
);
2757 /* gsi now points at the assignment to the lhs, get a
2758 stmt iterator to the memcpy call.
2759 ??? We can't use gsi_for_stmt as that doesn't work when the
2760 CFG isn't built yet. */
2761 gimple_stmt_iterator gsi2
= *gsi
;
2767 gsi_replace_with_seq_vops (gsi
, stmts
);
2775 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
2776 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
2777 attempt to simplify calls with more than 4 arguments.
2779 Return NULL_TREE if no simplification was possible, otherwise return the
2780 simplified form of the call as a tree. If IGNORED is true, it means that
2781 the caller does not use the returned value of the function. */
2784 gimple_fold_builtin_snprintf (gimple_stmt_iterator
*gsi
)
2786 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
2787 tree dest
= gimple_call_arg (stmt
, 0);
2788 tree destsize
= gimple_call_arg (stmt
, 1);
2789 tree fmt
= gimple_call_arg (stmt
, 2);
2790 tree orig
= NULL_TREE
;
2791 const char *fmt_str
= NULL
;
2793 if (gimple_call_num_args (stmt
) > 4)
2796 if (gimple_call_num_args (stmt
) == 4)
2797 orig
= gimple_call_arg (stmt
, 3);
2799 if (!tree_fits_uhwi_p (destsize
))
2801 unsigned HOST_WIDE_INT destlen
= tree_to_uhwi (destsize
);
2803 /* Check whether the format is a literal string constant. */
2804 fmt_str
= c_getstr (fmt
);
2805 if (fmt_str
== NULL
)
2808 if (!init_target_chars ())
2811 /* If the format doesn't contain % args or %%, use strcpy. */
2812 if (strchr (fmt_str
, target_percent
) == NULL
)
2814 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
2818 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
2822 /* We could expand this as
2823 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
2825 memcpy (str, fmt_with_nul_at_cstm1, cst);
2826 but in the former case that might increase code size
2827 and in the latter case grow .rodata section too much.
2829 size_t len
= strlen (fmt_str
);
2833 gimple_seq stmts
= NULL
;
2834 gimple
*repl
= gimple_build_call (fn
, 2, dest
, fmt
);
2835 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2836 if (gimple_call_lhs (stmt
))
2838 repl
= gimple_build_assign (gimple_call_lhs (stmt
),
2839 build_int_cst (integer_type_node
, len
));
2840 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2841 gsi_replace_with_seq_vops (gsi
, stmts
);
2842 /* gsi now points at the assignment to the lhs, get a
2843 stmt iterator to the memcpy call.
2844 ??? We can't use gsi_for_stmt as that doesn't work when the
2845 CFG isn't built yet. */
2846 gimple_stmt_iterator gsi2
= *gsi
;
2852 gsi_replace_with_seq_vops (gsi
, stmts
);
2858 /* If the format is "%s", use strcpy if the result isn't used. */
2859 else if (fmt_str
&& strcmp (fmt_str
, target_percent_s
) == 0)
2861 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
2865 /* Don't crash on snprintf (str1, cst, "%s"). */
2869 tree orig_len
= get_maxval_strlen (orig
, 0);
2870 if (!orig_len
|| TREE_CODE (orig_len
) != INTEGER_CST
)
2873 /* We could expand this as
2874 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
2876 memcpy (str1, str2_with_nul_at_cstm1, cst);
2877 but in the former case that might increase code size
2878 and in the latter case grow .rodata section too much.
2880 if (compare_tree_int (orig_len
, destlen
) >= 0)
2883 /* Convert snprintf (str1, cst, "%s", str2) into
2884 strcpy (str1, str2) if strlen (str2) < cst. */
2885 gimple_seq stmts
= NULL
;
2886 gimple
*repl
= gimple_build_call (fn
, 2, dest
, orig
);
2887 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2888 if (gimple_call_lhs (stmt
))
2890 if (!useless_type_conversion_p (integer_type_node
,
2891 TREE_TYPE (orig_len
)))
2892 orig_len
= fold_convert (integer_type_node
, orig_len
);
2893 repl
= gimple_build_assign (gimple_call_lhs (stmt
), orig_len
);
2894 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2895 gsi_replace_with_seq_vops (gsi
, stmts
);
2896 /* gsi now points at the assignment to the lhs, get a
2897 stmt iterator to the memcpy call.
2898 ??? We can't use gsi_for_stmt as that doesn't work when the
2899 CFG isn't built yet. */
2900 gimple_stmt_iterator gsi2
= *gsi
;
2906 gsi_replace_with_seq_vops (gsi
, stmts
);
2914 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
2915 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
2916 more than 3 arguments, and ARG may be null in the 2-argument case.
2918 Return NULL_TREE if no simplification was possible, otherwise return the
2919 simplified form of the call as a tree. FCODE is the BUILT_IN_*
2920 code of the function to be simplified. */
2923 gimple_fold_builtin_fprintf (gimple_stmt_iterator
*gsi
,
2924 tree fp
, tree fmt
, tree arg
,
2925 enum built_in_function fcode
)
2927 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
2928 tree fn_fputc
, fn_fputs
;
2929 const char *fmt_str
= NULL
;
2931 /* If the return value is used, don't do the transformation. */
2932 if (gimple_call_lhs (stmt
) != NULL_TREE
)
2935 /* Check whether the format is a literal string constant. */
2936 fmt_str
= c_getstr (fmt
);
2937 if (fmt_str
== NULL
)
2940 if (fcode
== BUILT_IN_FPRINTF_UNLOCKED
)
2942 /* If we're using an unlocked function, assume the other
2943 unlocked functions exist explicitly. */
2944 fn_fputc
= builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED
);
2945 fn_fputs
= builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED
);
2949 fn_fputc
= builtin_decl_implicit (BUILT_IN_FPUTC
);
2950 fn_fputs
= builtin_decl_implicit (BUILT_IN_FPUTS
);
2953 if (!init_target_chars ())
2956 /* If the format doesn't contain % args or %%, use strcpy. */
2957 if (strchr (fmt_str
, target_percent
) == NULL
)
2959 if (fcode
!= BUILT_IN_VFPRINTF
&& fcode
!= BUILT_IN_VFPRINTF_CHK
2963 /* If the format specifier was "", fprintf does nothing. */
2964 if (fmt_str
[0] == '\0')
2966 replace_call_with_value (gsi
, NULL_TREE
);
2970 /* When "string" doesn't contain %, replace all cases of
2971 fprintf (fp, string) with fputs (string, fp). The fputs
2972 builtin will take care of special cases like length == 1. */
2975 gcall
*repl
= gimple_build_call (fn_fputs
, 2, fmt
, fp
);
2976 replace_call_with_call_and_fold (gsi
, repl
);
2981 /* The other optimizations can be done only on the non-va_list variants. */
2982 else if (fcode
== BUILT_IN_VFPRINTF
|| fcode
== BUILT_IN_VFPRINTF_CHK
)
2985 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
2986 else if (strcmp (fmt_str
, target_percent_s
) == 0)
2988 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
2992 gcall
*repl
= gimple_build_call (fn_fputs
, 2, arg
, fp
);
2993 replace_call_with_call_and_fold (gsi
, repl
);
2998 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
2999 else if (strcmp (fmt_str
, target_percent_c
) == 0)
3002 || ! useless_type_conversion_p (integer_type_node
, TREE_TYPE (arg
)))
3006 gcall
*repl
= gimple_build_call (fn_fputc
, 2, arg
, fp
);
3007 replace_call_with_call_and_fold (gsi
, repl
);
3015 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
3016 FMT and ARG are the arguments to the call; we don't fold cases with
3017 more than 2 arguments, and ARG may be null if this is a 1-argument case.
3019 Return NULL_TREE if no simplification was possible, otherwise return the
3020 simplified form of the call as a tree. FCODE is the BUILT_IN_*
3021 code of the function to be simplified. */
3024 gimple_fold_builtin_printf (gimple_stmt_iterator
*gsi
, tree fmt
,
3025 tree arg
, enum built_in_function fcode
)
3027 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3028 tree fn_putchar
, fn_puts
, newarg
;
3029 const char *fmt_str
= NULL
;
3031 /* If the return value is used, don't do the transformation. */
3032 if (gimple_call_lhs (stmt
) != NULL_TREE
)
3035 /* Check whether the format is a literal string constant. */
3036 fmt_str
= c_getstr (fmt
);
3037 if (fmt_str
== NULL
)
3040 if (fcode
== BUILT_IN_PRINTF_UNLOCKED
)
3042 /* If we're using an unlocked function, assume the other
3043 unlocked functions exist explicitly. */
3044 fn_putchar
= builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED
);
3045 fn_puts
= builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED
);
3049 fn_putchar
= builtin_decl_implicit (BUILT_IN_PUTCHAR
);
3050 fn_puts
= builtin_decl_implicit (BUILT_IN_PUTS
);
3053 if (!init_target_chars ())
3056 if (strcmp (fmt_str
, target_percent_s
) == 0
3057 || strchr (fmt_str
, target_percent
) == NULL
)
3061 if (strcmp (fmt_str
, target_percent_s
) == 0)
3063 if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
3066 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
3069 str
= c_getstr (arg
);
3075 /* The format specifier doesn't contain any '%' characters. */
3076 if (fcode
!= BUILT_IN_VPRINTF
&& fcode
!= BUILT_IN_VPRINTF_CHK
3082 /* If the string was "", printf does nothing. */
3085 replace_call_with_value (gsi
, NULL_TREE
);
3089 /* If the string has length of 1, call putchar. */
3092 /* Given printf("c"), (where c is any one character,)
3093 convert "c"[0] to an int and pass that to the replacement
3095 newarg
= build_int_cst (integer_type_node
, str
[0]);
3098 gcall
*repl
= gimple_build_call (fn_putchar
, 1, newarg
);
3099 replace_call_with_call_and_fold (gsi
, repl
);
3105 /* If the string was "string\n", call puts("string"). */
3106 size_t len
= strlen (str
);
3107 if ((unsigned char)str
[len
- 1] == target_newline
3108 && (size_t) (int) len
== len
3112 tree offset_node
, string_cst
;
3114 /* Create a NUL-terminated string that's one char shorter
3115 than the original, stripping off the trailing '\n'. */
3116 newarg
= build_string_literal (len
, str
);
3117 string_cst
= string_constant (newarg
, &offset_node
);
3118 gcc_checking_assert (string_cst
3119 && (TREE_STRING_LENGTH (string_cst
)
3121 && integer_zerop (offset_node
)
3123 TREE_STRING_POINTER (string_cst
)[len
- 1]
3125 /* build_string_literal creates a new STRING_CST,
3126 modify it in place to avoid double copying. */
3127 newstr
= CONST_CAST (char *, TREE_STRING_POINTER (string_cst
));
3128 newstr
[len
- 1] = '\0';
3131 gcall
*repl
= gimple_build_call (fn_puts
, 1, newarg
);
3132 replace_call_with_call_and_fold (gsi
, repl
);
3137 /* We'd like to arrange to call fputs(string,stdout) here,
3138 but we need stdout and don't have a way to get it yet. */
3143 /* The other optimizations can be done only on the non-va_list variants. */
3144 else if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
3147 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
3148 else if (strcmp (fmt_str
, target_percent_s_newline
) == 0)
3150 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
3154 gcall
*repl
= gimple_build_call (fn_puts
, 1, arg
);
3155 replace_call_with_call_and_fold (gsi
, repl
);
3160 /* If the format specifier was "%c", call __builtin_putchar(arg). */
3161 else if (strcmp (fmt_str
, target_percent_c
) == 0)
3163 if (!arg
|| ! useless_type_conversion_p (integer_type_node
,
3168 gcall
*repl
= gimple_build_call (fn_putchar
, 1, arg
);
3169 replace_call_with_call_and_fold (gsi
, repl
);
3179 /* Fold a call to __builtin_strlen with known length LEN. */
3182 gimple_fold_builtin_strlen (gimple_stmt_iterator
*gsi
)
3184 gimple
*stmt
= gsi_stmt (*gsi
);
3185 tree len
= get_maxval_strlen (gimple_call_arg (stmt
, 0), 0);
3188 len
= force_gimple_operand_gsi (gsi
, len
, true, NULL
, true, GSI_SAME_STMT
);
3189 replace_call_with_value (gsi
, len
);
3193 /* Fold a call to __builtin_acc_on_device. */
3196 gimple_fold_builtin_acc_on_device (gimple_stmt_iterator
*gsi
, tree arg0
)
3198 /* Defer folding until we know which compiler we're in. */
3199 if (symtab
->state
!= EXPANSION
)
3202 unsigned val_host
= GOMP_DEVICE_HOST
;
3203 unsigned val_dev
= GOMP_DEVICE_NONE
;
3205 #ifdef ACCEL_COMPILER
3206 val_host
= GOMP_DEVICE_NOT_HOST
;
3207 val_dev
= ACCEL_COMPILER_acc_device
;
3210 location_t loc
= gimple_location (gsi_stmt (*gsi
));
3212 tree host_eq
= make_ssa_name (boolean_type_node
);
3213 gimple
*host_ass
= gimple_build_assign
3214 (host_eq
, EQ_EXPR
, arg0
, build_int_cst (TREE_TYPE (arg0
), val_host
));
3215 gimple_set_location (host_ass
, loc
);
3216 gsi_insert_before (gsi
, host_ass
, GSI_SAME_STMT
);
3218 tree dev_eq
= make_ssa_name (boolean_type_node
);
3219 gimple
*dev_ass
= gimple_build_assign
3220 (dev_eq
, EQ_EXPR
, arg0
, build_int_cst (TREE_TYPE (arg0
), val_dev
));
3221 gimple_set_location (dev_ass
, loc
);
3222 gsi_insert_before (gsi
, dev_ass
, GSI_SAME_STMT
);
3224 tree result
= make_ssa_name (boolean_type_node
);
3225 gimple
*result_ass
= gimple_build_assign
3226 (result
, BIT_IOR_EXPR
, host_eq
, dev_eq
);
3227 gimple_set_location (result_ass
, loc
);
3228 gsi_insert_before (gsi
, result_ass
, GSI_SAME_STMT
);
3230 replace_call_with_value (gsi
, result
);
3235 /* Fold the non-target builtin at *GSI and return whether any simplification
3239 gimple_fold_builtin (gimple_stmt_iterator
*gsi
)
3241 gcall
*stmt
= as_a
<gcall
*>(gsi_stmt (*gsi
));
3242 tree callee
= gimple_call_fndecl (stmt
);
3244 /* Give up for always_inline inline builtins until they are
3246 if (avoid_folding_inline_builtin (callee
))
3249 unsigned n
= gimple_call_num_args (stmt
);
3250 enum built_in_function fcode
= DECL_FUNCTION_CODE (callee
);
3253 case BUILT_IN_BZERO
:
3254 return gimple_fold_builtin_memset (gsi
, integer_zero_node
,
3255 gimple_call_arg (stmt
, 1));
3256 case BUILT_IN_MEMSET
:
3257 return gimple_fold_builtin_memset (gsi
,
3258 gimple_call_arg (stmt
, 1),
3259 gimple_call_arg (stmt
, 2));
3260 case BUILT_IN_BCOPY
:
3261 return gimple_fold_builtin_memory_op (gsi
, gimple_call_arg (stmt
, 1),
3262 gimple_call_arg (stmt
, 0), 3);
3263 case BUILT_IN_MEMCPY
:
3264 return gimple_fold_builtin_memory_op (gsi
, gimple_call_arg (stmt
, 0),
3265 gimple_call_arg (stmt
, 1), 0);
3266 case BUILT_IN_MEMPCPY
:
3267 return gimple_fold_builtin_memory_op (gsi
, gimple_call_arg (stmt
, 0),
3268 gimple_call_arg (stmt
, 1), 1);
3269 case BUILT_IN_MEMMOVE
:
3270 return gimple_fold_builtin_memory_op (gsi
, gimple_call_arg (stmt
, 0),
3271 gimple_call_arg (stmt
, 1), 3);
3272 case BUILT_IN_SPRINTF_CHK
:
3273 case BUILT_IN_VSPRINTF_CHK
:
3274 return gimple_fold_builtin_sprintf_chk (gsi
, fcode
);
3275 case BUILT_IN_STRCAT_CHK
:
3276 return gimple_fold_builtin_strcat_chk (gsi
);
3277 case BUILT_IN_STRNCAT_CHK
:
3278 return gimple_fold_builtin_strncat_chk (gsi
);
3279 case BUILT_IN_STRLEN
:
3280 return gimple_fold_builtin_strlen (gsi
);
3281 case BUILT_IN_STRCPY
:
3282 return gimple_fold_builtin_strcpy (gsi
,
3283 gimple_call_arg (stmt
, 0),
3284 gimple_call_arg (stmt
, 1));
3285 case BUILT_IN_STRNCPY
:
3286 return gimple_fold_builtin_strncpy (gsi
,
3287 gimple_call_arg (stmt
, 0),
3288 gimple_call_arg (stmt
, 1),
3289 gimple_call_arg (stmt
, 2));
3290 case BUILT_IN_STRCAT
:
3291 return gimple_fold_builtin_strcat (gsi
, gimple_call_arg (stmt
, 0),
3292 gimple_call_arg (stmt
, 1));
3293 case BUILT_IN_STRNCAT
:
3294 return gimple_fold_builtin_strncat (gsi
);
3295 case BUILT_IN_INDEX
:
3296 case BUILT_IN_STRCHR
:
3297 return gimple_fold_builtin_strchr (gsi
, false);
3298 case BUILT_IN_RINDEX
:
3299 case BUILT_IN_STRRCHR
:
3300 return gimple_fold_builtin_strchr (gsi
, true);
3301 case BUILT_IN_STRSTR
:
3302 return gimple_fold_builtin_strstr (gsi
);
3303 case BUILT_IN_STRCMP
:
3304 case BUILT_IN_STRCASECMP
:
3305 case BUILT_IN_STRNCMP
:
3306 case BUILT_IN_STRNCASECMP
:
3307 return gimple_fold_builtin_string_compare (gsi
);
3308 case BUILT_IN_MEMCHR
:
3309 return gimple_fold_builtin_memchr (gsi
);
3310 case BUILT_IN_FPUTS
:
3311 return gimple_fold_builtin_fputs (gsi
, gimple_call_arg (stmt
, 0),
3312 gimple_call_arg (stmt
, 1), false);
3313 case BUILT_IN_FPUTS_UNLOCKED
:
3314 return gimple_fold_builtin_fputs (gsi
, gimple_call_arg (stmt
, 0),
3315 gimple_call_arg (stmt
, 1), true);
3316 case BUILT_IN_MEMCPY_CHK
:
3317 case BUILT_IN_MEMPCPY_CHK
:
3318 case BUILT_IN_MEMMOVE_CHK
:
3319 case BUILT_IN_MEMSET_CHK
:
3320 return gimple_fold_builtin_memory_chk (gsi
,
3321 gimple_call_arg (stmt
, 0),
3322 gimple_call_arg (stmt
, 1),
3323 gimple_call_arg (stmt
, 2),
3324 gimple_call_arg (stmt
, 3),
3326 case BUILT_IN_STPCPY
:
3327 return gimple_fold_builtin_stpcpy (gsi
);
3328 case BUILT_IN_STRCPY_CHK
:
3329 case BUILT_IN_STPCPY_CHK
:
3330 return gimple_fold_builtin_stxcpy_chk (gsi
,
3331 gimple_call_arg (stmt
, 0),
3332 gimple_call_arg (stmt
, 1),
3333 gimple_call_arg (stmt
, 2),
3335 case BUILT_IN_STRNCPY_CHK
:
3336 case BUILT_IN_STPNCPY_CHK
:
3337 return gimple_fold_builtin_stxncpy_chk (gsi
,
3338 gimple_call_arg (stmt
, 0),
3339 gimple_call_arg (stmt
, 1),
3340 gimple_call_arg (stmt
, 2),
3341 gimple_call_arg (stmt
, 3),
3343 case BUILT_IN_SNPRINTF_CHK
:
3344 case BUILT_IN_VSNPRINTF_CHK
:
3345 return gimple_fold_builtin_snprintf_chk (gsi
, fcode
);
3346 case BUILT_IN_SNPRINTF
:
3347 return gimple_fold_builtin_snprintf (gsi
);
3348 case BUILT_IN_SPRINTF
:
3349 return gimple_fold_builtin_sprintf (gsi
);
3350 case BUILT_IN_FPRINTF
:
3351 case BUILT_IN_FPRINTF_UNLOCKED
:
3352 case BUILT_IN_VFPRINTF
:
3353 if (n
== 2 || n
== 3)
3354 return gimple_fold_builtin_fprintf (gsi
,
3355 gimple_call_arg (stmt
, 0),
3356 gimple_call_arg (stmt
, 1),
3358 ? gimple_call_arg (stmt
, 2)
3362 case BUILT_IN_FPRINTF_CHK
:
3363 case BUILT_IN_VFPRINTF_CHK
:
3364 if (n
== 3 || n
== 4)
3365 return gimple_fold_builtin_fprintf (gsi
,
3366 gimple_call_arg (stmt
, 0),
3367 gimple_call_arg (stmt
, 2),
3369 ? gimple_call_arg (stmt
, 3)
3373 case BUILT_IN_PRINTF
:
3374 case BUILT_IN_PRINTF_UNLOCKED
:
3375 case BUILT_IN_VPRINTF
:
3376 if (n
== 1 || n
== 2)
3377 return gimple_fold_builtin_printf (gsi
, gimple_call_arg (stmt
, 0),
3379 ? gimple_call_arg (stmt
, 1)
3380 : NULL_TREE
, fcode
);
3382 case BUILT_IN_PRINTF_CHK
:
3383 case BUILT_IN_VPRINTF_CHK
:
3384 if (n
== 2 || n
== 3)
3385 return gimple_fold_builtin_printf (gsi
, gimple_call_arg (stmt
, 1),
3387 ? gimple_call_arg (stmt
, 2)
3388 : NULL_TREE
, fcode
);
3390 case BUILT_IN_ACC_ON_DEVICE
:
3391 return gimple_fold_builtin_acc_on_device (gsi
,
3392 gimple_call_arg (stmt
, 0));
3396 /* Try the generic builtin folder. */
3397 bool ignore
= (gimple_call_lhs (stmt
) == NULL
);
3398 tree result
= fold_call_stmt (stmt
, ignore
);
3402 STRIP_NOPS (result
);
3404 result
= fold_convert (gimple_call_return_type (stmt
), result
);
3405 if (!update_call_from_tree (gsi
, result
))
3406 gimplify_and_update_call_from_tree (gsi
, result
);
3413 /* Transform IFN_GOACC_DIM_SIZE and IFN_GOACC_DIM_POS internal
3414 function calls to constants, where possible. */
3417 fold_internal_goacc_dim (const gimple
*call
)
3419 int axis
= get_oacc_ifn_dim_arg (call
);
3420 int size
= get_oacc_fn_dim_size (current_function_decl
, axis
);
3421 bool is_pos
= gimple_call_internal_fn (call
) == IFN_GOACC_DIM_POS
;
3422 tree result
= NULL_TREE
;
3424 /* If the size is 1, or we only want the size and it is not dynamic,
3425 we know the answer. */
3426 if (size
== 1 || (!is_pos
&& size
))
3428 tree type
= TREE_TYPE (gimple_call_lhs (call
));
3429 result
= build_int_cst (type
, size
- is_pos
);
3435 /* Return true if stmt is __atomic_compare_exchange_N call which is suitable
3436 for conversion into ATOMIC_COMPARE_EXCHANGE if the second argument is
3437 &var where var is only addressable because of such calls. */
3440 optimize_atomic_compare_exchange_p (gimple
*stmt
)
3442 if (gimple_call_num_args (stmt
) != 6
3443 || !flag_inline_atomics
3445 || (flag_sanitize
& (SANITIZE_THREAD
| SANITIZE_ADDRESS
)) != 0
3446 || !gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
)
3447 || !gimple_vdef (stmt
)
3448 || !gimple_vuse (stmt
))
3451 tree fndecl
= gimple_call_fndecl (stmt
);
3452 switch (DECL_FUNCTION_CODE (fndecl
))
3454 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
:
3455 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2
:
3456 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4
:
3457 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8
:
3458 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16
:
3464 tree expected
= gimple_call_arg (stmt
, 1);
3465 if (TREE_CODE (expected
) != ADDR_EXPR
3466 || !SSA_VAR_P (TREE_OPERAND (expected
, 0)))
3469 tree etype
= TREE_TYPE (TREE_OPERAND (expected
, 0));
3470 if (!is_gimple_reg_type (etype
)
3471 || !auto_var_in_fn_p (TREE_OPERAND (expected
, 0), current_function_decl
)
3472 || TREE_THIS_VOLATILE (etype
)
3473 || VECTOR_TYPE_P (etype
)
3474 || TREE_CODE (etype
) == COMPLEX_TYPE
3475 /* Don't optimize floating point expected vars, VIEW_CONVERT_EXPRs
3476 might not preserve all the bits. See PR71716. */
3477 || SCALAR_FLOAT_TYPE_P (etype
)
3478 || TYPE_PRECISION (etype
) != GET_MODE_BITSIZE (TYPE_MODE (etype
)))
3481 tree weak
= gimple_call_arg (stmt
, 3);
3482 if (!integer_zerop (weak
) && !integer_onep (weak
))
3485 tree parmt
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
3486 tree itype
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt
)));
3487 machine_mode mode
= TYPE_MODE (itype
);
3489 if (direct_optab_handler (atomic_compare_and_swap_optab
, mode
)
3491 && optab_handler (sync_compare_and_swap_optab
, mode
) == CODE_FOR_nothing
)
3494 if (int_size_in_bytes (etype
) != GET_MODE_SIZE (mode
))
3501 r = __atomic_compare_exchange_N (p, &e, d, w, s, f);
3503 _Complex uintN_t t = ATOMIC_COMPARE_EXCHANGE (p, e, d, w * 256 + N, s, f);
3504 i = IMAGPART_EXPR <t>;
3506 e = REALPART_EXPR <t>; */
3509 fold_builtin_atomic_compare_exchange (gimple_stmt_iterator
*gsi
)
3511 gimple
*stmt
= gsi_stmt (*gsi
);
3512 tree fndecl
= gimple_call_fndecl (stmt
);
3513 tree parmt
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
3514 tree itype
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt
)));
3515 tree ctype
= build_complex_type (itype
);
3516 tree expected
= TREE_OPERAND (gimple_call_arg (stmt
, 1), 0);
3517 gimple
*g
= gimple_build_assign (make_ssa_name (TREE_TYPE (expected
)),
3519 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
3520 gimple_stmt_iterator gsiret
= gsi_for_stmt (g
);
3521 if (!useless_type_conversion_p (itype
, TREE_TYPE (expected
)))
3523 g
= gimple_build_assign (make_ssa_name (itype
), VIEW_CONVERT_EXPR
,
3524 build1 (VIEW_CONVERT_EXPR
, itype
,
3525 gimple_assign_lhs (g
)));
3526 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
3528 int flag
= (integer_onep (gimple_call_arg (stmt
, 3)) ? 256 : 0)
3529 + int_size_in_bytes (itype
);
3530 g
= gimple_build_call_internal (IFN_ATOMIC_COMPARE_EXCHANGE
, 6,
3531 gimple_call_arg (stmt
, 0),
3532 gimple_assign_lhs (g
),
3533 gimple_call_arg (stmt
, 2),
3534 build_int_cst (integer_type_node
, flag
),
3535 gimple_call_arg (stmt
, 4),
3536 gimple_call_arg (stmt
, 5));
3537 tree lhs
= make_ssa_name (ctype
);
3538 gimple_call_set_lhs (g
, lhs
);
3539 gimple_set_vdef (g
, gimple_vdef (stmt
));
3540 gimple_set_vuse (g
, gimple_vuse (stmt
));
3541 SSA_NAME_DEF_STMT (gimple_vdef (g
)) = g
;
3542 if (gimple_call_lhs (stmt
))
3544 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
3545 g
= gimple_build_assign (make_ssa_name (itype
), IMAGPART_EXPR
,
3546 build1 (IMAGPART_EXPR
, itype
, lhs
));
3547 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
3548 g
= gimple_build_assign (gimple_call_lhs (stmt
), NOP_EXPR
,
3549 gimple_assign_lhs (g
));
3551 gsi_replace (gsi
, g
, true);
3552 g
= gimple_build_assign (make_ssa_name (itype
), REALPART_EXPR
,
3553 build1 (REALPART_EXPR
, itype
, lhs
));
3554 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
3555 if (!useless_type_conversion_p (TREE_TYPE (expected
), itype
))
3557 g
= gimple_build_assign (make_ssa_name (TREE_TYPE (expected
)),
3559 build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (expected
),
3560 gimple_assign_lhs (g
)));
3561 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
3563 g
= gimple_build_assign (expected
, SSA_NAME
, gimple_assign_lhs (g
));
3564 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
3568 /* Return true if ARG0 CODE ARG1 in infinite signed precision operation
3569 doesn't fit into TYPE. The test for overflow should be regardless of
3570 -fwrapv, and even for unsigned types. */
3573 arith_overflowed_p (enum tree_code code
, const_tree type
,
3574 const_tree arg0
, const_tree arg1
)
3576 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
* 2) widest2_int
;
3577 typedef generic_wide_int
<wi::extended_tree
<WIDE_INT_MAX_PRECISION
* 2> >
3579 widest2_int warg0
= widest2_int_cst (arg0
);
3580 widest2_int warg1
= widest2_int_cst (arg1
);
3584 case PLUS_EXPR
: wres
= wi::add (warg0
, warg1
); break;
3585 case MINUS_EXPR
: wres
= wi::sub (warg0
, warg1
); break;
3586 case MULT_EXPR
: wres
= wi::mul (warg0
, warg1
); break;
3587 default: gcc_unreachable ();
3589 signop sign
= TYPE_SIGN (type
);
3590 if (sign
== UNSIGNED
&& wi::neg_p (wres
))
3592 return wi::min_precision (wres
, sign
) > TYPE_PRECISION (type
);
3595 /* Attempt to fold a call statement referenced by the statement iterator GSI.
3596 The statement may be replaced by another statement, e.g., if the call
3597 simplifies to a constant value. Return true if any changes were made.
3598 It is assumed that the operands have been previously folded. */
3601 gimple_fold_call (gimple_stmt_iterator
*gsi
, bool inplace
)
3603 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3605 bool changed
= false;
3608 /* Fold *& in call arguments. */
3609 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
3610 if (REFERENCE_CLASS_P (gimple_call_arg (stmt
, i
)))
3612 tree tmp
= maybe_fold_reference (gimple_call_arg (stmt
, i
), false);
3615 gimple_call_set_arg (stmt
, i
, tmp
);
3620 /* Check for virtual calls that became direct calls. */
3621 callee
= gimple_call_fn (stmt
);
3622 if (callee
&& TREE_CODE (callee
) == OBJ_TYPE_REF
)
3624 if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee
)) != NULL_TREE
)
3626 if (dump_file
&& virtual_method_call_p (callee
)
3627 && !possible_polymorphic_call_target_p
3628 (callee
, stmt
, cgraph_node::get (gimple_call_addr_fndecl
3629 (OBJ_TYPE_REF_EXPR (callee
)))))
3632 "Type inheritance inconsistent devirtualization of ");
3633 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
3634 fprintf (dump_file
, " to ");
3635 print_generic_expr (dump_file
, callee
, TDF_SLIM
);
3636 fprintf (dump_file
, "\n");
3639 gimple_call_set_fn (stmt
, OBJ_TYPE_REF_EXPR (callee
));
3642 else if (flag_devirtualize
&& !inplace
&& virtual_method_call_p (callee
))
3645 vec
<cgraph_node
*>targets
3646 = possible_polymorphic_call_targets (callee
, stmt
, &final
);
3647 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
3649 tree lhs
= gimple_call_lhs (stmt
);
3650 if (dump_enabled_p ())
3652 location_t loc
= gimple_location_safe (stmt
);
3653 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, loc
,
3654 "folding virtual function call to %s\n",
3655 targets
.length () == 1
3656 ? targets
[0]->name ()
3657 : "__builtin_unreachable");
3659 if (targets
.length () == 1)
3661 tree fndecl
= targets
[0]->decl
;
3662 gimple_call_set_fndecl (stmt
, fndecl
);
3664 /* If changing the call to __cxa_pure_virtual
3665 or similar noreturn function, adjust gimple_call_fntype
3667 if (gimple_call_noreturn_p (stmt
)
3668 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl
)))
3669 && TYPE_ARG_TYPES (TREE_TYPE (fndecl
))
3670 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)))
3672 gimple_call_set_fntype (stmt
, TREE_TYPE (fndecl
));
3673 /* If the call becomes noreturn, remove the lhs. */
3675 && gimple_call_noreturn_p (stmt
)
3676 && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (stmt
)))
3677 || should_remove_lhs_p (lhs
)))
3679 if (TREE_CODE (lhs
) == SSA_NAME
)
3681 tree var
= create_tmp_var (TREE_TYPE (lhs
));
3682 tree def
= get_or_create_ssa_default_def (cfun
, var
);
3683 gimple
*new_stmt
= gimple_build_assign (lhs
, def
);
3684 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
3686 gimple_call_set_lhs (stmt
, NULL_TREE
);
3688 maybe_remove_unused_call_args (cfun
, stmt
);
3692 tree fndecl
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
3693 gimple
*new_stmt
= gimple_build_call (fndecl
, 0);
3694 gimple_set_location (new_stmt
, gimple_location (stmt
));
3695 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
3697 tree var
= create_tmp_var (TREE_TYPE (lhs
));
3698 tree def
= get_or_create_ssa_default_def (cfun
, var
);
3700 /* To satisfy condition for
3701 cgraph_update_edges_for_call_stmt_node,
3702 we need to preserve GIMPLE_CALL statement
3703 at position of GSI iterator. */
3704 update_call_from_tree (gsi
, def
);
3705 gsi_insert_before (gsi
, new_stmt
, GSI_NEW_STMT
);
3709 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
3710 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
3711 gsi_replace (gsi
, new_stmt
, false);
3719 /* Check for indirect calls that became direct calls, and then
3720 no longer require a static chain. */
3721 if (gimple_call_chain (stmt
))
3723 tree fn
= gimple_call_fndecl (stmt
);
3724 if (fn
&& !DECL_STATIC_CHAIN (fn
))
3726 gimple_call_set_chain (stmt
, NULL
);
3731 tree tmp
= maybe_fold_reference (gimple_call_chain (stmt
), false);
3734 gimple_call_set_chain (stmt
, tmp
);
3743 /* Check for builtins that CCP can handle using information not
3744 available in the generic fold routines. */
3745 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
3747 if (gimple_fold_builtin (gsi
))
3750 else if (gimple_call_builtin_p (stmt
, BUILT_IN_MD
))
3752 changed
|= targetm
.gimple_fold_builtin (gsi
);
3754 else if (gimple_call_internal_p (stmt
))
3756 enum tree_code subcode
= ERROR_MARK
;
3757 tree result
= NULL_TREE
;
3758 bool cplx_result
= false;
3759 tree overflow
= NULL_TREE
;
3760 switch (gimple_call_internal_fn (stmt
))
3762 case IFN_BUILTIN_EXPECT
:
3763 result
= fold_builtin_expect (gimple_location (stmt
),
3764 gimple_call_arg (stmt
, 0),
3765 gimple_call_arg (stmt
, 1),
3766 gimple_call_arg (stmt
, 2));
3768 case IFN_UBSAN_OBJECT_SIZE
:
3769 if (integer_all_onesp (gimple_call_arg (stmt
, 2))
3770 || (TREE_CODE (gimple_call_arg (stmt
, 1)) == INTEGER_CST
3771 && TREE_CODE (gimple_call_arg (stmt
, 2)) == INTEGER_CST
3772 && tree_int_cst_le (gimple_call_arg (stmt
, 1),
3773 gimple_call_arg (stmt
, 2))))
3775 gsi_replace (gsi
, gimple_build_nop (), false);
3776 unlink_stmt_vdef (stmt
);
3777 release_defs (stmt
);
3781 case IFN_GOACC_DIM_SIZE
:
3782 case IFN_GOACC_DIM_POS
:
3783 result
= fold_internal_goacc_dim (stmt
);
3785 case IFN_UBSAN_CHECK_ADD
:
3786 subcode
= PLUS_EXPR
;
3788 case IFN_UBSAN_CHECK_SUB
:
3789 subcode
= MINUS_EXPR
;
3791 case IFN_UBSAN_CHECK_MUL
:
3792 subcode
= MULT_EXPR
;
3794 case IFN_ADD_OVERFLOW
:
3795 subcode
= PLUS_EXPR
;
3798 case IFN_SUB_OVERFLOW
:
3799 subcode
= MINUS_EXPR
;
3802 case IFN_MUL_OVERFLOW
:
3803 subcode
= MULT_EXPR
;
3809 if (subcode
!= ERROR_MARK
)
3811 tree arg0
= gimple_call_arg (stmt
, 0);
3812 tree arg1
= gimple_call_arg (stmt
, 1);
3813 tree type
= TREE_TYPE (arg0
);
3816 tree lhs
= gimple_call_lhs (stmt
);
3817 if (lhs
== NULL_TREE
)
3820 type
= TREE_TYPE (TREE_TYPE (lhs
));
3822 if (type
== NULL_TREE
)
3824 /* x = y + 0; x = y - 0; x = y * 0; */
3825 else if (integer_zerop (arg1
))
3826 result
= subcode
== MULT_EXPR
? integer_zero_node
: arg0
;
3827 /* x = 0 + y; x = 0 * y; */
3828 else if (subcode
!= MINUS_EXPR
&& integer_zerop (arg0
))
3829 result
= subcode
== MULT_EXPR
? integer_zero_node
: arg1
;
3831 else if (subcode
== MINUS_EXPR
&& operand_equal_p (arg0
, arg1
, 0))
3832 result
= integer_zero_node
;
3833 /* x = y * 1; x = 1 * y; */
3834 else if (subcode
== MULT_EXPR
&& integer_onep (arg1
))
3836 else if (subcode
== MULT_EXPR
&& integer_onep (arg0
))
3838 else if (TREE_CODE (arg0
) == INTEGER_CST
3839 && TREE_CODE (arg1
) == INTEGER_CST
)
3842 result
= int_const_binop (subcode
, fold_convert (type
, arg0
),
3843 fold_convert (type
, arg1
));
3845 result
= int_const_binop (subcode
, arg0
, arg1
);
3846 if (result
&& arith_overflowed_p (subcode
, type
, arg0
, arg1
))
3849 overflow
= build_one_cst (type
);
3856 if (result
== integer_zero_node
)
3857 result
= build_zero_cst (type
);
3858 else if (cplx_result
&& TREE_TYPE (result
) != type
)
3860 if (TREE_CODE (result
) == INTEGER_CST
)
3862 if (arith_overflowed_p (PLUS_EXPR
, type
, result
,
3864 overflow
= build_one_cst (type
);
3866 else if ((!TYPE_UNSIGNED (TREE_TYPE (result
))
3867 && TYPE_UNSIGNED (type
))
3868 || (TYPE_PRECISION (type
)
3869 < (TYPE_PRECISION (TREE_TYPE (result
))
3870 + (TYPE_UNSIGNED (TREE_TYPE (result
))
3871 && !TYPE_UNSIGNED (type
)))))
3874 result
= fold_convert (type
, result
);
3881 if (TREE_CODE (result
) == INTEGER_CST
&& TREE_OVERFLOW (result
))
3882 result
= drop_tree_overflow (result
);
3885 if (overflow
== NULL_TREE
)
3886 overflow
= build_zero_cst (TREE_TYPE (result
));
3887 tree ctype
= build_complex_type (TREE_TYPE (result
));
3888 if (TREE_CODE (result
) == INTEGER_CST
3889 && TREE_CODE (overflow
) == INTEGER_CST
)
3890 result
= build_complex (ctype
, result
, overflow
);
3892 result
= build2_loc (gimple_location (stmt
), COMPLEX_EXPR
,
3893 ctype
, result
, overflow
);
3895 if (!update_call_from_tree (gsi
, result
))
3896 gimplify_and_update_call_from_tree (gsi
, result
);
3905 /* Return true whether NAME has a use on STMT. */
3908 has_use_on_stmt (tree name
, gimple
*stmt
)
3910 imm_use_iterator iter
;
3911 use_operand_p use_p
;
3912 FOR_EACH_IMM_USE_FAST (use_p
, iter
, name
)
3913 if (USE_STMT (use_p
) == stmt
)
3918 /* Worker for fold_stmt_1 dispatch to pattern based folding with
3921 Replaces *GSI with the simplification result in RCODE and OPS
3922 and the associated statements in *SEQ. Does the replacement
3923 according to INPLACE and returns true if the operation succeeded. */
3926 replace_stmt_with_simplification (gimple_stmt_iterator
*gsi
,
3927 code_helper rcode
, tree
*ops
,
3928 gimple_seq
*seq
, bool inplace
)
3930 gimple
*stmt
= gsi_stmt (*gsi
);
3932 /* Play safe and do not allow abnormals to be mentioned in
3933 newly created statements. See also maybe_push_res_to_seq.
3934 As an exception allow such uses if there was a use of the
3935 same SSA name on the old stmt. */
3936 if ((TREE_CODE (ops
[0]) == SSA_NAME
3937 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[0])
3938 && !has_use_on_stmt (ops
[0], stmt
))
3940 && TREE_CODE (ops
[1]) == SSA_NAME
3941 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[1])
3942 && !has_use_on_stmt (ops
[1], stmt
))
3944 && TREE_CODE (ops
[2]) == SSA_NAME
3945 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[2])
3946 && !has_use_on_stmt (ops
[2], stmt
))
3947 || (COMPARISON_CLASS_P (ops
[0])
3948 && ((TREE_CODE (TREE_OPERAND (ops
[0], 0)) == SSA_NAME
3949 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops
[0], 0))
3950 && !has_use_on_stmt (TREE_OPERAND (ops
[0], 0), stmt
))
3951 || (TREE_CODE (TREE_OPERAND (ops
[0], 1)) == SSA_NAME
3952 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops
[0], 1))
3953 && !has_use_on_stmt (TREE_OPERAND (ops
[0], 1), stmt
)))))
3956 /* Don't insert new statements when INPLACE is true, even if we could
3957 reuse STMT for the final statement. */
3958 if (inplace
&& !gimple_seq_empty_p (*seq
))
3961 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
3963 gcc_assert (rcode
.is_tree_code ());
3964 if (TREE_CODE_CLASS ((enum tree_code
)rcode
) == tcc_comparison
3965 /* GIMPLE_CONDs condition may not throw. */
3966 && (!flag_exceptions
3967 || !cfun
->can_throw_non_call_exceptions
3968 || !operation_could_trap_p (rcode
,
3969 FLOAT_TYPE_P (TREE_TYPE (ops
[0])),
3971 gimple_cond_set_condition (cond_stmt
, rcode
, ops
[0], ops
[1]);
3972 else if (rcode
== SSA_NAME
)
3973 gimple_cond_set_condition (cond_stmt
, NE_EXPR
, ops
[0],
3974 build_zero_cst (TREE_TYPE (ops
[0])));
3975 else if (rcode
== INTEGER_CST
)
3977 if (integer_zerop (ops
[0]))
3978 gimple_cond_make_false (cond_stmt
);
3980 gimple_cond_make_true (cond_stmt
);
3984 tree res
= maybe_push_res_to_seq (rcode
, boolean_type_node
,
3988 gimple_cond_set_condition (cond_stmt
, NE_EXPR
, res
,
3989 build_zero_cst (TREE_TYPE (res
)));
3993 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3995 fprintf (dump_file
, "gimple_simplified to ");
3996 if (!gimple_seq_empty_p (*seq
))
3997 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
3998 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
),
4001 gsi_insert_seq_before (gsi
, *seq
, GSI_SAME_STMT
);
4004 else if (is_gimple_assign (stmt
)
4005 && rcode
.is_tree_code ())
4008 || gimple_num_ops (stmt
) > get_gimple_rhs_num_ops (rcode
))
4010 maybe_build_generic_op (rcode
,
4011 TREE_TYPE (gimple_assign_lhs (stmt
)), ops
);
4012 gimple_assign_set_rhs_with_ops (gsi
, rcode
, ops
[0], ops
[1], ops
[2]);
4013 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4015 fprintf (dump_file
, "gimple_simplified to ");
4016 if (!gimple_seq_empty_p (*seq
))
4017 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
4018 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
),
4021 gsi_insert_seq_before (gsi
, *seq
, GSI_SAME_STMT
);
4025 else if (rcode
.is_fn_code ()
4026 && gimple_call_combined_fn (stmt
) == rcode
)
4029 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
4031 gcc_assert (ops
[i
] != NULL_TREE
);
4032 gimple_call_set_arg (stmt
, i
, ops
[i
]);
4035 gcc_assert (ops
[i
] == NULL_TREE
);
4036 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4038 fprintf (dump_file
, "gimple_simplified to ");
4039 if (!gimple_seq_empty_p (*seq
))
4040 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
4041 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
), 0, TDF_SLIM
);
4043 gsi_insert_seq_before (gsi
, *seq
, GSI_SAME_STMT
);
4048 if (gimple_has_lhs (stmt
))
4050 tree lhs
= gimple_get_lhs (stmt
);
4051 if (!maybe_push_res_to_seq (rcode
, TREE_TYPE (lhs
),
4054 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4056 fprintf (dump_file
, "gimple_simplified to ");
4057 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
4059 gsi_replace_with_seq_vops (gsi
, *seq
);
4069 /* Canonicalize MEM_REFs invariant address operand after propagation. */
4072 maybe_canonicalize_mem_ref_addr (tree
*t
)
4076 if (TREE_CODE (*t
) == ADDR_EXPR
)
4077 t
= &TREE_OPERAND (*t
, 0);
4079 /* The C and C++ frontends use an ARRAY_REF for indexing with their
4080 generic vector extension. The actual vector referenced is
4081 view-converted to an array type for this purpose. If the index
4082 is constant the canonical representation in the middle-end is a
4083 BIT_FIELD_REF so re-write the former to the latter here. */
4084 if (TREE_CODE (*t
) == ARRAY_REF
4085 && TREE_CODE (TREE_OPERAND (*t
, 0)) == VIEW_CONVERT_EXPR
4086 && TREE_CODE (TREE_OPERAND (*t
, 1)) == INTEGER_CST
4087 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t
, 0), 0))))
4089 tree vtype
= TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t
, 0), 0));
4090 if (VECTOR_TYPE_P (vtype
))
4092 tree low
= array_ref_low_bound (*t
);
4093 if (TREE_CODE (low
) == INTEGER_CST
)
4095 if (tree_int_cst_le (low
, TREE_OPERAND (*t
, 1)))
4097 widest_int idx
= wi::sub (wi::to_widest (TREE_OPERAND (*t
, 1)),
4098 wi::to_widest (low
));
4099 idx
= wi::mul (idx
, wi::to_widest
4100 (TYPE_SIZE (TREE_TYPE (*t
))));
4102 = wi::add (idx
, wi::to_widest (TYPE_SIZE (TREE_TYPE (*t
))));
4103 if (wi::les_p (ext
, wi::to_widest (TYPE_SIZE (vtype
))))
4105 *t
= build3_loc (EXPR_LOCATION (*t
), BIT_FIELD_REF
,
4107 TREE_OPERAND (TREE_OPERAND (*t
, 0), 0),
4108 TYPE_SIZE (TREE_TYPE (*t
)),
4109 wide_int_to_tree (sizetype
, idx
));
4117 while (handled_component_p (*t
))
4118 t
= &TREE_OPERAND (*t
, 0);
4120 /* Canonicalize MEM [&foo.bar, 0] which appears after propagating
4121 of invariant addresses into a SSA name MEM_REF address. */
4122 if (TREE_CODE (*t
) == MEM_REF
4123 || TREE_CODE (*t
) == TARGET_MEM_REF
)
4125 tree addr
= TREE_OPERAND (*t
, 0);
4126 if (TREE_CODE (addr
) == ADDR_EXPR
4127 && (TREE_CODE (TREE_OPERAND (addr
, 0)) == MEM_REF
4128 || handled_component_p (TREE_OPERAND (addr
, 0))))
4131 HOST_WIDE_INT coffset
;
4132 base
= get_addr_base_and_unit_offset (TREE_OPERAND (addr
, 0),
4137 TREE_OPERAND (*t
, 0) = build_fold_addr_expr (base
);
4138 TREE_OPERAND (*t
, 1) = int_const_binop (PLUS_EXPR
,
4139 TREE_OPERAND (*t
, 1),
4140 size_int (coffset
));
4143 gcc_checking_assert (TREE_CODE (TREE_OPERAND (*t
, 0)) == DEBUG_EXPR_DECL
4144 || is_gimple_mem_ref_addr (TREE_OPERAND (*t
, 0)));
4147 /* Canonicalize back MEM_REFs to plain reference trees if the object
4148 accessed is a decl that has the same access semantics as the MEM_REF. */
4149 if (TREE_CODE (*t
) == MEM_REF
4150 && TREE_CODE (TREE_OPERAND (*t
, 0)) == ADDR_EXPR
4151 && integer_zerop (TREE_OPERAND (*t
, 1))
4152 && MR_DEPENDENCE_CLIQUE (*t
) == 0)
4154 tree decl
= TREE_OPERAND (TREE_OPERAND (*t
, 0), 0);
4155 tree alias_type
= TREE_TYPE (TREE_OPERAND (*t
, 1));
4156 if (/* Same volatile qualification. */
4157 TREE_THIS_VOLATILE (*t
) == TREE_THIS_VOLATILE (decl
)
4158 /* Same TBAA behavior with -fstrict-aliasing. */
4159 && !TYPE_REF_CAN_ALIAS_ALL (alias_type
)
4160 && (TYPE_MAIN_VARIANT (TREE_TYPE (decl
))
4161 == TYPE_MAIN_VARIANT (TREE_TYPE (alias_type
)))
4162 /* Same alignment. */
4163 && TYPE_ALIGN (TREE_TYPE (decl
)) == TYPE_ALIGN (TREE_TYPE (*t
))
4164 /* We have to look out here to not drop a required conversion
4165 from the rhs to the lhs if *t appears on the lhs or vice-versa
4166 if it appears on the rhs. Thus require strict type
4168 && types_compatible_p (TREE_TYPE (*t
), TREE_TYPE (decl
)))
4170 *t
= TREE_OPERAND (TREE_OPERAND (*t
, 0), 0);
4175 /* Canonicalize TARGET_MEM_REF in particular with respect to
4176 the indexes becoming constant. */
4177 else if (TREE_CODE (*t
) == TARGET_MEM_REF
)
4179 tree tem
= maybe_fold_tmr (*t
);
4190 /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument
4191 distinguishes both cases. */
4194 fold_stmt_1 (gimple_stmt_iterator
*gsi
, bool inplace
, tree (*valueize
) (tree
))
4196 bool changed
= false;
4197 gimple
*stmt
= gsi_stmt (*gsi
);
4198 bool nowarning
= gimple_no_warning_p (stmt
);
4200 fold_defer_overflow_warnings ();
4202 /* First do required canonicalization of [TARGET_]MEM_REF addresses
4204 ??? This shouldn't be done in generic folding but in the
4205 propagation helpers which also know whether an address was
4207 Also canonicalize operand order. */
4208 switch (gimple_code (stmt
))
4211 if (gimple_assign_rhs_class (stmt
) == GIMPLE_SINGLE_RHS
)
4213 tree
*rhs
= gimple_assign_rhs1_ptr (stmt
);
4214 if ((REFERENCE_CLASS_P (*rhs
)
4215 || TREE_CODE (*rhs
) == ADDR_EXPR
)
4216 && maybe_canonicalize_mem_ref_addr (rhs
))
4218 tree
*lhs
= gimple_assign_lhs_ptr (stmt
);
4219 if (REFERENCE_CLASS_P (*lhs
)
4220 && maybe_canonicalize_mem_ref_addr (lhs
))
4225 /* Canonicalize operand order. */
4226 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4227 if (TREE_CODE_CLASS (code
) == tcc_comparison
4228 || commutative_tree_code (code
)
4229 || commutative_ternary_tree_code (code
))
4231 tree rhs1
= gimple_assign_rhs1 (stmt
);
4232 tree rhs2
= gimple_assign_rhs2 (stmt
);
4233 if (tree_swap_operands_p (rhs1
, rhs2
))
4235 gimple_assign_set_rhs1 (stmt
, rhs2
);
4236 gimple_assign_set_rhs2 (stmt
, rhs1
);
4237 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4238 gimple_assign_set_rhs_code (stmt
,
4239 swap_tree_comparison (code
));
4247 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
4249 tree
*arg
= gimple_call_arg_ptr (stmt
, i
);
4250 if (REFERENCE_CLASS_P (*arg
)
4251 && maybe_canonicalize_mem_ref_addr (arg
))
4254 tree
*lhs
= gimple_call_lhs_ptr (stmt
);
4256 && REFERENCE_CLASS_P (*lhs
)
4257 && maybe_canonicalize_mem_ref_addr (lhs
))
4263 gasm
*asm_stmt
= as_a
<gasm
*> (stmt
);
4264 for (i
= 0; i
< gimple_asm_noutputs (asm_stmt
); ++i
)
4266 tree link
= gimple_asm_output_op (asm_stmt
, i
);
4267 tree op
= TREE_VALUE (link
);
4268 if (REFERENCE_CLASS_P (op
)
4269 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link
)))
4272 for (i
= 0; i
< gimple_asm_ninputs (asm_stmt
); ++i
)
4274 tree link
= gimple_asm_input_op (asm_stmt
, i
);
4275 tree op
= TREE_VALUE (link
);
4276 if ((REFERENCE_CLASS_P (op
)
4277 || TREE_CODE (op
) == ADDR_EXPR
)
4278 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link
)))
4284 if (gimple_debug_bind_p (stmt
))
4286 tree
*val
= gimple_debug_bind_get_value_ptr (stmt
);
4288 && (REFERENCE_CLASS_P (*val
)
4289 || TREE_CODE (*val
) == ADDR_EXPR
)
4290 && maybe_canonicalize_mem_ref_addr (val
))
4296 /* Canonicalize operand order. */
4297 tree lhs
= gimple_cond_lhs (stmt
);
4298 tree rhs
= gimple_cond_rhs (stmt
);
4299 if (tree_swap_operands_p (lhs
, rhs
))
4301 gcond
*gc
= as_a
<gcond
*> (stmt
);
4302 gimple_cond_set_lhs (gc
, rhs
);
4303 gimple_cond_set_rhs (gc
, lhs
);
4304 gimple_cond_set_code (gc
,
4305 swap_tree_comparison (gimple_cond_code (gc
)));
4312 /* Dispatch to pattern-based folding. */
4314 || is_gimple_assign (stmt
)
4315 || gimple_code (stmt
) == GIMPLE_COND
)
4317 gimple_seq seq
= NULL
;
4320 if (gimple_simplify (stmt
, &rcode
, ops
, inplace
? NULL
: &seq
,
4321 valueize
, valueize
))
4323 if (replace_stmt_with_simplification (gsi
, rcode
, ops
, &seq
, inplace
))
4326 gimple_seq_discard (seq
);
4330 stmt
= gsi_stmt (*gsi
);
4332 /* Fold the main computation performed by the statement. */
4333 switch (gimple_code (stmt
))
4337 /* Try to canonicalize for boolean-typed X the comparisons
4338 X == 0, X == 1, X != 0, and X != 1. */
4339 if (gimple_assign_rhs_code (stmt
) == EQ_EXPR
4340 || gimple_assign_rhs_code (stmt
) == NE_EXPR
)
4342 tree lhs
= gimple_assign_lhs (stmt
);
4343 tree op1
= gimple_assign_rhs1 (stmt
);
4344 tree op2
= gimple_assign_rhs2 (stmt
);
4345 tree type
= TREE_TYPE (op1
);
4347 /* Check whether the comparison operands are of the same boolean
4348 type as the result type is.
4349 Check that second operand is an integer-constant with value
4351 if (TREE_CODE (op2
) == INTEGER_CST
4352 && (integer_zerop (op2
) || integer_onep (op2
))
4353 && useless_type_conversion_p (TREE_TYPE (lhs
), type
))
4355 enum tree_code cmp_code
= gimple_assign_rhs_code (stmt
);
4356 bool is_logical_not
= false;
4358 /* X == 0 and X != 1 is a logical-not.of X
4359 X == 1 and X != 0 is X */
4360 if ((cmp_code
== EQ_EXPR
&& integer_zerop (op2
))
4361 || (cmp_code
== NE_EXPR
&& integer_onep (op2
)))
4362 is_logical_not
= true;
4364 if (is_logical_not
== false)
4365 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op1
), op1
);
4366 /* Only for one-bit precision typed X the transformation
4367 !X -> ~X is valied. */
4368 else if (TYPE_PRECISION (type
) == 1)
4369 gimple_assign_set_rhs_with_ops (gsi
, BIT_NOT_EXPR
, op1
);
4370 /* Otherwise we use !X -> X ^ 1. */
4372 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op1
,
4373 build_int_cst (type
, 1));
4379 unsigned old_num_ops
= gimple_num_ops (stmt
);
4380 tree lhs
= gimple_assign_lhs (stmt
);
4381 tree new_rhs
= fold_gimple_assign (gsi
);
4383 && !useless_type_conversion_p (TREE_TYPE (lhs
),
4384 TREE_TYPE (new_rhs
)))
4385 new_rhs
= fold_convert (TREE_TYPE (lhs
), new_rhs
);
4388 || get_gimple_rhs_num_ops (TREE_CODE (new_rhs
)) < old_num_ops
))
4390 gimple_assign_set_rhs_from_tree (gsi
, new_rhs
);
4397 changed
|= gimple_fold_call (gsi
, inplace
);
4401 /* Fold *& in asm operands. */
4403 gasm
*asm_stmt
= as_a
<gasm
*> (stmt
);
4405 const char **oconstraints
;
4406 const char *constraint
;
4407 bool allows_mem
, allows_reg
;
4409 noutputs
= gimple_asm_noutputs (asm_stmt
);
4410 oconstraints
= XALLOCAVEC (const char *, noutputs
);
4412 for (i
= 0; i
< gimple_asm_noutputs (asm_stmt
); ++i
)
4414 tree link
= gimple_asm_output_op (asm_stmt
, i
);
4415 tree op
= TREE_VALUE (link
);
4417 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
4418 if (REFERENCE_CLASS_P (op
)
4419 && (op
= maybe_fold_reference (op
, true)) != NULL_TREE
)
4421 TREE_VALUE (link
) = op
;
4425 for (i
= 0; i
< gimple_asm_ninputs (asm_stmt
); ++i
)
4427 tree link
= gimple_asm_input_op (asm_stmt
, i
);
4428 tree op
= TREE_VALUE (link
);
4430 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
4431 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0,
4432 oconstraints
, &allows_mem
, &allows_reg
);
4433 if (REFERENCE_CLASS_P (op
)
4434 && (op
= maybe_fold_reference (op
, !allows_reg
&& allows_mem
))
4437 TREE_VALUE (link
) = op
;
4445 if (gimple_debug_bind_p (stmt
))
4447 tree val
= gimple_debug_bind_get_value (stmt
);
4449 && REFERENCE_CLASS_P (val
))
4451 tree tem
= maybe_fold_reference (val
, false);
4454 gimple_debug_bind_set_value (stmt
, tem
);
4459 && TREE_CODE (val
) == ADDR_EXPR
)
4461 tree ref
= TREE_OPERAND (val
, 0);
4462 tree tem
= maybe_fold_reference (ref
, false);
4465 tem
= build_fold_addr_expr_with_type (tem
, TREE_TYPE (val
));
4466 gimple_debug_bind_set_value (stmt
, tem
);
4475 greturn
*ret_stmt
= as_a
<greturn
*> (stmt
);
4476 tree ret
= gimple_return_retval(ret_stmt
);
4478 if (ret
&& TREE_CODE (ret
) == SSA_NAME
&& valueize
)
4480 tree val
= valueize (ret
);
4481 if (val
&& val
!= ret
4482 && may_propagate_copy (ret
, val
))
4484 gimple_return_set_retval (ret_stmt
, val
);
4494 stmt
= gsi_stmt (*gsi
);
4496 /* Fold *& on the lhs. */
4497 if (gimple_has_lhs (stmt
))
4499 tree lhs
= gimple_get_lhs (stmt
);
4500 if (lhs
&& REFERENCE_CLASS_P (lhs
))
4502 tree new_lhs
= maybe_fold_reference (lhs
, true);
4505 gimple_set_lhs (stmt
, new_lhs
);
4511 fold_undefer_overflow_warnings (changed
&& !nowarning
, stmt
, 0);
4515 /* Valueziation callback that ends up not following SSA edges. */
4518 no_follow_ssa_edges (tree
)
4523 /* Valueization callback that ends up following single-use SSA edges only. */
4526 follow_single_use_edges (tree val
)
4528 if (TREE_CODE (val
) == SSA_NAME
4529 && !has_single_use (val
))
4534 /* Fold the statement pointed to by GSI. In some cases, this function may
4535 replace the whole statement with a new one. Returns true iff folding
4537 The statement pointed to by GSI should be in valid gimple form but may
4538 be in unfolded state as resulting from for example constant propagation
4539 which can produce *&x = 0. */
4542 fold_stmt (gimple_stmt_iterator
*gsi
)
4544 return fold_stmt_1 (gsi
, false, no_follow_ssa_edges
);
4548 fold_stmt (gimple_stmt_iterator
*gsi
, tree (*valueize
) (tree
))
4550 return fold_stmt_1 (gsi
, false, valueize
);
4553 /* Perform the minimal folding on statement *GSI. Only operations like
4554 *&x created by constant propagation are handled. The statement cannot
4555 be replaced with a new one. Return true if the statement was
4556 changed, false otherwise.
4557 The statement *GSI should be in valid gimple form but may
4558 be in unfolded state as resulting from for example constant propagation
4559 which can produce *&x = 0. */
4562 fold_stmt_inplace (gimple_stmt_iterator
*gsi
)
4564 gimple
*stmt
= gsi_stmt (*gsi
);
4565 bool changed
= fold_stmt_1 (gsi
, true, no_follow_ssa_edges
);
4566 gcc_assert (gsi_stmt (*gsi
) == stmt
);
4570 /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
4571 if EXPR is null or we don't know how.
4572 If non-null, the result always has boolean type. */
4575 canonicalize_bool (tree expr
, bool invert
)
4581 if (integer_nonzerop (expr
))
4582 return boolean_false_node
;
4583 else if (integer_zerop (expr
))
4584 return boolean_true_node
;
4585 else if (TREE_CODE (expr
) == SSA_NAME
)
4586 return fold_build2 (EQ_EXPR
, boolean_type_node
, expr
,
4587 build_int_cst (TREE_TYPE (expr
), 0));
4588 else if (COMPARISON_CLASS_P (expr
))
4589 return fold_build2 (invert_tree_comparison (TREE_CODE (expr
), false),
4591 TREE_OPERAND (expr
, 0),
4592 TREE_OPERAND (expr
, 1));
4598 if (TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
)
4600 if (integer_nonzerop (expr
))
4601 return boolean_true_node
;
4602 else if (integer_zerop (expr
))
4603 return boolean_false_node
;
4604 else if (TREE_CODE (expr
) == SSA_NAME
)
4605 return fold_build2 (NE_EXPR
, boolean_type_node
, expr
,
4606 build_int_cst (TREE_TYPE (expr
), 0));
4607 else if (COMPARISON_CLASS_P (expr
))
4608 return fold_build2 (TREE_CODE (expr
),
4610 TREE_OPERAND (expr
, 0),
4611 TREE_OPERAND (expr
, 1));
4617 /* Check to see if a boolean expression EXPR is logically equivalent to the
4618 comparison (OP1 CODE OP2). Check for various identities involving
4622 same_bool_comparison_p (const_tree expr
, enum tree_code code
,
4623 const_tree op1
, const_tree op2
)
4627 /* The obvious case. */
4628 if (TREE_CODE (expr
) == code
4629 && operand_equal_p (TREE_OPERAND (expr
, 0), op1
, 0)
4630 && operand_equal_p (TREE_OPERAND (expr
, 1), op2
, 0))
4633 /* Check for comparing (name, name != 0) and the case where expr
4634 is an SSA_NAME with a definition matching the comparison. */
4635 if (TREE_CODE (expr
) == SSA_NAME
4636 && TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
)
4638 if (operand_equal_p (expr
, op1
, 0))
4639 return ((code
== NE_EXPR
&& integer_zerop (op2
))
4640 || (code
== EQ_EXPR
&& integer_nonzerop (op2
)));
4641 s
= SSA_NAME_DEF_STMT (expr
);
4642 if (is_gimple_assign (s
)
4643 && gimple_assign_rhs_code (s
) == code
4644 && operand_equal_p (gimple_assign_rhs1 (s
), op1
, 0)
4645 && operand_equal_p (gimple_assign_rhs2 (s
), op2
, 0))
4649 /* If op1 is of the form (name != 0) or (name == 0), and the definition
4650 of name is a comparison, recurse. */
4651 if (TREE_CODE (op1
) == SSA_NAME
4652 && TREE_CODE (TREE_TYPE (op1
)) == BOOLEAN_TYPE
)
4654 s
= SSA_NAME_DEF_STMT (op1
);
4655 if (is_gimple_assign (s
)
4656 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
)
4658 enum tree_code c
= gimple_assign_rhs_code (s
);
4659 if ((c
== NE_EXPR
&& integer_zerop (op2
))
4660 || (c
== EQ_EXPR
&& integer_nonzerop (op2
)))
4661 return same_bool_comparison_p (expr
, c
,
4662 gimple_assign_rhs1 (s
),
4663 gimple_assign_rhs2 (s
));
4664 if ((c
== EQ_EXPR
&& integer_zerop (op2
))
4665 || (c
== NE_EXPR
&& integer_nonzerop (op2
)))
4666 return same_bool_comparison_p (expr
,
4667 invert_tree_comparison (c
, false),
4668 gimple_assign_rhs1 (s
),
4669 gimple_assign_rhs2 (s
));
4675 /* Check to see if two boolean expressions OP1 and OP2 are logically
4679 same_bool_result_p (const_tree op1
, const_tree op2
)
4681 /* Simple cases first. */
4682 if (operand_equal_p (op1
, op2
, 0))
4685 /* Check the cases where at least one of the operands is a comparison.
4686 These are a bit smarter than operand_equal_p in that they apply some
4687 identifies on SSA_NAMEs. */
4688 if (COMPARISON_CLASS_P (op2
)
4689 && same_bool_comparison_p (op1
, TREE_CODE (op2
),
4690 TREE_OPERAND (op2
, 0),
4691 TREE_OPERAND (op2
, 1)))
4693 if (COMPARISON_CLASS_P (op1
)
4694 && same_bool_comparison_p (op2
, TREE_CODE (op1
),
4695 TREE_OPERAND (op1
, 0),
4696 TREE_OPERAND (op1
, 1)))
4703 /* Forward declarations for some mutually recursive functions. */
4706 and_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
4707 enum tree_code code2
, tree op2a
, tree op2b
);
4709 and_var_with_comparison (tree var
, bool invert
,
4710 enum tree_code code2
, tree op2a
, tree op2b
);
4712 and_var_with_comparison_1 (gimple
*stmt
,
4713 enum tree_code code2
, tree op2a
, tree op2b
);
4715 or_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
4716 enum tree_code code2
, tree op2a
, tree op2b
);
4718 or_var_with_comparison (tree var
, bool invert
,
4719 enum tree_code code2
, tree op2a
, tree op2b
);
4721 or_var_with_comparison_1 (gimple
*stmt
,
4722 enum tree_code code2
, tree op2a
, tree op2b
);
4724 /* Helper function for and_comparisons_1: try to simplify the AND of the
4725 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
4726 If INVERT is true, invert the value of the VAR before doing the AND.
4727 Return NULL_EXPR if we can't simplify this to a single expression. */
4730 and_var_with_comparison (tree var
, bool invert
,
4731 enum tree_code code2
, tree op2a
, tree op2b
)
4734 gimple
*stmt
= SSA_NAME_DEF_STMT (var
);
4736 /* We can only deal with variables whose definitions are assignments. */
4737 if (!is_gimple_assign (stmt
))
4740 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
4741 !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
4742 Then we only have to consider the simpler non-inverted cases. */
4744 t
= or_var_with_comparison_1 (stmt
,
4745 invert_tree_comparison (code2
, false),
4748 t
= and_var_with_comparison_1 (stmt
, code2
, op2a
, op2b
);
4749 return canonicalize_bool (t
, invert
);
4752 /* Try to simplify the AND of the ssa variable defined by the assignment
4753 STMT with the comparison specified by (OP2A CODE2 OP2B).
4754 Return NULL_EXPR if we can't simplify this to a single expression. */
4757 and_var_with_comparison_1 (gimple
*stmt
,
4758 enum tree_code code2
, tree op2a
, tree op2b
)
4760 tree var
= gimple_assign_lhs (stmt
);
4761 tree true_test_var
= NULL_TREE
;
4762 tree false_test_var
= NULL_TREE
;
4763 enum tree_code innercode
= gimple_assign_rhs_code (stmt
);
4765 /* Check for identities like (var AND (var == 0)) => false. */
4766 if (TREE_CODE (op2a
) == SSA_NAME
4767 && TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
)
4769 if ((code2
== NE_EXPR
&& integer_zerop (op2b
))
4770 || (code2
== EQ_EXPR
&& integer_nonzerop (op2b
)))
4772 true_test_var
= op2a
;
4773 if (var
== true_test_var
)
4776 else if ((code2
== EQ_EXPR
&& integer_zerop (op2b
))
4777 || (code2
== NE_EXPR
&& integer_nonzerop (op2b
)))
4779 false_test_var
= op2a
;
4780 if (var
== false_test_var
)
4781 return boolean_false_node
;
4785 /* If the definition is a comparison, recurse on it. */
4786 if (TREE_CODE_CLASS (innercode
) == tcc_comparison
)
4788 tree t
= and_comparisons_1 (innercode
,
4789 gimple_assign_rhs1 (stmt
),
4790 gimple_assign_rhs2 (stmt
),
4798 /* If the definition is an AND or OR expression, we may be able to
4799 simplify by reassociating. */
4800 if (TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
4801 && (innercode
== BIT_AND_EXPR
|| innercode
== BIT_IOR_EXPR
))
4803 tree inner1
= gimple_assign_rhs1 (stmt
);
4804 tree inner2
= gimple_assign_rhs2 (stmt
);
4807 tree partial
= NULL_TREE
;
4808 bool is_and
= (innercode
== BIT_AND_EXPR
);
4810 /* Check for boolean identities that don't require recursive examination
4812 inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
4813 inner1 AND (inner1 OR inner2) => inner1
4814 !inner1 AND (inner1 AND inner2) => false
4815 !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
4816 Likewise for similar cases involving inner2. */
4817 if (inner1
== true_test_var
)
4818 return (is_and
? var
: inner1
);
4819 else if (inner2
== true_test_var
)
4820 return (is_and
? var
: inner2
);
4821 else if (inner1
== false_test_var
)
4823 ? boolean_false_node
4824 : and_var_with_comparison (inner2
, false, code2
, op2a
, op2b
));
4825 else if (inner2
== false_test_var
)
4827 ? boolean_false_node
4828 : and_var_with_comparison (inner1
, false, code2
, op2a
, op2b
));
4830 /* Next, redistribute/reassociate the AND across the inner tests.
4831 Compute the first partial result, (inner1 AND (op2a code op2b)) */
4832 if (TREE_CODE (inner1
) == SSA_NAME
4833 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner1
))
4834 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
4835 && (t
= maybe_fold_and_comparisons (gimple_assign_rhs_code (s
),
4836 gimple_assign_rhs1 (s
),
4837 gimple_assign_rhs2 (s
),
4838 code2
, op2a
, op2b
)))
4840 /* Handle the AND case, where we are reassociating:
4841 (inner1 AND inner2) AND (op2a code2 op2b)
4843 If the partial result t is a constant, we win. Otherwise
4844 continue on to try reassociating with the other inner test. */
4847 if (integer_onep (t
))
4849 else if (integer_zerop (t
))
4850 return boolean_false_node
;
4853 /* Handle the OR case, where we are redistributing:
4854 (inner1 OR inner2) AND (op2a code2 op2b)
4855 => (t OR (inner2 AND (op2a code2 op2b))) */
4856 else if (integer_onep (t
))
4857 return boolean_true_node
;
4859 /* Save partial result for later. */
4863 /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
4864 if (TREE_CODE (inner2
) == SSA_NAME
4865 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner2
))
4866 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
4867 && (t
= maybe_fold_and_comparisons (gimple_assign_rhs_code (s
),
4868 gimple_assign_rhs1 (s
),
4869 gimple_assign_rhs2 (s
),
4870 code2
, op2a
, op2b
)))
4872 /* Handle the AND case, where we are reassociating:
4873 (inner1 AND inner2) AND (op2a code2 op2b)
4874 => (inner1 AND t) */
4877 if (integer_onep (t
))
4879 else if (integer_zerop (t
))
4880 return boolean_false_node
;
4881 /* If both are the same, we can apply the identity
4883 else if (partial
&& same_bool_result_p (t
, partial
))
4887 /* Handle the OR case. where we are redistributing:
4888 (inner1 OR inner2) AND (op2a code2 op2b)
4889 => (t OR (inner1 AND (op2a code2 op2b)))
4890 => (t OR partial) */
4893 if (integer_onep (t
))
4894 return boolean_true_node
;
4897 /* We already got a simplification for the other
4898 operand to the redistributed OR expression. The
4899 interesting case is when at least one is false.
4900 Or, if both are the same, we can apply the identity
4902 if (integer_zerop (partial
))
4904 else if (integer_zerop (t
))
4906 else if (same_bool_result_p (t
, partial
))
4915 /* Try to simplify the AND of two comparisons defined by
4916 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
4917 If this can be done without constructing an intermediate value,
4918 return the resulting tree; otherwise NULL_TREE is returned.
4919 This function is deliberately asymmetric as it recurses on SSA_DEFs
4920 in the first comparison but not the second. */
4923 and_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
4924 enum tree_code code2
, tree op2a
, tree op2b
)
4926 tree truth_type
= truth_type_for (TREE_TYPE (op1a
));
4928 /* First check for ((x CODE1 y) AND (x CODE2 y)). */
4929 if (operand_equal_p (op1a
, op2a
, 0)
4930 && operand_equal_p (op1b
, op2b
, 0))
4932 /* Result will be either NULL_TREE, or a combined comparison. */
4933 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
4934 TRUTH_ANDIF_EXPR
, code1
, code2
,
4935 truth_type
, op1a
, op1b
);
4940 /* Likewise the swapped case of the above. */
4941 if (operand_equal_p (op1a
, op2b
, 0)
4942 && operand_equal_p (op1b
, op2a
, 0))
4944 /* Result will be either NULL_TREE, or a combined comparison. */
4945 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
4946 TRUTH_ANDIF_EXPR
, code1
,
4947 swap_tree_comparison (code2
),
4948 truth_type
, op1a
, op1b
);
4953 /* If both comparisons are of the same value against constants, we might
4954 be able to merge them. */
4955 if (operand_equal_p (op1a
, op2a
, 0)
4956 && TREE_CODE (op1b
) == INTEGER_CST
4957 && TREE_CODE (op2b
) == INTEGER_CST
)
4959 int cmp
= tree_int_cst_compare (op1b
, op2b
);
4961 /* If we have (op1a == op1b), we should either be able to
4962 return that or FALSE, depending on whether the constant op1b
4963 also satisfies the other comparison against op2b. */
4964 if (code1
== EQ_EXPR
)
4970 case EQ_EXPR
: val
= (cmp
== 0); break;
4971 case NE_EXPR
: val
= (cmp
!= 0); break;
4972 case LT_EXPR
: val
= (cmp
< 0); break;
4973 case GT_EXPR
: val
= (cmp
> 0); break;
4974 case LE_EXPR
: val
= (cmp
<= 0); break;
4975 case GE_EXPR
: val
= (cmp
>= 0); break;
4976 default: done
= false;
4981 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
4983 return boolean_false_node
;
4986 /* Likewise if the second comparison is an == comparison. */
4987 else if (code2
== EQ_EXPR
)
4993 case EQ_EXPR
: val
= (cmp
== 0); break;
4994 case NE_EXPR
: val
= (cmp
!= 0); break;
4995 case LT_EXPR
: val
= (cmp
> 0); break;
4996 case GT_EXPR
: val
= (cmp
< 0); break;
4997 case LE_EXPR
: val
= (cmp
>= 0); break;
4998 case GE_EXPR
: val
= (cmp
<= 0); break;
4999 default: done
= false;
5004 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5006 return boolean_false_node
;
5010 /* Same business with inequality tests. */
5011 else if (code1
== NE_EXPR
)
5016 case EQ_EXPR
: val
= (cmp
!= 0); break;
5017 case NE_EXPR
: val
= (cmp
== 0); break;
5018 case LT_EXPR
: val
= (cmp
>= 0); break;
5019 case GT_EXPR
: val
= (cmp
<= 0); break;
5020 case LE_EXPR
: val
= (cmp
> 0); break;
5021 case GE_EXPR
: val
= (cmp
< 0); break;
5026 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5028 else if (code2
== NE_EXPR
)
5033 case EQ_EXPR
: val
= (cmp
== 0); break;
5034 case NE_EXPR
: val
= (cmp
!= 0); break;
5035 case LT_EXPR
: val
= (cmp
<= 0); break;
5036 case GT_EXPR
: val
= (cmp
>= 0); break;
5037 case LE_EXPR
: val
= (cmp
< 0); break;
5038 case GE_EXPR
: val
= (cmp
> 0); break;
5043 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5046 /* Chose the more restrictive of two < or <= comparisons. */
5047 else if ((code1
== LT_EXPR
|| code1
== LE_EXPR
)
5048 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
5050 if ((cmp
< 0) || (cmp
== 0 && code1
== LT_EXPR
))
5051 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5053 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5056 /* Likewise chose the more restrictive of two > or >= comparisons. */
5057 else if ((code1
== GT_EXPR
|| code1
== GE_EXPR
)
5058 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
5060 if ((cmp
> 0) || (cmp
== 0 && code1
== GT_EXPR
))
5061 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5063 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5066 /* Check for singleton ranges. */
5068 && ((code1
== LE_EXPR
&& code2
== GE_EXPR
)
5069 || (code1
== GE_EXPR
&& code2
== LE_EXPR
)))
5070 return fold_build2 (EQ_EXPR
, boolean_type_node
, op1a
, op2b
);
5072 /* Check for disjoint ranges. */
5074 && (code1
== LT_EXPR
|| code1
== LE_EXPR
)
5075 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
5076 return boolean_false_node
;
5078 && (code1
== GT_EXPR
|| code1
== GE_EXPR
)
5079 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
5080 return boolean_false_node
;
5083 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
5084 NAME's definition is a truth value. See if there are any simplifications
5085 that can be done against the NAME's definition. */
5086 if (TREE_CODE (op1a
) == SSA_NAME
5087 && (code1
== NE_EXPR
|| code1
== EQ_EXPR
)
5088 && (integer_zerop (op1b
) || integer_onep (op1b
)))
5090 bool invert
= ((code1
== EQ_EXPR
&& integer_zerop (op1b
))
5091 || (code1
== NE_EXPR
&& integer_onep (op1b
)));
5092 gimple
*stmt
= SSA_NAME_DEF_STMT (op1a
);
5093 switch (gimple_code (stmt
))
5096 /* Try to simplify by copy-propagating the definition. */
5097 return and_var_with_comparison (op1a
, invert
, code2
, op2a
, op2b
);
5100 /* If every argument to the PHI produces the same result when
5101 ANDed with the second comparison, we win.
5102 Do not do this unless the type is bool since we need a bool
5103 result here anyway. */
5104 if (TREE_CODE (TREE_TYPE (op1a
)) == BOOLEAN_TYPE
)
5106 tree result
= NULL_TREE
;
5108 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
5110 tree arg
= gimple_phi_arg_def (stmt
, i
);
5112 /* If this PHI has itself as an argument, ignore it.
5113 If all the other args produce the same result,
5115 if (arg
== gimple_phi_result (stmt
))
5117 else if (TREE_CODE (arg
) == INTEGER_CST
)
5119 if (invert
? integer_nonzerop (arg
) : integer_zerop (arg
))
5122 result
= boolean_false_node
;
5123 else if (!integer_zerop (result
))
5127 result
= fold_build2 (code2
, boolean_type_node
,
5129 else if (!same_bool_comparison_p (result
,
5133 else if (TREE_CODE (arg
) == SSA_NAME
5134 && !SSA_NAME_IS_DEFAULT_DEF (arg
))
5137 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
5138 /* In simple cases we can look through PHI nodes,
5139 but we have to be careful with loops.
5141 if (! dom_info_available_p (CDI_DOMINATORS
)
5142 || gimple_bb (def_stmt
) == gimple_bb (stmt
)
5143 || dominated_by_p (CDI_DOMINATORS
,
5144 gimple_bb (def_stmt
),
5147 temp
= and_var_with_comparison (arg
, invert
, code2
,
5153 else if (!same_bool_result_p (result
, temp
))
5169 /* Try to simplify the AND of two comparisons, specified by
5170 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
5171 If this can be simplified to a single expression (without requiring
5172 introducing more SSA variables to hold intermediate values),
5173 return the resulting tree. Otherwise return NULL_TREE.
5174 If the result expression is non-null, it has boolean type. */
5177 maybe_fold_and_comparisons (enum tree_code code1
, tree op1a
, tree op1b
,
5178 enum tree_code code2
, tree op2a
, tree op2b
)
5180 tree t
= and_comparisons_1 (code1
, op1a
, op1b
, code2
, op2a
, op2b
);
5184 return and_comparisons_1 (code2
, op2a
, op2b
, code1
, op1a
, op1b
);
5187 /* Helper function for or_comparisons_1: try to simplify the OR of the
5188 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
5189 If INVERT is true, invert the value of VAR before doing the OR.
5190 Return NULL_EXPR if we can't simplify this to a single expression. */
5193 or_var_with_comparison (tree var
, bool invert
,
5194 enum tree_code code2
, tree op2a
, tree op2b
)
5197 gimple
*stmt
= SSA_NAME_DEF_STMT (var
);
5199 /* We can only deal with variables whose definitions are assignments. */
5200 if (!is_gimple_assign (stmt
))
5203 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
5204 !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
5205 Then we only have to consider the simpler non-inverted cases. */
5207 t
= and_var_with_comparison_1 (stmt
,
5208 invert_tree_comparison (code2
, false),
5211 t
= or_var_with_comparison_1 (stmt
, code2
, op2a
, op2b
);
5212 return canonicalize_bool (t
, invert
);
5215 /* Try to simplify the OR of the ssa variable defined by the assignment
5216 STMT with the comparison specified by (OP2A CODE2 OP2B).
5217 Return NULL_EXPR if we can't simplify this to a single expression. */
5220 or_var_with_comparison_1 (gimple
*stmt
,
5221 enum tree_code code2
, tree op2a
, tree op2b
)
5223 tree var
= gimple_assign_lhs (stmt
);
5224 tree true_test_var
= NULL_TREE
;
5225 tree false_test_var
= NULL_TREE
;
5226 enum tree_code innercode
= gimple_assign_rhs_code (stmt
);
5228 /* Check for identities like (var OR (var != 0)) => true . */
5229 if (TREE_CODE (op2a
) == SSA_NAME
5230 && TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
)
5232 if ((code2
== NE_EXPR
&& integer_zerop (op2b
))
5233 || (code2
== EQ_EXPR
&& integer_nonzerop (op2b
)))
5235 true_test_var
= op2a
;
5236 if (var
== true_test_var
)
5239 else if ((code2
== EQ_EXPR
&& integer_zerop (op2b
))
5240 || (code2
== NE_EXPR
&& integer_nonzerop (op2b
)))
5242 false_test_var
= op2a
;
5243 if (var
== false_test_var
)
5244 return boolean_true_node
;
5248 /* If the definition is a comparison, recurse on it. */
5249 if (TREE_CODE_CLASS (innercode
) == tcc_comparison
)
5251 tree t
= or_comparisons_1 (innercode
,
5252 gimple_assign_rhs1 (stmt
),
5253 gimple_assign_rhs2 (stmt
),
5261 /* If the definition is an AND or OR expression, we may be able to
5262 simplify by reassociating. */
5263 if (TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
5264 && (innercode
== BIT_AND_EXPR
|| innercode
== BIT_IOR_EXPR
))
5266 tree inner1
= gimple_assign_rhs1 (stmt
);
5267 tree inner2
= gimple_assign_rhs2 (stmt
);
5270 tree partial
= NULL_TREE
;
5271 bool is_or
= (innercode
== BIT_IOR_EXPR
);
5273 /* Check for boolean identities that don't require recursive examination
5275 inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
5276 inner1 OR (inner1 AND inner2) => inner1
5277 !inner1 OR (inner1 OR inner2) => true
5278 !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
5280 if (inner1
== true_test_var
)
5281 return (is_or
? var
: inner1
);
5282 else if (inner2
== true_test_var
)
5283 return (is_or
? var
: inner2
);
5284 else if (inner1
== false_test_var
)
5287 : or_var_with_comparison (inner2
, false, code2
, op2a
, op2b
));
5288 else if (inner2
== false_test_var
)
5291 : or_var_with_comparison (inner1
, false, code2
, op2a
, op2b
));
5293 /* Next, redistribute/reassociate the OR across the inner tests.
5294 Compute the first partial result, (inner1 OR (op2a code op2b)) */
5295 if (TREE_CODE (inner1
) == SSA_NAME
5296 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner1
))
5297 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
5298 && (t
= maybe_fold_or_comparisons (gimple_assign_rhs_code (s
),
5299 gimple_assign_rhs1 (s
),
5300 gimple_assign_rhs2 (s
),
5301 code2
, op2a
, op2b
)))
5303 /* Handle the OR case, where we are reassociating:
5304 (inner1 OR inner2) OR (op2a code2 op2b)
5306 If the partial result t is a constant, we win. Otherwise
5307 continue on to try reassociating with the other inner test. */
5310 if (integer_onep (t
))
5311 return boolean_true_node
;
5312 else if (integer_zerop (t
))
5316 /* Handle the AND case, where we are redistributing:
5317 (inner1 AND inner2) OR (op2a code2 op2b)
5318 => (t AND (inner2 OR (op2a code op2b))) */
5319 else if (integer_zerop (t
))
5320 return boolean_false_node
;
5322 /* Save partial result for later. */
5326 /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
5327 if (TREE_CODE (inner2
) == SSA_NAME
5328 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner2
))
5329 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
5330 && (t
= maybe_fold_or_comparisons (gimple_assign_rhs_code (s
),
5331 gimple_assign_rhs1 (s
),
5332 gimple_assign_rhs2 (s
),
5333 code2
, op2a
, op2b
)))
5335 /* Handle the OR case, where we are reassociating:
5336 (inner1 OR inner2) OR (op2a code2 op2b)
5338 => (t OR partial) */
5341 if (integer_zerop (t
))
5343 else if (integer_onep (t
))
5344 return boolean_true_node
;
5345 /* If both are the same, we can apply the identity
5347 else if (partial
&& same_bool_result_p (t
, partial
))
5351 /* Handle the AND case, where we are redistributing:
5352 (inner1 AND inner2) OR (op2a code2 op2b)
5353 => (t AND (inner1 OR (op2a code2 op2b)))
5354 => (t AND partial) */
5357 if (integer_zerop (t
))
5358 return boolean_false_node
;
5361 /* We already got a simplification for the other
5362 operand to the redistributed AND expression. The
5363 interesting case is when at least one is true.
5364 Or, if both are the same, we can apply the identity
5366 if (integer_onep (partial
))
5368 else if (integer_onep (t
))
5370 else if (same_bool_result_p (t
, partial
))
5379 /* Try to simplify the OR of two comparisons defined by
5380 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
5381 If this can be done without constructing an intermediate value,
5382 return the resulting tree; otherwise NULL_TREE is returned.
5383 This function is deliberately asymmetric as it recurses on SSA_DEFs
5384 in the first comparison but not the second. */
5387 or_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
5388 enum tree_code code2
, tree op2a
, tree op2b
)
5390 tree truth_type
= truth_type_for (TREE_TYPE (op1a
));
5392 /* First check for ((x CODE1 y) OR (x CODE2 y)). */
5393 if (operand_equal_p (op1a
, op2a
, 0)
5394 && operand_equal_p (op1b
, op2b
, 0))
5396 /* Result will be either NULL_TREE, or a combined comparison. */
5397 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
5398 TRUTH_ORIF_EXPR
, code1
, code2
,
5399 truth_type
, op1a
, op1b
);
5404 /* Likewise the swapped case of the above. */
5405 if (operand_equal_p (op1a
, op2b
, 0)
5406 && operand_equal_p (op1b
, op2a
, 0))
5408 /* Result will be either NULL_TREE, or a combined comparison. */
5409 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
5410 TRUTH_ORIF_EXPR
, code1
,
5411 swap_tree_comparison (code2
),
5412 truth_type
, op1a
, op1b
);
5417 /* If both comparisons are of the same value against constants, we might
5418 be able to merge them. */
5419 if (operand_equal_p (op1a
, op2a
, 0)
5420 && TREE_CODE (op1b
) == INTEGER_CST
5421 && TREE_CODE (op2b
) == INTEGER_CST
)
5423 int cmp
= tree_int_cst_compare (op1b
, op2b
);
5425 /* If we have (op1a != op1b), we should either be able to
5426 return that or TRUE, depending on whether the constant op1b
5427 also satisfies the other comparison against op2b. */
5428 if (code1
== NE_EXPR
)
5434 case EQ_EXPR
: val
= (cmp
== 0); break;
5435 case NE_EXPR
: val
= (cmp
!= 0); break;
5436 case LT_EXPR
: val
= (cmp
< 0); break;
5437 case GT_EXPR
: val
= (cmp
> 0); break;
5438 case LE_EXPR
: val
= (cmp
<= 0); break;
5439 case GE_EXPR
: val
= (cmp
>= 0); break;
5440 default: done
= false;
5445 return boolean_true_node
;
5447 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5450 /* Likewise if the second comparison is a != comparison. */
5451 else if (code2
== NE_EXPR
)
5457 case EQ_EXPR
: val
= (cmp
== 0); break;
5458 case NE_EXPR
: val
= (cmp
!= 0); break;
5459 case LT_EXPR
: val
= (cmp
> 0); break;
5460 case GT_EXPR
: val
= (cmp
< 0); break;
5461 case LE_EXPR
: val
= (cmp
>= 0); break;
5462 case GE_EXPR
: val
= (cmp
<= 0); break;
5463 default: done
= false;
5468 return boolean_true_node
;
5470 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5474 /* See if an equality test is redundant with the other comparison. */
5475 else if (code1
== EQ_EXPR
)
5480 case EQ_EXPR
: val
= (cmp
== 0); break;
5481 case NE_EXPR
: val
= (cmp
!= 0); break;
5482 case LT_EXPR
: val
= (cmp
< 0); break;
5483 case GT_EXPR
: val
= (cmp
> 0); break;
5484 case LE_EXPR
: val
= (cmp
<= 0); break;
5485 case GE_EXPR
: val
= (cmp
>= 0); break;
5490 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5492 else if (code2
== EQ_EXPR
)
5497 case EQ_EXPR
: val
= (cmp
== 0); break;
5498 case NE_EXPR
: val
= (cmp
!= 0); break;
5499 case LT_EXPR
: val
= (cmp
> 0); break;
5500 case GT_EXPR
: val
= (cmp
< 0); break;
5501 case LE_EXPR
: val
= (cmp
>= 0); break;
5502 case GE_EXPR
: val
= (cmp
<= 0); break;
5507 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5510 /* Chose the less restrictive of two < or <= comparisons. */
5511 else if ((code1
== LT_EXPR
|| code1
== LE_EXPR
)
5512 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
5514 if ((cmp
< 0) || (cmp
== 0 && code1
== LT_EXPR
))
5515 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5517 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5520 /* Likewise chose the less restrictive of two > or >= comparisons. */
5521 else if ((code1
== GT_EXPR
|| code1
== GE_EXPR
)
5522 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
5524 if ((cmp
> 0) || (cmp
== 0 && code1
== GT_EXPR
))
5525 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5527 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5530 /* Check for singleton ranges. */
5532 && ((code1
== LT_EXPR
&& code2
== GT_EXPR
)
5533 || (code1
== GT_EXPR
&& code2
== LT_EXPR
)))
5534 return fold_build2 (NE_EXPR
, boolean_type_node
, op1a
, op2b
);
5536 /* Check for less/greater pairs that don't restrict the range at all. */
5538 && (code1
== LT_EXPR
|| code1
== LE_EXPR
)
5539 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
5540 return boolean_true_node
;
5542 && (code1
== GT_EXPR
|| code1
== GE_EXPR
)
5543 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
5544 return boolean_true_node
;
5547 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
5548 NAME's definition is a truth value. See if there are any simplifications
5549 that can be done against the NAME's definition. */
5550 if (TREE_CODE (op1a
) == SSA_NAME
5551 && (code1
== NE_EXPR
|| code1
== EQ_EXPR
)
5552 && (integer_zerop (op1b
) || integer_onep (op1b
)))
5554 bool invert
= ((code1
== EQ_EXPR
&& integer_zerop (op1b
))
5555 || (code1
== NE_EXPR
&& integer_onep (op1b
)));
5556 gimple
*stmt
= SSA_NAME_DEF_STMT (op1a
);
5557 switch (gimple_code (stmt
))
5560 /* Try to simplify by copy-propagating the definition. */
5561 return or_var_with_comparison (op1a
, invert
, code2
, op2a
, op2b
);
5564 /* If every argument to the PHI produces the same result when
5565 ORed with the second comparison, we win.
5566 Do not do this unless the type is bool since we need a bool
5567 result here anyway. */
5568 if (TREE_CODE (TREE_TYPE (op1a
)) == BOOLEAN_TYPE
)
5570 tree result
= NULL_TREE
;
5572 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
5574 tree arg
= gimple_phi_arg_def (stmt
, i
);
5576 /* If this PHI has itself as an argument, ignore it.
5577 If all the other args produce the same result,
5579 if (arg
== gimple_phi_result (stmt
))
5581 else if (TREE_CODE (arg
) == INTEGER_CST
)
5583 if (invert
? integer_zerop (arg
) : integer_nonzerop (arg
))
5586 result
= boolean_true_node
;
5587 else if (!integer_onep (result
))
5591 result
= fold_build2 (code2
, boolean_type_node
,
5593 else if (!same_bool_comparison_p (result
,
5597 else if (TREE_CODE (arg
) == SSA_NAME
5598 && !SSA_NAME_IS_DEFAULT_DEF (arg
))
5601 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
5602 /* In simple cases we can look through PHI nodes,
5603 but we have to be careful with loops.
5605 if (! dom_info_available_p (CDI_DOMINATORS
)
5606 || gimple_bb (def_stmt
) == gimple_bb (stmt
)
5607 || dominated_by_p (CDI_DOMINATORS
,
5608 gimple_bb (def_stmt
),
5611 temp
= or_var_with_comparison (arg
, invert
, code2
,
5617 else if (!same_bool_result_p (result
, temp
))
5633 /* Try to simplify the OR of two comparisons, specified by
5634 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
5635 If this can be simplified to a single expression (without requiring
5636 introducing more SSA variables to hold intermediate values),
5637 return the resulting tree. Otherwise return NULL_TREE.
5638 If the result expression is non-null, it has boolean type. */
5641 maybe_fold_or_comparisons (enum tree_code code1
, tree op1a
, tree op1b
,
5642 enum tree_code code2
, tree op2a
, tree op2b
)
5644 tree t
= or_comparisons_1 (code1
, op1a
, op1b
, code2
, op2a
, op2b
);
5648 return or_comparisons_1 (code2
, op2a
, op2b
, code1
, op1a
, op1b
);
5652 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
5654 Either NULL_TREE, a simplified but non-constant or a constant
5657 ??? This should go into a gimple-fold-inline.h file to be eventually
5658 privatized with the single valueize function used in the various TUs
5659 to avoid the indirect function call overhead. */
5662 gimple_fold_stmt_to_constant_1 (gimple
*stmt
, tree (*valueize
) (tree
),
5663 tree (*gvalueize
) (tree
))
5667 /* ??? The SSA propagators do not correctly deal with following SSA use-def
5668 edges if there are intermediate VARYING defs. For this reason
5669 do not follow SSA edges here even though SCCVN can technically
5670 just deal fine with that. */
5671 if (gimple_simplify (stmt
, &rcode
, ops
, NULL
, gvalueize
, valueize
))
5673 tree res
= NULL_TREE
;
5674 if (gimple_simplified_result_is_gimple_val (rcode
, ops
))
5676 else if (mprts_hook
)
5677 res
= mprts_hook (rcode
, gimple_expr_type (stmt
), ops
);
5680 if (dump_file
&& dump_flags
& TDF_DETAILS
)
5682 fprintf (dump_file
, "Match-and-simplified ");
5683 print_gimple_expr (dump_file
, stmt
, 0, TDF_SLIM
);
5684 fprintf (dump_file
, " to ");
5685 print_generic_expr (dump_file
, res
, 0);
5686 fprintf (dump_file
, "\n");
5692 location_t loc
= gimple_location (stmt
);
5693 switch (gimple_code (stmt
))
5697 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
5699 switch (get_gimple_rhs_class (subcode
))
5701 case GIMPLE_SINGLE_RHS
:
5703 tree rhs
= gimple_assign_rhs1 (stmt
);
5704 enum tree_code_class kind
= TREE_CODE_CLASS (subcode
);
5706 if (TREE_CODE (rhs
) == SSA_NAME
)
5708 /* If the RHS is an SSA_NAME, return its known constant value,
5710 return (*valueize
) (rhs
);
5712 /* Handle propagating invariant addresses into address
5714 else if (TREE_CODE (rhs
) == ADDR_EXPR
5715 && !is_gimple_min_invariant (rhs
))
5717 HOST_WIDE_INT offset
= 0;
5719 base
= get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs
, 0),
5723 && (CONSTANT_CLASS_P (base
)
5724 || decl_address_invariant_p (base
)))
5725 return build_invariant_address (TREE_TYPE (rhs
),
5728 else if (TREE_CODE (rhs
) == CONSTRUCTOR
5729 && TREE_CODE (TREE_TYPE (rhs
)) == VECTOR_TYPE
5730 && (CONSTRUCTOR_NELTS (rhs
)
5731 == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs
))))
5736 vec
= XALLOCAVEC (tree
,
5737 TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs
)));
5738 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs
), i
, val
)
5740 val
= (*valueize
) (val
);
5741 if (TREE_CODE (val
) == INTEGER_CST
5742 || TREE_CODE (val
) == REAL_CST
5743 || TREE_CODE (val
) == FIXED_CST
)
5749 return build_vector (TREE_TYPE (rhs
), vec
);
5751 if (subcode
== OBJ_TYPE_REF
)
5753 tree val
= (*valueize
) (OBJ_TYPE_REF_EXPR (rhs
));
5754 /* If callee is constant, we can fold away the wrapper. */
5755 if (is_gimple_min_invariant (val
))
5759 if (kind
== tcc_reference
)
5761 if ((TREE_CODE (rhs
) == VIEW_CONVERT_EXPR
5762 || TREE_CODE (rhs
) == REALPART_EXPR
5763 || TREE_CODE (rhs
) == IMAGPART_EXPR
)
5764 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
5766 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
5767 return fold_unary_loc (EXPR_LOCATION (rhs
),
5769 TREE_TYPE (rhs
), val
);
5771 else if (TREE_CODE (rhs
) == BIT_FIELD_REF
5772 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
5774 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
5775 return fold_ternary_loc (EXPR_LOCATION (rhs
),
5777 TREE_TYPE (rhs
), val
,
5778 TREE_OPERAND (rhs
, 1),
5779 TREE_OPERAND (rhs
, 2));
5781 else if (TREE_CODE (rhs
) == MEM_REF
5782 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
5784 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
5785 if (TREE_CODE (val
) == ADDR_EXPR
5786 && is_gimple_min_invariant (val
))
5788 tree tem
= fold_build2 (MEM_REF
, TREE_TYPE (rhs
),
5790 TREE_OPERAND (rhs
, 1));
5795 return fold_const_aggregate_ref_1 (rhs
, valueize
);
5797 else if (kind
== tcc_declaration
)
5798 return get_symbol_constant_value (rhs
);
5802 case GIMPLE_UNARY_RHS
:
5805 case GIMPLE_BINARY_RHS
:
5806 /* Translate &x + CST into an invariant form suitable for
5807 further propagation. */
5808 if (subcode
== POINTER_PLUS_EXPR
)
5810 tree op0
= (*valueize
) (gimple_assign_rhs1 (stmt
));
5811 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
5812 if (TREE_CODE (op0
) == ADDR_EXPR
5813 && TREE_CODE (op1
) == INTEGER_CST
)
5815 tree off
= fold_convert (ptr_type_node
, op1
);
5816 return build_fold_addr_expr_loc
5818 fold_build2 (MEM_REF
,
5819 TREE_TYPE (TREE_TYPE (op0
)),
5820 unshare_expr (op0
), off
));
5823 /* Canonicalize bool != 0 and bool == 0 appearing after
5824 valueization. While gimple_simplify handles this
5825 it can get confused by the ~X == 1 -> X == 0 transform
5826 which we cant reduce to a SSA name or a constant
5827 (and we have no way to tell gimple_simplify to not
5828 consider those transforms in the first place). */
5829 else if (subcode
== EQ_EXPR
5830 || subcode
== NE_EXPR
)
5832 tree lhs
= gimple_assign_lhs (stmt
);
5833 tree op0
= gimple_assign_rhs1 (stmt
);
5834 if (useless_type_conversion_p (TREE_TYPE (lhs
),
5837 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
5838 op0
= (*valueize
) (op0
);
5839 if (TREE_CODE (op0
) == INTEGER_CST
)
5840 std::swap (op0
, op1
);
5841 if (TREE_CODE (op1
) == INTEGER_CST
5842 && ((subcode
== NE_EXPR
&& integer_zerop (op1
))
5843 || (subcode
== EQ_EXPR
&& integer_onep (op1
))))
5849 case GIMPLE_TERNARY_RHS
:
5851 /* Handle ternary operators that can appear in GIMPLE form. */
5852 tree op0
= (*valueize
) (gimple_assign_rhs1 (stmt
));
5853 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
5854 tree op2
= (*valueize
) (gimple_assign_rhs3 (stmt
));
5855 return fold_ternary_loc (loc
, subcode
,
5856 gimple_expr_type (stmt
), op0
, op1
, op2
);
5867 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
5869 if (gimple_call_internal_p (stmt
))
5871 enum tree_code subcode
= ERROR_MARK
;
5872 switch (gimple_call_internal_fn (stmt
))
5874 case IFN_UBSAN_CHECK_ADD
:
5875 subcode
= PLUS_EXPR
;
5877 case IFN_UBSAN_CHECK_SUB
:
5878 subcode
= MINUS_EXPR
;
5880 case IFN_UBSAN_CHECK_MUL
:
5881 subcode
= MULT_EXPR
;
5883 case IFN_BUILTIN_EXPECT
:
5885 tree arg0
= gimple_call_arg (stmt
, 0);
5886 tree op0
= (*valueize
) (arg0
);
5887 if (TREE_CODE (op0
) == INTEGER_CST
)
5894 tree arg0
= gimple_call_arg (stmt
, 0);
5895 tree arg1
= gimple_call_arg (stmt
, 1);
5896 tree op0
= (*valueize
) (arg0
);
5897 tree op1
= (*valueize
) (arg1
);
5899 if (TREE_CODE (op0
) != INTEGER_CST
5900 || TREE_CODE (op1
) != INTEGER_CST
)
5905 /* x * 0 = 0 * x = 0 without overflow. */
5906 if (integer_zerop (op0
) || integer_zerop (op1
))
5907 return build_zero_cst (TREE_TYPE (arg0
));
5910 /* y - y = 0 without overflow. */
5911 if (operand_equal_p (op0
, op1
, 0))
5912 return build_zero_cst (TREE_TYPE (arg0
));
5919 = fold_binary_loc (loc
, subcode
, TREE_TYPE (arg0
), op0
, op1
);
5921 && TREE_CODE (res
) == INTEGER_CST
5922 && !TREE_OVERFLOW (res
))
5927 fn
= (*valueize
) (gimple_call_fn (stmt
));
5928 if (TREE_CODE (fn
) == ADDR_EXPR
5929 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
5930 && DECL_BUILT_IN (TREE_OPERAND (fn
, 0))
5931 && gimple_builtin_call_types_compatible_p (stmt
,
5932 TREE_OPERAND (fn
, 0)))
5934 tree
*args
= XALLOCAVEC (tree
, gimple_call_num_args (stmt
));
5937 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
5938 args
[i
] = (*valueize
) (gimple_call_arg (stmt
, i
));
5939 retval
= fold_builtin_call_array (loc
,
5940 gimple_call_return_type (call_stmt
),
5941 fn
, gimple_call_num_args (stmt
), args
);
5944 /* fold_call_expr wraps the result inside a NOP_EXPR. */
5945 STRIP_NOPS (retval
);
5946 retval
= fold_convert (gimple_call_return_type (call_stmt
),
5959 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
5960 Returns NULL_TREE if folding to a constant is not possible, otherwise
5961 returns a constant according to is_gimple_min_invariant. */
5964 gimple_fold_stmt_to_constant (gimple
*stmt
, tree (*valueize
) (tree
))
5966 tree res
= gimple_fold_stmt_to_constant_1 (stmt
, valueize
);
5967 if (res
&& is_gimple_min_invariant (res
))
5973 /* The following set of functions are supposed to fold references using
5974 their constant initializers. */
5976 /* See if we can find constructor defining value of BASE.
5977 When we know the consructor with constant offset (such as
5978 base is array[40] and we do know constructor of array), then
5979 BIT_OFFSET is adjusted accordingly.
5981 As a special case, return error_mark_node when constructor
5982 is not explicitly available, but it is known to be zero
5983 such as 'static const int a;'. */
5985 get_base_constructor (tree base
, HOST_WIDE_INT
*bit_offset
,
5986 tree (*valueize
)(tree
))
5988 HOST_WIDE_INT bit_offset2
, size
, max_size
;
5991 if (TREE_CODE (base
) == MEM_REF
)
5993 if (!integer_zerop (TREE_OPERAND (base
, 1)))
5995 if (!tree_fits_shwi_p (TREE_OPERAND (base
, 1)))
5997 *bit_offset
+= (mem_ref_offset (base
).to_short_addr ()
6002 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
6003 base
= valueize (TREE_OPERAND (base
, 0));
6004 if (!base
|| TREE_CODE (base
) != ADDR_EXPR
)
6006 base
= TREE_OPERAND (base
, 0);
6009 && TREE_CODE (base
) == SSA_NAME
)
6010 base
= valueize (base
);
6012 /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
6013 DECL_INITIAL. If BASE is a nested reference into another
6014 ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
6015 the inner reference. */
6016 switch (TREE_CODE (base
))
6021 tree init
= ctor_for_folding (base
);
6023 /* Our semantic is exact opposite of ctor_for_folding;
6024 NULL means unknown, while error_mark_node is 0. */
6025 if (init
== error_mark_node
)
6028 return error_mark_node
;
6032 case VIEW_CONVERT_EXPR
:
6033 return get_base_constructor (TREE_OPERAND (base
, 0),
6034 bit_offset
, valueize
);
6038 base
= get_ref_base_and_extent (base
, &bit_offset2
, &size
, &max_size
,
6040 if (max_size
== -1 || size
!= max_size
)
6042 *bit_offset
+= bit_offset2
;
6043 return get_base_constructor (base
, bit_offset
, valueize
);
6049 if (CONSTANT_CLASS_P (base
))
6056 /* CTOR is CONSTRUCTOR of an array type. Fold reference of type TYPE and size
6057 SIZE to the memory at bit OFFSET. */
6060 fold_array_ctor_reference (tree type
, tree ctor
,
6061 unsigned HOST_WIDE_INT offset
,
6062 unsigned HOST_WIDE_INT size
,
6065 offset_int low_bound
;
6066 offset_int elt_size
;
6067 offset_int access_index
;
6068 tree domain_type
= NULL_TREE
;
6069 HOST_WIDE_INT inner_offset
;
6071 /* Compute low bound and elt size. */
6072 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
)
6073 domain_type
= TYPE_DOMAIN (TREE_TYPE (ctor
));
6074 if (domain_type
&& TYPE_MIN_VALUE (domain_type
))
6076 /* Static constructors for variably sized objects makes no sense. */
6077 if (TREE_CODE (TYPE_MIN_VALUE (domain_type
)) != INTEGER_CST
)
6079 low_bound
= wi::to_offset (TYPE_MIN_VALUE (domain_type
));
6083 /* Static constructors for variably sized objects makes no sense. */
6084 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor
)))) != INTEGER_CST
)
6086 elt_size
= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor
))));
6088 /* We can handle only constantly sized accesses that are known to not
6089 be larger than size of array element. */
6090 if (!TYPE_SIZE_UNIT (type
)
6091 || TREE_CODE (TYPE_SIZE_UNIT (type
)) != INTEGER_CST
6092 || elt_size
< wi::to_offset (TYPE_SIZE_UNIT (type
))
6096 /* Compute the array index we look for. */
6097 access_index
= wi::udiv_trunc (offset_int (offset
/ BITS_PER_UNIT
),
6099 access_index
+= low_bound
;
6101 /* And offset within the access. */
6102 inner_offset
= offset
% (elt_size
.to_uhwi () * BITS_PER_UNIT
);
6104 /* See if the array field is large enough to span whole access. We do not
6105 care to fold accesses spanning multiple array indexes. */
6106 if (inner_offset
+ size
> elt_size
.to_uhwi () * BITS_PER_UNIT
)
6108 if (tree val
= get_array_ctor_element_at_index (ctor
, access_index
))
6109 return fold_ctor_reference (type
, val
, inner_offset
, size
, from_decl
);
6111 /* When memory is not explicitely mentioned in constructor,
6112 it is 0 (or out of range). */
6113 return build_zero_cst (type
);
6116 /* CTOR is CONSTRUCTOR of an aggregate or vector.
6117 Fold reference of type TYPE and size SIZE to the memory at bit OFFSET. */
6120 fold_nonarray_ctor_reference (tree type
, tree ctor
,
6121 unsigned HOST_WIDE_INT offset
,
6122 unsigned HOST_WIDE_INT size
,
6125 unsigned HOST_WIDE_INT cnt
;
6128 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
,
6131 tree byte_offset
= DECL_FIELD_OFFSET (cfield
);
6132 tree field_offset
= DECL_FIELD_BIT_OFFSET (cfield
);
6133 tree field_size
= DECL_SIZE (cfield
);
6134 offset_int bitoffset
;
6135 offset_int bitoffset_end
, access_end
;
6137 /* Variable sized objects in static constructors makes no sense,
6138 but field_size can be NULL for flexible array members. */
6139 gcc_assert (TREE_CODE (field_offset
) == INTEGER_CST
6140 && TREE_CODE (byte_offset
) == INTEGER_CST
6141 && (field_size
!= NULL_TREE
6142 ? TREE_CODE (field_size
) == INTEGER_CST
6143 : TREE_CODE (TREE_TYPE (cfield
)) == ARRAY_TYPE
));
6145 /* Compute bit offset of the field. */
6146 bitoffset
= (wi::to_offset (field_offset
)
6147 + (wi::to_offset (byte_offset
) << LOG2_BITS_PER_UNIT
));
6148 /* Compute bit offset where the field ends. */
6149 if (field_size
!= NULL_TREE
)
6150 bitoffset_end
= bitoffset
+ wi::to_offset (field_size
);
6154 access_end
= offset_int (offset
) + size
;
6156 /* Is there any overlap between [OFFSET, OFFSET+SIZE) and
6157 [BITOFFSET, BITOFFSET_END)? */
6158 if (wi::cmps (access_end
, bitoffset
) > 0
6159 && (field_size
== NULL_TREE
6160 || wi::lts_p (offset
, bitoffset_end
)))
6162 offset_int inner_offset
= offset_int (offset
) - bitoffset
;
6163 /* We do have overlap. Now see if field is large enough to
6164 cover the access. Give up for accesses spanning multiple
6166 if (wi::cmps (access_end
, bitoffset_end
) > 0)
6168 if (offset
< bitoffset
)
6170 return fold_ctor_reference (type
, cval
,
6171 inner_offset
.to_uhwi (), size
,
6175 /* When memory is not explicitely mentioned in constructor, it is 0. */
6176 return build_zero_cst (type
);
6179 /* CTOR is value initializing memory, fold reference of type TYPE and size SIZE
6180 to the memory at bit OFFSET. */
6183 fold_ctor_reference (tree type
, tree ctor
, unsigned HOST_WIDE_INT offset
,
6184 unsigned HOST_WIDE_INT size
, tree from_decl
)
6188 /* We found the field with exact match. */
6189 if (useless_type_conversion_p (type
, TREE_TYPE (ctor
))
6191 return canonicalize_constructor_val (unshare_expr (ctor
), from_decl
);
6193 /* We are at the end of walk, see if we can view convert the
6195 if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor
)) && !offset
6196 /* VIEW_CONVERT_EXPR is defined only for matching sizes. */
6197 && !compare_tree_int (TYPE_SIZE (type
), size
)
6198 && !compare_tree_int (TYPE_SIZE (TREE_TYPE (ctor
)), size
))
6200 ret
= canonicalize_constructor_val (unshare_expr (ctor
), from_decl
);
6201 ret
= fold_unary (VIEW_CONVERT_EXPR
, type
, ret
);
6203 STRIP_USELESS_TYPE_CONVERSION (ret
);
6206 /* For constants and byte-aligned/sized reads try to go through
6207 native_encode/interpret. */
6208 if (CONSTANT_CLASS_P (ctor
)
6209 && BITS_PER_UNIT
== 8
6210 && offset
% BITS_PER_UNIT
== 0
6211 && size
% BITS_PER_UNIT
== 0
6212 && size
<= MAX_BITSIZE_MODE_ANY_MODE
)
6214 unsigned char buf
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
6215 int len
= native_encode_expr (ctor
, buf
, size
/ BITS_PER_UNIT
,
6216 offset
/ BITS_PER_UNIT
);
6218 return native_interpret_expr (type
, buf
, len
);
6220 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
6223 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
6224 || TREE_CODE (TREE_TYPE (ctor
)) == VECTOR_TYPE
)
6225 return fold_array_ctor_reference (type
, ctor
, offset
, size
,
6228 return fold_nonarray_ctor_reference (type
, ctor
, offset
, size
,
6235 /* Return the tree representing the element referenced by T if T is an
6236 ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
6237 names using VALUEIZE. Return NULL_TREE otherwise. */
6240 fold_const_aggregate_ref_1 (tree t
, tree (*valueize
) (tree
))
6242 tree ctor
, idx
, base
;
6243 HOST_WIDE_INT offset
, size
, max_size
;
6247 if (TREE_THIS_VOLATILE (t
))
6251 return get_symbol_constant_value (t
);
6253 tem
= fold_read_from_constant_string (t
);
6257 switch (TREE_CODE (t
))
6260 case ARRAY_RANGE_REF
:
6261 /* Constant indexes are handled well by get_base_constructor.
6262 Only special case variable offsets.
6263 FIXME: This code can't handle nested references with variable indexes
6264 (they will be handled only by iteration of ccp). Perhaps we can bring
6265 get_ref_base_and_extent here and make it use a valueize callback. */
6266 if (TREE_CODE (TREE_OPERAND (t
, 1)) == SSA_NAME
6268 && (idx
= (*valueize
) (TREE_OPERAND (t
, 1)))
6269 && TREE_CODE (idx
) == INTEGER_CST
)
6271 tree low_bound
, unit_size
;
6273 /* If the resulting bit-offset is constant, track it. */
6274 if ((low_bound
= array_ref_low_bound (t
),
6275 TREE_CODE (low_bound
) == INTEGER_CST
)
6276 && (unit_size
= array_ref_element_size (t
),
6277 tree_fits_uhwi_p (unit_size
)))
6280 = wi::sext (wi::to_offset (idx
) - wi::to_offset (low_bound
),
6281 TYPE_PRECISION (TREE_TYPE (idx
)));
6283 if (wi::fits_shwi_p (woffset
))
6285 offset
= woffset
.to_shwi ();
6286 /* TODO: This code seems wrong, multiply then check
6287 to see if it fits. */
6288 offset
*= tree_to_uhwi (unit_size
);
6289 offset
*= BITS_PER_UNIT
;
6291 base
= TREE_OPERAND (t
, 0);
6292 ctor
= get_base_constructor (base
, &offset
, valueize
);
6293 /* Empty constructor. Always fold to 0. */
6294 if (ctor
== error_mark_node
)
6295 return build_zero_cst (TREE_TYPE (t
));
6296 /* Out of bound array access. Value is undefined,
6300 /* We can not determine ctor. */
6303 return fold_ctor_reference (TREE_TYPE (t
), ctor
, offset
,
6304 tree_to_uhwi (unit_size
)
6314 case TARGET_MEM_REF
:
6316 base
= get_ref_base_and_extent (t
, &offset
, &size
, &max_size
, &reverse
);
6317 ctor
= get_base_constructor (base
, &offset
, valueize
);
6319 /* Empty constructor. Always fold to 0. */
6320 if (ctor
== error_mark_node
)
6321 return build_zero_cst (TREE_TYPE (t
));
6322 /* We do not know precise address. */
6323 if (max_size
== -1 || max_size
!= size
)
6325 /* We can not determine ctor. */
6329 /* Out of bound array access. Value is undefined, but don't fold. */
6333 return fold_ctor_reference (TREE_TYPE (t
), ctor
, offset
, size
,
6339 tree c
= fold_const_aggregate_ref_1 (TREE_OPERAND (t
, 0), valueize
);
6340 if (c
&& TREE_CODE (c
) == COMPLEX_CST
)
6341 return fold_build1_loc (EXPR_LOCATION (t
),
6342 TREE_CODE (t
), TREE_TYPE (t
), c
);
6354 fold_const_aggregate_ref (tree t
)
6356 return fold_const_aggregate_ref_1 (t
, NULL
);
6359 /* Lookup virtual method with index TOKEN in a virtual table V
6361 Set CAN_REFER if non-NULL to false if method
6362 is not referable or if the virtual table is ill-formed (such as rewriten
6363 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
6366 gimple_get_virt_method_for_vtable (HOST_WIDE_INT token
,
6368 unsigned HOST_WIDE_INT offset
,
6371 tree vtable
= v
, init
, fn
;
6372 unsigned HOST_WIDE_INT size
;
6373 unsigned HOST_WIDE_INT elt_size
, access_index
;
6379 /* First of all double check we have virtual table. */
6380 if (!VAR_P (v
) || !DECL_VIRTUAL_P (v
))
6382 /* Pass down that we lost track of the target. */
6388 init
= ctor_for_folding (v
);
6390 /* The virtual tables should always be born with constructors
6391 and we always should assume that they are avaialble for
6392 folding. At the moment we do not stream them in all cases,
6393 but it should never happen that ctor seem unreachable. */
6395 if (init
== error_mark_node
)
6397 gcc_assert (in_lto_p
);
6398 /* Pass down that we lost track of the target. */
6403 gcc_checking_assert (TREE_CODE (TREE_TYPE (v
)) == ARRAY_TYPE
);
6404 size
= tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v
))));
6405 offset
*= BITS_PER_UNIT
;
6406 offset
+= token
* size
;
6408 /* Lookup the value in the constructor that is assumed to be array.
6409 This is equivalent to
6410 fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
6411 offset, size, NULL);
6412 but in a constant time. We expect that frontend produced a simple
6413 array without indexed initializers. */
6415 gcc_checking_assert (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
);
6416 domain_type
= TYPE_DOMAIN (TREE_TYPE (init
));
6417 gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type
)));
6418 elt_size
= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init
))));
6420 access_index
= offset
/ BITS_PER_UNIT
/ elt_size
;
6421 gcc_checking_assert (offset
% (elt_size
* BITS_PER_UNIT
) == 0);
6423 /* This code makes an assumption that there are no
6424 indexed fileds produced by C++ FE, so we can directly index the array. */
6425 if (access_index
< CONSTRUCTOR_NELTS (init
))
6427 fn
= CONSTRUCTOR_ELT (init
, access_index
)->value
;
6428 gcc_checking_assert (!CONSTRUCTOR_ELT (init
, access_index
)->index
);
6434 /* For type inconsistent program we may end up looking up virtual method
6435 in virtual table that does not contain TOKEN entries. We may overrun
6436 the virtual table and pick up a constant or RTTI info pointer.
6437 In any case the call is undefined. */
6439 || (TREE_CODE (fn
) != ADDR_EXPR
&& TREE_CODE (fn
) != FDESC_EXPR
)
6440 || TREE_CODE (TREE_OPERAND (fn
, 0)) != FUNCTION_DECL
)
6441 fn
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
6444 fn
= TREE_OPERAND (fn
, 0);
6446 /* When cgraph node is missing and function is not public, we cannot
6447 devirtualize. This can happen in WHOPR when the actual method
6448 ends up in other partition, because we found devirtualization
6449 possibility too late. */
6450 if (!can_refer_decl_in_current_unit_p (fn
, vtable
))
6461 /* Make sure we create a cgraph node for functions we'll reference.
6462 They can be non-existent if the reference comes from an entry
6463 of an external vtable for example. */
6464 cgraph_node::get_create (fn
);
6469 /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
6470 is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
6471 KNOWN_BINFO carries the binfo describing the true type of
6472 OBJ_TYPE_REF_OBJECT(REF).
6473 Set CAN_REFER if non-NULL to false if method
6474 is not referable or if the virtual table is ill-formed (such as rewriten
6475 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
6478 gimple_get_virt_method_for_binfo (HOST_WIDE_INT token
, tree known_binfo
,
6481 unsigned HOST_WIDE_INT offset
;
6484 v
= BINFO_VTABLE (known_binfo
);
6485 /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */
6489 if (!vtable_pointer_value_to_vtable (v
, &v
, &offset
))
6495 return gimple_get_virt_method_for_vtable (token
, v
, offset
, can_refer
);
6498 /* Given a pointer value OP0, return a simplified version of an
6499 indirection through OP0, or NULL_TREE if no simplification is
6500 possible. Note that the resulting type may be different from
6501 the type pointed to in the sense that it is still compatible
6502 from the langhooks point of view. */
6505 gimple_fold_indirect_ref (tree t
)
6507 tree ptype
= TREE_TYPE (t
), type
= TREE_TYPE (ptype
);
6512 subtype
= TREE_TYPE (sub
);
6513 if (!POINTER_TYPE_P (subtype
))
6516 if (TREE_CODE (sub
) == ADDR_EXPR
)
6518 tree op
= TREE_OPERAND (sub
, 0);
6519 tree optype
= TREE_TYPE (op
);
6521 if (useless_type_conversion_p (type
, optype
))
6524 /* *(foo *)&fooarray => fooarray[0] */
6525 if (TREE_CODE (optype
) == ARRAY_TYPE
6526 && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype
))) == INTEGER_CST
6527 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
6529 tree type_domain
= TYPE_DOMAIN (optype
);
6530 tree min_val
= size_zero_node
;
6531 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
6532 min_val
= TYPE_MIN_VALUE (type_domain
);
6533 if (TREE_CODE (min_val
) == INTEGER_CST
)
6534 return build4 (ARRAY_REF
, type
, op
, min_val
, NULL_TREE
, NULL_TREE
);
6536 /* *(foo *)&complexfoo => __real__ complexfoo */
6537 else if (TREE_CODE (optype
) == COMPLEX_TYPE
6538 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
6539 return fold_build1 (REALPART_EXPR
, type
, op
);
6540 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
6541 else if (TREE_CODE (optype
) == VECTOR_TYPE
6542 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
6544 tree part_width
= TYPE_SIZE (type
);
6545 tree index
= bitsize_int (0);
6546 return fold_build3 (BIT_FIELD_REF
, type
, op
, part_width
, index
);
6550 /* *(p + CST) -> ... */
6551 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
6552 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
6554 tree addr
= TREE_OPERAND (sub
, 0);
6555 tree off
= TREE_OPERAND (sub
, 1);
6559 addrtype
= TREE_TYPE (addr
);
6561 /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
6562 if (TREE_CODE (addr
) == ADDR_EXPR
6563 && TREE_CODE (TREE_TYPE (addrtype
)) == VECTOR_TYPE
6564 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (addrtype
)))
6565 && tree_fits_uhwi_p (off
))
6567 unsigned HOST_WIDE_INT offset
= tree_to_uhwi (off
);
6568 tree part_width
= TYPE_SIZE (type
);
6569 unsigned HOST_WIDE_INT part_widthi
6570 = tree_to_shwi (part_width
) / BITS_PER_UNIT
;
6571 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
6572 tree index
= bitsize_int (indexi
);
6573 if (offset
/ part_widthi
6574 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype
)))
6575 return fold_build3 (BIT_FIELD_REF
, type
, TREE_OPERAND (addr
, 0),
6579 /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
6580 if (TREE_CODE (addr
) == ADDR_EXPR
6581 && TREE_CODE (TREE_TYPE (addrtype
)) == COMPLEX_TYPE
6582 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (addrtype
))))
6584 tree size
= TYPE_SIZE_UNIT (type
);
6585 if (tree_int_cst_equal (size
, off
))
6586 return fold_build1 (IMAGPART_EXPR
, type
, TREE_OPERAND (addr
, 0));
6589 /* *(p + CST) -> MEM_REF <p, CST>. */
6590 if (TREE_CODE (addr
) != ADDR_EXPR
6591 || DECL_P (TREE_OPERAND (addr
, 0)))
6592 return fold_build2 (MEM_REF
, type
,
6594 wide_int_to_tree (ptype
, off
));
6597 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
6598 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
6599 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype
)))) == INTEGER_CST
6600 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (subtype
))))
6603 tree min_val
= size_zero_node
;
6605 sub
= gimple_fold_indirect_ref (sub
);
6607 sub
= build1 (INDIRECT_REF
, TREE_TYPE (subtype
), osub
);
6608 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
6609 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
6610 min_val
= TYPE_MIN_VALUE (type_domain
);
6611 if (TREE_CODE (min_val
) == INTEGER_CST
)
6612 return build4 (ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
, NULL_TREE
);
6618 /* Return true if CODE is an operation that when operating on signed
6619 integer types involves undefined behavior on overflow and the
6620 operation can be expressed with unsigned arithmetic. */
6623 arith_code_with_undefined_signed_overflow (tree_code code
)
6631 case POINTER_PLUS_EXPR
:
6638 /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic
6639 operation that can be transformed to unsigned arithmetic by converting
6640 its operand, carrying out the operation in the corresponding unsigned
6641 type and converting the result back to the original type.
6643 Returns a sequence of statements that replace STMT and also contain
6644 a modified form of STMT itself. */
6647 rewrite_to_defined_overflow (gimple
*stmt
)
6649 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6651 fprintf (dump_file
, "rewriting stmt with undefined signed "
6653 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
6656 tree lhs
= gimple_assign_lhs (stmt
);
6657 tree type
= unsigned_type_for (TREE_TYPE (lhs
));
6658 gimple_seq stmts
= NULL
;
6659 for (unsigned i
= 1; i
< gimple_num_ops (stmt
); ++i
)
6661 tree op
= gimple_op (stmt
, i
);
6662 op
= gimple_convert (&stmts
, type
, op
);
6663 gimple_set_op (stmt
, i
, op
);
6665 gimple_assign_set_lhs (stmt
, make_ssa_name (type
, stmt
));
6666 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
6667 gimple_assign_set_rhs_code (stmt
, PLUS_EXPR
);
6668 gimple_seq_add_stmt (&stmts
, stmt
);
6669 gimple
*cvt
= gimple_build_assign (lhs
, NOP_EXPR
, gimple_assign_lhs (stmt
));
6670 gimple_seq_add_stmt (&stmts
, cvt
);
6676 /* The valueization hook we use for the gimple_build API simplification.
6677 This makes us match fold_buildN behavior by only combining with
6678 statements in the sequence(s) we are currently building. */
6681 gimple_build_valueize (tree op
)
6683 if (gimple_bb (SSA_NAME_DEF_STMT (op
)) == NULL
)
6688 /* Build the expression CODE OP0 of type TYPE with location LOC,
6689 simplifying it first if possible. Returns the built
6690 expression value and appends statements possibly defining it
6694 gimple_build (gimple_seq
*seq
, location_t loc
,
6695 enum tree_code code
, tree type
, tree op0
)
6697 tree res
= gimple_simplify (code
, type
, op0
, seq
, gimple_build_valueize
);
6700 res
= create_tmp_reg_or_ssa_name (type
);
6702 if (code
== REALPART_EXPR
6703 || code
== IMAGPART_EXPR
6704 || code
== VIEW_CONVERT_EXPR
)
6705 stmt
= gimple_build_assign (res
, code
, build1 (code
, type
, op0
));
6707 stmt
= gimple_build_assign (res
, code
, op0
);
6708 gimple_set_location (stmt
, loc
);
6709 gimple_seq_add_stmt_without_update (seq
, stmt
);
6714 /* Build the expression OP0 CODE OP1 of type TYPE with location LOC,
6715 simplifying it first if possible. Returns the built
6716 expression value and appends statements possibly defining it
6720 gimple_build (gimple_seq
*seq
, location_t loc
,
6721 enum tree_code code
, tree type
, tree op0
, tree op1
)
6723 tree res
= gimple_simplify (code
, type
, op0
, op1
, seq
, gimple_build_valueize
);
6726 res
= create_tmp_reg_or_ssa_name (type
);
6727 gimple
*stmt
= gimple_build_assign (res
, code
, op0
, op1
);
6728 gimple_set_location (stmt
, loc
);
6729 gimple_seq_add_stmt_without_update (seq
, stmt
);
6734 /* Build the expression (CODE OP0 OP1 OP2) of type TYPE with location LOC,
6735 simplifying it first if possible. Returns the built
6736 expression value and appends statements possibly defining it
6740 gimple_build (gimple_seq
*seq
, location_t loc
,
6741 enum tree_code code
, tree type
, tree op0
, tree op1
, tree op2
)
6743 tree res
= gimple_simplify (code
, type
, op0
, op1
, op2
,
6744 seq
, gimple_build_valueize
);
6747 res
= create_tmp_reg_or_ssa_name (type
);
6749 if (code
== BIT_FIELD_REF
)
6750 stmt
= gimple_build_assign (res
, code
,
6751 build3 (code
, type
, op0
, op1
, op2
));
6753 stmt
= gimple_build_assign (res
, code
, op0
, op1
, op2
);
6754 gimple_set_location (stmt
, loc
);
6755 gimple_seq_add_stmt_without_update (seq
, stmt
);
6760 /* Build the call FN (ARG0) with a result of type TYPE
6761 (or no result if TYPE is void) with location LOC,
6762 simplifying it first if possible. Returns the built
6763 expression value (or NULL_TREE if TYPE is void) and appends
6764 statements possibly defining it to SEQ. */
6767 gimple_build (gimple_seq
*seq
, location_t loc
,
6768 enum built_in_function fn
, tree type
, tree arg0
)
6770 tree res
= gimple_simplify (fn
, type
, arg0
, seq
, gimple_build_valueize
);
6773 tree decl
= builtin_decl_implicit (fn
);
6774 gimple
*stmt
= gimple_build_call (decl
, 1, arg0
);
6775 if (!VOID_TYPE_P (type
))
6777 res
= create_tmp_reg_or_ssa_name (type
);
6778 gimple_call_set_lhs (stmt
, res
);
6780 gimple_set_location (stmt
, loc
);
6781 gimple_seq_add_stmt_without_update (seq
, stmt
);
6786 /* Build the call FN (ARG0, ARG1) with a result of type TYPE
6787 (or no result if TYPE is void) with location LOC,
6788 simplifying it first if possible. Returns the built
6789 expression value (or NULL_TREE if TYPE is void) and appends
6790 statements possibly defining it to SEQ. */
6793 gimple_build (gimple_seq
*seq
, location_t loc
,
6794 enum built_in_function fn
, tree type
, tree arg0
, tree arg1
)
6796 tree res
= gimple_simplify (fn
, type
, arg0
, arg1
, seq
, gimple_build_valueize
);
6799 tree decl
= builtin_decl_implicit (fn
);
6800 gimple
*stmt
= gimple_build_call (decl
, 2, arg0
, arg1
);
6801 if (!VOID_TYPE_P (type
))
6803 res
= create_tmp_reg_or_ssa_name (type
);
6804 gimple_call_set_lhs (stmt
, res
);
6806 gimple_set_location (stmt
, loc
);
6807 gimple_seq_add_stmt_without_update (seq
, stmt
);
6812 /* Build the call FN (ARG0, ARG1, ARG2) with a result of type TYPE
6813 (or no result if TYPE is void) with location LOC,
6814 simplifying it first if possible. Returns the built
6815 expression value (or NULL_TREE if TYPE is void) and appends
6816 statements possibly defining it to SEQ. */
6819 gimple_build (gimple_seq
*seq
, location_t loc
,
6820 enum built_in_function fn
, tree type
,
6821 tree arg0
, tree arg1
, tree arg2
)
6823 tree res
= gimple_simplify (fn
, type
, arg0
, arg1
, arg2
,
6824 seq
, gimple_build_valueize
);
6827 tree decl
= builtin_decl_implicit (fn
);
6828 gimple
*stmt
= gimple_build_call (decl
, 3, arg0
, arg1
, arg2
);
6829 if (!VOID_TYPE_P (type
))
6831 res
= create_tmp_reg_or_ssa_name (type
);
6832 gimple_call_set_lhs (stmt
, res
);
6834 gimple_set_location (stmt
, loc
);
6835 gimple_seq_add_stmt_without_update (seq
, stmt
);
6840 /* Build the conversion (TYPE) OP with a result of type TYPE
6841 with location LOC if such conversion is neccesary in GIMPLE,
6842 simplifying it first.
6843 Returns the built expression value and appends
6844 statements possibly defining it to SEQ. */
6847 gimple_convert (gimple_seq
*seq
, location_t loc
, tree type
, tree op
)
6849 if (useless_type_conversion_p (type
, TREE_TYPE (op
)))
6851 return gimple_build (seq
, loc
, NOP_EXPR
, type
, op
);
6854 /* Build the conversion (ptrofftype) OP with a result of a type
6855 compatible with ptrofftype with location LOC if such conversion
6856 is neccesary in GIMPLE, simplifying it first.
6857 Returns the built expression value and appends
6858 statements possibly defining it to SEQ. */
6861 gimple_convert_to_ptrofftype (gimple_seq
*seq
, location_t loc
, tree op
)
6863 if (ptrofftype_p (TREE_TYPE (op
)))
6865 return gimple_convert (seq
, loc
, sizetype
, op
);
6868 /* Return true if the result of assignment STMT is known to be non-negative.
6869 If the return value is based on the assumption that signed overflow is
6870 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
6871 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
6874 gimple_assign_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
6877 enum tree_code code
= gimple_assign_rhs_code (stmt
);
6878 switch (get_gimple_rhs_class (code
))
6880 case GIMPLE_UNARY_RHS
:
6881 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
6882 gimple_expr_type (stmt
),
6883 gimple_assign_rhs1 (stmt
),
6884 strict_overflow_p
, depth
);
6885 case GIMPLE_BINARY_RHS
:
6886 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
6887 gimple_expr_type (stmt
),
6888 gimple_assign_rhs1 (stmt
),
6889 gimple_assign_rhs2 (stmt
),
6890 strict_overflow_p
, depth
);
6891 case GIMPLE_TERNARY_RHS
:
6893 case GIMPLE_SINGLE_RHS
:
6894 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
6895 strict_overflow_p
, depth
);
6896 case GIMPLE_INVALID_RHS
:
6902 /* Return true if return value of call STMT is known to be non-negative.
6903 If the return value is based on the assumption that signed overflow is
6904 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
6905 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
6908 gimple_call_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
6911 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
6912 gimple_call_arg (stmt
, 0) : NULL_TREE
;
6913 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
6914 gimple_call_arg (stmt
, 1) : NULL_TREE
;
6916 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
6917 gimple_call_combined_fn (stmt
),
6920 strict_overflow_p
, depth
);
6923 /* Return true if return value of call STMT is known to be non-negative.
6924 If the return value is based on the assumption that signed overflow is
6925 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
6926 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
6929 gimple_phi_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
6932 for (unsigned i
= 0; i
< gimple_phi_num_args (stmt
); ++i
)
6934 tree arg
= gimple_phi_arg_def (stmt
, i
);
6935 if (!tree_single_nonnegative_warnv_p (arg
, strict_overflow_p
, depth
+ 1))
6941 /* Return true if STMT is known to compute a non-negative value.
6942 If the return value is based on the assumption that signed overflow is
6943 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
6944 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
6947 gimple_stmt_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
6950 switch (gimple_code (stmt
))
6953 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
,
6956 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
,
6959 return gimple_phi_nonnegative_warnv_p (stmt
, strict_overflow_p
,
6966 /* Return true if the floating-point value computed by assignment STMT
6967 is known to have an integer value. We also allow +Inf, -Inf and NaN
6968 to be considered integer values. Return false for signaling NaN.
6970 DEPTH is the current nesting depth of the query. */
6973 gimple_assign_integer_valued_real_p (gimple
*stmt
, int depth
)
6975 enum tree_code code
= gimple_assign_rhs_code (stmt
);
6976 switch (get_gimple_rhs_class (code
))
6978 case GIMPLE_UNARY_RHS
:
6979 return integer_valued_real_unary_p (gimple_assign_rhs_code (stmt
),
6980 gimple_assign_rhs1 (stmt
), depth
);
6981 case GIMPLE_BINARY_RHS
:
6982 return integer_valued_real_binary_p (gimple_assign_rhs_code (stmt
),
6983 gimple_assign_rhs1 (stmt
),
6984 gimple_assign_rhs2 (stmt
), depth
);
6985 case GIMPLE_TERNARY_RHS
:
6987 case GIMPLE_SINGLE_RHS
:
6988 return integer_valued_real_single_p (gimple_assign_rhs1 (stmt
), depth
);
6989 case GIMPLE_INVALID_RHS
:
6995 /* Return true if the floating-point value computed by call STMT is known
6996 to have an integer value. We also allow +Inf, -Inf and NaN to be
6997 considered integer values. Return false for signaling NaN.
6999 DEPTH is the current nesting depth of the query. */
7002 gimple_call_integer_valued_real_p (gimple
*stmt
, int depth
)
7004 tree arg0
= (gimple_call_num_args (stmt
) > 0
7005 ? gimple_call_arg (stmt
, 0)
7007 tree arg1
= (gimple_call_num_args (stmt
) > 1
7008 ? gimple_call_arg (stmt
, 1)
7010 return integer_valued_real_call_p (gimple_call_combined_fn (stmt
),
7014 /* Return true if the floating-point result of phi STMT is known to have
7015 an integer value. We also allow +Inf, -Inf and NaN to be considered
7016 integer values. Return false for signaling NaN.
7018 DEPTH is the current nesting depth of the query. */
7021 gimple_phi_integer_valued_real_p (gimple
*stmt
, int depth
)
7023 for (unsigned i
= 0; i
< gimple_phi_num_args (stmt
); ++i
)
7025 tree arg
= gimple_phi_arg_def (stmt
, i
);
7026 if (!integer_valued_real_single_p (arg
, depth
+ 1))
7032 /* Return true if the floating-point value computed by STMT is known
7033 to have an integer value. We also allow +Inf, -Inf and NaN to be
7034 considered integer values. Return false for signaling NaN.
7036 DEPTH is the current nesting depth of the query. */
7039 gimple_stmt_integer_valued_real_p (gimple
*stmt
, int depth
)
7041 switch (gimple_code (stmt
))
7044 return gimple_assign_integer_valued_real_p (stmt
, depth
);
7046 return gimple_call_integer_valued_real_p (stmt
, depth
);
7048 return gimple_phi_integer_valued_real_p (stmt
, depth
);