blob: ddce921d45e3b867586461296b8d8dbc9145755d [file] [log] [blame]
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001//===- UnifyFunctionExitNodes.cpp - Make all functions have a single exit -===//
Chris Lattner63a0b2a2001-07-06 16:58:36 +00002//
Chris Lattnerb444a1f2002-05-07 18:51:25 +00003// 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.
Chris Lattner63a0b2a2001-07-06 16:58:36 +00007//
8//===----------------------------------------------------------------------===//
9
Chris Lattnerfc514f42002-05-07 19:18:48 +000010#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
Chris Lattner6c0e0492003-03-31 17:30:25 +000011#include "llvm/Transforms/Scalar.h"
Chris Lattner63a0b2a2001-07-06 16:58:36 +000012#include "llvm/BasicBlock.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000013#include "llvm/Function.h"
Chris Lattner63a0b2a2001-07-06 16:58:36 +000014#include "llvm/iTerminators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000015#include "llvm/iPHINode.h"
Chris Lattner63a0b2a2001-07-06 16:58:36 +000016#include "llvm/Type.h"
17
Chris Lattner1e435162002-07-26 21:12:44 +000018static RegisterOpt<UnifyFunctionExitNodes>
Chris Lattnerf6293092002-07-23 18:06:35 +000019X("mergereturn", "Unify function exit nodes");
Chris Lattner93193f82002-01-31 00:42:27 +000020
Chris Lattnerf46057be2003-09-10 20:34:51 +000021Pass *createUnifyFunctionExitNodesPass() {
22 return new UnifyFunctionExitNodes();
23}
24
Chris Lattner6c0e0492003-03-31 17:30:25 +000025void UnifyFunctionExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{
26 // We preserve the non-critical-edgeness property
27 AU.addPreservedID(BreakCriticalEdgesID);
28}
29
Chris Lattner63a0b2a2001-07-06 16:58:36 +000030// UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
31// BasicBlock, and converting all returns to unconditional branches to this
32// new basic block. The singular exit node is returned.
33//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000034// If there are no return stmts in the Function, a null pointer is returned.
Chris Lattner63a0b2a2001-07-06 16:58:36 +000035//
Chris Lattner18961502002-06-25 16:12:52 +000036bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000037 // Loop over all of the blocks in a function, tracking all of the blocks that
Chris Lattner63a0b2a2001-07-06 16:58:36 +000038 // return.
39 //
Chris Lattnerde579f12003-05-22 22:00:07 +000040 std::vector<BasicBlock*> ReturningBlocks;
Chris Lattnerf46057be2003-09-10 20:34:51 +000041 std::vector<BasicBlock*> UnwindingBlocks;
Chris Lattner18961502002-06-25 16:12:52 +000042 for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
43 if (isa<ReturnInst>(I->getTerminator()))
44 ReturningBlocks.push_back(I);
Chris Lattnerf46057be2003-09-10 20:34:51 +000045 else if (isa<UnwindInst>(I->getTerminator()))
46 UnwindingBlocks.push_back(I);
Chris Lattner63a0b2a2001-07-06 16:58:36 +000047
Chris Lattnerf46057be2003-09-10 20:34:51 +000048 // Handle unwinding blocks first...
49 if (UnwindingBlocks.empty()) {
50 UnwindBlock = 0;
51 } else if (UnwindingBlocks.size() == 1) {
52 UnwindBlock = UnwindingBlocks.front();
53 } else {
54 UnwindBlock = new BasicBlock("UnifiedUnwindBlock", &F);
55 UnwindBlock->getInstList().push_back(new UnwindInst());
56
57 for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(),
58 E = UnwindingBlocks.end(); I != E; ++I) {
59 BasicBlock *BB = *I;
60 BB->getInstList().pop_back(); // Remove the return insn
61 BB->getInstList().push_back(new BranchInst(UnwindBlock));
62 }
63 }
64
65 // Now handle return blocks...
Chris Lattner417cf7e2002-02-01 04:53:48 +000066 if (ReturningBlocks.empty()) {
Chris Lattnerf46057be2003-09-10 20:34:51 +000067 ReturnBlock = 0;
Chris Lattnerb444a1f2002-05-07 18:51:25 +000068 return false; // No blocks return
Chris Lattner21801532002-01-31 01:12:06 +000069 } else if (ReturningBlocks.size() == 1) {
Chris Lattnerf46057be2003-09-10 20:34:51 +000070 ReturnBlock = ReturningBlocks.front(); // Already has a single return block
Chris Lattner21801532002-01-31 01:12:06 +000071 return false;
72 }
Chris Lattner63a0b2a2001-07-06 16:58:36 +000073
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000074 // Otherwise, we need to insert a new basic block into the function, add a PHI
Chris Lattner63a0b2a2001-07-06 16:58:36 +000075 // node (if the function returns a value), and convert all of the return
76 // instructions into unconditional branches.
77 //
Chris Lattnerf46057be2003-09-10 20:34:51 +000078 BasicBlock *NewRetBlock = new BasicBlock("UnifiedReturnBlock", &F);
Chris Lattner63a0b2a2001-07-06 16:58:36 +000079
Chris Lattner6c0e0492003-03-31 17:30:25 +000080 PHINode *PN = 0;
Chris Lattner18961502002-06-25 16:12:52 +000081 if (F.getReturnType() != Type::VoidTy) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000082 // If the function doesn't return void... add a PHI node to the block...
Chris Lattner6c0e0492003-03-31 17:30:25 +000083 PN = new PHINode(F.getReturnType(), "UnifiedRetVal");
Chris Lattner8606d992002-09-10 23:31:28 +000084 NewRetBlock->getInstList().push_back(PN);
Chris Lattner8606d992002-09-10 23:31:28 +000085 NewRetBlock->getInstList().push_back(new ReturnInst(PN));
Chris Lattner63a0b2a2001-07-06 16:58:36 +000086 } else {
87 // If it returns void, just add a return void instruction to the block
Chris Lattner1a7db9b2002-09-12 19:00:43 +000088 NewRetBlock->getInstList().push_back(new ReturnInst());
Chris Lattner63a0b2a2001-07-06 16:58:36 +000089 }
90
91 // Loop over all of the blocks, replacing the return instruction with an
92 // unconditional branch.
93 //
Chris Lattnerde579f12003-05-22 22:00:07 +000094 for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
95 E = ReturningBlocks.end(); I != E; ++I) {
Chris Lattner6c0e0492003-03-31 17:30:25 +000096 BasicBlock *BB = *I;
97
98 // Add an incoming element to the PHI node for every return instruction that
99 // is merging into this new block...
100 if (PN) PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
101
102 BB->getInstList().pop_back(); // Remove the return insn
103 BB->getInstList().push_back(new BranchInst(NewRetBlock));
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000104 }
Chris Lattnerf46057be2003-09-10 20:34:51 +0000105 ReturnBlock = NewRetBlock;
Chris Lattner21801532002-01-31 01:12:06 +0000106 return true;
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000107}