LLVM API Documentation

UnifyFunctionExitNodes.cpp
Go to the documentation of this file.
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/ADT/StringExtras.h"
00019 #include "llvm/IR/BasicBlock.h"
00020 #include "llvm/IR/Function.h"
00021 #include "llvm/IR/Instructions.h"
00022 #include "llvm/IR/Type.h"
00023 #include "llvm/Transforms/Scalar.h"
00024 using namespace llvm;
00025 
00026 char UnifyFunctionExitNodes::ID = 0;
00027 INITIALIZE_PASS(UnifyFunctionExitNodes, "mergereturn",
00028                 "Unify function exit nodes", false, false)
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(LowerSwitchID);
00039 }
00040 
00041 // UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
00042 // BasicBlock, and converting all returns to unconditional branches to this
00043 // new basic block.  The singular exit node is returned.
00044 //
00045 // If there are no return stmts in the Function, a null pointer is returned.
00046 //
00047 bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
00048   // Loop over all of the blocks in a function, tracking all of the blocks that
00049   // return.
00050   //
00051   std::vector<BasicBlock*> ReturningBlocks;
00052   std::vector<BasicBlock*> UnreachableBlocks;
00053   for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
00054     if (isa<ReturnInst>(I->getTerminator()))
00055       ReturningBlocks.push_back(I);
00056     else if (isa<UnreachableInst>(I->getTerminator()))
00057       UnreachableBlocks.push_back(I);
00058 
00059   // Then unreachable blocks.
00060   if (UnreachableBlocks.empty()) {
00061     UnreachableBlock = nullptr;
00062   } else if (UnreachableBlocks.size() == 1) {
00063     UnreachableBlock = UnreachableBlocks.front();
00064   } else {
00065     UnreachableBlock = BasicBlock::Create(F.getContext(), 
00066                                           "UnifiedUnreachableBlock", &F);
00067     new UnreachableInst(F.getContext(), UnreachableBlock);
00068 
00069     for (std::vector<BasicBlock*>::iterator I = UnreachableBlocks.begin(),
00070            E = UnreachableBlocks.end(); I != E; ++I) {
00071       BasicBlock *BB = *I;
00072       BB->getInstList().pop_back();  // Remove the unreachable inst.
00073       BranchInst::Create(UnreachableBlock, BB);
00074     }
00075   }
00076 
00077   // Now handle return blocks.
00078   if (ReturningBlocks.empty()) {
00079     ReturnBlock = nullptr;
00080     return false;                          // No blocks return
00081   } else if (ReturningBlocks.size() == 1) {
00082     ReturnBlock = ReturningBlocks.front(); // Already has a single return block
00083     return false;
00084   }
00085 
00086   // Otherwise, we need to insert a new basic block into the function, add a PHI
00087   // nodes (if the function returns values), and convert all of the return
00088   // instructions into unconditional branches.
00089   //
00090   BasicBlock *NewRetBlock = BasicBlock::Create(F.getContext(),
00091                                                "UnifiedReturnBlock", &F);
00092 
00093   PHINode *PN = nullptr;
00094   if (F.getReturnType()->isVoidTy()) {
00095     ReturnInst::Create(F.getContext(), nullptr, NewRetBlock);
00096   } else {
00097     // If the function doesn't return void... add a PHI node to the block...
00098     PN = PHINode::Create(F.getReturnType(), ReturningBlocks.size(),
00099                          "UnifiedRetVal");
00100     NewRetBlock->getInstList().push_back(PN);
00101     ReturnInst::Create(F.getContext(), PN, NewRetBlock);
00102   }
00103 
00104   // Loop over all of the blocks, replacing the return instruction with an
00105   // unconditional branch.
00106   //
00107   for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
00108          E = ReturningBlocks.end(); I != E; ++I) {
00109     BasicBlock *BB = *I;
00110 
00111     // Add an incoming element to the PHI node for every return instruction that
00112     // is merging into this new block...
00113     if (PN)
00114       PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
00115 
00116     BB->getInstList().pop_back();  // Remove the return insn
00117     BranchInst::Create(NewRetBlock, BB);
00118   }
00119   ReturnBlock = NewRetBlock;
00120   return true;
00121 }