blob: da7b1f609aea8f8ab610cff4237b219f4ad2cf7c [file] [log] [blame]
Chris Lattner29aae152001-07-06 16:58:36 +00001//===- 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
Chris Lattnerccf571a2002-01-31 00:42:27 +00008#include "llvm/Transforms/UnifyMethodExitNodes.h"
Chris Lattner29aae152001-07-06 16:58:36 +00009#include "llvm/BasicBlock.h"
10#include "llvm/Method.h"
11#include "llvm/iTerminators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000012#include "llvm/iPHINode.h"
Chris Lattner29aae152001-07-06 16:58:36 +000013#include "llvm/Type.h"
Chris Lattner7f74a562002-01-20 22:54:45 +000014using std::vector;
Chris Lattner29aae152001-07-06 16:58:36 +000015
Chris Lattnerccf571a2002-01-31 00:42:27 +000016AnalysisID UnifyMethodExitNodes::ID(AnalysisID::create<UnifyMethodExitNodes>());
17
18
Chris Lattner29aae152001-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//
23// If there are no return stmts in the Method, a null pointer is returned.
24//
Chris Lattnerf9f28962002-01-31 01:12:06 +000025bool UnifyMethodExitNodes::doit(Method *M, BasicBlock *&ExitNode) {
Chris Lattner29aae152001-07-06 16:58:36 +000026 // Loop over all of the blocks in a method, tracking all of the blocks that
27 // return.
28 //
Chris Lattner86595ae2002-02-01 04:53:48 +000029 vector<BasicBlock*> ReturningBlocks;
Chris Lattner29aae152001-07-06 16:58:36 +000030 for(Method::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattner86595ae2002-02-01 04:53:48 +000031 if (isa<ReturnInst>((*I)->getTerminator()))
Chris Lattner29aae152001-07-06 16:58:36 +000032 ReturningBlocks.push_back(*I);
33
Chris Lattner86595ae2002-02-01 04:53:48 +000034 if (ReturningBlocks.empty()) {
Chris Lattnerf9f28962002-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 Lattner29aae152001-07-06 16:58:36 +000041
42 // Otherwise, we need to insert a new basic block into the method, add a PHI
43 // node (if the function returns a value), and convert all of the return
44 // instructions into unconditional branches.
45 //
Chris Lattnerf9f28962002-01-31 01:12:06 +000046 BasicBlock *NewRetBlock = new BasicBlock("UnifiedExitNode", M);
Chris Lattner29aae152001-07-06 16:58:36 +000047
48 if (M->getReturnType() != Type::VoidTy) {
49 // If the method doesn't return void... add a PHI node to the block...
50 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 Lattnerf9f28962002-01-31 01:12:06 +000074 ExitNode = NewRetBlock;
75 return true;
Chris Lattner29aae152001-07-06 16:58:36 +000076}