blob: 007289ce7711dc54df57a7a3722962c1c860864d [file] [log] [blame]
Duncan Sandsb0f1e172009-05-22 20:36:31 +00001//===-- 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
Bill Wendling2ad4aca2010-03-26 23:41:30 +000011// generation. Required if using dwarf exception handling.
Duncan Sandsb0f1e172009-05-22 20:36:31 +000012//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "dwarfehprepare"
Duncan Sandsb0f1e172009-05-22 20:36:31 +000016#include "llvm/Function.h"
17#include "llvm/Instructions.h"
18#include "llvm/IntrinsicInst.h"
19#include "llvm/Module.h"
20#include "llvm/Pass.h"
Bill Wendling2ad4aca2010-03-26 23:41:30 +000021#include "llvm/ADT/Statistic.h"
22#include "llvm/Analysis/Dominators.h"
23#include "llvm/CodeGen/Passes.h"
Jim Grosbach8d77cc82010-01-20 23:03:55 +000024#include "llvm/MC/MCAsmInfo.h"
Gabor Greif2bf4b3b2010-06-25 11:25:30 +000025#include "llvm/Support/CallSite.h"
Duncan Sandsb0f1e172009-05-22 20:36:31 +000026#include "llvm/Target/TargetLowering.h"
27#include "llvm/Transforms/Utils/BasicBlockUtils.h"
28#include "llvm/Transforms/Utils/PromoteMemToReg.h"
29using namespace llvm;
30
Bill Wendlingf58898f2009-10-29 00:22:16 +000031STATISTIC(NumLandingPadsSplit, "Number of landing pads split");
Bill Wendlingf58898f2009-10-29 00:22:16 +000032STATISTIC(NumUnwindsLowered, "Number of unwind instructions lowered");
Bill Wendling8bedf972009-10-29 00:37:35 +000033STATISTIC(NumExceptionValuesMoved, "Number of eh.exception calls moved");
34STATISTIC(NumStackTempsIntroduced, "Number of stack temporaries introduced");
Duncan Sandsb0f1e172009-05-22 20:36:31 +000035
36namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000037 class DwarfEHPrepare : public FunctionPass {
Dan Gohman55e59c12010-04-19 19:05:59 +000038 const TargetMachine *TM;
Duncan Sandsb0f1e172009-05-22 20:36:31 +000039 const TargetLowering *TLI;
Bill Wendling8bedf972009-10-29 00:37:35 +000040 bool CompileFast;
Duncan Sandsb0f1e172009-05-22 20:36:31 +000041
42 // The eh.exception intrinsic.
Bill Wendling8bedf972009-10-29 00:37:35 +000043 Function *ExceptionValueIntrinsic;
Duncan Sandsb0f1e172009-05-22 20:36:31 +000044
Bill Wendling2ad4aca2010-03-26 23:41:30 +000045 // The eh.selector intrinsic.
46 Function *SelectorIntrinsic;
47
48 // _Unwind_Resume_or_Rethrow call.
49 Constant *URoR;
50
51 // The EH language-specific catch-all type.
52 GlobalVariable *EHCatchAllValue;
53
Duncan Sandsb0f1e172009-05-22 20:36:31 +000054 // _Unwind_Resume or the target equivalent.
55 Constant *RewindFunction;
56
57 // Dominator info is used when turning stack temporaries into registers.
58 DominatorTree *DT;
59 DominanceFrontier *DF;
60
61 // The function we are running on.
62 Function *F;
63
64 // The landing pads for this function.
65 typedef SmallPtrSet<BasicBlock*, 8> BBSet;
66 BBSet LandingPads;
67
68 // Stack temporary used to hold eh.exception values.
69 AllocaInst *ExceptionValueVar;
70
71 bool NormalizeLandingPads();
72 bool LowerUnwinds();
73 bool MoveExceptionValueCalls();
74 bool FinishStackTemporaries();
75 bool PromoteStackTemporaries();
76
77 Instruction *CreateExceptionValueCall(BasicBlock *BB);
78 Instruction *CreateValueLoad(BasicBlock *BB);
79
80 /// CreateReadOfExceptionValue - Return the result of the eh.exception
Bill Wendling2ad4aca2010-03-26 23:41:30 +000081 /// intrinsic by calling the intrinsic if in a landing pad, or loading it
82 /// from the exception value variable otherwise.
Duncan Sandsb0f1e172009-05-22 20:36:31 +000083 Instruction *CreateReadOfExceptionValue(BasicBlock *BB) {
84 return LandingPads.count(BB) ?
85 CreateExceptionValueCall(BB) : CreateValueLoad(BB);
86 }
87
Bill Wendlingbfbd8532010-03-27 01:19:12 +000088 /// CleanupSelectors - Any remaining eh.selector intrinsic calls which still
Gabor Greif3181c502010-04-15 15:14:46 +000089 /// use the ".llvm.eh.catch.all.value" call need to convert to using its
Bill Wendlingbfbd8532010-03-27 01:19:12 +000090 /// initializer instead.
Bill Wendling43de15f2010-03-27 01:22:38 +000091 bool CleanupSelectors();
Bill Wendlingbfbd8532010-03-27 01:19:12 +000092
Bill Wendlingefbf3062010-06-24 18:49:10 +000093 bool HasCatchAllInSelector(IntrinsicInst *);
Bill Wendling94366112010-06-12 02:34:29 +000094
Bill Wendlingbfde6442010-03-29 23:02:46 +000095 /// FindAllCleanupSelectors - Find all eh.selector calls that are clean-ups.
96 void FindAllCleanupSelectors(SmallPtrSet<IntrinsicInst*, 32> &Sels);
97
98 /// FindAllURoRInvokes - Find all URoR invokes in the function.
99 void FindAllURoRInvokes(SmallPtrSet<InvokeInst*, 32> &URoRInvokes);
100
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000101 /// HandleURoRInvokes - Handle invokes of "_Unwind_Resume_or_Rethrow"
102 /// calls. The "unwind" part of these invokes jump to a landing pad within
103 /// the current function. This is a candidate to merge the selector
104 /// associated with the URoR invoke with the one from the URoR's landing
105 /// pad.
106 bool HandleURoRInvokes();
107
Bill Wendling201f5f02010-03-29 23:37:07 +0000108 /// FindSelectorAndURoR - Find the eh.selector call and URoR call associated
109 /// with the eh.exception call. This recursively looks past instructions
110 /// which don't change the EH pointer value, like casts or PHI nodes.
111 bool FindSelectorAndURoR(Instruction *Inst, bool &URoRInvoke,
112 SmallPtrSet<IntrinsicInst*, 8> &SelCalls);
113
114 /// DoMem2RegPromotion - Take an alloca call and promote it from memory to a
115 /// register.
116 bool DoMem2RegPromotion(Value *V) {
117 AllocaInst *AI = dyn_cast<AllocaInst>(V);
118 if (!AI || !isAllocaPromotable(AI)) return false;
119
120 // Turn the alloca into a register.
121 std::vector<AllocaInst*> Allocas(1, AI);
122 PromoteMemToReg(Allocas, *DT, *DF);
123 return true;
124 }
125
126 /// PromoteStoreInst - Perform Mem2Reg on a StoreInst.
127 bool PromoteStoreInst(StoreInst *SI) {
128 if (!SI || !DT || !DF) return false;
129 if (DoMem2RegPromotion(SI->getOperand(1)))
130 return true;
131 return false;
132 }
133
134 /// PromoteEHPtrStore - Promote the storing of an EH pointer into a
135 /// register. This should get rid of the store and subsequent loads.
136 bool PromoteEHPtrStore(IntrinsicInst *II) {
137 if (!DT || !DF) return false;
138
139 bool Changed = false;
140 StoreInst *SI;
141
142 while (1) {
143 SI = 0;
144 for (Value::use_iterator
145 I = II->use_begin(), E = II->use_end(); I != E; ++I) {
146 SI = dyn_cast<StoreInst>(I);
147 if (SI) break;
148 }
149
150 if (!PromoteStoreInst(SI))
151 break;
152
153 Changed = true;
154 }
155
156 return false;
157 }
158
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000159 public:
160 static char ID; // Pass identification, replacement for typeid.
Dan Gohman55e59c12010-04-19 19:05:59 +0000161 DwarfEHPrepare(const TargetMachine *tm, bool fast) :
162 FunctionPass(&ID), TM(tm), TLI(TM->getTargetLowering()),
163 CompileFast(fast),
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000164 ExceptionValueIntrinsic(0), SelectorIntrinsic(0),
165 URoR(0), EHCatchAllValue(0), RewindFunction(0) {}
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000166
167 virtual bool runOnFunction(Function &Fn);
168
Bill Wendling8bedf972009-10-29 00:37:35 +0000169 // getAnalysisUsage - We need dominance frontiers for memory promotion.
170 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
171 if (!CompileFast)
172 AU.addRequired<DominatorTree>();
173 AU.addPreserved<DominatorTree>();
174 if (!CompileFast)
175 AU.addRequired<DominanceFrontier>();
176 AU.addPreserved<DominanceFrontier>();
177 }
178
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000179 const char *getPassName() const {
180 return "Exception handling preparation";
181 }
182
183 };
184} // end anonymous namespace
185
186char DwarfEHPrepare::ID = 0;
187
Dan Gohman55e59c12010-04-19 19:05:59 +0000188FunctionPass *llvm::createDwarfEHPass(const TargetMachine *tm, bool fast) {
189 return new DwarfEHPrepare(tm, fast);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000190}
191
Bill Wendlingefbf3062010-06-24 18:49:10 +0000192/// HasCatchAllInSelector - Return true if the intrinsic instruction has a
193/// catch-all.
194bool DwarfEHPrepare::HasCatchAllInSelector(IntrinsicInst *II) {
195 if (!EHCatchAllValue) return false;
Bill Wendling94366112010-06-12 02:34:29 +0000196
Gabor Greif2bf4b3b2010-06-25 11:25:30 +0000197 unsigned ArgIdx = II->getNumArgOperands() - 1;
198 GlobalVariable *GV = dyn_cast<GlobalVariable>(II->getArgOperand(ArgIdx));
Bill Wendlingefbf3062010-06-24 18:49:10 +0000199 return GV == EHCatchAllValue;
Bill Wendling94366112010-06-12 02:34:29 +0000200}
201
Bill Wendlingbfde6442010-03-29 23:02:46 +0000202/// FindAllCleanupSelectors - Find all eh.selector calls that are clean-ups.
203void DwarfEHPrepare::
204FindAllCleanupSelectors(SmallPtrSet<IntrinsicInst*, 32> &Sels) {
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000205 for (Value::use_iterator
Bill Wendlingbfde6442010-03-29 23:02:46 +0000206 I = SelectorIntrinsic->use_begin(),
207 E = SelectorIntrinsic->use_end(); I != E; ++I) {
Bill Wendling94366112010-06-12 02:34:29 +0000208 IntrinsicInst *II = cast<IntrinsicInst>(I);
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000209
Bill Wendling94366112010-06-12 02:34:29 +0000210 if (II->getParent()->getParent() != F)
211 continue;
Bill Wendlingbfde6442010-03-29 23:02:46 +0000212
Bill Wendlingefbf3062010-06-24 18:49:10 +0000213 if (!HasCatchAllInSelector(II))
Bill Wendling94366112010-06-12 02:34:29 +0000214 Sels.insert(II);
Bill Wendlingbfde6442010-03-29 23:02:46 +0000215 }
216}
217
218/// FindAllURoRInvokes - Find all URoR invokes in the function.
219void DwarfEHPrepare::
220FindAllURoRInvokes(SmallPtrSet<InvokeInst*, 32> &URoRInvokes) {
221 for (Value::use_iterator
222 I = URoR->use_begin(),
223 E = URoR->use_end(); I != E; ++I) {
224 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
225 URoRInvokes.insert(II);
226 }
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000227}
228
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000229/// CleanupSelectors - Any remaining eh.selector intrinsic calls which still use
Gabor Greif3181c502010-04-15 15:14:46 +0000230/// the ".llvm.eh.catch.all.value" call need to convert to using its
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000231/// initializer instead.
Bill Wendling43de15f2010-03-27 01:22:38 +0000232bool DwarfEHPrepare::CleanupSelectors() {
Bill Wendlingbfde6442010-03-29 23:02:46 +0000233 if (!EHCatchAllValue) return false;
234
235 if (!SelectorIntrinsic) {
236 SelectorIntrinsic =
237 Intrinsic::getDeclaration(F->getParent(), Intrinsic::eh_selector);
238 if (!SelectorIntrinsic) return false;
239 }
240
Bill Wendling43de15f2010-03-27 01:22:38 +0000241 bool Changed = false;
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000242 for (Value::use_iterator
243 I = SelectorIntrinsic->use_begin(),
244 E = SelectorIntrinsic->use_end(); I != E; ++I) {
245 IntrinsicInst *Sel = dyn_cast<IntrinsicInst>(I);
246 if (!Sel || Sel->getParent()->getParent() != F) continue;
247
248 // Index of the ".llvm.eh.catch.all.value" variable.
Eric Christopher551754c2010-04-16 23:37:20 +0000249 unsigned OpIdx = Sel->getNumOperands() - 1;
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000250 GlobalVariable *GV = dyn_cast<GlobalVariable>(Sel->getOperand(OpIdx));
251 if (GV != EHCatchAllValue) continue;
252 Sel->setOperand(OpIdx, EHCatchAllValue->getInitializer());
Bill Wendling43de15f2010-03-27 01:22:38 +0000253 Changed = true;
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000254 }
Bill Wendling43de15f2010-03-27 01:22:38 +0000255
256 return Changed;
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000257}
258
Bill Wendling201f5f02010-03-29 23:37:07 +0000259/// FindSelectorAndURoR - Find the eh.selector call associated with the
260/// eh.exception call. And indicate if there is a URoR "invoke" associated with
261/// the eh.exception call. This recursively looks past instructions which don't
262/// change the EH pointer value, like casts or PHI nodes.
263bool
264DwarfEHPrepare::FindSelectorAndURoR(Instruction *Inst, bool &URoRInvoke,
265 SmallPtrSet<IntrinsicInst*, 8> &SelCalls) {
266 SmallPtrSet<PHINode*, 32> SeenPHIs;
267 bool Changed = false;
268
269 restart:
270 for (Value::use_iterator
271 I = Inst->use_begin(), E = Inst->use_end(); I != E; ++I) {
272 Instruction *II = dyn_cast<Instruction>(I);
273 if (!II || II->getParent()->getParent() != F) continue;
274
275 if (IntrinsicInst *Sel = dyn_cast<IntrinsicInst>(II)) {
276 if (Sel->getIntrinsicID() == Intrinsic::eh_selector)
277 SelCalls.insert(Sel);
278 } else if (InvokeInst *Invoke = dyn_cast<InvokeInst>(II)) {
279 if (Invoke->getCalledFunction() == URoR)
280 URoRInvoke = true;
281 } else if (CastInst *CI = dyn_cast<CastInst>(II)) {
282 Changed |= FindSelectorAndURoR(CI, URoRInvoke, SelCalls);
283 } else if (StoreInst *SI = dyn_cast<StoreInst>(II)) {
284 if (!PromoteStoreInst(SI)) continue;
285 Changed = true;
286 SeenPHIs.clear();
287 goto restart; // Uses may have changed, restart loop.
288 } else if (PHINode *PN = dyn_cast<PHINode>(II)) {
289 if (SeenPHIs.insert(PN))
290 // Don't process a PHI node more than once.
291 Changed |= FindSelectorAndURoR(PN, URoRInvoke, SelCalls);
292 }
293 }
294
295 return Changed;
296}
297
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000298/// HandleURoRInvokes - Handle invokes of "_Unwind_Resume_or_Rethrow" calls. The
299/// "unwind" part of these invokes jump to a landing pad within the current
300/// function. This is a candidate to merge the selector associated with the URoR
301/// invoke with the one from the URoR's landing pad.
302bool DwarfEHPrepare::HandleURoRInvokes() {
Bill Wendlingbfde6442010-03-29 23:02:46 +0000303 if (!DT) return CleanupSelectors(); // We require DominatorTree information.
304
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000305 if (!EHCatchAllValue) {
306 EHCatchAllValue =
307 F->getParent()->getNamedGlobal(".llvm.eh.catch.all.value");
308 if (!EHCatchAllValue) return false;
309 }
310
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000311 if (!SelectorIntrinsic) {
312 SelectorIntrinsic =
313 Intrinsic::getDeclaration(F->getParent(), Intrinsic::eh_selector);
314 if (!SelectorIntrinsic) return false;
315 }
316
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000317 if (!URoR) {
318 URoR = F->getParent()->getFunction("_Unwind_Resume_or_Rethrow");
Bill Wendling43de15f2010-03-27 01:22:38 +0000319 if (!URoR) return CleanupSelectors();
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000320 }
321
Bill Wendlingbfde6442010-03-29 23:02:46 +0000322 SmallPtrSet<IntrinsicInst*, 32> Sels;
323 SmallPtrSet<InvokeInst*, 32> URoRInvokes;
324 FindAllCleanupSelectors(Sels);
325 FindAllURoRInvokes(URoRInvokes);
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000326
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000327 SmallPtrSet<IntrinsicInst*, 32> SelsToConvert;
328
Bill Wendlingbfde6442010-03-29 23:02:46 +0000329 for (SmallPtrSet<IntrinsicInst*, 32>::iterator
330 SI = Sels.begin(), SE = Sels.end(); SI != SE; ++SI) {
331 const BasicBlock *SelBB = (*SI)->getParent();
332 for (SmallPtrSet<InvokeInst*, 32>::iterator
333 UI = URoRInvokes.begin(), UE = URoRInvokes.end(); UI != UE; ++UI) {
334 const BasicBlock *URoRBB = (*UI)->getParent();
335 if (SelBB == URoRBB || DT->dominates(SelBB, URoRBB)) {
336 SelsToConvert.insert(*SI);
337 break;
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000338 }
339 }
340 }
341
Bill Wendlingbfde6442010-03-29 23:02:46 +0000342 bool Changed = false;
343
Bill Wendling201f5f02010-03-29 23:37:07 +0000344 if (Sels.size() != SelsToConvert.size()) {
345 // If we haven't been able to convert all of the clean-up selectors, then
346 // loop through the slow way to see if they still need to be converted.
347 if (!ExceptionValueIntrinsic) {
348 ExceptionValueIntrinsic =
349 Intrinsic::getDeclaration(F->getParent(), Intrinsic::eh_exception);
350 if (!ExceptionValueIntrinsic) return CleanupSelectors();
351 }
352
353 for (Value::use_iterator
354 I = ExceptionValueIntrinsic->use_begin(),
355 E = ExceptionValueIntrinsic->use_end(); I != E; ++I) {
356 IntrinsicInst *EHPtr = dyn_cast<IntrinsicInst>(I);
357 if (!EHPtr || EHPtr->getParent()->getParent() != F) continue;
358
359 Changed |= PromoteEHPtrStore(EHPtr);
360
361 bool URoRInvoke = false;
362 SmallPtrSet<IntrinsicInst*, 8> SelCalls;
363 Changed |= FindSelectorAndURoR(EHPtr, URoRInvoke, SelCalls);
364
365 if (URoRInvoke) {
366 // This EH pointer is being used by an invoke of an URoR instruction and
367 // an eh.selector intrinsic call. If the eh.selector is a 'clean-up', we
368 // need to convert it to a 'catch-all'.
369 for (SmallPtrSet<IntrinsicInst*, 8>::iterator
Bill Wendling94366112010-06-12 02:34:29 +0000370 SI = SelCalls.begin(), SE = SelCalls.end(); SI != SE; ++SI)
Bill Wendlingefbf3062010-06-24 18:49:10 +0000371 if (!HasCatchAllInSelector(*SI))
Bill Wendling94366112010-06-12 02:34:29 +0000372 SelsToConvert.insert(*SI);
Bill Wendling201f5f02010-03-29 23:37:07 +0000373 }
374 }
375 }
376
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000377 if (!SelsToConvert.empty()) {
378 // Convert all clean-up eh.selectors, which are associated with "invokes" of
379 // URoR calls, into catch-all eh.selectors.
380 Changed = true;
381
382 for (SmallPtrSet<IntrinsicInst*, 8>::iterator
383 SI = SelsToConvert.begin(), SE = SelsToConvert.end();
384 SI != SE; ++SI) {
385 IntrinsicInst *II = *SI;
386 SmallVector<Value*, 8> Args;
387
388 // Use the exception object pointer and the personality function
389 // from the original selector.
Gabor Greif2bf4b3b2010-06-25 11:25:30 +0000390 CallSite CS(II);
391 IntrinsicInst::op_iterator I = CS.arg_begin();
392 Args.push_back(*I++); // Exception object pointer.
393 Args.push_back(*I++); // Personality function.
Bill Wendling94366112010-06-12 02:34:29 +0000394
Gabor Greif2bf4b3b2010-06-25 11:25:30 +0000395 IntrinsicInst::op_iterator E = CS.arg_end();
396 IntrinsicInst::op_iterator B = prior(E);
397
398 // Exclude last argument if it is an integer.
399 if (isa<ConstantInt>(B)) E = B;
Bill Wendling94366112010-06-12 02:34:29 +0000400
401 // Add in any filter IDs.
Gabor Greif2bf4b3b2010-06-25 11:25:30 +0000402 for (; I != E; ++I)
403 Args.push_back(*I);
Bill Wendling94366112010-06-12 02:34:29 +0000404
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000405 Args.push_back(EHCatchAllValue->getInitializer()); // Catch-all indicator.
406
407 CallInst *NewSelector =
408 CallInst::Create(SelectorIntrinsic, Args.begin(), Args.end(),
409 "eh.sel.catch.all", II);
410
411 NewSelector->setTailCall(II->isTailCall());
412 NewSelector->setAttributes(II->getAttributes());
413 NewSelector->setCallingConv(II->getCallingConv());
414
415 II->replaceAllUsesWith(NewSelector);
416 II->eraseFromParent();
417 }
418 }
419
Bill Wendling43de15f2010-03-27 01:22:38 +0000420 Changed |= CleanupSelectors();
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000421 return Changed;
422}
423
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000424/// NormalizeLandingPads - Normalize and discover landing pads, noting them
425/// in the LandingPads set. A landing pad is normal if the only CFG edges
Eric Christopherec26bf72009-09-15 21:56:46 +0000426/// that end at it are unwind edges from invoke instructions. If we inlined
427/// through an invoke we could have a normal branch from the previous
428/// unwind block through to the landing pad for the original invoke.
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000429/// Abnormal landing pads are fixed up by redirecting all unwind edges to
430/// a new basic block which falls through to the original.
431bool DwarfEHPrepare::NormalizeLandingPads() {
432 bool Changed = false;
433
Dan Gohman55e59c12010-04-19 19:05:59 +0000434 const MCAsmInfo *MAI = TM->getMCAsmInfo();
Jim Grosbach8d77cc82010-01-20 23:03:55 +0000435 bool usingSjLjEH = MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
436
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000437 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
438 TerminatorInst *TI = I->getTerminator();
439 if (!isa<InvokeInst>(TI))
440 continue;
441 BasicBlock *LPad = TI->getSuccessor(1);
442 // Skip landing pads that have already been normalized.
443 if (LandingPads.count(LPad))
444 continue;
445
446 // Check that only invoke unwind edges end at the landing pad.
447 bool OnlyUnwoundTo = true;
Jim Grosbach8d77cc82010-01-20 23:03:55 +0000448 bool SwitchOK = usingSjLjEH;
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000449 for (pred_iterator PI = pred_begin(LPad), PE = pred_end(LPad);
450 PI != PE; ++PI) {
451 TerminatorInst *PT = (*PI)->getTerminator();
Jim Grosbach8d77cc82010-01-20 23:03:55 +0000452 // The SjLj dispatch block uses a switch instruction. This is effectively
453 // an unwind edge, so we can disregard it here. There will only ever
454 // be one dispatch, however, so if there are multiple switches, one
455 // of them truly is a normal edge, not an unwind edge.
456 if (SwitchOK && isa<SwitchInst>(PT)) {
457 SwitchOK = false;
458 continue;
459 }
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000460 if (!isa<InvokeInst>(PT) || LPad == PT->getSuccessor(0)) {
461 OnlyUnwoundTo = false;
462 break;
463 }
464 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000465
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000466 if (OnlyUnwoundTo) {
467 // Only unwind edges lead to the landing pad. Remember the landing pad.
468 LandingPads.insert(LPad);
469 continue;
470 }
471
472 // At least one normal edge ends at the landing pad. Redirect the unwind
473 // edges to a new basic block which falls through into this one.
474
475 // Create the new basic block.
Bill Wendling8bedf972009-10-29 00:37:35 +0000476 BasicBlock *NewBB = BasicBlock::Create(F->getContext(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000477 LPad->getName() + "_unwind_edge");
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000478
479 // Insert it into the function right before the original landing pad.
480 LPad->getParent()->getBasicBlockList().insert(LPad, NewBB);
481
482 // Redirect unwind edges from the original landing pad to NewBB.
483 for (pred_iterator PI = pred_begin(LPad), PE = pred_end(LPad); PI != PE; ) {
484 TerminatorInst *PT = (*PI++)->getTerminator();
485 if (isa<InvokeInst>(PT) && PT->getSuccessor(1) == LPad)
486 // Unwind to the new block.
487 PT->setSuccessor(1, NewBB);
488 }
489
490 // If there are any PHI nodes in LPad, we need to update them so that they
491 // merge incoming values from NewBB instead.
492 for (BasicBlock::iterator II = LPad->begin(); isa<PHINode>(II); ++II) {
493 PHINode *PN = cast<PHINode>(II);
494 pred_iterator PB = pred_begin(NewBB), PE = pred_end(NewBB);
495
496 // Check to see if all of the values coming in via unwind edges are the
497 // same. If so, we don't need to create a new PHI node.
498 Value *InVal = PN->getIncomingValueForBlock(*PB);
499 for (pred_iterator PI = PB; PI != PE; ++PI) {
500 if (PI != PB && InVal != PN->getIncomingValueForBlock(*PI)) {
501 InVal = 0;
502 break;
503 }
504 }
505
506 if (InVal == 0) {
507 // Different unwind edges have different values. Create a new PHI node
508 // in NewBB.
509 PHINode *NewPN = PHINode::Create(PN->getType(), PN->getName()+".unwind",
510 NewBB);
511 // Add an entry for each unwind edge, using the value from the old PHI.
512 for (pred_iterator PI = PB; PI != PE; ++PI)
513 NewPN->addIncoming(PN->getIncomingValueForBlock(*PI), *PI);
514
515 // Now use this new PHI as the common incoming value for NewBB in PN.
516 InVal = NewPN;
517 }
518
519 // Revector exactly one entry in the PHI node to come from NewBB
520 // and delete all other entries that come from unwind edges. If
521 // there are both normal and unwind edges from the same predecessor,
522 // this leaves an entry for the normal edge.
523 for (pred_iterator PI = PB; PI != PE; ++PI)
524 PN->removeIncomingValue(*PI);
525 PN->addIncoming(InVal, NewBB);
526 }
527
528 // Add a fallthrough from NewBB to the original landing pad.
529 BranchInst::Create(LPad, NewBB);
530
531 // Now update DominatorTree and DominanceFrontier analysis information.
532 if (DT)
533 DT->splitBlock(NewBB);
534 if (DF)
535 DF->splitBlock(NewBB);
536
537 // Remember the newly constructed landing pad. The original landing pad
538 // LPad is no longer a landing pad now that all unwind edges have been
539 // revectored to NewBB.
540 LandingPads.insert(NewBB);
541 ++NumLandingPadsSplit;
542 Changed = true;
543 }
544
545 return Changed;
546}
547
548/// LowerUnwinds - Turn unwind instructions into calls to _Unwind_Resume,
549/// rethrowing any previously caught exception. This will crash horribly
550/// at runtime if there is no such exception: using unwind to throw a new
551/// exception is currently not supported.
552bool DwarfEHPrepare::LowerUnwinds() {
Bill Wendling43488712009-09-14 20:52:37 +0000553 SmallVector<TerminatorInst*, 16> UnwindInsts;
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000554
555 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
556 TerminatorInst *TI = I->getTerminator();
Bill Wendling43488712009-09-14 20:52:37 +0000557 if (isa<UnwindInst>(TI))
558 UnwindInsts.push_back(TI);
559 }
560
561 if (UnwindInsts.empty()) return false;
562
563 // Find the rewind function if we didn't already.
564 if (!RewindFunction) {
Bill Wendling8bedf972009-10-29 00:37:35 +0000565 LLVMContext &Ctx = UnwindInsts[0]->getContext();
Bill Wendling43488712009-09-14 20:52:37 +0000566 std::vector<const Type*>
Bill Wendling8bedf972009-10-29 00:37:35 +0000567 Params(1, Type::getInt8PtrTy(Ctx));
568 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
Bill Wendling43488712009-09-14 20:52:37 +0000569 Params, false);
570 const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME);
571 RewindFunction = F->getParent()->getOrInsertFunction(RewindName, FTy);
572 }
573
Bill Wendling8bedf972009-10-29 00:37:35 +0000574 bool Changed = false;
575
Bill Wendling43488712009-09-14 20:52:37 +0000576 for (SmallVectorImpl<TerminatorInst*>::iterator
577 I = UnwindInsts.begin(), E = UnwindInsts.end(); I != E; ++I) {
578 TerminatorInst *TI = *I;
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000579
580 // Replace the unwind instruction with a call to _Unwind_Resume (or the
581 // appropriate target equivalent) followed by an UnreachableInst.
582
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000583 // Create the call...
Eric Christopher82f149d2009-09-04 01:14:14 +0000584 CallInst *CI = CallInst::Create(RewindFunction,
Daniel Dunbara279bc32009-09-20 02:20:51 +0000585 CreateReadOfExceptionValue(TI->getParent()),
Bill Wendling43488712009-09-14 20:52:37 +0000586 "", TI);
Eric Christopher82f149d2009-09-04 01:14:14 +0000587 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000588 // ...followed by an UnreachableInst.
Bill Wendling8bedf972009-10-29 00:37:35 +0000589 new UnreachableInst(TI->getContext(), TI);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000590
591 // Nuke the unwind instruction.
592 TI->eraseFromParent();
593 ++NumUnwindsLowered;
594 Changed = true;
595 }
596
597 return Changed;
598}
599
600/// MoveExceptionValueCalls - Ensure that eh.exception is only ever called from
601/// landing pads by replacing calls outside of landing pads with loads from a
602/// stack temporary. Move eh.exception calls inside landing pads to the start
603/// of the landing pad (optional, but may make things simpler for later passes).
604bool DwarfEHPrepare::MoveExceptionValueCalls() {
605 // If the eh.exception intrinsic is not declared in the module then there is
606 // nothing to do. Speed up compilation by checking for this common case.
Bill Wendling8bedf972009-10-29 00:37:35 +0000607 if (!ExceptionValueIntrinsic &&
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000608 !F->getParent()->getFunction(Intrinsic::getName(Intrinsic::eh_exception)))
609 return false;
610
611 bool Changed = false;
612
613 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
614 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;)
615 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++))
616 if (CI->getIntrinsicID() == Intrinsic::eh_exception) {
617 if (!CI->use_empty()) {
618 Value *ExceptionValue = CreateReadOfExceptionValue(BB);
619 if (CI == ExceptionValue) {
620 // The call was at the start of a landing pad - leave it alone.
621 assert(LandingPads.count(BB) &&
622 "Created eh.exception call outside landing pad!");
623 continue;
624 }
625 CI->replaceAllUsesWith(ExceptionValue);
626 }
627 CI->eraseFromParent();
628 ++NumExceptionValuesMoved;
629 Changed = true;
630 }
631 }
632
633 return Changed;
634}
635
636/// FinishStackTemporaries - If we introduced a stack variable to hold the
637/// exception value then initialize it in each landing pad.
638bool DwarfEHPrepare::FinishStackTemporaries() {
639 if (!ExceptionValueVar)
640 // Nothing to do.
641 return false;
642
643 bool Changed = false;
644
645 // Make sure that there is a store of the exception value at the start of
646 // each landing pad.
647 for (BBSet::iterator LI = LandingPads.begin(), LE = LandingPads.end();
648 LI != LE; ++LI) {
649 Instruction *ExceptionValue = CreateReadOfExceptionValue(*LI);
650 Instruction *Store = new StoreInst(ExceptionValue, ExceptionValueVar);
651 Store->insertAfter(ExceptionValue);
652 Changed = true;
653 }
654
655 return Changed;
656}
657
658/// PromoteStackTemporaries - Turn any stack temporaries we introduced into
659/// registers if possible.
660bool DwarfEHPrepare::PromoteStackTemporaries() {
661 if (ExceptionValueVar && DT && DF && isAllocaPromotable(ExceptionValueVar)) {
662 // Turn the exception temporary into registers and phi nodes if possible.
663 std::vector<AllocaInst*> Allocas(1, ExceptionValueVar);
Nick Lewyckyce2c51b2009-11-23 03:50:44 +0000664 PromoteMemToReg(Allocas, *DT, *DF);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000665 return true;
666 }
667 return false;
668}
669
670/// CreateExceptionValueCall - Insert a call to the eh.exception intrinsic at
671/// the start of the basic block (unless there already is one, in which case
672/// the existing call is returned).
673Instruction *DwarfEHPrepare::CreateExceptionValueCall(BasicBlock *BB) {
Dale Johannesen7249ef02010-04-02 21:49:27 +0000674 Instruction *Start = BB->getFirstNonPHIOrDbg();
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000675 // Is this a call to eh.exception?
676 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Start))
677 if (CI->getIntrinsicID() == Intrinsic::eh_exception)
678 // Reuse the existing call.
679 return Start;
680
681 // Find the eh.exception intrinsic if we didn't already.
Bill Wendling8bedf972009-10-29 00:37:35 +0000682 if (!ExceptionValueIntrinsic)
683 ExceptionValueIntrinsic = Intrinsic::getDeclaration(F->getParent(),
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000684 Intrinsic::eh_exception);
685
686 // Create the call.
Bill Wendling8bedf972009-10-29 00:37:35 +0000687 return CallInst::Create(ExceptionValueIntrinsic, "eh.value.call", Start);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000688}
689
690/// CreateValueLoad - Insert a load of the exception value stack variable
691/// (creating it if necessary) at the start of the basic block (unless
692/// there already is a load, in which case the existing load is returned).
693Instruction *DwarfEHPrepare::CreateValueLoad(BasicBlock *BB) {
Dale Johannesen7249ef02010-04-02 21:49:27 +0000694 Instruction *Start = BB->getFirstNonPHIOrDbg();
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000695 // Is this a load of the exception temporary?
696 if (ExceptionValueVar)
697 if (LoadInst* LI = dyn_cast<LoadInst>(Start))
698 if (LI->getPointerOperand() == ExceptionValueVar)
699 // Reuse the existing load.
700 return Start;
701
702 // Create the temporary if we didn't already.
703 if (!ExceptionValueVar) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000704 ExceptionValueVar = new AllocaInst(PointerType::getUnqual(
Bill Wendling8bedf972009-10-29 00:37:35 +0000705 Type::getInt8Ty(BB->getContext())), "eh.value", F->begin()->begin());
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000706 ++NumStackTempsIntroduced;
707 }
708
709 // Load the value.
710 return new LoadInst(ExceptionValueVar, "eh.value.load", Start);
711}
712
713bool DwarfEHPrepare::runOnFunction(Function &Fn) {
714 bool Changed = false;
715
716 // Initialize internal state.
717 DT = getAnalysisIfAvailable<DominatorTree>();
718 DF = getAnalysisIfAvailable<DominanceFrontier>();
719 ExceptionValueVar = 0;
720 F = &Fn;
721
722 // Ensure that only unwind edges end at landing pads (a landing pad is a
723 // basic block where an invoke unwind edge ends).
724 Changed |= NormalizeLandingPads();
725
726 // Turn unwind instructions into libcalls.
727 Changed |= LowerUnwinds();
728
Bill Wendling8bedf972009-10-29 00:37:35 +0000729 // TODO: Move eh.selector calls to landing pads and combine them.
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000730
731 // Move eh.exception calls to landing pads.
732 Changed |= MoveExceptionValueCalls();
733
734 // Initialize any stack temporaries we introduced.
735 Changed |= FinishStackTemporaries();
736
737 // Turn any stack temporaries into registers if possible.
Bill Wendling8bedf972009-10-29 00:37:35 +0000738 if (!CompileFast)
739 Changed |= PromoteStackTemporaries();
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000740
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000741 Changed |= HandleURoRInvokes();
742
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000743 LandingPads.clear();
744
745 return Changed;
746}