OSDN Git Service

If a selector has a call to ".llvm.eh.catch.all.value" that we haven't
[android-x86/external-llvm.git] / lib / CodeGen / DwarfEHPrepare.cpp
1 //===-- DwarfEHPrepare - Prepare exception handling for code generation ---===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass mulches exception handling code into a form adapted to code
11 // generation. Required if using dwarf exception handling.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "dwarfehprepare"
16 #include "llvm/Function.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/IntrinsicInst.h"
19 #include "llvm/Module.h"
20 #include "llvm/Pass.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Analysis/Dominators.h"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/MC/MCAsmInfo.h"
25 #include "llvm/Target/TargetLowering.h"
26 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
27 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
28 using namespace llvm;
29
30 STATISTIC(NumLandingPadsSplit,     "Number of landing pads split");
31 STATISTIC(NumUnwindsLowered,       "Number of unwind instructions lowered");
32 STATISTIC(NumExceptionValuesMoved, "Number of eh.exception calls moved");
33 STATISTIC(NumStackTempsIntroduced, "Number of stack temporaries introduced");
34
35 namespace {
36   class DwarfEHPrepare : public FunctionPass {
37     const TargetLowering *TLI;
38     bool CompileFast;
39
40     // The eh.exception intrinsic.
41     Function *ExceptionValueIntrinsic;
42
43     // The eh.selector intrinsic.
44     Function *SelectorIntrinsic;
45
46     // _Unwind_Resume_or_Rethrow call.
47     Constant *URoR;
48
49     // The EH language-specific catch-all type.
50     GlobalVariable *EHCatchAllValue;
51
52     // _Unwind_Resume or the target equivalent.
53     Constant *RewindFunction;
54
55     // Dominator info is used when turning stack temporaries into registers.
56     DominatorTree *DT;
57     DominanceFrontier *DF;
58
59     // The function we are running on.
60     Function *F;
61
62     // The landing pads for this function.
63     typedef SmallPtrSet<BasicBlock*, 8> BBSet;
64     BBSet LandingPads;
65
66     // Stack temporary used to hold eh.exception values.
67     AllocaInst *ExceptionValueVar;
68
69     bool NormalizeLandingPads();
70     bool LowerUnwinds();
71     bool MoveExceptionValueCalls();
72     bool FinishStackTemporaries();
73     bool PromoteStackTemporaries();
74
75     Instruction *CreateExceptionValueCall(BasicBlock *BB);
76     Instruction *CreateValueLoad(BasicBlock *BB);
77
78     /// CreateReadOfExceptionValue - Return the result of the eh.exception
79     /// intrinsic by calling the intrinsic if in a landing pad, or loading it
80     /// from the exception value variable otherwise.
81     Instruction *CreateReadOfExceptionValue(BasicBlock *BB) {
82       return LandingPads.count(BB) ?
83         CreateExceptionValueCall(BB) : CreateValueLoad(BB);
84     }
85
86     /// CleanupSelectors - Any remaining eh.selector intrinsic calls which still
87     /// use the ".llvm.eh.catch.all.value" call need to convert to using it's
88     /// initializer instead.
89     void CleanupSelectors();
90
91     /// HandleURoRInvokes - Handle invokes of "_Unwind_Resume_or_Rethrow"
92     /// calls. The "unwind" part of these invokes jump to a landing pad within
93     /// the current function. This is a candidate to merge the selector
94     /// associated with the URoR invoke with the one from the URoR's landing
95     /// pad.
96     bool HandleURoRInvokes();
97
98     /// FindSelectorAndURoR - Find the eh.selector call and URoR call associated
99     /// with the eh.exception call. This recursively looks past instructions
100     /// which don't change the EH pointer value, like casts or PHI nodes.
101     bool FindSelectorAndURoR(Instruction *Inst, bool &URoRInvoke,
102                              SmallPtrSet<IntrinsicInst*, 8> &SelCalls);
103       
104     /// DoMem2RegPromotion - Take an alloca call and promote it from memory to a
105     /// register.
106     bool DoMem2RegPromotion(Value *V) {
107       AllocaInst *AI = dyn_cast<AllocaInst>(V);
108       if (!AI || !isAllocaPromotable(AI)) return false;
109
110       // Turn the alloca into a register.
111       std::vector<AllocaInst*> Allocas(1, AI);
112       PromoteMemToReg(Allocas, *DT, *DF);
113       return true;
114     }
115
116     /// PromoteStoreInst - Perform Mem2Reg on a StoreInst.
117     bool PromoteStoreInst(StoreInst *SI) {
118       if (!SI || !DT || !DF) return false;
119       if (DoMem2RegPromotion(SI->getOperand(1)))
120         return true;
121       return false;
122     }
123
124     /// PromoteEHPtrStore - Promote the storing of an EH pointer into a
125     /// register. This should get rid of the store and subsequent loads.
126     bool PromoteEHPtrStore(IntrinsicInst *II) {
127       if (!DT || !DF) return false;
128
129       bool Changed = false;
130       StoreInst *SI;
131
132       while (1) {
133         SI = 0;
134         for (Value::use_iterator
135                I = II->use_begin(), E = II->use_end(); I != E; ++I) {
136           SI = dyn_cast<StoreInst>(I);
137           if (SI) break;
138         }
139
140         if (!PromoteStoreInst(SI))
141           break;
142
143         Changed = true;
144       }
145
146       return false;
147     }
148
149   public:
150     static char ID; // Pass identification, replacement for typeid.
151     DwarfEHPrepare(const TargetLowering *tli, bool fast) :
152       FunctionPass(&ID), TLI(tli), CompileFast(fast),
153       ExceptionValueIntrinsic(0), SelectorIntrinsic(0),
154       URoR(0), EHCatchAllValue(0), RewindFunction(0) {}
155
156     virtual bool runOnFunction(Function &Fn);
157
158     // getAnalysisUsage - We need dominance frontiers for memory promotion.
159     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
160       if (!CompileFast)
161         AU.addRequired<DominatorTree>();
162       AU.addPreserved<DominatorTree>();
163       if (!CompileFast)
164         AU.addRequired<DominanceFrontier>();
165       AU.addPreserved<DominanceFrontier>();
166     }
167
168     const char *getPassName() const {
169       return "Exception handling preparation";
170     }
171
172   };
173 } // end anonymous namespace
174
175 char DwarfEHPrepare::ID = 0;
176
177 FunctionPass *llvm::createDwarfEHPass(const TargetLowering *tli, bool fast) {
178   return new DwarfEHPrepare(tli, fast);
179 }
180
181 /// FindSelectorAndURoR - Find the eh.selector call associated with the
182 /// eh.exception call. And indicate if there is a URoR "invoke" associated with
183 /// the eh.exception call. This recursively looks past instructions which don't
184 /// change the EH pointer value, like casts or PHI nodes.
185 bool
186 DwarfEHPrepare::FindSelectorAndURoR(Instruction *Inst, bool &URoRInvoke,
187                                     SmallPtrSet<IntrinsicInst*, 8> &SelCalls) {
188   SmallPtrSet<PHINode*, 32> SeenPHIs;
189   bool Changed = false;
190
191  restart:
192   for (Value::use_iterator
193          I = Inst->use_begin(), E = Inst->use_end(); I != E; ++I) {
194     Instruction *II = dyn_cast<Instruction>(I);
195     if (!II || II->getParent()->getParent() != F) continue;
196     
197     if (IntrinsicInst *Sel = dyn_cast<IntrinsicInst>(II)) {
198       if (Sel->getIntrinsicID() == Intrinsic::eh_selector)
199         SelCalls.insert(Sel);
200     } else if (InvokeInst *Invoke = dyn_cast<InvokeInst>(II)) {
201       if (Invoke->getCalledFunction() == URoR)
202         URoRInvoke = true;
203     } else if (CastInst *CI = dyn_cast<CastInst>(II)) {
204       Changed |= FindSelectorAndURoR(CI, URoRInvoke, SelCalls);
205     } else if (StoreInst *SI = dyn_cast<StoreInst>(II)) {
206       if (!PromoteStoreInst(SI)) continue;
207       Changed = true;
208       SeenPHIs.clear();
209       goto restart;             // Uses may have changed, restart loop.
210     } else if (PHINode *PN = dyn_cast<PHINode>(II)) {
211       if (SeenPHIs.insert(PN))
212         // Don't process a PHI node more than once.
213         Changed |= FindSelectorAndURoR(PN, URoRInvoke, SelCalls);
214     }
215   }
216
217   return Changed;
218 }
219
220 /// CleanupSelectors - Any remaining eh.selector intrinsic calls which still use
221 /// the ".llvm.eh.catch.all.value" call need to convert to using it's
222 /// initializer instead.
223 void DwarfEHPrepare::CleanupSelectors() {
224   for (Value::use_iterator
225          I = SelectorIntrinsic->use_begin(),
226          E = SelectorIntrinsic->use_end(); I != E; ++I) {
227     IntrinsicInst *Sel = dyn_cast<IntrinsicInst>(I);
228     if (!Sel || Sel->getParent()->getParent() != F) continue;
229
230     // Index of the ".llvm.eh.catch.all.value" variable.
231     unsigned OpIdx = Sel->getNumOperands() - 1;
232     GlobalVariable *GV = dyn_cast<GlobalVariable>(Sel->getOperand(OpIdx));
233     if (GV != EHCatchAllValue) continue;
234     Sel->setOperand(OpIdx, EHCatchAllValue->getInitializer());
235   }
236 }
237
238 /// HandleURoRInvokes - Handle invokes of "_Unwind_Resume_or_Rethrow" calls. The
239 /// "unwind" part of these invokes jump to a landing pad within the current
240 /// function. This is a candidate to merge the selector associated with the URoR
241 /// invoke with the one from the URoR's landing pad.
242 bool DwarfEHPrepare::HandleURoRInvokes() {
243   if (!EHCatchAllValue) {
244     EHCatchAllValue =
245       F->getParent()->getNamedGlobal(".llvm.eh.catch.all.value");
246     if (!EHCatchAllValue) return false;
247   }
248
249   if (!SelectorIntrinsic) {
250     SelectorIntrinsic =
251       Intrinsic::getDeclaration(F->getParent(), Intrinsic::eh_selector);
252     if (!SelectorIntrinsic) return false;
253   }
254
255   if (!URoR) {
256     URoR = F->getParent()->getFunction("_Unwind_Resume_or_Rethrow");
257     if (!URoR) {
258       CleanupSelectors();
259       return false;
260     }
261   }
262
263   if (!ExceptionValueIntrinsic) {
264     ExceptionValueIntrinsic =
265       Intrinsic::getDeclaration(F->getParent(), Intrinsic::eh_exception);
266     if (!ExceptionValueIntrinsic) {
267       CleanupSelectors();
268       return false;
269     }
270   }
271
272   bool Changed = false;
273   SmallPtrSet<IntrinsicInst*, 32> SelsToConvert;
274
275   for (Value::use_iterator
276          I = ExceptionValueIntrinsic->use_begin(),
277          E = ExceptionValueIntrinsic->use_end(); I != E; ++I) {
278     IntrinsicInst *EHPtr = dyn_cast<IntrinsicInst>(I);
279     if (!EHPtr || EHPtr->getParent()->getParent() != F) continue;
280
281     Changed |= PromoteEHPtrStore(EHPtr);
282
283     bool URoRInvoke = false;
284     SmallPtrSet<IntrinsicInst*, 8> SelCalls;
285     Changed |= FindSelectorAndURoR(EHPtr, URoRInvoke, SelCalls);
286
287     if (URoRInvoke) {
288       // This EH pointer is being used by an invoke of an URoR instruction and
289       // an eh.selector intrinsic call. If the eh.selector is a 'clean-up', we
290       // need to convert it to a 'catch-all'.
291       for (SmallPtrSet<IntrinsicInst*, 8>::iterator
292              SI = SelCalls.begin(), SE = SelCalls.end(); SI != SE; ++SI) {
293         IntrinsicInst *II = *SI;
294         unsigned NumOps = II->getNumOperands();
295
296         if (NumOps <= 4) {
297           bool IsCleanUp = (NumOps == 3);
298
299           if (!IsCleanUp)
300             if (ConstantInt *CI = dyn_cast<ConstantInt>(II->getOperand(3)))
301               IsCleanUp = (CI->getZExtValue() == 0);
302
303           if (IsCleanUp)
304             SelsToConvert.insert(II);
305         }
306       }
307     }
308   }
309
310   if (!SelsToConvert.empty()) {
311     // Convert all clean-up eh.selectors, which are associated with "invokes" of
312     // URoR calls, into catch-all eh.selectors.
313     Changed = true;
314
315     for (SmallPtrSet<IntrinsicInst*, 8>::iterator
316            SI = SelsToConvert.begin(), SE = SelsToConvert.end();
317          SI != SE; ++SI) {
318       IntrinsicInst *II = *SI;
319       SmallVector<Value*, 8> Args;
320
321       // Use the exception object pointer and the personality function
322       // from the original selector.
323       Args.push_back(II->getOperand(1)); // Exception object pointer.
324       Args.push_back(II->getOperand(2)); // Personality function.
325       Args.push_back(EHCatchAllValue->getInitializer()); // Catch-all indicator.
326
327       CallInst *NewSelector =
328         CallInst::Create(SelectorIntrinsic, Args.begin(), Args.end(),
329                          "eh.sel.catch.all", II);
330
331       NewSelector->setTailCall(II->isTailCall());
332       NewSelector->setAttributes(II->getAttributes());
333       NewSelector->setCallingConv(II->getCallingConv());
334
335       II->replaceAllUsesWith(NewSelector);
336       II->eraseFromParent();
337     }
338   }
339
340   CleanupSelectors();
341   return Changed;
342 }
343
344 /// NormalizeLandingPads - Normalize and discover landing pads, noting them
345 /// in the LandingPads set.  A landing pad is normal if the only CFG edges
346 /// that end at it are unwind edges from invoke instructions. If we inlined
347 /// through an invoke we could have a normal branch from the previous
348 /// unwind block through to the landing pad for the original invoke.
349 /// Abnormal landing pads are fixed up by redirecting all unwind edges to
350 /// a new basic block which falls through to the original.
351 bool DwarfEHPrepare::NormalizeLandingPads() {
352   bool Changed = false;
353
354   const MCAsmInfo *MAI = TLI->getTargetMachine().getMCAsmInfo();
355   bool usingSjLjEH = MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
356
357   for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
358     TerminatorInst *TI = I->getTerminator();
359     if (!isa<InvokeInst>(TI))
360       continue;
361     BasicBlock *LPad = TI->getSuccessor(1);
362     // Skip landing pads that have already been normalized.
363     if (LandingPads.count(LPad))
364       continue;
365
366     // Check that only invoke unwind edges end at the landing pad.
367     bool OnlyUnwoundTo = true;
368     bool SwitchOK = usingSjLjEH;
369     for (pred_iterator PI = pred_begin(LPad), PE = pred_end(LPad);
370          PI != PE; ++PI) {
371       TerminatorInst *PT = (*PI)->getTerminator();
372       // The SjLj dispatch block uses a switch instruction. This is effectively
373       // an unwind edge, so we can disregard it here. There will only ever
374       // be one dispatch, however, so if there are multiple switches, one
375       // of them truly is a normal edge, not an unwind edge.
376       if (SwitchOK && isa<SwitchInst>(PT)) {
377         SwitchOK = false;
378         continue;
379       }
380       if (!isa<InvokeInst>(PT) || LPad == PT->getSuccessor(0)) {
381         OnlyUnwoundTo = false;
382         break;
383       }
384     }
385
386     if (OnlyUnwoundTo) {
387       // Only unwind edges lead to the landing pad.  Remember the landing pad.
388       LandingPads.insert(LPad);
389       continue;
390     }
391
392     // At least one normal edge ends at the landing pad.  Redirect the unwind
393     // edges to a new basic block which falls through into this one.
394
395     // Create the new basic block.
396     BasicBlock *NewBB = BasicBlock::Create(F->getContext(),
397                                            LPad->getName() + "_unwind_edge");
398
399     // Insert it into the function right before the original landing pad.
400     LPad->getParent()->getBasicBlockList().insert(LPad, NewBB);
401
402     // Redirect unwind edges from the original landing pad to NewBB.
403     for (pred_iterator PI = pred_begin(LPad), PE = pred_end(LPad); PI != PE; ) {
404       TerminatorInst *PT = (*PI++)->getTerminator();
405       if (isa<InvokeInst>(PT) && PT->getSuccessor(1) == LPad)
406         // Unwind to the new block.
407         PT->setSuccessor(1, NewBB);
408     }
409
410     // If there are any PHI nodes in LPad, we need to update them so that they
411     // merge incoming values from NewBB instead.
412     for (BasicBlock::iterator II = LPad->begin(); isa<PHINode>(II); ++II) {
413       PHINode *PN = cast<PHINode>(II);
414       pred_iterator PB = pred_begin(NewBB), PE = pred_end(NewBB);
415
416       // Check to see if all of the values coming in via unwind edges are the
417       // same.  If so, we don't need to create a new PHI node.
418       Value *InVal = PN->getIncomingValueForBlock(*PB);
419       for (pred_iterator PI = PB; PI != PE; ++PI) {
420         if (PI != PB && InVal != PN->getIncomingValueForBlock(*PI)) {
421           InVal = 0;
422           break;
423         }
424       }
425
426       if (InVal == 0) {
427         // Different unwind edges have different values.  Create a new PHI node
428         // in NewBB.
429         PHINode *NewPN = PHINode::Create(PN->getType(), PN->getName()+".unwind",
430                                          NewBB);
431         // Add an entry for each unwind edge, using the value from the old PHI.
432         for (pred_iterator PI = PB; PI != PE; ++PI)
433           NewPN->addIncoming(PN->getIncomingValueForBlock(*PI), *PI);
434
435         // Now use this new PHI as the common incoming value for NewBB in PN.
436         InVal = NewPN;
437       }
438
439       // Revector exactly one entry in the PHI node to come from NewBB
440       // and delete all other entries that come from unwind edges.  If
441       // there are both normal and unwind edges from the same predecessor,
442       // this leaves an entry for the normal edge.
443       for (pred_iterator PI = PB; PI != PE; ++PI)
444         PN->removeIncomingValue(*PI);
445       PN->addIncoming(InVal, NewBB);
446     }
447
448     // Add a fallthrough from NewBB to the original landing pad.
449     BranchInst::Create(LPad, NewBB);
450
451     // Now update DominatorTree and DominanceFrontier analysis information.
452     if (DT)
453       DT->splitBlock(NewBB);
454     if (DF)
455       DF->splitBlock(NewBB);
456
457     // Remember the newly constructed landing pad.  The original landing pad
458     // LPad is no longer a landing pad now that all unwind edges have been
459     // revectored to NewBB.
460     LandingPads.insert(NewBB);
461     ++NumLandingPadsSplit;
462     Changed = true;
463   }
464
465   return Changed;
466 }
467
468 /// LowerUnwinds - Turn unwind instructions into calls to _Unwind_Resume,
469 /// rethrowing any previously caught exception.  This will crash horribly
470 /// at runtime if there is no such exception: using unwind to throw a new
471 /// exception is currently not supported.
472 bool DwarfEHPrepare::LowerUnwinds() {
473   SmallVector<TerminatorInst*, 16> UnwindInsts;
474
475   for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
476     TerminatorInst *TI = I->getTerminator();
477     if (isa<UnwindInst>(TI))
478       UnwindInsts.push_back(TI);
479   }
480
481   if (UnwindInsts.empty()) return false;
482
483   // Find the rewind function if we didn't already.
484   if (!RewindFunction) {
485     LLVMContext &Ctx = UnwindInsts[0]->getContext();
486     std::vector<const Type*>
487       Params(1, Type::getInt8PtrTy(Ctx));
488     FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
489                                           Params, false);
490     const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME);
491     RewindFunction = F->getParent()->getOrInsertFunction(RewindName, FTy);
492   }
493
494   bool Changed = false;
495
496   for (SmallVectorImpl<TerminatorInst*>::iterator
497          I = UnwindInsts.begin(), E = UnwindInsts.end(); I != E; ++I) {
498     TerminatorInst *TI = *I;
499
500     // Replace the unwind instruction with a call to _Unwind_Resume (or the
501     // appropriate target equivalent) followed by an UnreachableInst.
502
503     // Create the call...
504     CallInst *CI = CallInst::Create(RewindFunction,
505                                     CreateReadOfExceptionValue(TI->getParent()),
506                                     "", TI);
507     CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
508     // ...followed by an UnreachableInst.
509     new UnreachableInst(TI->getContext(), TI);
510
511     // Nuke the unwind instruction.
512     TI->eraseFromParent();
513     ++NumUnwindsLowered;
514     Changed = true;
515   }
516
517   return Changed;
518 }
519
520 /// MoveExceptionValueCalls - Ensure that eh.exception is only ever called from
521 /// landing pads by replacing calls outside of landing pads with loads from a
522 /// stack temporary.  Move eh.exception calls inside landing pads to the start
523 /// of the landing pad (optional, but may make things simpler for later passes).
524 bool DwarfEHPrepare::MoveExceptionValueCalls() {
525   // If the eh.exception intrinsic is not declared in the module then there is
526   // nothing to do.  Speed up compilation by checking for this common case.
527   if (!ExceptionValueIntrinsic &&
528       !F->getParent()->getFunction(Intrinsic::getName(Intrinsic::eh_exception)))
529     return false;
530
531   bool Changed = false;
532
533   for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
534     for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;)
535       if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++))
536         if (CI->getIntrinsicID() == Intrinsic::eh_exception) {
537           if (!CI->use_empty()) {
538             Value *ExceptionValue = CreateReadOfExceptionValue(BB);
539             if (CI == ExceptionValue) {
540               // The call was at the start of a landing pad - leave it alone.
541               assert(LandingPads.count(BB) &&
542                      "Created eh.exception call outside landing pad!");
543               continue;
544             }
545             CI->replaceAllUsesWith(ExceptionValue);
546           }
547           CI->eraseFromParent();
548           ++NumExceptionValuesMoved;
549           Changed = true;
550         }
551   }
552
553   return Changed;
554 }
555
556 /// FinishStackTemporaries - If we introduced a stack variable to hold the
557 /// exception value then initialize it in each landing pad.
558 bool DwarfEHPrepare::FinishStackTemporaries() {
559   if (!ExceptionValueVar)
560     // Nothing to do.
561     return false;
562
563   bool Changed = false;
564
565   // Make sure that there is a store of the exception value at the start of
566   // each landing pad.
567   for (BBSet::iterator LI = LandingPads.begin(), LE = LandingPads.end();
568        LI != LE; ++LI) {
569     Instruction *ExceptionValue = CreateReadOfExceptionValue(*LI);
570     Instruction *Store = new StoreInst(ExceptionValue, ExceptionValueVar);
571     Store->insertAfter(ExceptionValue);
572     Changed = true;
573   }
574
575   return Changed;
576 }
577
578 /// PromoteStackTemporaries - Turn any stack temporaries we introduced into
579 /// registers if possible.
580 bool DwarfEHPrepare::PromoteStackTemporaries() {
581   if (ExceptionValueVar && DT && DF && isAllocaPromotable(ExceptionValueVar)) {
582     // Turn the exception temporary into registers and phi nodes if possible.
583     std::vector<AllocaInst*> Allocas(1, ExceptionValueVar);
584     PromoteMemToReg(Allocas, *DT, *DF);
585     return true;
586   }
587   return false;
588 }
589
590 /// CreateExceptionValueCall - Insert a call to the eh.exception intrinsic at
591 /// the start of the basic block (unless there already is one, in which case
592 /// the existing call is returned).
593 Instruction *DwarfEHPrepare::CreateExceptionValueCall(BasicBlock *BB) {
594   Instruction *Start = BB->getFirstNonPHI();
595   // Is this a call to eh.exception?
596   if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Start))
597     if (CI->getIntrinsicID() == Intrinsic::eh_exception)
598       // Reuse the existing call.
599       return Start;
600
601   // Find the eh.exception intrinsic if we didn't already.
602   if (!ExceptionValueIntrinsic)
603     ExceptionValueIntrinsic = Intrinsic::getDeclaration(F->getParent(),
604                                                        Intrinsic::eh_exception);
605
606   // Create the call.
607   return CallInst::Create(ExceptionValueIntrinsic, "eh.value.call", Start);
608 }
609
610 /// CreateValueLoad - Insert a load of the exception value stack variable
611 /// (creating it if necessary) at the start of the basic block (unless
612 /// there already is a load, in which case the existing load is returned).
613 Instruction *DwarfEHPrepare::CreateValueLoad(BasicBlock *BB) {
614   Instruction *Start = BB->getFirstNonPHI();
615   // Is this a load of the exception temporary?
616   if (ExceptionValueVar)
617     if (LoadInst* LI = dyn_cast<LoadInst>(Start))
618       if (LI->getPointerOperand() == ExceptionValueVar)
619         // Reuse the existing load.
620         return Start;
621
622   // Create the temporary if we didn't already.
623   if (!ExceptionValueVar) {
624     ExceptionValueVar = new AllocaInst(PointerType::getUnqual(
625            Type::getInt8Ty(BB->getContext())), "eh.value", F->begin()->begin());
626     ++NumStackTempsIntroduced;
627   }
628
629   // Load the value.
630   return new LoadInst(ExceptionValueVar, "eh.value.load", Start);
631 }
632
633 bool DwarfEHPrepare::runOnFunction(Function &Fn) {
634   bool Changed = false;
635
636   // Initialize internal state.
637   DT = getAnalysisIfAvailable<DominatorTree>();
638   DF = getAnalysisIfAvailable<DominanceFrontier>();
639   ExceptionValueVar = 0;
640   F = &Fn;
641
642   // Ensure that only unwind edges end at landing pads (a landing pad is a
643   // basic block where an invoke unwind edge ends).
644   Changed |= NormalizeLandingPads();
645
646   // Turn unwind instructions into libcalls.
647   Changed |= LowerUnwinds();
648
649   // TODO: Move eh.selector calls to landing pads and combine them.
650
651   // Move eh.exception calls to landing pads.
652   Changed |= MoveExceptionValueCalls();
653
654   // Initialize any stack temporaries we introduced.
655   Changed |= FinishStackTemporaries();
656
657   // Turn any stack temporaries into registers if possible.
658   if (!CompileFast)
659     Changed |= PromoteStackTemporaries();
660
661   Changed |= HandleURoRInvokes();
662
663   LandingPads.clear();
664
665   return Changed;
666 }