LLVM  6.0.0svn
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
39 }
40 
41 // UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
42 // BasicBlock, and converting all returns to unconditional branches to this
43 // new basic block. The singular exit node is returned.
44 //
45 // If there are no return stmts in the Function, a null pointer is returned.
46 //
48  // Loop over all of the blocks in a function, tracking all of the blocks that
49  // return.
50  //
51  std::vector<BasicBlock*> ReturningBlocks;
52  std::vector<BasicBlock*> UnreachableBlocks;
53  for (BasicBlock &I : F)
54  if (isa<ReturnInst>(I.getTerminator()))
55  ReturningBlocks.push_back(&I);
56  else if (isa<UnreachableInst>(I.getTerminator()))
57  UnreachableBlocks.push_back(&I);
58 
59  // Then unreachable blocks.
60  if (UnreachableBlocks.empty()) {
61  UnreachableBlock = nullptr;
62  } else if (UnreachableBlocks.size() == 1) {
63  UnreachableBlock = UnreachableBlocks.front();
64  } else {
65  UnreachableBlock = BasicBlock::Create(F.getContext(),
66  "UnifiedUnreachableBlock", &F);
67  new UnreachableInst(F.getContext(), UnreachableBlock);
68 
69  for (BasicBlock *BB : UnreachableBlocks) {
70  BB->getInstList().pop_back(); // Remove the unreachable inst.
72  }
73  }
74 
75  // Now handle return blocks.
76  if (ReturningBlocks.empty()) {
77  ReturnBlock = nullptr;
78  return false; // No blocks return
79  } else if (ReturningBlocks.size() == 1) {
80  ReturnBlock = ReturningBlocks.front(); // Already has a single return block
81  return false;
82  }
83 
84  // Otherwise, we need to insert a new basic block into the function, add a PHI
85  // nodes (if the function returns values), and convert all of the return
86  // instructions into unconditional branches.
87  //
88  BasicBlock *NewRetBlock = BasicBlock::Create(F.getContext(),
89  "UnifiedReturnBlock", &F);
90 
91  PHINode *PN = nullptr;
92  if (F.getReturnType()->isVoidTy()) {
93  ReturnInst::Create(F.getContext(), nullptr, NewRetBlock);
94  } else {
95  // If the function doesn't return void... add a PHI node to the block...
96  PN = PHINode::Create(F.getReturnType(), ReturningBlocks.size(),
97  "UnifiedRetVal");
98  NewRetBlock->getInstList().push_back(PN);
99  ReturnInst::Create(F.getContext(), PN, NewRetBlock);
100  }
101 
102  // Loop over all of the blocks, replacing the return instruction with an
103  // unconditional branch.
104  //
105  for (BasicBlock *BB : ReturningBlocks) {
106  // Add an incoming element to the PHI node for every return instruction that
107  // is merging into this new block...
108  if (PN)
109  PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
110 
111  BB->getInstList().pop_back(); // Remove the return insn
112  BranchInst::Create(NewRetBlock, BB);
113  }
114  ReturnBlock = NewRetBlock;
115  return true;
116 }
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:81
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
char & LowerSwitchID
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, Instruction *InsertBefore=nullptr)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
#define F(x, y, z)
Definition: MD5.cpp:55
AnalysisUsage & addPreservedID(const void *ID)
Pass * createUnifyFunctionExitNodesPass()
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
char & BreakCriticalEdgesID
This function has undefined behavior.
const Instruction & front() const
Definition: BasicBlock.h:264
Represent the analysis usage information of a pass.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:101
const InstListType & getInstList() const
Return the underlying instruction list container.
Definition: BasicBlock.h:317
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
void push_back(pointer val)
Definition: ilist.h:326
#define I(x, y, z)
Definition: MD5.cpp:58
INITIALIZE_PASS(UnifyFunctionExitNodes, "mergereturn", "Unify function exit nodes", false, false) Pass *llvm
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...