-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[DFAJumpThreading] Unify equivalent states #162447
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -384,6 +384,22 @@ inline raw_ostream &operator<<(raw_ostream &OS, const PathType &Path) { | |
return OS; | ||
} | ||
|
||
/// Helper to get the successor corresponding to a particular case value for | ||
/// a switch statement. | ||
static BasicBlock *getNextCaseSuccessor(SwitchInst *Switch, | ||
const APInt &NextState) { | ||
BasicBlock *NextCase = nullptr; | ||
for (auto Case : Switch->cases()) { | ||
if (Case.getCaseValue()->getValue() == NextState) { | ||
NextCase = Case.getCaseSuccessor(); | ||
break; | ||
} | ||
} | ||
if (!NextCase) | ||
NextCase = Switch->getDefaultDest(); | ||
return NextCase; | ||
} | ||
|
||
namespace { | ||
/// ThreadingPath is a path in the control flow of a loop that can be threaded | ||
/// by cloning necessary basic blocks and replacing conditional branches with | ||
|
@@ -396,6 +412,10 @@ struct ThreadingPath { | |
ExitVal = V->getValue(); | ||
IsExitValSet = true; | ||
} | ||
void setExitValue(const APInt &V) { | ||
ExitVal = V; | ||
IsExitValSet = true; | ||
} | ||
bool isExitValueSet() const { return IsExitValSet; } | ||
|
||
/// Determinator is the basic block that determines the next state of the DFA. | ||
|
@@ -578,44 +598,8 @@ struct AllSwitchPaths { | |
BasicBlock *getSwitchBlock() { return SwitchBlock; } | ||
|
||
void run() { | ||
StateDefMap StateDef = getStateDefMap(); | ||
if (StateDef.empty()) { | ||
ORE->emit([&]() { | ||
return OptimizationRemarkMissed(DEBUG_TYPE, "SwitchNotPredictable", | ||
Switch) | ||
<< "Switch instruction is not predictable."; | ||
}); | ||
return; | ||
} | ||
|
||
auto *SwitchPhi = cast<PHINode>(Switch->getOperand(0)); | ||
auto *SwitchPhiDefBB = SwitchPhi->getParent(); | ||
VisitedBlocks VB; | ||
// Get paths from the determinator BBs to SwitchPhiDefBB | ||
std::vector<ThreadingPath> PathsToPhiDef = | ||
getPathsFromStateDefMap(StateDef, SwitchPhi, VB, MaxNumPaths); | ||
if (SwitchPhiDefBB == SwitchBlock || PathsToPhiDef.empty()) { | ||
TPaths = std::move(PathsToPhiDef); | ||
return; | ||
} | ||
|
||
assert(MaxNumPaths >= PathsToPhiDef.size() && !PathsToPhiDef.empty()); | ||
auto PathsLimit = MaxNumPaths / PathsToPhiDef.size(); | ||
// Find and append paths from SwitchPhiDefBB to SwitchBlock. | ||
PathsType PathsToSwitchBB = | ||
paths(SwitchPhiDefBB, SwitchBlock, VB, /* PathDepth = */ 1, PathsLimit); | ||
if (PathsToSwitchBB.empty()) | ||
return; | ||
|
||
std::vector<ThreadingPath> TempList; | ||
for (const ThreadingPath &Path : PathsToPhiDef) { | ||
for (const PathType &PathToSw : PathsToSwitchBB) { | ||
ThreadingPath PathCopy(Path); | ||
PathCopy.appendExcludingFirst(PathToSw); | ||
TempList.push_back(PathCopy); | ||
} | ||
} | ||
TPaths = std::move(TempList); | ||
findTPaths(); | ||
unifyTPaths(); | ||
} | ||
|
||
private: | ||
|
@@ -807,6 +791,69 @@ struct AllSwitchPaths { | |
return Res; | ||
} | ||
|
||
// Find all threadable paths. | ||
void findTPaths() { | ||
StateDefMap StateDef = getStateDefMap(); | ||
if (StateDef.empty()) { | ||
ORE->emit([&]() { | ||
return OptimizationRemarkMissed(DEBUG_TYPE, "SwitchNotPredictable", | ||
Switch) | ||
<< "Switch instruction is not predictable."; | ||
}); | ||
return; | ||
} | ||
|
||
auto *SwitchPhi = cast<PHINode>(Switch->getOperand(0)); | ||
auto *SwitchPhiDefBB = SwitchPhi->getParent(); | ||
VisitedBlocks VB; | ||
// Get paths from the determinator BBs to SwitchPhiDefBB | ||
std::vector<ThreadingPath> PathsToPhiDef = | ||
getPathsFromStateDefMap(StateDef, SwitchPhi, VB, MaxNumPaths); | ||
if (SwitchPhiDefBB == SwitchBlock || PathsToPhiDef.empty()) { | ||
TPaths = std::move(PathsToPhiDef); | ||
return; | ||
} | ||
|
||
assert(MaxNumPaths >= PathsToPhiDef.size() && !PathsToPhiDef.empty()); | ||
auto PathsLimit = MaxNumPaths / PathsToPhiDef.size(); | ||
// Find and append paths from SwitchPhiDefBB to SwitchBlock. | ||
PathsType PathsToSwitchBB = | ||
paths(SwitchPhiDefBB, SwitchBlock, VB, /* PathDepth = */ 1, PathsLimit); | ||
if (PathsToSwitchBB.empty()) | ||
return; | ||
|
||
std::vector<ThreadingPath> TempList; | ||
for (const ThreadingPath &Path : PathsToPhiDef) { | ||
for (const PathType &PathToSw : PathsToSwitchBB) { | ||
ThreadingPath PathCopy(Path); | ||
PathCopy.appendExcludingFirst(PathToSw); | ||
TempList.push_back(PathCopy); | ||
} | ||
} | ||
TPaths = std::move(TempList); | ||
} | ||
|
||
// Two states are equivalent if they have the same switch destination. | ||
// Unify the states in different threading path if the states are equivalent. | ||
void unifyTPaths() { | ||
llvm::SmallDenseMap<BasicBlock *, APInt> DestToState; | ||
for (ThreadingPath &Path : TPaths) { | ||
APInt NextState = Path.getExitValue(); | ||
BasicBlock *Dest = getNextCaseSuccessor(Switch, NextState); | ||
auto StateIt = DestToState.find(Dest); | ||
if (StateIt == DestToState.end()) { | ||
DestToState.insert({Dest, NextState}); | ||
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. Can avoid the duplicate lookup here by using try_emplace. 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. Thanks for the review. I will resolve these problems. |
||
continue; | ||
} | ||
|
||
if (NextState != StateIt->second) { | ||
LLVM_DEBUG(dbgs() << "Next state in " << Path << " is equivalent to " | ||
<< StateIt->second << "\n"); | ||
Path.setExitValue(StateIt->second); | ||
} | ||
} | ||
} | ||
|
||
unsigned NumVisited = 0; | ||
SwitchInst *Switch; | ||
BasicBlock *SwitchBlock; | ||
|
@@ -1339,21 +1386,6 @@ struct TransformDFA { | |
return It != ClonedBBs.end() ? (*It).BB : nullptr; | ||
} | ||
|
||
/// Helper to get the successor corresponding to a particular case value for | ||
/// a switch statement. | ||
BasicBlock *getNextCaseSuccessor(SwitchInst *Switch, const APInt &NextState) { | ||
BasicBlock *NextCase = nullptr; | ||
for (auto Case : Switch->cases()) { | ||
if (Case.getCaseValue()->getValue() == NextState) { | ||
NextCase = Case.getCaseSuccessor(); | ||
break; | ||
} | ||
} | ||
if (!NextCase) | ||
NextCase = Switch->getDefaultDest(); | ||
return NextCase; | ||
} | ||
|
||
/// Returns true if IncomingBB is a predecessor of BB. | ||
bool isPredecessor(BasicBlock *BB, BasicBlock *IncomingBB) { | ||
return llvm::is_contained(predecessors(BB), IncomingBB); | ||
|
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.
I realize you only moved this code, but it would probably make sense to compute the value => successor mapping once instead of scanning the whole switch each time.
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.
Yes, though it may not be the bottleneck, it's worth pre-computing.