Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- UnifyFunctionExitNodes.cpp - Make all functions have a single exit -===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 | |
| 17 | #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" |
| 18 | #include "llvm/Transforms/Scalar.h" |
| 19 | #include "llvm/BasicBlock.h" |
| 20 | #include "llvm/Function.h" |
| 21 | #include "llvm/Instructions.h" |
| 22 | #include "llvm/Type.h" |
Devang Patel | d91d384 | 2008-03-10 18:11:41 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
| 26 | char UnifyFunctionExitNodes::ID = 0; |
Owen Anderson | 6374c3d | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 27 | INITIALIZE_PASS(UnifyFunctionExitNodes, "mergereturn", |
| 28 | "Unify function exit nodes", false, false); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 29 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 30 | Pass *llvm::createUnifyFunctionExitNodesPass() { |
| 31 | return new UnifyFunctionExitNodes(); |
| 32 | } |
| 33 | |
| 34 | void UnifyFunctionExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{ |
| 35 | // We preserve the non-critical-edgeness property |
| 36 | AU.addPreservedID(BreakCriticalEdgesID); |
| 37 | // This is a cluster of orthogonal Transforms |
Dan Gohman | 6b1b2fd | 2010-08-06 21:48:06 +0000 | [diff] [blame] | 38 | AU.addPreserved("mem2reg"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 39 | AU.addPreservedID(LowerSwitchID); |
| 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 | // |
| 48 | bool UnifyFunctionExitNodes::runOnFunction(Function &F) { |
| 49 | // Loop over all of the blocks in a function, tracking all of the blocks that |
| 50 | // return. |
| 51 | // |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 52 | std::vector<BasicBlock*> ReturningBlocks; |
| 53 | std::vector<BasicBlock*> UnwindingBlocks; |
| 54 | std::vector<BasicBlock*> UnreachableBlocks; |
| 55 | for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
| 56 | if (isa<ReturnInst>(I->getTerminator())) |
| 57 | ReturningBlocks.push_back(I); |
| 58 | else if (isa<UnwindInst>(I->getTerminator())) |
| 59 | UnwindingBlocks.push_back(I); |
| 60 | else if (isa<UnreachableInst>(I->getTerminator())) |
| 61 | UnreachableBlocks.push_back(I); |
| 62 | |
| 63 | // Handle unwinding blocks first. |
| 64 | if (UnwindingBlocks.empty()) { |
| 65 | UnwindBlock = 0; |
| 66 | } else if (UnwindingBlocks.size() == 1) { |
| 67 | UnwindBlock = UnwindingBlocks.front(); |
| 68 | } else { |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 69 | UnwindBlock = BasicBlock::Create(F.getContext(), "UnifiedUnwindBlock", &F); |
| 70 | new UnwindInst(F.getContext(), UnwindBlock); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 71 | |
| 72 | for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(), |
| 73 | E = UnwindingBlocks.end(); I != E; ++I) { |
| 74 | BasicBlock *BB = *I; |
| 75 | BB->getInstList().pop_back(); // Remove the unwind insn |
Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 76 | BranchInst::Create(UnwindBlock, BB); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | // Then unreachable blocks. |
| 81 | if (UnreachableBlocks.empty()) { |
| 82 | UnreachableBlock = 0; |
| 83 | } else if (UnreachableBlocks.size() == 1) { |
| 84 | UnreachableBlock = UnreachableBlocks.front(); |
| 85 | } else { |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 86 | UnreachableBlock = BasicBlock::Create(F.getContext(), |
| 87 | "UnifiedUnreachableBlock", &F); |
| 88 | new UnreachableInst(F.getContext(), UnreachableBlock); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 89 | |
| 90 | for (std::vector<BasicBlock*>::iterator I = UnreachableBlocks.begin(), |
| 91 | E = UnreachableBlocks.end(); I != E; ++I) { |
| 92 | BasicBlock *BB = *I; |
| 93 | BB->getInstList().pop_back(); // Remove the unreachable inst. |
Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 94 | BranchInst::Create(UnreachableBlock, BB); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | // Now handle return blocks. |
| 99 | if (ReturningBlocks.empty()) { |
| 100 | ReturnBlock = 0; |
| 101 | return false; // No blocks return |
| 102 | } else if (ReturningBlocks.size() == 1) { |
| 103 | ReturnBlock = ReturningBlocks.front(); // Already has a single return block |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | // Otherwise, we need to insert a new basic block into the function, add a PHI |
Devang Patel | 40a92a1 | 2008-03-05 21:50:24 +0000 | [diff] [blame] | 108 | // nodes (if the function returns values), and convert all of the return |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 109 | // instructions into unconditional branches. |
| 110 | // |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 111 | BasicBlock *NewRetBlock = BasicBlock::Create(F.getContext(), |
| 112 | "UnifiedReturnBlock", &F); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 113 | |
Dan Gohman | 29474e9 | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 114 | PHINode *PN = 0; |
Benjamin Kramer | f2052d5 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 115 | if (F.getReturnType()->isVoidTy()) { |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 116 | ReturnInst::Create(F.getContext(), NULL, NewRetBlock); |
Dan Gohman | 29474e9 | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 117 | } else { |
Devang Patel | 40a92a1 | 2008-03-05 21:50:24 +0000 | [diff] [blame] | 118 | // If the function doesn't return void... add a PHI node to the block... |
Dan Gohman | 29474e9 | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 119 | PN = PHINode::Create(F.getReturnType(), "UnifiedRetVal"); |
Devang Patel | 40a92a1 | 2008-03-05 21:50:24 +0000 | [diff] [blame] | 120 | NewRetBlock->getInstList().push_back(PN); |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 121 | ReturnInst::Create(F.getContext(), PN, NewRetBlock); |
Devang Patel | 40a92a1 | 2008-03-05 21:50:24 +0000 | [diff] [blame] | 122 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 123 | |
| 124 | // Loop over all of the blocks, replacing the return instruction with an |
| 125 | // unconditional branch. |
| 126 | // |
| 127 | for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(), |
| 128 | E = ReturningBlocks.end(); I != E; ++I) { |
| 129 | BasicBlock *BB = *I; |
| 130 | |
| 131 | // Add an incoming element to the PHI node for every return instruction that |
| 132 | // is merging into this new block... |
Dan Gohman | 29474e9 | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 133 | if (PN) |
| 134 | PN->addIncoming(BB->getTerminator()->getOperand(0), BB); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 135 | |
| 136 | BB->getInstList().pop_back(); // Remove the return insn |
Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 137 | BranchInst::Create(NewRetBlock, BB); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 138 | } |
| 139 | ReturnBlock = NewRetBlock; |
| 140 | return true; |
| 141 | } |