blob: 1a8f6b99dd7805cb6a5180ecbb5fc76ad2aad87d [file] [log] [blame]
Chandler Carruth7132e002007-08-04 01:51:18 +00001//===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chandler Carruth7132e002007-08-04 01:51:18 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the auto-upgrade helper functions
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/AutoUpgrade.h"
Anders Carlssonf924f342007-12-14 06:38:54 +000015#include "llvm/Constants.h"
Chandler Carruth7132e002007-08-04 01:51:18 +000016#include "llvm/Function.h"
Bill Wendling45449b12011-08-25 23:22:40 +000017#include "llvm/Instruction.h"
Owen Anderson155dccd82009-07-07 23:43:39 +000018#include "llvm/LLVMContext.h"
Chandler Carruth7132e002007-08-04 01:51:18 +000019#include "llvm/Module.h"
Devang Patel80ae3492009-08-28 23:24:31 +000020#include "llvm/IntrinsicInst.h"
Bill Wendling45449b12011-08-25 23:22:40 +000021#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner8a923e72008-03-12 17:45:29 +000023#include "llvm/ADT/SmallVector.h"
Gabor Greife5406532010-06-23 08:45:32 +000024#include "llvm/Support/CallSite.h"
Bill Wendling45449b12011-08-25 23:22:40 +000025#include "llvm/Support/CFG.h"
Torok Edwin56d06592009-07-11 20:10:48 +000026#include "llvm/Support/ErrorHandling.h"
Eric Christopher64831c62010-04-20 00:59:54 +000027#include "llvm/Support/IRBuilder.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000028#include <cstring>
Chandler Carruth7132e002007-08-04 01:51:18 +000029using namespace llvm;
30
31
Evan Cheng0e179d02007-12-17 22:33:23 +000032static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
Chandler Carruth7132e002007-08-04 01:51:18 +000033 assert(F && "Illegal to upgrade a non-existent Function.");
34
Chandler Carruth7132e002007-08-04 01:51:18 +000035 // Quickly eliminate it, if it's not a candidate.
Chris Lattnerb372f662011-06-18 18:56:39 +000036 StringRef Name = F->getName();
37 if (Name.size() <= 8 || !Name.startswith("llvm."))
Evan Cheng0e179d02007-12-17 22:33:23 +000038 return false;
Chris Lattnerb372f662011-06-18 18:56:39 +000039 Name = Name.substr(5); // Strip off "llvm."
Chandler Carruth7132e002007-08-04 01:51:18 +000040
Chris Lattner229907c2011-07-18 04:54:35 +000041 FunctionType *FTy = F->getFunctionType();
Chandler Carruth7132e002007-08-04 01:51:18 +000042 Module *M = F->getParent();
Chris Lattnerb372f662011-06-18 18:56:39 +000043
44 switch (Name[0]) {
Chandler Carruth7132e002007-08-04 01:51:18 +000045 default: break;
Chandler Carruth7132e002007-08-04 01:51:18 +000046 case 'p':
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +000047 // This upgrades the llvm.prefetch intrinsic to accept one more parameter,
48 // which is a instruction / data cache identifier. The old version only
49 // implicitly accepted the data version.
Chris Lattnerb372f662011-06-18 18:56:39 +000050 if (Name == "prefetch") {
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +000051 // Don't do anything if it has the correct number of arguments already
52 if (FTy->getNumParams() == 4)
53 break;
54
55 assert(FTy->getNumParams() == 3 && "old prefetch takes 3 args!");
56 // We first need to change the name of the old (bad) intrinsic, because
57 // its type is incorrect, but we cannot overload that name. We
58 // arbitrarily unique it here allowing us to construct a correctly named
59 // and typed function below.
Chris Lattnerb372f662011-06-18 18:56:39 +000060 std::string NameTmp = F->getName();
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +000061 F->setName("");
Chris Lattnerb372f662011-06-18 18:56:39 +000062 NewFn = cast<Function>(M->getOrInsertFunction(NameTmp,
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +000063 FTy->getReturnType(),
64 FTy->getParamType(0),
65 FTy->getParamType(1),
66 FTy->getParamType(2),
67 FTy->getParamType(2),
68 (Type*)0));
69 return true;
70 }
71
Chandler Carruth7132e002007-08-04 01:51:18 +000072 break;
Chris Lattnerb372f662011-06-18 18:56:39 +000073 case 'x': {
74 const char *NewFnName = NULL;
75 // This fixes the poorly named crc32 intrinsics.
76 if (Name == "x86.sse42.crc32.8")
77 NewFnName = "llvm.x86.sse42.crc32.32.8";
78 else if (Name == "x86.sse42.crc32.16")
79 NewFnName = "llvm.x86.sse42.crc32.32.16";
80 else if (Name == "x86.sse42.crc32.32")
81 NewFnName = "llvm.x86.sse42.crc32.32.32";
82 else if (Name == "x86.sse42.crc64.8")
83 NewFnName = "llvm.x86.sse42.crc32.64.8";
84 else if (Name == "x86.sse42.crc64.64")
85 NewFnName = "llvm.x86.sse42.crc32.64.64";
86
87 if (NewFnName) {
88 F->setName(NewFnName);
89 NewFn = F;
90 return true;
Chad Rosierb3628842011-05-26 23:13:19 +000091 }
92
Chris Lattnerb372f662011-06-18 18:56:39 +000093 // Calls to these instructions are transformed into unaligned loads.
94 if (Name == "x86.sse.loadu.ps" || Name == "x86.sse2.loadu.dq" ||
95 Name == "x86.sse2.loadu.pd")
Bill Wendlingb902f1d2011-04-13 00:36:11 +000096 return true;
Chris Lattner80ed9dc2011-06-18 06:05:24 +000097
Chris Lattnerb372f662011-06-18 18:56:39 +000098 // Calls to these instructions are transformed into nontemporal stores.
99 if (Name == "x86.sse.movnt.ps" || Name == "x86.sse2.movnt.dq" ||
100 Name == "x86.sse2.movnt.pd" || Name == "x86.sse2.movnt.i")
Bill Wendlingdb0996c2011-05-03 21:11:17 +0000101 return true;
Evan Cheng0e179d02007-12-17 22:33:23 +0000102
Anders Carlssonf924f342007-12-14 06:38:54 +0000103 break;
Chandler Carruth7132e002007-08-04 01:51:18 +0000104 }
Chris Lattnerb372f662011-06-18 18:56:39 +0000105 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000106
107 // This may not belong here. This function is effectively being overloaded
108 // to both detect an intrinsic which needs upgrading, and to provide the
109 // upgraded form of the intrinsic. We should perhaps have two separate
110 // functions for this.
Evan Cheng0e179d02007-12-17 22:33:23 +0000111 return false;
Chandler Carruth7132e002007-08-04 01:51:18 +0000112}
113
Evan Cheng0e179d02007-12-17 22:33:23 +0000114bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
115 NewFn = 0;
116 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
Duncan Sands38ef3a82007-12-03 20:06:50 +0000117
118 // Upgrade intrinsic attributes. This does not change the function.
Evan Cheng0e179d02007-12-17 22:33:23 +0000119 if (NewFn)
120 F = NewFn;
Dale Johannesenb842d522009-02-05 01:49:45 +0000121 if (unsigned id = F->getIntrinsicID())
Devang Patel4c758ea2008-09-25 21:00:45 +0000122 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
Duncan Sands38ef3a82007-12-03 20:06:50 +0000123 return Upgraded;
124}
125
Bill Wendlinge26fffc2010-09-10 18:51:56 +0000126bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000127 // Nothing to do yet.
Bill Wendlinge26fffc2010-09-10 18:51:56 +0000128 return false;
129}
130
Chandler Carruth7132e002007-08-04 01:51:18 +0000131// UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
132// upgraded intrinsic. All argument and return casting must be provided in
133// order to seamlessly integrate with existing context.
134void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Chandler Carruth7132e002007-08-04 01:51:18 +0000135 Function *F = CI->getCalledFunction();
Owen Anderson55f1c092009-08-13 21:58:54 +0000136 LLVMContext &C = CI->getContext();
Gabor Greife5406532010-06-23 08:45:32 +0000137 ImmutableCallSite CS(CI);
138
Chandler Carruth7132e002007-08-04 01:51:18 +0000139 assert(F && "CallInst has no function associated with it.");
Evan Cheng0e179d02007-12-17 22:33:23 +0000140
141 if (!NewFn) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000142 if (F->getName() == "llvm.x86.sse.loadu.ps" ||
143 F->getName() == "llvm.x86.sse2.loadu.dq" ||
144 F->getName() == "llvm.x86.sse2.loadu.pd") {
Bill Wendlingb902f1d2011-04-13 00:36:11 +0000145 // Convert to a native, unaligned load.
Chris Lattner229907c2011-07-18 04:54:35 +0000146 Type *VecTy = CI->getType();
147 Type *IntTy = IntegerType::get(C, 128);
Bill Wendlingb902f1d2011-04-13 00:36:11 +0000148 IRBuilder<> Builder(C);
149 Builder.SetInsertPoint(CI->getParent(), CI);
150
151 Value *BC = Builder.CreateBitCast(CI->getArgOperand(0),
152 PointerType::getUnqual(IntTy),
153 "cast");
154 LoadInst *LI = Builder.CreateLoad(BC, CI->getName());
155 LI->setAlignment(1); // Unaligned load.
156 BC = Builder.CreateBitCast(LI, VecTy, "new.cast");
157
158 // Fix up all the uses with our new load.
159 if (!CI->use_empty())
160 CI->replaceAllUsesWith(BC);
161
162 // Remove intrinsic.
163 CI->eraseFromParent();
Bill Wendlingdb0996c2011-05-03 21:11:17 +0000164 } else if (F->getName() == "llvm.x86.sse.movnt.ps" ||
165 F->getName() == "llvm.x86.sse2.movnt.dq" ||
166 F->getName() == "llvm.x86.sse2.movnt.pd" ||
167 F->getName() == "llvm.x86.sse2.movnt.i") {
168 IRBuilder<> Builder(C);
169 Builder.SetInsertPoint(CI->getParent(), CI);
170
171 Module *M = F->getParent();
172 SmallVector<Value *, 1> Elts;
173 Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
174 MDNode *Node = MDNode::get(C, Elts);
175
176 Value *Arg0 = CI->getArgOperand(0);
177 Value *Arg1 = CI->getArgOperand(1);
178
179 // Convert the type of the pointer to a pointer to the stored type.
180 Value *BC = Builder.CreateBitCast(Arg0,
181 PointerType::getUnqual(Arg1->getType()),
182 "cast");
183 StoreInst *SI = Builder.CreateStore(Arg1, BC);
184 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
185 SI->setAlignment(16);
186
187 // Remove intrinsic.
188 CI->eraseFromParent();
Evan Chenga8288f42007-12-18 01:04:25 +0000189 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000190 llvm_unreachable("Unknown function for CallInst upgrade.");
Evan Cheng0e179d02007-12-17 22:33:23 +0000191 }
192 return;
193 }
194
Gabor Greife9ecc682008-04-06 20:25:17 +0000195 switch (NewFn->getIntrinsicID()) {
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +0000196 case Intrinsic::prefetch: {
197 IRBuilder<> Builder(C);
198 Builder.SetInsertPoint(CI->getParent(), CI);
Chris Lattner229907c2011-07-18 04:54:35 +0000199 llvm::Type *I32Ty = llvm::Type::getInt32Ty(CI->getContext());
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +0000200
201 // Add the extra "data cache" argument
202 Value *Operands[4] = { CI->getArgOperand(0), CI->getArgOperand(1),
203 CI->getArgOperand(2),
204 llvm::ConstantInt::get(I32Ty, 1) };
Jay Foad5bd375a2011-07-15 08:37:34 +0000205 CallInst *NewCI = CallInst::Create(NewFn, Operands,
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +0000206 CI->getName(), CI);
207 NewCI->setTailCall(CI->isTailCall());
208 NewCI->setCallingConv(CI->getCallingConv());
209 // Handle any uses of the old CallInst.
210 if (!CI->use_empty())
211 // Replace all uses of the old call with the new cast which has the
212 // correct type.
213 CI->replaceAllUsesWith(NewCI);
214
215 // Clean up the old call now that it has been completely upgraded.
216 CI->eraseFromParent();
217 break;
218 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000219 }
220}
221
222// This tests each Function to determine if it needs upgrading. When we find
223// one we are interested in, we then upgrade all calls to reflect the new
224// function.
225void llvm::UpgradeCallsToIntrinsic(Function* F) {
226 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
227
228 // Upgrade the function and check if it is a totaly new function.
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000229 Function *NewFn;
Evan Cheng0e179d02007-12-17 22:33:23 +0000230 if (UpgradeIntrinsicFunction(F, NewFn)) {
Chandler Carruth7132e002007-08-04 01:51:18 +0000231 if (NewFn != F) {
232 // Replace all uses to the old function with the new one if necessary.
233 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
234 UI != UE; ) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000235 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
Chandler Carruth7132e002007-08-04 01:51:18 +0000236 UpgradeIntrinsicCall(CI, NewFn);
237 }
238 // Remove old function, no longer used, from the module.
239 F->eraseFromParent();
240 }
241 }
242}
Devang Patel80ae3492009-08-28 23:24:31 +0000243
Victor Hernandezc2044a12010-01-05 21:13:46 +0000244/// This function strips all debug info intrinsics, except for llvm.dbg.declare.
245/// If an llvm.dbg.declare intrinsic is invalid, then this function simply
246/// strips that use.
Devang Patel80ae3492009-08-28 23:24:31 +0000247void llvm::CheckDebugInfoIntrinsics(Module *M) {
Devang Patel80ae3492009-08-28 23:24:31 +0000248 if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000249 while (!FuncStart->use_empty())
250 cast<CallInst>(FuncStart->use_back())->eraseFromParent();
Devang Patelbe94f232010-01-05 01:10:40 +0000251 FuncStart->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000252 }
Devang Patelbe94f232010-01-05 01:10:40 +0000253
Devang Patel80ae3492009-08-28 23:24:31 +0000254 if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000255 while (!StopPoint->use_empty())
256 cast<CallInst>(StopPoint->use_back())->eraseFromParent();
Devang Patelbe94f232010-01-05 01:10:40 +0000257 StopPoint->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000258 }
259
260 if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000261 while (!RegionStart->use_empty())
262 cast<CallInst>(RegionStart->use_back())->eraseFromParent();
Devang Patelbe94f232010-01-05 01:10:40 +0000263 RegionStart->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000264 }
265
266 if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000267 while (!RegionEnd->use_empty())
268 cast<CallInst>(RegionEnd->use_back())->eraseFromParent();
Devang Patelbe94f232010-01-05 01:10:40 +0000269 RegionEnd->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000270 }
271
272 if (Function *Declare = M->getFunction("llvm.dbg.declare")) {
273 if (!Declare->use_empty()) {
274 DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back());
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000275 if (!isa<MDNode>(DDI->getArgOperand(0)) ||
276 !isa<MDNode>(DDI->getArgOperand(1))) {
Devang Patel80ae3492009-08-28 23:24:31 +0000277 while (!Declare->use_empty()) {
278 CallInst *CI = cast<CallInst>(Declare->use_back());
279 CI->eraseFromParent();
280 }
281 Declare->eraseFromParent();
282 }
283 }
284 }
285}
Bill Wendling45449b12011-08-25 23:22:40 +0000286
287/// FindExnAndSelIntrinsics - Find the eh_exception and eh_selector intrinsic
288/// calls reachable from the unwind basic block.
289static void FindExnAndSelIntrinsics(BasicBlock *BB, CallInst *&Exn,
290 CallInst *&Sel,
291 SmallPtrSet<BasicBlock*, 8> &Visited) {
292 if (!Visited.insert(BB)) return;
293
294 for (BasicBlock::iterator
295 I = BB->begin(), E = BB->end(); I != E; ++I) {
296 if (CallInst *CI = dyn_cast<CallInst>(I)) {
297 switch (CI->getCalledFunction()->getIntrinsicID()) {
298 default: break;
299 case Intrinsic::eh_exception:
300 assert(!Exn && "Found more than one eh.exception call!");
301 Exn = CI;
302 break;
303 case Intrinsic::eh_selector:
304 assert(!Sel && "Found more than one eh.selector call!");
305 Sel = CI;
306 break;
307 }
308
309 if (Exn && Sel) return;
310 }
311 }
312
313 if (Exn && Sel) return;
314
315 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
316 FindExnAndSelIntrinsics(*I, Exn, Sel, Visited);
317 if (Exn && Sel) return;
318 }
319}
320
321/// TransferClausesToLandingPadInst - Transfer the exception handling clauses
322/// from the eh_selector call to the new landingpad instruction.
323static void TransferClausesToLandingPadInst(LandingPadInst *LPI,
324 CallInst *EHSel) {
325 LLVMContext &Context = LPI->getContext();
326 unsigned N = EHSel->getNumArgOperands();
327
328 for (unsigned i = N - 1; i > 1; --i) {
329 if (const ConstantInt *CI = dyn_cast<ConstantInt>(EHSel->getArgOperand(i))){
330 unsigned FilterLength = CI->getZExtValue();
331 unsigned FirstCatch = i + FilterLength + !FilterLength;
332 assert(FirstCatch <= N && "Invalid filter length");
333
334 if (FirstCatch < N)
335 for (unsigned j = FirstCatch; j < N; ++j) {
336 Value *Val = EHSel->getArgOperand(j);
337 if (!Val->hasName() || Val->getName() != "llvm.eh.catch.all.value") {
338 LPI->addClause(EHSel->getArgOperand(j));
339 } else {
340 GlobalVariable *GV = cast<GlobalVariable>(Val);
341 LPI->addClause(GV->getInitializer());
342 }
343 }
344
345 if (!FilterLength) {
346 // Cleanup.
347 LPI->setCleanup(true);
348 } else {
349 // Filter.
350 SmallVector<Constant *, 4> TyInfo;
351 TyInfo.reserve(FilterLength - 1);
352 for (unsigned j = i + 1; j < FirstCatch; ++j)
353 TyInfo.push_back(cast<Constant>(EHSel->getArgOperand(j)));
354 ArrayType *AType =
355 ArrayType::get(!TyInfo.empty() ? TyInfo[0]->getType() :
356 PointerType::getUnqual(Type::getInt8Ty(Context)),
357 TyInfo.size());
358 LPI->addClause(ConstantArray::get(AType, TyInfo));
359 }
360
361 N = i;
362 }
363 }
364
365 if (N > 2)
366 for (unsigned j = 2; j < N; ++j) {
367 Value *Val = EHSel->getArgOperand(j);
368 if (!Val->hasName() || Val->getName() != "llvm.eh.catch.all.value") {
369 LPI->addClause(EHSel->getArgOperand(j));
370 } else {
371 GlobalVariable *GV = cast<GlobalVariable>(Val);
372 LPI->addClause(GV->getInitializer());
373 }
374 }
375}
376
377/// This function upgrades the old pre-3.0 exception handling system to the new
378/// one. N.B. This will be removed in 3.1.
379void llvm::UpgradeExceptionHandling(Module *M) {
380 Function *EHException = M->getFunction("llvm.eh.exception");
381 Function *EHSelector = M->getFunction("llvm.eh.selector");
382 if (!EHException || !EHSelector)
383 return;
384
385 LLVMContext &Context = M->getContext();
386 Type *ExnTy = PointerType::getUnqual(Type::getInt8Ty(Context));
387 Type *SelTy = Type::getInt32Ty(Context);
388 Type *LPadSlotTy = StructType::get(ExnTy, SelTy, NULL);
389
390 // This map stores the slots where the exception object and selector value are
391 // stored within a function.
392 SmallVector<Instruction*, 32> DeadInsts;
393 DenseMap<Function*, std::pair<Value*, Value*> > FnToLPadSlotMap;
394 for (Module::iterator
395 I = M->begin(), E = M->end(); I != E; ++I) {
396 Function &F = *I;
397
398 for (Function::iterator
399 II = F.begin(), IE = F.end(); II != IE; ++II) {
400 BasicBlock *BB = &*II;
401 InvokeInst *Inst = dyn_cast<InvokeInst>(BB->getTerminator());
402 if (!Inst) continue;
403 BasicBlock *UnwindDest = Inst->getUnwindDest();
404 if (UnwindDest->isLandingPad()) continue; // All ready converted.
405
406 // Store the exception object and selector value in the entry block.
407 Value *ExnSlot = 0;
408 Value *SelSlot = 0;
409 if (!FnToLPadSlotMap[&F].first) {
410 BasicBlock *Entry = &F.front();
411 ExnSlot = new AllocaInst(ExnTy, "exn", Entry->getTerminator());
412 SelSlot = new AllocaInst(SelTy, "sel", Entry->getTerminator());
413 FnToLPadSlotMap[&F] = std::make_pair(ExnSlot, SelSlot);
414 } else {
415 ExnSlot = FnToLPadSlotMap[&F].first;
416 SelSlot = FnToLPadSlotMap[&F].second;
417 }
418
419 // We're in an unwind block. Try to find the eh.exception and eh.selector
420 // calls.
421 IRBuilder<> Builder(Context);
422 Builder.SetInsertPoint(UnwindDest, UnwindDest->getFirstNonPHI());
423
424 SmallPtrSet<BasicBlock*, 8> Visited;
425 CallInst *Exn = 0;
426 CallInst *Sel = 0;
427 FindExnAndSelIntrinsics(UnwindDest, Exn, Sel, Visited);
428 assert(Exn && Sel && "Cannot find eh.exception and eh.selector calls!");
429
430 Value *PersFn = Sel->getArgOperand(1);
431 LandingPadInst *LPI = Builder.CreateLandingPad(LPadSlotTy, PersFn, 0);
432 Value *LPExn = Builder.CreateExtractValue(LPI, 0);
433 Value *LPSel = Builder.CreateExtractValue(LPI, 1);
434 Builder.CreateStore(LPExn, ExnSlot);
435 Builder.CreateStore(LPSel, SelSlot);
436
437 TransferClausesToLandingPadInst(LPI, Sel);
438
439 Exn->replaceAllUsesWith(LPExn);
440 Sel->replaceAllUsesWith(LPSel);
441
442 DeadInsts.push_back(Exn);
443 DeadInsts.push_back(Sel);
444 }
445 }
446
447 // Remove the dead instructions.
448 while (!DeadInsts.empty()) {
449 Instruction *Inst = DeadInsts.pop_back_val();
450 Inst->eraseFromParent();
451 }
452
453 // Replace calls to "llvm.eh.resume" with the 'resume' instruction. Load the
454 // exception and selector values from the stored place.
455 Function *EHResume = M->getFunction("llvm.eh.resume");
456 if (!EHResume) return;
457
458 while (!EHResume->use_empty()) {
459 CallInst *Resume = cast<CallInst>(EHResume->use_back());
460 BasicBlock *BB = Resume->getParent();
461 Function *Fn = BB->getParent();
462 std::pair<Value*, Value*> &ExnSel = FnToLPadSlotMap[Fn];
463 IRBuilder<> Builder(Context);
464 Builder.SetInsertPoint(BB, Resume);
465
466 Value *Exn = Builder.CreateLoad(ExnSel.first, "exn");
467 Value *Sel = Builder.CreateLoad(ExnSel.second, "sel");
468
469 Value *LPadVal =
470 Builder.CreateInsertValue(UndefValue::get(LPadSlotTy),
471 Exn, 0, "lpad.val");
472 LPadVal = Builder.CreateInsertValue(LPadVal, Sel, 1, "lpad.val");
473 Builder.CreateResume(LPadVal);
474
475 // Remove all instructions after the 'resume.'
476 BasicBlock::iterator I = Resume;
477 while (I != BB->end()) {
478 Instruction *Inst = &*I++;
479 Inst->eraseFromParent();
480 }
481 }
482}