* We now preserve the no-critical-edge pass (because we cannot insert critical edges)
[oota-llvm.git] / lib / Transforms / Utils / UnifyFunctionExitNodes.cpp
1 //===- UnifyFunctionExitNodes.cpp - Make all functions have a single exit -===//
2 //
3 // This pass is used to ensure that functions have at most one return
4 // instruction in them.  Additionally, it keeps track of which node is the new
5 // exit node of the CFG.  If there are no exit nodes in the CFG, the getExitNode
6 // method will return a null pointer.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
11 #include "llvm/Transforms/Scalar.h"
12 #include "llvm/BasicBlock.h"
13 #include "llvm/Function.h"
14 #include "llvm/iTerminators.h"
15 #include "llvm/iPHINode.h"
16 #include "llvm/Type.h"
17 using std::vector;
18
19 static RegisterOpt<UnifyFunctionExitNodes>
20 X("mergereturn", "Unify function exit nodes");
21
22 void UnifyFunctionExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{
23   // We preserve the non-critical-edgeness property
24   AU.addPreservedID(BreakCriticalEdgesID);
25 }
26
27 // UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
28 // BasicBlock, and converting all returns to unconditional branches to this
29 // new basic block.  The singular exit node is returned.
30 //
31 // If there are no return stmts in the Function, a null pointer is returned.
32 //
33 bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
34   // Loop over all of the blocks in a function, tracking all of the blocks that
35   // return.
36   //
37   vector<BasicBlock*> ReturningBlocks;
38   for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
39     if (isa<ReturnInst>(I->getTerminator()))
40       ReturningBlocks.push_back(I);
41
42   if (ReturningBlocks.empty()) {
43     ExitNode = 0;
44     return false;                          // No blocks return
45   } else if (ReturningBlocks.size() == 1) {
46     ExitNode = ReturningBlocks.front();    // Already has a single return block
47     return false;
48   }
49
50   // Otherwise, we need to insert a new basic block into the function, add a PHI
51   // node (if the function returns a value), and convert all of the return 
52   // instructions into unconditional branches.
53   //
54   BasicBlock *NewRetBlock = new BasicBlock("UnifiedExitNode", &F);
55
56   PHINode *PN = 0;
57   if (F.getReturnType() != Type::VoidTy) {
58     // If the function doesn't return void... add a PHI node to the block...
59     PN = new PHINode(F.getReturnType(), "UnifiedRetVal");
60     NewRetBlock->getInstList().push_back(PN);
61     NewRetBlock->getInstList().push_back(new ReturnInst(PN));
62   } else {
63     // If it returns void, just add a return void instruction to the block
64     NewRetBlock->getInstList().push_back(new ReturnInst());
65   }
66
67   // Loop over all of the blocks, replacing the return instruction with an
68   // unconditional branch.
69   //
70   for (vector<BasicBlock*>::iterator I = ReturningBlocks.begin(), 
71                                      E = ReturningBlocks.end(); I != E; ++I) {
72     BasicBlock *BB = *I;
73
74     // Add an incoming element to the PHI node for every return instruction that
75     // is merging into this new block...
76     if (PN) PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
77
78     BB->getInstList().pop_back();  // Remove the return insn
79     BB->getInstList().push_back(new BranchInst(NewRetBlock));
80   }
81   ExitNode = NewRetBlock;
82
83   return true;
84 }