Skip to content

Commit a6d2a8d

Browse files
committed
Add a subclass of IntrinsicInst for llvm.assume [nfc]
Add the subclass, update a few places which check for the intrinsic to use idiomatic dyn_cast, and update the public interface of AssumptionCache to use the new class. A follow up change will do the same for the newer assumption query/bundle mechanisms.
1 parent 3b1b1d7 commit a6d2a8d

17 files changed

+50
-50
lines changed

llvm/include/llvm/Analysis/AssumptionCache.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
namespace llvm {
2828

29-
class CallInst;
29+
class AssumeInst;
3030
class Function;
3131
class raw_ostream;
3232
class Value;
@@ -116,15 +116,15 @@ class AssumptionCache {
116116
///
117117
/// The call passed in must be an instruction within this function and must
118118
/// not already be in the cache.
119-
void registerAssumption(CallInst *CI);
119+
void registerAssumption(AssumeInst *CI);
120120

121121
/// Remove an \@llvm.assume intrinsic from this function's cache if it has
122122
/// been added to the cache earlier.
123-
void unregisterAssumption(CallInst *CI);
123+
void unregisterAssumption(AssumeInst *CI);
124124

125125
/// Update the cache of values being affected by this assumption (i.e.
126126
/// the values about which this assumption provides information).
127-
void updateAffectedValues(CallInst *CI);
127+
void updateAffectedValues(AssumeInst *CI);
128128

129129
/// Clear the cache of \@llvm.assume intrinsics for a function.
130130
///

llvm/include/llvm/IR/IntrinsicInst.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,18 @@ class GCResultInst : public GCProjectionInst {
12171217
}
12181218
};
12191219

1220+
1221+
/// This represents the llvm.assume intrinsic.
1222+
class AssumeInst : public IntrinsicInst {
1223+
public:
1224+
static bool classof(const IntrinsicInst *I) {
1225+
return I->getIntrinsicID() == Intrinsic::assume;
1226+
}
1227+
static bool classof(const Value *V) {
1228+
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1229+
}
1230+
};
1231+
12201232
} // end namespace llvm
12211233

12221234
#endif // LLVM_IR_INTRINSICINST_H

llvm/lib/Analysis/AssumptionCache.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ AssumptionCache::getOrInsertAffectedValues(Value *V) {
5656
}
5757

5858
static void
59-
findAffectedValues(CallInst *CI,
59+
findAffectedValues(CallBase *CI,
6060
SmallVectorImpl<AssumptionCache::ResultElem> &Affected) {
6161
// Note: This code must be kept in-sync with the code in
6262
// computeKnownBitsFromAssume in ValueTracking.
@@ -126,7 +126,7 @@ findAffectedValues(CallInst *CI,
126126
}
127127
}
128128

129-
void AssumptionCache::updateAffectedValues(CallInst *CI) {
129+
void AssumptionCache::updateAffectedValues(AssumeInst *CI) {
130130
SmallVector<AssumptionCache::ResultElem, 16> Affected;
131131
findAffectedValues(CI, Affected);
132132

@@ -139,7 +139,7 @@ void AssumptionCache::updateAffectedValues(CallInst *CI) {
139139
}
140140
}
141141

142-
void AssumptionCache::unregisterAssumption(CallInst *CI) {
142+
void AssumptionCache::unregisterAssumption(AssumeInst *CI) {
143143
SmallVector<AssumptionCache::ResultElem, 16> Affected;
144144
findAffectedValues(CI, Affected);
145145

@@ -211,13 +211,10 @@ void AssumptionCache::scanFunction() {
211211

212212
// Update affected values.
213213
for (auto &A : AssumeHandles)
214-
updateAffectedValues(cast<CallInst>(A));
214+
updateAffectedValues(cast<AssumeInst>(A));
215215
}
216216

217-
void AssumptionCache::registerAssumption(CallInst *CI) {
218-
assert(match(CI, m_Intrinsic<Intrinsic::assume>()) &&
219-
"Registered call does not call @llvm.assume");
220-
217+
void AssumptionCache::registerAssumption(AssumeInst *CI) {
221218
// If we haven't scanned the function yet, just drop this assumption. It will
222219
// be found when we scan later.
223220
if (!Scanned)

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,7 +1619,7 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
16191619
{RetainedKnowledge{Attribute::NonNull, 0, A}}, Next, &AC, &DT)) {
16201620

16211621
Replacement->insertBefore(Next);
1622-
AC.registerAssumption(Replacement);
1622+
AC.registerAssumption(cast<AssumeInst>(Replacement));
16231623
return RemoveConditionFromAssume(II);
16241624
}
16251625
}
@@ -1651,7 +1651,7 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
16511651
buildAssumeFromKnowledge(RK, Next, &AC, &DT)) {
16521652

16531653
Replacement->insertAfter(II);
1654-
AC.registerAssumption(Replacement);
1654+
AC.registerAssumption(cast<AssumeInst>(Replacement));
16551655
}
16561656
return RemoveConditionFromAssume(II);
16571657
}
@@ -1699,7 +1699,7 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
16991699

17001700
// Update the cache of affected values for this assumption (we might be
17011701
// here because we just simplified the condition).
1702-
AC.updateAffectedValues(II);
1702+
AC.updateAffectedValues(cast<AssumeInst>(II));
17031703
break;
17041704
}
17051705
case Intrinsic::experimental_guard: {

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3983,7 +3983,7 @@ static bool combineInstructionsOverFunction(
39833983
IRBuilderCallbackInserter([&Worklist, &AC](Instruction *I) {
39843984
Worklist.add(I);
39853985
if (match(I, m_Intrinsic<Intrinsic::assume>()))
3986-
AC.registerAssumption(cast<CallInst>(I));
3986+
AC.registerAssumption(cast<AssumeInst>(I));
39873987
}));
39883988

39893989
// Lower dbg.declare intrinsics otherwise their value may be clobbered

llvm/lib/Transforms/Scalar/LoopUnswitch.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,9 +1436,8 @@ void LoopUnswitch::unswitchNontrivialCondition(
14361436
for (Instruction &I : *NewBlocks[NBI]) {
14371437
RemapInstruction(&I, VMap,
14381438
RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
1439-
if (auto *II = dyn_cast<IntrinsicInst>(&I))
1440-
if (II->getIntrinsicID() == Intrinsic::assume)
1441-
AC->registerAssumption(II);
1439+
if (auto *II = dyn_cast<AssumeInst>(&I))
1440+
AC->registerAssumption(II);
14421441
}
14431442
}
14441443

llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,9 +1113,8 @@ static BasicBlock *buildClonedLoopBlocks(
11131113
for (Instruction &I : *ClonedBB) {
11141114
RemapInstruction(&I, VMap,
11151115
RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
1116-
if (auto *II = dyn_cast<IntrinsicInst>(&I))
1117-
if (II->getIntrinsicID() == Intrinsic::assume)
1118-
AC.registerAssumption(II);
1116+
if (auto *II = dyn_cast<AssumeInst>(&I))
1117+
AC.registerAssumption(II);
11191118
}
11201119

11211120
// Update any PHI nodes in the cloned successors of the skipped blocks to not

llvm/lib/Transforms/Utils/AssumeBundleBuilder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ void llvm::salvageKnowledge(Instruction *I, AssumptionCache *AC,
297297
if (IntrinsicInst *Intr = Builder.build()) {
298298
Intr->insertBefore(I);
299299
if (AC)
300-
AC->registerAssumption(Intr);
300+
AC->registerAssumption(cast<AssumeInst>(Intr));
301301
}
302302
}
303303

@@ -499,7 +499,7 @@ struct AssumeSimplify {
499499
return;
500500
MadeChange = true;
501501
MergedAssume->insertBefore(InsertPt);
502-
AC.registerAssumption(MergedAssume);
502+
AC.registerAssumption(cast<AssumeInst>(MergedAssume));
503503
}
504504

505505
/// Merge assume when they are in the same BasicBlock and for all instruction

llvm/lib/Transforms/Utils/CodeExtractor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,10 +1593,10 @@ CodeExtractor::extractCodeRegion(const CodeExtractorAnalysisCache &CEAC) {
15931593
Instruction *I = &*It;
15941594
++It;
15951595

1596-
if (match(I, m_Intrinsic<Intrinsic::assume>())) {
1596+
if (auto *AI = dyn_cast<AssumeInst>(I)) {
15971597
if (AC)
1598-
AC->unregisterAssumption(cast<CallInst>(I));
1599-
I->eraseFromParent();
1598+
AC->unregisterAssumption(AI);
1599+
AI->eraseFromParent();
16001600
}
16011601
}
16021602
}

llvm/lib/Transforms/Utils/InlineFunction.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,7 @@ static void AddAlignmentAssumptions(CallBase &CB, InlineFunctionInfo &IFI) {
12961296

12971297
CallInst *NewAsmp =
12981298
IRBuilder<>(&CB).CreateAlignmentAssumption(DL, ArgVal, Align);
1299-
AC->registerAssumption(NewAsmp);
1299+
AC->registerAssumption(cast<AssumeInst>(NewAsmp));
13001300
}
13011301
}
13021302
}
@@ -2050,9 +2050,8 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
20502050
for (BasicBlock &NewBlock :
20512051
make_range(FirstNewBlock->getIterator(), Caller->end()))
20522052
for (Instruction &I : NewBlock)
2053-
if (auto *II = dyn_cast<IntrinsicInst>(&I))
2054-
if (II->getIntrinsicID() == Intrinsic::assume)
2055-
IFI.GetAssumptionCache(*Caller).registerAssumption(II);
2053+
if (auto *II = dyn_cast<AssumeInst>(&I))
2054+
IFI.GetAssumptionCache(*Caller).registerAssumption(II);
20562055
}
20572056

20582057
// If there are any alloca instructions in the block that used to be the entry

llvm/lib/Transforms/Utils/LoopRotationUtils.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,8 @@ bool LoopRotate::rotateLoop(Loop *L, bool SimplifiedLatch) {
467467
C->setName(Inst->getName());
468468
C->insertBefore(LoopEntryBranch);
469469

470-
if (auto *II = dyn_cast<IntrinsicInst>(C))
471-
if (II->getIntrinsicID() == Intrinsic::assume)
472-
AC->registerAssumption(II);
470+
if (auto *II = dyn_cast<AssumeInst>(C))
471+
AC->registerAssumption(II);
473472
// MemorySSA cares whether the cloned instruction was inserted or not, and
474473
// not whether it can be remapped to a simplified value.
475474
if (MSSAU)

llvm/lib/Transforms/Utils/LoopUnroll.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -681,13 +681,10 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
681681

682682
// Remap all instructions in the most recent iteration
683683
remapInstructionsInBlocks(NewBlocks, LastValueMap);
684-
for (BasicBlock *NewBlock : NewBlocks) {
685-
for (Instruction &I : *NewBlock) {
686-
if (auto *II = dyn_cast<IntrinsicInst>(&I))
687-
if (II->getIntrinsicID() == Intrinsic::assume)
688-
AC->registerAssumption(II);
689-
}
690-
}
684+
for (BasicBlock *NewBlock : NewBlocks)
685+
for (Instruction &I : *NewBlock)
686+
if (auto *II = dyn_cast<AssumeInst>(&I))
687+
AC->registerAssumption(II);
691688

692689
{
693690
// Identify what other metadata depends on the cloned version. After

llvm/lib/Transforms/Utils/LoopUnrollAndJam.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,9 +432,8 @@ llvm::UnrollAndJamLoop(Loop *L, unsigned Count, unsigned TripCount,
432432
remapInstructionsInBlocks(NewBlocks, LastValueMap);
433433
for (BasicBlock *NewBlock : NewBlocks) {
434434
for (Instruction &I : *NewBlock) {
435-
if (auto *II = dyn_cast<IntrinsicInst>(&I))
436-
if (II->getIntrinsicID() == Intrinsic::assume)
437-
AC->registerAssumption(II);
435+
if (auto *II = dyn_cast<AssumeInst>(&I))
436+
AC->registerAssumption(II);
438437
}
439438
}
440439

llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ static void addAssumeNonNull(AssumptionCache *AC, LoadInst *LI) {
306306
LoadNotNull->insertAfter(LI);
307307
CallInst *CI = CallInst::Create(AssumeIntrinsic, {LoadNotNull});
308308
CI->insertAfter(LoadNotNull);
309-
AC->registerAssumption(CI);
309+
AC->registerAssumption(cast<AssumeInst>(CI));
310310
}
311311

312312
static void removeIntrinsicUsers(AllocaInst *AI) {

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2530,7 +2530,7 @@ static bool FoldCondBranchOnPHI(BranchInst *BI, DomTreeUpdater *DTU,
25302530

25312531
// Register the new instruction with the assumption cache if necessary.
25322532
if (AC && match(N, m_Intrinsic<Intrinsic::assume>()))
2533-
AC->registerAssumption(cast<IntrinsicInst>(N));
2533+
AC->registerAssumption(cast<AssumeInst>(N));
25342534
}
25352535
}
25362536

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3022,9 +3022,8 @@ void InnerLoopVectorizer::scalarizeInstruction(Instruction *Instr, VPValue *Def,
30223022
State.set(Def, Cloned, Instance);
30233023

30243024
// If we just cloned a new assumption, add it the assumption cache.
3025-
if (auto *II = dyn_cast<IntrinsicInst>(Cloned))
3026-
if (II->getIntrinsicID() == Intrinsic::assume)
3027-
AC->registerAssumption(II);
3025+
if (auto *II = dyn_cast<AssumeInst>(Cloned))
3026+
AC->registerAssumption(II);
30283027

30293028
// End if-block.
30303029
if (IfPredicateInstr)

llvm/unittests/Analysis/AssumeBundleQueriesTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ TEST(AssumeQueryAPI, AssumptionCache) {
542542
ASSERT_EQ(AR.size(), 1u);
543543
ASSERT_EQ(AR[0].Assume, &*Second);
544544
ASSERT_EQ(AR[0].Index, AssumptionCache::ExprResultIdx);
545-
AC.unregisterAssumption(cast<CallInst>(&*Second));
545+
AC.unregisterAssumption(cast<AssumeInst>(&*Second));
546546
AR = AC.assumptionsFor(F->getArg(1));
547547
ASSERT_EQ(AR.size(), 0u);
548548
AR = AC.assumptionsFor(F->getArg(0));

0 commit comments

Comments
 (0)