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
}
}
break;
+ case Instruction::Freeze:
+ if (isGuaranteedNotToBePoison(I->getOperand(0), Q.CxtI, Q.DT, Depth + 1))
+ computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
+ break;
}
}
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);
%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
+}
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 *>> {