Reverting merge from trunk
[official-gcc.git] / gcc / tree-ssa-ccp.c
blob50006abdc052253ce8e30023bf9db32efdc5d756
1 /* Conditional constant propagation pass for the GNU compiler.
2 Copyright (C) 2000-2013 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 3, or (at your option) any
11 later version.
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
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Conditional constant propagation (CCP) is based on the SSA
23 propagation engine (tree-ssa-propagate.c). Constant assignments of
24 the form VAR = CST are propagated from the assignments into uses of
25 VAR, which in turn may generate new constants. The simulation uses
26 a four level lattice to keep track of constant values associated
27 with SSA names. Given an SSA name V_i, it may take one of the
28 following values:
30 UNINITIALIZED -> the initial state of the value. This value
31 is replaced with a correct initial value
32 the first time the value is used, so the
33 rest of the pass does not need to care about
34 it. Using this value simplifies initialization
35 of the pass, and prevents us from needlessly
36 scanning statements that are never reached.
38 UNDEFINED -> V_i is a local variable whose definition
39 has not been processed yet. Therefore we
40 don't yet know if its value is a constant
41 or not.
43 CONSTANT -> V_i has been found to hold a constant
44 value C.
46 VARYING -> V_i cannot take a constant value, or if it
47 does, it is not possible to determine it
48 at compile time.
50 The core of SSA-CCP is in ccp_visit_stmt and ccp_visit_phi_node:
52 1- In ccp_visit_stmt, we are interested in assignments whose RHS
53 evaluates into a constant and conditional jumps whose predicate
54 evaluates into a boolean true or false. When an assignment of
55 the form V_i = CONST is found, V_i's lattice value is set to
56 CONSTANT and CONST is associated with it. This causes the
57 propagation engine to add all the SSA edges coming out the
58 assignment into the worklists, so that statements that use V_i
59 can be visited.
61 If the statement is a conditional with a constant predicate, we
62 mark the outgoing edges as executable or not executable
63 depending on the predicate's value. This is then used when
64 visiting PHI nodes to know when a PHI argument can be ignored.
67 2- In ccp_visit_phi_node, if all the PHI arguments evaluate to the
68 same constant C, then the LHS of the PHI is set to C. This
69 evaluation is known as the "meet operation". Since one of the
70 goals of this evaluation is to optimistically return constant
71 values as often as possible, it uses two main short cuts:
73 - If an argument is flowing in through a non-executable edge, it
74 is ignored. This is useful in cases like this:
76 if (PRED)
77 a_9 = 3;
78 else
79 a_10 = 100;
80 a_11 = PHI (a_9, a_10)
82 If PRED is known to always evaluate to false, then we can
83 assume that a_11 will always take its value from a_10, meaning
84 that instead of consider it VARYING (a_9 and a_10 have
85 different values), we can consider it CONSTANT 100.
87 - If an argument has an UNDEFINED value, then it does not affect
88 the outcome of the meet operation. If a variable V_i has an
89 UNDEFINED value, it means that either its defining statement
90 hasn't been visited yet or V_i has no defining statement, in
91 which case the original symbol 'V' is being used
92 uninitialized. Since 'V' is a local variable, the compiler
93 may assume any initial value for it.
96 After propagation, every variable V_i that ends up with a lattice
97 value of CONSTANT will have the associated constant value in the
98 array CONST_VAL[i].VALUE. That is fed into substitute_and_fold for
99 final substitution and folding.
101 References:
103 Constant propagation with conditional branches,
104 Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
106 Building an Optimizing Compiler,
107 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
109 Advanced Compiler Design and Implementation,
110 Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
112 #include "config.h"
113 #include "system.h"
114 #include "coretypes.h"
115 #include "tm.h"
116 #include "tree.h"
117 #include "flags.h"
118 #include "tm_p.h"
119 #include "basic-block.h"
120 #include "function.h"
121 #include "gimple-pretty-print.h"
122 #include "gimple.h"
123 #include "gimplify.h"
124 #include "gimple-iterator.h"
125 #include "gimple-ssa.h"
126 #include "tree-cfg.h"
127 #include "tree-phinodes.h"
128 #include "ssa-iterators.h"
129 #include "tree-ssanames.h"
130 #include "tree-pass.h"
131 #include "tree-ssa-propagate.h"
132 #include "value-prof.h"
133 #include "langhooks.h"
134 #include "target.h"
135 #include "diagnostic-core.h"
136 #include "dbgcnt.h"
137 #include "params.h"
138 #include "hash-table.h"
141 /* Possible lattice values. */
142 typedef enum
144 UNINITIALIZED,
145 UNDEFINED,
146 CONSTANT,
147 VARYING
148 } ccp_lattice_t;
150 struct prop_value_d {
151 /* Lattice value. */
152 ccp_lattice_t lattice_val;
154 /* Propagated value. */
155 tree value;
157 /* Mask that applies to the propagated value during CCP. For
158 X with a CONSTANT lattice value X & ~mask == value & ~mask. */
159 double_int mask;
162 typedef struct prop_value_d prop_value_t;
164 /* Array of propagated constant values. After propagation,
165 CONST_VAL[I].VALUE holds the constant value for SSA_NAME(I). If
166 the constant is held in an SSA name representing a memory store
167 (i.e., a VDEF), CONST_VAL[I].MEM_REF will contain the actual
168 memory reference used to store (i.e., the LHS of the assignment
169 doing the store). */
170 static prop_value_t *const_val;
171 static unsigned n_const_val;
173 static void canonicalize_value (prop_value_t *);
174 static bool ccp_fold_stmt (gimple_stmt_iterator *);
176 /* Dump constant propagation value VAL to file OUTF prefixed by PREFIX. */
178 static void
179 dump_lattice_value (FILE *outf, const char *prefix, prop_value_t val)
181 switch (val.lattice_val)
183 case UNINITIALIZED:
184 fprintf (outf, "%sUNINITIALIZED", prefix);
185 break;
186 case UNDEFINED:
187 fprintf (outf, "%sUNDEFINED", prefix);
188 break;
189 case VARYING:
190 fprintf (outf, "%sVARYING", prefix);
191 break;
192 case CONSTANT:
193 if (TREE_CODE (val.value) != INTEGER_CST
194 || val.mask.is_zero ())
196 fprintf (outf, "%sCONSTANT ", prefix);
197 print_generic_expr (outf, val.value, dump_flags);
199 else
201 double_int cval = tree_to_double_int (val.value).and_not (val.mask);
202 fprintf (outf, "%sCONSTANT " HOST_WIDE_INT_PRINT_DOUBLE_HEX,
203 prefix, cval.high, cval.low);
204 fprintf (outf, " (" HOST_WIDE_INT_PRINT_DOUBLE_HEX ")",
205 val.mask.high, val.mask.low);
207 break;
208 default:
209 gcc_unreachable ();
214 /* Print lattice value VAL to stderr. */
216 void debug_lattice_value (prop_value_t val);
218 DEBUG_FUNCTION void
219 debug_lattice_value (prop_value_t val)
221 dump_lattice_value (stderr, "", val);
222 fprintf (stderr, "\n");
226 /* Compute a default value for variable VAR and store it in the
227 CONST_VAL array. The following rules are used to get default
228 values:
230 1- Global and static variables that are declared constant are
231 considered CONSTANT.
233 2- Any other value is considered UNDEFINED. This is useful when
234 considering PHI nodes. PHI arguments that are undefined do not
235 change the constant value of the PHI node, which allows for more
236 constants to be propagated.
238 3- Variables defined by statements other than assignments and PHI
239 nodes are considered VARYING.
241 4- Initial values of variables that are not GIMPLE registers are
242 considered VARYING. */
244 static prop_value_t
245 get_default_value (tree var)
247 prop_value_t val = { UNINITIALIZED, NULL_TREE, { 0, 0 } };
248 gimple stmt;
250 stmt = SSA_NAME_DEF_STMT (var);
252 if (gimple_nop_p (stmt))
254 /* Variables defined by an empty statement are those used
255 before being initialized. If VAR is a local variable, we
256 can assume initially that it is UNDEFINED, otherwise we must
257 consider it VARYING. */
258 if (!virtual_operand_p (var)
259 && TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
260 val.lattice_val = UNDEFINED;
261 else
263 val.lattice_val = VARYING;
264 val.mask = double_int_minus_one;
265 if (flag_tree_bit_ccp)
267 double_int nonzero_bits = get_nonzero_bits (var);
268 double_int mask
269 = double_int::mask (TYPE_PRECISION (TREE_TYPE (var)));
270 if (nonzero_bits != double_int_minus_one && nonzero_bits != mask)
272 val.lattice_val = CONSTANT;
273 val.value = build_zero_cst (TREE_TYPE (var));
274 /* CCP wants the bits above precision set. */
275 val.mask = nonzero_bits | ~mask;
280 else if (is_gimple_assign (stmt))
282 tree cst;
283 if (gimple_assign_single_p (stmt)
284 && DECL_P (gimple_assign_rhs1 (stmt))
285 && (cst = get_symbol_constant_value (gimple_assign_rhs1 (stmt))))
287 val.lattice_val = CONSTANT;
288 val.value = cst;
290 else
292 /* Any other variable defined by an assignment is considered
293 UNDEFINED. */
294 val.lattice_val = UNDEFINED;
297 else if ((is_gimple_call (stmt)
298 && gimple_call_lhs (stmt) != NULL_TREE)
299 || gimple_code (stmt) == GIMPLE_PHI)
301 /* A variable defined by a call or a PHI node is considered
302 UNDEFINED. */
303 val.lattice_val = UNDEFINED;
305 else
307 /* Otherwise, VAR will never take on a constant value. */
308 val.lattice_val = VARYING;
309 val.mask = double_int_minus_one;
312 return val;
316 /* Get the constant value associated with variable VAR. */
318 static inline prop_value_t *
319 get_value (tree var)
321 prop_value_t *val;
323 if (const_val == NULL
324 || SSA_NAME_VERSION (var) >= n_const_val)
325 return NULL;
327 val = &const_val[SSA_NAME_VERSION (var)];
328 if (val->lattice_val == UNINITIALIZED)
329 *val = get_default_value (var);
331 canonicalize_value (val);
333 return val;
336 /* Return the constant tree value associated with VAR. */
338 static inline tree
339 get_constant_value (tree var)
341 prop_value_t *val;
342 if (TREE_CODE (var) != SSA_NAME)
344 if (is_gimple_min_invariant (var))
345 return var;
346 return NULL_TREE;
348 val = get_value (var);
349 if (val
350 && val->lattice_val == CONSTANT
351 && (TREE_CODE (val->value) != INTEGER_CST
352 || val->mask.is_zero ()))
353 return val->value;
354 return NULL_TREE;
357 /* Sets the value associated with VAR to VARYING. */
359 static inline void
360 set_value_varying (tree var)
362 prop_value_t *val = &const_val[SSA_NAME_VERSION (var)];
364 val->lattice_val = VARYING;
365 val->value = NULL_TREE;
366 val->mask = double_int_minus_one;
369 /* For float types, modify the value of VAL to make ccp work correctly
370 for non-standard values (-0, NaN):
372 If HONOR_SIGNED_ZEROS is false, and VAL = -0, we canonicalize it to 0.
373 If HONOR_NANS is false, and VAL is NaN, we canonicalize it to UNDEFINED.
374 This is to fix the following problem (see PR 29921): Suppose we have
376 x = 0.0 * y
378 and we set value of y to NaN. This causes value of x to be set to NaN.
379 When we later determine that y is in fact VARYING, fold uses the fact
380 that HONOR_NANS is false, and we try to change the value of x to 0,
381 causing an ICE. With HONOR_NANS being false, the real appearance of
382 NaN would cause undefined behavior, though, so claiming that y (and x)
383 are UNDEFINED initially is correct.
385 For other constants, make sure to drop TREE_OVERFLOW. */
387 static void
388 canonicalize_value (prop_value_t *val)
390 enum machine_mode mode;
391 tree type;
392 REAL_VALUE_TYPE d;
394 if (val->lattice_val != CONSTANT)
395 return;
397 if (TREE_OVERFLOW_P (val->value))
398 val->value = drop_tree_overflow (val->value);
400 if (TREE_CODE (val->value) != REAL_CST)
401 return;
403 d = TREE_REAL_CST (val->value);
404 type = TREE_TYPE (val->value);
405 mode = TYPE_MODE (type);
407 if (!HONOR_SIGNED_ZEROS (mode)
408 && REAL_VALUE_MINUS_ZERO (d))
410 val->value = build_real (type, dconst0);
411 return;
414 if (!HONOR_NANS (mode)
415 && REAL_VALUE_ISNAN (d))
417 val->lattice_val = UNDEFINED;
418 val->value = NULL;
419 return;
423 /* Return whether the lattice transition is valid. */
425 static bool
426 valid_lattice_transition (prop_value_t old_val, prop_value_t new_val)
428 /* Lattice transitions must always be monotonically increasing in
429 value. */
430 if (old_val.lattice_val < new_val.lattice_val)
431 return true;
433 if (old_val.lattice_val != new_val.lattice_val)
434 return false;
436 if (!old_val.value && !new_val.value)
437 return true;
439 /* Now both lattice values are CONSTANT. */
441 /* Allow transitioning from PHI <&x, not executable> == &x
442 to PHI <&x, &y> == common alignment. */
443 if (TREE_CODE (old_val.value) != INTEGER_CST
444 && TREE_CODE (new_val.value) == INTEGER_CST)
445 return true;
447 /* Bit-lattices have to agree in the still valid bits. */
448 if (TREE_CODE (old_val.value) == INTEGER_CST
449 && TREE_CODE (new_val.value) == INTEGER_CST)
450 return tree_to_double_int (old_val.value).and_not (new_val.mask)
451 == tree_to_double_int (new_val.value).and_not (new_val.mask);
453 /* Otherwise constant values have to agree. */
454 return operand_equal_p (old_val.value, new_val.value, 0);
457 /* Set the value for variable VAR to NEW_VAL. Return true if the new
458 value is different from VAR's previous value. */
460 static bool
461 set_lattice_value (tree var, prop_value_t new_val)
463 /* We can deal with old UNINITIALIZED values just fine here. */
464 prop_value_t *old_val = &const_val[SSA_NAME_VERSION (var)];
466 canonicalize_value (&new_val);
468 /* We have to be careful to not go up the bitwise lattice
469 represented by the mask.
470 ??? This doesn't seem to be the best place to enforce this. */
471 if (new_val.lattice_val == CONSTANT
472 && old_val->lattice_val == CONSTANT
473 && TREE_CODE (new_val.value) == INTEGER_CST
474 && TREE_CODE (old_val->value) == INTEGER_CST)
476 double_int diff;
477 diff = tree_to_double_int (new_val.value)
478 ^ tree_to_double_int (old_val->value);
479 new_val.mask = new_val.mask | old_val->mask | diff;
482 gcc_assert (valid_lattice_transition (*old_val, new_val));
484 /* If *OLD_VAL and NEW_VAL are the same, return false to inform the
485 caller that this was a non-transition. */
486 if (old_val->lattice_val != new_val.lattice_val
487 || (new_val.lattice_val == CONSTANT
488 && TREE_CODE (new_val.value) == INTEGER_CST
489 && (TREE_CODE (old_val->value) != INTEGER_CST
490 || new_val.mask != old_val->mask)))
492 /* ??? We would like to delay creation of INTEGER_CSTs from
493 partially constants here. */
495 if (dump_file && (dump_flags & TDF_DETAILS))
497 dump_lattice_value (dump_file, "Lattice value changed to ", new_val);
498 fprintf (dump_file, ". Adding SSA edges to worklist.\n");
501 *old_val = new_val;
503 gcc_assert (new_val.lattice_val != UNINITIALIZED);
504 return true;
507 return false;
510 static prop_value_t get_value_for_expr (tree, bool);
511 static prop_value_t bit_value_binop (enum tree_code, tree, tree, tree);
512 static void bit_value_binop_1 (enum tree_code, tree, double_int *, double_int *,
513 tree, double_int, double_int,
514 tree, double_int, double_int);
516 /* Return a double_int that can be used for bitwise simplifications
517 from VAL. */
519 static double_int
520 value_to_double_int (prop_value_t val)
522 if (val.value
523 && TREE_CODE (val.value) == INTEGER_CST)
524 return tree_to_double_int (val.value);
525 else
526 return double_int_zero;
529 /* Return the value for the address expression EXPR based on alignment
530 information. */
532 static prop_value_t
533 get_value_from_alignment (tree expr)
535 tree type = TREE_TYPE (expr);
536 prop_value_t val;
537 unsigned HOST_WIDE_INT bitpos;
538 unsigned int align;
540 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
542 get_pointer_alignment_1 (expr, &align, &bitpos);
543 val.mask = (POINTER_TYPE_P (type) || TYPE_UNSIGNED (type)
544 ? double_int::mask (TYPE_PRECISION (type))
545 : double_int_minus_one)
546 .and_not (double_int::from_uhwi (align / BITS_PER_UNIT - 1));
547 val.lattice_val = val.mask.is_minus_one () ? VARYING : CONSTANT;
548 if (val.lattice_val == CONSTANT)
549 val.value
550 = double_int_to_tree (type,
551 double_int::from_uhwi (bitpos / BITS_PER_UNIT));
552 else
553 val.value = NULL_TREE;
555 return val;
558 /* Return the value for the tree operand EXPR. If FOR_BITS_P is true
559 return constant bits extracted from alignment information for
560 invariant addresses. */
562 static prop_value_t
563 get_value_for_expr (tree expr, bool for_bits_p)
565 prop_value_t val;
567 if (TREE_CODE (expr) == SSA_NAME)
569 val = *get_value (expr);
570 if (for_bits_p
571 && val.lattice_val == CONSTANT
572 && TREE_CODE (val.value) == ADDR_EXPR)
573 val = get_value_from_alignment (val.value);
575 else if (is_gimple_min_invariant (expr)
576 && (!for_bits_p || TREE_CODE (expr) != ADDR_EXPR))
578 val.lattice_val = CONSTANT;
579 val.value = expr;
580 val.mask = double_int_zero;
581 canonicalize_value (&val);
583 else if (TREE_CODE (expr) == ADDR_EXPR)
584 val = get_value_from_alignment (expr);
585 else
587 val.lattice_val = VARYING;
588 val.mask = double_int_minus_one;
589 val.value = NULL_TREE;
591 return val;
594 /* Return the likely CCP lattice value for STMT.
596 If STMT has no operands, then return CONSTANT.
598 Else if undefinedness of operands of STMT cause its value to be
599 undefined, then return UNDEFINED.
601 Else if any operands of STMT are constants, then return CONSTANT.
603 Else return VARYING. */
605 static ccp_lattice_t
606 likely_value (gimple stmt)
608 bool has_constant_operand, has_undefined_operand, all_undefined_operands;
609 tree use;
610 ssa_op_iter iter;
611 unsigned i;
613 enum gimple_code code = gimple_code (stmt);
615 /* This function appears to be called only for assignments, calls,
616 conditionals, and switches, due to the logic in visit_stmt. */
617 gcc_assert (code == GIMPLE_ASSIGN
618 || code == GIMPLE_CALL
619 || code == GIMPLE_COND
620 || code == GIMPLE_SWITCH);
622 /* If the statement has volatile operands, it won't fold to a
623 constant value. */
624 if (gimple_has_volatile_ops (stmt))
625 return VARYING;
627 /* Arrive here for more complex cases. */
628 has_constant_operand = false;
629 has_undefined_operand = false;
630 all_undefined_operands = true;
631 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
633 prop_value_t *val = get_value (use);
635 if (val->lattice_val == UNDEFINED)
636 has_undefined_operand = true;
637 else
638 all_undefined_operands = false;
640 if (val->lattice_val == CONSTANT)
641 has_constant_operand = true;
644 /* There may be constants in regular rhs operands. For calls we
645 have to ignore lhs, fndecl and static chain, otherwise only
646 the lhs. */
647 for (i = (is_gimple_call (stmt) ? 2 : 0) + gimple_has_lhs (stmt);
648 i < gimple_num_ops (stmt); ++i)
650 tree op = gimple_op (stmt, i);
651 if (!op || TREE_CODE (op) == SSA_NAME)
652 continue;
653 if (is_gimple_min_invariant (op))
654 has_constant_operand = true;
657 if (has_constant_operand)
658 all_undefined_operands = false;
660 if (has_undefined_operand
661 && code == GIMPLE_CALL
662 && gimple_call_internal_p (stmt))
663 switch (gimple_call_internal_fn (stmt))
665 /* These 3 builtins use the first argument just as a magic
666 way how to find out a decl uid. */
667 case IFN_GOMP_SIMD_LANE:
668 case IFN_GOMP_SIMD_VF:
669 case IFN_GOMP_SIMD_LAST_LANE:
670 has_undefined_operand = false;
671 break;
672 default:
673 break;
676 /* If the operation combines operands like COMPLEX_EXPR make sure to
677 not mark the result UNDEFINED if only one part of the result is
678 undefined. */
679 if (has_undefined_operand && all_undefined_operands)
680 return UNDEFINED;
681 else if (code == GIMPLE_ASSIGN && has_undefined_operand)
683 switch (gimple_assign_rhs_code (stmt))
685 /* Unary operators are handled with all_undefined_operands. */
686 case PLUS_EXPR:
687 case MINUS_EXPR:
688 case POINTER_PLUS_EXPR:
689 /* Not MIN_EXPR, MAX_EXPR. One VARYING operand may be selected.
690 Not bitwise operators, one VARYING operand may specify the
691 result completely. Not logical operators for the same reason.
692 Not COMPLEX_EXPR as one VARYING operand makes the result partly
693 not UNDEFINED. Not *DIV_EXPR, comparisons and shifts because
694 the undefined operand may be promoted. */
695 return UNDEFINED;
697 case ADDR_EXPR:
698 /* If any part of an address is UNDEFINED, like the index
699 of an ARRAY_EXPR, then treat the result as UNDEFINED. */
700 return UNDEFINED;
702 default:
706 /* If there was an UNDEFINED operand but the result may be not UNDEFINED
707 fall back to CONSTANT. During iteration UNDEFINED may still drop
708 to CONSTANT. */
709 if (has_undefined_operand)
710 return CONSTANT;
712 /* We do not consider virtual operands here -- load from read-only
713 memory may have only VARYING virtual operands, but still be
714 constant. */
715 if (has_constant_operand
716 || gimple_references_memory_p (stmt))
717 return CONSTANT;
719 return VARYING;
722 /* Returns true if STMT cannot be constant. */
724 static bool
725 surely_varying_stmt_p (gimple stmt)
727 /* If the statement has operands that we cannot handle, it cannot be
728 constant. */
729 if (gimple_has_volatile_ops (stmt))
730 return true;
732 /* If it is a call and does not return a value or is not a
733 builtin and not an indirect call, it is varying. */
734 if (is_gimple_call (stmt))
736 tree fndecl;
737 if (!gimple_call_lhs (stmt)
738 || ((fndecl = gimple_call_fndecl (stmt)) != NULL_TREE
739 && !DECL_BUILT_IN (fndecl)))
740 return true;
743 /* Any other store operation is not interesting. */
744 else if (gimple_vdef (stmt))
745 return true;
747 /* Anything other than assignments and conditional jumps are not
748 interesting for CCP. */
749 if (gimple_code (stmt) != GIMPLE_ASSIGN
750 && gimple_code (stmt) != GIMPLE_COND
751 && gimple_code (stmt) != GIMPLE_SWITCH
752 && gimple_code (stmt) != GIMPLE_CALL)
753 return true;
755 return false;
758 /* Initialize local data structures for CCP. */
760 static void
761 ccp_initialize (void)
763 basic_block bb;
765 n_const_val = num_ssa_names;
766 const_val = XCNEWVEC (prop_value_t, n_const_val);
768 /* Initialize simulation flags for PHI nodes and statements. */
769 FOR_EACH_BB (bb)
771 gimple_stmt_iterator i;
773 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
775 gimple stmt = gsi_stmt (i);
776 bool is_varying;
778 /* If the statement is a control insn, then we do not
779 want to avoid simulating the statement once. Failure
780 to do so means that those edges will never get added. */
781 if (stmt_ends_bb_p (stmt))
782 is_varying = false;
783 else
784 is_varying = surely_varying_stmt_p (stmt);
786 if (is_varying)
788 tree def;
789 ssa_op_iter iter;
791 /* If the statement will not produce a constant, mark
792 all its outputs VARYING. */
793 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
794 set_value_varying (def);
796 prop_set_simulate_again (stmt, !is_varying);
800 /* Now process PHI nodes. We never clear the simulate_again flag on
801 phi nodes, since we do not know which edges are executable yet,
802 except for phi nodes for virtual operands when we do not do store ccp. */
803 FOR_EACH_BB (bb)
805 gimple_stmt_iterator i;
807 for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
809 gimple phi = gsi_stmt (i);
811 if (virtual_operand_p (gimple_phi_result (phi)))
812 prop_set_simulate_again (phi, false);
813 else
814 prop_set_simulate_again (phi, true);
819 /* Debug count support. Reset the values of ssa names
820 VARYING when the total number ssa names analyzed is
821 beyond the debug count specified. */
823 static void
824 do_dbg_cnt (void)
826 unsigned i;
827 for (i = 0; i < num_ssa_names; i++)
829 if (!dbg_cnt (ccp))
831 const_val[i].lattice_val = VARYING;
832 const_val[i].mask = double_int_minus_one;
833 const_val[i].value = NULL_TREE;
839 /* Do final substitution of propagated values, cleanup the flowgraph and
840 free allocated storage.
842 Return TRUE when something was optimized. */
844 static bool
845 ccp_finalize (void)
847 bool something_changed;
848 unsigned i;
850 do_dbg_cnt ();
852 /* Derive alignment and misalignment information from partially
853 constant pointers in the lattice or nonzero bits from partially
854 constant integers. */
855 for (i = 1; i < num_ssa_names; ++i)
857 tree name = ssa_name (i);
858 prop_value_t *val;
859 unsigned int tem, align;
861 if (!name
862 || (!POINTER_TYPE_P (TREE_TYPE (name))
863 && (!INTEGRAL_TYPE_P (TREE_TYPE (name))
864 /* Don't record nonzero bits before IPA to avoid
865 using too much memory. */
866 || first_pass_instance)))
867 continue;
869 val = get_value (name);
870 if (val->lattice_val != CONSTANT
871 || TREE_CODE (val->value) != INTEGER_CST)
872 continue;
874 if (POINTER_TYPE_P (TREE_TYPE (name)))
876 /* Trailing mask bits specify the alignment, trailing value
877 bits the misalignment. */
878 tem = val->mask.low;
879 align = (tem & -tem);
880 if (align > 1)
881 set_ptr_info_alignment (get_ptr_info (name), align,
882 (TREE_INT_CST_LOW (val->value)
883 & (align - 1)));
885 else
887 double_int nonzero_bits = val->mask;
888 nonzero_bits = nonzero_bits | tree_to_double_int (val->value);
889 nonzero_bits &= get_nonzero_bits (name);
890 set_nonzero_bits (name, nonzero_bits);
894 /* Perform substitutions based on the known constant values. */
895 something_changed = substitute_and_fold (get_constant_value,
896 ccp_fold_stmt, true);
898 free (const_val);
899 const_val = NULL;
900 return something_changed;;
904 /* Compute the meet operator between *VAL1 and *VAL2. Store the result
905 in VAL1.
907 any M UNDEFINED = any
908 any M VARYING = VARYING
909 Ci M Cj = Ci if (i == j)
910 Ci M Cj = VARYING if (i != j)
913 static void
914 ccp_lattice_meet (prop_value_t *val1, prop_value_t *val2)
916 if (val1->lattice_val == UNDEFINED)
918 /* UNDEFINED M any = any */
919 *val1 = *val2;
921 else if (val2->lattice_val == UNDEFINED)
923 /* any M UNDEFINED = any
924 Nothing to do. VAL1 already contains the value we want. */
927 else if (val1->lattice_val == VARYING
928 || val2->lattice_val == VARYING)
930 /* any M VARYING = VARYING. */
931 val1->lattice_val = VARYING;
932 val1->mask = double_int_minus_one;
933 val1->value = NULL_TREE;
935 else if (val1->lattice_val == CONSTANT
936 && val2->lattice_val == CONSTANT
937 && TREE_CODE (val1->value) == INTEGER_CST
938 && TREE_CODE (val2->value) == INTEGER_CST)
940 /* Ci M Cj = Ci if (i == j)
941 Ci M Cj = VARYING if (i != j)
943 For INTEGER_CSTs mask unequal bits. If no equal bits remain,
944 drop to varying. */
945 val1->mask = val1->mask | val2->mask
946 | (tree_to_double_int (val1->value)
947 ^ tree_to_double_int (val2->value));
948 if (val1->mask.is_minus_one ())
950 val1->lattice_val = VARYING;
951 val1->value = NULL_TREE;
954 else if (val1->lattice_val == CONSTANT
955 && val2->lattice_val == CONSTANT
956 && simple_cst_equal (val1->value, val2->value) == 1)
958 /* Ci M Cj = Ci if (i == j)
959 Ci M Cj = VARYING if (i != j)
961 VAL1 already contains the value we want for equivalent values. */
963 else if (val1->lattice_val == CONSTANT
964 && val2->lattice_val == CONSTANT
965 && (TREE_CODE (val1->value) == ADDR_EXPR
966 || TREE_CODE (val2->value) == ADDR_EXPR))
968 /* When not equal addresses are involved try meeting for
969 alignment. */
970 prop_value_t tem = *val2;
971 if (TREE_CODE (val1->value) == ADDR_EXPR)
972 *val1 = get_value_for_expr (val1->value, true);
973 if (TREE_CODE (val2->value) == ADDR_EXPR)
974 tem = get_value_for_expr (val2->value, true);
975 ccp_lattice_meet (val1, &tem);
977 else
979 /* Any other combination is VARYING. */
980 val1->lattice_val = VARYING;
981 val1->mask = double_int_minus_one;
982 val1->value = NULL_TREE;
987 /* Loop through the PHI_NODE's parameters for BLOCK and compare their
988 lattice values to determine PHI_NODE's lattice value. The value of a
989 PHI node is determined calling ccp_lattice_meet with all the arguments
990 of the PHI node that are incoming via executable edges. */
992 static enum ssa_prop_result
993 ccp_visit_phi_node (gimple phi)
995 unsigned i;
996 prop_value_t *old_val, new_val;
998 if (dump_file && (dump_flags & TDF_DETAILS))
1000 fprintf (dump_file, "\nVisiting PHI node: ");
1001 print_gimple_stmt (dump_file, phi, 0, dump_flags);
1004 old_val = get_value (gimple_phi_result (phi));
1005 switch (old_val->lattice_val)
1007 case VARYING:
1008 return SSA_PROP_VARYING;
1010 case CONSTANT:
1011 new_val = *old_val;
1012 break;
1014 case UNDEFINED:
1015 new_val.lattice_val = UNDEFINED;
1016 new_val.value = NULL_TREE;
1017 break;
1019 default:
1020 gcc_unreachable ();
1023 for (i = 0; i < gimple_phi_num_args (phi); i++)
1025 /* Compute the meet operator over all the PHI arguments flowing
1026 through executable edges. */
1027 edge e = gimple_phi_arg_edge (phi, i);
1029 if (dump_file && (dump_flags & TDF_DETAILS))
1031 fprintf (dump_file,
1032 "\n Argument #%d (%d -> %d %sexecutable)\n",
1033 i, e->src->index, e->dest->index,
1034 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
1037 /* If the incoming edge is executable, Compute the meet operator for
1038 the existing value of the PHI node and the current PHI argument. */
1039 if (e->flags & EDGE_EXECUTABLE)
1041 tree arg = gimple_phi_arg (phi, i)->def;
1042 prop_value_t arg_val = get_value_for_expr (arg, false);
1044 ccp_lattice_meet (&new_val, &arg_val);
1046 if (dump_file && (dump_flags & TDF_DETAILS))
1048 fprintf (dump_file, "\t");
1049 print_generic_expr (dump_file, arg, dump_flags);
1050 dump_lattice_value (dump_file, "\tValue: ", arg_val);
1051 fprintf (dump_file, "\n");
1054 if (new_val.lattice_val == VARYING)
1055 break;
1059 if (dump_file && (dump_flags & TDF_DETAILS))
1061 dump_lattice_value (dump_file, "\n PHI node value: ", new_val);
1062 fprintf (dump_file, "\n\n");
1065 /* Make the transition to the new value. */
1066 if (set_lattice_value (gimple_phi_result (phi), new_val))
1068 if (new_val.lattice_val == VARYING)
1069 return SSA_PROP_VARYING;
1070 else
1071 return SSA_PROP_INTERESTING;
1073 else
1074 return SSA_PROP_NOT_INTERESTING;
1077 /* Return the constant value for OP or OP otherwise. */
1079 static tree
1080 valueize_op (tree op)
1082 if (TREE_CODE (op) == SSA_NAME)
1084 tree tem = get_constant_value (op);
1085 if (tem)
1086 return tem;
1088 return op;
1091 /* CCP specific front-end to the non-destructive constant folding
1092 routines.
1094 Attempt to simplify the RHS of STMT knowing that one or more
1095 operands are constants.
1097 If simplification is possible, return the simplified RHS,
1098 otherwise return the original RHS or NULL_TREE. */
1100 static tree
1101 ccp_fold (gimple stmt)
1103 location_t loc = gimple_location (stmt);
1104 switch (gimple_code (stmt))
1106 case GIMPLE_COND:
1108 /* Handle comparison operators that can appear in GIMPLE form. */
1109 tree op0 = valueize_op (gimple_cond_lhs (stmt));
1110 tree op1 = valueize_op (gimple_cond_rhs (stmt));
1111 enum tree_code code = gimple_cond_code (stmt);
1112 return fold_binary_loc (loc, code, boolean_type_node, op0, op1);
1115 case GIMPLE_SWITCH:
1117 /* Return the constant switch index. */
1118 return valueize_op (gimple_switch_index (stmt));
1121 case GIMPLE_ASSIGN:
1122 case GIMPLE_CALL:
1123 return gimple_fold_stmt_to_constant_1 (stmt, valueize_op);
1125 default:
1126 gcc_unreachable ();
1130 /* Apply the operation CODE in type TYPE to the value, mask pair
1131 RVAL and RMASK representing a value of type RTYPE and set
1132 the value, mask pair *VAL and *MASK to the result. */
1134 static void
1135 bit_value_unop_1 (enum tree_code code, tree type,
1136 double_int *val, double_int *mask,
1137 tree rtype, double_int rval, double_int rmask)
1139 switch (code)
1141 case BIT_NOT_EXPR:
1142 *mask = rmask;
1143 *val = ~rval;
1144 break;
1146 case NEGATE_EXPR:
1148 double_int temv, temm;
1149 /* Return ~rval + 1. */
1150 bit_value_unop_1 (BIT_NOT_EXPR, type, &temv, &temm, type, rval, rmask);
1151 bit_value_binop_1 (PLUS_EXPR, type, val, mask,
1152 type, temv, temm,
1153 type, double_int_one, double_int_zero);
1154 break;
1157 CASE_CONVERT:
1159 bool uns;
1161 /* First extend mask and value according to the original type. */
1162 uns = TYPE_UNSIGNED (rtype);
1163 *mask = rmask.ext (TYPE_PRECISION (rtype), uns);
1164 *val = rval.ext (TYPE_PRECISION (rtype), uns);
1166 /* Then extend mask and value according to the target type. */
1167 uns = TYPE_UNSIGNED (type);
1168 *mask = (*mask).ext (TYPE_PRECISION (type), uns);
1169 *val = (*val).ext (TYPE_PRECISION (type), uns);
1170 break;
1173 default:
1174 *mask = double_int_minus_one;
1175 break;
1179 /* Apply the operation CODE in type TYPE to the value, mask pairs
1180 R1VAL, R1MASK and R2VAL, R2MASK representing a values of type R1TYPE
1181 and R2TYPE and set the value, mask pair *VAL and *MASK to the result. */
1183 static void
1184 bit_value_binop_1 (enum tree_code code, tree type,
1185 double_int *val, double_int *mask,
1186 tree r1type, double_int r1val, double_int r1mask,
1187 tree r2type, double_int r2val, double_int r2mask)
1189 bool uns = TYPE_UNSIGNED (type);
1190 /* Assume we'll get a constant result. Use an initial varying value,
1191 we fall back to varying in the end if necessary. */
1192 *mask = double_int_minus_one;
1193 switch (code)
1195 case BIT_AND_EXPR:
1196 /* The mask is constant where there is a known not
1197 set bit, (m1 | m2) & ((v1 | m1) & (v2 | m2)) */
1198 *mask = (r1mask | r2mask) & (r1val | r1mask) & (r2val | r2mask);
1199 *val = r1val & r2val;
1200 break;
1202 case BIT_IOR_EXPR:
1203 /* The mask is constant where there is a known
1204 set bit, (m1 | m2) & ~((v1 & ~m1) | (v2 & ~m2)). */
1205 *mask = (r1mask | r2mask)
1206 .and_not (r1val.and_not (r1mask) | r2val.and_not (r2mask));
1207 *val = r1val | r2val;
1208 break;
1210 case BIT_XOR_EXPR:
1211 /* m1 | m2 */
1212 *mask = r1mask | r2mask;
1213 *val = r1val ^ r2val;
1214 break;
1216 case LROTATE_EXPR:
1217 case RROTATE_EXPR:
1218 if (r2mask.is_zero ())
1220 HOST_WIDE_INT shift = r2val.low;
1221 if (code == RROTATE_EXPR)
1222 shift = -shift;
1223 *mask = r1mask.lrotate (shift, TYPE_PRECISION (type));
1224 *val = r1val.lrotate (shift, TYPE_PRECISION (type));
1226 break;
1228 case LSHIFT_EXPR:
1229 case RSHIFT_EXPR:
1230 /* ??? We can handle partially known shift counts if we know
1231 its sign. That way we can tell that (x << (y | 8)) & 255
1232 is zero. */
1233 if (r2mask.is_zero ())
1235 HOST_WIDE_INT shift = r2val.low;
1236 if (code == RSHIFT_EXPR)
1237 shift = -shift;
1238 /* We need to know if we are doing a left or a right shift
1239 to properly shift in zeros for left shift and unsigned
1240 right shifts and the sign bit for signed right shifts.
1241 For signed right shifts we shift in varying in case
1242 the sign bit was varying. */
1243 if (shift > 0)
1245 *mask = r1mask.llshift (shift, TYPE_PRECISION (type));
1246 *val = r1val.llshift (shift, TYPE_PRECISION (type));
1248 else if (shift < 0)
1250 shift = -shift;
1251 *mask = r1mask.rshift (shift, TYPE_PRECISION (type), !uns);
1252 *val = r1val.rshift (shift, TYPE_PRECISION (type), !uns);
1254 else
1256 *mask = r1mask;
1257 *val = r1val;
1260 break;
1262 case PLUS_EXPR:
1263 case POINTER_PLUS_EXPR:
1265 double_int lo, hi;
1266 /* Do the addition with unknown bits set to zero, to give carry-ins of
1267 zero wherever possible. */
1268 lo = r1val.and_not (r1mask) + r2val.and_not (r2mask);
1269 lo = lo.ext (TYPE_PRECISION (type), uns);
1270 /* Do the addition with unknown bits set to one, to give carry-ins of
1271 one wherever possible. */
1272 hi = (r1val | r1mask) + (r2val | r2mask);
1273 hi = hi.ext (TYPE_PRECISION (type), uns);
1274 /* Each bit in the result is known if (a) the corresponding bits in
1275 both inputs are known, and (b) the carry-in to that bit position
1276 is known. We can check condition (b) by seeing if we got the same
1277 result with minimised carries as with maximised carries. */
1278 *mask = r1mask | r2mask | (lo ^ hi);
1279 *mask = (*mask).ext (TYPE_PRECISION (type), uns);
1280 /* It shouldn't matter whether we choose lo or hi here. */
1281 *val = lo;
1282 break;
1285 case MINUS_EXPR:
1287 double_int temv, temm;
1288 bit_value_unop_1 (NEGATE_EXPR, r2type, &temv, &temm,
1289 r2type, r2val, r2mask);
1290 bit_value_binop_1 (PLUS_EXPR, type, val, mask,
1291 r1type, r1val, r1mask,
1292 r2type, temv, temm);
1293 break;
1296 case MULT_EXPR:
1298 /* Just track trailing zeros in both operands and transfer
1299 them to the other. */
1300 int r1tz = (r1val | r1mask).trailing_zeros ();
1301 int r2tz = (r2val | r2mask).trailing_zeros ();
1302 if (r1tz + r2tz >= HOST_BITS_PER_DOUBLE_INT)
1304 *mask = double_int_zero;
1305 *val = double_int_zero;
1307 else if (r1tz + r2tz > 0)
1309 *mask = ~double_int::mask (r1tz + r2tz);
1310 *mask = (*mask).ext (TYPE_PRECISION (type), uns);
1311 *val = double_int_zero;
1313 break;
1316 case EQ_EXPR:
1317 case NE_EXPR:
1319 double_int m = r1mask | r2mask;
1320 if (r1val.and_not (m) != r2val.and_not (m))
1322 *mask = double_int_zero;
1323 *val = ((code == EQ_EXPR) ? double_int_zero : double_int_one);
1325 else
1327 /* We know the result of a comparison is always one or zero. */
1328 *mask = double_int_one;
1329 *val = double_int_zero;
1331 break;
1334 case GE_EXPR:
1335 case GT_EXPR:
1337 double_int tem = r1val;
1338 r1val = r2val;
1339 r2val = tem;
1340 tem = r1mask;
1341 r1mask = r2mask;
1342 r2mask = tem;
1343 code = swap_tree_comparison (code);
1345 /* Fallthru. */
1346 case LT_EXPR:
1347 case LE_EXPR:
1349 int minmax, maxmin;
1350 /* If the most significant bits are not known we know nothing. */
1351 if (r1mask.is_negative () || r2mask.is_negative ())
1352 break;
1354 /* For comparisons the signedness is in the comparison operands. */
1355 uns = TYPE_UNSIGNED (r1type);
1357 /* If we know the most significant bits we know the values
1358 value ranges by means of treating varying bits as zero
1359 or one. Do a cross comparison of the max/min pairs. */
1360 maxmin = (r1val | r1mask).cmp (r2val.and_not (r2mask), uns);
1361 minmax = r1val.and_not (r1mask).cmp (r2val | r2mask, uns);
1362 if (maxmin < 0) /* r1 is less than r2. */
1364 *mask = double_int_zero;
1365 *val = double_int_one;
1367 else if (minmax > 0) /* r1 is not less or equal to r2. */
1369 *mask = double_int_zero;
1370 *val = double_int_zero;
1372 else if (maxmin == minmax) /* r1 and r2 are equal. */
1374 /* This probably should never happen as we'd have
1375 folded the thing during fully constant value folding. */
1376 *mask = double_int_zero;
1377 *val = (code == LE_EXPR ? double_int_one : double_int_zero);
1379 else
1381 /* We know the result of a comparison is always one or zero. */
1382 *mask = double_int_one;
1383 *val = double_int_zero;
1385 break;
1388 default:;
1392 /* Return the propagation value when applying the operation CODE to
1393 the value RHS yielding type TYPE. */
1395 static prop_value_t
1396 bit_value_unop (enum tree_code code, tree type, tree rhs)
1398 prop_value_t rval = get_value_for_expr (rhs, true);
1399 double_int value, mask;
1400 prop_value_t val;
1402 if (rval.lattice_val == UNDEFINED)
1403 return rval;
1405 gcc_assert ((rval.lattice_val == CONSTANT
1406 && TREE_CODE (rval.value) == INTEGER_CST)
1407 || rval.mask.is_minus_one ());
1408 bit_value_unop_1 (code, type, &value, &mask,
1409 TREE_TYPE (rhs), value_to_double_int (rval), rval.mask);
1410 if (!mask.is_minus_one ())
1412 val.lattice_val = CONSTANT;
1413 val.mask = mask;
1414 /* ??? Delay building trees here. */
1415 val.value = double_int_to_tree (type, value);
1417 else
1419 val.lattice_val = VARYING;
1420 val.value = NULL_TREE;
1421 val.mask = double_int_minus_one;
1423 return val;
1426 /* Return the propagation value when applying the operation CODE to
1427 the values RHS1 and RHS2 yielding type TYPE. */
1429 static prop_value_t
1430 bit_value_binop (enum tree_code code, tree type, tree rhs1, tree rhs2)
1432 prop_value_t r1val = get_value_for_expr (rhs1, true);
1433 prop_value_t r2val = get_value_for_expr (rhs2, true);
1434 double_int value, mask;
1435 prop_value_t val;
1437 if (r1val.lattice_val == UNDEFINED
1438 || r2val.lattice_val == UNDEFINED)
1440 val.lattice_val = VARYING;
1441 val.value = NULL_TREE;
1442 val.mask = double_int_minus_one;
1443 return val;
1446 gcc_assert ((r1val.lattice_val == CONSTANT
1447 && TREE_CODE (r1val.value) == INTEGER_CST)
1448 || r1val.mask.is_minus_one ());
1449 gcc_assert ((r2val.lattice_val == CONSTANT
1450 && TREE_CODE (r2val.value) == INTEGER_CST)
1451 || r2val.mask.is_minus_one ());
1452 bit_value_binop_1 (code, type, &value, &mask,
1453 TREE_TYPE (rhs1), value_to_double_int (r1val), r1val.mask,
1454 TREE_TYPE (rhs2), value_to_double_int (r2val), r2val.mask);
1455 if (!mask.is_minus_one ())
1457 val.lattice_val = CONSTANT;
1458 val.mask = mask;
1459 /* ??? Delay building trees here. */
1460 val.value = double_int_to_tree (type, value);
1462 else
1464 val.lattice_val = VARYING;
1465 val.value = NULL_TREE;
1466 val.mask = double_int_minus_one;
1468 return val;
1471 /* Return the propagation value when applying __builtin_assume_aligned to
1472 its arguments. */
1474 static prop_value_t
1475 bit_value_assume_aligned (gimple stmt)
1477 tree ptr = gimple_call_arg (stmt, 0), align, misalign = NULL_TREE;
1478 tree type = TREE_TYPE (ptr);
1479 unsigned HOST_WIDE_INT aligni, misaligni = 0;
1480 prop_value_t ptrval = get_value_for_expr (ptr, true);
1481 prop_value_t alignval;
1482 double_int value, mask;
1483 prop_value_t val;
1484 if (ptrval.lattice_val == UNDEFINED)
1485 return ptrval;
1486 gcc_assert ((ptrval.lattice_val == CONSTANT
1487 && TREE_CODE (ptrval.value) == INTEGER_CST)
1488 || ptrval.mask.is_minus_one ());
1489 align = gimple_call_arg (stmt, 1);
1490 if (!host_integerp (align, 1))
1491 return ptrval;
1492 aligni = tree_low_cst (align, 1);
1493 if (aligni <= 1
1494 || (aligni & (aligni - 1)) != 0)
1495 return ptrval;
1496 if (gimple_call_num_args (stmt) > 2)
1498 misalign = gimple_call_arg (stmt, 2);
1499 if (!host_integerp (misalign, 1))
1500 return ptrval;
1501 misaligni = tree_low_cst (misalign, 1);
1502 if (misaligni >= aligni)
1503 return ptrval;
1505 align = build_int_cst_type (type, -aligni);
1506 alignval = get_value_for_expr (align, true);
1507 bit_value_binop_1 (BIT_AND_EXPR, type, &value, &mask,
1508 type, value_to_double_int (ptrval), ptrval.mask,
1509 type, value_to_double_int (alignval), alignval.mask);
1510 if (!mask.is_minus_one ())
1512 val.lattice_val = CONSTANT;
1513 val.mask = mask;
1514 gcc_assert ((mask.low & (aligni - 1)) == 0);
1515 gcc_assert ((value.low & (aligni - 1)) == 0);
1516 value.low |= misaligni;
1517 /* ??? Delay building trees here. */
1518 val.value = double_int_to_tree (type, value);
1520 else
1522 val.lattice_val = VARYING;
1523 val.value = NULL_TREE;
1524 val.mask = double_int_minus_one;
1526 return val;
1529 /* Evaluate statement STMT.
1530 Valid only for assignments, calls, conditionals, and switches. */
1532 static prop_value_t
1533 evaluate_stmt (gimple stmt)
1535 prop_value_t val;
1536 tree simplified = NULL_TREE;
1537 ccp_lattice_t likelyvalue = likely_value (stmt);
1538 bool is_constant = false;
1539 unsigned int align;
1541 if (dump_file && (dump_flags & TDF_DETAILS))
1543 fprintf (dump_file, "which is likely ");
1544 switch (likelyvalue)
1546 case CONSTANT:
1547 fprintf (dump_file, "CONSTANT");
1548 break;
1549 case UNDEFINED:
1550 fprintf (dump_file, "UNDEFINED");
1551 break;
1552 case VARYING:
1553 fprintf (dump_file, "VARYING");
1554 break;
1555 default:;
1557 fprintf (dump_file, "\n");
1560 /* If the statement is likely to have a CONSTANT result, then try
1561 to fold the statement to determine the constant value. */
1562 /* FIXME. This is the only place that we call ccp_fold.
1563 Since likely_value never returns CONSTANT for calls, we will
1564 not attempt to fold them, including builtins that may profit. */
1565 if (likelyvalue == CONSTANT)
1567 fold_defer_overflow_warnings ();
1568 simplified = ccp_fold (stmt);
1569 is_constant = simplified && is_gimple_min_invariant (simplified);
1570 fold_undefer_overflow_warnings (is_constant, stmt, 0);
1571 if (is_constant)
1573 /* The statement produced a constant value. */
1574 val.lattice_val = CONSTANT;
1575 val.value = simplified;
1576 val.mask = double_int_zero;
1579 /* If the statement is likely to have a VARYING result, then do not
1580 bother folding the statement. */
1581 else if (likelyvalue == VARYING)
1583 enum gimple_code code = gimple_code (stmt);
1584 if (code == GIMPLE_ASSIGN)
1586 enum tree_code subcode = gimple_assign_rhs_code (stmt);
1588 /* Other cases cannot satisfy is_gimple_min_invariant
1589 without folding. */
1590 if (get_gimple_rhs_class (subcode) == GIMPLE_SINGLE_RHS)
1591 simplified = gimple_assign_rhs1 (stmt);
1593 else if (code == GIMPLE_SWITCH)
1594 simplified = gimple_switch_index (stmt);
1595 else
1596 /* These cannot satisfy is_gimple_min_invariant without folding. */
1597 gcc_assert (code == GIMPLE_CALL || code == GIMPLE_COND);
1598 is_constant = simplified && is_gimple_min_invariant (simplified);
1599 if (is_constant)
1601 /* The statement produced a constant value. */
1602 val.lattice_val = CONSTANT;
1603 val.value = simplified;
1604 val.mask = double_int_zero;
1608 /* Resort to simplification for bitwise tracking. */
1609 if (flag_tree_bit_ccp
1610 && (likelyvalue == CONSTANT || is_gimple_call (stmt))
1611 && !is_constant)
1613 enum gimple_code code = gimple_code (stmt);
1614 val.lattice_val = VARYING;
1615 val.value = NULL_TREE;
1616 val.mask = double_int_minus_one;
1617 if (code == GIMPLE_ASSIGN)
1619 enum tree_code subcode = gimple_assign_rhs_code (stmt);
1620 tree rhs1 = gimple_assign_rhs1 (stmt);
1621 switch (get_gimple_rhs_class (subcode))
1623 case GIMPLE_SINGLE_RHS:
1624 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1625 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1626 val = get_value_for_expr (rhs1, true);
1627 break;
1629 case GIMPLE_UNARY_RHS:
1630 if ((INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1631 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1632 && (INTEGRAL_TYPE_P (gimple_expr_type (stmt))
1633 || POINTER_TYPE_P (gimple_expr_type (stmt))))
1634 val = bit_value_unop (subcode, gimple_expr_type (stmt), rhs1);
1635 break;
1637 case GIMPLE_BINARY_RHS:
1638 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1639 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1641 tree lhs = gimple_assign_lhs (stmt);
1642 tree rhs2 = gimple_assign_rhs2 (stmt);
1643 val = bit_value_binop (subcode,
1644 TREE_TYPE (lhs), rhs1, rhs2);
1646 break;
1648 default:;
1651 else if (code == GIMPLE_COND)
1653 enum tree_code code = gimple_cond_code (stmt);
1654 tree rhs1 = gimple_cond_lhs (stmt);
1655 tree rhs2 = gimple_cond_rhs (stmt);
1656 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1657 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1658 val = bit_value_binop (code, TREE_TYPE (rhs1), rhs1, rhs2);
1660 else if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
1662 tree fndecl = gimple_call_fndecl (stmt);
1663 switch (DECL_FUNCTION_CODE (fndecl))
1665 case BUILT_IN_MALLOC:
1666 case BUILT_IN_REALLOC:
1667 case BUILT_IN_CALLOC:
1668 case BUILT_IN_STRDUP:
1669 case BUILT_IN_STRNDUP:
1670 val.lattice_val = CONSTANT;
1671 val.value = build_int_cst (TREE_TYPE (gimple_get_lhs (stmt)), 0);
1672 val.mask = double_int::from_shwi
1673 (~(((HOST_WIDE_INT) MALLOC_ABI_ALIGNMENT)
1674 / BITS_PER_UNIT - 1));
1675 break;
1677 case BUILT_IN_ALLOCA:
1678 case BUILT_IN_ALLOCA_WITH_ALIGN:
1679 align = (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_ALLOCA_WITH_ALIGN
1680 ? TREE_INT_CST_LOW (gimple_call_arg (stmt, 1))
1681 : BIGGEST_ALIGNMENT);
1682 val.lattice_val = CONSTANT;
1683 val.value = build_int_cst (TREE_TYPE (gimple_get_lhs (stmt)), 0);
1684 val.mask = double_int::from_shwi (~(((HOST_WIDE_INT) align)
1685 / BITS_PER_UNIT - 1));
1686 break;
1688 /* These builtins return their first argument, unmodified. */
1689 case BUILT_IN_MEMCPY:
1690 case BUILT_IN_MEMMOVE:
1691 case BUILT_IN_MEMSET:
1692 case BUILT_IN_STRCPY:
1693 case BUILT_IN_STRNCPY:
1694 case BUILT_IN_MEMCPY_CHK:
1695 case BUILT_IN_MEMMOVE_CHK:
1696 case BUILT_IN_MEMSET_CHK:
1697 case BUILT_IN_STRCPY_CHK:
1698 case BUILT_IN_STRNCPY_CHK:
1699 val = get_value_for_expr (gimple_call_arg (stmt, 0), true);
1700 break;
1702 case BUILT_IN_ASSUME_ALIGNED:
1703 val = bit_value_assume_aligned (stmt);
1704 break;
1706 default:;
1709 is_constant = (val.lattice_val == CONSTANT);
1712 if (flag_tree_bit_ccp
1713 && ((is_constant && TREE_CODE (val.value) == INTEGER_CST)
1714 || (!is_constant && likelyvalue != UNDEFINED))
1715 && gimple_get_lhs (stmt)
1716 && TREE_CODE (gimple_get_lhs (stmt)) == SSA_NAME)
1718 tree lhs = gimple_get_lhs (stmt);
1719 double_int nonzero_bits = get_nonzero_bits (lhs);
1720 double_int mask = double_int::mask (TYPE_PRECISION (TREE_TYPE (lhs)));
1721 if (nonzero_bits != double_int_minus_one && nonzero_bits != mask)
1723 if (!is_constant)
1725 val.lattice_val = CONSTANT;
1726 val.value = build_zero_cst (TREE_TYPE (lhs));
1727 /* CCP wants the bits above precision set. */
1728 val.mask = nonzero_bits | ~mask;
1729 is_constant = true;
1731 else
1733 double_int valv = tree_to_double_int (val.value);
1734 if (!(valv & ~nonzero_bits & mask).is_zero ())
1735 val.value = double_int_to_tree (TREE_TYPE (lhs),
1736 valv & nonzero_bits);
1737 if (nonzero_bits.is_zero ())
1738 val.mask = double_int_zero;
1739 else
1740 val.mask = val.mask & (nonzero_bits | ~mask);
1745 if (!is_constant)
1747 /* The statement produced a nonconstant value. If the statement
1748 had UNDEFINED operands, then the result of the statement
1749 should be UNDEFINED. Otherwise, the statement is VARYING. */
1750 if (likelyvalue == UNDEFINED)
1752 val.lattice_val = likelyvalue;
1753 val.mask = double_int_zero;
1755 else
1757 val.lattice_val = VARYING;
1758 val.mask = double_int_minus_one;
1761 val.value = NULL_TREE;
1764 return val;
1767 typedef hash_table <pointer_hash <gimple_statement_d> > gimple_htab;
1769 /* Given a BUILT_IN_STACK_SAVE value SAVED_VAL, insert a clobber of VAR before
1770 each matching BUILT_IN_STACK_RESTORE. Mark visited phis in VISITED. */
1772 static void
1773 insert_clobber_before_stack_restore (tree saved_val, tree var,
1774 gimple_htab *visited)
1776 gimple stmt, clobber_stmt;
1777 tree clobber;
1778 imm_use_iterator iter;
1779 gimple_stmt_iterator i;
1780 gimple *slot;
1782 FOR_EACH_IMM_USE_STMT (stmt, iter, saved_val)
1783 if (gimple_call_builtin_p (stmt, BUILT_IN_STACK_RESTORE))
1785 clobber = build_constructor (TREE_TYPE (var),
1786 NULL);
1787 TREE_THIS_VOLATILE (clobber) = 1;
1788 clobber_stmt = gimple_build_assign (var, clobber);
1790 i = gsi_for_stmt (stmt);
1791 gsi_insert_before (&i, clobber_stmt, GSI_SAME_STMT);
1793 else if (gimple_code (stmt) == GIMPLE_PHI)
1795 if (!visited->is_created ())
1796 visited->create (10);
1798 slot = visited->find_slot (stmt, INSERT);
1799 if (*slot != NULL)
1800 continue;
1802 *slot = stmt;
1803 insert_clobber_before_stack_restore (gimple_phi_result (stmt), var,
1804 visited);
1806 else if (gimple_assign_ssa_name_copy_p (stmt))
1807 insert_clobber_before_stack_restore (gimple_assign_lhs (stmt), var,
1808 visited);
1809 else
1810 gcc_assert (is_gimple_debug (stmt));
1813 /* Advance the iterator to the previous non-debug gimple statement in the same
1814 or dominating basic block. */
1816 static inline void
1817 gsi_prev_dom_bb_nondebug (gimple_stmt_iterator *i)
1819 basic_block dom;
1821 gsi_prev_nondebug (i);
1822 while (gsi_end_p (*i))
1824 dom = get_immediate_dominator (CDI_DOMINATORS, i->bb);
1825 if (dom == NULL || dom == ENTRY_BLOCK_PTR)
1826 return;
1828 *i = gsi_last_bb (dom);
1832 /* Find a BUILT_IN_STACK_SAVE dominating gsi_stmt (I), and insert
1833 a clobber of VAR before each matching BUILT_IN_STACK_RESTORE.
1835 It is possible that BUILT_IN_STACK_SAVE cannot be find in a dominator when a
1836 previous pass (such as DOM) duplicated it along multiple paths to a BB. In
1837 that case the function gives up without inserting the clobbers. */
1839 static void
1840 insert_clobbers_for_var (gimple_stmt_iterator i, tree var)
1842 gimple stmt;
1843 tree saved_val;
1844 gimple_htab visited;
1846 for (; !gsi_end_p (i); gsi_prev_dom_bb_nondebug (&i))
1848 stmt = gsi_stmt (i);
1850 if (!gimple_call_builtin_p (stmt, BUILT_IN_STACK_SAVE))
1851 continue;
1853 saved_val = gimple_call_lhs (stmt);
1854 if (saved_val == NULL_TREE)
1855 continue;
1857 insert_clobber_before_stack_restore (saved_val, var, &visited);
1858 break;
1861 if (visited.is_created ())
1862 visited.dispose ();
1865 /* Detects a __builtin_alloca_with_align with constant size argument. Declares
1866 fixed-size array and returns the address, if found, otherwise returns
1867 NULL_TREE. */
1869 static tree
1870 fold_builtin_alloca_with_align (gimple stmt)
1872 unsigned HOST_WIDE_INT size, threshold, n_elem;
1873 tree lhs, arg, block, var, elem_type, array_type;
1875 /* Get lhs. */
1876 lhs = gimple_call_lhs (stmt);
1877 if (lhs == NULL_TREE)
1878 return NULL_TREE;
1880 /* Detect constant argument. */
1881 arg = get_constant_value (gimple_call_arg (stmt, 0));
1882 if (arg == NULL_TREE
1883 || TREE_CODE (arg) != INTEGER_CST
1884 || !host_integerp (arg, 1))
1885 return NULL_TREE;
1887 size = TREE_INT_CST_LOW (arg);
1889 /* Heuristic: don't fold large allocas. */
1890 threshold = (unsigned HOST_WIDE_INT)PARAM_VALUE (PARAM_LARGE_STACK_FRAME);
1891 /* In case the alloca is located at function entry, it has the same lifetime
1892 as a declared array, so we allow a larger size. */
1893 block = gimple_block (stmt);
1894 if (!(cfun->after_inlining
1895 && TREE_CODE (BLOCK_SUPERCONTEXT (block)) == FUNCTION_DECL))
1896 threshold /= 10;
1897 if (size > threshold)
1898 return NULL_TREE;
1900 /* Declare array. */
1901 elem_type = build_nonstandard_integer_type (BITS_PER_UNIT, 1);
1902 n_elem = size * 8 / BITS_PER_UNIT;
1903 array_type = build_array_type_nelts (elem_type, n_elem);
1904 var = create_tmp_var (array_type, NULL);
1905 DECL_ALIGN (var) = TREE_INT_CST_LOW (gimple_call_arg (stmt, 1));
1907 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (lhs);
1908 if (pi != NULL && !pi->pt.anything)
1910 bool singleton_p;
1911 unsigned uid;
1912 singleton_p = pt_solution_singleton_p (&pi->pt, &uid);
1913 gcc_assert (singleton_p);
1914 SET_DECL_PT_UID (var, uid);
1918 /* Fold alloca to the address of the array. */
1919 return fold_convert (TREE_TYPE (lhs), build_fold_addr_expr (var));
1922 /* Fold the stmt at *GSI with CCP specific information that propagating
1923 and regular folding does not catch. */
1925 static bool
1926 ccp_fold_stmt (gimple_stmt_iterator *gsi)
1928 gimple stmt = gsi_stmt (*gsi);
1930 switch (gimple_code (stmt))
1932 case GIMPLE_COND:
1934 prop_value_t val;
1935 /* Statement evaluation will handle type mismatches in constants
1936 more gracefully than the final propagation. This allows us to
1937 fold more conditionals here. */
1938 val = evaluate_stmt (stmt);
1939 if (val.lattice_val != CONSTANT
1940 || !val.mask.is_zero ())
1941 return false;
1943 if (dump_file)
1945 fprintf (dump_file, "Folding predicate ");
1946 print_gimple_expr (dump_file, stmt, 0, 0);
1947 fprintf (dump_file, " to ");
1948 print_generic_expr (dump_file, val.value, 0);
1949 fprintf (dump_file, "\n");
1952 if (integer_zerop (val.value))
1953 gimple_cond_make_false (stmt);
1954 else
1955 gimple_cond_make_true (stmt);
1957 return true;
1960 case GIMPLE_CALL:
1962 tree lhs = gimple_call_lhs (stmt);
1963 int flags = gimple_call_flags (stmt);
1964 tree val;
1965 tree argt;
1966 bool changed = false;
1967 unsigned i;
1969 /* If the call was folded into a constant make sure it goes
1970 away even if we cannot propagate into all uses because of
1971 type issues. */
1972 if (lhs
1973 && TREE_CODE (lhs) == SSA_NAME
1974 && (val = get_constant_value (lhs))
1975 /* Don't optimize away calls that have side-effects. */
1976 && (flags & (ECF_CONST|ECF_PURE)) != 0
1977 && (flags & ECF_LOOPING_CONST_OR_PURE) == 0)
1979 tree new_rhs = unshare_expr (val);
1980 bool res;
1981 if (!useless_type_conversion_p (TREE_TYPE (lhs),
1982 TREE_TYPE (new_rhs)))
1983 new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
1984 res = update_call_from_tree (gsi, new_rhs);
1985 gcc_assert (res);
1986 return true;
1989 /* Internal calls provide no argument types, so the extra laxity
1990 for normal calls does not apply. */
1991 if (gimple_call_internal_p (stmt))
1992 return false;
1994 /* The heuristic of fold_builtin_alloca_with_align differs before and
1995 after inlining, so we don't require the arg to be changed into a
1996 constant for folding, but just to be constant. */
1997 if (gimple_call_builtin_p (stmt, BUILT_IN_ALLOCA_WITH_ALIGN))
1999 tree new_rhs = fold_builtin_alloca_with_align (stmt);
2000 if (new_rhs)
2002 bool res = update_call_from_tree (gsi, new_rhs);
2003 tree var = TREE_OPERAND (TREE_OPERAND (new_rhs, 0),0);
2004 gcc_assert (res);
2005 insert_clobbers_for_var (*gsi, var);
2006 return true;
2010 /* Propagate into the call arguments. Compared to replace_uses_in
2011 this can use the argument slot types for type verification
2012 instead of the current argument type. We also can safely
2013 drop qualifiers here as we are dealing with constants anyway. */
2014 argt = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
2015 for (i = 0; i < gimple_call_num_args (stmt) && argt;
2016 ++i, argt = TREE_CHAIN (argt))
2018 tree arg = gimple_call_arg (stmt, i);
2019 if (TREE_CODE (arg) == SSA_NAME
2020 && (val = get_constant_value (arg))
2021 && useless_type_conversion_p
2022 (TYPE_MAIN_VARIANT (TREE_VALUE (argt)),
2023 TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2025 gimple_call_set_arg (stmt, i, unshare_expr (val));
2026 changed = true;
2030 return changed;
2033 case GIMPLE_ASSIGN:
2035 tree lhs = gimple_assign_lhs (stmt);
2036 tree val;
2038 /* If we have a load that turned out to be constant replace it
2039 as we cannot propagate into all uses in all cases. */
2040 if (gimple_assign_single_p (stmt)
2041 && TREE_CODE (lhs) == SSA_NAME
2042 && (val = get_constant_value (lhs)))
2044 tree rhs = unshare_expr (val);
2045 if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs)))
2046 rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (lhs), rhs);
2047 gimple_assign_set_rhs_from_tree (gsi, rhs);
2048 return true;
2051 return false;
2054 default:
2055 return false;
2059 /* Visit the assignment statement STMT. Set the value of its LHS to the
2060 value computed by the RHS and store LHS in *OUTPUT_P. If STMT
2061 creates virtual definitions, set the value of each new name to that
2062 of the RHS (if we can derive a constant out of the RHS).
2063 Value-returning call statements also perform an assignment, and
2064 are handled here. */
2066 static enum ssa_prop_result
2067 visit_assignment (gimple stmt, tree *output_p)
2069 prop_value_t val;
2070 enum ssa_prop_result retval;
2072 tree lhs = gimple_get_lhs (stmt);
2074 gcc_assert (gimple_code (stmt) != GIMPLE_CALL
2075 || gimple_call_lhs (stmt) != NULL_TREE);
2077 if (gimple_assign_single_p (stmt)
2078 && gimple_assign_rhs_code (stmt) == SSA_NAME)
2079 /* For a simple copy operation, we copy the lattice values. */
2080 val = *get_value (gimple_assign_rhs1 (stmt));
2081 else
2082 /* Evaluate the statement, which could be
2083 either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
2084 val = evaluate_stmt (stmt);
2086 retval = SSA_PROP_NOT_INTERESTING;
2088 /* Set the lattice value of the statement's output. */
2089 if (TREE_CODE (lhs) == SSA_NAME)
2091 /* If STMT is an assignment to an SSA_NAME, we only have one
2092 value to set. */
2093 if (set_lattice_value (lhs, val))
2095 *output_p = lhs;
2096 if (val.lattice_val == VARYING)
2097 retval = SSA_PROP_VARYING;
2098 else
2099 retval = SSA_PROP_INTERESTING;
2103 return retval;
2107 /* Visit the conditional statement STMT. Return SSA_PROP_INTERESTING
2108 if it can determine which edge will be taken. Otherwise, return
2109 SSA_PROP_VARYING. */
2111 static enum ssa_prop_result
2112 visit_cond_stmt (gimple stmt, edge *taken_edge_p)
2114 prop_value_t val;
2115 basic_block block;
2117 block = gimple_bb (stmt);
2118 val = evaluate_stmt (stmt);
2119 if (val.lattice_val != CONSTANT
2120 || !val.mask.is_zero ())
2121 return SSA_PROP_VARYING;
2123 /* Find which edge out of the conditional block will be taken and add it
2124 to the worklist. If no single edge can be determined statically,
2125 return SSA_PROP_VARYING to feed all the outgoing edges to the
2126 propagation engine. */
2127 *taken_edge_p = find_taken_edge (block, val.value);
2128 if (*taken_edge_p)
2129 return SSA_PROP_INTERESTING;
2130 else
2131 return SSA_PROP_VARYING;
2135 /* Evaluate statement STMT. If the statement produces an output value and
2136 its evaluation changes the lattice value of its output, return
2137 SSA_PROP_INTERESTING and set *OUTPUT_P to the SSA_NAME holding the
2138 output value.
2140 If STMT is a conditional branch and we can determine its truth
2141 value, set *TAKEN_EDGE_P accordingly. If STMT produces a varying
2142 value, return SSA_PROP_VARYING. */
2144 static enum ssa_prop_result
2145 ccp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
2147 tree def;
2148 ssa_op_iter iter;
2150 if (dump_file && (dump_flags & TDF_DETAILS))
2152 fprintf (dump_file, "\nVisiting statement:\n");
2153 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2156 switch (gimple_code (stmt))
2158 case GIMPLE_ASSIGN:
2159 /* If the statement is an assignment that produces a single
2160 output value, evaluate its RHS to see if the lattice value of
2161 its output has changed. */
2162 return visit_assignment (stmt, output_p);
2164 case GIMPLE_CALL:
2165 /* A value-returning call also performs an assignment. */
2166 if (gimple_call_lhs (stmt) != NULL_TREE)
2167 return visit_assignment (stmt, output_p);
2168 break;
2170 case GIMPLE_COND:
2171 case GIMPLE_SWITCH:
2172 /* If STMT is a conditional branch, see if we can determine
2173 which branch will be taken. */
2174 /* FIXME. It appears that we should be able to optimize
2175 computed GOTOs here as well. */
2176 return visit_cond_stmt (stmt, taken_edge_p);
2178 default:
2179 break;
2182 /* Any other kind of statement is not interesting for constant
2183 propagation and, therefore, not worth simulating. */
2184 if (dump_file && (dump_flags & TDF_DETAILS))
2185 fprintf (dump_file, "No interesting values produced. Marked VARYING.\n");
2187 /* Definitions made by statements other than assignments to
2188 SSA_NAMEs represent unknown modifications to their outputs.
2189 Mark them VARYING. */
2190 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
2192 prop_value_t v = { VARYING, NULL_TREE, { -1, (HOST_WIDE_INT) -1 } };
2193 set_lattice_value (def, v);
2196 return SSA_PROP_VARYING;
2200 /* Main entry point for SSA Conditional Constant Propagation. */
2202 static unsigned int
2203 do_ssa_ccp (void)
2205 unsigned int todo = 0;
2206 calculate_dominance_info (CDI_DOMINATORS);
2207 ccp_initialize ();
2208 ssa_propagate (ccp_visit_stmt, ccp_visit_phi_node);
2209 if (ccp_finalize ())
2210 todo = (TODO_cleanup_cfg | TODO_update_ssa);
2211 free_dominance_info (CDI_DOMINATORS);
2212 return todo;
2216 static bool
2217 gate_ccp (void)
2219 return flag_tree_ccp != 0;
2223 namespace {
2225 const pass_data pass_data_ccp =
2227 GIMPLE_PASS, /* type */
2228 "ccp", /* name */
2229 OPTGROUP_NONE, /* optinfo_flags */
2230 true, /* has_gate */
2231 true, /* has_execute */
2232 TV_TREE_CCP, /* tv_id */
2233 ( PROP_cfg | PROP_ssa ), /* properties_required */
2234 0, /* properties_provided */
2235 0, /* properties_destroyed */
2236 0, /* todo_flags_start */
2237 ( TODO_verify_ssa | TODO_update_address_taken
2238 | TODO_verify_stmts ), /* todo_flags_finish */
2241 class pass_ccp : public gimple_opt_pass
2243 public:
2244 pass_ccp (gcc::context *ctxt)
2245 : gimple_opt_pass (pass_data_ccp, ctxt)
2248 /* opt_pass methods: */
2249 opt_pass * clone () { return new pass_ccp (m_ctxt); }
2250 bool gate () { return gate_ccp (); }
2251 unsigned int execute () { return do_ssa_ccp (); }
2253 }; // class pass_ccp
2255 } // anon namespace
2257 gimple_opt_pass *
2258 make_pass_ccp (gcc::context *ctxt)
2260 return new pass_ccp (ctxt);
2265 /* Try to optimize out __builtin_stack_restore. Optimize it out
2266 if there is another __builtin_stack_restore in the same basic
2267 block and no calls or ASM_EXPRs are in between, or if this block's
2268 only outgoing edge is to EXIT_BLOCK and there are no calls or
2269 ASM_EXPRs after this __builtin_stack_restore. */
2271 static tree
2272 optimize_stack_restore (gimple_stmt_iterator i)
2274 tree callee;
2275 gimple stmt;
2277 basic_block bb = gsi_bb (i);
2278 gimple call = gsi_stmt (i);
2280 if (gimple_code (call) != GIMPLE_CALL
2281 || gimple_call_num_args (call) != 1
2282 || TREE_CODE (gimple_call_arg (call, 0)) != SSA_NAME
2283 || !POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))))
2284 return NULL_TREE;
2286 for (gsi_next (&i); !gsi_end_p (i); gsi_next (&i))
2288 stmt = gsi_stmt (i);
2289 if (gimple_code (stmt) == GIMPLE_ASM)
2290 return NULL_TREE;
2291 if (gimple_code (stmt) != GIMPLE_CALL)
2292 continue;
2294 callee = gimple_call_fndecl (stmt);
2295 if (!callee
2296 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2297 /* All regular builtins are ok, just obviously not alloca. */
2298 || DECL_FUNCTION_CODE (callee) == BUILT_IN_ALLOCA
2299 || DECL_FUNCTION_CODE (callee) == BUILT_IN_ALLOCA_WITH_ALIGN)
2300 return NULL_TREE;
2302 if (DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_RESTORE)
2303 goto second_stack_restore;
2306 if (!gsi_end_p (i))
2307 return NULL_TREE;
2309 /* Allow one successor of the exit block, or zero successors. */
2310 switch (EDGE_COUNT (bb->succs))
2312 case 0:
2313 break;
2314 case 1:
2315 if (single_succ_edge (bb)->dest != EXIT_BLOCK_PTR)
2316 return NULL_TREE;
2317 break;
2318 default:
2319 return NULL_TREE;
2321 second_stack_restore:
2323 /* If there's exactly one use, then zap the call to __builtin_stack_save.
2324 If there are multiple uses, then the last one should remove the call.
2325 In any case, whether the call to __builtin_stack_save can be removed
2326 or not is irrelevant to removing the call to __builtin_stack_restore. */
2327 if (has_single_use (gimple_call_arg (call, 0)))
2329 gimple stack_save = SSA_NAME_DEF_STMT (gimple_call_arg (call, 0));
2330 if (is_gimple_call (stack_save))
2332 callee = gimple_call_fndecl (stack_save);
2333 if (callee
2334 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
2335 && DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_SAVE)
2337 gimple_stmt_iterator stack_save_gsi;
2338 tree rhs;
2340 stack_save_gsi = gsi_for_stmt (stack_save);
2341 rhs = build_int_cst (TREE_TYPE (gimple_call_arg (call, 0)), 0);
2342 update_call_from_tree (&stack_save_gsi, rhs);
2347 /* No effect, so the statement will be deleted. */
2348 return integer_zero_node;
2351 /* If va_list type is a simple pointer and nothing special is needed,
2352 optimize __builtin_va_start (&ap, 0) into ap = __builtin_next_arg (0),
2353 __builtin_va_end (&ap) out as NOP and __builtin_va_copy into a simple
2354 pointer assignment. */
2356 static tree
2357 optimize_stdarg_builtin (gimple call)
2359 tree callee, lhs, rhs, cfun_va_list;
2360 bool va_list_simple_ptr;
2361 location_t loc = gimple_location (call);
2363 if (gimple_code (call) != GIMPLE_CALL)
2364 return NULL_TREE;
2366 callee = gimple_call_fndecl (call);
2368 cfun_va_list = targetm.fn_abi_va_list (callee);
2369 va_list_simple_ptr = POINTER_TYPE_P (cfun_va_list)
2370 && (TREE_TYPE (cfun_va_list) == void_type_node
2371 || TREE_TYPE (cfun_va_list) == char_type_node);
2373 switch (DECL_FUNCTION_CODE (callee))
2375 case BUILT_IN_VA_START:
2376 if (!va_list_simple_ptr
2377 || targetm.expand_builtin_va_start != NULL
2378 || !builtin_decl_explicit_p (BUILT_IN_NEXT_ARG))
2379 return NULL_TREE;
2381 if (gimple_call_num_args (call) != 2)
2382 return NULL_TREE;
2384 lhs = gimple_call_arg (call, 0);
2385 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
2386 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
2387 != TYPE_MAIN_VARIANT (cfun_va_list))
2388 return NULL_TREE;
2390 lhs = build_fold_indirect_ref_loc (loc, lhs);
2391 rhs = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_NEXT_ARG),
2392 1, integer_zero_node);
2393 rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
2394 return build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, rhs);
2396 case BUILT_IN_VA_COPY:
2397 if (!va_list_simple_ptr)
2398 return NULL_TREE;
2400 if (gimple_call_num_args (call) != 2)
2401 return NULL_TREE;
2403 lhs = gimple_call_arg (call, 0);
2404 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
2405 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
2406 != TYPE_MAIN_VARIANT (cfun_va_list))
2407 return NULL_TREE;
2409 lhs = build_fold_indirect_ref_loc (loc, lhs);
2410 rhs = gimple_call_arg (call, 1);
2411 if (TYPE_MAIN_VARIANT (TREE_TYPE (rhs))
2412 != TYPE_MAIN_VARIANT (cfun_va_list))
2413 return NULL_TREE;
2415 rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
2416 return build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, rhs);
2418 case BUILT_IN_VA_END:
2419 /* No effect, so the statement will be deleted. */
2420 return integer_zero_node;
2422 default:
2423 gcc_unreachable ();
2427 /* Attemp to make the block of __builtin_unreachable I unreachable by changing
2428 the incoming jumps. Return true if at least one jump was changed. */
2430 static bool
2431 optimize_unreachable (gimple_stmt_iterator i)
2433 basic_block bb = gsi_bb (i);
2434 gimple_stmt_iterator gsi;
2435 gimple stmt;
2436 edge_iterator ei;
2437 edge e;
2438 bool ret;
2440 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2442 stmt = gsi_stmt (gsi);
2444 if (is_gimple_debug (stmt))
2445 continue;
2447 if (gimple_code (stmt) == GIMPLE_LABEL)
2449 /* Verify we do not need to preserve the label. */
2450 if (FORCED_LABEL (gimple_label_label (stmt)))
2451 return false;
2453 continue;
2456 /* Only handle the case that __builtin_unreachable is the first statement
2457 in the block. We rely on DCE to remove stmts without side-effects
2458 before __builtin_unreachable. */
2459 if (gsi_stmt (gsi) != gsi_stmt (i))
2460 return false;
2463 ret = false;
2464 FOR_EACH_EDGE (e, ei, bb->preds)
2466 gsi = gsi_last_bb (e->src);
2467 if (gsi_end_p (gsi))
2468 continue;
2470 stmt = gsi_stmt (gsi);
2471 if (gimple_code (stmt) == GIMPLE_COND)
2473 if (e->flags & EDGE_TRUE_VALUE)
2474 gimple_cond_make_false (stmt);
2475 else if (e->flags & EDGE_FALSE_VALUE)
2476 gimple_cond_make_true (stmt);
2477 else
2478 gcc_unreachable ();
2479 update_stmt (stmt);
2481 else
2483 /* Todo: handle other cases, f.i. switch statement. */
2484 continue;
2487 ret = true;
2490 return ret;
2493 /* A simple pass that attempts to fold all builtin functions. This pass
2494 is run after we've propagated as many constants as we can. */
2496 static unsigned int
2497 execute_fold_all_builtins (void)
2499 bool cfg_changed = false;
2500 basic_block bb;
2501 unsigned int todoflags = 0;
2503 FOR_EACH_BB (bb)
2505 gimple_stmt_iterator i;
2506 for (i = gsi_start_bb (bb); !gsi_end_p (i); )
2508 gimple stmt, old_stmt;
2509 tree callee, result;
2510 enum built_in_function fcode;
2512 stmt = gsi_stmt (i);
2514 if (gimple_code (stmt) != GIMPLE_CALL)
2516 /* Remove all *ssaname_N ={v} {CLOBBER}; stmts,
2517 after the last GIMPLE DSE they aren't needed and might
2518 unnecessarily keep the SSA_NAMEs live. */
2519 if (gimple_clobber_p (stmt))
2521 tree lhs = gimple_assign_lhs (stmt);
2522 if (TREE_CODE (lhs) == MEM_REF
2523 && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME)
2525 unlink_stmt_vdef (stmt);
2526 gsi_remove (&i, true);
2527 release_defs (stmt);
2528 continue;
2531 gsi_next (&i);
2532 continue;
2534 callee = gimple_call_fndecl (stmt);
2535 if (!callee || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL)
2537 gsi_next (&i);
2538 continue;
2540 fcode = DECL_FUNCTION_CODE (callee);
2542 result = gimple_fold_builtin (stmt);
2544 if (result)
2545 gimple_remove_stmt_histograms (cfun, stmt);
2547 if (!result)
2548 switch (DECL_FUNCTION_CODE (callee))
2550 case BUILT_IN_CONSTANT_P:
2551 /* Resolve __builtin_constant_p. If it hasn't been
2552 folded to integer_one_node by now, it's fairly
2553 certain that the value simply isn't constant. */
2554 result = integer_zero_node;
2555 break;
2557 case BUILT_IN_ASSUME_ALIGNED:
2558 /* Remove __builtin_assume_aligned. */
2559 result = gimple_call_arg (stmt, 0);
2560 break;
2562 case BUILT_IN_STACK_RESTORE:
2563 result = optimize_stack_restore (i);
2564 if (result)
2565 break;
2566 gsi_next (&i);
2567 continue;
2569 case BUILT_IN_UNREACHABLE:
2570 if (optimize_unreachable (i))
2571 cfg_changed = true;
2572 break;
2574 case BUILT_IN_VA_START:
2575 case BUILT_IN_VA_END:
2576 case BUILT_IN_VA_COPY:
2577 /* These shouldn't be folded before pass_stdarg. */
2578 result = optimize_stdarg_builtin (stmt);
2579 if (result)
2580 break;
2581 /* FALLTHRU */
2583 default:
2584 gsi_next (&i);
2585 continue;
2588 if (result == NULL_TREE)
2589 break;
2591 if (dump_file && (dump_flags & TDF_DETAILS))
2593 fprintf (dump_file, "Simplified\n ");
2594 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2597 old_stmt = stmt;
2598 if (!update_call_from_tree (&i, result))
2600 gimplify_and_update_call_from_tree (&i, result);
2601 todoflags |= TODO_update_address_taken;
2604 stmt = gsi_stmt (i);
2605 update_stmt (stmt);
2607 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt)
2608 && gimple_purge_dead_eh_edges (bb))
2609 cfg_changed = true;
2611 if (dump_file && (dump_flags & TDF_DETAILS))
2613 fprintf (dump_file, "to\n ");
2614 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2615 fprintf (dump_file, "\n");
2618 /* Retry the same statement if it changed into another
2619 builtin, there might be new opportunities now. */
2620 if (gimple_code (stmt) != GIMPLE_CALL)
2622 gsi_next (&i);
2623 continue;
2625 callee = gimple_call_fndecl (stmt);
2626 if (!callee
2627 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2628 || DECL_FUNCTION_CODE (callee) == fcode)
2629 gsi_next (&i);
2633 /* Delete unreachable blocks. */
2634 if (cfg_changed)
2635 todoflags |= TODO_cleanup_cfg;
2637 return todoflags;
2641 namespace {
2643 const pass_data pass_data_fold_builtins =
2645 GIMPLE_PASS, /* type */
2646 "fab", /* name */
2647 OPTGROUP_NONE, /* optinfo_flags */
2648 false, /* has_gate */
2649 true, /* has_execute */
2650 TV_NONE, /* tv_id */
2651 ( PROP_cfg | PROP_ssa ), /* properties_required */
2652 0, /* properties_provided */
2653 0, /* properties_destroyed */
2654 0, /* todo_flags_start */
2655 ( TODO_verify_ssa | TODO_update_ssa ), /* todo_flags_finish */
2658 class pass_fold_builtins : public gimple_opt_pass
2660 public:
2661 pass_fold_builtins (gcc::context *ctxt)
2662 : gimple_opt_pass (pass_data_fold_builtins, ctxt)
2665 /* opt_pass methods: */
2666 opt_pass * clone () { return new pass_fold_builtins (m_ctxt); }
2667 unsigned int execute () { return execute_fold_all_builtins (); }
2669 }; // class pass_fold_builtins
2671 } // anon namespace
2673 gimple_opt_pass *
2674 make_pass_fold_builtins (gcc::context *ctxt)
2676 return new pass_fold_builtins (ctxt);