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/DebugInfo.h" 19 #include "llvm/IR/DiagnosticInfo.h" 20 #include "llvm/IR/Function.h" 21 #include "llvm/IR/IRBuilder.h" 22 #include "llvm/IR/Instruction.h" 23 #include "llvm/IR/IntrinsicInst.h" 24 #include "llvm/IR/LLVMContext.h" 25 #include "llvm/IR/Module.h" 26 #include "llvm/Support/ErrorHandling.h" 27 #include <cstring> 28 using namespace llvm; 29 30 // Upgrade the declarations of the SSE4.1 functions whose arguments have 31 // changed their type from v4f32 to v2i64. 32 static bool UpgradeSSE41Function(Function* F, Intrinsic::ID IID, 33 Function *&NewFn) { 34 // Check whether this is an old version of the function, which received 35 // v4f32 arguments. 36 Type *Arg0Type = F->getFunctionType()->getParamType(0); 37 if (Arg0Type != VectorType::get(Type::getFloatTy(F->getContext()), 4)) 38 return false; 39 40 // Yes, it's old, replace it with new version. 41 F->setName(F->getName() + ".old"); 42 NewFn = Intrinsic::getDeclaration(F->getParent(), IID); 43 return true; 44 } 45 46 static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) { 47 assert(F && "Illegal to upgrade a non-existent Function."); 48 49 // Quickly eliminate it, if it's not a candidate. 50 StringRef Name = F->getName(); 51 if (Name.size() <= 8 || !Name.startswith("llvm.")) 52 return false; 53 Name = Name.substr(5); // Strip off "llvm." 54 55 switch (Name[0]) { 56 default: break; 57 case 'a': { 58 if (Name.startswith("arm.neon.vclz")) { 59 Type* args[2] = { 60 F->arg_begin()->getType(), 61 Type::getInt1Ty(F->getContext()) 62 }; 63 // Can't use Intrinsic::getDeclaration here as it adds a ".i1" to 64 // the end of the name. Change name from llvm.arm.neon.vclz.* to 65 // llvm.ctlz.* 66 FunctionType* fType = FunctionType::get(F->getReturnType(), args, false); 67 NewFn = Function::Create(fType, F->getLinkage(), 68 "llvm.ctlz." + Name.substr(14), F->getParent()); 69 return true; 70 } 71 if (Name.startswith("arm.neon.vcnt")) { 72 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctpop, 73 F->arg_begin()->getType()); 74 return true; 75 } 76 break; 77 } 78 case 'c': { 79 if (Name.startswith("ctlz.") && F->arg_size() == 1) { 80 F->setName(Name + ".old"); 81 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz, 82 F->arg_begin()->getType()); 83 return true; 84 } 85 if (Name.startswith("cttz.") && F->arg_size() == 1) { 86 F->setName(Name + ".old"); 87 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz, 88 F->arg_begin()->getType()); 89 return true; 90 } 91 break; 92 } 93 case 'o': 94 // We only need to change the name to match the mangling including the 95 // address space. 96 if (F->arg_size() == 2 && Name.startswith("objectsize.")) { 97 Type *Tys[2] = { F->getReturnType(), F->arg_begin()->getType() }; 98 if (F->getName() != Intrinsic::getName(Intrinsic::objectsize, Tys)) { 99 F->setName(Name + ".old"); 100 NewFn = Intrinsic::getDeclaration(F->getParent(), 101 Intrinsic::objectsize, Tys); 102 return true; 103 } 104 } 105 break; 106 107 case 'x': { 108 if (Name.startswith("x86.sse2.pcmpeq.") || 109 Name.startswith("x86.sse2.pcmpgt.") || 110 Name.startswith("x86.avx2.pcmpeq.") || 111 Name.startswith("x86.avx2.pcmpgt.") || 112 Name.startswith("x86.avx.vpermil.") || 113 Name == "x86.avx.movnt.dq.256" || 114 Name == "x86.avx.movnt.pd.256" || 115 Name == "x86.avx.movnt.ps.256" || 116 Name == "x86.sse42.crc32.64.8" || 117 (Name.startswith("x86.xop.vpcom") && F->arg_size() == 2)) { 118 NewFn = 0; 119 return true; 120 } 121 // SSE4.1 ptest functions may have an old signature. 122 if (Name.startswith("x86.sse41.ptest")) { 123 if (Name == "x86.sse41.ptestc") 124 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn); 125 if (Name == "x86.sse41.ptestz") 126 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn); 127 if (Name == "x86.sse41.ptestnzc") 128 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn); 129 } 130 // frcz.ss/sd may need to have an argument dropped 131 if (Name.startswith("x86.xop.vfrcz.ss") && F->arg_size() == 2) { 132 F->setName(Name + ".old"); 133 NewFn = Intrinsic::getDeclaration(F->getParent(), 134 Intrinsic::x86_xop_vfrcz_ss); 135 return true; 136 } 137 if (Name.startswith("x86.xop.vfrcz.sd") && F->arg_size() == 2) { 138 F->setName(Name + ".old"); 139 NewFn = Intrinsic::getDeclaration(F->getParent(), 140 Intrinsic::x86_xop_vfrcz_sd); 141 return true; 142 } 143 // Fix the FMA4 intrinsics to remove the 4 144 if (Name.startswith("x86.fma4.")) { 145 F->setName("llvm.x86.fma" + Name.substr(8)); 146 NewFn = F; 147 return true; 148 } 149 break; 150 } 151 } 152 153 // This may not belong here. This function is effectively being overloaded 154 // to both detect an intrinsic which needs upgrading, and to provide the 155 // upgraded form of the intrinsic. We should perhaps have two separate 156 // functions for this. 157 return false; 158 } 159 160 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) { 161 NewFn = 0; 162 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn); 163 164 // Upgrade intrinsic attributes. This does not change the function. 165 if (NewFn) 166 F = NewFn; 167 if (unsigned id = F->getIntrinsicID()) 168 F->setAttributes(Intrinsic::getAttributes(F->getContext(), 169 (Intrinsic::ID)id)); 170 return Upgraded; 171 } 172 173 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) { 174 // Nothing to do yet. 175 return false; 176 } 177 178 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the 179 // upgraded intrinsic. All argument and return casting must be provided in 180 // order to seamlessly integrate with existing context. 181 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) { 182 Function *F = CI->getCalledFunction(); 183 LLVMContext &C = CI->getContext(); 184 IRBuilder<> Builder(C); 185 Builder.SetInsertPoint(CI->getParent(), CI); 186 187 assert(F && "Intrinsic call is not direct?"); 188 189 if (!NewFn) { 190 // Get the Function's name. 191 StringRef Name = F->getName(); 192 193 Value *Rep; 194 // Upgrade packed integer vector compares intrinsics to compare instructions 195 if (Name.startswith("llvm.x86.sse2.pcmpeq.") || 196 Name.startswith("llvm.x86.avx2.pcmpeq.")) { 197 Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1), 198 "pcmpeq"); 199 // need to sign extend since icmp returns vector of i1 200 Rep = Builder.CreateSExt(Rep, CI->getType(), ""); 201 } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") || 202 Name.startswith("llvm.x86.avx2.pcmpgt.")) { 203 Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1), 204 "pcmpgt"); 205 // need to sign extend since icmp returns vector of i1 206 Rep = Builder.CreateSExt(Rep, CI->getType(), ""); 207 } else if (Name == "llvm.x86.avx.movnt.dq.256" || 208 Name == "llvm.x86.avx.movnt.ps.256" || 209 Name == "llvm.x86.avx.movnt.pd.256") { 210 IRBuilder<> Builder(C); 211 Builder.SetInsertPoint(CI->getParent(), CI); 212 213 Module *M = F->getParent(); 214 SmallVector<Value *, 1> Elts; 215 Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1)); 216 MDNode *Node = MDNode::get(C, Elts); 217 218 Value *Arg0 = CI->getArgOperand(0); 219 Value *Arg1 = CI->getArgOperand(1); 220 221 // Convert the type of the pointer to a pointer to the stored type. 222 Value *BC = Builder.CreateBitCast(Arg0, 223 PointerType::getUnqual(Arg1->getType()), 224 "cast"); 225 StoreInst *SI = Builder.CreateStore(Arg1, BC); 226 SI->setMetadata(M->getMDKindID("nontemporal"), Node); 227 SI->setAlignment(16); 228 229 // Remove intrinsic. 230 CI->eraseFromParent(); 231 return; 232 } else if (Name.startswith("llvm.x86.xop.vpcom")) { 233 Intrinsic::ID intID; 234 if (Name.endswith("ub")) 235 intID = Intrinsic::x86_xop_vpcomub; 236 else if (Name.endswith("uw")) 237 intID = Intrinsic::x86_xop_vpcomuw; 238 else if (Name.endswith("ud")) 239 intID = Intrinsic::x86_xop_vpcomud; 240 else if (Name.endswith("uq")) 241 intID = Intrinsic::x86_xop_vpcomuq; 242 else if (Name.endswith("b")) 243 intID = Intrinsic::x86_xop_vpcomb; 244 else if (Name.endswith("w")) 245 intID = Intrinsic::x86_xop_vpcomw; 246 else if (Name.endswith("d")) 247 intID = Intrinsic::x86_xop_vpcomd; 248 else if (Name.endswith("q")) 249 intID = Intrinsic::x86_xop_vpcomq; 250 else 251 llvm_unreachable("Unknown suffix"); 252 253 Name = Name.substr(18); // strip off "llvm.x86.xop.vpcom" 254 unsigned Imm; 255 if (Name.startswith("lt")) 256 Imm = 0; 257 else if (Name.startswith("le")) 258 Imm = 1; 259 else if (Name.startswith("gt")) 260 Imm = 2; 261 else if (Name.startswith("ge")) 262 Imm = 3; 263 else if (Name.startswith("eq")) 264 Imm = 4; 265 else if (Name.startswith("ne")) 266 Imm = 5; 267 else if (Name.startswith("true")) 268 Imm = 6; 269 else if (Name.startswith("false")) 270 Imm = 7; 271 else 272 llvm_unreachable("Unknown condition"); 273 274 Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID); 275 Rep = Builder.CreateCall3(VPCOM, CI->getArgOperand(0), 276 CI->getArgOperand(1), Builder.getInt8(Imm)); 277 } else if (Name == "llvm.x86.sse42.crc32.64.8") { 278 Function *CRC32 = Intrinsic::getDeclaration(F->getParent(), 279 Intrinsic::x86_sse42_crc32_32_8); 280 Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C)); 281 Rep = Builder.CreateCall2(CRC32, Trunc0, CI->getArgOperand(1)); 282 Rep = Builder.CreateZExt(Rep, CI->getType(), ""); 283 } else { 284 bool PD128 = false, PD256 = false, PS128 = false, PS256 = false; 285 if (Name == "llvm.x86.avx.vpermil.pd.256") 286 PD256 = true; 287 else if (Name == "llvm.x86.avx.vpermil.pd") 288 PD128 = true; 289 else if (Name == "llvm.x86.avx.vpermil.ps.256") 290 PS256 = true; 291 else if (Name == "llvm.x86.avx.vpermil.ps") 292 PS128 = true; 293 294 if (PD256 || PD128 || PS256 || PS128) { 295 Value *Op0 = CI->getArgOperand(0); 296 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue(); 297 SmallVector<Constant*, 8> Idxs; 298 299 if (PD128) 300 for (unsigned i = 0; i != 2; ++i) 301 Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1)); 302 else if (PD256) 303 for (unsigned l = 0; l != 4; l+=2) 304 for (unsigned i = 0; i != 2; ++i) 305 Idxs.push_back(Builder.getInt32(((Imm >> (l+i)) & 0x1) + l)); 306 else if (PS128) 307 for (unsigned i = 0; i != 4; ++i) 308 Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3)); 309 else if (PS256) 310 for (unsigned l = 0; l != 8; l+=4) 311 for (unsigned i = 0; i != 4; ++i) 312 Idxs.push_back(Builder.getInt32(((Imm >> (2 * i)) & 0x3) + l)); 313 else 314 llvm_unreachable("Unexpected function"); 315 316 Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs)); 317 } else { 318 llvm_unreachable("Unknown function for CallInst upgrade."); 319 } 320 } 321 322 CI->replaceAllUsesWith(Rep); 323 CI->eraseFromParent(); 324 return; 325 } 326 327 std::string Name = CI->getName().str(); 328 CI->setName(Name + ".old"); 329 330 switch (NewFn->getIntrinsicID()) { 331 default: 332 llvm_unreachable("Unknown function for CallInst upgrade."); 333 334 case Intrinsic::ctlz: 335 case Intrinsic::cttz: 336 assert(CI->getNumArgOperands() == 1 && 337 "Mismatch between function args and call args"); 338 CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0), 339 Builder.getFalse(), Name)); 340 CI->eraseFromParent(); 341 return; 342 343 case Intrinsic::objectsize: 344 CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, 345 CI->getArgOperand(0), 346 CI->getArgOperand(1), 347 Name)); 348 CI->eraseFromParent(); 349 return; 350 351 case Intrinsic::arm_neon_vclz: { 352 // Change name from llvm.arm.neon.vclz.* to llvm.ctlz.* 353 CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0), 354 Builder.getFalse(), 355 "llvm.ctlz." + Name.substr(14))); 356 CI->eraseFromParent(); 357 return; 358 } 359 case Intrinsic::ctpop: { 360 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(0))); 361 CI->eraseFromParent(); 362 return; 363 } 364 365 case Intrinsic::x86_xop_vfrcz_ss: 366 case Intrinsic::x86_xop_vfrcz_sd: 367 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(1), 368 Name)); 369 CI->eraseFromParent(); 370 return; 371 372 case Intrinsic::x86_sse41_ptestc: 373 case Intrinsic::x86_sse41_ptestz: 374 case Intrinsic::x86_sse41_ptestnzc: { 375 // The arguments for these intrinsics used to be v4f32, and changed 376 // to v2i64. This is purely a nop, since those are bitwise intrinsics. 377 // So, the only thing required is a bitcast for both arguments. 378 // First, check the arguments have the old type. 379 Value *Arg0 = CI->getArgOperand(0); 380 if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4)) 381 return; 382 383 // Old intrinsic, add bitcasts 384 Value *Arg1 = CI->getArgOperand(1); 385 386 Value *BC0 = 387 Builder.CreateBitCast(Arg0, 388 VectorType::get(Type::getInt64Ty(C), 2), 389 "cast"); 390 Value *BC1 = 391 Builder.CreateBitCast(Arg1, 392 VectorType::get(Type::getInt64Ty(C), 2), 393 "cast"); 394 395 CallInst* NewCall = Builder.CreateCall2(NewFn, BC0, BC1, Name); 396 CI->replaceAllUsesWith(NewCall); 397 CI->eraseFromParent(); 398 return; 399 } 400 } 401 } 402 403 // This tests each Function to determine if it needs upgrading. When we find 404 // one we are interested in, we then upgrade all calls to reflect the new 405 // function. 406 void llvm::UpgradeCallsToIntrinsic(Function* F) { 407 assert(F && "Illegal attempt to upgrade a non-existent intrinsic."); 408 409 // Upgrade the function and check if it is a totaly new function. 410 Function *NewFn; 411 if (UpgradeIntrinsicFunction(F, NewFn)) { 412 if (NewFn != F) { 413 // Replace all uses to the old function with the new one if necessary. 414 for (Value::user_iterator UI = F->user_begin(), UE = F->user_end(); 415 UI != UE; ) { 416 if (CallInst *CI = dyn_cast<CallInst>(*UI++)) 417 UpgradeIntrinsicCall(CI, NewFn); 418 } 419 // Remove old function, no longer used, from the module. 420 F->eraseFromParent(); 421 } 422 } 423 } 424 425 void llvm::UpgradeInstWithTBAATag(Instruction *I) { 426 MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa); 427 assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag"); 428 // Check if the tag uses struct-path aware TBAA format. 429 if (isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3) 430 return; 431 432 if (MD->getNumOperands() == 3) { 433 Value *Elts[] = { 434 MD->getOperand(0), 435 MD->getOperand(1) 436 }; 437 MDNode *ScalarType = MDNode::get(I->getContext(), Elts); 438 // Create a MDNode <ScalarType, ScalarType, offset 0, const> 439 Value *Elts2[] = { 440 ScalarType, ScalarType, 441 Constant::getNullValue(Type::getInt64Ty(I->getContext())), 442 MD->getOperand(2) 443 }; 444 I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2)); 445 } else { 446 // Create a MDNode <MD, MD, offset 0> 447 Value *Elts[] = {MD, MD, 448 Constant::getNullValue(Type::getInt64Ty(I->getContext()))}; 449 I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts)); 450 } 451 } 452 453 Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy, 454 Instruction *&Temp) { 455 if (Opc != Instruction::BitCast) 456 return 0; 457 458 Temp = 0; 459 Type *SrcTy = V->getType(); 460 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() && 461 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) { 462 LLVMContext &Context = V->getContext(); 463 464 // We have no information about target data layout, so we assume that 465 // the maximum pointer size is 64bit. 466 Type *MidTy = Type::getInt64Ty(Context); 467 Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy); 468 469 return CastInst::Create(Instruction::IntToPtr, Temp, DestTy); 470 } 471 472 return 0; 473 } 474 475 Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) { 476 if (Opc != Instruction::BitCast) 477 return 0; 478 479 Type *SrcTy = C->getType(); 480 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() && 481 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) { 482 LLVMContext &Context = C->getContext(); 483 484 // We have no information about target data layout, so we assume that 485 // the maximum pointer size is 64bit. 486 Type *MidTy = Type::getInt64Ty(Context); 487 488 return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy), 489 DestTy); 490 } 491 492 return 0; 493 } 494 495 /// Check the debug info version number, if it is out-dated, drop the debug 496 /// info. Return true if module is modified. 497 bool llvm::UpgradeDebugInfo(Module &M) { 498 unsigned Version = getDebugMetadataVersionFromModule(M); 499 if (Version == DEBUG_METADATA_VERSION) 500 return false; 501 502 bool RetCode = StripDebugInfo(M); 503 if (RetCode) { 504 DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version); 505 M.getContext().diagnose(DiagVersion); 506 } 507 return RetCode; 508 } 509