* MAINTAINERS (nvptx): Add self.
[official-gcc.git] / gcc / gimple-fold.c
blob86caa8cf96e54f91f957137e1e72eed1d423cab0
1 /* Statement simplification on GIMPLE.
2 Copyright (C) 2010-2015 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 "predict.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "rtl.h"
29 #include "ssa.h"
30 #include "alias.h"
31 #include "fold-const.h"
32 #include "flags.h"
33 #include "insn-config.h"
34 #include "expmed.h"
35 #include "dojump.h"
36 #include "explow.h"
37 #include "calls.h"
38 #include "emit-rtl.h"
39 #include "varasm.h"
40 #include "stmt.h"
41 #include "expr.h"
42 #include "stor-layout.h"
43 #include "dumpfile.h"
44 #include "dominance.h"
45 #include "internal-fn.h"
46 #include "gimple-fold.h"
47 #include "gimplify.h"
48 #include "gimple-iterator.h"
49 #include "tree-into-ssa.h"
50 #include "tree-dfa.h"
51 #include "tree-ssa.h"
52 #include "tree-ssa-propagate.h"
53 #include "target.h"
54 #include "cgraph.h"
55 #include "ipa-utils.h"
56 #include "gimple-pretty-print.h"
57 #include "tree-ssa-address.h"
58 #include "langhooks.h"
59 #include "gimplify-me.h"
60 #include "dbgcnt.h"
61 #include "builtins.h"
62 #include "output.h"
63 #include "tree-eh.h"
64 #include "gimple-match.h"
66 /* Return true when DECL can be referenced from current unit.
67 FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
68 We can get declarations that are not possible to reference for various
69 reasons:
71 1) When analyzing C++ virtual tables.
72 C++ virtual tables do have known constructors even
73 when they are keyed to other compilation unit.
74 Those tables can contain pointers to methods and vars
75 in other units. Those methods have both STATIC and EXTERNAL
76 set.
77 2) In WHOPR mode devirtualization might lead to reference
78 to method that was partitioned elsehwere.
79 In this case we have static VAR_DECL or FUNCTION_DECL
80 that has no corresponding callgraph/varpool node
81 declaring the body.
82 3) COMDAT functions referred by external vtables that
83 we devirtualize only during final compilation stage.
84 At this time we already decided that we will not output
85 the function body and thus we can't reference the symbol
86 directly. */
88 static bool
89 can_refer_decl_in_current_unit_p (tree decl, tree from_decl)
91 varpool_node *vnode;
92 struct cgraph_node *node;
93 symtab_node *snode;
95 if (DECL_ABSTRACT_P (decl))
96 return false;
98 /* We are concerned only about static/external vars and functions. */
99 if ((!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
100 || (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL))
101 return true;
103 /* Static objects can be referred only if they was not optimized out yet. */
104 if (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
106 /* Before we start optimizing unreachable code we can be sure all
107 static objects are defined. */
108 if (symtab->function_flags_ready)
109 return true;
110 snode = symtab_node::get (decl);
111 if (!snode || !snode->definition)
112 return false;
113 node = dyn_cast <cgraph_node *> (snode);
114 return !node || !node->global.inlined_to;
117 /* We will later output the initializer, so we can refer to it.
118 So we are concerned only when DECL comes from initializer of
119 external var or var that has been optimized out. */
120 if (!from_decl
121 || TREE_CODE (from_decl) != VAR_DECL
122 || (!DECL_EXTERNAL (from_decl)
123 && (vnode = varpool_node::get (from_decl)) != NULL
124 && vnode->definition)
125 || (flag_ltrans
126 && (vnode = varpool_node::get (from_decl)) != NULL
127 && vnode->in_other_partition))
128 return true;
129 /* We are folding reference from external vtable. The vtable may reffer
130 to a symbol keyed to other compilation unit. The other compilation
131 unit may be in separate DSO and the symbol may be hidden. */
132 if (DECL_VISIBILITY_SPECIFIED (decl)
133 && DECL_EXTERNAL (decl)
134 && DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT
135 && (!(snode = symtab_node::get (decl)) || !snode->in_other_partition))
136 return false;
137 /* When function is public, we always can introduce new reference.
138 Exception are the COMDAT functions where introducing a direct
139 reference imply need to include function body in the curren tunit. */
140 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
141 return true;
142 /* We have COMDAT. We are going to check if we still have definition
143 or if the definition is going to be output in other partition.
144 Bypass this when gimplifying; all needed functions will be produced.
146 As observed in PR20991 for already optimized out comdat virtual functions
147 it may be tempting to not necessarily give up because the copy will be
148 output elsewhere when corresponding vtable is output.
149 This is however not possible - ABI specify that COMDATs are output in
150 units where they are used and when the other unit was compiled with LTO
151 it is possible that vtable was kept public while the function itself
152 was privatized. */
153 if (!symtab->function_flags_ready)
154 return true;
156 snode = symtab_node::get (decl);
157 if (!snode
158 || ((!snode->definition || DECL_EXTERNAL (decl))
159 && (!snode->in_other_partition
160 || (!snode->forced_by_abi && !snode->force_output))))
161 return false;
162 node = dyn_cast <cgraph_node *> (snode);
163 return !node || !node->global.inlined_to;
166 /* CVAL is value taken from DECL_INITIAL of variable. Try to transform it into
167 acceptable form for is_gimple_min_invariant.
168 FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL. */
170 tree
171 canonicalize_constructor_val (tree cval, tree from_decl)
173 tree orig_cval = cval;
174 STRIP_NOPS (cval);
175 if (TREE_CODE (cval) == POINTER_PLUS_EXPR
176 && TREE_CODE (TREE_OPERAND (cval, 1)) == INTEGER_CST)
178 tree ptr = TREE_OPERAND (cval, 0);
179 if (is_gimple_min_invariant (ptr))
180 cval = build1_loc (EXPR_LOCATION (cval),
181 ADDR_EXPR, TREE_TYPE (ptr),
182 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (ptr)),
183 ptr,
184 fold_convert (ptr_type_node,
185 TREE_OPERAND (cval, 1))));
187 if (TREE_CODE (cval) == ADDR_EXPR)
189 tree base = NULL_TREE;
190 if (TREE_CODE (TREE_OPERAND (cval, 0)) == COMPOUND_LITERAL_EXPR)
192 base = COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval, 0));
193 if (base)
194 TREE_OPERAND (cval, 0) = base;
196 else
197 base = get_base_address (TREE_OPERAND (cval, 0));
198 if (!base)
199 return NULL_TREE;
201 if ((TREE_CODE (base) == VAR_DECL
202 || TREE_CODE (base) == FUNCTION_DECL)
203 && !can_refer_decl_in_current_unit_p (base, from_decl))
204 return NULL_TREE;
205 if (TREE_CODE (base) == VAR_DECL)
206 TREE_ADDRESSABLE (base) = 1;
207 else if (TREE_CODE (base) == FUNCTION_DECL)
209 /* Make sure we create a cgraph node for functions we'll reference.
210 They can be non-existent if the reference comes from an entry
211 of an external vtable for example. */
212 cgraph_node::get_create (base);
214 /* Fixup types in global initializers. */
215 if (TREE_TYPE (TREE_TYPE (cval)) != TREE_TYPE (TREE_OPERAND (cval, 0)))
216 cval = build_fold_addr_expr (TREE_OPERAND (cval, 0));
218 if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval)))
219 cval = fold_convert (TREE_TYPE (orig_cval), cval);
220 return cval;
222 if (TREE_OVERFLOW_P (cval))
223 return drop_tree_overflow (cval);
224 return orig_cval;
227 /* If SYM is a constant variable with known value, return the value.
228 NULL_TREE is returned otherwise. */
230 tree
231 get_symbol_constant_value (tree sym)
233 tree val = ctor_for_folding (sym);
234 if (val != error_mark_node)
236 if (val)
238 val = canonicalize_constructor_val (unshare_expr (val), sym);
239 if (val && is_gimple_min_invariant (val))
240 return val;
241 else
242 return NULL_TREE;
244 /* Variables declared 'const' without an initializer
245 have zero as the initializer if they may not be
246 overridden at link or run time. */
247 if (!val
248 && is_gimple_reg_type (TREE_TYPE (sym)))
249 return build_zero_cst (TREE_TYPE (sym));
252 return NULL_TREE;
257 /* Subroutine of fold_stmt. We perform several simplifications of the
258 memory reference tree EXPR and make sure to re-gimplify them properly
259 after propagation of constant addresses. IS_LHS is true if the
260 reference is supposed to be an lvalue. */
262 static tree
263 maybe_fold_reference (tree expr, bool is_lhs)
265 tree result;
267 if ((TREE_CODE (expr) == VIEW_CONVERT_EXPR
268 || TREE_CODE (expr) == REALPART_EXPR
269 || TREE_CODE (expr) == IMAGPART_EXPR)
270 && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
271 return fold_unary_loc (EXPR_LOCATION (expr),
272 TREE_CODE (expr),
273 TREE_TYPE (expr),
274 TREE_OPERAND (expr, 0));
275 else if (TREE_CODE (expr) == BIT_FIELD_REF
276 && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
277 return fold_ternary_loc (EXPR_LOCATION (expr),
278 TREE_CODE (expr),
279 TREE_TYPE (expr),
280 TREE_OPERAND (expr, 0),
281 TREE_OPERAND (expr, 1),
282 TREE_OPERAND (expr, 2));
284 if (!is_lhs
285 && (result = fold_const_aggregate_ref (expr))
286 && is_gimple_min_invariant (result))
287 return result;
289 return NULL_TREE;
293 /* Attempt to fold an assignment statement pointed-to by SI. Returns a
294 replacement rhs for the statement or NULL_TREE if no simplification
295 could be made. It is assumed that the operands have been previously
296 folded. */
298 static tree
299 fold_gimple_assign (gimple_stmt_iterator *si)
301 gimple stmt = gsi_stmt (*si);
302 enum tree_code subcode = gimple_assign_rhs_code (stmt);
303 location_t loc = gimple_location (stmt);
305 tree result = NULL_TREE;
307 switch (get_gimple_rhs_class (subcode))
309 case GIMPLE_SINGLE_RHS:
311 tree rhs = gimple_assign_rhs1 (stmt);
313 if (TREE_CLOBBER_P (rhs))
314 return NULL_TREE;
316 if (REFERENCE_CLASS_P (rhs))
317 return maybe_fold_reference (rhs, false);
319 else if (TREE_CODE (rhs) == OBJ_TYPE_REF)
321 tree val = OBJ_TYPE_REF_EXPR (rhs);
322 if (is_gimple_min_invariant (val))
323 return val;
324 else if (flag_devirtualize && virtual_method_call_p (rhs))
326 bool final;
327 vec <cgraph_node *>targets
328 = possible_polymorphic_call_targets (rhs, stmt, &final);
329 if (final && targets.length () <= 1 && dbg_cnt (devirt))
331 if (dump_enabled_p ())
333 location_t loc = gimple_location_safe (stmt);
334 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
335 "resolving virtual function address "
336 "reference to function %s\n",
337 targets.length () == 1
338 ? targets[0]->name ()
339 : "NULL");
341 if (targets.length () == 1)
343 val = fold_convert (TREE_TYPE (val),
344 build_fold_addr_expr_loc
345 (loc, targets[0]->decl));
346 STRIP_USELESS_TYPE_CONVERSION (val);
348 else
349 /* We can not use __builtin_unreachable here because it
350 can not have address taken. */
351 val = build_int_cst (TREE_TYPE (val), 0);
352 return val;
357 else if (TREE_CODE (rhs) == ADDR_EXPR)
359 tree ref = TREE_OPERAND (rhs, 0);
360 tree tem = maybe_fold_reference (ref, true);
361 if (tem
362 && TREE_CODE (tem) == MEM_REF
363 && integer_zerop (TREE_OPERAND (tem, 1)))
364 result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (tem, 0));
365 else if (tem)
366 result = fold_convert (TREE_TYPE (rhs),
367 build_fold_addr_expr_loc (loc, tem));
368 else if (TREE_CODE (ref) == MEM_REF
369 && integer_zerop (TREE_OPERAND (ref, 1)))
370 result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (ref, 0));
373 else if (TREE_CODE (rhs) == CONSTRUCTOR
374 && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE
375 && (CONSTRUCTOR_NELTS (rhs)
376 == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs))))
378 /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */
379 unsigned i;
380 tree val;
382 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
383 if (TREE_CODE (val) != INTEGER_CST
384 && TREE_CODE (val) != REAL_CST
385 && TREE_CODE (val) != FIXED_CST)
386 return NULL_TREE;
388 return build_vector_from_ctor (TREE_TYPE (rhs),
389 CONSTRUCTOR_ELTS (rhs));
392 else if (DECL_P (rhs))
393 return get_symbol_constant_value (rhs);
395 /* If we couldn't fold the RHS, hand over to the generic
396 fold routines. */
397 if (result == NULL_TREE)
398 result = fold (rhs);
400 /* Strip away useless type conversions. Both the NON_LVALUE_EXPR
401 that may have been added by fold, and "useless" type
402 conversions that might now be apparent due to propagation. */
403 STRIP_USELESS_TYPE_CONVERSION (result);
405 if (result != rhs && valid_gimple_rhs_p (result))
406 return result;
408 return NULL_TREE;
410 break;
412 case GIMPLE_UNARY_RHS:
413 break;
415 case GIMPLE_BINARY_RHS:
416 break;
418 case GIMPLE_TERNARY_RHS:
419 /* Try to fold a conditional expression. */
420 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
422 tree op0 = gimple_assign_rhs1 (stmt);
423 tree tem;
424 bool set = false;
425 location_t cond_loc = gimple_location (stmt);
427 if (COMPARISON_CLASS_P (op0))
429 fold_defer_overflow_warnings ();
430 tem = fold_binary_loc (cond_loc,
431 TREE_CODE (op0), TREE_TYPE (op0),
432 TREE_OPERAND (op0, 0),
433 TREE_OPERAND (op0, 1));
434 /* This is actually a conditional expression, not a GIMPLE
435 conditional statement, however, the valid_gimple_rhs_p
436 test still applies. */
437 set = (tem && is_gimple_condexpr (tem)
438 && valid_gimple_rhs_p (tem));
439 fold_undefer_overflow_warnings (set, stmt, 0);
441 else if (is_gimple_min_invariant (op0))
443 tem = op0;
444 set = true;
446 else
447 return NULL_TREE;
449 if (set)
450 result = fold_build3_loc (cond_loc, COND_EXPR,
451 TREE_TYPE (gimple_assign_lhs (stmt)), tem,
452 gimple_assign_rhs2 (stmt),
453 gimple_assign_rhs3 (stmt));
456 if (!result)
457 result = fold_ternary_loc (loc, subcode,
458 TREE_TYPE (gimple_assign_lhs (stmt)),
459 gimple_assign_rhs1 (stmt),
460 gimple_assign_rhs2 (stmt),
461 gimple_assign_rhs3 (stmt));
463 if (result)
465 STRIP_USELESS_TYPE_CONVERSION (result);
466 if (valid_gimple_rhs_p (result))
467 return result;
469 break;
471 case GIMPLE_INVALID_RHS:
472 gcc_unreachable ();
475 return NULL_TREE;
479 /* Replace a statement at *SI_P with a sequence of statements in STMTS,
480 adjusting the replacement stmts location and virtual operands.
481 If the statement has a lhs the last stmt in the sequence is expected
482 to assign to that lhs. */
484 static void
485 gsi_replace_with_seq_vops (gimple_stmt_iterator *si_p, gimple_seq stmts)
487 gimple stmt = gsi_stmt (*si_p);
489 if (gimple_has_location (stmt))
490 annotate_all_with_location (stmts, gimple_location (stmt));
492 /* First iterate over the replacement statements backward, assigning
493 virtual operands to their defining statements. */
494 gimple laststore = NULL;
495 for (gimple_stmt_iterator i = gsi_last (stmts);
496 !gsi_end_p (i); gsi_prev (&i))
498 gimple new_stmt = gsi_stmt (i);
499 if ((gimple_assign_single_p (new_stmt)
500 && !is_gimple_reg (gimple_assign_lhs (new_stmt)))
501 || (is_gimple_call (new_stmt)
502 && (gimple_call_flags (new_stmt)
503 & (ECF_NOVOPS | ECF_PURE | ECF_CONST | ECF_NORETURN)) == 0))
505 tree vdef;
506 if (!laststore)
507 vdef = gimple_vdef (stmt);
508 else
509 vdef = make_ssa_name (gimple_vop (cfun), new_stmt);
510 gimple_set_vdef (new_stmt, vdef);
511 if (vdef && TREE_CODE (vdef) == SSA_NAME)
512 SSA_NAME_DEF_STMT (vdef) = new_stmt;
513 laststore = new_stmt;
517 /* Second iterate over the statements forward, assigning virtual
518 operands to their uses. */
519 tree reaching_vuse = gimple_vuse (stmt);
520 for (gimple_stmt_iterator i = gsi_start (stmts);
521 !gsi_end_p (i); gsi_next (&i))
523 gimple new_stmt = gsi_stmt (i);
524 /* If the new statement possibly has a VUSE, update it with exact SSA
525 name we know will reach this one. */
526 if (gimple_has_mem_ops (new_stmt))
527 gimple_set_vuse (new_stmt, reaching_vuse);
528 gimple_set_modified (new_stmt, true);
529 if (gimple_vdef (new_stmt))
530 reaching_vuse = gimple_vdef (new_stmt);
533 /* If the new sequence does not do a store release the virtual
534 definition of the original statement. */
535 if (reaching_vuse
536 && reaching_vuse == gimple_vuse (stmt))
538 tree vdef = gimple_vdef (stmt);
539 if (vdef
540 && TREE_CODE (vdef) == SSA_NAME)
542 unlink_stmt_vdef (stmt);
543 release_ssa_name (vdef);
547 /* Finally replace the original statement with the sequence. */
548 gsi_replace_with_seq (si_p, stmts, false);
551 /* Convert EXPR into a GIMPLE value suitable for substitution on the
552 RHS of an assignment. Insert the necessary statements before
553 iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL
554 is replaced. If the call is expected to produces a result, then it
555 is replaced by an assignment of the new RHS to the result variable.
556 If the result is to be ignored, then the call is replaced by a
557 GIMPLE_NOP. A proper VDEF chain is retained by making the first
558 VUSE and the last VDEF of the whole sequence be the same as the replaced
559 statement and using new SSA names for stores in between. */
561 void
562 gimplify_and_update_call_from_tree (gimple_stmt_iterator *si_p, tree expr)
564 tree lhs;
565 gimple stmt, new_stmt;
566 gimple_stmt_iterator i;
567 gimple_seq stmts = NULL;
569 stmt = gsi_stmt (*si_p);
571 gcc_assert (is_gimple_call (stmt));
573 push_gimplify_context (gimple_in_ssa_p (cfun));
575 lhs = gimple_call_lhs (stmt);
576 if (lhs == NULL_TREE)
578 gimplify_and_add (expr, &stmts);
579 /* We can end up with folding a memcpy of an empty class assignment
580 which gets optimized away by C++ gimplification. */
581 if (gimple_seq_empty_p (stmts))
583 pop_gimplify_context (NULL);
584 if (gimple_in_ssa_p (cfun))
586 unlink_stmt_vdef (stmt);
587 release_defs (stmt);
589 gsi_replace (si_p, gimple_build_nop (), true);
590 return;
593 else
595 tree tmp = get_initialized_tmp_var (expr, &stmts, NULL);
596 new_stmt = gimple_build_assign (lhs, tmp);
597 i = gsi_last (stmts);
598 gsi_insert_after_without_update (&i, new_stmt,
599 GSI_CONTINUE_LINKING);
602 pop_gimplify_context (NULL);
604 gsi_replace_with_seq_vops (si_p, stmts);
608 /* Replace the call at *GSI with the gimple value VAL. */
610 static void
611 replace_call_with_value (gimple_stmt_iterator *gsi, tree val)
613 gimple stmt = gsi_stmt (*gsi);
614 tree lhs = gimple_call_lhs (stmt);
615 gimple repl;
616 if (lhs)
618 if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (val)))
619 val = fold_convert (TREE_TYPE (lhs), val);
620 repl = gimple_build_assign (lhs, val);
622 else
623 repl = gimple_build_nop ();
624 tree vdef = gimple_vdef (stmt);
625 if (vdef && TREE_CODE (vdef) == SSA_NAME)
627 unlink_stmt_vdef (stmt);
628 release_ssa_name (vdef);
630 gsi_replace (gsi, repl, true);
633 /* Replace the call at *GSI with the new call REPL and fold that
634 again. */
636 static void
637 replace_call_with_call_and_fold (gimple_stmt_iterator *gsi, gimple repl)
639 gimple stmt = gsi_stmt (*gsi);
640 gimple_call_set_lhs (repl, gimple_call_lhs (stmt));
641 gimple_set_location (repl, gimple_location (stmt));
642 if (gimple_vdef (stmt)
643 && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
645 gimple_set_vdef (repl, gimple_vdef (stmt));
646 gimple_set_vuse (repl, gimple_vuse (stmt));
647 SSA_NAME_DEF_STMT (gimple_vdef (repl)) = repl;
649 gsi_replace (gsi, repl, true);
650 fold_stmt (gsi);
653 /* Return true if VAR is a VAR_DECL or a component thereof. */
655 static bool
656 var_decl_component_p (tree var)
658 tree inner = var;
659 while (handled_component_p (inner))
660 inner = TREE_OPERAND (inner, 0);
661 return SSA_VAR_P (inner);
664 /* Fold function call to builtin mem{{,p}cpy,move}. Return
665 false if no simplification can be made.
666 If ENDP is 0, return DEST (like memcpy).
667 If ENDP is 1, return DEST+LEN (like mempcpy).
668 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
669 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
670 (memmove). */
672 static bool
673 gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi,
674 tree dest, tree src, int endp)
676 gimple stmt = gsi_stmt (*gsi);
677 tree lhs = gimple_call_lhs (stmt);
678 tree len = gimple_call_arg (stmt, 2);
679 tree destvar, srcvar;
680 location_t loc = gimple_location (stmt);
682 /* If the LEN parameter is zero, return DEST. */
683 if (integer_zerop (len))
685 gimple repl;
686 if (gimple_call_lhs (stmt))
687 repl = gimple_build_assign (gimple_call_lhs (stmt), dest);
688 else
689 repl = gimple_build_nop ();
690 tree vdef = gimple_vdef (stmt);
691 if (vdef && TREE_CODE (vdef) == SSA_NAME)
693 unlink_stmt_vdef (stmt);
694 release_ssa_name (vdef);
696 gsi_replace (gsi, repl, true);
697 return true;
700 /* If SRC and DEST are the same (and not volatile), return
701 DEST{,+LEN,+LEN-1}. */
702 if (operand_equal_p (src, dest, 0))
704 unlink_stmt_vdef (stmt);
705 if (gimple_vdef (stmt) && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
706 release_ssa_name (gimple_vdef (stmt));
707 if (!lhs)
709 gsi_replace (gsi, gimple_build_nop (), true);
710 return true;
712 goto done;
714 else
716 tree srctype, desttype;
717 unsigned int src_align, dest_align;
718 tree off0;
720 /* Build accesses at offset zero with a ref-all character type. */
721 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
722 ptr_mode, true), 0);
724 /* If we can perform the copy efficiently with first doing all loads
725 and then all stores inline it that way. Currently efficiently
726 means that we can load all the memory into a single integer
727 register which is what MOVE_MAX gives us. */
728 src_align = get_pointer_alignment (src);
729 dest_align = get_pointer_alignment (dest);
730 if (tree_fits_uhwi_p (len)
731 && compare_tree_int (len, MOVE_MAX) <= 0
732 /* ??? Don't transform copies from strings with known length this
733 confuses the tree-ssa-strlen.c. This doesn't handle
734 the case in gcc.dg/strlenopt-8.c which is XFAILed for that
735 reason. */
736 && !c_strlen (src, 2))
738 unsigned ilen = tree_to_uhwi (len);
739 if (exact_log2 (ilen) != -1)
741 tree type = lang_hooks.types.type_for_size (ilen * 8, 1);
742 if (type
743 && TYPE_MODE (type) != BLKmode
744 && (GET_MODE_SIZE (TYPE_MODE (type)) * BITS_PER_UNIT
745 == ilen * 8)
746 /* If the destination pointer is not aligned we must be able
747 to emit an unaligned store. */
748 && (dest_align >= GET_MODE_ALIGNMENT (TYPE_MODE (type))
749 || !SLOW_UNALIGNED_ACCESS (TYPE_MODE (type), dest_align)))
751 tree srctype = type;
752 tree desttype = type;
753 if (src_align < GET_MODE_ALIGNMENT (TYPE_MODE (type)))
754 srctype = build_aligned_type (type, src_align);
755 tree srcmem = fold_build2 (MEM_REF, srctype, src, off0);
756 tree tem = fold_const_aggregate_ref (srcmem);
757 if (tem)
758 srcmem = tem;
759 else if (src_align < GET_MODE_ALIGNMENT (TYPE_MODE (type))
760 && SLOW_UNALIGNED_ACCESS (TYPE_MODE (type),
761 src_align))
762 srcmem = NULL_TREE;
763 if (srcmem)
765 gimple new_stmt;
766 if (is_gimple_reg_type (TREE_TYPE (srcmem)))
768 new_stmt = gimple_build_assign (NULL_TREE, srcmem);
769 if (gimple_in_ssa_p (cfun))
770 srcmem = make_ssa_name (TREE_TYPE (srcmem),
771 new_stmt);
772 else
773 srcmem = create_tmp_reg (TREE_TYPE (srcmem));
774 gimple_assign_set_lhs (new_stmt, srcmem);
775 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
776 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
778 if (dest_align < GET_MODE_ALIGNMENT (TYPE_MODE (type)))
779 desttype = build_aligned_type (type, dest_align);
780 new_stmt
781 = gimple_build_assign (fold_build2 (MEM_REF, desttype,
782 dest, off0),
783 srcmem);
784 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
785 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
786 if (gimple_vdef (new_stmt)
787 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
788 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
789 if (!lhs)
791 gsi_replace (gsi, new_stmt, true);
792 return true;
794 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
795 goto done;
801 if (endp == 3)
803 /* Both DEST and SRC must be pointer types.
804 ??? This is what old code did. Is the testing for pointer types
805 really mandatory?
807 If either SRC is readonly or length is 1, we can use memcpy. */
808 if (!dest_align || !src_align)
809 return false;
810 if (readonly_data_expr (src)
811 || (tree_fits_uhwi_p (len)
812 && (MIN (src_align, dest_align) / BITS_PER_UNIT
813 >= tree_to_uhwi (len))))
815 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
816 if (!fn)
817 return false;
818 gimple_call_set_fndecl (stmt, fn);
819 gimple_call_set_arg (stmt, 0, dest);
820 gimple_call_set_arg (stmt, 1, src);
821 fold_stmt (gsi);
822 return true;
825 /* If *src and *dest can't overlap, optimize into memcpy as well. */
826 if (TREE_CODE (src) == ADDR_EXPR
827 && TREE_CODE (dest) == ADDR_EXPR)
829 tree src_base, dest_base, fn;
830 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
831 HOST_WIDE_INT size = -1;
832 HOST_WIDE_INT maxsize = -1;
834 srcvar = TREE_OPERAND (src, 0);
835 src_base = get_ref_base_and_extent (srcvar, &src_offset,
836 &size, &maxsize);
837 destvar = TREE_OPERAND (dest, 0);
838 dest_base = get_ref_base_and_extent (destvar, &dest_offset,
839 &size, &maxsize);
840 if (tree_fits_uhwi_p (len))
841 maxsize = tree_to_uhwi (len);
842 else
843 maxsize = -1;
844 src_offset /= BITS_PER_UNIT;
845 dest_offset /= BITS_PER_UNIT;
846 if (SSA_VAR_P (src_base)
847 && SSA_VAR_P (dest_base))
849 if (operand_equal_p (src_base, dest_base, 0)
850 && ranges_overlap_p (src_offset, maxsize,
851 dest_offset, maxsize))
852 return false;
854 else if (TREE_CODE (src_base) == MEM_REF
855 && TREE_CODE (dest_base) == MEM_REF)
857 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
858 TREE_OPERAND (dest_base, 0), 0))
859 return false;
860 offset_int off = mem_ref_offset (src_base) + src_offset;
861 if (!wi::fits_shwi_p (off))
862 return false;
863 src_offset = off.to_shwi ();
865 off = mem_ref_offset (dest_base) + dest_offset;
866 if (!wi::fits_shwi_p (off))
867 return false;
868 dest_offset = off.to_shwi ();
869 if (ranges_overlap_p (src_offset, maxsize,
870 dest_offset, maxsize))
871 return false;
873 else
874 return false;
876 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
877 if (!fn)
878 return false;
879 gimple_call_set_fndecl (stmt, fn);
880 gimple_call_set_arg (stmt, 0, dest);
881 gimple_call_set_arg (stmt, 1, src);
882 fold_stmt (gsi);
883 return true;
886 /* If the destination and source do not alias optimize into
887 memcpy as well. */
888 if ((is_gimple_min_invariant (dest)
889 || TREE_CODE (dest) == SSA_NAME)
890 && (is_gimple_min_invariant (src)
891 || TREE_CODE (src) == SSA_NAME))
893 ao_ref destr, srcr;
894 ao_ref_init_from_ptr_and_size (&destr, dest, len);
895 ao_ref_init_from_ptr_and_size (&srcr, src, len);
896 if (!refs_may_alias_p_1 (&destr, &srcr, false))
898 tree fn;
899 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
900 if (!fn)
901 return false;
902 gimple_call_set_fndecl (stmt, fn);
903 gimple_call_set_arg (stmt, 0, dest);
904 gimple_call_set_arg (stmt, 1, src);
905 fold_stmt (gsi);
906 return true;
910 return false;
913 if (!tree_fits_shwi_p (len))
914 return false;
915 /* FIXME:
916 This logic lose for arguments like (type *)malloc (sizeof (type)),
917 since we strip the casts of up to VOID return value from malloc.
918 Perhaps we ought to inherit type from non-VOID argument here? */
919 STRIP_NOPS (src);
920 STRIP_NOPS (dest);
921 if (!POINTER_TYPE_P (TREE_TYPE (src))
922 || !POINTER_TYPE_P (TREE_TYPE (dest)))
923 return false;
924 /* In the following try to find a type that is most natural to be
925 used for the memcpy source and destination and that allows
926 the most optimization when memcpy is turned into a plain assignment
927 using that type. In theory we could always use a char[len] type
928 but that only gains us that the destination and source possibly
929 no longer will have their address taken. */
930 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
931 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
933 tree tem = TREE_OPERAND (src, 0);
934 STRIP_NOPS (tem);
935 if (tem != TREE_OPERAND (src, 0))
936 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
938 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
940 tree tem = TREE_OPERAND (dest, 0);
941 STRIP_NOPS (tem);
942 if (tem != TREE_OPERAND (dest, 0))
943 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
945 srctype = TREE_TYPE (TREE_TYPE (src));
946 if (TREE_CODE (srctype) == ARRAY_TYPE
947 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
949 srctype = TREE_TYPE (srctype);
950 STRIP_NOPS (src);
951 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
953 desttype = TREE_TYPE (TREE_TYPE (dest));
954 if (TREE_CODE (desttype) == ARRAY_TYPE
955 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
957 desttype = TREE_TYPE (desttype);
958 STRIP_NOPS (dest);
959 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
961 if (TREE_ADDRESSABLE (srctype)
962 || TREE_ADDRESSABLE (desttype))
963 return false;
965 /* Make sure we are not copying using a floating-point mode or
966 a type whose size possibly does not match its precision. */
967 if (FLOAT_MODE_P (TYPE_MODE (desttype))
968 || TREE_CODE (desttype) == BOOLEAN_TYPE
969 || TREE_CODE (desttype) == ENUMERAL_TYPE)
970 desttype = bitwise_type_for_mode (TYPE_MODE (desttype));
971 if (FLOAT_MODE_P (TYPE_MODE (srctype))
972 || TREE_CODE (srctype) == BOOLEAN_TYPE
973 || TREE_CODE (srctype) == ENUMERAL_TYPE)
974 srctype = bitwise_type_for_mode (TYPE_MODE (srctype));
975 if (!srctype)
976 srctype = desttype;
977 if (!desttype)
978 desttype = srctype;
979 if (!srctype)
980 return false;
982 src_align = get_pointer_alignment (src);
983 dest_align = get_pointer_alignment (dest);
984 if (dest_align < TYPE_ALIGN (desttype)
985 || src_align < TYPE_ALIGN (srctype))
986 return false;
988 destvar = dest;
989 STRIP_NOPS (destvar);
990 if (TREE_CODE (destvar) == ADDR_EXPR
991 && var_decl_component_p (TREE_OPERAND (destvar, 0))
992 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
993 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
994 else
995 destvar = NULL_TREE;
997 srcvar = src;
998 STRIP_NOPS (srcvar);
999 if (TREE_CODE (srcvar) == ADDR_EXPR
1000 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
1001 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
1003 if (!destvar
1004 || src_align >= TYPE_ALIGN (desttype))
1005 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
1006 srcvar, off0);
1007 else if (!STRICT_ALIGNMENT)
1009 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
1010 src_align);
1011 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
1013 else
1014 srcvar = NULL_TREE;
1016 else
1017 srcvar = NULL_TREE;
1019 if (srcvar == NULL_TREE && destvar == NULL_TREE)
1020 return false;
1022 if (srcvar == NULL_TREE)
1024 STRIP_NOPS (src);
1025 if (src_align >= TYPE_ALIGN (desttype))
1026 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
1027 else
1029 if (STRICT_ALIGNMENT)
1030 return false;
1031 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
1032 src_align);
1033 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
1036 else if (destvar == NULL_TREE)
1038 STRIP_NOPS (dest);
1039 if (dest_align >= TYPE_ALIGN (srctype))
1040 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
1041 else
1043 if (STRICT_ALIGNMENT)
1044 return false;
1045 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
1046 dest_align);
1047 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
1051 gimple new_stmt;
1052 if (is_gimple_reg_type (TREE_TYPE (srcvar)))
1054 new_stmt = gimple_build_assign (NULL_TREE, srcvar);
1055 if (gimple_in_ssa_p (cfun))
1056 srcvar = make_ssa_name (TREE_TYPE (srcvar), new_stmt);
1057 else
1058 srcvar = create_tmp_reg (TREE_TYPE (srcvar));
1059 gimple_assign_set_lhs (new_stmt, srcvar);
1060 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
1061 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1063 new_stmt = gimple_build_assign (destvar, srcvar);
1064 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
1065 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
1066 if (gimple_vdef (new_stmt)
1067 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1068 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1069 if (!lhs)
1071 gsi_replace (gsi, new_stmt, true);
1072 return true;
1074 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1077 done:
1078 if (endp == 0 || endp == 3)
1079 len = NULL_TREE;
1080 else if (endp == 2)
1081 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
1082 ssize_int (1));
1083 if (endp == 2 || endp == 1)
1084 dest = fold_build_pointer_plus_loc (loc, dest, len);
1086 dest = force_gimple_operand_gsi (gsi, dest, false, NULL_TREE, true,
1087 GSI_SAME_STMT);
1088 gimple repl = gimple_build_assign (lhs, dest);
1089 gsi_replace (gsi, repl, true);
1090 return true;
1093 /* Fold function call to builtin memset or bzero at *GSI setting the
1094 memory of size LEN to VAL. Return whether a simplification was made. */
1096 static bool
1097 gimple_fold_builtin_memset (gimple_stmt_iterator *gsi, tree c, tree len)
1099 gimple stmt = gsi_stmt (*gsi);
1100 tree etype;
1101 unsigned HOST_WIDE_INT length, cval;
1103 /* If the LEN parameter is zero, return DEST. */
1104 if (integer_zerop (len))
1106 replace_call_with_value (gsi, gimple_call_arg (stmt, 0));
1107 return true;
1110 if (! tree_fits_uhwi_p (len))
1111 return false;
1113 if (TREE_CODE (c) != INTEGER_CST)
1114 return false;
1116 tree dest = gimple_call_arg (stmt, 0);
1117 tree var = dest;
1118 if (TREE_CODE (var) != ADDR_EXPR)
1119 return false;
1121 var = TREE_OPERAND (var, 0);
1122 if (TREE_THIS_VOLATILE (var))
1123 return false;
1125 etype = TREE_TYPE (var);
1126 if (TREE_CODE (etype) == ARRAY_TYPE)
1127 etype = TREE_TYPE (etype);
1129 if (!INTEGRAL_TYPE_P (etype)
1130 && !POINTER_TYPE_P (etype))
1131 return NULL_TREE;
1133 if (! var_decl_component_p (var))
1134 return NULL_TREE;
1136 length = tree_to_uhwi (len);
1137 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
1138 || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
1139 return NULL_TREE;
1141 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
1142 return NULL_TREE;
1144 if (integer_zerop (c))
1145 cval = 0;
1146 else
1148 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
1149 return NULL_TREE;
1151 cval = TREE_INT_CST_LOW (c);
1152 cval &= 0xff;
1153 cval |= cval << 8;
1154 cval |= cval << 16;
1155 cval |= (cval << 31) << 1;
1158 var = fold_build2 (MEM_REF, etype, dest, build_int_cst (ptr_type_node, 0));
1159 gimple store = gimple_build_assign (var, build_int_cst_type (etype, cval));
1160 gimple_set_vuse (store, gimple_vuse (stmt));
1161 tree vdef = gimple_vdef (stmt);
1162 if (vdef && TREE_CODE (vdef) == SSA_NAME)
1164 gimple_set_vdef (store, gimple_vdef (stmt));
1165 SSA_NAME_DEF_STMT (gimple_vdef (stmt)) = store;
1167 gsi_insert_before (gsi, store, GSI_SAME_STMT);
1168 if (gimple_call_lhs (stmt))
1170 gimple asgn = gimple_build_assign (gimple_call_lhs (stmt), dest);
1171 gsi_replace (gsi, asgn, true);
1173 else
1175 gimple_stmt_iterator gsi2 = *gsi;
1176 gsi_prev (gsi);
1177 gsi_remove (&gsi2, true);
1180 return true;
1184 /* Return the string length, maximum string length or maximum value of
1185 ARG in LENGTH.
1186 If ARG is an SSA name variable, follow its use-def chains. If LENGTH
1187 is not NULL and, for TYPE == 0, its value is not equal to the length
1188 we determine or if we are unable to determine the length or value,
1189 return false. VISITED is a bitmap of visited variables.
1190 TYPE is 0 if string length should be returned, 1 for maximum string
1191 length and 2 for maximum value ARG can have. */
1193 static bool
1194 get_maxval_strlen (tree arg, tree *length, bitmap *visited, int type)
1196 tree var, val;
1197 gimple def_stmt;
1199 if (TREE_CODE (arg) != SSA_NAME)
1201 /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */
1202 if (TREE_CODE (arg) == ADDR_EXPR
1203 && TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF
1204 && integer_zerop (TREE_OPERAND (TREE_OPERAND (arg, 0), 1)))
1206 tree aop0 = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
1207 if (TREE_CODE (aop0) == INDIRECT_REF
1208 && TREE_CODE (TREE_OPERAND (aop0, 0)) == SSA_NAME)
1209 return get_maxval_strlen (TREE_OPERAND (aop0, 0),
1210 length, visited, type);
1213 if (type == 2)
1215 val = arg;
1216 if (TREE_CODE (val) != INTEGER_CST
1217 || tree_int_cst_sgn (val) < 0)
1218 return false;
1220 else
1221 val = c_strlen (arg, 1);
1222 if (!val)
1223 return false;
1225 if (*length)
1227 if (type > 0)
1229 if (TREE_CODE (*length) != INTEGER_CST
1230 || TREE_CODE (val) != INTEGER_CST)
1231 return false;
1233 if (tree_int_cst_lt (*length, val))
1234 *length = val;
1235 return true;
1237 else if (simple_cst_equal (val, *length) != 1)
1238 return false;
1241 *length = val;
1242 return true;
1245 /* If ARG is registered for SSA update we cannot look at its defining
1246 statement. */
1247 if (name_registered_for_update_p (arg))
1248 return false;
1250 /* If we were already here, break the infinite cycle. */
1251 if (!*visited)
1252 *visited = BITMAP_ALLOC (NULL);
1253 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (arg)))
1254 return true;
1256 var = arg;
1257 def_stmt = SSA_NAME_DEF_STMT (var);
1259 switch (gimple_code (def_stmt))
1261 case GIMPLE_ASSIGN:
1262 /* The RHS of the statement defining VAR must either have a
1263 constant length or come from another SSA_NAME with a constant
1264 length. */
1265 if (gimple_assign_single_p (def_stmt)
1266 || gimple_assign_unary_nop_p (def_stmt))
1268 tree rhs = gimple_assign_rhs1 (def_stmt);
1269 return get_maxval_strlen (rhs, length, visited, type);
1271 else if (gimple_assign_rhs_code (def_stmt) == COND_EXPR)
1273 tree op2 = gimple_assign_rhs2 (def_stmt);
1274 tree op3 = gimple_assign_rhs3 (def_stmt);
1275 return get_maxval_strlen (op2, length, visited, type)
1276 && get_maxval_strlen (op3, length, visited, type);
1278 return false;
1280 case GIMPLE_PHI:
1282 /* All the arguments of the PHI node must have the same constant
1283 length. */
1284 unsigned i;
1286 for (i = 0; i < gimple_phi_num_args (def_stmt); i++)
1288 tree arg = gimple_phi_arg (def_stmt, i)->def;
1290 /* If this PHI has itself as an argument, we cannot
1291 determine the string length of this argument. However,
1292 if we can find a constant string length for the other
1293 PHI args then we can still be sure that this is a
1294 constant string length. So be optimistic and just
1295 continue with the next argument. */
1296 if (arg == gimple_phi_result (def_stmt))
1297 continue;
1299 if (!get_maxval_strlen (arg, length, visited, type))
1300 return false;
1303 return true;
1305 default:
1306 return false;
1310 tree
1311 get_maxval_strlen (tree arg, int type)
1313 bitmap visited = NULL;
1314 tree len = NULL_TREE;
1315 if (!get_maxval_strlen (arg, &len, &visited, type))
1316 len = NULL_TREE;
1317 if (visited)
1318 BITMAP_FREE (visited);
1320 return len;
1324 /* Fold function call to builtin strcpy with arguments DEST and SRC.
1325 If LEN is not NULL, it represents the length of the string to be
1326 copied. Return NULL_TREE if no simplification can be made. */
1328 static bool
1329 gimple_fold_builtin_strcpy (gimple_stmt_iterator *gsi,
1330 tree dest, tree src)
1332 location_t loc = gimple_location (gsi_stmt (*gsi));
1333 tree fn;
1335 /* If SRC and DEST are the same (and not volatile), return DEST. */
1336 if (operand_equal_p (src, dest, 0))
1338 replace_call_with_value (gsi, dest);
1339 return true;
1342 if (optimize_function_for_size_p (cfun))
1343 return false;
1345 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1346 if (!fn)
1347 return false;
1349 tree len = get_maxval_strlen (src, 0);
1350 if (!len)
1351 return false;
1353 len = fold_convert_loc (loc, size_type_node, len);
1354 len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
1355 len = force_gimple_operand_gsi (gsi, len, true,
1356 NULL_TREE, true, GSI_SAME_STMT);
1357 gimple repl = gimple_build_call (fn, 3, dest, src, len);
1358 replace_call_with_call_and_fold (gsi, repl);
1359 return true;
1362 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
1363 If SLEN is not NULL, it represents the length of the source string.
1364 Return NULL_TREE if no simplification can be made. */
1366 static bool
1367 gimple_fold_builtin_strncpy (gimple_stmt_iterator *gsi,
1368 tree dest, tree src, tree len)
1370 location_t loc = gimple_location (gsi_stmt (*gsi));
1371 tree fn;
1373 /* If the LEN parameter is zero, return DEST. */
1374 if (integer_zerop (len))
1376 replace_call_with_value (gsi, dest);
1377 return true;
1380 /* We can't compare slen with len as constants below if len is not a
1381 constant. */
1382 if (TREE_CODE (len) != INTEGER_CST)
1383 return false;
1385 /* Now, we must be passed a constant src ptr parameter. */
1386 tree slen = get_maxval_strlen (src, 0);
1387 if (!slen || TREE_CODE (slen) != INTEGER_CST)
1388 return false;
1390 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
1392 /* We do not support simplification of this case, though we do
1393 support it when expanding trees into RTL. */
1394 /* FIXME: generate a call to __builtin_memset. */
1395 if (tree_int_cst_lt (slen, len))
1396 return false;
1398 /* OK transform into builtin memcpy. */
1399 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1400 if (!fn)
1401 return false;
1403 len = fold_convert_loc (loc, size_type_node, len);
1404 len = force_gimple_operand_gsi (gsi, len, true,
1405 NULL_TREE, true, GSI_SAME_STMT);
1406 gimple repl = gimple_build_call (fn, 3, dest, src, len);
1407 replace_call_with_call_and_fold (gsi, repl);
1408 return true;
1411 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
1412 to the call.
1414 Return NULL_TREE if no simplification was possible, otherwise return the
1415 simplified form of the call as a tree.
1417 The simplified form may be a constant or other expression which
1418 computes the same value, but in a more efficient manner (including
1419 calls to other builtin functions).
1421 The call may contain arguments which need to be evaluated, but
1422 which are not useful to determine the result of the call. In
1423 this case we return a chain of COMPOUND_EXPRs. The LHS of each
1424 COMPOUND_EXPR will be an argument which must be evaluated.
1425 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
1426 COMPOUND_EXPR in the chain will contain the tree for the simplified
1427 form of the builtin function call. */
1429 static bool
1430 gimple_fold_builtin_strcat (gimple_stmt_iterator *gsi, tree dst, tree src)
1432 gimple stmt = gsi_stmt (*gsi);
1433 location_t loc = gimple_location (stmt);
1435 const char *p = c_getstr (src);
1437 /* If the string length is zero, return the dst parameter. */
1438 if (p && *p == '\0')
1440 replace_call_with_value (gsi, dst);
1441 return true;
1444 if (!optimize_bb_for_speed_p (gimple_bb (stmt)))
1445 return false;
1447 /* See if we can store by pieces into (dst + strlen(dst)). */
1448 tree newdst;
1449 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
1450 tree memcpy_fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1452 if (!strlen_fn || !memcpy_fn)
1453 return false;
1455 /* If the length of the source string isn't computable don't
1456 split strcat into strlen and memcpy. */
1457 tree len = get_maxval_strlen (src, 0);
1458 if (! len)
1459 return false;
1461 /* Create strlen (dst). */
1462 gimple_seq stmts = NULL, stmts2;
1463 gimple repl = gimple_build_call (strlen_fn, 1, dst);
1464 gimple_set_location (repl, loc);
1465 if (gimple_in_ssa_p (cfun))
1466 newdst = make_ssa_name (size_type_node);
1467 else
1468 newdst = create_tmp_reg (size_type_node);
1469 gimple_call_set_lhs (repl, newdst);
1470 gimple_seq_add_stmt_without_update (&stmts, repl);
1472 /* Create (dst p+ strlen (dst)). */
1473 newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
1474 newdst = force_gimple_operand (newdst, &stmts2, true, NULL_TREE);
1475 gimple_seq_add_seq_without_update (&stmts, stmts2);
1477 len = fold_convert_loc (loc, size_type_node, len);
1478 len = size_binop_loc (loc, PLUS_EXPR, len,
1479 build_int_cst (size_type_node, 1));
1480 len = force_gimple_operand (len, &stmts2, true, NULL_TREE);
1481 gimple_seq_add_seq_without_update (&stmts, stmts2);
1483 repl = gimple_build_call (memcpy_fn, 3, newdst, src, len);
1484 gimple_seq_add_stmt_without_update (&stmts, repl);
1485 if (gimple_call_lhs (stmt))
1487 repl = gimple_build_assign (gimple_call_lhs (stmt), dst);
1488 gimple_seq_add_stmt_without_update (&stmts, repl);
1489 gsi_replace_with_seq_vops (gsi, stmts);
1490 /* gsi now points at the assignment to the lhs, get a
1491 stmt iterator to the memcpy call.
1492 ??? We can't use gsi_for_stmt as that doesn't work when the
1493 CFG isn't built yet. */
1494 gimple_stmt_iterator gsi2 = *gsi;
1495 gsi_prev (&gsi2);
1496 fold_stmt (&gsi2);
1498 else
1500 gsi_replace_with_seq_vops (gsi, stmts);
1501 fold_stmt (gsi);
1503 return true;
1506 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
1507 are the arguments to the call. */
1509 static bool
1510 gimple_fold_builtin_strcat_chk (gimple_stmt_iterator *gsi)
1512 gimple stmt = gsi_stmt (*gsi);
1513 tree dest = gimple_call_arg (stmt, 0);
1514 tree src = gimple_call_arg (stmt, 1);
1515 tree size = gimple_call_arg (stmt, 2);
1516 tree fn;
1517 const char *p;
1520 p = c_getstr (src);
1521 /* If the SRC parameter is "", return DEST. */
1522 if (p && *p == '\0')
1524 replace_call_with_value (gsi, dest);
1525 return true;
1528 if (! tree_fits_uhwi_p (size) || ! integer_all_onesp (size))
1529 return false;
1531 /* If __builtin_strcat_chk is used, assume strcat is available. */
1532 fn = builtin_decl_explicit (BUILT_IN_STRCAT);
1533 if (!fn)
1534 return false;
1536 gimple repl = gimple_build_call (fn, 2, dest, src);
1537 replace_call_with_call_and_fold (gsi, repl);
1538 return true;
1541 /* Simplify a call to the strncat builtin. */
1543 static bool
1544 gimple_fold_builtin_strncat (gimple_stmt_iterator *gsi)
1546 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
1547 tree dst = gimple_call_arg (stmt, 0);
1548 tree src = gimple_call_arg (stmt, 1);
1549 tree len = gimple_call_arg (stmt, 2);
1551 const char *p = c_getstr (src);
1553 /* If the requested length is zero, or the src parameter string
1554 length is zero, return the dst parameter. */
1555 if (integer_zerop (len) || (p && *p == '\0'))
1557 replace_call_with_value (gsi, dst);
1558 return true;
1561 /* If the requested len is greater than or equal to the string
1562 length, call strcat. */
1563 if (TREE_CODE (len) == INTEGER_CST && p
1564 && compare_tree_int (len, strlen (p)) >= 0)
1566 tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
1568 /* If the replacement _DECL isn't initialized, don't do the
1569 transformation. */
1570 if (!fn)
1571 return false;
1573 gcall *repl = gimple_build_call (fn, 2, dst, src);
1574 replace_call_with_call_and_fold (gsi, repl);
1575 return true;
1578 return false;
1581 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
1582 LEN, and SIZE. */
1584 static bool
1585 gimple_fold_builtin_strncat_chk (gimple_stmt_iterator *gsi)
1587 gimple stmt = gsi_stmt (*gsi);
1588 tree dest = gimple_call_arg (stmt, 0);
1589 tree src = gimple_call_arg (stmt, 1);
1590 tree len = gimple_call_arg (stmt, 2);
1591 tree size = gimple_call_arg (stmt, 3);
1592 tree fn;
1593 const char *p;
1595 p = c_getstr (src);
1596 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
1597 if ((p && *p == '\0')
1598 || integer_zerop (len))
1600 replace_call_with_value (gsi, dest);
1601 return true;
1604 if (! tree_fits_uhwi_p (size))
1605 return false;
1607 if (! integer_all_onesp (size))
1609 tree src_len = c_strlen (src, 1);
1610 if (src_len
1611 && tree_fits_uhwi_p (src_len)
1612 && tree_fits_uhwi_p (len)
1613 && ! tree_int_cst_lt (len, src_len))
1615 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
1616 fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
1617 if (!fn)
1618 return false;
1620 gimple repl = gimple_build_call (fn, 3, dest, src, size);
1621 replace_call_with_call_and_fold (gsi, repl);
1622 return true;
1624 return false;
1627 /* If __builtin_strncat_chk is used, assume strncat is available. */
1628 fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
1629 if (!fn)
1630 return false;
1632 gimple repl = gimple_build_call (fn, 3, dest, src, len);
1633 replace_call_with_call_and_fold (gsi, repl);
1634 return true;
1637 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
1638 to the call. IGNORE is true if the value returned
1639 by the builtin will be ignored. UNLOCKED is true is true if this
1640 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
1641 the known length of the string. Return NULL_TREE if no simplification
1642 was possible. */
1644 static bool
1645 gimple_fold_builtin_fputs (gimple_stmt_iterator *gsi,
1646 tree arg0, tree arg1,
1647 bool unlocked)
1649 gimple stmt = gsi_stmt (*gsi);
1651 /* If we're using an unlocked function, assume the other unlocked
1652 functions exist explicitly. */
1653 tree const fn_fputc = (unlocked
1654 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
1655 : builtin_decl_implicit (BUILT_IN_FPUTC));
1656 tree const fn_fwrite = (unlocked
1657 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
1658 : builtin_decl_implicit (BUILT_IN_FWRITE));
1660 /* If the return value is used, don't do the transformation. */
1661 if (gimple_call_lhs (stmt))
1662 return false;
1664 /* Get the length of the string passed to fputs. If the length
1665 can't be determined, punt. */
1666 tree len = get_maxval_strlen (arg0, 0);
1667 if (!len
1668 || TREE_CODE (len) != INTEGER_CST)
1669 return false;
1671 switch (compare_tree_int (len, 1))
1673 case -1: /* length is 0, delete the call entirely . */
1674 replace_call_with_value (gsi, integer_zero_node);
1675 return true;
1677 case 0: /* length is 1, call fputc. */
1679 const char *p = c_getstr (arg0);
1680 if (p != NULL)
1682 if (!fn_fputc)
1683 return false;
1685 gimple repl = gimple_build_call (fn_fputc, 2,
1686 build_int_cst
1687 (integer_type_node, p[0]), arg1);
1688 replace_call_with_call_and_fold (gsi, repl);
1689 return true;
1692 /* FALLTHROUGH */
1693 case 1: /* length is greater than 1, call fwrite. */
1695 /* If optimizing for size keep fputs. */
1696 if (optimize_function_for_size_p (cfun))
1697 return false;
1698 /* New argument list transforming fputs(string, stream) to
1699 fwrite(string, 1, len, stream). */
1700 if (!fn_fwrite)
1701 return false;
1703 gimple repl = gimple_build_call (fn_fwrite, 4, arg0,
1704 size_one_node, len, arg1);
1705 replace_call_with_call_and_fold (gsi, repl);
1706 return true;
1708 default:
1709 gcc_unreachable ();
1711 return false;
1714 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
1715 DEST, SRC, LEN, and SIZE are the arguments to the call.
1716 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
1717 code of the builtin. If MAXLEN is not NULL, it is maximum length
1718 passed as third argument. */
1720 static bool
1721 gimple_fold_builtin_memory_chk (gimple_stmt_iterator *gsi,
1722 tree dest, tree src, tree len, tree size,
1723 enum built_in_function fcode)
1725 gimple stmt = gsi_stmt (*gsi);
1726 location_t loc = gimple_location (stmt);
1727 bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
1728 tree fn;
1730 /* If SRC and DEST are the same (and not volatile), return DEST
1731 (resp. DEST+LEN for __mempcpy_chk). */
1732 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
1734 if (fcode != BUILT_IN_MEMPCPY_CHK)
1736 replace_call_with_value (gsi, dest);
1737 return true;
1739 else
1741 tree temp = fold_build_pointer_plus_loc (loc, dest, len);
1742 temp = force_gimple_operand_gsi (gsi, temp,
1743 false, NULL_TREE, true,
1744 GSI_SAME_STMT);
1745 replace_call_with_value (gsi, temp);
1746 return true;
1750 if (! tree_fits_uhwi_p (size))
1751 return false;
1753 tree maxlen = get_maxval_strlen (len, 2);
1754 if (! integer_all_onesp (size))
1756 if (! tree_fits_uhwi_p (len))
1758 /* If LEN is not constant, try MAXLEN too.
1759 For MAXLEN only allow optimizing into non-_ocs function
1760 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
1761 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
1763 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
1765 /* (void) __mempcpy_chk () can be optimized into
1766 (void) __memcpy_chk (). */
1767 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
1768 if (!fn)
1769 return false;
1771 gimple repl = gimple_build_call (fn, 4, dest, src, len, size);
1772 replace_call_with_call_and_fold (gsi, repl);
1773 return true;
1775 return false;
1778 else
1779 maxlen = len;
1781 if (tree_int_cst_lt (size, maxlen))
1782 return false;
1785 fn = NULL_TREE;
1786 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
1787 mem{cpy,pcpy,move,set} is available. */
1788 switch (fcode)
1790 case BUILT_IN_MEMCPY_CHK:
1791 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
1792 break;
1793 case BUILT_IN_MEMPCPY_CHK:
1794 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
1795 break;
1796 case BUILT_IN_MEMMOVE_CHK:
1797 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
1798 break;
1799 case BUILT_IN_MEMSET_CHK:
1800 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
1801 break;
1802 default:
1803 break;
1806 if (!fn)
1807 return false;
1809 gimple repl = gimple_build_call (fn, 3, dest, src, len);
1810 replace_call_with_call_and_fold (gsi, repl);
1811 return true;
1814 /* Fold a call to the __st[rp]cpy_chk builtin.
1815 DEST, SRC, and SIZE are the arguments to the call.
1816 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
1817 code of the builtin. If MAXLEN is not NULL, it is maximum length of
1818 strings passed as second argument. */
1820 static bool
1821 gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator *gsi,
1822 tree dest,
1823 tree src, tree size,
1824 enum built_in_function fcode)
1826 gimple stmt = gsi_stmt (*gsi);
1827 location_t loc = gimple_location (stmt);
1828 bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
1829 tree len, fn;
1831 /* If SRC and DEST are the same (and not volatile), return DEST. */
1832 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
1834 replace_call_with_value (gsi, dest);
1835 return true;
1838 if (! tree_fits_uhwi_p (size))
1839 return false;
1841 tree maxlen = get_maxval_strlen (src, 1);
1842 if (! integer_all_onesp (size))
1844 len = c_strlen (src, 1);
1845 if (! len || ! tree_fits_uhwi_p (len))
1847 /* If LEN is not constant, try MAXLEN too.
1848 For MAXLEN only allow optimizing into non-_ocs function
1849 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
1850 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
1852 if (fcode == BUILT_IN_STPCPY_CHK)
1854 if (! ignore)
1855 return false;
1857 /* If return value of __stpcpy_chk is ignored,
1858 optimize into __strcpy_chk. */
1859 fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
1860 if (!fn)
1861 return false;
1863 gimple repl = gimple_build_call (fn, 3, dest, src, size);
1864 replace_call_with_call_and_fold (gsi, repl);
1865 return true;
1868 if (! len || TREE_SIDE_EFFECTS (len))
1869 return false;
1871 /* If c_strlen returned something, but not a constant,
1872 transform __strcpy_chk into __memcpy_chk. */
1873 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
1874 if (!fn)
1875 return false;
1877 len = fold_convert_loc (loc, size_type_node, len);
1878 len = size_binop_loc (loc, PLUS_EXPR, len,
1879 build_int_cst (size_type_node, 1));
1880 len = force_gimple_operand_gsi (gsi, len, true, NULL_TREE,
1881 true, GSI_SAME_STMT);
1882 gimple repl = gimple_build_call (fn, 4, dest, src, len, size);
1883 replace_call_with_call_and_fold (gsi, repl);
1884 return true;
1887 else
1888 maxlen = len;
1890 if (! tree_int_cst_lt (maxlen, size))
1891 return false;
1894 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
1895 fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK
1896 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
1897 if (!fn)
1898 return false;
1900 gimple repl = gimple_build_call (fn, 2, dest, src);
1901 replace_call_with_call_and_fold (gsi, repl);
1902 return true;
1905 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
1906 are the arguments to the call. If MAXLEN is not NULL, it is maximum
1907 length passed as third argument. IGNORE is true if return value can be
1908 ignored. FCODE is the BUILT_IN_* code of the builtin. */
1910 static bool
1911 gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator *gsi,
1912 tree dest, tree src,
1913 tree len, tree size,
1914 enum built_in_function fcode)
1916 gimple stmt = gsi_stmt (*gsi);
1917 bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
1918 tree fn;
1920 if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
1922 /* If return value of __stpncpy_chk is ignored,
1923 optimize into __strncpy_chk. */
1924 fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
1925 if (fn)
1927 gimple repl = gimple_build_call (fn, 4, dest, src, len, size);
1928 replace_call_with_call_and_fold (gsi, repl);
1929 return true;
1933 if (! tree_fits_uhwi_p (size))
1934 return false;
1936 tree maxlen = get_maxval_strlen (len, 2);
1937 if (! integer_all_onesp (size))
1939 if (! tree_fits_uhwi_p (len))
1941 /* If LEN is not constant, try MAXLEN too.
1942 For MAXLEN only allow optimizing into non-_ocs function
1943 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
1944 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
1945 return false;
1947 else
1948 maxlen = len;
1950 if (tree_int_cst_lt (size, maxlen))
1951 return false;
1954 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
1955 fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK
1956 ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
1957 if (!fn)
1958 return false;
1960 gimple repl = gimple_build_call (fn, 3, dest, src, len);
1961 replace_call_with_call_and_fold (gsi, repl);
1962 return true;
1965 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
1966 Return NULL_TREE if no simplification can be made. */
1968 static bool
1969 gimple_fold_builtin_stpcpy (gimple_stmt_iterator *gsi)
1971 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
1972 location_t loc = gimple_location (stmt);
1973 tree dest = gimple_call_arg (stmt, 0);
1974 tree src = gimple_call_arg (stmt, 1);
1975 tree fn, len, lenp1;
1977 /* If the result is unused, replace stpcpy with strcpy. */
1978 if (gimple_call_lhs (stmt) == NULL_TREE)
1980 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
1981 if (!fn)
1982 return false;
1983 gimple_call_set_fndecl (stmt, fn);
1984 fold_stmt (gsi);
1985 return true;
1988 len = c_strlen (src, 1);
1989 if (!len
1990 || TREE_CODE (len) != INTEGER_CST)
1991 return false;
1993 if (optimize_function_for_size_p (cfun)
1994 /* If length is zero it's small enough. */
1995 && !integer_zerop (len))
1996 return false;
1998 /* If the source has a known length replace stpcpy with memcpy. */
1999 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2000 if (!fn)
2001 return false;
2003 gimple_seq stmts = NULL;
2004 tree tem = gimple_convert (&stmts, loc, size_type_node, len);
2005 lenp1 = gimple_build (&stmts, loc, PLUS_EXPR, size_type_node,
2006 tem, build_int_cst (size_type_node, 1));
2007 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
2008 gcall *repl = gimple_build_call (fn, 3, dest, src, lenp1);
2009 gimple_set_vuse (repl, gimple_vuse (stmt));
2010 gimple_set_vdef (repl, gimple_vdef (stmt));
2011 if (gimple_vdef (repl)
2012 && TREE_CODE (gimple_vdef (repl)) == SSA_NAME)
2013 SSA_NAME_DEF_STMT (gimple_vdef (repl)) = repl;
2014 gsi_insert_before (gsi, repl, GSI_SAME_STMT);
2015 /* Replace the result with dest + len. */
2016 stmts = NULL;
2017 tem = gimple_convert (&stmts, loc, sizetype, len);
2018 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
2019 gassign *ret = gimple_build_assign (gimple_call_lhs (stmt),
2020 POINTER_PLUS_EXPR, dest, tem);
2021 gsi_replace (gsi, ret, true);
2022 /* Finally fold the memcpy call. */
2023 gimple_stmt_iterator gsi2 = *gsi;
2024 gsi_prev (&gsi2);
2025 fold_stmt (&gsi2);
2026 return true;
2029 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
2030 NULL_TREE if a normal call should be emitted rather than expanding
2031 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
2032 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
2033 passed as second argument. */
2035 static bool
2036 gimple_fold_builtin_snprintf_chk (gimple_stmt_iterator *gsi,
2037 enum built_in_function fcode)
2039 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
2040 tree dest, size, len, fn, fmt, flag;
2041 const char *fmt_str;
2043 /* Verify the required arguments in the original call. */
2044 if (gimple_call_num_args (stmt) < 5)
2045 return false;
2047 dest = gimple_call_arg (stmt, 0);
2048 len = gimple_call_arg (stmt, 1);
2049 flag = gimple_call_arg (stmt, 2);
2050 size = gimple_call_arg (stmt, 3);
2051 fmt = gimple_call_arg (stmt, 4);
2053 if (! tree_fits_uhwi_p (size))
2054 return false;
2056 if (! integer_all_onesp (size))
2058 tree maxlen = get_maxval_strlen (len, 2);
2059 if (! tree_fits_uhwi_p (len))
2061 /* If LEN is not constant, try MAXLEN too.
2062 For MAXLEN only allow optimizing into non-_ocs function
2063 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2064 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
2065 return false;
2067 else
2068 maxlen = len;
2070 if (tree_int_cst_lt (size, maxlen))
2071 return false;
2074 if (!init_target_chars ())
2075 return false;
2077 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
2078 or if format doesn't contain % chars or is "%s". */
2079 if (! integer_zerop (flag))
2081 fmt_str = c_getstr (fmt);
2082 if (fmt_str == NULL)
2083 return false;
2084 if (strchr (fmt_str, target_percent) != NULL
2085 && strcmp (fmt_str, target_percent_s))
2086 return false;
2089 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
2090 available. */
2091 fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
2092 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
2093 if (!fn)
2094 return false;
2096 /* Replace the called function and the first 5 argument by 3 retaining
2097 trailing varargs. */
2098 gimple_call_set_fndecl (stmt, fn);
2099 gimple_call_set_fntype (stmt, TREE_TYPE (fn));
2100 gimple_call_set_arg (stmt, 0, dest);
2101 gimple_call_set_arg (stmt, 1, len);
2102 gimple_call_set_arg (stmt, 2, fmt);
2103 for (unsigned i = 3; i < gimple_call_num_args (stmt) - 2; ++i)
2104 gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2));
2105 gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2);
2106 fold_stmt (gsi);
2107 return true;
2110 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
2111 Return NULL_TREE if a normal call should be emitted rather than
2112 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
2113 or BUILT_IN_VSPRINTF_CHK. */
2115 static bool
2116 gimple_fold_builtin_sprintf_chk (gimple_stmt_iterator *gsi,
2117 enum built_in_function fcode)
2119 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
2120 tree dest, size, len, fn, fmt, flag;
2121 const char *fmt_str;
2122 unsigned nargs = gimple_call_num_args (stmt);
2124 /* Verify the required arguments in the original call. */
2125 if (nargs < 4)
2126 return false;
2127 dest = gimple_call_arg (stmt, 0);
2128 flag = gimple_call_arg (stmt, 1);
2129 size = gimple_call_arg (stmt, 2);
2130 fmt = gimple_call_arg (stmt, 3);
2132 if (! tree_fits_uhwi_p (size))
2133 return false;
2135 len = NULL_TREE;
2137 if (!init_target_chars ())
2138 return false;
2140 /* Check whether the format is a literal string constant. */
2141 fmt_str = c_getstr (fmt);
2142 if (fmt_str != NULL)
2144 /* If the format doesn't contain % args or %%, we know the size. */
2145 if (strchr (fmt_str, target_percent) == 0)
2147 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
2148 len = build_int_cstu (size_type_node, strlen (fmt_str));
2150 /* If the format is "%s" and first ... argument is a string literal,
2151 we know the size too. */
2152 else if (fcode == BUILT_IN_SPRINTF_CHK
2153 && strcmp (fmt_str, target_percent_s) == 0)
2155 tree arg;
2157 if (nargs == 5)
2159 arg = gimple_call_arg (stmt, 4);
2160 if (POINTER_TYPE_P (TREE_TYPE (arg)))
2162 len = c_strlen (arg, 1);
2163 if (! len || ! tree_fits_uhwi_p (len))
2164 len = NULL_TREE;
2170 if (! integer_all_onesp (size))
2172 if (! len || ! tree_int_cst_lt (len, size))
2173 return false;
2176 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
2177 or if format doesn't contain % chars or is "%s". */
2178 if (! integer_zerop (flag))
2180 if (fmt_str == NULL)
2181 return false;
2182 if (strchr (fmt_str, target_percent) != NULL
2183 && strcmp (fmt_str, target_percent_s))
2184 return false;
2187 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
2188 fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
2189 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
2190 if (!fn)
2191 return false;
2193 /* Replace the called function and the first 4 argument by 2 retaining
2194 trailing varargs. */
2195 gimple_call_set_fndecl (stmt, fn);
2196 gimple_call_set_fntype (stmt, TREE_TYPE (fn));
2197 gimple_call_set_arg (stmt, 0, dest);
2198 gimple_call_set_arg (stmt, 1, fmt);
2199 for (unsigned i = 2; i < gimple_call_num_args (stmt) - 2; ++i)
2200 gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2));
2201 gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2);
2202 fold_stmt (gsi);
2203 return true;
2206 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
2207 ORIG may be null if this is a 2-argument call. We don't attempt to
2208 simplify calls with more than 3 arguments.
2210 Return NULL_TREE if no simplification was possible, otherwise return the
2211 simplified form of the call as a tree. If IGNORED is true, it means that
2212 the caller does not use the returned value of the function. */
2214 static bool
2215 gimple_fold_builtin_sprintf (gimple_stmt_iterator *gsi)
2217 gimple stmt = gsi_stmt (*gsi);
2218 tree dest = gimple_call_arg (stmt, 0);
2219 tree fmt = gimple_call_arg (stmt, 1);
2220 tree orig = NULL_TREE;
2221 const char *fmt_str = NULL;
2223 /* Verify the required arguments in the original call. We deal with two
2224 types of sprintf() calls: 'sprintf (str, fmt)' and
2225 'sprintf (dest, "%s", orig)'. */
2226 if (gimple_call_num_args (stmt) > 3)
2227 return false;
2229 if (gimple_call_num_args (stmt) == 3)
2230 orig = gimple_call_arg (stmt, 2);
2232 /* Check whether the format is a literal string constant. */
2233 fmt_str = c_getstr (fmt);
2234 if (fmt_str == NULL)
2235 return false;
2237 if (!init_target_chars ())
2238 return false;
2240 /* If the format doesn't contain % args or %%, use strcpy. */
2241 if (strchr (fmt_str, target_percent) == NULL)
2243 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
2245 if (!fn)
2246 return false;
2248 /* Don't optimize sprintf (buf, "abc", ptr++). */
2249 if (orig)
2250 return false;
2252 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
2253 'format' is known to contain no % formats. */
2254 gimple_seq stmts = NULL;
2255 gimple repl = gimple_build_call (fn, 2, dest, fmt);
2256 gimple_seq_add_stmt_without_update (&stmts, repl);
2257 if (gimple_call_lhs (stmt))
2259 repl = gimple_build_assign (gimple_call_lhs (stmt),
2260 build_int_cst (integer_type_node,
2261 strlen (fmt_str)));
2262 gimple_seq_add_stmt_without_update (&stmts, repl);
2263 gsi_replace_with_seq_vops (gsi, stmts);
2264 /* gsi now points at the assignment to the lhs, get a
2265 stmt iterator to the memcpy call.
2266 ??? We can't use gsi_for_stmt as that doesn't work when the
2267 CFG isn't built yet. */
2268 gimple_stmt_iterator gsi2 = *gsi;
2269 gsi_prev (&gsi2);
2270 fold_stmt (&gsi2);
2272 else
2274 gsi_replace_with_seq_vops (gsi, stmts);
2275 fold_stmt (gsi);
2277 return true;
2280 /* If the format is "%s", use strcpy if the result isn't used. */
2281 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
2283 tree fn;
2284 fn = builtin_decl_implicit (BUILT_IN_STRCPY);
2286 if (!fn)
2287 return false;
2289 /* Don't crash on sprintf (str1, "%s"). */
2290 if (!orig)
2291 return false;
2293 tree orig_len = NULL_TREE;
2294 if (gimple_call_lhs (stmt))
2296 orig_len = get_maxval_strlen (orig, 0);
2297 if (!orig_len)
2298 return false;
2301 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
2302 gimple_seq stmts = NULL;
2303 gimple repl = gimple_build_call (fn, 2, dest, orig);
2304 gimple_seq_add_stmt_without_update (&stmts, repl);
2305 if (gimple_call_lhs (stmt))
2307 if (!useless_type_conversion_p (integer_type_node,
2308 TREE_TYPE (orig_len)))
2309 orig_len = fold_convert (integer_type_node, orig_len);
2310 repl = gimple_build_assign (gimple_call_lhs (stmt), orig_len);
2311 gimple_seq_add_stmt_without_update (&stmts, repl);
2312 gsi_replace_with_seq_vops (gsi, stmts);
2313 /* gsi now points at the assignment to the lhs, get a
2314 stmt iterator to the memcpy call.
2315 ??? We can't use gsi_for_stmt as that doesn't work when the
2316 CFG isn't built yet. */
2317 gimple_stmt_iterator gsi2 = *gsi;
2318 gsi_prev (&gsi2);
2319 fold_stmt (&gsi2);
2321 else
2323 gsi_replace_with_seq_vops (gsi, stmts);
2324 fold_stmt (gsi);
2326 return true;
2328 return false;
2331 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
2332 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
2333 attempt to simplify calls with more than 4 arguments.
2335 Return NULL_TREE if no simplification was possible, otherwise return the
2336 simplified form of the call as a tree. If IGNORED is true, it means that
2337 the caller does not use the returned value of the function. */
2339 static bool
2340 gimple_fold_builtin_snprintf (gimple_stmt_iterator *gsi)
2342 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
2343 tree dest = gimple_call_arg (stmt, 0);
2344 tree destsize = gimple_call_arg (stmt, 1);
2345 tree fmt = gimple_call_arg (stmt, 2);
2346 tree orig = NULL_TREE;
2347 const char *fmt_str = NULL;
2349 if (gimple_call_num_args (stmt) > 4)
2350 return false;
2352 if (gimple_call_num_args (stmt) == 4)
2353 orig = gimple_call_arg (stmt, 3);
2355 if (!tree_fits_uhwi_p (destsize))
2356 return false;
2357 unsigned HOST_WIDE_INT destlen = tree_to_uhwi (destsize);
2359 /* Check whether the format is a literal string constant. */
2360 fmt_str = c_getstr (fmt);
2361 if (fmt_str == NULL)
2362 return false;
2364 if (!init_target_chars ())
2365 return false;
2367 /* If the format doesn't contain % args or %%, use strcpy. */
2368 if (strchr (fmt_str, target_percent) == NULL)
2370 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
2371 if (!fn)
2372 return false;
2374 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
2375 if (orig)
2376 return false;
2378 /* We could expand this as
2379 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
2380 or to
2381 memcpy (str, fmt_with_nul_at_cstm1, cst);
2382 but in the former case that might increase code size
2383 and in the latter case grow .rodata section too much.
2384 So punt for now. */
2385 size_t len = strlen (fmt_str);
2386 if (len >= destlen)
2387 return false;
2389 gimple_seq stmts = NULL;
2390 gimple repl = gimple_build_call (fn, 2, dest, fmt);
2391 gimple_seq_add_stmt_without_update (&stmts, repl);
2392 if (gimple_call_lhs (stmt))
2394 repl = gimple_build_assign (gimple_call_lhs (stmt),
2395 build_int_cst (integer_type_node, len));
2396 gimple_seq_add_stmt_without_update (&stmts, repl);
2397 gsi_replace_with_seq_vops (gsi, stmts);
2398 /* gsi now points at the assignment to the lhs, get a
2399 stmt iterator to the memcpy call.
2400 ??? We can't use gsi_for_stmt as that doesn't work when the
2401 CFG isn't built yet. */
2402 gimple_stmt_iterator gsi2 = *gsi;
2403 gsi_prev (&gsi2);
2404 fold_stmt (&gsi2);
2406 else
2408 gsi_replace_with_seq_vops (gsi, stmts);
2409 fold_stmt (gsi);
2411 return true;
2414 /* If the format is "%s", use strcpy if the result isn't used. */
2415 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
2417 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
2418 if (!fn)
2419 return false;
2421 /* Don't crash on snprintf (str1, cst, "%s"). */
2422 if (!orig)
2423 return false;
2425 tree orig_len = get_maxval_strlen (orig, 0);
2426 if (!orig_len || TREE_CODE (orig_len) != INTEGER_CST)
2427 return false;
2429 /* We could expand this as
2430 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
2431 or to
2432 memcpy (str1, str2_with_nul_at_cstm1, cst);
2433 but in the former case that might increase code size
2434 and in the latter case grow .rodata section too much.
2435 So punt for now. */
2436 if (compare_tree_int (orig_len, destlen) >= 0)
2437 return false;
2439 /* Convert snprintf (str1, cst, "%s", str2) into
2440 strcpy (str1, str2) if strlen (str2) < cst. */
2441 gimple_seq stmts = NULL;
2442 gimple repl = gimple_build_call (fn, 2, dest, orig);
2443 gimple_seq_add_stmt_without_update (&stmts, repl);
2444 if (gimple_call_lhs (stmt))
2446 if (!useless_type_conversion_p (integer_type_node,
2447 TREE_TYPE (orig_len)))
2448 orig_len = fold_convert (integer_type_node, orig_len);
2449 repl = gimple_build_assign (gimple_call_lhs (stmt), orig_len);
2450 gimple_seq_add_stmt_without_update (&stmts, repl);
2451 gsi_replace_with_seq_vops (gsi, stmts);
2452 /* gsi now points at the assignment to the lhs, get a
2453 stmt iterator to the memcpy call.
2454 ??? We can't use gsi_for_stmt as that doesn't work when the
2455 CFG isn't built yet. */
2456 gimple_stmt_iterator gsi2 = *gsi;
2457 gsi_prev (&gsi2);
2458 fold_stmt (&gsi2);
2460 else
2462 gsi_replace_with_seq_vops (gsi, stmts);
2463 fold_stmt (gsi);
2465 return true;
2467 return false;
2470 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
2471 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
2472 more than 3 arguments, and ARG may be null in the 2-argument case.
2474 Return NULL_TREE if no simplification was possible, otherwise return the
2475 simplified form of the call as a tree. FCODE is the BUILT_IN_*
2476 code of the function to be simplified. */
2478 static bool
2479 gimple_fold_builtin_fprintf (gimple_stmt_iterator *gsi,
2480 tree fp, tree fmt, tree arg,
2481 enum built_in_function fcode)
2483 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
2484 tree fn_fputc, fn_fputs;
2485 const char *fmt_str = NULL;
2487 /* If the return value is used, don't do the transformation. */
2488 if (gimple_call_lhs (stmt) != NULL_TREE)
2489 return false;
2491 /* Check whether the format is a literal string constant. */
2492 fmt_str = c_getstr (fmt);
2493 if (fmt_str == NULL)
2494 return false;
2496 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
2498 /* If we're using an unlocked function, assume the other
2499 unlocked functions exist explicitly. */
2500 fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
2501 fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
2503 else
2505 fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
2506 fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
2509 if (!init_target_chars ())
2510 return false;
2512 /* If the format doesn't contain % args or %%, use strcpy. */
2513 if (strchr (fmt_str, target_percent) == NULL)
2515 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
2516 && arg)
2517 return false;
2519 /* If the format specifier was "", fprintf does nothing. */
2520 if (fmt_str[0] == '\0')
2522 replace_call_with_value (gsi, NULL_TREE);
2523 return true;
2526 /* When "string" doesn't contain %, replace all cases of
2527 fprintf (fp, string) with fputs (string, fp). The fputs
2528 builtin will take care of special cases like length == 1. */
2529 if (fn_fputs)
2531 gcall *repl = gimple_build_call (fn_fputs, 2, fmt, fp);
2532 replace_call_with_call_and_fold (gsi, repl);
2533 return true;
2537 /* The other optimizations can be done only on the non-va_list variants. */
2538 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
2539 return false;
2541 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
2542 else if (strcmp (fmt_str, target_percent_s) == 0)
2544 if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
2545 return false;
2546 if (fn_fputs)
2548 gcall *repl = gimple_build_call (fn_fputs, 2, arg, fp);
2549 replace_call_with_call_and_fold (gsi, repl);
2550 return true;
2554 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
2555 else if (strcmp (fmt_str, target_percent_c) == 0)
2557 if (!arg
2558 || ! useless_type_conversion_p (integer_type_node, TREE_TYPE (arg)))
2559 return false;
2560 if (fn_fputc)
2562 gcall *repl = gimple_build_call (fn_fputc, 2, arg, fp);
2563 replace_call_with_call_and_fold (gsi, repl);
2564 return true;
2568 return false;
2571 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
2572 FMT and ARG are the arguments to the call; we don't fold cases with
2573 more than 2 arguments, and ARG may be null if this is a 1-argument case.
2575 Return NULL_TREE if no simplification was possible, otherwise return the
2576 simplified form of the call as a tree. FCODE is the BUILT_IN_*
2577 code of the function to be simplified. */
2579 static bool
2580 gimple_fold_builtin_printf (gimple_stmt_iterator *gsi, tree fmt,
2581 tree arg, enum built_in_function fcode)
2583 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
2584 tree fn_putchar, fn_puts, newarg;
2585 const char *fmt_str = NULL;
2587 /* If the return value is used, don't do the transformation. */
2588 if (gimple_call_lhs (stmt) != NULL_TREE)
2589 return false;
2591 /* Check whether the format is a literal string constant. */
2592 fmt_str = c_getstr (fmt);
2593 if (fmt_str == NULL)
2594 return false;
2596 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
2598 /* If we're using an unlocked function, assume the other
2599 unlocked functions exist explicitly. */
2600 fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
2601 fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
2603 else
2605 fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
2606 fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
2609 if (!init_target_chars ())
2610 return false;
2612 if (strcmp (fmt_str, target_percent_s) == 0
2613 || strchr (fmt_str, target_percent) == NULL)
2615 const char *str;
2617 if (strcmp (fmt_str, target_percent_s) == 0)
2619 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
2620 return false;
2622 if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
2623 return false;
2625 str = c_getstr (arg);
2626 if (str == NULL)
2627 return false;
2629 else
2631 /* The format specifier doesn't contain any '%' characters. */
2632 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
2633 && arg)
2634 return false;
2635 str = fmt_str;
2638 /* If the string was "", printf does nothing. */
2639 if (str[0] == '\0')
2641 replace_call_with_value (gsi, NULL_TREE);
2642 return true;
2645 /* If the string has length of 1, call putchar. */
2646 if (str[1] == '\0')
2648 /* Given printf("c"), (where c is any one character,)
2649 convert "c"[0] to an int and pass that to the replacement
2650 function. */
2651 newarg = build_int_cst (integer_type_node, str[0]);
2652 if (fn_putchar)
2654 gcall *repl = gimple_build_call (fn_putchar, 1, newarg);
2655 replace_call_with_call_and_fold (gsi, repl);
2656 return true;
2659 else
2661 /* If the string was "string\n", call puts("string"). */
2662 size_t len = strlen (str);
2663 if ((unsigned char)str[len - 1] == target_newline
2664 && (size_t) (int) len == len
2665 && (int) len > 0)
2667 char *newstr;
2668 tree offset_node, string_cst;
2670 /* Create a NUL-terminated string that's one char shorter
2671 than the original, stripping off the trailing '\n'. */
2672 newarg = build_string_literal (len, str);
2673 string_cst = string_constant (newarg, &offset_node);
2674 gcc_checking_assert (string_cst
2675 && (TREE_STRING_LENGTH (string_cst)
2676 == (int) len)
2677 && integer_zerop (offset_node)
2678 && (unsigned char)
2679 TREE_STRING_POINTER (string_cst)[len - 1]
2680 == target_newline);
2681 /* build_string_literal creates a new STRING_CST,
2682 modify it in place to avoid double copying. */
2683 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
2684 newstr[len - 1] = '\0';
2685 if (fn_puts)
2687 gcall *repl = gimple_build_call (fn_puts, 1, newarg);
2688 replace_call_with_call_and_fold (gsi, repl);
2689 return true;
2692 else
2693 /* We'd like to arrange to call fputs(string,stdout) here,
2694 but we need stdout and don't have a way to get it yet. */
2695 return false;
2699 /* The other optimizations can be done only on the non-va_list variants. */
2700 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
2701 return false;
2703 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
2704 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
2706 if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
2707 return false;
2708 if (fn_puts)
2710 gcall *repl = gimple_build_call (fn_puts, 1, arg);
2711 replace_call_with_call_and_fold (gsi, repl);
2712 return true;
2716 /* If the format specifier was "%c", call __builtin_putchar(arg). */
2717 else if (strcmp (fmt_str, target_percent_c) == 0)
2719 if (!arg || ! useless_type_conversion_p (integer_type_node,
2720 TREE_TYPE (arg)))
2721 return false;
2722 if (fn_putchar)
2724 gcall *repl = gimple_build_call (fn_putchar, 1, arg);
2725 replace_call_with_call_and_fold (gsi, repl);
2726 return true;
2730 return false;
2735 /* Fold a call to __builtin_strlen with known length LEN. */
2737 static bool
2738 gimple_fold_builtin_strlen (gimple_stmt_iterator *gsi)
2740 gimple stmt = gsi_stmt (*gsi);
2741 tree len = get_maxval_strlen (gimple_call_arg (stmt, 0), 0);
2742 if (!len)
2743 return false;
2744 len = force_gimple_operand_gsi (gsi, len, true, NULL, true, GSI_SAME_STMT);
2745 replace_call_with_value (gsi, len);
2746 return true;
2750 /* Fold the non-target builtin at *GSI and return whether any simplification
2751 was made. */
2753 static bool
2754 gimple_fold_builtin (gimple_stmt_iterator *gsi)
2756 gcall *stmt = as_a <gcall *>(gsi_stmt (*gsi));
2757 tree callee = gimple_call_fndecl (stmt);
2759 /* Give up for always_inline inline builtins until they are
2760 inlined. */
2761 if (avoid_folding_inline_builtin (callee))
2762 return false;
2764 unsigned n = gimple_call_num_args (stmt);
2765 enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
2766 switch (fcode)
2768 case BUILT_IN_BZERO:
2769 return gimple_fold_builtin_memset (gsi, integer_zero_node,
2770 gimple_call_arg (stmt, 1));
2771 case BUILT_IN_MEMSET:
2772 return gimple_fold_builtin_memset (gsi,
2773 gimple_call_arg (stmt, 1),
2774 gimple_call_arg (stmt, 2));
2775 case BUILT_IN_BCOPY:
2776 return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 1),
2777 gimple_call_arg (stmt, 0), 3);
2778 case BUILT_IN_MEMCPY:
2779 return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
2780 gimple_call_arg (stmt, 1), 0);
2781 case BUILT_IN_MEMPCPY:
2782 return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
2783 gimple_call_arg (stmt, 1), 1);
2784 case BUILT_IN_MEMMOVE:
2785 return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
2786 gimple_call_arg (stmt, 1), 3);
2787 case BUILT_IN_SPRINTF_CHK:
2788 case BUILT_IN_VSPRINTF_CHK:
2789 return gimple_fold_builtin_sprintf_chk (gsi, fcode);
2790 case BUILT_IN_STRCAT_CHK:
2791 return gimple_fold_builtin_strcat_chk (gsi);
2792 case BUILT_IN_STRNCAT_CHK:
2793 return gimple_fold_builtin_strncat_chk (gsi);
2794 case BUILT_IN_STRLEN:
2795 return gimple_fold_builtin_strlen (gsi);
2796 case BUILT_IN_STRCPY:
2797 return gimple_fold_builtin_strcpy (gsi,
2798 gimple_call_arg (stmt, 0),
2799 gimple_call_arg (stmt, 1));
2800 case BUILT_IN_STRNCPY:
2801 return gimple_fold_builtin_strncpy (gsi,
2802 gimple_call_arg (stmt, 0),
2803 gimple_call_arg (stmt, 1),
2804 gimple_call_arg (stmt, 2));
2805 case BUILT_IN_STRCAT:
2806 return gimple_fold_builtin_strcat (gsi, gimple_call_arg (stmt, 0),
2807 gimple_call_arg (stmt, 1));
2808 case BUILT_IN_STRNCAT:
2809 return gimple_fold_builtin_strncat (gsi);
2810 case BUILT_IN_FPUTS:
2811 return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
2812 gimple_call_arg (stmt, 1), false);
2813 case BUILT_IN_FPUTS_UNLOCKED:
2814 return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
2815 gimple_call_arg (stmt, 1), true);
2816 case BUILT_IN_MEMCPY_CHK:
2817 case BUILT_IN_MEMPCPY_CHK:
2818 case BUILT_IN_MEMMOVE_CHK:
2819 case BUILT_IN_MEMSET_CHK:
2820 return gimple_fold_builtin_memory_chk (gsi,
2821 gimple_call_arg (stmt, 0),
2822 gimple_call_arg (stmt, 1),
2823 gimple_call_arg (stmt, 2),
2824 gimple_call_arg (stmt, 3),
2825 fcode);
2826 case BUILT_IN_STPCPY:
2827 return gimple_fold_builtin_stpcpy (gsi);
2828 case BUILT_IN_STRCPY_CHK:
2829 case BUILT_IN_STPCPY_CHK:
2830 return gimple_fold_builtin_stxcpy_chk (gsi,
2831 gimple_call_arg (stmt, 0),
2832 gimple_call_arg (stmt, 1),
2833 gimple_call_arg (stmt, 2),
2834 fcode);
2835 case BUILT_IN_STRNCPY_CHK:
2836 case BUILT_IN_STPNCPY_CHK:
2837 return gimple_fold_builtin_stxncpy_chk (gsi,
2838 gimple_call_arg (stmt, 0),
2839 gimple_call_arg (stmt, 1),
2840 gimple_call_arg (stmt, 2),
2841 gimple_call_arg (stmt, 3),
2842 fcode);
2843 case BUILT_IN_SNPRINTF_CHK:
2844 case BUILT_IN_VSNPRINTF_CHK:
2845 return gimple_fold_builtin_snprintf_chk (gsi, fcode);
2846 case BUILT_IN_SNPRINTF:
2847 return gimple_fold_builtin_snprintf (gsi);
2848 case BUILT_IN_SPRINTF:
2849 return gimple_fold_builtin_sprintf (gsi);
2850 case BUILT_IN_FPRINTF:
2851 case BUILT_IN_FPRINTF_UNLOCKED:
2852 case BUILT_IN_VFPRINTF:
2853 if (n == 2 || n == 3)
2854 return gimple_fold_builtin_fprintf (gsi,
2855 gimple_call_arg (stmt, 0),
2856 gimple_call_arg (stmt, 1),
2857 n == 3
2858 ? gimple_call_arg (stmt, 2)
2859 : NULL_TREE,
2860 fcode);
2861 break;
2862 case BUILT_IN_FPRINTF_CHK:
2863 case BUILT_IN_VFPRINTF_CHK:
2864 if (n == 3 || n == 4)
2865 return gimple_fold_builtin_fprintf (gsi,
2866 gimple_call_arg (stmt, 0),
2867 gimple_call_arg (stmt, 2),
2868 n == 4
2869 ? gimple_call_arg (stmt, 3)
2870 : NULL_TREE,
2871 fcode);
2872 break;
2873 case BUILT_IN_PRINTF:
2874 case BUILT_IN_PRINTF_UNLOCKED:
2875 case BUILT_IN_VPRINTF:
2876 if (n == 1 || n == 2)
2877 return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 0),
2878 n == 2
2879 ? gimple_call_arg (stmt, 1)
2880 : NULL_TREE, fcode);
2881 break;
2882 case BUILT_IN_PRINTF_CHK:
2883 case BUILT_IN_VPRINTF_CHK:
2884 if (n == 2 || n == 3)
2885 return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 1),
2886 n == 3
2887 ? gimple_call_arg (stmt, 2)
2888 : NULL_TREE, fcode);
2889 default:;
2892 /* Try the generic builtin folder. */
2893 bool ignore = (gimple_call_lhs (stmt) == NULL);
2894 tree result = fold_call_stmt (stmt, ignore);
2895 if (result)
2897 if (ignore)
2898 STRIP_NOPS (result);
2899 else
2900 result = fold_convert (gimple_call_return_type (stmt), result);
2901 if (!update_call_from_tree (gsi, result))
2902 gimplify_and_update_call_from_tree (gsi, result);
2903 return true;
2906 return false;
2909 /* Return true if ARG0 CODE ARG1 in infinite signed precision operation
2910 doesn't fit into TYPE. The test for overflow should be regardless of
2911 -fwrapv, and even for unsigned types. */
2913 bool
2914 arith_overflowed_p (enum tree_code code, const_tree type,
2915 const_tree arg0, const_tree arg1)
2917 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) widest2_int;
2918 typedef generic_wide_int <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> >
2919 widest2_int_cst;
2920 widest2_int warg0 = widest2_int_cst (arg0);
2921 widest2_int warg1 = widest2_int_cst (arg1);
2922 widest2_int wres;
2923 switch (code)
2925 case PLUS_EXPR: wres = wi::add (warg0, warg1); break;
2926 case MINUS_EXPR: wres = wi::sub (warg0, warg1); break;
2927 case MULT_EXPR: wres = wi::mul (warg0, warg1); break;
2928 default: gcc_unreachable ();
2930 signop sign = TYPE_SIGN (type);
2931 if (sign == UNSIGNED && wi::neg_p (wres))
2932 return true;
2933 return wi::min_precision (wres, sign) > TYPE_PRECISION (type);
2936 /* Attempt to fold a call statement referenced by the statement iterator GSI.
2937 The statement may be replaced by another statement, e.g., if the call
2938 simplifies to a constant value. Return true if any changes were made.
2939 It is assumed that the operands have been previously folded. */
2941 static bool
2942 gimple_fold_call (gimple_stmt_iterator *gsi, bool inplace)
2944 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
2945 tree callee;
2946 bool changed = false;
2947 unsigned i;
2949 /* Fold *& in call arguments. */
2950 for (i = 0; i < gimple_call_num_args (stmt); ++i)
2951 if (REFERENCE_CLASS_P (gimple_call_arg (stmt, i)))
2953 tree tmp = maybe_fold_reference (gimple_call_arg (stmt, i), false);
2954 if (tmp)
2956 gimple_call_set_arg (stmt, i, tmp);
2957 changed = true;
2961 /* Check for virtual calls that became direct calls. */
2962 callee = gimple_call_fn (stmt);
2963 if (callee && TREE_CODE (callee) == OBJ_TYPE_REF)
2965 if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee)) != NULL_TREE)
2967 if (dump_file && virtual_method_call_p (callee)
2968 && !possible_polymorphic_call_target_p
2969 (callee, stmt, cgraph_node::get (gimple_call_addr_fndecl
2970 (OBJ_TYPE_REF_EXPR (callee)))))
2972 fprintf (dump_file,
2973 "Type inheritance inconsistent devirtualization of ");
2974 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2975 fprintf (dump_file, " to ");
2976 print_generic_expr (dump_file, callee, TDF_SLIM);
2977 fprintf (dump_file, "\n");
2980 gimple_call_set_fn (stmt, OBJ_TYPE_REF_EXPR (callee));
2981 changed = true;
2983 else if (flag_devirtualize && !inplace && virtual_method_call_p (callee))
2985 bool final;
2986 vec <cgraph_node *>targets
2987 = possible_polymorphic_call_targets (callee, stmt, &final);
2988 if (final && targets.length () <= 1 && dbg_cnt (devirt))
2990 tree lhs = gimple_call_lhs (stmt);
2991 if (dump_enabled_p ())
2993 location_t loc = gimple_location_safe (stmt);
2994 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
2995 "folding virtual function call to %s\n",
2996 targets.length () == 1
2997 ? targets[0]->name ()
2998 : "__builtin_unreachable");
3000 if (targets.length () == 1)
3002 gimple_call_set_fndecl (stmt, targets[0]->decl);
3003 changed = true;
3004 /* If the call becomes noreturn, remove the lhs. */
3005 if (lhs && (gimple_call_flags (stmt) & ECF_NORETURN))
3007 if (TREE_CODE (lhs) == SSA_NAME)
3009 tree var = create_tmp_var (TREE_TYPE (lhs));
3010 tree def = get_or_create_ssa_default_def (cfun, var);
3011 gimple new_stmt = gimple_build_assign (lhs, def);
3012 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
3014 gimple_call_set_lhs (stmt, NULL_TREE);
3016 maybe_remove_unused_call_args (cfun, stmt);
3018 else
3020 tree fndecl = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
3021 gimple new_stmt = gimple_build_call (fndecl, 0);
3022 gimple_set_location (new_stmt, gimple_location (stmt));
3023 if (lhs && TREE_CODE (lhs) == SSA_NAME)
3025 tree var = create_tmp_var (TREE_TYPE (lhs));
3026 tree def = get_or_create_ssa_default_def (cfun, var);
3028 /* To satisfy condition for
3029 cgraph_update_edges_for_call_stmt_node,
3030 we need to preserve GIMPLE_CALL statement
3031 at position of GSI iterator. */
3032 update_call_from_tree (gsi, def);
3033 gsi_insert_before (gsi, new_stmt, GSI_NEW_STMT);
3035 else
3037 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
3038 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
3039 gsi_replace (gsi, new_stmt, false);
3041 return true;
3047 /* Check for indirect calls that became direct calls, and then
3048 no longer require a static chain. */
3049 if (gimple_call_chain (stmt))
3051 tree fn = gimple_call_fndecl (stmt);
3052 if (fn && !DECL_STATIC_CHAIN (fn))
3054 gimple_call_set_chain (stmt, NULL);
3055 changed = true;
3057 else
3059 tree tmp = maybe_fold_reference (gimple_call_chain (stmt), false);
3060 if (tmp)
3062 gimple_call_set_chain (stmt, tmp);
3063 changed = true;
3068 if (inplace)
3069 return changed;
3071 /* Check for builtins that CCP can handle using information not
3072 available in the generic fold routines. */
3073 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3075 if (gimple_fold_builtin (gsi))
3076 changed = true;
3078 else if (gimple_call_builtin_p (stmt, BUILT_IN_MD))
3080 changed |= targetm.gimple_fold_builtin (gsi);
3082 else if (gimple_call_internal_p (stmt))
3084 enum tree_code subcode = ERROR_MARK;
3085 tree result = NULL_TREE;
3086 bool cplx_result = false;
3087 tree overflow = NULL_TREE;
3088 switch (gimple_call_internal_fn (stmt))
3090 case IFN_BUILTIN_EXPECT:
3091 result = fold_builtin_expect (gimple_location (stmt),
3092 gimple_call_arg (stmt, 0),
3093 gimple_call_arg (stmt, 1),
3094 gimple_call_arg (stmt, 2));
3095 break;
3096 case IFN_UBSAN_OBJECT_SIZE:
3097 if (integer_all_onesp (gimple_call_arg (stmt, 2))
3098 || (TREE_CODE (gimple_call_arg (stmt, 1)) == INTEGER_CST
3099 && TREE_CODE (gimple_call_arg (stmt, 2)) == INTEGER_CST
3100 && tree_int_cst_le (gimple_call_arg (stmt, 1),
3101 gimple_call_arg (stmt, 2))))
3103 gsi_replace (gsi, gimple_build_nop (), true);
3104 unlink_stmt_vdef (stmt);
3105 release_defs (stmt);
3106 return true;
3108 break;
3109 case IFN_UBSAN_CHECK_ADD:
3110 subcode = PLUS_EXPR;
3111 break;
3112 case IFN_UBSAN_CHECK_SUB:
3113 subcode = MINUS_EXPR;
3114 break;
3115 case IFN_UBSAN_CHECK_MUL:
3116 subcode = MULT_EXPR;
3117 break;
3118 case IFN_ADD_OVERFLOW:
3119 subcode = PLUS_EXPR;
3120 cplx_result = true;
3121 break;
3122 case IFN_SUB_OVERFLOW:
3123 subcode = MINUS_EXPR;
3124 cplx_result = true;
3125 break;
3126 case IFN_MUL_OVERFLOW:
3127 subcode = MULT_EXPR;
3128 cplx_result = true;
3129 break;
3130 default:
3131 break;
3133 if (subcode != ERROR_MARK)
3135 tree arg0 = gimple_call_arg (stmt, 0);
3136 tree arg1 = gimple_call_arg (stmt, 1);
3137 tree type = TREE_TYPE (arg0);
3138 if (cplx_result)
3140 tree lhs = gimple_call_lhs (stmt);
3141 if (lhs == NULL_TREE)
3142 type = NULL_TREE;
3143 else
3144 type = TREE_TYPE (TREE_TYPE (lhs));
3146 if (type == NULL_TREE)
3148 /* x = y + 0; x = y - 0; x = y * 0; */
3149 else if (integer_zerop (arg1))
3150 result = subcode == MULT_EXPR ? integer_zero_node : arg0;
3151 /* x = 0 + y; x = 0 * y; */
3152 else if (subcode != MINUS_EXPR && integer_zerop (arg0))
3153 result = subcode == MULT_EXPR ? integer_zero_node : arg1;
3154 /* x = y - y; */
3155 else if (subcode == MINUS_EXPR && operand_equal_p (arg0, arg1, 0))
3156 result = integer_zero_node;
3157 /* x = y * 1; x = 1 * y; */
3158 else if (subcode == MULT_EXPR && integer_onep (arg1))
3159 result = arg0;
3160 else if (subcode == MULT_EXPR && integer_onep (arg0))
3161 result = arg1;
3162 else if (TREE_CODE (arg0) == INTEGER_CST
3163 && TREE_CODE (arg1) == INTEGER_CST)
3165 if (cplx_result)
3166 result = int_const_binop (subcode, fold_convert (type, arg0),
3167 fold_convert (type, arg1));
3168 else
3169 result = int_const_binop (subcode, arg0, arg1);
3170 if (result && arith_overflowed_p (subcode, type, arg0, arg1))
3172 if (cplx_result)
3173 overflow = build_one_cst (type);
3174 else
3175 result = NULL_TREE;
3178 if (result)
3180 if (result == integer_zero_node)
3181 result = build_zero_cst (type);
3182 else if (cplx_result && TREE_TYPE (result) != type)
3184 if (TREE_CODE (result) == INTEGER_CST)
3186 if (arith_overflowed_p (PLUS_EXPR, type, result,
3187 integer_zero_node))
3188 overflow = build_one_cst (type);
3190 else if ((!TYPE_UNSIGNED (TREE_TYPE (result))
3191 && TYPE_UNSIGNED (type))
3192 || (TYPE_PRECISION (type)
3193 < (TYPE_PRECISION (TREE_TYPE (result))
3194 + (TYPE_UNSIGNED (TREE_TYPE (result))
3195 && !TYPE_UNSIGNED (type)))))
3196 result = NULL_TREE;
3197 if (result)
3198 result = fold_convert (type, result);
3203 if (result)
3205 if (TREE_CODE (result) == INTEGER_CST && TREE_OVERFLOW (result))
3206 result = drop_tree_overflow (result);
3207 if (cplx_result)
3209 if (overflow == NULL_TREE)
3210 overflow = build_zero_cst (TREE_TYPE (result));
3211 tree ctype = build_complex_type (TREE_TYPE (result));
3212 if (TREE_CODE (result) == INTEGER_CST
3213 && TREE_CODE (overflow) == INTEGER_CST)
3214 result = build_complex (ctype, result, overflow);
3215 else
3216 result = build2_loc (gimple_location (stmt), COMPLEX_EXPR,
3217 ctype, result, overflow);
3219 if (!update_call_from_tree (gsi, result))
3220 gimplify_and_update_call_from_tree (gsi, result);
3221 changed = true;
3225 return changed;
3229 /* Return true whether NAME has a use on STMT. */
3231 static bool
3232 has_use_on_stmt (tree name, gimple stmt)
3234 imm_use_iterator iter;
3235 use_operand_p use_p;
3236 FOR_EACH_IMM_USE_FAST (use_p, iter, name)
3237 if (USE_STMT (use_p) == stmt)
3238 return true;
3239 return false;
3242 /* Worker for fold_stmt_1 dispatch to pattern based folding with
3243 gimple_simplify.
3245 Replaces *GSI with the simplification result in RCODE and OPS
3246 and the associated statements in *SEQ. Does the replacement
3247 according to INPLACE and returns true if the operation succeeded. */
3249 static bool
3250 replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
3251 code_helper rcode, tree *ops,
3252 gimple_seq *seq, bool inplace)
3254 gimple stmt = gsi_stmt (*gsi);
3256 /* Play safe and do not allow abnormals to be mentioned in
3257 newly created statements. See also maybe_push_res_to_seq.
3258 As an exception allow such uses if there was a use of the
3259 same SSA name on the old stmt. */
3260 if ((TREE_CODE (ops[0]) == SSA_NAME
3261 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[0])
3262 && !has_use_on_stmt (ops[0], stmt))
3263 || (ops[1]
3264 && TREE_CODE (ops[1]) == SSA_NAME
3265 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[1])
3266 && !has_use_on_stmt (ops[1], stmt))
3267 || (ops[2]
3268 && TREE_CODE (ops[2]) == SSA_NAME
3269 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[2])
3270 && !has_use_on_stmt (ops[2], stmt)))
3271 return false;
3273 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
3275 gcc_assert (rcode.is_tree_code ());
3276 if (TREE_CODE_CLASS ((enum tree_code)rcode) == tcc_comparison
3277 /* GIMPLE_CONDs condition may not throw. */
3278 && (!flag_exceptions
3279 || !cfun->can_throw_non_call_exceptions
3280 || !operation_could_trap_p (rcode,
3281 FLOAT_TYPE_P (TREE_TYPE (ops[0])),
3282 false, NULL_TREE)))
3283 gimple_cond_set_condition (cond_stmt, rcode, ops[0], ops[1]);
3284 else if (rcode == SSA_NAME)
3285 gimple_cond_set_condition (cond_stmt, NE_EXPR, ops[0],
3286 build_zero_cst (TREE_TYPE (ops[0])));
3287 else if (rcode == INTEGER_CST)
3289 if (integer_zerop (ops[0]))
3290 gimple_cond_make_false (cond_stmt);
3291 else
3292 gimple_cond_make_true (cond_stmt);
3294 else if (!inplace)
3296 tree res = maybe_push_res_to_seq (rcode, boolean_type_node,
3297 ops, seq);
3298 if (!res)
3299 return false;
3300 gimple_cond_set_condition (cond_stmt, NE_EXPR, res,
3301 build_zero_cst (TREE_TYPE (res)));
3303 else
3304 return false;
3305 if (dump_file && (dump_flags & TDF_DETAILS))
3307 fprintf (dump_file, "gimple_simplified to ");
3308 if (!gimple_seq_empty_p (*seq))
3309 print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
3310 print_gimple_stmt (dump_file, gsi_stmt (*gsi),
3311 0, TDF_SLIM);
3313 gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
3314 return true;
3316 else if (is_gimple_assign (stmt)
3317 && rcode.is_tree_code ())
3319 if (!inplace
3320 || gimple_num_ops (stmt) > get_gimple_rhs_num_ops (rcode))
3322 maybe_build_generic_op (rcode,
3323 TREE_TYPE (gimple_assign_lhs (stmt)),
3324 &ops[0], ops[1], ops[2]);
3325 gimple_assign_set_rhs_with_ops (gsi, rcode, ops[0], ops[1], ops[2]);
3326 if (dump_file && (dump_flags & TDF_DETAILS))
3328 fprintf (dump_file, "gimple_simplified to ");
3329 if (!gimple_seq_empty_p (*seq))
3330 print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
3331 print_gimple_stmt (dump_file, gsi_stmt (*gsi),
3332 0, TDF_SLIM);
3334 gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
3335 return true;
3338 else if (rcode.is_fn_code ()
3339 && gimple_call_builtin_p (stmt, rcode))
3341 unsigned i;
3342 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3344 gcc_assert (ops[i] != NULL_TREE);
3345 gimple_call_set_arg (stmt, i, ops[i]);
3347 if (i < 3)
3348 gcc_assert (ops[i] == NULL_TREE);
3349 return true;
3351 else if (!inplace)
3353 if (gimple_has_lhs (stmt))
3355 tree lhs = gimple_get_lhs (stmt);
3356 if (!maybe_push_res_to_seq (rcode, TREE_TYPE (lhs),
3357 ops, seq, lhs))
3358 return false;
3359 if (dump_file && (dump_flags & TDF_DETAILS))
3361 fprintf (dump_file, "gimple_simplified to ");
3362 print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
3364 gsi_replace_with_seq_vops (gsi, *seq);
3365 return true;
3367 else
3368 gcc_unreachable ();
3371 return false;
3374 /* Canonicalize MEM_REFs invariant address operand after propagation. */
3376 static bool
3377 maybe_canonicalize_mem_ref_addr (tree *t)
3379 bool res = false;
3381 if (TREE_CODE (*t) == ADDR_EXPR)
3382 t = &TREE_OPERAND (*t, 0);
3384 while (handled_component_p (*t))
3385 t = &TREE_OPERAND (*t, 0);
3387 /* Canonicalize MEM [&foo.bar, 0] which appears after propagating
3388 of invariant addresses into a SSA name MEM_REF address. */
3389 if (TREE_CODE (*t) == MEM_REF
3390 || TREE_CODE (*t) == TARGET_MEM_REF)
3392 tree addr = TREE_OPERAND (*t, 0);
3393 if (TREE_CODE (addr) == ADDR_EXPR
3394 && (TREE_CODE (TREE_OPERAND (addr, 0)) == MEM_REF
3395 || handled_component_p (TREE_OPERAND (addr, 0))))
3397 tree base;
3398 HOST_WIDE_INT coffset;
3399 base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
3400 &coffset);
3401 if (!base)
3402 gcc_unreachable ();
3404 TREE_OPERAND (*t, 0) = build_fold_addr_expr (base);
3405 TREE_OPERAND (*t, 1) = int_const_binop (PLUS_EXPR,
3406 TREE_OPERAND (*t, 1),
3407 size_int (coffset));
3408 res = true;
3410 gcc_checking_assert (TREE_CODE (TREE_OPERAND (*t, 0)) == DEBUG_EXPR_DECL
3411 || is_gimple_mem_ref_addr (TREE_OPERAND (*t, 0)));
3414 /* Canonicalize back MEM_REFs to plain reference trees if the object
3415 accessed is a decl that has the same access semantics as the MEM_REF. */
3416 if (TREE_CODE (*t) == MEM_REF
3417 && TREE_CODE (TREE_OPERAND (*t, 0)) == ADDR_EXPR
3418 && integer_zerop (TREE_OPERAND (*t, 1))
3419 && MR_DEPENDENCE_CLIQUE (*t) == 0)
3421 tree decl = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
3422 tree alias_type = TREE_TYPE (TREE_OPERAND (*t, 1));
3423 if (/* Same volatile qualification. */
3424 TREE_THIS_VOLATILE (*t) == TREE_THIS_VOLATILE (decl)
3425 /* Same TBAA behavior with -fstrict-aliasing. */
3426 && !TYPE_REF_CAN_ALIAS_ALL (alias_type)
3427 && (TYPE_MAIN_VARIANT (TREE_TYPE (decl))
3428 == TYPE_MAIN_VARIANT (TREE_TYPE (alias_type)))
3429 /* Same alignment. */
3430 && TYPE_ALIGN (TREE_TYPE (decl)) == TYPE_ALIGN (TREE_TYPE (*t))
3431 /* We have to look out here to not drop a required conversion
3432 from the rhs to the lhs if *t appears on the lhs or vice-versa
3433 if it appears on the rhs. Thus require strict type
3434 compatibility. */
3435 && types_compatible_p (TREE_TYPE (*t), TREE_TYPE (decl)))
3437 *t = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
3438 res = true;
3442 /* Canonicalize TARGET_MEM_REF in particular with respect to
3443 the indexes becoming constant. */
3444 else if (TREE_CODE (*t) == TARGET_MEM_REF)
3446 tree tem = maybe_fold_tmr (*t);
3447 if (tem)
3449 *t = tem;
3450 res = true;
3454 return res;
3457 /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument
3458 distinguishes both cases. */
3460 static bool
3461 fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace, tree (*valueize) (tree))
3463 bool changed = false;
3464 gimple stmt = gsi_stmt (*gsi);
3465 unsigned i;
3467 /* First do required canonicalization of [TARGET_]MEM_REF addresses
3468 after propagation.
3469 ??? This shouldn't be done in generic folding but in the
3470 propagation helpers which also know whether an address was
3471 propagated.
3472 Also canonicalize operand order. */
3473 switch (gimple_code (stmt))
3475 case GIMPLE_ASSIGN:
3476 if (gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
3478 tree *rhs = gimple_assign_rhs1_ptr (stmt);
3479 if ((REFERENCE_CLASS_P (*rhs)
3480 || TREE_CODE (*rhs) == ADDR_EXPR)
3481 && maybe_canonicalize_mem_ref_addr (rhs))
3482 changed = true;
3483 tree *lhs = gimple_assign_lhs_ptr (stmt);
3484 if (REFERENCE_CLASS_P (*lhs)
3485 && maybe_canonicalize_mem_ref_addr (lhs))
3486 changed = true;
3488 else
3490 /* Canonicalize operand order. */
3491 enum tree_code code = gimple_assign_rhs_code (stmt);
3492 if (TREE_CODE_CLASS (code) == tcc_comparison
3493 || commutative_tree_code (code)
3494 || commutative_ternary_tree_code (code))
3496 tree rhs1 = gimple_assign_rhs1 (stmt);
3497 tree rhs2 = gimple_assign_rhs2 (stmt);
3498 if (tree_swap_operands_p (rhs1, rhs2, false))
3500 gimple_assign_set_rhs1 (stmt, rhs2);
3501 gimple_assign_set_rhs2 (stmt, rhs1);
3502 if (TREE_CODE_CLASS (code) == tcc_comparison)
3503 gimple_assign_set_rhs_code (stmt,
3504 swap_tree_comparison (code));
3505 changed = true;
3509 break;
3510 case GIMPLE_CALL:
3512 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3514 tree *arg = gimple_call_arg_ptr (stmt, i);
3515 if (REFERENCE_CLASS_P (*arg)
3516 && maybe_canonicalize_mem_ref_addr (arg))
3517 changed = true;
3519 tree *lhs = gimple_call_lhs_ptr (stmt);
3520 if (*lhs
3521 && REFERENCE_CLASS_P (*lhs)
3522 && maybe_canonicalize_mem_ref_addr (lhs))
3523 changed = true;
3524 break;
3526 case GIMPLE_ASM:
3528 gasm *asm_stmt = as_a <gasm *> (stmt);
3529 for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
3531 tree link = gimple_asm_output_op (asm_stmt, i);
3532 tree op = TREE_VALUE (link);
3533 if (REFERENCE_CLASS_P (op)
3534 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
3535 changed = true;
3537 for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
3539 tree link = gimple_asm_input_op (asm_stmt, i);
3540 tree op = TREE_VALUE (link);
3541 if ((REFERENCE_CLASS_P (op)
3542 || TREE_CODE (op) == ADDR_EXPR)
3543 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
3544 changed = true;
3547 break;
3548 case GIMPLE_DEBUG:
3549 if (gimple_debug_bind_p (stmt))
3551 tree *val = gimple_debug_bind_get_value_ptr (stmt);
3552 if (*val
3553 && (REFERENCE_CLASS_P (*val)
3554 || TREE_CODE (*val) == ADDR_EXPR)
3555 && maybe_canonicalize_mem_ref_addr (val))
3556 changed = true;
3558 break;
3559 case GIMPLE_COND:
3561 /* Canonicalize operand order. */
3562 tree lhs = gimple_cond_lhs (stmt);
3563 tree rhs = gimple_cond_rhs (stmt);
3564 if (tree_swap_operands_p (lhs, rhs, false))
3566 gcond *gc = as_a <gcond *> (stmt);
3567 gimple_cond_set_lhs (gc, rhs);
3568 gimple_cond_set_rhs (gc, lhs);
3569 gimple_cond_set_code (gc,
3570 swap_tree_comparison (gimple_cond_code (gc)));
3571 changed = true;
3574 default:;
3577 /* Dispatch to pattern-based folding. */
3578 if (!inplace
3579 || is_gimple_assign (stmt)
3580 || gimple_code (stmt) == GIMPLE_COND)
3582 gimple_seq seq = NULL;
3583 code_helper rcode;
3584 tree ops[3] = {};
3585 if (gimple_simplify (stmt, &rcode, ops, inplace ? NULL : &seq,
3586 valueize, valueize))
3588 if (replace_stmt_with_simplification (gsi, rcode, ops, &seq, inplace))
3589 changed = true;
3590 else
3591 gimple_seq_discard (seq);
3595 stmt = gsi_stmt (*gsi);
3597 /* Fold the main computation performed by the statement. */
3598 switch (gimple_code (stmt))
3600 case GIMPLE_ASSIGN:
3602 /* Try to canonicalize for boolean-typed X the comparisons
3603 X == 0, X == 1, X != 0, and X != 1. */
3604 if (gimple_assign_rhs_code (stmt) == EQ_EXPR
3605 || gimple_assign_rhs_code (stmt) == NE_EXPR)
3607 tree lhs = gimple_assign_lhs (stmt);
3608 tree op1 = gimple_assign_rhs1 (stmt);
3609 tree op2 = gimple_assign_rhs2 (stmt);
3610 tree type = TREE_TYPE (op1);
3612 /* Check whether the comparison operands are of the same boolean
3613 type as the result type is.
3614 Check that second operand is an integer-constant with value
3615 one or zero. */
3616 if (TREE_CODE (op2) == INTEGER_CST
3617 && (integer_zerop (op2) || integer_onep (op2))
3618 && useless_type_conversion_p (TREE_TYPE (lhs), type))
3620 enum tree_code cmp_code = gimple_assign_rhs_code (stmt);
3621 bool is_logical_not = false;
3623 /* X == 0 and X != 1 is a logical-not.of X
3624 X == 1 and X != 0 is X */
3625 if ((cmp_code == EQ_EXPR && integer_zerop (op2))
3626 || (cmp_code == NE_EXPR && integer_onep (op2)))
3627 is_logical_not = true;
3629 if (is_logical_not == false)
3630 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op1), op1);
3631 /* Only for one-bit precision typed X the transformation
3632 !X -> ~X is valied. */
3633 else if (TYPE_PRECISION (type) == 1)
3634 gimple_assign_set_rhs_with_ops (gsi, BIT_NOT_EXPR, op1);
3635 /* Otherwise we use !X -> X ^ 1. */
3636 else
3637 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op1,
3638 build_int_cst (type, 1));
3639 changed = true;
3640 break;
3644 unsigned old_num_ops = gimple_num_ops (stmt);
3645 tree lhs = gimple_assign_lhs (stmt);
3646 tree new_rhs = fold_gimple_assign (gsi);
3647 if (new_rhs
3648 && !useless_type_conversion_p (TREE_TYPE (lhs),
3649 TREE_TYPE (new_rhs)))
3650 new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
3651 if (new_rhs
3652 && (!inplace
3653 || get_gimple_rhs_num_ops (TREE_CODE (new_rhs)) < old_num_ops))
3655 gimple_assign_set_rhs_from_tree (gsi, new_rhs);
3656 changed = true;
3658 break;
3661 case GIMPLE_CALL:
3662 changed |= gimple_fold_call (gsi, inplace);
3663 break;
3665 case GIMPLE_ASM:
3666 /* Fold *& in asm operands. */
3668 gasm *asm_stmt = as_a <gasm *> (stmt);
3669 size_t noutputs;
3670 const char **oconstraints;
3671 const char *constraint;
3672 bool allows_mem, allows_reg;
3674 noutputs = gimple_asm_noutputs (asm_stmt);
3675 oconstraints = XALLOCAVEC (const char *, noutputs);
3677 for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
3679 tree link = gimple_asm_output_op (asm_stmt, i);
3680 tree op = TREE_VALUE (link);
3681 oconstraints[i]
3682 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
3683 if (REFERENCE_CLASS_P (op)
3684 && (op = maybe_fold_reference (op, true)) != NULL_TREE)
3686 TREE_VALUE (link) = op;
3687 changed = true;
3690 for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
3692 tree link = gimple_asm_input_op (asm_stmt, i);
3693 tree op = TREE_VALUE (link);
3694 constraint
3695 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
3696 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
3697 oconstraints, &allows_mem, &allows_reg);
3698 if (REFERENCE_CLASS_P (op)
3699 && (op = maybe_fold_reference (op, !allows_reg && allows_mem))
3700 != NULL_TREE)
3702 TREE_VALUE (link) = op;
3703 changed = true;
3707 break;
3709 case GIMPLE_DEBUG:
3710 if (gimple_debug_bind_p (stmt))
3712 tree val = gimple_debug_bind_get_value (stmt);
3713 if (val
3714 && REFERENCE_CLASS_P (val))
3716 tree tem = maybe_fold_reference (val, false);
3717 if (tem)
3719 gimple_debug_bind_set_value (stmt, tem);
3720 changed = true;
3723 else if (val
3724 && TREE_CODE (val) == ADDR_EXPR)
3726 tree ref = TREE_OPERAND (val, 0);
3727 tree tem = maybe_fold_reference (ref, false);
3728 if (tem)
3730 tem = build_fold_addr_expr_with_type (tem, TREE_TYPE (val));
3731 gimple_debug_bind_set_value (stmt, tem);
3732 changed = true;
3736 break;
3738 default:;
3741 stmt = gsi_stmt (*gsi);
3743 /* Fold *& on the lhs. */
3744 if (gimple_has_lhs (stmt))
3746 tree lhs = gimple_get_lhs (stmt);
3747 if (lhs && REFERENCE_CLASS_P (lhs))
3749 tree new_lhs = maybe_fold_reference (lhs, true);
3750 if (new_lhs)
3752 gimple_set_lhs (stmt, new_lhs);
3753 changed = true;
3758 return changed;
3761 /* Valueziation callback that ends up not following SSA edges. */
3763 tree
3764 no_follow_ssa_edges (tree)
3766 return NULL_TREE;
3769 /* Valueization callback that ends up following single-use SSA edges only. */
3771 tree
3772 follow_single_use_edges (tree val)
3774 if (TREE_CODE (val) == SSA_NAME
3775 && !has_single_use (val))
3776 return NULL_TREE;
3777 return val;
3780 /* Fold the statement pointed to by GSI. In some cases, this function may
3781 replace the whole statement with a new one. Returns true iff folding
3782 makes any changes.
3783 The statement pointed to by GSI should be in valid gimple form but may
3784 be in unfolded state as resulting from for example constant propagation
3785 which can produce *&x = 0. */
3787 bool
3788 fold_stmt (gimple_stmt_iterator *gsi)
3790 return fold_stmt_1 (gsi, false, no_follow_ssa_edges);
3793 bool
3794 fold_stmt (gimple_stmt_iterator *gsi, tree (*valueize) (tree))
3796 return fold_stmt_1 (gsi, false, valueize);
3799 /* Perform the minimal folding on statement *GSI. Only operations like
3800 *&x created by constant propagation are handled. The statement cannot
3801 be replaced with a new one. Return true if the statement was
3802 changed, false otherwise.
3803 The statement *GSI should be in valid gimple form but may
3804 be in unfolded state as resulting from for example constant propagation
3805 which can produce *&x = 0. */
3807 bool
3808 fold_stmt_inplace (gimple_stmt_iterator *gsi)
3810 gimple stmt = gsi_stmt (*gsi);
3811 bool changed = fold_stmt_1 (gsi, true, no_follow_ssa_edges);
3812 gcc_assert (gsi_stmt (*gsi) == stmt);
3813 return changed;
3816 /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
3817 if EXPR is null or we don't know how.
3818 If non-null, the result always has boolean type. */
3820 static tree
3821 canonicalize_bool (tree expr, bool invert)
3823 if (!expr)
3824 return NULL_TREE;
3825 else if (invert)
3827 if (integer_nonzerop (expr))
3828 return boolean_false_node;
3829 else if (integer_zerop (expr))
3830 return boolean_true_node;
3831 else if (TREE_CODE (expr) == SSA_NAME)
3832 return fold_build2 (EQ_EXPR, boolean_type_node, expr,
3833 build_int_cst (TREE_TYPE (expr), 0));
3834 else if (COMPARISON_CLASS_P (expr))
3835 return fold_build2 (invert_tree_comparison (TREE_CODE (expr), false),
3836 boolean_type_node,
3837 TREE_OPERAND (expr, 0),
3838 TREE_OPERAND (expr, 1));
3839 else
3840 return NULL_TREE;
3842 else
3844 if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
3845 return expr;
3846 if (integer_nonzerop (expr))
3847 return boolean_true_node;
3848 else if (integer_zerop (expr))
3849 return boolean_false_node;
3850 else if (TREE_CODE (expr) == SSA_NAME)
3851 return fold_build2 (NE_EXPR, boolean_type_node, expr,
3852 build_int_cst (TREE_TYPE (expr), 0));
3853 else if (COMPARISON_CLASS_P (expr))
3854 return fold_build2 (TREE_CODE (expr),
3855 boolean_type_node,
3856 TREE_OPERAND (expr, 0),
3857 TREE_OPERAND (expr, 1));
3858 else
3859 return NULL_TREE;
3863 /* Check to see if a boolean expression EXPR is logically equivalent to the
3864 comparison (OP1 CODE OP2). Check for various identities involving
3865 SSA_NAMEs. */
3867 static bool
3868 same_bool_comparison_p (const_tree expr, enum tree_code code,
3869 const_tree op1, const_tree op2)
3871 gimple s;
3873 /* The obvious case. */
3874 if (TREE_CODE (expr) == code
3875 && operand_equal_p (TREE_OPERAND (expr, 0), op1, 0)
3876 && operand_equal_p (TREE_OPERAND (expr, 1), op2, 0))
3877 return true;
3879 /* Check for comparing (name, name != 0) and the case where expr
3880 is an SSA_NAME with a definition matching the comparison. */
3881 if (TREE_CODE (expr) == SSA_NAME
3882 && TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
3884 if (operand_equal_p (expr, op1, 0))
3885 return ((code == NE_EXPR && integer_zerop (op2))
3886 || (code == EQ_EXPR && integer_nonzerop (op2)));
3887 s = SSA_NAME_DEF_STMT (expr);
3888 if (is_gimple_assign (s)
3889 && gimple_assign_rhs_code (s) == code
3890 && operand_equal_p (gimple_assign_rhs1 (s), op1, 0)
3891 && operand_equal_p (gimple_assign_rhs2 (s), op2, 0))
3892 return true;
3895 /* If op1 is of the form (name != 0) or (name == 0), and the definition
3896 of name is a comparison, recurse. */
3897 if (TREE_CODE (op1) == SSA_NAME
3898 && TREE_CODE (TREE_TYPE (op1)) == BOOLEAN_TYPE)
3900 s = SSA_NAME_DEF_STMT (op1);
3901 if (is_gimple_assign (s)
3902 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
3904 enum tree_code c = gimple_assign_rhs_code (s);
3905 if ((c == NE_EXPR && integer_zerop (op2))
3906 || (c == EQ_EXPR && integer_nonzerop (op2)))
3907 return same_bool_comparison_p (expr, c,
3908 gimple_assign_rhs1 (s),
3909 gimple_assign_rhs2 (s));
3910 if ((c == EQ_EXPR && integer_zerop (op2))
3911 || (c == NE_EXPR && integer_nonzerop (op2)))
3912 return same_bool_comparison_p (expr,
3913 invert_tree_comparison (c, false),
3914 gimple_assign_rhs1 (s),
3915 gimple_assign_rhs2 (s));
3918 return false;
3921 /* Check to see if two boolean expressions OP1 and OP2 are logically
3922 equivalent. */
3924 static bool
3925 same_bool_result_p (const_tree op1, const_tree op2)
3927 /* Simple cases first. */
3928 if (operand_equal_p (op1, op2, 0))
3929 return true;
3931 /* Check the cases where at least one of the operands is a comparison.
3932 These are a bit smarter than operand_equal_p in that they apply some
3933 identifies on SSA_NAMEs. */
3934 if (COMPARISON_CLASS_P (op2)
3935 && same_bool_comparison_p (op1, TREE_CODE (op2),
3936 TREE_OPERAND (op2, 0),
3937 TREE_OPERAND (op2, 1)))
3938 return true;
3939 if (COMPARISON_CLASS_P (op1)
3940 && same_bool_comparison_p (op2, TREE_CODE (op1),
3941 TREE_OPERAND (op1, 0),
3942 TREE_OPERAND (op1, 1)))
3943 return true;
3945 /* Default case. */
3946 return false;
3949 /* Forward declarations for some mutually recursive functions. */
3951 static tree
3952 and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
3953 enum tree_code code2, tree op2a, tree op2b);
3954 static tree
3955 and_var_with_comparison (tree var, bool invert,
3956 enum tree_code code2, tree op2a, tree op2b);
3957 static tree
3958 and_var_with_comparison_1 (gimple stmt,
3959 enum tree_code code2, tree op2a, tree op2b);
3960 static tree
3961 or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
3962 enum tree_code code2, tree op2a, tree op2b);
3963 static tree
3964 or_var_with_comparison (tree var, bool invert,
3965 enum tree_code code2, tree op2a, tree op2b);
3966 static tree
3967 or_var_with_comparison_1 (gimple stmt,
3968 enum tree_code code2, tree op2a, tree op2b);
3970 /* Helper function for and_comparisons_1: try to simplify the AND of the
3971 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
3972 If INVERT is true, invert the value of the VAR before doing the AND.
3973 Return NULL_EXPR if we can't simplify this to a single expression. */
3975 static tree
3976 and_var_with_comparison (tree var, bool invert,
3977 enum tree_code code2, tree op2a, tree op2b)
3979 tree t;
3980 gimple stmt = SSA_NAME_DEF_STMT (var);
3982 /* We can only deal with variables whose definitions are assignments. */
3983 if (!is_gimple_assign (stmt))
3984 return NULL_TREE;
3986 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
3987 !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
3988 Then we only have to consider the simpler non-inverted cases. */
3989 if (invert)
3990 t = or_var_with_comparison_1 (stmt,
3991 invert_tree_comparison (code2, false),
3992 op2a, op2b);
3993 else
3994 t = and_var_with_comparison_1 (stmt, code2, op2a, op2b);
3995 return canonicalize_bool (t, invert);
3998 /* Try to simplify the AND of the ssa variable defined by the assignment
3999 STMT with the comparison specified by (OP2A CODE2 OP2B).
4000 Return NULL_EXPR if we can't simplify this to a single expression. */
4002 static tree
4003 and_var_with_comparison_1 (gimple stmt,
4004 enum tree_code code2, tree op2a, tree op2b)
4006 tree var = gimple_assign_lhs (stmt);
4007 tree true_test_var = NULL_TREE;
4008 tree false_test_var = NULL_TREE;
4009 enum tree_code innercode = gimple_assign_rhs_code (stmt);
4011 /* Check for identities like (var AND (var == 0)) => false. */
4012 if (TREE_CODE (op2a) == SSA_NAME
4013 && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
4015 if ((code2 == NE_EXPR && integer_zerop (op2b))
4016 || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
4018 true_test_var = op2a;
4019 if (var == true_test_var)
4020 return var;
4022 else if ((code2 == EQ_EXPR && integer_zerop (op2b))
4023 || (code2 == NE_EXPR && integer_nonzerop (op2b)))
4025 false_test_var = op2a;
4026 if (var == false_test_var)
4027 return boolean_false_node;
4031 /* If the definition is a comparison, recurse on it. */
4032 if (TREE_CODE_CLASS (innercode) == tcc_comparison)
4034 tree t = and_comparisons_1 (innercode,
4035 gimple_assign_rhs1 (stmt),
4036 gimple_assign_rhs2 (stmt),
4037 code2,
4038 op2a,
4039 op2b);
4040 if (t)
4041 return t;
4044 /* If the definition is an AND or OR expression, we may be able to
4045 simplify by reassociating. */
4046 if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
4047 && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
4049 tree inner1 = gimple_assign_rhs1 (stmt);
4050 tree inner2 = gimple_assign_rhs2 (stmt);
4051 gimple s;
4052 tree t;
4053 tree partial = NULL_TREE;
4054 bool is_and = (innercode == BIT_AND_EXPR);
4056 /* Check for boolean identities that don't require recursive examination
4057 of inner1/inner2:
4058 inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
4059 inner1 AND (inner1 OR inner2) => inner1
4060 !inner1 AND (inner1 AND inner2) => false
4061 !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
4062 Likewise for similar cases involving inner2. */
4063 if (inner1 == true_test_var)
4064 return (is_and ? var : inner1);
4065 else if (inner2 == true_test_var)
4066 return (is_and ? var : inner2);
4067 else if (inner1 == false_test_var)
4068 return (is_and
4069 ? boolean_false_node
4070 : and_var_with_comparison (inner2, false, code2, op2a, op2b));
4071 else if (inner2 == false_test_var)
4072 return (is_and
4073 ? boolean_false_node
4074 : and_var_with_comparison (inner1, false, code2, op2a, op2b));
4076 /* Next, redistribute/reassociate the AND across the inner tests.
4077 Compute the first partial result, (inner1 AND (op2a code op2b)) */
4078 if (TREE_CODE (inner1) == SSA_NAME
4079 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
4080 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
4081 && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s),
4082 gimple_assign_rhs1 (s),
4083 gimple_assign_rhs2 (s),
4084 code2, op2a, op2b)))
4086 /* Handle the AND case, where we are reassociating:
4087 (inner1 AND inner2) AND (op2a code2 op2b)
4088 => (t AND inner2)
4089 If the partial result t is a constant, we win. Otherwise
4090 continue on to try reassociating with the other inner test. */
4091 if (is_and)
4093 if (integer_onep (t))
4094 return inner2;
4095 else if (integer_zerop (t))
4096 return boolean_false_node;
4099 /* Handle the OR case, where we are redistributing:
4100 (inner1 OR inner2) AND (op2a code2 op2b)
4101 => (t OR (inner2 AND (op2a code2 op2b))) */
4102 else if (integer_onep (t))
4103 return boolean_true_node;
4105 /* Save partial result for later. */
4106 partial = t;
4109 /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
4110 if (TREE_CODE (inner2) == SSA_NAME
4111 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
4112 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
4113 && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s),
4114 gimple_assign_rhs1 (s),
4115 gimple_assign_rhs2 (s),
4116 code2, op2a, op2b)))
4118 /* Handle the AND case, where we are reassociating:
4119 (inner1 AND inner2) AND (op2a code2 op2b)
4120 => (inner1 AND t) */
4121 if (is_and)
4123 if (integer_onep (t))
4124 return inner1;
4125 else if (integer_zerop (t))
4126 return boolean_false_node;
4127 /* If both are the same, we can apply the identity
4128 (x AND x) == x. */
4129 else if (partial && same_bool_result_p (t, partial))
4130 return t;
4133 /* Handle the OR case. where we are redistributing:
4134 (inner1 OR inner2) AND (op2a code2 op2b)
4135 => (t OR (inner1 AND (op2a code2 op2b)))
4136 => (t OR partial) */
4137 else
4139 if (integer_onep (t))
4140 return boolean_true_node;
4141 else if (partial)
4143 /* We already got a simplification for the other
4144 operand to the redistributed OR expression. The
4145 interesting case is when at least one is false.
4146 Or, if both are the same, we can apply the identity
4147 (x OR x) == x. */
4148 if (integer_zerop (partial))
4149 return t;
4150 else if (integer_zerop (t))
4151 return partial;
4152 else if (same_bool_result_p (t, partial))
4153 return t;
4158 return NULL_TREE;
4161 /* Try to simplify the AND of two comparisons defined by
4162 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
4163 If this can be done without constructing an intermediate value,
4164 return the resulting tree; otherwise NULL_TREE is returned.
4165 This function is deliberately asymmetric as it recurses on SSA_DEFs
4166 in the first comparison but not the second. */
4168 static tree
4169 and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
4170 enum tree_code code2, tree op2a, tree op2b)
4172 tree truth_type = truth_type_for (TREE_TYPE (op1a));
4174 /* First check for ((x CODE1 y) AND (x CODE2 y)). */
4175 if (operand_equal_p (op1a, op2a, 0)
4176 && operand_equal_p (op1b, op2b, 0))
4178 /* Result will be either NULL_TREE, or a combined comparison. */
4179 tree t = combine_comparisons (UNKNOWN_LOCATION,
4180 TRUTH_ANDIF_EXPR, code1, code2,
4181 truth_type, op1a, op1b);
4182 if (t)
4183 return t;
4186 /* Likewise the swapped case of the above. */
4187 if (operand_equal_p (op1a, op2b, 0)
4188 && operand_equal_p (op1b, op2a, 0))
4190 /* Result will be either NULL_TREE, or a combined comparison. */
4191 tree t = combine_comparisons (UNKNOWN_LOCATION,
4192 TRUTH_ANDIF_EXPR, code1,
4193 swap_tree_comparison (code2),
4194 truth_type, op1a, op1b);
4195 if (t)
4196 return t;
4199 /* If both comparisons are of the same value against constants, we might
4200 be able to merge them. */
4201 if (operand_equal_p (op1a, op2a, 0)
4202 && TREE_CODE (op1b) == INTEGER_CST
4203 && TREE_CODE (op2b) == INTEGER_CST)
4205 int cmp = tree_int_cst_compare (op1b, op2b);
4207 /* If we have (op1a == op1b), we should either be able to
4208 return that or FALSE, depending on whether the constant op1b
4209 also satisfies the other comparison against op2b. */
4210 if (code1 == EQ_EXPR)
4212 bool done = true;
4213 bool val;
4214 switch (code2)
4216 case EQ_EXPR: val = (cmp == 0); break;
4217 case NE_EXPR: val = (cmp != 0); break;
4218 case LT_EXPR: val = (cmp < 0); break;
4219 case GT_EXPR: val = (cmp > 0); break;
4220 case LE_EXPR: val = (cmp <= 0); break;
4221 case GE_EXPR: val = (cmp >= 0); break;
4222 default: done = false;
4224 if (done)
4226 if (val)
4227 return fold_build2 (code1, boolean_type_node, op1a, op1b);
4228 else
4229 return boolean_false_node;
4232 /* Likewise if the second comparison is an == comparison. */
4233 else if (code2 == EQ_EXPR)
4235 bool done = true;
4236 bool val;
4237 switch (code1)
4239 case EQ_EXPR: val = (cmp == 0); break;
4240 case NE_EXPR: val = (cmp != 0); break;
4241 case LT_EXPR: val = (cmp > 0); break;
4242 case GT_EXPR: val = (cmp < 0); break;
4243 case LE_EXPR: val = (cmp >= 0); break;
4244 case GE_EXPR: val = (cmp <= 0); break;
4245 default: done = false;
4247 if (done)
4249 if (val)
4250 return fold_build2 (code2, boolean_type_node, op2a, op2b);
4251 else
4252 return boolean_false_node;
4256 /* Same business with inequality tests. */
4257 else if (code1 == NE_EXPR)
4259 bool val;
4260 switch (code2)
4262 case EQ_EXPR: val = (cmp != 0); break;
4263 case NE_EXPR: val = (cmp == 0); break;
4264 case LT_EXPR: val = (cmp >= 0); break;
4265 case GT_EXPR: val = (cmp <= 0); break;
4266 case LE_EXPR: val = (cmp > 0); break;
4267 case GE_EXPR: val = (cmp < 0); break;
4268 default:
4269 val = false;
4271 if (val)
4272 return fold_build2 (code2, boolean_type_node, op2a, op2b);
4274 else if (code2 == NE_EXPR)
4276 bool val;
4277 switch (code1)
4279 case EQ_EXPR: val = (cmp == 0); break;
4280 case NE_EXPR: val = (cmp != 0); break;
4281 case LT_EXPR: val = (cmp <= 0); break;
4282 case GT_EXPR: val = (cmp >= 0); break;
4283 case LE_EXPR: val = (cmp < 0); break;
4284 case GE_EXPR: val = (cmp > 0); break;
4285 default:
4286 val = false;
4288 if (val)
4289 return fold_build2 (code1, boolean_type_node, op1a, op1b);
4292 /* Chose the more restrictive of two < or <= comparisons. */
4293 else if ((code1 == LT_EXPR || code1 == LE_EXPR)
4294 && (code2 == LT_EXPR || code2 == LE_EXPR))
4296 if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
4297 return fold_build2 (code1, boolean_type_node, op1a, op1b);
4298 else
4299 return fold_build2 (code2, boolean_type_node, op2a, op2b);
4302 /* Likewise chose the more restrictive of two > or >= comparisons. */
4303 else if ((code1 == GT_EXPR || code1 == GE_EXPR)
4304 && (code2 == GT_EXPR || code2 == GE_EXPR))
4306 if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
4307 return fold_build2 (code1, boolean_type_node, op1a, op1b);
4308 else
4309 return fold_build2 (code2, boolean_type_node, op2a, op2b);
4312 /* Check for singleton ranges. */
4313 else if (cmp == 0
4314 && ((code1 == LE_EXPR && code2 == GE_EXPR)
4315 || (code1 == GE_EXPR && code2 == LE_EXPR)))
4316 return fold_build2 (EQ_EXPR, boolean_type_node, op1a, op2b);
4318 /* Check for disjoint ranges. */
4319 else if (cmp <= 0
4320 && (code1 == LT_EXPR || code1 == LE_EXPR)
4321 && (code2 == GT_EXPR || code2 == GE_EXPR))
4322 return boolean_false_node;
4323 else if (cmp >= 0
4324 && (code1 == GT_EXPR || code1 == GE_EXPR)
4325 && (code2 == LT_EXPR || code2 == LE_EXPR))
4326 return boolean_false_node;
4329 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
4330 NAME's definition is a truth value. See if there are any simplifications
4331 that can be done against the NAME's definition. */
4332 if (TREE_CODE (op1a) == SSA_NAME
4333 && (code1 == NE_EXPR || code1 == EQ_EXPR)
4334 && (integer_zerop (op1b) || integer_onep (op1b)))
4336 bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
4337 || (code1 == NE_EXPR && integer_onep (op1b)));
4338 gimple stmt = SSA_NAME_DEF_STMT (op1a);
4339 switch (gimple_code (stmt))
4341 case GIMPLE_ASSIGN:
4342 /* Try to simplify by copy-propagating the definition. */
4343 return and_var_with_comparison (op1a, invert, code2, op2a, op2b);
4345 case GIMPLE_PHI:
4346 /* If every argument to the PHI produces the same result when
4347 ANDed with the second comparison, we win.
4348 Do not do this unless the type is bool since we need a bool
4349 result here anyway. */
4350 if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
4352 tree result = NULL_TREE;
4353 unsigned i;
4354 for (i = 0; i < gimple_phi_num_args (stmt); i++)
4356 tree arg = gimple_phi_arg_def (stmt, i);
4358 /* If this PHI has itself as an argument, ignore it.
4359 If all the other args produce the same result,
4360 we're still OK. */
4361 if (arg == gimple_phi_result (stmt))
4362 continue;
4363 else if (TREE_CODE (arg) == INTEGER_CST)
4365 if (invert ? integer_nonzerop (arg) : integer_zerop (arg))
4367 if (!result)
4368 result = boolean_false_node;
4369 else if (!integer_zerop (result))
4370 return NULL_TREE;
4372 else if (!result)
4373 result = fold_build2 (code2, boolean_type_node,
4374 op2a, op2b);
4375 else if (!same_bool_comparison_p (result,
4376 code2, op2a, op2b))
4377 return NULL_TREE;
4379 else if (TREE_CODE (arg) == SSA_NAME
4380 && !SSA_NAME_IS_DEFAULT_DEF (arg))
4382 tree temp;
4383 gimple def_stmt = SSA_NAME_DEF_STMT (arg);
4384 /* In simple cases we can look through PHI nodes,
4385 but we have to be careful with loops.
4386 See PR49073. */
4387 if (! dom_info_available_p (CDI_DOMINATORS)
4388 || gimple_bb (def_stmt) == gimple_bb (stmt)
4389 || dominated_by_p (CDI_DOMINATORS,
4390 gimple_bb (def_stmt),
4391 gimple_bb (stmt)))
4392 return NULL_TREE;
4393 temp = and_var_with_comparison (arg, invert, code2,
4394 op2a, op2b);
4395 if (!temp)
4396 return NULL_TREE;
4397 else if (!result)
4398 result = temp;
4399 else if (!same_bool_result_p (result, temp))
4400 return NULL_TREE;
4402 else
4403 return NULL_TREE;
4405 return result;
4408 default:
4409 break;
4412 return NULL_TREE;
4415 /* Try to simplify the AND of two comparisons, specified by
4416 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
4417 If this can be simplified to a single expression (without requiring
4418 introducing more SSA variables to hold intermediate values),
4419 return the resulting tree. Otherwise return NULL_TREE.
4420 If the result expression is non-null, it has boolean type. */
4422 tree
4423 maybe_fold_and_comparisons (enum tree_code code1, tree op1a, tree op1b,
4424 enum tree_code code2, tree op2a, tree op2b)
4426 tree t = and_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b);
4427 if (t)
4428 return t;
4429 else
4430 return and_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b);
4433 /* Helper function for or_comparisons_1: try to simplify the OR of the
4434 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
4435 If INVERT is true, invert the value of VAR before doing the OR.
4436 Return NULL_EXPR if we can't simplify this to a single expression. */
4438 static tree
4439 or_var_with_comparison (tree var, bool invert,
4440 enum tree_code code2, tree op2a, tree op2b)
4442 tree t;
4443 gimple stmt = SSA_NAME_DEF_STMT (var);
4445 /* We can only deal with variables whose definitions are assignments. */
4446 if (!is_gimple_assign (stmt))
4447 return NULL_TREE;
4449 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
4450 !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
4451 Then we only have to consider the simpler non-inverted cases. */
4452 if (invert)
4453 t = and_var_with_comparison_1 (stmt,
4454 invert_tree_comparison (code2, false),
4455 op2a, op2b);
4456 else
4457 t = or_var_with_comparison_1 (stmt, code2, op2a, op2b);
4458 return canonicalize_bool (t, invert);
4461 /* Try to simplify the OR of the ssa variable defined by the assignment
4462 STMT with the comparison specified by (OP2A CODE2 OP2B).
4463 Return NULL_EXPR if we can't simplify this to a single expression. */
4465 static tree
4466 or_var_with_comparison_1 (gimple stmt,
4467 enum tree_code code2, tree op2a, tree op2b)
4469 tree var = gimple_assign_lhs (stmt);
4470 tree true_test_var = NULL_TREE;
4471 tree false_test_var = NULL_TREE;
4472 enum tree_code innercode = gimple_assign_rhs_code (stmt);
4474 /* Check for identities like (var OR (var != 0)) => true . */
4475 if (TREE_CODE (op2a) == SSA_NAME
4476 && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
4478 if ((code2 == NE_EXPR && integer_zerop (op2b))
4479 || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
4481 true_test_var = op2a;
4482 if (var == true_test_var)
4483 return var;
4485 else if ((code2 == EQ_EXPR && integer_zerop (op2b))
4486 || (code2 == NE_EXPR && integer_nonzerop (op2b)))
4488 false_test_var = op2a;
4489 if (var == false_test_var)
4490 return boolean_true_node;
4494 /* If the definition is a comparison, recurse on it. */
4495 if (TREE_CODE_CLASS (innercode) == tcc_comparison)
4497 tree t = or_comparisons_1 (innercode,
4498 gimple_assign_rhs1 (stmt),
4499 gimple_assign_rhs2 (stmt),
4500 code2,
4501 op2a,
4502 op2b);
4503 if (t)
4504 return t;
4507 /* If the definition is an AND or OR expression, we may be able to
4508 simplify by reassociating. */
4509 if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
4510 && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
4512 tree inner1 = gimple_assign_rhs1 (stmt);
4513 tree inner2 = gimple_assign_rhs2 (stmt);
4514 gimple s;
4515 tree t;
4516 tree partial = NULL_TREE;
4517 bool is_or = (innercode == BIT_IOR_EXPR);
4519 /* Check for boolean identities that don't require recursive examination
4520 of inner1/inner2:
4521 inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
4522 inner1 OR (inner1 AND inner2) => inner1
4523 !inner1 OR (inner1 OR inner2) => true
4524 !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
4526 if (inner1 == true_test_var)
4527 return (is_or ? var : inner1);
4528 else if (inner2 == true_test_var)
4529 return (is_or ? var : inner2);
4530 else if (inner1 == false_test_var)
4531 return (is_or
4532 ? boolean_true_node
4533 : or_var_with_comparison (inner2, false, code2, op2a, op2b));
4534 else if (inner2 == false_test_var)
4535 return (is_or
4536 ? boolean_true_node
4537 : or_var_with_comparison (inner1, false, code2, op2a, op2b));
4539 /* Next, redistribute/reassociate the OR across the inner tests.
4540 Compute the first partial result, (inner1 OR (op2a code op2b)) */
4541 if (TREE_CODE (inner1) == SSA_NAME
4542 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
4543 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
4544 && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s),
4545 gimple_assign_rhs1 (s),
4546 gimple_assign_rhs2 (s),
4547 code2, op2a, op2b)))
4549 /* Handle the OR case, where we are reassociating:
4550 (inner1 OR inner2) OR (op2a code2 op2b)
4551 => (t OR inner2)
4552 If the partial result t is a constant, we win. Otherwise
4553 continue on to try reassociating with the other inner test. */
4554 if (is_or)
4556 if (integer_onep (t))
4557 return boolean_true_node;
4558 else if (integer_zerop (t))
4559 return inner2;
4562 /* Handle the AND case, where we are redistributing:
4563 (inner1 AND inner2) OR (op2a code2 op2b)
4564 => (t AND (inner2 OR (op2a code op2b))) */
4565 else if (integer_zerop (t))
4566 return boolean_false_node;
4568 /* Save partial result for later. */
4569 partial = t;
4572 /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
4573 if (TREE_CODE (inner2) == SSA_NAME
4574 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
4575 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
4576 && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s),
4577 gimple_assign_rhs1 (s),
4578 gimple_assign_rhs2 (s),
4579 code2, op2a, op2b)))
4581 /* Handle the OR case, where we are reassociating:
4582 (inner1 OR inner2) OR (op2a code2 op2b)
4583 => (inner1 OR t)
4584 => (t OR partial) */
4585 if (is_or)
4587 if (integer_zerop (t))
4588 return inner1;
4589 else if (integer_onep (t))
4590 return boolean_true_node;
4591 /* If both are the same, we can apply the identity
4592 (x OR x) == x. */
4593 else if (partial && same_bool_result_p (t, partial))
4594 return t;
4597 /* Handle the AND case, where we are redistributing:
4598 (inner1 AND inner2) OR (op2a code2 op2b)
4599 => (t AND (inner1 OR (op2a code2 op2b)))
4600 => (t AND partial) */
4601 else
4603 if (integer_zerop (t))
4604 return boolean_false_node;
4605 else if (partial)
4607 /* We already got a simplification for the other
4608 operand to the redistributed AND expression. The
4609 interesting case is when at least one is true.
4610 Or, if both are the same, we can apply the identity
4611 (x AND x) == x. */
4612 if (integer_onep (partial))
4613 return t;
4614 else if (integer_onep (t))
4615 return partial;
4616 else if (same_bool_result_p (t, partial))
4617 return t;
4622 return NULL_TREE;
4625 /* Try to simplify the OR of two comparisons defined by
4626 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
4627 If this can be done without constructing an intermediate value,
4628 return the resulting tree; otherwise NULL_TREE is returned.
4629 This function is deliberately asymmetric as it recurses on SSA_DEFs
4630 in the first comparison but not the second. */
4632 static tree
4633 or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
4634 enum tree_code code2, tree op2a, tree op2b)
4636 tree truth_type = truth_type_for (TREE_TYPE (op1a));
4638 /* First check for ((x CODE1 y) OR (x CODE2 y)). */
4639 if (operand_equal_p (op1a, op2a, 0)
4640 && operand_equal_p (op1b, op2b, 0))
4642 /* Result will be either NULL_TREE, or a combined comparison. */
4643 tree t = combine_comparisons (UNKNOWN_LOCATION,
4644 TRUTH_ORIF_EXPR, code1, code2,
4645 truth_type, op1a, op1b);
4646 if (t)
4647 return t;
4650 /* Likewise the swapped case of the above. */
4651 if (operand_equal_p (op1a, op2b, 0)
4652 && operand_equal_p (op1b, op2a, 0))
4654 /* Result will be either NULL_TREE, or a combined comparison. */
4655 tree t = combine_comparisons (UNKNOWN_LOCATION,
4656 TRUTH_ORIF_EXPR, code1,
4657 swap_tree_comparison (code2),
4658 truth_type, op1a, op1b);
4659 if (t)
4660 return t;
4663 /* If both comparisons are of the same value against constants, we might
4664 be able to merge them. */
4665 if (operand_equal_p (op1a, op2a, 0)
4666 && TREE_CODE (op1b) == INTEGER_CST
4667 && TREE_CODE (op2b) == INTEGER_CST)
4669 int cmp = tree_int_cst_compare (op1b, op2b);
4671 /* If we have (op1a != op1b), we should either be able to
4672 return that or TRUE, depending on whether the constant op1b
4673 also satisfies the other comparison against op2b. */
4674 if (code1 == NE_EXPR)
4676 bool done = true;
4677 bool val;
4678 switch (code2)
4680 case EQ_EXPR: val = (cmp == 0); break;
4681 case NE_EXPR: val = (cmp != 0); break;
4682 case LT_EXPR: val = (cmp < 0); break;
4683 case GT_EXPR: val = (cmp > 0); break;
4684 case LE_EXPR: val = (cmp <= 0); break;
4685 case GE_EXPR: val = (cmp >= 0); break;
4686 default: done = false;
4688 if (done)
4690 if (val)
4691 return boolean_true_node;
4692 else
4693 return fold_build2 (code1, boolean_type_node, op1a, op1b);
4696 /* Likewise if the second comparison is a != comparison. */
4697 else if (code2 == NE_EXPR)
4699 bool done = true;
4700 bool val;
4701 switch (code1)
4703 case EQ_EXPR: val = (cmp == 0); break;
4704 case NE_EXPR: val = (cmp != 0); break;
4705 case LT_EXPR: val = (cmp > 0); break;
4706 case GT_EXPR: val = (cmp < 0); break;
4707 case LE_EXPR: val = (cmp >= 0); break;
4708 case GE_EXPR: val = (cmp <= 0); break;
4709 default: done = false;
4711 if (done)
4713 if (val)
4714 return boolean_true_node;
4715 else
4716 return fold_build2 (code2, boolean_type_node, op2a, op2b);
4720 /* See if an equality test is redundant with the other comparison. */
4721 else if (code1 == EQ_EXPR)
4723 bool val;
4724 switch (code2)
4726 case EQ_EXPR: val = (cmp == 0); break;
4727 case NE_EXPR: val = (cmp != 0); break;
4728 case LT_EXPR: val = (cmp < 0); break;
4729 case GT_EXPR: val = (cmp > 0); break;
4730 case LE_EXPR: val = (cmp <= 0); break;
4731 case GE_EXPR: val = (cmp >= 0); break;
4732 default:
4733 val = false;
4735 if (val)
4736 return fold_build2 (code2, boolean_type_node, op2a, op2b);
4738 else if (code2 == EQ_EXPR)
4740 bool val;
4741 switch (code1)
4743 case EQ_EXPR: val = (cmp == 0); break;
4744 case NE_EXPR: val = (cmp != 0); break;
4745 case LT_EXPR: val = (cmp > 0); break;
4746 case GT_EXPR: val = (cmp < 0); break;
4747 case LE_EXPR: val = (cmp >= 0); break;
4748 case GE_EXPR: val = (cmp <= 0); break;
4749 default:
4750 val = false;
4752 if (val)
4753 return fold_build2 (code1, boolean_type_node, op1a, op1b);
4756 /* Chose the less restrictive of two < or <= comparisons. */
4757 else if ((code1 == LT_EXPR || code1 == LE_EXPR)
4758 && (code2 == LT_EXPR || code2 == LE_EXPR))
4760 if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
4761 return fold_build2 (code2, boolean_type_node, op2a, op2b);
4762 else
4763 return fold_build2 (code1, boolean_type_node, op1a, op1b);
4766 /* Likewise chose the less restrictive of two > or >= comparisons. */
4767 else if ((code1 == GT_EXPR || code1 == GE_EXPR)
4768 && (code2 == GT_EXPR || code2 == GE_EXPR))
4770 if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
4771 return fold_build2 (code2, boolean_type_node, op2a, op2b);
4772 else
4773 return fold_build2 (code1, boolean_type_node, op1a, op1b);
4776 /* Check for singleton ranges. */
4777 else if (cmp == 0
4778 && ((code1 == LT_EXPR && code2 == GT_EXPR)
4779 || (code1 == GT_EXPR && code2 == LT_EXPR)))
4780 return fold_build2 (NE_EXPR, boolean_type_node, op1a, op2b);
4782 /* Check for less/greater pairs that don't restrict the range at all. */
4783 else if (cmp >= 0
4784 && (code1 == LT_EXPR || code1 == LE_EXPR)
4785 && (code2 == GT_EXPR || code2 == GE_EXPR))
4786 return boolean_true_node;
4787 else if (cmp <= 0
4788 && (code1 == GT_EXPR || code1 == GE_EXPR)
4789 && (code2 == LT_EXPR || code2 == LE_EXPR))
4790 return boolean_true_node;
4793 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
4794 NAME's definition is a truth value. See if there are any simplifications
4795 that can be done against the NAME's definition. */
4796 if (TREE_CODE (op1a) == SSA_NAME
4797 && (code1 == NE_EXPR || code1 == EQ_EXPR)
4798 && (integer_zerop (op1b) || integer_onep (op1b)))
4800 bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
4801 || (code1 == NE_EXPR && integer_onep (op1b)));
4802 gimple stmt = SSA_NAME_DEF_STMT (op1a);
4803 switch (gimple_code (stmt))
4805 case GIMPLE_ASSIGN:
4806 /* Try to simplify by copy-propagating the definition. */
4807 return or_var_with_comparison (op1a, invert, code2, op2a, op2b);
4809 case GIMPLE_PHI:
4810 /* If every argument to the PHI produces the same result when
4811 ORed with the second comparison, we win.
4812 Do not do this unless the type is bool since we need a bool
4813 result here anyway. */
4814 if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
4816 tree result = NULL_TREE;
4817 unsigned i;
4818 for (i = 0; i < gimple_phi_num_args (stmt); i++)
4820 tree arg = gimple_phi_arg_def (stmt, i);
4822 /* If this PHI has itself as an argument, ignore it.
4823 If all the other args produce the same result,
4824 we're still OK. */
4825 if (arg == gimple_phi_result (stmt))
4826 continue;
4827 else if (TREE_CODE (arg) == INTEGER_CST)
4829 if (invert ? integer_zerop (arg) : integer_nonzerop (arg))
4831 if (!result)
4832 result = boolean_true_node;
4833 else if (!integer_onep (result))
4834 return NULL_TREE;
4836 else if (!result)
4837 result = fold_build2 (code2, boolean_type_node,
4838 op2a, op2b);
4839 else if (!same_bool_comparison_p (result,
4840 code2, op2a, op2b))
4841 return NULL_TREE;
4843 else if (TREE_CODE (arg) == SSA_NAME
4844 && !SSA_NAME_IS_DEFAULT_DEF (arg))
4846 tree temp;
4847 gimple def_stmt = SSA_NAME_DEF_STMT (arg);
4848 /* In simple cases we can look through PHI nodes,
4849 but we have to be careful with loops.
4850 See PR49073. */
4851 if (! dom_info_available_p (CDI_DOMINATORS)
4852 || gimple_bb (def_stmt) == gimple_bb (stmt)
4853 || dominated_by_p (CDI_DOMINATORS,
4854 gimple_bb (def_stmt),
4855 gimple_bb (stmt)))
4856 return NULL_TREE;
4857 temp = or_var_with_comparison (arg, invert, code2,
4858 op2a, op2b);
4859 if (!temp)
4860 return NULL_TREE;
4861 else if (!result)
4862 result = temp;
4863 else if (!same_bool_result_p (result, temp))
4864 return NULL_TREE;
4866 else
4867 return NULL_TREE;
4869 return result;
4872 default:
4873 break;
4876 return NULL_TREE;
4879 /* Try to simplify the OR of two comparisons, specified by
4880 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
4881 If this can be simplified to a single expression (without requiring
4882 introducing more SSA variables to hold intermediate values),
4883 return the resulting tree. Otherwise return NULL_TREE.
4884 If the result expression is non-null, it has boolean type. */
4886 tree
4887 maybe_fold_or_comparisons (enum tree_code code1, tree op1a, tree op1b,
4888 enum tree_code code2, tree op2a, tree op2b)
4890 tree t = or_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b);
4891 if (t)
4892 return t;
4893 else
4894 return or_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b);
4898 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
4900 Either NULL_TREE, a simplified but non-constant or a constant
4901 is returned.
4903 ??? This should go into a gimple-fold-inline.h file to be eventually
4904 privatized with the single valueize function used in the various TUs
4905 to avoid the indirect function call overhead. */
4907 tree
4908 gimple_fold_stmt_to_constant_1 (gimple stmt, tree (*valueize) (tree),
4909 tree (*gvalueize) (tree))
4911 code_helper rcode;
4912 tree ops[3] = {};
4913 /* ??? The SSA propagators do not correctly deal with following SSA use-def
4914 edges if there are intermediate VARYING defs. For this reason
4915 do not follow SSA edges here even though SCCVN can technically
4916 just deal fine with that. */
4917 if (gimple_simplify (stmt, &rcode, ops, NULL, gvalueize, valueize)
4918 && rcode.is_tree_code ()
4919 && (TREE_CODE_LENGTH ((tree_code) rcode) == 0
4920 || ((tree_code) rcode) == ADDR_EXPR)
4921 && is_gimple_val (ops[0]))
4923 tree res = ops[0];
4924 if (dump_file && dump_flags & TDF_DETAILS)
4926 fprintf (dump_file, "Match-and-simplified ");
4927 print_gimple_expr (dump_file, stmt, 0, TDF_SLIM);
4928 fprintf (dump_file, " to ");
4929 print_generic_expr (dump_file, res, 0);
4930 fprintf (dump_file, "\n");
4932 return res;
4935 location_t loc = gimple_location (stmt);
4936 switch (gimple_code (stmt))
4938 case GIMPLE_ASSIGN:
4940 enum tree_code subcode = gimple_assign_rhs_code (stmt);
4942 switch (get_gimple_rhs_class (subcode))
4944 case GIMPLE_SINGLE_RHS:
4946 tree rhs = gimple_assign_rhs1 (stmt);
4947 enum tree_code_class kind = TREE_CODE_CLASS (subcode);
4949 if (TREE_CODE (rhs) == SSA_NAME)
4951 /* If the RHS is an SSA_NAME, return its known constant value,
4952 if any. */
4953 return (*valueize) (rhs);
4955 /* Handle propagating invariant addresses into address
4956 operations. */
4957 else if (TREE_CODE (rhs) == ADDR_EXPR
4958 && !is_gimple_min_invariant (rhs))
4960 HOST_WIDE_INT offset = 0;
4961 tree base;
4962 base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs, 0),
4963 &offset,
4964 valueize);
4965 if (base
4966 && (CONSTANT_CLASS_P (base)
4967 || decl_address_invariant_p (base)))
4968 return build_invariant_address (TREE_TYPE (rhs),
4969 base, offset);
4971 else if (TREE_CODE (rhs) == CONSTRUCTOR
4972 && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE
4973 && (CONSTRUCTOR_NELTS (rhs)
4974 == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs))))
4976 unsigned i;
4977 tree val, *vec;
4979 vec = XALLOCAVEC (tree,
4980 TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)));
4981 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
4983 val = (*valueize) (val);
4984 if (TREE_CODE (val) == INTEGER_CST
4985 || TREE_CODE (val) == REAL_CST
4986 || TREE_CODE (val) == FIXED_CST)
4987 vec[i] = val;
4988 else
4989 return NULL_TREE;
4992 return build_vector (TREE_TYPE (rhs), vec);
4994 if (subcode == OBJ_TYPE_REF)
4996 tree val = (*valueize) (OBJ_TYPE_REF_EXPR (rhs));
4997 /* If callee is constant, we can fold away the wrapper. */
4998 if (is_gimple_min_invariant (val))
4999 return val;
5002 if (kind == tcc_reference)
5004 if ((TREE_CODE (rhs) == VIEW_CONVERT_EXPR
5005 || TREE_CODE (rhs) == REALPART_EXPR
5006 || TREE_CODE (rhs) == IMAGPART_EXPR)
5007 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
5009 tree val = (*valueize) (TREE_OPERAND (rhs, 0));
5010 return fold_unary_loc (EXPR_LOCATION (rhs),
5011 TREE_CODE (rhs),
5012 TREE_TYPE (rhs), val);
5014 else if (TREE_CODE (rhs) == BIT_FIELD_REF
5015 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
5017 tree val = (*valueize) (TREE_OPERAND (rhs, 0));
5018 return fold_ternary_loc (EXPR_LOCATION (rhs),
5019 TREE_CODE (rhs),
5020 TREE_TYPE (rhs), val,
5021 TREE_OPERAND (rhs, 1),
5022 TREE_OPERAND (rhs, 2));
5024 else if (TREE_CODE (rhs) == MEM_REF
5025 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
5027 tree val = (*valueize) (TREE_OPERAND (rhs, 0));
5028 if (TREE_CODE (val) == ADDR_EXPR
5029 && is_gimple_min_invariant (val))
5031 tree tem = fold_build2 (MEM_REF, TREE_TYPE (rhs),
5032 unshare_expr (val),
5033 TREE_OPERAND (rhs, 1));
5034 if (tem)
5035 rhs = tem;
5038 return fold_const_aggregate_ref_1 (rhs, valueize);
5040 else if (kind == tcc_declaration)
5041 return get_symbol_constant_value (rhs);
5042 return rhs;
5045 case GIMPLE_UNARY_RHS:
5046 return NULL_TREE;
5048 case GIMPLE_BINARY_RHS:
5050 /* Handle binary operators that can appear in GIMPLE form. */
5051 tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
5052 tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
5054 /* Translate &x + CST into an invariant form suitable for
5055 further propagation. */
5056 if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
5057 && TREE_CODE (op0) == ADDR_EXPR
5058 && TREE_CODE (op1) == INTEGER_CST)
5060 tree off = fold_convert (ptr_type_node, op1);
5061 return build_fold_addr_expr_loc
5062 (loc,
5063 fold_build2 (MEM_REF,
5064 TREE_TYPE (TREE_TYPE (op0)),
5065 unshare_expr (op0), off));
5068 return fold_binary_loc (loc, subcode,
5069 gimple_expr_type (stmt), op0, op1);
5072 case GIMPLE_TERNARY_RHS:
5074 /* Handle ternary operators that can appear in GIMPLE form. */
5075 tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
5076 tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
5077 tree op2 = (*valueize) (gimple_assign_rhs3 (stmt));
5079 /* Fold embedded expressions in ternary codes. */
5080 if ((subcode == COND_EXPR
5081 || subcode == VEC_COND_EXPR)
5082 && COMPARISON_CLASS_P (op0))
5084 tree op00 = (*valueize) (TREE_OPERAND (op0, 0));
5085 tree op01 = (*valueize) (TREE_OPERAND (op0, 1));
5086 tree tem = fold_binary_loc (loc, TREE_CODE (op0),
5087 TREE_TYPE (op0), op00, op01);
5088 if (tem)
5089 op0 = tem;
5092 return fold_ternary_loc (loc, subcode,
5093 gimple_expr_type (stmt), op0, op1, op2);
5096 default:
5097 gcc_unreachable ();
5101 case GIMPLE_CALL:
5103 tree fn;
5104 gcall *call_stmt = as_a <gcall *> (stmt);
5106 if (gimple_call_internal_p (stmt))
5108 enum tree_code subcode = ERROR_MARK;
5109 switch (gimple_call_internal_fn (stmt))
5111 case IFN_UBSAN_CHECK_ADD:
5112 subcode = PLUS_EXPR;
5113 break;
5114 case IFN_UBSAN_CHECK_SUB:
5115 subcode = MINUS_EXPR;
5116 break;
5117 case IFN_UBSAN_CHECK_MUL:
5118 subcode = MULT_EXPR;
5119 break;
5120 default:
5121 return NULL_TREE;
5123 tree arg0 = gimple_call_arg (stmt, 0);
5124 tree arg1 = gimple_call_arg (stmt, 1);
5125 tree op0 = (*valueize) (arg0);
5126 tree op1 = (*valueize) (arg1);
5128 if (TREE_CODE (op0) != INTEGER_CST
5129 || TREE_CODE (op1) != INTEGER_CST)
5131 switch (subcode)
5133 case MULT_EXPR:
5134 /* x * 0 = 0 * x = 0 without overflow. */
5135 if (integer_zerop (op0) || integer_zerop (op1))
5136 return build_zero_cst (TREE_TYPE (arg0));
5137 break;
5138 case MINUS_EXPR:
5139 /* y - y = 0 without overflow. */
5140 if (operand_equal_p (op0, op1, 0))
5141 return build_zero_cst (TREE_TYPE (arg0));
5142 break;
5143 default:
5144 break;
5147 tree res
5148 = fold_binary_loc (loc, subcode, TREE_TYPE (arg0), op0, op1);
5149 if (res
5150 && TREE_CODE (res) == INTEGER_CST
5151 && !TREE_OVERFLOW (res))
5152 return res;
5153 return NULL_TREE;
5156 fn = (*valueize) (gimple_call_fn (stmt));
5157 if (TREE_CODE (fn) == ADDR_EXPR
5158 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5159 && DECL_BUILT_IN (TREE_OPERAND (fn, 0))
5160 && gimple_builtin_call_types_compatible_p (stmt,
5161 TREE_OPERAND (fn, 0)))
5163 tree *args = XALLOCAVEC (tree, gimple_call_num_args (stmt));
5164 tree retval;
5165 unsigned i;
5166 for (i = 0; i < gimple_call_num_args (stmt); ++i)
5167 args[i] = (*valueize) (gimple_call_arg (stmt, i));
5168 retval = fold_builtin_call_array (loc,
5169 gimple_call_return_type (call_stmt),
5170 fn, gimple_call_num_args (stmt), args);
5171 if (retval)
5173 /* fold_call_expr wraps the result inside a NOP_EXPR. */
5174 STRIP_NOPS (retval);
5175 retval = fold_convert (gimple_call_return_type (call_stmt),
5176 retval);
5178 return retval;
5180 return NULL_TREE;
5183 default:
5184 return NULL_TREE;
5188 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
5189 Returns NULL_TREE if folding to a constant is not possible, otherwise
5190 returns a constant according to is_gimple_min_invariant. */
5192 tree
5193 gimple_fold_stmt_to_constant (gimple stmt, tree (*valueize) (tree))
5195 tree res = gimple_fold_stmt_to_constant_1 (stmt, valueize);
5196 if (res && is_gimple_min_invariant (res))
5197 return res;
5198 return NULL_TREE;
5202 /* The following set of functions are supposed to fold references using
5203 their constant initializers. */
5205 /* See if we can find constructor defining value of BASE.
5206 When we know the consructor with constant offset (such as
5207 base is array[40] and we do know constructor of array), then
5208 BIT_OFFSET is adjusted accordingly.
5210 As a special case, return error_mark_node when constructor
5211 is not explicitly available, but it is known to be zero
5212 such as 'static const int a;'. */
5213 static tree
5214 get_base_constructor (tree base, HOST_WIDE_INT *bit_offset,
5215 tree (*valueize)(tree))
5217 HOST_WIDE_INT bit_offset2, size, max_size;
5218 if (TREE_CODE (base) == MEM_REF)
5220 if (!integer_zerop (TREE_OPERAND (base, 1)))
5222 if (!tree_fits_shwi_p (TREE_OPERAND (base, 1)))
5223 return NULL_TREE;
5224 *bit_offset += (mem_ref_offset (base).to_short_addr ()
5225 * BITS_PER_UNIT);
5228 if (valueize
5229 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
5230 base = valueize (TREE_OPERAND (base, 0));
5231 if (!base || TREE_CODE (base) != ADDR_EXPR)
5232 return NULL_TREE;
5233 base = TREE_OPERAND (base, 0);
5236 /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
5237 DECL_INITIAL. If BASE is a nested reference into another
5238 ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
5239 the inner reference. */
5240 switch (TREE_CODE (base))
5242 case VAR_DECL:
5243 case CONST_DECL:
5245 tree init = ctor_for_folding (base);
5247 /* Our semantic is exact opposite of ctor_for_folding;
5248 NULL means unknown, while error_mark_node is 0. */
5249 if (init == error_mark_node)
5250 return NULL_TREE;
5251 if (!init)
5252 return error_mark_node;
5253 return init;
5256 case ARRAY_REF:
5257 case COMPONENT_REF:
5258 base = get_ref_base_and_extent (base, &bit_offset2, &size, &max_size);
5259 if (max_size == -1 || size != max_size)
5260 return NULL_TREE;
5261 *bit_offset += bit_offset2;
5262 return get_base_constructor (base, bit_offset, valueize);
5264 case STRING_CST:
5265 case CONSTRUCTOR:
5266 return base;
5268 default:
5269 return NULL_TREE;
5273 /* CTOR is CONSTRUCTOR of an array type. Fold reference of type TYPE and size
5274 SIZE to the memory at bit OFFSET. */
5276 static tree
5277 fold_array_ctor_reference (tree type, tree ctor,
5278 unsigned HOST_WIDE_INT offset,
5279 unsigned HOST_WIDE_INT size,
5280 tree from_decl)
5282 unsigned HOST_WIDE_INT cnt;
5283 tree cfield, cval;
5284 offset_int low_bound;
5285 offset_int elt_size;
5286 offset_int index, max_index;
5287 offset_int access_index;
5288 tree domain_type = NULL_TREE, index_type = NULL_TREE;
5289 HOST_WIDE_INT inner_offset;
5291 /* Compute low bound and elt size. */
5292 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
5293 domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
5294 if (domain_type && TYPE_MIN_VALUE (domain_type))
5296 /* Static constructors for variably sized objects makes no sense. */
5297 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
5298 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
5299 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
5301 else
5302 low_bound = 0;
5303 /* Static constructors for variably sized objects makes no sense. */
5304 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor))))
5305 == INTEGER_CST);
5306 elt_size = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor))));
5308 /* We can handle only constantly sized accesses that are known to not
5309 be larger than size of array element. */
5310 if (!TYPE_SIZE_UNIT (type)
5311 || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
5312 || wi::lts_p (elt_size, wi::to_offset (TYPE_SIZE_UNIT (type)))
5313 || elt_size == 0)
5314 return NULL_TREE;
5316 /* Compute the array index we look for. */
5317 access_index = wi::udiv_trunc (offset_int (offset / BITS_PER_UNIT),
5318 elt_size);
5319 access_index += low_bound;
5320 if (index_type)
5321 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
5322 TYPE_SIGN (index_type));
5324 /* And offset within the access. */
5325 inner_offset = offset % (elt_size.to_uhwi () * BITS_PER_UNIT);
5327 /* See if the array field is large enough to span whole access. We do not
5328 care to fold accesses spanning multiple array indexes. */
5329 if (inner_offset + size > elt_size.to_uhwi () * BITS_PER_UNIT)
5330 return NULL_TREE;
5332 index = low_bound - 1;
5333 if (index_type)
5334 index = wi::ext (index, TYPE_PRECISION (index_type),
5335 TYPE_SIGN (index_type));
5337 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
5339 /* Array constructor might explicitely set index, or specify range
5340 or leave index NULL meaning that it is next index after previous
5341 one. */
5342 if (cfield)
5344 if (TREE_CODE (cfield) == INTEGER_CST)
5345 max_index = index = wi::to_offset (cfield);
5346 else
5348 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
5349 index = wi::to_offset (TREE_OPERAND (cfield, 0));
5350 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
5353 else
5355 index += 1;
5356 if (index_type)
5357 index = wi::ext (index, TYPE_PRECISION (index_type),
5358 TYPE_SIGN (index_type));
5359 max_index = index;
5362 /* Do we have match? */
5363 if (wi::cmpu (access_index, index) >= 0
5364 && wi::cmpu (access_index, max_index) <= 0)
5365 return fold_ctor_reference (type, cval, inner_offset, size,
5366 from_decl);
5368 /* When memory is not explicitely mentioned in constructor,
5369 it is 0 (or out of range). */
5370 return build_zero_cst (type);
5373 /* CTOR is CONSTRUCTOR of an aggregate or vector.
5374 Fold reference of type TYPE and size SIZE to the memory at bit OFFSET. */
5376 static tree
5377 fold_nonarray_ctor_reference (tree type, tree ctor,
5378 unsigned HOST_WIDE_INT offset,
5379 unsigned HOST_WIDE_INT size,
5380 tree from_decl)
5382 unsigned HOST_WIDE_INT cnt;
5383 tree cfield, cval;
5385 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield,
5386 cval)
5388 tree byte_offset = DECL_FIELD_OFFSET (cfield);
5389 tree field_offset = DECL_FIELD_BIT_OFFSET (cfield);
5390 tree field_size = DECL_SIZE (cfield);
5391 offset_int bitoffset;
5392 offset_int bitoffset_end, access_end;
5394 /* Variable sized objects in static constructors makes no sense,
5395 but field_size can be NULL for flexible array members. */
5396 gcc_assert (TREE_CODE (field_offset) == INTEGER_CST
5397 && TREE_CODE (byte_offset) == INTEGER_CST
5398 && (field_size != NULL_TREE
5399 ? TREE_CODE (field_size) == INTEGER_CST
5400 : TREE_CODE (TREE_TYPE (cfield)) == ARRAY_TYPE));
5402 /* Compute bit offset of the field. */
5403 bitoffset = (wi::to_offset (field_offset)
5404 + wi::lshift (wi::to_offset (byte_offset),
5405 LOG2_BITS_PER_UNIT));
5406 /* Compute bit offset where the field ends. */
5407 if (field_size != NULL_TREE)
5408 bitoffset_end = bitoffset + wi::to_offset (field_size);
5409 else
5410 bitoffset_end = 0;
5412 access_end = offset_int (offset) + size;
5414 /* Is there any overlap between [OFFSET, OFFSET+SIZE) and
5415 [BITOFFSET, BITOFFSET_END)? */
5416 if (wi::cmps (access_end, bitoffset) > 0
5417 && (field_size == NULL_TREE
5418 || wi::lts_p (offset, bitoffset_end)))
5420 offset_int inner_offset = offset_int (offset) - bitoffset;
5421 /* We do have overlap. Now see if field is large enough to
5422 cover the access. Give up for accesses spanning multiple
5423 fields. */
5424 if (wi::cmps (access_end, bitoffset_end) > 0)
5425 return NULL_TREE;
5426 if (wi::lts_p (offset, bitoffset))
5427 return NULL_TREE;
5428 return fold_ctor_reference (type, cval,
5429 inner_offset.to_uhwi (), size,
5430 from_decl);
5433 /* When memory is not explicitely mentioned in constructor, it is 0. */
5434 return build_zero_cst (type);
5437 /* CTOR is value initializing memory, fold reference of type TYPE and size SIZE
5438 to the memory at bit OFFSET. */
5440 tree
5441 fold_ctor_reference (tree type, tree ctor, unsigned HOST_WIDE_INT offset,
5442 unsigned HOST_WIDE_INT size, tree from_decl)
5444 tree ret;
5446 /* We found the field with exact match. */
5447 if (useless_type_conversion_p (type, TREE_TYPE (ctor))
5448 && !offset)
5449 return canonicalize_constructor_val (unshare_expr (ctor), from_decl);
5451 /* We are at the end of walk, see if we can view convert the
5452 result. */
5453 if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor)) && !offset
5454 /* VIEW_CONVERT_EXPR is defined only for matching sizes. */
5455 && !compare_tree_int (TYPE_SIZE (type), size)
5456 && !compare_tree_int (TYPE_SIZE (TREE_TYPE (ctor)), size))
5458 ret = canonicalize_constructor_val (unshare_expr (ctor), from_decl);
5459 ret = fold_unary (VIEW_CONVERT_EXPR, type, ret);
5460 if (ret)
5461 STRIP_USELESS_TYPE_CONVERSION (ret);
5462 return ret;
5464 /* For constants and byte-aligned/sized reads try to go through
5465 native_encode/interpret. */
5466 if (CONSTANT_CLASS_P (ctor)
5467 && BITS_PER_UNIT == 8
5468 && offset % BITS_PER_UNIT == 0
5469 && size % BITS_PER_UNIT == 0
5470 && size <= MAX_BITSIZE_MODE_ANY_MODE)
5472 unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
5473 if (native_encode_expr (ctor, buf, size / BITS_PER_UNIT,
5474 offset / BITS_PER_UNIT) > 0)
5475 return native_interpret_expr (type, buf, size / BITS_PER_UNIT);
5477 if (TREE_CODE (ctor) == CONSTRUCTOR)
5480 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE
5481 || TREE_CODE (TREE_TYPE (ctor)) == VECTOR_TYPE)
5482 return fold_array_ctor_reference (type, ctor, offset, size,
5483 from_decl);
5484 else
5485 return fold_nonarray_ctor_reference (type, ctor, offset, size,
5486 from_decl);
5489 return NULL_TREE;
5492 /* Return the tree representing the element referenced by T if T is an
5493 ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
5494 names using VALUEIZE. Return NULL_TREE otherwise. */
5496 tree
5497 fold_const_aggregate_ref_1 (tree t, tree (*valueize) (tree))
5499 tree ctor, idx, base;
5500 HOST_WIDE_INT offset, size, max_size;
5501 tree tem;
5503 if (TREE_THIS_VOLATILE (t))
5504 return NULL_TREE;
5506 if (DECL_P (t))
5507 return get_symbol_constant_value (t);
5509 tem = fold_read_from_constant_string (t);
5510 if (tem)
5511 return tem;
5513 switch (TREE_CODE (t))
5515 case ARRAY_REF:
5516 case ARRAY_RANGE_REF:
5517 /* Constant indexes are handled well by get_base_constructor.
5518 Only special case variable offsets.
5519 FIXME: This code can't handle nested references with variable indexes
5520 (they will be handled only by iteration of ccp). Perhaps we can bring
5521 get_ref_base_and_extent here and make it use a valueize callback. */
5522 if (TREE_CODE (TREE_OPERAND (t, 1)) == SSA_NAME
5523 && valueize
5524 && (idx = (*valueize) (TREE_OPERAND (t, 1)))
5525 && TREE_CODE (idx) == INTEGER_CST)
5527 tree low_bound, unit_size;
5529 /* If the resulting bit-offset is constant, track it. */
5530 if ((low_bound = array_ref_low_bound (t),
5531 TREE_CODE (low_bound) == INTEGER_CST)
5532 && (unit_size = array_ref_element_size (t),
5533 tree_fits_uhwi_p (unit_size)))
5535 offset_int woffset
5536 = wi::sext (wi::to_offset (idx) - wi::to_offset (low_bound),
5537 TYPE_PRECISION (TREE_TYPE (idx)));
5539 if (wi::fits_shwi_p (woffset))
5541 offset = woffset.to_shwi ();
5542 /* TODO: This code seems wrong, multiply then check
5543 to see if it fits. */
5544 offset *= tree_to_uhwi (unit_size);
5545 offset *= BITS_PER_UNIT;
5547 base = TREE_OPERAND (t, 0);
5548 ctor = get_base_constructor (base, &offset, valueize);
5549 /* Empty constructor. Always fold to 0. */
5550 if (ctor == error_mark_node)
5551 return build_zero_cst (TREE_TYPE (t));
5552 /* Out of bound array access. Value is undefined,
5553 but don't fold. */
5554 if (offset < 0)
5555 return NULL_TREE;
5556 /* We can not determine ctor. */
5557 if (!ctor)
5558 return NULL_TREE;
5559 return fold_ctor_reference (TREE_TYPE (t), ctor, offset,
5560 tree_to_uhwi (unit_size)
5561 * BITS_PER_UNIT,
5562 base);
5566 /* Fallthru. */
5568 case COMPONENT_REF:
5569 case BIT_FIELD_REF:
5570 case TARGET_MEM_REF:
5571 case MEM_REF:
5572 base = get_ref_base_and_extent (t, &offset, &size, &max_size);
5573 ctor = get_base_constructor (base, &offset, valueize);
5575 /* Empty constructor. Always fold to 0. */
5576 if (ctor == error_mark_node)
5577 return build_zero_cst (TREE_TYPE (t));
5578 /* We do not know precise address. */
5579 if (max_size == -1 || max_size != size)
5580 return NULL_TREE;
5581 /* We can not determine ctor. */
5582 if (!ctor)
5583 return NULL_TREE;
5585 /* Out of bound array access. Value is undefined, but don't fold. */
5586 if (offset < 0)
5587 return NULL_TREE;
5589 return fold_ctor_reference (TREE_TYPE (t), ctor, offset, size,
5590 base);
5592 case REALPART_EXPR:
5593 case IMAGPART_EXPR:
5595 tree c = fold_const_aggregate_ref_1 (TREE_OPERAND (t, 0), valueize);
5596 if (c && TREE_CODE (c) == COMPLEX_CST)
5597 return fold_build1_loc (EXPR_LOCATION (t),
5598 TREE_CODE (t), TREE_TYPE (t), c);
5599 break;
5602 default:
5603 break;
5606 return NULL_TREE;
5609 tree
5610 fold_const_aggregate_ref (tree t)
5612 return fold_const_aggregate_ref_1 (t, NULL);
5615 /* Lookup virtual method with index TOKEN in a virtual table V
5616 at OFFSET.
5617 Set CAN_REFER if non-NULL to false if method
5618 is not referable or if the virtual table is ill-formed (such as rewriten
5619 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
5621 tree
5622 gimple_get_virt_method_for_vtable (HOST_WIDE_INT token,
5623 tree v,
5624 unsigned HOST_WIDE_INT offset,
5625 bool *can_refer)
5627 tree vtable = v, init, fn;
5628 unsigned HOST_WIDE_INT size;
5629 unsigned HOST_WIDE_INT elt_size, access_index;
5630 tree domain_type;
5632 if (can_refer)
5633 *can_refer = true;
5635 /* First of all double check we have virtual table. */
5636 if (TREE_CODE (v) != VAR_DECL
5637 || !DECL_VIRTUAL_P (v))
5639 /* Pass down that we lost track of the target. */
5640 if (can_refer)
5641 *can_refer = false;
5642 return NULL_TREE;
5645 init = ctor_for_folding (v);
5647 /* The virtual tables should always be born with constructors
5648 and we always should assume that they are avaialble for
5649 folding. At the moment we do not stream them in all cases,
5650 but it should never happen that ctor seem unreachable. */
5651 gcc_assert (init);
5652 if (init == error_mark_node)
5654 gcc_assert (in_lto_p);
5655 /* Pass down that we lost track of the target. */
5656 if (can_refer)
5657 *can_refer = false;
5658 return NULL_TREE;
5660 gcc_checking_assert (TREE_CODE (TREE_TYPE (v)) == ARRAY_TYPE);
5661 size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v))));
5662 offset *= BITS_PER_UNIT;
5663 offset += token * size;
5665 /* Lookup the value in the constructor that is assumed to be array.
5666 This is equivalent to
5667 fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
5668 offset, size, NULL);
5669 but in a constant time. We expect that frontend produced a simple
5670 array without indexed initializers. */
5672 gcc_checking_assert (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE);
5673 domain_type = TYPE_DOMAIN (TREE_TYPE (init));
5674 gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type)));
5675 elt_size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init))));
5677 access_index = offset / BITS_PER_UNIT / elt_size;
5678 gcc_checking_assert (offset % (elt_size * BITS_PER_UNIT) == 0);
5680 /* This code makes an assumption that there are no
5681 indexed fileds produced by C++ FE, so we can directly index the array. */
5682 if (access_index < CONSTRUCTOR_NELTS (init))
5684 fn = CONSTRUCTOR_ELT (init, access_index)->value;
5685 gcc_checking_assert (!CONSTRUCTOR_ELT (init, access_index)->index);
5686 STRIP_NOPS (fn);
5688 else
5689 fn = NULL;
5691 /* For type inconsistent program we may end up looking up virtual method
5692 in virtual table that does not contain TOKEN entries. We may overrun
5693 the virtual table and pick up a constant or RTTI info pointer.
5694 In any case the call is undefined. */
5695 if (!fn
5696 || (TREE_CODE (fn) != ADDR_EXPR && TREE_CODE (fn) != FDESC_EXPR)
5697 || TREE_CODE (TREE_OPERAND (fn, 0)) != FUNCTION_DECL)
5698 fn = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
5699 else
5701 fn = TREE_OPERAND (fn, 0);
5703 /* When cgraph node is missing and function is not public, we cannot
5704 devirtualize. This can happen in WHOPR when the actual method
5705 ends up in other partition, because we found devirtualization
5706 possibility too late. */
5707 if (!can_refer_decl_in_current_unit_p (fn, vtable))
5709 if (can_refer)
5711 *can_refer = false;
5712 return fn;
5714 return NULL_TREE;
5718 /* Make sure we create a cgraph node for functions we'll reference.
5719 They can be non-existent if the reference comes from an entry
5720 of an external vtable for example. */
5721 cgraph_node::get_create (fn);
5723 return fn;
5726 /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
5727 is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
5728 KNOWN_BINFO carries the binfo describing the true type of
5729 OBJ_TYPE_REF_OBJECT(REF).
5730 Set CAN_REFER if non-NULL to false if method
5731 is not referable or if the virtual table is ill-formed (such as rewriten
5732 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
5734 tree
5735 gimple_get_virt_method_for_binfo (HOST_WIDE_INT token, tree known_binfo,
5736 bool *can_refer)
5738 unsigned HOST_WIDE_INT offset;
5739 tree v;
5741 v = BINFO_VTABLE (known_binfo);
5742 /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */
5743 if (!v)
5744 return NULL_TREE;
5746 if (!vtable_pointer_value_to_vtable (v, &v, &offset))
5748 if (can_refer)
5749 *can_refer = false;
5750 return NULL_TREE;
5752 return gimple_get_virt_method_for_vtable (token, v, offset, can_refer);
5755 /* Return true iff VAL is a gimple expression that is known to be
5756 non-negative. Restricted to floating-point inputs. */
5758 bool
5759 gimple_val_nonnegative_real_p (tree val)
5761 gimple def_stmt;
5763 gcc_assert (val && SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)));
5765 /* Use existing logic for non-gimple trees. */
5766 if (tree_expr_nonnegative_p (val))
5767 return true;
5769 if (TREE_CODE (val) != SSA_NAME)
5770 return false;
5772 /* Currently we look only at the immediately defining statement
5773 to make this determination, since recursion on defining
5774 statements of operands can lead to quadratic behavior in the
5775 worst case. This is expected to catch almost all occurrences
5776 in practice. It would be possible to implement limited-depth
5777 recursion if important cases are lost. Alternatively, passes
5778 that need this information (such as the pow/powi lowering code
5779 in the cse_sincos pass) could be revised to provide it through
5780 dataflow propagation. */
5782 def_stmt = SSA_NAME_DEF_STMT (val);
5784 if (is_gimple_assign (def_stmt))
5786 tree op0, op1;
5788 /* See fold-const.c:tree_expr_nonnegative_p for additional
5789 cases that could be handled with recursion. */
5791 switch (gimple_assign_rhs_code (def_stmt))
5793 case ABS_EXPR:
5794 /* Always true for floating-point operands. */
5795 return true;
5797 case MULT_EXPR:
5798 /* True if the two operands are identical (since we are
5799 restricted to floating-point inputs). */
5800 op0 = gimple_assign_rhs1 (def_stmt);
5801 op1 = gimple_assign_rhs2 (def_stmt);
5803 if (op0 == op1
5804 || operand_equal_p (op0, op1, 0))
5805 return true;
5807 default:
5808 return false;
5811 else if (is_gimple_call (def_stmt))
5813 tree fndecl = gimple_call_fndecl (def_stmt);
5814 if (fndecl
5815 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
5817 tree arg1;
5819 switch (DECL_FUNCTION_CODE (fndecl))
5821 CASE_FLT_FN (BUILT_IN_ACOS):
5822 CASE_FLT_FN (BUILT_IN_ACOSH):
5823 CASE_FLT_FN (BUILT_IN_CABS):
5824 CASE_FLT_FN (BUILT_IN_COSH):
5825 CASE_FLT_FN (BUILT_IN_ERFC):
5826 CASE_FLT_FN (BUILT_IN_EXP):
5827 CASE_FLT_FN (BUILT_IN_EXP10):
5828 CASE_FLT_FN (BUILT_IN_EXP2):
5829 CASE_FLT_FN (BUILT_IN_FABS):
5830 CASE_FLT_FN (BUILT_IN_FDIM):
5831 CASE_FLT_FN (BUILT_IN_HYPOT):
5832 CASE_FLT_FN (BUILT_IN_POW10):
5833 return true;
5835 CASE_FLT_FN (BUILT_IN_SQRT):
5836 /* sqrt(-0.0) is -0.0, and sqrt is not defined over other
5837 nonnegative inputs. */
5838 if (!HONOR_SIGNED_ZEROS (val))
5839 return true;
5841 break;
5843 CASE_FLT_FN (BUILT_IN_POWI):
5844 /* True if the second argument is an even integer. */
5845 arg1 = gimple_call_arg (def_stmt, 1);
5847 if (TREE_CODE (arg1) == INTEGER_CST
5848 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
5849 return true;
5851 break;
5853 CASE_FLT_FN (BUILT_IN_POW):
5854 /* True if the second argument is an even integer-valued
5855 real. */
5856 arg1 = gimple_call_arg (def_stmt, 1);
5858 if (TREE_CODE (arg1) == REAL_CST)
5860 REAL_VALUE_TYPE c;
5861 HOST_WIDE_INT n;
5863 c = TREE_REAL_CST (arg1);
5864 n = real_to_integer (&c);
5866 if ((n & 1) == 0)
5868 REAL_VALUE_TYPE cint;
5869 real_from_integer (&cint, VOIDmode, n, SIGNED);
5870 if (real_identical (&c, &cint))
5871 return true;
5875 break;
5877 default:
5878 return false;
5883 return false;
5886 /* Given a pointer value OP0, return a simplified version of an
5887 indirection through OP0, or NULL_TREE if no simplification is
5888 possible. Note that the resulting type may be different from
5889 the type pointed to in the sense that it is still compatible
5890 from the langhooks point of view. */
5892 tree
5893 gimple_fold_indirect_ref (tree t)
5895 tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype);
5896 tree sub = t;
5897 tree subtype;
5899 STRIP_NOPS (sub);
5900 subtype = TREE_TYPE (sub);
5901 if (!POINTER_TYPE_P (subtype))
5902 return NULL_TREE;
5904 if (TREE_CODE (sub) == ADDR_EXPR)
5906 tree op = TREE_OPERAND (sub, 0);
5907 tree optype = TREE_TYPE (op);
5908 /* *&p => p */
5909 if (useless_type_conversion_p (type, optype))
5910 return op;
5912 /* *(foo *)&fooarray => fooarray[0] */
5913 if (TREE_CODE (optype) == ARRAY_TYPE
5914 && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST
5915 && useless_type_conversion_p (type, TREE_TYPE (optype)))
5917 tree type_domain = TYPE_DOMAIN (optype);
5918 tree min_val = size_zero_node;
5919 if (type_domain && TYPE_MIN_VALUE (type_domain))
5920 min_val = TYPE_MIN_VALUE (type_domain);
5921 if (TREE_CODE (min_val) == INTEGER_CST)
5922 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
5924 /* *(foo *)&complexfoo => __real__ complexfoo */
5925 else if (TREE_CODE (optype) == COMPLEX_TYPE
5926 && useless_type_conversion_p (type, TREE_TYPE (optype)))
5927 return fold_build1 (REALPART_EXPR, type, op);
5928 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
5929 else if (TREE_CODE (optype) == VECTOR_TYPE
5930 && useless_type_conversion_p (type, TREE_TYPE (optype)))
5932 tree part_width = TYPE_SIZE (type);
5933 tree index = bitsize_int (0);
5934 return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
5938 /* *(p + CST) -> ... */
5939 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
5940 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
5942 tree addr = TREE_OPERAND (sub, 0);
5943 tree off = TREE_OPERAND (sub, 1);
5944 tree addrtype;
5946 STRIP_NOPS (addr);
5947 addrtype = TREE_TYPE (addr);
5949 /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
5950 if (TREE_CODE (addr) == ADDR_EXPR
5951 && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE
5952 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))
5953 && tree_fits_uhwi_p (off))
5955 unsigned HOST_WIDE_INT offset = tree_to_uhwi (off);
5956 tree part_width = TYPE_SIZE (type);
5957 unsigned HOST_WIDE_INT part_widthi
5958 = tree_to_shwi (part_width) / BITS_PER_UNIT;
5959 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
5960 tree index = bitsize_int (indexi);
5961 if (offset / part_widthi
5962 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype)))
5963 return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0),
5964 part_width, index);
5967 /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
5968 if (TREE_CODE (addr) == ADDR_EXPR
5969 && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE
5970 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))))
5972 tree size = TYPE_SIZE_UNIT (type);
5973 if (tree_int_cst_equal (size, off))
5974 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0));
5977 /* *(p + CST) -> MEM_REF <p, CST>. */
5978 if (TREE_CODE (addr) != ADDR_EXPR
5979 || DECL_P (TREE_OPERAND (addr, 0)))
5980 return fold_build2 (MEM_REF, type,
5981 addr,
5982 wide_int_to_tree (ptype, off));
5985 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
5986 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
5987 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST
5988 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
5990 tree type_domain;
5991 tree min_val = size_zero_node;
5992 tree osub = sub;
5993 sub = gimple_fold_indirect_ref (sub);
5994 if (! sub)
5995 sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
5996 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
5997 if (type_domain && TYPE_MIN_VALUE (type_domain))
5998 min_val = TYPE_MIN_VALUE (type_domain);
5999 if (TREE_CODE (min_val) == INTEGER_CST)
6000 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
6003 return NULL_TREE;
6006 /* Return true if CODE is an operation that when operating on signed
6007 integer types involves undefined behavior on overflow and the
6008 operation can be expressed with unsigned arithmetic. */
6010 bool
6011 arith_code_with_undefined_signed_overflow (tree_code code)
6013 switch (code)
6015 case PLUS_EXPR:
6016 case MINUS_EXPR:
6017 case MULT_EXPR:
6018 case NEGATE_EXPR:
6019 case POINTER_PLUS_EXPR:
6020 return true;
6021 default:
6022 return false;
6026 /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic
6027 operation that can be transformed to unsigned arithmetic by converting
6028 its operand, carrying out the operation in the corresponding unsigned
6029 type and converting the result back to the original type.
6031 Returns a sequence of statements that replace STMT and also contain
6032 a modified form of STMT itself. */
6034 gimple_seq
6035 rewrite_to_defined_overflow (gimple stmt)
6037 if (dump_file && (dump_flags & TDF_DETAILS))
6039 fprintf (dump_file, "rewriting stmt with undefined signed "
6040 "overflow ");
6041 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
6044 tree lhs = gimple_assign_lhs (stmt);
6045 tree type = unsigned_type_for (TREE_TYPE (lhs));
6046 gimple_seq stmts = NULL;
6047 for (unsigned i = 1; i < gimple_num_ops (stmt); ++i)
6049 gimple_seq stmts2 = NULL;
6050 gimple_set_op (stmt, i,
6051 force_gimple_operand (fold_convert (type,
6052 gimple_op (stmt, i)),
6053 &stmts2, true, NULL_TREE));
6054 gimple_seq_add_seq (&stmts, stmts2);
6056 gimple_assign_set_lhs (stmt, make_ssa_name (type, stmt));
6057 if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
6058 gimple_assign_set_rhs_code (stmt, PLUS_EXPR);
6059 gimple_seq_add_stmt (&stmts, stmt);
6060 gimple cvt = gimple_build_assign (lhs, NOP_EXPR, gimple_assign_lhs (stmt));
6061 gimple_seq_add_stmt (&stmts, cvt);
6063 return stmts;
6067 /* The valueization hook we use for the gimple_build API simplification.
6068 This makes us match fold_buildN behavior by only combining with
6069 statements in the sequence(s) we are currently building. */
6071 static tree
6072 gimple_build_valueize (tree op)
6074 if (gimple_bb (SSA_NAME_DEF_STMT (op)) == NULL)
6075 return op;
6076 return NULL_TREE;
6079 /* Build the expression CODE OP0 of type TYPE with location LOC,
6080 simplifying it first if possible. Returns the built
6081 expression value and appends statements possibly defining it
6082 to SEQ. */
6084 tree
6085 gimple_build (gimple_seq *seq, location_t loc,
6086 enum tree_code code, tree type, tree op0)
6088 tree res = gimple_simplify (code, type, op0, seq, gimple_build_valueize);
6089 if (!res)
6091 if (gimple_in_ssa_p (cfun))
6092 res = make_ssa_name (type);
6093 else
6094 res = create_tmp_reg (type);
6095 gimple stmt;
6096 if (code == REALPART_EXPR
6097 || code == IMAGPART_EXPR
6098 || code == VIEW_CONVERT_EXPR)
6099 stmt = gimple_build_assign (res, code, build1 (code, type, op0));
6100 else
6101 stmt = gimple_build_assign (res, code, op0);
6102 gimple_set_location (stmt, loc);
6103 gimple_seq_add_stmt_without_update (seq, stmt);
6105 return res;
6108 /* Build the expression OP0 CODE OP1 of type TYPE with location LOC,
6109 simplifying it first if possible. Returns the built
6110 expression value and appends statements possibly defining it
6111 to SEQ. */
6113 tree
6114 gimple_build (gimple_seq *seq, location_t loc,
6115 enum tree_code code, tree type, tree op0, tree op1)
6117 tree res = gimple_simplify (code, type, op0, op1, seq, gimple_build_valueize);
6118 if (!res)
6120 if (gimple_in_ssa_p (cfun))
6121 res = make_ssa_name (type);
6122 else
6123 res = create_tmp_reg (type);
6124 gimple stmt = gimple_build_assign (res, code, op0, op1);
6125 gimple_set_location (stmt, loc);
6126 gimple_seq_add_stmt_without_update (seq, stmt);
6128 return res;
6131 /* Build the expression (CODE OP0 OP1 OP2) of type TYPE with location LOC,
6132 simplifying it first if possible. Returns the built
6133 expression value and appends statements possibly defining it
6134 to SEQ. */
6136 tree
6137 gimple_build (gimple_seq *seq, location_t loc,
6138 enum tree_code code, tree type, tree op0, tree op1, tree op2)
6140 tree res = gimple_simplify (code, type, op0, op1, op2,
6141 seq, gimple_build_valueize);
6142 if (!res)
6144 if (gimple_in_ssa_p (cfun))
6145 res = make_ssa_name (type);
6146 else
6147 res = create_tmp_reg (type);
6148 gimple stmt;
6149 if (code == BIT_FIELD_REF)
6150 stmt = gimple_build_assign (res, code,
6151 build3 (code, type, op0, op1, op2));
6152 else
6153 stmt = gimple_build_assign (res, code, op0, op1, op2);
6154 gimple_set_location (stmt, loc);
6155 gimple_seq_add_stmt_without_update (seq, stmt);
6157 return res;
6160 /* Build the call FN (ARG0) with a result of type TYPE
6161 (or no result if TYPE is void) with location LOC,
6162 simplifying it first if possible. Returns the built
6163 expression value (or NULL_TREE if TYPE is void) and appends
6164 statements possibly defining it to SEQ. */
6166 tree
6167 gimple_build (gimple_seq *seq, location_t loc,
6168 enum built_in_function fn, tree type, tree arg0)
6170 tree res = gimple_simplify (fn, type, arg0, seq, gimple_build_valueize);
6171 if (!res)
6173 tree decl = builtin_decl_implicit (fn);
6174 gimple stmt = gimple_build_call (decl, 1, arg0);
6175 if (!VOID_TYPE_P (type))
6177 if (gimple_in_ssa_p (cfun))
6178 res = make_ssa_name (type);
6179 else
6180 res = create_tmp_reg (type);
6181 gimple_call_set_lhs (stmt, res);
6183 gimple_set_location (stmt, loc);
6184 gimple_seq_add_stmt_without_update (seq, stmt);
6186 return res;
6189 /* Build the call FN (ARG0, ARG1) with a result of type TYPE
6190 (or no result if TYPE is void) with location LOC,
6191 simplifying it first if possible. Returns the built
6192 expression value (or NULL_TREE if TYPE is void) and appends
6193 statements possibly defining it to SEQ. */
6195 tree
6196 gimple_build (gimple_seq *seq, location_t loc,
6197 enum built_in_function fn, tree type, tree arg0, tree arg1)
6199 tree res = gimple_simplify (fn, type, arg0, arg1, seq, gimple_build_valueize);
6200 if (!res)
6202 tree decl = builtin_decl_implicit (fn);
6203 gimple stmt = gimple_build_call (decl, 2, arg0, arg1);
6204 if (!VOID_TYPE_P (type))
6206 if (gimple_in_ssa_p (cfun))
6207 res = make_ssa_name (type);
6208 else
6209 res = create_tmp_reg (type);
6210 gimple_call_set_lhs (stmt, res);
6212 gimple_set_location (stmt, loc);
6213 gimple_seq_add_stmt_without_update (seq, stmt);
6215 return res;
6218 /* Build the call FN (ARG0, ARG1, ARG2) with a result of type TYPE
6219 (or no result if TYPE is void) with location LOC,
6220 simplifying it first if possible. Returns the built
6221 expression value (or NULL_TREE if TYPE is void) and appends
6222 statements possibly defining it to SEQ. */
6224 tree
6225 gimple_build (gimple_seq *seq, location_t loc,
6226 enum built_in_function fn, tree type,
6227 tree arg0, tree arg1, tree arg2)
6229 tree res = gimple_simplify (fn, type, arg0, arg1, arg2,
6230 seq, gimple_build_valueize);
6231 if (!res)
6233 tree decl = builtin_decl_implicit (fn);
6234 gimple stmt = gimple_build_call (decl, 3, arg0, arg1, arg2);
6235 if (!VOID_TYPE_P (type))
6237 if (gimple_in_ssa_p (cfun))
6238 res = make_ssa_name (type);
6239 else
6240 res = create_tmp_reg (type);
6241 gimple_call_set_lhs (stmt, res);
6243 gimple_set_location (stmt, loc);
6244 gimple_seq_add_stmt_without_update (seq, stmt);
6246 return res;
6249 /* Build the conversion (TYPE) OP with a result of type TYPE
6250 with location LOC if such conversion is neccesary in GIMPLE,
6251 simplifying it first.
6252 Returns the built expression value and appends
6253 statements possibly defining it to SEQ. */
6255 tree
6256 gimple_convert (gimple_seq *seq, location_t loc, tree type, tree op)
6258 if (useless_type_conversion_p (type, TREE_TYPE (op)))
6259 return op;
6260 return gimple_build (seq, loc, NOP_EXPR, type, op);