LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UnifyFunctionExitNodes.cpp
Go to the documentation of this file.
1 //===- UnifyFunctionExitNodes.cpp - Make all functions have a single exit -===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass is used to ensure that functions have at most one return
11 // instruction in them. Additionally, it keeps track of which node is the new
12 // exit node of the CFG. If there are no exit nodes in the CFG, the getExitNode
13 // method will return a null pointer.
14 //
15 //===----------------------------------------------------------------------===//
16 
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/IR/BasicBlock.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/Type.h"
23 #include "llvm/Transforms/Scalar.h"
24 using namespace llvm;
25 
28  "Unify function exit nodes", false, false)
29 
31  return new UnifyFunctionExitNodes();
32 }
33 
35  // We preserve the non-critical-edgeness property
37  // This is a cluster of orthogonal Transforms
38  AU.addPreserved("mem2reg");
40 }
41 
42 // UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
43 // BasicBlock, and converting all returns to unconditional branches to this
44 // new basic block. The singular exit node is returned.
45 //
46 // If there are no return stmts in the Function, a null pointer is returned.
47 //
49  // Loop over all of the blocks in a function, tracking all of the blocks that
50  // return.
51  //
52  std::vector<BasicBlock*> ReturningBlocks;
53  std::vector<BasicBlock*> UnreachableBlocks;
54  for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
55  if (isa<ReturnInst>(I->getTerminator()))
56  ReturningBlocks.push_back(I);
57  else if (isa<UnreachableInst>(I->getTerminator()))
58  UnreachableBlocks.push_back(I);
59 
60  // Then unreachable blocks.
61  if (UnreachableBlocks.empty()) {
62  UnreachableBlock = 0;
63  } else if (UnreachableBlocks.size() == 1) {
64  UnreachableBlock = UnreachableBlocks.front();
65  } else {
67  "UnifiedUnreachableBlock", &F);
69 
70  for (std::vector<BasicBlock*>::iterator I = UnreachableBlocks.begin(),
71  E = UnreachableBlocks.end(); I != E; ++I) {
72  BasicBlock *BB = *I;
73  BB->getInstList().pop_back(); // Remove the unreachable inst.
75  }
76  }
77 
78  // Now handle return blocks.
79  if (ReturningBlocks.empty()) {
80  ReturnBlock = 0;
81  return false; // No blocks return
82  } else if (ReturningBlocks.size() == 1) {
83  ReturnBlock = ReturningBlocks.front(); // Already has a single return block
84  return false;
85  }
86 
87  // Otherwise, we need to insert a new basic block into the function, add a PHI
88  // nodes (if the function returns values), and convert all of the return
89  // instructions into unconditional branches.
90  //
91  BasicBlock *NewRetBlock = BasicBlock::Create(F.getContext(),
92  "UnifiedReturnBlock", &F);
93 
94  PHINode *PN = 0;
95  if (F.getReturnType()->isVoidTy()) {
96  ReturnInst::Create(F.getContext(), NULL, NewRetBlock);
97  } else {
98  // If the function doesn't return void... add a PHI node to the block...
99  PN = PHINode::Create(F.getReturnType(), ReturningBlocks.size(),
100  "UnifiedRetVal");
101  NewRetBlock->getInstList().push_back(PN);
102  ReturnInst::Create(F.getContext(), PN, NewRetBlock);
103  }
104 
105  // Loop over all of the blocks, replacing the return instruction with an
106  // unconditional branch.
107  //
108  for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
109  E = ReturningBlocks.end(); I != E; ++I) {
110  BasicBlock *BB = *I;
111 
112  // Add an incoming element to the PHI node for every return instruction that
113  // is merging into this new block...
114  if (PN)
115  PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
116 
117  BB->getInstList().pop_back(); // Remove the return insn
118  BranchInst::Create(NewRetBlock, BB);
119  }
120  ReturnBlock = NewRetBlock;
121  return true;
122 }
AnalysisUsage & addPreserved()
LLVMContext & getContext() const
Definition: Function.cpp:167
virtual void getAnalysisUsage(AnalysisUsage &AU) const
void pop_back()
Definition: ilist.h:559
iterator end()
Definition: Function.h:397
Type * getReturnType() const
Definition: Function.cpp:179
const Instruction & front() const
Definition: BasicBlock.h:205
F(f)
char & LowerSwitchID
void push_back(NodeTy *val)
Definition: ilist.h:554
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=0)
AnalysisUsage & addPreservedID(const void *ID)
Pass * createUnifyFunctionExitNodesPass()
iterator begin()
Definition: Function.h:395
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
char & BreakCriticalEdgesID
INITIALIZE_PASS(UnifyFunctionExitNodes,"mergereturn","Unify function exit nodes", false, false) Pass *llvm
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=0)
const InstListType & getInstList() const
Return the underlying instruction list container.
Definition: BasicBlock.h:214
Value * getOperand(unsigned i) const
Definition: User.h:88
#define I(x, y, z)
Definition: MD5.cpp:54
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:120
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=0, BasicBlock *InsertBefore=0)
Creates a new BasicBlock.
Definition: BasicBlock.h:109
static ReturnInst * Create(LLVMContext &C, Value *retVal=0, Instruction *InsertBefore=0)
virtual bool runOnFunction(Function &F)
bool isVoidTy() const
isVoidTy - Return true if this is 'void'.
Definition: Type.h:140