blob: abbc2c92c47206f03bb6f249c0626dfb04edb595 [file] [log] [blame]
Chris Lattner6f99eb52003-09-15 04:56:27 +00001//===- LowerSetJmp.cpp - Code pertaining to lowering set/long jumps -------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner6f99eb52003-09-15 04:56:27 +00009//
10// This file implements the lowering of setjmp and longjmp to use the
Chris Lattner40b66212003-09-15 05:43:05 +000011// LLVM invoke and unwind instructions as necessary.
Chris Lattner6f99eb52003-09-15 04:56:27 +000012//
13// Lowering of longjmp is fairly trivial. We replace the call with a
14// call to the LLVM library function "__llvm_sjljeh_throw_longjmp()".
15// This unwinds the stack for us calling all of the destructors for
16// objects allocated on the stack.
17//
18// At a setjmp call, the basic block is split and the setjmp removed.
19// The calls in a function that have a setjmp are converted to invoke
20// where the except part checks to see if it's a longjmp exception and,
21// if so, if it's handled in the function. If it is, then it gets the
22// value returned by the longjmp and goes to where the basic block was
23// split. Invoke instructions are handled in a similar fashion with the
24// original except block being executed if it isn't a longjmp except
25// that is handled by that function.
26//
27//===----------------------------------------------------------------------===//
28
29//===----------------------------------------------------------------------===//
30// FIXME: This pass doesn't deal with PHI statements just yet. That is,
31// we expect this to occur before SSAification is done. This would seem
32// to make sense, but in general, it might be a good idea to make this
33// pass invokable via the "opt" command at will.
34//===----------------------------------------------------------------------===//
35
36#include "llvm/Constants.h"
37#include "llvm/DerivedTypes.h"
38#include "llvm/Instructions.h"
39#include "llvm/Intrinsics.h"
40#include "llvm/Module.h"
41#include "llvm/Pass.h"
Chris Lattner56b85262003-10-13 00:57:16 +000042#include "llvm/Support/CFG.h"
Chris Lattner6f99eb52003-09-15 04:56:27 +000043#include "llvm/Support/InstVisitor.h"
Chris Lattnerb0a4b492003-11-06 19:18:49 +000044#include "llvm/Transforms/Utils/DemoteRegToStack.h"
Chris Lattner56b85262003-10-13 00:57:16 +000045#include "Support/DepthFirstIterator.h"
Chris Lattner6f99eb52003-09-15 04:56:27 +000046#include "Support/Statistic.h"
47#include "Support/StringExtras.h"
48#include "Support/VectorExtras.h"
49
Chris Lattner6f99eb52003-09-15 04:56:27 +000050namespace {
51 Statistic<> LongJmpsTransformed("lowersetjmp",
52 "Number of longjmps transformed");
53 Statistic<> SetJmpsTransformed("lowersetjmp",
54 "Number of setjmps transformed");
Chris Lattner3e5ff252003-10-28 23:14:59 +000055 Statistic<> CallsTransformed("lowersetjmp",
56 "Number of calls invokified");
57 Statistic<> InvokesTransformed("lowersetjmp",
58 "Number of invokes modified");
Chris Lattner6f99eb52003-09-15 04:56:27 +000059
60 //===--------------------------------------------------------------------===//
61 // LowerSetJmp pass implementation. This is subclassed from the "Pass"
62 // class because it works on a module as a whole, not a function at a
63 // time.
64
65 class LowerSetJmp : public Pass,
66 public InstVisitor<LowerSetJmp> {
67 // LLVM library functions...
68 Function* InitSJMap; // __llvm_sjljeh_init_setjmpmap
69 Function* DestroySJMap; // __llvm_sjljeh_destroy_setjmpmap
70 Function* AddSJToMap; // __llvm_sjljeh_add_setjmp_to_map
71 Function* ThrowLongJmp; // __llvm_sjljeh_throw_longjmp
72 Function* TryCatchLJ; // __llvm_sjljeh_try_catching_longjmp_exception
73 Function* IsLJException; // __llvm_sjljeh_is_longjmp_exception
74 Function* GetLJValue; // __llvm_sjljeh_get_longjmp_value
75
76 typedef std::pair<SwitchInst*, CallInst*> SwitchValuePair;
77
Chris Lattner56b85262003-10-13 00:57:16 +000078 // Keep track of those basic blocks reachable via a depth-first search of
79 // the CFG from a setjmp call. We only need to transform those "call" and
80 // "invoke" instructions that are reachable from the setjmp call site.
81 std::set<BasicBlock*> DFSBlocks;
82
Chris Lattner6f99eb52003-09-15 04:56:27 +000083 // The setjmp map is going to hold information about which setjmps
84 // were called (each setjmp gets its own number) and with which
85 // buffer it was called.
86 std::map<Function*, AllocaInst*> SJMap;
87
88 // The rethrow basic block map holds the basic block to branch to if
89 // the exception isn't handled in the current function and needs to
90 // be rethrown.
91 std::map<const Function*, BasicBlock*> RethrowBBMap;
92
93 // The preliminary basic block map holds a basic block that grabs the
94 // exception and determines if it's handled by the current function.
95 std::map<const Function*, BasicBlock*> PrelimBBMap;
96
97 // The switch/value map holds a switch inst/call inst pair. The
98 // switch inst controls which handler (if any) gets called and the
99 // value is the value returned to that handler by the call to
100 // __llvm_sjljeh_get_longjmp_value.
101 std::map<const Function*, SwitchValuePair> SwitchValMap;
102
103 // A map of which setjmps we've seen so far in a function.
104 std::map<const Function*, unsigned> SetJmpIDMap;
105
106 AllocaInst* GetSetJmpMap(Function* Func);
107 BasicBlock* GetRethrowBB(Function* Func);
108 SwitchValuePair GetSJSwitch(Function* Func, BasicBlock* Rethrow);
109
110 void TransformLongJmpCall(CallInst* Inst);
111 void TransformSetJmpCall(CallInst* Inst);
112
113 bool IsTransformableFunction(const std::string& Name);
114 public:
115 void visitCallInst(CallInst& CI);
116 void visitInvokeInst(InvokeInst& II);
117 void visitReturnInst(ReturnInst& RI);
118 void visitUnwindInst(UnwindInst& UI);
119
120 bool run(Module& M);
121 bool doInitialization(Module& M);
122 };
123
124 RegisterOpt<LowerSetJmp> X("lowersetjmp", "Lower Set Jump");
125} // end anonymous namespace
126
127// run - Run the transformation on the program. We grab the function
128// prototypes for longjmp and setjmp. If they are used in the program,
129// then we can go directly to the places they're at and transform them.
130bool LowerSetJmp::run(Module& M)
131{
132 bool Changed = false;
133
134 // These are what the functions are called.
135 Function* SetJmp = M.getNamedFunction("llvm.setjmp");
136 Function* LongJmp = M.getNamedFunction("llvm.longjmp");
137
138 // This program doesn't have longjmp and setjmp calls.
139 if ((!LongJmp || LongJmp->use_empty()) &&
140 (!SetJmp || SetJmp->use_empty())) return false;
141
142 // Initialize some values and functions we'll need to transform the
143 // setjmp/longjmp functions.
144 doInitialization(M);
145
Chris Lattner56b85262003-10-13 00:57:16 +0000146 if (SetJmp) {
Chris Lattner56b85262003-10-13 00:57:16 +0000147 for (Value::use_iterator B = SetJmp->use_begin(), E = SetJmp->use_end();
148 B != E; ++B) {
Chris Lattnerf7a60cd2003-10-13 01:02:33 +0000149 BasicBlock* BB = cast<Instruction>(*B)->getParent();
Chris Lattner612cafe2003-10-13 16:49:21 +0000150 for (df_ext_iterator<BasicBlock*> I = df_ext_begin(BB, DFSBlocks),
151 E = df_ext_end(BB, DFSBlocks); I != E; ++I)
Chris Lattner951e7322003-10-13 16:44:50 +0000152 /* empty */;
Chris Lattner56b85262003-10-13 00:57:16 +0000153 }
154
Chris Lattner6f99eb52003-09-15 04:56:27 +0000155 while (!SetJmp->use_empty()) {
156 assert(isa<CallInst>(SetJmp->use_back()) &&
157 "User of setjmp intrinsic not a call?");
158 TransformSetJmpCall(cast<CallInst>(SetJmp->use_back()));
159 Changed = true;
160 }
Chris Lattner56b85262003-10-13 00:57:16 +0000161 }
Chris Lattner6f99eb52003-09-15 04:56:27 +0000162
163 if (LongJmp)
164 while (!LongJmp->use_empty()) {
165 assert(isa<CallInst>(LongJmp->use_back()) &&
166 "User of longjmp intrinsic not a call?");
167 TransformLongJmpCall(cast<CallInst>(LongJmp->use_back()));
168 Changed = true;
169 }
170
171 // Now go through the affected functions and convert calls and invokes
172 // to new invokes...
173 for (std::map<Function*, AllocaInst*>::iterator
174 B = SJMap.begin(), E = SJMap.end(); B != E; ++B) {
175 Function* F = B->first;
176 for (Function::iterator BB = F->begin(), BE = F->end(); BB != BE; ++BB)
177 for (BasicBlock::iterator IB = BB->begin(), IE = BB->end(); IB != IE; ) {
178 visit(*IB++);
179 if (IB != BB->end() && IB->getParent() != BB)
180 break; // The next instruction got moved to a different block!
181 }
182 }
183
Chris Lattner56b85262003-10-13 00:57:16 +0000184 DFSBlocks.clear();
Chris Lattner6f99eb52003-09-15 04:56:27 +0000185 SJMap.clear();
186 RethrowBBMap.clear();
187 PrelimBBMap.clear();
188 SwitchValMap.clear();
189 SetJmpIDMap.clear();
190
191 return Changed;
192}
193
194// doInitialization - For the lower long/setjmp pass, this ensures that a
195// module contains a declaration for the intrisic functions we are going
196// to call to convert longjmp and setjmp calls.
197//
198// This function is always successful, unless it isn't.
199bool LowerSetJmp::doInitialization(Module& M)
200{
201 const Type *SBPTy = PointerType::get(Type::SByteTy);
202 const Type *SBPPTy = PointerType::get(SBPTy);
203
204 // N.B. See llvm/runtime/GCCLibraries/libexception/SJLJ-Exception.h for
205 // a description of the following library functions.
206
207 // void __llvm_sjljeh_init_setjmpmap(void**)
208 InitSJMap = M.getOrInsertFunction("__llvm_sjljeh_init_setjmpmap",
209 Type::VoidTy, SBPPTy, 0);
210 // void __llvm_sjljeh_destroy_setjmpmap(void**)
211 DestroySJMap = M.getOrInsertFunction("__llvm_sjljeh_destroy_setjmpmap",
212 Type::VoidTy, SBPPTy, 0);
213
214 // void __llvm_sjljeh_add_setjmp_to_map(void**, void*, unsigned)
215 AddSJToMap = M.getOrInsertFunction("__llvm_sjljeh_add_setjmp_to_map",
216 Type::VoidTy, SBPPTy, SBPTy,
217 Type::UIntTy, 0);
218
219 // void __llvm_sjljeh_throw_longjmp(int*, int)
220 ThrowLongJmp = M.getOrInsertFunction("__llvm_sjljeh_throw_longjmp",
221 Type::VoidTy, SBPTy, Type::IntTy, 0);
222
223 // unsigned __llvm_sjljeh_try_catching_longjmp_exception(void **)
224 TryCatchLJ =
225 M.getOrInsertFunction("__llvm_sjljeh_try_catching_longjmp_exception",
226 Type::UIntTy, SBPPTy, 0);
227
228 // bool __llvm_sjljeh_is_longjmp_exception()
229 IsLJException = M.getOrInsertFunction("__llvm_sjljeh_is_longjmp_exception",
230 Type::BoolTy, 0);
231
232 // int __llvm_sjljeh_get_longjmp_value()
233 GetLJValue = M.getOrInsertFunction("__llvm_sjljeh_get_longjmp_value",
234 Type::IntTy, 0);
235 return true;
236}
237
238// IsTransformableFunction - Return true if the function name isn't one
239// of the ones we don't want transformed. Currently, don't transform any
240// "llvm.{setjmp,longjmp}" functions and none of the setjmp/longjmp error
241// handling functions (beginning with __llvm_sjljeh_...they don't throw
242// exceptions).
243bool LowerSetJmp::IsTransformableFunction(const std::string& Name)
244{
245 std::string SJLJEh("__llvm_sjljeh");
246
Chris Lattner40b66212003-09-15 05:43:05 +0000247 if (Name.size() > SJLJEh.size())
248 return std::string(Name.begin(), Name.begin() + SJLJEh.size()) != SJLJEh;
Chris Lattner6f99eb52003-09-15 04:56:27 +0000249
250 return true;
251}
252
253// TransformLongJmpCall - Transform a longjmp call into a call to the
254// internal __llvm_sjljeh_throw_longjmp function. It then takes care of
255// throwing the exception for us.
256void LowerSetJmp::TransformLongJmpCall(CallInst* Inst)
257{
258 const Type* SBPTy = PointerType::get(Type::SByteTy);
259
260 // Create the call to "__llvm_sjljeh_throw_longjmp". This takes the
261 // same parameters as "longjmp", except that the buffer is cast to a
262 // char*. It returns "void", so it doesn't need to replace any of
263 // Inst's uses and doesn't get a name.
264 CastInst* CI = new CastInst(Inst->getOperand(1), SBPTy, "LJBuf", Inst);
265 new CallInst(ThrowLongJmp, make_vector<Value*>(CI, Inst->getOperand(2), 0),
266 "", Inst);
267
268 SwitchValuePair& SVP = SwitchValMap[Inst->getParent()->getParent()];
269
270 // If the function has a setjmp call in it (they are transformed first)
271 // we should branch to the basic block that determines if this longjmp
272 // is applicable here. Otherwise, issue an unwind.
273 if (SVP.first)
274 new BranchInst(SVP.first->getParent(), Inst);
275 else
276 new UnwindInst(Inst);
277
278 // Remove all insts after the branch/unwind inst.
279 Inst->getParent()->getInstList().erase(Inst,
280 Inst->getParent()->getInstList().end());
281
282 ++LongJmpsTransformed;
283}
284
285// GetSetJmpMap - Retrieve (create and initialize, if necessary) the
286// setjmp map. This map is going to hold information about which setjmps
287// were called (each setjmp gets its own number) and with which buffer it
288// was called. There can be only one!
289AllocaInst* LowerSetJmp::GetSetJmpMap(Function* Func)
290{
291 if (SJMap[Func]) return SJMap[Func];
292
293 // Insert the setjmp map initialization before the first instruction in
294 // the function.
Chris Lattner5dac64f2003-09-20 14:39:18 +0000295 Instruction* Inst = Func->getEntryBlock().begin();
Chris Lattner6f99eb52003-09-15 04:56:27 +0000296 assert(Inst && "Couldn't find even ONE instruction in entry block!");
297
298 // Fill in the alloca and call to initialize the SJ map.
299 const Type *SBPTy = PointerType::get(Type::SByteTy);
300 AllocaInst* Map = new AllocaInst(SBPTy, 0, "SJMap", Inst);
301 new CallInst(InitSJMap, make_vector<Value*>(Map, 0), "", Inst);
302 return SJMap[Func] = Map;
303}
304
305// GetRethrowBB - Only one rethrow basic block is needed per function.
306// If this is a longjmp exception but not handled in this block, this BB
307// performs the rethrow.
308BasicBlock* LowerSetJmp::GetRethrowBB(Function* Func)
309{
310 if (RethrowBBMap[Func]) return RethrowBBMap[Func];
311
312 // The basic block we're going to jump to if we need to rethrow the
313 // exception.
314 BasicBlock* Rethrow = new BasicBlock("RethrowExcept", Func);
315 BasicBlock::InstListType& RethrowBlkIL = Rethrow->getInstList();
316
317 // Fill in the "Rethrow" BB with a call to rethrow the exception. This
318 // is the last instruction in the BB since at this point the runtime
319 // should exit this function and go to the next function.
320 RethrowBlkIL.push_back(new UnwindInst());
321 return RethrowBBMap[Func] = Rethrow;
322}
323
324// GetSJSwitch - Return the switch statement that controls which handler
325// (if any) gets called and the value returned to that handler.
326LowerSetJmp::SwitchValuePair LowerSetJmp::GetSJSwitch(Function* Func,
327 BasicBlock* Rethrow)
328{
329 if (SwitchValMap[Func].first) return SwitchValMap[Func];
330
331 BasicBlock* LongJmpPre = new BasicBlock("LongJmpBlkPre", Func);
332 BasicBlock::InstListType& LongJmpPreIL = LongJmpPre->getInstList();
333
334 // Keep track of the preliminary basic block for some of the other
335 // transformations.
336 PrelimBBMap[Func] = LongJmpPre;
337
338 // Grab the exception.
339 CallInst* Cond = new
340 CallInst(IsLJException, std::vector<Value*>(), "IsLJExcept");
341 LongJmpPreIL.push_back(Cond);
342
343 // The "decision basic block" gets the number associated with the
344 // setjmp call returning to switch on and the value returned by
345 // longjmp.
346 BasicBlock* DecisionBB = new BasicBlock("LJDecisionBB", Func);
347 BasicBlock::InstListType& DecisionBBIL = DecisionBB->getInstList();
348
349 LongJmpPreIL.push_back(new BranchInst(DecisionBB, Rethrow, Cond));
350
351 // Fill in the "decision" basic block.
352 CallInst* LJVal = new CallInst(GetLJValue, std::vector<Value*>(), "LJVal");
353 DecisionBBIL.push_back(LJVal);
354 CallInst* SJNum = new
355 CallInst(TryCatchLJ, make_vector<Value*>(GetSetJmpMap(Func), 0), "SJNum");
356 DecisionBBIL.push_back(SJNum);
357
358 SwitchInst* SI = new SwitchInst(SJNum, Rethrow);
359 DecisionBBIL.push_back(SI);
360 return SwitchValMap[Func] = SwitchValuePair(SI, LJVal);
361}
362
363// TransformSetJmpCall - The setjmp call is a bit trickier to transform.
364// We're going to convert all setjmp calls to nops. Then all "call" and
365// "invoke" instructions in the function are converted to "invoke" where
366// the "except" branch is used when returning from a longjmp call.
367void LowerSetJmp::TransformSetJmpCall(CallInst* Inst)
368{
369 BasicBlock* ABlock = Inst->getParent();
370 Function* Func = ABlock->getParent();
371
372 // Add this setjmp to the setjmp map.
373 const Type* SBPTy = PointerType::get(Type::SByteTy);
374 CastInst* BufPtr = new CastInst(Inst->getOperand(1), SBPTy, "SBJmpBuf", Inst);
375 new CallInst(AddSJToMap,
376 make_vector<Value*>(GetSetJmpMap(Func), BufPtr,
377 ConstantUInt::get(Type::UIntTy,
378 SetJmpIDMap[Func]++), 0),
379 "", Inst);
380
Chris Lattnerb0a4b492003-11-06 19:18:49 +0000381 // We are guaranteed that there are no values live across basic blocks
382 // (because we are "not in SSA form" yet), but there can still be values live
383 // in basic blocks. Because of this, splitting the setjmp block can cause
384 // values above the setjmp to not dominate uses which are after the setjmp
385 // call. For all of these occasions, we must spill the value to the stack.
386 //
387 std::set<Instruction*> InstrsAfterCall;
388
389 // The call is probably very close to the end of the basic block, for the
390 // common usage pattern of: 'if (setjmp(...))', so keep track of the
391 // instructions after the call.
392 for (BasicBlock::iterator I = ++BasicBlock::iterator(Inst), E = ABlock->end();
393 I != E; ++I)
394 InstrsAfterCall.insert(I);
395
396 for (BasicBlock::iterator II = ABlock->begin();
397 II != BasicBlock::iterator(Inst); ++II)
398 // Loop over all of the uses of instruction. If any of them are after the
399 // call, "spill" the value to the stack.
400 for (Value::use_iterator UI = II->use_begin(), E = II->use_end();
401 UI != E; ++UI)
402 if (cast<Instruction>(*UI)->getParent() != ABlock ||
403 InstrsAfterCall.count(cast<Instruction>(*UI))) {
404 DemoteRegToStack(*II);
405 break;
406 }
407 InstrsAfterCall.clear();
408
Chris Lattner6f99eb52003-09-15 04:56:27 +0000409 // Change the setjmp call into a branch statement. We'll remove the
410 // setjmp call in a little bit. No worries.
411 BasicBlock* SetJmpContBlock = ABlock->splitBasicBlock(Inst);
412 assert(SetJmpContBlock && "Couldn't split setjmp BB!!");
413
414 SetJmpContBlock->setName("SetJmpContBlock");
415
416 // Reposition the split BB in the BB list to make things tidier.
417 Func->getBasicBlockList().remove(SetJmpContBlock);
418 Func->getBasicBlockList().insert(++Function::iterator(ABlock),
419 SetJmpContBlock);
420
421 // This PHI node will be in the new block created from the
422 // splitBasicBlock call.
423 PHINode* PHI = new PHINode(Type::IntTy, "SetJmpReturn", Inst);
424
425 // Coming from a call to setjmp, the return is 0.
426 PHI->addIncoming(ConstantInt::getNullValue(Type::IntTy), ABlock);
427
428 // Add the case for this setjmp's number...
429 SwitchValuePair SVP = GetSJSwitch(Func, GetRethrowBB(Func));
430 SVP.first->addCase(ConstantUInt::get(Type::UIntTy, SetJmpIDMap[Func] - 1),
431 SetJmpContBlock);
432
433 // Value coming from the handling of the exception.
434 PHI->addIncoming(SVP.second, SVP.second->getParent());
435
436 // Replace all uses of this instruction with the PHI node created by
437 // the eradication of setjmp.
438 Inst->replaceAllUsesWith(PHI);
439 Inst->getParent()->getInstList().erase(Inst);
440
441 ++SetJmpsTransformed;
442}
443
444// visitCallInst - This converts all LLVM call instructions into invoke
445// instructions. The except part of the invoke goes to the "LongJmpBlkPre"
446// that grabs the exception and proceeds to determine if it's a longjmp
447// exception or not.
448void LowerSetJmp::visitCallInst(CallInst& CI)
449{
450 if (CI.getCalledFunction())
451 if (!IsTransformableFunction(CI.getCalledFunction()->getName()) ||
452 CI.getCalledFunction()->isIntrinsic()) return;
453
454 BasicBlock* OldBB = CI.getParent();
Chris Lattner56b85262003-10-13 00:57:16 +0000455
456 // If not reachable from a setjmp call, don't transform.
457 if (!DFSBlocks.count(OldBB)) return;
458
Chris Lattner6f99eb52003-09-15 04:56:27 +0000459 BasicBlock* NewBB = OldBB->splitBasicBlock(CI);
460 assert(NewBB && "Couldn't split BB of \"call\" instruction!!");
461 NewBB->setName("Call2Invoke");
462
463 // Reposition the split BB in the BB list to make things tidier.
Chris Lattnerf7a60cd2003-10-13 01:02:33 +0000464 Function* Func = OldBB->getParent();
Chris Lattner6f99eb52003-09-15 04:56:27 +0000465 Func->getBasicBlockList().remove(NewBB);
466 Func->getBasicBlockList().insert(++Function::iterator(OldBB), NewBB);
467
468 // Construct the new "invoke" instruction.
469 TerminatorInst* Term = OldBB->getTerminator();
470 std::vector<Value*> Params(CI.op_begin() + 1, CI.op_end());
471 InvokeInst* II = new
472 InvokeInst(CI.getCalledValue(), NewBB, PrelimBBMap[Func],
473 Params, CI.getName(), Term);
474
475 // Replace the old call inst with the invoke inst and remove the call.
476 CI.replaceAllUsesWith(II);
477 CI.getParent()->getInstList().erase(&CI);
478
479 // The old terminator is useless now that we have the invoke inst.
480 Term->getParent()->getInstList().erase(Term);
Chris Lattner3e5ff252003-10-28 23:14:59 +0000481 ++CallsTransformed;
Chris Lattner6f99eb52003-09-15 04:56:27 +0000482}
483
484// visitInvokeInst - Converting the "invoke" instruction is fairly
485// straight-forward. The old exception part is replaced by a query asking
486// if this is a longjmp exception. If it is, then it goes to the longjmp
487// exception blocks. Otherwise, control is passed the old exception.
488void LowerSetJmp::visitInvokeInst(InvokeInst& II)
489{
490 if (II.getCalledFunction())
491 if (!IsTransformableFunction(II.getCalledFunction()->getName()) ||
492 II.getCalledFunction()->isIntrinsic()) return;
493
Chris Lattner56b85262003-10-13 00:57:16 +0000494 BasicBlock* BB = II.getParent();
Chris Lattner56b85262003-10-13 00:57:16 +0000495
496 // If not reachable from a setjmp call, don't transform.
497 if (!DFSBlocks.count(BB)) return;
Chris Lattner6f99eb52003-09-15 04:56:27 +0000498
499 BasicBlock* NormalBB = II.getNormalDest();
500 BasicBlock* ExceptBB = II.getExceptionalDest();
501
Chris Lattnerf7a60cd2003-10-13 01:02:33 +0000502 Function* Func = BB->getParent();
Chris Lattner6f99eb52003-09-15 04:56:27 +0000503 BasicBlock* NewExceptBB = new BasicBlock("InvokeExcept", Func);
504 BasicBlock::InstListType& InstList = NewExceptBB->getInstList();
505
506 // If this is a longjmp exception, then branch to the preliminary BB of
507 // the longjmp exception handling. Otherwise, go to the old exception.
508 CallInst* IsLJExcept = new
509 CallInst(IsLJException, std::vector<Value*>(), "IsLJExcept");
510 InstList.push_back(IsLJExcept);
511
512 BranchInst* BR = new BranchInst(PrelimBBMap[Func], ExceptBB, IsLJExcept);
513 InstList.push_back(BR);
514
515 II.setExceptionalDest(NewExceptBB);
Chris Lattner3e5ff252003-10-28 23:14:59 +0000516 ++InvokesTransformed;
Chris Lattner6f99eb52003-09-15 04:56:27 +0000517}
518
519// visitReturnInst - We want to destroy the setjmp map upon exit from the
520// function.
521void LowerSetJmp::visitReturnInst(ReturnInst& RI)
522{
523 Function* Func = RI.getParent()->getParent();
524 new CallInst(DestroySJMap, make_vector<Value*>(GetSetJmpMap(Func), 0),
525 "", &RI);
526}
527
528// visitUnwindInst - We want to destroy the setjmp map upon exit from the
529// function.
530void LowerSetJmp::visitUnwindInst(UnwindInst& UI)
531{
532 Function* Func = UI.getParent()->getParent();
533 new CallInst(DestroySJMap, make_vector<Value*>(GetSetJmpMap(Func), 0),
534 "", &UI);
535}
536
537Pass* createLowerSetJmpPass()
538{
539 return new LowerSetJmp();
540}