Use gimple_phi in many more places.
[official-gcc.git] / gcc / tree-ssa-ccp.c
blob114a0632b53d27ebf947beee4283f0d80a329a9c
1 /* Conditional constant propagation pass for the GNU compiler.
2 Copyright (C) 2000-2014 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 This algorithm uses wide-ints at the max precision of the target.
102 This means that, with one uninteresting exception, variables with
103 UNSIGNED types never go to VARYING because the bits above the
104 precision of the type of the variable are always zero. The
105 uninteresting case is a variable of UNSIGNED type that has the
106 maximum precision of the target. Such variables can go to VARYING,
107 but this causes no loss of infomation since these variables will
108 never be extended.
110 References:
112 Constant propagation with conditional branches,
113 Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
115 Building an Optimizing Compiler,
116 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
118 Advanced Compiler Design and Implementation,
119 Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
121 #include "config.h"
122 #include "system.h"
123 #include "coretypes.h"
124 #include "tm.h"
125 #include "tree.h"
126 #include "stor-layout.h"
127 #include "flags.h"
128 #include "tm_p.h"
129 #include "basic-block.h"
130 #include "function.h"
131 #include "gimple-pretty-print.h"
132 #include "hash-table.h"
133 #include "tree-ssa-alias.h"
134 #include "internal-fn.h"
135 #include "gimple-fold.h"
136 #include "tree-eh.h"
137 #include "gimple-expr.h"
138 #include "is-a.h"
139 #include "gimple.h"
140 #include "gimplify.h"
141 #include "gimple-iterator.h"
142 #include "gimple-ssa.h"
143 #include "tree-cfg.h"
144 #include "tree-phinodes.h"
145 #include "ssa-iterators.h"
146 #include "stringpool.h"
147 #include "tree-ssanames.h"
148 #include "tree-pass.h"
149 #include "tree-ssa-propagate.h"
150 #include "value-prof.h"
151 #include "langhooks.h"
152 #include "target.h"
153 #include "diagnostic-core.h"
154 #include "dbgcnt.h"
155 #include "params.h"
156 #include "wide-int-print.h"
157 #include "builtins.h"
160 /* Possible lattice values. */
161 typedef enum
163 UNINITIALIZED,
164 UNDEFINED,
165 CONSTANT,
166 VARYING
167 } ccp_lattice_t;
169 struct ccp_prop_value_t {
170 /* Lattice value. */
171 ccp_lattice_t lattice_val;
173 /* Propagated value. */
174 tree value;
176 /* Mask that applies to the propagated value during CCP. For X
177 with a CONSTANT lattice value X & ~mask == value & ~mask. The
178 zero bits in the mask cover constant values. The ones mean no
179 information. */
180 widest_int mask;
183 /* Array of propagated constant values. After propagation,
184 CONST_VAL[I].VALUE holds the constant value for SSA_NAME(I). If
185 the constant is held in an SSA name representing a memory store
186 (i.e., a VDEF), CONST_VAL[I].MEM_REF will contain the actual
187 memory reference used to store (i.e., the LHS of the assignment
188 doing the store). */
189 static ccp_prop_value_t *const_val;
190 static unsigned n_const_val;
192 static void canonicalize_value (ccp_prop_value_t *);
193 static bool ccp_fold_stmt (gimple_stmt_iterator *);
195 /* Dump constant propagation value VAL to file OUTF prefixed by PREFIX. */
197 static void
198 dump_lattice_value (FILE *outf, const char *prefix, ccp_prop_value_t val)
200 switch (val.lattice_val)
202 case UNINITIALIZED:
203 fprintf (outf, "%sUNINITIALIZED", prefix);
204 break;
205 case UNDEFINED:
206 fprintf (outf, "%sUNDEFINED", prefix);
207 break;
208 case VARYING:
209 fprintf (outf, "%sVARYING", prefix);
210 break;
211 case CONSTANT:
212 if (TREE_CODE (val.value) != INTEGER_CST
213 || val.mask == 0)
215 fprintf (outf, "%sCONSTANT ", prefix);
216 print_generic_expr (outf, val.value, dump_flags);
218 else
220 widest_int cval = wi::bit_and_not (wi::to_widest (val.value),
221 val.mask);
222 fprintf (outf, "%sCONSTANT ", prefix);
223 print_hex (cval, outf);
224 fprintf (outf, " (");
225 print_hex (val.mask, outf);
226 fprintf (outf, ")");
228 break;
229 default:
230 gcc_unreachable ();
235 /* Print lattice value VAL to stderr. */
237 void debug_lattice_value (ccp_prop_value_t val);
239 DEBUG_FUNCTION void
240 debug_lattice_value (ccp_prop_value_t val)
242 dump_lattice_value (stderr, "", val);
243 fprintf (stderr, "\n");
246 /* Extend NONZERO_BITS to a full mask, with the upper bits being set. */
248 static widest_int
249 extend_mask (const wide_int &nonzero_bits)
251 return (wi::mask <widest_int> (wi::get_precision (nonzero_bits), true)
252 | widest_int::from (nonzero_bits, UNSIGNED));
255 /* Compute a default value for variable VAR and store it in the
256 CONST_VAL array. The following rules are used to get default
257 values:
259 1- Global and static variables that are declared constant are
260 considered CONSTANT.
262 2- Any other value is considered UNDEFINED. This is useful when
263 considering PHI nodes. PHI arguments that are undefined do not
264 change the constant value of the PHI node, which allows for more
265 constants to be propagated.
267 3- Variables defined by statements other than assignments and PHI
268 nodes are considered VARYING.
270 4- Initial values of variables that are not GIMPLE registers are
271 considered VARYING. */
273 static ccp_prop_value_t
274 get_default_value (tree var)
276 ccp_prop_value_t val = { UNINITIALIZED, NULL_TREE, 0 };
277 gimple stmt;
279 stmt = SSA_NAME_DEF_STMT (var);
281 if (gimple_nop_p (stmt))
283 /* Variables defined by an empty statement are those used
284 before being initialized. If VAR is a local variable, we
285 can assume initially that it is UNDEFINED, otherwise we must
286 consider it VARYING. */
287 if (!virtual_operand_p (var)
288 && TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
289 val.lattice_val = UNDEFINED;
290 else
292 val.lattice_val = VARYING;
293 val.mask = -1;
294 if (flag_tree_bit_ccp)
296 wide_int nonzero_bits = get_nonzero_bits (var);
297 if (nonzero_bits != -1)
299 val.lattice_val = CONSTANT;
300 val.value = build_zero_cst (TREE_TYPE (var));
301 val.mask = extend_mask (nonzero_bits);
306 else if (is_gimple_assign (stmt))
308 tree cst;
309 if (gimple_assign_single_p (stmt)
310 && DECL_P (gimple_assign_rhs1 (stmt))
311 && (cst = get_symbol_constant_value (gimple_assign_rhs1 (stmt))))
313 val.lattice_val = CONSTANT;
314 val.value = cst;
316 else
318 /* Any other variable defined by an assignment is considered
319 UNDEFINED. */
320 val.lattice_val = UNDEFINED;
323 else if ((is_gimple_call (stmt)
324 && gimple_call_lhs (stmt) != NULL_TREE)
325 || gimple_code (stmt) == GIMPLE_PHI)
327 /* A variable defined by a call or a PHI node is considered
328 UNDEFINED. */
329 val.lattice_val = UNDEFINED;
331 else
333 /* Otherwise, VAR will never take on a constant value. */
334 val.lattice_val = VARYING;
335 val.mask = -1;
338 return val;
342 /* Get the constant value associated with variable VAR. */
344 static inline ccp_prop_value_t *
345 get_value (tree var)
347 ccp_prop_value_t *val;
349 if (const_val == NULL
350 || SSA_NAME_VERSION (var) >= n_const_val)
351 return NULL;
353 val = &const_val[SSA_NAME_VERSION (var)];
354 if (val->lattice_val == UNINITIALIZED)
355 *val = get_default_value (var);
357 canonicalize_value (val);
359 return val;
362 /* Return the constant tree value associated with VAR. */
364 static inline tree
365 get_constant_value (tree var)
367 ccp_prop_value_t *val;
368 if (TREE_CODE (var) != SSA_NAME)
370 if (is_gimple_min_invariant (var))
371 return var;
372 return NULL_TREE;
374 val = get_value (var);
375 if (val
376 && val->lattice_val == CONSTANT
377 && (TREE_CODE (val->value) != INTEGER_CST
378 || val->mask == 0))
379 return val->value;
380 return NULL_TREE;
383 /* Sets the value associated with VAR to VARYING. */
385 static inline void
386 set_value_varying (tree var)
388 ccp_prop_value_t *val = &const_val[SSA_NAME_VERSION (var)];
390 val->lattice_val = VARYING;
391 val->value = NULL_TREE;
392 val->mask = -1;
395 /* For float types, modify the value of VAL to make ccp work correctly
396 for non-standard values (-0, NaN):
398 If HONOR_SIGNED_ZEROS is false, and VAL = -0, we canonicalize it to 0.
399 If HONOR_NANS is false, and VAL is NaN, we canonicalize it to UNDEFINED.
400 This is to fix the following problem (see PR 29921): Suppose we have
402 x = 0.0 * y
404 and we set value of y to NaN. This causes value of x to be set to NaN.
405 When we later determine that y is in fact VARYING, fold uses the fact
406 that HONOR_NANS is false, and we try to change the value of x to 0,
407 causing an ICE. With HONOR_NANS being false, the real appearance of
408 NaN would cause undefined behavior, though, so claiming that y (and x)
409 are UNDEFINED initially is correct.
411 For other constants, make sure to drop TREE_OVERFLOW. */
413 static void
414 canonicalize_value (ccp_prop_value_t *val)
416 enum machine_mode mode;
417 tree type;
418 REAL_VALUE_TYPE d;
420 if (val->lattice_val != CONSTANT)
421 return;
423 if (TREE_OVERFLOW_P (val->value))
424 val->value = drop_tree_overflow (val->value);
426 if (TREE_CODE (val->value) != REAL_CST)
427 return;
429 d = TREE_REAL_CST (val->value);
430 type = TREE_TYPE (val->value);
431 mode = TYPE_MODE (type);
433 if (!HONOR_SIGNED_ZEROS (mode)
434 && REAL_VALUE_MINUS_ZERO (d))
436 val->value = build_real (type, dconst0);
437 return;
440 if (!HONOR_NANS (mode)
441 && REAL_VALUE_ISNAN (d))
443 val->lattice_val = UNDEFINED;
444 val->value = NULL;
445 return;
449 /* Return whether the lattice transition is valid. */
451 static bool
452 valid_lattice_transition (ccp_prop_value_t old_val, ccp_prop_value_t new_val)
454 /* Lattice transitions must always be monotonically increasing in
455 value. */
456 if (old_val.lattice_val < new_val.lattice_val)
457 return true;
459 if (old_val.lattice_val != new_val.lattice_val)
460 return false;
462 if (!old_val.value && !new_val.value)
463 return true;
465 /* Now both lattice values are CONSTANT. */
467 /* Allow transitioning from PHI <&x, not executable> == &x
468 to PHI <&x, &y> == common alignment. */
469 if (TREE_CODE (old_val.value) != INTEGER_CST
470 && TREE_CODE (new_val.value) == INTEGER_CST)
471 return true;
473 /* Bit-lattices have to agree in the still valid bits. */
474 if (TREE_CODE (old_val.value) == INTEGER_CST
475 && TREE_CODE (new_val.value) == INTEGER_CST)
476 return (wi::bit_and_not (wi::to_widest (old_val.value), new_val.mask)
477 == wi::bit_and_not (wi::to_widest (new_val.value), new_val.mask));
479 /* Otherwise constant values have to agree. */
480 return operand_equal_p (old_val.value, new_val.value, 0);
483 /* Set the value for variable VAR to NEW_VAL. Return true if the new
484 value is different from VAR's previous value. */
486 static bool
487 set_lattice_value (tree var, ccp_prop_value_t new_val)
489 /* We can deal with old UNINITIALIZED values just fine here. */
490 ccp_prop_value_t *old_val = &const_val[SSA_NAME_VERSION (var)];
492 canonicalize_value (&new_val);
494 /* We have to be careful to not go up the bitwise lattice
495 represented by the mask.
496 ??? This doesn't seem to be the best place to enforce this. */
497 if (new_val.lattice_val == CONSTANT
498 && old_val->lattice_val == CONSTANT
499 && TREE_CODE (new_val.value) == INTEGER_CST
500 && TREE_CODE (old_val->value) == INTEGER_CST)
502 widest_int diff = (wi::to_widest (new_val.value)
503 ^ wi::to_widest (old_val->value));
504 new_val.mask = new_val.mask | old_val->mask | diff;
507 gcc_assert (valid_lattice_transition (*old_val, new_val));
509 /* If *OLD_VAL and NEW_VAL are the same, return false to inform the
510 caller that this was a non-transition. */
511 if (old_val->lattice_val != new_val.lattice_val
512 || (new_val.lattice_val == CONSTANT
513 && TREE_CODE (new_val.value) == INTEGER_CST
514 && (TREE_CODE (old_val->value) != INTEGER_CST
515 || new_val.mask != old_val->mask)))
517 /* ??? We would like to delay creation of INTEGER_CSTs from
518 partially constants here. */
520 if (dump_file && (dump_flags & TDF_DETAILS))
522 dump_lattice_value (dump_file, "Lattice value changed to ", new_val);
523 fprintf (dump_file, ". Adding SSA edges to worklist.\n");
526 *old_val = new_val;
528 gcc_assert (new_val.lattice_val != UNINITIALIZED);
529 return true;
532 return false;
535 static ccp_prop_value_t get_value_for_expr (tree, bool);
536 static ccp_prop_value_t bit_value_binop (enum tree_code, tree, tree, tree);
537 static void bit_value_binop_1 (enum tree_code, tree, widest_int *, widest_int *,
538 tree, const widest_int &, const widest_int &,
539 tree, const widest_int &, const widest_int &);
541 /* Return a widest_int that can be used for bitwise simplifications
542 from VAL. */
544 static widest_int
545 value_to_wide_int (ccp_prop_value_t val)
547 if (val.value
548 && TREE_CODE (val.value) == INTEGER_CST)
549 return wi::to_widest (val.value);
551 return 0;
554 /* Return the value for the address expression EXPR based on alignment
555 information. */
557 static ccp_prop_value_t
558 get_value_from_alignment (tree expr)
560 tree type = TREE_TYPE (expr);
561 ccp_prop_value_t val;
562 unsigned HOST_WIDE_INT bitpos;
563 unsigned int align;
565 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
567 get_pointer_alignment_1 (expr, &align, &bitpos);
568 val.mask = (POINTER_TYPE_P (type) || TYPE_UNSIGNED (type)
569 ? wi::mask <widest_int> (TYPE_PRECISION (type), false)
570 : -1).and_not (align / BITS_PER_UNIT - 1);
571 val.lattice_val = val.mask == -1 ? VARYING : CONSTANT;
572 if (val.lattice_val == CONSTANT)
573 val.value = build_int_cstu (type, bitpos / BITS_PER_UNIT);
574 else
575 val.value = NULL_TREE;
577 return val;
580 /* Return the value for the tree operand EXPR. If FOR_BITS_P is true
581 return constant bits extracted from alignment information for
582 invariant addresses. */
584 static ccp_prop_value_t
585 get_value_for_expr (tree expr, bool for_bits_p)
587 ccp_prop_value_t val;
589 if (TREE_CODE (expr) == SSA_NAME)
591 val = *get_value (expr);
592 if (for_bits_p
593 && val.lattice_val == CONSTANT
594 && TREE_CODE (val.value) == ADDR_EXPR)
595 val = get_value_from_alignment (val.value);
597 else if (is_gimple_min_invariant (expr)
598 && (!for_bits_p || TREE_CODE (expr) != ADDR_EXPR))
600 val.lattice_val = CONSTANT;
601 val.value = expr;
602 val.mask = 0;
603 canonicalize_value (&val);
605 else if (TREE_CODE (expr) == ADDR_EXPR)
606 val = get_value_from_alignment (expr);
607 else
609 val.lattice_val = VARYING;
610 val.mask = -1;
611 val.value = NULL_TREE;
613 return val;
616 /* Return the likely CCP lattice value for STMT.
618 If STMT has no operands, then return CONSTANT.
620 Else if undefinedness of operands of STMT cause its value to be
621 undefined, then return UNDEFINED.
623 Else if any operands of STMT are constants, then return CONSTANT.
625 Else return VARYING. */
627 static ccp_lattice_t
628 likely_value (gimple stmt)
630 bool has_constant_operand, has_undefined_operand, all_undefined_operands;
631 tree use;
632 ssa_op_iter iter;
633 unsigned i;
635 enum gimple_code code = gimple_code (stmt);
637 /* This function appears to be called only for assignments, calls,
638 conditionals, and switches, due to the logic in visit_stmt. */
639 gcc_assert (code == GIMPLE_ASSIGN
640 || code == GIMPLE_CALL
641 || code == GIMPLE_COND
642 || code == GIMPLE_SWITCH);
644 /* If the statement has volatile operands, it won't fold to a
645 constant value. */
646 if (gimple_has_volatile_ops (stmt))
647 return VARYING;
649 /* Arrive here for more complex cases. */
650 has_constant_operand = false;
651 has_undefined_operand = false;
652 all_undefined_operands = true;
653 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
655 ccp_prop_value_t *val = get_value (use);
657 if (val->lattice_val == UNDEFINED)
658 has_undefined_operand = true;
659 else
660 all_undefined_operands = false;
662 if (val->lattice_val == CONSTANT)
663 has_constant_operand = true;
666 /* There may be constants in regular rhs operands. For calls we
667 have to ignore lhs, fndecl and static chain, otherwise only
668 the lhs. */
669 for (i = (is_gimple_call (stmt) ? 2 : 0) + gimple_has_lhs (stmt);
670 i < gimple_num_ops (stmt); ++i)
672 tree op = gimple_op (stmt, i);
673 if (!op || TREE_CODE (op) == SSA_NAME)
674 continue;
675 if (is_gimple_min_invariant (op))
676 has_constant_operand = true;
679 if (has_constant_operand)
680 all_undefined_operands = false;
682 if (has_undefined_operand
683 && code == GIMPLE_CALL
684 && gimple_call_internal_p (stmt))
685 switch (gimple_call_internal_fn (stmt))
687 /* These 3 builtins use the first argument just as a magic
688 way how to find out a decl uid. */
689 case IFN_GOMP_SIMD_LANE:
690 case IFN_GOMP_SIMD_VF:
691 case IFN_GOMP_SIMD_LAST_LANE:
692 has_undefined_operand = false;
693 break;
694 default:
695 break;
698 /* If the operation combines operands like COMPLEX_EXPR make sure to
699 not mark the result UNDEFINED if only one part of the result is
700 undefined. */
701 if (has_undefined_operand && all_undefined_operands)
702 return UNDEFINED;
703 else if (code == GIMPLE_ASSIGN && has_undefined_operand)
705 switch (gimple_assign_rhs_code (stmt))
707 /* Unary operators are handled with all_undefined_operands. */
708 case PLUS_EXPR:
709 case MINUS_EXPR:
710 case POINTER_PLUS_EXPR:
711 /* Not MIN_EXPR, MAX_EXPR. One VARYING operand may be selected.
712 Not bitwise operators, one VARYING operand may specify the
713 result completely. Not logical operators for the same reason.
714 Not COMPLEX_EXPR as one VARYING operand makes the result partly
715 not UNDEFINED. Not *DIV_EXPR, comparisons and shifts because
716 the undefined operand may be promoted. */
717 return UNDEFINED;
719 case ADDR_EXPR:
720 /* If any part of an address is UNDEFINED, like the index
721 of an ARRAY_EXPR, then treat the result as UNDEFINED. */
722 return UNDEFINED;
724 default:
728 /* If there was an UNDEFINED operand but the result may be not UNDEFINED
729 fall back to CONSTANT. During iteration UNDEFINED may still drop
730 to CONSTANT. */
731 if (has_undefined_operand)
732 return CONSTANT;
734 /* We do not consider virtual operands here -- load from read-only
735 memory may have only VARYING virtual operands, but still be
736 constant. */
737 if (has_constant_operand
738 || gimple_references_memory_p (stmt))
739 return CONSTANT;
741 return VARYING;
744 /* Returns true if STMT cannot be constant. */
746 static bool
747 surely_varying_stmt_p (gimple stmt)
749 /* If the statement has operands that we cannot handle, it cannot be
750 constant. */
751 if (gimple_has_volatile_ops (stmt))
752 return true;
754 /* If it is a call and does not return a value or is not a
755 builtin and not an indirect call or a call to function with
756 assume_aligned/alloc_align attribute, it is varying. */
757 if (is_gimple_call (stmt))
759 tree fndecl, fntype = gimple_call_fntype (stmt);
760 if (!gimple_call_lhs (stmt)
761 || ((fndecl = gimple_call_fndecl (stmt)) != NULL_TREE
762 && !DECL_BUILT_IN (fndecl)
763 && !lookup_attribute ("assume_aligned",
764 TYPE_ATTRIBUTES (fntype))
765 && !lookup_attribute ("alloc_align",
766 TYPE_ATTRIBUTES (fntype))))
767 return true;
770 /* Any other store operation is not interesting. */
771 else if (gimple_vdef (stmt))
772 return true;
774 /* Anything other than assignments and conditional jumps are not
775 interesting for CCP. */
776 if (gimple_code (stmt) != GIMPLE_ASSIGN
777 && gimple_code (stmt) != GIMPLE_COND
778 && gimple_code (stmt) != GIMPLE_SWITCH
779 && gimple_code (stmt) != GIMPLE_CALL)
780 return true;
782 return false;
785 /* Initialize local data structures for CCP. */
787 static void
788 ccp_initialize (void)
790 basic_block bb;
792 n_const_val = num_ssa_names;
793 const_val = XCNEWVEC (ccp_prop_value_t, n_const_val);
795 /* Initialize simulation flags for PHI nodes and statements. */
796 FOR_EACH_BB_FN (bb, cfun)
798 gimple_stmt_iterator i;
800 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
802 gimple stmt = gsi_stmt (i);
803 bool is_varying;
805 /* If the statement is a control insn, then we do not
806 want to avoid simulating the statement once. Failure
807 to do so means that those edges will never get added. */
808 if (stmt_ends_bb_p (stmt))
809 is_varying = false;
810 else
811 is_varying = surely_varying_stmt_p (stmt);
813 if (is_varying)
815 tree def;
816 ssa_op_iter iter;
818 /* If the statement will not produce a constant, mark
819 all its outputs VARYING. */
820 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
821 set_value_varying (def);
823 prop_set_simulate_again (stmt, !is_varying);
827 /* Now process PHI nodes. We never clear the simulate_again flag on
828 phi nodes, since we do not know which edges are executable yet,
829 except for phi nodes for virtual operands when we do not do store ccp. */
830 FOR_EACH_BB_FN (bb, cfun)
832 gimple_phi_iterator i;
834 for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
836 gimple_phi phi = i.phi ();
838 if (virtual_operand_p (gimple_phi_result (phi)))
839 prop_set_simulate_again (phi, false);
840 else
841 prop_set_simulate_again (phi, true);
846 /* Debug count support. Reset the values of ssa names
847 VARYING when the total number ssa names analyzed is
848 beyond the debug count specified. */
850 static void
851 do_dbg_cnt (void)
853 unsigned i;
854 for (i = 0; i < num_ssa_names; i++)
856 if (!dbg_cnt (ccp))
858 const_val[i].lattice_val = VARYING;
859 const_val[i].mask = -1;
860 const_val[i].value = NULL_TREE;
866 /* Do final substitution of propagated values, cleanup the flowgraph and
867 free allocated storage.
869 Return TRUE when something was optimized. */
871 static bool
872 ccp_finalize (void)
874 bool something_changed;
875 unsigned i;
877 do_dbg_cnt ();
879 /* Derive alignment and misalignment information from partially
880 constant pointers in the lattice or nonzero bits from partially
881 constant integers. */
882 for (i = 1; i < num_ssa_names; ++i)
884 tree name = ssa_name (i);
885 ccp_prop_value_t *val;
886 unsigned int tem, align;
888 if (!name
889 || (!POINTER_TYPE_P (TREE_TYPE (name))
890 && (!INTEGRAL_TYPE_P (TREE_TYPE (name))
891 /* Don't record nonzero bits before IPA to avoid
892 using too much memory. */
893 || first_pass_instance)))
894 continue;
896 val = get_value (name);
897 if (val->lattice_val != CONSTANT
898 || TREE_CODE (val->value) != INTEGER_CST)
899 continue;
901 if (POINTER_TYPE_P (TREE_TYPE (name)))
903 /* Trailing mask bits specify the alignment, trailing value
904 bits the misalignment. */
905 tem = val->mask.to_uhwi ();
906 align = (tem & -tem);
907 if (align > 1)
908 set_ptr_info_alignment (get_ptr_info (name), align,
909 (TREE_INT_CST_LOW (val->value)
910 & (align - 1)));
912 else
914 unsigned int precision = TYPE_PRECISION (TREE_TYPE (val->value));
915 wide_int nonzero_bits = wide_int::from (val->mask, precision,
916 UNSIGNED) | val->value;
917 nonzero_bits &= get_nonzero_bits (name);
918 set_nonzero_bits (name, nonzero_bits);
922 /* Perform substitutions based on the known constant values. */
923 something_changed = substitute_and_fold (get_constant_value,
924 ccp_fold_stmt, true);
926 free (const_val);
927 const_val = NULL;
928 return something_changed;;
932 /* Compute the meet operator between *VAL1 and *VAL2. Store the result
933 in VAL1.
935 any M UNDEFINED = any
936 any M VARYING = VARYING
937 Ci M Cj = Ci if (i == j)
938 Ci M Cj = VARYING if (i != j)
941 static void
942 ccp_lattice_meet (ccp_prop_value_t *val1, ccp_prop_value_t *val2)
944 if (val1->lattice_val == UNDEFINED)
946 /* UNDEFINED M any = any */
947 *val1 = *val2;
949 else if (val2->lattice_val == UNDEFINED)
951 /* any M UNDEFINED = any
952 Nothing to do. VAL1 already contains the value we want. */
955 else if (val1->lattice_val == VARYING
956 || val2->lattice_val == VARYING)
958 /* any M VARYING = VARYING. */
959 val1->lattice_val = VARYING;
960 val1->mask = -1;
961 val1->value = NULL_TREE;
963 else if (val1->lattice_val == CONSTANT
964 && val2->lattice_val == CONSTANT
965 && TREE_CODE (val1->value) == INTEGER_CST
966 && TREE_CODE (val2->value) == INTEGER_CST)
968 /* Ci M Cj = Ci if (i == j)
969 Ci M Cj = VARYING if (i != j)
971 For INTEGER_CSTs mask unequal bits. If no equal bits remain,
972 drop to varying. */
973 val1->mask = (val1->mask | val2->mask
974 | (wi::to_widest (val1->value)
975 ^ wi::to_widest (val2->value)));
976 if (val1->mask == -1)
978 val1->lattice_val = VARYING;
979 val1->value = NULL_TREE;
982 else if (val1->lattice_val == CONSTANT
983 && val2->lattice_val == CONSTANT
984 && simple_cst_equal (val1->value, val2->value) == 1)
986 /* Ci M Cj = Ci if (i == j)
987 Ci M Cj = VARYING if (i != j)
989 VAL1 already contains the value we want for equivalent values. */
991 else if (val1->lattice_val == CONSTANT
992 && val2->lattice_val == CONSTANT
993 && (TREE_CODE (val1->value) == ADDR_EXPR
994 || TREE_CODE (val2->value) == ADDR_EXPR))
996 /* When not equal addresses are involved try meeting for
997 alignment. */
998 ccp_prop_value_t tem = *val2;
999 if (TREE_CODE (val1->value) == ADDR_EXPR)
1000 *val1 = get_value_for_expr (val1->value, true);
1001 if (TREE_CODE (val2->value) == ADDR_EXPR)
1002 tem = get_value_for_expr (val2->value, true);
1003 ccp_lattice_meet (val1, &tem);
1005 else
1007 /* Any other combination is VARYING. */
1008 val1->lattice_val = VARYING;
1009 val1->mask = -1;
1010 val1->value = NULL_TREE;
1015 /* Loop through the PHI_NODE's parameters for BLOCK and compare their
1016 lattice values to determine PHI_NODE's lattice value. The value of a
1017 PHI node is determined calling ccp_lattice_meet with all the arguments
1018 of the PHI node that are incoming via executable edges. */
1020 static enum ssa_prop_result
1021 ccp_visit_phi_node (gimple_phi phi)
1023 unsigned i;
1024 ccp_prop_value_t *old_val, new_val;
1026 if (dump_file && (dump_flags & TDF_DETAILS))
1028 fprintf (dump_file, "\nVisiting PHI node: ");
1029 print_gimple_stmt (dump_file, phi, 0, dump_flags);
1032 old_val = get_value (gimple_phi_result (phi));
1033 switch (old_val->lattice_val)
1035 case VARYING:
1036 return SSA_PROP_VARYING;
1038 case CONSTANT:
1039 new_val = *old_val;
1040 break;
1042 case UNDEFINED:
1043 new_val.lattice_val = UNDEFINED;
1044 new_val.value = NULL_TREE;
1045 break;
1047 default:
1048 gcc_unreachable ();
1051 for (i = 0; i < gimple_phi_num_args (phi); i++)
1053 /* Compute the meet operator over all the PHI arguments flowing
1054 through executable edges. */
1055 edge e = gimple_phi_arg_edge (phi, i);
1057 if (dump_file && (dump_flags & TDF_DETAILS))
1059 fprintf (dump_file,
1060 "\n Argument #%d (%d -> %d %sexecutable)\n",
1061 i, e->src->index, e->dest->index,
1062 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
1065 /* If the incoming edge is executable, Compute the meet operator for
1066 the existing value of the PHI node and the current PHI argument. */
1067 if (e->flags & EDGE_EXECUTABLE)
1069 tree arg = gimple_phi_arg (phi, i)->def;
1070 ccp_prop_value_t arg_val = get_value_for_expr (arg, false);
1072 ccp_lattice_meet (&new_val, &arg_val);
1074 if (dump_file && (dump_flags & TDF_DETAILS))
1076 fprintf (dump_file, "\t");
1077 print_generic_expr (dump_file, arg, dump_flags);
1078 dump_lattice_value (dump_file, "\tValue: ", arg_val);
1079 fprintf (dump_file, "\n");
1082 if (new_val.lattice_val == VARYING)
1083 break;
1087 if (dump_file && (dump_flags & TDF_DETAILS))
1089 dump_lattice_value (dump_file, "\n PHI node value: ", new_val);
1090 fprintf (dump_file, "\n\n");
1093 /* Make the transition to the new value. */
1094 if (set_lattice_value (gimple_phi_result (phi), new_val))
1096 if (new_val.lattice_val == VARYING)
1097 return SSA_PROP_VARYING;
1098 else
1099 return SSA_PROP_INTERESTING;
1101 else
1102 return SSA_PROP_NOT_INTERESTING;
1105 /* Return the constant value for OP or OP otherwise. */
1107 static tree
1108 valueize_op (tree op)
1110 if (TREE_CODE (op) == SSA_NAME)
1112 tree tem = get_constant_value (op);
1113 if (tem)
1114 return tem;
1116 return op;
1119 /* CCP specific front-end to the non-destructive constant folding
1120 routines.
1122 Attempt to simplify the RHS of STMT knowing that one or more
1123 operands are constants.
1125 If simplification is possible, return the simplified RHS,
1126 otherwise return the original RHS or NULL_TREE. */
1128 static tree
1129 ccp_fold (gimple stmt)
1131 location_t loc = gimple_location (stmt);
1132 switch (gimple_code (stmt))
1134 case GIMPLE_COND:
1136 /* Handle comparison operators that can appear in GIMPLE form. */
1137 tree op0 = valueize_op (gimple_cond_lhs (stmt));
1138 tree op1 = valueize_op (gimple_cond_rhs (stmt));
1139 enum tree_code code = gimple_cond_code (stmt);
1140 return fold_binary_loc (loc, code, boolean_type_node, op0, op1);
1143 case GIMPLE_SWITCH:
1145 /* Return the constant switch index. */
1146 return valueize_op (gimple_switch_index (as_a <gimple_switch> (stmt)));
1149 case GIMPLE_ASSIGN:
1150 case GIMPLE_CALL:
1151 return gimple_fold_stmt_to_constant_1 (stmt, valueize_op);
1153 default:
1154 gcc_unreachable ();
1158 /* Apply the operation CODE in type TYPE to the value, mask pair
1159 RVAL and RMASK representing a value of type RTYPE and set
1160 the value, mask pair *VAL and *MASK to the result. */
1162 static void
1163 bit_value_unop_1 (enum tree_code code, tree type,
1164 widest_int *val, widest_int *mask,
1165 tree rtype, const widest_int &rval, const widest_int &rmask)
1167 switch (code)
1169 case BIT_NOT_EXPR:
1170 *mask = rmask;
1171 *val = ~rval;
1172 break;
1174 case NEGATE_EXPR:
1176 widest_int temv, temm;
1177 /* Return ~rval + 1. */
1178 bit_value_unop_1 (BIT_NOT_EXPR, type, &temv, &temm, type, rval, rmask);
1179 bit_value_binop_1 (PLUS_EXPR, type, val, mask,
1180 type, temv, temm, type, 1, 0);
1181 break;
1184 CASE_CONVERT:
1186 signop sgn;
1188 /* First extend mask and value according to the original type. */
1189 sgn = TYPE_SIGN (rtype);
1190 *mask = wi::ext (rmask, TYPE_PRECISION (rtype), sgn);
1191 *val = wi::ext (rval, TYPE_PRECISION (rtype), sgn);
1193 /* Then extend mask and value according to the target type. */
1194 sgn = TYPE_SIGN (type);
1195 *mask = wi::ext (*mask, TYPE_PRECISION (type), sgn);
1196 *val = wi::ext (*val, TYPE_PRECISION (type), sgn);
1197 break;
1200 default:
1201 *mask = -1;
1202 break;
1206 /* Apply the operation CODE in type TYPE to the value, mask pairs
1207 R1VAL, R1MASK and R2VAL, R2MASK representing a values of type R1TYPE
1208 and R2TYPE and set the value, mask pair *VAL and *MASK to the result. */
1210 static void
1211 bit_value_binop_1 (enum tree_code code, tree type,
1212 widest_int *val, widest_int *mask,
1213 tree r1type, const widest_int &r1val,
1214 const widest_int &r1mask, tree r2type,
1215 const widest_int &r2val, const widest_int &r2mask)
1217 signop sgn = TYPE_SIGN (type);
1218 int width = TYPE_PRECISION (type);
1219 bool swap_p = false;
1221 /* Assume we'll get a constant result. Use an initial non varying
1222 value, we fall back to varying in the end if necessary. */
1223 *mask = -1;
1225 switch (code)
1227 case BIT_AND_EXPR:
1228 /* The mask is constant where there is a known not
1229 set bit, (m1 | m2) & ((v1 | m1) & (v2 | m2)) */
1230 *mask = (r1mask | r2mask) & (r1val | r1mask) & (r2val | r2mask);
1231 *val = r1val & r2val;
1232 break;
1234 case BIT_IOR_EXPR:
1235 /* The mask is constant where there is a known
1236 set bit, (m1 | m2) & ~((v1 & ~m1) | (v2 & ~m2)). */
1237 *mask = (r1mask | r2mask)
1238 .and_not (r1val.and_not (r1mask) | r2val.and_not (r2mask));
1239 *val = r1val | r2val;
1240 break;
1242 case BIT_XOR_EXPR:
1243 /* m1 | m2 */
1244 *mask = r1mask | r2mask;
1245 *val = r1val ^ r2val;
1246 break;
1248 case LROTATE_EXPR:
1249 case RROTATE_EXPR:
1250 if (r2mask == 0)
1252 widest_int shift = r2val;
1253 if (shift == 0)
1255 *mask = r1mask;
1256 *val = r1val;
1258 else
1260 if (wi::neg_p (shift))
1262 shift = -shift;
1263 if (code == RROTATE_EXPR)
1264 code = LROTATE_EXPR;
1265 else
1266 code = RROTATE_EXPR;
1268 if (code == RROTATE_EXPR)
1270 *mask = wi::rrotate (r1mask, shift, width);
1271 *val = wi::rrotate (r1val, shift, width);
1273 else
1275 *mask = wi::lrotate (r1mask, shift, width);
1276 *val = wi::lrotate (r1val, shift, width);
1280 break;
1282 case LSHIFT_EXPR:
1283 case RSHIFT_EXPR:
1284 /* ??? We can handle partially known shift counts if we know
1285 its sign. That way we can tell that (x << (y | 8)) & 255
1286 is zero. */
1287 if (r2mask == 0)
1289 widest_int shift = r2val;
1290 if (shift == 0)
1292 *mask = r1mask;
1293 *val = r1val;
1295 else
1297 if (wi::neg_p (shift))
1299 shift = -shift;
1300 if (code == RSHIFT_EXPR)
1301 code = LSHIFT_EXPR;
1302 else
1303 code = RSHIFT_EXPR;
1305 if (code == RSHIFT_EXPR)
1307 *mask = wi::rshift (wi::ext (r1mask, width, sgn), shift, sgn);
1308 *val = wi::rshift (wi::ext (r1val, width, sgn), shift, sgn);
1310 else
1312 *mask = wi::ext (wi::lshift (r1mask, shift), width, sgn);
1313 *val = wi::ext (wi::lshift (r1val, shift), width, sgn);
1317 break;
1319 case PLUS_EXPR:
1320 case POINTER_PLUS_EXPR:
1322 /* Do the addition with unknown bits set to zero, to give carry-ins of
1323 zero wherever possible. */
1324 widest_int lo = r1val.and_not (r1mask) + r2val.and_not (r2mask);
1325 lo = wi::ext (lo, width, sgn);
1326 /* Do the addition with unknown bits set to one, to give carry-ins of
1327 one wherever possible. */
1328 widest_int hi = (r1val | r1mask) + (r2val | r2mask);
1329 hi = wi::ext (hi, width, sgn);
1330 /* Each bit in the result is known if (a) the corresponding bits in
1331 both inputs are known, and (b) the carry-in to that bit position
1332 is known. We can check condition (b) by seeing if we got the same
1333 result with minimised carries as with maximised carries. */
1334 *mask = r1mask | r2mask | (lo ^ hi);
1335 *mask = wi::ext (*mask, width, sgn);
1336 /* It shouldn't matter whether we choose lo or hi here. */
1337 *val = lo;
1338 break;
1341 case MINUS_EXPR:
1343 widest_int temv, temm;
1344 bit_value_unop_1 (NEGATE_EXPR, r2type, &temv, &temm,
1345 r2type, r2val, r2mask);
1346 bit_value_binop_1 (PLUS_EXPR, type, val, mask,
1347 r1type, r1val, r1mask,
1348 r2type, temv, temm);
1349 break;
1352 case MULT_EXPR:
1354 /* Just track trailing zeros in both operands and transfer
1355 them to the other. */
1356 int r1tz = wi::ctz (r1val | r1mask);
1357 int r2tz = wi::ctz (r2val | r2mask);
1358 if (r1tz + r2tz >= width)
1360 *mask = 0;
1361 *val = 0;
1363 else if (r1tz + r2tz > 0)
1365 *mask = wi::ext (wi::mask <widest_int> (r1tz + r2tz, true),
1366 width, sgn);
1367 *val = 0;
1369 break;
1372 case EQ_EXPR:
1373 case NE_EXPR:
1375 widest_int m = r1mask | r2mask;
1376 if (r1val.and_not (m) != r2val.and_not (m))
1378 *mask = 0;
1379 *val = ((code == EQ_EXPR) ? 0 : 1);
1381 else
1383 /* We know the result of a comparison is always one or zero. */
1384 *mask = 1;
1385 *val = 0;
1387 break;
1390 case GE_EXPR:
1391 case GT_EXPR:
1392 swap_p = true;
1393 code = swap_tree_comparison (code);
1394 /* Fall through. */
1395 case LT_EXPR:
1396 case LE_EXPR:
1398 int minmax, maxmin;
1400 const widest_int &o1val = swap_p ? r2val : r1val;
1401 const widest_int &o1mask = swap_p ? r2mask : r1mask;
1402 const widest_int &o2val = swap_p ? r1val : r2val;
1403 const widest_int &o2mask = swap_p ? r1mask : r2mask;
1405 /* If the most significant bits are not known we know nothing. */
1406 if (wi::neg_p (o1mask) || wi::neg_p (o2mask))
1407 break;
1409 /* For comparisons the signedness is in the comparison operands. */
1410 sgn = TYPE_SIGN (r1type);
1412 /* If we know the most significant bits we know the values
1413 value ranges by means of treating varying bits as zero
1414 or one. Do a cross comparison of the max/min pairs. */
1415 maxmin = wi::cmp (o1val | o1mask, o2val.and_not (o2mask), sgn);
1416 minmax = wi::cmp (o1val.and_not (o1mask), o2val | o2mask, sgn);
1417 if (maxmin < 0) /* o1 is less than o2. */
1419 *mask = 0;
1420 *val = 1;
1422 else if (minmax > 0) /* o1 is not less or equal to o2. */
1424 *mask = 0;
1425 *val = 0;
1427 else if (maxmin == minmax) /* o1 and o2 are equal. */
1429 /* This probably should never happen as we'd have
1430 folded the thing during fully constant value folding. */
1431 *mask = 0;
1432 *val = (code == LE_EXPR ? 1 : 0);
1434 else
1436 /* We know the result of a comparison is always one or zero. */
1437 *mask = 1;
1438 *val = 0;
1440 break;
1443 default:;
1447 /* Return the propagation value when applying the operation CODE to
1448 the value RHS yielding type TYPE. */
1450 static ccp_prop_value_t
1451 bit_value_unop (enum tree_code code, tree type, tree rhs)
1453 ccp_prop_value_t rval = get_value_for_expr (rhs, true);
1454 widest_int value, mask;
1455 ccp_prop_value_t val;
1457 if (rval.lattice_val == UNDEFINED)
1458 return rval;
1460 gcc_assert ((rval.lattice_val == CONSTANT
1461 && TREE_CODE (rval.value) == INTEGER_CST)
1462 || rval.mask == -1);
1463 bit_value_unop_1 (code, type, &value, &mask,
1464 TREE_TYPE (rhs), value_to_wide_int (rval), rval.mask);
1465 if (mask != -1)
1467 val.lattice_val = CONSTANT;
1468 val.mask = mask;
1469 /* ??? Delay building trees here. */
1470 val.value = wide_int_to_tree (type, value);
1472 else
1474 val.lattice_val = VARYING;
1475 val.value = NULL_TREE;
1476 val.mask = -1;
1478 return val;
1481 /* Return the propagation value when applying the operation CODE to
1482 the values RHS1 and RHS2 yielding type TYPE. */
1484 static ccp_prop_value_t
1485 bit_value_binop (enum tree_code code, tree type, tree rhs1, tree rhs2)
1487 ccp_prop_value_t r1val = get_value_for_expr (rhs1, true);
1488 ccp_prop_value_t r2val = get_value_for_expr (rhs2, true);
1489 widest_int value, mask;
1490 ccp_prop_value_t val;
1492 if (r1val.lattice_val == UNDEFINED
1493 || r2val.lattice_val == UNDEFINED)
1495 val.lattice_val = VARYING;
1496 val.value = NULL_TREE;
1497 val.mask = -1;
1498 return val;
1501 gcc_assert ((r1val.lattice_val == CONSTANT
1502 && TREE_CODE (r1val.value) == INTEGER_CST)
1503 || r1val.mask == -1);
1504 gcc_assert ((r2val.lattice_val == CONSTANT
1505 && TREE_CODE (r2val.value) == INTEGER_CST)
1506 || r2val.mask == -1);
1507 bit_value_binop_1 (code, type, &value, &mask,
1508 TREE_TYPE (rhs1), value_to_wide_int (r1val), r1val.mask,
1509 TREE_TYPE (rhs2), value_to_wide_int (r2val), r2val.mask);
1510 if (mask != -1)
1512 val.lattice_val = CONSTANT;
1513 val.mask = mask;
1514 /* ??? Delay building trees here. */
1515 val.value = wide_int_to_tree (type, value);
1517 else
1519 val.lattice_val = VARYING;
1520 val.value = NULL_TREE;
1521 val.mask = -1;
1523 return val;
1526 /* Return the propagation value for __builtin_assume_aligned
1527 and functions with assume_aligned or alloc_aligned attribute.
1528 For __builtin_assume_aligned, ATTR is NULL_TREE,
1529 for assume_aligned attribute ATTR is non-NULL and ALLOC_ALIGNED
1530 is false, for alloc_aligned attribute ATTR is non-NULL and
1531 ALLOC_ALIGNED is true. */
1533 static ccp_prop_value_t
1534 bit_value_assume_aligned (gimple stmt, tree attr, ccp_prop_value_t ptrval,
1535 bool alloc_aligned)
1537 tree align, misalign = NULL_TREE, type;
1538 unsigned HOST_WIDE_INT aligni, misaligni = 0;
1539 ccp_prop_value_t alignval;
1540 widest_int value, mask;
1541 ccp_prop_value_t val;
1543 if (attr == NULL_TREE)
1545 tree ptr = gimple_call_arg (stmt, 0);
1546 type = TREE_TYPE (ptr);
1547 ptrval = get_value_for_expr (ptr, true);
1549 else
1551 tree lhs = gimple_call_lhs (stmt);
1552 type = TREE_TYPE (lhs);
1555 if (ptrval.lattice_val == UNDEFINED)
1556 return ptrval;
1557 gcc_assert ((ptrval.lattice_val == CONSTANT
1558 && TREE_CODE (ptrval.value) == INTEGER_CST)
1559 || ptrval.mask == -1);
1560 if (attr == NULL_TREE)
1562 /* Get aligni and misaligni from __builtin_assume_aligned. */
1563 align = gimple_call_arg (stmt, 1);
1564 if (!tree_fits_uhwi_p (align))
1565 return ptrval;
1566 aligni = tree_to_uhwi (align);
1567 if (gimple_call_num_args (stmt) > 2)
1569 misalign = gimple_call_arg (stmt, 2);
1570 if (!tree_fits_uhwi_p (misalign))
1571 return ptrval;
1572 misaligni = tree_to_uhwi (misalign);
1575 else
1577 /* Get aligni and misaligni from assume_aligned or
1578 alloc_align attributes. */
1579 if (TREE_VALUE (attr) == NULL_TREE)
1580 return ptrval;
1581 attr = TREE_VALUE (attr);
1582 align = TREE_VALUE (attr);
1583 if (!tree_fits_uhwi_p (align))
1584 return ptrval;
1585 aligni = tree_to_uhwi (align);
1586 if (alloc_aligned)
1588 if (aligni == 0 || aligni > gimple_call_num_args (stmt))
1589 return ptrval;
1590 align = gimple_call_arg (stmt, aligni - 1);
1591 if (!tree_fits_uhwi_p (align))
1592 return ptrval;
1593 aligni = tree_to_uhwi (align);
1595 else if (TREE_CHAIN (attr) && TREE_VALUE (TREE_CHAIN (attr)))
1597 misalign = TREE_VALUE (TREE_CHAIN (attr));
1598 if (!tree_fits_uhwi_p (misalign))
1599 return ptrval;
1600 misaligni = tree_to_uhwi (misalign);
1603 if (aligni <= 1 || (aligni & (aligni - 1)) != 0 || misaligni >= aligni)
1604 return ptrval;
1606 align = build_int_cst_type (type, -aligni);
1607 alignval = get_value_for_expr (align, true);
1608 bit_value_binop_1 (BIT_AND_EXPR, type, &value, &mask,
1609 type, value_to_wide_int (ptrval), ptrval.mask,
1610 type, value_to_wide_int (alignval), alignval.mask);
1611 if (mask != -1)
1613 val.lattice_val = CONSTANT;
1614 val.mask = mask;
1615 gcc_assert ((mask.to_uhwi () & (aligni - 1)) == 0);
1616 gcc_assert ((value.to_uhwi () & (aligni - 1)) == 0);
1617 value |= misaligni;
1618 /* ??? Delay building trees here. */
1619 val.value = wide_int_to_tree (type, value);
1621 else
1623 val.lattice_val = VARYING;
1624 val.value = NULL_TREE;
1625 val.mask = -1;
1627 return val;
1630 /* Evaluate statement STMT.
1631 Valid only for assignments, calls, conditionals, and switches. */
1633 static ccp_prop_value_t
1634 evaluate_stmt (gimple stmt)
1636 ccp_prop_value_t val;
1637 tree simplified = NULL_TREE;
1638 ccp_lattice_t likelyvalue = likely_value (stmt);
1639 bool is_constant = false;
1640 unsigned int align;
1642 if (dump_file && (dump_flags & TDF_DETAILS))
1644 fprintf (dump_file, "which is likely ");
1645 switch (likelyvalue)
1647 case CONSTANT:
1648 fprintf (dump_file, "CONSTANT");
1649 break;
1650 case UNDEFINED:
1651 fprintf (dump_file, "UNDEFINED");
1652 break;
1653 case VARYING:
1654 fprintf (dump_file, "VARYING");
1655 break;
1656 default:;
1658 fprintf (dump_file, "\n");
1661 /* If the statement is likely to have a CONSTANT result, then try
1662 to fold the statement to determine the constant value. */
1663 /* FIXME. This is the only place that we call ccp_fold.
1664 Since likely_value never returns CONSTANT for calls, we will
1665 not attempt to fold them, including builtins that may profit. */
1666 if (likelyvalue == CONSTANT)
1668 fold_defer_overflow_warnings ();
1669 simplified = ccp_fold (stmt);
1670 is_constant = simplified && is_gimple_min_invariant (simplified);
1671 fold_undefer_overflow_warnings (is_constant, stmt, 0);
1672 if (is_constant)
1674 /* The statement produced a constant value. */
1675 val.lattice_val = CONSTANT;
1676 val.value = simplified;
1677 val.mask = 0;
1680 /* If the statement is likely to have a VARYING result, then do not
1681 bother folding the statement. */
1682 else if (likelyvalue == VARYING)
1684 enum gimple_code code = gimple_code (stmt);
1685 if (code == GIMPLE_ASSIGN)
1687 enum tree_code subcode = gimple_assign_rhs_code (stmt);
1689 /* Other cases cannot satisfy is_gimple_min_invariant
1690 without folding. */
1691 if (get_gimple_rhs_class (subcode) == GIMPLE_SINGLE_RHS)
1692 simplified = gimple_assign_rhs1 (stmt);
1694 else if (code == GIMPLE_SWITCH)
1695 simplified = gimple_switch_index (as_a <gimple_switch> (stmt));
1696 else
1697 /* These cannot satisfy is_gimple_min_invariant without folding. */
1698 gcc_assert (code == GIMPLE_CALL || code == GIMPLE_COND);
1699 is_constant = simplified && is_gimple_min_invariant (simplified);
1700 if (is_constant)
1702 /* The statement produced a constant value. */
1703 val.lattice_val = CONSTANT;
1704 val.value = simplified;
1705 val.mask = 0;
1709 /* Resort to simplification for bitwise tracking. */
1710 if (flag_tree_bit_ccp
1711 && (likelyvalue == CONSTANT || is_gimple_call (stmt))
1712 && !is_constant)
1714 enum gimple_code code = gimple_code (stmt);
1715 val.lattice_val = VARYING;
1716 val.value = NULL_TREE;
1717 val.mask = -1;
1718 if (code == GIMPLE_ASSIGN)
1720 enum tree_code subcode = gimple_assign_rhs_code (stmt);
1721 tree rhs1 = gimple_assign_rhs1 (stmt);
1722 switch (get_gimple_rhs_class (subcode))
1724 case GIMPLE_SINGLE_RHS:
1725 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1726 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1727 val = get_value_for_expr (rhs1, true);
1728 break;
1730 case GIMPLE_UNARY_RHS:
1731 if ((INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1732 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1733 && (INTEGRAL_TYPE_P (gimple_expr_type (stmt))
1734 || POINTER_TYPE_P (gimple_expr_type (stmt))))
1735 val = bit_value_unop (subcode, gimple_expr_type (stmt), rhs1);
1736 break;
1738 case GIMPLE_BINARY_RHS:
1739 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1740 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1742 tree lhs = gimple_assign_lhs (stmt);
1743 tree rhs2 = gimple_assign_rhs2 (stmt);
1744 val = bit_value_binop (subcode,
1745 TREE_TYPE (lhs), rhs1, rhs2);
1747 break;
1749 default:;
1752 else if (code == GIMPLE_COND)
1754 enum tree_code code = gimple_cond_code (stmt);
1755 tree rhs1 = gimple_cond_lhs (stmt);
1756 tree rhs2 = gimple_cond_rhs (stmt);
1757 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1758 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1759 val = bit_value_binop (code, TREE_TYPE (rhs1), rhs1, rhs2);
1761 else if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
1763 tree fndecl = gimple_call_fndecl (stmt);
1764 switch (DECL_FUNCTION_CODE (fndecl))
1766 case BUILT_IN_MALLOC:
1767 case BUILT_IN_REALLOC:
1768 case BUILT_IN_CALLOC:
1769 case BUILT_IN_STRDUP:
1770 case BUILT_IN_STRNDUP:
1771 val.lattice_val = CONSTANT;
1772 val.value = build_int_cst (TREE_TYPE (gimple_get_lhs (stmt)), 0);
1773 val.mask = ~((HOST_WIDE_INT) MALLOC_ABI_ALIGNMENT
1774 / BITS_PER_UNIT - 1);
1775 break;
1777 case BUILT_IN_ALLOCA:
1778 case BUILT_IN_ALLOCA_WITH_ALIGN:
1779 align = (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_ALLOCA_WITH_ALIGN
1780 ? TREE_INT_CST_LOW (gimple_call_arg (stmt, 1))
1781 : BIGGEST_ALIGNMENT);
1782 val.lattice_val = CONSTANT;
1783 val.value = build_int_cst (TREE_TYPE (gimple_get_lhs (stmt)), 0);
1784 val.mask = ~((HOST_WIDE_INT) align / BITS_PER_UNIT - 1);
1785 break;
1787 /* These builtins return their first argument, unmodified. */
1788 case BUILT_IN_MEMCPY:
1789 case BUILT_IN_MEMMOVE:
1790 case BUILT_IN_MEMSET:
1791 case BUILT_IN_STRCPY:
1792 case BUILT_IN_STRNCPY:
1793 case BUILT_IN_MEMCPY_CHK:
1794 case BUILT_IN_MEMMOVE_CHK:
1795 case BUILT_IN_MEMSET_CHK:
1796 case BUILT_IN_STRCPY_CHK:
1797 case BUILT_IN_STRNCPY_CHK:
1798 val = get_value_for_expr (gimple_call_arg (stmt, 0), true);
1799 break;
1801 case BUILT_IN_ASSUME_ALIGNED:
1802 val = bit_value_assume_aligned (stmt, NULL_TREE, val, false);
1803 break;
1805 case BUILT_IN_ALIGNED_ALLOC:
1807 tree align = get_constant_value (gimple_call_arg (stmt, 0));
1808 if (align
1809 && tree_fits_uhwi_p (align))
1811 unsigned HOST_WIDE_INT aligni = tree_to_uhwi (align);
1812 if (aligni > 1
1813 /* align must be power-of-two */
1814 && (aligni & (aligni - 1)) == 0)
1816 val.lattice_val = CONSTANT;
1817 val.value = build_int_cst (ptr_type_node, 0);
1818 val.mask = -aligni;
1821 break;
1824 default:;
1827 if (is_gimple_call (stmt) && gimple_call_lhs (stmt))
1829 tree fntype = gimple_call_fntype (stmt);
1830 if (fntype)
1832 tree attrs = lookup_attribute ("assume_aligned",
1833 TYPE_ATTRIBUTES (fntype));
1834 if (attrs)
1835 val = bit_value_assume_aligned (stmt, attrs, val, false);
1836 attrs = lookup_attribute ("alloc_align",
1837 TYPE_ATTRIBUTES (fntype));
1838 if (attrs)
1839 val = bit_value_assume_aligned (stmt, attrs, val, true);
1842 is_constant = (val.lattice_val == CONSTANT);
1845 if (flag_tree_bit_ccp
1846 && ((is_constant && TREE_CODE (val.value) == INTEGER_CST)
1847 || (!is_constant && likelyvalue != UNDEFINED))
1848 && gimple_get_lhs (stmt)
1849 && TREE_CODE (gimple_get_lhs (stmt)) == SSA_NAME)
1851 tree lhs = gimple_get_lhs (stmt);
1852 wide_int nonzero_bits = get_nonzero_bits (lhs);
1853 if (nonzero_bits != -1)
1855 if (!is_constant)
1857 val.lattice_val = CONSTANT;
1858 val.value = build_zero_cst (TREE_TYPE (lhs));
1859 val.mask = extend_mask (nonzero_bits);
1860 is_constant = true;
1862 else
1864 if (wi::bit_and_not (val.value, nonzero_bits) != 0)
1865 val.value = wide_int_to_tree (TREE_TYPE (lhs),
1866 nonzero_bits & val.value);
1867 if (nonzero_bits == 0)
1868 val.mask = 0;
1869 else
1870 val.mask = val.mask & extend_mask (nonzero_bits);
1875 if (!is_constant)
1877 /* The statement produced a nonconstant value. If the statement
1878 had UNDEFINED operands, then the result of the statement
1879 should be UNDEFINED. Otherwise, the statement is VARYING. */
1880 if (likelyvalue == UNDEFINED)
1882 val.lattice_val = likelyvalue;
1883 val.mask = 0;
1885 else
1887 val.lattice_val = VARYING;
1888 val.mask = -1;
1891 val.value = NULL_TREE;
1894 return val;
1897 typedef hash_table<pointer_hash<gimple_statement_base> > gimple_htab;
1899 /* Given a BUILT_IN_STACK_SAVE value SAVED_VAL, insert a clobber of VAR before
1900 each matching BUILT_IN_STACK_RESTORE. Mark visited phis in VISITED. */
1902 static void
1903 insert_clobber_before_stack_restore (tree saved_val, tree var,
1904 gimple_htab **visited)
1906 gimple stmt;
1907 gimple_assign clobber_stmt;
1908 tree clobber;
1909 imm_use_iterator iter;
1910 gimple_stmt_iterator i;
1911 gimple *slot;
1913 FOR_EACH_IMM_USE_STMT (stmt, iter, saved_val)
1914 if (gimple_call_builtin_p (stmt, BUILT_IN_STACK_RESTORE))
1916 clobber = build_constructor (TREE_TYPE (var),
1917 NULL);
1918 TREE_THIS_VOLATILE (clobber) = 1;
1919 clobber_stmt = gimple_build_assign (var, clobber);
1921 i = gsi_for_stmt (stmt);
1922 gsi_insert_before (&i, clobber_stmt, GSI_SAME_STMT);
1924 else if (gimple_code (stmt) == GIMPLE_PHI)
1926 if (!*visited)
1927 *visited = new gimple_htab (10);
1929 slot = (*visited)->find_slot (stmt, INSERT);
1930 if (*slot != NULL)
1931 continue;
1933 *slot = stmt;
1934 insert_clobber_before_stack_restore (gimple_phi_result (stmt), var,
1935 visited);
1937 else if (gimple_assign_ssa_name_copy_p (stmt))
1938 insert_clobber_before_stack_restore (gimple_assign_lhs (stmt), var,
1939 visited);
1940 else
1941 gcc_assert (is_gimple_debug (stmt));
1944 /* Advance the iterator to the previous non-debug gimple statement in the same
1945 or dominating basic block. */
1947 static inline void
1948 gsi_prev_dom_bb_nondebug (gimple_stmt_iterator *i)
1950 basic_block dom;
1952 gsi_prev_nondebug (i);
1953 while (gsi_end_p (*i))
1955 dom = get_immediate_dominator (CDI_DOMINATORS, i->bb);
1956 if (dom == NULL || dom == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1957 return;
1959 *i = gsi_last_bb (dom);
1963 /* Find a BUILT_IN_STACK_SAVE dominating gsi_stmt (I), and insert
1964 a clobber of VAR before each matching BUILT_IN_STACK_RESTORE.
1966 It is possible that BUILT_IN_STACK_SAVE cannot be find in a dominator when a
1967 previous pass (such as DOM) duplicated it along multiple paths to a BB. In
1968 that case the function gives up without inserting the clobbers. */
1970 static void
1971 insert_clobbers_for_var (gimple_stmt_iterator i, tree var)
1973 gimple stmt;
1974 tree saved_val;
1975 gimple_htab *visited = NULL;
1977 for (; !gsi_end_p (i); gsi_prev_dom_bb_nondebug (&i))
1979 stmt = gsi_stmt (i);
1981 if (!gimple_call_builtin_p (stmt, BUILT_IN_STACK_SAVE))
1982 continue;
1984 saved_val = gimple_call_lhs (stmt);
1985 if (saved_val == NULL_TREE)
1986 continue;
1988 insert_clobber_before_stack_restore (saved_val, var, &visited);
1989 break;
1992 delete visited;
1995 /* Detects a __builtin_alloca_with_align with constant size argument. Declares
1996 fixed-size array and returns the address, if found, otherwise returns
1997 NULL_TREE. */
1999 static tree
2000 fold_builtin_alloca_with_align (gimple stmt)
2002 unsigned HOST_WIDE_INT size, threshold, n_elem;
2003 tree lhs, arg, block, var, elem_type, array_type;
2005 /* Get lhs. */
2006 lhs = gimple_call_lhs (stmt);
2007 if (lhs == NULL_TREE)
2008 return NULL_TREE;
2010 /* Detect constant argument. */
2011 arg = get_constant_value (gimple_call_arg (stmt, 0));
2012 if (arg == NULL_TREE
2013 || TREE_CODE (arg) != INTEGER_CST
2014 || !tree_fits_uhwi_p (arg))
2015 return NULL_TREE;
2017 size = tree_to_uhwi (arg);
2019 /* Heuristic: don't fold large allocas. */
2020 threshold = (unsigned HOST_WIDE_INT)PARAM_VALUE (PARAM_LARGE_STACK_FRAME);
2021 /* In case the alloca is located at function entry, it has the same lifetime
2022 as a declared array, so we allow a larger size. */
2023 block = gimple_block (stmt);
2024 if (!(cfun->after_inlining
2025 && TREE_CODE (BLOCK_SUPERCONTEXT (block)) == FUNCTION_DECL))
2026 threshold /= 10;
2027 if (size > threshold)
2028 return NULL_TREE;
2030 /* Declare array. */
2031 elem_type = build_nonstandard_integer_type (BITS_PER_UNIT, 1);
2032 n_elem = size * 8 / BITS_PER_UNIT;
2033 array_type = build_array_type_nelts (elem_type, n_elem);
2034 var = create_tmp_var (array_type, NULL);
2035 DECL_ALIGN (var) = TREE_INT_CST_LOW (gimple_call_arg (stmt, 1));
2037 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (lhs);
2038 if (pi != NULL && !pi->pt.anything)
2040 bool singleton_p;
2041 unsigned uid;
2042 singleton_p = pt_solution_singleton_p (&pi->pt, &uid);
2043 gcc_assert (singleton_p);
2044 SET_DECL_PT_UID (var, uid);
2048 /* Fold alloca to the address of the array. */
2049 return fold_convert (TREE_TYPE (lhs), build_fold_addr_expr (var));
2052 /* Fold the stmt at *GSI with CCP specific information that propagating
2053 and regular folding does not catch. */
2055 static bool
2056 ccp_fold_stmt (gimple_stmt_iterator *gsi)
2058 gimple stmt = gsi_stmt (*gsi);
2060 switch (gimple_code (stmt))
2062 case GIMPLE_COND:
2064 gimple_cond cond_stmt = as_a <gimple_cond> (stmt);
2065 ccp_prop_value_t val;
2066 /* Statement evaluation will handle type mismatches in constants
2067 more gracefully than the final propagation. This allows us to
2068 fold more conditionals here. */
2069 val = evaluate_stmt (stmt);
2070 if (val.lattice_val != CONSTANT
2071 || val.mask != 0)
2072 return false;
2074 if (dump_file)
2076 fprintf (dump_file, "Folding predicate ");
2077 print_gimple_expr (dump_file, stmt, 0, 0);
2078 fprintf (dump_file, " to ");
2079 print_generic_expr (dump_file, val.value, 0);
2080 fprintf (dump_file, "\n");
2083 if (integer_zerop (val.value))
2084 gimple_cond_make_false (cond_stmt);
2085 else
2086 gimple_cond_make_true (cond_stmt);
2088 return true;
2091 case GIMPLE_CALL:
2093 tree lhs = gimple_call_lhs (stmt);
2094 int flags = gimple_call_flags (stmt);
2095 tree val;
2096 tree argt;
2097 bool changed = false;
2098 unsigned i;
2100 /* If the call was folded into a constant make sure it goes
2101 away even if we cannot propagate into all uses because of
2102 type issues. */
2103 if (lhs
2104 && TREE_CODE (lhs) == SSA_NAME
2105 && (val = get_constant_value (lhs))
2106 /* Don't optimize away calls that have side-effects. */
2107 && (flags & (ECF_CONST|ECF_PURE)) != 0
2108 && (flags & ECF_LOOPING_CONST_OR_PURE) == 0)
2110 tree new_rhs = unshare_expr (val);
2111 bool res;
2112 if (!useless_type_conversion_p (TREE_TYPE (lhs),
2113 TREE_TYPE (new_rhs)))
2114 new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
2115 res = update_call_from_tree (gsi, new_rhs);
2116 gcc_assert (res);
2117 return true;
2120 /* Internal calls provide no argument types, so the extra laxity
2121 for normal calls does not apply. */
2122 if (gimple_call_internal_p (stmt))
2123 return false;
2125 /* The heuristic of fold_builtin_alloca_with_align differs before and
2126 after inlining, so we don't require the arg to be changed into a
2127 constant for folding, but just to be constant. */
2128 if (gimple_call_builtin_p (stmt, BUILT_IN_ALLOCA_WITH_ALIGN))
2130 tree new_rhs = fold_builtin_alloca_with_align (stmt);
2131 if (new_rhs)
2133 bool res = update_call_from_tree (gsi, new_rhs);
2134 tree var = TREE_OPERAND (TREE_OPERAND (new_rhs, 0),0);
2135 gcc_assert (res);
2136 insert_clobbers_for_var (*gsi, var);
2137 return true;
2141 /* Propagate into the call arguments. Compared to replace_uses_in
2142 this can use the argument slot types for type verification
2143 instead of the current argument type. We also can safely
2144 drop qualifiers here as we are dealing with constants anyway. */
2145 argt = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
2146 for (i = 0; i < gimple_call_num_args (stmt) && argt;
2147 ++i, argt = TREE_CHAIN (argt))
2149 tree arg = gimple_call_arg (stmt, i);
2150 if (TREE_CODE (arg) == SSA_NAME
2151 && (val = get_constant_value (arg))
2152 && useless_type_conversion_p
2153 (TYPE_MAIN_VARIANT (TREE_VALUE (argt)),
2154 TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2156 gimple_call_set_arg (stmt, i, unshare_expr (val));
2157 changed = true;
2161 return changed;
2164 case GIMPLE_ASSIGN:
2166 tree lhs = gimple_assign_lhs (stmt);
2167 tree val;
2169 /* If we have a load that turned out to be constant replace it
2170 as we cannot propagate into all uses in all cases. */
2171 if (gimple_assign_single_p (stmt)
2172 && TREE_CODE (lhs) == SSA_NAME
2173 && (val = get_constant_value (lhs)))
2175 tree rhs = unshare_expr (val);
2176 if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs)))
2177 rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (lhs), rhs);
2178 gimple_assign_set_rhs_from_tree (gsi, rhs);
2179 return true;
2182 return false;
2185 default:
2186 return false;
2190 /* Visit the assignment statement STMT. Set the value of its LHS to the
2191 value computed by the RHS and store LHS in *OUTPUT_P. If STMT
2192 creates virtual definitions, set the value of each new name to that
2193 of the RHS (if we can derive a constant out of the RHS).
2194 Value-returning call statements also perform an assignment, and
2195 are handled here. */
2197 static enum ssa_prop_result
2198 visit_assignment (gimple stmt, tree *output_p)
2200 ccp_prop_value_t val;
2201 enum ssa_prop_result retval;
2203 tree lhs = gimple_get_lhs (stmt);
2205 gcc_assert (gimple_code (stmt) != GIMPLE_CALL
2206 || gimple_call_lhs (stmt) != NULL_TREE);
2208 if (gimple_assign_single_p (stmt)
2209 && gimple_assign_rhs_code (stmt) == SSA_NAME)
2210 /* For a simple copy operation, we copy the lattice values. */
2211 val = *get_value (gimple_assign_rhs1 (stmt));
2212 else
2213 /* Evaluate the statement, which could be
2214 either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
2215 val = evaluate_stmt (stmt);
2217 retval = SSA_PROP_NOT_INTERESTING;
2219 /* Set the lattice value of the statement's output. */
2220 if (TREE_CODE (lhs) == SSA_NAME)
2222 /* If STMT is an assignment to an SSA_NAME, we only have one
2223 value to set. */
2224 if (set_lattice_value (lhs, val))
2226 *output_p = lhs;
2227 if (val.lattice_val == VARYING)
2228 retval = SSA_PROP_VARYING;
2229 else
2230 retval = SSA_PROP_INTERESTING;
2234 return retval;
2238 /* Visit the conditional statement STMT. Return SSA_PROP_INTERESTING
2239 if it can determine which edge will be taken. Otherwise, return
2240 SSA_PROP_VARYING. */
2242 static enum ssa_prop_result
2243 visit_cond_stmt (gimple stmt, edge *taken_edge_p)
2245 ccp_prop_value_t val;
2246 basic_block block;
2248 block = gimple_bb (stmt);
2249 val = evaluate_stmt (stmt);
2250 if (val.lattice_val != CONSTANT
2251 || val.mask != 0)
2252 return SSA_PROP_VARYING;
2254 /* Find which edge out of the conditional block will be taken and add it
2255 to the worklist. If no single edge can be determined statically,
2256 return SSA_PROP_VARYING to feed all the outgoing edges to the
2257 propagation engine. */
2258 *taken_edge_p = find_taken_edge (block, val.value);
2259 if (*taken_edge_p)
2260 return SSA_PROP_INTERESTING;
2261 else
2262 return SSA_PROP_VARYING;
2266 /* Evaluate statement STMT. If the statement produces an output value and
2267 its evaluation changes the lattice value of its output, return
2268 SSA_PROP_INTERESTING and set *OUTPUT_P to the SSA_NAME holding the
2269 output value.
2271 If STMT is a conditional branch and we can determine its truth
2272 value, set *TAKEN_EDGE_P accordingly. If STMT produces a varying
2273 value, return SSA_PROP_VARYING. */
2275 static enum ssa_prop_result
2276 ccp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
2278 tree def;
2279 ssa_op_iter iter;
2281 if (dump_file && (dump_flags & TDF_DETAILS))
2283 fprintf (dump_file, "\nVisiting statement:\n");
2284 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2287 switch (gimple_code (stmt))
2289 case GIMPLE_ASSIGN:
2290 /* If the statement is an assignment that produces a single
2291 output value, evaluate its RHS to see if the lattice value of
2292 its output has changed. */
2293 return visit_assignment (stmt, output_p);
2295 case GIMPLE_CALL:
2296 /* A value-returning call also performs an assignment. */
2297 if (gimple_call_lhs (stmt) != NULL_TREE)
2298 return visit_assignment (stmt, output_p);
2299 break;
2301 case GIMPLE_COND:
2302 case GIMPLE_SWITCH:
2303 /* If STMT is a conditional branch, see if we can determine
2304 which branch will be taken. */
2305 /* FIXME. It appears that we should be able to optimize
2306 computed GOTOs here as well. */
2307 return visit_cond_stmt (stmt, taken_edge_p);
2309 default:
2310 break;
2313 /* Any other kind of statement is not interesting for constant
2314 propagation and, therefore, not worth simulating. */
2315 if (dump_file && (dump_flags & TDF_DETAILS))
2316 fprintf (dump_file, "No interesting values produced. Marked VARYING.\n");
2318 /* Definitions made by statements other than assignments to
2319 SSA_NAMEs represent unknown modifications to their outputs.
2320 Mark them VARYING. */
2321 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
2323 ccp_prop_value_t v = { VARYING, NULL_TREE, -1 };
2324 set_lattice_value (def, v);
2327 return SSA_PROP_VARYING;
2331 /* Main entry point for SSA Conditional Constant Propagation. */
2333 static unsigned int
2334 do_ssa_ccp (void)
2336 unsigned int todo = 0;
2337 calculate_dominance_info (CDI_DOMINATORS);
2338 ccp_initialize ();
2339 ssa_propagate (ccp_visit_stmt, ccp_visit_phi_node);
2340 if (ccp_finalize ())
2341 todo = (TODO_cleanup_cfg | TODO_update_ssa);
2342 free_dominance_info (CDI_DOMINATORS);
2343 return todo;
2347 namespace {
2349 const pass_data pass_data_ccp =
2351 GIMPLE_PASS, /* type */
2352 "ccp", /* name */
2353 OPTGROUP_NONE, /* optinfo_flags */
2354 TV_TREE_CCP, /* tv_id */
2355 ( PROP_cfg | PROP_ssa ), /* properties_required */
2356 0, /* properties_provided */
2357 0, /* properties_destroyed */
2358 0, /* todo_flags_start */
2359 TODO_update_address_taken, /* todo_flags_finish */
2362 class pass_ccp : public gimple_opt_pass
2364 public:
2365 pass_ccp (gcc::context *ctxt)
2366 : gimple_opt_pass (pass_data_ccp, ctxt)
2369 /* opt_pass methods: */
2370 opt_pass * clone () { return new pass_ccp (m_ctxt); }
2371 virtual bool gate (function *) { return flag_tree_ccp != 0; }
2372 virtual unsigned int execute (function *) { return do_ssa_ccp (); }
2374 }; // class pass_ccp
2376 } // anon namespace
2378 gimple_opt_pass *
2379 make_pass_ccp (gcc::context *ctxt)
2381 return new pass_ccp (ctxt);
2386 /* Try to optimize out __builtin_stack_restore. Optimize it out
2387 if there is another __builtin_stack_restore in the same basic
2388 block and no calls or ASM_EXPRs are in between, or if this block's
2389 only outgoing edge is to EXIT_BLOCK and there are no calls or
2390 ASM_EXPRs after this __builtin_stack_restore. */
2392 static tree
2393 optimize_stack_restore (gimple_stmt_iterator i)
2395 tree callee;
2396 gimple stmt;
2398 basic_block bb = gsi_bb (i);
2399 gimple call = gsi_stmt (i);
2401 if (gimple_code (call) != GIMPLE_CALL
2402 || gimple_call_num_args (call) != 1
2403 || TREE_CODE (gimple_call_arg (call, 0)) != SSA_NAME
2404 || !POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))))
2405 return NULL_TREE;
2407 for (gsi_next (&i); !gsi_end_p (i); gsi_next (&i))
2409 stmt = gsi_stmt (i);
2410 if (gimple_code (stmt) == GIMPLE_ASM)
2411 return NULL_TREE;
2412 if (gimple_code (stmt) != GIMPLE_CALL)
2413 continue;
2415 callee = gimple_call_fndecl (stmt);
2416 if (!callee
2417 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2418 /* All regular builtins are ok, just obviously not alloca. */
2419 || DECL_FUNCTION_CODE (callee) == BUILT_IN_ALLOCA
2420 || DECL_FUNCTION_CODE (callee) == BUILT_IN_ALLOCA_WITH_ALIGN)
2421 return NULL_TREE;
2423 if (DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_RESTORE)
2424 goto second_stack_restore;
2427 if (!gsi_end_p (i))
2428 return NULL_TREE;
2430 /* Allow one successor of the exit block, or zero successors. */
2431 switch (EDGE_COUNT (bb->succs))
2433 case 0:
2434 break;
2435 case 1:
2436 if (single_succ_edge (bb)->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
2437 return NULL_TREE;
2438 break;
2439 default:
2440 return NULL_TREE;
2442 second_stack_restore:
2444 /* If there's exactly one use, then zap the call to __builtin_stack_save.
2445 If there are multiple uses, then the last one should remove the call.
2446 In any case, whether the call to __builtin_stack_save can be removed
2447 or not is irrelevant to removing the call to __builtin_stack_restore. */
2448 if (has_single_use (gimple_call_arg (call, 0)))
2450 gimple stack_save = SSA_NAME_DEF_STMT (gimple_call_arg (call, 0));
2451 if (is_gimple_call (stack_save))
2453 callee = gimple_call_fndecl (stack_save);
2454 if (callee
2455 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
2456 && DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_SAVE)
2458 gimple_stmt_iterator stack_save_gsi;
2459 tree rhs;
2461 stack_save_gsi = gsi_for_stmt (stack_save);
2462 rhs = build_int_cst (TREE_TYPE (gimple_call_arg (call, 0)), 0);
2463 update_call_from_tree (&stack_save_gsi, rhs);
2468 /* No effect, so the statement will be deleted. */
2469 return integer_zero_node;
2472 /* If va_list type is a simple pointer and nothing special is needed,
2473 optimize __builtin_va_start (&ap, 0) into ap = __builtin_next_arg (0),
2474 __builtin_va_end (&ap) out as NOP and __builtin_va_copy into a simple
2475 pointer assignment. */
2477 static tree
2478 optimize_stdarg_builtin (gimple call)
2480 tree callee, lhs, rhs, cfun_va_list;
2481 bool va_list_simple_ptr;
2482 location_t loc = gimple_location (call);
2484 if (gimple_code (call) != GIMPLE_CALL)
2485 return NULL_TREE;
2487 callee = gimple_call_fndecl (call);
2489 cfun_va_list = targetm.fn_abi_va_list (callee);
2490 va_list_simple_ptr = POINTER_TYPE_P (cfun_va_list)
2491 && (TREE_TYPE (cfun_va_list) == void_type_node
2492 || TREE_TYPE (cfun_va_list) == char_type_node);
2494 switch (DECL_FUNCTION_CODE (callee))
2496 case BUILT_IN_VA_START:
2497 if (!va_list_simple_ptr
2498 || targetm.expand_builtin_va_start != NULL
2499 || !builtin_decl_explicit_p (BUILT_IN_NEXT_ARG))
2500 return NULL_TREE;
2502 if (gimple_call_num_args (call) != 2)
2503 return NULL_TREE;
2505 lhs = gimple_call_arg (call, 0);
2506 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
2507 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
2508 != TYPE_MAIN_VARIANT (cfun_va_list))
2509 return NULL_TREE;
2511 lhs = build_fold_indirect_ref_loc (loc, lhs);
2512 rhs = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_NEXT_ARG),
2513 1, integer_zero_node);
2514 rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
2515 return build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, rhs);
2517 case BUILT_IN_VA_COPY:
2518 if (!va_list_simple_ptr)
2519 return NULL_TREE;
2521 if (gimple_call_num_args (call) != 2)
2522 return NULL_TREE;
2524 lhs = gimple_call_arg (call, 0);
2525 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
2526 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
2527 != TYPE_MAIN_VARIANT (cfun_va_list))
2528 return NULL_TREE;
2530 lhs = build_fold_indirect_ref_loc (loc, lhs);
2531 rhs = gimple_call_arg (call, 1);
2532 if (TYPE_MAIN_VARIANT (TREE_TYPE (rhs))
2533 != TYPE_MAIN_VARIANT (cfun_va_list))
2534 return NULL_TREE;
2536 rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
2537 return build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, rhs);
2539 case BUILT_IN_VA_END:
2540 /* No effect, so the statement will be deleted. */
2541 return integer_zero_node;
2543 default:
2544 gcc_unreachable ();
2548 /* Attemp to make the block of __builtin_unreachable I unreachable by changing
2549 the incoming jumps. Return true if at least one jump was changed. */
2551 static bool
2552 optimize_unreachable (gimple_stmt_iterator i)
2554 basic_block bb = gsi_bb (i);
2555 gimple_stmt_iterator gsi;
2556 gimple stmt;
2557 edge_iterator ei;
2558 edge e;
2559 bool ret;
2561 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2563 stmt = gsi_stmt (gsi);
2565 if (is_gimple_debug (stmt))
2566 continue;
2568 if (gimple_label label_stmt = dyn_cast <gimple_label> (stmt))
2570 /* Verify we do not need to preserve the label. */
2571 if (FORCED_LABEL (gimple_label_label (label_stmt)))
2572 return false;
2574 continue;
2577 /* Only handle the case that __builtin_unreachable is the first statement
2578 in the block. We rely on DCE to remove stmts without side-effects
2579 before __builtin_unreachable. */
2580 if (gsi_stmt (gsi) != gsi_stmt (i))
2581 return false;
2584 ret = false;
2585 FOR_EACH_EDGE (e, ei, bb->preds)
2587 gsi = gsi_last_bb (e->src);
2588 if (gsi_end_p (gsi))
2589 continue;
2591 stmt = gsi_stmt (gsi);
2592 if (gimple_cond cond_stmt = dyn_cast <gimple_cond> (stmt))
2594 if (e->flags & EDGE_TRUE_VALUE)
2595 gimple_cond_make_false (cond_stmt);
2596 else if (e->flags & EDGE_FALSE_VALUE)
2597 gimple_cond_make_true (cond_stmt);
2598 else
2599 gcc_unreachable ();
2600 update_stmt (cond_stmt);
2602 else
2604 /* Todo: handle other cases, f.i. switch statement. */
2605 continue;
2608 ret = true;
2611 return ret;
2614 /* A simple pass that attempts to fold all builtin functions. This pass
2615 is run after we've propagated as many constants as we can. */
2617 namespace {
2619 const pass_data pass_data_fold_builtins =
2621 GIMPLE_PASS, /* type */
2622 "fab", /* name */
2623 OPTGROUP_NONE, /* optinfo_flags */
2624 TV_NONE, /* tv_id */
2625 ( PROP_cfg | PROP_ssa ), /* properties_required */
2626 0, /* properties_provided */
2627 0, /* properties_destroyed */
2628 0, /* todo_flags_start */
2629 TODO_update_ssa, /* todo_flags_finish */
2632 class pass_fold_builtins : public gimple_opt_pass
2634 public:
2635 pass_fold_builtins (gcc::context *ctxt)
2636 : gimple_opt_pass (pass_data_fold_builtins, ctxt)
2639 /* opt_pass methods: */
2640 opt_pass * clone () { return new pass_fold_builtins (m_ctxt); }
2641 virtual unsigned int execute (function *);
2643 }; // class pass_fold_builtins
2645 unsigned int
2646 pass_fold_builtins::execute (function *fun)
2648 bool cfg_changed = false;
2649 basic_block bb;
2650 unsigned int todoflags = 0;
2652 FOR_EACH_BB_FN (bb, fun)
2654 gimple_stmt_iterator i;
2655 for (i = gsi_start_bb (bb); !gsi_end_p (i); )
2657 gimple stmt, old_stmt;
2658 tree callee;
2659 enum built_in_function fcode;
2661 stmt = gsi_stmt (i);
2663 if (gimple_code (stmt) != GIMPLE_CALL)
2665 /* Remove all *ssaname_N ={v} {CLOBBER}; stmts,
2666 after the last GIMPLE DSE they aren't needed and might
2667 unnecessarily keep the SSA_NAMEs live. */
2668 if (gimple_clobber_p (stmt))
2670 tree lhs = gimple_assign_lhs (stmt);
2671 if (TREE_CODE (lhs) == MEM_REF
2672 && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME)
2674 unlink_stmt_vdef (stmt);
2675 gsi_remove (&i, true);
2676 release_defs (stmt);
2677 continue;
2680 gsi_next (&i);
2681 continue;
2684 callee = gimple_call_fndecl (stmt);
2685 if (!callee || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL)
2687 gsi_next (&i);
2688 continue;
2691 fcode = DECL_FUNCTION_CODE (callee);
2692 if (fold_stmt (&i))
2694 else
2696 tree result = NULL_TREE;
2697 switch (DECL_FUNCTION_CODE (callee))
2699 case BUILT_IN_CONSTANT_P:
2700 /* Resolve __builtin_constant_p. If it hasn't been
2701 folded to integer_one_node by now, it's fairly
2702 certain that the value simply isn't constant. */
2703 result = integer_zero_node;
2704 break;
2706 case BUILT_IN_ASSUME_ALIGNED:
2707 /* Remove __builtin_assume_aligned. */
2708 result = gimple_call_arg (stmt, 0);
2709 break;
2711 case BUILT_IN_STACK_RESTORE:
2712 result = optimize_stack_restore (i);
2713 if (result)
2714 break;
2715 gsi_next (&i);
2716 continue;
2718 case BUILT_IN_UNREACHABLE:
2719 if (optimize_unreachable (i))
2720 cfg_changed = true;
2721 break;
2723 case BUILT_IN_VA_START:
2724 case BUILT_IN_VA_END:
2725 case BUILT_IN_VA_COPY:
2726 /* These shouldn't be folded before pass_stdarg. */
2727 result = optimize_stdarg_builtin (stmt);
2728 if (result)
2729 break;
2730 /* FALLTHRU */
2732 default:;
2735 if (!result)
2737 gsi_next (&i);
2738 continue;
2741 if (!update_call_from_tree (&i, result))
2742 gimplify_and_update_call_from_tree (&i, result);
2745 todoflags |= TODO_update_address_taken;
2747 if (dump_file && (dump_flags & TDF_DETAILS))
2749 fprintf (dump_file, "Simplified\n ");
2750 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2753 old_stmt = stmt;
2754 stmt = gsi_stmt (i);
2755 update_stmt (stmt);
2757 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt)
2758 && gimple_purge_dead_eh_edges (bb))
2759 cfg_changed = true;
2761 if (dump_file && (dump_flags & TDF_DETAILS))
2763 fprintf (dump_file, "to\n ");
2764 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2765 fprintf (dump_file, "\n");
2768 /* Retry the same statement if it changed into another
2769 builtin, there might be new opportunities now. */
2770 if (gimple_code (stmt) != GIMPLE_CALL)
2772 gsi_next (&i);
2773 continue;
2775 callee = gimple_call_fndecl (stmt);
2776 if (!callee
2777 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2778 || DECL_FUNCTION_CODE (callee) == fcode)
2779 gsi_next (&i);
2783 /* Delete unreachable blocks. */
2784 if (cfg_changed)
2785 todoflags |= TODO_cleanup_cfg;
2787 return todoflags;
2790 } // anon namespace
2792 gimple_opt_pass *
2793 make_pass_fold_builtins (gcc::context *ctxt)
2795 return new pass_fold_builtins (ctxt);