71f286c36e5fe8147ade8c33a10e999dabe2b9b2
[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
18 static RegisterOpt<UnifyFunctionExitNodes>
19 X("mergereturn", "Unify function exit nodes");
20
21 void UnifyFunctionExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{
22   // We preserve the non-critical-edgeness property
23   AU.addPreservedID(BreakCriticalEdgesID);
24 }
25
26 // UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
27 // BasicBlock, and converting all returns to unconditional branches to this
28 // new basic block.  The singular exit node is returned.
29 //
30 // If there are no return stmts in the Function, a null pointer is returned.
31 //
32 bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
33   // Loop over all of the blocks in a function, tracking all of the blocks that
34   // return.
35   //
36   std::vector<BasicBlock*> ReturningBlocks;
37   for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
38     if (isa<ReturnInst>(I->getTerminator()))
39       ReturningBlocks.push_back(I);
40
41   if (ReturningBlocks.empty()) {
42     ExitNode = 0;
43     return false;                          // No blocks return
44   } else if (ReturningBlocks.size() == 1) {
45     ExitNode = ReturningBlocks.front();    // Already has a single return block
46     return false;
47   }
48
49   // Otherwise, we need to insert a new basic block into the function, add a PHI
50   // node (if the function returns a value), and convert all of the return 
51   // instructions into unconditional branches.
52   //
53   BasicBlock *NewRetBlock = new BasicBlock("UnifiedExitNode", &F);
54
55   PHINode *PN = 0;
56   if (F.getReturnType() != Type::VoidTy) {
57     // If the function doesn't return void... add a PHI node to the block...
58     PN = new PHINode(F.getReturnType(), "UnifiedRetVal");
59     NewRetBlock->getInstList().push_back(PN);
60     NewRetBlock->getInstList().push_back(new ReturnInst(PN));
61   } else {
62     // If it returns void, just add a return void instruction to the block
63     NewRetBlock->getInstList().push_back(new ReturnInst());
64   }
65
66   // Loop over all of the blocks, replacing the return instruction with an
67   // unconditional branch.
68   //
69   for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(), 
70          E = ReturningBlocks.end(); I != E; ++I) {
71     BasicBlock *BB = *I;
72
73     // Add an incoming element to the PHI node for every return instruction that
74     // is merging into this new block...
75     if (PN) PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
76
77     BB->getInstList().pop_back();  // Remove the return insn
78     BB->getInstList().push_back(new BranchInst(NewRetBlock));
79   }
80   ExitNode = NewRetBlock;
81
82   return true;
83 }