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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source 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 | |
| 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" |
| 23 | using namespace llvm; |
| 24 | |
| 25 | char UnifyFunctionExitNodes::ID = 0; |
| 26 | static RegisterPass<UnifyFunctionExitNodes> |
| 27 | X("mergereturn", "Unify function exit nodes"); |
| 28 | |
| 29 | int UnifyFunctionExitNodes::stub; |
| 30 | |
| 31 | Pass *llvm::createUnifyFunctionExitNodesPass() { |
| 32 | return new UnifyFunctionExitNodes(); |
| 33 | } |
| 34 | |
| 35 | void UnifyFunctionExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{ |
| 36 | // We preserve the non-critical-edgeness property |
| 37 | AU.addPreservedID(BreakCriticalEdgesID); |
| 38 | // This is a cluster of orthogonal Transforms |
| 39 | AU.addPreservedID(PromoteMemoryToRegisterID); |
| 40 | AU.addPreservedID(LowerSelectID); |
| 41 | AU.addPreservedID(LowerSwitchID); |
| 42 | } |
| 43 | |
| 44 | // UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new |
| 45 | // BasicBlock, and converting all returns to unconditional branches to this |
| 46 | // new basic block. The singular exit node is returned. |
| 47 | // |
| 48 | // If there are no return stmts in the Function, a null pointer is returned. |
| 49 | // |
| 50 | bool UnifyFunctionExitNodes::runOnFunction(Function &F) { |
| 51 | // Loop over all of the blocks in a function, tracking all of the blocks that |
| 52 | // return. |
| 53 | // |
| 54 | std::vector<BasicBlock*> ReturningBlocks; |
| 55 | std::vector<BasicBlock*> UnwindingBlocks; |
| 56 | std::vector<BasicBlock*> UnreachableBlocks; |
| 57 | for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
| 58 | if (isa<ReturnInst>(I->getTerminator())) |
| 59 | ReturningBlocks.push_back(I); |
| 60 | else if (isa<UnwindInst>(I->getTerminator())) |
| 61 | UnwindingBlocks.push_back(I); |
| 62 | else if (isa<UnreachableInst>(I->getTerminator())) |
| 63 | UnreachableBlocks.push_back(I); |
| 64 | |
| 65 | // Handle unwinding blocks first. |
| 66 | if (UnwindingBlocks.empty()) { |
| 67 | UnwindBlock = 0; |
| 68 | } else if (UnwindingBlocks.size() == 1) { |
| 69 | UnwindBlock = UnwindingBlocks.front(); |
| 70 | } else { |
| 71 | UnwindBlock = new BasicBlock("UnifiedUnwindBlock", &F); |
| 72 | new UnwindInst(UnwindBlock); |
| 73 | |
| 74 | for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(), |
| 75 | E = UnwindingBlocks.end(); I != E; ++I) { |
| 76 | BasicBlock *BB = *I; |
| 77 | BB->getInstList().pop_back(); // Remove the unwind insn |
| 78 | new BranchInst(UnwindBlock, BB); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Then unreachable blocks. |
| 83 | if (UnreachableBlocks.empty()) { |
| 84 | UnreachableBlock = 0; |
| 85 | } else if (UnreachableBlocks.size() == 1) { |
| 86 | UnreachableBlock = UnreachableBlocks.front(); |
| 87 | } else { |
| 88 | UnreachableBlock = new BasicBlock("UnifiedUnreachableBlock", &F); |
| 89 | new UnreachableInst(UnreachableBlock); |
| 90 | |
| 91 | for (std::vector<BasicBlock*>::iterator I = UnreachableBlocks.begin(), |
| 92 | E = UnreachableBlocks.end(); I != E; ++I) { |
| 93 | BasicBlock *BB = *I; |
| 94 | BB->getInstList().pop_back(); // Remove the unreachable inst. |
| 95 | new BranchInst(UnreachableBlock, BB); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Now handle return blocks. |
| 100 | if (ReturningBlocks.empty()) { |
| 101 | ReturnBlock = 0; |
| 102 | return false; // No blocks return |
| 103 | } else if (ReturningBlocks.size() == 1) { |
| 104 | ReturnBlock = ReturningBlocks.front(); // Already has a single return block |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | // Otherwise, we need to insert a new basic block into the function, add a PHI |
| 109 | // node (if the function returns a value), and convert all of the return |
| 110 | // instructions into unconditional branches. |
| 111 | // |
| 112 | BasicBlock *NewRetBlock = new BasicBlock("UnifiedReturnBlock", &F); |
| 113 | |
| 114 | PHINode *PN = 0; |
| 115 | if (F.getReturnType() != Type::VoidTy) { |
| 116 | // If the function doesn't return void... add a PHI node to the block... |
| 117 | PN = new PHINode(F.getReturnType(), "UnifiedRetVal"); |
| 118 | NewRetBlock->getInstList().push_back(PN); |
| 119 | } |
| 120 | new ReturnInst(PN, NewRetBlock); |
| 121 | |
| 122 | // Loop over all of the blocks, replacing the return instruction with an |
| 123 | // unconditional branch. |
| 124 | // |
| 125 | for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(), |
| 126 | E = ReturningBlocks.end(); I != E; ++I) { |
| 127 | BasicBlock *BB = *I; |
| 128 | |
| 129 | // Add an incoming element to the PHI node for every return instruction that |
| 130 | // is merging into this new block... |
| 131 | if (PN) PN->addIncoming(BB->getTerminator()->getOperand(0), BB); |
| 132 | |
| 133 | BB->getInstList().pop_back(); // Remove the return insn |
| 134 | new BranchInst(NewRetBlock, BB); |
| 135 | } |
| 136 | ReturnBlock = NewRetBlock; |
| 137 | return true; |
| 138 | } |