[llvm] 481b8f3 - [Attributor][NFC] Introduce helper struct
Johannes Doerfert via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 9 03:03:44 PDT 2022
Author: Johannes Doerfert
Date: 2022-06-09T12:00:26+02:00
New Revision: 481b8f31dff4101f37c982af9926f34adb6fffc1
URL: https://github.com/llvm/llvm-project/commit/481b8f31dff4101f37c982af9926f34adb6fffc1
DIFF: https://github.com/llvm/llvm-project/commit/481b8f31dff4101f37c982af9926f34adb6fffc1.diff
LOG: [Attributor][NFC] Introduce helper struct
We often use a context associated with a value. For now only one use
case has been changed.
Added:
Modified:
llvm/include/llvm/Transforms/IPO/Attributor.h
llvm/lib/Transforms/IPO/Attributor.cpp
llvm/lib/Transforms/IPO/AttributorAttributes.cpp
llvm/lib/Transforms/IPO/OpenMPOpt.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Transforms/IPO/Attributor.h b/llvm/include/llvm/Transforms/IPO/Attributor.h
index b786554aa4862..2fe0b25dec77c 100644
--- a/llvm/include/llvm/Transforms/IPO/Attributor.h
+++ b/llvm/include/llvm/Transforms/IPO/Attributor.h
@@ -155,6 +155,16 @@ enum ValueScope : uint8_t {
Interprocedural = 2,
};
+struct ValueAndContext : public std::pair<Value *, const Instruction *> {
+ using Base = std::pair<Value *, const Instruction *>;
+ ValueAndContext(const Base &B) : Base(B) {}
+ ValueAndContext(Value &V, const Instruction *CtxI) : Base(&V, CtxI) {}
+ ValueAndContext(Value &V, const Instruction &CtxI) : Base(&V, &CtxI) {}
+
+ Value *getValue() const { return this->first; }
+ const Instruction *getCtxI() const { return this->second; }
+};
+
/// Return true if \p I is a `nosync` instruction. Use generic reasoning and
/// potentially the corresponding AANoSync.
bool isNoSyncInst(Attributor &A, const Instruction &I,
@@ -172,11 +182,10 @@ bool isDynamicallyUnique(Attributor &A, const AbstractAttribute &QueryingAA,
/// instruction/argument of \p Scope.
bool isValidInScope(const Value &V, const Function *Scope);
-/// Return true if \p V is a valid value at position \p CtxI, that is a
-/// constant, an argument of the same function as \p CtxI, or an instruction in
-/// that function that dominates \p CtxI.
-bool isValidAtPosition(const Value &V, const Instruction &CtxI,
- InformationCache &InfoCache);
+/// Return true if the value of \p VAC is a valid at the position of \p VAC,
+/// that is a constant, an argument of the same function, or an instruction in
+/// that function that dominates the position.
+bool isValidAtPosition(const ValueAndContext &VAC, InformationCache &InfoCache);
/// Try to convert \p V to type \p Ty without introducing new instructions. If
/// this is not possible return `nullptr`. Note: this function basically knows
@@ -274,6 +283,26 @@ bool isPotentiallyReachable(
} // namespace AA
+template <>
+struct DenseMapInfo<AA::ValueAndContext>
+ : public DenseMapInfo<AA::ValueAndContext::Base> {
+ using Base = DenseMapInfo<AA::ValueAndContext::Base>;
+ static inline AA::ValueAndContext getEmptyKey() {
+ return Base::getEmptyKey();
+ }
+ static inline AA::ValueAndContext getTombstoneKey() {
+ return Base::getTombstoneKey();
+ }
+ static unsigned getHashValue(const AA::ValueAndContext &VAC) {
+ return Base::getHashValue(VAC);
+ }
+
+ static bool isEqual(const AA::ValueAndContext &LHS,
+ const AA::ValueAndContext &RHS) {
+ return Base::isEqual(LHS, RHS);
+ }
+};
+
/// The value passed to the line option that defines the maximal initialization
/// chain length.
extern unsigned MaxInitializationChainLength;
diff --git a/llvm/lib/Transforms/IPO/Attributor.cpp b/llvm/lib/Transforms/IPO/Attributor.cpp
index 1993c7bacdded..70489465766c2 100644
--- a/llvm/lib/Transforms/IPO/Attributor.cpp
+++ b/llvm/lib/Transforms/IPO/Attributor.cpp
@@ -242,24 +242,27 @@ bool AA::isValidInScope(const Value &V, const Function *Scope) {
return false;
}
-bool AA::isValidAtPosition(const Value &V, const Instruction &CtxI,
+bool AA::isValidAtPosition(const AA::ValueAndContext &VAC,
InformationCache &InfoCache) {
- if (isa<Constant>(V) || &V == &CtxI)
+ if (isa<Constant>(VAC.getValue()) || VAC.getValue() == VAC.getCtxI())
return true;
- const Function *Scope = CtxI.getFunction();
- if (auto *A = dyn_cast<Argument>(&V))
+ const Function *Scope = nullptr;
+ const Instruction *CtxI = VAC.getCtxI();
+ if (CtxI)
+ Scope = CtxI->getFunction();
+ if (auto *A = dyn_cast<Argument>(VAC.getValue()))
return A->getParent() == Scope;
- if (auto *I = dyn_cast<Instruction>(&V)) {
+ if (auto *I = dyn_cast<Instruction>(VAC.getValue())) {
if (I->getFunction() == Scope) {
if (const DominatorTree *DT =
InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(
*Scope))
- return DT->dominates(I, &CtxI);
+ return DT->dominates(I, CtxI);
// Local dominance check mostly for the old PM passes.
- if (I->getParent() == CtxI.getParent())
+ if (CtxI && I->getParent() == CtxI->getParent())
return llvm::any_of(
make_range(I->getIterator(), I->getParent()->end()),
- [&](const Instruction &AfterI) { return &AfterI == &CtxI; });
+ [&](const Instruction &AfterI) { return &AfterI == CtxI; });
}
}
return false;
diff --git a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp
index f544616f30a16..c7ca6b2aaebe2 100644
--- a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp
+++ b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp
@@ -5481,7 +5481,8 @@ struct AAValueSimplifyImpl : AAValueSimplify {
if (auto *C = dyn_cast<Constant>(EffectiveV))
if (!C->canTrap())
return C;
- if (CtxI && AA::isValidAtPosition(*EffectiveV, *CtxI, A.getInfoCache()))
+ if (CtxI && AA::isValidAtPosition(AA::ValueAndContext(*EffectiveV, *CtxI),
+ A.getInfoCache()))
return ensureType(A, *EffectiveV, Ty, CtxI, Check);
if (auto *I = dyn_cast<Instruction>(EffectiveV))
if (Value *NewV = reproduceInst(A, QueryingAA, *I, Ty, CtxI, Check, VMap))
diff --git a/llvm/lib/Transforms/IPO/OpenMPOpt.cpp b/llvm/lib/Transforms/IPO/OpenMPOpt.cpp
index d913214b797e5..5c457ba83f898 100644
--- a/llvm/lib/Transforms/IPO/OpenMPOpt.cpp
+++ b/llvm/lib/Transforms/IPO/OpenMPOpt.cpp
@@ -2473,7 +2473,8 @@ struct AAICVTrackerFunction : public AAICVTracker {
if (ICVTrackingAA.isAssumedTracked()) {
Optional<Value *> URV = ICVTrackingAA.getUniqueReplacementValue(ICV);
- if (!URV || (*URV && AA::isValidAtPosition(**URV, I, OMPInfoCache)))
+ if (!URV || (*URV && AA::isValidAtPosition(AA::ValueAndContext(**URV, I),
+ OMPInfoCache)))
return URV;
}
More information about the llvm-commits
mailing list