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