Start using the nicer terminator auto-insertion API
[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
25 namespace llvm {
26
27 static RegisterOpt<UnifyFunctionExitNodes>
28 X("mergereturn", "Unify function exit nodes");
29
30 Pass *createUnifyFunctionExitNodesPass() {
31   return new UnifyFunctionExitNodes();
32 }
33
34 void UnifyFunctionExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{
35   // We preserve the non-critical-edgeness property
36   AU.addPreservedID(BreakCriticalEdgesID);
37 }
38
39 // UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
40 // BasicBlock, and converting all returns to unconditional branches to this
41 // new basic block.  The singular exit node is returned.
42 //
43 // If there are no return stmts in the Function, a null pointer is returned.
44 //
45 bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
46   // Loop over all of the blocks in a function, tracking all of the blocks that
47   // return.
48   //
49   std::vector<BasicBlock*> ReturningBlocks;
50   std::vector<BasicBlock*> UnwindingBlocks;
51   for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
52     if (isa<ReturnInst>(I->getTerminator()))
53       ReturningBlocks.push_back(I);
54     else if (isa<UnwindInst>(I->getTerminator()))
55       UnwindingBlocks.push_back(I);
56
57   // Handle unwinding blocks first...
58   if (UnwindingBlocks.empty()) {
59     UnwindBlock = 0;
60   } else if (UnwindingBlocks.size() == 1) {
61     UnwindBlock = UnwindingBlocks.front();
62   } else {
63     UnwindBlock = new BasicBlock("UnifiedUnwindBlock", &F);
64     new UnwindInst(UnwindBlock);
65
66     for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(), 
67            E = UnwindingBlocks.end(); I != E; ++I) {
68       BasicBlock *BB = *I;
69       BB->getInstList().pop_back();  // Remove the return insn
70       new BranchInst(UnwindBlock, 0, 0, BB);
71     }
72   }
73
74   // Now handle return blocks...
75   if (ReturningBlocks.empty()) {
76     ReturnBlock = 0;
77     return false;                          // No blocks return
78   } else if (ReturningBlocks.size() == 1) {
79     ReturnBlock = ReturningBlocks.front(); // Already has a single return block
80     return false;
81   }
82
83   // Otherwise, we need to insert a new basic block into the function, add a PHI
84   // node (if the function returns a value), and convert all of the return 
85   // instructions into unconditional branches.
86   //
87   BasicBlock *NewRetBlock = new BasicBlock("UnifiedReturnBlock", &F);
88
89   PHINode *PN = 0;
90   if (F.getReturnType() != Type::VoidTy) {
91     // If the function doesn't return void... add a PHI node to the block...
92     PN = new PHINode(F.getReturnType(), "UnifiedRetVal");
93     NewRetBlock->getInstList().push_back(PN);
94     new ReturnInst(PN, NewRetBlock);
95   } else {
96     // If it returns void, just add a return void instruction to the block
97     new ReturnInst(0, NewRetBlock);
98   }
99
100   // Loop over all of the blocks, replacing the return instruction with an
101   // unconditional branch.
102   //
103   for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(), 
104          E = ReturningBlocks.end(); I != E; ++I) {
105     BasicBlock *BB = *I;
106
107     // Add an incoming element to the PHI node for every return instruction that
108     // is merging into this new block...
109     if (PN) PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
110
111     BB->getInstList().pop_back();  // Remove the return insn
112     new BranchInst(NewRetBlock, 0, 0, BB);
113   }
114   ReturnBlock = NewRetBlock;
115   return true;
116 }
117
118 } // End llvm namespace