Skip to content

Commit 9ef6aa0

Browse files
committed
Plumb AssumeInst through operand bundle apis [nfc]
Follow up to a6d2a8d. This covers all the public interfaces of the bundle related code. I tried to cleanup the internals where the changes were obvious, but there's definitely more room for improvement.
1 parent 96caf38 commit 9ef6aa0

File tree

10 files changed

+84
-95
lines changed

10 files changed

+84
-95
lines changed

llvm/include/llvm/Analysis/AssumeBundleQueries.h

+9-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "llvm/IR/Attributes.h"
1818
#include "llvm/IR/Instructions.h"
19+
#include "llvm/IR/IntrinsicInst.h"
1920
#include "llvm/ADT/DenseMap.h"
2021

2122
namespace llvm {
@@ -39,12 +40,12 @@ enum AssumeBundleArg {
3940
///
4041
/// Return true iff the queried attribute was found.
4142
/// If ArgVal is set. the argument will be stored to ArgVal.
42-
bool hasAttributeInAssume(CallInst &AssumeCI, Value *IsOn, StringRef AttrName,
43+
bool hasAttributeInAssume(AssumeInst &Assume, Value *IsOn, StringRef AttrName,
4344
uint64_t *ArgVal = nullptr);
44-
inline bool hasAttributeInAssume(CallInst &AssumeCI, Value *IsOn,
45+
inline bool hasAttributeInAssume(AssumeInst &Assume, Value *IsOn,
4546
Attribute::AttrKind Kind,
4647
uint64_t *ArgVal = nullptr) {
47-
return hasAttributeInAssume(AssumeCI, IsOn,
48+
return hasAttributeInAssume(Assume, IsOn,
4849
Attribute::getNameFromAttrKind(Kind), ArgVal);
4950
}
5051

@@ -87,7 +88,7 @@ using RetainedKnowledgeMap =
8788
/// many queries are going to be made on the same llvm.assume.
8889
/// String attributes are not inserted in the map.
8990
/// If the IR changes the map will be outdated.
90-
void fillMapFromAssume(CallInst &AssumeCI, RetainedKnowledgeMap &Result);
91+
void fillMapFromAssume(AssumeInst &Assume, RetainedKnowledgeMap &Result);
9192

9293
/// Represent one information held inside an operand bundle of an llvm.assume.
9394
/// AttrKind is the property that holds.
@@ -121,13 +122,13 @@ struct RetainedKnowledge {
121122

122123
/// Retreive the information help by Assume on the operand at index Idx.
123124
/// Assume should be an llvm.assume and Idx should be in the operand bundle.
124-
RetainedKnowledge getKnowledgeFromOperandInAssume(CallInst &Assume,
125+
RetainedKnowledge getKnowledgeFromOperandInAssume(AssumeInst &Assume,
125126
unsigned Idx);
126127

127128
/// Retreive the information help by the Use U of an llvm.assume. the use should
128129
/// be in the operand bundle.
129130
inline RetainedKnowledge getKnowledgeFromUseInAssume(const Use *U) {
130-
return getKnowledgeFromOperandInAssume(*cast<CallInst>(U->getUser()),
131+
return getKnowledgeFromOperandInAssume(*cast<AssumeInst>(U->getUser()),
131132
U->getOperandNo());
132133
}
133134

@@ -142,7 +143,7 @@ constexpr StringRef IgnoreBundleTag = "ignore";
142143
///
143144
/// the argument to the call of llvm.assume may still be useful even if the
144145
/// function returned true.
145-
bool isAssumeWithEmptyBundle(CallInst &Assume);
146+
bool isAssumeWithEmptyBundle(AssumeInst &Assume);
146147

147148
/// Return a valid Knowledge associated to the Use U if its Attribute kind is
148149
/// in AttrKinds.
@@ -168,7 +169,7 @@ RetainedKnowledge getKnowledgeValidInContext(
168169

169170
/// This extracts the Knowledge from an element of an operand bundle.
170171
/// This is mostly for use in the assume builder.
171-
RetainedKnowledge getKnowledgeFromBundle(CallInst &Assume,
172+
RetainedKnowledge getKnowledgeFromBundle(AssumeInst &Assume,
172173
const CallBase::BundleOpInfo &BOI);
173174

174175
} // namespace llvm

llvm/include/llvm/Transforms/Scalar/GVN.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
namespace llvm {
3636

3737
class AAResults;
38+
class AssumeInst;
3839
class AssumptionCache;
3940
class BasicBlock;
4041
class BranchInst;
@@ -309,7 +310,7 @@ class GVN : public PassInfoMixin<GVN> {
309310
// Helper functions of redundant load elimination
310311
bool processLoad(LoadInst *L);
311312
bool processNonLocalLoad(LoadInst *L);
312-
bool processAssumeIntrinsic(IntrinsicInst *II);
313+
bool processAssumeIntrinsic(AssumeInst *II);
313314

314315
/// Given a local dependency (Def or Clobber) determine if a value is
315316
/// available for the load. Returns true if an value is known to be

llvm/include/llvm/Transforms/Utils/AssumeBundleBuilder.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class DominatorTree;
3030
/// from the given instruction.
3131
/// If no information derived from \p I, this call returns null.
3232
/// The returned instruction is not inserted anywhere.
33-
IntrinsicInst *buildAssumeFromInst(Instruction *I);
33+
AssumeInst *buildAssumeFromInst(Instruction *I);
3434

3535
/// Calls BuildAssumeFromInst and if the resulting llvm.assume is valid insert
3636
/// if before I. This is usually what need to be done to salvage the knowledge
@@ -44,10 +44,10 @@ void salvageKnowledge(Instruction *I, AssumptionCache *AC = nullptr,
4444

4545
/// Build and return a new assume created from the provided knowledge
4646
/// if the knowledge in the assume is fully redundant this will return nullptr
47-
IntrinsicInst *buildAssumeFromKnowledge(ArrayRef<RetainedKnowledge> Knowledge,
48-
Instruction *CtxI,
49-
AssumptionCache *AC = nullptr,
50-
DominatorTree *DT = nullptr);
47+
AssumeInst *buildAssumeFromKnowledge(ArrayRef<RetainedKnowledge> Knowledge,
48+
Instruction *CtxI,
49+
AssumptionCache *AC = nullptr,
50+
DominatorTree *DT = nullptr);
5151

5252
/// This pass attempts to minimize the number of assume without loosing any
5353
/// information.
@@ -66,7 +66,7 @@ struct AssumeBuilderPass : public PassInfoMixin<AssumeBuilderPass> {
6666
/// canonicalize the RetainedKnowledge RK. it is assumed that RK is part of
6767
/// Assume. This will return an empty RetainedKnowledge if the knowledge is
6868
/// useless.
69-
RetainedKnowledge simplifyRetainedKnowledge(CallBase *Assume,
69+
RetainedKnowledge simplifyRetainedKnowledge(AssumeInst *Assume,
7070
RetainedKnowledge RK,
7171
AssumptionCache *AC,
7272
DominatorTree *DT);

llvm/lib/Analysis/AssumeBundleQueries.cpp

+10-24
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,15 @@ static bool bundleHasArgument(const CallBase::BundleOpInfo &BOI, unsigned Idx) {
3333
return BOI.End - BOI.Begin > Idx;
3434
}
3535

36-
static Value *getValueFromBundleOpInfo(CallInst &Assume,
36+
static Value *getValueFromBundleOpInfo(AssumeInst &Assume,
3737
const CallBase::BundleOpInfo &BOI,
3838
unsigned Idx) {
3939
assert(bundleHasArgument(BOI, Idx) && "index out of range");
4040
return (Assume.op_begin() + BOI.Begin + Idx)->get();
4141
}
4242

43-
bool llvm::hasAttributeInAssume(CallInst &AssumeCI, Value *IsOn,
43+
bool llvm::hasAttributeInAssume(AssumeInst &Assume, Value *IsOn,
4444
StringRef AttrName, uint64_t *ArgVal) {
45-
assert(isa<IntrinsicInst>(AssumeCI) &&
46-
"this function is intended to be used on llvm.assume");
47-
IntrinsicInst &Assume = cast<IntrinsicInst>(AssumeCI);
48-
assert(Assume.getIntrinsicID() == Intrinsic::assume &&
49-
"this function is intended to be used on llvm.assume");
5045
assert(Attribute::isExistingAttribute(AttrName) &&
5146
"this attribute doesn't exist");
5247
assert((ArgVal == nullptr || Attribute::doesAttrKindHaveArgument(
@@ -72,10 +67,7 @@ bool llvm::hasAttributeInAssume(CallInst &AssumeCI, Value *IsOn,
7267
return false;
7368
}
7469

75-
void llvm::fillMapFromAssume(CallInst &AssumeCI, RetainedKnowledgeMap &Result) {
76-
IntrinsicInst &Assume = cast<IntrinsicInst>(AssumeCI);
77-
assert(Assume.getIntrinsicID() == Intrinsic::assume &&
78-
"this function is intended to be used on llvm.assume");
70+
void llvm::fillMapFromAssume(AssumeInst &Assume, RetainedKnowledgeMap &Result) {
7971
for (auto &Bundles : Assume.bundle_op_infos()) {
8072
std::pair<Value *, Attribute::AttrKind> Key{
8173
nullptr, Attribute::getAttrKindFromName(Bundles.Tag->getKey())};
@@ -104,7 +96,7 @@ void llvm::fillMapFromAssume(CallInst &AssumeCI, RetainedKnowledgeMap &Result) {
10496
}
10597

10698
RetainedKnowledge
107-
llvm::getKnowledgeFromBundle(CallInst &Assume,
99+
llvm::getKnowledgeFromBundle(AssumeInst &Assume,
108100
const CallBase::BundleOpInfo &BOI) {
109101
RetainedKnowledge Result;
110102
Result.AttrKind = Attribute::getAttrKindFromName(BOI.Tag->getKey());
@@ -124,19 +116,13 @@ llvm::getKnowledgeFromBundle(CallInst &Assume,
124116
return Result;
125117
}
126118

127-
RetainedKnowledge llvm::getKnowledgeFromOperandInAssume(CallInst &AssumeCI,
119+
RetainedKnowledge llvm::getKnowledgeFromOperandInAssume(AssumeInst &Assume,
128120
unsigned Idx) {
129-
IntrinsicInst &Assume = cast<IntrinsicInst>(AssumeCI);
130-
assert(Assume.getIntrinsicID() == Intrinsic::assume &&
131-
"this function is intended to be used on llvm.assume");
132121
CallBase::BundleOpInfo BOI = Assume.getBundleOpInfoForOperand(Idx);
133-
return getKnowledgeFromBundle(AssumeCI, BOI);
122+
return getKnowledgeFromBundle(Assume, BOI);
134123
}
135124

136-
bool llvm::isAssumeWithEmptyBundle(CallInst &CI) {
137-
IntrinsicInst &Assume = cast<IntrinsicInst>(CI);
138-
assert(Assume.getIntrinsicID() == Intrinsic::assume &&
139-
"this function is intended to be used on llvm.assume");
125+
bool llvm::isAssumeWithEmptyBundle(AssumeInst &Assume) {
140126
return none_of(Assume.bundle_op_infos(),
141127
[](const CallBase::BundleOpInfo &BOI) {
142128
return BOI.Tag->getKey() != IgnoreBundleTag;
@@ -158,7 +144,7 @@ llvm::getKnowledgeFromUse(const Use *U,
158144
if (!Bundle)
159145
return RetainedKnowledge::none();
160146
RetainedKnowledge RK =
161-
getKnowledgeFromBundle(*cast<CallInst>(U->getUser()), *Bundle);
147+
getKnowledgeFromBundle(*cast<AssumeInst>(U->getUser()), *Bundle);
162148
if (llvm::is_contained(AttrKinds, RK.AttrKind))
163149
return RK;
164150
return RetainedKnowledge::none();
@@ -176,7 +162,7 @@ llvm::getKnowledgeForValue(const Value *V,
176162
return RetainedKnowledge::none();
177163
if (AC) {
178164
for (AssumptionCache::ResultElem &Elem : AC->assumptionsFor(V)) {
179-
IntrinsicInst *II = cast_or_null<IntrinsicInst>(Elem.Assume);
165+
auto *II = cast_or_null<AssumeInst>(Elem.Assume);
180166
if (!II || Elem.Index == AssumptionCache::ExprResultIdx)
181167
continue;
182168
if (RetainedKnowledge RK = getKnowledgeFromBundle(
@@ -197,7 +183,7 @@ llvm::getKnowledgeForValue(const Value *V,
197183
if (!Bundle)
198184
continue;
199185
if (RetainedKnowledge RK =
200-
getKnowledgeFromBundle(*cast<CallInst>(U.getUser()), *Bundle))
186+
getKnowledgeFromBundle(*cast<AssumeInst>(U.getUser()), *Bundle))
201187
if (is_contained(AttrKinds, RK.AttrKind) &&
202188
Filter(RK, cast<Instruction>(U.getUser()), Bundle)) {
203189
NumUsefullAssumeQueries++;

llvm/lib/Transforms/IPO/Attributor.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -1910,9 +1910,8 @@ void InformationCache::initializeInformationCache(const Function &CF,
19101910
// Calls are interesting on their own, additionally:
19111911
// For `llvm.assume` calls we also fill the KnowledgeMap as we find them.
19121912
// For `must-tail` calls we remember the caller and callee.
1913-
if (IntrinsicInst *Assume = dyn_cast<IntrinsicInst>(&I)) {
1914-
if (Assume->getIntrinsicID() == Intrinsic::assume)
1915-
fillMapFromAssume(*Assume, KnowledgeMap);
1913+
if (auto *Assume = dyn_cast<AssumeInst>(&I)) {
1914+
fillMapFromAssume(*Assume, KnowledgeMap);
19161915
} else if (cast<CallInst>(I).isMustTailCall()) {
19171916
FI.ContainsMustTailCall = true;
19181917
if (const Function *Callee = cast<CallInst>(I).getCalledFunction())

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

+13-10
Original file line numberDiff line numberDiff line change
@@ -1557,8 +1557,8 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
15571557
/// argument and remove the assume if it becomes useless.
15581558
/// always returns nullptr for use as a return values.
15591559
auto RemoveConditionFromAssume = [&](Instruction *Assume) -> Instruction * {
1560-
assert(isa<IntrinsicInst>(Assume));
1561-
if (isAssumeWithEmptyBundle(*cast<IntrinsicInst>(II)))
1560+
assert(isa<AssumeInst>(Assume));
1561+
if (isAssumeWithEmptyBundle(*cast<AssumeInst>(II)))
15621562
return eraseInstFromFunction(CI);
15631563
replaceUse(II->getOperandUse(0), ConstantInt::getTrue(II->getContext()));
15641564
return nullptr;
@@ -1615,11 +1615,11 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
16151615
if (EnableKnowledgeRetention &&
16161616
match(IIOperand, m_Cmp(Pred, m_Value(A), m_Zero())) &&
16171617
Pred == CmpInst::ICMP_NE && A->getType()->isPointerTy()) {
1618-
if (IntrinsicInst *Replacement = buildAssumeFromKnowledge(
1618+
if (auto *Replacement = buildAssumeFromKnowledge(
16191619
{RetainedKnowledge{Attribute::NonNull, 0, A}}, Next, &AC, &DT)) {
16201620

16211621
Replacement->insertBefore(Next);
1622-
AC.registerAssumption(cast<AssumeInst>(Replacement));
1622+
AC.registerAssumption(Replacement);
16231623
return RemoveConditionFromAssume(II);
16241624
}
16251625
}
@@ -1647,11 +1647,11 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
16471647
/// the offset.
16481648
RetainedKnowledge RK{Attribute::Alignment,
16491649
(unsigned)MinAlign(Offset, AlignMask + 1), A};
1650-
if (IntrinsicInst *Replacement =
1650+
if (auto *Replacement =
16511651
buildAssumeFromKnowledge(RK, Next, &AC, &DT)) {
16521652

16531653
Replacement->insertAfter(II);
1654-
AC.registerAssumption(cast<AssumeInst>(Replacement));
1654+
AC.registerAssumption(Replacement);
16551655
}
16561656
return RemoveConditionFromAssume(II);
16571657
}
@@ -1662,13 +1662,16 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
16621662
if (EnableKnowledgeRetention && II->hasOperandBundles()) {
16631663
for (unsigned Idx = 0; Idx < II->getNumOperandBundles(); Idx++) {
16641664
auto &BOI = II->bundle_op_info_begin()[Idx];
1665-
RetainedKnowledge RK = llvm::getKnowledgeFromBundle(*II, BOI);
1665+
RetainedKnowledge RK =
1666+
llvm::getKnowledgeFromBundle(cast<AssumeInst>(*II), BOI);
16661667
if (BOI.End - BOI.Begin > 2)
16671668
continue; // Prevent reducing knowledge in an align with offset since
16681669
// extracting a RetainedKnowledge form them looses offset
16691670
// information
1670-
RetainedKnowledge CanonRK = llvm::simplifyRetainedKnowledge(
1671-
II, RK, &getAssumptionCache(), &getDominatorTree());
1671+
RetainedKnowledge CanonRK =
1672+
llvm::simplifyRetainedKnowledge(cast<AssumeInst>(II), RK,
1673+
&getAssumptionCache(),
1674+
&getDominatorTree());
16721675
if (CanonRK == RK)
16731676
continue;
16741677
if (!CanonRK) {
@@ -1694,7 +1697,7 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
16941697
// then this one is redundant, and should be removed.
16951698
KnownBits Known(1);
16961699
computeKnownBits(IIOperand, Known, 0, II);
1697-
if (Known.isAllOnes() && isAssumeWithEmptyBundle(*II))
1700+
if (Known.isAllOnes() && isAssumeWithEmptyBundle(cast<AssumeInst>(*II)))
16981701
return eraseInstFromFunction(*II);
16991702

17001703
// Update the cache of affected values for this assumption (we might be

llvm/lib/Transforms/Scalar/GVN.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -1602,9 +1602,7 @@ static bool hasUsersIn(Value *V, BasicBlock *BB) {
16021602
return false;
16031603
}
16041604

1605-
bool GVN::processAssumeIntrinsic(IntrinsicInst *IntrinsicI) {
1606-
assert(IntrinsicI->getIntrinsicID() == Intrinsic::assume &&
1607-
"This function can only be called with llvm.assume intrinsic");
1605+
bool GVN::processAssumeIntrinsic(AssumeInst *IntrinsicI) {
16081606
Value *V = IntrinsicI->getArgOperand(0);
16091607

16101608
if (ConstantInt *Cond = dyn_cast<ConstantInt>(V)) {
@@ -2185,9 +2183,8 @@ bool GVN::processInstruction(Instruction *I) {
21852183
}
21862184
}
21872185

2188-
if (IntrinsicInst *IntrinsicI = dyn_cast<IntrinsicInst>(I))
2189-
if (IntrinsicI->getIntrinsicID() == Intrinsic::assume)
2190-
return processAssumeIntrinsic(IntrinsicI);
2186+
if (auto *Assume = dyn_cast<AssumeInst>(I))
2187+
return processAssumeIntrinsic(Assume);
21912188

21922189
if (LoadInst *Load = dyn_cast<LoadInst>(I)) {
21932190
if (processLoad(Load))

0 commit comments

Comments
 (0)