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
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
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
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
43 CONSTANT -> V_i has been found to hold a constant
46 VARYING -> V_i cannot take a constant value, or if it
47 does, it is not possible to determine it
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
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:
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
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 */
123 #include "coretypes.h"
126 #include "stor-layout.h"
132 #include "hash-set.h"
133 #include "machmode.h"
134 #include "hard-reg-set.h"
136 #include "function.h"
137 #include "dominance.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"
146 #include "gimple-expr.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"
162 #include "diagnostic-core.h"
165 #include "wide-int-print.h"
166 #include "builtins.h"
169 /* Possible lattice values. */
178 struct ccp_prop_value_t
{
180 ccp_lattice_t lattice_val
;
182 /* Propagated value. */
185 /* Mask that applies to the propagated value during CCP. For X
186 with a CONSTANT lattice value X & ~mask == value & ~mask. The
187 zero bits in the mask cover constant values. The ones mean no
192 /* Array of propagated constant values. After propagation,
193 CONST_VAL[I].VALUE holds the constant value for SSA_NAME(I). If
194 the constant is held in an SSA name representing a memory store
195 (i.e., a VDEF), CONST_VAL[I].MEM_REF will contain the actual
196 memory reference used to store (i.e., the LHS of the assignment
198 static ccp_prop_value_t
*const_val
;
199 static unsigned n_const_val
;
201 static void canonicalize_value (ccp_prop_value_t
*);
202 static bool ccp_fold_stmt (gimple_stmt_iterator
*);
204 /* Dump constant propagation value VAL to file OUTF prefixed by PREFIX. */
207 dump_lattice_value (FILE *outf
, const char *prefix
, ccp_prop_value_t val
)
209 switch (val
.lattice_val
)
212 fprintf (outf
, "%sUNINITIALIZED", prefix
);
215 fprintf (outf
, "%sUNDEFINED", prefix
);
218 fprintf (outf
, "%sVARYING", prefix
);
221 if (TREE_CODE (val
.value
) != INTEGER_CST
224 fprintf (outf
, "%sCONSTANT ", prefix
);
225 print_generic_expr (outf
, val
.value
, dump_flags
);
229 widest_int cval
= wi::bit_and_not (wi::to_widest (val
.value
),
231 fprintf (outf
, "%sCONSTANT ", prefix
);
232 print_hex (cval
, outf
);
233 fprintf (outf
, " (");
234 print_hex (val
.mask
, outf
);
244 /* Print lattice value VAL to stderr. */
246 void debug_lattice_value (ccp_prop_value_t val
);
249 debug_lattice_value (ccp_prop_value_t val
)
251 dump_lattice_value (stderr
, "", val
);
252 fprintf (stderr
, "\n");
255 /* Extend NONZERO_BITS to a full mask, with the upper bits being set. */
258 extend_mask (const wide_int
&nonzero_bits
)
260 return (wi::mask
<widest_int
> (wi::get_precision (nonzero_bits
), true)
261 | widest_int::from (nonzero_bits
, UNSIGNED
));
264 /* Compute a default value for variable VAR and store it in the
265 CONST_VAL array. The following rules are used to get default
268 1- Global and static variables that are declared constant are
271 2- Any other value is considered UNDEFINED. This is useful when
272 considering PHI nodes. PHI arguments that are undefined do not
273 change the constant value of the PHI node, which allows for more
274 constants to be propagated.
276 3- Variables defined by statements other than assignments and PHI
277 nodes are considered VARYING.
279 4- Initial values of variables that are not GIMPLE registers are
280 considered VARYING. */
282 static ccp_prop_value_t
283 get_default_value (tree var
)
285 ccp_prop_value_t val
= { UNINITIALIZED
, NULL_TREE
, 0 };
288 stmt
= SSA_NAME_DEF_STMT (var
);
290 if (gimple_nop_p (stmt
))
292 /* Variables defined by an empty statement are those used
293 before being initialized. If VAR is a local variable, we
294 can assume initially that it is UNDEFINED, otherwise we must
295 consider it VARYING. */
296 if (!virtual_operand_p (var
)
297 && TREE_CODE (SSA_NAME_VAR (var
)) == VAR_DECL
)
298 val
.lattice_val
= UNDEFINED
;
301 val
.lattice_val
= VARYING
;
303 if (flag_tree_bit_ccp
)
305 wide_int nonzero_bits
= get_nonzero_bits (var
);
306 if (nonzero_bits
!= -1)
308 val
.lattice_val
= CONSTANT
;
309 val
.value
= build_zero_cst (TREE_TYPE (var
));
310 val
.mask
= extend_mask (nonzero_bits
);
315 else if (is_gimple_assign (stmt
))
318 if (gimple_assign_single_p (stmt
)
319 && DECL_P (gimple_assign_rhs1 (stmt
))
320 && (cst
= get_symbol_constant_value (gimple_assign_rhs1 (stmt
))))
322 val
.lattice_val
= CONSTANT
;
327 /* Any other variable defined by an assignment is considered
329 val
.lattice_val
= UNDEFINED
;
332 else if ((is_gimple_call (stmt
)
333 && gimple_call_lhs (stmt
) != NULL_TREE
)
334 || gimple_code (stmt
) == GIMPLE_PHI
)
336 /* A variable defined by a call or a PHI node is considered
338 val
.lattice_val
= UNDEFINED
;
342 /* Otherwise, VAR will never take on a constant value. */
343 val
.lattice_val
= VARYING
;
351 /* Get the constant value associated with variable VAR. */
353 static inline ccp_prop_value_t
*
356 ccp_prop_value_t
*val
;
358 if (const_val
== NULL
359 || SSA_NAME_VERSION (var
) >= n_const_val
)
362 val
= &const_val
[SSA_NAME_VERSION (var
)];
363 if (val
->lattice_val
== UNINITIALIZED
)
364 *val
= get_default_value (var
);
366 canonicalize_value (val
);
371 /* Return the constant tree value associated with VAR. */
374 get_constant_value (tree var
)
376 ccp_prop_value_t
*val
;
377 if (TREE_CODE (var
) != SSA_NAME
)
379 if (is_gimple_min_invariant (var
))
383 val
= get_value (var
);
385 && val
->lattice_val
== CONSTANT
386 && (TREE_CODE (val
->value
) != INTEGER_CST
392 /* Sets the value associated with VAR to VARYING. */
395 set_value_varying (tree var
)
397 ccp_prop_value_t
*val
= &const_val
[SSA_NAME_VERSION (var
)];
399 val
->lattice_val
= VARYING
;
400 val
->value
= NULL_TREE
;
404 /* For float types, modify the value of VAL to make ccp work correctly
405 for non-standard values (-0, NaN):
407 If HONOR_SIGNED_ZEROS is false, and VAL = -0, we canonicalize it to 0.
408 If HONOR_NANS is false, and VAL is NaN, we canonicalize it to UNDEFINED.
409 This is to fix the following problem (see PR 29921): Suppose we have
413 and we set value of y to NaN. This causes value of x to be set to NaN.
414 When we later determine that y is in fact VARYING, fold uses the fact
415 that HONOR_NANS is false, and we try to change the value of x to 0,
416 causing an ICE. With HONOR_NANS being false, the real appearance of
417 NaN would cause undefined behavior, though, so claiming that y (and x)
418 are UNDEFINED initially is correct.
420 For other constants, make sure to drop TREE_OVERFLOW. */
423 canonicalize_value (ccp_prop_value_t
*val
)
429 if (val
->lattice_val
!= CONSTANT
)
432 if (TREE_OVERFLOW_P (val
->value
))
433 val
->value
= drop_tree_overflow (val
->value
);
435 if (TREE_CODE (val
->value
) != REAL_CST
)
438 d
= TREE_REAL_CST (val
->value
);
439 type
= TREE_TYPE (val
->value
);
440 mode
= TYPE_MODE (type
);
442 if (!HONOR_SIGNED_ZEROS (mode
)
443 && REAL_VALUE_MINUS_ZERO (d
))
445 val
->value
= build_real (type
, dconst0
);
449 if (!HONOR_NANS (mode
)
450 && REAL_VALUE_ISNAN (d
))
452 val
->lattice_val
= UNDEFINED
;
458 /* Return whether the lattice transition is valid. */
461 valid_lattice_transition (ccp_prop_value_t old_val
, ccp_prop_value_t new_val
)
463 /* Lattice transitions must always be monotonically increasing in
465 if (old_val
.lattice_val
< new_val
.lattice_val
)
468 if (old_val
.lattice_val
!= new_val
.lattice_val
)
471 if (!old_val
.value
&& !new_val
.value
)
474 /* Now both lattice values are CONSTANT. */
476 /* Allow transitioning from PHI <&x, not executable> == &x
477 to PHI <&x, &y> == common alignment. */
478 if (TREE_CODE (old_val
.value
) != INTEGER_CST
479 && TREE_CODE (new_val
.value
) == INTEGER_CST
)
482 /* Bit-lattices have to agree in the still valid bits. */
483 if (TREE_CODE (old_val
.value
) == INTEGER_CST
484 && TREE_CODE (new_val
.value
) == INTEGER_CST
)
485 return (wi::bit_and_not (wi::to_widest (old_val
.value
), new_val
.mask
)
486 == wi::bit_and_not (wi::to_widest (new_val
.value
), new_val
.mask
));
488 /* Otherwise constant values have to agree. */
489 return operand_equal_p (old_val
.value
, new_val
.value
, 0);
492 /* Set the value for variable VAR to NEW_VAL. Return true if the new
493 value is different from VAR's previous value. */
496 set_lattice_value (tree var
, ccp_prop_value_t new_val
)
498 /* We can deal with old UNINITIALIZED values just fine here. */
499 ccp_prop_value_t
*old_val
= &const_val
[SSA_NAME_VERSION (var
)];
501 canonicalize_value (&new_val
);
503 /* We have to be careful to not go up the bitwise lattice
504 represented by the mask.
505 ??? This doesn't seem to be the best place to enforce this. */
506 if (new_val
.lattice_val
== CONSTANT
507 && old_val
->lattice_val
== CONSTANT
508 && TREE_CODE (new_val
.value
) == INTEGER_CST
509 && TREE_CODE (old_val
->value
) == INTEGER_CST
)
511 widest_int diff
= (wi::to_widest (new_val
.value
)
512 ^ wi::to_widest (old_val
->value
));
513 new_val
.mask
= new_val
.mask
| old_val
->mask
| diff
;
516 gcc_assert (valid_lattice_transition (*old_val
, new_val
));
518 /* If *OLD_VAL and NEW_VAL are the same, return false to inform the
519 caller that this was a non-transition. */
520 if (old_val
->lattice_val
!= new_val
.lattice_val
521 || (new_val
.lattice_val
== CONSTANT
522 && TREE_CODE (new_val
.value
) == INTEGER_CST
523 && (TREE_CODE (old_val
->value
) != INTEGER_CST
524 || new_val
.mask
!= old_val
->mask
)))
526 /* ??? We would like to delay creation of INTEGER_CSTs from
527 partially constants here. */
529 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
531 dump_lattice_value (dump_file
, "Lattice value changed to ", new_val
);
532 fprintf (dump_file
, ". Adding SSA edges to worklist.\n");
537 gcc_assert (new_val
.lattice_val
!= UNINITIALIZED
);
544 static ccp_prop_value_t
get_value_for_expr (tree
, bool);
545 static ccp_prop_value_t
bit_value_binop (enum tree_code
, tree
, tree
, tree
);
546 static void bit_value_binop_1 (enum tree_code
, tree
, widest_int
*, widest_int
*,
547 tree
, const widest_int
&, const widest_int
&,
548 tree
, const widest_int
&, const widest_int
&);
550 /* Return a widest_int that can be used for bitwise simplifications
554 value_to_wide_int (ccp_prop_value_t val
)
557 && TREE_CODE (val
.value
) == INTEGER_CST
)
558 return wi::to_widest (val
.value
);
563 /* Return the value for the address expression EXPR based on alignment
566 static ccp_prop_value_t
567 get_value_from_alignment (tree expr
)
569 tree type
= TREE_TYPE (expr
);
570 ccp_prop_value_t val
;
571 unsigned HOST_WIDE_INT bitpos
;
574 gcc_assert (TREE_CODE (expr
) == ADDR_EXPR
);
576 get_pointer_alignment_1 (expr
, &align
, &bitpos
);
577 val
.mask
= (POINTER_TYPE_P (type
) || TYPE_UNSIGNED (type
)
578 ? wi::mask
<widest_int
> (TYPE_PRECISION (type
), false)
579 : -1).and_not (align
/ BITS_PER_UNIT
- 1);
580 val
.lattice_val
= val
.mask
== -1 ? VARYING
: CONSTANT
;
581 if (val
.lattice_val
== CONSTANT
)
582 val
.value
= build_int_cstu (type
, bitpos
/ BITS_PER_UNIT
);
584 val
.value
= NULL_TREE
;
589 /* Return the value for the tree operand EXPR. If FOR_BITS_P is true
590 return constant bits extracted from alignment information for
591 invariant addresses. */
593 static ccp_prop_value_t
594 get_value_for_expr (tree expr
, bool for_bits_p
)
596 ccp_prop_value_t val
;
598 if (TREE_CODE (expr
) == SSA_NAME
)
600 val
= *get_value (expr
);
602 && val
.lattice_val
== CONSTANT
603 && TREE_CODE (val
.value
) == ADDR_EXPR
)
604 val
= get_value_from_alignment (val
.value
);
606 else if (is_gimple_min_invariant (expr
)
607 && (!for_bits_p
|| TREE_CODE (expr
) != ADDR_EXPR
))
609 val
.lattice_val
= CONSTANT
;
612 canonicalize_value (&val
);
614 else if (TREE_CODE (expr
) == ADDR_EXPR
)
615 val
= get_value_from_alignment (expr
);
618 val
.lattice_val
= VARYING
;
620 val
.value
= NULL_TREE
;
625 /* Return the likely CCP lattice value for STMT.
627 If STMT has no operands, then return CONSTANT.
629 Else if undefinedness of operands of STMT cause its value to be
630 undefined, then return UNDEFINED.
632 Else if any operands of STMT are constants, then return CONSTANT.
634 Else return VARYING. */
637 likely_value (gimple stmt
)
639 bool has_constant_operand
, has_undefined_operand
, all_undefined_operands
;
644 enum gimple_code code
= gimple_code (stmt
);
646 /* This function appears to be called only for assignments, calls,
647 conditionals, and switches, due to the logic in visit_stmt. */
648 gcc_assert (code
== GIMPLE_ASSIGN
649 || code
== GIMPLE_CALL
650 || code
== GIMPLE_COND
651 || code
== GIMPLE_SWITCH
);
653 /* If the statement has volatile operands, it won't fold to a
655 if (gimple_has_volatile_ops (stmt
))
658 /* Arrive here for more complex cases. */
659 has_constant_operand
= false;
660 has_undefined_operand
= false;
661 all_undefined_operands
= true;
662 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, iter
, SSA_OP_USE
)
664 ccp_prop_value_t
*val
= get_value (use
);
666 if (val
->lattice_val
== UNDEFINED
)
667 has_undefined_operand
= true;
669 all_undefined_operands
= false;
671 if (val
->lattice_val
== CONSTANT
)
672 has_constant_operand
= true;
675 /* There may be constants in regular rhs operands. For calls we
676 have to ignore lhs, fndecl and static chain, otherwise only
678 for (i
= (is_gimple_call (stmt
) ? 2 : 0) + gimple_has_lhs (stmt
);
679 i
< gimple_num_ops (stmt
); ++i
)
681 tree op
= gimple_op (stmt
, i
);
682 if (!op
|| TREE_CODE (op
) == SSA_NAME
)
684 if (is_gimple_min_invariant (op
))
685 has_constant_operand
= true;
688 if (has_constant_operand
)
689 all_undefined_operands
= false;
691 if (has_undefined_operand
692 && code
== GIMPLE_CALL
693 && gimple_call_internal_p (stmt
))
694 switch (gimple_call_internal_fn (stmt
))
696 /* These 3 builtins use the first argument just as a magic
697 way how to find out a decl uid. */
698 case IFN_GOMP_SIMD_LANE
:
699 case IFN_GOMP_SIMD_VF
:
700 case IFN_GOMP_SIMD_LAST_LANE
:
701 has_undefined_operand
= false;
707 /* If the operation combines operands like COMPLEX_EXPR make sure to
708 not mark the result UNDEFINED if only one part of the result is
710 if (has_undefined_operand
&& all_undefined_operands
)
712 else if (code
== GIMPLE_ASSIGN
&& has_undefined_operand
)
714 switch (gimple_assign_rhs_code (stmt
))
716 /* Unary operators are handled with all_undefined_operands. */
719 case POINTER_PLUS_EXPR
:
720 /* Not MIN_EXPR, MAX_EXPR. One VARYING operand may be selected.
721 Not bitwise operators, one VARYING operand may specify the
722 result completely. Not logical operators for the same reason.
723 Not COMPLEX_EXPR as one VARYING operand makes the result partly
724 not UNDEFINED. Not *DIV_EXPR, comparisons and shifts because
725 the undefined operand may be promoted. */
729 /* If any part of an address is UNDEFINED, like the index
730 of an ARRAY_EXPR, then treat the result as UNDEFINED. */
737 /* If there was an UNDEFINED operand but the result may be not UNDEFINED
738 fall back to CONSTANT. During iteration UNDEFINED may still drop
740 if (has_undefined_operand
)
743 /* We do not consider virtual operands here -- load from read-only
744 memory may have only VARYING virtual operands, but still be
746 if (has_constant_operand
747 || gimple_references_memory_p (stmt
))
753 /* Returns true if STMT cannot be constant. */
756 surely_varying_stmt_p (gimple stmt
)
758 /* If the statement has operands that we cannot handle, it cannot be
760 if (gimple_has_volatile_ops (stmt
))
763 /* If it is a call and does not return a value or is not a
764 builtin and not an indirect call or a call to function with
765 assume_aligned/alloc_align attribute, it is varying. */
766 if (is_gimple_call (stmt
))
768 tree fndecl
, fntype
= gimple_call_fntype (stmt
);
769 if (!gimple_call_lhs (stmt
)
770 || ((fndecl
= gimple_call_fndecl (stmt
)) != NULL_TREE
771 && !DECL_BUILT_IN (fndecl
)
772 && !lookup_attribute ("assume_aligned",
773 TYPE_ATTRIBUTES (fntype
))
774 && !lookup_attribute ("alloc_align",
775 TYPE_ATTRIBUTES (fntype
))))
779 /* Any other store operation is not interesting. */
780 else if (gimple_vdef (stmt
))
783 /* Anything other than assignments and conditional jumps are not
784 interesting for CCP. */
785 if (gimple_code (stmt
) != GIMPLE_ASSIGN
786 && gimple_code (stmt
) != GIMPLE_COND
787 && gimple_code (stmt
) != GIMPLE_SWITCH
788 && gimple_code (stmt
) != GIMPLE_CALL
)
794 /* Initialize local data structures for CCP. */
797 ccp_initialize (void)
801 n_const_val
= num_ssa_names
;
802 const_val
= XCNEWVEC (ccp_prop_value_t
, n_const_val
);
804 /* Initialize simulation flags for PHI nodes and statements. */
805 FOR_EACH_BB_FN (bb
, cfun
)
807 gimple_stmt_iterator i
;
809 for (i
= gsi_start_bb (bb
); !gsi_end_p (i
); gsi_next (&i
))
811 gimple stmt
= gsi_stmt (i
);
814 /* If the statement is a control insn, then we do not
815 want to avoid simulating the statement once. Failure
816 to do so means that those edges will never get added. */
817 if (stmt_ends_bb_p (stmt
))
820 is_varying
= surely_varying_stmt_p (stmt
);
827 /* If the statement will not produce a constant, mark
828 all its outputs VARYING. */
829 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_ALL_DEFS
)
830 set_value_varying (def
);
832 prop_set_simulate_again (stmt
, !is_varying
);
836 /* Now process PHI nodes. We never clear the simulate_again flag on
837 phi nodes, since we do not know which edges are executable yet,
838 except for phi nodes for virtual operands when we do not do store ccp. */
839 FOR_EACH_BB_FN (bb
, cfun
)
841 gimple_stmt_iterator i
;
843 for (i
= gsi_start_phis (bb
); !gsi_end_p (i
); gsi_next (&i
))
845 gimple phi
= gsi_stmt (i
);
847 if (virtual_operand_p (gimple_phi_result (phi
)))
848 prop_set_simulate_again (phi
, false);
850 prop_set_simulate_again (phi
, true);
855 /* Debug count support. Reset the values of ssa names
856 VARYING when the total number ssa names analyzed is
857 beyond the debug count specified. */
863 for (i
= 0; i
< num_ssa_names
; i
++)
867 const_val
[i
].lattice_val
= VARYING
;
868 const_val
[i
].mask
= -1;
869 const_val
[i
].value
= NULL_TREE
;
875 /* Do final substitution of propagated values, cleanup the flowgraph and
876 free allocated storage.
878 Return TRUE when something was optimized. */
883 bool something_changed
;
888 /* Derive alignment and misalignment information from partially
889 constant pointers in the lattice or nonzero bits from partially
890 constant integers. */
891 for (i
= 1; i
< num_ssa_names
; ++i
)
893 tree name
= ssa_name (i
);
894 ccp_prop_value_t
*val
;
895 unsigned int tem
, align
;
898 || (!POINTER_TYPE_P (TREE_TYPE (name
))
899 && (!INTEGRAL_TYPE_P (TREE_TYPE (name
))
900 /* Don't record nonzero bits before IPA to avoid
901 using too much memory. */
902 || first_pass_instance
)))
905 val
= get_value (name
);
906 if (val
->lattice_val
!= CONSTANT
907 || TREE_CODE (val
->value
) != INTEGER_CST
)
910 if (POINTER_TYPE_P (TREE_TYPE (name
)))
912 /* Trailing mask bits specify the alignment, trailing value
913 bits the misalignment. */
914 tem
= val
->mask
.to_uhwi ();
915 align
= (tem
& -tem
);
917 set_ptr_info_alignment (get_ptr_info (name
), align
,
918 (TREE_INT_CST_LOW (val
->value
)
923 unsigned int precision
= TYPE_PRECISION (TREE_TYPE (val
->value
));
924 wide_int nonzero_bits
= wide_int::from (val
->mask
, precision
,
925 UNSIGNED
) | val
->value
;
926 nonzero_bits
&= get_nonzero_bits (name
);
927 set_nonzero_bits (name
, nonzero_bits
);
931 /* Perform substitutions based on the known constant values. */
932 something_changed
= substitute_and_fold (get_constant_value
,
933 ccp_fold_stmt
, true);
937 return something_changed
;;
941 /* Compute the meet operator between *VAL1 and *VAL2. Store the result
944 any M UNDEFINED = any
945 any M VARYING = VARYING
946 Ci M Cj = Ci if (i == j)
947 Ci M Cj = VARYING if (i != j)
951 ccp_lattice_meet (ccp_prop_value_t
*val1
, ccp_prop_value_t
*val2
)
953 if (val1
->lattice_val
== UNDEFINED
)
955 /* UNDEFINED M any = any */
958 else if (val2
->lattice_val
== UNDEFINED
)
960 /* any M UNDEFINED = any
961 Nothing to do. VAL1 already contains the value we want. */
964 else if (val1
->lattice_val
== VARYING
965 || val2
->lattice_val
== VARYING
)
967 /* any M VARYING = VARYING. */
968 val1
->lattice_val
= VARYING
;
970 val1
->value
= NULL_TREE
;
972 else if (val1
->lattice_val
== CONSTANT
973 && val2
->lattice_val
== CONSTANT
974 && TREE_CODE (val1
->value
) == INTEGER_CST
975 && TREE_CODE (val2
->value
) == INTEGER_CST
)
977 /* Ci M Cj = Ci if (i == j)
978 Ci M Cj = VARYING if (i != j)
980 For INTEGER_CSTs mask unequal bits. If no equal bits remain,
982 val1
->mask
= (val1
->mask
| val2
->mask
983 | (wi::to_widest (val1
->value
)
984 ^ wi::to_widest (val2
->value
)));
985 if (val1
->mask
== -1)
987 val1
->lattice_val
= VARYING
;
988 val1
->value
= NULL_TREE
;
991 else if (val1
->lattice_val
== CONSTANT
992 && val2
->lattice_val
== CONSTANT
993 && simple_cst_equal (val1
->value
, val2
->value
) == 1)
995 /* Ci M Cj = Ci if (i == j)
996 Ci M Cj = VARYING if (i != j)
998 VAL1 already contains the value we want for equivalent values. */
1000 else if (val1
->lattice_val
== CONSTANT
1001 && val2
->lattice_val
== CONSTANT
1002 && (TREE_CODE (val1
->value
) == ADDR_EXPR
1003 || TREE_CODE (val2
->value
) == ADDR_EXPR
))
1005 /* When not equal addresses are involved try meeting for
1007 ccp_prop_value_t tem
= *val2
;
1008 if (TREE_CODE (val1
->value
) == ADDR_EXPR
)
1009 *val1
= get_value_for_expr (val1
->value
, true);
1010 if (TREE_CODE (val2
->value
) == ADDR_EXPR
)
1011 tem
= get_value_for_expr (val2
->value
, true);
1012 ccp_lattice_meet (val1
, &tem
);
1016 /* Any other combination is VARYING. */
1017 val1
->lattice_val
= VARYING
;
1019 val1
->value
= NULL_TREE
;
1024 /* Loop through the PHI_NODE's parameters for BLOCK and compare their
1025 lattice values to determine PHI_NODE's lattice value. The value of a
1026 PHI node is determined calling ccp_lattice_meet with all the arguments
1027 of the PHI node that are incoming via executable edges. */
1029 static enum ssa_prop_result
1030 ccp_visit_phi_node (gimple phi
)
1033 ccp_prop_value_t
*old_val
, new_val
;
1035 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1037 fprintf (dump_file
, "\nVisiting PHI node: ");
1038 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
1041 old_val
= get_value (gimple_phi_result (phi
));
1042 switch (old_val
->lattice_val
)
1045 return SSA_PROP_VARYING
;
1052 new_val
.lattice_val
= UNDEFINED
;
1053 new_val
.value
= NULL_TREE
;
1060 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
1062 /* Compute the meet operator over all the PHI arguments flowing
1063 through executable edges. */
1064 edge e
= gimple_phi_arg_edge (phi
, i
);
1066 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1069 "\n Argument #%d (%d -> %d %sexecutable)\n",
1070 i
, e
->src
->index
, e
->dest
->index
,
1071 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
1074 /* If the incoming edge is executable, Compute the meet operator for
1075 the existing value of the PHI node and the current PHI argument. */
1076 if (e
->flags
& EDGE_EXECUTABLE
)
1078 tree arg
= gimple_phi_arg (phi
, i
)->def
;
1079 ccp_prop_value_t arg_val
= get_value_for_expr (arg
, false);
1081 ccp_lattice_meet (&new_val
, &arg_val
);
1083 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1085 fprintf (dump_file
, "\t");
1086 print_generic_expr (dump_file
, arg
, dump_flags
);
1087 dump_lattice_value (dump_file
, "\tValue: ", arg_val
);
1088 fprintf (dump_file
, "\n");
1091 if (new_val
.lattice_val
== VARYING
)
1096 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1098 dump_lattice_value (dump_file
, "\n PHI node value: ", new_val
);
1099 fprintf (dump_file
, "\n\n");
1102 /* Make the transition to the new value. */
1103 if (set_lattice_value (gimple_phi_result (phi
), new_val
))
1105 if (new_val
.lattice_val
== VARYING
)
1106 return SSA_PROP_VARYING
;
1108 return SSA_PROP_INTERESTING
;
1111 return SSA_PROP_NOT_INTERESTING
;
1114 /* Return the constant value for OP or OP otherwise. */
1117 valueize_op (tree op
)
1119 if (TREE_CODE (op
) == SSA_NAME
)
1121 tree tem
= get_constant_value (op
);
1128 /* CCP specific front-end to the non-destructive constant folding
1131 Attempt to simplify the RHS of STMT knowing that one or more
1132 operands are constants.
1134 If simplification is possible, return the simplified RHS,
1135 otherwise return the original RHS or NULL_TREE. */
1138 ccp_fold (gimple stmt
)
1140 location_t loc
= gimple_location (stmt
);
1141 switch (gimple_code (stmt
))
1145 /* Handle comparison operators that can appear in GIMPLE form. */
1146 tree op0
= valueize_op (gimple_cond_lhs (stmt
));
1147 tree op1
= valueize_op (gimple_cond_rhs (stmt
));
1148 enum tree_code code
= gimple_cond_code (stmt
);
1149 return fold_binary_loc (loc
, code
, boolean_type_node
, op0
, op1
);
1154 /* Return the constant switch index. */
1155 return valueize_op (gimple_switch_index (stmt
));
1160 return gimple_fold_stmt_to_constant_1 (stmt
, valueize_op
);
1167 /* Apply the operation CODE in type TYPE to the value, mask pair
1168 RVAL and RMASK representing a value of type RTYPE and set
1169 the value, mask pair *VAL and *MASK to the result. */
1172 bit_value_unop_1 (enum tree_code code
, tree type
,
1173 widest_int
*val
, widest_int
*mask
,
1174 tree rtype
, const widest_int
&rval
, const widest_int
&rmask
)
1185 widest_int temv
, temm
;
1186 /* Return ~rval + 1. */
1187 bit_value_unop_1 (BIT_NOT_EXPR
, type
, &temv
, &temm
, type
, rval
, rmask
);
1188 bit_value_binop_1 (PLUS_EXPR
, type
, val
, mask
,
1189 type
, temv
, temm
, type
, 1, 0);
1197 /* First extend mask and value according to the original type. */
1198 sgn
= TYPE_SIGN (rtype
);
1199 *mask
= wi::ext (rmask
, TYPE_PRECISION (rtype
), sgn
);
1200 *val
= wi::ext (rval
, TYPE_PRECISION (rtype
), sgn
);
1202 /* Then extend mask and value according to the target type. */
1203 sgn
= TYPE_SIGN (type
);
1204 *mask
= wi::ext (*mask
, TYPE_PRECISION (type
), sgn
);
1205 *val
= wi::ext (*val
, TYPE_PRECISION (type
), sgn
);
1215 /* Apply the operation CODE in type TYPE to the value, mask pairs
1216 R1VAL, R1MASK and R2VAL, R2MASK representing a values of type R1TYPE
1217 and R2TYPE and set the value, mask pair *VAL and *MASK to the result. */
1220 bit_value_binop_1 (enum tree_code code
, tree type
,
1221 widest_int
*val
, widest_int
*mask
,
1222 tree r1type
, const widest_int
&r1val
,
1223 const widest_int
&r1mask
, tree r2type
,
1224 const widest_int
&r2val
, const widest_int
&r2mask
)
1226 signop sgn
= TYPE_SIGN (type
);
1227 int width
= TYPE_PRECISION (type
);
1228 bool swap_p
= false;
1230 /* Assume we'll get a constant result. Use an initial non varying
1231 value, we fall back to varying in the end if necessary. */
1237 /* The mask is constant where there is a known not
1238 set bit, (m1 | m2) & ((v1 | m1) & (v2 | m2)) */
1239 *mask
= (r1mask
| r2mask
) & (r1val
| r1mask
) & (r2val
| r2mask
);
1240 *val
= r1val
& r2val
;
1244 /* The mask is constant where there is a known
1245 set bit, (m1 | m2) & ~((v1 & ~m1) | (v2 & ~m2)). */
1246 *mask
= (r1mask
| r2mask
)
1247 .and_not (r1val
.and_not (r1mask
) | r2val
.and_not (r2mask
));
1248 *val
= r1val
| r2val
;
1253 *mask
= r1mask
| r2mask
;
1254 *val
= r1val
^ r2val
;
1261 widest_int shift
= r2val
;
1269 if (wi::neg_p (shift
))
1272 if (code
== RROTATE_EXPR
)
1273 code
= LROTATE_EXPR
;
1275 code
= RROTATE_EXPR
;
1277 if (code
== RROTATE_EXPR
)
1279 *mask
= wi::rrotate (r1mask
, shift
, width
);
1280 *val
= wi::rrotate (r1val
, shift
, width
);
1284 *mask
= wi::lrotate (r1mask
, shift
, width
);
1285 *val
= wi::lrotate (r1val
, shift
, width
);
1293 /* ??? We can handle partially known shift counts if we know
1294 its sign. That way we can tell that (x << (y | 8)) & 255
1298 widest_int shift
= r2val
;
1306 if (wi::neg_p (shift
))
1309 if (code
== RSHIFT_EXPR
)
1314 if (code
== RSHIFT_EXPR
)
1316 *mask
= wi::rshift (wi::ext (r1mask
, width
, sgn
), shift
, sgn
);
1317 *val
= wi::rshift (wi::ext (r1val
, width
, sgn
), shift
, sgn
);
1321 *mask
= wi::ext (wi::lshift (r1mask
, shift
), width
, sgn
);
1322 *val
= wi::ext (wi::lshift (r1val
, shift
), width
, sgn
);
1329 case POINTER_PLUS_EXPR
:
1331 /* Do the addition with unknown bits set to zero, to give carry-ins of
1332 zero wherever possible. */
1333 widest_int lo
= r1val
.and_not (r1mask
) + r2val
.and_not (r2mask
);
1334 lo
= wi::ext (lo
, width
, sgn
);
1335 /* Do the addition with unknown bits set to one, to give carry-ins of
1336 one wherever possible. */
1337 widest_int hi
= (r1val
| r1mask
) + (r2val
| r2mask
);
1338 hi
= wi::ext (hi
, width
, sgn
);
1339 /* Each bit in the result is known if (a) the corresponding bits in
1340 both inputs are known, and (b) the carry-in to that bit position
1341 is known. We can check condition (b) by seeing if we got the same
1342 result with minimised carries as with maximised carries. */
1343 *mask
= r1mask
| r2mask
| (lo
^ hi
);
1344 *mask
= wi::ext (*mask
, width
, sgn
);
1345 /* It shouldn't matter whether we choose lo or hi here. */
1352 widest_int temv
, temm
;
1353 bit_value_unop_1 (NEGATE_EXPR
, r2type
, &temv
, &temm
,
1354 r2type
, r2val
, r2mask
);
1355 bit_value_binop_1 (PLUS_EXPR
, type
, val
, mask
,
1356 r1type
, r1val
, r1mask
,
1357 r2type
, temv
, temm
);
1363 /* Just track trailing zeros in both operands and transfer
1364 them to the other. */
1365 int r1tz
= wi::ctz (r1val
| r1mask
);
1366 int r2tz
= wi::ctz (r2val
| r2mask
);
1367 if (r1tz
+ r2tz
>= width
)
1372 else if (r1tz
+ r2tz
> 0)
1374 *mask
= wi::ext (wi::mask
<widest_int
> (r1tz
+ r2tz
, true),
1384 widest_int m
= r1mask
| r2mask
;
1385 if (r1val
.and_not (m
) != r2val
.and_not (m
))
1388 *val
= ((code
== EQ_EXPR
) ? 0 : 1);
1392 /* We know the result of a comparison is always one or zero. */
1402 code
= swap_tree_comparison (code
);
1409 const widest_int
&o1val
= swap_p
? r2val
: r1val
;
1410 const widest_int
&o1mask
= swap_p
? r2mask
: r1mask
;
1411 const widest_int
&o2val
= swap_p
? r1val
: r2val
;
1412 const widest_int
&o2mask
= swap_p
? r1mask
: r2mask
;
1414 /* If the most significant bits are not known we know nothing. */
1415 if (wi::neg_p (o1mask
) || wi::neg_p (o2mask
))
1418 /* For comparisons the signedness is in the comparison operands. */
1419 sgn
= TYPE_SIGN (r1type
);
1421 /* If we know the most significant bits we know the values
1422 value ranges by means of treating varying bits as zero
1423 or one. Do a cross comparison of the max/min pairs. */
1424 maxmin
= wi::cmp (o1val
| o1mask
, o2val
.and_not (o2mask
), sgn
);
1425 minmax
= wi::cmp (o1val
.and_not (o1mask
), o2val
| o2mask
, sgn
);
1426 if (maxmin
< 0) /* o1 is less than o2. */
1431 else if (minmax
> 0) /* o1 is not less or equal to o2. */
1436 else if (maxmin
== minmax
) /* o1 and o2 are equal. */
1438 /* This probably should never happen as we'd have
1439 folded the thing during fully constant value folding. */
1441 *val
= (code
== LE_EXPR
? 1 : 0);
1445 /* We know the result of a comparison is always one or zero. */
1456 /* Return the propagation value when applying the operation CODE to
1457 the value RHS yielding type TYPE. */
1459 static ccp_prop_value_t
1460 bit_value_unop (enum tree_code code
, tree type
, tree rhs
)
1462 ccp_prop_value_t rval
= get_value_for_expr (rhs
, true);
1463 widest_int value
, mask
;
1464 ccp_prop_value_t val
;
1466 if (rval
.lattice_val
== UNDEFINED
)
1469 gcc_assert ((rval
.lattice_val
== CONSTANT
1470 && TREE_CODE (rval
.value
) == INTEGER_CST
)
1471 || rval
.mask
== -1);
1472 bit_value_unop_1 (code
, type
, &value
, &mask
,
1473 TREE_TYPE (rhs
), value_to_wide_int (rval
), rval
.mask
);
1476 val
.lattice_val
= CONSTANT
;
1478 /* ??? Delay building trees here. */
1479 val
.value
= wide_int_to_tree (type
, value
);
1483 val
.lattice_val
= VARYING
;
1484 val
.value
= NULL_TREE
;
1490 /* Return the propagation value when applying the operation CODE to
1491 the values RHS1 and RHS2 yielding type TYPE. */
1493 static ccp_prop_value_t
1494 bit_value_binop (enum tree_code code
, tree type
, tree rhs1
, tree rhs2
)
1496 ccp_prop_value_t r1val
= get_value_for_expr (rhs1
, true);
1497 ccp_prop_value_t r2val
= get_value_for_expr (rhs2
, true);
1498 widest_int value
, mask
;
1499 ccp_prop_value_t val
;
1501 if (r1val
.lattice_val
== UNDEFINED
1502 || r2val
.lattice_val
== UNDEFINED
)
1504 val
.lattice_val
= VARYING
;
1505 val
.value
= NULL_TREE
;
1510 gcc_assert ((r1val
.lattice_val
== CONSTANT
1511 && TREE_CODE (r1val
.value
) == INTEGER_CST
)
1512 || r1val
.mask
== -1);
1513 gcc_assert ((r2val
.lattice_val
== CONSTANT
1514 && TREE_CODE (r2val
.value
) == INTEGER_CST
)
1515 || r2val
.mask
== -1);
1516 bit_value_binop_1 (code
, type
, &value
, &mask
,
1517 TREE_TYPE (rhs1
), value_to_wide_int (r1val
), r1val
.mask
,
1518 TREE_TYPE (rhs2
), value_to_wide_int (r2val
), r2val
.mask
);
1521 val
.lattice_val
= CONSTANT
;
1523 /* ??? Delay building trees here. */
1524 val
.value
= wide_int_to_tree (type
, value
);
1528 val
.lattice_val
= VARYING
;
1529 val
.value
= NULL_TREE
;
1535 /* Return the propagation value for __builtin_assume_aligned
1536 and functions with assume_aligned or alloc_aligned attribute.
1537 For __builtin_assume_aligned, ATTR is NULL_TREE,
1538 for assume_aligned attribute ATTR is non-NULL and ALLOC_ALIGNED
1539 is false, for alloc_aligned attribute ATTR is non-NULL and
1540 ALLOC_ALIGNED is true. */
1542 static ccp_prop_value_t
1543 bit_value_assume_aligned (gimple stmt
, tree attr
, ccp_prop_value_t ptrval
,
1546 tree align
, misalign
= NULL_TREE
, type
;
1547 unsigned HOST_WIDE_INT aligni
, misaligni
= 0;
1548 ccp_prop_value_t alignval
;
1549 widest_int value
, mask
;
1550 ccp_prop_value_t val
;
1552 if (attr
== NULL_TREE
)
1554 tree ptr
= gimple_call_arg (stmt
, 0);
1555 type
= TREE_TYPE (ptr
);
1556 ptrval
= get_value_for_expr (ptr
, true);
1560 tree lhs
= gimple_call_lhs (stmt
);
1561 type
= TREE_TYPE (lhs
);
1564 if (ptrval
.lattice_val
== UNDEFINED
)
1566 gcc_assert ((ptrval
.lattice_val
== CONSTANT
1567 && TREE_CODE (ptrval
.value
) == INTEGER_CST
)
1568 || ptrval
.mask
== -1);
1569 if (attr
== NULL_TREE
)
1571 /* Get aligni and misaligni from __builtin_assume_aligned. */
1572 align
= gimple_call_arg (stmt
, 1);
1573 if (!tree_fits_uhwi_p (align
))
1575 aligni
= tree_to_uhwi (align
);
1576 if (gimple_call_num_args (stmt
) > 2)
1578 misalign
= gimple_call_arg (stmt
, 2);
1579 if (!tree_fits_uhwi_p (misalign
))
1581 misaligni
= tree_to_uhwi (misalign
);
1586 /* Get aligni and misaligni from assume_aligned or
1587 alloc_align attributes. */
1588 if (TREE_VALUE (attr
) == NULL_TREE
)
1590 attr
= TREE_VALUE (attr
);
1591 align
= TREE_VALUE (attr
);
1592 if (!tree_fits_uhwi_p (align
))
1594 aligni
= tree_to_uhwi (align
);
1597 if (aligni
== 0 || aligni
> gimple_call_num_args (stmt
))
1599 align
= gimple_call_arg (stmt
, aligni
- 1);
1600 if (!tree_fits_uhwi_p (align
))
1602 aligni
= tree_to_uhwi (align
);
1604 else if (TREE_CHAIN (attr
) && TREE_VALUE (TREE_CHAIN (attr
)))
1606 misalign
= TREE_VALUE (TREE_CHAIN (attr
));
1607 if (!tree_fits_uhwi_p (misalign
))
1609 misaligni
= tree_to_uhwi (misalign
);
1612 if (aligni
<= 1 || (aligni
& (aligni
- 1)) != 0 || misaligni
>= aligni
)
1615 align
= build_int_cst_type (type
, -aligni
);
1616 alignval
= get_value_for_expr (align
, true);
1617 bit_value_binop_1 (BIT_AND_EXPR
, type
, &value
, &mask
,
1618 type
, value_to_wide_int (ptrval
), ptrval
.mask
,
1619 type
, value_to_wide_int (alignval
), alignval
.mask
);
1622 val
.lattice_val
= CONSTANT
;
1624 gcc_assert ((mask
.to_uhwi () & (aligni
- 1)) == 0);
1625 gcc_assert ((value
.to_uhwi () & (aligni
- 1)) == 0);
1627 /* ??? Delay building trees here. */
1628 val
.value
= wide_int_to_tree (type
, value
);
1632 val
.lattice_val
= VARYING
;
1633 val
.value
= NULL_TREE
;
1639 /* Evaluate statement STMT.
1640 Valid only for assignments, calls, conditionals, and switches. */
1642 static ccp_prop_value_t
1643 evaluate_stmt (gimple stmt
)
1645 ccp_prop_value_t val
;
1646 tree simplified
= NULL_TREE
;
1647 ccp_lattice_t likelyvalue
= likely_value (stmt
);
1648 bool is_constant
= false;
1651 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1653 fprintf (dump_file
, "which is likely ");
1654 switch (likelyvalue
)
1657 fprintf (dump_file
, "CONSTANT");
1660 fprintf (dump_file
, "UNDEFINED");
1663 fprintf (dump_file
, "VARYING");
1667 fprintf (dump_file
, "\n");
1670 /* If the statement is likely to have a CONSTANT result, then try
1671 to fold the statement to determine the constant value. */
1672 /* FIXME. This is the only place that we call ccp_fold.
1673 Since likely_value never returns CONSTANT for calls, we will
1674 not attempt to fold them, including builtins that may profit. */
1675 if (likelyvalue
== CONSTANT
)
1677 fold_defer_overflow_warnings ();
1678 simplified
= ccp_fold (stmt
);
1679 is_constant
= simplified
&& is_gimple_min_invariant (simplified
);
1680 fold_undefer_overflow_warnings (is_constant
, stmt
, 0);
1683 /* The statement produced a constant value. */
1684 val
.lattice_val
= CONSTANT
;
1685 val
.value
= simplified
;
1689 /* If the statement is likely to have a VARYING result, then do not
1690 bother folding the statement. */
1691 else if (likelyvalue
== VARYING
)
1693 enum gimple_code code
= gimple_code (stmt
);
1694 if (code
== GIMPLE_ASSIGN
)
1696 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
1698 /* Other cases cannot satisfy is_gimple_min_invariant
1700 if (get_gimple_rhs_class (subcode
) == GIMPLE_SINGLE_RHS
)
1701 simplified
= gimple_assign_rhs1 (stmt
);
1703 else if (code
== GIMPLE_SWITCH
)
1704 simplified
= gimple_switch_index (stmt
);
1706 /* These cannot satisfy is_gimple_min_invariant without folding. */
1707 gcc_assert (code
== GIMPLE_CALL
|| code
== GIMPLE_COND
);
1708 is_constant
= simplified
&& is_gimple_min_invariant (simplified
);
1711 /* The statement produced a constant value. */
1712 val
.lattice_val
= CONSTANT
;
1713 val
.value
= simplified
;
1718 /* Resort to simplification for bitwise tracking. */
1719 if (flag_tree_bit_ccp
1720 && (likelyvalue
== CONSTANT
|| is_gimple_call (stmt
))
1723 enum gimple_code code
= gimple_code (stmt
);
1724 val
.lattice_val
= VARYING
;
1725 val
.value
= NULL_TREE
;
1727 if (code
== GIMPLE_ASSIGN
)
1729 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
1730 tree rhs1
= gimple_assign_rhs1 (stmt
);
1731 switch (get_gimple_rhs_class (subcode
))
1733 case GIMPLE_SINGLE_RHS
:
1734 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
1735 || POINTER_TYPE_P (TREE_TYPE (rhs1
)))
1736 val
= get_value_for_expr (rhs1
, true);
1739 case GIMPLE_UNARY_RHS
:
1740 if ((INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
1741 || POINTER_TYPE_P (TREE_TYPE (rhs1
)))
1742 && (INTEGRAL_TYPE_P (gimple_expr_type (stmt
))
1743 || POINTER_TYPE_P (gimple_expr_type (stmt
))))
1744 val
= bit_value_unop (subcode
, gimple_expr_type (stmt
), rhs1
);
1747 case GIMPLE_BINARY_RHS
:
1748 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
1749 || POINTER_TYPE_P (TREE_TYPE (rhs1
)))
1751 tree lhs
= gimple_assign_lhs (stmt
);
1752 tree rhs2
= gimple_assign_rhs2 (stmt
);
1753 val
= bit_value_binop (subcode
,
1754 TREE_TYPE (lhs
), rhs1
, rhs2
);
1761 else if (code
== GIMPLE_COND
)
1763 enum tree_code code
= gimple_cond_code (stmt
);
1764 tree rhs1
= gimple_cond_lhs (stmt
);
1765 tree rhs2
= gimple_cond_rhs (stmt
);
1766 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
1767 || POINTER_TYPE_P (TREE_TYPE (rhs1
)))
1768 val
= bit_value_binop (code
, TREE_TYPE (rhs1
), rhs1
, rhs2
);
1770 else if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
1772 tree fndecl
= gimple_call_fndecl (stmt
);
1773 switch (DECL_FUNCTION_CODE (fndecl
))
1775 case BUILT_IN_MALLOC
:
1776 case BUILT_IN_REALLOC
:
1777 case BUILT_IN_CALLOC
:
1778 case BUILT_IN_STRDUP
:
1779 case BUILT_IN_STRNDUP
:
1780 val
.lattice_val
= CONSTANT
;
1781 val
.value
= build_int_cst (TREE_TYPE (gimple_get_lhs (stmt
)), 0);
1782 val
.mask
= ~((HOST_WIDE_INT
) MALLOC_ABI_ALIGNMENT
1783 / BITS_PER_UNIT
- 1);
1786 case BUILT_IN_ALLOCA
:
1787 case BUILT_IN_ALLOCA_WITH_ALIGN
:
1788 align
= (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_ALLOCA_WITH_ALIGN
1789 ? TREE_INT_CST_LOW (gimple_call_arg (stmt
, 1))
1790 : BIGGEST_ALIGNMENT
);
1791 val
.lattice_val
= CONSTANT
;
1792 val
.value
= build_int_cst (TREE_TYPE (gimple_get_lhs (stmt
)), 0);
1793 val
.mask
= ~((HOST_WIDE_INT
) align
/ BITS_PER_UNIT
- 1);
1796 /* These builtins return their first argument, unmodified. */
1797 case BUILT_IN_MEMCPY
:
1798 case BUILT_IN_MEMMOVE
:
1799 case BUILT_IN_MEMSET
:
1800 case BUILT_IN_STRCPY
:
1801 case BUILT_IN_STRNCPY
:
1802 case BUILT_IN_MEMCPY_CHK
:
1803 case BUILT_IN_MEMMOVE_CHK
:
1804 case BUILT_IN_MEMSET_CHK
:
1805 case BUILT_IN_STRCPY_CHK
:
1806 case BUILT_IN_STRNCPY_CHK
:
1807 val
= get_value_for_expr (gimple_call_arg (stmt
, 0), true);
1810 case BUILT_IN_ASSUME_ALIGNED
:
1811 val
= bit_value_assume_aligned (stmt
, NULL_TREE
, val
, false);
1814 case BUILT_IN_ALIGNED_ALLOC
:
1816 tree align
= get_constant_value (gimple_call_arg (stmt
, 0));
1818 && tree_fits_uhwi_p (align
))
1820 unsigned HOST_WIDE_INT aligni
= tree_to_uhwi (align
);
1822 /* align must be power-of-two */
1823 && (aligni
& (aligni
- 1)) == 0)
1825 val
.lattice_val
= CONSTANT
;
1826 val
.value
= build_int_cst (ptr_type_node
, 0);
1836 if (is_gimple_call (stmt
) && gimple_call_lhs (stmt
))
1838 tree fntype
= gimple_call_fntype (stmt
);
1841 tree attrs
= lookup_attribute ("assume_aligned",
1842 TYPE_ATTRIBUTES (fntype
));
1844 val
= bit_value_assume_aligned (stmt
, attrs
, val
, false);
1845 attrs
= lookup_attribute ("alloc_align",
1846 TYPE_ATTRIBUTES (fntype
));
1848 val
= bit_value_assume_aligned (stmt
, attrs
, val
, true);
1851 is_constant
= (val
.lattice_val
== CONSTANT
);
1854 if (flag_tree_bit_ccp
1855 && ((is_constant
&& TREE_CODE (val
.value
) == INTEGER_CST
)
1856 || (!is_constant
&& likelyvalue
!= UNDEFINED
))
1857 && gimple_get_lhs (stmt
)
1858 && TREE_CODE (gimple_get_lhs (stmt
)) == SSA_NAME
)
1860 tree lhs
= gimple_get_lhs (stmt
);
1861 wide_int nonzero_bits
= get_nonzero_bits (lhs
);
1862 if (nonzero_bits
!= -1)
1866 val
.lattice_val
= CONSTANT
;
1867 val
.value
= build_zero_cst (TREE_TYPE (lhs
));
1868 val
.mask
= extend_mask (nonzero_bits
);
1873 if (wi::bit_and_not (val
.value
, nonzero_bits
) != 0)
1874 val
.value
= wide_int_to_tree (TREE_TYPE (lhs
),
1875 nonzero_bits
& val
.value
);
1876 if (nonzero_bits
== 0)
1879 val
.mask
= val
.mask
& extend_mask (nonzero_bits
);
1886 /* The statement produced a nonconstant value. If the statement
1887 had UNDEFINED operands, then the result of the statement
1888 should be UNDEFINED. Otherwise, the statement is VARYING. */
1889 if (likelyvalue
== UNDEFINED
)
1891 val
.lattice_val
= likelyvalue
;
1896 val
.lattice_val
= VARYING
;
1900 val
.value
= NULL_TREE
;
1906 typedef hash_table
<pointer_hash
<gimple_statement_base
> > gimple_htab
;
1908 /* Given a BUILT_IN_STACK_SAVE value SAVED_VAL, insert a clobber of VAR before
1909 each matching BUILT_IN_STACK_RESTORE. Mark visited phis in VISITED. */
1912 insert_clobber_before_stack_restore (tree saved_val
, tree var
,
1913 gimple_htab
**visited
)
1915 gimple stmt
, clobber_stmt
;
1917 imm_use_iterator iter
;
1918 gimple_stmt_iterator i
;
1921 FOR_EACH_IMM_USE_STMT (stmt
, iter
, saved_val
)
1922 if (gimple_call_builtin_p (stmt
, BUILT_IN_STACK_RESTORE
))
1924 clobber
= build_constructor (TREE_TYPE (var
),
1926 TREE_THIS_VOLATILE (clobber
) = 1;
1927 clobber_stmt
= gimple_build_assign (var
, clobber
);
1929 i
= gsi_for_stmt (stmt
);
1930 gsi_insert_before (&i
, clobber_stmt
, GSI_SAME_STMT
);
1932 else if (gimple_code (stmt
) == GIMPLE_PHI
)
1935 *visited
= new gimple_htab (10);
1937 slot
= (*visited
)->find_slot (stmt
, INSERT
);
1942 insert_clobber_before_stack_restore (gimple_phi_result (stmt
), var
,
1945 else if (gimple_assign_ssa_name_copy_p (stmt
))
1946 insert_clobber_before_stack_restore (gimple_assign_lhs (stmt
), var
,
1949 gcc_assert (is_gimple_debug (stmt
));
1952 /* Advance the iterator to the previous non-debug gimple statement in the same
1953 or dominating basic block. */
1956 gsi_prev_dom_bb_nondebug (gimple_stmt_iterator
*i
)
1960 gsi_prev_nondebug (i
);
1961 while (gsi_end_p (*i
))
1963 dom
= get_immediate_dominator (CDI_DOMINATORS
, i
->bb
);
1964 if (dom
== NULL
|| dom
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1967 *i
= gsi_last_bb (dom
);
1971 /* Find a BUILT_IN_STACK_SAVE dominating gsi_stmt (I), and insert
1972 a clobber of VAR before each matching BUILT_IN_STACK_RESTORE.
1974 It is possible that BUILT_IN_STACK_SAVE cannot be find in a dominator when a
1975 previous pass (such as DOM) duplicated it along multiple paths to a BB. In
1976 that case the function gives up without inserting the clobbers. */
1979 insert_clobbers_for_var (gimple_stmt_iterator i
, tree var
)
1983 gimple_htab
*visited
= NULL
;
1985 for (; !gsi_end_p (i
); gsi_prev_dom_bb_nondebug (&i
))
1987 stmt
= gsi_stmt (i
);
1989 if (!gimple_call_builtin_p (stmt
, BUILT_IN_STACK_SAVE
))
1992 saved_val
= gimple_call_lhs (stmt
);
1993 if (saved_val
== NULL_TREE
)
1996 insert_clobber_before_stack_restore (saved_val
, var
, &visited
);
2003 /* Detects a __builtin_alloca_with_align with constant size argument. Declares
2004 fixed-size array and returns the address, if found, otherwise returns
2008 fold_builtin_alloca_with_align (gimple stmt
)
2010 unsigned HOST_WIDE_INT size
, threshold
, n_elem
;
2011 tree lhs
, arg
, block
, var
, elem_type
, array_type
;
2014 lhs
= gimple_call_lhs (stmt
);
2015 if (lhs
== NULL_TREE
)
2018 /* Detect constant argument. */
2019 arg
= get_constant_value (gimple_call_arg (stmt
, 0));
2020 if (arg
== NULL_TREE
2021 || TREE_CODE (arg
) != INTEGER_CST
2022 || !tree_fits_uhwi_p (arg
))
2025 size
= tree_to_uhwi (arg
);
2027 /* Heuristic: don't fold large allocas. */
2028 threshold
= (unsigned HOST_WIDE_INT
)PARAM_VALUE (PARAM_LARGE_STACK_FRAME
);
2029 /* In case the alloca is located at function entry, it has the same lifetime
2030 as a declared array, so we allow a larger size. */
2031 block
= gimple_block (stmt
);
2032 if (!(cfun
->after_inlining
2033 && TREE_CODE (BLOCK_SUPERCONTEXT (block
)) == FUNCTION_DECL
))
2035 if (size
> threshold
)
2038 /* Declare array. */
2039 elem_type
= build_nonstandard_integer_type (BITS_PER_UNIT
, 1);
2040 n_elem
= size
* 8 / BITS_PER_UNIT
;
2041 array_type
= build_array_type_nelts (elem_type
, n_elem
);
2042 var
= create_tmp_var (array_type
, NULL
);
2043 DECL_ALIGN (var
) = TREE_INT_CST_LOW (gimple_call_arg (stmt
, 1));
2045 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (lhs
);
2046 if (pi
!= NULL
&& !pi
->pt
.anything
)
2050 singleton_p
= pt_solution_singleton_p (&pi
->pt
, &uid
);
2051 gcc_assert (singleton_p
);
2052 SET_DECL_PT_UID (var
, uid
);
2056 /* Fold alloca to the address of the array. */
2057 return fold_convert (TREE_TYPE (lhs
), build_fold_addr_expr (var
));
2060 /* Fold the stmt at *GSI with CCP specific information that propagating
2061 and regular folding does not catch. */
2064 ccp_fold_stmt (gimple_stmt_iterator
*gsi
)
2066 gimple stmt
= gsi_stmt (*gsi
);
2068 switch (gimple_code (stmt
))
2072 ccp_prop_value_t val
;
2073 /* Statement evaluation will handle type mismatches in constants
2074 more gracefully than the final propagation. This allows us to
2075 fold more conditionals here. */
2076 val
= evaluate_stmt (stmt
);
2077 if (val
.lattice_val
!= CONSTANT
2083 fprintf (dump_file
, "Folding predicate ");
2084 print_gimple_expr (dump_file
, stmt
, 0, 0);
2085 fprintf (dump_file
, " to ");
2086 print_generic_expr (dump_file
, val
.value
, 0);
2087 fprintf (dump_file
, "\n");
2090 if (integer_zerop (val
.value
))
2091 gimple_cond_make_false (stmt
);
2093 gimple_cond_make_true (stmt
);
2100 tree lhs
= gimple_call_lhs (stmt
);
2101 int flags
= gimple_call_flags (stmt
);
2104 bool changed
= false;
2107 /* If the call was folded into a constant make sure it goes
2108 away even if we cannot propagate into all uses because of
2111 && TREE_CODE (lhs
) == SSA_NAME
2112 && (val
= get_constant_value (lhs
))
2113 /* Don't optimize away calls that have side-effects. */
2114 && (flags
& (ECF_CONST
|ECF_PURE
)) != 0
2115 && (flags
& ECF_LOOPING_CONST_OR_PURE
) == 0)
2117 tree new_rhs
= unshare_expr (val
);
2119 if (!useless_type_conversion_p (TREE_TYPE (lhs
),
2120 TREE_TYPE (new_rhs
)))
2121 new_rhs
= fold_convert (TREE_TYPE (lhs
), new_rhs
);
2122 res
= update_call_from_tree (gsi
, new_rhs
);
2127 /* Internal calls provide no argument types, so the extra laxity
2128 for normal calls does not apply. */
2129 if (gimple_call_internal_p (stmt
))
2132 /* The heuristic of fold_builtin_alloca_with_align differs before and
2133 after inlining, so we don't require the arg to be changed into a
2134 constant for folding, but just to be constant. */
2135 if (gimple_call_builtin_p (stmt
, BUILT_IN_ALLOCA_WITH_ALIGN
))
2137 tree new_rhs
= fold_builtin_alloca_with_align (stmt
);
2140 bool res
= update_call_from_tree (gsi
, new_rhs
);
2141 tree var
= TREE_OPERAND (TREE_OPERAND (new_rhs
, 0),0);
2143 insert_clobbers_for_var (*gsi
, var
);
2148 /* Propagate into the call arguments. Compared to replace_uses_in
2149 this can use the argument slot types for type verification
2150 instead of the current argument type. We also can safely
2151 drop qualifiers here as we are dealing with constants anyway. */
2152 argt
= TYPE_ARG_TYPES (gimple_call_fntype (stmt
));
2153 for (i
= 0; i
< gimple_call_num_args (stmt
) && argt
;
2154 ++i
, argt
= TREE_CHAIN (argt
))
2156 tree arg
= gimple_call_arg (stmt
, i
);
2157 if (TREE_CODE (arg
) == SSA_NAME
2158 && (val
= get_constant_value (arg
))
2159 && useless_type_conversion_p
2160 (TYPE_MAIN_VARIANT (TREE_VALUE (argt
)),
2161 TYPE_MAIN_VARIANT (TREE_TYPE (val
))))
2163 gimple_call_set_arg (stmt
, i
, unshare_expr (val
));
2173 tree lhs
= gimple_assign_lhs (stmt
);
2176 /* If we have a load that turned out to be constant replace it
2177 as we cannot propagate into all uses in all cases. */
2178 if (gimple_assign_single_p (stmt
)
2179 && TREE_CODE (lhs
) == SSA_NAME
2180 && (val
= get_constant_value (lhs
)))
2182 tree rhs
= unshare_expr (val
);
2183 if (!useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (rhs
)))
2184 rhs
= fold_build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (lhs
), rhs
);
2185 gimple_assign_set_rhs_from_tree (gsi
, rhs
);
2197 /* Visit the assignment statement STMT. Set the value of its LHS to the
2198 value computed by the RHS and store LHS in *OUTPUT_P. If STMT
2199 creates virtual definitions, set the value of each new name to that
2200 of the RHS (if we can derive a constant out of the RHS).
2201 Value-returning call statements also perform an assignment, and
2202 are handled here. */
2204 static enum ssa_prop_result
2205 visit_assignment (gimple stmt
, tree
*output_p
)
2207 ccp_prop_value_t val
;
2208 enum ssa_prop_result retval
;
2210 tree lhs
= gimple_get_lhs (stmt
);
2212 gcc_assert (gimple_code (stmt
) != GIMPLE_CALL
2213 || gimple_call_lhs (stmt
) != NULL_TREE
);
2215 if (gimple_assign_single_p (stmt
)
2216 && gimple_assign_rhs_code (stmt
) == SSA_NAME
)
2217 /* For a simple copy operation, we copy the lattice values. */
2218 val
= *get_value (gimple_assign_rhs1 (stmt
));
2220 /* Evaluate the statement, which could be
2221 either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
2222 val
= evaluate_stmt (stmt
);
2224 retval
= SSA_PROP_NOT_INTERESTING
;
2226 /* Set the lattice value of the statement's output. */
2227 if (TREE_CODE (lhs
) == SSA_NAME
)
2229 /* If STMT is an assignment to an SSA_NAME, we only have one
2231 if (set_lattice_value (lhs
, val
))
2234 if (val
.lattice_val
== VARYING
)
2235 retval
= SSA_PROP_VARYING
;
2237 retval
= SSA_PROP_INTERESTING
;
2245 /* Visit the conditional statement STMT. Return SSA_PROP_INTERESTING
2246 if it can determine which edge will be taken. Otherwise, return
2247 SSA_PROP_VARYING. */
2249 static enum ssa_prop_result
2250 visit_cond_stmt (gimple stmt
, edge
*taken_edge_p
)
2252 ccp_prop_value_t val
;
2255 block
= gimple_bb (stmt
);
2256 val
= evaluate_stmt (stmt
);
2257 if (val
.lattice_val
!= CONSTANT
2259 return SSA_PROP_VARYING
;
2261 /* Find which edge out of the conditional block will be taken and add it
2262 to the worklist. If no single edge can be determined statically,
2263 return SSA_PROP_VARYING to feed all the outgoing edges to the
2264 propagation engine. */
2265 *taken_edge_p
= find_taken_edge (block
, val
.value
);
2267 return SSA_PROP_INTERESTING
;
2269 return SSA_PROP_VARYING
;
2273 /* Evaluate statement STMT. If the statement produces an output value and
2274 its evaluation changes the lattice value of its output, return
2275 SSA_PROP_INTERESTING and set *OUTPUT_P to the SSA_NAME holding the
2278 If STMT is a conditional branch and we can determine its truth
2279 value, set *TAKEN_EDGE_P accordingly. If STMT produces a varying
2280 value, return SSA_PROP_VARYING. */
2282 static enum ssa_prop_result
2283 ccp_visit_stmt (gimple stmt
, edge
*taken_edge_p
, tree
*output_p
)
2288 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2290 fprintf (dump_file
, "\nVisiting statement:\n");
2291 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
2294 switch (gimple_code (stmt
))
2297 /* If the statement is an assignment that produces a single
2298 output value, evaluate its RHS to see if the lattice value of
2299 its output has changed. */
2300 return visit_assignment (stmt
, output_p
);
2303 /* A value-returning call also performs an assignment. */
2304 if (gimple_call_lhs (stmt
) != NULL_TREE
)
2305 return visit_assignment (stmt
, output_p
);
2310 /* If STMT is a conditional branch, see if we can determine
2311 which branch will be taken. */
2312 /* FIXME. It appears that we should be able to optimize
2313 computed GOTOs here as well. */
2314 return visit_cond_stmt (stmt
, taken_edge_p
);
2320 /* Any other kind of statement is not interesting for constant
2321 propagation and, therefore, not worth simulating. */
2322 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2323 fprintf (dump_file
, "No interesting values produced. Marked VARYING.\n");
2325 /* Definitions made by statements other than assignments to
2326 SSA_NAMEs represent unknown modifications to their outputs.
2327 Mark them VARYING. */
2328 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_ALL_DEFS
)
2330 ccp_prop_value_t v
= { VARYING
, NULL_TREE
, -1 };
2331 set_lattice_value (def
, v
);
2334 return SSA_PROP_VARYING
;
2338 /* Main entry point for SSA Conditional Constant Propagation. */
2343 unsigned int todo
= 0;
2344 calculate_dominance_info (CDI_DOMINATORS
);
2346 ssa_propagate (ccp_visit_stmt
, ccp_visit_phi_node
);
2347 if (ccp_finalize ())
2348 todo
= (TODO_cleanup_cfg
| TODO_update_ssa
);
2349 free_dominance_info (CDI_DOMINATORS
);
2356 const pass_data pass_data_ccp
=
2358 GIMPLE_PASS
, /* type */
2360 OPTGROUP_NONE
, /* optinfo_flags */
2361 TV_TREE_CCP
, /* tv_id */
2362 ( PROP_cfg
| PROP_ssa
), /* properties_required */
2363 0, /* properties_provided */
2364 0, /* properties_destroyed */
2365 0, /* todo_flags_start */
2366 TODO_update_address_taken
, /* todo_flags_finish */
2369 class pass_ccp
: public gimple_opt_pass
2372 pass_ccp (gcc::context
*ctxt
)
2373 : gimple_opt_pass (pass_data_ccp
, ctxt
)
2376 /* opt_pass methods: */
2377 opt_pass
* clone () { return new pass_ccp (m_ctxt
); }
2378 virtual bool gate (function
*) { return flag_tree_ccp
!= 0; }
2379 virtual unsigned int execute (function
*) { return do_ssa_ccp (); }
2381 }; // class pass_ccp
2386 make_pass_ccp (gcc::context
*ctxt
)
2388 return new pass_ccp (ctxt
);
2393 /* Try to optimize out __builtin_stack_restore. Optimize it out
2394 if there is another __builtin_stack_restore in the same basic
2395 block and no calls or ASM_EXPRs are in between, or if this block's
2396 only outgoing edge is to EXIT_BLOCK and there are no calls or
2397 ASM_EXPRs after this __builtin_stack_restore. */
2400 optimize_stack_restore (gimple_stmt_iterator i
)
2405 basic_block bb
= gsi_bb (i
);
2406 gimple call
= gsi_stmt (i
);
2408 if (gimple_code (call
) != GIMPLE_CALL
2409 || gimple_call_num_args (call
) != 1
2410 || TREE_CODE (gimple_call_arg (call
, 0)) != SSA_NAME
2411 || !POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call
, 0))))
2414 for (gsi_next (&i
); !gsi_end_p (i
); gsi_next (&i
))
2416 stmt
= gsi_stmt (i
);
2417 if (gimple_code (stmt
) == GIMPLE_ASM
)
2419 if (gimple_code (stmt
) != GIMPLE_CALL
)
2422 callee
= gimple_call_fndecl (stmt
);
2424 || DECL_BUILT_IN_CLASS (callee
) != BUILT_IN_NORMAL
2425 /* All regular builtins are ok, just obviously not alloca. */
2426 || DECL_FUNCTION_CODE (callee
) == BUILT_IN_ALLOCA
2427 || DECL_FUNCTION_CODE (callee
) == BUILT_IN_ALLOCA_WITH_ALIGN
)
2430 if (DECL_FUNCTION_CODE (callee
) == BUILT_IN_STACK_RESTORE
)
2431 goto second_stack_restore
;
2437 /* Allow one successor of the exit block, or zero successors. */
2438 switch (EDGE_COUNT (bb
->succs
))
2443 if (single_succ_edge (bb
)->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
2449 second_stack_restore
:
2451 /* If there's exactly one use, then zap the call to __builtin_stack_save.
2452 If there are multiple uses, then the last one should remove the call.
2453 In any case, whether the call to __builtin_stack_save can be removed
2454 or not is irrelevant to removing the call to __builtin_stack_restore. */
2455 if (has_single_use (gimple_call_arg (call
, 0)))
2457 gimple stack_save
= SSA_NAME_DEF_STMT (gimple_call_arg (call
, 0));
2458 if (is_gimple_call (stack_save
))
2460 callee
= gimple_call_fndecl (stack_save
);
2462 && DECL_BUILT_IN_CLASS (callee
) == BUILT_IN_NORMAL
2463 && DECL_FUNCTION_CODE (callee
) == BUILT_IN_STACK_SAVE
)
2465 gimple_stmt_iterator stack_save_gsi
;
2468 stack_save_gsi
= gsi_for_stmt (stack_save
);
2469 rhs
= build_int_cst (TREE_TYPE (gimple_call_arg (call
, 0)), 0);
2470 update_call_from_tree (&stack_save_gsi
, rhs
);
2475 /* No effect, so the statement will be deleted. */
2476 return integer_zero_node
;
2479 /* If va_list type is a simple pointer and nothing special is needed,
2480 optimize __builtin_va_start (&ap, 0) into ap = __builtin_next_arg (0),
2481 __builtin_va_end (&ap) out as NOP and __builtin_va_copy into a simple
2482 pointer assignment. */
2485 optimize_stdarg_builtin (gimple call
)
2487 tree callee
, lhs
, rhs
, cfun_va_list
;
2488 bool va_list_simple_ptr
;
2489 location_t loc
= gimple_location (call
);
2491 if (gimple_code (call
) != GIMPLE_CALL
)
2494 callee
= gimple_call_fndecl (call
);
2496 cfun_va_list
= targetm
.fn_abi_va_list (callee
);
2497 va_list_simple_ptr
= POINTER_TYPE_P (cfun_va_list
)
2498 && (TREE_TYPE (cfun_va_list
) == void_type_node
2499 || TREE_TYPE (cfun_va_list
) == char_type_node
);
2501 switch (DECL_FUNCTION_CODE (callee
))
2503 case BUILT_IN_VA_START
:
2504 if (!va_list_simple_ptr
2505 || targetm
.expand_builtin_va_start
!= NULL
2506 || !builtin_decl_explicit_p (BUILT_IN_NEXT_ARG
))
2509 if (gimple_call_num_args (call
) != 2)
2512 lhs
= gimple_call_arg (call
, 0);
2513 if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
2514 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs
)))
2515 != TYPE_MAIN_VARIANT (cfun_va_list
))
2518 lhs
= build_fold_indirect_ref_loc (loc
, lhs
);
2519 rhs
= build_call_expr_loc (loc
, builtin_decl_explicit (BUILT_IN_NEXT_ARG
),
2520 1, integer_zero_node
);
2521 rhs
= fold_convert_loc (loc
, TREE_TYPE (lhs
), rhs
);
2522 return build2 (MODIFY_EXPR
, TREE_TYPE (lhs
), lhs
, rhs
);
2524 case BUILT_IN_VA_COPY
:
2525 if (!va_list_simple_ptr
)
2528 if (gimple_call_num_args (call
) != 2)
2531 lhs
= gimple_call_arg (call
, 0);
2532 if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
2533 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs
)))
2534 != TYPE_MAIN_VARIANT (cfun_va_list
))
2537 lhs
= build_fold_indirect_ref_loc (loc
, lhs
);
2538 rhs
= gimple_call_arg (call
, 1);
2539 if (TYPE_MAIN_VARIANT (TREE_TYPE (rhs
))
2540 != TYPE_MAIN_VARIANT (cfun_va_list
))
2543 rhs
= fold_convert_loc (loc
, TREE_TYPE (lhs
), rhs
);
2544 return build2 (MODIFY_EXPR
, TREE_TYPE (lhs
), lhs
, rhs
);
2546 case BUILT_IN_VA_END
:
2547 /* No effect, so the statement will be deleted. */
2548 return integer_zero_node
;
2555 /* Attemp to make the block of __builtin_unreachable I unreachable by changing
2556 the incoming jumps. Return true if at least one jump was changed. */
2559 optimize_unreachable (gimple_stmt_iterator i
)
2561 basic_block bb
= gsi_bb (i
);
2562 gimple_stmt_iterator gsi
;
2568 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2570 stmt
= gsi_stmt (gsi
);
2572 if (is_gimple_debug (stmt
))
2575 if (gimple_code (stmt
) == GIMPLE_LABEL
)
2577 /* Verify we do not need to preserve the label. */
2578 if (FORCED_LABEL (gimple_label_label (stmt
)))
2584 /* Only handle the case that __builtin_unreachable is the first statement
2585 in the block. We rely on DCE to remove stmts without side-effects
2586 before __builtin_unreachable. */
2587 if (gsi_stmt (gsi
) != gsi_stmt (i
))
2592 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
2594 gsi
= gsi_last_bb (e
->src
);
2595 if (gsi_end_p (gsi
))
2598 stmt
= gsi_stmt (gsi
);
2599 if (gimple_code (stmt
) == GIMPLE_COND
)
2601 if (e
->flags
& EDGE_TRUE_VALUE
)
2602 gimple_cond_make_false (stmt
);
2603 else if (e
->flags
& EDGE_FALSE_VALUE
)
2604 gimple_cond_make_true (stmt
);
2611 /* Todo: handle other cases, f.i. switch statement. */
2621 /* A simple pass that attempts to fold all builtin functions. This pass
2622 is run after we've propagated as many constants as we can. */
2626 const pass_data pass_data_fold_builtins
=
2628 GIMPLE_PASS
, /* type */
2630 OPTGROUP_NONE
, /* optinfo_flags */
2631 TV_NONE
, /* tv_id */
2632 ( PROP_cfg
| PROP_ssa
), /* properties_required */
2633 0, /* properties_provided */
2634 0, /* properties_destroyed */
2635 0, /* todo_flags_start */
2636 TODO_update_ssa
, /* todo_flags_finish */
2639 class pass_fold_builtins
: public gimple_opt_pass
2642 pass_fold_builtins (gcc::context
*ctxt
)
2643 : gimple_opt_pass (pass_data_fold_builtins
, ctxt
)
2646 /* opt_pass methods: */
2647 opt_pass
* clone () { return new pass_fold_builtins (m_ctxt
); }
2648 virtual unsigned int execute (function
*);
2650 }; // class pass_fold_builtins
2653 pass_fold_builtins::execute (function
*fun
)
2655 bool cfg_changed
= false;
2657 unsigned int todoflags
= 0;
2659 FOR_EACH_BB_FN (bb
, fun
)
2661 gimple_stmt_iterator i
;
2662 for (i
= gsi_start_bb (bb
); !gsi_end_p (i
); )
2664 gimple stmt
, old_stmt
;
2666 enum built_in_function fcode
;
2668 stmt
= gsi_stmt (i
);
2670 if (gimple_code (stmt
) != GIMPLE_CALL
)
2672 /* Remove all *ssaname_N ={v} {CLOBBER}; stmts,
2673 after the last GIMPLE DSE they aren't needed and might
2674 unnecessarily keep the SSA_NAMEs live. */
2675 if (gimple_clobber_p (stmt
))
2677 tree lhs
= gimple_assign_lhs (stmt
);
2678 if (TREE_CODE (lhs
) == MEM_REF
2679 && TREE_CODE (TREE_OPERAND (lhs
, 0)) == SSA_NAME
)
2681 unlink_stmt_vdef (stmt
);
2682 gsi_remove (&i
, true);
2683 release_defs (stmt
);
2691 callee
= gimple_call_fndecl (stmt
);
2692 if (!callee
|| DECL_BUILT_IN_CLASS (callee
) != BUILT_IN_NORMAL
)
2698 fcode
= DECL_FUNCTION_CODE (callee
);
2703 tree result
= NULL_TREE
;
2704 switch (DECL_FUNCTION_CODE (callee
))
2706 case BUILT_IN_CONSTANT_P
:
2707 /* Resolve __builtin_constant_p. If it hasn't been
2708 folded to integer_one_node by now, it's fairly
2709 certain that the value simply isn't constant. */
2710 result
= integer_zero_node
;
2713 case BUILT_IN_ASSUME_ALIGNED
:
2714 /* Remove __builtin_assume_aligned. */
2715 result
= gimple_call_arg (stmt
, 0);
2718 case BUILT_IN_STACK_RESTORE
:
2719 result
= optimize_stack_restore (i
);
2725 case BUILT_IN_UNREACHABLE
:
2726 if (optimize_unreachable (i
))
2730 case BUILT_IN_VA_START
:
2731 case BUILT_IN_VA_END
:
2732 case BUILT_IN_VA_COPY
:
2733 /* These shouldn't be folded before pass_stdarg. */
2734 result
= optimize_stdarg_builtin (stmt
);
2748 if (!update_call_from_tree (&i
, result
))
2749 gimplify_and_update_call_from_tree (&i
, result
);
2752 todoflags
|= TODO_update_address_taken
;
2754 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2756 fprintf (dump_file
, "Simplified\n ");
2757 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
2761 stmt
= gsi_stmt (i
);
2764 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
)
2765 && gimple_purge_dead_eh_edges (bb
))
2768 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2770 fprintf (dump_file
, "to\n ");
2771 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
2772 fprintf (dump_file
, "\n");
2775 /* Retry the same statement if it changed into another
2776 builtin, there might be new opportunities now. */
2777 if (gimple_code (stmt
) != GIMPLE_CALL
)
2782 callee
= gimple_call_fndecl (stmt
);
2784 || DECL_BUILT_IN_CLASS (callee
) != BUILT_IN_NORMAL
2785 || DECL_FUNCTION_CODE (callee
) == fcode
)
2790 /* Delete unreachable blocks. */
2792 todoflags
|= TODO_cleanup_cfg
;
2800 make_pass_fold_builtins (gcc::context
*ctxt
)
2802 return new pass_fold_builtins (ctxt
);