1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "stor-layout.h"
29 #include "basic-block.h"
30 #include "tree-ssa-alias.h"
31 #include "internal-fn.h"
32 #include "gimple-fold.h"
34 #include "gimple-expr.h"
37 #include "gimple-iterator.h"
38 #include "gimple-walk.h"
39 #include "gimple-ssa.h"
41 #include "tree-phinodes.h"
42 #include "ssa-iterators.h"
43 #include "stringpool.h"
44 #include "tree-ssanames.h"
45 #include "tree-ssa-loop-manip.h"
46 #include "tree-ssa-loop-niter.h"
47 #include "tree-ssa-loop.h"
48 #include "tree-into-ssa.h"
50 #include "tree-pass.h"
51 #include "tree-dump.h"
52 #include "gimple-pretty-print.h"
53 #include "diagnostic-core.h"
56 #include "tree-scalar-evolution.h"
57 #include "tree-ssa-propagate.h"
58 #include "tree-chrec.h"
59 #include "tree-ssa-threadupdate.h"
62 #include "tree-ssa-threadedge.h"
67 /* Range of values that can be associated with an SSA_NAME after VRP
71 /* Lattice value represented by this range. */
72 enum value_range_type type
;
74 /* Minimum and maximum values represented by this range. These
75 values should be interpreted as follows:
77 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
80 - If TYPE == VR_RANGE then MIN holds the minimum value and
81 MAX holds the maximum value of the range [MIN, MAX].
83 - If TYPE == ANTI_RANGE the variable is known to NOT
84 take any values in the range [MIN, MAX]. */
88 /* Set of SSA names whose value ranges are equivalent to this one.
89 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
93 typedef struct value_range_d value_range_t
;
95 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
97 /* Set of SSA names found live during the RPO traversal of the function
98 for still active basic-blocks. */
101 /* Return true if the SSA name NAME is live on the edge E. */
104 live_on_edge (edge e
, tree name
)
106 return (live
[e
->dest
->index
]
107 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
110 /* Local functions. */
111 static int compare_values (tree val1
, tree val2
);
112 static int compare_values_warnv (tree val1
, tree val2
, bool *);
113 static void vrp_meet (value_range_t
*, value_range_t
*);
114 static void vrp_intersect_ranges (value_range_t
*, value_range_t
*);
115 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
116 tree
, tree
, bool, bool *,
119 /* Location information for ASSERT_EXPRs. Each instance of this
120 structure describes an ASSERT_EXPR for an SSA name. Since a single
121 SSA name may have more than one assertion associated with it, these
122 locations are kept in a linked list attached to the corresponding
124 struct assert_locus_d
126 /* Basic block where the assertion would be inserted. */
129 /* Some assertions need to be inserted on an edge (e.g., assertions
130 generated by COND_EXPRs). In those cases, BB will be NULL. */
133 /* Pointer to the statement that generated this assertion. */
134 gimple_stmt_iterator si
;
136 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
137 enum tree_code comp_code
;
139 /* Value being compared against. */
142 /* Expression to compare. */
145 /* Next node in the linked list. */
146 struct assert_locus_d
*next
;
149 typedef struct assert_locus_d
*assert_locus_t
;
151 /* If bit I is present, it means that SSA name N_i has a list of
152 assertions that should be inserted in the IL. */
153 static bitmap need_assert_for
;
155 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
156 holds a list of ASSERT_LOCUS_T nodes that describe where
157 ASSERT_EXPRs for SSA name N_I should be inserted. */
158 static assert_locus_t
*asserts_for
;
160 /* Value range array. After propagation, VR_VALUE[I] holds the range
161 of values that SSA name N_I may take. */
162 static unsigned num_vr_values
;
163 static value_range_t
**vr_value
;
164 static bool values_propagated
;
166 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
167 number of executable edges we saw the last time we visited the
169 static int *vr_phi_edge_counts
;
176 static vec
<edge
> to_remove_edges
;
177 static vec
<switch_update
> to_update_switch_stmts
;
180 /* Return the maximum value for TYPE. */
183 vrp_val_max (const_tree type
)
185 if (!INTEGRAL_TYPE_P (type
))
188 return TYPE_MAX_VALUE (type
);
191 /* Return the minimum value for TYPE. */
194 vrp_val_min (const_tree type
)
196 if (!INTEGRAL_TYPE_P (type
))
199 return TYPE_MIN_VALUE (type
);
202 /* Return whether VAL is equal to the maximum value of its type. This
203 will be true for a positive overflow infinity. We can't do a
204 simple equality comparison with TYPE_MAX_VALUE because C typedefs
205 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
206 to the integer constant with the same value in the type. */
209 vrp_val_is_max (const_tree val
)
211 tree type_max
= vrp_val_max (TREE_TYPE (val
));
212 return (val
== type_max
213 || (type_max
!= NULL_TREE
214 && operand_equal_p (val
, type_max
, 0)));
217 /* Return whether VAL is equal to the minimum value of its type. This
218 will be true for a negative overflow infinity. */
221 vrp_val_is_min (const_tree val
)
223 tree type_min
= vrp_val_min (TREE_TYPE (val
));
224 return (val
== type_min
225 || (type_min
!= NULL_TREE
226 && operand_equal_p (val
, type_min
, 0)));
230 /* Return whether TYPE should use an overflow infinity distinct from
231 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
232 represent a signed overflow during VRP computations. An infinity
233 is distinct from a half-range, which will go from some number to
234 TYPE_{MIN,MAX}_VALUE. */
237 needs_overflow_infinity (const_tree type
)
239 return INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
);
242 /* Return whether TYPE can support our overflow infinity
243 representation: we use the TREE_OVERFLOW flag, which only exists
244 for constants. If TYPE doesn't support this, we don't optimize
245 cases which would require signed overflow--we drop them to
249 supports_overflow_infinity (const_tree type
)
251 tree min
= vrp_val_min (type
), max
= vrp_val_max (type
);
252 #ifdef ENABLE_CHECKING
253 gcc_assert (needs_overflow_infinity (type
));
255 return (min
!= NULL_TREE
256 && CONSTANT_CLASS_P (min
)
258 && CONSTANT_CLASS_P (max
));
261 /* VAL is the maximum or minimum value of a type. Return a
262 corresponding overflow infinity. */
265 make_overflow_infinity (tree val
)
267 gcc_checking_assert (val
!= NULL_TREE
&& CONSTANT_CLASS_P (val
));
268 val
= copy_node (val
);
269 TREE_OVERFLOW (val
) = 1;
273 /* Return a negative overflow infinity for TYPE. */
276 negative_overflow_infinity (tree type
)
278 gcc_checking_assert (supports_overflow_infinity (type
));
279 return make_overflow_infinity (vrp_val_min (type
));
282 /* Return a positive overflow infinity for TYPE. */
285 positive_overflow_infinity (tree type
)
287 gcc_checking_assert (supports_overflow_infinity (type
));
288 return make_overflow_infinity (vrp_val_max (type
));
291 /* Return whether VAL is a negative overflow infinity. */
294 is_negative_overflow_infinity (const_tree val
)
296 return (TREE_OVERFLOW_P (val
)
297 && needs_overflow_infinity (TREE_TYPE (val
))
298 && vrp_val_is_min (val
));
301 /* Return whether VAL is a positive overflow infinity. */
304 is_positive_overflow_infinity (const_tree val
)
306 return (TREE_OVERFLOW_P (val
)
307 && needs_overflow_infinity (TREE_TYPE (val
))
308 && vrp_val_is_max (val
));
311 /* Return whether VAL is a positive or negative overflow infinity. */
314 is_overflow_infinity (const_tree val
)
316 return (TREE_OVERFLOW_P (val
)
317 && needs_overflow_infinity (TREE_TYPE (val
))
318 && (vrp_val_is_min (val
) || vrp_val_is_max (val
)));
321 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
324 stmt_overflow_infinity (gimple stmt
)
326 if (is_gimple_assign (stmt
)
327 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)) ==
329 return is_overflow_infinity (gimple_assign_rhs1 (stmt
));
333 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
334 the same value with TREE_OVERFLOW clear. This can be used to avoid
335 confusing a regular value with an overflow value. */
338 avoid_overflow_infinity (tree val
)
340 if (!is_overflow_infinity (val
))
343 if (vrp_val_is_max (val
))
344 return vrp_val_max (TREE_TYPE (val
));
347 gcc_checking_assert (vrp_val_is_min (val
));
348 return vrp_val_min (TREE_TYPE (val
));
353 /* Return true if ARG is marked with the nonnull attribute in the
354 current function signature. */
357 nonnull_arg_p (const_tree arg
)
359 tree t
, attrs
, fntype
;
360 unsigned HOST_WIDE_INT arg_num
;
362 gcc_assert (TREE_CODE (arg
) == PARM_DECL
&& POINTER_TYPE_P (TREE_TYPE (arg
)));
364 /* The static chain decl is always non null. */
365 if (arg
== cfun
->static_chain_decl
)
368 fntype
= TREE_TYPE (current_function_decl
);
369 for (attrs
= TYPE_ATTRIBUTES (fntype
); attrs
; attrs
= TREE_CHAIN (attrs
))
371 attrs
= lookup_attribute ("nonnull", attrs
);
373 /* If "nonnull" wasn't specified, we know nothing about the argument. */
374 if (attrs
== NULL_TREE
)
377 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
378 if (TREE_VALUE (attrs
) == NULL_TREE
)
381 /* Get the position number for ARG in the function signature. */
382 for (arg_num
= 1, t
= DECL_ARGUMENTS (current_function_decl
);
384 t
= DECL_CHAIN (t
), arg_num
++)
390 gcc_assert (t
== arg
);
392 /* Now see if ARG_NUM is mentioned in the nonnull list. */
393 for (t
= TREE_VALUE (attrs
); t
; t
= TREE_CHAIN (t
))
395 if (compare_tree_int (TREE_VALUE (t
), arg_num
) == 0)
404 /* Set value range VR to VR_UNDEFINED. */
407 set_value_range_to_undefined (value_range_t
*vr
)
409 vr
->type
= VR_UNDEFINED
;
410 vr
->min
= vr
->max
= NULL_TREE
;
412 bitmap_clear (vr
->equiv
);
416 /* Set value range VR to VR_VARYING. */
419 set_value_range_to_varying (value_range_t
*vr
)
421 vr
->type
= VR_VARYING
;
422 vr
->min
= vr
->max
= NULL_TREE
;
424 bitmap_clear (vr
->equiv
);
428 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
431 set_value_range (value_range_t
*vr
, enum value_range_type t
, tree min
,
432 tree max
, bitmap equiv
)
434 #if defined ENABLE_CHECKING
435 /* Check the validity of the range. */
436 if (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
)
440 gcc_assert (min
&& max
);
442 gcc_assert ((!TREE_OVERFLOW_P (min
) || is_overflow_infinity (min
))
443 && (!TREE_OVERFLOW_P (max
) || is_overflow_infinity (max
)));
445 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
446 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
448 cmp
= compare_values (min
, max
);
449 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
451 if (needs_overflow_infinity (TREE_TYPE (min
)))
452 gcc_assert (!is_overflow_infinity (min
)
453 || !is_overflow_infinity (max
));
456 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
457 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
459 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
460 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
467 /* Since updating the equivalence set involves deep copying the
468 bitmaps, only do it if absolutely necessary. */
469 if (vr
->equiv
== NULL
471 vr
->equiv
= BITMAP_ALLOC (NULL
);
473 if (equiv
!= vr
->equiv
)
475 if (equiv
&& !bitmap_empty_p (equiv
))
476 bitmap_copy (vr
->equiv
, equiv
);
478 bitmap_clear (vr
->equiv
);
483 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
484 This means adjusting T, MIN and MAX representing the case of a
485 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
486 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
487 In corner cases where MAX+1 or MIN-1 wraps this will fall back
489 This routine exists to ease canonicalization in the case where we
490 extract ranges from var + CST op limit. */
493 set_and_canonicalize_value_range (value_range_t
*vr
, enum value_range_type t
,
494 tree min
, tree max
, bitmap equiv
)
496 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
497 if (t
== VR_UNDEFINED
)
499 set_value_range_to_undefined (vr
);
502 else if (t
== VR_VARYING
)
504 set_value_range_to_varying (vr
);
508 /* Nothing to canonicalize for symbolic ranges. */
509 if (TREE_CODE (min
) != INTEGER_CST
510 || TREE_CODE (max
) != INTEGER_CST
)
512 set_value_range (vr
, t
, min
, max
, equiv
);
516 /* Wrong order for min and max, to swap them and the VR type we need
518 if (tree_int_cst_lt (max
, min
))
522 /* For one bit precision if max < min, then the swapped
523 range covers all values, so for VR_RANGE it is varying and
524 for VR_ANTI_RANGE empty range, so drop to varying as well. */
525 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
527 set_value_range_to_varying (vr
);
531 one
= build_int_cst (TREE_TYPE (min
), 1);
532 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
533 max
= int_const_binop (MINUS_EXPR
, min
, one
);
536 /* There's one corner case, if we had [C+1, C] before we now have
537 that again. But this represents an empty value range, so drop
538 to varying in this case. */
539 if (tree_int_cst_lt (max
, min
))
541 set_value_range_to_varying (vr
);
545 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
548 /* Anti-ranges that can be represented as ranges should be so. */
549 if (t
== VR_ANTI_RANGE
)
551 bool is_min
= vrp_val_is_min (min
);
552 bool is_max
= vrp_val_is_max (max
);
554 if (is_min
&& is_max
)
556 /* We cannot deal with empty ranges, drop to varying.
557 ??? This could be VR_UNDEFINED instead. */
558 set_value_range_to_varying (vr
);
561 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
562 && (is_min
|| is_max
))
564 /* Non-empty boolean ranges can always be represented
565 as a singleton range. */
567 min
= max
= vrp_val_max (TREE_TYPE (min
));
569 min
= max
= vrp_val_min (TREE_TYPE (min
));
573 /* As a special exception preserve non-null ranges. */
574 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
575 && integer_zerop (max
)))
577 tree one
= build_int_cst (TREE_TYPE (max
), 1);
578 min
= int_const_binop (PLUS_EXPR
, max
, one
);
579 max
= vrp_val_max (TREE_TYPE (max
));
584 tree one
= build_int_cst (TREE_TYPE (min
), 1);
585 max
= int_const_binop (MINUS_EXPR
, min
, one
);
586 min
= vrp_val_min (TREE_TYPE (min
));
591 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
592 if (needs_overflow_infinity (TREE_TYPE (min
))
593 && is_overflow_infinity (min
)
594 && is_overflow_infinity (max
))
596 set_value_range_to_varying (vr
);
600 set_value_range (vr
, t
, min
, max
, equiv
);
603 /* Copy value range FROM into value range TO. */
606 copy_value_range (value_range_t
*to
, value_range_t
*from
)
608 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
611 /* Set value range VR to a single value. This function is only called
612 with values we get from statements, and exists to clear the
613 TREE_OVERFLOW flag so that we don't think we have an overflow
614 infinity when we shouldn't. */
617 set_value_range_to_value (value_range_t
*vr
, tree val
, bitmap equiv
)
619 gcc_assert (is_gimple_min_invariant (val
));
620 if (TREE_OVERFLOW_P (val
))
621 val
= drop_tree_overflow (val
);
622 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
625 /* Set value range VR to a non-negative range of type TYPE.
626 OVERFLOW_INFINITY indicates whether to use an overflow infinity
627 rather than TYPE_MAX_VALUE; this should be true if we determine
628 that the range is nonnegative based on the assumption that signed
629 overflow does not occur. */
632 set_value_range_to_nonnegative (value_range_t
*vr
, tree type
,
633 bool overflow_infinity
)
637 if (overflow_infinity
&& !supports_overflow_infinity (type
))
639 set_value_range_to_varying (vr
);
643 zero
= build_int_cst (type
, 0);
644 set_value_range (vr
, VR_RANGE
, zero
,
646 ? positive_overflow_infinity (type
)
647 : TYPE_MAX_VALUE (type
)),
651 /* Set value range VR to a non-NULL range of type TYPE. */
654 set_value_range_to_nonnull (value_range_t
*vr
, tree type
)
656 tree zero
= build_int_cst (type
, 0);
657 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
661 /* Set value range VR to a NULL range of type TYPE. */
664 set_value_range_to_null (value_range_t
*vr
, tree type
)
666 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
670 /* Set value range VR to a range of a truthvalue of type TYPE. */
673 set_value_range_to_truthvalue (value_range_t
*vr
, tree type
)
675 if (TYPE_PRECISION (type
) == 1)
676 set_value_range_to_varying (vr
);
678 set_value_range (vr
, VR_RANGE
,
679 build_int_cst (type
, 0), build_int_cst (type
, 1),
684 /* If abs (min) < abs (max), set VR to [-max, max], if
685 abs (min) >= abs (max), set VR to [-min, min]. */
688 abs_extent_range (value_range_t
*vr
, tree min
, tree max
)
692 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
693 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
694 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
695 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
696 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
697 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
698 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
700 set_value_range_to_varying (vr
);
703 cmp
= compare_values (min
, max
);
705 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
706 else if (cmp
== 0 || cmp
== 1)
709 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
713 set_value_range_to_varying (vr
);
716 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
720 /* Return value range information for VAR.
722 If we have no values ranges recorded (ie, VRP is not running), then
723 return NULL. Otherwise create an empty range if none existed for VAR. */
725 static value_range_t
*
726 get_value_range (const_tree var
)
728 static const struct value_range_d vr_const_varying
729 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
732 unsigned ver
= SSA_NAME_VERSION (var
);
734 /* If we have no recorded ranges, then return NULL. */
738 /* If we query the range for a new SSA name return an unmodifiable VARYING.
739 We should get here at most from the substitute-and-fold stage which
740 will never try to change values. */
741 if (ver
>= num_vr_values
)
742 return CONST_CAST (value_range_t
*, &vr_const_varying
);
748 /* After propagation finished do not allocate new value-ranges. */
749 if (values_propagated
)
750 return CONST_CAST (value_range_t
*, &vr_const_varying
);
752 /* Create a default value range. */
753 vr_value
[ver
] = vr
= XCNEW (value_range_t
);
755 /* Defer allocating the equivalence set. */
758 /* If VAR is a default definition of a parameter, the variable can
759 take any value in VAR's type. */
760 if (SSA_NAME_IS_DEFAULT_DEF (var
))
762 sym
= SSA_NAME_VAR (var
);
763 if (TREE_CODE (sym
) == PARM_DECL
)
765 /* Try to use the "nonnull" attribute to create ~[0, 0]
766 anti-ranges for pointers. Note that this is only valid with
767 default definitions of PARM_DECLs. */
768 if (POINTER_TYPE_P (TREE_TYPE (sym
))
769 && nonnull_arg_p (sym
))
770 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
772 set_value_range_to_varying (vr
);
774 else if (TREE_CODE (sym
) == RESULT_DECL
775 && DECL_BY_REFERENCE (sym
))
776 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
782 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
785 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
789 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
791 return is_overflow_infinity (val1
) == is_overflow_infinity (val2
);
794 /* Return true, if the bitmaps B1 and B2 are equal. */
797 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
800 || ((!b1
|| bitmap_empty_p (b1
))
801 && (!b2
|| bitmap_empty_p (b2
)))
803 && bitmap_equal_p (b1
, b2
)));
806 /* Update the value range and equivalence set for variable VAR to
807 NEW_VR. Return true if NEW_VR is different from VAR's previous
810 NOTE: This function assumes that NEW_VR is a temporary value range
811 object created for the sole purpose of updating VAR's range. The
812 storage used by the equivalence set from NEW_VR will be freed by
813 this function. Do not call update_value_range when NEW_VR
814 is the range object associated with another SSA name. */
817 update_value_range (const_tree var
, value_range_t
*new_vr
)
819 value_range_t
*old_vr
;
822 /* Update the value range, if necessary. */
823 old_vr
= get_value_range (var
);
824 is_new
= old_vr
->type
!= new_vr
->type
825 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
826 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
827 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
831 /* Do not allow transitions up the lattice. The following
832 is slightly more awkward than just new_vr->type < old_vr->type
833 because VR_RANGE and VR_ANTI_RANGE need to be considered
834 the same. We may not have is_new when transitioning to
835 UNDEFINED or from VARYING. */
836 if (new_vr
->type
== VR_UNDEFINED
837 || old_vr
->type
== VR_VARYING
)
838 set_value_range_to_varying (old_vr
);
840 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
844 BITMAP_FREE (new_vr
->equiv
);
850 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
851 point where equivalence processing can be turned on/off. */
854 add_equivalence (bitmap
*equiv
, const_tree var
)
856 unsigned ver
= SSA_NAME_VERSION (var
);
857 value_range_t
*vr
= vr_value
[ver
];
860 *equiv
= BITMAP_ALLOC (NULL
);
861 bitmap_set_bit (*equiv
, ver
);
863 bitmap_ior_into (*equiv
, vr
->equiv
);
867 /* Return true if VR is ~[0, 0]. */
870 range_is_nonnull (value_range_t
*vr
)
872 return vr
->type
== VR_ANTI_RANGE
873 && integer_zerop (vr
->min
)
874 && integer_zerop (vr
->max
);
878 /* Return true if VR is [0, 0]. */
881 range_is_null (value_range_t
*vr
)
883 return vr
->type
== VR_RANGE
884 && integer_zerop (vr
->min
)
885 && integer_zerop (vr
->max
);
888 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
892 range_int_cst_p (value_range_t
*vr
)
894 return (vr
->type
== VR_RANGE
895 && TREE_CODE (vr
->max
) == INTEGER_CST
896 && TREE_CODE (vr
->min
) == INTEGER_CST
);
899 /* Return true if VR is a INTEGER_CST singleton. */
902 range_int_cst_singleton_p (value_range_t
*vr
)
904 return (range_int_cst_p (vr
)
905 && !is_overflow_infinity (vr
->min
)
906 && !is_overflow_infinity (vr
->max
)
907 && tree_int_cst_equal (vr
->min
, vr
->max
));
910 /* Return true if value range VR involves at least one symbol. */
913 symbolic_range_p (value_range_t
*vr
)
915 return (!is_gimple_min_invariant (vr
->min
)
916 || !is_gimple_min_invariant (vr
->max
));
919 /* Return true if value range VR uses an overflow infinity. */
922 overflow_infinity_range_p (value_range_t
*vr
)
924 return (vr
->type
== VR_RANGE
925 && (is_overflow_infinity (vr
->min
)
926 || is_overflow_infinity (vr
->max
)));
929 /* Return false if we can not make a valid comparison based on VR;
930 this will be the case if it uses an overflow infinity and overflow
931 is not undefined (i.e., -fno-strict-overflow is in effect).
932 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
933 uses an overflow infinity. */
936 usable_range_p (value_range_t
*vr
, bool *strict_overflow_p
)
938 gcc_assert (vr
->type
== VR_RANGE
);
939 if (is_overflow_infinity (vr
->min
))
941 *strict_overflow_p
= true;
942 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
945 if (is_overflow_infinity (vr
->max
))
947 *strict_overflow_p
= true;
948 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
955 /* Return true if the result of assignment STMT is know to be non-negative.
956 If the return value is based on the assumption that signed overflow is
957 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
958 *STRICT_OVERFLOW_P.*/
961 gimple_assign_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
963 enum tree_code code
= gimple_assign_rhs_code (stmt
);
964 switch (get_gimple_rhs_class (code
))
966 case GIMPLE_UNARY_RHS
:
967 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
968 gimple_expr_type (stmt
),
969 gimple_assign_rhs1 (stmt
),
971 case GIMPLE_BINARY_RHS
:
972 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
973 gimple_expr_type (stmt
),
974 gimple_assign_rhs1 (stmt
),
975 gimple_assign_rhs2 (stmt
),
977 case GIMPLE_TERNARY_RHS
:
979 case GIMPLE_SINGLE_RHS
:
980 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
982 case GIMPLE_INVALID_RHS
:
989 /* Return true if return value of call STMT is know to be non-negative.
990 If the return value is based on the assumption that signed overflow is
991 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
992 *STRICT_OVERFLOW_P.*/
995 gimple_call_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
997 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
998 gimple_call_arg (stmt
, 0) : NULL_TREE
;
999 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
1000 gimple_call_arg (stmt
, 1) : NULL_TREE
;
1002 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
1003 gimple_call_fndecl (stmt
),
1009 /* Return true if STMT is know to to compute a non-negative value.
1010 If the return value is based on the assumption that signed overflow is
1011 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1012 *STRICT_OVERFLOW_P.*/
1015 gimple_stmt_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1017 switch (gimple_code (stmt
))
1020 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1022 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1028 /* Return true if the result of assignment STMT is know to be non-zero.
1029 If the return value is based on the assumption that signed overflow is
1030 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1031 *STRICT_OVERFLOW_P.*/
1034 gimple_assign_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1036 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1037 switch (get_gimple_rhs_class (code
))
1039 case GIMPLE_UNARY_RHS
:
1040 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1041 gimple_expr_type (stmt
),
1042 gimple_assign_rhs1 (stmt
),
1044 case GIMPLE_BINARY_RHS
:
1045 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1046 gimple_expr_type (stmt
),
1047 gimple_assign_rhs1 (stmt
),
1048 gimple_assign_rhs2 (stmt
),
1050 case GIMPLE_TERNARY_RHS
:
1052 case GIMPLE_SINGLE_RHS
:
1053 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
1055 case GIMPLE_INVALID_RHS
:
1062 /* Return true if STMT is known to compute a non-zero value.
1063 If the return value is based on the assumption that signed overflow is
1064 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1065 *STRICT_OVERFLOW_P.*/
1068 gimple_stmt_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1070 switch (gimple_code (stmt
))
1073 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
1076 tree fndecl
= gimple_call_fndecl (stmt
);
1077 if (!fndecl
) return false;
1078 if (flag_delete_null_pointer_checks
&& !flag_check_new
1079 && DECL_IS_OPERATOR_NEW (fndecl
)
1080 && !TREE_NOTHROW (fndecl
))
1082 if (flag_delete_null_pointer_checks
&&
1083 lookup_attribute ("returns_nonnull",
1084 TYPE_ATTRIBUTES (gimple_call_fntype (stmt
))))
1086 return gimple_alloca_call_p (stmt
);
1093 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1097 vrp_stmt_computes_nonzero (gimple stmt
, bool *strict_overflow_p
)
1099 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
1102 /* If we have an expression of the form &X->a, then the expression
1103 is nonnull if X is nonnull. */
1104 if (is_gimple_assign (stmt
)
1105 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1107 tree expr
= gimple_assign_rhs1 (stmt
);
1108 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
1110 if (base
!= NULL_TREE
1111 && TREE_CODE (base
) == MEM_REF
1112 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1114 value_range_t
*vr
= get_value_range (TREE_OPERAND (base
, 0));
1115 if (range_is_nonnull (vr
))
1123 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1124 a gimple invariant, or SSA_NAME +- CST. */
1127 valid_value_p (tree expr
)
1129 if (TREE_CODE (expr
) == SSA_NAME
)
1132 if (TREE_CODE (expr
) == PLUS_EXPR
1133 || TREE_CODE (expr
) == MINUS_EXPR
)
1134 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
1135 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
1137 return is_gimple_min_invariant (expr
);
1143 -2 if those are incomparable. */
1145 operand_less_p (tree val
, tree val2
)
1147 /* LT is folded faster than GE and others. Inline the common case. */
1148 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
1149 return tree_int_cst_lt (val
, val2
);
1154 fold_defer_overflow_warnings ();
1156 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1158 fold_undefer_and_ignore_overflow_warnings ();
1161 || TREE_CODE (tcmp
) != INTEGER_CST
)
1164 if (!integer_zerop (tcmp
))
1168 /* val >= val2, not considering overflow infinity. */
1169 if (is_negative_overflow_infinity (val
))
1170 return is_negative_overflow_infinity (val2
) ? 0 : 1;
1171 else if (is_positive_overflow_infinity (val2
))
1172 return is_positive_overflow_infinity (val
) ? 0 : 1;
1177 /* Compare two values VAL1 and VAL2. Return
1179 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1182 +1 if VAL1 > VAL2, and
1185 This is similar to tree_int_cst_compare but supports pointer values
1186 and values that cannot be compared at compile time.
1188 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1189 true if the return value is only valid if we assume that signed
1190 overflow is undefined. */
1193 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1198 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1200 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1201 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1202 /* Convert the two values into the same type. This is needed because
1203 sizetype causes sign extension even for unsigned types. */
1204 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1205 STRIP_USELESS_TYPE_CONVERSION (val2
);
1207 if ((TREE_CODE (val1
) == SSA_NAME
1208 || TREE_CODE (val1
) == PLUS_EXPR
1209 || TREE_CODE (val1
) == MINUS_EXPR
)
1210 && (TREE_CODE (val2
) == SSA_NAME
1211 || TREE_CODE (val2
) == PLUS_EXPR
1212 || TREE_CODE (val2
) == MINUS_EXPR
))
1214 tree n1
, c1
, n2
, c2
;
1215 enum tree_code code1
, code2
;
1217 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
1218 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1219 same name, return -2. */
1220 if (TREE_CODE (val1
) == SSA_NAME
)
1228 code1
= TREE_CODE (val1
);
1229 n1
= TREE_OPERAND (val1
, 0);
1230 c1
= TREE_OPERAND (val1
, 1);
1231 if (tree_int_cst_sgn (c1
) == -1)
1233 if (is_negative_overflow_infinity (c1
))
1235 c1
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c1
), c1
);
1238 code1
= code1
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1242 if (TREE_CODE (val2
) == SSA_NAME
)
1250 code2
= TREE_CODE (val2
);
1251 n2
= TREE_OPERAND (val2
, 0);
1252 c2
= TREE_OPERAND (val2
, 1);
1253 if (tree_int_cst_sgn (c2
) == -1)
1255 if (is_negative_overflow_infinity (c2
))
1257 c2
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c2
), c2
);
1260 code2
= code2
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1264 /* Both values must use the same name. */
1268 if (code1
== SSA_NAME
1269 && code2
== SSA_NAME
)
1273 /* If overflow is defined we cannot simplify more. */
1274 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1277 if (strict_overflow_p
!= NULL
1278 && (code1
== SSA_NAME
|| !TREE_NO_WARNING (val1
))
1279 && (code2
== SSA_NAME
|| !TREE_NO_WARNING (val2
)))
1280 *strict_overflow_p
= true;
1282 if (code1
== SSA_NAME
)
1284 if (code2
== PLUS_EXPR
)
1285 /* NAME < NAME + CST */
1287 else if (code2
== MINUS_EXPR
)
1288 /* NAME > NAME - CST */
1291 else if (code1
== PLUS_EXPR
)
1293 if (code2
== SSA_NAME
)
1294 /* NAME + CST > NAME */
1296 else if (code2
== PLUS_EXPR
)
1297 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1298 return compare_values_warnv (c1
, c2
, strict_overflow_p
);
1299 else if (code2
== MINUS_EXPR
)
1300 /* NAME + CST1 > NAME - CST2 */
1303 else if (code1
== MINUS_EXPR
)
1305 if (code2
== SSA_NAME
)
1306 /* NAME - CST < NAME */
1308 else if (code2
== PLUS_EXPR
)
1309 /* NAME - CST1 < NAME + CST2 */
1311 else if (code2
== MINUS_EXPR
)
1312 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1313 C1 and C2 are swapped in the call to compare_values. */
1314 return compare_values_warnv (c2
, c1
, strict_overflow_p
);
1320 /* We cannot compare non-constants. */
1321 if (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
))
1324 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1326 /* We cannot compare overflowed values, except for overflow
1328 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1330 if (strict_overflow_p
!= NULL
)
1331 *strict_overflow_p
= true;
1332 if (is_negative_overflow_infinity (val1
))
1333 return is_negative_overflow_infinity (val2
) ? 0 : -1;
1334 else if (is_negative_overflow_infinity (val2
))
1336 else if (is_positive_overflow_infinity (val1
))
1337 return is_positive_overflow_infinity (val2
) ? 0 : 1;
1338 else if (is_positive_overflow_infinity (val2
))
1343 return tree_int_cst_compare (val1
, val2
);
1349 /* First see if VAL1 and VAL2 are not the same. */
1350 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1353 /* If VAL1 is a lower address than VAL2, return -1. */
1354 if (operand_less_p (val1
, val2
) == 1)
1357 /* If VAL1 is a higher address than VAL2, return +1. */
1358 if (operand_less_p (val2
, val1
) == 1)
1361 /* If VAL1 is different than VAL2, return +2.
1362 For integer constants we either have already returned -1 or 1
1363 or they are equivalent. We still might succeed in proving
1364 something about non-trivial operands. */
1365 if (TREE_CODE (val1
) != INTEGER_CST
1366 || TREE_CODE (val2
) != INTEGER_CST
)
1368 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1369 if (t
&& integer_onep (t
))
1377 /* Compare values like compare_values_warnv, but treat comparisons of
1378 nonconstants which rely on undefined overflow as incomparable. */
1381 compare_values (tree val1
, tree val2
)
1387 ret
= compare_values_warnv (val1
, val2
, &sop
);
1389 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1395 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1396 0 if VAL is not inside [MIN, MAX],
1397 -2 if we cannot tell either way.
1399 Benchmark compile/20001226-1.c compilation time after changing this
1403 value_inside_range (tree val
, tree min
, tree max
)
1407 cmp1
= operand_less_p (val
, min
);
1413 cmp2
= operand_less_p (max
, val
);
1421 /* Return true if value ranges VR0 and VR1 have a non-empty
1424 Benchmark compile/20001226-1.c compilation time after changing this
1429 value_ranges_intersect_p (value_range_t
*vr0
, value_range_t
*vr1
)
1431 /* The value ranges do not intersect if the maximum of the first range is
1432 less than the minimum of the second range or vice versa.
1433 When those relations are unknown, we can't do any better. */
1434 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1436 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1442 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1443 include the value zero, -2 if we cannot tell. */
1446 range_includes_zero_p (tree min
, tree max
)
1448 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1449 return value_inside_range (zero
, min
, max
);
1452 /* Return true if *VR is know to only contain nonnegative values. */
1455 value_range_nonnegative_p (value_range_t
*vr
)
1457 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1458 which would return a useful value should be encoded as a
1460 if (vr
->type
== VR_RANGE
)
1462 int result
= compare_values (vr
->min
, integer_zero_node
);
1463 return (result
== 0 || result
== 1);
1469 /* If *VR has a value rante that is a single constant value return that,
1470 otherwise return NULL_TREE. */
1473 value_range_constant_singleton (value_range_t
*vr
)
1475 if (vr
->type
== VR_RANGE
1476 && operand_equal_p (vr
->min
, vr
->max
, 0)
1477 && is_gimple_min_invariant (vr
->min
))
1483 /* If OP has a value range with a single constant value return that,
1484 otherwise return NULL_TREE. This returns OP itself if OP is a
1488 op_with_constant_singleton_value_range (tree op
)
1490 if (is_gimple_min_invariant (op
))
1493 if (TREE_CODE (op
) != SSA_NAME
)
1496 return value_range_constant_singleton (get_value_range (op
));
1499 /* Return true if op is in a boolean [0, 1] value-range. */
1502 op_with_boolean_value_range_p (tree op
)
1506 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1509 if (integer_zerop (op
)
1510 || integer_onep (op
))
1513 if (TREE_CODE (op
) != SSA_NAME
)
1516 vr
= get_value_range (op
);
1517 return (vr
->type
== VR_RANGE
1518 && integer_zerop (vr
->min
)
1519 && integer_onep (vr
->max
));
1522 /* Extract value range information from an ASSERT_EXPR EXPR and store
1526 extract_range_from_assert (value_range_t
*vr_p
, tree expr
)
1528 tree var
, cond
, limit
, min
, max
, type
;
1529 value_range_t
*limit_vr
;
1530 enum tree_code cond_code
;
1532 var
= ASSERT_EXPR_VAR (expr
);
1533 cond
= ASSERT_EXPR_COND (expr
);
1535 gcc_assert (COMPARISON_CLASS_P (cond
));
1537 /* Find VAR in the ASSERT_EXPR conditional. */
1538 if (var
== TREE_OPERAND (cond
, 0)
1539 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1540 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1542 /* If the predicate is of the form VAR COMP LIMIT, then we just
1543 take LIMIT from the RHS and use the same comparison code. */
1544 cond_code
= TREE_CODE (cond
);
1545 limit
= TREE_OPERAND (cond
, 1);
1546 cond
= TREE_OPERAND (cond
, 0);
1550 /* If the predicate is of the form LIMIT COMP VAR, then we need
1551 to flip around the comparison code to create the proper range
1553 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1554 limit
= TREE_OPERAND (cond
, 0);
1555 cond
= TREE_OPERAND (cond
, 1);
1558 limit
= avoid_overflow_infinity (limit
);
1560 type
= TREE_TYPE (var
);
1561 gcc_assert (limit
!= var
);
1563 /* For pointer arithmetic, we only keep track of pointer equality
1565 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1567 set_value_range_to_varying (vr_p
);
1571 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1572 try to use LIMIT's range to avoid creating symbolic ranges
1574 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1576 /* LIMIT's range is only interesting if it has any useful information. */
1578 && (limit_vr
->type
== VR_UNDEFINED
1579 || limit_vr
->type
== VR_VARYING
1580 || symbolic_range_p (limit_vr
)))
1583 /* Initially, the new range has the same set of equivalences of
1584 VAR's range. This will be revised before returning the final
1585 value. Since assertions may be chained via mutually exclusive
1586 predicates, we will need to trim the set of equivalences before
1588 gcc_assert (vr_p
->equiv
== NULL
);
1589 add_equivalence (&vr_p
->equiv
, var
);
1591 /* Extract a new range based on the asserted comparison for VAR and
1592 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1593 will only use it for equality comparisons (EQ_EXPR). For any
1594 other kind of assertion, we cannot derive a range from LIMIT's
1595 anti-range that can be used to describe the new range. For
1596 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1597 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1598 no single range for x_2 that could describe LE_EXPR, so we might
1599 as well build the range [b_4, +INF] for it.
1600 One special case we handle is extracting a range from a
1601 range test encoded as (unsigned)var + CST <= limit. */
1602 if (TREE_CODE (cond
) == NOP_EXPR
1603 || TREE_CODE (cond
) == PLUS_EXPR
)
1605 if (TREE_CODE (cond
) == PLUS_EXPR
)
1607 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (cond
, 1)),
1608 TREE_OPERAND (cond
, 1));
1609 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1610 cond
= TREE_OPERAND (cond
, 0);
1614 min
= build_int_cst (TREE_TYPE (var
), 0);
1618 /* Make sure to not set TREE_OVERFLOW on the final type
1619 conversion. We are willingly interpreting large positive
1620 unsigned values as negative singed values here. */
1621 min
= force_fit_type (TREE_TYPE (var
), wi::to_widest (min
), 0, false);
1622 max
= force_fit_type (TREE_TYPE (var
), wi::to_widest (max
), 0, false);
1624 /* We can transform a max, min range to an anti-range or
1625 vice-versa. Use set_and_canonicalize_value_range which does
1627 if (cond_code
== LE_EXPR
)
1628 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1629 min
, max
, vr_p
->equiv
);
1630 else if (cond_code
== GT_EXPR
)
1631 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1632 min
, max
, vr_p
->equiv
);
1636 else if (cond_code
== EQ_EXPR
)
1638 enum value_range_type range_type
;
1642 range_type
= limit_vr
->type
;
1643 min
= limit_vr
->min
;
1644 max
= limit_vr
->max
;
1648 range_type
= VR_RANGE
;
1653 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1655 /* When asserting the equality VAR == LIMIT and LIMIT is another
1656 SSA name, the new range will also inherit the equivalence set
1658 if (TREE_CODE (limit
) == SSA_NAME
)
1659 add_equivalence (&vr_p
->equiv
, limit
);
1661 else if (cond_code
== NE_EXPR
)
1663 /* As described above, when LIMIT's range is an anti-range and
1664 this assertion is an inequality (NE_EXPR), then we cannot
1665 derive anything from the anti-range. For instance, if
1666 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1667 not imply that VAR's range is [0, 0]. So, in the case of
1668 anti-ranges, we just assert the inequality using LIMIT and
1671 If LIMIT_VR is a range, we can only use it to build a new
1672 anti-range if LIMIT_VR is a single-valued range. For
1673 instance, if LIMIT_VR is [0, 1], the predicate
1674 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1675 Rather, it means that for value 0 VAR should be ~[0, 0]
1676 and for value 1, VAR should be ~[1, 1]. We cannot
1677 represent these ranges.
1679 The only situation in which we can build a valid
1680 anti-range is when LIMIT_VR is a single-valued range
1681 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1682 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1684 && limit_vr
->type
== VR_RANGE
1685 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1687 min
= limit_vr
->min
;
1688 max
= limit_vr
->max
;
1692 /* In any other case, we cannot use LIMIT's range to build a
1693 valid anti-range. */
1697 /* If MIN and MAX cover the whole range for their type, then
1698 just use the original LIMIT. */
1699 if (INTEGRAL_TYPE_P (type
)
1700 && vrp_val_is_min (min
)
1701 && vrp_val_is_max (max
))
1704 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1705 min
, max
, vr_p
->equiv
);
1707 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1709 min
= TYPE_MIN_VALUE (type
);
1711 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1715 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1716 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1718 max
= limit_vr
->max
;
1721 /* If the maximum value forces us to be out of bounds, simply punt.
1722 It would be pointless to try and do anything more since this
1723 all should be optimized away above us. */
1724 if ((cond_code
== LT_EXPR
1725 && compare_values (max
, min
) == 0)
1726 || is_overflow_infinity (max
))
1727 set_value_range_to_varying (vr_p
);
1730 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1731 if (cond_code
== LT_EXPR
)
1733 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1734 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1735 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1736 build_int_cst (TREE_TYPE (max
), -1));
1738 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1739 build_int_cst (TREE_TYPE (max
), 1));
1741 TREE_NO_WARNING (max
) = 1;
1744 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1747 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1749 max
= TYPE_MAX_VALUE (type
);
1751 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1755 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1756 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1758 min
= limit_vr
->min
;
1761 /* If the minimum value forces us to be out of bounds, simply punt.
1762 It would be pointless to try and do anything more since this
1763 all should be optimized away above us. */
1764 if ((cond_code
== GT_EXPR
1765 && compare_values (min
, max
) == 0)
1766 || is_overflow_infinity (min
))
1767 set_value_range_to_varying (vr_p
);
1770 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1771 if (cond_code
== GT_EXPR
)
1773 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1774 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1775 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1776 build_int_cst (TREE_TYPE (min
), -1));
1778 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1779 build_int_cst (TREE_TYPE (min
), 1));
1781 TREE_NO_WARNING (min
) = 1;
1784 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1790 /* Finally intersect the new range with what we already know about var. */
1791 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1795 /* Extract range information from SSA name VAR and store it in VR. If
1796 VAR has an interesting range, use it. Otherwise, create the
1797 range [VAR, VAR] and return it. This is useful in situations where
1798 we may have conditionals testing values of VARYING names. For
1805 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1809 extract_range_from_ssa_name (value_range_t
*vr
, tree var
)
1811 value_range_t
*var_vr
= get_value_range (var
);
1813 if (var_vr
->type
!= VR_VARYING
)
1814 copy_value_range (vr
, var_vr
);
1816 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1818 add_equivalence (&vr
->equiv
, var
);
1822 /* Wrapper around int_const_binop. If the operation overflows and we
1823 are not using wrapping arithmetic, then adjust the result to be
1824 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1825 NULL_TREE if we need to use an overflow infinity representation but
1826 the type does not support it. */
1829 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1833 res
= int_const_binop (code
, val1
, val2
);
1835 /* If we are using unsigned arithmetic, operate symbolically
1836 on -INF and +INF as int_const_binop only handles signed overflow. */
1837 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1839 int checkz
= compare_values (res
, val1
);
1840 bool overflow
= false;
1842 /* Ensure that res = val1 [+*] val2 >= val1
1843 or that res = val1 - val2 <= val1. */
1844 if ((code
== PLUS_EXPR
1845 && !(checkz
== 1 || checkz
== 0))
1846 || (code
== MINUS_EXPR
1847 && !(checkz
== 0 || checkz
== -1)))
1851 /* Checking for multiplication overflow is done by dividing the
1852 output of the multiplication by the first input of the
1853 multiplication. If the result of that division operation is
1854 not equal to the second input of the multiplication, then the
1855 multiplication overflowed. */
1856 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1858 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1861 int check
= compare_values (tmp
, val2
);
1869 res
= copy_node (res
);
1870 TREE_OVERFLOW (res
) = 1;
1874 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1875 /* If the singed operation wraps then int_const_binop has done
1876 everything we want. */
1878 /* Signed division of -1/0 overflows and by the time it gets here
1879 returns NULL_TREE. */
1882 else if ((TREE_OVERFLOW (res
)
1883 && !TREE_OVERFLOW (val1
)
1884 && !TREE_OVERFLOW (val2
))
1885 || is_overflow_infinity (val1
)
1886 || is_overflow_infinity (val2
))
1888 /* If the operation overflowed but neither VAL1 nor VAL2 are
1889 overflown, return -INF or +INF depending on the operation
1890 and the combination of signs of the operands. */
1891 int sgn1
= tree_int_cst_sgn (val1
);
1892 int sgn2
= tree_int_cst_sgn (val2
);
1894 if (needs_overflow_infinity (TREE_TYPE (res
))
1895 && !supports_overflow_infinity (TREE_TYPE (res
)))
1898 /* We have to punt on adding infinities of different signs,
1899 since we can't tell what the sign of the result should be.
1900 Likewise for subtracting infinities of the same sign. */
1901 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
1902 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
1903 && is_overflow_infinity (val1
)
1904 && is_overflow_infinity (val2
))
1907 /* Don't try to handle division or shifting of infinities. */
1908 if ((code
== TRUNC_DIV_EXPR
1909 || code
== FLOOR_DIV_EXPR
1910 || code
== CEIL_DIV_EXPR
1911 || code
== EXACT_DIV_EXPR
1912 || code
== ROUND_DIV_EXPR
1913 || code
== RSHIFT_EXPR
)
1914 && (is_overflow_infinity (val1
)
1915 || is_overflow_infinity (val2
)))
1918 /* Notice that we only need to handle the restricted set of
1919 operations handled by extract_range_from_binary_expr.
1920 Among them, only multiplication, addition and subtraction
1921 can yield overflow without overflown operands because we
1922 are working with integral types only... except in the
1923 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1924 for division too. */
1926 /* For multiplication, the sign of the overflow is given
1927 by the comparison of the signs of the operands. */
1928 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
1929 /* For addition, the operands must be of the same sign
1930 to yield an overflow. Its sign is therefore that
1931 of one of the operands, for example the first. For
1932 infinite operands X + -INF is negative, not positive. */
1933 || (code
== PLUS_EXPR
1935 ? !is_negative_overflow_infinity (val2
)
1936 : is_positive_overflow_infinity (val2
)))
1937 /* For subtraction, non-infinite operands must be of
1938 different signs to yield an overflow. Its sign is
1939 therefore that of the first operand or the opposite of
1940 that of the second operand. A first operand of 0 counts
1941 as positive here, for the corner case 0 - (-INF), which
1942 overflows, but must yield +INF. For infinite operands 0
1943 - INF is negative, not positive. */
1944 || (code
== MINUS_EXPR
1946 ? !is_positive_overflow_infinity (val2
)
1947 : is_negative_overflow_infinity (val2
)))
1948 /* We only get in here with positive shift count, so the
1949 overflow direction is the same as the sign of val1.
1950 Actually rshift does not overflow at all, but we only
1951 handle the case of shifting overflowed -INF and +INF. */
1952 || (code
== RSHIFT_EXPR
1954 /* For division, the only case is -INF / -1 = +INF. */
1955 || code
== TRUNC_DIV_EXPR
1956 || code
== FLOOR_DIV_EXPR
1957 || code
== CEIL_DIV_EXPR
1958 || code
== EXACT_DIV_EXPR
1959 || code
== ROUND_DIV_EXPR
)
1960 return (needs_overflow_infinity (TREE_TYPE (res
))
1961 ? positive_overflow_infinity (TREE_TYPE (res
))
1962 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
1964 return (needs_overflow_infinity (TREE_TYPE (res
))
1965 ? negative_overflow_infinity (TREE_TYPE (res
))
1966 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
1973 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1974 bitmask if some bit is unset, it means for all numbers in the range
1975 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1976 bitmask if some bit is set, it means for all numbers in the range
1977 the bit is 1, otherwise it might be 0 or 1. */
1980 zero_nonzero_bits_from_vr (const tree expr_type
,
1982 wide_int
*may_be_nonzero
,
1983 wide_int
*must_be_nonzero
)
1985 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
1986 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
1987 if (!range_int_cst_p (vr
)
1988 || is_overflow_infinity (vr
->min
)
1989 || is_overflow_infinity (vr
->max
))
1992 if (range_int_cst_singleton_p (vr
))
1994 *may_be_nonzero
= vr
->min
;
1995 *must_be_nonzero
= *may_be_nonzero
;
1997 else if (tree_int_cst_sgn (vr
->min
) >= 0
1998 || tree_int_cst_sgn (vr
->max
) < 0)
2000 wide_int xor_mask
= wi::bit_xor (vr
->min
, vr
->max
);
2001 *may_be_nonzero
= wi::bit_or (vr
->min
, vr
->max
);
2002 *must_be_nonzero
= wi::bit_and (vr
->min
, vr
->max
);
2005 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
2006 may_be_nonzero
->get_precision ());
2007 *may_be_nonzero
= *may_be_nonzero
| mask
;
2008 *must_be_nonzero
= must_be_nonzero
->and_not (mask
);
2015 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2016 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2017 false otherwise. If *AR can be represented with a single range
2018 *VR1 will be VR_UNDEFINED. */
2021 ranges_from_anti_range (value_range_t
*ar
,
2022 value_range_t
*vr0
, value_range_t
*vr1
)
2024 tree type
= TREE_TYPE (ar
->min
);
2026 vr0
->type
= VR_UNDEFINED
;
2027 vr1
->type
= VR_UNDEFINED
;
2029 if (ar
->type
!= VR_ANTI_RANGE
2030 || TREE_CODE (ar
->min
) != INTEGER_CST
2031 || TREE_CODE (ar
->max
) != INTEGER_CST
2032 || !vrp_val_min (type
)
2033 || !vrp_val_max (type
))
2036 if (!vrp_val_is_min (ar
->min
))
2038 vr0
->type
= VR_RANGE
;
2039 vr0
->min
= vrp_val_min (type
);
2040 vr0
->max
= wide_int_to_tree (type
, wi::sub (ar
->min
, 1));
2042 if (!vrp_val_is_max (ar
->max
))
2044 vr1
->type
= VR_RANGE
;
2045 vr1
->min
= wide_int_to_tree (type
, wi::add (ar
->max
, 1));
2046 vr1
->max
= vrp_val_max (type
);
2048 if (vr0
->type
== VR_UNDEFINED
)
2051 vr1
->type
= VR_UNDEFINED
;
2054 return vr0
->type
!= VR_UNDEFINED
;
2057 /* Helper to extract a value-range *VR for a multiplicative operation
2061 extract_range_from_multiplicative_op_1 (value_range_t
*vr
,
2062 enum tree_code code
,
2063 value_range_t
*vr0
, value_range_t
*vr1
)
2065 enum value_range_type type
;
2072 /* Multiplications, divisions and shifts are a bit tricky to handle,
2073 depending on the mix of signs we have in the two ranges, we
2074 need to operate on different values to get the minimum and
2075 maximum values for the new range. One approach is to figure
2076 out all the variations of range combinations and do the
2079 However, this involves several calls to compare_values and it
2080 is pretty convoluted. It's simpler to do the 4 operations
2081 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2082 MAX1) and then figure the smallest and largest values to form
2084 gcc_assert (code
== MULT_EXPR
2085 || code
== TRUNC_DIV_EXPR
2086 || code
== FLOOR_DIV_EXPR
2087 || code
== CEIL_DIV_EXPR
2088 || code
== EXACT_DIV_EXPR
2089 || code
== ROUND_DIV_EXPR
2090 || code
== RSHIFT_EXPR
2091 || code
== LSHIFT_EXPR
);
2092 gcc_assert ((vr0
->type
== VR_RANGE
2093 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
2094 && vr0
->type
== vr1
->type
);
2098 /* Compute the 4 cross operations. */
2100 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
2101 if (val
[0] == NULL_TREE
)
2104 if (vr1
->max
== vr1
->min
)
2108 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
2109 if (val
[1] == NULL_TREE
)
2113 if (vr0
->max
== vr0
->min
)
2117 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
2118 if (val
[2] == NULL_TREE
)
2122 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
2126 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
2127 if (val
[3] == NULL_TREE
)
2133 set_value_range_to_varying (vr
);
2137 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2141 for (i
= 1; i
< 4; i
++)
2143 if (!is_gimple_min_invariant (min
)
2144 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2145 || !is_gimple_min_invariant (max
)
2146 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2151 if (!is_gimple_min_invariant (val
[i
])
2152 || (TREE_OVERFLOW (val
[i
])
2153 && !is_overflow_infinity (val
[i
])))
2155 /* If we found an overflowed value, set MIN and MAX
2156 to it so that we set the resulting range to
2162 if (compare_values (val
[i
], min
) == -1)
2165 if (compare_values (val
[i
], max
) == 1)
2170 /* If either MIN or MAX overflowed, then set the resulting range to
2171 VARYING. But we do accept an overflow infinity
2173 if (min
== NULL_TREE
2174 || !is_gimple_min_invariant (min
)
2175 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2177 || !is_gimple_min_invariant (max
)
2178 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2180 set_value_range_to_varying (vr
);
2186 2) [-INF, +-INF(OVF)]
2187 3) [+-INF(OVF), +INF]
2188 4) [+-INF(OVF), +-INF(OVF)]
2189 We learn nothing when we have INF and INF(OVF) on both sides.
2190 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2192 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2193 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2195 set_value_range_to_varying (vr
);
2199 cmp
= compare_values (min
, max
);
2200 if (cmp
== -2 || cmp
== 1)
2202 /* If the new range has its limits swapped around (MIN > MAX),
2203 then the operation caused one of them to wrap around, mark
2204 the new range VARYING. */
2205 set_value_range_to_varying (vr
);
2208 set_value_range (vr
, type
, min
, max
, NULL
);
2211 /* Extract range information from a binary operation CODE based on
2212 the ranges of each of its operands, *VR0 and *VR1 with resulting
2213 type EXPR_TYPE. The resulting range is stored in *VR. */
2216 extract_range_from_binary_expr_1 (value_range_t
*vr
,
2217 enum tree_code code
, tree expr_type
,
2218 value_range_t
*vr0_
, value_range_t
*vr1_
)
2220 value_range_t vr0
= *vr0_
, vr1
= *vr1_
;
2221 value_range_t vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2222 enum value_range_type type
;
2223 tree min
= NULL_TREE
, max
= NULL_TREE
;
2226 if (!INTEGRAL_TYPE_P (expr_type
)
2227 && !POINTER_TYPE_P (expr_type
))
2229 set_value_range_to_varying (vr
);
2233 /* Not all binary expressions can be applied to ranges in a
2234 meaningful way. Handle only arithmetic operations. */
2235 if (code
!= PLUS_EXPR
2236 && code
!= MINUS_EXPR
2237 && code
!= POINTER_PLUS_EXPR
2238 && code
!= MULT_EXPR
2239 && code
!= TRUNC_DIV_EXPR
2240 && code
!= FLOOR_DIV_EXPR
2241 && code
!= CEIL_DIV_EXPR
2242 && code
!= EXACT_DIV_EXPR
2243 && code
!= ROUND_DIV_EXPR
2244 && code
!= TRUNC_MOD_EXPR
2245 && code
!= RSHIFT_EXPR
2246 && code
!= LSHIFT_EXPR
2249 && code
!= BIT_AND_EXPR
2250 && code
!= BIT_IOR_EXPR
2251 && code
!= BIT_XOR_EXPR
)
2253 set_value_range_to_varying (vr
);
2257 /* If both ranges are UNDEFINED, so is the result. */
2258 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2260 set_value_range_to_undefined (vr
);
2263 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2264 code. At some point we may want to special-case operations that
2265 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2267 else if (vr0
.type
== VR_UNDEFINED
)
2268 set_value_range_to_varying (&vr0
);
2269 else if (vr1
.type
== VR_UNDEFINED
)
2270 set_value_range_to_varying (&vr1
);
2272 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2273 and express ~[] op X as ([]' op X) U ([]'' op X). */
2274 if (vr0
.type
== VR_ANTI_RANGE
2275 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2277 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2278 if (vrtem1
.type
!= VR_UNDEFINED
)
2280 value_range_t vrres
= VR_INITIALIZER
;
2281 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2283 vrp_meet (vr
, &vrres
);
2287 /* Likewise for X op ~[]. */
2288 if (vr1
.type
== VR_ANTI_RANGE
2289 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2291 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2292 if (vrtem1
.type
!= VR_UNDEFINED
)
2294 value_range_t vrres
= VR_INITIALIZER
;
2295 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2297 vrp_meet (vr
, &vrres
);
2302 /* The type of the resulting value range defaults to VR0.TYPE. */
2305 /* Refuse to operate on VARYING ranges, ranges of different kinds
2306 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
2307 because we may be able to derive a useful range even if one of
2308 the operands is VR_VARYING or symbolic range. Similarly for
2309 divisions. TODO, we may be able to derive anti-ranges in
2311 if (code
!= BIT_AND_EXPR
2312 && code
!= BIT_IOR_EXPR
2313 && code
!= TRUNC_DIV_EXPR
2314 && code
!= FLOOR_DIV_EXPR
2315 && code
!= CEIL_DIV_EXPR
2316 && code
!= EXACT_DIV_EXPR
2317 && code
!= ROUND_DIV_EXPR
2318 && code
!= TRUNC_MOD_EXPR
2321 && (vr0
.type
== VR_VARYING
2322 || vr1
.type
== VR_VARYING
2323 || vr0
.type
!= vr1
.type
2324 || symbolic_range_p (&vr0
)
2325 || symbolic_range_p (&vr1
)))
2327 set_value_range_to_varying (vr
);
2331 /* Now evaluate the expression to determine the new range. */
2332 if (POINTER_TYPE_P (expr_type
))
2334 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2336 /* For MIN/MAX expressions with pointers, we only care about
2337 nullness, if both are non null, then the result is nonnull.
2338 If both are null, then the result is null. Otherwise they
2340 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2341 set_value_range_to_nonnull (vr
, expr_type
);
2342 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2343 set_value_range_to_null (vr
, expr_type
);
2345 set_value_range_to_varying (vr
);
2347 else if (code
== POINTER_PLUS_EXPR
)
2349 /* For pointer types, we are really only interested in asserting
2350 whether the expression evaluates to non-NULL. */
2351 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2352 set_value_range_to_nonnull (vr
, expr_type
);
2353 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2354 set_value_range_to_null (vr
, expr_type
);
2356 set_value_range_to_varying (vr
);
2358 else if (code
== BIT_AND_EXPR
)
2360 /* For pointer types, we are really only interested in asserting
2361 whether the expression evaluates to non-NULL. */
2362 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2363 set_value_range_to_nonnull (vr
, expr_type
);
2364 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2365 set_value_range_to_null (vr
, expr_type
);
2367 set_value_range_to_varying (vr
);
2370 set_value_range_to_varying (vr
);
2375 /* For integer ranges, apply the operation to each end of the
2376 range and see what we end up with. */
2377 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2379 /* If we have a PLUS_EXPR with two VR_RANGE integer constant
2380 ranges compute the precise range for such case if possible. */
2381 if (range_int_cst_p (&vr0
)
2382 && range_int_cst_p (&vr1
))
2384 signop sgn
= TYPE_SIGN (expr_type
);
2385 unsigned int prec
= TYPE_PRECISION (expr_type
);
2386 wide_int type_min
= wi::min_value (TYPE_PRECISION (expr_type
), sgn
);
2387 wide_int type_max
= wi::max_value (TYPE_PRECISION (expr_type
), sgn
);
2388 wide_int wmin
, wmax
;
2392 if (code
== PLUS_EXPR
)
2394 wmin
= wi::add (vr0
.min
, vr1
.min
);
2395 wmax
= wi::add (vr0
.max
, vr1
.max
);
2397 /* Check for overflow. */
2398 if (wi::cmp (vr1
.min
, 0, sgn
) != wi::cmp (wmin
, vr0
.min
, sgn
))
2399 min_ovf
= wi::cmp (vr0
.min
, wmin
, sgn
);
2400 if (wi::cmp (vr1
.max
, 0, sgn
) != wi::cmp (wmax
, vr0
.max
, sgn
))
2401 max_ovf
= wi::cmp (vr0
.max
, wmax
, sgn
);
2403 else /* if (code == MINUS_EXPR) */
2405 wmin
= wi::sub (vr0
.min
, vr1
.max
);
2406 wmax
= wi::sub (vr0
.max
, vr1
.min
);
2408 if (wi::cmp (0, vr1
.max
, sgn
) != wi::cmp (wmin
, vr0
.min
, sgn
))
2409 min_ovf
= wi::cmp (vr0
.min
, vr1
.max
, sgn
);
2410 if (wi::cmp (0, vr1
.min
, sgn
) != wi::cmp (wmax
, vr0
.max
, sgn
))
2411 max_ovf
= wi::cmp (vr0
.max
, vr1
.min
, sgn
);
2414 /* For non-wrapping arithmetic look at possibly smaller
2415 value-ranges of the type. */
2416 if (!TYPE_OVERFLOW_WRAPS (expr_type
))
2418 if (vrp_val_min (expr_type
))
2419 type_min
= vrp_val_min (expr_type
);
2420 if (vrp_val_max (expr_type
))
2421 type_max
= vrp_val_max (expr_type
);
2424 /* Check for type overflow. */
2427 if (wi::cmp (wmin
, type_min
, sgn
) == -1)
2429 else if (wi::cmp (wmin
, type_max
, sgn
) == 1)
2434 if (wi::cmp (wmax
, type_min
, sgn
) == -1)
2436 else if (wi::cmp (wmax
, type_max
, sgn
) == 1)
2440 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2442 /* If overflow wraps, truncate the values and adjust the
2443 range kind and bounds appropriately. */
2444 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
2445 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
2446 if (min_ovf
== max_ovf
)
2448 /* No overflow or both overflow or underflow. The
2449 range kind stays VR_RANGE. */
2450 min
= wide_int_to_tree (expr_type
, tmin
);
2451 max
= wide_int_to_tree (expr_type
, tmax
);
2453 else if (min_ovf
== -1
2456 /* Underflow and overflow, drop to VR_VARYING. */
2457 set_value_range_to_varying (vr
);
2462 /* Min underflow or max overflow. The range kind
2463 changes to VR_ANTI_RANGE. */
2464 bool covers
= false;
2465 wide_int tem
= tmin
;
2466 gcc_assert ((min_ovf
== -1 && max_ovf
== 0)
2467 || (max_ovf
== 1 && min_ovf
== 0));
2468 type
= VR_ANTI_RANGE
;
2470 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
2473 if (wi::cmp (tmax
, tem
, sgn
) > 0)
2475 /* If the anti-range would cover nothing, drop to varying.
2476 Likewise if the anti-range bounds are outside of the
2478 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
2480 set_value_range_to_varying (vr
);
2483 min
= wide_int_to_tree (expr_type
, tmin
);
2484 max
= wide_int_to_tree (expr_type
, tmax
);
2489 /* If overflow does not wrap, saturate to the types min/max
2493 if (needs_overflow_infinity (expr_type
)
2494 && supports_overflow_infinity (expr_type
))
2495 min
= negative_overflow_infinity (expr_type
);
2497 min
= wide_int_to_tree (expr_type
, type_min
);
2499 else if (min_ovf
== 1)
2501 if (needs_overflow_infinity (expr_type
)
2502 && supports_overflow_infinity (expr_type
))
2503 min
= positive_overflow_infinity (expr_type
);
2505 min
= wide_int_to_tree (expr_type
, type_max
);
2508 min
= wide_int_to_tree (expr_type
, wmin
);
2512 if (needs_overflow_infinity (expr_type
)
2513 && supports_overflow_infinity (expr_type
))
2514 max
= negative_overflow_infinity (expr_type
);
2516 max
= wide_int_to_tree (expr_type
, type_min
);
2518 else if (max_ovf
== 1)
2520 if (needs_overflow_infinity (expr_type
)
2521 && supports_overflow_infinity (expr_type
))
2522 max
= positive_overflow_infinity (expr_type
);
2524 max
= wide_int_to_tree (expr_type
, type_max
);
2527 max
= wide_int_to_tree (expr_type
, wmax
);
2529 if (needs_overflow_infinity (expr_type
)
2530 && supports_overflow_infinity (expr_type
))
2532 if (is_negative_overflow_infinity (vr0
.min
)
2533 || (code
== PLUS_EXPR
2534 ? is_negative_overflow_infinity (vr1
.min
)
2535 : is_positive_overflow_infinity (vr1
.max
)))
2536 min
= negative_overflow_infinity (expr_type
);
2537 if (is_positive_overflow_infinity (vr0
.max
)
2538 || (code
== PLUS_EXPR
2539 ? is_positive_overflow_infinity (vr1
.max
)
2540 : is_negative_overflow_infinity (vr1
.min
)))
2541 max
= positive_overflow_infinity (expr_type
);
2546 /* For other cases, for example if we have a PLUS_EXPR with two
2547 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2548 to compute a precise range for such a case.
2549 ??? General even mixed range kind operations can be expressed
2550 by for example transforming ~[3, 5] + [1, 2] to range-only
2551 operations and a union primitive:
2552 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2553 [-INF+1, 4] U [6, +INF(OVF)]
2554 though usually the union is not exactly representable with
2555 a single range or anti-range as the above is
2556 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2557 but one could use a scheme similar to equivalences for this. */
2558 set_value_range_to_varying (vr
);
2562 else if (code
== MIN_EXPR
2563 || code
== MAX_EXPR
)
2565 if (vr0
.type
== VR_RANGE
2566 && !symbolic_range_p (&vr0
))
2569 if (vr1
.type
== VR_RANGE
2570 && !symbolic_range_p (&vr1
))
2572 /* For operations that make the resulting range directly
2573 proportional to the original ranges, apply the operation to
2574 the same end of each range. */
2575 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2576 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2578 else if (code
== MIN_EXPR
)
2580 min
= vrp_val_min (expr_type
);
2583 else if (code
== MAX_EXPR
)
2586 max
= vrp_val_max (expr_type
);
2589 else if (vr1
.type
== VR_RANGE
2590 && !symbolic_range_p (&vr1
))
2593 if (code
== MIN_EXPR
)
2595 min
= vrp_val_min (expr_type
);
2598 else if (code
== MAX_EXPR
)
2601 max
= vrp_val_max (expr_type
);
2606 set_value_range_to_varying (vr
);
2610 else if (code
== MULT_EXPR
)
2612 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2613 drop to varying. This test requires 2*prec bits if both
2614 operands are signed and 2*prec + 2 bits if either is not. */
2616 signop sign
= TYPE_SIGN (expr_type
);
2617 unsigned int prec
= TYPE_PRECISION (expr_type
);
2619 if (range_int_cst_p (&vr0
)
2620 && range_int_cst_p (&vr1
)
2621 && TYPE_OVERFLOW_WRAPS (expr_type
))
2623 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
* 2) vrp_int
;
2624 typedef generic_wide_int
2625 <wi::extended_tree
<WIDE_INT_MAX_PRECISION
* 2> > vrp_int_cst
;
2626 vrp_int sizem1
= wi::mask
<vrp_int
> (prec
, false);
2627 vrp_int size
= sizem1
+ 1;
2629 /* Extend the values using the sign of the result to PREC2.
2630 From here on out, everthing is just signed math no matter
2631 what the input types were. */
2632 vrp_int min0
= vrp_int_cst (vr0
.min
);
2633 vrp_int max0
= vrp_int_cst (vr0
.max
);
2634 vrp_int min1
= vrp_int_cst (vr1
.min
);
2635 vrp_int max1
= vrp_int_cst (vr1
.max
);
2636 /* Canonicalize the intervals. */
2637 if (sign
== UNSIGNED
)
2639 if (wi::ltu_p (size
, min0
+ max0
))
2645 if (wi::ltu_p (size
, min1
+ max1
))
2652 vrp_int prod0
= min0
* min1
;
2653 vrp_int prod1
= min0
* max1
;
2654 vrp_int prod2
= max0
* min1
;
2655 vrp_int prod3
= max0
* max1
;
2657 /* Sort the 4 products so that min is in prod0 and max is in
2659 /* min0min1 > max0max1 */
2660 if (wi::gts_p (prod0
, prod3
))
2662 vrp_int tmp
= prod3
;
2667 /* min0max1 > max0min1 */
2668 if (wi::gts_p (prod1
, prod2
))
2670 vrp_int tmp
= prod2
;
2675 if (wi::gts_p (prod0
, prod1
))
2677 vrp_int tmp
= prod1
;
2682 if (wi::gts_p (prod2
, prod3
))
2684 vrp_int tmp
= prod3
;
2689 /* diff = max - min. */
2690 prod2
= prod3
- prod0
;
2691 if (wi::geu_p (prod2
, sizem1
))
2693 /* the range covers all values. */
2694 set_value_range_to_varying (vr
);
2698 /* The following should handle the wrapping and selecting
2699 VR_ANTI_RANGE for us. */
2700 min
= wide_int_to_tree (expr_type
, prod0
);
2701 max
= wide_int_to_tree (expr_type
, prod3
);
2702 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2706 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2707 drop to VR_VARYING. It would take more effort to compute a
2708 precise range for such a case. For example, if we have
2709 op0 == 65536 and op1 == 65536 with their ranges both being
2710 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2711 we cannot claim that the product is in ~[0,0]. Note that we
2712 are guaranteed to have vr0.type == vr1.type at this
2714 if (vr0
.type
== VR_ANTI_RANGE
2715 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2717 set_value_range_to_varying (vr
);
2721 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2724 else if (code
== RSHIFT_EXPR
2725 || code
== LSHIFT_EXPR
)
2727 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2728 then drop to VR_VARYING. Outside of this range we get undefined
2729 behavior from the shift operation. We cannot even trust
2730 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2731 shifts, and the operation at the tree level may be widened. */
2732 if (range_int_cst_p (&vr1
)
2733 && compare_tree_int (vr1
.min
, 0) >= 0
2734 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2736 if (code
== RSHIFT_EXPR
)
2738 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2741 /* We can map lshifts by constants to MULT_EXPR handling. */
2742 else if (code
== LSHIFT_EXPR
2743 && range_int_cst_singleton_p (&vr1
))
2745 bool saved_flag_wrapv
;
2746 value_range_t vr1p
= VR_INITIALIZER
;
2747 vr1p
.type
= VR_RANGE
;
2748 vr1p
.min
= (wide_int_to_tree
2750 wi::set_bit_in_zero (tree_to_shwi (vr1
.min
),
2751 TYPE_PRECISION (expr_type
))));
2752 vr1p
.max
= vr1p
.min
;
2753 /* We have to use a wrapping multiply though as signed overflow
2754 on lshifts is implementation defined in C89. */
2755 saved_flag_wrapv
= flag_wrapv
;
2757 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2759 flag_wrapv
= saved_flag_wrapv
;
2762 else if (code
== LSHIFT_EXPR
2763 && range_int_cst_p (&vr0
))
2765 int prec
= TYPE_PRECISION (expr_type
);
2766 int overflow_pos
= prec
;
2768 wide_int low_bound
, high_bound
;
2769 bool uns
= TYPE_UNSIGNED (expr_type
);
2770 bool in_bounds
= false;
2775 bound_shift
= overflow_pos
- tree_to_shwi (vr1
.max
);
2776 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2777 overflow. However, for that to happen, vr1.max needs to be
2778 zero, which means vr1 is a singleton range of zero, which
2779 means it should be handled by the previous LSHIFT_EXPR
2781 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
2782 wide_int complement
= ~(bound
- 1);
2787 high_bound
= complement
;
2788 if (wi::ltu_p (vr0
.max
, low_bound
))
2790 /* [5, 6] << [1, 2] == [10, 24]. */
2791 /* We're shifting out only zeroes, the value increases
2795 else if (wi::ltu_p (high_bound
, vr0
.min
))
2797 /* [0xffffff00, 0xffffffff] << [1, 2]
2798 == [0xfffffc00, 0xfffffffe]. */
2799 /* We're shifting out only ones, the value decreases
2806 /* [-1, 1] << [1, 2] == [-4, 4]. */
2807 low_bound
= complement
;
2809 if (wi::lts_p (vr0
.max
, high_bound
)
2810 && wi::lts_p (low_bound
, vr0
.min
))
2812 /* For non-negative numbers, we're shifting out only
2813 zeroes, the value increases monotonically.
2814 For negative numbers, we're shifting out only ones, the
2815 value decreases monotomically. */
2822 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2827 set_value_range_to_varying (vr
);
2830 else if (code
== TRUNC_DIV_EXPR
2831 || code
== FLOOR_DIV_EXPR
2832 || code
== CEIL_DIV_EXPR
2833 || code
== EXACT_DIV_EXPR
2834 || code
== ROUND_DIV_EXPR
)
2836 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2838 /* For division, if op1 has VR_RANGE but op0 does not, something
2839 can be deduced just from that range. Say [min, max] / [4, max]
2840 gives [min / 4, max / 4] range. */
2841 if (vr1
.type
== VR_RANGE
2842 && !symbolic_range_p (&vr1
)
2843 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
2845 vr0
.type
= type
= VR_RANGE
;
2846 vr0
.min
= vrp_val_min (expr_type
);
2847 vr0
.max
= vrp_val_max (expr_type
);
2851 set_value_range_to_varying (vr
);
2856 /* For divisions, if flag_non_call_exceptions is true, we must
2857 not eliminate a division by zero. */
2858 if (cfun
->can_throw_non_call_exceptions
2859 && (vr1
.type
!= VR_RANGE
2860 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2862 set_value_range_to_varying (vr
);
2866 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2867 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2869 if (vr0
.type
== VR_RANGE
2870 && (vr1
.type
!= VR_RANGE
2871 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2873 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
2878 if (TYPE_UNSIGNED (expr_type
)
2879 || value_range_nonnegative_p (&vr1
))
2881 /* For unsigned division or when divisor is known
2882 to be non-negative, the range has to cover
2883 all numbers from 0 to max for positive max
2884 and all numbers from min to 0 for negative min. */
2885 cmp
= compare_values (vr0
.max
, zero
);
2888 else if (cmp
== 0 || cmp
== 1)
2892 cmp
= compare_values (vr0
.min
, zero
);
2895 else if (cmp
== 0 || cmp
== -1)
2902 /* Otherwise the range is -max .. max or min .. -min
2903 depending on which bound is bigger in absolute value,
2904 as the division can change the sign. */
2905 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
2908 if (type
== VR_VARYING
)
2910 set_value_range_to_varying (vr
);
2916 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2920 else if (code
== TRUNC_MOD_EXPR
)
2922 if (vr1
.type
!= VR_RANGE
2923 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0
2924 || vrp_val_is_min (vr1
.min
))
2926 set_value_range_to_varying (vr
);
2930 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
2931 max
= fold_unary_to_constant (ABS_EXPR
, expr_type
, vr1
.min
);
2932 if (tree_int_cst_lt (max
, vr1
.max
))
2934 max
= int_const_binop (MINUS_EXPR
, max
, build_int_cst (TREE_TYPE (max
), 1));
2935 /* If the dividend is non-negative the modulus will be
2936 non-negative as well. */
2937 if (TYPE_UNSIGNED (expr_type
)
2938 || value_range_nonnegative_p (&vr0
))
2939 min
= build_int_cst (TREE_TYPE (max
), 0);
2941 min
= fold_unary_to_constant (NEGATE_EXPR
, expr_type
, max
);
2943 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
2945 bool int_cst_range0
, int_cst_range1
;
2946 wide_int may_be_nonzero0
, may_be_nonzero1
;
2947 wide_int must_be_nonzero0
, must_be_nonzero1
;
2949 int_cst_range0
= zero_nonzero_bits_from_vr (expr_type
, &vr0
,
2952 int_cst_range1
= zero_nonzero_bits_from_vr (expr_type
, &vr1
,
2957 if (code
== BIT_AND_EXPR
)
2959 min
= wide_int_to_tree (expr_type
,
2960 must_be_nonzero0
& must_be_nonzero1
);
2961 wide_int wmax
= may_be_nonzero0
& may_be_nonzero1
;
2962 /* If both input ranges contain only negative values we can
2963 truncate the result range maximum to the minimum of the
2964 input range maxima. */
2965 if (int_cst_range0
&& int_cst_range1
2966 && tree_int_cst_sgn (vr0
.max
) < 0
2967 && tree_int_cst_sgn (vr1
.max
) < 0)
2969 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
2970 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
2972 /* If either input range contains only non-negative values
2973 we can truncate the result range maximum to the respective
2974 maximum of the input range. */
2975 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
2976 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
2977 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
2978 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
2979 max
= wide_int_to_tree (expr_type
, wmax
);
2981 else if (code
== BIT_IOR_EXPR
)
2983 max
= wide_int_to_tree (expr_type
,
2984 may_be_nonzero0
| may_be_nonzero1
);
2985 wide_int wmin
= must_be_nonzero0
| must_be_nonzero1
;
2986 /* If the input ranges contain only positive values we can
2987 truncate the minimum of the result range to the maximum
2988 of the input range minima. */
2989 if (int_cst_range0
&& int_cst_range1
2990 && tree_int_cst_sgn (vr0
.min
) >= 0
2991 && tree_int_cst_sgn (vr1
.min
) >= 0)
2993 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
2994 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
2996 /* If either input range contains only negative values
2997 we can truncate the minimum of the result range to the
2998 respective minimum range. */
2999 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
3000 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3001 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
3002 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3003 min
= wide_int_to_tree (expr_type
, wmin
);
3005 else if (code
== BIT_XOR_EXPR
)
3007 wide_int result_zero_bits
= ((must_be_nonzero0
& must_be_nonzero1
)
3008 | ~(may_be_nonzero0
| may_be_nonzero1
));
3009 wide_int result_one_bits
3010 = (must_be_nonzero0
.and_not (may_be_nonzero1
)
3011 | must_be_nonzero1
.and_not (may_be_nonzero0
));
3012 max
= wide_int_to_tree (expr_type
, ~result_zero_bits
);
3013 min
= wide_int_to_tree (expr_type
, result_one_bits
);
3014 /* If the range has all positive or all negative values the
3015 result is better than VARYING. */
3016 if (tree_int_cst_sgn (min
) < 0
3017 || tree_int_cst_sgn (max
) >= 0)
3020 max
= min
= NULL_TREE
;
3026 /* If either MIN or MAX overflowed, then set the resulting range to
3027 VARYING. But we do accept an overflow infinity
3029 if (min
== NULL_TREE
3030 || !is_gimple_min_invariant (min
)
3031 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
3033 || !is_gimple_min_invariant (max
)
3034 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
3036 set_value_range_to_varying (vr
);
3042 2) [-INF, +-INF(OVF)]
3043 3) [+-INF(OVF), +INF]
3044 4) [+-INF(OVF), +-INF(OVF)]
3045 We learn nothing when we have INF and INF(OVF) on both sides.
3046 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3048 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
3049 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
3051 set_value_range_to_varying (vr
);
3055 cmp
= compare_values (min
, max
);
3056 if (cmp
== -2 || cmp
== 1)
3058 /* If the new range has its limits swapped around (MIN > MAX),
3059 then the operation caused one of them to wrap around, mark
3060 the new range VARYING. */
3061 set_value_range_to_varying (vr
);
3064 set_value_range (vr
, type
, min
, max
, NULL
);
3067 /* Extract range information from a binary expression OP0 CODE OP1 based on
3068 the ranges of each of its operands with resulting type EXPR_TYPE.
3069 The resulting range is stored in *VR. */
3072 extract_range_from_binary_expr (value_range_t
*vr
,
3073 enum tree_code code
,
3074 tree expr_type
, tree op0
, tree op1
)
3076 value_range_t vr0
= VR_INITIALIZER
;
3077 value_range_t vr1
= VR_INITIALIZER
;
3079 /* Get value ranges for each operand. For constant operands, create
3080 a new value range with the operand to simplify processing. */
3081 if (TREE_CODE (op0
) == SSA_NAME
)
3082 vr0
= *(get_value_range (op0
));
3083 else if (is_gimple_min_invariant (op0
))
3084 set_value_range_to_value (&vr0
, op0
, NULL
);
3086 set_value_range_to_varying (&vr0
);
3088 if (TREE_CODE (op1
) == SSA_NAME
)
3089 vr1
= *(get_value_range (op1
));
3090 else if (is_gimple_min_invariant (op1
))
3091 set_value_range_to_value (&vr1
, op1
, NULL
);
3093 set_value_range_to_varying (&vr1
);
3095 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3098 /* Extract range information from a unary operation CODE based on
3099 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3100 The The resulting range is stored in *VR. */
3103 extract_range_from_unary_expr_1 (value_range_t
*vr
,
3104 enum tree_code code
, tree type
,
3105 value_range_t
*vr0_
, tree op0_type
)
3107 value_range_t vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3109 /* VRP only operates on integral and pointer types. */
3110 if (!(INTEGRAL_TYPE_P (op0_type
)
3111 || POINTER_TYPE_P (op0_type
))
3112 || !(INTEGRAL_TYPE_P (type
)
3113 || POINTER_TYPE_P (type
)))
3115 set_value_range_to_varying (vr
);
3119 /* If VR0 is UNDEFINED, so is the result. */
3120 if (vr0
.type
== VR_UNDEFINED
)
3122 set_value_range_to_undefined (vr
);
3126 /* Handle operations that we express in terms of others. */
3127 if (code
== PAREN_EXPR
|| code
== OBJ_TYPE_REF
)
3129 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3130 copy_value_range (vr
, &vr0
);
3133 else if (code
== NEGATE_EXPR
)
3135 /* -X is simply 0 - X, so re-use existing code that also handles
3136 anti-ranges fine. */
3137 value_range_t zero
= VR_INITIALIZER
;
3138 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3139 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3142 else if (code
== BIT_NOT_EXPR
)
3144 /* ~X is simply -1 - X, so re-use existing code that also handles
3145 anti-ranges fine. */
3146 value_range_t minusone
= VR_INITIALIZER
;
3147 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3148 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3149 type
, &minusone
, &vr0
);
3153 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3154 and express op ~[] as (op []') U (op []''). */
3155 if (vr0
.type
== VR_ANTI_RANGE
3156 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3158 extract_range_from_unary_expr_1 (vr
, code
, type
, &vrtem0
, op0_type
);
3159 if (vrtem1
.type
!= VR_UNDEFINED
)
3161 value_range_t vrres
= VR_INITIALIZER
;
3162 extract_range_from_unary_expr_1 (&vrres
, code
, type
,
3164 vrp_meet (vr
, &vrres
);
3169 if (CONVERT_EXPR_CODE_P (code
))
3171 tree inner_type
= op0_type
;
3172 tree outer_type
= type
;
3174 /* If the expression evaluates to a pointer, we are only interested in
3175 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3176 if (POINTER_TYPE_P (type
))
3178 if (range_is_nonnull (&vr0
))
3179 set_value_range_to_nonnull (vr
, type
);
3180 else if (range_is_null (&vr0
))
3181 set_value_range_to_null (vr
, type
);
3183 set_value_range_to_varying (vr
);
3187 /* If VR0 is varying and we increase the type precision, assume
3188 a full range for the following transformation. */
3189 if (vr0
.type
== VR_VARYING
3190 && INTEGRAL_TYPE_P (inner_type
)
3191 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3193 vr0
.type
= VR_RANGE
;
3194 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3195 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3198 /* If VR0 is a constant range or anti-range and the conversion is
3199 not truncating we can convert the min and max values and
3200 canonicalize the resulting range. Otherwise we can do the
3201 conversion if the size of the range is less than what the
3202 precision of the target type can represent and the range is
3203 not an anti-range. */
3204 if ((vr0
.type
== VR_RANGE
3205 || vr0
.type
== VR_ANTI_RANGE
)
3206 && TREE_CODE (vr0
.min
) == INTEGER_CST
3207 && TREE_CODE (vr0
.max
) == INTEGER_CST
3208 && (!is_overflow_infinity (vr0
.min
)
3209 || (vr0
.type
== VR_RANGE
3210 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3211 && needs_overflow_infinity (outer_type
)
3212 && supports_overflow_infinity (outer_type
)))
3213 && (!is_overflow_infinity (vr0
.max
)
3214 || (vr0
.type
== VR_RANGE
3215 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3216 && needs_overflow_infinity (outer_type
)
3217 && supports_overflow_infinity (outer_type
)))
3218 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3219 || (vr0
.type
== VR_RANGE
3220 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3221 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3222 size_int (TYPE_PRECISION (outer_type
)))))))
3224 tree new_min
, new_max
;
3225 if (is_overflow_infinity (vr0
.min
))
3226 new_min
= negative_overflow_infinity (outer_type
);
3228 new_min
= force_fit_type (outer_type
, wi::to_widest (vr0
.min
),
3230 if (is_overflow_infinity (vr0
.max
))
3231 new_max
= positive_overflow_infinity (outer_type
);
3233 new_max
= force_fit_type (outer_type
, wi::to_widest (vr0
.max
),
3235 set_and_canonicalize_value_range (vr
, vr0
.type
,
3236 new_min
, new_max
, NULL
);
3240 set_value_range_to_varying (vr
);
3243 else if (code
== ABS_EXPR
)
3248 /* Pass through vr0 in the easy cases. */
3249 if (TYPE_UNSIGNED (type
)
3250 || value_range_nonnegative_p (&vr0
))
3252 copy_value_range (vr
, &vr0
);
3256 /* For the remaining varying or symbolic ranges we can't do anything
3258 if (vr0
.type
== VR_VARYING
3259 || symbolic_range_p (&vr0
))
3261 set_value_range_to_varying (vr
);
3265 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3267 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3268 && ((vr0
.type
== VR_RANGE
3269 && vrp_val_is_min (vr0
.min
))
3270 || (vr0
.type
== VR_ANTI_RANGE
3271 && !vrp_val_is_min (vr0
.min
))))
3273 set_value_range_to_varying (vr
);
3277 /* ABS_EXPR may flip the range around, if the original range
3278 included negative values. */
3279 if (is_overflow_infinity (vr0
.min
))
3280 min
= positive_overflow_infinity (type
);
3281 else if (!vrp_val_is_min (vr0
.min
))
3282 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3283 else if (!needs_overflow_infinity (type
))
3284 min
= TYPE_MAX_VALUE (type
);
3285 else if (supports_overflow_infinity (type
))
3286 min
= positive_overflow_infinity (type
);
3289 set_value_range_to_varying (vr
);
3293 if (is_overflow_infinity (vr0
.max
))
3294 max
= positive_overflow_infinity (type
);
3295 else if (!vrp_val_is_min (vr0
.max
))
3296 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3297 else if (!needs_overflow_infinity (type
))
3298 max
= TYPE_MAX_VALUE (type
);
3299 else if (supports_overflow_infinity (type
)
3300 /* We shouldn't generate [+INF, +INF] as set_value_range
3301 doesn't like this and ICEs. */
3302 && !is_positive_overflow_infinity (min
))
3303 max
= positive_overflow_infinity (type
);
3306 set_value_range_to_varying (vr
);
3310 cmp
= compare_values (min
, max
);
3312 /* If a VR_ANTI_RANGEs contains zero, then we have
3313 ~[-INF, min(MIN, MAX)]. */
3314 if (vr0
.type
== VR_ANTI_RANGE
)
3316 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3318 /* Take the lower of the two values. */
3322 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3323 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3324 flag_wrapv is set and the original anti-range doesn't include
3325 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3326 if (TYPE_OVERFLOW_WRAPS (type
))
3328 tree type_min_value
= TYPE_MIN_VALUE (type
);
3330 min
= (vr0
.min
!= type_min_value
3331 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3332 build_int_cst (TREE_TYPE (type_min_value
), 1))
3337 if (overflow_infinity_range_p (&vr0
))
3338 min
= negative_overflow_infinity (type
);
3340 min
= TYPE_MIN_VALUE (type
);
3345 /* All else has failed, so create the range [0, INF], even for
3346 flag_wrapv since TYPE_MIN_VALUE is in the original
3348 vr0
.type
= VR_RANGE
;
3349 min
= build_int_cst (type
, 0);
3350 if (needs_overflow_infinity (type
))
3352 if (supports_overflow_infinity (type
))
3353 max
= positive_overflow_infinity (type
);
3356 set_value_range_to_varying (vr
);
3361 max
= TYPE_MAX_VALUE (type
);
3365 /* If the range contains zero then we know that the minimum value in the
3366 range will be zero. */
3367 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3371 min
= build_int_cst (type
, 0);
3375 /* If the range was reversed, swap MIN and MAX. */
3384 cmp
= compare_values (min
, max
);
3385 if (cmp
== -2 || cmp
== 1)
3387 /* If the new range has its limits swapped around (MIN > MAX),
3388 then the operation caused one of them to wrap around, mark
3389 the new range VARYING. */
3390 set_value_range_to_varying (vr
);
3393 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3397 /* For unhandled operations fall back to varying. */
3398 set_value_range_to_varying (vr
);
3403 /* Extract range information from a unary expression CODE OP0 based on
3404 the range of its operand with resulting type TYPE.
3405 The resulting range is stored in *VR. */
3408 extract_range_from_unary_expr (value_range_t
*vr
, enum tree_code code
,
3409 tree type
, tree op0
)
3411 value_range_t vr0
= VR_INITIALIZER
;
3413 /* Get value ranges for the operand. For constant operands, create
3414 a new value range with the operand to simplify processing. */
3415 if (TREE_CODE (op0
) == SSA_NAME
)
3416 vr0
= *(get_value_range (op0
));
3417 else if (is_gimple_min_invariant (op0
))
3418 set_value_range_to_value (&vr0
, op0
, NULL
);
3420 set_value_range_to_varying (&vr0
);
3422 extract_range_from_unary_expr_1 (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3426 /* Extract range information from a conditional expression STMT based on
3427 the ranges of each of its operands and the expression code. */
3430 extract_range_from_cond_expr (value_range_t
*vr
, gimple stmt
)
3433 value_range_t vr0
= VR_INITIALIZER
;
3434 value_range_t vr1
= VR_INITIALIZER
;
3436 /* Get value ranges for each operand. For constant operands, create
3437 a new value range with the operand to simplify processing. */
3438 op0
= gimple_assign_rhs2 (stmt
);
3439 if (TREE_CODE (op0
) == SSA_NAME
)
3440 vr0
= *(get_value_range (op0
));
3441 else if (is_gimple_min_invariant (op0
))
3442 set_value_range_to_value (&vr0
, op0
, NULL
);
3444 set_value_range_to_varying (&vr0
);
3446 op1
= gimple_assign_rhs3 (stmt
);
3447 if (TREE_CODE (op1
) == SSA_NAME
)
3448 vr1
= *(get_value_range (op1
));
3449 else if (is_gimple_min_invariant (op1
))
3450 set_value_range_to_value (&vr1
, op1
, NULL
);
3452 set_value_range_to_varying (&vr1
);
3454 /* The resulting value range is the union of the operand ranges */
3455 copy_value_range (vr
, &vr0
);
3456 vrp_meet (vr
, &vr1
);
3460 /* Extract range information from a comparison expression EXPR based
3461 on the range of its operand and the expression code. */
3464 extract_range_from_comparison (value_range_t
*vr
, enum tree_code code
,
3465 tree type
, tree op0
, tree op1
)
3470 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3473 /* A disadvantage of using a special infinity as an overflow
3474 representation is that we lose the ability to record overflow
3475 when we don't have an infinity. So we have to ignore a result
3476 which relies on overflow. */
3478 if (val
&& !is_overflow_infinity (val
) && !sop
)
3480 /* Since this expression was found on the RHS of an assignment,
3481 its type may be different from _Bool. Convert VAL to EXPR's
3483 val
= fold_convert (type
, val
);
3484 if (is_gimple_min_invariant (val
))
3485 set_value_range_to_value (vr
, val
, vr
->equiv
);
3487 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3490 /* The result of a comparison is always true or false. */
3491 set_value_range_to_truthvalue (vr
, type
);
3494 /* Try to derive a nonnegative or nonzero range out of STMT relying
3495 primarily on generic routines in fold in conjunction with range data.
3496 Store the result in *VR */
3499 extract_range_basic (value_range_t
*vr
, gimple stmt
)
3502 tree type
= gimple_expr_type (stmt
);
3504 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
3506 tree fndecl
= gimple_call_fndecl (stmt
), arg
;
3507 int mini
, maxi
, zerov
= 0, prec
;
3509 switch (DECL_FUNCTION_CODE (fndecl
))
3511 case BUILT_IN_CONSTANT_P
:
3512 /* If the call is __builtin_constant_p and the argument is a
3513 function parameter resolve it to false. This avoids bogus
3514 array bound warnings.
3515 ??? We could do this as early as inlining is finished. */
3516 arg
= gimple_call_arg (stmt
, 0);
3517 if (TREE_CODE (arg
) == SSA_NAME
3518 && SSA_NAME_IS_DEFAULT_DEF (arg
)
3519 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
)
3521 set_value_range_to_null (vr
, type
);
3525 /* Both __builtin_ffs* and __builtin_popcount return
3527 CASE_INT_FN (BUILT_IN_FFS
):
3528 CASE_INT_FN (BUILT_IN_POPCOUNT
):
3529 arg
= gimple_call_arg (stmt
, 0);
3530 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3533 if (TREE_CODE (arg
) == SSA_NAME
)
3535 value_range_t
*vr0
= get_value_range (arg
);
3536 /* If arg is non-zero, then ffs or popcount
3538 if (((vr0
->type
== VR_RANGE
3539 && integer_nonzerop (vr0
->min
))
3540 || (vr0
->type
== VR_ANTI_RANGE
3541 && integer_zerop (vr0
->min
)))
3542 && !is_overflow_infinity (vr0
->min
))
3544 /* If some high bits are known to be zero,
3545 we can decrease the maximum. */
3546 if (vr0
->type
== VR_RANGE
3547 && TREE_CODE (vr0
->max
) == INTEGER_CST
3548 && !is_overflow_infinity (vr0
->max
))
3549 maxi
= tree_floor_log2 (vr0
->max
) + 1;
3552 /* __builtin_parity* returns [0, 1]. */
3553 CASE_INT_FN (BUILT_IN_PARITY
):
3557 /* __builtin_c[lt]z* return [0, prec-1], except for
3558 when the argument is 0, but that is undefined behavior.
3559 On many targets where the CLZ RTL or optab value is defined
3560 for 0 the value is prec, so include that in the range
3562 CASE_INT_FN (BUILT_IN_CLZ
):
3563 arg
= gimple_call_arg (stmt
, 0);
3564 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3567 if (optab_handler (clz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3569 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3571 /* Handle only the single common value. */
3573 /* Magic value to give up, unless vr0 proves
3576 if (TREE_CODE (arg
) == SSA_NAME
)
3578 value_range_t
*vr0
= get_value_range (arg
);
3579 /* From clz of VR_RANGE minimum we can compute
3581 if (vr0
->type
== VR_RANGE
3582 && TREE_CODE (vr0
->min
) == INTEGER_CST
3583 && !is_overflow_infinity (vr0
->min
))
3585 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min
);
3589 else if (vr0
->type
== VR_ANTI_RANGE
3590 && integer_zerop (vr0
->min
)
3591 && !is_overflow_infinity (vr0
->min
))
3598 /* From clz of VR_RANGE maximum we can compute
3600 if (vr0
->type
== VR_RANGE
3601 && TREE_CODE (vr0
->max
) == INTEGER_CST
3602 && !is_overflow_infinity (vr0
->max
))
3604 mini
= prec
- 1 - tree_floor_log2 (vr0
->max
);
3612 /* __builtin_ctz* return [0, prec-1], except for
3613 when the argument is 0, but that is undefined behavior.
3614 If there is a ctz optab for this mode and
3615 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3616 otherwise just assume 0 won't be seen. */
3617 CASE_INT_FN (BUILT_IN_CTZ
):
3618 arg
= gimple_call_arg (stmt
, 0);
3619 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3622 if (optab_handler (ctz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3624 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3627 /* Handle only the two common values. */
3630 else if (zerov
== prec
)
3633 /* Magic value to give up, unless vr0 proves
3637 if (TREE_CODE (arg
) == SSA_NAME
)
3639 value_range_t
*vr0
= get_value_range (arg
);
3640 /* If arg is non-zero, then use [0, prec - 1]. */
3641 if (((vr0
->type
== VR_RANGE
3642 && integer_nonzerop (vr0
->min
))
3643 || (vr0
->type
== VR_ANTI_RANGE
3644 && integer_zerop (vr0
->min
)))
3645 && !is_overflow_infinity (vr0
->min
))
3650 /* If some high bits are known to be zero,
3651 we can decrease the result maximum. */
3652 if (vr0
->type
== VR_RANGE
3653 && TREE_CODE (vr0
->max
) == INTEGER_CST
3654 && !is_overflow_infinity (vr0
->max
))
3656 maxi
= tree_floor_log2 (vr0
->max
);
3657 /* For vr0 [0, 0] give up. */
3665 /* __builtin_clrsb* returns [0, prec-1]. */
3666 CASE_INT_FN (BUILT_IN_CLRSB
):
3667 arg
= gimple_call_arg (stmt
, 0);
3668 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3673 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, mini
),
3674 build_int_cst (type
, maxi
), NULL
);
3680 else if (is_gimple_call (stmt
)
3681 && gimple_call_internal_p (stmt
))
3683 enum tree_code subcode
= ERROR_MARK
;
3684 switch (gimple_call_internal_fn (stmt
))
3686 case IFN_UBSAN_CHECK_ADD
:
3687 subcode
= PLUS_EXPR
;
3689 case IFN_UBSAN_CHECK_SUB
:
3690 subcode
= MINUS_EXPR
;
3692 case IFN_UBSAN_CHECK_MUL
:
3693 subcode
= MULT_EXPR
;
3698 if (subcode
!= ERROR_MARK
)
3700 bool saved_flag_wrapv
= flag_wrapv
;
3701 /* Pretend the arithmetics is wrapping. If there is
3702 any overflow, we'll complain, but will actually do
3703 wrapping operation. */
3705 extract_range_from_binary_expr (vr
, subcode
, type
,
3706 gimple_call_arg (stmt
, 0),
3707 gimple_call_arg (stmt
, 1));
3708 flag_wrapv
= saved_flag_wrapv
;
3710 /* If for both arguments vrp_valueize returned non-NULL,
3711 this should have been already folded and if not, it
3712 wasn't folded because of overflow. Avoid removing the
3713 UBSAN_CHECK_* calls in that case. */
3714 if (vr
->type
== VR_RANGE
3715 && (vr
->min
== vr
->max
3716 || operand_equal_p (vr
->min
, vr
->max
, 0)))
3717 set_value_range_to_varying (vr
);
3721 if (INTEGRAL_TYPE_P (type
)
3722 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
3723 set_value_range_to_nonnegative (vr
, type
,
3724 sop
|| stmt_overflow_infinity (stmt
));
3725 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
3727 set_value_range_to_nonnull (vr
, type
);
3729 set_value_range_to_varying (vr
);
3733 /* Try to compute a useful range out of assignment STMT and store it
3737 extract_range_from_assignment (value_range_t
*vr
, gimple stmt
)
3739 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3741 if (code
== ASSERT_EXPR
)
3742 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
3743 else if (code
== SSA_NAME
)
3744 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
3745 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
3746 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
3747 gimple_expr_type (stmt
),
3748 gimple_assign_rhs1 (stmt
),
3749 gimple_assign_rhs2 (stmt
));
3750 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
3751 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
3752 gimple_expr_type (stmt
),
3753 gimple_assign_rhs1 (stmt
));
3754 else if (code
== COND_EXPR
)
3755 extract_range_from_cond_expr (vr
, stmt
);
3756 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3757 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
3758 gimple_expr_type (stmt
),
3759 gimple_assign_rhs1 (stmt
),
3760 gimple_assign_rhs2 (stmt
));
3761 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
3762 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
3763 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
3765 set_value_range_to_varying (vr
);
3767 if (vr
->type
== VR_VARYING
)
3768 extract_range_basic (vr
, stmt
);
3771 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3772 would be profitable to adjust VR using scalar evolution information
3773 for VAR. If so, update VR with the new limits. */
3776 adjust_range_with_scev (value_range_t
*vr
, struct loop
*loop
,
3777 gimple stmt
, tree var
)
3779 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
3780 enum ev_direction dir
;
3782 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3783 better opportunities than a regular range, but I'm not sure. */
3784 if (vr
->type
== VR_ANTI_RANGE
)
3787 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
3789 /* Like in PR19590, scev can return a constant function. */
3790 if (is_gimple_min_invariant (chrec
))
3792 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
3796 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3799 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
3800 tem
= op_with_constant_singleton_value_range (init
);
3803 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
3804 tem
= op_with_constant_singleton_value_range (step
);
3808 /* If STEP is symbolic, we can't know whether INIT will be the
3809 minimum or maximum value in the range. Also, unless INIT is
3810 a simple expression, compare_values and possibly other functions
3811 in tree-vrp won't be able to handle it. */
3812 if (step
== NULL_TREE
3813 || !is_gimple_min_invariant (step
)
3814 || !valid_value_p (init
))
3817 dir
= scev_direction (chrec
);
3818 if (/* Do not adjust ranges if we do not know whether the iv increases
3819 or decreases, ... */
3820 dir
== EV_DIR_UNKNOWN
3821 /* ... or if it may wrap. */
3822 || scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
3826 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3827 negative_overflow_infinity and positive_overflow_infinity,
3828 because we have concluded that the loop probably does not
3831 type
= TREE_TYPE (var
);
3832 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
3833 tmin
= lower_bound_in_type (type
, type
);
3835 tmin
= TYPE_MIN_VALUE (type
);
3836 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
3837 tmax
= upper_bound_in_type (type
, type
);
3839 tmax
= TYPE_MAX_VALUE (type
);
3841 /* Try to use estimated number of iterations for the loop to constrain the
3842 final value in the evolution. */
3843 if (TREE_CODE (step
) == INTEGER_CST
3844 && is_gimple_val (init
)
3845 && (TREE_CODE (init
) != SSA_NAME
3846 || get_value_range (init
)->type
== VR_RANGE
))
3850 /* We are only entering here for loop header PHI nodes, so using
3851 the number of latch executions is the correct thing to use. */
3852 if (max_loop_iterations (loop
, &nit
))
3854 value_range_t maxvr
= VR_INITIALIZER
;
3855 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
3858 widest_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
,
3860 /* If the multiplication overflowed we can't do a meaningful
3861 adjustment. Likewise if the result doesn't fit in the type
3862 of the induction variable. For a signed type we have to
3863 check whether the result has the expected signedness which
3864 is that of the step as number of iterations is unsigned. */
3866 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
3868 || wi::gts_p (wtmp
, 0) == wi::gts_p (step
, 0)))
3870 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
3871 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
3872 TREE_TYPE (init
), init
, tem
);
3873 /* Likewise if the addition did. */
3874 if (maxvr
.type
== VR_RANGE
)
3883 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
3888 /* For VARYING or UNDEFINED ranges, just about anything we get
3889 from scalar evolutions should be better. */
3891 if (dir
== EV_DIR_DECREASES
)
3896 /* If we would create an invalid range, then just assume we
3897 know absolutely nothing. This may be over-conservative,
3898 but it's clearly safe, and should happen only in unreachable
3899 parts of code, or for invalid programs. */
3900 if (compare_values (min
, max
) == 1)
3903 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
3905 else if (vr
->type
== VR_RANGE
)
3910 if (dir
== EV_DIR_DECREASES
)
3912 /* INIT is the maximum value. If INIT is lower than VR->MAX
3913 but no smaller than VR->MIN, set VR->MAX to INIT. */
3914 if (compare_values (init
, max
) == -1)
3917 /* According to the loop information, the variable does not
3918 overflow. If we think it does, probably because of an
3919 overflow due to arithmetic on a different INF value,
3921 if (is_negative_overflow_infinity (min
)
3922 || compare_values (min
, tmin
) == -1)
3928 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
3929 if (compare_values (init
, min
) == 1)
3932 if (is_positive_overflow_infinity (max
)
3933 || compare_values (tmax
, max
) == -1)
3937 /* If we just created an invalid range with the minimum
3938 greater than the maximum, we fail conservatively.
3939 This should happen only in unreachable
3940 parts of code, or for invalid programs. */
3941 if (compare_values (min
, max
) == 1)
3944 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
3949 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
3951 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
3952 all the values in the ranges.
3954 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
3956 - Return NULL_TREE if it is not always possible to determine the
3957 value of the comparison.
3959 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
3960 overflow infinity was used in the test. */
3964 compare_ranges (enum tree_code comp
, value_range_t
*vr0
, value_range_t
*vr1
,
3965 bool *strict_overflow_p
)
3967 /* VARYING or UNDEFINED ranges cannot be compared. */
3968 if (vr0
->type
== VR_VARYING
3969 || vr0
->type
== VR_UNDEFINED
3970 || vr1
->type
== VR_VARYING
3971 || vr1
->type
== VR_UNDEFINED
)
3974 /* Anti-ranges need to be handled separately. */
3975 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
3977 /* If both are anti-ranges, then we cannot compute any
3979 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
3982 /* These comparisons are never statically computable. */
3989 /* Equality can be computed only between a range and an
3990 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
3991 if (vr0
->type
== VR_RANGE
)
3993 /* To simplify processing, make VR0 the anti-range. */
3994 value_range_t
*tmp
= vr0
;
3999 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
4001 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
4002 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
4003 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4008 if (!usable_range_p (vr0
, strict_overflow_p
)
4009 || !usable_range_p (vr1
, strict_overflow_p
))
4012 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4013 operands around and change the comparison code. */
4014 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4017 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
4023 if (comp
== EQ_EXPR
)
4025 /* Equality may only be computed if both ranges represent
4026 exactly one value. */
4027 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
4028 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
4030 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
4032 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
4034 if (cmp_min
== 0 && cmp_max
== 0)
4035 return boolean_true_node
;
4036 else if (cmp_min
!= -2 && cmp_max
!= -2)
4037 return boolean_false_node
;
4039 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4040 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
4041 strict_overflow_p
) == 1
4042 || compare_values_warnv (vr1
->min
, vr0
->max
,
4043 strict_overflow_p
) == 1)
4044 return boolean_false_node
;
4048 else if (comp
== NE_EXPR
)
4052 /* If VR0 is completely to the left or completely to the right
4053 of VR1, they are always different. Notice that we need to
4054 make sure that both comparisons yield similar results to
4055 avoid comparing values that cannot be compared at
4057 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4058 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4059 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
4060 return boolean_true_node
;
4062 /* If VR0 and VR1 represent a single value and are identical,
4064 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
4065 strict_overflow_p
) == 0
4066 && compare_values_warnv (vr1
->min
, vr1
->max
,
4067 strict_overflow_p
) == 0
4068 && compare_values_warnv (vr0
->min
, vr1
->min
,
4069 strict_overflow_p
) == 0
4070 && compare_values_warnv (vr0
->max
, vr1
->max
,
4071 strict_overflow_p
) == 0)
4072 return boolean_false_node
;
4074 /* Otherwise, they may or may not be different. */
4078 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4082 /* If VR0 is to the left of VR1, return true. */
4083 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4084 if ((comp
== LT_EXPR
&& tst
== -1)
4085 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4087 if (overflow_infinity_range_p (vr0
)
4088 || overflow_infinity_range_p (vr1
))
4089 *strict_overflow_p
= true;
4090 return boolean_true_node
;
4093 /* If VR0 is to the right of VR1, return false. */
4094 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4095 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4096 || (comp
== LE_EXPR
&& tst
== 1))
4098 if (overflow_infinity_range_p (vr0
)
4099 || overflow_infinity_range_p (vr1
))
4100 *strict_overflow_p
= true;
4101 return boolean_false_node
;
4104 /* Otherwise, we don't know. */
4112 /* Given a value range VR, a value VAL and a comparison code COMP, return
4113 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4114 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4115 always returns false. Return NULL_TREE if it is not always
4116 possible to determine the value of the comparison. Also set
4117 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4118 infinity was used in the test. */
4121 compare_range_with_value (enum tree_code comp
, value_range_t
*vr
, tree val
,
4122 bool *strict_overflow_p
)
4124 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4127 /* Anti-ranges need to be handled separately. */
4128 if (vr
->type
== VR_ANTI_RANGE
)
4130 /* For anti-ranges, the only predicates that we can compute at
4131 compile time are equality and inequality. */
4138 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4139 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4140 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4145 if (!usable_range_p (vr
, strict_overflow_p
))
4148 if (comp
== EQ_EXPR
)
4150 /* EQ_EXPR may only be computed if VR represents exactly
4152 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4154 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4156 return boolean_true_node
;
4157 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4158 return boolean_false_node
;
4160 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4161 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4162 return boolean_false_node
;
4166 else if (comp
== NE_EXPR
)
4168 /* If VAL is not inside VR, then they are always different. */
4169 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4170 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4171 return boolean_true_node
;
4173 /* If VR represents exactly one value equal to VAL, then return
4175 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4176 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4177 return boolean_false_node
;
4179 /* Otherwise, they may or may not be different. */
4182 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4186 /* If VR is to the left of VAL, return true. */
4187 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4188 if ((comp
== LT_EXPR
&& tst
== -1)
4189 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4191 if (overflow_infinity_range_p (vr
))
4192 *strict_overflow_p
= true;
4193 return boolean_true_node
;
4196 /* If VR is to the right of VAL, return false. */
4197 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4198 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4199 || (comp
== LE_EXPR
&& tst
== 1))
4201 if (overflow_infinity_range_p (vr
))
4202 *strict_overflow_p
= true;
4203 return boolean_false_node
;
4206 /* Otherwise, we don't know. */
4209 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4213 /* If VR is to the right of VAL, return true. */
4214 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4215 if ((comp
== GT_EXPR
&& tst
== 1)
4216 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4218 if (overflow_infinity_range_p (vr
))
4219 *strict_overflow_p
= true;
4220 return boolean_true_node
;
4223 /* If VR is to the left of VAL, return false. */
4224 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4225 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4226 || (comp
== GE_EXPR
&& tst
== -1))
4228 if (overflow_infinity_range_p (vr
))
4229 *strict_overflow_p
= true;
4230 return boolean_false_node
;
4233 /* Otherwise, we don't know. */
4241 /* Debugging dumps. */
4243 void dump_value_range (FILE *, value_range_t
*);
4244 void debug_value_range (value_range_t
*);
4245 void dump_all_value_ranges (FILE *);
4246 void debug_all_value_ranges (void);
4247 void dump_vr_equiv (FILE *, bitmap
);
4248 void debug_vr_equiv (bitmap
);
4251 /* Dump value range VR to FILE. */
4254 dump_value_range (FILE *file
, value_range_t
*vr
)
4257 fprintf (file
, "[]");
4258 else if (vr
->type
== VR_UNDEFINED
)
4259 fprintf (file
, "UNDEFINED");
4260 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4262 tree type
= TREE_TYPE (vr
->min
);
4264 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4266 if (is_negative_overflow_infinity (vr
->min
))
4267 fprintf (file
, "-INF(OVF)");
4268 else if (INTEGRAL_TYPE_P (type
)
4269 && !TYPE_UNSIGNED (type
)
4270 && vrp_val_is_min (vr
->min
))
4271 fprintf (file
, "-INF");
4273 print_generic_expr (file
, vr
->min
, 0);
4275 fprintf (file
, ", ");
4277 if (is_positive_overflow_infinity (vr
->max
))
4278 fprintf (file
, "+INF(OVF)");
4279 else if (INTEGRAL_TYPE_P (type
)
4280 && vrp_val_is_max (vr
->max
))
4281 fprintf (file
, "+INF");
4283 print_generic_expr (file
, vr
->max
, 0);
4285 fprintf (file
, "]");
4292 fprintf (file
, " EQUIVALENCES: { ");
4294 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4296 print_generic_expr (file
, ssa_name (i
), 0);
4297 fprintf (file
, " ");
4301 fprintf (file
, "} (%u elements)", c
);
4304 else if (vr
->type
== VR_VARYING
)
4305 fprintf (file
, "VARYING");
4307 fprintf (file
, "INVALID RANGE");
4311 /* Dump value range VR to stderr. */
4314 debug_value_range (value_range_t
*vr
)
4316 dump_value_range (stderr
, vr
);
4317 fprintf (stderr
, "\n");
4321 /* Dump value ranges of all SSA_NAMEs to FILE. */
4324 dump_all_value_ranges (FILE *file
)
4328 for (i
= 0; i
< num_vr_values
; i
++)
4332 print_generic_expr (file
, ssa_name (i
), 0);
4333 fprintf (file
, ": ");
4334 dump_value_range (file
, vr_value
[i
]);
4335 fprintf (file
, "\n");
4339 fprintf (file
, "\n");
4343 /* Dump all value ranges to stderr. */
4346 debug_all_value_ranges (void)
4348 dump_all_value_ranges (stderr
);
4352 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4353 create a new SSA name N and return the assertion assignment
4354 'N = ASSERT_EXPR <V, V OP W>'. */
4357 build_assert_expr_for (tree cond
, tree v
)
4362 gcc_assert (TREE_CODE (v
) == SSA_NAME
4363 && COMPARISON_CLASS_P (cond
));
4365 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4366 assertion
= gimple_build_assign (NULL_TREE
, a
);
4368 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4369 operand of the ASSERT_EXPR. Create it so the new name and the old one
4370 are registered in the replacement table so that we can fix the SSA web
4371 after adding all the ASSERT_EXPRs. */
4372 create_new_def_for (v
, assertion
, NULL
);
4378 /* Return false if EXPR is a predicate expression involving floating
4382 fp_predicate (gimple stmt
)
4384 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4386 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4389 /* If the range of values taken by OP can be inferred after STMT executes,
4390 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4391 describes the inferred range. Return true if a range could be
4395 infer_value_range (gimple stmt
, tree op
, enum tree_code
*comp_code_p
, tree
*val_p
)
4398 *comp_code_p
= ERROR_MARK
;
4400 /* Do not attempt to infer anything in names that flow through
4402 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4405 /* Similarly, don't infer anything from statements that may throw
4406 exceptions. ??? Relax this requirement? */
4407 if (stmt_could_throw_p (stmt
))
4410 /* If STMT is the last statement of a basic block with no normal
4411 successors, there is no point inferring anything about any of its
4412 operands. We would not be able to find a proper insertion point
4413 for the assertion, anyway. */
4414 if (stmt_ends_bb_p (stmt
))
4419 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
4420 if (!(e
->flags
& EDGE_ABNORMAL
))
4426 if (infer_nonnull_range (stmt
, op
, true, true))
4428 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4429 *comp_code_p
= NE_EXPR
;
4437 void dump_asserts_for (FILE *, tree
);
4438 void debug_asserts_for (tree
);
4439 void dump_all_asserts (FILE *);
4440 void debug_all_asserts (void);
4442 /* Dump all the registered assertions for NAME to FILE. */
4445 dump_asserts_for (FILE *file
, tree name
)
4449 fprintf (file
, "Assertions to be inserted for ");
4450 print_generic_expr (file
, name
, 0);
4451 fprintf (file
, "\n");
4453 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4456 fprintf (file
, "\t");
4457 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4458 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4461 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4462 loc
->e
->dest
->index
);
4463 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4465 fprintf (file
, "\n\tPREDICATE: ");
4466 print_generic_expr (file
, name
, 0);
4467 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
4468 print_generic_expr (file
, loc
->val
, 0);
4469 fprintf (file
, "\n\n");
4473 fprintf (file
, "\n");
4477 /* Dump all the registered assertions for NAME to stderr. */
4480 debug_asserts_for (tree name
)
4482 dump_asserts_for (stderr
, name
);
4486 /* Dump all the registered assertions for all the names to FILE. */
4489 dump_all_asserts (FILE *file
)
4494 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4495 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4496 dump_asserts_for (file
, ssa_name (i
));
4497 fprintf (file
, "\n");
4501 /* Dump all the registered assertions for all the names to stderr. */
4504 debug_all_asserts (void)
4506 dump_all_asserts (stderr
);
4510 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4511 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4512 E->DEST, then register this location as a possible insertion point
4513 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4515 BB, E and SI provide the exact insertion point for the new
4516 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4517 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4518 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4519 must not be NULL. */
4522 register_new_assert_for (tree name
, tree expr
,
4523 enum tree_code comp_code
,
4527 gimple_stmt_iterator si
)
4529 assert_locus_t n
, loc
, last_loc
;
4530 basic_block dest_bb
;
4532 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4535 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4536 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4538 /* Never build an assert comparing against an integer constant with
4539 TREE_OVERFLOW set. This confuses our undefined overflow warning
4541 if (TREE_OVERFLOW_P (val
))
4542 val
= drop_tree_overflow (val
);
4544 /* The new assertion A will be inserted at BB or E. We need to
4545 determine if the new location is dominated by a previously
4546 registered location for A. If we are doing an edge insertion,
4547 assume that A will be inserted at E->DEST. Note that this is not
4550 If E is a critical edge, it will be split. But even if E is
4551 split, the new block will dominate the same set of blocks that
4554 The reverse, however, is not true, blocks dominated by E->DEST
4555 will not be dominated by the new block created to split E. So,
4556 if the insertion location is on a critical edge, we will not use
4557 the new location to move another assertion previously registered
4558 at a block dominated by E->DEST. */
4559 dest_bb
= (bb
) ? bb
: e
->dest
;
4561 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4562 VAL at a block dominating DEST_BB, then we don't need to insert a new
4563 one. Similarly, if the same assertion already exists at a block
4564 dominated by DEST_BB and the new location is not on a critical
4565 edge, then update the existing location for the assertion (i.e.,
4566 move the assertion up in the dominance tree).
4568 Note, this is implemented as a simple linked list because there
4569 should not be more than a handful of assertions registered per
4570 name. If this becomes a performance problem, a table hashed by
4571 COMP_CODE and VAL could be implemented. */
4572 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4576 if (loc
->comp_code
== comp_code
4578 || operand_equal_p (loc
->val
, val
, 0))
4579 && (loc
->expr
== expr
4580 || operand_equal_p (loc
->expr
, expr
, 0)))
4582 /* If E is not a critical edge and DEST_BB
4583 dominates the existing location for the assertion, move
4584 the assertion up in the dominance tree by updating its
4585 location information. */
4586 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
4587 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
4596 /* Update the last node of the list and move to the next one. */
4601 /* If we didn't find an assertion already registered for
4602 NAME COMP_CODE VAL, add a new one at the end of the list of
4603 assertions associated with NAME. */
4604 n
= XNEW (struct assert_locus_d
);
4608 n
->comp_code
= comp_code
;
4616 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
4618 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
4621 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4622 Extract a suitable test code and value and store them into *CODE_P and
4623 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4625 If no extraction was possible, return FALSE, otherwise return TRUE.
4627 If INVERT is true, then we invert the result stored into *CODE_P. */
4630 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
4631 tree cond_op0
, tree cond_op1
,
4632 bool invert
, enum tree_code
*code_p
,
4635 enum tree_code comp_code
;
4638 /* Otherwise, we have a comparison of the form NAME COMP VAL
4639 or VAL COMP NAME. */
4640 if (name
== cond_op1
)
4642 /* If the predicate is of the form VAL COMP NAME, flip
4643 COMP around because we need to register NAME as the
4644 first operand in the predicate. */
4645 comp_code
= swap_tree_comparison (cond_code
);
4650 /* The comparison is of the form NAME COMP VAL, so the
4651 comparison code remains unchanged. */
4652 comp_code
= cond_code
;
4656 /* Invert the comparison code as necessary. */
4658 comp_code
= invert_tree_comparison (comp_code
, 0);
4660 /* VRP does not handle float types. */
4661 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)))
4664 /* Do not register always-false predicates.
4665 FIXME: this works around a limitation in fold() when dealing with
4666 enumerations. Given 'enum { N1, N2 } x;', fold will not
4667 fold 'if (x > N2)' to 'if (0)'. */
4668 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
4669 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
4671 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
4672 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
4674 if (comp_code
== GT_EXPR
4676 || compare_values (val
, max
) == 0))
4679 if (comp_code
== LT_EXPR
4681 || compare_values (val
, min
) == 0))
4684 *code_p
= comp_code
;
4689 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4690 (otherwise return VAL). VAL and MASK must be zero-extended for
4691 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4692 (to transform signed values into unsigned) and at the end xor
4696 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
4697 const wide_int
&sgnbit
, unsigned int prec
)
4699 wide_int bit
= wi::one (prec
), res
;
4702 wide_int val
= val_in
^ sgnbit
;
4703 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
4706 if ((res
& bit
) == 0)
4709 res
= (val
+ bit
).and_not (res
);
4711 if (wi::gtu_p (res
, val
))
4712 return res
^ sgnbit
;
4714 return val
^ sgnbit
;
4717 /* Try to register an edge assertion for SSA name NAME on edge E for
4718 the condition COND contributing to the conditional jump pointed to by BSI.
4719 Invert the condition COND if INVERT is true.
4720 Return true if an assertion for NAME could be registered. */
4723 register_edge_assert_for_2 (tree name
, edge e
, gimple_stmt_iterator bsi
,
4724 enum tree_code cond_code
,
4725 tree cond_op0
, tree cond_op1
, bool invert
)
4728 enum tree_code comp_code
;
4729 bool retval
= false;
4731 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
4734 invert
, &comp_code
, &val
))
4737 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4738 reachable from E. */
4739 if (live_on_edge (e
, name
)
4740 && !has_single_use (name
))
4742 register_new_assert_for (name
, name
, comp_code
, val
, NULL
, e
, bsi
);
4746 /* In the case of NAME <= CST and NAME being defined as
4747 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4748 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4749 This catches range and anti-range tests. */
4750 if ((comp_code
== LE_EXPR
4751 || comp_code
== GT_EXPR
)
4752 && TREE_CODE (val
) == INTEGER_CST
4753 && TYPE_UNSIGNED (TREE_TYPE (val
)))
4755 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
4756 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
4758 /* Extract CST2 from the (optional) addition. */
4759 if (is_gimple_assign (def_stmt
)
4760 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
4762 name2
= gimple_assign_rhs1 (def_stmt
);
4763 cst2
= gimple_assign_rhs2 (def_stmt
);
4764 if (TREE_CODE (name2
) == SSA_NAME
4765 && TREE_CODE (cst2
) == INTEGER_CST
)
4766 def_stmt
= SSA_NAME_DEF_STMT (name2
);
4769 /* Extract NAME2 from the (optional) sign-changing cast. */
4770 if (gimple_assign_cast_p (def_stmt
))
4772 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
4773 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
4774 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
4775 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
4776 name3
= gimple_assign_rhs1 (def_stmt
);
4779 /* If name3 is used later, create an ASSERT_EXPR for it. */
4780 if (name3
!= NULL_TREE
4781 && TREE_CODE (name3
) == SSA_NAME
4782 && (cst2
== NULL_TREE
4783 || TREE_CODE (cst2
) == INTEGER_CST
)
4784 && INTEGRAL_TYPE_P (TREE_TYPE (name3
))
4785 && live_on_edge (e
, name3
)
4786 && !has_single_use (name3
))
4790 /* Build an expression for the range test. */
4791 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
4792 if (cst2
!= NULL_TREE
)
4793 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4797 fprintf (dump_file
, "Adding assert for ");
4798 print_generic_expr (dump_file
, name3
, 0);
4799 fprintf (dump_file
, " from ");
4800 print_generic_expr (dump_file
, tmp
, 0);
4801 fprintf (dump_file
, "\n");
4804 register_new_assert_for (name3
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4809 /* If name2 is used later, create an ASSERT_EXPR for it. */
4810 if (name2
!= NULL_TREE
4811 && TREE_CODE (name2
) == SSA_NAME
4812 && TREE_CODE (cst2
) == INTEGER_CST
4813 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4814 && live_on_edge (e
, name2
)
4815 && !has_single_use (name2
))
4819 /* Build an expression for the range test. */
4821 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
4822 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
4823 if (cst2
!= NULL_TREE
)
4824 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4828 fprintf (dump_file
, "Adding assert for ");
4829 print_generic_expr (dump_file
, name2
, 0);
4830 fprintf (dump_file
, " from ");
4831 print_generic_expr (dump_file
, tmp
, 0);
4832 fprintf (dump_file
, "\n");
4835 register_new_assert_for (name2
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4841 /* In the case of post-in/decrement tests like if (i++) ... and uses
4842 of the in/decremented value on the edge the extra name we want to
4843 assert for is not on the def chain of the name compared. Instead
4844 it is in the set of use stmts. */
4845 if ((comp_code
== NE_EXPR
4846 || comp_code
== EQ_EXPR
)
4847 && TREE_CODE (val
) == INTEGER_CST
)
4849 imm_use_iterator ui
;
4851 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
4853 /* Cut off to use-stmts that are in the predecessor. */
4854 if (gimple_bb (use_stmt
) != e
->src
)
4857 if (!is_gimple_assign (use_stmt
))
4860 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
4861 if (code
!= PLUS_EXPR
4862 && code
!= MINUS_EXPR
)
4865 tree cst
= gimple_assign_rhs2 (use_stmt
);
4866 if (TREE_CODE (cst
) != INTEGER_CST
)
4869 tree name2
= gimple_assign_lhs (use_stmt
);
4870 if (live_on_edge (e
, name2
))
4872 cst
= int_const_binop (code
, val
, cst
);
4873 register_new_assert_for (name2
, name2
, comp_code
, cst
,
4880 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
4881 && TREE_CODE (val
) == INTEGER_CST
)
4883 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
4884 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
4885 tree val2
= NULL_TREE
;
4886 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
4887 wide_int mask
= wi::zero (prec
);
4888 unsigned int nprec
= prec
;
4889 enum tree_code rhs_code
= ERROR_MARK
;
4891 if (is_gimple_assign (def_stmt
))
4892 rhs_code
= gimple_assign_rhs_code (def_stmt
);
4894 /* Add asserts for NAME cmp CST and NAME being defined
4895 as NAME = (int) NAME2. */
4896 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
4897 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
4898 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
4899 && gimple_assign_cast_p (def_stmt
))
4901 name2
= gimple_assign_rhs1 (def_stmt
);
4902 if (CONVERT_EXPR_CODE_P (rhs_code
)
4903 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4904 && TYPE_UNSIGNED (TREE_TYPE (name2
))
4905 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
4906 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
4907 || !tree_int_cst_equal (val
,
4908 TYPE_MIN_VALUE (TREE_TYPE (val
))))
4909 && live_on_edge (e
, name2
)
4910 && !has_single_use (name2
))
4913 enum tree_code new_comp_code
= comp_code
;
4915 cst
= fold_convert (TREE_TYPE (name2
),
4916 TYPE_MIN_VALUE (TREE_TYPE (val
)));
4917 /* Build an expression for the range test. */
4918 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
4919 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
4920 fold_convert (TREE_TYPE (name2
), val
));
4921 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
4923 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
4924 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
4925 build_int_cst (TREE_TYPE (name2
), 1));
4930 fprintf (dump_file
, "Adding assert for ");
4931 print_generic_expr (dump_file
, name2
, 0);
4932 fprintf (dump_file
, " from ");
4933 print_generic_expr (dump_file
, tmp
, 0);
4934 fprintf (dump_file
, "\n");
4937 register_new_assert_for (name2
, tmp
, new_comp_code
, cst
, NULL
,
4944 /* Add asserts for NAME cmp CST and NAME being defined as
4945 NAME = NAME2 >> CST2.
4947 Extract CST2 from the right shift. */
4948 if (rhs_code
== RSHIFT_EXPR
)
4950 name2
= gimple_assign_rhs1 (def_stmt
);
4951 cst2
= gimple_assign_rhs2 (def_stmt
);
4952 if (TREE_CODE (name2
) == SSA_NAME
4953 && tree_fits_uhwi_p (cst2
)
4954 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4955 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
4956 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
)))
4957 && live_on_edge (e
, name2
)
4958 && !has_single_use (name2
))
4960 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
4961 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
4964 if (val2
!= NULL_TREE
4965 && TREE_CODE (val2
) == INTEGER_CST
4966 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
4970 enum tree_code new_comp_code
= comp_code
;
4974 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
4976 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
4978 tree type
= build_nonstandard_integer_type (prec
, 1);
4979 tmp
= build1 (NOP_EXPR
, type
, name2
);
4980 val2
= fold_convert (type
, val2
);
4982 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
4983 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
4984 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
4986 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
4989 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
4991 if (minval
== new_val
)
4992 new_val
= NULL_TREE
;
4997 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5000 new_val
= NULL_TREE
;
5002 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
5009 fprintf (dump_file
, "Adding assert for ");
5010 print_generic_expr (dump_file
, name2
, 0);
5011 fprintf (dump_file
, " from ");
5012 print_generic_expr (dump_file
, tmp
, 0);
5013 fprintf (dump_file
, "\n");
5016 register_new_assert_for (name2
, tmp
, new_comp_code
, new_val
,
5022 /* Add asserts for NAME cmp CST and NAME being defined as
5023 NAME = NAME2 & CST2.
5025 Extract CST2 from the and.
5028 NAME = (unsigned) NAME2;
5029 casts where NAME's type is unsigned and has smaller precision
5030 than NAME2's type as if it was NAME = NAME2 & MASK. */
5031 names
[0] = NULL_TREE
;
5032 names
[1] = NULL_TREE
;
5034 if (rhs_code
== BIT_AND_EXPR
5035 || (CONVERT_EXPR_CODE_P (rhs_code
)
5036 && TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
5037 && TYPE_UNSIGNED (TREE_TYPE (val
))
5038 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5042 name2
= gimple_assign_rhs1 (def_stmt
);
5043 if (rhs_code
== BIT_AND_EXPR
)
5044 cst2
= gimple_assign_rhs2 (def_stmt
);
5047 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5048 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
5050 if (TREE_CODE (name2
) == SSA_NAME
5051 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5052 && TREE_CODE (cst2
) == INTEGER_CST
5053 && !integer_zerop (cst2
)
5055 || TYPE_UNSIGNED (TREE_TYPE (val
))))
5057 gimple def_stmt2
= SSA_NAME_DEF_STMT (name2
);
5058 if (gimple_assign_cast_p (def_stmt2
))
5060 names
[1] = gimple_assign_rhs1 (def_stmt2
);
5061 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
5062 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
5063 || (TYPE_PRECISION (TREE_TYPE (name2
))
5064 != TYPE_PRECISION (TREE_TYPE (names
[1])))
5065 || !live_on_edge (e
, names
[1])
5066 || has_single_use (names
[1]))
5067 names
[1] = NULL_TREE
;
5069 if (live_on_edge (e
, name2
)
5070 && !has_single_use (name2
))
5074 if (names
[0] || names
[1])
5076 wide_int minv
, maxv
, valv
, cst2v
;
5077 wide_int tem
, sgnbit
;
5078 bool valid_p
= false, valn
, cst2n
;
5079 enum tree_code ccode
= comp_code
;
5081 valv
= wide_int::from (val
, nprec
, UNSIGNED
);
5082 cst2v
= wide_int::from (cst2
, nprec
, UNSIGNED
);
5083 valn
= wi::neg_p (valv
, TYPE_SIGN (TREE_TYPE (val
)));
5084 cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (TREE_TYPE (val
)));
5085 /* If CST2 doesn't have most significant bit set,
5086 but VAL is negative, we have comparison like
5087 if ((x & 0x123) > -4) (always true). Just give up. */
5091 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5093 sgnbit
= wi::zero (nprec
);
5094 minv
= valv
& cst2v
;
5098 /* Minimum unsigned value for equality is VAL & CST2
5099 (should be equal to VAL, otherwise we probably should
5100 have folded the comparison into false) and
5101 maximum unsigned value is VAL | ~CST2. */
5102 maxv
= valv
| ~cst2v
;
5107 tem
= valv
| ~cst2v
;
5108 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5112 sgnbit
= wi::zero (nprec
);
5115 /* If (VAL | ~CST2) is all ones, handle it as
5116 (X & CST2) < VAL. */
5121 sgnbit
= wi::zero (nprec
);
5124 if (!cst2n
&& wi::neg_p (cst2v
))
5125 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5134 if (tem
== wi::mask (nprec
- 1, false, nprec
))
5140 sgnbit
= wi::zero (nprec
);
5145 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5146 is VAL and maximum unsigned value is ~0. For signed
5147 comparison, if CST2 doesn't have most significant bit
5148 set, handle it similarly. If CST2 has MSB set,
5149 the minimum is the same, and maximum is ~0U/2. */
5152 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5154 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5158 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5164 /* Find out smallest MINV where MINV > VAL
5165 && (MINV & CST2) == MINV, if any. If VAL is signed and
5166 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5167 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5170 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5175 /* Minimum unsigned value for <= is 0 and maximum
5176 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5177 Otherwise, find smallest VAL2 where VAL2 > VAL
5178 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5180 For signed comparison, if CST2 doesn't have most
5181 significant bit set, handle it similarly. If CST2 has
5182 MSB set, the maximum is the same and minimum is INT_MIN. */
5187 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5199 /* Minimum unsigned value for < is 0 and maximum
5200 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5201 Otherwise, find smallest VAL2 where VAL2 > VAL
5202 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5204 For signed comparison, if CST2 doesn't have most
5205 significant bit set, handle it similarly. If CST2 has
5206 MSB set, the maximum is the same and minimum is INT_MIN. */
5215 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5229 && (maxv
- minv
) != -1)
5231 tree tmp
, new_val
, type
;
5234 for (i
= 0; i
< 2; i
++)
5237 wide_int maxv2
= maxv
;
5239 type
= TREE_TYPE (names
[i
]);
5240 if (!TYPE_UNSIGNED (type
))
5242 type
= build_nonstandard_integer_type (nprec
, 1);
5243 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5247 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5248 wide_int_to_tree (type
, -minv
));
5249 maxv2
= maxv
- minv
;
5251 new_val
= wide_int_to_tree (type
, maxv2
);
5255 fprintf (dump_file
, "Adding assert for ");
5256 print_generic_expr (dump_file
, names
[i
], 0);
5257 fprintf (dump_file
, " from ");
5258 print_generic_expr (dump_file
, tmp
, 0);
5259 fprintf (dump_file
, "\n");
5262 register_new_assert_for (names
[i
], tmp
, LE_EXPR
,
5263 new_val
, NULL
, e
, bsi
);
5273 /* OP is an operand of a truth value expression which is known to have
5274 a particular value. Register any asserts for OP and for any
5275 operands in OP's defining statement.
5277 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5278 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5281 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5282 edge e
, gimple_stmt_iterator bsi
)
5284 bool retval
= false;
5287 enum tree_code rhs_code
;
5289 /* We only care about SSA_NAMEs. */
5290 if (TREE_CODE (op
) != SSA_NAME
)
5293 /* We know that OP will have a zero or nonzero value. If OP is used
5294 more than once go ahead and register an assert for OP. */
5295 if (live_on_edge (e
, op
)
5296 && !has_single_use (op
))
5298 val
= build_int_cst (TREE_TYPE (op
), 0);
5299 register_new_assert_for (op
, op
, code
, val
, NULL
, e
, bsi
);
5303 /* Now look at how OP is set. If it's set from a comparison,
5304 a truth operation or some bit operations, then we may be able
5305 to register information about the operands of that assignment. */
5306 op_def
= SSA_NAME_DEF_STMT (op
);
5307 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5310 rhs_code
= gimple_assign_rhs_code (op_def
);
5312 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5314 bool invert
= (code
== EQ_EXPR
? true : false);
5315 tree op0
= gimple_assign_rhs1 (op_def
);
5316 tree op1
= gimple_assign_rhs2 (op_def
);
5318 if (TREE_CODE (op0
) == SSA_NAME
)
5319 retval
|= register_edge_assert_for_2 (op0
, e
, bsi
, rhs_code
, op0
, op1
,
5321 if (TREE_CODE (op1
) == SSA_NAME
)
5322 retval
|= register_edge_assert_for_2 (op1
, e
, bsi
, rhs_code
, op0
, op1
,
5325 else if ((code
== NE_EXPR
5326 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5328 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5330 /* Recurse on each operand. */
5331 tree op0
= gimple_assign_rhs1 (op_def
);
5332 tree op1
= gimple_assign_rhs2 (op_def
);
5333 if (TREE_CODE (op0
) == SSA_NAME
5334 && has_single_use (op0
))
5335 retval
|= register_edge_assert_for_1 (op0
, code
, e
, bsi
);
5336 if (TREE_CODE (op1
) == SSA_NAME
5337 && has_single_use (op1
))
5338 retval
|= register_edge_assert_for_1 (op1
, code
, e
, bsi
);
5340 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5341 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5343 /* Recurse, flipping CODE. */
5344 code
= invert_tree_comparison (code
, false);
5345 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5348 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5350 /* Recurse through the copy. */
5351 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5354 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5356 /* Recurse through the type conversion, unless it is a narrowing
5357 conversion or conversion from non-integral type. */
5358 tree rhs
= gimple_assign_rhs1 (op_def
);
5359 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
5360 && (TYPE_PRECISION (TREE_TYPE (rhs
))
5361 <= TYPE_PRECISION (TREE_TYPE (op
))))
5362 retval
|= register_edge_assert_for_1 (rhs
, code
, e
, bsi
);
5368 /* Try to register an edge assertion for SSA name NAME on edge E for
5369 the condition COND contributing to the conditional jump pointed to by SI.
5370 Return true if an assertion for NAME could be registered. */
5373 register_edge_assert_for (tree name
, edge e
, gimple_stmt_iterator si
,
5374 enum tree_code cond_code
, tree cond_op0
,
5378 enum tree_code comp_code
;
5379 bool retval
= false;
5380 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5382 /* Do not attempt to infer anything in names that flow through
5384 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5387 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5393 /* Register ASSERT_EXPRs for name. */
5394 retval
|= register_edge_assert_for_2 (name
, e
, si
, cond_code
, cond_op0
,
5395 cond_op1
, is_else_edge
);
5398 /* If COND is effectively an equality test of an SSA_NAME against
5399 the value zero or one, then we may be able to assert values
5400 for SSA_NAMEs which flow into COND. */
5402 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5403 statement of NAME we can assert both operands of the BIT_AND_EXPR
5404 have nonzero value. */
5405 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5406 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5408 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5410 if (is_gimple_assign (def_stmt
)
5411 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5413 tree op0
= gimple_assign_rhs1 (def_stmt
);
5414 tree op1
= gimple_assign_rhs2 (def_stmt
);
5415 retval
|= register_edge_assert_for_1 (op0
, NE_EXPR
, e
, si
);
5416 retval
|= register_edge_assert_for_1 (op1
, NE_EXPR
, e
, si
);
5420 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5421 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5423 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5424 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5426 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5428 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5429 necessarily zero value, or if type-precision is one. */
5430 if (is_gimple_assign (def_stmt
)
5431 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5432 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5433 || comp_code
== EQ_EXPR
)))
5435 tree op0
= gimple_assign_rhs1 (def_stmt
);
5436 tree op1
= gimple_assign_rhs2 (def_stmt
);
5437 retval
|= register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, si
);
5438 retval
|= register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, si
);
5446 /* Determine whether the outgoing edges of BB should receive an
5447 ASSERT_EXPR for each of the operands of BB's LAST statement.
5448 The last statement of BB must be a COND_EXPR.
5450 If any of the sub-graphs rooted at BB have an interesting use of
5451 the predicate operands, an assert location node is added to the
5452 list of assertions for the corresponding operands. */
5455 find_conditional_asserts (basic_block bb
, gimple last
)
5458 gimple_stmt_iterator bsi
;
5464 need_assert
= false;
5465 bsi
= gsi_for_stmt (last
);
5467 /* Look for uses of the operands in each of the sub-graphs
5468 rooted at BB. We need to check each of the outgoing edges
5469 separately, so that we know what kind of ASSERT_EXPR to
5471 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5476 /* Register the necessary assertions for each operand in the
5477 conditional predicate. */
5478 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5480 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5481 gimple_cond_code (last
),
5482 gimple_cond_lhs (last
),
5483 gimple_cond_rhs (last
));
5496 /* Compare two case labels sorting first by the destination bb index
5497 and then by the case value. */
5500 compare_case_labels (const void *p1
, const void *p2
)
5502 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5503 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5504 int idx1
= ci1
->bb
->index
;
5505 int idx2
= ci2
->bb
->index
;
5509 else if (idx1
== idx2
)
5511 /* Make sure the default label is first in a group. */
5512 if (!CASE_LOW (ci1
->expr
))
5514 else if (!CASE_LOW (ci2
->expr
))
5517 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5518 CASE_LOW (ci2
->expr
));
5524 /* Determine whether the outgoing edges of BB should receive an
5525 ASSERT_EXPR for each of the operands of BB's LAST statement.
5526 The last statement of BB must be a SWITCH_EXPR.
5528 If any of the sub-graphs rooted at BB have an interesting use of
5529 the predicate operands, an assert location node is added to the
5530 list of assertions for the corresponding operands. */
5533 find_switch_asserts (basic_block bb
, gimple last
)
5536 gimple_stmt_iterator bsi
;
5539 struct case_info
*ci
;
5540 size_t n
= gimple_switch_num_labels (last
);
5541 #if GCC_VERSION >= 4000
5544 /* Work around GCC 3.4 bug (PR 37086). */
5545 volatile unsigned int idx
;
5548 need_assert
= false;
5549 bsi
= gsi_for_stmt (last
);
5550 op
= gimple_switch_index (last
);
5551 if (TREE_CODE (op
) != SSA_NAME
)
5554 /* Build a vector of case labels sorted by destination label. */
5555 ci
= XNEWVEC (struct case_info
, n
);
5556 for (idx
= 0; idx
< n
; ++idx
)
5558 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5559 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5561 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5563 for (idx
= 0; idx
< n
; ++idx
)
5566 tree cl
= ci
[idx
].expr
;
5567 basic_block cbb
= ci
[idx
].bb
;
5569 min
= CASE_LOW (cl
);
5570 max
= CASE_HIGH (cl
);
5572 /* If there are multiple case labels with the same destination
5573 we need to combine them to a single value range for the edge. */
5574 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
5576 /* Skip labels until the last of the group. */
5579 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
5582 /* Pick up the maximum of the case label range. */
5583 if (CASE_HIGH (ci
[idx
].expr
))
5584 max
= CASE_HIGH (ci
[idx
].expr
);
5586 max
= CASE_LOW (ci
[idx
].expr
);
5589 /* Nothing to do if the range includes the default label until we
5590 can register anti-ranges. */
5591 if (min
== NULL_TREE
)
5594 /* Find the edge to register the assert expr on. */
5595 e
= find_edge (bb
, cbb
);
5597 /* Register the necessary assertions for the operand in the
5599 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5600 max
? GE_EXPR
: EQ_EXPR
,
5602 fold_convert (TREE_TYPE (op
),
5606 need_assert
|= register_edge_assert_for (op
, e
, bsi
, LE_EXPR
,
5608 fold_convert (TREE_TYPE (op
),
5618 /* Traverse all the statements in block BB looking for statements that
5619 may generate useful assertions for the SSA names in their operand.
5620 If a statement produces a useful assertion A for name N_i, then the
5621 list of assertions already generated for N_i is scanned to
5622 determine if A is actually needed.
5624 If N_i already had the assertion A at a location dominating the
5625 current location, then nothing needs to be done. Otherwise, the
5626 new location for A is recorded instead.
5628 1- For every statement S in BB, all the variables used by S are
5629 added to bitmap FOUND_IN_SUBGRAPH.
5631 2- If statement S uses an operand N in a way that exposes a known
5632 value range for N, then if N was not already generated by an
5633 ASSERT_EXPR, create a new assert location for N. For instance,
5634 if N is a pointer and the statement dereferences it, we can
5635 assume that N is not NULL.
5637 3- COND_EXPRs are a special case of #2. We can derive range
5638 information from the predicate but need to insert different
5639 ASSERT_EXPRs for each of the sub-graphs rooted at the
5640 conditional block. If the last statement of BB is a conditional
5641 expression of the form 'X op Y', then
5643 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5645 b) If the conditional is the only entry point to the sub-graph
5646 corresponding to the THEN_CLAUSE, recurse into it. On
5647 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5648 an ASSERT_EXPR is added for the corresponding variable.
5650 c) Repeat step (b) on the ELSE_CLAUSE.
5652 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5661 In this case, an assertion on the THEN clause is useful to
5662 determine that 'a' is always 9 on that edge. However, an assertion
5663 on the ELSE clause would be unnecessary.
5665 4- If BB does not end in a conditional expression, then we recurse
5666 into BB's dominator children.
5668 At the end of the recursive traversal, every SSA name will have a
5669 list of locations where ASSERT_EXPRs should be added. When a new
5670 location for name N is found, it is registered by calling
5671 register_new_assert_for. That function keeps track of all the
5672 registered assertions to prevent adding unnecessary assertions.
5673 For instance, if a pointer P_4 is dereferenced more than once in a
5674 dominator tree, only the location dominating all the dereference of
5675 P_4 will receive an ASSERT_EXPR.
5677 If this function returns true, then it means that there are names
5678 for which we need to generate ASSERT_EXPRs. Those assertions are
5679 inserted by process_assert_insertions. */
5682 find_assert_locations_1 (basic_block bb
, sbitmap live
)
5684 gimple_stmt_iterator si
;
5688 need_assert
= false;
5689 last
= last_stmt (bb
);
5691 /* If BB's last statement is a conditional statement involving integer
5692 operands, determine if we need to add ASSERT_EXPRs. */
5694 && gimple_code (last
) == GIMPLE_COND
5695 && !fp_predicate (last
)
5696 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5697 need_assert
|= find_conditional_asserts (bb
, last
);
5699 /* If BB's last statement is a switch statement involving integer
5700 operands, determine if we need to add ASSERT_EXPRs. */
5702 && gimple_code (last
) == GIMPLE_SWITCH
5703 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5704 need_assert
|= find_switch_asserts (bb
, last
);
5706 /* Traverse all the statements in BB marking used names and looking
5707 for statements that may infer assertions for their used operands. */
5708 for (si
= gsi_last_bb (bb
); !gsi_end_p (si
); gsi_prev (&si
))
5714 stmt
= gsi_stmt (si
);
5716 if (is_gimple_debug (stmt
))
5719 /* See if we can derive an assertion for any of STMT's operands. */
5720 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5723 enum tree_code comp_code
;
5725 /* If op is not live beyond this stmt, do not bother to insert
5727 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
5730 /* If OP is used in such a way that we can infer a value
5731 range for it, and we don't find a previous assertion for
5732 it, create a new assertion location node for OP. */
5733 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
5735 /* If we are able to infer a nonzero value range for OP,
5736 then walk backwards through the use-def chain to see if OP
5737 was set via a typecast.
5739 If so, then we can also infer a nonzero value range
5740 for the operand of the NOP_EXPR. */
5741 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
5744 gimple def_stmt
= SSA_NAME_DEF_STMT (t
);
5746 while (is_gimple_assign (def_stmt
)
5747 && gimple_assign_rhs_code (def_stmt
) == NOP_EXPR
5749 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
5751 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
5753 t
= gimple_assign_rhs1 (def_stmt
);
5754 def_stmt
= SSA_NAME_DEF_STMT (t
);
5756 /* Note we want to register the assert for the
5757 operand of the NOP_EXPR after SI, not after the
5759 if (! has_single_use (t
))
5761 register_new_assert_for (t
, t
, comp_code
, value
,
5768 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
5774 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5775 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
5776 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
5777 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
5780 /* Traverse all PHI nodes in BB, updating live. */
5781 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
5783 use_operand_p arg_p
;
5785 gimple phi
= gsi_stmt (si
);
5786 tree res
= gimple_phi_result (phi
);
5788 if (virtual_operand_p (res
))
5791 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
5793 tree arg
= USE_FROM_PTR (arg_p
);
5794 if (TREE_CODE (arg
) == SSA_NAME
)
5795 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
5798 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
5804 /* Do an RPO walk over the function computing SSA name liveness
5805 on-the-fly and deciding on assert expressions to insert.
5806 Returns true if there are assert expressions to be inserted. */
5809 find_assert_locations (void)
5811 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
5812 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
5813 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
5817 live
= XCNEWVEC (sbitmap
, last_basic_block_for_fn (cfun
));
5818 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
5819 for (i
= 0; i
< rpo_cnt
; ++i
)
5822 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
5823 the order we compute liveness and insert asserts we otherwise
5824 fail to insert asserts into the loop latch. */
5826 FOR_EACH_LOOP (loop
, 0)
5828 i
= loop
->latch
->index
;
5829 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
5830 for (gimple_stmt_iterator gsi
= gsi_start_phis (loop
->header
);
5831 !gsi_end_p (gsi
); gsi_next (&gsi
))
5833 gimple phi
= gsi_stmt (gsi
);
5834 if (virtual_operand_p (gimple_phi_result (phi
)))
5836 tree arg
= gimple_phi_arg_def (phi
, j
);
5837 if (TREE_CODE (arg
) == SSA_NAME
)
5839 if (live
[i
] == NULL
)
5841 live
[i
] = sbitmap_alloc (num_ssa_names
);
5842 bitmap_clear (live
[i
]);
5844 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
5849 need_asserts
= false;
5850 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
5852 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
5858 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
5859 bitmap_clear (live
[rpo
[i
]]);
5862 /* Process BB and update the live information with uses in
5864 need_asserts
|= find_assert_locations_1 (bb
, live
[rpo
[i
]]);
5866 /* Merge liveness into the predecessor blocks and free it. */
5867 if (!bitmap_empty_p (live
[rpo
[i
]]))
5870 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
5872 int pred
= e
->src
->index
;
5873 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
5878 live
[pred
] = sbitmap_alloc (num_ssa_names
);
5879 bitmap_clear (live
[pred
]);
5881 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
5883 if (bb_rpo
[pred
] < pred_rpo
)
5884 pred_rpo
= bb_rpo
[pred
];
5887 /* Record the RPO number of the last visited block that needs
5888 live information from this block. */
5889 last_rpo
[rpo
[i
]] = pred_rpo
;
5893 sbitmap_free (live
[rpo
[i
]]);
5894 live
[rpo
[i
]] = NULL
;
5897 /* We can free all successors live bitmaps if all their
5898 predecessors have been visited already. */
5899 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5900 if (last_rpo
[e
->dest
->index
] == i
5901 && live
[e
->dest
->index
])
5903 sbitmap_free (live
[e
->dest
->index
]);
5904 live
[e
->dest
->index
] = NULL
;
5909 XDELETEVEC (bb_rpo
);
5910 XDELETEVEC (last_rpo
);
5911 for (i
= 0; i
< last_basic_block_for_fn (cfun
); ++i
)
5913 sbitmap_free (live
[i
]);
5916 return need_asserts
;
5919 /* Create an ASSERT_EXPR for NAME and insert it in the location
5920 indicated by LOC. Return true if we made any edge insertions. */
5923 process_assert_insertions_for (tree name
, assert_locus_t loc
)
5925 /* Build the comparison expression NAME_i COMP_CODE VAL. */
5932 /* If we have X <=> X do not insert an assert expr for that. */
5933 if (loc
->expr
== loc
->val
)
5936 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
5937 assert_stmt
= build_assert_expr_for (cond
, name
);
5940 /* We have been asked to insert the assertion on an edge. This
5941 is used only by COND_EXPR and SWITCH_EXPR assertions. */
5942 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
5943 || (gimple_code (gsi_stmt (loc
->si
))
5946 gsi_insert_on_edge (loc
->e
, assert_stmt
);
5950 /* Otherwise, we can insert right after LOC->SI iff the
5951 statement must not be the last statement in the block. */
5952 stmt
= gsi_stmt (loc
->si
);
5953 if (!stmt_ends_bb_p (stmt
))
5955 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
5959 /* If STMT must be the last statement in BB, we can only insert new
5960 assertions on the non-abnormal edge out of BB. Note that since
5961 STMT is not control flow, there may only be one non-abnormal edge
5963 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
5964 if (!(e
->flags
& EDGE_ABNORMAL
))
5966 gsi_insert_on_edge (e
, assert_stmt
);
5974 /* Process all the insertions registered for every name N_i registered
5975 in NEED_ASSERT_FOR. The list of assertions to be inserted are
5976 found in ASSERTS_FOR[i]. */
5979 process_assert_insertions (void)
5983 bool update_edges_p
= false;
5984 int num_asserts
= 0;
5986 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5987 dump_all_asserts (dump_file
);
5989 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
5991 assert_locus_t loc
= asserts_for
[i
];
5996 assert_locus_t next
= loc
->next
;
5997 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
6005 gsi_commit_edge_inserts ();
6007 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
6012 /* Traverse the flowgraph looking for conditional jumps to insert range
6013 expressions. These range expressions are meant to provide information
6014 to optimizations that need to reason in terms of value ranges. They
6015 will not be expanded into RTL. For instance, given:
6024 this pass will transform the code into:
6030 x = ASSERT_EXPR <x, x < y>
6035 y = ASSERT_EXPR <y, x >= y>
6039 The idea is that once copy and constant propagation have run, other
6040 optimizations will be able to determine what ranges of values can 'x'
6041 take in different paths of the code, simply by checking the reaching
6042 definition of 'x'. */
6045 insert_range_assertions (void)
6047 need_assert_for
= BITMAP_ALLOC (NULL
);
6048 asserts_for
= XCNEWVEC (assert_locus_t
, num_ssa_names
);
6050 calculate_dominance_info (CDI_DOMINATORS
);
6052 if (find_assert_locations ())
6054 process_assert_insertions ();
6055 update_ssa (TODO_update_ssa_no_phi
);
6058 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6060 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
6061 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
6065 BITMAP_FREE (need_assert_for
);
6068 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6069 and "struct" hacks. If VRP can determine that the
6070 array subscript is a constant, check if it is outside valid
6071 range. If the array subscript is a RANGE, warn if it is
6072 non-overlapping with valid range.
6073 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6076 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
6078 value_range_t
* vr
= NULL
;
6079 tree low_sub
, up_sub
;
6080 tree low_bound
, up_bound
, up_bound_p1
;
6083 if (TREE_NO_WARNING (ref
))
6086 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
6087 up_bound
= array_ref_up_bound (ref
);
6089 /* Can not check flexible arrays. */
6091 || TREE_CODE (up_bound
) != INTEGER_CST
)
6094 /* Accesses to trailing arrays via pointers may access storage
6095 beyond the types array bounds. */
6096 base
= get_base_address (ref
);
6097 if (base
&& TREE_CODE (base
) == MEM_REF
)
6099 tree cref
, next
= NULL_TREE
;
6101 if (TREE_CODE (TREE_OPERAND (ref
, 0)) != COMPONENT_REF
)
6104 cref
= TREE_OPERAND (ref
, 0);
6105 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref
, 0))) == RECORD_TYPE
)
6106 for (next
= DECL_CHAIN (TREE_OPERAND (cref
, 1));
6107 next
&& TREE_CODE (next
) != FIELD_DECL
;
6108 next
= DECL_CHAIN (next
))
6111 /* If this is the last field in a struct type or a field in a
6112 union type do not warn. */
6117 low_bound
= array_ref_low_bound (ref
);
6118 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
6119 build_int_cst (TREE_TYPE (up_bound
), 1));
6121 if (TREE_CODE (low_sub
) == SSA_NAME
)
6123 vr
= get_value_range (low_sub
);
6124 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
6126 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
6127 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
6131 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
6133 if (TREE_CODE (up_sub
) == INTEGER_CST
6134 && tree_int_cst_lt (up_bound
, up_sub
)
6135 && TREE_CODE (low_sub
) == INTEGER_CST
6136 && tree_int_cst_lt (low_sub
, low_bound
))
6138 warning_at (location
, OPT_Warray_bounds
,
6139 "array subscript is outside array bounds");
6140 TREE_NO_WARNING (ref
) = 1;
6143 else if (TREE_CODE (up_sub
) == INTEGER_CST
6144 && (ignore_off_by_one
6145 ? (tree_int_cst_lt (up_bound
, up_sub
)
6146 && !tree_int_cst_equal (up_bound_p1
, up_sub
))
6147 : (tree_int_cst_lt (up_bound
, up_sub
)
6148 || tree_int_cst_equal (up_bound_p1
, up_sub
))))
6150 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6152 fprintf (dump_file
, "Array bound warning for ");
6153 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6154 fprintf (dump_file
, "\n");
6156 warning_at (location
, OPT_Warray_bounds
,
6157 "array subscript is above array bounds");
6158 TREE_NO_WARNING (ref
) = 1;
6160 else if (TREE_CODE (low_sub
) == INTEGER_CST
6161 && tree_int_cst_lt (low_sub
, low_bound
))
6163 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6165 fprintf (dump_file
, "Array bound warning for ");
6166 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6167 fprintf (dump_file
, "\n");
6169 warning_at (location
, OPT_Warray_bounds
,
6170 "array subscript is below array bounds");
6171 TREE_NO_WARNING (ref
) = 1;
6175 /* Searches if the expr T, located at LOCATION computes
6176 address of an ARRAY_REF, and call check_array_ref on it. */
6179 search_for_addr_array (tree t
, location_t location
)
6181 while (TREE_CODE (t
) == SSA_NAME
)
6183 gimple g
= SSA_NAME_DEF_STMT (t
);
6185 if (gimple_code (g
) != GIMPLE_ASSIGN
)
6188 if (get_gimple_rhs_class (gimple_assign_rhs_code (g
))
6189 != GIMPLE_SINGLE_RHS
)
6192 t
= gimple_assign_rhs1 (g
);
6196 /* We are only interested in addresses of ARRAY_REF's. */
6197 if (TREE_CODE (t
) != ADDR_EXPR
)
6200 /* Check each ARRAY_REFs in the reference chain. */
6203 if (TREE_CODE (t
) == ARRAY_REF
)
6204 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
6206 t
= TREE_OPERAND (t
, 0);
6208 while (handled_component_p (t
));
6210 if (TREE_CODE (t
) == MEM_REF
6211 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
6212 && !TREE_NO_WARNING (t
))
6214 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
6215 tree low_bound
, up_bound
, el_sz
;
6217 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
6218 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6219 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6222 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6223 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6224 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6226 || TREE_CODE (low_bound
) != INTEGER_CST
6228 || TREE_CODE (up_bound
) != INTEGER_CST
6230 || TREE_CODE (el_sz
) != INTEGER_CST
)
6233 idx
= mem_ref_offset (t
);
6234 idx
= wi::sdiv_trunc (idx
, wi::to_offset (el_sz
));
6235 if (wi::lts_p (idx
, 0))
6237 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6239 fprintf (dump_file
, "Array bound warning for ");
6240 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6241 fprintf (dump_file
, "\n");
6243 warning_at (location
, OPT_Warray_bounds
,
6244 "array subscript is below array bounds");
6245 TREE_NO_WARNING (t
) = 1;
6247 else if (wi::gts_p (idx
, (wi::to_offset (up_bound
)
6248 - wi::to_offset (low_bound
) + 1)))
6250 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6252 fprintf (dump_file
, "Array bound warning for ");
6253 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6254 fprintf (dump_file
, "\n");
6256 warning_at (location
, OPT_Warray_bounds
,
6257 "array subscript is above array bounds");
6258 TREE_NO_WARNING (t
) = 1;
6263 /* walk_tree() callback that checks if *TP is
6264 an ARRAY_REF inside an ADDR_EXPR (in which an array
6265 subscript one outside the valid range is allowed). Call
6266 check_array_ref for each ARRAY_REF found. The location is
6270 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6273 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6274 location_t location
;
6276 if (EXPR_HAS_LOCATION (t
))
6277 location
= EXPR_LOCATION (t
);
6280 location_t
*locp
= (location_t
*) wi
->info
;
6284 *walk_subtree
= TRUE
;
6286 if (TREE_CODE (t
) == ARRAY_REF
)
6287 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6289 if (TREE_CODE (t
) == MEM_REF
6290 || (TREE_CODE (t
) == RETURN_EXPR
&& TREE_OPERAND (t
, 0)))
6291 search_for_addr_array (TREE_OPERAND (t
, 0), location
);
6293 if (TREE_CODE (t
) == ADDR_EXPR
)
6294 *walk_subtree
= FALSE
;
6299 /* Walk over all statements of all reachable BBs and call check_array_bounds
6303 check_all_array_refs (void)
6306 gimple_stmt_iterator si
;
6308 FOR_EACH_BB_FN (bb
, cfun
)
6312 bool executable
= false;
6314 /* Skip blocks that were found to be unreachable. */
6315 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6316 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6320 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6322 gimple stmt
= gsi_stmt (si
);
6323 struct walk_stmt_info wi
;
6324 if (!gimple_has_location (stmt
))
6327 if (is_gimple_call (stmt
))
6330 size_t n
= gimple_call_num_args (stmt
);
6331 for (i
= 0; i
< n
; i
++)
6333 tree arg
= gimple_call_arg (stmt
, i
);
6334 search_for_addr_array (arg
, gimple_location (stmt
));
6339 memset (&wi
, 0, sizeof (wi
));
6340 wi
.info
= CONST_CAST (void *, (const void *)
6341 gimple_location_ptr (stmt
));
6343 walk_gimple_op (gsi_stmt (si
),
6351 /* Return true if all imm uses of VAR are either in STMT, or
6352 feed (optionally through a chain of single imm uses) GIMPLE_COND
6353 in basic block COND_BB. */
6356 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple stmt
, basic_block cond_bb
)
6358 use_operand_p use_p
, use2_p
;
6359 imm_use_iterator iter
;
6361 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
6362 if (USE_STMT (use_p
) != stmt
)
6364 gimple use_stmt
= USE_STMT (use_p
), use_stmt2
;
6365 if (is_gimple_debug (use_stmt
))
6367 while (is_gimple_assign (use_stmt
)
6368 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
6369 && single_imm_use (gimple_assign_lhs (use_stmt
),
6370 &use2_p
, &use_stmt2
))
6371 use_stmt
= use_stmt2
;
6372 if (gimple_code (use_stmt
) != GIMPLE_COND
6373 || gimple_bb (use_stmt
) != cond_bb
)
6386 __builtin_unreachable ();
6388 x_5 = ASSERT_EXPR <x_3, ...>;
6389 If x_3 has no other immediate uses (checked by caller),
6390 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6391 from the non-zero bitmask. */
6394 maybe_set_nonzero_bits (basic_block bb
, tree var
)
6396 edge e
= single_pred_edge (bb
);
6397 basic_block cond_bb
= e
->src
;
6398 gimple stmt
= last_stmt (cond_bb
);
6402 || gimple_code (stmt
) != GIMPLE_COND
6403 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
6404 ? EQ_EXPR
: NE_EXPR
)
6405 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
6406 || !integer_zerop (gimple_cond_rhs (stmt
)))
6409 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
6410 if (!is_gimple_assign (stmt
)
6411 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6412 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
6414 if (gimple_assign_rhs1 (stmt
) != var
)
6418 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
6420 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
6421 if (!gimple_assign_cast_p (stmt2
)
6422 || gimple_assign_rhs1 (stmt2
) != var
6423 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
6424 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
6425 != TYPE_PRECISION (TREE_TYPE (var
))))
6428 cst
= gimple_assign_rhs2 (stmt
);
6429 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
), cst
));
6432 /* Convert range assertion expressions into the implied copies and
6433 copy propagate away the copies. Doing the trivial copy propagation
6434 here avoids the need to run the full copy propagation pass after
6437 FIXME, this will eventually lead to copy propagation removing the
6438 names that had useful range information attached to them. For
6439 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6440 then N_i will have the range [3, +INF].
6442 However, by converting the assertion into the implied copy
6443 operation N_i = N_j, we will then copy-propagate N_j into the uses
6444 of N_i and lose the range information. We may want to hold on to
6445 ASSERT_EXPRs a little while longer as the ranges could be used in
6446 things like jump threading.
6448 The problem with keeping ASSERT_EXPRs around is that passes after
6449 VRP need to handle them appropriately.
6451 Another approach would be to make the range information a first
6452 class property of the SSA_NAME so that it can be queried from
6453 any pass. This is made somewhat more complex by the need for
6454 multiple ranges to be associated with one SSA_NAME. */
6457 remove_range_assertions (void)
6460 gimple_stmt_iterator si
;
6461 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6462 a basic block preceeded by GIMPLE_COND branching to it and
6463 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6466 /* Note that the BSI iterator bump happens at the bottom of the
6467 loop and no bump is necessary if we're removing the statement
6468 referenced by the current BSI. */
6469 FOR_EACH_BB_FN (bb
, cfun
)
6470 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
6472 gimple stmt
= gsi_stmt (si
);
6475 if (is_gimple_assign (stmt
)
6476 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6478 tree lhs
= gimple_assign_lhs (stmt
);
6479 tree rhs
= gimple_assign_rhs1 (stmt
);
6481 tree cond
= fold (ASSERT_EXPR_COND (rhs
));
6482 use_operand_p use_p
;
6483 imm_use_iterator iter
;
6485 gcc_assert (cond
!= boolean_false_node
);
6487 var
= ASSERT_EXPR_VAR (rhs
);
6488 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
6490 if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
6491 && SSA_NAME_RANGE_INFO (lhs
))
6493 if (is_unreachable
== -1)
6496 if (single_pred_p (bb
)
6497 && assert_unreachable_fallthru_edge_p
6498 (single_pred_edge (bb
)))
6502 if (x_7 >= 10 && x_7 < 20)
6503 __builtin_unreachable ();
6504 x_8 = ASSERT_EXPR <x_7, ...>;
6505 if the only uses of x_7 are in the ASSERT_EXPR and
6506 in the condition. In that case, we can copy the
6507 range info from x_8 computed in this pass also
6510 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
6513 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
6514 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
6515 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
6516 maybe_set_nonzero_bits (bb
, var
);
6520 /* Propagate the RHS into every use of the LHS. */
6521 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
6522 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
6523 SET_USE (use_p
, var
);
6525 /* And finally, remove the copy, it is not needed. */
6526 gsi_remove (&si
, true);
6527 release_defs (stmt
);
6538 /* Return true if STMT is interesting for VRP. */
6541 stmt_interesting_for_vrp (gimple stmt
)
6543 if (gimple_code (stmt
) == GIMPLE_PHI
)
6545 tree res
= gimple_phi_result (stmt
);
6546 return (!virtual_operand_p (res
)
6547 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
6548 || POINTER_TYPE_P (TREE_TYPE (res
))));
6550 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6552 tree lhs
= gimple_get_lhs (stmt
);
6554 /* In general, assignments with virtual operands are not useful
6555 for deriving ranges, with the obvious exception of calls to
6556 builtin functions. */
6557 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6558 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6559 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6560 && (is_gimple_call (stmt
)
6561 || !gimple_vuse (stmt
)))
6564 else if (gimple_code (stmt
) == GIMPLE_COND
6565 || gimple_code (stmt
) == GIMPLE_SWITCH
)
6572 /* Initialize local data structures for VRP. */
6575 vrp_initialize (void)
6579 values_propagated
= false;
6580 num_vr_values
= num_ssa_names
;
6581 vr_value
= XCNEWVEC (value_range_t
*, num_vr_values
);
6582 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
6584 FOR_EACH_BB_FN (bb
, cfun
)
6586 gimple_stmt_iterator si
;
6588 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
6590 gimple phi
= gsi_stmt (si
);
6591 if (!stmt_interesting_for_vrp (phi
))
6593 tree lhs
= PHI_RESULT (phi
);
6594 set_value_range_to_varying (get_value_range (lhs
));
6595 prop_set_simulate_again (phi
, false);
6598 prop_set_simulate_again (phi
, true);
6601 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6603 gimple stmt
= gsi_stmt (si
);
6605 /* If the statement is a control insn, then we do not
6606 want to avoid simulating the statement once. Failure
6607 to do so means that those edges will never get added. */
6608 if (stmt_ends_bb_p (stmt
))
6609 prop_set_simulate_again (stmt
, true);
6610 else if (!stmt_interesting_for_vrp (stmt
))
6614 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
6615 set_value_range_to_varying (get_value_range (def
));
6616 prop_set_simulate_again (stmt
, false);
6619 prop_set_simulate_again (stmt
, true);
6624 /* Return the singleton value-range for NAME or NAME. */
6627 vrp_valueize (tree name
)
6629 if (TREE_CODE (name
) == SSA_NAME
)
6631 value_range_t
*vr
= get_value_range (name
);
6632 if (vr
->type
== VR_RANGE
6633 && (vr
->min
== vr
->max
6634 || operand_equal_p (vr
->min
, vr
->max
, 0)))
6640 /* Visit assignment STMT. If it produces an interesting range, record
6641 the SSA name in *OUTPUT_P. */
6643 static enum ssa_prop_result
6644 vrp_visit_assignment_or_call (gimple stmt
, tree
*output_p
)
6648 enum gimple_code code
= gimple_code (stmt
);
6649 lhs
= gimple_get_lhs (stmt
);
6651 /* We only keep track of ranges in integral and pointer types. */
6652 if (TREE_CODE (lhs
) == SSA_NAME
6653 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6654 /* It is valid to have NULL MIN/MAX values on a type. See
6655 build_range_type. */
6656 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
6657 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
6658 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
6660 value_range_t new_vr
= VR_INITIALIZER
;
6662 /* Try folding the statement to a constant first. */
6663 tree tem
= gimple_fold_stmt_to_constant (stmt
, vrp_valueize
);
6665 set_value_range_to_value (&new_vr
, tem
, NULL
);
6666 /* Then dispatch to value-range extracting functions. */
6667 else if (code
== GIMPLE_CALL
)
6668 extract_range_basic (&new_vr
, stmt
);
6670 extract_range_from_assignment (&new_vr
, stmt
);
6672 if (update_value_range (lhs
, &new_vr
))
6676 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6678 fprintf (dump_file
, "Found new range for ");
6679 print_generic_expr (dump_file
, lhs
, 0);
6680 fprintf (dump_file
, ": ");
6681 dump_value_range (dump_file
, &new_vr
);
6682 fprintf (dump_file
, "\n");
6685 if (new_vr
.type
== VR_VARYING
)
6686 return SSA_PROP_VARYING
;
6688 return SSA_PROP_INTERESTING
;
6691 return SSA_PROP_NOT_INTERESTING
;
6694 /* Every other statement produces no useful ranges. */
6695 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
6696 set_value_range_to_varying (get_value_range (def
));
6698 return SSA_PROP_VARYING
;
6701 /* Helper that gets the value range of the SSA_NAME with version I
6702 or a symbolic range containing the SSA_NAME only if the value range
6703 is varying or undefined. */
6705 static inline value_range_t
6706 get_vr_for_comparison (int i
)
6708 value_range_t vr
= *get_value_range (ssa_name (i
));
6710 /* If name N_i does not have a valid range, use N_i as its own
6711 range. This allows us to compare against names that may
6712 have N_i in their ranges. */
6713 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
6716 vr
.min
= ssa_name (i
);
6717 vr
.max
= ssa_name (i
);
6723 /* Compare all the value ranges for names equivalent to VAR with VAL
6724 using comparison code COMP. Return the same value returned by
6725 compare_range_with_value, including the setting of
6726 *STRICT_OVERFLOW_P. */
6729 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
6730 bool *strict_overflow_p
)
6736 int used_strict_overflow
;
6738 value_range_t equiv_vr
;
6740 /* Get the set of equivalences for VAR. */
6741 e
= get_value_range (var
)->equiv
;
6743 /* Start at -1. Set it to 0 if we do a comparison without relying
6744 on overflow, or 1 if all comparisons rely on overflow. */
6745 used_strict_overflow
= -1;
6747 /* Compare vars' value range with val. */
6748 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
6750 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
6752 used_strict_overflow
= sop
? 1 : 0;
6754 /* If the equiv set is empty we have done all work we need to do. */
6758 && used_strict_overflow
> 0)
6759 *strict_overflow_p
= true;
6763 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
6765 equiv_vr
= get_vr_for_comparison (i
);
6767 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
6770 /* If we get different answers from different members
6771 of the equivalence set this check must be in a dead
6772 code region. Folding it to a trap representation
6773 would be correct here. For now just return don't-know. */
6783 used_strict_overflow
= 0;
6784 else if (used_strict_overflow
< 0)
6785 used_strict_overflow
= 1;
6790 && used_strict_overflow
> 0)
6791 *strict_overflow_p
= true;
6797 /* Given a comparison code COMP and names N1 and N2, compare all the
6798 ranges equivalent to N1 against all the ranges equivalent to N2
6799 to determine the value of N1 COMP N2. Return the same value
6800 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
6801 whether we relied on an overflow infinity in the comparison. */
6805 compare_names (enum tree_code comp
, tree n1
, tree n2
,
6806 bool *strict_overflow_p
)
6810 bitmap_iterator bi1
, bi2
;
6812 int used_strict_overflow
;
6813 static bitmap_obstack
*s_obstack
= NULL
;
6814 static bitmap s_e1
= NULL
, s_e2
= NULL
;
6816 /* Compare the ranges of every name equivalent to N1 against the
6817 ranges of every name equivalent to N2. */
6818 e1
= get_value_range (n1
)->equiv
;
6819 e2
= get_value_range (n2
)->equiv
;
6821 /* Use the fake bitmaps if e1 or e2 are not available. */
6822 if (s_obstack
== NULL
)
6824 s_obstack
= XNEW (bitmap_obstack
);
6825 bitmap_obstack_initialize (s_obstack
);
6826 s_e1
= BITMAP_ALLOC (s_obstack
);
6827 s_e2
= BITMAP_ALLOC (s_obstack
);
6834 /* Add N1 and N2 to their own set of equivalences to avoid
6835 duplicating the body of the loop just to check N1 and N2
6837 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
6838 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
6840 /* If the equivalence sets have a common intersection, then the two
6841 names can be compared without checking their ranges. */
6842 if (bitmap_intersect_p (e1
, e2
))
6844 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6845 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6847 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
6849 : boolean_false_node
;
6852 /* Start at -1. Set it to 0 if we do a comparison without relying
6853 on overflow, or 1 if all comparisons rely on overflow. */
6854 used_strict_overflow
= -1;
6856 /* Otherwise, compare all the equivalent ranges. First, add N1 and
6857 N2 to their own set of equivalences to avoid duplicating the body
6858 of the loop just to check N1 and N2 ranges. */
6859 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
6861 value_range_t vr1
= get_vr_for_comparison (i1
);
6863 t
= retval
= NULL_TREE
;
6864 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
6868 value_range_t vr2
= get_vr_for_comparison (i2
);
6870 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
6873 /* If we get different answers from different members
6874 of the equivalence set this check must be in a dead
6875 code region. Folding it to a trap representation
6876 would be correct here. For now just return don't-know. */
6880 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6881 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6887 used_strict_overflow
= 0;
6888 else if (used_strict_overflow
< 0)
6889 used_strict_overflow
= 1;
6895 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6896 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6897 if (used_strict_overflow
> 0)
6898 *strict_overflow_p
= true;
6903 /* None of the equivalent ranges are useful in computing this
6905 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6906 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6910 /* Helper function for vrp_evaluate_conditional_warnv. */
6913 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
6915 bool * strict_overflow_p
)
6917 value_range_t
*vr0
, *vr1
;
6919 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
6920 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
6922 tree res
= NULL_TREE
;
6924 res
= compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
6926 res
= compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
6928 res
= (compare_range_with_value
6929 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
6933 /* Helper function for vrp_evaluate_conditional_warnv. */
6936 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
6937 tree op1
, bool use_equiv_p
,
6938 bool *strict_overflow_p
, bool *only_ranges
)
6942 *only_ranges
= true;
6944 /* We only deal with integral and pointer types. */
6945 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6946 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
6952 && (ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
6953 (code
, op0
, op1
, strict_overflow_p
)))
6955 *only_ranges
= false;
6956 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
)
6957 return compare_names (code
, op0
, op1
, strict_overflow_p
);
6958 else if (TREE_CODE (op0
) == SSA_NAME
)
6959 return compare_name_with_value (code
, op0
, op1
, strict_overflow_p
);
6960 else if (TREE_CODE (op1
) == SSA_NAME
)
6961 return (compare_name_with_value
6962 (swap_tree_comparison (code
), op1
, op0
, strict_overflow_p
));
6965 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code
, op0
, op1
,
6970 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
6971 information. Return NULL if the conditional can not be evaluated.
6972 The ranges of all the names equivalent with the operands in COND
6973 will be used when trying to compute the value. If the result is
6974 based on undefined signed overflow, issue a warning if
6978 vrp_evaluate_conditional (enum tree_code code
, tree op0
, tree op1
, gimple stmt
)
6984 /* Some passes and foldings leak constants with overflow flag set
6985 into the IL. Avoid doing wrong things with these and bail out. */
6986 if ((TREE_CODE (op0
) == INTEGER_CST
6987 && TREE_OVERFLOW (op0
))
6988 || (TREE_CODE (op1
) == INTEGER_CST
6989 && TREE_OVERFLOW (op1
)))
6993 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
6998 enum warn_strict_overflow_code wc
;
6999 const char* warnmsg
;
7001 if (is_gimple_min_invariant (ret
))
7003 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
7004 warnmsg
= G_("assuming signed overflow does not occur when "
7005 "simplifying conditional to constant");
7009 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
7010 warnmsg
= G_("assuming signed overflow does not occur when "
7011 "simplifying conditional");
7014 if (issue_strict_overflow_warning (wc
))
7016 location_t location
;
7018 if (!gimple_has_location (stmt
))
7019 location
= input_location
;
7021 location
= gimple_location (stmt
);
7022 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
7026 if (warn_type_limits
7027 && ret
&& only_ranges
7028 && TREE_CODE_CLASS (code
) == tcc_comparison
7029 && TREE_CODE (op0
) == SSA_NAME
)
7031 /* If the comparison is being folded and the operand on the LHS
7032 is being compared against a constant value that is outside of
7033 the natural range of OP0's type, then the predicate will
7034 always fold regardless of the value of OP0. If -Wtype-limits
7035 was specified, emit a warning. */
7036 tree type
= TREE_TYPE (op0
);
7037 value_range_t
*vr0
= get_value_range (op0
);
7039 if (vr0
->type
!= VR_VARYING
7040 && INTEGRAL_TYPE_P (type
)
7041 && vrp_val_is_min (vr0
->min
)
7042 && vrp_val_is_max (vr0
->max
)
7043 && is_gimple_min_invariant (op1
))
7045 location_t location
;
7047 if (!gimple_has_location (stmt
))
7048 location
= input_location
;
7050 location
= gimple_location (stmt
);
7052 warning_at (location
, OPT_Wtype_limits
,
7054 ? G_("comparison always false "
7055 "due to limited range of data type")
7056 : G_("comparison always true "
7057 "due to limited range of data type"));
7065 /* Visit conditional statement STMT. If we can determine which edge
7066 will be taken out of STMT's basic block, record it in
7067 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7068 SSA_PROP_VARYING. */
7070 static enum ssa_prop_result
7071 vrp_visit_cond_stmt (gimple stmt
, edge
*taken_edge_p
)
7076 *taken_edge_p
= NULL
;
7078 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7083 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
7084 print_gimple_stmt (dump_file
, stmt
, 0, 0);
7085 fprintf (dump_file
, "\nWith known ranges\n");
7087 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
7089 fprintf (dump_file
, "\t");
7090 print_generic_expr (dump_file
, use
, 0);
7091 fprintf (dump_file
, ": ");
7092 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
7095 fprintf (dump_file
, "\n");
7098 /* Compute the value of the predicate COND by checking the known
7099 ranges of each of its operands.
7101 Note that we cannot evaluate all the equivalent ranges here
7102 because those ranges may not yet be final and with the current
7103 propagation strategy, we cannot determine when the value ranges
7104 of the names in the equivalence set have changed.
7106 For instance, given the following code fragment
7110 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7114 Assume that on the first visit to i_14, i_5 has the temporary
7115 range [8, 8] because the second argument to the PHI function is
7116 not yet executable. We derive the range ~[0, 0] for i_14 and the
7117 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7118 the first time, since i_14 is equivalent to the range [8, 8], we
7119 determine that the predicate is always false.
7121 On the next round of propagation, i_13 is determined to be
7122 VARYING, which causes i_5 to drop down to VARYING. So, another
7123 visit to i_14 is scheduled. In this second visit, we compute the
7124 exact same range and equivalence set for i_14, namely ~[0, 0] and
7125 { i_5 }. But we did not have the previous range for i_5
7126 registered, so vrp_visit_assignment thinks that the range for
7127 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7128 is not visited again, which stops propagation from visiting
7129 statements in the THEN clause of that if().
7131 To properly fix this we would need to keep the previous range
7132 value for the names in the equivalence set. This way we would've
7133 discovered that from one visit to the other i_5 changed from
7134 range [8, 8] to VR_VARYING.
7136 However, fixing this apparent limitation may not be worth the
7137 additional checking. Testing on several code bases (GCC, DLV,
7138 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7139 4 more predicates folded in SPEC. */
7142 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
7143 gimple_cond_lhs (stmt
),
7144 gimple_cond_rhs (stmt
),
7149 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
7152 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7154 "\nIgnoring predicate evaluation because "
7155 "it assumes that signed overflow is undefined");
7160 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7162 fprintf (dump_file
, "\nPredicate evaluates to: ");
7163 if (val
== NULL_TREE
)
7164 fprintf (dump_file
, "DON'T KNOW\n");
7166 print_generic_stmt (dump_file
, val
, 0);
7169 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
7172 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7173 that includes the value VAL. The search is restricted to the range
7174 [START_IDX, n - 1] where n is the size of VEC.
7176 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7179 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7180 it is placed in IDX and false is returned.
7182 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7186 find_case_label_index (gimple stmt
, size_t start_idx
, tree val
, size_t *idx
)
7188 size_t n
= gimple_switch_num_labels (stmt
);
7191 /* Find case label for minimum of the value range or the next one.
7192 At each iteration we are searching in [low, high - 1]. */
7194 for (low
= start_idx
, high
= n
; high
!= low
; )
7198 /* Note that i != high, so we never ask for n. */
7199 size_t i
= (high
+ low
) / 2;
7200 t
= gimple_switch_label (stmt
, i
);
7202 /* Cache the result of comparing CASE_LOW and val. */
7203 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
7207 /* Ranges cannot be empty. */
7216 if (CASE_HIGH (t
) != NULL
7217 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
7229 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7230 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7231 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7232 then MAX_IDX < MIN_IDX.
7233 Returns true if the default label is not needed. */
7236 find_case_label_range (gimple stmt
, tree min
, tree max
, size_t *min_idx
,
7240 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
7241 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
7245 && max_take_default
)
7247 /* Only the default case label reached.
7248 Return an empty range. */
7255 bool take_default
= min_take_default
|| max_take_default
;
7259 if (max_take_default
)
7262 /* If the case label range is continuous, we do not need
7263 the default case label. Verify that. */
7264 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
7265 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
7266 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
7267 for (k
= i
+ 1; k
<= j
; ++k
)
7269 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
7270 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
7272 take_default
= true;
7276 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
7277 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
7282 return !take_default
;
7286 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7287 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7288 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7289 Returns true if the default label is not needed. */
7292 find_case_label_ranges (gimple stmt
, value_range_t
*vr
, size_t *min_idx1
,
7293 size_t *max_idx1
, size_t *min_idx2
,
7297 unsigned int n
= gimple_switch_num_labels (stmt
);
7299 tree case_low
, case_high
;
7300 tree min
= vr
->min
, max
= vr
->max
;
7302 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
7304 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
7306 /* Set second range to emtpy. */
7310 if (vr
->type
== VR_RANGE
)
7314 return !take_default
;
7317 /* Set first range to all case labels. */
7324 /* Make sure all the values of case labels [i , j] are contained in
7325 range [MIN, MAX]. */
7326 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
7327 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
7328 if (tree_int_cst_compare (case_low
, min
) < 0)
7330 if (case_high
!= NULL_TREE
7331 && tree_int_cst_compare (max
, case_high
) < 0)
7337 /* If the range spans case labels [i, j], the corresponding anti-range spans
7338 the labels [1, i - 1] and [j + 1, n - 1]. */
7364 /* Visit switch statement STMT. If we can determine which edge
7365 will be taken out of STMT's basic block, record it in
7366 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7367 SSA_PROP_VARYING. */
7369 static enum ssa_prop_result
7370 vrp_visit_switch_stmt (gimple stmt
, edge
*taken_edge_p
)
7374 size_t i
= 0, j
= 0, k
, l
;
7377 *taken_edge_p
= NULL
;
7378 op
= gimple_switch_index (stmt
);
7379 if (TREE_CODE (op
) != SSA_NAME
)
7380 return SSA_PROP_VARYING
;
7382 vr
= get_value_range (op
);
7383 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7385 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7386 print_generic_expr (dump_file
, op
, 0);
7387 fprintf (dump_file
, " with known range ");
7388 dump_value_range (dump_file
, vr
);
7389 fprintf (dump_file
, "\n");
7392 if ((vr
->type
!= VR_RANGE
7393 && vr
->type
!= VR_ANTI_RANGE
)
7394 || symbolic_range_p (vr
))
7395 return SSA_PROP_VARYING
;
7397 /* Find the single edge that is taken from the switch expression. */
7398 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7400 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7404 gcc_assert (take_default
);
7405 val
= gimple_switch_default_label (stmt
);
7409 /* Check if labels with index i to j and maybe the default label
7410 are all reaching the same label. */
7412 val
= gimple_switch_label (stmt
, i
);
7414 && CASE_LABEL (gimple_switch_default_label (stmt
))
7415 != CASE_LABEL (val
))
7417 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7418 fprintf (dump_file
, " not a single destination for this "
7420 return SSA_PROP_VARYING
;
7422 for (++i
; i
<= j
; ++i
)
7424 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
7426 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7427 fprintf (dump_file
, " not a single destination for this "
7429 return SSA_PROP_VARYING
;
7434 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
7436 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7437 fprintf (dump_file
, " not a single destination for this "
7439 return SSA_PROP_VARYING
;
7444 *taken_edge_p
= find_edge (gimple_bb (stmt
),
7445 label_to_block (CASE_LABEL (val
)));
7447 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7449 fprintf (dump_file
, " will take edge to ");
7450 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
7453 return SSA_PROP_INTERESTING
;
7457 /* Evaluate statement STMT. If the statement produces a useful range,
7458 return SSA_PROP_INTERESTING and record the SSA name with the
7459 interesting range into *OUTPUT_P.
7461 If STMT is a conditional branch and we can determine its truth
7462 value, the taken edge is recorded in *TAKEN_EDGE_P.
7464 If STMT produces a varying value, return SSA_PROP_VARYING. */
7466 static enum ssa_prop_result
7467 vrp_visit_stmt (gimple stmt
, edge
*taken_edge_p
, tree
*output_p
)
7472 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7474 fprintf (dump_file
, "\nVisiting statement:\n");
7475 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
7478 if (!stmt_interesting_for_vrp (stmt
))
7479 gcc_assert (stmt_ends_bb_p (stmt
));
7480 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7481 return vrp_visit_assignment_or_call (stmt
, output_p
);
7482 else if (gimple_code (stmt
) == GIMPLE_COND
)
7483 return vrp_visit_cond_stmt (stmt
, taken_edge_p
);
7484 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
7485 return vrp_visit_switch_stmt (stmt
, taken_edge_p
);
7487 /* All other statements produce nothing of interest for VRP, so mark
7488 their outputs varying and prevent further simulation. */
7489 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7490 set_value_range_to_varying (get_value_range (def
));
7492 return SSA_PROP_VARYING
;
7495 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7496 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7497 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7498 possible such range. The resulting range is not canonicalized. */
7501 union_ranges (enum value_range_type
*vr0type
,
7502 tree
*vr0min
, tree
*vr0max
,
7503 enum value_range_type vr1type
,
7504 tree vr1min
, tree vr1max
)
7506 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7507 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7509 /* [] is vr0, () is vr1 in the following classification comments. */
7513 if (*vr0type
== vr1type
)
7514 /* Nothing to do for equal ranges. */
7516 else if ((*vr0type
== VR_RANGE
7517 && vr1type
== VR_ANTI_RANGE
)
7518 || (*vr0type
== VR_ANTI_RANGE
7519 && vr1type
== VR_RANGE
))
7521 /* For anti-range with range union the result is varying. */
7527 else if (operand_less_p (*vr0max
, vr1min
) == 1
7528 || operand_less_p (vr1max
, *vr0min
) == 1)
7530 /* [ ] ( ) or ( ) [ ]
7531 If the ranges have an empty intersection, result of the union
7532 operation is the anti-range or if both are anti-ranges
7534 if (*vr0type
== VR_ANTI_RANGE
7535 && vr1type
== VR_ANTI_RANGE
)
7537 else if (*vr0type
== VR_ANTI_RANGE
7538 && vr1type
== VR_RANGE
)
7540 else if (*vr0type
== VR_RANGE
7541 && vr1type
== VR_ANTI_RANGE
)
7547 else if (*vr0type
== VR_RANGE
7548 && vr1type
== VR_RANGE
)
7550 /* The result is the convex hull of both ranges. */
7551 if (operand_less_p (*vr0max
, vr1min
) == 1)
7553 /* If the result can be an anti-range, create one. */
7554 if (TREE_CODE (*vr0max
) == INTEGER_CST
7555 && TREE_CODE (vr1min
) == INTEGER_CST
7556 && vrp_val_is_min (*vr0min
)
7557 && vrp_val_is_max (vr1max
))
7559 tree min
= int_const_binop (PLUS_EXPR
,
7561 build_int_cst (TREE_TYPE (*vr0max
), 1));
7562 tree max
= int_const_binop (MINUS_EXPR
,
7564 build_int_cst (TREE_TYPE (vr1min
), 1));
7565 if (!operand_less_p (max
, min
))
7567 *vr0type
= VR_ANTI_RANGE
;
7579 /* If the result can be an anti-range, create one. */
7580 if (TREE_CODE (vr1max
) == INTEGER_CST
7581 && TREE_CODE (*vr0min
) == INTEGER_CST
7582 && vrp_val_is_min (vr1min
)
7583 && vrp_val_is_max (*vr0max
))
7585 tree min
= int_const_binop (PLUS_EXPR
,
7587 build_int_cst (TREE_TYPE (vr1max
), 1));
7588 tree max
= int_const_binop (MINUS_EXPR
,
7590 build_int_cst (TREE_TYPE (*vr0min
), 1));
7591 if (!operand_less_p (max
, min
))
7593 *vr0type
= VR_ANTI_RANGE
;
7607 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7608 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7610 /* [ ( ) ] or [( ) ] or [ ( )] */
7611 if (*vr0type
== VR_RANGE
7612 && vr1type
== VR_RANGE
)
7614 else if (*vr0type
== VR_ANTI_RANGE
7615 && vr1type
== VR_ANTI_RANGE
)
7621 else if (*vr0type
== VR_ANTI_RANGE
7622 && vr1type
== VR_RANGE
)
7624 /* Arbitrarily choose the right or left gap. */
7625 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
7626 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7627 build_int_cst (TREE_TYPE (vr1min
), 1));
7628 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
7629 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7630 build_int_cst (TREE_TYPE (vr1max
), 1));
7634 else if (*vr0type
== VR_RANGE
7635 && vr1type
== VR_ANTI_RANGE
)
7636 /* The result covers everything. */
7641 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7642 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7644 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7645 if (*vr0type
== VR_RANGE
7646 && vr1type
== VR_RANGE
)
7652 else if (*vr0type
== VR_ANTI_RANGE
7653 && vr1type
== VR_ANTI_RANGE
)
7655 else if (*vr0type
== VR_RANGE
7656 && vr1type
== VR_ANTI_RANGE
)
7658 *vr0type
= VR_ANTI_RANGE
;
7659 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
7661 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7662 build_int_cst (TREE_TYPE (*vr0min
), 1));
7665 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
7667 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7668 build_int_cst (TREE_TYPE (*vr0max
), 1));
7674 else if (*vr0type
== VR_ANTI_RANGE
7675 && vr1type
== VR_RANGE
)
7676 /* The result covers everything. */
7681 else if ((operand_less_p (vr1min
, *vr0max
) == 1
7682 || operand_equal_p (vr1min
, *vr0max
, 0))
7683 && operand_less_p (*vr0min
, vr1min
) == 1
7684 && operand_less_p (*vr0max
, vr1max
) == 1)
7686 /* [ ( ] ) or [ ]( ) */
7687 if (*vr0type
== VR_RANGE
7688 && vr1type
== VR_RANGE
)
7690 else if (*vr0type
== VR_ANTI_RANGE
7691 && vr1type
== VR_ANTI_RANGE
)
7693 else if (*vr0type
== VR_ANTI_RANGE
7694 && vr1type
== VR_RANGE
)
7696 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7697 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7698 build_int_cst (TREE_TYPE (vr1min
), 1));
7702 else if (*vr0type
== VR_RANGE
7703 && vr1type
== VR_ANTI_RANGE
)
7705 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7708 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7709 build_int_cst (TREE_TYPE (*vr0max
), 1));
7718 else if ((operand_less_p (*vr0min
, vr1max
) == 1
7719 || operand_equal_p (*vr0min
, vr1max
, 0))
7720 && operand_less_p (vr1min
, *vr0min
) == 1
7721 && operand_less_p (vr1max
, *vr0max
) == 1)
7723 /* ( [ ) ] or ( )[ ] */
7724 if (*vr0type
== VR_RANGE
7725 && vr1type
== VR_RANGE
)
7727 else if (*vr0type
== VR_ANTI_RANGE
7728 && vr1type
== VR_ANTI_RANGE
)
7730 else if (*vr0type
== VR_ANTI_RANGE
7731 && vr1type
== VR_RANGE
)
7733 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7734 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7735 build_int_cst (TREE_TYPE (vr1max
), 1));
7739 else if (*vr0type
== VR_RANGE
7740 && vr1type
== VR_ANTI_RANGE
)
7742 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7746 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7747 build_int_cst (TREE_TYPE (*vr0min
), 1));
7761 *vr0type
= VR_VARYING
;
7762 *vr0min
= NULL_TREE
;
7763 *vr0max
= NULL_TREE
;
7766 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7767 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7768 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7769 possible such range. The resulting range is not canonicalized. */
7772 intersect_ranges (enum value_range_type
*vr0type
,
7773 tree
*vr0min
, tree
*vr0max
,
7774 enum value_range_type vr1type
,
7775 tree vr1min
, tree vr1max
)
7777 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7778 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7780 /* [] is vr0, () is vr1 in the following classification comments. */
7784 if (*vr0type
== vr1type
)
7785 /* Nothing to do for equal ranges. */
7787 else if ((*vr0type
== VR_RANGE
7788 && vr1type
== VR_ANTI_RANGE
)
7789 || (*vr0type
== VR_ANTI_RANGE
7790 && vr1type
== VR_RANGE
))
7792 /* For anti-range with range intersection the result is empty. */
7793 *vr0type
= VR_UNDEFINED
;
7794 *vr0min
= NULL_TREE
;
7795 *vr0max
= NULL_TREE
;
7800 else if (operand_less_p (*vr0max
, vr1min
) == 1
7801 || operand_less_p (vr1max
, *vr0min
) == 1)
7803 /* [ ] ( ) or ( ) [ ]
7804 If the ranges have an empty intersection, the result of the
7805 intersect operation is the range for intersecting an
7806 anti-range with a range or empty when intersecting two ranges. */
7807 if (*vr0type
== VR_RANGE
7808 && vr1type
== VR_ANTI_RANGE
)
7810 else if (*vr0type
== VR_ANTI_RANGE
7811 && vr1type
== VR_RANGE
)
7817 else if (*vr0type
== VR_RANGE
7818 && vr1type
== VR_RANGE
)
7820 *vr0type
= VR_UNDEFINED
;
7821 *vr0min
= NULL_TREE
;
7822 *vr0max
= NULL_TREE
;
7824 else if (*vr0type
== VR_ANTI_RANGE
7825 && vr1type
== VR_ANTI_RANGE
)
7827 /* If the anti-ranges are adjacent to each other merge them. */
7828 if (TREE_CODE (*vr0max
) == INTEGER_CST
7829 && TREE_CODE (vr1min
) == INTEGER_CST
7830 && operand_less_p (*vr0max
, vr1min
) == 1
7831 && integer_onep (int_const_binop (MINUS_EXPR
,
7834 else if (TREE_CODE (vr1max
) == INTEGER_CST
7835 && TREE_CODE (*vr0min
) == INTEGER_CST
7836 && operand_less_p (vr1max
, *vr0min
) == 1
7837 && integer_onep (int_const_binop (MINUS_EXPR
,
7840 /* Else arbitrarily take VR0. */
7843 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7844 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7846 /* [ ( ) ] or [( ) ] or [ ( )] */
7847 if (*vr0type
== VR_RANGE
7848 && vr1type
== VR_RANGE
)
7850 /* If both are ranges the result is the inner one. */
7855 else if (*vr0type
== VR_RANGE
7856 && vr1type
== VR_ANTI_RANGE
)
7858 /* Choose the right gap if the left one is empty. */
7861 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7862 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7863 build_int_cst (TREE_TYPE (vr1max
), 1));
7867 /* Choose the left gap if the right one is empty. */
7870 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7871 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7872 build_int_cst (TREE_TYPE (vr1min
), 1));
7876 /* Choose the anti-range if the range is effectively varying. */
7877 else if (vrp_val_is_min (*vr0min
)
7878 && vrp_val_is_max (*vr0max
))
7884 /* Else choose the range. */
7886 else if (*vr0type
== VR_ANTI_RANGE
7887 && vr1type
== VR_ANTI_RANGE
)
7888 /* If both are anti-ranges the result is the outer one. */
7890 else if (*vr0type
== VR_ANTI_RANGE
7891 && vr1type
== VR_RANGE
)
7893 /* The intersection is empty. */
7894 *vr0type
= VR_UNDEFINED
;
7895 *vr0min
= NULL_TREE
;
7896 *vr0max
= NULL_TREE
;
7901 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7902 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7904 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7905 if (*vr0type
== VR_RANGE
7906 && vr1type
== VR_RANGE
)
7907 /* Choose the inner range. */
7909 else if (*vr0type
== VR_ANTI_RANGE
7910 && vr1type
== VR_RANGE
)
7912 /* Choose the right gap if the left is empty. */
7915 *vr0type
= VR_RANGE
;
7916 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7917 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7918 build_int_cst (TREE_TYPE (*vr0max
), 1));
7923 /* Choose the left gap if the right is empty. */
7926 *vr0type
= VR_RANGE
;
7927 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7928 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7929 build_int_cst (TREE_TYPE (*vr0min
), 1));
7934 /* Choose the anti-range if the range is effectively varying. */
7935 else if (vrp_val_is_min (vr1min
)
7936 && vrp_val_is_max (vr1max
))
7938 /* Else choose the range. */
7946 else if (*vr0type
== VR_ANTI_RANGE
7947 && vr1type
== VR_ANTI_RANGE
)
7949 /* If both are anti-ranges the result is the outer one. */
7954 else if (vr1type
== VR_ANTI_RANGE
7955 && *vr0type
== VR_RANGE
)
7957 /* The intersection is empty. */
7958 *vr0type
= VR_UNDEFINED
;
7959 *vr0min
= NULL_TREE
;
7960 *vr0max
= NULL_TREE
;
7965 else if ((operand_less_p (vr1min
, *vr0max
) == 1
7966 || operand_equal_p (vr1min
, *vr0max
, 0))
7967 && operand_less_p (*vr0min
, vr1min
) == 1)
7969 /* [ ( ] ) or [ ]( ) */
7970 if (*vr0type
== VR_ANTI_RANGE
7971 && vr1type
== VR_ANTI_RANGE
)
7973 else if (*vr0type
== VR_RANGE
7974 && vr1type
== VR_RANGE
)
7976 else if (*vr0type
== VR_RANGE
7977 && vr1type
== VR_ANTI_RANGE
)
7979 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7980 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7981 build_int_cst (TREE_TYPE (vr1min
), 1));
7985 else if (*vr0type
== VR_ANTI_RANGE
7986 && vr1type
== VR_RANGE
)
7988 *vr0type
= VR_RANGE
;
7989 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7990 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7991 build_int_cst (TREE_TYPE (*vr0max
), 1));
7999 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8000 || operand_equal_p (*vr0min
, vr1max
, 0))
8001 && operand_less_p (vr1min
, *vr0min
) == 1)
8003 /* ( [ ) ] or ( )[ ] */
8004 if (*vr0type
== VR_ANTI_RANGE
8005 && vr1type
== VR_ANTI_RANGE
)
8007 else if (*vr0type
== VR_RANGE
8008 && vr1type
== VR_RANGE
)
8010 else if (*vr0type
== VR_RANGE
8011 && vr1type
== VR_ANTI_RANGE
)
8013 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8014 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8015 build_int_cst (TREE_TYPE (vr1max
), 1));
8019 else if (*vr0type
== VR_ANTI_RANGE
8020 && vr1type
== VR_RANGE
)
8022 *vr0type
= VR_RANGE
;
8023 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8024 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8025 build_int_cst (TREE_TYPE (*vr0min
), 1));
8034 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8035 result for the intersection. That's always a conservative
8036 correct estimate. */
8042 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8043 in *VR0. This may not be the smallest possible such range. */
8046 vrp_intersect_ranges_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8048 value_range_t saved
;
8050 /* If either range is VR_VARYING the other one wins. */
8051 if (vr1
->type
== VR_VARYING
)
8053 if (vr0
->type
== VR_VARYING
)
8055 copy_value_range (vr0
, vr1
);
8059 /* When either range is VR_UNDEFINED the resulting range is
8060 VR_UNDEFINED, too. */
8061 if (vr0
->type
== VR_UNDEFINED
)
8063 if (vr1
->type
== VR_UNDEFINED
)
8065 set_value_range_to_undefined (vr0
);
8069 /* Save the original vr0 so we can return it as conservative intersection
8070 result when our worker turns things to varying. */
8072 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8073 vr1
->type
, vr1
->min
, vr1
->max
);
8074 /* Make sure to canonicalize the result though as the inversion of a
8075 VR_RANGE can still be a VR_RANGE. */
8076 set_and_canonicalize_value_range (vr0
, vr0
->type
,
8077 vr0
->min
, vr0
->max
, vr0
->equiv
);
8078 /* If that failed, use the saved original VR0. */
8079 if (vr0
->type
== VR_VARYING
)
8084 /* If the result is VR_UNDEFINED there is no need to mess with
8085 the equivalencies. */
8086 if (vr0
->type
== VR_UNDEFINED
)
8089 /* The resulting set of equivalences for range intersection is the union of
8091 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8092 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
8093 else if (vr1
->equiv
&& !vr0
->equiv
)
8094 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
8098 vrp_intersect_ranges (value_range_t
*vr0
, value_range_t
*vr1
)
8100 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8102 fprintf (dump_file
, "Intersecting\n ");
8103 dump_value_range (dump_file
, vr0
);
8104 fprintf (dump_file
, "\nand\n ");
8105 dump_value_range (dump_file
, vr1
);
8106 fprintf (dump_file
, "\n");
8108 vrp_intersect_ranges_1 (vr0
, vr1
);
8109 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8111 fprintf (dump_file
, "to\n ");
8112 dump_value_range (dump_file
, vr0
);
8113 fprintf (dump_file
, "\n");
8117 /* Meet operation for value ranges. Given two value ranges VR0 and
8118 VR1, store in VR0 a range that contains both VR0 and VR1. This
8119 may not be the smallest possible such range. */
8122 vrp_meet_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8124 value_range_t saved
;
8126 if (vr0
->type
== VR_UNDEFINED
)
8128 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
8132 if (vr1
->type
== VR_UNDEFINED
)
8134 /* VR0 already has the resulting range. */
8138 if (vr0
->type
== VR_VARYING
)
8140 /* Nothing to do. VR0 already has the resulting range. */
8144 if (vr1
->type
== VR_VARYING
)
8146 set_value_range_to_varying (vr0
);
8151 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8152 vr1
->type
, vr1
->min
, vr1
->max
);
8153 if (vr0
->type
== VR_VARYING
)
8155 /* Failed to find an efficient meet. Before giving up and setting
8156 the result to VARYING, see if we can at least derive a useful
8157 anti-range. FIXME, all this nonsense about distinguishing
8158 anti-ranges from ranges is necessary because of the odd
8159 semantics of range_includes_zero_p and friends. */
8160 if (((saved
.type
== VR_RANGE
8161 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
8162 || (saved
.type
== VR_ANTI_RANGE
8163 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
8164 && ((vr1
->type
== VR_RANGE
8165 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
8166 || (vr1
->type
== VR_ANTI_RANGE
8167 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
8169 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
8171 /* Since this meet operation did not result from the meeting of
8172 two equivalent names, VR0 cannot have any equivalences. */
8174 bitmap_clear (vr0
->equiv
);
8178 set_value_range_to_varying (vr0
);
8181 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
8183 if (vr0
->type
== VR_VARYING
)
8186 /* The resulting set of equivalences is always the intersection of
8188 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8189 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
8190 else if (vr0
->equiv
&& !vr1
->equiv
)
8191 bitmap_clear (vr0
->equiv
);
8195 vrp_meet (value_range_t
*vr0
, value_range_t
*vr1
)
8197 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8199 fprintf (dump_file
, "Meeting\n ");
8200 dump_value_range (dump_file
, vr0
);
8201 fprintf (dump_file
, "\nand\n ");
8202 dump_value_range (dump_file
, vr1
);
8203 fprintf (dump_file
, "\n");
8205 vrp_meet_1 (vr0
, vr1
);
8206 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8208 fprintf (dump_file
, "to\n ");
8209 dump_value_range (dump_file
, vr0
);
8210 fprintf (dump_file
, "\n");
8215 /* Visit all arguments for PHI node PHI that flow through executable
8216 edges. If a valid value range can be derived from all the incoming
8217 value ranges, set a new range for the LHS of PHI. */
8219 static enum ssa_prop_result
8220 vrp_visit_phi_node (gimple phi
)
8223 tree lhs
= PHI_RESULT (phi
);
8224 value_range_t
*lhs_vr
= get_value_range (lhs
);
8225 value_range_t vr_result
= VR_INITIALIZER
;
8227 int edges
, old_edges
;
8230 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8232 fprintf (dump_file
, "\nVisiting PHI node: ");
8233 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
8237 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
8239 edge e
= gimple_phi_arg_edge (phi
, i
);
8241 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8244 " Argument #%d (%d -> %d %sexecutable)\n",
8245 (int) i
, e
->src
->index
, e
->dest
->index
,
8246 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
8249 if (e
->flags
& EDGE_EXECUTABLE
)
8251 tree arg
= PHI_ARG_DEF (phi
, i
);
8252 value_range_t vr_arg
;
8256 if (TREE_CODE (arg
) == SSA_NAME
)
8258 vr_arg
= *(get_value_range (arg
));
8259 /* Do not allow equivalences or symbolic ranges to leak in from
8260 backedges. That creates invalid equivalencies.
8261 See PR53465 and PR54767. */
8262 if (e
->flags
& EDGE_DFS_BACK
)
8264 if (vr_arg
.type
== VR_RANGE
8265 || vr_arg
.type
== VR_ANTI_RANGE
)
8267 vr_arg
.equiv
= NULL
;
8268 if (symbolic_range_p (&vr_arg
))
8270 vr_arg
.type
= VR_VARYING
;
8271 vr_arg
.min
= NULL_TREE
;
8272 vr_arg
.max
= NULL_TREE
;
8278 /* If the non-backedge arguments range is VR_VARYING then
8279 we can still try recording a simple equivalence. */
8280 if (vr_arg
.type
== VR_VARYING
)
8282 vr_arg
.type
= VR_RANGE
;
8285 vr_arg
.equiv
= NULL
;
8291 if (TREE_OVERFLOW_P (arg
))
8292 arg
= drop_tree_overflow (arg
);
8294 vr_arg
.type
= VR_RANGE
;
8297 vr_arg
.equiv
= NULL
;
8300 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8302 fprintf (dump_file
, "\t");
8303 print_generic_expr (dump_file
, arg
, dump_flags
);
8304 fprintf (dump_file
, ": ");
8305 dump_value_range (dump_file
, &vr_arg
);
8306 fprintf (dump_file
, "\n");
8310 copy_value_range (&vr_result
, &vr_arg
);
8312 vrp_meet (&vr_result
, &vr_arg
);
8315 if (vr_result
.type
== VR_VARYING
)
8320 if (vr_result
.type
== VR_VARYING
)
8322 else if (vr_result
.type
== VR_UNDEFINED
)
8325 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
8326 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
8328 /* To prevent infinite iterations in the algorithm, derive ranges
8329 when the new value is slightly bigger or smaller than the
8330 previous one. We don't do this if we have seen a new executable
8331 edge; this helps us avoid an overflow infinity for conditionals
8332 which are not in a loop. If the old value-range was VR_UNDEFINED
8333 use the updated range and iterate one more time. */
8335 && gimple_phi_num_args (phi
) > 1
8336 && edges
== old_edges
8337 && lhs_vr
->type
!= VR_UNDEFINED
)
8339 /* Compare old and new ranges, fall back to varying if the
8340 values are not comparable. */
8341 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
.min
);
8344 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
.max
);
8348 /* For non VR_RANGE or for pointers fall back to varying if
8349 the range changed. */
8350 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
.type
!= VR_RANGE
8351 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
8352 && (cmp_min
!= 0 || cmp_max
!= 0))
8355 /* If the new minimum is larger than than the previous one
8356 retain the old value. If the new minimum value is smaller
8357 than the previous one and not -INF go all the way to -INF + 1.
8358 In the first case, to avoid infinite bouncing between different
8359 minimums, and in the other case to avoid iterating millions of
8360 times to reach -INF. Going to -INF + 1 also lets the following
8361 iteration compute whether there will be any overflow, at the
8362 expense of one additional iteration. */
8364 vr_result
.min
= lhs_vr
->min
;
8365 else if (cmp_min
> 0
8366 && !vrp_val_is_min (vr_result
.min
))
8368 = int_const_binop (PLUS_EXPR
,
8369 vrp_val_min (TREE_TYPE (vr_result
.min
)),
8370 build_int_cst (TREE_TYPE (vr_result
.min
), 1));
8372 /* Similarly for the maximum value. */
8374 vr_result
.max
= lhs_vr
->max
;
8375 else if (cmp_max
< 0
8376 && !vrp_val_is_max (vr_result
.max
))
8378 = int_const_binop (MINUS_EXPR
,
8379 vrp_val_max (TREE_TYPE (vr_result
.min
)),
8380 build_int_cst (TREE_TYPE (vr_result
.min
), 1));
8382 /* If we dropped either bound to +-INF then if this is a loop
8383 PHI node SCEV may known more about its value-range. */
8384 if ((cmp_min
> 0 || cmp_min
< 0
8385 || cmp_max
< 0 || cmp_max
> 0)
8386 && (l
= loop_containing_stmt (phi
))
8387 && l
->header
== gimple_bb (phi
))
8388 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
8390 /* If we will end up with a (-INF, +INF) range, set it to
8391 VARYING. Same if the previous max value was invalid for
8392 the type and we end up with vr_result.min > vr_result.max. */
8393 if ((vrp_val_is_max (vr_result
.max
)
8394 && vrp_val_is_min (vr_result
.min
))
8395 || compare_values (vr_result
.min
,
8400 /* If the new range is different than the previous value, keep
8403 if (update_value_range (lhs
, &vr_result
))
8405 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8407 fprintf (dump_file
, "Found new range for ");
8408 print_generic_expr (dump_file
, lhs
, 0);
8409 fprintf (dump_file
, ": ");
8410 dump_value_range (dump_file
, &vr_result
);
8411 fprintf (dump_file
, "\n");
8414 return SSA_PROP_INTERESTING
;
8417 /* Nothing changed, don't add outgoing edges. */
8418 return SSA_PROP_NOT_INTERESTING
;
8420 /* No match found. Set the LHS to VARYING. */
8422 set_value_range_to_varying (lhs_vr
);
8423 return SSA_PROP_VARYING
;
8426 /* Simplify boolean operations if the source is known
8427 to be already a boolean. */
8429 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8431 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8433 bool need_conversion
;
8435 /* We handle only !=/== case here. */
8436 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
8438 op0
= gimple_assign_rhs1 (stmt
);
8439 if (!op_with_boolean_value_range_p (op0
))
8442 op1
= gimple_assign_rhs2 (stmt
);
8443 if (!op_with_boolean_value_range_p (op1
))
8446 /* Reduce number of cases to handle to NE_EXPR. As there is no
8447 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8448 if (rhs_code
== EQ_EXPR
)
8450 if (TREE_CODE (op1
) == INTEGER_CST
)
8451 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
8452 build_int_cst (TREE_TYPE (op1
), 1));
8457 lhs
= gimple_assign_lhs (stmt
);
8459 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
8461 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8463 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
8464 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
8465 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
8468 /* For A != 0 we can substitute A itself. */
8469 if (integer_zerop (op1
))
8470 gimple_assign_set_rhs_with_ops (gsi
,
8472 ? NOP_EXPR
: TREE_CODE (op0
),
8474 /* For A != B we substitute A ^ B. Either with conversion. */
8475 else if (need_conversion
)
8477 tree tem
= make_ssa_name (TREE_TYPE (op0
), NULL
);
8478 gimple newop
= gimple_build_assign_with_ops (BIT_XOR_EXPR
, tem
, op0
, op1
);
8479 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
8480 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
, NULL_TREE
);
8484 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
8485 update_stmt (gsi_stmt (*gsi
));
8490 /* Simplify a division or modulo operator to a right shift or
8491 bitwise and if the first operand is unsigned or is greater
8492 than zero and the second operand is an exact power of two. */
8495 simplify_div_or_mod_using_ranges (gimple stmt
)
8497 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8499 tree op0
= gimple_assign_rhs1 (stmt
);
8500 tree op1
= gimple_assign_rhs2 (stmt
);
8501 value_range_t
*vr
= get_value_range (gimple_assign_rhs1 (stmt
));
8503 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
8505 val
= integer_one_node
;
8511 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
8515 && integer_onep (val
)
8516 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8518 location_t location
;
8520 if (!gimple_has_location (stmt
))
8521 location
= input_location
;
8523 location
= gimple_location (stmt
);
8524 warning_at (location
, OPT_Wstrict_overflow
,
8525 "assuming signed overflow does not occur when "
8526 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8530 if (val
&& integer_onep (val
))
8534 if (rhs_code
== TRUNC_DIV_EXPR
)
8536 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
8537 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
8538 gimple_assign_set_rhs1 (stmt
, op0
);
8539 gimple_assign_set_rhs2 (stmt
, t
);
8543 t
= build_int_cst (TREE_TYPE (op1
), 1);
8544 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
8545 t
= fold_convert (TREE_TYPE (op0
), t
);
8547 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
8548 gimple_assign_set_rhs1 (stmt
, op0
);
8549 gimple_assign_set_rhs2 (stmt
, t
);
8559 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8560 ABS_EXPR. If the operand is <= 0, then simplify the
8561 ABS_EXPR into a NEGATE_EXPR. */
8564 simplify_abs_using_ranges (gimple stmt
)
8567 tree op
= gimple_assign_rhs1 (stmt
);
8568 tree type
= TREE_TYPE (op
);
8569 value_range_t
*vr
= get_value_range (op
);
8571 if (TYPE_UNSIGNED (type
))
8573 val
= integer_zero_node
;
8579 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
8583 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
,
8588 if (integer_zerop (val
))
8589 val
= integer_one_node
;
8590 else if (integer_onep (val
))
8591 val
= integer_zero_node
;
8596 && (integer_onep (val
) || integer_zerop (val
)))
8598 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8600 location_t location
;
8602 if (!gimple_has_location (stmt
))
8603 location
= input_location
;
8605 location
= gimple_location (stmt
);
8606 warning_at (location
, OPT_Wstrict_overflow
,
8607 "assuming signed overflow does not occur when "
8608 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8611 gimple_assign_set_rhs1 (stmt
, op
);
8612 if (integer_onep (val
))
8613 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
8615 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
8624 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8625 If all the bits that are being cleared by & are already
8626 known to be zero from VR, or all the bits that are being
8627 set by | are already known to be one from VR, the bit
8628 operation is redundant. */
8631 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8633 tree op0
= gimple_assign_rhs1 (stmt
);
8634 tree op1
= gimple_assign_rhs2 (stmt
);
8635 tree op
= NULL_TREE
;
8636 value_range_t vr0
= VR_INITIALIZER
;
8637 value_range_t vr1
= VR_INITIALIZER
;
8638 wide_int may_be_nonzero0
, may_be_nonzero1
;
8639 wide_int must_be_nonzero0
, must_be_nonzero1
;
8642 if (TREE_CODE (op0
) == SSA_NAME
)
8643 vr0
= *(get_value_range (op0
));
8644 else if (is_gimple_min_invariant (op0
))
8645 set_value_range_to_value (&vr0
, op0
, NULL
);
8649 if (TREE_CODE (op1
) == SSA_NAME
)
8650 vr1
= *(get_value_range (op1
));
8651 else if (is_gimple_min_invariant (op1
))
8652 set_value_range_to_value (&vr1
, op1
, NULL
);
8656 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
,
8659 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
,
8663 switch (gimple_assign_rhs_code (stmt
))
8666 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8672 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8680 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8686 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8697 if (op
== NULL_TREE
)
8700 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
, NULL
);
8701 update_stmt (gsi_stmt (*gsi
));
8705 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8706 a known value range VR.
8708 If there is one and only one value which will satisfy the
8709 conditional, then return that value. Else return NULL. */
8712 test_for_singularity (enum tree_code cond_code
, tree op0
,
8713 tree op1
, value_range_t
*vr
)
8718 /* Extract minimum/maximum values which satisfy the
8719 the conditional as it was written. */
8720 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
8722 /* This should not be negative infinity; there is no overflow
8724 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
8727 if (cond_code
== LT_EXPR
&& !is_overflow_infinity (max
))
8729 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8730 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
8732 TREE_NO_WARNING (max
) = 1;
8735 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
8737 /* This should not be positive infinity; there is no overflow
8739 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
8742 if (cond_code
== GT_EXPR
&& !is_overflow_infinity (min
))
8744 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8745 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
8747 TREE_NO_WARNING (min
) = 1;
8751 /* Now refine the minimum and maximum values using any
8752 value range information we have for op0. */
8755 if (compare_values (vr
->min
, min
) == 1)
8757 if (compare_values (vr
->max
, max
) == -1)
8760 /* If the new min/max values have converged to a single value,
8761 then there is only one value which can satisfy the condition,
8762 return that value. */
8763 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
8769 /* Return whether the value range *VR fits in an integer type specified
8770 by PRECISION and UNSIGNED_P. */
8773 range_fits_type_p (value_range_t
*vr
, unsigned dest_precision
, signop dest_sgn
)
8776 unsigned src_precision
;
8780 /* We can only handle integral and pointer types. */
8781 src_type
= TREE_TYPE (vr
->min
);
8782 if (!INTEGRAL_TYPE_P (src_type
)
8783 && !POINTER_TYPE_P (src_type
))
8786 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
8787 and so is an identity transform. */
8788 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
8789 src_sgn
= TYPE_SIGN (src_type
);
8790 if ((src_precision
< dest_precision
8791 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
8792 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
8795 /* Now we can only handle ranges with constant bounds. */
8796 if (vr
->type
!= VR_RANGE
8797 || TREE_CODE (vr
->min
) != INTEGER_CST
8798 || TREE_CODE (vr
->max
) != INTEGER_CST
)
8801 /* For sign changes, the MSB of the wide_int has to be clear.
8802 An unsigned value with its MSB set cannot be represented by
8803 a signed wide_int, while a negative value cannot be represented
8804 by an unsigned wide_int. */
8805 if (src_sgn
!= dest_sgn
8806 && (wi::lts_p (vr
->min
, 0) || wi::lts_p (vr
->max
, 0)))
8809 /* Then we can perform the conversion on both ends and compare
8810 the result for equality. */
8811 tem
= wi::ext (wi::to_widest (vr
->min
), dest_precision
, dest_sgn
);
8812 if (tem
!= wi::to_widest (vr
->min
))
8814 tem
= wi::ext (wi::to_widest (vr
->max
), dest_precision
, dest_sgn
);
8815 if (tem
!= wi::to_widest (vr
->max
))
8821 /* Simplify a conditional using a relational operator to an equality
8822 test if the range information indicates only one value can satisfy
8823 the original conditional. */
8826 simplify_cond_using_ranges (gimple stmt
)
8828 tree op0
= gimple_cond_lhs (stmt
);
8829 tree op1
= gimple_cond_rhs (stmt
);
8830 enum tree_code cond_code
= gimple_cond_code (stmt
);
8832 if (cond_code
!= NE_EXPR
8833 && cond_code
!= EQ_EXPR
8834 && TREE_CODE (op0
) == SSA_NAME
8835 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
8836 && is_gimple_min_invariant (op1
))
8838 value_range_t
*vr
= get_value_range (op0
);
8840 /* If we have range information for OP0, then we might be
8841 able to simplify this conditional. */
8842 if (vr
->type
== VR_RANGE
)
8844 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
8850 fprintf (dump_file
, "Simplified relational ");
8851 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8852 fprintf (dump_file
, " into ");
8855 gimple_cond_set_code (stmt
, EQ_EXPR
);
8856 gimple_cond_set_lhs (stmt
, op0
);
8857 gimple_cond_set_rhs (stmt
, new_tree
);
8863 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8864 fprintf (dump_file
, "\n");
8870 /* Try again after inverting the condition. We only deal
8871 with integral types here, so no need to worry about
8872 issues with inverting FP comparisons. */
8873 cond_code
= invert_tree_comparison (cond_code
, false);
8874 new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
8880 fprintf (dump_file
, "Simplified relational ");
8881 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8882 fprintf (dump_file
, " into ");
8885 gimple_cond_set_code (stmt
, NE_EXPR
);
8886 gimple_cond_set_lhs (stmt
, op0
);
8887 gimple_cond_set_rhs (stmt
, new_tree
);
8893 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8894 fprintf (dump_file
, "\n");
8902 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
8903 see if OP0 was set by a type conversion where the source of
8904 the conversion is another SSA_NAME with a range that fits
8905 into the range of OP0's type.
8907 If so, the conversion is redundant as the earlier SSA_NAME can be
8908 used for the comparison directly if we just massage the constant in the
8910 if (TREE_CODE (op0
) == SSA_NAME
8911 && TREE_CODE (op1
) == INTEGER_CST
)
8913 gimple def_stmt
= SSA_NAME_DEF_STMT (op0
);
8916 if (!is_gimple_assign (def_stmt
)
8917 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
8920 innerop
= gimple_assign_rhs1 (def_stmt
);
8922 if (TREE_CODE (innerop
) == SSA_NAME
8923 && !POINTER_TYPE_P (TREE_TYPE (innerop
)))
8925 value_range_t
*vr
= get_value_range (innerop
);
8927 if (range_int_cst_p (vr
)
8928 && range_fits_type_p (vr
,
8929 TYPE_PRECISION (TREE_TYPE (op0
)),
8930 TYPE_SIGN (TREE_TYPE (op0
)))
8931 && int_fits_type_p (op1
, TREE_TYPE (innerop
))
8932 /* The range must not have overflowed, or if it did overflow
8933 we must not be wrapping/trapping overflow and optimizing
8934 with strict overflow semantics. */
8935 && ((!is_negative_overflow_infinity (vr
->min
)
8936 && !is_positive_overflow_infinity (vr
->max
))
8937 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop
))))
8939 /* If the range overflowed and the user has asked for warnings
8940 when strict overflow semantics were used to optimize code,
8941 issue an appropriate warning. */
8942 if ((is_negative_overflow_infinity (vr
->min
)
8943 || is_positive_overflow_infinity (vr
->max
))
8944 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL
))
8946 location_t location
;
8948 if (!gimple_has_location (stmt
))
8949 location
= input_location
;
8951 location
= gimple_location (stmt
);
8952 warning_at (location
, OPT_Wstrict_overflow
,
8953 "assuming signed overflow does not occur when "
8954 "simplifying conditional");
8957 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
8958 gimple_cond_set_lhs (stmt
, innerop
);
8959 gimple_cond_set_rhs (stmt
, newconst
);
8968 /* Simplify a switch statement using the value range of the switch
8972 simplify_switch_using_ranges (gimple stmt
)
8974 tree op
= gimple_switch_index (stmt
);
8979 size_t i
= 0, j
= 0, n
, n2
;
8982 size_t k
= 1, l
= 0;
8984 if (TREE_CODE (op
) == SSA_NAME
)
8986 vr
= get_value_range (op
);
8988 /* We can only handle integer ranges. */
8989 if ((vr
->type
!= VR_RANGE
8990 && vr
->type
!= VR_ANTI_RANGE
)
8991 || symbolic_range_p (vr
))
8994 /* Find case label for min/max of the value range. */
8995 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
8997 else if (TREE_CODE (op
) == INTEGER_CST
)
8999 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
9013 n
= gimple_switch_num_labels (stmt
);
9015 /* Bail out if this is just all edges taken. */
9021 /* Build a new vector of taken case labels. */
9022 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
9025 /* Add the default edge, if necessary. */
9027 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
9029 for (; i
<= j
; ++i
, ++n2
)
9030 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
9032 for (; k
<= l
; ++k
, ++n2
)
9033 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
9035 /* Mark needed edges. */
9036 for (i
= 0; i
< n2
; ++i
)
9038 e
= find_edge (gimple_bb (stmt
),
9039 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
9040 e
->aux
= (void *)-1;
9043 /* Queue not needed edges for later removal. */
9044 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
9046 if (e
->aux
== (void *)-1)
9052 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9054 fprintf (dump_file
, "removing unreachable case label\n");
9056 to_remove_edges
.safe_push (e
);
9057 e
->flags
&= ~EDGE_EXECUTABLE
;
9060 /* And queue an update for the stmt. */
9063 to_update_switch_stmts
.safe_push (su
);
9067 /* Simplify an integral conversion from an SSA name in STMT. */
9070 simplify_conversion_using_ranges (gimple stmt
)
9072 tree innerop
, middleop
, finaltype
;
9074 value_range_t
*innervr
;
9075 signop inner_sgn
, middle_sgn
, final_sgn
;
9076 unsigned inner_prec
, middle_prec
, final_prec
;
9077 widest_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
9079 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
9080 if (!INTEGRAL_TYPE_P (finaltype
))
9082 middleop
= gimple_assign_rhs1 (stmt
);
9083 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
9084 if (!is_gimple_assign (def_stmt
)
9085 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9087 innerop
= gimple_assign_rhs1 (def_stmt
);
9088 if (TREE_CODE (innerop
) != SSA_NAME
9089 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
9092 /* Get the value-range of the inner operand. */
9093 innervr
= get_value_range (innerop
);
9094 if (innervr
->type
!= VR_RANGE
9095 || TREE_CODE (innervr
->min
) != INTEGER_CST
9096 || TREE_CODE (innervr
->max
) != INTEGER_CST
)
9099 /* Simulate the conversion chain to check if the result is equal if
9100 the middle conversion is removed. */
9101 innermin
= wi::to_widest (innervr
->min
);
9102 innermax
= wi::to_widest (innervr
->max
);
9104 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
9105 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
9106 final_prec
= TYPE_PRECISION (finaltype
);
9108 /* If the first conversion is not injective, the second must not
9110 if (wi::gtu_p (innermax
- innermin
,
9111 wi::mask
<widest_int
> (middle_prec
, false))
9112 && middle_prec
< final_prec
)
9114 /* We also want a medium value so that we can track the effect that
9115 narrowing conversions with sign change have. */
9116 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
9117 if (inner_sgn
== UNSIGNED
)
9118 innermed
= wi::shifted_mask
<widest_int
> (1, inner_prec
- 1, false);
9121 if (wi::cmp (innermin
, innermed
, inner_sgn
) >= 0
9122 || wi::cmp (innermed
, innermax
, inner_sgn
) >= 0)
9123 innermed
= innermin
;
9125 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
9126 middlemin
= wi::ext (innermin
, middle_prec
, middle_sgn
);
9127 middlemed
= wi::ext (innermed
, middle_prec
, middle_sgn
);
9128 middlemax
= wi::ext (innermax
, middle_prec
, middle_sgn
);
9130 /* Require that the final conversion applied to both the original
9131 and the intermediate range produces the same result. */
9132 final_sgn
= TYPE_SIGN (finaltype
);
9133 if (wi::ext (middlemin
, final_prec
, final_sgn
)
9134 != wi::ext (innermin
, final_prec
, final_sgn
)
9135 || wi::ext (middlemed
, final_prec
, final_sgn
)
9136 != wi::ext (innermed
, final_prec
, final_sgn
)
9137 || wi::ext (middlemax
, final_prec
, final_sgn
)
9138 != wi::ext (innermax
, final_prec
, final_sgn
))
9141 gimple_assign_set_rhs1 (stmt
, innerop
);
9146 /* Simplify a conversion from integral SSA name to float in STMT. */
9149 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9151 tree rhs1
= gimple_assign_rhs1 (stmt
);
9152 value_range_t
*vr
= get_value_range (rhs1
);
9153 enum machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
9154 enum machine_mode mode
;
9158 /* We can only handle constant ranges. */
9159 if (vr
->type
!= VR_RANGE
9160 || TREE_CODE (vr
->min
) != INTEGER_CST
9161 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9164 /* First check if we can use a signed type in place of an unsigned. */
9165 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
9166 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
9167 != CODE_FOR_nothing
)
9168 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
9169 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
9170 /* If we can do the conversion in the current input mode do nothing. */
9171 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
9172 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
9174 /* Otherwise search for a mode we can use, starting from the narrowest
9175 integer mode available. */
9178 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
9181 /* If we cannot do a signed conversion to float from mode
9182 or if the value-range does not fit in the signed type
9183 try with a wider mode. */
9184 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
9185 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
9188 mode
= GET_MODE_WIDER_MODE (mode
);
9189 /* But do not widen the input. Instead leave that to the
9190 optabs expansion code. */
9191 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
9194 while (mode
!= VOIDmode
);
9195 if (mode
== VOIDmode
)
9199 /* It works, insert a truncation or sign-change before the
9200 float conversion. */
9201 tem
= make_ssa_name (build_nonstandard_integer_type
9202 (GET_MODE_PRECISION (mode
), 0), NULL
);
9203 conv
= gimple_build_assign_with_ops (NOP_EXPR
, tem
, rhs1
, NULL_TREE
);
9204 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
9205 gimple_assign_set_rhs1 (stmt
, tem
);
9211 /* Simplify an internal fn call using ranges if possible. */
9214 simplify_internal_call_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9216 enum tree_code subcode
;
9217 switch (gimple_call_internal_fn (stmt
))
9219 case IFN_UBSAN_CHECK_ADD
:
9220 subcode
= PLUS_EXPR
;
9222 case IFN_UBSAN_CHECK_SUB
:
9223 subcode
= MINUS_EXPR
;
9225 case IFN_UBSAN_CHECK_MUL
:
9226 subcode
= MULT_EXPR
;
9232 value_range_t vr0
= VR_INITIALIZER
;
9233 value_range_t vr1
= VR_INITIALIZER
;
9234 tree op0
= gimple_call_arg (stmt
, 0);
9235 tree op1
= gimple_call_arg (stmt
, 1);
9237 if (TREE_CODE (op0
) == SSA_NAME
)
9238 vr0
= *get_value_range (op0
);
9239 else if (TREE_CODE (op0
) == INTEGER_CST
)
9240 set_value_range_to_value (&vr0
, op0
, NULL
);
9242 set_value_range_to_varying (&vr0
);
9244 if (TREE_CODE (op1
) == SSA_NAME
)
9245 vr1
= *get_value_range (op1
);
9246 else if (TREE_CODE (op1
) == INTEGER_CST
)
9247 set_value_range_to_value (&vr1
, op1
, NULL
);
9249 set_value_range_to_varying (&vr1
);
9251 if (!range_int_cst_p (&vr0
))
9253 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9254 optimize at least x = y + 0; x = y - 0; x = y * 0;
9255 and x = y * 1; which never overflow. */
9256 if (!range_int_cst_p (&vr1
))
9258 if (tree_int_cst_sgn (vr1
.min
) == -1)
9260 if (compare_tree_int (vr1
.max
, subcode
== MULT_EXPR
) == 1)
9263 else if (!range_int_cst_p (&vr1
))
9265 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9266 optimize at least x = 0 + y; x = 0 * y; and x = 1 * y;
9267 which never overflow. */
9268 if (subcode
== MINUS_EXPR
)
9270 if (!range_int_cst_p (&vr0
))
9272 if (tree_int_cst_sgn (vr0
.min
) == -1)
9274 if (compare_tree_int (vr0
.max
, subcode
== MULT_EXPR
) == 1)
9279 tree r1
= int_const_binop (subcode
, vr0
.min
, vr1
.min
);
9280 tree r2
= int_const_binop (subcode
, vr0
.max
, vr1
.max
);
9281 if (r1
== NULL_TREE
|| TREE_OVERFLOW (r1
)
9282 || r2
== NULL_TREE
|| TREE_OVERFLOW (r2
))
9284 if (subcode
== MULT_EXPR
)
9286 tree r3
= int_const_binop (subcode
, vr0
.min
, vr1
.max
);
9287 tree r4
= int_const_binop (subcode
, vr0
.max
, vr1
.min
);
9288 if (r3
== NULL_TREE
|| TREE_OVERFLOW (r3
)
9289 || r4
== NULL_TREE
|| TREE_OVERFLOW (r4
))
9294 gimple g
= gimple_build_assign_with_ops (subcode
, gimple_call_lhs (stmt
),
9296 gsi_replace (gsi
, g
, false);
9300 /* Simplify STMT using ranges if possible. */
9303 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
9305 gimple stmt
= gsi_stmt (*gsi
);
9306 if (is_gimple_assign (stmt
))
9308 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9309 tree rhs1
= gimple_assign_rhs1 (stmt
);
9315 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9316 if the RHS is zero or one, and the LHS are known to be boolean
9318 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9319 return simplify_truth_ops_using_ranges (gsi
, stmt
);
9322 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9323 and BIT_AND_EXPR respectively if the first operand is greater
9324 than zero and the second operand is an exact power of two. */
9325 case TRUNC_DIV_EXPR
:
9326 case TRUNC_MOD_EXPR
:
9327 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
9328 && integer_pow2p (gimple_assign_rhs2 (stmt
)))
9329 return simplify_div_or_mod_using_ranges (stmt
);
9332 /* Transform ABS (X) into X or -X as appropriate. */
9334 if (TREE_CODE (rhs1
) == SSA_NAME
9335 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9336 return simplify_abs_using_ranges (stmt
);
9341 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9342 if all the bits being cleared are already cleared or
9343 all the bits being set are already set. */
9344 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9345 return simplify_bit_ops_using_ranges (gsi
, stmt
);
9349 if (TREE_CODE (rhs1
) == SSA_NAME
9350 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9351 return simplify_conversion_using_ranges (stmt
);
9355 if (TREE_CODE (rhs1
) == SSA_NAME
9356 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9357 return simplify_float_conversion_using_ranges (gsi
, stmt
);
9364 else if (gimple_code (stmt
) == GIMPLE_COND
)
9365 return simplify_cond_using_ranges (stmt
);
9366 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
9367 return simplify_switch_using_ranges (stmt
);
9368 else if (is_gimple_call (stmt
)
9369 && gimple_call_internal_p (stmt
))
9370 return simplify_internal_call_using_ranges (gsi
, stmt
);
9375 /* If the statement pointed by SI has a predicate whose value can be
9376 computed using the value range information computed by VRP, compute
9377 its value and return true. Otherwise, return false. */
9380 fold_predicate_in (gimple_stmt_iterator
*si
)
9382 bool assignment_p
= false;
9384 gimple stmt
= gsi_stmt (*si
);
9386 if (is_gimple_assign (stmt
)
9387 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
9389 assignment_p
= true;
9390 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
9391 gimple_assign_rhs1 (stmt
),
9392 gimple_assign_rhs2 (stmt
),
9395 else if (gimple_code (stmt
) == GIMPLE_COND
)
9396 val
= vrp_evaluate_conditional (gimple_cond_code (stmt
),
9397 gimple_cond_lhs (stmt
),
9398 gimple_cond_rhs (stmt
),
9406 val
= fold_convert (gimple_expr_type (stmt
), val
);
9410 fprintf (dump_file
, "Folding predicate ");
9411 print_gimple_expr (dump_file
, stmt
, 0, 0);
9412 fprintf (dump_file
, " to ");
9413 print_generic_expr (dump_file
, val
, 0);
9414 fprintf (dump_file
, "\n");
9417 if (is_gimple_assign (stmt
))
9418 gimple_assign_set_rhs_from_tree (si
, val
);
9421 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
9422 if (integer_zerop (val
))
9423 gimple_cond_make_false (stmt
);
9424 else if (integer_onep (val
))
9425 gimple_cond_make_true (stmt
);
9436 /* Callback for substitute_and_fold folding the stmt at *SI. */
9439 vrp_fold_stmt (gimple_stmt_iterator
*si
)
9441 if (fold_predicate_in (si
))
9444 return simplify_stmt_using_ranges (si
);
9447 /* Stack of dest,src equivalency pairs that need to be restored after
9448 each attempt to thread a block's incoming edge to an outgoing edge.
9450 A NULL entry is used to mark the end of pairs which need to be
9452 static vec
<tree
> equiv_stack
;
9454 /* A trivial wrapper so that we can present the generic jump threading
9455 code with a simple API for simplifying statements. STMT is the
9456 statement we want to simplify, WITHIN_STMT provides the location
9457 for any overflow warnings. */
9460 simplify_stmt_for_jump_threading (gimple stmt
, gimple within_stmt
)
9462 if (gimple_code (stmt
) == GIMPLE_COND
)
9463 return vrp_evaluate_conditional (gimple_cond_code (stmt
),
9464 gimple_cond_lhs (stmt
),
9465 gimple_cond_rhs (stmt
), within_stmt
);
9467 if (gimple_code (stmt
) == GIMPLE_ASSIGN
)
9469 value_range_t new_vr
= VR_INITIALIZER
;
9470 tree lhs
= gimple_assign_lhs (stmt
);
9472 if (TREE_CODE (lhs
) == SSA_NAME
9473 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
9474 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
9476 extract_range_from_assignment (&new_vr
, stmt
);
9477 if (range_int_cst_singleton_p (&new_vr
))
9485 /* Blocks which have more than one predecessor and more than
9486 one successor present jump threading opportunities, i.e.,
9487 when the block is reached from a specific predecessor, we
9488 may be able to determine which of the outgoing edges will
9489 be traversed. When this optimization applies, we are able
9490 to avoid conditionals at runtime and we may expose secondary
9491 optimization opportunities.
9493 This routine is effectively a driver for the generic jump
9494 threading code. It basically just presents the generic code
9495 with edges that may be suitable for jump threading.
9497 Unlike DOM, we do not iterate VRP if jump threading was successful.
9498 While iterating may expose new opportunities for VRP, it is expected
9499 those opportunities would be very limited and the compile time cost
9500 to expose those opportunities would be significant.
9502 As jump threading opportunities are discovered, they are registered
9503 for later realization. */
9506 identify_jump_threads (void)
9513 /* Ugh. When substituting values earlier in this pass we can
9514 wipe the dominance information. So rebuild the dominator
9515 information as we need it within the jump threading code. */
9516 calculate_dominance_info (CDI_DOMINATORS
);
9518 /* We do not allow VRP information to be used for jump threading
9519 across a back edge in the CFG. Otherwise it becomes too
9520 difficult to avoid eliminating loop exit tests. Of course
9521 EDGE_DFS_BACK is not accurate at this time so we have to
9523 mark_dfs_back_edges ();
9525 /* Do not thread across edges we are about to remove. Just marking
9526 them as EDGE_DFS_BACK will do. */
9527 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
9528 e
->flags
|= EDGE_DFS_BACK
;
9530 /* Allocate our unwinder stack to unwind any temporary equivalences
9531 that might be recorded. */
9532 equiv_stack
.create (20);
9534 /* To avoid lots of silly node creation, we create a single
9535 conditional and just modify it in-place when attempting to
9537 dummy
= gimple_build_cond (EQ_EXPR
,
9538 integer_zero_node
, integer_zero_node
,
9541 /* Walk through all the blocks finding those which present a
9542 potential jump threading opportunity. We could set this up
9543 as a dominator walker and record data during the walk, but
9544 I doubt it's worth the effort for the classes of jump
9545 threading opportunities we are trying to identify at this
9546 point in compilation. */
9547 FOR_EACH_BB_FN (bb
, cfun
)
9551 /* If the generic jump threading code does not find this block
9552 interesting, then there is nothing to do. */
9553 if (! potentially_threadable_block (bb
))
9556 /* We only care about blocks ending in a COND_EXPR. While there
9557 may be some value in handling SWITCH_EXPR here, I doubt it's
9558 terribly important. */
9559 last
= gsi_stmt (gsi_last_bb (bb
));
9561 /* We're basically looking for a switch or any kind of conditional with
9562 integral or pointer type arguments. Note the type of the second
9563 argument will be the same as the first argument, so no need to
9564 check it explicitly. */
9565 if (gimple_code (last
) == GIMPLE_SWITCH
9566 || (gimple_code (last
) == GIMPLE_COND
9567 && TREE_CODE (gimple_cond_lhs (last
)) == SSA_NAME
9568 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
)))
9569 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
))))
9570 && (TREE_CODE (gimple_cond_rhs (last
)) == SSA_NAME
9571 || is_gimple_min_invariant (gimple_cond_rhs (last
)))))
9575 /* We've got a block with multiple predecessors and multiple
9576 successors which also ends in a suitable conditional or
9577 switch statement. For each predecessor, see if we can thread
9578 it to a specific successor. */
9579 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
9581 /* Do not thread across back edges or abnormal edges
9583 if (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
9586 thread_across_edge (dummy
, e
, true, &equiv_stack
,
9587 simplify_stmt_for_jump_threading
);
9592 /* We do not actually update the CFG or SSA graphs at this point as
9593 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9594 handle ASSERT_EXPRs gracefully. */
9597 /* We identified all the jump threading opportunities earlier, but could
9598 not transform the CFG at that time. This routine transforms the
9599 CFG and arranges for the dominator tree to be rebuilt if necessary.
9601 Note the SSA graph update will occur during the normal TODO
9602 processing by the pass manager. */
9604 finalize_jump_threads (void)
9606 thread_through_all_blocks (false);
9607 equiv_stack
.release ();
9611 /* Traverse all the blocks folding conditionals with known ranges. */
9618 values_propagated
= true;
9622 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
9623 dump_all_value_ranges (dump_file
);
9624 fprintf (dump_file
, "\n");
9627 substitute_and_fold (op_with_constant_singleton_value_range
,
9628 vrp_fold_stmt
, false);
9630 if (warn_array_bounds
)
9631 check_all_array_refs ();
9633 /* We must identify jump threading opportunities before we release
9634 the datastructures built by VRP. */
9635 identify_jump_threads ();
9637 /* Set value range to non pointer SSA_NAMEs. */
9638 for (i
= 0; i
< num_vr_values
; i
++)
9641 tree name
= ssa_name (i
);
9644 || POINTER_TYPE_P (TREE_TYPE (name
))
9645 || (vr_value
[i
]->type
== VR_VARYING
)
9646 || (vr_value
[i
]->type
== VR_UNDEFINED
))
9649 if ((TREE_CODE (vr_value
[i
]->min
) == INTEGER_CST
)
9650 && (TREE_CODE (vr_value
[i
]->max
) == INTEGER_CST
)
9651 && (vr_value
[i
]->type
== VR_RANGE
9652 || vr_value
[i
]->type
== VR_ANTI_RANGE
))
9653 set_range_info (name
, vr_value
[i
]->type
, vr_value
[i
]->min
,
9657 /* Free allocated memory. */
9658 for (i
= 0; i
< num_vr_values
; i
++)
9661 BITMAP_FREE (vr_value
[i
]->equiv
);
9666 free (vr_phi_edge_counts
);
9668 /* So that we can distinguish between VRP data being available
9669 and not available. */
9671 vr_phi_edge_counts
= NULL
;
9675 /* Main entry point to VRP (Value Range Propagation). This pass is
9676 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9677 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9678 Programming Language Design and Implementation, pp. 67-78, 1995.
9679 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9681 This is essentially an SSA-CCP pass modified to deal with ranges
9682 instead of constants.
9684 While propagating ranges, we may find that two or more SSA name
9685 have equivalent, though distinct ranges. For instance,
9688 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9690 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9694 In the code above, pointer p_5 has range [q_2, q_2], but from the
9695 code we can also determine that p_5 cannot be NULL and, if q_2 had
9696 a non-varying range, p_5's range should also be compatible with it.
9698 These equivalences are created by two expressions: ASSERT_EXPR and
9699 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9700 result of another assertion, then we can use the fact that p_5 and
9701 p_4 are equivalent when evaluating p_5's range.
9703 Together with value ranges, we also propagate these equivalences
9704 between names so that we can take advantage of information from
9705 multiple ranges when doing final replacement. Note that this
9706 equivalency relation is transitive but not symmetric.
9708 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9709 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9710 in contexts where that assertion does not hold (e.g., in line 6).
9712 TODO, the main difference between this pass and Patterson's is that
9713 we do not propagate edge probabilities. We only compute whether
9714 edges can be taken or not. That is, instead of having a spectrum
9715 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9716 DON'T KNOW. In the future, it may be worthwhile to propagate
9717 probabilities to aid branch prediction. */
9726 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
9727 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
9730 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
9731 Inserting assertions may split edges which will invalidate
9733 insert_range_assertions ();
9735 to_remove_edges
.create (10);
9736 to_update_switch_stmts
.create (5);
9737 threadedge_initialize_values ();
9739 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
9740 mark_dfs_back_edges ();
9743 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
9746 free_numbers_of_iterations_estimates ();
9748 /* ASSERT_EXPRs must be removed before finalizing jump threads
9749 as finalizing jump threads calls the CFG cleanup code which
9750 does not properly handle ASSERT_EXPRs. */
9751 remove_range_assertions ();
9753 /* If we exposed any new variables, go ahead and put them into
9754 SSA form now, before we handle jump threading. This simplifies
9755 interactions between rewriting of _DECL nodes into SSA form
9756 and rewriting SSA_NAME nodes into SSA form after block
9757 duplication and CFG manipulation. */
9758 update_ssa (TODO_update_ssa
);
9760 finalize_jump_threads ();
9762 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
9763 CFG in a broken state and requires a cfg_cleanup run. */
9764 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
9766 /* Update SWITCH_EXPR case label vector. */
9767 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
9770 size_t n
= TREE_VEC_LENGTH (su
->vec
);
9772 gimple_switch_set_num_labels (su
->stmt
, n
);
9773 for (j
= 0; j
< n
; j
++)
9774 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
9775 /* As we may have replaced the default label with a regular one
9776 make sure to make it a real default label again. This ensures
9777 optimal expansion. */
9778 label
= gimple_switch_label (su
->stmt
, 0);
9779 CASE_LOW (label
) = NULL_TREE
;
9780 CASE_HIGH (label
) = NULL_TREE
;
9783 if (to_remove_edges
.length () > 0)
9785 free_dominance_info (CDI_DOMINATORS
);
9786 loops_state_set (LOOPS_NEED_FIXUP
);
9789 to_remove_edges
.release ();
9790 to_update_switch_stmts
.release ();
9791 threadedge_finalize_values ();
9794 loop_optimizer_finalize ();
9800 const pass_data pass_data_vrp
=
9802 GIMPLE_PASS
, /* type */
9804 OPTGROUP_NONE
, /* optinfo_flags */
9805 true, /* has_execute */
9806 TV_TREE_VRP
, /* tv_id */
9807 PROP_ssa
, /* properties_required */
9808 0, /* properties_provided */
9809 0, /* properties_destroyed */
9810 0, /* todo_flags_start */
9811 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
9814 class pass_vrp
: public gimple_opt_pass
9817 pass_vrp (gcc::context
*ctxt
)
9818 : gimple_opt_pass (pass_data_vrp
, ctxt
)
9821 /* opt_pass methods: */
9822 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
9823 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
9824 virtual unsigned int execute (function
*) { return execute_vrp (); }
9826 }; // class pass_vrp
9831 make_pass_vrp (gcc::context
*ctxt
)
9833 return new pass_vrp (ctxt
);