@@ -1920,6 +1920,50 @@ static void AddPHINodeEntriesForMappedBlock(BasicBlock *PHIBB,
1920
1920
}
1921
1921
}
1922
1922
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
+
1923
1967
// / ThreadEdge - We have decided that it is safe and profitable to factor the
1924
1968
// / blocks in PredBBs to one predecessor, then thread an edge from it to SuccBB
1925
1969
// / across BB. Transform the IR to reflect this change.
@@ -2045,44 +2089,7 @@ bool JumpThreadingPass::ThreadEdge(BasicBlock *BB,
2045
2089
{DominatorTree::Insert, PredBB, NewBB},
2046
2090
{DominatorTree::Delete, PredBB, BB}});
2047
2091
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);
2086
2093
2087
2094
// At this point, the IR is fully up to date and consistent. Do a quick scan
2088
2095
// over the new instructions and zap any that are constants or dead. This
@@ -2366,43 +2373,7 @@ bool JumpThreadingPass::DuplicateCondBranchOnPHIIntoPred(
2366
2373
AddPHINodeEntriesForMappedBlock (BBBranch->getSuccessor (1 ), BB, PredBB,
2367
2374
ValueMapping);
2368
2375
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);
2406
2377
2407
2378
// PredBB no longer jumps to BB, remove entries in the PHI node for the edge
2408
2379
// that we nuked.
0 commit comments