1 //===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the auto-upgrade helper functions
12 //===----------------------------------------------------------------------===//
14 #include "llvm/AutoUpgrade.h"
15 #include "llvm/Constants.h"
16 #include "llvm/Function.h"
17 #include "llvm/Instruction.h"
18 #include "llvm/LLVMContext.h"
19 #include "llvm/Module.h"
20 #include "llvm/IntrinsicInst.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/Support/CallSite.h"
25 #include "llvm/Support/CFG.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/IRBuilder.h"
32 static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
33 assert(F && "Illegal to upgrade a non-existent Function.");
35 // Quickly eliminate it, if it's not a candidate.
36 StringRef Name = F->getName();
37 if (Name.size() <= 8 || !Name.startswith("llvm."))
39 Name = Name.substr(5); // Strip off "llvm."
41 FunctionType *FTy = F->getFunctionType();
42 Module *M = F->getParent();
47 // This upgrades the old llvm.init.trampoline to the new
48 // llvm.init.trampoline and llvm.adjust.trampoline pair.
49 if (Name == "init.trampoline") {
50 // The new llvm.init.trampoline returns nothing.
51 if (FTy->getReturnType()->isVoidTy())
54 assert(FTy->getNumParams() == 3 && "old init.trampoline takes 3 args!");
56 // Change the name of the old intrinsic so that we can play with its type.
57 std::string NameTmp = F->getName();
59 NewFn = cast<Function>(M->getOrInsertFunction(
61 Type::getVoidTy(M->getContext()),
62 FTy->getParamType(0), FTy->getParamType(1),
63 FTy->getParamType(2), (Type *)0));
67 // This upgrades the llvm.prefetch intrinsic to accept one more parameter,
68 // which is a instruction / data cache identifier. The old version only
69 // implicitly accepted the data version.
70 if (Name == "prefetch") {
71 // Don't do anything if it has the correct number of arguments already
72 if (FTy->getNumParams() == 4)
75 assert(FTy->getNumParams() == 3 && "old prefetch takes 3 args!");
76 // We first need to change the name of the old (bad) intrinsic, because
77 // its type is incorrect, but we cannot overload that name. We
78 // arbitrarily unique it here allowing us to construct a correctly named
79 // and typed function below.
80 std::string NameTmp = F->getName();
82 NewFn = cast<Function>(M->getOrInsertFunction(NameTmp,
94 const char *NewFnName = NULL;
95 // This fixes the poorly named crc32 intrinsics.
96 if (Name == "x86.sse42.crc32.8")
97 NewFnName = "llvm.x86.sse42.crc32.32.8";
98 else if (Name == "x86.sse42.crc32.16")
99 NewFnName = "llvm.x86.sse42.crc32.32.16";
100 else if (Name == "x86.sse42.crc32.32")
101 NewFnName = "llvm.x86.sse42.crc32.32.32";
102 else if (Name == "x86.sse42.crc64.8")
103 NewFnName = "llvm.x86.sse42.crc32.64.8";
104 else if (Name == "x86.sse42.crc64.64")
105 NewFnName = "llvm.x86.sse42.crc32.64.64";
108 F->setName(NewFnName);
113 // Calls to these instructions are transformed into unaligned loads.
114 if (Name == "x86.sse.loadu.ps" || Name == "x86.sse2.loadu.dq" ||
115 Name == "x86.sse2.loadu.pd")
118 // Calls to these instructions are transformed into nontemporal stores.
119 if (Name == "x86.sse.movnt.ps" || Name == "x86.sse2.movnt.dq" ||
120 Name == "x86.sse2.movnt.pd" || Name == "x86.sse2.movnt.i")
127 // This may not belong here. This function is effectively being overloaded
128 // to both detect an intrinsic which needs upgrading, and to provide the
129 // upgraded form of the intrinsic. We should perhaps have two separate
130 // functions for this.
134 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
136 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
138 // Upgrade intrinsic attributes. This does not change the function.
141 if (unsigned id = F->getIntrinsicID())
142 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
146 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
147 // Nothing to do yet.
151 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
152 // upgraded intrinsic. All argument and return casting must be provided in
153 // order to seamlessly integrate with existing context.
154 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
155 Function *F = CI->getCalledFunction();
156 LLVMContext &C = CI->getContext();
157 ImmutableCallSite CS(CI);
159 assert(F && "CallInst has no function associated with it.");
162 if (F->getName() == "llvm.x86.sse.loadu.ps" ||
163 F->getName() == "llvm.x86.sse2.loadu.dq" ||
164 F->getName() == "llvm.x86.sse2.loadu.pd") {
165 // Convert to a native, unaligned load.
166 Type *VecTy = CI->getType();
167 Type *IntTy = IntegerType::get(C, 128);
168 IRBuilder<> Builder(C);
169 Builder.SetInsertPoint(CI->getParent(), CI);
171 Value *BC = Builder.CreateBitCast(CI->getArgOperand(0),
172 PointerType::getUnqual(IntTy),
174 LoadInst *LI = Builder.CreateLoad(BC, CI->getName());
175 LI->setAlignment(1); // Unaligned load.
176 BC = Builder.CreateBitCast(LI, VecTy, "new.cast");
178 // Fix up all the uses with our new load.
179 if (!CI->use_empty())
180 CI->replaceAllUsesWith(BC);
183 CI->eraseFromParent();
184 } else if (F->getName() == "llvm.x86.sse.movnt.ps" ||
185 F->getName() == "llvm.x86.sse2.movnt.dq" ||
186 F->getName() == "llvm.x86.sse2.movnt.pd" ||
187 F->getName() == "llvm.x86.sse2.movnt.i") {
188 IRBuilder<> Builder(C);
189 Builder.SetInsertPoint(CI->getParent(), CI);
191 Module *M = F->getParent();
192 SmallVector<Value *, 1> Elts;
193 Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
194 MDNode *Node = MDNode::get(C, Elts);
196 Value *Arg0 = CI->getArgOperand(0);
197 Value *Arg1 = CI->getArgOperand(1);
199 // Convert the type of the pointer to a pointer to the stored type.
200 Value *BC = Builder.CreateBitCast(Arg0,
201 PointerType::getUnqual(Arg1->getType()),
203 StoreInst *SI = Builder.CreateStore(Arg1, BC);
204 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
205 SI->setAlignment(16);
208 CI->eraseFromParent();
210 llvm_unreachable("Unknown function for CallInst upgrade.");
215 switch (NewFn->getIntrinsicID()) {
216 case Intrinsic::prefetch: {
217 IRBuilder<> Builder(C);
218 Builder.SetInsertPoint(CI->getParent(), CI);
219 llvm::Type *I32Ty = llvm::Type::getInt32Ty(CI->getContext());
221 // Add the extra "data cache" argument
222 Value *Operands[4] = { CI->getArgOperand(0), CI->getArgOperand(1),
223 CI->getArgOperand(2),
224 llvm::ConstantInt::get(I32Ty, 1) };
225 CallInst *NewCI = CallInst::Create(NewFn, Operands,
227 NewCI->setTailCall(CI->isTailCall());
228 NewCI->setCallingConv(CI->getCallingConv());
229 // Handle any uses of the old CallInst.
230 if (!CI->use_empty())
231 // Replace all uses of the old call with the new cast which has the
233 CI->replaceAllUsesWith(NewCI);
235 // Clean up the old call now that it has been completely upgraded.
236 CI->eraseFromParent();
239 case Intrinsic::init_trampoline: {
242 // %tramp = call i8* llvm.init.trampoline (i8* x, i8* y, i8* z)
244 // call void llvm.init.trampoline (i8* %x, i8* %y, i8* %z)
245 // %tramp = call i8* llvm.adjust.trampoline (i8* %x)
247 Function *AdjustTrampolineFn =
248 cast<Function>(Intrinsic::getDeclaration(F->getParent(),
249 Intrinsic::adjust_trampoline));
251 IRBuilder<> Builder(C);
252 Builder.SetInsertPoint(CI);
254 Builder.CreateCall3(NewFn, CI->getArgOperand(0), CI->getArgOperand(1),
255 CI->getArgOperand(2));
257 CallInst *AdjustCall = Builder.CreateCall(AdjustTrampolineFn,
258 CI->getArgOperand(0),
260 if (!CI->use_empty())
261 CI->replaceAllUsesWith(AdjustCall);
262 CI->eraseFromParent();
268 // This tests each Function to determine if it needs upgrading. When we find
269 // one we are interested in, we then upgrade all calls to reflect the new
271 void llvm::UpgradeCallsToIntrinsic(Function* F) {
272 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
274 // Upgrade the function and check if it is a totaly new function.
276 if (UpgradeIntrinsicFunction(F, NewFn)) {
278 // Replace all uses to the old function with the new one if necessary.
279 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
281 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
282 UpgradeIntrinsicCall(CI, NewFn);
284 // Remove old function, no longer used, from the module.
285 F->eraseFromParent();
290 /// This function strips all debug info intrinsics, except for llvm.dbg.declare.
291 /// If an llvm.dbg.declare intrinsic is invalid, then this function simply
293 void llvm::CheckDebugInfoIntrinsics(Module *M) {
294 if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) {
295 while (!FuncStart->use_empty())
296 cast<CallInst>(FuncStart->use_back())->eraseFromParent();
297 FuncStart->eraseFromParent();
300 if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) {
301 while (!StopPoint->use_empty())
302 cast<CallInst>(StopPoint->use_back())->eraseFromParent();
303 StopPoint->eraseFromParent();
306 if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) {
307 while (!RegionStart->use_empty())
308 cast<CallInst>(RegionStart->use_back())->eraseFromParent();
309 RegionStart->eraseFromParent();
312 if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) {
313 while (!RegionEnd->use_empty())
314 cast<CallInst>(RegionEnd->use_back())->eraseFromParent();
315 RegionEnd->eraseFromParent();
318 if (Function *Declare = M->getFunction("llvm.dbg.declare")) {
319 if (!Declare->use_empty()) {
320 DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back());
321 if (!isa<MDNode>(DDI->getArgOperand(0)) ||
322 !isa<MDNode>(DDI->getArgOperand(1))) {
323 while (!Declare->use_empty()) {
324 CallInst *CI = cast<CallInst>(Declare->use_back());
325 CI->eraseFromParent();
327 Declare->eraseFromParent();
333 /// FindExnAndSelIntrinsics - Find the eh_exception and eh_selector intrinsic
334 /// calls reachable from the unwind basic block.
335 static void FindExnAndSelIntrinsics(BasicBlock *BB, CallInst *&Exn,
337 SmallPtrSet<BasicBlock*, 8> &Visited) {
338 if (!Visited.insert(BB)) return;
340 for (BasicBlock::iterator
341 I = BB->begin(), E = BB->end(); I != E; ++I) {
342 if (CallInst *CI = dyn_cast<CallInst>(I)) {
343 switch (CI->getCalledFunction()->getIntrinsicID()) {
345 case Intrinsic::eh_exception:
346 assert(!Exn && "Found more than one eh.exception call!");
349 case Intrinsic::eh_selector:
350 assert(!Sel && "Found more than one eh.selector call!");
355 if (Exn && Sel) return;
359 if (Exn && Sel) return;
361 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
362 FindExnAndSelIntrinsics(*I, Exn, Sel, Visited);
363 if (Exn && Sel) return;
367 /// TransferClausesToLandingPadInst - Transfer the exception handling clauses
368 /// from the eh_selector call to the new landingpad instruction.
369 static void TransferClausesToLandingPadInst(LandingPadInst *LPI,
371 LLVMContext &Context = LPI->getContext();
372 unsigned N = EHSel->getNumArgOperands();
374 for (unsigned i = N - 1; i > 1; --i) {
375 if (const ConstantInt *CI = dyn_cast<ConstantInt>(EHSel->getArgOperand(i))){
376 unsigned FilterLength = CI->getZExtValue();
377 unsigned FirstCatch = i + FilterLength + !FilterLength;
378 assert(FirstCatch <= N && "Invalid filter length");
381 for (unsigned j = FirstCatch; j < N; ++j) {
382 Value *Val = EHSel->getArgOperand(j);
383 if (!Val->hasName() || Val->getName() != "llvm.eh.catch.all.value") {
384 LPI->addClause(EHSel->getArgOperand(j));
386 GlobalVariable *GV = cast<GlobalVariable>(Val);
387 LPI->addClause(GV->getInitializer());
393 LPI->setCleanup(true);
396 SmallVector<Constant *, 4> TyInfo;
397 TyInfo.reserve(FilterLength - 1);
398 for (unsigned j = i + 1; j < FirstCatch; ++j)
399 TyInfo.push_back(cast<Constant>(EHSel->getArgOperand(j)));
401 ArrayType::get(!TyInfo.empty() ? TyInfo[0]->getType() :
402 PointerType::getUnqual(Type::getInt8Ty(Context)),
404 LPI->addClause(ConstantArray::get(AType, TyInfo));
412 for (unsigned j = 2; j < N; ++j) {
413 Value *Val = EHSel->getArgOperand(j);
414 if (!Val->hasName() || Val->getName() != "llvm.eh.catch.all.value") {
415 LPI->addClause(EHSel->getArgOperand(j));
417 GlobalVariable *GV = cast<GlobalVariable>(Val);
418 LPI->addClause(GV->getInitializer());
423 /// This function upgrades the old pre-3.0 exception handling system to the new
424 /// one. N.B. This will be removed in 3.1.
425 void llvm::UpgradeExceptionHandling(Module *M) {
426 Function *EHException = M->getFunction("llvm.eh.exception");
427 Function *EHSelector = M->getFunction("llvm.eh.selector");
428 if (!EHException || !EHSelector)
431 LLVMContext &Context = M->getContext();
432 Type *ExnTy = PointerType::getUnqual(Type::getInt8Ty(Context));
433 Type *SelTy = Type::getInt32Ty(Context);
434 Type *LPadSlotTy = StructType::get(ExnTy, SelTy, NULL);
436 // This map links the invoke instruction with the eh.exception and eh.selector
437 // calls associated with it.
438 DenseMap<InvokeInst*, std::pair<Value*, Value*> > InvokeToIntrinsicsMap;
439 for (Module::iterator
440 I = M->begin(), E = M->end(); I != E; ++I) {
443 for (Function::iterator
444 II = F.begin(), IE = F.end(); II != IE; ++II) {
445 BasicBlock *BB = &*II;
446 InvokeInst *Inst = dyn_cast<InvokeInst>(BB->getTerminator());
448 BasicBlock *UnwindDest = Inst->getUnwindDest();
449 if (UnwindDest->isLandingPad()) continue; // Already converted.
451 SmallPtrSet<BasicBlock*, 8> Visited;
454 FindExnAndSelIntrinsics(UnwindDest, Exn, Sel, Visited);
455 assert(Exn && Sel && "Cannot find eh.exception and eh.selector calls!");
456 InvokeToIntrinsicsMap[Inst] = std::make_pair(Exn, Sel);
460 // This map stores the slots where the exception object and selector value are
461 // stored within a function.
462 DenseMap<Function*, std::pair<Value*, Value*> > FnToLPadSlotMap;
463 SmallPtrSet<Instruction*, 32> DeadInsts;
464 for (DenseMap<InvokeInst*, std::pair<Value*, Value*> >::iterator
465 I = InvokeToIntrinsicsMap.begin(), E = InvokeToIntrinsicsMap.end();
467 InvokeInst *Invoke = I->first;
468 BasicBlock *UnwindDest = Invoke->getUnwindDest();
469 Function *F = UnwindDest->getParent();
470 std::pair<Value*, Value*> EHIntrinsics = I->second;
471 CallInst *Exn = cast<CallInst>(EHIntrinsics.first);
472 CallInst *Sel = cast<CallInst>(EHIntrinsics.second);
474 // Store the exception object and selector value in the entry block.
477 if (!FnToLPadSlotMap[F].first) {
478 BasicBlock *Entry = &F->front();
479 ExnSlot = new AllocaInst(ExnTy, "exn", Entry->getTerminator());
480 SelSlot = new AllocaInst(SelTy, "sel", Entry->getTerminator());
481 FnToLPadSlotMap[F] = std::make_pair(ExnSlot, SelSlot);
483 ExnSlot = FnToLPadSlotMap[F].first;
484 SelSlot = FnToLPadSlotMap[F].second;
487 if (!UnwindDest->getSinglePredecessor()) {
488 // The unwind destination doesn't have a single predecessor. Create an
489 // unwind destination which has only one predecessor.
490 BasicBlock *NewBB = BasicBlock::Create(Context, "new.lpad",
491 UnwindDest->getParent());
492 BranchInst::Create(UnwindDest, NewBB);
493 Invoke->setUnwindDest(NewBB);
495 // Fix up any PHIs in the original unwind destination block.
496 for (BasicBlock::iterator
497 II = UnwindDest->begin(); isa<PHINode>(II); ++II) {
498 PHINode *PN = cast<PHINode>(II);
499 int Idx = PN->getBasicBlockIndex(Invoke->getParent());
500 if (Idx == -1) continue;
501 PN->setIncomingBlock(Idx, NewBB);
507 IRBuilder<> Builder(Context);
508 Builder.SetInsertPoint(UnwindDest, UnwindDest->getFirstInsertionPt());
510 Value *PersFn = Sel->getArgOperand(1);
511 LandingPadInst *LPI = Builder.CreateLandingPad(LPadSlotTy, PersFn, 0);
512 Value *LPExn = Builder.CreateExtractValue(LPI, 0);
513 Value *LPSel = Builder.CreateExtractValue(LPI, 1);
514 Builder.CreateStore(LPExn, ExnSlot);
515 Builder.CreateStore(LPSel, SelSlot);
517 TransferClausesToLandingPadInst(LPI, Sel);
519 DeadInsts.insert(Exn);
520 DeadInsts.insert(Sel);
523 // Replace the old intrinsic calls with the values from the landingpad
524 // instruction(s). These values were stored in allocas for us to use here.
525 for (DenseMap<InvokeInst*, std::pair<Value*, Value*> >::iterator
526 I = InvokeToIntrinsicsMap.begin(), E = InvokeToIntrinsicsMap.end();
528 std::pair<Value*, Value*> EHIntrinsics = I->second;
529 CallInst *Exn = cast<CallInst>(EHIntrinsics.first);
530 CallInst *Sel = cast<CallInst>(EHIntrinsics.second);
531 BasicBlock *Parent = Exn->getParent();
533 std::pair<Value*,Value*> ExnSelSlots = FnToLPadSlotMap[Parent->getParent()];
535 IRBuilder<> Builder(Context);
536 Builder.SetInsertPoint(Parent, Exn);
537 LoadInst *LPExn = Builder.CreateLoad(ExnSelSlots.first, "exn.load");
538 LoadInst *LPSel = Builder.CreateLoad(ExnSelSlots.second, "sel.load");
540 Exn->replaceAllUsesWith(LPExn);
541 Sel->replaceAllUsesWith(LPSel);
544 // Remove the dead instructions.
545 for (SmallPtrSet<Instruction*, 32>::iterator
546 I = DeadInsts.begin(), E = DeadInsts.end(); I != E; ++I) {
547 Instruction *Inst = *I;
548 Inst->eraseFromParent();
551 // Replace calls to "llvm.eh.resume" with the 'resume' instruction. Load the
552 // exception and selector values from the stored place.
553 Function *EHResume = M->getFunction("llvm.eh.resume");
554 if (!EHResume) return;
556 while (!EHResume->use_empty()) {
557 CallInst *Resume = cast<CallInst>(EHResume->use_back());
558 BasicBlock *BB = Resume->getParent();
560 IRBuilder<> Builder(Context);
561 Builder.SetInsertPoint(BB, Resume);
564 Builder.CreateInsertValue(UndefValue::get(LPadSlotTy),
565 Resume->getArgOperand(0), 0, "lpad.val");
566 LPadVal = Builder.CreateInsertValue(LPadVal, Resume->getArgOperand(1),
568 Builder.CreateResume(LPadVal);
570 // Remove all instructions after the 'resume.'
571 BasicBlock::iterator I = Resume;
572 while (I != BB->end()) {
573 Instruction *Inst = &*I++;
574 Inst->eraseFromParent();