Skip to content

Commit 0016c1f

Browse files
[JumpThreading] Factor out common code to update the SSA form (NFC)
Summary: This patch factors out common code to update the SSA form in JumpThreading.cpp -- partly for readability and partly to facilitate an coming patch of my own. Reviewers: wmi Subscribers: hiraditya, jfb, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D69811
1 parent 77debf5 commit 0016c1f

File tree

2 files changed

+48
-75
lines changed

2 files changed

+48
-75
lines changed

llvm/include/llvm/Transforms/Scalar/JumpThreading.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ class JumpThreadingPass : public PassInfoMixin<JumpThreadingPass> {
109109

110110
void FindLoopHeaders(Function &F);
111111
bool ProcessBlock(BasicBlock *BB);
112+
void UpdateSSA(BasicBlock *BB, BasicBlock *NewBB,
113+
DenseMap<Instruction *, Value *> &ValueMapping);
112114
bool ThreadEdge(BasicBlock *BB, const SmallVectorImpl<BasicBlock *> &PredBBs,
113115
BasicBlock *SuccBB);
114116
bool DuplicateCondBranchOnPHIIntoPred(

llvm/lib/Transforms/Scalar/JumpThreading.cpp

Lines changed: 46 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,6 +1920,50 @@ static void AddPHINodeEntriesForMappedBlock(BasicBlock *PHIBB,
19201920
}
19211921
}
19221922

1923+
/// Update the SSA form. NewBB contains instructions that are copied from BB.
1924+
/// ValueMapping maps old values in BB to new ones in NewBB.
1925+
void JumpThreadingPass::UpdateSSA(
1926+
BasicBlock *BB, BasicBlock *NewBB,
1927+
DenseMap<Instruction *, Value *> &ValueMapping) {
1928+
// If there were values defined in BB that are used outside the block, then we
1929+
// now have to update all uses of the value to use either the original value,
1930+
// the cloned value, or some PHI derived value. This can require arbitrary
1931+
// PHI insertion, of which we are prepared to do, clean these up now.
1932+
SSAUpdater SSAUpdate;
1933+
SmallVector<Use *, 16> UsesToRename;
1934+
1935+
for (Instruction &I : *BB) {
1936+
// Scan all uses of this instruction to see if it is used outside of its
1937+
// block, and if so, record them in UsesToRename.
1938+
for (Use &U : I.uses()) {
1939+
Instruction *User = cast<Instruction>(U.getUser());
1940+
if (PHINode *UserPN = dyn_cast<PHINode>(User)) {
1941+
if (UserPN->getIncomingBlock(U) == BB)
1942+
continue;
1943+
} else if (User->getParent() == BB)
1944+
continue;
1945+
1946+
UsesToRename.push_back(&U);
1947+
}
1948+
1949+
// If there are no uses outside the block, we're done with this instruction.
1950+
if (UsesToRename.empty())
1951+
continue;
1952+
LLVM_DEBUG(dbgs() << "JT: Renaming non-local uses of: " << I << "\n");
1953+
1954+
// We found a use of I outside of BB. Rename all uses of I that are outside
1955+
// its block to be uses of the appropriate PHI node etc. See ValuesInBlocks
1956+
// with the two values we know.
1957+
SSAUpdate.Initialize(I.getType(), I.getName());
1958+
SSAUpdate.AddAvailableValue(BB, &I);
1959+
SSAUpdate.AddAvailableValue(NewBB, ValueMapping[&I]);
1960+
1961+
while (!UsesToRename.empty())
1962+
SSAUpdate.RewriteUse(*UsesToRename.pop_back_val());
1963+
LLVM_DEBUG(dbgs() << "\n");
1964+
}
1965+
}
1966+
19231967
/// ThreadEdge - We have decided that it is safe and profitable to factor the
19241968
/// blocks in PredBBs to one predecessor, then thread an edge from it to SuccBB
19251969
/// across BB. Transform the IR to reflect this change.
@@ -2045,44 +2089,7 @@ bool JumpThreadingPass::ThreadEdge(BasicBlock *BB,
20452089
{DominatorTree::Insert, PredBB, NewBB},
20462090
{DominatorTree::Delete, PredBB, BB}});
20472091

2048-
// If there were values defined in BB that are used outside the block, then we
2049-
// now have to update all uses of the value to use either the original value,
2050-
// the cloned value, or some PHI derived value. This can require arbitrary
2051-
// PHI insertion, of which we are prepared to do, clean these up now.
2052-
SSAUpdater SSAUpdate;
2053-
SmallVector<Use*, 16> UsesToRename;
2054-
2055-
for (Instruction &I : *BB) {
2056-
// Scan all uses of this instruction to see if their uses are no longer
2057-
// dominated by the previous def and if so, record them in UsesToRename.
2058-
// Also, skip phi operands from PredBB - we'll remove them anyway.
2059-
for (Use &U : I.uses()) {
2060-
Instruction *User = cast<Instruction>(U.getUser());
2061-
if (PHINode *UserPN = dyn_cast<PHINode>(User)) {
2062-
if (UserPN->getIncomingBlock(U) == BB)
2063-
continue;
2064-
} else if (User->getParent() == BB)
2065-
continue;
2066-
2067-
UsesToRename.push_back(&U);
2068-
}
2069-
2070-
// If there are no uses outside the block, we're done with this instruction.
2071-
if (UsesToRename.empty())
2072-
continue;
2073-
LLVM_DEBUG(dbgs() << "JT: Renaming non-local uses of: " << I << "\n");
2074-
2075-
// We found a use of I outside of BB. Rename all uses of I that are outside
2076-
// its block to be uses of the appropriate PHI node etc. See ValuesInBlocks
2077-
// with the two values we know.
2078-
SSAUpdate.Initialize(I.getType(), I.getName());
2079-
SSAUpdate.AddAvailableValue(BB, &I);
2080-
SSAUpdate.AddAvailableValue(NewBB, ValueMapping[&I]);
2081-
2082-
while (!UsesToRename.empty())
2083-
SSAUpdate.RewriteUse(*UsesToRename.pop_back_val());
2084-
LLVM_DEBUG(dbgs() << "\n");
2085-
}
2092+
UpdateSSA(BB, NewBB, ValueMapping);
20862093

20872094
// At this point, the IR is fully up to date and consistent. Do a quick scan
20882095
// over the new instructions and zap any that are constants or dead. This
@@ -2366,43 +2373,7 @@ bool JumpThreadingPass::DuplicateCondBranchOnPHIIntoPred(
23662373
AddPHINodeEntriesForMappedBlock(BBBranch->getSuccessor(1), BB, PredBB,
23672374
ValueMapping);
23682375

2369-
// If there were values defined in BB that are used outside the block, then we
2370-
// now have to update all uses of the value to use either the original value,
2371-
// the cloned value, or some PHI derived value. This can require arbitrary
2372-
// PHI insertion, of which we are prepared to do, clean these up now.
2373-
SSAUpdater SSAUpdate;
2374-
SmallVector<Use*, 16> UsesToRename;
2375-
for (Instruction &I : *BB) {
2376-
// Scan all uses of this instruction to see if it is used outside of its
2377-
// block, and if so, record them in UsesToRename.
2378-
for (Use &U : I.uses()) {
2379-
Instruction *User = cast<Instruction>(U.getUser());
2380-
if (PHINode *UserPN = dyn_cast<PHINode>(User)) {
2381-
if (UserPN->getIncomingBlock(U) == BB)
2382-
continue;
2383-
} else if (User->getParent() == BB)
2384-
continue;
2385-
2386-
UsesToRename.push_back(&U);
2387-
}
2388-
2389-
// If there are no uses outside the block, we're done with this instruction.
2390-
if (UsesToRename.empty())
2391-
continue;
2392-
2393-
LLVM_DEBUG(dbgs() << "JT: Renaming non-local uses of: " << I << "\n");
2394-
2395-
// We found a use of I outside of BB. Rename all uses of I that are outside
2396-
// its block to be uses of the appropriate PHI node etc. See ValuesInBlocks
2397-
// with the two values we know.
2398-
SSAUpdate.Initialize(I.getType(), I.getName());
2399-
SSAUpdate.AddAvailableValue(BB, &I);
2400-
SSAUpdate.AddAvailableValue(PredBB, ValueMapping[&I]);
2401-
2402-
while (!UsesToRename.empty())
2403-
SSAUpdate.RewriteUse(*UsesToRename.pop_back_val());
2404-
LLVM_DEBUG(dbgs() << "\n");
2405-
}
2376+
UpdateSSA(BB, PredBB, ValueMapping);
24062377

24072378
// PredBB no longer jumps to BB, remove entries in the PHI node for the edge
24082379
// that we nuked.

0 commit comments

Comments
 (0)