blob: 0fa87f8e8d2ad0465ae0b2de56a8e3f55a85bbaf [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//
3// This file provides several routines that are useful for simplifying CFGs in
4// various ways...
5//
6//===----------------------------------------------------------------------===//
7
Chris Lattner483e14e2002-04-27 07:27:19 +00008#include "llvm/Transforms/UnifyFunctionExitNodes.h"
Chris Lattner63a0b2a2001-07-06 16:58:36 +00009#include "llvm/BasicBlock.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000010#include "llvm/Function.h"
Chris Lattner63a0b2a2001-07-06 16:58:36 +000011#include "llvm/iTerminators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000012#include "llvm/iPHINode.h"
Chris Lattner63a0b2a2001-07-06 16:58:36 +000013#include "llvm/Type.h"
Chris Lattner697954c2002-01-20 22:54:45 +000014using std::vector;
Chris Lattner63a0b2a2001-07-06 16:58:36 +000015
Chris Lattner483e14e2002-04-27 07:27:19 +000016AnalysisID UnifyFunctionExitNodes::ID(AnalysisID::create<UnifyFunctionExitNodes>());
Chris Lattner93193f82002-01-31 00:42:27 +000017
18
Chris Lattner63a0b2a2001-07-06 16:58:36 +000019// UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
20// BasicBlock, and converting all returns to unconditional branches to this
21// new basic block. The singular exit node is returned.
22//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000023// If there are no return stmts in the Function, a null pointer is returned.
Chris Lattner63a0b2a2001-07-06 16:58:36 +000024//
Chris Lattner483e14e2002-04-27 07:27:19 +000025bool UnifyFunctionExitNodes::doit(Function *M, BasicBlock *&ExitNode) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000026 // Loop over all of the blocks in a function, tracking all of the blocks that
Chris Lattner63a0b2a2001-07-06 16:58:36 +000027 // return.
28 //
Chris Lattner417cf7e2002-02-01 04:53:48 +000029 vector<BasicBlock*> ReturningBlocks;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000030 for(Function::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattner417cf7e2002-02-01 04:53:48 +000031 if (isa<ReturnInst>((*I)->getTerminator()))
Chris Lattner63a0b2a2001-07-06 16:58:36 +000032 ReturningBlocks.push_back(*I);
33
Chris Lattner417cf7e2002-02-01 04:53:48 +000034 if (ReturningBlocks.empty()) {
Chris Lattner21801532002-01-31 01:12:06 +000035 ExitNode = 0;
36 return false; // No blocks return
37 } else if (ReturningBlocks.size() == 1) {
38 ExitNode = ReturningBlocks.front(); // Already has a single return block
39 return false;
40 }
Chris Lattner63a0b2a2001-07-06 16:58:36 +000041
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000042 // Otherwise, we need to insert a new basic block into the function, add a PHI
Chris Lattner63a0b2a2001-07-06 16:58:36 +000043 // node (if the function returns a value), and convert all of the return
44 // instructions into unconditional branches.
45 //
Chris Lattner21801532002-01-31 01:12:06 +000046 BasicBlock *NewRetBlock = new BasicBlock("UnifiedExitNode", M);
Chris Lattner63a0b2a2001-07-06 16:58:36 +000047
48 if (M->getReturnType() != Type::VoidTy) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000049 // If the function doesn't return void... add a PHI node to the block...
Chris Lattner63a0b2a2001-07-06 16:58:36 +000050 PHINode *PN = new PHINode(M->getReturnType());
51 NewRetBlock->getInstList().push_back(PN);
52
53 // Add an incoming element to the PHI node for every return instruction that
54 // is merging into this new block...
55 for (vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
56 E = ReturningBlocks.end(); I != E; ++I)
57 PN->addIncoming((*I)->getTerminator()->getOperand(0), *I);
58
59 // Add a return instruction to return the result of the PHI node...
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 (vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
70 E = ReturningBlocks.end(); I != E; ++I) {
71 delete (*I)->getInstList().pop_back(); // Remove the return insn
72 (*I)->getInstList().push_back(new BranchInst(NewRetBlock));
73 }
Chris Lattner21801532002-01-31 01:12:06 +000074 ExitNode = NewRetBlock;
75 return true;
Chris Lattner63a0b2a2001-07-06 16:58:36 +000076}