1 /* Conditional constant propagation pass for the GNU compiler.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Adapted from original RTL SSA-CCP by Daniel Berlin <dberlin@dberlin.org>
4 Adapted to GIMPLE trees by Diego Novillo <dnovillo@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 GCC is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* Conditional constant propagation.
27 Constant propagation with conditional branches,
28 Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
30 Building an Optimizing Compiler,
31 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
33 Advanced Compiler Design and Implementation,
34 Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
38 #include "coretypes.h"
45 #include "basic-block.h"
50 #include "diagnostic.h"
52 #include "tree-dump.h"
53 #include "tree-flow.h"
54 #include "tree-pass.h"
55 #include "tree-ssa-propagate.h"
56 #include "langhooks.h"
59 /* Possible lattice values. */
69 /* Main structure for CCP. Contains the lattice value and, if it's a
70 constant, the constant value. */
73 latticevalue lattice_val;
77 /* This is used to track the current value of each variable. */
78 static value *value_vector;
81 /* Dump lattice value VAL to file OUTF prefixed by PREFIX. */
84 dump_lattice_value (FILE *outf, const char *prefix, value val)
86 switch (val.lattice_val)
89 fprintf (outf, "%sUNDEFINED", prefix);
92 fprintf (outf, "%sVARYING", prefix);
95 fprintf (outf, "%sUNKNOWN_VAL", prefix);
98 fprintf (outf, "%sCONSTANT ", prefix);
99 print_generic_expr (outf, val.const_val, dump_flags);
107 /* Return a default value for variable VAR using the following rules:
109 1- Function arguments are considered VARYING.
111 2- Global and static variables that are declared constant are
114 3- Any other virtually defined variable is considered UNKNOWN_VAL.
116 4- Any other value is considered UNDEFINED. This is useful when
117 considering PHI nodes. PHI arguments that are undefined do not
118 change the constant value of the PHI node, which allows for more
119 constants to be propagated. */
122 get_default_value (tree var)
127 if (TREE_CODE (var) == SSA_NAME)
128 sym = SSA_NAME_VAR (var);
131 gcc_assert (DECL_P (var));
135 val.lattice_val = UNDEFINED;
136 val.const_val = NULL_TREE;
138 if (TREE_CODE (var) == SSA_NAME
139 && SSA_NAME_VALUE (var)
140 && is_gimple_min_invariant (SSA_NAME_VALUE (var)))
142 val.lattice_val = CONSTANT;
143 val.const_val = SSA_NAME_VALUE (var);
145 else if (TREE_CODE (sym) == PARM_DECL || TREE_THIS_VOLATILE (sym))
147 /* Function arguments and volatile variables are considered VARYING. */
148 val.lattice_val = VARYING;
150 else if (TREE_STATIC (sym))
152 /* Globals and static variables are considered UNKNOWN_VAL,
153 unless they are declared 'const'. */
154 if (TREE_READONLY (sym)
155 && DECL_INITIAL (sym)
156 && is_gimple_min_invariant (DECL_INITIAL (sym)))
158 val.lattice_val = CONSTANT;
159 val.const_val = DECL_INITIAL (sym);
163 val.const_val = NULL_TREE;
164 val.lattice_val = UNKNOWN_VAL;
167 else if (!is_gimple_reg (sym))
169 val.const_val = NULL_TREE;
170 val.lattice_val = UNKNOWN_VAL;
175 tree stmt = SSA_NAME_DEF_STMT (var);
177 if (!IS_EMPTY_STMT (stmt))
179 code = TREE_CODE (stmt);
180 if (code != MODIFY_EXPR && code != PHI_NODE)
181 val.lattice_val = VARYING;
188 /* Get the constant value associated with variable VAR. */
195 gcc_assert (TREE_CODE (var) == SSA_NAME);
197 val = &value_vector[SSA_NAME_VERSION (var)];
198 if (val->lattice_val == UNINITIALIZED)
199 *val = get_default_value (var);
205 /* Set the lattice value for variable VAR to VAL. Return true if VAL
206 is different from VAR's previous value. */
209 set_lattice_value (tree var, value val)
211 value *old = get_value (var);
213 if (val.lattice_val == UNDEFINED)
215 /* CONSTANT->UNDEFINED is never a valid state transition. */
216 gcc_assert (old->lattice_val != CONSTANT);
218 /* UNKNOWN_VAL->UNDEFINED is never a valid state transition. */
219 gcc_assert (old->lattice_val != UNKNOWN_VAL);
221 /* VARYING->UNDEFINED is generally not a valid state transition,
222 except for values which are initialized to VARYING. */
223 gcc_assert (old->lattice_val != VARYING
224 || get_default_value (var).lattice_val == VARYING);
226 else if (val.lattice_val == CONSTANT)
227 /* VARYING -> CONSTANT is an invalid state transition, except
228 for objects which start off in a VARYING state. */
229 gcc_assert (old->lattice_val != VARYING
230 || get_default_value (var).lattice_val == VARYING);
232 /* If the constant for VAR has changed, then this VAR is really varying. */
233 if (old->lattice_val == CONSTANT
234 && val.lattice_val == CONSTANT
235 && !simple_cst_equal (old->const_val, val.const_val))
237 val.lattice_val = VARYING;
238 val.const_val = NULL_TREE;
241 if (old->lattice_val != val.lattice_val)
243 if (dump_file && (dump_flags & TDF_DETAILS))
245 dump_lattice_value (dump_file, "Lattice value changed to ", val);
246 fprintf (dump_file, ". Adding definition to SSA edges.\n");
257 /* Set the lattice value for the variable VAR to VARYING. */
260 def_to_varying (tree var)
263 val.lattice_val = VARYING;
264 val.const_val = NULL_TREE;
265 set_lattice_value (var, val);
269 /* Return the likely latticevalue for STMT.
271 If STMT has no operands, then return CONSTANT.
273 Else if any operands of STMT are undefined, then return UNDEFINED.
275 Else if any operands of STMT are constants, then return CONSTANT.
277 Else return VARYING. */
280 likely_value (tree stmt)
283 int found_constant = 0;
288 /* If the statement makes aliased loads or has volatile operands, it
289 won't fold to a constant value. */
290 ann = stmt_ann (stmt);
291 if (ann->makes_aliased_loads || ann->has_volatile_ops)
294 /* A CALL_EXPR is assumed to be varying. This may be overly conservative,
295 in the presence of const and pure calls. */
296 if (get_call_expr_in (stmt) != NULL_TREE)
299 get_stmt_operands (stmt);
301 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
303 value *val = get_value (use);
305 if (val->lattice_val == UNDEFINED)
308 if (val->lattice_val == CONSTANT)
312 vuses = VUSE_OPS (ann);
314 if (NUM_VUSES (vuses))
316 tree vuse = VUSE_OP (vuses, 0);
317 value *val = get_value (vuse);
319 if (val->lattice_val == UNKNOWN_VAL)
322 /* There should be no VUSE operands that are UNDEFINED. */
323 gcc_assert (val->lattice_val != UNDEFINED);
325 if (val->lattice_val == CONSTANT)
329 return ((found_constant || (!USE_OPS (ann) && !vuses)) ? CONSTANT : VARYING);
333 /* Function indicating whether we ought to include information for VAR
334 when calculating immediate uses. */
337 need_imm_uses_for (tree var)
339 return get_value (var)->lattice_val != VARYING;
343 /* Initialize local data structures for CCP. */
346 ccp_initialize (void)
351 value_vector = (value *) xmalloc (num_ssa_names * sizeof (value));
352 memset (value_vector, 0, num_ssa_names * sizeof (value));
354 /* Set of SSA_NAMEs that are defined by a V_MAY_DEF. */
355 is_may_def = sbitmap_alloc (num_ssa_names);
356 sbitmap_zero (is_may_def);
358 /* Initialize simulation flags for PHI nodes and statements. */
361 block_stmt_iterator i;
363 /* Mark all V_MAY_DEF operands VARYING. */
364 for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
366 bool is_varying = false;
367 tree stmt = bsi_stmt (i);
371 get_stmt_operands (stmt);
373 /* Get the default value for each DEF and V_MUST_DEF. */
374 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter,
375 (SSA_OP_DEF | SSA_OP_VMUSTDEF))
377 if (get_value (def)->lattice_val == VARYING)
381 /* Mark all V_MAY_DEF operands VARYING. */
382 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_VMAYDEF)
384 get_value (def)->lattice_val = VARYING;
385 SET_BIT (is_may_def, SSA_NAME_VERSION (def));
388 /* Statements other than MODIFY_EXPR, COND_EXPR and
389 SWITCH_EXPR are not interesting for constant propagation.
390 Mark them VARYING. */
391 if (TREE_CODE (stmt) != MODIFY_EXPR
392 && TREE_CODE (stmt) != COND_EXPR
393 && TREE_CODE (stmt) != SWITCH_EXPR)
396 DONT_SIMULATE_AGAIN (stmt) = is_varying;
400 /* Now process PHI nodes. */
406 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
408 value *val = get_value (PHI_RESULT (phi));
410 for (x = 0; x < PHI_NUM_ARGS (phi); x++)
412 var = PHI_ARG_DEF (phi, x);
414 /* If one argument has a V_MAY_DEF, the result is
416 if (TREE_CODE (var) == SSA_NAME)
418 if (TEST_BIT (is_may_def, SSA_NAME_VERSION (var)))
420 val->lattice_val = VARYING;
421 SET_BIT (is_may_def, SSA_NAME_VERSION (PHI_RESULT (phi)));
427 DONT_SIMULATE_AGAIN (phi) = (val->lattice_val == VARYING);
431 sbitmap_free (is_may_def);
433 /* Compute immediate uses for variables we care about. */
434 compute_immediate_uses (TDFA_USE_OPS | TDFA_USE_VOPS, need_imm_uses_for);
438 /* Replace USE references in statement STMT with their immediate reaching
439 definition. Return true if at least one reference was replaced. If
440 REPLACED_ADDRESSES_P is given, it will be set to true if an address
441 constant was replaced. */
444 replace_uses_in (tree stmt, bool *replaced_addresses_p)
446 bool replaced = false;
450 if (replaced_addresses_p)
451 *replaced_addresses_p = false;
453 get_stmt_operands (stmt);
455 FOR_EACH_SSA_USE_OPERAND (use, stmt, iter, SSA_OP_USE)
457 tree tuse = USE_FROM_PTR (use);
458 value *val = get_value (tuse);
460 if (val->lattice_val != CONSTANT)
463 if (TREE_CODE (stmt) == ASM_EXPR
464 && !may_propagate_copy_into_asm (tuse))
467 SET_USE (use, val->const_val);
470 if (POINTER_TYPE_P (TREE_TYPE (tuse)) && replaced_addresses_p)
471 *replaced_addresses_p = true;
478 /* Replace the VUSE references in statement STMT with its immediate reaching
479 definition. Return true if the reference was replaced. If
480 REPLACED_ADDRESSES_P is given, it will be set to true if an address
481 constant was replaced. */
484 replace_vuse_in (tree stmt, bool *replaced_addresses_p)
486 bool replaced = false;
491 if (replaced_addresses_p)
492 *replaced_addresses_p = false;
494 get_stmt_operands (stmt);
496 vuses = STMT_VUSE_OPS (stmt);
498 if (NUM_VUSES (vuses) != 1)
501 vuse = VUSE_OP_PTR (vuses, 0);
502 val = get_value (USE_FROM_PTR (vuse));
504 if (val->lattice_val == CONSTANT
505 && TREE_CODE (stmt) == MODIFY_EXPR
506 && DECL_P (TREE_OPERAND (stmt, 1))
507 && TREE_OPERAND (stmt, 1) == SSA_NAME_VAR (USE_FROM_PTR (vuse)))
509 TREE_OPERAND (stmt, 1) = val->const_val;
511 if (POINTER_TYPE_P (TREE_TYPE (USE_FROM_PTR (vuse)))
512 && replaced_addresses_p)
513 *replaced_addresses_p = true;
520 /* Perform final substitution and folding. After this pass the program
521 should still be in SSA form. */
524 substitute_and_fold (void)
529 if (dump_file && (dump_flags & TDF_DETAILS))
531 "\nSubstituing constants and folding statements\n\n");
533 /* Substitute constants in every statement of every basic block. */
536 block_stmt_iterator i;
539 /* Propagate our known constants into PHI nodes. */
540 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
544 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
547 use_operand_p orig_p = PHI_ARG_DEF_PTR (phi, i);
548 tree orig = USE_FROM_PTR (orig_p);
550 if (! SSA_VAR_P (orig))
553 new_val = get_value (orig);
554 if (new_val->lattice_val == CONSTANT
555 && may_propagate_copy (orig, new_val->const_val))
556 SET_USE (orig_p, new_val->const_val);
560 for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
562 bool replaced_address;
563 tree stmt = bsi_stmt (i);
565 /* Skip statements that have been folded already. */
566 if (stmt_modified_p (stmt) || !is_exec_stmt (stmt))
569 /* Replace the statement with its folded version and mark it
571 if (dump_file && (dump_flags & TDF_DETAILS))
573 fprintf (dump_file, "Line %d: replaced ", get_lineno (stmt));
574 print_generic_stmt (dump_file, stmt, TDF_SLIM);
577 if (replace_uses_in (stmt, &replaced_address)
578 || replace_vuse_in (stmt, &replaced_address))
580 bool changed = fold_stmt (bsi_stmt_ptr (i));
582 /* If we folded a builtin function, we'll likely
583 need to rename VDEFs. */
584 if (replaced_address || changed)
586 mark_new_vars_to_rename (stmt, vars_to_rename);
587 if (maybe_clean_eh_stmt (stmt))
588 tree_purge_dead_eh_edges (bb);
594 if (dump_file && (dump_flags & TDF_DETAILS))
596 fprintf (dump_file, " with ");
597 print_generic_stmt (dump_file, stmt, TDF_SLIM);
598 fprintf (dump_file, "\n");
603 /* And transfer what we learned from VALUE_VECTOR into the
604 SSA_NAMEs themselves. This probably isn't terribly important
605 since we probably constant propagated the values to their
607 for (i = 0; i < num_ssa_names; i++)
609 tree name = ssa_name (i);
615 value = get_value (name);
616 if (value->lattice_val == CONSTANT
617 && is_gimple_reg (name)
618 && is_gimple_min_invariant (value->const_val))
619 SSA_NAME_VALUE (name) = value->const_val;
624 /* Free allocated storage. */
629 /* Perform substitutions based on the known constant values. */
630 substitute_and_fold ();
637 /* Compute the meet operator between VAL1 and VAL2:
639 any M UNDEFINED = any
640 any M VARYING = VARYING
641 any M UNKNOWN_VAL = UNKNOWN_VAL
642 Ci M Cj = Ci if (i == j)
643 Ci M Cj = VARYING if (i != j) */
645 ccp_lattice_meet (value val1, value val2)
649 /* any M UNDEFINED = any. */
650 if (val1.lattice_val == UNDEFINED)
652 else if (val2.lattice_val == UNDEFINED)
655 /* any M VARYING = VARYING. */
656 if (val1.lattice_val == VARYING || val2.lattice_val == VARYING)
658 result.lattice_val = VARYING;
659 result.const_val = NULL_TREE;
663 /* any M UNKNOWN_VAL = UNKNOWN_VAL. */
664 if (val1.lattice_val == UNKNOWN_VAL
665 || val2.lattice_val == UNKNOWN_VAL)
667 result.lattice_val = UNKNOWN_VAL;
668 result.const_val = NULL_TREE;
672 /* Ci M Cj = Ci if (i == j)
673 Ci M Cj = VARYING if (i != j) */
674 if (simple_cst_equal (val1.const_val, val2.const_val) == 1)
676 result.lattice_val = CONSTANT;
677 result.const_val = val1.const_val;
681 result.lattice_val = VARYING;
682 result.const_val = NULL_TREE;
689 /* Loop through the PHI_NODE's parameters for BLOCK and compare their
690 lattice values to determine PHI_NODE's lattice value. The value of a
691 PHI node is determined calling ccp_lattice_meet() with all the arguments
692 of the PHI node that are incoming via executable edges. */
694 static enum ssa_prop_result
695 ccp_visit_phi_node (tree phi)
697 value new_val, *old_val;
700 if (dump_file && (dump_flags & TDF_DETAILS))
702 fprintf (dump_file, "\nVisiting PHI node: ");
703 print_generic_expr (dump_file, phi, dump_flags);
706 old_val = get_value (PHI_RESULT (phi));
707 switch (old_val->lattice_val)
710 return SSA_PROP_NOT_INTERESTING;
717 /* To avoid the default value of UNKNOWN_VAL overriding
718 that of its possible constant arguments, temporarily
719 set the PHI node's default lattice value to be
720 UNDEFINED. If the PHI node's old value was UNKNOWN_VAL and
721 the new value is UNDEFINED, then we prevent the invalid
722 transition by not calling set_lattice_value. */
723 new_val.lattice_val = UNDEFINED;
724 new_val.const_val = NULL_TREE;
729 new_val.lattice_val = UNDEFINED;
730 new_val.const_val = NULL_TREE;
737 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
739 /* Compute the meet operator over all the PHI arguments. */
740 edge e = PHI_ARG_EDGE (phi, i);
742 if (dump_file && (dump_flags & TDF_DETAILS))
745 "\n Argument #%d (%d -> %d %sexecutable)\n",
746 i, e->src->index, e->dest->index,
747 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
750 /* If the incoming edge is executable, Compute the meet operator for
751 the existing value of the PHI node and the current PHI argument. */
752 if (e->flags & EDGE_EXECUTABLE)
754 tree rdef = PHI_ARG_DEF (phi, i);
755 value *rdef_val, val;
757 if (is_gimple_min_invariant (rdef))
759 val.lattice_val = CONSTANT;
760 val.const_val = rdef;
764 rdef_val = get_value (rdef);
766 new_val = ccp_lattice_meet (new_val, *rdef_val);
768 if (dump_file && (dump_flags & TDF_DETAILS))
770 fprintf (dump_file, "\t");
771 print_generic_expr (dump_file, rdef, dump_flags);
772 dump_lattice_value (dump_file, "\tValue: ", *rdef_val);
773 fprintf (dump_file, "\n");
776 if (new_val.lattice_val == VARYING)
781 if (dump_file && (dump_flags & TDF_DETAILS))
783 dump_lattice_value (dump_file, "\n PHI node value: ", new_val);
784 fprintf (dump_file, "\n\n");
787 /* Check for an invalid change from UNKNOWN_VAL to UNDEFINED. */
788 if (old_val->lattice_val == UNKNOWN_VAL
789 && new_val.lattice_val == UNDEFINED)
790 return SSA_PROP_NOT_INTERESTING;
792 /* Otherwise, make the transition to the new value. */
793 if (set_lattice_value (PHI_RESULT (phi), new_val))
795 if (new_val.lattice_val == VARYING)
796 return SSA_PROP_VARYING;
798 return SSA_PROP_INTERESTING;
801 return SSA_PROP_NOT_INTERESTING;
805 /* CCP specific front-end to the non-destructive constant folding
808 Attempt to simplify the RHS of STMT knowing that one or more
809 operands are constants.
811 If simplification is possible, return the simplified RHS,
812 otherwise return the original RHS. */
817 tree rhs = get_rhs (stmt);
818 enum tree_code code = TREE_CODE (rhs);
819 enum tree_code_class kind = TREE_CODE_CLASS (code);
820 tree retval = NULL_TREE;
823 vuses = STMT_VUSE_OPS (stmt);
825 /* If the RHS is just a variable, then that variable must now have
826 a constant value that we can return directly. */
827 if (TREE_CODE (rhs) == SSA_NAME)
828 return get_value (rhs)->const_val;
829 else if (DECL_P (rhs)
830 && NUM_VUSES (vuses) == 1
831 && rhs == SSA_NAME_VAR (VUSE_OP (vuses, 0)))
832 return get_value (VUSE_OP (vuses, 0))->const_val;
834 /* Unary operators. Note that we know the single operand must
835 be a constant. So this should almost always return a
837 if (kind == tcc_unary)
839 /* Handle unary operators which can appear in GIMPLE form. */
840 tree op0 = TREE_OPERAND (rhs, 0);
842 /* Simplify the operand down to a constant. */
843 if (TREE_CODE (op0) == SSA_NAME)
845 value *val = get_value (op0);
846 if (val->lattice_val == CONSTANT)
847 op0 = get_value (op0)->const_val;
850 retval = fold_unary_to_constant (code, TREE_TYPE (rhs), op0);
852 /* If we folded, but did not create an invariant, then we can not
853 use this expression. */
854 if (retval && ! is_gimple_min_invariant (retval))
857 /* If we could not fold the expression, but the arguments are all
858 constants and gimple values, then build and return the new
861 In some cases the new expression is still something we can
862 use as a replacement for an argument. This happens with
863 NOP conversions of types for example.
865 In other cases the new expression can not be used as a
866 replacement for an argument (as it would create non-gimple
867 code). But the new expression can still be used to derive
869 if (! retval && is_gimple_min_invariant (op0))
870 return build1 (code, TREE_TYPE (rhs), op0);
873 /* Binary and comparison operators. We know one or both of the
874 operands are constants. */
875 else if (kind == tcc_binary
876 || kind == tcc_comparison
877 || code == TRUTH_AND_EXPR
878 || code == TRUTH_OR_EXPR
879 || code == TRUTH_XOR_EXPR)
881 /* Handle binary and comparison operators that can appear in
883 tree op0 = TREE_OPERAND (rhs, 0);
884 tree op1 = TREE_OPERAND (rhs, 1);
886 /* Simplify the operands down to constants when appropriate. */
887 if (TREE_CODE (op0) == SSA_NAME)
889 value *val = get_value (op0);
890 if (val->lattice_val == CONSTANT)
891 op0 = val->const_val;
894 if (TREE_CODE (op1) == SSA_NAME)
896 value *val = get_value (op1);
897 if (val->lattice_val == CONSTANT)
898 op1 = val->const_val;
901 retval = fold_binary_to_constant (code, TREE_TYPE (rhs), op0, op1);
903 /* If we folded, but did not create an invariant, then we can not
904 use this expression. */
905 if (retval && ! is_gimple_min_invariant (retval))
908 /* If we could not fold the expression, but the arguments are all
909 constants and gimple values, then build and return the new
912 In some cases the new expression is still something we can
913 use as a replacement for an argument. This happens with
914 NOP conversions of types for example.
916 In other cases the new expression can not be used as a
917 replacement for an argument (as it would create non-gimple
918 code). But the new expression can still be used to derive
921 && is_gimple_min_invariant (op0)
922 && is_gimple_min_invariant (op1))
923 return build (code, TREE_TYPE (rhs), op0, op1);
926 /* We may be able to fold away calls to builtin functions if their
927 arguments are constants. */
928 else if (code == CALL_EXPR
929 && TREE_CODE (TREE_OPERAND (rhs, 0)) == ADDR_EXPR
930 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0))
932 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0)))
934 use_optype uses = STMT_USE_OPS (stmt);
935 if (NUM_USES (uses) != 0)
940 /* Preserve the original values of every operand. */
941 orig = xmalloc (sizeof (tree) * NUM_USES (uses));
942 for (i = 0; i < NUM_USES (uses); i++)
943 orig[i] = USE_OP (uses, i);
945 /* Substitute operands with their values and try to fold. */
946 replace_uses_in (stmt, NULL);
947 retval = fold_builtin (rhs, false);
949 /* Restore operands to their original form. */
950 for (i = 0; i < NUM_USES (uses); i++)
951 SET_USE_OP (uses, i, orig[i]);
958 /* If we got a simplified form, see if we need to convert its type. */
960 return fold_convert (TREE_TYPE (rhs), retval);
962 /* No simplification was possible. */
967 /* Evaluate statement STMT. */
970 evaluate_stmt (tree stmt)
974 latticevalue likelyvalue = likely_value (stmt);
976 /* If the statement is likely to have a CONSTANT result, then try
977 to fold the statement to determine the constant value. */
978 if (likelyvalue == CONSTANT)
979 simplified = ccp_fold (stmt);
980 /* If the statement is likely to have a VARYING result, then do not
981 bother folding the statement. */
982 else if (likelyvalue == VARYING)
983 simplified = get_rhs (stmt);
984 /* Otherwise the statement is likely to have an UNDEFINED value and
985 there will be nothing to do. */
987 simplified = NULL_TREE;
989 if (simplified && is_gimple_min_invariant (simplified))
991 /* The statement produced a constant value. */
992 val.lattice_val = CONSTANT;
993 val.const_val = simplified;
997 /* The statement produced a nonconstant value. If the statement
998 had undefined or virtual operands, then the result of the
999 statement should be undefined or virtual respectively.
1000 Else the result of the statement is VARYING. */
1001 val.lattice_val = (likelyvalue == UNDEFINED ? UNDEFINED : VARYING);
1002 val.lattice_val = (likelyvalue == UNKNOWN_VAL
1003 ? UNKNOWN_VAL : val.lattice_val);
1004 val.const_val = NULL_TREE;
1011 /* Visit the assignment statement STMT. Set the value of its LHS to the
1012 value computed by the RHS and store LHS in *OUTPUT_P. */
1014 static enum ssa_prop_result
1015 visit_assignment (tree stmt, tree *output_p)
1020 v_must_def_optype v_must_defs;
1022 lhs = TREE_OPERAND (stmt, 0);
1023 rhs = TREE_OPERAND (stmt, 1);
1024 vuses = STMT_VUSE_OPS (stmt);
1025 v_must_defs = STMT_V_MUST_DEF_OPS (stmt);
1027 gcc_assert (NUM_V_MAY_DEFS (STMT_V_MAY_DEF_OPS (stmt)) == 0);
1028 gcc_assert (NUM_V_MUST_DEFS (v_must_defs) == 1
1029 || TREE_CODE (lhs) == SSA_NAME);
1031 /* We require the SSA version number of the lhs for the value_vector.
1032 Make sure we have it. */
1033 if (TREE_CODE (lhs) != SSA_NAME)
1035 /* If we make it here, then stmt only has one definition:
1037 lhs = V_MUST_DEF_RESULT (v_must_defs, 0);
1040 if (TREE_CODE (rhs) == SSA_NAME)
1042 /* For a simple copy operation, we copy the lattice values. */
1043 value *nval = get_value (rhs);
1046 else if (DECL_P (rhs)
1047 && NUM_VUSES (vuses) == 1
1048 && rhs == SSA_NAME_VAR (VUSE_OP (vuses, 0)))
1050 /* Same as above, but the rhs is not a gimple register and yet
1051 has a known VUSE. */
1052 value *nval = get_value (VUSE_OP (vuses, 0));
1056 /* Evaluate the statement. */
1057 val = evaluate_stmt (stmt);
1059 /* If the original LHS was a VIEW_CONVERT_EXPR, modify the constant
1060 value to be a VIEW_CONVERT_EXPR of the old constant value. This is
1061 valid because a VIEW_CONVERT_EXPR is valid everywhere an operand of
1062 aggregate type is valid.
1064 ??? Also, if this was a definition of a bitfield, we need to widen
1065 the constant value into the type of the destination variable. This
1066 should not be necessary if GCC represented bitfields properly. */
1068 tree orig_lhs = TREE_OPERAND (stmt, 0);
1070 if (TREE_CODE (orig_lhs) == VIEW_CONVERT_EXPR
1071 && val.lattice_val == CONSTANT)
1073 val.const_val = build1 (VIEW_CONVERT_EXPR,
1074 TREE_TYPE (TREE_OPERAND (orig_lhs, 0)),
1076 orig_lhs = TREE_OPERAND (orig_lhs, 1);
1079 if (val.lattice_val == CONSTANT
1080 && TREE_CODE (orig_lhs) == COMPONENT_REF
1081 && DECL_BIT_FIELD (TREE_OPERAND (orig_lhs, 1)))
1083 tree w = widen_bitfield (val.const_val, TREE_OPERAND (orig_lhs, 1),
1086 if (w && is_gimple_min_invariant (w))
1090 val.lattice_val = VARYING;
1091 val.const_val = NULL;
1096 /* If LHS is not a gimple register, then it cannot take on an
1098 if (!is_gimple_reg (SSA_NAME_VAR (lhs))
1099 && val.lattice_val == UNDEFINED)
1100 val.lattice_val = UNKNOWN_VAL;
1102 /* Set the lattice value of the statement's output. */
1103 if (set_lattice_value (lhs, val))
1106 if (val.lattice_val == VARYING)
1107 return SSA_PROP_VARYING;
1109 return SSA_PROP_INTERESTING;
1112 return SSA_PROP_NOT_INTERESTING;
1116 /* Visit the conditional statement STMT. Return SSA_PROP_INTERESTING
1117 if it can determine which edge will be taken. Otherwise, return
1118 SSA_PROP_VARYING. */
1120 static enum ssa_prop_result
1121 visit_cond_stmt (tree stmt, edge *taken_edge_p)
1126 block = bb_for_stmt (stmt);
1127 val = evaluate_stmt (stmt);
1129 /* Find which edge out of the conditional block will be taken and add it
1130 to the worklist. If no single edge can be determined statically,
1131 return SSA_PROP_VARYING to feed all the outgoing edges to the
1132 propagation engine. */
1133 *taken_edge_p = val.const_val ? find_taken_edge (block, val.const_val) : 0;
1135 return SSA_PROP_INTERESTING;
1137 return SSA_PROP_VARYING;
1141 /* Evaluate statement STMT. If the statement produces an output value and
1142 its evaluation changes the lattice value of its output, return
1143 SSA_PROP_INTERESTING and set *OUTPUT_P to the SSA_NAME holding the
1146 If STMT is a conditional branch and we can determine its truth
1147 value, set *TAKEN_EDGE_P accordingly. If STMT produces a varying
1148 value, return SSA_PROP_VARYING. */
1150 static enum ssa_prop_result
1151 ccp_visit_stmt (tree stmt, edge *taken_edge_p, tree *output_p)
1154 v_may_def_optype v_may_defs;
1155 v_must_def_optype v_must_defs;
1159 if (dump_file && (dump_flags & TDF_DETAILS))
1161 fprintf (dump_file, "\nVisiting statement: ");
1162 print_generic_stmt (dump_file, stmt, TDF_SLIM);
1163 fprintf (dump_file, "\n");
1166 ann = stmt_ann (stmt);
1168 v_must_defs = V_MUST_DEF_OPS (ann);
1169 v_may_defs = V_MAY_DEF_OPS (ann);
1170 if (TREE_CODE (stmt) == MODIFY_EXPR
1171 && NUM_V_MAY_DEFS (v_may_defs) == 0
1172 && (NUM_V_MUST_DEFS (v_must_defs) == 1
1173 || TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME))
1175 /* If the statement is an assignment that produces a single
1176 output value, evaluate its RHS to see if the lattice value of
1177 its output has changed. */
1178 return visit_assignment (stmt, output_p);
1180 else if (TREE_CODE (stmt) == COND_EXPR || TREE_CODE (stmt) == SWITCH_EXPR)
1182 /* If STMT is a conditional branch, see if we can determine
1183 which branch will be taken. */
1184 return visit_cond_stmt (stmt, taken_edge_p);
1187 /* Any other kind of statement is not interesting for constant
1188 propagation and, therefore, not worth simulating. */
1189 if (dump_file && (dump_flags & TDF_DETAILS))
1190 fprintf (dump_file, "No interesting values produced. Marked VARYING.\n");
1192 /* Definitions made by statements other than assignments to
1193 SSA_NAMEs represent unknown modifications to their outputs.
1194 Mark them VARYING. */
1195 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
1196 def_to_varying (def);
1198 /* Mark all V_MAY_DEF operands VARYING. */
1199 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_VMAYDEF)
1200 def_to_varying (def);
1202 return SSA_PROP_VARYING;
1206 /* Main entry point for SSA Conditional Constant Propagation.
1208 [ DESCRIBE MAIN ALGORITHM HERE ] */
1211 execute_ssa_ccp (void)
1214 ssa_propagate (ccp_visit_stmt, ccp_visit_phi_node);
1222 return flag_tree_ccp != 0;
1226 struct tree_opt_pass pass_ccp =
1229 gate_ccp, /* gate */
1230 execute_ssa_ccp, /* execute */
1233 0, /* static_pass_number */
1234 TV_TREE_CCP, /* tv_id */
1235 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
1236 0, /* properties_provided */
1237 0, /* properties_destroyed */
1238 0, /* todo_flags_start */
1239 TODO_cleanup_cfg | TODO_dump_func | TODO_rename_vars
1240 | TODO_ggc_collect | TODO_verify_ssa
1241 | TODO_verify_stmts, /* todo_flags_finish */
1246 /* Given a constant value VAL for bitfield FIELD, and a destination
1247 variable VAR, return VAL appropriately widened to fit into VAR. If
1248 FIELD is wider than HOST_WIDE_INT, NULL is returned. */
1251 widen_bitfield (tree val, tree field, tree var)
1253 unsigned HOST_WIDE_INT var_size, field_size;
1255 unsigned HOST_WIDE_INT mask;
1258 /* We can only do this if the size of the type and field and VAL are
1259 all constants representable in HOST_WIDE_INT. */
1260 if (!host_integerp (TYPE_SIZE (TREE_TYPE (var)), 1)
1261 || !host_integerp (DECL_SIZE (field), 1)
1262 || !host_integerp (val, 0))
1265 var_size = tree_low_cst (TYPE_SIZE (TREE_TYPE (var)), 1);
1266 field_size = tree_low_cst (DECL_SIZE (field), 1);
1268 /* Give up if either the bitfield or the variable are too wide. */
1269 if (field_size > HOST_BITS_PER_WIDE_INT || var_size > HOST_BITS_PER_WIDE_INT)
1272 gcc_assert (var_size >= field_size);
1274 /* If the sign bit of the value is not set or the field's type is unsigned,
1275 just mask off the high order bits of the value. */
1276 if (DECL_UNSIGNED (field)
1277 || !(tree_low_cst (val, 0) & (((HOST_WIDE_INT)1) << (field_size - 1))))
1279 /* Zero extension. Build a mask with the lower 'field_size' bits
1280 set and a BIT_AND_EXPR node to clear the high order bits of
1282 for (i = 0, mask = 0; i < field_size; i++)
1283 mask |= ((HOST_WIDE_INT) 1) << i;
1285 wide_val = build (BIT_AND_EXPR, TREE_TYPE (var), val,
1286 fold_convert (TREE_TYPE (var),
1287 build_int_cst (NULL_TREE, mask)));
1291 /* Sign extension. Create a mask with the upper 'field_size'
1292 bits set and a BIT_IOR_EXPR to set the high order bits of the
1294 for (i = 0, mask = 0; i < (var_size - field_size); i++)
1295 mask |= ((HOST_WIDE_INT) 1) << (var_size - i - 1);
1297 wide_val = build (BIT_IOR_EXPR, TREE_TYPE (var), val,
1298 fold_convert (TREE_TYPE (var),
1299 build_int_cst (NULL_TREE, mask)));
1302 return fold (wide_val);
1306 /* A subroutine of fold_stmt_r. Attempts to fold *(A+O) to A[X].
1307 BASE is an array type. OFFSET is a byte displacement. ORIG_TYPE
1308 is the desired result type. */
1311 maybe_fold_offset_to_array_ref (tree base, tree offset, tree orig_type)
1313 tree min_idx, idx, elt_offset = integer_zero_node;
1314 tree array_type, elt_type, elt_size;
1316 /* If BASE is an ARRAY_REF, we can pick up another offset (this time
1317 measured in units of the size of elements type) from that ARRAY_REF).
1318 We can't do anything if either is variable.
1320 The case we handle here is *(&A[N]+O). */
1321 if (TREE_CODE (base) == ARRAY_REF)
1323 tree low_bound = array_ref_low_bound (base);
1325 elt_offset = TREE_OPERAND (base, 1);
1326 if (TREE_CODE (low_bound) != INTEGER_CST
1327 || TREE_CODE (elt_offset) != INTEGER_CST)
1330 elt_offset = int_const_binop (MINUS_EXPR, elt_offset, low_bound, 0);
1331 base = TREE_OPERAND (base, 0);
1334 /* Ignore stupid user tricks of indexing non-array variables. */
1335 array_type = TREE_TYPE (base);
1336 if (TREE_CODE (array_type) != ARRAY_TYPE)
1338 elt_type = TREE_TYPE (array_type);
1339 if (!lang_hooks.types_compatible_p (orig_type, elt_type))
1342 /* If OFFSET and ELT_OFFSET are zero, we don't care about the size of the
1343 element type (so we can use the alignment if it's not constant).
1344 Otherwise, compute the offset as an index by using a division. If the
1345 division isn't exact, then don't do anything. */
1346 elt_size = TYPE_SIZE_UNIT (elt_type);
1347 if (integer_zerop (offset))
1349 if (TREE_CODE (elt_size) != INTEGER_CST)
1350 elt_size = size_int (TYPE_ALIGN (elt_type));
1352 idx = integer_zero_node;
1356 unsigned HOST_WIDE_INT lquo, lrem;
1357 HOST_WIDE_INT hquo, hrem;
1359 if (TREE_CODE (elt_size) != INTEGER_CST
1360 || div_and_round_double (TRUNC_DIV_EXPR, 1,
1361 TREE_INT_CST_LOW (offset),
1362 TREE_INT_CST_HIGH (offset),
1363 TREE_INT_CST_LOW (elt_size),
1364 TREE_INT_CST_HIGH (elt_size),
1365 &lquo, &hquo, &lrem, &hrem)
1369 idx = build_int_cst_wide (NULL_TREE, lquo, hquo);
1372 /* Assume the low bound is zero. If there is a domain type, get the
1373 low bound, if any, convert the index into that type, and add the
1375 min_idx = integer_zero_node;
1376 if (TYPE_DOMAIN (array_type))
1378 if (TYPE_MIN_VALUE (TYPE_DOMAIN (array_type)))
1379 min_idx = TYPE_MIN_VALUE (TYPE_DOMAIN (array_type));
1381 min_idx = fold_convert (TYPE_DOMAIN (array_type), min_idx);
1383 if (TREE_CODE (min_idx) != INTEGER_CST)
1386 idx = fold_convert (TYPE_DOMAIN (array_type), idx);
1387 elt_offset = fold_convert (TYPE_DOMAIN (array_type), elt_offset);
1390 if (!integer_zerop (min_idx))
1391 idx = int_const_binop (PLUS_EXPR, idx, min_idx, 0);
1392 if (!integer_zerop (elt_offset))
1393 idx = int_const_binop (PLUS_EXPR, idx, elt_offset, 0);
1395 return build (ARRAY_REF, orig_type, base, idx, min_idx,
1396 size_int (tree_low_cst (elt_size, 1)
1397 / (TYPE_ALIGN_UNIT (elt_type))));
1401 /* A subroutine of fold_stmt_r. Attempts to fold *(S+O) to S.X.
1402 BASE is a record type. OFFSET is a byte displacement. ORIG_TYPE
1403 is the desired result type. */
1404 /* ??? This doesn't handle class inheritance. */
1407 maybe_fold_offset_to_component_ref (tree record_type, tree base, tree offset,
1408 tree orig_type, bool base_is_ptr)
1410 tree f, t, field_type, tail_array_field, field_offset;
1412 if (TREE_CODE (record_type) != RECORD_TYPE
1413 && TREE_CODE (record_type) != UNION_TYPE
1414 && TREE_CODE (record_type) != QUAL_UNION_TYPE)
1417 /* Short-circuit silly cases. */
1418 if (lang_hooks.types_compatible_p (record_type, orig_type))
1421 tail_array_field = NULL_TREE;
1422 for (f = TYPE_FIELDS (record_type); f ; f = TREE_CHAIN (f))
1426 if (TREE_CODE (f) != FIELD_DECL)
1428 if (DECL_BIT_FIELD (f))
1431 field_offset = byte_position (f);
1432 if (TREE_CODE (field_offset) != INTEGER_CST)
1435 /* ??? Java creates "interesting" fields for representing base classes.
1436 They have no name, and have no context. With no context, we get into
1437 trouble with nonoverlapping_component_refs_p. Skip them. */
1438 if (!DECL_FIELD_CONTEXT (f))
1441 /* The previous array field isn't at the end. */
1442 tail_array_field = NULL_TREE;
1444 /* Check to see if this offset overlaps with the field. */
1445 cmp = tree_int_cst_compare (field_offset, offset);
1449 field_type = TREE_TYPE (f);
1452 /* Don't care about offsets into the middle of scalars. */
1453 if (!AGGREGATE_TYPE_P (field_type))
1456 /* Check for array at the end of the struct. This is often
1457 used as for flexible array members. We should be able to
1458 turn this into an array access anyway. */
1459 if (TREE_CODE (field_type) == ARRAY_TYPE)
1460 tail_array_field = f;
1462 /* Check the end of the field against the offset. */
1463 if (!DECL_SIZE_UNIT (f)
1464 || TREE_CODE (DECL_SIZE_UNIT (f)) != INTEGER_CST)
1466 t = int_const_binop (MINUS_EXPR, offset, DECL_FIELD_OFFSET (f), 1);
1467 if (!tree_int_cst_lt (t, DECL_SIZE_UNIT (f)))
1470 /* If we matched, then set offset to the displacement into
1475 /* Here we exactly match the offset being checked. If the types match,
1476 then we can return that field. */
1477 else if (lang_hooks.types_compatible_p (orig_type, field_type))
1480 base = build1 (INDIRECT_REF, record_type, base);
1481 t = build (COMPONENT_REF, field_type, base, f, NULL_TREE);
1485 /* Don't care about type-punning of scalars. */
1486 else if (!AGGREGATE_TYPE_P (field_type))
1492 if (!tail_array_field)
1495 f = tail_array_field;
1496 field_type = TREE_TYPE (f);
1499 /* If we get here, we've got an aggregate field, and a possibly
1500 nonzero offset into them. Recurse and hope for a valid match. */
1502 base = build1 (INDIRECT_REF, record_type, base);
1503 base = build (COMPONENT_REF, field_type, base, f, NULL_TREE);
1505 t = maybe_fold_offset_to_array_ref (base, offset, orig_type);
1508 return maybe_fold_offset_to_component_ref (field_type, base, offset,
1513 /* A subroutine of fold_stmt_r. Attempt to simplify *(BASE+OFFSET).
1514 Return the simplified expression, or NULL if nothing could be done. */
1517 maybe_fold_stmt_indirect (tree expr, tree base, tree offset)
1521 /* We may well have constructed a double-nested PLUS_EXPR via multiple
1522 substitutions. Fold that down to one. Remove NON_LVALUE_EXPRs that
1523 are sometimes added. */
1526 TREE_OPERAND (expr, 0) = base;
1528 /* One possibility is that the address reduces to a string constant. */
1529 t = fold_read_from_constant_string (expr);
1533 /* Add in any offset from a PLUS_EXPR. */
1534 if (TREE_CODE (base) == PLUS_EXPR)
1538 offset2 = TREE_OPERAND (base, 1);
1539 if (TREE_CODE (offset2) != INTEGER_CST)
1541 base = TREE_OPERAND (base, 0);
1543 offset = int_const_binop (PLUS_EXPR, offset, offset2, 1);
1546 if (TREE_CODE (base) == ADDR_EXPR)
1548 /* Strip the ADDR_EXPR. */
1549 base = TREE_OPERAND (base, 0);
1551 /* Fold away CONST_DECL to its value, if the type is scalar. */
1552 if (TREE_CODE (base) == CONST_DECL
1553 && is_gimple_min_invariant (DECL_INITIAL (base)))
1554 return DECL_INITIAL (base);
1556 /* Try folding *(&B+O) to B[X]. */
1557 t = maybe_fold_offset_to_array_ref (base, offset, TREE_TYPE (expr));
1561 /* Try folding *(&B+O) to B.X. */
1562 t = maybe_fold_offset_to_component_ref (TREE_TYPE (base), base, offset,
1563 TREE_TYPE (expr), false);
1567 /* Fold *&B to B. We can only do this if EXPR is the same type
1568 as BASE. We can't do this if EXPR is the element type of an array
1569 and BASE is the array. */
1570 if (integer_zerop (offset)
1571 && lang_hooks.types_compatible_p (TREE_TYPE (base),
1577 /* We can get here for out-of-range string constant accesses,
1578 such as "_"[3]. Bail out of the entire substitution search
1579 and arrange for the entire statement to be replaced by a
1580 call to __builtin_trap. In all likelyhood this will all be
1581 constant-folded away, but in the meantime we can't leave with
1582 something that get_expr_operands can't understand. */
1586 if (TREE_CODE (t) == ADDR_EXPR
1587 && TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST)
1589 /* FIXME: Except that this causes problems elsewhere with dead
1590 code not being deleted, and we abort in the rtl expanders
1591 because we failed to remove some ssa_name. In the meantime,
1592 just return zero. */
1593 /* FIXME2: This condition should be signaled by
1594 fold_read_from_constant_string directly, rather than
1595 re-checking for it here. */
1596 return integer_zero_node;
1599 /* Try folding *(B+O) to B->X. Still an improvement. */
1600 if (POINTER_TYPE_P (TREE_TYPE (base)))
1602 t = maybe_fold_offset_to_component_ref (TREE_TYPE (TREE_TYPE (base)),
1604 TREE_TYPE (expr), true);
1610 /* Otherwise we had an offset that we could not simplify. */
1615 /* A subroutine of fold_stmt_r. EXPR is a PLUS_EXPR.
1617 A quaint feature extant in our address arithmetic is that there
1618 can be hidden type changes here. The type of the result need
1619 not be the same as the type of the input pointer.
1621 What we're after here is an expression of the form
1622 (T *)(&array + const)
1623 where the cast doesn't actually exist, but is implicit in the
1624 type of the PLUS_EXPR. We'd like to turn this into
1626 which may be able to propagate further. */
1629 maybe_fold_stmt_addition (tree expr)
1631 tree op0 = TREE_OPERAND (expr, 0);
1632 tree op1 = TREE_OPERAND (expr, 1);
1633 tree ptr_type = TREE_TYPE (expr);
1636 bool subtract = (TREE_CODE (expr) == MINUS_EXPR);
1638 /* We're only interested in pointer arithmetic. */
1639 if (!POINTER_TYPE_P (ptr_type))
1641 /* Canonicalize the integral operand to op1. */
1642 if (INTEGRAL_TYPE_P (TREE_TYPE (op0)))
1646 t = op0, op0 = op1, op1 = t;
1648 /* It had better be a constant. */
1649 if (TREE_CODE (op1) != INTEGER_CST)
1651 /* The first operand should be an ADDR_EXPR. */
1652 if (TREE_CODE (op0) != ADDR_EXPR)
1654 op0 = TREE_OPERAND (op0, 0);
1656 /* If the first operand is an ARRAY_REF, expand it so that we can fold
1657 the offset into it. */
1658 while (TREE_CODE (op0) == ARRAY_REF)
1660 tree array_obj = TREE_OPERAND (op0, 0);
1661 tree array_idx = TREE_OPERAND (op0, 1);
1662 tree elt_type = TREE_TYPE (op0);
1663 tree elt_size = TYPE_SIZE_UNIT (elt_type);
1666 if (TREE_CODE (array_idx) != INTEGER_CST)
1668 if (TREE_CODE (elt_size) != INTEGER_CST)
1671 /* Un-bias the index by the min index of the array type. */
1672 min_idx = TYPE_DOMAIN (TREE_TYPE (array_obj));
1675 min_idx = TYPE_MIN_VALUE (min_idx);
1678 if (TREE_CODE (min_idx) != INTEGER_CST)
1681 array_idx = convert (TREE_TYPE (min_idx), array_idx);
1682 if (!integer_zerop (min_idx))
1683 array_idx = int_const_binop (MINUS_EXPR, array_idx,
1688 /* Convert the index to a byte offset. */
1689 array_idx = convert (sizetype, array_idx);
1690 array_idx = int_const_binop (MULT_EXPR, array_idx, elt_size, 0);
1692 /* Update the operands for the next round, or for folding. */
1693 /* If we're manipulating unsigned types, then folding into negative
1694 values can produce incorrect results. Particularly if the type
1695 is smaller than the width of the pointer. */
1697 && TYPE_UNSIGNED (TREE_TYPE (op1))
1698 && tree_int_cst_lt (array_idx, op1))
1700 op1 = int_const_binop (subtract ? MINUS_EXPR : PLUS_EXPR,
1706 /* If we weren't able to fold the subtraction into another array reference,
1707 canonicalize the integer for passing to the array and component ref
1708 simplification functions. */
1711 if (TYPE_UNSIGNED (TREE_TYPE (op1)))
1713 op1 = fold (build1 (NEGATE_EXPR, TREE_TYPE (op1), op1));
1714 /* ??? In theory fold should always produce another integer. */
1715 if (TREE_CODE (op1) != INTEGER_CST)
1719 ptd_type = TREE_TYPE (ptr_type);
1721 /* At which point we can try some of the same things as for indirects. */
1722 t = maybe_fold_offset_to_array_ref (op0, op1, ptd_type);
1724 t = maybe_fold_offset_to_component_ref (TREE_TYPE (op0), op0, op1,
1727 t = build1 (ADDR_EXPR, ptr_type, t);
1733 /* Subroutine of fold_stmt called via walk_tree. We perform several
1734 simplifications of EXPR_P, mostly having to do with pointer arithmetic. */
1737 fold_stmt_r (tree *expr_p, int *walk_subtrees, void *data)
1739 bool *changed_p = data;
1740 tree expr = *expr_p, t;
1742 /* ??? It'd be nice if walk_tree had a pre-order option. */
1743 switch (TREE_CODE (expr))
1746 t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL);
1751 t = maybe_fold_stmt_indirect (expr, TREE_OPERAND (expr, 0),
1755 /* ??? Could handle ARRAY_REF here, as a variant of INDIRECT_REF.
1756 We'd only want to bother decomposing an existing ARRAY_REF if
1757 the base array is found to have another offset contained within.
1758 Otherwise we'd be wasting time. */
1761 t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL);
1766 /* Set TREE_INVARIANT properly so that the value is properly
1767 considered constant, and so gets propagated as expected. */
1769 recompute_tree_invarant_for_addr_expr (expr);
1774 t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL);
1777 t = walk_tree (&TREE_OPERAND (expr, 1), fold_stmt_r, data, NULL);
1782 t = maybe_fold_stmt_addition (expr);
1786 t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL);
1791 /* Make sure the FIELD_DECL is actually a field in the type on the lhs.
1792 We've already checked that the records are compatible, so we should
1793 come up with a set of compatible fields. */
1795 tree expr_record = TREE_TYPE (TREE_OPERAND (expr, 0));
1796 tree expr_field = TREE_OPERAND (expr, 1);
1798 if (DECL_FIELD_CONTEXT (expr_field) != TYPE_MAIN_VARIANT (expr_record))
1800 expr_field = find_compatible_field (expr_record, expr_field);
1801 TREE_OPERAND (expr, 1) = expr_field;
1820 /* Return the string length of ARG in LENGTH. If ARG is an SSA name variable,
1821 follow its use-def chains. If LENGTH is not NULL and its value is not
1822 equal to the length we determine, or if we are unable to determine the
1823 length, return false. VISITED is a bitmap of visited variables. */
1826 get_strlen (tree arg, tree *length, bitmap visited)
1828 tree var, def_stmt, val;
1830 if (TREE_CODE (arg) != SSA_NAME)
1832 val = c_strlen (arg, 1);
1836 if (*length && simple_cst_equal (val, *length) != 1)
1843 /* If we were already here, break the infinite cycle. */
1844 if (bitmap_bit_p (visited, SSA_NAME_VERSION (arg)))
1846 bitmap_set_bit (visited, SSA_NAME_VERSION (arg));
1849 def_stmt = SSA_NAME_DEF_STMT (var);
1851 switch (TREE_CODE (def_stmt))
1857 /* The RHS of the statement defining VAR must either have a
1858 constant length or come from another SSA_NAME with a constant
1860 rhs = TREE_OPERAND (def_stmt, 1);
1862 if (TREE_CODE (rhs) == SSA_NAME)
1863 return get_strlen (rhs, length, visited);
1865 /* See if the RHS is a constant length. */
1866 len = c_strlen (rhs, 1);
1869 if (*length && simple_cst_equal (len, *length) != 1)
1881 /* All the arguments of the PHI node must have the same constant
1885 for (i = 0; i < PHI_NUM_ARGS (def_stmt); i++)
1887 tree arg = PHI_ARG_DEF (def_stmt, i);
1889 /* If this PHI has itself as an argument, we cannot
1890 determine the string length of this argument. However,
1891 if we can find a constant string length for the other
1892 PHI args then we can still be sure that this is a
1893 constant string length. So be optimistic and just
1894 continue with the next argument. */
1895 if (arg == PHI_RESULT (def_stmt))
1898 if (!get_strlen (arg, length, visited))
1914 /* Fold builtin call FN in statement STMT. If it cannot be folded into a
1915 constant, return NULL_TREE. Otherwise, return its constant value. */
1918 ccp_fold_builtin (tree stmt, tree fn)
1920 tree result, strlen_val[2];
1921 tree callee, arglist, a;
1926 ignore = TREE_CODE (stmt) != MODIFY_EXPR;
1928 /* First try the generic builtin folder. If that succeeds, return the
1930 result = fold_builtin (fn, ignore);
1934 STRIP_NOPS (result);
1938 /* Ignore MD builtins. */
1939 callee = get_callee_fndecl (fn);
1940 if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_MD)
1943 /* If the builtin could not be folded, and it has no argument list,
1945 arglist = TREE_OPERAND (fn, 1);
1949 /* Limit the work only for builtins we know how to simplify. */
1950 switch (DECL_FUNCTION_CODE (callee))
1952 case BUILT_IN_STRLEN:
1953 case BUILT_IN_FPUTS:
1954 case BUILT_IN_FPUTS_UNLOCKED:
1957 case BUILT_IN_STRCPY:
1958 case BUILT_IN_STRNCPY:
1965 /* Try to use the dataflow information gathered by the CCP process. */
1966 visited = BITMAP_XMALLOC ();
1968 memset (strlen_val, 0, sizeof (strlen_val));
1969 for (i = 0, a = arglist;
1971 i++, strlen_arg >>= 1, a = TREE_CHAIN (a))
1974 bitmap_clear (visited);
1975 if (!get_strlen (TREE_VALUE (a), &strlen_val[i], visited))
1976 strlen_val[i] = NULL_TREE;
1979 BITMAP_XFREE (visited);
1982 switch (DECL_FUNCTION_CODE (callee))
1984 case BUILT_IN_STRLEN:
1987 tree new = fold_convert (TREE_TYPE (fn), strlen_val[0]);
1989 /* If the result is not a valid gimple value, or not a cast
1990 of a valid gimple value, then we can not use the result. */
1991 if (is_gimple_val (new)
1992 || (is_gimple_cast (new)
1993 && is_gimple_val (TREE_OPERAND (new, 0))))
1998 case BUILT_IN_STRCPY:
1999 if (strlen_val[1] && is_gimple_val (strlen_val[1]))
2000 result = fold_builtin_strcpy (fn, strlen_val[1]);
2003 case BUILT_IN_STRNCPY:
2004 if (strlen_val[1] && is_gimple_val (strlen_val[1]))
2005 result = fold_builtin_strncpy (fn, strlen_val[1]);
2008 case BUILT_IN_FPUTS:
2009 result = fold_builtin_fputs (arglist,
2010 TREE_CODE (stmt) != MODIFY_EXPR, 0,
2014 case BUILT_IN_FPUTS_UNLOCKED:
2015 result = fold_builtin_fputs (arglist,
2016 TREE_CODE (stmt) != MODIFY_EXPR, 1,
2024 if (result && ignore)
2025 result = fold_ignored_result (result);
2030 /* Fold the statement pointed by STMT_P. In some cases, this function may
2031 replace the whole statement with a new one. Returns true iff folding
2032 makes any changes. */
2035 fold_stmt (tree *stmt_p)
2037 tree rhs, result, stmt;
2038 bool changed = false;
2042 /* If we replaced constants and the statement makes pointer dereferences,
2043 then we may need to fold instances of *&VAR into VAR, etc. */
2044 if (walk_tree (stmt_p, fold_stmt_r, &changed, NULL))
2047 = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP],
2052 rhs = get_rhs (stmt);
2057 if (TREE_CODE (rhs) == CALL_EXPR)
2061 /* Check for builtins that CCP can handle using information not
2062 available in the generic fold routines. */
2063 callee = get_callee_fndecl (rhs);
2064 if (callee && DECL_BUILT_IN (callee))
2065 result = ccp_fold_builtin (stmt, rhs);
2068 /* Check for resolvable OBJ_TYPE_REF. The only sorts we can resolve
2069 here are when we've propagated the address of a decl into the
2071 /* ??? Should perhaps do this in fold proper. However, doing it
2072 there requires that we create a new CALL_EXPR, and that requires
2073 copying EH region info to the new node. Easier to just do it
2074 here where we can just smash the call operand. */
2075 callee = TREE_OPERAND (rhs, 0);
2076 if (TREE_CODE (callee) == OBJ_TYPE_REF
2077 && lang_hooks.fold_obj_type_ref
2078 && TREE_CODE (OBJ_TYPE_REF_OBJECT (callee)) == ADDR_EXPR
2079 && DECL_P (TREE_OPERAND
2080 (OBJ_TYPE_REF_OBJECT (callee), 0)))
2084 /* ??? Caution: Broken ADDR_EXPR semantics means that
2085 looking at the type of the operand of the addr_expr
2086 can yield an array type. See silly exception in
2087 check_pointer_types_r. */
2089 t = TREE_TYPE (TREE_TYPE (OBJ_TYPE_REF_OBJECT (callee)));
2090 t = lang_hooks.fold_obj_type_ref (callee, t);
2093 TREE_OPERAND (rhs, 0) = t;
2100 /* If we couldn't fold the RHS, hand over to the generic fold routines. */
2101 if (result == NULL_TREE)
2102 result = fold (rhs);
2104 /* Strip away useless type conversions. Both the NON_LVALUE_EXPR that
2105 may have been added by fold, and "useless" type conversions that might
2106 now be apparent due to propagation. */
2107 STRIP_USELESS_TYPE_CONVERSION (result);
2110 changed |= set_rhs (stmt_p, result);
2116 /* Convert EXPR into a GIMPLE value suitable for substitution on the
2117 RHS of an assignment. Insert the necessary statements before
2121 convert_to_gimple_builtin (block_stmt_iterator *si_p, tree expr)
2123 tree_stmt_iterator ti;
2124 tree stmt = bsi_stmt (*si_p);
2125 tree tmp, stmts = NULL;
2127 push_gimplify_context ();
2128 tmp = get_initialized_tmp_var (expr, &stmts, NULL);
2129 pop_gimplify_context (NULL);
2131 /* The replacement can expose previously unreferenced variables. */
2132 for (ti = tsi_start (stmts); !tsi_end_p (ti); tsi_next (&ti))
2134 find_new_referenced_vars (tsi_stmt_ptr (ti));
2135 mark_new_vars_to_rename (tsi_stmt (ti), vars_to_rename);
2138 if (EXPR_HAS_LOCATION (stmt))
2139 annotate_all_with_locus (&stmts, EXPR_LOCATION (stmt));
2141 bsi_insert_before (si_p, stmts, BSI_SAME_STMT);
2147 /* A simple pass that attempts to fold all builtin functions. This pass
2148 is run after we've propagated as many constants as we can. */
2151 execute_fold_all_builtins (void)
2153 bool cfg_changed = false;
2157 block_stmt_iterator i;
2158 for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
2160 tree *stmtp = bsi_stmt_ptr (i);
2161 tree call = get_rhs (*stmtp);
2162 tree callee, result;
2164 if (!call || TREE_CODE (call) != CALL_EXPR)
2166 callee = get_callee_fndecl (call);
2167 if (!callee || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL)
2170 result = ccp_fold_builtin (*stmtp, call);
2172 switch (DECL_FUNCTION_CODE (callee))
2174 case BUILT_IN_CONSTANT_P:
2175 /* Resolve __builtin_constant_p. If it hasn't been
2176 folded to integer_one_node by now, it's fairly
2177 certain that the value simply isn't constant. */
2178 result = integer_zero_node;
2185 if (dump_file && (dump_flags & TDF_DETAILS))
2187 fprintf (dump_file, "Simplified\n ");
2188 print_generic_stmt (dump_file, *stmtp, dump_flags);
2191 if (!set_rhs (stmtp, result))
2193 result = convert_to_gimple_builtin (&i, result);
2194 if (result && !set_rhs (stmtp, result))
2197 modify_stmt (*stmtp);
2198 if (maybe_clean_eh_stmt (*stmtp)
2199 && tree_purge_dead_eh_edges (bb))
2202 if (dump_file && (dump_flags & TDF_DETAILS))
2204 fprintf (dump_file, "to\n ");
2205 print_generic_stmt (dump_file, *stmtp, dump_flags);
2206 fprintf (dump_file, "\n");
2211 /* Delete unreachable blocks. */
2213 cleanup_tree_cfg ();
2217 struct tree_opt_pass pass_fold_builtins =
2221 execute_fold_all_builtins, /* execute */
2224 0, /* static_pass_number */
2226 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
2227 0, /* properties_provided */
2228 0, /* properties_destroyed */
2229 0, /* todo_flags_start */
2232 | TODO_rename_vars, /* todo_flags_finish */