blob: 699d64bb189cb7025bcb162ee3c3b6fc17d68c90 [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"
Chris Lattner44556082010-08-29 19:54:28 +000029#include "llvm/Transforms/Utils/SSAUpdater.h"
Duncan Sandsb0f1e172009-05-22 20:36:31 +000030using namespace llvm;
31
Bill Wendlingf58898f2009-10-29 00:22:16 +000032STATISTIC(NumLandingPadsSplit, "Number of landing pads split");
Bill Wendlingf58898f2009-10-29 00:22:16 +000033STATISTIC(NumUnwindsLowered, "Number of unwind instructions lowered");
Bill Wendling8bedf972009-10-29 00:37:35 +000034STATISTIC(NumExceptionValuesMoved, "Number of eh.exception calls moved");
35STATISTIC(NumStackTempsIntroduced, "Number of stack temporaries introduced");
Duncan Sandsb0f1e172009-05-22 20:36:31 +000036
Chris Lattner44556082010-08-29 19:54:28 +000037static void PromoteAlloca(AllocaInst *AI);
38
Duncan Sandsb0f1e172009-05-22 20:36:31 +000039namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000040 class DwarfEHPrepare : public FunctionPass {
Dan Gohman55e59c12010-04-19 19:05:59 +000041 const TargetMachine *TM;
Duncan Sandsb0f1e172009-05-22 20:36:31 +000042 const TargetLowering *TLI;
Bill Wendling8bedf972009-10-29 00:37:35 +000043 bool CompileFast;
Duncan Sandsb0f1e172009-05-22 20:36:31 +000044
45 // The eh.exception intrinsic.
Bill Wendling8bedf972009-10-29 00:37:35 +000046 Function *ExceptionValueIntrinsic;
Duncan Sandsb0f1e172009-05-22 20:36:31 +000047
Bill Wendling2ad4aca2010-03-26 23:41:30 +000048 // The eh.selector intrinsic.
49 Function *SelectorIntrinsic;
50
51 // _Unwind_Resume_or_Rethrow call.
52 Constant *URoR;
53
54 // The EH language-specific catch-all type.
55 GlobalVariable *EHCatchAllValue;
56
Duncan Sandsb0f1e172009-05-22 20:36:31 +000057 // _Unwind_Resume or the target equivalent.
58 Constant *RewindFunction;
59
60 // Dominator info is used when turning stack temporaries into registers.
61 DominatorTree *DT;
Duncan Sandsb0f1e172009-05-22 20:36:31 +000062
63 // The function we are running on.
64 Function *F;
65
66 // The landing pads for this function.
67 typedef SmallPtrSet<BasicBlock*, 8> BBSet;
68 BBSet LandingPads;
69
70 // Stack temporary used to hold eh.exception values.
71 AllocaInst *ExceptionValueVar;
72
73 bool NormalizeLandingPads();
74 bool LowerUnwinds();
75 bool MoveExceptionValueCalls();
76 bool FinishStackTemporaries();
77 bool PromoteStackTemporaries();
78
79 Instruction *CreateExceptionValueCall(BasicBlock *BB);
80 Instruction *CreateValueLoad(BasicBlock *BB);
81
82 /// CreateReadOfExceptionValue - Return the result of the eh.exception
Bill Wendling2ad4aca2010-03-26 23:41:30 +000083 /// intrinsic by calling the intrinsic if in a landing pad, or loading it
84 /// from the exception value variable otherwise.
Duncan Sandsb0f1e172009-05-22 20:36:31 +000085 Instruction *CreateReadOfExceptionValue(BasicBlock *BB) {
86 return LandingPads.count(BB) ?
87 CreateExceptionValueCall(BB) : CreateValueLoad(BB);
88 }
89
Bill Wendlingbfbd8532010-03-27 01:19:12 +000090 /// CleanupSelectors - Any remaining eh.selector intrinsic calls which still
Bill Wendling23295cc2010-07-26 22:36:52 +000091 /// use the "llvm.eh.catch.all.value" call need to convert to using its
Bill Wendlingbfbd8532010-03-27 01:19:12 +000092 /// initializer instead.
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +000093 bool CleanupSelectors(SmallPtrSet<IntrinsicInst*, 32> &Sels);
Bill Wendlingbfbd8532010-03-27 01:19:12 +000094
Bill Wendlingefbf3062010-06-24 18:49:10 +000095 bool HasCatchAllInSelector(IntrinsicInst *);
Bill Wendling94366112010-06-12 02:34:29 +000096
Bill Wendlingbfde6442010-03-29 23:02:46 +000097 /// FindAllCleanupSelectors - Find all eh.selector calls that are clean-ups.
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +000098 void FindAllCleanupSelectors(SmallPtrSet<IntrinsicInst*, 32> &Sels,
99 SmallPtrSet<IntrinsicInst*, 32> &CatchAllSels);
Bill Wendlingbfde6442010-03-29 23:02:46 +0000100
101 /// FindAllURoRInvokes - Find all URoR invokes in the function.
102 void FindAllURoRInvokes(SmallPtrSet<InvokeInst*, 32> &URoRInvokes);
103
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000104 /// HandleURoRInvokes - Handle invokes of "_Unwind_Resume_or_Rethrow"
105 /// calls. The "unwind" part of these invokes jump to a landing pad within
106 /// the current function. This is a candidate to merge the selector
107 /// associated with the URoR invoke with the one from the URoR's landing
108 /// pad.
109 bool HandleURoRInvokes();
110
Bill Wendling201f5f02010-03-29 23:37:07 +0000111 /// FindSelectorAndURoR - Find the eh.selector call and URoR call associated
112 /// with the eh.exception call. This recursively looks past instructions
113 /// which don't change the EH pointer value, like casts or PHI nodes.
114 bool FindSelectorAndURoR(Instruction *Inst, bool &URoRInvoke,
115 SmallPtrSet<IntrinsicInst*, 8> &SelCalls);
116
Chris Lattnerc34c2202010-08-29 19:28:28 +0000117 /// PromoteStoreInst - Perform Mem2Reg on a StoreInst.
118 bool PromoteStoreInst(StoreInst *SI) {
Chris Lattnerc34c2202010-08-29 19:28:28 +0000119 AllocaInst *AI = dyn_cast<AllocaInst>(SI->getOperand(1));
Bill Wendling201f5f02010-03-29 23:37:07 +0000120 if (!AI || !isAllocaPromotable(AI)) return false;
Chris Lattnerc34c2202010-08-29 19:28:28 +0000121
Chris Lattner44556082010-08-29 19:54:28 +0000122 PromoteAlloca(AI);
Bill Wendling201f5f02010-03-29 23:37:07 +0000123 return true;
124 }
125
Bill Wendling201f5f02010-03-29 23:37:07 +0000126 /// PromoteEHPtrStore - Promote the storing of an EH pointer into a
127 /// register. This should get rid of the store and subsequent loads.
128 bool PromoteEHPtrStore(IntrinsicInst *II) {
Chris Lattner44556082010-08-29 19:54:28 +0000129 if (!CompileFast) return false;
Bill Wendling201f5f02010-03-29 23:37:07 +0000130
131 bool Changed = false;
132 StoreInst *SI;
133
134 while (1) {
135 SI = 0;
136 for (Value::use_iterator
137 I = II->use_begin(), E = II->use_end(); I != E; ++I) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000138 SI = dyn_cast<StoreInst>(*I);
Bill Wendling201f5f02010-03-29 23:37:07 +0000139 if (SI) break;
140 }
141
Chris Lattner44556082010-08-29 19:54:28 +0000142 if (SI && !PromoteStoreInst(SI))
Bill Wendling201f5f02010-03-29 23:37:07 +0000143 break;
144
145 Changed = true;
146 }
147
Duncan Sands80b9b892010-06-29 14:49:35 +0000148 return Changed;
Bill Wendling201f5f02010-03-29 23:37:07 +0000149 }
150
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000151 public:
152 static char ID; // Pass identification, replacement for typeid.
Dan Gohman55e59c12010-04-19 19:05:59 +0000153 DwarfEHPrepare(const TargetMachine *tm, bool fast) :
Owen Anderson90c579d2010-08-06 18:33:48 +0000154 FunctionPass(ID), TM(tm), TLI(TM->getTargetLowering()),
Dan Gohman55e59c12010-04-19 19:05:59 +0000155 CompileFast(fast),
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000156 ExceptionValueIntrinsic(0), SelectorIntrinsic(0),
157 URoR(0), EHCatchAllValue(0), RewindFunction(0) {}
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000158
159 virtual bool runOnFunction(Function &Fn);
160
Bill Wendling8bedf972009-10-29 00:37:35 +0000161 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Bill Wendling8bedf972009-10-29 00:37:35 +0000162 AU.addPreserved<DominatorTree>();
Bill Wendling8bedf972009-10-29 00:37:35 +0000163 }
164
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000165 const char *getPassName() const {
166 return "Exception handling preparation";
167 }
168
169 };
170} // end anonymous namespace
171
172char DwarfEHPrepare::ID = 0;
173
Dan Gohman55e59c12010-04-19 19:05:59 +0000174FunctionPass *llvm::createDwarfEHPass(const TargetMachine *tm, bool fast) {
175 return new DwarfEHPrepare(tm, fast);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000176}
177
Chris Lattner44556082010-08-29 19:54:28 +0000178/// PromoteAlloca - This promotes an alloca to registers when we know that it
179/// only has non-volatile loads and stores to it.
180static void PromoteAlloca(AllocaInst *AI) {
181 assert(isAllocaPromotable(AI));
182
183 // First step: bucket up uses of the pointers by the block they occur in.
184 // This is important because we have to handle multiple defs/uses in a block
185 // ourselves: SSAUpdater is purely for cross-block references.
186 // FIXME: Want a TinyVector<Instruction*> since there is usually 0/1 element.
187 DenseMap<BasicBlock*, std::vector<Instruction*> > UsesByBlock;
188 for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end();
189 UI != E; ++UI) {
190 Instruction *User = cast<Instruction>(*UI);
191 UsesByBlock[User->getParent()].push_back(User);
192 }
193
194 SSAUpdater SSA;
195
196 // It wants to know some value of the same type as what we'll be inserting.
197 Value *SomeValue;
198 if (isa<LoadInst>(*AI->use_begin()))
199 SomeValue = *AI->use_begin();
200 else
201 SomeValue = cast<StoreInst>(*AI->use_begin())->getOperand(0);
202 SSA.Initialize(SomeValue);
203
204 // Okay, now we can iterate over all the blocks in the loop with uses,
205 // processing them. Keep track of which loads are loading a live-in value.
206 SmallVector<LoadInst*, 32> LiveInLoads;
207
208 for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end();
209 UI != E; ++UI) {
210 Instruction *User = cast<Instruction>(*UI);
211 std::vector<Instruction*> &BlockUses = UsesByBlock[User->getParent()];
212
213 // If this block has already been processed, ignore this repeat use.
214 if (BlockUses.empty()) continue;
215
216 // Okay, this is the first use in the block. If this block just has a
217 // single user in it, we can rewrite it trivially.
218 if (BlockUses.size() == 1) {
219 // If it is a store, it is a trivial def of the value in the block.
220 if (isa<StoreInst>(User)) {
221 SSA.AddAvailableValue(User->getParent(),
222 cast<StoreInst>(User)->getOperand(0));
223 } else {
224 // Otherwise it is a load, queue it to rewrite as a live-in load.
225 LiveInLoads.push_back(cast<LoadInst>(User));
226 }
227 BlockUses.clear();
228 continue;
229 }
230
231 // Otherwise, check to see if this block is all loads. If so, we can queue
232 // them all as live in loads.
233 bool HasStore = false;
234 for (unsigned i = 0, e = BlockUses.size(); i != e; ++i) {
235 if (isa<StoreInst>(BlockUses[i])) {
236 HasStore = true;
237 break;
238 }
239 }
240
241 if (!HasStore) {
242 for (unsigned i = 0, e = BlockUses.size(); i != e; ++i)
243 LiveInLoads.push_back(cast<LoadInst>(BlockUses[i]));
244 BlockUses.clear();
245 continue;
246 }
247
248 // Otherwise, we have mixed loads and stores (or just a bunch of stores).
249 // Since SSAUpdater is purely for cross-block values, we need to determine
250 // the order of these instructions in the block. If the first use in the
251 // block is a load, then it uses the live in value. The last store defines
252 // the live out value. We handle this by doing a linear scan of the block.
253 BasicBlock *BB = User->getParent();
254 Value *StoredValue = 0;
255 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ++II) {
256 if (LoadInst *L = dyn_cast<LoadInst>(II)) {
257 // If this is a load to an unrelated pointer, ignore it.
258 if (L->getOperand(0) != AI) continue;
259
260 // If we haven't seen a store yet, this is a live in use, otherwise
261 // use the stored value.
262 if (StoredValue)
263 L->replaceAllUsesWith(StoredValue);
264 else
265 LiveInLoads.push_back(L);
266 continue;
267 }
268
269 if (StoreInst *S = dyn_cast<StoreInst>(II)) {
270 // If this is a store to an unrelated pointer, ignore it.
271 if (S->getOperand(1) != AI) continue;
272
273 // Remember that this is the active value in the block.
274 StoredValue = S->getOperand(0);
275 }
276 }
277
278 // The last stored value that happened is the live-out for the block.
279 assert(StoredValue && "Already checked that there is a store in block");
280 SSA.AddAvailableValue(BB, StoredValue);
281 BlockUses.clear();
282 }
283
284 // Okay, now we rewrite all loads that use live-in values in the loop,
285 // inserting PHI nodes as necessary.
286 for (unsigned i = 0, e = LiveInLoads.size(); i != e; ++i) {
287 LoadInst *ALoad = LiveInLoads[i];
288 ALoad->replaceAllUsesWith(SSA.GetValueInMiddleOfBlock(ALoad->getParent()));
289 }
290
291 // Now that everything is rewritten, delete the old instructions from the body
292 // of the loop. They should all be dead now.
293 for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end();
294 UI != E; ++UI)
295 cast<Instruction>(*UI)->eraseFromParent();
296}
297
298
299
Bill Wendlingefbf3062010-06-24 18:49:10 +0000300/// HasCatchAllInSelector - Return true if the intrinsic instruction has a
301/// catch-all.
302bool DwarfEHPrepare::HasCatchAllInSelector(IntrinsicInst *II) {
303 if (!EHCatchAllValue) return false;
Bill Wendling94366112010-06-12 02:34:29 +0000304
Gabor Greif2bf4b3b2010-06-25 11:25:30 +0000305 unsigned ArgIdx = II->getNumArgOperands() - 1;
306 GlobalVariable *GV = dyn_cast<GlobalVariable>(II->getArgOperand(ArgIdx));
Bill Wendlingefbf3062010-06-24 18:49:10 +0000307 return GV == EHCatchAllValue;
Bill Wendling94366112010-06-12 02:34:29 +0000308}
309
Bill Wendlingbfde6442010-03-29 23:02:46 +0000310/// FindAllCleanupSelectors - Find all eh.selector calls that are clean-ups.
311void DwarfEHPrepare::
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000312FindAllCleanupSelectors(SmallPtrSet<IntrinsicInst*, 32> &Sels,
313 SmallPtrSet<IntrinsicInst*, 32> &CatchAllSels) {
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000314 for (Value::use_iterator
Bill Wendlingbfde6442010-03-29 23:02:46 +0000315 I = SelectorIntrinsic->use_begin(),
316 E = SelectorIntrinsic->use_end(); I != E; ++I) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000317 IntrinsicInst *II = cast<IntrinsicInst>(*I);
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000318
Bill Wendling94366112010-06-12 02:34:29 +0000319 if (II->getParent()->getParent() != F)
320 continue;
Bill Wendlingbfde6442010-03-29 23:02:46 +0000321
Bill Wendlingefbf3062010-06-24 18:49:10 +0000322 if (!HasCatchAllInSelector(II))
Bill Wendling94366112010-06-12 02:34:29 +0000323 Sels.insert(II);
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000324 else
325 CatchAllSels.insert(II);
Bill Wendlingbfde6442010-03-29 23:02:46 +0000326 }
327}
328
329/// FindAllURoRInvokes - Find all URoR invokes in the function.
330void DwarfEHPrepare::
331FindAllURoRInvokes(SmallPtrSet<InvokeInst*, 32> &URoRInvokes) {
332 for (Value::use_iterator
333 I = URoR->use_begin(),
334 E = URoR->use_end(); I != E; ++I) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000335 if (InvokeInst *II = dyn_cast<InvokeInst>(*I))
Bill Wendlingbfde6442010-03-29 23:02:46 +0000336 URoRInvokes.insert(II);
337 }
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000338}
339
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000340/// CleanupSelectors - Any remaining eh.selector intrinsic calls which still use
Bill Wendling23295cc2010-07-26 22:36:52 +0000341/// the "llvm.eh.catch.all.value" call need to convert to using its
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000342/// initializer instead.
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000343bool DwarfEHPrepare::CleanupSelectors(SmallPtrSet<IntrinsicInst*, 32> &Sels) {
Bill Wendlingbfde6442010-03-29 23:02:46 +0000344 if (!EHCatchAllValue) return false;
345
346 if (!SelectorIntrinsic) {
347 SelectorIntrinsic =
348 Intrinsic::getDeclaration(F->getParent(), Intrinsic::eh_selector);
349 if (!SelectorIntrinsic) return false;
350 }
351
Bill Wendling43de15f2010-03-27 01:22:38 +0000352 bool Changed = false;
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000353 for (SmallPtrSet<IntrinsicInst*, 32>::iterator
354 I = Sels.begin(), E = Sels.end(); I != E; ++I) {
355 IntrinsicInst *Sel = *I;
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000356
Bill Wendling23295cc2010-07-26 22:36:52 +0000357 // Index of the "llvm.eh.catch.all.value" variable.
Gabor Greif9d677682010-06-29 13:03:46 +0000358 unsigned OpIdx = Sel->getNumArgOperands() - 1;
359 GlobalVariable *GV = dyn_cast<GlobalVariable>(Sel->getArgOperand(OpIdx));
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000360 if (GV != EHCatchAllValue) continue;
Gabor Greif9d677682010-06-29 13:03:46 +0000361 Sel->setArgOperand(OpIdx, EHCatchAllValue->getInitializer());
Bill Wendling43de15f2010-03-27 01:22:38 +0000362 Changed = true;
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000363 }
Bill Wendling43de15f2010-03-27 01:22:38 +0000364
365 return Changed;
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000366}
367
Bill Wendling201f5f02010-03-29 23:37:07 +0000368/// FindSelectorAndURoR - Find the eh.selector call associated with the
369/// eh.exception call. And indicate if there is a URoR "invoke" associated with
370/// the eh.exception call. This recursively looks past instructions which don't
371/// change the EH pointer value, like casts or PHI nodes.
372bool
373DwarfEHPrepare::FindSelectorAndURoR(Instruction *Inst, bool &URoRInvoke,
374 SmallPtrSet<IntrinsicInst*, 8> &SelCalls) {
375 SmallPtrSet<PHINode*, 32> SeenPHIs;
376 bool Changed = false;
377
378 restart:
379 for (Value::use_iterator
380 I = Inst->use_begin(), E = Inst->use_end(); I != E; ++I) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000381 Instruction *II = dyn_cast<Instruction>(*I);
Bill Wendling201f5f02010-03-29 23:37:07 +0000382 if (!II || II->getParent()->getParent() != F) continue;
383
384 if (IntrinsicInst *Sel = dyn_cast<IntrinsicInst>(II)) {
385 if (Sel->getIntrinsicID() == Intrinsic::eh_selector)
386 SelCalls.insert(Sel);
387 } else if (InvokeInst *Invoke = dyn_cast<InvokeInst>(II)) {
388 if (Invoke->getCalledFunction() == URoR)
389 URoRInvoke = true;
390 } else if (CastInst *CI = dyn_cast<CastInst>(II)) {
391 Changed |= FindSelectorAndURoR(CI, URoRInvoke, SelCalls);
392 } else if (StoreInst *SI = dyn_cast<StoreInst>(II)) {
393 if (!PromoteStoreInst(SI)) continue;
394 Changed = true;
395 SeenPHIs.clear();
396 goto restart; // Uses may have changed, restart loop.
397 } else if (PHINode *PN = dyn_cast<PHINode>(II)) {
398 if (SeenPHIs.insert(PN))
399 // Don't process a PHI node more than once.
400 Changed |= FindSelectorAndURoR(PN, URoRInvoke, SelCalls);
401 }
402 }
403
404 return Changed;
405}
406
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000407/// HandleURoRInvokes - Handle invokes of "_Unwind_Resume_or_Rethrow" calls. The
408/// "unwind" part of these invokes jump to a landing pad within the current
409/// function. This is a candidate to merge the selector associated with the URoR
410/// invoke with the one from the URoR's landing pad.
411bool DwarfEHPrepare::HandleURoRInvokes() {
412 if (!EHCatchAllValue) {
413 EHCatchAllValue =
Bill Wendling23295cc2010-07-26 22:36:52 +0000414 F->getParent()->getNamedGlobal("llvm.eh.catch.all.value");
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000415 if (!EHCatchAllValue) return false;
416 }
417
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000418 if (!SelectorIntrinsic) {
419 SelectorIntrinsic =
420 Intrinsic::getDeclaration(F->getParent(), Intrinsic::eh_selector);
421 if (!SelectorIntrinsic) return false;
422 }
423
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000424 SmallPtrSet<IntrinsicInst*, 32> Sels;
425 SmallPtrSet<IntrinsicInst*, 32> CatchAllSels;
426 FindAllCleanupSelectors(Sels, CatchAllSels);
427
428 if (!DT)
429 // We require DominatorTree information.
430 return CleanupSelectors(CatchAllSels);
431
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000432 if (!URoR) {
433 URoR = F->getParent()->getFunction("_Unwind_Resume_or_Rethrow");
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000434 if (!URoR) return CleanupSelectors(CatchAllSels);
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000435 }
436
Bill Wendlingbfde6442010-03-29 23:02:46 +0000437 SmallPtrSet<InvokeInst*, 32> URoRInvokes;
Bill Wendlingbfde6442010-03-29 23:02:46 +0000438 FindAllURoRInvokes(URoRInvokes);
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000439
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000440 SmallPtrSet<IntrinsicInst*, 32> SelsToConvert;
441
Bill Wendlingbfde6442010-03-29 23:02:46 +0000442 for (SmallPtrSet<IntrinsicInst*, 32>::iterator
443 SI = Sels.begin(), SE = Sels.end(); SI != SE; ++SI) {
444 const BasicBlock *SelBB = (*SI)->getParent();
445 for (SmallPtrSet<InvokeInst*, 32>::iterator
446 UI = URoRInvokes.begin(), UE = URoRInvokes.end(); UI != UE; ++UI) {
447 const BasicBlock *URoRBB = (*UI)->getParent();
Dan Gohmance0fe5d2010-07-26 17:38:15 +0000448 if (DT->dominates(SelBB, URoRBB)) {
Bill Wendlingbfde6442010-03-29 23:02:46 +0000449 SelsToConvert.insert(*SI);
450 break;
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000451 }
452 }
453 }
454
Bill Wendlingbfde6442010-03-29 23:02:46 +0000455 bool Changed = false;
456
Bill Wendling201f5f02010-03-29 23:37:07 +0000457 if (Sels.size() != SelsToConvert.size()) {
458 // If we haven't been able to convert all of the clean-up selectors, then
459 // loop through the slow way to see if they still need to be converted.
460 if (!ExceptionValueIntrinsic) {
461 ExceptionValueIntrinsic =
462 Intrinsic::getDeclaration(F->getParent(), Intrinsic::eh_exception);
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000463 if (!ExceptionValueIntrinsic)
464 return CleanupSelectors(CatchAllSels);
Bill Wendling201f5f02010-03-29 23:37:07 +0000465 }
466
467 for (Value::use_iterator
468 I = ExceptionValueIntrinsic->use_begin(),
469 E = ExceptionValueIntrinsic->use_end(); I != E; ++I) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000470 IntrinsicInst *EHPtr = dyn_cast<IntrinsicInst>(*I);
Bill Wendling201f5f02010-03-29 23:37:07 +0000471 if (!EHPtr || EHPtr->getParent()->getParent() != F) continue;
472
473 Changed |= PromoteEHPtrStore(EHPtr);
474
475 bool URoRInvoke = false;
476 SmallPtrSet<IntrinsicInst*, 8> SelCalls;
477 Changed |= FindSelectorAndURoR(EHPtr, URoRInvoke, SelCalls);
478
479 if (URoRInvoke) {
480 // This EH pointer is being used by an invoke of an URoR instruction and
481 // an eh.selector intrinsic call. If the eh.selector is a 'clean-up', we
482 // need to convert it to a 'catch-all'.
483 for (SmallPtrSet<IntrinsicInst*, 8>::iterator
Bill Wendling94366112010-06-12 02:34:29 +0000484 SI = SelCalls.begin(), SE = SelCalls.end(); SI != SE; ++SI)
Bill Wendlingefbf3062010-06-24 18:49:10 +0000485 if (!HasCatchAllInSelector(*SI))
Bill Wendling94366112010-06-12 02:34:29 +0000486 SelsToConvert.insert(*SI);
Bill Wendling201f5f02010-03-29 23:37:07 +0000487 }
488 }
489 }
490
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000491 if (!SelsToConvert.empty()) {
492 // Convert all clean-up eh.selectors, which are associated with "invokes" of
493 // URoR calls, into catch-all eh.selectors.
494 Changed = true;
495
496 for (SmallPtrSet<IntrinsicInst*, 8>::iterator
497 SI = SelsToConvert.begin(), SE = SelsToConvert.end();
498 SI != SE; ++SI) {
499 IntrinsicInst *II = *SI;
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000500
501 // Use the exception object pointer and the personality function
502 // from the original selector.
Gabor Greif2bf4b3b2010-06-25 11:25:30 +0000503 CallSite CS(II);
504 IntrinsicInst::op_iterator I = CS.arg_begin();
Gabor Greif2bf4b3b2010-06-25 11:25:30 +0000505 IntrinsicInst::op_iterator E = CS.arg_end();
506 IntrinsicInst::op_iterator B = prior(E);
507
508 // Exclude last argument if it is an integer.
509 if (isa<ConstantInt>(B)) E = B;
Bill Wendling94366112010-06-12 02:34:29 +0000510
Gabor Greif32621ad2010-06-28 16:40:52 +0000511 // Add exception object pointer (front).
512 // Add personality function (next).
513 // Add in any filter IDs (rest).
514 SmallVector<Value*, 8> Args(I, E);
Bill Wendling94366112010-06-12 02:34:29 +0000515
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000516 Args.push_back(EHCatchAllValue->getInitializer()); // Catch-all indicator.
517
518 CallInst *NewSelector =
519 CallInst::Create(SelectorIntrinsic, Args.begin(), Args.end(),
520 "eh.sel.catch.all", II);
521
522 NewSelector->setTailCall(II->isTailCall());
523 NewSelector->setAttributes(II->getAttributes());
524 NewSelector->setCallingConv(II->getCallingConv());
525
526 II->replaceAllUsesWith(NewSelector);
527 II->eraseFromParent();
528 }
529 }
530
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000531 Changed |= CleanupSelectors(CatchAllSels);
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000532 return Changed;
533}
534
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000535/// NormalizeLandingPads - Normalize and discover landing pads, noting them
536/// in the LandingPads set. A landing pad is normal if the only CFG edges
Eric Christopherec26bf72009-09-15 21:56:46 +0000537/// that end at it are unwind edges from invoke instructions. If we inlined
538/// through an invoke we could have a normal branch from the previous
539/// unwind block through to the landing pad for the original invoke.
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000540/// Abnormal landing pads are fixed up by redirecting all unwind edges to
541/// a new basic block which falls through to the original.
542bool DwarfEHPrepare::NormalizeLandingPads() {
543 bool Changed = false;
544
Dan Gohman55e59c12010-04-19 19:05:59 +0000545 const MCAsmInfo *MAI = TM->getMCAsmInfo();
Jim Grosbach8d77cc82010-01-20 23:03:55 +0000546 bool usingSjLjEH = MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
547
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000548 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
549 TerminatorInst *TI = I->getTerminator();
550 if (!isa<InvokeInst>(TI))
551 continue;
552 BasicBlock *LPad = TI->getSuccessor(1);
553 // Skip landing pads that have already been normalized.
554 if (LandingPads.count(LPad))
555 continue;
556
557 // Check that only invoke unwind edges end at the landing pad.
558 bool OnlyUnwoundTo = true;
Jim Grosbach8d77cc82010-01-20 23:03:55 +0000559 bool SwitchOK = usingSjLjEH;
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000560 for (pred_iterator PI = pred_begin(LPad), PE = pred_end(LPad);
561 PI != PE; ++PI) {
562 TerminatorInst *PT = (*PI)->getTerminator();
Jim Grosbach8d77cc82010-01-20 23:03:55 +0000563 // The SjLj dispatch block uses a switch instruction. This is effectively
564 // an unwind edge, so we can disregard it here. There will only ever
565 // be one dispatch, however, so if there are multiple switches, one
566 // of them truly is a normal edge, not an unwind edge.
567 if (SwitchOK && isa<SwitchInst>(PT)) {
568 SwitchOK = false;
569 continue;
570 }
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000571 if (!isa<InvokeInst>(PT) || LPad == PT->getSuccessor(0)) {
572 OnlyUnwoundTo = false;
573 break;
574 }
575 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000576
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000577 if (OnlyUnwoundTo) {
578 // Only unwind edges lead to the landing pad. Remember the landing pad.
579 LandingPads.insert(LPad);
580 continue;
581 }
582
583 // At least one normal edge ends at the landing pad. Redirect the unwind
584 // edges to a new basic block which falls through into this one.
585
586 // Create the new basic block.
Bill Wendling8bedf972009-10-29 00:37:35 +0000587 BasicBlock *NewBB = BasicBlock::Create(F->getContext(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000588 LPad->getName() + "_unwind_edge");
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000589
590 // Insert it into the function right before the original landing pad.
591 LPad->getParent()->getBasicBlockList().insert(LPad, NewBB);
592
593 // Redirect unwind edges from the original landing pad to NewBB.
594 for (pred_iterator PI = pred_begin(LPad), PE = pred_end(LPad); PI != PE; ) {
595 TerminatorInst *PT = (*PI++)->getTerminator();
596 if (isa<InvokeInst>(PT) && PT->getSuccessor(1) == LPad)
597 // Unwind to the new block.
598 PT->setSuccessor(1, NewBB);
599 }
600
601 // If there are any PHI nodes in LPad, we need to update them so that they
602 // merge incoming values from NewBB instead.
603 for (BasicBlock::iterator II = LPad->begin(); isa<PHINode>(II); ++II) {
604 PHINode *PN = cast<PHINode>(II);
605 pred_iterator PB = pred_begin(NewBB), PE = pred_end(NewBB);
606
607 // Check to see if all of the values coming in via unwind edges are the
608 // same. If so, we don't need to create a new PHI node.
609 Value *InVal = PN->getIncomingValueForBlock(*PB);
610 for (pred_iterator PI = PB; PI != PE; ++PI) {
611 if (PI != PB && InVal != PN->getIncomingValueForBlock(*PI)) {
612 InVal = 0;
613 break;
614 }
615 }
616
617 if (InVal == 0) {
618 // Different unwind edges have different values. Create a new PHI node
619 // in NewBB.
620 PHINode *NewPN = PHINode::Create(PN->getType(), PN->getName()+".unwind",
621 NewBB);
622 // Add an entry for each unwind edge, using the value from the old PHI.
623 for (pred_iterator PI = PB; PI != PE; ++PI)
624 NewPN->addIncoming(PN->getIncomingValueForBlock(*PI), *PI);
625
626 // Now use this new PHI as the common incoming value for NewBB in PN.
627 InVal = NewPN;
628 }
629
630 // Revector exactly one entry in the PHI node to come from NewBB
631 // and delete all other entries that come from unwind edges. If
632 // there are both normal and unwind edges from the same predecessor,
633 // this leaves an entry for the normal edge.
634 for (pred_iterator PI = PB; PI != PE; ++PI)
635 PN->removeIncomingValue(*PI);
636 PN->addIncoming(InVal, NewBB);
637 }
638
639 // Add a fallthrough from NewBB to the original landing pad.
640 BranchInst::Create(LPad, NewBB);
641
Chris Lattner44556082010-08-29 19:54:28 +0000642 // Now update DominatorTree analysis information if it is around.
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000643 if (DT)
644 DT->splitBlock(NewBB);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000645
646 // Remember the newly constructed landing pad. The original landing pad
647 // LPad is no longer a landing pad now that all unwind edges have been
648 // revectored to NewBB.
649 LandingPads.insert(NewBB);
650 ++NumLandingPadsSplit;
651 Changed = true;
652 }
653
654 return Changed;
655}
656
657/// LowerUnwinds - Turn unwind instructions into calls to _Unwind_Resume,
658/// rethrowing any previously caught exception. This will crash horribly
659/// at runtime if there is no such exception: using unwind to throw a new
660/// exception is currently not supported.
661bool DwarfEHPrepare::LowerUnwinds() {
Bill Wendling43488712009-09-14 20:52:37 +0000662 SmallVector<TerminatorInst*, 16> UnwindInsts;
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000663
664 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
665 TerminatorInst *TI = I->getTerminator();
Bill Wendling43488712009-09-14 20:52:37 +0000666 if (isa<UnwindInst>(TI))
667 UnwindInsts.push_back(TI);
668 }
669
670 if (UnwindInsts.empty()) return false;
671
672 // Find the rewind function if we didn't already.
673 if (!RewindFunction) {
Bill Wendling8bedf972009-10-29 00:37:35 +0000674 LLVMContext &Ctx = UnwindInsts[0]->getContext();
Bill Wendling43488712009-09-14 20:52:37 +0000675 std::vector<const Type*>
Bill Wendling8bedf972009-10-29 00:37:35 +0000676 Params(1, Type::getInt8PtrTy(Ctx));
677 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
Bill Wendling43488712009-09-14 20:52:37 +0000678 Params, false);
679 const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME);
680 RewindFunction = F->getParent()->getOrInsertFunction(RewindName, FTy);
681 }
682
Bill Wendling8bedf972009-10-29 00:37:35 +0000683 bool Changed = false;
684
Bill Wendling43488712009-09-14 20:52:37 +0000685 for (SmallVectorImpl<TerminatorInst*>::iterator
686 I = UnwindInsts.begin(), E = UnwindInsts.end(); I != E; ++I) {
687 TerminatorInst *TI = *I;
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000688
689 // Replace the unwind instruction with a call to _Unwind_Resume (or the
690 // appropriate target equivalent) followed by an UnreachableInst.
691
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000692 // Create the call...
Eric Christopher82f149d2009-09-04 01:14:14 +0000693 CallInst *CI = CallInst::Create(RewindFunction,
Daniel Dunbara279bc32009-09-20 02:20:51 +0000694 CreateReadOfExceptionValue(TI->getParent()),
Bill Wendling43488712009-09-14 20:52:37 +0000695 "", TI);
Eric Christopher82f149d2009-09-04 01:14:14 +0000696 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000697 // ...followed by an UnreachableInst.
Bill Wendling8bedf972009-10-29 00:37:35 +0000698 new UnreachableInst(TI->getContext(), TI);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000699
700 // Nuke the unwind instruction.
701 TI->eraseFromParent();
702 ++NumUnwindsLowered;
703 Changed = true;
704 }
705
706 return Changed;
707}
708
709/// MoveExceptionValueCalls - Ensure that eh.exception is only ever called from
710/// landing pads by replacing calls outside of landing pads with loads from a
711/// stack temporary. Move eh.exception calls inside landing pads to the start
712/// of the landing pad (optional, but may make things simpler for later passes).
713bool DwarfEHPrepare::MoveExceptionValueCalls() {
714 // If the eh.exception intrinsic is not declared in the module then there is
715 // nothing to do. Speed up compilation by checking for this common case.
Bill Wendling8bedf972009-10-29 00:37:35 +0000716 if (!ExceptionValueIntrinsic &&
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000717 !F->getParent()->getFunction(Intrinsic::getName(Intrinsic::eh_exception)))
718 return false;
719
720 bool Changed = false;
721
722 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
723 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;)
724 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++))
725 if (CI->getIntrinsicID() == Intrinsic::eh_exception) {
726 if (!CI->use_empty()) {
727 Value *ExceptionValue = CreateReadOfExceptionValue(BB);
728 if (CI == ExceptionValue) {
729 // The call was at the start of a landing pad - leave it alone.
730 assert(LandingPads.count(BB) &&
731 "Created eh.exception call outside landing pad!");
732 continue;
733 }
734 CI->replaceAllUsesWith(ExceptionValue);
735 }
736 CI->eraseFromParent();
737 ++NumExceptionValuesMoved;
738 Changed = true;
739 }
740 }
741
742 return Changed;
743}
744
745/// FinishStackTemporaries - If we introduced a stack variable to hold the
746/// exception value then initialize it in each landing pad.
747bool DwarfEHPrepare::FinishStackTemporaries() {
748 if (!ExceptionValueVar)
749 // Nothing to do.
750 return false;
751
752 bool Changed = false;
753
754 // Make sure that there is a store of the exception value at the start of
755 // each landing pad.
756 for (BBSet::iterator LI = LandingPads.begin(), LE = LandingPads.end();
757 LI != LE; ++LI) {
758 Instruction *ExceptionValue = CreateReadOfExceptionValue(*LI);
759 Instruction *Store = new StoreInst(ExceptionValue, ExceptionValueVar);
760 Store->insertAfter(ExceptionValue);
761 Changed = true;
762 }
763
764 return Changed;
765}
766
767/// PromoteStackTemporaries - Turn any stack temporaries we introduced into
768/// registers if possible.
769bool DwarfEHPrepare::PromoteStackTemporaries() {
Chris Lattner44556082010-08-29 19:54:28 +0000770 // Turn the exception temporary into registers and phi nodes if possible.
771 if (ExceptionValueVar && isAllocaPromotable(ExceptionValueVar)) {
772 PromoteAlloca(ExceptionValueVar);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000773 return true;
774 }
775 return false;
776}
777
778/// CreateExceptionValueCall - Insert a call to the eh.exception intrinsic at
779/// the start of the basic block (unless there already is one, in which case
780/// the existing call is returned).
781Instruction *DwarfEHPrepare::CreateExceptionValueCall(BasicBlock *BB) {
Dale Johannesen7249ef02010-04-02 21:49:27 +0000782 Instruction *Start = BB->getFirstNonPHIOrDbg();
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000783 // Is this a call to eh.exception?
784 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Start))
785 if (CI->getIntrinsicID() == Intrinsic::eh_exception)
786 // Reuse the existing call.
787 return Start;
788
789 // Find the eh.exception intrinsic if we didn't already.
Bill Wendling8bedf972009-10-29 00:37:35 +0000790 if (!ExceptionValueIntrinsic)
791 ExceptionValueIntrinsic = Intrinsic::getDeclaration(F->getParent(),
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000792 Intrinsic::eh_exception);
793
794 // Create the call.
Bill Wendling8bedf972009-10-29 00:37:35 +0000795 return CallInst::Create(ExceptionValueIntrinsic, "eh.value.call", Start);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000796}
797
798/// CreateValueLoad - Insert a load of the exception value stack variable
799/// (creating it if necessary) at the start of the basic block (unless
800/// there already is a load, in which case the existing load is returned).
801Instruction *DwarfEHPrepare::CreateValueLoad(BasicBlock *BB) {
Dale Johannesen7249ef02010-04-02 21:49:27 +0000802 Instruction *Start = BB->getFirstNonPHIOrDbg();
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000803 // Is this a load of the exception temporary?
804 if (ExceptionValueVar)
805 if (LoadInst* LI = dyn_cast<LoadInst>(Start))
806 if (LI->getPointerOperand() == ExceptionValueVar)
807 // Reuse the existing load.
808 return Start;
809
810 // Create the temporary if we didn't already.
811 if (!ExceptionValueVar) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000812 ExceptionValueVar = new AllocaInst(PointerType::getUnqual(
Bill Wendling8bedf972009-10-29 00:37:35 +0000813 Type::getInt8Ty(BB->getContext())), "eh.value", F->begin()->begin());
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000814 ++NumStackTempsIntroduced;
815 }
816
817 // Load the value.
818 return new LoadInst(ExceptionValueVar, "eh.value.load", Start);
819}
820
821bool DwarfEHPrepare::runOnFunction(Function &Fn) {
822 bool Changed = false;
823
824 // Initialize internal state.
825 DT = getAnalysisIfAvailable<DominatorTree>();
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000826 ExceptionValueVar = 0;
827 F = &Fn;
828
829 // Ensure that only unwind edges end at landing pads (a landing pad is a
830 // basic block where an invoke unwind edge ends).
831 Changed |= NormalizeLandingPads();
832
833 // Turn unwind instructions into libcalls.
834 Changed |= LowerUnwinds();
835
Bill Wendling8bedf972009-10-29 00:37:35 +0000836 // TODO: Move eh.selector calls to landing pads and combine them.
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000837
838 // Move eh.exception calls to landing pads.
839 Changed |= MoveExceptionValueCalls();
840
841 // Initialize any stack temporaries we introduced.
842 Changed |= FinishStackTemporaries();
843
Chris Lattner44556082010-08-29 19:54:28 +0000844 // Turn any stack temporaries into registers.
Bill Wendling8bedf972009-10-29 00:37:35 +0000845 if (!CompileFast)
846 Changed |= PromoteStackTemporaries();
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000847
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000848 Changed |= HandleURoRInvokes();
849
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000850 LandingPads.clear();
851
852 return Changed;
853}