2017-08-28 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / gimple-fold.c
blob0f3227da4f484bfad6ceff2b5d25b16286ee0648
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
10 later version.
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
15 for more details.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "target.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "predict.h"
30 #include "ssa.h"
31 #include "cgraph.h"
32 #include "gimple-pretty-print.h"
33 #include "fold-const.h"
34 #include "stmt.h"
35 #include "expr.h"
36 #include "stor-layout.h"
37 #include "dumpfile.h"
38 #include "gimple-fold.h"
39 #include "gimplify.h"
40 #include "gimple-iterator.h"
41 #include "tree-into-ssa.h"
42 #include "tree-dfa.h"
43 #include "tree-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"
49 #include "dbgcnt.h"
50 #include "builtins.h"
51 #include "tree-eh.h"
52 #include "gimple-match.h"
53 #include "gomp-constants.h"
54 #include "optabs-query.h"
55 #include "omp-general.h"
56 #include "ipa-chkp.h"
57 #include "tree-cfg.h"
58 #include "fold-const-call.h"
60 /* Return true when DECL can be referenced from current unit.
61 FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
62 We can get declarations that are not possible to reference for various
63 reasons:
65 1) When analyzing C++ virtual tables.
66 C++ virtual tables do have known constructors even
67 when they are keyed to other compilation unit.
68 Those tables can contain pointers to methods and vars
69 in other units. Those methods have both STATIC and EXTERNAL
70 set.
71 2) In WHOPR mode devirtualization might lead to reference
72 to method that was partitioned elsehwere.
73 In this case we have static VAR_DECL or FUNCTION_DECL
74 that has no corresponding callgraph/varpool node
75 declaring the body.
76 3) COMDAT functions referred by external vtables that
77 we devirtualize only during final compilation stage.
78 At this time we already decided that we will not output
79 the function body and thus we can't reference the symbol
80 directly. */
82 static bool
83 can_refer_decl_in_current_unit_p (tree decl, tree from_decl)
85 varpool_node *vnode;
86 struct cgraph_node *node;
87 symtab_node *snode;
89 if (DECL_ABSTRACT_P (decl))
90 return false;
92 /* We are concerned only about static/external vars and functions. */
93 if ((!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
94 || !VAR_OR_FUNCTION_DECL_P (decl))
95 return true;
97 /* Static objects can be referred only if they was not optimized out yet. */
98 if (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
100 /* Before we start optimizing unreachable code we can be sure all
101 static objects are defined. */
102 if (symtab->function_flags_ready)
103 return true;
104 snode = symtab_node::get (decl);
105 if (!snode || !snode->definition)
106 return false;
107 node = dyn_cast <cgraph_node *> (snode);
108 return !node || !node->global.inlined_to;
111 /* We will later output the initializer, so we can refer to it.
112 So we are concerned only when DECL comes from initializer of
113 external var or var that has been optimized out. */
114 if (!from_decl
115 || !VAR_P (from_decl)
116 || (!DECL_EXTERNAL (from_decl)
117 && (vnode = varpool_node::get (from_decl)) != NULL
118 && vnode->definition)
119 || (flag_ltrans
120 && (vnode = varpool_node::get (from_decl)) != NULL
121 && vnode->in_other_partition))
122 return true;
123 /* We are folding reference from external vtable. The vtable may reffer
124 to a symbol keyed to other compilation unit. The other compilation
125 unit may be in separate DSO and the symbol may be hidden. */
126 if (DECL_VISIBILITY_SPECIFIED (decl)
127 && DECL_EXTERNAL (decl)
128 && DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT
129 && (!(snode = symtab_node::get (decl)) || !snode->in_other_partition))
130 return false;
131 /* When function is public, we always can introduce new reference.
132 Exception are the COMDAT functions where introducing a direct
133 reference imply need to include function body in the curren tunit. */
134 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
135 return true;
136 /* We have COMDAT. We are going to check if we still have definition
137 or if the definition is going to be output in other partition.
138 Bypass this when gimplifying; all needed functions will be produced.
140 As observed in PR20991 for already optimized out comdat virtual functions
141 it may be tempting to not necessarily give up because the copy will be
142 output elsewhere when corresponding vtable is output.
143 This is however not possible - ABI specify that COMDATs are output in
144 units where they are used and when the other unit was compiled with LTO
145 it is possible that vtable was kept public while the function itself
146 was privatized. */
147 if (!symtab->function_flags_ready)
148 return true;
150 snode = symtab_node::get (decl);
151 if (!snode
152 || ((!snode->definition || DECL_EXTERNAL (decl))
153 && (!snode->in_other_partition
154 || (!snode->forced_by_abi && !snode->force_output))))
155 return false;
156 node = dyn_cast <cgraph_node *> (snode);
157 return !node || !node->global.inlined_to;
160 /* Create a temporary for TYPE for a statement STMT. If the current function
161 is in SSA form, a SSA name is created. Otherwise a temporary register
162 is made. */
164 static tree
165 create_tmp_reg_or_ssa_name (tree type, gimple *stmt = NULL)
167 if (gimple_in_ssa_p (cfun))
168 return make_ssa_name (type, stmt);
169 else
170 return create_tmp_reg (type);
173 /* CVAL is value taken from DECL_INITIAL of variable. Try to transform it into
174 acceptable form for is_gimple_min_invariant.
175 FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL. */
177 tree
178 canonicalize_constructor_val (tree cval, tree from_decl)
180 tree orig_cval = cval;
181 STRIP_NOPS (cval);
182 if (TREE_CODE (cval) == POINTER_PLUS_EXPR
183 && TREE_CODE (TREE_OPERAND (cval, 1)) == INTEGER_CST)
185 tree ptr = TREE_OPERAND (cval, 0);
186 if (is_gimple_min_invariant (ptr))
187 cval = build1_loc (EXPR_LOCATION (cval),
188 ADDR_EXPR, TREE_TYPE (ptr),
189 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (ptr)),
190 ptr,
191 fold_convert (ptr_type_node,
192 TREE_OPERAND (cval, 1))));
194 if (TREE_CODE (cval) == ADDR_EXPR)
196 tree base = NULL_TREE;
197 if (TREE_CODE (TREE_OPERAND (cval, 0)) == COMPOUND_LITERAL_EXPR)
199 base = COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval, 0));
200 if (base)
201 TREE_OPERAND (cval, 0) = base;
203 else
204 base = get_base_address (TREE_OPERAND (cval, 0));
205 if (!base)
206 return NULL_TREE;
208 if (VAR_OR_FUNCTION_DECL_P (base)
209 && !can_refer_decl_in_current_unit_p (base, from_decl))
210 return NULL_TREE;
211 if (TREE_TYPE (base) == error_mark_node)
212 return NULL_TREE;
213 if (VAR_P (base))
214 TREE_ADDRESSABLE (base) = 1;
215 else if (TREE_CODE (base) == FUNCTION_DECL)
217 /* Make sure we create a cgraph node for functions we'll reference.
218 They can be non-existent if the reference comes from an entry
219 of an external vtable for example. */
220 cgraph_node::get_create (base);
222 /* Fixup types in global initializers. */
223 if (TREE_TYPE (TREE_TYPE (cval)) != TREE_TYPE (TREE_OPERAND (cval, 0)))
224 cval = build_fold_addr_expr (TREE_OPERAND (cval, 0));
226 if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval)))
227 cval = fold_convert (TREE_TYPE (orig_cval), cval);
228 return cval;
230 if (TREE_OVERFLOW_P (cval))
231 return drop_tree_overflow (cval);
232 return orig_cval;
235 /* If SYM is a constant variable with known value, return the value.
236 NULL_TREE is returned otherwise. */
238 tree
239 get_symbol_constant_value (tree sym)
241 tree val = ctor_for_folding (sym);
242 if (val != error_mark_node)
244 if (val)
246 val = canonicalize_constructor_val (unshare_expr (val), sym);
247 if (val && is_gimple_min_invariant (val))
248 return val;
249 else
250 return NULL_TREE;
252 /* Variables declared 'const' without an initializer
253 have zero as the initializer if they may not be
254 overridden at link or run time. */
255 if (!val
256 && is_gimple_reg_type (TREE_TYPE (sym)))
257 return build_zero_cst (TREE_TYPE (sym));
260 return NULL_TREE;
265 /* Subroutine of fold_stmt. We perform several simplifications of the
266 memory reference tree EXPR and make sure to re-gimplify them properly
267 after propagation of constant addresses. IS_LHS is true if the
268 reference is supposed to be an lvalue. */
270 static tree
271 maybe_fold_reference (tree expr, bool is_lhs)
273 tree result;
275 if ((TREE_CODE (expr) == VIEW_CONVERT_EXPR
276 || TREE_CODE (expr) == REALPART_EXPR
277 || TREE_CODE (expr) == IMAGPART_EXPR)
278 && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
279 return fold_unary_loc (EXPR_LOCATION (expr),
280 TREE_CODE (expr),
281 TREE_TYPE (expr),
282 TREE_OPERAND (expr, 0));
283 else if (TREE_CODE (expr) == BIT_FIELD_REF
284 && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
285 return fold_ternary_loc (EXPR_LOCATION (expr),
286 TREE_CODE (expr),
287 TREE_TYPE (expr),
288 TREE_OPERAND (expr, 0),
289 TREE_OPERAND (expr, 1),
290 TREE_OPERAND (expr, 2));
292 if (!is_lhs
293 && (result = fold_const_aggregate_ref (expr))
294 && is_gimple_min_invariant (result))
295 return result;
297 return NULL_TREE;
301 /* Attempt to fold an assignment statement pointed-to by SI. Returns a
302 replacement rhs for the statement or NULL_TREE if no simplification
303 could be made. It is assumed that the operands have been previously
304 folded. */
306 static tree
307 fold_gimple_assign (gimple_stmt_iterator *si)
309 gimple *stmt = gsi_stmt (*si);
310 enum tree_code subcode = gimple_assign_rhs_code (stmt);
311 location_t loc = gimple_location (stmt);
313 tree result = NULL_TREE;
315 switch (get_gimple_rhs_class (subcode))
317 case GIMPLE_SINGLE_RHS:
319 tree rhs = gimple_assign_rhs1 (stmt);
321 if (TREE_CLOBBER_P (rhs))
322 return NULL_TREE;
324 if (REFERENCE_CLASS_P (rhs))
325 return maybe_fold_reference (rhs, false);
327 else if (TREE_CODE (rhs) == OBJ_TYPE_REF)
329 tree val = OBJ_TYPE_REF_EXPR (rhs);
330 if (is_gimple_min_invariant (val))
331 return val;
332 else if (flag_devirtualize && virtual_method_call_p (rhs))
334 bool final;
335 vec <cgraph_node *>targets
336 = possible_polymorphic_call_targets (rhs, stmt, &final);
337 if (final && targets.length () <= 1 && dbg_cnt (devirt))
339 if (dump_enabled_p ())
341 location_t loc = gimple_location_safe (stmt);
342 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
343 "resolving virtual function address "
344 "reference to function %s\n",
345 targets.length () == 1
346 ? targets[0]->name ()
347 : "NULL");
349 if (targets.length () == 1)
351 val = fold_convert (TREE_TYPE (val),
352 build_fold_addr_expr_loc
353 (loc, targets[0]->decl));
354 STRIP_USELESS_TYPE_CONVERSION (val);
356 else
357 /* We can not use __builtin_unreachable here because it
358 can not have address taken. */
359 val = build_int_cst (TREE_TYPE (val), 0);
360 return val;
365 else if (TREE_CODE (rhs) == ADDR_EXPR)
367 tree ref = TREE_OPERAND (rhs, 0);
368 tree tem = maybe_fold_reference (ref, true);
369 if (tem
370 && TREE_CODE (tem) == MEM_REF
371 && integer_zerop (TREE_OPERAND (tem, 1)))
372 result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (tem, 0));
373 else if (tem)
374 result = fold_convert (TREE_TYPE (rhs),
375 build_fold_addr_expr_loc (loc, tem));
376 else if (TREE_CODE (ref) == MEM_REF
377 && integer_zerop (TREE_OPERAND (ref, 1)))
378 result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (ref, 0));
380 if (result)
382 /* Strip away useless type conversions. Both the
383 NON_LVALUE_EXPR that may have been added by fold, and
384 "useless" type conversions that might now be apparent
385 due to propagation. */
386 STRIP_USELESS_TYPE_CONVERSION (result);
388 if (result != rhs && valid_gimple_rhs_p (result))
389 return result;
393 else if (TREE_CODE (rhs) == CONSTRUCTOR
394 && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE)
396 /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */
397 unsigned i;
398 tree val;
400 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
401 if (! CONSTANT_CLASS_P (val))
402 return NULL_TREE;
404 return build_vector_from_ctor (TREE_TYPE (rhs),
405 CONSTRUCTOR_ELTS (rhs));
408 else if (DECL_P (rhs))
409 return get_symbol_constant_value (rhs);
411 break;
413 case GIMPLE_UNARY_RHS:
414 break;
416 case GIMPLE_BINARY_RHS:
417 break;
419 case GIMPLE_TERNARY_RHS:
420 result = fold_ternary_loc (loc, subcode,
421 TREE_TYPE (gimple_assign_lhs (stmt)),
422 gimple_assign_rhs1 (stmt),
423 gimple_assign_rhs2 (stmt),
424 gimple_assign_rhs3 (stmt));
426 if (result)
428 STRIP_USELESS_TYPE_CONVERSION (result);
429 if (valid_gimple_rhs_p (result))
430 return result;
432 break;
434 case GIMPLE_INVALID_RHS:
435 gcc_unreachable ();
438 return NULL_TREE;
442 /* Replace a statement at *SI_P with a sequence of statements in STMTS,
443 adjusting the replacement stmts location and virtual operands.
444 If the statement has a lhs the last stmt in the sequence is expected
445 to assign to that lhs. */
447 static void
448 gsi_replace_with_seq_vops (gimple_stmt_iterator *si_p, gimple_seq stmts)
450 gimple *stmt = gsi_stmt (*si_p);
452 if (gimple_has_location (stmt))
453 annotate_all_with_location (stmts, gimple_location (stmt));
455 /* First iterate over the replacement statements backward, assigning
456 virtual operands to their defining statements. */
457 gimple *laststore = NULL;
458 for (gimple_stmt_iterator i = gsi_last (stmts);
459 !gsi_end_p (i); gsi_prev (&i))
461 gimple *new_stmt = gsi_stmt (i);
462 if ((gimple_assign_single_p (new_stmt)
463 && !is_gimple_reg (gimple_assign_lhs (new_stmt)))
464 || (is_gimple_call (new_stmt)
465 && (gimple_call_flags (new_stmt)
466 & (ECF_NOVOPS | ECF_PURE | ECF_CONST | ECF_NORETURN)) == 0))
468 tree vdef;
469 if (!laststore)
470 vdef = gimple_vdef (stmt);
471 else
472 vdef = make_ssa_name (gimple_vop (cfun), new_stmt);
473 gimple_set_vdef (new_stmt, vdef);
474 if (vdef && TREE_CODE (vdef) == SSA_NAME)
475 SSA_NAME_DEF_STMT (vdef) = new_stmt;
476 laststore = new_stmt;
480 /* Second iterate over the statements forward, assigning virtual
481 operands to their uses. */
482 tree reaching_vuse = gimple_vuse (stmt);
483 for (gimple_stmt_iterator i = gsi_start (stmts);
484 !gsi_end_p (i); gsi_next (&i))
486 gimple *new_stmt = gsi_stmt (i);
487 /* If the new statement possibly has a VUSE, update it with exact SSA
488 name we know will reach this one. */
489 if (gimple_has_mem_ops (new_stmt))
490 gimple_set_vuse (new_stmt, reaching_vuse);
491 gimple_set_modified (new_stmt, true);
492 if (gimple_vdef (new_stmt))
493 reaching_vuse = gimple_vdef (new_stmt);
496 /* If the new sequence does not do a store release the virtual
497 definition of the original statement. */
498 if (reaching_vuse
499 && reaching_vuse == gimple_vuse (stmt))
501 tree vdef = gimple_vdef (stmt);
502 if (vdef
503 && TREE_CODE (vdef) == SSA_NAME)
505 unlink_stmt_vdef (stmt);
506 release_ssa_name (vdef);
510 /* Finally replace the original statement with the sequence. */
511 gsi_replace_with_seq (si_p, stmts, false);
514 /* Convert EXPR into a GIMPLE value suitable for substitution on the
515 RHS of an assignment. Insert the necessary statements before
516 iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL
517 is replaced. If the call is expected to produces a result, then it
518 is replaced by an assignment of the new RHS to the result variable.
519 If the result is to be ignored, then the call is replaced by a
520 GIMPLE_NOP. A proper VDEF chain is retained by making the first
521 VUSE and the last VDEF of the whole sequence be the same as the replaced
522 statement and using new SSA names for stores in between. */
524 void
525 gimplify_and_update_call_from_tree (gimple_stmt_iterator *si_p, tree expr)
527 tree lhs;
528 gimple *stmt, *new_stmt;
529 gimple_stmt_iterator i;
530 gimple_seq stmts = NULL;
532 stmt = gsi_stmt (*si_p);
534 gcc_assert (is_gimple_call (stmt));
536 push_gimplify_context (gimple_in_ssa_p (cfun));
538 lhs = gimple_call_lhs (stmt);
539 if (lhs == NULL_TREE)
541 gimplify_and_add (expr, &stmts);
542 /* We can end up with folding a memcpy of an empty class assignment
543 which gets optimized away by C++ gimplification. */
544 if (gimple_seq_empty_p (stmts))
546 pop_gimplify_context (NULL);
547 if (gimple_in_ssa_p (cfun))
549 unlink_stmt_vdef (stmt);
550 release_defs (stmt);
552 gsi_replace (si_p, gimple_build_nop (), false);
553 return;
556 else
558 tree tmp = force_gimple_operand (expr, &stmts, false, NULL_TREE);
559 new_stmt = gimple_build_assign (lhs, tmp);
560 i = gsi_last (stmts);
561 gsi_insert_after_without_update (&i, new_stmt,
562 GSI_CONTINUE_LINKING);
565 pop_gimplify_context (NULL);
567 gsi_replace_with_seq_vops (si_p, stmts);
571 /* Replace the call at *GSI with the gimple value VAL. */
573 static void
574 replace_call_with_value (gimple_stmt_iterator *gsi, tree val)
576 gimple *stmt = gsi_stmt (*gsi);
577 tree lhs = gimple_call_lhs (stmt);
578 gimple *repl;
579 if (lhs)
581 if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (val)))
582 val = fold_convert (TREE_TYPE (lhs), val);
583 repl = gimple_build_assign (lhs, val);
585 else
586 repl = gimple_build_nop ();
587 tree vdef = gimple_vdef (stmt);
588 if (vdef && TREE_CODE (vdef) == SSA_NAME)
590 unlink_stmt_vdef (stmt);
591 release_ssa_name (vdef);
593 gsi_replace (gsi, repl, false);
596 /* Replace the call at *GSI with the new call REPL and fold that
597 again. */
599 static void
600 replace_call_with_call_and_fold (gimple_stmt_iterator *gsi, gimple *repl)
602 gimple *stmt = gsi_stmt (*gsi);
603 gimple_call_set_lhs (repl, gimple_call_lhs (stmt));
604 gimple_set_location (repl, gimple_location (stmt));
605 if (gimple_vdef (stmt)
606 && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
608 gimple_set_vdef (repl, gimple_vdef (stmt));
609 SSA_NAME_DEF_STMT (gimple_vdef (repl)) = repl;
611 if (gimple_vuse (stmt))
612 gimple_set_vuse (repl, gimple_vuse (stmt));
613 gsi_replace (gsi, repl, false);
614 fold_stmt (gsi);
617 /* Return true if VAR is a VAR_DECL or a component thereof. */
619 static bool
620 var_decl_component_p (tree var)
622 tree inner = var;
623 while (handled_component_p (inner))
624 inner = TREE_OPERAND (inner, 0);
625 return SSA_VAR_P (inner);
628 /* Fold function call to builtin mem{{,p}cpy,move}. Return
629 false if no simplification can be made.
630 If ENDP is 0, return DEST (like memcpy).
631 If ENDP is 1, return DEST+LEN (like mempcpy).
632 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
633 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
634 (memmove). */
636 static bool
637 gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi,
638 tree dest, tree src, int endp)
640 gimple *stmt = gsi_stmt (*gsi);
641 tree lhs = gimple_call_lhs (stmt);
642 tree len = gimple_call_arg (stmt, 2);
643 tree destvar, srcvar;
644 location_t loc = gimple_location (stmt);
646 /* If the LEN parameter is zero, return DEST. */
647 if (integer_zerop (len))
649 gimple *repl;
650 if (gimple_call_lhs (stmt))
651 repl = gimple_build_assign (gimple_call_lhs (stmt), dest);
652 else
653 repl = gimple_build_nop ();
654 tree vdef = gimple_vdef (stmt);
655 if (vdef && TREE_CODE (vdef) == SSA_NAME)
657 unlink_stmt_vdef (stmt);
658 release_ssa_name (vdef);
660 gsi_replace (gsi, repl, false);
661 return true;
664 /* If SRC and DEST are the same (and not volatile), return
665 DEST{,+LEN,+LEN-1}. */
666 if (operand_equal_p (src, dest, 0))
668 unlink_stmt_vdef (stmt);
669 if (gimple_vdef (stmt) && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
670 release_ssa_name (gimple_vdef (stmt));
671 if (!lhs)
673 gsi_replace (gsi, gimple_build_nop (), false);
674 return true;
676 goto done;
678 else
680 tree srctype, desttype;
681 unsigned int src_align, dest_align;
682 tree off0;
684 /* Inlining of memcpy/memmove may cause bounds lost (if we copy
685 pointers as wide integer) and also may result in huge function
686 size because of inlined bounds copy. Thus don't inline for
687 functions we want to instrument. */
688 if (flag_check_pointer_bounds
689 && chkp_instrumentable_p (cfun->decl)
690 /* Even if data may contain pointers we can inline if copy
691 less than a pointer size. */
692 && (!tree_fits_uhwi_p (len)
693 || compare_tree_int (len, POINTER_SIZE_UNITS) >= 0))
694 return false;
696 /* Build accesses at offset zero with a ref-all character type. */
697 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
698 ptr_mode, true), 0);
700 /* If we can perform the copy efficiently with first doing all loads
701 and then all stores inline it that way. Currently efficiently
702 means that we can load all the memory into a single integer
703 register which is what MOVE_MAX gives us. */
704 src_align = get_pointer_alignment (src);
705 dest_align = get_pointer_alignment (dest);
706 if (tree_fits_uhwi_p (len)
707 && compare_tree_int (len, MOVE_MAX) <= 0
708 /* ??? Don't transform copies from strings with known length this
709 confuses the tree-ssa-strlen.c. This doesn't handle
710 the case in gcc.dg/strlenopt-8.c which is XFAILed for that
711 reason. */
712 && !c_strlen (src, 2))
714 unsigned ilen = tree_to_uhwi (len);
715 if (pow2p_hwi (ilen))
717 tree type = lang_hooks.types.type_for_size (ilen * 8, 1);
718 if (type
719 && TYPE_MODE (type) != BLKmode
720 && (GET_MODE_SIZE (TYPE_MODE (type)) * BITS_PER_UNIT
721 == ilen * 8)
722 /* If the destination pointer is not aligned we must be able
723 to emit an unaligned store. */
724 && (dest_align >= GET_MODE_ALIGNMENT (TYPE_MODE (type))
725 || !SLOW_UNALIGNED_ACCESS (TYPE_MODE (type), dest_align)
726 || (optab_handler (movmisalign_optab, TYPE_MODE (type))
727 != CODE_FOR_nothing)))
729 tree srctype = type;
730 tree desttype = type;
731 if (src_align < GET_MODE_ALIGNMENT (TYPE_MODE (type)))
732 srctype = build_aligned_type (type, src_align);
733 tree srcmem = fold_build2 (MEM_REF, srctype, src, off0);
734 tree tem = fold_const_aggregate_ref (srcmem);
735 if (tem)
736 srcmem = tem;
737 else if (src_align < GET_MODE_ALIGNMENT (TYPE_MODE (type))
738 && SLOW_UNALIGNED_ACCESS (TYPE_MODE (type),
739 src_align)
740 && (optab_handler (movmisalign_optab,
741 TYPE_MODE (type))
742 == CODE_FOR_nothing))
743 srcmem = NULL_TREE;
744 if (srcmem)
746 gimple *new_stmt;
747 if (is_gimple_reg_type (TREE_TYPE (srcmem)))
749 new_stmt = gimple_build_assign (NULL_TREE, srcmem);
750 srcmem
751 = create_tmp_reg_or_ssa_name (TREE_TYPE (srcmem),
752 new_stmt);
753 gimple_assign_set_lhs (new_stmt, srcmem);
754 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
755 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
757 if (dest_align < GET_MODE_ALIGNMENT (TYPE_MODE (type)))
758 desttype = build_aligned_type (type, dest_align);
759 new_stmt
760 = gimple_build_assign (fold_build2 (MEM_REF, desttype,
761 dest, off0),
762 srcmem);
763 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
764 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
765 if (gimple_vdef (new_stmt)
766 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
767 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
768 if (!lhs)
770 gsi_replace (gsi, new_stmt, false);
771 return true;
773 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
774 goto done;
780 if (endp == 3)
782 /* Both DEST and SRC must be pointer types.
783 ??? This is what old code did. Is the testing for pointer types
784 really mandatory?
786 If either SRC is readonly or length is 1, we can use memcpy. */
787 if (!dest_align || !src_align)
788 return false;
789 if (readonly_data_expr (src)
790 || (tree_fits_uhwi_p (len)
791 && (MIN (src_align, dest_align) / BITS_PER_UNIT
792 >= tree_to_uhwi (len))))
794 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
795 if (!fn)
796 return false;
797 gimple_call_set_fndecl (stmt, fn);
798 gimple_call_set_arg (stmt, 0, dest);
799 gimple_call_set_arg (stmt, 1, src);
800 fold_stmt (gsi);
801 return true;
804 /* If *src and *dest can't overlap, optimize into memcpy as well. */
805 if (TREE_CODE (src) == ADDR_EXPR
806 && TREE_CODE (dest) == ADDR_EXPR)
808 tree src_base, dest_base, fn;
809 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
810 HOST_WIDE_INT maxsize;
812 srcvar = TREE_OPERAND (src, 0);
813 src_base = get_addr_base_and_unit_offset (srcvar, &src_offset);
814 if (src_base == NULL)
815 src_base = srcvar;
816 destvar = TREE_OPERAND (dest, 0);
817 dest_base = get_addr_base_and_unit_offset (destvar,
818 &dest_offset);
819 if (dest_base == NULL)
820 dest_base = destvar;
821 if (tree_fits_uhwi_p (len))
822 maxsize = tree_to_uhwi (len);
823 else
824 maxsize = -1;
825 if (SSA_VAR_P (src_base)
826 && SSA_VAR_P (dest_base))
828 if (operand_equal_p (src_base, dest_base, 0)
829 && ranges_overlap_p (src_offset, maxsize,
830 dest_offset, maxsize))
831 return false;
833 else if (TREE_CODE (src_base) == MEM_REF
834 && TREE_CODE (dest_base) == MEM_REF)
836 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
837 TREE_OPERAND (dest_base, 0), 0))
838 return false;
839 offset_int off = mem_ref_offset (src_base) + src_offset;
840 if (!wi::fits_shwi_p (off))
841 return false;
842 src_offset = off.to_shwi ();
844 off = mem_ref_offset (dest_base) + dest_offset;
845 if (!wi::fits_shwi_p (off))
846 return false;
847 dest_offset = off.to_shwi ();
848 if (ranges_overlap_p (src_offset, maxsize,
849 dest_offset, maxsize))
850 return false;
852 else
853 return false;
855 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
856 if (!fn)
857 return false;
858 gimple_call_set_fndecl (stmt, fn);
859 gimple_call_set_arg (stmt, 0, dest);
860 gimple_call_set_arg (stmt, 1, src);
861 fold_stmt (gsi);
862 return true;
865 /* If the destination and source do not alias optimize into
866 memcpy as well. */
867 if ((is_gimple_min_invariant (dest)
868 || TREE_CODE (dest) == SSA_NAME)
869 && (is_gimple_min_invariant (src)
870 || TREE_CODE (src) == SSA_NAME))
872 ao_ref destr, srcr;
873 ao_ref_init_from_ptr_and_size (&destr, dest, len);
874 ao_ref_init_from_ptr_and_size (&srcr, src, len);
875 if (!refs_may_alias_p_1 (&destr, &srcr, false))
877 tree fn;
878 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
879 if (!fn)
880 return false;
881 gimple_call_set_fndecl (stmt, fn);
882 gimple_call_set_arg (stmt, 0, dest);
883 gimple_call_set_arg (stmt, 1, src);
884 fold_stmt (gsi);
885 return true;
889 return false;
892 if (!tree_fits_shwi_p (len))
893 return false;
894 /* FIXME:
895 This logic lose for arguments like (type *)malloc (sizeof (type)),
896 since we strip the casts of up to VOID return value from malloc.
897 Perhaps we ought to inherit type from non-VOID argument here? */
898 STRIP_NOPS (src);
899 STRIP_NOPS (dest);
900 if (!POINTER_TYPE_P (TREE_TYPE (src))
901 || !POINTER_TYPE_P (TREE_TYPE (dest)))
902 return false;
903 /* In the following try to find a type that is most natural to be
904 used for the memcpy source and destination and that allows
905 the most optimization when memcpy is turned into a plain assignment
906 using that type. In theory we could always use a char[len] type
907 but that only gains us that the destination and source possibly
908 no longer will have their address taken. */
909 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
910 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
912 tree tem = TREE_OPERAND (src, 0);
913 STRIP_NOPS (tem);
914 if (tem != TREE_OPERAND (src, 0))
915 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
917 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
919 tree tem = TREE_OPERAND (dest, 0);
920 STRIP_NOPS (tem);
921 if (tem != TREE_OPERAND (dest, 0))
922 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
924 srctype = TREE_TYPE (TREE_TYPE (src));
925 if (TREE_CODE (srctype) == ARRAY_TYPE
926 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
928 srctype = TREE_TYPE (srctype);
929 STRIP_NOPS (src);
930 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
932 desttype = TREE_TYPE (TREE_TYPE (dest));
933 if (TREE_CODE (desttype) == ARRAY_TYPE
934 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
936 desttype = TREE_TYPE (desttype);
937 STRIP_NOPS (dest);
938 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
940 if (TREE_ADDRESSABLE (srctype)
941 || TREE_ADDRESSABLE (desttype))
942 return false;
944 /* Make sure we are not copying using a floating-point mode or
945 a type whose size possibly does not match its precision. */
946 if (FLOAT_MODE_P (TYPE_MODE (desttype))
947 || TREE_CODE (desttype) == BOOLEAN_TYPE
948 || TREE_CODE (desttype) == ENUMERAL_TYPE)
949 desttype = bitwise_type_for_mode (TYPE_MODE (desttype));
950 if (FLOAT_MODE_P (TYPE_MODE (srctype))
951 || TREE_CODE (srctype) == BOOLEAN_TYPE
952 || TREE_CODE (srctype) == ENUMERAL_TYPE)
953 srctype = bitwise_type_for_mode (TYPE_MODE (srctype));
954 if (!srctype)
955 srctype = desttype;
956 if (!desttype)
957 desttype = srctype;
958 if (!srctype)
959 return false;
961 src_align = get_pointer_alignment (src);
962 dest_align = get_pointer_alignment (dest);
963 if (dest_align < TYPE_ALIGN (desttype)
964 || src_align < TYPE_ALIGN (srctype))
965 return false;
967 destvar = dest;
968 STRIP_NOPS (destvar);
969 if (TREE_CODE (destvar) == ADDR_EXPR
970 && var_decl_component_p (TREE_OPERAND (destvar, 0))
971 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
972 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
973 else
974 destvar = NULL_TREE;
976 srcvar = src;
977 STRIP_NOPS (srcvar);
978 if (TREE_CODE (srcvar) == ADDR_EXPR
979 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
980 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
982 if (!destvar
983 || src_align >= TYPE_ALIGN (desttype))
984 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
985 srcvar, off0);
986 else if (!STRICT_ALIGNMENT)
988 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
989 src_align);
990 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
992 else
993 srcvar = NULL_TREE;
995 else
996 srcvar = NULL_TREE;
998 if (srcvar == NULL_TREE && destvar == NULL_TREE)
999 return false;
1001 if (srcvar == NULL_TREE)
1003 STRIP_NOPS (src);
1004 if (src_align >= TYPE_ALIGN (desttype))
1005 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
1006 else
1008 if (STRICT_ALIGNMENT)
1009 return false;
1010 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
1011 src_align);
1012 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
1015 else if (destvar == NULL_TREE)
1017 STRIP_NOPS (dest);
1018 if (dest_align >= TYPE_ALIGN (srctype))
1019 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
1020 else
1022 if (STRICT_ALIGNMENT)
1023 return false;
1024 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
1025 dest_align);
1026 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
1030 gimple *new_stmt;
1031 if (is_gimple_reg_type (TREE_TYPE (srcvar)))
1033 tree tem = fold_const_aggregate_ref (srcvar);
1034 if (tem)
1035 srcvar = tem;
1036 if (! is_gimple_min_invariant (srcvar))
1038 new_stmt = gimple_build_assign (NULL_TREE, srcvar);
1039 srcvar = create_tmp_reg_or_ssa_name (TREE_TYPE (srcvar),
1040 new_stmt);
1041 gimple_assign_set_lhs (new_stmt, srcvar);
1042 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
1043 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1046 new_stmt = gimple_build_assign (destvar, srcvar);
1047 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
1048 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
1049 if (gimple_vdef (new_stmt)
1050 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1051 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1052 if (!lhs)
1054 gsi_replace (gsi, new_stmt, false);
1055 return true;
1057 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1060 done:
1061 gimple_seq stmts = NULL;
1062 if (endp == 0 || endp == 3)
1063 len = NULL_TREE;
1064 else if (endp == 2)
1065 len = gimple_build (&stmts, loc, MINUS_EXPR, TREE_TYPE (len), len,
1066 ssize_int (1));
1067 if (endp == 2 || endp == 1)
1069 len = gimple_convert_to_ptrofftype (&stmts, loc, len);
1070 dest = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
1071 TREE_TYPE (dest), dest, len);
1074 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1075 gimple *repl = gimple_build_assign (lhs, dest);
1076 gsi_replace (gsi, repl, false);
1077 return true;
1080 /* Fold function call to builtin memset or bzero at *GSI setting the
1081 memory of size LEN to VAL. Return whether a simplification was made. */
1083 static bool
1084 gimple_fold_builtin_memset (gimple_stmt_iterator *gsi, tree c, tree len)
1086 gimple *stmt = gsi_stmt (*gsi);
1087 tree etype;
1088 unsigned HOST_WIDE_INT length, cval;
1090 /* If the LEN parameter is zero, return DEST. */
1091 if (integer_zerop (len))
1093 replace_call_with_value (gsi, gimple_call_arg (stmt, 0));
1094 return true;
1097 if (! tree_fits_uhwi_p (len))
1098 return false;
1100 if (TREE_CODE (c) != INTEGER_CST)
1101 return false;
1103 tree dest = gimple_call_arg (stmt, 0);
1104 tree var = dest;
1105 if (TREE_CODE (var) != ADDR_EXPR)
1106 return false;
1108 var = TREE_OPERAND (var, 0);
1109 if (TREE_THIS_VOLATILE (var))
1110 return false;
1112 etype = TREE_TYPE (var);
1113 if (TREE_CODE (etype) == ARRAY_TYPE)
1114 etype = TREE_TYPE (etype);
1116 if (!INTEGRAL_TYPE_P (etype)
1117 && !POINTER_TYPE_P (etype))
1118 return NULL_TREE;
1120 if (! var_decl_component_p (var))
1121 return NULL_TREE;
1123 length = tree_to_uhwi (len);
1124 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
1125 || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
1126 return NULL_TREE;
1128 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
1129 return NULL_TREE;
1131 if (integer_zerop (c))
1132 cval = 0;
1133 else
1135 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
1136 return NULL_TREE;
1138 cval = TREE_INT_CST_LOW (c);
1139 cval &= 0xff;
1140 cval |= cval << 8;
1141 cval |= cval << 16;
1142 cval |= (cval << 31) << 1;
1145 var = fold_build2 (MEM_REF, etype, dest, build_int_cst (ptr_type_node, 0));
1146 gimple *store = gimple_build_assign (var, build_int_cst_type (etype, cval));
1147 gimple_set_vuse (store, gimple_vuse (stmt));
1148 tree vdef = gimple_vdef (stmt);
1149 if (vdef && TREE_CODE (vdef) == SSA_NAME)
1151 gimple_set_vdef (store, gimple_vdef (stmt));
1152 SSA_NAME_DEF_STMT (gimple_vdef (stmt)) = store;
1154 gsi_insert_before (gsi, store, GSI_SAME_STMT);
1155 if (gimple_call_lhs (stmt))
1157 gimple *asgn = gimple_build_assign (gimple_call_lhs (stmt), dest);
1158 gsi_replace (gsi, asgn, false);
1160 else
1162 gimple_stmt_iterator gsi2 = *gsi;
1163 gsi_prev (gsi);
1164 gsi_remove (&gsi2, true);
1167 return true;
1171 /* Obtain the minimum and maximum string length or minimum and maximum
1172 value of ARG in LENGTH[0] and LENGTH[1], respectively.
1173 If ARG is an SSA name variable, follow its use-def chains. When
1174 TYPE == 0, if LENGTH[1] is not equal to the length we determine or
1175 if we are unable to determine the length or value, return False.
1176 VISITED is a bitmap of visited variables.
1177 TYPE is 0 if string length should be obtained, 1 for maximum string
1178 length and 2 for maximum value ARG can have.
1179 When FUZZY is set and the length of a string cannot be determined,
1180 the function instead considers as the maximum possible length the
1181 size of a character array it may refer to.
1182 Set *FLEXP to true if the range of the string lengths has been
1183 obtained from the upper bound of an array at the end of a struct.
1184 Such an array may hold a string that's longer than its upper bound
1185 due to it being used as a poor-man's flexible array member. */
1187 static bool
1188 get_range_strlen (tree arg, tree length[2], bitmap *visited, int type,
1189 bool fuzzy, bool *flexp)
1191 tree var, val;
1192 gimple *def_stmt;
1194 /* The minimum and maximum length. The MAXLEN pointer stays unchanged
1195 but MINLEN may be cleared during the execution of the function. */
1196 tree *minlen = length;
1197 tree *const maxlen = length + 1;
1199 if (TREE_CODE (arg) != SSA_NAME)
1201 /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */
1202 if (TREE_CODE (arg) == ADDR_EXPR
1203 && TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF
1204 && integer_zerop (TREE_OPERAND (TREE_OPERAND (arg, 0), 1)))
1206 tree aop0 = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
1207 if (TREE_CODE (aop0) == INDIRECT_REF
1208 && TREE_CODE (TREE_OPERAND (aop0, 0)) == SSA_NAME)
1209 return get_range_strlen (TREE_OPERAND (aop0, 0),
1210 length, visited, type, fuzzy, flexp);
1213 if (type == 2)
1215 val = arg;
1216 if (TREE_CODE (val) != INTEGER_CST
1217 || tree_int_cst_sgn (val) < 0)
1218 return false;
1220 else
1221 val = c_strlen (arg, 1);
1223 if (!val && fuzzy)
1225 if (TREE_CODE (arg) == ADDR_EXPR)
1226 return get_range_strlen (TREE_OPERAND (arg, 0), length,
1227 visited, type, fuzzy, flexp);
1229 if (TREE_CODE (arg) == COMPONENT_REF
1230 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 1))) == ARRAY_TYPE)
1232 /* Use the type of the member array to determine the upper
1233 bound on the length of the array. This may be overly
1234 optimistic if the array itself isn't NUL-terminated and
1235 the caller relies on the subsequent member to contain
1236 the NUL.
1237 Set *FLEXP to true if the array whose bound is being
1238 used is at the end of a struct. */
1239 if (array_at_struct_end_p (arg, true))
1240 *flexp = true;
1242 arg = TREE_OPERAND (arg, 1);
1243 val = TYPE_SIZE_UNIT (TREE_TYPE (arg));
1244 if (!val || integer_zerop (val))
1245 return false;
1246 val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val,
1247 integer_one_node);
1248 /* Set the minimum size to zero since the string in
1249 the array could have zero length. */
1250 *minlen = ssize_int (0);
1254 if (!val)
1255 return false;
1257 if (minlen
1258 && (!*minlen
1259 || (type > 0
1260 && TREE_CODE (*minlen) == INTEGER_CST
1261 && TREE_CODE (val) == INTEGER_CST
1262 && tree_int_cst_lt (val, *minlen))))
1263 *minlen = val;
1265 if (*maxlen)
1267 if (type > 0)
1269 if (TREE_CODE (*maxlen) != INTEGER_CST
1270 || TREE_CODE (val) != INTEGER_CST)
1271 return false;
1273 if (tree_int_cst_lt (*maxlen, val))
1274 *maxlen = val;
1275 return true;
1277 else if (simple_cst_equal (val, *maxlen) != 1)
1278 return false;
1281 *maxlen = val;
1282 return true;
1285 /* If ARG is registered for SSA update we cannot look at its defining
1286 statement. */
1287 if (name_registered_for_update_p (arg))
1288 return false;
1290 /* If we were already here, break the infinite cycle. */
1291 if (!*visited)
1292 *visited = BITMAP_ALLOC (NULL);
1293 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (arg)))
1294 return true;
1296 var = arg;
1297 def_stmt = SSA_NAME_DEF_STMT (var);
1299 switch (gimple_code (def_stmt))
1301 case GIMPLE_ASSIGN:
1302 /* The RHS of the statement defining VAR must either have a
1303 constant length or come from another SSA_NAME with a constant
1304 length. */
1305 if (gimple_assign_single_p (def_stmt)
1306 || gimple_assign_unary_nop_p (def_stmt))
1308 tree rhs = gimple_assign_rhs1 (def_stmt);
1309 return get_range_strlen (rhs, length, visited, type, fuzzy, flexp);
1311 else if (gimple_assign_rhs_code (def_stmt) == COND_EXPR)
1313 tree op2 = gimple_assign_rhs2 (def_stmt);
1314 tree op3 = gimple_assign_rhs3 (def_stmt);
1315 return get_range_strlen (op2, length, visited, type, fuzzy, flexp)
1316 && get_range_strlen (op3, length, visited, type, fuzzy, flexp);
1318 return false;
1320 case GIMPLE_PHI:
1322 /* All the arguments of the PHI node must have the same constant
1323 length. */
1324 unsigned i;
1326 for (i = 0; i < gimple_phi_num_args (def_stmt); i++)
1328 tree arg = gimple_phi_arg (def_stmt, i)->def;
1330 /* If this PHI has itself as an argument, we cannot
1331 determine the string length of this argument. However,
1332 if we can find a constant string length for the other
1333 PHI args then we can still be sure that this is a
1334 constant string length. So be optimistic and just
1335 continue with the next argument. */
1336 if (arg == gimple_phi_result (def_stmt))
1337 continue;
1339 if (!get_range_strlen (arg, length, visited, type, fuzzy, flexp))
1341 if (fuzzy)
1342 *maxlen = build_all_ones_cst (size_type_node);
1343 else
1344 return false;
1348 return true;
1350 default:
1351 return false;
1355 /* Determine the minimum and maximum value or string length that ARG
1356 refers to and store each in the first two elements of MINMAXLEN.
1357 For expressions that point to strings of unknown lengths that are
1358 character arrays, use the upper bound of the array as the maximum
1359 length. For example, given an expression like 'x ? array : "xyz"'
1360 and array declared as 'char array[8]', MINMAXLEN[0] will be set
1361 to 3 and MINMAXLEN[1] to 7, the longest string that could be
1362 stored in array.
1363 Return true if the range of the string lengths has been obtained
1364 from the upper bound of an array at the end of a struct. Such
1365 an array may hold a string that's longer than its upper bound
1366 due to it being used as a poor-man's flexible array member. */
1368 bool
1369 get_range_strlen (tree arg, tree minmaxlen[2])
1371 bitmap visited = NULL;
1373 minmaxlen[0] = NULL_TREE;
1374 minmaxlen[1] = NULL_TREE;
1376 bool flexarray = false;
1377 get_range_strlen (arg, minmaxlen, &visited, 1, true, &flexarray);
1379 if (visited)
1380 BITMAP_FREE (visited);
1382 return flexarray;
1385 tree
1386 get_maxval_strlen (tree arg, int type)
1388 bitmap visited = NULL;
1389 tree len[2] = { NULL_TREE, NULL_TREE };
1391 bool dummy;
1392 if (!get_range_strlen (arg, len, &visited, type, false, &dummy))
1393 len[1] = NULL_TREE;
1394 if (visited)
1395 BITMAP_FREE (visited);
1397 return len[1];
1401 /* Fold function call to builtin strcpy with arguments DEST and SRC.
1402 If LEN is not NULL, it represents the length of the string to be
1403 copied. Return NULL_TREE if no simplification can be made. */
1405 static bool
1406 gimple_fold_builtin_strcpy (gimple_stmt_iterator *gsi,
1407 tree dest, tree src)
1409 location_t loc = gimple_location (gsi_stmt (*gsi));
1410 tree fn;
1412 /* If SRC and DEST are the same (and not volatile), return DEST. */
1413 if (operand_equal_p (src, dest, 0))
1415 replace_call_with_value (gsi, dest);
1416 return true;
1419 if (optimize_function_for_size_p (cfun))
1420 return false;
1422 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1423 if (!fn)
1424 return false;
1426 tree len = get_maxval_strlen (src, 0);
1427 if (!len)
1428 return false;
1430 len = fold_convert_loc (loc, size_type_node, len);
1431 len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
1432 len = force_gimple_operand_gsi (gsi, len, true,
1433 NULL_TREE, true, GSI_SAME_STMT);
1434 gimple *repl = gimple_build_call (fn, 3, dest, src, len);
1435 replace_call_with_call_and_fold (gsi, repl);
1436 return true;
1439 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
1440 If SLEN is not NULL, it represents the length of the source string.
1441 Return NULL_TREE if no simplification can be made. */
1443 static bool
1444 gimple_fold_builtin_strncpy (gimple_stmt_iterator *gsi,
1445 tree dest, tree src, tree len)
1447 location_t loc = gimple_location (gsi_stmt (*gsi));
1448 tree fn;
1450 /* If the LEN parameter is zero, return DEST. */
1451 if (integer_zerop (len))
1453 replace_call_with_value (gsi, dest);
1454 return true;
1457 /* We can't compare slen with len as constants below if len is not a
1458 constant. */
1459 if (TREE_CODE (len) != INTEGER_CST)
1460 return false;
1462 /* Now, we must be passed a constant src ptr parameter. */
1463 tree slen = get_maxval_strlen (src, 0);
1464 if (!slen || TREE_CODE (slen) != INTEGER_CST)
1465 return false;
1467 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
1469 /* We do not support simplification of this case, though we do
1470 support it when expanding trees into RTL. */
1471 /* FIXME: generate a call to __builtin_memset. */
1472 if (tree_int_cst_lt (slen, len))
1473 return false;
1475 /* OK transform into builtin memcpy. */
1476 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1477 if (!fn)
1478 return false;
1480 len = fold_convert_loc (loc, size_type_node, len);
1481 len = force_gimple_operand_gsi (gsi, len, true,
1482 NULL_TREE, true, GSI_SAME_STMT);
1483 gimple *repl = gimple_build_call (fn, 3, dest, src, len);
1484 replace_call_with_call_and_fold (gsi, repl);
1485 return true;
1488 /* Fold function call to builtin strchr or strrchr.
1489 If both arguments are constant, evaluate and fold the result,
1490 otherwise simplify str(r)chr (str, 0) into str + strlen (str).
1491 In general strlen is significantly faster than strchr
1492 due to being a simpler operation. */
1493 static bool
1494 gimple_fold_builtin_strchr (gimple_stmt_iterator *gsi, bool is_strrchr)
1496 gimple *stmt = gsi_stmt (*gsi);
1497 tree str = gimple_call_arg (stmt, 0);
1498 tree c = gimple_call_arg (stmt, 1);
1499 location_t loc = gimple_location (stmt);
1500 const char *p;
1501 char ch;
1503 if (!gimple_call_lhs (stmt))
1504 return false;
1506 if ((p = c_getstr (str)) && target_char_cst_p (c, &ch))
1508 const char *p1 = is_strrchr ? strrchr (p, ch) : strchr (p, ch);
1510 if (p1 == NULL)
1512 replace_call_with_value (gsi, integer_zero_node);
1513 return true;
1516 tree len = build_int_cst (size_type_node, p1 - p);
1517 gimple_seq stmts = NULL;
1518 gimple *new_stmt = gimple_build_assign (gimple_call_lhs (stmt),
1519 POINTER_PLUS_EXPR, str, len);
1520 gimple_seq_add_stmt_without_update (&stmts, new_stmt);
1521 gsi_replace_with_seq_vops (gsi, stmts);
1522 return true;
1525 if (!integer_zerop (c))
1526 return false;
1528 /* Transform strrchr (s, 0) to strchr (s, 0) when optimizing for size. */
1529 if (is_strrchr && optimize_function_for_size_p (cfun))
1531 tree strchr_fn = builtin_decl_implicit (BUILT_IN_STRCHR);
1533 if (strchr_fn)
1535 gimple *repl = gimple_build_call (strchr_fn, 2, str, c);
1536 replace_call_with_call_and_fold (gsi, repl);
1537 return true;
1540 return false;
1543 tree len;
1544 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
1546 if (!strlen_fn)
1547 return false;
1549 /* Create newstr = strlen (str). */
1550 gimple_seq stmts = NULL;
1551 gimple *new_stmt = gimple_build_call (strlen_fn, 1, str);
1552 gimple_set_location (new_stmt, loc);
1553 len = create_tmp_reg_or_ssa_name (size_type_node);
1554 gimple_call_set_lhs (new_stmt, len);
1555 gimple_seq_add_stmt_without_update (&stmts, new_stmt);
1557 /* Create (str p+ strlen (str)). */
1558 new_stmt = gimple_build_assign (gimple_call_lhs (stmt),
1559 POINTER_PLUS_EXPR, str, len);
1560 gimple_seq_add_stmt_without_update (&stmts, new_stmt);
1561 gsi_replace_with_seq_vops (gsi, stmts);
1562 /* gsi now points at the assignment to the lhs, get a
1563 stmt iterator to the strlen.
1564 ??? We can't use gsi_for_stmt as that doesn't work when the
1565 CFG isn't built yet. */
1566 gimple_stmt_iterator gsi2 = *gsi;
1567 gsi_prev (&gsi2);
1568 fold_stmt (&gsi2);
1569 return true;
1572 /* Fold function call to builtin strstr.
1573 If both arguments are constant, evaluate and fold the result,
1574 additionally fold strstr (x, "") into x and strstr (x, "c")
1575 into strchr (x, 'c'). */
1576 static bool
1577 gimple_fold_builtin_strstr (gimple_stmt_iterator *gsi)
1579 gimple *stmt = gsi_stmt (*gsi);
1580 tree haystack = gimple_call_arg (stmt, 0);
1581 tree needle = gimple_call_arg (stmt, 1);
1582 const char *p, *q;
1584 if (!gimple_call_lhs (stmt))
1585 return false;
1587 q = c_getstr (needle);
1588 if (q == NULL)
1589 return false;
1591 if ((p = c_getstr (haystack)))
1593 const char *r = strstr (p, q);
1595 if (r == NULL)
1597 replace_call_with_value (gsi, integer_zero_node);
1598 return true;
1601 tree len = build_int_cst (size_type_node, r - p);
1602 gimple_seq stmts = NULL;
1603 gimple *new_stmt
1604 = gimple_build_assign (gimple_call_lhs (stmt), POINTER_PLUS_EXPR,
1605 haystack, len);
1606 gimple_seq_add_stmt_without_update (&stmts, new_stmt);
1607 gsi_replace_with_seq_vops (gsi, stmts);
1608 return true;
1611 /* For strstr (x, "") return x. */
1612 if (q[0] == '\0')
1614 replace_call_with_value (gsi, haystack);
1615 return true;
1618 /* Transform strstr (x, "c") into strchr (x, 'c'). */
1619 if (q[1] == '\0')
1621 tree strchr_fn = builtin_decl_implicit (BUILT_IN_STRCHR);
1622 if (strchr_fn)
1624 tree c = build_int_cst (integer_type_node, q[0]);
1625 gimple *repl = gimple_build_call (strchr_fn, 2, haystack, c);
1626 replace_call_with_call_and_fold (gsi, repl);
1627 return true;
1631 return false;
1634 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
1635 to the call.
1637 Return NULL_TREE if no simplification was possible, otherwise return the
1638 simplified form of the call as a tree.
1640 The simplified form may be a constant or other expression which
1641 computes the same value, but in a more efficient manner (including
1642 calls to other builtin functions).
1644 The call may contain arguments which need to be evaluated, but
1645 which are not useful to determine the result of the call. In
1646 this case we return a chain of COMPOUND_EXPRs. The LHS of each
1647 COMPOUND_EXPR will be an argument which must be evaluated.
1648 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
1649 COMPOUND_EXPR in the chain will contain the tree for the simplified
1650 form of the builtin function call. */
1652 static bool
1653 gimple_fold_builtin_strcat (gimple_stmt_iterator *gsi, tree dst, tree src)
1655 gimple *stmt = gsi_stmt (*gsi);
1656 location_t loc = gimple_location (stmt);
1658 const char *p = c_getstr (src);
1660 /* If the string length is zero, return the dst parameter. */
1661 if (p && *p == '\0')
1663 replace_call_with_value (gsi, dst);
1664 return true;
1667 if (!optimize_bb_for_speed_p (gimple_bb (stmt)))
1668 return false;
1670 /* See if we can store by pieces into (dst + strlen(dst)). */
1671 tree newdst;
1672 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
1673 tree memcpy_fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1675 if (!strlen_fn || !memcpy_fn)
1676 return false;
1678 /* If the length of the source string isn't computable don't
1679 split strcat into strlen and memcpy. */
1680 tree len = get_maxval_strlen (src, 0);
1681 if (! len)
1682 return false;
1684 /* Create strlen (dst). */
1685 gimple_seq stmts = NULL, stmts2;
1686 gimple *repl = gimple_build_call (strlen_fn, 1, dst);
1687 gimple_set_location (repl, loc);
1688 newdst = create_tmp_reg_or_ssa_name (size_type_node);
1689 gimple_call_set_lhs (repl, newdst);
1690 gimple_seq_add_stmt_without_update (&stmts, repl);
1692 /* Create (dst p+ strlen (dst)). */
1693 newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
1694 newdst = force_gimple_operand (newdst, &stmts2, true, NULL_TREE);
1695 gimple_seq_add_seq_without_update (&stmts, stmts2);
1697 len = fold_convert_loc (loc, size_type_node, len);
1698 len = size_binop_loc (loc, PLUS_EXPR, len,
1699 build_int_cst (size_type_node, 1));
1700 len = force_gimple_operand (len, &stmts2, true, NULL_TREE);
1701 gimple_seq_add_seq_without_update (&stmts, stmts2);
1703 repl = gimple_build_call (memcpy_fn, 3, newdst, src, len);
1704 gimple_seq_add_stmt_without_update (&stmts, repl);
1705 if (gimple_call_lhs (stmt))
1707 repl = gimple_build_assign (gimple_call_lhs (stmt), dst);
1708 gimple_seq_add_stmt_without_update (&stmts, repl);
1709 gsi_replace_with_seq_vops (gsi, stmts);
1710 /* gsi now points at the assignment to the lhs, get a
1711 stmt iterator to the memcpy call.
1712 ??? We can't use gsi_for_stmt as that doesn't work when the
1713 CFG isn't built yet. */
1714 gimple_stmt_iterator gsi2 = *gsi;
1715 gsi_prev (&gsi2);
1716 fold_stmt (&gsi2);
1718 else
1720 gsi_replace_with_seq_vops (gsi, stmts);
1721 fold_stmt (gsi);
1723 return true;
1726 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
1727 are the arguments to the call. */
1729 static bool
1730 gimple_fold_builtin_strcat_chk (gimple_stmt_iterator *gsi)
1732 gimple *stmt = gsi_stmt (*gsi);
1733 tree dest = gimple_call_arg (stmt, 0);
1734 tree src = gimple_call_arg (stmt, 1);
1735 tree size = gimple_call_arg (stmt, 2);
1736 tree fn;
1737 const char *p;
1740 p = c_getstr (src);
1741 /* If the SRC parameter is "", return DEST. */
1742 if (p && *p == '\0')
1744 replace_call_with_value (gsi, dest);
1745 return true;
1748 if (! tree_fits_uhwi_p (size) || ! integer_all_onesp (size))
1749 return false;
1751 /* If __builtin_strcat_chk is used, assume strcat is available. */
1752 fn = builtin_decl_explicit (BUILT_IN_STRCAT);
1753 if (!fn)
1754 return false;
1756 gimple *repl = gimple_build_call (fn, 2, dest, src);
1757 replace_call_with_call_and_fold (gsi, repl);
1758 return true;
1761 /* Simplify a call to the strncat builtin. */
1763 static bool
1764 gimple_fold_builtin_strncat (gimple_stmt_iterator *gsi)
1766 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
1767 tree dst = gimple_call_arg (stmt, 0);
1768 tree src = gimple_call_arg (stmt, 1);
1769 tree len = gimple_call_arg (stmt, 2);
1771 const char *p = c_getstr (src);
1773 /* If the requested length is zero, or the src parameter string
1774 length is zero, return the dst parameter. */
1775 if (integer_zerop (len) || (p && *p == '\0'))
1777 replace_call_with_value (gsi, dst);
1778 return true;
1781 /* If the requested len is greater than or equal to the string
1782 length, call strcat. */
1783 if (TREE_CODE (len) == INTEGER_CST && p
1784 && compare_tree_int (len, strlen (p)) >= 0)
1786 tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
1788 /* If the replacement _DECL isn't initialized, don't do the
1789 transformation. */
1790 if (!fn)
1791 return false;
1793 gcall *repl = gimple_build_call (fn, 2, dst, src);
1794 replace_call_with_call_and_fold (gsi, repl);
1795 return true;
1798 return false;
1801 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
1802 LEN, and SIZE. */
1804 static bool
1805 gimple_fold_builtin_strncat_chk (gimple_stmt_iterator *gsi)
1807 gimple *stmt = gsi_stmt (*gsi);
1808 tree dest = gimple_call_arg (stmt, 0);
1809 tree src = gimple_call_arg (stmt, 1);
1810 tree len = gimple_call_arg (stmt, 2);
1811 tree size = gimple_call_arg (stmt, 3);
1812 tree fn;
1813 const char *p;
1815 p = c_getstr (src);
1816 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
1817 if ((p && *p == '\0')
1818 || integer_zerop (len))
1820 replace_call_with_value (gsi, dest);
1821 return true;
1824 if (! tree_fits_uhwi_p (size))
1825 return false;
1827 if (! integer_all_onesp (size))
1829 tree src_len = c_strlen (src, 1);
1830 if (src_len
1831 && tree_fits_uhwi_p (src_len)
1832 && tree_fits_uhwi_p (len)
1833 && ! tree_int_cst_lt (len, src_len))
1835 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
1836 fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
1837 if (!fn)
1838 return false;
1840 gimple *repl = gimple_build_call (fn, 3, dest, src, size);
1841 replace_call_with_call_and_fold (gsi, repl);
1842 return true;
1844 return false;
1847 /* If __builtin_strncat_chk is used, assume strncat is available. */
1848 fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
1849 if (!fn)
1850 return false;
1852 gimple *repl = gimple_build_call (fn, 3, dest, src, len);
1853 replace_call_with_call_and_fold (gsi, repl);
1854 return true;
1857 /* Build and append gimple statements to STMTS that would load a first
1858 character of a memory location identified by STR. LOC is location
1859 of the statement. */
1861 static tree
1862 gimple_load_first_char (location_t loc, tree str, gimple_seq *stmts)
1864 tree var;
1866 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
1867 tree cst_uchar_ptr_node
1868 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
1869 tree off0 = build_int_cst (cst_uchar_ptr_node, 0);
1871 tree temp = fold_build2_loc (loc, MEM_REF, cst_uchar_node, str, off0);
1872 gassign *stmt = gimple_build_assign (NULL_TREE, temp);
1873 var = create_tmp_reg_or_ssa_name (cst_uchar_node, stmt);
1875 gimple_assign_set_lhs (stmt, var);
1876 gimple_seq_add_stmt_without_update (stmts, stmt);
1878 return var;
1881 /* Fold a call to the str{n}{case}cmp builtin pointed by GSI iterator.
1882 FCODE is the name of the builtin. */
1884 static bool
1885 gimple_fold_builtin_string_compare (gimple_stmt_iterator *gsi)
1887 gimple *stmt = gsi_stmt (*gsi);
1888 tree callee = gimple_call_fndecl (stmt);
1889 enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
1891 tree type = integer_type_node;
1892 tree str1 = gimple_call_arg (stmt, 0);
1893 tree str2 = gimple_call_arg (stmt, 1);
1894 tree lhs = gimple_call_lhs (stmt);
1895 HOST_WIDE_INT length = -1;
1897 /* Handle strncmp and strncasecmp functions. */
1898 if (gimple_call_num_args (stmt) == 3)
1900 tree len = gimple_call_arg (stmt, 2);
1901 if (tree_fits_uhwi_p (len))
1902 length = tree_to_uhwi (len);
1905 /* If the LEN parameter is zero, return zero. */
1906 if (length == 0)
1908 replace_call_with_value (gsi, integer_zero_node);
1909 return true;
1912 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
1913 if (operand_equal_p (str1, str2, 0))
1915 replace_call_with_value (gsi, integer_zero_node);
1916 return true;
1919 const char *p1 = c_getstr (str1);
1920 const char *p2 = c_getstr (str2);
1922 /* For known strings, return an immediate value. */
1923 if (p1 && p2)
1925 int r = 0;
1926 bool known_result = false;
1928 switch (fcode)
1930 case BUILT_IN_STRCMP:
1932 r = strcmp (p1, p2);
1933 known_result = true;
1934 break;
1936 case BUILT_IN_STRNCMP:
1938 if (length == -1)
1939 break;
1940 r = strncmp (p1, p2, length);
1941 known_result = true;
1942 break;
1944 /* Only handleable situation is where the string are equal (result 0),
1945 which is already handled by operand_equal_p case. */
1946 case BUILT_IN_STRCASECMP:
1947 break;
1948 case BUILT_IN_STRNCASECMP:
1950 if (length == -1)
1951 break;
1952 r = strncmp (p1, p2, length);
1953 if (r == 0)
1954 known_result = true;
1955 break;;
1957 default:
1958 gcc_unreachable ();
1961 if (known_result)
1963 replace_call_with_value (gsi, build_cmp_result (type, r));
1964 return true;
1968 bool nonzero_length = length >= 1
1969 || fcode == BUILT_IN_STRCMP
1970 || fcode == BUILT_IN_STRCASECMP;
1972 location_t loc = gimple_location (stmt);
1974 /* If the second arg is "", return *(const unsigned char*)arg1. */
1975 if (p2 && *p2 == '\0' && nonzero_length)
1977 gimple_seq stmts = NULL;
1978 tree var = gimple_load_first_char (loc, str1, &stmts);
1979 if (lhs)
1981 stmt = gimple_build_assign (lhs, NOP_EXPR, var);
1982 gimple_seq_add_stmt_without_update (&stmts, stmt);
1985 gsi_replace_with_seq_vops (gsi, stmts);
1986 return true;
1989 /* If the first arg is "", return -*(const unsigned char*)arg2. */
1990 if (p1 && *p1 == '\0' && nonzero_length)
1992 gimple_seq stmts = NULL;
1993 tree var = gimple_load_first_char (loc, str2, &stmts);
1995 if (lhs)
1997 tree c = create_tmp_reg_or_ssa_name (integer_type_node);
1998 stmt = gimple_build_assign (c, NOP_EXPR, var);
1999 gimple_seq_add_stmt_without_update (&stmts, stmt);
2001 stmt = gimple_build_assign (lhs, NEGATE_EXPR, c);
2002 gimple_seq_add_stmt_without_update (&stmts, stmt);
2005 gsi_replace_with_seq_vops (gsi, stmts);
2006 return true;
2009 /* If len parameter is one, return an expression corresponding to
2010 (*(const unsigned char*)arg2 - *(const unsigned char*)arg1). */
2011 if (fcode == BUILT_IN_STRNCMP && length == 1)
2013 gimple_seq stmts = NULL;
2014 tree temp1 = gimple_load_first_char (loc, str1, &stmts);
2015 tree temp2 = gimple_load_first_char (loc, str2, &stmts);
2017 if (lhs)
2019 tree c1 = create_tmp_reg_or_ssa_name (integer_type_node);
2020 gassign *convert1 = gimple_build_assign (c1, NOP_EXPR, temp1);
2021 gimple_seq_add_stmt_without_update (&stmts, convert1);
2023 tree c2 = create_tmp_reg_or_ssa_name (integer_type_node);
2024 gassign *convert2 = gimple_build_assign (c2, NOP_EXPR, temp2);
2025 gimple_seq_add_stmt_without_update (&stmts, convert2);
2027 stmt = gimple_build_assign (lhs, MINUS_EXPR, c1, c2);
2028 gimple_seq_add_stmt_without_update (&stmts, stmt);
2031 gsi_replace_with_seq_vops (gsi, stmts);
2032 return true;
2035 return false;
2038 /* Fold a call to the memchr pointed by GSI iterator. */
2040 static bool
2041 gimple_fold_builtin_memchr (gimple_stmt_iterator *gsi)
2043 gimple *stmt = gsi_stmt (*gsi);
2044 tree lhs = gimple_call_lhs (stmt);
2045 tree arg1 = gimple_call_arg (stmt, 0);
2046 tree arg2 = gimple_call_arg (stmt, 1);
2047 tree len = gimple_call_arg (stmt, 2);
2049 /* If the LEN parameter is zero, return zero. */
2050 if (integer_zerop (len))
2052 replace_call_with_value (gsi, build_int_cst (ptr_type_node, 0));
2053 return true;
2056 char c;
2057 if (TREE_CODE (arg2) != INTEGER_CST
2058 || !tree_fits_uhwi_p (len)
2059 || !target_char_cst_p (arg2, &c))
2060 return false;
2062 unsigned HOST_WIDE_INT length = tree_to_uhwi (len);
2063 unsigned HOST_WIDE_INT string_length;
2064 const char *p1 = c_getstr (arg1, &string_length);
2066 if (p1)
2068 const char *r = (const char *)memchr (p1, c, MIN (length, string_length));
2069 if (r == NULL)
2071 if (length <= string_length)
2073 replace_call_with_value (gsi, build_int_cst (ptr_type_node, 0));
2074 return true;
2077 else
2079 unsigned HOST_WIDE_INT offset = r - p1;
2080 gimple_seq stmts = NULL;
2081 if (lhs != NULL_TREE)
2083 tree offset_cst = build_int_cst (TREE_TYPE (len), offset);
2084 gassign *stmt = gimple_build_assign (lhs, POINTER_PLUS_EXPR,
2085 arg1, offset_cst);
2086 gimple_seq_add_stmt_without_update (&stmts, stmt);
2088 else
2089 gimple_seq_add_stmt_without_update (&stmts,
2090 gimple_build_nop ());
2092 gsi_replace_with_seq_vops (gsi, stmts);
2093 return true;
2097 return false;
2100 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
2101 to the call. IGNORE is true if the value returned
2102 by the builtin will be ignored. UNLOCKED is true is true if this
2103 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
2104 the known length of the string. Return NULL_TREE if no simplification
2105 was possible. */
2107 static bool
2108 gimple_fold_builtin_fputs (gimple_stmt_iterator *gsi,
2109 tree arg0, tree arg1,
2110 bool unlocked)
2112 gimple *stmt = gsi_stmt (*gsi);
2114 /* If we're using an unlocked function, assume the other unlocked
2115 functions exist explicitly. */
2116 tree const fn_fputc = (unlocked
2117 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
2118 : builtin_decl_implicit (BUILT_IN_FPUTC));
2119 tree const fn_fwrite = (unlocked
2120 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
2121 : builtin_decl_implicit (BUILT_IN_FWRITE));
2123 /* If the return value is used, don't do the transformation. */
2124 if (gimple_call_lhs (stmt))
2125 return false;
2127 /* Get the length of the string passed to fputs. If the length
2128 can't be determined, punt. */
2129 tree len = get_maxval_strlen (arg0, 0);
2130 if (!len
2131 || TREE_CODE (len) != INTEGER_CST)
2132 return false;
2134 switch (compare_tree_int (len, 1))
2136 case -1: /* length is 0, delete the call entirely . */
2137 replace_call_with_value (gsi, integer_zero_node);
2138 return true;
2140 case 0: /* length is 1, call fputc. */
2142 const char *p = c_getstr (arg0);
2143 if (p != NULL)
2145 if (!fn_fputc)
2146 return false;
2148 gimple *repl = gimple_build_call (fn_fputc, 2,
2149 build_int_cst
2150 (integer_type_node, p[0]), arg1);
2151 replace_call_with_call_and_fold (gsi, repl);
2152 return true;
2155 /* FALLTHROUGH */
2156 case 1: /* length is greater than 1, call fwrite. */
2158 /* If optimizing for size keep fputs. */
2159 if (optimize_function_for_size_p (cfun))
2160 return false;
2161 /* New argument list transforming fputs(string, stream) to
2162 fwrite(string, 1, len, stream). */
2163 if (!fn_fwrite)
2164 return false;
2166 gimple *repl = gimple_build_call (fn_fwrite, 4, arg0,
2167 size_one_node, len, arg1);
2168 replace_call_with_call_and_fold (gsi, repl);
2169 return true;
2171 default:
2172 gcc_unreachable ();
2174 return false;
2177 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
2178 DEST, SRC, LEN, and SIZE are the arguments to the call.
2179 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
2180 code of the builtin. If MAXLEN is not NULL, it is maximum length
2181 passed as third argument. */
2183 static bool
2184 gimple_fold_builtin_memory_chk (gimple_stmt_iterator *gsi,
2185 tree dest, tree src, tree len, tree size,
2186 enum built_in_function fcode)
2188 gimple *stmt = gsi_stmt (*gsi);
2189 location_t loc = gimple_location (stmt);
2190 bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
2191 tree fn;
2193 /* If SRC and DEST are the same (and not volatile), return DEST
2194 (resp. DEST+LEN for __mempcpy_chk). */
2195 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
2197 if (fcode != BUILT_IN_MEMPCPY_CHK)
2199 replace_call_with_value (gsi, dest);
2200 return true;
2202 else
2204 gimple_seq stmts = NULL;
2205 len = gimple_convert_to_ptrofftype (&stmts, loc, len);
2206 tree temp = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
2207 TREE_TYPE (dest), dest, len);
2208 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
2209 replace_call_with_value (gsi, temp);
2210 return true;
2214 if (! tree_fits_uhwi_p (size))
2215 return false;
2217 tree maxlen = get_maxval_strlen (len, 2);
2218 if (! integer_all_onesp (size))
2220 if (! tree_fits_uhwi_p (len))
2222 /* If LEN is not constant, try MAXLEN too.
2223 For MAXLEN only allow optimizing into non-_ocs function
2224 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2225 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
2227 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
2229 /* (void) __mempcpy_chk () can be optimized into
2230 (void) __memcpy_chk (). */
2231 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
2232 if (!fn)
2233 return false;
2235 gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
2236 replace_call_with_call_and_fold (gsi, repl);
2237 return true;
2239 return false;
2242 else
2243 maxlen = len;
2245 if (tree_int_cst_lt (size, maxlen))
2246 return false;
2249 fn = NULL_TREE;
2250 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
2251 mem{cpy,pcpy,move,set} is available. */
2252 switch (fcode)
2254 case BUILT_IN_MEMCPY_CHK:
2255 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
2256 break;
2257 case BUILT_IN_MEMPCPY_CHK:
2258 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
2259 break;
2260 case BUILT_IN_MEMMOVE_CHK:
2261 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
2262 break;
2263 case BUILT_IN_MEMSET_CHK:
2264 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
2265 break;
2266 default:
2267 break;
2270 if (!fn)
2271 return false;
2273 gimple *repl = gimple_build_call (fn, 3, dest, src, len);
2274 replace_call_with_call_and_fold (gsi, repl);
2275 return true;
2278 /* Fold a call to the __st[rp]cpy_chk builtin.
2279 DEST, SRC, and SIZE are the arguments to the call.
2280 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
2281 code of the builtin. If MAXLEN is not NULL, it is maximum length of
2282 strings passed as second argument. */
2284 static bool
2285 gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator *gsi,
2286 tree dest,
2287 tree src, tree size,
2288 enum built_in_function fcode)
2290 gimple *stmt = gsi_stmt (*gsi);
2291 location_t loc = gimple_location (stmt);
2292 bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
2293 tree len, fn;
2295 /* If SRC and DEST are the same (and not volatile), return DEST. */
2296 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
2298 replace_call_with_value (gsi, dest);
2299 return true;
2302 if (! tree_fits_uhwi_p (size))
2303 return false;
2305 tree maxlen = get_maxval_strlen (src, 1);
2306 if (! integer_all_onesp (size))
2308 len = c_strlen (src, 1);
2309 if (! len || ! tree_fits_uhwi_p (len))
2311 /* If LEN is not constant, try MAXLEN too.
2312 For MAXLEN only allow optimizing into non-_ocs function
2313 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2314 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
2316 if (fcode == BUILT_IN_STPCPY_CHK)
2318 if (! ignore)
2319 return false;
2321 /* If return value of __stpcpy_chk is ignored,
2322 optimize into __strcpy_chk. */
2323 fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
2324 if (!fn)
2325 return false;
2327 gimple *repl = gimple_build_call (fn, 3, dest, src, size);
2328 replace_call_with_call_and_fold (gsi, repl);
2329 return true;
2332 if (! len || TREE_SIDE_EFFECTS (len))
2333 return false;
2335 /* If c_strlen returned something, but not a constant,
2336 transform __strcpy_chk into __memcpy_chk. */
2337 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
2338 if (!fn)
2339 return false;
2341 gimple_seq stmts = NULL;
2342 len = gimple_convert (&stmts, loc, size_type_node, len);
2343 len = gimple_build (&stmts, loc, PLUS_EXPR, size_type_node, len,
2344 build_int_cst (size_type_node, 1));
2345 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
2346 gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
2347 replace_call_with_call_and_fold (gsi, repl);
2348 return true;
2351 else
2352 maxlen = len;
2354 if (! tree_int_cst_lt (maxlen, size))
2355 return false;
2358 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
2359 fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK
2360 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
2361 if (!fn)
2362 return false;
2364 gimple *repl = gimple_build_call (fn, 2, dest, src);
2365 replace_call_with_call_and_fold (gsi, repl);
2366 return true;
2369 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
2370 are the arguments to the call. If MAXLEN is not NULL, it is maximum
2371 length passed as third argument. IGNORE is true if return value can be
2372 ignored. FCODE is the BUILT_IN_* code of the builtin. */
2374 static bool
2375 gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator *gsi,
2376 tree dest, tree src,
2377 tree len, tree size,
2378 enum built_in_function fcode)
2380 gimple *stmt = gsi_stmt (*gsi);
2381 bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
2382 tree fn;
2384 if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
2386 /* If return value of __stpncpy_chk is ignored,
2387 optimize into __strncpy_chk. */
2388 fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
2389 if (fn)
2391 gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
2392 replace_call_with_call_and_fold (gsi, repl);
2393 return true;
2397 if (! tree_fits_uhwi_p (size))
2398 return false;
2400 tree maxlen = get_maxval_strlen (len, 2);
2401 if (! integer_all_onesp (size))
2403 if (! tree_fits_uhwi_p (len))
2405 /* If LEN is not constant, try MAXLEN too.
2406 For MAXLEN only allow optimizing into non-_ocs function
2407 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2408 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
2409 return false;
2411 else
2412 maxlen = len;
2414 if (tree_int_cst_lt (size, maxlen))
2415 return false;
2418 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
2419 fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK
2420 ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
2421 if (!fn)
2422 return false;
2424 gimple *repl = gimple_build_call (fn, 3, dest, src, len);
2425 replace_call_with_call_and_fold (gsi, repl);
2426 return true;
2429 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
2430 Return NULL_TREE if no simplification can be made. */
2432 static bool
2433 gimple_fold_builtin_stpcpy (gimple_stmt_iterator *gsi)
2435 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
2436 location_t loc = gimple_location (stmt);
2437 tree dest = gimple_call_arg (stmt, 0);
2438 tree src = gimple_call_arg (stmt, 1);
2439 tree fn, len, lenp1;
2441 /* If the result is unused, replace stpcpy with strcpy. */
2442 if (gimple_call_lhs (stmt) == NULL_TREE)
2444 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
2445 if (!fn)
2446 return false;
2447 gimple_call_set_fndecl (stmt, fn);
2448 fold_stmt (gsi);
2449 return true;
2452 len = c_strlen (src, 1);
2453 if (!len
2454 || TREE_CODE (len) != INTEGER_CST)
2455 return false;
2457 if (optimize_function_for_size_p (cfun)
2458 /* If length is zero it's small enough. */
2459 && !integer_zerop (len))
2460 return false;
2462 /* If the source has a known length replace stpcpy with memcpy. */
2463 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2464 if (!fn)
2465 return false;
2467 gimple_seq stmts = NULL;
2468 tree tem = gimple_convert (&stmts, loc, size_type_node, len);
2469 lenp1 = gimple_build (&stmts, loc, PLUS_EXPR, size_type_node,
2470 tem, build_int_cst (size_type_node, 1));
2471 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
2472 gcall *repl = gimple_build_call (fn, 3, dest, src, lenp1);
2473 gimple_set_vuse (repl, gimple_vuse (stmt));
2474 gimple_set_vdef (repl, gimple_vdef (stmt));
2475 if (gimple_vdef (repl)
2476 && TREE_CODE (gimple_vdef (repl)) == SSA_NAME)
2477 SSA_NAME_DEF_STMT (gimple_vdef (repl)) = repl;
2478 gsi_insert_before (gsi, repl, GSI_SAME_STMT);
2479 /* Replace the result with dest + len. */
2480 stmts = NULL;
2481 tem = gimple_convert (&stmts, loc, sizetype, len);
2482 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
2483 gassign *ret = gimple_build_assign (gimple_call_lhs (stmt),
2484 POINTER_PLUS_EXPR, dest, tem);
2485 gsi_replace (gsi, ret, false);
2486 /* Finally fold the memcpy call. */
2487 gimple_stmt_iterator gsi2 = *gsi;
2488 gsi_prev (&gsi2);
2489 fold_stmt (&gsi2);
2490 return true;
2493 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
2494 NULL_TREE if a normal call should be emitted rather than expanding
2495 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
2496 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
2497 passed as second argument. */
2499 static bool
2500 gimple_fold_builtin_snprintf_chk (gimple_stmt_iterator *gsi,
2501 enum built_in_function fcode)
2503 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
2504 tree dest, size, len, fn, fmt, flag;
2505 const char *fmt_str;
2507 /* Verify the required arguments in the original call. */
2508 if (gimple_call_num_args (stmt) < 5)
2509 return false;
2511 dest = gimple_call_arg (stmt, 0);
2512 len = gimple_call_arg (stmt, 1);
2513 flag = gimple_call_arg (stmt, 2);
2514 size = gimple_call_arg (stmt, 3);
2515 fmt = gimple_call_arg (stmt, 4);
2517 if (! tree_fits_uhwi_p (size))
2518 return false;
2520 if (! integer_all_onesp (size))
2522 tree maxlen = get_maxval_strlen (len, 2);
2523 if (! tree_fits_uhwi_p (len))
2525 /* If LEN is not constant, try MAXLEN too.
2526 For MAXLEN only allow optimizing into non-_ocs function
2527 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2528 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
2529 return false;
2531 else
2532 maxlen = len;
2534 if (tree_int_cst_lt (size, maxlen))
2535 return false;
2538 if (!init_target_chars ())
2539 return false;
2541 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
2542 or if format doesn't contain % chars or is "%s". */
2543 if (! integer_zerop (flag))
2545 fmt_str = c_getstr (fmt);
2546 if (fmt_str == NULL)
2547 return false;
2548 if (strchr (fmt_str, target_percent) != NULL
2549 && strcmp (fmt_str, target_percent_s))
2550 return false;
2553 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
2554 available. */
2555 fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
2556 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
2557 if (!fn)
2558 return false;
2560 /* Replace the called function and the first 5 argument by 3 retaining
2561 trailing varargs. */
2562 gimple_call_set_fndecl (stmt, fn);
2563 gimple_call_set_fntype (stmt, TREE_TYPE (fn));
2564 gimple_call_set_arg (stmt, 0, dest);
2565 gimple_call_set_arg (stmt, 1, len);
2566 gimple_call_set_arg (stmt, 2, fmt);
2567 for (unsigned i = 3; i < gimple_call_num_args (stmt) - 2; ++i)
2568 gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2));
2569 gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2);
2570 fold_stmt (gsi);
2571 return true;
2574 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
2575 Return NULL_TREE if a normal call should be emitted rather than
2576 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
2577 or BUILT_IN_VSPRINTF_CHK. */
2579 static bool
2580 gimple_fold_builtin_sprintf_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;
2586 unsigned nargs = gimple_call_num_args (stmt);
2588 /* Verify the required arguments in the original call. */
2589 if (nargs < 4)
2590 return false;
2591 dest = gimple_call_arg (stmt, 0);
2592 flag = gimple_call_arg (stmt, 1);
2593 size = gimple_call_arg (stmt, 2);
2594 fmt = gimple_call_arg (stmt, 3);
2596 if (! tree_fits_uhwi_p (size))
2597 return false;
2599 len = NULL_TREE;
2601 if (!init_target_chars ())
2602 return false;
2604 /* Check whether the format is a literal string constant. */
2605 fmt_str = c_getstr (fmt);
2606 if (fmt_str != NULL)
2608 /* If the format doesn't contain % args or %%, we know the size. */
2609 if (strchr (fmt_str, target_percent) == 0)
2611 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
2612 len = build_int_cstu (size_type_node, strlen (fmt_str));
2614 /* If the format is "%s" and first ... argument is a string literal,
2615 we know the size too. */
2616 else if (fcode == BUILT_IN_SPRINTF_CHK
2617 && strcmp (fmt_str, target_percent_s) == 0)
2619 tree arg;
2621 if (nargs == 5)
2623 arg = gimple_call_arg (stmt, 4);
2624 if (POINTER_TYPE_P (TREE_TYPE (arg)))
2626 len = c_strlen (arg, 1);
2627 if (! len || ! tree_fits_uhwi_p (len))
2628 len = NULL_TREE;
2634 if (! integer_all_onesp (size))
2636 if (! len || ! tree_int_cst_lt (len, size))
2637 return false;
2640 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
2641 or if format doesn't contain % chars or is "%s". */
2642 if (! integer_zerop (flag))
2644 if (fmt_str == NULL)
2645 return false;
2646 if (strchr (fmt_str, target_percent) != NULL
2647 && strcmp (fmt_str, target_percent_s))
2648 return false;
2651 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
2652 fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
2653 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
2654 if (!fn)
2655 return false;
2657 /* Replace the called function and the first 4 argument by 2 retaining
2658 trailing varargs. */
2659 gimple_call_set_fndecl (stmt, fn);
2660 gimple_call_set_fntype (stmt, TREE_TYPE (fn));
2661 gimple_call_set_arg (stmt, 0, dest);
2662 gimple_call_set_arg (stmt, 1, fmt);
2663 for (unsigned i = 2; i < gimple_call_num_args (stmt) - 2; ++i)
2664 gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2));
2665 gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2);
2666 fold_stmt (gsi);
2667 return true;
2670 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
2671 ORIG may be null if this is a 2-argument call. We don't attempt to
2672 simplify calls with more than 3 arguments.
2674 Return NULL_TREE if no simplification was possible, otherwise return the
2675 simplified form of the call as a tree. If IGNORED is true, it means that
2676 the caller does not use the returned value of the function. */
2678 static bool
2679 gimple_fold_builtin_sprintf (gimple_stmt_iterator *gsi)
2681 gimple *stmt = gsi_stmt (*gsi);
2682 tree dest = gimple_call_arg (stmt, 0);
2683 tree fmt = gimple_call_arg (stmt, 1);
2684 tree orig = NULL_TREE;
2685 const char *fmt_str = NULL;
2687 /* Verify the required arguments in the original call. We deal with two
2688 types of sprintf() calls: 'sprintf (str, fmt)' and
2689 'sprintf (dest, "%s", orig)'. */
2690 if (gimple_call_num_args (stmt) > 3)
2691 return false;
2693 if (gimple_call_num_args (stmt) == 3)
2694 orig = gimple_call_arg (stmt, 2);
2696 /* Check whether the format is a literal string constant. */
2697 fmt_str = c_getstr (fmt);
2698 if (fmt_str == NULL)
2699 return false;
2701 if (!init_target_chars ())
2702 return false;
2704 /* If the format doesn't contain % args or %%, use strcpy. */
2705 if (strchr (fmt_str, target_percent) == NULL)
2707 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
2709 if (!fn)
2710 return false;
2712 /* Don't optimize sprintf (buf, "abc", ptr++). */
2713 if (orig)
2714 return false;
2716 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
2717 'format' is known to contain no % formats. */
2718 gimple_seq stmts = NULL;
2719 gimple *repl = gimple_build_call (fn, 2, dest, fmt);
2720 gimple_seq_add_stmt_without_update (&stmts, repl);
2721 if (gimple_call_lhs (stmt))
2723 repl = gimple_build_assign (gimple_call_lhs (stmt),
2724 build_int_cst (integer_type_node,
2725 strlen (fmt_str)));
2726 gimple_seq_add_stmt_without_update (&stmts, repl);
2727 gsi_replace_with_seq_vops (gsi, stmts);
2728 /* gsi now points at the assignment to the lhs, get a
2729 stmt iterator to the memcpy call.
2730 ??? We can't use gsi_for_stmt as that doesn't work when the
2731 CFG isn't built yet. */
2732 gimple_stmt_iterator gsi2 = *gsi;
2733 gsi_prev (&gsi2);
2734 fold_stmt (&gsi2);
2736 else
2738 gsi_replace_with_seq_vops (gsi, stmts);
2739 fold_stmt (gsi);
2741 return true;
2744 /* If the format is "%s", use strcpy if the result isn't used. */
2745 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
2747 tree fn;
2748 fn = builtin_decl_implicit (BUILT_IN_STRCPY);
2750 if (!fn)
2751 return false;
2753 /* Don't crash on sprintf (str1, "%s"). */
2754 if (!orig)
2755 return false;
2757 tree orig_len = NULL_TREE;
2758 if (gimple_call_lhs (stmt))
2760 orig_len = get_maxval_strlen (orig, 0);
2761 if (!orig_len)
2762 return false;
2765 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
2766 gimple_seq stmts = NULL;
2767 gimple *repl = gimple_build_call (fn, 2, dest, orig);
2768 gimple_seq_add_stmt_without_update (&stmts, repl);
2769 if (gimple_call_lhs (stmt))
2771 if (!useless_type_conversion_p (integer_type_node,
2772 TREE_TYPE (orig_len)))
2773 orig_len = fold_convert (integer_type_node, orig_len);
2774 repl = gimple_build_assign (gimple_call_lhs (stmt), orig_len);
2775 gimple_seq_add_stmt_without_update (&stmts, repl);
2776 gsi_replace_with_seq_vops (gsi, stmts);
2777 /* gsi now points at the assignment to the lhs, get a
2778 stmt iterator to the memcpy call.
2779 ??? We can't use gsi_for_stmt as that doesn't work when the
2780 CFG isn't built yet. */
2781 gimple_stmt_iterator gsi2 = *gsi;
2782 gsi_prev (&gsi2);
2783 fold_stmt (&gsi2);
2785 else
2787 gsi_replace_with_seq_vops (gsi, stmts);
2788 fold_stmt (gsi);
2790 return true;
2792 return false;
2795 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
2796 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
2797 attempt to simplify calls with more than 4 arguments.
2799 Return NULL_TREE if no simplification was possible, otherwise return the
2800 simplified form of the call as a tree. If IGNORED is true, it means that
2801 the caller does not use the returned value of the function. */
2803 static bool
2804 gimple_fold_builtin_snprintf (gimple_stmt_iterator *gsi)
2806 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
2807 tree dest = gimple_call_arg (stmt, 0);
2808 tree destsize = gimple_call_arg (stmt, 1);
2809 tree fmt = gimple_call_arg (stmt, 2);
2810 tree orig = NULL_TREE;
2811 const char *fmt_str = NULL;
2813 if (gimple_call_num_args (stmt) > 4)
2814 return false;
2816 if (gimple_call_num_args (stmt) == 4)
2817 orig = gimple_call_arg (stmt, 3);
2819 if (!tree_fits_uhwi_p (destsize))
2820 return false;
2821 unsigned HOST_WIDE_INT destlen = tree_to_uhwi (destsize);
2823 /* Check whether the format is a literal string constant. */
2824 fmt_str = c_getstr (fmt);
2825 if (fmt_str == NULL)
2826 return false;
2828 if (!init_target_chars ())
2829 return false;
2831 /* If the format doesn't contain % args or %%, use strcpy. */
2832 if (strchr (fmt_str, target_percent) == NULL)
2834 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
2835 if (!fn)
2836 return false;
2838 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
2839 if (orig)
2840 return false;
2842 /* We could expand this as
2843 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
2844 or to
2845 memcpy (str, fmt_with_nul_at_cstm1, cst);
2846 but in the former case that might increase code size
2847 and in the latter case grow .rodata section too much.
2848 So punt for now. */
2849 size_t len = strlen (fmt_str);
2850 if (len >= destlen)
2851 return false;
2853 gimple_seq stmts = NULL;
2854 gimple *repl = gimple_build_call (fn, 2, dest, fmt);
2855 gimple_seq_add_stmt_without_update (&stmts, repl);
2856 if (gimple_call_lhs (stmt))
2858 repl = gimple_build_assign (gimple_call_lhs (stmt),
2859 build_int_cst (integer_type_node, len));
2860 gimple_seq_add_stmt_without_update (&stmts, repl);
2861 gsi_replace_with_seq_vops (gsi, stmts);
2862 /* gsi now points at the assignment to the lhs, get a
2863 stmt iterator to the memcpy call.
2864 ??? We can't use gsi_for_stmt as that doesn't work when the
2865 CFG isn't built yet. */
2866 gimple_stmt_iterator gsi2 = *gsi;
2867 gsi_prev (&gsi2);
2868 fold_stmt (&gsi2);
2870 else
2872 gsi_replace_with_seq_vops (gsi, stmts);
2873 fold_stmt (gsi);
2875 return true;
2878 /* If the format is "%s", use strcpy if the result isn't used. */
2879 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
2881 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
2882 if (!fn)
2883 return false;
2885 /* Don't crash on snprintf (str1, cst, "%s"). */
2886 if (!orig)
2887 return false;
2889 tree orig_len = get_maxval_strlen (orig, 0);
2890 if (!orig_len || TREE_CODE (orig_len) != INTEGER_CST)
2891 return false;
2893 /* We could expand this as
2894 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
2895 or to
2896 memcpy (str1, str2_with_nul_at_cstm1, cst);
2897 but in the former case that might increase code size
2898 and in the latter case grow .rodata section too much.
2899 So punt for now. */
2900 if (compare_tree_int (orig_len, destlen) >= 0)
2901 return false;
2903 /* Convert snprintf (str1, cst, "%s", str2) into
2904 strcpy (str1, str2) if strlen (str2) < cst. */
2905 gimple_seq stmts = NULL;
2906 gimple *repl = gimple_build_call (fn, 2, dest, orig);
2907 gimple_seq_add_stmt_without_update (&stmts, repl);
2908 if (gimple_call_lhs (stmt))
2910 if (!useless_type_conversion_p (integer_type_node,
2911 TREE_TYPE (orig_len)))
2912 orig_len = fold_convert (integer_type_node, orig_len);
2913 repl = gimple_build_assign (gimple_call_lhs (stmt), orig_len);
2914 gimple_seq_add_stmt_without_update (&stmts, repl);
2915 gsi_replace_with_seq_vops (gsi, stmts);
2916 /* gsi now points at the assignment to the lhs, get a
2917 stmt iterator to the memcpy call.
2918 ??? We can't use gsi_for_stmt as that doesn't work when the
2919 CFG isn't built yet. */
2920 gimple_stmt_iterator gsi2 = *gsi;
2921 gsi_prev (&gsi2);
2922 fold_stmt (&gsi2);
2924 else
2926 gsi_replace_with_seq_vops (gsi, stmts);
2927 fold_stmt (gsi);
2929 return true;
2931 return false;
2934 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
2935 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
2936 more than 3 arguments, and ARG may be null in the 2-argument case.
2938 Return NULL_TREE if no simplification was possible, otherwise return the
2939 simplified form of the call as a tree. FCODE is the BUILT_IN_*
2940 code of the function to be simplified. */
2942 static bool
2943 gimple_fold_builtin_fprintf (gimple_stmt_iterator *gsi,
2944 tree fp, tree fmt, tree arg,
2945 enum built_in_function fcode)
2947 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
2948 tree fn_fputc, fn_fputs;
2949 const char *fmt_str = NULL;
2951 /* If the return value is used, don't do the transformation. */
2952 if (gimple_call_lhs (stmt) != NULL_TREE)
2953 return false;
2955 /* Check whether the format is a literal string constant. */
2956 fmt_str = c_getstr (fmt);
2957 if (fmt_str == NULL)
2958 return false;
2960 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
2962 /* If we're using an unlocked function, assume the other
2963 unlocked functions exist explicitly. */
2964 fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
2965 fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
2967 else
2969 fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
2970 fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
2973 if (!init_target_chars ())
2974 return false;
2976 /* If the format doesn't contain % args or %%, use strcpy. */
2977 if (strchr (fmt_str, target_percent) == NULL)
2979 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
2980 && arg)
2981 return false;
2983 /* If the format specifier was "", fprintf does nothing. */
2984 if (fmt_str[0] == '\0')
2986 replace_call_with_value (gsi, NULL_TREE);
2987 return true;
2990 /* When "string" doesn't contain %, replace all cases of
2991 fprintf (fp, string) with fputs (string, fp). The fputs
2992 builtin will take care of special cases like length == 1. */
2993 if (fn_fputs)
2995 gcall *repl = gimple_build_call (fn_fputs, 2, fmt, fp);
2996 replace_call_with_call_and_fold (gsi, repl);
2997 return true;
3001 /* The other optimizations can be done only on the non-va_list variants. */
3002 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
3003 return false;
3005 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
3006 else if (strcmp (fmt_str, target_percent_s) == 0)
3008 if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
3009 return false;
3010 if (fn_fputs)
3012 gcall *repl = gimple_build_call (fn_fputs, 2, arg, fp);
3013 replace_call_with_call_and_fold (gsi, repl);
3014 return true;
3018 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
3019 else if (strcmp (fmt_str, target_percent_c) == 0)
3021 if (!arg
3022 || ! useless_type_conversion_p (integer_type_node, TREE_TYPE (arg)))
3023 return false;
3024 if (fn_fputc)
3026 gcall *repl = gimple_build_call (fn_fputc, 2, arg, fp);
3027 replace_call_with_call_and_fold (gsi, repl);
3028 return true;
3032 return false;
3035 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
3036 FMT and ARG are the arguments to the call; we don't fold cases with
3037 more than 2 arguments, and ARG may be null if this is a 1-argument case.
3039 Return NULL_TREE if no simplification was possible, otherwise return the
3040 simplified form of the call as a tree. FCODE is the BUILT_IN_*
3041 code of the function to be simplified. */
3043 static bool
3044 gimple_fold_builtin_printf (gimple_stmt_iterator *gsi, tree fmt,
3045 tree arg, enum built_in_function fcode)
3047 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3048 tree fn_putchar, fn_puts, newarg;
3049 const char *fmt_str = NULL;
3051 /* If the return value is used, don't do the transformation. */
3052 if (gimple_call_lhs (stmt) != NULL_TREE)
3053 return false;
3055 /* Check whether the format is a literal string constant. */
3056 fmt_str = c_getstr (fmt);
3057 if (fmt_str == NULL)
3058 return false;
3060 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
3062 /* If we're using an unlocked function, assume the other
3063 unlocked functions exist explicitly. */
3064 fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
3065 fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
3067 else
3069 fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
3070 fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
3073 if (!init_target_chars ())
3074 return false;
3076 if (strcmp (fmt_str, target_percent_s) == 0
3077 || strchr (fmt_str, target_percent) == NULL)
3079 const char *str;
3081 if (strcmp (fmt_str, target_percent_s) == 0)
3083 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
3084 return false;
3086 if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
3087 return false;
3089 str = c_getstr (arg);
3090 if (str == NULL)
3091 return false;
3093 else
3095 /* The format specifier doesn't contain any '%' characters. */
3096 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
3097 && arg)
3098 return false;
3099 str = fmt_str;
3102 /* If the string was "", printf does nothing. */
3103 if (str[0] == '\0')
3105 replace_call_with_value (gsi, NULL_TREE);
3106 return true;
3109 /* If the string has length of 1, call putchar. */
3110 if (str[1] == '\0')
3112 /* Given printf("c"), (where c is any one character,)
3113 convert "c"[0] to an int and pass that to the replacement
3114 function. */
3115 newarg = build_int_cst (integer_type_node, str[0]);
3116 if (fn_putchar)
3118 gcall *repl = gimple_build_call (fn_putchar, 1, newarg);
3119 replace_call_with_call_and_fold (gsi, repl);
3120 return true;
3123 else
3125 /* If the string was "string\n", call puts("string"). */
3126 size_t len = strlen (str);
3127 if ((unsigned char)str[len - 1] == target_newline
3128 && (size_t) (int) len == len
3129 && (int) len > 0)
3131 char *newstr;
3132 tree offset_node, string_cst;
3134 /* Create a NUL-terminated string that's one char shorter
3135 than the original, stripping off the trailing '\n'. */
3136 newarg = build_string_literal (len, str);
3137 string_cst = string_constant (newarg, &offset_node);
3138 gcc_checking_assert (string_cst
3139 && (TREE_STRING_LENGTH (string_cst)
3140 == (int) len)
3141 && integer_zerop (offset_node)
3142 && (unsigned char)
3143 TREE_STRING_POINTER (string_cst)[len - 1]
3144 == target_newline);
3145 /* build_string_literal creates a new STRING_CST,
3146 modify it in place to avoid double copying. */
3147 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
3148 newstr[len - 1] = '\0';
3149 if (fn_puts)
3151 gcall *repl = gimple_build_call (fn_puts, 1, newarg);
3152 replace_call_with_call_and_fold (gsi, repl);
3153 return true;
3156 else
3157 /* We'd like to arrange to call fputs(string,stdout) here,
3158 but we need stdout and don't have a way to get it yet. */
3159 return false;
3163 /* The other optimizations can be done only on the non-va_list variants. */
3164 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
3165 return false;
3167 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
3168 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
3170 if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
3171 return false;
3172 if (fn_puts)
3174 gcall *repl = gimple_build_call (fn_puts, 1, arg);
3175 replace_call_with_call_and_fold (gsi, repl);
3176 return true;
3180 /* If the format specifier was "%c", call __builtin_putchar(arg). */
3181 else if (strcmp (fmt_str, target_percent_c) == 0)
3183 if (!arg || ! useless_type_conversion_p (integer_type_node,
3184 TREE_TYPE (arg)))
3185 return false;
3186 if (fn_putchar)
3188 gcall *repl = gimple_build_call (fn_putchar, 1, arg);
3189 replace_call_with_call_and_fold (gsi, repl);
3190 return true;
3194 return false;
3199 /* Fold a call to __builtin_strlen with known length LEN. */
3201 static bool
3202 gimple_fold_builtin_strlen (gimple_stmt_iterator *gsi)
3204 gimple *stmt = gsi_stmt (*gsi);
3205 tree len = get_maxval_strlen (gimple_call_arg (stmt, 0), 0);
3206 if (!len)
3207 return false;
3208 len = force_gimple_operand_gsi (gsi, len, true, NULL, true, GSI_SAME_STMT);
3209 replace_call_with_value (gsi, len);
3210 return true;
3213 /* Fold a call to __builtin_acc_on_device. */
3215 static bool
3216 gimple_fold_builtin_acc_on_device (gimple_stmt_iterator *gsi, tree arg0)
3218 /* Defer folding until we know which compiler we're in. */
3219 if (symtab->state != EXPANSION)
3220 return false;
3222 unsigned val_host = GOMP_DEVICE_HOST;
3223 unsigned val_dev = GOMP_DEVICE_NONE;
3225 #ifdef ACCEL_COMPILER
3226 val_host = GOMP_DEVICE_NOT_HOST;
3227 val_dev = ACCEL_COMPILER_acc_device;
3228 #endif
3230 location_t loc = gimple_location (gsi_stmt (*gsi));
3232 tree host_eq = make_ssa_name (boolean_type_node);
3233 gimple *host_ass = gimple_build_assign
3234 (host_eq, EQ_EXPR, arg0, build_int_cst (TREE_TYPE (arg0), val_host));
3235 gimple_set_location (host_ass, loc);
3236 gsi_insert_before (gsi, host_ass, GSI_SAME_STMT);
3238 tree dev_eq = make_ssa_name (boolean_type_node);
3239 gimple *dev_ass = gimple_build_assign
3240 (dev_eq, EQ_EXPR, arg0, build_int_cst (TREE_TYPE (arg0), val_dev));
3241 gimple_set_location (dev_ass, loc);
3242 gsi_insert_before (gsi, dev_ass, GSI_SAME_STMT);
3244 tree result = make_ssa_name (boolean_type_node);
3245 gimple *result_ass = gimple_build_assign
3246 (result, BIT_IOR_EXPR, host_eq, dev_eq);
3247 gimple_set_location (result_ass, loc);
3248 gsi_insert_before (gsi, result_ass, GSI_SAME_STMT);
3250 replace_call_with_value (gsi, result);
3252 return true;
3255 /* Fold the non-target builtin at *GSI and return whether any simplification
3256 was made. */
3258 static bool
3259 gimple_fold_builtin (gimple_stmt_iterator *gsi)
3261 gcall *stmt = as_a <gcall *>(gsi_stmt (*gsi));
3262 tree callee = gimple_call_fndecl (stmt);
3264 /* Give up for always_inline inline builtins until they are
3265 inlined. */
3266 if (avoid_folding_inline_builtin (callee))
3267 return false;
3269 unsigned n = gimple_call_num_args (stmt);
3270 enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
3271 switch (fcode)
3273 case BUILT_IN_BZERO:
3274 return gimple_fold_builtin_memset (gsi, integer_zero_node,
3275 gimple_call_arg (stmt, 1));
3276 case BUILT_IN_MEMSET:
3277 return gimple_fold_builtin_memset (gsi,
3278 gimple_call_arg (stmt, 1),
3279 gimple_call_arg (stmt, 2));
3280 case BUILT_IN_BCOPY:
3281 return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 1),
3282 gimple_call_arg (stmt, 0), 3);
3283 case BUILT_IN_MEMCPY:
3284 return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
3285 gimple_call_arg (stmt, 1), 0);
3286 case BUILT_IN_MEMPCPY:
3287 return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
3288 gimple_call_arg (stmt, 1), 1);
3289 case BUILT_IN_MEMMOVE:
3290 return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
3291 gimple_call_arg (stmt, 1), 3);
3292 case BUILT_IN_SPRINTF_CHK:
3293 case BUILT_IN_VSPRINTF_CHK:
3294 return gimple_fold_builtin_sprintf_chk (gsi, fcode);
3295 case BUILT_IN_STRCAT_CHK:
3296 return gimple_fold_builtin_strcat_chk (gsi);
3297 case BUILT_IN_STRNCAT_CHK:
3298 return gimple_fold_builtin_strncat_chk (gsi);
3299 case BUILT_IN_STRLEN:
3300 return gimple_fold_builtin_strlen (gsi);
3301 case BUILT_IN_STRCPY:
3302 return gimple_fold_builtin_strcpy (gsi,
3303 gimple_call_arg (stmt, 0),
3304 gimple_call_arg (stmt, 1));
3305 case BUILT_IN_STRNCPY:
3306 return gimple_fold_builtin_strncpy (gsi,
3307 gimple_call_arg (stmt, 0),
3308 gimple_call_arg (stmt, 1),
3309 gimple_call_arg (stmt, 2));
3310 case BUILT_IN_STRCAT:
3311 return gimple_fold_builtin_strcat (gsi, gimple_call_arg (stmt, 0),
3312 gimple_call_arg (stmt, 1));
3313 case BUILT_IN_STRNCAT:
3314 return gimple_fold_builtin_strncat (gsi);
3315 case BUILT_IN_INDEX:
3316 case BUILT_IN_STRCHR:
3317 return gimple_fold_builtin_strchr (gsi, false);
3318 case BUILT_IN_RINDEX:
3319 case BUILT_IN_STRRCHR:
3320 return gimple_fold_builtin_strchr (gsi, true);
3321 case BUILT_IN_STRSTR:
3322 return gimple_fold_builtin_strstr (gsi);
3323 case BUILT_IN_STRCMP:
3324 case BUILT_IN_STRCASECMP:
3325 case BUILT_IN_STRNCMP:
3326 case BUILT_IN_STRNCASECMP:
3327 return gimple_fold_builtin_string_compare (gsi);
3328 case BUILT_IN_MEMCHR:
3329 return gimple_fold_builtin_memchr (gsi);
3330 case BUILT_IN_FPUTS:
3331 return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
3332 gimple_call_arg (stmt, 1), false);
3333 case BUILT_IN_FPUTS_UNLOCKED:
3334 return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
3335 gimple_call_arg (stmt, 1), true);
3336 case BUILT_IN_MEMCPY_CHK:
3337 case BUILT_IN_MEMPCPY_CHK:
3338 case BUILT_IN_MEMMOVE_CHK:
3339 case BUILT_IN_MEMSET_CHK:
3340 return gimple_fold_builtin_memory_chk (gsi,
3341 gimple_call_arg (stmt, 0),
3342 gimple_call_arg (stmt, 1),
3343 gimple_call_arg (stmt, 2),
3344 gimple_call_arg (stmt, 3),
3345 fcode);
3346 case BUILT_IN_STPCPY:
3347 return gimple_fold_builtin_stpcpy (gsi);
3348 case BUILT_IN_STRCPY_CHK:
3349 case BUILT_IN_STPCPY_CHK:
3350 return gimple_fold_builtin_stxcpy_chk (gsi,
3351 gimple_call_arg (stmt, 0),
3352 gimple_call_arg (stmt, 1),
3353 gimple_call_arg (stmt, 2),
3354 fcode);
3355 case BUILT_IN_STRNCPY_CHK:
3356 case BUILT_IN_STPNCPY_CHK:
3357 return gimple_fold_builtin_stxncpy_chk (gsi,
3358 gimple_call_arg (stmt, 0),
3359 gimple_call_arg (stmt, 1),
3360 gimple_call_arg (stmt, 2),
3361 gimple_call_arg (stmt, 3),
3362 fcode);
3363 case BUILT_IN_SNPRINTF_CHK:
3364 case BUILT_IN_VSNPRINTF_CHK:
3365 return gimple_fold_builtin_snprintf_chk (gsi, fcode);
3366 case BUILT_IN_SNPRINTF:
3367 return gimple_fold_builtin_snprintf (gsi);
3368 case BUILT_IN_SPRINTF:
3369 return gimple_fold_builtin_sprintf (gsi);
3370 case BUILT_IN_FPRINTF:
3371 case BUILT_IN_FPRINTF_UNLOCKED:
3372 case BUILT_IN_VFPRINTF:
3373 if (n == 2 || n == 3)
3374 return gimple_fold_builtin_fprintf (gsi,
3375 gimple_call_arg (stmt, 0),
3376 gimple_call_arg (stmt, 1),
3377 n == 3
3378 ? gimple_call_arg (stmt, 2)
3379 : NULL_TREE,
3380 fcode);
3381 break;
3382 case BUILT_IN_FPRINTF_CHK:
3383 case BUILT_IN_VFPRINTF_CHK:
3384 if (n == 3 || n == 4)
3385 return gimple_fold_builtin_fprintf (gsi,
3386 gimple_call_arg (stmt, 0),
3387 gimple_call_arg (stmt, 2),
3388 n == 4
3389 ? gimple_call_arg (stmt, 3)
3390 : NULL_TREE,
3391 fcode);
3392 break;
3393 case BUILT_IN_PRINTF:
3394 case BUILT_IN_PRINTF_UNLOCKED:
3395 case BUILT_IN_VPRINTF:
3396 if (n == 1 || n == 2)
3397 return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 0),
3398 n == 2
3399 ? gimple_call_arg (stmt, 1)
3400 : NULL_TREE, fcode);
3401 break;
3402 case BUILT_IN_PRINTF_CHK:
3403 case BUILT_IN_VPRINTF_CHK:
3404 if (n == 2 || n == 3)
3405 return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 1),
3406 n == 3
3407 ? gimple_call_arg (stmt, 2)
3408 : NULL_TREE, fcode);
3409 break;
3410 case BUILT_IN_ACC_ON_DEVICE:
3411 return gimple_fold_builtin_acc_on_device (gsi,
3412 gimple_call_arg (stmt, 0));
3413 default:;
3416 /* Try the generic builtin folder. */
3417 bool ignore = (gimple_call_lhs (stmt) == NULL);
3418 tree result = fold_call_stmt (stmt, ignore);
3419 if (result)
3421 if (ignore)
3422 STRIP_NOPS (result);
3423 else
3424 result = fold_convert (gimple_call_return_type (stmt), result);
3425 if (!update_call_from_tree (gsi, result))
3426 gimplify_and_update_call_from_tree (gsi, result);
3427 return true;
3430 return false;
3433 /* Transform IFN_GOACC_DIM_SIZE and IFN_GOACC_DIM_POS internal
3434 function calls to constants, where possible. */
3436 static tree
3437 fold_internal_goacc_dim (const gimple *call)
3439 int axis = oacc_get_ifn_dim_arg (call);
3440 int size = oacc_get_fn_dim_size (current_function_decl, axis);
3441 bool is_pos = gimple_call_internal_fn (call) == IFN_GOACC_DIM_POS;
3442 tree result = NULL_TREE;
3444 /* If the size is 1, or we only want the size and it is not dynamic,
3445 we know the answer. */
3446 if (size == 1 || (!is_pos && size))
3448 tree type = TREE_TYPE (gimple_call_lhs (call));
3449 result = build_int_cst (type, size - is_pos);
3452 return result;
3455 /* Return true if stmt is __atomic_compare_exchange_N call which is suitable
3456 for conversion into ATOMIC_COMPARE_EXCHANGE if the second argument is
3457 &var where var is only addressable because of such calls. */
3459 bool
3460 optimize_atomic_compare_exchange_p (gimple *stmt)
3462 if (gimple_call_num_args (stmt) != 6
3463 || !flag_inline_atomics
3464 || !optimize
3465 || (flag_sanitize & (SANITIZE_THREAD | SANITIZE_ADDRESS)) != 0
3466 || !gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)
3467 || !gimple_vdef (stmt)
3468 || !gimple_vuse (stmt))
3469 return false;
3471 tree fndecl = gimple_call_fndecl (stmt);
3472 switch (DECL_FUNCTION_CODE (fndecl))
3474 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
3475 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
3476 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
3477 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
3478 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
3479 break;
3480 default:
3481 return false;
3484 tree expected = gimple_call_arg (stmt, 1);
3485 if (TREE_CODE (expected) != ADDR_EXPR
3486 || !SSA_VAR_P (TREE_OPERAND (expected, 0)))
3487 return false;
3489 tree etype = TREE_TYPE (TREE_OPERAND (expected, 0));
3490 if (!is_gimple_reg_type (etype)
3491 || !auto_var_in_fn_p (TREE_OPERAND (expected, 0), current_function_decl)
3492 || TREE_THIS_VOLATILE (etype)
3493 || VECTOR_TYPE_P (etype)
3494 || TREE_CODE (etype) == COMPLEX_TYPE
3495 /* Don't optimize floating point expected vars, VIEW_CONVERT_EXPRs
3496 might not preserve all the bits. See PR71716. */
3497 || SCALAR_FLOAT_TYPE_P (etype)
3498 || TYPE_PRECISION (etype) != GET_MODE_BITSIZE (TYPE_MODE (etype)))
3499 return false;
3501 tree weak = gimple_call_arg (stmt, 3);
3502 if (!integer_zerop (weak) && !integer_onep (weak))
3503 return false;
3505 tree parmt = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3506 tree itype = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt)));
3507 machine_mode mode = TYPE_MODE (itype);
3509 if (direct_optab_handler (atomic_compare_and_swap_optab, mode)
3510 == CODE_FOR_nothing
3511 && optab_handler (sync_compare_and_swap_optab, mode) == CODE_FOR_nothing)
3512 return false;
3514 if (int_size_in_bytes (etype) != GET_MODE_SIZE (mode))
3515 return false;
3517 return true;
3520 /* Fold
3521 r = __atomic_compare_exchange_N (p, &e, d, w, s, f);
3522 into
3523 _Complex uintN_t t = ATOMIC_COMPARE_EXCHANGE (p, e, d, w * 256 + N, s, f);
3524 i = IMAGPART_EXPR <t>;
3525 r = (_Bool) i;
3526 e = REALPART_EXPR <t>; */
3528 void
3529 fold_builtin_atomic_compare_exchange (gimple_stmt_iterator *gsi)
3531 gimple *stmt = gsi_stmt (*gsi);
3532 tree fndecl = gimple_call_fndecl (stmt);
3533 tree parmt = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3534 tree itype = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt)));
3535 tree ctype = build_complex_type (itype);
3536 tree expected = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
3537 bool throws = false;
3538 edge e = NULL;
3539 gimple *g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)),
3540 expected);
3541 gsi_insert_before (gsi, g, GSI_SAME_STMT);
3542 gimple_stmt_iterator gsiret = gsi_for_stmt (g);
3543 if (!useless_type_conversion_p (itype, TREE_TYPE (expected)))
3545 g = gimple_build_assign (make_ssa_name (itype), VIEW_CONVERT_EXPR,
3546 build1 (VIEW_CONVERT_EXPR, itype,
3547 gimple_assign_lhs (g)));
3548 gsi_insert_before (gsi, g, GSI_SAME_STMT);
3550 int flag = (integer_onep (gimple_call_arg (stmt, 3)) ? 256 : 0)
3551 + int_size_in_bytes (itype);
3552 g = gimple_build_call_internal (IFN_ATOMIC_COMPARE_EXCHANGE, 6,
3553 gimple_call_arg (stmt, 0),
3554 gimple_assign_lhs (g),
3555 gimple_call_arg (stmt, 2),
3556 build_int_cst (integer_type_node, flag),
3557 gimple_call_arg (stmt, 4),
3558 gimple_call_arg (stmt, 5));
3559 tree lhs = make_ssa_name (ctype);
3560 gimple_call_set_lhs (g, lhs);
3561 gimple_set_vdef (g, gimple_vdef (stmt));
3562 gimple_set_vuse (g, gimple_vuse (stmt));
3563 SSA_NAME_DEF_STMT (gimple_vdef (g)) = g;
3564 tree oldlhs = gimple_call_lhs (stmt);
3565 if (stmt_can_throw_internal (stmt))
3567 throws = true;
3568 e = find_fallthru_edge (gsi_bb (*gsi)->succs);
3570 gimple_call_set_nothrow (as_a <gcall *> (g),
3571 gimple_call_nothrow_p (as_a <gcall *> (stmt)));
3572 gimple_call_set_lhs (stmt, NULL_TREE);
3573 gsi_replace (gsi, g, true);
3574 if (oldlhs)
3576 g = gimple_build_assign (make_ssa_name (itype), IMAGPART_EXPR,
3577 build1 (IMAGPART_EXPR, itype, lhs));
3578 if (throws)
3580 gsi_insert_on_edge_immediate (e, g);
3581 *gsi = gsi_for_stmt (g);
3583 else
3584 gsi_insert_after (gsi, g, GSI_NEW_STMT);
3585 g = gimple_build_assign (oldlhs, NOP_EXPR, gimple_assign_lhs (g));
3586 gsi_insert_after (gsi, g, GSI_NEW_STMT);
3588 g = gimple_build_assign (make_ssa_name (itype), REALPART_EXPR,
3589 build1 (REALPART_EXPR, itype, lhs));
3590 if (throws && oldlhs == NULL_TREE)
3592 gsi_insert_on_edge_immediate (e, g);
3593 *gsi = gsi_for_stmt (g);
3595 else
3596 gsi_insert_after (gsi, g, GSI_NEW_STMT);
3597 if (!useless_type_conversion_p (TREE_TYPE (expected), itype))
3599 g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)),
3600 VIEW_CONVERT_EXPR,
3601 build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expected),
3602 gimple_assign_lhs (g)));
3603 gsi_insert_after (gsi, g, GSI_NEW_STMT);
3605 g = gimple_build_assign (expected, SSA_NAME, gimple_assign_lhs (g));
3606 gsi_insert_after (gsi, g, GSI_NEW_STMT);
3607 *gsi = gsiret;
3610 /* Return true if ARG0 CODE ARG1 in infinite signed precision operation
3611 doesn't fit into TYPE. The test for overflow should be regardless of
3612 -fwrapv, and even for unsigned types. */
3614 bool
3615 arith_overflowed_p (enum tree_code code, const_tree type,
3616 const_tree arg0, const_tree arg1)
3618 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) widest2_int;
3619 typedef generic_wide_int <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> >
3620 widest2_int_cst;
3621 widest2_int warg0 = widest2_int_cst (arg0);
3622 widest2_int warg1 = widest2_int_cst (arg1);
3623 widest2_int wres;
3624 switch (code)
3626 case PLUS_EXPR: wres = wi::add (warg0, warg1); break;
3627 case MINUS_EXPR: wres = wi::sub (warg0, warg1); break;
3628 case MULT_EXPR: wres = wi::mul (warg0, warg1); break;
3629 default: gcc_unreachable ();
3631 signop sign = TYPE_SIGN (type);
3632 if (sign == UNSIGNED && wi::neg_p (wres))
3633 return true;
3634 return wi::min_precision (wres, sign) > TYPE_PRECISION (type);
3637 /* Attempt to fold a call statement referenced by the statement iterator GSI.
3638 The statement may be replaced by another statement, e.g., if the call
3639 simplifies to a constant value. Return true if any changes were made.
3640 It is assumed that the operands have been previously folded. */
3642 static bool
3643 gimple_fold_call (gimple_stmt_iterator *gsi, bool inplace)
3645 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3646 tree callee;
3647 bool changed = false;
3648 unsigned i;
3650 /* Fold *& in call arguments. */
3651 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3652 if (REFERENCE_CLASS_P (gimple_call_arg (stmt, i)))
3654 tree tmp = maybe_fold_reference (gimple_call_arg (stmt, i), false);
3655 if (tmp)
3657 gimple_call_set_arg (stmt, i, tmp);
3658 changed = true;
3662 /* Check for virtual calls that became direct calls. */
3663 callee = gimple_call_fn (stmt);
3664 if (callee && TREE_CODE (callee) == OBJ_TYPE_REF)
3666 if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee)) != NULL_TREE)
3668 if (dump_file && virtual_method_call_p (callee)
3669 && !possible_polymorphic_call_target_p
3670 (callee, stmt, cgraph_node::get (gimple_call_addr_fndecl
3671 (OBJ_TYPE_REF_EXPR (callee)))))
3673 fprintf (dump_file,
3674 "Type inheritance inconsistent devirtualization of ");
3675 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
3676 fprintf (dump_file, " to ");
3677 print_generic_expr (dump_file, callee, TDF_SLIM);
3678 fprintf (dump_file, "\n");
3681 gimple_call_set_fn (stmt, OBJ_TYPE_REF_EXPR (callee));
3682 changed = true;
3684 else if (flag_devirtualize && !inplace && virtual_method_call_p (callee))
3686 bool final;
3687 vec <cgraph_node *>targets
3688 = possible_polymorphic_call_targets (callee, stmt, &final);
3689 if (final && targets.length () <= 1 && dbg_cnt (devirt))
3691 tree lhs = gimple_call_lhs (stmt);
3692 if (dump_enabled_p ())
3694 location_t loc = gimple_location_safe (stmt);
3695 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
3696 "folding virtual function call to %s\n",
3697 targets.length () == 1
3698 ? targets[0]->name ()
3699 : "__builtin_unreachable");
3701 if (targets.length () == 1)
3703 tree fndecl = targets[0]->decl;
3704 gimple_call_set_fndecl (stmt, fndecl);
3705 changed = true;
3706 /* If changing the call to __cxa_pure_virtual
3707 or similar noreturn function, adjust gimple_call_fntype
3708 too. */
3709 if (gimple_call_noreturn_p (stmt)
3710 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl)))
3711 && TYPE_ARG_TYPES (TREE_TYPE (fndecl))
3712 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
3713 == void_type_node))
3714 gimple_call_set_fntype (stmt, TREE_TYPE (fndecl));
3715 /* If the call becomes noreturn, remove the lhs. */
3716 if (lhs
3717 && gimple_call_noreturn_p (stmt)
3718 && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (stmt)))
3719 || should_remove_lhs_p (lhs)))
3721 if (TREE_CODE (lhs) == SSA_NAME)
3723 tree var = create_tmp_var (TREE_TYPE (lhs));
3724 tree def = get_or_create_ssa_default_def (cfun, var);
3725 gimple *new_stmt = gimple_build_assign (lhs, def);
3726 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
3728 gimple_call_set_lhs (stmt, NULL_TREE);
3730 maybe_remove_unused_call_args (cfun, stmt);
3732 else
3734 tree fndecl = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
3735 gimple *new_stmt = gimple_build_call (fndecl, 0);
3736 gimple_set_location (new_stmt, gimple_location (stmt));
3737 if (lhs && TREE_CODE (lhs) == SSA_NAME)
3739 tree var = create_tmp_var (TREE_TYPE (lhs));
3740 tree def = get_or_create_ssa_default_def (cfun, var);
3742 /* To satisfy condition for
3743 cgraph_update_edges_for_call_stmt_node,
3744 we need to preserve GIMPLE_CALL statement
3745 at position of GSI iterator. */
3746 update_call_from_tree (gsi, def);
3747 gsi_insert_before (gsi, new_stmt, GSI_NEW_STMT);
3749 else
3751 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
3752 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
3753 gsi_replace (gsi, new_stmt, false);
3755 return true;
3761 /* Check for indirect calls that became direct calls, and then
3762 no longer require a static chain. */
3763 if (gimple_call_chain (stmt))
3765 tree fn = gimple_call_fndecl (stmt);
3766 if (fn && !DECL_STATIC_CHAIN (fn))
3768 gimple_call_set_chain (stmt, NULL);
3769 changed = true;
3771 else
3773 tree tmp = maybe_fold_reference (gimple_call_chain (stmt), false);
3774 if (tmp)
3776 gimple_call_set_chain (stmt, tmp);
3777 changed = true;
3782 if (inplace)
3783 return changed;
3785 /* Check for builtins that CCP can handle using information not
3786 available in the generic fold routines. */
3787 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3789 if (gimple_fold_builtin (gsi))
3790 changed = true;
3792 else if (gimple_call_builtin_p (stmt, BUILT_IN_MD))
3794 changed |= targetm.gimple_fold_builtin (gsi);
3796 else if (gimple_call_internal_p (stmt))
3798 enum tree_code subcode = ERROR_MARK;
3799 tree result = NULL_TREE;
3800 bool cplx_result = false;
3801 tree overflow = NULL_TREE;
3802 switch (gimple_call_internal_fn (stmt))
3804 case IFN_BUILTIN_EXPECT:
3805 result = fold_builtin_expect (gimple_location (stmt),
3806 gimple_call_arg (stmt, 0),
3807 gimple_call_arg (stmt, 1),
3808 gimple_call_arg (stmt, 2));
3809 break;
3810 case IFN_UBSAN_OBJECT_SIZE:
3811 if (integer_all_onesp (gimple_call_arg (stmt, 2))
3812 || (TREE_CODE (gimple_call_arg (stmt, 1)) == INTEGER_CST
3813 && TREE_CODE (gimple_call_arg (stmt, 2)) == INTEGER_CST
3814 && tree_int_cst_le (gimple_call_arg (stmt, 1),
3815 gimple_call_arg (stmt, 2))))
3817 gsi_replace (gsi, gimple_build_nop (), false);
3818 unlink_stmt_vdef (stmt);
3819 release_defs (stmt);
3820 return true;
3822 break;
3823 case IFN_GOACC_DIM_SIZE:
3824 case IFN_GOACC_DIM_POS:
3825 result = fold_internal_goacc_dim (stmt);
3826 break;
3827 case IFN_UBSAN_CHECK_ADD:
3828 subcode = PLUS_EXPR;
3829 break;
3830 case IFN_UBSAN_CHECK_SUB:
3831 subcode = MINUS_EXPR;
3832 break;
3833 case IFN_UBSAN_CHECK_MUL:
3834 subcode = MULT_EXPR;
3835 break;
3836 case IFN_ADD_OVERFLOW:
3837 subcode = PLUS_EXPR;
3838 cplx_result = true;
3839 break;
3840 case IFN_SUB_OVERFLOW:
3841 subcode = MINUS_EXPR;
3842 cplx_result = true;
3843 break;
3844 case IFN_MUL_OVERFLOW:
3845 subcode = MULT_EXPR;
3846 cplx_result = true;
3847 break;
3848 default:
3849 break;
3851 if (subcode != ERROR_MARK)
3853 tree arg0 = gimple_call_arg (stmt, 0);
3854 tree arg1 = gimple_call_arg (stmt, 1);
3855 tree type = TREE_TYPE (arg0);
3856 if (cplx_result)
3858 tree lhs = gimple_call_lhs (stmt);
3859 if (lhs == NULL_TREE)
3860 type = NULL_TREE;
3861 else
3862 type = TREE_TYPE (TREE_TYPE (lhs));
3864 if (type == NULL_TREE)
3866 /* x = y + 0; x = y - 0; x = y * 0; */
3867 else if (integer_zerop (arg1))
3868 result = subcode == MULT_EXPR ? integer_zero_node : arg0;
3869 /* x = 0 + y; x = 0 * y; */
3870 else if (subcode != MINUS_EXPR && integer_zerop (arg0))
3871 result = subcode == MULT_EXPR ? integer_zero_node : arg1;
3872 /* x = y - y; */
3873 else if (subcode == MINUS_EXPR && operand_equal_p (arg0, arg1, 0))
3874 result = integer_zero_node;
3875 /* x = y * 1; x = 1 * y; */
3876 else if (subcode == MULT_EXPR && integer_onep (arg1))
3877 result = arg0;
3878 else if (subcode == MULT_EXPR && integer_onep (arg0))
3879 result = arg1;
3880 else if (TREE_CODE (arg0) == INTEGER_CST
3881 && TREE_CODE (arg1) == INTEGER_CST)
3883 if (cplx_result)
3884 result = int_const_binop (subcode, fold_convert (type, arg0),
3885 fold_convert (type, arg1));
3886 else
3887 result = int_const_binop (subcode, arg0, arg1);
3888 if (result && arith_overflowed_p (subcode, type, arg0, arg1))
3890 if (cplx_result)
3891 overflow = build_one_cst (type);
3892 else
3893 result = NULL_TREE;
3896 if (result)
3898 if (result == integer_zero_node)
3899 result = build_zero_cst (type);
3900 else if (cplx_result && TREE_TYPE (result) != type)
3902 if (TREE_CODE (result) == INTEGER_CST)
3904 if (arith_overflowed_p (PLUS_EXPR, type, result,
3905 integer_zero_node))
3906 overflow = build_one_cst (type);
3908 else if ((!TYPE_UNSIGNED (TREE_TYPE (result))
3909 && TYPE_UNSIGNED (type))
3910 || (TYPE_PRECISION (type)
3911 < (TYPE_PRECISION (TREE_TYPE (result))
3912 + (TYPE_UNSIGNED (TREE_TYPE (result))
3913 && !TYPE_UNSIGNED (type)))))
3914 result = NULL_TREE;
3915 if (result)
3916 result = fold_convert (type, result);
3921 if (result)
3923 if (TREE_CODE (result) == INTEGER_CST && TREE_OVERFLOW (result))
3924 result = drop_tree_overflow (result);
3925 if (cplx_result)
3927 if (overflow == NULL_TREE)
3928 overflow = build_zero_cst (TREE_TYPE (result));
3929 tree ctype = build_complex_type (TREE_TYPE (result));
3930 if (TREE_CODE (result) == INTEGER_CST
3931 && TREE_CODE (overflow) == INTEGER_CST)
3932 result = build_complex (ctype, result, overflow);
3933 else
3934 result = build2_loc (gimple_location (stmt), COMPLEX_EXPR,
3935 ctype, result, overflow);
3937 if (!update_call_from_tree (gsi, result))
3938 gimplify_and_update_call_from_tree (gsi, result);
3939 changed = true;
3943 return changed;
3947 /* Return true whether NAME has a use on STMT. */
3949 static bool
3950 has_use_on_stmt (tree name, gimple *stmt)
3952 imm_use_iterator iter;
3953 use_operand_p use_p;
3954 FOR_EACH_IMM_USE_FAST (use_p, iter, name)
3955 if (USE_STMT (use_p) == stmt)
3956 return true;
3957 return false;
3960 /* Worker for fold_stmt_1 dispatch to pattern based folding with
3961 gimple_simplify.
3963 Replaces *GSI with the simplification result in RCODE and OPS
3964 and the associated statements in *SEQ. Does the replacement
3965 according to INPLACE and returns true if the operation succeeded. */
3967 static bool
3968 replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
3969 code_helper rcode, tree *ops,
3970 gimple_seq *seq, bool inplace)
3972 gimple *stmt = gsi_stmt (*gsi);
3974 /* Play safe and do not allow abnormals to be mentioned in
3975 newly created statements. See also maybe_push_res_to_seq.
3976 As an exception allow such uses if there was a use of the
3977 same SSA name on the old stmt. */
3978 if ((TREE_CODE (ops[0]) == SSA_NAME
3979 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[0])
3980 && !has_use_on_stmt (ops[0], stmt))
3981 || (ops[1]
3982 && TREE_CODE (ops[1]) == SSA_NAME
3983 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[1])
3984 && !has_use_on_stmt (ops[1], stmt))
3985 || (ops[2]
3986 && TREE_CODE (ops[2]) == SSA_NAME
3987 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[2])
3988 && !has_use_on_stmt (ops[2], stmt))
3989 || (COMPARISON_CLASS_P (ops[0])
3990 && ((TREE_CODE (TREE_OPERAND (ops[0], 0)) == SSA_NAME
3991 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops[0], 0))
3992 && !has_use_on_stmt (TREE_OPERAND (ops[0], 0), stmt))
3993 || (TREE_CODE (TREE_OPERAND (ops[0], 1)) == SSA_NAME
3994 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops[0], 1))
3995 && !has_use_on_stmt (TREE_OPERAND (ops[0], 1), stmt)))))
3996 return false;
3998 /* Don't insert new statements when INPLACE is true, even if we could
3999 reuse STMT for the final statement. */
4000 if (inplace && !gimple_seq_empty_p (*seq))
4001 return false;
4003 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
4005 gcc_assert (rcode.is_tree_code ());
4006 if (TREE_CODE_CLASS ((enum tree_code)rcode) == tcc_comparison
4007 /* GIMPLE_CONDs condition may not throw. */
4008 && (!flag_exceptions
4009 || !cfun->can_throw_non_call_exceptions
4010 || !operation_could_trap_p (rcode,
4011 FLOAT_TYPE_P (TREE_TYPE (ops[0])),
4012 false, NULL_TREE)))
4013 gimple_cond_set_condition (cond_stmt, rcode, ops[0], ops[1]);
4014 else if (rcode == SSA_NAME)
4015 gimple_cond_set_condition (cond_stmt, NE_EXPR, ops[0],
4016 build_zero_cst (TREE_TYPE (ops[0])));
4017 else if (rcode == INTEGER_CST)
4019 if (integer_zerop (ops[0]))
4020 gimple_cond_make_false (cond_stmt);
4021 else
4022 gimple_cond_make_true (cond_stmt);
4024 else if (!inplace)
4026 tree res = maybe_push_res_to_seq (rcode, boolean_type_node,
4027 ops, seq);
4028 if (!res)
4029 return false;
4030 gimple_cond_set_condition (cond_stmt, NE_EXPR, res,
4031 build_zero_cst (TREE_TYPE (res)));
4033 else
4034 return false;
4035 if (dump_file && (dump_flags & TDF_DETAILS))
4037 fprintf (dump_file, "gimple_simplified to ");
4038 if (!gimple_seq_empty_p (*seq))
4039 print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
4040 print_gimple_stmt (dump_file, gsi_stmt (*gsi),
4041 0, TDF_SLIM);
4043 gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
4044 return true;
4046 else if (is_gimple_assign (stmt)
4047 && rcode.is_tree_code ())
4049 if (!inplace
4050 || gimple_num_ops (stmt) > get_gimple_rhs_num_ops (rcode))
4052 maybe_build_generic_op (rcode,
4053 TREE_TYPE (gimple_assign_lhs (stmt)), ops);
4054 gimple_assign_set_rhs_with_ops (gsi, rcode, ops[0], ops[1], ops[2]);
4055 if (dump_file && (dump_flags & TDF_DETAILS))
4057 fprintf (dump_file, "gimple_simplified to ");
4058 if (!gimple_seq_empty_p (*seq))
4059 print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
4060 print_gimple_stmt (dump_file, gsi_stmt (*gsi),
4061 0, TDF_SLIM);
4063 gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
4064 return true;
4067 else if (rcode.is_fn_code ()
4068 && gimple_call_combined_fn (stmt) == rcode)
4070 unsigned i;
4071 for (i = 0; i < gimple_call_num_args (stmt); ++i)
4073 gcc_assert (ops[i] != NULL_TREE);
4074 gimple_call_set_arg (stmt, i, ops[i]);
4076 if (i < 3)
4077 gcc_assert (ops[i] == NULL_TREE);
4078 if (dump_file && (dump_flags & TDF_DETAILS))
4080 fprintf (dump_file, "gimple_simplified to ");
4081 if (!gimple_seq_empty_p (*seq))
4082 print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
4083 print_gimple_stmt (dump_file, gsi_stmt (*gsi), 0, TDF_SLIM);
4085 gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
4086 return true;
4088 else if (!inplace)
4090 if (gimple_has_lhs (stmt))
4092 tree lhs = gimple_get_lhs (stmt);
4093 if (!maybe_push_res_to_seq (rcode, TREE_TYPE (lhs),
4094 ops, seq, lhs))
4095 return false;
4096 if (dump_file && (dump_flags & TDF_DETAILS))
4098 fprintf (dump_file, "gimple_simplified to ");
4099 print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
4101 gsi_replace_with_seq_vops (gsi, *seq);
4102 return true;
4104 else
4105 gcc_unreachable ();
4108 return false;
4111 /* Canonicalize MEM_REFs invariant address operand after propagation. */
4113 static bool
4114 maybe_canonicalize_mem_ref_addr (tree *t)
4116 bool res = false;
4118 if (TREE_CODE (*t) == ADDR_EXPR)
4119 t = &TREE_OPERAND (*t, 0);
4121 /* The C and C++ frontends use an ARRAY_REF for indexing with their
4122 generic vector extension. The actual vector referenced is
4123 view-converted to an array type for this purpose. If the index
4124 is constant the canonical representation in the middle-end is a
4125 BIT_FIELD_REF so re-write the former to the latter here. */
4126 if (TREE_CODE (*t) == ARRAY_REF
4127 && TREE_CODE (TREE_OPERAND (*t, 0)) == VIEW_CONVERT_EXPR
4128 && TREE_CODE (TREE_OPERAND (*t, 1)) == INTEGER_CST
4129 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0))))
4131 tree vtype = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0));
4132 if (VECTOR_TYPE_P (vtype))
4134 tree low = array_ref_low_bound (*t);
4135 if (TREE_CODE (low) == INTEGER_CST)
4137 if (tree_int_cst_le (low, TREE_OPERAND (*t, 1)))
4139 widest_int idx = wi::sub (wi::to_widest (TREE_OPERAND (*t, 1)),
4140 wi::to_widest (low));
4141 idx = wi::mul (idx, wi::to_widest
4142 (TYPE_SIZE (TREE_TYPE (*t))));
4143 widest_int ext
4144 = wi::add (idx, wi::to_widest (TYPE_SIZE (TREE_TYPE (*t))));
4145 if (wi::les_p (ext, wi::to_widest (TYPE_SIZE (vtype))))
4147 *t = build3_loc (EXPR_LOCATION (*t), BIT_FIELD_REF,
4148 TREE_TYPE (*t),
4149 TREE_OPERAND (TREE_OPERAND (*t, 0), 0),
4150 TYPE_SIZE (TREE_TYPE (*t)),
4151 wide_int_to_tree (sizetype, idx));
4152 res = true;
4159 while (handled_component_p (*t))
4160 t = &TREE_OPERAND (*t, 0);
4162 /* Canonicalize MEM [&foo.bar, 0] which appears after propagating
4163 of invariant addresses into a SSA name MEM_REF address. */
4164 if (TREE_CODE (*t) == MEM_REF
4165 || TREE_CODE (*t) == TARGET_MEM_REF)
4167 tree addr = TREE_OPERAND (*t, 0);
4168 if (TREE_CODE (addr) == ADDR_EXPR
4169 && (TREE_CODE (TREE_OPERAND (addr, 0)) == MEM_REF
4170 || handled_component_p (TREE_OPERAND (addr, 0))))
4172 tree base;
4173 HOST_WIDE_INT coffset;
4174 base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
4175 &coffset);
4176 if (!base)
4177 gcc_unreachable ();
4179 TREE_OPERAND (*t, 0) = build_fold_addr_expr (base);
4180 TREE_OPERAND (*t, 1) = int_const_binop (PLUS_EXPR,
4181 TREE_OPERAND (*t, 1),
4182 size_int (coffset));
4183 res = true;
4185 gcc_checking_assert (TREE_CODE (TREE_OPERAND (*t, 0)) == DEBUG_EXPR_DECL
4186 || is_gimple_mem_ref_addr (TREE_OPERAND (*t, 0)));
4189 /* Canonicalize back MEM_REFs to plain reference trees if the object
4190 accessed is a decl that has the same access semantics as the MEM_REF. */
4191 if (TREE_CODE (*t) == MEM_REF
4192 && TREE_CODE (TREE_OPERAND (*t, 0)) == ADDR_EXPR
4193 && integer_zerop (TREE_OPERAND (*t, 1))
4194 && MR_DEPENDENCE_CLIQUE (*t) == 0)
4196 tree decl = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
4197 tree alias_type = TREE_TYPE (TREE_OPERAND (*t, 1));
4198 if (/* Same volatile qualification. */
4199 TREE_THIS_VOLATILE (*t) == TREE_THIS_VOLATILE (decl)
4200 /* Same TBAA behavior with -fstrict-aliasing. */
4201 && !TYPE_REF_CAN_ALIAS_ALL (alias_type)
4202 && (TYPE_MAIN_VARIANT (TREE_TYPE (decl))
4203 == TYPE_MAIN_VARIANT (TREE_TYPE (alias_type)))
4204 /* Same alignment. */
4205 && TYPE_ALIGN (TREE_TYPE (decl)) == TYPE_ALIGN (TREE_TYPE (*t))
4206 /* We have to look out here to not drop a required conversion
4207 from the rhs to the lhs if *t appears on the lhs or vice-versa
4208 if it appears on the rhs. Thus require strict type
4209 compatibility. */
4210 && types_compatible_p (TREE_TYPE (*t), TREE_TYPE (decl)))
4212 *t = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
4213 res = true;
4217 /* Canonicalize TARGET_MEM_REF in particular with respect to
4218 the indexes becoming constant. */
4219 else if (TREE_CODE (*t) == TARGET_MEM_REF)
4221 tree tem = maybe_fold_tmr (*t);
4222 if (tem)
4224 *t = tem;
4225 res = true;
4229 return res;
4232 /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument
4233 distinguishes both cases. */
4235 static bool
4236 fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace, tree (*valueize) (tree))
4238 bool changed = false;
4239 gimple *stmt = gsi_stmt (*gsi);
4240 bool nowarning = gimple_no_warning_p (stmt);
4241 unsigned i;
4242 fold_defer_overflow_warnings ();
4244 /* First do required canonicalization of [TARGET_]MEM_REF addresses
4245 after propagation.
4246 ??? This shouldn't be done in generic folding but in the
4247 propagation helpers which also know whether an address was
4248 propagated.
4249 Also canonicalize operand order. */
4250 switch (gimple_code (stmt))
4252 case GIMPLE_ASSIGN:
4253 if (gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
4255 tree *rhs = gimple_assign_rhs1_ptr (stmt);
4256 if ((REFERENCE_CLASS_P (*rhs)
4257 || TREE_CODE (*rhs) == ADDR_EXPR)
4258 && maybe_canonicalize_mem_ref_addr (rhs))
4259 changed = true;
4260 tree *lhs = gimple_assign_lhs_ptr (stmt);
4261 if (REFERENCE_CLASS_P (*lhs)
4262 && maybe_canonicalize_mem_ref_addr (lhs))
4263 changed = true;
4265 else
4267 /* Canonicalize operand order. */
4268 enum tree_code code = gimple_assign_rhs_code (stmt);
4269 if (TREE_CODE_CLASS (code) == tcc_comparison
4270 || commutative_tree_code (code)
4271 || commutative_ternary_tree_code (code))
4273 tree rhs1 = gimple_assign_rhs1 (stmt);
4274 tree rhs2 = gimple_assign_rhs2 (stmt);
4275 if (tree_swap_operands_p (rhs1, rhs2))
4277 gimple_assign_set_rhs1 (stmt, rhs2);
4278 gimple_assign_set_rhs2 (stmt, rhs1);
4279 if (TREE_CODE_CLASS (code) == tcc_comparison)
4280 gimple_assign_set_rhs_code (stmt,
4281 swap_tree_comparison (code));
4282 changed = true;
4286 break;
4287 case GIMPLE_CALL:
4289 for (i = 0; i < gimple_call_num_args (stmt); ++i)
4291 tree *arg = gimple_call_arg_ptr (stmt, i);
4292 if (REFERENCE_CLASS_P (*arg)
4293 && maybe_canonicalize_mem_ref_addr (arg))
4294 changed = true;
4296 tree *lhs = gimple_call_lhs_ptr (stmt);
4297 if (*lhs
4298 && REFERENCE_CLASS_P (*lhs)
4299 && maybe_canonicalize_mem_ref_addr (lhs))
4300 changed = true;
4301 break;
4303 case GIMPLE_ASM:
4305 gasm *asm_stmt = as_a <gasm *> (stmt);
4306 for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
4308 tree link = gimple_asm_output_op (asm_stmt, i);
4309 tree op = TREE_VALUE (link);
4310 if (REFERENCE_CLASS_P (op)
4311 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
4312 changed = true;
4314 for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
4316 tree link = gimple_asm_input_op (asm_stmt, i);
4317 tree op = TREE_VALUE (link);
4318 if ((REFERENCE_CLASS_P (op)
4319 || TREE_CODE (op) == ADDR_EXPR)
4320 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
4321 changed = true;
4324 break;
4325 case GIMPLE_DEBUG:
4326 if (gimple_debug_bind_p (stmt))
4328 tree *val = gimple_debug_bind_get_value_ptr (stmt);
4329 if (*val
4330 && (REFERENCE_CLASS_P (*val)
4331 || TREE_CODE (*val) == ADDR_EXPR)
4332 && maybe_canonicalize_mem_ref_addr (val))
4333 changed = true;
4335 break;
4336 case GIMPLE_COND:
4338 /* Canonicalize operand order. */
4339 tree lhs = gimple_cond_lhs (stmt);
4340 tree rhs = gimple_cond_rhs (stmt);
4341 if (tree_swap_operands_p (lhs, rhs))
4343 gcond *gc = as_a <gcond *> (stmt);
4344 gimple_cond_set_lhs (gc, rhs);
4345 gimple_cond_set_rhs (gc, lhs);
4346 gimple_cond_set_code (gc,
4347 swap_tree_comparison (gimple_cond_code (gc)));
4348 changed = true;
4351 default:;
4354 /* Dispatch to pattern-based folding. */
4355 if (!inplace
4356 || is_gimple_assign (stmt)
4357 || gimple_code (stmt) == GIMPLE_COND)
4359 gimple_seq seq = NULL;
4360 code_helper rcode;
4361 tree ops[3] = {};
4362 if (gimple_simplify (stmt, &rcode, ops, inplace ? NULL : &seq,
4363 valueize, valueize))
4365 if (replace_stmt_with_simplification (gsi, rcode, ops, &seq, inplace))
4366 changed = true;
4367 else
4368 gimple_seq_discard (seq);
4372 stmt = gsi_stmt (*gsi);
4374 /* Fold the main computation performed by the statement. */
4375 switch (gimple_code (stmt))
4377 case GIMPLE_ASSIGN:
4379 /* Try to canonicalize for boolean-typed X the comparisons
4380 X == 0, X == 1, X != 0, and X != 1. */
4381 if (gimple_assign_rhs_code (stmt) == EQ_EXPR
4382 || gimple_assign_rhs_code (stmt) == NE_EXPR)
4384 tree lhs = gimple_assign_lhs (stmt);
4385 tree op1 = gimple_assign_rhs1 (stmt);
4386 tree op2 = gimple_assign_rhs2 (stmt);
4387 tree type = TREE_TYPE (op1);
4389 /* Check whether the comparison operands are of the same boolean
4390 type as the result type is.
4391 Check that second operand is an integer-constant with value
4392 one or zero. */
4393 if (TREE_CODE (op2) == INTEGER_CST
4394 && (integer_zerop (op2) || integer_onep (op2))
4395 && useless_type_conversion_p (TREE_TYPE (lhs), type))
4397 enum tree_code cmp_code = gimple_assign_rhs_code (stmt);
4398 bool is_logical_not = false;
4400 /* X == 0 and X != 1 is a logical-not.of X
4401 X == 1 and X != 0 is X */
4402 if ((cmp_code == EQ_EXPR && integer_zerop (op2))
4403 || (cmp_code == NE_EXPR && integer_onep (op2)))
4404 is_logical_not = true;
4406 if (is_logical_not == false)
4407 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op1), op1);
4408 /* Only for one-bit precision typed X the transformation
4409 !X -> ~X is valied. */
4410 else if (TYPE_PRECISION (type) == 1)
4411 gimple_assign_set_rhs_with_ops (gsi, BIT_NOT_EXPR, op1);
4412 /* Otherwise we use !X -> X ^ 1. */
4413 else
4414 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op1,
4415 build_int_cst (type, 1));
4416 changed = true;
4417 break;
4421 unsigned old_num_ops = gimple_num_ops (stmt);
4422 tree lhs = gimple_assign_lhs (stmt);
4423 tree new_rhs = fold_gimple_assign (gsi);
4424 if (new_rhs
4425 && !useless_type_conversion_p (TREE_TYPE (lhs),
4426 TREE_TYPE (new_rhs)))
4427 new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
4428 if (new_rhs
4429 && (!inplace
4430 || get_gimple_rhs_num_ops (TREE_CODE (new_rhs)) < old_num_ops))
4432 gimple_assign_set_rhs_from_tree (gsi, new_rhs);
4433 changed = true;
4435 break;
4438 case GIMPLE_CALL:
4439 changed |= gimple_fold_call (gsi, inplace);
4440 break;
4442 case GIMPLE_ASM:
4443 /* Fold *& in asm operands. */
4445 gasm *asm_stmt = as_a <gasm *> (stmt);
4446 size_t noutputs;
4447 const char **oconstraints;
4448 const char *constraint;
4449 bool allows_mem, allows_reg;
4451 noutputs = gimple_asm_noutputs (asm_stmt);
4452 oconstraints = XALLOCAVEC (const char *, noutputs);
4454 for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
4456 tree link = gimple_asm_output_op (asm_stmt, i);
4457 tree op = TREE_VALUE (link);
4458 oconstraints[i]
4459 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4460 if (REFERENCE_CLASS_P (op)
4461 && (op = maybe_fold_reference (op, true)) != NULL_TREE)
4463 TREE_VALUE (link) = op;
4464 changed = true;
4467 for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
4469 tree link = gimple_asm_input_op (asm_stmt, i);
4470 tree op = TREE_VALUE (link);
4471 constraint
4472 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4473 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
4474 oconstraints, &allows_mem, &allows_reg);
4475 if (REFERENCE_CLASS_P (op)
4476 && (op = maybe_fold_reference (op, !allows_reg && allows_mem))
4477 != NULL_TREE)
4479 TREE_VALUE (link) = op;
4480 changed = true;
4484 break;
4486 case GIMPLE_DEBUG:
4487 if (gimple_debug_bind_p (stmt))
4489 tree val = gimple_debug_bind_get_value (stmt);
4490 if (val
4491 && REFERENCE_CLASS_P (val))
4493 tree tem = maybe_fold_reference (val, false);
4494 if (tem)
4496 gimple_debug_bind_set_value (stmt, tem);
4497 changed = true;
4500 else if (val
4501 && TREE_CODE (val) == ADDR_EXPR)
4503 tree ref = TREE_OPERAND (val, 0);
4504 tree tem = maybe_fold_reference (ref, false);
4505 if (tem)
4507 tem = build_fold_addr_expr_with_type (tem, TREE_TYPE (val));
4508 gimple_debug_bind_set_value (stmt, tem);
4509 changed = true;
4513 break;
4515 case GIMPLE_RETURN:
4517 greturn *ret_stmt = as_a<greturn *> (stmt);
4518 tree ret = gimple_return_retval(ret_stmt);
4520 if (ret && TREE_CODE (ret) == SSA_NAME && valueize)
4522 tree val = valueize (ret);
4523 if (val && val != ret
4524 && may_propagate_copy (ret, val))
4526 gimple_return_set_retval (ret_stmt, val);
4527 changed = true;
4531 break;
4533 default:;
4536 stmt = gsi_stmt (*gsi);
4538 /* Fold *& on the lhs. */
4539 if (gimple_has_lhs (stmt))
4541 tree lhs = gimple_get_lhs (stmt);
4542 if (lhs && REFERENCE_CLASS_P (lhs))
4544 tree new_lhs = maybe_fold_reference (lhs, true);
4545 if (new_lhs)
4547 gimple_set_lhs (stmt, new_lhs);
4548 changed = true;
4553 fold_undefer_overflow_warnings (changed && !nowarning, stmt, 0);
4554 return changed;
4557 /* Valueziation callback that ends up not following SSA edges. */
4559 tree
4560 no_follow_ssa_edges (tree)
4562 return NULL_TREE;
4565 /* Valueization callback that ends up following single-use SSA edges only. */
4567 tree
4568 follow_single_use_edges (tree val)
4570 if (TREE_CODE (val) == SSA_NAME
4571 && !has_single_use (val))
4572 return NULL_TREE;
4573 return val;
4576 /* Fold the statement pointed to by GSI. In some cases, this function may
4577 replace the whole statement with a new one. Returns true iff folding
4578 makes any changes.
4579 The statement pointed to by GSI should be in valid gimple form but may
4580 be in unfolded state as resulting from for example constant propagation
4581 which can produce *&x = 0. */
4583 bool
4584 fold_stmt (gimple_stmt_iterator *gsi)
4586 return fold_stmt_1 (gsi, false, no_follow_ssa_edges);
4589 bool
4590 fold_stmt (gimple_stmt_iterator *gsi, tree (*valueize) (tree))
4592 return fold_stmt_1 (gsi, false, valueize);
4595 /* Perform the minimal folding on statement *GSI. Only operations like
4596 *&x created by constant propagation are handled. The statement cannot
4597 be replaced with a new one. Return true if the statement was
4598 changed, false otherwise.
4599 The statement *GSI should be in valid gimple form but may
4600 be in unfolded state as resulting from for example constant propagation
4601 which can produce *&x = 0. */
4603 bool
4604 fold_stmt_inplace (gimple_stmt_iterator *gsi)
4606 gimple *stmt = gsi_stmt (*gsi);
4607 bool changed = fold_stmt_1 (gsi, true, no_follow_ssa_edges);
4608 gcc_assert (gsi_stmt (*gsi) == stmt);
4609 return changed;
4612 /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
4613 if EXPR is null or we don't know how.
4614 If non-null, the result always has boolean type. */
4616 static tree
4617 canonicalize_bool (tree expr, bool invert)
4619 if (!expr)
4620 return NULL_TREE;
4621 else if (invert)
4623 if (integer_nonzerop (expr))
4624 return boolean_false_node;
4625 else if (integer_zerop (expr))
4626 return boolean_true_node;
4627 else if (TREE_CODE (expr) == SSA_NAME)
4628 return fold_build2 (EQ_EXPR, boolean_type_node, expr,
4629 build_int_cst (TREE_TYPE (expr), 0));
4630 else if (COMPARISON_CLASS_P (expr))
4631 return fold_build2 (invert_tree_comparison (TREE_CODE (expr), false),
4632 boolean_type_node,
4633 TREE_OPERAND (expr, 0),
4634 TREE_OPERAND (expr, 1));
4635 else
4636 return NULL_TREE;
4638 else
4640 if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
4641 return expr;
4642 if (integer_nonzerop (expr))
4643 return boolean_true_node;
4644 else if (integer_zerop (expr))
4645 return boolean_false_node;
4646 else if (TREE_CODE (expr) == SSA_NAME)
4647 return fold_build2 (NE_EXPR, boolean_type_node, expr,
4648 build_int_cst (TREE_TYPE (expr), 0));
4649 else if (COMPARISON_CLASS_P (expr))
4650 return fold_build2 (TREE_CODE (expr),
4651 boolean_type_node,
4652 TREE_OPERAND (expr, 0),
4653 TREE_OPERAND (expr, 1));
4654 else
4655 return NULL_TREE;
4659 /* Check to see if a boolean expression EXPR is logically equivalent to the
4660 comparison (OP1 CODE OP2). Check for various identities involving
4661 SSA_NAMEs. */
4663 static bool
4664 same_bool_comparison_p (const_tree expr, enum tree_code code,
4665 const_tree op1, const_tree op2)
4667 gimple *s;
4669 /* The obvious case. */
4670 if (TREE_CODE (expr) == code
4671 && operand_equal_p (TREE_OPERAND (expr, 0), op1, 0)
4672 && operand_equal_p (TREE_OPERAND (expr, 1), op2, 0))
4673 return true;
4675 /* Check for comparing (name, name != 0) and the case where expr
4676 is an SSA_NAME with a definition matching the comparison. */
4677 if (TREE_CODE (expr) == SSA_NAME
4678 && TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
4680 if (operand_equal_p (expr, op1, 0))
4681 return ((code == NE_EXPR && integer_zerop (op2))
4682 || (code == EQ_EXPR && integer_nonzerop (op2)));
4683 s = SSA_NAME_DEF_STMT (expr);
4684 if (is_gimple_assign (s)
4685 && gimple_assign_rhs_code (s) == code
4686 && operand_equal_p (gimple_assign_rhs1 (s), op1, 0)
4687 && operand_equal_p (gimple_assign_rhs2 (s), op2, 0))
4688 return true;
4691 /* If op1 is of the form (name != 0) or (name == 0), and the definition
4692 of name is a comparison, recurse. */
4693 if (TREE_CODE (op1) == SSA_NAME
4694 && TREE_CODE (TREE_TYPE (op1)) == BOOLEAN_TYPE)
4696 s = SSA_NAME_DEF_STMT (op1);
4697 if (is_gimple_assign (s)
4698 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
4700 enum tree_code c = gimple_assign_rhs_code (s);
4701 if ((c == NE_EXPR && integer_zerop (op2))
4702 || (c == EQ_EXPR && integer_nonzerop (op2)))
4703 return same_bool_comparison_p (expr, c,
4704 gimple_assign_rhs1 (s),
4705 gimple_assign_rhs2 (s));
4706 if ((c == EQ_EXPR && integer_zerop (op2))
4707 || (c == NE_EXPR && integer_nonzerop (op2)))
4708 return same_bool_comparison_p (expr,
4709 invert_tree_comparison (c, false),
4710 gimple_assign_rhs1 (s),
4711 gimple_assign_rhs2 (s));
4714 return false;
4717 /* Check to see if two boolean expressions OP1 and OP2 are logically
4718 equivalent. */
4720 static bool
4721 same_bool_result_p (const_tree op1, const_tree op2)
4723 /* Simple cases first. */
4724 if (operand_equal_p (op1, op2, 0))
4725 return true;
4727 /* Check the cases where at least one of the operands is a comparison.
4728 These are a bit smarter than operand_equal_p in that they apply some
4729 identifies on SSA_NAMEs. */
4730 if (COMPARISON_CLASS_P (op2)
4731 && same_bool_comparison_p (op1, TREE_CODE (op2),
4732 TREE_OPERAND (op2, 0),
4733 TREE_OPERAND (op2, 1)))
4734 return true;
4735 if (COMPARISON_CLASS_P (op1)
4736 && same_bool_comparison_p (op2, TREE_CODE (op1),
4737 TREE_OPERAND (op1, 0),
4738 TREE_OPERAND (op1, 1)))
4739 return true;
4741 /* Default case. */
4742 return false;
4745 /* Forward declarations for some mutually recursive functions. */
4747 static tree
4748 and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
4749 enum tree_code code2, tree op2a, tree op2b);
4750 static tree
4751 and_var_with_comparison (tree var, bool invert,
4752 enum tree_code code2, tree op2a, tree op2b);
4753 static tree
4754 and_var_with_comparison_1 (gimple *stmt,
4755 enum tree_code code2, tree op2a, tree op2b);
4756 static tree
4757 or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
4758 enum tree_code code2, tree op2a, tree op2b);
4759 static tree
4760 or_var_with_comparison (tree var, bool invert,
4761 enum tree_code code2, tree op2a, tree op2b);
4762 static tree
4763 or_var_with_comparison_1 (gimple *stmt,
4764 enum tree_code code2, tree op2a, tree op2b);
4766 /* Helper function for and_comparisons_1: try to simplify the AND of the
4767 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
4768 If INVERT is true, invert the value of the VAR before doing the AND.
4769 Return NULL_EXPR if we can't simplify this to a single expression. */
4771 static tree
4772 and_var_with_comparison (tree var, bool invert,
4773 enum tree_code code2, tree op2a, tree op2b)
4775 tree t;
4776 gimple *stmt = SSA_NAME_DEF_STMT (var);
4778 /* We can only deal with variables whose definitions are assignments. */
4779 if (!is_gimple_assign (stmt))
4780 return NULL_TREE;
4782 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
4783 !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
4784 Then we only have to consider the simpler non-inverted cases. */
4785 if (invert)
4786 t = or_var_with_comparison_1 (stmt,
4787 invert_tree_comparison (code2, false),
4788 op2a, op2b);
4789 else
4790 t = and_var_with_comparison_1 (stmt, code2, op2a, op2b);
4791 return canonicalize_bool (t, invert);
4794 /* Try to simplify the AND of the ssa variable defined by the assignment
4795 STMT with the comparison specified by (OP2A CODE2 OP2B).
4796 Return NULL_EXPR if we can't simplify this to a single expression. */
4798 static tree
4799 and_var_with_comparison_1 (gimple *stmt,
4800 enum tree_code code2, tree op2a, tree op2b)
4802 tree var = gimple_assign_lhs (stmt);
4803 tree true_test_var = NULL_TREE;
4804 tree false_test_var = NULL_TREE;
4805 enum tree_code innercode = gimple_assign_rhs_code (stmt);
4807 /* Check for identities like (var AND (var == 0)) => false. */
4808 if (TREE_CODE (op2a) == SSA_NAME
4809 && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
4811 if ((code2 == NE_EXPR && integer_zerop (op2b))
4812 || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
4814 true_test_var = op2a;
4815 if (var == true_test_var)
4816 return var;
4818 else if ((code2 == EQ_EXPR && integer_zerop (op2b))
4819 || (code2 == NE_EXPR && integer_nonzerop (op2b)))
4821 false_test_var = op2a;
4822 if (var == false_test_var)
4823 return boolean_false_node;
4827 /* If the definition is a comparison, recurse on it. */
4828 if (TREE_CODE_CLASS (innercode) == tcc_comparison)
4830 tree t = and_comparisons_1 (innercode,
4831 gimple_assign_rhs1 (stmt),
4832 gimple_assign_rhs2 (stmt),
4833 code2,
4834 op2a,
4835 op2b);
4836 if (t)
4837 return t;
4840 /* If the definition is an AND or OR expression, we may be able to
4841 simplify by reassociating. */
4842 if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
4843 && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
4845 tree inner1 = gimple_assign_rhs1 (stmt);
4846 tree inner2 = gimple_assign_rhs2 (stmt);
4847 gimple *s;
4848 tree t;
4849 tree partial = NULL_TREE;
4850 bool is_and = (innercode == BIT_AND_EXPR);
4852 /* Check for boolean identities that don't require recursive examination
4853 of inner1/inner2:
4854 inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
4855 inner1 AND (inner1 OR inner2) => inner1
4856 !inner1 AND (inner1 AND inner2) => false
4857 !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
4858 Likewise for similar cases involving inner2. */
4859 if (inner1 == true_test_var)
4860 return (is_and ? var : inner1);
4861 else if (inner2 == true_test_var)
4862 return (is_and ? var : inner2);
4863 else if (inner1 == false_test_var)
4864 return (is_and
4865 ? boolean_false_node
4866 : and_var_with_comparison (inner2, false, code2, op2a, op2b));
4867 else if (inner2 == false_test_var)
4868 return (is_and
4869 ? boolean_false_node
4870 : and_var_with_comparison (inner1, false, code2, op2a, op2b));
4872 /* Next, redistribute/reassociate the AND across the inner tests.
4873 Compute the first partial result, (inner1 AND (op2a code op2b)) */
4874 if (TREE_CODE (inner1) == SSA_NAME
4875 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
4876 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
4877 && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s),
4878 gimple_assign_rhs1 (s),
4879 gimple_assign_rhs2 (s),
4880 code2, op2a, op2b)))
4882 /* Handle the AND case, where we are reassociating:
4883 (inner1 AND inner2) AND (op2a code2 op2b)
4884 => (t AND inner2)
4885 If the partial result t is a constant, we win. Otherwise
4886 continue on to try reassociating with the other inner test. */
4887 if (is_and)
4889 if (integer_onep (t))
4890 return inner2;
4891 else if (integer_zerop (t))
4892 return boolean_false_node;
4895 /* Handle the OR case, where we are redistributing:
4896 (inner1 OR inner2) AND (op2a code2 op2b)
4897 => (t OR (inner2 AND (op2a code2 op2b))) */
4898 else if (integer_onep (t))
4899 return boolean_true_node;
4901 /* Save partial result for later. */
4902 partial = t;
4905 /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
4906 if (TREE_CODE (inner2) == SSA_NAME
4907 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
4908 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
4909 && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s),
4910 gimple_assign_rhs1 (s),
4911 gimple_assign_rhs2 (s),
4912 code2, op2a, op2b)))
4914 /* Handle the AND case, where we are reassociating:
4915 (inner1 AND inner2) AND (op2a code2 op2b)
4916 => (inner1 AND t) */
4917 if (is_and)
4919 if (integer_onep (t))
4920 return inner1;
4921 else if (integer_zerop (t))
4922 return boolean_false_node;
4923 /* If both are the same, we can apply the identity
4924 (x AND x) == x. */
4925 else if (partial && same_bool_result_p (t, partial))
4926 return t;
4929 /* Handle the OR case. where we are redistributing:
4930 (inner1 OR inner2) AND (op2a code2 op2b)
4931 => (t OR (inner1 AND (op2a code2 op2b)))
4932 => (t OR partial) */
4933 else
4935 if (integer_onep (t))
4936 return boolean_true_node;
4937 else if (partial)
4939 /* We already got a simplification for the other
4940 operand to the redistributed OR expression. The
4941 interesting case is when at least one is false.
4942 Or, if both are the same, we can apply the identity
4943 (x OR x) == x. */
4944 if (integer_zerop (partial))
4945 return t;
4946 else if (integer_zerop (t))
4947 return partial;
4948 else if (same_bool_result_p (t, partial))
4949 return t;
4954 return NULL_TREE;
4957 /* Try to simplify the AND of two comparisons defined by
4958 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
4959 If this can be done without constructing an intermediate value,
4960 return the resulting tree; otherwise NULL_TREE is returned.
4961 This function is deliberately asymmetric as it recurses on SSA_DEFs
4962 in the first comparison but not the second. */
4964 static tree
4965 and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
4966 enum tree_code code2, tree op2a, tree op2b)
4968 tree truth_type = truth_type_for (TREE_TYPE (op1a));
4970 /* First check for ((x CODE1 y) AND (x CODE2 y)). */
4971 if (operand_equal_p (op1a, op2a, 0)
4972 && operand_equal_p (op1b, op2b, 0))
4974 /* Result will be either NULL_TREE, or a combined comparison. */
4975 tree t = combine_comparisons (UNKNOWN_LOCATION,
4976 TRUTH_ANDIF_EXPR, code1, code2,
4977 truth_type, op1a, op1b);
4978 if (t)
4979 return t;
4982 /* Likewise the swapped case of the above. */
4983 if (operand_equal_p (op1a, op2b, 0)
4984 && operand_equal_p (op1b, op2a, 0))
4986 /* Result will be either NULL_TREE, or a combined comparison. */
4987 tree t = combine_comparisons (UNKNOWN_LOCATION,
4988 TRUTH_ANDIF_EXPR, code1,
4989 swap_tree_comparison (code2),
4990 truth_type, op1a, op1b);
4991 if (t)
4992 return t;
4995 /* If both comparisons are of the same value against constants, we might
4996 be able to merge them. */
4997 if (operand_equal_p (op1a, op2a, 0)
4998 && TREE_CODE (op1b) == INTEGER_CST
4999 && TREE_CODE (op2b) == INTEGER_CST)
5001 int cmp = tree_int_cst_compare (op1b, op2b);
5003 /* If we have (op1a == op1b), we should either be able to
5004 return that or FALSE, depending on whether the constant op1b
5005 also satisfies the other comparison against op2b. */
5006 if (code1 == EQ_EXPR)
5008 bool done = true;
5009 bool val;
5010 switch (code2)
5012 case EQ_EXPR: val = (cmp == 0); break;
5013 case NE_EXPR: val = (cmp != 0); break;
5014 case LT_EXPR: val = (cmp < 0); break;
5015 case GT_EXPR: val = (cmp > 0); break;
5016 case LE_EXPR: val = (cmp <= 0); break;
5017 case GE_EXPR: val = (cmp >= 0); break;
5018 default: done = false;
5020 if (done)
5022 if (val)
5023 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5024 else
5025 return boolean_false_node;
5028 /* Likewise if the second comparison is an == comparison. */
5029 else if (code2 == EQ_EXPR)
5031 bool done = true;
5032 bool val;
5033 switch (code1)
5035 case EQ_EXPR: val = (cmp == 0); break;
5036 case NE_EXPR: val = (cmp != 0); break;
5037 case LT_EXPR: val = (cmp > 0); break;
5038 case GT_EXPR: val = (cmp < 0); break;
5039 case LE_EXPR: val = (cmp >= 0); break;
5040 case GE_EXPR: val = (cmp <= 0); break;
5041 default: done = false;
5043 if (done)
5045 if (val)
5046 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5047 else
5048 return boolean_false_node;
5052 /* Same business with inequality tests. */
5053 else if (code1 == NE_EXPR)
5055 bool val;
5056 switch (code2)
5058 case EQ_EXPR: val = (cmp != 0); break;
5059 case NE_EXPR: val = (cmp == 0); break;
5060 case LT_EXPR: val = (cmp >= 0); break;
5061 case GT_EXPR: val = (cmp <= 0); break;
5062 case LE_EXPR: val = (cmp > 0); break;
5063 case GE_EXPR: val = (cmp < 0); break;
5064 default:
5065 val = false;
5067 if (val)
5068 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5070 else if (code2 == NE_EXPR)
5072 bool val;
5073 switch (code1)
5075 case EQ_EXPR: val = (cmp == 0); break;
5076 case NE_EXPR: val = (cmp != 0); break;
5077 case LT_EXPR: val = (cmp <= 0); break;
5078 case GT_EXPR: val = (cmp >= 0); break;
5079 case LE_EXPR: val = (cmp < 0); break;
5080 case GE_EXPR: val = (cmp > 0); break;
5081 default:
5082 val = false;
5084 if (val)
5085 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5088 /* Chose the more restrictive of two < or <= comparisons. */
5089 else if ((code1 == LT_EXPR || code1 == LE_EXPR)
5090 && (code2 == LT_EXPR || code2 == LE_EXPR))
5092 if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
5093 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5094 else
5095 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5098 /* Likewise chose the more restrictive of two > or >= comparisons. */
5099 else if ((code1 == GT_EXPR || code1 == GE_EXPR)
5100 && (code2 == GT_EXPR || code2 == GE_EXPR))
5102 if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
5103 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5104 else
5105 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5108 /* Check for singleton ranges. */
5109 else if (cmp == 0
5110 && ((code1 == LE_EXPR && code2 == GE_EXPR)
5111 || (code1 == GE_EXPR && code2 == LE_EXPR)))
5112 return fold_build2 (EQ_EXPR, boolean_type_node, op1a, op2b);
5114 /* Check for disjoint ranges. */
5115 else if (cmp <= 0
5116 && (code1 == LT_EXPR || code1 == LE_EXPR)
5117 && (code2 == GT_EXPR || code2 == GE_EXPR))
5118 return boolean_false_node;
5119 else if (cmp >= 0
5120 && (code1 == GT_EXPR || code1 == GE_EXPR)
5121 && (code2 == LT_EXPR || code2 == LE_EXPR))
5122 return boolean_false_node;
5125 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
5126 NAME's definition is a truth value. See if there are any simplifications
5127 that can be done against the NAME's definition. */
5128 if (TREE_CODE (op1a) == SSA_NAME
5129 && (code1 == NE_EXPR || code1 == EQ_EXPR)
5130 && (integer_zerop (op1b) || integer_onep (op1b)))
5132 bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
5133 || (code1 == NE_EXPR && integer_onep (op1b)));
5134 gimple *stmt = SSA_NAME_DEF_STMT (op1a);
5135 switch (gimple_code (stmt))
5137 case GIMPLE_ASSIGN:
5138 /* Try to simplify by copy-propagating the definition. */
5139 return and_var_with_comparison (op1a, invert, code2, op2a, op2b);
5141 case GIMPLE_PHI:
5142 /* If every argument to the PHI produces the same result when
5143 ANDed with the second comparison, we win.
5144 Do not do this unless the type is bool since we need a bool
5145 result here anyway. */
5146 if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
5148 tree result = NULL_TREE;
5149 unsigned i;
5150 for (i = 0; i < gimple_phi_num_args (stmt); i++)
5152 tree arg = gimple_phi_arg_def (stmt, i);
5154 /* If this PHI has itself as an argument, ignore it.
5155 If all the other args produce the same result,
5156 we're still OK. */
5157 if (arg == gimple_phi_result (stmt))
5158 continue;
5159 else if (TREE_CODE (arg) == INTEGER_CST)
5161 if (invert ? integer_nonzerop (arg) : integer_zerop (arg))
5163 if (!result)
5164 result = boolean_false_node;
5165 else if (!integer_zerop (result))
5166 return NULL_TREE;
5168 else if (!result)
5169 result = fold_build2 (code2, boolean_type_node,
5170 op2a, op2b);
5171 else if (!same_bool_comparison_p (result,
5172 code2, op2a, op2b))
5173 return NULL_TREE;
5175 else if (TREE_CODE (arg) == SSA_NAME
5176 && !SSA_NAME_IS_DEFAULT_DEF (arg))
5178 tree temp;
5179 gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
5180 /* In simple cases we can look through PHI nodes,
5181 but we have to be careful with loops.
5182 See PR49073. */
5183 if (! dom_info_available_p (CDI_DOMINATORS)
5184 || gimple_bb (def_stmt) == gimple_bb (stmt)
5185 || dominated_by_p (CDI_DOMINATORS,
5186 gimple_bb (def_stmt),
5187 gimple_bb (stmt)))
5188 return NULL_TREE;
5189 temp = and_var_with_comparison (arg, invert, code2,
5190 op2a, op2b);
5191 if (!temp)
5192 return NULL_TREE;
5193 else if (!result)
5194 result = temp;
5195 else if (!same_bool_result_p (result, temp))
5196 return NULL_TREE;
5198 else
5199 return NULL_TREE;
5201 return result;
5204 default:
5205 break;
5208 return NULL_TREE;
5211 /* Try to simplify the AND of two comparisons, specified by
5212 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
5213 If this can be simplified to a single expression (without requiring
5214 introducing more SSA variables to hold intermediate values),
5215 return the resulting tree. Otherwise return NULL_TREE.
5216 If the result expression is non-null, it has boolean type. */
5218 tree
5219 maybe_fold_and_comparisons (enum tree_code code1, tree op1a, tree op1b,
5220 enum tree_code code2, tree op2a, tree op2b)
5222 tree t = and_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b);
5223 if (t)
5224 return t;
5225 else
5226 return and_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b);
5229 /* Helper function for or_comparisons_1: try to simplify the OR of the
5230 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
5231 If INVERT is true, invert the value of VAR before doing the OR.
5232 Return NULL_EXPR if we can't simplify this to a single expression. */
5234 static tree
5235 or_var_with_comparison (tree var, bool invert,
5236 enum tree_code code2, tree op2a, tree op2b)
5238 tree t;
5239 gimple *stmt = SSA_NAME_DEF_STMT (var);
5241 /* We can only deal with variables whose definitions are assignments. */
5242 if (!is_gimple_assign (stmt))
5243 return NULL_TREE;
5245 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
5246 !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
5247 Then we only have to consider the simpler non-inverted cases. */
5248 if (invert)
5249 t = and_var_with_comparison_1 (stmt,
5250 invert_tree_comparison (code2, false),
5251 op2a, op2b);
5252 else
5253 t = or_var_with_comparison_1 (stmt, code2, op2a, op2b);
5254 return canonicalize_bool (t, invert);
5257 /* Try to simplify the OR of the ssa variable defined by the assignment
5258 STMT with the comparison specified by (OP2A CODE2 OP2B).
5259 Return NULL_EXPR if we can't simplify this to a single expression. */
5261 static tree
5262 or_var_with_comparison_1 (gimple *stmt,
5263 enum tree_code code2, tree op2a, tree op2b)
5265 tree var = gimple_assign_lhs (stmt);
5266 tree true_test_var = NULL_TREE;
5267 tree false_test_var = NULL_TREE;
5268 enum tree_code innercode = gimple_assign_rhs_code (stmt);
5270 /* Check for identities like (var OR (var != 0)) => true . */
5271 if (TREE_CODE (op2a) == SSA_NAME
5272 && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
5274 if ((code2 == NE_EXPR && integer_zerop (op2b))
5275 || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
5277 true_test_var = op2a;
5278 if (var == true_test_var)
5279 return var;
5281 else if ((code2 == EQ_EXPR && integer_zerop (op2b))
5282 || (code2 == NE_EXPR && integer_nonzerop (op2b)))
5284 false_test_var = op2a;
5285 if (var == false_test_var)
5286 return boolean_true_node;
5290 /* If the definition is a comparison, recurse on it. */
5291 if (TREE_CODE_CLASS (innercode) == tcc_comparison)
5293 tree t = or_comparisons_1 (innercode,
5294 gimple_assign_rhs1 (stmt),
5295 gimple_assign_rhs2 (stmt),
5296 code2,
5297 op2a,
5298 op2b);
5299 if (t)
5300 return t;
5303 /* If the definition is an AND or OR expression, we may be able to
5304 simplify by reassociating. */
5305 if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
5306 && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
5308 tree inner1 = gimple_assign_rhs1 (stmt);
5309 tree inner2 = gimple_assign_rhs2 (stmt);
5310 gimple *s;
5311 tree t;
5312 tree partial = NULL_TREE;
5313 bool is_or = (innercode == BIT_IOR_EXPR);
5315 /* Check for boolean identities that don't require recursive examination
5316 of inner1/inner2:
5317 inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
5318 inner1 OR (inner1 AND inner2) => inner1
5319 !inner1 OR (inner1 OR inner2) => true
5320 !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
5322 if (inner1 == true_test_var)
5323 return (is_or ? var : inner1);
5324 else if (inner2 == true_test_var)
5325 return (is_or ? var : inner2);
5326 else if (inner1 == false_test_var)
5327 return (is_or
5328 ? boolean_true_node
5329 : or_var_with_comparison (inner2, false, code2, op2a, op2b));
5330 else if (inner2 == false_test_var)
5331 return (is_or
5332 ? boolean_true_node
5333 : or_var_with_comparison (inner1, false, code2, op2a, op2b));
5335 /* Next, redistribute/reassociate the OR across the inner tests.
5336 Compute the first partial result, (inner1 OR (op2a code op2b)) */
5337 if (TREE_CODE (inner1) == SSA_NAME
5338 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
5339 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
5340 && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s),
5341 gimple_assign_rhs1 (s),
5342 gimple_assign_rhs2 (s),
5343 code2, op2a, op2b)))
5345 /* Handle the OR case, where we are reassociating:
5346 (inner1 OR inner2) OR (op2a code2 op2b)
5347 => (t OR inner2)
5348 If the partial result t is a constant, we win. Otherwise
5349 continue on to try reassociating with the other inner test. */
5350 if (is_or)
5352 if (integer_onep (t))
5353 return boolean_true_node;
5354 else if (integer_zerop (t))
5355 return inner2;
5358 /* Handle the AND case, where we are redistributing:
5359 (inner1 AND inner2) OR (op2a code2 op2b)
5360 => (t AND (inner2 OR (op2a code op2b))) */
5361 else if (integer_zerop (t))
5362 return boolean_false_node;
5364 /* Save partial result for later. */
5365 partial = t;
5368 /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
5369 if (TREE_CODE (inner2) == SSA_NAME
5370 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
5371 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
5372 && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s),
5373 gimple_assign_rhs1 (s),
5374 gimple_assign_rhs2 (s),
5375 code2, op2a, op2b)))
5377 /* Handle the OR case, where we are reassociating:
5378 (inner1 OR inner2) OR (op2a code2 op2b)
5379 => (inner1 OR t)
5380 => (t OR partial) */
5381 if (is_or)
5383 if (integer_zerop (t))
5384 return inner1;
5385 else if (integer_onep (t))
5386 return boolean_true_node;
5387 /* If both are the same, we can apply the identity
5388 (x OR x) == x. */
5389 else if (partial && same_bool_result_p (t, partial))
5390 return t;
5393 /* Handle the AND case, where we are redistributing:
5394 (inner1 AND inner2) OR (op2a code2 op2b)
5395 => (t AND (inner1 OR (op2a code2 op2b)))
5396 => (t AND partial) */
5397 else
5399 if (integer_zerop (t))
5400 return boolean_false_node;
5401 else if (partial)
5403 /* We already got a simplification for the other
5404 operand to the redistributed AND expression. The
5405 interesting case is when at least one is true.
5406 Or, if both are the same, we can apply the identity
5407 (x AND x) == x. */
5408 if (integer_onep (partial))
5409 return t;
5410 else if (integer_onep (t))
5411 return partial;
5412 else if (same_bool_result_p (t, partial))
5413 return t;
5418 return NULL_TREE;
5421 /* Try to simplify the OR of two comparisons defined by
5422 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
5423 If this can be done without constructing an intermediate value,
5424 return the resulting tree; otherwise NULL_TREE is returned.
5425 This function is deliberately asymmetric as it recurses on SSA_DEFs
5426 in the first comparison but not the second. */
5428 static tree
5429 or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
5430 enum tree_code code2, tree op2a, tree op2b)
5432 tree truth_type = truth_type_for (TREE_TYPE (op1a));
5434 /* First check for ((x CODE1 y) OR (x CODE2 y)). */
5435 if (operand_equal_p (op1a, op2a, 0)
5436 && operand_equal_p (op1b, op2b, 0))
5438 /* Result will be either NULL_TREE, or a combined comparison. */
5439 tree t = combine_comparisons (UNKNOWN_LOCATION,
5440 TRUTH_ORIF_EXPR, code1, code2,
5441 truth_type, op1a, op1b);
5442 if (t)
5443 return t;
5446 /* Likewise the swapped case of the above. */
5447 if (operand_equal_p (op1a, op2b, 0)
5448 && operand_equal_p (op1b, op2a, 0))
5450 /* Result will be either NULL_TREE, or a combined comparison. */
5451 tree t = combine_comparisons (UNKNOWN_LOCATION,
5452 TRUTH_ORIF_EXPR, code1,
5453 swap_tree_comparison (code2),
5454 truth_type, op1a, op1b);
5455 if (t)
5456 return t;
5459 /* If both comparisons are of the same value against constants, we might
5460 be able to merge them. */
5461 if (operand_equal_p (op1a, op2a, 0)
5462 && TREE_CODE (op1b) == INTEGER_CST
5463 && TREE_CODE (op2b) == INTEGER_CST)
5465 int cmp = tree_int_cst_compare (op1b, op2b);
5467 /* If we have (op1a != op1b), we should either be able to
5468 return that or TRUE, depending on whether the constant op1b
5469 also satisfies the other comparison against op2b. */
5470 if (code1 == NE_EXPR)
5472 bool done = true;
5473 bool val;
5474 switch (code2)
5476 case EQ_EXPR: val = (cmp == 0); break;
5477 case NE_EXPR: val = (cmp != 0); break;
5478 case LT_EXPR: val = (cmp < 0); break;
5479 case GT_EXPR: val = (cmp > 0); break;
5480 case LE_EXPR: val = (cmp <= 0); break;
5481 case GE_EXPR: val = (cmp >= 0); break;
5482 default: done = false;
5484 if (done)
5486 if (val)
5487 return boolean_true_node;
5488 else
5489 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5492 /* Likewise if the second comparison is a != comparison. */
5493 else if (code2 == NE_EXPR)
5495 bool done = true;
5496 bool val;
5497 switch (code1)
5499 case EQ_EXPR: val = (cmp == 0); break;
5500 case NE_EXPR: val = (cmp != 0); break;
5501 case LT_EXPR: val = (cmp > 0); break;
5502 case GT_EXPR: val = (cmp < 0); break;
5503 case LE_EXPR: val = (cmp >= 0); break;
5504 case GE_EXPR: val = (cmp <= 0); break;
5505 default: done = false;
5507 if (done)
5509 if (val)
5510 return boolean_true_node;
5511 else
5512 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5516 /* See if an equality test is redundant with the other comparison. */
5517 else if (code1 == EQ_EXPR)
5519 bool val;
5520 switch (code2)
5522 case EQ_EXPR: val = (cmp == 0); break;
5523 case NE_EXPR: val = (cmp != 0); break;
5524 case LT_EXPR: val = (cmp < 0); break;
5525 case GT_EXPR: val = (cmp > 0); break;
5526 case LE_EXPR: val = (cmp <= 0); break;
5527 case GE_EXPR: val = (cmp >= 0); break;
5528 default:
5529 val = false;
5531 if (val)
5532 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5534 else if (code2 == EQ_EXPR)
5536 bool val;
5537 switch (code1)
5539 case EQ_EXPR: val = (cmp == 0); break;
5540 case NE_EXPR: val = (cmp != 0); break;
5541 case LT_EXPR: val = (cmp > 0); break;
5542 case GT_EXPR: val = (cmp < 0); break;
5543 case LE_EXPR: val = (cmp >= 0); break;
5544 case GE_EXPR: val = (cmp <= 0); break;
5545 default:
5546 val = false;
5548 if (val)
5549 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5552 /* Chose the less restrictive of two < or <= comparisons. */
5553 else if ((code1 == LT_EXPR || code1 == LE_EXPR)
5554 && (code2 == LT_EXPR || code2 == LE_EXPR))
5556 if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
5557 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5558 else
5559 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5562 /* Likewise chose the less restrictive of two > or >= comparisons. */
5563 else if ((code1 == GT_EXPR || code1 == GE_EXPR)
5564 && (code2 == GT_EXPR || code2 == GE_EXPR))
5566 if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
5567 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5568 else
5569 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5572 /* Check for singleton ranges. */
5573 else if (cmp == 0
5574 && ((code1 == LT_EXPR && code2 == GT_EXPR)
5575 || (code1 == GT_EXPR && code2 == LT_EXPR)))
5576 return fold_build2 (NE_EXPR, boolean_type_node, op1a, op2b);
5578 /* Check for less/greater pairs that don't restrict the range at all. */
5579 else if (cmp >= 0
5580 && (code1 == LT_EXPR || code1 == LE_EXPR)
5581 && (code2 == GT_EXPR || code2 == GE_EXPR))
5582 return boolean_true_node;
5583 else if (cmp <= 0
5584 && (code1 == GT_EXPR || code1 == GE_EXPR)
5585 && (code2 == LT_EXPR || code2 == LE_EXPR))
5586 return boolean_true_node;
5589 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
5590 NAME's definition is a truth value. See if there are any simplifications
5591 that can be done against the NAME's definition. */
5592 if (TREE_CODE (op1a) == SSA_NAME
5593 && (code1 == NE_EXPR || code1 == EQ_EXPR)
5594 && (integer_zerop (op1b) || integer_onep (op1b)))
5596 bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
5597 || (code1 == NE_EXPR && integer_onep (op1b)));
5598 gimple *stmt = SSA_NAME_DEF_STMT (op1a);
5599 switch (gimple_code (stmt))
5601 case GIMPLE_ASSIGN:
5602 /* Try to simplify by copy-propagating the definition. */
5603 return or_var_with_comparison (op1a, invert, code2, op2a, op2b);
5605 case GIMPLE_PHI:
5606 /* If every argument to the PHI produces the same result when
5607 ORed with the second comparison, we win.
5608 Do not do this unless the type is bool since we need a bool
5609 result here anyway. */
5610 if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
5612 tree result = NULL_TREE;
5613 unsigned i;
5614 for (i = 0; i < gimple_phi_num_args (stmt); i++)
5616 tree arg = gimple_phi_arg_def (stmt, i);
5618 /* If this PHI has itself as an argument, ignore it.
5619 If all the other args produce the same result,
5620 we're still OK. */
5621 if (arg == gimple_phi_result (stmt))
5622 continue;
5623 else if (TREE_CODE (arg) == INTEGER_CST)
5625 if (invert ? integer_zerop (arg) : integer_nonzerop (arg))
5627 if (!result)
5628 result = boolean_true_node;
5629 else if (!integer_onep (result))
5630 return NULL_TREE;
5632 else if (!result)
5633 result = fold_build2 (code2, boolean_type_node,
5634 op2a, op2b);
5635 else if (!same_bool_comparison_p (result,
5636 code2, op2a, op2b))
5637 return NULL_TREE;
5639 else if (TREE_CODE (arg) == SSA_NAME
5640 && !SSA_NAME_IS_DEFAULT_DEF (arg))
5642 tree temp;
5643 gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
5644 /* In simple cases we can look through PHI nodes,
5645 but we have to be careful with loops.
5646 See PR49073. */
5647 if (! dom_info_available_p (CDI_DOMINATORS)
5648 || gimple_bb (def_stmt) == gimple_bb (stmt)
5649 || dominated_by_p (CDI_DOMINATORS,
5650 gimple_bb (def_stmt),
5651 gimple_bb (stmt)))
5652 return NULL_TREE;
5653 temp = or_var_with_comparison (arg, invert, code2,
5654 op2a, op2b);
5655 if (!temp)
5656 return NULL_TREE;
5657 else if (!result)
5658 result = temp;
5659 else if (!same_bool_result_p (result, temp))
5660 return NULL_TREE;
5662 else
5663 return NULL_TREE;
5665 return result;
5668 default:
5669 break;
5672 return NULL_TREE;
5675 /* Try to simplify the OR of two comparisons, specified by
5676 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
5677 If this can be simplified to a single expression (without requiring
5678 introducing more SSA variables to hold intermediate values),
5679 return the resulting tree. Otherwise return NULL_TREE.
5680 If the result expression is non-null, it has boolean type. */
5682 tree
5683 maybe_fold_or_comparisons (enum tree_code code1, tree op1a, tree op1b,
5684 enum tree_code code2, tree op2a, tree op2b)
5686 tree t = or_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b);
5687 if (t)
5688 return t;
5689 else
5690 return or_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b);
5694 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
5696 Either NULL_TREE, a simplified but non-constant or a constant
5697 is returned.
5699 ??? This should go into a gimple-fold-inline.h file to be eventually
5700 privatized with the single valueize function used in the various TUs
5701 to avoid the indirect function call overhead. */
5703 tree
5704 gimple_fold_stmt_to_constant_1 (gimple *stmt, tree (*valueize) (tree),
5705 tree (*gvalueize) (tree))
5707 code_helper rcode;
5708 tree ops[3] = {};
5709 /* ??? The SSA propagators do not correctly deal with following SSA use-def
5710 edges if there are intermediate VARYING defs. For this reason
5711 do not follow SSA edges here even though SCCVN can technically
5712 just deal fine with that. */
5713 if (gimple_simplify (stmt, &rcode, ops, NULL, gvalueize, valueize))
5715 tree res = NULL_TREE;
5716 if (gimple_simplified_result_is_gimple_val (rcode, ops))
5717 res = ops[0];
5718 else if (mprts_hook)
5719 res = mprts_hook (rcode, gimple_expr_type (stmt), ops);
5720 if (res)
5722 if (dump_file && dump_flags & TDF_DETAILS)
5724 fprintf (dump_file, "Match-and-simplified ");
5725 print_gimple_expr (dump_file, stmt, 0, TDF_SLIM);
5726 fprintf (dump_file, " to ");
5727 print_generic_expr (dump_file, res, 0);
5728 fprintf (dump_file, "\n");
5730 return res;
5734 location_t loc = gimple_location (stmt);
5735 switch (gimple_code (stmt))
5737 case GIMPLE_ASSIGN:
5739 enum tree_code subcode = gimple_assign_rhs_code (stmt);
5741 switch (get_gimple_rhs_class (subcode))
5743 case GIMPLE_SINGLE_RHS:
5745 tree rhs = gimple_assign_rhs1 (stmt);
5746 enum tree_code_class kind = TREE_CODE_CLASS (subcode);
5748 if (TREE_CODE (rhs) == SSA_NAME)
5750 /* If the RHS is an SSA_NAME, return its known constant value,
5751 if any. */
5752 return (*valueize) (rhs);
5754 /* Handle propagating invariant addresses into address
5755 operations. */
5756 else if (TREE_CODE (rhs) == ADDR_EXPR
5757 && !is_gimple_min_invariant (rhs))
5759 HOST_WIDE_INT offset = 0;
5760 tree base;
5761 base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs, 0),
5762 &offset,
5763 valueize);
5764 if (base
5765 && (CONSTANT_CLASS_P (base)
5766 || decl_address_invariant_p (base)))
5767 return build_invariant_address (TREE_TYPE (rhs),
5768 base, offset);
5770 else if (TREE_CODE (rhs) == CONSTRUCTOR
5771 && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE
5772 && (CONSTRUCTOR_NELTS (rhs)
5773 == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs))))
5775 unsigned i;
5776 tree val, *vec;
5778 vec = XALLOCAVEC (tree,
5779 TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)));
5780 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
5782 val = (*valueize) (val);
5783 if (TREE_CODE (val) == INTEGER_CST
5784 || TREE_CODE (val) == REAL_CST
5785 || TREE_CODE (val) == FIXED_CST)
5786 vec[i] = val;
5787 else
5788 return NULL_TREE;
5791 return build_vector (TREE_TYPE (rhs), vec);
5793 if (subcode == OBJ_TYPE_REF)
5795 tree val = (*valueize) (OBJ_TYPE_REF_EXPR (rhs));
5796 /* If callee is constant, we can fold away the wrapper. */
5797 if (is_gimple_min_invariant (val))
5798 return val;
5801 if (kind == tcc_reference)
5803 if ((TREE_CODE (rhs) == VIEW_CONVERT_EXPR
5804 || TREE_CODE (rhs) == REALPART_EXPR
5805 || TREE_CODE (rhs) == IMAGPART_EXPR)
5806 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
5808 tree val = (*valueize) (TREE_OPERAND (rhs, 0));
5809 return fold_unary_loc (EXPR_LOCATION (rhs),
5810 TREE_CODE (rhs),
5811 TREE_TYPE (rhs), val);
5813 else if (TREE_CODE (rhs) == BIT_FIELD_REF
5814 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
5816 tree val = (*valueize) (TREE_OPERAND (rhs, 0));
5817 return fold_ternary_loc (EXPR_LOCATION (rhs),
5818 TREE_CODE (rhs),
5819 TREE_TYPE (rhs), val,
5820 TREE_OPERAND (rhs, 1),
5821 TREE_OPERAND (rhs, 2));
5823 else if (TREE_CODE (rhs) == MEM_REF
5824 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
5826 tree val = (*valueize) (TREE_OPERAND (rhs, 0));
5827 if (TREE_CODE (val) == ADDR_EXPR
5828 && is_gimple_min_invariant (val))
5830 tree tem = fold_build2 (MEM_REF, TREE_TYPE (rhs),
5831 unshare_expr (val),
5832 TREE_OPERAND (rhs, 1));
5833 if (tem)
5834 rhs = tem;
5837 return fold_const_aggregate_ref_1 (rhs, valueize);
5839 else if (kind == tcc_declaration)
5840 return get_symbol_constant_value (rhs);
5841 return rhs;
5844 case GIMPLE_UNARY_RHS:
5845 return NULL_TREE;
5847 case GIMPLE_BINARY_RHS:
5848 /* Translate &x + CST into an invariant form suitable for
5849 further propagation. */
5850 if (subcode == POINTER_PLUS_EXPR)
5852 tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
5853 tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
5854 if (TREE_CODE (op0) == ADDR_EXPR
5855 && TREE_CODE (op1) == INTEGER_CST)
5857 tree off = fold_convert (ptr_type_node, op1);
5858 return build_fold_addr_expr_loc
5859 (loc,
5860 fold_build2 (MEM_REF,
5861 TREE_TYPE (TREE_TYPE (op0)),
5862 unshare_expr (op0), off));
5865 /* Canonicalize bool != 0 and bool == 0 appearing after
5866 valueization. While gimple_simplify handles this
5867 it can get confused by the ~X == 1 -> X == 0 transform
5868 which we cant reduce to a SSA name or a constant
5869 (and we have no way to tell gimple_simplify to not
5870 consider those transforms in the first place). */
5871 else if (subcode == EQ_EXPR
5872 || subcode == NE_EXPR)
5874 tree lhs = gimple_assign_lhs (stmt);
5875 tree op0 = gimple_assign_rhs1 (stmt);
5876 if (useless_type_conversion_p (TREE_TYPE (lhs),
5877 TREE_TYPE (op0)))
5879 tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
5880 op0 = (*valueize) (op0);
5881 if (TREE_CODE (op0) == INTEGER_CST)
5882 std::swap (op0, op1);
5883 if (TREE_CODE (op1) == INTEGER_CST
5884 && ((subcode == NE_EXPR && integer_zerop (op1))
5885 || (subcode == EQ_EXPR && integer_onep (op1))))
5886 return op0;
5889 return NULL_TREE;
5891 case GIMPLE_TERNARY_RHS:
5893 /* Handle ternary operators that can appear in GIMPLE form. */
5894 tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
5895 tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
5896 tree op2 = (*valueize) (gimple_assign_rhs3 (stmt));
5897 return fold_ternary_loc (loc, subcode,
5898 gimple_expr_type (stmt), op0, op1, op2);
5901 default:
5902 gcc_unreachable ();
5906 case GIMPLE_CALL:
5908 tree fn;
5909 gcall *call_stmt = as_a <gcall *> (stmt);
5911 if (gimple_call_internal_p (stmt))
5913 enum tree_code subcode = ERROR_MARK;
5914 switch (gimple_call_internal_fn (stmt))
5916 case IFN_UBSAN_CHECK_ADD:
5917 subcode = PLUS_EXPR;
5918 break;
5919 case IFN_UBSAN_CHECK_SUB:
5920 subcode = MINUS_EXPR;
5921 break;
5922 case IFN_UBSAN_CHECK_MUL:
5923 subcode = MULT_EXPR;
5924 break;
5925 case IFN_BUILTIN_EXPECT:
5927 tree arg0 = gimple_call_arg (stmt, 0);
5928 tree op0 = (*valueize) (arg0);
5929 if (TREE_CODE (op0) == INTEGER_CST)
5930 return op0;
5931 return NULL_TREE;
5933 default:
5934 return NULL_TREE;
5936 tree arg0 = gimple_call_arg (stmt, 0);
5937 tree arg1 = gimple_call_arg (stmt, 1);
5938 tree op0 = (*valueize) (arg0);
5939 tree op1 = (*valueize) (arg1);
5941 if (TREE_CODE (op0) != INTEGER_CST
5942 || TREE_CODE (op1) != INTEGER_CST)
5944 switch (subcode)
5946 case MULT_EXPR:
5947 /* x * 0 = 0 * x = 0 without overflow. */
5948 if (integer_zerop (op0) || integer_zerop (op1))
5949 return build_zero_cst (TREE_TYPE (arg0));
5950 break;
5951 case MINUS_EXPR:
5952 /* y - y = 0 without overflow. */
5953 if (operand_equal_p (op0, op1, 0))
5954 return build_zero_cst (TREE_TYPE (arg0));
5955 break;
5956 default:
5957 break;
5960 tree res
5961 = fold_binary_loc (loc, subcode, TREE_TYPE (arg0), op0, op1);
5962 if (res
5963 && TREE_CODE (res) == INTEGER_CST
5964 && !TREE_OVERFLOW (res))
5965 return res;
5966 return NULL_TREE;
5969 fn = (*valueize) (gimple_call_fn (stmt));
5970 if (TREE_CODE (fn) == ADDR_EXPR
5971 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5972 && DECL_BUILT_IN (TREE_OPERAND (fn, 0))
5973 && gimple_builtin_call_types_compatible_p (stmt,
5974 TREE_OPERAND (fn, 0)))
5976 tree *args = XALLOCAVEC (tree, gimple_call_num_args (stmt));
5977 tree retval;
5978 unsigned i;
5979 for (i = 0; i < gimple_call_num_args (stmt); ++i)
5980 args[i] = (*valueize) (gimple_call_arg (stmt, i));
5981 retval = fold_builtin_call_array (loc,
5982 gimple_call_return_type (call_stmt),
5983 fn, gimple_call_num_args (stmt), args);
5984 if (retval)
5986 /* fold_call_expr wraps the result inside a NOP_EXPR. */
5987 STRIP_NOPS (retval);
5988 retval = fold_convert (gimple_call_return_type (call_stmt),
5989 retval);
5991 return retval;
5993 return NULL_TREE;
5996 default:
5997 return NULL_TREE;
6001 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
6002 Returns NULL_TREE if folding to a constant is not possible, otherwise
6003 returns a constant according to is_gimple_min_invariant. */
6005 tree
6006 gimple_fold_stmt_to_constant (gimple *stmt, tree (*valueize) (tree))
6008 tree res = gimple_fold_stmt_to_constant_1 (stmt, valueize);
6009 if (res && is_gimple_min_invariant (res))
6010 return res;
6011 return NULL_TREE;
6015 /* The following set of functions are supposed to fold references using
6016 their constant initializers. */
6018 /* See if we can find constructor defining value of BASE.
6019 When we know the consructor with constant offset (such as
6020 base is array[40] and we do know constructor of array), then
6021 BIT_OFFSET is adjusted accordingly.
6023 As a special case, return error_mark_node when constructor
6024 is not explicitly available, but it is known to be zero
6025 such as 'static const int a;'. */
6026 static tree
6027 get_base_constructor (tree base, HOST_WIDE_INT *bit_offset,
6028 tree (*valueize)(tree))
6030 HOST_WIDE_INT bit_offset2, size, max_size;
6031 bool reverse;
6033 if (TREE_CODE (base) == MEM_REF)
6035 if (!integer_zerop (TREE_OPERAND (base, 1)))
6037 if (!tree_fits_shwi_p (TREE_OPERAND (base, 1)))
6038 return NULL_TREE;
6039 *bit_offset += (mem_ref_offset (base).to_short_addr ()
6040 * BITS_PER_UNIT);
6043 if (valueize
6044 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
6045 base = valueize (TREE_OPERAND (base, 0));
6046 if (!base || TREE_CODE (base) != ADDR_EXPR)
6047 return NULL_TREE;
6048 base = TREE_OPERAND (base, 0);
6050 else if (valueize
6051 && TREE_CODE (base) == SSA_NAME)
6052 base = valueize (base);
6054 /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
6055 DECL_INITIAL. If BASE is a nested reference into another
6056 ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
6057 the inner reference. */
6058 switch (TREE_CODE (base))
6060 case VAR_DECL:
6061 case CONST_DECL:
6063 tree init = ctor_for_folding (base);
6065 /* Our semantic is exact opposite of ctor_for_folding;
6066 NULL means unknown, while error_mark_node is 0. */
6067 if (init == error_mark_node)
6068 return NULL_TREE;
6069 if (!init)
6070 return error_mark_node;
6071 return init;
6074 case VIEW_CONVERT_EXPR:
6075 return get_base_constructor (TREE_OPERAND (base, 0),
6076 bit_offset, valueize);
6078 case ARRAY_REF:
6079 case COMPONENT_REF:
6080 base = get_ref_base_and_extent (base, &bit_offset2, &size, &max_size,
6081 &reverse);
6082 if (max_size == -1 || size != max_size)
6083 return NULL_TREE;
6084 *bit_offset += bit_offset2;
6085 return get_base_constructor (base, bit_offset, valueize);
6087 case CONSTRUCTOR:
6088 return base;
6090 default:
6091 if (CONSTANT_CLASS_P (base))
6092 return base;
6094 return NULL_TREE;
6098 /* CTOR is CONSTRUCTOR of an array type. Fold reference of type TYPE and size
6099 SIZE to the memory at bit OFFSET. */
6101 static tree
6102 fold_array_ctor_reference (tree type, tree ctor,
6103 unsigned HOST_WIDE_INT offset,
6104 unsigned HOST_WIDE_INT size,
6105 tree from_decl)
6107 offset_int low_bound;
6108 offset_int elt_size;
6109 offset_int access_index;
6110 tree domain_type = NULL_TREE;
6111 HOST_WIDE_INT inner_offset;
6113 /* Compute low bound and elt size. */
6114 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
6115 domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
6116 if (domain_type && TYPE_MIN_VALUE (domain_type))
6118 /* Static constructors for variably sized objects makes no sense. */
6119 if (TREE_CODE (TYPE_MIN_VALUE (domain_type)) != INTEGER_CST)
6120 return NULL_TREE;
6121 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
6123 else
6124 low_bound = 0;
6125 /* Static constructors for variably sized objects makes no sense. */
6126 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor)))) != INTEGER_CST)
6127 return NULL_TREE;
6128 elt_size = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor))));
6130 /* We can handle only constantly sized accesses that are known to not
6131 be larger than size of array element. */
6132 if (!TYPE_SIZE_UNIT (type)
6133 || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
6134 || elt_size < wi::to_offset (TYPE_SIZE_UNIT (type))
6135 || elt_size == 0)
6136 return NULL_TREE;
6138 /* Compute the array index we look for. */
6139 access_index = wi::udiv_trunc (offset_int (offset / BITS_PER_UNIT),
6140 elt_size);
6141 access_index += low_bound;
6143 /* And offset within the access. */
6144 inner_offset = offset % (elt_size.to_uhwi () * BITS_PER_UNIT);
6146 /* See if the array field is large enough to span whole access. We do not
6147 care to fold accesses spanning multiple array indexes. */
6148 if (inner_offset + size > elt_size.to_uhwi () * BITS_PER_UNIT)
6149 return NULL_TREE;
6150 if (tree val = get_array_ctor_element_at_index (ctor, access_index))
6151 return fold_ctor_reference (type, val, inner_offset, size, from_decl);
6153 /* When memory is not explicitely mentioned in constructor,
6154 it is 0 (or out of range). */
6155 return build_zero_cst (type);
6158 /* CTOR is CONSTRUCTOR of an aggregate or vector.
6159 Fold reference of type TYPE and size SIZE to the memory at bit OFFSET. */
6161 static tree
6162 fold_nonarray_ctor_reference (tree type, tree ctor,
6163 unsigned HOST_WIDE_INT offset,
6164 unsigned HOST_WIDE_INT size,
6165 tree from_decl)
6167 unsigned HOST_WIDE_INT cnt;
6168 tree cfield, cval;
6170 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield,
6171 cval)
6173 tree byte_offset = DECL_FIELD_OFFSET (cfield);
6174 tree field_offset = DECL_FIELD_BIT_OFFSET (cfield);
6175 tree field_size = DECL_SIZE (cfield);
6176 offset_int bitoffset;
6177 offset_int bitoffset_end, access_end;
6179 /* Variable sized objects in static constructors makes no sense,
6180 but field_size can be NULL for flexible array members. */
6181 gcc_assert (TREE_CODE (field_offset) == INTEGER_CST
6182 && TREE_CODE (byte_offset) == INTEGER_CST
6183 && (field_size != NULL_TREE
6184 ? TREE_CODE (field_size) == INTEGER_CST
6185 : TREE_CODE (TREE_TYPE (cfield)) == ARRAY_TYPE));
6187 /* Compute bit offset of the field. */
6188 bitoffset = (wi::to_offset (field_offset)
6189 + (wi::to_offset (byte_offset) << LOG2_BITS_PER_UNIT));
6190 /* Compute bit offset where the field ends. */
6191 if (field_size != NULL_TREE)
6192 bitoffset_end = bitoffset + wi::to_offset (field_size);
6193 else
6194 bitoffset_end = 0;
6196 access_end = offset_int (offset) + size;
6198 /* Is there any overlap between [OFFSET, OFFSET+SIZE) and
6199 [BITOFFSET, BITOFFSET_END)? */
6200 if (wi::cmps (access_end, bitoffset) > 0
6201 && (field_size == NULL_TREE
6202 || wi::lts_p (offset, bitoffset_end)))
6204 offset_int inner_offset = offset_int (offset) - bitoffset;
6205 /* We do have overlap. Now see if field is large enough to
6206 cover the access. Give up for accesses spanning multiple
6207 fields. */
6208 if (wi::cmps (access_end, bitoffset_end) > 0)
6209 return NULL_TREE;
6210 if (offset < bitoffset)
6211 return NULL_TREE;
6212 return fold_ctor_reference (type, cval,
6213 inner_offset.to_uhwi (), size,
6214 from_decl);
6217 /* When memory is not explicitely mentioned in constructor, it is 0. */
6218 return build_zero_cst (type);
6221 /* CTOR is value initializing memory, fold reference of type TYPE and size SIZE
6222 to the memory at bit OFFSET. */
6224 tree
6225 fold_ctor_reference (tree type, tree ctor, unsigned HOST_WIDE_INT offset,
6226 unsigned HOST_WIDE_INT size, tree from_decl)
6228 tree ret;
6230 /* We found the field with exact match. */
6231 if (useless_type_conversion_p (type, TREE_TYPE (ctor))
6232 && !offset)
6233 return canonicalize_constructor_val (unshare_expr (ctor), from_decl);
6235 /* We are at the end of walk, see if we can view convert the
6236 result. */
6237 if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor)) && !offset
6238 /* VIEW_CONVERT_EXPR is defined only for matching sizes. */
6239 && !compare_tree_int (TYPE_SIZE (type), size)
6240 && !compare_tree_int (TYPE_SIZE (TREE_TYPE (ctor)), size))
6242 ret = canonicalize_constructor_val (unshare_expr (ctor), from_decl);
6243 if (ret)
6245 ret = fold_unary (VIEW_CONVERT_EXPR, type, ret);
6246 if (ret)
6247 STRIP_USELESS_TYPE_CONVERSION (ret);
6249 return ret;
6251 /* For constants and byte-aligned/sized reads try to go through
6252 native_encode/interpret. */
6253 if (CONSTANT_CLASS_P (ctor)
6254 && BITS_PER_UNIT == 8
6255 && offset % BITS_PER_UNIT == 0
6256 && size % BITS_PER_UNIT == 0
6257 && size <= MAX_BITSIZE_MODE_ANY_MODE)
6259 unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
6260 int len = native_encode_expr (ctor, buf, size / BITS_PER_UNIT,
6261 offset / BITS_PER_UNIT);
6262 if (len > 0)
6263 return native_interpret_expr (type, buf, len);
6265 if (TREE_CODE (ctor) == CONSTRUCTOR)
6268 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE
6269 || TREE_CODE (TREE_TYPE (ctor)) == VECTOR_TYPE)
6270 return fold_array_ctor_reference (type, ctor, offset, size,
6271 from_decl);
6272 else
6273 return fold_nonarray_ctor_reference (type, ctor, offset, size,
6274 from_decl);
6277 return NULL_TREE;
6280 /* Return the tree representing the element referenced by T if T is an
6281 ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
6282 names using VALUEIZE. Return NULL_TREE otherwise. */
6284 tree
6285 fold_const_aggregate_ref_1 (tree t, tree (*valueize) (tree))
6287 tree ctor, idx, base;
6288 HOST_WIDE_INT offset, size, max_size;
6289 tree tem;
6290 bool reverse;
6292 if (TREE_THIS_VOLATILE (t))
6293 return NULL_TREE;
6295 if (DECL_P (t))
6296 return get_symbol_constant_value (t);
6298 tem = fold_read_from_constant_string (t);
6299 if (tem)
6300 return tem;
6302 switch (TREE_CODE (t))
6304 case ARRAY_REF:
6305 case ARRAY_RANGE_REF:
6306 /* Constant indexes are handled well by get_base_constructor.
6307 Only special case variable offsets.
6308 FIXME: This code can't handle nested references with variable indexes
6309 (they will be handled only by iteration of ccp). Perhaps we can bring
6310 get_ref_base_and_extent here and make it use a valueize callback. */
6311 if (TREE_CODE (TREE_OPERAND (t, 1)) == SSA_NAME
6312 && valueize
6313 && (idx = (*valueize) (TREE_OPERAND (t, 1)))
6314 && TREE_CODE (idx) == INTEGER_CST)
6316 tree low_bound, unit_size;
6318 /* If the resulting bit-offset is constant, track it. */
6319 if ((low_bound = array_ref_low_bound (t),
6320 TREE_CODE (low_bound) == INTEGER_CST)
6321 && (unit_size = array_ref_element_size (t),
6322 tree_fits_uhwi_p (unit_size)))
6324 offset_int woffset
6325 = wi::sext (wi::to_offset (idx) - wi::to_offset (low_bound),
6326 TYPE_PRECISION (TREE_TYPE (idx)));
6328 if (wi::fits_shwi_p (woffset))
6330 offset = woffset.to_shwi ();
6331 /* TODO: This code seems wrong, multiply then check
6332 to see if it fits. */
6333 offset *= tree_to_uhwi (unit_size);
6334 offset *= BITS_PER_UNIT;
6336 base = TREE_OPERAND (t, 0);
6337 ctor = get_base_constructor (base, &offset, valueize);
6338 /* Empty constructor. Always fold to 0. */
6339 if (ctor == error_mark_node)
6340 return build_zero_cst (TREE_TYPE (t));
6341 /* Out of bound array access. Value is undefined,
6342 but don't fold. */
6343 if (offset < 0)
6344 return NULL_TREE;
6345 /* We can not determine ctor. */
6346 if (!ctor)
6347 return NULL_TREE;
6348 return fold_ctor_reference (TREE_TYPE (t), ctor, offset,
6349 tree_to_uhwi (unit_size)
6350 * BITS_PER_UNIT,
6351 base);
6355 /* Fallthru. */
6357 case COMPONENT_REF:
6358 case BIT_FIELD_REF:
6359 case TARGET_MEM_REF:
6360 case MEM_REF:
6361 base = get_ref_base_and_extent (t, &offset, &size, &max_size, &reverse);
6362 ctor = get_base_constructor (base, &offset, valueize);
6364 /* Empty constructor. Always fold to 0. */
6365 if (ctor == error_mark_node)
6366 return build_zero_cst (TREE_TYPE (t));
6367 /* We do not know precise address. */
6368 if (max_size == -1 || max_size != size)
6369 return NULL_TREE;
6370 /* We can not determine ctor. */
6371 if (!ctor)
6372 return NULL_TREE;
6374 /* Out of bound array access. Value is undefined, but don't fold. */
6375 if (offset < 0)
6376 return NULL_TREE;
6378 return fold_ctor_reference (TREE_TYPE (t), ctor, offset, size,
6379 base);
6381 case REALPART_EXPR:
6382 case IMAGPART_EXPR:
6384 tree c = fold_const_aggregate_ref_1 (TREE_OPERAND (t, 0), valueize);
6385 if (c && TREE_CODE (c) == COMPLEX_CST)
6386 return fold_build1_loc (EXPR_LOCATION (t),
6387 TREE_CODE (t), TREE_TYPE (t), c);
6388 break;
6391 default:
6392 break;
6395 return NULL_TREE;
6398 tree
6399 fold_const_aggregate_ref (tree t)
6401 return fold_const_aggregate_ref_1 (t, NULL);
6404 /* Lookup virtual method with index TOKEN in a virtual table V
6405 at OFFSET.
6406 Set CAN_REFER if non-NULL to false if method
6407 is not referable or if the virtual table is ill-formed (such as rewriten
6408 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
6410 tree
6411 gimple_get_virt_method_for_vtable (HOST_WIDE_INT token,
6412 tree v,
6413 unsigned HOST_WIDE_INT offset,
6414 bool *can_refer)
6416 tree vtable = v, init, fn;
6417 unsigned HOST_WIDE_INT size;
6418 unsigned HOST_WIDE_INT elt_size, access_index;
6419 tree domain_type;
6421 if (can_refer)
6422 *can_refer = true;
6424 /* First of all double check we have virtual table. */
6425 if (!VAR_P (v) || !DECL_VIRTUAL_P (v))
6427 /* Pass down that we lost track of the target. */
6428 if (can_refer)
6429 *can_refer = false;
6430 return NULL_TREE;
6433 init = ctor_for_folding (v);
6435 /* The virtual tables should always be born with constructors
6436 and we always should assume that they are avaialble for
6437 folding. At the moment we do not stream them in all cases,
6438 but it should never happen that ctor seem unreachable. */
6439 gcc_assert (init);
6440 if (init == error_mark_node)
6442 gcc_assert (in_lto_p);
6443 /* Pass down that we lost track of the target. */
6444 if (can_refer)
6445 *can_refer = false;
6446 return NULL_TREE;
6448 gcc_checking_assert (TREE_CODE (TREE_TYPE (v)) == ARRAY_TYPE);
6449 size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v))));
6450 offset *= BITS_PER_UNIT;
6451 offset += token * size;
6453 /* Lookup the value in the constructor that is assumed to be array.
6454 This is equivalent to
6455 fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
6456 offset, size, NULL);
6457 but in a constant time. We expect that frontend produced a simple
6458 array without indexed initializers. */
6460 gcc_checking_assert (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE);
6461 domain_type = TYPE_DOMAIN (TREE_TYPE (init));
6462 gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type)));
6463 elt_size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init))));
6465 access_index = offset / BITS_PER_UNIT / elt_size;
6466 gcc_checking_assert (offset % (elt_size * BITS_PER_UNIT) == 0);
6468 /* This code makes an assumption that there are no
6469 indexed fileds produced by C++ FE, so we can directly index the array. */
6470 if (access_index < CONSTRUCTOR_NELTS (init))
6472 fn = CONSTRUCTOR_ELT (init, access_index)->value;
6473 gcc_checking_assert (!CONSTRUCTOR_ELT (init, access_index)->index);
6474 STRIP_NOPS (fn);
6476 else
6477 fn = NULL;
6479 /* For type inconsistent program we may end up looking up virtual method
6480 in virtual table that does not contain TOKEN entries. We may overrun
6481 the virtual table and pick up a constant or RTTI info pointer.
6482 In any case the call is undefined. */
6483 if (!fn
6484 || (TREE_CODE (fn) != ADDR_EXPR && TREE_CODE (fn) != FDESC_EXPR)
6485 || TREE_CODE (TREE_OPERAND (fn, 0)) != FUNCTION_DECL)
6486 fn = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
6487 else
6489 fn = TREE_OPERAND (fn, 0);
6491 /* When cgraph node is missing and function is not public, we cannot
6492 devirtualize. This can happen in WHOPR when the actual method
6493 ends up in other partition, because we found devirtualization
6494 possibility too late. */
6495 if (!can_refer_decl_in_current_unit_p (fn, vtable))
6497 if (can_refer)
6499 *can_refer = false;
6500 return fn;
6502 return NULL_TREE;
6506 /* Make sure we create a cgraph node for functions we'll reference.
6507 They can be non-existent if the reference comes from an entry
6508 of an external vtable for example. */
6509 cgraph_node::get_create (fn);
6511 return fn;
6514 /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
6515 is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
6516 KNOWN_BINFO carries the binfo describing the true type of
6517 OBJ_TYPE_REF_OBJECT(REF).
6518 Set CAN_REFER if non-NULL to false if method
6519 is not referable or if the virtual table is ill-formed (such as rewriten
6520 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
6522 tree
6523 gimple_get_virt_method_for_binfo (HOST_WIDE_INT token, tree known_binfo,
6524 bool *can_refer)
6526 unsigned HOST_WIDE_INT offset;
6527 tree v;
6529 v = BINFO_VTABLE (known_binfo);
6530 /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */
6531 if (!v)
6532 return NULL_TREE;
6534 if (!vtable_pointer_value_to_vtable (v, &v, &offset))
6536 if (can_refer)
6537 *can_refer = false;
6538 return NULL_TREE;
6540 return gimple_get_virt_method_for_vtable (token, v, offset, can_refer);
6543 /* Given a pointer value T, return a simplified version of an
6544 indirection through T, or NULL_TREE if no simplification is
6545 possible. Note that the resulting type may be different from
6546 the type pointed to in the sense that it is still compatible
6547 from the langhooks point of view. */
6549 tree
6550 gimple_fold_indirect_ref (tree t)
6552 tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype);
6553 tree sub = t;
6554 tree subtype;
6556 STRIP_NOPS (sub);
6557 subtype = TREE_TYPE (sub);
6558 if (!POINTER_TYPE_P (subtype)
6559 || TYPE_REF_CAN_ALIAS_ALL (ptype))
6560 return NULL_TREE;
6562 if (TREE_CODE (sub) == ADDR_EXPR)
6564 tree op = TREE_OPERAND (sub, 0);
6565 tree optype = TREE_TYPE (op);
6566 /* *&p => p */
6567 if (useless_type_conversion_p (type, optype))
6568 return op;
6570 /* *(foo *)&fooarray => fooarray[0] */
6571 if (TREE_CODE (optype) == ARRAY_TYPE
6572 && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST
6573 && useless_type_conversion_p (type, TREE_TYPE (optype)))
6575 tree type_domain = TYPE_DOMAIN (optype);
6576 tree min_val = size_zero_node;
6577 if (type_domain && TYPE_MIN_VALUE (type_domain))
6578 min_val = TYPE_MIN_VALUE (type_domain);
6579 if (TREE_CODE (min_val) == INTEGER_CST)
6580 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
6582 /* *(foo *)&complexfoo => __real__ complexfoo */
6583 else if (TREE_CODE (optype) == COMPLEX_TYPE
6584 && useless_type_conversion_p (type, TREE_TYPE (optype)))
6585 return fold_build1 (REALPART_EXPR, type, op);
6586 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
6587 else if (TREE_CODE (optype) == VECTOR_TYPE
6588 && useless_type_conversion_p (type, TREE_TYPE (optype)))
6590 tree part_width = TYPE_SIZE (type);
6591 tree index = bitsize_int (0);
6592 return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
6596 /* *(p + CST) -> ... */
6597 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
6598 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
6600 tree addr = TREE_OPERAND (sub, 0);
6601 tree off = TREE_OPERAND (sub, 1);
6602 tree addrtype;
6604 STRIP_NOPS (addr);
6605 addrtype = TREE_TYPE (addr);
6607 /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
6608 if (TREE_CODE (addr) == ADDR_EXPR
6609 && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE
6610 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))
6611 && tree_fits_uhwi_p (off))
6613 unsigned HOST_WIDE_INT offset = tree_to_uhwi (off);
6614 tree part_width = TYPE_SIZE (type);
6615 unsigned HOST_WIDE_INT part_widthi
6616 = tree_to_shwi (part_width) / BITS_PER_UNIT;
6617 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
6618 tree index = bitsize_int (indexi);
6619 if (offset / part_widthi
6620 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype)))
6621 return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0),
6622 part_width, index);
6625 /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
6626 if (TREE_CODE (addr) == ADDR_EXPR
6627 && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE
6628 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))))
6630 tree size = TYPE_SIZE_UNIT (type);
6631 if (tree_int_cst_equal (size, off))
6632 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0));
6635 /* *(p + CST) -> MEM_REF <p, CST>. */
6636 if (TREE_CODE (addr) != ADDR_EXPR
6637 || DECL_P (TREE_OPERAND (addr, 0)))
6638 return fold_build2 (MEM_REF, type,
6639 addr,
6640 wide_int_to_tree (ptype, off));
6643 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
6644 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
6645 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST
6646 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
6648 tree type_domain;
6649 tree min_val = size_zero_node;
6650 tree osub = sub;
6651 sub = gimple_fold_indirect_ref (sub);
6652 if (! sub)
6653 sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
6654 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
6655 if (type_domain && TYPE_MIN_VALUE (type_domain))
6656 min_val = TYPE_MIN_VALUE (type_domain);
6657 if (TREE_CODE (min_val) == INTEGER_CST)
6658 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
6661 return NULL_TREE;
6664 /* Return true if CODE is an operation that when operating on signed
6665 integer types involves undefined behavior on overflow and the
6666 operation can be expressed with unsigned arithmetic. */
6668 bool
6669 arith_code_with_undefined_signed_overflow (tree_code code)
6671 switch (code)
6673 case PLUS_EXPR:
6674 case MINUS_EXPR:
6675 case MULT_EXPR:
6676 case NEGATE_EXPR:
6677 case POINTER_PLUS_EXPR:
6678 return true;
6679 default:
6680 return false;
6684 /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic
6685 operation that can be transformed to unsigned arithmetic by converting
6686 its operand, carrying out the operation in the corresponding unsigned
6687 type and converting the result back to the original type.
6689 Returns a sequence of statements that replace STMT and also contain
6690 a modified form of STMT itself. */
6692 gimple_seq
6693 rewrite_to_defined_overflow (gimple *stmt)
6695 if (dump_file && (dump_flags & TDF_DETAILS))
6697 fprintf (dump_file, "rewriting stmt with undefined signed "
6698 "overflow ");
6699 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
6702 tree lhs = gimple_assign_lhs (stmt);
6703 tree type = unsigned_type_for (TREE_TYPE (lhs));
6704 gimple_seq stmts = NULL;
6705 for (unsigned i = 1; i < gimple_num_ops (stmt); ++i)
6707 tree op = gimple_op (stmt, i);
6708 op = gimple_convert (&stmts, type, op);
6709 gimple_set_op (stmt, i, op);
6711 gimple_assign_set_lhs (stmt, make_ssa_name (type, stmt));
6712 if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
6713 gimple_assign_set_rhs_code (stmt, PLUS_EXPR);
6714 gimple_seq_add_stmt (&stmts, stmt);
6715 gimple *cvt = gimple_build_assign (lhs, NOP_EXPR, gimple_assign_lhs (stmt));
6716 gimple_seq_add_stmt (&stmts, cvt);
6718 return stmts;
6722 /* The valueization hook we use for the gimple_build API simplification.
6723 This makes us match fold_buildN behavior by only combining with
6724 statements in the sequence(s) we are currently building. */
6726 static tree
6727 gimple_build_valueize (tree op)
6729 if (gimple_bb (SSA_NAME_DEF_STMT (op)) == NULL)
6730 return op;
6731 return NULL_TREE;
6734 /* Build the expression CODE OP0 of type TYPE with location LOC,
6735 simplifying it first if possible. Returns the built
6736 expression value and appends statements possibly defining it
6737 to SEQ. */
6739 tree
6740 gimple_build (gimple_seq *seq, location_t loc,
6741 enum tree_code code, tree type, tree op0)
6743 tree res = gimple_simplify (code, type, op0, seq, gimple_build_valueize);
6744 if (!res)
6746 res = create_tmp_reg_or_ssa_name (type);
6747 gimple *stmt;
6748 if (code == REALPART_EXPR
6749 || code == IMAGPART_EXPR
6750 || code == VIEW_CONVERT_EXPR)
6751 stmt = gimple_build_assign (res, code, build1 (code, type, op0));
6752 else
6753 stmt = gimple_build_assign (res, code, op0);
6754 gimple_set_location (stmt, loc);
6755 gimple_seq_add_stmt_without_update (seq, stmt);
6757 return res;
6760 /* Build the expression OP0 CODE OP1 of type TYPE with location LOC,
6761 simplifying it first if possible. Returns the built
6762 expression value and appends statements possibly defining it
6763 to SEQ. */
6765 tree
6766 gimple_build (gimple_seq *seq, location_t loc,
6767 enum tree_code code, tree type, tree op0, tree op1)
6769 tree res = gimple_simplify (code, type, op0, op1, seq, gimple_build_valueize);
6770 if (!res)
6772 res = create_tmp_reg_or_ssa_name (type);
6773 gimple *stmt = gimple_build_assign (res, code, op0, op1);
6774 gimple_set_location (stmt, loc);
6775 gimple_seq_add_stmt_without_update (seq, stmt);
6777 return res;
6780 /* Build the expression (CODE OP0 OP1 OP2) of type TYPE with location LOC,
6781 simplifying it first if possible. Returns the built
6782 expression value and appends statements possibly defining it
6783 to SEQ. */
6785 tree
6786 gimple_build (gimple_seq *seq, location_t loc,
6787 enum tree_code code, tree type, tree op0, tree op1, tree op2)
6789 tree res = gimple_simplify (code, type, op0, op1, op2,
6790 seq, gimple_build_valueize);
6791 if (!res)
6793 res = create_tmp_reg_or_ssa_name (type);
6794 gimple *stmt;
6795 if (code == BIT_FIELD_REF)
6796 stmt = gimple_build_assign (res, code,
6797 build3 (code, type, op0, op1, op2));
6798 else
6799 stmt = gimple_build_assign (res, code, op0, op1, op2);
6800 gimple_set_location (stmt, loc);
6801 gimple_seq_add_stmt_without_update (seq, stmt);
6803 return res;
6806 /* Build the call FN (ARG0) with a result of type TYPE
6807 (or no result if TYPE is void) with location LOC,
6808 simplifying it first if possible. Returns the built
6809 expression value (or NULL_TREE if TYPE is void) and appends
6810 statements possibly defining it to SEQ. */
6812 tree
6813 gimple_build (gimple_seq *seq, location_t loc,
6814 enum built_in_function fn, tree type, tree arg0)
6816 tree res = gimple_simplify (fn, type, arg0, seq, gimple_build_valueize);
6817 if (!res)
6819 tree decl = builtin_decl_implicit (fn);
6820 gimple *stmt = gimple_build_call (decl, 1, arg0);
6821 if (!VOID_TYPE_P (type))
6823 res = create_tmp_reg_or_ssa_name (type);
6824 gimple_call_set_lhs (stmt, res);
6826 gimple_set_location (stmt, loc);
6827 gimple_seq_add_stmt_without_update (seq, stmt);
6829 return res;
6832 /* Build the call FN (ARG0, ARG1) with a result of type TYPE
6833 (or no result if TYPE is void) with location LOC,
6834 simplifying it first if possible. Returns the built
6835 expression value (or NULL_TREE if TYPE is void) and appends
6836 statements possibly defining it to SEQ. */
6838 tree
6839 gimple_build (gimple_seq *seq, location_t loc,
6840 enum built_in_function fn, tree type, tree arg0, tree arg1)
6842 tree res = gimple_simplify (fn, type, arg0, arg1, seq, gimple_build_valueize);
6843 if (!res)
6845 tree decl = builtin_decl_implicit (fn);
6846 gimple *stmt = gimple_build_call (decl, 2, arg0, arg1);
6847 if (!VOID_TYPE_P (type))
6849 res = create_tmp_reg_or_ssa_name (type);
6850 gimple_call_set_lhs (stmt, res);
6852 gimple_set_location (stmt, loc);
6853 gimple_seq_add_stmt_without_update (seq, stmt);
6855 return res;
6858 /* Build the call FN (ARG0, ARG1, ARG2) with a result of type TYPE
6859 (or no result if TYPE is void) with location LOC,
6860 simplifying it first if possible. Returns the built
6861 expression value (or NULL_TREE if TYPE is void) and appends
6862 statements possibly defining it to SEQ. */
6864 tree
6865 gimple_build (gimple_seq *seq, location_t loc,
6866 enum built_in_function fn, tree type,
6867 tree arg0, tree arg1, tree arg2)
6869 tree res = gimple_simplify (fn, type, arg0, arg1, arg2,
6870 seq, gimple_build_valueize);
6871 if (!res)
6873 tree decl = builtin_decl_implicit (fn);
6874 gimple *stmt = gimple_build_call (decl, 3, arg0, arg1, arg2);
6875 if (!VOID_TYPE_P (type))
6877 res = create_tmp_reg_or_ssa_name (type);
6878 gimple_call_set_lhs (stmt, res);
6880 gimple_set_location (stmt, loc);
6881 gimple_seq_add_stmt_without_update (seq, stmt);
6883 return res;
6886 /* Build the conversion (TYPE) OP with a result of type TYPE
6887 with location LOC if such conversion is neccesary in GIMPLE,
6888 simplifying it first.
6889 Returns the built expression value and appends
6890 statements possibly defining it to SEQ. */
6892 tree
6893 gimple_convert (gimple_seq *seq, location_t loc, tree type, tree op)
6895 if (useless_type_conversion_p (type, TREE_TYPE (op)))
6896 return op;
6897 return gimple_build (seq, loc, NOP_EXPR, type, op);
6900 /* Build the conversion (ptrofftype) OP with a result of a type
6901 compatible with ptrofftype with location LOC if such conversion
6902 is neccesary in GIMPLE, simplifying it first.
6903 Returns the built expression value and appends
6904 statements possibly defining it to SEQ. */
6906 tree
6907 gimple_convert_to_ptrofftype (gimple_seq *seq, location_t loc, tree op)
6909 if (ptrofftype_p (TREE_TYPE (op)))
6910 return op;
6911 return gimple_convert (seq, loc, sizetype, op);
6914 /* Return true if the result of assignment STMT is known to be non-negative.
6915 If the return value is based on the assumption that signed overflow is
6916 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
6917 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
6919 static bool
6920 gimple_assign_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
6921 int depth)
6923 enum tree_code code = gimple_assign_rhs_code (stmt);
6924 switch (get_gimple_rhs_class (code))
6926 case GIMPLE_UNARY_RHS:
6927 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
6928 gimple_expr_type (stmt),
6929 gimple_assign_rhs1 (stmt),
6930 strict_overflow_p, depth);
6931 case GIMPLE_BINARY_RHS:
6932 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
6933 gimple_expr_type (stmt),
6934 gimple_assign_rhs1 (stmt),
6935 gimple_assign_rhs2 (stmt),
6936 strict_overflow_p, depth);
6937 case GIMPLE_TERNARY_RHS:
6938 return false;
6939 case GIMPLE_SINGLE_RHS:
6940 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
6941 strict_overflow_p, depth);
6942 case GIMPLE_INVALID_RHS:
6943 break;
6945 gcc_unreachable ();
6948 /* Return true if return value of call STMT is known to be non-negative.
6949 If the return value is based on the assumption that signed overflow is
6950 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
6951 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
6953 static bool
6954 gimple_call_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
6955 int depth)
6957 tree arg0 = gimple_call_num_args (stmt) > 0 ?
6958 gimple_call_arg (stmt, 0) : NULL_TREE;
6959 tree arg1 = gimple_call_num_args (stmt) > 1 ?
6960 gimple_call_arg (stmt, 1) : NULL_TREE;
6962 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
6963 gimple_call_combined_fn (stmt),
6964 arg0,
6965 arg1,
6966 strict_overflow_p, depth);
6969 /* Return true if return value of call STMT is known to be non-negative.
6970 If the return value is based on the assumption that signed overflow is
6971 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
6972 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
6974 static bool
6975 gimple_phi_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
6976 int depth)
6978 for (unsigned i = 0; i < gimple_phi_num_args (stmt); ++i)
6980 tree arg = gimple_phi_arg_def (stmt, i);
6981 if (!tree_single_nonnegative_warnv_p (arg, strict_overflow_p, depth + 1))
6982 return false;
6984 return true;
6987 /* Return true if STMT is known to compute a non-negative value.
6988 If the return value is based on the assumption that signed overflow is
6989 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
6990 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
6992 bool
6993 gimple_stmt_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
6994 int depth)
6996 switch (gimple_code (stmt))
6998 case GIMPLE_ASSIGN:
6999 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p,
7000 depth);
7001 case GIMPLE_CALL:
7002 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p,
7003 depth);
7004 case GIMPLE_PHI:
7005 return gimple_phi_nonnegative_warnv_p (stmt, strict_overflow_p,
7006 depth);
7007 default:
7008 return false;
7012 /* Return true if the floating-point value computed by assignment STMT
7013 is known to have an integer value. We also allow +Inf, -Inf and NaN
7014 to be considered integer values. Return false for signaling NaN.
7016 DEPTH is the current nesting depth of the query. */
7018 static bool
7019 gimple_assign_integer_valued_real_p (gimple *stmt, int depth)
7021 enum tree_code code = gimple_assign_rhs_code (stmt);
7022 switch (get_gimple_rhs_class (code))
7024 case GIMPLE_UNARY_RHS:
7025 return integer_valued_real_unary_p (gimple_assign_rhs_code (stmt),
7026 gimple_assign_rhs1 (stmt), depth);
7027 case GIMPLE_BINARY_RHS:
7028 return integer_valued_real_binary_p (gimple_assign_rhs_code (stmt),
7029 gimple_assign_rhs1 (stmt),
7030 gimple_assign_rhs2 (stmt), depth);
7031 case GIMPLE_TERNARY_RHS:
7032 return false;
7033 case GIMPLE_SINGLE_RHS:
7034 return integer_valued_real_single_p (gimple_assign_rhs1 (stmt), depth);
7035 case GIMPLE_INVALID_RHS:
7036 break;
7038 gcc_unreachable ();
7041 /* Return true if the floating-point value computed by call STMT is known
7042 to have an integer value. We also allow +Inf, -Inf and NaN to be
7043 considered integer values. Return false for signaling NaN.
7045 DEPTH is the current nesting depth of the query. */
7047 static bool
7048 gimple_call_integer_valued_real_p (gimple *stmt, int depth)
7050 tree arg0 = (gimple_call_num_args (stmt) > 0
7051 ? gimple_call_arg (stmt, 0)
7052 : NULL_TREE);
7053 tree arg1 = (gimple_call_num_args (stmt) > 1
7054 ? gimple_call_arg (stmt, 1)
7055 : NULL_TREE);
7056 return integer_valued_real_call_p (gimple_call_combined_fn (stmt),
7057 arg0, arg1, depth);
7060 /* Return true if the floating-point result of phi STMT is known to have
7061 an integer value. We also allow +Inf, -Inf and NaN to be considered
7062 integer values. Return false for signaling NaN.
7064 DEPTH is the current nesting depth of the query. */
7066 static bool
7067 gimple_phi_integer_valued_real_p (gimple *stmt, int depth)
7069 for (unsigned i = 0; i < gimple_phi_num_args (stmt); ++i)
7071 tree arg = gimple_phi_arg_def (stmt, i);
7072 if (!integer_valued_real_single_p (arg, depth + 1))
7073 return false;
7075 return true;
7078 /* Return true if the floating-point value computed by STMT is known
7079 to have an integer value. We also allow +Inf, -Inf and NaN to be
7080 considered integer values. Return false for signaling NaN.
7082 DEPTH is the current nesting depth of the query. */
7084 bool
7085 gimple_stmt_integer_valued_real_p (gimple *stmt, int depth)
7087 switch (gimple_code (stmt))
7089 case GIMPLE_ASSIGN:
7090 return gimple_assign_integer_valued_real_p (stmt, depth);
7091 case GIMPLE_CALL:
7092 return gimple_call_integer_valued_real_p (stmt, depth);
7093 case GIMPLE_PHI:
7094 return gimple_phi_integer_valued_real_p (stmt, depth);
7095 default:
7096 return false;