From f2da9410a99e89409754da48bdfce52290e85bc0 Mon Sep 17 00:00:00 2001 From: Matt Arsenault Date: Fri, 9 May 2025 08:16:18 +0200 Subject: [PATCH] Analysis: Remove no-AssumptionCache path in getKnowledgeForValue As requested in https://github.com/llvm/llvm-project/pull/138961#discussion_r2078483175 --- .../llvm/Analysis/AssumeBundleQueries.h | 13 +++--- llvm/lib/Analysis/AssumeBundleQueries.cpp | 42 ++++++------------- llvm/lib/Analysis/Loads.cpp | 4 +- llvm/lib/Analysis/ValueTracking.cpp | 2 +- .../Transforms/Utils/AssumeBundleBuilder.cpp | 4 +- 5 files changed, 24 insertions(+), 41 deletions(-) diff --git a/llvm/include/llvm/Analysis/AssumeBundleQueries.h b/llvm/include/llvm/Analysis/AssumeBundleQueries.h index f7a893708758c..98e9207e4435a 100644 --- a/llvm/include/llvm/Analysis/AssumeBundleQueries.h +++ b/llvm/include/llvm/Analysis/AssumeBundleQueries.h @@ -152,18 +152,19 @@ RetainedKnowledge getKnowledgeFromUse(const Use *U, /// in AttrKinds and it matches the Filter. RetainedKnowledge getKnowledgeForValue( const Value *V, ArrayRef AttrKinds, - AssumptionCache *AC = nullptr, + AssumptionCache &AC, function_ref + const CallBase::BundleOpInfo *)> Filter = [](auto...) { return true; }); /// Return a valid Knowledge associated to the Value V if its Attribute kind is /// in AttrKinds and the knowledge is suitable to be used in the context of /// CtxI. -RetainedKnowledge getKnowledgeValidInContext( - const Value *V, ArrayRef AttrKinds, - const Instruction *CtxI, const DominatorTree *DT = nullptr, - AssumptionCache *AC = nullptr); +RetainedKnowledge +getKnowledgeValidInContext(const Value *V, + ArrayRef AttrKinds, + AssumptionCache &AC, const Instruction *CtxI, + const DominatorTree *DT = nullptr); /// This extracts the Knowledge from an element of an operand bundle. /// This is mostly for use in the assume builder. diff --git a/llvm/lib/Analysis/AssumeBundleQueries.cpp b/llvm/lib/Analysis/AssumeBundleQueries.cpp index b37b2270bbec5..4c890a5d21b54 100644 --- a/llvm/lib/Analysis/AssumeBundleQueries.cpp +++ b/llvm/lib/Analysis/AssumeBundleQueries.cpp @@ -157,51 +157,33 @@ llvm::getKnowledgeFromUse(const Use *U, RetainedKnowledge llvm::getKnowledgeForValue(const Value *V, ArrayRef AttrKinds, - AssumptionCache *AC, + AssumptionCache &AC, function_ref Filter) { NumAssumeQueries++; - if (AC) { - for (AssumptionCache::ResultElem &Elem : AC->assumptionsFor(V)) { - auto *II = cast_or_null(Elem.Assume); - if (!II || Elem.Index == AssumptionCache::ExprResultIdx) - continue; - if (RetainedKnowledge RK = getKnowledgeFromBundle( - *II, II->bundle_op_info_begin()[Elem.Index])) { - if (V != RK.WasOn) - continue; - if (is_contained(AttrKinds, RK.AttrKind) && - Filter(RK, II, &II->bundle_op_info_begin()[Elem.Index])) { - NumUsefullAssumeQueries++; - return RK; - } - } - } - return RetainedKnowledge::none(); - } - - if (!V->hasUseList()) - return RetainedKnowledge::none(); - - for (const auto &U : V->uses()) { - CallInst::BundleOpInfo* Bundle = getBundleFromUse(&U); - if (!Bundle) + for (AssumptionCache::ResultElem &Elem : AC.assumptionsFor(V)) { + auto *II = cast_or_null(Elem.Assume); + if (!II || Elem.Index == AssumptionCache::ExprResultIdx) continue; - if (RetainedKnowledge RK = - getKnowledgeFromBundle(*cast(U.getUser()), *Bundle)) + if (RetainedKnowledge RK = getKnowledgeFromBundle( + *II, II->bundle_op_info_begin()[Elem.Index])) { + if (V != RK.WasOn) + continue; if (is_contained(AttrKinds, RK.AttrKind) && - Filter(RK, cast(U.getUser()), Bundle)) { + Filter(RK, II, &II->bundle_op_info_begin()[Elem.Index])) { NumUsefullAssumeQueries++; return RK; } + } } + return RetainedKnowledge::none(); } RetainedKnowledge llvm::getKnowledgeValidInContext( const Value *V, ArrayRef AttrKinds, - const Instruction *CtxI, const DominatorTree *DT, AssumptionCache *AC) { + AssumptionCache &AC, const Instruction *CtxI, const DominatorTree *DT) { return getKnowledgeForValue(V, AttrKinds, AC, [&](auto, Instruction *I, auto) { return isValidAssumeForContext(I, CtxI, DT); diff --git a/llvm/lib/Analysis/Loads.cpp b/llvm/lib/Analysis/Loads.cpp index b461c41d29e84..425f3682122cd 100644 --- a/llvm/lib/Analysis/Loads.cpp +++ b/llvm/lib/Analysis/Loads.cpp @@ -174,14 +174,14 @@ static bool isDereferenceableAndAlignedPointer( // information for values that cannot be freed in the function. // TODO: More precisely check if the pointer can be freed between assumption // and use. - if (CtxI && !V->canBeFreed()) { + if (CtxI && AC && !V->canBeFreed()) { /// Look through assumes to see if both dereferencability and alignment can /// be proven by an assume if needed. RetainedKnowledge AlignRK; RetainedKnowledge DerefRK; bool IsAligned = V->getPointerAlignment(DL) >= Alignment; if (getKnowledgeForValue( - V, {Attribute::Dereferenceable, Attribute::Alignment}, AC, + V, {Attribute::Dereferenceable, Attribute::Alignment}, *AC, [&](RetainedKnowledge RK, Instruction *Assume, auto) { if (!isValidAssumeForContext(Assume, CtxI, DT)) return false; diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index ba6da4dd6d614..3d403531cea2f 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -8010,7 +8010,7 @@ static bool isGuaranteedNotToBeUndefOrPoison( Dominator = Dominator->getIDom(); } - if (getKnowledgeValidInContext(V, {Attribute::NoUndef}, CtxI, DT, AC)) + if (AC && getKnowledgeValidInContext(V, {Attribute::NoUndef}, *AC, CtxI, DT)) return true; return false; diff --git a/llvm/lib/Transforms/Utils/AssumeBundleBuilder.cpp b/llvm/lib/Transforms/Utils/AssumeBundleBuilder.cpp index 78b9c7d06e183..7210fee9c7b7c 100644 --- a/llvm/lib/Transforms/Utils/AssumeBundleBuilder.cpp +++ b/llvm/lib/Transforms/Utils/AssumeBundleBuilder.cpp @@ -114,12 +114,12 @@ struct AssumeBuilderState { : M(M), InstBeingModified(I), AC(AC), DT(DT) {} bool tryToPreserveWithoutAddingAssume(RetainedKnowledge RK) { - if (!InstBeingModified || !RK.WasOn) + if (!InstBeingModified || !RK.WasOn || !AC) return false; bool HasBeenPreserved = false; Use* ToUpdate = nullptr; getKnowledgeForValue( - RK.WasOn, {RK.AttrKind}, AC, + RK.WasOn, {RK.AttrKind}, *AC, [&](RetainedKnowledge RKOther, Instruction *Assume, const CallInst::BundleOpInfo *Bundle) { if (!isValidAssumeForContext(Assume, InstBeingModified, DT))