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/IR/AutoUpgrade.h"
15 #include "llvm/IR/CFG.h"
16 #include "llvm/IR/CallSite.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DIBuilder.h"
19 #include "llvm/IR/DebugInfo.h"
20 #include "llvm/IR/DiagnosticInfo.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/IRBuilder.h"
23 #include "llvm/IR/Instruction.h"
24 #include "llvm/IR/IntrinsicInst.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/Support/ErrorHandling.h"
31 // Upgrade the declarations of the SSE4.1 functions whose arguments have
32 // changed their type from v4f32 to v2i64.
33 static bool UpgradeSSE41Function(Function* F, Intrinsic::ID IID,
35 // Check whether this is an old version of the function, which received
37 Type *Arg0Type = F->getFunctionType()->getParamType(0);
38 if (Arg0Type != VectorType::get(Type::getFloatTy(F->getContext()), 4))
41 // Yes, it's old, replace it with new version.
42 F->setName(F->getName() + ".old");
43 NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
47 // Upgrade the declarations of intrinsic functions whose 8-bit immediate mask
48 // arguments have changed their type from i32 to i8.
49 static bool UpgradeX86IntrinsicsWith8BitMask(Function *F, Intrinsic::ID IID,
51 // Check that the last argument is an i32.
52 Type *LastArgType = F->getFunctionType()->getParamType(
53 F->getFunctionType()->getNumParams() - 1);
54 if (!LastArgType->isIntegerTy(32))
57 // Move this function aside and map down.
58 F->setName(F->getName() + ".old");
59 NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
63 // Upgrade the declarations of AVX-512 cmp intrinsic functions whose 8-bit
64 // immediates have changed their type from i32 to i8.
65 static bool UpgradeAVX512CmpIntrinsic(Function *F, Intrinsic::ID IID,
67 // Check that the last argument is an i32.
68 Type *LastArgType = F->getFunctionType()->getParamType(2);
69 if (!LastArgType->isIntegerTy(32))
72 // Move this function aside and map down.
73 F->setName(F->getName() + ".old");
74 NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
78 static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
79 assert(F && "Illegal to upgrade a non-existent Function.");
81 // Quickly eliminate it, if it's not a candidate.
82 StringRef Name = F->getName();
83 if (Name.size() <= 8 || !Name.startswith("llvm."))
85 Name = Name.substr(5); // Strip off "llvm."
90 if (Name.startswith("arm.neon.vclz")) {
92 F->arg_begin()->getType(),
93 Type::getInt1Ty(F->getContext())
95 // Can't use Intrinsic::getDeclaration here as it adds a ".i1" to
96 // the end of the name. Change name from llvm.arm.neon.vclz.* to
98 FunctionType* fType = FunctionType::get(F->getReturnType(), args, false);
99 NewFn = Function::Create(fType, F->getLinkage(),
100 "llvm.ctlz." + Name.substr(14), F->getParent());
103 if (Name.startswith("arm.neon.vcnt")) {
104 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctpop,
105 F->arg_begin()->getType());
111 if (Name.startswith("ctlz.") && F->arg_size() == 1) {
112 F->setName(Name + ".old");
113 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
114 F->arg_begin()->getType());
117 if (Name.startswith("cttz.") && F->arg_size() == 1) {
118 F->setName(Name + ".old");
119 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz,
120 F->arg_begin()->getType());
126 if (Name.startswith("dbg.declare") && F->arg_size() == 2) {
127 F->setName(Name + ".old");
128 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::dbg_declare);
131 if (Name.startswith("dbg.value") && F->arg_size() == 3) {
132 F->setName(Name + ".old");
133 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::dbg_value);
140 // We only need to change the name to match the mangling including the
142 if (F->arg_size() == 2 && Name.startswith("objectsize.")) {
143 Type *Tys[2] = { F->getReturnType(), F->arg_begin()->getType() };
144 if (F->getName() != Intrinsic::getName(Intrinsic::objectsize, Tys)) {
145 F->setName(Name + ".old");
146 NewFn = Intrinsic::getDeclaration(F->getParent(),
147 Intrinsic::objectsize, Tys);
154 if (Name.startswith("x86.sse2.pcmpeq.") ||
155 Name.startswith("x86.sse2.pcmpgt.") ||
156 Name.startswith("x86.avx2.pcmpeq.") ||
157 Name.startswith("x86.avx2.pcmpgt.") ||
158 Name.startswith("x86.avx.vpermil.") ||
159 Name == "x86.avx.movnt.dq.256" ||
160 Name == "x86.avx.movnt.pd.256" ||
161 Name == "x86.avx.movnt.ps.256" ||
162 Name == "x86.sse42.crc32.64.8" ||
163 Name == "x86.avx.vbroadcast.ss" ||
164 Name == "x86.avx.vbroadcast.ss.256" ||
165 Name == "x86.avx.vbroadcast.sd.256" ||
166 Name == "x86.sse2.psll.dq.bs" ||
167 Name == "x86.sse2.psrl.dq.bs" ||
168 (Name.startswith("x86.xop.vpcom") && F->arg_size() == 2)) {
172 // SSE4.1 ptest functions may have an old signature.
173 if (Name.startswith("x86.sse41.ptest")) {
174 if (Name == "x86.sse41.ptestc")
175 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn);
176 if (Name == "x86.sse41.ptestz")
177 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn);
178 if (Name == "x86.sse41.ptestnzc")
179 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn);
181 // Several blend and other instructions with maskes used the wrong number of
183 if (Name == "x86.sse41.pblendw")
184 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_pblendw,
186 if (Name == "x86.sse41.blendpd")
187 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_blendpd,
189 if (Name == "x86.sse41.blendps")
190 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_blendps,
192 if (Name == "x86.sse41.insertps")
193 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_insertps,
195 if (Name == "x86.sse41.dppd")
196 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dppd,
198 if (Name == "x86.sse41.dpps")
199 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dpps,
201 if (Name == "x86.sse41.mpsadbw")
202 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_mpsadbw,
204 if (Name == "x86.avx.blend.pd.256")
205 return UpgradeX86IntrinsicsWith8BitMask(
206 F, Intrinsic::x86_avx_blend_pd_256, NewFn);
207 if (Name == "x86.avx.blend.ps.256")
208 return UpgradeX86IntrinsicsWith8BitMask(
209 F, Intrinsic::x86_avx_blend_ps_256, NewFn);
210 if (Name == "x86.avx.dp.ps.256")
211 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx_dp_ps_256,
213 if (Name == "x86.avx2.pblendw")
214 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_pblendw,
216 if (Name == "x86.avx2.pblendd.128")
217 return UpgradeX86IntrinsicsWith8BitMask(
218 F, Intrinsic::x86_avx2_pblendd_128, NewFn);
219 if (Name == "x86.avx2.pblendd.256")
220 return UpgradeX86IntrinsicsWith8BitMask(
221 F, Intrinsic::x86_avx2_pblendd_256, NewFn);
222 if (Name == "x86.avx2.mpsadbw")
223 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_mpsadbw,
226 if (Name == "x86.avx512.mask.cmp.ps.512")
227 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_ps_512,
229 if (Name == "x86.avx512.mask.cmp.pd.512")
230 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_pd_512,
233 if (Name == "x86.avx512.mask.cmp.b.512")
234 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_b_512,
236 if (Name == "x86.avx512.mask.cmp.w.512")
237 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_w_512,
239 if (Name == "x86.avx512.mask.cmp.d.512")
240 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_d_512,
242 if (Name == "x86.avx512.mask.cmp.q.512")
243 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_q_512,
245 if (Name == "x86.avx512.mask.ucmp.b.512")
246 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_b_512,
248 if (Name == "x86.avx512.mask.ucmp.w.512")
249 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_w_512,
251 if (Name == "x86.avx512.mask.ucmp.d.512")
252 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_d_512,
254 if (Name == "x86.avx512.mask.ucmp.q.512")
255 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_q_512,
258 if (Name == "x86.avx512.mask.cmp.b.256")
259 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_b_256,
261 if (Name == "x86.avx512.mask.cmp.w.256")
262 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_w_256,
264 if (Name == "x86.avx512.mask.cmp.d.256")
265 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_d_256,
267 if (Name == "x86.avx512.mask.cmp.q.256")
268 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_q_256,
270 if (Name == "x86.avx512.mask.ucmp.b.256")
271 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_b_256,
273 if (Name == "x86.avx512.mask.ucmp.w.256")
274 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_w_256,
276 if (Name == "x86.avx512.mask.ucmp.d.256")
277 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_d_256,
279 if (Name == "x86.avx512.mask.ucmp.q.256")
280 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_q_256,
283 if (Name == "x86.avx512.mask.cmp.b.128")
284 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_b_128,
286 if (Name == "x86.avx512.mask.cmp.w.128")
287 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_w_128,
289 if (Name == "x86.avx512.mask.cmp.d.128")
290 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_d_128,
292 if (Name == "x86.avx512.mask.cmp.q.128")
293 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_q_128,
295 if (Name == "x86.avx512.mask.ucmp.b.128")
296 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_b_128,
298 if (Name == "x86.avx512.mask.ucmp.w.128")
299 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_w_128,
301 if (Name == "x86.avx512.mask.ucmp.d.128")
302 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_d_128,
304 if (Name == "x86.avx512.mask.ucmp.q.128")
305 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_q_128,
308 // frcz.ss/sd may need to have an argument dropped
309 if (Name.startswith("x86.xop.vfrcz.ss") && F->arg_size() == 2) {
310 F->setName(Name + ".old");
311 NewFn = Intrinsic::getDeclaration(F->getParent(),
312 Intrinsic::x86_xop_vfrcz_ss);
315 if (Name.startswith("x86.xop.vfrcz.sd") && F->arg_size() == 2) {
316 F->setName(Name + ".old");
317 NewFn = Intrinsic::getDeclaration(F->getParent(),
318 Intrinsic::x86_xop_vfrcz_sd);
321 // Fix the FMA4 intrinsics to remove the 4
322 if (Name.startswith("x86.fma4.")) {
323 F->setName("llvm.x86.fma" + Name.substr(8));
331 // This may not belong here. This function is effectively being overloaded
332 // to both detect an intrinsic which needs upgrading, and to provide the
333 // upgraded form of the intrinsic. We should perhaps have two separate
334 // functions for this.
338 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
340 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
342 // Upgrade intrinsic attributes. This does not change the function.
345 if (unsigned id = F->getIntrinsicID())
346 F->setAttributes(Intrinsic::getAttributes(F->getContext(),
351 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
352 // Nothing to do yet.
356 static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
357 if (!DbgNode || Elt >= DbgNode->getNumOperands())
359 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(Elt));
362 static MetadataAsValue *getExpression(Value *VarOperand, Function *F) {
363 // Old-style DIVariables have an optional expression as the 8th element.
364 DIExpression Expr(getNodeField(
365 cast<MDNode>(cast<MetadataAsValue>(VarOperand)->getMetadata()), 8));
367 DIBuilder DIB(*F->getParent(), /*AllowUnresolved*/ false);
368 Expr = DIB.createExpression();
370 return MetadataAsValue::get(F->getContext(), Expr);
373 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
374 // upgraded intrinsic. All argument and return casting must be provided in
375 // order to seamlessly integrate with existing context.
376 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
377 Function *F = CI->getCalledFunction();
378 LLVMContext &C = CI->getContext();
379 IRBuilder<> Builder(C);
380 Builder.SetInsertPoint(CI->getParent(), CI);
382 assert(F && "Intrinsic call is not direct?");
385 // Get the Function's name.
386 StringRef Name = F->getName();
389 // Upgrade packed integer vector compares intrinsics to compare instructions
390 if (Name.startswith("llvm.x86.sse2.pcmpeq.") ||
391 Name.startswith("llvm.x86.avx2.pcmpeq.")) {
392 Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
394 // need to sign extend since icmp returns vector of i1
395 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
396 } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") ||
397 Name.startswith("llvm.x86.avx2.pcmpgt.")) {
398 Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
400 // need to sign extend since icmp returns vector of i1
401 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
402 } else if (Name == "llvm.x86.avx.movnt.dq.256" ||
403 Name == "llvm.x86.avx.movnt.ps.256" ||
404 Name == "llvm.x86.avx.movnt.pd.256") {
405 IRBuilder<> Builder(C);
406 Builder.SetInsertPoint(CI->getParent(), CI);
408 Module *M = F->getParent();
409 SmallVector<Metadata *, 1> Elts;
411 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
412 MDNode *Node = MDNode::get(C, Elts);
414 Value *Arg0 = CI->getArgOperand(0);
415 Value *Arg1 = CI->getArgOperand(1);
417 // Convert the type of the pointer to a pointer to the stored type.
418 Value *BC = Builder.CreateBitCast(Arg0,
419 PointerType::getUnqual(Arg1->getType()),
421 StoreInst *SI = Builder.CreateStore(Arg1, BC);
422 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
423 SI->setAlignment(16);
426 CI->eraseFromParent();
428 } else if (Name.startswith("llvm.x86.xop.vpcom")) {
430 if (Name.endswith("ub"))
431 intID = Intrinsic::x86_xop_vpcomub;
432 else if (Name.endswith("uw"))
433 intID = Intrinsic::x86_xop_vpcomuw;
434 else if (Name.endswith("ud"))
435 intID = Intrinsic::x86_xop_vpcomud;
436 else if (Name.endswith("uq"))
437 intID = Intrinsic::x86_xop_vpcomuq;
438 else if (Name.endswith("b"))
439 intID = Intrinsic::x86_xop_vpcomb;
440 else if (Name.endswith("w"))
441 intID = Intrinsic::x86_xop_vpcomw;
442 else if (Name.endswith("d"))
443 intID = Intrinsic::x86_xop_vpcomd;
444 else if (Name.endswith("q"))
445 intID = Intrinsic::x86_xop_vpcomq;
447 llvm_unreachable("Unknown suffix");
449 Name = Name.substr(18); // strip off "llvm.x86.xop.vpcom"
451 if (Name.startswith("lt"))
453 else if (Name.startswith("le"))
455 else if (Name.startswith("gt"))
457 else if (Name.startswith("ge"))
459 else if (Name.startswith("eq"))
461 else if (Name.startswith("ne"))
463 else if (Name.startswith("false"))
465 else if (Name.startswith("true"))
468 llvm_unreachable("Unknown condition");
470 Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
471 Rep = Builder.CreateCall3(VPCOM, CI->getArgOperand(0),
472 CI->getArgOperand(1), Builder.getInt8(Imm));
473 } else if (Name == "llvm.x86.sse42.crc32.64.8") {
474 Function *CRC32 = Intrinsic::getDeclaration(F->getParent(),
475 Intrinsic::x86_sse42_crc32_32_8);
476 Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C));
477 Rep = Builder.CreateCall2(CRC32, Trunc0, CI->getArgOperand(1));
478 Rep = Builder.CreateZExt(Rep, CI->getType(), "");
479 } else if (Name.startswith("llvm.x86.avx.vbroadcast")) {
480 // Replace broadcasts with a series of insertelements.
481 Type *VecTy = CI->getType();
482 Type *EltTy = VecTy->getVectorElementType();
483 unsigned EltNum = VecTy->getVectorNumElements();
484 Value *Cast = Builder.CreateBitCast(CI->getArgOperand(0),
485 EltTy->getPointerTo());
486 Value *Load = Builder.CreateLoad(Cast);
487 Type *I32Ty = Type::getInt32Ty(C);
488 Rep = UndefValue::get(VecTy);
489 for (unsigned I = 0; I < EltNum; ++I)
490 Rep = Builder.CreateInsertElement(Rep, Load,
491 ConstantInt::get(I32Ty, I));
492 } else if (Name == "llvm.x86.sse2.psll.dq.bs") {
493 Value *Op0 = ConstantVector::getSplat(16, Builder.getInt8(0));
494 Value *Op1 = Builder.CreateBitCast(CI->getArgOperand(0),
495 VectorType::get(Type::getInt8Ty(C),16),
498 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
501 SmallVector<Constant*, 16> Idxs;
502 for (unsigned i = 16; i != 32; ++i)
503 Idxs.push_back(Builder.getInt32(i - Shift));
505 Op0 = Builder.CreateShuffleVector(Op0, Op1, ConstantVector::get(Idxs));
508 Rep = Builder.CreateBitCast(Op0,
509 VectorType::get(Type::getInt64Ty(C), 2),
511 } else if (Name == "llvm.x86.sse2.psrl.dq.bs") {
512 Value *Op0 = Builder.CreateBitCast(CI->getArgOperand(0),
513 VectorType::get(Type::getInt8Ty(C),16),
515 Value *Op1 = ConstantVector::getSplat(16, Builder.getInt8(0));
517 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
520 SmallVector<Constant*, 16> Idxs;
521 for (unsigned i = 0; i != 16; ++i)
522 Idxs.push_back(Builder.getInt32(i + Shift));
524 Op1 = Builder.CreateShuffleVector(Op0, Op1, ConstantVector::get(Idxs));
526 Rep = Builder.CreateBitCast(Op1,
527 VectorType::get(Type::getInt64Ty(C), 2),
530 bool PD128 = false, PD256 = false, PS128 = false, PS256 = false;
531 if (Name == "llvm.x86.avx.vpermil.pd.256")
533 else if (Name == "llvm.x86.avx.vpermil.pd")
535 else if (Name == "llvm.x86.avx.vpermil.ps.256")
537 else if (Name == "llvm.x86.avx.vpermil.ps")
540 if (PD256 || PD128 || PS256 || PS128) {
541 Value *Op0 = CI->getArgOperand(0);
542 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
543 SmallVector<Constant*, 8> Idxs;
546 for (unsigned i = 0; i != 2; ++i)
547 Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1));
549 for (unsigned l = 0; l != 4; l+=2)
550 for (unsigned i = 0; i != 2; ++i)
551 Idxs.push_back(Builder.getInt32(((Imm >> (l+i)) & 0x1) + l));
553 for (unsigned i = 0; i != 4; ++i)
554 Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3));
556 for (unsigned l = 0; l != 8; l+=4)
557 for (unsigned i = 0; i != 4; ++i)
558 Idxs.push_back(Builder.getInt32(((Imm >> (2 * i)) & 0x3) + l));
560 llvm_unreachable("Unexpected function");
562 Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs));
564 llvm_unreachable("Unknown function for CallInst upgrade.");
568 CI->replaceAllUsesWith(Rep);
569 CI->eraseFromParent();
573 std::string Name = CI->getName().str();
575 CI->setName(Name + ".old");
577 switch (NewFn->getIntrinsicID()) {
579 llvm_unreachable("Unknown function for CallInst upgrade.");
581 // Upgrade debug intrinsics to use an additional DIExpression argument.
582 case Intrinsic::dbg_declare: {
584 Builder.CreateCall3(NewFn, CI->getArgOperand(0), CI->getArgOperand(1),
585 getExpression(CI->getArgOperand(1), F), Name);
586 NewCI->setDebugLoc(CI->getDebugLoc());
587 CI->replaceAllUsesWith(NewCI);
588 CI->eraseFromParent();
591 case Intrinsic::dbg_value: {
592 auto NewCI = Builder.CreateCall4(
593 NewFn, CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2),
594 getExpression(CI->getArgOperand(2), F), Name);
595 NewCI->setDebugLoc(CI->getDebugLoc());
596 CI->replaceAllUsesWith(NewCI);
597 CI->eraseFromParent();
600 case Intrinsic::ctlz:
601 case Intrinsic::cttz:
602 assert(CI->getNumArgOperands() == 1 &&
603 "Mismatch between function args and call args");
604 CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
605 Builder.getFalse(), Name));
606 CI->eraseFromParent();
609 case Intrinsic::objectsize:
610 CI->replaceAllUsesWith(Builder.CreateCall2(NewFn,
611 CI->getArgOperand(0),
612 CI->getArgOperand(1),
614 CI->eraseFromParent();
617 case Intrinsic::ctpop: {
618 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(0)));
619 CI->eraseFromParent();
623 case Intrinsic::x86_xop_vfrcz_ss:
624 case Intrinsic::x86_xop_vfrcz_sd:
625 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(1),
627 CI->eraseFromParent();
630 case Intrinsic::x86_sse41_ptestc:
631 case Intrinsic::x86_sse41_ptestz:
632 case Intrinsic::x86_sse41_ptestnzc: {
633 // The arguments for these intrinsics used to be v4f32, and changed
634 // to v2i64. This is purely a nop, since those are bitwise intrinsics.
635 // So, the only thing required is a bitcast for both arguments.
636 // First, check the arguments have the old type.
637 Value *Arg0 = CI->getArgOperand(0);
638 if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4))
641 // Old intrinsic, add bitcasts
642 Value *Arg1 = CI->getArgOperand(1);
645 Builder.CreateBitCast(Arg0,
646 VectorType::get(Type::getInt64Ty(C), 2),
649 Builder.CreateBitCast(Arg1,
650 VectorType::get(Type::getInt64Ty(C), 2),
653 CallInst* NewCall = Builder.CreateCall2(NewFn, BC0, BC1, Name);
654 CI->replaceAllUsesWith(NewCall);
655 CI->eraseFromParent();
659 case Intrinsic::x86_sse41_pblendw:
660 case Intrinsic::x86_sse41_blendpd:
661 case Intrinsic::x86_sse41_blendps:
662 case Intrinsic::x86_sse41_insertps:
663 case Intrinsic::x86_sse41_dppd:
664 case Intrinsic::x86_sse41_dpps:
665 case Intrinsic::x86_sse41_mpsadbw:
666 case Intrinsic::x86_avx_blend_pd_256:
667 case Intrinsic::x86_avx_blend_ps_256:
668 case Intrinsic::x86_avx_dp_ps_256:
669 case Intrinsic::x86_avx2_pblendw:
670 case Intrinsic::x86_avx2_pblendd_128:
671 case Intrinsic::x86_avx2_pblendd_256:
672 case Intrinsic::x86_avx2_mpsadbw: {
673 // Need to truncate the last argument from i32 to i8 -- this argument models
674 // an inherently 8-bit immediate operand to these x86 instructions.
675 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
676 CI->arg_operands().end());
678 // Replace the last argument with a trunc.
679 Args.back() = Builder.CreateTrunc(Args.back(), Type::getInt8Ty(C), "trunc");
681 CallInst *NewCall = Builder.CreateCall(NewFn, Args);
682 CI->replaceAllUsesWith(NewCall);
683 CI->eraseFromParent();
686 case Intrinsic::x86_avx512_mask_cmp_ps_512:
687 case Intrinsic::x86_avx512_mask_cmp_pd_512: {
688 // Need to truncate the last argument from i32 to i8 -- this argument models
689 // an inherently 8-bit immediate operand to these x86 instructions.
690 SmallVector<Value *, 5> Args(CI->arg_operands().begin(),
691 CI->arg_operands().end());
693 // Replace the last argument with a trunc.
694 Args[2] = Builder.CreateTrunc(Args[2], Type::getInt8Ty(C), "trunc");
696 CallInst *NewCall = Builder.CreateCall(NewFn, Args);
697 CI->replaceAllUsesWith(NewCall);
698 CI->eraseFromParent();
704 // This tests each Function to determine if it needs upgrading. When we find
705 // one we are interested in, we then upgrade all calls to reflect the new
707 void llvm::UpgradeCallsToIntrinsic(Function* F) {
708 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
710 // Upgrade the function and check if it is a totaly new function.
712 if (UpgradeIntrinsicFunction(F, NewFn)) {
714 // Replace all uses to the old function with the new one if necessary.
715 for (Value::user_iterator UI = F->user_begin(), UE = F->user_end();
717 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
718 UpgradeIntrinsicCall(CI, NewFn);
720 // Remove old function, no longer used, from the module.
721 F->eraseFromParent();
726 void llvm::UpgradeInstWithTBAATag(Instruction *I) {
727 MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa);
728 assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
729 // Check if the tag uses struct-path aware TBAA format.
730 if (isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3)
733 if (MD->getNumOperands() == 3) {
734 Metadata *Elts[] = {MD->getOperand(0), MD->getOperand(1)};
735 MDNode *ScalarType = MDNode::get(I->getContext(), Elts);
736 // Create a MDNode <ScalarType, ScalarType, offset 0, const>
737 Metadata *Elts2[] = {ScalarType, ScalarType,
738 ConstantAsMetadata::get(Constant::getNullValue(
739 Type::getInt64Ty(I->getContext()))),
741 I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2));
743 // Create a MDNode <MD, MD, offset 0>
744 Metadata *Elts[] = {MD, MD, ConstantAsMetadata::get(Constant::getNullValue(
745 Type::getInt64Ty(I->getContext())))};
746 I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts));
750 Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,
751 Instruction *&Temp) {
752 if (Opc != Instruction::BitCast)
756 Type *SrcTy = V->getType();
757 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
758 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
759 LLVMContext &Context = V->getContext();
761 // We have no information about target data layout, so we assume that
762 // the maximum pointer size is 64bit.
763 Type *MidTy = Type::getInt64Ty(Context);
764 Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy);
766 return CastInst::Create(Instruction::IntToPtr, Temp, DestTy);
772 Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) {
773 if (Opc != Instruction::BitCast)
776 Type *SrcTy = C->getType();
777 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
778 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
779 LLVMContext &Context = C->getContext();
781 // We have no information about target data layout, so we assume that
782 // the maximum pointer size is 64bit.
783 Type *MidTy = Type::getInt64Ty(Context);
785 return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy),
792 /// Check the debug info version number, if it is out-dated, drop the debug
793 /// info. Return true if module is modified.
794 bool llvm::UpgradeDebugInfo(Module &M) {
795 unsigned Version = getDebugMetadataVersionFromModule(M);
796 if (Version == DEBUG_METADATA_VERSION)
799 bool RetCode = StripDebugInfo(M);
801 DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version);
802 M.getContext().diagnose(DiagVersion);
807 void llvm::UpgradeMDStringConstant(std::string &String) {
808 const std::string OldPrefix = "llvm.vectorizer.";
809 if (String == "llvm.vectorizer.unroll") {
810 String = "llvm.loop.interleave.count";
811 } else if (String.find(OldPrefix) == 0) {
812 String.replace(0, OldPrefix.size(), "llvm.loop.vectorize.");