-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[ValueTracking] Make the MaxAnalysisRecursionDepth overridable #137721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jrbyrnes
wants to merge
1
commit into
llvm:main
Choose a base branch
from
jrbyrnes:RecursionDepth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,8 @@ using namespace llvm::PatternMatch; | |
static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses", | ||
cl::Hidden, cl::init(20)); | ||
|
||
static cl::opt<bool> ExhaustiveRecursion("exhaustive-analysis-recursion", | ||
cl::Hidden); | ||
|
||
/// Returns the bitwidth of the given scalar or pointer type. For vector types, | ||
/// returns the element type's bitwidth. | ||
|
@@ -129,6 +131,12 @@ static bool getShuffleDemandedElts(const ShuffleVectorInst *Shuf, | |
DemandedElts, DemandedLHS, DemandedRHS); | ||
} | ||
|
||
unsigned llvm::getAnalysisRecursionDepthLimit() { | ||
if (!ExhaustiveRecursion.getNumOccurrences() || !ExhaustiveRecursion) | ||
return MaxAnalysisRecursionDepth; | ||
return -1; | ||
} | ||
|
||
static void computeKnownBits(const Value *V, const APInt &DemandedElts, | ||
KnownBits &Known, unsigned Depth, | ||
const SimplifyQuery &Q); | ||
|
@@ -793,7 +801,7 @@ static void computeKnownBitsFromCond(const Value *V, Value *Cond, | |
KnownBits &Known, unsigned Depth, | ||
const SimplifyQuery &SQ, bool Invert) { | ||
Value *A, *B; | ||
if (Depth < MaxAnalysisRecursionDepth && | ||
if (Depth < getAnalysisRecursionDepthLimit() && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid calling this helper function multiple times inside a function. Compute it once and then reuse the result. This PR contains many such instances. |
||
match(Cond, m_LogicalOp(m_Value(A), m_Value(B)))) { | ||
KnownBits Known2(Known.getBitWidth()); | ||
KnownBits Known3(Known.getBitWidth()); | ||
|
@@ -828,7 +836,8 @@ static void computeKnownBitsFromCond(const Value *V, Value *Cond, | |
return; | ||
} | ||
|
||
if (Depth < MaxAnalysisRecursionDepth && match(Cond, m_Not(m_Value(A)))) | ||
if (Depth < getAnalysisRecursionDepthLimit() && | ||
match(Cond, m_Not(m_Value(A)))) | ||
computeKnownBitsFromCond(V, A, Known, Depth + 1, SQ, !Invert); | ||
} | ||
|
||
|
@@ -922,7 +931,7 @@ void llvm::computeKnownBitsFromContext(const Value *V, KnownBits &Known, | |
} | ||
|
||
// The remaining tests are all recursive, so bail out if we hit the limit. | ||
if (Depth == MaxAnalysisRecursionDepth) | ||
if (Depth == getAnalysisRecursionDepthLimit()) | ||
continue; | ||
|
||
ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg); | ||
|
@@ -1690,7 +1699,7 @@ static void computeKnownBitsFromOperator(const Operator *I, | |
|
||
// Otherwise take the unions of the known bit sets of the operands, | ||
// taking conservative care to avoid excessive recursion. | ||
if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) { | ||
if (Depth < getAnalysisRecursionDepthLimit() - 1 && Known.isUnknown()) { | ||
// Skip if every incoming value references to ourself. | ||
if (isa_and_nonnull<UndefValue>(P->hasConstantValue())) | ||
break; | ||
|
@@ -1719,7 +1728,7 @@ static void computeKnownBitsFromOperator(const Operator *I, | |
// TODO: See if we can base recursion limiter on number of incoming phi | ||
// edges so we don't overly clamp analysis. | ||
computeKnownBits(IncValue, DemandedElts, Known2, | ||
MaxAnalysisRecursionDepth - 1, RecQ); | ||
getAnalysisRecursionDepthLimit() - 1, RecQ); | ||
|
||
// See if we can further use a conditional branch into the phi | ||
// to help us determine the range of the value. | ||
|
@@ -2188,7 +2197,7 @@ void computeKnownBits(const Value *V, const APInt &DemandedElts, | |
} | ||
|
||
assert(V && "No Value?"); | ||
assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth"); | ||
assert(Depth <= getAnalysisRecursionDepthLimit() && "Limit Search Depth"); | ||
|
||
#ifndef NDEBUG | ||
Type *Ty = V->getType(); | ||
|
@@ -2287,7 +2296,7 @@ void computeKnownBits(const Value *V, const APInt &DemandedElts, | |
Known = Range->toKnownBits(); | ||
|
||
// All recursive calls that increase depth must come after this. | ||
if (Depth == MaxAnalysisRecursionDepth) | ||
if (Depth == getAnalysisRecursionDepthLimit()) | ||
return; | ||
|
||
// A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has | ||
|
@@ -2400,7 +2409,7 @@ static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero, | |
/// types and vectors of integers. | ||
bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth, | ||
const SimplifyQuery &Q) { | ||
assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth"); | ||
assert(Depth <= getAnalysisRecursionDepthLimit() && "Limit Search Depth"); | ||
|
||
if (isa<Constant>(V)) | ||
return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2()); | ||
|
@@ -2462,7 +2471,7 @@ bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth, | |
return true; | ||
|
||
// The remaining tests are all recursive, so bail out if we hit the limit. | ||
if (Depth++ == MaxAnalysisRecursionDepth) | ||
if (Depth++ == getAnalysisRecursionDepthLimit()) | ||
return false; | ||
|
||
switch (I->getOpcode()) { | ||
|
@@ -2550,7 +2559,7 @@ bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth, | |
|
||
// Recursively check all incoming values. Limit recursion to 2 levels, so | ||
// that search complexity is limited to number of operands^2. | ||
unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1); | ||
unsigned NewDepth = std::max(Depth, getAnalysisRecursionDepthLimit() - 1); | ||
return llvm::all_of(PN->operands(), [&](const Use &U) { | ||
// Value is power of 2 if it is coming from PHI node itself by induction. | ||
if (U.get() == PN) | ||
|
@@ -2654,7 +2663,7 @@ static bool isGEPKnownNonNull(const GEPOperator *GEP, unsigned Depth, | |
// to recurse 10k times just because we have 10k GEP operands. We don't | ||
// bail completely out because we want to handle constant GEPs regardless | ||
// of depth. | ||
if (Depth++ >= MaxAnalysisRecursionDepth) | ||
if (Depth++ >= getAnalysisRecursionDepthLimit()) | ||
continue; | ||
|
||
if (isKnownNonZero(GTI.getOperand(), Q, Depth)) | ||
|
@@ -3158,7 +3167,7 @@ static bool isKnownNonZeroFromOperator(const Operator *I, | |
|
||
// Check if all incoming values are non-zero using recursion. | ||
SimplifyQuery RecQ = Q.getWithoutCondContext(); | ||
unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1); | ||
unsigned NewDepth = std::max(Depth, getAnalysisRecursionDepthLimit() - 1); | ||
return llvm::all_of(PN->operands(), [&](const Use &U) { | ||
if (U.get() == PN) | ||
return true; | ||
|
@@ -3424,7 +3433,7 @@ bool isKnownNonZero(const Value *V, const APInt &DemandedElts, | |
Type *Ty = V->getType(); | ||
|
||
#ifndef NDEBUG | ||
assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth"); | ||
assert(Depth <= getAnalysisRecursionDepthLimit() && "Limit Search Depth"); | ||
|
||
if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) { | ||
assert( | ||
|
@@ -3487,7 +3496,7 @@ bool isKnownNonZero(const Value *V, const APInt &DemandedElts, | |
return true; | ||
|
||
// Some of the tests below are recursive, so bail out if we hit the limit. | ||
if (Depth++ >= MaxAnalysisRecursionDepth) | ||
if (Depth++ >= getAnalysisRecursionDepthLimit()) | ||
return false; | ||
|
||
// Check for pointer simplifications. | ||
|
@@ -3871,7 +3880,7 @@ static bool isKnownNonEqual(const Value *V1, const Value *V2, | |
// We can't look through casts yet. | ||
return false; | ||
|
||
if (Depth >= MaxAnalysisRecursionDepth) | ||
if (Depth >= getAnalysisRecursionDepthLimit()) | ||
return false; | ||
|
||
// See if we can recurse through (exactly one of) our operands. This | ||
|
@@ -3988,7 +3997,7 @@ static unsigned ComputeNumSignBitsImpl(const Value *V, | |
unsigned Depth, const SimplifyQuery &Q) { | ||
Type *Ty = V->getType(); | ||
#ifndef NDEBUG | ||
assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth"); | ||
assert(Depth <= getAnalysisRecursionDepthLimit() && "Limit Search Depth"); | ||
|
||
if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) { | ||
assert( | ||
|
@@ -4015,7 +4024,7 @@ static unsigned ComputeNumSignBitsImpl(const Value *V, | |
// Note that ConstantInt is handled by the general computeKnownBits case | ||
// below. | ||
|
||
if (Depth == MaxAnalysisRecursionDepth) | ||
if (Depth == getAnalysisRecursionDepthLimit()) | ||
return 1; | ||
|
||
if (auto *U = dyn_cast<Operator>(V)) { | ||
|
@@ -4970,7 +4979,7 @@ static void computeKnownFPClassFromCond(const Value *V, Value *Cond, | |
const Instruction *CxtI, | ||
KnownFPClass &KnownFromContext) { | ||
Value *A, *B; | ||
if (Depth < MaxAnalysisRecursionDepth && | ||
if (Depth < getAnalysisRecursionDepthLimit() && | ||
(CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B))) | ||
: match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) { | ||
computeKnownFPClassFromCond(V, A, Depth + 1, CondIsTrue, CxtI, | ||
|
@@ -4979,7 +4988,8 @@ static void computeKnownFPClassFromCond(const Value *V, Value *Cond, | |
KnownFromContext); | ||
return; | ||
} | ||
if (Depth < MaxAnalysisRecursionDepth && match(Cond, m_Not(m_Value(A)))) { | ||
if (Depth < getAnalysisRecursionDepthLimit() && | ||
match(Cond, m_Not(m_Value(A)))) { | ||
computeKnownFPClassFromCond(V, A, Depth + 1, !CondIsTrue, CxtI, | ||
KnownFromContext); | ||
return; | ||
|
@@ -5106,7 +5116,7 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts, | |
return; | ||
} | ||
|
||
assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth"); | ||
assert(Depth <= getAnalysisRecursionDepthLimit() && "Limit Search Depth"); | ||
|
||
if (auto *CFP = dyn_cast<ConstantFP>(V)) { | ||
Known.KnownFPClasses = CFP->getValueAPF().classify(); | ||
|
@@ -5200,7 +5210,7 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts, | |
return; | ||
|
||
// All recursive calls that increase depth must come after this. | ||
if (Depth == MaxAnalysisRecursionDepth) | ||
if (Depth == getAnalysisRecursionDepthLimit()) | ||
return; | ||
|
||
const unsigned Opc = Op->getOpcode(); | ||
|
@@ -6144,7 +6154,7 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts, | |
|
||
// Otherwise take the unions of the known bit sets of the operands, | ||
// taking conservative care to avoid excessive recursion. | ||
const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2; | ||
const unsigned PhiRecursionLimit = getAnalysisRecursionDepthLimit() - 2; | ||
|
||
if (Depth < PhiRecursionLimit) { | ||
// Skip if every incoming value references to ourself. | ||
|
@@ -7857,7 +7867,7 @@ static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly); | |
static bool isGuaranteedNotToBeUndefOrPoison( | ||
const Value *V, AssumptionCache *AC, const Instruction *CtxI, | ||
const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) { | ||
if (Depth >= MaxAnalysisRecursionDepth) | ||
if (Depth >= getAnalysisRecursionDepthLimit()) | ||
return false; | ||
|
||
if (isa<MetadataAsValue>(V)) | ||
|
@@ -9193,7 +9203,7 @@ static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2, | |
SelectPatternResult llvm::matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, | ||
Instruction::CastOps *CastOp, | ||
unsigned Depth) { | ||
if (Depth >= MaxAnalysisRecursionDepth) | ||
if (Depth >= getAnalysisRecursionDepthLimit()) | ||
return {SPF_UNKNOWN, SPNB_NA, false}; | ||
|
||
SelectInst *SI = dyn_cast<SelectInst>(V); | ||
|
@@ -9613,10 +9623,10 @@ isImpliedCondICmps(const ICmpInst *LHS, CmpPredicate RPred, const Value *R0, | |
// C1` (see discussion: D58633). | ||
ConstantRange LCR = computeConstantRange( | ||
L1, ICmpInst::isSigned(LPred), /* UseInstrInfo=*/true, /*AC=*/nullptr, | ||
/*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1); | ||
/*CxtI=*/nullptr, /*DT=*/nullptr, getAnalysisRecursionDepthLimit() - 1); | ||
ConstantRange RCR = computeConstantRange( | ||
R1, ICmpInst::isSigned(RPred), /* UseInstrInfo=*/true, /*AC=*/nullptr, | ||
/*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1); | ||
/*CxtI=*/nullptr, /*DT=*/nullptr, getAnalysisRecursionDepthLimit() - 1); | ||
// Even if L1/R1 are not both constant, we can still sometimes deduce | ||
// relationship from a single constant. For example X u> Y implies X != 0. | ||
if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR)) | ||
|
@@ -9681,7 +9691,7 @@ isImpliedCondAndOr(const Instruction *LHS, CmpPredicate RHSPred, | |
LHS->getOpcode() == Instruction::Select) && | ||
"Expected LHS to be 'and', 'or', or 'select'."); | ||
|
||
assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit"); | ||
assert(Depth <= getAnalysisRecursionDepthLimit() && "Hit recursion limit"); | ||
|
||
// If the result of an 'or' is false, then we know both legs of the 'or' are | ||
// false. Similarly, if the result of an 'and' is true, then we know both | ||
|
@@ -9706,7 +9716,7 @@ llvm::isImpliedCondition(const Value *LHS, CmpPredicate RHSPred, | |
const Value *RHSOp0, const Value *RHSOp1, | ||
const DataLayout &DL, bool LHSIsTrue, unsigned Depth) { | ||
// Bail out when we hit the limit. | ||
if (Depth == MaxAnalysisRecursionDepth) | ||
if (Depth == getAnalysisRecursionDepthLimit()) | ||
return std::nullopt; | ||
|
||
// A mismatch occurs when we compare a scalar cmp to a vector cmp, for | ||
|
@@ -9762,7 +9772,7 @@ std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS, | |
return std::nullopt; | ||
} | ||
|
||
if (Depth == MaxAnalysisRecursionDepth) | ||
if (Depth == getAnalysisRecursionDepthLimit()) | ||
return std::nullopt; | ||
|
||
// LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2 | ||
|
@@ -10224,7 +10234,7 @@ ConstantRange llvm::computeConstantRange(const Value *V, bool ForSigned, | |
unsigned Depth) { | ||
assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction"); | ||
|
||
if (Depth == MaxAnalysisRecursionDepth) | ||
if (Depth == getAnalysisRecursionDepthLimit()) | ||
return ConstantRange::getFull(V->getType()->getScalarSizeInBits()); | ||
|
||
if (auto *C = dyn_cast<Constant>(V)) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should not be a cl::opt, should be a local pass parameter if configurable