BasicBlockUtils.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. //===-- BasicBlockUtils.cpp - BasicBlock Utilities -------------------------==//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This family of functions perform manipulations on basic blocks, and
  11. // instructions contained within basic blocks.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  15. #include "llvm/Function.h"
  16. #include "llvm/Instructions.h"
  17. #include "llvm/IntrinsicInst.h"
  18. #include "llvm/Constant.h"
  19. #include "llvm/Type.h"
  20. #include "llvm/Analysis/AliasAnalysis.h"
  21. #include "llvm/Analysis/Dominators.h"
  22. #include "llvm/Analysis/LoopInfo.h"
  23. #include "llvm/Analysis/MemoryDependenceAnalysis.h"
  24. #include "llvm/Target/TargetData.h"
  25. #include "llvm/Transforms/Utils/Local.h"
  26. #include "llvm/Transforms/Scalar.h"
  27. #include "llvm/Support/ErrorHandling.h"
  28. #include "llvm/Support/ValueHandle.h"
  29. #include <algorithm>
  30. using namespace llvm;
  31. /// DeleteDeadBlock - Delete the specified block, which must have no
  32. /// predecessors.
  33. void llvm::DeleteDeadBlock(BasicBlock *BB) {
  34. assert((pred_begin(BB) == pred_end(BB) ||
  35. // Can delete self loop.
  36. BB->getSinglePredecessor() == BB) && "Block is not dead!");
  37. TerminatorInst *BBTerm = BB->getTerminator();
  38. // Loop through all of our successors and make sure they know that one
  39. // of their predecessors is going away.
  40. for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i)
  41. BBTerm->getSuccessor(i)->removePredecessor(BB);
  42. // Zap all the instructions in the block.
  43. while (!BB->empty()) {
  44. Instruction &I = BB->back();
  45. // If this instruction is used, replace uses with an arbitrary value.
  46. // Because control flow can't get here, we don't care what we replace the
  47. // value with. Note that since this block is unreachable, and all values
  48. // contained within it must dominate their uses, that all uses will
  49. // eventually be removed (they are themselves dead).
  50. if (!I.use_empty())
  51. I.replaceAllUsesWith(UndefValue::get(I.getType()));
  52. BB->getInstList().pop_back();
  53. }
  54. // Zap the block!
  55. BB->eraseFromParent();
  56. }
  57. /// FoldSingleEntryPHINodes - We know that BB has one predecessor. If there are
  58. /// any single-entry PHI nodes in it, fold them away. This handles the case
  59. /// when all entries to the PHI nodes in a block are guaranteed equal, such as
  60. /// when the block has exactly one predecessor.
  61. void llvm::FoldSingleEntryPHINodes(BasicBlock *BB, Pass *P) {
  62. if (!isa<PHINode>(BB->begin())) return;
  63. AliasAnalysis *AA = 0;
  64. MemoryDependenceAnalysis *MemDep = 0;
  65. if (P) {
  66. AA = P->getAnalysisIfAvailable<AliasAnalysis>();
  67. MemDep = P->getAnalysisIfAvailable<MemoryDependenceAnalysis>();
  68. }
  69. while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
  70. if (PN->getIncomingValue(0) != PN)
  71. PN->replaceAllUsesWith(PN->getIncomingValue(0));
  72. else
  73. PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
  74. if (MemDep)
  75. MemDep->removeInstruction(PN); // Memdep updates AA itself.
  76. else if (AA && isa<PointerType>(PN->getType()))
  77. AA->deleteValue(PN);
  78. PN->eraseFromParent();
  79. }
  80. }
  81. /// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it
  82. /// is dead. Also recursively delete any operands that become dead as
  83. /// a result. This includes tracing the def-use list from the PHI to see if
  84. /// it is ultimately unused or if it reaches an unused cycle.
  85. bool llvm::DeleteDeadPHIs(BasicBlock *BB) {
  86. // Recursively deleting a PHI may cause multiple PHIs to be deleted
  87. // or RAUW'd undef, so use an array of WeakVH for the PHIs to delete.
  88. SmallVector<WeakVH, 8> PHIs;
  89. for (BasicBlock::iterator I = BB->begin();
  90. PHINode *PN = dyn_cast<PHINode>(I); ++I)
  91. PHIs.push_back(PN);
  92. bool Changed = false;
  93. for (unsigned i = 0, e = PHIs.size(); i != e; ++i)
  94. if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i].operator Value*()))
  95. Changed |= RecursivelyDeleteDeadPHINode(PN);
  96. return Changed;
  97. }
  98. /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
  99. /// if possible. The return value indicates success or failure.
  100. bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, Pass *P) {
  101. // Don't merge away blocks who have their address taken.
  102. if (BB->hasAddressTaken()) return false;
  103. // Can't merge if there are multiple predecessors, or no predecessors.
  104. BasicBlock *PredBB = BB->getUniquePredecessor();
  105. if (!PredBB) return false;
  106. // Don't break self-loops.
  107. if (PredBB == BB) return false;
  108. // Don't break invokes.
  109. if (isa<InvokeInst>(PredBB->getTerminator())) return false;
  110. succ_iterator SI(succ_begin(PredBB)), SE(succ_end(PredBB));
  111. BasicBlock *OnlySucc = BB;
  112. for (; SI != SE; ++SI)
  113. if (*SI != OnlySucc) {
  114. OnlySucc = 0; // There are multiple distinct successors!
  115. break;
  116. }
  117. // Can't merge if there are multiple successors.
  118. if (!OnlySucc) return false;
  119. // Can't merge if there is PHI loop.
  120. for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; ++BI) {
  121. if (PHINode *PN = dyn_cast<PHINode>(BI)) {
  122. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
  123. if (PN->getIncomingValue(i) == PN)
  124. return false;
  125. } else
  126. break;
  127. }
  128. // Begin by getting rid of unneeded PHIs.
  129. if (isa<PHINode>(BB->front()))
  130. FoldSingleEntryPHINodes(BB, P);
  131. // Delete the unconditional branch from the predecessor...
  132. PredBB->getInstList().pop_back();
  133. // Move all definitions in the successor to the predecessor...
  134. PredBB->getInstList().splice(PredBB->end(), BB->getInstList());
  135. // Make all PHI nodes that referred to BB now refer to Pred as their
  136. // source...
  137. BB->replaceAllUsesWith(PredBB);
  138. // Inherit predecessors name if it exists.
  139. if (!PredBB->hasName())
  140. PredBB->takeName(BB);
  141. // Finally, erase the old block and update dominator info.
  142. if (P) {
  143. if (DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>()) {
  144. if (DomTreeNode *DTN = DT->getNode(BB)) {
  145. DomTreeNode *PredDTN = DT->getNode(PredBB);
  146. SmallVector<DomTreeNode*, 8> Children(DTN->begin(), DTN->end());
  147. for (SmallVector<DomTreeNode*, 8>::iterator DI = Children.begin(),
  148. DE = Children.end(); DI != DE; ++DI)
  149. DT->changeImmediateDominator(*DI, PredDTN);
  150. DT->eraseNode(BB);
  151. }
  152. if (LoopInfo *LI = P->getAnalysisIfAvailable<LoopInfo>())
  153. LI->removeBlock(BB);
  154. if (MemoryDependenceAnalysis *MD =
  155. P->getAnalysisIfAvailable<MemoryDependenceAnalysis>())
  156. MD->invalidateCachedPredecessors();
  157. }
  158. }
  159. BB->eraseFromParent();
  160. return true;
  161. }
  162. /// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
  163. /// with a value, then remove and delete the original instruction.
  164. ///
  165. void llvm::ReplaceInstWithValue(BasicBlock::InstListType &BIL,
  166. BasicBlock::iterator &BI, Value *V) {
  167. Instruction &I = *BI;
  168. // Replaces all of the uses of the instruction with uses of the value
  169. I.replaceAllUsesWith(V);
  170. // Make sure to propagate a name if there is one already.
  171. if (I.hasName() && !V->hasName())
  172. V->takeName(&I);
  173. // Delete the unnecessary instruction now...
  174. BI = BIL.erase(BI);
  175. }
  176. /// ReplaceInstWithInst - Replace the instruction specified by BI with the
  177. /// instruction specified by I. The original instruction is deleted and BI is
  178. /// updated to point to the new instruction.
  179. ///
  180. void llvm::ReplaceInstWithInst(BasicBlock::InstListType &BIL,
  181. BasicBlock::iterator &BI, Instruction *I) {
  182. assert(I->getParent() == 0 &&
  183. "ReplaceInstWithInst: Instruction already inserted into basic block!");
  184. // Insert the new instruction into the basic block...
  185. BasicBlock::iterator New = BIL.insert(BI, I);
  186. // Replace all uses of the old instruction, and delete it.
  187. ReplaceInstWithValue(BIL, BI, I);
  188. // Move BI back to point to the newly inserted instruction
  189. BI = New;
  190. }
  191. /// ReplaceInstWithInst - Replace the instruction specified by From with the
  192. /// instruction specified by To.
  193. ///
  194. void llvm::ReplaceInstWithInst(Instruction *From, Instruction *To) {
  195. BasicBlock::iterator BI(From);
  196. ReplaceInstWithInst(From->getParent()->getInstList(), BI, To);
  197. }
  198. /// GetSuccessorNumber - Search for the specified successor of basic block BB
  199. /// and return its position in the terminator instruction's list of
  200. /// successors. It is an error to call this with a block that is not a
  201. /// successor.
  202. unsigned llvm::GetSuccessorNumber(BasicBlock *BB, BasicBlock *Succ) {
  203. TerminatorInst *Term = BB->getTerminator();
  204. #ifndef NDEBUG
  205. unsigned e = Term->getNumSuccessors();
  206. #endif
  207. for (unsigned i = 0; ; ++i) {
  208. assert(i != e && "Didn't find edge?");
  209. if (Term->getSuccessor(i) == Succ)
  210. return i;
  211. }
  212. return 0;
  213. }
  214. /// SplitEdge - Split the edge connecting specified block. Pass P must
  215. /// not be NULL.
  216. BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, Pass *P) {
  217. unsigned SuccNum = GetSuccessorNumber(BB, Succ);
  218. // If this is a critical edge, let SplitCriticalEdge do it.
  219. TerminatorInst *LatchTerm = BB->getTerminator();
  220. if (SplitCriticalEdge(LatchTerm, SuccNum, P))
  221. return LatchTerm->getSuccessor(SuccNum);
  222. // If the edge isn't critical, then BB has a single successor or Succ has a
  223. // single pred. Split the block.
  224. BasicBlock::iterator SplitPoint;
  225. if (BasicBlock *SP = Succ->getSinglePredecessor()) {
  226. // If the successor only has a single pred, split the top of the successor
  227. // block.
  228. assert(SP == BB && "CFG broken");
  229. SP = NULL;
  230. return SplitBlock(Succ, Succ->begin(), P);
  231. }
  232. // Otherwise, if BB has a single successor, split it at the bottom of the
  233. // block.
  234. assert(BB->getTerminator()->getNumSuccessors() == 1 &&
  235. "Should have a single succ!");
  236. return SplitBlock(BB, BB->getTerminator(), P);
  237. }
  238. /// SplitBlock - Split the specified block at the specified instruction - every
  239. /// thing before SplitPt stays in Old and everything starting with SplitPt moves
  240. /// to a new block. The two blocks are joined by an unconditional branch and
  241. /// the loop info is updated.
  242. ///
  243. BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P) {
  244. BasicBlock::iterator SplitIt = SplitPt;
  245. while (isa<PHINode>(SplitIt))
  246. ++SplitIt;
  247. BasicBlock *New = Old->splitBasicBlock(SplitIt, Old->getName()+".split");
  248. // The new block lives in whichever loop the old one did. This preserves
  249. // LCSSA as well, because we force the split point to be after any PHI nodes.
  250. if (LoopInfo *LI = P->getAnalysisIfAvailable<LoopInfo>())
  251. if (Loop *L = LI->getLoopFor(Old))
  252. L->addBasicBlockToLoop(New, LI->getBase());
  253. if (DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>()) {
  254. // Old dominates New. New node dominates all other nodes dominated by Old.
  255. DomTreeNode *OldNode = DT->getNode(Old);
  256. std::vector<DomTreeNode *> Children;
  257. for (DomTreeNode::iterator I = OldNode->begin(), E = OldNode->end();
  258. I != E; ++I)
  259. Children.push_back(*I);
  260. DomTreeNode *NewNode = DT->addNewBlock(New,Old);
  261. for (std::vector<DomTreeNode *>::iterator I = Children.begin(),
  262. E = Children.end(); I != E; ++I)
  263. DT->changeImmediateDominator(*I, NewNode);
  264. }
  265. return New;
  266. }
  267. /// SplitBlockPredecessors - This method transforms BB by introducing a new
  268. /// basic block into the function, and moving some of the predecessors of BB to
  269. /// be predecessors of the new block. The new predecessors are indicated by the
  270. /// Preds array, which has NumPreds elements in it. The new block is given a
  271. /// suffix of 'Suffix'.
  272. ///
  273. /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
  274. /// LoopInfo, and LCCSA but no other analyses. In particular, it does not
  275. /// preserve LoopSimplify (because it's complicated to handle the case where one
  276. /// of the edges being split is an exit of a loop with other exits).
  277. ///
  278. BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB,
  279. BasicBlock *const *Preds,
  280. unsigned NumPreds, const char *Suffix,
  281. Pass *P) {
  282. // Create new basic block, insert right before the original block.
  283. BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), BB->getName()+Suffix,
  284. BB->getParent(), BB);
  285. // The new block unconditionally branches to the old block.
  286. BranchInst *BI = BranchInst::Create(BB, NewBB);
  287. LoopInfo *LI = P ? P->getAnalysisIfAvailable<LoopInfo>() : 0;
  288. Loop *L = LI ? LI->getLoopFor(BB) : 0;
  289. bool PreserveLCSSA = P->mustPreserveAnalysisID(LCSSAID);
  290. // Move the edges from Preds to point to NewBB instead of BB.
  291. // While here, if we need to preserve loop analyses, collect
  292. // some information about how this split will affect loops.
  293. bool HasLoopExit = false;
  294. bool IsLoopEntry = !!L;
  295. bool SplitMakesNewLoopHeader = false;
  296. for (unsigned i = 0; i != NumPreds; ++i) {
  297. // This is slightly more strict than necessary; the minimum requirement
  298. // is that there be no more than one indirectbr branching to BB. And
  299. // all BlockAddress uses would need to be updated.
  300. assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) &&
  301. "Cannot split an edge from an IndirectBrInst");
  302. Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB);
  303. if (LI) {
  304. // If we need to preserve LCSSA, determine if any of
  305. // the preds is a loop exit.
  306. if (PreserveLCSSA)
  307. if (Loop *PL = LI->getLoopFor(Preds[i]))
  308. if (!PL->contains(BB))
  309. HasLoopExit = true;
  310. // If we need to preserve LoopInfo, note whether any of the
  311. // preds crosses an interesting loop boundary.
  312. if (L) {
  313. if (L->contains(Preds[i]))
  314. IsLoopEntry = false;
  315. else
  316. SplitMakesNewLoopHeader = true;
  317. }
  318. }
  319. }
  320. // Update dominator tree if available.
  321. DominatorTree *DT = P ? P->getAnalysisIfAvailable<DominatorTree>() : 0;
  322. if (DT)
  323. DT->splitBlock(NewBB);
  324. // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI
  325. // node becomes an incoming value for BB's phi node. However, if the Preds
  326. // list is empty, we need to insert dummy entries into the PHI nodes in BB to
  327. // account for the newly created predecessor.
  328. if (NumPreds == 0) {
  329. // Insert dummy values as the incoming value.
  330. for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I)
  331. cast<PHINode>(I)->addIncoming(UndefValue::get(I->getType()), NewBB);
  332. return NewBB;
  333. }
  334. AliasAnalysis *AA = P ? P->getAnalysisIfAvailable<AliasAnalysis>() : 0;
  335. if (L) {
  336. if (IsLoopEntry) {
  337. // Add the new block to the nearest enclosing loop (and not an
  338. // adjacent loop). To find this, examine each of the predecessors and
  339. // determine which loops enclose them, and select the most-nested loop
  340. // which contains the loop containing the block being split.
  341. Loop *InnermostPredLoop = 0;
  342. for (unsigned i = 0; i != NumPreds; ++i)
  343. if (Loop *PredLoop = LI->getLoopFor(Preds[i])) {
  344. // Seek a loop which actually contains the block being split (to
  345. // avoid adjacent loops).
  346. while (PredLoop && !PredLoop->contains(BB))
  347. PredLoop = PredLoop->getParentLoop();
  348. // Select the most-nested of these loops which contains the block.
  349. if (PredLoop &&
  350. PredLoop->contains(BB) &&
  351. (!InnermostPredLoop ||
  352. InnermostPredLoop->getLoopDepth() < PredLoop->getLoopDepth()))
  353. InnermostPredLoop = PredLoop;
  354. }
  355. if (InnermostPredLoop)
  356. InnermostPredLoop->addBasicBlockToLoop(NewBB, LI->getBase());
  357. } else {
  358. L->addBasicBlockToLoop(NewBB, LI->getBase());
  359. if (SplitMakesNewLoopHeader)
  360. L->moveToHeader(NewBB);
  361. }
  362. }
  363. // Otherwise, create a new PHI node in NewBB for each PHI node in BB.
  364. for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ) {
  365. PHINode *PN = cast<PHINode>(I++);
  366. // Check to see if all of the values coming in are the same. If so, we
  367. // don't need to create a new PHI node, unless it's needed for LCSSA.
  368. Value *InVal = 0;
  369. if (!HasLoopExit) {
  370. InVal = PN->getIncomingValueForBlock(Preds[0]);
  371. for (unsigned i = 1; i != NumPreds; ++i)
  372. if (InVal != PN->getIncomingValueForBlock(Preds[i])) {
  373. InVal = 0;
  374. break;
  375. }
  376. }
  377. if (InVal) {
  378. // If all incoming values for the new PHI would be the same, just don't
  379. // make a new PHI. Instead, just remove the incoming values from the old
  380. // PHI.
  381. for (unsigned i = 0; i != NumPreds; ++i)
  382. PN->removeIncomingValue(Preds[i], false);
  383. } else {
  384. // If the values coming into the block are not the same, we need a PHI.
  385. // Create the new PHI node, insert it into NewBB at the end of the block
  386. PHINode *NewPHI =
  387. PHINode::Create(PN->getType(), PN->getName()+".ph", BI);
  388. NewPHI->reserveOperandSpace(NumPreds);
  389. if (AA) AA->copyValue(PN, NewPHI);
  390. // Move all of the PHI values for 'Preds' to the new PHI.
  391. for (unsigned i = 0; i != NumPreds; ++i) {
  392. Value *V = PN->removeIncomingValue(Preds[i], false);
  393. NewPHI->addIncoming(V, Preds[i]);
  394. }
  395. InVal = NewPHI;
  396. }
  397. // Add an incoming value to the PHI node in the loop for the preheader
  398. // edge.
  399. PN->addIncoming(InVal, NewBB);
  400. }
  401. return NewBB;
  402. }
  403. /// FindFunctionBackedges - Analyze the specified function to find all of the
  404. /// loop backedges in the function and return them. This is a relatively cheap
  405. /// (compared to computing dominators and loop info) analysis.
  406. ///
  407. /// The output is added to Result, as pairs of <from,to> edge info.
  408. void llvm::FindFunctionBackedges(const Function &F,
  409. SmallVectorImpl<std::pair<const BasicBlock*,const BasicBlock*> > &Result) {
  410. const BasicBlock *BB = &F.getEntryBlock();
  411. if (succ_begin(BB) == succ_end(BB))
  412. return;
  413. SmallPtrSet<const BasicBlock*, 8> Visited;
  414. SmallVector<std::pair<const BasicBlock*, succ_const_iterator>, 8> VisitStack;
  415. SmallPtrSet<const BasicBlock*, 8> InStack;
  416. Visited.insert(BB);
  417. VisitStack.push_back(std::make_pair(BB, succ_begin(BB)));
  418. InStack.insert(BB);
  419. do {
  420. std::pair<const BasicBlock*, succ_const_iterator> &Top = VisitStack.back();
  421. const BasicBlock *ParentBB = Top.first;
  422. succ_const_iterator &I = Top.second;
  423. bool FoundNew = false;
  424. while (I != succ_end(ParentBB)) {
  425. BB = *I++;
  426. if (Visited.insert(BB)) {
  427. FoundNew = true;
  428. break;
  429. }
  430. // Successor is in VisitStack, it's a back edge.
  431. if (InStack.count(BB))
  432. Result.push_back(std::make_pair(ParentBB, BB));
  433. }
  434. if (FoundNew) {
  435. // Go down one level if there is a unvisited successor.
  436. InStack.insert(BB);
  437. VisitStack.push_back(std::make_pair(BB, succ_begin(BB)));
  438. } else {
  439. // Go up one level.
  440. InStack.erase(VisitStack.pop_back_val().first);
  441. }
  442. } while (!VisitStack.empty());
  443. }
  444. /// FoldReturnIntoUncondBranch - This method duplicates the specified return
  445. /// instruction into a predecessor which ends in an unconditional branch. If
  446. /// the return instruction returns a value defined by a PHI, propagate the
  447. /// right value into the return. It returns the new return instruction in the
  448. /// predecessor.
  449. ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
  450. BasicBlock *Pred) {
  451. Instruction *UncondBranch = Pred->getTerminator();
  452. // Clone the return and add it to the end of the predecessor.
  453. Instruction *NewRet = RI->clone();
  454. Pred->getInstList().push_back(NewRet);
  455. // If the return instruction returns a value, and if the value was a
  456. // PHI node in "BB", propagate the right value into the return.
  457. for (User::op_iterator i = NewRet->op_begin(), e = NewRet->op_end();
  458. i != e; ++i)
  459. if (PHINode *PN = dyn_cast<PHINode>(*i))
  460. if (PN->getParent() == BB)
  461. *i = PN->getIncomingValueForBlock(Pred);
  462. // Update any PHI nodes in the returning block to realize that we no
  463. // longer branch to them.
  464. BB->removePredecessor(Pred);
  465. UncondBranch->eraseFromParent();
  466. return cast<ReturnInst>(NewRet);
  467. }