blob: 2522d0b67e372670f6342bb8d9d41b0ce443aa34 [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"
Duncan Sandsb0f1e172009-05-22 20:36:31 +000028using namespace llvm;
29
Bill Wendlingf58898f2009-10-29 00:22:16 +000030STATISTIC(NumLandingPadsSplit, "Number of landing pads split");
Bill Wendlingf58898f2009-10-29 00:22:16 +000031STATISTIC(NumUnwindsLowered, "Number of unwind instructions lowered");
Bill Wendling8bedf972009-10-29 00:37:35 +000032STATISTIC(NumExceptionValuesMoved, "Number of eh.exception calls moved");
Eric Christopheradc581f2010-09-01 17:29:10 +000033STATISTIC(NumStackTempsIntroduced, "Number of stack temporaries introduced");
Duncan Sandsb0f1e172009-05-22 20:36:31 +000034
35namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000036 class DwarfEHPrepare : public FunctionPass {
Dan Gohman55e59c12010-04-19 19:05:59 +000037 const TargetMachine *TM;
Duncan Sandsb0f1e172009-05-22 20:36:31 +000038 const TargetLowering *TLI;
Duncan Sandsb0f1e172009-05-22 20:36:31 +000039
40 // The eh.exception intrinsic.
Bill Wendling8bedf972009-10-29 00:37:35 +000041 Function *ExceptionValueIntrinsic;
Duncan Sandsb0f1e172009-05-22 20:36:31 +000042
Bill Wendling2ad4aca2010-03-26 23:41:30 +000043 // The eh.selector intrinsic.
44 Function *SelectorIntrinsic;
45
46 // _Unwind_Resume_or_Rethrow call.
47 Constant *URoR;
48
49 // The EH language-specific catch-all type.
50 GlobalVariable *EHCatchAllValue;
51
Duncan Sandsb0f1e172009-05-22 20:36:31 +000052 // _Unwind_Resume or the target equivalent.
53 Constant *RewindFunction;
54
Duncan Sands22efc182010-08-31 09:05:06 +000055 // We both use and preserve dominator info.
Duncan Sandsb0f1e172009-05-22 20:36:31 +000056 DominatorTree *DT;
Duncan Sandsb0f1e172009-05-22 20:36:31 +000057
58 // The function we are running on.
59 Function *F;
60
61 // The landing pads for this function.
62 typedef SmallPtrSet<BasicBlock*, 8> BBSet;
63 BBSet LandingPads;
64
Eric Christopheradc581f2010-09-01 17:29:10 +000065 // Stack temporary used to hold eh.exception values.
66 AllocaInst *ExceptionValueVar;
67
Duncan Sandsb0f1e172009-05-22 20:36:31 +000068 bool NormalizeLandingPads();
69 bool LowerUnwinds();
70 bool MoveExceptionValueCalls();
Eric Christopheradc581f2010-09-01 17:29:10 +000071 bool FinishStackTemporaries();
Duncan Sandsb0f1e172009-05-22 20:36:31 +000072
73 Instruction *CreateExceptionValueCall(BasicBlock *BB);
Eric Christopheradc581f2010-09-01 17:29:10 +000074 Instruction *CreateValueLoad(BasicBlock *BB);
75
76 /// CreateReadOfExceptionValue - Return the result of the eh.exception
77 /// intrinsic by calling the intrinsic if in a landing pad, or loading it
78 /// from the exception value variable otherwise.
79 Instruction *CreateReadOfExceptionValue(BasicBlock *BB) {
80 return LandingPads.count(BB) ?
81 CreateExceptionValueCall(BB) : CreateValueLoad(BB);
82 }
Duncan Sandsb0f1e172009-05-22 20:36:31 +000083
Bill Wendlingbfbd8532010-03-27 01:19:12 +000084 /// CleanupSelectors - Any remaining eh.selector intrinsic calls which still
Bill Wendling23295cc2010-07-26 22:36:52 +000085 /// use the "llvm.eh.catch.all.value" call need to convert to using its
Bill Wendlingbfbd8532010-03-27 01:19:12 +000086 /// initializer instead.
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +000087 bool CleanupSelectors(SmallPtrSet<IntrinsicInst*, 32> &Sels);
Bill Wendlingbfbd8532010-03-27 01:19:12 +000088
Bill Wendlingefbf3062010-06-24 18:49:10 +000089 bool HasCatchAllInSelector(IntrinsicInst *);
Bill Wendling94366112010-06-12 02:34:29 +000090
Bill Wendlingbfde6442010-03-29 23:02:46 +000091 /// FindAllCleanupSelectors - Find all eh.selector calls that are clean-ups.
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +000092 void FindAllCleanupSelectors(SmallPtrSet<IntrinsicInst*, 32> &Sels,
93 SmallPtrSet<IntrinsicInst*, 32> &CatchAllSels);
Bill Wendlingbfde6442010-03-29 23:02:46 +000094
95 /// FindAllURoRInvokes - Find all URoR invokes in the function.
96 void FindAllURoRInvokes(SmallPtrSet<InvokeInst*, 32> &URoRInvokes);
97
Bill Wendling2ad4aca2010-03-26 23:41:30 +000098 /// HandleURoRInvokes - Handle invokes of "_Unwind_Resume_or_Rethrow"
99 /// calls. The "unwind" part of these invokes jump to a landing pad within
100 /// the current function. This is a candidate to merge the selector
101 /// associated with the URoR invoke with the one from the URoR's landing
102 /// pad.
103 bool HandleURoRInvokes();
104
Bill Wendling201f5f02010-03-29 23:37:07 +0000105 /// FindSelectorAndURoR - Find the eh.selector call and URoR call associated
106 /// with the eh.exception call. This recursively looks past instructions
107 /// which don't change the EH pointer value, like casts or PHI nodes.
108 bool FindSelectorAndURoR(Instruction *Inst, bool &URoRInvoke,
109 SmallPtrSet<IntrinsicInst*, 8> &SelCalls);
110
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000111 public:
112 static char ID; // Pass identification, replacement for typeid.
Duncan Sands22efc182010-08-31 09:05:06 +0000113 DwarfEHPrepare(const TargetMachine *tm) :
Owen Anderson90c579d2010-08-06 18:33:48 +0000114 FunctionPass(ID), TM(tm), TLI(TM->getTargetLowering()),
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000115 ExceptionValueIntrinsic(0), SelectorIntrinsic(0),
116 URoR(0), EHCatchAllValue(0), RewindFunction(0) {}
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000117
118 virtual bool runOnFunction(Function &Fn);
119
Eric Christopheradc581f2010-09-01 17:29:10 +0000120 // getAnalysisUsage - We need dominance frontiers for memory promotion.
Bill Wendling8bedf972009-10-29 00:37:35 +0000121 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Duncan Sands22efc182010-08-31 09:05:06 +0000122 AU.addRequired<DominatorTree>();
Bill Wendling8bedf972009-10-29 00:37:35 +0000123 AU.addPreserved<DominatorTree>();
Bill Wendling8bedf972009-10-29 00:37:35 +0000124 }
125
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000126 const char *getPassName() const {
127 return "Exception handling preparation";
128 }
129
130 };
131} // end anonymous namespace
132
133char DwarfEHPrepare::ID = 0;
134
Duncan Sands22efc182010-08-31 09:05:06 +0000135FunctionPass *llvm::createDwarfEHPass(const TargetMachine *tm) {
136 return new DwarfEHPrepare(tm);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000137}
138
Bill Wendlingefbf3062010-06-24 18:49:10 +0000139/// HasCatchAllInSelector - Return true if the intrinsic instruction has a
140/// catch-all.
141bool DwarfEHPrepare::HasCatchAllInSelector(IntrinsicInst *II) {
142 if (!EHCatchAllValue) return false;
Bill Wendling94366112010-06-12 02:34:29 +0000143
Gabor Greif2bf4b3b2010-06-25 11:25:30 +0000144 unsigned ArgIdx = II->getNumArgOperands() - 1;
145 GlobalVariable *GV = dyn_cast<GlobalVariable>(II->getArgOperand(ArgIdx));
Bill Wendlingefbf3062010-06-24 18:49:10 +0000146 return GV == EHCatchAllValue;
Bill Wendling94366112010-06-12 02:34:29 +0000147}
148
Bill Wendlingbfde6442010-03-29 23:02:46 +0000149/// FindAllCleanupSelectors - Find all eh.selector calls that are clean-ups.
150void DwarfEHPrepare::
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000151FindAllCleanupSelectors(SmallPtrSet<IntrinsicInst*, 32> &Sels,
152 SmallPtrSet<IntrinsicInst*, 32> &CatchAllSels) {
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000153 for (Value::use_iterator
Bill Wendlingbfde6442010-03-29 23:02:46 +0000154 I = SelectorIntrinsic->use_begin(),
155 E = SelectorIntrinsic->use_end(); I != E; ++I) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000156 IntrinsicInst *II = cast<IntrinsicInst>(*I);
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000157
Bill Wendling94366112010-06-12 02:34:29 +0000158 if (II->getParent()->getParent() != F)
159 continue;
Bill Wendlingbfde6442010-03-29 23:02:46 +0000160
Bill Wendlingefbf3062010-06-24 18:49:10 +0000161 if (!HasCatchAllInSelector(II))
Bill Wendling94366112010-06-12 02:34:29 +0000162 Sels.insert(II);
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000163 else
164 CatchAllSels.insert(II);
Bill Wendlingbfde6442010-03-29 23:02:46 +0000165 }
166}
167
168/// FindAllURoRInvokes - Find all URoR invokes in the function.
169void DwarfEHPrepare::
170FindAllURoRInvokes(SmallPtrSet<InvokeInst*, 32> &URoRInvokes) {
171 for (Value::use_iterator
172 I = URoR->use_begin(),
173 E = URoR->use_end(); I != E; ++I) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000174 if (InvokeInst *II = dyn_cast<InvokeInst>(*I))
Bill Wendlingbfde6442010-03-29 23:02:46 +0000175 URoRInvokes.insert(II);
176 }
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000177}
178
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000179/// CleanupSelectors - Any remaining eh.selector intrinsic calls which still use
Bill Wendling23295cc2010-07-26 22:36:52 +0000180/// the "llvm.eh.catch.all.value" call need to convert to using its
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000181/// initializer instead.
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000182bool DwarfEHPrepare::CleanupSelectors(SmallPtrSet<IntrinsicInst*, 32> &Sels) {
Bill Wendlingbfde6442010-03-29 23:02:46 +0000183 if (!EHCatchAllValue) return false;
184
185 if (!SelectorIntrinsic) {
186 SelectorIntrinsic =
187 Intrinsic::getDeclaration(F->getParent(), Intrinsic::eh_selector);
188 if (!SelectorIntrinsic) return false;
189 }
190
Bill Wendling43de15f2010-03-27 01:22:38 +0000191 bool Changed = false;
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000192 for (SmallPtrSet<IntrinsicInst*, 32>::iterator
193 I = Sels.begin(), E = Sels.end(); I != E; ++I) {
194 IntrinsicInst *Sel = *I;
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000195
Bill Wendling23295cc2010-07-26 22:36:52 +0000196 // Index of the "llvm.eh.catch.all.value" variable.
Gabor Greif9d677682010-06-29 13:03:46 +0000197 unsigned OpIdx = Sel->getNumArgOperands() - 1;
198 GlobalVariable *GV = dyn_cast<GlobalVariable>(Sel->getArgOperand(OpIdx));
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000199 if (GV != EHCatchAllValue) continue;
Gabor Greif9d677682010-06-29 13:03:46 +0000200 Sel->setArgOperand(OpIdx, EHCatchAllValue->getInitializer());
Bill Wendling43de15f2010-03-27 01:22:38 +0000201 Changed = true;
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000202 }
Bill Wendling43de15f2010-03-27 01:22:38 +0000203
204 return Changed;
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000205}
206
Bill Wendling201f5f02010-03-29 23:37:07 +0000207/// FindSelectorAndURoR - Find the eh.selector call associated with the
208/// eh.exception call. And indicate if there is a URoR "invoke" associated with
209/// the eh.exception call. This recursively looks past instructions which don't
210/// change the EH pointer value, like casts or PHI nodes.
211bool
212DwarfEHPrepare::FindSelectorAndURoR(Instruction *Inst, bool &URoRInvoke,
213 SmallPtrSet<IntrinsicInst*, 8> &SelCalls) {
214 SmallPtrSet<PHINode*, 32> SeenPHIs;
215 bool Changed = false;
216
Bill Wendling201f5f02010-03-29 23:37:07 +0000217 for (Value::use_iterator
218 I = Inst->use_begin(), E = Inst->use_end(); I != E; ++I) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000219 Instruction *II = dyn_cast<Instruction>(*I);
Bill Wendling201f5f02010-03-29 23:37:07 +0000220 if (!II || II->getParent()->getParent() != F) continue;
221
222 if (IntrinsicInst *Sel = dyn_cast<IntrinsicInst>(II)) {
223 if (Sel->getIntrinsicID() == Intrinsic::eh_selector)
224 SelCalls.insert(Sel);
225 } else if (InvokeInst *Invoke = dyn_cast<InvokeInst>(II)) {
226 if (Invoke->getCalledFunction() == URoR)
227 URoRInvoke = true;
228 } else if (CastInst *CI = dyn_cast<CastInst>(II)) {
229 Changed |= FindSelectorAndURoR(CI, URoRInvoke, SelCalls);
Bill Wendling201f5f02010-03-29 23:37:07 +0000230 } else if (PHINode *PN = dyn_cast<PHINode>(II)) {
231 if (SeenPHIs.insert(PN))
232 // Don't process a PHI node more than once.
233 Changed |= FindSelectorAndURoR(PN, URoRInvoke, SelCalls);
234 }
235 }
236
237 return Changed;
238}
239
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000240/// HandleURoRInvokes - Handle invokes of "_Unwind_Resume_or_Rethrow" calls. The
241/// "unwind" part of these invokes jump to a landing pad within the current
242/// function. This is a candidate to merge the selector associated with the URoR
243/// invoke with the one from the URoR's landing pad.
244bool DwarfEHPrepare::HandleURoRInvokes() {
245 if (!EHCatchAllValue) {
246 EHCatchAllValue =
Bill Wendling23295cc2010-07-26 22:36:52 +0000247 F->getParent()->getNamedGlobal("llvm.eh.catch.all.value");
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000248 if (!EHCatchAllValue) return false;
249 }
250
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000251 if (!SelectorIntrinsic) {
252 SelectorIntrinsic =
253 Intrinsic::getDeclaration(F->getParent(), Intrinsic::eh_selector);
254 if (!SelectorIntrinsic) return false;
255 }
256
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000257 SmallPtrSet<IntrinsicInst*, 32> Sels;
258 SmallPtrSet<IntrinsicInst*, 32> CatchAllSels;
259 FindAllCleanupSelectors(Sels, CatchAllSels);
260
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000261 if (!URoR) {
262 URoR = F->getParent()->getFunction("_Unwind_Resume_or_Rethrow");
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000263 if (!URoR) return CleanupSelectors(CatchAllSels);
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000264 }
265
Bill Wendlingbfde6442010-03-29 23:02:46 +0000266 SmallPtrSet<InvokeInst*, 32> URoRInvokes;
Bill Wendlingbfde6442010-03-29 23:02:46 +0000267 FindAllURoRInvokes(URoRInvokes);
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000268
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000269 SmallPtrSet<IntrinsicInst*, 32> SelsToConvert;
270
Bill Wendlingbfde6442010-03-29 23:02:46 +0000271 for (SmallPtrSet<IntrinsicInst*, 32>::iterator
272 SI = Sels.begin(), SE = Sels.end(); SI != SE; ++SI) {
273 const BasicBlock *SelBB = (*SI)->getParent();
274 for (SmallPtrSet<InvokeInst*, 32>::iterator
275 UI = URoRInvokes.begin(), UE = URoRInvokes.end(); UI != UE; ++UI) {
276 const BasicBlock *URoRBB = (*UI)->getParent();
Dan Gohmance0fe5d2010-07-26 17:38:15 +0000277 if (DT->dominates(SelBB, URoRBB)) {
Bill Wendlingbfde6442010-03-29 23:02:46 +0000278 SelsToConvert.insert(*SI);
279 break;
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000280 }
281 }
282 }
283
Bill Wendlingbfde6442010-03-29 23:02:46 +0000284 bool Changed = false;
285
Bill Wendling201f5f02010-03-29 23:37:07 +0000286 if (Sels.size() != SelsToConvert.size()) {
287 // If we haven't been able to convert all of the clean-up selectors, then
288 // loop through the slow way to see if they still need to be converted.
289 if (!ExceptionValueIntrinsic) {
290 ExceptionValueIntrinsic =
291 Intrinsic::getDeclaration(F->getParent(), Intrinsic::eh_exception);
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000292 if (!ExceptionValueIntrinsic)
293 return CleanupSelectors(CatchAllSels);
Bill Wendling201f5f02010-03-29 23:37:07 +0000294 }
295
296 for (Value::use_iterator
297 I = ExceptionValueIntrinsic->use_begin(),
298 E = ExceptionValueIntrinsic->use_end(); I != E; ++I) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000299 IntrinsicInst *EHPtr = dyn_cast<IntrinsicInst>(*I);
Bill Wendling201f5f02010-03-29 23:37:07 +0000300 if (!EHPtr || EHPtr->getParent()->getParent() != F) continue;
301
Bill Wendling201f5f02010-03-29 23:37:07 +0000302 bool URoRInvoke = false;
303 SmallPtrSet<IntrinsicInst*, 8> SelCalls;
304 Changed |= FindSelectorAndURoR(EHPtr, URoRInvoke, SelCalls);
305
306 if (URoRInvoke) {
307 // This EH pointer is being used by an invoke of an URoR instruction and
308 // an eh.selector intrinsic call. If the eh.selector is a 'clean-up', we
309 // need to convert it to a 'catch-all'.
310 for (SmallPtrSet<IntrinsicInst*, 8>::iterator
Bill Wendling94366112010-06-12 02:34:29 +0000311 SI = SelCalls.begin(), SE = SelCalls.end(); SI != SE; ++SI)
Bill Wendlingefbf3062010-06-24 18:49:10 +0000312 if (!HasCatchAllInSelector(*SI))
Bill Wendling94366112010-06-12 02:34:29 +0000313 SelsToConvert.insert(*SI);
Bill Wendling201f5f02010-03-29 23:37:07 +0000314 }
315 }
316 }
317
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000318 if (!SelsToConvert.empty()) {
319 // Convert all clean-up eh.selectors, which are associated with "invokes" of
320 // URoR calls, into catch-all eh.selectors.
321 Changed = true;
322
323 for (SmallPtrSet<IntrinsicInst*, 8>::iterator
324 SI = SelsToConvert.begin(), SE = SelsToConvert.end();
325 SI != SE; ++SI) {
326 IntrinsicInst *II = *SI;
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000327
328 // Use the exception object pointer and the personality function
329 // from the original selector.
Gabor Greif2bf4b3b2010-06-25 11:25:30 +0000330 CallSite CS(II);
331 IntrinsicInst::op_iterator I = CS.arg_begin();
Gabor Greif2bf4b3b2010-06-25 11:25:30 +0000332 IntrinsicInst::op_iterator E = CS.arg_end();
333 IntrinsicInst::op_iterator B = prior(E);
334
335 // Exclude last argument if it is an integer.
336 if (isa<ConstantInt>(B)) E = B;
Bill Wendling94366112010-06-12 02:34:29 +0000337
Gabor Greif32621ad2010-06-28 16:40:52 +0000338 // Add exception object pointer (front).
339 // Add personality function (next).
340 // Add in any filter IDs (rest).
341 SmallVector<Value*, 8> Args(I, E);
Bill Wendling94366112010-06-12 02:34:29 +0000342
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000343 Args.push_back(EHCatchAllValue->getInitializer()); // Catch-all indicator.
344
345 CallInst *NewSelector =
346 CallInst::Create(SelectorIntrinsic, Args.begin(), Args.end(),
347 "eh.sel.catch.all", II);
348
349 NewSelector->setTailCall(II->isTailCall());
350 NewSelector->setAttributes(II->getAttributes());
351 NewSelector->setCallingConv(II->getCallingConv());
352
353 II->replaceAllUsesWith(NewSelector);
354 II->eraseFromParent();
355 }
356 }
357
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000358 Changed |= CleanupSelectors(CatchAllSels);
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000359 return Changed;
360}
361
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000362/// NormalizeLandingPads - Normalize and discover landing pads, noting them
363/// in the LandingPads set. A landing pad is normal if the only CFG edges
Eric Christopherec26bf72009-09-15 21:56:46 +0000364/// that end at it are unwind edges from invoke instructions. If we inlined
365/// through an invoke we could have a normal branch from the previous
366/// unwind block through to the landing pad for the original invoke.
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000367/// Abnormal landing pads are fixed up by redirecting all unwind edges to
368/// a new basic block which falls through to the original.
369bool DwarfEHPrepare::NormalizeLandingPads() {
370 bool Changed = false;
371
Dan Gohman55e59c12010-04-19 19:05:59 +0000372 const MCAsmInfo *MAI = TM->getMCAsmInfo();
Jim Grosbach8d77cc82010-01-20 23:03:55 +0000373 bool usingSjLjEH = MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
374
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000375 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
376 TerminatorInst *TI = I->getTerminator();
377 if (!isa<InvokeInst>(TI))
378 continue;
379 BasicBlock *LPad = TI->getSuccessor(1);
380 // Skip landing pads that have already been normalized.
381 if (LandingPads.count(LPad))
382 continue;
383
384 // Check that only invoke unwind edges end at the landing pad.
385 bool OnlyUnwoundTo = true;
Jim Grosbach8d77cc82010-01-20 23:03:55 +0000386 bool SwitchOK = usingSjLjEH;
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000387 for (pred_iterator PI = pred_begin(LPad), PE = pred_end(LPad);
388 PI != PE; ++PI) {
389 TerminatorInst *PT = (*PI)->getTerminator();
Jim Grosbach8d77cc82010-01-20 23:03:55 +0000390 // The SjLj dispatch block uses a switch instruction. This is effectively
391 // an unwind edge, so we can disregard it here. There will only ever
392 // be one dispatch, however, so if there are multiple switches, one
393 // of them truly is a normal edge, not an unwind edge.
394 if (SwitchOK && isa<SwitchInst>(PT)) {
395 SwitchOK = false;
396 continue;
397 }
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000398 if (!isa<InvokeInst>(PT) || LPad == PT->getSuccessor(0)) {
399 OnlyUnwoundTo = false;
400 break;
401 }
402 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000403
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000404 if (OnlyUnwoundTo) {
405 // Only unwind edges lead to the landing pad. Remember the landing pad.
406 LandingPads.insert(LPad);
407 continue;
408 }
409
410 // At least one normal edge ends at the landing pad. Redirect the unwind
411 // edges to a new basic block which falls through into this one.
412
413 // Create the new basic block.
Bill Wendling8bedf972009-10-29 00:37:35 +0000414 BasicBlock *NewBB = BasicBlock::Create(F->getContext(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000415 LPad->getName() + "_unwind_edge");
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000416
417 // Insert it into the function right before the original landing pad.
418 LPad->getParent()->getBasicBlockList().insert(LPad, NewBB);
419
420 // Redirect unwind edges from the original landing pad to NewBB.
421 for (pred_iterator PI = pred_begin(LPad), PE = pred_end(LPad); PI != PE; ) {
422 TerminatorInst *PT = (*PI++)->getTerminator();
423 if (isa<InvokeInst>(PT) && PT->getSuccessor(1) == LPad)
424 // Unwind to the new block.
425 PT->setSuccessor(1, NewBB);
426 }
427
428 // If there are any PHI nodes in LPad, we need to update them so that they
429 // merge incoming values from NewBB instead.
430 for (BasicBlock::iterator II = LPad->begin(); isa<PHINode>(II); ++II) {
431 PHINode *PN = cast<PHINode>(II);
432 pred_iterator PB = pred_begin(NewBB), PE = pred_end(NewBB);
433
434 // Check to see if all of the values coming in via unwind edges are the
435 // same. If so, we don't need to create a new PHI node.
436 Value *InVal = PN->getIncomingValueForBlock(*PB);
437 for (pred_iterator PI = PB; PI != PE; ++PI) {
438 if (PI != PB && InVal != PN->getIncomingValueForBlock(*PI)) {
439 InVal = 0;
440 break;
441 }
442 }
443
444 if (InVal == 0) {
445 // Different unwind edges have different values. Create a new PHI node
446 // in NewBB.
447 PHINode *NewPN = PHINode::Create(PN->getType(), PN->getName()+".unwind",
448 NewBB);
449 // Add an entry for each unwind edge, using the value from the old PHI.
450 for (pred_iterator PI = PB; PI != PE; ++PI)
451 NewPN->addIncoming(PN->getIncomingValueForBlock(*PI), *PI);
452
453 // Now use this new PHI as the common incoming value for NewBB in PN.
454 InVal = NewPN;
455 }
456
457 // Revector exactly one entry in the PHI node to come from NewBB
458 // and delete all other entries that come from unwind edges. If
459 // there are both normal and unwind edges from the same predecessor,
460 // this leaves an entry for the normal edge.
461 for (pred_iterator PI = PB; PI != PE; ++PI)
462 PN->removeIncomingValue(*PI);
463 PN->addIncoming(InVal, NewBB);
464 }
465
466 // Add a fallthrough from NewBB to the original landing pad.
467 BranchInst::Create(LPad, NewBB);
468
Duncan Sands22efc182010-08-31 09:05:06 +0000469 // Now update DominatorTree analysis information.
470 DT->splitBlock(NewBB);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000471
472 // Remember the newly constructed landing pad. The original landing pad
473 // LPad is no longer a landing pad now that all unwind edges have been
474 // revectored to NewBB.
475 LandingPads.insert(NewBB);
476 ++NumLandingPadsSplit;
477 Changed = true;
478 }
479
480 return Changed;
481}
482
483/// LowerUnwinds - Turn unwind instructions into calls to _Unwind_Resume,
484/// rethrowing any previously caught exception. This will crash horribly
485/// at runtime if there is no such exception: using unwind to throw a new
486/// exception is currently not supported.
487bool DwarfEHPrepare::LowerUnwinds() {
Bill Wendling43488712009-09-14 20:52:37 +0000488 SmallVector<TerminatorInst*, 16> UnwindInsts;
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000489
490 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
491 TerminatorInst *TI = I->getTerminator();
Bill Wendling43488712009-09-14 20:52:37 +0000492 if (isa<UnwindInst>(TI))
493 UnwindInsts.push_back(TI);
494 }
495
496 if (UnwindInsts.empty()) return false;
497
498 // Find the rewind function if we didn't already.
499 if (!RewindFunction) {
Bill Wendling8bedf972009-10-29 00:37:35 +0000500 LLVMContext &Ctx = UnwindInsts[0]->getContext();
Bill Wendling43488712009-09-14 20:52:37 +0000501 std::vector<const Type*>
Bill Wendling8bedf972009-10-29 00:37:35 +0000502 Params(1, Type::getInt8PtrTy(Ctx));
503 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
Bill Wendling43488712009-09-14 20:52:37 +0000504 Params, false);
505 const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME);
506 RewindFunction = F->getParent()->getOrInsertFunction(RewindName, FTy);
507 }
508
Bill Wendling8bedf972009-10-29 00:37:35 +0000509 bool Changed = false;
510
Bill Wendling43488712009-09-14 20:52:37 +0000511 for (SmallVectorImpl<TerminatorInst*>::iterator
512 I = UnwindInsts.begin(), E = UnwindInsts.end(); I != E; ++I) {
513 TerminatorInst *TI = *I;
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000514
515 // Replace the unwind instruction with a call to _Unwind_Resume (or the
516 // appropriate target equivalent) followed by an UnreachableInst.
517
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000518 // Create the call...
Eric Christopher82f149d2009-09-04 01:14:14 +0000519 CallInst *CI = CallInst::Create(RewindFunction,
Eric Christopheradc581f2010-09-01 17:29:10 +0000520 CreateReadOfExceptionValue(TI->getParent()),
Bill Wendling43488712009-09-14 20:52:37 +0000521 "", TI);
Eric Christopher82f149d2009-09-04 01:14:14 +0000522 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000523 // ...followed by an UnreachableInst.
Bill Wendling8bedf972009-10-29 00:37:35 +0000524 new UnreachableInst(TI->getContext(), TI);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000525
526 // Nuke the unwind instruction.
527 TI->eraseFromParent();
528 ++NumUnwindsLowered;
529 Changed = true;
530 }
531
532 return Changed;
533}
534
535/// MoveExceptionValueCalls - Ensure that eh.exception is only ever called from
Eric Christopheradc581f2010-09-01 17:29:10 +0000536/// landing pads by replacing calls outside of landing pads with loads from a
537/// stack temporary. Move eh.exception calls inside landing pads to the start
538/// of the landing pad (optional, but may make things simpler for later passes).
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000539bool DwarfEHPrepare::MoveExceptionValueCalls() {
540 // If the eh.exception intrinsic is not declared in the module then there is
541 // nothing to do. Speed up compilation by checking for this common case.
Bill Wendling8bedf972009-10-29 00:37:35 +0000542 if (!ExceptionValueIntrinsic &&
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000543 !F->getParent()->getFunction(Intrinsic::getName(Intrinsic::eh_exception)))
544 return false;
545
546 bool Changed = false;
547
Eric Christopheradc581f2010-09-01 17:29:10 +0000548 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
549 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;)
550 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++))
551 if (CI->getIntrinsicID() == Intrinsic::eh_exception) {
552 if (!CI->use_empty()) {
553 Value *ExceptionValue = CreateReadOfExceptionValue(BB);
554 if (CI == ExceptionValue) {
555 // The call was at the start of a landing pad - leave it alone.
556 assert(LandingPads.count(BB) &&
557 "Created eh.exception call outside landing pad!");
558 continue;
559 }
560 CI->replaceAllUsesWith(ExceptionValue);
561 }
562 CI->eraseFromParent();
563 ++NumExceptionValuesMoved;
564 Changed = true;
Duncan Sandsfb4e8ab2010-09-01 14:07:47 +0000565 }
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000566 }
567
Eric Christopheradc581f2010-09-01 17:29:10 +0000568 return Changed;
569}
Duncan Sandsfb4e8ab2010-09-01 14:07:47 +0000570
Eric Christopheradc581f2010-09-01 17:29:10 +0000571/// FinishStackTemporaries - If we introduced a stack variable to hold the
572/// exception value then initialize it in each landing pad.
573bool DwarfEHPrepare::FinishStackTemporaries() {
574 if (!ExceptionValueVar)
575 // Nothing to do.
576 return false;
Duncan Sandsfb4e8ab2010-09-01 14:07:47 +0000577
Eric Christopheradc581f2010-09-01 17:29:10 +0000578 bool Changed = false;
Duncan Sandsfb4e8ab2010-09-01 14:07:47 +0000579
Eric Christopheradc581f2010-09-01 17:29:10 +0000580 // Make sure that there is a store of the exception value at the start of
581 // each landing pad.
Duncan Sandsfb4e8ab2010-09-01 14:07:47 +0000582 for (BBSet::iterator LI = LandingPads.begin(), LE = LandingPads.end();
Eric Christopheradc581f2010-09-01 17:29:10 +0000583 LI != LE; ++LI) {
584 Instruction *ExceptionValue = CreateReadOfExceptionValue(*LI);
585 Instruction *Store = new StoreInst(ExceptionValue, ExceptionValueVar);
586 Store->insertAfter(ExceptionValue);
587 Changed = true;
Duncan Sandsfb4e8ab2010-09-01 14:07:47 +0000588 }
589
Eric Christopheradc581f2010-09-01 17:29:10 +0000590 return Changed;
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000591}
592
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000593/// CreateExceptionValueCall - Insert a call to the eh.exception intrinsic at
594/// the start of the basic block (unless there already is one, in which case
595/// the existing call is returned).
596Instruction *DwarfEHPrepare::CreateExceptionValueCall(BasicBlock *BB) {
Dale Johannesen7249ef02010-04-02 21:49:27 +0000597 Instruction *Start = BB->getFirstNonPHIOrDbg();
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000598 // Is this a call to eh.exception?
599 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Start))
600 if (CI->getIntrinsicID() == Intrinsic::eh_exception)
601 // Reuse the existing call.
602 return Start;
603
604 // Find the eh.exception intrinsic if we didn't already.
Bill Wendling8bedf972009-10-29 00:37:35 +0000605 if (!ExceptionValueIntrinsic)
606 ExceptionValueIntrinsic = Intrinsic::getDeclaration(F->getParent(),
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000607 Intrinsic::eh_exception);
608
609 // Create the call.
Bill Wendling8bedf972009-10-29 00:37:35 +0000610 return CallInst::Create(ExceptionValueIntrinsic, "eh.value.call", Start);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000611}
612
Eric Christopheradc581f2010-09-01 17:29:10 +0000613/// CreateValueLoad - Insert a load of the exception value stack variable
614/// (creating it if necessary) at the start of the basic block (unless
615/// there already is a load, in which case the existing load is returned).
616Instruction *DwarfEHPrepare::CreateValueLoad(BasicBlock *BB) {
617 Instruction *Start = BB->getFirstNonPHIOrDbg();
618 // Is this a load of the exception temporary?
619 if (ExceptionValueVar)
620 if (LoadInst* LI = dyn_cast<LoadInst>(Start))
621 if (LI->getPointerOperand() == ExceptionValueVar)
622 // Reuse the existing load.
623 return Start;
624
625 // Create the temporary if we didn't already.
626 if (!ExceptionValueVar) {
627 ExceptionValueVar = new AllocaInst(PointerType::getUnqual(
628 Type::getInt8Ty(BB->getContext())), "eh.value", F->begin()->begin());
629 ++NumStackTempsIntroduced;
630 }
631
632 // Load the value.
633 return new LoadInst(ExceptionValueVar, "eh.value.load", Start);
634}
635
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000636bool DwarfEHPrepare::runOnFunction(Function &Fn) {
637 bool Changed = false;
638
639 // Initialize internal state.
Duncan Sands22efc182010-08-31 09:05:06 +0000640 DT = &getAnalysis<DominatorTree>();
Eric Christopheradc581f2010-09-01 17:29:10 +0000641 ExceptionValueVar = 0;
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000642 F = &Fn;
643
644 // Ensure that only unwind edges end at landing pads (a landing pad is a
645 // basic block where an invoke unwind edge ends).
646 Changed |= NormalizeLandingPads();
647
648 // Turn unwind instructions into libcalls.
649 Changed |= LowerUnwinds();
650
Bill Wendling8bedf972009-10-29 00:37:35 +0000651 // TODO: Move eh.selector calls to landing pads and combine them.
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000652
653 // Move eh.exception calls to landing pads.
654 Changed |= MoveExceptionValueCalls();
655
Eric Christopheradc581f2010-09-01 17:29:10 +0000656 // Initialize any stack temporaries we introduced.
657 Changed |= FinishStackTemporaries();
658
659 // TODO: Turn any stack temporaries into registers if possible.
660
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000661 Changed |= HandleURoRInvokes();
662
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000663 LandingPads.clear();
664
665 return Changed;
666}