Skip to content

Commit 56f5690

Browse files
committed
[ValueTracking] Handle operand bundle assumes in same loop (NFCI)
We are already iterating over all assumes in AC, so handle operand bundle based assumes in the same loop, instead of querying them separately. To keep the debug counter working, make it work per-bundle rather than per-value.
1 parent a243128 commit 56f5690

File tree

2 files changed

+40
-33
lines changed

2 files changed

+40
-33
lines changed

llvm/lib/Analysis/AssumeBundleQueries.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ RetainedKnowledge
9999
llvm::getKnowledgeFromBundle(AssumeInst &Assume,
100100
const CallBase::BundleOpInfo &BOI) {
101101
RetainedKnowledge Result;
102+
if (!DebugCounter::shouldExecute(AssumeQueryCounter))
103+
return Result;
104+
102105
Result.AttrKind = Attribute::getAttrKindFromName(BOI.Tag->getKey());
103106
if (bundleHasArgument(BOI, ABA_WasOn))
104107
Result.WasOn = getValueFromBundleOpInfo(Assume, BOI, ABA_WasOn);
@@ -158,8 +161,6 @@ llvm::getKnowledgeForValue(const Value *V,
158161
const CallBase::BundleOpInfo *)>
159162
Filter) {
160163
NumAssumeQueries++;
161-
if (!DebugCounter::shouldExecute(AssumeQueryCounter))
162-
return RetainedKnowledge::none();
163164
if (AC) {
164165
for (AssumptionCache::ResultElem &Elem : AC->assumptionsFor(V)) {
165166
auto *II = cast_or_null<AssumeInst>(Elem.Assume);

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -589,30 +589,34 @@ static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
589589
if (!Q.AC || !Q.CxtI)
590590
return false;
591591

592-
if (Q.CxtI && V->getType()->isPointerTy()) {
593-
SmallVector<Attribute::AttrKind, 2> AttrKinds{Attribute::NonNull};
594-
if (!NullPointerIsDefined(Q.CxtI->getFunction(),
595-
V->getType()->getPointerAddressSpace()))
596-
AttrKinds.push_back(Attribute::Dereferenceable);
597-
598-
if (getKnowledgeValidInContext(V, AttrKinds, Q.CxtI, Q.DT, Q.AC))
599-
return true;
600-
}
601-
602-
for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
603-
if (!AssumeVH)
592+
for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
593+
if (!Elem.Assume)
604594
continue;
605-
CallInst *I = cast<CallInst>(AssumeVH);
595+
596+
AssumeInst *I = cast<AssumeInst>(Elem.Assume);
606597
assert(I->getFunction() == Q.CxtI->getFunction() &&
607598
"Got assumption for the wrong function!");
608599

600+
if (Elem.Index != AssumptionCache::ExprResultIdx) {
601+
if (!V->getType()->isPointerTy())
602+
continue;
603+
if (RetainedKnowledge RK = getKnowledgeFromBundle(
604+
*I, I->bundle_op_info_begin()[Elem.Index])) {
605+
if (RK.WasOn == V &&
606+
(RK.AttrKind == Attribute::NonNull ||
607+
(RK.AttrKind == Attribute::Dereferenceable &&
608+
!NullPointerIsDefined(Q.CxtI->getFunction(),
609+
V->getType()->getPointerAddressSpace()))) &&
610+
isValidAssumeForContext(I, Q.CxtI, Q.DT))
611+
return true;
612+
}
613+
continue;
614+
}
615+
609616
// Warning: This loop can end up being somewhat performance sensitive.
610617
// We're running this loop for once for each value queried resulting in a
611618
// runtime of ~O(#assumes * #values).
612619

613-
assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume &&
614-
"must be an assume intrinsic");
615-
616620
Value *RHS;
617621
CmpInst::Predicate Pred;
618622
auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
@@ -741,32 +745,34 @@ void llvm::computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
741745

742746
unsigned BitWidth = Known.getBitWidth();
743747

744-
// Refine Known set if the pointer alignment is set by assume bundles.
745-
if (V->getType()->isPointerTy()) {
746-
if (RetainedKnowledge RK = getKnowledgeValidInContext(
747-
V, { Attribute::Alignment }, Q.CxtI, Q.DT, Q.AC)) {
748-
if (isPowerOf2_64(RK.ArgValue))
749-
Known.Zero.setLowBits(Log2_64(RK.ArgValue));
750-
}
751-
}
752-
753748
// Note that the patterns below need to be kept in sync with the code
754749
// in AssumptionCache::updateAffectedValues.
755750

756-
for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
757-
if (!AssumeVH)
751+
for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
752+
if (!Elem.Assume)
758753
continue;
759-
CallInst *I = cast<CallInst>(AssumeVH);
754+
755+
AssumeInst *I = cast<AssumeInst>(Elem.Assume);
760756
assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
761757
"Got assumption for the wrong function!");
762758

759+
if (Elem.Index != AssumptionCache::ExprResultIdx) {
760+
if (!V->getType()->isPointerTy())
761+
continue;
762+
if (RetainedKnowledge RK = getKnowledgeFromBundle(
763+
*I, I->bundle_op_info_begin()[Elem.Index])) {
764+
if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&
765+
isPowerOf2_64(RK.ArgValue) &&
766+
isValidAssumeForContext(I, Q.CxtI, Q.DT))
767+
Known.Zero.setLowBits(Log2_64(RK.ArgValue));
768+
}
769+
continue;
770+
}
771+
763772
// Warning: This loop can end up being somewhat performance sensitive.
764773
// We're running this loop for once for each value queried resulting in a
765774
// runtime of ~O(#assumes * #values).
766775

767-
assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume &&
768-
"must be an assume intrinsic");
769-
770776
Value *Arg = I->getArgOperand(0);
771777

772778
if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {

0 commit comments

Comments
 (0)