[llvm] a6183d0 - [ValueTracking] isKnownNonZero, computeKnownBits for freeze
Juneyoung Lee via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 9 16:08:05 PDT 2020
Author: Juneyoung Lee
Date: 2020-09-10T08:07:38+09:00
New Revision: a6183d0f028cb73eccc82a7cce9534708a149762
URL: https://github.com/llvm/llvm-project/commit/a6183d0f028cb73eccc82a7cce9534708a149762
DIFF: https://github.com/llvm/llvm-project/commit/a6183d0f028cb73eccc82a7cce9534708a149762.diff
LOG: [ValueTracking] isKnownNonZero, computeKnownBits for freeze
This implements support for isKnownNonZero, computeKnownBits when freeze is involved.
```
br (x != 0), BB1, BB2
BB1:
y = freeze x
```
In the above program, we can say that y is non-zero. The reason is as follows:
(1) If x was poison, `br (x != 0)` raised UB
(2) If x was fully undef, the branch again raised UB
(3) If x was non-zero partially undef, say `undef | 1`, `freeze x` will return a nondeterministic value which is also non-zero.
(4) If x was just a concrete value, it is trivial
Reviewed By: nikic
Differential Revision: https://reviews.llvm.org/D75808
Added:
Modified:
llvm/lib/Analysis/ValueTracking.cpp
llvm/test/Transforms/InstSimplify/known-non-zero.ll
llvm/unittests/Analysis/ValueTrackingTest.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 469257d91071..1a894959c5bd 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -1872,6 +1872,10 @@ static void computeKnownBitsFromOperator(const Operator *I,
}
}
break;
+ case Instruction::Freeze:
+ if (isGuaranteedNotToBePoison(I->getOperand(0), Q.CxtI, Q.DT, Depth + 1))
+ computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
+ break;
}
}
@@ -2577,6 +2581,13 @@ bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth,
return isKnownNonZero(Vec, DemandedVecElts, Depth, Q);
}
}
+ // Freeze
+ else if (const FreezeInst *FI = dyn_cast<FreezeInst>(V)) {
+ auto *Op = FI->getOperand(0);
+ if (isKnownNonZero(Op, Depth, Q) &&
+ isGuaranteedNotToBePoison(Op, Q.CxtI, Q.DT, Depth))
+ return true;
+ }
KnownBits Known(BitWidth);
computeKnownBits(V, DemandedElts, Known, Depth, Q);
diff --git a/llvm/test/Transforms/InstSimplify/known-non-zero.ll b/llvm/test/Transforms/InstSimplify/known-non-zero.ll
index 524e51be76f5..2af4f2716206 100644
--- a/llvm/test/Transforms/InstSimplify/known-non-zero.ll
+++ b/llvm/test/Transforms/InstSimplify/known-non-zero.ll
@@ -145,3 +145,24 @@ for.body: ; preds = %for.cond
%inc = add nuw nsw i32 %shift.0, 1
br label %for.cond
}
+
+define i1 @freeze_nonzero(i8 %x, i8 %mask) {
+; CHECK-LABEL: @freeze_nonzero(
+; CHECK-NEXT: [[Y:%.*]] = or i8 [[X:%.*]], [[MASK:%.*]]
+; CHECK-NEXT: [[C:%.*]] = icmp ne i8 [[Y]], 0
+; CHECK-NEXT: br i1 [[C]], label [[A:%.*]], label [[B:%.*]]
+; CHECK: A:
+; CHECK-NEXT: ret i1 false
+; CHECK: B:
+; CHECK-NEXT: ret i1 false
+;
+ %y = or i8 %x, %mask
+ %c = icmp ne i8 %y, 0
+ br i1 %c, label %A, label %B
+A:
+ %fr = freeze i8 %y
+ %c2 = icmp eq i8 %fr, 0
+ ret i1 %c2
+B:
+ ret i1 0
+}
diff --git a/llvm/unittests/Analysis/ValueTrackingTest.cpp b/llvm/unittests/Analysis/ValueTrackingTest.cpp
index 09faad448459..c45bca1c53bf 100644
--- a/llvm/unittests/Analysis/ValueTrackingTest.cpp
+++ b/llvm/unittests/Analysis/ValueTrackingTest.cpp
@@ -1059,6 +1059,24 @@ TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntZext) {
EXPECT_EQ(Known.One.getZExtValue(), 0u);
}
+TEST_F(ComputeKnownBitsTest, ComputeKnownBitsFreeze) {
+ parseAssembly("define void @test() {\n"
+ " %m = call i32 @any_num()\n"
+ " %A = freeze i32 %m\n"
+ " %n = and i32 %m, 31\n"
+ " %c = icmp eq i32 %n, 0\n"
+ " call void @llvm.assume(i1 %c)\n"
+ " ret void\n"
+ "}\n"
+ "declare void @llvm.assume(i1)\n"
+ "declare i32 @any_num()\n");
+ AssumptionCache AC(*F);
+ KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC,
+ F->front().getTerminator());
+ EXPECT_EQ(Known.Zero.getZExtValue(), 31u);
+ EXPECT_EQ(Known.One.getZExtValue(), 0u);
+}
+
class IsBytewiseValueTest : public ValueTrackingTest,
public ::testing::WithParamInterface<
std::pair<const char *, const char *>> {
More information about the llvm-commits
mailing list