Pull RaiseAllocations stuff out of the CleanGCC pass into it's own pass in
[oota-llvm.git] / lib / Transforms / Utils / UnifyFunctionExitNodes.cpp
1 //===- SimplifyCFG.cpp - CFG Simplification Routines -------------*- C++ -*--=//
2 //
3 // This file provides several routines that are useful for simplifying CFGs in
4 // various ways...
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "llvm/Analysis/SimplifyCFG.h"
9 #include "llvm/BasicBlock.h"
10 #include "llvm/Method.h"
11 #include "llvm/iTerminators.h"
12 #include "llvm/iPHINode.h"
13 #include "llvm/Type.h"
14 using std::vector;
15
16 // UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
17 // BasicBlock, and converting all returns to unconditional branches to this
18 // new basic block.  The singular exit node is returned.
19 //
20 // If there are no return stmts in the Method, a null pointer is returned.
21 //
22 BasicBlock *cfg::UnifyAllExitNodes(Method *M) {
23   vector<BasicBlock*> ReturningBlocks;
24
25   // Loop over all of the blocks in a method, tracking all of the blocks that
26   // return.
27   //
28   for(Method::iterator I = M->begin(), E = M->end(); I != E; ++I)
29     if ((*I)->getTerminator()->getOpcode() == Instruction::Ret)
30       ReturningBlocks.push_back(*I);
31
32   if (ReturningBlocks.size() == 0) 
33     return 0;                          // No blocks return
34   else if (ReturningBlocks.size() == 1)
35     return ReturningBlocks.front();    // Already has a single return block
36
37   // Otherwise, we need to insert a new basic block into the method, add a PHI
38   // node (if the function returns a value), and convert all of the return 
39   // instructions into unconditional branches.
40   //
41   BasicBlock *NewRetBlock = new BasicBlock("", M);
42
43   if (M->getReturnType() != Type::VoidTy) {
44     // If the method doesn't return void... add a PHI node to the block...
45     PHINode *PN = new PHINode(M->getReturnType());
46     NewRetBlock->getInstList().push_back(PN);
47
48     // Add an incoming element to the PHI node for every return instruction that
49     // is merging into this new block...
50     for (vector<BasicBlock*>::iterator I = ReturningBlocks.begin(), 
51                                        E = ReturningBlocks.end(); I != E; ++I)
52       PN->addIncoming((*I)->getTerminator()->getOperand(0), *I);
53
54     // Add a return instruction to return the result of the PHI node...
55     NewRetBlock->getInstList().push_back(new ReturnInst(PN));
56   } else {
57     // If it returns void, just add a return void instruction to the block
58     NewRetBlock->getInstList().push_back(new ReturnInst());
59   }
60
61   // Loop over all of the blocks, replacing the return instruction with an
62   // unconditional branch.
63   //
64   for (vector<BasicBlock*>::iterator I = ReturningBlocks.begin(), 
65                                      E = ReturningBlocks.end(); I != E; ++I) {
66     delete (*I)->getInstList().pop_back();  // Remove the return insn
67     (*I)->getInstList().push_back(new BranchInst(NewRetBlock));
68   }
69   return NewRetBlock;
70 }