Skip to content

Commit 10b44fb

Browse files
committed
[InstCombine] Add KnownBits consistency assertion behind option (NFC)
I'm occasionally using this to find cases where computeKnownBits() and SimplifyDemandedBits() went out of sync. This option is not enabled by default (even under EXPENSIVE_CHECKS) because it has a number of known failures in our tests. The reason for this failures is that computeKnownBits() performs recursive queries using the original context instruction, while SimplifyDemandedBits() uses the current instruction. This is something we can improve, but using the original context wouldn't always be safe in this context (when non-speculatable instructions are involved).
1 parent cd02e4b commit 10b44fb

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ using namespace llvm::PatternMatch;
2424

2525
#define DEBUG_TYPE "instcombine"
2626

27+
static cl::opt<bool>
28+
VerifyKnownBits("instcombine-verify-known-bits",
29+
cl::desc("Verify that computeKnownBits() and "
30+
"SimplifyDemandedBits() are consistent"),
31+
cl::Hidden, cl::init(false));
32+
2733
/// Check to see if the specified operand of the specified instruction is a
2834
/// constant integer. If so, check to see if there are any bits set in the
2935
/// constant that are not demanded. If so, shrink the constant and return true.
@@ -1033,6 +1039,18 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
10331039
// TODO: We could return `(inttoptr const)` for pointers.
10341040
if (!V->getType()->isPointerTy() && DemandedMask.isSubsetOf(Known.Zero | Known.One))
10351041
return Constant::getIntegerValue(VTy, Known.One);
1042+
1043+
if (VerifyKnownBits) {
1044+
KnownBits ReferenceKnown = computeKnownBits(V, Depth, CxtI);
1045+
if (Known != ReferenceKnown) {
1046+
errs() << "Mismatched known bits for " << *V << " in "
1047+
<< I->getFunction()->getName() << "\n";
1048+
errs() << "computeKnownBits(): " << ReferenceKnown << "\n";
1049+
errs() << "SimplifyDemandedBits(): " << Known << "\n";
1050+
std::abort();
1051+
}
1052+
}
1053+
10361054
return nullptr;
10371055
}
10381056

0 commit comments

Comments
 (0)