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 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.
50 if (Name == "prefetch") {
51 // Don't do anything if it has the correct number of arguments already
52 if (FTy->getNumParams() == 4)
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.
60 std::string NameTmp = F->getName();
62 NewFn = cast<Function>(M->getOrInsertFunction(NameTmp,
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";
88 F->setName(NewFnName);
93 // 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")
98 // 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")
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.
114 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
116 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
118 // Upgrade intrinsic attributes. This does not change the function.
121 if (unsigned id = F->getIntrinsicID())
122 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
126 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
127 // Nothing to do yet.
131 // 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.
134 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
135 Function *F = CI->getCalledFunction();
136 LLVMContext &C = CI->getContext();
137 ImmutableCallSite CS(CI);
139 assert(F && "CallInst has no function associated with it.");
142 if (F->getName() == "llvm.x86.sse.loadu.ps" ||
143 F->getName() == "llvm.x86.sse2.loadu.dq" ||
144 F->getName() == "llvm.x86.sse2.loadu.pd") {
145 // Convert to a native, unaligned load.
146 Type *VecTy = CI->getType();
147 Type *IntTy = IntegerType::get(C, 128);
148 IRBuilder<> Builder(C);
149 Builder.SetInsertPoint(CI->getParent(), CI);
151 Value *BC = Builder.CreateBitCast(CI->getArgOperand(0),
152 PointerType::getUnqual(IntTy),
154 LoadInst *LI = Builder.CreateLoad(BC, CI->getName());
155 LI->setAlignment(1); // Unaligned load.
156 BC = Builder.CreateBitCast(LI, VecTy, "new.cast");
158 // Fix up all the uses with our new load.
159 if (!CI->use_empty())
160 CI->replaceAllUsesWith(BC);
163 CI->eraseFromParent();
164 } 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);
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);
176 Value *Arg0 = CI->getArgOperand(0);
177 Value *Arg1 = CI->getArgOperand(1);
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()),
183 StoreInst *SI = Builder.CreateStore(Arg1, BC);
184 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
185 SI->setAlignment(16);
188 CI->eraseFromParent();
190 llvm_unreachable("Unknown function for CallInst upgrade.");
195 switch (NewFn->getIntrinsicID()) {
196 case Intrinsic::prefetch: {
197 IRBuilder<> Builder(C);
198 Builder.SetInsertPoint(CI->getParent(), CI);
199 llvm::Type *I32Ty = llvm::Type::getInt32Ty(CI->getContext());
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) };
205 CallInst *NewCI = CallInst::Create(NewFn, Operands,
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
213 CI->replaceAllUsesWith(NewCI);
215 // Clean up the old call now that it has been completely upgraded.
216 CI->eraseFromParent();
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
225 void llvm::UpgradeCallsToIntrinsic(Function* F) {
226 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
228 // Upgrade the function and check if it is a totaly new function.
230 if (UpgradeIntrinsicFunction(F, NewFn)) {
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();
235 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
236 UpgradeIntrinsicCall(CI, NewFn);
238 // Remove old function, no longer used, from the module.
239 F->eraseFromParent();
244 /// 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
247 void llvm::CheckDebugInfoIntrinsics(Module *M) {
248 if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) {
249 while (!FuncStart->use_empty())
250 cast<CallInst>(FuncStart->use_back())->eraseFromParent();
251 FuncStart->eraseFromParent();
254 if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) {
255 while (!StopPoint->use_empty())
256 cast<CallInst>(StopPoint->use_back())->eraseFromParent();
257 StopPoint->eraseFromParent();
260 if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) {
261 while (!RegionStart->use_empty())
262 cast<CallInst>(RegionStart->use_back())->eraseFromParent();
263 RegionStart->eraseFromParent();
266 if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) {
267 while (!RegionEnd->use_empty())
268 cast<CallInst>(RegionEnd->use_back())->eraseFromParent();
269 RegionEnd->eraseFromParent();
272 if (Function *Declare = M->getFunction("llvm.dbg.declare")) {
273 if (!Declare->use_empty()) {
274 DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back());
275 if (!isa<MDNode>(DDI->getArgOperand(0)) ||
276 !isa<MDNode>(DDI->getArgOperand(1))) {
277 while (!Declare->use_empty()) {
278 CallInst *CI = cast<CallInst>(Declare->use_back());
279 CI->eraseFromParent();
281 Declare->eraseFromParent();
287 /// FindExnAndSelIntrinsics - Find the eh_exception and eh_selector intrinsic
288 /// calls reachable from the unwind basic block.
289 static void FindExnAndSelIntrinsics(BasicBlock *BB, CallInst *&Exn,
291 SmallPtrSet<BasicBlock*, 8> &Visited) {
292 if (!Visited.insert(BB)) return;
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()) {
299 case Intrinsic::eh_exception:
300 assert(!Exn && "Found more than one eh.exception call!");
303 case Intrinsic::eh_selector:
304 assert(!Sel && "Found more than one eh.selector call!");
309 if (Exn && Sel) return;
313 if (Exn && Sel) return;
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;
321 /// TransferClausesToLandingPadInst - Transfer the exception handling clauses
322 /// from the eh_selector call to the new landingpad instruction.
323 static void TransferClausesToLandingPadInst(LandingPadInst *LPI,
325 LLVMContext &Context = LPI->getContext();
326 unsigned N = EHSel->getNumArgOperands();
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");
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));
340 GlobalVariable *GV = cast<GlobalVariable>(Val);
341 LPI->addClause(GV->getInitializer());
347 LPI->setCleanup(true);
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)));
355 ArrayType::get(!TyInfo.empty() ? TyInfo[0]->getType() :
356 PointerType::getUnqual(Type::getInt8Ty(Context)),
358 LPI->addClause(ConstantArray::get(AType, TyInfo));
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));
371 GlobalVariable *GV = cast<GlobalVariable>(Val);
372 LPI->addClause(GV->getInitializer());
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.
379 void llvm::UpgradeExceptionHandling(Module *M) {
380 Function *EHException = M->getFunction("llvm.eh.exception");
381 Function *EHSelector = M->getFunction("llvm.eh.selector");
382 if (!EHException || !EHSelector)
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);
390 // This map links the invoke instruction with the eh.exception and eh.selector
391 // calls associated with it.
392 DenseMap<InvokeInst*, std::pair<Value*, Value*> > InvokeToIntrinsicsMap;
393 for (Module::iterator
394 I = M->begin(), E = M->end(); I != E; ++I) {
397 for (Function::iterator
398 II = F.begin(), IE = F.end(); II != IE; ++II) {
399 BasicBlock *BB = &*II;
400 InvokeInst *Inst = dyn_cast<InvokeInst>(BB->getTerminator());
402 BasicBlock *UnwindDest = Inst->getUnwindDest();
403 if (UnwindDest->isLandingPad()) continue; // Already converted.
405 SmallPtrSet<BasicBlock*, 8> Visited;
408 FindExnAndSelIntrinsics(UnwindDest, Exn, Sel, Visited);
409 assert(Exn && Sel && "Cannot find eh.exception and eh.selector calls!");
410 InvokeToIntrinsicsMap[Inst] = std::make_pair(Exn, Sel);
414 // This map stores the slots where the exception object and selector value are
415 // stored within a function.
416 DenseMap<Function*, std::pair<Value*, Value*> > FnToLPadSlotMap;
418 // This maps the old intrinsic calls (eh.exception & eh.selector) to the new
419 // landingpad instruction(s).
420 DenseMap<std::pair<CallInst*, CallInst*>,
421 SmallVector<std::pair<Value*, Value*>, 8> > OldExnToNewExnMap;
423 SmallPtrSet<Instruction*, 32> DeadInsts;
424 for (DenseMap<InvokeInst*, std::pair<Value*, Value*> >::iterator
425 I = InvokeToIntrinsicsMap.begin(), E = InvokeToIntrinsicsMap.end();
427 InvokeInst *Invoke = I->first;
428 BasicBlock *UnwindDest = Invoke->getUnwindDest();
429 Function *F = UnwindDest->getParent();
430 std::pair<Value*, Value*> EHIntrinsics = I->second;
431 CallInst *Exn = cast<CallInst>(EHIntrinsics.first);
432 CallInst *Sel = cast<CallInst>(EHIntrinsics.second);
434 // Store the exception object and selector value in the entry block.
437 if (!FnToLPadSlotMap[F].first) {
438 BasicBlock *Entry = &F->front();
439 ExnSlot = new AllocaInst(ExnTy, "exn", Entry->getTerminator());
440 SelSlot = new AllocaInst(SelTy, "sel", Entry->getTerminator());
441 FnToLPadSlotMap[F] = std::make_pair(ExnSlot, SelSlot);
443 ExnSlot = FnToLPadSlotMap[F].first;
444 SelSlot = FnToLPadSlotMap[F].second;
447 if (!UnwindDest->getSinglePredecessor()) {
448 // The unwind destination doesn't have a single predecessor. Create an
449 // unwind destination which has only one predecessor.
450 BasicBlock *NewBB = BasicBlock::Create(Context, "new.lpad",
451 UnwindDest->getParent());
452 BranchInst::Create(UnwindDest, NewBB);
453 Invoke->setUnwindDest(NewBB);
455 // Fix up any PHIs in the original unwind destination block.
456 for (BasicBlock::iterator
457 II = UnwindDest->begin(); isa<PHINode>(II); ++II) {
458 PHINode *PN = cast<PHINode>(II);
459 int Idx = PN->getBasicBlockIndex(Invoke->getParent());
460 if (Idx == -1) continue;
461 PN->setIncomingBlock(Idx, NewBB);
467 IRBuilder<> Builder(Context);
468 Builder.SetInsertPoint(UnwindDest, UnwindDest->getFirstInsertionPt());
470 Value *PersFn = Sel->getArgOperand(1);
471 LandingPadInst *LPI = Builder.CreateLandingPad(LPadSlotTy, PersFn, 0);
472 Value *LPExn = Builder.CreateExtractValue(LPI, 0);
473 Value *LPSel = Builder.CreateExtractValue(LPI, 1);
474 Builder.CreateStore(LPExn, ExnSlot);
475 Builder.CreateStore(LPSel, SelSlot);
477 TransferClausesToLandingPadInst(LPI, Sel);
479 OldExnToNewExnMap[std::make_pair(Exn, Sel)].
480 push_back(std::make_pair(LPExn, LPSel));
482 DeadInsts.insert(Exn);
483 DeadInsts.insert(Sel);
486 // Replace the old intrinsic calls with the new (possibly PHI'ed) values.
487 for (DenseMap<std::pair<CallInst*, CallInst*>,
488 SmallVector<std::pair<Value*, Value*>, 8> >::iterator
489 I = OldExnToNewExnMap.begin(), E = OldExnToNewExnMap.end();
491 std::pair<CallInst*, CallInst*> OldExnSel = I->first;
492 CallInst *Exn = OldExnSel.first;
493 CallInst *Sel = OldExnSel.second;
494 SmallVector<std::pair<Value*, Value*>, 8> &LPExnSel = I->second;
495 unsigned Size = LPExnSel.size();
496 Value *LPExn = LPExnSel[0].first;
497 Value *LPSel = LPExnSel[0].second;
500 BasicBlock *Parent = Exn->getParent();
501 IRBuilder<> Builder(Context);
502 Builder.SetInsertPoint(Parent, Parent->getFirstInsertionPt());
504 PHINode *PN = Builder.CreatePHI(Exn->getType(), Size, "exn.phi");
505 for (SmallVector<std::pair<Value*, Value*>, 8>::iterator
506 II = LPExnSel.begin(), IE = LPExnSel.end(); II != IE; ++II)
507 PN->addIncoming(II->first, cast<Instruction>(II->first)->getParent());
511 Parent = Sel->getParent();
512 Builder.SetInsertPoint(Parent, Parent->getFirstInsertionPt());
514 PN = Builder.CreatePHI(Sel->getType(), Size, "sel.phi");
515 for (SmallVector<std::pair<Value*, Value*>, 8>::iterator
516 II = LPExnSel.begin(), IE = LPExnSel.end(); II != IE; ++II)
517 PN->addIncoming(II->second, cast<Instruction>(II->second)->getParent());
522 Exn->replaceAllUsesWith(LPExn);
523 Sel->replaceAllUsesWith(LPSel);
526 // Remove the dead instructions.
527 for (SmallPtrSet<Instruction*, 32>::iterator
528 I = DeadInsts.begin(), E = DeadInsts.end(); I != E; ++I) {
529 Instruction *Inst = *I;
530 Inst->eraseFromParent();
533 // Replace calls to "llvm.eh.resume" with the 'resume' instruction. Load the
534 // exception and selector values from the stored place.
535 Function *EHResume = M->getFunction("llvm.eh.resume");
536 if (!EHResume) return;
538 while (!EHResume->use_empty()) {
539 CallInst *Resume = cast<CallInst>(EHResume->use_back());
540 BasicBlock *BB = Resume->getParent();
541 Function *Fn = BB->getParent();
542 std::pair<Value*, Value*> &ExnSel = FnToLPadSlotMap[Fn];
543 IRBuilder<> Builder(Context);
544 Builder.SetInsertPoint(BB, Resume);
546 Value *Exn = Builder.CreateLoad(ExnSel.first, "exn");
547 Value *Sel = Builder.CreateLoad(ExnSel.second, "sel");
550 Builder.CreateInsertValue(UndefValue::get(LPadSlotTy),
552 LPadVal = Builder.CreateInsertValue(LPadVal, Sel, 1, "lpad.val");
553 Builder.CreateResume(LPadVal);
555 // Remove all instructions after the 'resume.'
556 BasicBlock::iterator I = Resume;
557 while (I != BB->end()) {
558 Instruction *Inst = &*I++;
559 Inst->eraseFromParent();