-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[ScalarEvolution] Limit recursion in getRangeRef for PHI nodes. #152823
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
efriedma-quic
wants to merge
1
commit into
llvm:main
Choose a base branch
from
efriedma-quic:scev-getrangeref-recursion
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 |
|---|---|---|
|
|
@@ -6000,7 +6000,9 @@ static bool BrPHIToSelect(DominatorTree &DT, BranchInst *BI, PHINode *Merge, | |
| return false; | ||
| } | ||
|
|
||
| const SCEV *ScalarEvolution::createNodeFromSelectLikePHI(PHINode *PN) { | ||
| static bool getOperandsForSelectLikePHI(DominatorTree &DT, PHINode *PN, | ||
| Value *&Cond, Value *&LHS, | ||
| Value *&RHS) { | ||
| auto IsReachable = | ||
| [&](BasicBlock *BB) { return DT.isReachableFromEntry(BB); }; | ||
| if (PN->getNumIncomingValues() == 2 && all_of(PN->blocks(), IsReachable)) { | ||
|
|
@@ -6020,29 +6022,23 @@ const SCEV *ScalarEvolution::createNodeFromSelectLikePHI(PHINode *PN) { | |
| assert(IDom && "At least the entry block should dominate PN"); | ||
|
|
||
| auto *BI = dyn_cast<BranchInst>(IDom->getTerminator()); | ||
| Value *Cond = nullptr, *LHS = nullptr, *RHS = nullptr; | ||
|
|
||
| if (BI && BI->isConditional() && | ||
| BrPHIToSelect(DT, BI, PN, Cond, LHS, RHS) && | ||
| properlyDominates(getSCEV(LHS), PN->getParent()) && | ||
| properlyDominates(getSCEV(RHS), PN->getParent())) | ||
| return createNodeForSelectOrPHI(PN, Cond, LHS, RHS); | ||
| return BI && BI->isConditional() && | ||
| BrPHIToSelect(DT, BI, PN, Cond, LHS, RHS); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| const SCEV *ScalarEvolution::createNodeFromSelectLikePHI(PHINode *PN) { | ||
| Value *Cond = nullptr, *LHS = nullptr, *RHS = nullptr; | ||
| if (getOperandsForSelectLikePHI(DT, PN, Cond, LHS, RHS) && | ||
| properlyDominates(getSCEV(LHS), PN->getParent()) && | ||
| properlyDominates(getSCEV(RHS), PN->getParent())) | ||
| return createNodeForSelectOrPHI(PN, Cond, LHS, RHS); | ||
|
|
||
| return nullptr; | ||
| } | ||
|
|
||
| /// Returns SCEV for the first operand of a phi if all phi operands have | ||
| /// identical opcodes and operands | ||
| /// eg. | ||
| /// a: %add = %a + %b | ||
| /// br %c | ||
| /// b: %add1 = %a + %b | ||
| /// br %c | ||
| /// c: %phi = phi [%add, a], [%add1, b] | ||
| /// scev(%phi) => scev(%add) | ||
| const SCEV * | ||
| ScalarEvolution::createNodeForPHIWithIdenticalOperands(PHINode *PN) { | ||
| static BinaryOperator *getCommonInstForPHI(PHINode *PN) { | ||
| BinaryOperator *CommonInst = nullptr; | ||
| // Check if instructions are identical. | ||
| for (Value *Incoming : PN->incoming_values()) { | ||
|
|
@@ -6057,6 +6053,21 @@ ScalarEvolution::createNodeForPHIWithIdenticalOperands(PHINode *PN) { | |
| CommonInst = IncomingInst; | ||
| } | ||
| } | ||
| return CommonInst; | ||
| } | ||
|
|
||
| /// Returns SCEV for the first operand of a phi if all phi operands have | ||
| /// identical opcodes and operands | ||
| /// eg. | ||
| /// a: %add = %a + %b | ||
| /// br %c | ||
| /// b: %add1 = %a + %b | ||
| /// br %c | ||
| /// c: %phi = phi [%add, a], [%add1, b] | ||
| /// scev(%phi) => scev(%add) | ||
| const SCEV * | ||
| ScalarEvolution::createNodeForPHIWithIdenticalOperands(PHINode *PN) { | ||
| BinaryOperator *CommonInst = getCommonInstForPHI(PN); | ||
| if (!CommonInst) | ||
| return nullptr; | ||
|
|
||
|
|
@@ -6581,6 +6592,28 @@ getRangeForUnknownRecurrence(const SCEVUnknown *U) { | |
| return FullSet; | ||
| } | ||
|
|
||
| // The goal of this function is to check if recursively visiting the operands | ||
| // of this PHI might lead to an infinite loop. If we do see such a loop, | ||
| // there's no good way to break it, so we avoid analyzing such cases. | ||
| // | ||
| // getRangeRef previously used a visited set to avoid infinite loops, but this | ||
| // caused other issues: the result was dependent on the order of getRangeRef | ||
| // calls, and the interaction with createSCEVIter could cause a stack overflow | ||
| // in some cases (see issue #148253). | ||
| // | ||
| // FIXME: The way this is implemented is overly conservative. | ||
| static bool RangeRefPHIAllowedOperands(DominatorTree &DT, PHINode *PH) { | ||
| Value *Cond = nullptr, *LHS = nullptr, *RHS = nullptr; | ||
| if (getOperandsForSelectLikePHI(DT, PH, Cond, LHS, RHS)) | ||
| return true; | ||
|
|
||
| if (all_of(PH->operands(), | ||
| [&](Value *Operand) { return DT.dominates(Operand, PH); })) | ||
| return true; | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| const ConstantRange & | ||
| ScalarEvolution::getRangeRefIter(const SCEV *S, | ||
| ScalarEvolution::RangeSignHint SignHint) { | ||
|
|
@@ -6636,8 +6669,8 @@ ScalarEvolution::getRangeRefIter(const SCEV *S, | |
| continue; | ||
| } | ||
| // `SCEVUnknown`'s require special treatment. | ||
| if (const PHINode *P = dyn_cast<PHINode>(UnknownS->getValue())) { | ||
| if (!PendingPhiRangesIter.insert(P).second) | ||
| if (PHINode *P = dyn_cast<PHINode>(UnknownS->getValue())) { | ||
| if (!RangeRefPHIAllowedOperands(DT, P)) | ||
| continue; | ||
| for (auto &Op : reverse(P->operands())) | ||
| AddToWorklist(getSCEV(Op)); | ||
|
|
@@ -6650,10 +6683,6 @@ ScalarEvolution::getRangeRefIter(const SCEV *S, | |
| // their users in most cases. | ||
| for (const SCEV *P : reverse(drop_begin(WorkList))) { | ||
| getRangeRef(P, SignHint); | ||
|
|
||
| if (auto *UnknownS = dyn_cast<SCEVUnknown>(P)) | ||
| if (const PHINode *P = dyn_cast<PHINode>(UnknownS->getValue())) | ||
| PendingPhiRangesIter.erase(P); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -6963,8 +6992,13 @@ const ConstantRange &ScalarEvolution::getRangeRef( | |
|
|
||
| // A range of Phi is a subset of union of all ranges of its input. | ||
| if (PHINode *Phi = dyn_cast<PHINode>(V)) { | ||
| // SCEVExpander sometimes creates SCEVUnknowns that are secretly | ||
| // AddRecs; return the range for the corresponding AddRec. | ||
| if (auto *AR = dyn_cast<SCEVAddRecExpr>(getSCEV(V))) | ||
| return getRangeRef(AR, SignHint, Depth + 1); | ||
|
|
||
| // Make sure that we do not run over cycled Phis. | ||
| if (PendingPhiRanges.insert(Phi).second) { | ||
| if (RangeRefPHIAllowedOperands(DT, Phi)) { | ||
|
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. We could fall back to ValueTracking here to get at least some information. Not sure if that would be practically useful or not. |
||
| ConstantRange RangeFromOps(BitWidth, /*isFullSet=*/false); | ||
|
|
||
| for (const auto &Op : Phi->operands()) { | ||
|
|
@@ -6976,9 +7010,6 @@ const ConstantRange &ScalarEvolution::getRangeRef( | |
| } | ||
| ConservativeResult = | ||
| ConservativeResult.intersectWith(RangeFromOps, RangeType); | ||
| bool Erased = PendingPhiRanges.erase(Phi); | ||
| assert(Erased && "Failed to erase Phi properly?"); | ||
| (void)Erased; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -7657,7 +7688,55 @@ ScalarEvolution::getOperandsToCreate(Value *V, SmallVectorImpl<Value *> &Ops) { | |
| return getUnknown(V); | ||
|
|
||
| case Instruction::PHI: | ||
| // Keep constructing SCEVs' for phis recursively for now. | ||
| // getNodeForPHI has four ways to turn a PHI into a SCEV; retrieve the | ||
| // relevant nodes for each of them. | ||
| // | ||
| // The first is just to call simplifyInstruction, and get something back | ||
| // that isn't a PHI. | ||
| if (Value *V = simplifyInstruction( | ||
| cast<PHINode>(U), | ||
| {getDataLayout(), &TLI, &DT, &AC, /*CtxI=*/nullptr, | ||
| /*UseInstrInfo=*/true, /*CanUseUndef=*/false})) { | ||
| assert(V); | ||
| Ops.push_back(V); | ||
| return nullptr; | ||
| } | ||
| // The second is createNodeForPHIWithIdenticalOperands: this looks for | ||
| // operands which all perform the same operation, but haven't been | ||
| // CSE'ed for whatever reason. | ||
| if (BinaryOperator *BO = getCommonInstForPHI(cast<PHINode>(U))) { | ||
| assert(BO); | ||
| Ops.push_back(BO); | ||
| return nullptr; | ||
| } | ||
| // The third is createNodeFromSelectLikePHI; this takes a PHI which | ||
| // is equivalent to a select, and analyzes it like a select. | ||
| { | ||
| Value *Cond = nullptr, *LHS = nullptr, *RHS = nullptr; | ||
| if (getOperandsForSelectLikePHI(DT, cast<PHINode>(U), Cond, LHS, RHS)) { | ||
| assert(Cond); | ||
| assert(LHS); | ||
| assert(RHS); | ||
| if (auto *CondICmp = dyn_cast<ICmpInst>(Cond)) { | ||
| Ops.push_back(CondICmp->getOperand(0)); | ||
| Ops.push_back(CondICmp->getOperand(1)); | ||
| } | ||
| Ops.push_back(Cond); | ||
| Ops.push_back(LHS); | ||
| Ops.push_back(RHS); | ||
| return nullptr; | ||
| } | ||
| } | ||
| // The fourth way is createAddRecFromPHI. It's complicated to handle here, | ||
| // so just construct it recursively. | ||
| // | ||
| // In addition to getNodeForPHI, also construct nodes which might be needed | ||
| // by getRangeRef. | ||
| if (RangeRefPHIAllowedOperands(DT, cast<PHINode>(U))) { | ||
| for (Value *V : cast<PHINode>(U)->operands()) | ||
| Ops.push_back(V); | ||
| return nullptr; | ||
| } | ||
| return nullptr; | ||
|
|
||
| case Instruction::Select: { | ||
|
|
@@ -13728,7 +13807,6 @@ ScalarEvolution::ScalarEvolution(ScalarEvolution &&Arg) | |
| DT(Arg.DT), LI(Arg.LI), CouldNotCompute(std::move(Arg.CouldNotCompute)), | ||
| ValueExprMap(std::move(Arg.ValueExprMap)), | ||
| PendingLoopPredicates(std::move(Arg.PendingLoopPredicates)), | ||
| PendingPhiRanges(std::move(Arg.PendingPhiRanges)), | ||
| PendingMerges(std::move(Arg.PendingMerges)), | ||
| ConstantMultipleCache(std::move(Arg.ConstantMultipleCache)), | ||
| BackedgeTakenCounts(std::move(Arg.BackedgeTakenCounts)), | ||
|
|
@@ -13771,7 +13849,6 @@ ScalarEvolution::~ScalarEvolution() { | |
| PredicatedBackedgeTakenCounts.clear(); | ||
|
|
||
| assert(PendingLoopPredicates.empty() && "isImpliedCond garbage"); | ||
| assert(PendingPhiRanges.empty() && "getRangeRef garbage"); | ||
| assert(PendingMerges.empty() && "isImpliedViaMerge garbage"); | ||
| assert(!WalkingBEDominatingConds && "isLoopBackedgeGuardedByCond garbage!"); | ||
| assert(!ProvingSplitPredicate && "ProvingSplitPredicate garbage!"); | ||
|
|
||
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
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.
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.
nit: PH -> PHI, Phi, P