blob: 15e37e399335bff3811d56a64f833ac3d9f1a5de [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 vector<BasicBlock*> ReturningBlocks;
27
28 // Loop over all of the blocks in a method, tracking all of the blocks that
29 // return.
30 //
31 for(Method::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +000032 if ((*I)->getTerminator()->getOpcode() == Instruction::Ret)
Chris Lattner29aae152001-07-06 16:58:36 +000033 ReturningBlocks.push_back(*I);
34
Chris Lattnerf9f28962002-01-31 01:12:06 +000035 if (ReturningBlocks.size() == 0) {
36 ExitNode = 0;
37 return false; // No blocks return
38 } else if (ReturningBlocks.size() == 1) {
39 ExitNode = ReturningBlocks.front(); // Already has a single return block
40 return false;
41 }
Chris Lattner29aae152001-07-06 16:58:36 +000042
43 // Otherwise, we need to insert a new basic block into the method, add a PHI
44 // node (if the function returns a value), and convert all of the return
45 // instructions into unconditional branches.
46 //
Chris Lattnerf9f28962002-01-31 01:12:06 +000047 BasicBlock *NewRetBlock = new BasicBlock("UnifiedExitNode", M);
Chris Lattner29aae152001-07-06 16:58:36 +000048
49 if (M->getReturnType() != Type::VoidTy) {
50 // If the method doesn't return void... add a PHI node to the block...
51 PHINode *PN = new PHINode(M->getReturnType());
52 NewRetBlock->getInstList().push_back(PN);
53
54 // Add an incoming element to the PHI node for every return instruction that
55 // is merging into this new block...
56 for (vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
57 E = ReturningBlocks.end(); I != E; ++I)
58 PN->addIncoming((*I)->getTerminator()->getOperand(0), *I);
59
60 // Add a return instruction to return the result of the PHI node...
61 NewRetBlock->getInstList().push_back(new ReturnInst(PN));
62 } else {
63 // If it returns void, just add a return void instruction to the block
64 NewRetBlock->getInstList().push_back(new ReturnInst());
65 }
66
67 // Loop over all of the blocks, replacing the return instruction with an
68 // unconditional branch.
69 //
70 for (vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
71 E = ReturningBlocks.end(); I != E; ++I) {
72 delete (*I)->getInstList().pop_back(); // Remove the return insn
73 (*I)->getInstList().push_back(new BranchInst(NewRetBlock));
74 }
Chris Lattnerf9f28962002-01-31 01:12:06 +000075 ExitNode = NewRetBlock;
76 return true;
Chris Lattner29aae152001-07-06 16:58:36 +000077}