1 //===-- DwarfEHPrepare - Prepare exception handling for code generation ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass mulches exception handling code into a form adapted to code
11 // generation. Required if using dwarf exception handling.
13 //===----------------------------------------------------------------------===//
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"
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");
36 class DwarfEHPrepare
: public FunctionPass
{
37 const TargetMachine
*TM
;
38 const TargetLowering
*TLI
;
40 // The eh.exception intrinsic.
41 Function
*ExceptionValueIntrinsic
;
43 // The eh.selector intrinsic.
44 Function
*SelectorIntrinsic
;
46 // _Unwind_Resume_or_Rethrow or _Unwind_SjLj_Resume call.
49 // The EH language-specific catch-all type.
50 GlobalVariable
*EHCatchAllValue
;
52 // _Unwind_Resume or the target equivalent.
53 Constant
*RewindFunction
;
55 // We both use and preserve dominator info.
58 // The function we are running on.
61 // The landing pads for this function.
62 typedef SmallPtrSet
<BasicBlock
*, 8> BBSet
;
65 bool NormalizeLandingPads();
67 bool MoveExceptionValueCalls();
69 Instruction
*CreateExceptionValueCall(BasicBlock
*BB
);
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
);
76 bool HasCatchAllInSelector(IntrinsicInst
*);
78 /// FindAllCleanupSelectors - Find all eh.selector calls that are clean-ups.
79 void FindAllCleanupSelectors(SmallPtrSet
<IntrinsicInst
*, 32> &Sels
,
80 SmallPtrSet
<IntrinsicInst
*, 32> &CatchAllSels
);
82 /// FindAllURoRInvokes - Find all URoR invokes in the function.
83 void FindAllURoRInvokes(SmallPtrSet
<InvokeInst
*, 32> &URoRInvokes
);
85 /// HandleURoRInvokes - Handle invokes of "_Unwind_Resume_or_Rethrow" or
86 /// "_Unwind_SjLj_Resume" calls. The "unwind" part of these invokes jump to
87 /// a landing pad within the current function. This is a candidate to merge
88 /// the selector associated with the URoR invoke with the one from the
89 /// URoR's landing pad.
90 bool HandleURoRInvokes();
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 SmallPtrSet
<PHINode
*, 32> &SeenPHIs
);
100 static char ID
; // Pass identification, replacement for typeid.
101 DwarfEHPrepare(const TargetMachine
*tm
) :
102 FunctionPass(ID
), TM(tm
), TLI(TM
->getTargetLowering()),
103 ExceptionValueIntrinsic(0), SelectorIntrinsic(0),
104 URoR(0), EHCatchAllValue(0), RewindFunction(0) {
105 initializeDominatorTreePass(*PassRegistry::getPassRegistry());
108 virtual bool runOnFunction(Function
&Fn
);
110 // getAnalysisUsage - We need the dominator tree for handling URoR.
111 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
112 AU
.addRequired
<DominatorTree
>();
113 AU
.addPreserved
<DominatorTree
>();
116 const char *getPassName() const {
117 return "Exception handling preparation";
121 } // end anonymous namespace
123 char DwarfEHPrepare::ID
= 0;
125 FunctionPass
*llvm::createDwarfEHPass(const TargetMachine
*tm
) {
126 return new DwarfEHPrepare(tm
);
129 /// HasCatchAllInSelector - Return true if the intrinsic instruction has a
131 bool DwarfEHPrepare::HasCatchAllInSelector(IntrinsicInst
*II
) {
132 if (!EHCatchAllValue
) return false;
134 unsigned ArgIdx
= II
->getNumArgOperands() - 1;
135 GlobalVariable
*GV
= dyn_cast
<GlobalVariable
>(II
->getArgOperand(ArgIdx
));
136 return GV
== EHCatchAllValue
;
139 /// FindAllCleanupSelectors - Find all eh.selector calls that are clean-ups.
140 void DwarfEHPrepare::
141 FindAllCleanupSelectors(SmallPtrSet
<IntrinsicInst
*, 32> &Sels
,
142 SmallPtrSet
<IntrinsicInst
*, 32> &CatchAllSels
) {
143 for (Value::use_iterator
144 I
= SelectorIntrinsic
->use_begin(),
145 E
= SelectorIntrinsic
->use_end(); I
!= E
; ++I
) {
146 IntrinsicInst
*II
= cast
<IntrinsicInst
>(*I
);
148 if (II
->getParent()->getParent() != F
)
151 if (!HasCatchAllInSelector(II
))
154 CatchAllSels
.insert(II
);
158 /// FindAllURoRInvokes - Find all URoR invokes in the function.
159 void DwarfEHPrepare::
160 FindAllURoRInvokes(SmallPtrSet
<InvokeInst
*, 32> &URoRInvokes
) {
161 for (Value::use_iterator
162 I
= URoR
->use_begin(),
163 E
= URoR
->use_end(); I
!= E
; ++I
) {
164 if (InvokeInst
*II
= dyn_cast
<InvokeInst
>(*I
))
165 URoRInvokes
.insert(II
);
169 /// CleanupSelectors - Any remaining eh.selector intrinsic calls which still use
170 /// the "llvm.eh.catch.all.value" call need to convert to using its
171 /// initializer instead.
172 bool DwarfEHPrepare::CleanupSelectors(SmallPtrSet
<IntrinsicInst
*, 32> &Sels
) {
173 if (!EHCatchAllValue
) return false;
175 if (!SelectorIntrinsic
) {
177 Intrinsic::getDeclaration(F
->getParent(), Intrinsic::eh_selector
);
178 if (!SelectorIntrinsic
) return false;
181 bool Changed
= false;
182 for (SmallPtrSet
<IntrinsicInst
*, 32>::iterator
183 I
= Sels
.begin(), E
= Sels
.end(); I
!= E
; ++I
) {
184 IntrinsicInst
*Sel
= *I
;
186 // Index of the "llvm.eh.catch.all.value" variable.
187 unsigned OpIdx
= Sel
->getNumArgOperands() - 1;
188 GlobalVariable
*GV
= dyn_cast
<GlobalVariable
>(Sel
->getArgOperand(OpIdx
));
189 if (GV
!= EHCatchAllValue
) continue;
190 Sel
->setArgOperand(OpIdx
, EHCatchAllValue
->getInitializer());
197 /// FindSelectorAndURoR - Find the eh.selector call associated with the
198 /// eh.exception call. And indicate if there is a URoR "invoke" associated with
199 /// the eh.exception call. This recursively looks past instructions which don't
200 /// change the EH pointer value, like casts or PHI nodes.
202 DwarfEHPrepare::FindSelectorAndURoR(Instruction
*Inst
, bool &URoRInvoke
,
203 SmallPtrSet
<IntrinsicInst
*, 8> &SelCalls
,
204 SmallPtrSet
<PHINode
*, 32> &SeenPHIs
) {
205 bool Changed
= false;
207 for (Value::use_iterator
208 I
= Inst
->use_begin(), E
= Inst
->use_end(); I
!= E
; ++I
) {
209 Instruction
*II
= dyn_cast
<Instruction
>(*I
);
210 if (!II
|| II
->getParent()->getParent() != F
) continue;
212 if (IntrinsicInst
*Sel
= dyn_cast
<IntrinsicInst
>(II
)) {
213 if (Sel
->getIntrinsicID() == Intrinsic::eh_selector
)
214 SelCalls
.insert(Sel
);
215 } else if (InvokeInst
*Invoke
= dyn_cast
<InvokeInst
>(II
)) {
216 if (Invoke
->getCalledFunction() == URoR
)
218 } else if (CastInst
*CI
= dyn_cast
<CastInst
>(II
)) {
219 Changed
|= FindSelectorAndURoR(CI
, URoRInvoke
, SelCalls
, SeenPHIs
);
220 } else if (PHINode
*PN
= dyn_cast
<PHINode
>(II
)) {
221 if (SeenPHIs
.insert(PN
))
222 // Don't process a PHI node more than once.
223 Changed
|= FindSelectorAndURoR(PN
, URoRInvoke
, SelCalls
, SeenPHIs
);
230 /// HandleURoRInvokes - Handle invokes of "_Unwind_Resume_or_Rethrow" or
231 /// "_Unwind_SjLj_Resume" calls. The "unwind" part of these invokes jump to a
232 /// landing pad within the current function. This is a candidate to merge the
233 /// selector associated with the URoR invoke with the one from the URoR's
235 bool DwarfEHPrepare::HandleURoRInvokes() {
236 if (!EHCatchAllValue
) {
238 F
->getParent()->getNamedGlobal("llvm.eh.catch.all.value");
239 if (!EHCatchAllValue
) return false;
242 if (!SelectorIntrinsic
) {
244 Intrinsic::getDeclaration(F
->getParent(), Intrinsic::eh_selector
);
245 if (!SelectorIntrinsic
) return false;
248 SmallPtrSet
<IntrinsicInst
*, 32> Sels
;
249 SmallPtrSet
<IntrinsicInst
*, 32> CatchAllSels
;
250 FindAllCleanupSelectors(Sels
, CatchAllSels
);
253 URoR
= F
->getParent()->getFunction("_Unwind_Resume_or_Rethrow");
255 URoR
= F
->getParent()->getFunction("_Unwind_SjLj_Resume");
256 if (!URoR
) return CleanupSelectors(CatchAllSels
);
260 SmallPtrSet
<InvokeInst
*, 32> URoRInvokes
;
261 FindAllURoRInvokes(URoRInvokes
);
263 SmallPtrSet
<IntrinsicInst
*, 32> SelsToConvert
;
265 for (SmallPtrSet
<IntrinsicInst
*, 32>::iterator
266 SI
= Sels
.begin(), SE
= Sels
.end(); SI
!= SE
; ++SI
) {
267 const BasicBlock
*SelBB
= (*SI
)->getParent();
268 for (SmallPtrSet
<InvokeInst
*, 32>::iterator
269 UI
= URoRInvokes
.begin(), UE
= URoRInvokes
.end(); UI
!= UE
; ++UI
) {
270 const BasicBlock
*URoRBB
= (*UI
)->getParent();
271 if (DT
->dominates(SelBB
, URoRBB
)) {
272 SelsToConvert
.insert(*SI
);
278 bool Changed
= false;
280 if (Sels
.size() != SelsToConvert
.size()) {
281 // If we haven't been able to convert all of the clean-up selectors, then
282 // loop through the slow way to see if they still need to be converted.
283 if (!ExceptionValueIntrinsic
) {
284 ExceptionValueIntrinsic
=
285 Intrinsic::getDeclaration(F
->getParent(), Intrinsic::eh_exception
);
286 if (!ExceptionValueIntrinsic
)
287 return CleanupSelectors(CatchAllSels
);
290 for (Value::use_iterator
291 I
= ExceptionValueIntrinsic
->use_begin(),
292 E
= ExceptionValueIntrinsic
->use_end(); I
!= E
; ++I
) {
293 IntrinsicInst
*EHPtr
= dyn_cast
<IntrinsicInst
>(*I
);
294 if (!EHPtr
|| EHPtr
->getParent()->getParent() != F
) continue;
296 bool URoRInvoke
= false;
297 SmallPtrSet
<IntrinsicInst
*, 8> SelCalls
;
298 SmallPtrSet
<PHINode
*, 32> SeenPHIs
;
299 Changed
|= FindSelectorAndURoR(EHPtr
, URoRInvoke
, SelCalls
, SeenPHIs
);
302 // This EH pointer is being used by an invoke of an URoR instruction and
303 // an eh.selector intrinsic call. If the eh.selector is a 'clean-up', we
304 // need to convert it to a 'catch-all'.
305 for (SmallPtrSet
<IntrinsicInst
*, 8>::iterator
306 SI
= SelCalls
.begin(), SE
= SelCalls
.end(); SI
!= SE
; ++SI
)
307 if (!HasCatchAllInSelector(*SI
))
308 SelsToConvert
.insert(*SI
);
313 if (!SelsToConvert
.empty()) {
314 // Convert all clean-up eh.selectors, which are associated with "invokes" of
315 // URoR calls, into catch-all eh.selectors.
318 for (SmallPtrSet
<IntrinsicInst
*, 8>::iterator
319 SI
= SelsToConvert
.begin(), SE
= SelsToConvert
.end();
321 IntrinsicInst
*II
= *SI
;
323 // Use the exception object pointer and the personality function
324 // from the original selector.
326 IntrinsicInst::op_iterator I
= CS
.arg_begin();
327 IntrinsicInst::op_iterator E
= CS
.arg_end();
328 IntrinsicInst::op_iterator B
= prior(E
);
330 // Exclude last argument if it is an integer.
331 if (isa
<ConstantInt
>(B
)) E
= B
;
333 // Add exception object pointer (front).
334 // Add personality function (next).
335 // Add in any filter IDs (rest).
336 SmallVector
<Value
*, 8> Args(I
, E
);
338 Args
.push_back(EHCatchAllValue
->getInitializer()); // Catch-all indicator.
340 CallInst
*NewSelector
=
341 CallInst::Create(SelectorIntrinsic
, Args
.begin(), Args
.end(),
342 "eh.sel.catch.all", II
);
344 NewSelector
->setTailCall(II
->isTailCall());
345 NewSelector
->setAttributes(II
->getAttributes());
346 NewSelector
->setCallingConv(II
->getCallingConv());
348 II
->replaceAllUsesWith(NewSelector
);
349 II
->eraseFromParent();
353 Changed
|= CleanupSelectors(CatchAllSels
);
357 /// NormalizeLandingPads - Normalize and discover landing pads, noting them
358 /// in the LandingPads set. A landing pad is normal if the only CFG edges
359 /// that end at it are unwind edges from invoke instructions. If we inlined
360 /// through an invoke we could have a normal branch from the previous
361 /// unwind block through to the landing pad for the original invoke.
362 /// Abnormal landing pads are fixed up by redirecting all unwind edges to
363 /// a new basic block which falls through to the original.
364 bool DwarfEHPrepare::NormalizeLandingPads() {
365 bool Changed
= false;
367 const MCAsmInfo
*MAI
= TM
->getMCAsmInfo();
368 bool usingSjLjEH
= MAI
->getExceptionHandlingType() == ExceptionHandling::SjLj
;
370 for (Function::iterator I
= F
->begin(), E
= F
->end(); I
!= E
; ++I
) {
371 TerminatorInst
*TI
= I
->getTerminator();
372 if (!isa
<InvokeInst
>(TI
))
374 BasicBlock
*LPad
= TI
->getSuccessor(1);
375 // Skip landing pads that have already been normalized.
376 if (LandingPads
.count(LPad
))
379 // Check that only invoke unwind edges end at the landing pad.
380 bool OnlyUnwoundTo
= true;
381 bool SwitchOK
= usingSjLjEH
;
382 for (pred_iterator PI
= pred_begin(LPad
), PE
= pred_end(LPad
);
384 TerminatorInst
*PT
= (*PI
)->getTerminator();
385 // The SjLj dispatch block uses a switch instruction. This is effectively
386 // an unwind edge, so we can disregard it here. There will only ever
387 // be one dispatch, however, so if there are multiple switches, one
388 // of them truly is a normal edge, not an unwind edge.
389 if (SwitchOK
&& isa
<SwitchInst
>(PT
)) {
393 if (!isa
<InvokeInst
>(PT
) || LPad
== PT
->getSuccessor(0)) {
394 OnlyUnwoundTo
= false;
400 // Only unwind edges lead to the landing pad. Remember the landing pad.
401 LandingPads
.insert(LPad
);
405 // At least one normal edge ends at the landing pad. Redirect the unwind
406 // edges to a new basic block which falls through into this one.
408 // Create the new basic block.
409 BasicBlock
*NewBB
= BasicBlock::Create(F
->getContext(),
410 LPad
->getName() + "_unwind_edge");
412 // Insert it into the function right before the original landing pad.
413 LPad
->getParent()->getBasicBlockList().insert(LPad
, NewBB
);
415 // Redirect unwind edges from the original landing pad to NewBB.
416 for (pred_iterator PI
= pred_begin(LPad
), PE
= pred_end(LPad
); PI
!= PE
; ) {
417 TerminatorInst
*PT
= (*PI
++)->getTerminator();
418 if (isa
<InvokeInst
>(PT
) && PT
->getSuccessor(1) == LPad
)
419 // Unwind to the new block.
420 PT
->setSuccessor(1, NewBB
);
423 // If there are any PHI nodes in LPad, we need to update them so that they
424 // merge incoming values from NewBB instead.
425 for (BasicBlock::iterator II
= LPad
->begin(); isa
<PHINode
>(II
); ++II
) {
426 PHINode
*PN
= cast
<PHINode
>(II
);
427 pred_iterator PB
= pred_begin(NewBB
), PE
= pred_end(NewBB
);
429 // Check to see if all of the values coming in via unwind edges are the
430 // same. If so, we don't need to create a new PHI node.
431 Value
*InVal
= PN
->getIncomingValueForBlock(*PB
);
432 for (pred_iterator PI
= PB
; PI
!= PE
; ++PI
) {
433 if (PI
!= PB
&& InVal
!= PN
->getIncomingValueForBlock(*PI
)) {
440 // Different unwind edges have different values. Create a new PHI node
442 PHINode
*NewPN
= PHINode::Create(PN
->getType(), PN
->getName()+".unwind",
444 // Add an entry for each unwind edge, using the value from the old PHI.
445 for (pred_iterator PI
= PB
; PI
!= PE
; ++PI
)
446 NewPN
->addIncoming(PN
->getIncomingValueForBlock(*PI
), *PI
);
448 // Now use this new PHI as the common incoming value for NewBB in PN.
452 // Revector exactly one entry in the PHI node to come from NewBB
453 // and delete all other entries that come from unwind edges. If
454 // there are both normal and unwind edges from the same predecessor,
455 // this leaves an entry for the normal edge.
456 for (pred_iterator PI
= PB
; PI
!= PE
; ++PI
)
457 PN
->removeIncomingValue(*PI
);
458 PN
->addIncoming(InVal
, NewBB
);
461 // Add a fallthrough from NewBB to the original landing pad.
462 BranchInst::Create(LPad
, NewBB
);
464 // Now update DominatorTree analysis information.
465 DT
->splitBlock(NewBB
);
467 // Remember the newly constructed landing pad. The original landing pad
468 // LPad is no longer a landing pad now that all unwind edges have been
469 // revectored to NewBB.
470 LandingPads
.insert(NewBB
);
471 ++NumLandingPadsSplit
;
478 /// LowerUnwinds - Turn unwind instructions into calls to _Unwind_Resume,
479 /// rethrowing any previously caught exception. This will crash horribly
480 /// at runtime if there is no such exception: using unwind to throw a new
481 /// exception is currently not supported.
482 bool DwarfEHPrepare::LowerUnwinds() {
483 SmallVector
<TerminatorInst
*, 16> UnwindInsts
;
485 for (Function::iterator I
= F
->begin(), E
= F
->end(); I
!= E
; ++I
) {
486 TerminatorInst
*TI
= I
->getTerminator();
487 if (isa
<UnwindInst
>(TI
))
488 UnwindInsts
.push_back(TI
);
491 if (UnwindInsts
.empty()) return false;
493 // Find the rewind function if we didn't already.
494 if (!RewindFunction
) {
495 LLVMContext
&Ctx
= UnwindInsts
[0]->getContext();
496 std::vector
<const Type
*>
497 Params(1, Type::getInt8PtrTy(Ctx
));
498 FunctionType
*FTy
= FunctionType::get(Type::getVoidTy(Ctx
),
500 const char *RewindName
= TLI
->getLibcallName(RTLIB::UNWIND_RESUME
);
501 RewindFunction
= F
->getParent()->getOrInsertFunction(RewindName
, FTy
);
504 bool Changed
= false;
506 for (SmallVectorImpl
<TerminatorInst
*>::iterator
507 I
= UnwindInsts
.begin(), E
= UnwindInsts
.end(); I
!= E
; ++I
) {
508 TerminatorInst
*TI
= *I
;
510 // Replace the unwind instruction with a call to _Unwind_Resume (or the
511 // appropriate target equivalent) followed by an UnreachableInst.
513 // Create the call...
514 CallInst
*CI
= CallInst::Create(RewindFunction
,
515 CreateExceptionValueCall(TI
->getParent()),
517 CI
->setCallingConv(TLI
->getLibcallCallingConv(RTLIB::UNWIND_RESUME
));
518 // ...followed by an UnreachableInst.
519 new UnreachableInst(TI
->getContext(), TI
);
521 // Nuke the unwind instruction.
522 TI
->eraseFromParent();
530 /// MoveExceptionValueCalls - Ensure that eh.exception is only ever called from
531 /// landing pads by replacing calls outside of landing pads with direct use of
532 /// a register holding the appropriate value; this requires adding calls inside
533 /// all landing pads to initialize the register. Also, move eh.exception calls
534 /// inside landing pads to the start of the landing pad (optional, but may make
535 /// things simpler for later passes).
536 bool DwarfEHPrepare::MoveExceptionValueCalls() {
537 // If the eh.exception intrinsic is not declared in the module then there is
538 // nothing to do. Speed up compilation by checking for this common case.
539 if (!ExceptionValueIntrinsic
&&
540 !F
->getParent()->getFunction(Intrinsic::getName(Intrinsic::eh_exception
)))
543 bool Changed
= false;
545 // Move calls to eh.exception that are inside a landing pad to the start of
547 for (BBSet::const_iterator LI
= LandingPads
.begin(), LE
= LandingPads
.end();
549 BasicBlock
*LP
= *LI
;
550 for (BasicBlock::iterator II
= LP
->getFirstNonPHIOrDbg(), IE
= LP
->end();
552 if (EHExceptionInst
*EI
= dyn_cast
<EHExceptionInst
>(II
++)) {
553 // Found a call to eh.exception.
554 if (!EI
->use_empty()) {
555 // If there is already a call to eh.exception at the start of the
556 // landing pad, then get hold of it; otherwise create such a call.
557 Value
*CallAtStart
= CreateExceptionValueCall(LP
);
559 // If the call was at the start of a landing pad then leave it alone.
560 if (EI
== CallAtStart
)
562 EI
->replaceAllUsesWith(CallAtStart
);
564 EI
->eraseFromParent();
565 ++NumExceptionValuesMoved
;
570 // Look for calls to eh.exception that are not in a landing pad. If one is
571 // found, then a register that holds the exception value will be created in
572 // each landing pad, and the SSAUpdater will be used to compute the values
573 // returned by eh.exception calls outside of landing pads.
576 // Remember where we found the eh.exception call, to avoid rescanning earlier
577 // basic blocks which we already know contain no eh.exception calls.
578 bool FoundCallOutsideLandingPad
= false;
579 Function::iterator BB
= F
->begin();
580 for (Function::iterator BE
= F
->end(); BB
!= BE
; ++BB
) {
581 // Skip over landing pads.
582 if (LandingPads
.count(BB
))
585 for (BasicBlock::iterator II
= BB
->getFirstNonPHIOrDbg(), IE
= BB
->end();
587 if (isa
<EHExceptionInst
>(II
)) {
588 SSA
.Initialize(II
->getType(), II
->getName());
589 FoundCallOutsideLandingPad
= true;
593 if (FoundCallOutsideLandingPad
)
597 // If all calls to eh.exception are in landing pads then we are done.
598 if (!FoundCallOutsideLandingPad
)
601 // Add a call to eh.exception at the start of each landing pad, and tell the
602 // SSAUpdater that this is the value produced by the landing pad.
603 for (BBSet::iterator LI
= LandingPads
.begin(), LE
= LandingPads
.end();
605 SSA
.AddAvailableValue(*LI
, CreateExceptionValueCall(*LI
));
607 // Now turn all calls to eh.exception that are not in a landing pad into a use
608 // of the appropriate register.
609 for (Function::iterator BE
= F
->end(); BB
!= BE
; ++BB
) {
610 // Skip over landing pads.
611 if (LandingPads
.count(BB
))
614 for (BasicBlock::iterator II
= BB
->getFirstNonPHIOrDbg(), IE
= BB
->end();
616 if (EHExceptionInst
*EI
= dyn_cast
<EHExceptionInst
>(II
++)) {
617 // Found a call to eh.exception, replace it with the value from any
618 // upstream landing pad(s).
619 EI
->replaceAllUsesWith(SSA
.GetValueAtEndOfBlock(BB
));
620 EI
->eraseFromParent();
621 ++NumExceptionValuesMoved
;
628 /// CreateExceptionValueCall - Insert a call to the eh.exception intrinsic at
629 /// the start of the basic block (unless there already is one, in which case
630 /// the existing call is returned).
631 Instruction
*DwarfEHPrepare::CreateExceptionValueCall(BasicBlock
*BB
) {
632 Instruction
*Start
= BB
->getFirstNonPHIOrDbg();
633 // Is this a call to eh.exception?
634 if (IntrinsicInst
*CI
= dyn_cast
<IntrinsicInst
>(Start
))
635 if (CI
->getIntrinsicID() == Intrinsic::eh_exception
)
636 // Reuse the existing call.
639 // Find the eh.exception intrinsic if we didn't already.
640 if (!ExceptionValueIntrinsic
)
641 ExceptionValueIntrinsic
= Intrinsic::getDeclaration(F
->getParent(),
642 Intrinsic::eh_exception
);
645 return CallInst::Create(ExceptionValueIntrinsic
, "eh.value.call", Start
);
648 bool DwarfEHPrepare::runOnFunction(Function
&Fn
) {
649 bool Changed
= false;
651 // Initialize internal state.
652 DT
= &getAnalysis
<DominatorTree
>();
655 // Ensure that only unwind edges end at landing pads (a landing pad is a
656 // basic block where an invoke unwind edge ends).
657 Changed
|= NormalizeLandingPads();
659 // Turn unwind instructions into libcalls.
660 Changed
|= LowerUnwinds();
662 // TODO: Move eh.selector calls to landing pads and combine them.
664 // Move eh.exception calls to landing pads.
665 Changed
|= MoveExceptionValueCalls();
667 Changed
|= HandleURoRInvokes();