diff options
author | Oliver Stannard <oliver.stannard@arm.com> | 2017-03-14 10:13:17 +0000 |
---|---|---|
committer | Oliver Stannard <oliver.stannard@arm.com> | 2017-03-14 10:13:17 +0000 |
commit | 062041113f86993cc0bb1488e90184ce5adf1e59 (patch) | |
tree | 98358496888a47e46834fc1c8a3d087d1223d354 | |
parent | d0bee506a04f012052d566baa206d8c8af95a3af (diff) | |
download | bcm5719-llvm-062041113f86993cc0bb1488e90184ce5adf1e59.tar.gz bcm5719-llvm-062041113f86993cc0bb1488e90184ce5adf1e59.zip |
[ValueTracking] Out of range shifts might be undef
If it is possible for the RHS of a shift operation to be greater than or equal
to the bit-width, then the result might be undef, and we can't report any known
bits.
In some cases, this was allowing a transformation in instcombine which widened
an undef value from i1 to i32, increasing the range of values that a function
could return.
Differential revision: https://reviews.llvm.org/D30781
llvm-svn: 297724
-rw-r--r-- | llvm/lib/Analysis/ValueTracking.cpp | 8 | ||||
-rw-r--r-- | llvm/test/Transforms/InstCombine/zext-or-icmp.ll | 30 |
2 files changed, 38 insertions, 0 deletions
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index 1761dac269d..a3d4198edcb 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -848,6 +848,14 @@ static void computeKnownBitsFromShiftOperator( computeKnownBits(I->getOperand(1), KnownZero, KnownOne, Depth + 1, Q); + // If the shift amount could be greater than or equal to the bit-width of the LHS, the + // value could be undef, so we don't know anything about it. + if ((~KnownZero).uge(BitWidth)) { + KnownZero.clearAllBits(); + KnownOne.clearAllBits(); + return; + } + // Note: We cannot use KnownZero.getLimitedValue() here, because if // BitWidth > 64 and any upper bits are known, we'll end up returning the // limit value (which implies all bits are known). diff --git a/llvm/test/Transforms/InstCombine/zext-or-icmp.ll b/llvm/test/Transforms/InstCombine/zext-or-icmp.ll index 610e9a754f0..afbe36da3e3 100644 --- a/llvm/test/Transforms/InstCombine/zext-or-icmp.ll +++ b/llvm/test/Transforms/InstCombine/zext-or-icmp.ll @@ -19,3 +19,33 @@ define i8 @zext_or_icmp_icmp(i8 %a, i8 %b) { ; CHECK-NEXT: ret i8 %zext } +; Here, widening the or from i1 to i32 and removing one of the icmps would +; widen an undef value (created by the out-of-range shift), increasing the +; range of valid values for the return, so we can't do it. +define i32 @dont_widen_undef() { +entry: + br label %block2 + +block1: + br label %block2 + +block2: + %m.011 = phi i32 [ 33, %entry ], [ 0, %block1 ] + %cmp.i = icmp ugt i32 %m.011, 1 + %m.1.op = lshr i32 1, %m.011 + %sext.mask = and i32 %m.1.op, 65535 + %cmp115 = icmp ne i32 %sext.mask, 0 + %cmp1 = or i1 %cmp.i, %cmp115 + %conv2 = zext i1 %cmp1 to i32 + ret i32 %conv2 + +; CHECK-LABEL: dont_widen_undef( +; CHECK: %m.011 = phi i32 [ 33, %entry ], [ 0, %block1 ] +; CHECK-NEXT: %cmp.i = icmp ugt i32 %m.011, 1 +; CHECK-NEXT: %m.1.op = lshr i32 1, %m.011 +; CHECK-NEXT: %sext.mask = and i32 %m.1.op, 65535 +; CHECK-NEXT: %cmp115 = icmp ne i32 %sext.mask, 0 +; CHECK-NEXT: %cmp1 = or i1 %cmp.i, %cmp115 +; CHECK-NEXT: %conv2 = zext i1 %cmp1 to i32 +; CHECK-NEXT: ret i32 %conv2 +} |