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, "vmlal", 5) == 0 ||
88 Name.compare(14, 5, "vmlsl", 5) == 0 ||
89 Name.compare(14, 5, "vabdl", 5) == 0 ||
90 Name.compare(14, 5, "vabal", 5) == 0) &&
91 (Name.compare(19, 2, "s.", 2) == 0 ||
92 Name.compare(19, 2, "u.", 2) == 0)) ||
94 (Name.compare(14, 4, "vaba", 4) == 0 &&
95 (Name.compare(18, 2, "s.", 2) == 0 ||
96 Name.compare(18, 2, "u.", 2) == 0)) ||
98 (Name.compare(14, 6, "vmovn.", 6) == 0)) {
100 // Calls to these are transformed into IR without intrinsics.
104 // Old versions of NEON ld/st intrinsics are missing alignment arguments.
105 bool isVLd = (Name.compare(14, 3, "vld", 3) == 0);
106 bool isVSt = (Name.compare(14, 3, "vst", 3) == 0);
107 if (isVLd || isVSt) {
108 unsigned NumVecs = Name.at(17) - '0';
109 if (NumVecs == 0 || NumVecs > 4)
111 bool isLaneOp = (Name.compare(18, 5, "lane.", 5) == 0);
112 if (!isLaneOp && Name.at(18) != '.')
114 unsigned ExpectedArgs = 2; // for the address and alignment
115 if (isVSt || isLaneOp)
116 ExpectedArgs += NumVecs;
118 ExpectedArgs += 1; // for the lane number
119 unsigned NumP = FTy->getNumParams();
120 if (NumP != ExpectedArgs - 1)
123 // Change the name of the old (bad) intrinsic, because
124 // its type is incorrect, but we cannot overload that name.
127 // One argument is missing: add the alignment argument.
128 std::vector<const Type*> NewParams;
129 for (unsigned p = 0; p < NumP; ++p)
130 NewParams.push_back(FTy->getParamType(p));
131 NewParams.push_back(Type::getInt32Ty(F->getContext()));
132 FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(),
134 NewFn = cast<Function>(M->getOrInsertFunction(Name, NewFTy));
140 // This upgrades the name of the llvm.bswap intrinsic function to only use
141 // a single type name for overloading. We only care about the old format
142 // 'llvm.bswap.i*.i*', so check for 'bswap.' and then for there being
143 // a '.' after 'bswap.'
144 if (Name.compare(5,6,"bswap.",6) == 0) {
145 std::string::size_type delim = Name.find('.',11);
147 if (delim != std::string::npos) {
148 // Construct the new name as 'llvm.bswap' + '.i*'
149 F->setName(Name.substr(0,10)+Name.substr(delim));
157 // We only want to fix the 'llvm.ct*' intrinsics which do not have the
158 // correct return type, so we check for the name, and then check if the
159 // return type does not match the parameter type.
160 if ( (Name.compare(5,5,"ctpop",5) == 0 ||
161 Name.compare(5,4,"ctlz",4) == 0 ||
162 Name.compare(5,4,"cttz",4) == 0) &&
163 FTy->getReturnType() != FTy->getParamType(0)) {
164 // We first need to change the name of the old (bad) intrinsic, because
165 // its type is incorrect, but we cannot overload that name. We
166 // arbitrarily unique it here allowing us to construct a correctly named
167 // and typed function below.
170 // Now construct the new intrinsic with the correct name and type. We
171 // leave the old function around in order to query its type, whatever it
172 // may be, and correctly convert up to the new type.
173 NewFn = cast<Function>(M->getOrInsertFunction(Name,
174 FTy->getParamType(0),
175 FTy->getParamType(0),
182 // The old llvm.eh.selector.i32 is equivalent to the new llvm.eh.selector.
183 if (Name.compare("llvm.eh.selector.i32") == 0) {
184 F->setName("llvm.eh.selector");
188 // The old llvm.eh.typeid.for.i32 is equivalent to llvm.eh.typeid.for.
189 if (Name.compare("llvm.eh.typeid.for.i32") == 0) {
190 F->setName("llvm.eh.typeid.for");
194 // Convert the old llvm.eh.selector.i64 to a call to llvm.eh.selector.
195 if (Name.compare("llvm.eh.selector.i64") == 0) {
196 NewFn = Intrinsic::getDeclaration(M, Intrinsic::eh_selector);
199 // Convert the old llvm.eh.typeid.for.i64 to a call to llvm.eh.typeid.for.
200 if (Name.compare("llvm.eh.typeid.for.i64") == 0) {
201 NewFn = Intrinsic::getDeclaration(M, Intrinsic::eh_typeid_for);
207 // This upgrades the llvm.memcpy, llvm.memmove, and llvm.memset to the
208 // new format that allows overloading the pointer for different address
209 // space (e.g., llvm.memcpy.i16 => llvm.memcpy.p0i8.p0i8.i16)
210 const char* NewFnName = NULL;
211 if (Name.compare(5,8,"memcpy.i",8) == 0) {
213 NewFnName = "llvm.memcpy.p0i8.p0i8.i8";
214 else if (Name.compare(13,2,"16") == 0)
215 NewFnName = "llvm.memcpy.p0i8.p0i8.i16";
216 else if (Name.compare(13,2,"32") == 0)
217 NewFnName = "llvm.memcpy.p0i8.p0i8.i32";
218 else if (Name.compare(13,2,"64") == 0)
219 NewFnName = "llvm.memcpy.p0i8.p0i8.i64";
220 } else if (Name.compare(5,9,"memmove.i",9) == 0) {
222 NewFnName = "llvm.memmove.p0i8.p0i8.i8";
223 else if (Name.compare(14,2,"16") == 0)
224 NewFnName = "llvm.memmove.p0i8.p0i8.i16";
225 else if (Name.compare(14,2,"32") == 0)
226 NewFnName = "llvm.memmove.p0i8.p0i8.i32";
227 else if (Name.compare(14,2,"64") == 0)
228 NewFnName = "llvm.memmove.p0i8.p0i8.i64";
230 else if (Name.compare(5,8,"memset.i",8) == 0) {
232 NewFnName = "llvm.memset.p0i8.i8";
233 else if (Name.compare(13,2,"16") == 0)
234 NewFnName = "llvm.memset.p0i8.i16";
235 else if (Name.compare(13,2,"32") == 0)
236 NewFnName = "llvm.memset.p0i8.i32";
237 else if (Name.compare(13,2,"64") == 0)
238 NewFnName = "llvm.memset.p0i8.i64";
241 NewFn = cast<Function>(M->getOrInsertFunction(NewFnName,
242 FTy->getReturnType(),
243 FTy->getParamType(0),
244 FTy->getParamType(1),
245 FTy->getParamType(2),
246 FTy->getParamType(3),
247 Type::getInt1Ty(F->getContext()),
254 // This upgrades the llvm.part.select overloaded intrinsic names to only
255 // use one type specifier in the name. We only care about the old format
256 // 'llvm.part.select.i*.i*', and solve as above with bswap.
257 if (Name.compare(5,12,"part.select.",12) == 0) {
258 std::string::size_type delim = Name.find('.',17);
260 if (delim != std::string::npos) {
261 // Construct a new name as 'llvm.part.select' + '.i*'
262 F->setName(Name.substr(0,16)+Name.substr(delim));
269 // This upgrades the llvm.part.set intrinsics similarly as above, however
270 // we care about 'llvm.part.set.i*.i*.i*', but only the first two types
271 // must match. There is an additional type specifier after these two
272 // matching types that we must retain when upgrading. Thus, we require
273 // finding 2 periods, not just one, after the intrinsic name.
274 if (Name.compare(5,9,"part.set.",9) == 0) {
275 std::string::size_type delim = Name.find('.',14);
277 if (delim != std::string::npos &&
278 Name.find('.',delim+1) != std::string::npos) {
279 // Construct a new name as 'llvm.part.select' + '.i*.i*'
280 F->setName(Name.substr(0,13)+Name.substr(delim));
289 // This fixes all MMX shift intrinsic instructions to take a
290 // x86_mmx instead of a v1i64, v2i32, v4i16, or v8i8.
291 if (Name.compare(5, 8, "x86.mmx.", 8) == 0) {
292 const Type *X86_MMXTy = VectorType::getX86_MMXTy(FTy->getContext());
294 if (Name.compare(13, 4, "padd", 4) == 0 ||
295 Name.compare(13, 4, "psub", 4) == 0 ||
296 Name.compare(13, 4, "pmul", 4) == 0 ||
297 Name.compare(13, 5, "pmadd", 5) == 0 ||
298 Name.compare(13, 4, "pand", 4) == 0 ||
299 Name.compare(13, 3, "por", 3) == 0 ||
300 Name.compare(13, 4, "pxor", 4) == 0 ||
301 Name.compare(13, 4, "pavg", 4) == 0 ||
302 Name.compare(13, 4, "pmax", 4) == 0 ||
303 Name.compare(13, 4, "pmin", 4) == 0 ||
304 Name.compare(13, 4, "psad", 4) == 0 ||
305 Name.compare(13, 4, "psll", 4) == 0 ||
306 Name.compare(13, 4, "psrl", 4) == 0 ||
307 Name.compare(13, 4, "psra", 4) == 0 ||
308 Name.compare(13, 4, "pack", 4) == 0 ||
309 Name.compare(13, 6, "punpck", 6) == 0 ||
310 Name.compare(13, 4, "pcmp", 4) == 0) {
311 assert(FTy->getNumParams() == 2 && "MMX intrinsic takes 2 args!");
312 const Type *SecondParamTy = X86_MMXTy;
314 if (Name.compare(13, 5, "pslli", 5) == 0 ||
315 Name.compare(13, 5, "psrli", 5) == 0 ||
316 Name.compare(13, 5, "psrai", 5) == 0)
317 SecondParamTy = FTy->getParamType(1);
319 // Don't do anything if it has the correct types.
320 if (FTy->getReturnType() == X86_MMXTy &&
321 FTy->getParamType(0) == X86_MMXTy &&
322 FTy->getParamType(1) == SecondParamTy)
325 // We first need to change the name of the old (bad) intrinsic, because
326 // its type is incorrect, but we cannot overload that name. We
327 // arbitrarily unique it here allowing us to construct a correctly named
328 // and typed function below.
331 // Now construct the new intrinsic with the correct name and type. We
332 // leave the old function around in order to query its type, whatever it
333 // may be, and correctly convert up to the new type.
334 NewFn = cast<Function>(M->getOrInsertFunction(Name,
335 X86_MMXTy, X86_MMXTy,
336 SecondParamTy, (Type*)0));
340 if (Name.compare(13, 8, "maskmovq", 8) == 0) {
341 // Don't do anything if it has the correct types.
342 if (FTy->getParamType(0) == X86_MMXTy &&
343 FTy->getParamType(1) == X86_MMXTy)
347 NewFn = cast<Function>(M->getOrInsertFunction(Name,
348 FTy->getReturnType(),
351 FTy->getParamType(2),
356 if (Name.compare(13, 8, "pmovmskb", 8) == 0) {
357 if (FTy->getParamType(0) == X86_MMXTy)
361 NewFn = cast<Function>(M->getOrInsertFunction(Name,
362 FTy->getReturnType(),
368 if (Name.compare(13, 5, "movnt", 5) == 0) {
369 if (FTy->getParamType(1) == X86_MMXTy)
373 NewFn = cast<Function>(M->getOrInsertFunction(Name,
374 FTy->getReturnType(),
375 FTy->getParamType(0),
381 if (Name.compare(13, 7, "palignr", 7) == 0) {
382 if (FTy->getReturnType() == X86_MMXTy &&
383 FTy->getParamType(0) == X86_MMXTy &&
384 FTy->getParamType(1) == X86_MMXTy)
388 NewFn = cast<Function>(M->getOrInsertFunction(Name,
392 FTy->getParamType(2),
397 if (Name.compare(13, 5, "pextr", 5) == 0) {
398 if (FTy->getParamType(0) == X86_MMXTy)
402 NewFn = cast<Function>(M->getOrInsertFunction(Name,
403 FTy->getReturnType(),
405 FTy->getParamType(1),
410 if (Name.compare(13, 5, "pinsr", 5) == 0) {
411 if (FTy->getReturnType() == X86_MMXTy &&
412 FTy->getParamType(0) == X86_MMXTy)
416 NewFn = cast<Function>(M->getOrInsertFunction(Name,
419 FTy->getParamType(1),
420 FTy->getParamType(2),
425 if (Name.compare(13, 12, "cvtsi32.si64", 12) == 0) {
426 if (FTy->getReturnType() == X86_MMXTy)
430 NewFn = cast<Function>(M->getOrInsertFunction(Name,
432 FTy->getParamType(0),
437 if (Name.compare(13, 12, "cvtsi64.si32", 12) == 0) {
438 if (FTy->getParamType(0) == X86_MMXTy)
442 NewFn = cast<Function>(M->getOrInsertFunction(Name,
443 FTy->getReturnType(),
449 if (Name.compare(13, 8, "vec.init", 8) == 0) {
450 if (FTy->getReturnType() == X86_MMXTy)
455 if (Name.compare(21, 2, ".b", 2) == 0)
456 NewFn = cast<Function>(M->getOrInsertFunction(Name,
458 FTy->getParamType(0),
459 FTy->getParamType(1),
460 FTy->getParamType(2),
461 FTy->getParamType(3),
462 FTy->getParamType(4),
463 FTy->getParamType(5),
464 FTy->getParamType(6),
465 FTy->getParamType(7),
467 else if (Name.compare(21, 2, ".w", 2) == 0)
468 NewFn = cast<Function>(M->getOrInsertFunction(Name,
470 FTy->getParamType(0),
471 FTy->getParamType(1),
472 FTy->getParamType(2),
473 FTy->getParamType(3),
475 else if (Name.compare(21, 2, ".d", 2) == 0)
476 NewFn = cast<Function>(M->getOrInsertFunction(Name,
478 FTy->getParamType(0),
479 FTy->getParamType(1),
485 if (Name.compare(13, 9, "vec.ext.d", 9) == 0) {
486 if (FTy->getReturnType() == X86_MMXTy &&
487 FTy->getParamType(0) == X86_MMXTy)
491 NewFn = cast<Function>(M->getOrInsertFunction(Name,
494 FTy->getParamType(1),
499 if (Name.compare(13, 9, "emms", 4) == 0 ||
500 Name.compare(13, 9, "femms", 5) == 0) {
505 // We really shouldn't get here ever.
506 assert(0 && "Invalid MMX intrinsic!");
508 } else if (Name.compare(5,17,"x86.sse2.loadh.pd",17) == 0 ||
509 Name.compare(5,17,"x86.sse2.loadl.pd",17) == 0 ||
510 Name.compare(5,16,"x86.sse2.movl.dq",16) == 0 ||
511 Name.compare(5,15,"x86.sse2.movs.d",15) == 0 ||
512 Name.compare(5,16,"x86.sse2.shuf.pd",16) == 0 ||
513 Name.compare(5,18,"x86.sse2.unpckh.pd",18) == 0 ||
514 Name.compare(5,18,"x86.sse2.unpckl.pd",18) == 0 ||
515 Name.compare(5,20,"x86.sse2.punpckh.qdq",20) == 0 ||
516 Name.compare(5,20,"x86.sse2.punpckl.qdq",20) == 0) {
517 // Calls to these intrinsics are transformed into ShuffleVector's.
520 } else if (Name.compare(5, 16, "x86.sse41.pmulld", 16) == 0) {
521 // Calls to these intrinsics are transformed into vector multiplies.
524 } else if (Name.compare(5, 18, "x86.ssse3.palign.r", 18) == 0 ||
525 Name.compare(5, 22, "x86.ssse3.palign.r.128", 22) == 0) {
526 // Calls to these intrinsics are transformed into vector shuffles, shifts,
530 } else if (Name.compare(5, 17, "x86.ssse3.pshuf.w", 17) == 0) {
531 // This is an SSE/MMX instruction.
532 const Type *X86_MMXTy = VectorType::getX86_MMXTy(FTy->getContext());
534 cast<Function>(M->getOrInsertFunction("llvm.x86.sse.pshuf.w",
537 Type::getInt8Ty(F->getContext()),
545 // This may not belong here. This function is effectively being overloaded
546 // to both detect an intrinsic which needs upgrading, and to provide the
547 // upgraded form of the intrinsic. We should perhaps have two separate
548 // functions for this.
552 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
554 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
556 // Upgrade intrinsic attributes. This does not change the function.
559 if (unsigned id = F->getIntrinsicID())
560 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
564 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
565 StringRef Name(GV->getName());
567 // We are only upgrading one symbol here.
568 if (Name == ".llvm.eh.catch.all.value") {
569 GV->setName("llvm.eh.catch.all.value");
576 /// ExtendNEONArgs - For NEON "long" and "wide" operations, where the results
577 /// have vector elements twice as big as one or both source operands, do the
578 /// sign- or zero-extension that used to be handled by intrinsics. The
579 /// extended values are returned via V0 and V1.
580 static void ExtendNEONArgs(CallInst *CI, Value *Arg0, Value *Arg1,
581 Value *&V0, Value *&V1) {
582 Function *F = CI->getCalledFunction();
583 const std::string& Name = F->getName();
584 bool isLong = (Name.at(18) == 'l');
585 bool isSigned = (Name.at(19) == 's');
589 V0 = new SExtInst(Arg0, CI->getType(), "", CI);
592 V1 = new SExtInst(Arg1, CI->getType(), "", CI);
595 V0 = new ZExtInst(Arg0, CI->getType(), "", CI);
598 V1 = new ZExtInst(Arg1, CI->getType(), "", CI);
602 /// CallVABD - As part of expanding a call to one of the old NEON vabdl, vaba,
603 /// or vabal intrinsics, construct a call to a vabd intrinsic. Examine the
604 /// name of the old intrinsic to determine whether to use a signed or unsigned
605 /// vabd intrinsic. Get the type from the old call instruction, adjusted for
606 /// half-size vector elements if the old intrinsic was vabdl or vabal.
607 static Instruction *CallVABD(CallInst *CI, Value *Arg0, Value *Arg1) {
608 Function *F = CI->getCalledFunction();
609 const std::string& Name = F->getName();
610 bool isLong = (Name.at(18) == 'l');
611 bool isSigned = (Name.at(isLong ? 19 : 18) == 's');
615 intID = Intrinsic::arm_neon_vabds;
617 intID = Intrinsic::arm_neon_vabdu;
619 const Type *Ty = CI->getType();
621 Ty = VectorType::getTruncatedElementVectorType(cast<const VectorType>(Ty));
623 Function *VABD = Intrinsic::getDeclaration(F->getParent(), intID, &Ty, 1);
627 return CallInst::Create(VABD, Operands, Operands+2,
628 "upgraded."+CI->getName(), CI);
631 /// ConstructNewCallInst - Construct a new CallInst with the signature of NewFn.
632 static void ConstructNewCallInst(Function *NewFn, CallInst *OldCI,
633 Value **Operands, unsigned NumOps,
634 bool AssignName = true) {
635 // Construct a new CallInst.
637 CallInst::Create(NewFn, Operands, Operands + NumOps,
638 AssignName ? "upgraded." + OldCI->getName() : "", OldCI);
640 NewCI->setTailCall(OldCI->isTailCall());
641 NewCI->setCallingConv(OldCI->getCallingConv());
643 // Handle any uses of the old CallInst. If the type has changed, add a cast.
644 if (!OldCI->use_empty()) {
645 if (OldCI->getType() != NewCI->getType()) {
646 Function *OldFn = OldCI->getCalledFunction();
648 CastInst::Create(CastInst::getCastOpcode(NewCI, true,
649 OldFn->getReturnType(), true),
650 NewCI, OldFn->getReturnType(), NewCI->getName(),OldCI);
652 // Replace all uses of the old call with the new cast which has the
654 OldCI->replaceAllUsesWith(RetCast);
656 OldCI->replaceAllUsesWith(NewCI);
660 // Clean up the old call now that it has been completely upgraded.
661 OldCI->eraseFromParent();
664 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
665 // upgraded intrinsic. All argument and return casting must be provided in
666 // order to seamlessly integrate with existing context.
667 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
668 Function *F = CI->getCalledFunction();
669 LLVMContext &C = CI->getContext();
670 ImmutableCallSite CS(CI);
672 assert(F && "CallInst has no function associated with it.");
675 // Get the Function's name.
676 const std::string& Name = F->getName();
678 // Upgrade ARM NEON intrinsics.
679 if (Name.compare(5, 9, "arm.neon.", 9) == 0) {
682 if (Name.compare(14, 7, "vmovls.", 7) == 0) {
683 NewI = new SExtInst(CI->getArgOperand(0), CI->getType(),
684 "upgraded." + CI->getName(), CI);
685 } else if (Name.compare(14, 7, "vmovlu.", 7) == 0) {
686 NewI = new ZExtInst(CI->getArgOperand(0), CI->getType(),
687 "upgraded." + CI->getName(), CI);
688 } else if (Name.compare(14, 4, "vadd", 4) == 0) {
689 ExtendNEONArgs(CI, CI->getArgOperand(0), CI->getArgOperand(1), V0, V1);
690 NewI = BinaryOperator::CreateAdd(V0, V1, "upgraded."+CI->getName(), CI);
691 } else if (Name.compare(14, 4, "vsub", 4) == 0) {
692 ExtendNEONArgs(CI, CI->getArgOperand(0), CI->getArgOperand(1), V0, V1);
693 NewI = BinaryOperator::CreateSub(V0, V1,"upgraded."+CI->getName(),CI);
694 } else if (Name.compare(14, 4, "vmul", 4) == 0) {
695 ExtendNEONArgs(CI, CI->getArgOperand(0), CI->getArgOperand(1), V0, V1);
696 NewI = BinaryOperator::CreateMul(V0, V1,"upgraded."+CI->getName(),CI);
697 } else if (Name.compare(14, 4, "vmla", 4) == 0) {
698 ExtendNEONArgs(CI, CI->getArgOperand(1), CI->getArgOperand(2), V0, V1);
699 Instruction *MulI = BinaryOperator::CreateMul(V0, V1, "", CI);
700 NewI = BinaryOperator::CreateAdd(CI->getArgOperand(0), MulI,
701 "upgraded."+CI->getName(), CI);
702 } else if (Name.compare(14, 4, "vmls", 4) == 0) {
703 ExtendNEONArgs(CI, CI->getArgOperand(1), CI->getArgOperand(2), V0, V1);
704 Instruction *MulI = BinaryOperator::CreateMul(V0, V1, "", CI);
705 NewI = BinaryOperator::CreateSub(CI->getArgOperand(0), MulI,
706 "upgraded."+CI->getName(), CI);
707 } else if (Name.compare(14, 4, "vabd", 4) == 0) {
708 NewI = CallVABD(CI, CI->getArgOperand(0), CI->getArgOperand(1));
709 NewI = new ZExtInst(NewI, CI->getType(), "upgraded."+CI->getName(), CI);
710 } else if (Name.compare(14, 4, "vaba", 4) == 0) {
711 NewI = CallVABD(CI, CI->getArgOperand(1), CI->getArgOperand(2));
712 if (Name.at(18) == 'l')
713 NewI = new ZExtInst(NewI, CI->getType(), "", CI);
714 NewI = BinaryOperator::CreateAdd(CI->getArgOperand(0), NewI,
715 "upgraded."+CI->getName(), CI);
716 } else if (Name.compare(14, 6, "vmovn.", 6) == 0) {
717 NewI = new TruncInst(CI->getArgOperand(0), CI->getType(),
718 "upgraded." + CI->getName(), CI);
720 llvm_unreachable("Unknown arm.neon function for CallInst upgrade.");
722 // Replace any uses of the old CallInst.
723 if (!CI->use_empty())
724 CI->replaceAllUsesWith(NewI);
725 CI->eraseFromParent();
729 bool isLoadH = false, isLoadL = false, isMovL = false;
730 bool isMovSD = false, isShufPD = false;
731 bool isUnpckhPD = false, isUnpcklPD = false;
732 bool isPunpckhQPD = false, isPunpcklQPD = false;
733 if (F->getName() == "llvm.x86.sse2.loadh.pd")
735 else if (F->getName() == "llvm.x86.sse2.loadl.pd")
737 else if (F->getName() == "llvm.x86.sse2.movl.dq")
739 else if (F->getName() == "llvm.x86.sse2.movs.d")
741 else if (F->getName() == "llvm.x86.sse2.shuf.pd")
743 else if (F->getName() == "llvm.x86.sse2.unpckh.pd")
745 else if (F->getName() == "llvm.x86.sse2.unpckl.pd")
747 else if (F->getName() == "llvm.x86.sse2.punpckh.qdq")
749 else if (F->getName() == "llvm.x86.sse2.punpckl.qdq")
752 if (isLoadH || isLoadL || isMovL || isMovSD || isShufPD ||
753 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
754 std::vector<Constant*> Idxs;
755 Value *Op0 = CI->getArgOperand(0);
756 ShuffleVectorInst *SI = NULL;
757 if (isLoadH || isLoadL) {
758 Value *Op1 = UndefValue::get(Op0->getType());
759 Value *Addr = new BitCastInst(CI->getArgOperand(1),
760 Type::getDoublePtrTy(C),
762 Value *Load = new LoadInst(Addr, "upgraded.", false, 8, CI);
763 Value *Idx = ConstantInt::get(Type::getInt32Ty(C), 0);
764 Op1 = InsertElementInst::Create(Op1, Load, Idx, "upgraded.", CI);
767 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 0));
768 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
770 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
771 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
773 Value *Mask = ConstantVector::get(Idxs);
774 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
776 Constant *Zero = ConstantInt::get(Type::getInt32Ty(C), 0);
777 Idxs.push_back(Zero);
778 Idxs.push_back(Zero);
779 Idxs.push_back(Zero);
780 Idxs.push_back(Zero);
781 Value *ZeroV = ConstantVector::get(Idxs);
784 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 4));
785 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 5));
786 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
787 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 3));
788 Value *Mask = ConstantVector::get(Idxs);
789 SI = new ShuffleVectorInst(ZeroV, Op0, Mask, "upgraded.", CI);
790 } else if (isMovSD ||
791 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
792 Value *Op1 = CI->getArgOperand(1);
794 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
795 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
796 } else if (isUnpckhPD || isPunpckhQPD) {
797 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
798 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 3));
800 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 0));
801 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
803 Value *Mask = ConstantVector::get(Idxs);
804 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
805 } else if (isShufPD) {
806 Value *Op1 = CI->getArgOperand(1);
808 cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
809 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), MaskVal & 1));
810 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C),
811 ((MaskVal >> 1) & 1)+2));
812 Value *Mask = ConstantVector::get(Idxs);
813 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
816 assert(SI && "Unexpected!");
818 // Handle any uses of the old CallInst.
819 if (!CI->use_empty())
820 // Replace all uses of the old call with the new cast which has the
822 CI->replaceAllUsesWith(SI);
824 // Clean up the old call now that it has been completely upgraded.
825 CI->eraseFromParent();
826 } else if (F->getName() == "llvm.x86.sse41.pmulld") {
827 // Upgrade this set of intrinsics into vector multiplies.
828 Instruction *Mul = BinaryOperator::CreateMul(CI->getArgOperand(0),
829 CI->getArgOperand(1),
832 // Fix up all the uses with our new multiply.
833 if (!CI->use_empty())
834 CI->replaceAllUsesWith(Mul);
836 // Remove upgraded multiply.
837 CI->eraseFromParent();
838 } else if (F->getName() == "llvm.x86.ssse3.palign.r") {
839 Value *Op1 = CI->getArgOperand(0);
840 Value *Op2 = CI->getArgOperand(1);
841 Value *Op3 = CI->getArgOperand(2);
842 unsigned shiftVal = cast<ConstantInt>(Op3)->getZExtValue();
844 IRBuilder<> Builder(C);
845 Builder.SetInsertPoint(CI->getParent(), CI);
847 // If palignr is shifting the pair of input vectors less than 9 bytes,
848 // emit a shuffle instruction.
850 const Type *IntTy = Type::getInt32Ty(C);
851 const Type *EltTy = Type::getInt8Ty(C);
852 const Type *VecTy = VectorType::get(EltTy, 8);
854 Op2 = Builder.CreateBitCast(Op2, VecTy);
855 Op1 = Builder.CreateBitCast(Op1, VecTy);
857 llvm::SmallVector<llvm::Constant*, 8> Indices;
858 for (unsigned i = 0; i != 8; ++i)
859 Indices.push_back(ConstantInt::get(IntTy, shiftVal + i));
861 Value *SV = ConstantVector::get(Indices);
862 Rep = Builder.CreateShuffleVector(Op2, Op1, SV, "palignr");
863 Rep = Builder.CreateBitCast(Rep, F->getReturnType());
866 // If palignr is shifting the pair of input vectors more than 8 but less
867 // than 16 bytes, emit a logical right shift of the destination.
868 else if (shiftVal < 16) {
869 // MMX has these as 1 x i64 vectors for some odd optimization reasons.
870 const Type *EltTy = Type::getInt64Ty(C);
871 const Type *VecTy = VectorType::get(EltTy, 1);
873 Op1 = Builder.CreateBitCast(Op1, VecTy, "cast");
874 Op2 = ConstantInt::get(VecTy, (shiftVal-8) * 8);
876 // create i32 constant
878 Intrinsic::getDeclaration(F->getParent(), Intrinsic::x86_mmx_psrl_q);
879 Rep = Builder.CreateCall2(I, Op1, Op2, "palignr");
882 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
884 Rep = Constant::getNullValue(F->getReturnType());
887 // Replace any uses with our new instruction.
888 if (!CI->use_empty())
889 CI->replaceAllUsesWith(Rep);
891 // Remove upgraded instruction.
892 CI->eraseFromParent();
894 } else if (F->getName() == "llvm.x86.ssse3.palign.r.128") {
895 Value *Op1 = CI->getArgOperand(0);
896 Value *Op2 = CI->getArgOperand(1);
897 Value *Op3 = CI->getArgOperand(2);
898 unsigned shiftVal = cast<ConstantInt>(Op3)->getZExtValue();
900 IRBuilder<> Builder(C);
901 Builder.SetInsertPoint(CI->getParent(), CI);
903 // If palignr is shifting the pair of input vectors less than 17 bytes,
904 // emit a shuffle instruction.
905 if (shiftVal <= 16) {
906 const Type *IntTy = Type::getInt32Ty(C);
907 const Type *EltTy = Type::getInt8Ty(C);
908 const Type *VecTy = VectorType::get(EltTy, 16);
910 Op2 = Builder.CreateBitCast(Op2, VecTy);
911 Op1 = Builder.CreateBitCast(Op1, VecTy);
913 llvm::SmallVector<llvm::Constant*, 16> Indices;
914 for (unsigned i = 0; i != 16; ++i)
915 Indices.push_back(ConstantInt::get(IntTy, shiftVal + i));
917 Value *SV = ConstantVector::get(Indices);
918 Rep = Builder.CreateShuffleVector(Op2, Op1, SV, "palignr");
919 Rep = Builder.CreateBitCast(Rep, F->getReturnType());
922 // If palignr is shifting the pair of input vectors more than 16 but less
923 // than 32 bytes, emit a logical right shift of the destination.
924 else if (shiftVal < 32) {
925 const Type *EltTy = Type::getInt64Ty(C);
926 const Type *VecTy = VectorType::get(EltTy, 2);
927 const Type *IntTy = Type::getInt32Ty(C);
929 Op1 = Builder.CreateBitCast(Op1, VecTy, "cast");
930 Op2 = ConstantInt::get(IntTy, (shiftVal-16) * 8);
932 // create i32 constant
934 Intrinsic::getDeclaration(F->getParent(), Intrinsic::x86_sse2_psrl_dq);
935 Rep = Builder.CreateCall2(I, Op1, Op2, "palignr");
938 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
940 Rep = Constant::getNullValue(F->getReturnType());
943 // Replace any uses with our new instruction.
944 if (!CI->use_empty())
945 CI->replaceAllUsesWith(Rep);
947 // Remove upgraded instruction.
948 CI->eraseFromParent();
951 llvm_unreachable("Unknown function for CallInst upgrade.");
956 switch (NewFn->getIntrinsicID()) {
957 default: llvm_unreachable("Unknown function for CallInst upgrade.");
958 case Intrinsic::arm_neon_vld1:
959 case Intrinsic::arm_neon_vld2:
960 case Intrinsic::arm_neon_vld3:
961 case Intrinsic::arm_neon_vld4:
962 case Intrinsic::arm_neon_vst1:
963 case Intrinsic::arm_neon_vst2:
964 case Intrinsic::arm_neon_vst3:
965 case Intrinsic::arm_neon_vst4:
966 case Intrinsic::arm_neon_vld2lane:
967 case Intrinsic::arm_neon_vld3lane:
968 case Intrinsic::arm_neon_vld4lane:
969 case Intrinsic::arm_neon_vst2lane:
970 case Intrinsic::arm_neon_vst3lane:
971 case Intrinsic::arm_neon_vst4lane: {
972 // Add a default alignment argument of 1.
973 SmallVector<Value*, 8> Operands(CS.arg_begin(), CS.arg_end());
974 Operands.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
975 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
977 NewCI->setTailCall(CI->isTailCall());
978 NewCI->setCallingConv(CI->getCallingConv());
980 // Handle any uses of the old CallInst.
981 if (!CI->use_empty())
982 // Replace all uses of the old call with the new cast which has the
984 CI->replaceAllUsesWith(NewCI);
986 // Clean up the old call now that it has been completely upgraded.
987 CI->eraseFromParent();
991 case Intrinsic::x86_mmx_padd_b:
992 case Intrinsic::x86_mmx_padd_w:
993 case Intrinsic::x86_mmx_padd_d:
994 case Intrinsic::x86_mmx_padd_q:
995 case Intrinsic::x86_mmx_padds_b:
996 case Intrinsic::x86_mmx_padds_w:
997 case Intrinsic::x86_mmx_paddus_b:
998 case Intrinsic::x86_mmx_paddus_w:
999 case Intrinsic::x86_mmx_psub_b:
1000 case Intrinsic::x86_mmx_psub_w:
1001 case Intrinsic::x86_mmx_psub_d:
1002 case Intrinsic::x86_mmx_psub_q:
1003 case Intrinsic::x86_mmx_psubs_b:
1004 case Intrinsic::x86_mmx_psubs_w:
1005 case Intrinsic::x86_mmx_psubus_b:
1006 case Intrinsic::x86_mmx_psubus_w:
1007 case Intrinsic::x86_mmx_pmulh_w:
1008 case Intrinsic::x86_mmx_pmull_w:
1009 case Intrinsic::x86_mmx_pmulhu_w:
1010 case Intrinsic::x86_mmx_pmulu_dq:
1011 case Intrinsic::x86_mmx_pmadd_wd:
1012 case Intrinsic::x86_mmx_pand:
1013 case Intrinsic::x86_mmx_pandn:
1014 case Intrinsic::x86_mmx_por:
1015 case Intrinsic::x86_mmx_pxor:
1016 case Intrinsic::x86_mmx_pavg_b:
1017 case Intrinsic::x86_mmx_pavg_w:
1018 case Intrinsic::x86_mmx_pmaxu_b:
1019 case Intrinsic::x86_mmx_pmaxs_w:
1020 case Intrinsic::x86_mmx_pminu_b:
1021 case Intrinsic::x86_mmx_pmins_w:
1022 case Intrinsic::x86_mmx_psad_bw:
1023 case Intrinsic::x86_mmx_psll_w:
1024 case Intrinsic::x86_mmx_psll_d:
1025 case Intrinsic::x86_mmx_psll_q:
1026 case Intrinsic::x86_mmx_pslli_w:
1027 case Intrinsic::x86_mmx_pslli_d:
1028 case Intrinsic::x86_mmx_pslli_q:
1029 case Intrinsic::x86_mmx_psrl_w:
1030 case Intrinsic::x86_mmx_psrl_d:
1031 case Intrinsic::x86_mmx_psrl_q:
1032 case Intrinsic::x86_mmx_psrli_w:
1033 case Intrinsic::x86_mmx_psrli_d:
1034 case Intrinsic::x86_mmx_psrli_q:
1035 case Intrinsic::x86_mmx_psra_w:
1036 case Intrinsic::x86_mmx_psra_d:
1037 case Intrinsic::x86_mmx_psrai_w:
1038 case Intrinsic::x86_mmx_psrai_d:
1039 case Intrinsic::x86_mmx_packsswb:
1040 case Intrinsic::x86_mmx_packssdw:
1041 case Intrinsic::x86_mmx_packuswb:
1042 case Intrinsic::x86_mmx_punpckhbw:
1043 case Intrinsic::x86_mmx_punpckhwd:
1044 case Intrinsic::x86_mmx_punpckhdq:
1045 case Intrinsic::x86_mmx_punpcklbw:
1046 case Intrinsic::x86_mmx_punpcklwd:
1047 case Intrinsic::x86_mmx_punpckldq:
1048 case Intrinsic::x86_mmx_pcmpeq_b:
1049 case Intrinsic::x86_mmx_pcmpeq_w:
1050 case Intrinsic::x86_mmx_pcmpeq_d:
1051 case Intrinsic::x86_mmx_pcmpgt_b:
1052 case Intrinsic::x86_mmx_pcmpgt_w:
1053 case Intrinsic::x86_mmx_pcmpgt_d: {
1056 // Cast the operand to the X86 MMX type.
1057 Operands[0] = new BitCastInst(CI->getArgOperand(0),
1058 NewFn->getFunctionType()->getParamType(0),
1061 switch (NewFn->getIntrinsicID()) {
1063 // Cast to the X86 MMX type.
1064 Operands[1] = new BitCastInst(CI->getArgOperand(1),
1065 NewFn->getFunctionType()->getParamType(1),
1068 case Intrinsic::x86_mmx_pslli_w:
1069 case Intrinsic::x86_mmx_pslli_d:
1070 case Intrinsic::x86_mmx_pslli_q:
1071 case Intrinsic::x86_mmx_psrli_w:
1072 case Intrinsic::x86_mmx_psrli_d:
1073 case Intrinsic::x86_mmx_psrli_q:
1074 case Intrinsic::x86_mmx_psrai_w:
1075 case Intrinsic::x86_mmx_psrai_d:
1076 // These take an i32 as their second parameter.
1077 Operands[1] = CI->getArgOperand(1);
1081 ConstructNewCallInst(NewFn, CI, Operands, 2);
1084 case Intrinsic::x86_mmx_maskmovq: {
1087 // Cast the operands to the X86 MMX type.
1088 Operands[0] = new BitCastInst(CI->getArgOperand(0),
1089 NewFn->getFunctionType()->getParamType(0),
1091 Operands[1] = new BitCastInst(CI->getArgOperand(1),
1092 NewFn->getFunctionType()->getParamType(1),
1094 Operands[2] = CI->getArgOperand(2);
1096 ConstructNewCallInst(NewFn, CI, Operands, 3, false);
1099 case Intrinsic::x86_mmx_pmovmskb: {
1102 // Cast the operand to the X86 MMX type.
1103 Operands[0] = new BitCastInst(CI->getArgOperand(0),
1104 NewFn->getFunctionType()->getParamType(0),
1107 ConstructNewCallInst(NewFn, CI, Operands, 1);
1110 case Intrinsic::x86_mmx_movnt_dq: {
1113 Operands[0] = CI->getArgOperand(0);
1115 // Cast the operand to the X86 MMX type.
1116 Operands[1] = new BitCastInst(CI->getArgOperand(1),
1117 NewFn->getFunctionType()->getParamType(1),
1120 ConstructNewCallInst(NewFn, CI, Operands, 2, false);
1123 case Intrinsic::x86_mmx_palignr_b: {
1126 // Cast the operands to the X86 MMX type.
1127 Operands[0] = new BitCastInst(CI->getArgOperand(0),
1128 NewFn->getFunctionType()->getParamType(0),
1130 Operands[1] = new BitCastInst(CI->getArgOperand(1),
1131 NewFn->getFunctionType()->getParamType(1),
1133 Operands[2] = CI->getArgOperand(2);
1135 ConstructNewCallInst(NewFn, CI, Operands, 3);
1138 case Intrinsic::x86_mmx_pextr_w: {
1141 // Cast the operands to the X86 MMX type.
1142 Operands[0] = new BitCastInst(CI->getArgOperand(0),
1143 NewFn->getFunctionType()->getParamType(0),
1145 Operands[1] = CI->getArgOperand(1);
1147 ConstructNewCallInst(NewFn, CI, Operands, 2);
1150 case Intrinsic::x86_mmx_pinsr_w: {
1153 // Cast the operands to the X86 MMX type.
1154 Operands[0] = new BitCastInst(CI->getArgOperand(0),
1155 NewFn->getFunctionType()->getParamType(0),
1157 Operands[1] = CI->getArgOperand(1);
1158 Operands[2] = CI->getArgOperand(2);
1160 ConstructNewCallInst(NewFn, CI, Operands, 3);
1163 case Intrinsic::x86_sse_pshuf_w: {
1164 IRBuilder<> Builder(C);
1165 Builder.SetInsertPoint(CI->getParent(), CI);
1167 // Cast the operand to the X86 MMX type.
1170 Builder.CreateBitCast(CI->getArgOperand(0),
1171 NewFn->getFunctionType()->getParamType(0),
1174 Builder.CreateTrunc(CI->getArgOperand(1),
1178 ConstructNewCallInst(NewFn, CI, Operands, 2);
1182 case Intrinsic::ctlz:
1183 case Intrinsic::ctpop:
1184 case Intrinsic::cttz: {
1185 // Build a small vector of the original arguments.
1186 SmallVector<Value*, 8> Operands(CS.arg_begin(), CS.arg_end());
1188 // Construct a new CallInst
1189 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
1190 "upgraded."+CI->getName(), CI);
1191 NewCI->setTailCall(CI->isTailCall());
1192 NewCI->setCallingConv(CI->getCallingConv());
1194 // Handle any uses of the old CallInst.
1195 if (!CI->use_empty()) {
1196 // Check for sign extend parameter attributes on the return values.
1197 bool SrcSExt = NewFn->getAttributes().paramHasAttr(0, Attribute::SExt);
1198 bool DestSExt = F->getAttributes().paramHasAttr(0, Attribute::SExt);
1200 // Construct an appropriate cast from the new return type to the old.
1201 CastInst *RetCast = CastInst::Create(
1202 CastInst::getCastOpcode(NewCI, SrcSExt,
1205 NewCI, F->getReturnType(),
1206 NewCI->getName(), CI);
1207 NewCI->moveBefore(RetCast);
1209 // Replace all uses of the old call with the new cast which has the
1211 CI->replaceAllUsesWith(RetCast);
1214 // Clean up the old call now that it has been completely upgraded.
1215 CI->eraseFromParent();
1218 case Intrinsic::eh_selector:
1219 case Intrinsic::eh_typeid_for: {
1220 // Only the return type changed.
1221 SmallVector<Value*, 8> Operands(CS.arg_begin(), CS.arg_end());
1222 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
1223 "upgraded." + CI->getName(), CI);
1224 NewCI->setTailCall(CI->isTailCall());
1225 NewCI->setCallingConv(CI->getCallingConv());
1227 // Handle any uses of the old CallInst.
1228 if (!CI->use_empty()) {
1229 // Construct an appropriate cast from the new return type to the old.
1231 CastInst::Create(CastInst::getCastOpcode(NewCI, true,
1232 F->getReturnType(), true),
1233 NewCI, F->getReturnType(), NewCI->getName(), CI);
1234 CI->replaceAllUsesWith(RetCast);
1236 CI->eraseFromParent();
1239 case Intrinsic::memcpy:
1240 case Intrinsic::memmove:
1241 case Intrinsic::memset: {
1243 const llvm::Type *I1Ty = llvm::Type::getInt1Ty(CI->getContext());
1244 Value *Operands[5] = { CI->getArgOperand(0), CI->getArgOperand(1),
1245 CI->getArgOperand(2), CI->getArgOperand(3),
1246 llvm::ConstantInt::get(I1Ty, 0) };
1247 CallInst *NewCI = CallInst::Create(NewFn, Operands, Operands+5,
1249 NewCI->setTailCall(CI->isTailCall());
1250 NewCI->setCallingConv(CI->getCallingConv());
1251 // Handle any uses of the old CallInst.
1252 if (!CI->use_empty())
1253 // Replace all uses of the old call with the new cast which has the
1255 CI->replaceAllUsesWith(NewCI);
1257 // Clean up the old call now that it has been completely upgraded.
1258 CI->eraseFromParent();
1264 // This tests each Function to determine if it needs upgrading. When we find
1265 // one we are interested in, we then upgrade all calls to reflect the new
1267 void llvm::UpgradeCallsToIntrinsic(Function* F) {
1268 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
1270 // Upgrade the function and check if it is a totaly new function.
1272 if (UpgradeIntrinsicFunction(F, NewFn)) {
1274 // Replace all uses to the old function with the new one if necessary.
1275 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
1277 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
1278 UpgradeIntrinsicCall(CI, NewFn);
1280 // Remove old function, no longer used, from the module.
1281 F->eraseFromParent();
1286 /// This function strips all debug info intrinsics, except for llvm.dbg.declare.
1287 /// If an llvm.dbg.declare intrinsic is invalid, then this function simply
1288 /// strips that use.
1289 void llvm::CheckDebugInfoIntrinsics(Module *M) {
1292 if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) {
1293 while (!FuncStart->use_empty()) {
1294 CallInst *CI = cast<CallInst>(FuncStart->use_back());
1295 CI->eraseFromParent();
1297 FuncStart->eraseFromParent();
1300 if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) {
1301 while (!StopPoint->use_empty()) {
1302 CallInst *CI = cast<CallInst>(StopPoint->use_back());
1303 CI->eraseFromParent();
1305 StopPoint->eraseFromParent();
1308 if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) {
1309 while (!RegionStart->use_empty()) {
1310 CallInst *CI = cast<CallInst>(RegionStart->use_back());
1311 CI->eraseFromParent();
1313 RegionStart->eraseFromParent();
1316 if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) {
1317 while (!RegionEnd->use_empty()) {
1318 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
1319 CI->eraseFromParent();
1321 RegionEnd->eraseFromParent();
1324 if (Function *Declare = M->getFunction("llvm.dbg.declare")) {
1325 if (!Declare->use_empty()) {
1326 DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back());
1327 if (!isa<MDNode>(DDI->getArgOperand(0)) ||
1328 !isa<MDNode>(DDI->getArgOperand(1))) {
1329 while (!Declare->use_empty()) {
1330 CallInst *CI = cast<CallInst>(Declare->use_back());
1331 CI->eraseFromParent();
1333 Declare->eraseFromParent();