diff --git a/llvm/include/llvm/Analysis/CaptureTracking.h b/llvm/include/llvm/Analysis/CaptureTracking.h --- a/llvm/include/llvm/Analysis/CaptureTracking.h +++ b/llvm/include/llvm/Analysis/CaptureTracking.h @@ -21,13 +21,10 @@ class Instruction; class DominatorTree; - /// The default value for MaxUsesToExplore argument. It's relatively small to - /// keep the cost of analysis reasonable for clients like BasicAliasAnalysis, - /// where the results can't be cached. - /// TODO: we should probably introduce a caching CaptureTracking analysis and - /// use it where possible. The caching version can use much higher limit or - /// don't have this cap at all. - unsigned constexpr DefaultMaxUsesToExplore = 20; + /// getDefaultMaxUsesToExploreForCaptureTracking - Return default value of + /// the maximal number of uses to explore before giving up. It is used by + /// PointerMayBeCaptured family analysys. + unsigned getDefaultMaxUsesToExploreForCaptureTracking(); /// PointerMayBeCaptured - Return true if this pointer value may be captured /// by the enclosing function (which is required to exist). This routine can @@ -38,10 +35,10 @@ /// automatically counts as capturing it or not. /// MaxUsesToExplore specifies how many uses should the analysis explore for /// one value before giving up due too "too many uses". - bool PointerMayBeCaptured(const Value *V, - bool ReturnCaptures, + bool PointerMayBeCaptured(const Value *V, bool ReturnCaptures, bool StoreCaptures, - unsigned MaxUsesToExplore = DefaultMaxUsesToExplore); + unsigned MaxUsesToExplore = + getDefaultMaxUsesToExploreForCaptureTracking()); /// PointerMayBeCapturedBefore - Return true if this pointer value may be /// captured by the enclosing function (which is required to exist). If a @@ -55,10 +52,11 @@ /// final parameter is true. /// MaxUsesToExplore specifies how many uses should the analysis explore for /// one value before giving up due too "too many uses". - bool PointerMayBeCapturedBefore(const Value *V, bool ReturnCaptures, - bool StoreCaptures, const Instruction *I, - const DominatorTree *DT, bool IncludeI = false, - unsigned MaxUsesToExplore = DefaultMaxUsesToExplore); + bool PointerMayBeCapturedBefore( + const Value *V, bool ReturnCaptures, bool StoreCaptures, + const Instruction *I, const DominatorTree *DT, bool IncludeI = false, + unsigned MaxUsesToExplore = + getDefaultMaxUsesToExploreForCaptureTracking()); /// This callback is used in conjunction with PointerMayBeCaptured. In /// addition to the interface here, you'll need to provide your own getters @@ -94,7 +92,8 @@ /// MaxUsesToExplore specifies how many uses should the analysis explore for /// one value before giving up due too "too many uses". void PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker, - unsigned MaxUsesToExplore = DefaultMaxUsesToExplore); + unsigned MaxUsesToExplore = + getDefaultMaxUsesToExploreForCaptureTracking()); } // end namespace llvm #endif diff --git a/llvm/lib/Analysis/CaptureTracking.cpp b/llvm/lib/Analysis/CaptureTracking.cpp --- a/llvm/lib/Analysis/CaptureTracking.cpp +++ b/llvm/lib/Analysis/CaptureTracking.cpp @@ -28,6 +28,21 @@ using namespace llvm; +/// The default value for MaxUsesToExplore argument. It's relatively small to +/// keep the cost of analysis reasonable for clients like BasicAliasAnalysis, +/// where the results can't be cached. +/// TODO: we should probably introduce a caching CaptureTracking analysis and +/// use it where possible. The caching version can use much higher limit or +/// don't have this cap at all. +static cl::opt +DefaultMaxUsesToExplore("capture-tracking-max-uses-to-explore", cl::Hidden, + cl::desc("Maximal number of uses to explore."), + cl::init(20)); + +unsigned llvm::getDefaultMaxUsesToExploreForCaptureTracking() { + return DefaultMaxUsesToExplore; +} + CaptureTracker::~CaptureTracker() {} bool CaptureTracker::shouldExplore(const Use *U) { return true; } @@ -215,8 +230,9 @@ void llvm::PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker, unsigned MaxUsesToExplore) { assert(V->getType()->isPointerTy() && "Capture is for pointers only!"); - SmallVector Worklist; - SmallSet Visited; + SmallVector Worklist; + Worklist.reserve(getDefaultMaxUsesToExploreForCaptureTracking()); + SmallSet Visited; auto AddUses = [&](const Value *V) { unsigned Count = 0; diff --git a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp --- a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp +++ b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp @@ -4170,7 +4170,8 @@ // defined in AACaptureUseTracker, that can look at in-flight abstract // attributes and directly updates the assumed state. SmallVector PotentialCopies; - unsigned RemainingUsesToExplore = DefaultMaxUsesToExplore; + unsigned RemainingUsesToExplore = + getDefaultMaxUsesToExploreForCaptureTracking(); AACaptureUseTracker Tracker(A, *this, IsDeadAA, T, PotentialCopies, RemainingUsesToExplore);