1 //===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the auto-upgrade helper functions 11 // 12 //===----------------------------------------------------------------------===// 13 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" 28 #include <cstring> 29 using namespace llvm; 30 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, 34 Function *&NewFn) { 35 // Check whether this is an old version of the function, which received 36 // v4f32 arguments. 37 Type *Arg0Type = F->getFunctionType()->getParamType(0); 38 if (Arg0Type != VectorType::get(Type::getFloatTy(F->getContext()), 4)) 39 return false; 40 41 // Yes, it's old, replace it with new version. 42 F->setName(F->getName() + ".old"); 43 NewFn = Intrinsic::getDeclaration(F->getParent(), IID); 44 return true; 45 } 46 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, 50 Function *&NewFn) { 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)) 55 return false; 56 57 // Move this function aside and map down. 58 F->setName(F->getName() + ".old"); 59 NewFn = Intrinsic::getDeclaration(F->getParent(), IID); 60 return true; 61 } 62 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, 66 Function *&NewFn) { 67 // Check that the last argument is an i32. 68 Type *LastArgType = F->getFunctionType()->getParamType(2); 69 if (!LastArgType->isIntegerTy(32)) 70 return false; 71 72 // Move this function aside and map down. 73 F->setName(F->getName() + ".old"); 74 NewFn = Intrinsic::getDeclaration(F->getParent(), IID); 75 return true; 76 } 77 78 static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) { 79 assert(F && "Illegal to upgrade a non-existent Function."); 80 81 // Quickly eliminate it, if it's not a candidate. 82 StringRef Name = F->getName(); 83 if (Name.size() <= 8 || !Name.startswith("llvm.")) 84 return false; 85 Name = Name.substr(5); // Strip off "llvm." 86 87 switch (Name[0]) { 88 default: break; 89 case 'a': { 90 if (Name.startswith("arm.neon.vclz")) { 91 Type* args[2] = { 92 F->arg_begin()->getType(), 93 Type::getInt1Ty(F->getContext()) 94 }; 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 97 // llvm.ctlz.* 98 FunctionType* fType = FunctionType::get(F->getReturnType(), args, false); 99 NewFn = Function::Create(fType, F->getLinkage(), 100 "llvm.ctlz." + Name.substr(14), F->getParent()); 101 return true; 102 } 103 if (Name.startswith("arm.neon.vcnt")) { 104 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctpop, 105 F->arg_begin()->getType()); 106 return true; 107 } 108 break; 109 } 110 case 'c': { 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()); 115 return true; 116 } 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()); 121 return true; 122 } 123 break; 124 } 125 case 'd': { 126 if (Name.startswith("dbg.declare") && F->arg_size() == 2) { 127 F->setName(Name + ".old"); 128 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::dbg_declare); 129 return true; 130 } 131 if (Name.startswith("dbg.value") && F->arg_size() == 3) { 132 F->setName(Name + ".old"); 133 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::dbg_value); 134 return true; 135 } 136 break; 137 } 138 139 case 'o': 140 // We only need to change the name to match the mangling including the 141 // address space. 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); 148 return true; 149 } 150 } 151 break; 152 153 case 'x': { 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)) { 167 NewFn = nullptr; 168 return true; 169 } 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); 178 } 179 // Several blend and other instructions with maskes used the wrong number of 180 // bits. 181 if (Name == "x86.sse41.pblendw") 182 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_pblendw, 183 NewFn); 184 if (Name == "x86.sse41.blendpd") 185 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_blendpd, 186 NewFn); 187 if (Name == "x86.sse41.blendps") 188 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_blendps, 189 NewFn); 190 if (Name == "x86.sse41.insertps") 191 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_insertps, 192 NewFn); 193 if (Name == "x86.sse41.dppd") 194 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dppd, 195 NewFn); 196 if (Name == "x86.sse41.dpps") 197 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dpps, 198 NewFn); 199 if (Name == "x86.sse41.mpsadbw") 200 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_mpsadbw, 201 NewFn); 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, 210 NewFn); 211 if (Name == "x86.avx2.pblendw") 212 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_pblendw, 213 NewFn); 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, 222 NewFn); 223 224 if (Name == "x86.avx512.mask.cmp.ps.512") 225 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_ps_512, 226 NewFn); 227 if (Name == "x86.avx512.mask.cmp.pd.512") 228 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_pd_512, 229 NewFn); 230 231 if (Name == "x86.avx512.mask.cmp.b.512") 232 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_b_512, 233 NewFn); 234 if (Name == "x86.avx512.mask.cmp.w.512") 235 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_w_512, 236 NewFn); 237 if (Name == "x86.avx512.mask.cmp.d.512") 238 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_d_512, 239 NewFn); 240 if (Name == "x86.avx512.mask.cmp.q.512") 241 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_q_512, 242 NewFn); 243 if (Name == "x86.avx512.mask.ucmp.b.512") 244 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_b_512, 245 NewFn); 246 if (Name == "x86.avx512.mask.ucmp.w.512") 247 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_w_512, 248 NewFn); 249 if (Name == "x86.avx512.mask.ucmp.d.512") 250 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_d_512, 251 NewFn); 252 if (Name == "x86.avx512.mask.ucmp.q.512") 253 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_q_512, 254 NewFn); 255 256 if (Name == "x86.avx512.mask.cmp.b.256") 257 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_b_256, 258 NewFn); 259 if (Name == "x86.avx512.mask.cmp.w.256") 260 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_w_256, 261 NewFn); 262 if (Name == "x86.avx512.mask.cmp.d.256") 263 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_d_256, 264 NewFn); 265 if (Name == "x86.avx512.mask.cmp.q.256") 266 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_q_256, 267 NewFn); 268 if (Name == "x86.avx512.mask.ucmp.b.256") 269 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_b_256, 270 NewFn); 271 if (Name == "x86.avx512.mask.ucmp.w.256") 272 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_w_256, 273 NewFn); 274 if (Name == "x86.avx512.mask.ucmp.d.256") 275 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_d_256, 276 NewFn); 277 if (Name == "x86.avx512.mask.ucmp.q.256") 278 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_q_256, 279 NewFn); 280 281 if (Name == "x86.avx512.mask.cmp.b.128") 282 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_b_128, 283 NewFn); 284 if (Name == "x86.avx512.mask.cmp.w.128") 285 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_w_128, 286 NewFn); 287 if (Name == "x86.avx512.mask.cmp.d.128") 288 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_d_128, 289 NewFn); 290 if (Name == "x86.avx512.mask.cmp.q.128") 291 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_q_128, 292 NewFn); 293 if (Name == "x86.avx512.mask.ucmp.b.128") 294 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_b_128, 295 NewFn); 296 if (Name == "x86.avx512.mask.ucmp.w.128") 297 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_w_128, 298 NewFn); 299 if (Name == "x86.avx512.mask.ucmp.d.128") 300 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_d_128, 301 NewFn); 302 if (Name == "x86.avx512.mask.ucmp.q.128") 303 return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_q_128, 304 NewFn); 305 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); 311 return true; 312 } 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); 317 return true; 318 } 319 // Fix the FMA4 intrinsics to remove the 4 320 if (Name.startswith("x86.fma4.")) { 321 F->setName("llvm.x86.fma" + Name.substr(8)); 322 NewFn = F; 323 return true; 324 } 325 break; 326 } 327 } 328 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. 333 return false; 334 } 335 336 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) { 337 NewFn = nullptr; 338 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn); 339 340 // Upgrade intrinsic attributes. This does not change the function. 341 if (NewFn) 342 F = NewFn; 343 if (unsigned id = F->getIntrinsicID()) 344 F->setAttributes(Intrinsic::getAttributes(F->getContext(), 345 (Intrinsic::ID)id)); 346 return Upgraded; 347 } 348 349 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) { 350 // Nothing to do yet. 351 return false; 352 } 353 354 static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) { 355 if (!DbgNode || Elt >= DbgNode->getNumOperands()) 356 return nullptr; 357 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(Elt)); 358 } 359 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)); 364 if (!Expr) { 365 DIBuilder DIB(*F->getParent(), /*AllowUnresolved*/ false); 366 Expr = DIB.createExpression(); 367 } 368 return MetadataAsValue::get(F->getContext(), Expr); 369 } 370 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); 379 380 assert(F && "Intrinsic call is not direct?"); 381 382 if (!NewFn) { 383 // Get the Function's name. 384 StringRef Name = F->getName(); 385 386 Value *Rep; 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), 391 "pcmpeq"); 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), 397 "pcmpgt"); 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); 405 406 Module *M = F->getParent(); 407 SmallVector<Metadata *, 1> Elts; 408 Elts.push_back( 409 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1))); 410 MDNode *Node = MDNode::get(C, Elts); 411 412 Value *Arg0 = CI->getArgOperand(0); 413 Value *Arg1 = CI->getArgOperand(1); 414 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()), 418 "cast"); 419 StoreInst *SI = Builder.CreateStore(Arg1, BC); 420 SI->setMetadata(M->getMDKindID("nontemporal"), Node); 421 SI->setAlignment(16); 422 423 // Remove intrinsic. 424 CI->eraseFromParent(); 425 return; 426 } else if (Name.startswith("llvm.x86.xop.vpcom")) { 427 Intrinsic::ID intID; 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; 444 else 445 llvm_unreachable("Unknown suffix"); 446 447 Name = Name.substr(18); // strip off "llvm.x86.xop.vpcom" 448 unsigned Imm; 449 if (Name.startswith("lt")) 450 Imm = 0; 451 else if (Name.startswith("le")) 452 Imm = 1; 453 else if (Name.startswith("gt")) 454 Imm = 2; 455 else if (Name.startswith("ge")) 456 Imm = 3; 457 else if (Name.startswith("eq")) 458 Imm = 4; 459 else if (Name.startswith("ne")) 460 Imm = 5; 461 else if (Name.startswith("true")) 462 Imm = 6; 463 else if (Name.startswith("false")) 464 Imm = 7; 465 else 466 llvm_unreachable("Unknown condition"); 467 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)); 490 } else { 491 bool PD128 = false, PD256 = false, PS128 = false, PS256 = false; 492 if (Name == "llvm.x86.avx.vpermil.pd.256") 493 PD256 = true; 494 else if (Name == "llvm.x86.avx.vpermil.pd") 495 PD128 = true; 496 else if (Name == "llvm.x86.avx.vpermil.ps.256") 497 PS256 = true; 498 else if (Name == "llvm.x86.avx.vpermil.ps") 499 PS128 = true; 500 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; 505 506 if (PD128) 507 for (unsigned i = 0; i != 2; ++i) 508 Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1)); 509 else if (PD256) 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)); 513 else if (PS128) 514 for (unsigned i = 0; i != 4; ++i) 515 Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3)); 516 else if (PS256) 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)); 520 else 521 llvm_unreachable("Unexpected function"); 522 523 Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs)); 524 } else { 525 llvm_unreachable("Unknown function for CallInst upgrade."); 526 } 527 } 528 529 CI->replaceAllUsesWith(Rep); 530 CI->eraseFromParent(); 531 return; 532 } 533 534 std::string Name = CI->getName().str(); 535 if (!Name.empty()) 536 CI->setName(Name + ".old"); 537 538 switch (NewFn->getIntrinsicID()) { 539 default: 540 llvm_unreachable("Unknown function for CallInst upgrade."); 541 542 // Upgrade debug intrinsics to use an additional DIExpression argument. 543 case Intrinsic::dbg_declare: { 544 auto NewCI = 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(); 550 return; 551 } 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(); 559 return; 560 } 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(); 568 return; 569 570 case Intrinsic::objectsize: 571 CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, 572 CI->getArgOperand(0), 573 CI->getArgOperand(1), 574 Name)); 575 CI->eraseFromParent(); 576 return; 577 578 case Intrinsic::ctpop: { 579 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(0))); 580 CI->eraseFromParent(); 581 return; 582 } 583 584 case Intrinsic::x86_xop_vfrcz_ss: 585 case Intrinsic::x86_xop_vfrcz_sd: 586 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(1), 587 Name)); 588 CI->eraseFromParent(); 589 return; 590 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)) 600 return; 601 602 // Old intrinsic, add bitcasts 603 Value *Arg1 = CI->getArgOperand(1); 604 605 Value *BC0 = 606 Builder.CreateBitCast(Arg0, 607 VectorType::get(Type::getInt64Ty(C), 2), 608 "cast"); 609 Value *BC1 = 610 Builder.CreateBitCast(Arg1, 611 VectorType::get(Type::getInt64Ty(C), 2), 612 "cast"); 613 614 CallInst* NewCall = Builder.CreateCall2(NewFn, BC0, BC1, Name); 615 CI->replaceAllUsesWith(NewCall); 616 CI->eraseFromParent(); 617 return; 618 } 619 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()); 638 639 // Replace the last argument with a trunc. 640 Args.back() = Builder.CreateTrunc(Args.back(), Type::getInt8Ty(C), "trunc"); 641 642 CallInst *NewCall = Builder.CreateCall(NewFn, Args); 643 CI->replaceAllUsesWith(NewCall); 644 CI->eraseFromParent(); 645 return; 646 } 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()); 653 654 // Replace the last argument with a trunc. 655 Args[2] = Builder.CreateTrunc(Args[2], Type::getInt8Ty(C), "trunc"); 656 657 CallInst *NewCall = Builder.CreateCall(NewFn, Args); 658 CI->replaceAllUsesWith(NewCall); 659 CI->eraseFromParent(); 660 return; 661 } 662 } 663 } 664 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 667 // function. 668 void llvm::UpgradeCallsToIntrinsic(Function* F) { 669 assert(F && "Illegal attempt to upgrade a non-existent intrinsic."); 670 671 // Upgrade the function and check if it is a totaly new function. 672 Function *NewFn; 673 if (UpgradeIntrinsicFunction(F, NewFn)) { 674 if (NewFn != F) { 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(); 677 UI != UE; ) { 678 if (CallInst *CI = dyn_cast<CallInst>(*UI++)) 679 UpgradeIntrinsicCall(CI, NewFn); 680 } 681 // Remove old function, no longer used, from the module. 682 F->eraseFromParent(); 683 } 684 } 685 } 686 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) 692 return; 693 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()))), 701 MD->getOperand(2)}; 702 I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2)); 703 } else { 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)); 708 } 709 } 710 711 Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy, 712 Instruction *&Temp) { 713 if (Opc != Instruction::BitCast) 714 return nullptr; 715 716 Temp = nullptr; 717 Type *SrcTy = V->getType(); 718 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() && 719 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) { 720 LLVMContext &Context = V->getContext(); 721 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); 726 727 return CastInst::Create(Instruction::IntToPtr, Temp, DestTy); 728 } 729 730 return nullptr; 731 } 732 733 Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) { 734 if (Opc != Instruction::BitCast) 735 return nullptr; 736 737 Type *SrcTy = C->getType(); 738 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() && 739 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) { 740 LLVMContext &Context = C->getContext(); 741 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); 745 746 return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy), 747 DestTy); 748 } 749 750 return nullptr; 751 } 752 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) 758 return false; 759 760 bool RetCode = StripDebugInfo(M); 761 if (RetCode) { 762 DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version); 763 M.getContext().diagnose(DiagVersion); 764 } 765 return RetCode; 766 } 767 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."); 774 } 775 } 776