i386: Rewrite check for AVX512 features
[official-gcc.git] / gcc / gimple-fold.c
blobd82d0606cbeff6c20fc500007e3397e4f8b8ccfc
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"
59 #include "asan.h"
61 /* Return true when DECL can be referenced from current unit.
62 FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
63 We can get declarations that are not possible to reference for various
64 reasons:
66 1) When analyzing C++ virtual tables.
67 C++ virtual tables do have known constructors even
68 when they are keyed to other compilation unit.
69 Those tables can contain pointers to methods and vars
70 in other units. Those methods have both STATIC and EXTERNAL
71 set.
72 2) In WHOPR mode devirtualization might lead to reference
73 to method that was partitioned elsehwere.
74 In this case we have static VAR_DECL or FUNCTION_DECL
75 that has no corresponding callgraph/varpool node
76 declaring the body.
77 3) COMDAT functions referred by external vtables that
78 we devirtualize only during final compilation stage.
79 At this time we already decided that we will not output
80 the function body and thus we can't reference the symbol
81 directly. */
83 static bool
84 can_refer_decl_in_current_unit_p (tree decl, tree from_decl)
86 varpool_node *vnode;
87 struct cgraph_node *node;
88 symtab_node *snode;
90 if (DECL_ABSTRACT_P (decl))
91 return false;
93 /* We are concerned only about static/external vars and functions. */
94 if ((!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
95 || !VAR_OR_FUNCTION_DECL_P (decl))
96 return true;
98 /* Static objects can be referred only if they was not optimized out yet. */
99 if (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
101 /* Before we start optimizing unreachable code we can be sure all
102 static objects are defined. */
103 if (symtab->function_flags_ready)
104 return true;
105 snode = symtab_node::get (decl);
106 if (!snode || !snode->definition)
107 return false;
108 node = dyn_cast <cgraph_node *> (snode);
109 return !node || !node->global.inlined_to;
112 /* We will later output the initializer, so we can refer to it.
113 So we are concerned only when DECL comes from initializer of
114 external var or var that has been optimized out. */
115 if (!from_decl
116 || !VAR_P (from_decl)
117 || (!DECL_EXTERNAL (from_decl)
118 && (vnode = varpool_node::get (from_decl)) != NULL
119 && vnode->definition)
120 || (flag_ltrans
121 && (vnode = varpool_node::get (from_decl)) != NULL
122 && vnode->in_other_partition))
123 return true;
124 /* We are folding reference from external vtable. The vtable may reffer
125 to a symbol keyed to other compilation unit. The other compilation
126 unit may be in separate DSO and the symbol may be hidden. */
127 if (DECL_VISIBILITY_SPECIFIED (decl)
128 && DECL_EXTERNAL (decl)
129 && DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT
130 && (!(snode = symtab_node::get (decl)) || !snode->in_other_partition))
131 return false;
132 /* When function is public, we always can introduce new reference.
133 Exception are the COMDAT functions where introducing a direct
134 reference imply need to include function body in the curren tunit. */
135 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
136 return true;
137 /* We have COMDAT. We are going to check if we still have definition
138 or if the definition is going to be output in other partition.
139 Bypass this when gimplifying; all needed functions will be produced.
141 As observed in PR20991 for already optimized out comdat virtual functions
142 it may be tempting to not necessarily give up because the copy will be
143 output elsewhere when corresponding vtable is output.
144 This is however not possible - ABI specify that COMDATs are output in
145 units where they are used and when the other unit was compiled with LTO
146 it is possible that vtable was kept public while the function itself
147 was privatized. */
148 if (!symtab->function_flags_ready)
149 return true;
151 snode = symtab_node::get (decl);
152 if (!snode
153 || ((!snode->definition || DECL_EXTERNAL (decl))
154 && (!snode->in_other_partition
155 || (!snode->forced_by_abi && !snode->force_output))))
156 return false;
157 node = dyn_cast <cgraph_node *> (snode);
158 return !node || !node->global.inlined_to;
161 /* Create a temporary for TYPE for a statement STMT. If the current function
162 is in SSA form, a SSA name is created. Otherwise a temporary register
163 is made. */
165 tree
166 create_tmp_reg_or_ssa_name (tree type, gimple *stmt)
168 if (gimple_in_ssa_p (cfun))
169 return make_ssa_name (type, stmt);
170 else
171 return create_tmp_reg (type);
174 /* CVAL is value taken from DECL_INITIAL of variable. Try to transform it into
175 acceptable form for is_gimple_min_invariant.
176 FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL. */
178 tree
179 canonicalize_constructor_val (tree cval, tree from_decl)
181 tree orig_cval = cval;
182 STRIP_NOPS (cval);
183 if (TREE_CODE (cval) == POINTER_PLUS_EXPR
184 && TREE_CODE (TREE_OPERAND (cval, 1)) == INTEGER_CST)
186 tree ptr = TREE_OPERAND (cval, 0);
187 if (is_gimple_min_invariant (ptr))
188 cval = build1_loc (EXPR_LOCATION (cval),
189 ADDR_EXPR, TREE_TYPE (ptr),
190 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (ptr)),
191 ptr,
192 fold_convert (ptr_type_node,
193 TREE_OPERAND (cval, 1))));
195 if (TREE_CODE (cval) == ADDR_EXPR)
197 tree base = NULL_TREE;
198 if (TREE_CODE (TREE_OPERAND (cval, 0)) == COMPOUND_LITERAL_EXPR)
200 base = COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval, 0));
201 if (base)
202 TREE_OPERAND (cval, 0) = base;
204 else
205 base = get_base_address (TREE_OPERAND (cval, 0));
206 if (!base)
207 return NULL_TREE;
209 if (VAR_OR_FUNCTION_DECL_P (base)
210 && !can_refer_decl_in_current_unit_p (base, from_decl))
211 return NULL_TREE;
212 if (TREE_TYPE (base) == error_mark_node)
213 return NULL_TREE;
214 if (VAR_P (base))
215 TREE_ADDRESSABLE (base) = 1;
216 else if (TREE_CODE (base) == FUNCTION_DECL)
218 /* Make sure we create a cgraph node for functions we'll reference.
219 They can be non-existent if the reference comes from an entry
220 of an external vtable for example. */
221 cgraph_node::get_create (base);
223 /* Fixup types in global initializers. */
224 if (TREE_TYPE (TREE_TYPE (cval)) != TREE_TYPE (TREE_OPERAND (cval, 0)))
225 cval = build_fold_addr_expr (TREE_OPERAND (cval, 0));
227 if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval)))
228 cval = fold_convert (TREE_TYPE (orig_cval), cval);
229 return cval;
231 if (TREE_OVERFLOW_P (cval))
232 return drop_tree_overflow (cval);
233 return orig_cval;
236 /* If SYM is a constant variable with known value, return the value.
237 NULL_TREE is returned otherwise. */
239 tree
240 get_symbol_constant_value (tree sym)
242 tree val = ctor_for_folding (sym);
243 if (val != error_mark_node)
245 if (val)
247 val = canonicalize_constructor_val (unshare_expr (val), sym);
248 if (val && is_gimple_min_invariant (val))
249 return val;
250 else
251 return NULL_TREE;
253 /* Variables declared 'const' without an initializer
254 have zero as the initializer if they may not be
255 overridden at link or run time. */
256 if (!val
257 && is_gimple_reg_type (TREE_TYPE (sym)))
258 return build_zero_cst (TREE_TYPE (sym));
261 return NULL_TREE;
266 /* Subroutine of fold_stmt. We perform several simplifications of the
267 memory reference tree EXPR and make sure to re-gimplify them properly
268 after propagation of constant addresses. IS_LHS is true if the
269 reference is supposed to be an lvalue. */
271 static tree
272 maybe_fold_reference (tree expr, bool is_lhs)
274 tree result;
276 if ((TREE_CODE (expr) == VIEW_CONVERT_EXPR
277 || TREE_CODE (expr) == REALPART_EXPR
278 || TREE_CODE (expr) == IMAGPART_EXPR)
279 && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
280 return fold_unary_loc (EXPR_LOCATION (expr),
281 TREE_CODE (expr),
282 TREE_TYPE (expr),
283 TREE_OPERAND (expr, 0));
284 else if (TREE_CODE (expr) == BIT_FIELD_REF
285 && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
286 return fold_ternary_loc (EXPR_LOCATION (expr),
287 TREE_CODE (expr),
288 TREE_TYPE (expr),
289 TREE_OPERAND (expr, 0),
290 TREE_OPERAND (expr, 1),
291 TREE_OPERAND (expr, 2));
293 if (!is_lhs
294 && (result = fold_const_aggregate_ref (expr))
295 && is_gimple_min_invariant (result))
296 return result;
298 return NULL_TREE;
302 /* Attempt to fold an assignment statement pointed-to by SI. Returns a
303 replacement rhs for the statement or NULL_TREE if no simplification
304 could be made. It is assumed that the operands have been previously
305 folded. */
307 static tree
308 fold_gimple_assign (gimple_stmt_iterator *si)
310 gimple *stmt = gsi_stmt (*si);
311 enum tree_code subcode = gimple_assign_rhs_code (stmt);
312 location_t loc = gimple_location (stmt);
314 tree result = NULL_TREE;
316 switch (get_gimple_rhs_class (subcode))
318 case GIMPLE_SINGLE_RHS:
320 tree rhs = gimple_assign_rhs1 (stmt);
322 if (TREE_CLOBBER_P (rhs))
323 return NULL_TREE;
325 if (REFERENCE_CLASS_P (rhs))
326 return maybe_fold_reference (rhs, false);
328 else if (TREE_CODE (rhs) == OBJ_TYPE_REF)
330 tree val = OBJ_TYPE_REF_EXPR (rhs);
331 if (is_gimple_min_invariant (val))
332 return val;
333 else if (flag_devirtualize && virtual_method_call_p (rhs))
335 bool final;
336 vec <cgraph_node *>targets
337 = possible_polymorphic_call_targets (rhs, stmt, &final);
338 if (final && targets.length () <= 1 && dbg_cnt (devirt))
340 if (dump_enabled_p ())
342 location_t loc = gimple_location_safe (stmt);
343 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
344 "resolving virtual function address "
345 "reference to function %s\n",
346 targets.length () == 1
347 ? targets[0]->name ()
348 : "NULL");
350 if (targets.length () == 1)
352 val = fold_convert (TREE_TYPE (val),
353 build_fold_addr_expr_loc
354 (loc, targets[0]->decl));
355 STRIP_USELESS_TYPE_CONVERSION (val);
357 else
358 /* We can not use __builtin_unreachable here because it
359 can not have address taken. */
360 val = build_int_cst (TREE_TYPE (val), 0);
361 return val;
366 else if (TREE_CODE (rhs) == ADDR_EXPR)
368 tree ref = TREE_OPERAND (rhs, 0);
369 tree tem = maybe_fold_reference (ref, true);
370 if (tem
371 && TREE_CODE (tem) == MEM_REF
372 && integer_zerop (TREE_OPERAND (tem, 1)))
373 result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (tem, 0));
374 else if (tem)
375 result = fold_convert (TREE_TYPE (rhs),
376 build_fold_addr_expr_loc (loc, tem));
377 else if (TREE_CODE (ref) == MEM_REF
378 && integer_zerop (TREE_OPERAND (ref, 1)))
379 result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (ref, 0));
381 if (result)
383 /* Strip away useless type conversions. Both the
384 NON_LVALUE_EXPR that may have been added by fold, and
385 "useless" type conversions that might now be apparent
386 due to propagation. */
387 STRIP_USELESS_TYPE_CONVERSION (result);
389 if (result != rhs && valid_gimple_rhs_p (result))
390 return result;
394 else if (TREE_CODE (rhs) == CONSTRUCTOR
395 && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE)
397 /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */
398 unsigned i;
399 tree val;
401 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
402 if (! CONSTANT_CLASS_P (val))
403 return NULL_TREE;
405 return build_vector_from_ctor (TREE_TYPE (rhs),
406 CONSTRUCTOR_ELTS (rhs));
409 else if (DECL_P (rhs))
410 return get_symbol_constant_value (rhs);
412 break;
414 case GIMPLE_UNARY_RHS:
415 break;
417 case GIMPLE_BINARY_RHS:
418 break;
420 case GIMPLE_TERNARY_RHS:
421 result = fold_ternary_loc (loc, subcode,
422 TREE_TYPE (gimple_assign_lhs (stmt)),
423 gimple_assign_rhs1 (stmt),
424 gimple_assign_rhs2 (stmt),
425 gimple_assign_rhs3 (stmt));
427 if (result)
429 STRIP_USELESS_TYPE_CONVERSION (result);
430 if (valid_gimple_rhs_p (result))
431 return result;
433 break;
435 case GIMPLE_INVALID_RHS:
436 gcc_unreachable ();
439 return NULL_TREE;
443 /* Replace a statement at *SI_P with a sequence of statements in STMTS,
444 adjusting the replacement stmts location and virtual operands.
445 If the statement has a lhs the last stmt in the sequence is expected
446 to assign to that lhs. */
448 static void
449 gsi_replace_with_seq_vops (gimple_stmt_iterator *si_p, gimple_seq stmts)
451 gimple *stmt = gsi_stmt (*si_p);
453 if (gimple_has_location (stmt))
454 annotate_all_with_location (stmts, gimple_location (stmt));
456 /* First iterate over the replacement statements backward, assigning
457 virtual operands to their defining statements. */
458 gimple *laststore = NULL;
459 for (gimple_stmt_iterator i = gsi_last (stmts);
460 !gsi_end_p (i); gsi_prev (&i))
462 gimple *new_stmt = gsi_stmt (i);
463 if ((gimple_assign_single_p (new_stmt)
464 && !is_gimple_reg (gimple_assign_lhs (new_stmt)))
465 || (is_gimple_call (new_stmt)
466 && (gimple_call_flags (new_stmt)
467 & (ECF_NOVOPS | ECF_PURE | ECF_CONST | ECF_NORETURN)) == 0))
469 tree vdef;
470 if (!laststore)
471 vdef = gimple_vdef (stmt);
472 else
473 vdef = make_ssa_name (gimple_vop (cfun), new_stmt);
474 gimple_set_vdef (new_stmt, vdef);
475 if (vdef && TREE_CODE (vdef) == SSA_NAME)
476 SSA_NAME_DEF_STMT (vdef) = new_stmt;
477 laststore = new_stmt;
481 /* Second iterate over the statements forward, assigning virtual
482 operands to their uses. */
483 tree reaching_vuse = gimple_vuse (stmt);
484 for (gimple_stmt_iterator i = gsi_start (stmts);
485 !gsi_end_p (i); gsi_next (&i))
487 gimple *new_stmt = gsi_stmt (i);
488 /* If the new statement possibly has a VUSE, update it with exact SSA
489 name we know will reach this one. */
490 if (gimple_has_mem_ops (new_stmt))
491 gimple_set_vuse (new_stmt, reaching_vuse);
492 gimple_set_modified (new_stmt, true);
493 if (gimple_vdef (new_stmt))
494 reaching_vuse = gimple_vdef (new_stmt);
497 /* If the new sequence does not do a store release the virtual
498 definition of the original statement. */
499 if (reaching_vuse
500 && reaching_vuse == gimple_vuse (stmt))
502 tree vdef = gimple_vdef (stmt);
503 if (vdef
504 && TREE_CODE (vdef) == SSA_NAME)
506 unlink_stmt_vdef (stmt);
507 release_ssa_name (vdef);
511 /* Finally replace the original statement with the sequence. */
512 gsi_replace_with_seq (si_p, stmts, false);
515 /* Convert EXPR into a GIMPLE value suitable for substitution on the
516 RHS of an assignment. Insert the necessary statements before
517 iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL
518 is replaced. If the call is expected to produces a result, then it
519 is replaced by an assignment of the new RHS to the result variable.
520 If the result is to be ignored, then the call is replaced by a
521 GIMPLE_NOP. A proper VDEF chain is retained by making the first
522 VUSE and the last VDEF of the whole sequence be the same as the replaced
523 statement and using new SSA names for stores in between. */
525 void
526 gimplify_and_update_call_from_tree (gimple_stmt_iterator *si_p, tree expr)
528 tree lhs;
529 gimple *stmt, *new_stmt;
530 gimple_stmt_iterator i;
531 gimple_seq stmts = NULL;
533 stmt = gsi_stmt (*si_p);
535 gcc_assert (is_gimple_call (stmt));
537 push_gimplify_context (gimple_in_ssa_p (cfun));
539 lhs = gimple_call_lhs (stmt);
540 if (lhs == NULL_TREE)
542 gimplify_and_add (expr, &stmts);
543 /* We can end up with folding a memcpy of an empty class assignment
544 which gets optimized away by C++ gimplification. */
545 if (gimple_seq_empty_p (stmts))
547 pop_gimplify_context (NULL);
548 if (gimple_in_ssa_p (cfun))
550 unlink_stmt_vdef (stmt);
551 release_defs (stmt);
553 gsi_replace (si_p, gimple_build_nop (), false);
554 return;
557 else
559 tree tmp = force_gimple_operand (expr, &stmts, false, NULL_TREE);
560 new_stmt = gimple_build_assign (lhs, tmp);
561 i = gsi_last (stmts);
562 gsi_insert_after_without_update (&i, new_stmt,
563 GSI_CONTINUE_LINKING);
566 pop_gimplify_context (NULL);
568 gsi_replace_with_seq_vops (si_p, stmts);
572 /* Replace the call at *GSI with the gimple value VAL. */
574 void
575 replace_call_with_value (gimple_stmt_iterator *gsi, tree val)
577 gimple *stmt = gsi_stmt (*gsi);
578 tree lhs = gimple_call_lhs (stmt);
579 gimple *repl;
580 if (lhs)
582 if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (val)))
583 val = fold_convert (TREE_TYPE (lhs), val);
584 repl = gimple_build_assign (lhs, val);
586 else
587 repl = gimple_build_nop ();
588 tree vdef = gimple_vdef (stmt);
589 if (vdef && TREE_CODE (vdef) == SSA_NAME)
591 unlink_stmt_vdef (stmt);
592 release_ssa_name (vdef);
594 gsi_replace (gsi, repl, false);
597 /* Replace the call at *GSI with the new call REPL and fold that
598 again. */
600 static void
601 replace_call_with_call_and_fold (gimple_stmt_iterator *gsi, gimple *repl)
603 gimple *stmt = gsi_stmt (*gsi);
604 gimple_call_set_lhs (repl, gimple_call_lhs (stmt));
605 gimple_set_location (repl, gimple_location (stmt));
606 if (gimple_vdef (stmt)
607 && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
609 gimple_set_vdef (repl, gimple_vdef (stmt));
610 SSA_NAME_DEF_STMT (gimple_vdef (repl)) = repl;
612 if (gimple_vuse (stmt))
613 gimple_set_vuse (repl, gimple_vuse (stmt));
614 gsi_replace (gsi, repl, false);
615 fold_stmt (gsi);
618 /* Return true if VAR is a VAR_DECL or a component thereof. */
620 static bool
621 var_decl_component_p (tree var)
623 tree inner = var;
624 while (handled_component_p (inner))
625 inner = TREE_OPERAND (inner, 0);
626 return SSA_VAR_P (inner);
629 /* Fold function call to builtin mem{{,p}cpy,move}. Return
630 false if no simplification can be made.
631 If ENDP is 0, return DEST (like memcpy).
632 If ENDP is 1, return DEST+LEN (like mempcpy).
633 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
634 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
635 (memmove). */
637 static bool
638 gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi,
639 tree dest, tree src, int endp)
641 gimple *stmt = gsi_stmt (*gsi);
642 tree lhs = gimple_call_lhs (stmt);
643 tree len = gimple_call_arg (stmt, 2);
644 tree destvar, srcvar;
645 location_t loc = gimple_location (stmt);
647 /* If the LEN parameter is zero, return DEST. */
648 if (integer_zerop (len))
650 gimple *repl;
651 if (gimple_call_lhs (stmt))
652 repl = gimple_build_assign (gimple_call_lhs (stmt), dest);
653 else
654 repl = gimple_build_nop ();
655 tree vdef = gimple_vdef (stmt);
656 if (vdef && TREE_CODE (vdef) == SSA_NAME)
658 unlink_stmt_vdef (stmt);
659 release_ssa_name (vdef);
661 gsi_replace (gsi, repl, false);
662 return true;
665 /* If SRC and DEST are the same (and not volatile), return
666 DEST{,+LEN,+LEN-1}. */
667 if (operand_equal_p (src, dest, 0))
669 unlink_stmt_vdef (stmt);
670 if (gimple_vdef (stmt) && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
671 release_ssa_name (gimple_vdef (stmt));
672 if (!lhs)
674 gsi_replace (gsi, gimple_build_nop (), false);
675 return true;
677 goto done;
679 else
681 tree srctype, desttype;
682 unsigned int src_align, dest_align;
683 tree off0;
685 /* Inlining of memcpy/memmove may cause bounds lost (if we copy
686 pointers as wide integer) and also may result in huge function
687 size because of inlined bounds copy. Thus don't inline for
688 functions we want to instrument. */
689 if (flag_check_pointer_bounds
690 && chkp_instrumentable_p (cfun->decl)
691 /* Even if data may contain pointers we can inline if copy
692 less than a pointer size. */
693 && (!tree_fits_uhwi_p (len)
694 || compare_tree_int (len, POINTER_SIZE_UNITS) >= 0))
695 return false;
697 /* Build accesses at offset zero with a ref-all character type. */
698 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
699 ptr_mode, true), 0);
701 /* If we can perform the copy efficiently with first doing all loads
702 and then all stores inline it that way. Currently efficiently
703 means that we can load all the memory into a single integer
704 register which is what MOVE_MAX gives us. */
705 src_align = get_pointer_alignment (src);
706 dest_align = get_pointer_alignment (dest);
707 if (tree_fits_uhwi_p (len)
708 && compare_tree_int (len, MOVE_MAX) <= 0
709 /* ??? Don't transform copies from strings with known length this
710 confuses the tree-ssa-strlen.c. This doesn't handle
711 the case in gcc.dg/strlenopt-8.c which is XFAILed for that
712 reason. */
713 && !c_strlen (src, 2))
715 unsigned ilen = tree_to_uhwi (len);
716 if (pow2p_hwi (ilen))
718 tree type = lang_hooks.types.type_for_size (ilen * 8, 1);
719 if (type
720 && TYPE_MODE (type) != BLKmode
721 && (GET_MODE_SIZE (TYPE_MODE (type)) * BITS_PER_UNIT
722 == ilen * 8)
723 /* If the destination pointer is not aligned we must be able
724 to emit an unaligned store. */
725 && (dest_align >= GET_MODE_ALIGNMENT (TYPE_MODE (type))
726 || !SLOW_UNALIGNED_ACCESS (TYPE_MODE (type), dest_align)
727 || (optab_handler (movmisalign_optab, TYPE_MODE (type))
728 != CODE_FOR_nothing)))
730 tree srctype = type;
731 tree desttype = type;
732 if (src_align < GET_MODE_ALIGNMENT (TYPE_MODE (type)))
733 srctype = build_aligned_type (type, src_align);
734 tree srcmem = fold_build2 (MEM_REF, srctype, src, off0);
735 tree tem = fold_const_aggregate_ref (srcmem);
736 if (tem)
737 srcmem = tem;
738 else if (src_align < GET_MODE_ALIGNMENT (TYPE_MODE (type))
739 && SLOW_UNALIGNED_ACCESS (TYPE_MODE (type),
740 src_align)
741 && (optab_handler (movmisalign_optab,
742 TYPE_MODE (type))
743 == CODE_FOR_nothing))
744 srcmem = NULL_TREE;
745 if (srcmem)
747 gimple *new_stmt;
748 if (is_gimple_reg_type (TREE_TYPE (srcmem)))
750 new_stmt = gimple_build_assign (NULL_TREE, srcmem);
751 srcmem
752 = create_tmp_reg_or_ssa_name (TREE_TYPE (srcmem),
753 new_stmt);
754 gimple_assign_set_lhs (new_stmt, srcmem);
755 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
756 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
758 if (dest_align < GET_MODE_ALIGNMENT (TYPE_MODE (type)))
759 desttype = build_aligned_type (type, dest_align);
760 new_stmt
761 = gimple_build_assign (fold_build2 (MEM_REF, desttype,
762 dest, off0),
763 srcmem);
764 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
765 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
766 if (gimple_vdef (new_stmt)
767 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
768 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
769 if (!lhs)
771 gsi_replace (gsi, new_stmt, false);
772 return true;
774 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
775 goto done;
781 if (endp == 3)
783 /* Both DEST and SRC must be pointer types.
784 ??? This is what old code did. Is the testing for pointer types
785 really mandatory?
787 If either SRC is readonly or length is 1, we can use memcpy. */
788 if (!dest_align || !src_align)
789 return false;
790 if (readonly_data_expr (src)
791 || (tree_fits_uhwi_p (len)
792 && (MIN (src_align, dest_align) / BITS_PER_UNIT
793 >= tree_to_uhwi (len))))
795 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
796 if (!fn)
797 return false;
798 gimple_call_set_fndecl (stmt, fn);
799 gimple_call_set_arg (stmt, 0, dest);
800 gimple_call_set_arg (stmt, 1, src);
801 fold_stmt (gsi);
802 return true;
805 /* If *src and *dest can't overlap, optimize into memcpy as well. */
806 if (TREE_CODE (src) == ADDR_EXPR
807 && TREE_CODE (dest) == ADDR_EXPR)
809 tree src_base, dest_base, fn;
810 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
811 HOST_WIDE_INT maxsize;
813 srcvar = TREE_OPERAND (src, 0);
814 src_base = get_addr_base_and_unit_offset (srcvar, &src_offset);
815 if (src_base == NULL)
816 src_base = srcvar;
817 destvar = TREE_OPERAND (dest, 0);
818 dest_base = get_addr_base_and_unit_offset (destvar,
819 &dest_offset);
820 if (dest_base == NULL)
821 dest_base = destvar;
822 if (tree_fits_uhwi_p (len))
823 maxsize = tree_to_uhwi (len);
824 else
825 maxsize = -1;
826 if (SSA_VAR_P (src_base)
827 && SSA_VAR_P (dest_base))
829 if (operand_equal_p (src_base, dest_base, 0)
830 && ranges_overlap_p (src_offset, maxsize,
831 dest_offset, maxsize))
832 return false;
834 else if (TREE_CODE (src_base) == MEM_REF
835 && TREE_CODE (dest_base) == MEM_REF)
837 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
838 TREE_OPERAND (dest_base, 0), 0))
839 return false;
840 offset_int off = mem_ref_offset (src_base) + src_offset;
841 if (!wi::fits_shwi_p (off))
842 return false;
843 src_offset = off.to_shwi ();
845 off = mem_ref_offset (dest_base) + dest_offset;
846 if (!wi::fits_shwi_p (off))
847 return false;
848 dest_offset = off.to_shwi ();
849 if (ranges_overlap_p (src_offset, maxsize,
850 dest_offset, maxsize))
851 return false;
853 else
854 return false;
856 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
857 if (!fn)
858 return false;
859 gimple_call_set_fndecl (stmt, fn);
860 gimple_call_set_arg (stmt, 0, dest);
861 gimple_call_set_arg (stmt, 1, src);
862 fold_stmt (gsi);
863 return true;
866 /* If the destination and source do not alias optimize into
867 memcpy as well. */
868 if ((is_gimple_min_invariant (dest)
869 || TREE_CODE (dest) == SSA_NAME)
870 && (is_gimple_min_invariant (src)
871 || TREE_CODE (src) == SSA_NAME))
873 ao_ref destr, srcr;
874 ao_ref_init_from_ptr_and_size (&destr, dest, len);
875 ao_ref_init_from_ptr_and_size (&srcr, src, len);
876 if (!refs_may_alias_p_1 (&destr, &srcr, false))
878 tree fn;
879 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
880 if (!fn)
881 return false;
882 gimple_call_set_fndecl (stmt, fn);
883 gimple_call_set_arg (stmt, 0, dest);
884 gimple_call_set_arg (stmt, 1, src);
885 fold_stmt (gsi);
886 return true;
890 return false;
893 if (!tree_fits_shwi_p (len))
894 return false;
895 /* FIXME:
896 This logic lose for arguments like (type *)malloc (sizeof (type)),
897 since we strip the casts of up to VOID return value from malloc.
898 Perhaps we ought to inherit type from non-VOID argument here? */
899 STRIP_NOPS (src);
900 STRIP_NOPS (dest);
901 if (!POINTER_TYPE_P (TREE_TYPE (src))
902 || !POINTER_TYPE_P (TREE_TYPE (dest)))
903 return false;
904 /* In the following try to find a type that is most natural to be
905 used for the memcpy source and destination and that allows
906 the most optimization when memcpy is turned into a plain assignment
907 using that type. In theory we could always use a char[len] type
908 but that only gains us that the destination and source possibly
909 no longer will have their address taken. */
910 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
911 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
913 tree tem = TREE_OPERAND (src, 0);
914 STRIP_NOPS (tem);
915 if (tem != TREE_OPERAND (src, 0))
916 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
918 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
920 tree tem = TREE_OPERAND (dest, 0);
921 STRIP_NOPS (tem);
922 if (tem != TREE_OPERAND (dest, 0))
923 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
925 srctype = TREE_TYPE (TREE_TYPE (src));
926 if (TREE_CODE (srctype) == ARRAY_TYPE
927 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
929 srctype = TREE_TYPE (srctype);
930 STRIP_NOPS (src);
931 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
933 desttype = TREE_TYPE (TREE_TYPE (dest));
934 if (TREE_CODE (desttype) == ARRAY_TYPE
935 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
937 desttype = TREE_TYPE (desttype);
938 STRIP_NOPS (dest);
939 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
941 if (TREE_ADDRESSABLE (srctype)
942 || TREE_ADDRESSABLE (desttype))
943 return false;
945 /* Make sure we are not copying using a floating-point mode or
946 a type whose size possibly does not match its precision. */
947 if (FLOAT_MODE_P (TYPE_MODE (desttype))
948 || TREE_CODE (desttype) == BOOLEAN_TYPE
949 || TREE_CODE (desttype) == ENUMERAL_TYPE)
950 desttype = bitwise_type_for_mode (TYPE_MODE (desttype));
951 if (FLOAT_MODE_P (TYPE_MODE (srctype))
952 || TREE_CODE (srctype) == BOOLEAN_TYPE
953 || TREE_CODE (srctype) == ENUMERAL_TYPE)
954 srctype = bitwise_type_for_mode (TYPE_MODE (srctype));
955 if (!srctype)
956 srctype = desttype;
957 if (!desttype)
958 desttype = srctype;
959 if (!srctype)
960 return false;
962 src_align = get_pointer_alignment (src);
963 dest_align = get_pointer_alignment (dest);
964 if (dest_align < TYPE_ALIGN (desttype)
965 || src_align < TYPE_ALIGN (srctype))
966 return false;
968 destvar = dest;
969 STRIP_NOPS (destvar);
970 if (TREE_CODE (destvar) == ADDR_EXPR
971 && var_decl_component_p (TREE_OPERAND (destvar, 0))
972 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
973 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
974 else
975 destvar = NULL_TREE;
977 srcvar = src;
978 STRIP_NOPS (srcvar);
979 if (TREE_CODE (srcvar) == ADDR_EXPR
980 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
981 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
983 if (!destvar
984 || src_align >= TYPE_ALIGN (desttype))
985 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
986 srcvar, off0);
987 else if (!STRICT_ALIGNMENT)
989 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
990 src_align);
991 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
993 else
994 srcvar = NULL_TREE;
996 else
997 srcvar = NULL_TREE;
999 if (srcvar == NULL_TREE && destvar == NULL_TREE)
1000 return false;
1002 if (srcvar == NULL_TREE)
1004 STRIP_NOPS (src);
1005 if (src_align >= TYPE_ALIGN (desttype))
1006 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
1007 else
1009 if (STRICT_ALIGNMENT)
1010 return false;
1011 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
1012 src_align);
1013 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
1016 else if (destvar == NULL_TREE)
1018 STRIP_NOPS (dest);
1019 if (dest_align >= TYPE_ALIGN (srctype))
1020 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
1021 else
1023 if (STRICT_ALIGNMENT)
1024 return false;
1025 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
1026 dest_align);
1027 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
1031 gimple *new_stmt;
1032 if (is_gimple_reg_type (TREE_TYPE (srcvar)))
1034 tree tem = fold_const_aggregate_ref (srcvar);
1035 if (tem)
1036 srcvar = tem;
1037 if (! is_gimple_min_invariant (srcvar))
1039 new_stmt = gimple_build_assign (NULL_TREE, srcvar);
1040 srcvar = create_tmp_reg_or_ssa_name (TREE_TYPE (srcvar),
1041 new_stmt);
1042 gimple_assign_set_lhs (new_stmt, srcvar);
1043 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
1044 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1047 new_stmt = gimple_build_assign (destvar, srcvar);
1048 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
1049 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
1050 if (gimple_vdef (new_stmt)
1051 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1052 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1053 if (!lhs)
1055 gsi_replace (gsi, new_stmt, false);
1056 return true;
1058 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1061 done:
1062 gimple_seq stmts = NULL;
1063 if (endp == 0 || endp == 3)
1064 len = NULL_TREE;
1065 else if (endp == 2)
1066 len = gimple_build (&stmts, loc, MINUS_EXPR, TREE_TYPE (len), len,
1067 ssize_int (1));
1068 if (endp == 2 || endp == 1)
1070 len = gimple_convert_to_ptrofftype (&stmts, loc, len);
1071 dest = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
1072 TREE_TYPE (dest), dest, len);
1075 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1076 gimple *repl = gimple_build_assign (lhs, dest);
1077 gsi_replace (gsi, repl, false);
1078 return true;
1081 /* Transform a call to built-in bcmp(a, b, len) at *GSI into one
1082 to built-in memcmp (a, b, len). */
1084 static bool
1085 gimple_fold_builtin_bcmp (gimple_stmt_iterator *gsi)
1087 tree fn = builtin_decl_implicit (BUILT_IN_MEMCMP);
1089 if (!fn)
1090 return false;
1092 /* Transform bcmp (a, b, len) into memcmp (a, b, len). */
1094 gimple *stmt = gsi_stmt (*gsi);
1095 tree a = gimple_call_arg (stmt, 0);
1096 tree b = gimple_call_arg (stmt, 1);
1097 tree len = gimple_call_arg (stmt, 2);
1099 gimple *repl = gimple_build_call (fn, 3, a, b, len);
1100 replace_call_with_call_and_fold (gsi, repl);
1102 return true;
1105 /* Transform a call to built-in bcopy (src, dest, len) at *GSI into one
1106 to built-in memmove (dest, src, len). */
1108 static bool
1109 gimple_fold_builtin_bcopy (gimple_stmt_iterator *gsi)
1111 tree fn = builtin_decl_implicit (BUILT_IN_MEMMOVE);
1113 if (!fn)
1114 return false;
1116 /* bcopy has been removed from POSIX in Issue 7 but Issue 6 specifies
1117 it's quivalent to memmove (not memcpy). Transform bcopy (src, dest,
1118 len) into memmove (dest, src, len). */
1120 gimple *stmt = gsi_stmt (*gsi);
1121 tree src = gimple_call_arg (stmt, 0);
1122 tree dest = gimple_call_arg (stmt, 1);
1123 tree len = gimple_call_arg (stmt, 2);
1125 gimple *repl = gimple_build_call (fn, 3, dest, src, len);
1126 gimple_call_set_fntype (as_a <gcall *> (stmt), TREE_TYPE (fn));
1127 replace_call_with_call_and_fold (gsi, repl);
1129 return true;
1132 /* Transform a call to built-in bzero (dest, len) at *GSI into one
1133 to built-in memset (dest, 0, len). */
1135 static bool
1136 gimple_fold_builtin_bzero (gimple_stmt_iterator *gsi)
1138 tree fn = builtin_decl_implicit (BUILT_IN_MEMSET);
1140 if (!fn)
1141 return false;
1143 /* Transform bzero (dest, len) into memset (dest, 0, len). */
1145 gimple *stmt = gsi_stmt (*gsi);
1146 tree dest = gimple_call_arg (stmt, 0);
1147 tree len = gimple_call_arg (stmt, 1);
1149 gimple_seq seq = NULL;
1150 gimple *repl = gimple_build_call (fn, 3, dest, integer_zero_node, len);
1151 gimple_seq_add_stmt_without_update (&seq, repl);
1152 gsi_replace_with_seq_vops (gsi, seq);
1153 fold_stmt (gsi);
1155 return true;
1158 /* Fold function call to builtin memset or bzero at *GSI setting the
1159 memory of size LEN to VAL. Return whether a simplification was made. */
1161 static bool
1162 gimple_fold_builtin_memset (gimple_stmt_iterator *gsi, tree c, tree len)
1164 gimple *stmt = gsi_stmt (*gsi);
1165 tree etype;
1166 unsigned HOST_WIDE_INT length, cval;
1168 /* If the LEN parameter is zero, return DEST. */
1169 if (integer_zerop (len))
1171 replace_call_with_value (gsi, gimple_call_arg (stmt, 0));
1172 return true;
1175 if (! tree_fits_uhwi_p (len))
1176 return false;
1178 if (TREE_CODE (c) != INTEGER_CST)
1179 return false;
1181 tree dest = gimple_call_arg (stmt, 0);
1182 tree var = dest;
1183 if (TREE_CODE (var) != ADDR_EXPR)
1184 return false;
1186 var = TREE_OPERAND (var, 0);
1187 if (TREE_THIS_VOLATILE (var))
1188 return false;
1190 etype = TREE_TYPE (var);
1191 if (TREE_CODE (etype) == ARRAY_TYPE)
1192 etype = TREE_TYPE (etype);
1194 if (!INTEGRAL_TYPE_P (etype)
1195 && !POINTER_TYPE_P (etype))
1196 return NULL_TREE;
1198 if (! var_decl_component_p (var))
1199 return NULL_TREE;
1201 length = tree_to_uhwi (len);
1202 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
1203 || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
1204 return NULL_TREE;
1206 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
1207 return NULL_TREE;
1209 if (integer_zerop (c))
1210 cval = 0;
1211 else
1213 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
1214 return NULL_TREE;
1216 cval = TREE_INT_CST_LOW (c);
1217 cval &= 0xff;
1218 cval |= cval << 8;
1219 cval |= cval << 16;
1220 cval |= (cval << 31) << 1;
1223 var = fold_build2 (MEM_REF, etype, dest, build_int_cst (ptr_type_node, 0));
1224 gimple *store = gimple_build_assign (var, build_int_cst_type (etype, cval));
1225 gimple_set_vuse (store, gimple_vuse (stmt));
1226 tree vdef = gimple_vdef (stmt);
1227 if (vdef && TREE_CODE (vdef) == SSA_NAME)
1229 gimple_set_vdef (store, gimple_vdef (stmt));
1230 SSA_NAME_DEF_STMT (gimple_vdef (stmt)) = store;
1232 gsi_insert_before (gsi, store, GSI_SAME_STMT);
1233 if (gimple_call_lhs (stmt))
1235 gimple *asgn = gimple_build_assign (gimple_call_lhs (stmt), dest);
1236 gsi_replace (gsi, asgn, false);
1238 else
1240 gimple_stmt_iterator gsi2 = *gsi;
1241 gsi_prev (gsi);
1242 gsi_remove (&gsi2, true);
1245 return true;
1249 /* Obtain the minimum and maximum string length or minimum and maximum
1250 value of ARG in LENGTH[0] and LENGTH[1], respectively.
1251 If ARG is an SSA name variable, follow its use-def chains. When
1252 TYPE == 0, if LENGTH[1] is not equal to the length we determine or
1253 if we are unable to determine the length or value, return False.
1254 VISITED is a bitmap of visited variables.
1255 TYPE is 0 if string length should be obtained, 1 for maximum string
1256 length and 2 for maximum value ARG can have.
1257 When FUZZY is set and the length of a string cannot be determined,
1258 the function instead considers as the maximum possible length the
1259 size of a character array it may refer to.
1260 Set *FLEXP to true if the range of the string lengths has been
1261 obtained from the upper bound of an array at the end of a struct.
1262 Such an array may hold a string that's longer than its upper bound
1263 due to it being used as a poor-man's flexible array member. */
1265 static bool
1266 get_range_strlen (tree arg, tree length[2], bitmap *visited, int type,
1267 bool fuzzy, bool *flexp)
1269 tree var, val;
1270 gimple *def_stmt;
1272 /* The minimum and maximum length. The MAXLEN pointer stays unchanged
1273 but MINLEN may be cleared during the execution of the function. */
1274 tree *minlen = length;
1275 tree *const maxlen = length + 1;
1277 if (TREE_CODE (arg) != SSA_NAME)
1279 /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */
1280 if (TREE_CODE (arg) == ADDR_EXPR
1281 && TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF
1282 && integer_zerop (TREE_OPERAND (TREE_OPERAND (arg, 0), 1)))
1284 tree aop0 = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
1285 if (TREE_CODE (aop0) == INDIRECT_REF
1286 && TREE_CODE (TREE_OPERAND (aop0, 0)) == SSA_NAME)
1287 return get_range_strlen (TREE_OPERAND (aop0, 0),
1288 length, visited, type, fuzzy, flexp);
1291 if (type == 2)
1293 val = arg;
1294 if (TREE_CODE (val) != INTEGER_CST
1295 || tree_int_cst_sgn (val) < 0)
1296 return false;
1298 else
1299 val = c_strlen (arg, 1);
1301 if (!val && fuzzy)
1303 if (TREE_CODE (arg) == ADDR_EXPR)
1304 return get_range_strlen (TREE_OPERAND (arg, 0), length,
1305 visited, type, fuzzy, flexp);
1307 if (TREE_CODE (arg) == COMPONENT_REF
1308 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 1))) == ARRAY_TYPE)
1310 /* Use the type of the member array to determine the upper
1311 bound on the length of the array. This may be overly
1312 optimistic if the array itself isn't NUL-terminated and
1313 the caller relies on the subsequent member to contain
1314 the NUL.
1315 Set *FLEXP to true if the array whose bound is being
1316 used is at the end of a struct. */
1317 if (array_at_struct_end_p (arg))
1318 *flexp = true;
1320 arg = TREE_OPERAND (arg, 1);
1321 val = TYPE_SIZE_UNIT (TREE_TYPE (arg));
1322 if (!val || integer_zerop (val))
1323 return false;
1324 val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val,
1325 integer_one_node);
1326 /* Set the minimum size to zero since the string in
1327 the array could have zero length. */
1328 *minlen = ssize_int (0);
1332 if (!val)
1333 return false;
1335 if (minlen
1336 && (!*minlen
1337 || (type > 0
1338 && TREE_CODE (*minlen) == INTEGER_CST
1339 && TREE_CODE (val) == INTEGER_CST
1340 && tree_int_cst_lt (val, *minlen))))
1341 *minlen = val;
1343 if (*maxlen)
1345 if (type > 0)
1347 if (TREE_CODE (*maxlen) != INTEGER_CST
1348 || TREE_CODE (val) != INTEGER_CST)
1349 return false;
1351 if (tree_int_cst_lt (*maxlen, val))
1352 *maxlen = val;
1353 return true;
1355 else if (simple_cst_equal (val, *maxlen) != 1)
1356 return false;
1359 *maxlen = val;
1360 return true;
1363 /* If ARG is registered for SSA update we cannot look at its defining
1364 statement. */
1365 if (name_registered_for_update_p (arg))
1366 return false;
1368 /* If we were already here, break the infinite cycle. */
1369 if (!*visited)
1370 *visited = BITMAP_ALLOC (NULL);
1371 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (arg)))
1372 return true;
1374 var = arg;
1375 def_stmt = SSA_NAME_DEF_STMT (var);
1377 switch (gimple_code (def_stmt))
1379 case GIMPLE_ASSIGN:
1380 /* The RHS of the statement defining VAR must either have a
1381 constant length or come from another SSA_NAME with a constant
1382 length. */
1383 if (gimple_assign_single_p (def_stmt)
1384 || gimple_assign_unary_nop_p (def_stmt))
1386 tree rhs = gimple_assign_rhs1 (def_stmt);
1387 return get_range_strlen (rhs, length, visited, type, fuzzy, flexp);
1389 else if (gimple_assign_rhs_code (def_stmt) == COND_EXPR)
1391 tree op2 = gimple_assign_rhs2 (def_stmt);
1392 tree op3 = gimple_assign_rhs3 (def_stmt);
1393 return get_range_strlen (op2, length, visited, type, fuzzy, flexp)
1394 && get_range_strlen (op3, length, visited, type, fuzzy, flexp);
1396 return false;
1398 case GIMPLE_PHI:
1400 /* All the arguments of the PHI node must have the same constant
1401 length. */
1402 unsigned i;
1404 for (i = 0; i < gimple_phi_num_args (def_stmt); i++)
1406 tree arg = gimple_phi_arg (def_stmt, i)->def;
1408 /* If this PHI has itself as an argument, we cannot
1409 determine the string length of this argument. However,
1410 if we can find a constant string length for the other
1411 PHI args then we can still be sure that this is a
1412 constant string length. So be optimistic and just
1413 continue with the next argument. */
1414 if (arg == gimple_phi_result (def_stmt))
1415 continue;
1417 if (!get_range_strlen (arg, length, visited, type, fuzzy, flexp))
1419 if (fuzzy)
1420 *maxlen = build_all_ones_cst (size_type_node);
1421 else
1422 return false;
1426 return true;
1428 default:
1429 return false;
1433 /* Determine the minimum and maximum value or string length that ARG
1434 refers to and store each in the first two elements of MINMAXLEN.
1435 For expressions that point to strings of unknown lengths that are
1436 character arrays, use the upper bound of the array as the maximum
1437 length. For example, given an expression like 'x ? array : "xyz"'
1438 and array declared as 'char array[8]', MINMAXLEN[0] will be set
1439 to 3 and MINMAXLEN[1] to 7, the longest string that could be
1440 stored in array.
1441 Return true if the range of the string lengths has been obtained
1442 from the upper bound of an array at the end of a struct. Such
1443 an array may hold a string that's longer than its upper bound
1444 due to it being used as a poor-man's flexible array member. */
1446 bool
1447 get_range_strlen (tree arg, tree minmaxlen[2])
1449 bitmap visited = NULL;
1451 minmaxlen[0] = NULL_TREE;
1452 minmaxlen[1] = NULL_TREE;
1454 bool flexarray = false;
1455 get_range_strlen (arg, minmaxlen, &visited, 1, true, &flexarray);
1457 if (visited)
1458 BITMAP_FREE (visited);
1460 return flexarray;
1463 tree
1464 get_maxval_strlen (tree arg, int type)
1466 bitmap visited = NULL;
1467 tree len[2] = { NULL_TREE, NULL_TREE };
1469 bool dummy;
1470 if (!get_range_strlen (arg, len, &visited, type, false, &dummy))
1471 len[1] = NULL_TREE;
1472 if (visited)
1473 BITMAP_FREE (visited);
1475 return len[1];
1479 /* Fold function call to builtin strcpy with arguments DEST and SRC.
1480 If LEN is not NULL, it represents the length of the string to be
1481 copied. Return NULL_TREE if no simplification can be made. */
1483 static bool
1484 gimple_fold_builtin_strcpy (gimple_stmt_iterator *gsi,
1485 tree dest, tree src)
1487 location_t loc = gimple_location (gsi_stmt (*gsi));
1488 tree fn;
1490 /* If SRC and DEST are the same (and not volatile), return DEST. */
1491 if (operand_equal_p (src, dest, 0))
1493 replace_call_with_value (gsi, dest);
1494 return true;
1497 if (optimize_function_for_size_p (cfun))
1498 return false;
1500 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1501 if (!fn)
1502 return false;
1504 tree len = get_maxval_strlen (src, 0);
1505 if (!len)
1506 return false;
1508 len = fold_convert_loc (loc, size_type_node, len);
1509 len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
1510 len = force_gimple_operand_gsi (gsi, len, true,
1511 NULL_TREE, true, GSI_SAME_STMT);
1512 gimple *repl = gimple_build_call (fn, 3, dest, src, len);
1513 replace_call_with_call_and_fold (gsi, repl);
1514 return true;
1517 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
1518 If SLEN is not NULL, it represents the length of the source string.
1519 Return NULL_TREE if no simplification can be made. */
1521 static bool
1522 gimple_fold_builtin_strncpy (gimple_stmt_iterator *gsi,
1523 tree dest, tree src, tree len)
1525 location_t loc = gimple_location (gsi_stmt (*gsi));
1526 tree fn;
1528 /* If the LEN parameter is zero, return DEST. */
1529 if (integer_zerop (len))
1531 replace_call_with_value (gsi, dest);
1532 return true;
1535 /* We can't compare slen with len as constants below if len is not a
1536 constant. */
1537 if (TREE_CODE (len) != INTEGER_CST)
1538 return false;
1540 /* Now, we must be passed a constant src ptr parameter. */
1541 tree slen = get_maxval_strlen (src, 0);
1542 if (!slen || TREE_CODE (slen) != INTEGER_CST)
1543 return false;
1545 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
1547 /* We do not support simplification of this case, though we do
1548 support it when expanding trees into RTL. */
1549 /* FIXME: generate a call to __builtin_memset. */
1550 if (tree_int_cst_lt (slen, len))
1551 return false;
1553 /* OK transform into builtin memcpy. */
1554 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1555 if (!fn)
1556 return false;
1558 len = fold_convert_loc (loc, size_type_node, len);
1559 len = force_gimple_operand_gsi (gsi, len, true,
1560 NULL_TREE, true, GSI_SAME_STMT);
1561 gimple *repl = gimple_build_call (fn, 3, dest, src, len);
1562 replace_call_with_call_and_fold (gsi, repl);
1563 return true;
1566 /* Fold function call to builtin strchr or strrchr.
1567 If both arguments are constant, evaluate and fold the result,
1568 otherwise simplify str(r)chr (str, 0) into str + strlen (str).
1569 In general strlen is significantly faster than strchr
1570 due to being a simpler operation. */
1571 static bool
1572 gimple_fold_builtin_strchr (gimple_stmt_iterator *gsi, bool is_strrchr)
1574 gimple *stmt = gsi_stmt (*gsi);
1575 tree str = gimple_call_arg (stmt, 0);
1576 tree c = gimple_call_arg (stmt, 1);
1577 location_t loc = gimple_location (stmt);
1578 const char *p;
1579 char ch;
1581 if (!gimple_call_lhs (stmt))
1582 return false;
1584 if ((p = c_getstr (str)) && target_char_cst_p (c, &ch))
1586 const char *p1 = is_strrchr ? strrchr (p, ch) : strchr (p, ch);
1588 if (p1 == NULL)
1590 replace_call_with_value (gsi, integer_zero_node);
1591 return true;
1594 tree len = build_int_cst (size_type_node, p1 - p);
1595 gimple_seq stmts = NULL;
1596 gimple *new_stmt = gimple_build_assign (gimple_call_lhs (stmt),
1597 POINTER_PLUS_EXPR, str, len);
1598 gimple_seq_add_stmt_without_update (&stmts, new_stmt);
1599 gsi_replace_with_seq_vops (gsi, stmts);
1600 return true;
1603 if (!integer_zerop (c))
1604 return false;
1606 /* Transform strrchr (s, 0) to strchr (s, 0) when optimizing for size. */
1607 if (is_strrchr && optimize_function_for_size_p (cfun))
1609 tree strchr_fn = builtin_decl_implicit (BUILT_IN_STRCHR);
1611 if (strchr_fn)
1613 gimple *repl = gimple_build_call (strchr_fn, 2, str, c);
1614 replace_call_with_call_and_fold (gsi, repl);
1615 return true;
1618 return false;
1621 tree len;
1622 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
1624 if (!strlen_fn)
1625 return false;
1627 /* Create newstr = strlen (str). */
1628 gimple_seq stmts = NULL;
1629 gimple *new_stmt = gimple_build_call (strlen_fn, 1, str);
1630 gimple_set_location (new_stmt, loc);
1631 len = create_tmp_reg_or_ssa_name (size_type_node);
1632 gimple_call_set_lhs (new_stmt, len);
1633 gimple_seq_add_stmt_without_update (&stmts, new_stmt);
1635 /* Create (str p+ strlen (str)). */
1636 new_stmt = gimple_build_assign (gimple_call_lhs (stmt),
1637 POINTER_PLUS_EXPR, str, len);
1638 gimple_seq_add_stmt_without_update (&stmts, new_stmt);
1639 gsi_replace_with_seq_vops (gsi, stmts);
1640 /* gsi now points at the assignment to the lhs, get a
1641 stmt iterator to the strlen.
1642 ??? We can't use gsi_for_stmt as that doesn't work when the
1643 CFG isn't built yet. */
1644 gimple_stmt_iterator gsi2 = *gsi;
1645 gsi_prev (&gsi2);
1646 fold_stmt (&gsi2);
1647 return true;
1650 /* Fold function call to builtin strstr.
1651 If both arguments are constant, evaluate and fold the result,
1652 additionally fold strstr (x, "") into x and strstr (x, "c")
1653 into strchr (x, 'c'). */
1654 static bool
1655 gimple_fold_builtin_strstr (gimple_stmt_iterator *gsi)
1657 gimple *stmt = gsi_stmt (*gsi);
1658 tree haystack = gimple_call_arg (stmt, 0);
1659 tree needle = gimple_call_arg (stmt, 1);
1660 const char *p, *q;
1662 if (!gimple_call_lhs (stmt))
1663 return false;
1665 q = c_getstr (needle);
1666 if (q == NULL)
1667 return false;
1669 if ((p = c_getstr (haystack)))
1671 const char *r = strstr (p, q);
1673 if (r == NULL)
1675 replace_call_with_value (gsi, integer_zero_node);
1676 return true;
1679 tree len = build_int_cst (size_type_node, r - p);
1680 gimple_seq stmts = NULL;
1681 gimple *new_stmt
1682 = gimple_build_assign (gimple_call_lhs (stmt), POINTER_PLUS_EXPR,
1683 haystack, len);
1684 gimple_seq_add_stmt_without_update (&stmts, new_stmt);
1685 gsi_replace_with_seq_vops (gsi, stmts);
1686 return true;
1689 /* For strstr (x, "") return x. */
1690 if (q[0] == '\0')
1692 replace_call_with_value (gsi, haystack);
1693 return true;
1696 /* Transform strstr (x, "c") into strchr (x, 'c'). */
1697 if (q[1] == '\0')
1699 tree strchr_fn = builtin_decl_implicit (BUILT_IN_STRCHR);
1700 if (strchr_fn)
1702 tree c = build_int_cst (integer_type_node, q[0]);
1703 gimple *repl = gimple_build_call (strchr_fn, 2, haystack, c);
1704 replace_call_with_call_and_fold (gsi, repl);
1705 return true;
1709 return false;
1712 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
1713 to the call.
1715 Return NULL_TREE if no simplification was possible, otherwise return the
1716 simplified form of the call as a tree.
1718 The simplified form may be a constant or other expression which
1719 computes the same value, but in a more efficient manner (including
1720 calls to other builtin functions).
1722 The call may contain arguments which need to be evaluated, but
1723 which are not useful to determine the result of the call. In
1724 this case we return a chain of COMPOUND_EXPRs. The LHS of each
1725 COMPOUND_EXPR will be an argument which must be evaluated.
1726 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
1727 COMPOUND_EXPR in the chain will contain the tree for the simplified
1728 form of the builtin function call. */
1730 static bool
1731 gimple_fold_builtin_strcat (gimple_stmt_iterator *gsi, tree dst, tree src)
1733 gimple *stmt = gsi_stmt (*gsi);
1734 location_t loc = gimple_location (stmt);
1736 const char *p = c_getstr (src);
1738 /* If the string length is zero, return the dst parameter. */
1739 if (p && *p == '\0')
1741 replace_call_with_value (gsi, dst);
1742 return true;
1745 if (!optimize_bb_for_speed_p (gimple_bb (stmt)))
1746 return false;
1748 /* See if we can store by pieces into (dst + strlen(dst)). */
1749 tree newdst;
1750 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
1751 tree memcpy_fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1753 if (!strlen_fn || !memcpy_fn)
1754 return false;
1756 /* If the length of the source string isn't computable don't
1757 split strcat into strlen and memcpy. */
1758 tree len = get_maxval_strlen (src, 0);
1759 if (! len)
1760 return false;
1762 /* Create strlen (dst). */
1763 gimple_seq stmts = NULL, stmts2;
1764 gimple *repl = gimple_build_call (strlen_fn, 1, dst);
1765 gimple_set_location (repl, loc);
1766 newdst = create_tmp_reg_or_ssa_name (size_type_node);
1767 gimple_call_set_lhs (repl, newdst);
1768 gimple_seq_add_stmt_without_update (&stmts, repl);
1770 /* Create (dst p+ strlen (dst)). */
1771 newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
1772 newdst = force_gimple_operand (newdst, &stmts2, true, NULL_TREE);
1773 gimple_seq_add_seq_without_update (&stmts, stmts2);
1775 len = fold_convert_loc (loc, size_type_node, len);
1776 len = size_binop_loc (loc, PLUS_EXPR, len,
1777 build_int_cst (size_type_node, 1));
1778 len = force_gimple_operand (len, &stmts2, true, NULL_TREE);
1779 gimple_seq_add_seq_without_update (&stmts, stmts2);
1781 repl = gimple_build_call (memcpy_fn, 3, newdst, src, len);
1782 gimple_seq_add_stmt_without_update (&stmts, repl);
1783 if (gimple_call_lhs (stmt))
1785 repl = gimple_build_assign (gimple_call_lhs (stmt), dst);
1786 gimple_seq_add_stmt_without_update (&stmts, repl);
1787 gsi_replace_with_seq_vops (gsi, stmts);
1788 /* gsi now points at the assignment to the lhs, get a
1789 stmt iterator to the memcpy call.
1790 ??? We can't use gsi_for_stmt as that doesn't work when the
1791 CFG isn't built yet. */
1792 gimple_stmt_iterator gsi2 = *gsi;
1793 gsi_prev (&gsi2);
1794 fold_stmt (&gsi2);
1796 else
1798 gsi_replace_with_seq_vops (gsi, stmts);
1799 fold_stmt (gsi);
1801 return true;
1804 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
1805 are the arguments to the call. */
1807 static bool
1808 gimple_fold_builtin_strcat_chk (gimple_stmt_iterator *gsi)
1810 gimple *stmt = gsi_stmt (*gsi);
1811 tree dest = gimple_call_arg (stmt, 0);
1812 tree src = gimple_call_arg (stmt, 1);
1813 tree size = gimple_call_arg (stmt, 2);
1814 tree fn;
1815 const char *p;
1818 p = c_getstr (src);
1819 /* If the SRC parameter is "", return DEST. */
1820 if (p && *p == '\0')
1822 replace_call_with_value (gsi, dest);
1823 return true;
1826 if (! tree_fits_uhwi_p (size) || ! integer_all_onesp (size))
1827 return false;
1829 /* If __builtin_strcat_chk is used, assume strcat is available. */
1830 fn = builtin_decl_explicit (BUILT_IN_STRCAT);
1831 if (!fn)
1832 return false;
1834 gimple *repl = gimple_build_call (fn, 2, dest, src);
1835 replace_call_with_call_and_fold (gsi, repl);
1836 return true;
1839 /* Simplify a call to the strncat builtin. */
1841 static bool
1842 gimple_fold_builtin_strncat (gimple_stmt_iterator *gsi)
1844 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
1845 tree dst = gimple_call_arg (stmt, 0);
1846 tree src = gimple_call_arg (stmt, 1);
1847 tree len = gimple_call_arg (stmt, 2);
1849 const char *p = c_getstr (src);
1851 /* If the requested length is zero, or the src parameter string
1852 length is zero, return the dst parameter. */
1853 if (integer_zerop (len) || (p && *p == '\0'))
1855 replace_call_with_value (gsi, dst);
1856 return true;
1859 /* If the requested len is greater than or equal to the string
1860 length, call strcat. */
1861 if (TREE_CODE (len) == INTEGER_CST && p
1862 && compare_tree_int (len, strlen (p)) >= 0)
1864 tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
1866 /* If the replacement _DECL isn't initialized, don't do the
1867 transformation. */
1868 if (!fn)
1869 return false;
1871 gcall *repl = gimple_build_call (fn, 2, dst, src);
1872 replace_call_with_call_and_fold (gsi, repl);
1873 return true;
1876 return false;
1879 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
1880 LEN, and SIZE. */
1882 static bool
1883 gimple_fold_builtin_strncat_chk (gimple_stmt_iterator *gsi)
1885 gimple *stmt = gsi_stmt (*gsi);
1886 tree dest = gimple_call_arg (stmt, 0);
1887 tree src = gimple_call_arg (stmt, 1);
1888 tree len = gimple_call_arg (stmt, 2);
1889 tree size = gimple_call_arg (stmt, 3);
1890 tree fn;
1891 const char *p;
1893 p = c_getstr (src);
1894 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
1895 if ((p && *p == '\0')
1896 || integer_zerop (len))
1898 replace_call_with_value (gsi, dest);
1899 return true;
1902 if (! tree_fits_uhwi_p (size))
1903 return false;
1905 if (! integer_all_onesp (size))
1907 tree src_len = c_strlen (src, 1);
1908 if (src_len
1909 && tree_fits_uhwi_p (src_len)
1910 && tree_fits_uhwi_p (len)
1911 && ! tree_int_cst_lt (len, src_len))
1913 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
1914 fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
1915 if (!fn)
1916 return false;
1918 gimple *repl = gimple_build_call (fn, 3, dest, src, size);
1919 replace_call_with_call_and_fold (gsi, repl);
1920 return true;
1922 return false;
1925 /* If __builtin_strncat_chk is used, assume strncat is available. */
1926 fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
1927 if (!fn)
1928 return false;
1930 gimple *repl = gimple_build_call (fn, 3, dest, src, len);
1931 replace_call_with_call_and_fold (gsi, repl);
1932 return true;
1935 /* Build and append gimple statements to STMTS that would load a first
1936 character of a memory location identified by STR. LOC is location
1937 of the statement. */
1939 static tree
1940 gimple_load_first_char (location_t loc, tree str, gimple_seq *stmts)
1942 tree var;
1944 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
1945 tree cst_uchar_ptr_node
1946 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
1947 tree off0 = build_int_cst (cst_uchar_ptr_node, 0);
1949 tree temp = fold_build2_loc (loc, MEM_REF, cst_uchar_node, str, off0);
1950 gassign *stmt = gimple_build_assign (NULL_TREE, temp);
1951 var = create_tmp_reg_or_ssa_name (cst_uchar_node, stmt);
1953 gimple_assign_set_lhs (stmt, var);
1954 gimple_seq_add_stmt_without_update (stmts, stmt);
1956 return var;
1959 /* Fold a call to the str{n}{case}cmp builtin pointed by GSI iterator.
1960 FCODE is the name of the builtin. */
1962 static bool
1963 gimple_fold_builtin_string_compare (gimple_stmt_iterator *gsi)
1965 gimple *stmt = gsi_stmt (*gsi);
1966 tree callee = gimple_call_fndecl (stmt);
1967 enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
1969 tree type = integer_type_node;
1970 tree str1 = gimple_call_arg (stmt, 0);
1971 tree str2 = gimple_call_arg (stmt, 1);
1972 tree lhs = gimple_call_lhs (stmt);
1973 HOST_WIDE_INT length = -1;
1975 /* Handle strncmp and strncasecmp functions. */
1976 if (gimple_call_num_args (stmt) == 3)
1978 tree len = gimple_call_arg (stmt, 2);
1979 if (tree_fits_uhwi_p (len))
1980 length = tree_to_uhwi (len);
1983 /* If the LEN parameter is zero, return zero. */
1984 if (length == 0)
1986 replace_call_with_value (gsi, integer_zero_node);
1987 return true;
1990 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
1991 if (operand_equal_p (str1, str2, 0))
1993 replace_call_with_value (gsi, integer_zero_node);
1994 return true;
1997 const char *p1 = c_getstr (str1);
1998 const char *p2 = c_getstr (str2);
2000 /* For known strings, return an immediate value. */
2001 if (p1 && p2)
2003 int r = 0;
2004 bool known_result = false;
2006 switch (fcode)
2008 case BUILT_IN_STRCMP:
2010 r = strcmp (p1, p2);
2011 known_result = true;
2012 break;
2014 case BUILT_IN_STRNCMP:
2016 if (length == -1)
2017 break;
2018 r = strncmp (p1, p2, length);
2019 known_result = true;
2020 break;
2022 /* Only handleable situation is where the string are equal (result 0),
2023 which is already handled by operand_equal_p case. */
2024 case BUILT_IN_STRCASECMP:
2025 break;
2026 case BUILT_IN_STRNCASECMP:
2028 if (length == -1)
2029 break;
2030 r = strncmp (p1, p2, length);
2031 if (r == 0)
2032 known_result = true;
2033 break;;
2035 default:
2036 gcc_unreachable ();
2039 if (known_result)
2041 replace_call_with_value (gsi, build_cmp_result (type, r));
2042 return true;
2046 bool nonzero_length = length >= 1
2047 || fcode == BUILT_IN_STRCMP
2048 || fcode == BUILT_IN_STRCASECMP;
2050 location_t loc = gimple_location (stmt);
2052 /* If the second arg is "", return *(const unsigned char*)arg1. */
2053 if (p2 && *p2 == '\0' && nonzero_length)
2055 gimple_seq stmts = NULL;
2056 tree var = gimple_load_first_char (loc, str1, &stmts);
2057 if (lhs)
2059 stmt = gimple_build_assign (lhs, NOP_EXPR, var);
2060 gimple_seq_add_stmt_without_update (&stmts, stmt);
2063 gsi_replace_with_seq_vops (gsi, stmts);
2064 return true;
2067 /* If the first arg is "", return -*(const unsigned char*)arg2. */
2068 if (p1 && *p1 == '\0' && nonzero_length)
2070 gimple_seq stmts = NULL;
2071 tree var = gimple_load_first_char (loc, str2, &stmts);
2073 if (lhs)
2075 tree c = create_tmp_reg_or_ssa_name (integer_type_node);
2076 stmt = gimple_build_assign (c, NOP_EXPR, var);
2077 gimple_seq_add_stmt_without_update (&stmts, stmt);
2079 stmt = gimple_build_assign (lhs, NEGATE_EXPR, c);
2080 gimple_seq_add_stmt_without_update (&stmts, stmt);
2083 gsi_replace_with_seq_vops (gsi, stmts);
2084 return true;
2087 /* If len parameter is one, return an expression corresponding to
2088 (*(const unsigned char*)arg2 - *(const unsigned char*)arg1). */
2089 if (fcode == BUILT_IN_STRNCMP && length == 1)
2091 gimple_seq stmts = NULL;
2092 tree temp1 = gimple_load_first_char (loc, str1, &stmts);
2093 tree temp2 = gimple_load_first_char (loc, str2, &stmts);
2095 if (lhs)
2097 tree c1 = create_tmp_reg_or_ssa_name (integer_type_node);
2098 gassign *convert1 = gimple_build_assign (c1, NOP_EXPR, temp1);
2099 gimple_seq_add_stmt_without_update (&stmts, convert1);
2101 tree c2 = create_tmp_reg_or_ssa_name (integer_type_node);
2102 gassign *convert2 = gimple_build_assign (c2, NOP_EXPR, temp2);
2103 gimple_seq_add_stmt_without_update (&stmts, convert2);
2105 stmt = gimple_build_assign (lhs, MINUS_EXPR, c1, c2);
2106 gimple_seq_add_stmt_without_update (&stmts, stmt);
2109 gsi_replace_with_seq_vops (gsi, stmts);
2110 return true;
2113 return false;
2116 /* Fold a call to the memchr pointed by GSI iterator. */
2118 static bool
2119 gimple_fold_builtin_memchr (gimple_stmt_iterator *gsi)
2121 gimple *stmt = gsi_stmt (*gsi);
2122 tree lhs = gimple_call_lhs (stmt);
2123 tree arg1 = gimple_call_arg (stmt, 0);
2124 tree arg2 = gimple_call_arg (stmt, 1);
2125 tree len = gimple_call_arg (stmt, 2);
2127 /* If the LEN parameter is zero, return zero. */
2128 if (integer_zerop (len))
2130 replace_call_with_value (gsi, build_int_cst (ptr_type_node, 0));
2131 return true;
2134 char c;
2135 if (TREE_CODE (arg2) != INTEGER_CST
2136 || !tree_fits_uhwi_p (len)
2137 || !target_char_cst_p (arg2, &c))
2138 return false;
2140 unsigned HOST_WIDE_INT length = tree_to_uhwi (len);
2141 unsigned HOST_WIDE_INT string_length;
2142 const char *p1 = c_getstr (arg1, &string_length);
2144 if (p1)
2146 const char *r = (const char *)memchr (p1, c, MIN (length, string_length));
2147 if (r == NULL)
2149 if (length <= string_length)
2151 replace_call_with_value (gsi, build_int_cst (ptr_type_node, 0));
2152 return true;
2155 else
2157 unsigned HOST_WIDE_INT offset = r - p1;
2158 gimple_seq stmts = NULL;
2159 if (lhs != NULL_TREE)
2161 tree offset_cst = build_int_cst (TREE_TYPE (len), offset);
2162 gassign *stmt = gimple_build_assign (lhs, POINTER_PLUS_EXPR,
2163 arg1, offset_cst);
2164 gimple_seq_add_stmt_without_update (&stmts, stmt);
2166 else
2167 gimple_seq_add_stmt_without_update (&stmts,
2168 gimple_build_nop ());
2170 gsi_replace_with_seq_vops (gsi, stmts);
2171 return true;
2175 return false;
2178 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
2179 to the call. IGNORE is true if the value returned
2180 by the builtin will be ignored. UNLOCKED is true is true if this
2181 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
2182 the known length of the string. Return NULL_TREE if no simplification
2183 was possible. */
2185 static bool
2186 gimple_fold_builtin_fputs (gimple_stmt_iterator *gsi,
2187 tree arg0, tree arg1,
2188 bool unlocked)
2190 gimple *stmt = gsi_stmt (*gsi);
2192 /* If we're using an unlocked function, assume the other unlocked
2193 functions exist explicitly. */
2194 tree const fn_fputc = (unlocked
2195 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
2196 : builtin_decl_implicit (BUILT_IN_FPUTC));
2197 tree const fn_fwrite = (unlocked
2198 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
2199 : builtin_decl_implicit (BUILT_IN_FWRITE));
2201 /* If the return value is used, don't do the transformation. */
2202 if (gimple_call_lhs (stmt))
2203 return false;
2205 /* Get the length of the string passed to fputs. If the length
2206 can't be determined, punt. */
2207 tree len = get_maxval_strlen (arg0, 0);
2208 if (!len
2209 || TREE_CODE (len) != INTEGER_CST)
2210 return false;
2212 switch (compare_tree_int (len, 1))
2214 case -1: /* length is 0, delete the call entirely . */
2215 replace_call_with_value (gsi, integer_zero_node);
2216 return true;
2218 case 0: /* length is 1, call fputc. */
2220 const char *p = c_getstr (arg0);
2221 if (p != NULL)
2223 if (!fn_fputc)
2224 return false;
2226 gimple *repl = gimple_build_call (fn_fputc, 2,
2227 build_int_cst
2228 (integer_type_node, p[0]), arg1);
2229 replace_call_with_call_and_fold (gsi, repl);
2230 return true;
2233 /* FALLTHROUGH */
2234 case 1: /* length is greater than 1, call fwrite. */
2236 /* If optimizing for size keep fputs. */
2237 if (optimize_function_for_size_p (cfun))
2238 return false;
2239 /* New argument list transforming fputs(string, stream) to
2240 fwrite(string, 1, len, stream). */
2241 if (!fn_fwrite)
2242 return false;
2244 gimple *repl = gimple_build_call (fn_fwrite, 4, arg0,
2245 size_one_node, len, arg1);
2246 replace_call_with_call_and_fold (gsi, repl);
2247 return true;
2249 default:
2250 gcc_unreachable ();
2252 return false;
2255 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
2256 DEST, SRC, LEN, and SIZE are the arguments to the call.
2257 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
2258 code of the builtin. If MAXLEN is not NULL, it is maximum length
2259 passed as third argument. */
2261 static bool
2262 gimple_fold_builtin_memory_chk (gimple_stmt_iterator *gsi,
2263 tree dest, tree src, tree len, tree size,
2264 enum built_in_function fcode)
2266 gimple *stmt = gsi_stmt (*gsi);
2267 location_t loc = gimple_location (stmt);
2268 bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
2269 tree fn;
2271 /* If SRC and DEST are the same (and not volatile), return DEST
2272 (resp. DEST+LEN for __mempcpy_chk). */
2273 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
2275 if (fcode != BUILT_IN_MEMPCPY_CHK)
2277 replace_call_with_value (gsi, dest);
2278 return true;
2280 else
2282 gimple_seq stmts = NULL;
2283 len = gimple_convert_to_ptrofftype (&stmts, loc, len);
2284 tree temp = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
2285 TREE_TYPE (dest), dest, len);
2286 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
2287 replace_call_with_value (gsi, temp);
2288 return true;
2292 if (! tree_fits_uhwi_p (size))
2293 return false;
2295 tree maxlen = get_maxval_strlen (len, 2);
2296 if (! integer_all_onesp (size))
2298 if (! tree_fits_uhwi_p (len))
2300 /* If LEN is not constant, try MAXLEN too.
2301 For MAXLEN only allow optimizing into non-_ocs function
2302 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2303 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
2305 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
2307 /* (void) __mempcpy_chk () can be optimized into
2308 (void) __memcpy_chk (). */
2309 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
2310 if (!fn)
2311 return false;
2313 gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
2314 replace_call_with_call_and_fold (gsi, repl);
2315 return true;
2317 return false;
2320 else
2321 maxlen = len;
2323 if (tree_int_cst_lt (size, maxlen))
2324 return false;
2327 fn = NULL_TREE;
2328 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
2329 mem{cpy,pcpy,move,set} is available. */
2330 switch (fcode)
2332 case BUILT_IN_MEMCPY_CHK:
2333 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
2334 break;
2335 case BUILT_IN_MEMPCPY_CHK:
2336 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
2337 break;
2338 case BUILT_IN_MEMMOVE_CHK:
2339 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
2340 break;
2341 case BUILT_IN_MEMSET_CHK:
2342 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
2343 break;
2344 default:
2345 break;
2348 if (!fn)
2349 return false;
2351 gimple *repl = gimple_build_call (fn, 3, dest, src, len);
2352 replace_call_with_call_and_fold (gsi, repl);
2353 return true;
2356 /* Fold a call to the __st[rp]cpy_chk builtin.
2357 DEST, SRC, and SIZE are the arguments to the call.
2358 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
2359 code of the builtin. If MAXLEN is not NULL, it is maximum length of
2360 strings passed as second argument. */
2362 static bool
2363 gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator *gsi,
2364 tree dest,
2365 tree src, tree size,
2366 enum built_in_function fcode)
2368 gimple *stmt = gsi_stmt (*gsi);
2369 location_t loc = gimple_location (stmt);
2370 bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
2371 tree len, fn;
2373 /* If SRC and DEST are the same (and not volatile), return DEST. */
2374 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
2376 replace_call_with_value (gsi, dest);
2377 return true;
2380 if (! tree_fits_uhwi_p (size))
2381 return false;
2383 tree maxlen = get_maxval_strlen (src, 1);
2384 if (! integer_all_onesp (size))
2386 len = c_strlen (src, 1);
2387 if (! len || ! tree_fits_uhwi_p (len))
2389 /* If LEN is not constant, try MAXLEN too.
2390 For MAXLEN only allow optimizing into non-_ocs function
2391 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2392 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
2394 if (fcode == BUILT_IN_STPCPY_CHK)
2396 if (! ignore)
2397 return false;
2399 /* If return value of __stpcpy_chk is ignored,
2400 optimize into __strcpy_chk. */
2401 fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
2402 if (!fn)
2403 return false;
2405 gimple *repl = gimple_build_call (fn, 3, dest, src, size);
2406 replace_call_with_call_and_fold (gsi, repl);
2407 return true;
2410 if (! len || TREE_SIDE_EFFECTS (len))
2411 return false;
2413 /* If c_strlen returned something, but not a constant,
2414 transform __strcpy_chk into __memcpy_chk. */
2415 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
2416 if (!fn)
2417 return false;
2419 gimple_seq stmts = NULL;
2420 len = gimple_convert (&stmts, loc, size_type_node, len);
2421 len = gimple_build (&stmts, loc, PLUS_EXPR, size_type_node, len,
2422 build_int_cst (size_type_node, 1));
2423 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
2424 gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
2425 replace_call_with_call_and_fold (gsi, repl);
2426 return true;
2429 else
2430 maxlen = len;
2432 if (! tree_int_cst_lt (maxlen, size))
2433 return false;
2436 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
2437 fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK
2438 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
2439 if (!fn)
2440 return false;
2442 gimple *repl = gimple_build_call (fn, 2, dest, src);
2443 replace_call_with_call_and_fold (gsi, repl);
2444 return true;
2447 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
2448 are the arguments to the call. If MAXLEN is not NULL, it is maximum
2449 length passed as third argument. IGNORE is true if return value can be
2450 ignored. FCODE is the BUILT_IN_* code of the builtin. */
2452 static bool
2453 gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator *gsi,
2454 tree dest, tree src,
2455 tree len, tree size,
2456 enum built_in_function fcode)
2458 gimple *stmt = gsi_stmt (*gsi);
2459 bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
2460 tree fn;
2462 if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
2464 /* If return value of __stpncpy_chk is ignored,
2465 optimize into __strncpy_chk. */
2466 fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
2467 if (fn)
2469 gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
2470 replace_call_with_call_and_fold (gsi, repl);
2471 return true;
2475 if (! tree_fits_uhwi_p (size))
2476 return false;
2478 tree maxlen = get_maxval_strlen (len, 2);
2479 if (! integer_all_onesp (size))
2481 if (! tree_fits_uhwi_p (len))
2483 /* If LEN is not constant, try MAXLEN too.
2484 For MAXLEN only allow optimizing into non-_ocs function
2485 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2486 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
2487 return false;
2489 else
2490 maxlen = len;
2492 if (tree_int_cst_lt (size, maxlen))
2493 return false;
2496 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
2497 fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK
2498 ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
2499 if (!fn)
2500 return false;
2502 gimple *repl = gimple_build_call (fn, 3, dest, src, len);
2503 replace_call_with_call_and_fold (gsi, repl);
2504 return true;
2507 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
2508 Return NULL_TREE if no simplification can be made. */
2510 static bool
2511 gimple_fold_builtin_stpcpy (gimple_stmt_iterator *gsi)
2513 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
2514 location_t loc = gimple_location (stmt);
2515 tree dest = gimple_call_arg (stmt, 0);
2516 tree src = gimple_call_arg (stmt, 1);
2517 tree fn, len, lenp1;
2519 /* If the result is unused, replace stpcpy with strcpy. */
2520 if (gimple_call_lhs (stmt) == NULL_TREE)
2522 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
2523 if (!fn)
2524 return false;
2525 gimple_call_set_fndecl (stmt, fn);
2526 fold_stmt (gsi);
2527 return true;
2530 len = c_strlen (src, 1);
2531 if (!len
2532 || TREE_CODE (len) != INTEGER_CST)
2533 return false;
2535 if (optimize_function_for_size_p (cfun)
2536 /* If length is zero it's small enough. */
2537 && !integer_zerop (len))
2538 return false;
2540 /* If the source has a known length replace stpcpy with memcpy. */
2541 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2542 if (!fn)
2543 return false;
2545 gimple_seq stmts = NULL;
2546 tree tem = gimple_convert (&stmts, loc, size_type_node, len);
2547 lenp1 = gimple_build (&stmts, loc, PLUS_EXPR, size_type_node,
2548 tem, build_int_cst (size_type_node, 1));
2549 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
2550 gcall *repl = gimple_build_call (fn, 3, dest, src, lenp1);
2551 gimple_set_vuse (repl, gimple_vuse (stmt));
2552 gimple_set_vdef (repl, gimple_vdef (stmt));
2553 if (gimple_vdef (repl)
2554 && TREE_CODE (gimple_vdef (repl)) == SSA_NAME)
2555 SSA_NAME_DEF_STMT (gimple_vdef (repl)) = repl;
2556 gsi_insert_before (gsi, repl, GSI_SAME_STMT);
2557 /* Replace the result with dest + len. */
2558 stmts = NULL;
2559 tem = gimple_convert (&stmts, loc, sizetype, len);
2560 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
2561 gassign *ret = gimple_build_assign (gimple_call_lhs (stmt),
2562 POINTER_PLUS_EXPR, dest, tem);
2563 gsi_replace (gsi, ret, false);
2564 /* Finally fold the memcpy call. */
2565 gimple_stmt_iterator gsi2 = *gsi;
2566 gsi_prev (&gsi2);
2567 fold_stmt (&gsi2);
2568 return true;
2571 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
2572 NULL_TREE if a normal call should be emitted rather than expanding
2573 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
2574 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
2575 passed as second argument. */
2577 static bool
2578 gimple_fold_builtin_snprintf_chk (gimple_stmt_iterator *gsi,
2579 enum built_in_function fcode)
2581 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
2582 tree dest, size, len, fn, fmt, flag;
2583 const char *fmt_str;
2585 /* Verify the required arguments in the original call. */
2586 if (gimple_call_num_args (stmt) < 5)
2587 return false;
2589 dest = gimple_call_arg (stmt, 0);
2590 len = gimple_call_arg (stmt, 1);
2591 flag = gimple_call_arg (stmt, 2);
2592 size = gimple_call_arg (stmt, 3);
2593 fmt = gimple_call_arg (stmt, 4);
2595 if (! tree_fits_uhwi_p (size))
2596 return false;
2598 if (! integer_all_onesp (size))
2600 tree maxlen = get_maxval_strlen (len, 2);
2601 if (! tree_fits_uhwi_p (len))
2603 /* If LEN is not constant, try MAXLEN too.
2604 For MAXLEN only allow optimizing into non-_ocs function
2605 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2606 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
2607 return false;
2609 else
2610 maxlen = len;
2612 if (tree_int_cst_lt (size, maxlen))
2613 return false;
2616 if (!init_target_chars ())
2617 return false;
2619 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
2620 or if format doesn't contain % chars or is "%s". */
2621 if (! integer_zerop (flag))
2623 fmt_str = c_getstr (fmt);
2624 if (fmt_str == NULL)
2625 return false;
2626 if (strchr (fmt_str, target_percent) != NULL
2627 && strcmp (fmt_str, target_percent_s))
2628 return false;
2631 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
2632 available. */
2633 fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
2634 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
2635 if (!fn)
2636 return false;
2638 /* Replace the called function and the first 5 argument by 3 retaining
2639 trailing varargs. */
2640 gimple_call_set_fndecl (stmt, fn);
2641 gimple_call_set_fntype (stmt, TREE_TYPE (fn));
2642 gimple_call_set_arg (stmt, 0, dest);
2643 gimple_call_set_arg (stmt, 1, len);
2644 gimple_call_set_arg (stmt, 2, fmt);
2645 for (unsigned i = 3; i < gimple_call_num_args (stmt) - 2; ++i)
2646 gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2));
2647 gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2);
2648 fold_stmt (gsi);
2649 return true;
2652 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
2653 Return NULL_TREE if a normal call should be emitted rather than
2654 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
2655 or BUILT_IN_VSPRINTF_CHK. */
2657 static bool
2658 gimple_fold_builtin_sprintf_chk (gimple_stmt_iterator *gsi,
2659 enum built_in_function fcode)
2661 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
2662 tree dest, size, len, fn, fmt, flag;
2663 const char *fmt_str;
2664 unsigned nargs = gimple_call_num_args (stmt);
2666 /* Verify the required arguments in the original call. */
2667 if (nargs < 4)
2668 return false;
2669 dest = gimple_call_arg (stmt, 0);
2670 flag = gimple_call_arg (stmt, 1);
2671 size = gimple_call_arg (stmt, 2);
2672 fmt = gimple_call_arg (stmt, 3);
2674 if (! tree_fits_uhwi_p (size))
2675 return false;
2677 len = NULL_TREE;
2679 if (!init_target_chars ())
2680 return false;
2682 /* Check whether the format is a literal string constant. */
2683 fmt_str = c_getstr (fmt);
2684 if (fmt_str != NULL)
2686 /* If the format doesn't contain % args or %%, we know the size. */
2687 if (strchr (fmt_str, target_percent) == 0)
2689 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
2690 len = build_int_cstu (size_type_node, strlen (fmt_str));
2692 /* If the format is "%s" and first ... argument is a string literal,
2693 we know the size too. */
2694 else if (fcode == BUILT_IN_SPRINTF_CHK
2695 && strcmp (fmt_str, target_percent_s) == 0)
2697 tree arg;
2699 if (nargs == 5)
2701 arg = gimple_call_arg (stmt, 4);
2702 if (POINTER_TYPE_P (TREE_TYPE (arg)))
2704 len = c_strlen (arg, 1);
2705 if (! len || ! tree_fits_uhwi_p (len))
2706 len = NULL_TREE;
2712 if (! integer_all_onesp (size))
2714 if (! len || ! tree_int_cst_lt (len, size))
2715 return false;
2718 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
2719 or if format doesn't contain % chars or is "%s". */
2720 if (! integer_zerop (flag))
2722 if (fmt_str == NULL)
2723 return false;
2724 if (strchr (fmt_str, target_percent) != NULL
2725 && strcmp (fmt_str, target_percent_s))
2726 return false;
2729 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
2730 fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
2731 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
2732 if (!fn)
2733 return false;
2735 /* Replace the called function and the first 4 argument by 2 retaining
2736 trailing varargs. */
2737 gimple_call_set_fndecl (stmt, fn);
2738 gimple_call_set_fntype (stmt, TREE_TYPE (fn));
2739 gimple_call_set_arg (stmt, 0, dest);
2740 gimple_call_set_arg (stmt, 1, fmt);
2741 for (unsigned i = 2; i < gimple_call_num_args (stmt) - 2; ++i)
2742 gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2));
2743 gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2);
2744 fold_stmt (gsi);
2745 return true;
2748 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
2749 ORIG may be null if this is a 2-argument call. We don't attempt to
2750 simplify calls with more than 3 arguments.
2752 Return true if simplification was possible, otherwise false. */
2754 bool
2755 gimple_fold_builtin_sprintf (gimple_stmt_iterator *gsi)
2757 gimple *stmt = gsi_stmt (*gsi);
2758 tree dest = gimple_call_arg (stmt, 0);
2759 tree fmt = gimple_call_arg (stmt, 1);
2760 tree orig = NULL_TREE;
2761 const char *fmt_str = NULL;
2763 /* Verify the required arguments in the original call. We deal with two
2764 types of sprintf() calls: 'sprintf (str, fmt)' and
2765 'sprintf (dest, "%s", orig)'. */
2766 if (gimple_call_num_args (stmt) > 3)
2767 return false;
2769 if (gimple_call_num_args (stmt) == 3)
2770 orig = gimple_call_arg (stmt, 2);
2772 /* Check whether the format is a literal string constant. */
2773 fmt_str = c_getstr (fmt);
2774 if (fmt_str == NULL)
2775 return false;
2777 if (!init_target_chars ())
2778 return false;
2780 /* If the format doesn't contain % args or %%, use strcpy. */
2781 if (strchr (fmt_str, target_percent) == NULL)
2783 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
2785 if (!fn)
2786 return false;
2788 /* Don't optimize sprintf (buf, "abc", ptr++). */
2789 if (orig)
2790 return false;
2792 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
2793 'format' is known to contain no % formats. */
2794 gimple_seq stmts = NULL;
2795 gimple *repl = gimple_build_call (fn, 2, dest, fmt);
2796 gimple_seq_add_stmt_without_update (&stmts, repl);
2797 if (gimple_call_lhs (stmt))
2799 repl = gimple_build_assign (gimple_call_lhs (stmt),
2800 build_int_cst (integer_type_node,
2801 strlen (fmt_str)));
2802 gimple_seq_add_stmt_without_update (&stmts, repl);
2803 gsi_replace_with_seq_vops (gsi, stmts);
2804 /* gsi now points at the assignment to the lhs, get a
2805 stmt iterator to the memcpy call.
2806 ??? We can't use gsi_for_stmt as that doesn't work when the
2807 CFG isn't built yet. */
2808 gimple_stmt_iterator gsi2 = *gsi;
2809 gsi_prev (&gsi2);
2810 fold_stmt (&gsi2);
2812 else
2814 gsi_replace_with_seq_vops (gsi, stmts);
2815 fold_stmt (gsi);
2817 return true;
2820 /* If the format is "%s", use strcpy if the result isn't used. */
2821 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
2823 tree fn;
2824 fn = builtin_decl_implicit (BUILT_IN_STRCPY);
2826 if (!fn)
2827 return false;
2829 /* Don't crash on sprintf (str1, "%s"). */
2830 if (!orig)
2831 return false;
2833 tree orig_len = NULL_TREE;
2834 if (gimple_call_lhs (stmt))
2836 orig_len = get_maxval_strlen (orig, 0);
2837 if (!orig_len)
2838 return false;
2841 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
2842 gimple_seq stmts = NULL;
2843 gimple *repl = gimple_build_call (fn, 2, dest, orig);
2844 gimple_seq_add_stmt_without_update (&stmts, repl);
2845 if (gimple_call_lhs (stmt))
2847 if (!useless_type_conversion_p (integer_type_node,
2848 TREE_TYPE (orig_len)))
2849 orig_len = fold_convert (integer_type_node, orig_len);
2850 repl = gimple_build_assign (gimple_call_lhs (stmt), orig_len);
2851 gimple_seq_add_stmt_without_update (&stmts, repl);
2852 gsi_replace_with_seq_vops (gsi, stmts);
2853 /* gsi now points at the assignment to the lhs, get a
2854 stmt iterator to the memcpy call.
2855 ??? We can't use gsi_for_stmt as that doesn't work when the
2856 CFG isn't built yet. */
2857 gimple_stmt_iterator gsi2 = *gsi;
2858 gsi_prev (&gsi2);
2859 fold_stmt (&gsi2);
2861 else
2863 gsi_replace_with_seq_vops (gsi, stmts);
2864 fold_stmt (gsi);
2866 return true;
2868 return false;
2871 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
2872 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
2873 attempt to simplify calls with more than 4 arguments.
2875 Return true if simplification was possible, otherwise false. */
2877 bool
2878 gimple_fold_builtin_snprintf (gimple_stmt_iterator *gsi)
2880 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
2881 tree dest = gimple_call_arg (stmt, 0);
2882 tree destsize = gimple_call_arg (stmt, 1);
2883 tree fmt = gimple_call_arg (stmt, 2);
2884 tree orig = NULL_TREE;
2885 const char *fmt_str = NULL;
2887 if (gimple_call_num_args (stmt) > 4)
2888 return false;
2890 if (gimple_call_num_args (stmt) == 4)
2891 orig = gimple_call_arg (stmt, 3);
2893 if (!tree_fits_uhwi_p (destsize))
2894 return false;
2895 unsigned HOST_WIDE_INT destlen = tree_to_uhwi (destsize);
2897 /* Check whether the format is a literal string constant. */
2898 fmt_str = c_getstr (fmt);
2899 if (fmt_str == NULL)
2900 return false;
2902 if (!init_target_chars ())
2903 return false;
2905 /* If the format doesn't contain % args or %%, use strcpy. */
2906 if (strchr (fmt_str, target_percent) == NULL)
2908 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
2909 if (!fn)
2910 return false;
2912 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
2913 if (orig)
2914 return false;
2916 /* We could expand this as
2917 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
2918 or to
2919 memcpy (str, fmt_with_nul_at_cstm1, cst);
2920 but in the former case that might increase code size
2921 and in the latter case grow .rodata section too much.
2922 So punt for now. */
2923 size_t len = strlen (fmt_str);
2924 if (len >= destlen)
2925 return false;
2927 gimple_seq stmts = NULL;
2928 gimple *repl = gimple_build_call (fn, 2, dest, fmt);
2929 gimple_seq_add_stmt_without_update (&stmts, repl);
2930 if (gimple_call_lhs (stmt))
2932 repl = gimple_build_assign (gimple_call_lhs (stmt),
2933 build_int_cst (integer_type_node, len));
2934 gimple_seq_add_stmt_without_update (&stmts, repl);
2935 gsi_replace_with_seq_vops (gsi, stmts);
2936 /* gsi now points at the assignment to the lhs, get a
2937 stmt iterator to the memcpy call.
2938 ??? We can't use gsi_for_stmt as that doesn't work when the
2939 CFG isn't built yet. */
2940 gimple_stmt_iterator gsi2 = *gsi;
2941 gsi_prev (&gsi2);
2942 fold_stmt (&gsi2);
2944 else
2946 gsi_replace_with_seq_vops (gsi, stmts);
2947 fold_stmt (gsi);
2949 return true;
2952 /* If the format is "%s", use strcpy if the result isn't used. */
2953 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
2955 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
2956 if (!fn)
2957 return false;
2959 /* Don't crash on snprintf (str1, cst, "%s"). */
2960 if (!orig)
2961 return false;
2963 tree orig_len = get_maxval_strlen (orig, 0);
2964 if (!orig_len || TREE_CODE (orig_len) != INTEGER_CST)
2965 return false;
2967 /* We could expand this as
2968 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
2969 or to
2970 memcpy (str1, str2_with_nul_at_cstm1, cst);
2971 but in the former case that might increase code size
2972 and in the latter case grow .rodata section too much.
2973 So punt for now. */
2974 if (compare_tree_int (orig_len, destlen) >= 0)
2975 return false;
2977 /* Convert snprintf (str1, cst, "%s", str2) into
2978 strcpy (str1, str2) if strlen (str2) < cst. */
2979 gimple_seq stmts = NULL;
2980 gimple *repl = gimple_build_call (fn, 2, dest, orig);
2981 gimple_seq_add_stmt_without_update (&stmts, repl);
2982 if (gimple_call_lhs (stmt))
2984 if (!useless_type_conversion_p (integer_type_node,
2985 TREE_TYPE (orig_len)))
2986 orig_len = fold_convert (integer_type_node, orig_len);
2987 repl = gimple_build_assign (gimple_call_lhs (stmt), orig_len);
2988 gimple_seq_add_stmt_without_update (&stmts, repl);
2989 gsi_replace_with_seq_vops (gsi, stmts);
2990 /* gsi now points at the assignment to the lhs, get a
2991 stmt iterator to the memcpy call.
2992 ??? We can't use gsi_for_stmt as that doesn't work when the
2993 CFG isn't built yet. */
2994 gimple_stmt_iterator gsi2 = *gsi;
2995 gsi_prev (&gsi2);
2996 fold_stmt (&gsi2);
2998 else
3000 gsi_replace_with_seq_vops (gsi, stmts);
3001 fold_stmt (gsi);
3003 return true;
3005 return false;
3008 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
3009 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
3010 more than 3 arguments, and ARG may be null in the 2-argument case.
3012 Return NULL_TREE if no simplification was possible, otherwise return the
3013 simplified form of the call as a tree. FCODE is the BUILT_IN_*
3014 code of the function to be simplified. */
3016 static bool
3017 gimple_fold_builtin_fprintf (gimple_stmt_iterator *gsi,
3018 tree fp, tree fmt, tree arg,
3019 enum built_in_function fcode)
3021 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3022 tree fn_fputc, fn_fputs;
3023 const char *fmt_str = NULL;
3025 /* If the return value is used, don't do the transformation. */
3026 if (gimple_call_lhs (stmt) != NULL_TREE)
3027 return false;
3029 /* Check whether the format is a literal string constant. */
3030 fmt_str = c_getstr (fmt);
3031 if (fmt_str == NULL)
3032 return false;
3034 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
3036 /* If we're using an unlocked function, assume the other
3037 unlocked functions exist explicitly. */
3038 fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
3039 fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
3041 else
3043 fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
3044 fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
3047 if (!init_target_chars ())
3048 return false;
3050 /* If the format doesn't contain % args or %%, use strcpy. */
3051 if (strchr (fmt_str, target_percent) == NULL)
3053 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
3054 && arg)
3055 return false;
3057 /* If the format specifier was "", fprintf does nothing. */
3058 if (fmt_str[0] == '\0')
3060 replace_call_with_value (gsi, NULL_TREE);
3061 return true;
3064 /* When "string" doesn't contain %, replace all cases of
3065 fprintf (fp, string) with fputs (string, fp). The fputs
3066 builtin will take care of special cases like length == 1. */
3067 if (fn_fputs)
3069 gcall *repl = gimple_build_call (fn_fputs, 2, fmt, fp);
3070 replace_call_with_call_and_fold (gsi, repl);
3071 return true;
3075 /* The other optimizations can be done only on the non-va_list variants. */
3076 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
3077 return false;
3079 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
3080 else if (strcmp (fmt_str, target_percent_s) == 0)
3082 if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
3083 return false;
3084 if (fn_fputs)
3086 gcall *repl = gimple_build_call (fn_fputs, 2, arg, fp);
3087 replace_call_with_call_and_fold (gsi, repl);
3088 return true;
3092 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
3093 else if (strcmp (fmt_str, target_percent_c) == 0)
3095 if (!arg
3096 || ! useless_type_conversion_p (integer_type_node, TREE_TYPE (arg)))
3097 return false;
3098 if (fn_fputc)
3100 gcall *repl = gimple_build_call (fn_fputc, 2, arg, fp);
3101 replace_call_with_call_and_fold (gsi, repl);
3102 return true;
3106 return false;
3109 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
3110 FMT and ARG are the arguments to the call; we don't fold cases with
3111 more than 2 arguments, and ARG may be null if this is a 1-argument case.
3113 Return NULL_TREE if no simplification was possible, otherwise return the
3114 simplified form of the call as a tree. FCODE is the BUILT_IN_*
3115 code of the function to be simplified. */
3117 static bool
3118 gimple_fold_builtin_printf (gimple_stmt_iterator *gsi, tree fmt,
3119 tree arg, enum built_in_function fcode)
3121 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3122 tree fn_putchar, fn_puts, newarg;
3123 const char *fmt_str = NULL;
3125 /* If the return value is used, don't do the transformation. */
3126 if (gimple_call_lhs (stmt) != NULL_TREE)
3127 return false;
3129 /* Check whether the format is a literal string constant. */
3130 fmt_str = c_getstr (fmt);
3131 if (fmt_str == NULL)
3132 return false;
3134 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
3136 /* If we're using an unlocked function, assume the other
3137 unlocked functions exist explicitly. */
3138 fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
3139 fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
3141 else
3143 fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
3144 fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
3147 if (!init_target_chars ())
3148 return false;
3150 if (strcmp (fmt_str, target_percent_s) == 0
3151 || strchr (fmt_str, target_percent) == NULL)
3153 const char *str;
3155 if (strcmp (fmt_str, target_percent_s) == 0)
3157 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
3158 return false;
3160 if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
3161 return false;
3163 str = c_getstr (arg);
3164 if (str == NULL)
3165 return false;
3167 else
3169 /* The format specifier doesn't contain any '%' characters. */
3170 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
3171 && arg)
3172 return false;
3173 str = fmt_str;
3176 /* If the string was "", printf does nothing. */
3177 if (str[0] == '\0')
3179 replace_call_with_value (gsi, NULL_TREE);
3180 return true;
3183 /* If the string has length of 1, call putchar. */
3184 if (str[1] == '\0')
3186 /* Given printf("c"), (where c is any one character,)
3187 convert "c"[0] to an int and pass that to the replacement
3188 function. */
3189 newarg = build_int_cst (integer_type_node, str[0]);
3190 if (fn_putchar)
3192 gcall *repl = gimple_build_call (fn_putchar, 1, newarg);
3193 replace_call_with_call_and_fold (gsi, repl);
3194 return true;
3197 else
3199 /* If the string was "string\n", call puts("string"). */
3200 size_t len = strlen (str);
3201 if ((unsigned char)str[len - 1] == target_newline
3202 && (size_t) (int) len == len
3203 && (int) len > 0)
3205 char *newstr;
3206 tree offset_node, string_cst;
3208 /* Create a NUL-terminated string that's one char shorter
3209 than the original, stripping off the trailing '\n'. */
3210 newarg = build_string_literal (len, str);
3211 string_cst = string_constant (newarg, &offset_node);
3212 gcc_checking_assert (string_cst
3213 && (TREE_STRING_LENGTH (string_cst)
3214 == (int) len)
3215 && integer_zerop (offset_node)
3216 && (unsigned char)
3217 TREE_STRING_POINTER (string_cst)[len - 1]
3218 == target_newline);
3219 /* build_string_literal creates a new STRING_CST,
3220 modify it in place to avoid double copying. */
3221 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
3222 newstr[len - 1] = '\0';
3223 if (fn_puts)
3225 gcall *repl = gimple_build_call (fn_puts, 1, newarg);
3226 replace_call_with_call_and_fold (gsi, repl);
3227 return true;
3230 else
3231 /* We'd like to arrange to call fputs(string,stdout) here,
3232 but we need stdout and don't have a way to get it yet. */
3233 return false;
3237 /* The other optimizations can be done only on the non-va_list variants. */
3238 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
3239 return false;
3241 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
3242 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
3244 if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
3245 return false;
3246 if (fn_puts)
3248 gcall *repl = gimple_build_call (fn_puts, 1, arg);
3249 replace_call_with_call_and_fold (gsi, repl);
3250 return true;
3254 /* If the format specifier was "%c", call __builtin_putchar(arg). */
3255 else if (strcmp (fmt_str, target_percent_c) == 0)
3257 if (!arg || ! useless_type_conversion_p (integer_type_node,
3258 TREE_TYPE (arg)))
3259 return false;
3260 if (fn_putchar)
3262 gcall *repl = gimple_build_call (fn_putchar, 1, arg);
3263 replace_call_with_call_and_fold (gsi, repl);
3264 return true;
3268 return false;
3273 /* Fold a call to __builtin_strlen with known length LEN. */
3275 static bool
3276 gimple_fold_builtin_strlen (gimple_stmt_iterator *gsi)
3278 gimple *stmt = gsi_stmt (*gsi);
3279 tree len = get_maxval_strlen (gimple_call_arg (stmt, 0), 0);
3280 if (!len)
3281 return false;
3282 len = force_gimple_operand_gsi (gsi, len, true, NULL, true, GSI_SAME_STMT);
3283 replace_call_with_value (gsi, len);
3284 return true;
3287 /* Fold a call to __builtin_acc_on_device. */
3289 static bool
3290 gimple_fold_builtin_acc_on_device (gimple_stmt_iterator *gsi, tree arg0)
3292 /* Defer folding until we know which compiler we're in. */
3293 if (symtab->state != EXPANSION)
3294 return false;
3296 unsigned val_host = GOMP_DEVICE_HOST;
3297 unsigned val_dev = GOMP_DEVICE_NONE;
3299 #ifdef ACCEL_COMPILER
3300 val_host = GOMP_DEVICE_NOT_HOST;
3301 val_dev = ACCEL_COMPILER_acc_device;
3302 #endif
3304 location_t loc = gimple_location (gsi_stmt (*gsi));
3306 tree host_eq = make_ssa_name (boolean_type_node);
3307 gimple *host_ass = gimple_build_assign
3308 (host_eq, EQ_EXPR, arg0, build_int_cst (TREE_TYPE (arg0), val_host));
3309 gimple_set_location (host_ass, loc);
3310 gsi_insert_before (gsi, host_ass, GSI_SAME_STMT);
3312 tree dev_eq = make_ssa_name (boolean_type_node);
3313 gimple *dev_ass = gimple_build_assign
3314 (dev_eq, EQ_EXPR, arg0, build_int_cst (TREE_TYPE (arg0), val_dev));
3315 gimple_set_location (dev_ass, loc);
3316 gsi_insert_before (gsi, dev_ass, GSI_SAME_STMT);
3318 tree result = make_ssa_name (boolean_type_node);
3319 gimple *result_ass = gimple_build_assign
3320 (result, BIT_IOR_EXPR, host_eq, dev_eq);
3321 gimple_set_location (result_ass, loc);
3322 gsi_insert_before (gsi, result_ass, GSI_SAME_STMT);
3324 replace_call_with_value (gsi, result);
3326 return true;
3329 /* Fold realloc (0, n) -> malloc (n). */
3331 static bool
3332 gimple_fold_builtin_realloc (gimple_stmt_iterator *gsi)
3334 gimple *stmt = gsi_stmt (*gsi);
3335 tree arg = gimple_call_arg (stmt, 0);
3336 tree size = gimple_call_arg (stmt, 1);
3338 if (operand_equal_p (arg, null_pointer_node, 0))
3340 tree fn_malloc = builtin_decl_implicit (BUILT_IN_MALLOC);
3341 if (fn_malloc)
3343 gcall *repl = gimple_build_call (fn_malloc, 1, size);
3344 replace_call_with_call_and_fold (gsi, repl);
3345 return true;
3348 return false;
3351 /* Fold the non-target builtin at *GSI and return whether any simplification
3352 was made. */
3354 static bool
3355 gimple_fold_builtin (gimple_stmt_iterator *gsi)
3357 gcall *stmt = as_a <gcall *>(gsi_stmt (*gsi));
3358 tree callee = gimple_call_fndecl (stmt);
3360 /* Give up for always_inline inline builtins until they are
3361 inlined. */
3362 if (avoid_folding_inline_builtin (callee))
3363 return false;
3365 unsigned n = gimple_call_num_args (stmt);
3366 enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
3367 switch (fcode)
3369 case BUILT_IN_BCMP:
3370 return gimple_fold_builtin_bcmp (gsi);
3371 case BUILT_IN_BCOPY:
3372 return gimple_fold_builtin_bcopy (gsi);
3373 case BUILT_IN_BZERO:
3374 return gimple_fold_builtin_bzero (gsi);
3376 case BUILT_IN_MEMSET:
3377 return gimple_fold_builtin_memset (gsi,
3378 gimple_call_arg (stmt, 1),
3379 gimple_call_arg (stmt, 2));
3380 case BUILT_IN_MEMCPY:
3381 return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
3382 gimple_call_arg (stmt, 1), 0);
3383 case BUILT_IN_MEMPCPY:
3384 return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
3385 gimple_call_arg (stmt, 1), 1);
3386 case BUILT_IN_MEMMOVE:
3387 return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
3388 gimple_call_arg (stmt, 1), 3);
3389 case BUILT_IN_SPRINTF_CHK:
3390 case BUILT_IN_VSPRINTF_CHK:
3391 return gimple_fold_builtin_sprintf_chk (gsi, fcode);
3392 case BUILT_IN_STRCAT_CHK:
3393 return gimple_fold_builtin_strcat_chk (gsi);
3394 case BUILT_IN_STRNCAT_CHK:
3395 return gimple_fold_builtin_strncat_chk (gsi);
3396 case BUILT_IN_STRLEN:
3397 return gimple_fold_builtin_strlen (gsi);
3398 case BUILT_IN_STRCPY:
3399 return gimple_fold_builtin_strcpy (gsi,
3400 gimple_call_arg (stmt, 0),
3401 gimple_call_arg (stmt, 1));
3402 case BUILT_IN_STRNCPY:
3403 return gimple_fold_builtin_strncpy (gsi,
3404 gimple_call_arg (stmt, 0),
3405 gimple_call_arg (stmt, 1),
3406 gimple_call_arg (stmt, 2));
3407 case BUILT_IN_STRCAT:
3408 return gimple_fold_builtin_strcat (gsi, gimple_call_arg (stmt, 0),
3409 gimple_call_arg (stmt, 1));
3410 case BUILT_IN_STRNCAT:
3411 return gimple_fold_builtin_strncat (gsi);
3412 case BUILT_IN_INDEX:
3413 case BUILT_IN_STRCHR:
3414 return gimple_fold_builtin_strchr (gsi, false);
3415 case BUILT_IN_RINDEX:
3416 case BUILT_IN_STRRCHR:
3417 return gimple_fold_builtin_strchr (gsi, true);
3418 case BUILT_IN_STRSTR:
3419 return gimple_fold_builtin_strstr (gsi);
3420 case BUILT_IN_STRCMP:
3421 case BUILT_IN_STRCASECMP:
3422 case BUILT_IN_STRNCMP:
3423 case BUILT_IN_STRNCASECMP:
3424 return gimple_fold_builtin_string_compare (gsi);
3425 case BUILT_IN_MEMCHR:
3426 return gimple_fold_builtin_memchr (gsi);
3427 case BUILT_IN_FPUTS:
3428 return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
3429 gimple_call_arg (stmt, 1), false);
3430 case BUILT_IN_FPUTS_UNLOCKED:
3431 return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
3432 gimple_call_arg (stmt, 1), true);
3433 case BUILT_IN_MEMCPY_CHK:
3434 case BUILT_IN_MEMPCPY_CHK:
3435 case BUILT_IN_MEMMOVE_CHK:
3436 case BUILT_IN_MEMSET_CHK:
3437 return gimple_fold_builtin_memory_chk (gsi,
3438 gimple_call_arg (stmt, 0),
3439 gimple_call_arg (stmt, 1),
3440 gimple_call_arg (stmt, 2),
3441 gimple_call_arg (stmt, 3),
3442 fcode);
3443 case BUILT_IN_STPCPY:
3444 return gimple_fold_builtin_stpcpy (gsi);
3445 case BUILT_IN_STRCPY_CHK:
3446 case BUILT_IN_STPCPY_CHK:
3447 return gimple_fold_builtin_stxcpy_chk (gsi,
3448 gimple_call_arg (stmt, 0),
3449 gimple_call_arg (stmt, 1),
3450 gimple_call_arg (stmt, 2),
3451 fcode);
3452 case BUILT_IN_STRNCPY_CHK:
3453 case BUILT_IN_STPNCPY_CHK:
3454 return gimple_fold_builtin_stxncpy_chk (gsi,
3455 gimple_call_arg (stmt, 0),
3456 gimple_call_arg (stmt, 1),
3457 gimple_call_arg (stmt, 2),
3458 gimple_call_arg (stmt, 3),
3459 fcode);
3460 case BUILT_IN_SNPRINTF_CHK:
3461 case BUILT_IN_VSNPRINTF_CHK:
3462 return gimple_fold_builtin_snprintf_chk (gsi, fcode);
3464 case BUILT_IN_FPRINTF:
3465 case BUILT_IN_FPRINTF_UNLOCKED:
3466 case BUILT_IN_VFPRINTF:
3467 if (n == 2 || n == 3)
3468 return gimple_fold_builtin_fprintf (gsi,
3469 gimple_call_arg (stmt, 0),
3470 gimple_call_arg (stmt, 1),
3471 n == 3
3472 ? gimple_call_arg (stmt, 2)
3473 : NULL_TREE,
3474 fcode);
3475 break;
3476 case BUILT_IN_FPRINTF_CHK:
3477 case BUILT_IN_VFPRINTF_CHK:
3478 if (n == 3 || n == 4)
3479 return gimple_fold_builtin_fprintf (gsi,
3480 gimple_call_arg (stmt, 0),
3481 gimple_call_arg (stmt, 2),
3482 n == 4
3483 ? gimple_call_arg (stmt, 3)
3484 : NULL_TREE,
3485 fcode);
3486 break;
3487 case BUILT_IN_PRINTF:
3488 case BUILT_IN_PRINTF_UNLOCKED:
3489 case BUILT_IN_VPRINTF:
3490 if (n == 1 || n == 2)
3491 return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 0),
3492 n == 2
3493 ? gimple_call_arg (stmt, 1)
3494 : NULL_TREE, fcode);
3495 break;
3496 case BUILT_IN_PRINTF_CHK:
3497 case BUILT_IN_VPRINTF_CHK:
3498 if (n == 2 || n == 3)
3499 return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 1),
3500 n == 3
3501 ? gimple_call_arg (stmt, 2)
3502 : NULL_TREE, fcode);
3503 break;
3504 case BUILT_IN_ACC_ON_DEVICE:
3505 return gimple_fold_builtin_acc_on_device (gsi,
3506 gimple_call_arg (stmt, 0));
3507 case BUILT_IN_REALLOC:
3508 return gimple_fold_builtin_realloc (gsi);
3510 default:;
3513 /* Try the generic builtin folder. */
3514 bool ignore = (gimple_call_lhs (stmt) == NULL);
3515 tree result = fold_call_stmt (stmt, ignore);
3516 if (result)
3518 if (ignore)
3519 STRIP_NOPS (result);
3520 else
3521 result = fold_convert (gimple_call_return_type (stmt), result);
3522 if (!update_call_from_tree (gsi, result))
3523 gimplify_and_update_call_from_tree (gsi, result);
3524 return true;
3527 return false;
3530 /* Transform IFN_GOACC_DIM_SIZE and IFN_GOACC_DIM_POS internal
3531 function calls to constants, where possible. */
3533 static tree
3534 fold_internal_goacc_dim (const gimple *call)
3536 int axis = oacc_get_ifn_dim_arg (call);
3537 int size = oacc_get_fn_dim_size (current_function_decl, axis);
3538 bool is_pos = gimple_call_internal_fn (call) == IFN_GOACC_DIM_POS;
3539 tree result = NULL_TREE;
3541 /* If the size is 1, or we only want the size and it is not dynamic,
3542 we know the answer. */
3543 if (size == 1 || (!is_pos && size))
3545 tree type = TREE_TYPE (gimple_call_lhs (call));
3546 result = build_int_cst (type, size - is_pos);
3549 return result;
3552 /* Return true if stmt is __atomic_compare_exchange_N call which is suitable
3553 for conversion into ATOMIC_COMPARE_EXCHANGE if the second argument is
3554 &var where var is only addressable because of such calls. */
3556 bool
3557 optimize_atomic_compare_exchange_p (gimple *stmt)
3559 if (gimple_call_num_args (stmt) != 6
3560 || !flag_inline_atomics
3561 || !optimize
3562 || sanitize_flags_p (SANITIZE_THREAD | SANITIZE_ADDRESS)
3563 || !gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)
3564 || !gimple_vdef (stmt)
3565 || !gimple_vuse (stmt))
3566 return false;
3568 tree fndecl = gimple_call_fndecl (stmt);
3569 switch (DECL_FUNCTION_CODE (fndecl))
3571 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
3572 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
3573 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
3574 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
3575 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
3576 break;
3577 default:
3578 return false;
3581 tree expected = gimple_call_arg (stmt, 1);
3582 if (TREE_CODE (expected) != ADDR_EXPR
3583 || !SSA_VAR_P (TREE_OPERAND (expected, 0)))
3584 return false;
3586 tree etype = TREE_TYPE (TREE_OPERAND (expected, 0));
3587 if (!is_gimple_reg_type (etype)
3588 || !auto_var_in_fn_p (TREE_OPERAND (expected, 0), current_function_decl)
3589 || TREE_THIS_VOLATILE (etype)
3590 || VECTOR_TYPE_P (etype)
3591 || TREE_CODE (etype) == COMPLEX_TYPE
3592 /* Don't optimize floating point expected vars, VIEW_CONVERT_EXPRs
3593 might not preserve all the bits. See PR71716. */
3594 || SCALAR_FLOAT_TYPE_P (etype)
3595 || TYPE_PRECISION (etype) != GET_MODE_BITSIZE (TYPE_MODE (etype)))
3596 return false;
3598 tree weak = gimple_call_arg (stmt, 3);
3599 if (!integer_zerop (weak) && !integer_onep (weak))
3600 return false;
3602 tree parmt = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3603 tree itype = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt)));
3604 machine_mode mode = TYPE_MODE (itype);
3606 if (direct_optab_handler (atomic_compare_and_swap_optab, mode)
3607 == CODE_FOR_nothing
3608 && optab_handler (sync_compare_and_swap_optab, mode) == CODE_FOR_nothing)
3609 return false;
3611 if (int_size_in_bytes (etype) != GET_MODE_SIZE (mode))
3612 return false;
3614 return true;
3617 /* Fold
3618 r = __atomic_compare_exchange_N (p, &e, d, w, s, f);
3619 into
3620 _Complex uintN_t t = ATOMIC_COMPARE_EXCHANGE (p, e, d, w * 256 + N, s, f);
3621 i = IMAGPART_EXPR <t>;
3622 r = (_Bool) i;
3623 e = REALPART_EXPR <t>; */
3625 void
3626 fold_builtin_atomic_compare_exchange (gimple_stmt_iterator *gsi)
3628 gimple *stmt = gsi_stmt (*gsi);
3629 tree fndecl = gimple_call_fndecl (stmt);
3630 tree parmt = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3631 tree itype = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt)));
3632 tree ctype = build_complex_type (itype);
3633 tree expected = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
3634 bool throws = false;
3635 edge e = NULL;
3636 gimple *g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)),
3637 expected);
3638 gsi_insert_before (gsi, g, GSI_SAME_STMT);
3639 gimple_stmt_iterator gsiret = gsi_for_stmt (g);
3640 if (!useless_type_conversion_p (itype, TREE_TYPE (expected)))
3642 g = gimple_build_assign (make_ssa_name (itype), VIEW_CONVERT_EXPR,
3643 build1 (VIEW_CONVERT_EXPR, itype,
3644 gimple_assign_lhs (g)));
3645 gsi_insert_before (gsi, g, GSI_SAME_STMT);
3647 int flag = (integer_onep (gimple_call_arg (stmt, 3)) ? 256 : 0)
3648 + int_size_in_bytes (itype);
3649 g = gimple_build_call_internal (IFN_ATOMIC_COMPARE_EXCHANGE, 6,
3650 gimple_call_arg (stmt, 0),
3651 gimple_assign_lhs (g),
3652 gimple_call_arg (stmt, 2),
3653 build_int_cst (integer_type_node, flag),
3654 gimple_call_arg (stmt, 4),
3655 gimple_call_arg (stmt, 5));
3656 tree lhs = make_ssa_name (ctype);
3657 gimple_call_set_lhs (g, lhs);
3658 gimple_set_vdef (g, gimple_vdef (stmt));
3659 gimple_set_vuse (g, gimple_vuse (stmt));
3660 SSA_NAME_DEF_STMT (gimple_vdef (g)) = g;
3661 tree oldlhs = gimple_call_lhs (stmt);
3662 if (stmt_can_throw_internal (stmt))
3664 throws = true;
3665 e = find_fallthru_edge (gsi_bb (*gsi)->succs);
3667 gimple_call_set_nothrow (as_a <gcall *> (g),
3668 gimple_call_nothrow_p (as_a <gcall *> (stmt)));
3669 gimple_call_set_lhs (stmt, NULL_TREE);
3670 gsi_replace (gsi, g, true);
3671 if (oldlhs)
3673 g = gimple_build_assign (make_ssa_name (itype), IMAGPART_EXPR,
3674 build1 (IMAGPART_EXPR, itype, lhs));
3675 if (throws)
3677 gsi_insert_on_edge_immediate (e, g);
3678 *gsi = gsi_for_stmt (g);
3680 else
3681 gsi_insert_after (gsi, g, GSI_NEW_STMT);
3682 g = gimple_build_assign (oldlhs, NOP_EXPR, gimple_assign_lhs (g));
3683 gsi_insert_after (gsi, g, GSI_NEW_STMT);
3685 g = gimple_build_assign (make_ssa_name (itype), REALPART_EXPR,
3686 build1 (REALPART_EXPR, itype, lhs));
3687 if (throws && oldlhs == NULL_TREE)
3689 gsi_insert_on_edge_immediate (e, g);
3690 *gsi = gsi_for_stmt (g);
3692 else
3693 gsi_insert_after (gsi, g, GSI_NEW_STMT);
3694 if (!useless_type_conversion_p (TREE_TYPE (expected), itype))
3696 g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)),
3697 VIEW_CONVERT_EXPR,
3698 build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expected),
3699 gimple_assign_lhs (g)));
3700 gsi_insert_after (gsi, g, GSI_NEW_STMT);
3702 g = gimple_build_assign (expected, SSA_NAME, gimple_assign_lhs (g));
3703 gsi_insert_after (gsi, g, GSI_NEW_STMT);
3704 *gsi = gsiret;
3707 /* Return true if ARG0 CODE ARG1 in infinite signed precision operation
3708 doesn't fit into TYPE. The test for overflow should be regardless of
3709 -fwrapv, and even for unsigned types. */
3711 bool
3712 arith_overflowed_p (enum tree_code code, const_tree type,
3713 const_tree arg0, const_tree arg1)
3715 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) widest2_int;
3716 typedef generic_wide_int <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> >
3717 widest2_int_cst;
3718 widest2_int warg0 = widest2_int_cst (arg0);
3719 widest2_int warg1 = widest2_int_cst (arg1);
3720 widest2_int wres;
3721 switch (code)
3723 case PLUS_EXPR: wres = wi::add (warg0, warg1); break;
3724 case MINUS_EXPR: wres = wi::sub (warg0, warg1); break;
3725 case MULT_EXPR: wres = wi::mul (warg0, warg1); break;
3726 default: gcc_unreachable ();
3728 signop sign = TYPE_SIGN (type);
3729 if (sign == UNSIGNED && wi::neg_p (wres))
3730 return true;
3731 return wi::min_precision (wres, sign) > TYPE_PRECISION (type);
3734 /* Attempt to fold a call statement referenced by the statement iterator GSI.
3735 The statement may be replaced by another statement, e.g., if the call
3736 simplifies to a constant value. Return true if any changes were made.
3737 It is assumed that the operands have been previously folded. */
3739 static bool
3740 gimple_fold_call (gimple_stmt_iterator *gsi, bool inplace)
3742 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3743 tree callee;
3744 bool changed = false;
3745 unsigned i;
3747 /* Fold *& in call arguments. */
3748 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3749 if (REFERENCE_CLASS_P (gimple_call_arg (stmt, i)))
3751 tree tmp = maybe_fold_reference (gimple_call_arg (stmt, i), false);
3752 if (tmp)
3754 gimple_call_set_arg (stmt, i, tmp);
3755 changed = true;
3759 /* Check for virtual calls that became direct calls. */
3760 callee = gimple_call_fn (stmt);
3761 if (callee && TREE_CODE (callee) == OBJ_TYPE_REF)
3763 if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee)) != NULL_TREE)
3765 if (dump_file && virtual_method_call_p (callee)
3766 && !possible_polymorphic_call_target_p
3767 (callee, stmt, cgraph_node::get (gimple_call_addr_fndecl
3768 (OBJ_TYPE_REF_EXPR (callee)))))
3770 fprintf (dump_file,
3771 "Type inheritance inconsistent devirtualization of ");
3772 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
3773 fprintf (dump_file, " to ");
3774 print_generic_expr (dump_file, callee, TDF_SLIM);
3775 fprintf (dump_file, "\n");
3778 gimple_call_set_fn (stmt, OBJ_TYPE_REF_EXPR (callee));
3779 changed = true;
3781 else if (flag_devirtualize && !inplace && virtual_method_call_p (callee))
3783 bool final;
3784 vec <cgraph_node *>targets
3785 = possible_polymorphic_call_targets (callee, stmt, &final);
3786 if (final && targets.length () <= 1 && dbg_cnt (devirt))
3788 tree lhs = gimple_call_lhs (stmt);
3789 if (dump_enabled_p ())
3791 location_t loc = gimple_location_safe (stmt);
3792 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
3793 "folding virtual function call to %s\n",
3794 targets.length () == 1
3795 ? targets[0]->name ()
3796 : "__builtin_unreachable");
3798 if (targets.length () == 1)
3800 tree fndecl = targets[0]->decl;
3801 gimple_call_set_fndecl (stmt, fndecl);
3802 changed = true;
3803 /* If changing the call to __cxa_pure_virtual
3804 or similar noreturn function, adjust gimple_call_fntype
3805 too. */
3806 if (gimple_call_noreturn_p (stmt)
3807 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl)))
3808 && TYPE_ARG_TYPES (TREE_TYPE (fndecl))
3809 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
3810 == void_type_node))
3811 gimple_call_set_fntype (stmt, TREE_TYPE (fndecl));
3812 /* If the call becomes noreturn, remove the lhs. */
3813 if (lhs
3814 && gimple_call_noreturn_p (stmt)
3815 && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (stmt)))
3816 || should_remove_lhs_p (lhs)))
3818 if (TREE_CODE (lhs) == SSA_NAME)
3820 tree var = create_tmp_var (TREE_TYPE (lhs));
3821 tree def = get_or_create_ssa_default_def (cfun, var);
3822 gimple *new_stmt = gimple_build_assign (lhs, def);
3823 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
3825 gimple_call_set_lhs (stmt, NULL_TREE);
3827 maybe_remove_unused_call_args (cfun, stmt);
3829 else
3831 tree fndecl = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
3832 gimple *new_stmt = gimple_build_call (fndecl, 0);
3833 gimple_set_location (new_stmt, gimple_location (stmt));
3834 if (lhs && TREE_CODE (lhs) == SSA_NAME)
3836 tree var = create_tmp_var (TREE_TYPE (lhs));
3837 tree def = get_or_create_ssa_default_def (cfun, var);
3839 /* To satisfy condition for
3840 cgraph_update_edges_for_call_stmt_node,
3841 we need to preserve GIMPLE_CALL statement
3842 at position of GSI iterator. */
3843 update_call_from_tree (gsi, def);
3844 gsi_insert_before (gsi, new_stmt, GSI_NEW_STMT);
3846 else
3848 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
3849 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
3850 gsi_replace (gsi, new_stmt, false);
3852 return true;
3858 /* Check for indirect calls that became direct calls, and then
3859 no longer require a static chain. */
3860 if (gimple_call_chain (stmt))
3862 tree fn = gimple_call_fndecl (stmt);
3863 if (fn && !DECL_STATIC_CHAIN (fn))
3865 gimple_call_set_chain (stmt, NULL);
3866 changed = true;
3868 else
3870 tree tmp = maybe_fold_reference (gimple_call_chain (stmt), false);
3871 if (tmp)
3873 gimple_call_set_chain (stmt, tmp);
3874 changed = true;
3879 if (inplace)
3880 return changed;
3882 /* Check for builtins that CCP can handle using information not
3883 available in the generic fold routines. */
3884 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3886 if (gimple_fold_builtin (gsi))
3887 changed = true;
3889 else if (gimple_call_builtin_p (stmt, BUILT_IN_MD))
3891 changed |= targetm.gimple_fold_builtin (gsi);
3893 else if (gimple_call_internal_p (stmt))
3895 enum tree_code subcode = ERROR_MARK;
3896 tree result = NULL_TREE;
3897 bool cplx_result = false;
3898 tree overflow = NULL_TREE;
3899 switch (gimple_call_internal_fn (stmt))
3901 case IFN_BUILTIN_EXPECT:
3902 result = fold_builtin_expect (gimple_location (stmt),
3903 gimple_call_arg (stmt, 0),
3904 gimple_call_arg (stmt, 1),
3905 gimple_call_arg (stmt, 2));
3906 break;
3907 case IFN_UBSAN_OBJECT_SIZE:
3908 if (integer_all_onesp (gimple_call_arg (stmt, 2))
3909 || (TREE_CODE (gimple_call_arg (stmt, 1)) == INTEGER_CST
3910 && TREE_CODE (gimple_call_arg (stmt, 2)) == INTEGER_CST
3911 && tree_int_cst_le (gimple_call_arg (stmt, 1),
3912 gimple_call_arg (stmt, 2))))
3914 gsi_replace (gsi, gimple_build_nop (), false);
3915 unlink_stmt_vdef (stmt);
3916 release_defs (stmt);
3917 return true;
3919 break;
3920 case IFN_GOACC_DIM_SIZE:
3921 case IFN_GOACC_DIM_POS:
3922 result = fold_internal_goacc_dim (stmt);
3923 break;
3924 case IFN_UBSAN_CHECK_ADD:
3925 subcode = PLUS_EXPR;
3926 break;
3927 case IFN_UBSAN_CHECK_SUB:
3928 subcode = MINUS_EXPR;
3929 break;
3930 case IFN_UBSAN_CHECK_MUL:
3931 subcode = MULT_EXPR;
3932 break;
3933 case IFN_ADD_OVERFLOW:
3934 subcode = PLUS_EXPR;
3935 cplx_result = true;
3936 break;
3937 case IFN_SUB_OVERFLOW:
3938 subcode = MINUS_EXPR;
3939 cplx_result = true;
3940 break;
3941 case IFN_MUL_OVERFLOW:
3942 subcode = MULT_EXPR;
3943 cplx_result = true;
3944 break;
3945 default:
3946 break;
3948 if (subcode != ERROR_MARK)
3950 tree arg0 = gimple_call_arg (stmt, 0);
3951 tree arg1 = gimple_call_arg (stmt, 1);
3952 tree type = TREE_TYPE (arg0);
3953 if (cplx_result)
3955 tree lhs = gimple_call_lhs (stmt);
3956 if (lhs == NULL_TREE)
3957 type = NULL_TREE;
3958 else
3959 type = TREE_TYPE (TREE_TYPE (lhs));
3961 if (type == NULL_TREE)
3963 /* x = y + 0; x = y - 0; x = y * 0; */
3964 else if (integer_zerop (arg1))
3965 result = subcode == MULT_EXPR ? integer_zero_node : arg0;
3966 /* x = 0 + y; x = 0 * y; */
3967 else if (subcode != MINUS_EXPR && integer_zerop (arg0))
3968 result = subcode == MULT_EXPR ? integer_zero_node : arg1;
3969 /* x = y - y; */
3970 else if (subcode == MINUS_EXPR && operand_equal_p (arg0, arg1, 0))
3971 result = integer_zero_node;
3972 /* x = y * 1; x = 1 * y; */
3973 else if (subcode == MULT_EXPR && integer_onep (arg1))
3974 result = arg0;
3975 else if (subcode == MULT_EXPR && integer_onep (arg0))
3976 result = arg1;
3977 else if (TREE_CODE (arg0) == INTEGER_CST
3978 && TREE_CODE (arg1) == INTEGER_CST)
3980 if (cplx_result)
3981 result = int_const_binop (subcode, fold_convert (type, arg0),
3982 fold_convert (type, arg1));
3983 else
3984 result = int_const_binop (subcode, arg0, arg1);
3985 if (result && arith_overflowed_p (subcode, type, arg0, arg1))
3987 if (cplx_result)
3988 overflow = build_one_cst (type);
3989 else
3990 result = NULL_TREE;
3993 if (result)
3995 if (result == integer_zero_node)
3996 result = build_zero_cst (type);
3997 else if (cplx_result && TREE_TYPE (result) != type)
3999 if (TREE_CODE (result) == INTEGER_CST)
4001 if (arith_overflowed_p (PLUS_EXPR, type, result,
4002 integer_zero_node))
4003 overflow = build_one_cst (type);
4005 else if ((!TYPE_UNSIGNED (TREE_TYPE (result))
4006 && TYPE_UNSIGNED (type))
4007 || (TYPE_PRECISION (type)
4008 < (TYPE_PRECISION (TREE_TYPE (result))
4009 + (TYPE_UNSIGNED (TREE_TYPE (result))
4010 && !TYPE_UNSIGNED (type)))))
4011 result = NULL_TREE;
4012 if (result)
4013 result = fold_convert (type, result);
4018 if (result)
4020 if (TREE_CODE (result) == INTEGER_CST && TREE_OVERFLOW (result))
4021 result = drop_tree_overflow (result);
4022 if (cplx_result)
4024 if (overflow == NULL_TREE)
4025 overflow = build_zero_cst (TREE_TYPE (result));
4026 tree ctype = build_complex_type (TREE_TYPE (result));
4027 if (TREE_CODE (result) == INTEGER_CST
4028 && TREE_CODE (overflow) == INTEGER_CST)
4029 result = build_complex (ctype, result, overflow);
4030 else
4031 result = build2_loc (gimple_location (stmt), COMPLEX_EXPR,
4032 ctype, result, overflow);
4034 if (!update_call_from_tree (gsi, result))
4035 gimplify_and_update_call_from_tree (gsi, result);
4036 changed = true;
4040 return changed;
4044 /* Return true whether NAME has a use on STMT. */
4046 static bool
4047 has_use_on_stmt (tree name, gimple *stmt)
4049 imm_use_iterator iter;
4050 use_operand_p use_p;
4051 FOR_EACH_IMM_USE_FAST (use_p, iter, name)
4052 if (USE_STMT (use_p) == stmt)
4053 return true;
4054 return false;
4057 /* Worker for fold_stmt_1 dispatch to pattern based folding with
4058 gimple_simplify.
4060 Replaces *GSI with the simplification result in RCODE and OPS
4061 and the associated statements in *SEQ. Does the replacement
4062 according to INPLACE and returns true if the operation succeeded. */
4064 static bool
4065 replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
4066 code_helper rcode, tree *ops,
4067 gimple_seq *seq, bool inplace)
4069 gimple *stmt = gsi_stmt (*gsi);
4071 /* Play safe and do not allow abnormals to be mentioned in
4072 newly created statements. See also maybe_push_res_to_seq.
4073 As an exception allow such uses if there was a use of the
4074 same SSA name on the old stmt. */
4075 if ((TREE_CODE (ops[0]) == SSA_NAME
4076 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[0])
4077 && !has_use_on_stmt (ops[0], stmt))
4078 || (ops[1]
4079 && TREE_CODE (ops[1]) == SSA_NAME
4080 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[1])
4081 && !has_use_on_stmt (ops[1], stmt))
4082 || (ops[2]
4083 && TREE_CODE (ops[2]) == SSA_NAME
4084 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[2])
4085 && !has_use_on_stmt (ops[2], stmt))
4086 || (COMPARISON_CLASS_P (ops[0])
4087 && ((TREE_CODE (TREE_OPERAND (ops[0], 0)) == SSA_NAME
4088 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops[0], 0))
4089 && !has_use_on_stmt (TREE_OPERAND (ops[0], 0), stmt))
4090 || (TREE_CODE (TREE_OPERAND (ops[0], 1)) == SSA_NAME
4091 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops[0], 1))
4092 && !has_use_on_stmt (TREE_OPERAND (ops[0], 1), stmt)))))
4093 return false;
4095 /* Don't insert new statements when INPLACE is true, even if we could
4096 reuse STMT for the final statement. */
4097 if (inplace && !gimple_seq_empty_p (*seq))
4098 return false;
4100 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
4102 gcc_assert (rcode.is_tree_code ());
4103 if (TREE_CODE_CLASS ((enum tree_code)rcode) == tcc_comparison
4104 /* GIMPLE_CONDs condition may not throw. */
4105 && (!flag_exceptions
4106 || !cfun->can_throw_non_call_exceptions
4107 || !operation_could_trap_p (rcode,
4108 FLOAT_TYPE_P (TREE_TYPE (ops[0])),
4109 false, NULL_TREE)))
4110 gimple_cond_set_condition (cond_stmt, rcode, ops[0], ops[1]);
4111 else if (rcode == SSA_NAME)
4112 gimple_cond_set_condition (cond_stmt, NE_EXPR, ops[0],
4113 build_zero_cst (TREE_TYPE (ops[0])));
4114 else if (rcode == INTEGER_CST)
4116 if (integer_zerop (ops[0]))
4117 gimple_cond_make_false (cond_stmt);
4118 else
4119 gimple_cond_make_true (cond_stmt);
4121 else if (!inplace)
4123 tree res = maybe_push_res_to_seq (rcode, boolean_type_node,
4124 ops, seq);
4125 if (!res)
4126 return false;
4127 gimple_cond_set_condition (cond_stmt, NE_EXPR, res,
4128 build_zero_cst (TREE_TYPE (res)));
4130 else
4131 return false;
4132 if (dump_file && (dump_flags & TDF_DETAILS))
4134 fprintf (dump_file, "gimple_simplified to ");
4135 if (!gimple_seq_empty_p (*seq))
4136 print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
4137 print_gimple_stmt (dump_file, gsi_stmt (*gsi),
4138 0, TDF_SLIM);
4140 gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
4141 return true;
4143 else if (is_gimple_assign (stmt)
4144 && rcode.is_tree_code ())
4146 if (!inplace
4147 || gimple_num_ops (stmt) > get_gimple_rhs_num_ops (rcode))
4149 maybe_build_generic_op (rcode,
4150 TREE_TYPE (gimple_assign_lhs (stmt)), ops);
4151 gimple_assign_set_rhs_with_ops (gsi, rcode, ops[0], ops[1], ops[2]);
4152 if (dump_file && (dump_flags & TDF_DETAILS))
4154 fprintf (dump_file, "gimple_simplified to ");
4155 if (!gimple_seq_empty_p (*seq))
4156 print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
4157 print_gimple_stmt (dump_file, gsi_stmt (*gsi),
4158 0, TDF_SLIM);
4160 gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
4161 return true;
4164 else if (rcode.is_fn_code ()
4165 && gimple_call_combined_fn (stmt) == rcode)
4167 unsigned i;
4168 for (i = 0; i < gimple_call_num_args (stmt); ++i)
4170 gcc_assert (ops[i] != NULL_TREE);
4171 gimple_call_set_arg (stmt, i, ops[i]);
4173 if (i < 3)
4174 gcc_assert (ops[i] == NULL_TREE);
4175 if (dump_file && (dump_flags & TDF_DETAILS))
4177 fprintf (dump_file, "gimple_simplified to ");
4178 if (!gimple_seq_empty_p (*seq))
4179 print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
4180 print_gimple_stmt (dump_file, gsi_stmt (*gsi), 0, TDF_SLIM);
4182 gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
4183 return true;
4185 else if (!inplace)
4187 if (gimple_has_lhs (stmt))
4189 tree lhs = gimple_get_lhs (stmt);
4190 if (!maybe_push_res_to_seq (rcode, TREE_TYPE (lhs),
4191 ops, seq, lhs))
4192 return false;
4193 if (dump_file && (dump_flags & TDF_DETAILS))
4195 fprintf (dump_file, "gimple_simplified to ");
4196 print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
4198 gsi_replace_with_seq_vops (gsi, *seq);
4199 return true;
4201 else
4202 gcc_unreachable ();
4205 return false;
4208 /* Canonicalize MEM_REFs invariant address operand after propagation. */
4210 static bool
4211 maybe_canonicalize_mem_ref_addr (tree *t)
4213 bool res = false;
4215 if (TREE_CODE (*t) == ADDR_EXPR)
4216 t = &TREE_OPERAND (*t, 0);
4218 /* The C and C++ frontends use an ARRAY_REF for indexing with their
4219 generic vector extension. The actual vector referenced is
4220 view-converted to an array type for this purpose. If the index
4221 is constant the canonical representation in the middle-end is a
4222 BIT_FIELD_REF so re-write the former to the latter here. */
4223 if (TREE_CODE (*t) == ARRAY_REF
4224 && TREE_CODE (TREE_OPERAND (*t, 0)) == VIEW_CONVERT_EXPR
4225 && TREE_CODE (TREE_OPERAND (*t, 1)) == INTEGER_CST
4226 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0))))
4228 tree vtype = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0));
4229 if (VECTOR_TYPE_P (vtype))
4231 tree low = array_ref_low_bound (*t);
4232 if (TREE_CODE (low) == INTEGER_CST)
4234 if (tree_int_cst_le (low, TREE_OPERAND (*t, 1)))
4236 widest_int idx = wi::sub (wi::to_widest (TREE_OPERAND (*t, 1)),
4237 wi::to_widest (low));
4238 idx = wi::mul (idx, wi::to_widest
4239 (TYPE_SIZE (TREE_TYPE (*t))));
4240 widest_int ext
4241 = wi::add (idx, wi::to_widest (TYPE_SIZE (TREE_TYPE (*t))));
4242 if (wi::les_p (ext, wi::to_widest (TYPE_SIZE (vtype))))
4244 *t = build3_loc (EXPR_LOCATION (*t), BIT_FIELD_REF,
4245 TREE_TYPE (*t),
4246 TREE_OPERAND (TREE_OPERAND (*t, 0), 0),
4247 TYPE_SIZE (TREE_TYPE (*t)),
4248 wide_int_to_tree (bitsizetype, idx));
4249 res = true;
4256 while (handled_component_p (*t))
4257 t = &TREE_OPERAND (*t, 0);
4259 /* Canonicalize MEM [&foo.bar, 0] which appears after propagating
4260 of invariant addresses into a SSA name MEM_REF address. */
4261 if (TREE_CODE (*t) == MEM_REF
4262 || TREE_CODE (*t) == TARGET_MEM_REF)
4264 tree addr = TREE_OPERAND (*t, 0);
4265 if (TREE_CODE (addr) == ADDR_EXPR
4266 && (TREE_CODE (TREE_OPERAND (addr, 0)) == MEM_REF
4267 || handled_component_p (TREE_OPERAND (addr, 0))))
4269 tree base;
4270 HOST_WIDE_INT coffset;
4271 base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
4272 &coffset);
4273 if (!base)
4274 gcc_unreachable ();
4276 TREE_OPERAND (*t, 0) = build_fold_addr_expr (base);
4277 TREE_OPERAND (*t, 1) = int_const_binop (PLUS_EXPR,
4278 TREE_OPERAND (*t, 1),
4279 size_int (coffset));
4280 res = true;
4282 gcc_checking_assert (TREE_CODE (TREE_OPERAND (*t, 0)) == DEBUG_EXPR_DECL
4283 || is_gimple_mem_ref_addr (TREE_OPERAND (*t, 0)));
4286 /* Canonicalize back MEM_REFs to plain reference trees if the object
4287 accessed is a decl that has the same access semantics as the MEM_REF. */
4288 if (TREE_CODE (*t) == MEM_REF
4289 && TREE_CODE (TREE_OPERAND (*t, 0)) == ADDR_EXPR
4290 && integer_zerop (TREE_OPERAND (*t, 1))
4291 && MR_DEPENDENCE_CLIQUE (*t) == 0)
4293 tree decl = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
4294 tree alias_type = TREE_TYPE (TREE_OPERAND (*t, 1));
4295 if (/* Same volatile qualification. */
4296 TREE_THIS_VOLATILE (*t) == TREE_THIS_VOLATILE (decl)
4297 /* Same TBAA behavior with -fstrict-aliasing. */
4298 && !TYPE_REF_CAN_ALIAS_ALL (alias_type)
4299 && (TYPE_MAIN_VARIANT (TREE_TYPE (decl))
4300 == TYPE_MAIN_VARIANT (TREE_TYPE (alias_type)))
4301 /* Same alignment. */
4302 && TYPE_ALIGN (TREE_TYPE (decl)) == TYPE_ALIGN (TREE_TYPE (*t))
4303 /* We have to look out here to not drop a required conversion
4304 from the rhs to the lhs if *t appears on the lhs or vice-versa
4305 if it appears on the rhs. Thus require strict type
4306 compatibility. */
4307 && types_compatible_p (TREE_TYPE (*t), TREE_TYPE (decl)))
4309 *t = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
4310 res = true;
4314 /* Canonicalize TARGET_MEM_REF in particular with respect to
4315 the indexes becoming constant. */
4316 else if (TREE_CODE (*t) == TARGET_MEM_REF)
4318 tree tem = maybe_fold_tmr (*t);
4319 if (tem)
4321 *t = tem;
4322 res = true;
4326 return res;
4329 /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument
4330 distinguishes both cases. */
4332 static bool
4333 fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace, tree (*valueize) (tree))
4335 bool changed = false;
4336 gimple *stmt = gsi_stmt (*gsi);
4337 bool nowarning = gimple_no_warning_p (stmt);
4338 unsigned i;
4339 fold_defer_overflow_warnings ();
4341 /* First do required canonicalization of [TARGET_]MEM_REF addresses
4342 after propagation.
4343 ??? This shouldn't be done in generic folding but in the
4344 propagation helpers which also know whether an address was
4345 propagated.
4346 Also canonicalize operand order. */
4347 switch (gimple_code (stmt))
4349 case GIMPLE_ASSIGN:
4350 if (gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
4352 tree *rhs = gimple_assign_rhs1_ptr (stmt);
4353 if ((REFERENCE_CLASS_P (*rhs)
4354 || TREE_CODE (*rhs) == ADDR_EXPR)
4355 && maybe_canonicalize_mem_ref_addr (rhs))
4356 changed = true;
4357 tree *lhs = gimple_assign_lhs_ptr (stmt);
4358 if (REFERENCE_CLASS_P (*lhs)
4359 && maybe_canonicalize_mem_ref_addr (lhs))
4360 changed = true;
4362 else
4364 /* Canonicalize operand order. */
4365 enum tree_code code = gimple_assign_rhs_code (stmt);
4366 if (TREE_CODE_CLASS (code) == tcc_comparison
4367 || commutative_tree_code (code)
4368 || commutative_ternary_tree_code (code))
4370 tree rhs1 = gimple_assign_rhs1 (stmt);
4371 tree rhs2 = gimple_assign_rhs2 (stmt);
4372 if (tree_swap_operands_p (rhs1, rhs2))
4374 gimple_assign_set_rhs1 (stmt, rhs2);
4375 gimple_assign_set_rhs2 (stmt, rhs1);
4376 if (TREE_CODE_CLASS (code) == tcc_comparison)
4377 gimple_assign_set_rhs_code (stmt,
4378 swap_tree_comparison (code));
4379 changed = true;
4383 break;
4384 case GIMPLE_CALL:
4386 for (i = 0; i < gimple_call_num_args (stmt); ++i)
4388 tree *arg = gimple_call_arg_ptr (stmt, i);
4389 if (REFERENCE_CLASS_P (*arg)
4390 && maybe_canonicalize_mem_ref_addr (arg))
4391 changed = true;
4393 tree *lhs = gimple_call_lhs_ptr (stmt);
4394 if (*lhs
4395 && REFERENCE_CLASS_P (*lhs)
4396 && maybe_canonicalize_mem_ref_addr (lhs))
4397 changed = true;
4398 break;
4400 case GIMPLE_ASM:
4402 gasm *asm_stmt = as_a <gasm *> (stmt);
4403 for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
4405 tree link = gimple_asm_output_op (asm_stmt, i);
4406 tree op = TREE_VALUE (link);
4407 if (REFERENCE_CLASS_P (op)
4408 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
4409 changed = true;
4411 for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
4413 tree link = gimple_asm_input_op (asm_stmt, i);
4414 tree op = TREE_VALUE (link);
4415 if ((REFERENCE_CLASS_P (op)
4416 || TREE_CODE (op) == ADDR_EXPR)
4417 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
4418 changed = true;
4421 break;
4422 case GIMPLE_DEBUG:
4423 if (gimple_debug_bind_p (stmt))
4425 tree *val = gimple_debug_bind_get_value_ptr (stmt);
4426 if (*val
4427 && (REFERENCE_CLASS_P (*val)
4428 || TREE_CODE (*val) == ADDR_EXPR)
4429 && maybe_canonicalize_mem_ref_addr (val))
4430 changed = true;
4432 break;
4433 case GIMPLE_COND:
4435 /* Canonicalize operand order. */
4436 tree lhs = gimple_cond_lhs (stmt);
4437 tree rhs = gimple_cond_rhs (stmt);
4438 if (tree_swap_operands_p (lhs, rhs))
4440 gcond *gc = as_a <gcond *> (stmt);
4441 gimple_cond_set_lhs (gc, rhs);
4442 gimple_cond_set_rhs (gc, lhs);
4443 gimple_cond_set_code (gc,
4444 swap_tree_comparison (gimple_cond_code (gc)));
4445 changed = true;
4448 default:;
4451 /* Dispatch to pattern-based folding. */
4452 if (!inplace
4453 || is_gimple_assign (stmt)
4454 || gimple_code (stmt) == GIMPLE_COND)
4456 gimple_seq seq = NULL;
4457 code_helper rcode;
4458 tree ops[3] = {};
4459 if (gimple_simplify (stmt, &rcode, ops, inplace ? NULL : &seq,
4460 valueize, valueize))
4462 if (replace_stmt_with_simplification (gsi, rcode, ops, &seq, inplace))
4463 changed = true;
4464 else
4465 gimple_seq_discard (seq);
4469 stmt = gsi_stmt (*gsi);
4471 /* Fold the main computation performed by the statement. */
4472 switch (gimple_code (stmt))
4474 case GIMPLE_ASSIGN:
4476 /* Try to canonicalize for boolean-typed X the comparisons
4477 X == 0, X == 1, X != 0, and X != 1. */
4478 if (gimple_assign_rhs_code (stmt) == EQ_EXPR
4479 || gimple_assign_rhs_code (stmt) == NE_EXPR)
4481 tree lhs = gimple_assign_lhs (stmt);
4482 tree op1 = gimple_assign_rhs1 (stmt);
4483 tree op2 = gimple_assign_rhs2 (stmt);
4484 tree type = TREE_TYPE (op1);
4486 /* Check whether the comparison operands are of the same boolean
4487 type as the result type is.
4488 Check that second operand is an integer-constant with value
4489 one or zero. */
4490 if (TREE_CODE (op2) == INTEGER_CST
4491 && (integer_zerop (op2) || integer_onep (op2))
4492 && useless_type_conversion_p (TREE_TYPE (lhs), type))
4494 enum tree_code cmp_code = gimple_assign_rhs_code (stmt);
4495 bool is_logical_not = false;
4497 /* X == 0 and X != 1 is a logical-not.of X
4498 X == 1 and X != 0 is X */
4499 if ((cmp_code == EQ_EXPR && integer_zerop (op2))
4500 || (cmp_code == NE_EXPR && integer_onep (op2)))
4501 is_logical_not = true;
4503 if (is_logical_not == false)
4504 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op1), op1);
4505 /* Only for one-bit precision typed X the transformation
4506 !X -> ~X is valied. */
4507 else if (TYPE_PRECISION (type) == 1)
4508 gimple_assign_set_rhs_with_ops (gsi, BIT_NOT_EXPR, op1);
4509 /* Otherwise we use !X -> X ^ 1. */
4510 else
4511 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op1,
4512 build_int_cst (type, 1));
4513 changed = true;
4514 break;
4518 unsigned old_num_ops = gimple_num_ops (stmt);
4519 tree lhs = gimple_assign_lhs (stmt);
4520 tree new_rhs = fold_gimple_assign (gsi);
4521 if (new_rhs
4522 && !useless_type_conversion_p (TREE_TYPE (lhs),
4523 TREE_TYPE (new_rhs)))
4524 new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
4525 if (new_rhs
4526 && (!inplace
4527 || get_gimple_rhs_num_ops (TREE_CODE (new_rhs)) < old_num_ops))
4529 gimple_assign_set_rhs_from_tree (gsi, new_rhs);
4530 changed = true;
4532 break;
4535 case GIMPLE_CALL:
4536 changed |= gimple_fold_call (gsi, inplace);
4537 break;
4539 case GIMPLE_ASM:
4540 /* Fold *& in asm operands. */
4542 gasm *asm_stmt = as_a <gasm *> (stmt);
4543 size_t noutputs;
4544 const char **oconstraints;
4545 const char *constraint;
4546 bool allows_mem, allows_reg;
4548 noutputs = gimple_asm_noutputs (asm_stmt);
4549 oconstraints = XALLOCAVEC (const char *, noutputs);
4551 for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
4553 tree link = gimple_asm_output_op (asm_stmt, i);
4554 tree op = TREE_VALUE (link);
4555 oconstraints[i]
4556 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4557 if (REFERENCE_CLASS_P (op)
4558 && (op = maybe_fold_reference (op, true)) != NULL_TREE)
4560 TREE_VALUE (link) = op;
4561 changed = true;
4564 for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
4566 tree link = gimple_asm_input_op (asm_stmt, i);
4567 tree op = TREE_VALUE (link);
4568 constraint
4569 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4570 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
4571 oconstraints, &allows_mem, &allows_reg);
4572 if (REFERENCE_CLASS_P (op)
4573 && (op = maybe_fold_reference (op, !allows_reg && allows_mem))
4574 != NULL_TREE)
4576 TREE_VALUE (link) = op;
4577 changed = true;
4581 break;
4583 case GIMPLE_DEBUG:
4584 if (gimple_debug_bind_p (stmt))
4586 tree val = gimple_debug_bind_get_value (stmt);
4587 if (val
4588 && REFERENCE_CLASS_P (val))
4590 tree tem = maybe_fold_reference (val, false);
4591 if (tem)
4593 gimple_debug_bind_set_value (stmt, tem);
4594 changed = true;
4597 else if (val
4598 && TREE_CODE (val) == ADDR_EXPR)
4600 tree ref = TREE_OPERAND (val, 0);
4601 tree tem = maybe_fold_reference (ref, false);
4602 if (tem)
4604 tem = build_fold_addr_expr_with_type (tem, TREE_TYPE (val));
4605 gimple_debug_bind_set_value (stmt, tem);
4606 changed = true;
4610 break;
4612 case GIMPLE_RETURN:
4614 greturn *ret_stmt = as_a<greturn *> (stmt);
4615 tree ret = gimple_return_retval(ret_stmt);
4617 if (ret && TREE_CODE (ret) == SSA_NAME && valueize)
4619 tree val = valueize (ret);
4620 if (val && val != ret
4621 && may_propagate_copy (ret, val))
4623 gimple_return_set_retval (ret_stmt, val);
4624 changed = true;
4628 break;
4630 default:;
4633 stmt = gsi_stmt (*gsi);
4635 /* Fold *& on the lhs. */
4636 if (gimple_has_lhs (stmt))
4638 tree lhs = gimple_get_lhs (stmt);
4639 if (lhs && REFERENCE_CLASS_P (lhs))
4641 tree new_lhs = maybe_fold_reference (lhs, true);
4642 if (new_lhs)
4644 gimple_set_lhs (stmt, new_lhs);
4645 changed = true;
4650 fold_undefer_overflow_warnings (changed && !nowarning, stmt, 0);
4651 return changed;
4654 /* Valueziation callback that ends up not following SSA edges. */
4656 tree
4657 no_follow_ssa_edges (tree)
4659 return NULL_TREE;
4662 /* Valueization callback that ends up following single-use SSA edges only. */
4664 tree
4665 follow_single_use_edges (tree val)
4667 if (TREE_CODE (val) == SSA_NAME
4668 && !has_single_use (val))
4669 return NULL_TREE;
4670 return val;
4673 /* Fold the statement pointed to by GSI. In some cases, this function may
4674 replace the whole statement with a new one. Returns true iff folding
4675 makes any changes.
4676 The statement pointed to by GSI should be in valid gimple form but may
4677 be in unfolded state as resulting from for example constant propagation
4678 which can produce *&x = 0. */
4680 bool
4681 fold_stmt (gimple_stmt_iterator *gsi)
4683 return fold_stmt_1 (gsi, false, no_follow_ssa_edges);
4686 bool
4687 fold_stmt (gimple_stmt_iterator *gsi, tree (*valueize) (tree))
4689 return fold_stmt_1 (gsi, false, valueize);
4692 /* Perform the minimal folding on statement *GSI. Only operations like
4693 *&x created by constant propagation are handled. The statement cannot
4694 be replaced with a new one. Return true if the statement was
4695 changed, false otherwise.
4696 The statement *GSI should be in valid gimple form but may
4697 be in unfolded state as resulting from for example constant propagation
4698 which can produce *&x = 0. */
4700 bool
4701 fold_stmt_inplace (gimple_stmt_iterator *gsi)
4703 gimple *stmt = gsi_stmt (*gsi);
4704 bool changed = fold_stmt_1 (gsi, true, no_follow_ssa_edges);
4705 gcc_assert (gsi_stmt (*gsi) == stmt);
4706 return changed;
4709 /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
4710 if EXPR is null or we don't know how.
4711 If non-null, the result always has boolean type. */
4713 static tree
4714 canonicalize_bool (tree expr, bool invert)
4716 if (!expr)
4717 return NULL_TREE;
4718 else if (invert)
4720 if (integer_nonzerop (expr))
4721 return boolean_false_node;
4722 else if (integer_zerop (expr))
4723 return boolean_true_node;
4724 else if (TREE_CODE (expr) == SSA_NAME)
4725 return fold_build2 (EQ_EXPR, boolean_type_node, expr,
4726 build_int_cst (TREE_TYPE (expr), 0));
4727 else if (COMPARISON_CLASS_P (expr))
4728 return fold_build2 (invert_tree_comparison (TREE_CODE (expr), false),
4729 boolean_type_node,
4730 TREE_OPERAND (expr, 0),
4731 TREE_OPERAND (expr, 1));
4732 else
4733 return NULL_TREE;
4735 else
4737 if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
4738 return expr;
4739 if (integer_nonzerop (expr))
4740 return boolean_true_node;
4741 else if (integer_zerop (expr))
4742 return boolean_false_node;
4743 else if (TREE_CODE (expr) == SSA_NAME)
4744 return fold_build2 (NE_EXPR, boolean_type_node, expr,
4745 build_int_cst (TREE_TYPE (expr), 0));
4746 else if (COMPARISON_CLASS_P (expr))
4747 return fold_build2 (TREE_CODE (expr),
4748 boolean_type_node,
4749 TREE_OPERAND (expr, 0),
4750 TREE_OPERAND (expr, 1));
4751 else
4752 return NULL_TREE;
4756 /* Check to see if a boolean expression EXPR is logically equivalent to the
4757 comparison (OP1 CODE OP2). Check for various identities involving
4758 SSA_NAMEs. */
4760 static bool
4761 same_bool_comparison_p (const_tree expr, enum tree_code code,
4762 const_tree op1, const_tree op2)
4764 gimple *s;
4766 /* The obvious case. */
4767 if (TREE_CODE (expr) == code
4768 && operand_equal_p (TREE_OPERAND (expr, 0), op1, 0)
4769 && operand_equal_p (TREE_OPERAND (expr, 1), op2, 0))
4770 return true;
4772 /* Check for comparing (name, name != 0) and the case where expr
4773 is an SSA_NAME with a definition matching the comparison. */
4774 if (TREE_CODE (expr) == SSA_NAME
4775 && TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
4777 if (operand_equal_p (expr, op1, 0))
4778 return ((code == NE_EXPR && integer_zerop (op2))
4779 || (code == EQ_EXPR && integer_nonzerop (op2)));
4780 s = SSA_NAME_DEF_STMT (expr);
4781 if (is_gimple_assign (s)
4782 && gimple_assign_rhs_code (s) == code
4783 && operand_equal_p (gimple_assign_rhs1 (s), op1, 0)
4784 && operand_equal_p (gimple_assign_rhs2 (s), op2, 0))
4785 return true;
4788 /* If op1 is of the form (name != 0) or (name == 0), and the definition
4789 of name is a comparison, recurse. */
4790 if (TREE_CODE (op1) == SSA_NAME
4791 && TREE_CODE (TREE_TYPE (op1)) == BOOLEAN_TYPE)
4793 s = SSA_NAME_DEF_STMT (op1);
4794 if (is_gimple_assign (s)
4795 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
4797 enum tree_code c = gimple_assign_rhs_code (s);
4798 if ((c == NE_EXPR && integer_zerop (op2))
4799 || (c == EQ_EXPR && integer_nonzerop (op2)))
4800 return same_bool_comparison_p (expr, c,
4801 gimple_assign_rhs1 (s),
4802 gimple_assign_rhs2 (s));
4803 if ((c == EQ_EXPR && integer_zerop (op2))
4804 || (c == NE_EXPR && integer_nonzerop (op2)))
4805 return same_bool_comparison_p (expr,
4806 invert_tree_comparison (c, false),
4807 gimple_assign_rhs1 (s),
4808 gimple_assign_rhs2 (s));
4811 return false;
4814 /* Check to see if two boolean expressions OP1 and OP2 are logically
4815 equivalent. */
4817 static bool
4818 same_bool_result_p (const_tree op1, const_tree op2)
4820 /* Simple cases first. */
4821 if (operand_equal_p (op1, op2, 0))
4822 return true;
4824 /* Check the cases where at least one of the operands is a comparison.
4825 These are a bit smarter than operand_equal_p in that they apply some
4826 identifies on SSA_NAMEs. */
4827 if (COMPARISON_CLASS_P (op2)
4828 && same_bool_comparison_p (op1, TREE_CODE (op2),
4829 TREE_OPERAND (op2, 0),
4830 TREE_OPERAND (op2, 1)))
4831 return true;
4832 if (COMPARISON_CLASS_P (op1)
4833 && same_bool_comparison_p (op2, TREE_CODE (op1),
4834 TREE_OPERAND (op1, 0),
4835 TREE_OPERAND (op1, 1)))
4836 return true;
4838 /* Default case. */
4839 return false;
4842 /* Forward declarations for some mutually recursive functions. */
4844 static tree
4845 and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
4846 enum tree_code code2, tree op2a, tree op2b);
4847 static tree
4848 and_var_with_comparison (tree var, bool invert,
4849 enum tree_code code2, tree op2a, tree op2b);
4850 static tree
4851 and_var_with_comparison_1 (gimple *stmt,
4852 enum tree_code code2, tree op2a, tree op2b);
4853 static tree
4854 or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
4855 enum tree_code code2, tree op2a, tree op2b);
4856 static tree
4857 or_var_with_comparison (tree var, bool invert,
4858 enum tree_code code2, tree op2a, tree op2b);
4859 static tree
4860 or_var_with_comparison_1 (gimple *stmt,
4861 enum tree_code code2, tree op2a, tree op2b);
4863 /* Helper function for and_comparisons_1: try to simplify the AND of the
4864 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
4865 If INVERT is true, invert the value of the VAR before doing the AND.
4866 Return NULL_EXPR if we can't simplify this to a single expression. */
4868 static tree
4869 and_var_with_comparison (tree var, bool invert,
4870 enum tree_code code2, tree op2a, tree op2b)
4872 tree t;
4873 gimple *stmt = SSA_NAME_DEF_STMT (var);
4875 /* We can only deal with variables whose definitions are assignments. */
4876 if (!is_gimple_assign (stmt))
4877 return NULL_TREE;
4879 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
4880 !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
4881 Then we only have to consider the simpler non-inverted cases. */
4882 if (invert)
4883 t = or_var_with_comparison_1 (stmt,
4884 invert_tree_comparison (code2, false),
4885 op2a, op2b);
4886 else
4887 t = and_var_with_comparison_1 (stmt, code2, op2a, op2b);
4888 return canonicalize_bool (t, invert);
4891 /* Try to simplify the AND of the ssa variable defined by the assignment
4892 STMT with the comparison specified by (OP2A CODE2 OP2B).
4893 Return NULL_EXPR if we can't simplify this to a single expression. */
4895 static tree
4896 and_var_with_comparison_1 (gimple *stmt,
4897 enum tree_code code2, tree op2a, tree op2b)
4899 tree var = gimple_assign_lhs (stmt);
4900 tree true_test_var = NULL_TREE;
4901 tree false_test_var = NULL_TREE;
4902 enum tree_code innercode = gimple_assign_rhs_code (stmt);
4904 /* Check for identities like (var AND (var == 0)) => false. */
4905 if (TREE_CODE (op2a) == SSA_NAME
4906 && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
4908 if ((code2 == NE_EXPR && integer_zerop (op2b))
4909 || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
4911 true_test_var = op2a;
4912 if (var == true_test_var)
4913 return var;
4915 else if ((code2 == EQ_EXPR && integer_zerop (op2b))
4916 || (code2 == NE_EXPR && integer_nonzerop (op2b)))
4918 false_test_var = op2a;
4919 if (var == false_test_var)
4920 return boolean_false_node;
4924 /* If the definition is a comparison, recurse on it. */
4925 if (TREE_CODE_CLASS (innercode) == tcc_comparison)
4927 tree t = and_comparisons_1 (innercode,
4928 gimple_assign_rhs1 (stmt),
4929 gimple_assign_rhs2 (stmt),
4930 code2,
4931 op2a,
4932 op2b);
4933 if (t)
4934 return t;
4937 /* If the definition is an AND or OR expression, we may be able to
4938 simplify by reassociating. */
4939 if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
4940 && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
4942 tree inner1 = gimple_assign_rhs1 (stmt);
4943 tree inner2 = gimple_assign_rhs2 (stmt);
4944 gimple *s;
4945 tree t;
4946 tree partial = NULL_TREE;
4947 bool is_and = (innercode == BIT_AND_EXPR);
4949 /* Check for boolean identities that don't require recursive examination
4950 of inner1/inner2:
4951 inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
4952 inner1 AND (inner1 OR inner2) => inner1
4953 !inner1 AND (inner1 AND inner2) => false
4954 !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
4955 Likewise for similar cases involving inner2. */
4956 if (inner1 == true_test_var)
4957 return (is_and ? var : inner1);
4958 else if (inner2 == true_test_var)
4959 return (is_and ? var : inner2);
4960 else if (inner1 == false_test_var)
4961 return (is_and
4962 ? boolean_false_node
4963 : and_var_with_comparison (inner2, false, code2, op2a, op2b));
4964 else if (inner2 == false_test_var)
4965 return (is_and
4966 ? boolean_false_node
4967 : and_var_with_comparison (inner1, false, code2, op2a, op2b));
4969 /* Next, redistribute/reassociate the AND across the inner tests.
4970 Compute the first partial result, (inner1 AND (op2a code op2b)) */
4971 if (TREE_CODE (inner1) == SSA_NAME
4972 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
4973 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
4974 && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s),
4975 gimple_assign_rhs1 (s),
4976 gimple_assign_rhs2 (s),
4977 code2, op2a, op2b)))
4979 /* Handle the AND case, where we are reassociating:
4980 (inner1 AND inner2) AND (op2a code2 op2b)
4981 => (t AND inner2)
4982 If the partial result t is a constant, we win. Otherwise
4983 continue on to try reassociating with the other inner test. */
4984 if (is_and)
4986 if (integer_onep (t))
4987 return inner2;
4988 else if (integer_zerop (t))
4989 return boolean_false_node;
4992 /* Handle the OR case, where we are redistributing:
4993 (inner1 OR inner2) AND (op2a code2 op2b)
4994 => (t OR (inner2 AND (op2a code2 op2b))) */
4995 else if (integer_onep (t))
4996 return boolean_true_node;
4998 /* Save partial result for later. */
4999 partial = t;
5002 /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
5003 if (TREE_CODE (inner2) == SSA_NAME
5004 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
5005 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
5006 && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s),
5007 gimple_assign_rhs1 (s),
5008 gimple_assign_rhs2 (s),
5009 code2, op2a, op2b)))
5011 /* Handle the AND case, where we are reassociating:
5012 (inner1 AND inner2) AND (op2a code2 op2b)
5013 => (inner1 AND t) */
5014 if (is_and)
5016 if (integer_onep (t))
5017 return inner1;
5018 else if (integer_zerop (t))
5019 return boolean_false_node;
5020 /* If both are the same, we can apply the identity
5021 (x AND x) == x. */
5022 else if (partial && same_bool_result_p (t, partial))
5023 return t;
5026 /* Handle the OR case. where we are redistributing:
5027 (inner1 OR inner2) AND (op2a code2 op2b)
5028 => (t OR (inner1 AND (op2a code2 op2b)))
5029 => (t OR partial) */
5030 else
5032 if (integer_onep (t))
5033 return boolean_true_node;
5034 else if (partial)
5036 /* We already got a simplification for the other
5037 operand to the redistributed OR expression. The
5038 interesting case is when at least one is false.
5039 Or, if both are the same, we can apply the identity
5040 (x OR x) == x. */
5041 if (integer_zerop (partial))
5042 return t;
5043 else if (integer_zerop (t))
5044 return partial;
5045 else if (same_bool_result_p (t, partial))
5046 return t;
5051 return NULL_TREE;
5054 /* Try to simplify the AND of two comparisons defined by
5055 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
5056 If this can be done without constructing an intermediate value,
5057 return the resulting tree; otherwise NULL_TREE is returned.
5058 This function is deliberately asymmetric as it recurses on SSA_DEFs
5059 in the first comparison but not the second. */
5061 static tree
5062 and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
5063 enum tree_code code2, tree op2a, tree op2b)
5065 tree truth_type = truth_type_for (TREE_TYPE (op1a));
5067 /* First check for ((x CODE1 y) AND (x CODE2 y)). */
5068 if (operand_equal_p (op1a, op2a, 0)
5069 && operand_equal_p (op1b, op2b, 0))
5071 /* Result will be either NULL_TREE, or a combined comparison. */
5072 tree t = combine_comparisons (UNKNOWN_LOCATION,
5073 TRUTH_ANDIF_EXPR, code1, code2,
5074 truth_type, op1a, op1b);
5075 if (t)
5076 return t;
5079 /* Likewise the swapped case of the above. */
5080 if (operand_equal_p (op1a, op2b, 0)
5081 && operand_equal_p (op1b, op2a, 0))
5083 /* Result will be either NULL_TREE, or a combined comparison. */
5084 tree t = combine_comparisons (UNKNOWN_LOCATION,
5085 TRUTH_ANDIF_EXPR, code1,
5086 swap_tree_comparison (code2),
5087 truth_type, op1a, op1b);
5088 if (t)
5089 return t;
5092 /* If both comparisons are of the same value against constants, we might
5093 be able to merge them. */
5094 if (operand_equal_p (op1a, op2a, 0)
5095 && TREE_CODE (op1b) == INTEGER_CST
5096 && TREE_CODE (op2b) == INTEGER_CST)
5098 int cmp = tree_int_cst_compare (op1b, op2b);
5100 /* If we have (op1a == op1b), we should either be able to
5101 return that or FALSE, depending on whether the constant op1b
5102 also satisfies the other comparison against op2b. */
5103 if (code1 == EQ_EXPR)
5105 bool done = true;
5106 bool val;
5107 switch (code2)
5109 case EQ_EXPR: val = (cmp == 0); break;
5110 case NE_EXPR: val = (cmp != 0); break;
5111 case LT_EXPR: val = (cmp < 0); break;
5112 case GT_EXPR: val = (cmp > 0); break;
5113 case LE_EXPR: val = (cmp <= 0); break;
5114 case GE_EXPR: val = (cmp >= 0); break;
5115 default: done = false;
5117 if (done)
5119 if (val)
5120 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5121 else
5122 return boolean_false_node;
5125 /* Likewise if the second comparison is an == comparison. */
5126 else if (code2 == EQ_EXPR)
5128 bool done = true;
5129 bool val;
5130 switch (code1)
5132 case EQ_EXPR: val = (cmp == 0); break;
5133 case NE_EXPR: val = (cmp != 0); break;
5134 case LT_EXPR: val = (cmp > 0); break;
5135 case GT_EXPR: val = (cmp < 0); break;
5136 case LE_EXPR: val = (cmp >= 0); break;
5137 case GE_EXPR: val = (cmp <= 0); break;
5138 default: done = false;
5140 if (done)
5142 if (val)
5143 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5144 else
5145 return boolean_false_node;
5149 /* Same business with inequality tests. */
5150 else if (code1 == NE_EXPR)
5152 bool val;
5153 switch (code2)
5155 case EQ_EXPR: val = (cmp != 0); break;
5156 case NE_EXPR: val = (cmp == 0); break;
5157 case LT_EXPR: val = (cmp >= 0); break;
5158 case GT_EXPR: val = (cmp <= 0); break;
5159 case LE_EXPR: val = (cmp > 0); break;
5160 case GE_EXPR: val = (cmp < 0); break;
5161 default:
5162 val = false;
5164 if (val)
5165 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5167 else if (code2 == NE_EXPR)
5169 bool val;
5170 switch (code1)
5172 case EQ_EXPR: val = (cmp == 0); break;
5173 case NE_EXPR: val = (cmp != 0); break;
5174 case LT_EXPR: val = (cmp <= 0); break;
5175 case GT_EXPR: val = (cmp >= 0); break;
5176 case LE_EXPR: val = (cmp < 0); break;
5177 case GE_EXPR: val = (cmp > 0); break;
5178 default:
5179 val = false;
5181 if (val)
5182 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5185 /* Chose the more restrictive of two < or <= comparisons. */
5186 else if ((code1 == LT_EXPR || code1 == LE_EXPR)
5187 && (code2 == LT_EXPR || code2 == LE_EXPR))
5189 if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
5190 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5191 else
5192 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5195 /* Likewise chose the more restrictive of two > or >= comparisons. */
5196 else if ((code1 == GT_EXPR || code1 == GE_EXPR)
5197 && (code2 == GT_EXPR || code2 == GE_EXPR))
5199 if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
5200 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5201 else
5202 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5205 /* Check for singleton ranges. */
5206 else if (cmp == 0
5207 && ((code1 == LE_EXPR && code2 == GE_EXPR)
5208 || (code1 == GE_EXPR && code2 == LE_EXPR)))
5209 return fold_build2 (EQ_EXPR, boolean_type_node, op1a, op2b);
5211 /* Check for disjoint ranges. */
5212 else if (cmp <= 0
5213 && (code1 == LT_EXPR || code1 == LE_EXPR)
5214 && (code2 == GT_EXPR || code2 == GE_EXPR))
5215 return boolean_false_node;
5216 else if (cmp >= 0
5217 && (code1 == GT_EXPR || code1 == GE_EXPR)
5218 && (code2 == LT_EXPR || code2 == LE_EXPR))
5219 return boolean_false_node;
5222 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
5223 NAME's definition is a truth value. See if there are any simplifications
5224 that can be done against the NAME's definition. */
5225 if (TREE_CODE (op1a) == SSA_NAME
5226 && (code1 == NE_EXPR || code1 == EQ_EXPR)
5227 && (integer_zerop (op1b) || integer_onep (op1b)))
5229 bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
5230 || (code1 == NE_EXPR && integer_onep (op1b)));
5231 gimple *stmt = SSA_NAME_DEF_STMT (op1a);
5232 switch (gimple_code (stmt))
5234 case GIMPLE_ASSIGN:
5235 /* Try to simplify by copy-propagating the definition. */
5236 return and_var_with_comparison (op1a, invert, code2, op2a, op2b);
5238 case GIMPLE_PHI:
5239 /* If every argument to the PHI produces the same result when
5240 ANDed with the second comparison, we win.
5241 Do not do this unless the type is bool since we need a bool
5242 result here anyway. */
5243 if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
5245 tree result = NULL_TREE;
5246 unsigned i;
5247 for (i = 0; i < gimple_phi_num_args (stmt); i++)
5249 tree arg = gimple_phi_arg_def (stmt, i);
5251 /* If this PHI has itself as an argument, ignore it.
5252 If all the other args produce the same result,
5253 we're still OK. */
5254 if (arg == gimple_phi_result (stmt))
5255 continue;
5256 else if (TREE_CODE (arg) == INTEGER_CST)
5258 if (invert ? integer_nonzerop (arg) : integer_zerop (arg))
5260 if (!result)
5261 result = boolean_false_node;
5262 else if (!integer_zerop (result))
5263 return NULL_TREE;
5265 else if (!result)
5266 result = fold_build2 (code2, boolean_type_node,
5267 op2a, op2b);
5268 else if (!same_bool_comparison_p (result,
5269 code2, op2a, op2b))
5270 return NULL_TREE;
5272 else if (TREE_CODE (arg) == SSA_NAME
5273 && !SSA_NAME_IS_DEFAULT_DEF (arg))
5275 tree temp;
5276 gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
5277 /* In simple cases we can look through PHI nodes,
5278 but we have to be careful with loops.
5279 See PR49073. */
5280 if (! dom_info_available_p (CDI_DOMINATORS)
5281 || gimple_bb (def_stmt) == gimple_bb (stmt)
5282 || dominated_by_p (CDI_DOMINATORS,
5283 gimple_bb (def_stmt),
5284 gimple_bb (stmt)))
5285 return NULL_TREE;
5286 temp = and_var_with_comparison (arg, invert, code2,
5287 op2a, op2b);
5288 if (!temp)
5289 return NULL_TREE;
5290 else if (!result)
5291 result = temp;
5292 else if (!same_bool_result_p (result, temp))
5293 return NULL_TREE;
5295 else
5296 return NULL_TREE;
5298 return result;
5301 default:
5302 break;
5305 return NULL_TREE;
5308 /* Try to simplify the AND of two comparisons, specified by
5309 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
5310 If this can be simplified to a single expression (without requiring
5311 introducing more SSA variables to hold intermediate values),
5312 return the resulting tree. Otherwise return NULL_TREE.
5313 If the result expression is non-null, it has boolean type. */
5315 tree
5316 maybe_fold_and_comparisons (enum tree_code code1, tree op1a, tree op1b,
5317 enum tree_code code2, tree op2a, tree op2b)
5319 tree t = and_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b);
5320 if (t)
5321 return t;
5322 else
5323 return and_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b);
5326 /* Helper function for or_comparisons_1: try to simplify the OR of the
5327 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
5328 If INVERT is true, invert the value of VAR before doing the OR.
5329 Return NULL_EXPR if we can't simplify this to a single expression. */
5331 static tree
5332 or_var_with_comparison (tree var, bool invert,
5333 enum tree_code code2, tree op2a, tree op2b)
5335 tree t;
5336 gimple *stmt = SSA_NAME_DEF_STMT (var);
5338 /* We can only deal with variables whose definitions are assignments. */
5339 if (!is_gimple_assign (stmt))
5340 return NULL_TREE;
5342 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
5343 !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
5344 Then we only have to consider the simpler non-inverted cases. */
5345 if (invert)
5346 t = and_var_with_comparison_1 (stmt,
5347 invert_tree_comparison (code2, false),
5348 op2a, op2b);
5349 else
5350 t = or_var_with_comparison_1 (stmt, code2, op2a, op2b);
5351 return canonicalize_bool (t, invert);
5354 /* Try to simplify the OR of the ssa variable defined by the assignment
5355 STMT with the comparison specified by (OP2A CODE2 OP2B).
5356 Return NULL_EXPR if we can't simplify this to a single expression. */
5358 static tree
5359 or_var_with_comparison_1 (gimple *stmt,
5360 enum tree_code code2, tree op2a, tree op2b)
5362 tree var = gimple_assign_lhs (stmt);
5363 tree true_test_var = NULL_TREE;
5364 tree false_test_var = NULL_TREE;
5365 enum tree_code innercode = gimple_assign_rhs_code (stmt);
5367 /* Check for identities like (var OR (var != 0)) => true . */
5368 if (TREE_CODE (op2a) == SSA_NAME
5369 && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
5371 if ((code2 == NE_EXPR && integer_zerop (op2b))
5372 || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
5374 true_test_var = op2a;
5375 if (var == true_test_var)
5376 return var;
5378 else if ((code2 == EQ_EXPR && integer_zerop (op2b))
5379 || (code2 == NE_EXPR && integer_nonzerop (op2b)))
5381 false_test_var = op2a;
5382 if (var == false_test_var)
5383 return boolean_true_node;
5387 /* If the definition is a comparison, recurse on it. */
5388 if (TREE_CODE_CLASS (innercode) == tcc_comparison)
5390 tree t = or_comparisons_1 (innercode,
5391 gimple_assign_rhs1 (stmt),
5392 gimple_assign_rhs2 (stmt),
5393 code2,
5394 op2a,
5395 op2b);
5396 if (t)
5397 return t;
5400 /* If the definition is an AND or OR expression, we may be able to
5401 simplify by reassociating. */
5402 if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
5403 && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
5405 tree inner1 = gimple_assign_rhs1 (stmt);
5406 tree inner2 = gimple_assign_rhs2 (stmt);
5407 gimple *s;
5408 tree t;
5409 tree partial = NULL_TREE;
5410 bool is_or = (innercode == BIT_IOR_EXPR);
5412 /* Check for boolean identities that don't require recursive examination
5413 of inner1/inner2:
5414 inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
5415 inner1 OR (inner1 AND inner2) => inner1
5416 !inner1 OR (inner1 OR inner2) => true
5417 !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
5419 if (inner1 == true_test_var)
5420 return (is_or ? var : inner1);
5421 else if (inner2 == true_test_var)
5422 return (is_or ? var : inner2);
5423 else if (inner1 == false_test_var)
5424 return (is_or
5425 ? boolean_true_node
5426 : or_var_with_comparison (inner2, false, code2, op2a, op2b));
5427 else if (inner2 == false_test_var)
5428 return (is_or
5429 ? boolean_true_node
5430 : or_var_with_comparison (inner1, false, code2, op2a, op2b));
5432 /* Next, redistribute/reassociate the OR across the inner tests.
5433 Compute the first partial result, (inner1 OR (op2a code op2b)) */
5434 if (TREE_CODE (inner1) == SSA_NAME
5435 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
5436 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
5437 && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s),
5438 gimple_assign_rhs1 (s),
5439 gimple_assign_rhs2 (s),
5440 code2, op2a, op2b)))
5442 /* Handle the OR case, where we are reassociating:
5443 (inner1 OR inner2) OR (op2a code2 op2b)
5444 => (t OR inner2)
5445 If the partial result t is a constant, we win. Otherwise
5446 continue on to try reassociating with the other inner test. */
5447 if (is_or)
5449 if (integer_onep (t))
5450 return boolean_true_node;
5451 else if (integer_zerop (t))
5452 return inner2;
5455 /* Handle the AND case, where we are redistributing:
5456 (inner1 AND inner2) OR (op2a code2 op2b)
5457 => (t AND (inner2 OR (op2a code op2b))) */
5458 else if (integer_zerop (t))
5459 return boolean_false_node;
5461 /* Save partial result for later. */
5462 partial = t;
5465 /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
5466 if (TREE_CODE (inner2) == SSA_NAME
5467 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
5468 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
5469 && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s),
5470 gimple_assign_rhs1 (s),
5471 gimple_assign_rhs2 (s),
5472 code2, op2a, op2b)))
5474 /* Handle the OR case, where we are reassociating:
5475 (inner1 OR inner2) OR (op2a code2 op2b)
5476 => (inner1 OR t)
5477 => (t OR partial) */
5478 if (is_or)
5480 if (integer_zerop (t))
5481 return inner1;
5482 else if (integer_onep (t))
5483 return boolean_true_node;
5484 /* If both are the same, we can apply the identity
5485 (x OR x) == x. */
5486 else if (partial && same_bool_result_p (t, partial))
5487 return t;
5490 /* Handle the AND case, where we are redistributing:
5491 (inner1 AND inner2) OR (op2a code2 op2b)
5492 => (t AND (inner1 OR (op2a code2 op2b)))
5493 => (t AND partial) */
5494 else
5496 if (integer_zerop (t))
5497 return boolean_false_node;
5498 else if (partial)
5500 /* We already got a simplification for the other
5501 operand to the redistributed AND expression. The
5502 interesting case is when at least one is true.
5503 Or, if both are the same, we can apply the identity
5504 (x AND x) == x. */
5505 if (integer_onep (partial))
5506 return t;
5507 else if (integer_onep (t))
5508 return partial;
5509 else if (same_bool_result_p (t, partial))
5510 return t;
5515 return NULL_TREE;
5518 /* Try to simplify the OR of two comparisons defined by
5519 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
5520 If this can be done without constructing an intermediate value,
5521 return the resulting tree; otherwise NULL_TREE is returned.
5522 This function is deliberately asymmetric as it recurses on SSA_DEFs
5523 in the first comparison but not the second. */
5525 static tree
5526 or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
5527 enum tree_code code2, tree op2a, tree op2b)
5529 tree truth_type = truth_type_for (TREE_TYPE (op1a));
5531 /* First check for ((x CODE1 y) OR (x CODE2 y)). */
5532 if (operand_equal_p (op1a, op2a, 0)
5533 && operand_equal_p (op1b, op2b, 0))
5535 /* Result will be either NULL_TREE, or a combined comparison. */
5536 tree t = combine_comparisons (UNKNOWN_LOCATION,
5537 TRUTH_ORIF_EXPR, code1, code2,
5538 truth_type, op1a, op1b);
5539 if (t)
5540 return t;
5543 /* Likewise the swapped case of the above. */
5544 if (operand_equal_p (op1a, op2b, 0)
5545 && operand_equal_p (op1b, op2a, 0))
5547 /* Result will be either NULL_TREE, or a combined comparison. */
5548 tree t = combine_comparisons (UNKNOWN_LOCATION,
5549 TRUTH_ORIF_EXPR, code1,
5550 swap_tree_comparison (code2),
5551 truth_type, op1a, op1b);
5552 if (t)
5553 return t;
5556 /* If both comparisons are of the same value against constants, we might
5557 be able to merge them. */
5558 if (operand_equal_p (op1a, op2a, 0)
5559 && TREE_CODE (op1b) == INTEGER_CST
5560 && TREE_CODE (op2b) == INTEGER_CST)
5562 int cmp = tree_int_cst_compare (op1b, op2b);
5564 /* If we have (op1a != op1b), we should either be able to
5565 return that or TRUE, depending on whether the constant op1b
5566 also satisfies the other comparison against op2b. */
5567 if (code1 == NE_EXPR)
5569 bool done = true;
5570 bool val;
5571 switch (code2)
5573 case EQ_EXPR: val = (cmp == 0); break;
5574 case NE_EXPR: val = (cmp != 0); break;
5575 case LT_EXPR: val = (cmp < 0); break;
5576 case GT_EXPR: val = (cmp > 0); break;
5577 case LE_EXPR: val = (cmp <= 0); break;
5578 case GE_EXPR: val = (cmp >= 0); break;
5579 default: done = false;
5581 if (done)
5583 if (val)
5584 return boolean_true_node;
5585 else
5586 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5589 /* Likewise if the second comparison is a != comparison. */
5590 else if (code2 == NE_EXPR)
5592 bool done = true;
5593 bool val;
5594 switch (code1)
5596 case EQ_EXPR: val = (cmp == 0); break;
5597 case NE_EXPR: val = (cmp != 0); break;
5598 case LT_EXPR: val = (cmp > 0); break;
5599 case GT_EXPR: val = (cmp < 0); break;
5600 case LE_EXPR: val = (cmp >= 0); break;
5601 case GE_EXPR: val = (cmp <= 0); break;
5602 default: done = false;
5604 if (done)
5606 if (val)
5607 return boolean_true_node;
5608 else
5609 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5613 /* See if an equality test is redundant with the other comparison. */
5614 else if (code1 == EQ_EXPR)
5616 bool val;
5617 switch (code2)
5619 case EQ_EXPR: val = (cmp == 0); break;
5620 case NE_EXPR: val = (cmp != 0); break;
5621 case LT_EXPR: val = (cmp < 0); break;
5622 case GT_EXPR: val = (cmp > 0); break;
5623 case LE_EXPR: val = (cmp <= 0); break;
5624 case GE_EXPR: val = (cmp >= 0); break;
5625 default:
5626 val = false;
5628 if (val)
5629 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5631 else if (code2 == EQ_EXPR)
5633 bool val;
5634 switch (code1)
5636 case EQ_EXPR: val = (cmp == 0); break;
5637 case NE_EXPR: val = (cmp != 0); break;
5638 case LT_EXPR: val = (cmp > 0); break;
5639 case GT_EXPR: val = (cmp < 0); break;
5640 case LE_EXPR: val = (cmp >= 0); break;
5641 case GE_EXPR: val = (cmp <= 0); break;
5642 default:
5643 val = false;
5645 if (val)
5646 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5649 /* Chose the less restrictive of two < or <= comparisons. */
5650 else if ((code1 == LT_EXPR || code1 == LE_EXPR)
5651 && (code2 == LT_EXPR || code2 == LE_EXPR))
5653 if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
5654 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5655 else
5656 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5659 /* Likewise chose the less restrictive of two > or >= comparisons. */
5660 else if ((code1 == GT_EXPR || code1 == GE_EXPR)
5661 && (code2 == GT_EXPR || code2 == GE_EXPR))
5663 if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
5664 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5665 else
5666 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5669 /* Check for singleton ranges. */
5670 else if (cmp == 0
5671 && ((code1 == LT_EXPR && code2 == GT_EXPR)
5672 || (code1 == GT_EXPR && code2 == LT_EXPR)))
5673 return fold_build2 (NE_EXPR, boolean_type_node, op1a, op2b);
5675 /* Check for less/greater pairs that don't restrict the range at all. */
5676 else if (cmp >= 0
5677 && (code1 == LT_EXPR || code1 == LE_EXPR)
5678 && (code2 == GT_EXPR || code2 == GE_EXPR))
5679 return boolean_true_node;
5680 else if (cmp <= 0
5681 && (code1 == GT_EXPR || code1 == GE_EXPR)
5682 && (code2 == LT_EXPR || code2 == LE_EXPR))
5683 return boolean_true_node;
5686 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
5687 NAME's definition is a truth value. See if there are any simplifications
5688 that can be done against the NAME's definition. */
5689 if (TREE_CODE (op1a) == SSA_NAME
5690 && (code1 == NE_EXPR || code1 == EQ_EXPR)
5691 && (integer_zerop (op1b) || integer_onep (op1b)))
5693 bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
5694 || (code1 == NE_EXPR && integer_onep (op1b)));
5695 gimple *stmt = SSA_NAME_DEF_STMT (op1a);
5696 switch (gimple_code (stmt))
5698 case GIMPLE_ASSIGN:
5699 /* Try to simplify by copy-propagating the definition. */
5700 return or_var_with_comparison (op1a, invert, code2, op2a, op2b);
5702 case GIMPLE_PHI:
5703 /* If every argument to the PHI produces the same result when
5704 ORed with the second comparison, we win.
5705 Do not do this unless the type is bool since we need a bool
5706 result here anyway. */
5707 if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
5709 tree result = NULL_TREE;
5710 unsigned i;
5711 for (i = 0; i < gimple_phi_num_args (stmt); i++)
5713 tree arg = gimple_phi_arg_def (stmt, i);
5715 /* If this PHI has itself as an argument, ignore it.
5716 If all the other args produce the same result,
5717 we're still OK. */
5718 if (arg == gimple_phi_result (stmt))
5719 continue;
5720 else if (TREE_CODE (arg) == INTEGER_CST)
5722 if (invert ? integer_zerop (arg) : integer_nonzerop (arg))
5724 if (!result)
5725 result = boolean_true_node;
5726 else if (!integer_onep (result))
5727 return NULL_TREE;
5729 else if (!result)
5730 result = fold_build2 (code2, boolean_type_node,
5731 op2a, op2b);
5732 else if (!same_bool_comparison_p (result,
5733 code2, op2a, op2b))
5734 return NULL_TREE;
5736 else if (TREE_CODE (arg) == SSA_NAME
5737 && !SSA_NAME_IS_DEFAULT_DEF (arg))
5739 tree temp;
5740 gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
5741 /* In simple cases we can look through PHI nodes,
5742 but we have to be careful with loops.
5743 See PR49073. */
5744 if (! dom_info_available_p (CDI_DOMINATORS)
5745 || gimple_bb (def_stmt) == gimple_bb (stmt)
5746 || dominated_by_p (CDI_DOMINATORS,
5747 gimple_bb (def_stmt),
5748 gimple_bb (stmt)))
5749 return NULL_TREE;
5750 temp = or_var_with_comparison (arg, invert, code2,
5751 op2a, op2b);
5752 if (!temp)
5753 return NULL_TREE;
5754 else if (!result)
5755 result = temp;
5756 else if (!same_bool_result_p (result, temp))
5757 return NULL_TREE;
5759 else
5760 return NULL_TREE;
5762 return result;
5765 default:
5766 break;
5769 return NULL_TREE;
5772 /* Try to simplify the OR of two comparisons, specified by
5773 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
5774 If this can be simplified to a single expression (without requiring
5775 introducing more SSA variables to hold intermediate values),
5776 return the resulting tree. Otherwise return NULL_TREE.
5777 If the result expression is non-null, it has boolean type. */
5779 tree
5780 maybe_fold_or_comparisons (enum tree_code code1, tree op1a, tree op1b,
5781 enum tree_code code2, tree op2a, tree op2b)
5783 tree t = or_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b);
5784 if (t)
5785 return t;
5786 else
5787 return or_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b);
5791 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
5793 Either NULL_TREE, a simplified but non-constant or a constant
5794 is returned.
5796 ??? This should go into a gimple-fold-inline.h file to be eventually
5797 privatized with the single valueize function used in the various TUs
5798 to avoid the indirect function call overhead. */
5800 tree
5801 gimple_fold_stmt_to_constant_1 (gimple *stmt, tree (*valueize) (tree),
5802 tree (*gvalueize) (tree))
5804 code_helper rcode;
5805 tree ops[3] = {};
5806 /* ??? The SSA propagators do not correctly deal with following SSA use-def
5807 edges if there are intermediate VARYING defs. For this reason
5808 do not follow SSA edges here even though SCCVN can technically
5809 just deal fine with that. */
5810 if (gimple_simplify (stmt, &rcode, ops, NULL, gvalueize, valueize))
5812 tree res = NULL_TREE;
5813 if (gimple_simplified_result_is_gimple_val (rcode, ops))
5814 res = ops[0];
5815 else if (mprts_hook)
5816 res = mprts_hook (rcode, gimple_expr_type (stmt), ops);
5817 if (res)
5819 if (dump_file && dump_flags & TDF_DETAILS)
5821 fprintf (dump_file, "Match-and-simplified ");
5822 print_gimple_expr (dump_file, stmt, 0, TDF_SLIM);
5823 fprintf (dump_file, " to ");
5824 print_generic_expr (dump_file, res);
5825 fprintf (dump_file, "\n");
5827 return res;
5831 location_t loc = gimple_location (stmt);
5832 switch (gimple_code (stmt))
5834 case GIMPLE_ASSIGN:
5836 enum tree_code subcode = gimple_assign_rhs_code (stmt);
5838 switch (get_gimple_rhs_class (subcode))
5840 case GIMPLE_SINGLE_RHS:
5842 tree rhs = gimple_assign_rhs1 (stmt);
5843 enum tree_code_class kind = TREE_CODE_CLASS (subcode);
5845 if (TREE_CODE (rhs) == SSA_NAME)
5847 /* If the RHS is an SSA_NAME, return its known constant value,
5848 if any. */
5849 return (*valueize) (rhs);
5851 /* Handle propagating invariant addresses into address
5852 operations. */
5853 else if (TREE_CODE (rhs) == ADDR_EXPR
5854 && !is_gimple_min_invariant (rhs))
5856 HOST_WIDE_INT offset = 0;
5857 tree base;
5858 base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs, 0),
5859 &offset,
5860 valueize);
5861 if (base
5862 && (CONSTANT_CLASS_P (base)
5863 || decl_address_invariant_p (base)))
5864 return build_invariant_address (TREE_TYPE (rhs),
5865 base, offset);
5867 else if (TREE_CODE (rhs) == CONSTRUCTOR
5868 && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE
5869 && (CONSTRUCTOR_NELTS (rhs)
5870 == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs))))
5872 unsigned i;
5873 tree val, *vec;
5875 vec = XALLOCAVEC (tree,
5876 TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)));
5877 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
5879 val = (*valueize) (val);
5880 if (TREE_CODE (val) == INTEGER_CST
5881 || TREE_CODE (val) == REAL_CST
5882 || TREE_CODE (val) == FIXED_CST)
5883 vec[i] = val;
5884 else
5885 return NULL_TREE;
5888 return build_vector (TREE_TYPE (rhs), vec);
5890 if (subcode == OBJ_TYPE_REF)
5892 tree val = (*valueize) (OBJ_TYPE_REF_EXPR (rhs));
5893 /* If callee is constant, we can fold away the wrapper. */
5894 if (is_gimple_min_invariant (val))
5895 return val;
5898 if (kind == tcc_reference)
5900 if ((TREE_CODE (rhs) == VIEW_CONVERT_EXPR
5901 || TREE_CODE (rhs) == REALPART_EXPR
5902 || TREE_CODE (rhs) == IMAGPART_EXPR)
5903 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
5905 tree val = (*valueize) (TREE_OPERAND (rhs, 0));
5906 return fold_unary_loc (EXPR_LOCATION (rhs),
5907 TREE_CODE (rhs),
5908 TREE_TYPE (rhs), val);
5910 else if (TREE_CODE (rhs) == BIT_FIELD_REF
5911 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
5913 tree val = (*valueize) (TREE_OPERAND (rhs, 0));
5914 return fold_ternary_loc (EXPR_LOCATION (rhs),
5915 TREE_CODE (rhs),
5916 TREE_TYPE (rhs), val,
5917 TREE_OPERAND (rhs, 1),
5918 TREE_OPERAND (rhs, 2));
5920 else if (TREE_CODE (rhs) == MEM_REF
5921 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
5923 tree val = (*valueize) (TREE_OPERAND (rhs, 0));
5924 if (TREE_CODE (val) == ADDR_EXPR
5925 && is_gimple_min_invariant (val))
5927 tree tem = fold_build2 (MEM_REF, TREE_TYPE (rhs),
5928 unshare_expr (val),
5929 TREE_OPERAND (rhs, 1));
5930 if (tem)
5931 rhs = tem;
5934 return fold_const_aggregate_ref_1 (rhs, valueize);
5936 else if (kind == tcc_declaration)
5937 return get_symbol_constant_value (rhs);
5938 return rhs;
5941 case GIMPLE_UNARY_RHS:
5942 return NULL_TREE;
5944 case GIMPLE_BINARY_RHS:
5945 /* Translate &x + CST into an invariant form suitable for
5946 further propagation. */
5947 if (subcode == POINTER_PLUS_EXPR)
5949 tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
5950 tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
5951 if (TREE_CODE (op0) == ADDR_EXPR
5952 && TREE_CODE (op1) == INTEGER_CST)
5954 tree off = fold_convert (ptr_type_node, op1);
5955 return build_fold_addr_expr_loc
5956 (loc,
5957 fold_build2 (MEM_REF,
5958 TREE_TYPE (TREE_TYPE (op0)),
5959 unshare_expr (op0), off));
5962 /* Canonicalize bool != 0 and bool == 0 appearing after
5963 valueization. While gimple_simplify handles this
5964 it can get confused by the ~X == 1 -> X == 0 transform
5965 which we cant reduce to a SSA name or a constant
5966 (and we have no way to tell gimple_simplify to not
5967 consider those transforms in the first place). */
5968 else if (subcode == EQ_EXPR
5969 || subcode == NE_EXPR)
5971 tree lhs = gimple_assign_lhs (stmt);
5972 tree op0 = gimple_assign_rhs1 (stmt);
5973 if (useless_type_conversion_p (TREE_TYPE (lhs),
5974 TREE_TYPE (op0)))
5976 tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
5977 op0 = (*valueize) (op0);
5978 if (TREE_CODE (op0) == INTEGER_CST)
5979 std::swap (op0, op1);
5980 if (TREE_CODE (op1) == INTEGER_CST
5981 && ((subcode == NE_EXPR && integer_zerop (op1))
5982 || (subcode == EQ_EXPR && integer_onep (op1))))
5983 return op0;
5986 return NULL_TREE;
5988 case GIMPLE_TERNARY_RHS:
5990 /* Handle ternary operators that can appear in GIMPLE form. */
5991 tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
5992 tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
5993 tree op2 = (*valueize) (gimple_assign_rhs3 (stmt));
5994 return fold_ternary_loc (loc, subcode,
5995 gimple_expr_type (stmt), op0, op1, op2);
5998 default:
5999 gcc_unreachable ();
6003 case GIMPLE_CALL:
6005 tree fn;
6006 gcall *call_stmt = as_a <gcall *> (stmt);
6008 if (gimple_call_internal_p (stmt))
6010 enum tree_code subcode = ERROR_MARK;
6011 switch (gimple_call_internal_fn (stmt))
6013 case IFN_UBSAN_CHECK_ADD:
6014 subcode = PLUS_EXPR;
6015 break;
6016 case IFN_UBSAN_CHECK_SUB:
6017 subcode = MINUS_EXPR;
6018 break;
6019 case IFN_UBSAN_CHECK_MUL:
6020 subcode = MULT_EXPR;
6021 break;
6022 case IFN_BUILTIN_EXPECT:
6024 tree arg0 = gimple_call_arg (stmt, 0);
6025 tree op0 = (*valueize) (arg0);
6026 if (TREE_CODE (op0) == INTEGER_CST)
6027 return op0;
6028 return NULL_TREE;
6030 default:
6031 return NULL_TREE;
6033 tree arg0 = gimple_call_arg (stmt, 0);
6034 tree arg1 = gimple_call_arg (stmt, 1);
6035 tree op0 = (*valueize) (arg0);
6036 tree op1 = (*valueize) (arg1);
6038 if (TREE_CODE (op0) != INTEGER_CST
6039 || TREE_CODE (op1) != INTEGER_CST)
6041 switch (subcode)
6043 case MULT_EXPR:
6044 /* x * 0 = 0 * x = 0 without overflow. */
6045 if (integer_zerop (op0) || integer_zerop (op1))
6046 return build_zero_cst (TREE_TYPE (arg0));
6047 break;
6048 case MINUS_EXPR:
6049 /* y - y = 0 without overflow. */
6050 if (operand_equal_p (op0, op1, 0))
6051 return build_zero_cst (TREE_TYPE (arg0));
6052 break;
6053 default:
6054 break;
6057 tree res
6058 = fold_binary_loc (loc, subcode, TREE_TYPE (arg0), op0, op1);
6059 if (res
6060 && TREE_CODE (res) == INTEGER_CST
6061 && !TREE_OVERFLOW (res))
6062 return res;
6063 return NULL_TREE;
6066 fn = (*valueize) (gimple_call_fn (stmt));
6067 if (TREE_CODE (fn) == ADDR_EXPR
6068 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
6069 && DECL_BUILT_IN (TREE_OPERAND (fn, 0))
6070 && gimple_builtin_call_types_compatible_p (stmt,
6071 TREE_OPERAND (fn, 0)))
6073 tree *args = XALLOCAVEC (tree, gimple_call_num_args (stmt));
6074 tree retval;
6075 unsigned i;
6076 for (i = 0; i < gimple_call_num_args (stmt); ++i)
6077 args[i] = (*valueize) (gimple_call_arg (stmt, i));
6078 retval = fold_builtin_call_array (loc,
6079 gimple_call_return_type (call_stmt),
6080 fn, gimple_call_num_args (stmt), args);
6081 if (retval)
6083 /* fold_call_expr wraps the result inside a NOP_EXPR. */
6084 STRIP_NOPS (retval);
6085 retval = fold_convert (gimple_call_return_type (call_stmt),
6086 retval);
6088 return retval;
6090 return NULL_TREE;
6093 default:
6094 return NULL_TREE;
6098 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
6099 Returns NULL_TREE if folding to a constant is not possible, otherwise
6100 returns a constant according to is_gimple_min_invariant. */
6102 tree
6103 gimple_fold_stmt_to_constant (gimple *stmt, tree (*valueize) (tree))
6105 tree res = gimple_fold_stmt_to_constant_1 (stmt, valueize);
6106 if (res && is_gimple_min_invariant (res))
6107 return res;
6108 return NULL_TREE;
6112 /* The following set of functions are supposed to fold references using
6113 their constant initializers. */
6115 /* See if we can find constructor defining value of BASE.
6116 When we know the consructor with constant offset (such as
6117 base is array[40] and we do know constructor of array), then
6118 BIT_OFFSET is adjusted accordingly.
6120 As a special case, return error_mark_node when constructor
6121 is not explicitly available, but it is known to be zero
6122 such as 'static const int a;'. */
6123 static tree
6124 get_base_constructor (tree base, HOST_WIDE_INT *bit_offset,
6125 tree (*valueize)(tree))
6127 HOST_WIDE_INT bit_offset2, size, max_size;
6128 bool reverse;
6130 if (TREE_CODE (base) == MEM_REF)
6132 if (!integer_zerop (TREE_OPERAND (base, 1)))
6134 if (!tree_fits_shwi_p (TREE_OPERAND (base, 1)))
6135 return NULL_TREE;
6136 *bit_offset += (mem_ref_offset (base).to_short_addr ()
6137 * BITS_PER_UNIT);
6140 if (valueize
6141 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
6142 base = valueize (TREE_OPERAND (base, 0));
6143 if (!base || TREE_CODE (base) != ADDR_EXPR)
6144 return NULL_TREE;
6145 base = TREE_OPERAND (base, 0);
6147 else if (valueize
6148 && TREE_CODE (base) == SSA_NAME)
6149 base = valueize (base);
6151 /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
6152 DECL_INITIAL. If BASE is a nested reference into another
6153 ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
6154 the inner reference. */
6155 switch (TREE_CODE (base))
6157 case VAR_DECL:
6158 case CONST_DECL:
6160 tree init = ctor_for_folding (base);
6162 /* Our semantic is exact opposite of ctor_for_folding;
6163 NULL means unknown, while error_mark_node is 0. */
6164 if (init == error_mark_node)
6165 return NULL_TREE;
6166 if (!init)
6167 return error_mark_node;
6168 return init;
6171 case VIEW_CONVERT_EXPR:
6172 return get_base_constructor (TREE_OPERAND (base, 0),
6173 bit_offset, valueize);
6175 case ARRAY_REF:
6176 case COMPONENT_REF:
6177 base = get_ref_base_and_extent (base, &bit_offset2, &size, &max_size,
6178 &reverse);
6179 if (max_size == -1 || size != max_size)
6180 return NULL_TREE;
6181 *bit_offset += bit_offset2;
6182 return get_base_constructor (base, bit_offset, valueize);
6184 case CONSTRUCTOR:
6185 return base;
6187 default:
6188 if (CONSTANT_CLASS_P (base))
6189 return base;
6191 return NULL_TREE;
6195 /* CTOR is CONSTRUCTOR of an array type. Fold reference of type TYPE and size
6196 SIZE to the memory at bit OFFSET. */
6198 static tree
6199 fold_array_ctor_reference (tree type, tree ctor,
6200 unsigned HOST_WIDE_INT offset,
6201 unsigned HOST_WIDE_INT size,
6202 tree from_decl)
6204 offset_int low_bound;
6205 offset_int elt_size;
6206 offset_int access_index;
6207 tree domain_type = NULL_TREE;
6208 HOST_WIDE_INT inner_offset;
6210 /* Compute low bound and elt size. */
6211 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
6212 domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
6213 if (domain_type && TYPE_MIN_VALUE (domain_type))
6215 /* Static constructors for variably sized objects makes no sense. */
6216 if (TREE_CODE (TYPE_MIN_VALUE (domain_type)) != INTEGER_CST)
6217 return NULL_TREE;
6218 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
6220 else
6221 low_bound = 0;
6222 /* Static constructors for variably sized objects makes no sense. */
6223 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor)))) != INTEGER_CST)
6224 return NULL_TREE;
6225 elt_size = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor))));
6227 /* We can handle only constantly sized accesses that are known to not
6228 be larger than size of array element. */
6229 if (!TYPE_SIZE_UNIT (type)
6230 || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
6231 || elt_size < wi::to_offset (TYPE_SIZE_UNIT (type))
6232 || elt_size == 0)
6233 return NULL_TREE;
6235 /* Compute the array index we look for. */
6236 access_index = wi::udiv_trunc (offset_int (offset / BITS_PER_UNIT),
6237 elt_size);
6238 access_index += low_bound;
6240 /* And offset within the access. */
6241 inner_offset = offset % (elt_size.to_uhwi () * BITS_PER_UNIT);
6243 /* See if the array field is large enough to span whole access. We do not
6244 care to fold accesses spanning multiple array indexes. */
6245 if (inner_offset + size > elt_size.to_uhwi () * BITS_PER_UNIT)
6246 return NULL_TREE;
6247 if (tree val = get_array_ctor_element_at_index (ctor, access_index))
6248 return fold_ctor_reference (type, val, inner_offset, size, from_decl);
6250 /* When memory is not explicitely mentioned in constructor,
6251 it is 0 (or out of range). */
6252 return build_zero_cst (type);
6255 /* CTOR is CONSTRUCTOR of an aggregate or vector.
6256 Fold reference of type TYPE and size SIZE to the memory at bit OFFSET. */
6258 static tree
6259 fold_nonarray_ctor_reference (tree type, tree ctor,
6260 unsigned HOST_WIDE_INT offset,
6261 unsigned HOST_WIDE_INT size,
6262 tree from_decl)
6264 unsigned HOST_WIDE_INT cnt;
6265 tree cfield, cval;
6267 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield,
6268 cval)
6270 tree byte_offset = DECL_FIELD_OFFSET (cfield);
6271 tree field_offset = DECL_FIELD_BIT_OFFSET (cfield);
6272 tree field_size = DECL_SIZE (cfield);
6273 offset_int bitoffset;
6274 offset_int bitoffset_end, access_end;
6276 /* Variable sized objects in static constructors makes no sense,
6277 but field_size can be NULL for flexible array members. */
6278 gcc_assert (TREE_CODE (field_offset) == INTEGER_CST
6279 && TREE_CODE (byte_offset) == INTEGER_CST
6280 && (field_size != NULL_TREE
6281 ? TREE_CODE (field_size) == INTEGER_CST
6282 : TREE_CODE (TREE_TYPE (cfield)) == ARRAY_TYPE));
6284 /* Compute bit offset of the field. */
6285 bitoffset = (wi::to_offset (field_offset)
6286 + (wi::to_offset (byte_offset) << LOG2_BITS_PER_UNIT));
6287 /* Compute bit offset where the field ends. */
6288 if (field_size != NULL_TREE)
6289 bitoffset_end = bitoffset + wi::to_offset (field_size);
6290 else
6291 bitoffset_end = 0;
6293 access_end = offset_int (offset) + size;
6295 /* Is there any overlap between [OFFSET, OFFSET+SIZE) and
6296 [BITOFFSET, BITOFFSET_END)? */
6297 if (wi::cmps (access_end, bitoffset) > 0
6298 && (field_size == NULL_TREE
6299 || wi::lts_p (offset, bitoffset_end)))
6301 offset_int inner_offset = offset_int (offset) - bitoffset;
6302 /* We do have overlap. Now see if field is large enough to
6303 cover the access. Give up for accesses spanning multiple
6304 fields. */
6305 if (wi::cmps (access_end, bitoffset_end) > 0)
6306 return NULL_TREE;
6307 if (offset < bitoffset)
6308 return NULL_TREE;
6309 return fold_ctor_reference (type, cval,
6310 inner_offset.to_uhwi (), size,
6311 from_decl);
6314 /* When memory is not explicitely mentioned in constructor, it is 0. */
6315 return build_zero_cst (type);
6318 /* CTOR is value initializing memory, fold reference of type TYPE and size SIZE
6319 to the memory at bit OFFSET. */
6321 tree
6322 fold_ctor_reference (tree type, tree ctor, unsigned HOST_WIDE_INT offset,
6323 unsigned HOST_WIDE_INT size, tree from_decl)
6325 tree ret;
6327 /* We found the field with exact match. */
6328 if (useless_type_conversion_p (type, TREE_TYPE (ctor))
6329 && !offset)
6330 return canonicalize_constructor_val (unshare_expr (ctor), from_decl);
6332 /* We are at the end of walk, see if we can view convert the
6333 result. */
6334 if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor)) && !offset
6335 /* VIEW_CONVERT_EXPR is defined only for matching sizes. */
6336 && !compare_tree_int (TYPE_SIZE (type), size)
6337 && !compare_tree_int (TYPE_SIZE (TREE_TYPE (ctor)), size))
6339 ret = canonicalize_constructor_val (unshare_expr (ctor), from_decl);
6340 if (ret)
6342 ret = fold_unary (VIEW_CONVERT_EXPR, type, ret);
6343 if (ret)
6344 STRIP_USELESS_TYPE_CONVERSION (ret);
6346 return ret;
6348 /* For constants and byte-aligned/sized reads try to go through
6349 native_encode/interpret. */
6350 if (CONSTANT_CLASS_P (ctor)
6351 && BITS_PER_UNIT == 8
6352 && offset % BITS_PER_UNIT == 0
6353 && size % BITS_PER_UNIT == 0
6354 && size <= MAX_BITSIZE_MODE_ANY_MODE)
6356 unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
6357 int len = native_encode_expr (ctor, buf, size / BITS_PER_UNIT,
6358 offset / BITS_PER_UNIT);
6359 if (len > 0)
6360 return native_interpret_expr (type, buf, len);
6362 if (TREE_CODE (ctor) == CONSTRUCTOR)
6365 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE
6366 || TREE_CODE (TREE_TYPE (ctor)) == VECTOR_TYPE)
6367 return fold_array_ctor_reference (type, ctor, offset, size,
6368 from_decl);
6369 else
6370 return fold_nonarray_ctor_reference (type, ctor, offset, size,
6371 from_decl);
6374 return NULL_TREE;
6377 /* Return the tree representing the element referenced by T if T is an
6378 ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
6379 names using VALUEIZE. Return NULL_TREE otherwise. */
6381 tree
6382 fold_const_aggregate_ref_1 (tree t, tree (*valueize) (tree))
6384 tree ctor, idx, base;
6385 HOST_WIDE_INT offset, size, max_size;
6386 tree tem;
6387 bool reverse;
6389 if (TREE_THIS_VOLATILE (t))
6390 return NULL_TREE;
6392 if (DECL_P (t))
6393 return get_symbol_constant_value (t);
6395 tem = fold_read_from_constant_string (t);
6396 if (tem)
6397 return tem;
6399 switch (TREE_CODE (t))
6401 case ARRAY_REF:
6402 case ARRAY_RANGE_REF:
6403 /* Constant indexes are handled well by get_base_constructor.
6404 Only special case variable offsets.
6405 FIXME: This code can't handle nested references with variable indexes
6406 (they will be handled only by iteration of ccp). Perhaps we can bring
6407 get_ref_base_and_extent here and make it use a valueize callback. */
6408 if (TREE_CODE (TREE_OPERAND (t, 1)) == SSA_NAME
6409 && valueize
6410 && (idx = (*valueize) (TREE_OPERAND (t, 1)))
6411 && TREE_CODE (idx) == INTEGER_CST)
6413 tree low_bound, unit_size;
6415 /* If the resulting bit-offset is constant, track it. */
6416 if ((low_bound = array_ref_low_bound (t),
6417 TREE_CODE (low_bound) == INTEGER_CST)
6418 && (unit_size = array_ref_element_size (t),
6419 tree_fits_uhwi_p (unit_size)))
6421 offset_int woffset
6422 = wi::sext (wi::to_offset (idx) - wi::to_offset (low_bound),
6423 TYPE_PRECISION (TREE_TYPE (idx)));
6425 if (wi::fits_shwi_p (woffset))
6427 offset = woffset.to_shwi ();
6428 /* TODO: This code seems wrong, multiply then check
6429 to see if it fits. */
6430 offset *= tree_to_uhwi (unit_size);
6431 offset *= BITS_PER_UNIT;
6433 base = TREE_OPERAND (t, 0);
6434 ctor = get_base_constructor (base, &offset, valueize);
6435 /* Empty constructor. Always fold to 0. */
6436 if (ctor == error_mark_node)
6437 return build_zero_cst (TREE_TYPE (t));
6438 /* Out of bound array access. Value is undefined,
6439 but don't fold. */
6440 if (offset < 0)
6441 return NULL_TREE;
6442 /* We can not determine ctor. */
6443 if (!ctor)
6444 return NULL_TREE;
6445 return fold_ctor_reference (TREE_TYPE (t), ctor, offset,
6446 tree_to_uhwi (unit_size)
6447 * BITS_PER_UNIT,
6448 base);
6452 /* Fallthru. */
6454 case COMPONENT_REF:
6455 case BIT_FIELD_REF:
6456 case TARGET_MEM_REF:
6457 case MEM_REF:
6458 base = get_ref_base_and_extent (t, &offset, &size, &max_size, &reverse);
6459 ctor = get_base_constructor (base, &offset, valueize);
6461 /* Empty constructor. Always fold to 0. */
6462 if (ctor == error_mark_node)
6463 return build_zero_cst (TREE_TYPE (t));
6464 /* We do not know precise address. */
6465 if (max_size == -1 || max_size != size)
6466 return NULL_TREE;
6467 /* We can not determine ctor. */
6468 if (!ctor)
6469 return NULL_TREE;
6471 /* Out of bound array access. Value is undefined, but don't fold. */
6472 if (offset < 0)
6473 return NULL_TREE;
6475 return fold_ctor_reference (TREE_TYPE (t), ctor, offset, size,
6476 base);
6478 case REALPART_EXPR:
6479 case IMAGPART_EXPR:
6481 tree c = fold_const_aggregate_ref_1 (TREE_OPERAND (t, 0), valueize);
6482 if (c && TREE_CODE (c) == COMPLEX_CST)
6483 return fold_build1_loc (EXPR_LOCATION (t),
6484 TREE_CODE (t), TREE_TYPE (t), c);
6485 break;
6488 default:
6489 break;
6492 return NULL_TREE;
6495 tree
6496 fold_const_aggregate_ref (tree t)
6498 return fold_const_aggregate_ref_1 (t, NULL);
6501 /* Lookup virtual method with index TOKEN in a virtual table V
6502 at OFFSET.
6503 Set CAN_REFER if non-NULL to false if method
6504 is not referable or if the virtual table is ill-formed (such as rewriten
6505 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
6507 tree
6508 gimple_get_virt_method_for_vtable (HOST_WIDE_INT token,
6509 tree v,
6510 unsigned HOST_WIDE_INT offset,
6511 bool *can_refer)
6513 tree vtable = v, init, fn;
6514 unsigned HOST_WIDE_INT size;
6515 unsigned HOST_WIDE_INT elt_size, access_index;
6516 tree domain_type;
6518 if (can_refer)
6519 *can_refer = true;
6521 /* First of all double check we have virtual table. */
6522 if (!VAR_P (v) || !DECL_VIRTUAL_P (v))
6524 /* Pass down that we lost track of the target. */
6525 if (can_refer)
6526 *can_refer = false;
6527 return NULL_TREE;
6530 init = ctor_for_folding (v);
6532 /* The virtual tables should always be born with constructors
6533 and we always should assume that they are avaialble for
6534 folding. At the moment we do not stream them in all cases,
6535 but it should never happen that ctor seem unreachable. */
6536 gcc_assert (init);
6537 if (init == error_mark_node)
6539 gcc_assert (in_lto_p);
6540 /* Pass down that we lost track of the target. */
6541 if (can_refer)
6542 *can_refer = false;
6543 return NULL_TREE;
6545 gcc_checking_assert (TREE_CODE (TREE_TYPE (v)) == ARRAY_TYPE);
6546 size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v))));
6547 offset *= BITS_PER_UNIT;
6548 offset += token * size;
6550 /* Lookup the value in the constructor that is assumed to be array.
6551 This is equivalent to
6552 fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
6553 offset, size, NULL);
6554 but in a constant time. We expect that frontend produced a simple
6555 array without indexed initializers. */
6557 gcc_checking_assert (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE);
6558 domain_type = TYPE_DOMAIN (TREE_TYPE (init));
6559 gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type)));
6560 elt_size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init))));
6562 access_index = offset / BITS_PER_UNIT / elt_size;
6563 gcc_checking_assert (offset % (elt_size * BITS_PER_UNIT) == 0);
6565 /* This code makes an assumption that there are no
6566 indexed fileds produced by C++ FE, so we can directly index the array. */
6567 if (access_index < CONSTRUCTOR_NELTS (init))
6569 fn = CONSTRUCTOR_ELT (init, access_index)->value;
6570 gcc_checking_assert (!CONSTRUCTOR_ELT (init, access_index)->index);
6571 STRIP_NOPS (fn);
6573 else
6574 fn = NULL;
6576 /* For type inconsistent program we may end up looking up virtual method
6577 in virtual table that does not contain TOKEN entries. We may overrun
6578 the virtual table and pick up a constant or RTTI info pointer.
6579 In any case the call is undefined. */
6580 if (!fn
6581 || (TREE_CODE (fn) != ADDR_EXPR && TREE_CODE (fn) != FDESC_EXPR)
6582 || TREE_CODE (TREE_OPERAND (fn, 0)) != FUNCTION_DECL)
6583 fn = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
6584 else
6586 fn = TREE_OPERAND (fn, 0);
6588 /* When cgraph node is missing and function is not public, we cannot
6589 devirtualize. This can happen in WHOPR when the actual method
6590 ends up in other partition, because we found devirtualization
6591 possibility too late. */
6592 if (!can_refer_decl_in_current_unit_p (fn, vtable))
6594 if (can_refer)
6596 *can_refer = false;
6597 return fn;
6599 return NULL_TREE;
6603 /* Make sure we create a cgraph node for functions we'll reference.
6604 They can be non-existent if the reference comes from an entry
6605 of an external vtable for example. */
6606 cgraph_node::get_create (fn);
6608 return fn;
6611 /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
6612 is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
6613 KNOWN_BINFO carries the binfo describing the true type of
6614 OBJ_TYPE_REF_OBJECT(REF).
6615 Set CAN_REFER if non-NULL to false if method
6616 is not referable or if the virtual table is ill-formed (such as rewriten
6617 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
6619 tree
6620 gimple_get_virt_method_for_binfo (HOST_WIDE_INT token, tree known_binfo,
6621 bool *can_refer)
6623 unsigned HOST_WIDE_INT offset;
6624 tree v;
6626 v = BINFO_VTABLE (known_binfo);
6627 /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */
6628 if (!v)
6629 return NULL_TREE;
6631 if (!vtable_pointer_value_to_vtable (v, &v, &offset))
6633 if (can_refer)
6634 *can_refer = false;
6635 return NULL_TREE;
6637 return gimple_get_virt_method_for_vtable (token, v, offset, can_refer);
6640 /* Given a pointer value T, return a simplified version of an
6641 indirection through T, or NULL_TREE if no simplification is
6642 possible. Note that the resulting type may be different from
6643 the type pointed to in the sense that it is still compatible
6644 from the langhooks point of view. */
6646 tree
6647 gimple_fold_indirect_ref (tree t)
6649 tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype);
6650 tree sub = t;
6651 tree subtype;
6653 STRIP_NOPS (sub);
6654 subtype = TREE_TYPE (sub);
6655 if (!POINTER_TYPE_P (subtype)
6656 || TYPE_REF_CAN_ALIAS_ALL (ptype))
6657 return NULL_TREE;
6659 if (TREE_CODE (sub) == ADDR_EXPR)
6661 tree op = TREE_OPERAND (sub, 0);
6662 tree optype = TREE_TYPE (op);
6663 /* *&p => p */
6664 if (useless_type_conversion_p (type, optype))
6665 return op;
6667 /* *(foo *)&fooarray => fooarray[0] */
6668 if (TREE_CODE (optype) == ARRAY_TYPE
6669 && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST
6670 && useless_type_conversion_p (type, TREE_TYPE (optype)))
6672 tree type_domain = TYPE_DOMAIN (optype);
6673 tree min_val = size_zero_node;
6674 if (type_domain && TYPE_MIN_VALUE (type_domain))
6675 min_val = TYPE_MIN_VALUE (type_domain);
6676 if (TREE_CODE (min_val) == INTEGER_CST)
6677 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
6679 /* *(foo *)&complexfoo => __real__ complexfoo */
6680 else if (TREE_CODE (optype) == COMPLEX_TYPE
6681 && useless_type_conversion_p (type, TREE_TYPE (optype)))
6682 return fold_build1 (REALPART_EXPR, type, op);
6683 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
6684 else if (TREE_CODE (optype) == VECTOR_TYPE
6685 && useless_type_conversion_p (type, TREE_TYPE (optype)))
6687 tree part_width = TYPE_SIZE (type);
6688 tree index = bitsize_int (0);
6689 return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
6693 /* *(p + CST) -> ... */
6694 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
6695 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
6697 tree addr = TREE_OPERAND (sub, 0);
6698 tree off = TREE_OPERAND (sub, 1);
6699 tree addrtype;
6701 STRIP_NOPS (addr);
6702 addrtype = TREE_TYPE (addr);
6704 /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
6705 if (TREE_CODE (addr) == ADDR_EXPR
6706 && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE
6707 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))
6708 && tree_fits_uhwi_p (off))
6710 unsigned HOST_WIDE_INT offset = tree_to_uhwi (off);
6711 tree part_width = TYPE_SIZE (type);
6712 unsigned HOST_WIDE_INT part_widthi
6713 = tree_to_shwi (part_width) / BITS_PER_UNIT;
6714 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
6715 tree index = bitsize_int (indexi);
6716 if (offset / part_widthi
6717 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype)))
6718 return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0),
6719 part_width, index);
6722 /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
6723 if (TREE_CODE (addr) == ADDR_EXPR
6724 && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE
6725 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))))
6727 tree size = TYPE_SIZE_UNIT (type);
6728 if (tree_int_cst_equal (size, off))
6729 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0));
6732 /* *(p + CST) -> MEM_REF <p, CST>. */
6733 if (TREE_CODE (addr) != ADDR_EXPR
6734 || DECL_P (TREE_OPERAND (addr, 0)))
6735 return fold_build2 (MEM_REF, type,
6736 addr,
6737 wide_int_to_tree (ptype, off));
6740 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
6741 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
6742 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST
6743 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
6745 tree type_domain;
6746 tree min_val = size_zero_node;
6747 tree osub = sub;
6748 sub = gimple_fold_indirect_ref (sub);
6749 if (! sub)
6750 sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
6751 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
6752 if (type_domain && TYPE_MIN_VALUE (type_domain))
6753 min_val = TYPE_MIN_VALUE (type_domain);
6754 if (TREE_CODE (min_val) == INTEGER_CST)
6755 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
6758 return NULL_TREE;
6761 /* Return true if CODE is an operation that when operating on signed
6762 integer types involves undefined behavior on overflow and the
6763 operation can be expressed with unsigned arithmetic. */
6765 bool
6766 arith_code_with_undefined_signed_overflow (tree_code code)
6768 switch (code)
6770 case PLUS_EXPR:
6771 case MINUS_EXPR:
6772 case MULT_EXPR:
6773 case NEGATE_EXPR:
6774 case POINTER_PLUS_EXPR:
6775 return true;
6776 default:
6777 return false;
6781 /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic
6782 operation that can be transformed to unsigned arithmetic by converting
6783 its operand, carrying out the operation in the corresponding unsigned
6784 type and converting the result back to the original type.
6786 Returns a sequence of statements that replace STMT and also contain
6787 a modified form of STMT itself. */
6789 gimple_seq
6790 rewrite_to_defined_overflow (gimple *stmt)
6792 if (dump_file && (dump_flags & TDF_DETAILS))
6794 fprintf (dump_file, "rewriting stmt with undefined signed "
6795 "overflow ");
6796 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
6799 tree lhs = gimple_assign_lhs (stmt);
6800 tree type = unsigned_type_for (TREE_TYPE (lhs));
6801 gimple_seq stmts = NULL;
6802 for (unsigned i = 1; i < gimple_num_ops (stmt); ++i)
6804 tree op = gimple_op (stmt, i);
6805 op = gimple_convert (&stmts, type, op);
6806 gimple_set_op (stmt, i, op);
6808 gimple_assign_set_lhs (stmt, make_ssa_name (type, stmt));
6809 if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
6810 gimple_assign_set_rhs_code (stmt, PLUS_EXPR);
6811 gimple_seq_add_stmt (&stmts, stmt);
6812 gimple *cvt = gimple_build_assign (lhs, NOP_EXPR, gimple_assign_lhs (stmt));
6813 gimple_seq_add_stmt (&stmts, cvt);
6815 return stmts;
6819 /* The valueization hook we use for the gimple_build API simplification.
6820 This makes us match fold_buildN behavior by only combining with
6821 statements in the sequence(s) we are currently building. */
6823 static tree
6824 gimple_build_valueize (tree op)
6826 if (gimple_bb (SSA_NAME_DEF_STMT (op)) == NULL)
6827 return op;
6828 return NULL_TREE;
6831 /* Build the expression CODE OP0 of type TYPE with location LOC,
6832 simplifying it first if possible. Returns the built
6833 expression value and appends statements possibly defining it
6834 to SEQ. */
6836 tree
6837 gimple_build (gimple_seq *seq, location_t loc,
6838 enum tree_code code, tree type, tree op0)
6840 tree res = gimple_simplify (code, type, op0, seq, gimple_build_valueize);
6841 if (!res)
6843 res = create_tmp_reg_or_ssa_name (type);
6844 gimple *stmt;
6845 if (code == REALPART_EXPR
6846 || code == IMAGPART_EXPR
6847 || code == VIEW_CONVERT_EXPR)
6848 stmt = gimple_build_assign (res, code, build1 (code, type, op0));
6849 else
6850 stmt = gimple_build_assign (res, code, op0);
6851 gimple_set_location (stmt, loc);
6852 gimple_seq_add_stmt_without_update (seq, stmt);
6854 return res;
6857 /* Build the expression OP0 CODE OP1 of type TYPE with location LOC,
6858 simplifying it first if possible. Returns the built
6859 expression value and appends statements possibly defining it
6860 to SEQ. */
6862 tree
6863 gimple_build (gimple_seq *seq, location_t loc,
6864 enum tree_code code, tree type, tree op0, tree op1)
6866 tree res = gimple_simplify (code, type, op0, op1, seq, gimple_build_valueize);
6867 if (!res)
6869 res = create_tmp_reg_or_ssa_name (type);
6870 gimple *stmt = gimple_build_assign (res, code, op0, op1);
6871 gimple_set_location (stmt, loc);
6872 gimple_seq_add_stmt_without_update (seq, stmt);
6874 return res;
6877 /* Build the expression (CODE OP0 OP1 OP2) of type TYPE with location LOC,
6878 simplifying it first if possible. Returns the built
6879 expression value and appends statements possibly defining it
6880 to SEQ. */
6882 tree
6883 gimple_build (gimple_seq *seq, location_t loc,
6884 enum tree_code code, tree type, tree op0, tree op1, tree op2)
6886 tree res = gimple_simplify (code, type, op0, op1, op2,
6887 seq, gimple_build_valueize);
6888 if (!res)
6890 res = create_tmp_reg_or_ssa_name (type);
6891 gimple *stmt;
6892 if (code == BIT_FIELD_REF)
6893 stmt = gimple_build_assign (res, code,
6894 build3 (code, type, op0, op1, op2));
6895 else
6896 stmt = gimple_build_assign (res, code, op0, op1, op2);
6897 gimple_set_location (stmt, loc);
6898 gimple_seq_add_stmt_without_update (seq, stmt);
6900 return res;
6903 /* Build the call FN (ARG0) with a result of type TYPE
6904 (or no result if TYPE is void) with location LOC,
6905 simplifying it first if possible. Returns the built
6906 expression value (or NULL_TREE if TYPE is void) and appends
6907 statements possibly defining it to SEQ. */
6909 tree
6910 gimple_build (gimple_seq *seq, location_t loc,
6911 enum built_in_function fn, tree type, tree arg0)
6913 tree res = gimple_simplify (fn, type, arg0, seq, gimple_build_valueize);
6914 if (!res)
6916 tree decl = builtin_decl_implicit (fn);
6917 gimple *stmt = gimple_build_call (decl, 1, arg0);
6918 if (!VOID_TYPE_P (type))
6920 res = create_tmp_reg_or_ssa_name (type);
6921 gimple_call_set_lhs (stmt, res);
6923 gimple_set_location (stmt, loc);
6924 gimple_seq_add_stmt_without_update (seq, stmt);
6926 return res;
6929 /* Build the call FN (ARG0, ARG1) with a result of type TYPE
6930 (or no result if TYPE is void) with location LOC,
6931 simplifying it first if possible. Returns the built
6932 expression value (or NULL_TREE if TYPE is void) and appends
6933 statements possibly defining it to SEQ. */
6935 tree
6936 gimple_build (gimple_seq *seq, location_t loc,
6937 enum built_in_function fn, tree type, tree arg0, tree arg1)
6939 tree res = gimple_simplify (fn, type, arg0, arg1, seq, gimple_build_valueize);
6940 if (!res)
6942 tree decl = builtin_decl_implicit (fn);
6943 gimple *stmt = gimple_build_call (decl, 2, arg0, arg1);
6944 if (!VOID_TYPE_P (type))
6946 res = create_tmp_reg_or_ssa_name (type);
6947 gimple_call_set_lhs (stmt, res);
6949 gimple_set_location (stmt, loc);
6950 gimple_seq_add_stmt_without_update (seq, stmt);
6952 return res;
6955 /* Build the call FN (ARG0, ARG1, ARG2) with a result of type TYPE
6956 (or no result if TYPE is void) with location LOC,
6957 simplifying it first if possible. Returns the built
6958 expression value (or NULL_TREE if TYPE is void) and appends
6959 statements possibly defining it to SEQ. */
6961 tree
6962 gimple_build (gimple_seq *seq, location_t loc,
6963 enum built_in_function fn, tree type,
6964 tree arg0, tree arg1, tree arg2)
6966 tree res = gimple_simplify (fn, type, arg0, arg1, arg2,
6967 seq, gimple_build_valueize);
6968 if (!res)
6970 tree decl = builtin_decl_implicit (fn);
6971 gimple *stmt = gimple_build_call (decl, 3, arg0, arg1, arg2);
6972 if (!VOID_TYPE_P (type))
6974 res = create_tmp_reg_or_ssa_name (type);
6975 gimple_call_set_lhs (stmt, res);
6977 gimple_set_location (stmt, loc);
6978 gimple_seq_add_stmt_without_update (seq, stmt);
6980 return res;
6983 /* Build the conversion (TYPE) OP with a result of type TYPE
6984 with location LOC if such conversion is neccesary in GIMPLE,
6985 simplifying it first.
6986 Returns the built expression value and appends
6987 statements possibly defining it to SEQ. */
6989 tree
6990 gimple_convert (gimple_seq *seq, location_t loc, tree type, tree op)
6992 if (useless_type_conversion_p (type, TREE_TYPE (op)))
6993 return op;
6994 return gimple_build (seq, loc, NOP_EXPR, type, op);
6997 /* Build the conversion (ptrofftype) OP with a result of a type
6998 compatible with ptrofftype with location LOC if such conversion
6999 is neccesary in GIMPLE, simplifying it first.
7000 Returns the built expression value and appends
7001 statements possibly defining it to SEQ. */
7003 tree
7004 gimple_convert_to_ptrofftype (gimple_seq *seq, location_t loc, tree op)
7006 if (ptrofftype_p (TREE_TYPE (op)))
7007 return op;
7008 return gimple_convert (seq, loc, sizetype, op);
7011 /* Return true if the result of assignment STMT is known to be non-negative.
7012 If the return value is based on the assumption that signed overflow is
7013 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7014 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7016 static bool
7017 gimple_assign_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
7018 int depth)
7020 enum tree_code code = gimple_assign_rhs_code (stmt);
7021 switch (get_gimple_rhs_class (code))
7023 case GIMPLE_UNARY_RHS:
7024 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
7025 gimple_expr_type (stmt),
7026 gimple_assign_rhs1 (stmt),
7027 strict_overflow_p, depth);
7028 case GIMPLE_BINARY_RHS:
7029 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
7030 gimple_expr_type (stmt),
7031 gimple_assign_rhs1 (stmt),
7032 gimple_assign_rhs2 (stmt),
7033 strict_overflow_p, depth);
7034 case GIMPLE_TERNARY_RHS:
7035 return false;
7036 case GIMPLE_SINGLE_RHS:
7037 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
7038 strict_overflow_p, depth);
7039 case GIMPLE_INVALID_RHS:
7040 break;
7042 gcc_unreachable ();
7045 /* Return true if return value of call STMT is known to be non-negative.
7046 If the return value is based on the assumption that signed overflow is
7047 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7048 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7050 static bool
7051 gimple_call_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
7052 int depth)
7054 tree arg0 = gimple_call_num_args (stmt) > 0 ?
7055 gimple_call_arg (stmt, 0) : NULL_TREE;
7056 tree arg1 = gimple_call_num_args (stmt) > 1 ?
7057 gimple_call_arg (stmt, 1) : NULL_TREE;
7059 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
7060 gimple_call_combined_fn (stmt),
7061 arg0,
7062 arg1,
7063 strict_overflow_p, depth);
7066 /* Return true if return value of call STMT is known to be non-negative.
7067 If the return value is based on the assumption that signed overflow is
7068 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7069 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7071 static bool
7072 gimple_phi_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
7073 int depth)
7075 for (unsigned i = 0; i < gimple_phi_num_args (stmt); ++i)
7077 tree arg = gimple_phi_arg_def (stmt, i);
7078 if (!tree_single_nonnegative_warnv_p (arg, strict_overflow_p, depth + 1))
7079 return false;
7081 return true;
7084 /* Return true if STMT is known to compute a non-negative value.
7085 If the return value is based on the assumption that signed overflow is
7086 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7087 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7089 bool
7090 gimple_stmt_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
7091 int depth)
7093 switch (gimple_code (stmt))
7095 case GIMPLE_ASSIGN:
7096 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p,
7097 depth);
7098 case GIMPLE_CALL:
7099 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p,
7100 depth);
7101 case GIMPLE_PHI:
7102 return gimple_phi_nonnegative_warnv_p (stmt, strict_overflow_p,
7103 depth);
7104 default:
7105 return false;
7109 /* Return true if the floating-point value computed by assignment STMT
7110 is known to have an integer value. We also allow +Inf, -Inf and NaN
7111 to be considered integer values. Return false for signaling NaN.
7113 DEPTH is the current nesting depth of the query. */
7115 static bool
7116 gimple_assign_integer_valued_real_p (gimple *stmt, int depth)
7118 enum tree_code code = gimple_assign_rhs_code (stmt);
7119 switch (get_gimple_rhs_class (code))
7121 case GIMPLE_UNARY_RHS:
7122 return integer_valued_real_unary_p (gimple_assign_rhs_code (stmt),
7123 gimple_assign_rhs1 (stmt), depth);
7124 case GIMPLE_BINARY_RHS:
7125 return integer_valued_real_binary_p (gimple_assign_rhs_code (stmt),
7126 gimple_assign_rhs1 (stmt),
7127 gimple_assign_rhs2 (stmt), depth);
7128 case GIMPLE_TERNARY_RHS:
7129 return false;
7130 case GIMPLE_SINGLE_RHS:
7131 return integer_valued_real_single_p (gimple_assign_rhs1 (stmt), depth);
7132 case GIMPLE_INVALID_RHS:
7133 break;
7135 gcc_unreachable ();
7138 /* Return true if the floating-point value computed by call STMT is known
7139 to have an integer value. We also allow +Inf, -Inf and NaN to be
7140 considered integer values. Return false for signaling NaN.
7142 DEPTH is the current nesting depth of the query. */
7144 static bool
7145 gimple_call_integer_valued_real_p (gimple *stmt, int depth)
7147 tree arg0 = (gimple_call_num_args (stmt) > 0
7148 ? gimple_call_arg (stmt, 0)
7149 : NULL_TREE);
7150 tree arg1 = (gimple_call_num_args (stmt) > 1
7151 ? gimple_call_arg (stmt, 1)
7152 : NULL_TREE);
7153 return integer_valued_real_call_p (gimple_call_combined_fn (stmt),
7154 arg0, arg1, depth);
7157 /* Return true if the floating-point result of phi STMT is known to have
7158 an integer value. We also allow +Inf, -Inf and NaN to be considered
7159 integer values. Return false for signaling NaN.
7161 DEPTH is the current nesting depth of the query. */
7163 static bool
7164 gimple_phi_integer_valued_real_p (gimple *stmt, int depth)
7166 for (unsigned i = 0; i < gimple_phi_num_args (stmt); ++i)
7168 tree arg = gimple_phi_arg_def (stmt, i);
7169 if (!integer_valued_real_single_p (arg, depth + 1))
7170 return false;
7172 return true;
7175 /* Return true if the floating-point value computed by STMT is known
7176 to have an integer value. We also allow +Inf, -Inf and NaN to be
7177 considered integer values. Return false for signaling NaN.
7179 DEPTH is the current nesting depth of the query. */
7181 bool
7182 gimple_stmt_integer_valued_real_p (gimple *stmt, int depth)
7184 switch (gimple_code (stmt))
7186 case GIMPLE_ASSIGN:
7187 return gimple_assign_integer_valued_real_p (stmt, depth);
7188 case GIMPLE_CALL:
7189 return gimple_call_integer_valued_real_p (stmt, depth);
7190 case GIMPLE_PHI:
7191 return gimple_phi_integer_valued_real_p (stmt, depth);
7192 default:
7193 return false;