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 // This is where deprecated IR intrinsics and other IR features are updated to 12 // current specifications. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "llvm/IR/AutoUpgrade.h" 17 #include "llvm/IR/CFG.h" 18 #include "llvm/IR/CallSite.h" 19 #include "llvm/IR/Constants.h" 20 #include "llvm/IR/DIBuilder.h" 21 #include "llvm/IR/DebugInfo.h" 22 #include "llvm/IR/DiagnosticInfo.h" 23 #include "llvm/IR/Function.h" 24 #include "llvm/IR/IRBuilder.h" 25 #include "llvm/IR/Instruction.h" 26 #include "llvm/IR/IntrinsicInst.h" 27 #include "llvm/IR/LLVMContext.h" 28 #include "llvm/IR/Module.h" 29 #include "llvm/Support/ErrorHandling.h" 30 #include "llvm/Support/Regex.h" 31 #include <cstring> 32 using namespace llvm; 33 34 // Upgrade the declarations of the SSE4.1 functions whose arguments have 35 // changed their type from v4f32 to v2i64. 36 static bool UpgradeSSE41Function(Function* F, Intrinsic::ID IID, 37 Function *&NewFn) { 38 // Check whether this is an old version of the function, which received 39 // v4f32 arguments. 40 Type *Arg0Type = F->getFunctionType()->getParamType(0); 41 if (Arg0Type != VectorType::get(Type::getFloatTy(F->getContext()), 4)) 42 return false; 43 44 // Yes, it's old, replace it with new version. 45 F->setName(F->getName() + ".old"); 46 NewFn = Intrinsic::getDeclaration(F->getParent(), IID); 47 return true; 48 } 49 50 // Upgrade the declarations of intrinsic functions whose 8-bit immediate mask 51 // arguments have changed their type from i32 to i8. 52 static bool UpgradeX86IntrinsicsWith8BitMask(Function *F, Intrinsic::ID IID, 53 Function *&NewFn) { 54 // Check that the last argument is an i32. 55 Type *LastArgType = F->getFunctionType()->getParamType( 56 F->getFunctionType()->getNumParams() - 1); 57 if (!LastArgType->isIntegerTy(32)) 58 return false; 59 60 // Move this function aside and map down. 61 F->setName(F->getName() + ".old"); 62 NewFn = Intrinsic::getDeclaration(F->getParent(), IID); 63 return true; 64 } 65 66 static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) { 67 assert(F && "Illegal to upgrade a non-existent Function."); 68 69 // Quickly eliminate it, if it's not a candidate. 70 StringRef Name = F->getName(); 71 if (Name.size() <= 8 || !Name.startswith("llvm.")) 72 return false; 73 Name = Name.substr(5); // Strip off "llvm." 74 75 switch (Name[0]) { 76 default: break; 77 case 'a': { 78 if (Name.startswith("arm.neon.vclz")) { 79 Type* args[2] = { 80 F->arg_begin()->getType(), 81 Type::getInt1Ty(F->getContext()) 82 }; 83 // Can't use Intrinsic::getDeclaration here as it adds a ".i1" to 84 // the end of the name. Change name from llvm.arm.neon.vclz.* to 85 // llvm.ctlz.* 86 FunctionType* fType = FunctionType::get(F->getReturnType(), args, false); 87 NewFn = Function::Create(fType, F->getLinkage(), 88 "llvm.ctlz." + Name.substr(14), F->getParent()); 89 return true; 90 } 91 if (Name.startswith("arm.neon.vcnt")) { 92 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctpop, 93 F->arg_begin()->getType()); 94 return true; 95 } 96 Regex vldRegex("^arm\\.neon\\.vld([1234]|[234]lane)\\.v[a-z0-9]*$"); 97 if (vldRegex.match(Name)) { 98 auto fArgs = F->getFunctionType()->params(); 99 SmallVector<Type *, 4> Tys(fArgs.begin(), fArgs.end()); 100 // Can't use Intrinsic::getDeclaration here as the return types might 101 // then only be structurally equal. 102 FunctionType* fType = FunctionType::get(F->getReturnType(), Tys, false); 103 NewFn = Function::Create(fType, F->getLinkage(), 104 "llvm." + Name + ".p0i8", F->getParent()); 105 return true; 106 } 107 Regex vstRegex("^arm\\.neon\\.vst([1234]|[234]lane)\\.v[a-z0-9]*$"); 108 if (vstRegex.match(Name)) { 109 static const Intrinsic::ID StoreInts[] = {Intrinsic::arm_neon_vst1, 110 Intrinsic::arm_neon_vst2, 111 Intrinsic::arm_neon_vst3, 112 Intrinsic::arm_neon_vst4}; 113 114 static const Intrinsic::ID StoreLaneInts[] = { 115 Intrinsic::arm_neon_vst2lane, Intrinsic::arm_neon_vst3lane, 116 Intrinsic::arm_neon_vst4lane 117 }; 118 119 auto fArgs = F->getFunctionType()->params(); 120 Type *Tys[] = {fArgs[0], fArgs[1]}; 121 if (Name.find("lane") == StringRef::npos) 122 NewFn = Intrinsic::getDeclaration(F->getParent(), 123 StoreInts[fArgs.size() - 3], Tys); 124 else 125 NewFn = Intrinsic::getDeclaration(F->getParent(), 126 StoreLaneInts[fArgs.size() - 5], Tys); 127 return true; 128 } 129 if (Name == "aarch64.thread.pointer" || Name == "arm.thread.pointer") { 130 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::thread_pointer); 131 return true; 132 } 133 break; 134 } 135 136 case 'c': { 137 if (Name.startswith("ctlz.") && F->arg_size() == 1) { 138 F->setName(Name + ".old"); 139 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz, 140 F->arg_begin()->getType()); 141 return true; 142 } 143 if (Name.startswith("cttz.") && F->arg_size() == 1) { 144 F->setName(Name + ".old"); 145 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz, 146 F->arg_begin()->getType()); 147 return true; 148 } 149 break; 150 } 151 152 case 'o': 153 // We only need to change the name to match the mangling including the 154 // address space. 155 if (F->arg_size() == 2 && Name.startswith("objectsize.")) { 156 Type *Tys[2] = { F->getReturnType(), F->arg_begin()->getType() }; 157 if (F->getName() != Intrinsic::getName(Intrinsic::objectsize, Tys)) { 158 F->setName(Name + ".old"); 159 NewFn = Intrinsic::getDeclaration(F->getParent(), 160 Intrinsic::objectsize, Tys); 161 return true; 162 } 163 } 164 break; 165 166 case 's': 167 if (Name == "stackprotectorcheck") { 168 NewFn = nullptr; 169 return true; 170 } 171 172 case 'x': { 173 if (Name.startswith("x86.sse2.pcmpeq.") || 174 Name.startswith("x86.sse2.pcmpgt.") || 175 Name.startswith("x86.avx2.pcmpeq.") || 176 Name.startswith("x86.avx2.pcmpgt.") || 177 Name.startswith("x86.avx2.vbroadcast") || 178 Name.startswith("x86.avx2.pbroadcast") || 179 Name.startswith("x86.avx.vpermil.") || 180 Name.startswith("x86.sse41.pmovsx") || 181 Name.startswith("x86.sse41.pmovzx") || 182 Name.startswith("x86.avx2.pmovsx") || 183 Name.startswith("x86.avx2.pmovzx") || 184 Name == "x86.sse2.cvtdq2pd" || 185 Name == "x86.sse2.cvtps2pd" || 186 Name == "x86.avx.cvtdq2.pd.256" || 187 Name == "x86.avx.cvt.ps2.pd.256" || 188 Name.startswith("x86.avx.vinsertf128.") || 189 Name == "x86.avx2.vinserti128" || 190 Name.startswith("x86.avx.vextractf128.") || 191 Name == "x86.avx2.vextracti128" || 192 Name.startswith("x86.avx.movnt.") || 193 Name == "x86.sse2.storel.dq" || 194 Name.startswith("x86.sse.storeu.") || 195 Name.startswith("x86.sse2.storeu.") || 196 Name.startswith("x86.avx.storeu.") || 197 Name.startswith("x86.avx512.mask.storeu.p") || 198 Name.startswith("x86.avx512.mask.storeu.b.") || 199 Name.startswith("x86.avx512.mask.storeu.w.") || 200 Name.startswith("x86.avx512.mask.storeu.d.") || 201 Name.startswith("x86.avx512.mask.storeu.q.") || 202 Name.startswith("x86.avx512.mask.store.p") || 203 Name.startswith("x86.avx512.mask.store.b.") || 204 Name.startswith("x86.avx512.mask.store.w.") || 205 Name.startswith("x86.avx512.mask.store.d.") || 206 Name.startswith("x86.avx512.mask.store.q.") || 207 Name == "x86.sse42.crc32.64.8" || 208 Name.startswith("x86.avx.vbroadcast.s") || 209 Name.startswith("x86.sse2.psll.dq") || 210 Name.startswith("x86.sse2.psrl.dq") || 211 Name.startswith("x86.avx2.psll.dq") || 212 Name.startswith("x86.avx2.psrl.dq") || 213 Name == "x86.sse41.pblendw" || 214 Name.startswith("x86.sse41.blendp") || 215 Name.startswith("x86.avx.blend.p") || 216 Name == "x86.avx2.pblendw" || 217 Name.startswith("x86.avx2.pblendd.") || 218 Name == "x86.avx2.vbroadcasti128" || 219 Name == "x86.xop.vpcmov" || 220 (Name.startswith("x86.xop.vpcom") && F->arg_size() == 2)) { 221 NewFn = nullptr; 222 return true; 223 } 224 // SSE4.1 ptest functions may have an old signature. 225 if (Name.startswith("x86.sse41.ptest")) { 226 if (Name == "x86.sse41.ptestc") 227 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn); 228 if (Name == "x86.sse41.ptestz") 229 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn); 230 if (Name == "x86.sse41.ptestnzc") 231 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn); 232 } 233 // Several blend and other instructions with masks used the wrong number of 234 // bits. 235 if (Name == "x86.sse41.insertps") 236 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_insertps, 237 NewFn); 238 if (Name == "x86.sse41.dppd") 239 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dppd, 240 NewFn); 241 if (Name == "x86.sse41.dpps") 242 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dpps, 243 NewFn); 244 if (Name == "x86.sse41.mpsadbw") 245 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_mpsadbw, 246 NewFn); 247 if (Name == "x86.avx.dp.ps.256") 248 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx_dp_ps_256, 249 NewFn); 250 if (Name == "x86.avx2.mpsadbw") 251 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_mpsadbw, 252 NewFn); 253 254 // frcz.ss/sd may need to have an argument dropped 255 if (Name.startswith("x86.xop.vfrcz.ss") && F->arg_size() == 2) { 256 F->setName(Name + ".old"); 257 NewFn = Intrinsic::getDeclaration(F->getParent(), 258 Intrinsic::x86_xop_vfrcz_ss); 259 return true; 260 } 261 if (Name.startswith("x86.xop.vfrcz.sd") && F->arg_size() == 2) { 262 F->setName(Name + ".old"); 263 NewFn = Intrinsic::getDeclaration(F->getParent(), 264 Intrinsic::x86_xop_vfrcz_sd); 265 return true; 266 } 267 // Fix the FMA4 intrinsics to remove the 4 268 if (Name.startswith("x86.fma4.")) { 269 F->setName("llvm.x86.fma" + Name.substr(8)); 270 NewFn = F; 271 return true; 272 } 273 break; 274 } 275 } 276 277 // This may not belong here. This function is effectively being overloaded 278 // to both detect an intrinsic which needs upgrading, and to provide the 279 // upgraded form of the intrinsic. We should perhaps have two separate 280 // functions for this. 281 return false; 282 } 283 284 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) { 285 NewFn = nullptr; 286 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn); 287 assert(F != NewFn && "Intrinsic function upgraded to the same function"); 288 289 // Upgrade intrinsic attributes. This does not change the function. 290 if (NewFn) 291 F = NewFn; 292 if (Intrinsic::ID id = F->getIntrinsicID()) 293 F->setAttributes(Intrinsic::getAttributes(F->getContext(), id)); 294 return Upgraded; 295 } 296 297 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) { 298 // Nothing to do yet. 299 return false; 300 } 301 302 // Handles upgrading SSE2 and AVX2 PSLLDQ intrinsics by converting them 303 // to byte shuffles. 304 static Value *UpgradeX86PSLLDQIntrinsics(IRBuilder<> &Builder, LLVMContext &C, 305 Value *Op, unsigned Shift) { 306 Type *ResultTy = Op->getType(); 307 unsigned NumElts = ResultTy->getVectorNumElements() * 8; 308 309 // Bitcast from a 64-bit element type to a byte element type. 310 Type *VecTy = VectorType::get(Type::getInt8Ty(C), NumElts); 311 Op = Builder.CreateBitCast(Op, VecTy, "cast"); 312 313 // We'll be shuffling in zeroes. 314 Value *Res = Constant::getNullValue(VecTy); 315 316 // If shift is less than 16, emit a shuffle to move the bytes. Otherwise, 317 // we'll just return the zero vector. 318 if (Shift < 16) { 319 int Idxs[32]; 320 // 256-bit version is split into two 16-byte lanes. 321 for (unsigned l = 0; l != NumElts; l += 16) 322 for (unsigned i = 0; i != 16; ++i) { 323 unsigned Idx = NumElts + i - Shift; 324 if (Idx < NumElts) 325 Idx -= NumElts - 16; // end of lane, switch operand. 326 Idxs[l + i] = Idx + l; 327 } 328 329 Res = Builder.CreateShuffleVector(Res, Op, makeArrayRef(Idxs, NumElts)); 330 } 331 332 // Bitcast back to a 64-bit element type. 333 return Builder.CreateBitCast(Res, ResultTy, "cast"); 334 } 335 336 // Handles upgrading SSE2 and AVX2 PSRLDQ intrinsics by converting them 337 // to byte shuffles. 338 static Value *UpgradeX86PSRLDQIntrinsics(IRBuilder<> &Builder, LLVMContext &C, 339 Value *Op, 340 unsigned Shift) { 341 Type *ResultTy = Op->getType(); 342 unsigned NumElts = ResultTy->getVectorNumElements() * 8; 343 344 // Bitcast from a 64-bit element type to a byte element type. 345 Type *VecTy = VectorType::get(Type::getInt8Ty(C), NumElts); 346 Op = Builder.CreateBitCast(Op, VecTy, "cast"); 347 348 // We'll be shuffling in zeroes. 349 Value *Res = Constant::getNullValue(VecTy); 350 351 // If shift is less than 16, emit a shuffle to move the bytes. Otherwise, 352 // we'll just return the zero vector. 353 if (Shift < 16) { 354 int Idxs[32]; 355 // 256-bit version is split into two 16-byte lanes. 356 for (unsigned l = 0; l != NumElts; l += 16) 357 for (unsigned i = 0; i != 16; ++i) { 358 unsigned Idx = i + Shift; 359 if (Idx >= 16) 360 Idx += NumElts - 16; // end of lane, switch operand. 361 Idxs[l + i] = Idx + l; 362 } 363 364 Res = Builder.CreateShuffleVector(Op, Res, makeArrayRef(Idxs, NumElts)); 365 } 366 367 // Bitcast back to a 64-bit element type. 368 return Builder.CreateBitCast(Res, ResultTy, "cast"); 369 } 370 371 static Value *UpgradeMaskedStore(IRBuilder<> &Builder, LLVMContext &C, 372 Value *Ptr, Value *Data, Value *Mask, 373 bool Aligned) { 374 // Cast the pointer to the right type. 375 Ptr = Builder.CreateBitCast(Ptr, 376 llvm::PointerType::getUnqual(Data->getType())); 377 unsigned Align = 378 Aligned ? cast<VectorType>(Data->getType())->getBitWidth() / 8 : 1; 379 380 // If the mask is all ones just emit a regular store. 381 if (const auto *C = dyn_cast<Constant>(Mask)) 382 if (C->isAllOnesValue()) 383 return Builder.CreateAlignedStore(Data, Ptr, Align); 384 385 // Convert the mask from an integer type to a vector of i1. 386 unsigned NumElts = Data->getType()->getVectorNumElements(); 387 llvm::VectorType *MaskTy = llvm::VectorType::get(Builder.getInt1Ty(), 388 cast<IntegerType>(Mask->getType())->getBitWidth()); 389 Mask = Builder.CreateBitCast(Mask, MaskTy); 390 391 // If we have less than 8 elements, then the starting mask was an i8 and 392 // we need to extract down to the right number of elements. 393 if (NumElts < 8) { 394 int Indices[4]; 395 for (unsigned i = 0; i != NumElts; ++i) 396 Indices[i] = i; 397 Mask = Builder.CreateShuffleVector(Mask, Mask, 398 makeArrayRef(Indices, NumElts), 399 "extract"); 400 } 401 402 return Builder.CreateMaskedStore(Data, Ptr, Align, Mask); 403 } 404 405 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the 406 // upgraded intrinsic. All argument and return casting must be provided in 407 // order to seamlessly integrate with existing context. 408 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) { 409 Function *F = CI->getCalledFunction(); 410 LLVMContext &C = CI->getContext(); 411 IRBuilder<> Builder(C); 412 Builder.SetInsertPoint(CI->getParent(), CI->getIterator()); 413 414 assert(F && "Intrinsic call is not direct?"); 415 416 if (!NewFn) { 417 // Get the Function's name. 418 StringRef Name = F->getName(); 419 420 Value *Rep; 421 // Upgrade packed integer vector compares intrinsics to compare instructions 422 if (Name.startswith("llvm.x86.sse2.pcmpeq.") || 423 Name.startswith("llvm.x86.avx2.pcmpeq.")) { 424 Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1), 425 "pcmpeq"); 426 // need to sign extend since icmp returns vector of i1 427 Rep = Builder.CreateSExt(Rep, CI->getType(), ""); 428 } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") || 429 Name.startswith("llvm.x86.avx2.pcmpgt.")) { 430 Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1), 431 "pcmpgt"); 432 // need to sign extend since icmp returns vector of i1 433 Rep = Builder.CreateSExt(Rep, CI->getType(), ""); 434 } else if (Name == "llvm.x86.sse2.cvtdq2pd" || 435 Name == "llvm.x86.sse2.cvtps2pd" || 436 Name == "llvm.x86.avx.cvtdq2.pd.256" || 437 Name == "llvm.x86.avx.cvt.ps2.pd.256") { 438 // Lossless i32/float to double conversion. 439 // Extract the bottom elements if necessary and convert to double vector. 440 Value *Src = CI->getArgOperand(0); 441 VectorType *SrcTy = cast<VectorType>(Src->getType()); 442 VectorType *DstTy = cast<VectorType>(CI->getType()); 443 Rep = CI->getArgOperand(0); 444 445 unsigned NumDstElts = DstTy->getNumElements(); 446 if (NumDstElts < SrcTy->getNumElements()) { 447 assert(NumDstElts == 2 && "Unexpected vector size"); 448 const int ShuffleMask[2] = { 0, 1 }; 449 Rep = Builder.CreateShuffleVector(Rep, UndefValue::get(SrcTy), ShuffleMask); 450 } 451 452 bool Int2Double = (StringRef::npos != Name.find("cvtdq2")); 453 if (Int2Double) 454 Rep = Builder.CreateSIToFP(Rep, DstTy, "cvtdq2pd"); 455 else 456 Rep = Builder.CreateFPExt(Rep, DstTy, "cvtps2pd"); 457 } else if (Name.startswith("llvm.x86.avx.movnt.")) { 458 Module *M = F->getParent(); 459 SmallVector<Metadata *, 1> Elts; 460 Elts.push_back( 461 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1))); 462 MDNode *Node = MDNode::get(C, Elts); 463 464 Value *Arg0 = CI->getArgOperand(0); 465 Value *Arg1 = CI->getArgOperand(1); 466 467 // Convert the type of the pointer to a pointer to the stored type. 468 Value *BC = Builder.CreateBitCast(Arg0, 469 PointerType::getUnqual(Arg1->getType()), 470 "cast"); 471 StoreInst *SI = Builder.CreateAlignedStore(Arg1, BC, 32); 472 SI->setMetadata(M->getMDKindID("nontemporal"), Node); 473 474 // Remove intrinsic. 475 CI->eraseFromParent(); 476 return; 477 } else if (Name == "llvm.x86.sse2.storel.dq") { 478 Value *Arg0 = CI->getArgOperand(0); 479 Value *Arg1 = CI->getArgOperand(1); 480 481 Type *NewVecTy = VectorType::get(Type::getInt64Ty(C), 2); 482 Value *BC0 = Builder.CreateBitCast(Arg1, NewVecTy, "cast"); 483 Value *Elt = Builder.CreateExtractElement(BC0, (uint64_t)0); 484 Value *BC = Builder.CreateBitCast(Arg0, 485 PointerType::getUnqual(Elt->getType()), 486 "cast"); 487 Builder.CreateAlignedStore(Elt, BC, 1); 488 489 // Remove intrinsic. 490 CI->eraseFromParent(); 491 return; 492 } else if (Name.startswith("llvm.x86.sse.storeu.") || 493 Name.startswith("llvm.x86.sse2.storeu.") || 494 Name.startswith("llvm.x86.avx.storeu.")) { 495 Value *Arg0 = CI->getArgOperand(0); 496 Value *Arg1 = CI->getArgOperand(1); 497 498 Arg0 = Builder.CreateBitCast(Arg0, 499 PointerType::getUnqual(Arg1->getType()), 500 "cast"); 501 Builder.CreateAlignedStore(Arg1, Arg0, 1); 502 503 // Remove intrinsic. 504 CI->eraseFromParent(); 505 return; 506 } else if (Name.startswith("llvm.x86.avx512.mask.storeu.p") || 507 Name.startswith("llvm.x86.avx512.mask.storeu.b.") || 508 Name.startswith("llvm.x86.avx512.mask.storeu.w.") || 509 Name.startswith("llvm.x86.avx512.mask.storeu.d.") || 510 Name.startswith("llvm.x86.avx512.mask.storeu.q.")) { 511 UpgradeMaskedStore(Builder, C, CI->getArgOperand(0), CI->getArgOperand(1), 512 CI->getArgOperand(2), /*Aligned*/false); 513 514 // Remove intrinsic. 515 CI->eraseFromParent(); 516 return; 517 } else if (Name.startswith("llvm.x86.avx512.mask.store.p") || 518 Name.startswith("llvm.x86.avx512.mask.store.b.") || 519 Name.startswith("llvm.x86.avx512.mask.store.w.") || 520 Name.startswith("llvm.x86.avx512.mask.store.d.") || 521 Name.startswith("llvm.x86.avx512.mask.store.q.")) { 522 UpgradeMaskedStore(Builder, C, CI->getArgOperand(0), CI->getArgOperand(1), 523 CI->getArgOperand(2), /*Aligned*/true); 524 525 // Remove intrinsic. 526 CI->eraseFromParent(); 527 return; 528 } else if (Name.startswith("llvm.x86.xop.vpcom")) { 529 Intrinsic::ID intID; 530 if (Name.endswith("ub")) 531 intID = Intrinsic::x86_xop_vpcomub; 532 else if (Name.endswith("uw")) 533 intID = Intrinsic::x86_xop_vpcomuw; 534 else if (Name.endswith("ud")) 535 intID = Intrinsic::x86_xop_vpcomud; 536 else if (Name.endswith("uq")) 537 intID = Intrinsic::x86_xop_vpcomuq; 538 else if (Name.endswith("b")) 539 intID = Intrinsic::x86_xop_vpcomb; 540 else if (Name.endswith("w")) 541 intID = Intrinsic::x86_xop_vpcomw; 542 else if (Name.endswith("d")) 543 intID = Intrinsic::x86_xop_vpcomd; 544 else if (Name.endswith("q")) 545 intID = Intrinsic::x86_xop_vpcomq; 546 else 547 llvm_unreachable("Unknown suffix"); 548 549 Name = Name.substr(18); // strip off "llvm.x86.xop.vpcom" 550 unsigned Imm; 551 if (Name.startswith("lt")) 552 Imm = 0; 553 else if (Name.startswith("le")) 554 Imm = 1; 555 else if (Name.startswith("gt")) 556 Imm = 2; 557 else if (Name.startswith("ge")) 558 Imm = 3; 559 else if (Name.startswith("eq")) 560 Imm = 4; 561 else if (Name.startswith("ne")) 562 Imm = 5; 563 else if (Name.startswith("false")) 564 Imm = 6; 565 else if (Name.startswith("true")) 566 Imm = 7; 567 else 568 llvm_unreachable("Unknown condition"); 569 570 Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID); 571 Rep = 572 Builder.CreateCall(VPCOM, {CI->getArgOperand(0), CI->getArgOperand(1), 573 Builder.getInt8(Imm)}); 574 } else if (Name == "llvm.x86.xop.vpcmov") { 575 Value *Arg0 = CI->getArgOperand(0); 576 Value *Arg1 = CI->getArgOperand(1); 577 Value *Sel = CI->getArgOperand(2); 578 unsigned NumElts = CI->getType()->getVectorNumElements(); 579 Constant *MinusOne = ConstantVector::getSplat(NumElts, Builder.getInt64(-1)); 580 Value *NotSel = Builder.CreateXor(Sel, MinusOne); 581 Value *Sel0 = Builder.CreateAnd(Arg0, Sel); 582 Value *Sel1 = Builder.CreateAnd(Arg1, NotSel); 583 Rep = Builder.CreateOr(Sel0, Sel1); 584 } else if (Name == "llvm.x86.sse42.crc32.64.8") { 585 Function *CRC32 = Intrinsic::getDeclaration(F->getParent(), 586 Intrinsic::x86_sse42_crc32_32_8); 587 Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C)); 588 Rep = Builder.CreateCall(CRC32, {Trunc0, CI->getArgOperand(1)}); 589 Rep = Builder.CreateZExt(Rep, CI->getType(), ""); 590 } else if (Name.startswith("llvm.x86.avx.vbroadcast")) { 591 // Replace broadcasts with a series of insertelements. 592 Type *VecTy = CI->getType(); 593 Type *EltTy = VecTy->getVectorElementType(); 594 unsigned EltNum = VecTy->getVectorNumElements(); 595 Value *Cast = Builder.CreateBitCast(CI->getArgOperand(0), 596 EltTy->getPointerTo()); 597 Value *Load = Builder.CreateLoad(EltTy, Cast); 598 Type *I32Ty = Type::getInt32Ty(C); 599 Rep = UndefValue::get(VecTy); 600 for (unsigned I = 0; I < EltNum; ++I) 601 Rep = Builder.CreateInsertElement(Rep, Load, 602 ConstantInt::get(I32Ty, I)); 603 } else if (Name.startswith("llvm.x86.sse41.pmovsx") || 604 Name.startswith("llvm.x86.sse41.pmovzx") || 605 Name.startswith("llvm.x86.avx2.pmovsx") || 606 Name.startswith("llvm.x86.avx2.pmovzx")) { 607 VectorType *SrcTy = cast<VectorType>(CI->getArgOperand(0)->getType()); 608 VectorType *DstTy = cast<VectorType>(CI->getType()); 609 unsigned NumDstElts = DstTy->getNumElements(); 610 611 // Extract a subvector of the first NumDstElts lanes and sign/zero extend. 612 SmallVector<int, 8> ShuffleMask; 613 for (int i = 0; i != (int)NumDstElts; ++i) 614 ShuffleMask.push_back(i); 615 616 Value *SV = Builder.CreateShuffleVector( 617 CI->getArgOperand(0), UndefValue::get(SrcTy), ShuffleMask); 618 619 bool DoSext = (StringRef::npos != Name.find("pmovsx")); 620 Rep = DoSext ? Builder.CreateSExt(SV, DstTy) 621 : Builder.CreateZExt(SV, DstTy); 622 } else if (Name == "llvm.x86.avx2.vbroadcasti128") { 623 // Replace vbroadcasts with a vector shuffle. 624 Type *VT = VectorType::get(Type::getInt64Ty(C), 2); 625 Value *Op = Builder.CreatePointerCast(CI->getArgOperand(0), 626 PointerType::getUnqual(VT)); 627 Value *Load = Builder.CreateLoad(VT, Op); 628 const int Idxs[4] = { 0, 1, 0, 1 }; 629 Rep = Builder.CreateShuffleVector(Load, UndefValue::get(Load->getType()), 630 Idxs); 631 } else if (Name.startswith("llvm.x86.avx2.pbroadcast") || 632 Name.startswith("llvm.x86.avx2.vbroadcast")) { 633 // Replace vp?broadcasts with a vector shuffle. 634 Value *Op = CI->getArgOperand(0); 635 unsigned NumElts = CI->getType()->getVectorNumElements(); 636 Type *MaskTy = VectorType::get(Type::getInt32Ty(C), NumElts); 637 Rep = Builder.CreateShuffleVector(Op, UndefValue::get(Op->getType()), 638 Constant::getNullValue(MaskTy)); 639 } else if (Name == "llvm.x86.sse2.psll.dq" || 640 Name == "llvm.x86.avx2.psll.dq") { 641 // 128/256-bit shift left specified in bits. 642 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue(); 643 Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0), 644 Shift / 8); // Shift is in bits. 645 } else if (Name == "llvm.x86.sse2.psrl.dq" || 646 Name == "llvm.x86.avx2.psrl.dq") { 647 // 128/256-bit shift right specified in bits. 648 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue(); 649 Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0), 650 Shift / 8); // Shift is in bits. 651 } else if (Name == "llvm.x86.sse2.psll.dq.bs" || 652 Name == "llvm.x86.avx2.psll.dq.bs") { 653 // 128/256-bit shift left specified in bytes. 654 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue(); 655 Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0), Shift); 656 } else if (Name == "llvm.x86.sse2.psrl.dq.bs" || 657 Name == "llvm.x86.avx2.psrl.dq.bs") { 658 // 128/256-bit shift right specified in bytes. 659 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue(); 660 Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0), Shift); 661 } else if (Name == "llvm.x86.sse41.pblendw" || 662 Name.startswith("llvm.x86.sse41.blendp") || 663 Name.startswith("llvm.x86.avx.blend.p") || 664 Name == "llvm.x86.avx2.pblendw" || 665 Name.startswith("llvm.x86.avx2.pblendd.")) { 666 Value *Op0 = CI->getArgOperand(0); 667 Value *Op1 = CI->getArgOperand(1); 668 unsigned Imm = cast <ConstantInt>(CI->getArgOperand(2))->getZExtValue(); 669 VectorType *VecTy = cast<VectorType>(CI->getType()); 670 unsigned NumElts = VecTy->getNumElements(); 671 672 SmallVector<Constant*, 16> Idxs; 673 for (unsigned i = 0; i != NumElts; ++i) { 674 unsigned Idx = ((Imm >> (i%8)) & 1) ? i + NumElts : i; 675 Idxs.push_back(Builder.getInt32(Idx)); 676 } 677 678 Rep = Builder.CreateShuffleVector(Op0, Op1, ConstantVector::get(Idxs)); 679 } else if (Name.startswith("llvm.x86.avx.vinsertf128.") || 680 Name == "llvm.x86.avx2.vinserti128") { 681 Value *Op0 = CI->getArgOperand(0); 682 Value *Op1 = CI->getArgOperand(1); 683 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue(); 684 VectorType *VecTy = cast<VectorType>(CI->getType()); 685 unsigned NumElts = VecTy->getNumElements(); 686 687 // Mask off the high bits of the immediate value; hardware ignores those. 688 Imm = Imm & 1; 689 690 // Extend the second operand into a vector that is twice as big. 691 Value *UndefV = UndefValue::get(Op1->getType()); 692 SmallVector<Constant*, 8> Idxs; 693 for (unsigned i = 0; i != NumElts; ++i) { 694 Idxs.push_back(Builder.getInt32(i)); 695 } 696 Rep = Builder.CreateShuffleVector(Op1, UndefV, ConstantVector::get(Idxs)); 697 698 // Insert the second operand into the first operand. 699 700 // Note that there is no guarantee that instruction lowering will actually 701 // produce a vinsertf128 instruction for the created shuffles. In 702 // particular, the 0 immediate case involves no lane changes, so it can 703 // be handled as a blend. 704 705 // Example of shuffle mask for 32-bit elements: 706 // Imm = 1 <i32 0, i32 1, i32 2, i32 3, i32 8, i32 9, i32 10, i32 11> 707 // Imm = 0 <i32 8, i32 9, i32 10, i32 11, i32 4, i32 5, i32 6, i32 7 > 708 709 SmallVector<Constant*, 8> Idxs2; 710 // The low half of the result is either the low half of the 1st operand 711 // or the low half of the 2nd operand (the inserted vector). 712 for (unsigned i = 0; i != NumElts / 2; ++i) { 713 unsigned Idx = Imm ? i : (i + NumElts); 714 Idxs2.push_back(Builder.getInt32(Idx)); 715 } 716 // The high half of the result is either the low half of the 2nd operand 717 // (the inserted vector) or the high half of the 1st operand. 718 for (unsigned i = NumElts / 2; i != NumElts; ++i) { 719 unsigned Idx = Imm ? (i + NumElts / 2) : i; 720 Idxs2.push_back(Builder.getInt32(Idx)); 721 } 722 Rep = Builder.CreateShuffleVector(Op0, Rep, ConstantVector::get(Idxs2)); 723 } else if (Name.startswith("llvm.x86.avx.vextractf128.") || 724 Name == "llvm.x86.avx2.vextracti128") { 725 Value *Op0 = CI->getArgOperand(0); 726 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue(); 727 VectorType *VecTy = cast<VectorType>(CI->getType()); 728 unsigned NumElts = VecTy->getNumElements(); 729 730 // Mask off the high bits of the immediate value; hardware ignores those. 731 Imm = Imm & 1; 732 733 // Get indexes for either the high half or low half of the input vector. 734 SmallVector<Constant*, 4> Idxs(NumElts); 735 for (unsigned i = 0; i != NumElts; ++i) { 736 unsigned Idx = Imm ? (i + NumElts) : i; 737 Idxs[i] = Builder.getInt32(Idx); 738 } 739 740 Value *UndefV = UndefValue::get(Op0->getType()); 741 Rep = Builder.CreateShuffleVector(Op0, UndefV, ConstantVector::get(Idxs)); 742 } else if (Name == "llvm.stackprotectorcheck") { 743 Rep = nullptr; 744 } else { 745 bool PD128 = false, PD256 = false, PS128 = false, PS256 = false; 746 if (Name == "llvm.x86.avx.vpermil.pd.256") 747 PD256 = true; 748 else if (Name == "llvm.x86.avx.vpermil.pd") 749 PD128 = true; 750 else if (Name == "llvm.x86.avx.vpermil.ps.256") 751 PS256 = true; 752 else if (Name == "llvm.x86.avx.vpermil.ps") 753 PS128 = true; 754 755 if (PD256 || PD128 || PS256 || PS128) { 756 Value *Op0 = CI->getArgOperand(0); 757 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue(); 758 SmallVector<Constant*, 8> Idxs; 759 760 if (PD128) 761 for (unsigned i = 0; i != 2; ++i) 762 Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1)); 763 else if (PD256) 764 for (unsigned l = 0; l != 4; l+=2) 765 for (unsigned i = 0; i != 2; ++i) 766 Idxs.push_back(Builder.getInt32(((Imm >> (l+i)) & 0x1) + l)); 767 else if (PS128) 768 for (unsigned i = 0; i != 4; ++i) 769 Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3)); 770 else if (PS256) 771 for (unsigned l = 0; l != 8; l+=4) 772 for (unsigned i = 0; i != 4; ++i) 773 Idxs.push_back(Builder.getInt32(((Imm >> (2 * i)) & 0x3) + l)); 774 else 775 llvm_unreachable("Unexpected function"); 776 777 Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs)); 778 } else { 779 llvm_unreachable("Unknown function for CallInst upgrade."); 780 } 781 } 782 783 if (Rep) 784 CI->replaceAllUsesWith(Rep); 785 CI->eraseFromParent(); 786 return; 787 } 788 789 std::string Name = CI->getName(); 790 if (!Name.empty()) 791 CI->setName(Name + ".old"); 792 793 switch (NewFn->getIntrinsicID()) { 794 default: 795 llvm_unreachable("Unknown function for CallInst upgrade."); 796 797 case Intrinsic::arm_neon_vld1: 798 case Intrinsic::arm_neon_vld2: 799 case Intrinsic::arm_neon_vld3: 800 case Intrinsic::arm_neon_vld4: 801 case Intrinsic::arm_neon_vld2lane: 802 case Intrinsic::arm_neon_vld3lane: 803 case Intrinsic::arm_neon_vld4lane: 804 case Intrinsic::arm_neon_vst1: 805 case Intrinsic::arm_neon_vst2: 806 case Intrinsic::arm_neon_vst3: 807 case Intrinsic::arm_neon_vst4: 808 case Intrinsic::arm_neon_vst2lane: 809 case Intrinsic::arm_neon_vst3lane: 810 case Intrinsic::arm_neon_vst4lane: { 811 SmallVector<Value *, 4> Args(CI->arg_operands().begin(), 812 CI->arg_operands().end()); 813 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args)); 814 CI->eraseFromParent(); 815 return; 816 } 817 818 case Intrinsic::ctlz: 819 case Intrinsic::cttz: 820 assert(CI->getNumArgOperands() == 1 && 821 "Mismatch between function args and call args"); 822 CI->replaceAllUsesWith(Builder.CreateCall( 823 NewFn, {CI->getArgOperand(0), Builder.getFalse()}, Name)); 824 CI->eraseFromParent(); 825 return; 826 827 case Intrinsic::objectsize: 828 CI->replaceAllUsesWith(Builder.CreateCall( 829 NewFn, {CI->getArgOperand(0), CI->getArgOperand(1)}, Name)); 830 CI->eraseFromParent(); 831 return; 832 833 case Intrinsic::ctpop: { 834 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, {CI->getArgOperand(0)})); 835 CI->eraseFromParent(); 836 return; 837 } 838 839 case Intrinsic::x86_xop_vfrcz_ss: 840 case Intrinsic::x86_xop_vfrcz_sd: 841 CI->replaceAllUsesWith( 842 Builder.CreateCall(NewFn, {CI->getArgOperand(1)}, Name)); 843 CI->eraseFromParent(); 844 return; 845 846 case Intrinsic::x86_sse41_ptestc: 847 case Intrinsic::x86_sse41_ptestz: 848 case Intrinsic::x86_sse41_ptestnzc: { 849 // The arguments for these intrinsics used to be v4f32, and changed 850 // to v2i64. This is purely a nop, since those are bitwise intrinsics. 851 // So, the only thing required is a bitcast for both arguments. 852 // First, check the arguments have the old type. 853 Value *Arg0 = CI->getArgOperand(0); 854 if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4)) 855 return; 856 857 // Old intrinsic, add bitcasts 858 Value *Arg1 = CI->getArgOperand(1); 859 860 Type *NewVecTy = VectorType::get(Type::getInt64Ty(C), 2); 861 862 Value *BC0 = Builder.CreateBitCast(Arg0, NewVecTy, "cast"); 863 Value *BC1 = Builder.CreateBitCast(Arg1, NewVecTy, "cast"); 864 865 CallInst *NewCall = Builder.CreateCall(NewFn, {BC0, BC1}, Name); 866 CI->replaceAllUsesWith(NewCall); 867 CI->eraseFromParent(); 868 return; 869 } 870 871 case Intrinsic::x86_sse41_insertps: 872 case Intrinsic::x86_sse41_dppd: 873 case Intrinsic::x86_sse41_dpps: 874 case Intrinsic::x86_sse41_mpsadbw: 875 case Intrinsic::x86_avx_dp_ps_256: 876 case Intrinsic::x86_avx2_mpsadbw: { 877 // Need to truncate the last argument from i32 to i8 -- this argument models 878 // an inherently 8-bit immediate operand to these x86 instructions. 879 SmallVector<Value *, 4> Args(CI->arg_operands().begin(), 880 CI->arg_operands().end()); 881 882 // Replace the last argument with a trunc. 883 Args.back() = Builder.CreateTrunc(Args.back(), Type::getInt8Ty(C), "trunc"); 884 885 CallInst *NewCall = Builder.CreateCall(NewFn, Args); 886 CI->replaceAllUsesWith(NewCall); 887 CI->eraseFromParent(); 888 return; 889 } 890 891 case Intrinsic::thread_pointer: { 892 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, {})); 893 CI->eraseFromParent(); 894 return; 895 } 896 } 897 } 898 899 void llvm::UpgradeCallsToIntrinsic(Function *F) { 900 assert(F && "Illegal attempt to upgrade a non-existent intrinsic."); 901 902 // Check if this function should be upgraded and get the replacement function 903 // if there is one. 904 Function *NewFn; 905 if (UpgradeIntrinsicFunction(F, NewFn)) { 906 // Replace all users of the old function with the new function or new 907 // instructions. This is not a range loop because the call is deleted. 908 for (auto UI = F->user_begin(), UE = F->user_end(); UI != UE; ) 909 if (CallInst *CI = dyn_cast<CallInst>(*UI++)) 910 UpgradeIntrinsicCall(CI, NewFn); 911 912 // Remove old function, no longer used, from the module. 913 F->eraseFromParent(); 914 } 915 } 916 917 void llvm::UpgradeInstWithTBAATag(Instruction *I) { 918 MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa); 919 assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag"); 920 // Check if the tag uses struct-path aware TBAA format. 921 if (isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3) 922 return; 923 924 if (MD->getNumOperands() == 3) { 925 Metadata *Elts[] = {MD->getOperand(0), MD->getOperand(1)}; 926 MDNode *ScalarType = MDNode::get(I->getContext(), Elts); 927 // Create a MDNode <ScalarType, ScalarType, offset 0, const> 928 Metadata *Elts2[] = {ScalarType, ScalarType, 929 ConstantAsMetadata::get(Constant::getNullValue( 930 Type::getInt64Ty(I->getContext()))), 931 MD->getOperand(2)}; 932 I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2)); 933 } else { 934 // Create a MDNode <MD, MD, offset 0> 935 Metadata *Elts[] = {MD, MD, ConstantAsMetadata::get(Constant::getNullValue( 936 Type::getInt64Ty(I->getContext())))}; 937 I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts)); 938 } 939 } 940 941 Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy, 942 Instruction *&Temp) { 943 if (Opc != Instruction::BitCast) 944 return nullptr; 945 946 Temp = nullptr; 947 Type *SrcTy = V->getType(); 948 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() && 949 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) { 950 LLVMContext &Context = V->getContext(); 951 952 // We have no information about target data layout, so we assume that 953 // the maximum pointer size is 64bit. 954 Type *MidTy = Type::getInt64Ty(Context); 955 Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy); 956 957 return CastInst::Create(Instruction::IntToPtr, Temp, DestTy); 958 } 959 960 return nullptr; 961 } 962 963 Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) { 964 if (Opc != Instruction::BitCast) 965 return nullptr; 966 967 Type *SrcTy = C->getType(); 968 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() && 969 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) { 970 LLVMContext &Context = C->getContext(); 971 972 // We have no information about target data layout, so we assume that 973 // the maximum pointer size is 64bit. 974 Type *MidTy = Type::getInt64Ty(Context); 975 976 return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy), 977 DestTy); 978 } 979 980 return nullptr; 981 } 982 983 /// Check the debug info version number, if it is out-dated, drop the debug 984 /// info. Return true if module is modified. 985 bool llvm::UpgradeDebugInfo(Module &M) { 986 unsigned Version = getDebugMetadataVersionFromModule(M); 987 if (Version == DEBUG_METADATA_VERSION) 988 return false; 989 990 bool RetCode = StripDebugInfo(M); 991 if (RetCode) { 992 DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version); 993 M.getContext().diagnose(DiagVersion); 994 } 995 return RetCode; 996 } 997 998 bool llvm::UpgradeModuleFlags(Module &M) { 999 const NamedMDNode *ModFlags = M.getModuleFlagsMetadata(); 1000 if (!ModFlags) 1001 return false; 1002 1003 bool HasObjCFlag = false, HasClassProperties = false; 1004 for (unsigned I = 0, E = ModFlags->getNumOperands(); I != E; ++I) { 1005 MDNode *Op = ModFlags->getOperand(I); 1006 if (Op->getNumOperands() < 2) 1007 continue; 1008 MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(1)); 1009 if (!ID) 1010 continue; 1011 if (ID->getString() == "Objective-C Image Info Version") 1012 HasObjCFlag = true; 1013 if (ID->getString() == "Objective-C Class Properties") 1014 HasClassProperties = true; 1015 } 1016 // "Objective-C Class Properties" is recently added for Objective-C. We 1017 // upgrade ObjC bitcodes to contain a "Objective-C Class Properties" module 1018 // flag of value 0, so we can correclty report error when trying to link 1019 // an ObjC bitcode without this module flag with an ObjC bitcode with this 1020 // module flag. 1021 if (HasObjCFlag && !HasClassProperties) { 1022 M.addModuleFlag(llvm::Module::Error, "Objective-C Class Properties", 1023 (uint32_t)0); 1024 return true; 1025 } 1026 return false; 1027 } 1028 1029 static bool isOldLoopArgument(Metadata *MD) { 1030 auto *T = dyn_cast_or_null<MDTuple>(MD); 1031 if (!T) 1032 return false; 1033 if (T->getNumOperands() < 1) 1034 return false; 1035 auto *S = dyn_cast_or_null<MDString>(T->getOperand(0)); 1036 if (!S) 1037 return false; 1038 return S->getString().startswith("llvm.vectorizer."); 1039 } 1040 1041 static MDString *upgradeLoopTag(LLVMContext &C, StringRef OldTag) { 1042 StringRef OldPrefix = "llvm.vectorizer."; 1043 assert(OldTag.startswith(OldPrefix) && "Expected old prefix"); 1044 1045 if (OldTag == "llvm.vectorizer.unroll") 1046 return MDString::get(C, "llvm.loop.interleave.count"); 1047 1048 return MDString::get( 1049 C, (Twine("llvm.loop.vectorize.") + OldTag.drop_front(OldPrefix.size())) 1050 .str()); 1051 } 1052 1053 static Metadata *upgradeLoopArgument(Metadata *MD) { 1054 auto *T = dyn_cast_or_null<MDTuple>(MD); 1055 if (!T) 1056 return MD; 1057 if (T->getNumOperands() < 1) 1058 return MD; 1059 auto *OldTag = dyn_cast_or_null<MDString>(T->getOperand(0)); 1060 if (!OldTag) 1061 return MD; 1062 if (!OldTag->getString().startswith("llvm.vectorizer.")) 1063 return MD; 1064 1065 // This has an old tag. Upgrade it. 1066 SmallVector<Metadata *, 8> Ops; 1067 Ops.reserve(T->getNumOperands()); 1068 Ops.push_back(upgradeLoopTag(T->getContext(), OldTag->getString())); 1069 for (unsigned I = 1, E = T->getNumOperands(); I != E; ++I) 1070 Ops.push_back(T->getOperand(I)); 1071 1072 return MDTuple::get(T->getContext(), Ops); 1073 } 1074 1075 MDNode *llvm::upgradeInstructionLoopAttachment(MDNode &N) { 1076 auto *T = dyn_cast<MDTuple>(&N); 1077 if (!T) 1078 return &N; 1079 1080 if (!llvm::any_of(T->operands(), isOldLoopArgument)) 1081 return &N; 1082 1083 SmallVector<Metadata *, 8> Ops; 1084 Ops.reserve(T->getNumOperands()); 1085 for (Metadata *MD : T->operands()) 1086 Ops.push_back(upgradeLoopArgument(MD)); 1087 1088 return MDTuple::get(T->getContext(), Ops); 1089 } 1090