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/LLVMContext.h"
18 #include "llvm/Module.h"
19 #include "llvm/IntrinsicInst.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/Support/CallSite.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/IRBuilder.h"
28 static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
29 assert(F && "Illegal to upgrade a non-existent Function.");
31 // Get the Function's name.
32 const std::string& Name = F->getName();
35 const FunctionType *FTy = F->getFunctionType();
37 // Quickly eliminate it, if it's not a candidate.
38 if (Name.length() <= 8 || Name[0] != 'l' || Name[1] != 'l' ||
39 Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.')
42 Module *M = F->getParent();
46 // This upgrades the llvm.atomic.lcs, llvm.atomic.las, llvm.atomic.lss,
47 // and atomics with default address spaces to their new names to their new
48 // function name (e.g. llvm.atomic.add.i32 => llvm.atomic.add.i32.p0i32)
49 if (Name.compare(5,7,"atomic.",7) == 0) {
50 if (Name.compare(12,3,"lcs",3) == 0) {
51 std::string::size_type delim = Name.find('.',12);
52 F->setName("llvm.atomic.cmp.swap" + Name.substr(delim) +
53 ".p0" + Name.substr(delim+1));
57 else if (Name.compare(12,3,"las",3) == 0) {
58 std::string::size_type delim = Name.find('.',12);
59 F->setName("llvm.atomic.load.add"+Name.substr(delim)
60 + ".p0" + Name.substr(delim+1));
64 else if (Name.compare(12,3,"lss",3) == 0) {
65 std::string::size_type delim = Name.find('.',12);
66 F->setName("llvm.atomic.load.sub"+Name.substr(delim)
67 + ".p0" + Name.substr(delim+1));
71 else if (Name.rfind(".p") == std::string::npos) {
72 // We don't have an address space qualifier so this has be upgraded
73 // to the new name. Copy the type name at the end of the intrinsic
75 std::string::size_type delim = Name.find_last_of('.');
76 assert(delim != std::string::npos && "can not find type");
77 F->setName(Name + ".p0" + Name.substr(delim+1));
81 } else if (Name.compare(5, 9, "arm.neon.", 9) == 0) {
82 if (((Name.compare(14, 5, "vmovl", 5) == 0 ||
83 Name.compare(14, 5, "vaddl", 5) == 0 ||
84 Name.compare(14, 5, "vsubl", 5) == 0 ||
85 Name.compare(14, 5, "vaddw", 5) == 0 ||
86 Name.compare(14, 5, "vsubw", 5) == 0 ||
87 Name.compare(14, 5, "vmull", 5) == 0 ||
88 Name.compare(14, 5, "vmlal", 5) == 0 ||
89 Name.compare(14, 5, "vmlsl", 5) == 0 ||
90 Name.compare(14, 5, "vabdl", 5) == 0 ||
91 Name.compare(14, 5, "vabal", 5) == 0) &&
92 (Name.compare(19, 2, "s.", 2) == 0 ||
93 Name.compare(19, 2, "u.", 2) == 0)) ||
95 (Name.compare(14, 4, "vaba", 4) == 0 &&
96 (Name.compare(18, 2, "s.", 2) == 0 ||
97 Name.compare(18, 2, "u.", 2) == 0)) ||
99 (Name.compare(14, 6, "vmovn.", 6) == 0)) {
101 // Calls to these are transformed into IR without intrinsics.
105 // Old versions of NEON ld/st intrinsics are missing alignment arguments.
106 bool isVLd = (Name.compare(14, 3, "vld", 3) == 0);
107 bool isVSt = (Name.compare(14, 3, "vst", 3) == 0);
108 if (isVLd || isVSt) {
109 unsigned NumVecs = Name.at(17) - '0';
110 if (NumVecs == 0 || NumVecs > 4)
112 bool isLaneOp = (Name.compare(18, 5, "lane.", 5) == 0);
113 if (!isLaneOp && Name.at(18) != '.')
115 unsigned ExpectedArgs = 2; // for the address and alignment
116 if (isVSt || isLaneOp)
117 ExpectedArgs += NumVecs;
119 ExpectedArgs += 1; // for the lane number
120 unsigned NumP = FTy->getNumParams();
121 if (NumP != ExpectedArgs - 1)
124 // Change the name of the old (bad) intrinsic, because
125 // its type is incorrect, but we cannot overload that name.
128 // One argument is missing: add the alignment argument.
129 std::vector<const Type*> NewParams;
130 for (unsigned p = 0; p < NumP; ++p)
131 NewParams.push_back(FTy->getParamType(p));
132 NewParams.push_back(Type::getInt32Ty(F->getContext()));
133 FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(),
135 NewFn = cast<Function>(M->getOrInsertFunction(Name, NewFTy));
141 // This upgrades the name of the llvm.bswap intrinsic function to only use
142 // a single type name for overloading. We only care about the old format
143 // 'llvm.bswap.i*.i*', so check for 'bswap.' and then for there being
144 // a '.' after 'bswap.'
145 if (Name.compare(5,6,"bswap.",6) == 0) {
146 std::string::size_type delim = Name.find('.',11);
148 if (delim != std::string::npos) {
149 // Construct the new name as 'llvm.bswap' + '.i*'
150 F->setName(Name.substr(0,10)+Name.substr(delim));
158 // We only want to fix the 'llvm.ct*' intrinsics which do not have the
159 // correct return type, so we check for the name, and then check if the
160 // return type does not match the parameter type.
161 if ( (Name.compare(5,5,"ctpop",5) == 0 ||
162 Name.compare(5,4,"ctlz",4) == 0 ||
163 Name.compare(5,4,"cttz",4) == 0) &&
164 FTy->getReturnType() != FTy->getParamType(0)) {
165 // We first need to change the name of the old (bad) intrinsic, because
166 // its type is incorrect, but we cannot overload that name. We
167 // arbitrarily unique it here allowing us to construct a correctly named
168 // and typed function below.
171 // Now construct the new intrinsic with the correct name and type. We
172 // leave the old function around in order to query its type, whatever it
173 // may be, and correctly convert up to the new type.
174 NewFn = cast<Function>(M->getOrInsertFunction(Name,
175 FTy->getParamType(0),
176 FTy->getParamType(0),
183 // The old llvm.eh.selector.i32 is equivalent to the new llvm.eh.selector.
184 if (Name.compare("llvm.eh.selector.i32") == 0) {
185 F->setName("llvm.eh.selector");
189 // The old llvm.eh.typeid.for.i32 is equivalent to llvm.eh.typeid.for.
190 if (Name.compare("llvm.eh.typeid.for.i32") == 0) {
191 F->setName("llvm.eh.typeid.for");
195 // Convert the old llvm.eh.selector.i64 to a call to llvm.eh.selector.
196 if (Name.compare("llvm.eh.selector.i64") == 0) {
197 NewFn = Intrinsic::getDeclaration(M, Intrinsic::eh_selector);
200 // Convert the old llvm.eh.typeid.for.i64 to a call to llvm.eh.typeid.for.
201 if (Name.compare("llvm.eh.typeid.for.i64") == 0) {
202 NewFn = Intrinsic::getDeclaration(M, Intrinsic::eh_typeid_for);
208 // This upgrades the llvm.memcpy, llvm.memmove, and llvm.memset to the
209 // new format that allows overloading the pointer for different address
210 // space (e.g., llvm.memcpy.i16 => llvm.memcpy.p0i8.p0i8.i16)
211 const char* NewFnName = NULL;
212 if (Name.compare(5,8,"memcpy.i",8) == 0) {
214 NewFnName = "llvm.memcpy.p0i8.p0i8.i8";
215 else if (Name.compare(13,2,"16") == 0)
216 NewFnName = "llvm.memcpy.p0i8.p0i8.i16";
217 else if (Name.compare(13,2,"32") == 0)
218 NewFnName = "llvm.memcpy.p0i8.p0i8.i32";
219 else if (Name.compare(13,2,"64") == 0)
220 NewFnName = "llvm.memcpy.p0i8.p0i8.i64";
221 } else if (Name.compare(5,9,"memmove.i",9) == 0) {
223 NewFnName = "llvm.memmove.p0i8.p0i8.i8";
224 else if (Name.compare(14,2,"16") == 0)
225 NewFnName = "llvm.memmove.p0i8.p0i8.i16";
226 else if (Name.compare(14,2,"32") == 0)
227 NewFnName = "llvm.memmove.p0i8.p0i8.i32";
228 else if (Name.compare(14,2,"64") == 0)
229 NewFnName = "llvm.memmove.p0i8.p0i8.i64";
231 else if (Name.compare(5,8,"memset.i",8) == 0) {
233 NewFnName = "llvm.memset.p0i8.i8";
234 else if (Name.compare(13,2,"16") == 0)
235 NewFnName = "llvm.memset.p0i8.i16";
236 else if (Name.compare(13,2,"32") == 0)
237 NewFnName = "llvm.memset.p0i8.i32";
238 else if (Name.compare(13,2,"64") == 0)
239 NewFnName = "llvm.memset.p0i8.i64";
242 NewFn = cast<Function>(M->getOrInsertFunction(NewFnName,
243 FTy->getReturnType(),
244 FTy->getParamType(0),
245 FTy->getParamType(1),
246 FTy->getParamType(2),
247 FTy->getParamType(3),
248 Type::getInt1Ty(F->getContext()),
255 // This upgrades the llvm.part.select overloaded intrinsic names to only
256 // use one type specifier in the name. We only care about the old format
257 // 'llvm.part.select.i*.i*', and solve as above with bswap.
258 if (Name.compare(5,12,"part.select.",12) == 0) {
259 std::string::size_type delim = Name.find('.',17);
261 if (delim != std::string::npos) {
262 // Construct a new name as 'llvm.part.select' + '.i*'
263 F->setName(Name.substr(0,16)+Name.substr(delim));
270 // This upgrades the llvm.part.set intrinsics similarly as above, however
271 // we care about 'llvm.part.set.i*.i*.i*', but only the first two types
272 // must match. There is an additional type specifier after these two
273 // matching types that we must retain when upgrading. Thus, we require
274 // finding 2 periods, not just one, after the intrinsic name.
275 if (Name.compare(5,9,"part.set.",9) == 0) {
276 std::string::size_type delim = Name.find('.',14);
278 if (delim != std::string::npos &&
279 Name.find('.',delim+1) != std::string::npos) {
280 // Construct a new name as 'llvm.part.select' + '.i*.i*'
281 F->setName(Name.substr(0,13)+Name.substr(delim));
290 // This fixes all MMX shift intrinsic instructions to take a
291 // v1i64 instead of a v2i32 as the second parameter.
292 if (Name.compare(5,10,"x86.mmx.ps",10) == 0 &&
293 (Name.compare(13,4,"psll", 4) == 0 ||
294 Name.compare(13,4,"psra", 4) == 0 ||
295 Name.compare(13,4,"psrl", 4) == 0) && Name[17] != 'i') {
297 const llvm::Type *VT =
298 VectorType::get(IntegerType::get(FTy->getContext(), 64), 1);
300 // We don't have to do anything if the parameter already has
302 if (FTy->getParamType(1) == VT)
305 // We first need to change the name of the old (bad) intrinsic, because
306 // its type is incorrect, but we cannot overload that name. We
307 // arbitrarily unique it here allowing us to construct a correctly named
308 // and typed function below.
311 assert(FTy->getNumParams() == 2 && "MMX shift intrinsics take 2 args!");
313 // Now construct the new intrinsic with the correct name and type. We
314 // leave the old function around in order to query its type, whatever it
315 // may be, and correctly convert up to the new type.
316 NewFn = cast<Function>(M->getOrInsertFunction(Name,
317 FTy->getReturnType(),
318 FTy->getParamType(0),
322 } else if (Name.compare(5,17,"x86.sse2.loadh.pd",17) == 0 ||
323 Name.compare(5,17,"x86.sse2.loadl.pd",17) == 0 ||
324 Name.compare(5,16,"x86.sse2.movl.dq",16) == 0 ||
325 Name.compare(5,15,"x86.sse2.movs.d",15) == 0 ||
326 Name.compare(5,16,"x86.sse2.shuf.pd",16) == 0 ||
327 Name.compare(5,18,"x86.sse2.unpckh.pd",18) == 0 ||
328 Name.compare(5,18,"x86.sse2.unpckl.pd",18) == 0 ||
329 Name.compare(5,20,"x86.sse2.punpckh.qdq",20) == 0 ||
330 Name.compare(5,20,"x86.sse2.punpckl.qdq",20) == 0) {
331 // Calls to these intrinsics are transformed into ShuffleVector's.
334 } else if (Name.compare(5, 16, "x86.sse41.pmulld", 16) == 0) {
335 // Calls to these intrinsics are transformed into vector multiplies.
338 } else if (Name.compare(5, 18, "x86.ssse3.palign.r", 18) == 0 ||
339 Name.compare(5, 22, "x86.ssse3.palign.r.128", 22) == 0) {
340 // Calls to these intrinsics are transformed into vector shuffles, shifts,
349 // This may not belong here. This function is effectively being overloaded
350 // to both detect an intrinsic which needs upgrading, and to provide the
351 // upgraded form of the intrinsic. We should perhaps have two separate
352 // functions for this.
356 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
358 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
360 // Upgrade intrinsic attributes. This does not change the function.
363 if (unsigned id = F->getIntrinsicID())
364 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
368 /// ExtendNEONArgs - For NEON "long" and "wide" operations, where the results
369 /// have vector elements twice as big as one or both source operands, do the
370 /// sign- or zero-extension that used to be handled by intrinsics. The
371 /// extended values are returned via V0 and V1.
372 static void ExtendNEONArgs(CallInst *CI, Value *Arg0, Value *Arg1,
373 Value *&V0, Value *&V1) {
374 Function *F = CI->getCalledFunction();
375 const std::string& Name = F->getName();
376 bool isLong = (Name.at(18) == 'l');
377 bool isSigned = (Name.at(19) == 's');
381 V0 = new SExtInst(Arg0, CI->getType(), "", CI);
384 V1 = new SExtInst(Arg1, CI->getType(), "", CI);
387 V0 = new ZExtInst(Arg0, CI->getType(), "", CI);
390 V1 = new ZExtInst(Arg1, CI->getType(), "", CI);
394 /// CallVABD - As part of expanding a call to one of the old NEON vabdl, vaba,
395 /// or vabal intrinsics, construct a call to a vabd intrinsic. Examine the
396 /// name of the old intrinsic to determine whether to use a signed or unsigned
397 /// vabd intrinsic. Get the type from the old call instruction, adjusted for
398 /// half-size vector elements if the old intrinsic was vabdl or vabal.
399 static Instruction *CallVABD(CallInst *CI, Value *Arg0, Value *Arg1) {
400 Function *F = CI->getCalledFunction();
401 const std::string& Name = F->getName();
402 bool isLong = (Name.at(18) == 'l');
403 bool isSigned = (Name.at(isLong ? 19 : 18) == 's');
407 intID = Intrinsic::arm_neon_vabds;
409 intID = Intrinsic::arm_neon_vabdu;
411 const Type *Ty = CI->getType();
413 Ty = VectorType::getTruncatedElementVectorType(cast<const VectorType>(Ty));
415 Function *VABD = Intrinsic::getDeclaration(F->getParent(), intID, &Ty, 1);
419 return CallInst::Create(VABD, Operands, Operands+2,
420 "upgraded."+CI->getName(), CI);
423 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
424 // upgraded intrinsic. All argument and return casting must be provided in
425 // order to seamlessly integrate with existing context.
426 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
427 Function *F = CI->getCalledFunction();
428 LLVMContext &C = CI->getContext();
429 ImmutableCallSite CS(CI);
431 assert(F && "CallInst has no function associated with it.");
434 // Get the Function's name.
435 const std::string& Name = F->getName();
437 // Upgrade ARM NEON intrinsics.
438 if (Name.compare(5, 9, "arm.neon.", 9) == 0) {
441 if (Name.compare(14, 7, "vmovls.", 7) == 0) {
442 NewI = new SExtInst(CI->getArgOperand(0), CI->getType(),
443 "upgraded." + CI->getName(), CI);
444 } else if (Name.compare(14, 7, "vmovlu.", 7) == 0) {
445 NewI = new ZExtInst(CI->getArgOperand(0), CI->getType(),
446 "upgraded." + CI->getName(), CI);
447 } else if (Name.compare(14, 4, "vadd", 4) == 0) {
448 ExtendNEONArgs(CI, CI->getArgOperand(0), CI->getArgOperand(1), V0, V1);
449 NewI = BinaryOperator::CreateAdd(V0, V1, "upgraded."+CI->getName(), CI);
450 } else if (Name.compare(14, 4, "vsub", 4) == 0) {
451 ExtendNEONArgs(CI, CI->getArgOperand(0), CI->getArgOperand(1), V0, V1);
452 NewI = BinaryOperator::CreateSub(V0, V1,"upgraded."+CI->getName(),CI);
453 } else if (Name.compare(14, 4, "vmul", 4) == 0) {
454 ExtendNEONArgs(CI, CI->getArgOperand(0), CI->getArgOperand(1), V0, V1);
455 NewI = BinaryOperator::CreateMul(V0, V1,"upgraded."+CI->getName(),CI);
456 } else if (Name.compare(14, 4, "vmla", 4) == 0) {
457 ExtendNEONArgs(CI, CI->getArgOperand(1), CI->getArgOperand(2), V0, V1);
458 Instruction *MulI = BinaryOperator::CreateMul(V0, V1, "", CI);
459 NewI = BinaryOperator::CreateAdd(CI->getArgOperand(0), MulI,
460 "upgraded."+CI->getName(), CI);
461 } else if (Name.compare(14, 4, "vmls", 4) == 0) {
462 ExtendNEONArgs(CI, CI->getArgOperand(1), CI->getArgOperand(2), V0, V1);
463 Instruction *MulI = BinaryOperator::CreateMul(V0, V1, "", CI);
464 NewI = BinaryOperator::CreateSub(CI->getArgOperand(0), MulI,
465 "upgraded."+CI->getName(), CI);
466 } else if (Name.compare(14, 4, "vabd", 4) == 0) {
467 NewI = CallVABD(CI, CI->getArgOperand(0), CI->getArgOperand(1));
468 NewI = new ZExtInst(NewI, CI->getType(), "upgraded."+CI->getName(), CI);
469 } else if (Name.compare(14, 4, "vaba", 4) == 0) {
470 NewI = CallVABD(CI, CI->getArgOperand(1), CI->getArgOperand(2));
471 if (Name.at(18) == 'l')
472 NewI = new ZExtInst(NewI, CI->getType(), "", CI);
473 NewI = BinaryOperator::CreateAdd(CI->getArgOperand(0), NewI,
474 "upgraded."+CI->getName(), CI);
475 } else if (Name.compare(14, 6, "vmovn.", 6) == 0) {
476 NewI = new TruncInst(CI->getArgOperand(0), CI->getType(),
477 "upgraded." + CI->getName(), CI);
479 llvm_unreachable("Unknown arm.neon function for CallInst upgrade.");
481 // Replace any uses of the old CallInst.
482 if (!CI->use_empty())
483 CI->replaceAllUsesWith(NewI);
484 CI->eraseFromParent();
488 bool isLoadH = false, isLoadL = false, isMovL = false;
489 bool isMovSD = false, isShufPD = false;
490 bool isUnpckhPD = false, isUnpcklPD = false;
491 bool isPunpckhQPD = false, isPunpcklQPD = false;
492 if (F->getName() == "llvm.x86.sse2.loadh.pd")
494 else if (F->getName() == "llvm.x86.sse2.loadl.pd")
496 else if (F->getName() == "llvm.x86.sse2.movl.dq")
498 else if (F->getName() == "llvm.x86.sse2.movs.d")
500 else if (F->getName() == "llvm.x86.sse2.shuf.pd")
502 else if (F->getName() == "llvm.x86.sse2.unpckh.pd")
504 else if (F->getName() == "llvm.x86.sse2.unpckl.pd")
506 else if (F->getName() == "llvm.x86.sse2.punpckh.qdq")
508 else if (F->getName() == "llvm.x86.sse2.punpckl.qdq")
511 if (isLoadH || isLoadL || isMovL || isMovSD || isShufPD ||
512 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
513 std::vector<Constant*> Idxs;
514 Value *Op0 = CI->getArgOperand(0);
515 ShuffleVectorInst *SI = NULL;
516 if (isLoadH || isLoadL) {
517 Value *Op1 = UndefValue::get(Op0->getType());
518 Value *Addr = new BitCastInst(CI->getArgOperand(1),
519 Type::getDoublePtrTy(C),
521 Value *Load = new LoadInst(Addr, "upgraded.", false, 8, CI);
522 Value *Idx = ConstantInt::get(Type::getInt32Ty(C), 0);
523 Op1 = InsertElementInst::Create(Op1, Load, Idx, "upgraded.", CI);
526 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 0));
527 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
529 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
530 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
532 Value *Mask = ConstantVector::get(Idxs);
533 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
535 Constant *Zero = ConstantInt::get(Type::getInt32Ty(C), 0);
536 Idxs.push_back(Zero);
537 Idxs.push_back(Zero);
538 Idxs.push_back(Zero);
539 Idxs.push_back(Zero);
540 Value *ZeroV = ConstantVector::get(Idxs);
543 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 4));
544 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 5));
545 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
546 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 3));
547 Value *Mask = ConstantVector::get(Idxs);
548 SI = new ShuffleVectorInst(ZeroV, Op0, Mask, "upgraded.", CI);
549 } else if (isMovSD ||
550 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
551 Value *Op1 = CI->getArgOperand(1);
553 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
554 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
555 } else if (isUnpckhPD || isPunpckhQPD) {
556 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
557 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 3));
559 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 0));
560 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
562 Value *Mask = ConstantVector::get(Idxs);
563 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
564 } else if (isShufPD) {
565 Value *Op1 = CI->getArgOperand(1);
567 cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
568 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), MaskVal & 1));
569 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C),
570 ((MaskVal >> 1) & 1)+2));
571 Value *Mask = ConstantVector::get(Idxs);
572 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
575 assert(SI && "Unexpected!");
577 // Handle any uses of the old CallInst.
578 if (!CI->use_empty())
579 // Replace all uses of the old call with the new cast which has the
581 CI->replaceAllUsesWith(SI);
583 // Clean up the old call now that it has been completely upgraded.
584 CI->eraseFromParent();
585 } else if (F->getName() == "llvm.x86.sse41.pmulld") {
586 // Upgrade this set of intrinsics into vector multiplies.
587 Instruction *Mul = BinaryOperator::CreateMul(CI->getArgOperand(0),
588 CI->getArgOperand(1),
591 // Fix up all the uses with our new multiply.
592 if (!CI->use_empty())
593 CI->replaceAllUsesWith(Mul);
595 // Remove upgraded multiply.
596 CI->eraseFromParent();
597 } else if (F->getName() == "llvm.x86.ssse3.palign.r") {
598 Value *Op1 = CI->getArgOperand(0);
599 Value *Op2 = CI->getArgOperand(1);
600 Value *Op3 = CI->getArgOperand(2);
601 unsigned shiftVal = cast<ConstantInt>(Op3)->getZExtValue();
603 IRBuilder<> Builder(C);
604 Builder.SetInsertPoint(CI->getParent(), CI);
606 // If palignr is shifting the pair of input vectors less than 9 bytes,
607 // emit a shuffle instruction.
609 const Type *IntTy = Type::getInt32Ty(C);
610 const Type *EltTy = Type::getInt8Ty(C);
611 const Type *VecTy = VectorType::get(EltTy, 8);
613 Op2 = Builder.CreateBitCast(Op2, VecTy);
614 Op1 = Builder.CreateBitCast(Op1, VecTy);
616 llvm::SmallVector<llvm::Constant*, 8> Indices;
617 for (unsigned i = 0; i != 8; ++i)
618 Indices.push_back(ConstantInt::get(IntTy, shiftVal + i));
620 Value *SV = ConstantVector::get(Indices.begin(), Indices.size());
621 Rep = Builder.CreateShuffleVector(Op2, Op1, SV, "palignr");
622 Rep = Builder.CreateBitCast(Rep, F->getReturnType());
625 // If palignr is shifting the pair of input vectors more than 8 but less
626 // than 16 bytes, emit a logical right shift of the destination.
627 else if (shiftVal < 16) {
628 // MMX has these as 1 x i64 vectors for some odd optimization reasons.
629 const Type *EltTy = Type::getInt64Ty(C);
630 const Type *VecTy = VectorType::get(EltTy, 1);
632 Op1 = Builder.CreateBitCast(Op1, VecTy, "cast");
633 Op2 = ConstantInt::get(VecTy, (shiftVal-8) * 8);
635 // create i32 constant
637 Intrinsic::getDeclaration(F->getParent(), Intrinsic::x86_mmx_psrl_q);
638 Rep = Builder.CreateCall2(I, Op1, Op2, "palignr");
641 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
643 Rep = Constant::getNullValue(F->getReturnType());
646 // Replace any uses with our new instruction.
647 if (!CI->use_empty())
648 CI->replaceAllUsesWith(Rep);
650 // Remove upgraded instruction.
651 CI->eraseFromParent();
653 } else if (F->getName() == "llvm.x86.ssse3.palign.r.128") {
654 Value *Op1 = CI->getArgOperand(0);
655 Value *Op2 = CI->getArgOperand(1);
656 Value *Op3 = CI->getArgOperand(2);
657 unsigned shiftVal = cast<ConstantInt>(Op3)->getZExtValue();
659 IRBuilder<> Builder(C);
660 Builder.SetInsertPoint(CI->getParent(), CI);
662 // If palignr is shifting the pair of input vectors less than 17 bytes,
663 // emit a shuffle instruction.
664 if (shiftVal <= 16) {
665 const Type *IntTy = Type::getInt32Ty(C);
666 const Type *EltTy = Type::getInt8Ty(C);
667 const Type *VecTy = VectorType::get(EltTy, 16);
669 Op2 = Builder.CreateBitCast(Op2, VecTy);
670 Op1 = Builder.CreateBitCast(Op1, VecTy);
672 llvm::SmallVector<llvm::Constant*, 16> Indices;
673 for (unsigned i = 0; i != 16; ++i)
674 Indices.push_back(ConstantInt::get(IntTy, shiftVal + i));
676 Value *SV = ConstantVector::get(Indices.begin(), Indices.size());
677 Rep = Builder.CreateShuffleVector(Op2, Op1, SV, "palignr");
678 Rep = Builder.CreateBitCast(Rep, F->getReturnType());
681 // If palignr is shifting the pair of input vectors more than 16 but less
682 // than 32 bytes, emit a logical right shift of the destination.
683 else if (shiftVal < 32) {
684 const Type *EltTy = Type::getInt64Ty(C);
685 const Type *VecTy = VectorType::get(EltTy, 2);
686 const Type *IntTy = Type::getInt32Ty(C);
688 Op1 = Builder.CreateBitCast(Op1, VecTy, "cast");
689 Op2 = ConstantInt::get(IntTy, (shiftVal-16) * 8);
691 // create i32 constant
693 Intrinsic::getDeclaration(F->getParent(), Intrinsic::x86_sse2_psrl_dq);
694 Rep = Builder.CreateCall2(I, Op1, Op2, "palignr");
697 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
699 Rep = Constant::getNullValue(F->getReturnType());
702 // Replace any uses with our new instruction.
703 if (!CI->use_empty())
704 CI->replaceAllUsesWith(Rep);
706 // Remove upgraded instruction.
707 CI->eraseFromParent();
710 llvm_unreachable("Unknown function for CallInst upgrade.");
715 switch (NewFn->getIntrinsicID()) {
716 default: llvm_unreachable("Unknown function for CallInst upgrade.");
717 case Intrinsic::arm_neon_vld1:
718 case Intrinsic::arm_neon_vld2:
719 case Intrinsic::arm_neon_vld3:
720 case Intrinsic::arm_neon_vld4:
721 case Intrinsic::arm_neon_vst1:
722 case Intrinsic::arm_neon_vst2:
723 case Intrinsic::arm_neon_vst3:
724 case Intrinsic::arm_neon_vst4:
725 case Intrinsic::arm_neon_vld2lane:
726 case Intrinsic::arm_neon_vld3lane:
727 case Intrinsic::arm_neon_vld4lane:
728 case Intrinsic::arm_neon_vst2lane:
729 case Intrinsic::arm_neon_vst3lane:
730 case Intrinsic::arm_neon_vst4lane: {
731 // Add a default alignment argument of 1.
732 SmallVector<Value*, 8> Operands(CS.arg_begin(), CS.arg_end());
733 Operands.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
734 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
736 NewCI->setTailCall(CI->isTailCall());
737 NewCI->setCallingConv(CI->getCallingConv());
739 // Handle any uses of the old CallInst.
740 if (!CI->use_empty())
741 // Replace all uses of the old call with the new cast which has the
743 CI->replaceAllUsesWith(NewCI);
745 // Clean up the old call now that it has been completely upgraded.
746 CI->eraseFromParent();
750 case Intrinsic::x86_mmx_psll_d:
751 case Intrinsic::x86_mmx_psll_q:
752 case Intrinsic::x86_mmx_psll_w:
753 case Intrinsic::x86_mmx_psra_d:
754 case Intrinsic::x86_mmx_psra_w:
755 case Intrinsic::x86_mmx_psrl_d:
756 case Intrinsic::x86_mmx_psrl_q:
757 case Intrinsic::x86_mmx_psrl_w: {
760 Operands[0] = CI->getArgOperand(0);
762 // Cast the second parameter to the correct type.
763 BitCastInst *BC = new BitCastInst(CI->getArgOperand(1),
764 NewFn->getFunctionType()->getParamType(1),
768 // Construct a new CallInst
769 CallInst *NewCI = CallInst::Create(NewFn, Operands, Operands+2,
770 "upgraded."+CI->getName(), CI);
771 NewCI->setTailCall(CI->isTailCall());
772 NewCI->setCallingConv(CI->getCallingConv());
774 // Handle any uses of the old CallInst.
775 if (!CI->use_empty())
776 // Replace all uses of the old call with the new cast which has the
778 CI->replaceAllUsesWith(NewCI);
780 // Clean up the old call now that it has been completely upgraded.
781 CI->eraseFromParent();
784 case Intrinsic::ctlz:
785 case Intrinsic::ctpop:
786 case Intrinsic::cttz: {
787 // Build a small vector of the original arguments.
788 SmallVector<Value*, 8> Operands(CS.arg_begin(), CS.arg_end());
790 // Construct a new CallInst
791 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
792 "upgraded."+CI->getName(), CI);
793 NewCI->setTailCall(CI->isTailCall());
794 NewCI->setCallingConv(CI->getCallingConv());
796 // Handle any uses of the old CallInst.
797 if (!CI->use_empty()) {
798 // Check for sign extend parameter attributes on the return values.
799 bool SrcSExt = NewFn->getAttributes().paramHasAttr(0, Attribute::SExt);
800 bool DestSExt = F->getAttributes().paramHasAttr(0, Attribute::SExt);
802 // Construct an appropriate cast from the new return type to the old.
803 CastInst *RetCast = CastInst::Create(
804 CastInst::getCastOpcode(NewCI, SrcSExt,
807 NewCI, F->getReturnType(),
808 NewCI->getName(), CI);
809 NewCI->moveBefore(RetCast);
811 // Replace all uses of the old call with the new cast which has the
813 CI->replaceAllUsesWith(RetCast);
816 // Clean up the old call now that it has been completely upgraded.
817 CI->eraseFromParent();
820 case Intrinsic::eh_selector:
821 case Intrinsic::eh_typeid_for: {
822 // Only the return type changed.
823 SmallVector<Value*, 8> Operands(CS.arg_begin(), CS.arg_end());
824 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
825 "upgraded." + CI->getName(), CI);
826 NewCI->setTailCall(CI->isTailCall());
827 NewCI->setCallingConv(CI->getCallingConv());
829 // Handle any uses of the old CallInst.
830 if (!CI->use_empty()) {
831 // Construct an appropriate cast from the new return type to the old.
833 CastInst::Create(CastInst::getCastOpcode(NewCI, true,
834 F->getReturnType(), true),
835 NewCI, F->getReturnType(), NewCI->getName(), CI);
836 CI->replaceAllUsesWith(RetCast);
838 CI->eraseFromParent();
841 case Intrinsic::memcpy:
842 case Intrinsic::memmove:
843 case Intrinsic::memset: {
845 const llvm::Type *I1Ty = llvm::Type::getInt1Ty(CI->getContext());
846 Value *Operands[5] = { CI->getArgOperand(0), CI->getArgOperand(1),
847 CI->getArgOperand(2), CI->getArgOperand(3),
848 llvm::ConstantInt::get(I1Ty, 0) };
849 CallInst *NewCI = CallInst::Create(NewFn, Operands, Operands+5,
851 NewCI->setTailCall(CI->isTailCall());
852 NewCI->setCallingConv(CI->getCallingConv());
853 // Handle any uses of the old CallInst.
854 if (!CI->use_empty())
855 // Replace all uses of the old call with the new cast which has the
857 CI->replaceAllUsesWith(NewCI);
859 // Clean up the old call now that it has been completely upgraded.
860 CI->eraseFromParent();
866 // This tests each Function to determine if it needs upgrading. When we find
867 // one we are interested in, we then upgrade all calls to reflect the new
869 void llvm::UpgradeCallsToIntrinsic(Function* F) {
870 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
872 // Upgrade the function and check if it is a totaly new function.
874 if (UpgradeIntrinsicFunction(F, NewFn)) {
876 // Replace all uses to the old function with the new one if necessary.
877 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
879 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
880 UpgradeIntrinsicCall(CI, NewFn);
882 // Remove old function, no longer used, from the module.
883 F->eraseFromParent();
888 /// This function strips all debug info intrinsics, except for llvm.dbg.declare.
889 /// If an llvm.dbg.declare intrinsic is invalid, then this function simply
891 void llvm::CheckDebugInfoIntrinsics(Module *M) {
894 if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) {
895 while (!FuncStart->use_empty()) {
896 CallInst *CI = cast<CallInst>(FuncStart->use_back());
897 CI->eraseFromParent();
899 FuncStart->eraseFromParent();
902 if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) {
903 while (!StopPoint->use_empty()) {
904 CallInst *CI = cast<CallInst>(StopPoint->use_back());
905 CI->eraseFromParent();
907 StopPoint->eraseFromParent();
910 if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) {
911 while (!RegionStart->use_empty()) {
912 CallInst *CI = cast<CallInst>(RegionStart->use_back());
913 CI->eraseFromParent();
915 RegionStart->eraseFromParent();
918 if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) {
919 while (!RegionEnd->use_empty()) {
920 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
921 CI->eraseFromParent();
923 RegionEnd->eraseFromParent();
926 if (Function *Declare = M->getFunction("llvm.dbg.declare")) {
927 if (!Declare->use_empty()) {
928 DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back());
929 if (!isa<MDNode>(DDI->getArgOperand(0)) ||
930 !isa<MDNode>(DDI->getArgOperand(1))) {
931 while (!Declare->use_empty()) {
932 CallInst *CI = cast<CallInst>(Declare->use_back());
933 CI->eraseFromParent();
935 Declare->eraseFromParent();