ipa-utils.h (polymorphic_call_context): Add metdhos dump, debug and clear_outer_type.
[official-gcc.git] / gcc / tree-ssa-ccp.c
blobce23019058d5c57e126ec08e0f5e6e7952915666
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_stmt_iterator i;
834 for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
836 gimple phi = gsi_stmt (i);
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)
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 (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 (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, clobber_stmt;
1907 tree clobber;
1908 imm_use_iterator iter;
1909 gimple_stmt_iterator i;
1910 gimple *slot;
1912 FOR_EACH_IMM_USE_STMT (stmt, iter, saved_val)
1913 if (gimple_call_builtin_p (stmt, BUILT_IN_STACK_RESTORE))
1915 clobber = build_constructor (TREE_TYPE (var),
1916 NULL);
1917 TREE_THIS_VOLATILE (clobber) = 1;
1918 clobber_stmt = gimple_build_assign (var, clobber);
1920 i = gsi_for_stmt (stmt);
1921 gsi_insert_before (&i, clobber_stmt, GSI_SAME_STMT);
1923 else if (gimple_code (stmt) == GIMPLE_PHI)
1925 if (!*visited)
1926 *visited = new gimple_htab (10);
1928 slot = (*visited)->find_slot (stmt, INSERT);
1929 if (*slot != NULL)
1930 continue;
1932 *slot = stmt;
1933 insert_clobber_before_stack_restore (gimple_phi_result (stmt), var,
1934 visited);
1936 else if (gimple_assign_ssa_name_copy_p (stmt))
1937 insert_clobber_before_stack_restore (gimple_assign_lhs (stmt), var,
1938 visited);
1939 else
1940 gcc_assert (is_gimple_debug (stmt));
1943 /* Advance the iterator to the previous non-debug gimple statement in the same
1944 or dominating basic block. */
1946 static inline void
1947 gsi_prev_dom_bb_nondebug (gimple_stmt_iterator *i)
1949 basic_block dom;
1951 gsi_prev_nondebug (i);
1952 while (gsi_end_p (*i))
1954 dom = get_immediate_dominator (CDI_DOMINATORS, i->bb);
1955 if (dom == NULL || dom == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1956 return;
1958 *i = gsi_last_bb (dom);
1962 /* Find a BUILT_IN_STACK_SAVE dominating gsi_stmt (I), and insert
1963 a clobber of VAR before each matching BUILT_IN_STACK_RESTORE.
1965 It is possible that BUILT_IN_STACK_SAVE cannot be find in a dominator when a
1966 previous pass (such as DOM) duplicated it along multiple paths to a BB. In
1967 that case the function gives up without inserting the clobbers. */
1969 static void
1970 insert_clobbers_for_var (gimple_stmt_iterator i, tree var)
1972 gimple stmt;
1973 tree saved_val;
1974 gimple_htab *visited = NULL;
1976 for (; !gsi_end_p (i); gsi_prev_dom_bb_nondebug (&i))
1978 stmt = gsi_stmt (i);
1980 if (!gimple_call_builtin_p (stmt, BUILT_IN_STACK_SAVE))
1981 continue;
1983 saved_val = gimple_call_lhs (stmt);
1984 if (saved_val == NULL_TREE)
1985 continue;
1987 insert_clobber_before_stack_restore (saved_val, var, &visited);
1988 break;
1991 delete visited;
1994 /* Detects a __builtin_alloca_with_align with constant size argument. Declares
1995 fixed-size array and returns the address, if found, otherwise returns
1996 NULL_TREE. */
1998 static tree
1999 fold_builtin_alloca_with_align (gimple stmt)
2001 unsigned HOST_WIDE_INT size, threshold, n_elem;
2002 tree lhs, arg, block, var, elem_type, array_type;
2004 /* Get lhs. */
2005 lhs = gimple_call_lhs (stmt);
2006 if (lhs == NULL_TREE)
2007 return NULL_TREE;
2009 /* Detect constant argument. */
2010 arg = get_constant_value (gimple_call_arg (stmt, 0));
2011 if (arg == NULL_TREE
2012 || TREE_CODE (arg) != INTEGER_CST
2013 || !tree_fits_uhwi_p (arg))
2014 return NULL_TREE;
2016 size = tree_to_uhwi (arg);
2018 /* Heuristic: don't fold large allocas. */
2019 threshold = (unsigned HOST_WIDE_INT)PARAM_VALUE (PARAM_LARGE_STACK_FRAME);
2020 /* In case the alloca is located at function entry, it has the same lifetime
2021 as a declared array, so we allow a larger size. */
2022 block = gimple_block (stmt);
2023 if (!(cfun->after_inlining
2024 && TREE_CODE (BLOCK_SUPERCONTEXT (block)) == FUNCTION_DECL))
2025 threshold /= 10;
2026 if (size > threshold)
2027 return NULL_TREE;
2029 /* Declare array. */
2030 elem_type = build_nonstandard_integer_type (BITS_PER_UNIT, 1);
2031 n_elem = size * 8 / BITS_PER_UNIT;
2032 array_type = build_array_type_nelts (elem_type, n_elem);
2033 var = create_tmp_var (array_type, NULL);
2034 DECL_ALIGN (var) = TREE_INT_CST_LOW (gimple_call_arg (stmt, 1));
2036 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (lhs);
2037 if (pi != NULL && !pi->pt.anything)
2039 bool singleton_p;
2040 unsigned uid;
2041 singleton_p = pt_solution_singleton_p (&pi->pt, &uid);
2042 gcc_assert (singleton_p);
2043 SET_DECL_PT_UID (var, uid);
2047 /* Fold alloca to the address of the array. */
2048 return fold_convert (TREE_TYPE (lhs), build_fold_addr_expr (var));
2051 /* Fold the stmt at *GSI with CCP specific information that propagating
2052 and regular folding does not catch. */
2054 static bool
2055 ccp_fold_stmt (gimple_stmt_iterator *gsi)
2057 gimple stmt = gsi_stmt (*gsi);
2059 switch (gimple_code (stmt))
2061 case GIMPLE_COND:
2063 ccp_prop_value_t val;
2064 /* Statement evaluation will handle type mismatches in constants
2065 more gracefully than the final propagation. This allows us to
2066 fold more conditionals here. */
2067 val = evaluate_stmt (stmt);
2068 if (val.lattice_val != CONSTANT
2069 || val.mask != 0)
2070 return false;
2072 if (dump_file)
2074 fprintf (dump_file, "Folding predicate ");
2075 print_gimple_expr (dump_file, stmt, 0, 0);
2076 fprintf (dump_file, " to ");
2077 print_generic_expr (dump_file, val.value, 0);
2078 fprintf (dump_file, "\n");
2081 if (integer_zerop (val.value))
2082 gimple_cond_make_false (stmt);
2083 else
2084 gimple_cond_make_true (stmt);
2086 return true;
2089 case GIMPLE_CALL:
2091 tree lhs = gimple_call_lhs (stmt);
2092 int flags = gimple_call_flags (stmt);
2093 tree val;
2094 tree argt;
2095 bool changed = false;
2096 unsigned i;
2098 /* If the call was folded into a constant make sure it goes
2099 away even if we cannot propagate into all uses because of
2100 type issues. */
2101 if (lhs
2102 && TREE_CODE (lhs) == SSA_NAME
2103 && (val = get_constant_value (lhs))
2104 /* Don't optimize away calls that have side-effects. */
2105 && (flags & (ECF_CONST|ECF_PURE)) != 0
2106 && (flags & ECF_LOOPING_CONST_OR_PURE) == 0)
2108 tree new_rhs = unshare_expr (val);
2109 bool res;
2110 if (!useless_type_conversion_p (TREE_TYPE (lhs),
2111 TREE_TYPE (new_rhs)))
2112 new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
2113 res = update_call_from_tree (gsi, new_rhs);
2114 gcc_assert (res);
2115 return true;
2118 /* Internal calls provide no argument types, so the extra laxity
2119 for normal calls does not apply. */
2120 if (gimple_call_internal_p (stmt))
2121 return false;
2123 /* The heuristic of fold_builtin_alloca_with_align differs before and
2124 after inlining, so we don't require the arg to be changed into a
2125 constant for folding, but just to be constant. */
2126 if (gimple_call_builtin_p (stmt, BUILT_IN_ALLOCA_WITH_ALIGN))
2128 tree new_rhs = fold_builtin_alloca_with_align (stmt);
2129 if (new_rhs)
2131 bool res = update_call_from_tree (gsi, new_rhs);
2132 tree var = TREE_OPERAND (TREE_OPERAND (new_rhs, 0),0);
2133 gcc_assert (res);
2134 insert_clobbers_for_var (*gsi, var);
2135 return true;
2139 /* Propagate into the call arguments. Compared to replace_uses_in
2140 this can use the argument slot types for type verification
2141 instead of the current argument type. We also can safely
2142 drop qualifiers here as we are dealing with constants anyway. */
2143 argt = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
2144 for (i = 0; i < gimple_call_num_args (stmt) && argt;
2145 ++i, argt = TREE_CHAIN (argt))
2147 tree arg = gimple_call_arg (stmt, i);
2148 if (TREE_CODE (arg) == SSA_NAME
2149 && (val = get_constant_value (arg))
2150 && useless_type_conversion_p
2151 (TYPE_MAIN_VARIANT (TREE_VALUE (argt)),
2152 TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2154 gimple_call_set_arg (stmt, i, unshare_expr (val));
2155 changed = true;
2159 return changed;
2162 case GIMPLE_ASSIGN:
2164 tree lhs = gimple_assign_lhs (stmt);
2165 tree val;
2167 /* If we have a load that turned out to be constant replace it
2168 as we cannot propagate into all uses in all cases. */
2169 if (gimple_assign_single_p (stmt)
2170 && TREE_CODE (lhs) == SSA_NAME
2171 && (val = get_constant_value (lhs)))
2173 tree rhs = unshare_expr (val);
2174 if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs)))
2175 rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (lhs), rhs);
2176 gimple_assign_set_rhs_from_tree (gsi, rhs);
2177 return true;
2180 return false;
2183 default:
2184 return false;
2188 /* Visit the assignment statement STMT. Set the value of its LHS to the
2189 value computed by the RHS and store LHS in *OUTPUT_P. If STMT
2190 creates virtual definitions, set the value of each new name to that
2191 of the RHS (if we can derive a constant out of the RHS).
2192 Value-returning call statements also perform an assignment, and
2193 are handled here. */
2195 static enum ssa_prop_result
2196 visit_assignment (gimple stmt, tree *output_p)
2198 ccp_prop_value_t val;
2199 enum ssa_prop_result retval;
2201 tree lhs = gimple_get_lhs (stmt);
2203 gcc_assert (gimple_code (stmt) != GIMPLE_CALL
2204 || gimple_call_lhs (stmt) != NULL_TREE);
2206 if (gimple_assign_single_p (stmt)
2207 && gimple_assign_rhs_code (stmt) == SSA_NAME)
2208 /* For a simple copy operation, we copy the lattice values. */
2209 val = *get_value (gimple_assign_rhs1 (stmt));
2210 else
2211 /* Evaluate the statement, which could be
2212 either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
2213 val = evaluate_stmt (stmt);
2215 retval = SSA_PROP_NOT_INTERESTING;
2217 /* Set the lattice value of the statement's output. */
2218 if (TREE_CODE (lhs) == SSA_NAME)
2220 /* If STMT is an assignment to an SSA_NAME, we only have one
2221 value to set. */
2222 if (set_lattice_value (lhs, val))
2224 *output_p = lhs;
2225 if (val.lattice_val == VARYING)
2226 retval = SSA_PROP_VARYING;
2227 else
2228 retval = SSA_PROP_INTERESTING;
2232 return retval;
2236 /* Visit the conditional statement STMT. Return SSA_PROP_INTERESTING
2237 if it can determine which edge will be taken. Otherwise, return
2238 SSA_PROP_VARYING. */
2240 static enum ssa_prop_result
2241 visit_cond_stmt (gimple stmt, edge *taken_edge_p)
2243 ccp_prop_value_t val;
2244 basic_block block;
2246 block = gimple_bb (stmt);
2247 val = evaluate_stmt (stmt);
2248 if (val.lattice_val != CONSTANT
2249 || val.mask != 0)
2250 return SSA_PROP_VARYING;
2252 /* Find which edge out of the conditional block will be taken and add it
2253 to the worklist. If no single edge can be determined statically,
2254 return SSA_PROP_VARYING to feed all the outgoing edges to the
2255 propagation engine. */
2256 *taken_edge_p = find_taken_edge (block, val.value);
2257 if (*taken_edge_p)
2258 return SSA_PROP_INTERESTING;
2259 else
2260 return SSA_PROP_VARYING;
2264 /* Evaluate statement STMT. If the statement produces an output value and
2265 its evaluation changes the lattice value of its output, return
2266 SSA_PROP_INTERESTING and set *OUTPUT_P to the SSA_NAME holding the
2267 output value.
2269 If STMT is a conditional branch and we can determine its truth
2270 value, set *TAKEN_EDGE_P accordingly. If STMT produces a varying
2271 value, return SSA_PROP_VARYING. */
2273 static enum ssa_prop_result
2274 ccp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
2276 tree def;
2277 ssa_op_iter iter;
2279 if (dump_file && (dump_flags & TDF_DETAILS))
2281 fprintf (dump_file, "\nVisiting statement:\n");
2282 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2285 switch (gimple_code (stmt))
2287 case GIMPLE_ASSIGN:
2288 /* If the statement is an assignment that produces a single
2289 output value, evaluate its RHS to see if the lattice value of
2290 its output has changed. */
2291 return visit_assignment (stmt, output_p);
2293 case GIMPLE_CALL:
2294 /* A value-returning call also performs an assignment. */
2295 if (gimple_call_lhs (stmt) != NULL_TREE)
2296 return visit_assignment (stmt, output_p);
2297 break;
2299 case GIMPLE_COND:
2300 case GIMPLE_SWITCH:
2301 /* If STMT is a conditional branch, see if we can determine
2302 which branch will be taken. */
2303 /* FIXME. It appears that we should be able to optimize
2304 computed GOTOs here as well. */
2305 return visit_cond_stmt (stmt, taken_edge_p);
2307 default:
2308 break;
2311 /* Any other kind of statement is not interesting for constant
2312 propagation and, therefore, not worth simulating. */
2313 if (dump_file && (dump_flags & TDF_DETAILS))
2314 fprintf (dump_file, "No interesting values produced. Marked VARYING.\n");
2316 /* Definitions made by statements other than assignments to
2317 SSA_NAMEs represent unknown modifications to their outputs.
2318 Mark them VARYING. */
2319 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
2321 ccp_prop_value_t v = { VARYING, NULL_TREE, -1 };
2322 set_lattice_value (def, v);
2325 return SSA_PROP_VARYING;
2329 /* Main entry point for SSA Conditional Constant Propagation. */
2331 static unsigned int
2332 do_ssa_ccp (void)
2334 unsigned int todo = 0;
2335 calculate_dominance_info (CDI_DOMINATORS);
2336 ccp_initialize ();
2337 ssa_propagate (ccp_visit_stmt, ccp_visit_phi_node);
2338 if (ccp_finalize ())
2339 todo = (TODO_cleanup_cfg | TODO_update_ssa);
2340 free_dominance_info (CDI_DOMINATORS);
2341 return todo;
2345 namespace {
2347 const pass_data pass_data_ccp =
2349 GIMPLE_PASS, /* type */
2350 "ccp", /* name */
2351 OPTGROUP_NONE, /* optinfo_flags */
2352 TV_TREE_CCP, /* tv_id */
2353 ( PROP_cfg | PROP_ssa ), /* properties_required */
2354 0, /* properties_provided */
2355 0, /* properties_destroyed */
2356 0, /* todo_flags_start */
2357 TODO_update_address_taken, /* todo_flags_finish */
2360 class pass_ccp : public gimple_opt_pass
2362 public:
2363 pass_ccp (gcc::context *ctxt)
2364 : gimple_opt_pass (pass_data_ccp, ctxt)
2367 /* opt_pass methods: */
2368 opt_pass * clone () { return new pass_ccp (m_ctxt); }
2369 virtual bool gate (function *) { return flag_tree_ccp != 0; }
2370 virtual unsigned int execute (function *) { return do_ssa_ccp (); }
2372 }; // class pass_ccp
2374 } // anon namespace
2376 gimple_opt_pass *
2377 make_pass_ccp (gcc::context *ctxt)
2379 return new pass_ccp (ctxt);
2384 /* Try to optimize out __builtin_stack_restore. Optimize it out
2385 if there is another __builtin_stack_restore in the same basic
2386 block and no calls or ASM_EXPRs are in between, or if this block's
2387 only outgoing edge is to EXIT_BLOCK and there are no calls or
2388 ASM_EXPRs after this __builtin_stack_restore. */
2390 static tree
2391 optimize_stack_restore (gimple_stmt_iterator i)
2393 tree callee;
2394 gimple stmt;
2396 basic_block bb = gsi_bb (i);
2397 gimple call = gsi_stmt (i);
2399 if (gimple_code (call) != GIMPLE_CALL
2400 || gimple_call_num_args (call) != 1
2401 || TREE_CODE (gimple_call_arg (call, 0)) != SSA_NAME
2402 || !POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))))
2403 return NULL_TREE;
2405 for (gsi_next (&i); !gsi_end_p (i); gsi_next (&i))
2407 stmt = gsi_stmt (i);
2408 if (gimple_code (stmt) == GIMPLE_ASM)
2409 return NULL_TREE;
2410 if (gimple_code (stmt) != GIMPLE_CALL)
2411 continue;
2413 callee = gimple_call_fndecl (stmt);
2414 if (!callee
2415 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2416 /* All regular builtins are ok, just obviously not alloca. */
2417 || DECL_FUNCTION_CODE (callee) == BUILT_IN_ALLOCA
2418 || DECL_FUNCTION_CODE (callee) == BUILT_IN_ALLOCA_WITH_ALIGN)
2419 return NULL_TREE;
2421 if (DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_RESTORE)
2422 goto second_stack_restore;
2425 if (!gsi_end_p (i))
2426 return NULL_TREE;
2428 /* Allow one successor of the exit block, or zero successors. */
2429 switch (EDGE_COUNT (bb->succs))
2431 case 0:
2432 break;
2433 case 1:
2434 if (single_succ_edge (bb)->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
2435 return NULL_TREE;
2436 break;
2437 default:
2438 return NULL_TREE;
2440 second_stack_restore:
2442 /* If there's exactly one use, then zap the call to __builtin_stack_save.
2443 If there are multiple uses, then the last one should remove the call.
2444 In any case, whether the call to __builtin_stack_save can be removed
2445 or not is irrelevant to removing the call to __builtin_stack_restore. */
2446 if (has_single_use (gimple_call_arg (call, 0)))
2448 gimple stack_save = SSA_NAME_DEF_STMT (gimple_call_arg (call, 0));
2449 if (is_gimple_call (stack_save))
2451 callee = gimple_call_fndecl (stack_save);
2452 if (callee
2453 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
2454 && DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_SAVE)
2456 gimple_stmt_iterator stack_save_gsi;
2457 tree rhs;
2459 stack_save_gsi = gsi_for_stmt (stack_save);
2460 rhs = build_int_cst (TREE_TYPE (gimple_call_arg (call, 0)), 0);
2461 update_call_from_tree (&stack_save_gsi, rhs);
2466 /* No effect, so the statement will be deleted. */
2467 return integer_zero_node;
2470 /* If va_list type is a simple pointer and nothing special is needed,
2471 optimize __builtin_va_start (&ap, 0) into ap = __builtin_next_arg (0),
2472 __builtin_va_end (&ap) out as NOP and __builtin_va_copy into a simple
2473 pointer assignment. */
2475 static tree
2476 optimize_stdarg_builtin (gimple call)
2478 tree callee, lhs, rhs, cfun_va_list;
2479 bool va_list_simple_ptr;
2480 location_t loc = gimple_location (call);
2482 if (gimple_code (call) != GIMPLE_CALL)
2483 return NULL_TREE;
2485 callee = gimple_call_fndecl (call);
2487 cfun_va_list = targetm.fn_abi_va_list (callee);
2488 va_list_simple_ptr = POINTER_TYPE_P (cfun_va_list)
2489 && (TREE_TYPE (cfun_va_list) == void_type_node
2490 || TREE_TYPE (cfun_va_list) == char_type_node);
2492 switch (DECL_FUNCTION_CODE (callee))
2494 case BUILT_IN_VA_START:
2495 if (!va_list_simple_ptr
2496 || targetm.expand_builtin_va_start != NULL
2497 || !builtin_decl_explicit_p (BUILT_IN_NEXT_ARG))
2498 return NULL_TREE;
2500 if (gimple_call_num_args (call) != 2)
2501 return NULL_TREE;
2503 lhs = gimple_call_arg (call, 0);
2504 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
2505 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
2506 != TYPE_MAIN_VARIANT (cfun_va_list))
2507 return NULL_TREE;
2509 lhs = build_fold_indirect_ref_loc (loc, lhs);
2510 rhs = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_NEXT_ARG),
2511 1, integer_zero_node);
2512 rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
2513 return build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, rhs);
2515 case BUILT_IN_VA_COPY:
2516 if (!va_list_simple_ptr)
2517 return NULL_TREE;
2519 if (gimple_call_num_args (call) != 2)
2520 return NULL_TREE;
2522 lhs = gimple_call_arg (call, 0);
2523 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
2524 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
2525 != TYPE_MAIN_VARIANT (cfun_va_list))
2526 return NULL_TREE;
2528 lhs = build_fold_indirect_ref_loc (loc, lhs);
2529 rhs = gimple_call_arg (call, 1);
2530 if (TYPE_MAIN_VARIANT (TREE_TYPE (rhs))
2531 != TYPE_MAIN_VARIANT (cfun_va_list))
2532 return NULL_TREE;
2534 rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
2535 return build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, rhs);
2537 case BUILT_IN_VA_END:
2538 /* No effect, so the statement will be deleted. */
2539 return integer_zero_node;
2541 default:
2542 gcc_unreachable ();
2546 /* Attemp to make the block of __builtin_unreachable I unreachable by changing
2547 the incoming jumps. Return true if at least one jump was changed. */
2549 static bool
2550 optimize_unreachable (gimple_stmt_iterator i)
2552 basic_block bb = gsi_bb (i);
2553 gimple_stmt_iterator gsi;
2554 gimple stmt;
2555 edge_iterator ei;
2556 edge e;
2557 bool ret;
2559 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2561 stmt = gsi_stmt (gsi);
2563 if (is_gimple_debug (stmt))
2564 continue;
2566 if (gimple_code (stmt) == GIMPLE_LABEL)
2568 /* Verify we do not need to preserve the label. */
2569 if (FORCED_LABEL (gimple_label_label (stmt)))
2570 return false;
2572 continue;
2575 /* Only handle the case that __builtin_unreachable is the first statement
2576 in the block. We rely on DCE to remove stmts without side-effects
2577 before __builtin_unreachable. */
2578 if (gsi_stmt (gsi) != gsi_stmt (i))
2579 return false;
2582 ret = false;
2583 FOR_EACH_EDGE (e, ei, bb->preds)
2585 gsi = gsi_last_bb (e->src);
2586 if (gsi_end_p (gsi))
2587 continue;
2589 stmt = gsi_stmt (gsi);
2590 if (gimple_code (stmt) == GIMPLE_COND)
2592 if (e->flags & EDGE_TRUE_VALUE)
2593 gimple_cond_make_false (stmt);
2594 else if (e->flags & EDGE_FALSE_VALUE)
2595 gimple_cond_make_true (stmt);
2596 else
2597 gcc_unreachable ();
2598 update_stmt (stmt);
2600 else
2602 /* Todo: handle other cases, f.i. switch statement. */
2603 continue;
2606 ret = true;
2609 return ret;
2612 /* A simple pass that attempts to fold all builtin functions. This pass
2613 is run after we've propagated as many constants as we can. */
2615 namespace {
2617 const pass_data pass_data_fold_builtins =
2619 GIMPLE_PASS, /* type */
2620 "fab", /* name */
2621 OPTGROUP_NONE, /* optinfo_flags */
2622 TV_NONE, /* tv_id */
2623 ( PROP_cfg | PROP_ssa ), /* properties_required */
2624 0, /* properties_provided */
2625 0, /* properties_destroyed */
2626 0, /* todo_flags_start */
2627 TODO_update_ssa, /* todo_flags_finish */
2630 class pass_fold_builtins : public gimple_opt_pass
2632 public:
2633 pass_fold_builtins (gcc::context *ctxt)
2634 : gimple_opt_pass (pass_data_fold_builtins, ctxt)
2637 /* opt_pass methods: */
2638 opt_pass * clone () { return new pass_fold_builtins (m_ctxt); }
2639 virtual unsigned int execute (function *);
2641 }; // class pass_fold_builtins
2643 unsigned int
2644 pass_fold_builtins::execute (function *fun)
2646 bool cfg_changed = false;
2647 basic_block bb;
2648 unsigned int todoflags = 0;
2650 FOR_EACH_BB_FN (bb, fun)
2652 gimple_stmt_iterator i;
2653 for (i = gsi_start_bb (bb); !gsi_end_p (i); )
2655 gimple stmt, old_stmt;
2656 tree callee;
2657 enum built_in_function fcode;
2659 stmt = gsi_stmt (i);
2661 if (gimple_code (stmt) != GIMPLE_CALL)
2663 /* Remove all *ssaname_N ={v} {CLOBBER}; stmts,
2664 after the last GIMPLE DSE they aren't needed and might
2665 unnecessarily keep the SSA_NAMEs live. */
2666 if (gimple_clobber_p (stmt))
2668 tree lhs = gimple_assign_lhs (stmt);
2669 if (TREE_CODE (lhs) == MEM_REF
2670 && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME)
2672 unlink_stmt_vdef (stmt);
2673 gsi_remove (&i, true);
2674 release_defs (stmt);
2675 continue;
2678 gsi_next (&i);
2679 continue;
2682 callee = gimple_call_fndecl (stmt);
2683 if (!callee || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL)
2685 gsi_next (&i);
2686 continue;
2689 fcode = DECL_FUNCTION_CODE (callee);
2690 if (fold_stmt (&i))
2692 else
2694 tree result = NULL_TREE;
2695 switch (DECL_FUNCTION_CODE (callee))
2697 case BUILT_IN_CONSTANT_P:
2698 /* Resolve __builtin_constant_p. If it hasn't been
2699 folded to integer_one_node by now, it's fairly
2700 certain that the value simply isn't constant. */
2701 result = integer_zero_node;
2702 break;
2704 case BUILT_IN_ASSUME_ALIGNED:
2705 /* Remove __builtin_assume_aligned. */
2706 result = gimple_call_arg (stmt, 0);
2707 break;
2709 case BUILT_IN_STACK_RESTORE:
2710 result = optimize_stack_restore (i);
2711 if (result)
2712 break;
2713 gsi_next (&i);
2714 continue;
2716 case BUILT_IN_UNREACHABLE:
2717 if (optimize_unreachable (i))
2718 cfg_changed = true;
2719 break;
2721 case BUILT_IN_VA_START:
2722 case BUILT_IN_VA_END:
2723 case BUILT_IN_VA_COPY:
2724 /* These shouldn't be folded before pass_stdarg. */
2725 result = optimize_stdarg_builtin (stmt);
2726 if (result)
2727 break;
2728 /* FALLTHRU */
2730 default:;
2733 if (!result)
2735 gsi_next (&i);
2736 continue;
2739 if (!update_call_from_tree (&i, result))
2740 gimplify_and_update_call_from_tree (&i, result);
2743 todoflags |= TODO_update_address_taken;
2745 if (dump_file && (dump_flags & TDF_DETAILS))
2747 fprintf (dump_file, "Simplified\n ");
2748 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2751 old_stmt = stmt;
2752 stmt = gsi_stmt (i);
2753 update_stmt (stmt);
2755 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt)
2756 && gimple_purge_dead_eh_edges (bb))
2757 cfg_changed = true;
2759 if (dump_file && (dump_flags & TDF_DETAILS))
2761 fprintf (dump_file, "to\n ");
2762 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2763 fprintf (dump_file, "\n");
2766 /* Retry the same statement if it changed into another
2767 builtin, there might be new opportunities now. */
2768 if (gimple_code (stmt) != GIMPLE_CALL)
2770 gsi_next (&i);
2771 continue;
2773 callee = gimple_call_fndecl (stmt);
2774 if (!callee
2775 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2776 || DECL_FUNCTION_CODE (callee) == fcode)
2777 gsi_next (&i);
2781 /* Delete unreachable blocks. */
2782 if (cfg_changed)
2783 todoflags |= TODO_cleanup_cfg;
2785 return todoflags;
2788 } // anon namespace
2790 gimple_opt_pass *
2791 make_pass_fold_builtins (gcc::context *ctxt)
2793 return new pass_fold_builtins (ctxt);