1 /* Statement simplification on GIMPLE.
2 Copyright (C) 2010-2017 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"
55 #include "omp-general.h"
58 #include "fold-const-call.h"
59 #include "stringpool.h"
63 /* Return true when DECL can be referenced from current unit.
64 FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
65 We can get declarations that are not possible to reference for various
68 1) When analyzing C++ virtual tables.
69 C++ virtual tables do have known constructors even
70 when they are keyed to other compilation unit.
71 Those tables can contain pointers to methods and vars
72 in other units. Those methods have both STATIC and EXTERNAL
74 2) In WHOPR mode devirtualization might lead to reference
75 to method that was partitioned elsehwere.
76 In this case we have static VAR_DECL or FUNCTION_DECL
77 that has no corresponding callgraph/varpool node
79 3) COMDAT functions referred by external vtables that
80 we devirtualize only during final compilation stage.
81 At this time we already decided that we will not output
82 the function body and thus we can't reference the symbol
86 can_refer_decl_in_current_unit_p (tree decl
, tree from_decl
)
89 struct cgraph_node
*node
;
92 if (DECL_ABSTRACT_P (decl
))
95 /* We are concerned only about static/external vars and functions. */
96 if ((!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
97 || !VAR_OR_FUNCTION_DECL_P (decl
))
100 /* Static objects can be referred only if they was not optimized out yet. */
101 if (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
103 /* Before we start optimizing unreachable code we can be sure all
104 static objects are defined. */
105 if (symtab
->function_flags_ready
)
107 snode
= symtab_node::get (decl
);
108 if (!snode
|| !snode
->definition
)
110 node
= dyn_cast
<cgraph_node
*> (snode
);
111 return !node
|| !node
->global
.inlined_to
;
114 /* We will later output the initializer, so we can refer to it.
115 So we are concerned only when DECL comes from initializer of
116 external var or var that has been optimized out. */
118 || !VAR_P (from_decl
)
119 || (!DECL_EXTERNAL (from_decl
)
120 && (vnode
= varpool_node::get (from_decl
)) != NULL
121 && vnode
->definition
)
123 && (vnode
= varpool_node::get (from_decl
)) != NULL
124 && vnode
->in_other_partition
))
126 /* We are folding reference from external vtable. The vtable may reffer
127 to a symbol keyed to other compilation unit. The other compilation
128 unit may be in separate DSO and the symbol may be hidden. */
129 if (DECL_VISIBILITY_SPECIFIED (decl
)
130 && DECL_EXTERNAL (decl
)
131 && DECL_VISIBILITY (decl
) != VISIBILITY_DEFAULT
132 && (!(snode
= symtab_node::get (decl
)) || !snode
->in_other_partition
))
134 /* When function is public, we always can introduce new reference.
135 Exception are the COMDAT functions where introducing a direct
136 reference imply need to include function body in the curren tunit. */
137 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
))
139 /* We have COMDAT. We are going to check if we still have definition
140 or if the definition is going to be output in other partition.
141 Bypass this when gimplifying; all needed functions will be produced.
143 As observed in PR20991 for already optimized out comdat virtual functions
144 it may be tempting to not necessarily give up because the copy will be
145 output elsewhere when corresponding vtable is output.
146 This is however not possible - ABI specify that COMDATs are output in
147 units where they are used and when the other unit was compiled with LTO
148 it is possible that vtable was kept public while the function itself
150 if (!symtab
->function_flags_ready
)
153 snode
= symtab_node::get (decl
);
155 || ((!snode
->definition
|| DECL_EXTERNAL (decl
))
156 && (!snode
->in_other_partition
157 || (!snode
->forced_by_abi
&& !snode
->force_output
))))
159 node
= dyn_cast
<cgraph_node
*> (snode
);
160 return !node
|| !node
->global
.inlined_to
;
163 /* Create a temporary for TYPE for a statement STMT. If the current function
164 is in SSA form, a SSA name is created. Otherwise a temporary register
168 create_tmp_reg_or_ssa_name (tree type
, gimple
*stmt
)
170 if (gimple_in_ssa_p (cfun
))
171 return make_ssa_name (type
, stmt
);
173 return create_tmp_reg (type
);
176 /* CVAL is value taken from DECL_INITIAL of variable. Try to transform it into
177 acceptable form for is_gimple_min_invariant.
178 FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL. */
181 canonicalize_constructor_val (tree cval
, tree from_decl
)
183 tree orig_cval
= cval
;
185 if (TREE_CODE (cval
) == POINTER_PLUS_EXPR
186 && TREE_CODE (TREE_OPERAND (cval
, 1)) == INTEGER_CST
)
188 tree ptr
= TREE_OPERAND (cval
, 0);
189 if (is_gimple_min_invariant (ptr
))
190 cval
= build1_loc (EXPR_LOCATION (cval
),
191 ADDR_EXPR
, TREE_TYPE (ptr
),
192 fold_build2 (MEM_REF
, TREE_TYPE (TREE_TYPE (ptr
)),
194 fold_convert (ptr_type_node
,
195 TREE_OPERAND (cval
, 1))));
197 if (TREE_CODE (cval
) == ADDR_EXPR
)
199 tree base
= NULL_TREE
;
200 if (TREE_CODE (TREE_OPERAND (cval
, 0)) == COMPOUND_LITERAL_EXPR
)
202 base
= COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval
, 0));
204 TREE_OPERAND (cval
, 0) = base
;
207 base
= get_base_address (TREE_OPERAND (cval
, 0));
211 if (VAR_OR_FUNCTION_DECL_P (base
)
212 && !can_refer_decl_in_current_unit_p (base
, from_decl
))
214 if (TREE_TYPE (base
) == error_mark_node
)
217 TREE_ADDRESSABLE (base
) = 1;
218 else if (TREE_CODE (base
) == FUNCTION_DECL
)
220 /* Make sure we create a cgraph node for functions we'll reference.
221 They can be non-existent if the reference comes from an entry
222 of an external vtable for example. */
223 cgraph_node::get_create (base
);
225 /* Fixup types in global initializers. */
226 if (TREE_TYPE (TREE_TYPE (cval
)) != TREE_TYPE (TREE_OPERAND (cval
, 0)))
227 cval
= build_fold_addr_expr (TREE_OPERAND (cval
, 0));
229 if (!useless_type_conversion_p (TREE_TYPE (orig_cval
), TREE_TYPE (cval
)))
230 cval
= fold_convert (TREE_TYPE (orig_cval
), cval
);
233 if (TREE_OVERFLOW_P (cval
))
234 return drop_tree_overflow (cval
);
238 /* If SYM is a constant variable with known value, return the value.
239 NULL_TREE is returned otherwise. */
242 get_symbol_constant_value (tree sym
)
244 tree val
= ctor_for_folding (sym
);
245 if (val
!= error_mark_node
)
249 val
= canonicalize_constructor_val (unshare_expr (val
), sym
);
250 if (val
&& is_gimple_min_invariant (val
))
255 /* Variables declared 'const' without an initializer
256 have zero as the initializer if they may not be
257 overridden at link or run time. */
259 && is_gimple_reg_type (TREE_TYPE (sym
)))
260 return build_zero_cst (TREE_TYPE (sym
));
268 /* Subroutine of fold_stmt. We perform several simplifications of the
269 memory reference tree EXPR and make sure to re-gimplify them properly
270 after propagation of constant addresses. IS_LHS is true if the
271 reference is supposed to be an lvalue. */
274 maybe_fold_reference (tree expr
, bool is_lhs
)
278 if ((TREE_CODE (expr
) == VIEW_CONVERT_EXPR
279 || TREE_CODE (expr
) == REALPART_EXPR
280 || TREE_CODE (expr
) == IMAGPART_EXPR
)
281 && CONSTANT_CLASS_P (TREE_OPERAND (expr
, 0)))
282 return fold_unary_loc (EXPR_LOCATION (expr
),
285 TREE_OPERAND (expr
, 0));
286 else if (TREE_CODE (expr
) == BIT_FIELD_REF
287 && CONSTANT_CLASS_P (TREE_OPERAND (expr
, 0)))
288 return fold_ternary_loc (EXPR_LOCATION (expr
),
291 TREE_OPERAND (expr
, 0),
292 TREE_OPERAND (expr
, 1),
293 TREE_OPERAND (expr
, 2));
296 && (result
= fold_const_aggregate_ref (expr
))
297 && is_gimple_min_invariant (result
))
304 /* Attempt to fold an assignment statement pointed-to by SI. Returns a
305 replacement rhs for the statement or NULL_TREE if no simplification
306 could be made. It is assumed that the operands have been previously
310 fold_gimple_assign (gimple_stmt_iterator
*si
)
312 gimple
*stmt
= gsi_stmt (*si
);
313 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
314 location_t loc
= gimple_location (stmt
);
316 tree result
= NULL_TREE
;
318 switch (get_gimple_rhs_class (subcode
))
320 case GIMPLE_SINGLE_RHS
:
322 tree rhs
= gimple_assign_rhs1 (stmt
);
324 if (TREE_CLOBBER_P (rhs
))
327 if (REFERENCE_CLASS_P (rhs
))
328 return maybe_fold_reference (rhs
, false);
330 else if (TREE_CODE (rhs
) == OBJ_TYPE_REF
)
332 tree val
= OBJ_TYPE_REF_EXPR (rhs
);
333 if (is_gimple_min_invariant (val
))
335 else if (flag_devirtualize
&& virtual_method_call_p (rhs
))
338 vec
<cgraph_node
*>targets
339 = possible_polymorphic_call_targets (rhs
, stmt
, &final
);
340 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
342 if (dump_enabled_p ())
344 location_t loc
= gimple_location_safe (stmt
);
345 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, loc
,
346 "resolving virtual function address "
347 "reference to function %s\n",
348 targets
.length () == 1
349 ? targets
[0]->name ()
352 if (targets
.length () == 1)
354 val
= fold_convert (TREE_TYPE (val
),
355 build_fold_addr_expr_loc
356 (loc
, targets
[0]->decl
));
357 STRIP_USELESS_TYPE_CONVERSION (val
);
360 /* We can not use __builtin_unreachable here because it
361 can not have address taken. */
362 val
= build_int_cst (TREE_TYPE (val
), 0);
368 else if (TREE_CODE (rhs
) == ADDR_EXPR
)
370 tree ref
= TREE_OPERAND (rhs
, 0);
371 tree tem
= maybe_fold_reference (ref
, true);
373 && TREE_CODE (tem
) == MEM_REF
374 && integer_zerop (TREE_OPERAND (tem
, 1)))
375 result
= fold_convert (TREE_TYPE (rhs
), TREE_OPERAND (tem
, 0));
377 result
= fold_convert (TREE_TYPE (rhs
),
378 build_fold_addr_expr_loc (loc
, tem
));
379 else if (TREE_CODE (ref
) == MEM_REF
380 && integer_zerop (TREE_OPERAND (ref
, 1)))
381 result
= fold_convert (TREE_TYPE (rhs
), TREE_OPERAND (ref
, 0));
385 /* Strip away useless type conversions. Both the
386 NON_LVALUE_EXPR that may have been added by fold, and
387 "useless" type conversions that might now be apparent
388 due to propagation. */
389 STRIP_USELESS_TYPE_CONVERSION (result
);
391 if (result
!= rhs
&& valid_gimple_rhs_p (result
))
396 else if (TREE_CODE (rhs
) == CONSTRUCTOR
397 && TREE_CODE (TREE_TYPE (rhs
)) == VECTOR_TYPE
)
399 /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */
403 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs
), i
, val
)
404 if (! CONSTANT_CLASS_P (val
))
407 return build_vector_from_ctor (TREE_TYPE (rhs
),
408 CONSTRUCTOR_ELTS (rhs
));
411 else if (DECL_P (rhs
))
412 return get_symbol_constant_value (rhs
);
416 case GIMPLE_UNARY_RHS
:
419 case GIMPLE_BINARY_RHS
:
422 case GIMPLE_TERNARY_RHS
:
423 result
= fold_ternary_loc (loc
, subcode
,
424 TREE_TYPE (gimple_assign_lhs (stmt
)),
425 gimple_assign_rhs1 (stmt
),
426 gimple_assign_rhs2 (stmt
),
427 gimple_assign_rhs3 (stmt
));
431 STRIP_USELESS_TYPE_CONVERSION (result
);
432 if (valid_gimple_rhs_p (result
))
437 case GIMPLE_INVALID_RHS
:
445 /* Replace a statement at *SI_P with a sequence of statements in STMTS,
446 adjusting the replacement stmts location and virtual operands.
447 If the statement has a lhs the last stmt in the sequence is expected
448 to assign to that lhs. */
451 gsi_replace_with_seq_vops (gimple_stmt_iterator
*si_p
, gimple_seq stmts
)
453 gimple
*stmt
= gsi_stmt (*si_p
);
455 if (gimple_has_location (stmt
))
456 annotate_all_with_location (stmts
, gimple_location (stmt
));
458 /* First iterate over the replacement statements backward, assigning
459 virtual operands to their defining statements. */
460 gimple
*laststore
= NULL
;
461 for (gimple_stmt_iterator i
= gsi_last (stmts
);
462 !gsi_end_p (i
); gsi_prev (&i
))
464 gimple
*new_stmt
= gsi_stmt (i
);
465 if ((gimple_assign_single_p (new_stmt
)
466 && !is_gimple_reg (gimple_assign_lhs (new_stmt
)))
467 || (is_gimple_call (new_stmt
)
468 && (gimple_call_flags (new_stmt
)
469 & (ECF_NOVOPS
| ECF_PURE
| ECF_CONST
| ECF_NORETURN
)) == 0))
473 vdef
= gimple_vdef (stmt
);
475 vdef
= make_ssa_name (gimple_vop (cfun
), new_stmt
);
476 gimple_set_vdef (new_stmt
, vdef
);
477 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
478 SSA_NAME_DEF_STMT (vdef
) = new_stmt
;
479 laststore
= new_stmt
;
483 /* Second iterate over the statements forward, assigning virtual
484 operands to their uses. */
485 tree reaching_vuse
= gimple_vuse (stmt
);
486 for (gimple_stmt_iterator i
= gsi_start (stmts
);
487 !gsi_end_p (i
); gsi_next (&i
))
489 gimple
*new_stmt
= gsi_stmt (i
);
490 /* If the new statement possibly has a VUSE, update it with exact SSA
491 name we know will reach this one. */
492 if (gimple_has_mem_ops (new_stmt
))
493 gimple_set_vuse (new_stmt
, reaching_vuse
);
494 gimple_set_modified (new_stmt
, true);
495 if (gimple_vdef (new_stmt
))
496 reaching_vuse
= gimple_vdef (new_stmt
);
499 /* If the new sequence does not do a store release the virtual
500 definition of the original statement. */
502 && reaching_vuse
== gimple_vuse (stmt
))
504 tree vdef
= gimple_vdef (stmt
);
506 && TREE_CODE (vdef
) == SSA_NAME
)
508 unlink_stmt_vdef (stmt
);
509 release_ssa_name (vdef
);
513 /* Finally replace the original statement with the sequence. */
514 gsi_replace_with_seq (si_p
, stmts
, false);
517 /* Convert EXPR into a GIMPLE value suitable for substitution on the
518 RHS of an assignment. Insert the necessary statements before
519 iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL
520 is replaced. If the call is expected to produces a result, then it
521 is replaced by an assignment of the new RHS to the result variable.
522 If the result is to be ignored, then the call is replaced by a
523 GIMPLE_NOP. A proper VDEF chain is retained by making the first
524 VUSE and the last VDEF of the whole sequence be the same as the replaced
525 statement and using new SSA names for stores in between. */
528 gimplify_and_update_call_from_tree (gimple_stmt_iterator
*si_p
, tree expr
)
531 gimple
*stmt
, *new_stmt
;
532 gimple_stmt_iterator i
;
533 gimple_seq stmts
= NULL
;
535 stmt
= gsi_stmt (*si_p
);
537 gcc_assert (is_gimple_call (stmt
));
539 push_gimplify_context (gimple_in_ssa_p (cfun
));
541 lhs
= gimple_call_lhs (stmt
);
542 if (lhs
== NULL_TREE
)
544 gimplify_and_add (expr
, &stmts
);
545 /* We can end up with folding a memcpy of an empty class assignment
546 which gets optimized away by C++ gimplification. */
547 if (gimple_seq_empty_p (stmts
))
549 pop_gimplify_context (NULL
);
550 if (gimple_in_ssa_p (cfun
))
552 unlink_stmt_vdef (stmt
);
555 gsi_replace (si_p
, gimple_build_nop (), false);
561 tree tmp
= force_gimple_operand (expr
, &stmts
, false, NULL_TREE
);
562 new_stmt
= gimple_build_assign (lhs
, tmp
);
563 i
= gsi_last (stmts
);
564 gsi_insert_after_without_update (&i
, new_stmt
,
565 GSI_CONTINUE_LINKING
);
568 pop_gimplify_context (NULL
);
570 gsi_replace_with_seq_vops (si_p
, stmts
);
574 /* Replace the call at *GSI with the gimple value VAL. */
577 replace_call_with_value (gimple_stmt_iterator
*gsi
, tree val
)
579 gimple
*stmt
= gsi_stmt (*gsi
);
580 tree lhs
= gimple_call_lhs (stmt
);
584 if (!useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (val
)))
585 val
= fold_convert (TREE_TYPE (lhs
), val
);
586 repl
= gimple_build_assign (lhs
, val
);
589 repl
= gimple_build_nop ();
590 tree vdef
= gimple_vdef (stmt
);
591 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
593 unlink_stmt_vdef (stmt
);
594 release_ssa_name (vdef
);
596 gsi_replace (gsi
, repl
, false);
599 /* Replace the call at *GSI with the new call REPL and fold that
603 replace_call_with_call_and_fold (gimple_stmt_iterator
*gsi
, gimple
*repl
)
605 gimple
*stmt
= gsi_stmt (*gsi
);
606 gimple_call_set_lhs (repl
, gimple_call_lhs (stmt
));
607 gimple_set_location (repl
, gimple_location (stmt
));
608 if (gimple_vdef (stmt
)
609 && TREE_CODE (gimple_vdef (stmt
)) == SSA_NAME
)
611 gimple_set_vdef (repl
, gimple_vdef (stmt
));
612 SSA_NAME_DEF_STMT (gimple_vdef (repl
)) = repl
;
614 if (gimple_vuse (stmt
))
615 gimple_set_vuse (repl
, gimple_vuse (stmt
));
616 gsi_replace (gsi
, repl
, false);
620 /* Return true if VAR is a VAR_DECL or a component thereof. */
623 var_decl_component_p (tree var
)
626 while (handled_component_p (inner
))
627 inner
= TREE_OPERAND (inner
, 0);
628 return SSA_VAR_P (inner
);
631 /* Fold function call to builtin mem{{,p}cpy,move}. Return
632 false if no simplification can be made.
633 If ENDP is 0, return DEST (like memcpy).
634 If ENDP is 1, return DEST+LEN (like mempcpy).
635 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
636 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
640 gimple_fold_builtin_memory_op (gimple_stmt_iterator
*gsi
,
641 tree dest
, tree src
, int endp
)
643 gimple
*stmt
= gsi_stmt (*gsi
);
644 tree lhs
= gimple_call_lhs (stmt
);
645 tree len
= gimple_call_arg (stmt
, 2);
646 tree destvar
, srcvar
;
647 location_t loc
= gimple_location (stmt
);
649 /* If the LEN parameter is zero, return DEST. */
650 if (integer_zerop (len
))
653 if (gimple_call_lhs (stmt
))
654 repl
= gimple_build_assign (gimple_call_lhs (stmt
), dest
);
656 repl
= gimple_build_nop ();
657 tree vdef
= gimple_vdef (stmt
);
658 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
660 unlink_stmt_vdef (stmt
);
661 release_ssa_name (vdef
);
663 gsi_replace (gsi
, repl
, false);
667 /* If SRC and DEST are the same (and not volatile), return
668 DEST{,+LEN,+LEN-1}. */
669 if (operand_equal_p (src
, dest
, 0))
671 unlink_stmt_vdef (stmt
);
672 if (gimple_vdef (stmt
) && TREE_CODE (gimple_vdef (stmt
)) == SSA_NAME
)
673 release_ssa_name (gimple_vdef (stmt
));
676 gsi_replace (gsi
, gimple_build_nop (), false);
683 tree srctype
, desttype
;
684 unsigned int src_align
, dest_align
;
687 /* Inlining of memcpy/memmove may cause bounds lost (if we copy
688 pointers as wide integer) and also may result in huge function
689 size because of inlined bounds copy. Thus don't inline for
690 functions we want to instrument. */
691 if (flag_check_pointer_bounds
692 && chkp_instrumentable_p (cfun
->decl
)
693 /* Even if data may contain pointers we can inline if copy
694 less than a pointer size. */
695 && (!tree_fits_uhwi_p (len
)
696 || compare_tree_int (len
, POINTER_SIZE_UNITS
) >= 0))
699 /* Build accesses at offset zero with a ref-all character type. */
700 off0
= build_int_cst (build_pointer_type_for_mode (char_type_node
,
703 /* If we can perform the copy efficiently with first doing all loads
704 and then all stores inline it that way. Currently efficiently
705 means that we can load all the memory into a single integer
706 register which is what MOVE_MAX gives us. */
707 src_align
= get_pointer_alignment (src
);
708 dest_align
= get_pointer_alignment (dest
);
709 if (tree_fits_uhwi_p (len
)
710 && compare_tree_int (len
, MOVE_MAX
) <= 0
711 /* ??? Don't transform copies from strings with known length this
712 confuses the tree-ssa-strlen.c. This doesn't handle
713 the case in gcc.dg/strlenopt-8.c which is XFAILed for that
715 && !c_strlen (src
, 2))
717 unsigned ilen
= tree_to_uhwi (len
);
718 if (pow2p_hwi (ilen
))
720 tree type
= lang_hooks
.types
.type_for_size (ilen
* 8, 1);
722 && TYPE_MODE (type
) != BLKmode
723 && (GET_MODE_SIZE (TYPE_MODE (type
)) * BITS_PER_UNIT
725 /* If the destination pointer is not aligned we must be able
726 to emit an unaligned store. */
727 && (dest_align
>= GET_MODE_ALIGNMENT (TYPE_MODE (type
))
728 || !SLOW_UNALIGNED_ACCESS (TYPE_MODE (type
), dest_align
)
729 || (optab_handler (movmisalign_optab
, TYPE_MODE (type
))
730 != CODE_FOR_nothing
)))
733 tree desttype
= type
;
734 if (src_align
< GET_MODE_ALIGNMENT (TYPE_MODE (type
)))
735 srctype
= build_aligned_type (type
, src_align
);
736 tree srcmem
= fold_build2 (MEM_REF
, srctype
, src
, off0
);
737 tree tem
= fold_const_aggregate_ref (srcmem
);
740 else if (src_align
< GET_MODE_ALIGNMENT (TYPE_MODE (type
))
741 && SLOW_UNALIGNED_ACCESS (TYPE_MODE (type
),
743 && (optab_handler (movmisalign_optab
,
745 == CODE_FOR_nothing
))
750 if (is_gimple_reg_type (TREE_TYPE (srcmem
)))
752 new_stmt
= gimple_build_assign (NULL_TREE
, srcmem
);
754 = create_tmp_reg_or_ssa_name (TREE_TYPE (srcmem
),
756 gimple_assign_set_lhs (new_stmt
, srcmem
);
757 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
758 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
760 if (dest_align
< GET_MODE_ALIGNMENT (TYPE_MODE (type
)))
761 desttype
= build_aligned_type (type
, dest_align
);
763 = gimple_build_assign (fold_build2 (MEM_REF
, desttype
,
766 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
767 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
768 if (gimple_vdef (new_stmt
)
769 && TREE_CODE (gimple_vdef (new_stmt
)) == SSA_NAME
)
770 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt
)) = new_stmt
;
773 gsi_replace (gsi
, new_stmt
, false);
776 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
785 /* Both DEST and SRC must be pointer types.
786 ??? This is what old code did. Is the testing for pointer types
789 If either SRC is readonly or length is 1, we can use memcpy. */
790 if (!dest_align
|| !src_align
)
792 if (readonly_data_expr (src
)
793 || (tree_fits_uhwi_p (len
)
794 && (MIN (src_align
, dest_align
) / BITS_PER_UNIT
795 >= tree_to_uhwi (len
))))
797 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
800 gimple_call_set_fndecl (stmt
, fn
);
801 gimple_call_set_arg (stmt
, 0, dest
);
802 gimple_call_set_arg (stmt
, 1, src
);
807 /* If *src and *dest can't overlap, optimize into memcpy as well. */
808 if (TREE_CODE (src
) == ADDR_EXPR
809 && TREE_CODE (dest
) == ADDR_EXPR
)
811 tree src_base
, dest_base
, fn
;
812 HOST_WIDE_INT src_offset
= 0, dest_offset
= 0;
813 HOST_WIDE_INT maxsize
;
815 srcvar
= TREE_OPERAND (src
, 0);
816 src_base
= get_addr_base_and_unit_offset (srcvar
, &src_offset
);
817 if (src_base
== NULL
)
819 destvar
= TREE_OPERAND (dest
, 0);
820 dest_base
= get_addr_base_and_unit_offset (destvar
,
822 if (dest_base
== NULL
)
824 if (tree_fits_uhwi_p (len
))
825 maxsize
= tree_to_uhwi (len
);
828 if (SSA_VAR_P (src_base
)
829 && SSA_VAR_P (dest_base
))
831 if (operand_equal_p (src_base
, dest_base
, 0)
832 && ranges_overlap_p (src_offset
, maxsize
,
833 dest_offset
, maxsize
))
836 else if (TREE_CODE (src_base
) == MEM_REF
837 && TREE_CODE (dest_base
) == MEM_REF
)
839 if (! operand_equal_p (TREE_OPERAND (src_base
, 0),
840 TREE_OPERAND (dest_base
, 0), 0))
842 offset_int off
= mem_ref_offset (src_base
) + src_offset
;
843 if (!wi::fits_shwi_p (off
))
845 src_offset
= off
.to_shwi ();
847 off
= mem_ref_offset (dest_base
) + dest_offset
;
848 if (!wi::fits_shwi_p (off
))
850 dest_offset
= off
.to_shwi ();
851 if (ranges_overlap_p (src_offset
, maxsize
,
852 dest_offset
, maxsize
))
858 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
861 gimple_call_set_fndecl (stmt
, fn
);
862 gimple_call_set_arg (stmt
, 0, dest
);
863 gimple_call_set_arg (stmt
, 1, src
);
868 /* If the destination and source do not alias optimize into
870 if ((is_gimple_min_invariant (dest
)
871 || TREE_CODE (dest
) == SSA_NAME
)
872 && (is_gimple_min_invariant (src
)
873 || TREE_CODE (src
) == SSA_NAME
))
876 ao_ref_init_from_ptr_and_size (&destr
, dest
, len
);
877 ao_ref_init_from_ptr_and_size (&srcr
, src
, len
);
878 if (!refs_may_alias_p_1 (&destr
, &srcr
, false))
881 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
884 gimple_call_set_fndecl (stmt
, fn
);
885 gimple_call_set_arg (stmt
, 0, dest
);
886 gimple_call_set_arg (stmt
, 1, src
);
895 if (!tree_fits_shwi_p (len
))
898 This logic lose for arguments like (type *)malloc (sizeof (type)),
899 since we strip the casts of up to VOID return value from malloc.
900 Perhaps we ought to inherit type from non-VOID argument here? */
903 if (!POINTER_TYPE_P (TREE_TYPE (src
))
904 || !POINTER_TYPE_P (TREE_TYPE (dest
)))
906 /* In the following try to find a type that is most natural to be
907 used for the memcpy source and destination and that allows
908 the most optimization when memcpy is turned into a plain assignment
909 using that type. In theory we could always use a char[len] type
910 but that only gains us that the destination and source possibly
911 no longer will have their address taken. */
912 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
913 if (TREE_CODE (src
) == POINTER_PLUS_EXPR
)
915 tree tem
= TREE_OPERAND (src
, 0);
917 if (tem
!= TREE_OPERAND (src
, 0))
918 src
= build1 (NOP_EXPR
, TREE_TYPE (tem
), src
);
920 if (TREE_CODE (dest
) == POINTER_PLUS_EXPR
)
922 tree tem
= TREE_OPERAND (dest
, 0);
924 if (tem
!= TREE_OPERAND (dest
, 0))
925 dest
= build1 (NOP_EXPR
, TREE_TYPE (tem
), dest
);
927 srctype
= TREE_TYPE (TREE_TYPE (src
));
928 if (TREE_CODE (srctype
) == ARRAY_TYPE
929 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype
), len
))
931 srctype
= TREE_TYPE (srctype
);
933 src
= build1 (NOP_EXPR
, build_pointer_type (srctype
), src
);
935 desttype
= TREE_TYPE (TREE_TYPE (dest
));
936 if (TREE_CODE (desttype
) == ARRAY_TYPE
937 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype
), len
))
939 desttype
= TREE_TYPE (desttype
);
941 dest
= build1 (NOP_EXPR
, build_pointer_type (desttype
), dest
);
943 if (TREE_ADDRESSABLE (srctype
)
944 || TREE_ADDRESSABLE (desttype
))
947 /* Make sure we are not copying using a floating-point mode or
948 a type whose size possibly does not match its precision. */
949 if (FLOAT_MODE_P (TYPE_MODE (desttype
))
950 || TREE_CODE (desttype
) == BOOLEAN_TYPE
951 || TREE_CODE (desttype
) == ENUMERAL_TYPE
)
952 desttype
= bitwise_type_for_mode (TYPE_MODE (desttype
));
953 if (FLOAT_MODE_P (TYPE_MODE (srctype
))
954 || TREE_CODE (srctype
) == BOOLEAN_TYPE
955 || TREE_CODE (srctype
) == ENUMERAL_TYPE
)
956 srctype
= bitwise_type_for_mode (TYPE_MODE (srctype
));
964 src_align
= get_pointer_alignment (src
);
965 dest_align
= get_pointer_alignment (dest
);
966 if (dest_align
< TYPE_ALIGN (desttype
)
967 || src_align
< TYPE_ALIGN (srctype
))
971 STRIP_NOPS (destvar
);
972 if (TREE_CODE (destvar
) == ADDR_EXPR
973 && var_decl_component_p (TREE_OPERAND (destvar
, 0))
974 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype
), len
))
975 destvar
= fold_build2 (MEM_REF
, desttype
, destvar
, off0
);
981 if (TREE_CODE (srcvar
) == ADDR_EXPR
982 && var_decl_component_p (TREE_OPERAND (srcvar
, 0))
983 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype
), len
))
986 || src_align
>= TYPE_ALIGN (desttype
))
987 srcvar
= fold_build2 (MEM_REF
, destvar
? desttype
: srctype
,
989 else if (!STRICT_ALIGNMENT
)
991 srctype
= build_aligned_type (TYPE_MAIN_VARIANT (desttype
),
993 srcvar
= fold_build2 (MEM_REF
, srctype
, srcvar
, off0
);
1001 if (srcvar
== NULL_TREE
&& destvar
== NULL_TREE
)
1004 if (srcvar
== NULL_TREE
)
1007 if (src_align
>= TYPE_ALIGN (desttype
))
1008 srcvar
= fold_build2 (MEM_REF
, desttype
, src
, off0
);
1011 if (STRICT_ALIGNMENT
)
1013 srctype
= build_aligned_type (TYPE_MAIN_VARIANT (desttype
),
1015 srcvar
= fold_build2 (MEM_REF
, srctype
, src
, off0
);
1018 else if (destvar
== NULL_TREE
)
1021 if (dest_align
>= TYPE_ALIGN (srctype
))
1022 destvar
= fold_build2 (MEM_REF
, srctype
, dest
, off0
);
1025 if (STRICT_ALIGNMENT
)
1027 desttype
= build_aligned_type (TYPE_MAIN_VARIANT (srctype
),
1029 destvar
= fold_build2 (MEM_REF
, desttype
, dest
, off0
);
1034 if (is_gimple_reg_type (TREE_TYPE (srcvar
)))
1036 tree tem
= fold_const_aggregate_ref (srcvar
);
1039 if (! is_gimple_min_invariant (srcvar
))
1041 new_stmt
= gimple_build_assign (NULL_TREE
, srcvar
);
1042 srcvar
= create_tmp_reg_or_ssa_name (TREE_TYPE (srcvar
),
1044 gimple_assign_set_lhs (new_stmt
, srcvar
);
1045 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
1046 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
1049 new_stmt
= gimple_build_assign (destvar
, srcvar
);
1050 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
1051 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
1052 if (gimple_vdef (new_stmt
)
1053 && TREE_CODE (gimple_vdef (new_stmt
)) == SSA_NAME
)
1054 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt
)) = new_stmt
;
1057 gsi_replace (gsi
, new_stmt
, false);
1060 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
1064 gimple_seq stmts
= NULL
;
1065 if (endp
== 0 || endp
== 3)
1068 len
= gimple_build (&stmts
, loc
, MINUS_EXPR
, TREE_TYPE (len
), len
,
1070 if (endp
== 2 || endp
== 1)
1072 len
= gimple_convert_to_ptrofftype (&stmts
, loc
, len
);
1073 dest
= gimple_build (&stmts
, loc
, POINTER_PLUS_EXPR
,
1074 TREE_TYPE (dest
), dest
, len
);
1077 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1078 gimple
*repl
= gimple_build_assign (lhs
, dest
);
1079 gsi_replace (gsi
, repl
, false);
1083 /* Transform a call to built-in bcmp(a, b, len) at *GSI into one
1084 to built-in memcmp (a, b, len). */
1087 gimple_fold_builtin_bcmp (gimple_stmt_iterator
*gsi
)
1089 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCMP
);
1094 /* Transform bcmp (a, b, len) into memcmp (a, b, len). */
1096 gimple
*stmt
= gsi_stmt (*gsi
);
1097 tree a
= gimple_call_arg (stmt
, 0);
1098 tree b
= gimple_call_arg (stmt
, 1);
1099 tree len
= gimple_call_arg (stmt
, 2);
1101 gimple
*repl
= gimple_build_call (fn
, 3, a
, b
, len
);
1102 replace_call_with_call_and_fold (gsi
, repl
);
1107 /* Transform a call to built-in bcopy (src, dest, len) at *GSI into one
1108 to built-in memmove (dest, src, len). */
1111 gimple_fold_builtin_bcopy (gimple_stmt_iterator
*gsi
)
1113 tree fn
= builtin_decl_implicit (BUILT_IN_MEMMOVE
);
1118 /* bcopy has been removed from POSIX in Issue 7 but Issue 6 specifies
1119 it's quivalent to memmove (not memcpy). Transform bcopy (src, dest,
1120 len) into memmove (dest, src, len). */
1122 gimple
*stmt
= gsi_stmt (*gsi
);
1123 tree src
= gimple_call_arg (stmt
, 0);
1124 tree dest
= gimple_call_arg (stmt
, 1);
1125 tree len
= gimple_call_arg (stmt
, 2);
1127 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1128 gimple_call_set_fntype (as_a
<gcall
*> (stmt
), TREE_TYPE (fn
));
1129 replace_call_with_call_and_fold (gsi
, repl
);
1134 /* Transform a call to built-in bzero (dest, len) at *GSI into one
1135 to built-in memset (dest, 0, len). */
1138 gimple_fold_builtin_bzero (gimple_stmt_iterator
*gsi
)
1140 tree fn
= builtin_decl_implicit (BUILT_IN_MEMSET
);
1145 /* Transform bzero (dest, len) into memset (dest, 0, len). */
1147 gimple
*stmt
= gsi_stmt (*gsi
);
1148 tree dest
= gimple_call_arg (stmt
, 0);
1149 tree len
= gimple_call_arg (stmt
, 1);
1151 gimple_seq seq
= NULL
;
1152 gimple
*repl
= gimple_build_call (fn
, 3, dest
, integer_zero_node
, len
);
1153 gimple_seq_add_stmt_without_update (&seq
, repl
);
1154 gsi_replace_with_seq_vops (gsi
, seq
);
1160 /* Fold function call to builtin memset or bzero at *GSI setting the
1161 memory of size LEN to VAL. Return whether a simplification was made. */
1164 gimple_fold_builtin_memset (gimple_stmt_iterator
*gsi
, tree c
, tree len
)
1166 gimple
*stmt
= gsi_stmt (*gsi
);
1168 unsigned HOST_WIDE_INT length
, cval
;
1170 /* If the LEN parameter is zero, return DEST. */
1171 if (integer_zerop (len
))
1173 replace_call_with_value (gsi
, gimple_call_arg (stmt
, 0));
1177 if (! tree_fits_uhwi_p (len
))
1180 if (TREE_CODE (c
) != INTEGER_CST
)
1183 tree dest
= gimple_call_arg (stmt
, 0);
1185 if (TREE_CODE (var
) != ADDR_EXPR
)
1188 var
= TREE_OPERAND (var
, 0);
1189 if (TREE_THIS_VOLATILE (var
))
1192 etype
= TREE_TYPE (var
);
1193 if (TREE_CODE (etype
) == ARRAY_TYPE
)
1194 etype
= TREE_TYPE (etype
);
1196 if (!INTEGRAL_TYPE_P (etype
)
1197 && !POINTER_TYPE_P (etype
))
1200 if (! var_decl_component_p (var
))
1203 length
= tree_to_uhwi (len
);
1204 if (GET_MODE_SIZE (TYPE_MODE (etype
)) != length
1205 || get_pointer_alignment (dest
) / BITS_PER_UNIT
< length
)
1208 if (length
> HOST_BITS_PER_WIDE_INT
/ BITS_PER_UNIT
)
1211 if (integer_zerop (c
))
1215 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8 || HOST_BITS_PER_WIDE_INT
> 64)
1218 cval
= TREE_INT_CST_LOW (c
);
1222 cval
|= (cval
<< 31) << 1;
1225 var
= fold_build2 (MEM_REF
, etype
, dest
, build_int_cst (ptr_type_node
, 0));
1226 gimple
*store
= gimple_build_assign (var
, build_int_cst_type (etype
, cval
));
1227 gimple_set_vuse (store
, gimple_vuse (stmt
));
1228 tree vdef
= gimple_vdef (stmt
);
1229 if (vdef
&& TREE_CODE (vdef
) == SSA_NAME
)
1231 gimple_set_vdef (store
, gimple_vdef (stmt
));
1232 SSA_NAME_DEF_STMT (gimple_vdef (stmt
)) = store
;
1234 gsi_insert_before (gsi
, store
, GSI_SAME_STMT
);
1235 if (gimple_call_lhs (stmt
))
1237 gimple
*asgn
= gimple_build_assign (gimple_call_lhs (stmt
), dest
);
1238 gsi_replace (gsi
, asgn
, false);
1242 gimple_stmt_iterator gsi2
= *gsi
;
1244 gsi_remove (&gsi2
, true);
1251 /* Obtain the minimum and maximum string length or minimum and maximum
1252 value of ARG in LENGTH[0] and LENGTH[1], respectively.
1253 If ARG is an SSA name variable, follow its use-def chains. When
1254 TYPE == 0, if LENGTH[1] is not equal to the length we determine or
1255 if we are unable to determine the length or value, return False.
1256 VISITED is a bitmap of visited variables.
1257 TYPE is 0 if string length should be obtained, 1 for maximum string
1258 length and 2 for maximum value ARG can have.
1259 When FUZZY is set and the length of a string cannot be determined,
1260 the function instead considers as the maximum possible length the
1261 size of a character array it may refer to.
1262 Set *FLEXP to true if the range of the string lengths has been
1263 obtained from the upper bound of an array at the end of a struct.
1264 Such an array may hold a string that's longer than its upper bound
1265 due to it being used as a poor-man's flexible array member. */
1268 get_range_strlen (tree arg
, tree length
[2], bitmap
*visited
, int type
,
1269 bool fuzzy
, bool *flexp
)
1274 /* The minimum and maximum length. The MAXLEN pointer stays unchanged
1275 but MINLEN may be cleared during the execution of the function. */
1276 tree
*minlen
= length
;
1277 tree
*const maxlen
= length
+ 1;
1279 if (TREE_CODE (arg
) != SSA_NAME
)
1281 /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */
1282 if (TREE_CODE (arg
) == ADDR_EXPR
1283 && TREE_CODE (TREE_OPERAND (arg
, 0)) == ARRAY_REF
1284 && integer_zerop (TREE_OPERAND (TREE_OPERAND (arg
, 0), 1)))
1286 tree aop0
= TREE_OPERAND (TREE_OPERAND (arg
, 0), 0);
1287 if (TREE_CODE (aop0
) == INDIRECT_REF
1288 && TREE_CODE (TREE_OPERAND (aop0
, 0)) == SSA_NAME
)
1289 return get_range_strlen (TREE_OPERAND (aop0
, 0),
1290 length
, visited
, type
, fuzzy
, flexp
);
1296 if (TREE_CODE (val
) != INTEGER_CST
1297 || tree_int_cst_sgn (val
) < 0)
1301 val
= c_strlen (arg
, 1);
1305 if (TREE_CODE (arg
) == ADDR_EXPR
)
1306 return get_range_strlen (TREE_OPERAND (arg
, 0), length
,
1307 visited
, type
, fuzzy
, flexp
);
1309 if (TREE_CODE (arg
) == COMPONENT_REF
1310 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg
, 1))) == ARRAY_TYPE
)
1312 /* Use the type of the member array to determine the upper
1313 bound on the length of the array. This may be overly
1314 optimistic if the array itself isn't NUL-terminated and
1315 the caller relies on the subsequent member to contain
1317 Set *FLEXP to true if the array whose bound is being
1318 used is at the end of a struct. */
1319 if (array_at_struct_end_p (arg
))
1322 arg
= TREE_OPERAND (arg
, 1);
1323 val
= TYPE_SIZE_UNIT (TREE_TYPE (arg
));
1324 if (!val
|| integer_zerop (val
))
1326 val
= fold_build2 (MINUS_EXPR
, TREE_TYPE (val
), val
,
1328 /* Set the minimum size to zero since the string in
1329 the array could have zero length. */
1330 *minlen
= ssize_int (0);
1340 && TREE_CODE (*minlen
) == INTEGER_CST
1341 && TREE_CODE (val
) == INTEGER_CST
1342 && tree_int_cst_lt (val
, *minlen
))))
1349 if (TREE_CODE (*maxlen
) != INTEGER_CST
1350 || TREE_CODE (val
) != INTEGER_CST
)
1353 if (tree_int_cst_lt (*maxlen
, val
))
1357 else if (simple_cst_equal (val
, *maxlen
) != 1)
1365 /* If ARG is registered for SSA update we cannot look at its defining
1367 if (name_registered_for_update_p (arg
))
1370 /* If we were already here, break the infinite cycle. */
1372 *visited
= BITMAP_ALLOC (NULL
);
1373 if (!bitmap_set_bit (*visited
, SSA_NAME_VERSION (arg
)))
1377 def_stmt
= SSA_NAME_DEF_STMT (var
);
1379 switch (gimple_code (def_stmt
))
1382 /* The RHS of the statement defining VAR must either have a
1383 constant length or come from another SSA_NAME with a constant
1385 if (gimple_assign_single_p (def_stmt
)
1386 || gimple_assign_unary_nop_p (def_stmt
))
1388 tree rhs
= gimple_assign_rhs1 (def_stmt
);
1389 return get_range_strlen (rhs
, length
, visited
, type
, fuzzy
, flexp
);
1391 else if (gimple_assign_rhs_code (def_stmt
) == COND_EXPR
)
1393 tree op2
= gimple_assign_rhs2 (def_stmt
);
1394 tree op3
= gimple_assign_rhs3 (def_stmt
);
1395 return get_range_strlen (op2
, length
, visited
, type
, fuzzy
, flexp
)
1396 && get_range_strlen (op3
, length
, visited
, type
, fuzzy
, flexp
);
1402 /* All the arguments of the PHI node must have the same constant
1406 for (i
= 0; i
< gimple_phi_num_args (def_stmt
); i
++)
1408 tree arg
= gimple_phi_arg (def_stmt
, i
)->def
;
1410 /* If this PHI has itself as an argument, we cannot
1411 determine the string length of this argument. However,
1412 if we can find a constant string length for the other
1413 PHI args then we can still be sure that this is a
1414 constant string length. So be optimistic and just
1415 continue with the next argument. */
1416 if (arg
== gimple_phi_result (def_stmt
))
1419 if (!get_range_strlen (arg
, length
, visited
, type
, fuzzy
, flexp
))
1422 *maxlen
= build_all_ones_cst (size_type_node
);
1435 /* Determine the minimum and maximum value or string length that ARG
1436 refers to and store each in the first two elements of MINMAXLEN.
1437 For expressions that point to strings of unknown lengths that are
1438 character arrays, use the upper bound of the array as the maximum
1439 length. For example, given an expression like 'x ? array : "xyz"'
1440 and array declared as 'char array[8]', MINMAXLEN[0] will be set
1441 to 3 and MINMAXLEN[1] to 7, the longest string that could be
1443 Return true if the range of the string lengths has been obtained
1444 from the upper bound of an array at the end of a struct. Such
1445 an array may hold a string that's longer than its upper bound
1446 due to it being used as a poor-man's flexible array member. */
1449 get_range_strlen (tree arg
, tree minmaxlen
[2])
1451 bitmap visited
= NULL
;
1453 minmaxlen
[0] = NULL_TREE
;
1454 minmaxlen
[1] = NULL_TREE
;
1456 bool flexarray
= false;
1457 get_range_strlen (arg
, minmaxlen
, &visited
, 1, true, &flexarray
);
1460 BITMAP_FREE (visited
);
1466 get_maxval_strlen (tree arg
, int type
)
1468 bitmap visited
= NULL
;
1469 tree len
[2] = { NULL_TREE
, NULL_TREE
};
1472 if (!get_range_strlen (arg
, len
, &visited
, type
, false, &dummy
))
1475 BITMAP_FREE (visited
);
1481 /* Fold function call to builtin strcpy with arguments DEST and SRC.
1482 If LEN is not NULL, it represents the length of the string to be
1483 copied. Return NULL_TREE if no simplification can be made. */
1486 gimple_fold_builtin_strcpy (gimple_stmt_iterator
*gsi
,
1487 tree dest
, tree src
)
1489 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1492 /* If SRC and DEST are the same (and not volatile), return DEST. */
1493 if (operand_equal_p (src
, dest
, 0))
1495 replace_call_with_value (gsi
, dest
);
1499 if (optimize_function_for_size_p (cfun
))
1502 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
1506 tree len
= get_maxval_strlen (src
, 0);
1510 len
= fold_convert_loc (loc
, size_type_node
, len
);
1511 len
= size_binop_loc (loc
, PLUS_EXPR
, len
, build_int_cst (size_type_node
, 1));
1512 len
= force_gimple_operand_gsi (gsi
, len
, true,
1513 NULL_TREE
, true, GSI_SAME_STMT
);
1514 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1515 replace_call_with_call_and_fold (gsi
, repl
);
1519 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
1520 If SLEN is not NULL, it represents the length of the source string.
1521 Return NULL_TREE if no simplification can be made. */
1524 gimple_fold_builtin_strncpy (gimple_stmt_iterator
*gsi
,
1525 tree dest
, tree src
, tree len
)
1527 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1530 /* If the LEN parameter is zero, return DEST. */
1531 if (integer_zerop (len
))
1533 replace_call_with_value (gsi
, dest
);
1537 /* We can't compare slen with len as constants below if len is not a
1539 if (TREE_CODE (len
) != INTEGER_CST
)
1542 /* Now, we must be passed a constant src ptr parameter. */
1543 tree slen
= get_maxval_strlen (src
, 0);
1544 if (!slen
|| TREE_CODE (slen
) != INTEGER_CST
)
1547 slen
= size_binop_loc (loc
, PLUS_EXPR
, slen
, ssize_int (1));
1549 /* We do not support simplification of this case, though we do
1550 support it when expanding trees into RTL. */
1551 /* FIXME: generate a call to __builtin_memset. */
1552 if (tree_int_cst_lt (slen
, len
))
1555 /* OK transform into builtin memcpy. */
1556 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
1560 len
= fold_convert_loc (loc
, size_type_node
, len
);
1561 len
= force_gimple_operand_gsi (gsi
, len
, true,
1562 NULL_TREE
, true, GSI_SAME_STMT
);
1563 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1564 replace_call_with_call_and_fold (gsi
, repl
);
1568 /* Fold function call to builtin strchr or strrchr.
1569 If both arguments are constant, evaluate and fold the result,
1570 otherwise simplify str(r)chr (str, 0) into str + strlen (str).
1571 In general strlen is significantly faster than strchr
1572 due to being a simpler operation. */
1574 gimple_fold_builtin_strchr (gimple_stmt_iterator
*gsi
, bool is_strrchr
)
1576 gimple
*stmt
= gsi_stmt (*gsi
);
1577 tree str
= gimple_call_arg (stmt
, 0);
1578 tree c
= gimple_call_arg (stmt
, 1);
1579 location_t loc
= gimple_location (stmt
);
1583 if (!gimple_call_lhs (stmt
))
1586 if ((p
= c_getstr (str
)) && target_char_cst_p (c
, &ch
))
1588 const char *p1
= is_strrchr
? strrchr (p
, ch
) : strchr (p
, ch
);
1592 replace_call_with_value (gsi
, integer_zero_node
);
1596 tree len
= build_int_cst (size_type_node
, p1
- p
);
1597 gimple_seq stmts
= NULL
;
1598 gimple
*new_stmt
= gimple_build_assign (gimple_call_lhs (stmt
),
1599 POINTER_PLUS_EXPR
, str
, len
);
1600 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
1601 gsi_replace_with_seq_vops (gsi
, stmts
);
1605 if (!integer_zerop (c
))
1608 /* Transform strrchr (s, 0) to strchr (s, 0) when optimizing for size. */
1609 if (is_strrchr
&& optimize_function_for_size_p (cfun
))
1611 tree strchr_fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
1615 gimple
*repl
= gimple_build_call (strchr_fn
, 2, str
, c
);
1616 replace_call_with_call_and_fold (gsi
, repl
);
1624 tree strlen_fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
1629 /* Create newstr = strlen (str). */
1630 gimple_seq stmts
= NULL
;
1631 gimple
*new_stmt
= gimple_build_call (strlen_fn
, 1, str
);
1632 gimple_set_location (new_stmt
, loc
);
1633 len
= create_tmp_reg_or_ssa_name (size_type_node
);
1634 gimple_call_set_lhs (new_stmt
, len
);
1635 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
1637 /* Create (str p+ strlen (str)). */
1638 new_stmt
= gimple_build_assign (gimple_call_lhs (stmt
),
1639 POINTER_PLUS_EXPR
, str
, len
);
1640 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
1641 gsi_replace_with_seq_vops (gsi
, stmts
);
1642 /* gsi now points at the assignment to the lhs, get a
1643 stmt iterator to the strlen.
1644 ??? We can't use gsi_for_stmt as that doesn't work when the
1645 CFG isn't built yet. */
1646 gimple_stmt_iterator gsi2
= *gsi
;
1652 /* Fold function call to builtin strstr.
1653 If both arguments are constant, evaluate and fold the result,
1654 additionally fold strstr (x, "") into x and strstr (x, "c")
1655 into strchr (x, 'c'). */
1657 gimple_fold_builtin_strstr (gimple_stmt_iterator
*gsi
)
1659 gimple
*stmt
= gsi_stmt (*gsi
);
1660 tree haystack
= gimple_call_arg (stmt
, 0);
1661 tree needle
= gimple_call_arg (stmt
, 1);
1664 if (!gimple_call_lhs (stmt
))
1667 q
= c_getstr (needle
);
1671 if ((p
= c_getstr (haystack
)))
1673 const char *r
= strstr (p
, q
);
1677 replace_call_with_value (gsi
, integer_zero_node
);
1681 tree len
= build_int_cst (size_type_node
, r
- p
);
1682 gimple_seq stmts
= NULL
;
1684 = gimple_build_assign (gimple_call_lhs (stmt
), POINTER_PLUS_EXPR
,
1686 gimple_seq_add_stmt_without_update (&stmts
, new_stmt
);
1687 gsi_replace_with_seq_vops (gsi
, stmts
);
1691 /* For strstr (x, "") return x. */
1694 replace_call_with_value (gsi
, haystack
);
1698 /* Transform strstr (x, "c") into strchr (x, 'c'). */
1701 tree strchr_fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
1704 tree c
= build_int_cst (integer_type_node
, q
[0]);
1705 gimple
*repl
= gimple_build_call (strchr_fn
, 2, haystack
, c
);
1706 replace_call_with_call_and_fold (gsi
, repl
);
1714 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
1717 Return NULL_TREE if no simplification was possible, otherwise return the
1718 simplified form of the call as a tree.
1720 The simplified form may be a constant or other expression which
1721 computes the same value, but in a more efficient manner (including
1722 calls to other builtin functions).
1724 The call may contain arguments which need to be evaluated, but
1725 which are not useful to determine the result of the call. In
1726 this case we return a chain of COMPOUND_EXPRs. The LHS of each
1727 COMPOUND_EXPR will be an argument which must be evaluated.
1728 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
1729 COMPOUND_EXPR in the chain will contain the tree for the simplified
1730 form of the builtin function call. */
1733 gimple_fold_builtin_strcat (gimple_stmt_iterator
*gsi
, tree dst
, tree src
)
1735 gimple
*stmt
= gsi_stmt (*gsi
);
1736 location_t loc
= gimple_location (stmt
);
1738 const char *p
= c_getstr (src
);
1740 /* If the string length is zero, return the dst parameter. */
1741 if (p
&& *p
== '\0')
1743 replace_call_with_value (gsi
, dst
);
1747 if (!optimize_bb_for_speed_p (gimple_bb (stmt
)))
1750 /* See if we can store by pieces into (dst + strlen(dst)). */
1752 tree strlen_fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
1753 tree memcpy_fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
1755 if (!strlen_fn
|| !memcpy_fn
)
1758 /* If the length of the source string isn't computable don't
1759 split strcat into strlen and memcpy. */
1760 tree len
= get_maxval_strlen (src
, 0);
1764 /* Create strlen (dst). */
1765 gimple_seq stmts
= NULL
, stmts2
;
1766 gimple
*repl
= gimple_build_call (strlen_fn
, 1, dst
);
1767 gimple_set_location (repl
, loc
);
1768 newdst
= create_tmp_reg_or_ssa_name (size_type_node
);
1769 gimple_call_set_lhs (repl
, newdst
);
1770 gimple_seq_add_stmt_without_update (&stmts
, repl
);
1772 /* Create (dst p+ strlen (dst)). */
1773 newdst
= fold_build_pointer_plus_loc (loc
, dst
, newdst
);
1774 newdst
= force_gimple_operand (newdst
, &stmts2
, true, NULL_TREE
);
1775 gimple_seq_add_seq_without_update (&stmts
, stmts2
);
1777 len
= fold_convert_loc (loc
, size_type_node
, len
);
1778 len
= size_binop_loc (loc
, PLUS_EXPR
, len
,
1779 build_int_cst (size_type_node
, 1));
1780 len
= force_gimple_operand (len
, &stmts2
, true, NULL_TREE
);
1781 gimple_seq_add_seq_without_update (&stmts
, stmts2
);
1783 repl
= gimple_build_call (memcpy_fn
, 3, newdst
, src
, len
);
1784 gimple_seq_add_stmt_without_update (&stmts
, repl
);
1785 if (gimple_call_lhs (stmt
))
1787 repl
= gimple_build_assign (gimple_call_lhs (stmt
), dst
);
1788 gimple_seq_add_stmt_without_update (&stmts
, repl
);
1789 gsi_replace_with_seq_vops (gsi
, stmts
);
1790 /* gsi now points at the assignment to the lhs, get a
1791 stmt iterator to the memcpy call.
1792 ??? We can't use gsi_for_stmt as that doesn't work when the
1793 CFG isn't built yet. */
1794 gimple_stmt_iterator gsi2
= *gsi
;
1800 gsi_replace_with_seq_vops (gsi
, stmts
);
1806 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
1807 are the arguments to the call. */
1810 gimple_fold_builtin_strcat_chk (gimple_stmt_iterator
*gsi
)
1812 gimple
*stmt
= gsi_stmt (*gsi
);
1813 tree dest
= gimple_call_arg (stmt
, 0);
1814 tree src
= gimple_call_arg (stmt
, 1);
1815 tree size
= gimple_call_arg (stmt
, 2);
1821 /* If the SRC parameter is "", return DEST. */
1822 if (p
&& *p
== '\0')
1824 replace_call_with_value (gsi
, dest
);
1828 if (! tree_fits_uhwi_p (size
) || ! integer_all_onesp (size
))
1831 /* If __builtin_strcat_chk is used, assume strcat is available. */
1832 fn
= builtin_decl_explicit (BUILT_IN_STRCAT
);
1836 gimple
*repl
= gimple_build_call (fn
, 2, dest
, src
);
1837 replace_call_with_call_and_fold (gsi
, repl
);
1841 /* Simplify a call to the strncat builtin. */
1844 gimple_fold_builtin_strncat (gimple_stmt_iterator
*gsi
)
1846 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
1847 tree dst
= gimple_call_arg (stmt
, 0);
1848 tree src
= gimple_call_arg (stmt
, 1);
1849 tree len
= gimple_call_arg (stmt
, 2);
1851 const char *p
= c_getstr (src
);
1853 /* If the requested length is zero, or the src parameter string
1854 length is zero, return the dst parameter. */
1855 if (integer_zerop (len
) || (p
&& *p
== '\0'))
1857 replace_call_with_value (gsi
, dst
);
1861 /* If the requested len is greater than or equal to the string
1862 length, call strcat. */
1863 if (TREE_CODE (len
) == INTEGER_CST
&& p
1864 && compare_tree_int (len
, strlen (p
)) >= 0)
1866 tree fn
= builtin_decl_implicit (BUILT_IN_STRCAT
);
1868 /* If the replacement _DECL isn't initialized, don't do the
1873 gcall
*repl
= gimple_build_call (fn
, 2, dst
, src
);
1874 replace_call_with_call_and_fold (gsi
, repl
);
1881 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
1885 gimple_fold_builtin_strncat_chk (gimple_stmt_iterator
*gsi
)
1887 gimple
*stmt
= gsi_stmt (*gsi
);
1888 tree dest
= gimple_call_arg (stmt
, 0);
1889 tree src
= gimple_call_arg (stmt
, 1);
1890 tree len
= gimple_call_arg (stmt
, 2);
1891 tree size
= gimple_call_arg (stmt
, 3);
1896 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
1897 if ((p
&& *p
== '\0')
1898 || integer_zerop (len
))
1900 replace_call_with_value (gsi
, dest
);
1904 if (! tree_fits_uhwi_p (size
))
1907 if (! integer_all_onesp (size
))
1909 tree src_len
= c_strlen (src
, 1);
1911 && tree_fits_uhwi_p (src_len
)
1912 && tree_fits_uhwi_p (len
)
1913 && ! tree_int_cst_lt (len
, src_len
))
1915 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
1916 fn
= builtin_decl_explicit (BUILT_IN_STRCAT_CHK
);
1920 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, size
);
1921 replace_call_with_call_and_fold (gsi
, repl
);
1927 /* If __builtin_strncat_chk is used, assume strncat is available. */
1928 fn
= builtin_decl_explicit (BUILT_IN_STRNCAT
);
1932 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
1933 replace_call_with_call_and_fold (gsi
, repl
);
1937 /* Build and append gimple statements to STMTS that would load a first
1938 character of a memory location identified by STR. LOC is location
1939 of the statement. */
1942 gimple_load_first_char (location_t loc
, tree str
, gimple_seq
*stmts
)
1946 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
1947 tree cst_uchar_ptr_node
1948 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
1949 tree off0
= build_int_cst (cst_uchar_ptr_node
, 0);
1951 tree temp
= fold_build2_loc (loc
, MEM_REF
, cst_uchar_node
, str
, off0
);
1952 gassign
*stmt
= gimple_build_assign (NULL_TREE
, temp
);
1953 var
= create_tmp_reg_or_ssa_name (cst_uchar_node
, stmt
);
1955 gimple_assign_set_lhs (stmt
, var
);
1956 gimple_seq_add_stmt_without_update (stmts
, stmt
);
1961 /* Fold a call to the str{n}{case}cmp builtin pointed by GSI iterator.
1962 FCODE is the name of the builtin. */
1965 gimple_fold_builtin_string_compare (gimple_stmt_iterator
*gsi
)
1967 gimple
*stmt
= gsi_stmt (*gsi
);
1968 tree callee
= gimple_call_fndecl (stmt
);
1969 enum built_in_function fcode
= DECL_FUNCTION_CODE (callee
);
1971 tree type
= integer_type_node
;
1972 tree str1
= gimple_call_arg (stmt
, 0);
1973 tree str2
= gimple_call_arg (stmt
, 1);
1974 tree lhs
= gimple_call_lhs (stmt
);
1975 HOST_WIDE_INT length
= -1;
1977 /* Handle strncmp and strncasecmp functions. */
1978 if (gimple_call_num_args (stmt
) == 3)
1980 tree len
= gimple_call_arg (stmt
, 2);
1981 if (tree_fits_uhwi_p (len
))
1982 length
= tree_to_uhwi (len
);
1985 /* If the LEN parameter is zero, return zero. */
1988 replace_call_with_value (gsi
, integer_zero_node
);
1992 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
1993 if (operand_equal_p (str1
, str2
, 0))
1995 replace_call_with_value (gsi
, integer_zero_node
);
1999 const char *p1
= c_getstr (str1
);
2000 const char *p2
= c_getstr (str2
);
2002 /* For known strings, return an immediate value. */
2006 bool known_result
= false;
2010 case BUILT_IN_STRCMP
:
2012 r
= strcmp (p1
, p2
);
2013 known_result
= true;
2016 case BUILT_IN_STRNCMP
:
2020 r
= strncmp (p1
, p2
, length
);
2021 known_result
= true;
2024 /* Only handleable situation is where the string are equal (result 0),
2025 which is already handled by operand_equal_p case. */
2026 case BUILT_IN_STRCASECMP
:
2028 case BUILT_IN_STRNCASECMP
:
2032 r
= strncmp (p1
, p2
, length
);
2034 known_result
= true;
2043 replace_call_with_value (gsi
, build_cmp_result (type
, r
));
2048 bool nonzero_length
= length
>= 1
2049 || fcode
== BUILT_IN_STRCMP
2050 || fcode
== BUILT_IN_STRCASECMP
;
2052 location_t loc
= gimple_location (stmt
);
2054 /* If the second arg is "", return *(const unsigned char*)arg1. */
2055 if (p2
&& *p2
== '\0' && nonzero_length
)
2057 gimple_seq stmts
= NULL
;
2058 tree var
= gimple_load_first_char (loc
, str1
, &stmts
);
2061 stmt
= gimple_build_assign (lhs
, NOP_EXPR
, var
);
2062 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2065 gsi_replace_with_seq_vops (gsi
, stmts
);
2069 /* If the first arg is "", return -*(const unsigned char*)arg2. */
2070 if (p1
&& *p1
== '\0' && nonzero_length
)
2072 gimple_seq stmts
= NULL
;
2073 tree var
= gimple_load_first_char (loc
, str2
, &stmts
);
2077 tree c
= create_tmp_reg_or_ssa_name (integer_type_node
);
2078 stmt
= gimple_build_assign (c
, NOP_EXPR
, var
);
2079 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2081 stmt
= gimple_build_assign (lhs
, NEGATE_EXPR
, c
);
2082 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2085 gsi_replace_with_seq_vops (gsi
, stmts
);
2089 /* If len parameter is one, return an expression corresponding to
2090 (*(const unsigned char*)arg2 - *(const unsigned char*)arg1). */
2091 if (fcode
== BUILT_IN_STRNCMP
&& length
== 1)
2093 gimple_seq stmts
= NULL
;
2094 tree temp1
= gimple_load_first_char (loc
, str1
, &stmts
);
2095 tree temp2
= gimple_load_first_char (loc
, str2
, &stmts
);
2099 tree c1
= create_tmp_reg_or_ssa_name (integer_type_node
);
2100 gassign
*convert1
= gimple_build_assign (c1
, NOP_EXPR
, temp1
);
2101 gimple_seq_add_stmt_without_update (&stmts
, convert1
);
2103 tree c2
= create_tmp_reg_or_ssa_name (integer_type_node
);
2104 gassign
*convert2
= gimple_build_assign (c2
, NOP_EXPR
, temp2
);
2105 gimple_seq_add_stmt_without_update (&stmts
, convert2
);
2107 stmt
= gimple_build_assign (lhs
, MINUS_EXPR
, c1
, c2
);
2108 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2111 gsi_replace_with_seq_vops (gsi
, stmts
);
2118 /* Fold a call to the memchr pointed by GSI iterator. */
2121 gimple_fold_builtin_memchr (gimple_stmt_iterator
*gsi
)
2123 gimple
*stmt
= gsi_stmt (*gsi
);
2124 tree lhs
= gimple_call_lhs (stmt
);
2125 tree arg1
= gimple_call_arg (stmt
, 0);
2126 tree arg2
= gimple_call_arg (stmt
, 1);
2127 tree len
= gimple_call_arg (stmt
, 2);
2129 /* If the LEN parameter is zero, return zero. */
2130 if (integer_zerop (len
))
2132 replace_call_with_value (gsi
, build_int_cst (ptr_type_node
, 0));
2137 if (TREE_CODE (arg2
) != INTEGER_CST
2138 || !tree_fits_uhwi_p (len
)
2139 || !target_char_cst_p (arg2
, &c
))
2142 unsigned HOST_WIDE_INT length
= tree_to_uhwi (len
);
2143 unsigned HOST_WIDE_INT string_length
;
2144 const char *p1
= c_getstr (arg1
, &string_length
);
2148 const char *r
= (const char *)memchr (p1
, c
, MIN (length
, string_length
));
2151 if (length
<= string_length
)
2153 replace_call_with_value (gsi
, build_int_cst (ptr_type_node
, 0));
2159 unsigned HOST_WIDE_INT offset
= r
- p1
;
2160 gimple_seq stmts
= NULL
;
2161 if (lhs
!= NULL_TREE
)
2163 tree offset_cst
= build_int_cst (TREE_TYPE (len
), offset
);
2164 gassign
*stmt
= gimple_build_assign (lhs
, POINTER_PLUS_EXPR
,
2166 gimple_seq_add_stmt_without_update (&stmts
, stmt
);
2169 gimple_seq_add_stmt_without_update (&stmts
,
2170 gimple_build_nop ());
2172 gsi_replace_with_seq_vops (gsi
, stmts
);
2180 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
2181 to the call. IGNORE is true if the value returned
2182 by the builtin will be ignored. UNLOCKED is true is true if this
2183 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
2184 the known length of the string. Return NULL_TREE if no simplification
2188 gimple_fold_builtin_fputs (gimple_stmt_iterator
*gsi
,
2189 tree arg0
, tree arg1
,
2192 gimple
*stmt
= gsi_stmt (*gsi
);
2194 /* If we're using an unlocked function, assume the other unlocked
2195 functions exist explicitly. */
2196 tree
const fn_fputc
= (unlocked
2197 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED
)
2198 : builtin_decl_implicit (BUILT_IN_FPUTC
));
2199 tree
const fn_fwrite
= (unlocked
2200 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED
)
2201 : builtin_decl_implicit (BUILT_IN_FWRITE
));
2203 /* If the return value is used, don't do the transformation. */
2204 if (gimple_call_lhs (stmt
))
2207 /* Get the length of the string passed to fputs. If the length
2208 can't be determined, punt. */
2209 tree len
= get_maxval_strlen (arg0
, 0);
2211 || TREE_CODE (len
) != INTEGER_CST
)
2214 switch (compare_tree_int (len
, 1))
2216 case -1: /* length is 0, delete the call entirely . */
2217 replace_call_with_value (gsi
, integer_zero_node
);
2220 case 0: /* length is 1, call fputc. */
2222 const char *p
= c_getstr (arg0
);
2228 gimple
*repl
= gimple_build_call (fn_fputc
, 2,
2230 (integer_type_node
, p
[0]), arg1
);
2231 replace_call_with_call_and_fold (gsi
, repl
);
2236 case 1: /* length is greater than 1, call fwrite. */
2238 /* If optimizing for size keep fputs. */
2239 if (optimize_function_for_size_p (cfun
))
2241 /* New argument list transforming fputs(string, stream) to
2242 fwrite(string, 1, len, stream). */
2246 gimple
*repl
= gimple_build_call (fn_fwrite
, 4, arg0
,
2247 size_one_node
, len
, arg1
);
2248 replace_call_with_call_and_fold (gsi
, repl
);
2257 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
2258 DEST, SRC, LEN, and SIZE are the arguments to the call.
2259 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
2260 code of the builtin. If MAXLEN is not NULL, it is maximum length
2261 passed as third argument. */
2264 gimple_fold_builtin_memory_chk (gimple_stmt_iterator
*gsi
,
2265 tree dest
, tree src
, tree len
, tree size
,
2266 enum built_in_function fcode
)
2268 gimple
*stmt
= gsi_stmt (*gsi
);
2269 location_t loc
= gimple_location (stmt
);
2270 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
2273 /* If SRC and DEST are the same (and not volatile), return DEST
2274 (resp. DEST+LEN for __mempcpy_chk). */
2275 if (fcode
!= BUILT_IN_MEMSET_CHK
&& operand_equal_p (src
, dest
, 0))
2277 if (fcode
!= BUILT_IN_MEMPCPY_CHK
)
2279 replace_call_with_value (gsi
, dest
);
2284 gimple_seq stmts
= NULL
;
2285 len
= gimple_convert_to_ptrofftype (&stmts
, loc
, len
);
2286 tree temp
= gimple_build (&stmts
, loc
, POINTER_PLUS_EXPR
,
2287 TREE_TYPE (dest
), dest
, len
);
2288 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2289 replace_call_with_value (gsi
, temp
);
2294 if (! tree_fits_uhwi_p (size
))
2297 tree maxlen
= get_maxval_strlen (len
, 2);
2298 if (! integer_all_onesp (size
))
2300 if (! tree_fits_uhwi_p (len
))
2302 /* If LEN is not constant, try MAXLEN too.
2303 For MAXLEN only allow optimizing into non-_ocs function
2304 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2305 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
2307 if (fcode
== BUILT_IN_MEMPCPY_CHK
&& ignore
)
2309 /* (void) __mempcpy_chk () can be optimized into
2310 (void) __memcpy_chk (). */
2311 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
2315 gimple
*repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
2316 replace_call_with_call_and_fold (gsi
, repl
);
2325 if (tree_int_cst_lt (size
, maxlen
))
2330 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
2331 mem{cpy,pcpy,move,set} is available. */
2334 case BUILT_IN_MEMCPY_CHK
:
2335 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY
);
2337 case BUILT_IN_MEMPCPY_CHK
:
2338 fn
= builtin_decl_explicit (BUILT_IN_MEMPCPY
);
2340 case BUILT_IN_MEMMOVE_CHK
:
2341 fn
= builtin_decl_explicit (BUILT_IN_MEMMOVE
);
2343 case BUILT_IN_MEMSET_CHK
:
2344 fn
= builtin_decl_explicit (BUILT_IN_MEMSET
);
2353 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
2354 replace_call_with_call_and_fold (gsi
, repl
);
2358 /* Fold a call to the __st[rp]cpy_chk builtin.
2359 DEST, SRC, and SIZE are the arguments to the call.
2360 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
2361 code of the builtin. If MAXLEN is not NULL, it is maximum length of
2362 strings passed as second argument. */
2365 gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator
*gsi
,
2367 tree src
, tree size
,
2368 enum built_in_function fcode
)
2370 gimple
*stmt
= gsi_stmt (*gsi
);
2371 location_t loc
= gimple_location (stmt
);
2372 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
2375 /* If SRC and DEST are the same (and not volatile), return DEST. */
2376 if (fcode
== BUILT_IN_STRCPY_CHK
&& operand_equal_p (src
, dest
, 0))
2378 replace_call_with_value (gsi
, dest
);
2382 if (! tree_fits_uhwi_p (size
))
2385 tree maxlen
= get_maxval_strlen (src
, 1);
2386 if (! integer_all_onesp (size
))
2388 len
= c_strlen (src
, 1);
2389 if (! len
|| ! tree_fits_uhwi_p (len
))
2391 /* If LEN is not constant, try MAXLEN too.
2392 For MAXLEN only allow optimizing into non-_ocs function
2393 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2394 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
2396 if (fcode
== BUILT_IN_STPCPY_CHK
)
2401 /* If return value of __stpcpy_chk is ignored,
2402 optimize into __strcpy_chk. */
2403 fn
= builtin_decl_explicit (BUILT_IN_STRCPY_CHK
);
2407 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, size
);
2408 replace_call_with_call_and_fold (gsi
, repl
);
2412 if (! len
|| TREE_SIDE_EFFECTS (len
))
2415 /* If c_strlen returned something, but not a constant,
2416 transform __strcpy_chk into __memcpy_chk. */
2417 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
2421 gimple_seq stmts
= NULL
;
2422 len
= gimple_convert (&stmts
, loc
, size_type_node
, len
);
2423 len
= gimple_build (&stmts
, loc
, PLUS_EXPR
, size_type_node
, len
,
2424 build_int_cst (size_type_node
, 1));
2425 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2426 gimple
*repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
2427 replace_call_with_call_and_fold (gsi
, repl
);
2434 if (! tree_int_cst_lt (maxlen
, size
))
2438 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
2439 fn
= builtin_decl_explicit (fcode
== BUILT_IN_STPCPY_CHK
2440 ? BUILT_IN_STPCPY
: BUILT_IN_STRCPY
);
2444 gimple
*repl
= gimple_build_call (fn
, 2, dest
, src
);
2445 replace_call_with_call_and_fold (gsi
, repl
);
2449 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
2450 are the arguments to the call. If MAXLEN is not NULL, it is maximum
2451 length passed as third argument. IGNORE is true if return value can be
2452 ignored. FCODE is the BUILT_IN_* code of the builtin. */
2455 gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator
*gsi
,
2456 tree dest
, tree src
,
2457 tree len
, tree size
,
2458 enum built_in_function fcode
)
2460 gimple
*stmt
= gsi_stmt (*gsi
);
2461 bool ignore
= gimple_call_lhs (stmt
) == NULL_TREE
;
2464 if (fcode
== BUILT_IN_STPNCPY_CHK
&& ignore
)
2466 /* If return value of __stpncpy_chk is ignored,
2467 optimize into __strncpy_chk. */
2468 fn
= builtin_decl_explicit (BUILT_IN_STRNCPY_CHK
);
2471 gimple
*repl
= gimple_build_call (fn
, 4, dest
, src
, len
, size
);
2472 replace_call_with_call_and_fold (gsi
, repl
);
2477 if (! tree_fits_uhwi_p (size
))
2480 tree maxlen
= get_maxval_strlen (len
, 2);
2481 if (! integer_all_onesp (size
))
2483 if (! tree_fits_uhwi_p (len
))
2485 /* If LEN is not constant, try MAXLEN too.
2486 For MAXLEN only allow optimizing into non-_ocs function
2487 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2488 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
2494 if (tree_int_cst_lt (size
, maxlen
))
2498 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
2499 fn
= builtin_decl_explicit (fcode
== BUILT_IN_STPNCPY_CHK
2500 ? BUILT_IN_STPNCPY
: BUILT_IN_STRNCPY
);
2504 gimple
*repl
= gimple_build_call (fn
, 3, dest
, src
, len
);
2505 replace_call_with_call_and_fold (gsi
, repl
);
2509 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
2510 Return NULL_TREE if no simplification can be made. */
2513 gimple_fold_builtin_stpcpy (gimple_stmt_iterator
*gsi
)
2515 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
2516 location_t loc
= gimple_location (stmt
);
2517 tree dest
= gimple_call_arg (stmt
, 0);
2518 tree src
= gimple_call_arg (stmt
, 1);
2519 tree fn
, len
, lenp1
;
2521 /* If the result is unused, replace stpcpy with strcpy. */
2522 if (gimple_call_lhs (stmt
) == NULL_TREE
)
2524 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
2527 gimple_call_set_fndecl (stmt
, fn
);
2532 len
= c_strlen (src
, 1);
2534 || TREE_CODE (len
) != INTEGER_CST
)
2537 if (optimize_function_for_size_p (cfun
)
2538 /* If length is zero it's small enough. */
2539 && !integer_zerop (len
))
2542 /* If the source has a known length replace stpcpy with memcpy. */
2543 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
2547 gimple_seq stmts
= NULL
;
2548 tree tem
= gimple_convert (&stmts
, loc
, size_type_node
, len
);
2549 lenp1
= gimple_build (&stmts
, loc
, PLUS_EXPR
, size_type_node
,
2550 tem
, build_int_cst (size_type_node
, 1));
2551 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2552 gcall
*repl
= gimple_build_call (fn
, 3, dest
, src
, lenp1
);
2553 gimple_set_vuse (repl
, gimple_vuse (stmt
));
2554 gimple_set_vdef (repl
, gimple_vdef (stmt
));
2555 if (gimple_vdef (repl
)
2556 && TREE_CODE (gimple_vdef (repl
)) == SSA_NAME
)
2557 SSA_NAME_DEF_STMT (gimple_vdef (repl
)) = repl
;
2558 gsi_insert_before (gsi
, repl
, GSI_SAME_STMT
);
2559 /* Replace the result with dest + len. */
2561 tem
= gimple_convert (&stmts
, loc
, sizetype
, len
);
2562 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2563 gassign
*ret
= gimple_build_assign (gimple_call_lhs (stmt
),
2564 POINTER_PLUS_EXPR
, dest
, tem
);
2565 gsi_replace (gsi
, ret
, false);
2566 /* Finally fold the memcpy call. */
2567 gimple_stmt_iterator gsi2
= *gsi
;
2573 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
2574 NULL_TREE if a normal call should be emitted rather than expanding
2575 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
2576 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
2577 passed as second argument. */
2580 gimple_fold_builtin_snprintf_chk (gimple_stmt_iterator
*gsi
,
2581 enum built_in_function fcode
)
2583 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
2584 tree dest
, size
, len
, fn
, fmt
, flag
;
2585 const char *fmt_str
;
2587 /* Verify the required arguments in the original call. */
2588 if (gimple_call_num_args (stmt
) < 5)
2591 dest
= gimple_call_arg (stmt
, 0);
2592 len
= gimple_call_arg (stmt
, 1);
2593 flag
= gimple_call_arg (stmt
, 2);
2594 size
= gimple_call_arg (stmt
, 3);
2595 fmt
= gimple_call_arg (stmt
, 4);
2597 if (! tree_fits_uhwi_p (size
))
2600 if (! integer_all_onesp (size
))
2602 tree maxlen
= get_maxval_strlen (len
, 2);
2603 if (! tree_fits_uhwi_p (len
))
2605 /* If LEN is not constant, try MAXLEN too.
2606 For MAXLEN only allow optimizing into non-_ocs function
2607 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2608 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
2614 if (tree_int_cst_lt (size
, maxlen
))
2618 if (!init_target_chars ())
2621 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
2622 or if format doesn't contain % chars or is "%s". */
2623 if (! integer_zerop (flag
))
2625 fmt_str
= c_getstr (fmt
);
2626 if (fmt_str
== NULL
)
2628 if (strchr (fmt_str
, target_percent
) != NULL
2629 && strcmp (fmt_str
, target_percent_s
))
2633 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
2635 fn
= builtin_decl_explicit (fcode
== BUILT_IN_VSNPRINTF_CHK
2636 ? BUILT_IN_VSNPRINTF
: BUILT_IN_SNPRINTF
);
2640 /* Replace the called function and the first 5 argument by 3 retaining
2641 trailing varargs. */
2642 gimple_call_set_fndecl (stmt
, fn
);
2643 gimple_call_set_fntype (stmt
, TREE_TYPE (fn
));
2644 gimple_call_set_arg (stmt
, 0, dest
);
2645 gimple_call_set_arg (stmt
, 1, len
);
2646 gimple_call_set_arg (stmt
, 2, fmt
);
2647 for (unsigned i
= 3; i
< gimple_call_num_args (stmt
) - 2; ++i
)
2648 gimple_call_set_arg (stmt
, i
, gimple_call_arg (stmt
, i
+ 2));
2649 gimple_set_num_ops (stmt
, gimple_num_ops (stmt
) - 2);
2654 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
2655 Return NULL_TREE if a normal call should be emitted rather than
2656 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
2657 or BUILT_IN_VSPRINTF_CHK. */
2660 gimple_fold_builtin_sprintf_chk (gimple_stmt_iterator
*gsi
,
2661 enum built_in_function fcode
)
2663 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
2664 tree dest
, size
, len
, fn
, fmt
, flag
;
2665 const char *fmt_str
;
2666 unsigned nargs
= gimple_call_num_args (stmt
);
2668 /* Verify the required arguments in the original call. */
2671 dest
= gimple_call_arg (stmt
, 0);
2672 flag
= gimple_call_arg (stmt
, 1);
2673 size
= gimple_call_arg (stmt
, 2);
2674 fmt
= gimple_call_arg (stmt
, 3);
2676 if (! tree_fits_uhwi_p (size
))
2681 if (!init_target_chars ())
2684 /* Check whether the format is a literal string constant. */
2685 fmt_str
= c_getstr (fmt
);
2686 if (fmt_str
!= NULL
)
2688 /* If the format doesn't contain % args or %%, we know the size. */
2689 if (strchr (fmt_str
, target_percent
) == 0)
2691 if (fcode
!= BUILT_IN_SPRINTF_CHK
|| nargs
== 4)
2692 len
= build_int_cstu (size_type_node
, strlen (fmt_str
));
2694 /* If the format is "%s" and first ... argument is a string literal,
2695 we know the size too. */
2696 else if (fcode
== BUILT_IN_SPRINTF_CHK
2697 && strcmp (fmt_str
, target_percent_s
) == 0)
2703 arg
= gimple_call_arg (stmt
, 4);
2704 if (POINTER_TYPE_P (TREE_TYPE (arg
)))
2706 len
= c_strlen (arg
, 1);
2707 if (! len
|| ! tree_fits_uhwi_p (len
))
2714 if (! integer_all_onesp (size
))
2716 if (! len
|| ! tree_int_cst_lt (len
, size
))
2720 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
2721 or if format doesn't contain % chars or is "%s". */
2722 if (! integer_zerop (flag
))
2724 if (fmt_str
== NULL
)
2726 if (strchr (fmt_str
, target_percent
) != NULL
2727 && strcmp (fmt_str
, target_percent_s
))
2731 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
2732 fn
= builtin_decl_explicit (fcode
== BUILT_IN_VSPRINTF_CHK
2733 ? BUILT_IN_VSPRINTF
: BUILT_IN_SPRINTF
);
2737 /* Replace the called function and the first 4 argument by 2 retaining
2738 trailing varargs. */
2739 gimple_call_set_fndecl (stmt
, fn
);
2740 gimple_call_set_fntype (stmt
, TREE_TYPE (fn
));
2741 gimple_call_set_arg (stmt
, 0, dest
);
2742 gimple_call_set_arg (stmt
, 1, fmt
);
2743 for (unsigned i
= 2; i
< gimple_call_num_args (stmt
) - 2; ++i
)
2744 gimple_call_set_arg (stmt
, i
, gimple_call_arg (stmt
, i
+ 2));
2745 gimple_set_num_ops (stmt
, gimple_num_ops (stmt
) - 2);
2750 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
2751 ORIG may be null if this is a 2-argument call. We don't attempt to
2752 simplify calls with more than 3 arguments.
2754 Return true if simplification was possible, otherwise false. */
2757 gimple_fold_builtin_sprintf (gimple_stmt_iterator
*gsi
)
2759 gimple
*stmt
= gsi_stmt (*gsi
);
2760 tree dest
= gimple_call_arg (stmt
, 0);
2761 tree fmt
= gimple_call_arg (stmt
, 1);
2762 tree orig
= NULL_TREE
;
2763 const char *fmt_str
= NULL
;
2765 /* Verify the required arguments in the original call. We deal with two
2766 types of sprintf() calls: 'sprintf (str, fmt)' and
2767 'sprintf (dest, "%s", orig)'. */
2768 if (gimple_call_num_args (stmt
) > 3)
2771 if (gimple_call_num_args (stmt
) == 3)
2772 orig
= gimple_call_arg (stmt
, 2);
2774 /* Check whether the format is a literal string constant. */
2775 fmt_str
= c_getstr (fmt
);
2776 if (fmt_str
== NULL
)
2779 if (!init_target_chars ())
2782 /* If the format doesn't contain % args or %%, use strcpy. */
2783 if (strchr (fmt_str
, target_percent
) == NULL
)
2785 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
2790 /* Don't optimize sprintf (buf, "abc", ptr++). */
2794 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
2795 'format' is known to contain no % formats. */
2796 gimple_seq stmts
= NULL
;
2797 gimple
*repl
= gimple_build_call (fn
, 2, dest
, fmt
);
2798 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2799 if (gimple_call_lhs (stmt
))
2801 repl
= gimple_build_assign (gimple_call_lhs (stmt
),
2802 build_int_cst (integer_type_node
,
2804 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2805 gsi_replace_with_seq_vops (gsi
, stmts
);
2806 /* gsi now points at the assignment to the lhs, get a
2807 stmt iterator to the memcpy call.
2808 ??? We can't use gsi_for_stmt as that doesn't work when the
2809 CFG isn't built yet. */
2810 gimple_stmt_iterator gsi2
= *gsi
;
2816 gsi_replace_with_seq_vops (gsi
, stmts
);
2822 /* If the format is "%s", use strcpy if the result isn't used. */
2823 else if (fmt_str
&& strcmp (fmt_str
, target_percent_s
) == 0)
2826 fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
2831 /* Don't crash on sprintf (str1, "%s"). */
2835 tree orig_len
= NULL_TREE
;
2836 if (gimple_call_lhs (stmt
))
2838 orig_len
= get_maxval_strlen (orig
, 0);
2843 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
2844 gimple_seq stmts
= NULL
;
2845 gimple
*repl
= gimple_build_call (fn
, 2, dest
, orig
);
2846 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2847 if (gimple_call_lhs (stmt
))
2849 if (!useless_type_conversion_p (integer_type_node
,
2850 TREE_TYPE (orig_len
)))
2851 orig_len
= fold_convert (integer_type_node
, orig_len
);
2852 repl
= gimple_build_assign (gimple_call_lhs (stmt
), orig_len
);
2853 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2854 gsi_replace_with_seq_vops (gsi
, stmts
);
2855 /* gsi now points at the assignment to the lhs, get a
2856 stmt iterator to the memcpy call.
2857 ??? We can't use gsi_for_stmt as that doesn't work when the
2858 CFG isn't built yet. */
2859 gimple_stmt_iterator gsi2
= *gsi
;
2865 gsi_replace_with_seq_vops (gsi
, stmts
);
2873 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
2874 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
2875 attempt to simplify calls with more than 4 arguments.
2877 Return true if simplification was possible, otherwise false. */
2880 gimple_fold_builtin_snprintf (gimple_stmt_iterator
*gsi
)
2882 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
2883 tree dest
= gimple_call_arg (stmt
, 0);
2884 tree destsize
= gimple_call_arg (stmt
, 1);
2885 tree fmt
= gimple_call_arg (stmt
, 2);
2886 tree orig
= NULL_TREE
;
2887 const char *fmt_str
= NULL
;
2889 if (gimple_call_num_args (stmt
) > 4)
2892 if (gimple_call_num_args (stmt
) == 4)
2893 orig
= gimple_call_arg (stmt
, 3);
2895 if (!tree_fits_uhwi_p (destsize
))
2897 unsigned HOST_WIDE_INT destlen
= tree_to_uhwi (destsize
);
2899 /* Check whether the format is a literal string constant. */
2900 fmt_str
= c_getstr (fmt
);
2901 if (fmt_str
== NULL
)
2904 if (!init_target_chars ())
2907 /* If the format doesn't contain % args or %%, use strcpy. */
2908 if (strchr (fmt_str
, target_percent
) == NULL
)
2910 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
2914 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
2918 /* We could expand this as
2919 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
2921 memcpy (str, fmt_with_nul_at_cstm1, cst);
2922 but in the former case that might increase code size
2923 and in the latter case grow .rodata section too much.
2925 size_t len
= strlen (fmt_str
);
2929 gimple_seq stmts
= NULL
;
2930 gimple
*repl
= gimple_build_call (fn
, 2, dest
, fmt
);
2931 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2932 if (gimple_call_lhs (stmt
))
2934 repl
= gimple_build_assign (gimple_call_lhs (stmt
),
2935 build_int_cst (integer_type_node
, len
));
2936 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2937 gsi_replace_with_seq_vops (gsi
, stmts
);
2938 /* gsi now points at the assignment to the lhs, get a
2939 stmt iterator to the memcpy call.
2940 ??? We can't use gsi_for_stmt as that doesn't work when the
2941 CFG isn't built yet. */
2942 gimple_stmt_iterator gsi2
= *gsi
;
2948 gsi_replace_with_seq_vops (gsi
, stmts
);
2954 /* If the format is "%s", use strcpy if the result isn't used. */
2955 else if (fmt_str
&& strcmp (fmt_str
, target_percent_s
) == 0)
2957 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
2961 /* Don't crash on snprintf (str1, cst, "%s"). */
2965 tree orig_len
= get_maxval_strlen (orig
, 0);
2966 if (!orig_len
|| TREE_CODE (orig_len
) != INTEGER_CST
)
2969 /* We could expand this as
2970 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
2972 memcpy (str1, str2_with_nul_at_cstm1, cst);
2973 but in the former case that might increase code size
2974 and in the latter case grow .rodata section too much.
2976 if (compare_tree_int (orig_len
, destlen
) >= 0)
2979 /* Convert snprintf (str1, cst, "%s", str2) into
2980 strcpy (str1, str2) if strlen (str2) < cst. */
2981 gimple_seq stmts
= NULL
;
2982 gimple
*repl
= gimple_build_call (fn
, 2, dest
, orig
);
2983 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2984 if (gimple_call_lhs (stmt
))
2986 if (!useless_type_conversion_p (integer_type_node
,
2987 TREE_TYPE (orig_len
)))
2988 orig_len
= fold_convert (integer_type_node
, orig_len
);
2989 repl
= gimple_build_assign (gimple_call_lhs (stmt
), orig_len
);
2990 gimple_seq_add_stmt_without_update (&stmts
, repl
);
2991 gsi_replace_with_seq_vops (gsi
, stmts
);
2992 /* gsi now points at the assignment to the lhs, get a
2993 stmt iterator to the memcpy call.
2994 ??? We can't use gsi_for_stmt as that doesn't work when the
2995 CFG isn't built yet. */
2996 gimple_stmt_iterator gsi2
= *gsi
;
3002 gsi_replace_with_seq_vops (gsi
, stmts
);
3010 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
3011 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
3012 more than 3 arguments, and ARG may be null in the 2-argument case.
3014 Return NULL_TREE if no simplification was possible, otherwise return the
3015 simplified form of the call as a tree. FCODE is the BUILT_IN_*
3016 code of the function to be simplified. */
3019 gimple_fold_builtin_fprintf (gimple_stmt_iterator
*gsi
,
3020 tree fp
, tree fmt
, tree arg
,
3021 enum built_in_function fcode
)
3023 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3024 tree fn_fputc
, fn_fputs
;
3025 const char *fmt_str
= NULL
;
3027 /* If the return value is used, don't do the transformation. */
3028 if (gimple_call_lhs (stmt
) != NULL_TREE
)
3031 /* Check whether the format is a literal string constant. */
3032 fmt_str
= c_getstr (fmt
);
3033 if (fmt_str
== NULL
)
3036 if (fcode
== BUILT_IN_FPRINTF_UNLOCKED
)
3038 /* If we're using an unlocked function, assume the other
3039 unlocked functions exist explicitly. */
3040 fn_fputc
= builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED
);
3041 fn_fputs
= builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED
);
3045 fn_fputc
= builtin_decl_implicit (BUILT_IN_FPUTC
);
3046 fn_fputs
= builtin_decl_implicit (BUILT_IN_FPUTS
);
3049 if (!init_target_chars ())
3052 /* If the format doesn't contain % args or %%, use strcpy. */
3053 if (strchr (fmt_str
, target_percent
) == NULL
)
3055 if (fcode
!= BUILT_IN_VFPRINTF
&& fcode
!= BUILT_IN_VFPRINTF_CHK
3059 /* If the format specifier was "", fprintf does nothing. */
3060 if (fmt_str
[0] == '\0')
3062 replace_call_with_value (gsi
, NULL_TREE
);
3066 /* When "string" doesn't contain %, replace all cases of
3067 fprintf (fp, string) with fputs (string, fp). The fputs
3068 builtin will take care of special cases like length == 1. */
3071 gcall
*repl
= gimple_build_call (fn_fputs
, 2, fmt
, fp
);
3072 replace_call_with_call_and_fold (gsi
, repl
);
3077 /* The other optimizations can be done only on the non-va_list variants. */
3078 else if (fcode
== BUILT_IN_VFPRINTF
|| fcode
== BUILT_IN_VFPRINTF_CHK
)
3081 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
3082 else if (strcmp (fmt_str
, target_percent_s
) == 0)
3084 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
3088 gcall
*repl
= gimple_build_call (fn_fputs
, 2, arg
, fp
);
3089 replace_call_with_call_and_fold (gsi
, repl
);
3094 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
3095 else if (strcmp (fmt_str
, target_percent_c
) == 0)
3098 || ! useless_type_conversion_p (integer_type_node
, TREE_TYPE (arg
)))
3102 gcall
*repl
= gimple_build_call (fn_fputc
, 2, arg
, fp
);
3103 replace_call_with_call_and_fold (gsi
, repl
);
3111 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
3112 FMT and ARG are the arguments to the call; we don't fold cases with
3113 more than 2 arguments, and ARG may be null if this is a 1-argument case.
3115 Return NULL_TREE if no simplification was possible, otherwise return the
3116 simplified form of the call as a tree. FCODE is the BUILT_IN_*
3117 code of the function to be simplified. */
3120 gimple_fold_builtin_printf (gimple_stmt_iterator
*gsi
, tree fmt
,
3121 tree arg
, enum built_in_function fcode
)
3123 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3124 tree fn_putchar
, fn_puts
, newarg
;
3125 const char *fmt_str
= NULL
;
3127 /* If the return value is used, don't do the transformation. */
3128 if (gimple_call_lhs (stmt
) != NULL_TREE
)
3131 /* Check whether the format is a literal string constant. */
3132 fmt_str
= c_getstr (fmt
);
3133 if (fmt_str
== NULL
)
3136 if (fcode
== BUILT_IN_PRINTF_UNLOCKED
)
3138 /* If we're using an unlocked function, assume the other
3139 unlocked functions exist explicitly. */
3140 fn_putchar
= builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED
);
3141 fn_puts
= builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED
);
3145 fn_putchar
= builtin_decl_implicit (BUILT_IN_PUTCHAR
);
3146 fn_puts
= builtin_decl_implicit (BUILT_IN_PUTS
);
3149 if (!init_target_chars ())
3152 if (strcmp (fmt_str
, target_percent_s
) == 0
3153 || strchr (fmt_str
, target_percent
) == NULL
)
3157 if (strcmp (fmt_str
, target_percent_s
) == 0)
3159 if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
3162 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
3165 str
= c_getstr (arg
);
3171 /* The format specifier doesn't contain any '%' characters. */
3172 if (fcode
!= BUILT_IN_VPRINTF
&& fcode
!= BUILT_IN_VPRINTF_CHK
3178 /* If the string was "", printf does nothing. */
3181 replace_call_with_value (gsi
, NULL_TREE
);
3185 /* If the string has length of 1, call putchar. */
3188 /* Given printf("c"), (where c is any one character,)
3189 convert "c"[0] to an int and pass that to the replacement
3191 newarg
= build_int_cst (integer_type_node
, str
[0]);
3194 gcall
*repl
= gimple_build_call (fn_putchar
, 1, newarg
);
3195 replace_call_with_call_and_fold (gsi
, repl
);
3201 /* If the string was "string\n", call puts("string"). */
3202 size_t len
= strlen (str
);
3203 if ((unsigned char)str
[len
- 1] == target_newline
3204 && (size_t) (int) len
== len
3208 tree offset_node
, string_cst
;
3210 /* Create a NUL-terminated string that's one char shorter
3211 than the original, stripping off the trailing '\n'. */
3212 newarg
= build_string_literal (len
, str
);
3213 string_cst
= string_constant (newarg
, &offset_node
);
3214 gcc_checking_assert (string_cst
3215 && (TREE_STRING_LENGTH (string_cst
)
3217 && integer_zerop (offset_node
)
3219 TREE_STRING_POINTER (string_cst
)[len
- 1]
3221 /* build_string_literal creates a new STRING_CST,
3222 modify it in place to avoid double copying. */
3223 newstr
= CONST_CAST (char *, TREE_STRING_POINTER (string_cst
));
3224 newstr
[len
- 1] = '\0';
3227 gcall
*repl
= gimple_build_call (fn_puts
, 1, newarg
);
3228 replace_call_with_call_and_fold (gsi
, repl
);
3233 /* We'd like to arrange to call fputs(string,stdout) here,
3234 but we need stdout and don't have a way to get it yet. */
3239 /* The other optimizations can be done only on the non-va_list variants. */
3240 else if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
3243 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
3244 else if (strcmp (fmt_str
, target_percent_s_newline
) == 0)
3246 if (!arg
|| ! POINTER_TYPE_P (TREE_TYPE (arg
)))
3250 gcall
*repl
= gimple_build_call (fn_puts
, 1, arg
);
3251 replace_call_with_call_and_fold (gsi
, repl
);
3256 /* If the format specifier was "%c", call __builtin_putchar(arg). */
3257 else if (strcmp (fmt_str
, target_percent_c
) == 0)
3259 if (!arg
|| ! useless_type_conversion_p (integer_type_node
,
3264 gcall
*repl
= gimple_build_call (fn_putchar
, 1, arg
);
3265 replace_call_with_call_and_fold (gsi
, repl
);
3275 /* Fold a call to __builtin_strlen with known length LEN. */
3278 gimple_fold_builtin_strlen (gimple_stmt_iterator
*gsi
)
3280 gimple
*stmt
= gsi_stmt (*gsi
);
3281 tree len
= get_maxval_strlen (gimple_call_arg (stmt
, 0), 0);
3284 len
= force_gimple_operand_gsi (gsi
, len
, true, NULL
, true, GSI_SAME_STMT
);
3285 replace_call_with_value (gsi
, len
);
3289 /* Fold a call to __builtin_acc_on_device. */
3292 gimple_fold_builtin_acc_on_device (gimple_stmt_iterator
*gsi
, tree arg0
)
3294 /* Defer folding until we know which compiler we're in. */
3295 if (symtab
->state
!= EXPANSION
)
3298 unsigned val_host
= GOMP_DEVICE_HOST
;
3299 unsigned val_dev
= GOMP_DEVICE_NONE
;
3301 #ifdef ACCEL_COMPILER
3302 val_host
= GOMP_DEVICE_NOT_HOST
;
3303 val_dev
= ACCEL_COMPILER_acc_device
;
3306 location_t loc
= gimple_location (gsi_stmt (*gsi
));
3308 tree host_eq
= make_ssa_name (boolean_type_node
);
3309 gimple
*host_ass
= gimple_build_assign
3310 (host_eq
, EQ_EXPR
, arg0
, build_int_cst (TREE_TYPE (arg0
), val_host
));
3311 gimple_set_location (host_ass
, loc
);
3312 gsi_insert_before (gsi
, host_ass
, GSI_SAME_STMT
);
3314 tree dev_eq
= make_ssa_name (boolean_type_node
);
3315 gimple
*dev_ass
= gimple_build_assign
3316 (dev_eq
, EQ_EXPR
, arg0
, build_int_cst (TREE_TYPE (arg0
), val_dev
));
3317 gimple_set_location (dev_ass
, loc
);
3318 gsi_insert_before (gsi
, dev_ass
, GSI_SAME_STMT
);
3320 tree result
= make_ssa_name (boolean_type_node
);
3321 gimple
*result_ass
= gimple_build_assign
3322 (result
, BIT_IOR_EXPR
, host_eq
, dev_eq
);
3323 gimple_set_location (result_ass
, loc
);
3324 gsi_insert_before (gsi
, result_ass
, GSI_SAME_STMT
);
3326 replace_call_with_value (gsi
, result
);
3331 /* Fold realloc (0, n) -> malloc (n). */
3334 gimple_fold_builtin_realloc (gimple_stmt_iterator
*gsi
)
3336 gimple
*stmt
= gsi_stmt (*gsi
);
3337 tree arg
= gimple_call_arg (stmt
, 0);
3338 tree size
= gimple_call_arg (stmt
, 1);
3340 if (operand_equal_p (arg
, null_pointer_node
, 0))
3342 tree fn_malloc
= builtin_decl_implicit (BUILT_IN_MALLOC
);
3345 gcall
*repl
= gimple_build_call (fn_malloc
, 1, size
);
3346 replace_call_with_call_and_fold (gsi
, repl
);
3353 /* Fold the non-target builtin at *GSI and return whether any simplification
3357 gimple_fold_builtin (gimple_stmt_iterator
*gsi
)
3359 gcall
*stmt
= as_a
<gcall
*>(gsi_stmt (*gsi
));
3360 tree callee
= gimple_call_fndecl (stmt
);
3362 /* Give up for always_inline inline builtins until they are
3364 if (avoid_folding_inline_builtin (callee
))
3367 unsigned n
= gimple_call_num_args (stmt
);
3368 enum built_in_function fcode
= DECL_FUNCTION_CODE (callee
);
3372 return gimple_fold_builtin_bcmp (gsi
);
3373 case BUILT_IN_BCOPY
:
3374 return gimple_fold_builtin_bcopy (gsi
);
3375 case BUILT_IN_BZERO
:
3376 return gimple_fold_builtin_bzero (gsi
);
3378 case BUILT_IN_MEMSET
:
3379 return gimple_fold_builtin_memset (gsi
,
3380 gimple_call_arg (stmt
, 1),
3381 gimple_call_arg (stmt
, 2));
3382 case BUILT_IN_MEMCPY
:
3383 return gimple_fold_builtin_memory_op (gsi
, gimple_call_arg (stmt
, 0),
3384 gimple_call_arg (stmt
, 1), 0);
3385 case BUILT_IN_MEMPCPY
:
3386 return gimple_fold_builtin_memory_op (gsi
, gimple_call_arg (stmt
, 0),
3387 gimple_call_arg (stmt
, 1), 1);
3388 case BUILT_IN_MEMMOVE
:
3389 return gimple_fold_builtin_memory_op (gsi
, gimple_call_arg (stmt
, 0),
3390 gimple_call_arg (stmt
, 1), 3);
3391 case BUILT_IN_SPRINTF_CHK
:
3392 case BUILT_IN_VSPRINTF_CHK
:
3393 return gimple_fold_builtin_sprintf_chk (gsi
, fcode
);
3394 case BUILT_IN_STRCAT_CHK
:
3395 return gimple_fold_builtin_strcat_chk (gsi
);
3396 case BUILT_IN_STRNCAT_CHK
:
3397 return gimple_fold_builtin_strncat_chk (gsi
);
3398 case BUILT_IN_STRLEN
:
3399 return gimple_fold_builtin_strlen (gsi
);
3400 case BUILT_IN_STRCPY
:
3401 return gimple_fold_builtin_strcpy (gsi
,
3402 gimple_call_arg (stmt
, 0),
3403 gimple_call_arg (stmt
, 1));
3404 case BUILT_IN_STRNCPY
:
3405 return gimple_fold_builtin_strncpy (gsi
,
3406 gimple_call_arg (stmt
, 0),
3407 gimple_call_arg (stmt
, 1),
3408 gimple_call_arg (stmt
, 2));
3409 case BUILT_IN_STRCAT
:
3410 return gimple_fold_builtin_strcat (gsi
, gimple_call_arg (stmt
, 0),
3411 gimple_call_arg (stmt
, 1));
3412 case BUILT_IN_STRNCAT
:
3413 return gimple_fold_builtin_strncat (gsi
);
3414 case BUILT_IN_INDEX
:
3415 case BUILT_IN_STRCHR
:
3416 return gimple_fold_builtin_strchr (gsi
, false);
3417 case BUILT_IN_RINDEX
:
3418 case BUILT_IN_STRRCHR
:
3419 return gimple_fold_builtin_strchr (gsi
, true);
3420 case BUILT_IN_STRSTR
:
3421 return gimple_fold_builtin_strstr (gsi
);
3422 case BUILT_IN_STRCMP
:
3423 case BUILT_IN_STRCASECMP
:
3424 case BUILT_IN_STRNCMP
:
3425 case BUILT_IN_STRNCASECMP
:
3426 return gimple_fold_builtin_string_compare (gsi
);
3427 case BUILT_IN_MEMCHR
:
3428 return gimple_fold_builtin_memchr (gsi
);
3429 case BUILT_IN_FPUTS
:
3430 return gimple_fold_builtin_fputs (gsi
, gimple_call_arg (stmt
, 0),
3431 gimple_call_arg (stmt
, 1), false);
3432 case BUILT_IN_FPUTS_UNLOCKED
:
3433 return gimple_fold_builtin_fputs (gsi
, gimple_call_arg (stmt
, 0),
3434 gimple_call_arg (stmt
, 1), true);
3435 case BUILT_IN_MEMCPY_CHK
:
3436 case BUILT_IN_MEMPCPY_CHK
:
3437 case BUILT_IN_MEMMOVE_CHK
:
3438 case BUILT_IN_MEMSET_CHK
:
3439 return gimple_fold_builtin_memory_chk (gsi
,
3440 gimple_call_arg (stmt
, 0),
3441 gimple_call_arg (stmt
, 1),
3442 gimple_call_arg (stmt
, 2),
3443 gimple_call_arg (stmt
, 3),
3445 case BUILT_IN_STPCPY
:
3446 return gimple_fold_builtin_stpcpy (gsi
);
3447 case BUILT_IN_STRCPY_CHK
:
3448 case BUILT_IN_STPCPY_CHK
:
3449 return gimple_fold_builtin_stxcpy_chk (gsi
,
3450 gimple_call_arg (stmt
, 0),
3451 gimple_call_arg (stmt
, 1),
3452 gimple_call_arg (stmt
, 2),
3454 case BUILT_IN_STRNCPY_CHK
:
3455 case BUILT_IN_STPNCPY_CHK
:
3456 return gimple_fold_builtin_stxncpy_chk (gsi
,
3457 gimple_call_arg (stmt
, 0),
3458 gimple_call_arg (stmt
, 1),
3459 gimple_call_arg (stmt
, 2),
3460 gimple_call_arg (stmt
, 3),
3462 case BUILT_IN_SNPRINTF_CHK
:
3463 case BUILT_IN_VSNPRINTF_CHK
:
3464 return gimple_fold_builtin_snprintf_chk (gsi
, fcode
);
3466 case BUILT_IN_FPRINTF
:
3467 case BUILT_IN_FPRINTF_UNLOCKED
:
3468 case BUILT_IN_VFPRINTF
:
3469 if (n
== 2 || n
== 3)
3470 return gimple_fold_builtin_fprintf (gsi
,
3471 gimple_call_arg (stmt
, 0),
3472 gimple_call_arg (stmt
, 1),
3474 ? gimple_call_arg (stmt
, 2)
3478 case BUILT_IN_FPRINTF_CHK
:
3479 case BUILT_IN_VFPRINTF_CHK
:
3480 if (n
== 3 || n
== 4)
3481 return gimple_fold_builtin_fprintf (gsi
,
3482 gimple_call_arg (stmt
, 0),
3483 gimple_call_arg (stmt
, 2),
3485 ? gimple_call_arg (stmt
, 3)
3489 case BUILT_IN_PRINTF
:
3490 case BUILT_IN_PRINTF_UNLOCKED
:
3491 case BUILT_IN_VPRINTF
:
3492 if (n
== 1 || n
== 2)
3493 return gimple_fold_builtin_printf (gsi
, gimple_call_arg (stmt
, 0),
3495 ? gimple_call_arg (stmt
, 1)
3496 : NULL_TREE
, fcode
);
3498 case BUILT_IN_PRINTF_CHK
:
3499 case BUILT_IN_VPRINTF_CHK
:
3500 if (n
== 2 || n
== 3)
3501 return gimple_fold_builtin_printf (gsi
, gimple_call_arg (stmt
, 1),
3503 ? gimple_call_arg (stmt
, 2)
3504 : NULL_TREE
, fcode
);
3506 case BUILT_IN_ACC_ON_DEVICE
:
3507 return gimple_fold_builtin_acc_on_device (gsi
,
3508 gimple_call_arg (stmt
, 0));
3509 case BUILT_IN_REALLOC
:
3510 return gimple_fold_builtin_realloc (gsi
);
3515 /* Try the generic builtin folder. */
3516 bool ignore
= (gimple_call_lhs (stmt
) == NULL
);
3517 tree result
= fold_call_stmt (stmt
, ignore
);
3521 STRIP_NOPS (result
);
3523 result
= fold_convert (gimple_call_return_type (stmt
), result
);
3524 if (!update_call_from_tree (gsi
, result
))
3525 gimplify_and_update_call_from_tree (gsi
, result
);
3532 /* Transform IFN_GOACC_DIM_SIZE and IFN_GOACC_DIM_POS internal
3533 function calls to constants, where possible. */
3536 fold_internal_goacc_dim (const gimple
*call
)
3538 int axis
= oacc_get_ifn_dim_arg (call
);
3539 int size
= oacc_get_fn_dim_size (current_function_decl
, axis
);
3540 bool is_pos
= gimple_call_internal_fn (call
) == IFN_GOACC_DIM_POS
;
3541 tree result
= NULL_TREE
;
3543 /* If the size is 1, or we only want the size and it is not dynamic,
3544 we know the answer. */
3545 if (size
== 1 || (!is_pos
&& size
))
3547 tree type
= TREE_TYPE (gimple_call_lhs (call
));
3548 result
= build_int_cst (type
, size
- is_pos
);
3554 /* Return true if stmt is __atomic_compare_exchange_N call which is suitable
3555 for conversion into ATOMIC_COMPARE_EXCHANGE if the second argument is
3556 &var where var is only addressable because of such calls. */
3559 optimize_atomic_compare_exchange_p (gimple
*stmt
)
3561 if (gimple_call_num_args (stmt
) != 6
3562 || !flag_inline_atomics
3564 || sanitize_flags_p (SANITIZE_THREAD
| SANITIZE_ADDRESS
)
3565 || !gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
)
3566 || !gimple_vdef (stmt
)
3567 || !gimple_vuse (stmt
))
3570 tree fndecl
= gimple_call_fndecl (stmt
);
3571 switch (DECL_FUNCTION_CODE (fndecl
))
3573 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
:
3574 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2
:
3575 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4
:
3576 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8
:
3577 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16
:
3583 tree expected
= gimple_call_arg (stmt
, 1);
3584 if (TREE_CODE (expected
) != ADDR_EXPR
3585 || !SSA_VAR_P (TREE_OPERAND (expected
, 0)))
3588 tree etype
= TREE_TYPE (TREE_OPERAND (expected
, 0));
3589 if (!is_gimple_reg_type (etype
)
3590 || !auto_var_in_fn_p (TREE_OPERAND (expected
, 0), current_function_decl
)
3591 || TREE_THIS_VOLATILE (etype
)
3592 || VECTOR_TYPE_P (etype
)
3593 || TREE_CODE (etype
) == COMPLEX_TYPE
3594 /* Don't optimize floating point expected vars, VIEW_CONVERT_EXPRs
3595 might not preserve all the bits. See PR71716. */
3596 || SCALAR_FLOAT_TYPE_P (etype
)
3597 || TYPE_PRECISION (etype
) != GET_MODE_BITSIZE (TYPE_MODE (etype
)))
3600 tree weak
= gimple_call_arg (stmt
, 3);
3601 if (!integer_zerop (weak
) && !integer_onep (weak
))
3604 tree parmt
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
3605 tree itype
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt
)));
3606 machine_mode mode
= TYPE_MODE (itype
);
3608 if (direct_optab_handler (atomic_compare_and_swap_optab
, mode
)
3610 && optab_handler (sync_compare_and_swap_optab
, mode
) == CODE_FOR_nothing
)
3613 if (int_size_in_bytes (etype
) != GET_MODE_SIZE (mode
))
3620 r = __atomic_compare_exchange_N (p, &e, d, w, s, f);
3622 _Complex uintN_t t = ATOMIC_COMPARE_EXCHANGE (p, e, d, w * 256 + N, s, f);
3623 i = IMAGPART_EXPR <t>;
3625 e = REALPART_EXPR <t>; */
3628 fold_builtin_atomic_compare_exchange (gimple_stmt_iterator
*gsi
)
3630 gimple
*stmt
= gsi_stmt (*gsi
);
3631 tree fndecl
= gimple_call_fndecl (stmt
);
3632 tree parmt
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
3633 tree itype
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt
)));
3634 tree ctype
= build_complex_type (itype
);
3635 tree expected
= TREE_OPERAND (gimple_call_arg (stmt
, 1), 0);
3636 bool throws
= false;
3638 gimple
*g
= gimple_build_assign (make_ssa_name (TREE_TYPE (expected
)),
3640 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
3641 gimple_stmt_iterator gsiret
= gsi_for_stmt (g
);
3642 if (!useless_type_conversion_p (itype
, TREE_TYPE (expected
)))
3644 g
= gimple_build_assign (make_ssa_name (itype
), VIEW_CONVERT_EXPR
,
3645 build1 (VIEW_CONVERT_EXPR
, itype
,
3646 gimple_assign_lhs (g
)));
3647 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
3649 int flag
= (integer_onep (gimple_call_arg (stmt
, 3)) ? 256 : 0)
3650 + int_size_in_bytes (itype
);
3651 g
= gimple_build_call_internal (IFN_ATOMIC_COMPARE_EXCHANGE
, 6,
3652 gimple_call_arg (stmt
, 0),
3653 gimple_assign_lhs (g
),
3654 gimple_call_arg (stmt
, 2),
3655 build_int_cst (integer_type_node
, flag
),
3656 gimple_call_arg (stmt
, 4),
3657 gimple_call_arg (stmt
, 5));
3658 tree lhs
= make_ssa_name (ctype
);
3659 gimple_call_set_lhs (g
, lhs
);
3660 gimple_set_vdef (g
, gimple_vdef (stmt
));
3661 gimple_set_vuse (g
, gimple_vuse (stmt
));
3662 SSA_NAME_DEF_STMT (gimple_vdef (g
)) = g
;
3663 tree oldlhs
= gimple_call_lhs (stmt
);
3664 if (stmt_can_throw_internal (stmt
))
3667 e
= find_fallthru_edge (gsi_bb (*gsi
)->succs
);
3669 gimple_call_set_nothrow (as_a
<gcall
*> (g
),
3670 gimple_call_nothrow_p (as_a
<gcall
*> (stmt
)));
3671 gimple_call_set_lhs (stmt
, NULL_TREE
);
3672 gsi_replace (gsi
, g
, true);
3675 g
= gimple_build_assign (make_ssa_name (itype
), IMAGPART_EXPR
,
3676 build1 (IMAGPART_EXPR
, itype
, lhs
));
3679 gsi_insert_on_edge_immediate (e
, g
);
3680 *gsi
= gsi_for_stmt (g
);
3683 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
3684 g
= gimple_build_assign (oldlhs
, NOP_EXPR
, gimple_assign_lhs (g
));
3685 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
3687 g
= gimple_build_assign (make_ssa_name (itype
), REALPART_EXPR
,
3688 build1 (REALPART_EXPR
, itype
, lhs
));
3689 if (throws
&& oldlhs
== NULL_TREE
)
3691 gsi_insert_on_edge_immediate (e
, g
);
3692 *gsi
= gsi_for_stmt (g
);
3695 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
3696 if (!useless_type_conversion_p (TREE_TYPE (expected
), itype
))
3698 g
= gimple_build_assign (make_ssa_name (TREE_TYPE (expected
)),
3700 build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (expected
),
3701 gimple_assign_lhs (g
)));
3702 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
3704 g
= gimple_build_assign (expected
, SSA_NAME
, gimple_assign_lhs (g
));
3705 gsi_insert_after (gsi
, g
, GSI_NEW_STMT
);
3709 /* Return true if ARG0 CODE ARG1 in infinite signed precision operation
3710 doesn't fit into TYPE. The test for overflow should be regardless of
3711 -fwrapv, and even for unsigned types. */
3714 arith_overflowed_p (enum tree_code code
, const_tree type
,
3715 const_tree arg0
, const_tree arg1
)
3717 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
* 2) widest2_int
;
3718 typedef generic_wide_int
<wi::extended_tree
<WIDE_INT_MAX_PRECISION
* 2> >
3720 widest2_int warg0
= widest2_int_cst (arg0
);
3721 widest2_int warg1
= widest2_int_cst (arg1
);
3725 case PLUS_EXPR
: wres
= wi::add (warg0
, warg1
); break;
3726 case MINUS_EXPR
: wres
= wi::sub (warg0
, warg1
); break;
3727 case MULT_EXPR
: wres
= wi::mul (warg0
, warg1
); break;
3728 default: gcc_unreachable ();
3730 signop sign
= TYPE_SIGN (type
);
3731 if (sign
== UNSIGNED
&& wi::neg_p (wres
))
3733 return wi::min_precision (wres
, sign
) > TYPE_PRECISION (type
);
3736 /* Attempt to fold a call statement referenced by the statement iterator GSI.
3737 The statement may be replaced by another statement, e.g., if the call
3738 simplifies to a constant value. Return true if any changes were made.
3739 It is assumed that the operands have been previously folded. */
3742 gimple_fold_call (gimple_stmt_iterator
*gsi
, bool inplace
)
3744 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
3746 bool changed
= false;
3749 /* Fold *& in call arguments. */
3750 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
3751 if (REFERENCE_CLASS_P (gimple_call_arg (stmt
, i
)))
3753 tree tmp
= maybe_fold_reference (gimple_call_arg (stmt
, i
), false);
3756 gimple_call_set_arg (stmt
, i
, tmp
);
3761 /* Check for virtual calls that became direct calls. */
3762 callee
= gimple_call_fn (stmt
);
3763 if (callee
&& TREE_CODE (callee
) == OBJ_TYPE_REF
)
3765 if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee
)) != NULL_TREE
)
3767 if (dump_file
&& virtual_method_call_p (callee
)
3768 && !possible_polymorphic_call_target_p
3769 (callee
, stmt
, cgraph_node::get (gimple_call_addr_fndecl
3770 (OBJ_TYPE_REF_EXPR (callee
)))))
3773 "Type inheritance inconsistent devirtualization of ");
3774 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
3775 fprintf (dump_file
, " to ");
3776 print_generic_expr (dump_file
, callee
, TDF_SLIM
);
3777 fprintf (dump_file
, "\n");
3780 gimple_call_set_fn (stmt
, OBJ_TYPE_REF_EXPR (callee
));
3783 else if (flag_devirtualize
&& !inplace
&& virtual_method_call_p (callee
))
3786 vec
<cgraph_node
*>targets
3787 = possible_polymorphic_call_targets (callee
, stmt
, &final
);
3788 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
3790 tree lhs
= gimple_call_lhs (stmt
);
3791 if (dump_enabled_p ())
3793 location_t loc
= gimple_location_safe (stmt
);
3794 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, loc
,
3795 "folding virtual function call to %s\n",
3796 targets
.length () == 1
3797 ? targets
[0]->name ()
3798 : "__builtin_unreachable");
3800 if (targets
.length () == 1)
3802 tree fndecl
= targets
[0]->decl
;
3803 gimple_call_set_fndecl (stmt
, fndecl
);
3805 /* If changing the call to __cxa_pure_virtual
3806 or similar noreturn function, adjust gimple_call_fntype
3808 if (gimple_call_noreturn_p (stmt
)
3809 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl
)))
3810 && TYPE_ARG_TYPES (TREE_TYPE (fndecl
))
3811 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)))
3813 gimple_call_set_fntype (stmt
, TREE_TYPE (fndecl
));
3814 /* If the call becomes noreturn, remove the lhs. */
3816 && gimple_call_noreturn_p (stmt
)
3817 && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (stmt
)))
3818 || should_remove_lhs_p (lhs
)))
3820 if (TREE_CODE (lhs
) == SSA_NAME
)
3822 tree var
= create_tmp_var (TREE_TYPE (lhs
));
3823 tree def
= get_or_create_ssa_default_def (cfun
, var
);
3824 gimple
*new_stmt
= gimple_build_assign (lhs
, def
);
3825 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
3827 gimple_call_set_lhs (stmt
, NULL_TREE
);
3829 maybe_remove_unused_call_args (cfun
, stmt
);
3833 tree fndecl
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
3834 gimple
*new_stmt
= gimple_build_call (fndecl
, 0);
3835 gimple_set_location (new_stmt
, gimple_location (stmt
));
3836 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
3838 tree var
= create_tmp_var (TREE_TYPE (lhs
));
3839 tree def
= get_or_create_ssa_default_def (cfun
, var
);
3841 /* To satisfy condition for
3842 cgraph_update_edges_for_call_stmt_node,
3843 we need to preserve GIMPLE_CALL statement
3844 at position of GSI iterator. */
3845 update_call_from_tree (gsi
, def
);
3846 gsi_insert_before (gsi
, new_stmt
, GSI_NEW_STMT
);
3850 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
3851 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
3852 gsi_replace (gsi
, new_stmt
, false);
3860 /* Check for indirect calls that became direct calls, and then
3861 no longer require a static chain. */
3862 if (gimple_call_chain (stmt
))
3864 tree fn
= gimple_call_fndecl (stmt
);
3865 if (fn
&& !DECL_STATIC_CHAIN (fn
))
3867 gimple_call_set_chain (stmt
, NULL
);
3872 tree tmp
= maybe_fold_reference (gimple_call_chain (stmt
), false);
3875 gimple_call_set_chain (stmt
, tmp
);
3884 /* Check for builtins that CCP can handle using information not
3885 available in the generic fold routines. */
3886 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
3888 if (gimple_fold_builtin (gsi
))
3891 else if (gimple_call_builtin_p (stmt
, BUILT_IN_MD
))
3893 changed
|= targetm
.gimple_fold_builtin (gsi
);
3895 else if (gimple_call_internal_p (stmt
))
3897 enum tree_code subcode
= ERROR_MARK
;
3898 tree result
= NULL_TREE
;
3899 bool cplx_result
= false;
3900 tree overflow
= NULL_TREE
;
3901 switch (gimple_call_internal_fn (stmt
))
3903 case IFN_BUILTIN_EXPECT
:
3904 result
= fold_builtin_expect (gimple_location (stmt
),
3905 gimple_call_arg (stmt
, 0),
3906 gimple_call_arg (stmt
, 1),
3907 gimple_call_arg (stmt
, 2));
3909 case IFN_UBSAN_OBJECT_SIZE
:
3910 if (integer_all_onesp (gimple_call_arg (stmt
, 2))
3911 || (TREE_CODE (gimple_call_arg (stmt
, 1)) == INTEGER_CST
3912 && TREE_CODE (gimple_call_arg (stmt
, 2)) == INTEGER_CST
3913 && tree_int_cst_le (gimple_call_arg (stmt
, 1),
3914 gimple_call_arg (stmt
, 2))))
3916 gsi_replace (gsi
, gimple_build_nop (), false);
3917 unlink_stmt_vdef (stmt
);
3918 release_defs (stmt
);
3922 case IFN_GOACC_DIM_SIZE
:
3923 case IFN_GOACC_DIM_POS
:
3924 result
= fold_internal_goacc_dim (stmt
);
3926 case IFN_UBSAN_CHECK_ADD
:
3927 subcode
= PLUS_EXPR
;
3929 case IFN_UBSAN_CHECK_SUB
:
3930 subcode
= MINUS_EXPR
;
3932 case IFN_UBSAN_CHECK_MUL
:
3933 subcode
= MULT_EXPR
;
3935 case IFN_ADD_OVERFLOW
:
3936 subcode
= PLUS_EXPR
;
3939 case IFN_SUB_OVERFLOW
:
3940 subcode
= MINUS_EXPR
;
3943 case IFN_MUL_OVERFLOW
:
3944 subcode
= MULT_EXPR
;
3950 if (subcode
!= ERROR_MARK
)
3952 tree arg0
= gimple_call_arg (stmt
, 0);
3953 tree arg1
= gimple_call_arg (stmt
, 1);
3954 tree type
= TREE_TYPE (arg0
);
3957 tree lhs
= gimple_call_lhs (stmt
);
3958 if (lhs
== NULL_TREE
)
3961 type
= TREE_TYPE (TREE_TYPE (lhs
));
3963 if (type
== NULL_TREE
)
3965 /* x = y + 0; x = y - 0; x = y * 0; */
3966 else if (integer_zerop (arg1
))
3967 result
= subcode
== MULT_EXPR
? integer_zero_node
: arg0
;
3968 /* x = 0 + y; x = 0 * y; */
3969 else if (subcode
!= MINUS_EXPR
&& integer_zerop (arg0
))
3970 result
= subcode
== MULT_EXPR
? integer_zero_node
: arg1
;
3972 else if (subcode
== MINUS_EXPR
&& operand_equal_p (arg0
, arg1
, 0))
3973 result
= integer_zero_node
;
3974 /* x = y * 1; x = 1 * y; */
3975 else if (subcode
== MULT_EXPR
&& integer_onep (arg1
))
3977 else if (subcode
== MULT_EXPR
&& integer_onep (arg0
))
3979 else if (TREE_CODE (arg0
) == INTEGER_CST
3980 && TREE_CODE (arg1
) == INTEGER_CST
)
3983 result
= int_const_binop (subcode
, fold_convert (type
, arg0
),
3984 fold_convert (type
, arg1
));
3986 result
= int_const_binop (subcode
, arg0
, arg1
);
3987 if (result
&& arith_overflowed_p (subcode
, type
, arg0
, arg1
))
3990 overflow
= build_one_cst (type
);
3997 if (result
== integer_zero_node
)
3998 result
= build_zero_cst (type
);
3999 else if (cplx_result
&& TREE_TYPE (result
) != type
)
4001 if (TREE_CODE (result
) == INTEGER_CST
)
4003 if (arith_overflowed_p (PLUS_EXPR
, type
, result
,
4005 overflow
= build_one_cst (type
);
4007 else if ((!TYPE_UNSIGNED (TREE_TYPE (result
))
4008 && TYPE_UNSIGNED (type
))
4009 || (TYPE_PRECISION (type
)
4010 < (TYPE_PRECISION (TREE_TYPE (result
))
4011 + (TYPE_UNSIGNED (TREE_TYPE (result
))
4012 && !TYPE_UNSIGNED (type
)))))
4015 result
= fold_convert (type
, result
);
4022 if (TREE_CODE (result
) == INTEGER_CST
&& TREE_OVERFLOW (result
))
4023 result
= drop_tree_overflow (result
);
4026 if (overflow
== NULL_TREE
)
4027 overflow
= build_zero_cst (TREE_TYPE (result
));
4028 tree ctype
= build_complex_type (TREE_TYPE (result
));
4029 if (TREE_CODE (result
) == INTEGER_CST
4030 && TREE_CODE (overflow
) == INTEGER_CST
)
4031 result
= build_complex (ctype
, result
, overflow
);
4033 result
= build2_loc (gimple_location (stmt
), COMPLEX_EXPR
,
4034 ctype
, result
, overflow
);
4036 if (!update_call_from_tree (gsi
, result
))
4037 gimplify_and_update_call_from_tree (gsi
, result
);
4046 /* Return true whether NAME has a use on STMT. */
4049 has_use_on_stmt (tree name
, gimple
*stmt
)
4051 imm_use_iterator iter
;
4052 use_operand_p use_p
;
4053 FOR_EACH_IMM_USE_FAST (use_p
, iter
, name
)
4054 if (USE_STMT (use_p
) == stmt
)
4059 /* Worker for fold_stmt_1 dispatch to pattern based folding with
4062 Replaces *GSI with the simplification result in RCODE and OPS
4063 and the associated statements in *SEQ. Does the replacement
4064 according to INPLACE and returns true if the operation succeeded. */
4067 replace_stmt_with_simplification (gimple_stmt_iterator
*gsi
,
4068 code_helper rcode
, tree
*ops
,
4069 gimple_seq
*seq
, bool inplace
)
4071 gimple
*stmt
= gsi_stmt (*gsi
);
4073 /* Play safe and do not allow abnormals to be mentioned in
4074 newly created statements. See also maybe_push_res_to_seq.
4075 As an exception allow such uses if there was a use of the
4076 same SSA name on the old stmt. */
4077 if ((TREE_CODE (ops
[0]) == SSA_NAME
4078 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[0])
4079 && !has_use_on_stmt (ops
[0], stmt
))
4081 && TREE_CODE (ops
[1]) == SSA_NAME
4082 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[1])
4083 && !has_use_on_stmt (ops
[1], stmt
))
4085 && TREE_CODE (ops
[2]) == SSA_NAME
4086 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops
[2])
4087 && !has_use_on_stmt (ops
[2], stmt
))
4088 || (COMPARISON_CLASS_P (ops
[0])
4089 && ((TREE_CODE (TREE_OPERAND (ops
[0], 0)) == SSA_NAME
4090 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops
[0], 0))
4091 && !has_use_on_stmt (TREE_OPERAND (ops
[0], 0), stmt
))
4092 || (TREE_CODE (TREE_OPERAND (ops
[0], 1)) == SSA_NAME
4093 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops
[0], 1))
4094 && !has_use_on_stmt (TREE_OPERAND (ops
[0], 1), stmt
)))))
4097 /* Don't insert new statements when INPLACE is true, even if we could
4098 reuse STMT for the final statement. */
4099 if (inplace
&& !gimple_seq_empty_p (*seq
))
4102 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
4104 gcc_assert (rcode
.is_tree_code ());
4105 if (TREE_CODE_CLASS ((enum tree_code
)rcode
) == tcc_comparison
4106 /* GIMPLE_CONDs condition may not throw. */
4107 && (!flag_exceptions
4108 || !cfun
->can_throw_non_call_exceptions
4109 || !operation_could_trap_p (rcode
,
4110 FLOAT_TYPE_P (TREE_TYPE (ops
[0])),
4112 gimple_cond_set_condition (cond_stmt
, rcode
, ops
[0], ops
[1]);
4113 else if (rcode
== SSA_NAME
)
4114 gimple_cond_set_condition (cond_stmt
, NE_EXPR
, ops
[0],
4115 build_zero_cst (TREE_TYPE (ops
[0])));
4116 else if (rcode
== INTEGER_CST
)
4118 if (integer_zerop (ops
[0]))
4119 gimple_cond_make_false (cond_stmt
);
4121 gimple_cond_make_true (cond_stmt
);
4125 tree res
= maybe_push_res_to_seq (rcode
, boolean_type_node
,
4129 gimple_cond_set_condition (cond_stmt
, NE_EXPR
, res
,
4130 build_zero_cst (TREE_TYPE (res
)));
4134 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4136 fprintf (dump_file
, "gimple_simplified to ");
4137 if (!gimple_seq_empty_p (*seq
))
4138 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
4139 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
),
4142 gsi_insert_seq_before (gsi
, *seq
, GSI_SAME_STMT
);
4145 else if (is_gimple_assign (stmt
)
4146 && rcode
.is_tree_code ())
4149 || gimple_num_ops (stmt
) > get_gimple_rhs_num_ops (rcode
))
4151 maybe_build_generic_op (rcode
,
4152 TREE_TYPE (gimple_assign_lhs (stmt
)), ops
);
4153 gimple_assign_set_rhs_with_ops (gsi
, rcode
, ops
[0], ops
[1], ops
[2]);
4154 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4156 fprintf (dump_file
, "gimple_simplified to ");
4157 if (!gimple_seq_empty_p (*seq
))
4158 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
4159 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
),
4162 gsi_insert_seq_before (gsi
, *seq
, GSI_SAME_STMT
);
4166 else if (rcode
.is_fn_code ()
4167 && gimple_call_combined_fn (stmt
) == rcode
)
4170 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
4172 gcc_assert (ops
[i
] != NULL_TREE
);
4173 gimple_call_set_arg (stmt
, i
, ops
[i
]);
4176 gcc_assert (ops
[i
] == NULL_TREE
);
4177 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4179 fprintf (dump_file
, "gimple_simplified to ");
4180 if (!gimple_seq_empty_p (*seq
))
4181 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
4182 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
), 0, TDF_SLIM
);
4184 gsi_insert_seq_before (gsi
, *seq
, GSI_SAME_STMT
);
4189 if (gimple_has_lhs (stmt
))
4191 tree lhs
= gimple_get_lhs (stmt
);
4192 if (!maybe_push_res_to_seq (rcode
, TREE_TYPE (lhs
),
4195 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4197 fprintf (dump_file
, "gimple_simplified to ");
4198 print_gimple_seq (dump_file
, *seq
, 0, TDF_SLIM
);
4200 gsi_replace_with_seq_vops (gsi
, *seq
);
4210 /* Canonicalize MEM_REFs invariant address operand after propagation. */
4213 maybe_canonicalize_mem_ref_addr (tree
*t
)
4217 if (TREE_CODE (*t
) == ADDR_EXPR
)
4218 t
= &TREE_OPERAND (*t
, 0);
4220 /* The C and C++ frontends use an ARRAY_REF for indexing with their
4221 generic vector extension. The actual vector referenced is
4222 view-converted to an array type for this purpose. If the index
4223 is constant the canonical representation in the middle-end is a
4224 BIT_FIELD_REF so re-write the former to the latter here. */
4225 if (TREE_CODE (*t
) == ARRAY_REF
4226 && TREE_CODE (TREE_OPERAND (*t
, 0)) == VIEW_CONVERT_EXPR
4227 && TREE_CODE (TREE_OPERAND (*t
, 1)) == INTEGER_CST
4228 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t
, 0), 0))))
4230 tree vtype
= TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t
, 0), 0));
4231 if (VECTOR_TYPE_P (vtype
))
4233 tree low
= array_ref_low_bound (*t
);
4234 if (TREE_CODE (low
) == INTEGER_CST
)
4236 if (tree_int_cst_le (low
, TREE_OPERAND (*t
, 1)))
4238 widest_int idx
= wi::sub (wi::to_widest (TREE_OPERAND (*t
, 1)),
4239 wi::to_widest (low
));
4240 idx
= wi::mul (idx
, wi::to_widest
4241 (TYPE_SIZE (TREE_TYPE (*t
))));
4243 = wi::add (idx
, wi::to_widest (TYPE_SIZE (TREE_TYPE (*t
))));
4244 if (wi::les_p (ext
, wi::to_widest (TYPE_SIZE (vtype
))))
4246 *t
= build3_loc (EXPR_LOCATION (*t
), BIT_FIELD_REF
,
4248 TREE_OPERAND (TREE_OPERAND (*t
, 0), 0),
4249 TYPE_SIZE (TREE_TYPE (*t
)),
4250 wide_int_to_tree (bitsizetype
, idx
));
4258 while (handled_component_p (*t
))
4259 t
= &TREE_OPERAND (*t
, 0);
4261 /* Canonicalize MEM [&foo.bar, 0] which appears after propagating
4262 of invariant addresses into a SSA name MEM_REF address. */
4263 if (TREE_CODE (*t
) == MEM_REF
4264 || TREE_CODE (*t
) == TARGET_MEM_REF
)
4266 tree addr
= TREE_OPERAND (*t
, 0);
4267 if (TREE_CODE (addr
) == ADDR_EXPR
4268 && (TREE_CODE (TREE_OPERAND (addr
, 0)) == MEM_REF
4269 || handled_component_p (TREE_OPERAND (addr
, 0))))
4272 HOST_WIDE_INT coffset
;
4273 base
= get_addr_base_and_unit_offset (TREE_OPERAND (addr
, 0),
4278 TREE_OPERAND (*t
, 0) = build_fold_addr_expr (base
);
4279 TREE_OPERAND (*t
, 1) = int_const_binop (PLUS_EXPR
,
4280 TREE_OPERAND (*t
, 1),
4281 size_int (coffset
));
4284 gcc_checking_assert (TREE_CODE (TREE_OPERAND (*t
, 0)) == DEBUG_EXPR_DECL
4285 || is_gimple_mem_ref_addr (TREE_OPERAND (*t
, 0)));
4288 /* Canonicalize back MEM_REFs to plain reference trees if the object
4289 accessed is a decl that has the same access semantics as the MEM_REF. */
4290 if (TREE_CODE (*t
) == MEM_REF
4291 && TREE_CODE (TREE_OPERAND (*t
, 0)) == ADDR_EXPR
4292 && integer_zerop (TREE_OPERAND (*t
, 1))
4293 && MR_DEPENDENCE_CLIQUE (*t
) == 0)
4295 tree decl
= TREE_OPERAND (TREE_OPERAND (*t
, 0), 0);
4296 tree alias_type
= TREE_TYPE (TREE_OPERAND (*t
, 1));
4297 if (/* Same volatile qualification. */
4298 TREE_THIS_VOLATILE (*t
) == TREE_THIS_VOLATILE (decl
)
4299 /* Same TBAA behavior with -fstrict-aliasing. */
4300 && !TYPE_REF_CAN_ALIAS_ALL (alias_type
)
4301 && (TYPE_MAIN_VARIANT (TREE_TYPE (decl
))
4302 == TYPE_MAIN_VARIANT (TREE_TYPE (alias_type
)))
4303 /* Same alignment. */
4304 && TYPE_ALIGN (TREE_TYPE (decl
)) == TYPE_ALIGN (TREE_TYPE (*t
))
4305 /* We have to look out here to not drop a required conversion
4306 from the rhs to the lhs if *t appears on the lhs or vice-versa
4307 if it appears on the rhs. Thus require strict type
4309 && types_compatible_p (TREE_TYPE (*t
), TREE_TYPE (decl
)))
4311 *t
= TREE_OPERAND (TREE_OPERAND (*t
, 0), 0);
4316 /* Canonicalize TARGET_MEM_REF in particular with respect to
4317 the indexes becoming constant. */
4318 else if (TREE_CODE (*t
) == TARGET_MEM_REF
)
4320 tree tem
= maybe_fold_tmr (*t
);
4331 /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument
4332 distinguishes both cases. */
4335 fold_stmt_1 (gimple_stmt_iterator
*gsi
, bool inplace
, tree (*valueize
) (tree
))
4337 bool changed
= false;
4338 gimple
*stmt
= gsi_stmt (*gsi
);
4339 bool nowarning
= gimple_no_warning_p (stmt
);
4341 fold_defer_overflow_warnings ();
4343 /* First do required canonicalization of [TARGET_]MEM_REF addresses
4345 ??? This shouldn't be done in generic folding but in the
4346 propagation helpers which also know whether an address was
4348 Also canonicalize operand order. */
4349 switch (gimple_code (stmt
))
4352 if (gimple_assign_rhs_class (stmt
) == GIMPLE_SINGLE_RHS
)
4354 tree
*rhs
= gimple_assign_rhs1_ptr (stmt
);
4355 if ((REFERENCE_CLASS_P (*rhs
)
4356 || TREE_CODE (*rhs
) == ADDR_EXPR
)
4357 && maybe_canonicalize_mem_ref_addr (rhs
))
4359 tree
*lhs
= gimple_assign_lhs_ptr (stmt
);
4360 if (REFERENCE_CLASS_P (*lhs
)
4361 && maybe_canonicalize_mem_ref_addr (lhs
))
4366 /* Canonicalize operand order. */
4367 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4368 if (TREE_CODE_CLASS (code
) == tcc_comparison
4369 || commutative_tree_code (code
)
4370 || commutative_ternary_tree_code (code
))
4372 tree rhs1
= gimple_assign_rhs1 (stmt
);
4373 tree rhs2
= gimple_assign_rhs2 (stmt
);
4374 if (tree_swap_operands_p (rhs1
, rhs2
))
4376 gimple_assign_set_rhs1 (stmt
, rhs2
);
4377 gimple_assign_set_rhs2 (stmt
, rhs1
);
4378 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4379 gimple_assign_set_rhs_code (stmt
,
4380 swap_tree_comparison (code
));
4388 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
4390 tree
*arg
= gimple_call_arg_ptr (stmt
, i
);
4391 if (REFERENCE_CLASS_P (*arg
)
4392 && maybe_canonicalize_mem_ref_addr (arg
))
4395 tree
*lhs
= gimple_call_lhs_ptr (stmt
);
4397 && REFERENCE_CLASS_P (*lhs
)
4398 && maybe_canonicalize_mem_ref_addr (lhs
))
4404 gasm
*asm_stmt
= as_a
<gasm
*> (stmt
);
4405 for (i
= 0; i
< gimple_asm_noutputs (asm_stmt
); ++i
)
4407 tree link
= gimple_asm_output_op (asm_stmt
, i
);
4408 tree op
= TREE_VALUE (link
);
4409 if (REFERENCE_CLASS_P (op
)
4410 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link
)))
4413 for (i
= 0; i
< gimple_asm_ninputs (asm_stmt
); ++i
)
4415 tree link
= gimple_asm_input_op (asm_stmt
, i
);
4416 tree op
= TREE_VALUE (link
);
4417 if ((REFERENCE_CLASS_P (op
)
4418 || TREE_CODE (op
) == ADDR_EXPR
)
4419 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link
)))
4425 if (gimple_debug_bind_p (stmt
))
4427 tree
*val
= gimple_debug_bind_get_value_ptr (stmt
);
4429 && (REFERENCE_CLASS_P (*val
)
4430 || TREE_CODE (*val
) == ADDR_EXPR
)
4431 && maybe_canonicalize_mem_ref_addr (val
))
4437 /* Canonicalize operand order. */
4438 tree lhs
= gimple_cond_lhs (stmt
);
4439 tree rhs
= gimple_cond_rhs (stmt
);
4440 if (tree_swap_operands_p (lhs
, rhs
))
4442 gcond
*gc
= as_a
<gcond
*> (stmt
);
4443 gimple_cond_set_lhs (gc
, rhs
);
4444 gimple_cond_set_rhs (gc
, lhs
);
4445 gimple_cond_set_code (gc
,
4446 swap_tree_comparison (gimple_cond_code (gc
)));
4453 /* Dispatch to pattern-based folding. */
4455 || is_gimple_assign (stmt
)
4456 || gimple_code (stmt
) == GIMPLE_COND
)
4458 gimple_seq seq
= NULL
;
4461 if (gimple_simplify (stmt
, &rcode
, ops
, inplace
? NULL
: &seq
,
4462 valueize
, valueize
))
4464 if (replace_stmt_with_simplification (gsi
, rcode
, ops
, &seq
, inplace
))
4467 gimple_seq_discard (seq
);
4471 stmt
= gsi_stmt (*gsi
);
4473 /* Fold the main computation performed by the statement. */
4474 switch (gimple_code (stmt
))
4478 /* Try to canonicalize for boolean-typed X the comparisons
4479 X == 0, X == 1, X != 0, and X != 1. */
4480 if (gimple_assign_rhs_code (stmt
) == EQ_EXPR
4481 || gimple_assign_rhs_code (stmt
) == NE_EXPR
)
4483 tree lhs
= gimple_assign_lhs (stmt
);
4484 tree op1
= gimple_assign_rhs1 (stmt
);
4485 tree op2
= gimple_assign_rhs2 (stmt
);
4486 tree type
= TREE_TYPE (op1
);
4488 /* Check whether the comparison operands are of the same boolean
4489 type as the result type is.
4490 Check that second operand is an integer-constant with value
4492 if (TREE_CODE (op2
) == INTEGER_CST
4493 && (integer_zerop (op2
) || integer_onep (op2
))
4494 && useless_type_conversion_p (TREE_TYPE (lhs
), type
))
4496 enum tree_code cmp_code
= gimple_assign_rhs_code (stmt
);
4497 bool is_logical_not
= false;
4499 /* X == 0 and X != 1 is a logical-not.of X
4500 X == 1 and X != 0 is X */
4501 if ((cmp_code
== EQ_EXPR
&& integer_zerop (op2
))
4502 || (cmp_code
== NE_EXPR
&& integer_onep (op2
)))
4503 is_logical_not
= true;
4505 if (is_logical_not
== false)
4506 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op1
), op1
);
4507 /* Only for one-bit precision typed X the transformation
4508 !X -> ~X is valied. */
4509 else if (TYPE_PRECISION (type
) == 1)
4510 gimple_assign_set_rhs_with_ops (gsi
, BIT_NOT_EXPR
, op1
);
4511 /* Otherwise we use !X -> X ^ 1. */
4513 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op1
,
4514 build_int_cst (type
, 1));
4520 unsigned old_num_ops
= gimple_num_ops (stmt
);
4521 tree lhs
= gimple_assign_lhs (stmt
);
4522 tree new_rhs
= fold_gimple_assign (gsi
);
4524 && !useless_type_conversion_p (TREE_TYPE (lhs
),
4525 TREE_TYPE (new_rhs
)))
4526 new_rhs
= fold_convert (TREE_TYPE (lhs
), new_rhs
);
4529 || get_gimple_rhs_num_ops (TREE_CODE (new_rhs
)) < old_num_ops
))
4531 gimple_assign_set_rhs_from_tree (gsi
, new_rhs
);
4538 changed
|= gimple_fold_call (gsi
, inplace
);
4542 /* Fold *& in asm operands. */
4544 gasm
*asm_stmt
= as_a
<gasm
*> (stmt
);
4546 const char **oconstraints
;
4547 const char *constraint
;
4548 bool allows_mem
, allows_reg
;
4550 noutputs
= gimple_asm_noutputs (asm_stmt
);
4551 oconstraints
= XALLOCAVEC (const char *, noutputs
);
4553 for (i
= 0; i
< gimple_asm_noutputs (asm_stmt
); ++i
)
4555 tree link
= gimple_asm_output_op (asm_stmt
, i
);
4556 tree op
= TREE_VALUE (link
);
4558 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
4559 if (REFERENCE_CLASS_P (op
)
4560 && (op
= maybe_fold_reference (op
, true)) != NULL_TREE
)
4562 TREE_VALUE (link
) = op
;
4566 for (i
= 0; i
< gimple_asm_ninputs (asm_stmt
); ++i
)
4568 tree link
= gimple_asm_input_op (asm_stmt
, i
);
4569 tree op
= TREE_VALUE (link
);
4571 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
4572 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0,
4573 oconstraints
, &allows_mem
, &allows_reg
);
4574 if (REFERENCE_CLASS_P (op
)
4575 && (op
= maybe_fold_reference (op
, !allows_reg
&& allows_mem
))
4578 TREE_VALUE (link
) = op
;
4586 if (gimple_debug_bind_p (stmt
))
4588 tree val
= gimple_debug_bind_get_value (stmt
);
4590 && REFERENCE_CLASS_P (val
))
4592 tree tem
= maybe_fold_reference (val
, false);
4595 gimple_debug_bind_set_value (stmt
, tem
);
4600 && TREE_CODE (val
) == ADDR_EXPR
)
4602 tree ref
= TREE_OPERAND (val
, 0);
4603 tree tem
= maybe_fold_reference (ref
, false);
4606 tem
= build_fold_addr_expr_with_type (tem
, TREE_TYPE (val
));
4607 gimple_debug_bind_set_value (stmt
, tem
);
4616 greturn
*ret_stmt
= as_a
<greturn
*> (stmt
);
4617 tree ret
= gimple_return_retval(ret_stmt
);
4619 if (ret
&& TREE_CODE (ret
) == SSA_NAME
&& valueize
)
4621 tree val
= valueize (ret
);
4622 if (val
&& val
!= ret
4623 && may_propagate_copy (ret
, val
))
4625 gimple_return_set_retval (ret_stmt
, val
);
4635 stmt
= gsi_stmt (*gsi
);
4637 /* Fold *& on the lhs. */
4638 if (gimple_has_lhs (stmt
))
4640 tree lhs
= gimple_get_lhs (stmt
);
4641 if (lhs
&& REFERENCE_CLASS_P (lhs
))
4643 tree new_lhs
= maybe_fold_reference (lhs
, true);
4646 gimple_set_lhs (stmt
, new_lhs
);
4652 fold_undefer_overflow_warnings (changed
&& !nowarning
, stmt
, 0);
4656 /* Valueziation callback that ends up not following SSA edges. */
4659 no_follow_ssa_edges (tree
)
4664 /* Valueization callback that ends up following single-use SSA edges only. */
4667 follow_single_use_edges (tree val
)
4669 if (TREE_CODE (val
) == SSA_NAME
4670 && !has_single_use (val
))
4675 /* Fold the statement pointed to by GSI. In some cases, this function may
4676 replace the whole statement with a new one. Returns true iff folding
4678 The statement pointed to by GSI should be in valid gimple form but may
4679 be in unfolded state as resulting from for example constant propagation
4680 which can produce *&x = 0. */
4683 fold_stmt (gimple_stmt_iterator
*gsi
)
4685 return fold_stmt_1 (gsi
, false, no_follow_ssa_edges
);
4689 fold_stmt (gimple_stmt_iterator
*gsi
, tree (*valueize
) (tree
))
4691 return fold_stmt_1 (gsi
, false, valueize
);
4694 /* Perform the minimal folding on statement *GSI. Only operations like
4695 *&x created by constant propagation are handled. The statement cannot
4696 be replaced with a new one. Return true if the statement was
4697 changed, false otherwise.
4698 The statement *GSI should be in valid gimple form but may
4699 be in unfolded state as resulting from for example constant propagation
4700 which can produce *&x = 0. */
4703 fold_stmt_inplace (gimple_stmt_iterator
*gsi
)
4705 gimple
*stmt
= gsi_stmt (*gsi
);
4706 bool changed
= fold_stmt_1 (gsi
, true, no_follow_ssa_edges
);
4707 gcc_assert (gsi_stmt (*gsi
) == stmt
);
4711 /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
4712 if EXPR is null or we don't know how.
4713 If non-null, the result always has boolean type. */
4716 canonicalize_bool (tree expr
, bool invert
)
4722 if (integer_nonzerop (expr
))
4723 return boolean_false_node
;
4724 else if (integer_zerop (expr
))
4725 return boolean_true_node
;
4726 else if (TREE_CODE (expr
) == SSA_NAME
)
4727 return fold_build2 (EQ_EXPR
, boolean_type_node
, expr
,
4728 build_int_cst (TREE_TYPE (expr
), 0));
4729 else if (COMPARISON_CLASS_P (expr
))
4730 return fold_build2 (invert_tree_comparison (TREE_CODE (expr
), false),
4732 TREE_OPERAND (expr
, 0),
4733 TREE_OPERAND (expr
, 1));
4739 if (TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
)
4741 if (integer_nonzerop (expr
))
4742 return boolean_true_node
;
4743 else if (integer_zerop (expr
))
4744 return boolean_false_node
;
4745 else if (TREE_CODE (expr
) == SSA_NAME
)
4746 return fold_build2 (NE_EXPR
, boolean_type_node
, expr
,
4747 build_int_cst (TREE_TYPE (expr
), 0));
4748 else if (COMPARISON_CLASS_P (expr
))
4749 return fold_build2 (TREE_CODE (expr
),
4751 TREE_OPERAND (expr
, 0),
4752 TREE_OPERAND (expr
, 1));
4758 /* Check to see if a boolean expression EXPR is logically equivalent to the
4759 comparison (OP1 CODE OP2). Check for various identities involving
4763 same_bool_comparison_p (const_tree expr
, enum tree_code code
,
4764 const_tree op1
, const_tree op2
)
4768 /* The obvious case. */
4769 if (TREE_CODE (expr
) == code
4770 && operand_equal_p (TREE_OPERAND (expr
, 0), op1
, 0)
4771 && operand_equal_p (TREE_OPERAND (expr
, 1), op2
, 0))
4774 /* Check for comparing (name, name != 0) and the case where expr
4775 is an SSA_NAME with a definition matching the comparison. */
4776 if (TREE_CODE (expr
) == SSA_NAME
4777 && TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
)
4779 if (operand_equal_p (expr
, op1
, 0))
4780 return ((code
== NE_EXPR
&& integer_zerop (op2
))
4781 || (code
== EQ_EXPR
&& integer_nonzerop (op2
)));
4782 s
= SSA_NAME_DEF_STMT (expr
);
4783 if (is_gimple_assign (s
)
4784 && gimple_assign_rhs_code (s
) == code
4785 && operand_equal_p (gimple_assign_rhs1 (s
), op1
, 0)
4786 && operand_equal_p (gimple_assign_rhs2 (s
), op2
, 0))
4790 /* If op1 is of the form (name != 0) or (name == 0), and the definition
4791 of name is a comparison, recurse. */
4792 if (TREE_CODE (op1
) == SSA_NAME
4793 && TREE_CODE (TREE_TYPE (op1
)) == BOOLEAN_TYPE
)
4795 s
= SSA_NAME_DEF_STMT (op1
);
4796 if (is_gimple_assign (s
)
4797 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
)
4799 enum tree_code c
= gimple_assign_rhs_code (s
);
4800 if ((c
== NE_EXPR
&& integer_zerop (op2
))
4801 || (c
== EQ_EXPR
&& integer_nonzerop (op2
)))
4802 return same_bool_comparison_p (expr
, c
,
4803 gimple_assign_rhs1 (s
),
4804 gimple_assign_rhs2 (s
));
4805 if ((c
== EQ_EXPR
&& integer_zerop (op2
))
4806 || (c
== NE_EXPR
&& integer_nonzerop (op2
)))
4807 return same_bool_comparison_p (expr
,
4808 invert_tree_comparison (c
, false),
4809 gimple_assign_rhs1 (s
),
4810 gimple_assign_rhs2 (s
));
4816 /* Check to see if two boolean expressions OP1 and OP2 are logically
4820 same_bool_result_p (const_tree op1
, const_tree op2
)
4822 /* Simple cases first. */
4823 if (operand_equal_p (op1
, op2
, 0))
4826 /* Check the cases where at least one of the operands is a comparison.
4827 These are a bit smarter than operand_equal_p in that they apply some
4828 identifies on SSA_NAMEs. */
4829 if (COMPARISON_CLASS_P (op2
)
4830 && same_bool_comparison_p (op1
, TREE_CODE (op2
),
4831 TREE_OPERAND (op2
, 0),
4832 TREE_OPERAND (op2
, 1)))
4834 if (COMPARISON_CLASS_P (op1
)
4835 && same_bool_comparison_p (op2
, TREE_CODE (op1
),
4836 TREE_OPERAND (op1
, 0),
4837 TREE_OPERAND (op1
, 1)))
4844 /* Forward declarations for some mutually recursive functions. */
4847 and_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
4848 enum tree_code code2
, tree op2a
, tree op2b
);
4850 and_var_with_comparison (tree var
, bool invert
,
4851 enum tree_code code2
, tree op2a
, tree op2b
);
4853 and_var_with_comparison_1 (gimple
*stmt
,
4854 enum tree_code code2
, tree op2a
, tree op2b
);
4856 or_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
4857 enum tree_code code2
, tree op2a
, tree op2b
);
4859 or_var_with_comparison (tree var
, bool invert
,
4860 enum tree_code code2
, tree op2a
, tree op2b
);
4862 or_var_with_comparison_1 (gimple
*stmt
,
4863 enum tree_code code2
, tree op2a
, tree op2b
);
4865 /* Helper function for and_comparisons_1: try to simplify the AND of the
4866 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
4867 If INVERT is true, invert the value of the VAR before doing the AND.
4868 Return NULL_EXPR if we can't simplify this to a single expression. */
4871 and_var_with_comparison (tree var
, bool invert
,
4872 enum tree_code code2
, tree op2a
, tree op2b
)
4875 gimple
*stmt
= SSA_NAME_DEF_STMT (var
);
4877 /* We can only deal with variables whose definitions are assignments. */
4878 if (!is_gimple_assign (stmt
))
4881 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
4882 !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
4883 Then we only have to consider the simpler non-inverted cases. */
4885 t
= or_var_with_comparison_1 (stmt
,
4886 invert_tree_comparison (code2
, false),
4889 t
= and_var_with_comparison_1 (stmt
, code2
, op2a
, op2b
);
4890 return canonicalize_bool (t
, invert
);
4893 /* Try to simplify the AND of the ssa variable defined by the assignment
4894 STMT with the comparison specified by (OP2A CODE2 OP2B).
4895 Return NULL_EXPR if we can't simplify this to a single expression. */
4898 and_var_with_comparison_1 (gimple
*stmt
,
4899 enum tree_code code2
, tree op2a
, tree op2b
)
4901 tree var
= gimple_assign_lhs (stmt
);
4902 tree true_test_var
= NULL_TREE
;
4903 tree false_test_var
= NULL_TREE
;
4904 enum tree_code innercode
= gimple_assign_rhs_code (stmt
);
4906 /* Check for identities like (var AND (var == 0)) => false. */
4907 if (TREE_CODE (op2a
) == SSA_NAME
4908 && TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
)
4910 if ((code2
== NE_EXPR
&& integer_zerop (op2b
))
4911 || (code2
== EQ_EXPR
&& integer_nonzerop (op2b
)))
4913 true_test_var
= op2a
;
4914 if (var
== true_test_var
)
4917 else if ((code2
== EQ_EXPR
&& integer_zerop (op2b
))
4918 || (code2
== NE_EXPR
&& integer_nonzerop (op2b
)))
4920 false_test_var
= op2a
;
4921 if (var
== false_test_var
)
4922 return boolean_false_node
;
4926 /* If the definition is a comparison, recurse on it. */
4927 if (TREE_CODE_CLASS (innercode
) == tcc_comparison
)
4929 tree t
= and_comparisons_1 (innercode
,
4930 gimple_assign_rhs1 (stmt
),
4931 gimple_assign_rhs2 (stmt
),
4939 /* If the definition is an AND or OR expression, we may be able to
4940 simplify by reassociating. */
4941 if (TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
4942 && (innercode
== BIT_AND_EXPR
|| innercode
== BIT_IOR_EXPR
))
4944 tree inner1
= gimple_assign_rhs1 (stmt
);
4945 tree inner2
= gimple_assign_rhs2 (stmt
);
4948 tree partial
= NULL_TREE
;
4949 bool is_and
= (innercode
== BIT_AND_EXPR
);
4951 /* Check for boolean identities that don't require recursive examination
4953 inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
4954 inner1 AND (inner1 OR inner2) => inner1
4955 !inner1 AND (inner1 AND inner2) => false
4956 !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
4957 Likewise for similar cases involving inner2. */
4958 if (inner1
== true_test_var
)
4959 return (is_and
? var
: inner1
);
4960 else if (inner2
== true_test_var
)
4961 return (is_and
? var
: inner2
);
4962 else if (inner1
== false_test_var
)
4964 ? boolean_false_node
4965 : and_var_with_comparison (inner2
, false, code2
, op2a
, op2b
));
4966 else if (inner2
== false_test_var
)
4968 ? boolean_false_node
4969 : and_var_with_comparison (inner1
, false, code2
, op2a
, op2b
));
4971 /* Next, redistribute/reassociate the AND across the inner tests.
4972 Compute the first partial result, (inner1 AND (op2a code op2b)) */
4973 if (TREE_CODE (inner1
) == SSA_NAME
4974 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner1
))
4975 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
4976 && (t
= maybe_fold_and_comparisons (gimple_assign_rhs_code (s
),
4977 gimple_assign_rhs1 (s
),
4978 gimple_assign_rhs2 (s
),
4979 code2
, op2a
, op2b
)))
4981 /* Handle the AND case, where we are reassociating:
4982 (inner1 AND inner2) AND (op2a code2 op2b)
4984 If the partial result t is a constant, we win. Otherwise
4985 continue on to try reassociating with the other inner test. */
4988 if (integer_onep (t
))
4990 else if (integer_zerop (t
))
4991 return boolean_false_node
;
4994 /* Handle the OR case, where we are redistributing:
4995 (inner1 OR inner2) AND (op2a code2 op2b)
4996 => (t OR (inner2 AND (op2a code2 op2b))) */
4997 else if (integer_onep (t
))
4998 return boolean_true_node
;
5000 /* Save partial result for later. */
5004 /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
5005 if (TREE_CODE (inner2
) == SSA_NAME
5006 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner2
))
5007 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
5008 && (t
= maybe_fold_and_comparisons (gimple_assign_rhs_code (s
),
5009 gimple_assign_rhs1 (s
),
5010 gimple_assign_rhs2 (s
),
5011 code2
, op2a
, op2b
)))
5013 /* Handle the AND case, where we are reassociating:
5014 (inner1 AND inner2) AND (op2a code2 op2b)
5015 => (inner1 AND t) */
5018 if (integer_onep (t
))
5020 else if (integer_zerop (t
))
5021 return boolean_false_node
;
5022 /* If both are the same, we can apply the identity
5024 else if (partial
&& same_bool_result_p (t
, partial
))
5028 /* Handle the OR case. where we are redistributing:
5029 (inner1 OR inner2) AND (op2a code2 op2b)
5030 => (t OR (inner1 AND (op2a code2 op2b)))
5031 => (t OR partial) */
5034 if (integer_onep (t
))
5035 return boolean_true_node
;
5038 /* We already got a simplification for the other
5039 operand to the redistributed OR expression. The
5040 interesting case is when at least one is false.
5041 Or, if both are the same, we can apply the identity
5043 if (integer_zerop (partial
))
5045 else if (integer_zerop (t
))
5047 else if (same_bool_result_p (t
, partial
))
5056 /* Try to simplify the AND of two comparisons defined by
5057 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
5058 If this can be done without constructing an intermediate value,
5059 return the resulting tree; otherwise NULL_TREE is returned.
5060 This function is deliberately asymmetric as it recurses on SSA_DEFs
5061 in the first comparison but not the second. */
5064 and_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
5065 enum tree_code code2
, tree op2a
, tree op2b
)
5067 tree truth_type
= truth_type_for (TREE_TYPE (op1a
));
5069 /* First check for ((x CODE1 y) AND (x CODE2 y)). */
5070 if (operand_equal_p (op1a
, op2a
, 0)
5071 && operand_equal_p (op1b
, op2b
, 0))
5073 /* Result will be either NULL_TREE, or a combined comparison. */
5074 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
5075 TRUTH_ANDIF_EXPR
, code1
, code2
,
5076 truth_type
, op1a
, op1b
);
5081 /* Likewise the swapped case of the above. */
5082 if (operand_equal_p (op1a
, op2b
, 0)
5083 && operand_equal_p (op1b
, op2a
, 0))
5085 /* Result will be either NULL_TREE, or a combined comparison. */
5086 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
5087 TRUTH_ANDIF_EXPR
, code1
,
5088 swap_tree_comparison (code2
),
5089 truth_type
, op1a
, op1b
);
5094 /* If both comparisons are of the same value against constants, we might
5095 be able to merge them. */
5096 if (operand_equal_p (op1a
, op2a
, 0)
5097 && TREE_CODE (op1b
) == INTEGER_CST
5098 && TREE_CODE (op2b
) == INTEGER_CST
)
5100 int cmp
= tree_int_cst_compare (op1b
, op2b
);
5102 /* If we have (op1a == op1b), we should either be able to
5103 return that or FALSE, depending on whether the constant op1b
5104 also satisfies the other comparison against op2b. */
5105 if (code1
== EQ_EXPR
)
5111 case EQ_EXPR
: val
= (cmp
== 0); break;
5112 case NE_EXPR
: val
= (cmp
!= 0); break;
5113 case LT_EXPR
: val
= (cmp
< 0); break;
5114 case GT_EXPR
: val
= (cmp
> 0); break;
5115 case LE_EXPR
: val
= (cmp
<= 0); break;
5116 case GE_EXPR
: val
= (cmp
>= 0); break;
5117 default: done
= false;
5122 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5124 return boolean_false_node
;
5127 /* Likewise if the second comparison is an == comparison. */
5128 else if (code2
== EQ_EXPR
)
5134 case EQ_EXPR
: val
= (cmp
== 0); break;
5135 case NE_EXPR
: val
= (cmp
!= 0); break;
5136 case LT_EXPR
: val
= (cmp
> 0); break;
5137 case GT_EXPR
: val
= (cmp
< 0); break;
5138 case LE_EXPR
: val
= (cmp
>= 0); break;
5139 case GE_EXPR
: val
= (cmp
<= 0); break;
5140 default: done
= false;
5145 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5147 return boolean_false_node
;
5151 /* Same business with inequality tests. */
5152 else if (code1
== NE_EXPR
)
5157 case EQ_EXPR
: val
= (cmp
!= 0); break;
5158 case NE_EXPR
: val
= (cmp
== 0); break;
5159 case LT_EXPR
: val
= (cmp
>= 0); break;
5160 case GT_EXPR
: val
= (cmp
<= 0); break;
5161 case LE_EXPR
: val
= (cmp
> 0); break;
5162 case GE_EXPR
: val
= (cmp
< 0); break;
5167 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5169 else if (code2
== NE_EXPR
)
5174 case EQ_EXPR
: val
= (cmp
== 0); break;
5175 case NE_EXPR
: val
= (cmp
!= 0); break;
5176 case LT_EXPR
: val
= (cmp
<= 0); break;
5177 case GT_EXPR
: val
= (cmp
>= 0); break;
5178 case LE_EXPR
: val
= (cmp
< 0); break;
5179 case GE_EXPR
: val
= (cmp
> 0); break;
5184 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5187 /* Chose the more restrictive of two < or <= comparisons. */
5188 else if ((code1
== LT_EXPR
|| code1
== LE_EXPR
)
5189 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
5191 if ((cmp
< 0) || (cmp
== 0 && code1
== LT_EXPR
))
5192 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5194 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5197 /* Likewise chose the more restrictive of two > or >= comparisons. */
5198 else if ((code1
== GT_EXPR
|| code1
== GE_EXPR
)
5199 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
5201 if ((cmp
> 0) || (cmp
== 0 && code1
== GT_EXPR
))
5202 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5204 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5207 /* Check for singleton ranges. */
5209 && ((code1
== LE_EXPR
&& code2
== GE_EXPR
)
5210 || (code1
== GE_EXPR
&& code2
== LE_EXPR
)))
5211 return fold_build2 (EQ_EXPR
, boolean_type_node
, op1a
, op2b
);
5213 /* Check for disjoint ranges. */
5215 && (code1
== LT_EXPR
|| code1
== LE_EXPR
)
5216 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
5217 return boolean_false_node
;
5219 && (code1
== GT_EXPR
|| code1
== GE_EXPR
)
5220 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
5221 return boolean_false_node
;
5224 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
5225 NAME's definition is a truth value. See if there are any simplifications
5226 that can be done against the NAME's definition. */
5227 if (TREE_CODE (op1a
) == SSA_NAME
5228 && (code1
== NE_EXPR
|| code1
== EQ_EXPR
)
5229 && (integer_zerop (op1b
) || integer_onep (op1b
)))
5231 bool invert
= ((code1
== EQ_EXPR
&& integer_zerop (op1b
))
5232 || (code1
== NE_EXPR
&& integer_onep (op1b
)));
5233 gimple
*stmt
= SSA_NAME_DEF_STMT (op1a
);
5234 switch (gimple_code (stmt
))
5237 /* Try to simplify by copy-propagating the definition. */
5238 return and_var_with_comparison (op1a
, invert
, code2
, op2a
, op2b
);
5241 /* If every argument to the PHI produces the same result when
5242 ANDed with the second comparison, we win.
5243 Do not do this unless the type is bool since we need a bool
5244 result here anyway. */
5245 if (TREE_CODE (TREE_TYPE (op1a
)) == BOOLEAN_TYPE
)
5247 tree result
= NULL_TREE
;
5249 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
5251 tree arg
= gimple_phi_arg_def (stmt
, i
);
5253 /* If this PHI has itself as an argument, ignore it.
5254 If all the other args produce the same result,
5256 if (arg
== gimple_phi_result (stmt
))
5258 else if (TREE_CODE (arg
) == INTEGER_CST
)
5260 if (invert
? integer_nonzerop (arg
) : integer_zerop (arg
))
5263 result
= boolean_false_node
;
5264 else if (!integer_zerop (result
))
5268 result
= fold_build2 (code2
, boolean_type_node
,
5270 else if (!same_bool_comparison_p (result
,
5274 else if (TREE_CODE (arg
) == SSA_NAME
5275 && !SSA_NAME_IS_DEFAULT_DEF (arg
))
5278 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
5279 /* In simple cases we can look through PHI nodes,
5280 but we have to be careful with loops.
5282 if (! dom_info_available_p (CDI_DOMINATORS
)
5283 || gimple_bb (def_stmt
) == gimple_bb (stmt
)
5284 || dominated_by_p (CDI_DOMINATORS
,
5285 gimple_bb (def_stmt
),
5288 temp
= and_var_with_comparison (arg
, invert
, code2
,
5294 else if (!same_bool_result_p (result
, temp
))
5310 /* Try to simplify the AND of two comparisons, specified by
5311 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
5312 If this can be simplified to a single expression (without requiring
5313 introducing more SSA variables to hold intermediate values),
5314 return the resulting tree. Otherwise return NULL_TREE.
5315 If the result expression is non-null, it has boolean type. */
5318 maybe_fold_and_comparisons (enum tree_code code1
, tree op1a
, tree op1b
,
5319 enum tree_code code2
, tree op2a
, tree op2b
)
5321 tree t
= and_comparisons_1 (code1
, op1a
, op1b
, code2
, op2a
, op2b
);
5325 return and_comparisons_1 (code2
, op2a
, op2b
, code1
, op1a
, op1b
);
5328 /* Helper function for or_comparisons_1: try to simplify the OR of the
5329 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
5330 If INVERT is true, invert the value of VAR before doing the OR.
5331 Return NULL_EXPR if we can't simplify this to a single expression. */
5334 or_var_with_comparison (tree var
, bool invert
,
5335 enum tree_code code2
, tree op2a
, tree op2b
)
5338 gimple
*stmt
= SSA_NAME_DEF_STMT (var
);
5340 /* We can only deal with variables whose definitions are assignments. */
5341 if (!is_gimple_assign (stmt
))
5344 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
5345 !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
5346 Then we only have to consider the simpler non-inverted cases. */
5348 t
= and_var_with_comparison_1 (stmt
,
5349 invert_tree_comparison (code2
, false),
5352 t
= or_var_with_comparison_1 (stmt
, code2
, op2a
, op2b
);
5353 return canonicalize_bool (t
, invert
);
5356 /* Try to simplify the OR of the ssa variable defined by the assignment
5357 STMT with the comparison specified by (OP2A CODE2 OP2B).
5358 Return NULL_EXPR if we can't simplify this to a single expression. */
5361 or_var_with_comparison_1 (gimple
*stmt
,
5362 enum tree_code code2
, tree op2a
, tree op2b
)
5364 tree var
= gimple_assign_lhs (stmt
);
5365 tree true_test_var
= NULL_TREE
;
5366 tree false_test_var
= NULL_TREE
;
5367 enum tree_code innercode
= gimple_assign_rhs_code (stmt
);
5369 /* Check for identities like (var OR (var != 0)) => true . */
5370 if (TREE_CODE (op2a
) == SSA_NAME
5371 && TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
)
5373 if ((code2
== NE_EXPR
&& integer_zerop (op2b
))
5374 || (code2
== EQ_EXPR
&& integer_nonzerop (op2b
)))
5376 true_test_var
= op2a
;
5377 if (var
== true_test_var
)
5380 else if ((code2
== EQ_EXPR
&& integer_zerop (op2b
))
5381 || (code2
== NE_EXPR
&& integer_nonzerop (op2b
)))
5383 false_test_var
= op2a
;
5384 if (var
== false_test_var
)
5385 return boolean_true_node
;
5389 /* If the definition is a comparison, recurse on it. */
5390 if (TREE_CODE_CLASS (innercode
) == tcc_comparison
)
5392 tree t
= or_comparisons_1 (innercode
,
5393 gimple_assign_rhs1 (stmt
),
5394 gimple_assign_rhs2 (stmt
),
5402 /* If the definition is an AND or OR expression, we may be able to
5403 simplify by reassociating. */
5404 if (TREE_CODE (TREE_TYPE (var
)) == BOOLEAN_TYPE
5405 && (innercode
== BIT_AND_EXPR
|| innercode
== BIT_IOR_EXPR
))
5407 tree inner1
= gimple_assign_rhs1 (stmt
);
5408 tree inner2
= gimple_assign_rhs2 (stmt
);
5411 tree partial
= NULL_TREE
;
5412 bool is_or
= (innercode
== BIT_IOR_EXPR
);
5414 /* Check for boolean identities that don't require recursive examination
5416 inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
5417 inner1 OR (inner1 AND inner2) => inner1
5418 !inner1 OR (inner1 OR inner2) => true
5419 !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
5421 if (inner1
== true_test_var
)
5422 return (is_or
? var
: inner1
);
5423 else if (inner2
== true_test_var
)
5424 return (is_or
? var
: inner2
);
5425 else if (inner1
== false_test_var
)
5428 : or_var_with_comparison (inner2
, false, code2
, op2a
, op2b
));
5429 else if (inner2
== false_test_var
)
5432 : or_var_with_comparison (inner1
, false, code2
, op2a
, op2b
));
5434 /* Next, redistribute/reassociate the OR across the inner tests.
5435 Compute the first partial result, (inner1 OR (op2a code op2b)) */
5436 if (TREE_CODE (inner1
) == SSA_NAME
5437 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner1
))
5438 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
5439 && (t
= maybe_fold_or_comparisons (gimple_assign_rhs_code (s
),
5440 gimple_assign_rhs1 (s
),
5441 gimple_assign_rhs2 (s
),
5442 code2
, op2a
, op2b
)))
5444 /* Handle the OR case, where we are reassociating:
5445 (inner1 OR inner2) OR (op2a code2 op2b)
5447 If the partial result t is a constant, we win. Otherwise
5448 continue on to try reassociating with the other inner test. */
5451 if (integer_onep (t
))
5452 return boolean_true_node
;
5453 else if (integer_zerop (t
))
5457 /* Handle the AND case, where we are redistributing:
5458 (inner1 AND inner2) OR (op2a code2 op2b)
5459 => (t AND (inner2 OR (op2a code op2b))) */
5460 else if (integer_zerop (t
))
5461 return boolean_false_node
;
5463 /* Save partial result for later. */
5467 /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
5468 if (TREE_CODE (inner2
) == SSA_NAME
5469 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (inner2
))
5470 && TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
5471 && (t
= maybe_fold_or_comparisons (gimple_assign_rhs_code (s
),
5472 gimple_assign_rhs1 (s
),
5473 gimple_assign_rhs2 (s
),
5474 code2
, op2a
, op2b
)))
5476 /* Handle the OR case, where we are reassociating:
5477 (inner1 OR inner2) OR (op2a code2 op2b)
5479 => (t OR partial) */
5482 if (integer_zerop (t
))
5484 else if (integer_onep (t
))
5485 return boolean_true_node
;
5486 /* If both are the same, we can apply the identity
5488 else if (partial
&& same_bool_result_p (t
, partial
))
5492 /* Handle the AND case, where we are redistributing:
5493 (inner1 AND inner2) OR (op2a code2 op2b)
5494 => (t AND (inner1 OR (op2a code2 op2b)))
5495 => (t AND partial) */
5498 if (integer_zerop (t
))
5499 return boolean_false_node
;
5502 /* We already got a simplification for the other
5503 operand to the redistributed AND expression. The
5504 interesting case is when at least one is true.
5505 Or, if both are the same, we can apply the identity
5507 if (integer_onep (partial
))
5509 else if (integer_onep (t
))
5511 else if (same_bool_result_p (t
, partial
))
5520 /* Try to simplify the OR of two comparisons defined by
5521 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
5522 If this can be done without constructing an intermediate value,
5523 return the resulting tree; otherwise NULL_TREE is returned.
5524 This function is deliberately asymmetric as it recurses on SSA_DEFs
5525 in the first comparison but not the second. */
5528 or_comparisons_1 (enum tree_code code1
, tree op1a
, tree op1b
,
5529 enum tree_code code2
, tree op2a
, tree op2b
)
5531 tree truth_type
= truth_type_for (TREE_TYPE (op1a
));
5533 /* First check for ((x CODE1 y) OR (x CODE2 y)). */
5534 if (operand_equal_p (op1a
, op2a
, 0)
5535 && operand_equal_p (op1b
, op2b
, 0))
5537 /* Result will be either NULL_TREE, or a combined comparison. */
5538 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
5539 TRUTH_ORIF_EXPR
, code1
, code2
,
5540 truth_type
, op1a
, op1b
);
5545 /* Likewise the swapped case of the above. */
5546 if (operand_equal_p (op1a
, op2b
, 0)
5547 && operand_equal_p (op1b
, op2a
, 0))
5549 /* Result will be either NULL_TREE, or a combined comparison. */
5550 tree t
= combine_comparisons (UNKNOWN_LOCATION
,
5551 TRUTH_ORIF_EXPR
, code1
,
5552 swap_tree_comparison (code2
),
5553 truth_type
, op1a
, op1b
);
5558 /* If both comparisons are of the same value against constants, we might
5559 be able to merge them. */
5560 if (operand_equal_p (op1a
, op2a
, 0)
5561 && TREE_CODE (op1b
) == INTEGER_CST
5562 && TREE_CODE (op2b
) == INTEGER_CST
)
5564 int cmp
= tree_int_cst_compare (op1b
, op2b
);
5566 /* If we have (op1a != op1b), we should either be able to
5567 return that or TRUE, depending on whether the constant op1b
5568 also satisfies the other comparison against op2b. */
5569 if (code1
== NE_EXPR
)
5575 case EQ_EXPR
: val
= (cmp
== 0); break;
5576 case NE_EXPR
: val
= (cmp
!= 0); break;
5577 case LT_EXPR
: val
= (cmp
< 0); break;
5578 case GT_EXPR
: val
= (cmp
> 0); break;
5579 case LE_EXPR
: val
= (cmp
<= 0); break;
5580 case GE_EXPR
: val
= (cmp
>= 0); break;
5581 default: done
= false;
5586 return boolean_true_node
;
5588 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5591 /* Likewise if the second comparison is a != comparison. */
5592 else if (code2
== NE_EXPR
)
5598 case EQ_EXPR
: val
= (cmp
== 0); break;
5599 case NE_EXPR
: val
= (cmp
!= 0); break;
5600 case LT_EXPR
: val
= (cmp
> 0); break;
5601 case GT_EXPR
: val
= (cmp
< 0); break;
5602 case LE_EXPR
: val
= (cmp
>= 0); break;
5603 case GE_EXPR
: val
= (cmp
<= 0); break;
5604 default: done
= false;
5609 return boolean_true_node
;
5611 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5615 /* See if an equality test is redundant with the other comparison. */
5616 else if (code1
== EQ_EXPR
)
5621 case EQ_EXPR
: val
= (cmp
== 0); break;
5622 case NE_EXPR
: val
= (cmp
!= 0); break;
5623 case LT_EXPR
: val
= (cmp
< 0); break;
5624 case GT_EXPR
: val
= (cmp
> 0); break;
5625 case LE_EXPR
: val
= (cmp
<= 0); break;
5626 case GE_EXPR
: val
= (cmp
>= 0); break;
5631 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5633 else if (code2
== EQ_EXPR
)
5638 case EQ_EXPR
: val
= (cmp
== 0); break;
5639 case NE_EXPR
: val
= (cmp
!= 0); break;
5640 case LT_EXPR
: val
= (cmp
> 0); break;
5641 case GT_EXPR
: val
= (cmp
< 0); break;
5642 case LE_EXPR
: val
= (cmp
>= 0); break;
5643 case GE_EXPR
: val
= (cmp
<= 0); break;
5648 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5651 /* Chose the less restrictive of two < or <= comparisons. */
5652 else if ((code1
== LT_EXPR
|| code1
== LE_EXPR
)
5653 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
5655 if ((cmp
< 0) || (cmp
== 0 && code1
== LT_EXPR
))
5656 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5658 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5661 /* Likewise chose the less restrictive of two > or >= comparisons. */
5662 else if ((code1
== GT_EXPR
|| code1
== GE_EXPR
)
5663 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
5665 if ((cmp
> 0) || (cmp
== 0 && code1
== GT_EXPR
))
5666 return fold_build2 (code2
, boolean_type_node
, op2a
, op2b
);
5668 return fold_build2 (code1
, boolean_type_node
, op1a
, op1b
);
5671 /* Check for singleton ranges. */
5673 && ((code1
== LT_EXPR
&& code2
== GT_EXPR
)
5674 || (code1
== GT_EXPR
&& code2
== LT_EXPR
)))
5675 return fold_build2 (NE_EXPR
, boolean_type_node
, op1a
, op2b
);
5677 /* Check for less/greater pairs that don't restrict the range at all. */
5679 && (code1
== LT_EXPR
|| code1
== LE_EXPR
)
5680 && (code2
== GT_EXPR
|| code2
== GE_EXPR
))
5681 return boolean_true_node
;
5683 && (code1
== GT_EXPR
|| code1
== GE_EXPR
)
5684 && (code2
== LT_EXPR
|| code2
== LE_EXPR
))
5685 return boolean_true_node
;
5688 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
5689 NAME's definition is a truth value. See if there are any simplifications
5690 that can be done against the NAME's definition. */
5691 if (TREE_CODE (op1a
) == SSA_NAME
5692 && (code1
== NE_EXPR
|| code1
== EQ_EXPR
)
5693 && (integer_zerop (op1b
) || integer_onep (op1b
)))
5695 bool invert
= ((code1
== EQ_EXPR
&& integer_zerop (op1b
))
5696 || (code1
== NE_EXPR
&& integer_onep (op1b
)));
5697 gimple
*stmt
= SSA_NAME_DEF_STMT (op1a
);
5698 switch (gimple_code (stmt
))
5701 /* Try to simplify by copy-propagating the definition. */
5702 return or_var_with_comparison (op1a
, invert
, code2
, op2a
, op2b
);
5705 /* If every argument to the PHI produces the same result when
5706 ORed with the second comparison, we win.
5707 Do not do this unless the type is bool since we need a bool
5708 result here anyway. */
5709 if (TREE_CODE (TREE_TYPE (op1a
)) == BOOLEAN_TYPE
)
5711 tree result
= NULL_TREE
;
5713 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
5715 tree arg
= gimple_phi_arg_def (stmt
, i
);
5717 /* If this PHI has itself as an argument, ignore it.
5718 If all the other args produce the same result,
5720 if (arg
== gimple_phi_result (stmt
))
5722 else if (TREE_CODE (arg
) == INTEGER_CST
)
5724 if (invert
? integer_zerop (arg
) : integer_nonzerop (arg
))
5727 result
= boolean_true_node
;
5728 else if (!integer_onep (result
))
5732 result
= fold_build2 (code2
, boolean_type_node
,
5734 else if (!same_bool_comparison_p (result
,
5738 else if (TREE_CODE (arg
) == SSA_NAME
5739 && !SSA_NAME_IS_DEFAULT_DEF (arg
))
5742 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
5743 /* In simple cases we can look through PHI nodes,
5744 but we have to be careful with loops.
5746 if (! dom_info_available_p (CDI_DOMINATORS
)
5747 || gimple_bb (def_stmt
) == gimple_bb (stmt
)
5748 || dominated_by_p (CDI_DOMINATORS
,
5749 gimple_bb (def_stmt
),
5752 temp
= or_var_with_comparison (arg
, invert
, code2
,
5758 else if (!same_bool_result_p (result
, temp
))
5774 /* Try to simplify the OR of two comparisons, specified by
5775 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
5776 If this can be simplified to a single expression (without requiring
5777 introducing more SSA variables to hold intermediate values),
5778 return the resulting tree. Otherwise return NULL_TREE.
5779 If the result expression is non-null, it has boolean type. */
5782 maybe_fold_or_comparisons (enum tree_code code1
, tree op1a
, tree op1b
,
5783 enum tree_code code2
, tree op2a
, tree op2b
)
5785 tree t
= or_comparisons_1 (code1
, op1a
, op1b
, code2
, op2a
, op2b
);
5789 return or_comparisons_1 (code2
, op2a
, op2b
, code1
, op1a
, op1b
);
5793 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
5795 Either NULL_TREE, a simplified but non-constant or a constant
5798 ??? This should go into a gimple-fold-inline.h file to be eventually
5799 privatized with the single valueize function used in the various TUs
5800 to avoid the indirect function call overhead. */
5803 gimple_fold_stmt_to_constant_1 (gimple
*stmt
, tree (*valueize
) (tree
),
5804 tree (*gvalueize
) (tree
))
5808 /* ??? The SSA propagators do not correctly deal with following SSA use-def
5809 edges if there are intermediate VARYING defs. For this reason
5810 do not follow SSA edges here even though SCCVN can technically
5811 just deal fine with that. */
5812 if (gimple_simplify (stmt
, &rcode
, ops
, NULL
, gvalueize
, valueize
))
5814 tree res
= NULL_TREE
;
5815 if (gimple_simplified_result_is_gimple_val (rcode
, ops
))
5817 else if (mprts_hook
)
5818 res
= mprts_hook (rcode
, gimple_expr_type (stmt
), ops
);
5821 if (dump_file
&& dump_flags
& TDF_DETAILS
)
5823 fprintf (dump_file
, "Match-and-simplified ");
5824 print_gimple_expr (dump_file
, stmt
, 0, TDF_SLIM
);
5825 fprintf (dump_file
, " to ");
5826 print_generic_expr (dump_file
, res
);
5827 fprintf (dump_file
, "\n");
5833 location_t loc
= gimple_location (stmt
);
5834 switch (gimple_code (stmt
))
5838 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
5840 switch (get_gimple_rhs_class (subcode
))
5842 case GIMPLE_SINGLE_RHS
:
5844 tree rhs
= gimple_assign_rhs1 (stmt
);
5845 enum tree_code_class kind
= TREE_CODE_CLASS (subcode
);
5847 if (TREE_CODE (rhs
) == SSA_NAME
)
5849 /* If the RHS is an SSA_NAME, return its known constant value,
5851 return (*valueize
) (rhs
);
5853 /* Handle propagating invariant addresses into address
5855 else if (TREE_CODE (rhs
) == ADDR_EXPR
5856 && !is_gimple_min_invariant (rhs
))
5858 HOST_WIDE_INT offset
= 0;
5860 base
= get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs
, 0),
5864 && (CONSTANT_CLASS_P (base
)
5865 || decl_address_invariant_p (base
)))
5866 return build_invariant_address (TREE_TYPE (rhs
),
5869 else if (TREE_CODE (rhs
) == CONSTRUCTOR
5870 && TREE_CODE (TREE_TYPE (rhs
)) == VECTOR_TYPE
5871 && (CONSTRUCTOR_NELTS (rhs
)
5872 == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs
))))
5877 vec
= XALLOCAVEC (tree
,
5878 TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs
)));
5879 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs
), i
, val
)
5881 val
= (*valueize
) (val
);
5882 if (TREE_CODE (val
) == INTEGER_CST
5883 || TREE_CODE (val
) == REAL_CST
5884 || TREE_CODE (val
) == FIXED_CST
)
5890 return build_vector (TREE_TYPE (rhs
), vec
);
5892 if (subcode
== OBJ_TYPE_REF
)
5894 tree val
= (*valueize
) (OBJ_TYPE_REF_EXPR (rhs
));
5895 /* If callee is constant, we can fold away the wrapper. */
5896 if (is_gimple_min_invariant (val
))
5900 if (kind
== tcc_reference
)
5902 if ((TREE_CODE (rhs
) == VIEW_CONVERT_EXPR
5903 || TREE_CODE (rhs
) == REALPART_EXPR
5904 || TREE_CODE (rhs
) == IMAGPART_EXPR
)
5905 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
5907 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
5908 return fold_unary_loc (EXPR_LOCATION (rhs
),
5910 TREE_TYPE (rhs
), val
);
5912 else if (TREE_CODE (rhs
) == BIT_FIELD_REF
5913 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
5915 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
5916 return fold_ternary_loc (EXPR_LOCATION (rhs
),
5918 TREE_TYPE (rhs
), val
,
5919 TREE_OPERAND (rhs
, 1),
5920 TREE_OPERAND (rhs
, 2));
5922 else if (TREE_CODE (rhs
) == MEM_REF
5923 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
5925 tree val
= (*valueize
) (TREE_OPERAND (rhs
, 0));
5926 if (TREE_CODE (val
) == ADDR_EXPR
5927 && is_gimple_min_invariant (val
))
5929 tree tem
= fold_build2 (MEM_REF
, TREE_TYPE (rhs
),
5931 TREE_OPERAND (rhs
, 1));
5936 return fold_const_aggregate_ref_1 (rhs
, valueize
);
5938 else if (kind
== tcc_declaration
)
5939 return get_symbol_constant_value (rhs
);
5943 case GIMPLE_UNARY_RHS
:
5946 case GIMPLE_BINARY_RHS
:
5947 /* Translate &x + CST into an invariant form suitable for
5948 further propagation. */
5949 if (subcode
== POINTER_PLUS_EXPR
)
5951 tree op0
= (*valueize
) (gimple_assign_rhs1 (stmt
));
5952 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
5953 if (TREE_CODE (op0
) == ADDR_EXPR
5954 && TREE_CODE (op1
) == INTEGER_CST
)
5956 tree off
= fold_convert (ptr_type_node
, op1
);
5957 return build_fold_addr_expr_loc
5959 fold_build2 (MEM_REF
,
5960 TREE_TYPE (TREE_TYPE (op0
)),
5961 unshare_expr (op0
), off
));
5964 /* Canonicalize bool != 0 and bool == 0 appearing after
5965 valueization. While gimple_simplify handles this
5966 it can get confused by the ~X == 1 -> X == 0 transform
5967 which we cant reduce to a SSA name or a constant
5968 (and we have no way to tell gimple_simplify to not
5969 consider those transforms in the first place). */
5970 else if (subcode
== EQ_EXPR
5971 || subcode
== NE_EXPR
)
5973 tree lhs
= gimple_assign_lhs (stmt
);
5974 tree op0
= gimple_assign_rhs1 (stmt
);
5975 if (useless_type_conversion_p (TREE_TYPE (lhs
),
5978 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
5979 op0
= (*valueize
) (op0
);
5980 if (TREE_CODE (op0
) == INTEGER_CST
)
5981 std::swap (op0
, op1
);
5982 if (TREE_CODE (op1
) == INTEGER_CST
5983 && ((subcode
== NE_EXPR
&& integer_zerop (op1
))
5984 || (subcode
== EQ_EXPR
&& integer_onep (op1
))))
5990 case GIMPLE_TERNARY_RHS
:
5992 /* Handle ternary operators that can appear in GIMPLE form. */
5993 tree op0
= (*valueize
) (gimple_assign_rhs1 (stmt
));
5994 tree op1
= (*valueize
) (gimple_assign_rhs2 (stmt
));
5995 tree op2
= (*valueize
) (gimple_assign_rhs3 (stmt
));
5996 return fold_ternary_loc (loc
, subcode
,
5997 gimple_expr_type (stmt
), op0
, op1
, op2
);
6008 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
6010 if (gimple_call_internal_p (stmt
))
6012 enum tree_code subcode
= ERROR_MARK
;
6013 switch (gimple_call_internal_fn (stmt
))
6015 case IFN_UBSAN_CHECK_ADD
:
6016 subcode
= PLUS_EXPR
;
6018 case IFN_UBSAN_CHECK_SUB
:
6019 subcode
= MINUS_EXPR
;
6021 case IFN_UBSAN_CHECK_MUL
:
6022 subcode
= MULT_EXPR
;
6024 case IFN_BUILTIN_EXPECT
:
6026 tree arg0
= gimple_call_arg (stmt
, 0);
6027 tree op0
= (*valueize
) (arg0
);
6028 if (TREE_CODE (op0
) == INTEGER_CST
)
6035 tree arg0
= gimple_call_arg (stmt
, 0);
6036 tree arg1
= gimple_call_arg (stmt
, 1);
6037 tree op0
= (*valueize
) (arg0
);
6038 tree op1
= (*valueize
) (arg1
);
6040 if (TREE_CODE (op0
) != INTEGER_CST
6041 || TREE_CODE (op1
) != INTEGER_CST
)
6046 /* x * 0 = 0 * x = 0 without overflow. */
6047 if (integer_zerop (op0
) || integer_zerop (op1
))
6048 return build_zero_cst (TREE_TYPE (arg0
));
6051 /* y - y = 0 without overflow. */
6052 if (operand_equal_p (op0
, op1
, 0))
6053 return build_zero_cst (TREE_TYPE (arg0
));
6060 = fold_binary_loc (loc
, subcode
, TREE_TYPE (arg0
), op0
, op1
);
6062 && TREE_CODE (res
) == INTEGER_CST
6063 && !TREE_OVERFLOW (res
))
6068 fn
= (*valueize
) (gimple_call_fn (stmt
));
6069 if (TREE_CODE (fn
) == ADDR_EXPR
6070 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
6071 && DECL_BUILT_IN (TREE_OPERAND (fn
, 0))
6072 && gimple_builtin_call_types_compatible_p (stmt
,
6073 TREE_OPERAND (fn
, 0)))
6075 tree
*args
= XALLOCAVEC (tree
, gimple_call_num_args (stmt
));
6078 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
6079 args
[i
] = (*valueize
) (gimple_call_arg (stmt
, i
));
6080 retval
= fold_builtin_call_array (loc
,
6081 gimple_call_return_type (call_stmt
),
6082 fn
, gimple_call_num_args (stmt
), args
);
6085 /* fold_call_expr wraps the result inside a NOP_EXPR. */
6086 STRIP_NOPS (retval
);
6087 retval
= fold_convert (gimple_call_return_type (call_stmt
),
6100 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
6101 Returns NULL_TREE if folding to a constant is not possible, otherwise
6102 returns a constant according to is_gimple_min_invariant. */
6105 gimple_fold_stmt_to_constant (gimple
*stmt
, tree (*valueize
) (tree
))
6107 tree res
= gimple_fold_stmt_to_constant_1 (stmt
, valueize
);
6108 if (res
&& is_gimple_min_invariant (res
))
6114 /* The following set of functions are supposed to fold references using
6115 their constant initializers. */
6117 /* See if we can find constructor defining value of BASE.
6118 When we know the consructor with constant offset (such as
6119 base is array[40] and we do know constructor of array), then
6120 BIT_OFFSET is adjusted accordingly.
6122 As a special case, return error_mark_node when constructor
6123 is not explicitly available, but it is known to be zero
6124 such as 'static const int a;'. */
6126 get_base_constructor (tree base
, HOST_WIDE_INT
*bit_offset
,
6127 tree (*valueize
)(tree
))
6129 HOST_WIDE_INT bit_offset2
, size
, max_size
;
6132 if (TREE_CODE (base
) == MEM_REF
)
6134 if (!integer_zerop (TREE_OPERAND (base
, 1)))
6136 if (!tree_fits_shwi_p (TREE_OPERAND (base
, 1)))
6138 *bit_offset
+= (mem_ref_offset (base
).to_short_addr ()
6143 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
6144 base
= valueize (TREE_OPERAND (base
, 0));
6145 if (!base
|| TREE_CODE (base
) != ADDR_EXPR
)
6147 base
= TREE_OPERAND (base
, 0);
6150 && TREE_CODE (base
) == SSA_NAME
)
6151 base
= valueize (base
);
6153 /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
6154 DECL_INITIAL. If BASE is a nested reference into another
6155 ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
6156 the inner reference. */
6157 switch (TREE_CODE (base
))
6162 tree init
= ctor_for_folding (base
);
6164 /* Our semantic is exact opposite of ctor_for_folding;
6165 NULL means unknown, while error_mark_node is 0. */
6166 if (init
== error_mark_node
)
6169 return error_mark_node
;
6173 case VIEW_CONVERT_EXPR
:
6174 return get_base_constructor (TREE_OPERAND (base
, 0),
6175 bit_offset
, valueize
);
6179 base
= get_ref_base_and_extent (base
, &bit_offset2
, &size
, &max_size
,
6181 if (max_size
== -1 || size
!= max_size
)
6183 *bit_offset
+= bit_offset2
;
6184 return get_base_constructor (base
, bit_offset
, valueize
);
6190 if (CONSTANT_CLASS_P (base
))
6197 /* CTOR is CONSTRUCTOR of an array type. Fold reference of type TYPE and size
6198 SIZE to the memory at bit OFFSET. */
6201 fold_array_ctor_reference (tree type
, tree ctor
,
6202 unsigned HOST_WIDE_INT offset
,
6203 unsigned HOST_WIDE_INT size
,
6206 offset_int low_bound
;
6207 offset_int elt_size
;
6208 offset_int access_index
;
6209 tree domain_type
= NULL_TREE
;
6210 HOST_WIDE_INT inner_offset
;
6212 /* Compute low bound and elt size. */
6213 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
)
6214 domain_type
= TYPE_DOMAIN (TREE_TYPE (ctor
));
6215 if (domain_type
&& TYPE_MIN_VALUE (domain_type
))
6217 /* Static constructors for variably sized objects makes no sense. */
6218 if (TREE_CODE (TYPE_MIN_VALUE (domain_type
)) != INTEGER_CST
)
6220 low_bound
= wi::to_offset (TYPE_MIN_VALUE (domain_type
));
6224 /* Static constructors for variably sized objects makes no sense. */
6225 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor
)))) != INTEGER_CST
)
6227 elt_size
= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor
))));
6229 /* We can handle only constantly sized accesses that are known to not
6230 be larger than size of array element. */
6231 if (!TYPE_SIZE_UNIT (type
)
6232 || TREE_CODE (TYPE_SIZE_UNIT (type
)) != INTEGER_CST
6233 || elt_size
< wi::to_offset (TYPE_SIZE_UNIT (type
))
6237 /* Compute the array index we look for. */
6238 access_index
= wi::udiv_trunc (offset_int (offset
/ BITS_PER_UNIT
),
6240 access_index
+= low_bound
;
6242 /* And offset within the access. */
6243 inner_offset
= offset
% (elt_size
.to_uhwi () * BITS_PER_UNIT
);
6245 /* See if the array field is large enough to span whole access. We do not
6246 care to fold accesses spanning multiple array indexes. */
6247 if (inner_offset
+ size
> elt_size
.to_uhwi () * BITS_PER_UNIT
)
6249 if (tree val
= get_array_ctor_element_at_index (ctor
, access_index
))
6250 return fold_ctor_reference (type
, val
, inner_offset
, size
, from_decl
);
6252 /* When memory is not explicitely mentioned in constructor,
6253 it is 0 (or out of range). */
6254 return build_zero_cst (type
);
6257 /* CTOR is CONSTRUCTOR of an aggregate or vector.
6258 Fold reference of type TYPE and size SIZE to the memory at bit OFFSET. */
6261 fold_nonarray_ctor_reference (tree type
, tree ctor
,
6262 unsigned HOST_WIDE_INT offset
,
6263 unsigned HOST_WIDE_INT size
,
6266 unsigned HOST_WIDE_INT cnt
;
6269 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
,
6272 tree byte_offset
= DECL_FIELD_OFFSET (cfield
);
6273 tree field_offset
= DECL_FIELD_BIT_OFFSET (cfield
);
6274 tree field_size
= DECL_SIZE (cfield
);
6275 offset_int bitoffset
;
6276 offset_int bitoffset_end
, access_end
;
6278 /* Variable sized objects in static constructors makes no sense,
6279 but field_size can be NULL for flexible array members. */
6280 gcc_assert (TREE_CODE (field_offset
) == INTEGER_CST
6281 && TREE_CODE (byte_offset
) == INTEGER_CST
6282 && (field_size
!= NULL_TREE
6283 ? TREE_CODE (field_size
) == INTEGER_CST
6284 : TREE_CODE (TREE_TYPE (cfield
)) == ARRAY_TYPE
));
6286 /* Compute bit offset of the field. */
6287 bitoffset
= (wi::to_offset (field_offset
)
6288 + (wi::to_offset (byte_offset
) << LOG2_BITS_PER_UNIT
));
6289 /* Compute bit offset where the field ends. */
6290 if (field_size
!= NULL_TREE
)
6291 bitoffset_end
= bitoffset
+ wi::to_offset (field_size
);
6295 access_end
= offset_int (offset
) + size
;
6297 /* Is there any overlap between [OFFSET, OFFSET+SIZE) and
6298 [BITOFFSET, BITOFFSET_END)? */
6299 if (wi::cmps (access_end
, bitoffset
) > 0
6300 && (field_size
== NULL_TREE
6301 || wi::lts_p (offset
, bitoffset_end
)))
6303 offset_int inner_offset
= offset_int (offset
) - bitoffset
;
6304 /* We do have overlap. Now see if field is large enough to
6305 cover the access. Give up for accesses spanning multiple
6307 if (wi::cmps (access_end
, bitoffset_end
) > 0)
6309 if (offset
< bitoffset
)
6311 return fold_ctor_reference (type
, cval
,
6312 inner_offset
.to_uhwi (), size
,
6316 /* When memory is not explicitely mentioned in constructor, it is 0. */
6317 return build_zero_cst (type
);
6320 /* CTOR is value initializing memory, fold reference of type TYPE and size SIZE
6321 to the memory at bit OFFSET. */
6324 fold_ctor_reference (tree type
, tree ctor
, unsigned HOST_WIDE_INT offset
,
6325 unsigned HOST_WIDE_INT size
, tree from_decl
)
6329 /* We found the field with exact match. */
6330 if (useless_type_conversion_p (type
, TREE_TYPE (ctor
))
6332 return canonicalize_constructor_val (unshare_expr (ctor
), from_decl
);
6334 /* We are at the end of walk, see if we can view convert the
6336 if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor
)) && !offset
6337 /* VIEW_CONVERT_EXPR is defined only for matching sizes. */
6338 && !compare_tree_int (TYPE_SIZE (type
), size
)
6339 && !compare_tree_int (TYPE_SIZE (TREE_TYPE (ctor
)), size
))
6341 ret
= canonicalize_constructor_val (unshare_expr (ctor
), from_decl
);
6344 ret
= fold_unary (VIEW_CONVERT_EXPR
, type
, ret
);
6346 STRIP_USELESS_TYPE_CONVERSION (ret
);
6350 /* For constants and byte-aligned/sized reads try to go through
6351 native_encode/interpret. */
6352 if (CONSTANT_CLASS_P (ctor
)
6353 && BITS_PER_UNIT
== 8
6354 && offset
% BITS_PER_UNIT
== 0
6355 && size
% BITS_PER_UNIT
== 0
6356 && size
<= MAX_BITSIZE_MODE_ANY_MODE
)
6358 unsigned char buf
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
6359 int len
= native_encode_expr (ctor
, buf
, size
/ BITS_PER_UNIT
,
6360 offset
/ BITS_PER_UNIT
);
6362 return native_interpret_expr (type
, buf
, len
);
6364 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
6367 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
6368 || TREE_CODE (TREE_TYPE (ctor
)) == VECTOR_TYPE
)
6369 return fold_array_ctor_reference (type
, ctor
, offset
, size
,
6372 return fold_nonarray_ctor_reference (type
, ctor
, offset
, size
,
6379 /* Return the tree representing the element referenced by T if T is an
6380 ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
6381 names using VALUEIZE. Return NULL_TREE otherwise. */
6384 fold_const_aggregate_ref_1 (tree t
, tree (*valueize
) (tree
))
6386 tree ctor
, idx
, base
;
6387 HOST_WIDE_INT offset
, size
, max_size
;
6391 if (TREE_THIS_VOLATILE (t
))
6395 return get_symbol_constant_value (t
);
6397 tem
= fold_read_from_constant_string (t
);
6401 switch (TREE_CODE (t
))
6404 case ARRAY_RANGE_REF
:
6405 /* Constant indexes are handled well by get_base_constructor.
6406 Only special case variable offsets.
6407 FIXME: This code can't handle nested references with variable indexes
6408 (they will be handled only by iteration of ccp). Perhaps we can bring
6409 get_ref_base_and_extent here and make it use a valueize callback. */
6410 if (TREE_CODE (TREE_OPERAND (t
, 1)) == SSA_NAME
6412 && (idx
= (*valueize
) (TREE_OPERAND (t
, 1)))
6413 && TREE_CODE (idx
) == INTEGER_CST
)
6415 tree low_bound
, unit_size
;
6417 /* If the resulting bit-offset is constant, track it. */
6418 if ((low_bound
= array_ref_low_bound (t
),
6419 TREE_CODE (low_bound
) == INTEGER_CST
)
6420 && (unit_size
= array_ref_element_size (t
),
6421 tree_fits_uhwi_p (unit_size
)))
6424 = wi::sext (wi::to_offset (idx
) - wi::to_offset (low_bound
),
6425 TYPE_PRECISION (TREE_TYPE (idx
)));
6427 if (wi::fits_shwi_p (woffset
))
6429 offset
= woffset
.to_shwi ();
6430 /* TODO: This code seems wrong, multiply then check
6431 to see if it fits. */
6432 offset
*= tree_to_uhwi (unit_size
);
6433 offset
*= BITS_PER_UNIT
;
6435 base
= TREE_OPERAND (t
, 0);
6436 ctor
= get_base_constructor (base
, &offset
, valueize
);
6437 /* Empty constructor. Always fold to 0. */
6438 if (ctor
== error_mark_node
)
6439 return build_zero_cst (TREE_TYPE (t
));
6440 /* Out of bound array access. Value is undefined,
6444 /* We can not determine ctor. */
6447 return fold_ctor_reference (TREE_TYPE (t
), ctor
, offset
,
6448 tree_to_uhwi (unit_size
)
6458 case TARGET_MEM_REF
:
6460 base
= get_ref_base_and_extent (t
, &offset
, &size
, &max_size
, &reverse
);
6461 ctor
= get_base_constructor (base
, &offset
, valueize
);
6463 /* Empty constructor. Always fold to 0. */
6464 if (ctor
== error_mark_node
)
6465 return build_zero_cst (TREE_TYPE (t
));
6466 /* We do not know precise address. */
6467 if (max_size
== -1 || max_size
!= size
)
6469 /* We can not determine ctor. */
6473 /* Out of bound array access. Value is undefined, but don't fold. */
6477 return fold_ctor_reference (TREE_TYPE (t
), ctor
, offset
, size
,
6483 tree c
= fold_const_aggregate_ref_1 (TREE_OPERAND (t
, 0), valueize
);
6484 if (c
&& TREE_CODE (c
) == COMPLEX_CST
)
6485 return fold_build1_loc (EXPR_LOCATION (t
),
6486 TREE_CODE (t
), TREE_TYPE (t
), c
);
6498 fold_const_aggregate_ref (tree t
)
6500 return fold_const_aggregate_ref_1 (t
, NULL
);
6503 /* Lookup virtual method with index TOKEN in a virtual table V
6505 Set CAN_REFER if non-NULL to false if method
6506 is not referable or if the virtual table is ill-formed (such as rewriten
6507 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
6510 gimple_get_virt_method_for_vtable (HOST_WIDE_INT token
,
6512 unsigned HOST_WIDE_INT offset
,
6515 tree vtable
= v
, init
, fn
;
6516 unsigned HOST_WIDE_INT size
;
6517 unsigned HOST_WIDE_INT elt_size
, access_index
;
6523 /* First of all double check we have virtual table. */
6524 if (!VAR_P (v
) || !DECL_VIRTUAL_P (v
))
6526 /* Pass down that we lost track of the target. */
6532 init
= ctor_for_folding (v
);
6534 /* The virtual tables should always be born with constructors
6535 and we always should assume that they are avaialble for
6536 folding. At the moment we do not stream them in all cases,
6537 but it should never happen that ctor seem unreachable. */
6539 if (init
== error_mark_node
)
6541 gcc_assert (in_lto_p
);
6542 /* Pass down that we lost track of the target. */
6547 gcc_checking_assert (TREE_CODE (TREE_TYPE (v
)) == ARRAY_TYPE
);
6548 size
= tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v
))));
6549 offset
*= BITS_PER_UNIT
;
6550 offset
+= token
* size
;
6552 /* Lookup the value in the constructor that is assumed to be array.
6553 This is equivalent to
6554 fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
6555 offset, size, NULL);
6556 but in a constant time. We expect that frontend produced a simple
6557 array without indexed initializers. */
6559 gcc_checking_assert (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
);
6560 domain_type
= TYPE_DOMAIN (TREE_TYPE (init
));
6561 gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type
)));
6562 elt_size
= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init
))));
6564 access_index
= offset
/ BITS_PER_UNIT
/ elt_size
;
6565 gcc_checking_assert (offset
% (elt_size
* BITS_PER_UNIT
) == 0);
6567 /* This code makes an assumption that there are no
6568 indexed fileds produced by C++ FE, so we can directly index the array. */
6569 if (access_index
< CONSTRUCTOR_NELTS (init
))
6571 fn
= CONSTRUCTOR_ELT (init
, access_index
)->value
;
6572 gcc_checking_assert (!CONSTRUCTOR_ELT (init
, access_index
)->index
);
6578 /* For type inconsistent program we may end up looking up virtual method
6579 in virtual table that does not contain TOKEN entries. We may overrun
6580 the virtual table and pick up a constant or RTTI info pointer.
6581 In any case the call is undefined. */
6583 || (TREE_CODE (fn
) != ADDR_EXPR
&& TREE_CODE (fn
) != FDESC_EXPR
)
6584 || TREE_CODE (TREE_OPERAND (fn
, 0)) != FUNCTION_DECL
)
6585 fn
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
6588 fn
= TREE_OPERAND (fn
, 0);
6590 /* When cgraph node is missing and function is not public, we cannot
6591 devirtualize. This can happen in WHOPR when the actual method
6592 ends up in other partition, because we found devirtualization
6593 possibility too late. */
6594 if (!can_refer_decl_in_current_unit_p (fn
, vtable
))
6605 /* Make sure we create a cgraph node for functions we'll reference.
6606 They can be non-existent if the reference comes from an entry
6607 of an external vtable for example. */
6608 cgraph_node::get_create (fn
);
6613 /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
6614 is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
6615 KNOWN_BINFO carries the binfo describing the true type of
6616 OBJ_TYPE_REF_OBJECT(REF).
6617 Set CAN_REFER if non-NULL to false if method
6618 is not referable or if the virtual table is ill-formed (such as rewriten
6619 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
6622 gimple_get_virt_method_for_binfo (HOST_WIDE_INT token
, tree known_binfo
,
6625 unsigned HOST_WIDE_INT offset
;
6628 v
= BINFO_VTABLE (known_binfo
);
6629 /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */
6633 if (!vtable_pointer_value_to_vtable (v
, &v
, &offset
))
6639 return gimple_get_virt_method_for_vtable (token
, v
, offset
, can_refer
);
6642 /* Given a pointer value T, return a simplified version of an
6643 indirection through T, or NULL_TREE if no simplification is
6644 possible. Note that the resulting type may be different from
6645 the type pointed to in the sense that it is still compatible
6646 from the langhooks point of view. */
6649 gimple_fold_indirect_ref (tree t
)
6651 tree ptype
= TREE_TYPE (t
), type
= TREE_TYPE (ptype
);
6656 subtype
= TREE_TYPE (sub
);
6657 if (!POINTER_TYPE_P (subtype
)
6658 || TYPE_REF_CAN_ALIAS_ALL (ptype
))
6661 if (TREE_CODE (sub
) == ADDR_EXPR
)
6663 tree op
= TREE_OPERAND (sub
, 0);
6664 tree optype
= TREE_TYPE (op
);
6666 if (useless_type_conversion_p (type
, optype
))
6669 /* *(foo *)&fooarray => fooarray[0] */
6670 if (TREE_CODE (optype
) == ARRAY_TYPE
6671 && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype
))) == INTEGER_CST
6672 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
6674 tree type_domain
= TYPE_DOMAIN (optype
);
6675 tree min_val
= size_zero_node
;
6676 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
6677 min_val
= TYPE_MIN_VALUE (type_domain
);
6678 if (TREE_CODE (min_val
) == INTEGER_CST
)
6679 return build4 (ARRAY_REF
, type
, op
, min_val
, NULL_TREE
, NULL_TREE
);
6681 /* *(foo *)&complexfoo => __real__ complexfoo */
6682 else if (TREE_CODE (optype
) == COMPLEX_TYPE
6683 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
6684 return fold_build1 (REALPART_EXPR
, type
, op
);
6685 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
6686 else if (TREE_CODE (optype
) == VECTOR_TYPE
6687 && useless_type_conversion_p (type
, TREE_TYPE (optype
)))
6689 tree part_width
= TYPE_SIZE (type
);
6690 tree index
= bitsize_int (0);
6691 return fold_build3 (BIT_FIELD_REF
, type
, op
, part_width
, index
);
6695 /* *(p + CST) -> ... */
6696 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
6697 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
6699 tree addr
= TREE_OPERAND (sub
, 0);
6700 tree off
= TREE_OPERAND (sub
, 1);
6704 addrtype
= TREE_TYPE (addr
);
6706 /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
6707 if (TREE_CODE (addr
) == ADDR_EXPR
6708 && TREE_CODE (TREE_TYPE (addrtype
)) == VECTOR_TYPE
6709 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (addrtype
)))
6710 && tree_fits_uhwi_p (off
))
6712 unsigned HOST_WIDE_INT offset
= tree_to_uhwi (off
);
6713 tree part_width
= TYPE_SIZE (type
);
6714 unsigned HOST_WIDE_INT part_widthi
6715 = tree_to_shwi (part_width
) / BITS_PER_UNIT
;
6716 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
6717 tree index
= bitsize_int (indexi
);
6718 if (offset
/ part_widthi
6719 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype
)))
6720 return fold_build3 (BIT_FIELD_REF
, type
, TREE_OPERAND (addr
, 0),
6724 /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
6725 if (TREE_CODE (addr
) == ADDR_EXPR
6726 && TREE_CODE (TREE_TYPE (addrtype
)) == COMPLEX_TYPE
6727 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (addrtype
))))
6729 tree size
= TYPE_SIZE_UNIT (type
);
6730 if (tree_int_cst_equal (size
, off
))
6731 return fold_build1 (IMAGPART_EXPR
, type
, TREE_OPERAND (addr
, 0));
6734 /* *(p + CST) -> MEM_REF <p, CST>. */
6735 if (TREE_CODE (addr
) != ADDR_EXPR
6736 || DECL_P (TREE_OPERAND (addr
, 0)))
6737 return fold_build2 (MEM_REF
, type
,
6739 wide_int_to_tree (ptype
, off
));
6742 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
6743 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
6744 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype
)))) == INTEGER_CST
6745 && useless_type_conversion_p (type
, TREE_TYPE (TREE_TYPE (subtype
))))
6748 tree min_val
= size_zero_node
;
6750 sub
= gimple_fold_indirect_ref (sub
);
6752 sub
= build1 (INDIRECT_REF
, TREE_TYPE (subtype
), osub
);
6753 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
6754 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
6755 min_val
= TYPE_MIN_VALUE (type_domain
);
6756 if (TREE_CODE (min_val
) == INTEGER_CST
)
6757 return build4 (ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
, NULL_TREE
);
6763 /* Return true if CODE is an operation that when operating on signed
6764 integer types involves undefined behavior on overflow and the
6765 operation can be expressed with unsigned arithmetic. */
6768 arith_code_with_undefined_signed_overflow (tree_code code
)
6776 case POINTER_PLUS_EXPR
:
6783 /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic
6784 operation that can be transformed to unsigned arithmetic by converting
6785 its operand, carrying out the operation in the corresponding unsigned
6786 type and converting the result back to the original type.
6788 Returns a sequence of statements that replace STMT and also contain
6789 a modified form of STMT itself. */
6792 rewrite_to_defined_overflow (gimple
*stmt
)
6794 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6796 fprintf (dump_file
, "rewriting stmt with undefined signed "
6798 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
6801 tree lhs
= gimple_assign_lhs (stmt
);
6802 tree type
= unsigned_type_for (TREE_TYPE (lhs
));
6803 gimple_seq stmts
= NULL
;
6804 for (unsigned i
= 1; i
< gimple_num_ops (stmt
); ++i
)
6806 tree op
= gimple_op (stmt
, i
);
6807 op
= gimple_convert (&stmts
, type
, op
);
6808 gimple_set_op (stmt
, i
, op
);
6810 gimple_assign_set_lhs (stmt
, make_ssa_name (type
, stmt
));
6811 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
6812 gimple_assign_set_rhs_code (stmt
, PLUS_EXPR
);
6813 gimple_seq_add_stmt (&stmts
, stmt
);
6814 gimple
*cvt
= gimple_build_assign (lhs
, NOP_EXPR
, gimple_assign_lhs (stmt
));
6815 gimple_seq_add_stmt (&stmts
, cvt
);
6821 /* The valueization hook we use for the gimple_build API simplification.
6822 This makes us match fold_buildN behavior by only combining with
6823 statements in the sequence(s) we are currently building. */
6826 gimple_build_valueize (tree op
)
6828 if (gimple_bb (SSA_NAME_DEF_STMT (op
)) == NULL
)
6833 /* Build the expression CODE OP0 of type TYPE with location LOC,
6834 simplifying it first if possible. Returns the built
6835 expression value and appends statements possibly defining it
6839 gimple_build (gimple_seq
*seq
, location_t loc
,
6840 enum tree_code code
, tree type
, tree op0
)
6842 tree res
= gimple_simplify (code
, type
, op0
, seq
, gimple_build_valueize
);
6845 res
= create_tmp_reg_or_ssa_name (type
);
6847 if (code
== REALPART_EXPR
6848 || code
== IMAGPART_EXPR
6849 || code
== VIEW_CONVERT_EXPR
)
6850 stmt
= gimple_build_assign (res
, code
, build1 (code
, type
, op0
));
6852 stmt
= gimple_build_assign (res
, code
, op0
);
6853 gimple_set_location (stmt
, loc
);
6854 gimple_seq_add_stmt_without_update (seq
, stmt
);
6859 /* Build the expression OP0 CODE OP1 of type TYPE with location LOC,
6860 simplifying it first if possible. Returns the built
6861 expression value and appends statements possibly defining it
6865 gimple_build (gimple_seq
*seq
, location_t loc
,
6866 enum tree_code code
, tree type
, tree op0
, tree op1
)
6868 tree res
= gimple_simplify (code
, type
, op0
, op1
, seq
, gimple_build_valueize
);
6871 res
= create_tmp_reg_or_ssa_name (type
);
6872 gimple
*stmt
= gimple_build_assign (res
, code
, op0
, op1
);
6873 gimple_set_location (stmt
, loc
);
6874 gimple_seq_add_stmt_without_update (seq
, stmt
);
6879 /* Build the expression (CODE OP0 OP1 OP2) of type TYPE with location LOC,
6880 simplifying it first if possible. Returns the built
6881 expression value and appends statements possibly defining it
6885 gimple_build (gimple_seq
*seq
, location_t loc
,
6886 enum tree_code code
, tree type
, tree op0
, tree op1
, tree op2
)
6888 tree res
= gimple_simplify (code
, type
, op0
, op1
, op2
,
6889 seq
, gimple_build_valueize
);
6892 res
= create_tmp_reg_or_ssa_name (type
);
6894 if (code
== BIT_FIELD_REF
)
6895 stmt
= gimple_build_assign (res
, code
,
6896 build3 (code
, type
, op0
, op1
, op2
));
6898 stmt
= gimple_build_assign (res
, code
, op0
, op1
, op2
);
6899 gimple_set_location (stmt
, loc
);
6900 gimple_seq_add_stmt_without_update (seq
, stmt
);
6905 /* Build the call FN (ARG0) with a result of type TYPE
6906 (or no result if TYPE is void) with location LOC,
6907 simplifying it first if possible. Returns the built
6908 expression value (or NULL_TREE if TYPE is void) and appends
6909 statements possibly defining it to SEQ. */
6912 gimple_build (gimple_seq
*seq
, location_t loc
,
6913 enum built_in_function fn
, tree type
, tree arg0
)
6915 tree res
= gimple_simplify (fn
, type
, arg0
, seq
, gimple_build_valueize
);
6918 tree decl
= builtin_decl_implicit (fn
);
6919 gimple
*stmt
= gimple_build_call (decl
, 1, arg0
);
6920 if (!VOID_TYPE_P (type
))
6922 res
= create_tmp_reg_or_ssa_name (type
);
6923 gimple_call_set_lhs (stmt
, res
);
6925 gimple_set_location (stmt
, loc
);
6926 gimple_seq_add_stmt_without_update (seq
, stmt
);
6931 /* Build the call FN (ARG0, ARG1) with a result of type TYPE
6932 (or no result if TYPE is void) with location LOC,
6933 simplifying it first if possible. Returns the built
6934 expression value (or NULL_TREE if TYPE is void) and appends
6935 statements possibly defining it to SEQ. */
6938 gimple_build (gimple_seq
*seq
, location_t loc
,
6939 enum built_in_function fn
, tree type
, tree arg0
, tree arg1
)
6941 tree res
= gimple_simplify (fn
, type
, arg0
, arg1
, seq
, gimple_build_valueize
);
6944 tree decl
= builtin_decl_implicit (fn
);
6945 gimple
*stmt
= gimple_build_call (decl
, 2, arg0
, arg1
);
6946 if (!VOID_TYPE_P (type
))
6948 res
= create_tmp_reg_or_ssa_name (type
);
6949 gimple_call_set_lhs (stmt
, res
);
6951 gimple_set_location (stmt
, loc
);
6952 gimple_seq_add_stmt_without_update (seq
, stmt
);
6957 /* Build the call FN (ARG0, ARG1, ARG2) with a result of type TYPE
6958 (or no result if TYPE is void) with location LOC,
6959 simplifying it first if possible. Returns the built
6960 expression value (or NULL_TREE if TYPE is void) and appends
6961 statements possibly defining it to SEQ. */
6964 gimple_build (gimple_seq
*seq
, location_t loc
,
6965 enum built_in_function fn
, tree type
,
6966 tree arg0
, tree arg1
, tree arg2
)
6968 tree res
= gimple_simplify (fn
, type
, arg0
, arg1
, arg2
,
6969 seq
, gimple_build_valueize
);
6972 tree decl
= builtin_decl_implicit (fn
);
6973 gimple
*stmt
= gimple_build_call (decl
, 3, arg0
, arg1
, arg2
);
6974 if (!VOID_TYPE_P (type
))
6976 res
= create_tmp_reg_or_ssa_name (type
);
6977 gimple_call_set_lhs (stmt
, res
);
6979 gimple_set_location (stmt
, loc
);
6980 gimple_seq_add_stmt_without_update (seq
, stmt
);
6985 /* Build the conversion (TYPE) OP with a result of type TYPE
6986 with location LOC if such conversion is neccesary in GIMPLE,
6987 simplifying it first.
6988 Returns the built expression value and appends
6989 statements possibly defining it to SEQ. */
6992 gimple_convert (gimple_seq
*seq
, location_t loc
, tree type
, tree op
)
6994 if (useless_type_conversion_p (type
, TREE_TYPE (op
)))
6996 return gimple_build (seq
, loc
, NOP_EXPR
, type
, op
);
6999 /* Build the conversion (ptrofftype) OP with a result of a type
7000 compatible with ptrofftype with location LOC if such conversion
7001 is neccesary in GIMPLE, simplifying it first.
7002 Returns the built expression value and appends
7003 statements possibly defining it to SEQ. */
7006 gimple_convert_to_ptrofftype (gimple_seq
*seq
, location_t loc
, tree op
)
7008 if (ptrofftype_p (TREE_TYPE (op
)))
7010 return gimple_convert (seq
, loc
, sizetype
, op
);
7013 /* Return true if the result of assignment STMT is known to be non-negative.
7014 If the return value is based on the assumption that signed overflow is
7015 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7016 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7019 gimple_assign_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
7022 enum tree_code code
= gimple_assign_rhs_code (stmt
);
7023 switch (get_gimple_rhs_class (code
))
7025 case GIMPLE_UNARY_RHS
:
7026 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
7027 gimple_expr_type (stmt
),
7028 gimple_assign_rhs1 (stmt
),
7029 strict_overflow_p
, depth
);
7030 case GIMPLE_BINARY_RHS
:
7031 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
7032 gimple_expr_type (stmt
),
7033 gimple_assign_rhs1 (stmt
),
7034 gimple_assign_rhs2 (stmt
),
7035 strict_overflow_p
, depth
);
7036 case GIMPLE_TERNARY_RHS
:
7038 case GIMPLE_SINGLE_RHS
:
7039 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
7040 strict_overflow_p
, depth
);
7041 case GIMPLE_INVALID_RHS
:
7047 /* Return true if return value of call STMT is known to be non-negative.
7048 If the return value is based on the assumption that signed overflow is
7049 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7050 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7053 gimple_call_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
7056 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
7057 gimple_call_arg (stmt
, 0) : NULL_TREE
;
7058 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
7059 gimple_call_arg (stmt
, 1) : NULL_TREE
;
7061 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
7062 gimple_call_combined_fn (stmt
),
7065 strict_overflow_p
, depth
);
7068 /* Return true if return value of call STMT is known to be non-negative.
7069 If the return value is based on the assumption that signed overflow is
7070 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7071 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7074 gimple_phi_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
7077 for (unsigned i
= 0; i
< gimple_phi_num_args (stmt
); ++i
)
7079 tree arg
= gimple_phi_arg_def (stmt
, i
);
7080 if (!tree_single_nonnegative_warnv_p (arg
, strict_overflow_p
, depth
+ 1))
7086 /* Return true if STMT is known to compute a non-negative value.
7087 If the return value is based on the assumption that signed overflow is
7088 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7089 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7092 gimple_stmt_nonnegative_warnv_p (gimple
*stmt
, bool *strict_overflow_p
,
7095 switch (gimple_code (stmt
))
7098 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
,
7101 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
,
7104 return gimple_phi_nonnegative_warnv_p (stmt
, strict_overflow_p
,
7111 /* Return true if the floating-point value computed by assignment STMT
7112 is known to have an integer value. We also allow +Inf, -Inf and NaN
7113 to be considered integer values. Return false for signaling NaN.
7115 DEPTH is the current nesting depth of the query. */
7118 gimple_assign_integer_valued_real_p (gimple
*stmt
, int depth
)
7120 enum tree_code code
= gimple_assign_rhs_code (stmt
);
7121 switch (get_gimple_rhs_class (code
))
7123 case GIMPLE_UNARY_RHS
:
7124 return integer_valued_real_unary_p (gimple_assign_rhs_code (stmt
),
7125 gimple_assign_rhs1 (stmt
), depth
);
7126 case GIMPLE_BINARY_RHS
:
7127 return integer_valued_real_binary_p (gimple_assign_rhs_code (stmt
),
7128 gimple_assign_rhs1 (stmt
),
7129 gimple_assign_rhs2 (stmt
), depth
);
7130 case GIMPLE_TERNARY_RHS
:
7132 case GIMPLE_SINGLE_RHS
:
7133 return integer_valued_real_single_p (gimple_assign_rhs1 (stmt
), depth
);
7134 case GIMPLE_INVALID_RHS
:
7140 /* Return true if the floating-point value computed by call STMT is known
7141 to have an integer value. We also allow +Inf, -Inf and NaN to be
7142 considered integer values. Return false for signaling NaN.
7144 DEPTH is the current nesting depth of the query. */
7147 gimple_call_integer_valued_real_p (gimple
*stmt
, int depth
)
7149 tree arg0
= (gimple_call_num_args (stmt
) > 0
7150 ? gimple_call_arg (stmt
, 0)
7152 tree arg1
= (gimple_call_num_args (stmt
) > 1
7153 ? gimple_call_arg (stmt
, 1)
7155 return integer_valued_real_call_p (gimple_call_combined_fn (stmt
),
7159 /* Return true if the floating-point result of phi STMT is known to have
7160 an integer value. We also allow +Inf, -Inf and NaN to be considered
7161 integer values. Return false for signaling NaN.
7163 DEPTH is the current nesting depth of the query. */
7166 gimple_phi_integer_valued_real_p (gimple
*stmt
, int depth
)
7168 for (unsigned i
= 0; i
< gimple_phi_num_args (stmt
); ++i
)
7170 tree arg
= gimple_phi_arg_def (stmt
, i
);
7171 if (!integer_valued_real_single_p (arg
, depth
+ 1))
7177 /* Return true if the floating-point value computed by STMT is known
7178 to have an integer value. We also allow +Inf, -Inf and NaN to be
7179 considered integer values. Return false for signaling NaN.
7181 DEPTH is the current nesting depth of the query. */
7184 gimple_stmt_integer_valued_real_p (gimple
*stmt
, int depth
)
7186 switch (gimple_code (stmt
))
7189 return gimple_assign_integer_valued_real_p (stmt
, depth
);
7191 return gimple_call_integer_valued_real_p (stmt
, depth
);
7193 return gimple_phi_integer_valued_real_p (stmt
, depth
);