* tree.h (TYPE_OVERFLOW_SANITIZED): Define.
[official-gcc.git] / gcc / tree-ssa-ccp.c
blob52d85036e1ff20afb701ae0d9a90e06537658747
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 "predict.h"
130 #include "vec.h"
131 #include "hashtab.h"
132 #include "hash-set.h"
133 #include "machmode.h"
134 #include "hard-reg-set.h"
135 #include "input.h"
136 #include "function.h"
137 #include "dominance.h"
138 #include "cfg.h"
139 #include "basic-block.h"
140 #include "gimple-pretty-print.h"
141 #include "hash-table.h"
142 #include "tree-ssa-alias.h"
143 #include "internal-fn.h"
144 #include "gimple-fold.h"
145 #include "tree-eh.h"
146 #include "gimple-expr.h"
147 #include "is-a.h"
148 #include "gimple.h"
149 #include "gimplify.h"
150 #include "gimple-iterator.h"
151 #include "gimple-ssa.h"
152 #include "tree-cfg.h"
153 #include "tree-phinodes.h"
154 #include "ssa-iterators.h"
155 #include "stringpool.h"
156 #include "tree-ssanames.h"
157 #include "tree-pass.h"
158 #include "tree-ssa-propagate.h"
159 #include "value-prof.h"
160 #include "langhooks.h"
161 #include "target.h"
162 #include "diagnostic-core.h"
163 #include "dbgcnt.h"
164 #include "params.h"
165 #include "wide-int-print.h"
166 #include "builtins.h"
167 #include "tree-chkp.h"
170 /* Possible lattice values. */
171 typedef enum
173 UNINITIALIZED,
174 UNDEFINED,
175 CONSTANT,
176 VARYING
177 } ccp_lattice_t;
179 struct ccp_prop_value_t {
180 /* Lattice value. */
181 ccp_lattice_t lattice_val;
183 /* Propagated value. */
184 tree value;
186 /* Mask that applies to the propagated value during CCP. For X
187 with a CONSTANT lattice value X & ~mask == value & ~mask. The
188 zero bits in the mask cover constant values. The ones mean no
189 information. */
190 widest_int mask;
193 /* Array of propagated constant values. After propagation,
194 CONST_VAL[I].VALUE holds the constant value for SSA_NAME(I). If
195 the constant is held in an SSA name representing a memory store
196 (i.e., a VDEF), CONST_VAL[I].MEM_REF will contain the actual
197 memory reference used to store (i.e., the LHS of the assignment
198 doing the store). */
199 static ccp_prop_value_t *const_val;
200 static unsigned n_const_val;
202 static void canonicalize_value (ccp_prop_value_t *);
203 static bool ccp_fold_stmt (gimple_stmt_iterator *);
205 /* Dump constant propagation value VAL to file OUTF prefixed by PREFIX. */
207 static void
208 dump_lattice_value (FILE *outf, const char *prefix, ccp_prop_value_t val)
210 switch (val.lattice_val)
212 case UNINITIALIZED:
213 fprintf (outf, "%sUNINITIALIZED", prefix);
214 break;
215 case UNDEFINED:
216 fprintf (outf, "%sUNDEFINED", prefix);
217 break;
218 case VARYING:
219 fprintf (outf, "%sVARYING", prefix);
220 break;
221 case CONSTANT:
222 if (TREE_CODE (val.value) != INTEGER_CST
223 || val.mask == 0)
225 fprintf (outf, "%sCONSTANT ", prefix);
226 print_generic_expr (outf, val.value, dump_flags);
228 else
230 widest_int cval = wi::bit_and_not (wi::to_widest (val.value),
231 val.mask);
232 fprintf (outf, "%sCONSTANT ", prefix);
233 print_hex (cval, outf);
234 fprintf (outf, " (");
235 print_hex (val.mask, outf);
236 fprintf (outf, ")");
238 break;
239 default:
240 gcc_unreachable ();
245 /* Print lattice value VAL to stderr. */
247 void debug_lattice_value (ccp_prop_value_t val);
249 DEBUG_FUNCTION void
250 debug_lattice_value (ccp_prop_value_t val)
252 dump_lattice_value (stderr, "", val);
253 fprintf (stderr, "\n");
256 /* Extend NONZERO_BITS to a full mask, with the upper bits being set. */
258 static widest_int
259 extend_mask (const wide_int &nonzero_bits)
261 return (wi::mask <widest_int> (wi::get_precision (nonzero_bits), true)
262 | widest_int::from (nonzero_bits, UNSIGNED));
265 /* Compute a default value for variable VAR and store it in the
266 CONST_VAL array. The following rules are used to get default
267 values:
269 1- Global and static variables that are declared constant are
270 considered CONSTANT.
272 2- Any other value is considered UNDEFINED. This is useful when
273 considering PHI nodes. PHI arguments that are undefined do not
274 change the constant value of the PHI node, which allows for more
275 constants to be propagated.
277 3- Variables defined by statements other than assignments and PHI
278 nodes are considered VARYING.
280 4- Initial values of variables that are not GIMPLE registers are
281 considered VARYING. */
283 static ccp_prop_value_t
284 get_default_value (tree var)
286 ccp_prop_value_t val = { UNINITIALIZED, NULL_TREE, 0 };
287 gimple stmt;
289 stmt = SSA_NAME_DEF_STMT (var);
291 if (gimple_nop_p (stmt))
293 /* Variables defined by an empty statement are those used
294 before being initialized. If VAR is a local variable, we
295 can assume initially that it is UNDEFINED, otherwise we must
296 consider it VARYING. */
297 if (!virtual_operand_p (var)
298 && TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
299 val.lattice_val = UNDEFINED;
300 else
302 val.lattice_val = VARYING;
303 val.mask = -1;
304 if (flag_tree_bit_ccp)
306 wide_int nonzero_bits = get_nonzero_bits (var);
307 if (nonzero_bits != -1)
309 val.lattice_val = CONSTANT;
310 val.value = build_zero_cst (TREE_TYPE (var));
311 val.mask = extend_mask (nonzero_bits);
316 else if (is_gimple_assign (stmt))
318 tree cst;
319 if (gimple_assign_single_p (stmt)
320 && DECL_P (gimple_assign_rhs1 (stmt))
321 && (cst = get_symbol_constant_value (gimple_assign_rhs1 (stmt))))
323 val.lattice_val = CONSTANT;
324 val.value = cst;
326 else
328 /* Any other variable defined by an assignment is considered
329 UNDEFINED. */
330 val.lattice_val = UNDEFINED;
333 else if ((is_gimple_call (stmt)
334 && gimple_call_lhs (stmt) != NULL_TREE)
335 || gimple_code (stmt) == GIMPLE_PHI)
337 /* A variable defined by a call or a PHI node is considered
338 UNDEFINED. */
339 val.lattice_val = UNDEFINED;
341 else
343 /* Otherwise, VAR will never take on a constant value. */
344 val.lattice_val = VARYING;
345 val.mask = -1;
348 return val;
352 /* Get the constant value associated with variable VAR. */
354 static inline ccp_prop_value_t *
355 get_value (tree var)
357 ccp_prop_value_t *val;
359 if (const_val == NULL
360 || SSA_NAME_VERSION (var) >= n_const_val)
361 return NULL;
363 val = &const_val[SSA_NAME_VERSION (var)];
364 if (val->lattice_val == UNINITIALIZED)
365 *val = get_default_value (var);
367 canonicalize_value (val);
369 return val;
372 /* Return the constant tree value associated with VAR. */
374 static inline tree
375 get_constant_value (tree var)
377 ccp_prop_value_t *val;
378 if (TREE_CODE (var) != SSA_NAME)
380 if (is_gimple_min_invariant (var))
381 return var;
382 return NULL_TREE;
384 val = get_value (var);
385 if (val
386 && val->lattice_val == CONSTANT
387 && (TREE_CODE (val->value) != INTEGER_CST
388 || val->mask == 0))
389 return val->value;
390 return NULL_TREE;
393 /* Sets the value associated with VAR to VARYING. */
395 static inline void
396 set_value_varying (tree var)
398 ccp_prop_value_t *val = &const_val[SSA_NAME_VERSION (var)];
400 val->lattice_val = VARYING;
401 val->value = NULL_TREE;
402 val->mask = -1;
405 /* For float types, modify the value of VAL to make ccp work correctly
406 for non-standard values (-0, NaN):
408 If HONOR_SIGNED_ZEROS is false, and VAL = -0, we canonicalize it to 0.
409 If HONOR_NANS is false, and VAL is NaN, we canonicalize it to UNDEFINED.
410 This is to fix the following problem (see PR 29921): Suppose we have
412 x = 0.0 * y
414 and we set value of y to NaN. This causes value of x to be set to NaN.
415 When we later determine that y is in fact VARYING, fold uses the fact
416 that HONOR_NANS is false, and we try to change the value of x to 0,
417 causing an ICE. With HONOR_NANS being false, the real appearance of
418 NaN would cause undefined behavior, though, so claiming that y (and x)
419 are UNDEFINED initially is correct.
421 For other constants, make sure to drop TREE_OVERFLOW. */
423 static void
424 canonicalize_value (ccp_prop_value_t *val)
426 machine_mode mode;
427 tree type;
428 REAL_VALUE_TYPE d;
430 if (val->lattice_val != CONSTANT)
431 return;
433 if (TREE_OVERFLOW_P (val->value))
434 val->value = drop_tree_overflow (val->value);
436 if (TREE_CODE (val->value) != REAL_CST)
437 return;
439 d = TREE_REAL_CST (val->value);
440 type = TREE_TYPE (val->value);
441 mode = TYPE_MODE (type);
443 if (!HONOR_SIGNED_ZEROS (mode)
444 && REAL_VALUE_MINUS_ZERO (d))
446 val->value = build_real (type, dconst0);
447 return;
450 if (!HONOR_NANS (mode)
451 && REAL_VALUE_ISNAN (d))
453 val->lattice_val = UNDEFINED;
454 val->value = NULL;
455 return;
459 /* Return whether the lattice transition is valid. */
461 static bool
462 valid_lattice_transition (ccp_prop_value_t old_val, ccp_prop_value_t new_val)
464 /* Lattice transitions must always be monotonically increasing in
465 value. */
466 if (old_val.lattice_val < new_val.lattice_val)
467 return true;
469 if (old_val.lattice_val != new_val.lattice_val)
470 return false;
472 if (!old_val.value && !new_val.value)
473 return true;
475 /* Now both lattice values are CONSTANT. */
477 /* Allow transitioning from PHI <&x, not executable> == &x
478 to PHI <&x, &y> == common alignment. */
479 if (TREE_CODE (old_val.value) != INTEGER_CST
480 && TREE_CODE (new_val.value) == INTEGER_CST)
481 return true;
483 /* Bit-lattices have to agree in the still valid bits. */
484 if (TREE_CODE (old_val.value) == INTEGER_CST
485 && TREE_CODE (new_val.value) == INTEGER_CST)
486 return (wi::bit_and_not (wi::to_widest (old_val.value), new_val.mask)
487 == wi::bit_and_not (wi::to_widest (new_val.value), new_val.mask));
489 /* Otherwise constant values have to agree. */
490 return operand_equal_p (old_val.value, new_val.value, 0);
493 /* Set the value for variable VAR to NEW_VAL. Return true if the new
494 value is different from VAR's previous value. */
496 static bool
497 set_lattice_value (tree var, ccp_prop_value_t new_val)
499 /* We can deal with old UNINITIALIZED values just fine here. */
500 ccp_prop_value_t *old_val = &const_val[SSA_NAME_VERSION (var)];
502 canonicalize_value (&new_val);
504 /* We have to be careful to not go up the bitwise lattice
505 represented by the mask.
506 ??? This doesn't seem to be the best place to enforce this. */
507 if (new_val.lattice_val == CONSTANT
508 && old_val->lattice_val == CONSTANT
509 && TREE_CODE (new_val.value) == INTEGER_CST
510 && TREE_CODE (old_val->value) == INTEGER_CST)
512 widest_int diff = (wi::to_widest (new_val.value)
513 ^ wi::to_widest (old_val->value));
514 new_val.mask = new_val.mask | old_val->mask | diff;
517 gcc_assert (valid_lattice_transition (*old_val, new_val));
519 /* If *OLD_VAL and NEW_VAL are the same, return false to inform the
520 caller that this was a non-transition. */
521 if (old_val->lattice_val != new_val.lattice_val
522 || (new_val.lattice_val == CONSTANT
523 && TREE_CODE (new_val.value) == INTEGER_CST
524 && (TREE_CODE (old_val->value) != INTEGER_CST
525 || new_val.mask != old_val->mask)))
527 /* ??? We would like to delay creation of INTEGER_CSTs from
528 partially constants here. */
530 if (dump_file && (dump_flags & TDF_DETAILS))
532 dump_lattice_value (dump_file, "Lattice value changed to ", new_val);
533 fprintf (dump_file, ". Adding SSA edges to worklist.\n");
536 *old_val = new_val;
538 gcc_assert (new_val.lattice_val != UNINITIALIZED);
539 return true;
542 return false;
545 static ccp_prop_value_t get_value_for_expr (tree, bool);
546 static ccp_prop_value_t bit_value_binop (enum tree_code, tree, tree, tree);
547 static void bit_value_binop_1 (enum tree_code, tree, widest_int *, widest_int *,
548 tree, const widest_int &, const widest_int &,
549 tree, const widest_int &, const widest_int &);
551 /* Return a widest_int that can be used for bitwise simplifications
552 from VAL. */
554 static widest_int
555 value_to_wide_int (ccp_prop_value_t val)
557 if (val.value
558 && TREE_CODE (val.value) == INTEGER_CST)
559 return wi::to_widest (val.value);
561 return 0;
564 /* Return the value for the address expression EXPR based on alignment
565 information. */
567 static ccp_prop_value_t
568 get_value_from_alignment (tree expr)
570 tree type = TREE_TYPE (expr);
571 ccp_prop_value_t val;
572 unsigned HOST_WIDE_INT bitpos;
573 unsigned int align;
575 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
577 get_pointer_alignment_1 (expr, &align, &bitpos);
578 val.mask = (POINTER_TYPE_P (type) || TYPE_UNSIGNED (type)
579 ? wi::mask <widest_int> (TYPE_PRECISION (type), false)
580 : -1).and_not (align / BITS_PER_UNIT - 1);
581 val.lattice_val = val.mask == -1 ? VARYING : CONSTANT;
582 if (val.lattice_val == CONSTANT)
583 val.value = build_int_cstu (type, bitpos / BITS_PER_UNIT);
584 else
585 val.value = NULL_TREE;
587 return val;
590 /* Return the value for the tree operand EXPR. If FOR_BITS_P is true
591 return constant bits extracted from alignment information for
592 invariant addresses. */
594 static ccp_prop_value_t
595 get_value_for_expr (tree expr, bool for_bits_p)
597 ccp_prop_value_t val;
599 if (TREE_CODE (expr) == SSA_NAME)
601 val = *get_value (expr);
602 if (for_bits_p
603 && val.lattice_val == CONSTANT
604 && TREE_CODE (val.value) == ADDR_EXPR)
605 val = get_value_from_alignment (val.value);
607 else if (is_gimple_min_invariant (expr)
608 && (!for_bits_p || TREE_CODE (expr) != ADDR_EXPR))
610 val.lattice_val = CONSTANT;
611 val.value = expr;
612 val.mask = 0;
613 canonicalize_value (&val);
615 else if (TREE_CODE (expr) == ADDR_EXPR)
616 val = get_value_from_alignment (expr);
617 else
619 val.lattice_val = VARYING;
620 val.mask = -1;
621 val.value = NULL_TREE;
623 return val;
626 /* Return the likely CCP lattice value for STMT.
628 If STMT has no operands, then return CONSTANT.
630 Else if undefinedness of operands of STMT cause its value to be
631 undefined, then return UNDEFINED.
633 Else if any operands of STMT are constants, then return CONSTANT.
635 Else return VARYING. */
637 static ccp_lattice_t
638 likely_value (gimple stmt)
640 bool has_constant_operand, has_undefined_operand, all_undefined_operands;
641 tree use;
642 ssa_op_iter iter;
643 unsigned i;
645 enum gimple_code code = gimple_code (stmt);
647 /* This function appears to be called only for assignments, calls,
648 conditionals, and switches, due to the logic in visit_stmt. */
649 gcc_assert (code == GIMPLE_ASSIGN
650 || code == GIMPLE_CALL
651 || code == GIMPLE_COND
652 || code == GIMPLE_SWITCH);
654 /* If the statement has volatile operands, it won't fold to a
655 constant value. */
656 if (gimple_has_volatile_ops (stmt))
657 return VARYING;
659 /* Arrive here for more complex cases. */
660 has_constant_operand = false;
661 has_undefined_operand = false;
662 all_undefined_operands = true;
663 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
665 ccp_prop_value_t *val = get_value (use);
667 if (val->lattice_val == UNDEFINED)
668 has_undefined_operand = true;
669 else
670 all_undefined_operands = false;
672 if (val->lattice_val == CONSTANT)
673 has_constant_operand = true;
676 /* There may be constants in regular rhs operands. For calls we
677 have to ignore lhs, fndecl and static chain, otherwise only
678 the lhs. */
679 for (i = (is_gimple_call (stmt) ? 2 : 0) + gimple_has_lhs (stmt);
680 i < gimple_num_ops (stmt); ++i)
682 tree op = gimple_op (stmt, i);
683 if (!op || TREE_CODE (op) == SSA_NAME)
684 continue;
685 if (is_gimple_min_invariant (op))
686 has_constant_operand = true;
689 if (has_constant_operand)
690 all_undefined_operands = false;
692 if (has_undefined_operand
693 && code == GIMPLE_CALL
694 && gimple_call_internal_p (stmt))
695 switch (gimple_call_internal_fn (stmt))
697 /* These 3 builtins use the first argument just as a magic
698 way how to find out a decl uid. */
699 case IFN_GOMP_SIMD_LANE:
700 case IFN_GOMP_SIMD_VF:
701 case IFN_GOMP_SIMD_LAST_LANE:
702 has_undefined_operand = false;
703 break;
704 default:
705 break;
708 /* If the operation combines operands like COMPLEX_EXPR make sure to
709 not mark the result UNDEFINED if only one part of the result is
710 undefined. */
711 if (has_undefined_operand && all_undefined_operands)
712 return UNDEFINED;
713 else if (code == GIMPLE_ASSIGN && has_undefined_operand)
715 switch (gimple_assign_rhs_code (stmt))
717 /* Unary operators are handled with all_undefined_operands. */
718 case PLUS_EXPR:
719 case MINUS_EXPR:
720 case POINTER_PLUS_EXPR:
721 /* Not MIN_EXPR, MAX_EXPR. One VARYING operand may be selected.
722 Not bitwise operators, one VARYING operand may specify the
723 result completely. Not logical operators for the same reason.
724 Not COMPLEX_EXPR as one VARYING operand makes the result partly
725 not UNDEFINED. Not *DIV_EXPR, comparisons and shifts because
726 the undefined operand may be promoted. */
727 return UNDEFINED;
729 case ADDR_EXPR:
730 /* If any part of an address is UNDEFINED, like the index
731 of an ARRAY_EXPR, then treat the result as UNDEFINED. */
732 return UNDEFINED;
734 default:
738 /* If there was an UNDEFINED operand but the result may be not UNDEFINED
739 fall back to CONSTANT. During iteration UNDEFINED may still drop
740 to CONSTANT. */
741 if (has_undefined_operand)
742 return CONSTANT;
744 /* We do not consider virtual operands here -- load from read-only
745 memory may have only VARYING virtual operands, but still be
746 constant. */
747 if (has_constant_operand
748 || gimple_references_memory_p (stmt))
749 return CONSTANT;
751 return VARYING;
754 /* Returns true if STMT cannot be constant. */
756 static bool
757 surely_varying_stmt_p (gimple stmt)
759 /* If the statement has operands that we cannot handle, it cannot be
760 constant. */
761 if (gimple_has_volatile_ops (stmt))
762 return true;
764 /* If it is a call and does not return a value or is not a
765 builtin and not an indirect call or a call to function with
766 assume_aligned/alloc_align attribute, it is varying. */
767 if (is_gimple_call (stmt))
769 tree fndecl, fntype = gimple_call_fntype (stmt);
770 if (!gimple_call_lhs (stmt)
771 || ((fndecl = gimple_call_fndecl (stmt)) != NULL_TREE
772 && !DECL_BUILT_IN (fndecl)
773 && !lookup_attribute ("assume_aligned",
774 TYPE_ATTRIBUTES (fntype))
775 && !lookup_attribute ("alloc_align",
776 TYPE_ATTRIBUTES (fntype))))
777 return true;
780 /* Any other store operation is not interesting. */
781 else if (gimple_vdef (stmt))
782 return true;
784 /* Anything other than assignments and conditional jumps are not
785 interesting for CCP. */
786 if (gimple_code (stmt) != GIMPLE_ASSIGN
787 && gimple_code (stmt) != GIMPLE_COND
788 && gimple_code (stmt) != GIMPLE_SWITCH
789 && gimple_code (stmt) != GIMPLE_CALL)
790 return true;
792 return false;
795 /* Initialize local data structures for CCP. */
797 static void
798 ccp_initialize (void)
800 basic_block bb;
802 n_const_val = num_ssa_names;
803 const_val = XCNEWVEC (ccp_prop_value_t, n_const_val);
805 /* Initialize simulation flags for PHI nodes and statements. */
806 FOR_EACH_BB_FN (bb, cfun)
808 gimple_stmt_iterator i;
810 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
812 gimple stmt = gsi_stmt (i);
813 bool is_varying;
815 /* If the statement is a control insn, then we do not
816 want to avoid simulating the statement once. Failure
817 to do so means that those edges will never get added. */
818 if (stmt_ends_bb_p (stmt))
819 is_varying = false;
820 else
821 is_varying = surely_varying_stmt_p (stmt);
823 if (is_varying)
825 tree def;
826 ssa_op_iter iter;
828 /* If the statement will not produce a constant, mark
829 all its outputs VARYING. */
830 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
831 set_value_varying (def);
833 prop_set_simulate_again (stmt, !is_varying);
837 /* Now process PHI nodes. We never clear the simulate_again flag on
838 phi nodes, since we do not know which edges are executable yet,
839 except for phi nodes for virtual operands when we do not do store ccp. */
840 FOR_EACH_BB_FN (bb, cfun)
842 gimple_stmt_iterator i;
844 for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
846 gimple phi = gsi_stmt (i);
848 if (virtual_operand_p (gimple_phi_result (phi)))
849 prop_set_simulate_again (phi, false);
850 else
851 prop_set_simulate_again (phi, true);
856 /* Debug count support. Reset the values of ssa names
857 VARYING when the total number ssa names analyzed is
858 beyond the debug count specified. */
860 static void
861 do_dbg_cnt (void)
863 unsigned i;
864 for (i = 0; i < num_ssa_names; i++)
866 if (!dbg_cnt (ccp))
868 const_val[i].lattice_val = VARYING;
869 const_val[i].mask = -1;
870 const_val[i].value = NULL_TREE;
876 /* Do final substitution of propagated values, cleanup the flowgraph and
877 free allocated storage.
879 Return TRUE when something was optimized. */
881 static bool
882 ccp_finalize (void)
884 bool something_changed;
885 unsigned i;
887 do_dbg_cnt ();
889 /* Derive alignment and misalignment information from partially
890 constant pointers in the lattice or nonzero bits from partially
891 constant integers. */
892 for (i = 1; i < num_ssa_names; ++i)
894 tree name = ssa_name (i);
895 ccp_prop_value_t *val;
896 unsigned int tem, align;
898 if (!name
899 || (!POINTER_TYPE_P (TREE_TYPE (name))
900 && (!INTEGRAL_TYPE_P (TREE_TYPE (name))
901 /* Don't record nonzero bits before IPA to avoid
902 using too much memory. */
903 || first_pass_instance)))
904 continue;
906 val = get_value (name);
907 if (val->lattice_val != CONSTANT
908 || TREE_CODE (val->value) != INTEGER_CST)
909 continue;
911 if (POINTER_TYPE_P (TREE_TYPE (name)))
913 /* Trailing mask bits specify the alignment, trailing value
914 bits the misalignment. */
915 tem = val->mask.to_uhwi ();
916 align = (tem & -tem);
917 if (align > 1)
918 set_ptr_info_alignment (get_ptr_info (name), align,
919 (TREE_INT_CST_LOW (val->value)
920 & (align - 1)));
922 else
924 unsigned int precision = TYPE_PRECISION (TREE_TYPE (val->value));
925 wide_int nonzero_bits = wide_int::from (val->mask, precision,
926 UNSIGNED) | val->value;
927 nonzero_bits &= get_nonzero_bits (name);
928 set_nonzero_bits (name, nonzero_bits);
932 /* Perform substitutions based on the known constant values. */
933 something_changed = substitute_and_fold (get_constant_value,
934 ccp_fold_stmt, true);
936 free (const_val);
937 const_val = NULL;
938 return something_changed;;
942 /* Compute the meet operator between *VAL1 and *VAL2. Store the result
943 in VAL1.
945 any M UNDEFINED = any
946 any M VARYING = VARYING
947 Ci M Cj = Ci if (i == j)
948 Ci M Cj = VARYING if (i != j)
951 static void
952 ccp_lattice_meet (ccp_prop_value_t *val1, ccp_prop_value_t *val2)
954 if (val1->lattice_val == UNDEFINED)
956 /* UNDEFINED M any = any */
957 *val1 = *val2;
959 else if (val2->lattice_val == UNDEFINED)
961 /* any M UNDEFINED = any
962 Nothing to do. VAL1 already contains the value we want. */
965 else if (val1->lattice_val == VARYING
966 || val2->lattice_val == VARYING)
968 /* any M VARYING = VARYING. */
969 val1->lattice_val = VARYING;
970 val1->mask = -1;
971 val1->value = NULL_TREE;
973 else if (val1->lattice_val == CONSTANT
974 && val2->lattice_val == CONSTANT
975 && TREE_CODE (val1->value) == INTEGER_CST
976 && TREE_CODE (val2->value) == INTEGER_CST)
978 /* Ci M Cj = Ci if (i == j)
979 Ci M Cj = VARYING if (i != j)
981 For INTEGER_CSTs mask unequal bits. If no equal bits remain,
982 drop to varying. */
983 val1->mask = (val1->mask | val2->mask
984 | (wi::to_widest (val1->value)
985 ^ wi::to_widest (val2->value)));
986 if (val1->mask == -1)
988 val1->lattice_val = VARYING;
989 val1->value = NULL_TREE;
992 else if (val1->lattice_val == CONSTANT
993 && val2->lattice_val == CONSTANT
994 && simple_cst_equal (val1->value, val2->value) == 1)
996 /* Ci M Cj = Ci if (i == j)
997 Ci M Cj = VARYING if (i != j)
999 VAL1 already contains the value we want for equivalent values. */
1001 else if (val1->lattice_val == CONSTANT
1002 && val2->lattice_val == CONSTANT
1003 && (TREE_CODE (val1->value) == ADDR_EXPR
1004 || TREE_CODE (val2->value) == ADDR_EXPR))
1006 /* When not equal addresses are involved try meeting for
1007 alignment. */
1008 ccp_prop_value_t tem = *val2;
1009 if (TREE_CODE (val1->value) == ADDR_EXPR)
1010 *val1 = get_value_for_expr (val1->value, true);
1011 if (TREE_CODE (val2->value) == ADDR_EXPR)
1012 tem = get_value_for_expr (val2->value, true);
1013 ccp_lattice_meet (val1, &tem);
1015 else
1017 /* Any other combination is VARYING. */
1018 val1->lattice_val = VARYING;
1019 val1->mask = -1;
1020 val1->value = NULL_TREE;
1025 /* Loop through the PHI_NODE's parameters for BLOCK and compare their
1026 lattice values to determine PHI_NODE's lattice value. The value of a
1027 PHI node is determined calling ccp_lattice_meet with all the arguments
1028 of the PHI node that are incoming via executable edges. */
1030 static enum ssa_prop_result
1031 ccp_visit_phi_node (gimple phi)
1033 unsigned i;
1034 ccp_prop_value_t *old_val, new_val;
1036 if (dump_file && (dump_flags & TDF_DETAILS))
1038 fprintf (dump_file, "\nVisiting PHI node: ");
1039 print_gimple_stmt (dump_file, phi, 0, dump_flags);
1042 old_val = get_value (gimple_phi_result (phi));
1043 switch (old_val->lattice_val)
1045 case VARYING:
1046 return SSA_PROP_VARYING;
1048 case CONSTANT:
1049 new_val = *old_val;
1050 break;
1052 case UNDEFINED:
1053 new_val.lattice_val = UNDEFINED;
1054 new_val.value = NULL_TREE;
1055 break;
1057 default:
1058 gcc_unreachable ();
1061 for (i = 0; i < gimple_phi_num_args (phi); i++)
1063 /* Compute the meet operator over all the PHI arguments flowing
1064 through executable edges. */
1065 edge e = gimple_phi_arg_edge (phi, i);
1067 if (dump_file && (dump_flags & TDF_DETAILS))
1069 fprintf (dump_file,
1070 "\n Argument #%d (%d -> %d %sexecutable)\n",
1071 i, e->src->index, e->dest->index,
1072 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
1075 /* If the incoming edge is executable, Compute the meet operator for
1076 the existing value of the PHI node and the current PHI argument. */
1077 if (e->flags & EDGE_EXECUTABLE)
1079 tree arg = gimple_phi_arg (phi, i)->def;
1080 ccp_prop_value_t arg_val = get_value_for_expr (arg, false);
1082 ccp_lattice_meet (&new_val, &arg_val);
1084 if (dump_file && (dump_flags & TDF_DETAILS))
1086 fprintf (dump_file, "\t");
1087 print_generic_expr (dump_file, arg, dump_flags);
1088 dump_lattice_value (dump_file, "\tValue: ", arg_val);
1089 fprintf (dump_file, "\n");
1092 if (new_val.lattice_val == VARYING)
1093 break;
1097 if (dump_file && (dump_flags & TDF_DETAILS))
1099 dump_lattice_value (dump_file, "\n PHI node value: ", new_val);
1100 fprintf (dump_file, "\n\n");
1103 /* Make the transition to the new value. */
1104 if (set_lattice_value (gimple_phi_result (phi), new_val))
1106 if (new_val.lattice_val == VARYING)
1107 return SSA_PROP_VARYING;
1108 else
1109 return SSA_PROP_INTERESTING;
1111 else
1112 return SSA_PROP_NOT_INTERESTING;
1115 /* Return the constant value for OP or OP otherwise. */
1117 static tree
1118 valueize_op (tree op)
1120 if (TREE_CODE (op) == SSA_NAME)
1122 tree tem = get_constant_value (op);
1123 if (tem)
1124 return tem;
1126 return op;
1129 /* CCP specific front-end to the non-destructive constant folding
1130 routines.
1132 Attempt to simplify the RHS of STMT knowing that one or more
1133 operands are constants.
1135 If simplification is possible, return the simplified RHS,
1136 otherwise return the original RHS or NULL_TREE. */
1138 static tree
1139 ccp_fold (gimple stmt)
1141 location_t loc = gimple_location (stmt);
1142 switch (gimple_code (stmt))
1144 case GIMPLE_COND:
1146 /* Handle comparison operators that can appear in GIMPLE form. */
1147 tree op0 = valueize_op (gimple_cond_lhs (stmt));
1148 tree op1 = valueize_op (gimple_cond_rhs (stmt));
1149 enum tree_code code = gimple_cond_code (stmt);
1150 return fold_binary_loc (loc, code, boolean_type_node, op0, op1);
1153 case GIMPLE_SWITCH:
1155 /* Return the constant switch index. */
1156 return valueize_op (gimple_switch_index (stmt));
1159 case GIMPLE_ASSIGN:
1160 case GIMPLE_CALL:
1161 return gimple_fold_stmt_to_constant_1 (stmt, valueize_op);
1163 default:
1164 gcc_unreachable ();
1168 /* Apply the operation CODE in type TYPE to the value, mask pair
1169 RVAL and RMASK representing a value of type RTYPE and set
1170 the value, mask pair *VAL and *MASK to the result. */
1172 static void
1173 bit_value_unop_1 (enum tree_code code, tree type,
1174 widest_int *val, widest_int *mask,
1175 tree rtype, const widest_int &rval, const widest_int &rmask)
1177 switch (code)
1179 case BIT_NOT_EXPR:
1180 *mask = rmask;
1181 *val = ~rval;
1182 break;
1184 case NEGATE_EXPR:
1186 widest_int temv, temm;
1187 /* Return ~rval + 1. */
1188 bit_value_unop_1 (BIT_NOT_EXPR, type, &temv, &temm, type, rval, rmask);
1189 bit_value_binop_1 (PLUS_EXPR, type, val, mask,
1190 type, temv, temm, type, 1, 0);
1191 break;
1194 CASE_CONVERT:
1196 signop sgn;
1198 /* First extend mask and value according to the original type. */
1199 sgn = TYPE_SIGN (rtype);
1200 *mask = wi::ext (rmask, TYPE_PRECISION (rtype), sgn);
1201 *val = wi::ext (rval, TYPE_PRECISION (rtype), sgn);
1203 /* Then extend mask and value according to the target type. */
1204 sgn = TYPE_SIGN (type);
1205 *mask = wi::ext (*mask, TYPE_PRECISION (type), sgn);
1206 *val = wi::ext (*val, TYPE_PRECISION (type), sgn);
1207 break;
1210 default:
1211 *mask = -1;
1212 break;
1216 /* Apply the operation CODE in type TYPE to the value, mask pairs
1217 R1VAL, R1MASK and R2VAL, R2MASK representing a values of type R1TYPE
1218 and R2TYPE and set the value, mask pair *VAL and *MASK to the result. */
1220 static void
1221 bit_value_binop_1 (enum tree_code code, tree type,
1222 widest_int *val, widest_int *mask,
1223 tree r1type, const widest_int &r1val,
1224 const widest_int &r1mask, tree r2type,
1225 const widest_int &r2val, const widest_int &r2mask)
1227 signop sgn = TYPE_SIGN (type);
1228 int width = TYPE_PRECISION (type);
1229 bool swap_p = false;
1231 /* Assume we'll get a constant result. Use an initial non varying
1232 value, we fall back to varying in the end if necessary. */
1233 *mask = -1;
1235 switch (code)
1237 case BIT_AND_EXPR:
1238 /* The mask is constant where there is a known not
1239 set bit, (m1 | m2) & ((v1 | m1) & (v2 | m2)) */
1240 *mask = (r1mask | r2mask) & (r1val | r1mask) & (r2val | r2mask);
1241 *val = r1val & r2val;
1242 break;
1244 case BIT_IOR_EXPR:
1245 /* The mask is constant where there is a known
1246 set bit, (m1 | m2) & ~((v1 & ~m1) | (v2 & ~m2)). */
1247 *mask = (r1mask | r2mask)
1248 .and_not (r1val.and_not (r1mask) | r2val.and_not (r2mask));
1249 *val = r1val | r2val;
1250 break;
1252 case BIT_XOR_EXPR:
1253 /* m1 | m2 */
1254 *mask = r1mask | r2mask;
1255 *val = r1val ^ r2val;
1256 break;
1258 case LROTATE_EXPR:
1259 case RROTATE_EXPR:
1260 if (r2mask == 0)
1262 widest_int shift = r2val;
1263 if (shift == 0)
1265 *mask = r1mask;
1266 *val = r1val;
1268 else
1270 if (wi::neg_p (shift))
1272 shift = -shift;
1273 if (code == RROTATE_EXPR)
1274 code = LROTATE_EXPR;
1275 else
1276 code = RROTATE_EXPR;
1278 if (code == RROTATE_EXPR)
1280 *mask = wi::rrotate (r1mask, shift, width);
1281 *val = wi::rrotate (r1val, shift, width);
1283 else
1285 *mask = wi::lrotate (r1mask, shift, width);
1286 *val = wi::lrotate (r1val, shift, width);
1290 break;
1292 case LSHIFT_EXPR:
1293 case RSHIFT_EXPR:
1294 /* ??? We can handle partially known shift counts if we know
1295 its sign. That way we can tell that (x << (y | 8)) & 255
1296 is zero. */
1297 if (r2mask == 0)
1299 widest_int shift = r2val;
1300 if (shift == 0)
1302 *mask = r1mask;
1303 *val = r1val;
1305 else
1307 if (wi::neg_p (shift))
1309 shift = -shift;
1310 if (code == RSHIFT_EXPR)
1311 code = LSHIFT_EXPR;
1312 else
1313 code = RSHIFT_EXPR;
1315 if (code == RSHIFT_EXPR)
1317 *mask = wi::rshift (wi::ext (r1mask, width, sgn), shift, sgn);
1318 *val = wi::rshift (wi::ext (r1val, width, sgn), shift, sgn);
1320 else
1322 *mask = wi::ext (wi::lshift (r1mask, shift), width, sgn);
1323 *val = wi::ext (wi::lshift (r1val, shift), width, sgn);
1327 break;
1329 case PLUS_EXPR:
1330 case POINTER_PLUS_EXPR:
1332 /* Do the addition with unknown bits set to zero, to give carry-ins of
1333 zero wherever possible. */
1334 widest_int lo = r1val.and_not (r1mask) + r2val.and_not (r2mask);
1335 lo = wi::ext (lo, width, sgn);
1336 /* Do the addition with unknown bits set to one, to give carry-ins of
1337 one wherever possible. */
1338 widest_int hi = (r1val | r1mask) + (r2val | r2mask);
1339 hi = wi::ext (hi, width, sgn);
1340 /* Each bit in the result is known if (a) the corresponding bits in
1341 both inputs are known, and (b) the carry-in to that bit position
1342 is known. We can check condition (b) by seeing if we got the same
1343 result with minimised carries as with maximised carries. */
1344 *mask = r1mask | r2mask | (lo ^ hi);
1345 *mask = wi::ext (*mask, width, sgn);
1346 /* It shouldn't matter whether we choose lo or hi here. */
1347 *val = lo;
1348 break;
1351 case MINUS_EXPR:
1353 widest_int temv, temm;
1354 bit_value_unop_1 (NEGATE_EXPR, r2type, &temv, &temm,
1355 r2type, r2val, r2mask);
1356 bit_value_binop_1 (PLUS_EXPR, type, val, mask,
1357 r1type, r1val, r1mask,
1358 r2type, temv, temm);
1359 break;
1362 case MULT_EXPR:
1364 /* Just track trailing zeros in both operands and transfer
1365 them to the other. */
1366 int r1tz = wi::ctz (r1val | r1mask);
1367 int r2tz = wi::ctz (r2val | r2mask);
1368 if (r1tz + r2tz >= width)
1370 *mask = 0;
1371 *val = 0;
1373 else if (r1tz + r2tz > 0)
1375 *mask = wi::ext (wi::mask <widest_int> (r1tz + r2tz, true),
1376 width, sgn);
1377 *val = 0;
1379 break;
1382 case EQ_EXPR:
1383 case NE_EXPR:
1385 widest_int m = r1mask | r2mask;
1386 if (r1val.and_not (m) != r2val.and_not (m))
1388 *mask = 0;
1389 *val = ((code == EQ_EXPR) ? 0 : 1);
1391 else
1393 /* We know the result of a comparison is always one or zero. */
1394 *mask = 1;
1395 *val = 0;
1397 break;
1400 case GE_EXPR:
1401 case GT_EXPR:
1402 swap_p = true;
1403 code = swap_tree_comparison (code);
1404 /* Fall through. */
1405 case LT_EXPR:
1406 case LE_EXPR:
1408 int minmax, maxmin;
1410 const widest_int &o1val = swap_p ? r2val : r1val;
1411 const widest_int &o1mask = swap_p ? r2mask : r1mask;
1412 const widest_int &o2val = swap_p ? r1val : r2val;
1413 const widest_int &o2mask = swap_p ? r1mask : r2mask;
1415 /* If the most significant bits are not known we know nothing. */
1416 if (wi::neg_p (o1mask) || wi::neg_p (o2mask))
1417 break;
1419 /* For comparisons the signedness is in the comparison operands. */
1420 sgn = TYPE_SIGN (r1type);
1422 /* If we know the most significant bits we know the values
1423 value ranges by means of treating varying bits as zero
1424 or one. Do a cross comparison of the max/min pairs. */
1425 maxmin = wi::cmp (o1val | o1mask, o2val.and_not (o2mask), sgn);
1426 minmax = wi::cmp (o1val.and_not (o1mask), o2val | o2mask, sgn);
1427 if (maxmin < 0) /* o1 is less than o2. */
1429 *mask = 0;
1430 *val = 1;
1432 else if (minmax > 0) /* o1 is not less or equal to o2. */
1434 *mask = 0;
1435 *val = 0;
1437 else if (maxmin == minmax) /* o1 and o2 are equal. */
1439 /* This probably should never happen as we'd have
1440 folded the thing during fully constant value folding. */
1441 *mask = 0;
1442 *val = (code == LE_EXPR ? 1 : 0);
1444 else
1446 /* We know the result of a comparison is always one or zero. */
1447 *mask = 1;
1448 *val = 0;
1450 break;
1453 default:;
1457 /* Return the propagation value when applying the operation CODE to
1458 the value RHS yielding type TYPE. */
1460 static ccp_prop_value_t
1461 bit_value_unop (enum tree_code code, tree type, tree rhs)
1463 ccp_prop_value_t rval = get_value_for_expr (rhs, true);
1464 widest_int value, mask;
1465 ccp_prop_value_t val;
1467 if (rval.lattice_val == UNDEFINED)
1468 return rval;
1470 gcc_assert ((rval.lattice_val == CONSTANT
1471 && TREE_CODE (rval.value) == INTEGER_CST)
1472 || rval.mask == -1);
1473 bit_value_unop_1 (code, type, &value, &mask,
1474 TREE_TYPE (rhs), value_to_wide_int (rval), rval.mask);
1475 if (mask != -1)
1477 val.lattice_val = CONSTANT;
1478 val.mask = mask;
1479 /* ??? Delay building trees here. */
1480 val.value = wide_int_to_tree (type, value);
1482 else
1484 val.lattice_val = VARYING;
1485 val.value = NULL_TREE;
1486 val.mask = -1;
1488 return val;
1491 /* Return the propagation value when applying the operation CODE to
1492 the values RHS1 and RHS2 yielding type TYPE. */
1494 static ccp_prop_value_t
1495 bit_value_binop (enum tree_code code, tree type, tree rhs1, tree rhs2)
1497 ccp_prop_value_t r1val = get_value_for_expr (rhs1, true);
1498 ccp_prop_value_t r2val = get_value_for_expr (rhs2, true);
1499 widest_int value, mask;
1500 ccp_prop_value_t val;
1502 if (r1val.lattice_val == UNDEFINED
1503 || r2val.lattice_val == UNDEFINED)
1505 val.lattice_val = VARYING;
1506 val.value = NULL_TREE;
1507 val.mask = -1;
1508 return val;
1511 gcc_assert ((r1val.lattice_val == CONSTANT
1512 && TREE_CODE (r1val.value) == INTEGER_CST)
1513 || r1val.mask == -1);
1514 gcc_assert ((r2val.lattice_val == CONSTANT
1515 && TREE_CODE (r2val.value) == INTEGER_CST)
1516 || r2val.mask == -1);
1517 bit_value_binop_1 (code, type, &value, &mask,
1518 TREE_TYPE (rhs1), value_to_wide_int (r1val), r1val.mask,
1519 TREE_TYPE (rhs2), value_to_wide_int (r2val), r2val.mask);
1520 if (mask != -1)
1522 val.lattice_val = CONSTANT;
1523 val.mask = mask;
1524 /* ??? Delay building trees here. */
1525 val.value = wide_int_to_tree (type, value);
1527 else
1529 val.lattice_val = VARYING;
1530 val.value = NULL_TREE;
1531 val.mask = -1;
1533 return val;
1536 /* Return the propagation value for __builtin_assume_aligned
1537 and functions with assume_aligned or alloc_aligned attribute.
1538 For __builtin_assume_aligned, ATTR is NULL_TREE,
1539 for assume_aligned attribute ATTR is non-NULL and ALLOC_ALIGNED
1540 is false, for alloc_aligned attribute ATTR is non-NULL and
1541 ALLOC_ALIGNED is true. */
1543 static ccp_prop_value_t
1544 bit_value_assume_aligned (gimple stmt, tree attr, ccp_prop_value_t ptrval,
1545 bool alloc_aligned)
1547 tree align, misalign = NULL_TREE, type;
1548 unsigned HOST_WIDE_INT aligni, misaligni = 0;
1549 ccp_prop_value_t alignval;
1550 widest_int value, mask;
1551 ccp_prop_value_t val;
1553 if (attr == NULL_TREE)
1555 tree ptr = gimple_call_arg (stmt, 0);
1556 type = TREE_TYPE (ptr);
1557 ptrval = get_value_for_expr (ptr, true);
1559 else
1561 tree lhs = gimple_call_lhs (stmt);
1562 type = TREE_TYPE (lhs);
1565 if (ptrval.lattice_val == UNDEFINED)
1566 return ptrval;
1567 gcc_assert ((ptrval.lattice_val == CONSTANT
1568 && TREE_CODE (ptrval.value) == INTEGER_CST)
1569 || ptrval.mask == -1);
1570 if (attr == NULL_TREE)
1572 /* Get aligni and misaligni from __builtin_assume_aligned. */
1573 align = gimple_call_arg (stmt, 1);
1574 if (!tree_fits_uhwi_p (align))
1575 return ptrval;
1576 aligni = tree_to_uhwi (align);
1577 if (gimple_call_num_args (stmt) > 2)
1579 misalign = gimple_call_arg (stmt, 2);
1580 if (!tree_fits_uhwi_p (misalign))
1581 return ptrval;
1582 misaligni = tree_to_uhwi (misalign);
1585 else
1587 /* Get aligni and misaligni from assume_aligned or
1588 alloc_align attributes. */
1589 if (TREE_VALUE (attr) == NULL_TREE)
1590 return ptrval;
1591 attr = TREE_VALUE (attr);
1592 align = TREE_VALUE (attr);
1593 if (!tree_fits_uhwi_p (align))
1594 return ptrval;
1595 aligni = tree_to_uhwi (align);
1596 if (alloc_aligned)
1598 if (aligni == 0 || aligni > gimple_call_num_args (stmt))
1599 return ptrval;
1600 align = gimple_call_arg (stmt, aligni - 1);
1601 if (!tree_fits_uhwi_p (align))
1602 return ptrval;
1603 aligni = tree_to_uhwi (align);
1605 else if (TREE_CHAIN (attr) && TREE_VALUE (TREE_CHAIN (attr)))
1607 misalign = TREE_VALUE (TREE_CHAIN (attr));
1608 if (!tree_fits_uhwi_p (misalign))
1609 return ptrval;
1610 misaligni = tree_to_uhwi (misalign);
1613 if (aligni <= 1 || (aligni & (aligni - 1)) != 0 || misaligni >= aligni)
1614 return ptrval;
1616 align = build_int_cst_type (type, -aligni);
1617 alignval = get_value_for_expr (align, true);
1618 bit_value_binop_1 (BIT_AND_EXPR, type, &value, &mask,
1619 type, value_to_wide_int (ptrval), ptrval.mask,
1620 type, value_to_wide_int (alignval), alignval.mask);
1621 if (mask != -1)
1623 val.lattice_val = CONSTANT;
1624 val.mask = mask;
1625 gcc_assert ((mask.to_uhwi () & (aligni - 1)) == 0);
1626 gcc_assert ((value.to_uhwi () & (aligni - 1)) == 0);
1627 value |= misaligni;
1628 /* ??? Delay building trees here. */
1629 val.value = wide_int_to_tree (type, value);
1631 else
1633 val.lattice_val = VARYING;
1634 val.value = NULL_TREE;
1635 val.mask = -1;
1637 return val;
1640 /* Evaluate statement STMT.
1641 Valid only for assignments, calls, conditionals, and switches. */
1643 static ccp_prop_value_t
1644 evaluate_stmt (gimple stmt)
1646 ccp_prop_value_t val;
1647 tree simplified = NULL_TREE;
1648 ccp_lattice_t likelyvalue = likely_value (stmt);
1649 bool is_constant = false;
1650 unsigned int align;
1652 if (dump_file && (dump_flags & TDF_DETAILS))
1654 fprintf (dump_file, "which is likely ");
1655 switch (likelyvalue)
1657 case CONSTANT:
1658 fprintf (dump_file, "CONSTANT");
1659 break;
1660 case UNDEFINED:
1661 fprintf (dump_file, "UNDEFINED");
1662 break;
1663 case VARYING:
1664 fprintf (dump_file, "VARYING");
1665 break;
1666 default:;
1668 fprintf (dump_file, "\n");
1671 /* If the statement is likely to have a CONSTANT result, then try
1672 to fold the statement to determine the constant value. */
1673 /* FIXME. This is the only place that we call ccp_fold.
1674 Since likely_value never returns CONSTANT for calls, we will
1675 not attempt to fold them, including builtins that may profit. */
1676 if (likelyvalue == CONSTANT)
1678 fold_defer_overflow_warnings ();
1679 simplified = ccp_fold (stmt);
1680 is_constant = simplified && is_gimple_min_invariant (simplified);
1681 fold_undefer_overflow_warnings (is_constant, stmt, 0);
1682 if (is_constant)
1684 /* The statement produced a constant value. */
1685 val.lattice_val = CONSTANT;
1686 val.value = simplified;
1687 val.mask = 0;
1690 /* If the statement is likely to have a VARYING result, then do not
1691 bother folding the statement. */
1692 else if (likelyvalue == VARYING)
1694 enum gimple_code code = gimple_code (stmt);
1695 if (code == GIMPLE_ASSIGN)
1697 enum tree_code subcode = gimple_assign_rhs_code (stmt);
1699 /* Other cases cannot satisfy is_gimple_min_invariant
1700 without folding. */
1701 if (get_gimple_rhs_class (subcode) == GIMPLE_SINGLE_RHS)
1702 simplified = gimple_assign_rhs1 (stmt);
1704 else if (code == GIMPLE_SWITCH)
1705 simplified = gimple_switch_index (stmt);
1706 else
1707 /* These cannot satisfy is_gimple_min_invariant without folding. */
1708 gcc_assert (code == GIMPLE_CALL || code == GIMPLE_COND);
1709 is_constant = simplified && is_gimple_min_invariant (simplified);
1710 if (is_constant)
1712 /* The statement produced a constant value. */
1713 val.lattice_val = CONSTANT;
1714 val.value = simplified;
1715 val.mask = 0;
1719 /* Resort to simplification for bitwise tracking. */
1720 if (flag_tree_bit_ccp
1721 && (likelyvalue == CONSTANT || is_gimple_call (stmt))
1722 && !is_constant)
1724 enum gimple_code code = gimple_code (stmt);
1725 val.lattice_val = VARYING;
1726 val.value = NULL_TREE;
1727 val.mask = -1;
1728 if (code == GIMPLE_ASSIGN)
1730 enum tree_code subcode = gimple_assign_rhs_code (stmt);
1731 tree rhs1 = gimple_assign_rhs1 (stmt);
1732 switch (get_gimple_rhs_class (subcode))
1734 case GIMPLE_SINGLE_RHS:
1735 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1736 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1737 val = get_value_for_expr (rhs1, true);
1738 break;
1740 case GIMPLE_UNARY_RHS:
1741 if ((INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1742 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1743 && (INTEGRAL_TYPE_P (gimple_expr_type (stmt))
1744 || POINTER_TYPE_P (gimple_expr_type (stmt))))
1745 val = bit_value_unop (subcode, gimple_expr_type (stmt), rhs1);
1746 break;
1748 case GIMPLE_BINARY_RHS:
1749 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1750 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1752 tree lhs = gimple_assign_lhs (stmt);
1753 tree rhs2 = gimple_assign_rhs2 (stmt);
1754 val = bit_value_binop (subcode,
1755 TREE_TYPE (lhs), rhs1, rhs2);
1757 break;
1759 default:;
1762 else if (code == GIMPLE_COND)
1764 enum tree_code code = gimple_cond_code (stmt);
1765 tree rhs1 = gimple_cond_lhs (stmt);
1766 tree rhs2 = gimple_cond_rhs (stmt);
1767 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1768 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1769 val = bit_value_binop (code, TREE_TYPE (rhs1), rhs1, rhs2);
1771 else if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
1773 tree fndecl = gimple_call_fndecl (stmt);
1774 switch (DECL_FUNCTION_CODE (fndecl))
1776 case BUILT_IN_MALLOC:
1777 case BUILT_IN_REALLOC:
1778 case BUILT_IN_CALLOC:
1779 case BUILT_IN_STRDUP:
1780 case BUILT_IN_STRNDUP:
1781 val.lattice_val = CONSTANT;
1782 val.value = build_int_cst (TREE_TYPE (gimple_get_lhs (stmt)), 0);
1783 val.mask = ~((HOST_WIDE_INT) MALLOC_ABI_ALIGNMENT
1784 / BITS_PER_UNIT - 1);
1785 break;
1787 case BUILT_IN_ALLOCA:
1788 case BUILT_IN_ALLOCA_WITH_ALIGN:
1789 align = (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_ALLOCA_WITH_ALIGN
1790 ? TREE_INT_CST_LOW (gimple_call_arg (stmt, 1))
1791 : BIGGEST_ALIGNMENT);
1792 val.lattice_val = CONSTANT;
1793 val.value = build_int_cst (TREE_TYPE (gimple_get_lhs (stmt)), 0);
1794 val.mask = ~((HOST_WIDE_INT) align / BITS_PER_UNIT - 1);
1795 break;
1797 /* These builtins return their first argument, unmodified. */
1798 case BUILT_IN_MEMCPY:
1799 case BUILT_IN_MEMMOVE:
1800 case BUILT_IN_MEMSET:
1801 case BUILT_IN_STRCPY:
1802 case BUILT_IN_STRNCPY:
1803 case BUILT_IN_MEMCPY_CHK:
1804 case BUILT_IN_MEMMOVE_CHK:
1805 case BUILT_IN_MEMSET_CHK:
1806 case BUILT_IN_STRCPY_CHK:
1807 case BUILT_IN_STRNCPY_CHK:
1808 val = get_value_for_expr (gimple_call_arg (stmt, 0), true);
1809 break;
1811 case BUILT_IN_ASSUME_ALIGNED:
1812 val = bit_value_assume_aligned (stmt, NULL_TREE, val, false);
1813 break;
1815 case BUILT_IN_ALIGNED_ALLOC:
1817 tree align = get_constant_value (gimple_call_arg (stmt, 0));
1818 if (align
1819 && tree_fits_uhwi_p (align))
1821 unsigned HOST_WIDE_INT aligni = tree_to_uhwi (align);
1822 if (aligni > 1
1823 /* align must be power-of-two */
1824 && (aligni & (aligni - 1)) == 0)
1826 val.lattice_val = CONSTANT;
1827 val.value = build_int_cst (ptr_type_node, 0);
1828 val.mask = -aligni;
1831 break;
1834 default:;
1837 if (is_gimple_call (stmt) && gimple_call_lhs (stmt))
1839 tree fntype = gimple_call_fntype (stmt);
1840 if (fntype)
1842 tree attrs = lookup_attribute ("assume_aligned",
1843 TYPE_ATTRIBUTES (fntype));
1844 if (attrs)
1845 val = bit_value_assume_aligned (stmt, attrs, val, false);
1846 attrs = lookup_attribute ("alloc_align",
1847 TYPE_ATTRIBUTES (fntype));
1848 if (attrs)
1849 val = bit_value_assume_aligned (stmt, attrs, val, true);
1852 is_constant = (val.lattice_val == CONSTANT);
1855 if (flag_tree_bit_ccp
1856 && ((is_constant && TREE_CODE (val.value) == INTEGER_CST)
1857 || (!is_constant && likelyvalue != UNDEFINED))
1858 && gimple_get_lhs (stmt)
1859 && TREE_CODE (gimple_get_lhs (stmt)) == SSA_NAME)
1861 tree lhs = gimple_get_lhs (stmt);
1862 wide_int nonzero_bits = get_nonzero_bits (lhs);
1863 if (nonzero_bits != -1)
1865 if (!is_constant)
1867 val.lattice_val = CONSTANT;
1868 val.value = build_zero_cst (TREE_TYPE (lhs));
1869 val.mask = extend_mask (nonzero_bits);
1870 is_constant = true;
1872 else
1874 if (wi::bit_and_not (val.value, nonzero_bits) != 0)
1875 val.value = wide_int_to_tree (TREE_TYPE (lhs),
1876 nonzero_bits & val.value);
1877 if (nonzero_bits == 0)
1878 val.mask = 0;
1879 else
1880 val.mask = val.mask & extend_mask (nonzero_bits);
1885 if (!is_constant)
1887 /* The statement produced a nonconstant value. If the statement
1888 had UNDEFINED operands, then the result of the statement
1889 should be UNDEFINED. Otherwise, the statement is VARYING. */
1890 if (likelyvalue == UNDEFINED)
1892 val.lattice_val = likelyvalue;
1893 val.mask = 0;
1895 else
1897 val.lattice_val = VARYING;
1898 val.mask = -1;
1901 val.value = NULL_TREE;
1904 return val;
1907 typedef hash_table<pointer_hash<gimple_statement_base> > gimple_htab;
1909 /* Given a BUILT_IN_STACK_SAVE value SAVED_VAL, insert a clobber of VAR before
1910 each matching BUILT_IN_STACK_RESTORE. Mark visited phis in VISITED. */
1912 static void
1913 insert_clobber_before_stack_restore (tree saved_val, tree var,
1914 gimple_htab **visited)
1916 gimple stmt, clobber_stmt;
1917 tree clobber;
1918 imm_use_iterator iter;
1919 gimple_stmt_iterator i;
1920 gimple *slot;
1922 FOR_EACH_IMM_USE_STMT (stmt, iter, saved_val)
1923 if (gimple_call_builtin_p (stmt, BUILT_IN_STACK_RESTORE))
1925 clobber = build_constructor (TREE_TYPE (var),
1926 NULL);
1927 TREE_THIS_VOLATILE (clobber) = 1;
1928 clobber_stmt = gimple_build_assign (var, clobber);
1930 i = gsi_for_stmt (stmt);
1931 gsi_insert_before (&i, clobber_stmt, GSI_SAME_STMT);
1933 else if (gimple_code (stmt) == GIMPLE_PHI)
1935 if (!*visited)
1936 *visited = new gimple_htab (10);
1938 slot = (*visited)->find_slot (stmt, INSERT);
1939 if (*slot != NULL)
1940 continue;
1942 *slot = stmt;
1943 insert_clobber_before_stack_restore (gimple_phi_result (stmt), var,
1944 visited);
1946 else if (gimple_assign_ssa_name_copy_p (stmt))
1947 insert_clobber_before_stack_restore (gimple_assign_lhs (stmt), var,
1948 visited);
1949 else if (chkp_gimple_call_builtin_p (stmt, BUILT_IN_CHKP_BNDRET))
1950 continue;
1951 else
1952 gcc_assert (is_gimple_debug (stmt));
1955 /* Advance the iterator to the previous non-debug gimple statement in the same
1956 or dominating basic block. */
1958 static inline void
1959 gsi_prev_dom_bb_nondebug (gimple_stmt_iterator *i)
1961 basic_block dom;
1963 gsi_prev_nondebug (i);
1964 while (gsi_end_p (*i))
1966 dom = get_immediate_dominator (CDI_DOMINATORS, i->bb);
1967 if (dom == NULL || dom == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1968 return;
1970 *i = gsi_last_bb (dom);
1974 /* Find a BUILT_IN_STACK_SAVE dominating gsi_stmt (I), and insert
1975 a clobber of VAR before each matching BUILT_IN_STACK_RESTORE.
1977 It is possible that BUILT_IN_STACK_SAVE cannot be find in a dominator when a
1978 previous pass (such as DOM) duplicated it along multiple paths to a BB. In
1979 that case the function gives up without inserting the clobbers. */
1981 static void
1982 insert_clobbers_for_var (gimple_stmt_iterator i, tree var)
1984 gimple stmt;
1985 tree saved_val;
1986 gimple_htab *visited = NULL;
1988 for (; !gsi_end_p (i); gsi_prev_dom_bb_nondebug (&i))
1990 stmt = gsi_stmt (i);
1992 if (!gimple_call_builtin_p (stmt, BUILT_IN_STACK_SAVE))
1993 continue;
1995 saved_val = gimple_call_lhs (stmt);
1996 if (saved_val == NULL_TREE)
1997 continue;
1999 insert_clobber_before_stack_restore (saved_val, var, &visited);
2000 break;
2003 delete visited;
2006 /* Detects a __builtin_alloca_with_align with constant size argument. Declares
2007 fixed-size array and returns the address, if found, otherwise returns
2008 NULL_TREE. */
2010 static tree
2011 fold_builtin_alloca_with_align (gimple stmt)
2013 unsigned HOST_WIDE_INT size, threshold, n_elem;
2014 tree lhs, arg, block, var, elem_type, array_type;
2016 /* Get lhs. */
2017 lhs = gimple_call_lhs (stmt);
2018 if (lhs == NULL_TREE)
2019 return NULL_TREE;
2021 /* Detect constant argument. */
2022 arg = get_constant_value (gimple_call_arg (stmt, 0));
2023 if (arg == NULL_TREE
2024 || TREE_CODE (arg) != INTEGER_CST
2025 || !tree_fits_uhwi_p (arg))
2026 return NULL_TREE;
2028 size = tree_to_uhwi (arg);
2030 /* Heuristic: don't fold large allocas. */
2031 threshold = (unsigned HOST_WIDE_INT)PARAM_VALUE (PARAM_LARGE_STACK_FRAME);
2032 /* In case the alloca is located at function entry, it has the same lifetime
2033 as a declared array, so we allow a larger size. */
2034 block = gimple_block (stmt);
2035 if (!(cfun->after_inlining
2036 && TREE_CODE (BLOCK_SUPERCONTEXT (block)) == FUNCTION_DECL))
2037 threshold /= 10;
2038 if (size > threshold)
2039 return NULL_TREE;
2041 /* Declare array. */
2042 elem_type = build_nonstandard_integer_type (BITS_PER_UNIT, 1);
2043 n_elem = size * 8 / BITS_PER_UNIT;
2044 array_type = build_array_type_nelts (elem_type, n_elem);
2045 var = create_tmp_var (array_type, NULL);
2046 DECL_ALIGN (var) = TREE_INT_CST_LOW (gimple_call_arg (stmt, 1));
2048 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (lhs);
2049 if (pi != NULL && !pi->pt.anything)
2051 bool singleton_p;
2052 unsigned uid;
2053 singleton_p = pt_solution_singleton_p (&pi->pt, &uid);
2054 gcc_assert (singleton_p);
2055 SET_DECL_PT_UID (var, uid);
2059 /* Fold alloca to the address of the array. */
2060 return fold_convert (TREE_TYPE (lhs), build_fold_addr_expr (var));
2063 /* Fold the stmt at *GSI with CCP specific information that propagating
2064 and regular folding does not catch. */
2066 static bool
2067 ccp_fold_stmt (gimple_stmt_iterator *gsi)
2069 gimple stmt = gsi_stmt (*gsi);
2071 switch (gimple_code (stmt))
2073 case GIMPLE_COND:
2075 ccp_prop_value_t val;
2076 /* Statement evaluation will handle type mismatches in constants
2077 more gracefully than the final propagation. This allows us to
2078 fold more conditionals here. */
2079 val = evaluate_stmt (stmt);
2080 if (val.lattice_val != CONSTANT
2081 || val.mask != 0)
2082 return false;
2084 if (dump_file)
2086 fprintf (dump_file, "Folding predicate ");
2087 print_gimple_expr (dump_file, stmt, 0, 0);
2088 fprintf (dump_file, " to ");
2089 print_generic_expr (dump_file, val.value, 0);
2090 fprintf (dump_file, "\n");
2093 if (integer_zerop (val.value))
2094 gimple_cond_make_false (stmt);
2095 else
2096 gimple_cond_make_true (stmt);
2098 return true;
2101 case GIMPLE_CALL:
2103 tree lhs = gimple_call_lhs (stmt);
2104 int flags = gimple_call_flags (stmt);
2105 tree val;
2106 tree argt;
2107 bool changed = false;
2108 unsigned i;
2110 /* If the call was folded into a constant make sure it goes
2111 away even if we cannot propagate into all uses because of
2112 type issues. */
2113 if (lhs
2114 && TREE_CODE (lhs) == SSA_NAME
2115 && (val = get_constant_value (lhs))
2116 /* Don't optimize away calls that have side-effects. */
2117 && (flags & (ECF_CONST|ECF_PURE)) != 0
2118 && (flags & ECF_LOOPING_CONST_OR_PURE) == 0)
2120 tree new_rhs = unshare_expr (val);
2121 bool res;
2122 if (!useless_type_conversion_p (TREE_TYPE (lhs),
2123 TREE_TYPE (new_rhs)))
2124 new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
2125 res = update_call_from_tree (gsi, new_rhs);
2126 gcc_assert (res);
2127 return true;
2130 /* Internal calls provide no argument types, so the extra laxity
2131 for normal calls does not apply. */
2132 if (gimple_call_internal_p (stmt))
2133 return false;
2135 /* The heuristic of fold_builtin_alloca_with_align differs before and
2136 after inlining, so we don't require the arg to be changed into a
2137 constant for folding, but just to be constant. */
2138 if (gimple_call_builtin_p (stmt, BUILT_IN_ALLOCA_WITH_ALIGN))
2140 tree new_rhs = fold_builtin_alloca_with_align (stmt);
2141 if (new_rhs)
2143 bool res = update_call_from_tree (gsi, new_rhs);
2144 tree var = TREE_OPERAND (TREE_OPERAND (new_rhs, 0),0);
2145 gcc_assert (res);
2146 insert_clobbers_for_var (*gsi, var);
2147 return true;
2151 /* Propagate into the call arguments. Compared to replace_uses_in
2152 this can use the argument slot types for type verification
2153 instead of the current argument type. We also can safely
2154 drop qualifiers here as we are dealing with constants anyway. */
2155 argt = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
2156 for (i = 0; i < gimple_call_num_args (stmt) && argt;
2157 ++i, argt = TREE_CHAIN (argt))
2159 tree arg = gimple_call_arg (stmt, i);
2160 if (TREE_CODE (arg) == SSA_NAME
2161 && (val = get_constant_value (arg))
2162 && useless_type_conversion_p
2163 (TYPE_MAIN_VARIANT (TREE_VALUE (argt)),
2164 TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2166 gimple_call_set_arg (stmt, i, unshare_expr (val));
2167 changed = true;
2171 return changed;
2174 case GIMPLE_ASSIGN:
2176 tree lhs = gimple_assign_lhs (stmt);
2177 tree val;
2179 /* If we have a load that turned out to be constant replace it
2180 as we cannot propagate into all uses in all cases. */
2181 if (gimple_assign_single_p (stmt)
2182 && TREE_CODE (lhs) == SSA_NAME
2183 && (val = get_constant_value (lhs)))
2185 tree rhs = unshare_expr (val);
2186 if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs)))
2187 rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (lhs), rhs);
2188 gimple_assign_set_rhs_from_tree (gsi, rhs);
2189 return true;
2192 return false;
2195 default:
2196 return false;
2200 /* Visit the assignment statement STMT. Set the value of its LHS to the
2201 value computed by the RHS and store LHS in *OUTPUT_P. If STMT
2202 creates virtual definitions, set the value of each new name to that
2203 of the RHS (if we can derive a constant out of the RHS).
2204 Value-returning call statements also perform an assignment, and
2205 are handled here. */
2207 static enum ssa_prop_result
2208 visit_assignment (gimple stmt, tree *output_p)
2210 ccp_prop_value_t val;
2211 enum ssa_prop_result retval;
2213 tree lhs = gimple_get_lhs (stmt);
2215 gcc_assert (gimple_code (stmt) != GIMPLE_CALL
2216 || gimple_call_lhs (stmt) != NULL_TREE);
2218 if (gimple_assign_single_p (stmt)
2219 && gimple_assign_rhs_code (stmt) == SSA_NAME)
2220 /* For a simple copy operation, we copy the lattice values. */
2221 val = *get_value (gimple_assign_rhs1 (stmt));
2222 else
2223 /* Evaluate the statement, which could be
2224 either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
2225 val = evaluate_stmt (stmt);
2227 retval = SSA_PROP_NOT_INTERESTING;
2229 /* Set the lattice value of the statement's output. */
2230 if (TREE_CODE (lhs) == SSA_NAME)
2232 /* If STMT is an assignment to an SSA_NAME, we only have one
2233 value to set. */
2234 if (set_lattice_value (lhs, val))
2236 *output_p = lhs;
2237 if (val.lattice_val == VARYING)
2238 retval = SSA_PROP_VARYING;
2239 else
2240 retval = SSA_PROP_INTERESTING;
2244 return retval;
2248 /* Visit the conditional statement STMT. Return SSA_PROP_INTERESTING
2249 if it can determine which edge will be taken. Otherwise, return
2250 SSA_PROP_VARYING. */
2252 static enum ssa_prop_result
2253 visit_cond_stmt (gimple stmt, edge *taken_edge_p)
2255 ccp_prop_value_t val;
2256 basic_block block;
2258 block = gimple_bb (stmt);
2259 val = evaluate_stmt (stmt);
2260 if (val.lattice_val != CONSTANT
2261 || val.mask != 0)
2262 return SSA_PROP_VARYING;
2264 /* Find which edge out of the conditional block will be taken and add it
2265 to the worklist. If no single edge can be determined statically,
2266 return SSA_PROP_VARYING to feed all the outgoing edges to the
2267 propagation engine. */
2268 *taken_edge_p = find_taken_edge (block, val.value);
2269 if (*taken_edge_p)
2270 return SSA_PROP_INTERESTING;
2271 else
2272 return SSA_PROP_VARYING;
2276 /* Evaluate statement STMT. If the statement produces an output value and
2277 its evaluation changes the lattice value of its output, return
2278 SSA_PROP_INTERESTING and set *OUTPUT_P to the SSA_NAME holding the
2279 output value.
2281 If STMT is a conditional branch and we can determine its truth
2282 value, set *TAKEN_EDGE_P accordingly. If STMT produces a varying
2283 value, return SSA_PROP_VARYING. */
2285 static enum ssa_prop_result
2286 ccp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
2288 tree def;
2289 ssa_op_iter iter;
2291 if (dump_file && (dump_flags & TDF_DETAILS))
2293 fprintf (dump_file, "\nVisiting statement:\n");
2294 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2297 switch (gimple_code (stmt))
2299 case GIMPLE_ASSIGN:
2300 /* If the statement is an assignment that produces a single
2301 output value, evaluate its RHS to see if the lattice value of
2302 its output has changed. */
2303 return visit_assignment (stmt, output_p);
2305 case GIMPLE_CALL:
2306 /* A value-returning call also performs an assignment. */
2307 if (gimple_call_lhs (stmt) != NULL_TREE)
2308 return visit_assignment (stmt, output_p);
2309 break;
2311 case GIMPLE_COND:
2312 case GIMPLE_SWITCH:
2313 /* If STMT is a conditional branch, see if we can determine
2314 which branch will be taken. */
2315 /* FIXME. It appears that we should be able to optimize
2316 computed GOTOs here as well. */
2317 return visit_cond_stmt (stmt, taken_edge_p);
2319 default:
2320 break;
2323 /* Any other kind of statement is not interesting for constant
2324 propagation and, therefore, not worth simulating. */
2325 if (dump_file && (dump_flags & TDF_DETAILS))
2326 fprintf (dump_file, "No interesting values produced. Marked VARYING.\n");
2328 /* Definitions made by statements other than assignments to
2329 SSA_NAMEs represent unknown modifications to their outputs.
2330 Mark them VARYING. */
2331 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
2333 ccp_prop_value_t v = { VARYING, NULL_TREE, -1 };
2334 set_lattice_value (def, v);
2337 return SSA_PROP_VARYING;
2341 /* Main entry point for SSA Conditional Constant Propagation. */
2343 static unsigned int
2344 do_ssa_ccp (void)
2346 unsigned int todo = 0;
2347 calculate_dominance_info (CDI_DOMINATORS);
2348 ccp_initialize ();
2349 ssa_propagate (ccp_visit_stmt, ccp_visit_phi_node);
2350 if (ccp_finalize ())
2351 todo = (TODO_cleanup_cfg | TODO_update_ssa);
2352 free_dominance_info (CDI_DOMINATORS);
2353 return todo;
2357 namespace {
2359 const pass_data pass_data_ccp =
2361 GIMPLE_PASS, /* type */
2362 "ccp", /* name */
2363 OPTGROUP_NONE, /* optinfo_flags */
2364 TV_TREE_CCP, /* tv_id */
2365 ( PROP_cfg | PROP_ssa ), /* properties_required */
2366 0, /* properties_provided */
2367 0, /* properties_destroyed */
2368 0, /* todo_flags_start */
2369 TODO_update_address_taken, /* todo_flags_finish */
2372 class pass_ccp : public gimple_opt_pass
2374 public:
2375 pass_ccp (gcc::context *ctxt)
2376 : gimple_opt_pass (pass_data_ccp, ctxt)
2379 /* opt_pass methods: */
2380 opt_pass * clone () { return new pass_ccp (m_ctxt); }
2381 virtual bool gate (function *) { return flag_tree_ccp != 0; }
2382 virtual unsigned int execute (function *) { return do_ssa_ccp (); }
2384 }; // class pass_ccp
2386 } // anon namespace
2388 gimple_opt_pass *
2389 make_pass_ccp (gcc::context *ctxt)
2391 return new pass_ccp (ctxt);
2396 /* Try to optimize out __builtin_stack_restore. Optimize it out
2397 if there is another __builtin_stack_restore in the same basic
2398 block and no calls or ASM_EXPRs are in between, or if this block's
2399 only outgoing edge is to EXIT_BLOCK and there are no calls or
2400 ASM_EXPRs after this __builtin_stack_restore. */
2402 static tree
2403 optimize_stack_restore (gimple_stmt_iterator i)
2405 tree callee;
2406 gimple stmt;
2408 basic_block bb = gsi_bb (i);
2409 gimple call = gsi_stmt (i);
2411 if (gimple_code (call) != GIMPLE_CALL
2412 || gimple_call_num_args (call) != 1
2413 || TREE_CODE (gimple_call_arg (call, 0)) != SSA_NAME
2414 || !POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))))
2415 return NULL_TREE;
2417 for (gsi_next (&i); !gsi_end_p (i); gsi_next (&i))
2419 stmt = gsi_stmt (i);
2420 if (gimple_code (stmt) == GIMPLE_ASM)
2421 return NULL_TREE;
2422 if (gimple_code (stmt) != GIMPLE_CALL)
2423 continue;
2425 callee = gimple_call_fndecl (stmt);
2426 if (!callee
2427 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2428 /* All regular builtins are ok, just obviously not alloca. */
2429 || DECL_FUNCTION_CODE (callee) == BUILT_IN_ALLOCA
2430 || DECL_FUNCTION_CODE (callee) == BUILT_IN_ALLOCA_WITH_ALIGN)
2431 return NULL_TREE;
2433 if (DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_RESTORE)
2434 goto second_stack_restore;
2437 if (!gsi_end_p (i))
2438 return NULL_TREE;
2440 /* Allow one successor of the exit block, or zero successors. */
2441 switch (EDGE_COUNT (bb->succs))
2443 case 0:
2444 break;
2445 case 1:
2446 if (single_succ_edge (bb)->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
2447 return NULL_TREE;
2448 break;
2449 default:
2450 return NULL_TREE;
2452 second_stack_restore:
2454 /* If there's exactly one use, then zap the call to __builtin_stack_save.
2455 If there are multiple uses, then the last one should remove the call.
2456 In any case, whether the call to __builtin_stack_save can be removed
2457 or not is irrelevant to removing the call to __builtin_stack_restore. */
2458 if (has_single_use (gimple_call_arg (call, 0)))
2460 gimple stack_save = SSA_NAME_DEF_STMT (gimple_call_arg (call, 0));
2461 if (is_gimple_call (stack_save))
2463 callee = gimple_call_fndecl (stack_save);
2464 if (callee
2465 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
2466 && DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_SAVE)
2468 gimple_stmt_iterator stack_save_gsi;
2469 tree rhs;
2471 stack_save_gsi = gsi_for_stmt (stack_save);
2472 rhs = build_int_cst (TREE_TYPE (gimple_call_arg (call, 0)), 0);
2473 update_call_from_tree (&stack_save_gsi, rhs);
2478 /* No effect, so the statement will be deleted. */
2479 return integer_zero_node;
2482 /* If va_list type is a simple pointer and nothing special is needed,
2483 optimize __builtin_va_start (&ap, 0) into ap = __builtin_next_arg (0),
2484 __builtin_va_end (&ap) out as NOP and __builtin_va_copy into a simple
2485 pointer assignment. */
2487 static tree
2488 optimize_stdarg_builtin (gimple call)
2490 tree callee, lhs, rhs, cfun_va_list;
2491 bool va_list_simple_ptr;
2492 location_t loc = gimple_location (call);
2494 if (gimple_code (call) != GIMPLE_CALL)
2495 return NULL_TREE;
2497 callee = gimple_call_fndecl (call);
2499 cfun_va_list = targetm.fn_abi_va_list (callee);
2500 va_list_simple_ptr = POINTER_TYPE_P (cfun_va_list)
2501 && (TREE_TYPE (cfun_va_list) == void_type_node
2502 || TREE_TYPE (cfun_va_list) == char_type_node);
2504 switch (DECL_FUNCTION_CODE (callee))
2506 case BUILT_IN_VA_START:
2507 if (!va_list_simple_ptr
2508 || targetm.expand_builtin_va_start != NULL
2509 || !builtin_decl_explicit_p (BUILT_IN_NEXT_ARG))
2510 return NULL_TREE;
2512 if (gimple_call_num_args (call) != 2)
2513 return NULL_TREE;
2515 lhs = gimple_call_arg (call, 0);
2516 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
2517 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
2518 != TYPE_MAIN_VARIANT (cfun_va_list))
2519 return NULL_TREE;
2521 lhs = build_fold_indirect_ref_loc (loc, lhs);
2522 rhs = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_NEXT_ARG),
2523 1, integer_zero_node);
2524 rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
2525 return build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, rhs);
2527 case BUILT_IN_VA_COPY:
2528 if (!va_list_simple_ptr)
2529 return NULL_TREE;
2531 if (gimple_call_num_args (call) != 2)
2532 return NULL_TREE;
2534 lhs = gimple_call_arg (call, 0);
2535 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
2536 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
2537 != TYPE_MAIN_VARIANT (cfun_va_list))
2538 return NULL_TREE;
2540 lhs = build_fold_indirect_ref_loc (loc, lhs);
2541 rhs = gimple_call_arg (call, 1);
2542 if (TYPE_MAIN_VARIANT (TREE_TYPE (rhs))
2543 != TYPE_MAIN_VARIANT (cfun_va_list))
2544 return NULL_TREE;
2546 rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
2547 return build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, rhs);
2549 case BUILT_IN_VA_END:
2550 /* No effect, so the statement will be deleted. */
2551 return integer_zero_node;
2553 default:
2554 gcc_unreachable ();
2558 /* Attemp to make the block of __builtin_unreachable I unreachable by changing
2559 the incoming jumps. Return true if at least one jump was changed. */
2561 static bool
2562 optimize_unreachable (gimple_stmt_iterator i)
2564 basic_block bb = gsi_bb (i);
2565 gimple_stmt_iterator gsi;
2566 gimple stmt;
2567 edge_iterator ei;
2568 edge e;
2569 bool ret;
2571 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2573 stmt = gsi_stmt (gsi);
2575 if (is_gimple_debug (stmt))
2576 continue;
2578 if (gimple_code (stmt) == GIMPLE_LABEL)
2580 /* Verify we do not need to preserve the label. */
2581 if (FORCED_LABEL (gimple_label_label (stmt)))
2582 return false;
2584 continue;
2587 /* Only handle the case that __builtin_unreachable is the first statement
2588 in the block. We rely on DCE to remove stmts without side-effects
2589 before __builtin_unreachable. */
2590 if (gsi_stmt (gsi) != gsi_stmt (i))
2591 return false;
2594 ret = false;
2595 FOR_EACH_EDGE (e, ei, bb->preds)
2597 gsi = gsi_last_bb (e->src);
2598 if (gsi_end_p (gsi))
2599 continue;
2601 stmt = gsi_stmt (gsi);
2602 if (gimple_code (stmt) == GIMPLE_COND)
2604 if (e->flags & EDGE_TRUE_VALUE)
2605 gimple_cond_make_false (stmt);
2606 else if (e->flags & EDGE_FALSE_VALUE)
2607 gimple_cond_make_true (stmt);
2608 else
2609 gcc_unreachable ();
2610 update_stmt (stmt);
2612 else
2614 /* Todo: handle other cases, f.i. switch statement. */
2615 continue;
2618 ret = true;
2621 return ret;
2624 /* A simple pass that attempts to fold all builtin functions. This pass
2625 is run after we've propagated as many constants as we can. */
2627 namespace {
2629 const pass_data pass_data_fold_builtins =
2631 GIMPLE_PASS, /* type */
2632 "fab", /* name */
2633 OPTGROUP_NONE, /* optinfo_flags */
2634 TV_NONE, /* tv_id */
2635 ( PROP_cfg | PROP_ssa ), /* properties_required */
2636 0, /* properties_provided */
2637 0, /* properties_destroyed */
2638 0, /* todo_flags_start */
2639 TODO_update_ssa, /* todo_flags_finish */
2642 class pass_fold_builtins : public gimple_opt_pass
2644 public:
2645 pass_fold_builtins (gcc::context *ctxt)
2646 : gimple_opt_pass (pass_data_fold_builtins, ctxt)
2649 /* opt_pass methods: */
2650 opt_pass * clone () { return new pass_fold_builtins (m_ctxt); }
2651 virtual unsigned int execute (function *);
2653 }; // class pass_fold_builtins
2655 unsigned int
2656 pass_fold_builtins::execute (function *fun)
2658 bool cfg_changed = false;
2659 basic_block bb;
2660 unsigned int todoflags = 0;
2662 FOR_EACH_BB_FN (bb, fun)
2664 gimple_stmt_iterator i;
2665 for (i = gsi_start_bb (bb); !gsi_end_p (i); )
2667 gimple stmt, old_stmt;
2668 tree callee;
2669 enum built_in_function fcode;
2671 stmt = gsi_stmt (i);
2673 if (gimple_code (stmt) != GIMPLE_CALL)
2675 /* Remove all *ssaname_N ={v} {CLOBBER}; stmts,
2676 after the last GIMPLE DSE they aren't needed and might
2677 unnecessarily keep the SSA_NAMEs live. */
2678 if (gimple_clobber_p (stmt))
2680 tree lhs = gimple_assign_lhs (stmt);
2681 if (TREE_CODE (lhs) == MEM_REF
2682 && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME)
2684 unlink_stmt_vdef (stmt);
2685 gsi_remove (&i, true);
2686 release_defs (stmt);
2687 continue;
2690 gsi_next (&i);
2691 continue;
2694 callee = gimple_call_fndecl (stmt);
2695 if (!callee || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL)
2697 gsi_next (&i);
2698 continue;
2701 fcode = DECL_FUNCTION_CODE (callee);
2702 if (fold_stmt (&i))
2704 else
2706 tree result = NULL_TREE;
2707 switch (DECL_FUNCTION_CODE (callee))
2709 case BUILT_IN_CONSTANT_P:
2710 /* Resolve __builtin_constant_p. If it hasn't been
2711 folded to integer_one_node by now, it's fairly
2712 certain that the value simply isn't constant. */
2713 result = integer_zero_node;
2714 break;
2716 case BUILT_IN_ASSUME_ALIGNED:
2717 /* Remove __builtin_assume_aligned. */
2718 result = gimple_call_arg (stmt, 0);
2719 break;
2721 case BUILT_IN_STACK_RESTORE:
2722 result = optimize_stack_restore (i);
2723 if (result)
2724 break;
2725 gsi_next (&i);
2726 continue;
2728 case BUILT_IN_UNREACHABLE:
2729 if (optimize_unreachable (i))
2730 cfg_changed = true;
2731 break;
2733 case BUILT_IN_VA_START:
2734 case BUILT_IN_VA_END:
2735 case BUILT_IN_VA_COPY:
2736 /* These shouldn't be folded before pass_stdarg. */
2737 result = optimize_stdarg_builtin (stmt);
2738 if (result)
2739 break;
2740 /* FALLTHRU */
2742 default:;
2745 if (!result)
2747 gsi_next (&i);
2748 continue;
2751 if (!update_call_from_tree (&i, result))
2752 gimplify_and_update_call_from_tree (&i, result);
2755 todoflags |= TODO_update_address_taken;
2757 if (dump_file && (dump_flags & TDF_DETAILS))
2759 fprintf (dump_file, "Simplified\n ");
2760 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2763 old_stmt = stmt;
2764 stmt = gsi_stmt (i);
2765 update_stmt (stmt);
2767 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt)
2768 && gimple_purge_dead_eh_edges (bb))
2769 cfg_changed = true;
2771 if (dump_file && (dump_flags & TDF_DETAILS))
2773 fprintf (dump_file, "to\n ");
2774 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2775 fprintf (dump_file, "\n");
2778 /* Retry the same statement if it changed into another
2779 builtin, there might be new opportunities now. */
2780 if (gimple_code (stmt) != GIMPLE_CALL)
2782 gsi_next (&i);
2783 continue;
2785 callee = gimple_call_fndecl (stmt);
2786 if (!callee
2787 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2788 || DECL_FUNCTION_CODE (callee) == fcode)
2789 gsi_next (&i);
2793 /* Delete unreachable blocks. */
2794 if (cfg_changed)
2795 todoflags |= TODO_cleanup_cfg;
2797 return todoflags;
2800 } // anon namespace
2802 gimple_opt_pass *
2803 make_pass_fold_builtins (gcc::context *ctxt)
2805 return new pass_fold_builtins (ctxt);