1 //===- LowerSetJmp.cpp - Code pertaining to lowering set/long jumps -------===//
3 // This file implements the lowering of setjmp and longjmp to use the
4 // LLVM invoke and unwind instructions as necessary.
6 // Lowering of longjmp is fairly trivial. We replace the call with a
7 // call to the LLVM library function "__llvm_sjljeh_throw_longjmp()".
8 // This unwinds the stack for us calling all of the destructors for
9 // objects allocated on the stack.
11 // At a setjmp call, the basic block is split and the setjmp removed.
12 // The calls in a function that have a setjmp are converted to invoke
13 // where the except part checks to see if it's a longjmp exception and,
14 // if so, if it's handled in the function. If it is, then it gets the
15 // value returned by the longjmp and goes to where the basic block was
16 // split. Invoke instructions are handled in a similar fashion with the
17 // original except block being executed if it isn't a longjmp except
18 // that is handled by that function.
20 //===----------------------------------------------------------------------===//
22 //===----------------------------------------------------------------------===//
23 // FIXME: This pass doesn't deal with PHI statements just yet. That is,
24 // we expect this to occur before SSAification is done. This would seem
25 // to make sense, but in general, it might be a good idea to make this
26 // pass invokable via the "opt" command at will.
27 //===----------------------------------------------------------------------===//
29 #include "llvm/Constants.h"
30 #include "llvm/DerivedTypes.h"
31 #include "llvm/Instructions.h"
32 #include "llvm/Intrinsics.h"
33 #include "llvm/Module.h"
34 #include "llvm/Pass.h"
35 #include "llvm/Support/InstIterator.h"
36 #include "llvm/Support/InstVisitor.h"
37 #include "Support/Statistic.h"
38 #include "Support/StringExtras.h"
39 #include "Support/VectorExtras.h"
44 Statistic<> LongJmpsTransformed("lowersetjmp",
45 "Number of longjmps transformed");
46 Statistic<> SetJmpsTransformed("lowersetjmp",
47 "Number of setjmps transformed");
49 //===--------------------------------------------------------------------===//
50 // LowerSetJmp pass implementation. This is subclassed from the "Pass"
51 // class because it works on a module as a whole, not a function at a
54 class LowerSetJmp : public Pass,
55 public InstVisitor<LowerSetJmp> {
56 // LLVM library functions...
57 Function* InitSJMap; // __llvm_sjljeh_init_setjmpmap
58 Function* DestroySJMap; // __llvm_sjljeh_destroy_setjmpmap
59 Function* AddSJToMap; // __llvm_sjljeh_add_setjmp_to_map
60 Function* ThrowLongJmp; // __llvm_sjljeh_throw_longjmp
61 Function* TryCatchLJ; // __llvm_sjljeh_try_catching_longjmp_exception
62 Function* IsLJException; // __llvm_sjljeh_is_longjmp_exception
63 Function* GetLJValue; // __llvm_sjljeh_get_longjmp_value
65 typedef std::pair<SwitchInst*, CallInst*> SwitchValuePair;
67 // The setjmp map is going to hold information about which setjmps
68 // were called (each setjmp gets its own number) and with which
69 // buffer it was called.
70 std::map<Function*, AllocaInst*> SJMap;
72 // The rethrow basic block map holds the basic block to branch to if
73 // the exception isn't handled in the current function and needs to
75 std::map<const Function*, BasicBlock*> RethrowBBMap;
77 // The preliminary basic block map holds a basic block that grabs the
78 // exception and determines if it's handled by the current function.
79 std::map<const Function*, BasicBlock*> PrelimBBMap;
81 // The switch/value map holds a switch inst/call inst pair. The
82 // switch inst controls which handler (if any) gets called and the
83 // value is the value returned to that handler by the call to
84 // __llvm_sjljeh_get_longjmp_value.
85 std::map<const Function*, SwitchValuePair> SwitchValMap;
87 // A map of which setjmps we've seen so far in a function.
88 std::map<const Function*, unsigned> SetJmpIDMap;
90 AllocaInst* GetSetJmpMap(Function* Func);
91 BasicBlock* GetRethrowBB(Function* Func);
92 SwitchValuePair GetSJSwitch(Function* Func, BasicBlock* Rethrow);
94 void TransformLongJmpCall(CallInst* Inst);
95 void TransformSetJmpCall(CallInst* Inst);
97 bool IsTransformableFunction(const std::string& Name);
99 void visitCallInst(CallInst& CI);
100 void visitInvokeInst(InvokeInst& II);
101 void visitReturnInst(ReturnInst& RI);
102 void visitUnwindInst(UnwindInst& UI);
105 bool doInitialization(Module& M);
108 RegisterOpt<LowerSetJmp> X("lowersetjmp", "Lower Set Jump");
109 } // end anonymous namespace
111 // run - Run the transformation on the program. We grab the function
112 // prototypes for longjmp and setjmp. If they are used in the program,
113 // then we can go directly to the places they're at and transform them.
114 bool LowerSetJmp::run(Module& M)
116 bool Changed = false;
118 // These are what the functions are called.
119 Function* SetJmp = M.getNamedFunction("llvm.setjmp");
120 Function* LongJmp = M.getNamedFunction("llvm.longjmp");
122 // This program doesn't have longjmp and setjmp calls.
123 if ((!LongJmp || LongJmp->use_empty()) &&
124 (!SetJmp || SetJmp->use_empty())) return false;
126 // Initialize some values and functions we'll need to transform the
127 // setjmp/longjmp functions.
131 while (!SetJmp->use_empty()) {
132 assert(isa<CallInst>(SetJmp->use_back()) &&
133 "User of setjmp intrinsic not a call?");
134 TransformSetJmpCall(cast<CallInst>(SetJmp->use_back()));
139 while (!LongJmp->use_empty()) {
140 assert(isa<CallInst>(LongJmp->use_back()) &&
141 "User of longjmp intrinsic not a call?");
142 TransformLongJmpCall(cast<CallInst>(LongJmp->use_back()));
146 // Now go through the affected functions and convert calls and invokes
148 for (std::map<Function*, AllocaInst*>::iterator
149 B = SJMap.begin(), E = SJMap.end(); B != E; ++B) {
150 Function* F = B->first;
151 for (Function::iterator BB = F->begin(), BE = F->end(); BB != BE; ++BB)
152 for (BasicBlock::iterator IB = BB->begin(), IE = BB->end(); IB != IE; ) {
154 if (IB != BB->end() && IB->getParent() != BB)
155 break; // The next instruction got moved to a different block!
160 RethrowBBMap.clear();
162 SwitchValMap.clear();
168 // doInitialization - For the lower long/setjmp pass, this ensures that a
169 // module contains a declaration for the intrisic functions we are going
170 // to call to convert longjmp and setjmp calls.
172 // This function is always successful, unless it isn't.
173 bool LowerSetJmp::doInitialization(Module& M)
175 const Type *SBPTy = PointerType::get(Type::SByteTy);
176 const Type *SBPPTy = PointerType::get(SBPTy);
178 // N.B. See llvm/runtime/GCCLibraries/libexception/SJLJ-Exception.h for
179 // a description of the following library functions.
181 // void __llvm_sjljeh_init_setjmpmap(void**)
182 InitSJMap = M.getOrInsertFunction("__llvm_sjljeh_init_setjmpmap",
183 Type::VoidTy, SBPPTy, 0);
184 // void __llvm_sjljeh_destroy_setjmpmap(void**)
185 DestroySJMap = M.getOrInsertFunction("__llvm_sjljeh_destroy_setjmpmap",
186 Type::VoidTy, SBPPTy, 0);
188 // void __llvm_sjljeh_add_setjmp_to_map(void**, void*, unsigned)
189 AddSJToMap = M.getOrInsertFunction("__llvm_sjljeh_add_setjmp_to_map",
190 Type::VoidTy, SBPPTy, SBPTy,
193 // void __llvm_sjljeh_throw_longjmp(int*, int)
194 ThrowLongJmp = M.getOrInsertFunction("__llvm_sjljeh_throw_longjmp",
195 Type::VoidTy, SBPTy, Type::IntTy, 0);
197 // unsigned __llvm_sjljeh_try_catching_longjmp_exception(void **)
199 M.getOrInsertFunction("__llvm_sjljeh_try_catching_longjmp_exception",
200 Type::UIntTy, SBPPTy, 0);
202 // bool __llvm_sjljeh_is_longjmp_exception()
203 IsLJException = M.getOrInsertFunction("__llvm_sjljeh_is_longjmp_exception",
206 // int __llvm_sjljeh_get_longjmp_value()
207 GetLJValue = M.getOrInsertFunction("__llvm_sjljeh_get_longjmp_value",
212 // IsTransformableFunction - Return true if the function name isn't one
213 // of the ones we don't want transformed. Currently, don't transform any
214 // "llvm.{setjmp,longjmp}" functions and none of the setjmp/longjmp error
215 // handling functions (beginning with __llvm_sjljeh_...they don't throw
217 bool LowerSetJmp::IsTransformableFunction(const std::string& Name)
219 std::string SJLJEh("__llvm_sjljeh");
221 if (Name.size() > SJLJEh.size())
222 return std::string(Name.begin(), Name.begin() + SJLJEh.size()) != SJLJEh;
227 // TransformLongJmpCall - Transform a longjmp call into a call to the
228 // internal __llvm_sjljeh_throw_longjmp function. It then takes care of
229 // throwing the exception for us.
230 void LowerSetJmp::TransformLongJmpCall(CallInst* Inst)
232 const Type* SBPTy = PointerType::get(Type::SByteTy);
234 // Create the call to "__llvm_sjljeh_throw_longjmp". This takes the
235 // same parameters as "longjmp", except that the buffer is cast to a
236 // char*. It returns "void", so it doesn't need to replace any of
237 // Inst's uses and doesn't get a name.
238 CastInst* CI = new CastInst(Inst->getOperand(1), SBPTy, "LJBuf", Inst);
239 new CallInst(ThrowLongJmp, make_vector<Value*>(CI, Inst->getOperand(2), 0),
242 SwitchValuePair& SVP = SwitchValMap[Inst->getParent()->getParent()];
244 // If the function has a setjmp call in it (they are transformed first)
245 // we should branch to the basic block that determines if this longjmp
246 // is applicable here. Otherwise, issue an unwind.
248 new BranchInst(SVP.first->getParent(), Inst);
250 new UnwindInst(Inst);
252 // Remove all insts after the branch/unwind inst.
253 Inst->getParent()->getInstList().erase(Inst,
254 Inst->getParent()->getInstList().end());
256 ++LongJmpsTransformed;
259 // GetSetJmpMap - Retrieve (create and initialize, if necessary) the
260 // setjmp map. This map is going to hold information about which setjmps
261 // were called (each setjmp gets its own number) and with which buffer it
262 // was called. There can be only one!
263 AllocaInst* LowerSetJmp::GetSetJmpMap(Function* Func)
265 if (SJMap[Func]) return SJMap[Func];
267 // Insert the setjmp map initialization before the first instruction in
269 Instruction* Inst = Func->getEntryBlock().begin();
270 assert(Inst && "Couldn't find even ONE instruction in entry block!");
272 // Fill in the alloca and call to initialize the SJ map.
273 const Type *SBPTy = PointerType::get(Type::SByteTy);
274 AllocaInst* Map = new AllocaInst(SBPTy, 0, "SJMap", Inst);
275 new CallInst(InitSJMap, make_vector<Value*>(Map, 0), "", Inst);
276 return SJMap[Func] = Map;
279 // GetRethrowBB - Only one rethrow basic block is needed per function.
280 // If this is a longjmp exception but not handled in this block, this BB
281 // performs the rethrow.
282 BasicBlock* LowerSetJmp::GetRethrowBB(Function* Func)
284 if (RethrowBBMap[Func]) return RethrowBBMap[Func];
286 // The basic block we're going to jump to if we need to rethrow the
288 BasicBlock* Rethrow = new BasicBlock("RethrowExcept", Func);
289 BasicBlock::InstListType& RethrowBlkIL = Rethrow->getInstList();
291 // Fill in the "Rethrow" BB with a call to rethrow the exception. This
292 // is the last instruction in the BB since at this point the runtime
293 // should exit this function and go to the next function.
294 RethrowBlkIL.push_back(new UnwindInst());
295 return RethrowBBMap[Func] = Rethrow;
298 // GetSJSwitch - Return the switch statement that controls which handler
299 // (if any) gets called and the value returned to that handler.
300 LowerSetJmp::SwitchValuePair LowerSetJmp::GetSJSwitch(Function* Func,
303 if (SwitchValMap[Func].first) return SwitchValMap[Func];
305 BasicBlock* LongJmpPre = new BasicBlock("LongJmpBlkPre", Func);
306 BasicBlock::InstListType& LongJmpPreIL = LongJmpPre->getInstList();
308 // Keep track of the preliminary basic block for some of the other
310 PrelimBBMap[Func] = LongJmpPre;
312 // Grab the exception.
314 CallInst(IsLJException, std::vector<Value*>(), "IsLJExcept");
315 LongJmpPreIL.push_back(Cond);
317 // The "decision basic block" gets the number associated with the
318 // setjmp call returning to switch on and the value returned by
320 BasicBlock* DecisionBB = new BasicBlock("LJDecisionBB", Func);
321 BasicBlock::InstListType& DecisionBBIL = DecisionBB->getInstList();
323 LongJmpPreIL.push_back(new BranchInst(DecisionBB, Rethrow, Cond));
325 // Fill in the "decision" basic block.
326 CallInst* LJVal = new CallInst(GetLJValue, std::vector<Value*>(), "LJVal");
327 DecisionBBIL.push_back(LJVal);
328 CallInst* SJNum = new
329 CallInst(TryCatchLJ, make_vector<Value*>(GetSetJmpMap(Func), 0), "SJNum");
330 DecisionBBIL.push_back(SJNum);
332 SwitchInst* SI = new SwitchInst(SJNum, Rethrow);
333 DecisionBBIL.push_back(SI);
334 return SwitchValMap[Func] = SwitchValuePair(SI, LJVal);
337 // TransformSetJmpCall - The setjmp call is a bit trickier to transform.
338 // We're going to convert all setjmp calls to nops. Then all "call" and
339 // "invoke" instructions in the function are converted to "invoke" where
340 // the "except" branch is used when returning from a longjmp call.
341 void LowerSetJmp::TransformSetJmpCall(CallInst* Inst)
343 BasicBlock* ABlock = Inst->getParent();
344 Function* Func = ABlock->getParent();
346 // Add this setjmp to the setjmp map.
347 const Type* SBPTy = PointerType::get(Type::SByteTy);
348 CastInst* BufPtr = new CastInst(Inst->getOperand(1), SBPTy, "SBJmpBuf", Inst);
349 new CallInst(AddSJToMap,
350 make_vector<Value*>(GetSetJmpMap(Func), BufPtr,
351 ConstantUInt::get(Type::UIntTy,
352 SetJmpIDMap[Func]++), 0),
355 // Change the setjmp call into a branch statement. We'll remove the
356 // setjmp call in a little bit. No worries.
357 BasicBlock* SetJmpContBlock = ABlock->splitBasicBlock(Inst);
358 assert(SetJmpContBlock && "Couldn't split setjmp BB!!");
360 SetJmpContBlock->setName("SetJmpContBlock");
362 // Reposition the split BB in the BB list to make things tidier.
363 Func->getBasicBlockList().remove(SetJmpContBlock);
364 Func->getBasicBlockList().insert(++Function::iterator(ABlock),
367 // This PHI node will be in the new block created from the
368 // splitBasicBlock call.
369 PHINode* PHI = new PHINode(Type::IntTy, "SetJmpReturn", Inst);
371 // Coming from a call to setjmp, the return is 0.
372 PHI->addIncoming(ConstantInt::getNullValue(Type::IntTy), ABlock);
374 // Add the case for this setjmp's number...
375 SwitchValuePair SVP = GetSJSwitch(Func, GetRethrowBB(Func));
376 SVP.first->addCase(ConstantUInt::get(Type::UIntTy, SetJmpIDMap[Func] - 1),
379 // Value coming from the handling of the exception.
380 PHI->addIncoming(SVP.second, SVP.second->getParent());
382 // Replace all uses of this instruction with the PHI node created by
383 // the eradication of setjmp.
384 Inst->replaceAllUsesWith(PHI);
385 Inst->getParent()->getInstList().erase(Inst);
387 ++SetJmpsTransformed;
390 // visitCallInst - This converts all LLVM call instructions into invoke
391 // instructions. The except part of the invoke goes to the "LongJmpBlkPre"
392 // that grabs the exception and proceeds to determine if it's a longjmp
394 void LowerSetJmp::visitCallInst(CallInst& CI)
396 if (CI.getCalledFunction())
397 if (!IsTransformableFunction(CI.getCalledFunction()->getName()) ||
398 CI.getCalledFunction()->isIntrinsic()) return;
400 BasicBlock* OldBB = CI.getParent();
401 BasicBlock* NewBB = OldBB->splitBasicBlock(CI);
402 assert(NewBB && "Couldn't split BB of \"call\" instruction!!");
403 NewBB->setName("Call2Invoke");
405 // Reposition the split BB in the BB list to make things tidier.
406 Function* Func = OldBB->getParent();
407 Func->getBasicBlockList().remove(NewBB);
408 Func->getBasicBlockList().insert(++Function::iterator(OldBB), NewBB);
410 // Construct the new "invoke" instruction.
411 TerminatorInst* Term = OldBB->getTerminator();
412 std::vector<Value*> Params(CI.op_begin() + 1, CI.op_end());
414 InvokeInst(CI.getCalledValue(), NewBB, PrelimBBMap[Func],
415 Params, CI.getName(), Term);
417 // Replace the old call inst with the invoke inst and remove the call.
418 CI.replaceAllUsesWith(II);
419 CI.getParent()->getInstList().erase(&CI);
421 // The old terminator is useless now that we have the invoke inst.
422 Term->getParent()->getInstList().erase(Term);
425 // visitInvokeInst - Converting the "invoke" instruction is fairly
426 // straight-forward. The old exception part is replaced by a query asking
427 // if this is a longjmp exception. If it is, then it goes to the longjmp
428 // exception blocks. Otherwise, control is passed the old exception.
429 void LowerSetJmp::visitInvokeInst(InvokeInst& II)
431 if (II.getCalledFunction())
432 if (!IsTransformableFunction(II.getCalledFunction()->getName()) ||
433 II.getCalledFunction()->isIntrinsic()) return;
435 Function* Func = II.getParent()->getParent();
437 BasicBlock* NormalBB = II.getNormalDest();
438 BasicBlock* ExceptBB = II.getExceptionalDest();
440 BasicBlock* NewExceptBB = new BasicBlock("InvokeExcept", Func);
441 BasicBlock::InstListType& InstList = NewExceptBB->getInstList();
443 // If this is a longjmp exception, then branch to the preliminary BB of
444 // the longjmp exception handling. Otherwise, go to the old exception.
445 CallInst* IsLJExcept = new
446 CallInst(IsLJException, std::vector<Value*>(), "IsLJExcept");
447 InstList.push_back(IsLJExcept);
449 BranchInst* BR = new BranchInst(PrelimBBMap[Func], ExceptBB, IsLJExcept);
450 InstList.push_back(BR);
452 II.setExceptionalDest(NewExceptBB);
455 // visitReturnInst - We want to destroy the setjmp map upon exit from the
457 void LowerSetJmp::visitReturnInst(ReturnInst& RI)
459 Function* Func = RI.getParent()->getParent();
460 new CallInst(DestroySJMap, make_vector<Value*>(GetSetJmpMap(Func), 0),
464 // visitUnwindInst - We want to destroy the setjmp map upon exit from the
466 void LowerSetJmp::visitUnwindInst(UnwindInst& UI)
468 Function* Func = UI.getParent()->getParent();
469 new CallInst(DestroySJMap, make_vector<Value*>(GetSetJmpMap(Func), 0),
473 Pass* createLowerSetJmpPass()
475 return new LowerSetJmp();