blob: f43136a2894e18c9d724fcce4491f6238ae330fb [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 Sands850fcd42010-09-03 08:31:48 +000028#include "llvm/Transforms/Utils/SSAUpdater.h"
Duncan Sandsb0f1e172009-05-22 20:36:31 +000029using 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");
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
Duncan Sandsb0f1e172009-05-22 20:36:31 +000065 bool NormalizeLandingPads();
66 bool LowerUnwinds();
67 bool MoveExceptionValueCalls();
Duncan Sandsb0f1e172009-05-22 20:36:31 +000068
69 Instruction *CreateExceptionValueCall(BasicBlock *BB);
Duncan Sandsb0f1e172009-05-22 20:36:31 +000070
Bill Wendlingbfbd8532010-03-27 01:19:12 +000071 /// CleanupSelectors - Any remaining eh.selector intrinsic calls which still
Bill Wendling23295cc2010-07-26 22:36:52 +000072 /// use the "llvm.eh.catch.all.value" call need to convert to using its
Bill Wendlingbfbd8532010-03-27 01:19:12 +000073 /// initializer instead.
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +000074 bool CleanupSelectors(SmallPtrSet<IntrinsicInst*, 32> &Sels);
Bill Wendlingbfbd8532010-03-27 01:19:12 +000075
Bill Wendlingefbf3062010-06-24 18:49:10 +000076 bool HasCatchAllInSelector(IntrinsicInst *);
Bill Wendling94366112010-06-12 02:34:29 +000077
Bill Wendlingbfde6442010-03-29 23:02:46 +000078 /// FindAllCleanupSelectors - Find all eh.selector calls that are clean-ups.
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +000079 void FindAllCleanupSelectors(SmallPtrSet<IntrinsicInst*, 32> &Sels,
80 SmallPtrSet<IntrinsicInst*, 32> &CatchAllSels);
Bill Wendlingbfde6442010-03-29 23:02:46 +000081
82 /// FindAllURoRInvokes - Find all URoR invokes in the function.
83 void FindAllURoRInvokes(SmallPtrSet<InvokeInst*, 32> &URoRInvokes);
84
Bill Wendling2ad4aca2010-03-26 23:41:30 +000085 /// HandleURoRInvokes - Handle invokes of "_Unwind_Resume_or_Rethrow"
86 /// calls. The "unwind" part of these invokes jump to a landing pad within
87 /// the current function. This is a candidate to merge the selector
88 /// associated with the URoR invoke with the one from the URoR's landing
89 /// pad.
90 bool HandleURoRInvokes();
91
Bill Wendling201f5f02010-03-29 23:37:07 +000092 /// FindSelectorAndURoR - Find the eh.selector call and URoR call associated
93 /// with the eh.exception call. This recursively looks past instructions
94 /// which don't change the EH pointer value, like casts or PHI nodes.
95 bool FindSelectorAndURoR(Instruction *Inst, bool &URoRInvoke,
96 SmallPtrSet<IntrinsicInst*, 8> &SelCalls);
97
Duncan Sandsb0f1e172009-05-22 20:36:31 +000098 public:
99 static char ID; // Pass identification, replacement for typeid.
Duncan Sands22efc182010-08-31 09:05:06 +0000100 DwarfEHPrepare(const TargetMachine *tm) :
Owen Anderson90c579d2010-08-06 18:33:48 +0000101 FunctionPass(ID), TM(tm), TLI(TM->getTargetLowering()),
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000102 ExceptionValueIntrinsic(0), SelectorIntrinsic(0),
Owen Anderson081c34b2010-10-19 17:21:58 +0000103 URoR(0), EHCatchAllValue(0), RewindFunction(0) {
104 initializeDominatorTreePass(*PassRegistry::getPassRegistry());
105 }
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000106
107 virtual bool runOnFunction(Function &Fn);
108
Duncan Sands850fcd42010-09-03 08:31:48 +0000109 // getAnalysisUsage - We need the dominator tree for handling URoR.
Bill Wendling8bedf972009-10-29 00:37:35 +0000110 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Duncan Sands22efc182010-08-31 09:05:06 +0000111 AU.addRequired<DominatorTree>();
Bill Wendling8bedf972009-10-29 00:37:35 +0000112 AU.addPreserved<DominatorTree>();
Bill Wendling8bedf972009-10-29 00:37:35 +0000113 }
114
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000115 const char *getPassName() const {
116 return "Exception handling preparation";
117 }
118
119 };
120} // end anonymous namespace
121
122char DwarfEHPrepare::ID = 0;
123
Duncan Sands22efc182010-08-31 09:05:06 +0000124FunctionPass *llvm::createDwarfEHPass(const TargetMachine *tm) {
125 return new DwarfEHPrepare(tm);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000126}
127
Bill Wendlingefbf3062010-06-24 18:49:10 +0000128/// HasCatchAllInSelector - Return true if the intrinsic instruction has a
129/// catch-all.
130bool DwarfEHPrepare::HasCatchAllInSelector(IntrinsicInst *II) {
131 if (!EHCatchAllValue) return false;
Bill Wendling94366112010-06-12 02:34:29 +0000132
Gabor Greif2bf4b3b2010-06-25 11:25:30 +0000133 unsigned ArgIdx = II->getNumArgOperands() - 1;
134 GlobalVariable *GV = dyn_cast<GlobalVariable>(II->getArgOperand(ArgIdx));
Bill Wendlingefbf3062010-06-24 18:49:10 +0000135 return GV == EHCatchAllValue;
Bill Wendling94366112010-06-12 02:34:29 +0000136}
137
Bill Wendlingbfde6442010-03-29 23:02:46 +0000138/// FindAllCleanupSelectors - Find all eh.selector calls that are clean-ups.
139void DwarfEHPrepare::
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000140FindAllCleanupSelectors(SmallPtrSet<IntrinsicInst*, 32> &Sels,
141 SmallPtrSet<IntrinsicInst*, 32> &CatchAllSels) {
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000142 for (Value::use_iterator
Bill Wendlingbfde6442010-03-29 23:02:46 +0000143 I = SelectorIntrinsic->use_begin(),
144 E = SelectorIntrinsic->use_end(); I != E; ++I) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000145 IntrinsicInst *II = cast<IntrinsicInst>(*I);
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000146
Bill Wendling94366112010-06-12 02:34:29 +0000147 if (II->getParent()->getParent() != F)
148 continue;
Bill Wendlingbfde6442010-03-29 23:02:46 +0000149
Bill Wendlingefbf3062010-06-24 18:49:10 +0000150 if (!HasCatchAllInSelector(II))
Bill Wendling94366112010-06-12 02:34:29 +0000151 Sels.insert(II);
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000152 else
153 CatchAllSels.insert(II);
Bill Wendlingbfde6442010-03-29 23:02:46 +0000154 }
155}
156
157/// FindAllURoRInvokes - Find all URoR invokes in the function.
158void DwarfEHPrepare::
159FindAllURoRInvokes(SmallPtrSet<InvokeInst*, 32> &URoRInvokes) {
160 for (Value::use_iterator
161 I = URoR->use_begin(),
162 E = URoR->use_end(); I != E; ++I) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000163 if (InvokeInst *II = dyn_cast<InvokeInst>(*I))
Bill Wendlingbfde6442010-03-29 23:02:46 +0000164 URoRInvokes.insert(II);
165 }
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000166}
167
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000168/// CleanupSelectors - Any remaining eh.selector intrinsic calls which still use
Bill Wendling23295cc2010-07-26 22:36:52 +0000169/// the "llvm.eh.catch.all.value" call need to convert to using its
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000170/// initializer instead.
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000171bool DwarfEHPrepare::CleanupSelectors(SmallPtrSet<IntrinsicInst*, 32> &Sels) {
Bill Wendlingbfde6442010-03-29 23:02:46 +0000172 if (!EHCatchAllValue) return false;
173
174 if (!SelectorIntrinsic) {
175 SelectorIntrinsic =
176 Intrinsic::getDeclaration(F->getParent(), Intrinsic::eh_selector);
177 if (!SelectorIntrinsic) return false;
178 }
179
Bill Wendling43de15f2010-03-27 01:22:38 +0000180 bool Changed = false;
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000181 for (SmallPtrSet<IntrinsicInst*, 32>::iterator
182 I = Sels.begin(), E = Sels.end(); I != E; ++I) {
183 IntrinsicInst *Sel = *I;
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000184
Bill Wendling23295cc2010-07-26 22:36:52 +0000185 // Index of the "llvm.eh.catch.all.value" variable.
Gabor Greif9d677682010-06-29 13:03:46 +0000186 unsigned OpIdx = Sel->getNumArgOperands() - 1;
187 GlobalVariable *GV = dyn_cast<GlobalVariable>(Sel->getArgOperand(OpIdx));
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000188 if (GV != EHCatchAllValue) continue;
Gabor Greif9d677682010-06-29 13:03:46 +0000189 Sel->setArgOperand(OpIdx, EHCatchAllValue->getInitializer());
Bill Wendling43de15f2010-03-27 01:22:38 +0000190 Changed = true;
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000191 }
Bill Wendling43de15f2010-03-27 01:22:38 +0000192
193 return Changed;
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000194}
195
Bill Wendling201f5f02010-03-29 23:37:07 +0000196/// FindSelectorAndURoR - Find the eh.selector call associated with the
197/// eh.exception call. And indicate if there is a URoR "invoke" associated with
198/// the eh.exception call. This recursively looks past instructions which don't
199/// change the EH pointer value, like casts or PHI nodes.
200bool
201DwarfEHPrepare::FindSelectorAndURoR(Instruction *Inst, bool &URoRInvoke,
202 SmallPtrSet<IntrinsicInst*, 8> &SelCalls) {
203 SmallPtrSet<PHINode*, 32> SeenPHIs;
204 bool Changed = false;
205
Bill Wendling201f5f02010-03-29 23:37:07 +0000206 for (Value::use_iterator
207 I = Inst->use_begin(), E = Inst->use_end(); I != E; ++I) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000208 Instruction *II = dyn_cast<Instruction>(*I);
Bill Wendling201f5f02010-03-29 23:37:07 +0000209 if (!II || II->getParent()->getParent() != F) continue;
210
211 if (IntrinsicInst *Sel = dyn_cast<IntrinsicInst>(II)) {
212 if (Sel->getIntrinsicID() == Intrinsic::eh_selector)
213 SelCalls.insert(Sel);
214 } else if (InvokeInst *Invoke = dyn_cast<InvokeInst>(II)) {
215 if (Invoke->getCalledFunction() == URoR)
216 URoRInvoke = true;
217 } else if (CastInst *CI = dyn_cast<CastInst>(II)) {
218 Changed |= FindSelectorAndURoR(CI, URoRInvoke, SelCalls);
Bill Wendling201f5f02010-03-29 23:37:07 +0000219 } else if (PHINode *PN = dyn_cast<PHINode>(II)) {
220 if (SeenPHIs.insert(PN))
221 // Don't process a PHI node more than once.
222 Changed |= FindSelectorAndURoR(PN, URoRInvoke, SelCalls);
223 }
224 }
225
226 return Changed;
227}
228
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000229/// HandleURoRInvokes - Handle invokes of "_Unwind_Resume_or_Rethrow" calls. The
230/// "unwind" part of these invokes jump to a landing pad within the current
231/// function. This is a candidate to merge the selector associated with the URoR
232/// invoke with the one from the URoR's landing pad.
233bool DwarfEHPrepare::HandleURoRInvokes() {
234 if (!EHCatchAllValue) {
235 EHCatchAllValue =
Bill Wendling23295cc2010-07-26 22:36:52 +0000236 F->getParent()->getNamedGlobal("llvm.eh.catch.all.value");
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000237 if (!EHCatchAllValue) return false;
238 }
239
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000240 if (!SelectorIntrinsic) {
241 SelectorIntrinsic =
242 Intrinsic::getDeclaration(F->getParent(), Intrinsic::eh_selector);
243 if (!SelectorIntrinsic) return false;
244 }
245
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000246 SmallPtrSet<IntrinsicInst*, 32> Sels;
247 SmallPtrSet<IntrinsicInst*, 32> CatchAllSels;
248 FindAllCleanupSelectors(Sels, CatchAllSels);
249
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000250 if (!URoR) {
251 URoR = F->getParent()->getFunction("_Unwind_Resume_or_Rethrow");
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000252 if (!URoR) return CleanupSelectors(CatchAllSels);
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000253 }
254
Bill Wendlingbfde6442010-03-29 23:02:46 +0000255 SmallPtrSet<InvokeInst*, 32> URoRInvokes;
Bill Wendlingbfde6442010-03-29 23:02:46 +0000256 FindAllURoRInvokes(URoRInvokes);
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000257
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000258 SmallPtrSet<IntrinsicInst*, 32> SelsToConvert;
259
Bill Wendlingbfde6442010-03-29 23:02:46 +0000260 for (SmallPtrSet<IntrinsicInst*, 32>::iterator
261 SI = Sels.begin(), SE = Sels.end(); SI != SE; ++SI) {
262 const BasicBlock *SelBB = (*SI)->getParent();
263 for (SmallPtrSet<InvokeInst*, 32>::iterator
264 UI = URoRInvokes.begin(), UE = URoRInvokes.end(); UI != UE; ++UI) {
265 const BasicBlock *URoRBB = (*UI)->getParent();
Dan Gohmance0fe5d2010-07-26 17:38:15 +0000266 if (DT->dominates(SelBB, URoRBB)) {
Bill Wendlingbfde6442010-03-29 23:02:46 +0000267 SelsToConvert.insert(*SI);
268 break;
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000269 }
270 }
271 }
272
Bill Wendlingbfde6442010-03-29 23:02:46 +0000273 bool Changed = false;
274
Bill Wendling201f5f02010-03-29 23:37:07 +0000275 if (Sels.size() != SelsToConvert.size()) {
276 // If we haven't been able to convert all of the clean-up selectors, then
277 // loop through the slow way to see if they still need to be converted.
278 if (!ExceptionValueIntrinsic) {
279 ExceptionValueIntrinsic =
280 Intrinsic::getDeclaration(F->getParent(), Intrinsic::eh_exception);
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000281 if (!ExceptionValueIntrinsic)
282 return CleanupSelectors(CatchAllSels);
Bill Wendling201f5f02010-03-29 23:37:07 +0000283 }
284
285 for (Value::use_iterator
286 I = ExceptionValueIntrinsic->use_begin(),
287 E = ExceptionValueIntrinsic->use_end(); I != E; ++I) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000288 IntrinsicInst *EHPtr = dyn_cast<IntrinsicInst>(*I);
Bill Wendling201f5f02010-03-29 23:37:07 +0000289 if (!EHPtr || EHPtr->getParent()->getParent() != F) continue;
290
Bill Wendling201f5f02010-03-29 23:37:07 +0000291 bool URoRInvoke = false;
292 SmallPtrSet<IntrinsicInst*, 8> SelCalls;
293 Changed |= FindSelectorAndURoR(EHPtr, URoRInvoke, SelCalls);
294
295 if (URoRInvoke) {
296 // This EH pointer is being used by an invoke of an URoR instruction and
297 // an eh.selector intrinsic call. If the eh.selector is a 'clean-up', we
298 // need to convert it to a 'catch-all'.
299 for (SmallPtrSet<IntrinsicInst*, 8>::iterator
Bill Wendling94366112010-06-12 02:34:29 +0000300 SI = SelCalls.begin(), SE = SelCalls.end(); SI != SE; ++SI)
Bill Wendlingefbf3062010-06-24 18:49:10 +0000301 if (!HasCatchAllInSelector(*SI))
Bill Wendling94366112010-06-12 02:34:29 +0000302 SelsToConvert.insert(*SI);
Bill Wendling201f5f02010-03-29 23:37:07 +0000303 }
304 }
305 }
306
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000307 if (!SelsToConvert.empty()) {
308 // Convert all clean-up eh.selectors, which are associated with "invokes" of
309 // URoR calls, into catch-all eh.selectors.
310 Changed = true;
311
312 for (SmallPtrSet<IntrinsicInst*, 8>::iterator
313 SI = SelsToConvert.begin(), SE = SelsToConvert.end();
314 SI != SE; ++SI) {
315 IntrinsicInst *II = *SI;
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000316
317 // Use the exception object pointer and the personality function
318 // from the original selector.
Gabor Greif2bf4b3b2010-06-25 11:25:30 +0000319 CallSite CS(II);
320 IntrinsicInst::op_iterator I = CS.arg_begin();
Gabor Greif2bf4b3b2010-06-25 11:25:30 +0000321 IntrinsicInst::op_iterator E = CS.arg_end();
322 IntrinsicInst::op_iterator B = prior(E);
323
324 // Exclude last argument if it is an integer.
325 if (isa<ConstantInt>(B)) E = B;
Bill Wendling94366112010-06-12 02:34:29 +0000326
Gabor Greif32621ad2010-06-28 16:40:52 +0000327 // Add exception object pointer (front).
328 // Add personality function (next).
329 // Add in any filter IDs (rest).
330 SmallVector<Value*, 8> Args(I, E);
Bill Wendling94366112010-06-12 02:34:29 +0000331
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000332 Args.push_back(EHCatchAllValue->getInitializer()); // Catch-all indicator.
333
334 CallInst *NewSelector =
335 CallInst::Create(SelectorIntrinsic, Args.begin(), Args.end(),
336 "eh.sel.catch.all", II);
337
338 NewSelector->setTailCall(II->isTailCall());
339 NewSelector->setAttributes(II->getAttributes());
340 NewSelector->setCallingConv(II->getCallingConv());
341
342 II->replaceAllUsesWith(NewSelector);
343 II->eraseFromParent();
344 }
345 }
346
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000347 Changed |= CleanupSelectors(CatchAllSels);
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000348 return Changed;
349}
350
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000351/// NormalizeLandingPads - Normalize and discover landing pads, noting them
352/// in the LandingPads set. A landing pad is normal if the only CFG edges
Eric Christopherec26bf72009-09-15 21:56:46 +0000353/// that end at it are unwind edges from invoke instructions. If we inlined
354/// through an invoke we could have a normal branch from the previous
355/// unwind block through to the landing pad for the original invoke.
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000356/// Abnormal landing pads are fixed up by redirecting all unwind edges to
357/// a new basic block which falls through to the original.
358bool DwarfEHPrepare::NormalizeLandingPads() {
359 bool Changed = false;
360
Dan Gohman55e59c12010-04-19 19:05:59 +0000361 const MCAsmInfo *MAI = TM->getMCAsmInfo();
Jim Grosbach8d77cc82010-01-20 23:03:55 +0000362 bool usingSjLjEH = MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
363
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000364 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
365 TerminatorInst *TI = I->getTerminator();
366 if (!isa<InvokeInst>(TI))
367 continue;
368 BasicBlock *LPad = TI->getSuccessor(1);
369 // Skip landing pads that have already been normalized.
370 if (LandingPads.count(LPad))
371 continue;
372
373 // Check that only invoke unwind edges end at the landing pad.
374 bool OnlyUnwoundTo = true;
Jim Grosbach8d77cc82010-01-20 23:03:55 +0000375 bool SwitchOK = usingSjLjEH;
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000376 for (pred_iterator PI = pred_begin(LPad), PE = pred_end(LPad);
377 PI != PE; ++PI) {
378 TerminatorInst *PT = (*PI)->getTerminator();
Jim Grosbach8d77cc82010-01-20 23:03:55 +0000379 // The SjLj dispatch block uses a switch instruction. This is effectively
380 // an unwind edge, so we can disregard it here. There will only ever
381 // be one dispatch, however, so if there are multiple switches, one
382 // of them truly is a normal edge, not an unwind edge.
383 if (SwitchOK && isa<SwitchInst>(PT)) {
384 SwitchOK = false;
385 continue;
386 }
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000387 if (!isa<InvokeInst>(PT) || LPad == PT->getSuccessor(0)) {
388 OnlyUnwoundTo = false;
389 break;
390 }
391 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000392
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000393 if (OnlyUnwoundTo) {
394 // Only unwind edges lead to the landing pad. Remember the landing pad.
395 LandingPads.insert(LPad);
396 continue;
397 }
398
399 // At least one normal edge ends at the landing pad. Redirect the unwind
400 // edges to a new basic block which falls through into this one.
401
402 // Create the new basic block.
Bill Wendling8bedf972009-10-29 00:37:35 +0000403 BasicBlock *NewBB = BasicBlock::Create(F->getContext(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000404 LPad->getName() + "_unwind_edge");
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000405
406 // Insert it into the function right before the original landing pad.
407 LPad->getParent()->getBasicBlockList().insert(LPad, NewBB);
408
409 // Redirect unwind edges from the original landing pad to NewBB.
410 for (pred_iterator PI = pred_begin(LPad), PE = pred_end(LPad); PI != PE; ) {
411 TerminatorInst *PT = (*PI++)->getTerminator();
412 if (isa<InvokeInst>(PT) && PT->getSuccessor(1) == LPad)
413 // Unwind to the new block.
414 PT->setSuccessor(1, NewBB);
415 }
416
417 // If there are any PHI nodes in LPad, we need to update them so that they
418 // merge incoming values from NewBB instead.
419 for (BasicBlock::iterator II = LPad->begin(); isa<PHINode>(II); ++II) {
420 PHINode *PN = cast<PHINode>(II);
421 pred_iterator PB = pred_begin(NewBB), PE = pred_end(NewBB);
422
423 // Check to see if all of the values coming in via unwind edges are the
424 // same. If so, we don't need to create a new PHI node.
425 Value *InVal = PN->getIncomingValueForBlock(*PB);
426 for (pred_iterator PI = PB; PI != PE; ++PI) {
427 if (PI != PB && InVal != PN->getIncomingValueForBlock(*PI)) {
428 InVal = 0;
429 break;
430 }
431 }
432
433 if (InVal == 0) {
434 // Different unwind edges have different values. Create a new PHI node
435 // in NewBB.
436 PHINode *NewPN = PHINode::Create(PN->getType(), PN->getName()+".unwind",
437 NewBB);
438 // Add an entry for each unwind edge, using the value from the old PHI.
439 for (pred_iterator PI = PB; PI != PE; ++PI)
440 NewPN->addIncoming(PN->getIncomingValueForBlock(*PI), *PI);
441
442 // Now use this new PHI as the common incoming value for NewBB in PN.
443 InVal = NewPN;
444 }
445
446 // Revector exactly one entry in the PHI node to come from NewBB
447 // and delete all other entries that come from unwind edges. If
448 // there are both normal and unwind edges from the same predecessor,
449 // this leaves an entry for the normal edge.
450 for (pred_iterator PI = PB; PI != PE; ++PI)
451 PN->removeIncomingValue(*PI);
452 PN->addIncoming(InVal, NewBB);
453 }
454
455 // Add a fallthrough from NewBB to the original landing pad.
456 BranchInst::Create(LPad, NewBB);
457
Duncan Sands22efc182010-08-31 09:05:06 +0000458 // Now update DominatorTree analysis information.
459 DT->splitBlock(NewBB);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000460
461 // Remember the newly constructed landing pad. The original landing pad
462 // LPad is no longer a landing pad now that all unwind edges have been
463 // revectored to NewBB.
464 LandingPads.insert(NewBB);
465 ++NumLandingPadsSplit;
466 Changed = true;
467 }
468
469 return Changed;
470}
471
472/// LowerUnwinds - Turn unwind instructions into calls to _Unwind_Resume,
473/// rethrowing any previously caught exception. This will crash horribly
474/// at runtime if there is no such exception: using unwind to throw a new
475/// exception is currently not supported.
476bool DwarfEHPrepare::LowerUnwinds() {
Bill Wendling43488712009-09-14 20:52:37 +0000477 SmallVector<TerminatorInst*, 16> UnwindInsts;
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000478
479 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
480 TerminatorInst *TI = I->getTerminator();
Bill Wendling43488712009-09-14 20:52:37 +0000481 if (isa<UnwindInst>(TI))
482 UnwindInsts.push_back(TI);
483 }
484
485 if (UnwindInsts.empty()) return false;
486
487 // Find the rewind function if we didn't already.
488 if (!RewindFunction) {
Bill Wendling8bedf972009-10-29 00:37:35 +0000489 LLVMContext &Ctx = UnwindInsts[0]->getContext();
Bill Wendling43488712009-09-14 20:52:37 +0000490 std::vector<const Type*>
Bill Wendling8bedf972009-10-29 00:37:35 +0000491 Params(1, Type::getInt8PtrTy(Ctx));
492 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
Bill Wendling43488712009-09-14 20:52:37 +0000493 Params, false);
494 const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME);
495 RewindFunction = F->getParent()->getOrInsertFunction(RewindName, FTy);
496 }
497
Bill Wendling8bedf972009-10-29 00:37:35 +0000498 bool Changed = false;
499
Bill Wendling43488712009-09-14 20:52:37 +0000500 for (SmallVectorImpl<TerminatorInst*>::iterator
501 I = UnwindInsts.begin(), E = UnwindInsts.end(); I != E; ++I) {
502 TerminatorInst *TI = *I;
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000503
504 // Replace the unwind instruction with a call to _Unwind_Resume (or the
505 // appropriate target equivalent) followed by an UnreachableInst.
506
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000507 // Create the call...
Eric Christopher82f149d2009-09-04 01:14:14 +0000508 CallInst *CI = CallInst::Create(RewindFunction,
Duncan Sands850fcd42010-09-03 08:31:48 +0000509 CreateExceptionValueCall(TI->getParent()),
Bill Wendling43488712009-09-14 20:52:37 +0000510 "", TI);
Eric Christopher82f149d2009-09-04 01:14:14 +0000511 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000512 // ...followed by an UnreachableInst.
Bill Wendling8bedf972009-10-29 00:37:35 +0000513 new UnreachableInst(TI->getContext(), TI);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000514
515 // Nuke the unwind instruction.
516 TI->eraseFromParent();
517 ++NumUnwindsLowered;
518 Changed = true;
519 }
520
521 return Changed;
522}
523
524/// MoveExceptionValueCalls - Ensure that eh.exception is only ever called from
Duncan Sands850fcd42010-09-03 08:31:48 +0000525/// landing pads by replacing calls outside of landing pads with direct use of
526/// a register holding the appropriate value; this requires adding calls inside
527/// all landing pads to initialize the register. Also, move eh.exception calls
528/// inside landing pads to the start of the landing pad (optional, but may make
529/// things simpler for later passes).
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000530bool DwarfEHPrepare::MoveExceptionValueCalls() {
531 // If the eh.exception intrinsic is not declared in the module then there is
532 // nothing to do. Speed up compilation by checking for this common case.
Bill Wendling8bedf972009-10-29 00:37:35 +0000533 if (!ExceptionValueIntrinsic &&
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000534 !F->getParent()->getFunction(Intrinsic::getName(Intrinsic::eh_exception)))
535 return false;
536
537 bool Changed = false;
538
Duncan Sands850fcd42010-09-03 08:31:48 +0000539 // Move calls to eh.exception that are inside a landing pad to the start of
540 // the landing pad.
541 for (BBSet::const_iterator LI = LandingPads.begin(), LE = LandingPads.end();
Eric Christopheradc581f2010-09-01 17:29:10 +0000542 LI != LE; ++LI) {
Duncan Sands850fcd42010-09-03 08:31:48 +0000543 BasicBlock *LP = *LI;
544 for (BasicBlock::iterator II = LP->getFirstNonPHIOrDbg(), IE = LP->end();
545 II != IE;)
546 if (EHExceptionInst *EI = dyn_cast<EHExceptionInst>(II++)) {
547 // Found a call to eh.exception.
548 if (!EI->use_empty()) {
549 // If there is already a call to eh.exception at the start of the
550 // landing pad, then get hold of it; otherwise create such a call.
551 Value *CallAtStart = CreateExceptionValueCall(LP);
552
553 // If the call was at the start of a landing pad then leave it alone.
554 if (EI == CallAtStart)
555 continue;
556 EI->replaceAllUsesWith(CallAtStart);
557 }
558 EI->eraseFromParent();
559 ++NumExceptionValuesMoved;
560 Changed = true;
561 }
Duncan Sandsfb4e8ab2010-09-01 14:07:47 +0000562 }
563
Duncan Sands850fcd42010-09-03 08:31:48 +0000564 // Look for calls to eh.exception that are not in a landing pad. If one is
565 // found, then a register that holds the exception value will be created in
566 // each landing pad, and the SSAUpdater will be used to compute the values
567 // returned by eh.exception calls outside of landing pads.
568 SSAUpdater SSA;
569
570 // Remember where we found the eh.exception call, to avoid rescanning earlier
571 // basic blocks which we already know contain no eh.exception calls.
572 bool FoundCallOutsideLandingPad = false;
573 Function::iterator BB = F->begin();
574 for (Function::iterator BE = F->end(); BB != BE; ++BB) {
575 // Skip over landing pads.
576 if (LandingPads.count(BB))
577 continue;
578
579 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end();
580 II != IE; ++II)
581 if (isa<EHExceptionInst>(II)) {
582 SSA.Initialize(II->getType(), II->getName());
583 FoundCallOutsideLandingPad = true;
584 break;
585 }
586
587 if (FoundCallOutsideLandingPad)
588 break;
589 }
590
591 // If all calls to eh.exception are in landing pads then we are done.
592 if (!FoundCallOutsideLandingPad)
593 return Changed;
594
595 // Add a call to eh.exception at the start of each landing pad, and tell the
596 // SSAUpdater that this is the value produced by the landing pad.
597 for (BBSet::iterator LI = LandingPads.begin(), LE = LandingPads.end();
598 LI != LE; ++LI)
599 SSA.AddAvailableValue(*LI, CreateExceptionValueCall(*LI));
600
601 // Now turn all calls to eh.exception that are not in a landing pad into a use
602 // of the appropriate register.
603 for (Function::iterator BE = F->end(); BB != BE; ++BB) {
604 // Skip over landing pads.
605 if (LandingPads.count(BB))
606 continue;
607
608 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end();
609 II != IE;)
610 if (EHExceptionInst *EI = dyn_cast<EHExceptionInst>(II++)) {
611 // Found a call to eh.exception, replace it with the value from any
612 // upstream landing pad(s).
613 EI->replaceAllUsesWith(SSA.GetValueAtEndOfBlock(BB));
614 EI->eraseFromParent();
615 ++NumExceptionValuesMoved;
616 }
617 }
618
619 return true;
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000620}
621
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000622/// CreateExceptionValueCall - Insert a call to the eh.exception intrinsic at
623/// the start of the basic block (unless there already is one, in which case
624/// the existing call is returned).
625Instruction *DwarfEHPrepare::CreateExceptionValueCall(BasicBlock *BB) {
Dale Johannesen7249ef02010-04-02 21:49:27 +0000626 Instruction *Start = BB->getFirstNonPHIOrDbg();
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000627 // Is this a call to eh.exception?
628 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Start))
629 if (CI->getIntrinsicID() == Intrinsic::eh_exception)
630 // Reuse the existing call.
631 return Start;
632
633 // Find the eh.exception intrinsic if we didn't already.
Bill Wendling8bedf972009-10-29 00:37:35 +0000634 if (!ExceptionValueIntrinsic)
635 ExceptionValueIntrinsic = Intrinsic::getDeclaration(F->getParent(),
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000636 Intrinsic::eh_exception);
637
638 // Create the call.
Bill Wendling8bedf972009-10-29 00:37:35 +0000639 return CallInst::Create(ExceptionValueIntrinsic, "eh.value.call", Start);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000640}
641
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000642bool DwarfEHPrepare::runOnFunction(Function &Fn) {
643 bool Changed = false;
644
645 // Initialize internal state.
Duncan Sands22efc182010-08-31 09:05:06 +0000646 DT = &getAnalysis<DominatorTree>();
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000647 F = &Fn;
648
649 // Ensure that only unwind edges end at landing pads (a landing pad is a
650 // basic block where an invoke unwind edge ends).
651 Changed |= NormalizeLandingPads();
652
653 // Turn unwind instructions into libcalls.
654 Changed |= LowerUnwinds();
655
Bill Wendling8bedf972009-10-29 00:37:35 +0000656 // TODO: Move eh.selector calls to landing pads and combine them.
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000657
658 // Move eh.exception calls to landing pads.
659 Changed |= MoveExceptionValueCalls();
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}