blob: 9a65fcecf440d7dd0b983e19d8480b00fd91d175 [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 Lattner63a0b2a2001-07-06 16:58:36 +000011#include "llvm/BasicBlock.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000012#include "llvm/Function.h"
Chris Lattner63a0b2a2001-07-06 16:58:36 +000013#include "llvm/iTerminators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000014#include "llvm/iPHINode.h"
Chris Lattner63a0b2a2001-07-06 16:58:36 +000015#include "llvm/Type.h"
Chris Lattner697954c2002-01-20 22:54:45 +000016using std::vector;
Chris Lattner63a0b2a2001-07-06 16:58:36 +000017
Chris Lattner483e14e2002-04-27 07:27:19 +000018AnalysisID UnifyFunctionExitNodes::ID(AnalysisID::create<UnifyFunctionExitNodes>());
Chris Lattner93193f82002-01-31 00:42:27 +000019
20
Chris Lattner63a0b2a2001-07-06 16:58:36 +000021// UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
22// BasicBlock, and converting all returns to unconditional branches to this
23// new basic block. The singular exit node is returned.
24//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000025// If there are no return stmts in the Function, a null pointer is returned.
Chris Lattner63a0b2a2001-07-06 16:58:36 +000026//
Chris Lattner18961502002-06-25 16:12:52 +000027bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000028 // Loop over all of the blocks in a function, tracking all of the blocks that
Chris Lattner63a0b2a2001-07-06 16:58:36 +000029 // return.
30 //
Chris Lattner417cf7e2002-02-01 04:53:48 +000031 vector<BasicBlock*> ReturningBlocks;
Chris Lattner18961502002-06-25 16:12:52 +000032 for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
33 if (isa<ReturnInst>(I->getTerminator()))
34 ReturningBlocks.push_back(I);
Chris Lattner63a0b2a2001-07-06 16:58:36 +000035
Chris Lattner417cf7e2002-02-01 04:53:48 +000036 if (ReturningBlocks.empty()) {
Chris Lattner21801532002-01-31 01:12:06 +000037 ExitNode = 0;
Chris Lattnerb444a1f2002-05-07 18:51:25 +000038 return false; // No blocks return
Chris Lattner21801532002-01-31 01:12:06 +000039 } else if (ReturningBlocks.size() == 1) {
40 ExitNode = ReturningBlocks.front(); // Already has a single return block
41 return false;
42 }
Chris Lattner63a0b2a2001-07-06 16:58:36 +000043
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000044 // Otherwise, we need to insert a new basic block into the function, add a PHI
Chris Lattner63a0b2a2001-07-06 16:58:36 +000045 // node (if the function returns a value), and convert all of the return
46 // instructions into unconditional branches.
47 //
Chris Lattner18961502002-06-25 16:12:52 +000048 BasicBlock *NewRetBlock = new BasicBlock("UnifiedExitNode", &F);
Chris Lattner63a0b2a2001-07-06 16:58:36 +000049
Chris Lattner18961502002-06-25 16:12:52 +000050 if (F.getReturnType() != Type::VoidTy) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000051 // If the function doesn't return void... add a PHI node to the block...
Chris Lattner18961502002-06-25 16:12:52 +000052 PHINode *PN = new PHINode(F.getReturnType(), "UnifiedRetVal");
Chris Lattner63a0b2a2001-07-06 16:58:36 +000053 NewRetBlock->getInstList().push_back(PN);
54
55 // Add an incoming element to the PHI node for every return instruction that
56 // is merging into this new block...
57 for (vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
58 E = ReturningBlocks.end(); I != E; ++I)
59 PN->addIncoming((*I)->getTerminator()->getOperand(0), *I);
60
61 // Add a return instruction to return the result of the PHI node...
62 NewRetBlock->getInstList().push_back(new ReturnInst(PN));
63 } else {
64 // If it returns void, just add a return void instruction to the block
65 NewRetBlock->getInstList().push_back(new ReturnInst());
66 }
67
68 // Loop over all of the blocks, replacing the return instruction with an
69 // unconditional branch.
70 //
71 for (vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
72 E = ReturningBlocks.end(); I != E; ++I) {
Chris Lattner18961502002-06-25 16:12:52 +000073 (*I)->getInstList().pop_back(); // Remove the return insn
Chris Lattner63a0b2a2001-07-06 16:58:36 +000074 (*I)->getInstList().push_back(new BranchInst(NewRetBlock));
75 }
Chris Lattner21801532002-01-31 01:12:06 +000076 ExitNode = NewRetBlock;
77 return true;
Chris Lattner63a0b2a2001-07-06 16:58:36 +000078}