LLVM API Documentation
00001 //===- UnifyFunctionExitNodes.cpp - Make all functions have a single exit -===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This pass is used to ensure that functions have at most one return 00011 // instruction in them. Additionally, it keeps track of which node is the new 00012 // exit node of the CFG. If there are no exit nodes in the CFG, the getExitNode 00013 // method will return a null pointer. 00014 // 00015 //===----------------------------------------------------------------------===// 00016 00017 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" 00018 #include "llvm/Transforms/Scalar.h" 00019 #include "llvm/BasicBlock.h" 00020 #include "llvm/Function.h" 00021 #include "llvm/Instructions.h" 00022 #include "llvm/Type.h" 00023 #include "llvm/ADT/StringExtras.h" 00024 using namespace llvm; 00025 00026 char UnifyFunctionExitNodes::ID = 0; 00027 static RegisterPass<UnifyFunctionExitNodes> 00028 X("mergereturn", "Unify function exit nodes"); 00029 00030 Pass *llvm::createUnifyFunctionExitNodesPass() { 00031 return new UnifyFunctionExitNodes(); 00032 } 00033 00034 void UnifyFunctionExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{ 00035 // We preserve the non-critical-edgeness property 00036 AU.addPreservedID(BreakCriticalEdgesID); 00037 // This is a cluster of orthogonal Transforms 00038 AU.addPreservedID(PromoteMemoryToRegisterID); 00039 AU.addPreservedID(LowerSwitchID); 00040 } 00041 00042 // UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new 00043 // BasicBlock, and converting all returns to unconditional branches to this 00044 // new basic block. The singular exit node is returned. 00045 // 00046 // If there are no return stmts in the Function, a null pointer is returned. 00047 // 00048 bool UnifyFunctionExitNodes::runOnFunction(Function &F) { 00049 // Loop over all of the blocks in a function, tracking all of the blocks that 00050 // return. 00051 // 00052 std::vector<BasicBlock*> ReturningBlocks; 00053 std::vector<BasicBlock*> UnwindingBlocks; 00054 std::vector<BasicBlock*> UnreachableBlocks; 00055 for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I) 00056 if (isa<ReturnInst>(I->getTerminator())) 00057 ReturningBlocks.push_back(I); 00058 else if (isa<UnwindInst>(I->getTerminator())) 00059 UnwindingBlocks.push_back(I); 00060 else if (isa<UnreachableInst>(I->getTerminator())) 00061 UnreachableBlocks.push_back(I); 00062 00063 // Handle unwinding blocks first. 00064 if (UnwindingBlocks.empty()) { 00065 UnwindBlock = 0; 00066 } else if (UnwindingBlocks.size() == 1) { 00067 UnwindBlock = UnwindingBlocks.front(); 00068 } else { 00069 UnwindBlock = BasicBlock::Create("UnifiedUnwindBlock", &F); 00070 new UnwindInst(UnwindBlock); 00071 00072 for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(), 00073 E = UnwindingBlocks.end(); I != E; ++I) { 00074 BasicBlock *BB = *I; 00075 BB->getInstList().pop_back(); // Remove the unwind insn 00076 BranchInst::Create(UnwindBlock, BB); 00077 } 00078 } 00079 00080 // Then unreachable blocks. 00081 if (UnreachableBlocks.empty()) { 00082 UnreachableBlock = 0; 00083 } else if (UnreachableBlocks.size() == 1) { 00084 UnreachableBlock = UnreachableBlocks.front(); 00085 } else { 00086 UnreachableBlock = BasicBlock::Create("UnifiedUnreachableBlock", &F); 00087 new UnreachableInst(UnreachableBlock); 00088 00089 for (std::vector<BasicBlock*>::iterator I = UnreachableBlocks.begin(), 00090 E = UnreachableBlocks.end(); I != E; ++I) { 00091 BasicBlock *BB = *I; 00092 BB->getInstList().pop_back(); // Remove the unreachable inst. 00093 BranchInst::Create(UnreachableBlock, BB); 00094 } 00095 } 00096 00097 // Now handle return blocks. 00098 if (ReturningBlocks.empty()) { 00099 ReturnBlock = 0; 00100 return false; // No blocks return 00101 } else if (ReturningBlocks.size() == 1) { 00102 ReturnBlock = ReturningBlocks.front(); // Already has a single return block 00103 return false; 00104 } 00105 00106 // Otherwise, we need to insert a new basic block into the function, add a PHI 00107 // nodes (if the function returns values), and convert all of the return 00108 // instructions into unconditional branches. 00109 // 00110 BasicBlock *NewRetBlock = BasicBlock::Create("UnifiedReturnBlock", &F); 00111 00112 PHINode *PN = 0; 00113 if (F.getReturnType() == Type::VoidTy) { 00114 ReturnInst::Create(NULL, NewRetBlock); 00115 } else { 00116 // If the function doesn't return void... add a PHI node to the block... 00117 PN = PHINode::Create(F.getReturnType(), "UnifiedRetVal"); 00118 NewRetBlock->getInstList().push_back(PN); 00119 ReturnInst::Create(PN, NewRetBlock); 00120 } 00121 00122 // Loop over all of the blocks, replacing the return instruction with an 00123 // unconditional branch. 00124 // 00125 for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(), 00126 E = ReturningBlocks.end(); I != E; ++I) { 00127 BasicBlock *BB = *I; 00128 00129 // Add an incoming element to the PHI node for every return instruction that 00130 // is merging into this new block... 00131 if (PN) 00132 PN->addIncoming(BB->getTerminator()->getOperand(0), BB); 00133 00134 BB->getInstList().pop_back(); // Remove the return insn 00135 BranchInst::Create(NewRetBlock, BB); 00136 } 00137 ReturnBlock = NewRetBlock; 00138 return true; 00139 }
This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.