LLVM API Documentation
00001 //===- PruneEH.cpp - Pass which deletes unused exception handlers ---------===// 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 file implements a simple interprocedural pass which walks the 00011 // call-graph, turning invoke instructions into calls, iff the callee cannot 00012 // throw an exception, and marking functions 'nounwind' if they cannot throw. 00013 // It implements this as a bottom-up traversal of the call-graph. 00014 // 00015 //===----------------------------------------------------------------------===// 00016 00017 #include "llvm/Transforms/IPO.h" 00018 #include "llvm/ADT/SmallPtrSet.h" 00019 #include "llvm/ADT/SmallVector.h" 00020 #include "llvm/ADT/Statistic.h" 00021 #include "llvm/Analysis/CallGraph.h" 00022 #include "llvm/Analysis/CallGraphSCCPass.h" 00023 #include "llvm/IR/CFG.h" 00024 #include "llvm/IR/Constants.h" 00025 #include "llvm/IR/Function.h" 00026 #include "llvm/IR/Instructions.h" 00027 #include "llvm/IR/IntrinsicInst.h" 00028 #include "llvm/IR/LLVMContext.h" 00029 #include <algorithm> 00030 using namespace llvm; 00031 00032 #define DEBUG_TYPE "prune-eh" 00033 00034 STATISTIC(NumRemoved, "Number of invokes removed"); 00035 STATISTIC(NumUnreach, "Number of noreturn calls optimized"); 00036 00037 namespace { 00038 struct PruneEH : public CallGraphSCCPass { 00039 static char ID; // Pass identification, replacement for typeid 00040 PruneEH() : CallGraphSCCPass(ID) { 00041 initializePruneEHPass(*PassRegistry::getPassRegistry()); 00042 } 00043 00044 // runOnSCC - Analyze the SCC, performing the transformation if possible. 00045 bool runOnSCC(CallGraphSCC &SCC) override; 00046 00047 bool SimplifyFunction(Function *F); 00048 void DeleteBasicBlock(BasicBlock *BB); 00049 }; 00050 } 00051 00052 char PruneEH::ID = 0; 00053 INITIALIZE_PASS_BEGIN(PruneEH, "prune-eh", 00054 "Remove unused exception handling info", false, false) 00055 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) 00056 INITIALIZE_PASS_END(PruneEH, "prune-eh", 00057 "Remove unused exception handling info", false, false) 00058 00059 Pass *llvm::createPruneEHPass() { return new PruneEH(); } 00060 00061 00062 bool PruneEH::runOnSCC(CallGraphSCC &SCC) { 00063 SmallPtrSet<CallGraphNode *, 8> SCCNodes; 00064 CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph(); 00065 bool MadeChange = false; 00066 00067 // Fill SCCNodes with the elements of the SCC. Used for quickly 00068 // looking up whether a given CallGraphNode is in this SCC. 00069 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) 00070 SCCNodes.insert(*I); 00071 00072 // First pass, scan all of the functions in the SCC, simplifying them 00073 // according to what we know. 00074 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) 00075 if (Function *F = (*I)->getFunction()) 00076 MadeChange |= SimplifyFunction(F); 00077 00078 // Next, check to see if any callees might throw or if there are any external 00079 // functions in this SCC: if so, we cannot prune any functions in this SCC. 00080 // Definitions that are weak and not declared non-throwing might be 00081 // overridden at linktime with something that throws, so assume that. 00082 // If this SCC includes the unwind instruction, we KNOW it throws, so 00083 // obviously the SCC might throw. 00084 // 00085 bool SCCMightUnwind = false, SCCMightReturn = false; 00086 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); 00087 (!SCCMightUnwind || !SCCMightReturn) && I != E; ++I) { 00088 Function *F = (*I)->getFunction(); 00089 if (!F) { 00090 SCCMightUnwind = true; 00091 SCCMightReturn = true; 00092 } else if (F->isDeclaration() || F->mayBeOverridden()) { 00093 SCCMightUnwind |= !F->doesNotThrow(); 00094 SCCMightReturn |= !F->doesNotReturn(); 00095 } else { 00096 bool CheckUnwind = !SCCMightUnwind && !F->doesNotThrow(); 00097 bool CheckReturn = !SCCMightReturn && !F->doesNotReturn(); 00098 00099 if (!CheckUnwind && !CheckReturn) 00100 continue; 00101 00102 // Check to see if this function performs an unwind or calls an 00103 // unwinding function. 00104 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { 00105 if (CheckUnwind && isa<ResumeInst>(BB->getTerminator())) { 00106 // Uses unwind / resume! 00107 SCCMightUnwind = true; 00108 } else if (CheckReturn && isa<ReturnInst>(BB->getTerminator())) { 00109 SCCMightReturn = true; 00110 } 00111 00112 // Invoke instructions don't allow unwinding to continue, so we are 00113 // only interested in call instructions. 00114 if (CheckUnwind && !SCCMightUnwind) 00115 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) 00116 if (CallInst *CI = dyn_cast<CallInst>(I)) { 00117 if (CI->doesNotThrow()) { 00118 // This call cannot throw. 00119 } else if (Function *Callee = CI->getCalledFunction()) { 00120 CallGraphNode *CalleeNode = CG[Callee]; 00121 // If the callee is outside our current SCC then we may 00122 // throw because it might. 00123 if (!SCCNodes.count(CalleeNode)) { 00124 SCCMightUnwind = true; 00125 break; 00126 } 00127 } else { 00128 // Indirect call, it might throw. 00129 SCCMightUnwind = true; 00130 break; 00131 } 00132 } 00133 if (SCCMightUnwind && SCCMightReturn) break; 00134 } 00135 } 00136 } 00137 00138 // If the SCC doesn't unwind or doesn't throw, note this fact. 00139 if (!SCCMightUnwind || !SCCMightReturn) 00140 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 00141 AttrBuilder NewAttributes; 00142 00143 if (!SCCMightUnwind) 00144 NewAttributes.addAttribute(Attribute::NoUnwind); 00145 if (!SCCMightReturn) 00146 NewAttributes.addAttribute(Attribute::NoReturn); 00147 00148 Function *F = (*I)->getFunction(); 00149 const AttributeSet &PAL = F->getAttributes().getFnAttributes(); 00150 const AttributeSet &NPAL = AttributeSet::get( 00151 F->getContext(), AttributeSet::FunctionIndex, NewAttributes); 00152 00153 if (PAL != NPAL) { 00154 MadeChange = true; 00155 F->addAttributes(AttributeSet::FunctionIndex, NPAL); 00156 } 00157 } 00158 00159 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 00160 // Convert any invoke instructions to non-throwing functions in this node 00161 // into call instructions with a branch. This makes the exception blocks 00162 // dead. 00163 if (Function *F = (*I)->getFunction()) 00164 MadeChange |= SimplifyFunction(F); 00165 } 00166 00167 return MadeChange; 00168 } 00169 00170 00171 // SimplifyFunction - Given information about callees, simplify the specified 00172 // function if we have invokes to non-unwinding functions or code after calls to 00173 // no-return functions. 00174 bool PruneEH::SimplifyFunction(Function *F) { 00175 bool MadeChange = false; 00176 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { 00177 if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) 00178 if (II->doesNotThrow()) { 00179 SmallVector<Value*, 8> Args(II->op_begin(), II->op_end() - 3); 00180 // Insert a call instruction before the invoke. 00181 CallInst *Call = CallInst::Create(II->getCalledValue(), Args, "", II); 00182 Call->takeName(II); 00183 Call->setCallingConv(II->getCallingConv()); 00184 Call->setAttributes(II->getAttributes()); 00185 Call->setDebugLoc(II->getDebugLoc()); 00186 00187 // Anything that used the value produced by the invoke instruction 00188 // now uses the value produced by the call instruction. Note that we 00189 // do this even for void functions and calls with no uses so that the 00190 // callgraph edge is updated. 00191 II->replaceAllUsesWith(Call); 00192 BasicBlock *UnwindBlock = II->getUnwindDest(); 00193 UnwindBlock->removePredecessor(II->getParent()); 00194 00195 // Insert a branch to the normal destination right before the 00196 // invoke. 00197 BranchInst::Create(II->getNormalDest(), II); 00198 00199 // Finally, delete the invoke instruction! 00200 BB->getInstList().pop_back(); 00201 00202 // If the unwind block is now dead, nuke it. 00203 if (pred_begin(UnwindBlock) == pred_end(UnwindBlock)) 00204 DeleteBasicBlock(UnwindBlock); // Delete the new BB. 00205 00206 ++NumRemoved; 00207 MadeChange = true; 00208 } 00209 00210 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) 00211 if (CallInst *CI = dyn_cast<CallInst>(I++)) 00212 if (CI->doesNotReturn() && !isa<UnreachableInst>(I)) { 00213 // This call calls a function that cannot return. Insert an 00214 // unreachable instruction after it and simplify the code. Do this 00215 // by splitting the BB, adding the unreachable, then deleting the 00216 // new BB. 00217 BasicBlock *New = BB->splitBasicBlock(I); 00218 00219 // Remove the uncond branch and add an unreachable. 00220 BB->getInstList().pop_back(); 00221 new UnreachableInst(BB->getContext(), BB); 00222 00223 DeleteBasicBlock(New); // Delete the new BB. 00224 MadeChange = true; 00225 ++NumUnreach; 00226 break; 00227 } 00228 } 00229 00230 return MadeChange; 00231 } 00232 00233 /// DeleteBasicBlock - remove the specified basic block from the program, 00234 /// updating the callgraph to reflect any now-obsolete edges due to calls that 00235 /// exist in the BB. 00236 void PruneEH::DeleteBasicBlock(BasicBlock *BB) { 00237 assert(pred_begin(BB) == pred_end(BB) && "BB is not dead!"); 00238 CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph(); 00239 00240 CallGraphNode *CGN = CG[BB->getParent()]; 00241 for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; ) { 00242 --I; 00243 if (CallInst *CI = dyn_cast<CallInst>(I)) { 00244 if (!isa<IntrinsicInst>(I)) 00245 CGN->removeCallEdgeFor(CI); 00246 } else if (InvokeInst *II = dyn_cast<InvokeInst>(I)) 00247 CGN->removeCallEdgeFor(II); 00248 if (!I->use_empty()) 00249 I->replaceAllUsesWith(UndefValue::get(I->getType())); 00250 } 00251 00252 // Get the list of successors of this block. 00253 std::vector<BasicBlock*> Succs(succ_begin(BB), succ_end(BB)); 00254 00255 for (unsigned i = 0, e = Succs.size(); i != e; ++i) 00256 Succs[i]->removePredecessor(BB); 00257 00258 BB->eraseFromParent(); 00259 }