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/Target/TargetLowering.h"
26 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
27 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
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");
36 class DwarfEHPrepare : public FunctionPass {
37 const TargetLowering *TLI;
40 // The eh.exception intrinsic.
41 Function *ExceptionValueIntrinsic;
43 // The eh.selector intrinsic.
44 Function *SelectorIntrinsic;
46 // _Unwind_Resume_or_Rethrow call.
49 // The EH language-specific catch-all type.
50 GlobalVariable *EHCatchAllValue;
52 // _Unwind_Resume or the target equivalent.
53 Constant *RewindFunction;
55 // Dominator info is used when turning stack temporaries into registers.
57 DominanceFrontier *DF;
59 // The function we are running on.
62 // The landing pads for this function.
63 typedef SmallPtrSet<BasicBlock*, 8> BBSet;
66 // Stack temporary used to hold eh.exception values.
67 AllocaInst *ExceptionValueVar;
69 bool NormalizeLandingPads();
71 bool MoveExceptionValueCalls();
72 bool FinishStackTemporaries();
73 bool PromoteStackTemporaries();
75 Instruction *CreateExceptionValueCall(BasicBlock *BB);
76 Instruction *CreateValueLoad(BasicBlock *BB);
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);
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();
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
96 bool HandleURoRInvokes();
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);
104 /// DoMem2RegPromotion - Take an alloca call and promote it from memory to a
106 bool DoMem2RegPromotion(Value *V) {
107 AllocaInst *AI = dyn_cast<AllocaInst>(V);
108 if (!AI || !isAllocaPromotable(AI)) return false;
110 // Turn the alloca into a register.
111 std::vector<AllocaInst*> Allocas(1, AI);
112 PromoteMemToReg(Allocas, *DT, *DF);
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)))
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;
129 bool Changed = false;
134 for (Value::use_iterator
135 I = II->use_begin(), E = II->use_end(); I != E; ++I) {
136 SI = dyn_cast<StoreInst>(I);
140 if (!PromoteStoreInst(SI))
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) {}
156 virtual bool runOnFunction(Function &Fn);
158 // getAnalysisUsage - We need dominance frontiers for memory promotion.
159 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
161 AU.addRequired<DominatorTree>();
162 AU.addPreserved<DominatorTree>();
164 AU.addRequired<DominanceFrontier>();
165 AU.addPreserved<DominanceFrontier>();
168 const char *getPassName() const {
169 return "Exception handling preparation";
173 } // end anonymous namespace
175 char DwarfEHPrepare::ID = 0;
177 FunctionPass *llvm::createDwarfEHPass(const TargetLowering *tli, bool fast) {
178 return new DwarfEHPrepare(tli, fast);
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.
186 DwarfEHPrepare::FindSelectorAndURoR(Instruction *Inst, bool &URoRInvoke,
187 SmallPtrSet<IntrinsicInst*, 8> &SelCalls) {
188 SmallPtrSet<PHINode*, 32> SeenPHIs;
189 bool Changed = false;
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;
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)
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;
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);
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;
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());
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) {
245 F->getParent()->getNamedGlobal(".llvm.eh.catch.all.value");
246 if (!EHCatchAllValue) return false;
249 if (!SelectorIntrinsic) {
251 Intrinsic::getDeclaration(F->getParent(), Intrinsic::eh_selector);
252 if (!SelectorIntrinsic) return false;
256 URoR = F->getParent()->getFunction("_Unwind_Resume_or_Rethrow");
263 if (!ExceptionValueIntrinsic) {
264 ExceptionValueIntrinsic =
265 Intrinsic::getDeclaration(F->getParent(), Intrinsic::eh_exception);
266 if (!ExceptionValueIntrinsic) {
272 bool Changed = false;
273 SmallPtrSet<IntrinsicInst*, 32> SelsToConvert;
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;
281 Changed |= PromoteEHPtrStore(EHPtr);
283 bool URoRInvoke = false;
284 SmallPtrSet<IntrinsicInst*, 8> SelCalls;
285 Changed |= FindSelectorAndURoR(EHPtr, URoRInvoke, SelCalls);
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();
297 bool IsCleanUp = (NumOps == 3);
300 if (ConstantInt *CI = dyn_cast<ConstantInt>(II->getOperand(3)))
301 IsCleanUp = (CI->getZExtValue() == 0);
304 SelsToConvert.insert(II);
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.
315 for (SmallPtrSet<IntrinsicInst*, 8>::iterator
316 SI = SelsToConvert.begin(), SE = SelsToConvert.end();
318 IntrinsicInst *II = *SI;
319 SmallVector<Value*, 8> Args;
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.
327 CallInst *NewSelector =
328 CallInst::Create(SelectorIntrinsic, Args.begin(), Args.end(),
329 "eh.sel.catch.all", II);
331 NewSelector->setTailCall(II->isTailCall());
332 NewSelector->setAttributes(II->getAttributes());
333 NewSelector->setCallingConv(II->getCallingConv());
335 II->replaceAllUsesWith(NewSelector);
336 II->eraseFromParent();
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;
354 const MCAsmInfo *MAI = TLI->getTargetMachine().getMCAsmInfo();
355 bool usingSjLjEH = MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
357 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
358 TerminatorInst *TI = I->getTerminator();
359 if (!isa<InvokeInst>(TI))
361 BasicBlock *LPad = TI->getSuccessor(1);
362 // Skip landing pads that have already been normalized.
363 if (LandingPads.count(LPad))
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);
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)) {
380 if (!isa<InvokeInst>(PT) || LPad == PT->getSuccessor(0)) {
381 OnlyUnwoundTo = false;
387 // Only unwind edges lead to the landing pad. Remember the landing pad.
388 LandingPads.insert(LPad);
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.
395 // Create the new basic block.
396 BasicBlock *NewBB = BasicBlock::Create(F->getContext(),
397 LPad->getName() + "_unwind_edge");
399 // Insert it into the function right before the original landing pad.
400 LPad->getParent()->getBasicBlockList().insert(LPad, NewBB);
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);
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);
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)) {
427 // Different unwind edges have different values. Create a new PHI node
429 PHINode *NewPN = PHINode::Create(PN->getType(), PN->getName()+".unwind",
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);
435 // Now use this new PHI as the common incoming value for NewBB in PN.
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);
448 // Add a fallthrough from NewBB to the original landing pad.
449 BranchInst::Create(LPad, NewBB);
451 // Now update DominatorTree and DominanceFrontier analysis information.
453 DT->splitBlock(NewBB);
455 DF->splitBlock(NewBB);
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;
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;
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);
481 if (UnwindInsts.empty()) return false;
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),
490 const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME);
491 RewindFunction = F->getParent()->getOrInsertFunction(RewindName, FTy);
494 bool Changed = false;
496 for (SmallVectorImpl<TerminatorInst*>::iterator
497 I = UnwindInsts.begin(), E = UnwindInsts.end(); I != E; ++I) {
498 TerminatorInst *TI = *I;
500 // Replace the unwind instruction with a call to _Unwind_Resume (or the
501 // appropriate target equivalent) followed by an UnreachableInst.
503 // Create the call...
504 CallInst *CI = CallInst::Create(RewindFunction,
505 CreateReadOfExceptionValue(TI->getParent()),
507 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
508 // ...followed by an UnreachableInst.
509 new UnreachableInst(TI->getContext(), TI);
511 // Nuke the unwind instruction.
512 TI->eraseFromParent();
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)))
531 bool Changed = false;
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!");
545 CI->replaceAllUsesWith(ExceptionValue);
547 CI->eraseFromParent();
548 ++NumExceptionValuesMoved;
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)
563 bool Changed = false;
565 // Make sure that there is a store of the exception value at the start of
567 for (BBSet::iterator LI = LandingPads.begin(), LE = LandingPads.end();
569 Instruction *ExceptionValue = CreateReadOfExceptionValue(*LI);
570 Instruction *Store = new StoreInst(ExceptionValue, ExceptionValueVar);
571 Store->insertAfter(ExceptionValue);
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);
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.
601 // Find the eh.exception intrinsic if we didn't already.
602 if (!ExceptionValueIntrinsic)
603 ExceptionValueIntrinsic = Intrinsic::getDeclaration(F->getParent(),
604 Intrinsic::eh_exception);
607 return CallInst::Create(ExceptionValueIntrinsic, "eh.value.call", Start);
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.
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;
630 return new LoadInst(ExceptionValueVar, "eh.value.load", Start);
633 bool DwarfEHPrepare::runOnFunction(Function &Fn) {
634 bool Changed = false;
636 // Initialize internal state.
637 DT = getAnalysisIfAvailable<DominatorTree>();
638 DF = getAnalysisIfAvailable<DominanceFrontier>();
639 ExceptionValueVar = 0;
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();
646 // Turn unwind instructions into libcalls.
647 Changed |= LowerUnwinds();
649 // TODO: Move eh.selector calls to landing pads and combine them.
651 // Move eh.exception calls to landing pads.
652 Changed |= MoveExceptionValueCalls();
654 // Initialize any stack temporaries we introduced.
655 Changed |= FinishStackTemporaries();
657 // Turn any stack temporaries into registers if possible.
659 Changed |= PromoteStackTemporaries();
661 Changed |= HandleURoRInvokes();