Finally, add the required constraint checks to fix Transforms/SimplifyCFG/2005-08...
[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/Instructions.h"
22 #include "llvm/Type.h"
23 using namespace llvm;
24
25 static RegisterOpt<UnifyFunctionExitNodes>
26 X("mergereturn", "Unify function exit nodes");
27
28 Pass *llvm::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   std::vector<BasicBlock*> UnreachableBlocks;
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     else if (isa<UnreachableInst>(I->getTerminator()))
56       UnreachableBlocks.push_back(I);
57
58   // Handle unwinding blocks first.
59   if (UnwindingBlocks.empty()) {
60     UnwindBlock = 0;
61   } else if (UnwindingBlocks.size() == 1) {
62     UnwindBlock = UnwindingBlocks.front();
63   } else {
64     UnwindBlock = new BasicBlock("UnifiedUnwindBlock", &F);
65     new UnwindInst(UnwindBlock);
66
67     for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(),
68            E = UnwindingBlocks.end(); I != E; ++I) {
69       BasicBlock *BB = *I;
70       BB->getInstList().pop_back();  // Remove the unwind insn
71       new BranchInst(UnwindBlock, BB);
72     }
73   }
74
75   // Then unreachable blocks.
76   if (UnreachableBlocks.empty()) {
77     UnreachableBlock = 0;
78   } else if (UnreachableBlocks.size() == 1) {
79     UnreachableBlock = UnreachableBlocks.front();
80   } else {
81     UnreachableBlock = new BasicBlock("UnifiedUnreachableBlock", &F);
82     new UnreachableInst(UnreachableBlock);
83
84     for (std::vector<BasicBlock*>::iterator I = UnreachableBlocks.begin(),
85            E = UnreachableBlocks.end(); I != E; ++I) {
86       BasicBlock *BB = *I;
87       BB->getInstList().pop_back();  // Remove the unreachable inst.
88       new BranchInst(UnreachableBlock, BB);
89     }
90   }
91
92   // Now handle return blocks.
93   if (ReturningBlocks.empty()) {
94     ReturnBlock = 0;
95     return false;                          // No blocks return
96   } else if (ReturningBlocks.size() == 1) {
97     ReturnBlock = ReturningBlocks.front(); // Already has a single return block
98     return false;
99   }
100
101   // Otherwise, we need to insert a new basic block into the function, add a PHI
102   // node (if the function returns a value), and convert all of the return
103   // instructions into unconditional branches.
104   //
105   BasicBlock *NewRetBlock = new BasicBlock("UnifiedReturnBlock", &F);
106
107   PHINode *PN = 0;
108   if (F.getReturnType() != Type::VoidTy) {
109     // If the function doesn't return void... add a PHI node to the block...
110     PN = new PHINode(F.getReturnType(), "UnifiedRetVal");
111     NewRetBlock->getInstList().push_back(PN);
112   }
113   new ReturnInst(PN, NewRetBlock);
114
115   // Loop over all of the blocks, replacing the return instruction with an
116   // unconditional branch.
117   //
118   for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
119          E = ReturningBlocks.end(); I != E; ++I) {
120     BasicBlock *BB = *I;
121
122     // Add an incoming element to the PHI node for every return instruction that
123     // is merging into this new block...
124     if (PN) PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
125
126     BB->getInstList().pop_back();  // Remove the return insn
127     new BranchInst(NewRetBlock, BB);
128   }
129   ReturnBlock = NewRetBlock;
130   return true;
131 }