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