2015-03-03 Andrew Sutton <andrew.n.sutton@gmail.com>
[official-gcc.git] / gcc / gimple-fold.c
blobaeb180370d8185903cca2676e046462de1eb37ad
1 /* Statement simplification on GIMPLE.
2 Copyright (C) 2010-2014 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 "tm.h"
25 #include "tree.h"
26 #include "stringpool.h"
27 #include "expr.h"
28 #include "stmt.h"
29 #include "stor-layout.h"
30 #include "flags.h"
31 #include "hashtab.h"
32 #include "hash-set.h"
33 #include "vec.h"
34 #include "machmode.h"
35 #include "hard-reg-set.h"
36 #include "input.h"
37 #include "function.h"
38 #include "dumpfile.h"
39 #include "bitmap.h"
40 #include "predict.h"
41 #include "dominance.h"
42 #include "basic-block.h"
43 #include "tree-ssa-alias.h"
44 #include "internal-fn.h"
45 #include "gimple-fold.h"
46 #include "gimple-expr.h"
47 #include "is-a.h"
48 #include "gimple.h"
49 #include "gimplify.h"
50 #include "gimple-iterator.h"
51 #include "gimple-ssa.h"
52 #include "tree-ssanames.h"
53 #include "tree-into-ssa.h"
54 #include "tree-dfa.h"
55 #include "tree-ssa.h"
56 #include "tree-ssa-propagate.h"
57 #include "target.h"
58 #include "hash-map.h"
59 #include "plugin-api.h"
60 #include "ipa-ref.h"
61 #include "cgraph.h"
62 #include "ipa-utils.h"
63 #include "gimple-pretty-print.h"
64 #include "tree-ssa-address.h"
65 #include "langhooks.h"
66 #include "gimplify-me.h"
67 #include "dbgcnt.h"
68 #include "builtins.h"
69 #include "output.h"
70 #include "tree-eh.h"
71 #include "gimple-match.h"
72 #include "tree-phinodes.h"
73 #include "ssa-iterators.h"
75 /* Return true when DECL can be referenced from current unit.
76 FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
77 We can get declarations that are not possible to reference for various
78 reasons:
80 1) When analyzing C++ virtual tables.
81 C++ virtual tables do have known constructors even
82 when they are keyed to other compilation unit.
83 Those tables can contain pointers to methods and vars
84 in other units. Those methods have both STATIC and EXTERNAL
85 set.
86 2) In WHOPR mode devirtualization might lead to reference
87 to method that was partitioned elsehwere.
88 In this case we have static VAR_DECL or FUNCTION_DECL
89 that has no corresponding callgraph/varpool node
90 declaring the body.
91 3) COMDAT functions referred by external vtables that
92 we devirtualize only during final compilation stage.
93 At this time we already decided that we will not output
94 the function body and thus we can't reference the symbol
95 directly. */
97 static bool
98 can_refer_decl_in_current_unit_p (tree decl, tree from_decl)
100 varpool_node *vnode;
101 struct cgraph_node *node;
102 symtab_node *snode;
104 if (DECL_ABSTRACT_P (decl))
105 return false;
107 /* We are concerned only about static/external vars and functions. */
108 if ((!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
109 || (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL))
110 return true;
112 /* Static objects can be referred only if they was not optimized out yet. */
113 if (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
115 /* Before we start optimizing unreachable code we can be sure all
116 static objects are defined. */
117 if (symtab->function_flags_ready)
118 return true;
119 snode = symtab_node::get (decl);
120 if (!snode || !snode->definition)
121 return false;
122 node = dyn_cast <cgraph_node *> (snode);
123 return !node || !node->global.inlined_to;
126 /* We will later output the initializer, so we can refer to it.
127 So we are concerned only when DECL comes from initializer of
128 external var or var that has been optimized out. */
129 if (!from_decl
130 || TREE_CODE (from_decl) != VAR_DECL
131 || (!DECL_EXTERNAL (from_decl)
132 && (vnode = varpool_node::get (from_decl)) != NULL
133 && vnode->definition)
134 || (flag_ltrans
135 && (vnode = varpool_node::get (from_decl)) != NULL
136 && vnode->in_other_partition))
137 return true;
138 /* We are folding reference from external vtable. The vtable may reffer
139 to a symbol keyed to other compilation unit. The other compilation
140 unit may be in separate DSO and the symbol may be hidden. */
141 if (DECL_VISIBILITY_SPECIFIED (decl)
142 && DECL_EXTERNAL (decl)
143 && DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT
144 && (!(snode = symtab_node::get (decl)) || !snode->in_other_partition))
145 return false;
146 /* When function is public, we always can introduce new reference.
147 Exception are the COMDAT functions where introducing a direct
148 reference imply need to include function body in the curren tunit. */
149 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
150 return true;
151 /* We have COMDAT. We are going to check if we still have definition
152 or if the definition is going to be output in other partition.
153 Bypass this when gimplifying; all needed functions will be produced.
155 As observed in PR20991 for already optimized out comdat virtual functions
156 it may be tempting to not necessarily give up because the copy will be
157 output elsewhere when corresponding vtable is output.
158 This is however not possible - ABI specify that COMDATs are output in
159 units where they are used and when the other unit was compiled with LTO
160 it is possible that vtable was kept public while the function itself
161 was privatized. */
162 if (!symtab->function_flags_ready)
163 return true;
165 snode = symtab_node::get (decl);
166 if (!snode
167 || ((!snode->definition || DECL_EXTERNAL (decl))
168 && (!snode->in_other_partition
169 || (!snode->forced_by_abi && !snode->force_output))))
170 return false;
171 node = dyn_cast <cgraph_node *> (snode);
172 return !node || !node->global.inlined_to;
175 /* CVAL is value taken from DECL_INITIAL of variable. Try to transform it into
176 acceptable form for is_gimple_min_invariant.
177 FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL. */
179 tree
180 canonicalize_constructor_val (tree cval, tree from_decl)
182 tree orig_cval = cval;
183 STRIP_NOPS (cval);
184 if (TREE_CODE (cval) == POINTER_PLUS_EXPR
185 && TREE_CODE (TREE_OPERAND (cval, 1)) == INTEGER_CST)
187 tree ptr = TREE_OPERAND (cval, 0);
188 if (is_gimple_min_invariant (ptr))
189 cval = build1_loc (EXPR_LOCATION (cval),
190 ADDR_EXPR, TREE_TYPE (ptr),
191 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (ptr)),
192 ptr,
193 fold_convert (ptr_type_node,
194 TREE_OPERAND (cval, 1))));
196 if (TREE_CODE (cval) == ADDR_EXPR)
198 tree base = NULL_TREE;
199 if (TREE_CODE (TREE_OPERAND (cval, 0)) == COMPOUND_LITERAL_EXPR)
201 base = COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval, 0));
202 if (base)
203 TREE_OPERAND (cval, 0) = base;
205 else
206 base = get_base_address (TREE_OPERAND (cval, 0));
207 if (!base)
208 return NULL_TREE;
210 if ((TREE_CODE (base) == VAR_DECL
211 || TREE_CODE (base) == FUNCTION_DECL)
212 && !can_refer_decl_in_current_unit_p (base, from_decl))
213 return NULL_TREE;
214 if (TREE_CODE (base) == VAR_DECL)
215 TREE_ADDRESSABLE (base) = 1;
216 else if (TREE_CODE (base) == FUNCTION_DECL)
218 /* Make sure we create a cgraph node for functions we'll reference.
219 They can be non-existent if the reference comes from an entry
220 of an external vtable for example. */
221 cgraph_node::get_create (base);
223 /* Fixup types in global initializers. */
224 if (TREE_TYPE (TREE_TYPE (cval)) != TREE_TYPE (TREE_OPERAND (cval, 0)))
225 cval = build_fold_addr_expr (TREE_OPERAND (cval, 0));
227 if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval)))
228 cval = fold_convert (TREE_TYPE (orig_cval), cval);
229 return cval;
231 if (TREE_OVERFLOW_P (cval))
232 return drop_tree_overflow (cval);
233 return orig_cval;
236 /* If SYM is a constant variable with known value, return the value.
237 NULL_TREE is returned otherwise. */
239 tree
240 get_symbol_constant_value (tree sym)
242 tree val = ctor_for_folding (sym);
243 if (val != error_mark_node)
245 if (val)
247 val = canonicalize_constructor_val (unshare_expr (val), sym);
248 if (val && is_gimple_min_invariant (val))
249 return val;
250 else
251 return NULL_TREE;
253 /* Variables declared 'const' without an initializer
254 have zero as the initializer if they may not be
255 overridden at link or run time. */
256 if (!val
257 && (INTEGRAL_TYPE_P (TREE_TYPE (sym))
258 || SCALAR_FLOAT_TYPE_P (TREE_TYPE (sym))))
259 return build_zero_cst (TREE_TYPE (sym));
262 return NULL_TREE;
267 /* Subroutine of fold_stmt. We perform several simplifications of the
268 memory reference tree EXPR and make sure to re-gimplify them properly
269 after propagation of constant addresses. IS_LHS is true if the
270 reference is supposed to be an lvalue. */
272 static tree
273 maybe_fold_reference (tree expr, bool is_lhs)
275 tree result;
277 if ((TREE_CODE (expr) == VIEW_CONVERT_EXPR
278 || TREE_CODE (expr) == REALPART_EXPR
279 || TREE_CODE (expr) == IMAGPART_EXPR)
280 && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
281 return fold_unary_loc (EXPR_LOCATION (expr),
282 TREE_CODE (expr),
283 TREE_TYPE (expr),
284 TREE_OPERAND (expr, 0));
285 else if (TREE_CODE (expr) == BIT_FIELD_REF
286 && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
287 return fold_ternary_loc (EXPR_LOCATION (expr),
288 TREE_CODE (expr),
289 TREE_TYPE (expr),
290 TREE_OPERAND (expr, 0),
291 TREE_OPERAND (expr, 1),
292 TREE_OPERAND (expr, 2));
294 if (!is_lhs
295 && (result = fold_const_aggregate_ref (expr))
296 && is_gimple_min_invariant (result))
297 return result;
299 return NULL_TREE;
303 /* Attempt to fold an assignment statement pointed-to by SI. Returns a
304 replacement rhs for the statement or NULL_TREE if no simplification
305 could be made. It is assumed that the operands have been previously
306 folded. */
308 static tree
309 fold_gimple_assign (gimple_stmt_iterator *si)
311 gimple stmt = gsi_stmt (*si);
312 enum tree_code subcode = gimple_assign_rhs_code (stmt);
313 location_t loc = gimple_location (stmt);
315 tree result = NULL_TREE;
317 switch (get_gimple_rhs_class (subcode))
319 case GIMPLE_SINGLE_RHS:
321 tree rhs = gimple_assign_rhs1 (stmt);
323 if (TREE_CLOBBER_P (rhs))
324 return NULL_TREE;
326 if (REFERENCE_CLASS_P (rhs))
327 return maybe_fold_reference (rhs, false);
329 else if (TREE_CODE (rhs) == OBJ_TYPE_REF)
331 tree val = OBJ_TYPE_REF_EXPR (rhs);
332 if (is_gimple_min_invariant (val))
333 return val;
334 else if (flag_devirtualize && virtual_method_call_p (rhs))
336 bool final;
337 vec <cgraph_node *>targets
338 = possible_polymorphic_call_targets (rhs, stmt, &final);
339 if (final && targets.length () <= 1 && dbg_cnt (devirt))
341 if (dump_enabled_p ())
343 location_t loc = gimple_location_safe (stmt);
344 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
345 "resolving virtual function address "
346 "reference to function %s\n",
347 targets.length () == 1
348 ? targets[0]->name ()
349 : "NULL");
351 if (targets.length () == 1)
353 val = fold_convert (TREE_TYPE (val),
354 build_fold_addr_expr_loc
355 (loc, targets[0]->decl));
356 STRIP_USELESS_TYPE_CONVERSION (val);
358 else
359 /* We can not use __builtin_unreachable here because it
360 can not have address taken. */
361 val = build_int_cst (TREE_TYPE (val), 0);
362 return val;
367 else if (TREE_CODE (rhs) == ADDR_EXPR)
369 tree ref = TREE_OPERAND (rhs, 0);
370 tree tem = maybe_fold_reference (ref, true);
371 if (tem
372 && TREE_CODE (tem) == MEM_REF
373 && integer_zerop (TREE_OPERAND (tem, 1)))
374 result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (tem, 0));
375 else if (tem)
376 result = fold_convert (TREE_TYPE (rhs),
377 build_fold_addr_expr_loc (loc, tem));
378 else if (TREE_CODE (ref) == MEM_REF
379 && integer_zerop (TREE_OPERAND (ref, 1)))
380 result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (ref, 0));
383 else if (TREE_CODE (rhs) == CONSTRUCTOR
384 && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE
385 && (CONSTRUCTOR_NELTS (rhs)
386 == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs))))
388 /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */
389 unsigned i;
390 tree val;
392 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
393 if (TREE_CODE (val) != INTEGER_CST
394 && TREE_CODE (val) != REAL_CST
395 && TREE_CODE (val) != FIXED_CST)
396 return NULL_TREE;
398 return build_vector_from_ctor (TREE_TYPE (rhs),
399 CONSTRUCTOR_ELTS (rhs));
402 else if (DECL_P (rhs))
403 return get_symbol_constant_value (rhs);
405 /* If we couldn't fold the RHS, hand over to the generic
406 fold routines. */
407 if (result == NULL_TREE)
408 result = fold (rhs);
410 /* Strip away useless type conversions. Both the NON_LVALUE_EXPR
411 that may have been added by fold, and "useless" type
412 conversions that might now be apparent due to propagation. */
413 STRIP_USELESS_TYPE_CONVERSION (result);
415 if (result != rhs && valid_gimple_rhs_p (result))
416 return result;
418 return NULL_TREE;
420 break;
422 case GIMPLE_UNARY_RHS:
423 break;
425 case GIMPLE_BINARY_RHS:
426 /* Try to canonicalize for boolean-typed X the comparisons
427 X == 0, X == 1, X != 0, and X != 1. */
428 if (gimple_assign_rhs_code (stmt) == EQ_EXPR
429 || gimple_assign_rhs_code (stmt) == NE_EXPR)
431 tree lhs = gimple_assign_lhs (stmt);
432 tree op1 = gimple_assign_rhs1 (stmt);
433 tree op2 = gimple_assign_rhs2 (stmt);
434 tree type = TREE_TYPE (op1);
436 /* Check whether the comparison operands are of the same boolean
437 type as the result type is.
438 Check that second operand is an integer-constant with value
439 one or zero. */
440 if (TREE_CODE (op2) == INTEGER_CST
441 && (integer_zerop (op2) || integer_onep (op2))
442 && useless_type_conversion_p (TREE_TYPE (lhs), type))
444 enum tree_code cmp_code = gimple_assign_rhs_code (stmt);
445 bool is_logical_not = false;
447 /* X == 0 and X != 1 is a logical-not.of X
448 X == 1 and X != 0 is X */
449 if ((cmp_code == EQ_EXPR && integer_zerop (op2))
450 || (cmp_code == NE_EXPR && integer_onep (op2)))
451 is_logical_not = true;
453 if (is_logical_not == false)
454 result = op1;
455 /* Only for one-bit precision typed X the transformation
456 !X -> ~X is valied. */
457 else if (TYPE_PRECISION (type) == 1)
458 result = build1_loc (gimple_location (stmt), BIT_NOT_EXPR,
459 type, op1);
460 /* Otherwise we use !X -> X ^ 1. */
461 else
462 result = build2_loc (gimple_location (stmt), BIT_XOR_EXPR,
463 type, op1, build_int_cst (type, 1));
468 if (!result)
469 result = fold_binary_loc (loc, subcode,
470 TREE_TYPE (gimple_assign_lhs (stmt)),
471 gimple_assign_rhs1 (stmt),
472 gimple_assign_rhs2 (stmt));
474 if (result)
476 STRIP_USELESS_TYPE_CONVERSION (result);
477 if (valid_gimple_rhs_p (result))
478 return result;
480 break;
482 case GIMPLE_TERNARY_RHS:
483 /* Try to fold a conditional expression. */
484 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
486 tree op0 = gimple_assign_rhs1 (stmt);
487 tree tem;
488 bool set = false;
489 location_t cond_loc = gimple_location (stmt);
491 if (COMPARISON_CLASS_P (op0))
493 fold_defer_overflow_warnings ();
494 tem = fold_binary_loc (cond_loc,
495 TREE_CODE (op0), TREE_TYPE (op0),
496 TREE_OPERAND (op0, 0),
497 TREE_OPERAND (op0, 1));
498 /* This is actually a conditional expression, not a GIMPLE
499 conditional statement, however, the valid_gimple_rhs_p
500 test still applies. */
501 set = (tem && is_gimple_condexpr (tem)
502 && valid_gimple_rhs_p (tem));
503 fold_undefer_overflow_warnings (set, stmt, 0);
505 else if (is_gimple_min_invariant (op0))
507 tem = op0;
508 set = true;
510 else
511 return NULL_TREE;
513 if (set)
514 result = fold_build3_loc (cond_loc, COND_EXPR,
515 TREE_TYPE (gimple_assign_lhs (stmt)), tem,
516 gimple_assign_rhs2 (stmt),
517 gimple_assign_rhs3 (stmt));
520 if (!result)
521 result = fold_ternary_loc (loc, subcode,
522 TREE_TYPE (gimple_assign_lhs (stmt)),
523 gimple_assign_rhs1 (stmt),
524 gimple_assign_rhs2 (stmt),
525 gimple_assign_rhs3 (stmt));
527 if (result)
529 STRIP_USELESS_TYPE_CONVERSION (result);
530 if (valid_gimple_rhs_p (result))
531 return result;
533 break;
535 case GIMPLE_INVALID_RHS:
536 gcc_unreachable ();
539 return NULL_TREE;
542 /* Attempt to fold a conditional statement. Return true if any changes were
543 made. We only attempt to fold the condition expression, and do not perform
544 any transformation that would require alteration of the cfg. It is
545 assumed that the operands have been previously folded. */
547 static bool
548 fold_gimple_cond (gimple stmt)
550 tree result = fold_binary_loc (gimple_location (stmt),
551 gimple_cond_code (stmt),
552 boolean_type_node,
553 gimple_cond_lhs (stmt),
554 gimple_cond_rhs (stmt));
556 if (result)
558 STRIP_USELESS_TYPE_CONVERSION (result);
559 if (is_gimple_condexpr (result) && valid_gimple_rhs_p (result))
561 gimple_cond_set_condition_from_tree (stmt, result);
562 return true;
566 return false;
570 /* Replace a statement at *SI_P with a sequence of statements in STMTS,
571 adjusting the replacement stmts location and virtual operands.
572 If the statement has a lhs the last stmt in the sequence is expected
573 to assign to that lhs. */
575 static void
576 gsi_replace_with_seq_vops (gimple_stmt_iterator *si_p, gimple_seq stmts)
578 gimple stmt = gsi_stmt (*si_p);
580 if (gimple_has_location (stmt))
581 annotate_all_with_location (stmts, gimple_location (stmt));
583 /* First iterate over the replacement statements backward, assigning
584 virtual operands to their defining statements. */
585 gimple laststore = NULL;
586 for (gimple_stmt_iterator i = gsi_last (stmts);
587 !gsi_end_p (i); gsi_prev (&i))
589 gimple new_stmt = gsi_stmt (i);
590 if ((gimple_assign_single_p (new_stmt)
591 && !is_gimple_reg (gimple_assign_lhs (new_stmt)))
592 || (is_gimple_call (new_stmt)
593 && (gimple_call_flags (new_stmt)
594 & (ECF_NOVOPS | ECF_PURE | ECF_CONST | ECF_NORETURN)) == 0))
596 tree vdef;
597 if (!laststore)
598 vdef = gimple_vdef (stmt);
599 else
600 vdef = make_ssa_name (gimple_vop (cfun), new_stmt);
601 gimple_set_vdef (new_stmt, vdef);
602 if (vdef && TREE_CODE (vdef) == SSA_NAME)
603 SSA_NAME_DEF_STMT (vdef) = new_stmt;
604 laststore = new_stmt;
608 /* Second iterate over the statements forward, assigning virtual
609 operands to their uses. */
610 tree reaching_vuse = gimple_vuse (stmt);
611 for (gimple_stmt_iterator i = gsi_start (stmts);
612 !gsi_end_p (i); gsi_next (&i))
614 gimple new_stmt = gsi_stmt (i);
615 /* If the new statement possibly has a VUSE, update it with exact SSA
616 name we know will reach this one. */
617 if (gimple_has_mem_ops (new_stmt))
618 gimple_set_vuse (new_stmt, reaching_vuse);
619 gimple_set_modified (new_stmt, true);
620 if (gimple_vdef (new_stmt))
621 reaching_vuse = gimple_vdef (new_stmt);
624 /* If the new sequence does not do a store release the virtual
625 definition of the original statement. */
626 if (reaching_vuse
627 && reaching_vuse == gimple_vuse (stmt))
629 tree vdef = gimple_vdef (stmt);
630 if (vdef
631 && TREE_CODE (vdef) == SSA_NAME)
633 unlink_stmt_vdef (stmt);
634 release_ssa_name (vdef);
638 /* Finally replace the original statement with the sequence. */
639 gsi_replace_with_seq (si_p, stmts, false);
642 /* Convert EXPR into a GIMPLE value suitable for substitution on the
643 RHS of an assignment. Insert the necessary statements before
644 iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL
645 is replaced. If the call is expected to produces a result, then it
646 is replaced by an assignment of the new RHS to the result variable.
647 If the result is to be ignored, then the call is replaced by a
648 GIMPLE_NOP. A proper VDEF chain is retained by making the first
649 VUSE and the last VDEF of the whole sequence be the same as the replaced
650 statement and using new SSA names for stores in between. */
652 void
653 gimplify_and_update_call_from_tree (gimple_stmt_iterator *si_p, tree expr)
655 tree lhs;
656 gimple stmt, new_stmt;
657 gimple_stmt_iterator i;
658 gimple_seq stmts = NULL;
660 stmt = gsi_stmt (*si_p);
662 gcc_assert (is_gimple_call (stmt));
664 push_gimplify_context (gimple_in_ssa_p (cfun));
666 lhs = gimple_call_lhs (stmt);
667 if (lhs == NULL_TREE)
669 gimplify_and_add (expr, &stmts);
670 /* We can end up with folding a memcpy of an empty class assignment
671 which gets optimized away by C++ gimplification. */
672 if (gimple_seq_empty_p (stmts))
674 pop_gimplify_context (NULL);
675 if (gimple_in_ssa_p (cfun))
677 unlink_stmt_vdef (stmt);
678 release_defs (stmt);
680 gsi_replace (si_p, gimple_build_nop (), true);
681 return;
684 else
686 tree tmp = get_initialized_tmp_var (expr, &stmts, NULL);
687 new_stmt = gimple_build_assign (lhs, tmp);
688 i = gsi_last (stmts);
689 gsi_insert_after_without_update (&i, new_stmt,
690 GSI_CONTINUE_LINKING);
693 pop_gimplify_context (NULL);
695 gsi_replace_with_seq_vops (si_p, stmts);
699 /* Replace the call at *GSI with the gimple value VAL. */
701 static void
702 replace_call_with_value (gimple_stmt_iterator *gsi, tree val)
704 gimple stmt = gsi_stmt (*gsi);
705 tree lhs = gimple_call_lhs (stmt);
706 gimple repl;
707 if (lhs)
709 if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (val)))
710 val = fold_convert (TREE_TYPE (lhs), val);
711 repl = gimple_build_assign (lhs, val);
713 else
714 repl = gimple_build_nop ();
715 tree vdef = gimple_vdef (stmt);
716 if (vdef && TREE_CODE (vdef) == SSA_NAME)
718 unlink_stmt_vdef (stmt);
719 release_ssa_name (vdef);
721 gsi_replace (gsi, repl, true);
724 /* Replace the call at *GSI with the new call REPL and fold that
725 again. */
727 static void
728 replace_call_with_call_and_fold (gimple_stmt_iterator *gsi, gimple repl)
730 gimple stmt = gsi_stmt (*gsi);
731 gimple_call_set_lhs (repl, gimple_call_lhs (stmt));
732 gimple_set_location (repl, gimple_location (stmt));
733 if (gimple_vdef (stmt)
734 && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
736 gimple_set_vdef (repl, gimple_vdef (stmt));
737 gimple_set_vuse (repl, gimple_vuse (stmt));
738 SSA_NAME_DEF_STMT (gimple_vdef (repl)) = repl;
740 gsi_replace (gsi, repl, true);
741 fold_stmt (gsi);
744 /* Return true if VAR is a VAR_DECL or a component thereof. */
746 static bool
747 var_decl_component_p (tree var)
749 tree inner = var;
750 while (handled_component_p (inner))
751 inner = TREE_OPERAND (inner, 0);
752 return SSA_VAR_P (inner);
755 /* Fold function call to builtin mem{{,p}cpy,move}. Return
756 NULL_TREE if no simplification can be made.
757 If ENDP is 0, return DEST (like memcpy).
758 If ENDP is 1, return DEST+LEN (like mempcpy).
759 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
760 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
761 (memmove). */
763 static bool
764 gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi,
765 tree dest, tree src, int endp)
767 gimple stmt = gsi_stmt (*gsi);
768 tree lhs = gimple_call_lhs (stmt);
769 tree len = gimple_call_arg (stmt, 2);
770 tree destvar, srcvar;
771 location_t loc = gimple_location (stmt);
773 /* If the LEN parameter is zero, return DEST. */
774 if (integer_zerop (len))
776 gimple repl;
777 if (gimple_call_lhs (stmt))
778 repl = gimple_build_assign (gimple_call_lhs (stmt), dest);
779 else
780 repl = gimple_build_nop ();
781 tree vdef = gimple_vdef (stmt);
782 if (vdef && TREE_CODE (vdef) == SSA_NAME)
784 unlink_stmt_vdef (stmt);
785 release_ssa_name (vdef);
787 gsi_replace (gsi, repl, true);
788 return true;
791 /* If SRC and DEST are the same (and not volatile), return
792 DEST{,+LEN,+LEN-1}. */
793 if (operand_equal_p (src, dest, 0))
795 unlink_stmt_vdef (stmt);
796 if (gimple_vdef (stmt) && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
797 release_ssa_name (gimple_vdef (stmt));
798 if (!lhs)
800 gsi_replace (gsi, gimple_build_nop (), true);
801 return true;
803 goto done;
805 else
807 tree srctype, desttype;
808 unsigned int src_align, dest_align;
809 tree off0;
811 /* Build accesses at offset zero with a ref-all character type. */
812 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
813 ptr_mode, true), 0);
815 /* If we can perform the copy efficiently with first doing all loads
816 and then all stores inline it that way. Currently efficiently
817 means that we can load all the memory into a single integer
818 register which is what MOVE_MAX gives us. */
819 src_align = get_pointer_alignment (src);
820 dest_align = get_pointer_alignment (dest);
821 if (tree_fits_uhwi_p (len)
822 && compare_tree_int (len, MOVE_MAX) <= 0
823 /* ??? Don't transform copies from strings with known length this
824 confuses the tree-ssa-strlen.c. This doesn't handle
825 the case in gcc.dg/strlenopt-8.c which is XFAILed for that
826 reason. */
827 && !c_strlen (src, 2))
829 unsigned ilen = tree_to_uhwi (len);
830 if (exact_log2 (ilen) != -1)
832 tree type = lang_hooks.types.type_for_size (ilen * 8, 1);
833 if (type
834 && TYPE_MODE (type) != BLKmode
835 && (GET_MODE_SIZE (TYPE_MODE (type)) * BITS_PER_UNIT
836 == ilen * 8)
837 /* If the destination pointer is not aligned we must be able
838 to emit an unaligned store. */
839 && (dest_align >= GET_MODE_ALIGNMENT (TYPE_MODE (type))
840 || !SLOW_UNALIGNED_ACCESS (TYPE_MODE (type), dest_align)))
842 tree srctype = type;
843 tree desttype = type;
844 if (src_align < GET_MODE_ALIGNMENT (TYPE_MODE (type)))
845 srctype = build_aligned_type (type, src_align);
846 tree srcmem = fold_build2 (MEM_REF, srctype, src, off0);
847 tree tem = fold_const_aggregate_ref (srcmem);
848 if (tem)
849 srcmem = tem;
850 else if (src_align < GET_MODE_ALIGNMENT (TYPE_MODE (type))
851 && SLOW_UNALIGNED_ACCESS (TYPE_MODE (type),
852 src_align))
853 srcmem = NULL_TREE;
854 if (srcmem)
856 gimple new_stmt;
857 if (is_gimple_reg_type (TREE_TYPE (srcmem)))
859 new_stmt = gimple_build_assign (NULL_TREE, srcmem);
860 if (gimple_in_ssa_p (cfun))
861 srcmem = make_ssa_name (TREE_TYPE (srcmem),
862 new_stmt);
863 else
864 srcmem = create_tmp_reg (TREE_TYPE (srcmem),
865 NULL);
866 gimple_assign_set_lhs (new_stmt, srcmem);
867 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
868 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
870 if (dest_align < GET_MODE_ALIGNMENT (TYPE_MODE (type)))
871 desttype = build_aligned_type (type, dest_align);
872 new_stmt
873 = gimple_build_assign (fold_build2 (MEM_REF, desttype,
874 dest, off0),
875 srcmem);
876 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
877 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
878 if (gimple_vdef (new_stmt)
879 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
880 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
881 if (!lhs)
883 gsi_replace (gsi, new_stmt, true);
884 return true;
886 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
887 goto done;
893 if (endp == 3)
895 /* Both DEST and SRC must be pointer types.
896 ??? This is what old code did. Is the testing for pointer types
897 really mandatory?
899 If either SRC is readonly or length is 1, we can use memcpy. */
900 if (!dest_align || !src_align)
901 return false;
902 if (readonly_data_expr (src)
903 || (tree_fits_uhwi_p (len)
904 && (MIN (src_align, dest_align) / BITS_PER_UNIT
905 >= tree_to_uhwi (len))))
907 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
908 if (!fn)
909 return false;
910 gimple_call_set_fndecl (stmt, fn);
911 gimple_call_set_arg (stmt, 0, dest);
912 gimple_call_set_arg (stmt, 1, src);
913 fold_stmt (gsi);
914 return true;
917 /* If *src and *dest can't overlap, optimize into memcpy as well. */
918 if (TREE_CODE (src) == ADDR_EXPR
919 && TREE_CODE (dest) == ADDR_EXPR)
921 tree src_base, dest_base, fn;
922 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
923 HOST_WIDE_INT size = -1;
924 HOST_WIDE_INT maxsize = -1;
926 srcvar = TREE_OPERAND (src, 0);
927 src_base = get_ref_base_and_extent (srcvar, &src_offset,
928 &size, &maxsize);
929 destvar = TREE_OPERAND (dest, 0);
930 dest_base = get_ref_base_and_extent (destvar, &dest_offset,
931 &size, &maxsize);
932 if (tree_fits_uhwi_p (len))
933 maxsize = tree_to_uhwi (len);
934 else
935 maxsize = -1;
936 src_offset /= BITS_PER_UNIT;
937 dest_offset /= BITS_PER_UNIT;
938 if (SSA_VAR_P (src_base)
939 && SSA_VAR_P (dest_base))
941 if (operand_equal_p (src_base, dest_base, 0)
942 && ranges_overlap_p (src_offset, maxsize,
943 dest_offset, maxsize))
944 return false;
946 else if (TREE_CODE (src_base) == MEM_REF
947 && TREE_CODE (dest_base) == MEM_REF)
949 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
950 TREE_OPERAND (dest_base, 0), 0))
951 return false;
952 offset_int off = mem_ref_offset (src_base) + src_offset;
953 if (!wi::fits_shwi_p (off))
954 return false;
955 src_offset = off.to_shwi ();
957 off = mem_ref_offset (dest_base) + dest_offset;
958 if (!wi::fits_shwi_p (off))
959 return false;
960 dest_offset = off.to_shwi ();
961 if (ranges_overlap_p (src_offset, maxsize,
962 dest_offset, maxsize))
963 return false;
965 else
966 return false;
968 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
969 if (!fn)
970 return false;
971 gimple_call_set_fndecl (stmt, fn);
972 gimple_call_set_arg (stmt, 0, dest);
973 gimple_call_set_arg (stmt, 1, src);
974 fold_stmt (gsi);
975 return true;
978 /* If the destination and source do not alias optimize into
979 memcpy as well. */
980 if ((is_gimple_min_invariant (dest)
981 || TREE_CODE (dest) == SSA_NAME)
982 && (is_gimple_min_invariant (src)
983 || TREE_CODE (src) == SSA_NAME))
985 ao_ref destr, srcr;
986 ao_ref_init_from_ptr_and_size (&destr, dest, len);
987 ao_ref_init_from_ptr_and_size (&srcr, src, len);
988 if (!refs_may_alias_p_1 (&destr, &srcr, false))
990 tree fn;
991 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
992 if (!fn)
993 return false;
994 gimple_call_set_fndecl (stmt, fn);
995 gimple_call_set_arg (stmt, 0, dest);
996 gimple_call_set_arg (stmt, 1, src);
997 fold_stmt (gsi);
998 return true;
1002 return false;
1005 if (!tree_fits_shwi_p (len))
1006 return false;
1007 /* FIXME:
1008 This logic lose for arguments like (type *)malloc (sizeof (type)),
1009 since we strip the casts of up to VOID return value from malloc.
1010 Perhaps we ought to inherit type from non-VOID argument here? */
1011 STRIP_NOPS (src);
1012 STRIP_NOPS (dest);
1013 if (!POINTER_TYPE_P (TREE_TYPE (src))
1014 || !POINTER_TYPE_P (TREE_TYPE (dest)))
1015 return false;
1016 /* In the following try to find a type that is most natural to be
1017 used for the memcpy source and destination and that allows
1018 the most optimization when memcpy is turned into a plain assignment
1019 using that type. In theory we could always use a char[len] type
1020 but that only gains us that the destination and source possibly
1021 no longer will have their address taken. */
1022 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
1023 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
1025 tree tem = TREE_OPERAND (src, 0);
1026 STRIP_NOPS (tem);
1027 if (tem != TREE_OPERAND (src, 0))
1028 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
1030 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
1032 tree tem = TREE_OPERAND (dest, 0);
1033 STRIP_NOPS (tem);
1034 if (tem != TREE_OPERAND (dest, 0))
1035 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
1037 srctype = TREE_TYPE (TREE_TYPE (src));
1038 if (TREE_CODE (srctype) == ARRAY_TYPE
1039 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
1041 srctype = TREE_TYPE (srctype);
1042 STRIP_NOPS (src);
1043 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
1045 desttype = TREE_TYPE (TREE_TYPE (dest));
1046 if (TREE_CODE (desttype) == ARRAY_TYPE
1047 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
1049 desttype = TREE_TYPE (desttype);
1050 STRIP_NOPS (dest);
1051 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
1053 if (TREE_ADDRESSABLE (srctype)
1054 || TREE_ADDRESSABLE (desttype))
1055 return false;
1057 /* Make sure we are not copying using a floating-point mode or
1058 a type whose size possibly does not match its precision. */
1059 if (FLOAT_MODE_P (TYPE_MODE (desttype))
1060 || TREE_CODE (desttype) == BOOLEAN_TYPE
1061 || TREE_CODE (desttype) == ENUMERAL_TYPE)
1062 desttype = bitwise_type_for_mode (TYPE_MODE (desttype));
1063 if (FLOAT_MODE_P (TYPE_MODE (srctype))
1064 || TREE_CODE (srctype) == BOOLEAN_TYPE
1065 || TREE_CODE (srctype) == ENUMERAL_TYPE)
1066 srctype = bitwise_type_for_mode (TYPE_MODE (srctype));
1067 if (!srctype)
1068 srctype = desttype;
1069 if (!desttype)
1070 desttype = srctype;
1071 if (!srctype)
1072 return false;
1074 src_align = get_pointer_alignment (src);
1075 dest_align = get_pointer_alignment (dest);
1076 if (dest_align < TYPE_ALIGN (desttype)
1077 || src_align < TYPE_ALIGN (srctype))
1078 return false;
1080 destvar = dest;
1081 STRIP_NOPS (destvar);
1082 if (TREE_CODE (destvar) == ADDR_EXPR
1083 && var_decl_component_p (TREE_OPERAND (destvar, 0))
1084 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
1085 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
1086 else
1087 destvar = NULL_TREE;
1089 srcvar = src;
1090 STRIP_NOPS (srcvar);
1091 if (TREE_CODE (srcvar) == ADDR_EXPR
1092 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
1093 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
1095 if (!destvar
1096 || src_align >= TYPE_ALIGN (desttype))
1097 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
1098 srcvar, off0);
1099 else if (!STRICT_ALIGNMENT)
1101 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
1102 src_align);
1103 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
1105 else
1106 srcvar = NULL_TREE;
1108 else
1109 srcvar = NULL_TREE;
1111 if (srcvar == NULL_TREE && destvar == NULL_TREE)
1112 return false;
1114 if (srcvar == NULL_TREE)
1116 STRIP_NOPS (src);
1117 if (src_align >= TYPE_ALIGN (desttype))
1118 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
1119 else
1121 if (STRICT_ALIGNMENT)
1122 return false;
1123 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
1124 src_align);
1125 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
1128 else if (destvar == NULL_TREE)
1130 STRIP_NOPS (dest);
1131 if (dest_align >= TYPE_ALIGN (srctype))
1132 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
1133 else
1135 if (STRICT_ALIGNMENT)
1136 return false;
1137 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
1138 dest_align);
1139 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
1143 gimple new_stmt;
1144 if (is_gimple_reg_type (TREE_TYPE (srcvar)))
1146 new_stmt = gimple_build_assign (NULL_TREE, srcvar);
1147 if (gimple_in_ssa_p (cfun))
1148 srcvar = make_ssa_name (TREE_TYPE (srcvar), new_stmt);
1149 else
1150 srcvar = create_tmp_reg (TREE_TYPE (srcvar), NULL);
1151 gimple_assign_set_lhs (new_stmt, srcvar);
1152 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
1153 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1155 new_stmt = gimple_build_assign (destvar, srcvar);
1156 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
1157 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
1158 if (gimple_vdef (new_stmt)
1159 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1160 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1161 if (!lhs)
1163 gsi_replace (gsi, new_stmt, true);
1164 return true;
1166 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1169 done:
1170 if (endp == 0 || endp == 3)
1171 len = NULL_TREE;
1172 else if (endp == 2)
1173 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
1174 ssize_int (1));
1175 if (endp == 2 || endp == 1)
1176 dest = fold_build_pointer_plus_loc (loc, dest, len);
1178 dest = force_gimple_operand_gsi (gsi, dest, false, NULL_TREE, true,
1179 GSI_SAME_STMT);
1180 gimple repl = gimple_build_assign (lhs, dest);
1181 gsi_replace (gsi, repl, true);
1182 return true;
1185 /* Fold function call to builtin memset or bzero at *GSI setting the
1186 memory of size LEN to VAL. Return whether a simplification was made. */
1188 static bool
1189 gimple_fold_builtin_memset (gimple_stmt_iterator *gsi, tree c, tree len)
1191 gimple stmt = gsi_stmt (*gsi);
1192 tree etype;
1193 unsigned HOST_WIDE_INT length, cval;
1195 /* If the LEN parameter is zero, return DEST. */
1196 if (integer_zerop (len))
1198 replace_call_with_value (gsi, gimple_call_arg (stmt, 0));
1199 return true;
1202 if (! tree_fits_uhwi_p (len))
1203 return false;
1205 if (TREE_CODE (c) != INTEGER_CST)
1206 return false;
1208 tree dest = gimple_call_arg (stmt, 0);
1209 tree var = dest;
1210 if (TREE_CODE (var) != ADDR_EXPR)
1211 return false;
1213 var = TREE_OPERAND (var, 0);
1214 if (TREE_THIS_VOLATILE (var))
1215 return false;
1217 etype = TREE_TYPE (var);
1218 if (TREE_CODE (etype) == ARRAY_TYPE)
1219 etype = TREE_TYPE (etype);
1221 if (!INTEGRAL_TYPE_P (etype)
1222 && !POINTER_TYPE_P (etype))
1223 return NULL_TREE;
1225 if (! var_decl_component_p (var))
1226 return NULL_TREE;
1228 length = tree_to_uhwi (len);
1229 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
1230 || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
1231 return NULL_TREE;
1233 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
1234 return NULL_TREE;
1236 if (integer_zerop (c))
1237 cval = 0;
1238 else
1240 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
1241 return NULL_TREE;
1243 cval = TREE_INT_CST_LOW (c);
1244 cval &= 0xff;
1245 cval |= cval << 8;
1246 cval |= cval << 16;
1247 cval |= (cval << 31) << 1;
1250 var = fold_build2 (MEM_REF, etype, dest, build_int_cst (ptr_type_node, 0));
1251 gimple store = gimple_build_assign (var, build_int_cst_type (etype, cval));
1252 gimple_set_vuse (store, gimple_vuse (stmt));
1253 tree vdef = gimple_vdef (stmt);
1254 if (vdef && TREE_CODE (vdef) == SSA_NAME)
1256 gimple_set_vdef (store, gimple_vdef (stmt));
1257 SSA_NAME_DEF_STMT (gimple_vdef (stmt)) = store;
1259 gsi_insert_before (gsi, store, GSI_SAME_STMT);
1260 if (gimple_call_lhs (stmt))
1262 gimple asgn = gimple_build_assign (gimple_call_lhs (stmt), dest);
1263 gsi_replace (gsi, asgn, true);
1265 else
1267 gimple_stmt_iterator gsi2 = *gsi;
1268 gsi_prev (gsi);
1269 gsi_remove (&gsi2, true);
1272 return true;
1276 /* Return the string length, maximum string length or maximum value of
1277 ARG in LENGTH.
1278 If ARG is an SSA name variable, follow its use-def chains. If LENGTH
1279 is not NULL and, for TYPE == 0, its value is not equal to the length
1280 we determine or if we are unable to determine the length or value,
1281 return false. VISITED is a bitmap of visited variables.
1282 TYPE is 0 if string length should be returned, 1 for maximum string
1283 length and 2 for maximum value ARG can have. */
1285 static bool
1286 get_maxval_strlen (tree arg, tree *length, bitmap *visited, int type)
1288 tree var, val;
1289 gimple def_stmt;
1291 if (TREE_CODE (arg) != SSA_NAME)
1293 /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */
1294 if (TREE_CODE (arg) == ADDR_EXPR
1295 && TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF
1296 && integer_zerop (TREE_OPERAND (TREE_OPERAND (arg, 0), 1)))
1298 tree aop0 = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
1299 if (TREE_CODE (aop0) == INDIRECT_REF
1300 && TREE_CODE (TREE_OPERAND (aop0, 0)) == SSA_NAME)
1301 return get_maxval_strlen (TREE_OPERAND (aop0, 0),
1302 length, visited, type);
1305 if (type == 2)
1307 val = arg;
1308 if (TREE_CODE (val) != INTEGER_CST
1309 || tree_int_cst_sgn (val) < 0)
1310 return false;
1312 else
1313 val = c_strlen (arg, 1);
1314 if (!val)
1315 return false;
1317 if (*length)
1319 if (type > 0)
1321 if (TREE_CODE (*length) != INTEGER_CST
1322 || TREE_CODE (val) != INTEGER_CST)
1323 return false;
1325 if (tree_int_cst_lt (*length, val))
1326 *length = val;
1327 return true;
1329 else if (simple_cst_equal (val, *length) != 1)
1330 return false;
1333 *length = val;
1334 return true;
1337 /* If ARG is registered for SSA update we cannot look at its defining
1338 statement. */
1339 if (name_registered_for_update_p (arg))
1340 return false;
1342 /* If we were already here, break the infinite cycle. */
1343 if (!*visited)
1344 *visited = BITMAP_ALLOC (NULL);
1345 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (arg)))
1346 return true;
1348 var = arg;
1349 def_stmt = SSA_NAME_DEF_STMT (var);
1351 switch (gimple_code (def_stmt))
1353 case GIMPLE_ASSIGN:
1354 /* The RHS of the statement defining VAR must either have a
1355 constant length or come from another SSA_NAME with a constant
1356 length. */
1357 if (gimple_assign_single_p (def_stmt)
1358 || gimple_assign_unary_nop_p (def_stmt))
1360 tree rhs = gimple_assign_rhs1 (def_stmt);
1361 return get_maxval_strlen (rhs, length, visited, type);
1363 else if (gimple_assign_rhs_code (def_stmt) == COND_EXPR)
1365 tree op2 = gimple_assign_rhs2 (def_stmt);
1366 tree op3 = gimple_assign_rhs3 (def_stmt);
1367 return get_maxval_strlen (op2, length, visited, type)
1368 && get_maxval_strlen (op3, length, visited, type);
1370 return false;
1372 case GIMPLE_PHI:
1374 /* All the arguments of the PHI node must have the same constant
1375 length. */
1376 unsigned i;
1378 for (i = 0; i < gimple_phi_num_args (def_stmt); i++)
1380 tree arg = gimple_phi_arg (def_stmt, i)->def;
1382 /* If this PHI has itself as an argument, we cannot
1383 determine the string length of this argument. However,
1384 if we can find a constant string length for the other
1385 PHI args then we can still be sure that this is a
1386 constant string length. So be optimistic and just
1387 continue with the next argument. */
1388 if (arg == gimple_phi_result (def_stmt))
1389 continue;
1391 if (!get_maxval_strlen (arg, length, visited, type))
1392 return false;
1395 return true;
1397 default:
1398 return false;
1402 tree
1403 get_maxval_strlen (tree arg, int type)
1405 bitmap visited = NULL;
1406 tree len = NULL_TREE;
1407 if (!get_maxval_strlen (arg, &len, &visited, type))
1408 len = NULL_TREE;
1409 if (visited)
1410 BITMAP_FREE (visited);
1412 return len;
1416 /* Fold function call to builtin strcpy with arguments DEST and SRC.
1417 If LEN is not NULL, it represents the length of the string to be
1418 copied. Return NULL_TREE if no simplification can be made. */
1420 static bool
1421 gimple_fold_builtin_strcpy (gimple_stmt_iterator *gsi,
1422 tree dest, tree src)
1424 location_t loc = gimple_location (gsi_stmt (*gsi));
1425 tree fn;
1427 /* If SRC and DEST are the same (and not volatile), return DEST. */
1428 if (operand_equal_p (src, dest, 0))
1430 replace_call_with_value (gsi, dest);
1431 return true;
1434 if (optimize_function_for_size_p (cfun))
1435 return false;
1437 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1438 if (!fn)
1439 return false;
1441 tree len = get_maxval_strlen (src, 0);
1442 if (!len)
1443 return false;
1445 len = fold_convert_loc (loc, size_type_node, len);
1446 len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
1447 len = force_gimple_operand_gsi (gsi, len, true,
1448 NULL_TREE, true, GSI_SAME_STMT);
1449 gimple repl = gimple_build_call (fn, 3, dest, src, len);
1450 replace_call_with_call_and_fold (gsi, repl);
1451 return true;
1454 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
1455 If SLEN is not NULL, it represents the length of the source string.
1456 Return NULL_TREE if no simplification can be made. */
1458 static bool
1459 gimple_fold_builtin_strncpy (gimple_stmt_iterator *gsi,
1460 tree dest, tree src, tree len)
1462 location_t loc = gimple_location (gsi_stmt (*gsi));
1463 tree fn;
1465 /* If the LEN parameter is zero, return DEST. */
1466 if (integer_zerop (len))
1468 replace_call_with_value (gsi, dest);
1469 return true;
1472 /* We can't compare slen with len as constants below if len is not a
1473 constant. */
1474 if (TREE_CODE (len) != INTEGER_CST)
1475 return false;
1477 /* Now, we must be passed a constant src ptr parameter. */
1478 tree slen = get_maxval_strlen (src, 0);
1479 if (!slen || TREE_CODE (slen) != INTEGER_CST)
1480 return false;
1482 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
1484 /* We do not support simplification of this case, though we do
1485 support it when expanding trees into RTL. */
1486 /* FIXME: generate a call to __builtin_memset. */
1487 if (tree_int_cst_lt (slen, len))
1488 return false;
1490 /* OK transform into builtin memcpy. */
1491 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1492 if (!fn)
1493 return false;
1495 len = fold_convert_loc (loc, size_type_node, len);
1496 len = force_gimple_operand_gsi (gsi, len, true,
1497 NULL_TREE, true, GSI_SAME_STMT);
1498 gimple repl = gimple_build_call (fn, 3, dest, src, len);
1499 replace_call_with_call_and_fold (gsi, repl);
1500 return true;
1503 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
1504 to the call.
1506 Return NULL_TREE if no simplification was possible, otherwise return the
1507 simplified form of the call as a tree.
1509 The simplified form may be a constant or other expression which
1510 computes the same value, but in a more efficient manner (including
1511 calls to other builtin functions).
1513 The call may contain arguments which need to be evaluated, but
1514 which are not useful to determine the result of the call. In
1515 this case we return a chain of COMPOUND_EXPRs. The LHS of each
1516 COMPOUND_EXPR will be an argument which must be evaluated.
1517 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
1518 COMPOUND_EXPR in the chain will contain the tree for the simplified
1519 form of the builtin function call. */
1521 static bool
1522 gimple_fold_builtin_strcat (gimple_stmt_iterator *gsi, tree dst, tree src)
1524 gimple stmt = gsi_stmt (*gsi);
1525 location_t loc = gimple_location (stmt);
1527 const char *p = c_getstr (src);
1529 /* If the string length is zero, return the dst parameter. */
1530 if (p && *p == '\0')
1532 replace_call_with_value (gsi, dst);
1533 return true;
1536 if (!optimize_bb_for_speed_p (gimple_bb (stmt)))
1537 return false;
1539 /* See if we can store by pieces into (dst + strlen(dst)). */
1540 tree newdst;
1541 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
1542 tree memcpy_fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1544 if (!strlen_fn || !memcpy_fn)
1545 return false;
1547 /* If the length of the source string isn't computable don't
1548 split strcat into strlen and memcpy. */
1549 tree len = get_maxval_strlen (src, 0);
1550 if (! len)
1551 return false;
1553 /* Create strlen (dst). */
1554 gimple_seq stmts = NULL, stmts2;
1555 gimple repl = gimple_build_call (strlen_fn, 1, dst);
1556 gimple_set_location (repl, loc);
1557 if (gimple_in_ssa_p (cfun))
1558 newdst = make_ssa_name (size_type_node, NULL);
1559 else
1560 newdst = create_tmp_reg (size_type_node, NULL);
1561 gimple_call_set_lhs (repl, newdst);
1562 gimple_seq_add_stmt_without_update (&stmts, repl);
1564 /* Create (dst p+ strlen (dst)). */
1565 newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
1566 newdst = force_gimple_operand (newdst, &stmts2, true, NULL_TREE);
1567 gimple_seq_add_seq_without_update (&stmts, stmts2);
1569 len = fold_convert_loc (loc, size_type_node, len);
1570 len = size_binop_loc (loc, PLUS_EXPR, len,
1571 build_int_cst (size_type_node, 1));
1572 len = force_gimple_operand (len, &stmts2, true, NULL_TREE);
1573 gimple_seq_add_seq_without_update (&stmts, stmts2);
1575 repl = gimple_build_call (memcpy_fn, 3, newdst, src, len);
1576 gimple_seq_add_stmt_without_update (&stmts, repl);
1577 if (gimple_call_lhs (stmt))
1579 repl = gimple_build_assign (gimple_call_lhs (stmt), dst);
1580 gimple_seq_add_stmt_without_update (&stmts, repl);
1581 gsi_replace_with_seq_vops (gsi, stmts);
1582 /* gsi now points at the assignment to the lhs, get a
1583 stmt iterator to the memcpy call.
1584 ??? We can't use gsi_for_stmt as that doesn't work when the
1585 CFG isn't built yet. */
1586 gimple_stmt_iterator gsi2 = *gsi;
1587 gsi_prev (&gsi2);
1588 fold_stmt (&gsi2);
1590 else
1592 gsi_replace_with_seq_vops (gsi, stmts);
1593 fold_stmt (gsi);
1595 return true;
1598 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
1599 are the arguments to the call. */
1601 static bool
1602 gimple_fold_builtin_strcat_chk (gimple_stmt_iterator *gsi)
1604 gimple stmt = gsi_stmt (*gsi);
1605 tree dest = gimple_call_arg (stmt, 0);
1606 tree src = gimple_call_arg (stmt, 1);
1607 tree size = gimple_call_arg (stmt, 2);
1608 tree fn;
1609 const char *p;
1612 p = c_getstr (src);
1613 /* If the SRC parameter is "", return DEST. */
1614 if (p && *p == '\0')
1616 replace_call_with_value (gsi, dest);
1617 return true;
1620 if (! tree_fits_uhwi_p (size) || ! integer_all_onesp (size))
1621 return false;
1623 /* If __builtin_strcat_chk is used, assume strcat is available. */
1624 fn = builtin_decl_explicit (BUILT_IN_STRCAT);
1625 if (!fn)
1626 return false;
1628 gimple repl = gimple_build_call (fn, 2, dest, src);
1629 replace_call_with_call_and_fold (gsi, repl);
1630 return true;
1633 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
1634 LEN, and SIZE. */
1636 static bool
1637 gimple_fold_builtin_strncat_chk (gimple_stmt_iterator *gsi)
1639 gimple stmt = gsi_stmt (*gsi);
1640 tree dest = gimple_call_arg (stmt, 0);
1641 tree src = gimple_call_arg (stmt, 1);
1642 tree len = gimple_call_arg (stmt, 2);
1643 tree size = gimple_call_arg (stmt, 3);
1644 tree fn;
1645 const char *p;
1647 p = c_getstr (src);
1648 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
1649 if ((p && *p == '\0')
1650 || integer_zerop (len))
1652 replace_call_with_value (gsi, dest);
1653 return true;
1656 if (! tree_fits_uhwi_p (size))
1657 return false;
1659 if (! integer_all_onesp (size))
1661 tree src_len = c_strlen (src, 1);
1662 if (src_len
1663 && tree_fits_uhwi_p (src_len)
1664 && tree_fits_uhwi_p (len)
1665 && ! tree_int_cst_lt (len, src_len))
1667 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
1668 fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
1669 if (!fn)
1670 return false;
1672 gimple repl = gimple_build_call (fn, 3, dest, src, size);
1673 replace_call_with_call_and_fold (gsi, repl);
1674 return true;
1676 return false;
1679 /* If __builtin_strncat_chk is used, assume strncat is available. */
1680 fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
1681 if (!fn)
1682 return false;
1684 gimple repl = gimple_build_call (fn, 3, dest, src, len);
1685 replace_call_with_call_and_fold (gsi, repl);
1686 return true;
1689 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
1690 to the call. IGNORE is true if the value returned
1691 by the builtin will be ignored. UNLOCKED is true is true if this
1692 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
1693 the known length of the string. Return NULL_TREE if no simplification
1694 was possible. */
1696 static bool
1697 gimple_fold_builtin_fputs (gimple_stmt_iterator *gsi,
1698 tree arg0, tree arg1,
1699 bool unlocked)
1701 gimple stmt = gsi_stmt (*gsi);
1703 /* If we're using an unlocked function, assume the other unlocked
1704 functions exist explicitly. */
1705 tree const fn_fputc = (unlocked
1706 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
1707 : builtin_decl_implicit (BUILT_IN_FPUTC));
1708 tree const fn_fwrite = (unlocked
1709 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
1710 : builtin_decl_implicit (BUILT_IN_FWRITE));
1712 /* If the return value is used, don't do the transformation. */
1713 if (gimple_call_lhs (stmt))
1714 return false;
1716 /* Get the length of the string passed to fputs. If the length
1717 can't be determined, punt. */
1718 tree len = get_maxval_strlen (arg0, 0);
1719 if (!len
1720 || TREE_CODE (len) != INTEGER_CST)
1721 return false;
1723 switch (compare_tree_int (len, 1))
1725 case -1: /* length is 0, delete the call entirely . */
1726 replace_call_with_value (gsi, integer_zero_node);
1727 return true;
1729 case 0: /* length is 1, call fputc. */
1731 const char *p = c_getstr (arg0);
1732 if (p != NULL)
1734 if (!fn_fputc)
1735 return false;
1737 gimple repl = gimple_build_call (fn_fputc, 2,
1738 build_int_cst
1739 (integer_type_node, p[0]), arg1);
1740 replace_call_with_call_and_fold (gsi, repl);
1741 return true;
1744 /* FALLTHROUGH */
1745 case 1: /* length is greater than 1, call fwrite. */
1747 /* If optimizing for size keep fputs. */
1748 if (optimize_function_for_size_p (cfun))
1749 return false;
1750 /* New argument list transforming fputs(string, stream) to
1751 fwrite(string, 1, len, stream). */
1752 if (!fn_fwrite)
1753 return false;
1755 gimple repl = gimple_build_call (fn_fwrite, 4, arg0,
1756 size_one_node, len, arg1);
1757 replace_call_with_call_and_fold (gsi, repl);
1758 return true;
1760 default:
1761 gcc_unreachable ();
1763 return false;
1766 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
1767 DEST, SRC, LEN, and SIZE are the arguments to the call.
1768 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
1769 code of the builtin. If MAXLEN is not NULL, it is maximum length
1770 passed as third argument. */
1772 static bool
1773 gimple_fold_builtin_memory_chk (gimple_stmt_iterator *gsi,
1774 tree dest, tree src, tree len, tree size,
1775 enum built_in_function fcode)
1777 gimple stmt = gsi_stmt (*gsi);
1778 location_t loc = gimple_location (stmt);
1779 bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
1780 tree fn;
1782 /* If SRC and DEST are the same (and not volatile), return DEST
1783 (resp. DEST+LEN for __mempcpy_chk). */
1784 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
1786 if (fcode != BUILT_IN_MEMPCPY_CHK)
1788 replace_call_with_value (gsi, dest);
1789 return true;
1791 else
1793 tree temp = fold_build_pointer_plus_loc (loc, dest, len);
1794 temp = force_gimple_operand_gsi (gsi, temp,
1795 false, NULL_TREE, true,
1796 GSI_SAME_STMT);
1797 replace_call_with_value (gsi, temp);
1798 return true;
1802 if (! tree_fits_uhwi_p (size))
1803 return false;
1805 tree maxlen = get_maxval_strlen (len, 2);
1806 if (! integer_all_onesp (size))
1808 if (! tree_fits_uhwi_p (len))
1810 /* If LEN is not constant, try MAXLEN too.
1811 For MAXLEN only allow optimizing into non-_ocs function
1812 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
1813 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
1815 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
1817 /* (void) __mempcpy_chk () can be optimized into
1818 (void) __memcpy_chk (). */
1819 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
1820 if (!fn)
1821 return false;
1823 gimple repl = gimple_build_call (fn, 4, dest, src, len, size);
1824 replace_call_with_call_and_fold (gsi, repl);
1825 return true;
1827 return false;
1830 else
1831 maxlen = len;
1833 if (tree_int_cst_lt (size, maxlen))
1834 return false;
1837 fn = NULL_TREE;
1838 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
1839 mem{cpy,pcpy,move,set} is available. */
1840 switch (fcode)
1842 case BUILT_IN_MEMCPY_CHK:
1843 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
1844 break;
1845 case BUILT_IN_MEMPCPY_CHK:
1846 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
1847 break;
1848 case BUILT_IN_MEMMOVE_CHK:
1849 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
1850 break;
1851 case BUILT_IN_MEMSET_CHK:
1852 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
1853 break;
1854 default:
1855 break;
1858 if (!fn)
1859 return false;
1861 gimple repl = gimple_build_call (fn, 3, dest, src, len);
1862 replace_call_with_call_and_fold (gsi, repl);
1863 return true;
1866 /* Fold a call to the __st[rp]cpy_chk builtin.
1867 DEST, SRC, and SIZE are the arguments to the call.
1868 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
1869 code of the builtin. If MAXLEN is not NULL, it is maximum length of
1870 strings passed as second argument. */
1872 static bool
1873 gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator *gsi,
1874 tree dest,
1875 tree src, tree size,
1876 enum built_in_function fcode)
1878 gimple stmt = gsi_stmt (*gsi);
1879 location_t loc = gimple_location (stmt);
1880 bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
1881 tree len, fn;
1883 /* If SRC and DEST are the same (and not volatile), return DEST. */
1884 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
1886 replace_call_with_value (gsi, dest);
1887 return true;
1890 if (! tree_fits_uhwi_p (size))
1891 return false;
1893 tree maxlen = get_maxval_strlen (src, 1);
1894 if (! integer_all_onesp (size))
1896 len = c_strlen (src, 1);
1897 if (! len || ! tree_fits_uhwi_p (len))
1899 /* If LEN is not constant, try MAXLEN too.
1900 For MAXLEN only allow optimizing into non-_ocs function
1901 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
1902 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
1904 if (fcode == BUILT_IN_STPCPY_CHK)
1906 if (! ignore)
1907 return false;
1909 /* If return value of __stpcpy_chk is ignored,
1910 optimize into __strcpy_chk. */
1911 fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
1912 if (!fn)
1913 return false;
1915 gimple repl = gimple_build_call (fn, 3, dest, src, size);
1916 replace_call_with_call_and_fold (gsi, repl);
1917 return true;
1920 if (! len || TREE_SIDE_EFFECTS (len))
1921 return false;
1923 /* If c_strlen returned something, but not a constant,
1924 transform __strcpy_chk into __memcpy_chk. */
1925 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
1926 if (!fn)
1927 return false;
1929 len = fold_convert_loc (loc, size_type_node, len);
1930 len = size_binop_loc (loc, PLUS_EXPR, len,
1931 build_int_cst (size_type_node, 1));
1932 len = force_gimple_operand_gsi (gsi, len, true, NULL_TREE,
1933 true, GSI_SAME_STMT);
1934 gimple repl = gimple_build_call (fn, 4, dest, src, len, size);
1935 replace_call_with_call_and_fold (gsi, repl);
1936 return true;
1939 else
1940 maxlen = len;
1942 if (! tree_int_cst_lt (maxlen, size))
1943 return false;
1946 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
1947 fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK
1948 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
1949 if (!fn)
1950 return false;
1952 gimple repl = gimple_build_call (fn, 2, dest, src);
1953 replace_call_with_call_and_fold (gsi, repl);
1954 return true;
1957 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
1958 are the arguments to the call. If MAXLEN is not NULL, it is maximum
1959 length passed as third argument. IGNORE is true if return value can be
1960 ignored. FCODE is the BUILT_IN_* code of the builtin. */
1962 static bool
1963 gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator *gsi,
1964 tree dest, tree src,
1965 tree len, tree size,
1966 enum built_in_function fcode)
1968 gimple stmt = gsi_stmt (*gsi);
1969 bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
1970 tree fn;
1972 if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
1974 /* If return value of __stpncpy_chk is ignored,
1975 optimize into __strncpy_chk. */
1976 fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
1977 if (fn)
1979 gimple repl = gimple_build_call (fn, 4, dest, src, len, size);
1980 replace_call_with_call_and_fold (gsi, repl);
1981 return true;
1985 if (! tree_fits_uhwi_p (size))
1986 return false;
1988 tree maxlen = get_maxval_strlen (len, 2);
1989 if (! integer_all_onesp (size))
1991 if (! tree_fits_uhwi_p (len))
1993 /* If LEN is not constant, try MAXLEN too.
1994 For MAXLEN only allow optimizing into non-_ocs function
1995 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
1996 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
1997 return false;
1999 else
2000 maxlen = len;
2002 if (tree_int_cst_lt (size, maxlen))
2003 return false;
2006 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
2007 fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK
2008 ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
2009 if (!fn)
2010 return false;
2012 gimple repl = gimple_build_call (fn, 3, dest, src, len);
2013 replace_call_with_call_and_fold (gsi, repl);
2014 return true;
2017 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
2018 NULL_TREE if a normal call should be emitted rather than expanding
2019 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
2020 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
2021 passed as second argument. */
2023 static bool
2024 gimple_fold_builtin_snprintf_chk (gimple_stmt_iterator *gsi,
2025 enum built_in_function fcode)
2027 gimple stmt = gsi_stmt (*gsi);
2028 tree dest, size, len, fn, fmt, flag;
2029 const char *fmt_str;
2031 /* Verify the required arguments in the original call. */
2032 if (gimple_call_num_args (stmt) < 5)
2033 return false;
2035 dest = gimple_call_arg (stmt, 0);
2036 len = gimple_call_arg (stmt, 1);
2037 flag = gimple_call_arg (stmt, 2);
2038 size = gimple_call_arg (stmt, 3);
2039 fmt = gimple_call_arg (stmt, 4);
2041 if (! tree_fits_uhwi_p (size))
2042 return false;
2044 if (! integer_all_onesp (size))
2046 tree maxlen = get_maxval_strlen (len, 2);
2047 if (! tree_fits_uhwi_p (len))
2049 /* If LEN is not constant, try MAXLEN too.
2050 For MAXLEN only allow optimizing into non-_ocs function
2051 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2052 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
2053 return false;
2055 else
2056 maxlen = len;
2058 if (tree_int_cst_lt (size, maxlen))
2059 return false;
2062 if (!init_target_chars ())
2063 return false;
2065 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
2066 or if format doesn't contain % chars or is "%s". */
2067 if (! integer_zerop (flag))
2069 fmt_str = c_getstr (fmt);
2070 if (fmt_str == NULL)
2071 return false;
2072 if (strchr (fmt_str, target_percent) != NULL
2073 && strcmp (fmt_str, target_percent_s))
2074 return false;
2077 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
2078 available. */
2079 fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
2080 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
2081 if (!fn)
2082 return false;
2084 /* Replace the called function and the first 5 argument by 3 retaining
2085 trailing varargs. */
2086 gimple_call_set_fndecl (stmt, fn);
2087 gimple_call_set_fntype (stmt, TREE_TYPE (fn));
2088 gimple_call_set_arg (stmt, 0, dest);
2089 gimple_call_set_arg (stmt, 1, len);
2090 gimple_call_set_arg (stmt, 2, fmt);
2091 for (unsigned i = 3; i < gimple_call_num_args (stmt) - 2; ++i)
2092 gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2));
2093 gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2);
2094 fold_stmt (gsi);
2095 return true;
2098 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
2099 Return NULL_TREE if a normal call should be emitted rather than
2100 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
2101 or BUILT_IN_VSPRINTF_CHK. */
2103 static bool
2104 gimple_fold_builtin_sprintf_chk (gimple_stmt_iterator *gsi,
2105 enum built_in_function fcode)
2107 gimple stmt = gsi_stmt (*gsi);
2108 tree dest, size, len, fn, fmt, flag;
2109 const char *fmt_str;
2110 unsigned nargs = gimple_call_num_args (stmt);
2112 /* Verify the required arguments in the original call. */
2113 if (nargs < 4)
2114 return false;
2115 dest = gimple_call_arg (stmt, 0);
2116 flag = gimple_call_arg (stmt, 1);
2117 size = gimple_call_arg (stmt, 2);
2118 fmt = gimple_call_arg (stmt, 3);
2120 if (! tree_fits_uhwi_p (size))
2121 return false;
2123 len = NULL_TREE;
2125 if (!init_target_chars ())
2126 return false;
2128 /* Check whether the format is a literal string constant. */
2129 fmt_str = c_getstr (fmt);
2130 if (fmt_str != NULL)
2132 /* If the format doesn't contain % args or %%, we know the size. */
2133 if (strchr (fmt_str, target_percent) == 0)
2135 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
2136 len = build_int_cstu (size_type_node, strlen (fmt_str));
2138 /* If the format is "%s" and first ... argument is a string literal,
2139 we know the size too. */
2140 else if (fcode == BUILT_IN_SPRINTF_CHK
2141 && strcmp (fmt_str, target_percent_s) == 0)
2143 tree arg;
2145 if (nargs == 5)
2147 arg = gimple_call_arg (stmt, 4);
2148 if (POINTER_TYPE_P (TREE_TYPE (arg)))
2150 len = c_strlen (arg, 1);
2151 if (! len || ! tree_fits_uhwi_p (len))
2152 len = NULL_TREE;
2158 if (! integer_all_onesp (size))
2160 if (! len || ! tree_int_cst_lt (len, size))
2161 return false;
2164 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
2165 or if format doesn't contain % chars or is "%s". */
2166 if (! integer_zerop (flag))
2168 if (fmt_str == NULL)
2169 return false;
2170 if (strchr (fmt_str, target_percent) != NULL
2171 && strcmp (fmt_str, target_percent_s))
2172 return false;
2175 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
2176 fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
2177 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
2178 if (!fn)
2179 return false;
2181 /* Replace the called function and the first 4 argument by 2 retaining
2182 trailing varargs. */
2183 gimple_call_set_fndecl (stmt, fn);
2184 gimple_call_set_fntype (stmt, TREE_TYPE (fn));
2185 gimple_call_set_arg (stmt, 0, dest);
2186 gimple_call_set_arg (stmt, 1, fmt);
2187 for (unsigned i = 2; i < gimple_call_num_args (stmt) - 2; ++i)
2188 gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2));
2189 gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2);
2190 fold_stmt (gsi);
2191 return true;
2194 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
2195 ORIG may be null if this is a 2-argument call. We don't attempt to
2196 simplify calls with more than 3 arguments.
2198 Return NULL_TREE if no simplification was possible, otherwise return the
2199 simplified form of the call as a tree. If IGNORED is true, it means that
2200 the caller does not use the returned value of the function. */
2202 static bool
2203 gimple_fold_builtin_sprintf (gimple_stmt_iterator *gsi)
2205 gimple stmt = gsi_stmt (*gsi);
2206 tree dest = gimple_call_arg (stmt, 0);
2207 tree fmt = gimple_call_arg (stmt, 1);
2208 tree orig = NULL_TREE;
2209 const char *fmt_str = NULL;
2211 /* Verify the required arguments in the original call. We deal with two
2212 types of sprintf() calls: 'sprintf (str, fmt)' and
2213 'sprintf (dest, "%s", orig)'. */
2214 if (gimple_call_num_args (stmt) > 3)
2215 return false;
2217 if (gimple_call_num_args (stmt) == 3)
2218 orig = gimple_call_arg (stmt, 2);
2220 /* Check whether the format is a literal string constant. */
2221 fmt_str = c_getstr (fmt);
2222 if (fmt_str == NULL)
2223 return false;
2225 if (!init_target_chars ())
2226 return false;
2228 /* If the format doesn't contain % args or %%, use strcpy. */
2229 if (strchr (fmt_str, target_percent) == NULL)
2231 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
2233 if (!fn)
2234 return false;
2236 /* Don't optimize sprintf (buf, "abc", ptr++). */
2237 if (orig)
2238 return false;
2240 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
2241 'format' is known to contain no % formats. */
2242 gimple_seq stmts = NULL;
2243 gimple repl = gimple_build_call (fn, 2, dest, fmt);
2244 gimple_seq_add_stmt_without_update (&stmts, repl);
2245 if (gimple_call_lhs (stmt))
2247 repl = gimple_build_assign (gimple_call_lhs (stmt),
2248 build_int_cst (integer_type_node,
2249 strlen (fmt_str)));
2250 gimple_seq_add_stmt_without_update (&stmts, repl);
2251 gsi_replace_with_seq_vops (gsi, stmts);
2252 /* gsi now points at the assignment to the lhs, get a
2253 stmt iterator to the memcpy call.
2254 ??? We can't use gsi_for_stmt as that doesn't work when the
2255 CFG isn't built yet. */
2256 gimple_stmt_iterator gsi2 = *gsi;
2257 gsi_prev (&gsi2);
2258 fold_stmt (&gsi2);
2260 else
2262 gsi_replace_with_seq_vops (gsi, stmts);
2263 fold_stmt (gsi);
2265 return true;
2268 /* If the format is "%s", use strcpy if the result isn't used. */
2269 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
2271 tree fn;
2272 fn = builtin_decl_implicit (BUILT_IN_STRCPY);
2274 if (!fn)
2275 return false;
2277 /* Don't crash on sprintf (str1, "%s"). */
2278 if (!orig)
2279 return false;
2281 tree orig_len = NULL_TREE;
2282 if (gimple_call_lhs (stmt))
2284 orig_len = get_maxval_strlen (orig, 0);
2285 if (!orig_len)
2286 return false;
2289 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
2290 gimple_seq stmts = NULL;
2291 gimple repl = gimple_build_call (fn, 2, dest, orig);
2292 gimple_seq_add_stmt_without_update (&stmts, repl);
2293 if (gimple_call_lhs (stmt))
2295 if (!useless_type_conversion_p (integer_type_node,
2296 TREE_TYPE (orig_len)))
2297 orig_len = fold_convert (integer_type_node, orig_len);
2298 repl = gimple_build_assign (gimple_call_lhs (stmt), orig_len);
2299 gimple_seq_add_stmt_without_update (&stmts, repl);
2300 gsi_replace_with_seq_vops (gsi, stmts);
2301 /* gsi now points at the assignment to the lhs, get a
2302 stmt iterator to the memcpy call.
2303 ??? We can't use gsi_for_stmt as that doesn't work when the
2304 CFG isn't built yet. */
2305 gimple_stmt_iterator gsi2 = *gsi;
2306 gsi_prev (&gsi2);
2307 fold_stmt (&gsi2);
2309 else
2311 gsi_replace_with_seq_vops (gsi, stmts);
2312 fold_stmt (gsi);
2314 return true;
2316 return false;
2319 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
2320 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
2321 attempt to simplify calls with more than 4 arguments.
2323 Return NULL_TREE if no simplification was possible, otherwise return the
2324 simplified form of the call as a tree. If IGNORED is true, it means that
2325 the caller does not use the returned value of the function. */
2327 static bool
2328 gimple_fold_builtin_snprintf (gimple_stmt_iterator *gsi)
2330 gimple stmt = gsi_stmt (*gsi);
2331 tree dest = gimple_call_arg (stmt, 0);
2332 tree destsize = gimple_call_arg (stmt, 1);
2333 tree fmt = gimple_call_arg (stmt, 2);
2334 tree orig = NULL_TREE;
2335 const char *fmt_str = NULL;
2337 if (gimple_call_num_args (stmt) > 4)
2338 return false;
2340 if (gimple_call_num_args (stmt) == 4)
2341 orig = gimple_call_arg (stmt, 3);
2343 if (!tree_fits_uhwi_p (destsize))
2344 return false;
2345 unsigned HOST_WIDE_INT destlen = tree_to_uhwi (destsize);
2347 /* Check whether the format is a literal string constant. */
2348 fmt_str = c_getstr (fmt);
2349 if (fmt_str == NULL)
2350 return false;
2352 if (!init_target_chars ())
2353 return false;
2355 /* If the format doesn't contain % args or %%, use strcpy. */
2356 if (strchr (fmt_str, target_percent) == NULL)
2358 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
2359 if (!fn)
2360 return false;
2362 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
2363 if (orig)
2364 return false;
2366 /* We could expand this as
2367 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
2368 or to
2369 memcpy (str, fmt_with_nul_at_cstm1, cst);
2370 but in the former case that might increase code size
2371 and in the latter case grow .rodata section too much.
2372 So punt for now. */
2373 size_t len = strlen (fmt_str);
2374 if (len >= destlen)
2375 return false;
2377 gimple_seq stmts = NULL;
2378 gimple repl = gimple_build_call (fn, 2, dest, fmt);
2379 gimple_seq_add_stmt_without_update (&stmts, repl);
2380 if (gimple_call_lhs (stmt))
2382 repl = gimple_build_assign (gimple_call_lhs (stmt),
2383 build_int_cst (integer_type_node, len));
2384 gimple_seq_add_stmt_without_update (&stmts, repl);
2385 gsi_replace_with_seq_vops (gsi, stmts);
2386 /* gsi now points at the assignment to the lhs, get a
2387 stmt iterator to the memcpy call.
2388 ??? We can't use gsi_for_stmt as that doesn't work when the
2389 CFG isn't built yet. */
2390 gimple_stmt_iterator gsi2 = *gsi;
2391 gsi_prev (&gsi2);
2392 fold_stmt (&gsi2);
2394 else
2396 gsi_replace_with_seq_vops (gsi, stmts);
2397 fold_stmt (gsi);
2399 return true;
2402 /* If the format is "%s", use strcpy if the result isn't used. */
2403 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
2405 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
2406 if (!fn)
2407 return false;
2409 /* Don't crash on snprintf (str1, cst, "%s"). */
2410 if (!orig)
2411 return false;
2413 tree orig_len = get_maxval_strlen (orig, 0);
2414 if (!orig_len)
2415 return false;
2417 /* We could expand this as
2418 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
2419 or to
2420 memcpy (str1, str2_with_nul_at_cstm1, cst);
2421 but in the former case that might increase code size
2422 and in the latter case grow .rodata section too much.
2423 So punt for now. */
2424 if (compare_tree_int (orig_len, destlen) >= 0)
2425 return false;
2427 /* Convert snprintf (str1, cst, "%s", str2) into
2428 strcpy (str1, str2) if strlen (str2) < cst. */
2429 gimple_seq stmts = NULL;
2430 gimple repl = gimple_build_call (fn, 2, dest, orig);
2431 gimple_seq_add_stmt_without_update (&stmts, repl);
2432 if (gimple_call_lhs (stmt))
2434 if (!useless_type_conversion_p (integer_type_node,
2435 TREE_TYPE (orig_len)))
2436 orig_len = fold_convert (integer_type_node, orig_len);
2437 repl = gimple_build_assign (gimple_call_lhs (stmt), orig_len);
2438 gimple_seq_add_stmt_without_update (&stmts, repl);
2439 gsi_replace_with_seq_vops (gsi, stmts);
2440 /* gsi now points at the assignment to the lhs, get a
2441 stmt iterator to the memcpy call.
2442 ??? We can't use gsi_for_stmt as that doesn't work when the
2443 CFG isn't built yet. */
2444 gimple_stmt_iterator gsi2 = *gsi;
2445 gsi_prev (&gsi2);
2446 fold_stmt (&gsi2);
2448 else
2450 gsi_replace_with_seq_vops (gsi, stmts);
2451 fold_stmt (gsi);
2453 return true;
2455 return false;
2459 /* Fold a call to __builtin_strlen with known length LEN. */
2461 static bool
2462 gimple_fold_builtin_strlen (gimple_stmt_iterator *gsi)
2464 gimple stmt = gsi_stmt (*gsi);
2465 tree len = get_maxval_strlen (gimple_call_arg (stmt, 0), 0);
2466 if (!len)
2467 return false;
2468 len = force_gimple_operand_gsi (gsi, len, true, NULL, true, GSI_SAME_STMT);
2469 replace_call_with_value (gsi, len);
2470 return true;
2474 /* Fold the non-target builtin at *GSI and return whether any simplification
2475 was made. */
2477 static bool
2478 gimple_fold_builtin (gimple_stmt_iterator *gsi)
2480 gimple stmt = gsi_stmt (*gsi);
2481 tree callee = gimple_call_fndecl (stmt);
2483 /* Give up for always_inline inline builtins until they are
2484 inlined. */
2485 if (avoid_folding_inline_builtin (callee))
2486 return false;
2488 switch (DECL_FUNCTION_CODE (callee))
2490 case BUILT_IN_BZERO:
2491 return gimple_fold_builtin_memset (gsi, integer_zero_node,
2492 gimple_call_arg (stmt, 1));
2493 case BUILT_IN_MEMSET:
2494 return gimple_fold_builtin_memset (gsi,
2495 gimple_call_arg (stmt, 1),
2496 gimple_call_arg (stmt, 2));
2497 case BUILT_IN_BCOPY:
2498 return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 1),
2499 gimple_call_arg (stmt, 0), 3);
2500 case BUILT_IN_MEMCPY:
2501 return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
2502 gimple_call_arg (stmt, 1), 0);
2503 case BUILT_IN_MEMPCPY:
2504 return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
2505 gimple_call_arg (stmt, 1), 1);
2506 case BUILT_IN_MEMMOVE:
2507 return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
2508 gimple_call_arg (stmt, 1), 3);
2509 case BUILT_IN_SPRINTF_CHK:
2510 case BUILT_IN_VSPRINTF_CHK:
2511 return gimple_fold_builtin_sprintf_chk (gsi, DECL_FUNCTION_CODE (callee));
2512 case BUILT_IN_STRCAT_CHK:
2513 return gimple_fold_builtin_strcat_chk (gsi);
2514 case BUILT_IN_STRNCAT_CHK:
2515 return gimple_fold_builtin_strncat_chk (gsi);
2516 case BUILT_IN_STRLEN:
2517 return gimple_fold_builtin_strlen (gsi);
2518 case BUILT_IN_STRCPY:
2519 return gimple_fold_builtin_strcpy (gsi,
2520 gimple_call_arg (stmt, 0),
2521 gimple_call_arg (stmt, 1));
2522 case BUILT_IN_STRNCPY:
2523 return gimple_fold_builtin_strncpy (gsi,
2524 gimple_call_arg (stmt, 0),
2525 gimple_call_arg (stmt, 1),
2526 gimple_call_arg (stmt, 2));
2527 case BUILT_IN_STRCAT:
2528 return gimple_fold_builtin_strcat (gsi, gimple_call_arg (stmt, 0),
2529 gimple_call_arg (stmt, 1));
2530 case BUILT_IN_FPUTS:
2531 return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
2532 gimple_call_arg (stmt, 1), false);
2533 case BUILT_IN_FPUTS_UNLOCKED:
2534 return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
2535 gimple_call_arg (stmt, 1), true);
2536 case BUILT_IN_MEMCPY_CHK:
2537 case BUILT_IN_MEMPCPY_CHK:
2538 case BUILT_IN_MEMMOVE_CHK:
2539 case BUILT_IN_MEMSET_CHK:
2540 return gimple_fold_builtin_memory_chk (gsi,
2541 gimple_call_arg (stmt, 0),
2542 gimple_call_arg (stmt, 1),
2543 gimple_call_arg (stmt, 2),
2544 gimple_call_arg (stmt, 3),
2545 DECL_FUNCTION_CODE (callee));
2546 case BUILT_IN_STRCPY_CHK:
2547 case BUILT_IN_STPCPY_CHK:
2548 return gimple_fold_builtin_stxcpy_chk (gsi,
2549 gimple_call_arg (stmt, 0),
2550 gimple_call_arg (stmt, 1),
2551 gimple_call_arg (stmt, 2),
2552 DECL_FUNCTION_CODE (callee));
2553 case BUILT_IN_STRNCPY_CHK:
2554 case BUILT_IN_STPNCPY_CHK:
2555 return gimple_fold_builtin_stxncpy_chk (gsi,
2556 gimple_call_arg (stmt, 0),
2557 gimple_call_arg (stmt, 1),
2558 gimple_call_arg (stmt, 2),
2559 gimple_call_arg (stmt, 3),
2560 DECL_FUNCTION_CODE (callee));
2561 case BUILT_IN_SNPRINTF_CHK:
2562 case BUILT_IN_VSNPRINTF_CHK:
2563 return gimple_fold_builtin_snprintf_chk (gsi,
2564 DECL_FUNCTION_CODE (callee));
2565 case BUILT_IN_SNPRINTF:
2566 return gimple_fold_builtin_snprintf (gsi);
2567 case BUILT_IN_SPRINTF:
2568 return gimple_fold_builtin_sprintf (gsi);
2569 default:;
2572 /* Try the generic builtin folder. */
2573 bool ignore = (gimple_call_lhs (stmt) == NULL);
2574 tree result = fold_call_stmt (stmt, ignore);
2575 if (result)
2577 if (ignore)
2578 STRIP_NOPS (result);
2579 else
2580 result = fold_convert (gimple_call_return_type (stmt), result);
2581 if (!update_call_from_tree (gsi, result))
2582 gimplify_and_update_call_from_tree (gsi, result);
2583 return true;
2586 return false;
2589 /* Return true if ARG0 CODE ARG1 in infinite signed precision operation
2590 doesn't fit into TYPE. The test for overflow should be regardless of
2591 -fwrapv, and even for unsigned types. */
2593 bool
2594 arith_overflowed_p (enum tree_code code, const_tree type,
2595 const_tree arg0, const_tree arg1)
2597 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) widest2_int;
2598 typedef generic_wide_int <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> >
2599 widest2_int_cst;
2600 widest2_int warg0 = widest2_int_cst (arg0);
2601 widest2_int warg1 = widest2_int_cst (arg1);
2602 widest2_int wres;
2603 switch (code)
2605 case PLUS_EXPR: wres = wi::add (warg0, warg1); break;
2606 case MINUS_EXPR: wres = wi::sub (warg0, warg1); break;
2607 case MULT_EXPR: wres = wi::mul (warg0, warg1); break;
2608 default: gcc_unreachable ();
2610 signop sign = TYPE_SIGN (type);
2611 if (sign == UNSIGNED && wi::neg_p (wres))
2612 return true;
2613 return wi::min_precision (wres, sign) > TYPE_PRECISION (type);
2616 /* Attempt to fold a call statement referenced by the statement iterator GSI.
2617 The statement may be replaced by another statement, e.g., if the call
2618 simplifies to a constant value. Return true if any changes were made.
2619 It is assumed that the operands have been previously folded. */
2621 static bool
2622 gimple_fold_call (gimple_stmt_iterator *gsi, bool inplace)
2624 gimple stmt = gsi_stmt (*gsi);
2625 tree callee;
2626 bool changed = false;
2627 unsigned i;
2629 /* Fold *& in call arguments. */
2630 for (i = 0; i < gimple_call_num_args (stmt); ++i)
2631 if (REFERENCE_CLASS_P (gimple_call_arg (stmt, i)))
2633 tree tmp = maybe_fold_reference (gimple_call_arg (stmt, i), false);
2634 if (tmp)
2636 gimple_call_set_arg (stmt, i, tmp);
2637 changed = true;
2641 /* Check for virtual calls that became direct calls. */
2642 callee = gimple_call_fn (stmt);
2643 if (callee && TREE_CODE (callee) == OBJ_TYPE_REF)
2645 if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee)) != NULL_TREE)
2647 if (dump_file && virtual_method_call_p (callee)
2648 && !possible_polymorphic_call_target_p
2649 (callee, stmt, cgraph_node::get (gimple_call_addr_fndecl
2650 (OBJ_TYPE_REF_EXPR (callee)))))
2652 fprintf (dump_file,
2653 "Type inheritance inconsistent devirtualization of ");
2654 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2655 fprintf (dump_file, " to ");
2656 print_generic_expr (dump_file, callee, TDF_SLIM);
2657 fprintf (dump_file, "\n");
2660 gimple_call_set_fn (stmt, OBJ_TYPE_REF_EXPR (callee));
2661 changed = true;
2663 else if (flag_devirtualize && !inplace && virtual_method_call_p (callee))
2665 bool final;
2666 vec <cgraph_node *>targets
2667 = possible_polymorphic_call_targets (callee, stmt, &final);
2668 if (final && targets.length () <= 1 && dbg_cnt (devirt))
2670 tree lhs = gimple_call_lhs (stmt);
2671 if (dump_enabled_p ())
2673 location_t loc = gimple_location_safe (stmt);
2674 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
2675 "folding virtual function call to %s\n",
2676 targets.length () == 1
2677 ? targets[0]->name ()
2678 : "__builtin_unreachable");
2680 if (targets.length () == 1)
2682 gimple_call_set_fndecl (stmt, targets[0]->decl);
2683 changed = true;
2684 /* If the call becomes noreturn, remove the lhs. */
2685 if (lhs && (gimple_call_flags (stmt) & ECF_NORETURN))
2687 if (TREE_CODE (lhs) == SSA_NAME)
2689 tree var = create_tmp_var (TREE_TYPE (lhs), NULL);
2690 tree def = get_or_create_ssa_default_def (cfun, var);
2691 gimple new_stmt = gimple_build_assign (lhs, def);
2692 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
2694 gimple_call_set_lhs (stmt, NULL_TREE);
2697 else
2699 tree fndecl = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
2700 gimple new_stmt = gimple_build_call (fndecl, 0);
2701 gimple_set_location (new_stmt, gimple_location (stmt));
2702 if (lhs && TREE_CODE (lhs) == SSA_NAME)
2704 tree var = create_tmp_var (TREE_TYPE (lhs), NULL);
2705 tree def = get_or_create_ssa_default_def (cfun, var);
2707 /* To satisfy condition for
2708 cgraph_update_edges_for_call_stmt_node,
2709 we need to preserve GIMPLE_CALL statement
2710 at position of GSI iterator. */
2711 update_call_from_tree (gsi, def);
2712 gsi_insert_before (gsi, new_stmt, GSI_NEW_STMT);
2714 else
2716 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
2717 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
2718 gsi_replace (gsi, new_stmt, false);
2720 return true;
2726 if (inplace)
2727 return changed;
2729 /* Check for builtins that CCP can handle using information not
2730 available in the generic fold routines. */
2731 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
2733 if (gimple_fold_builtin (gsi))
2734 changed = true;
2736 else if (gimple_call_builtin_p (stmt, BUILT_IN_MD))
2738 changed |= targetm.gimple_fold_builtin (gsi);
2740 else if (gimple_call_internal_p (stmt))
2742 enum tree_code subcode = ERROR_MARK;
2743 tree result = NULL_TREE;
2744 bool cplx_result = false;
2745 tree overflow = NULL_TREE;
2746 switch (gimple_call_internal_fn (stmt))
2748 case IFN_BUILTIN_EXPECT:
2749 result = fold_builtin_expect (gimple_location (stmt),
2750 gimple_call_arg (stmt, 0),
2751 gimple_call_arg (stmt, 1),
2752 gimple_call_arg (stmt, 2));
2753 break;
2754 case IFN_UBSAN_OBJECT_SIZE:
2755 if (integer_all_onesp (gimple_call_arg (stmt, 2))
2756 || (TREE_CODE (gimple_call_arg (stmt, 1)) == INTEGER_CST
2757 && TREE_CODE (gimple_call_arg (stmt, 2)) == INTEGER_CST
2758 && tree_int_cst_le (gimple_call_arg (stmt, 1),
2759 gimple_call_arg (stmt, 2))))
2761 gsi_replace (gsi, gimple_build_nop (), true);
2762 unlink_stmt_vdef (stmt);
2763 release_defs (stmt);
2764 return true;
2766 break;
2767 case IFN_UBSAN_CHECK_ADD:
2768 subcode = PLUS_EXPR;
2769 break;
2770 case IFN_UBSAN_CHECK_SUB:
2771 subcode = MINUS_EXPR;
2772 break;
2773 case IFN_UBSAN_CHECK_MUL:
2774 subcode = MULT_EXPR;
2775 break;
2776 case IFN_ADD_OVERFLOW:
2777 subcode = PLUS_EXPR;
2778 cplx_result = true;
2779 break;
2780 case IFN_SUB_OVERFLOW:
2781 subcode = MINUS_EXPR;
2782 cplx_result = true;
2783 break;
2784 case IFN_MUL_OVERFLOW:
2785 subcode = MULT_EXPR;
2786 cplx_result = true;
2787 break;
2788 default:
2789 break;
2791 if (subcode != ERROR_MARK)
2793 tree arg0 = gimple_call_arg (stmt, 0);
2794 tree arg1 = gimple_call_arg (stmt, 1);
2795 tree type = TREE_TYPE (arg0);
2796 if (cplx_result)
2798 tree lhs = gimple_call_lhs (stmt);
2799 if (lhs == NULL_TREE)
2800 type = NULL_TREE;
2801 else
2802 type = TREE_TYPE (TREE_TYPE (lhs));
2804 if (type == NULL_TREE)
2806 /* x = y + 0; x = y - 0; x = y * 0; */
2807 else if (integer_zerop (arg1))
2808 result = subcode == MULT_EXPR ? integer_zero_node : arg0;
2809 /* x = 0 + y; x = 0 * y; */
2810 else if (subcode != MINUS_EXPR && integer_zerop (arg0))
2811 result = subcode == MULT_EXPR ? integer_zero_node : arg1;
2812 /* x = y - y; */
2813 else if (subcode == MINUS_EXPR && operand_equal_p (arg0, arg1, 0))
2814 result = integer_zero_node;
2815 /* x = y * 1; x = 1 * y; */
2816 else if (subcode == MULT_EXPR && integer_onep (arg1))
2817 result = arg0;
2818 else if (subcode == MULT_EXPR && integer_onep (arg0))
2819 result = arg1;
2820 else if (TREE_CODE (arg0) == INTEGER_CST
2821 && TREE_CODE (arg1) == INTEGER_CST)
2823 if (cplx_result)
2824 result = int_const_binop (subcode, fold_convert (type, arg0),
2825 fold_convert (type, arg1));
2826 else
2827 result = int_const_binop (subcode, arg0, arg1);
2828 if (result && arith_overflowed_p (subcode, type, arg0, arg1))
2830 if (cplx_result)
2831 overflow = build_one_cst (type);
2832 else
2833 result = NULL_TREE;
2836 if (result)
2838 if (result == integer_zero_node)
2839 result = build_zero_cst (type);
2840 else if (cplx_result && TREE_TYPE (result) != type)
2842 if (TREE_CODE (result) == INTEGER_CST)
2844 if (arith_overflowed_p (PLUS_EXPR, type, result,
2845 integer_zero_node))
2846 overflow = build_one_cst (type);
2848 else if ((!TYPE_UNSIGNED (TREE_TYPE (result))
2849 && TYPE_UNSIGNED (type))
2850 || (TYPE_PRECISION (type)
2851 < (TYPE_PRECISION (TREE_TYPE (result))
2852 + (TYPE_UNSIGNED (TREE_TYPE (result))
2853 && !TYPE_UNSIGNED (type)))))
2854 result = NULL_TREE;
2855 if (result)
2856 result = fold_convert (type, result);
2861 if (result)
2863 if (TREE_CODE (result) == INTEGER_CST && TREE_OVERFLOW (result))
2864 result = drop_tree_overflow (result);
2865 if (cplx_result)
2867 if (overflow == NULL_TREE)
2868 overflow = build_zero_cst (TREE_TYPE (result));
2869 tree ctype = build_complex_type (TREE_TYPE (result));
2870 if (TREE_CODE (result) == INTEGER_CST
2871 && TREE_CODE (overflow) == INTEGER_CST)
2872 result = build_complex (ctype, result, overflow);
2873 else
2874 result = build2_loc (gimple_location (stmt), COMPLEX_EXPR,
2875 ctype, result, overflow);
2877 if (!update_call_from_tree (gsi, result))
2878 gimplify_and_update_call_from_tree (gsi, result);
2879 changed = true;
2883 return changed;
2887 /* Worker for fold_stmt_1 dispatch to pattern based folding with
2888 gimple_simplify.
2890 Replaces *GSI with the simplification result in RCODE and OPS
2891 and the associated statements in *SEQ. Does the replacement
2892 according to INPLACE and returns true if the operation succeeded. */
2894 static bool
2895 replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
2896 code_helper rcode, tree *ops,
2897 gimple_seq *seq, bool inplace)
2899 gimple stmt = gsi_stmt (*gsi);
2901 /* Play safe and do not allow abnormals to be mentioned in
2902 newly created statements. See also maybe_push_res_to_seq. */
2903 if ((TREE_CODE (ops[0]) == SSA_NAME
2904 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[0]))
2905 || (ops[1]
2906 && TREE_CODE (ops[1]) == SSA_NAME
2907 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[1]))
2908 || (ops[2]
2909 && TREE_CODE (ops[2]) == SSA_NAME
2910 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[2])))
2911 return false;
2913 if (gimple_code (stmt) == GIMPLE_COND)
2915 gcc_assert (rcode.is_tree_code ());
2916 if (TREE_CODE_CLASS ((enum tree_code)rcode) == tcc_comparison
2917 /* GIMPLE_CONDs condition may not throw. */
2918 && (!flag_exceptions
2919 || !cfun->can_throw_non_call_exceptions
2920 || !operation_could_trap_p (rcode,
2921 FLOAT_TYPE_P (TREE_TYPE (ops[0])),
2922 false, NULL_TREE)))
2923 gimple_cond_set_condition (stmt, rcode, ops[0], ops[1]);
2924 else if (rcode == SSA_NAME)
2925 gimple_cond_set_condition (stmt, NE_EXPR, ops[0],
2926 build_zero_cst (TREE_TYPE (ops[0])));
2927 else if (rcode == INTEGER_CST)
2929 if (integer_zerop (ops[0]))
2930 gimple_cond_make_false (stmt);
2931 else
2932 gimple_cond_make_true (stmt);
2934 else if (!inplace)
2936 tree res = maybe_push_res_to_seq (rcode, boolean_type_node,
2937 ops, seq);
2938 if (!res)
2939 return false;
2940 gimple_cond_set_condition (stmt, NE_EXPR, res,
2941 build_zero_cst (TREE_TYPE (res)));
2943 else
2944 return false;
2945 if (dump_file && (dump_flags & TDF_DETAILS))
2947 fprintf (dump_file, "gimple_simplified to ");
2948 if (!gimple_seq_empty_p (*seq))
2949 print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
2950 print_gimple_stmt (dump_file, gsi_stmt (*gsi),
2951 0, TDF_SLIM);
2953 gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
2954 return true;
2956 else if (is_gimple_assign (stmt)
2957 && rcode.is_tree_code ())
2959 if (!inplace
2960 || gimple_num_ops (stmt) > get_gimple_rhs_num_ops (rcode))
2962 maybe_build_generic_op (rcode,
2963 TREE_TYPE (gimple_assign_lhs (stmt)),
2964 &ops[0], ops[1], ops[2]);
2965 gimple_assign_set_rhs_with_ops_1 (gsi, rcode,
2966 ops[0], ops[1], ops[2]);
2967 if (dump_file && (dump_flags & TDF_DETAILS))
2969 fprintf (dump_file, "gimple_simplified to ");
2970 if (!gimple_seq_empty_p (*seq))
2971 print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
2972 print_gimple_stmt (dump_file, gsi_stmt (*gsi),
2973 0, TDF_SLIM);
2975 gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
2976 return true;
2979 else if (!inplace)
2981 if (gimple_has_lhs (stmt))
2983 tree lhs = gimple_get_lhs (stmt);
2984 maybe_push_res_to_seq (rcode, TREE_TYPE (lhs),
2985 ops, seq, lhs);
2986 if (dump_file && (dump_flags & TDF_DETAILS))
2988 fprintf (dump_file, "gimple_simplified to ");
2989 print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
2991 gsi_replace_with_seq_vops (gsi, *seq);
2992 return true;
2994 else
2995 gcc_unreachable ();
2998 return false;
3001 /* Canonicalize MEM_REFs invariant address operand after propagation. */
3003 static bool
3004 maybe_canonicalize_mem_ref_addr (tree *t)
3006 bool res = false;
3008 if (TREE_CODE (*t) == ADDR_EXPR)
3009 t = &TREE_OPERAND (*t, 0);
3011 while (handled_component_p (*t))
3012 t = &TREE_OPERAND (*t, 0);
3014 /* Canonicalize MEM [&foo.bar, 0] which appears after propagating
3015 of invariant addresses into a SSA name MEM_REF address. */
3016 if (TREE_CODE (*t) == MEM_REF
3017 || TREE_CODE (*t) == TARGET_MEM_REF)
3019 tree addr = TREE_OPERAND (*t, 0);
3020 if (TREE_CODE (addr) == ADDR_EXPR
3021 && (TREE_CODE (TREE_OPERAND (addr, 0)) == MEM_REF
3022 || handled_component_p (TREE_OPERAND (addr, 0))))
3024 tree base;
3025 HOST_WIDE_INT coffset;
3026 base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
3027 &coffset);
3028 if (!base)
3029 gcc_unreachable ();
3031 TREE_OPERAND (*t, 0) = build_fold_addr_expr (base);
3032 TREE_OPERAND (*t, 1) = int_const_binop (PLUS_EXPR,
3033 TREE_OPERAND (*t, 1),
3034 size_int (coffset));
3035 res = true;
3037 gcc_checking_assert (TREE_CODE (TREE_OPERAND (*t, 0)) == DEBUG_EXPR_DECL
3038 || is_gimple_mem_ref_addr (TREE_OPERAND (*t, 0)));
3041 /* Canonicalize back MEM_REFs to plain reference trees if the object
3042 accessed is a decl that has the same access semantics as the MEM_REF. */
3043 if (TREE_CODE (*t) == MEM_REF
3044 && TREE_CODE (TREE_OPERAND (*t, 0)) == ADDR_EXPR
3045 && integer_zerop (TREE_OPERAND (*t, 1)))
3047 tree decl = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
3048 tree alias_type = TREE_TYPE (TREE_OPERAND (*t, 1));
3049 if (/* Same volatile qualification. */
3050 TREE_THIS_VOLATILE (*t) == TREE_THIS_VOLATILE (decl)
3051 /* Same TBAA behavior with -fstrict-aliasing. */
3052 && !TYPE_REF_CAN_ALIAS_ALL (alias_type)
3053 && (TYPE_MAIN_VARIANT (TREE_TYPE (decl))
3054 == TYPE_MAIN_VARIANT (TREE_TYPE (alias_type)))
3055 /* Same alignment. */
3056 && TYPE_ALIGN (TREE_TYPE (decl)) == TYPE_ALIGN (TREE_TYPE (*t))
3057 /* We have to look out here to not drop a required conversion
3058 from the rhs to the lhs if *t appears on the lhs or vice-versa
3059 if it appears on the rhs. Thus require strict type
3060 compatibility. */
3061 && types_compatible_p (TREE_TYPE (*t), TREE_TYPE (decl)))
3063 *t = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
3064 res = true;
3068 /* Canonicalize TARGET_MEM_REF in particular with respect to
3069 the indexes becoming constant. */
3070 else if (TREE_CODE (*t) == TARGET_MEM_REF)
3072 tree tem = maybe_fold_tmr (*t);
3073 if (tem)
3075 *t = tem;
3076 res = true;
3080 return res;
3083 /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument
3084 distinguishes both cases. */
3086 static bool
3087 fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace, tree (*valueize) (tree))
3089 bool changed = false;
3090 gimple stmt = gsi_stmt (*gsi);
3091 unsigned i;
3093 /* First do required canonicalization of [TARGET_]MEM_REF addresses
3094 after propagation.
3095 ??? This shouldn't be done in generic folding but in the
3096 propagation helpers which also know whether an address was
3097 propagated. */
3098 switch (gimple_code (stmt))
3100 case GIMPLE_ASSIGN:
3101 if (gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
3103 tree *rhs = gimple_assign_rhs1_ptr (stmt);
3104 if ((REFERENCE_CLASS_P (*rhs)
3105 || TREE_CODE (*rhs) == ADDR_EXPR)
3106 && maybe_canonicalize_mem_ref_addr (rhs))
3107 changed = true;
3108 tree *lhs = gimple_assign_lhs_ptr (stmt);
3109 if (REFERENCE_CLASS_P (*lhs)
3110 && maybe_canonicalize_mem_ref_addr (lhs))
3111 changed = true;
3113 break;
3114 case GIMPLE_CALL:
3116 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3118 tree *arg = gimple_call_arg_ptr (stmt, i);
3119 if (REFERENCE_CLASS_P (*arg)
3120 && maybe_canonicalize_mem_ref_addr (arg))
3121 changed = true;
3123 tree *lhs = gimple_call_lhs_ptr (stmt);
3124 if (*lhs
3125 && REFERENCE_CLASS_P (*lhs)
3126 && maybe_canonicalize_mem_ref_addr (lhs))
3127 changed = true;
3128 break;
3130 case GIMPLE_ASM:
3132 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
3134 tree link = gimple_asm_output_op (stmt, i);
3135 tree op = TREE_VALUE (link);
3136 if (REFERENCE_CLASS_P (op)
3137 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
3138 changed = true;
3140 for (i = 0; i < gimple_asm_ninputs (stmt); ++i)
3142 tree link = gimple_asm_input_op (stmt, i);
3143 tree op = TREE_VALUE (link);
3144 if ((REFERENCE_CLASS_P (op)
3145 || TREE_CODE (op) == ADDR_EXPR)
3146 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
3147 changed = true;
3150 break;
3151 case GIMPLE_DEBUG:
3152 if (gimple_debug_bind_p (stmt))
3154 tree *val = gimple_debug_bind_get_value_ptr (stmt);
3155 if (*val
3156 && (REFERENCE_CLASS_P (*val)
3157 || TREE_CODE (*val) == ADDR_EXPR)
3158 && maybe_canonicalize_mem_ref_addr (val))
3159 changed = true;
3161 break;
3162 default:;
3165 /* Dispatch to pattern-based folding. */
3166 if (!inplace
3167 || is_gimple_assign (stmt)
3168 || gimple_code (stmt) == GIMPLE_COND)
3170 gimple_seq seq = NULL;
3171 code_helper rcode;
3172 tree ops[3] = {};
3173 if (gimple_simplify (stmt, &rcode, ops, inplace ? NULL : &seq, valueize))
3175 if (replace_stmt_with_simplification (gsi, rcode, ops, &seq, inplace))
3176 changed = true;
3177 else
3178 gimple_seq_discard (seq);
3182 stmt = gsi_stmt (*gsi);
3184 /* Fold the main computation performed by the statement. */
3185 switch (gimple_code (stmt))
3187 case GIMPLE_ASSIGN:
3189 unsigned old_num_ops = gimple_num_ops (stmt);
3190 enum tree_code subcode = gimple_assign_rhs_code (stmt);
3191 tree lhs = gimple_assign_lhs (stmt);
3192 tree new_rhs;
3193 /* First canonicalize operand order. This avoids building new
3194 trees if this is the only thing fold would later do. */
3195 if ((commutative_tree_code (subcode)
3196 || commutative_ternary_tree_code (subcode))
3197 && tree_swap_operands_p (gimple_assign_rhs1 (stmt),
3198 gimple_assign_rhs2 (stmt), false))
3200 tree tem = gimple_assign_rhs1 (stmt);
3201 gimple_assign_set_rhs1 (stmt, gimple_assign_rhs2 (stmt));
3202 gimple_assign_set_rhs2 (stmt, tem);
3203 changed = true;
3205 new_rhs = fold_gimple_assign (gsi);
3206 if (new_rhs
3207 && !useless_type_conversion_p (TREE_TYPE (lhs),
3208 TREE_TYPE (new_rhs)))
3209 new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
3210 if (new_rhs
3211 && (!inplace
3212 || get_gimple_rhs_num_ops (TREE_CODE (new_rhs)) < old_num_ops))
3214 gimple_assign_set_rhs_from_tree (gsi, new_rhs);
3215 changed = true;
3217 break;
3220 case GIMPLE_COND:
3221 changed |= fold_gimple_cond (stmt);
3222 break;
3224 case GIMPLE_CALL:
3225 changed |= gimple_fold_call (gsi, inplace);
3226 break;
3228 case GIMPLE_ASM:
3229 /* Fold *& in asm operands. */
3231 size_t noutputs;
3232 const char **oconstraints;
3233 const char *constraint;
3234 bool allows_mem, allows_reg;
3236 noutputs = gimple_asm_noutputs (stmt);
3237 oconstraints = XALLOCAVEC (const char *, noutputs);
3239 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
3241 tree link = gimple_asm_output_op (stmt, i);
3242 tree op = TREE_VALUE (link);
3243 oconstraints[i]
3244 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
3245 if (REFERENCE_CLASS_P (op)
3246 && (op = maybe_fold_reference (op, true)) != NULL_TREE)
3248 TREE_VALUE (link) = op;
3249 changed = true;
3252 for (i = 0; i < gimple_asm_ninputs (stmt); ++i)
3254 tree link = gimple_asm_input_op (stmt, i);
3255 tree op = TREE_VALUE (link);
3256 constraint
3257 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
3258 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
3259 oconstraints, &allows_mem, &allows_reg);
3260 if (REFERENCE_CLASS_P (op)
3261 && (op = maybe_fold_reference (op, !allows_reg && allows_mem))
3262 != NULL_TREE)
3264 TREE_VALUE (link) = op;
3265 changed = true;
3269 break;
3271 case GIMPLE_DEBUG:
3272 if (gimple_debug_bind_p (stmt))
3274 tree val = gimple_debug_bind_get_value (stmt);
3275 if (val
3276 && REFERENCE_CLASS_P (val))
3278 tree tem = maybe_fold_reference (val, false);
3279 if (tem)
3281 gimple_debug_bind_set_value (stmt, tem);
3282 changed = true;
3285 else if (val
3286 && TREE_CODE (val) == ADDR_EXPR)
3288 tree ref = TREE_OPERAND (val, 0);
3289 tree tem = maybe_fold_reference (ref, false);
3290 if (tem)
3292 tem = build_fold_addr_expr_with_type (tem, TREE_TYPE (val));
3293 gimple_debug_bind_set_value (stmt, tem);
3294 changed = true;
3298 break;
3300 default:;
3303 stmt = gsi_stmt (*gsi);
3305 /* Fold *& on the lhs. */
3306 if (gimple_has_lhs (stmt))
3308 tree lhs = gimple_get_lhs (stmt);
3309 if (lhs && REFERENCE_CLASS_P (lhs))
3311 tree new_lhs = maybe_fold_reference (lhs, true);
3312 if (new_lhs)
3314 gimple_set_lhs (stmt, new_lhs);
3315 changed = true;
3320 return changed;
3323 /* Valueziation callback that ends up not following SSA edges. */
3325 tree
3326 no_follow_ssa_edges (tree)
3328 return NULL_TREE;
3331 /* Valueization callback that ends up following single-use SSA edges only. */
3333 tree
3334 follow_single_use_edges (tree val)
3336 if (TREE_CODE (val) == SSA_NAME
3337 && !has_single_use (val))
3338 return NULL_TREE;
3339 return val;
3342 /* Fold the statement pointed to by GSI. In some cases, this function may
3343 replace the whole statement with a new one. Returns true iff folding
3344 makes any changes.
3345 The statement pointed to by GSI should be in valid gimple form but may
3346 be in unfolded state as resulting from for example constant propagation
3347 which can produce *&x = 0. */
3349 bool
3350 fold_stmt (gimple_stmt_iterator *gsi)
3352 return fold_stmt_1 (gsi, false, no_follow_ssa_edges);
3355 bool
3356 fold_stmt (gimple_stmt_iterator *gsi, tree (*valueize) (tree))
3358 return fold_stmt_1 (gsi, false, valueize);
3361 /* Perform the minimal folding on statement *GSI. Only operations like
3362 *&x created by constant propagation are handled. The statement cannot
3363 be replaced with a new one. Return true if the statement was
3364 changed, false otherwise.
3365 The statement *GSI should be in valid gimple form but may
3366 be in unfolded state as resulting from for example constant propagation
3367 which can produce *&x = 0. */
3369 bool
3370 fold_stmt_inplace (gimple_stmt_iterator *gsi)
3372 gimple stmt = gsi_stmt (*gsi);
3373 bool changed = fold_stmt_1 (gsi, true, no_follow_ssa_edges);
3374 gcc_assert (gsi_stmt (*gsi) == stmt);
3375 return changed;
3378 /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
3379 if EXPR is null or we don't know how.
3380 If non-null, the result always has boolean type. */
3382 static tree
3383 canonicalize_bool (tree expr, bool invert)
3385 if (!expr)
3386 return NULL_TREE;
3387 else if (invert)
3389 if (integer_nonzerop (expr))
3390 return boolean_false_node;
3391 else if (integer_zerop (expr))
3392 return boolean_true_node;
3393 else if (TREE_CODE (expr) == SSA_NAME)
3394 return fold_build2 (EQ_EXPR, boolean_type_node, expr,
3395 build_int_cst (TREE_TYPE (expr), 0));
3396 else if (TREE_CODE_CLASS (TREE_CODE (expr)) == tcc_comparison)
3397 return fold_build2 (invert_tree_comparison (TREE_CODE (expr), false),
3398 boolean_type_node,
3399 TREE_OPERAND (expr, 0),
3400 TREE_OPERAND (expr, 1));
3401 else
3402 return NULL_TREE;
3404 else
3406 if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
3407 return expr;
3408 if (integer_nonzerop (expr))
3409 return boolean_true_node;
3410 else if (integer_zerop (expr))
3411 return boolean_false_node;
3412 else if (TREE_CODE (expr) == SSA_NAME)
3413 return fold_build2 (NE_EXPR, boolean_type_node, expr,
3414 build_int_cst (TREE_TYPE (expr), 0));
3415 else if (TREE_CODE_CLASS (TREE_CODE (expr)) == tcc_comparison)
3416 return fold_build2 (TREE_CODE (expr),
3417 boolean_type_node,
3418 TREE_OPERAND (expr, 0),
3419 TREE_OPERAND (expr, 1));
3420 else
3421 return NULL_TREE;
3425 /* Check to see if a boolean expression EXPR is logically equivalent to the
3426 comparison (OP1 CODE OP2). Check for various identities involving
3427 SSA_NAMEs. */
3429 static bool
3430 same_bool_comparison_p (const_tree expr, enum tree_code code,
3431 const_tree op1, const_tree op2)
3433 gimple s;
3435 /* The obvious case. */
3436 if (TREE_CODE (expr) == code
3437 && operand_equal_p (TREE_OPERAND (expr, 0), op1, 0)
3438 && operand_equal_p (TREE_OPERAND (expr, 1), op2, 0))
3439 return true;
3441 /* Check for comparing (name, name != 0) and the case where expr
3442 is an SSA_NAME with a definition matching the comparison. */
3443 if (TREE_CODE (expr) == SSA_NAME
3444 && TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
3446 if (operand_equal_p (expr, op1, 0))
3447 return ((code == NE_EXPR && integer_zerop (op2))
3448 || (code == EQ_EXPR && integer_nonzerop (op2)));
3449 s = SSA_NAME_DEF_STMT (expr);
3450 if (is_gimple_assign (s)
3451 && gimple_assign_rhs_code (s) == code
3452 && operand_equal_p (gimple_assign_rhs1 (s), op1, 0)
3453 && operand_equal_p (gimple_assign_rhs2 (s), op2, 0))
3454 return true;
3457 /* If op1 is of the form (name != 0) or (name == 0), and the definition
3458 of name is a comparison, recurse. */
3459 if (TREE_CODE (op1) == SSA_NAME
3460 && TREE_CODE (TREE_TYPE (op1)) == BOOLEAN_TYPE)
3462 s = SSA_NAME_DEF_STMT (op1);
3463 if (is_gimple_assign (s)
3464 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
3466 enum tree_code c = gimple_assign_rhs_code (s);
3467 if ((c == NE_EXPR && integer_zerop (op2))
3468 || (c == EQ_EXPR && integer_nonzerop (op2)))
3469 return same_bool_comparison_p (expr, c,
3470 gimple_assign_rhs1 (s),
3471 gimple_assign_rhs2 (s));
3472 if ((c == EQ_EXPR && integer_zerop (op2))
3473 || (c == NE_EXPR && integer_nonzerop (op2)))
3474 return same_bool_comparison_p (expr,
3475 invert_tree_comparison (c, false),
3476 gimple_assign_rhs1 (s),
3477 gimple_assign_rhs2 (s));
3480 return false;
3483 /* Check to see if two boolean expressions OP1 and OP2 are logically
3484 equivalent. */
3486 static bool
3487 same_bool_result_p (const_tree op1, const_tree op2)
3489 /* Simple cases first. */
3490 if (operand_equal_p (op1, op2, 0))
3491 return true;
3493 /* Check the cases where at least one of the operands is a comparison.
3494 These are a bit smarter than operand_equal_p in that they apply some
3495 identifies on SSA_NAMEs. */
3496 if (TREE_CODE_CLASS (TREE_CODE (op2)) == tcc_comparison
3497 && same_bool_comparison_p (op1, TREE_CODE (op2),
3498 TREE_OPERAND (op2, 0),
3499 TREE_OPERAND (op2, 1)))
3500 return true;
3501 if (TREE_CODE_CLASS (TREE_CODE (op1)) == tcc_comparison
3502 && same_bool_comparison_p (op2, TREE_CODE (op1),
3503 TREE_OPERAND (op1, 0),
3504 TREE_OPERAND (op1, 1)))
3505 return true;
3507 /* Default case. */
3508 return false;
3511 /* Forward declarations for some mutually recursive functions. */
3513 static tree
3514 and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
3515 enum tree_code code2, tree op2a, tree op2b);
3516 static tree
3517 and_var_with_comparison (tree var, bool invert,
3518 enum tree_code code2, tree op2a, tree op2b);
3519 static tree
3520 and_var_with_comparison_1 (gimple stmt,
3521 enum tree_code code2, tree op2a, tree op2b);
3522 static tree
3523 or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
3524 enum tree_code code2, tree op2a, tree op2b);
3525 static tree
3526 or_var_with_comparison (tree var, bool invert,
3527 enum tree_code code2, tree op2a, tree op2b);
3528 static tree
3529 or_var_with_comparison_1 (gimple stmt,
3530 enum tree_code code2, tree op2a, tree op2b);
3532 /* Helper function for and_comparisons_1: try to simplify the AND of the
3533 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
3534 If INVERT is true, invert the value of the VAR before doing the AND.
3535 Return NULL_EXPR if we can't simplify this to a single expression. */
3537 static tree
3538 and_var_with_comparison (tree var, bool invert,
3539 enum tree_code code2, tree op2a, tree op2b)
3541 tree t;
3542 gimple stmt = SSA_NAME_DEF_STMT (var);
3544 /* We can only deal with variables whose definitions are assignments. */
3545 if (!is_gimple_assign (stmt))
3546 return NULL_TREE;
3548 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
3549 !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
3550 Then we only have to consider the simpler non-inverted cases. */
3551 if (invert)
3552 t = or_var_with_comparison_1 (stmt,
3553 invert_tree_comparison (code2, false),
3554 op2a, op2b);
3555 else
3556 t = and_var_with_comparison_1 (stmt, code2, op2a, op2b);
3557 return canonicalize_bool (t, invert);
3560 /* Try to simplify the AND of the ssa variable defined by the assignment
3561 STMT with the comparison specified by (OP2A CODE2 OP2B).
3562 Return NULL_EXPR if we can't simplify this to a single expression. */
3564 static tree
3565 and_var_with_comparison_1 (gimple stmt,
3566 enum tree_code code2, tree op2a, tree op2b)
3568 tree var = gimple_assign_lhs (stmt);
3569 tree true_test_var = NULL_TREE;
3570 tree false_test_var = NULL_TREE;
3571 enum tree_code innercode = gimple_assign_rhs_code (stmt);
3573 /* Check for identities like (var AND (var == 0)) => false. */
3574 if (TREE_CODE (op2a) == SSA_NAME
3575 && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
3577 if ((code2 == NE_EXPR && integer_zerop (op2b))
3578 || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
3580 true_test_var = op2a;
3581 if (var == true_test_var)
3582 return var;
3584 else if ((code2 == EQ_EXPR && integer_zerop (op2b))
3585 || (code2 == NE_EXPR && integer_nonzerop (op2b)))
3587 false_test_var = op2a;
3588 if (var == false_test_var)
3589 return boolean_false_node;
3593 /* If the definition is a comparison, recurse on it. */
3594 if (TREE_CODE_CLASS (innercode) == tcc_comparison)
3596 tree t = and_comparisons_1 (innercode,
3597 gimple_assign_rhs1 (stmt),
3598 gimple_assign_rhs2 (stmt),
3599 code2,
3600 op2a,
3601 op2b);
3602 if (t)
3603 return t;
3606 /* If the definition is an AND or OR expression, we may be able to
3607 simplify by reassociating. */
3608 if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
3609 && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
3611 tree inner1 = gimple_assign_rhs1 (stmt);
3612 tree inner2 = gimple_assign_rhs2 (stmt);
3613 gimple s;
3614 tree t;
3615 tree partial = NULL_TREE;
3616 bool is_and = (innercode == BIT_AND_EXPR);
3618 /* Check for boolean identities that don't require recursive examination
3619 of inner1/inner2:
3620 inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
3621 inner1 AND (inner1 OR inner2) => inner1
3622 !inner1 AND (inner1 AND inner2) => false
3623 !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
3624 Likewise for similar cases involving inner2. */
3625 if (inner1 == true_test_var)
3626 return (is_and ? var : inner1);
3627 else if (inner2 == true_test_var)
3628 return (is_and ? var : inner2);
3629 else if (inner1 == false_test_var)
3630 return (is_and
3631 ? boolean_false_node
3632 : and_var_with_comparison (inner2, false, code2, op2a, op2b));
3633 else if (inner2 == false_test_var)
3634 return (is_and
3635 ? boolean_false_node
3636 : and_var_with_comparison (inner1, false, code2, op2a, op2b));
3638 /* Next, redistribute/reassociate the AND across the inner tests.
3639 Compute the first partial result, (inner1 AND (op2a code op2b)) */
3640 if (TREE_CODE (inner1) == SSA_NAME
3641 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
3642 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
3643 && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s),
3644 gimple_assign_rhs1 (s),
3645 gimple_assign_rhs2 (s),
3646 code2, op2a, op2b)))
3648 /* Handle the AND case, where we are reassociating:
3649 (inner1 AND inner2) AND (op2a code2 op2b)
3650 => (t AND inner2)
3651 If the partial result t is a constant, we win. Otherwise
3652 continue on to try reassociating with the other inner test. */
3653 if (is_and)
3655 if (integer_onep (t))
3656 return inner2;
3657 else if (integer_zerop (t))
3658 return boolean_false_node;
3661 /* Handle the OR case, where we are redistributing:
3662 (inner1 OR inner2) AND (op2a code2 op2b)
3663 => (t OR (inner2 AND (op2a code2 op2b))) */
3664 else if (integer_onep (t))
3665 return boolean_true_node;
3667 /* Save partial result for later. */
3668 partial = t;
3671 /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
3672 if (TREE_CODE (inner2) == SSA_NAME
3673 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
3674 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
3675 && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s),
3676 gimple_assign_rhs1 (s),
3677 gimple_assign_rhs2 (s),
3678 code2, op2a, op2b)))
3680 /* Handle the AND case, where we are reassociating:
3681 (inner1 AND inner2) AND (op2a code2 op2b)
3682 => (inner1 AND t) */
3683 if (is_and)
3685 if (integer_onep (t))
3686 return inner1;
3687 else if (integer_zerop (t))
3688 return boolean_false_node;
3689 /* If both are the same, we can apply the identity
3690 (x AND x) == x. */
3691 else if (partial && same_bool_result_p (t, partial))
3692 return t;
3695 /* Handle the OR case. where we are redistributing:
3696 (inner1 OR inner2) AND (op2a code2 op2b)
3697 => (t OR (inner1 AND (op2a code2 op2b)))
3698 => (t OR partial) */
3699 else
3701 if (integer_onep (t))
3702 return boolean_true_node;
3703 else if (partial)
3705 /* We already got a simplification for the other
3706 operand to the redistributed OR expression. The
3707 interesting case is when at least one is false.
3708 Or, if both are the same, we can apply the identity
3709 (x OR x) == x. */
3710 if (integer_zerop (partial))
3711 return t;
3712 else if (integer_zerop (t))
3713 return partial;
3714 else if (same_bool_result_p (t, partial))
3715 return t;
3720 return NULL_TREE;
3723 /* Try to simplify the AND of two comparisons defined by
3724 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
3725 If this can be done without constructing an intermediate value,
3726 return the resulting tree; otherwise NULL_TREE is returned.
3727 This function is deliberately asymmetric as it recurses on SSA_DEFs
3728 in the first comparison but not the second. */
3730 static tree
3731 and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
3732 enum tree_code code2, tree op2a, tree op2b)
3734 tree truth_type = truth_type_for (TREE_TYPE (op1a));
3736 /* First check for ((x CODE1 y) AND (x CODE2 y)). */
3737 if (operand_equal_p (op1a, op2a, 0)
3738 && operand_equal_p (op1b, op2b, 0))
3740 /* Result will be either NULL_TREE, or a combined comparison. */
3741 tree t = combine_comparisons (UNKNOWN_LOCATION,
3742 TRUTH_ANDIF_EXPR, code1, code2,
3743 truth_type, op1a, op1b);
3744 if (t)
3745 return t;
3748 /* Likewise the swapped case of the above. */
3749 if (operand_equal_p (op1a, op2b, 0)
3750 && operand_equal_p (op1b, op2a, 0))
3752 /* Result will be either NULL_TREE, or a combined comparison. */
3753 tree t = combine_comparisons (UNKNOWN_LOCATION,
3754 TRUTH_ANDIF_EXPR, code1,
3755 swap_tree_comparison (code2),
3756 truth_type, op1a, op1b);
3757 if (t)
3758 return t;
3761 /* If both comparisons are of the same value against constants, we might
3762 be able to merge them. */
3763 if (operand_equal_p (op1a, op2a, 0)
3764 && TREE_CODE (op1b) == INTEGER_CST
3765 && TREE_CODE (op2b) == INTEGER_CST)
3767 int cmp = tree_int_cst_compare (op1b, op2b);
3769 /* If we have (op1a == op1b), we should either be able to
3770 return that or FALSE, depending on whether the constant op1b
3771 also satisfies the other comparison against op2b. */
3772 if (code1 == EQ_EXPR)
3774 bool done = true;
3775 bool val;
3776 switch (code2)
3778 case EQ_EXPR: val = (cmp == 0); break;
3779 case NE_EXPR: val = (cmp != 0); break;
3780 case LT_EXPR: val = (cmp < 0); break;
3781 case GT_EXPR: val = (cmp > 0); break;
3782 case LE_EXPR: val = (cmp <= 0); break;
3783 case GE_EXPR: val = (cmp >= 0); break;
3784 default: done = false;
3786 if (done)
3788 if (val)
3789 return fold_build2 (code1, boolean_type_node, op1a, op1b);
3790 else
3791 return boolean_false_node;
3794 /* Likewise if the second comparison is an == comparison. */
3795 else if (code2 == EQ_EXPR)
3797 bool done = true;
3798 bool val;
3799 switch (code1)
3801 case EQ_EXPR: val = (cmp == 0); break;
3802 case NE_EXPR: val = (cmp != 0); break;
3803 case LT_EXPR: val = (cmp > 0); break;
3804 case GT_EXPR: val = (cmp < 0); break;
3805 case LE_EXPR: val = (cmp >= 0); break;
3806 case GE_EXPR: val = (cmp <= 0); break;
3807 default: done = false;
3809 if (done)
3811 if (val)
3812 return fold_build2 (code2, boolean_type_node, op2a, op2b);
3813 else
3814 return boolean_false_node;
3818 /* Same business with inequality tests. */
3819 else if (code1 == NE_EXPR)
3821 bool val;
3822 switch (code2)
3824 case EQ_EXPR: val = (cmp != 0); break;
3825 case NE_EXPR: val = (cmp == 0); break;
3826 case LT_EXPR: val = (cmp >= 0); break;
3827 case GT_EXPR: val = (cmp <= 0); break;
3828 case LE_EXPR: val = (cmp > 0); break;
3829 case GE_EXPR: val = (cmp < 0); break;
3830 default:
3831 val = false;
3833 if (val)
3834 return fold_build2 (code2, boolean_type_node, op2a, op2b);
3836 else if (code2 == NE_EXPR)
3838 bool val;
3839 switch (code1)
3841 case EQ_EXPR: val = (cmp == 0); break;
3842 case NE_EXPR: val = (cmp != 0); break;
3843 case LT_EXPR: val = (cmp <= 0); break;
3844 case GT_EXPR: val = (cmp >= 0); break;
3845 case LE_EXPR: val = (cmp < 0); break;
3846 case GE_EXPR: val = (cmp > 0); break;
3847 default:
3848 val = false;
3850 if (val)
3851 return fold_build2 (code1, boolean_type_node, op1a, op1b);
3854 /* Chose the more restrictive of two < or <= comparisons. */
3855 else if ((code1 == LT_EXPR || code1 == LE_EXPR)
3856 && (code2 == LT_EXPR || code2 == LE_EXPR))
3858 if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3859 return fold_build2 (code1, boolean_type_node, op1a, op1b);
3860 else
3861 return fold_build2 (code2, boolean_type_node, op2a, op2b);
3864 /* Likewise chose the more restrictive of two > or >= comparisons. */
3865 else if ((code1 == GT_EXPR || code1 == GE_EXPR)
3866 && (code2 == GT_EXPR || code2 == GE_EXPR))
3868 if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3869 return fold_build2 (code1, boolean_type_node, op1a, op1b);
3870 else
3871 return fold_build2 (code2, boolean_type_node, op2a, op2b);
3874 /* Check for singleton ranges. */
3875 else if (cmp == 0
3876 && ((code1 == LE_EXPR && code2 == GE_EXPR)
3877 || (code1 == GE_EXPR && code2 == LE_EXPR)))
3878 return fold_build2 (EQ_EXPR, boolean_type_node, op1a, op2b);
3880 /* Check for disjoint ranges. */
3881 else if (cmp <= 0
3882 && (code1 == LT_EXPR || code1 == LE_EXPR)
3883 && (code2 == GT_EXPR || code2 == GE_EXPR))
3884 return boolean_false_node;
3885 else if (cmp >= 0
3886 && (code1 == GT_EXPR || code1 == GE_EXPR)
3887 && (code2 == LT_EXPR || code2 == LE_EXPR))
3888 return boolean_false_node;
3891 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
3892 NAME's definition is a truth value. See if there are any simplifications
3893 that can be done against the NAME's definition. */
3894 if (TREE_CODE (op1a) == SSA_NAME
3895 && (code1 == NE_EXPR || code1 == EQ_EXPR)
3896 && (integer_zerop (op1b) || integer_onep (op1b)))
3898 bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
3899 || (code1 == NE_EXPR && integer_onep (op1b)));
3900 gimple stmt = SSA_NAME_DEF_STMT (op1a);
3901 switch (gimple_code (stmt))
3903 case GIMPLE_ASSIGN:
3904 /* Try to simplify by copy-propagating the definition. */
3905 return and_var_with_comparison (op1a, invert, code2, op2a, op2b);
3907 case GIMPLE_PHI:
3908 /* If every argument to the PHI produces the same result when
3909 ANDed with the second comparison, we win.
3910 Do not do this unless the type is bool since we need a bool
3911 result here anyway. */
3912 if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
3914 tree result = NULL_TREE;
3915 unsigned i;
3916 for (i = 0; i < gimple_phi_num_args (stmt); i++)
3918 tree arg = gimple_phi_arg_def (stmt, i);
3920 /* If this PHI has itself as an argument, ignore it.
3921 If all the other args produce the same result,
3922 we're still OK. */
3923 if (arg == gimple_phi_result (stmt))
3924 continue;
3925 else if (TREE_CODE (arg) == INTEGER_CST)
3927 if (invert ? integer_nonzerop (arg) : integer_zerop (arg))
3929 if (!result)
3930 result = boolean_false_node;
3931 else if (!integer_zerop (result))
3932 return NULL_TREE;
3934 else if (!result)
3935 result = fold_build2 (code2, boolean_type_node,
3936 op2a, op2b);
3937 else if (!same_bool_comparison_p (result,
3938 code2, op2a, op2b))
3939 return NULL_TREE;
3941 else if (TREE_CODE (arg) == SSA_NAME
3942 && !SSA_NAME_IS_DEFAULT_DEF (arg))
3944 tree temp;
3945 gimple def_stmt = SSA_NAME_DEF_STMT (arg);
3946 /* In simple cases we can look through PHI nodes,
3947 but we have to be careful with loops.
3948 See PR49073. */
3949 if (! dom_info_available_p (CDI_DOMINATORS)
3950 || gimple_bb (def_stmt) == gimple_bb (stmt)
3951 || dominated_by_p (CDI_DOMINATORS,
3952 gimple_bb (def_stmt),
3953 gimple_bb (stmt)))
3954 return NULL_TREE;
3955 temp = and_var_with_comparison (arg, invert, code2,
3956 op2a, op2b);
3957 if (!temp)
3958 return NULL_TREE;
3959 else if (!result)
3960 result = temp;
3961 else if (!same_bool_result_p (result, temp))
3962 return NULL_TREE;
3964 else
3965 return NULL_TREE;
3967 return result;
3970 default:
3971 break;
3974 return NULL_TREE;
3977 /* Try to simplify the AND of two comparisons, specified by
3978 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
3979 If this can be simplified to a single expression (without requiring
3980 introducing more SSA variables to hold intermediate values),
3981 return the resulting tree. Otherwise return NULL_TREE.
3982 If the result expression is non-null, it has boolean type. */
3984 tree
3985 maybe_fold_and_comparisons (enum tree_code code1, tree op1a, tree op1b,
3986 enum tree_code code2, tree op2a, tree op2b)
3988 tree t = and_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b);
3989 if (t)
3990 return t;
3991 else
3992 return and_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b);
3995 /* Helper function for or_comparisons_1: try to simplify the OR of the
3996 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
3997 If INVERT is true, invert the value of VAR before doing the OR.
3998 Return NULL_EXPR if we can't simplify this to a single expression. */
4000 static tree
4001 or_var_with_comparison (tree var, bool invert,
4002 enum tree_code code2, tree op2a, tree op2b)
4004 tree t;
4005 gimple stmt = SSA_NAME_DEF_STMT (var);
4007 /* We can only deal with variables whose definitions are assignments. */
4008 if (!is_gimple_assign (stmt))
4009 return NULL_TREE;
4011 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
4012 !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
4013 Then we only have to consider the simpler non-inverted cases. */
4014 if (invert)
4015 t = and_var_with_comparison_1 (stmt,
4016 invert_tree_comparison (code2, false),
4017 op2a, op2b);
4018 else
4019 t = or_var_with_comparison_1 (stmt, code2, op2a, op2b);
4020 return canonicalize_bool (t, invert);
4023 /* Try to simplify the OR of the ssa variable defined by the assignment
4024 STMT with the comparison specified by (OP2A CODE2 OP2B).
4025 Return NULL_EXPR if we can't simplify this to a single expression. */
4027 static tree
4028 or_var_with_comparison_1 (gimple stmt,
4029 enum tree_code code2, tree op2a, tree op2b)
4031 tree var = gimple_assign_lhs (stmt);
4032 tree true_test_var = NULL_TREE;
4033 tree false_test_var = NULL_TREE;
4034 enum tree_code innercode = gimple_assign_rhs_code (stmt);
4036 /* Check for identities like (var OR (var != 0)) => true . */
4037 if (TREE_CODE (op2a) == SSA_NAME
4038 && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
4040 if ((code2 == NE_EXPR && integer_zerop (op2b))
4041 || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
4043 true_test_var = op2a;
4044 if (var == true_test_var)
4045 return var;
4047 else if ((code2 == EQ_EXPR && integer_zerop (op2b))
4048 || (code2 == NE_EXPR && integer_nonzerop (op2b)))
4050 false_test_var = op2a;
4051 if (var == false_test_var)
4052 return boolean_true_node;
4056 /* If the definition is a comparison, recurse on it. */
4057 if (TREE_CODE_CLASS (innercode) == tcc_comparison)
4059 tree t = or_comparisons_1 (innercode,
4060 gimple_assign_rhs1 (stmt),
4061 gimple_assign_rhs2 (stmt),
4062 code2,
4063 op2a,
4064 op2b);
4065 if (t)
4066 return t;
4069 /* If the definition is an AND or OR expression, we may be able to
4070 simplify by reassociating. */
4071 if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
4072 && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
4074 tree inner1 = gimple_assign_rhs1 (stmt);
4075 tree inner2 = gimple_assign_rhs2 (stmt);
4076 gimple s;
4077 tree t;
4078 tree partial = NULL_TREE;
4079 bool is_or = (innercode == BIT_IOR_EXPR);
4081 /* Check for boolean identities that don't require recursive examination
4082 of inner1/inner2:
4083 inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
4084 inner1 OR (inner1 AND inner2) => inner1
4085 !inner1 OR (inner1 OR inner2) => true
4086 !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
4088 if (inner1 == true_test_var)
4089 return (is_or ? var : inner1);
4090 else if (inner2 == true_test_var)
4091 return (is_or ? var : inner2);
4092 else if (inner1 == false_test_var)
4093 return (is_or
4094 ? boolean_true_node
4095 : or_var_with_comparison (inner2, false, code2, op2a, op2b));
4096 else if (inner2 == false_test_var)
4097 return (is_or
4098 ? boolean_true_node
4099 : or_var_with_comparison (inner1, false, code2, op2a, op2b));
4101 /* Next, redistribute/reassociate the OR across the inner tests.
4102 Compute the first partial result, (inner1 OR (op2a code op2b)) */
4103 if (TREE_CODE (inner1) == SSA_NAME
4104 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
4105 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
4106 && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s),
4107 gimple_assign_rhs1 (s),
4108 gimple_assign_rhs2 (s),
4109 code2, op2a, op2b)))
4111 /* Handle the OR case, where we are reassociating:
4112 (inner1 OR inner2) OR (op2a code2 op2b)
4113 => (t OR inner2)
4114 If the partial result t is a constant, we win. Otherwise
4115 continue on to try reassociating with the other inner test. */
4116 if (is_or)
4118 if (integer_onep (t))
4119 return boolean_true_node;
4120 else if (integer_zerop (t))
4121 return inner2;
4124 /* Handle the AND case, where we are redistributing:
4125 (inner1 AND inner2) OR (op2a code2 op2b)
4126 => (t AND (inner2 OR (op2a code op2b))) */
4127 else if (integer_zerop (t))
4128 return boolean_false_node;
4130 /* Save partial result for later. */
4131 partial = t;
4134 /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
4135 if (TREE_CODE (inner2) == SSA_NAME
4136 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
4137 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
4138 && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s),
4139 gimple_assign_rhs1 (s),
4140 gimple_assign_rhs2 (s),
4141 code2, op2a, op2b)))
4143 /* Handle the OR case, where we are reassociating:
4144 (inner1 OR inner2) OR (op2a code2 op2b)
4145 => (inner1 OR t)
4146 => (t OR partial) */
4147 if (is_or)
4149 if (integer_zerop (t))
4150 return inner1;
4151 else if (integer_onep (t))
4152 return boolean_true_node;
4153 /* If both are the same, we can apply the identity
4154 (x OR x) == x. */
4155 else if (partial && same_bool_result_p (t, partial))
4156 return t;
4159 /* Handle the AND case, where we are redistributing:
4160 (inner1 AND inner2) OR (op2a code2 op2b)
4161 => (t AND (inner1 OR (op2a code2 op2b)))
4162 => (t AND partial) */
4163 else
4165 if (integer_zerop (t))
4166 return boolean_false_node;
4167 else if (partial)
4169 /* We already got a simplification for the other
4170 operand to the redistributed AND expression. The
4171 interesting case is when at least one is true.
4172 Or, if both are the same, we can apply the identity
4173 (x AND x) == x. */
4174 if (integer_onep (partial))
4175 return t;
4176 else if (integer_onep (t))
4177 return partial;
4178 else if (same_bool_result_p (t, partial))
4179 return t;
4184 return NULL_TREE;
4187 /* Try to simplify the OR of two comparisons defined by
4188 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
4189 If this can be done without constructing an intermediate value,
4190 return the resulting tree; otherwise NULL_TREE is returned.
4191 This function is deliberately asymmetric as it recurses on SSA_DEFs
4192 in the first comparison but not the second. */
4194 static tree
4195 or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
4196 enum tree_code code2, tree op2a, tree op2b)
4198 tree truth_type = truth_type_for (TREE_TYPE (op1a));
4200 /* First check for ((x CODE1 y) OR (x CODE2 y)). */
4201 if (operand_equal_p (op1a, op2a, 0)
4202 && operand_equal_p (op1b, op2b, 0))
4204 /* Result will be either NULL_TREE, or a combined comparison. */
4205 tree t = combine_comparisons (UNKNOWN_LOCATION,
4206 TRUTH_ORIF_EXPR, code1, code2,
4207 truth_type, op1a, op1b);
4208 if (t)
4209 return t;
4212 /* Likewise the swapped case of the above. */
4213 if (operand_equal_p (op1a, op2b, 0)
4214 && operand_equal_p (op1b, op2a, 0))
4216 /* Result will be either NULL_TREE, or a combined comparison. */
4217 tree t = combine_comparisons (UNKNOWN_LOCATION,
4218 TRUTH_ORIF_EXPR, code1,
4219 swap_tree_comparison (code2),
4220 truth_type, op1a, op1b);
4221 if (t)
4222 return t;
4225 /* If both comparisons are of the same value against constants, we might
4226 be able to merge them. */
4227 if (operand_equal_p (op1a, op2a, 0)
4228 && TREE_CODE (op1b) == INTEGER_CST
4229 && TREE_CODE (op2b) == INTEGER_CST)
4231 int cmp = tree_int_cst_compare (op1b, op2b);
4233 /* If we have (op1a != op1b), we should either be able to
4234 return that or TRUE, depending on whether the constant op1b
4235 also satisfies the other comparison against op2b. */
4236 if (code1 == NE_EXPR)
4238 bool done = true;
4239 bool val;
4240 switch (code2)
4242 case EQ_EXPR: val = (cmp == 0); break;
4243 case NE_EXPR: val = (cmp != 0); break;
4244 case LT_EXPR: val = (cmp < 0); break;
4245 case GT_EXPR: val = (cmp > 0); break;
4246 case LE_EXPR: val = (cmp <= 0); break;
4247 case GE_EXPR: val = (cmp >= 0); break;
4248 default: done = false;
4250 if (done)
4252 if (val)
4253 return boolean_true_node;
4254 else
4255 return fold_build2 (code1, boolean_type_node, op1a, op1b);
4258 /* Likewise if the second comparison is a != comparison. */
4259 else if (code2 == NE_EXPR)
4261 bool done = true;
4262 bool val;
4263 switch (code1)
4265 case EQ_EXPR: val = (cmp == 0); break;
4266 case NE_EXPR: val = (cmp != 0); break;
4267 case LT_EXPR: val = (cmp > 0); break;
4268 case GT_EXPR: val = (cmp < 0); break;
4269 case LE_EXPR: val = (cmp >= 0); break;
4270 case GE_EXPR: val = (cmp <= 0); break;
4271 default: done = false;
4273 if (done)
4275 if (val)
4276 return boolean_true_node;
4277 else
4278 return fold_build2 (code2, boolean_type_node, op2a, op2b);
4282 /* See if an equality test is redundant with the other comparison. */
4283 else if (code1 == EQ_EXPR)
4285 bool val;
4286 switch (code2)
4288 case EQ_EXPR: val = (cmp == 0); break;
4289 case NE_EXPR: val = (cmp != 0); break;
4290 case LT_EXPR: val = (cmp < 0); break;
4291 case GT_EXPR: val = (cmp > 0); break;
4292 case LE_EXPR: val = (cmp <= 0); break;
4293 case GE_EXPR: val = (cmp >= 0); break;
4294 default:
4295 val = false;
4297 if (val)
4298 return fold_build2 (code2, boolean_type_node, op2a, op2b);
4300 else if (code2 == EQ_EXPR)
4302 bool val;
4303 switch (code1)
4305 case EQ_EXPR: val = (cmp == 0); break;
4306 case NE_EXPR: val = (cmp != 0); break;
4307 case LT_EXPR: val = (cmp > 0); break;
4308 case GT_EXPR: val = (cmp < 0); break;
4309 case LE_EXPR: val = (cmp >= 0); break;
4310 case GE_EXPR: val = (cmp <= 0); break;
4311 default:
4312 val = false;
4314 if (val)
4315 return fold_build2 (code1, boolean_type_node, op1a, op1b);
4318 /* Chose the less restrictive of two < or <= comparisons. */
4319 else if ((code1 == LT_EXPR || code1 == LE_EXPR)
4320 && (code2 == LT_EXPR || code2 == LE_EXPR))
4322 if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
4323 return fold_build2 (code2, boolean_type_node, op2a, op2b);
4324 else
4325 return fold_build2 (code1, boolean_type_node, op1a, op1b);
4328 /* Likewise chose the less restrictive of two > or >= comparisons. */
4329 else if ((code1 == GT_EXPR || code1 == GE_EXPR)
4330 && (code2 == GT_EXPR || code2 == GE_EXPR))
4332 if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
4333 return fold_build2 (code2, boolean_type_node, op2a, op2b);
4334 else
4335 return fold_build2 (code1, boolean_type_node, op1a, op1b);
4338 /* Check for singleton ranges. */
4339 else if (cmp == 0
4340 && ((code1 == LT_EXPR && code2 == GT_EXPR)
4341 || (code1 == GT_EXPR && code2 == LT_EXPR)))
4342 return fold_build2 (NE_EXPR, boolean_type_node, op1a, op2b);
4344 /* Check for less/greater pairs that don't restrict the range at all. */
4345 else if (cmp >= 0
4346 && (code1 == LT_EXPR || code1 == LE_EXPR)
4347 && (code2 == GT_EXPR || code2 == GE_EXPR))
4348 return boolean_true_node;
4349 else if (cmp <= 0
4350 && (code1 == GT_EXPR || code1 == GE_EXPR)
4351 && (code2 == LT_EXPR || code2 == LE_EXPR))
4352 return boolean_true_node;
4355 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
4356 NAME's definition is a truth value. See if there are any simplifications
4357 that can be done against the NAME's definition. */
4358 if (TREE_CODE (op1a) == SSA_NAME
4359 && (code1 == NE_EXPR || code1 == EQ_EXPR)
4360 && (integer_zerop (op1b) || integer_onep (op1b)))
4362 bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
4363 || (code1 == NE_EXPR && integer_onep (op1b)));
4364 gimple stmt = SSA_NAME_DEF_STMT (op1a);
4365 switch (gimple_code (stmt))
4367 case GIMPLE_ASSIGN:
4368 /* Try to simplify by copy-propagating the definition. */
4369 return or_var_with_comparison (op1a, invert, code2, op2a, op2b);
4371 case GIMPLE_PHI:
4372 /* If every argument to the PHI produces the same result when
4373 ORed with the second comparison, we win.
4374 Do not do this unless the type is bool since we need a bool
4375 result here anyway. */
4376 if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
4378 tree result = NULL_TREE;
4379 unsigned i;
4380 for (i = 0; i < gimple_phi_num_args (stmt); i++)
4382 tree arg = gimple_phi_arg_def (stmt, i);
4384 /* If this PHI has itself as an argument, ignore it.
4385 If all the other args produce the same result,
4386 we're still OK. */
4387 if (arg == gimple_phi_result (stmt))
4388 continue;
4389 else if (TREE_CODE (arg) == INTEGER_CST)
4391 if (invert ? integer_zerop (arg) : integer_nonzerop (arg))
4393 if (!result)
4394 result = boolean_true_node;
4395 else if (!integer_onep (result))
4396 return NULL_TREE;
4398 else if (!result)
4399 result = fold_build2 (code2, boolean_type_node,
4400 op2a, op2b);
4401 else if (!same_bool_comparison_p (result,
4402 code2, op2a, op2b))
4403 return NULL_TREE;
4405 else if (TREE_CODE (arg) == SSA_NAME
4406 && !SSA_NAME_IS_DEFAULT_DEF (arg))
4408 tree temp;
4409 gimple def_stmt = SSA_NAME_DEF_STMT (arg);
4410 /* In simple cases we can look through PHI nodes,
4411 but we have to be careful with loops.
4412 See PR49073. */
4413 if (! dom_info_available_p (CDI_DOMINATORS)
4414 || gimple_bb (def_stmt) == gimple_bb (stmt)
4415 || dominated_by_p (CDI_DOMINATORS,
4416 gimple_bb (def_stmt),
4417 gimple_bb (stmt)))
4418 return NULL_TREE;
4419 temp = or_var_with_comparison (arg, invert, code2,
4420 op2a, op2b);
4421 if (!temp)
4422 return NULL_TREE;
4423 else if (!result)
4424 result = temp;
4425 else if (!same_bool_result_p (result, temp))
4426 return NULL_TREE;
4428 else
4429 return NULL_TREE;
4431 return result;
4434 default:
4435 break;
4438 return NULL_TREE;
4441 /* Try to simplify the OR of two comparisons, specified by
4442 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
4443 If this can be simplified to a single expression (without requiring
4444 introducing more SSA variables to hold intermediate values),
4445 return the resulting tree. Otherwise return NULL_TREE.
4446 If the result expression is non-null, it has boolean type. */
4448 tree
4449 maybe_fold_or_comparisons (enum tree_code code1, tree op1a, tree op1b,
4450 enum tree_code code2, tree op2a, tree op2b)
4452 tree t = or_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b);
4453 if (t)
4454 return t;
4455 else
4456 return or_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b);
4460 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
4462 Either NULL_TREE, a simplified but non-constant or a constant
4463 is returned.
4465 ??? This should go into a gimple-fold-inline.h file to be eventually
4466 privatized with the single valueize function used in the various TUs
4467 to avoid the indirect function call overhead. */
4469 tree
4470 gimple_fold_stmt_to_constant_1 (gimple stmt, tree (*valueize) (tree))
4472 code_helper rcode;
4473 tree ops[3] = {};
4474 /* ??? The SSA propagators do not correctly deal with following SSA use-def
4475 edges if there are intermediate VARYING defs. For this reason
4476 do not follow SSA edges here even though SCCVN can technically
4477 just deal fine with that. */
4478 if (gimple_simplify (stmt, &rcode, ops, NULL, no_follow_ssa_edges)
4479 && rcode.is_tree_code ()
4480 && (TREE_CODE_LENGTH ((tree_code) rcode) == 0
4481 || ((tree_code) rcode) == ADDR_EXPR)
4482 && is_gimple_val (ops[0]))
4484 tree res = ops[0];
4485 if (dump_file && dump_flags & TDF_DETAILS)
4487 fprintf (dump_file, "Match-and-simplified ");
4488 print_gimple_expr (dump_file, stmt, 0, TDF_SLIM);
4489 fprintf (dump_file, " to ");
4490 print_generic_expr (dump_file, res, 0);
4491 fprintf (dump_file, "\n");
4493 return res;
4496 location_t loc = gimple_location (stmt);
4497 switch (gimple_code (stmt))
4499 case GIMPLE_ASSIGN:
4501 enum tree_code subcode = gimple_assign_rhs_code (stmt);
4503 switch (get_gimple_rhs_class (subcode))
4505 case GIMPLE_SINGLE_RHS:
4507 tree rhs = gimple_assign_rhs1 (stmt);
4508 enum tree_code_class kind = TREE_CODE_CLASS (subcode);
4510 if (TREE_CODE (rhs) == SSA_NAME)
4512 /* If the RHS is an SSA_NAME, return its known constant value,
4513 if any. */
4514 return (*valueize) (rhs);
4516 /* Handle propagating invariant addresses into address
4517 operations. */
4518 else if (TREE_CODE (rhs) == ADDR_EXPR
4519 && !is_gimple_min_invariant (rhs))
4521 HOST_WIDE_INT offset = 0;
4522 tree base;
4523 base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs, 0),
4524 &offset,
4525 valueize);
4526 if (base
4527 && (CONSTANT_CLASS_P (base)
4528 || decl_address_invariant_p (base)))
4529 return build_invariant_address (TREE_TYPE (rhs),
4530 base, offset);
4532 else if (TREE_CODE (rhs) == CONSTRUCTOR
4533 && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE
4534 && (CONSTRUCTOR_NELTS (rhs)
4535 == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs))))
4537 unsigned i;
4538 tree val, *vec;
4540 vec = XALLOCAVEC (tree,
4541 TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)));
4542 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
4544 val = (*valueize) (val);
4545 if (TREE_CODE (val) == INTEGER_CST
4546 || TREE_CODE (val) == REAL_CST
4547 || TREE_CODE (val) == FIXED_CST)
4548 vec[i] = val;
4549 else
4550 return NULL_TREE;
4553 return build_vector (TREE_TYPE (rhs), vec);
4555 if (subcode == OBJ_TYPE_REF)
4557 tree val = (*valueize) (OBJ_TYPE_REF_EXPR (rhs));
4558 /* If callee is constant, we can fold away the wrapper. */
4559 if (is_gimple_min_invariant (val))
4560 return val;
4563 if (kind == tcc_reference)
4565 if ((TREE_CODE (rhs) == VIEW_CONVERT_EXPR
4566 || TREE_CODE (rhs) == REALPART_EXPR
4567 || TREE_CODE (rhs) == IMAGPART_EXPR)
4568 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
4570 tree val = (*valueize) (TREE_OPERAND (rhs, 0));
4571 return fold_unary_loc (EXPR_LOCATION (rhs),
4572 TREE_CODE (rhs),
4573 TREE_TYPE (rhs), val);
4575 else if (TREE_CODE (rhs) == BIT_FIELD_REF
4576 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
4578 tree val = (*valueize) (TREE_OPERAND (rhs, 0));
4579 return fold_ternary_loc (EXPR_LOCATION (rhs),
4580 TREE_CODE (rhs),
4581 TREE_TYPE (rhs), val,
4582 TREE_OPERAND (rhs, 1),
4583 TREE_OPERAND (rhs, 2));
4585 else if (TREE_CODE (rhs) == MEM_REF
4586 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
4588 tree val = (*valueize) (TREE_OPERAND (rhs, 0));
4589 if (TREE_CODE (val) == ADDR_EXPR
4590 && is_gimple_min_invariant (val))
4592 tree tem = fold_build2 (MEM_REF, TREE_TYPE (rhs),
4593 unshare_expr (val),
4594 TREE_OPERAND (rhs, 1));
4595 if (tem)
4596 rhs = tem;
4599 return fold_const_aggregate_ref_1 (rhs, valueize);
4601 else if (kind == tcc_declaration)
4602 return get_symbol_constant_value (rhs);
4603 return rhs;
4606 case GIMPLE_UNARY_RHS:
4607 return NULL_TREE;
4609 case GIMPLE_BINARY_RHS:
4611 /* Handle binary operators that can appear in GIMPLE form. */
4612 tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
4613 tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
4615 /* Translate &x + CST into an invariant form suitable for
4616 further propagation. */
4617 if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
4618 && TREE_CODE (op0) == ADDR_EXPR
4619 && TREE_CODE (op1) == INTEGER_CST)
4621 tree off = fold_convert (ptr_type_node, op1);
4622 return build_fold_addr_expr_loc
4623 (loc,
4624 fold_build2 (MEM_REF,
4625 TREE_TYPE (TREE_TYPE (op0)),
4626 unshare_expr (op0), off));
4629 return fold_binary_loc (loc, subcode,
4630 gimple_expr_type (stmt), op0, op1);
4633 case GIMPLE_TERNARY_RHS:
4635 /* Handle ternary operators that can appear in GIMPLE form. */
4636 tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
4637 tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
4638 tree op2 = (*valueize) (gimple_assign_rhs3 (stmt));
4640 /* Fold embedded expressions in ternary codes. */
4641 if ((subcode == COND_EXPR
4642 || subcode == VEC_COND_EXPR)
4643 && COMPARISON_CLASS_P (op0))
4645 tree op00 = (*valueize) (TREE_OPERAND (op0, 0));
4646 tree op01 = (*valueize) (TREE_OPERAND (op0, 1));
4647 tree tem = fold_binary_loc (loc, TREE_CODE (op0),
4648 TREE_TYPE (op0), op00, op01);
4649 if (tem)
4650 op0 = tem;
4653 return fold_ternary_loc (loc, subcode,
4654 gimple_expr_type (stmt), op0, op1, op2);
4657 default:
4658 gcc_unreachable ();
4662 case GIMPLE_CALL:
4664 tree fn;
4666 if (gimple_call_internal_p (stmt))
4668 enum tree_code subcode = ERROR_MARK;
4669 switch (gimple_call_internal_fn (stmt))
4671 case IFN_UBSAN_CHECK_ADD:
4672 subcode = PLUS_EXPR;
4673 break;
4674 case IFN_UBSAN_CHECK_SUB:
4675 subcode = MINUS_EXPR;
4676 break;
4677 case IFN_UBSAN_CHECK_MUL:
4678 subcode = MULT_EXPR;
4679 break;
4680 default:
4681 return NULL_TREE;
4683 tree arg0 = gimple_call_arg (stmt, 0);
4684 tree arg1 = gimple_call_arg (stmt, 1);
4685 tree op0 = (*valueize) (arg0);
4686 tree op1 = (*valueize) (arg1);
4688 if (TREE_CODE (op0) != INTEGER_CST
4689 || TREE_CODE (op1) != INTEGER_CST)
4691 switch (subcode)
4693 case MULT_EXPR:
4694 /* x * 0 = 0 * x = 0 without overflow. */
4695 if (integer_zerop (op0) || integer_zerop (op1))
4696 return build_zero_cst (TREE_TYPE (arg0));
4697 break;
4698 case MINUS_EXPR:
4699 /* y - y = 0 without overflow. */
4700 if (operand_equal_p (op0, op1, 0))
4701 return build_zero_cst (TREE_TYPE (arg0));
4702 break;
4703 default:
4704 break;
4707 tree res
4708 = fold_binary_loc (loc, subcode, TREE_TYPE (arg0), op0, op1);
4709 if (res
4710 && TREE_CODE (res) == INTEGER_CST
4711 && !TREE_OVERFLOW (res))
4712 return res;
4713 return NULL_TREE;
4716 fn = (*valueize) (gimple_call_fn (stmt));
4717 if (TREE_CODE (fn) == ADDR_EXPR
4718 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4719 && DECL_BUILT_IN (TREE_OPERAND (fn, 0))
4720 && gimple_builtin_call_types_compatible_p (stmt,
4721 TREE_OPERAND (fn, 0)))
4723 tree *args = XALLOCAVEC (tree, gimple_call_num_args (stmt));
4724 tree call, retval;
4725 unsigned i;
4726 for (i = 0; i < gimple_call_num_args (stmt); ++i)
4727 args[i] = (*valueize) (gimple_call_arg (stmt, i));
4728 call = build_call_array_loc (loc,
4729 gimple_call_return_type (stmt),
4730 fn, gimple_call_num_args (stmt), args);
4731 retval = fold_call_expr (EXPR_LOCATION (call), call, false);
4732 if (retval)
4734 /* fold_call_expr wraps the result inside a NOP_EXPR. */
4735 STRIP_NOPS (retval);
4736 retval = fold_convert (gimple_call_return_type (stmt), retval);
4738 return retval;
4740 return NULL_TREE;
4743 default:
4744 return NULL_TREE;
4748 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
4749 Returns NULL_TREE if folding to a constant is not possible, otherwise
4750 returns a constant according to is_gimple_min_invariant. */
4752 tree
4753 gimple_fold_stmt_to_constant (gimple stmt, tree (*valueize) (tree))
4755 tree res = gimple_fold_stmt_to_constant_1 (stmt, valueize);
4756 if (res && is_gimple_min_invariant (res))
4757 return res;
4758 return NULL_TREE;
4762 /* The following set of functions are supposed to fold references using
4763 their constant initializers. */
4765 static tree fold_ctor_reference (tree type, tree ctor,
4766 unsigned HOST_WIDE_INT offset,
4767 unsigned HOST_WIDE_INT size, tree);
4769 /* See if we can find constructor defining value of BASE.
4770 When we know the consructor with constant offset (such as
4771 base is array[40] and we do know constructor of array), then
4772 BIT_OFFSET is adjusted accordingly.
4774 As a special case, return error_mark_node when constructor
4775 is not explicitly available, but it is known to be zero
4776 such as 'static const int a;'. */
4777 static tree
4778 get_base_constructor (tree base, HOST_WIDE_INT *bit_offset,
4779 tree (*valueize)(tree))
4781 HOST_WIDE_INT bit_offset2, size, max_size;
4782 if (TREE_CODE (base) == MEM_REF)
4784 if (!integer_zerop (TREE_OPERAND (base, 1)))
4786 if (!tree_fits_shwi_p (TREE_OPERAND (base, 1)))
4787 return NULL_TREE;
4788 *bit_offset += (mem_ref_offset (base).to_short_addr ()
4789 * BITS_PER_UNIT);
4792 if (valueize
4793 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
4794 base = valueize (TREE_OPERAND (base, 0));
4795 if (!base || TREE_CODE (base) != ADDR_EXPR)
4796 return NULL_TREE;
4797 base = TREE_OPERAND (base, 0);
4800 /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
4801 DECL_INITIAL. If BASE is a nested reference into another
4802 ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
4803 the inner reference. */
4804 switch (TREE_CODE (base))
4806 case VAR_DECL:
4807 case CONST_DECL:
4809 tree init = ctor_for_folding (base);
4811 /* Our semantic is exact opposite of ctor_for_folding;
4812 NULL means unknown, while error_mark_node is 0. */
4813 if (init == error_mark_node)
4814 return NULL_TREE;
4815 if (!init)
4816 return error_mark_node;
4817 return init;
4820 case ARRAY_REF:
4821 case COMPONENT_REF:
4822 base = get_ref_base_and_extent (base, &bit_offset2, &size, &max_size);
4823 if (max_size == -1 || size != max_size)
4824 return NULL_TREE;
4825 *bit_offset += bit_offset2;
4826 return get_base_constructor (base, bit_offset, valueize);
4828 case STRING_CST:
4829 case CONSTRUCTOR:
4830 return base;
4832 default:
4833 return NULL_TREE;
4837 /* CTOR is CONSTRUCTOR of an array type. Fold reference of type TYPE and size
4838 SIZE to the memory at bit OFFSET. */
4840 static tree
4841 fold_array_ctor_reference (tree type, tree ctor,
4842 unsigned HOST_WIDE_INT offset,
4843 unsigned HOST_WIDE_INT size,
4844 tree from_decl)
4846 unsigned HOST_WIDE_INT cnt;
4847 tree cfield, cval;
4848 offset_int low_bound;
4849 offset_int elt_size;
4850 offset_int index, max_index;
4851 offset_int access_index;
4852 tree domain_type = NULL_TREE, index_type = NULL_TREE;
4853 HOST_WIDE_INT inner_offset;
4855 /* Compute low bound and elt size. */
4856 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
4857 domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
4858 if (domain_type && TYPE_MIN_VALUE (domain_type))
4860 /* Static constructors for variably sized objects makes no sense. */
4861 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
4862 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
4863 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
4865 else
4866 low_bound = 0;
4867 /* Static constructors for variably sized objects makes no sense. */
4868 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor))))
4869 == INTEGER_CST);
4870 elt_size = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor))));
4872 /* We can handle only constantly sized accesses that are known to not
4873 be larger than size of array element. */
4874 if (!TYPE_SIZE_UNIT (type)
4875 || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
4876 || wi::lts_p (elt_size, wi::to_offset (TYPE_SIZE_UNIT (type)))
4877 || elt_size == 0)
4878 return NULL_TREE;
4880 /* Compute the array index we look for. */
4881 access_index = wi::udiv_trunc (offset_int (offset / BITS_PER_UNIT),
4882 elt_size);
4883 access_index += low_bound;
4884 if (index_type)
4885 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
4886 TYPE_SIGN (index_type));
4888 /* And offset within the access. */
4889 inner_offset = offset % (elt_size.to_uhwi () * BITS_PER_UNIT);
4891 /* See if the array field is large enough to span whole access. We do not
4892 care to fold accesses spanning multiple array indexes. */
4893 if (inner_offset + size > elt_size.to_uhwi () * BITS_PER_UNIT)
4894 return NULL_TREE;
4896 index = low_bound - 1;
4897 if (index_type)
4898 index = wi::ext (index, TYPE_PRECISION (index_type),
4899 TYPE_SIGN (index_type));
4901 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
4903 /* Array constructor might explicitely set index, or specify range
4904 or leave index NULL meaning that it is next index after previous
4905 one. */
4906 if (cfield)
4908 if (TREE_CODE (cfield) == INTEGER_CST)
4909 max_index = index = wi::to_offset (cfield);
4910 else
4912 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
4913 index = wi::to_offset (TREE_OPERAND (cfield, 0));
4914 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
4917 else
4919 index += 1;
4920 if (index_type)
4921 index = wi::ext (index, TYPE_PRECISION (index_type),
4922 TYPE_SIGN (index_type));
4923 max_index = index;
4926 /* Do we have match? */
4927 if (wi::cmpu (access_index, index) >= 0
4928 && wi::cmpu (access_index, max_index) <= 0)
4929 return fold_ctor_reference (type, cval, inner_offset, size,
4930 from_decl);
4932 /* When memory is not explicitely mentioned in constructor,
4933 it is 0 (or out of range). */
4934 return build_zero_cst (type);
4937 /* CTOR is CONSTRUCTOR of an aggregate or vector.
4938 Fold reference of type TYPE and size SIZE to the memory at bit OFFSET. */
4940 static tree
4941 fold_nonarray_ctor_reference (tree type, tree ctor,
4942 unsigned HOST_WIDE_INT offset,
4943 unsigned HOST_WIDE_INT size,
4944 tree from_decl)
4946 unsigned HOST_WIDE_INT cnt;
4947 tree cfield, cval;
4949 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield,
4950 cval)
4952 tree byte_offset = DECL_FIELD_OFFSET (cfield);
4953 tree field_offset = DECL_FIELD_BIT_OFFSET (cfield);
4954 tree field_size = DECL_SIZE (cfield);
4955 offset_int bitoffset;
4956 offset_int bitoffset_end, access_end;
4958 /* Variable sized objects in static constructors makes no sense,
4959 but field_size can be NULL for flexible array members. */
4960 gcc_assert (TREE_CODE (field_offset) == INTEGER_CST
4961 && TREE_CODE (byte_offset) == INTEGER_CST
4962 && (field_size != NULL_TREE
4963 ? TREE_CODE (field_size) == INTEGER_CST
4964 : TREE_CODE (TREE_TYPE (cfield)) == ARRAY_TYPE));
4966 /* Compute bit offset of the field. */
4967 bitoffset = (wi::to_offset (field_offset)
4968 + wi::lshift (wi::to_offset (byte_offset),
4969 LOG2_BITS_PER_UNIT));
4970 /* Compute bit offset where the field ends. */
4971 if (field_size != NULL_TREE)
4972 bitoffset_end = bitoffset + wi::to_offset (field_size);
4973 else
4974 bitoffset_end = 0;
4976 access_end = offset_int (offset) + size;
4978 /* Is there any overlap between [OFFSET, OFFSET+SIZE) and
4979 [BITOFFSET, BITOFFSET_END)? */
4980 if (wi::cmps (access_end, bitoffset) > 0
4981 && (field_size == NULL_TREE
4982 || wi::lts_p (offset, bitoffset_end)))
4984 offset_int inner_offset = offset_int (offset) - bitoffset;
4985 /* We do have overlap. Now see if field is large enough to
4986 cover the access. Give up for accesses spanning multiple
4987 fields. */
4988 if (wi::cmps (access_end, bitoffset_end) > 0)
4989 return NULL_TREE;
4990 if (wi::lts_p (offset, bitoffset))
4991 return NULL_TREE;
4992 return fold_ctor_reference (type, cval,
4993 inner_offset.to_uhwi (), size,
4994 from_decl);
4997 /* When memory is not explicitely mentioned in constructor, it is 0. */
4998 return build_zero_cst (type);
5001 /* CTOR is value initializing memory, fold reference of type TYPE and size SIZE
5002 to the memory at bit OFFSET. */
5004 static tree
5005 fold_ctor_reference (tree type, tree ctor, unsigned HOST_WIDE_INT offset,
5006 unsigned HOST_WIDE_INT size, tree from_decl)
5008 tree ret;
5010 /* We found the field with exact match. */
5011 if (useless_type_conversion_p (type, TREE_TYPE (ctor))
5012 && !offset)
5013 return canonicalize_constructor_val (unshare_expr (ctor), from_decl);
5015 /* We are at the end of walk, see if we can view convert the
5016 result. */
5017 if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor)) && !offset
5018 /* VIEW_CONVERT_EXPR is defined only for matching sizes. */
5019 && !compare_tree_int (TYPE_SIZE (type), size)
5020 && !compare_tree_int (TYPE_SIZE (TREE_TYPE (ctor)), size))
5022 ret = canonicalize_constructor_val (unshare_expr (ctor), from_decl);
5023 ret = fold_unary (VIEW_CONVERT_EXPR, type, ret);
5024 if (ret)
5025 STRIP_NOPS (ret);
5026 return ret;
5028 /* For constants and byte-aligned/sized reads try to go through
5029 native_encode/interpret. */
5030 if (CONSTANT_CLASS_P (ctor)
5031 && BITS_PER_UNIT == 8
5032 && offset % BITS_PER_UNIT == 0
5033 && size % BITS_PER_UNIT == 0
5034 && size <= MAX_BITSIZE_MODE_ANY_MODE)
5036 unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
5037 if (native_encode_expr (ctor, buf, size / BITS_PER_UNIT,
5038 offset / BITS_PER_UNIT) > 0)
5039 return native_interpret_expr (type, buf, size / BITS_PER_UNIT);
5041 if (TREE_CODE (ctor) == CONSTRUCTOR)
5044 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE
5045 || TREE_CODE (TREE_TYPE (ctor)) == VECTOR_TYPE)
5046 return fold_array_ctor_reference (type, ctor, offset, size,
5047 from_decl);
5048 else
5049 return fold_nonarray_ctor_reference (type, ctor, offset, size,
5050 from_decl);
5053 return NULL_TREE;
5056 /* Return the tree representing the element referenced by T if T is an
5057 ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
5058 names using VALUEIZE. Return NULL_TREE otherwise. */
5060 tree
5061 fold_const_aggregate_ref_1 (tree t, tree (*valueize) (tree))
5063 tree ctor, idx, base;
5064 HOST_WIDE_INT offset, size, max_size;
5065 tree tem;
5067 if (TREE_THIS_VOLATILE (t))
5068 return NULL_TREE;
5070 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
5071 return get_symbol_constant_value (t);
5073 tem = fold_read_from_constant_string (t);
5074 if (tem)
5075 return tem;
5077 switch (TREE_CODE (t))
5079 case ARRAY_REF:
5080 case ARRAY_RANGE_REF:
5081 /* Constant indexes are handled well by get_base_constructor.
5082 Only special case variable offsets.
5083 FIXME: This code can't handle nested references with variable indexes
5084 (they will be handled only by iteration of ccp). Perhaps we can bring
5085 get_ref_base_and_extent here and make it use a valueize callback. */
5086 if (TREE_CODE (TREE_OPERAND (t, 1)) == SSA_NAME
5087 && valueize
5088 && (idx = (*valueize) (TREE_OPERAND (t, 1)))
5089 && TREE_CODE (idx) == INTEGER_CST)
5091 tree low_bound, unit_size;
5093 /* If the resulting bit-offset is constant, track it. */
5094 if ((low_bound = array_ref_low_bound (t),
5095 TREE_CODE (low_bound) == INTEGER_CST)
5096 && (unit_size = array_ref_element_size (t),
5097 tree_fits_uhwi_p (unit_size)))
5099 offset_int woffset
5100 = wi::sext (wi::to_offset (idx) - wi::to_offset (low_bound),
5101 TYPE_PRECISION (TREE_TYPE (idx)));
5103 if (wi::fits_shwi_p (woffset))
5105 offset = woffset.to_shwi ();
5106 /* TODO: This code seems wrong, multiply then check
5107 to see if it fits. */
5108 offset *= tree_to_uhwi (unit_size);
5109 offset *= BITS_PER_UNIT;
5111 base = TREE_OPERAND (t, 0);
5112 ctor = get_base_constructor (base, &offset, valueize);
5113 /* Empty constructor. Always fold to 0. */
5114 if (ctor == error_mark_node)
5115 return build_zero_cst (TREE_TYPE (t));
5116 /* Out of bound array access. Value is undefined,
5117 but don't fold. */
5118 if (offset < 0)
5119 return NULL_TREE;
5120 /* We can not determine ctor. */
5121 if (!ctor)
5122 return NULL_TREE;
5123 return fold_ctor_reference (TREE_TYPE (t), ctor, offset,
5124 tree_to_uhwi (unit_size)
5125 * BITS_PER_UNIT,
5126 base);
5130 /* Fallthru. */
5132 case COMPONENT_REF:
5133 case BIT_FIELD_REF:
5134 case TARGET_MEM_REF:
5135 case MEM_REF:
5136 base = get_ref_base_and_extent (t, &offset, &size, &max_size);
5137 ctor = get_base_constructor (base, &offset, valueize);
5139 /* Empty constructor. Always fold to 0. */
5140 if (ctor == error_mark_node)
5141 return build_zero_cst (TREE_TYPE (t));
5142 /* We do not know precise address. */
5143 if (max_size == -1 || max_size != size)
5144 return NULL_TREE;
5145 /* We can not determine ctor. */
5146 if (!ctor)
5147 return NULL_TREE;
5149 /* Out of bound array access. Value is undefined, but don't fold. */
5150 if (offset < 0)
5151 return NULL_TREE;
5153 return fold_ctor_reference (TREE_TYPE (t), ctor, offset, size,
5154 base);
5156 case REALPART_EXPR:
5157 case IMAGPART_EXPR:
5159 tree c = fold_const_aggregate_ref_1 (TREE_OPERAND (t, 0), valueize);
5160 if (c && TREE_CODE (c) == COMPLEX_CST)
5161 return fold_build1_loc (EXPR_LOCATION (t),
5162 TREE_CODE (t), TREE_TYPE (t), c);
5163 break;
5166 default:
5167 break;
5170 return NULL_TREE;
5173 tree
5174 fold_const_aggregate_ref (tree t)
5176 return fold_const_aggregate_ref_1 (t, NULL);
5179 /* Lookup virtual method with index TOKEN in a virtual table V
5180 at OFFSET.
5181 Set CAN_REFER if non-NULL to false if method
5182 is not referable or if the virtual table is ill-formed (such as rewriten
5183 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
5185 tree
5186 gimple_get_virt_method_for_vtable (HOST_WIDE_INT token,
5187 tree v,
5188 unsigned HOST_WIDE_INT offset,
5189 bool *can_refer)
5191 tree vtable = v, init, fn;
5192 unsigned HOST_WIDE_INT size;
5193 unsigned HOST_WIDE_INT elt_size, access_index;
5194 tree domain_type;
5196 if (can_refer)
5197 *can_refer = true;
5199 /* First of all double check we have virtual table. */
5200 if (TREE_CODE (v) != VAR_DECL
5201 || !DECL_VIRTUAL_P (v))
5203 gcc_assert (in_lto_p);
5204 /* Pass down that we lost track of the target. */
5205 if (can_refer)
5206 *can_refer = false;
5207 return NULL_TREE;
5210 init = ctor_for_folding (v);
5212 /* The virtual tables should always be born with constructors
5213 and we always should assume that they are avaialble for
5214 folding. At the moment we do not stream them in all cases,
5215 but it should never happen that ctor seem unreachable. */
5216 gcc_assert (init);
5217 if (init == error_mark_node)
5219 gcc_assert (in_lto_p);
5220 /* Pass down that we lost track of the target. */
5221 if (can_refer)
5222 *can_refer = false;
5223 return NULL_TREE;
5225 gcc_checking_assert (TREE_CODE (TREE_TYPE (v)) == ARRAY_TYPE);
5226 size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v))));
5227 offset *= BITS_PER_UNIT;
5228 offset += token * size;
5230 /* Lookup the value in the constructor that is assumed to be array.
5231 This is equivalent to
5232 fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
5233 offset, size, NULL);
5234 but in a constant time. We expect that frontend produced a simple
5235 array without indexed initializers. */
5237 gcc_checking_assert (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE);
5238 domain_type = TYPE_DOMAIN (TREE_TYPE (init));
5239 gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type)));
5240 elt_size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init))));
5242 access_index = offset / BITS_PER_UNIT / elt_size;
5243 gcc_checking_assert (offset % (elt_size * BITS_PER_UNIT) == 0);
5245 /* This code makes an assumption that there are no
5246 indexed fileds produced by C++ FE, so we can directly index the array. */
5247 if (access_index < CONSTRUCTOR_NELTS (init))
5249 fn = CONSTRUCTOR_ELT (init, access_index)->value;
5250 gcc_checking_assert (!CONSTRUCTOR_ELT (init, access_index)->index);
5251 STRIP_NOPS (fn);
5253 else
5254 fn = NULL;
5256 /* For type inconsistent program we may end up looking up virtual method
5257 in virtual table that does not contain TOKEN entries. We may overrun
5258 the virtual table and pick up a constant or RTTI info pointer.
5259 In any case the call is undefined. */
5260 if (!fn
5261 || (TREE_CODE (fn) != ADDR_EXPR && TREE_CODE (fn) != FDESC_EXPR)
5262 || TREE_CODE (TREE_OPERAND (fn, 0)) != FUNCTION_DECL)
5263 fn = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
5264 else
5266 fn = TREE_OPERAND (fn, 0);
5268 /* When cgraph node is missing and function is not public, we cannot
5269 devirtualize. This can happen in WHOPR when the actual method
5270 ends up in other partition, because we found devirtualization
5271 possibility too late. */
5272 if (!can_refer_decl_in_current_unit_p (fn, vtable))
5274 if (can_refer)
5276 *can_refer = false;
5277 return fn;
5279 return NULL_TREE;
5283 /* Make sure we create a cgraph node for functions we'll reference.
5284 They can be non-existent if the reference comes from an entry
5285 of an external vtable for example. */
5286 cgraph_node::get_create (fn);
5288 return fn;
5291 /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
5292 is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
5293 KNOWN_BINFO carries the binfo describing the true type of
5294 OBJ_TYPE_REF_OBJECT(REF).
5295 Set CAN_REFER if non-NULL to false if method
5296 is not referable or if the virtual table is ill-formed (such as rewriten
5297 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
5299 tree
5300 gimple_get_virt_method_for_binfo (HOST_WIDE_INT token, tree known_binfo,
5301 bool *can_refer)
5303 unsigned HOST_WIDE_INT offset;
5304 tree v;
5306 v = BINFO_VTABLE (known_binfo);
5307 /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */
5308 if (!v)
5309 return NULL_TREE;
5311 if (!vtable_pointer_value_to_vtable (v, &v, &offset))
5313 if (can_refer)
5314 *can_refer = false;
5315 return NULL_TREE;
5317 return gimple_get_virt_method_for_vtable (token, v, offset, can_refer);
5320 /* Return true iff VAL is a gimple expression that is known to be
5321 non-negative. Restricted to floating-point inputs. */
5323 bool
5324 gimple_val_nonnegative_real_p (tree val)
5326 gimple def_stmt;
5328 gcc_assert (val && SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)));
5330 /* Use existing logic for non-gimple trees. */
5331 if (tree_expr_nonnegative_p (val))
5332 return true;
5334 if (TREE_CODE (val) != SSA_NAME)
5335 return false;
5337 /* Currently we look only at the immediately defining statement
5338 to make this determination, since recursion on defining
5339 statements of operands can lead to quadratic behavior in the
5340 worst case. This is expected to catch almost all occurrences
5341 in practice. It would be possible to implement limited-depth
5342 recursion if important cases are lost. Alternatively, passes
5343 that need this information (such as the pow/powi lowering code
5344 in the cse_sincos pass) could be revised to provide it through
5345 dataflow propagation. */
5347 def_stmt = SSA_NAME_DEF_STMT (val);
5349 if (is_gimple_assign (def_stmt))
5351 tree op0, op1;
5353 /* See fold-const.c:tree_expr_nonnegative_p for additional
5354 cases that could be handled with recursion. */
5356 switch (gimple_assign_rhs_code (def_stmt))
5358 case ABS_EXPR:
5359 /* Always true for floating-point operands. */
5360 return true;
5362 case MULT_EXPR:
5363 /* True if the two operands are identical (since we are
5364 restricted to floating-point inputs). */
5365 op0 = gimple_assign_rhs1 (def_stmt);
5366 op1 = gimple_assign_rhs2 (def_stmt);
5368 if (op0 == op1
5369 || operand_equal_p (op0, op1, 0))
5370 return true;
5372 default:
5373 return false;
5376 else if (is_gimple_call (def_stmt))
5378 tree fndecl = gimple_call_fndecl (def_stmt);
5379 if (fndecl
5380 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
5382 tree arg1;
5384 switch (DECL_FUNCTION_CODE (fndecl))
5386 CASE_FLT_FN (BUILT_IN_ACOS):
5387 CASE_FLT_FN (BUILT_IN_ACOSH):
5388 CASE_FLT_FN (BUILT_IN_CABS):
5389 CASE_FLT_FN (BUILT_IN_COSH):
5390 CASE_FLT_FN (BUILT_IN_ERFC):
5391 CASE_FLT_FN (BUILT_IN_EXP):
5392 CASE_FLT_FN (BUILT_IN_EXP10):
5393 CASE_FLT_FN (BUILT_IN_EXP2):
5394 CASE_FLT_FN (BUILT_IN_FABS):
5395 CASE_FLT_FN (BUILT_IN_FDIM):
5396 CASE_FLT_FN (BUILT_IN_HYPOT):
5397 CASE_FLT_FN (BUILT_IN_POW10):
5398 return true;
5400 CASE_FLT_FN (BUILT_IN_SQRT):
5401 /* sqrt(-0.0) is -0.0, and sqrt is not defined over other
5402 nonnegative inputs. */
5403 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (val))))
5404 return true;
5406 break;
5408 CASE_FLT_FN (BUILT_IN_POWI):
5409 /* True if the second argument is an even integer. */
5410 arg1 = gimple_call_arg (def_stmt, 1);
5412 if (TREE_CODE (arg1) == INTEGER_CST
5413 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
5414 return true;
5416 break;
5418 CASE_FLT_FN (BUILT_IN_POW):
5419 /* True if the second argument is an even integer-valued
5420 real. */
5421 arg1 = gimple_call_arg (def_stmt, 1);
5423 if (TREE_CODE (arg1) == REAL_CST)
5425 REAL_VALUE_TYPE c;
5426 HOST_WIDE_INT n;
5428 c = TREE_REAL_CST (arg1);
5429 n = real_to_integer (&c);
5431 if ((n & 1) == 0)
5433 REAL_VALUE_TYPE cint;
5434 real_from_integer (&cint, VOIDmode, n, SIGNED);
5435 if (real_identical (&c, &cint))
5436 return true;
5440 break;
5442 default:
5443 return false;
5448 return false;
5451 /* Given a pointer value OP0, return a simplified version of an
5452 indirection through OP0, or NULL_TREE if no simplification is
5453 possible. Note that the resulting type may be different from
5454 the type pointed to in the sense that it is still compatible
5455 from the langhooks point of view. */
5457 tree
5458 gimple_fold_indirect_ref (tree t)
5460 tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype);
5461 tree sub = t;
5462 tree subtype;
5464 STRIP_NOPS (sub);
5465 subtype = TREE_TYPE (sub);
5466 if (!POINTER_TYPE_P (subtype))
5467 return NULL_TREE;
5469 if (TREE_CODE (sub) == ADDR_EXPR)
5471 tree op = TREE_OPERAND (sub, 0);
5472 tree optype = TREE_TYPE (op);
5473 /* *&p => p */
5474 if (useless_type_conversion_p (type, optype))
5475 return op;
5477 /* *(foo *)&fooarray => fooarray[0] */
5478 if (TREE_CODE (optype) == ARRAY_TYPE
5479 && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST
5480 && useless_type_conversion_p (type, TREE_TYPE (optype)))
5482 tree type_domain = TYPE_DOMAIN (optype);
5483 tree min_val = size_zero_node;
5484 if (type_domain && TYPE_MIN_VALUE (type_domain))
5485 min_val = TYPE_MIN_VALUE (type_domain);
5486 if (TREE_CODE (min_val) == INTEGER_CST)
5487 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
5489 /* *(foo *)&complexfoo => __real__ complexfoo */
5490 else if (TREE_CODE (optype) == COMPLEX_TYPE
5491 && useless_type_conversion_p (type, TREE_TYPE (optype)))
5492 return fold_build1 (REALPART_EXPR, type, op);
5493 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
5494 else if (TREE_CODE (optype) == VECTOR_TYPE
5495 && useless_type_conversion_p (type, TREE_TYPE (optype)))
5497 tree part_width = TYPE_SIZE (type);
5498 tree index = bitsize_int (0);
5499 return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
5503 /* *(p + CST) -> ... */
5504 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
5505 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
5507 tree addr = TREE_OPERAND (sub, 0);
5508 tree off = TREE_OPERAND (sub, 1);
5509 tree addrtype;
5511 STRIP_NOPS (addr);
5512 addrtype = TREE_TYPE (addr);
5514 /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
5515 if (TREE_CODE (addr) == ADDR_EXPR
5516 && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE
5517 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))
5518 && tree_fits_uhwi_p (off))
5520 unsigned HOST_WIDE_INT offset = tree_to_uhwi (off);
5521 tree part_width = TYPE_SIZE (type);
5522 unsigned HOST_WIDE_INT part_widthi
5523 = tree_to_shwi (part_width) / BITS_PER_UNIT;
5524 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
5525 tree index = bitsize_int (indexi);
5526 if (offset / part_widthi
5527 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype)))
5528 return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0),
5529 part_width, index);
5532 /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
5533 if (TREE_CODE (addr) == ADDR_EXPR
5534 && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE
5535 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))))
5537 tree size = TYPE_SIZE_UNIT (type);
5538 if (tree_int_cst_equal (size, off))
5539 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0));
5542 /* *(p + CST) -> MEM_REF <p, CST>. */
5543 if (TREE_CODE (addr) != ADDR_EXPR
5544 || DECL_P (TREE_OPERAND (addr, 0)))
5545 return fold_build2 (MEM_REF, type,
5546 addr,
5547 wide_int_to_tree (ptype, off));
5550 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
5551 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
5552 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST
5553 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
5555 tree type_domain;
5556 tree min_val = size_zero_node;
5557 tree osub = sub;
5558 sub = gimple_fold_indirect_ref (sub);
5559 if (! sub)
5560 sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
5561 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
5562 if (type_domain && TYPE_MIN_VALUE (type_domain))
5563 min_val = TYPE_MIN_VALUE (type_domain);
5564 if (TREE_CODE (min_val) == INTEGER_CST)
5565 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
5568 return NULL_TREE;
5571 /* Return true if CODE is an operation that when operating on signed
5572 integer types involves undefined behavior on overflow and the
5573 operation can be expressed with unsigned arithmetic. */
5575 bool
5576 arith_code_with_undefined_signed_overflow (tree_code code)
5578 switch (code)
5580 case PLUS_EXPR:
5581 case MINUS_EXPR:
5582 case MULT_EXPR:
5583 case NEGATE_EXPR:
5584 case POINTER_PLUS_EXPR:
5585 return true;
5586 default:
5587 return false;
5591 /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic
5592 operation that can be transformed to unsigned arithmetic by converting
5593 its operand, carrying out the operation in the corresponding unsigned
5594 type and converting the result back to the original type.
5596 Returns a sequence of statements that replace STMT and also contain
5597 a modified form of STMT itself. */
5599 gimple_seq
5600 rewrite_to_defined_overflow (gimple stmt)
5602 if (dump_file && (dump_flags & TDF_DETAILS))
5604 fprintf (dump_file, "rewriting stmt with undefined signed "
5605 "overflow ");
5606 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
5609 tree lhs = gimple_assign_lhs (stmt);
5610 tree type = unsigned_type_for (TREE_TYPE (lhs));
5611 gimple_seq stmts = NULL;
5612 for (unsigned i = 1; i < gimple_num_ops (stmt); ++i)
5614 gimple_seq stmts2 = NULL;
5615 gimple_set_op (stmt, i,
5616 force_gimple_operand (fold_convert (type,
5617 gimple_op (stmt, i)),
5618 &stmts2, true, NULL_TREE));
5619 gimple_seq_add_seq (&stmts, stmts2);
5621 gimple_assign_set_lhs (stmt, make_ssa_name (type, stmt));
5622 if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
5623 gimple_assign_set_rhs_code (stmt, PLUS_EXPR);
5624 gimple_seq_add_stmt (&stmts, stmt);
5625 gimple cvt = gimple_build_assign_with_ops
5626 (NOP_EXPR, lhs, gimple_assign_lhs (stmt), NULL_TREE);
5627 gimple_seq_add_stmt (&stmts, cvt);
5629 return stmts;
5633 /* Build the expression CODE OP0 of type TYPE with location LOC,
5634 simplifying it first if possible using VALUEIZE if not NULL.
5635 OP0 is expected to be valueized already. Returns the built
5636 expression value and appends statements possibly defining it
5637 to SEQ. */
5639 tree
5640 gimple_build (gimple_seq *seq, location_t loc,
5641 enum tree_code code, tree type, tree op0,
5642 tree (*valueize)(tree))
5644 tree res = gimple_simplify (code, type, op0, seq, valueize);
5645 if (!res)
5647 if (gimple_in_ssa_p (cfun))
5648 res = make_ssa_name (type, NULL);
5649 else
5650 res = create_tmp_reg (type, NULL);
5651 gimple stmt;
5652 if (code == REALPART_EXPR
5653 || code == IMAGPART_EXPR
5654 || code == VIEW_CONVERT_EXPR)
5655 stmt = gimple_build_assign_with_ops (code, res,
5656 build1 (code, type,
5657 op0), NULL_TREE);
5658 else
5659 stmt = gimple_build_assign_with_ops (code, res, op0, NULL_TREE);
5660 gimple_set_location (stmt, loc);
5661 gimple_seq_add_stmt_without_update (seq, stmt);
5663 return res;
5666 /* Build the expression OP0 CODE OP1 of type TYPE with location LOC,
5667 simplifying it first if possible using VALUEIZE if not NULL.
5668 OP0 and OP1 are expected to be valueized already. Returns the built
5669 expression value and appends statements possibly defining it
5670 to SEQ. */
5672 tree
5673 gimple_build (gimple_seq *seq, location_t loc,
5674 enum tree_code code, tree type, tree op0, tree op1,
5675 tree (*valueize)(tree))
5677 tree res = gimple_simplify (code, type, op0, op1, seq, valueize);
5678 if (!res)
5680 if (gimple_in_ssa_p (cfun))
5681 res = make_ssa_name (type, NULL);
5682 else
5683 res = create_tmp_reg (type, NULL);
5684 gimple stmt = gimple_build_assign_with_ops (code, res, op0, op1);
5685 gimple_set_location (stmt, loc);
5686 gimple_seq_add_stmt_without_update (seq, stmt);
5688 return res;
5691 /* Build the expression (CODE OP0 OP1 OP2) of type TYPE with location LOC,
5692 simplifying it first if possible using VALUEIZE if not NULL.
5693 OP0, OP1 and OP2 are expected to be valueized already. Returns the built
5694 expression value and appends statements possibly defining it
5695 to SEQ. */
5697 tree
5698 gimple_build (gimple_seq *seq, location_t loc,
5699 enum tree_code code, tree type, tree op0, tree op1, tree op2,
5700 tree (*valueize)(tree))
5702 tree res = gimple_simplify (code, type, op0, op1, op2,
5703 seq, valueize);
5704 if (!res)
5706 if (gimple_in_ssa_p (cfun))
5707 res = make_ssa_name (type, NULL);
5708 else
5709 res = create_tmp_reg (type, NULL);
5710 gimple stmt;
5711 if (code == BIT_FIELD_REF)
5712 stmt = gimple_build_assign_with_ops (code, res,
5713 build3 (BIT_FIELD_REF, type,
5714 op0, op1, op2),
5715 NULL_TREE);
5716 else
5717 stmt = gimple_build_assign_with_ops (code, res, op0, op1, op2);
5718 gimple_set_location (stmt, loc);
5719 gimple_seq_add_stmt_without_update (seq, stmt);
5721 return res;
5724 /* Build the call FN (ARG0) with a result of type TYPE
5725 (or no result if TYPE is void) with location LOC,
5726 simplifying it first if possible using VALUEIZE if not NULL.
5727 ARG0 is expected to be valueized already. Returns the built
5728 expression value (or NULL_TREE if TYPE is void) and appends
5729 statements possibly defining it to SEQ. */
5731 tree
5732 gimple_build (gimple_seq *seq, location_t loc,
5733 enum built_in_function fn, tree type, tree arg0,
5734 tree (*valueize)(tree))
5736 tree res = gimple_simplify (fn, type, arg0, seq, valueize);
5737 if (!res)
5739 tree decl = builtin_decl_implicit (fn);
5740 gimple stmt = gimple_build_call (decl, 1, arg0);
5741 if (!VOID_TYPE_P (type))
5743 if (gimple_in_ssa_p (cfun))
5744 res = make_ssa_name (type, NULL);
5745 else
5746 res = create_tmp_reg (type, NULL);
5747 gimple_call_set_lhs (stmt, res);
5749 gimple_set_location (stmt, loc);
5750 gimple_seq_add_stmt_without_update (seq, stmt);
5752 return res;
5755 /* Build the call FN (ARG0, ARG1) with a result of type TYPE
5756 (or no result if TYPE is void) with location LOC,
5757 simplifying it first if possible using VALUEIZE if not NULL.
5758 ARG0 is expected to be valueized already. Returns the built
5759 expression value (or NULL_TREE if TYPE is void) and appends
5760 statements possibly defining it to SEQ. */
5762 tree
5763 gimple_build (gimple_seq *seq, location_t loc,
5764 enum built_in_function fn, tree type, tree arg0, tree arg1,
5765 tree (*valueize)(tree))
5767 tree res = gimple_simplify (fn, type, arg0, arg1, seq, valueize);
5768 if (!res)
5770 tree decl = builtin_decl_implicit (fn);
5771 gimple stmt = gimple_build_call (decl, 2, arg0, arg1);
5772 if (!VOID_TYPE_P (type))
5774 if (gimple_in_ssa_p (cfun))
5775 res = make_ssa_name (type, NULL);
5776 else
5777 res = create_tmp_reg (type, NULL);
5778 gimple_call_set_lhs (stmt, res);
5780 gimple_set_location (stmt, loc);
5781 gimple_seq_add_stmt_without_update (seq, stmt);
5783 return res;
5786 /* Build the call FN (ARG0, ARG1, ARG2) with a result of type TYPE
5787 (or no result if TYPE is void) with location LOC,
5788 simplifying it first if possible using VALUEIZE if not NULL.
5789 ARG0 is expected to be valueized already. Returns the built
5790 expression value (or NULL_TREE if TYPE is void) and appends
5791 statements possibly defining it to SEQ. */
5793 tree
5794 gimple_build (gimple_seq *seq, location_t loc,
5795 enum built_in_function fn, tree type,
5796 tree arg0, tree arg1, tree arg2,
5797 tree (*valueize)(tree))
5799 tree res = gimple_simplify (fn, type, arg0, arg1, arg2, seq, valueize);
5800 if (!res)
5802 tree decl = builtin_decl_implicit (fn);
5803 gimple stmt = gimple_build_call (decl, 3, arg0, arg1, arg2);
5804 if (!VOID_TYPE_P (type))
5806 if (gimple_in_ssa_p (cfun))
5807 res = make_ssa_name (type, NULL);
5808 else
5809 res = create_tmp_reg (type, NULL);
5810 gimple_call_set_lhs (stmt, res);
5812 gimple_set_location (stmt, loc);
5813 gimple_seq_add_stmt_without_update (seq, stmt);
5815 return res;
5818 /* Build the conversion (TYPE) OP with a result of type TYPE
5819 with location LOC if such conversion is neccesary in GIMPLE,
5820 simplifying it first.
5821 Returns the built expression value and appends
5822 statements possibly defining it to SEQ. */
5824 tree
5825 gimple_convert (gimple_seq *seq, location_t loc, tree type, tree op)
5827 if (useless_type_conversion_p (type, TREE_TYPE (op)))
5828 return op;
5829 return gimple_build (seq, loc, NOP_EXPR, type, op);