blob: 56c4b204ca9f0c54b38054ccc06d41efd0389d6b [file] [log] [blame]
Chris Lattner63a0b2a2001-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
8#include "llvm/Analysis/SimplifyCFG.h"
9#include "llvm/BasicBlock.h"
10#include "llvm/Method.h"
11#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
16// UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
17// BasicBlock, and converting all returns to unconditional branches to this
18// new basic block. The singular exit node is returned.
19//
20// If there are no return stmts in the Method, a null pointer is returned.
21//
22BasicBlock *cfg::UnifyAllExitNodes(Method *M) {
23 vector<BasicBlock*> ReturningBlocks;
24
25 // Loop over all of the blocks in a method, tracking all of the blocks that
26 // return.
27 //
28 for(Method::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnera41f50d2001-07-07 19:24:15 +000029 if ((*I)->getTerminator()->getOpcode() == Instruction::Ret)
Chris Lattner63a0b2a2001-07-06 16:58:36 +000030 ReturningBlocks.push_back(*I);
31
32 if (ReturningBlocks.size() == 0)
33 return 0; // No blocks return
34 else if (ReturningBlocks.size() == 1)
35 return ReturningBlocks.front(); // Already has a single return block
36
37 // Otherwise, we need to insert a new basic block into the method, add a PHI
38 // node (if the function returns a value), and convert all of the return
39 // instructions into unconditional branches.
40 //
41 BasicBlock *NewRetBlock = new BasicBlock("", M);
42
43 if (M->getReturnType() != Type::VoidTy) {
44 // If the method doesn't return void... add a PHI node to the block...
45 PHINode *PN = new PHINode(M->getReturnType());
46 NewRetBlock->getInstList().push_back(PN);
47
48 // Add an incoming element to the PHI node for every return instruction that
49 // is merging into this new block...
50 for (vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
51 E = ReturningBlocks.end(); I != E; ++I)
52 PN->addIncoming((*I)->getTerminator()->getOperand(0), *I);
53
54 // Add a return instruction to return the result of the PHI node...
55 NewRetBlock->getInstList().push_back(new ReturnInst(PN));
56 } else {
57 // If it returns void, just add a return void instruction to the block
58 NewRetBlock->getInstList().push_back(new ReturnInst());
59 }
60
61 // Loop over all of the blocks, replacing the return instruction with an
62 // unconditional branch.
63 //
64 for (vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
65 E = ReturningBlocks.end(); I != E; ++I) {
66 delete (*I)->getInstList().pop_back(); // Remove the return insn
67 (*I)->getInstList().push_back(new BranchInst(NewRetBlock));
68 }
69 return NewRetBlock;
70}