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.startswith("x86.xop.vpcom") && F->arg_size() == 2)) {
170 // SSE4.1 ptest functions may have an old signature.
171 if (Name.startswith("x86.sse41.ptest")) {
172 if (Name == "x86.sse41.ptestc")
173 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn);
174 if (Name == "x86.sse41.ptestz")
175 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn);
176 if (Name == "x86.sse41.ptestnzc")
177 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn);
179 // Several blend and other instructions with maskes used the wrong number of
181 if (Name == "x86.sse41.pblendw")
182 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_pblendw,
184 if (Name == "x86.sse41.blendpd")
185 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_blendpd,
187 if (Name == "x86.sse41.blendps")
188 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_blendps,
190 if (Name == "x86.sse41.insertps")
191 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_insertps,
193 if (Name == "x86.sse41.dppd")
194 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dppd,
196 if (Name == "x86.sse41.dpps")
197 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dpps,
199 if (Name == "x86.sse41.mpsadbw")
200 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_mpsadbw,
202 if (Name == "x86.avx.blend.pd.256")
203 return UpgradeX86IntrinsicsWith8BitMask(
204 F, Intrinsic::x86_avx_blend_pd_256, NewFn);
205 if (Name == "x86.avx.blend.ps.256")
206 return UpgradeX86IntrinsicsWith8BitMask(
207 F, Intrinsic::x86_avx_blend_ps_256, NewFn);
208 if (Name == "x86.avx.dp.ps.256")
209 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx_dp_ps_256,
211 if (Name == "x86.avx2.pblendw")
212 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_pblendw,
214 if (Name == "x86.avx2.pblendd.128")
215 return UpgradeX86IntrinsicsWith8BitMask(
216 F, Intrinsic::x86_avx2_pblendd_128, NewFn);
217 if (Name == "x86.avx2.pblendd.256")
218 return UpgradeX86IntrinsicsWith8BitMask(
219 F, Intrinsic::x86_avx2_pblendd_256, NewFn);
220 if (Name == "x86.avx2.mpsadbw")
221 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_mpsadbw,
224 if (Name == "x86.avx512.mask.cmp.ps.512")
225 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_ps_512,
227 if (Name == "x86.avx512.mask.cmp.pd.512")
228 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_pd_512,
231 if (Name == "x86.avx512.mask.cmp.b.512")
232 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_b_512,
234 if (Name == "x86.avx512.mask.cmp.w.512")
235 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_w_512,
237 if (Name == "x86.avx512.mask.cmp.d.512")
238 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_d_512,
240 if (Name == "x86.avx512.mask.cmp.q.512")
241 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_q_512,
243 if (Name == "x86.avx512.mask.ucmp.b.512")
244 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_b_512,
246 if (Name == "x86.avx512.mask.ucmp.w.512")
247 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_w_512,
249 if (Name == "x86.avx512.mask.ucmp.d.512")
250 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_d_512,
252 if (Name == "x86.avx512.mask.ucmp.q.512")
253 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_q_512,
256 if (Name == "x86.avx512.mask.cmp.b.256")
257 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_b_256,
259 if (Name == "x86.avx512.mask.cmp.w.256")
260 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_w_256,
262 if (Name == "x86.avx512.mask.cmp.d.256")
263 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_d_256,
265 if (Name == "x86.avx512.mask.cmp.q.256")
266 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_q_256,
268 if (Name == "x86.avx512.mask.ucmp.b.256")
269 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_b_256,
271 if (Name == "x86.avx512.mask.ucmp.w.256")
272 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_w_256,
274 if (Name == "x86.avx512.mask.ucmp.d.256")
275 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_d_256,
277 if (Name == "x86.avx512.mask.ucmp.q.256")
278 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_q_256,
281 if (Name == "x86.avx512.mask.cmp.b.128")
282 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_b_128,
284 if (Name == "x86.avx512.mask.cmp.w.128")
285 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_w_128,
287 if (Name == "x86.avx512.mask.cmp.d.128")
288 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_d_128,
290 if (Name == "x86.avx512.mask.cmp.q.128")
291 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_q_128,
293 if (Name == "x86.avx512.mask.ucmp.b.128")
294 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_b_128,
296 if (Name == "x86.avx512.mask.ucmp.w.128")
297 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_w_128,
299 if (Name == "x86.avx512.mask.ucmp.d.128")
300 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_d_128,
302 if (Name == "x86.avx512.mask.ucmp.q.128")
303 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_q_128,
306 // frcz.ss/sd may need to have an argument dropped
307 if (Name.startswith("x86.xop.vfrcz.ss") && F->arg_size() == 2) {
308 F->setName(Name + ".old");
309 NewFn = Intrinsic::getDeclaration(F->getParent(),
310 Intrinsic::x86_xop_vfrcz_ss);
313 if (Name.startswith("x86.xop.vfrcz.sd") && F->arg_size() == 2) {
314 F->setName(Name + ".old");
315 NewFn = Intrinsic::getDeclaration(F->getParent(),
316 Intrinsic::x86_xop_vfrcz_sd);
319 // Fix the FMA4 intrinsics to remove the 4
320 if (Name.startswith("x86.fma4.")) {
321 F->setName("llvm.x86.fma" + Name.substr(8));
329 // This may not belong here. This function is effectively being overloaded
330 // to both detect an intrinsic which needs upgrading, and to provide the
331 // upgraded form of the intrinsic. We should perhaps have two separate
332 // functions for this.
336 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
338 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
340 // Upgrade intrinsic attributes. This does not change the function.
343 if (unsigned id = F->getIntrinsicID())
344 F->setAttributes(Intrinsic::getAttributes(F->getContext(),
349 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
350 // Nothing to do yet.
354 static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
355 if (!DbgNode || Elt >= DbgNode->getNumOperands())
357 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(Elt));
360 static MetadataAsValue *getExpression(Value *VarOperand, Function *F) {
361 // Old-style DIVariables have an optional expression as the 8th element.
362 DIExpression Expr(getNodeField(
363 cast<MDNode>(cast<MetadataAsValue>(VarOperand)->getMetadata()), 8));
365 DIBuilder DIB(*F->getParent(), /*AllowUnresolved*/ false);
366 Expr = DIB.createExpression();
368 return MetadataAsValue::get(F->getContext(), Expr);
371 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
372 // upgraded intrinsic. All argument and return casting must be provided in
373 // order to seamlessly integrate with existing context.
374 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
375 Function *F = CI->getCalledFunction();
376 LLVMContext &C = CI->getContext();
377 IRBuilder<> Builder(C);
378 Builder.SetInsertPoint(CI->getParent(), CI);
380 assert(F && "Intrinsic call is not direct?");
383 // Get the Function's name.
384 StringRef Name = F->getName();
387 // Upgrade packed integer vector compares intrinsics to compare instructions
388 if (Name.startswith("llvm.x86.sse2.pcmpeq.") ||
389 Name.startswith("llvm.x86.avx2.pcmpeq.")) {
390 Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
392 // need to sign extend since icmp returns vector of i1
393 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
394 } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") ||
395 Name.startswith("llvm.x86.avx2.pcmpgt.")) {
396 Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
398 // need to sign extend since icmp returns vector of i1
399 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
400 } else if (Name == "llvm.x86.avx.movnt.dq.256" ||
401 Name == "llvm.x86.avx.movnt.ps.256" ||
402 Name == "llvm.x86.avx.movnt.pd.256") {
403 IRBuilder<> Builder(C);
404 Builder.SetInsertPoint(CI->getParent(), CI);
406 Module *M = F->getParent();
407 SmallVector<Metadata *, 1> Elts;
409 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
410 MDNode *Node = MDNode::get(C, Elts);
412 Value *Arg0 = CI->getArgOperand(0);
413 Value *Arg1 = CI->getArgOperand(1);
415 // Convert the type of the pointer to a pointer to the stored type.
416 Value *BC = Builder.CreateBitCast(Arg0,
417 PointerType::getUnqual(Arg1->getType()),
419 StoreInst *SI = Builder.CreateStore(Arg1, BC);
420 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
421 SI->setAlignment(16);
424 CI->eraseFromParent();
426 } else if (Name.startswith("llvm.x86.xop.vpcom")) {
428 if (Name.endswith("ub"))
429 intID = Intrinsic::x86_xop_vpcomub;
430 else if (Name.endswith("uw"))
431 intID = Intrinsic::x86_xop_vpcomuw;
432 else if (Name.endswith("ud"))
433 intID = Intrinsic::x86_xop_vpcomud;
434 else if (Name.endswith("uq"))
435 intID = Intrinsic::x86_xop_vpcomuq;
436 else if (Name.endswith("b"))
437 intID = Intrinsic::x86_xop_vpcomb;
438 else if (Name.endswith("w"))
439 intID = Intrinsic::x86_xop_vpcomw;
440 else if (Name.endswith("d"))
441 intID = Intrinsic::x86_xop_vpcomd;
442 else if (Name.endswith("q"))
443 intID = Intrinsic::x86_xop_vpcomq;
445 llvm_unreachable("Unknown suffix");
447 Name = Name.substr(18); // strip off "llvm.x86.xop.vpcom"
449 if (Name.startswith("lt"))
451 else if (Name.startswith("le"))
453 else if (Name.startswith("gt"))
455 else if (Name.startswith("ge"))
457 else if (Name.startswith("eq"))
459 else if (Name.startswith("ne"))
461 else if (Name.startswith("true"))
463 else if (Name.startswith("false"))
466 llvm_unreachable("Unknown condition");
468 Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
469 Rep = Builder.CreateCall3(VPCOM, CI->getArgOperand(0),
470 CI->getArgOperand(1), Builder.getInt8(Imm));
471 } else if (Name == "llvm.x86.sse42.crc32.64.8") {
472 Function *CRC32 = Intrinsic::getDeclaration(F->getParent(),
473 Intrinsic::x86_sse42_crc32_32_8);
474 Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C));
475 Rep = Builder.CreateCall2(CRC32, Trunc0, CI->getArgOperand(1));
476 Rep = Builder.CreateZExt(Rep, CI->getType(), "");
477 } else if (Name.startswith("llvm.x86.avx.vbroadcast")) {
478 // Replace broadcasts with a series of insertelements.
479 Type *VecTy = CI->getType();
480 Type *EltTy = VecTy->getVectorElementType();
481 unsigned EltNum = VecTy->getVectorNumElements();
482 Value *Cast = Builder.CreateBitCast(CI->getArgOperand(0),
483 EltTy->getPointerTo());
484 Value *Load = Builder.CreateLoad(Cast);
485 Type *I32Ty = Type::getInt32Ty(C);
486 Rep = UndefValue::get(VecTy);
487 for (unsigned I = 0; I < EltNum; ++I)
488 Rep = Builder.CreateInsertElement(Rep, Load,
489 ConstantInt::get(I32Ty, I));
491 bool PD128 = false, PD256 = false, PS128 = false, PS256 = false;
492 if (Name == "llvm.x86.avx.vpermil.pd.256")
494 else if (Name == "llvm.x86.avx.vpermil.pd")
496 else if (Name == "llvm.x86.avx.vpermil.ps.256")
498 else if (Name == "llvm.x86.avx.vpermil.ps")
501 if (PD256 || PD128 || PS256 || PS128) {
502 Value *Op0 = CI->getArgOperand(0);
503 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
504 SmallVector<Constant*, 8> Idxs;
507 for (unsigned i = 0; i != 2; ++i)
508 Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1));
510 for (unsigned l = 0; l != 4; l+=2)
511 for (unsigned i = 0; i != 2; ++i)
512 Idxs.push_back(Builder.getInt32(((Imm >> (l+i)) & 0x1) + l));
514 for (unsigned i = 0; i != 4; ++i)
515 Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3));
517 for (unsigned l = 0; l != 8; l+=4)
518 for (unsigned i = 0; i != 4; ++i)
519 Idxs.push_back(Builder.getInt32(((Imm >> (2 * i)) & 0x3) + l));
521 llvm_unreachable("Unexpected function");
523 Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs));
525 llvm_unreachable("Unknown function for CallInst upgrade.");
529 CI->replaceAllUsesWith(Rep);
530 CI->eraseFromParent();
534 std::string Name = CI->getName().str();
536 CI->setName(Name + ".old");
538 switch (NewFn->getIntrinsicID()) {
540 llvm_unreachable("Unknown function for CallInst upgrade.");
542 // Upgrade debug intrinsics to use an additional DIExpression argument.
543 case Intrinsic::dbg_declare: {
545 Builder.CreateCall3(NewFn, CI->getArgOperand(0), CI->getArgOperand(1),
546 getExpression(CI->getArgOperand(1), F), Name);
547 NewCI->setDebugLoc(CI->getDebugLoc());
548 CI->replaceAllUsesWith(NewCI);
549 CI->eraseFromParent();
552 case Intrinsic::dbg_value: {
553 auto NewCI = Builder.CreateCall4(
554 NewFn, CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2),
555 getExpression(CI->getArgOperand(2), F), Name);
556 NewCI->setDebugLoc(CI->getDebugLoc());
557 CI->replaceAllUsesWith(NewCI);
558 CI->eraseFromParent();
561 case Intrinsic::ctlz:
562 case Intrinsic::cttz:
563 assert(CI->getNumArgOperands() == 1 &&
564 "Mismatch between function args and call args");
565 CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
566 Builder.getFalse(), Name));
567 CI->eraseFromParent();
570 case Intrinsic::objectsize:
571 CI->replaceAllUsesWith(Builder.CreateCall2(NewFn,
572 CI->getArgOperand(0),
573 CI->getArgOperand(1),
575 CI->eraseFromParent();
578 case Intrinsic::ctpop: {
579 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(0)));
580 CI->eraseFromParent();
584 case Intrinsic::x86_xop_vfrcz_ss:
585 case Intrinsic::x86_xop_vfrcz_sd:
586 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(1),
588 CI->eraseFromParent();
591 case Intrinsic::x86_sse41_ptestc:
592 case Intrinsic::x86_sse41_ptestz:
593 case Intrinsic::x86_sse41_ptestnzc: {
594 // The arguments for these intrinsics used to be v4f32, and changed
595 // to v2i64. This is purely a nop, since those are bitwise intrinsics.
596 // So, the only thing required is a bitcast for both arguments.
597 // First, check the arguments have the old type.
598 Value *Arg0 = CI->getArgOperand(0);
599 if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4))
602 // Old intrinsic, add bitcasts
603 Value *Arg1 = CI->getArgOperand(1);
606 Builder.CreateBitCast(Arg0,
607 VectorType::get(Type::getInt64Ty(C), 2),
610 Builder.CreateBitCast(Arg1,
611 VectorType::get(Type::getInt64Ty(C), 2),
614 CallInst* NewCall = Builder.CreateCall2(NewFn, BC0, BC1, Name);
615 CI->replaceAllUsesWith(NewCall);
616 CI->eraseFromParent();
620 case Intrinsic::x86_sse41_pblendw:
621 case Intrinsic::x86_sse41_blendpd:
622 case Intrinsic::x86_sse41_blendps:
623 case Intrinsic::x86_sse41_insertps:
624 case Intrinsic::x86_sse41_dppd:
625 case Intrinsic::x86_sse41_dpps:
626 case Intrinsic::x86_sse41_mpsadbw:
627 case Intrinsic::x86_avx_blend_pd_256:
628 case Intrinsic::x86_avx_blend_ps_256:
629 case Intrinsic::x86_avx_dp_ps_256:
630 case Intrinsic::x86_avx2_pblendw:
631 case Intrinsic::x86_avx2_pblendd_128:
632 case Intrinsic::x86_avx2_pblendd_256:
633 case Intrinsic::x86_avx2_mpsadbw: {
634 // Need to truncate the last argument from i32 to i8 -- this argument models
635 // an inherently 8-bit immediate operand to these x86 instructions.
636 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
637 CI->arg_operands().end());
639 // Replace the last argument with a trunc.
640 Args.back() = Builder.CreateTrunc(Args.back(), Type::getInt8Ty(C), "trunc");
642 CallInst *NewCall = Builder.CreateCall(NewFn, Args);
643 CI->replaceAllUsesWith(NewCall);
644 CI->eraseFromParent();
647 case Intrinsic::x86_avx512_mask_cmp_ps_512:
648 case Intrinsic::x86_avx512_mask_cmp_pd_512: {
649 // Need to truncate the last argument from i32 to i8 -- this argument models
650 // an inherently 8-bit immediate operand to these x86 instructions.
651 SmallVector<Value *, 5> Args(CI->arg_operands().begin(),
652 CI->arg_operands().end());
654 // Replace the last argument with a trunc.
655 Args[2] = Builder.CreateTrunc(Args[2], Type::getInt8Ty(C), "trunc");
657 CallInst *NewCall = Builder.CreateCall(NewFn, Args);
658 CI->replaceAllUsesWith(NewCall);
659 CI->eraseFromParent();
665 // This tests each Function to determine if it needs upgrading. When we find
666 // one we are interested in, we then upgrade all calls to reflect the new
668 void llvm::UpgradeCallsToIntrinsic(Function* F) {
669 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
671 // Upgrade the function and check if it is a totaly new function.
673 if (UpgradeIntrinsicFunction(F, NewFn)) {
675 // Replace all uses to the old function with the new one if necessary.
676 for (Value::user_iterator UI = F->user_begin(), UE = F->user_end();
678 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
679 UpgradeIntrinsicCall(CI, NewFn);
681 // Remove old function, no longer used, from the module.
682 F->eraseFromParent();
687 void llvm::UpgradeInstWithTBAATag(Instruction *I) {
688 MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa);
689 assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
690 // Check if the tag uses struct-path aware TBAA format.
691 if (isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3)
694 if (MD->getNumOperands() == 3) {
695 Metadata *Elts[] = {MD->getOperand(0), MD->getOperand(1)};
696 MDNode *ScalarType = MDNode::get(I->getContext(), Elts);
697 // Create a MDNode <ScalarType, ScalarType, offset 0, const>
698 Metadata *Elts2[] = {ScalarType, ScalarType,
699 ConstantAsMetadata::get(Constant::getNullValue(
700 Type::getInt64Ty(I->getContext()))),
702 I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2));
704 // Create a MDNode <MD, MD, offset 0>
705 Metadata *Elts[] = {MD, MD, ConstantAsMetadata::get(Constant::getNullValue(
706 Type::getInt64Ty(I->getContext())))};
707 I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts));
711 Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,
712 Instruction *&Temp) {
713 if (Opc != Instruction::BitCast)
717 Type *SrcTy = V->getType();
718 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
719 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
720 LLVMContext &Context = V->getContext();
722 // We have no information about target data layout, so we assume that
723 // the maximum pointer size is 64bit.
724 Type *MidTy = Type::getInt64Ty(Context);
725 Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy);
727 return CastInst::Create(Instruction::IntToPtr, Temp, DestTy);
733 Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) {
734 if (Opc != Instruction::BitCast)
737 Type *SrcTy = C->getType();
738 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
739 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
740 LLVMContext &Context = C->getContext();
742 // We have no information about target data layout, so we assume that
743 // the maximum pointer size is 64bit.
744 Type *MidTy = Type::getInt64Ty(Context);
746 return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy),
753 /// Check the debug info version number, if it is out-dated, drop the debug
754 /// info. Return true if module is modified.
755 bool llvm::UpgradeDebugInfo(Module &M) {
756 unsigned Version = getDebugMetadataVersionFromModule(M);
757 if (Version == DEBUG_METADATA_VERSION)
760 bool RetCode = StripDebugInfo(M);
762 DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version);
763 M.getContext().diagnose(DiagVersion);
768 void llvm::UpgradeMDStringConstant(std::string &String) {
769 const std::string OldPrefix = "llvm.vectorizer.";
770 if (String == "llvm.vectorizer.unroll") {
771 String = "llvm.loop.interleave.count";
772 } else if (String.find(OldPrefix) == 0) {
773 String.replace(0, OldPrefix.size(), "llvm.loop.vectorize.");