Fix bootstrap/PR63632
[official-gcc.git] / gcc / tree-ssa-ccp.c
blob325a9f05a8aa2e72f36b40c27439db28e3ef32a0
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 "hashtab.h"
131 #include "hash-set.h"
132 #include "vec.h"
133 #include "machmode.h"
134 #include "hard-reg-set.h"
135 #include "input.h"
136 #include "function.h"
137 #include "gimple-pretty-print.h"
138 #include "hash-table.h"
139 #include "tree-ssa-alias.h"
140 #include "internal-fn.h"
141 #include "gimple-fold.h"
142 #include "tree-eh.h"
143 #include "gimple-expr.h"
144 #include "is-a.h"
145 #include "gimple.h"
146 #include "gimplify.h"
147 #include "gimple-iterator.h"
148 #include "gimple-ssa.h"
149 #include "tree-cfg.h"
150 #include "tree-phinodes.h"
151 #include "ssa-iterators.h"
152 #include "stringpool.h"
153 #include "tree-ssanames.h"
154 #include "tree-pass.h"
155 #include "tree-ssa-propagate.h"
156 #include "value-prof.h"
157 #include "langhooks.h"
158 #include "target.h"
159 #include "diagnostic-core.h"
160 #include "dbgcnt.h"
161 #include "params.h"
162 #include "wide-int-print.h"
163 #include "builtins.h"
166 /* Possible lattice values. */
167 typedef enum
169 UNINITIALIZED,
170 UNDEFINED,
171 CONSTANT,
172 VARYING
173 } ccp_lattice_t;
175 struct ccp_prop_value_t {
176 /* Lattice value. */
177 ccp_lattice_t lattice_val;
179 /* Propagated value. */
180 tree value;
182 /* Mask that applies to the propagated value during CCP. For X
183 with a CONSTANT lattice value X & ~mask == value & ~mask. The
184 zero bits in the mask cover constant values. The ones mean no
185 information. */
186 widest_int mask;
189 /* Array of propagated constant values. After propagation,
190 CONST_VAL[I].VALUE holds the constant value for SSA_NAME(I). If
191 the constant is held in an SSA name representing a memory store
192 (i.e., a VDEF), CONST_VAL[I].MEM_REF will contain the actual
193 memory reference used to store (i.e., the LHS of the assignment
194 doing the store). */
195 static ccp_prop_value_t *const_val;
196 static unsigned n_const_val;
198 static void canonicalize_value (ccp_prop_value_t *);
199 static bool ccp_fold_stmt (gimple_stmt_iterator *);
201 /* Dump constant propagation value VAL to file OUTF prefixed by PREFIX. */
203 static void
204 dump_lattice_value (FILE *outf, const char *prefix, ccp_prop_value_t val)
206 switch (val.lattice_val)
208 case UNINITIALIZED:
209 fprintf (outf, "%sUNINITIALIZED", prefix);
210 break;
211 case UNDEFINED:
212 fprintf (outf, "%sUNDEFINED", prefix);
213 break;
214 case VARYING:
215 fprintf (outf, "%sVARYING", prefix);
216 break;
217 case CONSTANT:
218 if (TREE_CODE (val.value) != INTEGER_CST
219 || val.mask == 0)
221 fprintf (outf, "%sCONSTANT ", prefix);
222 print_generic_expr (outf, val.value, dump_flags);
224 else
226 widest_int cval = wi::bit_and_not (wi::to_widest (val.value),
227 val.mask);
228 fprintf (outf, "%sCONSTANT ", prefix);
229 print_hex (cval, outf);
230 fprintf (outf, " (");
231 print_hex (val.mask, outf);
232 fprintf (outf, ")");
234 break;
235 default:
236 gcc_unreachable ();
241 /* Print lattice value VAL to stderr. */
243 void debug_lattice_value (ccp_prop_value_t val);
245 DEBUG_FUNCTION void
246 debug_lattice_value (ccp_prop_value_t val)
248 dump_lattice_value (stderr, "", val);
249 fprintf (stderr, "\n");
252 /* Extend NONZERO_BITS to a full mask, with the upper bits being set. */
254 static widest_int
255 extend_mask (const wide_int &nonzero_bits)
257 return (wi::mask <widest_int> (wi::get_precision (nonzero_bits), true)
258 | widest_int::from (nonzero_bits, UNSIGNED));
261 /* Compute a default value for variable VAR and store it in the
262 CONST_VAL array. The following rules are used to get default
263 values:
265 1- Global and static variables that are declared constant are
266 considered CONSTANT.
268 2- Any other value is considered UNDEFINED. This is useful when
269 considering PHI nodes. PHI arguments that are undefined do not
270 change the constant value of the PHI node, which allows for more
271 constants to be propagated.
273 3- Variables defined by statements other than assignments and PHI
274 nodes are considered VARYING.
276 4- Initial values of variables that are not GIMPLE registers are
277 considered VARYING. */
279 static ccp_prop_value_t
280 get_default_value (tree var)
282 ccp_prop_value_t val = { UNINITIALIZED, NULL_TREE, 0 };
283 gimple stmt;
285 stmt = SSA_NAME_DEF_STMT (var);
287 if (gimple_nop_p (stmt))
289 /* Variables defined by an empty statement are those used
290 before being initialized. If VAR is a local variable, we
291 can assume initially that it is UNDEFINED, otherwise we must
292 consider it VARYING. */
293 if (!virtual_operand_p (var)
294 && TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
295 val.lattice_val = UNDEFINED;
296 else
298 val.lattice_val = VARYING;
299 val.mask = -1;
300 if (flag_tree_bit_ccp)
302 wide_int nonzero_bits = get_nonzero_bits (var);
303 if (nonzero_bits != -1)
305 val.lattice_val = CONSTANT;
306 val.value = build_zero_cst (TREE_TYPE (var));
307 val.mask = extend_mask (nonzero_bits);
312 else if (is_gimple_assign (stmt))
314 tree cst;
315 if (gimple_assign_single_p (stmt)
316 && DECL_P (gimple_assign_rhs1 (stmt))
317 && (cst = get_symbol_constant_value (gimple_assign_rhs1 (stmt))))
319 val.lattice_val = CONSTANT;
320 val.value = cst;
322 else
324 /* Any other variable defined by an assignment is considered
325 UNDEFINED. */
326 val.lattice_val = UNDEFINED;
329 else if ((is_gimple_call (stmt)
330 && gimple_call_lhs (stmt) != NULL_TREE)
331 || gimple_code (stmt) == GIMPLE_PHI)
333 /* A variable defined by a call or a PHI node is considered
334 UNDEFINED. */
335 val.lattice_val = UNDEFINED;
337 else
339 /* Otherwise, VAR will never take on a constant value. */
340 val.lattice_val = VARYING;
341 val.mask = -1;
344 return val;
348 /* Get the constant value associated with variable VAR. */
350 static inline ccp_prop_value_t *
351 get_value (tree var)
353 ccp_prop_value_t *val;
355 if (const_val == NULL
356 || SSA_NAME_VERSION (var) >= n_const_val)
357 return NULL;
359 val = &const_val[SSA_NAME_VERSION (var)];
360 if (val->lattice_val == UNINITIALIZED)
361 *val = get_default_value (var);
363 canonicalize_value (val);
365 return val;
368 /* Return the constant tree value associated with VAR. */
370 static inline tree
371 get_constant_value (tree var)
373 ccp_prop_value_t *val;
374 if (TREE_CODE (var) != SSA_NAME)
376 if (is_gimple_min_invariant (var))
377 return var;
378 return NULL_TREE;
380 val = get_value (var);
381 if (val
382 && val->lattice_val == CONSTANT
383 && (TREE_CODE (val->value) != INTEGER_CST
384 || val->mask == 0))
385 return val->value;
386 return NULL_TREE;
389 /* Sets the value associated with VAR to VARYING. */
391 static inline void
392 set_value_varying (tree var)
394 ccp_prop_value_t *val = &const_val[SSA_NAME_VERSION (var)];
396 val->lattice_val = VARYING;
397 val->value = NULL_TREE;
398 val->mask = -1;
401 /* For float types, modify the value of VAL to make ccp work correctly
402 for non-standard values (-0, NaN):
404 If HONOR_SIGNED_ZEROS is false, and VAL = -0, we canonicalize it to 0.
405 If HONOR_NANS is false, and VAL is NaN, we canonicalize it to UNDEFINED.
406 This is to fix the following problem (see PR 29921): Suppose we have
408 x = 0.0 * y
410 and we set value of y to NaN. This causes value of x to be set to NaN.
411 When we later determine that y is in fact VARYING, fold uses the fact
412 that HONOR_NANS is false, and we try to change the value of x to 0,
413 causing an ICE. With HONOR_NANS being false, the real appearance of
414 NaN would cause undefined behavior, though, so claiming that y (and x)
415 are UNDEFINED initially is correct.
417 For other constants, make sure to drop TREE_OVERFLOW. */
419 static void
420 canonicalize_value (ccp_prop_value_t *val)
422 enum machine_mode mode;
423 tree type;
424 REAL_VALUE_TYPE d;
426 if (val->lattice_val != CONSTANT)
427 return;
429 if (TREE_OVERFLOW_P (val->value))
430 val->value = drop_tree_overflow (val->value);
432 if (TREE_CODE (val->value) != REAL_CST)
433 return;
435 d = TREE_REAL_CST (val->value);
436 type = TREE_TYPE (val->value);
437 mode = TYPE_MODE (type);
439 if (!HONOR_SIGNED_ZEROS (mode)
440 && REAL_VALUE_MINUS_ZERO (d))
442 val->value = build_real (type, dconst0);
443 return;
446 if (!HONOR_NANS (mode)
447 && REAL_VALUE_ISNAN (d))
449 val->lattice_val = UNDEFINED;
450 val->value = NULL;
451 return;
455 /* Return whether the lattice transition is valid. */
457 static bool
458 valid_lattice_transition (ccp_prop_value_t old_val, ccp_prop_value_t new_val)
460 /* Lattice transitions must always be monotonically increasing in
461 value. */
462 if (old_val.lattice_val < new_val.lattice_val)
463 return true;
465 if (old_val.lattice_val != new_val.lattice_val)
466 return false;
468 if (!old_val.value && !new_val.value)
469 return true;
471 /* Now both lattice values are CONSTANT. */
473 /* Allow transitioning from PHI <&x, not executable> == &x
474 to PHI <&x, &y> == common alignment. */
475 if (TREE_CODE (old_val.value) != INTEGER_CST
476 && TREE_CODE (new_val.value) == INTEGER_CST)
477 return true;
479 /* Bit-lattices have to agree in the still valid bits. */
480 if (TREE_CODE (old_val.value) == INTEGER_CST
481 && TREE_CODE (new_val.value) == INTEGER_CST)
482 return (wi::bit_and_not (wi::to_widest (old_val.value), new_val.mask)
483 == wi::bit_and_not (wi::to_widest (new_val.value), new_val.mask));
485 /* Otherwise constant values have to agree. */
486 return operand_equal_p (old_val.value, new_val.value, 0);
489 /* Set the value for variable VAR to NEW_VAL. Return true if the new
490 value is different from VAR's previous value. */
492 static bool
493 set_lattice_value (tree var, ccp_prop_value_t new_val)
495 /* We can deal with old UNINITIALIZED values just fine here. */
496 ccp_prop_value_t *old_val = &const_val[SSA_NAME_VERSION (var)];
498 canonicalize_value (&new_val);
500 /* We have to be careful to not go up the bitwise lattice
501 represented by the mask.
502 ??? This doesn't seem to be the best place to enforce this. */
503 if (new_val.lattice_val == CONSTANT
504 && old_val->lattice_val == CONSTANT
505 && TREE_CODE (new_val.value) == INTEGER_CST
506 && TREE_CODE (old_val->value) == INTEGER_CST)
508 widest_int diff = (wi::to_widest (new_val.value)
509 ^ wi::to_widest (old_val->value));
510 new_val.mask = new_val.mask | old_val->mask | diff;
513 gcc_assert (valid_lattice_transition (*old_val, new_val));
515 /* If *OLD_VAL and NEW_VAL are the same, return false to inform the
516 caller that this was a non-transition. */
517 if (old_val->lattice_val != new_val.lattice_val
518 || (new_val.lattice_val == CONSTANT
519 && TREE_CODE (new_val.value) == INTEGER_CST
520 && (TREE_CODE (old_val->value) != INTEGER_CST
521 || new_val.mask != old_val->mask)))
523 /* ??? We would like to delay creation of INTEGER_CSTs from
524 partially constants here. */
526 if (dump_file && (dump_flags & TDF_DETAILS))
528 dump_lattice_value (dump_file, "Lattice value changed to ", new_val);
529 fprintf (dump_file, ". Adding SSA edges to worklist.\n");
532 *old_val = new_val;
534 gcc_assert (new_val.lattice_val != UNINITIALIZED);
535 return true;
538 return false;
541 static ccp_prop_value_t get_value_for_expr (tree, bool);
542 static ccp_prop_value_t bit_value_binop (enum tree_code, tree, tree, tree);
543 static void bit_value_binop_1 (enum tree_code, tree, widest_int *, widest_int *,
544 tree, const widest_int &, const widest_int &,
545 tree, const widest_int &, const widest_int &);
547 /* Return a widest_int that can be used for bitwise simplifications
548 from VAL. */
550 static widest_int
551 value_to_wide_int (ccp_prop_value_t val)
553 if (val.value
554 && TREE_CODE (val.value) == INTEGER_CST)
555 return wi::to_widest (val.value);
557 return 0;
560 /* Return the value for the address expression EXPR based on alignment
561 information. */
563 static ccp_prop_value_t
564 get_value_from_alignment (tree expr)
566 tree type = TREE_TYPE (expr);
567 ccp_prop_value_t val;
568 unsigned HOST_WIDE_INT bitpos;
569 unsigned int align;
571 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
573 get_pointer_alignment_1 (expr, &align, &bitpos);
574 val.mask = (POINTER_TYPE_P (type) || TYPE_UNSIGNED (type)
575 ? wi::mask <widest_int> (TYPE_PRECISION (type), false)
576 : -1).and_not (align / BITS_PER_UNIT - 1);
577 val.lattice_val = val.mask == -1 ? VARYING : CONSTANT;
578 if (val.lattice_val == CONSTANT)
579 val.value = build_int_cstu (type, bitpos / BITS_PER_UNIT);
580 else
581 val.value = NULL_TREE;
583 return val;
586 /* Return the value for the tree operand EXPR. If FOR_BITS_P is true
587 return constant bits extracted from alignment information for
588 invariant addresses. */
590 static ccp_prop_value_t
591 get_value_for_expr (tree expr, bool for_bits_p)
593 ccp_prop_value_t val;
595 if (TREE_CODE (expr) == SSA_NAME)
597 val = *get_value (expr);
598 if (for_bits_p
599 && val.lattice_val == CONSTANT
600 && TREE_CODE (val.value) == ADDR_EXPR)
601 val = get_value_from_alignment (val.value);
603 else if (is_gimple_min_invariant (expr)
604 && (!for_bits_p || TREE_CODE (expr) != ADDR_EXPR))
606 val.lattice_val = CONSTANT;
607 val.value = expr;
608 val.mask = 0;
609 canonicalize_value (&val);
611 else if (TREE_CODE (expr) == ADDR_EXPR)
612 val = get_value_from_alignment (expr);
613 else
615 val.lattice_val = VARYING;
616 val.mask = -1;
617 val.value = NULL_TREE;
619 return val;
622 /* Return the likely CCP lattice value for STMT.
624 If STMT has no operands, then return CONSTANT.
626 Else if undefinedness of operands of STMT cause its value to be
627 undefined, then return UNDEFINED.
629 Else if any operands of STMT are constants, then return CONSTANT.
631 Else return VARYING. */
633 static ccp_lattice_t
634 likely_value (gimple stmt)
636 bool has_constant_operand, has_undefined_operand, all_undefined_operands;
637 tree use;
638 ssa_op_iter iter;
639 unsigned i;
641 enum gimple_code code = gimple_code (stmt);
643 /* This function appears to be called only for assignments, calls,
644 conditionals, and switches, due to the logic in visit_stmt. */
645 gcc_assert (code == GIMPLE_ASSIGN
646 || code == GIMPLE_CALL
647 || code == GIMPLE_COND
648 || code == GIMPLE_SWITCH);
650 /* If the statement has volatile operands, it won't fold to a
651 constant value. */
652 if (gimple_has_volatile_ops (stmt))
653 return VARYING;
655 /* Arrive here for more complex cases. */
656 has_constant_operand = false;
657 has_undefined_operand = false;
658 all_undefined_operands = true;
659 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
661 ccp_prop_value_t *val = get_value (use);
663 if (val->lattice_val == UNDEFINED)
664 has_undefined_operand = true;
665 else
666 all_undefined_operands = false;
668 if (val->lattice_val == CONSTANT)
669 has_constant_operand = true;
672 /* There may be constants in regular rhs operands. For calls we
673 have to ignore lhs, fndecl and static chain, otherwise only
674 the lhs. */
675 for (i = (is_gimple_call (stmt) ? 2 : 0) + gimple_has_lhs (stmt);
676 i < gimple_num_ops (stmt); ++i)
678 tree op = gimple_op (stmt, i);
679 if (!op || TREE_CODE (op) == SSA_NAME)
680 continue;
681 if (is_gimple_min_invariant (op))
682 has_constant_operand = true;
685 if (has_constant_operand)
686 all_undefined_operands = false;
688 if (has_undefined_operand
689 && code == GIMPLE_CALL
690 && gimple_call_internal_p (stmt))
691 switch (gimple_call_internal_fn (stmt))
693 /* These 3 builtins use the first argument just as a magic
694 way how to find out a decl uid. */
695 case IFN_GOMP_SIMD_LANE:
696 case IFN_GOMP_SIMD_VF:
697 case IFN_GOMP_SIMD_LAST_LANE:
698 has_undefined_operand = false;
699 break;
700 default:
701 break;
704 /* If the operation combines operands like COMPLEX_EXPR make sure to
705 not mark the result UNDEFINED if only one part of the result is
706 undefined. */
707 if (has_undefined_operand && all_undefined_operands)
708 return UNDEFINED;
709 else if (code == GIMPLE_ASSIGN && has_undefined_operand)
711 switch (gimple_assign_rhs_code (stmt))
713 /* Unary operators are handled with all_undefined_operands. */
714 case PLUS_EXPR:
715 case MINUS_EXPR:
716 case POINTER_PLUS_EXPR:
717 /* Not MIN_EXPR, MAX_EXPR. One VARYING operand may be selected.
718 Not bitwise operators, one VARYING operand may specify the
719 result completely. Not logical operators for the same reason.
720 Not COMPLEX_EXPR as one VARYING operand makes the result partly
721 not UNDEFINED. Not *DIV_EXPR, comparisons and shifts because
722 the undefined operand may be promoted. */
723 return UNDEFINED;
725 case ADDR_EXPR:
726 /* If any part of an address is UNDEFINED, like the index
727 of an ARRAY_EXPR, then treat the result as UNDEFINED. */
728 return UNDEFINED;
730 default:
734 /* If there was an UNDEFINED operand but the result may be not UNDEFINED
735 fall back to CONSTANT. During iteration UNDEFINED may still drop
736 to CONSTANT. */
737 if (has_undefined_operand)
738 return CONSTANT;
740 /* We do not consider virtual operands here -- load from read-only
741 memory may have only VARYING virtual operands, but still be
742 constant. */
743 if (has_constant_operand
744 || gimple_references_memory_p (stmt))
745 return CONSTANT;
747 return VARYING;
750 /* Returns true if STMT cannot be constant. */
752 static bool
753 surely_varying_stmt_p (gimple stmt)
755 /* If the statement has operands that we cannot handle, it cannot be
756 constant. */
757 if (gimple_has_volatile_ops (stmt))
758 return true;
760 /* If it is a call and does not return a value or is not a
761 builtin and not an indirect call or a call to function with
762 assume_aligned/alloc_align attribute, it is varying. */
763 if (is_gimple_call (stmt))
765 tree fndecl, fntype = gimple_call_fntype (stmt);
766 if (!gimple_call_lhs (stmt)
767 || ((fndecl = gimple_call_fndecl (stmt)) != NULL_TREE
768 && !DECL_BUILT_IN (fndecl)
769 && !lookup_attribute ("assume_aligned",
770 TYPE_ATTRIBUTES (fntype))
771 && !lookup_attribute ("alloc_align",
772 TYPE_ATTRIBUTES (fntype))))
773 return true;
776 /* Any other store operation is not interesting. */
777 else if (gimple_vdef (stmt))
778 return true;
780 /* Anything other than assignments and conditional jumps are not
781 interesting for CCP. */
782 if (gimple_code (stmt) != GIMPLE_ASSIGN
783 && gimple_code (stmt) != GIMPLE_COND
784 && gimple_code (stmt) != GIMPLE_SWITCH
785 && gimple_code (stmt) != GIMPLE_CALL)
786 return true;
788 return false;
791 /* Initialize local data structures for CCP. */
793 static void
794 ccp_initialize (void)
796 basic_block bb;
798 n_const_val = num_ssa_names;
799 const_val = XCNEWVEC (ccp_prop_value_t, n_const_val);
801 /* Initialize simulation flags for PHI nodes and statements. */
802 FOR_EACH_BB_FN (bb, cfun)
804 gimple_stmt_iterator i;
806 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
808 gimple stmt = gsi_stmt (i);
809 bool is_varying;
811 /* If the statement is a control insn, then we do not
812 want to avoid simulating the statement once. Failure
813 to do so means that those edges will never get added. */
814 if (stmt_ends_bb_p (stmt))
815 is_varying = false;
816 else
817 is_varying = surely_varying_stmt_p (stmt);
819 if (is_varying)
821 tree def;
822 ssa_op_iter iter;
824 /* If the statement will not produce a constant, mark
825 all its outputs VARYING. */
826 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
827 set_value_varying (def);
829 prop_set_simulate_again (stmt, !is_varying);
833 /* Now process PHI nodes. We never clear the simulate_again flag on
834 phi nodes, since we do not know which edges are executable yet,
835 except for phi nodes for virtual operands when we do not do store ccp. */
836 FOR_EACH_BB_FN (bb, cfun)
838 gimple_stmt_iterator i;
840 for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
842 gimple phi = gsi_stmt (i);
844 if (virtual_operand_p (gimple_phi_result (phi)))
845 prop_set_simulate_again (phi, false);
846 else
847 prop_set_simulate_again (phi, true);
852 /* Debug count support. Reset the values of ssa names
853 VARYING when the total number ssa names analyzed is
854 beyond the debug count specified. */
856 static void
857 do_dbg_cnt (void)
859 unsigned i;
860 for (i = 0; i < num_ssa_names; i++)
862 if (!dbg_cnt (ccp))
864 const_val[i].lattice_val = VARYING;
865 const_val[i].mask = -1;
866 const_val[i].value = NULL_TREE;
872 /* Do final substitution of propagated values, cleanup the flowgraph and
873 free allocated storage.
875 Return TRUE when something was optimized. */
877 static bool
878 ccp_finalize (void)
880 bool something_changed;
881 unsigned i;
883 do_dbg_cnt ();
885 /* Derive alignment and misalignment information from partially
886 constant pointers in the lattice or nonzero bits from partially
887 constant integers. */
888 for (i = 1; i < num_ssa_names; ++i)
890 tree name = ssa_name (i);
891 ccp_prop_value_t *val;
892 unsigned int tem, align;
894 if (!name
895 || (!POINTER_TYPE_P (TREE_TYPE (name))
896 && (!INTEGRAL_TYPE_P (TREE_TYPE (name))
897 /* Don't record nonzero bits before IPA to avoid
898 using too much memory. */
899 || first_pass_instance)))
900 continue;
902 val = get_value (name);
903 if (val->lattice_val != CONSTANT
904 || TREE_CODE (val->value) != INTEGER_CST)
905 continue;
907 if (POINTER_TYPE_P (TREE_TYPE (name)))
909 /* Trailing mask bits specify the alignment, trailing value
910 bits the misalignment. */
911 tem = val->mask.to_uhwi ();
912 align = (tem & -tem);
913 if (align > 1)
914 set_ptr_info_alignment (get_ptr_info (name), align,
915 (TREE_INT_CST_LOW (val->value)
916 & (align - 1)));
918 else
920 unsigned int precision = TYPE_PRECISION (TREE_TYPE (val->value));
921 wide_int nonzero_bits = wide_int::from (val->mask, precision,
922 UNSIGNED) | val->value;
923 nonzero_bits &= get_nonzero_bits (name);
924 set_nonzero_bits (name, nonzero_bits);
928 /* Perform substitutions based on the known constant values. */
929 something_changed = substitute_and_fold (get_constant_value,
930 ccp_fold_stmt, true);
932 free (const_val);
933 const_val = NULL;
934 return something_changed;;
938 /* Compute the meet operator between *VAL1 and *VAL2. Store the result
939 in VAL1.
941 any M UNDEFINED = any
942 any M VARYING = VARYING
943 Ci M Cj = Ci if (i == j)
944 Ci M Cj = VARYING if (i != j)
947 static void
948 ccp_lattice_meet (ccp_prop_value_t *val1, ccp_prop_value_t *val2)
950 if (val1->lattice_val == UNDEFINED)
952 /* UNDEFINED M any = any */
953 *val1 = *val2;
955 else if (val2->lattice_val == UNDEFINED)
957 /* any M UNDEFINED = any
958 Nothing to do. VAL1 already contains the value we want. */
961 else if (val1->lattice_val == VARYING
962 || val2->lattice_val == VARYING)
964 /* any M VARYING = VARYING. */
965 val1->lattice_val = VARYING;
966 val1->mask = -1;
967 val1->value = NULL_TREE;
969 else if (val1->lattice_val == CONSTANT
970 && val2->lattice_val == CONSTANT
971 && TREE_CODE (val1->value) == INTEGER_CST
972 && TREE_CODE (val2->value) == INTEGER_CST)
974 /* Ci M Cj = Ci if (i == j)
975 Ci M Cj = VARYING if (i != j)
977 For INTEGER_CSTs mask unequal bits. If no equal bits remain,
978 drop to varying. */
979 val1->mask = (val1->mask | val2->mask
980 | (wi::to_widest (val1->value)
981 ^ wi::to_widest (val2->value)));
982 if (val1->mask == -1)
984 val1->lattice_val = VARYING;
985 val1->value = NULL_TREE;
988 else if (val1->lattice_val == CONSTANT
989 && val2->lattice_val == CONSTANT
990 && simple_cst_equal (val1->value, val2->value) == 1)
992 /* Ci M Cj = Ci if (i == j)
993 Ci M Cj = VARYING if (i != j)
995 VAL1 already contains the value we want for equivalent values. */
997 else if (val1->lattice_val == CONSTANT
998 && val2->lattice_val == CONSTANT
999 && (TREE_CODE (val1->value) == ADDR_EXPR
1000 || TREE_CODE (val2->value) == ADDR_EXPR))
1002 /* When not equal addresses are involved try meeting for
1003 alignment. */
1004 ccp_prop_value_t tem = *val2;
1005 if (TREE_CODE (val1->value) == ADDR_EXPR)
1006 *val1 = get_value_for_expr (val1->value, true);
1007 if (TREE_CODE (val2->value) == ADDR_EXPR)
1008 tem = get_value_for_expr (val2->value, true);
1009 ccp_lattice_meet (val1, &tem);
1011 else
1013 /* Any other combination is VARYING. */
1014 val1->lattice_val = VARYING;
1015 val1->mask = -1;
1016 val1->value = NULL_TREE;
1021 /* Loop through the PHI_NODE's parameters for BLOCK and compare their
1022 lattice values to determine PHI_NODE's lattice value. The value of a
1023 PHI node is determined calling ccp_lattice_meet with all the arguments
1024 of the PHI node that are incoming via executable edges. */
1026 static enum ssa_prop_result
1027 ccp_visit_phi_node (gimple phi)
1029 unsigned i;
1030 ccp_prop_value_t *old_val, new_val;
1032 if (dump_file && (dump_flags & TDF_DETAILS))
1034 fprintf (dump_file, "\nVisiting PHI node: ");
1035 print_gimple_stmt (dump_file, phi, 0, dump_flags);
1038 old_val = get_value (gimple_phi_result (phi));
1039 switch (old_val->lattice_val)
1041 case VARYING:
1042 return SSA_PROP_VARYING;
1044 case CONSTANT:
1045 new_val = *old_val;
1046 break;
1048 case UNDEFINED:
1049 new_val.lattice_val = UNDEFINED;
1050 new_val.value = NULL_TREE;
1051 break;
1053 default:
1054 gcc_unreachable ();
1057 for (i = 0; i < gimple_phi_num_args (phi); i++)
1059 /* Compute the meet operator over all the PHI arguments flowing
1060 through executable edges. */
1061 edge e = gimple_phi_arg_edge (phi, i);
1063 if (dump_file && (dump_flags & TDF_DETAILS))
1065 fprintf (dump_file,
1066 "\n Argument #%d (%d -> %d %sexecutable)\n",
1067 i, e->src->index, e->dest->index,
1068 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
1071 /* If the incoming edge is executable, Compute the meet operator for
1072 the existing value of the PHI node and the current PHI argument. */
1073 if (e->flags & EDGE_EXECUTABLE)
1075 tree arg = gimple_phi_arg (phi, i)->def;
1076 ccp_prop_value_t arg_val = get_value_for_expr (arg, false);
1078 ccp_lattice_meet (&new_val, &arg_val);
1080 if (dump_file && (dump_flags & TDF_DETAILS))
1082 fprintf (dump_file, "\t");
1083 print_generic_expr (dump_file, arg, dump_flags);
1084 dump_lattice_value (dump_file, "\tValue: ", arg_val);
1085 fprintf (dump_file, "\n");
1088 if (new_val.lattice_val == VARYING)
1089 break;
1093 if (dump_file && (dump_flags & TDF_DETAILS))
1095 dump_lattice_value (dump_file, "\n PHI node value: ", new_val);
1096 fprintf (dump_file, "\n\n");
1099 /* Make the transition to the new value. */
1100 if (set_lattice_value (gimple_phi_result (phi), new_val))
1102 if (new_val.lattice_val == VARYING)
1103 return SSA_PROP_VARYING;
1104 else
1105 return SSA_PROP_INTERESTING;
1107 else
1108 return SSA_PROP_NOT_INTERESTING;
1111 /* Return the constant value for OP or OP otherwise. */
1113 static tree
1114 valueize_op (tree op)
1116 if (TREE_CODE (op) == SSA_NAME)
1118 tree tem = get_constant_value (op);
1119 if (tem)
1120 return tem;
1122 return op;
1125 /* CCP specific front-end to the non-destructive constant folding
1126 routines.
1128 Attempt to simplify the RHS of STMT knowing that one or more
1129 operands are constants.
1131 If simplification is possible, return the simplified RHS,
1132 otherwise return the original RHS or NULL_TREE. */
1134 static tree
1135 ccp_fold (gimple stmt)
1137 location_t loc = gimple_location (stmt);
1138 switch (gimple_code (stmt))
1140 case GIMPLE_COND:
1142 /* Handle comparison operators that can appear in GIMPLE form. */
1143 tree op0 = valueize_op (gimple_cond_lhs (stmt));
1144 tree op1 = valueize_op (gimple_cond_rhs (stmt));
1145 enum tree_code code = gimple_cond_code (stmt);
1146 return fold_binary_loc (loc, code, boolean_type_node, op0, op1);
1149 case GIMPLE_SWITCH:
1151 /* Return the constant switch index. */
1152 return valueize_op (gimple_switch_index (stmt));
1155 case GIMPLE_ASSIGN:
1156 case GIMPLE_CALL:
1157 return gimple_fold_stmt_to_constant_1 (stmt, valueize_op);
1159 default:
1160 gcc_unreachable ();
1164 /* Apply the operation CODE in type TYPE to the value, mask pair
1165 RVAL and RMASK representing a value of type RTYPE and set
1166 the value, mask pair *VAL and *MASK to the result. */
1168 static void
1169 bit_value_unop_1 (enum tree_code code, tree type,
1170 widest_int *val, widest_int *mask,
1171 tree rtype, const widest_int &rval, const widest_int &rmask)
1173 switch (code)
1175 case BIT_NOT_EXPR:
1176 *mask = rmask;
1177 *val = ~rval;
1178 break;
1180 case NEGATE_EXPR:
1182 widest_int temv, temm;
1183 /* Return ~rval + 1. */
1184 bit_value_unop_1 (BIT_NOT_EXPR, type, &temv, &temm, type, rval, rmask);
1185 bit_value_binop_1 (PLUS_EXPR, type, val, mask,
1186 type, temv, temm, type, 1, 0);
1187 break;
1190 CASE_CONVERT:
1192 signop sgn;
1194 /* First extend mask and value according to the original type. */
1195 sgn = TYPE_SIGN (rtype);
1196 *mask = wi::ext (rmask, TYPE_PRECISION (rtype), sgn);
1197 *val = wi::ext (rval, TYPE_PRECISION (rtype), sgn);
1199 /* Then extend mask and value according to the target type. */
1200 sgn = TYPE_SIGN (type);
1201 *mask = wi::ext (*mask, TYPE_PRECISION (type), sgn);
1202 *val = wi::ext (*val, TYPE_PRECISION (type), sgn);
1203 break;
1206 default:
1207 *mask = -1;
1208 break;
1212 /* Apply the operation CODE in type TYPE to the value, mask pairs
1213 R1VAL, R1MASK and R2VAL, R2MASK representing a values of type R1TYPE
1214 and R2TYPE and set the value, mask pair *VAL and *MASK to the result. */
1216 static void
1217 bit_value_binop_1 (enum tree_code code, tree type,
1218 widest_int *val, widest_int *mask,
1219 tree r1type, const widest_int &r1val,
1220 const widest_int &r1mask, tree r2type,
1221 const widest_int &r2val, const widest_int &r2mask)
1223 signop sgn = TYPE_SIGN (type);
1224 int width = TYPE_PRECISION (type);
1225 bool swap_p = false;
1227 /* Assume we'll get a constant result. Use an initial non varying
1228 value, we fall back to varying in the end if necessary. */
1229 *mask = -1;
1231 switch (code)
1233 case BIT_AND_EXPR:
1234 /* The mask is constant where there is a known not
1235 set bit, (m1 | m2) & ((v1 | m1) & (v2 | m2)) */
1236 *mask = (r1mask | r2mask) & (r1val | r1mask) & (r2val | r2mask);
1237 *val = r1val & r2val;
1238 break;
1240 case BIT_IOR_EXPR:
1241 /* The mask is constant where there is a known
1242 set bit, (m1 | m2) & ~((v1 & ~m1) | (v2 & ~m2)). */
1243 *mask = (r1mask | r2mask)
1244 .and_not (r1val.and_not (r1mask) | r2val.and_not (r2mask));
1245 *val = r1val | r2val;
1246 break;
1248 case BIT_XOR_EXPR:
1249 /* m1 | m2 */
1250 *mask = r1mask | r2mask;
1251 *val = r1val ^ r2val;
1252 break;
1254 case LROTATE_EXPR:
1255 case RROTATE_EXPR:
1256 if (r2mask == 0)
1258 widest_int shift = r2val;
1259 if (shift == 0)
1261 *mask = r1mask;
1262 *val = r1val;
1264 else
1266 if (wi::neg_p (shift))
1268 shift = -shift;
1269 if (code == RROTATE_EXPR)
1270 code = LROTATE_EXPR;
1271 else
1272 code = RROTATE_EXPR;
1274 if (code == RROTATE_EXPR)
1276 *mask = wi::rrotate (r1mask, shift, width);
1277 *val = wi::rrotate (r1val, shift, width);
1279 else
1281 *mask = wi::lrotate (r1mask, shift, width);
1282 *val = wi::lrotate (r1val, shift, width);
1286 break;
1288 case LSHIFT_EXPR:
1289 case RSHIFT_EXPR:
1290 /* ??? We can handle partially known shift counts if we know
1291 its sign. That way we can tell that (x << (y | 8)) & 255
1292 is zero. */
1293 if (r2mask == 0)
1295 widest_int shift = r2val;
1296 if (shift == 0)
1298 *mask = r1mask;
1299 *val = r1val;
1301 else
1303 if (wi::neg_p (shift))
1305 shift = -shift;
1306 if (code == RSHIFT_EXPR)
1307 code = LSHIFT_EXPR;
1308 else
1309 code = RSHIFT_EXPR;
1311 if (code == RSHIFT_EXPR)
1313 *mask = wi::rshift (wi::ext (r1mask, width, sgn), shift, sgn);
1314 *val = wi::rshift (wi::ext (r1val, width, sgn), shift, sgn);
1316 else
1318 *mask = wi::ext (wi::lshift (r1mask, shift), width, sgn);
1319 *val = wi::ext (wi::lshift (r1val, shift), width, sgn);
1323 break;
1325 case PLUS_EXPR:
1326 case POINTER_PLUS_EXPR:
1328 /* Do the addition with unknown bits set to zero, to give carry-ins of
1329 zero wherever possible. */
1330 widest_int lo = r1val.and_not (r1mask) + r2val.and_not (r2mask);
1331 lo = wi::ext (lo, width, sgn);
1332 /* Do the addition with unknown bits set to one, to give carry-ins of
1333 one wherever possible. */
1334 widest_int hi = (r1val | r1mask) + (r2val | r2mask);
1335 hi = wi::ext (hi, width, sgn);
1336 /* Each bit in the result is known if (a) the corresponding bits in
1337 both inputs are known, and (b) the carry-in to that bit position
1338 is known. We can check condition (b) by seeing if we got the same
1339 result with minimised carries as with maximised carries. */
1340 *mask = r1mask | r2mask | (lo ^ hi);
1341 *mask = wi::ext (*mask, width, sgn);
1342 /* It shouldn't matter whether we choose lo or hi here. */
1343 *val = lo;
1344 break;
1347 case MINUS_EXPR:
1349 widest_int temv, temm;
1350 bit_value_unop_1 (NEGATE_EXPR, r2type, &temv, &temm,
1351 r2type, r2val, r2mask);
1352 bit_value_binop_1 (PLUS_EXPR, type, val, mask,
1353 r1type, r1val, r1mask,
1354 r2type, temv, temm);
1355 break;
1358 case MULT_EXPR:
1360 /* Just track trailing zeros in both operands and transfer
1361 them to the other. */
1362 int r1tz = wi::ctz (r1val | r1mask);
1363 int r2tz = wi::ctz (r2val | r2mask);
1364 if (r1tz + r2tz >= width)
1366 *mask = 0;
1367 *val = 0;
1369 else if (r1tz + r2tz > 0)
1371 *mask = wi::ext (wi::mask <widest_int> (r1tz + r2tz, true),
1372 width, sgn);
1373 *val = 0;
1375 break;
1378 case EQ_EXPR:
1379 case NE_EXPR:
1381 widest_int m = r1mask | r2mask;
1382 if (r1val.and_not (m) != r2val.and_not (m))
1384 *mask = 0;
1385 *val = ((code == EQ_EXPR) ? 0 : 1);
1387 else
1389 /* We know the result of a comparison is always one or zero. */
1390 *mask = 1;
1391 *val = 0;
1393 break;
1396 case GE_EXPR:
1397 case GT_EXPR:
1398 swap_p = true;
1399 code = swap_tree_comparison (code);
1400 /* Fall through. */
1401 case LT_EXPR:
1402 case LE_EXPR:
1404 int minmax, maxmin;
1406 const widest_int &o1val = swap_p ? r2val : r1val;
1407 const widest_int &o1mask = swap_p ? r2mask : r1mask;
1408 const widest_int &o2val = swap_p ? r1val : r2val;
1409 const widest_int &o2mask = swap_p ? r1mask : r2mask;
1411 /* If the most significant bits are not known we know nothing. */
1412 if (wi::neg_p (o1mask) || wi::neg_p (o2mask))
1413 break;
1415 /* For comparisons the signedness is in the comparison operands. */
1416 sgn = TYPE_SIGN (r1type);
1418 /* If we know the most significant bits we know the values
1419 value ranges by means of treating varying bits as zero
1420 or one. Do a cross comparison of the max/min pairs. */
1421 maxmin = wi::cmp (o1val | o1mask, o2val.and_not (o2mask), sgn);
1422 minmax = wi::cmp (o1val.and_not (o1mask), o2val | o2mask, sgn);
1423 if (maxmin < 0) /* o1 is less than o2. */
1425 *mask = 0;
1426 *val = 1;
1428 else if (minmax > 0) /* o1 is not less or equal to o2. */
1430 *mask = 0;
1431 *val = 0;
1433 else if (maxmin == minmax) /* o1 and o2 are equal. */
1435 /* This probably should never happen as we'd have
1436 folded the thing during fully constant value folding. */
1437 *mask = 0;
1438 *val = (code == LE_EXPR ? 1 : 0);
1440 else
1442 /* We know the result of a comparison is always one or zero. */
1443 *mask = 1;
1444 *val = 0;
1446 break;
1449 default:;
1453 /* Return the propagation value when applying the operation CODE to
1454 the value RHS yielding type TYPE. */
1456 static ccp_prop_value_t
1457 bit_value_unop (enum tree_code code, tree type, tree rhs)
1459 ccp_prop_value_t rval = get_value_for_expr (rhs, true);
1460 widest_int value, mask;
1461 ccp_prop_value_t val;
1463 if (rval.lattice_val == UNDEFINED)
1464 return rval;
1466 gcc_assert ((rval.lattice_val == CONSTANT
1467 && TREE_CODE (rval.value) == INTEGER_CST)
1468 || rval.mask == -1);
1469 bit_value_unop_1 (code, type, &value, &mask,
1470 TREE_TYPE (rhs), value_to_wide_int (rval), rval.mask);
1471 if (mask != -1)
1473 val.lattice_val = CONSTANT;
1474 val.mask = mask;
1475 /* ??? Delay building trees here. */
1476 val.value = wide_int_to_tree (type, value);
1478 else
1480 val.lattice_val = VARYING;
1481 val.value = NULL_TREE;
1482 val.mask = -1;
1484 return val;
1487 /* Return the propagation value when applying the operation CODE to
1488 the values RHS1 and RHS2 yielding type TYPE. */
1490 static ccp_prop_value_t
1491 bit_value_binop (enum tree_code code, tree type, tree rhs1, tree rhs2)
1493 ccp_prop_value_t r1val = get_value_for_expr (rhs1, true);
1494 ccp_prop_value_t r2val = get_value_for_expr (rhs2, true);
1495 widest_int value, mask;
1496 ccp_prop_value_t val;
1498 if (r1val.lattice_val == UNDEFINED
1499 || r2val.lattice_val == UNDEFINED)
1501 val.lattice_val = VARYING;
1502 val.value = NULL_TREE;
1503 val.mask = -1;
1504 return val;
1507 gcc_assert ((r1val.lattice_val == CONSTANT
1508 && TREE_CODE (r1val.value) == INTEGER_CST)
1509 || r1val.mask == -1);
1510 gcc_assert ((r2val.lattice_val == CONSTANT
1511 && TREE_CODE (r2val.value) == INTEGER_CST)
1512 || r2val.mask == -1);
1513 bit_value_binop_1 (code, type, &value, &mask,
1514 TREE_TYPE (rhs1), value_to_wide_int (r1val), r1val.mask,
1515 TREE_TYPE (rhs2), value_to_wide_int (r2val), r2val.mask);
1516 if (mask != -1)
1518 val.lattice_val = CONSTANT;
1519 val.mask = mask;
1520 /* ??? Delay building trees here. */
1521 val.value = wide_int_to_tree (type, value);
1523 else
1525 val.lattice_val = VARYING;
1526 val.value = NULL_TREE;
1527 val.mask = -1;
1529 return val;
1532 /* Return the propagation value for __builtin_assume_aligned
1533 and functions with assume_aligned or alloc_aligned attribute.
1534 For __builtin_assume_aligned, ATTR is NULL_TREE,
1535 for assume_aligned attribute ATTR is non-NULL and ALLOC_ALIGNED
1536 is false, for alloc_aligned attribute ATTR is non-NULL and
1537 ALLOC_ALIGNED is true. */
1539 static ccp_prop_value_t
1540 bit_value_assume_aligned (gimple stmt, tree attr, ccp_prop_value_t ptrval,
1541 bool alloc_aligned)
1543 tree align, misalign = NULL_TREE, type;
1544 unsigned HOST_WIDE_INT aligni, misaligni = 0;
1545 ccp_prop_value_t alignval;
1546 widest_int value, mask;
1547 ccp_prop_value_t val;
1549 if (attr == NULL_TREE)
1551 tree ptr = gimple_call_arg (stmt, 0);
1552 type = TREE_TYPE (ptr);
1553 ptrval = get_value_for_expr (ptr, true);
1555 else
1557 tree lhs = gimple_call_lhs (stmt);
1558 type = TREE_TYPE (lhs);
1561 if (ptrval.lattice_val == UNDEFINED)
1562 return ptrval;
1563 gcc_assert ((ptrval.lattice_val == CONSTANT
1564 && TREE_CODE (ptrval.value) == INTEGER_CST)
1565 || ptrval.mask == -1);
1566 if (attr == NULL_TREE)
1568 /* Get aligni and misaligni from __builtin_assume_aligned. */
1569 align = gimple_call_arg (stmt, 1);
1570 if (!tree_fits_uhwi_p (align))
1571 return ptrval;
1572 aligni = tree_to_uhwi (align);
1573 if (gimple_call_num_args (stmt) > 2)
1575 misalign = gimple_call_arg (stmt, 2);
1576 if (!tree_fits_uhwi_p (misalign))
1577 return ptrval;
1578 misaligni = tree_to_uhwi (misalign);
1581 else
1583 /* Get aligni and misaligni from assume_aligned or
1584 alloc_align attributes. */
1585 if (TREE_VALUE (attr) == NULL_TREE)
1586 return ptrval;
1587 attr = TREE_VALUE (attr);
1588 align = TREE_VALUE (attr);
1589 if (!tree_fits_uhwi_p (align))
1590 return ptrval;
1591 aligni = tree_to_uhwi (align);
1592 if (alloc_aligned)
1594 if (aligni == 0 || aligni > gimple_call_num_args (stmt))
1595 return ptrval;
1596 align = gimple_call_arg (stmt, aligni - 1);
1597 if (!tree_fits_uhwi_p (align))
1598 return ptrval;
1599 aligni = tree_to_uhwi (align);
1601 else if (TREE_CHAIN (attr) && TREE_VALUE (TREE_CHAIN (attr)))
1603 misalign = TREE_VALUE (TREE_CHAIN (attr));
1604 if (!tree_fits_uhwi_p (misalign))
1605 return ptrval;
1606 misaligni = tree_to_uhwi (misalign);
1609 if (aligni <= 1 || (aligni & (aligni - 1)) != 0 || misaligni >= aligni)
1610 return ptrval;
1612 align = build_int_cst_type (type, -aligni);
1613 alignval = get_value_for_expr (align, true);
1614 bit_value_binop_1 (BIT_AND_EXPR, type, &value, &mask,
1615 type, value_to_wide_int (ptrval), ptrval.mask,
1616 type, value_to_wide_int (alignval), alignval.mask);
1617 if (mask != -1)
1619 val.lattice_val = CONSTANT;
1620 val.mask = mask;
1621 gcc_assert ((mask.to_uhwi () & (aligni - 1)) == 0);
1622 gcc_assert ((value.to_uhwi () & (aligni - 1)) == 0);
1623 value |= misaligni;
1624 /* ??? Delay building trees here. */
1625 val.value = wide_int_to_tree (type, value);
1627 else
1629 val.lattice_val = VARYING;
1630 val.value = NULL_TREE;
1631 val.mask = -1;
1633 return val;
1636 /* Evaluate statement STMT.
1637 Valid only for assignments, calls, conditionals, and switches. */
1639 static ccp_prop_value_t
1640 evaluate_stmt (gimple stmt)
1642 ccp_prop_value_t val;
1643 tree simplified = NULL_TREE;
1644 ccp_lattice_t likelyvalue = likely_value (stmt);
1645 bool is_constant = false;
1646 unsigned int align;
1648 if (dump_file && (dump_flags & TDF_DETAILS))
1650 fprintf (dump_file, "which is likely ");
1651 switch (likelyvalue)
1653 case CONSTANT:
1654 fprintf (dump_file, "CONSTANT");
1655 break;
1656 case UNDEFINED:
1657 fprintf (dump_file, "UNDEFINED");
1658 break;
1659 case VARYING:
1660 fprintf (dump_file, "VARYING");
1661 break;
1662 default:;
1664 fprintf (dump_file, "\n");
1667 /* If the statement is likely to have a CONSTANT result, then try
1668 to fold the statement to determine the constant value. */
1669 /* FIXME. This is the only place that we call ccp_fold.
1670 Since likely_value never returns CONSTANT for calls, we will
1671 not attempt to fold them, including builtins that may profit. */
1672 if (likelyvalue == CONSTANT)
1674 fold_defer_overflow_warnings ();
1675 simplified = ccp_fold (stmt);
1676 is_constant = simplified && is_gimple_min_invariant (simplified);
1677 fold_undefer_overflow_warnings (is_constant, stmt, 0);
1678 if (is_constant)
1680 /* The statement produced a constant value. */
1681 val.lattice_val = CONSTANT;
1682 val.value = simplified;
1683 val.mask = 0;
1686 /* If the statement is likely to have a VARYING result, then do not
1687 bother folding the statement. */
1688 else if (likelyvalue == VARYING)
1690 enum gimple_code code = gimple_code (stmt);
1691 if (code == GIMPLE_ASSIGN)
1693 enum tree_code subcode = gimple_assign_rhs_code (stmt);
1695 /* Other cases cannot satisfy is_gimple_min_invariant
1696 without folding. */
1697 if (get_gimple_rhs_class (subcode) == GIMPLE_SINGLE_RHS)
1698 simplified = gimple_assign_rhs1 (stmt);
1700 else if (code == GIMPLE_SWITCH)
1701 simplified = gimple_switch_index (stmt);
1702 else
1703 /* These cannot satisfy is_gimple_min_invariant without folding. */
1704 gcc_assert (code == GIMPLE_CALL || code == GIMPLE_COND);
1705 is_constant = simplified && is_gimple_min_invariant (simplified);
1706 if (is_constant)
1708 /* The statement produced a constant value. */
1709 val.lattice_val = CONSTANT;
1710 val.value = simplified;
1711 val.mask = 0;
1715 /* Resort to simplification for bitwise tracking. */
1716 if (flag_tree_bit_ccp
1717 && (likelyvalue == CONSTANT || is_gimple_call (stmt))
1718 && !is_constant)
1720 enum gimple_code code = gimple_code (stmt);
1721 val.lattice_val = VARYING;
1722 val.value = NULL_TREE;
1723 val.mask = -1;
1724 if (code == GIMPLE_ASSIGN)
1726 enum tree_code subcode = gimple_assign_rhs_code (stmt);
1727 tree rhs1 = gimple_assign_rhs1 (stmt);
1728 switch (get_gimple_rhs_class (subcode))
1730 case GIMPLE_SINGLE_RHS:
1731 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1732 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1733 val = get_value_for_expr (rhs1, true);
1734 break;
1736 case GIMPLE_UNARY_RHS:
1737 if ((INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1738 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1739 && (INTEGRAL_TYPE_P (gimple_expr_type (stmt))
1740 || POINTER_TYPE_P (gimple_expr_type (stmt))))
1741 val = bit_value_unop (subcode, gimple_expr_type (stmt), rhs1);
1742 break;
1744 case GIMPLE_BINARY_RHS:
1745 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1746 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1748 tree lhs = gimple_assign_lhs (stmt);
1749 tree rhs2 = gimple_assign_rhs2 (stmt);
1750 val = bit_value_binop (subcode,
1751 TREE_TYPE (lhs), rhs1, rhs2);
1753 break;
1755 default:;
1758 else if (code == GIMPLE_COND)
1760 enum tree_code code = gimple_cond_code (stmt);
1761 tree rhs1 = gimple_cond_lhs (stmt);
1762 tree rhs2 = gimple_cond_rhs (stmt);
1763 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1764 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1765 val = bit_value_binop (code, TREE_TYPE (rhs1), rhs1, rhs2);
1767 else if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
1769 tree fndecl = gimple_call_fndecl (stmt);
1770 switch (DECL_FUNCTION_CODE (fndecl))
1772 case BUILT_IN_MALLOC:
1773 case BUILT_IN_REALLOC:
1774 case BUILT_IN_CALLOC:
1775 case BUILT_IN_STRDUP:
1776 case BUILT_IN_STRNDUP:
1777 val.lattice_val = CONSTANT;
1778 val.value = build_int_cst (TREE_TYPE (gimple_get_lhs (stmt)), 0);
1779 val.mask = ~((HOST_WIDE_INT) MALLOC_ABI_ALIGNMENT
1780 / BITS_PER_UNIT - 1);
1781 break;
1783 case BUILT_IN_ALLOCA:
1784 case BUILT_IN_ALLOCA_WITH_ALIGN:
1785 align = (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_ALLOCA_WITH_ALIGN
1786 ? TREE_INT_CST_LOW (gimple_call_arg (stmt, 1))
1787 : BIGGEST_ALIGNMENT);
1788 val.lattice_val = CONSTANT;
1789 val.value = build_int_cst (TREE_TYPE (gimple_get_lhs (stmt)), 0);
1790 val.mask = ~((HOST_WIDE_INT) align / BITS_PER_UNIT - 1);
1791 break;
1793 /* These builtins return their first argument, unmodified. */
1794 case BUILT_IN_MEMCPY:
1795 case BUILT_IN_MEMMOVE:
1796 case BUILT_IN_MEMSET:
1797 case BUILT_IN_STRCPY:
1798 case BUILT_IN_STRNCPY:
1799 case BUILT_IN_MEMCPY_CHK:
1800 case BUILT_IN_MEMMOVE_CHK:
1801 case BUILT_IN_MEMSET_CHK:
1802 case BUILT_IN_STRCPY_CHK:
1803 case BUILT_IN_STRNCPY_CHK:
1804 val = get_value_for_expr (gimple_call_arg (stmt, 0), true);
1805 break;
1807 case BUILT_IN_ASSUME_ALIGNED:
1808 val = bit_value_assume_aligned (stmt, NULL_TREE, val, false);
1809 break;
1811 case BUILT_IN_ALIGNED_ALLOC:
1813 tree align = get_constant_value (gimple_call_arg (stmt, 0));
1814 if (align
1815 && tree_fits_uhwi_p (align))
1817 unsigned HOST_WIDE_INT aligni = tree_to_uhwi (align);
1818 if (aligni > 1
1819 /* align must be power-of-two */
1820 && (aligni & (aligni - 1)) == 0)
1822 val.lattice_val = CONSTANT;
1823 val.value = build_int_cst (ptr_type_node, 0);
1824 val.mask = -aligni;
1827 break;
1830 default:;
1833 if (is_gimple_call (stmt) && gimple_call_lhs (stmt))
1835 tree fntype = gimple_call_fntype (stmt);
1836 if (fntype)
1838 tree attrs = lookup_attribute ("assume_aligned",
1839 TYPE_ATTRIBUTES (fntype));
1840 if (attrs)
1841 val = bit_value_assume_aligned (stmt, attrs, val, false);
1842 attrs = lookup_attribute ("alloc_align",
1843 TYPE_ATTRIBUTES (fntype));
1844 if (attrs)
1845 val = bit_value_assume_aligned (stmt, attrs, val, true);
1848 is_constant = (val.lattice_val == CONSTANT);
1851 if (flag_tree_bit_ccp
1852 && ((is_constant && TREE_CODE (val.value) == INTEGER_CST)
1853 || (!is_constant && likelyvalue != UNDEFINED))
1854 && gimple_get_lhs (stmt)
1855 && TREE_CODE (gimple_get_lhs (stmt)) == SSA_NAME)
1857 tree lhs = gimple_get_lhs (stmt);
1858 wide_int nonzero_bits = get_nonzero_bits (lhs);
1859 if (nonzero_bits != -1)
1861 if (!is_constant)
1863 val.lattice_val = CONSTANT;
1864 val.value = build_zero_cst (TREE_TYPE (lhs));
1865 val.mask = extend_mask (nonzero_bits);
1866 is_constant = true;
1868 else
1870 if (wi::bit_and_not (val.value, nonzero_bits) != 0)
1871 val.value = wide_int_to_tree (TREE_TYPE (lhs),
1872 nonzero_bits & val.value);
1873 if (nonzero_bits == 0)
1874 val.mask = 0;
1875 else
1876 val.mask = val.mask & extend_mask (nonzero_bits);
1881 if (!is_constant)
1883 /* The statement produced a nonconstant value. If the statement
1884 had UNDEFINED operands, then the result of the statement
1885 should be UNDEFINED. Otherwise, the statement is VARYING. */
1886 if (likelyvalue == UNDEFINED)
1888 val.lattice_val = likelyvalue;
1889 val.mask = 0;
1891 else
1893 val.lattice_val = VARYING;
1894 val.mask = -1;
1897 val.value = NULL_TREE;
1900 return val;
1903 typedef hash_table<pointer_hash<gimple_statement_base> > gimple_htab;
1905 /* Given a BUILT_IN_STACK_SAVE value SAVED_VAL, insert a clobber of VAR before
1906 each matching BUILT_IN_STACK_RESTORE. Mark visited phis in VISITED. */
1908 static void
1909 insert_clobber_before_stack_restore (tree saved_val, tree var,
1910 gimple_htab **visited)
1912 gimple stmt, clobber_stmt;
1913 tree clobber;
1914 imm_use_iterator iter;
1915 gimple_stmt_iterator i;
1916 gimple *slot;
1918 FOR_EACH_IMM_USE_STMT (stmt, iter, saved_val)
1919 if (gimple_call_builtin_p (stmt, BUILT_IN_STACK_RESTORE))
1921 clobber = build_constructor (TREE_TYPE (var),
1922 NULL);
1923 TREE_THIS_VOLATILE (clobber) = 1;
1924 clobber_stmt = gimple_build_assign (var, clobber);
1926 i = gsi_for_stmt (stmt);
1927 gsi_insert_before (&i, clobber_stmt, GSI_SAME_STMT);
1929 else if (gimple_code (stmt) == GIMPLE_PHI)
1931 if (!*visited)
1932 *visited = new gimple_htab (10);
1934 slot = (*visited)->find_slot (stmt, INSERT);
1935 if (*slot != NULL)
1936 continue;
1938 *slot = stmt;
1939 insert_clobber_before_stack_restore (gimple_phi_result (stmt), var,
1940 visited);
1942 else if (gimple_assign_ssa_name_copy_p (stmt))
1943 insert_clobber_before_stack_restore (gimple_assign_lhs (stmt), var,
1944 visited);
1945 else
1946 gcc_assert (is_gimple_debug (stmt));
1949 /* Advance the iterator to the previous non-debug gimple statement in the same
1950 or dominating basic block. */
1952 static inline void
1953 gsi_prev_dom_bb_nondebug (gimple_stmt_iterator *i)
1955 basic_block dom;
1957 gsi_prev_nondebug (i);
1958 while (gsi_end_p (*i))
1960 dom = get_immediate_dominator (CDI_DOMINATORS, i->bb);
1961 if (dom == NULL || dom == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1962 return;
1964 *i = gsi_last_bb (dom);
1968 /* Find a BUILT_IN_STACK_SAVE dominating gsi_stmt (I), and insert
1969 a clobber of VAR before each matching BUILT_IN_STACK_RESTORE.
1971 It is possible that BUILT_IN_STACK_SAVE cannot be find in a dominator when a
1972 previous pass (such as DOM) duplicated it along multiple paths to a BB. In
1973 that case the function gives up without inserting the clobbers. */
1975 static void
1976 insert_clobbers_for_var (gimple_stmt_iterator i, tree var)
1978 gimple stmt;
1979 tree saved_val;
1980 gimple_htab *visited = NULL;
1982 for (; !gsi_end_p (i); gsi_prev_dom_bb_nondebug (&i))
1984 stmt = gsi_stmt (i);
1986 if (!gimple_call_builtin_p (stmt, BUILT_IN_STACK_SAVE))
1987 continue;
1989 saved_val = gimple_call_lhs (stmt);
1990 if (saved_val == NULL_TREE)
1991 continue;
1993 insert_clobber_before_stack_restore (saved_val, var, &visited);
1994 break;
1997 delete visited;
2000 /* Detects a __builtin_alloca_with_align with constant size argument. Declares
2001 fixed-size array and returns the address, if found, otherwise returns
2002 NULL_TREE. */
2004 static tree
2005 fold_builtin_alloca_with_align (gimple stmt)
2007 unsigned HOST_WIDE_INT size, threshold, n_elem;
2008 tree lhs, arg, block, var, elem_type, array_type;
2010 /* Get lhs. */
2011 lhs = gimple_call_lhs (stmt);
2012 if (lhs == NULL_TREE)
2013 return NULL_TREE;
2015 /* Detect constant argument. */
2016 arg = get_constant_value (gimple_call_arg (stmt, 0));
2017 if (arg == NULL_TREE
2018 || TREE_CODE (arg) != INTEGER_CST
2019 || !tree_fits_uhwi_p (arg))
2020 return NULL_TREE;
2022 size = tree_to_uhwi (arg);
2024 /* Heuristic: don't fold large allocas. */
2025 threshold = (unsigned HOST_WIDE_INT)PARAM_VALUE (PARAM_LARGE_STACK_FRAME);
2026 /* In case the alloca is located at function entry, it has the same lifetime
2027 as a declared array, so we allow a larger size. */
2028 block = gimple_block (stmt);
2029 if (!(cfun->after_inlining
2030 && TREE_CODE (BLOCK_SUPERCONTEXT (block)) == FUNCTION_DECL))
2031 threshold /= 10;
2032 if (size > threshold)
2033 return NULL_TREE;
2035 /* Declare array. */
2036 elem_type = build_nonstandard_integer_type (BITS_PER_UNIT, 1);
2037 n_elem = size * 8 / BITS_PER_UNIT;
2038 array_type = build_array_type_nelts (elem_type, n_elem);
2039 var = create_tmp_var (array_type, NULL);
2040 DECL_ALIGN (var) = TREE_INT_CST_LOW (gimple_call_arg (stmt, 1));
2042 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (lhs);
2043 if (pi != NULL && !pi->pt.anything)
2045 bool singleton_p;
2046 unsigned uid;
2047 singleton_p = pt_solution_singleton_p (&pi->pt, &uid);
2048 gcc_assert (singleton_p);
2049 SET_DECL_PT_UID (var, uid);
2053 /* Fold alloca to the address of the array. */
2054 return fold_convert (TREE_TYPE (lhs), build_fold_addr_expr (var));
2057 /* Fold the stmt at *GSI with CCP specific information that propagating
2058 and regular folding does not catch. */
2060 static bool
2061 ccp_fold_stmt (gimple_stmt_iterator *gsi)
2063 gimple stmt = gsi_stmt (*gsi);
2065 switch (gimple_code (stmt))
2067 case GIMPLE_COND:
2069 ccp_prop_value_t val;
2070 /* Statement evaluation will handle type mismatches in constants
2071 more gracefully than the final propagation. This allows us to
2072 fold more conditionals here. */
2073 val = evaluate_stmt (stmt);
2074 if (val.lattice_val != CONSTANT
2075 || val.mask != 0)
2076 return false;
2078 if (dump_file)
2080 fprintf (dump_file, "Folding predicate ");
2081 print_gimple_expr (dump_file, stmt, 0, 0);
2082 fprintf (dump_file, " to ");
2083 print_generic_expr (dump_file, val.value, 0);
2084 fprintf (dump_file, "\n");
2087 if (integer_zerop (val.value))
2088 gimple_cond_make_false (stmt);
2089 else
2090 gimple_cond_make_true (stmt);
2092 return true;
2095 case GIMPLE_CALL:
2097 tree lhs = gimple_call_lhs (stmt);
2098 int flags = gimple_call_flags (stmt);
2099 tree val;
2100 tree argt;
2101 bool changed = false;
2102 unsigned i;
2104 /* If the call was folded into a constant make sure it goes
2105 away even if we cannot propagate into all uses because of
2106 type issues. */
2107 if (lhs
2108 && TREE_CODE (lhs) == SSA_NAME
2109 && (val = get_constant_value (lhs))
2110 /* Don't optimize away calls that have side-effects. */
2111 && (flags & (ECF_CONST|ECF_PURE)) != 0
2112 && (flags & ECF_LOOPING_CONST_OR_PURE) == 0)
2114 tree new_rhs = unshare_expr (val);
2115 bool res;
2116 if (!useless_type_conversion_p (TREE_TYPE (lhs),
2117 TREE_TYPE (new_rhs)))
2118 new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
2119 res = update_call_from_tree (gsi, new_rhs);
2120 gcc_assert (res);
2121 return true;
2124 /* Internal calls provide no argument types, so the extra laxity
2125 for normal calls does not apply. */
2126 if (gimple_call_internal_p (stmt))
2127 return false;
2129 /* The heuristic of fold_builtin_alloca_with_align differs before and
2130 after inlining, so we don't require the arg to be changed into a
2131 constant for folding, but just to be constant. */
2132 if (gimple_call_builtin_p (stmt, BUILT_IN_ALLOCA_WITH_ALIGN))
2134 tree new_rhs = fold_builtin_alloca_with_align (stmt);
2135 if (new_rhs)
2137 bool res = update_call_from_tree (gsi, new_rhs);
2138 tree var = TREE_OPERAND (TREE_OPERAND (new_rhs, 0),0);
2139 gcc_assert (res);
2140 insert_clobbers_for_var (*gsi, var);
2141 return true;
2145 /* Propagate into the call arguments. Compared to replace_uses_in
2146 this can use the argument slot types for type verification
2147 instead of the current argument type. We also can safely
2148 drop qualifiers here as we are dealing with constants anyway. */
2149 argt = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
2150 for (i = 0; i < gimple_call_num_args (stmt) && argt;
2151 ++i, argt = TREE_CHAIN (argt))
2153 tree arg = gimple_call_arg (stmt, i);
2154 if (TREE_CODE (arg) == SSA_NAME
2155 && (val = get_constant_value (arg))
2156 && useless_type_conversion_p
2157 (TYPE_MAIN_VARIANT (TREE_VALUE (argt)),
2158 TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2160 gimple_call_set_arg (stmt, i, unshare_expr (val));
2161 changed = true;
2165 return changed;
2168 case GIMPLE_ASSIGN:
2170 tree lhs = gimple_assign_lhs (stmt);
2171 tree val;
2173 /* If we have a load that turned out to be constant replace it
2174 as we cannot propagate into all uses in all cases. */
2175 if (gimple_assign_single_p (stmt)
2176 && TREE_CODE (lhs) == SSA_NAME
2177 && (val = get_constant_value (lhs)))
2179 tree rhs = unshare_expr (val);
2180 if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs)))
2181 rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (lhs), rhs);
2182 gimple_assign_set_rhs_from_tree (gsi, rhs);
2183 return true;
2186 return false;
2189 default:
2190 return false;
2194 /* Visit the assignment statement STMT. Set the value of its LHS to the
2195 value computed by the RHS and store LHS in *OUTPUT_P. If STMT
2196 creates virtual definitions, set the value of each new name to that
2197 of the RHS (if we can derive a constant out of the RHS).
2198 Value-returning call statements also perform an assignment, and
2199 are handled here. */
2201 static enum ssa_prop_result
2202 visit_assignment (gimple stmt, tree *output_p)
2204 ccp_prop_value_t val;
2205 enum ssa_prop_result retval;
2207 tree lhs = gimple_get_lhs (stmt);
2209 gcc_assert (gimple_code (stmt) != GIMPLE_CALL
2210 || gimple_call_lhs (stmt) != NULL_TREE);
2212 if (gimple_assign_single_p (stmt)
2213 && gimple_assign_rhs_code (stmt) == SSA_NAME)
2214 /* For a simple copy operation, we copy the lattice values. */
2215 val = *get_value (gimple_assign_rhs1 (stmt));
2216 else
2217 /* Evaluate the statement, which could be
2218 either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
2219 val = evaluate_stmt (stmt);
2221 retval = SSA_PROP_NOT_INTERESTING;
2223 /* Set the lattice value of the statement's output. */
2224 if (TREE_CODE (lhs) == SSA_NAME)
2226 /* If STMT is an assignment to an SSA_NAME, we only have one
2227 value to set. */
2228 if (set_lattice_value (lhs, val))
2230 *output_p = lhs;
2231 if (val.lattice_val == VARYING)
2232 retval = SSA_PROP_VARYING;
2233 else
2234 retval = SSA_PROP_INTERESTING;
2238 return retval;
2242 /* Visit the conditional statement STMT. Return SSA_PROP_INTERESTING
2243 if it can determine which edge will be taken. Otherwise, return
2244 SSA_PROP_VARYING. */
2246 static enum ssa_prop_result
2247 visit_cond_stmt (gimple stmt, edge *taken_edge_p)
2249 ccp_prop_value_t val;
2250 basic_block block;
2252 block = gimple_bb (stmt);
2253 val = evaluate_stmt (stmt);
2254 if (val.lattice_val != CONSTANT
2255 || val.mask != 0)
2256 return SSA_PROP_VARYING;
2258 /* Find which edge out of the conditional block will be taken and add it
2259 to the worklist. If no single edge can be determined statically,
2260 return SSA_PROP_VARYING to feed all the outgoing edges to the
2261 propagation engine. */
2262 *taken_edge_p = find_taken_edge (block, val.value);
2263 if (*taken_edge_p)
2264 return SSA_PROP_INTERESTING;
2265 else
2266 return SSA_PROP_VARYING;
2270 /* Evaluate statement STMT. If the statement produces an output value and
2271 its evaluation changes the lattice value of its output, return
2272 SSA_PROP_INTERESTING and set *OUTPUT_P to the SSA_NAME holding the
2273 output value.
2275 If STMT is a conditional branch and we can determine its truth
2276 value, set *TAKEN_EDGE_P accordingly. If STMT produces a varying
2277 value, return SSA_PROP_VARYING. */
2279 static enum ssa_prop_result
2280 ccp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
2282 tree def;
2283 ssa_op_iter iter;
2285 if (dump_file && (dump_flags & TDF_DETAILS))
2287 fprintf (dump_file, "\nVisiting statement:\n");
2288 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2291 switch (gimple_code (stmt))
2293 case GIMPLE_ASSIGN:
2294 /* If the statement is an assignment that produces a single
2295 output value, evaluate its RHS to see if the lattice value of
2296 its output has changed. */
2297 return visit_assignment (stmt, output_p);
2299 case GIMPLE_CALL:
2300 /* A value-returning call also performs an assignment. */
2301 if (gimple_call_lhs (stmt) != NULL_TREE)
2302 return visit_assignment (stmt, output_p);
2303 break;
2305 case GIMPLE_COND:
2306 case GIMPLE_SWITCH:
2307 /* If STMT is a conditional branch, see if we can determine
2308 which branch will be taken. */
2309 /* FIXME. It appears that we should be able to optimize
2310 computed GOTOs here as well. */
2311 return visit_cond_stmt (stmt, taken_edge_p);
2313 default:
2314 break;
2317 /* Any other kind of statement is not interesting for constant
2318 propagation and, therefore, not worth simulating. */
2319 if (dump_file && (dump_flags & TDF_DETAILS))
2320 fprintf (dump_file, "No interesting values produced. Marked VARYING.\n");
2322 /* Definitions made by statements other than assignments to
2323 SSA_NAMEs represent unknown modifications to their outputs.
2324 Mark them VARYING. */
2325 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
2327 ccp_prop_value_t v = { VARYING, NULL_TREE, -1 };
2328 set_lattice_value (def, v);
2331 return SSA_PROP_VARYING;
2335 /* Main entry point for SSA Conditional Constant Propagation. */
2337 static unsigned int
2338 do_ssa_ccp (void)
2340 unsigned int todo = 0;
2341 calculate_dominance_info (CDI_DOMINATORS);
2342 ccp_initialize ();
2343 ssa_propagate (ccp_visit_stmt, ccp_visit_phi_node);
2344 if (ccp_finalize ())
2345 todo = (TODO_cleanup_cfg | TODO_update_ssa);
2346 free_dominance_info (CDI_DOMINATORS);
2347 return todo;
2351 namespace {
2353 const pass_data pass_data_ccp =
2355 GIMPLE_PASS, /* type */
2356 "ccp", /* name */
2357 OPTGROUP_NONE, /* optinfo_flags */
2358 TV_TREE_CCP, /* tv_id */
2359 ( PROP_cfg | PROP_ssa ), /* properties_required */
2360 0, /* properties_provided */
2361 0, /* properties_destroyed */
2362 0, /* todo_flags_start */
2363 TODO_update_address_taken, /* todo_flags_finish */
2366 class pass_ccp : public gimple_opt_pass
2368 public:
2369 pass_ccp (gcc::context *ctxt)
2370 : gimple_opt_pass (pass_data_ccp, ctxt)
2373 /* opt_pass methods: */
2374 opt_pass * clone () { return new pass_ccp (m_ctxt); }
2375 virtual bool gate (function *) { return flag_tree_ccp != 0; }
2376 virtual unsigned int execute (function *) { return do_ssa_ccp (); }
2378 }; // class pass_ccp
2380 } // anon namespace
2382 gimple_opt_pass *
2383 make_pass_ccp (gcc::context *ctxt)
2385 return new pass_ccp (ctxt);
2390 /* Try to optimize out __builtin_stack_restore. Optimize it out
2391 if there is another __builtin_stack_restore in the same basic
2392 block and no calls or ASM_EXPRs are in between, or if this block's
2393 only outgoing edge is to EXIT_BLOCK and there are no calls or
2394 ASM_EXPRs after this __builtin_stack_restore. */
2396 static tree
2397 optimize_stack_restore (gimple_stmt_iterator i)
2399 tree callee;
2400 gimple stmt;
2402 basic_block bb = gsi_bb (i);
2403 gimple call = gsi_stmt (i);
2405 if (gimple_code (call) != GIMPLE_CALL
2406 || gimple_call_num_args (call) != 1
2407 || TREE_CODE (gimple_call_arg (call, 0)) != SSA_NAME
2408 || !POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))))
2409 return NULL_TREE;
2411 for (gsi_next (&i); !gsi_end_p (i); gsi_next (&i))
2413 stmt = gsi_stmt (i);
2414 if (gimple_code (stmt) == GIMPLE_ASM)
2415 return NULL_TREE;
2416 if (gimple_code (stmt) != GIMPLE_CALL)
2417 continue;
2419 callee = gimple_call_fndecl (stmt);
2420 if (!callee
2421 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2422 /* All regular builtins are ok, just obviously not alloca. */
2423 || DECL_FUNCTION_CODE (callee) == BUILT_IN_ALLOCA
2424 || DECL_FUNCTION_CODE (callee) == BUILT_IN_ALLOCA_WITH_ALIGN)
2425 return NULL_TREE;
2427 if (DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_RESTORE)
2428 goto second_stack_restore;
2431 if (!gsi_end_p (i))
2432 return NULL_TREE;
2434 /* Allow one successor of the exit block, or zero successors. */
2435 switch (EDGE_COUNT (bb->succs))
2437 case 0:
2438 break;
2439 case 1:
2440 if (single_succ_edge (bb)->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
2441 return NULL_TREE;
2442 break;
2443 default:
2444 return NULL_TREE;
2446 second_stack_restore:
2448 /* If there's exactly one use, then zap the call to __builtin_stack_save.
2449 If there are multiple uses, then the last one should remove the call.
2450 In any case, whether the call to __builtin_stack_save can be removed
2451 or not is irrelevant to removing the call to __builtin_stack_restore. */
2452 if (has_single_use (gimple_call_arg (call, 0)))
2454 gimple stack_save = SSA_NAME_DEF_STMT (gimple_call_arg (call, 0));
2455 if (is_gimple_call (stack_save))
2457 callee = gimple_call_fndecl (stack_save);
2458 if (callee
2459 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
2460 && DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_SAVE)
2462 gimple_stmt_iterator stack_save_gsi;
2463 tree rhs;
2465 stack_save_gsi = gsi_for_stmt (stack_save);
2466 rhs = build_int_cst (TREE_TYPE (gimple_call_arg (call, 0)), 0);
2467 update_call_from_tree (&stack_save_gsi, rhs);
2472 /* No effect, so the statement will be deleted. */
2473 return integer_zero_node;
2476 /* If va_list type is a simple pointer and nothing special is needed,
2477 optimize __builtin_va_start (&ap, 0) into ap = __builtin_next_arg (0),
2478 __builtin_va_end (&ap) out as NOP and __builtin_va_copy into a simple
2479 pointer assignment. */
2481 static tree
2482 optimize_stdarg_builtin (gimple call)
2484 tree callee, lhs, rhs, cfun_va_list;
2485 bool va_list_simple_ptr;
2486 location_t loc = gimple_location (call);
2488 if (gimple_code (call) != GIMPLE_CALL)
2489 return NULL_TREE;
2491 callee = gimple_call_fndecl (call);
2493 cfun_va_list = targetm.fn_abi_va_list (callee);
2494 va_list_simple_ptr = POINTER_TYPE_P (cfun_va_list)
2495 && (TREE_TYPE (cfun_va_list) == void_type_node
2496 || TREE_TYPE (cfun_va_list) == char_type_node);
2498 switch (DECL_FUNCTION_CODE (callee))
2500 case BUILT_IN_VA_START:
2501 if (!va_list_simple_ptr
2502 || targetm.expand_builtin_va_start != NULL
2503 || !builtin_decl_explicit_p (BUILT_IN_NEXT_ARG))
2504 return NULL_TREE;
2506 if (gimple_call_num_args (call) != 2)
2507 return NULL_TREE;
2509 lhs = gimple_call_arg (call, 0);
2510 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
2511 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
2512 != TYPE_MAIN_VARIANT (cfun_va_list))
2513 return NULL_TREE;
2515 lhs = build_fold_indirect_ref_loc (loc, lhs);
2516 rhs = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_NEXT_ARG),
2517 1, integer_zero_node);
2518 rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
2519 return build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, rhs);
2521 case BUILT_IN_VA_COPY:
2522 if (!va_list_simple_ptr)
2523 return NULL_TREE;
2525 if (gimple_call_num_args (call) != 2)
2526 return NULL_TREE;
2528 lhs = gimple_call_arg (call, 0);
2529 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
2530 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
2531 != TYPE_MAIN_VARIANT (cfun_va_list))
2532 return NULL_TREE;
2534 lhs = build_fold_indirect_ref_loc (loc, lhs);
2535 rhs = gimple_call_arg (call, 1);
2536 if (TYPE_MAIN_VARIANT (TREE_TYPE (rhs))
2537 != TYPE_MAIN_VARIANT (cfun_va_list))
2538 return NULL_TREE;
2540 rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
2541 return build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, rhs);
2543 case BUILT_IN_VA_END:
2544 /* No effect, so the statement will be deleted. */
2545 return integer_zero_node;
2547 default:
2548 gcc_unreachable ();
2552 /* Attemp to make the block of __builtin_unreachable I unreachable by changing
2553 the incoming jumps. Return true if at least one jump was changed. */
2555 static bool
2556 optimize_unreachable (gimple_stmt_iterator i)
2558 basic_block bb = gsi_bb (i);
2559 gimple_stmt_iterator gsi;
2560 gimple stmt;
2561 edge_iterator ei;
2562 edge e;
2563 bool ret;
2565 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2567 stmt = gsi_stmt (gsi);
2569 if (is_gimple_debug (stmt))
2570 continue;
2572 if (gimple_code (stmt) == GIMPLE_LABEL)
2574 /* Verify we do not need to preserve the label. */
2575 if (FORCED_LABEL (gimple_label_label (stmt)))
2576 return false;
2578 continue;
2581 /* Only handle the case that __builtin_unreachable is the first statement
2582 in the block. We rely on DCE to remove stmts without side-effects
2583 before __builtin_unreachable. */
2584 if (gsi_stmt (gsi) != gsi_stmt (i))
2585 return false;
2588 ret = false;
2589 FOR_EACH_EDGE (e, ei, bb->preds)
2591 gsi = gsi_last_bb (e->src);
2592 if (gsi_end_p (gsi))
2593 continue;
2595 stmt = gsi_stmt (gsi);
2596 if (gimple_code (stmt) == GIMPLE_COND)
2598 if (e->flags & EDGE_TRUE_VALUE)
2599 gimple_cond_make_false (stmt);
2600 else if (e->flags & EDGE_FALSE_VALUE)
2601 gimple_cond_make_true (stmt);
2602 else
2603 gcc_unreachable ();
2604 update_stmt (stmt);
2606 else
2608 /* Todo: handle other cases, f.i. switch statement. */
2609 continue;
2612 ret = true;
2615 return ret;
2618 /* A simple pass that attempts to fold all builtin functions. This pass
2619 is run after we've propagated as many constants as we can. */
2621 namespace {
2623 const pass_data pass_data_fold_builtins =
2625 GIMPLE_PASS, /* type */
2626 "fab", /* name */
2627 OPTGROUP_NONE, /* optinfo_flags */
2628 TV_NONE, /* tv_id */
2629 ( PROP_cfg | PROP_ssa ), /* properties_required */
2630 0, /* properties_provided */
2631 0, /* properties_destroyed */
2632 0, /* todo_flags_start */
2633 TODO_update_ssa, /* todo_flags_finish */
2636 class pass_fold_builtins : public gimple_opt_pass
2638 public:
2639 pass_fold_builtins (gcc::context *ctxt)
2640 : gimple_opt_pass (pass_data_fold_builtins, ctxt)
2643 /* opt_pass methods: */
2644 opt_pass * clone () { return new pass_fold_builtins (m_ctxt); }
2645 virtual unsigned int execute (function *);
2647 }; // class pass_fold_builtins
2649 unsigned int
2650 pass_fold_builtins::execute (function *fun)
2652 bool cfg_changed = false;
2653 basic_block bb;
2654 unsigned int todoflags = 0;
2656 FOR_EACH_BB_FN (bb, fun)
2658 gimple_stmt_iterator i;
2659 for (i = gsi_start_bb (bb); !gsi_end_p (i); )
2661 gimple stmt, old_stmt;
2662 tree callee;
2663 enum built_in_function fcode;
2665 stmt = gsi_stmt (i);
2667 if (gimple_code (stmt) != GIMPLE_CALL)
2669 /* Remove all *ssaname_N ={v} {CLOBBER}; stmts,
2670 after the last GIMPLE DSE they aren't needed and might
2671 unnecessarily keep the SSA_NAMEs live. */
2672 if (gimple_clobber_p (stmt))
2674 tree lhs = gimple_assign_lhs (stmt);
2675 if (TREE_CODE (lhs) == MEM_REF
2676 && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME)
2678 unlink_stmt_vdef (stmt);
2679 gsi_remove (&i, true);
2680 release_defs (stmt);
2681 continue;
2684 gsi_next (&i);
2685 continue;
2688 callee = gimple_call_fndecl (stmt);
2689 if (!callee || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL)
2691 gsi_next (&i);
2692 continue;
2695 fcode = DECL_FUNCTION_CODE (callee);
2696 if (fold_stmt (&i))
2698 else
2700 tree result = NULL_TREE;
2701 switch (DECL_FUNCTION_CODE (callee))
2703 case BUILT_IN_CONSTANT_P:
2704 /* Resolve __builtin_constant_p. If it hasn't been
2705 folded to integer_one_node by now, it's fairly
2706 certain that the value simply isn't constant. */
2707 result = integer_zero_node;
2708 break;
2710 case BUILT_IN_ASSUME_ALIGNED:
2711 /* Remove __builtin_assume_aligned. */
2712 result = gimple_call_arg (stmt, 0);
2713 break;
2715 case BUILT_IN_STACK_RESTORE:
2716 result = optimize_stack_restore (i);
2717 if (result)
2718 break;
2719 gsi_next (&i);
2720 continue;
2722 case BUILT_IN_UNREACHABLE:
2723 if (optimize_unreachable (i))
2724 cfg_changed = true;
2725 break;
2727 case BUILT_IN_VA_START:
2728 case BUILT_IN_VA_END:
2729 case BUILT_IN_VA_COPY:
2730 /* These shouldn't be folded before pass_stdarg. */
2731 result = optimize_stdarg_builtin (stmt);
2732 if (result)
2733 break;
2734 /* FALLTHRU */
2736 default:;
2739 if (!result)
2741 gsi_next (&i);
2742 continue;
2745 if (!update_call_from_tree (&i, result))
2746 gimplify_and_update_call_from_tree (&i, result);
2749 todoflags |= TODO_update_address_taken;
2751 if (dump_file && (dump_flags & TDF_DETAILS))
2753 fprintf (dump_file, "Simplified\n ");
2754 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2757 old_stmt = stmt;
2758 stmt = gsi_stmt (i);
2759 update_stmt (stmt);
2761 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt)
2762 && gimple_purge_dead_eh_edges (bb))
2763 cfg_changed = true;
2765 if (dump_file && (dump_flags & TDF_DETAILS))
2767 fprintf (dump_file, "to\n ");
2768 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2769 fprintf (dump_file, "\n");
2772 /* Retry the same statement if it changed into another
2773 builtin, there might be new opportunities now. */
2774 if (gimple_code (stmt) != GIMPLE_CALL)
2776 gsi_next (&i);
2777 continue;
2779 callee = gimple_call_fndecl (stmt);
2780 if (!callee
2781 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2782 || DECL_FUNCTION_CODE (callee) == fcode)
2783 gsi_next (&i);
2787 /* Delete unreachable blocks. */
2788 if (cfg_changed)
2789 todoflags |= TODO_cleanup_cfg;
2791 return todoflags;
2794 } // anon namespace
2796 gimple_opt_pass *
2797 make_pass_fold_builtins (gcc::context *ctxt)
2799 return new pass_fold_builtins (ctxt);