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