1 /* Conditional constant propagation pass for the GNU compiler.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
4 Adapted from original RTL SSA-CCP by Daniel Berlin <dberlin@dberlin.org>
5 Adapted to GIMPLE trees by Diego Novillo <dnovillo@redhat.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 2, or (at your option) any
14 GCC is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to the Free
21 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
24 /* Conditional constant propagation (CCP) is based on the SSA
25 propagation engine (tree-ssa-propagate.c). Constant assignments of
26 the form VAR = CST are propagated from the assignments into uses of
27 VAR, which in turn may generate new constants. The simulation uses
28 a four level lattice to keep track of constant values associated
29 with SSA names. Given an SSA name V_i, it may take one of the
32 UNINITIALIZED -> This is the default starting value. V_i
33 has not been processed yet.
35 UNDEFINED -> V_i is a local variable whose definition
36 has not been processed yet. Therefore we
37 don't yet know if its value is a constant
40 CONSTANT -> V_i has been found to hold a constant
43 VARYING -> V_i cannot take a constant value, or if it
44 does, it is not possible to determine it
47 The core of SSA-CCP is in ccp_visit_stmt and ccp_visit_phi_node:
49 1- In ccp_visit_stmt, we are interested in assignments whose RHS
50 evaluates into a constant and conditional jumps whose predicate
51 evaluates into a boolean true or false. When an assignment of
52 the form V_i = CONST is found, V_i's lattice value is set to
53 CONSTANT and CONST is associated with it. This causes the
54 propagation engine to add all the SSA edges coming out the
55 assignment into the worklists, so that statements that use V_i
58 If the statement is a conditional with a constant predicate, we
59 mark the outgoing edges as executable or not executable
60 depending on the predicate's value. This is then used when
61 visiting PHI nodes to know when a PHI argument can be ignored.
64 2- In ccp_visit_phi_node, if all the PHI arguments evaluate to the
65 same constant C, then the LHS of the PHI is set to C. This
66 evaluation is known as the "meet operation". Since one of the
67 goals of this evaluation is to optimistically return constant
68 values as often as possible, it uses two main short cuts:
70 - If an argument is flowing in through a non-executable edge, it
71 is ignored. This is useful in cases like this:
77 a_11 = PHI (a_9, a_10)
79 If PRED is known to always evaluate to false, then we can
80 assume that a_11 will always take its value from a_10, meaning
81 that instead of consider it VARYING (a_9 and a_10 have
82 different values), we can consider it CONSTANT 100.
84 - If an argument has an UNDEFINED value, then it does not affect
85 the outcome of the meet operation. If a variable V_i has an
86 UNDEFINED value, it means that either its defining statement
87 hasn't been visited yet or V_i has no defining statement, in
88 which case the original symbol 'V' is being used
89 uninitialized. Since 'V' is a local variable, the compiler
90 may assume any initial value for it.
93 After propagation, every variable V_i that ends up with a lattice
94 value of CONSTANT will have the associated constant value in the
95 array CONST_VAL[i].VALUE. That is fed into substitute_and_fold for
96 final substitution and folding.
99 Constant propagation in stores and loads (STORE-CCP)
100 ----------------------------------------------------
102 While CCP has all the logic to propagate constants in GIMPLE
103 registers, it is missing the ability to associate constants with
104 stores and loads (i.e., pointer dereferences, structures and
105 global/aliased variables). We don't keep loads and stores in
106 SSA, but we do build a factored use-def web for them (in the
109 For instance, consider the following code fragment:
128 We should be able to deduce that the predicate 'a.a != B' is always
129 false. To achieve this, we associate constant values to the SSA
130 names in the V_MAY_DEF and V_MUST_DEF operands for each store.
131 Additionally, since we also glob partial loads/stores with the base
132 symbol, we also keep track of the memory reference where the
133 constant value was stored (in the MEM_REF field of PROP_VALUE_T).
136 # a_5 = V_MAY_DEF <a_4>
142 In the example above, CCP will associate value '2' with 'a_5', but
143 it would be wrong to replace the load from 'a.b' with '2', because
144 '2' had been stored into a.a.
146 To support STORE-CCP, it is necessary to add a new value to the
147 constant propagation lattice. When evaluating a load for a memory
148 reference we can no longer assume a value of UNDEFINED if we
149 haven't seen a preceding store to the same memory location.
150 Consider, for instance global variables:
158 # A_5 = PHI (A_4, A_2);
166 The value of A_2 cannot be assumed to be UNDEFINED, as it may have
167 been defined outside of foo. If we were to assume it UNDEFINED, we
168 would erroneously optimize the above into 'return 3;'. Therefore,
169 when doing STORE-CCP, we introduce a fifth lattice value
170 (UNKNOWN_VAL), which overrides any other value when computing the
171 meet operation in PHI nodes.
173 Though STORE-CCP is not too expensive, it does have to do more work
174 than regular CCP, so it is only enabled at -O2. Both regular CCP
175 and STORE-CCP use the exact same algorithm. The only distinction
176 is that when doing STORE-CCP, the boolean variable DO_STORE_CCP is
177 set to true. This affects the evaluation of statements and PHI
182 Constant propagation with conditional branches,
183 Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
185 Building an Optimizing Compiler,
186 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
188 Advanced Compiler Design and Implementation,
189 Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
193 #include "coretypes.h"
200 #include "basic-block.h"
203 #include "function.h"
204 #include "diagnostic.h"
206 #include "tree-dump.h"
207 #include "tree-flow.h"
208 #include "tree-pass.h"
209 #include "tree-ssa-propagate.h"
210 #include "langhooks.h"
214 /* Possible lattice values. */
224 /* Array of propagated constant values. After propagation,
225 CONST_VAL[I].VALUE holds the constant value for SSA_NAME(I). If
226 the constant is held in an SSA name representing a memory store
227 (i.e., a V_MAY_DEF or V_MUST_DEF), CONST_VAL[I].MEM_REF will
228 contain the actual memory reference used to store (i.e., the LHS of
229 the assignment doing the store). */
230 static prop_value_t *const_val;
232 /* True if we are also propagating constants in stores and loads. */
233 static bool do_store_ccp;
235 /* Dump constant propagation value VAL to file OUTF prefixed by PREFIX. */
238 dump_lattice_value (FILE *outf, const char *prefix, prop_value_t val)
240 switch (val.lattice_val)
243 fprintf (outf, "%sUNINITIALIZED", prefix);
246 fprintf (outf, "%sUNDEFINED", prefix);
249 fprintf (outf, "%sVARYING", prefix);
252 fprintf (outf, "%sUNKNOWN_VAL", prefix);
255 fprintf (outf, "%sCONSTANT ", prefix);
256 print_generic_expr (outf, val.value, dump_flags);
264 /* Print lattice value VAL to stderr. */
266 void debug_lattice_value (prop_value_t val);
269 debug_lattice_value (prop_value_t val)
271 dump_lattice_value (stderr, "", val);
272 fprintf (stderr, "\n");
276 /* The regular is_gimple_min_invariant does a shallow test of the object.
277 It assumes that full gimplification has happened, or will happen on the
278 object. For a value coming from DECL_INITIAL, this is not true, so we
279 have to be more strict ourselves. */
282 ccp_decl_initial_min_invariant (tree t)
284 if (!is_gimple_min_invariant (t))
286 if (TREE_CODE (t) == ADDR_EXPR)
288 /* Inline and unroll is_gimple_addressable. */
291 t = TREE_OPERAND (t, 0);
292 if (is_gimple_id (t))
294 if (!handled_component_p (t))
302 /* Compute a default value for variable VAR and store it in the
303 CONST_VAL array. The following rules are used to get default
306 1- Global and static variables that are declared constant are
309 2- Any other value is considered UNDEFINED. This is useful when
310 considering PHI nodes. PHI arguments that are undefined do not
311 change the constant value of the PHI node, which allows for more
312 constants to be propagated.
314 3- If SSA_NAME_VALUE is set and it is a constant, its value is
317 4- Variables defined by statements other than assignments and PHI
318 nodes are considered VARYING.
320 5- Variables that are not GIMPLE registers are considered
321 UNKNOWN_VAL, which is really a stronger version of UNDEFINED.
322 It's used to avoid the short circuit evaluation implied by
323 UNDEFINED in ccp_lattice_meet. */
326 get_default_value (tree var)
328 tree sym = SSA_NAME_VAR (var);
329 prop_value_t val = { UNINITIALIZED, NULL_TREE, NULL_TREE };
331 if (!do_store_ccp && !is_gimple_reg (var))
333 /* Short circuit for regular CCP. We are not interested in any
334 non-register when DO_STORE_CCP is false. */
335 val.lattice_val = VARYING;
337 else if (SSA_NAME_VALUE (var)
338 && is_gimple_min_invariant (SSA_NAME_VALUE (var)))
340 val.lattice_val = CONSTANT;
341 val.value = SSA_NAME_VALUE (var);
343 else if (TREE_STATIC (sym)
344 && TREE_READONLY (sym)
346 && DECL_INITIAL (sym)
347 && ccp_decl_initial_min_invariant (DECL_INITIAL (sym)))
349 /* Globals and static variables declared 'const' take their
351 val.lattice_val = CONSTANT;
352 val.value = DECL_INITIAL (sym);
357 tree stmt = SSA_NAME_DEF_STMT (var);
359 if (IS_EMPTY_STMT (stmt))
361 /* Variables defined by an empty statement are those used
362 before being initialized. If VAR is a local variable, we
363 can assume initially that it is UNDEFINED. If we are
364 doing STORE-CCP, function arguments and non-register
365 variables are initially UNKNOWN_VAL, because we cannot
366 discard the value incoming from outside of this function
367 (see ccp_lattice_meet for details). */
368 if (is_gimple_reg (sym) && TREE_CODE (sym) != PARM_DECL)
369 val.lattice_val = UNDEFINED;
370 else if (do_store_ccp)
371 val.lattice_val = UNKNOWN_VAL;
373 val.lattice_val = VARYING;
375 else if (TREE_CODE (stmt) == MODIFY_EXPR
376 || TREE_CODE (stmt) == PHI_NODE)
378 /* Any other variable defined by an assignment or a PHI node
379 is considered UNDEFINED (or UNKNOWN_VAL if VAR is not a
381 val.lattice_val = is_gimple_reg (sym) ? UNDEFINED : UNKNOWN_VAL;
385 /* Otherwise, VAR will never take on a constant value. */
386 val.lattice_val = VARYING;
394 /* Get the constant value associated with variable VAR. If
395 MAY_USE_DEFAULT_P is true, call get_default_value on variables that
396 have the lattice value UNINITIALIZED. */
398 static prop_value_t *
399 get_value (tree var, bool may_use_default_p)
401 prop_value_t *val = &const_val[SSA_NAME_VERSION (var)];
402 if (may_use_default_p && val->lattice_val == UNINITIALIZED)
403 *val = get_default_value (var);
409 /* Set the value for variable VAR to NEW_VAL. Return true if the new
410 value is different from VAR's previous value. */
413 set_lattice_value (tree var, prop_value_t new_val)
415 prop_value_t *old_val = get_value (var, false);
417 /* Lattice transitions must always be monotonically increasing in
418 value. We allow two exceptions:
420 1- If *OLD_VAL and NEW_VAL are the same, return false to
421 inform the caller that this was a non-transition.
423 2- If we are doing store-ccp (i.e., DOING_STORE_CCP is true),
424 allow CONSTANT->UNKNOWN_VAL. The UNKNOWN_VAL state is a
425 special type of UNDEFINED state which prevents the short
426 circuit evaluation of PHI arguments (see ccp_visit_phi_node
427 and ccp_lattice_meet). */
428 gcc_assert (old_val->lattice_val <= new_val.lattice_val
429 || (old_val->lattice_val == new_val.lattice_val
430 && old_val->value == new_val.value
431 && old_val->mem_ref == new_val.mem_ref)
433 && old_val->lattice_val == CONSTANT
434 && new_val.lattice_val == UNKNOWN_VAL));
436 if (old_val->lattice_val != new_val.lattice_val)
438 if (dump_file && (dump_flags & TDF_DETAILS))
440 dump_lattice_value (dump_file, "Lattice value changed to ", new_val);
441 fprintf (dump_file, ". %sdding SSA edges to worklist.\n",
442 new_val.lattice_val != UNDEFINED ? "A" : "Not a");
447 /* Transitions UNINITIALIZED -> UNDEFINED are never interesting
448 for propagation purposes. In these cases return false to
449 avoid doing useless work. */
450 return (new_val.lattice_val != UNDEFINED);
457 /* Return the likely CCP lattice value for STMT.
459 If STMT has no operands, then return CONSTANT.
461 Else if any operands of STMT are undefined, then return UNDEFINED.
463 Else if any operands of STMT are constants, then return CONSTANT.
465 Else return VARYING. */
468 likely_value (tree stmt)
475 ann = stmt_ann (stmt);
477 /* If the statement has volatile operands, it won't fold to a
479 if (ann->has_volatile_ops)
482 /* If we are not doing store-ccp, statements with loads
483 and/or stores will never fold into a constant. */
485 && !ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
489 /* A CALL_EXPR is assumed to be varying. NOTE: This may be overly
490 conservative, in the presence of const and pure calls. */
491 if (get_call_expr_in (stmt) != NULL_TREE)
494 /* Anything other than assignments and conditional jumps are not
495 interesting for CCP. */
496 if (TREE_CODE (stmt) != MODIFY_EXPR
497 && TREE_CODE (stmt) != COND_EXPR
498 && TREE_CODE (stmt) != SWITCH_EXPR)
501 if (is_gimple_min_invariant (get_rhs (stmt)))
504 found_constant = false;
505 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE|SSA_OP_VUSE)
507 prop_value_t *val = get_value (use, true);
509 if (val->lattice_val == VARYING)
512 if (val->lattice_val == UNKNOWN_VAL)
514 /* UNKNOWN_VAL is invalid when not doing STORE-CCP. */
515 gcc_assert (do_store_ccp);
519 if (val->lattice_val == CONSTANT)
520 found_constant = true;
524 || ZERO_SSA_OPERANDS (stmt, SSA_OP_USE)
525 || ZERO_SSA_OPERANDS (stmt, SSA_OP_VUSE))
532 /* Initialize local data structures for CCP. */
535 ccp_initialize (void)
539 const_val = XNEWVEC (prop_value_t, num_ssa_names);
540 memset (const_val, 0, num_ssa_names * sizeof (*const_val));
542 /* Initialize simulation flags for PHI nodes and statements. */
545 block_stmt_iterator i;
547 for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
549 bool is_varying = false;
550 tree stmt = bsi_stmt (i);
552 if (likely_value (stmt) == VARYING)
558 /* If the statement will not produce a constant, mark
559 all its outputs VARYING. */
560 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
561 get_value (def, false)->lattice_val = VARYING;
563 /* Never mark conditional jumps with DONT_SIMULATE_AGAIN,
564 otherwise the propagator will never add the outgoing
566 if (TREE_CODE (stmt) != COND_EXPR
567 && TREE_CODE (stmt) != SWITCH_EXPR)
571 DONT_SIMULATE_AGAIN (stmt) = is_varying;
575 /* Now process PHI nodes. */
580 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
584 prop_value_t *val = get_value (PHI_RESULT (phi), false);
586 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
588 arg = PHI_ARG_DEF (phi, i);
590 if (TREE_CODE (arg) == SSA_NAME
591 && get_value (arg, false)->lattice_val == VARYING)
593 val->lattice_val = VARYING;
598 DONT_SIMULATE_AGAIN (phi) = (val->lattice_val == VARYING);
604 /* Do final substitution of propagated values, cleanup the flowgraph and
605 free allocated storage. */
610 /* Perform substitutions based on the known constant values. */
611 substitute_and_fold (const_val, false);
617 /* Compute the meet operator between *VAL1 and *VAL2. Store the result
620 any M UNDEFINED = any
621 any M UNKNOWN_VAL = UNKNOWN_VAL
622 any M VARYING = VARYING
623 Ci M Cj = Ci if (i == j)
624 Ci M Cj = VARYING if (i != j)
626 Lattice values UNKNOWN_VAL and UNDEFINED are similar but have
627 different semantics at PHI nodes. Both values imply that we don't
628 know whether the variable is constant or not. However, UNKNOWN_VAL
629 values override all others. For instance, suppose that A is a
639 | A_3 = PHI (A_2, A_1)
644 If the edge into A_2 is not executable, the first visit to A_3 will
645 yield the constant 4. But the second visit to A_3 will be with A_2
646 in state UNKNOWN_VAL. We can no longer conclude that A_3 is 4
647 because A_2 may have been set in another function. If we had used
648 the lattice value UNDEFINED, we would have had wrongly concluded
653 ccp_lattice_meet (prop_value_t *val1, prop_value_t *val2)
655 if (val1->lattice_val == UNDEFINED)
657 /* UNDEFINED M any = any */
660 else if (val2->lattice_val == UNDEFINED)
662 /* any M UNDEFINED = any
663 Nothing to do. VAL1 already contains the value we want. */
666 else if (val1->lattice_val == UNKNOWN_VAL
667 || val2->lattice_val == UNKNOWN_VAL)
669 /* UNKNOWN_VAL values are invalid if we are not doing STORE-CCP. */
670 gcc_assert (do_store_ccp);
672 /* any M UNKNOWN_VAL = UNKNOWN_VAL. */
673 val1->lattice_val = UNKNOWN_VAL;
674 val1->value = NULL_TREE;
675 val1->mem_ref = NULL_TREE;
677 else if (val1->lattice_val == VARYING
678 || val2->lattice_val == VARYING)
680 /* any M VARYING = VARYING. */
681 val1->lattice_val = VARYING;
682 val1->value = NULL_TREE;
683 val1->mem_ref = NULL_TREE;
685 else if (val1->lattice_val == CONSTANT
686 && val2->lattice_val == CONSTANT
687 && simple_cst_equal (val1->value, val2->value) == 1
689 || (val1->mem_ref && val2->mem_ref
690 && operand_equal_p (val1->mem_ref, val2->mem_ref, 0))))
692 /* Ci M Cj = Ci if (i == j)
693 Ci M Cj = VARYING if (i != j)
695 If these two values come from memory stores, make sure that
696 they come from the same memory reference. */
697 val1->lattice_val = CONSTANT;
698 val1->value = val1->value;
699 val1->mem_ref = val1->mem_ref;
703 /* Any other combination is VARYING. */
704 val1->lattice_val = VARYING;
705 val1->value = NULL_TREE;
706 val1->mem_ref = NULL_TREE;
711 /* Loop through the PHI_NODE's parameters for BLOCK and compare their
712 lattice values to determine PHI_NODE's lattice value. The value of a
713 PHI node is determined calling ccp_lattice_meet with all the arguments
714 of the PHI node that are incoming via executable edges. */
716 static enum ssa_prop_result
717 ccp_visit_phi_node (tree phi)
720 prop_value_t *old_val, new_val;
722 if (dump_file && (dump_flags & TDF_DETAILS))
724 fprintf (dump_file, "\nVisiting PHI node: ");
725 print_generic_expr (dump_file, phi, dump_flags);
728 old_val = get_value (PHI_RESULT (phi), false);
729 switch (old_val->lattice_val)
732 return SSA_PROP_VARYING;
739 /* To avoid the default value of UNKNOWN_VAL overriding
740 that of its possible constant arguments, temporarily
741 set the PHI node's default lattice value to be
742 UNDEFINED. If the PHI node's old value was UNKNOWN_VAL and
743 the new value is UNDEFINED, then we prevent the invalid
744 transition by not calling set_lattice_value. */
745 gcc_assert (do_store_ccp);
751 new_val.lattice_val = UNDEFINED;
752 new_val.value = NULL_TREE;
753 new_val.mem_ref = NULL_TREE;
760 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
762 /* Compute the meet operator over all the PHI arguments flowing
763 through executable edges. */
764 edge e = PHI_ARG_EDGE (phi, i);
766 if (dump_file && (dump_flags & TDF_DETAILS))
769 "\n Argument #%d (%d -> %d %sexecutable)\n",
770 i, e->src->index, e->dest->index,
771 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
774 /* If the incoming edge is executable, Compute the meet operator for
775 the existing value of the PHI node and the current PHI argument. */
776 if (e->flags & EDGE_EXECUTABLE)
778 tree arg = PHI_ARG_DEF (phi, i);
779 prop_value_t arg_val;
781 if (is_gimple_min_invariant (arg))
783 arg_val.lattice_val = CONSTANT;
785 arg_val.mem_ref = NULL_TREE;
788 arg_val = *(get_value (arg, true));
790 ccp_lattice_meet (&new_val, &arg_val);
792 if (dump_file && (dump_flags & TDF_DETAILS))
794 fprintf (dump_file, "\t");
795 print_generic_expr (dump_file, arg, dump_flags);
796 dump_lattice_value (dump_file, "\tValue: ", arg_val);
797 fprintf (dump_file, "\n");
800 if (new_val.lattice_val == VARYING)
805 if (dump_file && (dump_flags & TDF_DETAILS))
807 dump_lattice_value (dump_file, "\n PHI node value: ", new_val);
808 fprintf (dump_file, "\n\n");
811 /* Check for an invalid change from UNKNOWN_VAL to UNDEFINED. */
813 && old_val->lattice_val == UNKNOWN_VAL
814 && new_val.lattice_val == UNDEFINED)
815 return SSA_PROP_NOT_INTERESTING;
817 /* Otherwise, make the transition to the new value. */
818 if (set_lattice_value (PHI_RESULT (phi), new_val))
820 if (new_val.lattice_val == VARYING)
821 return SSA_PROP_VARYING;
823 return SSA_PROP_INTERESTING;
826 return SSA_PROP_NOT_INTERESTING;
830 /* CCP specific front-end to the non-destructive constant folding
833 Attempt to simplify the RHS of STMT knowing that one or more
834 operands are constants.
836 If simplification is possible, return the simplified RHS,
837 otherwise return the original RHS. */
842 tree rhs = get_rhs (stmt);
843 enum tree_code code = TREE_CODE (rhs);
844 enum tree_code_class kind = TREE_CODE_CLASS (code);
845 tree retval = NULL_TREE;
847 if (TREE_CODE (rhs) == SSA_NAME)
849 /* If the RHS is an SSA_NAME, return its known constant value,
851 return get_value (rhs, true)->value;
853 else if (do_store_ccp && stmt_makes_single_load (stmt))
855 /* If the RHS is a memory load, see if the VUSEs associated with
856 it are a valid constant for that memory load. */
857 prop_value_t *val = get_value_loaded_by (stmt, const_val);
858 if (val && val->mem_ref)
860 if (operand_equal_p (val->mem_ref, rhs, 0))
863 /* If RHS is extracting REALPART_EXPR or IMAGPART_EXPR of a
864 complex type with a known constant value, return it. */
865 if ((TREE_CODE (rhs) == REALPART_EXPR
866 || TREE_CODE (rhs) == IMAGPART_EXPR)
867 && operand_equal_p (val->mem_ref, TREE_OPERAND (rhs, 0), 0))
868 return fold_build1 (TREE_CODE (rhs), TREE_TYPE (rhs), val->value);
873 /* Unary operators. Note that we know the single operand must
874 be a constant. So this should almost always return a
876 if (kind == tcc_unary)
878 /* Handle unary operators which can appear in GIMPLE form. */
879 tree op0 = TREE_OPERAND (rhs, 0);
881 /* Simplify the operand down to a constant. */
882 if (TREE_CODE (op0) == SSA_NAME)
884 prop_value_t *val = get_value (op0, true);
885 if (val->lattice_val == CONSTANT)
886 op0 = get_value (op0, true)->value;
889 if ((code == NOP_EXPR || code == CONVERT_EXPR)
890 && tree_ssa_useless_type_conversion_1 (TREE_TYPE (rhs),
893 return fold_unary (code, TREE_TYPE (rhs), op0);
896 /* Binary and comparison operators. We know one or both of the
897 operands are constants. */
898 else if (kind == tcc_binary
899 || kind == tcc_comparison
900 || code == TRUTH_AND_EXPR
901 || code == TRUTH_OR_EXPR
902 || code == TRUTH_XOR_EXPR)
904 /* Handle binary and comparison operators that can appear in
906 tree op0 = TREE_OPERAND (rhs, 0);
907 tree op1 = TREE_OPERAND (rhs, 1);
909 /* Simplify the operands down to constants when appropriate. */
910 if (TREE_CODE (op0) == SSA_NAME)
912 prop_value_t *val = get_value (op0, true);
913 if (val->lattice_val == CONSTANT)
917 if (TREE_CODE (op1) == SSA_NAME)
919 prop_value_t *val = get_value (op1, true);
920 if (val->lattice_val == CONSTANT)
924 return fold_binary (code, TREE_TYPE (rhs), op0, op1);
927 /* We may be able to fold away calls to builtin functions if their
928 arguments are constants. */
929 else if (code == CALL_EXPR
930 && TREE_CODE (TREE_OPERAND (rhs, 0)) == ADDR_EXPR
931 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0))
933 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0)))
935 if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_USE))
938 tree fndecl, arglist;
943 /* Preserve the original values of every operand. */
944 orig = XNEWVEC (tree, NUM_SSA_OPERANDS (stmt, SSA_OP_USE));
945 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
948 /* Substitute operands with their values and try to fold. */
949 replace_uses_in (stmt, NULL, const_val);
950 fndecl = get_callee_fndecl (rhs);
951 arglist = TREE_OPERAND (rhs, 1);
952 retval = fold_builtin (fndecl, arglist, false);
954 /* Restore operands to their original form. */
956 FOR_EACH_SSA_USE_OPERAND (var_p, stmt, iter, SSA_OP_USE)
957 SET_USE (var_p, orig[i++]);
964 /* If we got a simplified form, see if we need to convert its type. */
966 return fold_convert (TREE_TYPE (rhs), retval);
968 /* No simplification was possible. */
973 /* Return the tree representing the element referenced by T if T is an
974 ARRAY_REF or COMPONENT_REF into constant aggregates. Return
975 NULL_TREE otherwise. */
978 fold_const_aggregate_ref (tree t)
981 tree base, ctor, idx, field;
982 unsigned HOST_WIDE_INT cnt;
985 switch (TREE_CODE (t))
988 /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
989 DECL_INITIAL. If BASE is a nested reference into another
990 ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
991 the inner reference. */
992 base = TREE_OPERAND (t, 0);
993 switch (TREE_CODE (base))
996 if (!TREE_READONLY (base)
997 || TREE_CODE (TREE_TYPE (base)) != ARRAY_TYPE
998 || !targetm.binds_local_p (base))
1001 ctor = DECL_INITIAL (base);
1006 ctor = fold_const_aggregate_ref (base);
1013 if (ctor == NULL_TREE
1014 || TREE_CODE (ctor) != CONSTRUCTOR
1015 || !TREE_STATIC (ctor))
1018 /* Get the index. If we have an SSA_NAME, try to resolve it
1019 with the current lattice value for the SSA_NAME. */
1020 idx = TREE_OPERAND (t, 1);
1021 switch (TREE_CODE (idx))
1024 if ((value = get_value (idx, true))
1025 && value->lattice_val == CONSTANT
1026 && TREE_CODE (value->value) == INTEGER_CST)
1039 /* Whoo-hoo! I'll fold ya baby. Yeah! */
1040 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
1041 if (tree_int_cst_equal (cfield, idx))
1046 /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
1047 DECL_INITIAL. If BASE is a nested reference into another
1048 ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
1049 the inner reference. */
1050 base = TREE_OPERAND (t, 0);
1051 switch (TREE_CODE (base))
1054 if (!TREE_READONLY (base)
1055 || TREE_CODE (TREE_TYPE (base)) != RECORD_TYPE
1056 || !targetm.binds_local_p (base))
1059 ctor = DECL_INITIAL (base);
1064 ctor = fold_const_aggregate_ref (base);
1071 if (ctor == NULL_TREE
1072 || TREE_CODE (ctor) != CONSTRUCTOR
1073 || !TREE_STATIC (ctor))
1076 field = TREE_OPERAND (t, 1);
1078 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
1080 /* FIXME: Handle bit-fields. */
1081 && ! DECL_BIT_FIELD (cfield))
1088 tree c = fold_const_aggregate_ref (TREE_OPERAND (t, 0));
1089 if (c && TREE_CODE (c) == COMPLEX_CST)
1090 return fold_build1 (TREE_CODE (t), TREE_TYPE (t), c);
1101 /* Evaluate statement STMT. */
1104 evaluate_stmt (tree stmt)
1108 ccp_lattice_t likelyvalue = likely_value (stmt);
1110 val.mem_ref = NULL_TREE;
1112 /* If the statement is likely to have a CONSTANT result, then try
1113 to fold the statement to determine the constant value. */
1114 if (likelyvalue == CONSTANT)
1115 simplified = ccp_fold (stmt);
1116 /* If the statement is likely to have a VARYING result, then do not
1117 bother folding the statement. */
1118 else if (likelyvalue == VARYING)
1119 simplified = get_rhs (stmt);
1120 /* If the statement is an ARRAY_REF or COMPONENT_REF into constant
1121 aggregates, extract the referenced constant. Otherwise the
1122 statement is likely to have an UNDEFINED value, and there will be
1123 nothing to do. Note that fold_const_aggregate_ref returns
1124 NULL_TREE if the first case does not match. */
1126 simplified = fold_const_aggregate_ref (get_rhs (stmt));
1128 if (simplified && is_gimple_min_invariant (simplified))
1130 /* The statement produced a constant value. */
1131 val.lattice_val = CONSTANT;
1132 val.value = simplified;
1136 /* The statement produced a nonconstant value. If the statement
1137 had UNDEFINED operands, then the result of the statement
1138 should be UNDEFINED. Otherwise, the statement is VARYING. */
1139 if (likelyvalue == UNDEFINED || likelyvalue == UNKNOWN_VAL)
1140 val.lattice_val = likelyvalue;
1142 val.lattice_val = VARYING;
1144 val.value = NULL_TREE;
1151 /* Visit the assignment statement STMT. Set the value of its LHS to the
1152 value computed by the RHS and store LHS in *OUTPUT_P. If STMT
1153 creates virtual definitions, set the value of each new name to that
1154 of the RHS (if we can derive a constant out of the RHS). */
1156 static enum ssa_prop_result
1157 visit_assignment (tree stmt, tree *output_p)
1161 enum ssa_prop_result retval;
1163 lhs = TREE_OPERAND (stmt, 0);
1164 rhs = TREE_OPERAND (stmt, 1);
1166 if (TREE_CODE (rhs) == SSA_NAME)
1168 /* For a simple copy operation, we copy the lattice values. */
1169 prop_value_t *nval = get_value (rhs, true);
1172 else if (do_store_ccp && stmt_makes_single_load (stmt))
1174 /* Same as above, but the RHS is not a gimple register and yet
1175 has a known VUSE. If STMT is loading from the same memory
1176 location that created the SSA_NAMEs for the virtual operands,
1177 we can propagate the value on the RHS. */
1178 prop_value_t *nval = get_value_loaded_by (stmt, const_val);
1180 if (nval && nval->mem_ref
1181 && operand_equal_p (nval->mem_ref, rhs, 0))
1184 val = evaluate_stmt (stmt);
1187 /* Evaluate the statement. */
1188 val = evaluate_stmt (stmt);
1190 /* If the original LHS was a VIEW_CONVERT_EXPR, modify the constant
1191 value to be a VIEW_CONVERT_EXPR of the old constant value.
1193 ??? Also, if this was a definition of a bitfield, we need to widen
1194 the constant value into the type of the destination variable. This
1195 should not be necessary if GCC represented bitfields properly. */
1197 tree orig_lhs = TREE_OPERAND (stmt, 0);
1199 if (TREE_CODE (orig_lhs) == VIEW_CONVERT_EXPR
1200 && val.lattice_val == CONSTANT)
1202 tree w = fold_unary (VIEW_CONVERT_EXPR,
1203 TREE_TYPE (TREE_OPERAND (orig_lhs, 0)),
1206 orig_lhs = TREE_OPERAND (orig_lhs, 0);
1207 if (w && is_gimple_min_invariant (w))
1211 val.lattice_val = VARYING;
1216 if (val.lattice_val == CONSTANT
1217 && TREE_CODE (orig_lhs) == COMPONENT_REF
1218 && DECL_BIT_FIELD (TREE_OPERAND (orig_lhs, 1)))
1220 tree w = widen_bitfield (val.value, TREE_OPERAND (orig_lhs, 1),
1223 if (w && is_gimple_min_invariant (w))
1227 val.lattice_val = VARYING;
1228 val.value = NULL_TREE;
1229 val.mem_ref = NULL_TREE;
1234 retval = SSA_PROP_NOT_INTERESTING;
1236 /* Set the lattice value of the statement's output. */
1237 if (TREE_CODE (lhs) == SSA_NAME)
1239 /* If STMT is an assignment to an SSA_NAME, we only have one
1241 if (set_lattice_value (lhs, val))
1244 if (val.lattice_val == VARYING)
1245 retval = SSA_PROP_VARYING;
1247 retval = SSA_PROP_INTERESTING;
1250 else if (do_store_ccp && stmt_makes_single_store (stmt))
1252 /* Otherwise, set the names in V_MAY_DEF/V_MUST_DEF operands
1253 to the new constant value and mark the LHS as the memory
1254 reference associated with VAL. */
1259 /* Stores cannot take on an UNDEFINED value. */
1260 if (val.lattice_val == UNDEFINED)
1261 val.lattice_val = UNKNOWN_VAL;
1263 /* Mark VAL as stored in the LHS of this assignment. */
1266 /* Set the value of every VDEF to VAL. */
1268 FOR_EACH_SSA_TREE_OPERAND (vdef, stmt, i, SSA_OP_VIRTUAL_DEFS)
1269 changed |= set_lattice_value (vdef, val);
1271 /* Note that for propagation purposes, we are only interested in
1272 visiting statements that load the exact same memory reference
1273 stored here. Those statements will have the exact same list
1274 of virtual uses, so it is enough to set the output of this
1275 statement to be its first virtual definition. */
1276 *output_p = first_vdef (stmt);
1279 if (val.lattice_val == VARYING)
1280 retval = SSA_PROP_VARYING;
1282 retval = SSA_PROP_INTERESTING;
1290 /* Visit the conditional statement STMT. Return SSA_PROP_INTERESTING
1291 if it can determine which edge will be taken. Otherwise, return
1292 SSA_PROP_VARYING. */
1294 static enum ssa_prop_result
1295 visit_cond_stmt (tree stmt, edge *taken_edge_p)
1300 block = bb_for_stmt (stmt);
1301 val = evaluate_stmt (stmt);
1303 /* Find which edge out of the conditional block will be taken and add it
1304 to the worklist. If no single edge can be determined statically,
1305 return SSA_PROP_VARYING to feed all the outgoing edges to the
1306 propagation engine. */
1307 *taken_edge_p = val.value ? find_taken_edge (block, val.value) : 0;
1309 return SSA_PROP_INTERESTING;
1311 return SSA_PROP_VARYING;
1315 /* Evaluate statement STMT. If the statement produces an output value and
1316 its evaluation changes the lattice value of its output, return
1317 SSA_PROP_INTERESTING and set *OUTPUT_P to the SSA_NAME holding the
1320 If STMT is a conditional branch and we can determine its truth
1321 value, set *TAKEN_EDGE_P accordingly. If STMT produces a varying
1322 value, return SSA_PROP_VARYING. */
1324 static enum ssa_prop_result
1325 ccp_visit_stmt (tree stmt, edge *taken_edge_p, tree *output_p)
1330 if (dump_file && (dump_flags & TDF_DETAILS))
1332 fprintf (dump_file, "\nVisiting statement:\n");
1333 print_generic_stmt (dump_file, stmt, dump_flags);
1334 fprintf (dump_file, "\n");
1337 if (TREE_CODE (stmt) == MODIFY_EXPR)
1339 /* If the statement is an assignment that produces a single
1340 output value, evaluate its RHS to see if the lattice value of
1341 its output has changed. */
1342 return visit_assignment (stmt, output_p);
1344 else if (TREE_CODE (stmt) == COND_EXPR || TREE_CODE (stmt) == SWITCH_EXPR)
1346 /* If STMT is a conditional branch, see if we can determine
1347 which branch will be taken. */
1348 return visit_cond_stmt (stmt, taken_edge_p);
1351 /* Any other kind of statement is not interesting for constant
1352 propagation and, therefore, not worth simulating. */
1353 if (dump_file && (dump_flags & TDF_DETAILS))
1354 fprintf (dump_file, "No interesting values produced. Marked VARYING.\n");
1356 /* Definitions made by statements other than assignments to
1357 SSA_NAMEs represent unknown modifications to their outputs.
1358 Mark them VARYING. */
1359 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
1361 prop_value_t v = { VARYING, NULL_TREE, NULL_TREE };
1362 set_lattice_value (def, v);
1365 return SSA_PROP_VARYING;
1369 /* Main entry point for SSA Conditional Constant Propagation. */
1372 execute_ssa_ccp (bool store_ccp)
1374 do_store_ccp = store_ccp;
1376 ssa_propagate (ccp_visit_stmt, ccp_visit_phi_node);
1384 execute_ssa_ccp (false);
1391 return flag_tree_ccp != 0;
1395 struct tree_opt_pass pass_ccp =
1398 gate_ccp, /* gate */
1399 do_ssa_ccp, /* execute */
1402 0, /* static_pass_number */
1403 TV_TREE_CCP, /* tv_id */
1404 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
1405 0, /* properties_provided */
1406 PROP_tmt_usage, /* properties_destroyed */
1407 0, /* todo_flags_start */
1408 TODO_cleanup_cfg | TODO_dump_func | TODO_update_ssa
1409 | TODO_ggc_collect | TODO_verify_ssa
1410 | TODO_verify_stmts | TODO_update_tmt_usage, /* todo_flags_finish */
1416 do_ssa_store_ccp (void)
1418 /* If STORE-CCP is not enabled, we just run regular CCP. */
1419 execute_ssa_ccp (flag_tree_store_ccp != 0);
1423 gate_store_ccp (void)
1425 /* STORE-CCP is enabled only with -ftree-store-ccp, but when
1426 -fno-tree-store-ccp is specified, we should run regular CCP.
1427 That's why the pass is enabled with either flag. */
1428 return flag_tree_store_ccp != 0 || flag_tree_ccp != 0;
1432 struct tree_opt_pass pass_store_ccp =
1434 "store_ccp", /* name */
1435 gate_store_ccp, /* gate */
1436 do_ssa_store_ccp, /* execute */
1439 0, /* static_pass_number */
1440 TV_TREE_STORE_CCP, /* tv_id */
1441 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
1442 0, /* properties_provided */
1443 PROP_tmt_usage, /* properties_destroyed */
1444 0, /* todo_flags_start */
1445 TODO_dump_func | TODO_update_ssa
1446 | TODO_ggc_collect | TODO_verify_ssa
1448 | TODO_verify_stmts | TODO_update_tmt_usage, /* todo_flags_finish */
1452 /* Given a constant value VAL for bitfield FIELD, and a destination
1453 variable VAR, return VAL appropriately widened to fit into VAR. If
1454 FIELD is wider than HOST_WIDE_INT, NULL is returned. */
1457 widen_bitfield (tree val, tree field, tree var)
1459 unsigned HOST_WIDE_INT var_size, field_size;
1461 unsigned HOST_WIDE_INT mask;
1464 /* We can only do this if the size of the type and field and VAL are
1465 all constants representable in HOST_WIDE_INT. */
1466 if (!host_integerp (TYPE_SIZE (TREE_TYPE (var)), 1)
1467 || !host_integerp (DECL_SIZE (field), 1)
1468 || !host_integerp (val, 0))
1471 var_size = tree_low_cst (TYPE_SIZE (TREE_TYPE (var)), 1);
1472 field_size = tree_low_cst (DECL_SIZE (field), 1);
1474 /* Give up if either the bitfield or the variable are too wide. */
1475 if (field_size > HOST_BITS_PER_WIDE_INT || var_size > HOST_BITS_PER_WIDE_INT)
1478 gcc_assert (var_size >= field_size);
1480 /* If the sign bit of the value is not set or the field's type is unsigned,
1481 just mask off the high order bits of the value. */
1482 if (DECL_UNSIGNED (field)
1483 || !(tree_low_cst (val, 0) & (((HOST_WIDE_INT)1) << (field_size - 1))))
1485 /* Zero extension. Build a mask with the lower 'field_size' bits
1486 set and a BIT_AND_EXPR node to clear the high order bits of
1488 for (i = 0, mask = 0; i < field_size; i++)
1489 mask |= ((HOST_WIDE_INT) 1) << i;
1491 wide_val = fold_build2 (BIT_AND_EXPR, TREE_TYPE (var), val,
1492 build_int_cst (TREE_TYPE (var), mask));
1496 /* Sign extension. Create a mask with the upper 'field_size'
1497 bits set and a BIT_IOR_EXPR to set the high order bits of the
1499 for (i = 0, mask = 0; i < (var_size - field_size); i++)
1500 mask |= ((HOST_WIDE_INT) 1) << (var_size - i - 1);
1502 wide_val = fold_build2 (BIT_IOR_EXPR, TREE_TYPE (var), val,
1503 build_int_cst (TREE_TYPE (var), mask));
1510 /* A subroutine of fold_stmt_r. Attempts to fold *(A+O) to A[X].
1511 BASE is an array type. OFFSET is a byte displacement. ORIG_TYPE
1512 is the desired result type. */
1515 maybe_fold_offset_to_array_ref (tree base, tree offset, tree orig_type)
1517 tree min_idx, idx, elt_offset = integer_zero_node;
1518 tree array_type, elt_type, elt_size;
1520 /* If BASE is an ARRAY_REF, we can pick up another offset (this time
1521 measured in units of the size of elements type) from that ARRAY_REF).
1522 We can't do anything if either is variable.
1524 The case we handle here is *(&A[N]+O). */
1525 if (TREE_CODE (base) == ARRAY_REF)
1527 tree low_bound = array_ref_low_bound (base);
1529 elt_offset = TREE_OPERAND (base, 1);
1530 if (TREE_CODE (low_bound) != INTEGER_CST
1531 || TREE_CODE (elt_offset) != INTEGER_CST)
1534 elt_offset = int_const_binop (MINUS_EXPR, elt_offset, low_bound, 0);
1535 base = TREE_OPERAND (base, 0);
1538 /* Ignore stupid user tricks of indexing non-array variables. */
1539 array_type = TREE_TYPE (base);
1540 if (TREE_CODE (array_type) != ARRAY_TYPE)
1542 elt_type = TREE_TYPE (array_type);
1543 if (!lang_hooks.types_compatible_p (orig_type, elt_type))
1546 /* If OFFSET and ELT_OFFSET are zero, we don't care about the size of the
1547 element type (so we can use the alignment if it's not constant).
1548 Otherwise, compute the offset as an index by using a division. If the
1549 division isn't exact, then don't do anything. */
1550 elt_size = TYPE_SIZE_UNIT (elt_type);
1551 if (integer_zerop (offset))
1553 if (TREE_CODE (elt_size) != INTEGER_CST)
1554 elt_size = size_int (TYPE_ALIGN (elt_type));
1556 idx = integer_zero_node;
1560 unsigned HOST_WIDE_INT lquo, lrem;
1561 HOST_WIDE_INT hquo, hrem;
1563 if (TREE_CODE (elt_size) != INTEGER_CST
1564 || div_and_round_double (TRUNC_DIV_EXPR, 1,
1565 TREE_INT_CST_LOW (offset),
1566 TREE_INT_CST_HIGH (offset),
1567 TREE_INT_CST_LOW (elt_size),
1568 TREE_INT_CST_HIGH (elt_size),
1569 &lquo, &hquo, &lrem, &hrem)
1573 idx = build_int_cst_wide (NULL_TREE, lquo, hquo);
1576 /* Assume the low bound is zero. If there is a domain type, get the
1577 low bound, if any, convert the index into that type, and add the
1579 min_idx = integer_zero_node;
1580 if (TYPE_DOMAIN (array_type))
1582 if (TYPE_MIN_VALUE (TYPE_DOMAIN (array_type)))
1583 min_idx = TYPE_MIN_VALUE (TYPE_DOMAIN (array_type));
1585 min_idx = fold_convert (TYPE_DOMAIN (array_type), min_idx);
1587 if (TREE_CODE (min_idx) != INTEGER_CST)
1590 idx = fold_convert (TYPE_DOMAIN (array_type), idx);
1591 elt_offset = fold_convert (TYPE_DOMAIN (array_type), elt_offset);
1594 if (!integer_zerop (min_idx))
1595 idx = int_const_binop (PLUS_EXPR, idx, min_idx, 0);
1596 if (!integer_zerop (elt_offset))
1597 idx = int_const_binop (PLUS_EXPR, idx, elt_offset, 0);
1599 return build4 (ARRAY_REF, orig_type, base, idx, min_idx,
1600 size_int (tree_low_cst (elt_size, 1)
1601 / (TYPE_ALIGN_UNIT (elt_type))));
1605 /* A subroutine of fold_stmt_r. Attempts to fold *(S+O) to S.X.
1606 BASE is a record type. OFFSET is a byte displacement. ORIG_TYPE
1607 is the desired result type. */
1608 /* ??? This doesn't handle class inheritance. */
1611 maybe_fold_offset_to_component_ref (tree record_type, tree base, tree offset,
1612 tree orig_type, bool base_is_ptr)
1614 tree f, t, field_type, tail_array_field, field_offset;
1616 if (TREE_CODE (record_type) != RECORD_TYPE
1617 && TREE_CODE (record_type) != UNION_TYPE
1618 && TREE_CODE (record_type) != QUAL_UNION_TYPE)
1621 /* Short-circuit silly cases. */
1622 if (lang_hooks.types_compatible_p (record_type, orig_type))
1625 tail_array_field = NULL_TREE;
1626 for (f = TYPE_FIELDS (record_type); f ; f = TREE_CHAIN (f))
1630 if (TREE_CODE (f) != FIELD_DECL)
1632 if (DECL_BIT_FIELD (f))
1635 field_offset = byte_position (f);
1636 if (TREE_CODE (field_offset) != INTEGER_CST)
1639 /* ??? Java creates "interesting" fields for representing base classes.
1640 They have no name, and have no context. With no context, we get into
1641 trouble with nonoverlapping_component_refs_p. Skip them. */
1642 if (!DECL_FIELD_CONTEXT (f))
1645 /* The previous array field isn't at the end. */
1646 tail_array_field = NULL_TREE;
1648 /* Check to see if this offset overlaps with the field. */
1649 cmp = tree_int_cst_compare (field_offset, offset);
1653 field_type = TREE_TYPE (f);
1655 /* Here we exactly match the offset being checked. If the types match,
1656 then we can return that field. */
1658 && lang_hooks.types_compatible_p (orig_type, field_type))
1661 base = build1 (INDIRECT_REF, record_type, base);
1662 t = build3 (COMPONENT_REF, field_type, base, f, NULL_TREE);
1666 /* Don't care about offsets into the middle of scalars. */
1667 if (!AGGREGATE_TYPE_P (field_type))
1670 /* Check for array at the end of the struct. This is often
1671 used as for flexible array members. We should be able to
1672 turn this into an array access anyway. */
1673 if (TREE_CODE (field_type) == ARRAY_TYPE)
1674 tail_array_field = f;
1676 /* Check the end of the field against the offset. */
1677 if (!DECL_SIZE_UNIT (f)
1678 || TREE_CODE (DECL_SIZE_UNIT (f)) != INTEGER_CST)
1680 t = int_const_binop (MINUS_EXPR, offset, field_offset, 1);
1681 if (!tree_int_cst_lt (t, DECL_SIZE_UNIT (f)))
1684 /* If we matched, then set offset to the displacement into
1690 if (!tail_array_field)
1693 f = tail_array_field;
1694 field_type = TREE_TYPE (f);
1695 offset = int_const_binop (MINUS_EXPR, offset, byte_position (f), 1);
1698 /* If we get here, we've got an aggregate field, and a possibly
1699 nonzero offset into them. Recurse and hope for a valid match. */
1701 base = build1 (INDIRECT_REF, record_type, base);
1702 base = build3 (COMPONENT_REF, field_type, base, f, NULL_TREE);
1704 t = maybe_fold_offset_to_array_ref (base, offset, orig_type);
1707 return maybe_fold_offset_to_component_ref (field_type, base, offset,
1712 /* A subroutine of fold_stmt_r. Attempt to simplify *(BASE+OFFSET).
1713 Return the simplified expression, or NULL if nothing could be done. */
1716 maybe_fold_stmt_indirect (tree expr, tree base, tree offset)
1720 /* We may well have constructed a double-nested PLUS_EXPR via multiple
1721 substitutions. Fold that down to one. Remove NON_LVALUE_EXPRs that
1722 are sometimes added. */
1724 STRIP_TYPE_NOPS (base);
1725 TREE_OPERAND (expr, 0) = base;
1727 /* One possibility is that the address reduces to a string constant. */
1728 t = fold_read_from_constant_string (expr);
1732 /* Add in any offset from a PLUS_EXPR. */
1733 if (TREE_CODE (base) == PLUS_EXPR)
1737 offset2 = TREE_OPERAND (base, 1);
1738 if (TREE_CODE (offset2) != INTEGER_CST)
1740 base = TREE_OPERAND (base, 0);
1742 offset = int_const_binop (PLUS_EXPR, offset, offset2, 1);
1745 if (TREE_CODE (base) == ADDR_EXPR)
1747 /* Strip the ADDR_EXPR. */
1748 base = TREE_OPERAND (base, 0);
1750 /* Fold away CONST_DECL to its value, if the type is scalar. */
1751 if (TREE_CODE (base) == CONST_DECL
1752 && ccp_decl_initial_min_invariant (DECL_INITIAL (base)))
1753 return DECL_INITIAL (base);
1755 /* Try folding *(&B+O) to B[X]. */
1756 t = maybe_fold_offset_to_array_ref (base, offset, TREE_TYPE (expr));
1760 /* Try folding *(&B+O) to B.X. */
1761 t = maybe_fold_offset_to_component_ref (TREE_TYPE (base), base, offset,
1762 TREE_TYPE (expr), false);
1766 /* Fold *&B to B. We can only do this if EXPR is the same type
1767 as BASE. We can't do this if EXPR is the element type of an array
1768 and BASE is the array. */
1769 if (integer_zerop (offset)
1770 && lang_hooks.types_compatible_p (TREE_TYPE (base),
1776 /* We can get here for out-of-range string constant accesses,
1777 such as "_"[3]. Bail out of the entire substitution search
1778 and arrange for the entire statement to be replaced by a
1779 call to __builtin_trap. In all likelihood this will all be
1780 constant-folded away, but in the meantime we can't leave with
1781 something that get_expr_operands can't understand. */
1785 if (TREE_CODE (t) == ADDR_EXPR
1786 && TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST)
1788 /* FIXME: Except that this causes problems elsewhere with dead
1789 code not being deleted, and we die in the rtl expanders
1790 because we failed to remove some ssa_name. In the meantime,
1791 just return zero. */
1792 /* FIXME2: This condition should be signaled by
1793 fold_read_from_constant_string directly, rather than
1794 re-checking for it here. */
1795 return integer_zero_node;
1798 /* Try folding *(B+O) to B->X. Still an improvement. */
1799 if (POINTER_TYPE_P (TREE_TYPE (base)))
1801 t = maybe_fold_offset_to_component_ref (TREE_TYPE (TREE_TYPE (base)),
1803 TREE_TYPE (expr), true);
1809 /* Otherwise we had an offset that we could not simplify. */
1814 /* A subroutine of fold_stmt_r. EXPR is a PLUS_EXPR.
1816 A quaint feature extant in our address arithmetic is that there
1817 can be hidden type changes here. The type of the result need
1818 not be the same as the type of the input pointer.
1820 What we're after here is an expression of the form
1821 (T *)(&array + const)
1822 where the cast doesn't actually exist, but is implicit in the
1823 type of the PLUS_EXPR. We'd like to turn this into
1825 which may be able to propagate further. */
1828 maybe_fold_stmt_addition (tree expr)
1830 tree op0 = TREE_OPERAND (expr, 0);
1831 tree op1 = TREE_OPERAND (expr, 1);
1832 tree ptr_type = TREE_TYPE (expr);
1835 bool subtract = (TREE_CODE (expr) == MINUS_EXPR);
1837 /* We're only interested in pointer arithmetic. */
1838 if (!POINTER_TYPE_P (ptr_type))
1840 /* Canonicalize the integral operand to op1. */
1841 if (INTEGRAL_TYPE_P (TREE_TYPE (op0)))
1845 t = op0, op0 = op1, op1 = t;
1847 /* It had better be a constant. */
1848 if (TREE_CODE (op1) != INTEGER_CST)
1850 /* The first operand should be an ADDR_EXPR. */
1851 if (TREE_CODE (op0) != ADDR_EXPR)
1853 op0 = TREE_OPERAND (op0, 0);
1855 /* If the first operand is an ARRAY_REF, expand it so that we can fold
1856 the offset into it. */
1857 while (TREE_CODE (op0) == ARRAY_REF)
1859 tree array_obj = TREE_OPERAND (op0, 0);
1860 tree array_idx = TREE_OPERAND (op0, 1);
1861 tree elt_type = TREE_TYPE (op0);
1862 tree elt_size = TYPE_SIZE_UNIT (elt_type);
1865 if (TREE_CODE (array_idx) != INTEGER_CST)
1867 if (TREE_CODE (elt_size) != INTEGER_CST)
1870 /* Un-bias the index by the min index of the array type. */
1871 min_idx = TYPE_DOMAIN (TREE_TYPE (array_obj));
1874 min_idx = TYPE_MIN_VALUE (min_idx);
1877 if (TREE_CODE (min_idx) != INTEGER_CST)
1880 array_idx = convert (TREE_TYPE (min_idx), array_idx);
1881 if (!integer_zerop (min_idx))
1882 array_idx = int_const_binop (MINUS_EXPR, array_idx,
1887 /* Convert the index to a byte offset. */
1888 array_idx = convert (sizetype, array_idx);
1889 array_idx = int_const_binop (MULT_EXPR, array_idx, elt_size, 0);
1891 /* Update the operands for the next round, or for folding. */
1892 /* If we're manipulating unsigned types, then folding into negative
1893 values can produce incorrect results. Particularly if the type
1894 is smaller than the width of the pointer. */
1896 && TYPE_UNSIGNED (TREE_TYPE (op1))
1897 && tree_int_cst_lt (array_idx, op1))
1899 op1 = int_const_binop (subtract ? MINUS_EXPR : PLUS_EXPR,
1905 /* If we weren't able to fold the subtraction into another array reference,
1906 canonicalize the integer for passing to the array and component ref
1907 simplification functions. */
1910 if (TYPE_UNSIGNED (TREE_TYPE (op1)))
1912 op1 = fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1);
1913 /* ??? In theory fold should always produce another integer. */
1914 if (op1 == NULL || TREE_CODE (op1) != INTEGER_CST)
1918 ptd_type = TREE_TYPE (ptr_type);
1920 /* At which point we can try some of the same things as for indirects. */
1921 t = maybe_fold_offset_to_array_ref (op0, op1, ptd_type);
1923 t = maybe_fold_offset_to_component_ref (TREE_TYPE (op0), op0, op1,
1926 t = build1 (ADDR_EXPR, ptr_type, t);
1931 /* For passing state through walk_tree into fold_stmt_r and its
1934 struct fold_stmt_r_data
1937 bool *inside_addr_expr_p;
1940 /* Subroutine of fold_stmt called via walk_tree. We perform several
1941 simplifications of EXPR_P, mostly having to do with pointer arithmetic. */
1944 fold_stmt_r (tree *expr_p, int *walk_subtrees, void *data)
1946 struct fold_stmt_r_data *fold_stmt_r_data = (struct fold_stmt_r_data *) data;
1947 bool *inside_addr_expr_p = fold_stmt_r_data->inside_addr_expr_p;
1948 bool *changed_p = fold_stmt_r_data->changed_p;
1949 tree expr = *expr_p, t;
1951 /* ??? It'd be nice if walk_tree had a pre-order option. */
1952 switch (TREE_CODE (expr))
1955 t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL);
1960 t = maybe_fold_stmt_indirect (expr, TREE_OPERAND (expr, 0),
1964 /* ??? Could handle more ARRAY_REFs here, as a variant of INDIRECT_REF.
1965 We'd only want to bother decomposing an existing ARRAY_REF if
1966 the base array is found to have another offset contained within.
1967 Otherwise we'd be wasting time. */
1969 /* If we are not processing expressions found within an
1970 ADDR_EXPR, then we can fold constant array references. */
1971 if (!*inside_addr_expr_p)
1972 t = fold_read_from_constant_string (expr);
1978 *inside_addr_expr_p = true;
1979 t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL);
1980 *inside_addr_expr_p = false;
1985 /* Set TREE_INVARIANT properly so that the value is properly
1986 considered constant, and so gets propagated as expected. */
1988 recompute_tree_invariant_for_addr_expr (expr);
1993 t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL);
1996 t = walk_tree (&TREE_OPERAND (expr, 1), fold_stmt_r, data, NULL);
2001 t = maybe_fold_stmt_addition (expr);
2005 t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL);
2010 /* Make sure the FIELD_DECL is actually a field in the type on the lhs.
2011 We've already checked that the records are compatible, so we should
2012 come up with a set of compatible fields. */
2014 tree expr_record = TREE_TYPE (TREE_OPERAND (expr, 0));
2015 tree expr_field = TREE_OPERAND (expr, 1);
2017 if (DECL_FIELD_CONTEXT (expr_field) != TYPE_MAIN_VARIANT (expr_record))
2019 expr_field = find_compatible_field (expr_record, expr_field);
2020 TREE_OPERAND (expr, 1) = expr_field;
2025 case TARGET_MEM_REF:
2026 t = maybe_fold_tmr (expr);
2043 /* Return the string length, maximum string length or maximum value of
2045 If ARG is an SSA name variable, follow its use-def chains. If LENGTH
2046 is not NULL and, for TYPE == 0, its value is not equal to the length
2047 we determine or if we are unable to determine the length or value,
2048 return false. VISITED is a bitmap of visited variables.
2049 TYPE is 0 if string length should be returned, 1 for maximum string
2050 length and 2 for maximum value ARG can have. */
2053 get_maxval_strlen (tree arg, tree *length, bitmap visited, int type)
2055 tree var, def_stmt, val;
2057 if (TREE_CODE (arg) != SSA_NAME)
2062 if (TREE_CODE (val) != INTEGER_CST
2063 || tree_int_cst_sgn (val) < 0)
2067 val = c_strlen (arg, 1);
2075 if (TREE_CODE (*length) != INTEGER_CST
2076 || TREE_CODE (val) != INTEGER_CST)
2079 if (tree_int_cst_lt (*length, val))
2083 else if (simple_cst_equal (val, *length) != 1)
2091 /* If we were already here, break the infinite cycle. */
2092 if (bitmap_bit_p (visited, SSA_NAME_VERSION (arg)))
2094 bitmap_set_bit (visited, SSA_NAME_VERSION (arg));
2097 def_stmt = SSA_NAME_DEF_STMT (var);
2099 switch (TREE_CODE (def_stmt))
2105 /* The RHS of the statement defining VAR must either have a
2106 constant length or come from another SSA_NAME with a constant
2108 rhs = TREE_OPERAND (def_stmt, 1);
2110 return get_maxval_strlen (rhs, length, visited, type);
2115 /* All the arguments of the PHI node must have the same constant
2119 for (i = 0; i < PHI_NUM_ARGS (def_stmt); i++)
2121 tree arg = PHI_ARG_DEF (def_stmt, i);
2123 /* If this PHI has itself as an argument, we cannot
2124 determine the string length of this argument. However,
2125 if we can find a constant string length for the other
2126 PHI args then we can still be sure that this is a
2127 constant string length. So be optimistic and just
2128 continue with the next argument. */
2129 if (arg == PHI_RESULT (def_stmt))
2132 if (!get_maxval_strlen (arg, length, visited, type))
2148 /* Fold builtin call FN in statement STMT. If it cannot be folded into a
2149 constant, return NULL_TREE. Otherwise, return its constant value. */
2152 ccp_fold_builtin (tree stmt, tree fn)
2154 tree result, val[3];
2155 tree callee, arglist, a;
2156 int arg_mask, i, type;
2160 ignore = TREE_CODE (stmt) != MODIFY_EXPR;
2162 /* First try the generic builtin folder. If that succeeds, return the
2164 callee = get_callee_fndecl (fn);
2165 arglist = TREE_OPERAND (fn, 1);
2166 result = fold_builtin (callee, arglist, ignore);
2170 STRIP_NOPS (result);
2174 /* Ignore MD builtins. */
2175 if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_MD)
2178 /* If the builtin could not be folded, and it has no argument list,
2183 /* Limit the work only for builtins we know how to simplify. */
2184 switch (DECL_FUNCTION_CODE (callee))
2186 case BUILT_IN_STRLEN:
2187 case BUILT_IN_FPUTS:
2188 case BUILT_IN_FPUTS_UNLOCKED:
2192 case BUILT_IN_STRCPY:
2193 case BUILT_IN_STRNCPY:
2197 case BUILT_IN_MEMCPY_CHK:
2198 case BUILT_IN_MEMPCPY_CHK:
2199 case BUILT_IN_MEMMOVE_CHK:
2200 case BUILT_IN_MEMSET_CHK:
2201 case BUILT_IN_STRNCPY_CHK:
2205 case BUILT_IN_STRCPY_CHK:
2206 case BUILT_IN_STPCPY_CHK:
2210 case BUILT_IN_SNPRINTF_CHK:
2211 case BUILT_IN_VSNPRINTF_CHK:
2219 /* Try to use the dataflow information gathered by the CCP process. */
2220 visited = BITMAP_ALLOC (NULL);
2222 memset (val, 0, sizeof (val));
2223 for (i = 0, a = arglist;
2225 i++, arg_mask >>= 1, a = TREE_CHAIN (a))
2228 bitmap_clear (visited);
2229 if (!get_maxval_strlen (TREE_VALUE (a), &val[i], visited, type))
2233 BITMAP_FREE (visited);
2236 switch (DECL_FUNCTION_CODE (callee))
2238 case BUILT_IN_STRLEN:
2241 tree new = fold_convert (TREE_TYPE (fn), val[0]);
2243 /* If the result is not a valid gimple value, or not a cast
2244 of a valid gimple value, then we can not use the result. */
2245 if (is_gimple_val (new)
2246 || (is_gimple_cast (new)
2247 && is_gimple_val (TREE_OPERAND (new, 0))))
2252 case BUILT_IN_STRCPY:
2253 if (val[1] && is_gimple_val (val[1]))
2254 result = fold_builtin_strcpy (callee, arglist, val[1]);
2257 case BUILT_IN_STRNCPY:
2258 if (val[1] && is_gimple_val (val[1]))
2259 result = fold_builtin_strncpy (callee, arglist, val[1]);
2262 case BUILT_IN_FPUTS:
2263 result = fold_builtin_fputs (arglist,
2264 TREE_CODE (stmt) != MODIFY_EXPR, 0,
2268 case BUILT_IN_FPUTS_UNLOCKED:
2269 result = fold_builtin_fputs (arglist,
2270 TREE_CODE (stmt) != MODIFY_EXPR, 1,
2274 case BUILT_IN_MEMCPY_CHK:
2275 case BUILT_IN_MEMPCPY_CHK:
2276 case BUILT_IN_MEMMOVE_CHK:
2277 case BUILT_IN_MEMSET_CHK:
2278 if (val[2] && is_gimple_val (val[2]))
2279 result = fold_builtin_memory_chk (callee, arglist, val[2], ignore,
2280 DECL_FUNCTION_CODE (callee));
2283 case BUILT_IN_STRCPY_CHK:
2284 case BUILT_IN_STPCPY_CHK:
2285 if (val[1] && is_gimple_val (val[1]))
2286 result = fold_builtin_stxcpy_chk (callee, arglist, val[1], ignore,
2287 DECL_FUNCTION_CODE (callee));
2290 case BUILT_IN_STRNCPY_CHK:
2291 if (val[2] && is_gimple_val (val[2]))
2292 result = fold_builtin_strncpy_chk (arglist, val[2]);
2295 case BUILT_IN_SNPRINTF_CHK:
2296 case BUILT_IN_VSNPRINTF_CHK:
2297 if (val[1] && is_gimple_val (val[1]))
2298 result = fold_builtin_snprintf_chk (arglist, val[1],
2299 DECL_FUNCTION_CODE (callee));
2306 if (result && ignore)
2307 result = fold_ignored_result (result);
2312 /* Fold the statement pointed to by STMT_P. In some cases, this function may
2313 replace the whole statement with a new one. Returns true iff folding
2314 makes any changes. */
2317 fold_stmt (tree *stmt_p)
2319 tree rhs, result, stmt;
2320 struct fold_stmt_r_data fold_stmt_r_data;
2321 bool changed = false;
2322 bool inside_addr_expr = false;
2324 fold_stmt_r_data.changed_p = &changed;
2325 fold_stmt_r_data.inside_addr_expr_p = &inside_addr_expr;
2329 /* If we replaced constants and the statement makes pointer dereferences,
2330 then we may need to fold instances of *&VAR into VAR, etc. */
2331 if (walk_tree (stmt_p, fold_stmt_r, &fold_stmt_r_data, NULL))
2334 = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP],
2339 rhs = get_rhs (stmt);
2344 if (TREE_CODE (rhs) == CALL_EXPR)
2348 /* Check for builtins that CCP can handle using information not
2349 available in the generic fold routines. */
2350 callee = get_callee_fndecl (rhs);
2351 if (callee && DECL_BUILT_IN (callee))
2352 result = ccp_fold_builtin (stmt, rhs);
2355 /* Check for resolvable OBJ_TYPE_REF. The only sorts we can resolve
2356 here are when we've propagated the address of a decl into the
2358 /* ??? Should perhaps do this in fold proper. However, doing it
2359 there requires that we create a new CALL_EXPR, and that requires
2360 copying EH region info to the new node. Easier to just do it
2361 here where we can just smash the call operand. Also
2362 CALL_EXPR_RETURN_SLOT_OPT needs to be handled correctly and
2363 copied, fold_ternary does not have not information. */
2364 callee = TREE_OPERAND (rhs, 0);
2365 if (TREE_CODE (callee) == OBJ_TYPE_REF
2366 && lang_hooks.fold_obj_type_ref
2367 && TREE_CODE (OBJ_TYPE_REF_OBJECT (callee)) == ADDR_EXPR
2368 && DECL_P (TREE_OPERAND
2369 (OBJ_TYPE_REF_OBJECT (callee), 0)))
2373 /* ??? Caution: Broken ADDR_EXPR semantics means that
2374 looking at the type of the operand of the addr_expr
2375 can yield an array type. See silly exception in
2376 check_pointer_types_r. */
2378 t = TREE_TYPE (TREE_TYPE (OBJ_TYPE_REF_OBJECT (callee)));
2379 t = lang_hooks.fold_obj_type_ref (callee, t);
2382 TREE_OPERAND (rhs, 0) = t;
2389 /* If we couldn't fold the RHS, hand over to the generic fold routines. */
2390 if (result == NULL_TREE)
2391 result = fold (rhs);
2393 /* Strip away useless type conversions. Both the NON_LVALUE_EXPR that
2394 may have been added by fold, and "useless" type conversions that might
2395 now be apparent due to propagation. */
2396 STRIP_USELESS_TYPE_CONVERSION (result);
2399 changed |= set_rhs (stmt_p, result);
2404 /* Perform the minimal folding on statement STMT. Only operations like
2405 *&x created by constant propagation are handled. The statement cannot
2406 be replaced with a new one. */
2409 fold_stmt_inplace (tree stmt)
2411 tree old_stmt = stmt, rhs, new_rhs;
2412 struct fold_stmt_r_data fold_stmt_r_data;
2413 bool changed = false;
2414 bool inside_addr_expr = false;
2416 fold_stmt_r_data.changed_p = &changed;
2417 fold_stmt_r_data.inside_addr_expr_p = &inside_addr_expr;
2419 walk_tree (&stmt, fold_stmt_r, &fold_stmt_r_data, NULL);
2420 gcc_assert (stmt == old_stmt);
2422 rhs = get_rhs (stmt);
2423 if (!rhs || rhs == stmt)
2426 new_rhs = fold (rhs);
2427 STRIP_USELESS_TYPE_CONVERSION (new_rhs);
2431 changed |= set_rhs (&stmt, new_rhs);
2432 gcc_assert (stmt == old_stmt);
2437 /* Convert EXPR into a GIMPLE value suitable for substitution on the
2438 RHS of an assignment. Insert the necessary statements before
2442 convert_to_gimple_builtin (block_stmt_iterator *si_p, tree expr)
2444 tree_stmt_iterator ti;
2445 tree stmt = bsi_stmt (*si_p);
2446 tree tmp, stmts = NULL;
2448 push_gimplify_context ();
2449 tmp = get_initialized_tmp_var (expr, &stmts, NULL);
2450 pop_gimplify_context (NULL);
2452 if (EXPR_HAS_LOCATION (stmt))
2453 annotate_all_with_locus (&stmts, EXPR_LOCATION (stmt));
2455 /* The replacement can expose previously unreferenced variables. */
2456 for (ti = tsi_start (stmts); !tsi_end_p (ti); tsi_next (&ti))
2458 tree new_stmt = tsi_stmt (ti);
2459 find_new_referenced_vars (tsi_stmt_ptr (ti));
2460 bsi_insert_before (si_p, new_stmt, BSI_NEW_STMT);
2461 mark_new_vars_to_rename (bsi_stmt (*si_p));
2469 /* A simple pass that attempts to fold all builtin functions. This pass
2470 is run after we've propagated as many constants as we can. */
2473 execute_fold_all_builtins (void)
2475 bool cfg_changed = false;
2479 block_stmt_iterator i;
2480 for (i = bsi_start (bb); !bsi_end_p (i); )
2482 tree *stmtp = bsi_stmt_ptr (i);
2483 tree old_stmt = *stmtp;
2484 tree call = get_rhs (*stmtp);
2485 tree callee, result;
2486 enum built_in_function fcode;
2488 if (!call || TREE_CODE (call) != CALL_EXPR)
2493 callee = get_callee_fndecl (call);
2494 if (!callee || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL)
2499 fcode = DECL_FUNCTION_CODE (callee);
2501 result = ccp_fold_builtin (*stmtp, call);
2503 switch (DECL_FUNCTION_CODE (callee))
2505 case BUILT_IN_CONSTANT_P:
2506 /* Resolve __builtin_constant_p. If it hasn't been
2507 folded to integer_one_node by now, it's fairly
2508 certain that the value simply isn't constant. */
2509 result = integer_zero_node;
2517 if (dump_file && (dump_flags & TDF_DETAILS))
2519 fprintf (dump_file, "Simplified\n ");
2520 print_generic_stmt (dump_file, *stmtp, dump_flags);
2523 if (!set_rhs (stmtp, result))
2525 result = convert_to_gimple_builtin (&i, result);
2528 bool ok = set_rhs (stmtp, result);
2533 mark_new_vars_to_rename (*stmtp);
2534 if (maybe_clean_or_replace_eh_stmt (old_stmt, *stmtp)
2535 && tree_purge_dead_eh_edges (bb))
2538 if (dump_file && (dump_flags & TDF_DETAILS))
2540 fprintf (dump_file, "to\n ");
2541 print_generic_stmt (dump_file, *stmtp, dump_flags);
2542 fprintf (dump_file, "\n");
2545 /* Retry the same statement if it changed into another
2546 builtin, there might be new opportunities now. */
2547 call = get_rhs (*stmtp);
2548 if (!call || TREE_CODE (call) != CALL_EXPR)
2553 callee = get_callee_fndecl (call);
2555 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2556 || DECL_FUNCTION_CODE (callee) == fcode)
2561 /* Delete unreachable blocks. */
2563 cleanup_tree_cfg ();
2567 struct tree_opt_pass pass_fold_builtins =
2571 execute_fold_all_builtins, /* execute */
2574 0, /* static_pass_number */
2576 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
2577 0, /* properties_provided */
2578 0, /* properties_destroyed */
2579 0, /* todo_flags_start */
2582 | TODO_update_ssa, /* todo_flags_finish */