Delete dead line
[oota-llvm.git] / lib / Transforms / Utils / UnifyFunctionExitNodes.cpp
1 //===- UnifyFunctionExitNodes.cpp - Make all functions have a single exit -===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass is used to ensure that functions have at most one return
11 // instruction in them.  Additionally, it keeps track of which node is the new
12 // exit node of the CFG.  If there are no exit nodes in the CFG, the getExitNode
13 // method will return a null pointer.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
18 #include "llvm/Transforms/Scalar.h"
19 #include "llvm/BasicBlock.h"
20 #include "llvm/Function.h"
21 #include "llvm/iTerminators.h"
22 #include "llvm/iPHINode.h"
23 #include "llvm/Type.h"
24 using namespace llvm;
25
26 static RegisterOpt<UnifyFunctionExitNodes>
27 X("mergereturn", "Unify function exit nodes");
28
29 Pass *llvm::createUnifyFunctionExitNodesPass() {
30   return new UnifyFunctionExitNodes();
31 }
32
33 void UnifyFunctionExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{
34   // We preserve the non-critical-edgeness property
35   AU.addPreservedID(BreakCriticalEdgesID);
36 }
37
38 // UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
39 // BasicBlock, and converting all returns to unconditional branches to this
40 // new basic block.  The singular exit node is returned.
41 //
42 // If there are no return stmts in the Function, a null pointer is returned.
43 //
44 bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
45   // Loop over all of the blocks in a function, tracking all of the blocks that
46   // return.
47   //
48   std::vector<BasicBlock*> ReturningBlocks;
49   std::vector<BasicBlock*> UnwindingBlocks;
50   for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
51     if (isa<ReturnInst>(I->getTerminator()))
52       ReturningBlocks.push_back(I);
53     else if (isa<UnwindInst>(I->getTerminator()))
54       UnwindingBlocks.push_back(I);
55
56   // Handle unwinding blocks first...
57   if (UnwindingBlocks.empty()) {
58     UnwindBlock = 0;
59   } else if (UnwindingBlocks.size() == 1) {
60     UnwindBlock = UnwindingBlocks.front();
61   } else {
62     UnwindBlock = new BasicBlock("UnifiedUnwindBlock", &F);
63     new UnwindInst(UnwindBlock);
64
65     for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(), 
66            E = UnwindingBlocks.end(); I != E; ++I) {
67       BasicBlock *BB = *I;
68       BB->getInstList().pop_back();  // Remove the return insn
69       new BranchInst(UnwindBlock, 0, 0, BB);
70     }
71   }
72
73   // Now handle return blocks...
74   if (ReturningBlocks.empty()) {
75     ReturnBlock = 0;
76     return false;                          // No blocks return
77   } else if (ReturningBlocks.size() == 1) {
78     ReturnBlock = ReturningBlocks.front(); // Already has a single return block
79     return false;
80   }
81
82   // Otherwise, we need to insert a new basic block into the function, add a PHI
83   // node (if the function returns a value), and convert all of the return 
84   // instructions into unconditional branches.
85   //
86   BasicBlock *NewRetBlock = new BasicBlock("UnifiedReturnBlock", &F);
87
88   PHINode *PN = 0;
89   if (F.getReturnType() != Type::VoidTy) {
90     // If the function doesn't return void... add a PHI node to the block...
91     PN = new PHINode(F.getReturnType(), "UnifiedRetVal");
92     NewRetBlock->getInstList().push_back(PN);
93   }
94   new ReturnInst(PN, NewRetBlock);
95
96   // Loop over all of the blocks, replacing the return instruction with an
97   // unconditional branch.
98   //
99   for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(), 
100          E = ReturningBlocks.end(); I != E; ++I) {
101     BasicBlock *BB = *I;
102
103     // Add an incoming element to the PHI node for every return instruction that
104     // is merging into this new block...
105     if (PN) PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
106
107     BB->getInstList().pop_back();  // Remove the return insn
108     new BranchInst(NewRetBlock, BB);
109   }
110   ReturnBlock = NewRetBlock;
111   return true;
112 }