7639d3f967f764daf1b5aed8a851b5ce09f4cd6a
[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 is distributed under the University of Illinois Open Source
6 // 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/Instructions.h"
22 #include "llvm/Type.h"
23 #include "llvm/ADT/SmallVector.h"
24 using namespace llvm;
25
26 char UnifyFunctionExitNodes::ID = 0;
27 static RegisterPass<UnifyFunctionExitNodes>
28 X("mergereturn", "Unify function exit nodes");
29
30 int UnifyFunctionExitNodes::stub;
31
32 Pass *llvm::createUnifyFunctionExitNodesPass() {
33   return new UnifyFunctionExitNodes();
34 }
35
36 void UnifyFunctionExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{
37   // We preserve the non-critical-edgeness property
38   AU.addPreservedID(BreakCriticalEdgesID);
39   // This is a cluster of orthogonal Transforms
40   AU.addPreservedID(PromoteMemoryToRegisterID);
41   AU.addPreservedID(LowerSwitchID);
42 }
43
44 // UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
45 // BasicBlock, and converting all returns to unconditional branches to this
46 // new basic block.  The singular exit node is returned.
47 //
48 // If there are no return stmts in the Function, a null pointer is returned.
49 //
50 bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
51   // Loop over all of the blocks in a function, tracking all of the blocks that
52   // return.
53   //
54   std::vector<BasicBlock*> ReturningBlocks;
55   std::vector<BasicBlock*> UnwindingBlocks;
56   std::vector<BasicBlock*> UnreachableBlocks;
57   for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
58     if (isa<ReturnInst>(I->getTerminator()))
59       ReturningBlocks.push_back(I);
60     else if (isa<UnwindInst>(I->getTerminator()))
61       UnwindingBlocks.push_back(I);
62     else if (isa<UnreachableInst>(I->getTerminator()))
63       UnreachableBlocks.push_back(I);
64
65   // Handle unwinding blocks first.
66   if (UnwindingBlocks.empty()) {
67     UnwindBlock = 0;
68   } else if (UnwindingBlocks.size() == 1) {
69     UnwindBlock = UnwindingBlocks.front();
70   } else {
71     UnwindBlock = new BasicBlock("UnifiedUnwindBlock", &F);
72     new UnwindInst(UnwindBlock);
73
74     for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(),
75            E = UnwindingBlocks.end(); I != E; ++I) {
76       BasicBlock *BB = *I;
77       BB->getInstList().pop_back();  // Remove the unwind insn
78       new BranchInst(UnwindBlock, BB);
79     }
80   }
81
82   // Then unreachable blocks.
83   if (UnreachableBlocks.empty()) {
84     UnreachableBlock = 0;
85   } else if (UnreachableBlocks.size() == 1) {
86     UnreachableBlock = UnreachableBlocks.front();
87   } else {
88     UnreachableBlock = new BasicBlock("UnifiedUnreachableBlock", &F);
89     new UnreachableInst(UnreachableBlock);
90
91     for (std::vector<BasicBlock*>::iterator I = UnreachableBlocks.begin(),
92            E = UnreachableBlocks.end(); I != E; ++I) {
93       BasicBlock *BB = *I;
94       BB->getInstList().pop_back();  // Remove the unreachable inst.
95       new BranchInst(UnreachableBlock, BB);
96     }
97   }
98
99   // Now handle return blocks.
100   if (ReturningBlocks.empty()) {
101     ReturnBlock = 0;
102     return false;                          // No blocks return
103   } else if (ReturningBlocks.size() == 1) {
104     ReturnBlock = ReturningBlocks.front(); // Already has a single return block
105     return false;
106   }
107
108   // Otherwise, we need to insert a new basic block into the function, add a PHI
109   // nodes (if the function returns values), and convert all of the return
110   // instructions into unconditional branches.
111   //
112   BasicBlock *NewRetBlock = new BasicBlock("UnifiedReturnBlock", &F);
113
114   SmallVector<Value *, 4> Phis;
115   unsigned NumRetVals = ReturningBlocks[0]->getTerminator()->getNumOperands();
116   if (NumRetVals == 0)
117     new ReturnInst(NULL, NewRetBlock);
118   else if (const StructType *STy = dyn_cast<StructType>(F.getReturnType())) {
119     for (unsigned i = 0; i < NumRetVals; ++i) {
120       PHINode *PN = new PHINode(STy->getElementType(i), "UnifiedRetVal");
121       NewRetBlock->getInstList().push_back(PN);
122       Phis.push_back(PN);
123     }
124     new ReturnInst(&Phis[0], NumRetVals);
125   }
126   else {
127     // If the function doesn't return void... add a PHI node to the block...
128     PHINode *PN = new PHINode(F.getReturnType(), "UnifiedRetVal");
129     NewRetBlock->getInstList().push_back(PN);
130     Phis.push_back(PN);
131     new ReturnInst(PN, NewRetBlock);
132   }
133
134   // Loop over all of the blocks, replacing the return instruction with an
135   // unconditional branch.
136   //
137   for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
138          E = ReturningBlocks.end(); I != E; ++I) {
139     BasicBlock *BB = *I;
140
141     // Add an incoming element to the PHI node for every return instruction that
142     // is merging into this new block...
143     if (!Phis.empty()) {
144       for (unsigned i = 0; i < NumRetVals; ++i) 
145         cast<PHINode>(Phis[i])->addIncoming(BB->getTerminator()->getOperand(i), 
146                                             BB);
147     }
148
149     BB->getInstList().pop_back();  // Remove the return insn
150     new BranchInst(NewRetBlock, BB);
151   }
152   ReturnBlock = NewRetBlock;
153   return true;
154 }