blob: d2b129a448ee1ae73424ca055c46ee0866b999fa [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");
John McCalld7c10862011-05-28 07:45:59 +000033STATISTIC(NumResumesLowered, "Number of eh.resume calls lowered");
Bill Wendling8bedf972009-10-29 00:37:35 +000034STATISTIC(NumExceptionValuesMoved, "Number of eh.exception calls moved");
Duncan Sandsb0f1e172009-05-22 20:36:31 +000035
36namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000037 class DwarfEHPrepare : public FunctionPass {
Dan Gohman55e59c12010-04-19 19:05:59 +000038 const TargetMachine *TM;
Duncan Sandsb0f1e172009-05-22 20:36:31 +000039 const TargetLowering *TLI;
Duncan Sandsb0f1e172009-05-22 20:36:31 +000040
41 // The eh.exception intrinsic.
Bill Wendling8bedf972009-10-29 00:37:35 +000042 Function *ExceptionValueIntrinsic;
Duncan Sandsb0f1e172009-05-22 20:36:31 +000043
Bill Wendling2ad4aca2010-03-26 23:41:30 +000044 // The eh.selector intrinsic.
45 Function *SelectorIntrinsic;
46
Bill Wendling49ad7312010-10-29 07:46:01 +000047 // _Unwind_Resume_or_Rethrow or _Unwind_SjLj_Resume call.
Bill Wendling2ad4aca2010-03-26 23:41:30 +000048 Constant *URoR;
49
50 // The EH language-specific catch-all type.
51 GlobalVariable *EHCatchAllValue;
52
Duncan Sandsb0f1e172009-05-22 20:36:31 +000053 // _Unwind_Resume or the target equivalent.
54 Constant *RewindFunction;
55
Duncan Sands22efc182010-08-31 09:05:06 +000056 // We both use and preserve dominator info.
Duncan Sandsb0f1e172009-05-22 20:36:31 +000057 DominatorTree *DT;
Duncan Sandsb0f1e172009-05-22 20:36:31 +000058
59 // The function we are running on.
60 Function *F;
61
62 // The landing pads for this function.
63 typedef SmallPtrSet<BasicBlock*, 8> BBSet;
64 BBSet LandingPads;
65
Bill Wendling35adbb32011-08-17 19:48:49 +000066 bool InsertUnwindResumeCalls();
67
Duncan Sandsb0f1e172009-05-22 20:36:31 +000068 bool NormalizeLandingPads();
John McCalld7c10862011-05-28 07:45:59 +000069 bool LowerUnwindsAndResumes();
Duncan Sandsb0f1e172009-05-22 20:36:31 +000070 bool MoveExceptionValueCalls();
Duncan Sandsb0f1e172009-05-22 20:36:31 +000071
72 Instruction *CreateExceptionValueCall(BasicBlock *BB);
Duncan Sandsb0f1e172009-05-22 20:36:31 +000073
Bill Wendlingbfbd8532010-03-27 01:19:12 +000074 /// CleanupSelectors - Any remaining eh.selector intrinsic calls which still
Bill Wendling23295cc2010-07-26 22:36:52 +000075 /// use the "llvm.eh.catch.all.value" call need to convert to using its
Bill Wendlingbfbd8532010-03-27 01:19:12 +000076 /// initializer instead.
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +000077 bool CleanupSelectors(SmallPtrSet<IntrinsicInst*, 32> &Sels);
Bill Wendlingbfbd8532010-03-27 01:19:12 +000078
Bill Wendlingefbf3062010-06-24 18:49:10 +000079 bool HasCatchAllInSelector(IntrinsicInst *);
Bill Wendling94366112010-06-12 02:34:29 +000080
Bill Wendlingbfde6442010-03-29 23:02:46 +000081 /// FindAllCleanupSelectors - Find all eh.selector calls that are clean-ups.
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +000082 void FindAllCleanupSelectors(SmallPtrSet<IntrinsicInst*, 32> &Sels,
83 SmallPtrSet<IntrinsicInst*, 32> &CatchAllSels);
Bill Wendlingbfde6442010-03-29 23:02:46 +000084
85 /// FindAllURoRInvokes - Find all URoR invokes in the function.
86 void FindAllURoRInvokes(SmallPtrSet<InvokeInst*, 32> &URoRInvokes);
87
Bill Wendling49ad7312010-10-29 07:46:01 +000088 /// HandleURoRInvokes - Handle invokes of "_Unwind_Resume_or_Rethrow" or
89 /// "_Unwind_SjLj_Resume" calls. The "unwind" part of these invokes jump to
90 /// a landing pad within the current function. This is a candidate to merge
91 /// the selector associated with the URoR invoke with the one from the
92 /// URoR's landing pad.
Bill Wendling2ad4aca2010-03-26 23:41:30 +000093 bool HandleURoRInvokes();
94
Bill Wendling201f5f02010-03-29 23:37:07 +000095 /// FindSelectorAndURoR - Find the eh.selector call and URoR call associated
96 /// with the eh.exception call. This recursively looks past instructions
97 /// which don't change the EH pointer value, like casts or PHI nodes.
98 bool FindSelectorAndURoR(Instruction *Inst, bool &URoRInvoke,
Bill Wendling28cc1aa2011-03-15 01:03:17 +000099 SmallPtrSet<IntrinsicInst*, 8> &SelCalls,
100 SmallPtrSet<PHINode*, 32> &SeenPHIs);
Bill Wendling201f5f02010-03-29 23:37:07 +0000101
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000102 public:
103 static char ID; // Pass identification, replacement for typeid.
Duncan Sands22efc182010-08-31 09:05:06 +0000104 DwarfEHPrepare(const TargetMachine *tm) :
Owen Anderson90c579d2010-08-06 18:33:48 +0000105 FunctionPass(ID), TM(tm), TLI(TM->getTargetLowering()),
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000106 ExceptionValueIntrinsic(0), SelectorIntrinsic(0),
Owen Anderson081c34b2010-10-19 17:21:58 +0000107 URoR(0), EHCatchAllValue(0), RewindFunction(0) {
108 initializeDominatorTreePass(*PassRegistry::getPassRegistry());
109 }
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000110
111 virtual bool runOnFunction(Function &Fn);
112
Duncan Sands850fcd42010-09-03 08:31:48 +0000113 // getAnalysisUsage - We need the dominator tree for handling URoR.
Bill Wendling8bedf972009-10-29 00:37:35 +0000114 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Duncan Sands22efc182010-08-31 09:05:06 +0000115 AU.addRequired<DominatorTree>();
Bill Wendling8bedf972009-10-29 00:37:35 +0000116 AU.addPreserved<DominatorTree>();
Bill Wendling8bedf972009-10-29 00:37:35 +0000117 }
118
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000119 const char *getPassName() const {
120 return "Exception handling preparation";
121 }
122
123 };
124} // end anonymous namespace
125
126char DwarfEHPrepare::ID = 0;
127
Duncan Sands22efc182010-08-31 09:05:06 +0000128FunctionPass *llvm::createDwarfEHPass(const TargetMachine *tm) {
129 return new DwarfEHPrepare(tm);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000130}
131
Bill Wendlingefbf3062010-06-24 18:49:10 +0000132/// HasCatchAllInSelector - Return true if the intrinsic instruction has a
133/// catch-all.
134bool DwarfEHPrepare::HasCatchAllInSelector(IntrinsicInst *II) {
135 if (!EHCatchAllValue) return false;
Bill Wendling94366112010-06-12 02:34:29 +0000136
Gabor Greif2bf4b3b2010-06-25 11:25:30 +0000137 unsigned ArgIdx = II->getNumArgOperands() - 1;
138 GlobalVariable *GV = dyn_cast<GlobalVariable>(II->getArgOperand(ArgIdx));
Bill Wendlingefbf3062010-06-24 18:49:10 +0000139 return GV == EHCatchAllValue;
Bill Wendling94366112010-06-12 02:34:29 +0000140}
141
Bill Wendlingbfde6442010-03-29 23:02:46 +0000142/// FindAllCleanupSelectors - Find all eh.selector calls that are clean-ups.
143void DwarfEHPrepare::
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000144FindAllCleanupSelectors(SmallPtrSet<IntrinsicInst*, 32> &Sels,
145 SmallPtrSet<IntrinsicInst*, 32> &CatchAllSels) {
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000146 for (Value::use_iterator
Bill Wendlingbfde6442010-03-29 23:02:46 +0000147 I = SelectorIntrinsic->use_begin(),
148 E = SelectorIntrinsic->use_end(); I != E; ++I) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000149 IntrinsicInst *II = cast<IntrinsicInst>(*I);
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000150
Bill Wendling94366112010-06-12 02:34:29 +0000151 if (II->getParent()->getParent() != F)
152 continue;
Bill Wendlingbfde6442010-03-29 23:02:46 +0000153
Bill Wendlingefbf3062010-06-24 18:49:10 +0000154 if (!HasCatchAllInSelector(II))
Bill Wendling94366112010-06-12 02:34:29 +0000155 Sels.insert(II);
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000156 else
157 CatchAllSels.insert(II);
Bill Wendlingbfde6442010-03-29 23:02:46 +0000158 }
159}
160
161/// FindAllURoRInvokes - Find all URoR invokes in the function.
162void DwarfEHPrepare::
163FindAllURoRInvokes(SmallPtrSet<InvokeInst*, 32> &URoRInvokes) {
164 for (Value::use_iterator
165 I = URoR->use_begin(),
166 E = URoR->use_end(); I != E; ++I) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000167 if (InvokeInst *II = dyn_cast<InvokeInst>(*I))
Bill Wendlingbfde6442010-03-29 23:02:46 +0000168 URoRInvokes.insert(II);
169 }
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000170}
171
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000172/// CleanupSelectors - Any remaining eh.selector intrinsic calls which still use
Bill Wendling23295cc2010-07-26 22:36:52 +0000173/// the "llvm.eh.catch.all.value" call need to convert to using its
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000174/// initializer instead.
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000175bool DwarfEHPrepare::CleanupSelectors(SmallPtrSet<IntrinsicInst*, 32> &Sels) {
Bill Wendlingbfde6442010-03-29 23:02:46 +0000176 if (!EHCatchAllValue) return false;
177
178 if (!SelectorIntrinsic) {
179 SelectorIntrinsic =
180 Intrinsic::getDeclaration(F->getParent(), Intrinsic::eh_selector);
181 if (!SelectorIntrinsic) return false;
182 }
183
Bill Wendling43de15f2010-03-27 01:22:38 +0000184 bool Changed = false;
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000185 for (SmallPtrSet<IntrinsicInst*, 32>::iterator
186 I = Sels.begin(), E = Sels.end(); I != E; ++I) {
187 IntrinsicInst *Sel = *I;
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000188
Bill Wendling23295cc2010-07-26 22:36:52 +0000189 // Index of the "llvm.eh.catch.all.value" variable.
Gabor Greif9d677682010-06-29 13:03:46 +0000190 unsigned OpIdx = Sel->getNumArgOperands() - 1;
191 GlobalVariable *GV = dyn_cast<GlobalVariable>(Sel->getArgOperand(OpIdx));
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000192 if (GV != EHCatchAllValue) continue;
Gabor Greif9d677682010-06-29 13:03:46 +0000193 Sel->setArgOperand(OpIdx, EHCatchAllValue->getInitializer());
Bill Wendling43de15f2010-03-27 01:22:38 +0000194 Changed = true;
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000195 }
Bill Wendling43de15f2010-03-27 01:22:38 +0000196
197 return Changed;
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000198}
199
Bill Wendling201f5f02010-03-29 23:37:07 +0000200/// FindSelectorAndURoR - Find the eh.selector call associated with the
201/// eh.exception call. And indicate if there is a URoR "invoke" associated with
202/// the eh.exception call. This recursively looks past instructions which don't
203/// change the EH pointer value, like casts or PHI nodes.
204bool
205DwarfEHPrepare::FindSelectorAndURoR(Instruction *Inst, bool &URoRInvoke,
Bill Wendling28cc1aa2011-03-15 01:03:17 +0000206 SmallPtrSet<IntrinsicInst*, 8> &SelCalls,
207 SmallPtrSet<PHINode*, 32> &SeenPHIs) {
Bill Wendling201f5f02010-03-29 23:37:07 +0000208 bool Changed = false;
209
Bill Wendling201f5f02010-03-29 23:37:07 +0000210 for (Value::use_iterator
211 I = Inst->use_begin(), E = Inst->use_end(); I != E; ++I) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000212 Instruction *II = dyn_cast<Instruction>(*I);
Bill Wendling201f5f02010-03-29 23:37:07 +0000213 if (!II || II->getParent()->getParent() != F) continue;
214
215 if (IntrinsicInst *Sel = dyn_cast<IntrinsicInst>(II)) {
216 if (Sel->getIntrinsicID() == Intrinsic::eh_selector)
217 SelCalls.insert(Sel);
218 } else if (InvokeInst *Invoke = dyn_cast<InvokeInst>(II)) {
219 if (Invoke->getCalledFunction() == URoR)
220 URoRInvoke = true;
221 } else if (CastInst *CI = dyn_cast<CastInst>(II)) {
Bill Wendling28cc1aa2011-03-15 01:03:17 +0000222 Changed |= FindSelectorAndURoR(CI, URoRInvoke, SelCalls, SeenPHIs);
Bill Wendling201f5f02010-03-29 23:37:07 +0000223 } else if (PHINode *PN = dyn_cast<PHINode>(II)) {
224 if (SeenPHIs.insert(PN))
225 // Don't process a PHI node more than once.
Bill Wendling28cc1aa2011-03-15 01:03:17 +0000226 Changed |= FindSelectorAndURoR(PN, URoRInvoke, SelCalls, SeenPHIs);
Bill Wendling201f5f02010-03-29 23:37:07 +0000227 }
228 }
229
230 return Changed;
231}
232
Bill Wendling49ad7312010-10-29 07:46:01 +0000233/// HandleURoRInvokes - Handle invokes of "_Unwind_Resume_or_Rethrow" or
234/// "_Unwind_SjLj_Resume" calls. The "unwind" part of these invokes jump to a
235/// landing pad within the current function. This is a candidate to merge the
236/// selector associated with the URoR invoke with the one from the URoR's
237/// landing pad.
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000238bool DwarfEHPrepare::HandleURoRInvokes() {
239 if (!EHCatchAllValue) {
240 EHCatchAllValue =
Bill Wendling23295cc2010-07-26 22:36:52 +0000241 F->getParent()->getNamedGlobal("llvm.eh.catch.all.value");
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000242 if (!EHCatchAllValue) return false;
243 }
244
Bill Wendlingbfbd8532010-03-27 01:19:12 +0000245 if (!SelectorIntrinsic) {
246 SelectorIntrinsic =
247 Intrinsic::getDeclaration(F->getParent(), Intrinsic::eh_selector);
248 if (!SelectorIntrinsic) return false;
249 }
250
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000251 SmallPtrSet<IntrinsicInst*, 32> Sels;
252 SmallPtrSet<IntrinsicInst*, 32> CatchAllSels;
253 FindAllCleanupSelectors(Sels, CatchAllSels);
254
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000255 if (!URoR) {
256 URoR = F->getParent()->getFunction("_Unwind_Resume_or_Rethrow");
Bill Wendlingedac4922011-06-01 01:49:35 +0000257 if (!URoR) return CleanupSelectors(CatchAllSels);
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000258 }
259
Bill Wendlingbfde6442010-03-29 23:02:46 +0000260 SmallPtrSet<InvokeInst*, 32> URoRInvokes;
Bill Wendlingbfde6442010-03-29 23:02:46 +0000261 FindAllURoRInvokes(URoRInvokes);
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000262
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000263 SmallPtrSet<IntrinsicInst*, 32> SelsToConvert;
264
Bill Wendlingbfde6442010-03-29 23:02:46 +0000265 for (SmallPtrSet<IntrinsicInst*, 32>::iterator
266 SI = Sels.begin(), SE = Sels.end(); SI != SE; ++SI) {
267 const BasicBlock *SelBB = (*SI)->getParent();
268 for (SmallPtrSet<InvokeInst*, 32>::iterator
269 UI = URoRInvokes.begin(), UE = URoRInvokes.end(); UI != UE; ++UI) {
270 const BasicBlock *URoRBB = (*UI)->getParent();
Dan Gohmance0fe5d2010-07-26 17:38:15 +0000271 if (DT->dominates(SelBB, URoRBB)) {
Bill Wendlingbfde6442010-03-29 23:02:46 +0000272 SelsToConvert.insert(*SI);
273 break;
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000274 }
275 }
276 }
277
Bill Wendlingbfde6442010-03-29 23:02:46 +0000278 bool Changed = false;
279
Bill Wendling201f5f02010-03-29 23:37:07 +0000280 if (Sels.size() != SelsToConvert.size()) {
281 // If we haven't been able to convert all of the clean-up selectors, then
282 // loop through the slow way to see if they still need to be converted.
283 if (!ExceptionValueIntrinsic) {
284 ExceptionValueIntrinsic =
285 Intrinsic::getDeclaration(F->getParent(), Intrinsic::eh_exception);
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000286 if (!ExceptionValueIntrinsic)
287 return CleanupSelectors(CatchAllSels);
Bill Wendling201f5f02010-03-29 23:37:07 +0000288 }
289
290 for (Value::use_iterator
291 I = ExceptionValueIntrinsic->use_begin(),
292 E = ExceptionValueIntrinsic->use_end(); I != E; ++I) {
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000293 IntrinsicInst *EHPtr = dyn_cast<IntrinsicInst>(*I);
Bill Wendling201f5f02010-03-29 23:37:07 +0000294 if (!EHPtr || EHPtr->getParent()->getParent() != F) continue;
295
Bill Wendling201f5f02010-03-29 23:37:07 +0000296 bool URoRInvoke = false;
297 SmallPtrSet<IntrinsicInst*, 8> SelCalls;
Bill Wendling28cc1aa2011-03-15 01:03:17 +0000298 SmallPtrSet<PHINode*, 32> SeenPHIs;
299 Changed |= FindSelectorAndURoR(EHPtr, URoRInvoke, SelCalls, SeenPHIs);
Bill Wendling201f5f02010-03-29 23:37:07 +0000300
301 if (URoRInvoke) {
302 // This EH pointer is being used by an invoke of an URoR instruction and
303 // an eh.selector intrinsic call. If the eh.selector is a 'clean-up', we
304 // need to convert it to a 'catch-all'.
305 for (SmallPtrSet<IntrinsicInst*, 8>::iterator
Bill Wendling94366112010-06-12 02:34:29 +0000306 SI = SelCalls.begin(), SE = SelCalls.end(); SI != SE; ++SI)
Bill Wendlingefbf3062010-06-24 18:49:10 +0000307 if (!HasCatchAllInSelector(*SI))
Bill Wendling94366112010-06-12 02:34:29 +0000308 SelsToConvert.insert(*SI);
Bill Wendling201f5f02010-03-29 23:37:07 +0000309 }
310 }
311 }
312
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000313 if (!SelsToConvert.empty()) {
314 // Convert all clean-up eh.selectors, which are associated with "invokes" of
315 // URoR calls, into catch-all eh.selectors.
316 Changed = true;
317
318 for (SmallPtrSet<IntrinsicInst*, 8>::iterator
319 SI = SelsToConvert.begin(), SE = SelsToConvert.end();
320 SI != SE; ++SI) {
321 IntrinsicInst *II = *SI;
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000322
323 // Use the exception object pointer and the personality function
324 // from the original selector.
Gabor Greif2bf4b3b2010-06-25 11:25:30 +0000325 CallSite CS(II);
326 IntrinsicInst::op_iterator I = CS.arg_begin();
Gabor Greif2bf4b3b2010-06-25 11:25:30 +0000327 IntrinsicInst::op_iterator E = CS.arg_end();
328 IntrinsicInst::op_iterator B = prior(E);
329
330 // Exclude last argument if it is an integer.
331 if (isa<ConstantInt>(B)) E = B;
Bill Wendling94366112010-06-12 02:34:29 +0000332
Gabor Greif32621ad2010-06-28 16:40:52 +0000333 // Add exception object pointer (front).
334 // Add personality function (next).
335 // Add in any filter IDs (rest).
336 SmallVector<Value*, 8> Args(I, E);
Bill Wendling94366112010-06-12 02:34:29 +0000337
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000338 Args.push_back(EHCatchAllValue->getInitializer()); // Catch-all indicator.
339
340 CallInst *NewSelector =
Jay Foada3efbb12011-07-15 08:37:34 +0000341 CallInst::Create(SelectorIntrinsic, Args, "eh.sel.catch.all", II);
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000342
343 NewSelector->setTailCall(II->isTailCall());
344 NewSelector->setAttributes(II->getAttributes());
345 NewSelector->setCallingConv(II->getCallingConv());
346
347 II->replaceAllUsesWith(NewSelector);
348 II->eraseFromParent();
349 }
350 }
351
Bill Wendlingd9e3b2b2010-06-30 22:49:53 +0000352 Changed |= CleanupSelectors(CatchAllSels);
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000353 return Changed;
354}
355
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000356/// NormalizeLandingPads - Normalize and discover landing pads, noting them
357/// in the LandingPads set. A landing pad is normal if the only CFG edges
Eric Christopherec26bf72009-09-15 21:56:46 +0000358/// that end at it are unwind edges from invoke instructions. If we inlined
359/// through an invoke we could have a normal branch from the previous
360/// unwind block through to the landing pad for the original invoke.
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000361/// Abnormal landing pads are fixed up by redirecting all unwind edges to
362/// a new basic block which falls through to the original.
363bool DwarfEHPrepare::NormalizeLandingPads() {
364 bool Changed = false;
365
Dan Gohman55e59c12010-04-19 19:05:59 +0000366 const MCAsmInfo *MAI = TM->getMCAsmInfo();
Jim Grosbach8d77cc82010-01-20 23:03:55 +0000367 bool usingSjLjEH = MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
368
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000369 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
370 TerminatorInst *TI = I->getTerminator();
371 if (!isa<InvokeInst>(TI))
372 continue;
373 BasicBlock *LPad = TI->getSuccessor(1);
374 // Skip landing pads that have already been normalized.
375 if (LandingPads.count(LPad))
376 continue;
377
378 // Check that only invoke unwind edges end at the landing pad.
379 bool OnlyUnwoundTo = true;
Jim Grosbach8d77cc82010-01-20 23:03:55 +0000380 bool SwitchOK = usingSjLjEH;
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000381 for (pred_iterator PI = pred_begin(LPad), PE = pred_end(LPad);
382 PI != PE; ++PI) {
383 TerminatorInst *PT = (*PI)->getTerminator();
Jim Grosbach8d77cc82010-01-20 23:03:55 +0000384 // The SjLj dispatch block uses a switch instruction. This is effectively
385 // an unwind edge, so we can disregard it here. There will only ever
386 // be one dispatch, however, so if there are multiple switches, one
387 // of them truly is a normal edge, not an unwind edge.
388 if (SwitchOK && isa<SwitchInst>(PT)) {
389 SwitchOK = false;
390 continue;
391 }
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000392 if (!isa<InvokeInst>(PT) || LPad == PT->getSuccessor(0)) {
393 OnlyUnwoundTo = false;
394 break;
395 }
396 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000397
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000398 if (OnlyUnwoundTo) {
399 // Only unwind edges lead to the landing pad. Remember the landing pad.
400 LandingPads.insert(LPad);
401 continue;
402 }
403
404 // At least one normal edge ends at the landing pad. Redirect the unwind
405 // edges to a new basic block which falls through into this one.
406
407 // Create the new basic block.
Bill Wendling8bedf972009-10-29 00:37:35 +0000408 BasicBlock *NewBB = BasicBlock::Create(F->getContext(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000409 LPad->getName() + "_unwind_edge");
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000410
411 // Insert it into the function right before the original landing pad.
412 LPad->getParent()->getBasicBlockList().insert(LPad, NewBB);
413
414 // Redirect unwind edges from the original landing pad to NewBB.
415 for (pred_iterator PI = pred_begin(LPad), PE = pred_end(LPad); PI != PE; ) {
416 TerminatorInst *PT = (*PI++)->getTerminator();
417 if (isa<InvokeInst>(PT) && PT->getSuccessor(1) == LPad)
418 // Unwind to the new block.
419 PT->setSuccessor(1, NewBB);
420 }
421
422 // If there are any PHI nodes in LPad, we need to update them so that they
423 // merge incoming values from NewBB instead.
424 for (BasicBlock::iterator II = LPad->begin(); isa<PHINode>(II); ++II) {
425 PHINode *PN = cast<PHINode>(II);
426 pred_iterator PB = pred_begin(NewBB), PE = pred_end(NewBB);
427
428 // Check to see if all of the values coming in via unwind edges are the
429 // same. If so, we don't need to create a new PHI node.
430 Value *InVal = PN->getIncomingValueForBlock(*PB);
431 for (pred_iterator PI = PB; PI != PE; ++PI) {
432 if (PI != PB && InVal != PN->getIncomingValueForBlock(*PI)) {
433 InVal = 0;
434 break;
435 }
436 }
437
438 if (InVal == 0) {
439 // Different unwind edges have different values. Create a new PHI node
440 // in NewBB.
Jay Foad3ecfc862011-03-30 11:28:46 +0000441 PHINode *NewPN = PHINode::Create(PN->getType(),
442 PN->getNumIncomingValues(),
443 PN->getName()+".unwind", NewBB);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000444 // Add an entry for each unwind edge, using the value from the old PHI.
445 for (pred_iterator PI = PB; PI != PE; ++PI)
446 NewPN->addIncoming(PN->getIncomingValueForBlock(*PI), *PI);
447
448 // Now use this new PHI as the common incoming value for NewBB in PN.
449 InVal = NewPN;
450 }
451
452 // Revector exactly one entry in the PHI node to come from NewBB
453 // and delete all other entries that come from unwind edges. If
454 // there are both normal and unwind edges from the same predecessor,
455 // this leaves an entry for the normal edge.
456 for (pred_iterator PI = PB; PI != PE; ++PI)
457 PN->removeIncomingValue(*PI);
458 PN->addIncoming(InVal, NewBB);
459 }
460
461 // Add a fallthrough from NewBB to the original landing pad.
462 BranchInst::Create(LPad, NewBB);
463
Duncan Sands22efc182010-08-31 09:05:06 +0000464 // Now update DominatorTree analysis information.
465 DT->splitBlock(NewBB);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000466
467 // Remember the newly constructed landing pad. The original landing pad
468 // LPad is no longer a landing pad now that all unwind edges have been
469 // revectored to NewBB.
470 LandingPads.insert(NewBB);
471 ++NumLandingPadsSplit;
472 Changed = true;
473 }
474
475 return Changed;
476}
477
478/// LowerUnwinds - Turn unwind instructions into calls to _Unwind_Resume,
479/// rethrowing any previously caught exception. This will crash horribly
480/// at runtime if there is no such exception: using unwind to throw a new
481/// exception is currently not supported.
John McCalld7c10862011-05-28 07:45:59 +0000482bool DwarfEHPrepare::LowerUnwindsAndResumes() {
483 SmallVector<Instruction*, 16> ResumeInsts;
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000484
John McCalld7c10862011-05-28 07:45:59 +0000485 for (Function::iterator fi = F->begin(), fe = F->end(); fi != fe; ++fi) {
486 for (BasicBlock::iterator bi = fi->begin(), be = fi->end(); bi != be; ++bi){
487 if (isa<UnwindInst>(bi))
488 ResumeInsts.push_back(bi);
489 else if (CallInst *call = dyn_cast<CallInst>(bi))
490 if (Function *fn = dyn_cast<Function>(call->getCalledValue()))
491 if (fn->getName() == "llvm.eh.resume")
492 ResumeInsts.push_back(bi);
493 }
Bill Wendling43488712009-09-14 20:52:37 +0000494 }
495
John McCalld7c10862011-05-28 07:45:59 +0000496 if (ResumeInsts.empty()) return false;
Bill Wendling43488712009-09-14 20:52:37 +0000497
498 // Find the rewind function if we didn't already.
499 if (!RewindFunction) {
John McCalld7c10862011-05-28 07:45:59 +0000500 LLVMContext &Ctx = ResumeInsts[0]->getContext();
Bill Wendling8bedf972009-10-29 00:37:35 +0000501 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
Jay Foad5fdd6c82011-07-12 14:06:48 +0000502 Type::getInt8PtrTy(Ctx), false);
Bill Wendling43488712009-09-14 20:52:37 +0000503 const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME);
504 RewindFunction = F->getParent()->getOrInsertFunction(RewindName, FTy);
505 }
506
Bill Wendling8bedf972009-10-29 00:37:35 +0000507 bool Changed = false;
508
John McCalld7c10862011-05-28 07:45:59 +0000509 for (SmallVectorImpl<Instruction*>::iterator
510 I = ResumeInsts.begin(), E = ResumeInsts.end(); I != E; ++I) {
511 Instruction *RI = *I;
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000512
John McCalld7c10862011-05-28 07:45:59 +0000513 // Replace the resuming instruction with a call to _Unwind_Resume (or the
514 // appropriate target equivalent).
515
516 llvm::Value *ExnValue;
517 if (isa<UnwindInst>(RI))
518 ExnValue = CreateExceptionValueCall(RI->getParent());
519 else
520 ExnValue = cast<CallInst>(RI)->getArgOperand(0);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000521
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000522 // Create the call...
John McCalld7c10862011-05-28 07:45:59 +0000523 CallInst *CI = CallInst::Create(RewindFunction, ExnValue, "", RI);
Eric Christopher82f149d2009-09-04 01:14:14 +0000524 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000525
John McCalld7c10862011-05-28 07:45:59 +0000526 // ...followed by an UnreachableInst, if it was an unwind.
527 // Calls to llvm.eh.resume are typically already followed by this.
528 if (isa<UnwindInst>(RI))
529 new UnreachableInst(RI->getContext(), RI);
530
John McCalld7c10862011-05-28 07:45:59 +0000531 if (isa<UnwindInst>(RI))
532 ++NumUnwindsLowered;
533 else
534 ++NumResumesLowered;
Benjamin Kramer33858012011-05-28 11:48:37 +0000535
536 // Nuke the resume instruction.
537 RI->eraseFromParent();
538
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000539 Changed = true;
540 }
541
542 return Changed;
543}
544
545/// MoveExceptionValueCalls - Ensure that eh.exception is only ever called from
Duncan Sands850fcd42010-09-03 08:31:48 +0000546/// landing pads by replacing calls outside of landing pads with direct use of
547/// a register holding the appropriate value; this requires adding calls inside
548/// all landing pads to initialize the register. Also, move eh.exception calls
549/// inside landing pads to the start of the landing pad (optional, but may make
550/// things simpler for later passes).
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000551bool DwarfEHPrepare::MoveExceptionValueCalls() {
552 // If the eh.exception intrinsic is not declared in the module then there is
553 // nothing to do. Speed up compilation by checking for this common case.
Bill Wendling8bedf972009-10-29 00:37:35 +0000554 if (!ExceptionValueIntrinsic &&
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000555 !F->getParent()->getFunction(Intrinsic::getName(Intrinsic::eh_exception)))
556 return false;
557
558 bool Changed = false;
559
Duncan Sands850fcd42010-09-03 08:31:48 +0000560 // Move calls to eh.exception that are inside a landing pad to the start of
561 // the landing pad.
562 for (BBSet::const_iterator LI = LandingPads.begin(), LE = LandingPads.end();
Eric Christopheradc581f2010-09-01 17:29:10 +0000563 LI != LE; ++LI) {
Duncan Sands850fcd42010-09-03 08:31:48 +0000564 BasicBlock *LP = *LI;
565 for (BasicBlock::iterator II = LP->getFirstNonPHIOrDbg(), IE = LP->end();
566 II != IE;)
567 if (EHExceptionInst *EI = dyn_cast<EHExceptionInst>(II++)) {
568 // Found a call to eh.exception.
569 if (!EI->use_empty()) {
570 // If there is already a call to eh.exception at the start of the
571 // landing pad, then get hold of it; otherwise create such a call.
572 Value *CallAtStart = CreateExceptionValueCall(LP);
573
574 // If the call was at the start of a landing pad then leave it alone.
575 if (EI == CallAtStart)
576 continue;
577 EI->replaceAllUsesWith(CallAtStart);
578 }
579 EI->eraseFromParent();
580 ++NumExceptionValuesMoved;
581 Changed = true;
582 }
Duncan Sandsfb4e8ab2010-09-01 14:07:47 +0000583 }
584
Duncan Sands850fcd42010-09-03 08:31:48 +0000585 // Look for calls to eh.exception that are not in a landing pad. If one is
586 // found, then a register that holds the exception value will be created in
587 // each landing pad, and the SSAUpdater will be used to compute the values
588 // returned by eh.exception calls outside of landing pads.
589 SSAUpdater SSA;
590
591 // Remember where we found the eh.exception call, to avoid rescanning earlier
592 // basic blocks which we already know contain no eh.exception calls.
593 bool FoundCallOutsideLandingPad = false;
594 Function::iterator BB = F->begin();
595 for (Function::iterator BE = F->end(); BB != BE; ++BB) {
596 // Skip over landing pads.
597 if (LandingPads.count(BB))
598 continue;
599
600 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end();
601 II != IE; ++II)
602 if (isa<EHExceptionInst>(II)) {
603 SSA.Initialize(II->getType(), II->getName());
604 FoundCallOutsideLandingPad = true;
605 break;
606 }
607
608 if (FoundCallOutsideLandingPad)
609 break;
610 }
611
612 // If all calls to eh.exception are in landing pads then we are done.
613 if (!FoundCallOutsideLandingPad)
614 return Changed;
615
616 // Add a call to eh.exception at the start of each landing pad, and tell the
617 // SSAUpdater that this is the value produced by the landing pad.
618 for (BBSet::iterator LI = LandingPads.begin(), LE = LandingPads.end();
619 LI != LE; ++LI)
620 SSA.AddAvailableValue(*LI, CreateExceptionValueCall(*LI));
621
622 // Now turn all calls to eh.exception that are not in a landing pad into a use
623 // of the appropriate register.
624 for (Function::iterator BE = F->end(); BB != BE; ++BB) {
625 // Skip over landing pads.
626 if (LandingPads.count(BB))
627 continue;
628
629 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end();
630 II != IE;)
631 if (EHExceptionInst *EI = dyn_cast<EHExceptionInst>(II++)) {
632 // Found a call to eh.exception, replace it with the value from any
633 // upstream landing pad(s).
634 EI->replaceAllUsesWith(SSA.GetValueAtEndOfBlock(BB));
635 EI->eraseFromParent();
636 ++NumExceptionValuesMoved;
637 }
638 }
639
640 return true;
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000641}
642
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000643/// CreateExceptionValueCall - Insert a call to the eh.exception intrinsic at
644/// the start of the basic block (unless there already is one, in which case
645/// the existing call is returned).
646Instruction *DwarfEHPrepare::CreateExceptionValueCall(BasicBlock *BB) {
Dale Johannesen7249ef02010-04-02 21:49:27 +0000647 Instruction *Start = BB->getFirstNonPHIOrDbg();
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000648 // Is this a call to eh.exception?
649 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Start))
650 if (CI->getIntrinsicID() == Intrinsic::eh_exception)
651 // Reuse the existing call.
652 return Start;
653
654 // Find the eh.exception intrinsic if we didn't already.
Bill Wendling8bedf972009-10-29 00:37:35 +0000655 if (!ExceptionValueIntrinsic)
656 ExceptionValueIntrinsic = Intrinsic::getDeclaration(F->getParent(),
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000657 Intrinsic::eh_exception);
658
659 // Create the call.
Bill Wendling8bedf972009-10-29 00:37:35 +0000660 return CallInst::Create(ExceptionValueIntrinsic, "eh.value.call", Start);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000661}
662
Bill Wendling35adbb32011-08-17 19:48:49 +0000663/// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present
664/// into calls to the appropriate _Unwind_Resume function.
665bool DwarfEHPrepare::InsertUnwindResumeCalls() {
Bill Wendling09908c42011-08-25 23:48:11 +0000666 bool UsesNewEH = false;
Bill Wendling35adbb32011-08-17 19:48:49 +0000667 SmallVector<ResumeInst*, 16> Resumes;
Bill Wendling09908c42011-08-25 23:48:11 +0000668 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
669 TerminatorInst *TI = I->getTerminator();
670 if (ResumeInst *RI = dyn_cast<ResumeInst>(TI))
671 Resumes.push_back(RI);
672 else if (InvokeInst *II = dyn_cast<InvokeInst>(TI))
673 UsesNewEH = II->getUnwindDest()->isLandingPad();
674 }
Bill Wendling35adbb32011-08-17 19:48:49 +0000675
676 if (Resumes.empty())
Bill Wendling09908c42011-08-25 23:48:11 +0000677 return UsesNewEH;
Bill Wendling35adbb32011-08-17 19:48:49 +0000678
679 // Find the rewind function if we didn't already.
680 if (!RewindFunction) {
681 LLVMContext &Ctx = Resumes[0]->getContext();
682 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
683 Type::getInt8PtrTy(Ctx), false);
684 const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME);
685 RewindFunction = F->getParent()->getOrInsertFunction(RewindName, FTy);
686 }
687
688 // Create the basic block where the _Unwind_Resume call will live.
689 LLVMContext &Ctx = F->getContext();
690 BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", F);
691 PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), Resumes.size(),
692 "exn.obj", UnwindBB);
693
694 // Extract the exception object from the ResumeInst and add it to the PHI node
695 // that feeds the _Unwind_Resume call.
696 for (SmallVectorImpl<ResumeInst*>::iterator
697 I = Resumes.begin(), E = Resumes.end(); I != E; ++I) {
698 ResumeInst *RI = *I;
699 BranchInst::Create(UnwindBB, RI->getParent());
700 ExtractValueInst *ExnObj = ExtractValueInst::Create(RI->getOperand(0),
701 0, "exn.obj", RI);
702 PN->addIncoming(ExnObj, RI->getParent());
703 RI->eraseFromParent();
704 }
705
706 // Call the function.
707 CallInst *CI = CallInst::Create(RewindFunction, PN, "", UnwindBB);
708 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
709
710 // We never expect _Unwind_Resume to return.
711 new UnreachableInst(Ctx, UnwindBB);
712 return true;
713}
714
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000715bool DwarfEHPrepare::runOnFunction(Function &Fn) {
716 bool Changed = false;
717
718 // Initialize internal state.
Bill Wendling35adbb32011-08-17 19:48:49 +0000719 DT = &getAnalysis<DominatorTree>(); // FIXME: We won't need this with the new EH.
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000720 F = &Fn;
721
Bill Wendling35adbb32011-08-17 19:48:49 +0000722 if (InsertUnwindResumeCalls()) {
723 // FIXME: The reset of this function can go once the new EH is done.
724 LandingPads.clear();
725 return true;
726 }
727
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000728 // Ensure that only unwind edges end at landing pads (a landing pad is a
729 // basic block where an invoke unwind edge ends).
730 Changed |= NormalizeLandingPads();
731
John McCalld7c10862011-05-28 07:45:59 +0000732 // Turn unwind instructions and eh.resume calls into libcalls.
733 Changed |= LowerUnwindsAndResumes();
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000734
Bill Wendling8bedf972009-10-29 00:37:35 +0000735 // TODO: Move eh.selector calls to landing pads and combine them.
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000736
737 // Move eh.exception calls to landing pads.
738 Changed |= MoveExceptionValueCalls();
739
Bill Wendling2ad4aca2010-03-26 23:41:30 +0000740 Changed |= HandleURoRInvokes();
741
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000742 LandingPads.clear();
743
744 return Changed;
745}