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