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