diff options
author | Richard Sandiford <richard.sandiford@linaro.org> | 2018-02-08 15:16:29 +0000 |
---|---|---|
committer | Richard Sandiford <rsandifo@gcc.gnu.org> | 2018-02-08 15:16:29 +0000 |
commit | fff2290073cc2d57dcade125227b74cd27c48066 (patch) | |
tree | 09569e058630b4d92cbafdd8c0bd833aa0df8da9 /gcc/tree-vrp.c | |
parent | 39aa9b2369eff7f2be0712ea7f1ee12f8697ce36 (diff) | |
download | gcc-fff2290073cc2d57dcade125227b74cd27c48066.zip gcc-fff2290073cc2d57dcade125227b74cd27c48066.tar.gz gcc-fff2290073cc2d57dcade125227b74cd27c48066.tar.bz2 |
Use nonzero bits to refine range in split_constant_offset (PR 81635)
This patch is part 2 of the fix for PR 81635. It means that
split_constant_offset can handle loops like:
for (unsigned int i = 0; i < n; i += 4)
{
a[i] = ...;
a[i + 1] = ...;
}
CCP records that "i" must have its low 2 bits clear, but we don't
include this information in the range of "i", which remains [0, +INF].
I tried making set_nonzero_bits update the range info in the same
way that set_range_info updates the nonzero bits, but it regressed
cases like vrp117.c and made some other tests worse.
vrp117.c has a multiplication by 10, so CCP can infer that the low bit
of the result is clear. If we included that in the range, the range
would go from [-INF, +INF] to [-INF, not-quite-+INF]. However,
the multiplication is also known to overflow in all cases, so VRP
saturates the result to [INT_MAX, INT_MAX]. This obviously creates a
contradiction with the nonzero bits, and intersecting the new saturated
range with an existing not-quite-+INF range would make us drop to
VR_UNDEFINED. We're prepared to fold a comparison with an [INT_MAX,
INT_MAX] value but not with a VR_UNDEFINED value.
The other problems were created when intersecting [-INF, not-quite-+INF]
with a useful VR_ANTI_RANGE like ~[-1, 1]. The intersection would
keep the former range rather than the latter.
The patch therefore keeps the adjustment local to split_constant_offset
for now, but adds a helper routine so that it's easy to move this later.
2018-02-08 Richard Sandiford <richard.sandiford@linaro.org>
gcc/
PR tree-optimization/81635
* wide-int.h (wi::round_down_for_mask, wi::round_up_for_mask): Declare.
* wide-int.cc (wi::round_down_for_mask, wi::round_up_for_mask)
(test_round_for_mask): New functions.
(wide_int_cc_tests): Call test_round_for_mask.
* tree-vrp.h (intersect_range_with_nonzero_bits): Declare.
* tree-vrp.c (intersect_range_with_nonzero_bits): New function.
* tree-data-ref.c (split_constant_offset_1): Use it to refine the
range returned by get_range_info.
gcc/testsuite/
PR tree-optimization/81635
* gcc.dg/vect/bb-slp-pr81635-3.c: New test.
* gcc.dg/vect/bb-slp-pr81635-4.c: Likewise.
From-SVN: r257491
Diffstat (limited to 'gcc/tree-vrp.c')
-rw-r--r-- | gcc/tree-vrp.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/gcc/tree-vrp.c b/gcc/tree-vrp.c index 3af81f7..cba58e0 100644 --- a/gcc/tree-vrp.c +++ b/gcc/tree-vrp.c @@ -171,6 +171,53 @@ vrp_val_is_min (const_tree val) && operand_equal_p (val, type_min, 0))); } +/* VR_TYPE describes a range with mininum value *MIN and maximum + value *MAX. Restrict the range to the set of values that have + no bits set outside NONZERO_BITS. Update *MIN and *MAX and + return the new range type. + + SGN gives the sign of the values described by the range. */ + +enum value_range_type +intersect_range_with_nonzero_bits (enum value_range_type vr_type, + wide_int *min, wide_int *max, + const wide_int &nonzero_bits, + signop sgn) +{ + if (vr_type == VR_RANGE) + { + *max = wi::round_down_for_mask (*max, nonzero_bits); + + /* Check that the range contains at least one valid value. */ + if (wi::gt_p (*min, *max, sgn)) + return VR_UNDEFINED; + + *min = wi::round_up_for_mask (*min, nonzero_bits); + gcc_checking_assert (wi::le_p (*min, *max, sgn)); + } + if (vr_type == VR_ANTI_RANGE) + { + *max = wi::round_up_for_mask (*max, nonzero_bits); + + /* If the calculation wrapped, we now have a VR_RANGE whose + lower bound is *MAX and whose upper bound is *MIN. */ + if (wi::gt_p (*min, *max, sgn)) + { + std::swap (*min, *max); + *max = wi::round_down_for_mask (*max, nonzero_bits); + gcc_checking_assert (wi::le_p (*min, *max, sgn)); + return VR_RANGE; + } + + *min = wi::round_down_for_mask (*min, nonzero_bits); + gcc_checking_assert (wi::le_p (*min, *max, sgn)); + + /* Check whether we now have an empty set of values. */ + if (*min - 1 == *max) + return VR_UNDEFINED; + } + return vr_type; +} /* Set value range VR to VR_UNDEFINED. */ |