1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2017 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"
25 #include "insn-codes.h"
30 #include "tree-pass.h"
32 #include "optabs-tree.h"
33 #include "gimple-pretty-print.h"
34 #include "diagnostic-core.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
40 #include "gimple-fold.h"
42 #include "gimple-iterator.h"
43 #include "gimple-walk.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"
52 #include "tree-scalar-evolution.h"
53 #include "tree-ssa-propagate.h"
54 #include "tree-chrec.h"
55 #include "tree-ssa-threadupdate.h"
56 #include "tree-ssa-scopedtables.h"
57 #include "tree-ssa-threadedge.h"
58 #include "omp-general.h"
60 #include "case-cfn-macros.h"
62 #include "alloc-pool.h"
64 #include "tree-cfgcleanup.h"
66 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
68 /* Allocation pools for tree-vrp allocations. */
69 static object_allocator
<value_range
> vrp_value_range_pool ("Tree VRP value ranges");
70 static bitmap_obstack vrp_equiv_obstack
;
72 /* Set of SSA names found live during the RPO traversal of the function
73 for still active basic-blocks. */
76 /* Return true if the SSA name NAME is live on the edge E. */
79 live_on_edge (edge e
, tree name
)
81 return (live
[e
->dest
->index
]
82 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
85 /* Local functions. */
86 static int compare_values (tree val1
, tree val2
);
87 static int compare_values_warnv (tree val1
, tree val2
, bool *);
88 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
89 tree
, tree
, bool, bool *,
94 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
95 enum tree_code comp_code
;
97 /* Name to register the assert for. */
100 /* Value being compared against. */
103 /* Expression to compare. */
107 /* Location information for ASSERT_EXPRs. Each instance of this
108 structure describes an ASSERT_EXPR for an SSA name. Since a single
109 SSA name may have more than one assertion associated with it, these
110 locations are kept in a linked list attached to the corresponding
114 /* Basic block where the assertion would be inserted. */
117 /* Some assertions need to be inserted on an edge (e.g., assertions
118 generated by COND_EXPRs). In those cases, BB will be NULL. */
121 /* Pointer to the statement that generated this assertion. */
122 gimple_stmt_iterator si
;
124 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
125 enum tree_code comp_code
;
127 /* Value being compared against. */
130 /* Expression to compare. */
133 /* Next node in the linked list. */
137 /* If bit I is present, it means that SSA name N_i has a list of
138 assertions that should be inserted in the IL. */
139 static bitmap need_assert_for
;
141 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
142 holds a list of ASSERT_LOCUS_T nodes that describe where
143 ASSERT_EXPRs for SSA name N_I should be inserted. */
144 static assert_locus
**asserts_for
;
146 /* Value range array. After propagation, VR_VALUE[I] holds the range
147 of values that SSA name N_I may take. */
148 static unsigned num_vr_values
;
149 static value_range
**vr_value
;
150 static bool values_propagated
;
152 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
153 number of executable edges we saw the last time we visited the
155 static int *vr_phi_edge_counts
;
157 struct switch_update
{
162 static vec
<edge
> to_remove_edges
;
163 static vec
<switch_update
> to_update_switch_stmts
;
166 /* Return the maximum value for TYPE. */
169 vrp_val_max (const_tree type
)
171 if (!INTEGRAL_TYPE_P (type
))
174 return TYPE_MAX_VALUE (type
);
177 /* Return the minimum value for TYPE. */
180 vrp_val_min (const_tree type
)
182 if (!INTEGRAL_TYPE_P (type
))
185 return TYPE_MIN_VALUE (type
);
188 /* Return whether VAL is equal to the maximum value of its type. This
189 will be true for a positive overflow infinity. We can't do a
190 simple equality comparison with TYPE_MAX_VALUE because C typedefs
191 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
192 to the integer constant with the same value in the type. */
195 vrp_val_is_max (const_tree val
)
197 tree type_max
= vrp_val_max (TREE_TYPE (val
));
198 return (val
== type_max
199 || (type_max
!= NULL_TREE
200 && operand_equal_p (val
, type_max
, 0)));
203 /* Return whether VAL is equal to the minimum value of its type. This
204 will be true for a negative overflow infinity. */
207 vrp_val_is_min (const_tree val
)
209 tree type_min
= vrp_val_min (TREE_TYPE (val
));
210 return (val
== type_min
211 || (type_min
!= NULL_TREE
212 && operand_equal_p (val
, type_min
, 0)));
216 /* Set value range VR to VR_UNDEFINED. */
219 set_value_range_to_undefined (value_range
*vr
)
221 vr
->type
= VR_UNDEFINED
;
222 vr
->min
= vr
->max
= NULL_TREE
;
224 bitmap_clear (vr
->equiv
);
228 /* Set value range VR to VR_VARYING. */
231 set_value_range_to_varying (value_range
*vr
)
233 vr
->type
= VR_VARYING
;
234 vr
->min
= vr
->max
= NULL_TREE
;
236 bitmap_clear (vr
->equiv
);
240 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
243 set_value_range (value_range
*vr
, enum value_range_type t
, tree min
,
244 tree max
, bitmap equiv
)
246 /* Check the validity of the range. */
248 && (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
))
252 gcc_assert (min
&& max
);
254 gcc_assert (!TREE_OVERFLOW_P (min
) && !TREE_OVERFLOW_P (max
));
256 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
257 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
259 cmp
= compare_values (min
, max
);
260 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
264 && (t
== VR_UNDEFINED
|| t
== VR_VARYING
))
266 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
267 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
274 /* Since updating the equivalence set involves deep copying the
275 bitmaps, only do it if absolutely necessary. */
276 if (vr
->equiv
== NULL
278 vr
->equiv
= BITMAP_ALLOC (&vrp_equiv_obstack
);
280 if (equiv
!= vr
->equiv
)
282 if (equiv
&& !bitmap_empty_p (equiv
))
283 bitmap_copy (vr
->equiv
, equiv
);
285 bitmap_clear (vr
->equiv
);
290 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
291 This means adjusting T, MIN and MAX representing the case of a
292 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
293 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
294 In corner cases where MAX+1 or MIN-1 wraps this will fall back
296 This routine exists to ease canonicalization in the case where we
297 extract ranges from var + CST op limit. */
300 set_and_canonicalize_value_range (value_range
*vr
, enum value_range_type t
,
301 tree min
, tree max
, bitmap equiv
)
303 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
304 if (t
== VR_UNDEFINED
)
306 set_value_range_to_undefined (vr
);
309 else if (t
== VR_VARYING
)
311 set_value_range_to_varying (vr
);
315 /* Nothing to canonicalize for symbolic ranges. */
316 if (TREE_CODE (min
) != INTEGER_CST
317 || TREE_CODE (max
) != INTEGER_CST
)
319 set_value_range (vr
, t
, min
, max
, equiv
);
323 /* Wrong order for min and max, to swap them and the VR type we need
325 if (tree_int_cst_lt (max
, min
))
329 /* For one bit precision if max < min, then the swapped
330 range covers all values, so for VR_RANGE it is varying and
331 for VR_ANTI_RANGE empty range, so drop to varying as well. */
332 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
334 set_value_range_to_varying (vr
);
338 one
= build_int_cst (TREE_TYPE (min
), 1);
339 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
340 max
= int_const_binop (MINUS_EXPR
, min
, one
);
343 /* There's one corner case, if we had [C+1, C] before we now have
344 that again. But this represents an empty value range, so drop
345 to varying in this case. */
346 if (tree_int_cst_lt (max
, min
))
348 set_value_range_to_varying (vr
);
352 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
355 /* Anti-ranges that can be represented as ranges should be so. */
356 if (t
== VR_ANTI_RANGE
)
358 bool is_min
= vrp_val_is_min (min
);
359 bool is_max
= vrp_val_is_max (max
);
361 if (is_min
&& is_max
)
363 /* We cannot deal with empty ranges, drop to varying.
364 ??? This could be VR_UNDEFINED instead. */
365 set_value_range_to_varying (vr
);
368 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
369 && (is_min
|| is_max
))
371 /* Non-empty boolean ranges can always be represented
372 as a singleton range. */
374 min
= max
= vrp_val_max (TREE_TYPE (min
));
376 min
= max
= vrp_val_min (TREE_TYPE (min
));
380 /* As a special exception preserve non-null ranges. */
381 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
382 && integer_zerop (max
)))
384 tree one
= build_int_cst (TREE_TYPE (max
), 1);
385 min
= int_const_binop (PLUS_EXPR
, max
, one
);
386 max
= vrp_val_max (TREE_TYPE (max
));
391 tree one
= build_int_cst (TREE_TYPE (min
), 1);
392 max
= int_const_binop (MINUS_EXPR
, min
, one
);
393 min
= vrp_val_min (TREE_TYPE (min
));
398 /* Do not drop [-INF(OVF), +INF(OVF)] to varying. (OVF) has to be sticky
399 to make sure VRP iteration terminates, otherwise we can get into
402 set_value_range (vr
, t
, min
, max
, equiv
);
405 /* Copy value range FROM into value range TO. */
408 copy_value_range (value_range
*to
, value_range
*from
)
410 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
413 /* Set value range VR to a single value. This function is only called
414 with values we get from statements, and exists to clear the
415 TREE_OVERFLOW flag so that we don't think we have an overflow
416 infinity when we shouldn't. */
419 set_value_range_to_value (value_range
*vr
, tree val
, bitmap equiv
)
421 gcc_assert (is_gimple_min_invariant (val
));
422 if (TREE_OVERFLOW_P (val
))
423 val
= drop_tree_overflow (val
);
424 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
427 /* Set value range VR to a non-negative range of type TYPE.
428 OVERFLOW_INFINITY indicates whether to use an overflow infinity
429 rather than TYPE_MAX_VALUE; this should be true if we determine
430 that the range is nonnegative based on the assumption that signed
431 overflow does not occur. */
434 set_value_range_to_nonnegative (value_range
*vr
, tree type
)
436 tree zero
= build_int_cst (type
, 0);
437 set_value_range (vr
, VR_RANGE
, zero
, vrp_val_max (type
), vr
->equiv
);
440 /* Set value range VR to a non-NULL range of type TYPE. */
443 set_value_range_to_nonnull (value_range
*vr
, tree type
)
445 tree zero
= build_int_cst (type
, 0);
446 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
450 /* Set value range VR to a NULL range of type TYPE. */
453 set_value_range_to_null (value_range
*vr
, tree type
)
455 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
459 /* Set value range VR to a range of a truthvalue of type TYPE. */
462 set_value_range_to_truthvalue (value_range
*vr
, tree type
)
464 if (TYPE_PRECISION (type
) == 1)
465 set_value_range_to_varying (vr
);
467 set_value_range (vr
, VR_RANGE
,
468 build_int_cst (type
, 0), build_int_cst (type
, 1),
473 /* If abs (min) < abs (max), set VR to [-max, max], if
474 abs (min) >= abs (max), set VR to [-min, min]. */
477 abs_extent_range (value_range
*vr
, tree min
, tree max
)
481 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
482 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
483 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
484 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
485 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
486 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
487 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
489 set_value_range_to_varying (vr
);
492 cmp
= compare_values (min
, max
);
494 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
495 else if (cmp
== 0 || cmp
== 1)
498 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
502 set_value_range_to_varying (vr
);
505 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
509 /* Return value range information for VAR.
511 If we have no values ranges recorded (ie, VRP is not running), then
512 return NULL. Otherwise create an empty range if none existed for VAR. */
515 get_value_range (const_tree var
)
517 static const value_range vr_const_varying
518 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
521 unsigned ver
= SSA_NAME_VERSION (var
);
523 /* If we have no recorded ranges, then return NULL. */
527 /* If we query the range for a new SSA name return an unmodifiable VARYING.
528 We should get here at most from the substitute-and-fold stage which
529 will never try to change values. */
530 if (ver
>= num_vr_values
)
531 return CONST_CAST (value_range
*, &vr_const_varying
);
537 /* After propagation finished do not allocate new value-ranges. */
538 if (values_propagated
)
539 return CONST_CAST (value_range
*, &vr_const_varying
);
541 /* Create a default value range. */
542 vr_value
[ver
] = vr
= vrp_value_range_pool
.allocate ();
543 memset (vr
, 0, sizeof (*vr
));
545 /* Defer allocating the equivalence set. */
548 /* If VAR is a default definition of a parameter, the variable can
549 take any value in VAR's type. */
550 if (SSA_NAME_IS_DEFAULT_DEF (var
))
552 sym
= SSA_NAME_VAR (var
);
553 if (TREE_CODE (sym
) == PARM_DECL
)
555 /* Try to use the "nonnull" attribute to create ~[0, 0]
556 anti-ranges for pointers. Note that this is only valid with
557 default definitions of PARM_DECLs. */
558 if (POINTER_TYPE_P (TREE_TYPE (sym
))
559 && (nonnull_arg_p (sym
)
560 || get_ptr_nonnull (var
)))
561 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
562 else if (INTEGRAL_TYPE_P (TREE_TYPE (sym
)))
565 value_range_type rtype
= get_range_info (var
, &min
, &max
);
566 if (rtype
== VR_RANGE
|| rtype
== VR_ANTI_RANGE
)
567 set_value_range (vr
, rtype
,
568 wide_int_to_tree (TREE_TYPE (var
), min
),
569 wide_int_to_tree (TREE_TYPE (var
), max
),
572 set_value_range_to_varying (vr
);
575 set_value_range_to_varying (vr
);
577 else if (TREE_CODE (sym
) == RESULT_DECL
578 && DECL_BY_REFERENCE (sym
))
579 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
585 /* Set value-ranges of all SSA names defined by STMT to varying. */
588 set_defs_to_varying (gimple
*stmt
)
592 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
594 value_range
*vr
= get_value_range (def
);
595 /* Avoid writing to vr_const_varying get_value_range may return. */
596 if (vr
->type
!= VR_VARYING
)
597 set_value_range_to_varying (vr
);
602 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
605 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
609 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
614 /* Return true, if the bitmaps B1 and B2 are equal. */
617 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
620 || ((!b1
|| bitmap_empty_p (b1
))
621 && (!b2
|| bitmap_empty_p (b2
)))
623 && bitmap_equal_p (b1
, b2
)));
626 /* Update the value range and equivalence set for variable VAR to
627 NEW_VR. Return true if NEW_VR is different from VAR's previous
630 NOTE: This function assumes that NEW_VR is a temporary value range
631 object created for the sole purpose of updating VAR's range. The
632 storage used by the equivalence set from NEW_VR will be freed by
633 this function. Do not call update_value_range when NEW_VR
634 is the range object associated with another SSA name. */
637 update_value_range (const_tree var
, value_range
*new_vr
)
642 /* If there is a value-range on the SSA name from earlier analysis
644 if (INTEGRAL_TYPE_P (TREE_TYPE (var
)))
647 value_range_type rtype
= get_range_info (var
, &min
, &max
);
648 if (rtype
== VR_RANGE
|| rtype
== VR_ANTI_RANGE
)
651 nr_min
= wide_int_to_tree (TREE_TYPE (var
), min
);
652 nr_max
= wide_int_to_tree (TREE_TYPE (var
), max
);
653 value_range nr
= VR_INITIALIZER
;
654 set_and_canonicalize_value_range (&nr
, rtype
, nr_min
, nr_max
, NULL
);
655 vrp_intersect_ranges (new_vr
, &nr
);
659 /* Update the value range, if necessary. */
660 old_vr
= get_value_range (var
);
661 is_new
= old_vr
->type
!= new_vr
->type
662 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
663 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
664 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
668 /* Do not allow transitions up the lattice. The following
669 is slightly more awkward than just new_vr->type < old_vr->type
670 because VR_RANGE and VR_ANTI_RANGE need to be considered
671 the same. We may not have is_new when transitioning to
672 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
674 if (new_vr
->type
== VR_UNDEFINED
)
676 BITMAP_FREE (new_vr
->equiv
);
677 set_value_range_to_varying (old_vr
);
678 set_value_range_to_varying (new_vr
);
682 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
686 BITMAP_FREE (new_vr
->equiv
);
692 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
693 point where equivalence processing can be turned on/off. */
696 add_equivalence (bitmap
*equiv
, const_tree var
)
698 unsigned ver
= SSA_NAME_VERSION (var
);
699 value_range
*vr
= get_value_range (var
);
702 *equiv
= BITMAP_ALLOC (&vrp_equiv_obstack
);
703 bitmap_set_bit (*equiv
, ver
);
705 bitmap_ior_into (*equiv
, vr
->equiv
);
709 /* Return true if VR is ~[0, 0]. */
712 range_is_nonnull (value_range
*vr
)
714 return vr
->type
== VR_ANTI_RANGE
715 && integer_zerop (vr
->min
)
716 && integer_zerop (vr
->max
);
720 /* Return true if VR is [0, 0]. */
723 range_is_null (value_range
*vr
)
725 return vr
->type
== VR_RANGE
726 && integer_zerop (vr
->min
)
727 && integer_zerop (vr
->max
);
730 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
734 range_int_cst_p (value_range
*vr
)
736 return (vr
->type
== VR_RANGE
737 && TREE_CODE (vr
->max
) == INTEGER_CST
738 && TREE_CODE (vr
->min
) == INTEGER_CST
);
741 /* Return true if VR is a INTEGER_CST singleton. */
744 range_int_cst_singleton_p (value_range
*vr
)
746 return (range_int_cst_p (vr
)
747 && tree_int_cst_equal (vr
->min
, vr
->max
));
750 /* Return true if value range VR involves at least one symbol. */
753 symbolic_range_p (value_range
*vr
)
755 return (!is_gimple_min_invariant (vr
->min
)
756 || !is_gimple_min_invariant (vr
->max
));
759 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
760 otherwise. We only handle additive operations and set NEG to true if the
761 symbol is negated and INV to the invariant part, if any. */
764 get_single_symbol (tree t
, bool *neg
, tree
*inv
)
772 if (TREE_CODE (t
) == PLUS_EXPR
773 || TREE_CODE (t
) == POINTER_PLUS_EXPR
774 || TREE_CODE (t
) == MINUS_EXPR
)
776 if (is_gimple_min_invariant (TREE_OPERAND (t
, 0)))
778 neg_
= (TREE_CODE (t
) == MINUS_EXPR
);
779 inv_
= TREE_OPERAND (t
, 0);
780 t
= TREE_OPERAND (t
, 1);
782 else if (is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
785 inv_
= TREE_OPERAND (t
, 1);
786 t
= TREE_OPERAND (t
, 0);
797 if (TREE_CODE (t
) == NEGATE_EXPR
)
799 t
= TREE_OPERAND (t
, 0);
803 if (TREE_CODE (t
) != SSA_NAME
)
811 /* The reverse operation: build a symbolic expression with TYPE
812 from symbol SYM, negated according to NEG, and invariant INV. */
815 build_symbolic_expr (tree type
, tree sym
, bool neg
, tree inv
)
817 const bool pointer_p
= POINTER_TYPE_P (type
);
821 t
= build1 (NEGATE_EXPR
, type
, t
);
823 if (integer_zerop (inv
))
826 return build2 (pointer_p
? POINTER_PLUS_EXPR
: PLUS_EXPR
, type
, t
, inv
);
829 /* Return true if value range VR involves exactly one symbol SYM. */
832 symbolic_range_based_on_p (value_range
*vr
, const_tree sym
)
834 bool neg
, min_has_symbol
, max_has_symbol
;
837 if (is_gimple_min_invariant (vr
->min
))
838 min_has_symbol
= false;
839 else if (get_single_symbol (vr
->min
, &neg
, &inv
) == sym
)
840 min_has_symbol
= true;
844 if (is_gimple_min_invariant (vr
->max
))
845 max_has_symbol
= false;
846 else if (get_single_symbol (vr
->max
, &neg
, &inv
) == sym
)
847 max_has_symbol
= true;
851 return (min_has_symbol
|| max_has_symbol
);
854 /* Return true if the result of assignment STMT is know to be non-zero.
855 If the return value is based on the assumption that signed overflow is
856 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
857 *STRICT_OVERFLOW_P.*/
860 gimple_assign_nonzero_warnv_p (gimple
*stmt
, bool *strict_overflow_p
)
862 enum tree_code code
= gimple_assign_rhs_code (stmt
);
863 switch (get_gimple_rhs_class (code
))
865 case GIMPLE_UNARY_RHS
:
866 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
867 gimple_expr_type (stmt
),
868 gimple_assign_rhs1 (stmt
),
870 case GIMPLE_BINARY_RHS
:
871 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
872 gimple_expr_type (stmt
),
873 gimple_assign_rhs1 (stmt
),
874 gimple_assign_rhs2 (stmt
),
876 case GIMPLE_TERNARY_RHS
:
878 case GIMPLE_SINGLE_RHS
:
879 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
881 case GIMPLE_INVALID_RHS
:
888 /* Return true if STMT is known to compute a non-zero value.
889 If the return value is based on the assumption that signed overflow is
890 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
891 *STRICT_OVERFLOW_P.*/
894 gimple_stmt_nonzero_warnv_p (gimple
*stmt
, bool *strict_overflow_p
)
896 switch (gimple_code (stmt
))
899 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
902 tree fndecl
= gimple_call_fndecl (stmt
);
903 if (!fndecl
) return false;
904 if (flag_delete_null_pointer_checks
&& !flag_check_new
905 && DECL_IS_OPERATOR_NEW (fndecl
)
906 && !TREE_NOTHROW (fndecl
))
908 /* References are always non-NULL. */
909 if (flag_delete_null_pointer_checks
910 && TREE_CODE (TREE_TYPE (fndecl
)) == REFERENCE_TYPE
)
912 if (flag_delete_null_pointer_checks
&&
913 lookup_attribute ("returns_nonnull",
914 TYPE_ATTRIBUTES (gimple_call_fntype (stmt
))))
917 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
918 unsigned rf
= gimple_call_return_flags (call_stmt
);
919 if (rf
& ERF_RETURNS_ARG
)
921 unsigned argnum
= rf
& ERF_RETURN_ARG_MASK
;
922 if (argnum
< gimple_call_num_args (call_stmt
))
924 tree arg
= gimple_call_arg (call_stmt
, argnum
);
926 && infer_nonnull_range_by_attribute (stmt
, arg
))
930 return gimple_alloca_call_p (stmt
);
937 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
941 vrp_stmt_computes_nonzero (gimple
*stmt
, bool *strict_overflow_p
)
943 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
946 /* If we have an expression of the form &X->a, then the expression
947 is nonnull if X is nonnull. */
948 if (is_gimple_assign (stmt
)
949 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
951 tree expr
= gimple_assign_rhs1 (stmt
);
952 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
954 if (base
!= NULL_TREE
955 && TREE_CODE (base
) == MEM_REF
956 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
958 value_range
*vr
= get_value_range (TREE_OPERAND (base
, 0));
959 if (range_is_nonnull (vr
))
967 /* Returns true if EXPR is a valid value (as expected by compare_values) --
968 a gimple invariant, or SSA_NAME +- CST. */
971 valid_value_p (tree expr
)
973 if (TREE_CODE (expr
) == SSA_NAME
)
976 if (TREE_CODE (expr
) == PLUS_EXPR
977 || TREE_CODE (expr
) == MINUS_EXPR
)
978 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
979 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
981 return is_gimple_min_invariant (expr
);
987 -2 if those are incomparable. */
989 operand_less_p (tree val
, tree val2
)
991 /* LT is folded faster than GE and others. Inline the common case. */
992 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
993 return tree_int_cst_lt (val
, val2
);
998 fold_defer_overflow_warnings ();
1000 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1002 fold_undefer_and_ignore_overflow_warnings ();
1005 || TREE_CODE (tcmp
) != INTEGER_CST
)
1008 if (!integer_zerop (tcmp
))
1015 /* Compare two values VAL1 and VAL2. Return
1017 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1020 +1 if VAL1 > VAL2, and
1023 This is similar to tree_int_cst_compare but supports pointer values
1024 and values that cannot be compared at compile time.
1026 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1027 true if the return value is only valid if we assume that signed
1028 overflow is undefined. */
1031 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1036 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1038 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1039 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1041 /* Convert the two values into the same type. This is needed because
1042 sizetype causes sign extension even for unsigned types. */
1043 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1044 STRIP_USELESS_TYPE_CONVERSION (val2
);
1046 const bool overflow_undefined
1047 = INTEGRAL_TYPE_P (TREE_TYPE (val1
))
1048 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
));
1051 tree sym1
= get_single_symbol (val1
, &neg1
, &inv1
);
1052 tree sym2
= get_single_symbol (val2
, &neg2
, &inv2
);
1054 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
1055 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
1058 /* Both values must use the same name with the same sign. */
1059 if (sym1
!= sym2
|| neg1
!= neg2
)
1062 /* [-]NAME + CST == [-]NAME + CST. */
1066 /* If overflow is defined we cannot simplify more. */
1067 if (!overflow_undefined
)
1070 if (strict_overflow_p
!= NULL
1071 && (!inv1
|| !TREE_NO_WARNING (val1
))
1072 && (!inv2
|| !TREE_NO_WARNING (val2
)))
1073 *strict_overflow_p
= true;
1076 inv1
= build_int_cst (TREE_TYPE (val1
), 0);
1078 inv2
= build_int_cst (TREE_TYPE (val2
), 0);
1080 return compare_values_warnv (inv1
, inv2
, strict_overflow_p
);
1083 const bool cst1
= is_gimple_min_invariant (val1
);
1084 const bool cst2
= is_gimple_min_invariant (val2
);
1086 /* If one is of the form '[-]NAME + CST' and the other is constant, then
1087 it might be possible to say something depending on the constants. */
1088 if ((sym1
&& inv1
&& cst2
) || (sym2
&& inv2
&& cst1
))
1090 if (!overflow_undefined
)
1093 if (strict_overflow_p
!= NULL
1094 && (!sym1
|| !TREE_NO_WARNING (val1
))
1095 && (!sym2
|| !TREE_NO_WARNING (val2
)))
1096 *strict_overflow_p
= true;
1098 const signop sgn
= TYPE_SIGN (TREE_TYPE (val1
));
1099 tree cst
= cst1
? val1
: val2
;
1100 tree inv
= cst1
? inv2
: inv1
;
1102 /* Compute the difference between the constants. If it overflows or
1103 underflows, this means that we can trivially compare the NAME with
1104 it and, consequently, the two values with each other. */
1105 wide_int diff
= wi::sub (cst
, inv
);
1106 if (wi::cmp (0, inv
, sgn
) != wi::cmp (diff
, cst
, sgn
))
1108 const int res
= wi::cmp (cst
, inv
, sgn
);
1109 return cst1
? res
: -res
;
1115 /* We cannot say anything more for non-constants. */
1119 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1121 /* We cannot compare overflowed values, except for overflow
1123 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1125 if (strict_overflow_p
!= NULL
)
1126 *strict_overflow_p
= true;
1130 return tree_int_cst_compare (val1
, val2
);
1136 /* First see if VAL1 and VAL2 are not the same. */
1137 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1140 /* If VAL1 is a lower address than VAL2, return -1. */
1141 if (operand_less_p (val1
, val2
) == 1)
1144 /* If VAL1 is a higher address than VAL2, return +1. */
1145 if (operand_less_p (val2
, val1
) == 1)
1148 /* If VAL1 is different than VAL2, return +2.
1149 For integer constants we either have already returned -1 or 1
1150 or they are equivalent. We still might succeed in proving
1151 something about non-trivial operands. */
1152 if (TREE_CODE (val1
) != INTEGER_CST
1153 || TREE_CODE (val2
) != INTEGER_CST
)
1155 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1156 if (t
&& integer_onep (t
))
1164 /* Compare values like compare_values_warnv, but treat comparisons of
1165 nonconstants which rely on undefined overflow as incomparable. */
1168 compare_values (tree val1
, tree val2
)
1174 ret
= compare_values_warnv (val1
, val2
, &sop
);
1176 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1182 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1183 0 if VAL is not inside [MIN, MAX],
1184 -2 if we cannot tell either way.
1186 Benchmark compile/20001226-1.c compilation time after changing this
1190 value_inside_range (tree val
, tree min
, tree max
)
1194 cmp1
= operand_less_p (val
, min
);
1200 cmp2
= operand_less_p (max
, val
);
1208 /* Return true if value ranges VR0 and VR1 have a non-empty
1211 Benchmark compile/20001226-1.c compilation time after changing this
1216 value_ranges_intersect_p (value_range
*vr0
, value_range
*vr1
)
1218 /* The value ranges do not intersect if the maximum of the first range is
1219 less than the minimum of the second range or vice versa.
1220 When those relations are unknown, we can't do any better. */
1221 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1223 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1229 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1230 include the value zero, -2 if we cannot tell. */
1233 range_includes_zero_p (tree min
, tree max
)
1235 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1236 return value_inside_range (zero
, min
, max
);
1239 /* Return true if *VR is know to only contain nonnegative values. */
1242 value_range_nonnegative_p (value_range
*vr
)
1244 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1245 which would return a useful value should be encoded as a
1247 if (vr
->type
== VR_RANGE
)
1249 int result
= compare_values (vr
->min
, integer_zero_node
);
1250 return (result
== 0 || result
== 1);
1256 /* If *VR has a value rante that is a single constant value return that,
1257 otherwise return NULL_TREE. */
1260 value_range_constant_singleton (value_range
*vr
)
1262 if (vr
->type
== VR_RANGE
1263 && vrp_operand_equal_p (vr
->min
, vr
->max
)
1264 && is_gimple_min_invariant (vr
->min
))
1270 /* If OP has a value range with a single constant value return that,
1271 otherwise return NULL_TREE. This returns OP itself if OP is a
1275 op_with_constant_singleton_value_range (tree op
)
1277 if (is_gimple_min_invariant (op
))
1280 if (TREE_CODE (op
) != SSA_NAME
)
1283 return value_range_constant_singleton (get_value_range (op
));
1286 /* Return true if op is in a boolean [0, 1] value-range. */
1289 op_with_boolean_value_range_p (tree op
)
1293 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1296 if (integer_zerop (op
)
1297 || integer_onep (op
))
1300 if (TREE_CODE (op
) != SSA_NAME
)
1303 vr
= get_value_range (op
);
1304 return (vr
->type
== VR_RANGE
1305 && integer_zerop (vr
->min
)
1306 && integer_onep (vr
->max
));
1309 /* Extract value range information for VAR when (OP COND_CODE LIMIT) is
1310 true and store it in *VR_P. */
1313 extract_range_for_var_from_comparison_expr (tree var
, enum tree_code cond_code
,
1314 tree op
, tree limit
,
1317 tree min
, max
, type
;
1318 value_range
*limit_vr
;
1319 type
= TREE_TYPE (var
);
1320 gcc_assert (limit
!= var
);
1322 /* For pointer arithmetic, we only keep track of pointer equality
1324 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1326 set_value_range_to_varying (vr_p
);
1330 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1331 try to use LIMIT's range to avoid creating symbolic ranges
1333 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1335 /* LIMIT's range is only interesting if it has any useful information. */
1337 || limit_vr
->type
== VR_UNDEFINED
1338 || limit_vr
->type
== VR_VARYING
1339 || (symbolic_range_p (limit_vr
)
1340 && ! (limit_vr
->type
== VR_RANGE
1341 && (limit_vr
->min
== limit_vr
->max
1342 || operand_equal_p (limit_vr
->min
, limit_vr
->max
, 0)))))
1345 /* Initially, the new range has the same set of equivalences of
1346 VAR's range. This will be revised before returning the final
1347 value. Since assertions may be chained via mutually exclusive
1348 predicates, we will need to trim the set of equivalences before
1350 gcc_assert (vr_p
->equiv
== NULL
);
1351 add_equivalence (&vr_p
->equiv
, var
);
1353 /* Extract a new range based on the asserted comparison for VAR and
1354 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1355 will only use it for equality comparisons (EQ_EXPR). For any
1356 other kind of assertion, we cannot derive a range from LIMIT's
1357 anti-range that can be used to describe the new range. For
1358 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1359 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1360 no single range for x_2 that could describe LE_EXPR, so we might
1361 as well build the range [b_4, +INF] for it.
1362 One special case we handle is extracting a range from a
1363 range test encoded as (unsigned)var + CST <= limit. */
1364 if (TREE_CODE (op
) == NOP_EXPR
1365 || TREE_CODE (op
) == PLUS_EXPR
)
1367 if (TREE_CODE (op
) == PLUS_EXPR
)
1369 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (op
, 1)),
1370 TREE_OPERAND (op
, 1));
1371 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1372 op
= TREE_OPERAND (op
, 0);
1376 min
= build_int_cst (TREE_TYPE (var
), 0);
1380 /* Make sure to not set TREE_OVERFLOW on the final type
1381 conversion. We are willingly interpreting large positive
1382 unsigned values as negative signed values here. */
1383 min
= force_fit_type (TREE_TYPE (var
), wi::to_widest (min
), 0, false);
1384 max
= force_fit_type (TREE_TYPE (var
), wi::to_widest (max
), 0, false);
1386 /* We can transform a max, min range to an anti-range or
1387 vice-versa. Use set_and_canonicalize_value_range which does
1389 if (cond_code
== LE_EXPR
)
1390 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1391 min
, max
, vr_p
->equiv
);
1392 else if (cond_code
== GT_EXPR
)
1393 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1394 min
, max
, vr_p
->equiv
);
1398 else if (cond_code
== EQ_EXPR
)
1400 enum value_range_type range_type
;
1404 range_type
= limit_vr
->type
;
1405 min
= limit_vr
->min
;
1406 max
= limit_vr
->max
;
1410 range_type
= VR_RANGE
;
1415 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1417 /* When asserting the equality VAR == LIMIT and LIMIT is another
1418 SSA name, the new range will also inherit the equivalence set
1420 if (TREE_CODE (limit
) == SSA_NAME
)
1421 add_equivalence (&vr_p
->equiv
, limit
);
1423 else if (cond_code
== NE_EXPR
)
1425 /* As described above, when LIMIT's range is an anti-range and
1426 this assertion is an inequality (NE_EXPR), then we cannot
1427 derive anything from the anti-range. For instance, if
1428 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1429 not imply that VAR's range is [0, 0]. So, in the case of
1430 anti-ranges, we just assert the inequality using LIMIT and
1433 If LIMIT_VR is a range, we can only use it to build a new
1434 anti-range if LIMIT_VR is a single-valued range. For
1435 instance, if LIMIT_VR is [0, 1], the predicate
1436 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1437 Rather, it means that for value 0 VAR should be ~[0, 0]
1438 and for value 1, VAR should be ~[1, 1]. We cannot
1439 represent these ranges.
1441 The only situation in which we can build a valid
1442 anti-range is when LIMIT_VR is a single-valued range
1443 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1444 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1446 && limit_vr
->type
== VR_RANGE
1447 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1449 min
= limit_vr
->min
;
1450 max
= limit_vr
->max
;
1454 /* In any other case, we cannot use LIMIT's range to build a
1455 valid anti-range. */
1459 /* If MIN and MAX cover the whole range for their type, then
1460 just use the original LIMIT. */
1461 if (INTEGRAL_TYPE_P (type
)
1462 && vrp_val_is_min (min
)
1463 && vrp_val_is_max (max
))
1466 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1467 min
, max
, vr_p
->equiv
);
1469 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1471 min
= TYPE_MIN_VALUE (type
);
1473 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1477 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1478 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1480 max
= limit_vr
->max
;
1483 /* If the maximum value forces us to be out of bounds, simply punt.
1484 It would be pointless to try and do anything more since this
1485 all should be optimized away above us. */
1486 if (cond_code
== LT_EXPR
1487 && compare_values (max
, min
) == 0)
1488 set_value_range_to_varying (vr_p
);
1491 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1492 if (cond_code
== LT_EXPR
)
1494 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1495 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1496 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1497 build_int_cst (TREE_TYPE (max
), -1));
1499 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1500 build_int_cst (TREE_TYPE (max
), 1));
1502 TREE_NO_WARNING (max
) = 1;
1505 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1508 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1510 max
= TYPE_MAX_VALUE (type
);
1512 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1516 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1517 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1519 min
= limit_vr
->min
;
1522 /* If the minimum value forces us to be out of bounds, simply punt.
1523 It would be pointless to try and do anything more since this
1524 all should be optimized away above us. */
1525 if (cond_code
== GT_EXPR
1526 && compare_values (min
, max
) == 0)
1527 set_value_range_to_varying (vr_p
);
1530 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1531 if (cond_code
== GT_EXPR
)
1533 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1534 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1535 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1536 build_int_cst (TREE_TYPE (min
), -1));
1538 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1539 build_int_cst (TREE_TYPE (min
), 1));
1541 TREE_NO_WARNING (min
) = 1;
1544 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1550 /* Finally intersect the new range with what we already know about var. */
1551 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1554 /* Extract value range information from an ASSERT_EXPR EXPR and store
1558 extract_range_from_assert (value_range
*vr_p
, tree expr
)
1560 tree var
= ASSERT_EXPR_VAR (expr
);
1561 tree cond
= ASSERT_EXPR_COND (expr
);
1563 enum tree_code cond_code
;
1564 gcc_assert (COMPARISON_CLASS_P (cond
));
1566 /* Find VAR in the ASSERT_EXPR conditional. */
1567 if (var
== TREE_OPERAND (cond
, 0)
1568 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1569 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1571 /* If the predicate is of the form VAR COMP LIMIT, then we just
1572 take LIMIT from the RHS and use the same comparison code. */
1573 cond_code
= TREE_CODE (cond
);
1574 limit
= TREE_OPERAND (cond
, 1);
1575 op
= TREE_OPERAND (cond
, 0);
1579 /* If the predicate is of the form LIMIT COMP VAR, then we need
1580 to flip around the comparison code to create the proper range
1582 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1583 limit
= TREE_OPERAND (cond
, 0);
1584 op
= TREE_OPERAND (cond
, 1);
1586 extract_range_for_var_from_comparison_expr (var
, cond_code
, op
,
1590 /* Extract range information from SSA name VAR and store it in VR. If
1591 VAR has an interesting range, use it. Otherwise, create the
1592 range [VAR, VAR] and return it. This is useful in situations where
1593 we may have conditionals testing values of VARYING names. For
1600 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1604 extract_range_from_ssa_name (value_range
*vr
, tree var
)
1606 value_range
*var_vr
= get_value_range (var
);
1608 if (var_vr
->type
!= VR_VARYING
)
1609 copy_value_range (vr
, var_vr
);
1611 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1613 add_equivalence (&vr
->equiv
, var
);
1617 /* Wrapper around int_const_binop. If the operation overflows and we
1618 are not using wrapping arithmetic, then adjust the result to be
1619 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1620 NULL_TREE if we need to use an overflow infinity representation but
1621 the type does not support it. */
1624 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1628 res
= int_const_binop (code
, val1
, val2
);
1630 /* If we are using unsigned arithmetic, operate symbolically
1631 on -INF and +INF as int_const_binop only handles signed overflow. */
1632 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1634 int checkz
= compare_values (res
, val1
);
1635 bool overflow
= false;
1637 /* Ensure that res = val1 [+*] val2 >= val1
1638 or that res = val1 - val2 <= val1. */
1639 if ((code
== PLUS_EXPR
1640 && !(checkz
== 1 || checkz
== 0))
1641 || (code
== MINUS_EXPR
1642 && !(checkz
== 0 || checkz
== -1)))
1646 /* Checking for multiplication overflow is done by dividing the
1647 output of the multiplication by the first input of the
1648 multiplication. If the result of that division operation is
1649 not equal to the second input of the multiplication, then the
1650 multiplication overflowed. */
1651 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1653 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1656 int check
= compare_values (tmp
, val2
);
1664 res
= copy_node (res
);
1665 TREE_OVERFLOW (res
) = 1;
1669 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1670 /* If the singed operation wraps then int_const_binop has done
1671 everything we want. */
1673 /* Signed division of -1/0 overflows and by the time it gets here
1674 returns NULL_TREE. */
1677 else if (TREE_OVERFLOW (res
)
1678 && ! TREE_OVERFLOW (val1
)
1679 && ! TREE_OVERFLOW (val2
))
1681 /* If the operation overflowed but neither VAL1 nor VAL2 are
1682 overflown, return -INF or +INF depending on the operation
1683 and the combination of signs of the operands. */
1684 int sgn1
= tree_int_cst_sgn (val1
);
1685 int sgn2
= tree_int_cst_sgn (val2
);
1687 /* Notice that we only need to handle the restricted set of
1688 operations handled by extract_range_from_binary_expr.
1689 Among them, only multiplication, addition and subtraction
1690 can yield overflow without overflown operands because we
1691 are working with integral types only... except in the
1692 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1693 for division too. */
1695 /* For multiplication, the sign of the overflow is given
1696 by the comparison of the signs of the operands. */
1697 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
1698 /* For addition, the operands must be of the same sign
1699 to yield an overflow. Its sign is therefore that
1700 of one of the operands, for example the first. */
1701 || (code
== PLUS_EXPR
&& sgn1
>= 0)
1702 /* For subtraction, operands must be of
1703 different signs to yield an overflow. Its sign is
1704 therefore that of the first operand or the opposite of
1705 that of the second operand. A first operand of 0 counts
1706 as positive here, for the corner case 0 - (-INF), which
1707 overflows, but must yield +INF. */
1708 || (code
== MINUS_EXPR
&& sgn1
>= 0)
1709 /* We only get in here with positive shift count, so the
1710 overflow direction is the same as the sign of val1.
1711 Actually rshift does not overflow at all, but we only
1712 handle the case of shifting overflowed -INF and +INF. */
1713 || (code
== RSHIFT_EXPR
1715 /* For division, the only case is -INF / -1 = +INF. */
1716 || code
== TRUNC_DIV_EXPR
1717 || code
== FLOOR_DIV_EXPR
1718 || code
== CEIL_DIV_EXPR
1719 || code
== EXACT_DIV_EXPR
1720 || code
== ROUND_DIV_EXPR
)
1721 return TYPE_MAX_VALUE (TREE_TYPE (res
));
1723 return TYPE_MIN_VALUE (TREE_TYPE (res
));
1730 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1731 bitmask if some bit is unset, it means for all numbers in the range
1732 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1733 bitmask if some bit is set, it means for all numbers in the range
1734 the bit is 1, otherwise it might be 0 or 1. */
1737 zero_nonzero_bits_from_vr (const tree expr_type
,
1739 wide_int
*may_be_nonzero
,
1740 wide_int
*must_be_nonzero
)
1742 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
1743 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
1744 if (!range_int_cst_p (vr
))
1747 if (range_int_cst_singleton_p (vr
))
1749 *may_be_nonzero
= vr
->min
;
1750 *must_be_nonzero
= *may_be_nonzero
;
1752 else if (tree_int_cst_sgn (vr
->min
) >= 0
1753 || tree_int_cst_sgn (vr
->max
) < 0)
1755 wide_int xor_mask
= wi::bit_xor (vr
->min
, vr
->max
);
1756 *may_be_nonzero
= wi::bit_or (vr
->min
, vr
->max
);
1757 *must_be_nonzero
= wi::bit_and (vr
->min
, vr
->max
);
1760 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
1761 may_be_nonzero
->get_precision ());
1762 *may_be_nonzero
= *may_be_nonzero
| mask
;
1763 *must_be_nonzero
= must_be_nonzero
->and_not (mask
);
1770 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
1771 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
1772 false otherwise. If *AR can be represented with a single range
1773 *VR1 will be VR_UNDEFINED. */
1776 ranges_from_anti_range (value_range
*ar
,
1777 value_range
*vr0
, value_range
*vr1
)
1779 tree type
= TREE_TYPE (ar
->min
);
1781 vr0
->type
= VR_UNDEFINED
;
1782 vr1
->type
= VR_UNDEFINED
;
1784 if (ar
->type
!= VR_ANTI_RANGE
1785 || TREE_CODE (ar
->min
) != INTEGER_CST
1786 || TREE_CODE (ar
->max
) != INTEGER_CST
1787 || !vrp_val_min (type
)
1788 || !vrp_val_max (type
))
1791 if (!vrp_val_is_min (ar
->min
))
1793 vr0
->type
= VR_RANGE
;
1794 vr0
->min
= vrp_val_min (type
);
1795 vr0
->max
= wide_int_to_tree (type
, wi::sub (ar
->min
, 1));
1797 if (!vrp_val_is_max (ar
->max
))
1799 vr1
->type
= VR_RANGE
;
1800 vr1
->min
= wide_int_to_tree (type
, wi::add (ar
->max
, 1));
1801 vr1
->max
= vrp_val_max (type
);
1803 if (vr0
->type
== VR_UNDEFINED
)
1806 vr1
->type
= VR_UNDEFINED
;
1809 return vr0
->type
!= VR_UNDEFINED
;
1812 /* Helper to extract a value-range *VR for a multiplicative operation
1816 extract_range_from_multiplicative_op_1 (value_range
*vr
,
1817 enum tree_code code
,
1818 value_range
*vr0
, value_range
*vr1
)
1820 enum value_range_type type
;
1827 /* Multiplications, divisions and shifts are a bit tricky to handle,
1828 depending on the mix of signs we have in the two ranges, we
1829 need to operate on different values to get the minimum and
1830 maximum values for the new range. One approach is to figure
1831 out all the variations of range combinations and do the
1834 However, this involves several calls to compare_values and it
1835 is pretty convoluted. It's simpler to do the 4 operations
1836 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
1837 MAX1) and then figure the smallest and largest values to form
1839 gcc_assert (code
== MULT_EXPR
1840 || code
== TRUNC_DIV_EXPR
1841 || code
== FLOOR_DIV_EXPR
1842 || code
== CEIL_DIV_EXPR
1843 || code
== EXACT_DIV_EXPR
1844 || code
== ROUND_DIV_EXPR
1845 || code
== RSHIFT_EXPR
1846 || code
== LSHIFT_EXPR
);
1847 gcc_assert ((vr0
->type
== VR_RANGE
1848 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
1849 && vr0
->type
== vr1
->type
);
1853 /* Compute the 4 cross operations. */
1855 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
1856 if (val
[0] == NULL_TREE
)
1859 if (vr1
->max
== vr1
->min
)
1863 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
1864 if (val
[1] == NULL_TREE
)
1868 if (vr0
->max
== vr0
->min
)
1872 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
1873 if (val
[2] == NULL_TREE
)
1877 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
1881 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
1882 if (val
[3] == NULL_TREE
)
1888 set_value_range_to_varying (vr
);
1892 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
1896 for (i
= 1; i
< 4; i
++)
1898 if (!is_gimple_min_invariant (min
)
1899 || TREE_OVERFLOW (min
)
1900 || !is_gimple_min_invariant (max
)
1901 || TREE_OVERFLOW (max
))
1906 if (!is_gimple_min_invariant (val
[i
])
1907 || TREE_OVERFLOW (val
[i
]))
1909 /* If we found an overflowed value, set MIN and MAX
1910 to it so that we set the resulting range to
1916 if (compare_values (val
[i
], min
) == -1)
1919 if (compare_values (val
[i
], max
) == 1)
1924 /* If either MIN or MAX overflowed, then set the resulting range to
1925 VARYING. But we do accept an overflow infinity
1927 if (min
== NULL_TREE
1928 || !is_gimple_min_invariant (min
)
1929 || TREE_OVERFLOW (min
)
1931 || !is_gimple_min_invariant (max
)
1932 || TREE_OVERFLOW (max
))
1934 set_value_range_to_varying (vr
);
1938 /* We punt for [-INF, +INF].
1939 We learn nothing when we have INF on both sides.
1940 Note that we do accept [-INF, -INF] and [+INF, +INF]. */
1941 if (vrp_val_is_min (min
)
1942 && vrp_val_is_max (max
))
1944 set_value_range_to_varying (vr
);
1948 cmp
= compare_values (min
, max
);
1949 if (cmp
== -2 || cmp
== 1)
1951 /* If the new range has its limits swapped around (MIN > MAX),
1952 then the operation caused one of them to wrap around, mark
1953 the new range VARYING. */
1954 set_value_range_to_varying (vr
);
1957 set_value_range (vr
, type
, min
, max
, NULL
);
1960 /* Extract range information from a binary operation CODE based on
1961 the ranges of each of its operands *VR0 and *VR1 with resulting
1962 type EXPR_TYPE. The resulting range is stored in *VR. */
1965 extract_range_from_binary_expr_1 (value_range
*vr
,
1966 enum tree_code code
, tree expr_type
,
1967 value_range
*vr0_
, value_range
*vr1_
)
1969 value_range vr0
= *vr0_
, vr1
= *vr1_
;
1970 value_range vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
1971 enum value_range_type type
;
1972 tree min
= NULL_TREE
, max
= NULL_TREE
;
1975 if (!INTEGRAL_TYPE_P (expr_type
)
1976 && !POINTER_TYPE_P (expr_type
))
1978 set_value_range_to_varying (vr
);
1982 /* Not all binary expressions can be applied to ranges in a
1983 meaningful way. Handle only arithmetic operations. */
1984 if (code
!= PLUS_EXPR
1985 && code
!= MINUS_EXPR
1986 && code
!= POINTER_PLUS_EXPR
1987 && code
!= MULT_EXPR
1988 && code
!= TRUNC_DIV_EXPR
1989 && code
!= FLOOR_DIV_EXPR
1990 && code
!= CEIL_DIV_EXPR
1991 && code
!= EXACT_DIV_EXPR
1992 && code
!= ROUND_DIV_EXPR
1993 && code
!= TRUNC_MOD_EXPR
1994 && code
!= RSHIFT_EXPR
1995 && code
!= LSHIFT_EXPR
1998 && code
!= BIT_AND_EXPR
1999 && code
!= BIT_IOR_EXPR
2000 && code
!= BIT_XOR_EXPR
)
2002 set_value_range_to_varying (vr
);
2006 /* If both ranges are UNDEFINED, so is the result. */
2007 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2009 set_value_range_to_undefined (vr
);
2012 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2013 code. At some point we may want to special-case operations that
2014 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2016 else if (vr0
.type
== VR_UNDEFINED
)
2017 set_value_range_to_varying (&vr0
);
2018 else if (vr1
.type
== VR_UNDEFINED
)
2019 set_value_range_to_varying (&vr1
);
2021 /* We get imprecise results from ranges_from_anti_range when
2022 code is EXACT_DIV_EXPR. We could mask out bits in the resulting
2023 range, but then we also need to hack up vrp_meet. It's just
2024 easier to special case when vr0 is ~[0,0] for EXACT_DIV_EXPR. */
2025 if (code
== EXACT_DIV_EXPR
2026 && vr0
.type
== VR_ANTI_RANGE
2027 && vr0
.min
== vr0
.max
2028 && integer_zerop (vr0
.min
))
2030 set_value_range_to_nonnull (vr
, expr_type
);
2034 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2035 and express ~[] op X as ([]' op X) U ([]'' op X). */
2036 if (vr0
.type
== VR_ANTI_RANGE
2037 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2039 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2040 if (vrtem1
.type
!= VR_UNDEFINED
)
2042 value_range vrres
= VR_INITIALIZER
;
2043 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2045 vrp_meet (vr
, &vrres
);
2049 /* Likewise for X op ~[]. */
2050 if (vr1
.type
== VR_ANTI_RANGE
2051 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2053 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2054 if (vrtem1
.type
!= VR_UNDEFINED
)
2056 value_range vrres
= VR_INITIALIZER
;
2057 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2059 vrp_meet (vr
, &vrres
);
2064 /* The type of the resulting value range defaults to VR0.TYPE. */
2067 /* Refuse to operate on VARYING ranges, ranges of different kinds
2068 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2069 because we may be able to derive a useful range even if one of
2070 the operands is VR_VARYING or symbolic range. Similarly for
2071 divisions, MIN/MAX and PLUS/MINUS.
2073 TODO, we may be able to derive anti-ranges in some cases. */
2074 if (code
!= BIT_AND_EXPR
2075 && code
!= BIT_IOR_EXPR
2076 && code
!= TRUNC_DIV_EXPR
2077 && code
!= FLOOR_DIV_EXPR
2078 && code
!= CEIL_DIV_EXPR
2079 && code
!= EXACT_DIV_EXPR
2080 && code
!= ROUND_DIV_EXPR
2081 && code
!= TRUNC_MOD_EXPR
2084 && code
!= PLUS_EXPR
2085 && code
!= MINUS_EXPR
2086 && code
!= RSHIFT_EXPR
2087 && (vr0
.type
== VR_VARYING
2088 || vr1
.type
== VR_VARYING
2089 || vr0
.type
!= vr1
.type
2090 || symbolic_range_p (&vr0
)
2091 || symbolic_range_p (&vr1
)))
2093 set_value_range_to_varying (vr
);
2097 /* Now evaluate the expression to determine the new range. */
2098 if (POINTER_TYPE_P (expr_type
))
2100 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2102 /* For MIN/MAX expressions with pointers, we only care about
2103 nullness, if both are non null, then the result is nonnull.
2104 If both are null, then the result is null. Otherwise they
2106 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2107 set_value_range_to_nonnull (vr
, expr_type
);
2108 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2109 set_value_range_to_null (vr
, expr_type
);
2111 set_value_range_to_varying (vr
);
2113 else if (code
== POINTER_PLUS_EXPR
)
2115 /* For pointer types, we are really only interested in asserting
2116 whether the expression evaluates to non-NULL. */
2117 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2118 set_value_range_to_nonnull (vr
, expr_type
);
2119 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2120 set_value_range_to_null (vr
, expr_type
);
2122 set_value_range_to_varying (vr
);
2124 else if (code
== BIT_AND_EXPR
)
2126 /* For pointer types, we are really only interested in asserting
2127 whether the expression evaluates to non-NULL. */
2128 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2129 set_value_range_to_nonnull (vr
, expr_type
);
2130 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2131 set_value_range_to_null (vr
, expr_type
);
2133 set_value_range_to_varying (vr
);
2136 set_value_range_to_varying (vr
);
2141 /* For integer ranges, apply the operation to each end of the
2142 range and see what we end up with. */
2143 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2145 const bool minus_p
= (code
== MINUS_EXPR
);
2146 tree min_op0
= vr0
.min
;
2147 tree min_op1
= minus_p
? vr1
.max
: vr1
.min
;
2148 tree max_op0
= vr0
.max
;
2149 tree max_op1
= minus_p
? vr1
.min
: vr1
.max
;
2150 tree sym_min_op0
= NULL_TREE
;
2151 tree sym_min_op1
= NULL_TREE
;
2152 tree sym_max_op0
= NULL_TREE
;
2153 tree sym_max_op1
= NULL_TREE
;
2154 bool neg_min_op0
, neg_min_op1
, neg_max_op0
, neg_max_op1
;
2156 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2157 single-symbolic ranges, try to compute the precise resulting range,
2158 but only if we know that this resulting range will also be constant
2159 or single-symbolic. */
2160 if (vr0
.type
== VR_RANGE
&& vr1
.type
== VR_RANGE
2161 && (TREE_CODE (min_op0
) == INTEGER_CST
2163 = get_single_symbol (min_op0
, &neg_min_op0
, &min_op0
)))
2164 && (TREE_CODE (min_op1
) == INTEGER_CST
2166 = get_single_symbol (min_op1
, &neg_min_op1
, &min_op1
)))
2167 && (!(sym_min_op0
&& sym_min_op1
)
2168 || (sym_min_op0
== sym_min_op1
2169 && neg_min_op0
== (minus_p
? neg_min_op1
: !neg_min_op1
)))
2170 && (TREE_CODE (max_op0
) == INTEGER_CST
2172 = get_single_symbol (max_op0
, &neg_max_op0
, &max_op0
)))
2173 && (TREE_CODE (max_op1
) == INTEGER_CST
2175 = get_single_symbol (max_op1
, &neg_max_op1
, &max_op1
)))
2176 && (!(sym_max_op0
&& sym_max_op1
)
2177 || (sym_max_op0
== sym_max_op1
2178 && neg_max_op0
== (minus_p
? neg_max_op1
: !neg_max_op1
))))
2180 const signop sgn
= TYPE_SIGN (expr_type
);
2181 const unsigned int prec
= TYPE_PRECISION (expr_type
);
2182 wide_int type_min
, type_max
, wmin
, wmax
;
2186 /* Get the lower and upper bounds of the type. */
2187 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2189 type_min
= wi::min_value (prec
, sgn
);
2190 type_max
= wi::max_value (prec
, sgn
);
2194 type_min
= vrp_val_min (expr_type
);
2195 type_max
= vrp_val_max (expr_type
);
2198 /* Combine the lower bounds, if any. */
2199 if (min_op0
&& min_op1
)
2203 wmin
= wi::sub (min_op0
, min_op1
);
2205 /* Check for overflow. */
2206 if (wi::cmp (0, min_op1
, sgn
)
2207 != wi::cmp (wmin
, min_op0
, sgn
))
2208 min_ovf
= wi::cmp (min_op0
, min_op1
, sgn
);
2212 wmin
= wi::add (min_op0
, min_op1
);
2214 /* Check for overflow. */
2215 if (wi::cmp (min_op1
, 0, sgn
)
2216 != wi::cmp (wmin
, min_op0
, sgn
))
2217 min_ovf
= wi::cmp (min_op0
, wmin
, sgn
);
2226 wmin
= wi::neg (min_op1
);
2228 /* Check for overflow. */
2229 if (sgn
== SIGNED
&& wi::neg_p (min_op1
) && wi::neg_p (wmin
))
2231 else if (sgn
== UNSIGNED
&& wi::ne_p (min_op1
, 0))
2238 wmin
= wi::shwi (0, prec
);
2240 /* Combine the upper bounds, if any. */
2241 if (max_op0
&& max_op1
)
2245 wmax
= wi::sub (max_op0
, max_op1
);
2247 /* Check for overflow. */
2248 if (wi::cmp (0, max_op1
, sgn
)
2249 != wi::cmp (wmax
, max_op0
, sgn
))
2250 max_ovf
= wi::cmp (max_op0
, max_op1
, sgn
);
2254 wmax
= wi::add (max_op0
, max_op1
);
2256 if (wi::cmp (max_op1
, 0, sgn
)
2257 != wi::cmp (wmax
, max_op0
, sgn
))
2258 max_ovf
= wi::cmp (max_op0
, wmax
, sgn
);
2267 wmax
= wi::neg (max_op1
);
2269 /* Check for overflow. */
2270 if (sgn
== SIGNED
&& wi::neg_p (max_op1
) && wi::neg_p (wmax
))
2272 else if (sgn
== UNSIGNED
&& wi::ne_p (max_op1
, 0))
2279 wmax
= wi::shwi (0, prec
);
2281 /* Check for type overflow. */
2284 if (wi::cmp (wmin
, type_min
, sgn
) == -1)
2286 else if (wi::cmp (wmin
, type_max
, sgn
) == 1)
2291 if (wi::cmp (wmax
, type_min
, sgn
) == -1)
2293 else if (wi::cmp (wmax
, type_max
, sgn
) == 1)
2297 /* If we have overflow for the constant part and the resulting
2298 range will be symbolic, drop to VR_VARYING. */
2299 if ((min_ovf
&& sym_min_op0
!= sym_min_op1
)
2300 || (max_ovf
&& sym_max_op0
!= sym_max_op1
))
2302 set_value_range_to_varying (vr
);
2306 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2308 /* If overflow wraps, truncate the values and adjust the
2309 range kind and bounds appropriately. */
2310 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
2311 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
2312 if (min_ovf
== max_ovf
)
2314 /* No overflow or both overflow or underflow. The
2315 range kind stays VR_RANGE. */
2316 min
= wide_int_to_tree (expr_type
, tmin
);
2317 max
= wide_int_to_tree (expr_type
, tmax
);
2319 else if ((min_ovf
== -1 && max_ovf
== 0)
2320 || (max_ovf
== 1 && min_ovf
== 0))
2322 /* Min underflow or max overflow. The range kind
2323 changes to VR_ANTI_RANGE. */
2324 bool covers
= false;
2325 wide_int tem
= tmin
;
2326 type
= VR_ANTI_RANGE
;
2328 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
2331 if (wi::cmp (tmax
, tem
, sgn
) > 0)
2333 /* If the anti-range would cover nothing, drop to varying.
2334 Likewise if the anti-range bounds are outside of the
2336 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
2338 set_value_range_to_varying (vr
);
2341 min
= wide_int_to_tree (expr_type
, tmin
);
2342 max
= wide_int_to_tree (expr_type
, tmax
);
2346 /* Other underflow and/or overflow, drop to VR_VARYING. */
2347 set_value_range_to_varying (vr
);
2353 /* If overflow does not wrap, saturate to the types min/max
2356 min
= wide_int_to_tree (expr_type
, type_min
);
2357 else if (min_ovf
== 1)
2358 min
= wide_int_to_tree (expr_type
, type_max
);
2360 min
= wide_int_to_tree (expr_type
, wmin
);
2363 max
= wide_int_to_tree (expr_type
, type_min
);
2364 else if (max_ovf
== 1)
2365 max
= wide_int_to_tree (expr_type
, type_max
);
2367 max
= wide_int_to_tree (expr_type
, wmax
);
2370 /* If the result lower bound is constant, we're done;
2371 otherwise, build the symbolic lower bound. */
2372 if (sym_min_op0
== sym_min_op1
)
2374 else if (sym_min_op0
)
2375 min
= build_symbolic_expr (expr_type
, sym_min_op0
,
2377 else if (sym_min_op1
)
2379 /* We may not negate if that might introduce
2380 undefined overflow. */
2383 || TYPE_OVERFLOW_WRAPS (expr_type
))
2384 min
= build_symbolic_expr (expr_type
, sym_min_op1
,
2385 neg_min_op1
^ minus_p
, min
);
2390 /* Likewise for the upper bound. */
2391 if (sym_max_op0
== sym_max_op1
)
2393 else if (sym_max_op0
)
2394 max
= build_symbolic_expr (expr_type
, sym_max_op0
,
2396 else if (sym_max_op1
)
2398 /* We may not negate if that might introduce
2399 undefined overflow. */
2402 || TYPE_OVERFLOW_WRAPS (expr_type
))
2403 max
= build_symbolic_expr (expr_type
, sym_max_op1
,
2404 neg_max_op1
^ minus_p
, max
);
2411 /* For other cases, for example if we have a PLUS_EXPR with two
2412 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2413 to compute a precise range for such a case.
2414 ??? General even mixed range kind operations can be expressed
2415 by for example transforming ~[3, 5] + [1, 2] to range-only
2416 operations and a union primitive:
2417 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2418 [-INF+1, 4] U [6, +INF(OVF)]
2419 though usually the union is not exactly representable with
2420 a single range or anti-range as the above is
2421 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2422 but one could use a scheme similar to equivalences for this. */
2423 set_value_range_to_varying (vr
);
2427 else if (code
== MIN_EXPR
2428 || code
== MAX_EXPR
)
2430 if (vr0
.type
== VR_RANGE
2431 && !symbolic_range_p (&vr0
))
2434 if (vr1
.type
== VR_RANGE
2435 && !symbolic_range_p (&vr1
))
2437 /* For operations that make the resulting range directly
2438 proportional to the original ranges, apply the operation to
2439 the same end of each range. */
2440 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2441 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2443 else if (code
== MIN_EXPR
)
2445 min
= vrp_val_min (expr_type
);
2448 else if (code
== MAX_EXPR
)
2451 max
= vrp_val_max (expr_type
);
2454 else if (vr1
.type
== VR_RANGE
2455 && !symbolic_range_p (&vr1
))
2458 if (code
== MIN_EXPR
)
2460 min
= vrp_val_min (expr_type
);
2463 else if (code
== MAX_EXPR
)
2466 max
= vrp_val_max (expr_type
);
2471 set_value_range_to_varying (vr
);
2475 else if (code
== MULT_EXPR
)
2477 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2478 drop to varying. This test requires 2*prec bits if both
2479 operands are signed and 2*prec + 2 bits if either is not. */
2481 signop sign
= TYPE_SIGN (expr_type
);
2482 unsigned int prec
= TYPE_PRECISION (expr_type
);
2484 if (range_int_cst_p (&vr0
)
2485 && range_int_cst_p (&vr1
)
2486 && TYPE_OVERFLOW_WRAPS (expr_type
))
2488 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
* 2) vrp_int
;
2489 typedef generic_wide_int
2490 <wi::extended_tree
<WIDE_INT_MAX_PRECISION
* 2> > vrp_int_cst
;
2491 vrp_int sizem1
= wi::mask
<vrp_int
> (prec
, false);
2492 vrp_int size
= sizem1
+ 1;
2494 /* Extend the values using the sign of the result to PREC2.
2495 From here on out, everthing is just signed math no matter
2496 what the input types were. */
2497 vrp_int min0
= vrp_int_cst (vr0
.min
);
2498 vrp_int max0
= vrp_int_cst (vr0
.max
);
2499 vrp_int min1
= vrp_int_cst (vr1
.min
);
2500 vrp_int max1
= vrp_int_cst (vr1
.max
);
2501 /* Canonicalize the intervals. */
2502 if (sign
== UNSIGNED
)
2504 if (wi::ltu_p (size
, min0
+ max0
))
2510 if (wi::ltu_p (size
, min1
+ max1
))
2517 vrp_int prod0
= min0
* min1
;
2518 vrp_int prod1
= min0
* max1
;
2519 vrp_int prod2
= max0
* min1
;
2520 vrp_int prod3
= max0
* max1
;
2522 /* Sort the 4 products so that min is in prod0 and max is in
2524 /* min0min1 > max0max1 */
2526 std::swap (prod0
, prod3
);
2528 /* min0max1 > max0min1 */
2530 std::swap (prod1
, prod2
);
2533 std::swap (prod0
, prod1
);
2536 std::swap (prod2
, prod3
);
2538 /* diff = max - min. */
2539 prod2
= prod3
- prod0
;
2540 if (wi::geu_p (prod2
, sizem1
))
2542 /* the range covers all values. */
2543 set_value_range_to_varying (vr
);
2547 /* The following should handle the wrapping and selecting
2548 VR_ANTI_RANGE for us. */
2549 min
= wide_int_to_tree (expr_type
, prod0
);
2550 max
= wide_int_to_tree (expr_type
, prod3
);
2551 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2555 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2556 drop to VR_VARYING. It would take more effort to compute a
2557 precise range for such a case. For example, if we have
2558 op0 == 65536 and op1 == 65536 with their ranges both being
2559 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2560 we cannot claim that the product is in ~[0,0]. Note that we
2561 are guaranteed to have vr0.type == vr1.type at this
2563 if (vr0
.type
== VR_ANTI_RANGE
2564 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2566 set_value_range_to_varying (vr
);
2570 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2573 else if (code
== RSHIFT_EXPR
2574 || code
== LSHIFT_EXPR
)
2576 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2577 then drop to VR_VARYING. Outside of this range we get undefined
2578 behavior from the shift operation. We cannot even trust
2579 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2580 shifts, and the operation at the tree level may be widened. */
2581 if (range_int_cst_p (&vr1
)
2582 && compare_tree_int (vr1
.min
, 0) >= 0
2583 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2585 if (code
== RSHIFT_EXPR
)
2587 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2588 useful ranges just from the shift count. E.g.
2589 x >> 63 for signed 64-bit x is always [-1, 0]. */
2590 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2592 vr0
.type
= type
= VR_RANGE
;
2593 vr0
.min
= vrp_val_min (expr_type
);
2594 vr0
.max
= vrp_val_max (expr_type
);
2596 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2599 /* We can map lshifts by constants to MULT_EXPR handling. */
2600 else if (code
== LSHIFT_EXPR
2601 && range_int_cst_singleton_p (&vr1
))
2603 bool saved_flag_wrapv
;
2604 value_range vr1p
= VR_INITIALIZER
;
2605 vr1p
.type
= VR_RANGE
;
2606 vr1p
.min
= (wide_int_to_tree
2608 wi::set_bit_in_zero (tree_to_shwi (vr1
.min
),
2609 TYPE_PRECISION (expr_type
))));
2610 vr1p
.max
= vr1p
.min
;
2611 /* We have to use a wrapping multiply though as signed overflow
2612 on lshifts is implementation defined in C89. */
2613 saved_flag_wrapv
= flag_wrapv
;
2615 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2617 flag_wrapv
= saved_flag_wrapv
;
2620 else if (code
== LSHIFT_EXPR
2621 && range_int_cst_p (&vr0
))
2623 int prec
= TYPE_PRECISION (expr_type
);
2624 int overflow_pos
= prec
;
2626 wide_int low_bound
, high_bound
;
2627 bool uns
= TYPE_UNSIGNED (expr_type
);
2628 bool in_bounds
= false;
2633 bound_shift
= overflow_pos
- tree_to_shwi (vr1
.max
);
2634 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2635 overflow. However, for that to happen, vr1.max needs to be
2636 zero, which means vr1 is a singleton range of zero, which
2637 means it should be handled by the previous LSHIFT_EXPR
2639 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
2640 wide_int complement
= ~(bound
- 1);
2645 high_bound
= complement
;
2646 if (wi::ltu_p (vr0
.max
, low_bound
))
2648 /* [5, 6] << [1, 2] == [10, 24]. */
2649 /* We're shifting out only zeroes, the value increases
2653 else if (wi::ltu_p (high_bound
, vr0
.min
))
2655 /* [0xffffff00, 0xffffffff] << [1, 2]
2656 == [0xfffffc00, 0xfffffffe]. */
2657 /* We're shifting out only ones, the value decreases
2664 /* [-1, 1] << [1, 2] == [-4, 4]. */
2665 low_bound
= complement
;
2667 if (wi::lts_p (vr0
.max
, high_bound
)
2668 && wi::lts_p (low_bound
, vr0
.min
))
2670 /* For non-negative numbers, we're shifting out only
2671 zeroes, the value increases monotonically.
2672 For negative numbers, we're shifting out only ones, the
2673 value decreases monotomically. */
2680 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2685 set_value_range_to_varying (vr
);
2688 else if (code
== TRUNC_DIV_EXPR
2689 || code
== FLOOR_DIV_EXPR
2690 || code
== CEIL_DIV_EXPR
2691 || code
== EXACT_DIV_EXPR
2692 || code
== ROUND_DIV_EXPR
)
2694 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2696 /* For division, if op1 has VR_RANGE but op0 does not, something
2697 can be deduced just from that range. Say [min, max] / [4, max]
2698 gives [min / 4, max / 4] range. */
2699 if (vr1
.type
== VR_RANGE
2700 && !symbolic_range_p (&vr1
)
2701 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
2703 vr0
.type
= type
= VR_RANGE
;
2704 vr0
.min
= vrp_val_min (expr_type
);
2705 vr0
.max
= vrp_val_max (expr_type
);
2709 set_value_range_to_varying (vr
);
2714 /* For divisions, if flag_non_call_exceptions is true, we must
2715 not eliminate a division by zero. */
2716 if (cfun
->can_throw_non_call_exceptions
2717 && (vr1
.type
!= VR_RANGE
2718 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2720 set_value_range_to_varying (vr
);
2724 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2725 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2727 if (vr0
.type
== VR_RANGE
2728 && (vr1
.type
!= VR_RANGE
2729 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2731 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
2736 if (TYPE_UNSIGNED (expr_type
)
2737 || value_range_nonnegative_p (&vr1
))
2739 /* For unsigned division or when divisor is known
2740 to be non-negative, the range has to cover
2741 all numbers from 0 to max for positive max
2742 and all numbers from min to 0 for negative min. */
2743 cmp
= compare_values (vr0
.max
, zero
);
2746 /* When vr0.max < 0, vr1.min != 0 and value
2747 ranges for dividend and divisor are available. */
2748 if (vr1
.type
== VR_RANGE
2749 && !symbolic_range_p (&vr0
)
2750 && !symbolic_range_p (&vr1
)
2751 && compare_values (vr1
.min
, zero
) != 0)
2752 max
= int_const_binop (code
, vr0
.max
, vr1
.min
);
2756 else if (cmp
== 0 || cmp
== 1)
2760 cmp
= compare_values (vr0
.min
, zero
);
2763 /* For unsigned division when value ranges for dividend
2764 and divisor are available. */
2765 if (vr1
.type
== VR_RANGE
2766 && !symbolic_range_p (&vr0
)
2767 && !symbolic_range_p (&vr1
)
2768 && compare_values (vr1
.max
, zero
) != 0)
2769 min
= int_const_binop (code
, vr0
.min
, vr1
.max
);
2773 else if (cmp
== 0 || cmp
== -1)
2780 /* Otherwise the range is -max .. max or min .. -min
2781 depending on which bound is bigger in absolute value,
2782 as the division can change the sign. */
2783 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
2786 if (type
== VR_VARYING
)
2788 set_value_range_to_varying (vr
);
2792 else if (!symbolic_range_p (&vr0
) && !symbolic_range_p (&vr1
))
2794 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2798 else if (code
== TRUNC_MOD_EXPR
)
2800 if (range_is_null (&vr1
))
2802 set_value_range_to_undefined (vr
);
2805 /* ABS (A % B) < ABS (B) and either
2806 0 <= A % B <= A or A <= A % B <= 0. */
2808 signop sgn
= TYPE_SIGN (expr_type
);
2809 unsigned int prec
= TYPE_PRECISION (expr_type
);
2810 wide_int wmin
, wmax
, tmp
;
2811 wide_int zero
= wi::zero (prec
);
2812 wide_int one
= wi::one (prec
);
2813 if (vr1
.type
== VR_RANGE
&& !symbolic_range_p (&vr1
))
2815 wmax
= wi::sub (vr1
.max
, one
);
2818 tmp
= wi::sub (wi::minus_one (prec
), vr1
.min
);
2819 wmax
= wi::smax (wmax
, tmp
);
2824 wmax
= wi::max_value (prec
, sgn
);
2825 /* X % INT_MIN may be INT_MAX. */
2826 if (sgn
== UNSIGNED
)
2830 if (sgn
== UNSIGNED
)
2835 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.min
) == INTEGER_CST
)
2838 if (wi::gts_p (tmp
, zero
))
2840 wmin
= wi::smax (wmin
, tmp
);
2844 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.max
) == INTEGER_CST
)
2847 if (sgn
== SIGNED
&& wi::neg_p (tmp
))
2849 wmax
= wi::min (wmax
, tmp
, sgn
);
2852 min
= wide_int_to_tree (expr_type
, wmin
);
2853 max
= wide_int_to_tree (expr_type
, wmax
);
2855 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
2857 bool int_cst_range0
, int_cst_range1
;
2858 wide_int may_be_nonzero0
, may_be_nonzero1
;
2859 wide_int must_be_nonzero0
, must_be_nonzero1
;
2861 int_cst_range0
= zero_nonzero_bits_from_vr (expr_type
, &vr0
,
2864 int_cst_range1
= zero_nonzero_bits_from_vr (expr_type
, &vr1
,
2868 if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
)
2870 value_range
*vr0p
= NULL
, *vr1p
= NULL
;
2871 if (range_int_cst_singleton_p (&vr1
))
2876 else if (range_int_cst_singleton_p (&vr0
))
2881 /* For op & or | attempt to optimize:
2882 [x, y] op z into [x op z, y op z]
2883 if z is a constant which (for op | its bitwise not) has n
2884 consecutive least significant bits cleared followed by m 1
2885 consecutive bits set immediately above it and either
2886 m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
2887 The least significant n bits of all the values in the range are
2888 cleared or set, the m bits above it are preserved and any bits
2889 above these are required to be the same for all values in the
2891 if (vr0p
&& range_int_cst_p (vr0p
))
2893 wide_int w
= vr1p
->min
;
2895 if (code
== BIT_IOR_EXPR
)
2897 if (wi::eq_p (w
, 0))
2898 n
= TYPE_PRECISION (expr_type
);
2902 w
= ~(w
| wi::mask (n
, false, w
.get_precision ()));
2903 if (wi::eq_p (w
, 0))
2904 m
= TYPE_PRECISION (expr_type
) - n
;
2906 m
= wi::ctz (w
) - n
;
2908 wide_int mask
= wi::mask (m
+ n
, true, w
.get_precision ());
2909 if (wi::eq_p (mask
& vr0p
->min
, mask
& vr0p
->max
))
2911 min
= int_const_binop (code
, vr0p
->min
, vr1p
->min
);
2912 max
= int_const_binop (code
, vr0p
->max
, vr1p
->min
);
2919 /* Optimized above already. */;
2920 else if (code
== BIT_AND_EXPR
)
2922 min
= wide_int_to_tree (expr_type
,
2923 must_be_nonzero0
& must_be_nonzero1
);
2924 wide_int wmax
= may_be_nonzero0
& may_be_nonzero1
;
2925 /* If both input ranges contain only negative values we can
2926 truncate the result range maximum to the minimum of the
2927 input range maxima. */
2928 if (int_cst_range0
&& int_cst_range1
2929 && tree_int_cst_sgn (vr0
.max
) < 0
2930 && tree_int_cst_sgn (vr1
.max
) < 0)
2932 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
2933 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
2935 /* If either input range contains only non-negative values
2936 we can truncate the result range maximum to the respective
2937 maximum of the input range. */
2938 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
2939 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
2940 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
2941 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
2942 max
= wide_int_to_tree (expr_type
, wmax
);
2943 cmp
= compare_values (min
, max
);
2944 /* PR68217: In case of signed & sign-bit-CST should
2945 result in [-INF, 0] instead of [-INF, INF]. */
2946 if (cmp
== -2 || cmp
== 1)
2949 = wi::set_bit_in_zero (TYPE_PRECISION (expr_type
) - 1,
2950 TYPE_PRECISION (expr_type
));
2951 if (!TYPE_UNSIGNED (expr_type
)
2952 && ((value_range_constant_singleton (&vr0
)
2953 && !wi::cmps (vr0
.min
, sign_bit
))
2954 || (value_range_constant_singleton (&vr1
)
2955 && !wi::cmps (vr1
.min
, sign_bit
))))
2957 min
= TYPE_MIN_VALUE (expr_type
);
2958 max
= build_int_cst (expr_type
, 0);
2962 else if (code
== BIT_IOR_EXPR
)
2964 max
= wide_int_to_tree (expr_type
,
2965 may_be_nonzero0
| may_be_nonzero1
);
2966 wide_int wmin
= must_be_nonzero0
| must_be_nonzero1
;
2967 /* If the input ranges contain only positive values we can
2968 truncate the minimum of the result range to the maximum
2969 of the input range minima. */
2970 if (int_cst_range0
&& int_cst_range1
2971 && tree_int_cst_sgn (vr0
.min
) >= 0
2972 && tree_int_cst_sgn (vr1
.min
) >= 0)
2974 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
2975 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
2977 /* If either input range contains only negative values
2978 we can truncate the minimum of the result range to the
2979 respective minimum range. */
2980 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
2981 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
2982 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
2983 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
2984 min
= wide_int_to_tree (expr_type
, wmin
);
2986 else if (code
== BIT_XOR_EXPR
)
2988 wide_int result_zero_bits
= ((must_be_nonzero0
& must_be_nonzero1
)
2989 | ~(may_be_nonzero0
| may_be_nonzero1
));
2990 wide_int result_one_bits
2991 = (must_be_nonzero0
.and_not (may_be_nonzero1
)
2992 | must_be_nonzero1
.and_not (may_be_nonzero0
));
2993 max
= wide_int_to_tree (expr_type
, ~result_zero_bits
);
2994 min
= wide_int_to_tree (expr_type
, result_one_bits
);
2995 /* If the range has all positive or all negative values the
2996 result is better than VARYING. */
2997 if (tree_int_cst_sgn (min
) < 0
2998 || tree_int_cst_sgn (max
) >= 0)
3001 max
= min
= NULL_TREE
;
3007 /* If either MIN or MAX overflowed, then set the resulting range to
3009 if (min
== NULL_TREE
3010 || TREE_OVERFLOW_P (min
)
3012 || TREE_OVERFLOW_P (max
))
3014 set_value_range_to_varying (vr
);
3018 /* We punt for [-INF, +INF].
3019 We learn nothing when we have INF on both sides.
3020 Note that we do accept [-INF, -INF] and [+INF, +INF]. */
3021 if (vrp_val_is_min (min
) && vrp_val_is_max (max
))
3023 set_value_range_to_varying (vr
);
3027 cmp
= compare_values (min
, max
);
3028 if (cmp
== -2 || cmp
== 1)
3030 /* If the new range has its limits swapped around (MIN > MAX),
3031 then the operation caused one of them to wrap around, mark
3032 the new range VARYING. */
3033 set_value_range_to_varying (vr
);
3036 set_value_range (vr
, type
, min
, max
, NULL
);
3039 /* Extract range information from a binary expression OP0 CODE OP1 based on
3040 the ranges of each of its operands with resulting type EXPR_TYPE.
3041 The resulting range is stored in *VR. */
3044 extract_range_from_binary_expr (value_range
*vr
,
3045 enum tree_code code
,
3046 tree expr_type
, tree op0
, tree op1
)
3048 value_range vr0
= VR_INITIALIZER
;
3049 value_range vr1
= VR_INITIALIZER
;
3051 /* Get value ranges for each operand. For constant operands, create
3052 a new value range with the operand to simplify processing. */
3053 if (TREE_CODE (op0
) == SSA_NAME
)
3054 vr0
= *(get_value_range (op0
));
3055 else if (is_gimple_min_invariant (op0
))
3056 set_value_range_to_value (&vr0
, op0
, NULL
);
3058 set_value_range_to_varying (&vr0
);
3060 if (TREE_CODE (op1
) == SSA_NAME
)
3061 vr1
= *(get_value_range (op1
));
3062 else if (is_gimple_min_invariant (op1
))
3063 set_value_range_to_value (&vr1
, op1
, NULL
);
3065 set_value_range_to_varying (&vr1
);
3067 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3069 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3070 and based on the other operand, for example if it was deduced from a
3071 symbolic comparison. When a bound of the range of the first operand
3072 is invariant, we set the corresponding bound of the new range to INF
3073 in order to avoid recursing on the range of the second operand. */
3074 if (vr
->type
== VR_VARYING
3075 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3076 && TREE_CODE (op1
) == SSA_NAME
3077 && vr0
.type
== VR_RANGE
3078 && symbolic_range_based_on_p (&vr0
, op1
))
3080 const bool minus_p
= (code
== MINUS_EXPR
);
3081 value_range n_vr1
= VR_INITIALIZER
;
3083 /* Try with VR0 and [-INF, OP1]. */
3084 if (is_gimple_min_invariant (minus_p
? vr0
.max
: vr0
.min
))
3085 set_value_range (&n_vr1
, VR_RANGE
, vrp_val_min (expr_type
), op1
, NULL
);
3087 /* Try with VR0 and [OP1, +INF]. */
3088 else if (is_gimple_min_invariant (minus_p
? vr0
.min
: vr0
.max
))
3089 set_value_range (&n_vr1
, VR_RANGE
, op1
, vrp_val_max (expr_type
), NULL
);
3091 /* Try with VR0 and [OP1, OP1]. */
3093 set_value_range (&n_vr1
, VR_RANGE
, op1
, op1
, NULL
);
3095 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &n_vr1
);
3098 if (vr
->type
== VR_VARYING
3099 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3100 && TREE_CODE (op0
) == SSA_NAME
3101 && vr1
.type
== VR_RANGE
3102 && symbolic_range_based_on_p (&vr1
, op0
))
3104 const bool minus_p
= (code
== MINUS_EXPR
);
3105 value_range n_vr0
= VR_INITIALIZER
;
3107 /* Try with [-INF, OP0] and VR1. */
3108 if (is_gimple_min_invariant (minus_p
? vr1
.max
: vr1
.min
))
3109 set_value_range (&n_vr0
, VR_RANGE
, vrp_val_min (expr_type
), op0
, NULL
);
3111 /* Try with [OP0, +INF] and VR1. */
3112 else if (is_gimple_min_invariant (minus_p
? vr1
.min
: vr1
.max
))
3113 set_value_range (&n_vr0
, VR_RANGE
, op0
, vrp_val_max (expr_type
), NULL
);
3115 /* Try with [OP0, OP0] and VR1. */
3117 set_value_range (&n_vr0
, VR_RANGE
, op0
, op0
, NULL
);
3119 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &n_vr0
, &vr1
);
3122 /* If we didn't derive a range for MINUS_EXPR, and
3123 op1's range is ~[op0,op0] or vice-versa, then we
3124 can derive a non-null range. This happens often for
3125 pointer subtraction. */
3126 if (vr
->type
== VR_VARYING
3127 && code
== MINUS_EXPR
3128 && TREE_CODE (op0
) == SSA_NAME
3129 && ((vr0
.type
== VR_ANTI_RANGE
3131 && vr0
.min
== vr0
.max
)
3132 || (vr1
.type
== VR_ANTI_RANGE
3134 && vr1
.min
== vr1
.max
)))
3135 set_value_range_to_nonnull (vr
, TREE_TYPE (op0
));
3138 /* Extract range information from a unary operation CODE based on
3139 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3140 The resulting range is stored in *VR. */
3143 extract_range_from_unary_expr (value_range
*vr
,
3144 enum tree_code code
, tree type
,
3145 value_range
*vr0_
, tree op0_type
)
3147 value_range vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3149 /* VRP only operates on integral and pointer types. */
3150 if (!(INTEGRAL_TYPE_P (op0_type
)
3151 || POINTER_TYPE_P (op0_type
))
3152 || !(INTEGRAL_TYPE_P (type
)
3153 || POINTER_TYPE_P (type
)))
3155 set_value_range_to_varying (vr
);
3159 /* If VR0 is UNDEFINED, so is the result. */
3160 if (vr0
.type
== VR_UNDEFINED
)
3162 set_value_range_to_undefined (vr
);
3166 /* Handle operations that we express in terms of others. */
3167 if (code
== PAREN_EXPR
|| code
== OBJ_TYPE_REF
)
3169 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3170 copy_value_range (vr
, &vr0
);
3173 else if (code
== NEGATE_EXPR
)
3175 /* -X is simply 0 - X, so re-use existing code that also handles
3176 anti-ranges fine. */
3177 value_range zero
= VR_INITIALIZER
;
3178 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3179 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3182 else if (code
== BIT_NOT_EXPR
)
3184 /* ~X is simply -1 - X, so re-use existing code that also handles
3185 anti-ranges fine. */
3186 value_range minusone
= VR_INITIALIZER
;
3187 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3188 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3189 type
, &minusone
, &vr0
);
3193 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3194 and express op ~[] as (op []') U (op []''). */
3195 if (vr0
.type
== VR_ANTI_RANGE
3196 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3198 extract_range_from_unary_expr (vr
, code
, type
, &vrtem0
, op0_type
);
3199 if (vrtem1
.type
!= VR_UNDEFINED
)
3201 value_range vrres
= VR_INITIALIZER
;
3202 extract_range_from_unary_expr (&vrres
, code
, type
,
3204 vrp_meet (vr
, &vrres
);
3209 if (CONVERT_EXPR_CODE_P (code
))
3211 tree inner_type
= op0_type
;
3212 tree outer_type
= type
;
3214 /* If the expression evaluates to a pointer, we are only interested in
3215 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3216 if (POINTER_TYPE_P (type
))
3218 if (range_is_nonnull (&vr0
))
3219 set_value_range_to_nonnull (vr
, type
);
3220 else if (range_is_null (&vr0
))
3221 set_value_range_to_null (vr
, type
);
3223 set_value_range_to_varying (vr
);
3227 /* If VR0 is varying and we increase the type precision, assume
3228 a full range for the following transformation. */
3229 if (vr0
.type
== VR_VARYING
3230 && INTEGRAL_TYPE_P (inner_type
)
3231 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3233 vr0
.type
= VR_RANGE
;
3234 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3235 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3238 /* If VR0 is a constant range or anti-range and the conversion is
3239 not truncating we can convert the min and max values and
3240 canonicalize the resulting range. Otherwise we can do the
3241 conversion if the size of the range is less than what the
3242 precision of the target type can represent and the range is
3243 not an anti-range. */
3244 if ((vr0
.type
== VR_RANGE
3245 || vr0
.type
== VR_ANTI_RANGE
)
3246 && TREE_CODE (vr0
.min
) == INTEGER_CST
3247 && TREE_CODE (vr0
.max
) == INTEGER_CST
3248 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3249 || (vr0
.type
== VR_RANGE
3250 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3251 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3252 size_int (TYPE_PRECISION (outer_type
)))))))
3254 tree new_min
, new_max
;
3255 new_min
= force_fit_type (outer_type
, wi::to_widest (vr0
.min
),
3257 new_max
= force_fit_type (outer_type
, wi::to_widest (vr0
.max
),
3259 set_and_canonicalize_value_range (vr
, vr0
.type
,
3260 new_min
, new_max
, NULL
);
3264 set_value_range_to_varying (vr
);
3267 else if (code
== ABS_EXPR
)
3272 /* Pass through vr0 in the easy cases. */
3273 if (TYPE_UNSIGNED (type
)
3274 || value_range_nonnegative_p (&vr0
))
3276 copy_value_range (vr
, &vr0
);
3280 /* For the remaining varying or symbolic ranges we can't do anything
3282 if (vr0
.type
== VR_VARYING
3283 || symbolic_range_p (&vr0
))
3285 set_value_range_to_varying (vr
);
3289 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3291 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3292 && ((vr0
.type
== VR_RANGE
3293 && vrp_val_is_min (vr0
.min
))
3294 || (vr0
.type
== VR_ANTI_RANGE
3295 && !vrp_val_is_min (vr0
.min
))))
3297 set_value_range_to_varying (vr
);
3301 /* ABS_EXPR may flip the range around, if the original range
3302 included negative values. */
3303 if (!vrp_val_is_min (vr0
.min
))
3304 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3306 min
= TYPE_MAX_VALUE (type
);
3308 if (!vrp_val_is_min (vr0
.max
))
3309 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3311 max
= TYPE_MAX_VALUE (type
);
3313 cmp
= compare_values (min
, max
);
3315 /* If a VR_ANTI_RANGEs contains zero, then we have
3316 ~[-INF, min(MIN, MAX)]. */
3317 if (vr0
.type
== VR_ANTI_RANGE
)
3319 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3321 /* Take the lower of the two values. */
3325 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3326 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3327 flag_wrapv is set and the original anti-range doesn't include
3328 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3329 if (TYPE_OVERFLOW_WRAPS (type
))
3331 tree type_min_value
= TYPE_MIN_VALUE (type
);
3333 min
= (vr0
.min
!= type_min_value
3334 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3335 build_int_cst (TREE_TYPE (type_min_value
), 1))
3339 min
= TYPE_MIN_VALUE (type
);
3343 /* All else has failed, so create the range [0, INF], even for
3344 flag_wrapv since TYPE_MIN_VALUE is in the original
3346 vr0
.type
= VR_RANGE
;
3347 min
= build_int_cst (type
, 0);
3348 max
= TYPE_MAX_VALUE (type
);
3352 /* If the range contains zero then we know that the minimum value in the
3353 range will be zero. */
3354 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3358 min
= build_int_cst (type
, 0);
3362 /* If the range was reversed, swap MIN and MAX. */
3364 std::swap (min
, max
);
3367 cmp
= compare_values (min
, max
);
3368 if (cmp
== -2 || cmp
== 1)
3370 /* If the new range has its limits swapped around (MIN > MAX),
3371 then the operation caused one of them to wrap around, mark
3372 the new range VARYING. */
3373 set_value_range_to_varying (vr
);
3376 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3380 /* For unhandled operations fall back to varying. */
3381 set_value_range_to_varying (vr
);
3386 /* Extract range information from a unary expression CODE OP0 based on
3387 the range of its operand with resulting type TYPE.
3388 The resulting range is stored in *VR. */
3391 extract_range_from_unary_expr (value_range
*vr
, enum tree_code code
,
3392 tree type
, tree op0
)
3394 value_range vr0
= VR_INITIALIZER
;
3396 /* Get value ranges for the operand. For constant operands, create
3397 a new value range with the operand to simplify processing. */
3398 if (TREE_CODE (op0
) == SSA_NAME
)
3399 vr0
= *(get_value_range (op0
));
3400 else if (is_gimple_min_invariant (op0
))
3401 set_value_range_to_value (&vr0
, op0
, NULL
);
3403 set_value_range_to_varying (&vr0
);
3405 extract_range_from_unary_expr (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3409 /* Extract range information from a conditional expression STMT based on
3410 the ranges of each of its operands and the expression code. */
3413 extract_range_from_cond_expr (value_range
*vr
, gassign
*stmt
)
3416 value_range vr0
= VR_INITIALIZER
;
3417 value_range vr1
= VR_INITIALIZER
;
3419 /* Get value ranges for each operand. For constant operands, create
3420 a new value range with the operand to simplify processing. */
3421 op0
= gimple_assign_rhs2 (stmt
);
3422 if (TREE_CODE (op0
) == SSA_NAME
)
3423 vr0
= *(get_value_range (op0
));
3424 else if (is_gimple_min_invariant (op0
))
3425 set_value_range_to_value (&vr0
, op0
, NULL
);
3427 set_value_range_to_varying (&vr0
);
3429 op1
= gimple_assign_rhs3 (stmt
);
3430 if (TREE_CODE (op1
) == SSA_NAME
)
3431 vr1
= *(get_value_range (op1
));
3432 else if (is_gimple_min_invariant (op1
))
3433 set_value_range_to_value (&vr1
, op1
, NULL
);
3435 set_value_range_to_varying (&vr1
);
3437 /* The resulting value range is the union of the operand ranges */
3438 copy_value_range (vr
, &vr0
);
3439 vrp_meet (vr
, &vr1
);
3443 /* Extract range information from a comparison expression EXPR based
3444 on the range of its operand and the expression code. */
3447 extract_range_from_comparison (value_range
*vr
, enum tree_code code
,
3448 tree type
, tree op0
, tree op1
)
3453 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3456 /* A disadvantage of using a special infinity as an overflow
3457 representation is that we lose the ability to record overflow
3458 when we don't have an infinity. So we have to ignore a result
3459 which relies on overflow. */
3463 /* Since this expression was found on the RHS of an assignment,
3464 its type may be different from _Bool. Convert VAL to EXPR's
3466 val
= fold_convert (type
, val
);
3467 if (is_gimple_min_invariant (val
))
3468 set_value_range_to_value (vr
, val
, vr
->equiv
);
3470 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3473 /* The result of a comparison is always true or false. */
3474 set_value_range_to_truthvalue (vr
, type
);
3477 /* Helper function for simplify_internal_call_using_ranges and
3478 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3479 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3480 always overflow. Set *OVF to true if it is known to always
3484 check_for_binary_op_overflow (enum tree_code subcode
, tree type
,
3485 tree op0
, tree op1
, bool *ovf
)
3487 value_range vr0
= VR_INITIALIZER
;
3488 value_range vr1
= VR_INITIALIZER
;
3489 if (TREE_CODE (op0
) == SSA_NAME
)
3490 vr0
= *get_value_range (op0
);
3491 else if (TREE_CODE (op0
) == INTEGER_CST
)
3492 set_value_range_to_value (&vr0
, op0
, NULL
);
3494 set_value_range_to_varying (&vr0
);
3496 if (TREE_CODE (op1
) == SSA_NAME
)
3497 vr1
= *get_value_range (op1
);
3498 else if (TREE_CODE (op1
) == INTEGER_CST
)
3499 set_value_range_to_value (&vr1
, op1
, NULL
);
3501 set_value_range_to_varying (&vr1
);
3503 if (!range_int_cst_p (&vr0
)
3504 || TREE_OVERFLOW (vr0
.min
)
3505 || TREE_OVERFLOW (vr0
.max
))
3507 vr0
.min
= vrp_val_min (TREE_TYPE (op0
));
3508 vr0
.max
= vrp_val_max (TREE_TYPE (op0
));
3510 if (!range_int_cst_p (&vr1
)
3511 || TREE_OVERFLOW (vr1
.min
)
3512 || TREE_OVERFLOW (vr1
.max
))
3514 vr1
.min
= vrp_val_min (TREE_TYPE (op1
));
3515 vr1
.max
= vrp_val_max (TREE_TYPE (op1
));
3517 *ovf
= arith_overflowed_p (subcode
, type
, vr0
.min
,
3518 subcode
== MINUS_EXPR
? vr1
.max
: vr1
.min
);
3519 if (arith_overflowed_p (subcode
, type
, vr0
.max
,
3520 subcode
== MINUS_EXPR
? vr1
.min
: vr1
.max
) != *ovf
)
3522 if (subcode
== MULT_EXPR
)
3524 if (arith_overflowed_p (subcode
, type
, vr0
.min
, vr1
.max
) != *ovf
3525 || arith_overflowed_p (subcode
, type
, vr0
.max
, vr1
.min
) != *ovf
)
3530 /* So far we found that there is an overflow on the boundaries.
3531 That doesn't prove that there is an overflow even for all values
3532 in between the boundaries. For that compute widest_int range
3533 of the result and see if it doesn't overlap the range of
3535 widest_int wmin
, wmax
;
3538 w
[0] = wi::to_widest (vr0
.min
);
3539 w
[1] = wi::to_widest (vr0
.max
);
3540 w
[2] = wi::to_widest (vr1
.min
);
3541 w
[3] = wi::to_widest (vr1
.max
);
3542 for (i
= 0; i
< 4; i
++)
3548 wt
= wi::add (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3551 wt
= wi::sub (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3554 wt
= wi::mul (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3566 wmin
= wi::smin (wmin
, wt
);
3567 wmax
= wi::smax (wmax
, wt
);
3570 /* The result of op0 CODE op1 is known to be in range
3572 widest_int wtmin
= wi::to_widest (vrp_val_min (type
));
3573 widest_int wtmax
= wi::to_widest (vrp_val_max (type
));
3574 /* If all values in [wmin, wmax] are smaller than
3575 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3576 the arithmetic operation will always overflow. */
3577 if (wmax
< wtmin
|| wmin
> wtmax
)
3584 /* Try to derive a nonnegative or nonzero range out of STMT relying
3585 primarily on generic routines in fold in conjunction with range data.
3586 Store the result in *VR */
3589 extract_range_basic (value_range
*vr
, gimple
*stmt
)
3592 tree type
= gimple_expr_type (stmt
);
3594 if (is_gimple_call (stmt
))
3597 int mini
, maxi
, zerov
= 0, prec
;
3598 enum tree_code subcode
= ERROR_MARK
;
3599 combined_fn cfn
= gimple_call_combined_fn (stmt
);
3603 case CFN_BUILT_IN_CONSTANT_P
:
3604 /* If the call is __builtin_constant_p and the argument is a
3605 function parameter resolve it to false. This avoids bogus
3606 array bound warnings.
3607 ??? We could do this as early as inlining is finished. */
3608 arg
= gimple_call_arg (stmt
, 0);
3609 if (TREE_CODE (arg
) == SSA_NAME
3610 && SSA_NAME_IS_DEFAULT_DEF (arg
)
3611 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
3612 && cfun
->after_inlining
)
3614 set_value_range_to_null (vr
, type
);
3618 /* Both __builtin_ffs* and __builtin_popcount return
3622 arg
= gimple_call_arg (stmt
, 0);
3623 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3626 if (TREE_CODE (arg
) == SSA_NAME
)
3628 value_range
*vr0
= get_value_range (arg
);
3629 /* If arg is non-zero, then ffs or popcount
3631 if ((vr0
->type
== VR_RANGE
3632 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 0)
3633 || (vr0
->type
== VR_ANTI_RANGE
3634 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 1))
3636 /* If some high bits are known to be zero,
3637 we can decrease the maximum. */
3638 if (vr0
->type
== VR_RANGE
3639 && TREE_CODE (vr0
->max
) == INTEGER_CST
3640 && !operand_less_p (vr0
->min
,
3641 build_zero_cst (TREE_TYPE (vr0
->min
))))
3642 maxi
= tree_floor_log2 (vr0
->max
) + 1;
3645 /* __builtin_parity* returns [0, 1]. */
3650 /* __builtin_c[lt]z* return [0, prec-1], except for
3651 when the argument is 0, but that is undefined behavior.
3652 On many targets where the CLZ RTL or optab value is defined
3653 for 0 the value is prec, so include that in the range
3656 arg
= gimple_call_arg (stmt
, 0);
3657 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3660 if (optab_handler (clz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3662 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3664 /* Handle only the single common value. */
3666 /* Magic value to give up, unless vr0 proves
3669 if (TREE_CODE (arg
) == SSA_NAME
)
3671 value_range
*vr0
= get_value_range (arg
);
3672 /* From clz of VR_RANGE minimum we can compute
3674 if (vr0
->type
== VR_RANGE
3675 && TREE_CODE (vr0
->min
) == INTEGER_CST
)
3677 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min
);
3681 else if (vr0
->type
== VR_ANTI_RANGE
3682 && integer_zerop (vr0
->min
))
3689 /* From clz of VR_RANGE maximum we can compute
3691 if (vr0
->type
== VR_RANGE
3692 && TREE_CODE (vr0
->max
) == INTEGER_CST
)
3694 mini
= prec
- 1 - tree_floor_log2 (vr0
->max
);
3702 /* __builtin_ctz* return [0, prec-1], except for
3703 when the argument is 0, but that is undefined behavior.
3704 If there is a ctz optab for this mode and
3705 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3706 otherwise just assume 0 won't be seen. */
3708 arg
= gimple_call_arg (stmt
, 0);
3709 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3712 if (optab_handler (ctz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3714 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3717 /* Handle only the two common values. */
3720 else if (zerov
== prec
)
3723 /* Magic value to give up, unless vr0 proves
3727 if (TREE_CODE (arg
) == SSA_NAME
)
3729 value_range
*vr0
= get_value_range (arg
);
3730 /* If arg is non-zero, then use [0, prec - 1]. */
3731 if ((vr0
->type
== VR_RANGE
3732 && integer_nonzerop (vr0
->min
))
3733 || (vr0
->type
== VR_ANTI_RANGE
3734 && integer_zerop (vr0
->min
)))
3739 /* If some high bits are known to be zero,
3740 we can decrease the result maximum. */
3741 if (vr0
->type
== VR_RANGE
3742 && TREE_CODE (vr0
->max
) == INTEGER_CST
)
3744 maxi
= tree_floor_log2 (vr0
->max
);
3745 /* For vr0 [0, 0] give up. */
3753 /* __builtin_clrsb* returns [0, prec-1]. */
3755 arg
= gimple_call_arg (stmt
, 0);
3756 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3761 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, mini
),
3762 build_int_cst (type
, maxi
), NULL
);
3764 case CFN_UBSAN_CHECK_ADD
:
3765 subcode
= PLUS_EXPR
;
3767 case CFN_UBSAN_CHECK_SUB
:
3768 subcode
= MINUS_EXPR
;
3770 case CFN_UBSAN_CHECK_MUL
:
3771 subcode
= MULT_EXPR
;
3773 case CFN_GOACC_DIM_SIZE
:
3774 case CFN_GOACC_DIM_POS
:
3775 /* Optimizing these two internal functions helps the loop
3776 optimizer eliminate outer comparisons. Size is [1,N]
3777 and pos is [0,N-1]. */
3779 bool is_pos
= cfn
== CFN_GOACC_DIM_POS
;
3780 int axis
= oacc_get_ifn_dim_arg (stmt
);
3781 int size
= oacc_get_fn_dim_size (current_function_decl
, axis
);
3784 /* If it's dynamic, the backend might know a hardware
3786 size
= targetm
.goacc
.dim_limit (axis
);
3788 tree type
= TREE_TYPE (gimple_call_lhs (stmt
));
3789 set_value_range (vr
, VR_RANGE
,
3790 build_int_cst (type
, is_pos
? 0 : 1),
3791 size
? build_int_cst (type
, size
- is_pos
)
3792 : vrp_val_max (type
), NULL
);
3795 case CFN_BUILT_IN_STRLEN
:
3796 if (tree lhs
= gimple_call_lhs (stmt
))
3797 if (ptrdiff_type_node
3798 && (TYPE_PRECISION (ptrdiff_type_node
)
3799 == TYPE_PRECISION (TREE_TYPE (lhs
))))
3801 tree type
= TREE_TYPE (lhs
);
3802 tree max
= vrp_val_max (ptrdiff_type_node
);
3803 wide_int wmax
= wi::to_wide (max
, TYPE_PRECISION (TREE_TYPE (max
)));
3804 tree range_min
= build_zero_cst (type
);
3805 tree range_max
= wide_int_to_tree (type
, wmax
- 1);
3806 set_value_range (vr
, VR_RANGE
, range_min
, range_max
, NULL
);
3813 if (subcode
!= ERROR_MARK
)
3815 bool saved_flag_wrapv
= flag_wrapv
;
3816 /* Pretend the arithmetics is wrapping. If there is
3817 any overflow, we'll complain, but will actually do
3818 wrapping operation. */
3820 extract_range_from_binary_expr (vr
, subcode
, type
,
3821 gimple_call_arg (stmt
, 0),
3822 gimple_call_arg (stmt
, 1));
3823 flag_wrapv
= saved_flag_wrapv
;
3825 /* If for both arguments vrp_valueize returned non-NULL,
3826 this should have been already folded and if not, it
3827 wasn't folded because of overflow. Avoid removing the
3828 UBSAN_CHECK_* calls in that case. */
3829 if (vr
->type
== VR_RANGE
3830 && (vr
->min
== vr
->max
3831 || operand_equal_p (vr
->min
, vr
->max
, 0)))
3832 set_value_range_to_varying (vr
);
3836 /* Handle extraction of the two results (result of arithmetics and
3837 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
3838 internal function. Similarly from ATOMIC_COMPARE_EXCHANGE. */
3839 else if (is_gimple_assign (stmt
)
3840 && (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
3841 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
3842 && INTEGRAL_TYPE_P (type
))
3844 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3845 tree op
= gimple_assign_rhs1 (stmt
);
3846 if (TREE_CODE (op
) == code
&& TREE_CODE (TREE_OPERAND (op
, 0)) == SSA_NAME
)
3848 gimple
*g
= SSA_NAME_DEF_STMT (TREE_OPERAND (op
, 0));
3849 if (is_gimple_call (g
) && gimple_call_internal_p (g
))
3851 enum tree_code subcode
= ERROR_MARK
;
3852 switch (gimple_call_internal_fn (g
))
3854 case IFN_ADD_OVERFLOW
:
3855 subcode
= PLUS_EXPR
;
3857 case IFN_SUB_OVERFLOW
:
3858 subcode
= MINUS_EXPR
;
3860 case IFN_MUL_OVERFLOW
:
3861 subcode
= MULT_EXPR
;
3863 case IFN_ATOMIC_COMPARE_EXCHANGE
:
3864 if (code
== IMAGPART_EXPR
)
3866 /* This is the boolean return value whether compare and
3867 exchange changed anything or not. */
3868 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, 0),
3869 build_int_cst (type
, 1), NULL
);
3876 if (subcode
!= ERROR_MARK
)
3878 tree op0
= gimple_call_arg (g
, 0);
3879 tree op1
= gimple_call_arg (g
, 1);
3880 if (code
== IMAGPART_EXPR
)
3883 if (check_for_binary_op_overflow (subcode
, type
,
3885 set_value_range_to_value (vr
,
3886 build_int_cst (type
, ovf
),
3888 else if (TYPE_PRECISION (type
) == 1
3889 && !TYPE_UNSIGNED (type
))
3890 set_value_range_to_varying (vr
);
3892 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, 0),
3893 build_int_cst (type
, 1), NULL
);
3895 else if (types_compatible_p (type
, TREE_TYPE (op0
))
3896 && types_compatible_p (type
, TREE_TYPE (op1
)))
3898 bool saved_flag_wrapv
= flag_wrapv
;
3899 /* Pretend the arithmetics is wrapping. If there is
3900 any overflow, IMAGPART_EXPR will be set. */
3902 extract_range_from_binary_expr (vr
, subcode
, type
,
3904 flag_wrapv
= saved_flag_wrapv
;
3908 value_range vr0
= VR_INITIALIZER
;
3909 value_range vr1
= VR_INITIALIZER
;
3910 bool saved_flag_wrapv
= flag_wrapv
;
3911 /* Pretend the arithmetics is wrapping. If there is
3912 any overflow, IMAGPART_EXPR will be set. */
3914 extract_range_from_unary_expr (&vr0
, NOP_EXPR
,
3916 extract_range_from_unary_expr (&vr1
, NOP_EXPR
,
3918 extract_range_from_binary_expr_1 (vr
, subcode
, type
,
3920 flag_wrapv
= saved_flag_wrapv
;
3927 if (INTEGRAL_TYPE_P (type
)
3928 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
3929 set_value_range_to_nonnegative (vr
, type
);
3930 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
3932 set_value_range_to_nonnull (vr
, type
);
3934 set_value_range_to_varying (vr
);
3938 /* Try to compute a useful range out of assignment STMT and store it
3942 extract_range_from_assignment (value_range
*vr
, gassign
*stmt
)
3944 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3946 if (code
== ASSERT_EXPR
)
3947 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
3948 else if (code
== SSA_NAME
)
3949 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
3950 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
3951 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
3952 gimple_expr_type (stmt
),
3953 gimple_assign_rhs1 (stmt
),
3954 gimple_assign_rhs2 (stmt
));
3955 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
3956 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
3957 gimple_expr_type (stmt
),
3958 gimple_assign_rhs1 (stmt
));
3959 else if (code
== COND_EXPR
)
3960 extract_range_from_cond_expr (vr
, stmt
);
3961 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3962 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
3963 gimple_expr_type (stmt
),
3964 gimple_assign_rhs1 (stmt
),
3965 gimple_assign_rhs2 (stmt
));
3966 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
3967 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
3968 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
3970 set_value_range_to_varying (vr
);
3972 if (vr
->type
== VR_VARYING
)
3973 extract_range_basic (vr
, stmt
);
3976 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3977 would be profitable to adjust VR using scalar evolution information
3978 for VAR. If so, update VR with the new limits. */
3981 adjust_range_with_scev (value_range
*vr
, struct loop
*loop
,
3982 gimple
*stmt
, tree var
)
3984 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
3985 enum ev_direction dir
;
3987 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3988 better opportunities than a regular range, but I'm not sure. */
3989 if (vr
->type
== VR_ANTI_RANGE
)
3992 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
3994 /* Like in PR19590, scev can return a constant function. */
3995 if (is_gimple_min_invariant (chrec
))
3997 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
4001 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
4004 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
4005 tem
= op_with_constant_singleton_value_range (init
);
4008 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
4009 tem
= op_with_constant_singleton_value_range (step
);
4013 /* If STEP is symbolic, we can't know whether INIT will be the
4014 minimum or maximum value in the range. Also, unless INIT is
4015 a simple expression, compare_values and possibly other functions
4016 in tree-vrp won't be able to handle it. */
4017 if (step
== NULL_TREE
4018 || !is_gimple_min_invariant (step
)
4019 || !valid_value_p (init
))
4022 dir
= scev_direction (chrec
);
4023 if (/* Do not adjust ranges if we do not know whether the iv increases
4024 or decreases, ... */
4025 dir
== EV_DIR_UNKNOWN
4026 /* ... or if it may wrap. */
4027 || scev_probably_wraps_p (NULL_TREE
, init
, step
, stmt
,
4028 get_chrec_loop (chrec
), true))
4031 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4032 negative_overflow_infinity and positive_overflow_infinity,
4033 because we have concluded that the loop probably does not
4036 type
= TREE_TYPE (var
);
4037 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
4038 tmin
= lower_bound_in_type (type
, type
);
4040 tmin
= TYPE_MIN_VALUE (type
);
4041 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
4042 tmax
= upper_bound_in_type (type
, type
);
4044 tmax
= TYPE_MAX_VALUE (type
);
4046 /* Try to use estimated number of iterations for the loop to constrain the
4047 final value in the evolution. */
4048 if (TREE_CODE (step
) == INTEGER_CST
4049 && is_gimple_val (init
)
4050 && (TREE_CODE (init
) != SSA_NAME
4051 || get_value_range (init
)->type
== VR_RANGE
))
4055 /* We are only entering here for loop header PHI nodes, so using
4056 the number of latch executions is the correct thing to use. */
4057 if (max_loop_iterations (loop
, &nit
))
4059 value_range maxvr
= VR_INITIALIZER
;
4060 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
4063 widest_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
,
4065 /* If the multiplication overflowed we can't do a meaningful
4066 adjustment. Likewise if the result doesn't fit in the type
4067 of the induction variable. For a signed type we have to
4068 check whether the result has the expected signedness which
4069 is that of the step as number of iterations is unsigned. */
4071 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
4073 || wi::gts_p (wtmp
, 0) == wi::gts_p (step
, 0)))
4075 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
4076 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
4077 TREE_TYPE (init
), init
, tem
);
4078 /* Likewise if the addition did. */
4079 if (maxvr
.type
== VR_RANGE
)
4081 value_range initvr
= VR_INITIALIZER
;
4083 if (TREE_CODE (init
) == SSA_NAME
)
4084 initvr
= *(get_value_range (init
));
4085 else if (is_gimple_min_invariant (init
))
4086 set_value_range_to_value (&initvr
, init
, NULL
);
4090 /* Check if init + nit * step overflows. Though we checked
4091 scev {init, step}_loop doesn't wrap, it is not enough
4092 because the loop may exit immediately. Overflow could
4093 happen in the plus expression in this case. */
4094 if ((dir
== EV_DIR_DECREASES
4095 && compare_values (maxvr
.min
, initvr
.min
) != -1)
4096 || (dir
== EV_DIR_GROWS
4097 && compare_values (maxvr
.max
, initvr
.max
) != 1))
4107 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4112 /* For VARYING or UNDEFINED ranges, just about anything we get
4113 from scalar evolutions should be better. */
4115 if (dir
== EV_DIR_DECREASES
)
4120 else if (vr
->type
== VR_RANGE
)
4125 if (dir
== EV_DIR_DECREASES
)
4127 /* INIT is the maximum value. If INIT is lower than VR->MAX
4128 but no smaller than VR->MIN, set VR->MAX to INIT. */
4129 if (compare_values (init
, max
) == -1)
4132 /* According to the loop information, the variable does not
4134 if (compare_values (min
, tmin
) == -1)
4140 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4141 if (compare_values (init
, min
) == 1)
4144 if (compare_values (tmax
, max
) == -1)
4151 /* If we just created an invalid range with the minimum
4152 greater than the maximum, we fail conservatively.
4153 This should happen only in unreachable
4154 parts of code, or for invalid programs. */
4155 if (compare_values (min
, max
) == 1)
4158 /* Even for valid range info, sometimes overflow flag will leak in.
4159 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
4161 if (TREE_OVERFLOW_P (min
))
4162 min
= drop_tree_overflow (min
);
4163 if (TREE_OVERFLOW_P (max
))
4164 max
= drop_tree_overflow (max
);
4166 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
4170 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4172 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4173 all the values in the ranges.
4175 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4177 - Return NULL_TREE if it is not always possible to determine the
4178 value of the comparison.
4180 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4181 overflow infinity was used in the test. */
4185 compare_ranges (enum tree_code comp
, value_range
*vr0
, value_range
*vr1
,
4186 bool *strict_overflow_p
)
4188 /* VARYING or UNDEFINED ranges cannot be compared. */
4189 if (vr0
->type
== VR_VARYING
4190 || vr0
->type
== VR_UNDEFINED
4191 || vr1
->type
== VR_VARYING
4192 || vr1
->type
== VR_UNDEFINED
)
4195 /* Anti-ranges need to be handled separately. */
4196 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
4198 /* If both are anti-ranges, then we cannot compute any
4200 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
4203 /* These comparisons are never statically computable. */
4210 /* Equality can be computed only between a range and an
4211 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4212 if (vr0
->type
== VR_RANGE
)
4214 /* To simplify processing, make VR0 the anti-range. */
4215 value_range
*tmp
= vr0
;
4220 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
4222 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
4223 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
4224 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4229 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4230 operands around and change the comparison code. */
4231 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4233 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
4234 std::swap (vr0
, vr1
);
4237 if (comp
== EQ_EXPR
)
4239 /* Equality may only be computed if both ranges represent
4240 exactly one value. */
4241 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
4242 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
4244 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
4246 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
4248 if (cmp_min
== 0 && cmp_max
== 0)
4249 return boolean_true_node
;
4250 else if (cmp_min
!= -2 && cmp_max
!= -2)
4251 return boolean_false_node
;
4253 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4254 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
4255 strict_overflow_p
) == 1
4256 || compare_values_warnv (vr1
->min
, vr0
->max
,
4257 strict_overflow_p
) == 1)
4258 return boolean_false_node
;
4262 else if (comp
== NE_EXPR
)
4266 /* If VR0 is completely to the left or completely to the right
4267 of VR1, they are always different. Notice that we need to
4268 make sure that both comparisons yield similar results to
4269 avoid comparing values that cannot be compared at
4271 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4272 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4273 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
4274 return boolean_true_node
;
4276 /* If VR0 and VR1 represent a single value and are identical,
4278 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
4279 strict_overflow_p
) == 0
4280 && compare_values_warnv (vr1
->min
, vr1
->max
,
4281 strict_overflow_p
) == 0
4282 && compare_values_warnv (vr0
->min
, vr1
->min
,
4283 strict_overflow_p
) == 0
4284 && compare_values_warnv (vr0
->max
, vr1
->max
,
4285 strict_overflow_p
) == 0)
4286 return boolean_false_node
;
4288 /* Otherwise, they may or may not be different. */
4292 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4296 /* If VR0 is to the left of VR1, return true. */
4297 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4298 if ((comp
== LT_EXPR
&& tst
== -1)
4299 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4300 return boolean_true_node
;
4302 /* If VR0 is to the right of VR1, return false. */
4303 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4304 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4305 || (comp
== LE_EXPR
&& tst
== 1))
4306 return boolean_false_node
;
4308 /* Otherwise, we don't know. */
4316 /* Given a value range VR, a value VAL and a comparison code COMP, return
4317 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4318 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4319 always returns false. Return NULL_TREE if it is not always
4320 possible to determine the value of the comparison. Also set
4321 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4322 infinity was used in the test. */
4325 compare_range_with_value (enum tree_code comp
, value_range
*vr
, tree val
,
4326 bool *strict_overflow_p
)
4328 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4331 /* Anti-ranges need to be handled separately. */
4332 if (vr
->type
== VR_ANTI_RANGE
)
4334 /* For anti-ranges, the only predicates that we can compute at
4335 compile time are equality and inequality. */
4342 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4343 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4344 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4349 if (comp
== EQ_EXPR
)
4351 /* EQ_EXPR may only be computed if VR represents exactly
4353 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4355 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4357 return boolean_true_node
;
4358 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4359 return boolean_false_node
;
4361 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4362 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4363 return boolean_false_node
;
4367 else if (comp
== NE_EXPR
)
4369 /* If VAL is not inside VR, then they are always different. */
4370 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4371 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4372 return boolean_true_node
;
4374 /* If VR represents exactly one value equal to VAL, then return
4376 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4377 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4378 return boolean_false_node
;
4380 /* Otherwise, they may or may not be different. */
4383 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4387 /* If VR is to the left of VAL, return true. */
4388 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4389 if ((comp
== LT_EXPR
&& tst
== -1)
4390 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4391 return boolean_true_node
;
4393 /* If VR is to the right of VAL, return false. */
4394 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4395 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4396 || (comp
== LE_EXPR
&& tst
== 1))
4397 return boolean_false_node
;
4399 /* Otherwise, we don't know. */
4402 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4406 /* If VR is to the right of VAL, return true. */
4407 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4408 if ((comp
== GT_EXPR
&& tst
== 1)
4409 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4410 return boolean_true_node
;
4412 /* If VR is to the left of VAL, return false. */
4413 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4414 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4415 || (comp
== GE_EXPR
&& tst
== -1))
4416 return boolean_false_node
;
4418 /* Otherwise, we don't know. */
4426 /* Debugging dumps. */
4428 void dump_value_range (FILE *, const value_range
*);
4429 void debug_value_range (value_range
*);
4430 void dump_all_value_ranges (FILE *);
4431 void debug_all_value_ranges (void);
4432 void dump_vr_equiv (FILE *, bitmap
);
4433 void debug_vr_equiv (bitmap
);
4436 /* Dump value range VR to FILE. */
4439 dump_value_range (FILE *file
, const value_range
*vr
)
4442 fprintf (file
, "[]");
4443 else if (vr
->type
== VR_UNDEFINED
)
4444 fprintf (file
, "UNDEFINED");
4445 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4447 tree type
= TREE_TYPE (vr
->min
);
4449 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4451 if (INTEGRAL_TYPE_P (type
)
4452 && !TYPE_UNSIGNED (type
)
4453 && vrp_val_is_min (vr
->min
))
4454 fprintf (file
, "-INF");
4456 print_generic_expr (file
, vr
->min
, 0);
4458 fprintf (file
, ", ");
4460 if (INTEGRAL_TYPE_P (type
)
4461 && vrp_val_is_max (vr
->max
))
4462 fprintf (file
, "+INF");
4464 print_generic_expr (file
, vr
->max
, 0);
4466 fprintf (file
, "]");
4473 fprintf (file
, " EQUIVALENCES: { ");
4475 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4477 print_generic_expr (file
, ssa_name (i
), 0);
4478 fprintf (file
, " ");
4482 fprintf (file
, "} (%u elements)", c
);
4485 else if (vr
->type
== VR_VARYING
)
4486 fprintf (file
, "VARYING");
4488 fprintf (file
, "INVALID RANGE");
4492 /* Dump value range VR to stderr. */
4495 debug_value_range (value_range
*vr
)
4497 dump_value_range (stderr
, vr
);
4498 fprintf (stderr
, "\n");
4502 /* Dump value ranges of all SSA_NAMEs to FILE. */
4505 dump_all_value_ranges (FILE *file
)
4509 for (i
= 0; i
< num_vr_values
; i
++)
4513 print_generic_expr (file
, ssa_name (i
), 0);
4514 fprintf (file
, ": ");
4515 dump_value_range (file
, vr_value
[i
]);
4516 fprintf (file
, "\n");
4520 fprintf (file
, "\n");
4524 /* Dump all value ranges to stderr. */
4527 debug_all_value_ranges (void)
4529 dump_all_value_ranges (stderr
);
4533 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4534 create a new SSA name N and return the assertion assignment
4535 'N = ASSERT_EXPR <V, V OP W>'. */
4538 build_assert_expr_for (tree cond
, tree v
)
4543 gcc_assert (TREE_CODE (v
) == SSA_NAME
4544 && COMPARISON_CLASS_P (cond
));
4546 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4547 assertion
= gimple_build_assign (NULL_TREE
, a
);
4549 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4550 operand of the ASSERT_EXPR. Create it so the new name and the old one
4551 are registered in the replacement table so that we can fix the SSA web
4552 after adding all the ASSERT_EXPRs. */
4553 create_new_def_for (v
, assertion
, NULL
);
4559 /* Return false if EXPR is a predicate expression involving floating
4563 fp_predicate (gimple
*stmt
)
4565 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4567 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4570 /* If the range of values taken by OP can be inferred after STMT executes,
4571 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4572 describes the inferred range. Return true if a range could be
4576 infer_value_range (gimple
*stmt
, tree op
, tree_code
*comp_code_p
, tree
*val_p
)
4579 *comp_code_p
= ERROR_MARK
;
4581 /* Do not attempt to infer anything in names that flow through
4583 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4586 /* If STMT is the last statement of a basic block with no normal
4587 successors, there is no point inferring anything about any of its
4588 operands. We would not be able to find a proper insertion point
4589 for the assertion, anyway. */
4590 if (stmt_ends_bb_p (stmt
))
4595 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
4596 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
4602 if (infer_nonnull_range (stmt
, op
))
4604 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4605 *comp_code_p
= NE_EXPR
;
4613 void dump_asserts_for (FILE *, tree
);
4614 void debug_asserts_for (tree
);
4615 void dump_all_asserts (FILE *);
4616 void debug_all_asserts (void);
4618 /* Dump all the registered assertions for NAME to FILE. */
4621 dump_asserts_for (FILE *file
, tree name
)
4625 fprintf (file
, "Assertions to be inserted for ");
4626 print_generic_expr (file
, name
, 0);
4627 fprintf (file
, "\n");
4629 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4632 fprintf (file
, "\t");
4633 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4634 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4637 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4638 loc
->e
->dest
->index
);
4639 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4641 fprintf (file
, "\n\tPREDICATE: ");
4642 print_generic_expr (file
, loc
->expr
, 0);
4643 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
4644 print_generic_expr (file
, loc
->val
, 0);
4645 fprintf (file
, "\n\n");
4649 fprintf (file
, "\n");
4653 /* Dump all the registered assertions for NAME to stderr. */
4656 debug_asserts_for (tree name
)
4658 dump_asserts_for (stderr
, name
);
4662 /* Dump all the registered assertions for all the names to FILE. */
4665 dump_all_asserts (FILE *file
)
4670 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4671 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4672 dump_asserts_for (file
, ssa_name (i
));
4673 fprintf (file
, "\n");
4677 /* Dump all the registered assertions for all the names to stderr. */
4680 debug_all_asserts (void)
4682 dump_all_asserts (stderr
);
4685 /* Push the assert info for NAME, EXPR, COMP_CODE and VAL to ASSERTS. */
4688 add_assert_info (vec
<assert_info
> &asserts
,
4689 tree name
, tree expr
, enum tree_code comp_code
, tree val
)
4692 info
.comp_code
= comp_code
;
4696 asserts
.safe_push (info
);
4699 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4700 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4701 E->DEST, then register this location as a possible insertion point
4702 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4704 BB, E and SI provide the exact insertion point for the new
4705 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4706 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4707 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4708 must not be NULL. */
4711 register_new_assert_for (tree name
, tree expr
,
4712 enum tree_code comp_code
,
4716 gimple_stmt_iterator si
)
4718 assert_locus
*n
, *loc
, *last_loc
;
4719 basic_block dest_bb
;
4721 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4724 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4725 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4727 /* Never build an assert comparing against an integer constant with
4728 TREE_OVERFLOW set. This confuses our undefined overflow warning
4730 if (TREE_OVERFLOW_P (val
))
4731 val
= drop_tree_overflow (val
);
4733 /* The new assertion A will be inserted at BB or E. We need to
4734 determine if the new location is dominated by a previously
4735 registered location for A. If we are doing an edge insertion,
4736 assume that A will be inserted at E->DEST. Note that this is not
4739 If E is a critical edge, it will be split. But even if E is
4740 split, the new block will dominate the same set of blocks that
4743 The reverse, however, is not true, blocks dominated by E->DEST
4744 will not be dominated by the new block created to split E. So,
4745 if the insertion location is on a critical edge, we will not use
4746 the new location to move another assertion previously registered
4747 at a block dominated by E->DEST. */
4748 dest_bb
= (bb
) ? bb
: e
->dest
;
4750 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4751 VAL at a block dominating DEST_BB, then we don't need to insert a new
4752 one. Similarly, if the same assertion already exists at a block
4753 dominated by DEST_BB and the new location is not on a critical
4754 edge, then update the existing location for the assertion (i.e.,
4755 move the assertion up in the dominance tree).
4757 Note, this is implemented as a simple linked list because there
4758 should not be more than a handful of assertions registered per
4759 name. If this becomes a performance problem, a table hashed by
4760 COMP_CODE and VAL could be implemented. */
4761 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4765 if (loc
->comp_code
== comp_code
4767 || operand_equal_p (loc
->val
, val
, 0))
4768 && (loc
->expr
== expr
4769 || operand_equal_p (loc
->expr
, expr
, 0)))
4771 /* If E is not a critical edge and DEST_BB
4772 dominates the existing location for the assertion, move
4773 the assertion up in the dominance tree by updating its
4774 location information. */
4775 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
4776 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
4785 /* Update the last node of the list and move to the next one. */
4790 /* If we didn't find an assertion already registered for
4791 NAME COMP_CODE VAL, add a new one at the end of the list of
4792 assertions associated with NAME. */
4793 n
= XNEW (struct assert_locus
);
4797 n
->comp_code
= comp_code
;
4805 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
4807 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
4810 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4811 Extract a suitable test code and value and store them into *CODE_P and
4812 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4814 If no extraction was possible, return FALSE, otherwise return TRUE.
4816 If INVERT is true, then we invert the result stored into *CODE_P. */
4819 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
4820 tree cond_op0
, tree cond_op1
,
4821 bool invert
, enum tree_code
*code_p
,
4824 enum tree_code comp_code
;
4827 /* Otherwise, we have a comparison of the form NAME COMP VAL
4828 or VAL COMP NAME. */
4829 if (name
== cond_op1
)
4831 /* If the predicate is of the form VAL COMP NAME, flip
4832 COMP around because we need to register NAME as the
4833 first operand in the predicate. */
4834 comp_code
= swap_tree_comparison (cond_code
);
4837 else if (name
== cond_op0
)
4839 /* The comparison is of the form NAME COMP VAL, so the
4840 comparison code remains unchanged. */
4841 comp_code
= cond_code
;
4847 /* Invert the comparison code as necessary. */
4849 comp_code
= invert_tree_comparison (comp_code
, 0);
4851 /* VRP only handles integral and pointer types. */
4852 if (! INTEGRAL_TYPE_P (TREE_TYPE (val
))
4853 && ! POINTER_TYPE_P (TREE_TYPE (val
)))
4856 /* Do not register always-false predicates.
4857 FIXME: this works around a limitation in fold() when dealing with
4858 enumerations. Given 'enum { N1, N2 } x;', fold will not
4859 fold 'if (x > N2)' to 'if (0)'. */
4860 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
4861 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
4863 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
4864 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
4866 if (comp_code
== GT_EXPR
4868 || compare_values (val
, max
) == 0))
4871 if (comp_code
== LT_EXPR
4873 || compare_values (val
, min
) == 0))
4876 *code_p
= comp_code
;
4881 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4882 (otherwise return VAL). VAL and MASK must be zero-extended for
4883 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4884 (to transform signed values into unsigned) and at the end xor
4888 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
4889 const wide_int
&sgnbit
, unsigned int prec
)
4891 wide_int bit
= wi::one (prec
), res
;
4894 wide_int val
= val_in
^ sgnbit
;
4895 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
4898 if ((res
& bit
) == 0)
4901 res
= (val
+ bit
).and_not (res
);
4903 if (wi::gtu_p (res
, val
))
4904 return res
^ sgnbit
;
4906 return val
^ sgnbit
;
4909 /* Helper for overflow_comparison_p
4911 OP0 CODE OP1 is a comparison. Examine the comparison and potentially
4912 OP1's defining statement to see if it ultimately has the form
4913 OP0 CODE (OP0 PLUS INTEGER_CST)
4915 If so, return TRUE indicating this is an overflow test and store into
4916 *NEW_CST an updated constant that can be used in a narrowed range test.
4918 REVERSED indicates if the comparison was originally:
4922 This affects how we build the updated constant. */
4925 overflow_comparison_p_1 (enum tree_code code
, tree op0
, tree op1
,
4926 bool follow_assert_exprs
, bool reversed
, tree
*new_cst
)
4928 /* See if this is a relational operation between two SSA_NAMES with
4929 unsigned, overflow wrapping values. If so, check it more deeply. */
4930 if ((code
== LT_EXPR
|| code
== LE_EXPR
4931 || code
== GE_EXPR
|| code
== GT_EXPR
)
4932 && TREE_CODE (op0
) == SSA_NAME
4933 && TREE_CODE (op1
) == SSA_NAME
4934 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
4935 && TYPE_UNSIGNED (TREE_TYPE (op0
))
4936 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
)))
4938 gimple
*op1_def
= SSA_NAME_DEF_STMT (op1
);
4940 /* If requested, follow any ASSERT_EXPRs backwards for OP1. */
4941 if (follow_assert_exprs
)
4943 while (gimple_assign_single_p (op1_def
)
4944 && TREE_CODE (gimple_assign_rhs1 (op1_def
)) == ASSERT_EXPR
)
4946 op1
= TREE_OPERAND (gimple_assign_rhs1 (op1_def
), 0);
4947 if (TREE_CODE (op1
) != SSA_NAME
)
4949 op1_def
= SSA_NAME_DEF_STMT (op1
);
4953 /* Now look at the defining statement of OP1 to see if it adds
4954 or subtracts a nonzero constant from another operand. */
4956 && is_gimple_assign (op1_def
)
4957 && gimple_assign_rhs_code (op1_def
) == PLUS_EXPR
4958 && TREE_CODE (gimple_assign_rhs2 (op1_def
)) == INTEGER_CST
4959 && !integer_zerop (gimple_assign_rhs2 (op1_def
)))
4961 tree target
= gimple_assign_rhs1 (op1_def
);
4963 /* If requested, follow ASSERT_EXPRs backwards for op0 looking
4964 for one where TARGET appears on the RHS. */
4965 if (follow_assert_exprs
)
4967 /* Now see if that "other operand" is op0, following the chain
4968 of ASSERT_EXPRs if necessary. */
4969 gimple
*op0_def
= SSA_NAME_DEF_STMT (op0
);
4970 while (op0
!= target
4971 && gimple_assign_single_p (op0_def
)
4972 && TREE_CODE (gimple_assign_rhs1 (op0_def
)) == ASSERT_EXPR
)
4974 op0
= TREE_OPERAND (gimple_assign_rhs1 (op0_def
), 0);
4975 if (TREE_CODE (op0
) != SSA_NAME
)
4977 op0_def
= SSA_NAME_DEF_STMT (op0
);
4981 /* If we did not find our target SSA_NAME, then this is not
4982 an overflow test. */
4986 tree type
= TREE_TYPE (op0
);
4987 wide_int max
= wi::max_value (TYPE_PRECISION (type
), UNSIGNED
);
4988 tree inc
= gimple_assign_rhs2 (op1_def
);
4990 *new_cst
= wide_int_to_tree (type
, max
+ inc
);
4992 *new_cst
= wide_int_to_tree (type
, max
- inc
);
4999 /* OP0 CODE OP1 is a comparison. Examine the comparison and potentially
5000 OP1's defining statement to see if it ultimately has the form
5001 OP0 CODE (OP0 PLUS INTEGER_CST)
5003 If so, return TRUE indicating this is an overflow test and store into
5004 *NEW_CST an updated constant that can be used in a narrowed range test.
5006 These statements are left as-is in the IL to facilitate discovery of
5007 {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But
5008 the alternate range representation is often useful within VRP. */
5011 overflow_comparison_p (tree_code code
, tree name
, tree val
,
5012 bool use_equiv_p
, tree
*new_cst
)
5014 if (overflow_comparison_p_1 (code
, name
, val
, use_equiv_p
, false, new_cst
))
5016 return overflow_comparison_p_1 (swap_tree_comparison (code
), val
, name
,
5017 use_equiv_p
, true, new_cst
);
5021 /* Try to register an edge assertion for SSA name NAME on edge E for
5022 the condition COND contributing to the conditional jump pointed to by BSI.
5023 Invert the condition COND if INVERT is true. */
5026 register_edge_assert_for_2 (tree name
, edge e
,
5027 enum tree_code cond_code
,
5028 tree cond_op0
, tree cond_op1
, bool invert
,
5029 vec
<assert_info
> &asserts
)
5032 enum tree_code comp_code
;
5034 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5037 invert
, &comp_code
, &val
))
5040 /* Queue the assert. */
5042 if (overflow_comparison_p (comp_code
, name
, val
, false, &x
))
5044 enum tree_code new_code
= ((comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
5045 ? GT_EXPR
: LE_EXPR
);
5046 add_assert_info (asserts
, name
, name
, new_code
, x
);
5048 add_assert_info (asserts
, name
, name
, comp_code
, val
);
5050 /* In the case of NAME <= CST and NAME being defined as
5051 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5052 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5053 This catches range and anti-range tests. */
5054 if ((comp_code
== LE_EXPR
5055 || comp_code
== GT_EXPR
)
5056 && TREE_CODE (val
) == INTEGER_CST
5057 && TYPE_UNSIGNED (TREE_TYPE (val
)))
5059 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5060 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
5062 /* Extract CST2 from the (optional) addition. */
5063 if (is_gimple_assign (def_stmt
)
5064 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
5066 name2
= gimple_assign_rhs1 (def_stmt
);
5067 cst2
= gimple_assign_rhs2 (def_stmt
);
5068 if (TREE_CODE (name2
) == SSA_NAME
5069 && TREE_CODE (cst2
) == INTEGER_CST
)
5070 def_stmt
= SSA_NAME_DEF_STMT (name2
);
5073 /* Extract NAME2 from the (optional) sign-changing cast. */
5074 if (gimple_assign_cast_p (def_stmt
))
5076 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
5077 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5078 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
5079 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
5080 name3
= gimple_assign_rhs1 (def_stmt
);
5083 /* If name3 is used later, create an ASSERT_EXPR for it. */
5084 if (name3
!= NULL_TREE
5085 && TREE_CODE (name3
) == SSA_NAME
5086 && (cst2
== NULL_TREE
5087 || TREE_CODE (cst2
) == INTEGER_CST
)
5088 && INTEGRAL_TYPE_P (TREE_TYPE (name3
)))
5092 /* Build an expression for the range test. */
5093 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
5094 if (cst2
!= NULL_TREE
)
5095 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5099 fprintf (dump_file
, "Adding assert for ");
5100 print_generic_expr (dump_file
, name3
, 0);
5101 fprintf (dump_file
, " from ");
5102 print_generic_expr (dump_file
, tmp
, 0);
5103 fprintf (dump_file
, "\n");
5106 add_assert_info (asserts
, name3
, tmp
, comp_code
, val
);
5109 /* If name2 is used later, create an ASSERT_EXPR for it. */
5110 if (name2
!= NULL_TREE
5111 && TREE_CODE (name2
) == SSA_NAME
5112 && TREE_CODE (cst2
) == INTEGER_CST
5113 && INTEGRAL_TYPE_P (TREE_TYPE (name2
)))
5117 /* Build an expression for the range test. */
5119 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
5120 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
5121 if (cst2
!= NULL_TREE
)
5122 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5126 fprintf (dump_file
, "Adding assert for ");
5127 print_generic_expr (dump_file
, name2
, 0);
5128 fprintf (dump_file
, " from ");
5129 print_generic_expr (dump_file
, tmp
, 0);
5130 fprintf (dump_file
, "\n");
5133 add_assert_info (asserts
, name2
, tmp
, comp_code
, val
);
5137 /* In the case of post-in/decrement tests like if (i++) ... and uses
5138 of the in/decremented value on the edge the extra name we want to
5139 assert for is not on the def chain of the name compared. Instead
5140 it is in the set of use stmts.
5141 Similar cases happen for conversions that were simplified through
5142 fold_{sign_changed,widened}_comparison. */
5143 if ((comp_code
== NE_EXPR
5144 || comp_code
== EQ_EXPR
)
5145 && TREE_CODE (val
) == INTEGER_CST
)
5147 imm_use_iterator ui
;
5149 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
5151 if (!is_gimple_assign (use_stmt
))
5154 /* Cut off to use-stmts that are dominating the predecessor. */
5155 if (!dominated_by_p (CDI_DOMINATORS
, e
->src
, gimple_bb (use_stmt
)))
5158 tree name2
= gimple_assign_lhs (use_stmt
);
5159 if (TREE_CODE (name2
) != SSA_NAME
)
5162 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
5164 if (code
== PLUS_EXPR
5165 || code
== MINUS_EXPR
)
5167 cst
= gimple_assign_rhs2 (use_stmt
);
5168 if (TREE_CODE (cst
) != INTEGER_CST
)
5170 cst
= int_const_binop (code
, val
, cst
);
5172 else if (CONVERT_EXPR_CODE_P (code
))
5174 /* For truncating conversions we cannot record
5176 if (comp_code
== NE_EXPR
5177 && (TYPE_PRECISION (TREE_TYPE (name2
))
5178 < TYPE_PRECISION (TREE_TYPE (name
))))
5180 cst
= fold_convert (TREE_TYPE (name2
), val
);
5185 if (TREE_OVERFLOW_P (cst
))
5186 cst
= drop_tree_overflow (cst
);
5187 add_assert_info (asserts
, name2
, name2
, comp_code
, cst
);
5191 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
5192 && TREE_CODE (val
) == INTEGER_CST
)
5194 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5195 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
5196 tree val2
= NULL_TREE
;
5197 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
5198 wide_int mask
= wi::zero (prec
);
5199 unsigned int nprec
= prec
;
5200 enum tree_code rhs_code
= ERROR_MARK
;
5202 if (is_gimple_assign (def_stmt
))
5203 rhs_code
= gimple_assign_rhs_code (def_stmt
);
5205 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
5206 assert that A != CST1 -+ CST2. */
5207 if ((comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5208 && (rhs_code
== PLUS_EXPR
|| rhs_code
== MINUS_EXPR
))
5210 tree op0
= gimple_assign_rhs1 (def_stmt
);
5211 tree op1
= gimple_assign_rhs2 (def_stmt
);
5212 if (TREE_CODE (op0
) == SSA_NAME
5213 && TREE_CODE (op1
) == INTEGER_CST
)
5215 enum tree_code reverse_op
= (rhs_code
== PLUS_EXPR
5216 ? MINUS_EXPR
: PLUS_EXPR
);
5217 op1
= int_const_binop (reverse_op
, val
, op1
);
5218 if (TREE_OVERFLOW (op1
))
5219 op1
= drop_tree_overflow (op1
);
5220 add_assert_info (asserts
, op0
, op0
, comp_code
, op1
);
5224 /* Add asserts for NAME cmp CST and NAME being defined
5225 as NAME = (int) NAME2. */
5226 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
5227 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
5228 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
5229 && gimple_assign_cast_p (def_stmt
))
5231 name2
= gimple_assign_rhs1 (def_stmt
);
5232 if (CONVERT_EXPR_CODE_P (rhs_code
)
5233 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5234 && TYPE_UNSIGNED (TREE_TYPE (name2
))
5235 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
5236 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
5237 || !tree_int_cst_equal (val
,
5238 TYPE_MIN_VALUE (TREE_TYPE (val
)))))
5241 enum tree_code new_comp_code
= comp_code
;
5243 cst
= fold_convert (TREE_TYPE (name2
),
5244 TYPE_MIN_VALUE (TREE_TYPE (val
)));
5245 /* Build an expression for the range test. */
5246 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
5247 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
5248 fold_convert (TREE_TYPE (name2
), val
));
5249 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5251 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
5252 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
5253 build_int_cst (TREE_TYPE (name2
), 1));
5258 fprintf (dump_file
, "Adding assert for ");
5259 print_generic_expr (dump_file
, name2
, 0);
5260 fprintf (dump_file
, " from ");
5261 print_generic_expr (dump_file
, tmp
, 0);
5262 fprintf (dump_file
, "\n");
5265 add_assert_info (asserts
, name2
, tmp
, new_comp_code
, cst
);
5269 /* Add asserts for NAME cmp CST and NAME being defined as
5270 NAME = NAME2 >> CST2.
5272 Extract CST2 from the right shift. */
5273 if (rhs_code
== RSHIFT_EXPR
)
5275 name2
= gimple_assign_rhs1 (def_stmt
);
5276 cst2
= gimple_assign_rhs2 (def_stmt
);
5277 if (TREE_CODE (name2
) == SSA_NAME
5278 && tree_fits_uhwi_p (cst2
)
5279 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5280 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
5281 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
))))
5283 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
5284 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
5287 if (val2
!= NULL_TREE
5288 && TREE_CODE (val2
) == INTEGER_CST
5289 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
5293 enum tree_code new_comp_code
= comp_code
;
5297 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5299 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
5301 tree type
= build_nonstandard_integer_type (prec
, 1);
5302 tmp
= build1 (NOP_EXPR
, type
, name2
);
5303 val2
= fold_convert (type
, val2
);
5305 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
5306 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
5307 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
5309 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5312 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5314 if (minval
== new_val
)
5315 new_val
= NULL_TREE
;
5320 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5323 new_val
= NULL_TREE
;
5325 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
5332 fprintf (dump_file
, "Adding assert for ");
5333 print_generic_expr (dump_file
, name2
, 0);
5334 fprintf (dump_file
, " from ");
5335 print_generic_expr (dump_file
, tmp
, 0);
5336 fprintf (dump_file
, "\n");
5339 add_assert_info (asserts
, name2
, tmp
, new_comp_code
, new_val
);
5343 /* Add asserts for NAME cmp CST and NAME being defined as
5344 NAME = NAME2 & CST2.
5346 Extract CST2 from the and.
5349 NAME = (unsigned) NAME2;
5350 casts where NAME's type is unsigned and has smaller precision
5351 than NAME2's type as if it was NAME = NAME2 & MASK. */
5352 names
[0] = NULL_TREE
;
5353 names
[1] = NULL_TREE
;
5355 if (rhs_code
== BIT_AND_EXPR
5356 || (CONVERT_EXPR_CODE_P (rhs_code
)
5357 && INTEGRAL_TYPE_P (TREE_TYPE (val
))
5358 && TYPE_UNSIGNED (TREE_TYPE (val
))
5359 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5362 name2
= gimple_assign_rhs1 (def_stmt
);
5363 if (rhs_code
== BIT_AND_EXPR
)
5364 cst2
= gimple_assign_rhs2 (def_stmt
);
5367 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5368 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
5370 if (TREE_CODE (name2
) == SSA_NAME
5371 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5372 && TREE_CODE (cst2
) == INTEGER_CST
5373 && !integer_zerop (cst2
)
5375 || TYPE_UNSIGNED (TREE_TYPE (val
))))
5377 gimple
*def_stmt2
= SSA_NAME_DEF_STMT (name2
);
5378 if (gimple_assign_cast_p (def_stmt2
))
5380 names
[1] = gimple_assign_rhs1 (def_stmt2
);
5381 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
5382 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
5383 || (TYPE_PRECISION (TREE_TYPE (name2
))
5384 != TYPE_PRECISION (TREE_TYPE (names
[1]))))
5385 names
[1] = NULL_TREE
;
5390 if (names
[0] || names
[1])
5392 wide_int minv
, maxv
, valv
, cst2v
;
5393 wide_int tem
, sgnbit
;
5394 bool valid_p
= false, valn
, cst2n
;
5395 enum tree_code ccode
= comp_code
;
5397 valv
= wide_int::from (val
, nprec
, UNSIGNED
);
5398 cst2v
= wide_int::from (cst2
, nprec
, UNSIGNED
);
5399 valn
= wi::neg_p (valv
, TYPE_SIGN (TREE_TYPE (val
)));
5400 cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (TREE_TYPE (val
)));
5401 /* If CST2 doesn't have most significant bit set,
5402 but VAL is negative, we have comparison like
5403 if ((x & 0x123) > -4) (always true). Just give up. */
5407 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5409 sgnbit
= wi::zero (nprec
);
5410 minv
= valv
& cst2v
;
5414 /* Minimum unsigned value for equality is VAL & CST2
5415 (should be equal to VAL, otherwise we probably should
5416 have folded the comparison into false) and
5417 maximum unsigned value is VAL | ~CST2. */
5418 maxv
= valv
| ~cst2v
;
5423 tem
= valv
| ~cst2v
;
5424 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5428 sgnbit
= wi::zero (nprec
);
5431 /* If (VAL | ~CST2) is all ones, handle it as
5432 (X & CST2) < VAL. */
5437 sgnbit
= wi::zero (nprec
);
5440 if (!cst2n
&& wi::neg_p (cst2v
))
5441 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5450 if (tem
== wi::mask (nprec
- 1, false, nprec
))
5456 sgnbit
= wi::zero (nprec
);
5461 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5462 is VAL and maximum unsigned value is ~0. For signed
5463 comparison, if CST2 doesn't have most significant bit
5464 set, handle it similarly. If CST2 has MSB set,
5465 the minimum is the same, and maximum is ~0U/2. */
5468 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5470 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5474 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5480 /* Find out smallest MINV where MINV > VAL
5481 && (MINV & CST2) == MINV, if any. If VAL is signed and
5482 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5483 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5486 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5491 /* Minimum unsigned value for <= is 0 and maximum
5492 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5493 Otherwise, find smallest VAL2 where VAL2 > VAL
5494 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5496 For signed comparison, if CST2 doesn't have most
5497 significant bit set, handle it similarly. If CST2 has
5498 MSB set, the maximum is the same and minimum is INT_MIN. */
5503 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5515 /* Minimum unsigned value for < is 0 and maximum
5516 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5517 Otherwise, find smallest VAL2 where VAL2 > VAL
5518 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5520 For signed comparison, if CST2 doesn't have most
5521 significant bit set, handle it similarly. If CST2 has
5522 MSB set, the maximum is the same and minimum is INT_MIN. */
5531 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5545 && (maxv
- minv
) != -1)
5547 tree tmp
, new_val
, type
;
5550 for (i
= 0; i
< 2; i
++)
5553 wide_int maxv2
= maxv
;
5555 type
= TREE_TYPE (names
[i
]);
5556 if (!TYPE_UNSIGNED (type
))
5558 type
= build_nonstandard_integer_type (nprec
, 1);
5559 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5563 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5564 wide_int_to_tree (type
, -minv
));
5565 maxv2
= maxv
- minv
;
5567 new_val
= wide_int_to_tree (type
, maxv2
);
5571 fprintf (dump_file
, "Adding assert for ");
5572 print_generic_expr (dump_file
, names
[i
], 0);
5573 fprintf (dump_file
, " from ");
5574 print_generic_expr (dump_file
, tmp
, 0);
5575 fprintf (dump_file
, "\n");
5578 add_assert_info (asserts
, names
[i
], tmp
, LE_EXPR
, new_val
);
5585 /* OP is an operand of a truth value expression which is known to have
5586 a particular value. Register any asserts for OP and for any
5587 operands in OP's defining statement.
5589 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5590 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5593 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5594 edge e
, vec
<assert_info
> &asserts
)
5598 enum tree_code rhs_code
;
5600 /* We only care about SSA_NAMEs. */
5601 if (TREE_CODE (op
) != SSA_NAME
)
5604 /* We know that OP will have a zero or nonzero value. */
5605 val
= build_int_cst (TREE_TYPE (op
), 0);
5606 add_assert_info (asserts
, op
, op
, code
, val
);
5608 /* Now look at how OP is set. If it's set from a comparison,
5609 a truth operation or some bit operations, then we may be able
5610 to register information about the operands of that assignment. */
5611 op_def
= SSA_NAME_DEF_STMT (op
);
5612 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5615 rhs_code
= gimple_assign_rhs_code (op_def
);
5617 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5619 bool invert
= (code
== EQ_EXPR
? true : false);
5620 tree op0
= gimple_assign_rhs1 (op_def
);
5621 tree op1
= gimple_assign_rhs2 (op_def
);
5623 if (TREE_CODE (op0
) == SSA_NAME
)
5624 register_edge_assert_for_2 (op0
, e
, rhs_code
, op0
, op1
, invert
, asserts
);
5625 if (TREE_CODE (op1
) == SSA_NAME
)
5626 register_edge_assert_for_2 (op1
, e
, rhs_code
, op0
, op1
, invert
, asserts
);
5628 else if ((code
== NE_EXPR
5629 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5631 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5633 /* Recurse on each operand. */
5634 tree op0
= gimple_assign_rhs1 (op_def
);
5635 tree op1
= gimple_assign_rhs2 (op_def
);
5636 if (TREE_CODE (op0
) == SSA_NAME
5637 && has_single_use (op0
))
5638 register_edge_assert_for_1 (op0
, code
, e
, asserts
);
5639 if (TREE_CODE (op1
) == SSA_NAME
5640 && has_single_use (op1
))
5641 register_edge_assert_for_1 (op1
, code
, e
, asserts
);
5643 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5644 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5646 /* Recurse, flipping CODE. */
5647 code
= invert_tree_comparison (code
, false);
5648 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, asserts
);
5650 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5652 /* Recurse through the copy. */
5653 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, asserts
);
5655 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5657 /* Recurse through the type conversion, unless it is a narrowing
5658 conversion or conversion from non-integral type. */
5659 tree rhs
= gimple_assign_rhs1 (op_def
);
5660 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
5661 && (TYPE_PRECISION (TREE_TYPE (rhs
))
5662 <= TYPE_PRECISION (TREE_TYPE (op
))))
5663 register_edge_assert_for_1 (rhs
, code
, e
, asserts
);
5667 /* Try to register an edge assertion for SSA name NAME on edge E for
5668 the condition COND contributing to the conditional jump pointed to by
5672 register_edge_assert_for (tree name
, edge e
,
5673 enum tree_code cond_code
, tree cond_op0
,
5674 tree cond_op1
, vec
<assert_info
> &asserts
)
5677 enum tree_code comp_code
;
5678 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5680 /* Do not attempt to infer anything in names that flow through
5682 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5685 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5691 /* Register ASSERT_EXPRs for name. */
5692 register_edge_assert_for_2 (name
, e
, cond_code
, cond_op0
,
5693 cond_op1
, is_else_edge
, asserts
);
5696 /* If COND is effectively an equality test of an SSA_NAME against
5697 the value zero or one, then we may be able to assert values
5698 for SSA_NAMEs which flow into COND. */
5700 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5701 statement of NAME we can assert both operands of the BIT_AND_EXPR
5702 have nonzero value. */
5703 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5704 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5706 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5708 if (is_gimple_assign (def_stmt
)
5709 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5711 tree op0
= gimple_assign_rhs1 (def_stmt
);
5712 tree op1
= gimple_assign_rhs2 (def_stmt
);
5713 register_edge_assert_for_1 (op0
, NE_EXPR
, e
, asserts
);
5714 register_edge_assert_for_1 (op1
, NE_EXPR
, e
, asserts
);
5718 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5719 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5721 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5722 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5724 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5726 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5727 necessarily zero value, or if type-precision is one. */
5728 if (is_gimple_assign (def_stmt
)
5729 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5730 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5731 || comp_code
== EQ_EXPR
)))
5733 tree op0
= gimple_assign_rhs1 (def_stmt
);
5734 tree op1
= gimple_assign_rhs2 (def_stmt
);
5735 register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, asserts
);
5736 register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, asserts
);
5741 /* Finish found ASSERTS for E and register them at GSI. */
5744 finish_register_edge_assert_for (edge e
, gimple_stmt_iterator gsi
,
5745 vec
<assert_info
> &asserts
)
5747 for (unsigned i
= 0; i
< asserts
.length (); ++i
)
5748 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5749 reachable from E. */
5750 if (live_on_edge (e
, asserts
[i
].name
))
5751 register_new_assert_for (asserts
[i
].name
, asserts
[i
].expr
,
5752 asserts
[i
].comp_code
, asserts
[i
].val
,
5758 /* Determine whether the outgoing edges of BB should receive an
5759 ASSERT_EXPR for each of the operands of BB's LAST statement.
5760 The last statement of BB must be a COND_EXPR.
5762 If any of the sub-graphs rooted at BB have an interesting use of
5763 the predicate operands, an assert location node is added to the
5764 list of assertions for the corresponding operands. */
5767 find_conditional_asserts (basic_block bb
, gcond
*last
)
5769 gimple_stmt_iterator bsi
;
5775 bsi
= gsi_for_stmt (last
);
5777 /* Look for uses of the operands in each of the sub-graphs
5778 rooted at BB. We need to check each of the outgoing edges
5779 separately, so that we know what kind of ASSERT_EXPR to
5781 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5786 /* Register the necessary assertions for each operand in the
5787 conditional predicate. */
5788 auto_vec
<assert_info
, 8> asserts
;
5789 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5790 register_edge_assert_for (op
, e
,
5791 gimple_cond_code (last
),
5792 gimple_cond_lhs (last
),
5793 gimple_cond_rhs (last
), asserts
);
5794 finish_register_edge_assert_for (e
, bsi
, asserts
);
5804 /* Compare two case labels sorting first by the destination bb index
5805 and then by the case value. */
5808 compare_case_labels (const void *p1
, const void *p2
)
5810 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5811 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5812 int idx1
= ci1
->bb
->index
;
5813 int idx2
= ci2
->bb
->index
;
5817 else if (idx1
== idx2
)
5819 /* Make sure the default label is first in a group. */
5820 if (!CASE_LOW (ci1
->expr
))
5822 else if (!CASE_LOW (ci2
->expr
))
5825 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5826 CASE_LOW (ci2
->expr
));
5832 /* Determine whether the outgoing edges of BB should receive an
5833 ASSERT_EXPR for each of the operands of BB's LAST statement.
5834 The last statement of BB must be a SWITCH_EXPR.
5836 If any of the sub-graphs rooted at BB have an interesting use of
5837 the predicate operands, an assert location node is added to the
5838 list of assertions for the corresponding operands. */
5841 find_switch_asserts (basic_block bb
, gswitch
*last
)
5843 gimple_stmt_iterator bsi
;
5846 struct case_info
*ci
;
5847 size_t n
= gimple_switch_num_labels (last
);
5848 #if GCC_VERSION >= 4000
5851 /* Work around GCC 3.4 bug (PR 37086). */
5852 volatile unsigned int idx
;
5855 bsi
= gsi_for_stmt (last
);
5856 op
= gimple_switch_index (last
);
5857 if (TREE_CODE (op
) != SSA_NAME
)
5860 /* Build a vector of case labels sorted by destination label. */
5861 ci
= XNEWVEC (struct case_info
, n
);
5862 for (idx
= 0; idx
< n
; ++idx
)
5864 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5865 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5867 edge default_edge
= find_edge (bb
, ci
[0].bb
);
5868 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5870 for (idx
= 0; idx
< n
; ++idx
)
5873 tree cl
= ci
[idx
].expr
;
5874 basic_block cbb
= ci
[idx
].bb
;
5876 min
= CASE_LOW (cl
);
5877 max
= CASE_HIGH (cl
);
5879 /* If there are multiple case labels with the same destination
5880 we need to combine them to a single value range for the edge. */
5881 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
5883 /* Skip labels until the last of the group. */
5886 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
5889 /* Pick up the maximum of the case label range. */
5890 if (CASE_HIGH (ci
[idx
].expr
))
5891 max
= CASE_HIGH (ci
[idx
].expr
);
5893 max
= CASE_LOW (ci
[idx
].expr
);
5896 /* Can't extract a useful assertion out of a range that includes the
5898 if (min
== NULL_TREE
)
5901 /* Find the edge to register the assert expr on. */
5902 e
= find_edge (bb
, cbb
);
5904 /* Register the necessary assertions for the operand in the
5906 auto_vec
<assert_info
, 8> asserts
;
5907 register_edge_assert_for (op
, e
,
5908 max
? GE_EXPR
: EQ_EXPR
,
5909 op
, fold_convert (TREE_TYPE (op
), min
),
5912 register_edge_assert_for (op
, e
, LE_EXPR
, op
,
5913 fold_convert (TREE_TYPE (op
), max
),
5915 finish_register_edge_assert_for (e
, bsi
, asserts
);
5920 if (!live_on_edge (default_edge
, op
))
5923 /* Now register along the default label assertions that correspond to the
5924 anti-range of each label. */
5925 int insertion_limit
= PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS
);
5926 if (insertion_limit
== 0)
5929 /* We can't do this if the default case shares a label with another case. */
5930 tree default_cl
= gimple_switch_default_label (last
);
5931 for (idx
= 1; idx
< n
; idx
++)
5934 tree cl
= gimple_switch_label (last
, idx
);
5935 if (CASE_LABEL (cl
) == CASE_LABEL (default_cl
))
5938 min
= CASE_LOW (cl
);
5939 max
= CASE_HIGH (cl
);
5941 /* Combine contiguous case ranges to reduce the number of assertions
5943 for (idx
= idx
+ 1; idx
< n
; idx
++)
5945 tree next_min
, next_max
;
5946 tree next_cl
= gimple_switch_label (last
, idx
);
5947 if (CASE_LABEL (next_cl
) == CASE_LABEL (default_cl
))
5950 next_min
= CASE_LOW (next_cl
);
5951 next_max
= CASE_HIGH (next_cl
);
5953 wide_int difference
= wi::sub (next_min
, max
? max
: min
);
5954 if (wi::eq_p (difference
, 1))
5955 max
= next_max
? next_max
: next_min
;
5961 if (max
== NULL_TREE
)
5963 /* Register the assertion OP != MIN. */
5964 auto_vec
<assert_info
, 8> asserts
;
5965 min
= fold_convert (TREE_TYPE (op
), min
);
5966 register_edge_assert_for (op
, default_edge
, NE_EXPR
, op
, min
,
5968 finish_register_edge_assert_for (default_edge
, bsi
, asserts
);
5972 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
5973 which will give OP the anti-range ~[MIN,MAX]. */
5974 tree uop
= fold_convert (unsigned_type_for (TREE_TYPE (op
)), op
);
5975 min
= fold_convert (TREE_TYPE (uop
), min
);
5976 max
= fold_convert (TREE_TYPE (uop
), max
);
5978 tree lhs
= fold_build2 (MINUS_EXPR
, TREE_TYPE (uop
), uop
, min
);
5979 tree rhs
= int_const_binop (MINUS_EXPR
, max
, min
);
5980 register_new_assert_for (op
, lhs
, GT_EXPR
, rhs
,
5981 NULL
, default_edge
, bsi
);
5984 if (--insertion_limit
== 0)
5990 /* Traverse all the statements in block BB looking for statements that
5991 may generate useful assertions for the SSA names in their operand.
5992 If a statement produces a useful assertion A for name N_i, then the
5993 list of assertions already generated for N_i is scanned to
5994 determine if A is actually needed.
5996 If N_i already had the assertion A at a location dominating the
5997 current location, then nothing needs to be done. Otherwise, the
5998 new location for A is recorded instead.
6000 1- For every statement S in BB, all the variables used by S are
6001 added to bitmap FOUND_IN_SUBGRAPH.
6003 2- If statement S uses an operand N in a way that exposes a known
6004 value range for N, then if N was not already generated by an
6005 ASSERT_EXPR, create a new assert location for N. For instance,
6006 if N is a pointer and the statement dereferences it, we can
6007 assume that N is not NULL.
6009 3- COND_EXPRs are a special case of #2. We can derive range
6010 information from the predicate but need to insert different
6011 ASSERT_EXPRs for each of the sub-graphs rooted at the
6012 conditional block. If the last statement of BB is a conditional
6013 expression of the form 'X op Y', then
6015 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6017 b) If the conditional is the only entry point to the sub-graph
6018 corresponding to the THEN_CLAUSE, recurse into it. On
6019 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6020 an ASSERT_EXPR is added for the corresponding variable.
6022 c) Repeat step (b) on the ELSE_CLAUSE.
6024 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6033 In this case, an assertion on the THEN clause is useful to
6034 determine that 'a' is always 9 on that edge. However, an assertion
6035 on the ELSE clause would be unnecessary.
6037 4- If BB does not end in a conditional expression, then we recurse
6038 into BB's dominator children.
6040 At the end of the recursive traversal, every SSA name will have a
6041 list of locations where ASSERT_EXPRs should be added. When a new
6042 location for name N is found, it is registered by calling
6043 register_new_assert_for. That function keeps track of all the
6044 registered assertions to prevent adding unnecessary assertions.
6045 For instance, if a pointer P_4 is dereferenced more than once in a
6046 dominator tree, only the location dominating all the dereference of
6047 P_4 will receive an ASSERT_EXPR. */
6050 find_assert_locations_1 (basic_block bb
, sbitmap live
)
6054 last
= last_stmt (bb
);
6056 /* If BB's last statement is a conditional statement involving integer
6057 operands, determine if we need to add ASSERT_EXPRs. */
6059 && gimple_code (last
) == GIMPLE_COND
6060 && !fp_predicate (last
)
6061 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
6062 find_conditional_asserts (bb
, as_a
<gcond
*> (last
));
6064 /* If BB's last statement is a switch statement involving integer
6065 operands, determine if we need to add ASSERT_EXPRs. */
6067 && gimple_code (last
) == GIMPLE_SWITCH
6068 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
6069 find_switch_asserts (bb
, as_a
<gswitch
*> (last
));
6071 /* Traverse all the statements in BB marking used names and looking
6072 for statements that may infer assertions for their used operands. */
6073 for (gimple_stmt_iterator si
= gsi_last_bb (bb
); !gsi_end_p (si
);
6080 stmt
= gsi_stmt (si
);
6082 if (is_gimple_debug (stmt
))
6085 /* See if we can derive an assertion for any of STMT's operands. */
6086 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6089 enum tree_code comp_code
;
6091 /* If op is not live beyond this stmt, do not bother to insert
6093 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
6096 /* If OP is used in such a way that we can infer a value
6097 range for it, and we don't find a previous assertion for
6098 it, create a new assertion location node for OP. */
6099 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
6101 /* If we are able to infer a nonzero value range for OP,
6102 then walk backwards through the use-def chain to see if OP
6103 was set via a typecast.
6105 If so, then we can also infer a nonzero value range
6106 for the operand of the NOP_EXPR. */
6107 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
6110 gimple
*def_stmt
= SSA_NAME_DEF_STMT (t
);
6112 while (is_gimple_assign (def_stmt
)
6113 && CONVERT_EXPR_CODE_P
6114 (gimple_assign_rhs_code (def_stmt
))
6116 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
6118 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
6120 t
= gimple_assign_rhs1 (def_stmt
);
6121 def_stmt
= SSA_NAME_DEF_STMT (t
);
6123 /* Note we want to register the assert for the
6124 operand of the NOP_EXPR after SI, not after the
6126 if (bitmap_bit_p (live
, SSA_NAME_VERSION (t
)))
6127 register_new_assert_for (t
, t
, comp_code
, value
,
6132 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
6137 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6138 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
6139 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
6140 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
6143 /* Traverse all PHI nodes in BB, updating live. */
6144 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
6147 use_operand_p arg_p
;
6149 gphi
*phi
= si
.phi ();
6150 tree res
= gimple_phi_result (phi
);
6152 if (virtual_operand_p (res
))
6155 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
6157 tree arg
= USE_FROM_PTR (arg_p
);
6158 if (TREE_CODE (arg
) == SSA_NAME
)
6159 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
6162 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
6166 /* Do an RPO walk over the function computing SSA name liveness
6167 on-the-fly and deciding on assert expressions to insert. */
6170 find_assert_locations (void)
6172 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6173 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6174 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
6177 live
= XCNEWVEC (sbitmap
, last_basic_block_for_fn (cfun
));
6178 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
6179 for (i
= 0; i
< rpo_cnt
; ++i
)
6182 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6183 the order we compute liveness and insert asserts we otherwise
6184 fail to insert asserts into the loop latch. */
6186 FOR_EACH_LOOP (loop
, 0)
6188 i
= loop
->latch
->index
;
6189 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
6190 for (gphi_iterator gsi
= gsi_start_phis (loop
->header
);
6191 !gsi_end_p (gsi
); gsi_next (&gsi
))
6193 gphi
*phi
= gsi
.phi ();
6194 if (virtual_operand_p (gimple_phi_result (phi
)))
6196 tree arg
= gimple_phi_arg_def (phi
, j
);
6197 if (TREE_CODE (arg
) == SSA_NAME
)
6199 if (live
[i
] == NULL
)
6201 live
[i
] = sbitmap_alloc (num_ssa_names
);
6202 bitmap_clear (live
[i
]);
6204 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
6209 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
6211 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
6217 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
6218 bitmap_clear (live
[rpo
[i
]]);
6221 /* Process BB and update the live information with uses in
6223 find_assert_locations_1 (bb
, live
[rpo
[i
]]);
6225 /* Merge liveness into the predecessor blocks and free it. */
6226 if (!bitmap_empty_p (live
[rpo
[i
]]))
6229 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6231 int pred
= e
->src
->index
;
6232 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
6237 live
[pred
] = sbitmap_alloc (num_ssa_names
);
6238 bitmap_clear (live
[pred
]);
6240 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
6242 if (bb_rpo
[pred
] < pred_rpo
)
6243 pred_rpo
= bb_rpo
[pred
];
6246 /* Record the RPO number of the last visited block that needs
6247 live information from this block. */
6248 last_rpo
[rpo
[i
]] = pred_rpo
;
6252 sbitmap_free (live
[rpo
[i
]]);
6253 live
[rpo
[i
]] = NULL
;
6256 /* We can free all successors live bitmaps if all their
6257 predecessors have been visited already. */
6258 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
6259 if (last_rpo
[e
->dest
->index
] == i
6260 && live
[e
->dest
->index
])
6262 sbitmap_free (live
[e
->dest
->index
]);
6263 live
[e
->dest
->index
] = NULL
;
6268 XDELETEVEC (bb_rpo
);
6269 XDELETEVEC (last_rpo
);
6270 for (i
= 0; i
< last_basic_block_for_fn (cfun
); ++i
)
6272 sbitmap_free (live
[i
]);
6276 /* Create an ASSERT_EXPR for NAME and insert it in the location
6277 indicated by LOC. Return true if we made any edge insertions. */
6280 process_assert_insertions_for (tree name
, assert_locus
*loc
)
6282 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6285 gimple
*assert_stmt
;
6289 /* If we have X <=> X do not insert an assert expr for that. */
6290 if (loc
->expr
== loc
->val
)
6293 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
6294 assert_stmt
= build_assert_expr_for (cond
, name
);
6297 /* We have been asked to insert the assertion on an edge. This
6298 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6299 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
6300 || (gimple_code (gsi_stmt (loc
->si
))
6303 gsi_insert_on_edge (loc
->e
, assert_stmt
);
6307 /* If the stmt iterator points at the end then this is an insertion
6308 at the beginning of a block. */
6309 if (gsi_end_p (loc
->si
))
6311 gimple_stmt_iterator si
= gsi_after_labels (loc
->bb
);
6312 gsi_insert_before (&si
, assert_stmt
, GSI_SAME_STMT
);
6316 /* Otherwise, we can insert right after LOC->SI iff the
6317 statement must not be the last statement in the block. */
6318 stmt
= gsi_stmt (loc
->si
);
6319 if (!stmt_ends_bb_p (stmt
))
6321 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
6325 /* If STMT must be the last statement in BB, we can only insert new
6326 assertions on the non-abnormal edge out of BB. Note that since
6327 STMT is not control flow, there may only be one non-abnormal/eh edge
6329 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
6330 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
6332 gsi_insert_on_edge (e
, assert_stmt
);
6339 /* Qsort helper for sorting assert locations. */
6342 compare_assert_loc (const void *pa
, const void *pb
)
6344 assert_locus
* const a
= *(assert_locus
* const *)pa
;
6345 assert_locus
* const b
= *(assert_locus
* const *)pb
;
6348 else if (a
->e
&& ! b
->e
)
6351 /* Sort after destination index. */
6352 if (! a
->e
&& ! b
->e
)
6354 else if (a
->e
->dest
->index
> b
->e
->dest
->index
)
6356 else if (a
->e
->dest
->index
< b
->e
->dest
->index
)
6359 /* Sort after comp_code. */
6360 if (a
->comp_code
> b
->comp_code
)
6362 else if (a
->comp_code
< b
->comp_code
)
6365 /* Break the tie using hashing and source/bb index. */
6366 hashval_t ha
= iterative_hash_expr (a
->expr
, iterative_hash_expr (a
->val
, 0));
6367 hashval_t hb
= iterative_hash_expr (b
->expr
, iterative_hash_expr (b
->val
, 0));
6369 return (a
->e
&& b
->e
6370 ? a
->e
->src
->index
- b
->e
->src
->index
6371 : a
->bb
->index
- b
->bb
->index
);
6375 /* Process all the insertions registered for every name N_i registered
6376 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6377 found in ASSERTS_FOR[i]. */
6380 process_assert_insertions (void)
6384 bool update_edges_p
= false;
6385 int num_asserts
= 0;
6387 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6388 dump_all_asserts (dump_file
);
6390 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
6392 assert_locus
*loc
= asserts_for
[i
];
6395 auto_vec
<assert_locus
*, 16> asserts
;
6396 for (; loc
; loc
= loc
->next
)
6397 asserts
.safe_push (loc
);
6398 asserts
.qsort (compare_assert_loc
);
6400 /* Push down common asserts to successors and remove redundant ones. */
6402 assert_locus
*common
= NULL
;
6403 unsigned commonj
= 0;
6404 for (unsigned j
= 0; j
< asserts
.length (); ++j
)
6410 || loc
->e
->dest
!= common
->e
->dest
6411 || loc
->comp_code
!= common
->comp_code
6412 || ! operand_equal_p (loc
->val
, common
->val
, 0)
6413 || ! operand_equal_p (loc
->expr
, common
->expr
, 0))
6419 else if (loc
->e
== asserts
[j
-1]->e
)
6421 /* Remove duplicate asserts. */
6422 if (commonj
== j
- 1)
6427 free (asserts
[j
-1]);
6428 asserts
[j
-1] = NULL
;
6433 if (EDGE_COUNT (common
->e
->dest
->preds
) == ecnt
)
6435 /* We have the same assertion on all incoming edges of a BB.
6436 Insert it at the beginning of that block. */
6437 loc
->bb
= loc
->e
->dest
;
6439 loc
->si
= gsi_none ();
6441 /* Clear asserts commoned. */
6442 for (; commonj
!= j
; ++commonj
)
6443 if (asserts
[commonj
])
6445 free (asserts
[commonj
]);
6446 asserts
[commonj
] = NULL
;
6452 for (unsigned j
= 0; j
< asserts
.length (); ++j
)
6457 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
6464 gsi_commit_edge_inserts ();
6466 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
6471 /* Traverse the flowgraph looking for conditional jumps to insert range
6472 expressions. These range expressions are meant to provide information
6473 to optimizations that need to reason in terms of value ranges. They
6474 will not be expanded into RTL. For instance, given:
6483 this pass will transform the code into:
6489 x = ASSERT_EXPR <x, x < y>
6494 y = ASSERT_EXPR <y, x >= y>
6498 The idea is that once copy and constant propagation have run, other
6499 optimizations will be able to determine what ranges of values can 'x'
6500 take in different paths of the code, simply by checking the reaching
6501 definition of 'x'. */
6504 insert_range_assertions (void)
6506 need_assert_for
= BITMAP_ALLOC (NULL
);
6507 asserts_for
= XCNEWVEC (assert_locus
*, num_ssa_names
);
6509 calculate_dominance_info (CDI_DOMINATORS
);
6511 find_assert_locations ();
6512 if (!bitmap_empty_p (need_assert_for
))
6514 process_assert_insertions ();
6515 update_ssa (TODO_update_ssa_no_phi
);
6518 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6520 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
6521 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
6525 BITMAP_FREE (need_assert_for
);
6528 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6529 and "struct" hacks. If VRP can determine that the
6530 array subscript is a constant, check if it is outside valid
6531 range. If the array subscript is a RANGE, warn if it is
6532 non-overlapping with valid range.
6533 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6536 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
6538 value_range
*vr
= NULL
;
6539 tree low_sub
, up_sub
;
6540 tree low_bound
, up_bound
, up_bound_p1
;
6542 if (TREE_NO_WARNING (ref
))
6545 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
6546 up_bound
= array_ref_up_bound (ref
);
6548 /* Can not check flexible arrays. */
6550 || TREE_CODE (up_bound
) != INTEGER_CST
)
6553 /* Accesses to trailing arrays via pointers may access storage
6554 beyond the types array bounds. */
6555 if (warn_array_bounds
< 2
6556 && array_at_struct_end_p (ref
))
6559 low_bound
= array_ref_low_bound (ref
);
6560 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
6561 build_int_cst (TREE_TYPE (up_bound
), 1));
6564 if (tree_int_cst_equal (low_bound
, up_bound_p1
))
6566 warning_at (location
, OPT_Warray_bounds
,
6567 "array subscript is above array bounds");
6568 TREE_NO_WARNING (ref
) = 1;
6571 if (TREE_CODE (low_sub
) == SSA_NAME
)
6573 vr
= get_value_range (low_sub
);
6574 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
6576 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
6577 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
6581 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
6583 if (TREE_CODE (up_sub
) == INTEGER_CST
6584 && (ignore_off_by_one
6585 ? tree_int_cst_lt (up_bound
, up_sub
)
6586 : tree_int_cst_le (up_bound
, up_sub
))
6587 && TREE_CODE (low_sub
) == INTEGER_CST
6588 && tree_int_cst_le (low_sub
, low_bound
))
6590 warning_at (location
, OPT_Warray_bounds
,
6591 "array subscript is outside array bounds");
6592 TREE_NO_WARNING (ref
) = 1;
6595 else if (TREE_CODE (up_sub
) == INTEGER_CST
6596 && (ignore_off_by_one
6597 ? !tree_int_cst_le (up_sub
, up_bound_p1
)
6598 : !tree_int_cst_le (up_sub
, up_bound
)))
6600 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6602 fprintf (dump_file
, "Array bound warning for ");
6603 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6604 fprintf (dump_file
, "\n");
6606 warning_at (location
, OPT_Warray_bounds
,
6607 "array subscript is above array bounds");
6608 TREE_NO_WARNING (ref
) = 1;
6610 else if (TREE_CODE (low_sub
) == INTEGER_CST
6611 && tree_int_cst_lt (low_sub
, low_bound
))
6613 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6615 fprintf (dump_file
, "Array bound warning for ");
6616 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6617 fprintf (dump_file
, "\n");
6619 warning_at (location
, OPT_Warray_bounds
,
6620 "array subscript is below array bounds");
6621 TREE_NO_WARNING (ref
) = 1;
6625 /* Searches if the expr T, located at LOCATION computes
6626 address of an ARRAY_REF, and call check_array_ref on it. */
6629 search_for_addr_array (tree t
, location_t location
)
6631 /* Check each ARRAY_REFs in the reference chain. */
6634 if (TREE_CODE (t
) == ARRAY_REF
)
6635 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
6637 t
= TREE_OPERAND (t
, 0);
6639 while (handled_component_p (t
));
6641 if (TREE_CODE (t
) == MEM_REF
6642 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
6643 && !TREE_NO_WARNING (t
))
6645 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
6646 tree low_bound
, up_bound
, el_sz
;
6648 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
6649 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6650 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6653 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6654 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6655 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6657 || TREE_CODE (low_bound
) != INTEGER_CST
6659 || TREE_CODE (up_bound
) != INTEGER_CST
6661 || TREE_CODE (el_sz
) != INTEGER_CST
)
6664 idx
= mem_ref_offset (t
);
6665 idx
= wi::sdiv_trunc (idx
, wi::to_offset (el_sz
));
6668 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6670 fprintf (dump_file
, "Array bound warning for ");
6671 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6672 fprintf (dump_file
, "\n");
6674 warning_at (location
, OPT_Warray_bounds
,
6675 "array subscript is below array bounds");
6676 TREE_NO_WARNING (t
) = 1;
6678 else if (idx
> (wi::to_offset (up_bound
)
6679 - wi::to_offset (low_bound
) + 1))
6681 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6683 fprintf (dump_file
, "Array bound warning for ");
6684 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6685 fprintf (dump_file
, "\n");
6687 warning_at (location
, OPT_Warray_bounds
,
6688 "array subscript is above array bounds");
6689 TREE_NO_WARNING (t
) = 1;
6694 /* walk_tree() callback that checks if *TP is
6695 an ARRAY_REF inside an ADDR_EXPR (in which an array
6696 subscript one outside the valid range is allowed). Call
6697 check_array_ref for each ARRAY_REF found. The location is
6701 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6704 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6705 location_t location
;
6707 if (EXPR_HAS_LOCATION (t
))
6708 location
= EXPR_LOCATION (t
);
6711 location_t
*locp
= (location_t
*) wi
->info
;
6715 *walk_subtree
= TRUE
;
6717 if (TREE_CODE (t
) == ARRAY_REF
)
6718 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6720 else if (TREE_CODE (t
) == ADDR_EXPR
)
6722 search_for_addr_array (t
, location
);
6723 *walk_subtree
= FALSE
;
6729 /* Walk over all statements of all reachable BBs and call check_array_bounds
6733 check_all_array_refs (void)
6736 gimple_stmt_iterator si
;
6738 FOR_EACH_BB_FN (bb
, cfun
)
6742 bool executable
= false;
6744 /* Skip blocks that were found to be unreachable. */
6745 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6746 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6750 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6752 gimple
*stmt
= gsi_stmt (si
);
6753 struct walk_stmt_info wi
;
6754 if (!gimple_has_location (stmt
)
6755 || is_gimple_debug (stmt
))
6758 memset (&wi
, 0, sizeof (wi
));
6760 location_t loc
= gimple_location (stmt
);
6763 walk_gimple_op (gsi_stmt (si
),
6770 /* Return true if all imm uses of VAR are either in STMT, or
6771 feed (optionally through a chain of single imm uses) GIMPLE_COND
6772 in basic block COND_BB. */
6775 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple
*stmt
, basic_block cond_bb
)
6777 use_operand_p use_p
, use2_p
;
6778 imm_use_iterator iter
;
6780 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
6781 if (USE_STMT (use_p
) != stmt
)
6783 gimple
*use_stmt
= USE_STMT (use_p
), *use_stmt2
;
6784 if (is_gimple_debug (use_stmt
))
6786 while (is_gimple_assign (use_stmt
)
6787 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
6788 && single_imm_use (gimple_assign_lhs (use_stmt
),
6789 &use2_p
, &use_stmt2
))
6790 use_stmt
= use_stmt2
;
6791 if (gimple_code (use_stmt
) != GIMPLE_COND
6792 || gimple_bb (use_stmt
) != cond_bb
)
6805 __builtin_unreachable ();
6807 x_5 = ASSERT_EXPR <x_3, ...>;
6808 If x_3 has no other immediate uses (checked by caller),
6809 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6810 from the non-zero bitmask. */
6813 maybe_set_nonzero_bits (basic_block bb
, tree var
)
6815 edge e
= single_pred_edge (bb
);
6816 basic_block cond_bb
= e
->src
;
6817 gimple
*stmt
= last_stmt (cond_bb
);
6821 || gimple_code (stmt
) != GIMPLE_COND
6822 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
6823 ? EQ_EXPR
: NE_EXPR
)
6824 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
6825 || !integer_zerop (gimple_cond_rhs (stmt
)))
6828 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
6829 if (!is_gimple_assign (stmt
)
6830 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6831 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
6833 if (gimple_assign_rhs1 (stmt
) != var
)
6837 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
6839 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
6840 if (!gimple_assign_cast_p (stmt2
)
6841 || gimple_assign_rhs1 (stmt2
) != var
6842 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
6843 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
6844 != TYPE_PRECISION (TREE_TYPE (var
))))
6847 cst
= gimple_assign_rhs2 (stmt
);
6848 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
), cst
));
6851 /* Convert range assertion expressions into the implied copies and
6852 copy propagate away the copies. Doing the trivial copy propagation
6853 here avoids the need to run the full copy propagation pass after
6856 FIXME, this will eventually lead to copy propagation removing the
6857 names that had useful range information attached to them. For
6858 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6859 then N_i will have the range [3, +INF].
6861 However, by converting the assertion into the implied copy
6862 operation N_i = N_j, we will then copy-propagate N_j into the uses
6863 of N_i and lose the range information. We may want to hold on to
6864 ASSERT_EXPRs a little while longer as the ranges could be used in
6865 things like jump threading.
6867 The problem with keeping ASSERT_EXPRs around is that passes after
6868 VRP need to handle them appropriately.
6870 Another approach would be to make the range information a first
6871 class property of the SSA_NAME so that it can be queried from
6872 any pass. This is made somewhat more complex by the need for
6873 multiple ranges to be associated with one SSA_NAME. */
6876 remove_range_assertions (void)
6879 gimple_stmt_iterator si
;
6880 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6881 a basic block preceeded by GIMPLE_COND branching to it and
6882 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6885 /* Note that the BSI iterator bump happens at the bottom of the
6886 loop and no bump is necessary if we're removing the statement
6887 referenced by the current BSI. */
6888 FOR_EACH_BB_FN (bb
, cfun
)
6889 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
6891 gimple
*stmt
= gsi_stmt (si
);
6893 if (is_gimple_assign (stmt
)
6894 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6896 tree lhs
= gimple_assign_lhs (stmt
);
6897 tree rhs
= gimple_assign_rhs1 (stmt
);
6900 var
= ASSERT_EXPR_VAR (rhs
);
6902 if (TREE_CODE (var
) == SSA_NAME
6903 && !POINTER_TYPE_P (TREE_TYPE (lhs
))
6904 && SSA_NAME_RANGE_INFO (lhs
))
6906 if (is_unreachable
== -1)
6909 if (single_pred_p (bb
)
6910 && assert_unreachable_fallthru_edge_p
6911 (single_pred_edge (bb
)))
6915 if (x_7 >= 10 && x_7 < 20)
6916 __builtin_unreachable ();
6917 x_8 = ASSERT_EXPR <x_7, ...>;
6918 if the only uses of x_7 are in the ASSERT_EXPR and
6919 in the condition. In that case, we can copy the
6920 range info from x_8 computed in this pass also
6923 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
6926 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
6927 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
6928 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
6929 maybe_set_nonzero_bits (bb
, var
);
6933 /* Propagate the RHS into every use of the LHS. For SSA names
6934 also propagate abnormals as it merely restores the original
6935 IL in this case (an replace_uses_by would assert). */
6936 if (TREE_CODE (var
) == SSA_NAME
)
6938 imm_use_iterator iter
;
6939 use_operand_p use_p
;
6941 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
6942 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
6943 SET_USE (use_p
, var
);
6946 replace_uses_by (lhs
, var
);
6948 /* And finally, remove the copy, it is not needed. */
6949 gsi_remove (&si
, true);
6950 release_defs (stmt
);
6954 if (!is_gimple_debug (gsi_stmt (si
)))
6962 /* Return true if STMT is interesting for VRP. */
6965 stmt_interesting_for_vrp (gimple
*stmt
)
6967 if (gimple_code (stmt
) == GIMPLE_PHI
)
6969 tree res
= gimple_phi_result (stmt
);
6970 return (!virtual_operand_p (res
)
6971 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
6972 || POINTER_TYPE_P (TREE_TYPE (res
))));
6974 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6976 tree lhs
= gimple_get_lhs (stmt
);
6978 /* In general, assignments with virtual operands are not useful
6979 for deriving ranges, with the obvious exception of calls to
6980 builtin functions. */
6981 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6982 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6983 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6984 && (is_gimple_call (stmt
)
6985 || !gimple_vuse (stmt
)))
6987 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
6988 switch (gimple_call_internal_fn (stmt
))
6990 case IFN_ADD_OVERFLOW
:
6991 case IFN_SUB_OVERFLOW
:
6992 case IFN_MUL_OVERFLOW
:
6993 case IFN_ATOMIC_COMPARE_EXCHANGE
:
6994 /* These internal calls return _Complex integer type,
6995 but are interesting to VRP nevertheless. */
6996 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
7003 else if (gimple_code (stmt
) == GIMPLE_COND
7004 || gimple_code (stmt
) == GIMPLE_SWITCH
)
7010 /* Initialize VRP lattice. */
7013 vrp_initialize_lattice ()
7015 values_propagated
= false;
7016 num_vr_values
= num_ssa_names
;
7017 vr_value
= XCNEWVEC (value_range
*, num_vr_values
);
7018 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
7019 bitmap_obstack_initialize (&vrp_equiv_obstack
);
7022 /* Initialization required by ssa_propagate engine. */
7029 FOR_EACH_BB_FN (bb
, cfun
)
7031 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
7034 gphi
*phi
= si
.phi ();
7035 if (!stmt_interesting_for_vrp (phi
))
7037 tree lhs
= PHI_RESULT (phi
);
7038 set_value_range_to_varying (get_value_range (lhs
));
7039 prop_set_simulate_again (phi
, false);
7042 prop_set_simulate_again (phi
, true);
7045 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
7048 gimple
*stmt
= gsi_stmt (si
);
7050 /* If the statement is a control insn, then we do not
7051 want to avoid simulating the statement once. Failure
7052 to do so means that those edges will never get added. */
7053 if (stmt_ends_bb_p (stmt
))
7054 prop_set_simulate_again (stmt
, true);
7055 else if (!stmt_interesting_for_vrp (stmt
))
7057 set_defs_to_varying (stmt
);
7058 prop_set_simulate_again (stmt
, false);
7061 prop_set_simulate_again (stmt
, true);
7066 /* Return the singleton value-range for NAME or NAME. */
7069 vrp_valueize (tree name
)
7071 if (TREE_CODE (name
) == SSA_NAME
)
7073 value_range
*vr
= get_value_range (name
);
7074 if (vr
->type
== VR_RANGE
7075 && (TREE_CODE (vr
->min
) == SSA_NAME
7076 || is_gimple_min_invariant (vr
->min
))
7077 && vrp_operand_equal_p (vr
->min
, vr
->max
))
7083 /* Return the singleton value-range for NAME if that is a constant
7084 but signal to not follow SSA edges. */
7087 vrp_valueize_1 (tree name
)
7089 if (TREE_CODE (name
) == SSA_NAME
)
7091 /* If the definition may be simulated again we cannot follow
7092 this SSA edge as the SSA propagator does not necessarily
7093 re-visit the use. */
7094 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
7095 if (!gimple_nop_p (def_stmt
)
7096 && prop_simulate_again_p (def_stmt
))
7098 value_range
*vr
= get_value_range (name
);
7099 if (range_int_cst_singleton_p (vr
))
7105 /* Visit assignment STMT. If it produces an interesting range, record
7106 the range in VR and set LHS to OUTPUT_P. */
7109 vrp_visit_assignment_or_call (gimple
*stmt
, tree
*output_p
, value_range
*vr
)
7112 enum gimple_code code
= gimple_code (stmt
);
7113 lhs
= gimple_get_lhs (stmt
);
7114 *output_p
= NULL_TREE
;
7116 /* We only keep track of ranges in integral and pointer types. */
7117 if (TREE_CODE (lhs
) == SSA_NAME
7118 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
7119 /* It is valid to have NULL MIN/MAX values on a type. See
7120 build_range_type. */
7121 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
7122 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
7123 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
7127 /* Try folding the statement to a constant first. */
7128 tree tem
= gimple_fold_stmt_to_constant_1 (stmt
, vrp_valueize
,
7132 if (TREE_CODE (tem
) == SSA_NAME
7133 && (SSA_NAME_IS_DEFAULT_DEF (tem
)
7134 || ! prop_simulate_again_p (SSA_NAME_DEF_STMT (tem
))))
7136 extract_range_from_ssa_name (vr
, tem
);
7139 else if (is_gimple_min_invariant (tem
))
7141 set_value_range_to_value (vr
, tem
, NULL
);
7145 /* Then dispatch to value-range extracting functions. */
7146 if (code
== GIMPLE_CALL
)
7147 extract_range_basic (vr
, stmt
);
7149 extract_range_from_assignment (vr
, as_a
<gassign
*> (stmt
));
7153 /* Helper that gets the value range of the SSA_NAME with version I
7154 or a symbolic range containing the SSA_NAME only if the value range
7155 is varying or undefined. */
7157 static inline value_range
7158 get_vr_for_comparison (int i
)
7160 value_range vr
= *get_value_range (ssa_name (i
));
7162 /* If name N_i does not have a valid range, use N_i as its own
7163 range. This allows us to compare against names that may
7164 have N_i in their ranges. */
7165 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
7168 vr
.min
= ssa_name (i
);
7169 vr
.max
= ssa_name (i
);
7175 /* Compare all the value ranges for names equivalent to VAR with VAL
7176 using comparison code COMP. Return the same value returned by
7177 compare_range_with_value, including the setting of
7178 *STRICT_OVERFLOW_P. */
7181 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
7182 bool *strict_overflow_p
, bool use_equiv_p
)
7188 int used_strict_overflow
;
7190 value_range equiv_vr
;
7192 /* Get the set of equivalences for VAR. */
7193 e
= get_value_range (var
)->equiv
;
7195 /* Start at -1. Set it to 0 if we do a comparison without relying
7196 on overflow, or 1 if all comparisons rely on overflow. */
7197 used_strict_overflow
= -1;
7199 /* Compare vars' value range with val. */
7200 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
7202 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7204 used_strict_overflow
= sop
? 1 : 0;
7206 /* If the equiv set is empty we have done all work we need to do. */
7210 && used_strict_overflow
> 0)
7211 *strict_overflow_p
= true;
7215 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
7217 tree name
= ssa_name (i
);
7222 && ! SSA_NAME_IS_DEFAULT_DEF (name
)
7223 && prop_simulate_again_p (SSA_NAME_DEF_STMT (name
)))
7226 equiv_vr
= get_vr_for_comparison (i
);
7228 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7231 /* If we get different answers from different members
7232 of the equivalence set this check must be in a dead
7233 code region. Folding it to a trap representation
7234 would be correct here. For now just return don't-know. */
7244 used_strict_overflow
= 0;
7245 else if (used_strict_overflow
< 0)
7246 used_strict_overflow
= 1;
7251 && used_strict_overflow
> 0)
7252 *strict_overflow_p
= true;
7258 /* Given a comparison code COMP and names N1 and N2, compare all the
7259 ranges equivalent to N1 against all the ranges equivalent to N2
7260 to determine the value of N1 COMP N2. Return the same value
7261 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7262 whether we relied on an overflow infinity in the comparison. */
7266 compare_names (enum tree_code comp
, tree n1
, tree n2
,
7267 bool *strict_overflow_p
)
7271 bitmap_iterator bi1
, bi2
;
7273 int used_strict_overflow
;
7274 static bitmap_obstack
*s_obstack
= NULL
;
7275 static bitmap s_e1
= NULL
, s_e2
= NULL
;
7277 /* Compare the ranges of every name equivalent to N1 against the
7278 ranges of every name equivalent to N2. */
7279 e1
= get_value_range (n1
)->equiv
;
7280 e2
= get_value_range (n2
)->equiv
;
7282 /* Use the fake bitmaps if e1 or e2 are not available. */
7283 if (s_obstack
== NULL
)
7285 s_obstack
= XNEW (bitmap_obstack
);
7286 bitmap_obstack_initialize (s_obstack
);
7287 s_e1
= BITMAP_ALLOC (s_obstack
);
7288 s_e2
= BITMAP_ALLOC (s_obstack
);
7295 /* Add N1 and N2 to their own set of equivalences to avoid
7296 duplicating the body of the loop just to check N1 and N2
7298 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
7299 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
7301 /* If the equivalence sets have a common intersection, then the two
7302 names can be compared without checking their ranges. */
7303 if (bitmap_intersect_p (e1
, e2
))
7305 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7306 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7308 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
7310 : boolean_false_node
;
7313 /* Start at -1. Set it to 0 if we do a comparison without relying
7314 on overflow, or 1 if all comparisons rely on overflow. */
7315 used_strict_overflow
= -1;
7317 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7318 N2 to their own set of equivalences to avoid duplicating the body
7319 of the loop just to check N1 and N2 ranges. */
7320 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
7322 if (! ssa_name (i1
))
7325 value_range vr1
= get_vr_for_comparison (i1
);
7327 t
= retval
= NULL_TREE
;
7328 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
7330 if (! ssa_name (i2
))
7335 value_range vr2
= get_vr_for_comparison (i2
);
7337 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
7340 /* If we get different answers from different members
7341 of the equivalence set this check must be in a dead
7342 code region. Folding it to a trap representation
7343 would be correct here. For now just return don't-know. */
7347 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7348 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7354 used_strict_overflow
= 0;
7355 else if (used_strict_overflow
< 0)
7356 used_strict_overflow
= 1;
7362 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7363 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7364 if (used_strict_overflow
> 0)
7365 *strict_overflow_p
= true;
7370 /* None of the equivalent ranges are useful in computing this
7372 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7373 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7377 /* Helper function for vrp_evaluate_conditional_warnv & other
7381 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
7383 bool * strict_overflow_p
)
7385 value_range
*vr0
, *vr1
;
7387 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
7388 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
7390 tree res
= NULL_TREE
;
7392 res
= compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
7394 res
= compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
7396 res
= (compare_range_with_value
7397 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
7401 /* Helper function for vrp_evaluate_conditional_warnv. */
7404 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
7405 tree op1
, bool use_equiv_p
,
7406 bool *strict_overflow_p
, bool *only_ranges
)
7410 *only_ranges
= true;
7412 /* We only deal with integral and pointer types. */
7413 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7414 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
7417 /* If OP0 CODE OP1 is an overflow comparison, if it can be expressed
7418 as a simple equality test, then prefer that over its current form
7421 An overflow test which collapses to an equality test can always be
7422 expressed as a comparison of one argument against zero. Overflow
7423 occurs when the chosen argument is zero and does not occur if the
7424 chosen argument is not zero. */
7426 if (overflow_comparison_p (code
, op0
, op1
, use_equiv_p
, &x
))
7428 wide_int max
= wi::max_value (TYPE_PRECISION (TREE_TYPE (op0
)), UNSIGNED
);
7429 /* B = A - 1; if (A < B) -> B = A - 1; if (A == 0)
7430 B = A - 1; if (A > B) -> B = A - 1; if (A != 0)
7431 B = A + 1; if (B < A) -> B = A + 1; if (B == 0)
7432 B = A + 1; if (B > A) -> B = A + 1; if (B != 0) */
7433 if (integer_zerop (x
))
7436 code
= (code
== LT_EXPR
|| code
== LE_EXPR
) ? EQ_EXPR
: NE_EXPR
;
7438 /* B = A + 1; if (A > B) -> B = A + 1; if (B == 0)
7439 B = A + 1; if (A < B) -> B = A + 1; if (B != 0)
7440 B = A - 1; if (B > A) -> B = A - 1; if (A == 0)
7441 B = A - 1; if (B < A) -> B = A - 1; if (A != 0) */
7442 else if (wi::eq_p (x
, max
- 1))
7445 op1
= wide_int_to_tree (TREE_TYPE (op0
), 0);
7446 code
= (code
== GT_EXPR
|| code
== GE_EXPR
) ? EQ_EXPR
: NE_EXPR
;
7450 if ((ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
7451 (code
, op0
, op1
, strict_overflow_p
)))
7454 *only_ranges
= false;
7455 /* Do not use compare_names during propagation, it's quadratic. */
7456 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
7458 return compare_names (code
, op0
, op1
, strict_overflow_p
);
7459 else if (TREE_CODE (op0
) == SSA_NAME
)
7460 return compare_name_with_value (code
, op0
, op1
,
7461 strict_overflow_p
, use_equiv_p
);
7462 else if (TREE_CODE (op1
) == SSA_NAME
)
7463 return compare_name_with_value (swap_tree_comparison (code
), op1
, op0
,
7464 strict_overflow_p
, use_equiv_p
);
7468 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7469 information. Return NULL if the conditional can not be evaluated.
7470 The ranges of all the names equivalent with the operands in COND
7471 will be used when trying to compute the value. If the result is
7472 based on undefined signed overflow, issue a warning if
7476 vrp_evaluate_conditional (tree_code code
, tree op0
, tree op1
, gimple
*stmt
)
7482 /* Some passes and foldings leak constants with overflow flag set
7483 into the IL. Avoid doing wrong things with these and bail out. */
7484 if ((TREE_CODE (op0
) == INTEGER_CST
7485 && TREE_OVERFLOW (op0
))
7486 || (TREE_CODE (op1
) == INTEGER_CST
7487 && TREE_OVERFLOW (op1
)))
7491 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
7496 enum warn_strict_overflow_code wc
;
7497 const char* warnmsg
;
7499 if (is_gimple_min_invariant (ret
))
7501 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
7502 warnmsg
= G_("assuming signed overflow does not occur when "
7503 "simplifying conditional to constant");
7507 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
7508 warnmsg
= G_("assuming signed overflow does not occur when "
7509 "simplifying conditional");
7512 if (issue_strict_overflow_warning (wc
))
7514 location_t location
;
7516 if (!gimple_has_location (stmt
))
7517 location
= input_location
;
7519 location
= gimple_location (stmt
);
7520 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
7524 if (warn_type_limits
7525 && ret
&& only_ranges
7526 && TREE_CODE_CLASS (code
) == tcc_comparison
7527 && TREE_CODE (op0
) == SSA_NAME
)
7529 /* If the comparison is being folded and the operand on the LHS
7530 is being compared against a constant value that is outside of
7531 the natural range of OP0's type, then the predicate will
7532 always fold regardless of the value of OP0. If -Wtype-limits
7533 was specified, emit a warning. */
7534 tree type
= TREE_TYPE (op0
);
7535 value_range
*vr0
= get_value_range (op0
);
7537 if (vr0
->type
== VR_RANGE
7538 && INTEGRAL_TYPE_P (type
)
7539 && vrp_val_is_min (vr0
->min
)
7540 && vrp_val_is_max (vr0
->max
)
7541 && is_gimple_min_invariant (op1
))
7543 location_t location
;
7545 if (!gimple_has_location (stmt
))
7546 location
= input_location
;
7548 location
= gimple_location (stmt
);
7550 warning_at (location
, OPT_Wtype_limits
,
7552 ? G_("comparison always false "
7553 "due to limited range of data type")
7554 : G_("comparison always true "
7555 "due to limited range of data type"));
7563 /* Visit conditional statement STMT. If we can determine which edge
7564 will be taken out of STMT's basic block, record it in
7565 *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */
7568 vrp_visit_cond_stmt (gcond
*stmt
, edge
*taken_edge_p
)
7573 *taken_edge_p
= NULL
;
7575 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7580 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
7581 print_gimple_stmt (dump_file
, stmt
, 0, 0);
7582 fprintf (dump_file
, "\nWith known ranges\n");
7584 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
7586 fprintf (dump_file
, "\t");
7587 print_generic_expr (dump_file
, use
, 0);
7588 fprintf (dump_file
, ": ");
7589 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
7592 fprintf (dump_file
, "\n");
7595 /* Compute the value of the predicate COND by checking the known
7596 ranges of each of its operands.
7598 Note that we cannot evaluate all the equivalent ranges here
7599 because those ranges may not yet be final and with the current
7600 propagation strategy, we cannot determine when the value ranges
7601 of the names in the equivalence set have changed.
7603 For instance, given the following code fragment
7607 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7611 Assume that on the first visit to i_14, i_5 has the temporary
7612 range [8, 8] because the second argument to the PHI function is
7613 not yet executable. We derive the range ~[0, 0] for i_14 and the
7614 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7615 the first time, since i_14 is equivalent to the range [8, 8], we
7616 determine that the predicate is always false.
7618 On the next round of propagation, i_13 is determined to be
7619 VARYING, which causes i_5 to drop down to VARYING. So, another
7620 visit to i_14 is scheduled. In this second visit, we compute the
7621 exact same range and equivalence set for i_14, namely ~[0, 0] and
7622 { i_5 }. But we did not have the previous range for i_5
7623 registered, so vrp_visit_assignment thinks that the range for
7624 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7625 is not visited again, which stops propagation from visiting
7626 statements in the THEN clause of that if().
7628 To properly fix this we would need to keep the previous range
7629 value for the names in the equivalence set. This way we would've
7630 discovered that from one visit to the other i_5 changed from
7631 range [8, 8] to VR_VARYING.
7633 However, fixing this apparent limitation may not be worth the
7634 additional checking. Testing on several code bases (GCC, DLV,
7635 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7636 4 more predicates folded in SPEC. */
7639 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
7640 gimple_cond_lhs (stmt
),
7641 gimple_cond_rhs (stmt
),
7646 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
7649 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7651 "\nIgnoring predicate evaluation because "
7652 "it assumes that signed overflow is undefined");
7657 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7659 fprintf (dump_file
, "\nPredicate evaluates to: ");
7660 if (val
== NULL_TREE
)
7661 fprintf (dump_file
, "DON'T KNOW\n");
7663 print_generic_stmt (dump_file
, val
, 0);
7667 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7668 that includes the value VAL. The search is restricted to the range
7669 [START_IDX, n - 1] where n is the size of VEC.
7671 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7674 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7675 it is placed in IDX and false is returned.
7677 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7681 find_case_label_index (gswitch
*stmt
, size_t start_idx
, tree val
, size_t *idx
)
7683 size_t n
= gimple_switch_num_labels (stmt
);
7686 /* Find case label for minimum of the value range or the next one.
7687 At each iteration we are searching in [low, high - 1]. */
7689 for (low
= start_idx
, high
= n
; high
!= low
; )
7693 /* Note that i != high, so we never ask for n. */
7694 size_t i
= (high
+ low
) / 2;
7695 t
= gimple_switch_label (stmt
, i
);
7697 /* Cache the result of comparing CASE_LOW and val. */
7698 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
7702 /* Ranges cannot be empty. */
7711 if (CASE_HIGH (t
) != NULL
7712 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
7724 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7725 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7726 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7727 then MAX_IDX < MIN_IDX.
7728 Returns true if the default label is not needed. */
7731 find_case_label_range (gswitch
*stmt
, tree min
, tree max
, size_t *min_idx
,
7735 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
7736 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
7740 && max_take_default
)
7742 /* Only the default case label reached.
7743 Return an empty range. */
7750 bool take_default
= min_take_default
|| max_take_default
;
7754 if (max_take_default
)
7757 /* If the case label range is continuous, we do not need
7758 the default case label. Verify that. */
7759 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
7760 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
7761 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
7762 for (k
= i
+ 1; k
<= j
; ++k
)
7764 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
7765 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
7767 take_default
= true;
7771 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
7772 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
7777 return !take_default
;
7781 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7782 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7783 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7784 Returns true if the default label is not needed. */
7787 find_case_label_ranges (gswitch
*stmt
, value_range
*vr
, size_t *min_idx1
,
7788 size_t *max_idx1
, size_t *min_idx2
,
7792 unsigned int n
= gimple_switch_num_labels (stmt
);
7794 tree case_low
, case_high
;
7795 tree min
= vr
->min
, max
= vr
->max
;
7797 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
7799 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
7801 /* Set second range to emtpy. */
7805 if (vr
->type
== VR_RANGE
)
7809 return !take_default
;
7812 /* Set first range to all case labels. */
7819 /* Make sure all the values of case labels [i , j] are contained in
7820 range [MIN, MAX]. */
7821 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
7822 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
7823 if (tree_int_cst_compare (case_low
, min
) < 0)
7825 if (case_high
!= NULL_TREE
7826 && tree_int_cst_compare (max
, case_high
) < 0)
7832 /* If the range spans case labels [i, j], the corresponding anti-range spans
7833 the labels [1, i - 1] and [j + 1, n - 1]. */
7859 /* Visit switch statement STMT. If we can determine which edge
7860 will be taken out of STMT's basic block, record it in
7861 *TAKEN_EDGE_P. Otherwise, *TAKEN_EDGE_P set to NULL. */
7864 vrp_visit_switch_stmt (gswitch
*stmt
, edge
*taken_edge_p
)
7868 size_t i
= 0, j
= 0, k
, l
;
7871 *taken_edge_p
= NULL
;
7872 op
= gimple_switch_index (stmt
);
7873 if (TREE_CODE (op
) != SSA_NAME
)
7876 vr
= get_value_range (op
);
7877 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7879 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7880 print_generic_expr (dump_file
, op
, 0);
7881 fprintf (dump_file
, " with known range ");
7882 dump_value_range (dump_file
, vr
);
7883 fprintf (dump_file
, "\n");
7886 if ((vr
->type
!= VR_RANGE
7887 && vr
->type
!= VR_ANTI_RANGE
)
7888 || symbolic_range_p (vr
))
7891 /* Find the single edge that is taken from the switch expression. */
7892 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7894 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7898 gcc_assert (take_default
);
7899 val
= gimple_switch_default_label (stmt
);
7903 /* Check if labels with index i to j and maybe the default label
7904 are all reaching the same label. */
7906 val
= gimple_switch_label (stmt
, i
);
7908 && CASE_LABEL (gimple_switch_default_label (stmt
))
7909 != CASE_LABEL (val
))
7911 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7912 fprintf (dump_file
, " not a single destination for this "
7916 for (++i
; i
<= j
; ++i
)
7918 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
7920 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7921 fprintf (dump_file
, " not a single destination for this "
7928 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
7930 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7931 fprintf (dump_file
, " not a single destination for this "
7938 *taken_edge_p
= find_edge (gimple_bb (stmt
),
7939 label_to_block (CASE_LABEL (val
)));
7941 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7943 fprintf (dump_file
, " will take edge to ");
7944 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
7949 /* Evaluate statement STMT. If the statement produces a useful range,
7950 set VR and corepsponding OUTPUT_P.
7952 If STMT is a conditional branch and we can determine its truth
7953 value, the taken edge is recorded in *TAKEN_EDGE_P. */
7956 extract_range_from_stmt (gimple
*stmt
, edge
*taken_edge_p
,
7957 tree
*output_p
, value_range
*vr
)
7960 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7962 fprintf (dump_file
, "\nVisiting statement:\n");
7963 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
7966 if (!stmt_interesting_for_vrp (stmt
))
7967 gcc_assert (stmt_ends_bb_p (stmt
));
7968 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7969 vrp_visit_assignment_or_call (stmt
, output_p
, vr
);
7970 else if (gimple_code (stmt
) == GIMPLE_COND
)
7971 vrp_visit_cond_stmt (as_a
<gcond
*> (stmt
), taken_edge_p
);
7972 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
7973 vrp_visit_switch_stmt (as_a
<gswitch
*> (stmt
), taken_edge_p
);
7976 /* Evaluate statement STMT. If the statement produces a useful range,
7977 return SSA_PROP_INTERESTING and record the SSA name with the
7978 interesting range into *OUTPUT_P.
7980 If STMT is a conditional branch and we can determine its truth
7981 value, the taken edge is recorded in *TAKEN_EDGE_P.
7983 If STMT produces a varying value, return SSA_PROP_VARYING. */
7985 static enum ssa_prop_result
7986 vrp_visit_stmt (gimple
*stmt
, edge
*taken_edge_p
, tree
*output_p
)
7988 value_range vr
= VR_INITIALIZER
;
7989 tree lhs
= gimple_get_lhs (stmt
);
7990 extract_range_from_stmt (stmt
, taken_edge_p
, output_p
, &vr
);
7994 if (update_value_range (*output_p
, &vr
))
7996 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7998 fprintf (dump_file
, "Found new range for ");
7999 print_generic_expr (dump_file
, *output_p
, 0);
8000 fprintf (dump_file
, ": ");
8001 dump_value_range (dump_file
, &vr
);
8002 fprintf (dump_file
, "\n");
8005 if (vr
.type
== VR_VARYING
)
8006 return SSA_PROP_VARYING
;
8008 return SSA_PROP_INTERESTING
;
8010 return SSA_PROP_NOT_INTERESTING
;
8013 if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
8014 switch (gimple_call_internal_fn (stmt
))
8016 case IFN_ADD_OVERFLOW
:
8017 case IFN_SUB_OVERFLOW
:
8018 case IFN_MUL_OVERFLOW
:
8019 case IFN_ATOMIC_COMPARE_EXCHANGE
:
8020 /* These internal calls return _Complex integer type,
8021 which VRP does not track, but the immediate uses
8022 thereof might be interesting. */
8023 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
8025 imm_use_iterator iter
;
8026 use_operand_p use_p
;
8027 enum ssa_prop_result res
= SSA_PROP_VARYING
;
8029 set_value_range_to_varying (get_value_range (lhs
));
8031 FOR_EACH_IMM_USE_FAST (use_p
, iter
, lhs
)
8033 gimple
*use_stmt
= USE_STMT (use_p
);
8034 if (!is_gimple_assign (use_stmt
))
8036 enum tree_code rhs_code
= gimple_assign_rhs_code (use_stmt
);
8037 if (rhs_code
!= REALPART_EXPR
&& rhs_code
!= IMAGPART_EXPR
)
8039 tree rhs1
= gimple_assign_rhs1 (use_stmt
);
8040 tree use_lhs
= gimple_assign_lhs (use_stmt
);
8041 if (TREE_CODE (rhs1
) != rhs_code
8042 || TREE_OPERAND (rhs1
, 0) != lhs
8043 || TREE_CODE (use_lhs
) != SSA_NAME
8044 || !stmt_interesting_for_vrp (use_stmt
)
8045 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs
))
8046 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs
))
8047 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs
))))
8050 /* If there is a change in the value range for any of the
8051 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
8052 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
8053 or IMAGPART_EXPR immediate uses, but none of them have
8054 a change in their value ranges, return
8055 SSA_PROP_NOT_INTERESTING. If there are no
8056 {REAL,IMAG}PART_EXPR uses at all,
8057 return SSA_PROP_VARYING. */
8058 value_range new_vr
= VR_INITIALIZER
;
8059 extract_range_basic (&new_vr
, use_stmt
);
8060 value_range
*old_vr
= get_value_range (use_lhs
);
8061 if (old_vr
->type
!= new_vr
.type
8062 || !vrp_operand_equal_p (old_vr
->min
, new_vr
.min
)
8063 || !vrp_operand_equal_p (old_vr
->max
, new_vr
.max
)
8064 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
.equiv
))
8065 res
= SSA_PROP_INTERESTING
;
8067 res
= SSA_PROP_NOT_INTERESTING
;
8068 BITMAP_FREE (new_vr
.equiv
);
8069 if (res
== SSA_PROP_INTERESTING
)
8083 /* All other statements produce nothing of interest for VRP, so mark
8084 their outputs varying and prevent further simulation. */
8085 set_defs_to_varying (stmt
);
8087 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
8090 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8091 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8092 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8093 possible such range. The resulting range is not canonicalized. */
8096 union_ranges (enum value_range_type
*vr0type
,
8097 tree
*vr0min
, tree
*vr0max
,
8098 enum value_range_type vr1type
,
8099 tree vr1min
, tree vr1max
)
8101 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
8102 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
8104 /* [] is vr0, () is vr1 in the following classification comments. */
8108 if (*vr0type
== vr1type
)
8109 /* Nothing to do for equal ranges. */
8111 else if ((*vr0type
== VR_RANGE
8112 && vr1type
== VR_ANTI_RANGE
)
8113 || (*vr0type
== VR_ANTI_RANGE
8114 && vr1type
== VR_RANGE
))
8116 /* For anti-range with range union the result is varying. */
8122 else if (operand_less_p (*vr0max
, vr1min
) == 1
8123 || operand_less_p (vr1max
, *vr0min
) == 1)
8125 /* [ ] ( ) or ( ) [ ]
8126 If the ranges have an empty intersection, result of the union
8127 operation is the anti-range or if both are anti-ranges
8129 if (*vr0type
== VR_ANTI_RANGE
8130 && vr1type
== VR_ANTI_RANGE
)
8132 else if (*vr0type
== VR_ANTI_RANGE
8133 && vr1type
== VR_RANGE
)
8135 else if (*vr0type
== VR_RANGE
8136 && vr1type
== VR_ANTI_RANGE
)
8142 else if (*vr0type
== VR_RANGE
8143 && vr1type
== VR_RANGE
)
8145 /* The result is the convex hull of both ranges. */
8146 if (operand_less_p (*vr0max
, vr1min
) == 1)
8148 /* If the result can be an anti-range, create one. */
8149 if (TREE_CODE (*vr0max
) == INTEGER_CST
8150 && TREE_CODE (vr1min
) == INTEGER_CST
8151 && vrp_val_is_min (*vr0min
)
8152 && vrp_val_is_max (vr1max
))
8154 tree min
= int_const_binop (PLUS_EXPR
,
8156 build_int_cst (TREE_TYPE (*vr0max
), 1));
8157 tree max
= int_const_binop (MINUS_EXPR
,
8159 build_int_cst (TREE_TYPE (vr1min
), 1));
8160 if (!operand_less_p (max
, min
))
8162 *vr0type
= VR_ANTI_RANGE
;
8174 /* If the result can be an anti-range, create one. */
8175 if (TREE_CODE (vr1max
) == INTEGER_CST
8176 && TREE_CODE (*vr0min
) == INTEGER_CST
8177 && vrp_val_is_min (vr1min
)
8178 && vrp_val_is_max (*vr0max
))
8180 tree min
= int_const_binop (PLUS_EXPR
,
8182 build_int_cst (TREE_TYPE (vr1max
), 1));
8183 tree max
= int_const_binop (MINUS_EXPR
,
8185 build_int_cst (TREE_TYPE (*vr0min
), 1));
8186 if (!operand_less_p (max
, min
))
8188 *vr0type
= VR_ANTI_RANGE
;
8202 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8203 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8205 /* [ ( ) ] or [( ) ] or [ ( )] */
8206 if (*vr0type
== VR_RANGE
8207 && vr1type
== VR_RANGE
)
8209 else if (*vr0type
== VR_ANTI_RANGE
8210 && vr1type
== VR_ANTI_RANGE
)
8216 else if (*vr0type
== VR_ANTI_RANGE
8217 && vr1type
== VR_RANGE
)
8219 /* Arbitrarily choose the right or left gap. */
8220 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
8221 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8222 build_int_cst (TREE_TYPE (vr1min
), 1));
8223 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
8224 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8225 build_int_cst (TREE_TYPE (vr1max
), 1));
8229 else if (*vr0type
== VR_RANGE
8230 && vr1type
== VR_ANTI_RANGE
)
8231 /* The result covers everything. */
8236 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8237 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8239 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8240 if (*vr0type
== VR_RANGE
8241 && vr1type
== VR_RANGE
)
8247 else if (*vr0type
== VR_ANTI_RANGE
8248 && vr1type
== VR_ANTI_RANGE
)
8250 else if (*vr0type
== VR_RANGE
8251 && vr1type
== VR_ANTI_RANGE
)
8253 *vr0type
= VR_ANTI_RANGE
;
8254 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
8256 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8257 build_int_cst (TREE_TYPE (*vr0min
), 1));
8260 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
8262 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8263 build_int_cst (TREE_TYPE (*vr0max
), 1));
8269 else if (*vr0type
== VR_ANTI_RANGE
8270 && vr1type
== VR_RANGE
)
8271 /* The result covers everything. */
8276 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8277 || operand_equal_p (vr1min
, *vr0max
, 0))
8278 && operand_less_p (*vr0min
, vr1min
) == 1
8279 && operand_less_p (*vr0max
, vr1max
) == 1)
8281 /* [ ( ] ) or [ ]( ) */
8282 if (*vr0type
== VR_RANGE
8283 && vr1type
== VR_RANGE
)
8285 else if (*vr0type
== VR_ANTI_RANGE
8286 && vr1type
== VR_ANTI_RANGE
)
8288 else if (*vr0type
== VR_ANTI_RANGE
8289 && vr1type
== VR_RANGE
)
8291 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8292 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8293 build_int_cst (TREE_TYPE (vr1min
), 1));
8297 else if (*vr0type
== VR_RANGE
8298 && vr1type
== VR_ANTI_RANGE
)
8300 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8303 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8304 build_int_cst (TREE_TYPE (*vr0max
), 1));
8313 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8314 || operand_equal_p (*vr0min
, vr1max
, 0))
8315 && operand_less_p (vr1min
, *vr0min
) == 1
8316 && operand_less_p (vr1max
, *vr0max
) == 1)
8318 /* ( [ ) ] or ( )[ ] */
8319 if (*vr0type
== VR_RANGE
8320 && vr1type
== VR_RANGE
)
8322 else if (*vr0type
== VR_ANTI_RANGE
8323 && vr1type
== VR_ANTI_RANGE
)
8325 else if (*vr0type
== VR_ANTI_RANGE
8326 && vr1type
== VR_RANGE
)
8328 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8329 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8330 build_int_cst (TREE_TYPE (vr1max
), 1));
8334 else if (*vr0type
== VR_RANGE
8335 && vr1type
== VR_ANTI_RANGE
)
8337 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8341 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8342 build_int_cst (TREE_TYPE (*vr0min
), 1));
8356 *vr0type
= VR_VARYING
;
8357 *vr0min
= NULL_TREE
;
8358 *vr0max
= NULL_TREE
;
8361 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8362 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8363 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8364 possible such range. The resulting range is not canonicalized. */
8367 intersect_ranges (enum value_range_type
*vr0type
,
8368 tree
*vr0min
, tree
*vr0max
,
8369 enum value_range_type vr1type
,
8370 tree vr1min
, tree vr1max
)
8372 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
8373 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
8375 /* [] is vr0, () is vr1 in the following classification comments. */
8379 if (*vr0type
== vr1type
)
8380 /* Nothing to do for equal ranges. */
8382 else if ((*vr0type
== VR_RANGE
8383 && vr1type
== VR_ANTI_RANGE
)
8384 || (*vr0type
== VR_ANTI_RANGE
8385 && vr1type
== VR_RANGE
))
8387 /* For anti-range with range intersection the result is empty. */
8388 *vr0type
= VR_UNDEFINED
;
8389 *vr0min
= NULL_TREE
;
8390 *vr0max
= NULL_TREE
;
8395 else if (operand_less_p (*vr0max
, vr1min
) == 1
8396 || operand_less_p (vr1max
, *vr0min
) == 1)
8398 /* [ ] ( ) or ( ) [ ]
8399 If the ranges have an empty intersection, the result of the
8400 intersect operation is the range for intersecting an
8401 anti-range with a range or empty when intersecting two ranges. */
8402 if (*vr0type
== VR_RANGE
8403 && vr1type
== VR_ANTI_RANGE
)
8405 else if (*vr0type
== VR_ANTI_RANGE
8406 && vr1type
== VR_RANGE
)
8412 else if (*vr0type
== VR_RANGE
8413 && vr1type
== VR_RANGE
)
8415 *vr0type
= VR_UNDEFINED
;
8416 *vr0min
= NULL_TREE
;
8417 *vr0max
= NULL_TREE
;
8419 else if (*vr0type
== VR_ANTI_RANGE
8420 && vr1type
== VR_ANTI_RANGE
)
8422 /* If the anti-ranges are adjacent to each other merge them. */
8423 if (TREE_CODE (*vr0max
) == INTEGER_CST
8424 && TREE_CODE (vr1min
) == INTEGER_CST
8425 && operand_less_p (*vr0max
, vr1min
) == 1
8426 && integer_onep (int_const_binop (MINUS_EXPR
,
8429 else if (TREE_CODE (vr1max
) == INTEGER_CST
8430 && TREE_CODE (*vr0min
) == INTEGER_CST
8431 && operand_less_p (vr1max
, *vr0min
) == 1
8432 && integer_onep (int_const_binop (MINUS_EXPR
,
8435 /* Else arbitrarily take VR0. */
8438 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8439 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8441 /* [ ( ) ] or [( ) ] or [ ( )] */
8442 if (*vr0type
== VR_RANGE
8443 && vr1type
== VR_RANGE
)
8445 /* If both are ranges the result is the inner one. */
8450 else if (*vr0type
== VR_RANGE
8451 && vr1type
== VR_ANTI_RANGE
)
8453 /* Choose the right gap if the left one is empty. */
8456 if (TREE_CODE (vr1max
) != INTEGER_CST
)
8458 else if (TYPE_PRECISION (TREE_TYPE (vr1max
)) == 1
8459 && !TYPE_UNSIGNED (TREE_TYPE (vr1max
)))
8461 = int_const_binop (MINUS_EXPR
, vr1max
,
8462 build_int_cst (TREE_TYPE (vr1max
), -1));
8465 = int_const_binop (PLUS_EXPR
, vr1max
,
8466 build_int_cst (TREE_TYPE (vr1max
), 1));
8468 /* Choose the left gap if the right one is empty. */
8471 if (TREE_CODE (vr1min
) != INTEGER_CST
)
8473 else if (TYPE_PRECISION (TREE_TYPE (vr1min
)) == 1
8474 && !TYPE_UNSIGNED (TREE_TYPE (vr1min
)))
8476 = int_const_binop (PLUS_EXPR
, vr1min
,
8477 build_int_cst (TREE_TYPE (vr1min
), -1));
8480 = int_const_binop (MINUS_EXPR
, vr1min
,
8481 build_int_cst (TREE_TYPE (vr1min
), 1));
8483 /* Choose the anti-range if the range is effectively varying. */
8484 else if (vrp_val_is_min (*vr0min
)
8485 && vrp_val_is_max (*vr0max
))
8491 /* Else choose the range. */
8493 else if (*vr0type
== VR_ANTI_RANGE
8494 && vr1type
== VR_ANTI_RANGE
)
8495 /* If both are anti-ranges the result is the outer one. */
8497 else if (*vr0type
== VR_ANTI_RANGE
8498 && vr1type
== VR_RANGE
)
8500 /* The intersection is empty. */
8501 *vr0type
= VR_UNDEFINED
;
8502 *vr0min
= NULL_TREE
;
8503 *vr0max
= NULL_TREE
;
8508 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8509 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8511 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8512 if (*vr0type
== VR_RANGE
8513 && vr1type
== VR_RANGE
)
8514 /* Choose the inner range. */
8516 else if (*vr0type
== VR_ANTI_RANGE
8517 && vr1type
== VR_RANGE
)
8519 /* Choose the right gap if the left is empty. */
8522 *vr0type
= VR_RANGE
;
8523 if (TREE_CODE (*vr0max
) != INTEGER_CST
)
8525 else if (TYPE_PRECISION (TREE_TYPE (*vr0max
)) == 1
8526 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max
)))
8528 = int_const_binop (MINUS_EXPR
, *vr0max
,
8529 build_int_cst (TREE_TYPE (*vr0max
), -1));
8532 = int_const_binop (PLUS_EXPR
, *vr0max
,
8533 build_int_cst (TREE_TYPE (*vr0max
), 1));
8536 /* Choose the left gap if the right is empty. */
8539 *vr0type
= VR_RANGE
;
8540 if (TREE_CODE (*vr0min
) != INTEGER_CST
)
8542 else if (TYPE_PRECISION (TREE_TYPE (*vr0min
)) == 1
8543 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min
)))
8545 = int_const_binop (PLUS_EXPR
, *vr0min
,
8546 build_int_cst (TREE_TYPE (*vr0min
), -1));
8549 = int_const_binop (MINUS_EXPR
, *vr0min
,
8550 build_int_cst (TREE_TYPE (*vr0min
), 1));
8553 /* Choose the anti-range if the range is effectively varying. */
8554 else if (vrp_val_is_min (vr1min
)
8555 && vrp_val_is_max (vr1max
))
8557 /* Choose the anti-range if it is ~[0,0], that range is special
8558 enough to special case when vr1's range is relatively wide. */
8559 else if (*vr0min
== *vr0max
8560 && integer_zerop (*vr0min
)
8561 && (TYPE_PRECISION (TREE_TYPE (*vr0min
))
8562 == TYPE_PRECISION (ptr_type_node
))
8563 && TREE_CODE (vr1max
) == INTEGER_CST
8564 && TREE_CODE (vr1min
) == INTEGER_CST
8565 && (wi::clz (wi::sub (vr1max
, vr1min
))
8566 < TYPE_PRECISION (TREE_TYPE (*vr0min
)) / 2))
8568 /* Else choose the range. */
8576 else if (*vr0type
== VR_ANTI_RANGE
8577 && vr1type
== VR_ANTI_RANGE
)
8579 /* If both are anti-ranges the result is the outer one. */
8584 else if (vr1type
== VR_ANTI_RANGE
8585 && *vr0type
== VR_RANGE
)
8587 /* The intersection is empty. */
8588 *vr0type
= VR_UNDEFINED
;
8589 *vr0min
= NULL_TREE
;
8590 *vr0max
= NULL_TREE
;
8595 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8596 || operand_equal_p (vr1min
, *vr0max
, 0))
8597 && operand_less_p (*vr0min
, vr1min
) == 1)
8599 /* [ ( ] ) or [ ]( ) */
8600 if (*vr0type
== VR_ANTI_RANGE
8601 && vr1type
== VR_ANTI_RANGE
)
8603 else if (*vr0type
== VR_RANGE
8604 && vr1type
== VR_RANGE
)
8606 else if (*vr0type
== VR_RANGE
8607 && vr1type
== VR_ANTI_RANGE
)
8609 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8610 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8611 build_int_cst (TREE_TYPE (vr1min
), 1));
8615 else if (*vr0type
== VR_ANTI_RANGE
8616 && vr1type
== VR_RANGE
)
8618 *vr0type
= VR_RANGE
;
8619 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8620 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8621 build_int_cst (TREE_TYPE (*vr0max
), 1));
8629 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8630 || operand_equal_p (*vr0min
, vr1max
, 0))
8631 && operand_less_p (vr1min
, *vr0min
) == 1)
8633 /* ( [ ) ] or ( )[ ] */
8634 if (*vr0type
== VR_ANTI_RANGE
8635 && vr1type
== VR_ANTI_RANGE
)
8637 else if (*vr0type
== VR_RANGE
8638 && vr1type
== VR_RANGE
)
8640 else if (*vr0type
== VR_RANGE
8641 && vr1type
== VR_ANTI_RANGE
)
8643 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8644 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8645 build_int_cst (TREE_TYPE (vr1max
), 1));
8649 else if (*vr0type
== VR_ANTI_RANGE
8650 && vr1type
== VR_RANGE
)
8652 *vr0type
= VR_RANGE
;
8653 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8654 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8655 build_int_cst (TREE_TYPE (*vr0min
), 1));
8664 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8665 result for the intersection. That's always a conservative
8666 correct estimate unless VR1 is a constant singleton range
8667 in which case we choose that. */
8668 if (vr1type
== VR_RANGE
8669 && is_gimple_min_invariant (vr1min
)
8670 && vrp_operand_equal_p (vr1min
, vr1max
))
8681 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8682 in *VR0. This may not be the smallest possible such range. */
8685 vrp_intersect_ranges_1 (value_range
*vr0
, value_range
*vr1
)
8689 /* If either range is VR_VARYING the other one wins. */
8690 if (vr1
->type
== VR_VARYING
)
8692 if (vr0
->type
== VR_VARYING
)
8694 copy_value_range (vr0
, vr1
);
8698 /* When either range is VR_UNDEFINED the resulting range is
8699 VR_UNDEFINED, too. */
8700 if (vr0
->type
== VR_UNDEFINED
)
8702 if (vr1
->type
== VR_UNDEFINED
)
8704 set_value_range_to_undefined (vr0
);
8708 /* Save the original vr0 so we can return it as conservative intersection
8709 result when our worker turns things to varying. */
8711 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8712 vr1
->type
, vr1
->min
, vr1
->max
);
8713 /* Make sure to canonicalize the result though as the inversion of a
8714 VR_RANGE can still be a VR_RANGE. */
8715 set_and_canonicalize_value_range (vr0
, vr0
->type
,
8716 vr0
->min
, vr0
->max
, vr0
->equiv
);
8717 /* If that failed, use the saved original VR0. */
8718 if (vr0
->type
== VR_VARYING
)
8723 /* If the result is VR_UNDEFINED there is no need to mess with
8724 the equivalencies. */
8725 if (vr0
->type
== VR_UNDEFINED
)
8728 /* The resulting set of equivalences for range intersection is the union of
8730 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8731 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
8732 else if (vr1
->equiv
&& !vr0
->equiv
)
8734 vr0
->equiv
= BITMAP_ALLOC (&vrp_equiv_obstack
);
8735 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
8740 vrp_intersect_ranges (value_range
*vr0
, value_range
*vr1
)
8742 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8744 fprintf (dump_file
, "Intersecting\n ");
8745 dump_value_range (dump_file
, vr0
);
8746 fprintf (dump_file
, "\nand\n ");
8747 dump_value_range (dump_file
, vr1
);
8748 fprintf (dump_file
, "\n");
8750 vrp_intersect_ranges_1 (vr0
, vr1
);
8751 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8753 fprintf (dump_file
, "to\n ");
8754 dump_value_range (dump_file
, vr0
);
8755 fprintf (dump_file
, "\n");
8759 /* Meet operation for value ranges. Given two value ranges VR0 and
8760 VR1, store in VR0 a range that contains both VR0 and VR1. This
8761 may not be the smallest possible such range. */
8764 vrp_meet_1 (value_range
*vr0
, const value_range
*vr1
)
8768 if (vr0
->type
== VR_UNDEFINED
)
8770 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
8774 if (vr1
->type
== VR_UNDEFINED
)
8776 /* VR0 already has the resulting range. */
8780 if (vr0
->type
== VR_VARYING
)
8782 /* Nothing to do. VR0 already has the resulting range. */
8786 if (vr1
->type
== VR_VARYING
)
8788 set_value_range_to_varying (vr0
);
8793 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8794 vr1
->type
, vr1
->min
, vr1
->max
);
8795 if (vr0
->type
== VR_VARYING
)
8797 /* Failed to find an efficient meet. Before giving up and setting
8798 the result to VARYING, see if we can at least derive a useful
8799 anti-range. FIXME, all this nonsense about distinguishing
8800 anti-ranges from ranges is necessary because of the odd
8801 semantics of range_includes_zero_p and friends. */
8802 if (((saved
.type
== VR_RANGE
8803 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
8804 || (saved
.type
== VR_ANTI_RANGE
8805 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
8806 && ((vr1
->type
== VR_RANGE
8807 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
8808 || (vr1
->type
== VR_ANTI_RANGE
8809 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
8811 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
8813 /* Since this meet operation did not result from the meeting of
8814 two equivalent names, VR0 cannot have any equivalences. */
8816 bitmap_clear (vr0
->equiv
);
8820 set_value_range_to_varying (vr0
);
8823 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
8825 if (vr0
->type
== VR_VARYING
)
8828 /* The resulting set of equivalences is always the intersection of
8830 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8831 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
8832 else if (vr0
->equiv
&& !vr1
->equiv
)
8833 bitmap_clear (vr0
->equiv
);
8837 vrp_meet (value_range
*vr0
, const value_range
*vr1
)
8839 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8841 fprintf (dump_file
, "Meeting\n ");
8842 dump_value_range (dump_file
, vr0
);
8843 fprintf (dump_file
, "\nand\n ");
8844 dump_value_range (dump_file
, vr1
);
8845 fprintf (dump_file
, "\n");
8847 vrp_meet_1 (vr0
, vr1
);
8848 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8850 fprintf (dump_file
, "to\n ");
8851 dump_value_range (dump_file
, vr0
);
8852 fprintf (dump_file
, "\n");
8857 /* Visit all arguments for PHI node PHI that flow through executable
8858 edges. If a valid value range can be derived from all the incoming
8859 value ranges, set a new range in VR_RESULT. */
8862 extract_range_from_phi_node (gphi
*phi
, value_range
*vr_result
)
8865 tree lhs
= PHI_RESULT (phi
);
8866 value_range
*lhs_vr
= get_value_range (lhs
);
8868 int edges
, old_edges
;
8871 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8873 fprintf (dump_file
, "\nVisiting PHI node: ");
8874 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
8877 bool may_simulate_backedge_again
= false;
8879 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
8881 edge e
= gimple_phi_arg_edge (phi
, i
);
8883 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8886 " Argument #%d (%d -> %d %sexecutable)\n",
8887 (int) i
, e
->src
->index
, e
->dest
->index
,
8888 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
8891 if (e
->flags
& EDGE_EXECUTABLE
)
8893 tree arg
= PHI_ARG_DEF (phi
, i
);
8898 if (TREE_CODE (arg
) == SSA_NAME
)
8900 /* See if we are eventually going to change one of the args. */
8901 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
8902 if (! gimple_nop_p (def_stmt
)
8903 && prop_simulate_again_p (def_stmt
)
8904 && e
->flags
& EDGE_DFS_BACK
)
8905 may_simulate_backedge_again
= true;
8907 vr_arg
= *(get_value_range (arg
));
8908 /* Do not allow equivalences or symbolic ranges to leak in from
8909 backedges. That creates invalid equivalencies.
8910 See PR53465 and PR54767. */
8911 if (e
->flags
& EDGE_DFS_BACK
)
8913 if (vr_arg
.type
== VR_RANGE
8914 || vr_arg
.type
== VR_ANTI_RANGE
)
8916 vr_arg
.equiv
= NULL
;
8917 if (symbolic_range_p (&vr_arg
))
8919 vr_arg
.type
= VR_VARYING
;
8920 vr_arg
.min
= NULL_TREE
;
8921 vr_arg
.max
= NULL_TREE
;
8927 /* If the non-backedge arguments range is VR_VARYING then
8928 we can still try recording a simple equivalence. */
8929 if (vr_arg
.type
== VR_VARYING
)
8931 vr_arg
.type
= VR_RANGE
;
8934 vr_arg
.equiv
= NULL
;
8940 if (TREE_OVERFLOW_P (arg
))
8941 arg
= drop_tree_overflow (arg
);
8943 vr_arg
.type
= VR_RANGE
;
8946 vr_arg
.equiv
= NULL
;
8949 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8951 fprintf (dump_file
, "\t");
8952 print_generic_expr (dump_file
, arg
, dump_flags
);
8953 fprintf (dump_file
, ": ");
8954 dump_value_range (dump_file
, &vr_arg
);
8955 fprintf (dump_file
, "\n");
8959 copy_value_range (vr_result
, &vr_arg
);
8961 vrp_meet (vr_result
, &vr_arg
);
8964 if (vr_result
->type
== VR_VARYING
)
8969 if (vr_result
->type
== VR_VARYING
)
8971 else if (vr_result
->type
== VR_UNDEFINED
)
8974 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
8975 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
8977 /* To prevent infinite iterations in the algorithm, derive ranges
8978 when the new value is slightly bigger or smaller than the
8979 previous one. We don't do this if we have seen a new executable
8980 edge; this helps us avoid an overflow infinity for conditionals
8981 which are not in a loop. If the old value-range was VR_UNDEFINED
8982 use the updated range and iterate one more time. If we will not
8983 simulate this PHI again via the backedge allow us to iterate. */
8985 && gimple_phi_num_args (phi
) > 1
8986 && edges
== old_edges
8987 && lhs_vr
->type
!= VR_UNDEFINED
8988 && may_simulate_backedge_again
)
8990 /* Compare old and new ranges, fall back to varying if the
8991 values are not comparable. */
8992 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
->min
);
8995 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
->max
);
8999 /* For non VR_RANGE or for pointers fall back to varying if
9000 the range changed. */
9001 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
->type
!= VR_RANGE
9002 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
9003 && (cmp_min
!= 0 || cmp_max
!= 0))
9006 /* If the new minimum is larger than the previous one
9007 retain the old value. If the new minimum value is smaller
9008 than the previous one and not -INF go all the way to -INF + 1.
9009 In the first case, to avoid infinite bouncing between different
9010 minimums, and in the other case to avoid iterating millions of
9011 times to reach -INF. Going to -INF + 1 also lets the following
9012 iteration compute whether there will be any overflow, at the
9013 expense of one additional iteration. */
9015 vr_result
->min
= lhs_vr
->min
;
9016 else if (cmp_min
> 0
9017 && !vrp_val_is_min (vr_result
->min
))
9019 = int_const_binop (PLUS_EXPR
,
9020 vrp_val_min (TREE_TYPE (vr_result
->min
)),
9021 build_int_cst (TREE_TYPE (vr_result
->min
), 1));
9023 /* Similarly for the maximum value. */
9025 vr_result
->max
= lhs_vr
->max
;
9026 else if (cmp_max
< 0
9027 && !vrp_val_is_max (vr_result
->max
))
9029 = int_const_binop (MINUS_EXPR
,
9030 vrp_val_max (TREE_TYPE (vr_result
->min
)),
9031 build_int_cst (TREE_TYPE (vr_result
->min
), 1));
9033 /* If we dropped either bound to +-INF then if this is a loop
9034 PHI node SCEV may known more about its value-range. */
9035 if (cmp_min
> 0 || cmp_min
< 0
9036 || cmp_max
< 0 || cmp_max
> 0)
9039 goto infinite_check
;
9045 set_value_range_to_varying (vr_result
);
9048 /* If this is a loop PHI node SCEV may known more about its value-range.
9049 scev_check can be reached from two paths, one is a fall through from above
9050 "varying" label, the other is direct goto from code block which tries to
9051 avoid infinite simulation. */
9052 if ((l
= loop_containing_stmt (phi
))
9053 && l
->header
== gimple_bb (phi
))
9054 adjust_range_with_scev (vr_result
, l
, phi
, lhs
);
9057 /* If we will end up with a (-INF, +INF) range, set it to
9058 VARYING. Same if the previous max value was invalid for
9059 the type and we end up with vr_result.min > vr_result.max. */
9060 if ((vr_result
->type
== VR_RANGE
|| vr_result
->type
== VR_ANTI_RANGE
)
9061 && !((vrp_val_is_max (vr_result
->max
) && vrp_val_is_min (vr_result
->min
))
9062 || compare_values (vr_result
->min
, vr_result
->max
) > 0))
9065 set_value_range_to_varying (vr_result
);
9067 /* If the new range is different than the previous value, keep
9073 /* Visit all arguments for PHI node PHI that flow through executable
9074 edges. If a valid value range can be derived from all the incoming
9075 value ranges, set a new range for the LHS of PHI. */
9077 static enum ssa_prop_result
9078 vrp_visit_phi_node (gphi
*phi
)
9080 tree lhs
= PHI_RESULT (phi
);
9081 value_range vr_result
= VR_INITIALIZER
;
9082 extract_range_from_phi_node (phi
, &vr_result
);
9083 if (update_value_range (lhs
, &vr_result
))
9085 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9087 fprintf (dump_file
, "Found new range for ");
9088 print_generic_expr (dump_file
, lhs
, 0);
9089 fprintf (dump_file
, ": ");
9090 dump_value_range (dump_file
, &vr_result
);
9091 fprintf (dump_file
, "\n");
9094 if (vr_result
.type
== VR_VARYING
)
9095 return SSA_PROP_VARYING
;
9097 return SSA_PROP_INTERESTING
;
9100 /* Nothing changed, don't add outgoing edges. */
9101 return SSA_PROP_NOT_INTERESTING
;
9104 /* Simplify boolean operations if the source is known
9105 to be already a boolean. */
9107 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9109 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9111 bool need_conversion
;
9113 /* We handle only !=/== case here. */
9114 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
9116 op0
= gimple_assign_rhs1 (stmt
);
9117 if (!op_with_boolean_value_range_p (op0
))
9120 op1
= gimple_assign_rhs2 (stmt
);
9121 if (!op_with_boolean_value_range_p (op1
))
9124 /* Reduce number of cases to handle to NE_EXPR. As there is no
9125 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
9126 if (rhs_code
== EQ_EXPR
)
9128 if (TREE_CODE (op1
) == INTEGER_CST
)
9129 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
9130 build_int_cst (TREE_TYPE (op1
), 1));
9135 lhs
= gimple_assign_lhs (stmt
);
9137 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
9139 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
9141 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
9142 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
9143 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
9146 /* For A != 0 we can substitute A itself. */
9147 if (integer_zerop (op1
))
9148 gimple_assign_set_rhs_with_ops (gsi
,
9150 ? NOP_EXPR
: TREE_CODE (op0
), op0
);
9151 /* For A != B we substitute A ^ B. Either with conversion. */
9152 else if (need_conversion
)
9154 tree tem
= make_ssa_name (TREE_TYPE (op0
));
9156 = gimple_build_assign (tem
, BIT_XOR_EXPR
, op0
, op1
);
9157 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
9158 if (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
9159 && TYPE_PRECISION (TREE_TYPE (tem
)) > 1)
9160 set_range_info (tem
, VR_RANGE
,
9161 wi::zero (TYPE_PRECISION (TREE_TYPE (tem
))),
9162 wi::one (TYPE_PRECISION (TREE_TYPE (tem
))));
9163 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
);
9167 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
9168 update_stmt (gsi_stmt (*gsi
));
9169 fold_stmt (gsi
, follow_single_use_edges
);
9174 /* Simplify a division or modulo operator to a right shift or bitwise and
9175 if the first operand is unsigned or is greater than zero and the second
9176 operand is an exact power of two. For TRUNC_MOD_EXPR op0 % op1 with
9177 constant op1 (op1min = op1) or with op1 in [op1min, op1max] range,
9178 optimize it into just op0 if op0's range is known to be a subset of
9179 [-op1min + 1, op1min - 1] for signed and [0, op1min - 1] for unsigned
9183 simplify_div_or_mod_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9185 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9187 tree op0
= gimple_assign_rhs1 (stmt
);
9188 tree op1
= gimple_assign_rhs2 (stmt
);
9189 tree op0min
= NULL_TREE
, op0max
= NULL_TREE
;
9191 value_range
*vr
= NULL
;
9193 if (TREE_CODE (op0
) == INTEGER_CST
)
9200 vr
= get_value_range (op0
);
9201 if (range_int_cst_p (vr
))
9208 if (rhs_code
== TRUNC_MOD_EXPR
9209 && TREE_CODE (op1
) == SSA_NAME
)
9211 value_range
*vr1
= get_value_range (op1
);
9212 if (range_int_cst_p (vr1
))
9215 if (rhs_code
== TRUNC_MOD_EXPR
9216 && TREE_CODE (op1min
) == INTEGER_CST
9217 && tree_int_cst_sgn (op1min
) == 1
9219 && tree_int_cst_lt (op0max
, op1min
))
9221 if (TYPE_UNSIGNED (TREE_TYPE (op0
))
9222 || tree_int_cst_sgn (op0min
) >= 0
9223 || tree_int_cst_lt (fold_unary (NEGATE_EXPR
, TREE_TYPE (op1min
), op1min
),
9226 /* If op0 already has the range op0 % op1 has,
9227 then TRUNC_MOD_EXPR won't change anything. */
9228 gimple_assign_set_rhs_from_tree (gsi
, op0
);
9233 if (TREE_CODE (op0
) != SSA_NAME
)
9236 if (!integer_pow2p (op1
))
9238 /* X % -Y can be only optimized into X % Y either if
9239 X is not INT_MIN, or Y is not -1. Fold it now, as after
9240 remove_range_assertions the range info might be not available
9242 if (rhs_code
== TRUNC_MOD_EXPR
9243 && fold_stmt (gsi
, follow_single_use_edges
))
9248 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
9249 val
= integer_one_node
;
9254 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
9258 && integer_onep (val
)
9259 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9261 location_t location
;
9263 if (!gimple_has_location (stmt
))
9264 location
= input_location
;
9266 location
= gimple_location (stmt
);
9267 warning_at (location
, OPT_Wstrict_overflow
,
9268 "assuming signed overflow does not occur when "
9269 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9273 if (val
&& integer_onep (val
))
9277 if (rhs_code
== TRUNC_DIV_EXPR
)
9279 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
9280 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
9281 gimple_assign_set_rhs1 (stmt
, op0
);
9282 gimple_assign_set_rhs2 (stmt
, t
);
9286 t
= build_int_cst (TREE_TYPE (op1
), 1);
9287 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
9288 t
= fold_convert (TREE_TYPE (op0
), t
);
9290 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
9291 gimple_assign_set_rhs1 (stmt
, op0
);
9292 gimple_assign_set_rhs2 (stmt
, t
);
9296 fold_stmt (gsi
, follow_single_use_edges
);
9303 /* Simplify a min or max if the ranges of the two operands are
9304 disjoint. Return true if we do simplify. */
9307 simplify_min_or_max_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9309 tree op0
= gimple_assign_rhs1 (stmt
);
9310 tree op1
= gimple_assign_rhs2 (stmt
);
9314 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9315 (LE_EXPR
, op0
, op1
, &sop
));
9319 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9320 (LT_EXPR
, op0
, op1
, &sop
));
9325 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9327 location_t location
;
9329 if (!gimple_has_location (stmt
))
9330 location
= input_location
;
9332 location
= gimple_location (stmt
);
9333 warning_at (location
, OPT_Wstrict_overflow
,
9334 "assuming signed overflow does not occur when "
9335 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9338 /* VAL == TRUE -> OP0 < or <= op1
9339 VAL == FALSE -> OP0 > or >= op1. */
9340 tree res
= ((gimple_assign_rhs_code (stmt
) == MAX_EXPR
)
9341 == integer_zerop (val
)) ? op0
: op1
;
9342 gimple_assign_set_rhs_from_tree (gsi
, res
);
9349 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9350 ABS_EXPR. If the operand is <= 0, then simplify the
9351 ABS_EXPR into a NEGATE_EXPR. */
9354 simplify_abs_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9356 tree op
= gimple_assign_rhs1 (stmt
);
9357 value_range
*vr
= get_value_range (op
);
9364 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
9367 /* The range is neither <= 0 nor > 0. Now see if it is
9368 either < 0 or >= 0. */
9370 val
= compare_range_with_value (LT_EXPR
, vr
, integer_zero_node
,
9376 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9378 location_t location
;
9380 if (!gimple_has_location (stmt
))
9381 location
= input_location
;
9383 location
= gimple_location (stmt
);
9384 warning_at (location
, OPT_Wstrict_overflow
,
9385 "assuming signed overflow does not occur when "
9386 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9389 gimple_assign_set_rhs1 (stmt
, op
);
9390 if (integer_zerop (val
))
9391 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
9393 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
9395 fold_stmt (gsi
, follow_single_use_edges
);
9403 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9404 If all the bits that are being cleared by & are already
9405 known to be zero from VR, or all the bits that are being
9406 set by | are already known to be one from VR, the bit
9407 operation is redundant. */
9410 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9412 tree op0
= gimple_assign_rhs1 (stmt
);
9413 tree op1
= gimple_assign_rhs2 (stmt
);
9414 tree op
= NULL_TREE
;
9415 value_range vr0
= VR_INITIALIZER
;
9416 value_range vr1
= VR_INITIALIZER
;
9417 wide_int may_be_nonzero0
, may_be_nonzero1
;
9418 wide_int must_be_nonzero0
, must_be_nonzero1
;
9421 if (TREE_CODE (op0
) == SSA_NAME
)
9422 vr0
= *(get_value_range (op0
));
9423 else if (is_gimple_min_invariant (op0
))
9424 set_value_range_to_value (&vr0
, op0
, NULL
);
9428 if (TREE_CODE (op1
) == SSA_NAME
)
9429 vr1
= *(get_value_range (op1
));
9430 else if (is_gimple_min_invariant (op1
))
9431 set_value_range_to_value (&vr1
, op1
, NULL
);
9435 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
,
9438 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
,
9442 switch (gimple_assign_rhs_code (stmt
))
9445 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
9451 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
9459 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
9465 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
9476 if (op
== NULL_TREE
)
9479 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
);
9480 update_stmt (gsi_stmt (*gsi
));
9484 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9485 a known value range VR.
9487 If there is one and only one value which will satisfy the
9488 conditional, then return that value. Else return NULL.
9490 If signed overflow must be undefined for the value to satisfy
9491 the conditional, then set *STRICT_OVERFLOW_P to true. */
9494 test_for_singularity (enum tree_code cond_code
, tree op0
,
9495 tree op1
, value_range
*vr
)
9500 /* Extract minimum/maximum values which satisfy the conditional as it was
9502 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
9504 /* This should not be negative infinity; there is no overflow
9506 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
9509 if (cond_code
== LT_EXPR
)
9511 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9512 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
9514 TREE_NO_WARNING (max
) = 1;
9517 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
9519 /* This should not be positive infinity; there is no overflow
9521 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
9524 if (cond_code
== GT_EXPR
)
9526 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9527 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
9529 TREE_NO_WARNING (min
) = 1;
9533 /* Now refine the minimum and maximum values using any
9534 value range information we have for op0. */
9537 if (compare_values (vr
->min
, min
) == 1)
9539 if (compare_values (vr
->max
, max
) == -1)
9542 /* If the new min/max values have converged to a single value,
9543 then there is only one value which can satisfy the condition,
9544 return that value. */
9545 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
9551 /* Return whether the value range *VR fits in an integer type specified
9552 by PRECISION and UNSIGNED_P. */
9555 range_fits_type_p (value_range
*vr
, unsigned dest_precision
, signop dest_sgn
)
9558 unsigned src_precision
;
9562 /* We can only handle integral and pointer types. */
9563 src_type
= TREE_TYPE (vr
->min
);
9564 if (!INTEGRAL_TYPE_P (src_type
)
9565 && !POINTER_TYPE_P (src_type
))
9568 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9569 and so is an identity transform. */
9570 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
9571 src_sgn
= TYPE_SIGN (src_type
);
9572 if ((src_precision
< dest_precision
9573 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
9574 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
9577 /* Now we can only handle ranges with constant bounds. */
9578 if (vr
->type
!= VR_RANGE
9579 || TREE_CODE (vr
->min
) != INTEGER_CST
9580 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9583 /* For sign changes, the MSB of the wide_int has to be clear.
9584 An unsigned value with its MSB set cannot be represented by
9585 a signed wide_int, while a negative value cannot be represented
9586 by an unsigned wide_int. */
9587 if (src_sgn
!= dest_sgn
9588 && (wi::lts_p (vr
->min
, 0) || wi::lts_p (vr
->max
, 0)))
9591 /* Then we can perform the conversion on both ends and compare
9592 the result for equality. */
9593 tem
= wi::ext (wi::to_widest (vr
->min
), dest_precision
, dest_sgn
);
9594 if (tem
!= wi::to_widest (vr
->min
))
9596 tem
= wi::ext (wi::to_widest (vr
->max
), dest_precision
, dest_sgn
);
9597 if (tem
!= wi::to_widest (vr
->max
))
9603 /* Simplify STMT, an ASSERT_EXPR, using ranges. This is helpful because jump
9604 threading looks at the ASSERT_EXPRs. Collapsing the condition of
9605 an ASSERT_EXPR from a relational to an equality test is where most
9606 of the benefit occurrs, so that's the only thing we currently do. */
9609 simplify_assert_expr_using_ranges (gimple
*stmt
)
9611 tree cond
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 1);
9612 tree_code code
= TREE_CODE (cond
);
9613 tree op0
= TREE_OPERAND (cond
, 0);
9615 /* The condition of the ASSERT_EXPR must be a simple relational
9616 between an SSA_NAME (with a range) and a constant. */
9617 if (TREE_CODE (op0
) != SSA_NAME
9618 || !INTEGRAL_TYPE_P (TREE_TYPE (op0
)))
9621 tree op1
= TREE_OPERAND (cond
, 1);
9622 if (TREE_CODE (op1
) != INTEGER_CST
)
9625 value_range
*vr
= get_value_range (op0
);
9626 if (!vr
|| vr
->type
!= VR_RANGE
)
9629 tree res
= test_for_singularity (code
, op0
, op1
, vr
);
9632 TREE_SET_CODE (cond
, EQ_EXPR
);
9633 TREE_OPERAND (cond
, 1) = res
;
9639 /* Simplify a conditional using a relational operator to an equality
9640 test if the range information indicates only one value can satisfy
9641 the original conditional. */
9644 simplify_cond_using_ranges_1 (gcond
*stmt
)
9646 tree op0
= gimple_cond_lhs (stmt
);
9647 tree op1
= gimple_cond_rhs (stmt
);
9648 enum tree_code cond_code
= gimple_cond_code (stmt
);
9650 if (cond_code
!= NE_EXPR
9651 && cond_code
!= EQ_EXPR
9652 && TREE_CODE (op0
) == SSA_NAME
9653 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9654 && is_gimple_min_invariant (op1
))
9656 value_range
*vr
= get_value_range (op0
);
9658 /* If we have range information for OP0, then we might be
9659 able to simplify this conditional. */
9660 if (vr
->type
== VR_RANGE
)
9662 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
9667 fprintf (dump_file
, "Simplified relational ");
9668 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9669 fprintf (dump_file
, " into ");
9672 gimple_cond_set_code (stmt
, EQ_EXPR
);
9673 gimple_cond_set_lhs (stmt
, op0
);
9674 gimple_cond_set_rhs (stmt
, new_tree
);
9680 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9681 fprintf (dump_file
, "\n");
9687 /* Try again after inverting the condition. We only deal
9688 with integral types here, so no need to worry about
9689 issues with inverting FP comparisons. */
9690 new_tree
= test_for_singularity
9691 (invert_tree_comparison (cond_code
, false),
9697 fprintf (dump_file
, "Simplified relational ");
9698 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9699 fprintf (dump_file
, " into ");
9702 gimple_cond_set_code (stmt
, NE_EXPR
);
9703 gimple_cond_set_lhs (stmt
, op0
);
9704 gimple_cond_set_rhs (stmt
, new_tree
);
9710 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9711 fprintf (dump_file
, "\n");
9721 /* STMT is a conditional at the end of a basic block.
9723 If the conditional is of the form SSA_NAME op constant and the SSA_NAME
9724 was set via a type conversion, try to replace the SSA_NAME with the RHS
9725 of the type conversion. Doing so makes the conversion dead which helps
9726 subsequent passes. */
9729 simplify_cond_using_ranges_2 (gcond
*stmt
)
9731 tree op0
= gimple_cond_lhs (stmt
);
9732 tree op1
= gimple_cond_rhs (stmt
);
9734 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9735 see if OP0 was set by a type conversion where the source of
9736 the conversion is another SSA_NAME with a range that fits
9737 into the range of OP0's type.
9739 If so, the conversion is redundant as the earlier SSA_NAME can be
9740 used for the comparison directly if we just massage the constant in the
9742 if (TREE_CODE (op0
) == SSA_NAME
9743 && TREE_CODE (op1
) == INTEGER_CST
)
9745 gimple
*def_stmt
= SSA_NAME_DEF_STMT (op0
);
9748 if (!is_gimple_assign (def_stmt
)
9749 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9752 innerop
= gimple_assign_rhs1 (def_stmt
);
9754 if (TREE_CODE (innerop
) == SSA_NAME
9755 && !POINTER_TYPE_P (TREE_TYPE (innerop
))
9756 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
)
9757 && desired_pro_or_demotion_p (TREE_TYPE (innerop
), TREE_TYPE (op0
)))
9759 value_range
*vr
= get_value_range (innerop
);
9761 if (range_int_cst_p (vr
)
9762 && range_fits_type_p (vr
,
9763 TYPE_PRECISION (TREE_TYPE (op0
)),
9764 TYPE_SIGN (TREE_TYPE (op0
)))
9765 && int_fits_type_p (op1
, TREE_TYPE (innerop
)))
9767 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
9768 gimple_cond_set_lhs (stmt
, innerop
);
9769 gimple_cond_set_rhs (stmt
, newconst
);
9771 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9773 fprintf (dump_file
, "Folded into: ");
9774 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
9775 fprintf (dump_file
, "\n");
9782 /* Simplify a switch statement using the value range of the switch
9786 simplify_switch_using_ranges (gswitch
*stmt
)
9788 tree op
= gimple_switch_index (stmt
);
9789 value_range
*vr
= NULL
;
9793 size_t i
= 0, j
= 0, n
, n2
;
9796 size_t k
= 1, l
= 0;
9798 if (TREE_CODE (op
) == SSA_NAME
)
9800 vr
= get_value_range (op
);
9802 /* We can only handle integer ranges. */
9803 if ((vr
->type
!= VR_RANGE
9804 && vr
->type
!= VR_ANTI_RANGE
)
9805 || symbolic_range_p (vr
))
9808 /* Find case label for min/max of the value range. */
9809 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
9811 else if (TREE_CODE (op
) == INTEGER_CST
)
9813 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
9827 n
= gimple_switch_num_labels (stmt
);
9829 /* We can truncate the case label ranges that partially overlap with OP's
9831 size_t min_idx
= 1, max_idx
= 0;
9833 find_case_label_range (stmt
, vr
->min
, vr
->max
, &min_idx
, &max_idx
);
9834 if (min_idx
<= max_idx
)
9836 tree min_label
= gimple_switch_label (stmt
, min_idx
);
9837 tree max_label
= gimple_switch_label (stmt
, max_idx
);
9839 /* Avoid changing the type of the case labels when truncating. */
9840 tree case_label_type
= TREE_TYPE (CASE_LOW (min_label
));
9841 tree vr_min
= fold_convert (case_label_type
, vr
->min
);
9842 tree vr_max
= fold_convert (case_label_type
, vr
->max
);
9844 if (vr
->type
== VR_RANGE
)
9846 /* If OP's value range is [2,8] and the low label range is
9847 0 ... 3, truncate the label's range to 2 .. 3. */
9848 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
9849 && CASE_HIGH (min_label
) != NULL_TREE
9850 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_min
) >= 0)
9851 CASE_LOW (min_label
) = vr_min
;
9853 /* If OP's value range is [2,8] and the high label range is
9854 7 ... 10, truncate the label's range to 7 .. 8. */
9855 if (tree_int_cst_compare (CASE_LOW (max_label
), vr_max
) <= 0
9856 && CASE_HIGH (max_label
) != NULL_TREE
9857 && tree_int_cst_compare (CASE_HIGH (max_label
), vr_max
) > 0)
9858 CASE_HIGH (max_label
) = vr_max
;
9860 else if (vr
->type
== VR_ANTI_RANGE
)
9862 tree one_cst
= build_one_cst (case_label_type
);
9864 if (min_label
== max_label
)
9866 /* If OP's value range is ~[7,8] and the label's range is
9867 7 ... 10, truncate the label's range to 9 ... 10. */
9868 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) == 0
9869 && CASE_HIGH (min_label
) != NULL_TREE
9870 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_max
) > 0)
9871 CASE_LOW (min_label
)
9872 = int_const_binop (PLUS_EXPR
, vr_max
, one_cst
);
9874 /* If OP's value range is ~[7,8] and the label's range is
9875 5 ... 8, truncate the label's range to 5 ... 6. */
9876 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
9877 && CASE_HIGH (min_label
) != NULL_TREE
9878 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_max
) == 0)
9879 CASE_HIGH (min_label
)
9880 = int_const_binop (MINUS_EXPR
, vr_min
, one_cst
);
9884 /* If OP's value range is ~[2,8] and the low label range is
9885 0 ... 3, truncate the label's range to 0 ... 1. */
9886 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
9887 && CASE_HIGH (min_label
) != NULL_TREE
9888 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_min
) >= 0)
9889 CASE_HIGH (min_label
)
9890 = int_const_binop (MINUS_EXPR
, vr_min
, one_cst
);
9892 /* If OP's value range is ~[2,8] and the high label range is
9893 7 ... 10, truncate the label's range to 9 ... 10. */
9894 if (tree_int_cst_compare (CASE_LOW (max_label
), vr_max
) <= 0
9895 && CASE_HIGH (max_label
) != NULL_TREE
9896 && tree_int_cst_compare (CASE_HIGH (max_label
), vr_max
) > 0)
9897 CASE_LOW (max_label
)
9898 = int_const_binop (PLUS_EXPR
, vr_max
, one_cst
);
9902 /* Canonicalize singleton case ranges. */
9903 if (tree_int_cst_equal (CASE_LOW (min_label
), CASE_HIGH (min_label
)))
9904 CASE_HIGH (min_label
) = NULL_TREE
;
9905 if (tree_int_cst_equal (CASE_LOW (max_label
), CASE_HIGH (max_label
)))
9906 CASE_HIGH (max_label
) = NULL_TREE
;
9909 /* We can also eliminate case labels that lie completely outside OP's value
9912 /* Bail out if this is just all edges taken. */
9918 /* Build a new vector of taken case labels. */
9919 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
9922 /* Add the default edge, if necessary. */
9924 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
9926 for (; i
<= j
; ++i
, ++n2
)
9927 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
9929 for (; k
<= l
; ++k
, ++n2
)
9930 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
9932 /* Mark needed edges. */
9933 for (i
= 0; i
< n2
; ++i
)
9935 e
= find_edge (gimple_bb (stmt
),
9936 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
9937 e
->aux
= (void *)-1;
9940 /* Queue not needed edges for later removal. */
9941 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
9943 if (e
->aux
== (void *)-1)
9949 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9951 fprintf (dump_file
, "removing unreachable case label\n");
9953 to_remove_edges
.safe_push (e
);
9954 e
->flags
&= ~EDGE_EXECUTABLE
;
9957 /* And queue an update for the stmt. */
9960 to_update_switch_stmts
.safe_push (su
);
9964 /* Simplify an integral conversion from an SSA name in STMT. */
9967 simplify_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9969 tree innerop
, middleop
, finaltype
;
9971 signop inner_sgn
, middle_sgn
, final_sgn
;
9972 unsigned inner_prec
, middle_prec
, final_prec
;
9973 widest_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
9975 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
9976 if (!INTEGRAL_TYPE_P (finaltype
))
9978 middleop
= gimple_assign_rhs1 (stmt
);
9979 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
9980 if (!is_gimple_assign (def_stmt
)
9981 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9983 innerop
= gimple_assign_rhs1 (def_stmt
);
9984 if (TREE_CODE (innerop
) != SSA_NAME
9985 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
9988 /* Get the value-range of the inner operand. Use get_range_info in
9989 case innerop was created during substitute-and-fold. */
9990 wide_int imin
, imax
;
9991 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop
))
9992 || get_range_info (innerop
, &imin
, &imax
) != VR_RANGE
)
9994 innermin
= widest_int::from (imin
, TYPE_SIGN (TREE_TYPE (innerop
)));
9995 innermax
= widest_int::from (imax
, TYPE_SIGN (TREE_TYPE (innerop
)));
9997 /* Simulate the conversion chain to check if the result is equal if
9998 the middle conversion is removed. */
9999 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
10000 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
10001 final_prec
= TYPE_PRECISION (finaltype
);
10003 /* If the first conversion is not injective, the second must not
10005 if (wi::gtu_p (innermax
- innermin
,
10006 wi::mask
<widest_int
> (middle_prec
, false))
10007 && middle_prec
< final_prec
)
10009 /* We also want a medium value so that we can track the effect that
10010 narrowing conversions with sign change have. */
10011 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
10012 if (inner_sgn
== UNSIGNED
)
10013 innermed
= wi::shifted_mask
<widest_int
> (1, inner_prec
- 1, false);
10016 if (wi::cmp (innermin
, innermed
, inner_sgn
) >= 0
10017 || wi::cmp (innermed
, innermax
, inner_sgn
) >= 0)
10018 innermed
= innermin
;
10020 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
10021 middlemin
= wi::ext (innermin
, middle_prec
, middle_sgn
);
10022 middlemed
= wi::ext (innermed
, middle_prec
, middle_sgn
);
10023 middlemax
= wi::ext (innermax
, middle_prec
, middle_sgn
);
10025 /* Require that the final conversion applied to both the original
10026 and the intermediate range produces the same result. */
10027 final_sgn
= TYPE_SIGN (finaltype
);
10028 if (wi::ext (middlemin
, final_prec
, final_sgn
)
10029 != wi::ext (innermin
, final_prec
, final_sgn
)
10030 || wi::ext (middlemed
, final_prec
, final_sgn
)
10031 != wi::ext (innermed
, final_prec
, final_sgn
)
10032 || wi::ext (middlemax
, final_prec
, final_sgn
)
10033 != wi::ext (innermax
, final_prec
, final_sgn
))
10036 gimple_assign_set_rhs1 (stmt
, innerop
);
10037 fold_stmt (gsi
, follow_single_use_edges
);
10041 /* Simplify a conversion from integral SSA name to float in STMT. */
10044 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
,
10047 tree rhs1
= gimple_assign_rhs1 (stmt
);
10048 value_range
*vr
= get_value_range (rhs1
);
10049 machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
10054 /* We can only handle constant ranges. */
10055 if (vr
->type
!= VR_RANGE
10056 || TREE_CODE (vr
->min
) != INTEGER_CST
10057 || TREE_CODE (vr
->max
) != INTEGER_CST
)
10060 /* First check if we can use a signed type in place of an unsigned. */
10061 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
10062 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
10063 != CODE_FOR_nothing
)
10064 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
10065 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
10066 /* If we can do the conversion in the current input mode do nothing. */
10067 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
10068 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
10070 /* Otherwise search for a mode we can use, starting from the narrowest
10071 integer mode available. */
10074 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
10077 /* If we cannot do a signed conversion to float from mode
10078 or if the value-range does not fit in the signed type
10079 try with a wider mode. */
10080 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
10081 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
10084 mode
= GET_MODE_WIDER_MODE (mode
);
10085 /* But do not widen the input. Instead leave that to the
10086 optabs expansion code. */
10087 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
10090 while (mode
!= VOIDmode
);
10091 if (mode
== VOIDmode
)
10095 /* It works, insert a truncation or sign-change before the
10096 float conversion. */
10097 tem
= make_ssa_name (build_nonstandard_integer_type
10098 (GET_MODE_PRECISION (mode
), 0));
10099 conv
= gimple_build_assign (tem
, NOP_EXPR
, rhs1
);
10100 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
10101 gimple_assign_set_rhs1 (stmt
, tem
);
10102 fold_stmt (gsi
, follow_single_use_edges
);
10107 /* Simplify an internal fn call using ranges if possible. */
10110 simplify_internal_call_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
10112 enum tree_code subcode
;
10113 bool is_ubsan
= false;
10115 switch (gimple_call_internal_fn (stmt
))
10117 case IFN_UBSAN_CHECK_ADD
:
10118 subcode
= PLUS_EXPR
;
10121 case IFN_UBSAN_CHECK_SUB
:
10122 subcode
= MINUS_EXPR
;
10125 case IFN_UBSAN_CHECK_MUL
:
10126 subcode
= MULT_EXPR
;
10129 case IFN_ADD_OVERFLOW
:
10130 subcode
= PLUS_EXPR
;
10132 case IFN_SUB_OVERFLOW
:
10133 subcode
= MINUS_EXPR
;
10135 case IFN_MUL_OVERFLOW
:
10136 subcode
= MULT_EXPR
;
10142 tree op0
= gimple_call_arg (stmt
, 0);
10143 tree op1
= gimple_call_arg (stmt
, 1);
10147 type
= TREE_TYPE (op0
);
10148 if (VECTOR_TYPE_P (type
))
10151 else if (gimple_call_lhs (stmt
) == NULL_TREE
)
10154 type
= TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt
)));
10155 if (!check_for_binary_op_overflow (subcode
, type
, op0
, op1
, &ovf
)
10156 || (is_ubsan
&& ovf
))
10160 location_t loc
= gimple_location (stmt
);
10162 g
= gimple_build_assign (gimple_call_lhs (stmt
), subcode
, op0
, op1
);
10165 int prec
= TYPE_PRECISION (type
);
10168 || !useless_type_conversion_p (type
, TREE_TYPE (op0
))
10169 || !useless_type_conversion_p (type
, TREE_TYPE (op1
)))
10170 utype
= build_nonstandard_integer_type (prec
, 1);
10171 if (TREE_CODE (op0
) == INTEGER_CST
)
10172 op0
= fold_convert (utype
, op0
);
10173 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op0
)))
10175 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op0
);
10176 gimple_set_location (g
, loc
);
10177 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
10178 op0
= gimple_assign_lhs (g
);
10180 if (TREE_CODE (op1
) == INTEGER_CST
)
10181 op1
= fold_convert (utype
, op1
);
10182 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op1
)))
10184 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op1
);
10185 gimple_set_location (g
, loc
);
10186 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
10187 op1
= gimple_assign_lhs (g
);
10189 g
= gimple_build_assign (make_ssa_name (utype
), subcode
, op0
, op1
);
10190 gimple_set_location (g
, loc
);
10191 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
10194 g
= gimple_build_assign (make_ssa_name (type
), NOP_EXPR
,
10195 gimple_assign_lhs (g
));
10196 gimple_set_location (g
, loc
);
10197 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
10199 g
= gimple_build_assign (gimple_call_lhs (stmt
), COMPLEX_EXPR
,
10200 gimple_assign_lhs (g
),
10201 build_int_cst (type
, ovf
));
10203 gimple_set_location (g
, loc
);
10204 gsi_replace (gsi
, g
, false);
10208 /* Return true if VAR is a two-valued variable. Set a and b with the
10209 two-values when it is true. Return false otherwise. */
10212 two_valued_val_range_p (tree var
, tree
*a
, tree
*b
)
10214 value_range
*vr
= get_value_range (var
);
10215 if ((vr
->type
!= VR_RANGE
10216 && vr
->type
!= VR_ANTI_RANGE
)
10217 || TREE_CODE (vr
->min
) != INTEGER_CST
10218 || TREE_CODE (vr
->max
) != INTEGER_CST
)
10221 if (vr
->type
== VR_RANGE
10222 && wi::sub (vr
->max
, vr
->min
) == 1)
10229 /* ~[TYPE_MIN + 1, TYPE_MAX - 1] */
10230 if (vr
->type
== VR_ANTI_RANGE
10231 && wi::sub (vr
->min
, vrp_val_min (TREE_TYPE (var
))) == 1
10232 && wi::sub (vrp_val_max (TREE_TYPE (var
)), vr
->max
) == 1)
10234 *a
= vrp_val_min (TREE_TYPE (var
));
10235 *b
= vrp_val_max (TREE_TYPE (var
));
10242 /* Simplify STMT using ranges if possible. */
10245 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
10247 gimple
*stmt
= gsi_stmt (*gsi
);
10248 if (is_gimple_assign (stmt
))
10250 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
10251 tree rhs1
= gimple_assign_rhs1 (stmt
);
10252 tree rhs2
= gimple_assign_rhs2 (stmt
);
10253 tree lhs
= gimple_assign_lhs (stmt
);
10254 tree val1
= NULL_TREE
, val2
= NULL_TREE
;
10255 use_operand_p use_p
;
10259 LHS = CST BINOP VAR
10260 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10262 LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
10265 LHS = VAR BINOP CST
10266 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10268 LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
10270 if (TREE_CODE_CLASS (rhs_code
) == tcc_binary
10271 && INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
10272 && ((TREE_CODE (rhs1
) == INTEGER_CST
10273 && TREE_CODE (rhs2
) == SSA_NAME
)
10274 || (TREE_CODE (rhs2
) == INTEGER_CST
10275 && TREE_CODE (rhs1
) == SSA_NAME
))
10276 && single_imm_use (lhs
, &use_p
, &use_stmt
)
10277 && gimple_code (use_stmt
) == GIMPLE_COND
)
10280 tree new_rhs1
= NULL_TREE
;
10281 tree new_rhs2
= NULL_TREE
;
10282 tree cmp_var
= NULL_TREE
;
10284 if (TREE_CODE (rhs2
) == SSA_NAME
10285 && two_valued_val_range_p (rhs2
, &val1
, &val2
))
10287 /* Optimize RHS1 OP [VAL1, VAL2]. */
10288 new_rhs1
= int_const_binop (rhs_code
, rhs1
, val1
);
10289 new_rhs2
= int_const_binop (rhs_code
, rhs1
, val2
);
10292 else if (TREE_CODE (rhs1
) == SSA_NAME
10293 && two_valued_val_range_p (rhs1
, &val1
, &val2
))
10295 /* Optimize [VAL1, VAL2] OP RHS2. */
10296 new_rhs1
= int_const_binop (rhs_code
, val1
, rhs2
);
10297 new_rhs2
= int_const_binop (rhs_code
, val2
, rhs2
);
10301 /* If we could not find two-vals or the optimzation is invalid as
10302 in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
10303 if (new_rhs1
&& new_rhs2
)
10305 tree cond
= build2 (EQ_EXPR
, boolean_type_node
, cmp_var
, val1
);
10306 gimple_assign_set_rhs_with_ops (gsi
,
10310 update_stmt (gsi_stmt (*gsi
));
10311 fold_stmt (gsi
, follow_single_use_edges
);
10320 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
10321 if the RHS is zero or one, and the LHS are known to be boolean
10323 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10324 return simplify_truth_ops_using_ranges (gsi
, stmt
);
10327 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
10328 and BIT_AND_EXPR respectively if the first operand is greater
10329 than zero and the second operand is an exact power of two.
10330 Also optimize TRUNC_MOD_EXPR away if the second operand is
10331 constant and the first operand already has the right value
10333 case TRUNC_DIV_EXPR
:
10334 case TRUNC_MOD_EXPR
:
10335 if ((TREE_CODE (rhs1
) == SSA_NAME
10336 || TREE_CODE (rhs1
) == INTEGER_CST
)
10337 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10338 return simplify_div_or_mod_using_ranges (gsi
, stmt
);
10341 /* Transform ABS (X) into X or -X as appropriate. */
10343 if (TREE_CODE (rhs1
) == SSA_NAME
10344 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10345 return simplify_abs_using_ranges (gsi
, stmt
);
10350 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
10351 if all the bits being cleared are already cleared or
10352 all the bits being set are already set. */
10353 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10354 return simplify_bit_ops_using_ranges (gsi
, stmt
);
10358 if (TREE_CODE (rhs1
) == SSA_NAME
10359 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10360 return simplify_conversion_using_ranges (gsi
, stmt
);
10364 if (TREE_CODE (rhs1
) == SSA_NAME
10365 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10366 return simplify_float_conversion_using_ranges (gsi
, stmt
);
10371 return simplify_min_or_max_using_ranges (gsi
, stmt
);
10374 return simplify_assert_expr_using_ranges (stmt
);
10380 else if (gimple_code (stmt
) == GIMPLE_COND
)
10381 return simplify_cond_using_ranges_1 (as_a
<gcond
*> (stmt
));
10382 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
10383 return simplify_switch_using_ranges (as_a
<gswitch
*> (stmt
));
10384 else if (is_gimple_call (stmt
)
10385 && gimple_call_internal_p (stmt
))
10386 return simplify_internal_call_using_ranges (gsi
, stmt
);
10391 /* If the statement pointed by SI has a predicate whose value can be
10392 computed using the value range information computed by VRP, compute
10393 its value and return true. Otherwise, return false. */
10396 fold_predicate_in (gimple_stmt_iterator
*si
)
10398 bool assignment_p
= false;
10400 gimple
*stmt
= gsi_stmt (*si
);
10402 if (is_gimple_assign (stmt
)
10403 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
10405 assignment_p
= true;
10406 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
10407 gimple_assign_rhs1 (stmt
),
10408 gimple_assign_rhs2 (stmt
),
10411 else if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
10412 val
= vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
10413 gimple_cond_lhs (cond_stmt
),
10414 gimple_cond_rhs (cond_stmt
),
10422 val
= fold_convert (gimple_expr_type (stmt
), val
);
10426 fprintf (dump_file
, "Folding predicate ");
10427 print_gimple_expr (dump_file
, stmt
, 0, 0);
10428 fprintf (dump_file
, " to ");
10429 print_generic_expr (dump_file
, val
, 0);
10430 fprintf (dump_file
, "\n");
10433 if (is_gimple_assign (stmt
))
10434 gimple_assign_set_rhs_from_tree (si
, val
);
10437 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
10438 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
10439 if (integer_zerop (val
))
10440 gimple_cond_make_false (cond_stmt
);
10441 else if (integer_onep (val
))
10442 gimple_cond_make_true (cond_stmt
);
10444 gcc_unreachable ();
10453 /* Callback for substitute_and_fold folding the stmt at *SI. */
10456 vrp_fold_stmt (gimple_stmt_iterator
*si
)
10458 if (fold_predicate_in (si
))
10461 return simplify_stmt_using_ranges (si
);
10464 /* Return the LHS of any ASSERT_EXPR where OP appears as the first
10465 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
10466 BB. If no such ASSERT_EXPR is found, return OP. */
10469 lhs_of_dominating_assert (tree op
, basic_block bb
, gimple
*stmt
)
10471 imm_use_iterator imm_iter
;
10473 use_operand_p use_p
;
10475 if (TREE_CODE (op
) == SSA_NAME
)
10477 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, op
)
10479 use_stmt
= USE_STMT (use_p
);
10480 if (use_stmt
!= stmt
10481 && gimple_assign_single_p (use_stmt
)
10482 && TREE_CODE (gimple_assign_rhs1 (use_stmt
)) == ASSERT_EXPR
10483 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt
), 0) == op
10484 && dominated_by_p (CDI_DOMINATORS
, bb
, gimple_bb (use_stmt
)))
10485 return gimple_assign_lhs (use_stmt
);
10491 /* A trivial wrapper so that we can present the generic jump threading
10492 code with a simple API for simplifying statements. STMT is the
10493 statement we want to simplify, WITHIN_STMT provides the location
10494 for any overflow warnings. */
10497 simplify_stmt_for_jump_threading (gimple
*stmt
, gimple
*within_stmt
,
10498 class avail_exprs_stack
*avail_exprs_stack ATTRIBUTE_UNUSED
,
10501 /* First see if the conditional is in the hash table. */
10502 tree cached_lhs
= avail_exprs_stack
->lookup_avail_expr (stmt
, false, true);
10503 if (cached_lhs
&& is_gimple_min_invariant (cached_lhs
))
10506 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
10508 tree op0
= gimple_cond_lhs (cond_stmt
);
10509 op0
= lhs_of_dominating_assert (op0
, bb
, stmt
);
10511 tree op1
= gimple_cond_rhs (cond_stmt
);
10512 op1
= lhs_of_dominating_assert (op1
, bb
, stmt
);
10514 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
10515 op0
, op1
, within_stmt
);
10518 /* We simplify a switch statement by trying to determine which case label
10519 will be taken. If we are successful then we return the corresponding
10520 CASE_LABEL_EXPR. */
10521 if (gswitch
*switch_stmt
= dyn_cast
<gswitch
*> (stmt
))
10523 tree op
= gimple_switch_index (switch_stmt
);
10524 if (TREE_CODE (op
) != SSA_NAME
)
10527 op
= lhs_of_dominating_assert (op
, bb
, stmt
);
10529 value_range
*vr
= get_value_range (op
);
10530 if ((vr
->type
!= VR_RANGE
&& vr
->type
!= VR_ANTI_RANGE
)
10531 || symbolic_range_p (vr
))
10534 if (vr
->type
== VR_RANGE
)
10537 /* Get the range of labels that contain a part of the operand's
10539 find_case_label_range (switch_stmt
, vr
->min
, vr
->max
, &i
, &j
);
10541 /* Is there only one such label? */
10544 tree label
= gimple_switch_label (switch_stmt
, i
);
10546 /* The i'th label will be taken only if the value range of the
10547 operand is entirely within the bounds of this label. */
10548 if (CASE_HIGH (label
) != NULL_TREE
10549 ? (tree_int_cst_compare (CASE_LOW (label
), vr
->min
) <= 0
10550 && tree_int_cst_compare (CASE_HIGH (label
), vr
->max
) >= 0)
10551 : (tree_int_cst_equal (CASE_LOW (label
), vr
->min
)
10552 && tree_int_cst_equal (vr
->min
, vr
->max
)))
10556 /* If there are no such labels then the default label will be
10559 return gimple_switch_label (switch_stmt
, 0);
10562 if (vr
->type
== VR_ANTI_RANGE
)
10564 unsigned n
= gimple_switch_num_labels (switch_stmt
);
10565 tree min_label
= gimple_switch_label (switch_stmt
, 1);
10566 tree max_label
= gimple_switch_label (switch_stmt
, n
- 1);
10568 /* The default label will be taken only if the anti-range of the
10569 operand is entirely outside the bounds of all the (non-default)
10571 if (tree_int_cst_compare (vr
->min
, CASE_LOW (min_label
)) <= 0
10572 && (CASE_HIGH (max_label
) != NULL_TREE
10573 ? tree_int_cst_compare (vr
->max
, CASE_HIGH (max_label
)) >= 0
10574 : tree_int_cst_compare (vr
->max
, CASE_LOW (max_label
)) >= 0))
10575 return gimple_switch_label (switch_stmt
, 0);
10581 if (gassign
*assign_stmt
= dyn_cast
<gassign
*> (stmt
))
10583 value_range new_vr
= VR_INITIALIZER
;
10584 tree lhs
= gimple_assign_lhs (assign_stmt
);
10586 if (TREE_CODE (lhs
) == SSA_NAME
10587 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
10588 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
10590 extract_range_from_assignment (&new_vr
, assign_stmt
);
10591 if (range_int_cst_singleton_p (&new_vr
))
10599 class vrp_dom_walker
: public dom_walker
10602 vrp_dom_walker (cdi_direction direction
,
10603 class const_and_copies
*const_and_copies
,
10604 class avail_exprs_stack
*avail_exprs_stack
)
10605 : dom_walker (direction
, true),
10606 m_const_and_copies (const_and_copies
),
10607 m_avail_exprs_stack (avail_exprs_stack
),
10608 m_dummy_cond (NULL
) {}
10610 virtual edge
before_dom_children (basic_block
);
10611 virtual void after_dom_children (basic_block
);
10614 class const_and_copies
*m_const_and_copies
;
10615 class avail_exprs_stack
*m_avail_exprs_stack
;
10617 gcond
*m_dummy_cond
;
10620 /* Called before processing dominator children of BB. We want to look
10621 at ASSERT_EXPRs and record information from them in the appropriate
10624 We could look at other statements here. It's not seen as likely
10625 to significantly increase the jump threads we discover. */
10628 vrp_dom_walker::before_dom_children (basic_block bb
)
10630 gimple_stmt_iterator gsi
;
10632 for (gsi
= gsi_start_nondebug_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
10634 gimple
*stmt
= gsi_stmt (gsi
);
10635 if (gimple_assign_single_p (stmt
)
10636 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ASSERT_EXPR
)
10638 tree rhs1
= gimple_assign_rhs1 (stmt
);
10639 tree cond
= TREE_OPERAND (rhs1
, 1);
10640 tree lhs
= gimple_assign_lhs (stmt
);
10641 m_const_and_copies
->record_const_or_copy (lhs
, TREE_OPERAND (rhs1
, 0));
10643 if (TREE_CODE (cond
) == EQ_EXPR
)
10645 tree cond_op0
= TREE_OPERAND (cond
, 0);
10646 tree cond_op1
= TREE_OPERAND (cond
, 1);
10647 if (TREE_CODE (cond_op0
) == SSA_NAME
)
10648 m_const_and_copies
->record_const_or_copy (cond_op0
, cond_op1
);
10652 tree inverted
= invert_truthvalue (cond
);
10653 vec
<cond_equivalence
> p
;
10655 record_conditions (&p
, cond
, inverted
);
10656 for (unsigned int i
= 0; i
< p
.length (); i
++)
10657 m_avail_exprs_stack
->record_cond (&p
[i
]);
10667 /* Called after processing dominator children of BB. This is where we
10668 actually call into the threader. */
10670 vrp_dom_walker::after_dom_children (basic_block bb
)
10673 m_dummy_cond
= gimple_build_cond (NE_EXPR
,
10674 integer_zero_node
, integer_zero_node
,
10677 thread_outgoing_edges (bb
, m_dummy_cond
, m_const_and_copies
,
10678 m_avail_exprs_stack
,
10679 simplify_stmt_for_jump_threading
);
10681 m_avail_exprs_stack
->pop_to_marker ();
10682 m_const_and_copies
->pop_to_marker ();
10685 /* Blocks which have more than one predecessor and more than
10686 one successor present jump threading opportunities, i.e.,
10687 when the block is reached from a specific predecessor, we
10688 may be able to determine which of the outgoing edges will
10689 be traversed. When this optimization applies, we are able
10690 to avoid conditionals at runtime and we may expose secondary
10691 optimization opportunities.
10693 This routine is effectively a driver for the generic jump
10694 threading code. It basically just presents the generic code
10695 with edges that may be suitable for jump threading.
10697 Unlike DOM, we do not iterate VRP if jump threading was successful.
10698 While iterating may expose new opportunities for VRP, it is expected
10699 those opportunities would be very limited and the compile time cost
10700 to expose those opportunities would be significant.
10702 As jump threading opportunities are discovered, they are registered
10703 for later realization. */
10706 identify_jump_threads (void)
10711 /* Ugh. When substituting values earlier in this pass we can
10712 wipe the dominance information. So rebuild the dominator
10713 information as we need it within the jump threading code. */
10714 calculate_dominance_info (CDI_DOMINATORS
);
10716 /* We do not allow VRP information to be used for jump threading
10717 across a back edge in the CFG. Otherwise it becomes too
10718 difficult to avoid eliminating loop exit tests. Of course
10719 EDGE_DFS_BACK is not accurate at this time so we have to
10721 mark_dfs_back_edges ();
10723 /* Do not thread across edges we are about to remove. Just marking
10724 them as EDGE_IGNORE will do. */
10725 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10726 e
->flags
|= EDGE_IGNORE
;
10728 /* Allocate our unwinder stack to unwind any temporary equivalences
10729 that might be recorded. */
10730 const_and_copies
*equiv_stack
= new const_and_copies ();
10732 hash_table
<expr_elt_hasher
> *avail_exprs
10733 = new hash_table
<expr_elt_hasher
> (1024);
10734 avail_exprs_stack
*avail_exprs_stack
10735 = new class avail_exprs_stack (avail_exprs
);
10737 vrp_dom_walker
walker (CDI_DOMINATORS
, equiv_stack
, avail_exprs_stack
);
10738 walker
.walk (cfun
->cfg
->x_entry_block_ptr
);
10740 /* Clear EDGE_IGNORE. */
10741 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10742 e
->flags
&= ~EDGE_IGNORE
;
10744 /* We do not actually update the CFG or SSA graphs at this point as
10745 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10746 handle ASSERT_EXPRs gracefully. */
10747 delete equiv_stack
;
10748 delete avail_exprs
;
10749 delete avail_exprs_stack
;
10752 /* Free VRP lattice. */
10755 vrp_free_lattice ()
10757 /* Free allocated memory. */
10759 free (vr_phi_edge_counts
);
10760 bitmap_obstack_release (&vrp_equiv_obstack
);
10761 vrp_value_range_pool
.release ();
10763 /* So that we can distinguish between VRP data being available
10764 and not available. */
10766 vr_phi_edge_counts
= NULL
;
10769 /* Traverse all the blocks folding conditionals with known ranges. */
10772 vrp_finalize (bool warn_array_bounds_p
)
10776 values_propagated
= true;
10780 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
10781 dump_all_value_ranges (dump_file
);
10782 fprintf (dump_file
, "\n");
10785 /* Set value range to non pointer SSA_NAMEs. */
10786 for (i
= 0; i
< num_vr_values
; i
++)
10789 tree name
= ssa_name (i
);
10792 || (vr_value
[i
]->type
== VR_VARYING
)
10793 || (vr_value
[i
]->type
== VR_UNDEFINED
)
10794 || (TREE_CODE (vr_value
[i
]->min
) != INTEGER_CST
)
10795 || (TREE_CODE (vr_value
[i
]->max
) != INTEGER_CST
))
10798 if (POINTER_TYPE_P (TREE_TYPE (name
))
10799 && ((vr_value
[i
]->type
== VR_RANGE
10800 && range_includes_zero_p (vr_value
[i
]->min
,
10801 vr_value
[i
]->max
) == 0)
10802 || (vr_value
[i
]->type
== VR_ANTI_RANGE
10803 && range_includes_zero_p (vr_value
[i
]->min
,
10804 vr_value
[i
]->max
) == 1)))
10805 set_ptr_nonnull (name
);
10806 else if (!POINTER_TYPE_P (TREE_TYPE (name
)))
10807 set_range_info (name
, vr_value
[i
]->type
, vr_value
[i
]->min
,
10811 substitute_and_fold (op_with_constant_singleton_value_range
, vrp_fold_stmt
);
10813 if (warn_array_bounds
&& warn_array_bounds_p
)
10814 check_all_array_refs ();
10817 /* evrp_dom_walker visits the basic blocks in the dominance order and set
10818 the Value Ranges (VR) for SSA_NAMEs in the scope. Use this VR to
10819 discover more VRs. */
10821 class evrp_dom_walker
: public dom_walker
10825 : dom_walker (CDI_DOMINATORS
), stack (10)
10827 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
10829 ~evrp_dom_walker ()
10831 BITMAP_FREE (need_eh_cleanup
);
10833 virtual edge
before_dom_children (basic_block
);
10834 virtual void after_dom_children (basic_block
);
10835 void push_value_range (tree var
, value_range
*vr
);
10836 value_range
*pop_value_range (tree var
);
10837 value_range
*try_find_new_range (tree
, tree op
, tree_code code
, tree limit
);
10839 /* Cond_stack holds the old VR. */
10840 auto_vec
<std::pair
<tree
, value_range
*> > stack
;
10841 bitmap need_eh_cleanup
;
10842 auto_vec
<gimple
*> stmts_to_fixup
;
10843 auto_vec
<gimple
*> stmts_to_remove
;
10846 /* Find new range for NAME such that (OP CODE LIMIT) is true. */
10849 evrp_dom_walker::try_find_new_range (tree name
,
10850 tree op
, tree_code code
, tree limit
)
10852 value_range vr
= VR_INITIALIZER
;
10853 value_range
*old_vr
= get_value_range (name
);
10855 /* Discover VR when condition is true. */
10856 extract_range_for_var_from_comparison_expr (name
, code
, op
,
10858 /* If we found any usable VR, set the VR to ssa_name and create a
10859 PUSH old value in the stack with the old VR. */
10860 if (vr
.type
== VR_RANGE
|| vr
.type
== VR_ANTI_RANGE
)
10862 if (old_vr
->type
== vr
.type
10863 && vrp_operand_equal_p (old_vr
->min
, vr
.min
)
10864 && vrp_operand_equal_p (old_vr
->max
, vr
.max
))
10866 value_range
*new_vr
= vrp_value_range_pool
.allocate ();
10873 /* See if there is any new scope is entered with new VR and set that VR to
10874 ssa_name before visiting the statements in the scope. */
10877 evrp_dom_walker::before_dom_children (basic_block bb
)
10879 tree op0
= NULL_TREE
;
10883 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
10884 fprintf (dump_file
, "Visiting BB%d\n", bb
->index
);
10886 stack
.safe_push (std::make_pair (NULL_TREE
, (value_range
*)NULL
));
10888 edge pred_e
= NULL
;
10889 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
10891 /* Ignore simple backedges from this to allow recording conditions
10892 in loop headers. */
10893 if (dominated_by_p (CDI_DOMINATORS
, e
->src
, e
->dest
))
10905 gimple
*stmt
= last_stmt (pred_e
->src
);
10907 && gimple_code (stmt
) == GIMPLE_COND
10908 && (op0
= gimple_cond_lhs (stmt
))
10909 && TREE_CODE (op0
) == SSA_NAME
10910 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)))
10911 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)))))
10913 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
10915 fprintf (dump_file
, "Visiting controlling predicate ");
10916 print_gimple_stmt (dump_file
, stmt
, 0, 0);
10918 /* Entering a new scope. Try to see if we can find a VR
10920 tree op1
= gimple_cond_rhs (stmt
);
10921 if (TREE_OVERFLOW_P (op1
))
10922 op1
= drop_tree_overflow (op1
);
10923 tree_code code
= gimple_cond_code (stmt
);
10925 auto_vec
<assert_info
, 8> asserts
;
10926 register_edge_assert_for (op0
, pred_e
, code
, op0
, op1
, asserts
);
10927 if (TREE_CODE (op1
) == SSA_NAME
)
10928 register_edge_assert_for (op1
, pred_e
, code
, op0
, op1
, asserts
);
10930 auto_vec
<std::pair
<tree
, value_range
*>, 8> vrs
;
10931 for (unsigned i
= 0; i
< asserts
.length (); ++i
)
10933 value_range
*vr
= try_find_new_range (asserts
[i
].name
,
10935 asserts
[i
].comp_code
,
10938 vrs
.safe_push (std::make_pair (asserts
[i
].name
, vr
));
10940 /* Push updated ranges only after finding all of them to avoid
10941 ordering issues that can lead to worse ranges. */
10942 for (unsigned i
= 0; i
< vrs
.length (); ++i
)
10943 push_value_range (vrs
[i
].first
, vrs
[i
].second
);
10947 /* Visit PHI stmts and discover any new VRs possible. */
10948 bool has_unvisited_preds
= false;
10949 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
10950 if (e
->flags
& EDGE_EXECUTABLE
10951 && !(e
->src
->flags
& BB_VISITED
))
10953 has_unvisited_preds
= true;
10957 for (gphi_iterator gpi
= gsi_start_phis (bb
);
10958 !gsi_end_p (gpi
); gsi_next (&gpi
))
10960 gphi
*phi
= gpi
.phi ();
10961 tree lhs
= PHI_RESULT (phi
);
10962 if (virtual_operand_p (lhs
))
10964 value_range vr_result
= VR_INITIALIZER
;
10965 bool interesting
= stmt_interesting_for_vrp (phi
);
10966 if (interesting
&& dump_file
&& (dump_flags
& TDF_DETAILS
))
10968 fprintf (dump_file
, "Visiting PHI node ");
10969 print_gimple_stmt (dump_file
, phi
, 0, 0);
10971 if (!has_unvisited_preds
10973 extract_range_from_phi_node (phi
, &vr_result
);
10976 set_value_range_to_varying (&vr_result
);
10977 /* When we have an unvisited executable predecessor we can't
10978 use PHI arg ranges which may be still UNDEFINED but have
10979 to use VARYING for them. But we can still resort to
10980 SCEV for loop header PHIs. */
10983 && (l
= loop_containing_stmt (phi
))
10984 && l
->header
== gimple_bb (phi
))
10985 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
10987 update_value_range (lhs
, &vr_result
);
10989 /* Mark PHIs whose lhs we fully propagate for removal. */
10990 tree val
= op_with_constant_singleton_value_range (lhs
);
10991 if (val
&& may_propagate_copy (lhs
, val
))
10993 stmts_to_remove
.safe_push (phi
);
10997 /* Set the SSA with the value range. */
10998 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs
)))
11000 if ((vr_result
.type
== VR_RANGE
11001 || vr_result
.type
== VR_ANTI_RANGE
)
11002 && (TREE_CODE (vr_result
.min
) == INTEGER_CST
)
11003 && (TREE_CODE (vr_result
.max
) == INTEGER_CST
))
11004 set_range_info (lhs
,
11005 vr_result
.type
, vr_result
.min
, vr_result
.max
);
11007 else if (POINTER_TYPE_P (TREE_TYPE (lhs
))
11008 && ((vr_result
.type
== VR_RANGE
11009 && range_includes_zero_p (vr_result
.min
,
11010 vr_result
.max
) == 0)
11011 || (vr_result
.type
== VR_ANTI_RANGE
11012 && range_includes_zero_p (vr_result
.min
,
11013 vr_result
.max
) == 1)))
11014 set_ptr_nonnull (lhs
);
11017 edge taken_edge
= NULL
;
11019 /* Visit all other stmts and discover any new VRs possible. */
11020 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
);
11021 !gsi_end_p (gsi
); gsi_next (&gsi
))
11023 gimple
*stmt
= gsi_stmt (gsi
);
11024 tree output
= NULL_TREE
;
11025 gimple
*old_stmt
= stmt
;
11026 bool was_noreturn
= (is_gimple_call (stmt
)
11027 && gimple_call_noreturn_p (stmt
));
11029 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
11031 fprintf (dump_file
, "Visiting stmt ");
11032 print_gimple_stmt (dump_file
, stmt
, 0, 0);
11035 if (gcond
*cond
= dyn_cast
<gcond
*> (stmt
))
11037 vrp_visit_cond_stmt (cond
, &taken_edge
);
11040 if (taken_edge
->flags
& EDGE_TRUE_VALUE
)
11041 gimple_cond_make_true (cond
);
11042 else if (taken_edge
->flags
& EDGE_FALSE_VALUE
)
11043 gimple_cond_make_false (cond
);
11045 gcc_unreachable ();
11046 update_stmt (stmt
);
11049 else if (stmt_interesting_for_vrp (stmt
))
11052 value_range vr
= VR_INITIALIZER
;
11053 extract_range_from_stmt (stmt
, &taken_edge
, &output
, &vr
);
11055 && (vr
.type
== VR_RANGE
|| vr
.type
== VR_ANTI_RANGE
))
11057 update_value_range (output
, &vr
);
11058 vr
= *get_value_range (output
);
11060 /* Mark stmts whose output we fully propagate for removal. */
11062 if ((val
= op_with_constant_singleton_value_range (output
))
11063 && may_propagate_copy (output
, val
)
11064 && !stmt_could_throw_p (stmt
)
11065 && !gimple_has_side_effects (stmt
))
11067 stmts_to_remove
.safe_push (stmt
);
11071 /* Set the SSA with the value range. */
11072 if (INTEGRAL_TYPE_P (TREE_TYPE (output
)))
11074 if ((vr
.type
== VR_RANGE
11075 || vr
.type
== VR_ANTI_RANGE
)
11076 && (TREE_CODE (vr
.min
) == INTEGER_CST
)
11077 && (TREE_CODE (vr
.max
) == INTEGER_CST
))
11078 set_range_info (output
, vr
.type
, vr
.min
, vr
.max
);
11080 else if (POINTER_TYPE_P (TREE_TYPE (output
))
11081 && ((vr
.type
== VR_RANGE
11082 && range_includes_zero_p (vr
.min
,
11084 || (vr
.type
== VR_ANTI_RANGE
11085 && range_includes_zero_p (vr
.min
,
11087 set_ptr_nonnull (output
);
11090 set_defs_to_varying (stmt
);
11093 set_defs_to_varying (stmt
);
11095 /* See if we can derive a range for any of STMT's operands. */
11098 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
11101 enum tree_code comp_code
;
11103 /* If OP is used in such a way that we can infer a value
11104 range for it, and we don't find a previous assertion for
11105 it, create a new assertion location node for OP. */
11106 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
11108 /* If we are able to infer a nonzero value range for OP,
11109 then walk backwards through the use-def chain to see if OP
11110 was set via a typecast.
11111 If so, then we can also infer a nonzero value range
11112 for the operand of the NOP_EXPR. */
11113 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
11116 gimple
*def_stmt
= SSA_NAME_DEF_STMT (t
);
11117 while (is_gimple_assign (def_stmt
)
11118 && CONVERT_EXPR_CODE_P
11119 (gimple_assign_rhs_code (def_stmt
))
11121 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
11123 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
11125 t
= gimple_assign_rhs1 (def_stmt
);
11126 def_stmt
= SSA_NAME_DEF_STMT (t
);
11128 /* Add VR when (T COMP_CODE value) condition is
11130 value_range
*op_range
11131 = try_find_new_range (t
, t
, comp_code
, value
);
11133 push_value_range (t
, op_range
);
11136 /* Add VR when (OP COMP_CODE value) condition is true. */
11137 value_range
*op_range
= try_find_new_range (op
, op
,
11140 push_value_range (op
, op_range
);
11144 /* Try folding stmts with the VR discovered. */
11146 = replace_uses_in (stmt
, op_with_constant_singleton_value_range
);
11147 if (fold_stmt (&gsi
, follow_single_use_edges
)
11150 stmt
= gsi_stmt (gsi
);
11151 update_stmt (stmt
);
11152 did_replace
= true;
11157 /* If we cleaned up EH information from the statement,
11158 remove EH edges. */
11159 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
11160 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
11162 /* If we turned a not noreturn call into a noreturn one
11163 schedule it for fixup. */
11165 && is_gimple_call (stmt
)
11166 && gimple_call_noreturn_p (stmt
))
11167 stmts_to_fixup
.safe_push (stmt
);
11169 if (gimple_assign_single_p (stmt
))
11171 tree rhs
= gimple_assign_rhs1 (stmt
);
11172 if (TREE_CODE (rhs
) == ADDR_EXPR
)
11173 recompute_tree_invariant_for_addr_expr (rhs
);
11178 /* Visit BB successor PHI nodes and replace PHI args. */
11179 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
11181 for (gphi_iterator gpi
= gsi_start_phis (e
->dest
);
11182 !gsi_end_p (gpi
); gsi_next (&gpi
))
11184 gphi
*phi
= gpi
.phi ();
11185 use_operand_p use_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
);
11186 tree arg
= USE_FROM_PTR (use_p
);
11187 if (TREE_CODE (arg
) != SSA_NAME
11188 || virtual_operand_p (arg
))
11190 tree val
= op_with_constant_singleton_value_range (arg
);
11191 if (val
&& may_propagate_copy (arg
, val
))
11192 propagate_value (use_p
, val
);
11196 bb
->flags
|= BB_VISITED
;
11201 /* Restore/pop VRs valid only for BB when we leave BB. */
11204 evrp_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED
)
11206 gcc_checking_assert (!stack
.is_empty ());
11207 while (stack
.last ().first
!= NULL_TREE
)
11208 pop_value_range (stack
.last ().first
);
11212 /* Push the Value Range of VAR to the stack and update it with new VR. */
11215 evrp_dom_walker::push_value_range (tree var
, value_range
*vr
)
11217 if (SSA_NAME_VERSION (var
) >= num_vr_values
)
11219 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
11221 fprintf (dump_file
, "pushing new range for ");
11222 print_generic_expr (dump_file
, var
, 0);
11223 fprintf (dump_file
, ": ");
11224 dump_value_range (dump_file
, vr
);
11225 fprintf (dump_file
, "\n");
11227 stack
.safe_push (std::make_pair (var
, get_value_range (var
)));
11228 vr_value
[SSA_NAME_VERSION (var
)] = vr
;
11231 /* Pop the Value Range from the vrp_stack and update VAR with it. */
11234 evrp_dom_walker::pop_value_range (tree var
)
11236 value_range
*vr
= stack
.last ().second
;
11237 gcc_checking_assert (var
== stack
.last ().first
);
11238 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
11240 fprintf (dump_file
, "popping range for ");
11241 print_generic_expr (dump_file
, var
, 0);
11242 fprintf (dump_file
, ", restoring ");
11243 dump_value_range (dump_file
, vr
);
11244 fprintf (dump_file
, "\n");
11246 vr_value
[SSA_NAME_VERSION (var
)] = vr
;
11252 /* Main entry point for the early vrp pass which is a simplified non-iterative
11253 version of vrp where basic blocks are visited in dominance order. Value
11254 ranges discovered in early vrp will also be used by ipa-vrp. */
11256 static unsigned int
11257 execute_early_vrp ()
11263 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
11264 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
11265 scev_initialize ();
11266 calculate_dominance_info (CDI_DOMINATORS
);
11267 FOR_EACH_BB_FN (bb
, cfun
)
11269 bb
->flags
&= ~BB_VISITED
;
11270 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
11271 e
->flags
|= EDGE_EXECUTABLE
;
11273 vrp_initialize_lattice ();
11275 /* Walk stmts in dominance order and propagate VRP. */
11276 evrp_dom_walker walker
;
11277 walker
.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
11281 fprintf (dump_file
, "\nValue ranges after Early VRP:\n\n");
11282 dump_all_value_ranges (dump_file
);
11283 fprintf (dump_file
, "\n");
11286 /* Remove stmts in reverse order to make debug stmt creation possible. */
11287 while (! walker
.stmts_to_remove
.is_empty ())
11289 gimple
*stmt
= walker
.stmts_to_remove
.pop ();
11290 if (dump_file
&& dump_flags
& TDF_DETAILS
)
11292 fprintf (dump_file
, "Removing dead stmt ");
11293 print_gimple_stmt (dump_file
, stmt
, 0, 0);
11294 fprintf (dump_file
, "\n");
11296 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
11297 if (gimple_code (stmt
) == GIMPLE_PHI
)
11298 remove_phi_node (&gsi
, true);
11301 unlink_stmt_vdef (stmt
);
11302 gsi_remove (&gsi
, true);
11303 release_defs (stmt
);
11307 if (!bitmap_empty_p (walker
.need_eh_cleanup
))
11308 gimple_purge_all_dead_eh_edges (walker
.need_eh_cleanup
);
11310 /* Fixup stmts that became noreturn calls. This may require splitting
11311 blocks and thus isn't possible during the dominator walk. Do this
11312 in reverse order so we don't inadvertedly remove a stmt we want to
11313 fixup by visiting a dominating now noreturn call first. */
11314 while (!walker
.stmts_to_fixup
.is_empty ())
11316 gimple
*stmt
= walker
.stmts_to_fixup
.pop ();
11317 fixup_noreturn_call (stmt
);
11320 vrp_free_lattice ();
11322 loop_optimizer_finalize ();
11327 /* Main entry point to VRP (Value Range Propagation). This pass is
11328 loosely based on J. R. C. Patterson, ``Accurate Static Branch
11329 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
11330 Programming Language Design and Implementation, pp. 67-78, 1995.
11331 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
11333 This is essentially an SSA-CCP pass modified to deal with ranges
11334 instead of constants.
11336 While propagating ranges, we may find that two or more SSA name
11337 have equivalent, though distinct ranges. For instance,
11340 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
11342 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
11346 In the code above, pointer p_5 has range [q_2, q_2], but from the
11347 code we can also determine that p_5 cannot be NULL and, if q_2 had
11348 a non-varying range, p_5's range should also be compatible with it.
11350 These equivalences are created by two expressions: ASSERT_EXPR and
11351 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
11352 result of another assertion, then we can use the fact that p_5 and
11353 p_4 are equivalent when evaluating p_5's range.
11355 Together with value ranges, we also propagate these equivalences
11356 between names so that we can take advantage of information from
11357 multiple ranges when doing final replacement. Note that this
11358 equivalency relation is transitive but not symmetric.
11360 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
11361 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
11362 in contexts where that assertion does not hold (e.g., in line 6).
11364 TODO, the main difference between this pass and Patterson's is that
11365 we do not propagate edge probabilities. We only compute whether
11366 edges can be taken or not. That is, instead of having a spectrum
11367 of jump probabilities between 0 and 1, we only deal with 0, 1 and
11368 DON'T KNOW. In the future, it may be worthwhile to propagate
11369 probabilities to aid branch prediction. */
11371 static unsigned int
11372 execute_vrp (bool warn_array_bounds_p
)
11378 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
11379 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
11380 scev_initialize ();
11382 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
11383 Inserting assertions may split edges which will invalidate
11385 insert_range_assertions ();
11387 to_remove_edges
.create (10);
11388 to_update_switch_stmts
.create (5);
11389 threadedge_initialize_values ();
11391 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
11392 mark_dfs_back_edges ();
11394 vrp_initialize_lattice ();
11396 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
11397 vrp_finalize (warn_array_bounds_p
);
11399 /* We must identify jump threading opportunities before we release
11400 the datastructures built by VRP. */
11401 identify_jump_threads ();
11403 /* A comparison of an SSA_NAME against a constant where the SSA_NAME
11404 was set by a type conversion can often be rewritten to use the
11405 RHS of the type conversion.
11407 However, doing so inhibits jump threading through the comparison.
11408 So that transformation is not performed until after jump threading
11411 FOR_EACH_BB_FN (bb
, cfun
)
11413 gimple
*last
= last_stmt (bb
);
11414 if (last
&& gimple_code (last
) == GIMPLE_COND
)
11415 simplify_cond_using_ranges_2 (as_a
<gcond
*> (last
));
11418 vrp_free_lattice ();
11420 free_numbers_of_iterations_estimates (cfun
);
11422 /* ASSERT_EXPRs must be removed before finalizing jump threads
11423 as finalizing jump threads calls the CFG cleanup code which
11424 does not properly handle ASSERT_EXPRs. */
11425 remove_range_assertions ();
11427 /* If we exposed any new variables, go ahead and put them into
11428 SSA form now, before we handle jump threading. This simplifies
11429 interactions between rewriting of _DECL nodes into SSA form
11430 and rewriting SSA_NAME nodes into SSA form after block
11431 duplication and CFG manipulation. */
11432 update_ssa (TODO_update_ssa
);
11434 /* We identified all the jump threading opportunities earlier, but could
11435 not transform the CFG at that time. This routine transforms the
11436 CFG and arranges for the dominator tree to be rebuilt if necessary.
11438 Note the SSA graph update will occur during the normal TODO
11439 processing by the pass manager. */
11440 thread_through_all_blocks (false);
11442 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
11443 CFG in a broken state and requires a cfg_cleanup run. */
11444 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
11446 /* Update SWITCH_EXPR case label vector. */
11447 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
11450 size_t n
= TREE_VEC_LENGTH (su
->vec
);
11452 gimple_switch_set_num_labels (su
->stmt
, n
);
11453 for (j
= 0; j
< n
; j
++)
11454 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
11455 /* As we may have replaced the default label with a regular one
11456 make sure to make it a real default label again. This ensures
11457 optimal expansion. */
11458 label
= gimple_switch_label (su
->stmt
, 0);
11459 CASE_LOW (label
) = NULL_TREE
;
11460 CASE_HIGH (label
) = NULL_TREE
;
11463 if (to_remove_edges
.length () > 0)
11465 free_dominance_info (CDI_DOMINATORS
);
11466 loops_state_set (LOOPS_NEED_FIXUP
);
11469 to_remove_edges
.release ();
11470 to_update_switch_stmts
.release ();
11471 threadedge_finalize_values ();
11474 loop_optimizer_finalize ();
11480 const pass_data pass_data_vrp
=
11482 GIMPLE_PASS
, /* type */
11484 OPTGROUP_NONE
, /* optinfo_flags */
11485 TV_TREE_VRP
, /* tv_id */
11486 PROP_ssa
, /* properties_required */
11487 0, /* properties_provided */
11488 0, /* properties_destroyed */
11489 0, /* todo_flags_start */
11490 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
11493 class pass_vrp
: public gimple_opt_pass
11496 pass_vrp (gcc::context
*ctxt
)
11497 : gimple_opt_pass (pass_data_vrp
, ctxt
), warn_array_bounds_p (false)
11500 /* opt_pass methods: */
11501 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
11502 void set_pass_param (unsigned int n
, bool param
)
11504 gcc_assert (n
== 0);
11505 warn_array_bounds_p
= param
;
11507 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
11508 virtual unsigned int execute (function
*)
11509 { return execute_vrp (warn_array_bounds_p
); }
11512 bool warn_array_bounds_p
;
11513 }; // class pass_vrp
11515 } // anon namespace
11518 make_pass_vrp (gcc::context
*ctxt
)
11520 return new pass_vrp (ctxt
);
11525 const pass_data pass_data_early_vrp
=
11527 GIMPLE_PASS
, /* type */
11529 OPTGROUP_NONE
, /* optinfo_flags */
11530 TV_TREE_EARLY_VRP
, /* tv_id */
11531 PROP_ssa
, /* properties_required */
11532 0, /* properties_provided */
11533 0, /* properties_destroyed */
11534 0, /* todo_flags_start */
11535 ( TODO_cleanup_cfg
| TODO_update_ssa
| TODO_verify_all
),
11538 class pass_early_vrp
: public gimple_opt_pass
11541 pass_early_vrp (gcc::context
*ctxt
)
11542 : gimple_opt_pass (pass_data_early_vrp
, ctxt
)
11545 /* opt_pass methods: */
11546 opt_pass
* clone () { return new pass_early_vrp (m_ctxt
); }
11547 virtual bool gate (function
*)
11549 return flag_tree_vrp
!= 0;
11551 virtual unsigned int execute (function
*)
11552 { return execute_early_vrp (); }
11554 }; // class pass_vrp
11555 } // anon namespace
11558 make_pass_early_vrp (gcc::context
*ctxt
)
11560 return new pass_early_vrp (ctxt
);