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/ADT/Statistic.h"
17 #include "llvm/Analysis/Dominators.h"
18 #include "llvm/CodeGen/Passes.h"
19 #include "llvm/Function.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/IntrinsicInst.h"
22 #include "llvm/Module.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Support/Compiler.h"
25 #include "llvm/Support/IRBuilder.h"
26 #include "llvm/Target/TargetLowering.h"
27 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
28 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
31 STATISTIC(NumExceptionValuesMoved, "Number of eh.exception calls moved");
32 STATISTIC(NumLonelyLandingPads, "Number of landing pads with no selector");
33 STATISTIC(NumLonelySelectors, "Number of lonely selectors lowered");
34 STATISTIC(NumLandingPadsSplit, "Number of landing pads split");
35 STATISTIC(NumSelectorsAdjusted, "Number of selector results adjusted");
36 STATISTIC(NumSelectorsSimplified, "Number of selectors truncated");
37 STATISTIC(NumStackTempsIntroduced, "Number of stack temporaries introduced");
38 STATISTIC(NumUnwindsLowered, "Number of unwind instructions lowered");
41 class DwarfEHPrepare : public FunctionPass {
42 const TargetLowering *TLI;
44 // The eh.exception intrinsic.
45 Function *ExceptionIntrinsic;
47 // The eh.selector intrinsic.
48 Function *SelectorIntrinsic;
50 // The eh.typeid.for intrinsic.
51 Function *TypeIdIntrinsic;
53 // _Unwind_Resume or the target equivalent.
54 Constant *RewindFunction;
56 // _Unwind_RaiseException.
57 Constant *UnwindFunction;
59 // Dominator info is used when turning stack temporaries into registers.
61 DominanceFrontier *DF;
63 // The function we are running on.
66 // The current context.
69 // The personality and catch-all value for this function.
70 Constant *Personality;
73 // The landing pads for this function.
74 typedef SmallPtrSet<BasicBlock*, 8> BBSet;
77 // Stack temporary used to hold eh.exception values.
78 AllocaInst *ExceptionValueVar;
80 bool NormalizeLandingPads();
82 bool MoveSelectorCalls();
83 bool RectifySelectorCalls();
84 bool MoveExceptionValueCalls();
85 bool AddMissingSelectors();
86 bool FinishStackTemporaries();
87 bool PromoteStackTemporaries();
89 Instruction *CreateExceptionValueCall(BasicBlock *BB);
90 Instruction *CreateValueLoad(BasicBlock *BB);
92 /// CreateReadOfExceptionValue - Return the result of the eh.exception
93 /// intrinsic by calling the intrinsic if in a landing pad, or loading
94 /// it from the exception value variable otherwise.
95 Instruction *CreateReadOfExceptionValue(BasicBlock *BB) {
96 return LandingPads.count(BB) ?
97 CreateExceptionValueCall(BB) : CreateValueLoad(BB);
101 static char ID; // Pass identification, replacement for typeid.
102 DwarfEHPrepare(const TargetLowering *tli) :
103 FunctionPass(&ID), TLI(tli), ExceptionIntrinsic(0),
104 SelectorIntrinsic(0), TypeIdIntrinsic(0), RewindFunction(0),
107 virtual bool runOnFunction(Function &Fn);
109 const char *getPassName() const {
110 return "Exception handling preparation";
114 } // end anonymous namespace
116 char DwarfEHPrepare::ID = 0;
118 FunctionPass *llvm::createDwarfEHPass(const TargetLowering *tli) {
119 return new DwarfEHPrepare(tli);
122 /// NormalizeLandingPads - Normalize and discover landing pads, noting them
123 /// in the LandingPads set. A landing pad is normal if the only CFG edges
124 /// that end at it are unwind edges from invoke instructions. If we inlined
125 /// through an invoke we could have a normal branch from the previous
126 /// unwind block through to the landing pad for the original invoke.
127 /// Abnormal landing pads are fixed up by redirecting all unwind edges to
128 /// a new basic block which falls through to the original.
129 bool DwarfEHPrepare::NormalizeLandingPads() {
130 bool Changed = false;
132 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
133 TerminatorInst *TI = I->getTerminator();
134 if (!isa<InvokeInst>(TI))
136 BasicBlock *LPad = TI->getSuccessor(1);
137 // Skip landing pads that have already been normalized.
138 if (LandingPads.count(LPad))
141 // Check that only invoke unwind edges end at the landing pad.
142 bool OnlyUnwoundTo = true;
143 for (pred_iterator PI = pred_begin(LPad), PE = pred_end(LPad);
145 TerminatorInst *PT = (*PI)->getTerminator();
146 if (!isa<InvokeInst>(PT) || LPad == PT->getSuccessor(0)) {
147 OnlyUnwoundTo = false;
153 // Only unwind edges lead to the landing pad. Remember the landing pad.
154 LandingPads.insert(LPad);
158 // At least one normal edge ends at the landing pad. Redirect the unwind
159 // edges to a new basic block which falls through into this one.
161 // Create the new basic block.
162 BasicBlock *NewBB = BasicBlock::Create(*Context,
163 LPad->getName() + "_unwind_edge");
165 // Insert it into the function right before the original landing pad.
166 LPad->getParent()->getBasicBlockList().insert(LPad, NewBB);
168 // Redirect unwind edges from the original landing pad to NewBB.
169 for (pred_iterator PI = pred_begin(LPad), PE = pred_end(LPad); PI != PE; ) {
170 TerminatorInst *PT = (*PI++)->getTerminator();
171 if (isa<InvokeInst>(PT) && PT->getSuccessor(1) == LPad)
172 // Unwind to the new block.
173 PT->setSuccessor(1, NewBB);
176 // If there are any PHI nodes in LPad, we need to update them so that they
177 // merge incoming values from NewBB instead.
178 for (BasicBlock::iterator II = LPad->begin(); isa<PHINode>(II); ++II) {
179 PHINode *PN = cast<PHINode>(II);
180 pred_iterator PB = pred_begin(NewBB), PE = pred_end(NewBB);
182 // Check to see if all of the values coming in via unwind edges are the
183 // same. If so, we don't need to create a new PHI node.
184 Value *InVal = PN->getIncomingValueForBlock(*PB);
185 for (pred_iterator PI = PB; PI != PE; ++PI) {
186 if (PI != PB && InVal != PN->getIncomingValueForBlock(*PI)) {
193 // Different unwind edges have different values. Create a new PHI node
195 PHINode *NewPN = PHINode::Create(PN->getType(), PN->getName()+".unwind",
197 // Add an entry for each unwind edge, using the value from the old PHI.
198 for (pred_iterator PI = PB; PI != PE; ++PI)
199 NewPN->addIncoming(PN->getIncomingValueForBlock(*PI), *PI);
201 // Now use this new PHI as the common incoming value for NewBB in PN.
205 // Revector exactly one entry in the PHI node to come from NewBB
206 // and delete all other entries that come from unwind edges. If
207 // there are both normal and unwind edges from the same predecessor,
208 // this leaves an entry for the normal edge.
209 for (pred_iterator PI = PB; PI != PE; ++PI)
210 PN->removeIncomingValue(*PI);
211 PN->addIncoming(InVal, NewBB);
214 // Add a fallthrough from NewBB to the original landing pad.
215 BranchInst::Create(LPad, NewBB);
217 // Now update DominatorTree and DominanceFrontier analysis information.
219 DT->splitBlock(NewBB);
221 DF->splitBlock(NewBB);
223 // Remember the newly constructed landing pad. The original landing pad
224 // LPad is no longer a landing pad now that all unwind edges have been
225 // revectored to NewBB.
226 LandingPads.insert(NewBB);
227 ++NumLandingPadsSplit;
234 /// LowerUnwinds - Turn unwind instructions into calls to _Unwind_Resume,
235 /// rethrowing any previously caught exception. This will crash horribly
236 /// at runtime if there is no such exception: using unwind to throw a new
237 /// exception is currently not supported.
238 bool DwarfEHPrepare::LowerUnwinds() {
239 SmallVector<TerminatorInst*, 16> UnwindInsts;
241 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
242 TerminatorInst *TI = I->getTerminator();
243 if (isa<UnwindInst>(TI))
244 UnwindInsts.push_back(TI);
247 if (UnwindInsts.empty()) return false;
249 // Find the rewind function if we didn't already.
250 if (!RewindFunction) {
251 std::vector<const Type*>
252 Params(1, Type::getInt8PtrTy(*Context));
253 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Context),
255 const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME);
256 RewindFunction = F->getParent()->getOrInsertFunction(RewindName, FTy);
259 for (SmallVectorImpl<TerminatorInst*>::iterator
260 I = UnwindInsts.begin(), E = UnwindInsts.end(); I != E; ++I) {
261 TerminatorInst *TI = *I;
263 // Replace the unwind instruction with a call to _Unwind_Resume (or the
264 // appropriate target equivalent) followed by an UnreachableInst.
266 // Create the call...
267 CallInst *CI = CallInst::Create(RewindFunction,
268 CreateReadOfExceptionValue(TI->getParent()),
270 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
271 // ...followed by an UnreachableInst.
272 new UnreachableInst(*Context, TI);
274 // Nuke the unwind instruction.
275 TI->eraseFromParent();
282 /// MoveSelectorCalls - Make sure that every call to eh.selector occurs in its
283 /// own landing pad, the landing pad corresponding to the exception object.
284 bool DwarfEHPrepare::MoveSelectorCalls() {
285 // If the eh.selector intrinsic is not declared in the module then there is
286 // nothing to do. Speed up compilation by checking for this common case.
287 if (!F->getParent()->getFunction(Intrinsic::getName(Intrinsic::eh_selector)))
290 // TODO: There is a lot of room for optimization here.
292 bool Changed = false;
293 BasicBlock *UnrBB = 0;
295 for (Function::iterator BB = F->begin(); BB != F->end(); ++BB) {
296 // If this basic block is not a landing pad then synthesize a landing pad
297 // for every selector in it.
298 bool SynthesizeLandingPad = !LandingPads.count(BB);
300 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II) {
301 EHSelectorInst *SI = dyn_cast<EHSelectorInst>(II);
302 // Only interested in eh.selector calls.
306 // Note the personality and catch-all for later use.
307 Personality = cast<Constant>(SI->getOperand(2));
308 CatchAll = cast<Constant>(SI->getOperand(SI->getNumOperands() - 1)
309 ->stripPointerCasts());
311 // The exception object.
312 Value *Exception = SI->getOperand(1);
314 if (!SynthesizeLandingPad) {
315 // Did the exception come from unwinding to this landing pad or another?
316 // If it comes from a different landing pad then we need to synthesize a
317 // new landing pad for the selector.
318 EHExceptionInst *EI = dyn_cast<EHExceptionInst>(Exception);
319 SynthesizeLandingPad = !EI || EI->getParent() != BB;
322 if (!SynthesizeLandingPad) {
323 // This is the first selector in this landing pad, and it is the landing
324 // pad corresponding to the exception object. No need to do anything to
325 // this selector, but any subsequent selectors in this landing pad will
326 // need their own invoke in order to make them independent of this one.
327 SynthesizeLandingPad = true;
331 // Rethrow the exception and catch it again, generating a landing pad for
332 // this selector to live in.
334 // Find _Unwind_RaiseException if we didn't already.
335 if (!UnwindFunction) {
336 std::vector<const Type*> ArgTys(1, Type::getInt8PtrTy(*Context));
337 const FunctionType *FTy =
338 FunctionType::get(Type::getInt32Ty(*Context), ArgTys, true);
340 const char *Name = "_Unwind_RaiseException";
341 UnwindFunction = F->getParent()->getOrInsertFunction(Name, FTy);
344 // Create a basic block containing only an unreachable instruction if we
347 UnrBB = BasicBlock::Create(*Context, "unreachable", F);
348 new UnreachableInst(*Context, UnrBB);
351 // Split the basic block before the selector.
352 BasicBlock *NewBB = SplitBlock(BB, SI, this);
354 // Replace the terminator with an invoke of _Unwind_RaiseException.
355 BB->getTerminator()->eraseFromParent();
356 InvokeInst::Create(UnwindFunction, UnrBB, NewBB, &Exception,
357 1 + &Exception, "", BB);
359 // The split off basic block is now a landing pad.
360 LandingPads.insert(NewBB);
362 // Replace the exception argument in the selector call with a call to
363 // eh.exception. This is not really necessary but it makes things more
365 Exception = CreateExceptionValueCall(NewBB);
366 SI->setOperand(1, Exception);
368 ++NumLonelySelectors;
371 // All instructions still in the original basic block have been scanned.
372 // Move on to the next basic block.
380 /// RectifySelectorCalls - Remove useless catch-all clauses from the ends of
381 /// selectors, or correct the selector result for the presence of the catch-all
382 /// if it is really needed.
383 bool DwarfEHPrepare::RectifySelectorCalls() {
384 // If the eh.selector intrinsic is not declared in the module then there is
385 // nothing to do. Speed up compilation by checking for this common case.
386 if (!F->getParent()->getFunction(Intrinsic::getName(Intrinsic::eh_selector)))
389 bool Changed = false;
391 for (BBSet::iterator I = LandingPads.begin(), E = LandingPads.end(); I != E;
393 for (BasicBlock::iterator II = (*I)->begin(), IE = (*I)->end(); II != IE; )
394 if (EHSelectorInst *SI = dyn_cast<EHSelectorInst>(II++)) {
395 // Found a call to eh.selector. Check whether it has a catch-all in the
397 unsigned LastIndex = 0;
398 for (unsigned i = 3, e = SI->getNumOperands() - 1; i < e; ++i) {
399 Value *V = SI->getOperand(i);
400 if (V->stripPointerCasts() == CatchAll) {
401 // A catch-all. The catch-all at the end was not needed.
404 } else if (ConstantInt *FilterLength = dyn_cast<ConstantInt>(V)) {
405 // A cleanup or a filter.
406 unsigned Length = FilterLength->getZExtValue();
408 // A cleanup - skip it.
411 // A catch-all filter. Drop everything that follows.
415 // A filter, skip over the typeinfos.
421 // Drop the pointless catch-all from the end. In fact drop everything
422 // after LastIndex as an optimization.
423 SmallVector<Value*, 16> Args;
424 Args.reserve(LastIndex);
425 for (unsigned i = 1; i <= LastIndex; ++i)
426 Args.push_back(SI->getOperand(i));
427 CallInst *CI = CallInst::Create(SI->getOperand(0), Args.begin(),
430 SI->replaceAllUsesWith(CI);
431 SI->eraseFromParent();
432 ++NumSelectorsSimplified;
433 } else if (!isa<ConstantInt>(CatchAll) && // Not a cleanup.
435 // Correct the selector value to return zero if the catch-all matches.
436 Constant *Zero = ConstantInt::getNullValue(Type::getInt32Ty(*Context));
438 // Create the new selector value, with placeholders instead of the
439 // real operands and make everyone use it. The reason for this round
440 // about approach is that the computation of the new value makes use
441 // of the old value, so we can't just compute it then do RAUW.
442 SelectInst *S = SelectInst::Create(ConstantInt::getFalse(*Context),
444 SI->replaceAllUsesWith(S);
446 // Now calculate the operands of the select.
447 IRBuilder<> Builder(*I, S);
449 // Find the eh.typeid.for intrinsic if we didn't already.
450 if (!TypeIdIntrinsic)
451 TypeIdIntrinsic = Intrinsic::getDeclaration(F->getParent(),
452 Intrinsic::eh_typeid_for);
454 // Obtain the id of the catch-all.
455 Value *CatchAllId = Builder.CreateCall(TypeIdIntrinsic,
456 ConstantExpr::getBitCast(CatchAll, Type::getInt8PtrTy(*Context)));
458 // Compare it with the original selector result. If it matched then
459 // the selector result is zero, otherwise it is the original selector.
460 Value *MatchesCatchAll = Builder.CreateICmpEQ(SI, CatchAllId);
461 S->setOperand(0, MatchesCatchAll);
462 S->setOperand(2, SI);
463 ++NumSelectorsAdjusted;
473 /// Make sure every landing pad has a selector in it.
474 bool DwarfEHPrepare::AddMissingSelectors() {
476 // We only know how to codegen invokes if there is a personality.
477 // FIXME: This results in wrong code.
480 bool Changed = false;
482 for (BBSet::iterator I = LandingPads.begin(), E = LandingPads.end(); I != E;
484 bool FoundSelector = false;
486 // Check whether the landing pad already contains a call to eh.selector.
487 for (BasicBlock::iterator II = (*I)->begin(), IE = (*I)->end(); II != IE;
489 if (isa<EHSelectorInst>(II)) {
490 FoundSelector = true;
497 // Find the eh.selector intrinsic if we didn't already.
498 if (!SelectorIntrinsic)
499 SelectorIntrinsic = Intrinsic::getDeclaration(F->getParent(),
500 Intrinsic::eh_selector);
502 // Get the exception object.
503 Instruction *Exception = CreateExceptionValueCall(*I);
505 Value *Args[3] = { Exception, Personality, CatchAll };
506 CallInst *Selector = CallInst::Create(SelectorIntrinsic, Args, Args + 3);
507 Selector->insertAfter(Exception);
509 ++NumLonelyLandingPads;
516 /// MoveExceptionValueCalls - Ensure that eh.exception is only ever called from
517 /// landing pads by replacing calls outside of landing pads with loads from a
518 /// stack temporary. Move eh.exception calls inside landing pads to the start
519 /// of the landing pad (optional, but may make things simpler for later passes).
520 bool DwarfEHPrepare::MoveExceptionValueCalls() {
521 // If the eh.exception intrinsic is not declared in the module then there is
522 // nothing to do. Speed up compilation by checking for this common case.
523 if (!ExceptionIntrinsic &&
524 !F->getParent()->getFunction(Intrinsic::getName(Intrinsic::eh_exception)))
527 bool Changed = false;
529 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
530 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;)
531 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++))
532 if (CI->getIntrinsicID() == Intrinsic::eh_exception) {
533 if (!CI->use_empty()) {
534 Value *ExceptionValue = CreateReadOfExceptionValue(BB);
535 if (CI == ExceptionValue) {
536 // The call was at the start of a landing pad - leave it alone.
537 assert(LandingPads.count(BB) &&
538 "Created eh.exception call outside landing pad!");
541 CI->replaceAllUsesWith(ExceptionValue);
543 CI->eraseFromParent();
544 ++NumExceptionValuesMoved;
552 /// FinishStackTemporaries - If we introduced a stack variable to hold the
553 /// exception value then initialize it in each landing pad.
554 bool DwarfEHPrepare::FinishStackTemporaries() {
555 if (!ExceptionValueVar)
559 bool Changed = false;
561 // Make sure that there is a store of the exception value at the start of
563 for (BBSet::iterator LI = LandingPads.begin(), LE = LandingPads.end();
565 Instruction *ExceptionValue = CreateReadOfExceptionValue(*LI);
566 Instruction *Store = new StoreInst(ExceptionValue, ExceptionValueVar);
567 Store->insertAfter(ExceptionValue);
574 /// PromoteStackTemporaries - Turn any stack temporaries we introduced into
575 /// registers if possible.
576 bool DwarfEHPrepare::PromoteStackTemporaries() {
577 if (ExceptionValueVar && DT && DF && isAllocaPromotable(ExceptionValueVar)) {
578 // Turn the exception temporary into registers and phi nodes if possible.
579 std::vector<AllocaInst*> Allocas(1, ExceptionValueVar);
580 PromoteMemToReg(Allocas, *DT, *DF, *Context);
586 /// CreateExceptionValueCall - Insert a call to the eh.exception intrinsic at
587 /// the start of the basic block (unless there already is one, in which case
588 /// the existing call is returned).
589 Instruction *DwarfEHPrepare::CreateExceptionValueCall(BasicBlock *BB) {
590 Instruction *Start = BB->getFirstNonPHI();
591 // Is this a call to eh.exception?
592 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Start))
593 if (CI->getIntrinsicID() == Intrinsic::eh_exception)
594 // Reuse the existing call.
597 // Find the eh.exception intrinsic if we didn't already.
598 if (!ExceptionIntrinsic)
599 ExceptionIntrinsic = Intrinsic::getDeclaration(F->getParent(),
600 Intrinsic::eh_exception);
603 return CallInst::Create(ExceptionIntrinsic, "eh.value.call", Start);
606 /// CreateValueLoad - Insert a load of the exception value stack variable
607 /// (creating it if necessary) at the start of the basic block (unless
608 /// there already is a load, in which case the existing load is returned).
609 Instruction *DwarfEHPrepare::CreateValueLoad(BasicBlock *BB) {
610 Instruction *Start = BB->getFirstNonPHI();
611 // Is this a load of the exception temporary?
612 if (ExceptionValueVar)
613 if (LoadInst* LI = dyn_cast<LoadInst>(Start))
614 if (LI->getPointerOperand() == ExceptionValueVar)
615 // Reuse the existing load.
618 // Create the temporary if we didn't already.
619 if (!ExceptionValueVar) {
620 ExceptionValueVar = new AllocaInst(PointerType::getUnqual(
621 Type::getInt8Ty(*Context)), "eh.value", F->begin()->begin());
622 ++NumStackTempsIntroduced;
626 return new LoadInst(ExceptionValueVar, "eh.value.load", Start);
629 bool DwarfEHPrepare::runOnFunction(Function &Fn) {
630 bool Changed = false;
632 // Initialize internal state.
633 DT = getAnalysisIfAvailable<DominatorTree>();
634 DF = getAnalysisIfAvailable<DominanceFrontier>();
635 ExceptionValueVar = 0;
638 Context = &Fn.getContext();
641 // Ensure that only unwind edges end at landing pads (a landing pad is a
642 // basic block where an invoke unwind edge ends).
643 Changed |= NormalizeLandingPads();
645 // Turn unwind instructions into libcalls.
646 Changed |= LowerUnwinds();
648 // Make sure that every call to eh.selector occurs in its own landing pad.
649 Changed |= MoveSelectorCalls();
651 // Remove useless catch-all clauses from the ends of selectors, or correct the
652 // selector result for the presence of the catch-all if it is really needed.
653 Changed |= RectifySelectorCalls();
655 // Make sure every landing pad has a selector in it.
656 Changed |= AddMissingSelectors();
658 // Move eh.exception calls to landing pads.
659 Changed |= MoveExceptionValueCalls();
661 // Initialize any stack temporaries we introduced.
662 Changed |= FinishStackTemporaries();
664 // Turn any stack temporaries into registers if possible.
665 //TODO if (!CompileFast)
666 //TODO Changed |= PromoteStackTemporaries();