LLVM API Documentation
00001 //===- LowerSetJmp.cpp - Code pertaining to lowering set/long jumps -------===// 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 the lowering of setjmp and longjmp to use the 00011 // LLVM invoke and unwind instructions as necessary. 00012 // 00013 // Lowering of longjmp is fairly trivial. We replace the call with a 00014 // call to the LLVM library function "__llvm_sjljeh_throw_longjmp()". 00015 // This unwinds the stack for us calling all of the destructors for 00016 // objects allocated on the stack. 00017 // 00018 // At a setjmp call, the basic block is split and the setjmp removed. 00019 // The calls in a function that have a setjmp are converted to invoke 00020 // where the except part checks to see if it's a longjmp exception and, 00021 // if so, if it's handled in the function. If it is, then it gets the 00022 // value returned by the longjmp and goes to where the basic block was 00023 // split. Invoke instructions are handled in a similar fashion with the 00024 // original except block being executed if it isn't a longjmp except 00025 // that is handled by that function. 00026 // 00027 //===----------------------------------------------------------------------===// 00028 00029 //===----------------------------------------------------------------------===// 00030 // FIXME: This pass doesn't deal with PHI statements just yet. That is, 00031 // we expect this to occur before SSAification is done. This would seem 00032 // to make sense, but in general, it might be a good idea to make this 00033 // pass invokable via the "opt" command at will. 00034 //===----------------------------------------------------------------------===// 00035 00036 #define DEBUG_TYPE "lowersetjmp" 00037 #include "llvm/Transforms/IPO.h" 00038 #include "llvm/Constants.h" 00039 #include "llvm/DerivedTypes.h" 00040 #include "llvm/Instructions.h" 00041 #include "llvm/Intrinsics.h" 00042 #include "llvm/LLVMContext.h" 00043 #include "llvm/Module.h" 00044 #include "llvm/Pass.h" 00045 #include "llvm/Support/CFG.h" 00046 #include "llvm/Support/Compiler.h" 00047 #include "llvm/Support/InstVisitor.h" 00048 #include "llvm/Transforms/Utils/Local.h" 00049 #include "llvm/ADT/DepthFirstIterator.h" 00050 #include "llvm/ADT/Statistic.h" 00051 #include "llvm/ADT/StringExtras.h" 00052 #include "llvm/ADT/VectorExtras.h" 00053 #include "llvm/ADT/SmallVector.h" 00054 #include <map> 00055 using namespace llvm; 00056 00057 STATISTIC(LongJmpsTransformed, "Number of longjmps transformed"); 00058 STATISTIC(SetJmpsTransformed , "Number of setjmps transformed"); 00059 STATISTIC(CallsTransformed , "Number of calls invokified"); 00060 STATISTIC(InvokesTransformed , "Number of invokes modified"); 00061 00062 namespace { 00063 //===--------------------------------------------------------------------===// 00064 // LowerSetJmp pass implementation. 00065 class VISIBILITY_HIDDEN LowerSetJmp : public ModulePass, 00066 public InstVisitor<LowerSetJmp> { 00067 // LLVM library functions... 00068 Constant *InitSJMap; // __llvm_sjljeh_init_setjmpmap 00069 Constant *DestroySJMap; // __llvm_sjljeh_destroy_setjmpmap 00070 Constant *AddSJToMap; // __llvm_sjljeh_add_setjmp_to_map 00071 Constant *ThrowLongJmp; // __llvm_sjljeh_throw_longjmp 00072 Constant *TryCatchLJ; // __llvm_sjljeh_try_catching_longjmp_exception 00073 Constant *IsLJException; // __llvm_sjljeh_is_longjmp_exception 00074 Constant *GetLJValue; // __llvm_sjljeh_get_longjmp_value 00075 00076 typedef std::pair<SwitchInst*, CallInst*> SwitchValuePair; 00077 00078 // Keep track of those basic blocks reachable via a depth-first search of 00079 // the CFG from a setjmp call. We only need to transform those "call" and 00080 // "invoke" instructions that are reachable from the setjmp call site. 00081 std::set<BasicBlock*> DFSBlocks; 00082 00083 // The setjmp map is going to hold information about which setjmps 00084 // were called (each setjmp gets its own number) and with which 00085 // buffer it was called. 00086 std::map<Function*, AllocaInst*> SJMap; 00087 00088 // The rethrow basic block map holds the basic block to branch to if 00089 // the exception isn't handled in the current function and needs to 00090 // be rethrown. 00091 std::map<const Function*, BasicBlock*> RethrowBBMap; 00092 00093 // The preliminary basic block map holds a basic block that grabs the 00094 // exception and determines if it's handled by the current function. 00095 std::map<const Function*, BasicBlock*> PrelimBBMap; 00096 00097 // The switch/value map holds a switch inst/call inst pair. The 00098 // switch inst controls which handler (if any) gets called and the 00099 // value is the value returned to that handler by the call to 00100 // __llvm_sjljeh_get_longjmp_value. 00101 std::map<const Function*, SwitchValuePair> SwitchValMap; 00102 00103 // A map of which setjmps we've seen so far in a function. 00104 std::map<const Function*, unsigned> SetJmpIDMap; 00105 00106 AllocaInst* GetSetJmpMap(Function* Func); 00107 BasicBlock* GetRethrowBB(Function* Func); 00108 SwitchValuePair GetSJSwitch(Function* Func, BasicBlock* Rethrow); 00109 00110 void TransformLongJmpCall(CallInst* Inst); 00111 void TransformSetJmpCall(CallInst* Inst); 00112 00113 bool IsTransformableFunction(const std::string& Name); 00114 public: 00115 static char ID; // Pass identification, replacement for typeid 00116 LowerSetJmp() : ModulePass(&ID) {} 00117 00118 void visitCallInst(CallInst& CI); 00119 void visitInvokeInst(InvokeInst& II); 00120 void visitReturnInst(ReturnInst& RI); 00121 void visitUnwindInst(UnwindInst& UI); 00122 00123 bool runOnModule(Module& M); 00124 bool doInitialization(Module& M); 00125 }; 00126 } // end anonymous namespace 00127 00128 char LowerSetJmp::ID = 0; 00129 static RegisterPass<LowerSetJmp> X("lowersetjmp", "Lower Set Jump"); 00130 00131 // run - Run the transformation on the program. We grab the function 00132 // prototypes for longjmp and setjmp. If they are used in the program, 00133 // then we can go directly to the places they're at and transform them. 00134 bool LowerSetJmp::runOnModule(Module& M) { 00135 bool Changed = false; 00136 00137 // These are what the functions are called. 00138 Function* SetJmp = M.getFunction("llvm.setjmp"); 00139 Function* LongJmp = M.getFunction("llvm.longjmp"); 00140 00141 // This program doesn't have longjmp and setjmp calls. 00142 if ((!LongJmp || LongJmp->use_empty()) && 00143 (!SetJmp || SetJmp->use_empty())) return false; 00144 00145 // Initialize some values and functions we'll need to transform the 00146 // setjmp/longjmp functions. 00147 doInitialization(M); 00148 00149 if (SetJmp) { 00150 for (Value::use_iterator B = SetJmp->use_begin(), E = SetJmp->use_end(); 00151 B != E; ++B) { 00152 BasicBlock* BB = cast<Instruction>(*B)->getParent(); 00153 for (df_ext_iterator<BasicBlock*> I = df_ext_begin(BB, DFSBlocks), 00154 E = df_ext_end(BB, DFSBlocks); I != E; ++I) 00155 /* empty */; 00156 } 00157 00158 while (!SetJmp->use_empty()) { 00159 assert(isa<CallInst>(SetJmp->use_back()) && 00160 "User of setjmp intrinsic not a call?"); 00161 TransformSetJmpCall(cast<CallInst>(SetJmp->use_back())); 00162 Changed = true; 00163 } 00164 } 00165 00166 if (LongJmp) 00167 while (!LongJmp->use_empty()) { 00168 assert(isa<CallInst>(LongJmp->use_back()) && 00169 "User of longjmp intrinsic not a call?"); 00170 TransformLongJmpCall(cast<CallInst>(LongJmp->use_back())); 00171 Changed = true; 00172 } 00173 00174 // Now go through the affected functions and convert calls and invokes 00175 // to new invokes... 00176 for (std::map<Function*, AllocaInst*>::iterator 00177 B = SJMap.begin(), E = SJMap.end(); B != E; ++B) { 00178 Function* F = B->first; 00179 for (Function::iterator BB = F->begin(), BE = F->end(); BB != BE; ++BB) 00180 for (BasicBlock::iterator IB = BB->begin(), IE = BB->end(); IB != IE; ) { 00181 visit(*IB++); 00182 if (IB != BB->end() && IB->getParent() != BB) 00183 break; // The next instruction got moved to a different block! 00184 } 00185 } 00186 00187 DFSBlocks.clear(); 00188 SJMap.clear(); 00189 RethrowBBMap.clear(); 00190 PrelimBBMap.clear(); 00191 SwitchValMap.clear(); 00192 SetJmpIDMap.clear(); 00193 00194 return Changed; 00195 } 00196 00197 // doInitialization - For the lower long/setjmp pass, this ensures that a 00198 // module contains a declaration for the intrisic functions we are going 00199 // to call to convert longjmp and setjmp calls. 00200 // 00201 // This function is always successful, unless it isn't. 00202 bool LowerSetJmp::doInitialization(Module& M) 00203 { 00204 const Type *SBPTy = PointerType::getUnqual(Type::getInt8Ty(M.getContext())); 00205 const Type *SBPPTy = PointerType::getUnqual(SBPTy); 00206 00207 // N.B. See llvm/runtime/GCCLibraries/libexception/SJLJ-Exception.h for 00208 // a description of the following library functions. 00209 00210 // void __llvm_sjljeh_init_setjmpmap(void**) 00211 InitSJMap = M.getOrInsertFunction("__llvm_sjljeh_init_setjmpmap", 00212 Type::getVoidTy(M.getContext()), 00213 SBPPTy, (Type *)0); 00214 // void __llvm_sjljeh_destroy_setjmpmap(void**) 00215 DestroySJMap = M.getOrInsertFunction("__llvm_sjljeh_destroy_setjmpmap", 00216 Type::getVoidTy(M.getContext()), 00217 SBPPTy, (Type *)0); 00218 00219 // void __llvm_sjljeh_add_setjmp_to_map(void**, void*, unsigned) 00220 AddSJToMap = M.getOrInsertFunction("__llvm_sjljeh_add_setjmp_to_map", 00221 Type::getVoidTy(M.getContext()), 00222 SBPPTy, SBPTy, 00223 Type::getInt32Ty(M.getContext()), 00224 (Type *)0); 00225 00226 // void __llvm_sjljeh_throw_longjmp(int*, int) 00227 ThrowLongJmp = M.getOrInsertFunction("__llvm_sjljeh_throw_longjmp", 00228 Type::getVoidTy(M.getContext()), SBPTy, 00229 Type::getInt32Ty(M.getContext()), 00230 (Type *)0); 00231 00232 // unsigned __llvm_sjljeh_try_catching_longjmp_exception(void **) 00233 TryCatchLJ = 00234 M.getOrInsertFunction("__llvm_sjljeh_try_catching_longjmp_exception", 00235 Type::getInt32Ty(M.getContext()), SBPPTy, (Type *)0); 00236 00237 // bool __llvm_sjljeh_is_longjmp_exception() 00238 IsLJException = M.getOrInsertFunction("__llvm_sjljeh_is_longjmp_exception", 00239 Type::getInt1Ty(M.getContext()), 00240 (Type *)0); 00241 00242 // int __llvm_sjljeh_get_longjmp_value() 00243 GetLJValue = M.getOrInsertFunction("__llvm_sjljeh_get_longjmp_value", 00244 Type::getInt32Ty(M.getContext()), 00245 (Type *)0); 00246 return true; 00247 } 00248 00249 // IsTransformableFunction - Return true if the function name isn't one 00250 // of the ones we don't want transformed. Currently, don't transform any 00251 // "llvm.{setjmp,longjmp}" functions and none of the setjmp/longjmp error 00252 // handling functions (beginning with __llvm_sjljeh_...they don't throw 00253 // exceptions). 00254 bool LowerSetJmp::IsTransformableFunction(const std::string& Name) { 00255 std::string SJLJEh("__llvm_sjljeh"); 00256 00257 if (Name.size() > SJLJEh.size()) 00258 return std::string(Name.begin(), Name.begin() + SJLJEh.size()) != SJLJEh; 00259 00260 return true; 00261 } 00262 00263 // TransformLongJmpCall - Transform a longjmp call into a call to the 00264 // internal __llvm_sjljeh_throw_longjmp function. It then takes care of 00265 // throwing the exception for us. 00266 void LowerSetJmp::TransformLongJmpCall(CallInst* Inst) 00267 { 00268 const Type* SBPTy = 00269 PointerType::getUnqual(Type::getInt8Ty(Inst->getContext())); 00270 00271 // Create the call to "__llvm_sjljeh_throw_longjmp". This takes the 00272 // same parameters as "longjmp", except that the buffer is cast to a 00273 // char*. It returns "void", so it doesn't need to replace any of 00274 // Inst's uses and doesn't get a name. 00275 CastInst* CI = 00276 new BitCastInst(Inst->getOperand(1), SBPTy, "LJBuf", Inst); 00277 SmallVector<Value *, 2> Args; 00278 Args.push_back(CI); 00279 Args.push_back(Inst->getOperand(2)); 00280 CallInst::Create(ThrowLongJmp, Args.begin(), Args.end(), "", Inst); 00281 00282 SwitchValuePair& SVP = SwitchValMap[Inst->getParent()->getParent()]; 00283 00284 // If the function has a setjmp call in it (they are transformed first) 00285 // we should branch to the basic block that determines if this longjmp 00286 // is applicable here. Otherwise, issue an unwind. 00287 if (SVP.first) 00288 BranchInst::Create(SVP.first->getParent(), Inst); 00289 else 00290 new UnwindInst(Inst->getContext(), Inst); 00291 00292 // Remove all insts after the branch/unwind inst. Go from back to front to 00293 // avoid replaceAllUsesWith if possible. 00294 BasicBlock *BB = Inst->getParent(); 00295 Instruction *Removed; 00296 do { 00297 Removed = &BB->back(); 00298 // If the removed instructions have any users, replace them now. 00299 if (!Removed->use_empty()) 00300 Removed->replaceAllUsesWith(UndefValue::get(Removed->getType())); 00301 Removed->eraseFromParent(); 00302 } while (Removed != Inst); 00303 00304 ++LongJmpsTransformed; 00305 } 00306 00307 // GetSetJmpMap - Retrieve (create and initialize, if necessary) the 00308 // setjmp map. This map is going to hold information about which setjmps 00309 // were called (each setjmp gets its own number) and with which buffer it 00310 // was called. There can be only one! 00311 AllocaInst* LowerSetJmp::GetSetJmpMap(Function* Func) 00312 { 00313 if (SJMap[Func]) return SJMap[Func]; 00314 00315 // Insert the setjmp map initialization before the first instruction in 00316 // the function. 00317 Instruction* Inst = Func->getEntryBlock().begin(); 00318 assert(Inst && "Couldn't find even ONE instruction in entry block!"); 00319 00320 // Fill in the alloca and call to initialize the SJ map. 00321 const Type *SBPTy = 00322 PointerType::getUnqual(Type::getInt8Ty(Func->getContext())); 00323 AllocaInst* Map = new AllocaInst(SBPTy, 0, "SJMap", Inst); 00324 CallInst::Create(InitSJMap, Map, "", Inst); 00325 return SJMap[Func] = Map; 00326 } 00327 00328 // GetRethrowBB - Only one rethrow basic block is needed per function. 00329 // If this is a longjmp exception but not handled in this block, this BB 00330 // performs the rethrow. 00331 BasicBlock* LowerSetJmp::GetRethrowBB(Function* Func) 00332 { 00333 if (RethrowBBMap[Func]) return RethrowBBMap[Func]; 00334 00335 // The basic block we're going to jump to if we need to rethrow the 00336 // exception. 00337 BasicBlock* Rethrow = 00338 BasicBlock::Create(Func->getContext(), "RethrowExcept", Func); 00339 00340 // Fill in the "Rethrow" BB with a call to rethrow the exception. This 00341 // is the last instruction in the BB since at this point the runtime 00342 // should exit this function and go to the next function. 00343 new UnwindInst(Func->getContext(), Rethrow); 00344 return RethrowBBMap[Func] = Rethrow; 00345 } 00346 00347 // GetSJSwitch - Return the switch statement that controls which handler 00348 // (if any) gets called and the value returned to that handler. 00349 LowerSetJmp::SwitchValuePair LowerSetJmp::GetSJSwitch(Function* Func, 00350 BasicBlock* Rethrow) 00351 { 00352 if (SwitchValMap[Func].first) return SwitchValMap[Func]; 00353 00354 BasicBlock* LongJmpPre = 00355 BasicBlock::Create(Func->getContext(), "LongJmpBlkPre", Func); 00356 00357 // Keep track of the preliminary basic block for some of the other 00358 // transformations. 00359 PrelimBBMap[Func] = LongJmpPre; 00360 00361 // Grab the exception. 00362 CallInst* Cond = CallInst::Create(IsLJException, "IsLJExcept", LongJmpPre); 00363 00364 // The "decision basic block" gets the number associated with the 00365 // setjmp call returning to switch on and the value returned by 00366 // longjmp. 00367 BasicBlock* DecisionBB = 00368 BasicBlock::Create(Func->getContext(), "LJDecisionBB", Func); 00369 00370 BranchInst::Create(DecisionBB, Rethrow, Cond, LongJmpPre); 00371 00372 // Fill in the "decision" basic block. 00373 CallInst* LJVal = CallInst::Create(GetLJValue, "LJVal", DecisionBB); 00374 CallInst* SJNum = CallInst::Create(TryCatchLJ, GetSetJmpMap(Func), "SJNum", 00375 DecisionBB); 00376 00377 SwitchInst* SI = SwitchInst::Create(SJNum, Rethrow, 0, DecisionBB); 00378 return SwitchValMap[Func] = SwitchValuePair(SI, LJVal); 00379 } 00380 00381 // TransformSetJmpCall - The setjmp call is a bit trickier to transform. 00382 // We're going to convert all setjmp calls to nops. Then all "call" and 00383 // "invoke" instructions in the function are converted to "invoke" where 00384 // the "except" branch is used when returning from a longjmp call. 00385 void LowerSetJmp::TransformSetJmpCall(CallInst* Inst) 00386 { 00387 BasicBlock* ABlock = Inst->getParent(); 00388 Function* Func = ABlock->getParent(); 00389 00390 // Add this setjmp to the setjmp map. 00391 const Type* SBPTy = 00392 PointerType::getUnqual(Type::getInt8Ty(Inst->getContext())); 00393 CastInst* BufPtr = 00394 new BitCastInst(Inst->getOperand(1), SBPTy, "SBJmpBuf", Inst); 00395 std::vector<Value*> Args = 00396 make_vector<Value*>(GetSetJmpMap(Func), BufPtr, 00397 ConstantInt::get(Type::getInt32Ty(Inst->getContext()), 00398 SetJmpIDMap[Func]++), 0); 00399 CallInst::Create(AddSJToMap, Args.begin(), Args.end(), "", Inst); 00400 00401 // We are guaranteed that there are no values live across basic blocks 00402 // (because we are "not in SSA form" yet), but there can still be values live 00403 // in basic blocks. Because of this, splitting the setjmp block can cause 00404 // values above the setjmp to not dominate uses which are after the setjmp 00405 // call. For all of these occasions, we must spill the value to the stack. 00406 // 00407 std::set<Instruction*> InstrsAfterCall; 00408 00409 // The call is probably very close to the end of the basic block, for the 00410 // common usage pattern of: 'if (setjmp(...))', so keep track of the 00411 // instructions after the call. 00412 for (BasicBlock::iterator I = ++BasicBlock::iterator(Inst), E = ABlock->end(); 00413 I != E; ++I) 00414 InstrsAfterCall.insert(I); 00415 00416 for (BasicBlock::iterator II = ABlock->begin(); 00417 II != BasicBlock::iterator(Inst); ++II) 00418 // Loop over all of the uses of instruction. If any of them are after the 00419 // call, "spill" the value to the stack. 00420 for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); 00421 UI != E; ++UI) 00422 if (cast<Instruction>(*UI)->getParent() != ABlock || 00423 InstrsAfterCall.count(cast<Instruction>(*UI))) { 00424 DemoteRegToStack(*II); 00425 break; 00426 } 00427 InstrsAfterCall.clear(); 00428 00429 // Change the setjmp call into a branch statement. We'll remove the 00430 // setjmp call in a little bit. No worries. 00431 BasicBlock* SetJmpContBlock = ABlock->splitBasicBlock(Inst); 00432 assert(SetJmpContBlock && "Couldn't split setjmp BB!!"); 00433 00434 SetJmpContBlock->setName(ABlock->getName()+"SetJmpCont"); 00435 00436 // Add the SetJmpContBlock to the set of blocks reachable from a setjmp. 00437 DFSBlocks.insert(SetJmpContBlock); 00438 00439 // This PHI node will be in the new block created from the 00440 // splitBasicBlock call. 00441 PHINode* PHI = PHINode::Create(Type::getInt32Ty(Inst->getContext()), 00442 "SetJmpReturn", Inst); 00443 00444 // Coming from a call to setjmp, the return is 0. 00445 PHI->addIncoming(Constant::getNullValue(Type::getInt32Ty(Inst->getContext())), 00446 ABlock); 00447 00448 // Add the case for this setjmp's number... 00449 SwitchValuePair SVP = GetSJSwitch(Func, GetRethrowBB(Func)); 00450 SVP.first->addCase(ConstantInt::get(Type::getInt32Ty(Inst->getContext()), 00451 SetJmpIDMap[Func] - 1), 00452 SetJmpContBlock); 00453 00454 // Value coming from the handling of the exception. 00455 PHI->addIncoming(SVP.second, SVP.second->getParent()); 00456 00457 // Replace all uses of this instruction with the PHI node created by 00458 // the eradication of setjmp. 00459 Inst->replaceAllUsesWith(PHI); 00460 Inst->eraseFromParent(); 00461 00462 ++SetJmpsTransformed; 00463 } 00464 00465 // visitCallInst - This converts all LLVM call instructions into invoke 00466 // instructions. The except part of the invoke goes to the "LongJmpBlkPre" 00467 // that grabs the exception and proceeds to determine if it's a longjmp 00468 // exception or not. 00469 void LowerSetJmp::visitCallInst(CallInst& CI) 00470 { 00471 if (CI.getCalledFunction()) 00472 if (!IsTransformableFunction(CI.getCalledFunction()->getName()) || 00473 CI.getCalledFunction()->isIntrinsic()) return; 00474 00475 BasicBlock* OldBB = CI.getParent(); 00476 00477 // If not reachable from a setjmp call, don't transform. 00478 if (!DFSBlocks.count(OldBB)) return; 00479 00480 BasicBlock* NewBB = OldBB->splitBasicBlock(CI); 00481 assert(NewBB && "Couldn't split BB of \"call\" instruction!!"); 00482 DFSBlocks.insert(NewBB); 00483 NewBB->setName("Call2Invoke"); 00484 00485 Function* Func = OldBB->getParent(); 00486 00487 // Construct the new "invoke" instruction. 00488 TerminatorInst* Term = OldBB->getTerminator(); 00489 std::vector<Value*> Params(CI.op_begin() + 1, CI.op_end()); 00490 InvokeInst* II = 00491 InvokeInst::Create(CI.getCalledValue(), NewBB, PrelimBBMap[Func], 00492 Params.begin(), Params.end(), CI.getName(), Term); 00493 II->setCallingConv(CI.getCallingConv()); 00494 II->setAttributes(CI.getAttributes()); 00495 00496 // Replace the old call inst with the invoke inst and remove the call. 00497 CI.replaceAllUsesWith(II); 00498 CI.eraseFromParent(); 00499 00500 // The old terminator is useless now that we have the invoke inst. 00501 Term->eraseFromParent(); 00502 ++CallsTransformed; 00503 } 00504 00505 // visitInvokeInst - Converting the "invoke" instruction is fairly 00506 // straight-forward. The old exception part is replaced by a query asking 00507 // if this is a longjmp exception. If it is, then it goes to the longjmp 00508 // exception blocks. Otherwise, control is passed the old exception. 00509 void LowerSetJmp::visitInvokeInst(InvokeInst& II) 00510 { 00511 if (II.getCalledFunction()) 00512 if (!IsTransformableFunction(II.getCalledFunction()->getName()) || 00513 II.getCalledFunction()->isIntrinsic()) return; 00514 00515 BasicBlock* BB = II.getParent(); 00516 00517 // If not reachable from a setjmp call, don't transform. 00518 if (!DFSBlocks.count(BB)) return; 00519 00520 BasicBlock* ExceptBB = II.getUnwindDest(); 00521 00522 Function* Func = BB->getParent(); 00523 BasicBlock* NewExceptBB = BasicBlock::Create(II.getContext(), 00524 "InvokeExcept", Func); 00525 00526 // If this is a longjmp exception, then branch to the preliminary BB of 00527 // the longjmp exception handling. Otherwise, go to the old exception. 00528 CallInst* IsLJExcept = CallInst::Create(IsLJException, "IsLJExcept", 00529 NewExceptBB); 00530 00531 BranchInst::Create(PrelimBBMap[Func], ExceptBB, IsLJExcept, NewExceptBB); 00532 00533 II.setUnwindDest(NewExceptBB); 00534 ++InvokesTransformed; 00535 } 00536 00537 // visitReturnInst - We want to destroy the setjmp map upon exit from the 00538 // function. 00539 void LowerSetJmp::visitReturnInst(ReturnInst &RI) { 00540 Function* Func = RI.getParent()->getParent(); 00541 CallInst::Create(DestroySJMap, GetSetJmpMap(Func), "", &RI); 00542 } 00543 00544 // visitUnwindInst - We want to destroy the setjmp map upon exit from the 00545 // function. 00546 void LowerSetJmp::visitUnwindInst(UnwindInst &UI) { 00547 Function* Func = UI.getParent()->getParent(); 00548 CallInst::Create(DestroySJMap, GetSetJmpMap(Func), "", &UI); 00549 } 00550 00551 ModulePass *llvm::createLowerSetJmpPass() { 00552 return new LowerSetJmp(); 00553 } 00554