LLVM API Documentation

AutoUpgrade.cpp
Go to the documentation of this file.
00001 //===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the auto-upgrade helper functions
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/IR/AutoUpgrade.h"
00015 #include "llvm/IR/CFG.h"
00016 #include "llvm/IR/CallSite.h"
00017 #include "llvm/IR/Constants.h"
00018 #include "llvm/IR/DebugInfo.h"
00019 #include "llvm/IR/DiagnosticInfo.h"
00020 #include "llvm/IR/Function.h"
00021 #include "llvm/IR/IRBuilder.h"
00022 #include "llvm/IR/Instruction.h"
00023 #include "llvm/IR/IntrinsicInst.h"
00024 #include "llvm/IR/LLVMContext.h"
00025 #include "llvm/IR/Module.h"
00026 #include "llvm/Support/ErrorHandling.h"
00027 #include <cstring>
00028 using namespace llvm;
00029 
00030 // Upgrade the declarations of the SSE4.1 functions whose arguments have
00031 // changed their type from v4f32 to v2i64.
00032 static bool UpgradeSSE41Function(Function* F, Intrinsic::ID IID,
00033                                  Function *&NewFn) {
00034   // Check whether this is an old version of the function, which received
00035   // v4f32 arguments.
00036   Type *Arg0Type = F->getFunctionType()->getParamType(0);
00037   if (Arg0Type != VectorType::get(Type::getFloatTy(F->getContext()), 4))
00038     return false;
00039 
00040   // Yes, it's old, replace it with new version.
00041   F->setName(F->getName() + ".old");
00042   NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
00043   return true;
00044 }
00045 
00046 static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
00047   assert(F && "Illegal to upgrade a non-existent Function.");
00048 
00049   // Quickly eliminate it, if it's not a candidate.
00050   StringRef Name = F->getName();
00051   if (Name.size() <= 8 || !Name.startswith("llvm."))
00052     return false;
00053   Name = Name.substr(5); // Strip off "llvm."
00054 
00055   switch (Name[0]) {
00056   default: break;
00057   case 'a': {
00058     if (Name.startswith("arm.neon.vclz")) {
00059       Type* args[2] = {
00060         F->arg_begin()->getType(),
00061         Type::getInt1Ty(F->getContext())
00062       };
00063       // Can't use Intrinsic::getDeclaration here as it adds a ".i1" to
00064       // the end of the name. Change name from llvm.arm.neon.vclz.* to
00065       //  llvm.ctlz.*
00066       FunctionType* fType = FunctionType::get(F->getReturnType(), args, false);
00067       NewFn = Function::Create(fType, F->getLinkage(),
00068                                "llvm.ctlz." + Name.substr(14), F->getParent());
00069       return true;
00070     }
00071     if (Name.startswith("arm.neon.vcnt")) {
00072       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctpop,
00073                                         F->arg_begin()->getType());
00074       return true;
00075     }
00076     break;
00077   }
00078   case 'c': {
00079     if (Name.startswith("ctlz.") && F->arg_size() == 1) {
00080       F->setName(Name + ".old");
00081       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
00082                                         F->arg_begin()->getType());
00083       return true;
00084     }
00085     if (Name.startswith("cttz.") && F->arg_size() == 1) {
00086       F->setName(Name + ".old");
00087       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz,
00088                                         F->arg_begin()->getType());
00089       return true;
00090     }
00091     break;
00092   }
00093   case 'o':
00094     // We only need to change the name to match the mangling including the
00095     // address space.
00096     if (F->arg_size() == 2 && Name.startswith("objectsize.")) {
00097       Type *Tys[2] = { F->getReturnType(), F->arg_begin()->getType() };
00098       if (F->getName() != Intrinsic::getName(Intrinsic::objectsize, Tys)) {
00099         F->setName(Name + ".old");
00100         NewFn = Intrinsic::getDeclaration(F->getParent(),
00101                                           Intrinsic::objectsize, Tys);
00102         return true;
00103       }
00104     }
00105     break;
00106 
00107   case 'x': {
00108     if (Name.startswith("x86.sse2.pcmpeq.") ||
00109         Name.startswith("x86.sse2.pcmpgt.") ||
00110         Name.startswith("x86.avx2.pcmpeq.") ||
00111         Name.startswith("x86.avx2.pcmpgt.") ||
00112         Name.startswith("x86.avx.vpermil.") ||
00113         Name == "x86.avx.movnt.dq.256" ||
00114         Name == "x86.avx.movnt.pd.256" ||
00115         Name == "x86.avx.movnt.ps.256" ||
00116         Name == "x86.sse42.crc32.64.8" ||
00117         (Name.startswith("x86.xop.vpcom") && F->arg_size() == 2)) {
00118       NewFn = nullptr;
00119       return true;
00120     }
00121     // SSE4.1 ptest functions may have an old signature.
00122     if (Name.startswith("x86.sse41.ptest")) {
00123       if (Name == "x86.sse41.ptestc")
00124         return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn);
00125       if (Name == "x86.sse41.ptestz")
00126         return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn);
00127       if (Name == "x86.sse41.ptestnzc")
00128         return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn);
00129     }
00130     // frcz.ss/sd may need to have an argument dropped
00131     if (Name.startswith("x86.xop.vfrcz.ss") && F->arg_size() == 2) {
00132       F->setName(Name + ".old");
00133       NewFn = Intrinsic::getDeclaration(F->getParent(),
00134                                         Intrinsic::x86_xop_vfrcz_ss);
00135       return true;
00136     }
00137     if (Name.startswith("x86.xop.vfrcz.sd") && F->arg_size() == 2) {
00138       F->setName(Name + ".old");
00139       NewFn = Intrinsic::getDeclaration(F->getParent(),
00140                                         Intrinsic::x86_xop_vfrcz_sd);
00141       return true;
00142     }
00143     // Fix the FMA4 intrinsics to remove the 4
00144     if (Name.startswith("x86.fma4.")) {
00145       F->setName("llvm.x86.fma" + Name.substr(8));
00146       NewFn = F;
00147       return true;
00148     }
00149     break;
00150   }
00151   }
00152 
00153   //  This may not belong here. This function is effectively being overloaded
00154   //  to both detect an intrinsic which needs upgrading, and to provide the
00155   //  upgraded form of the intrinsic. We should perhaps have two separate
00156   //  functions for this.
00157   return false;
00158 }
00159 
00160 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
00161   NewFn = nullptr;
00162   bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
00163 
00164   // Upgrade intrinsic attributes.  This does not change the function.
00165   if (NewFn)
00166     F = NewFn;
00167   if (unsigned id = F->getIntrinsicID())
00168     F->setAttributes(Intrinsic::getAttributes(F->getContext(),
00169                                               (Intrinsic::ID)id));
00170   return Upgraded;
00171 }
00172 
00173 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
00174   // Nothing to do yet.
00175   return false;
00176 }
00177 
00178 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
00179 // upgraded intrinsic. All argument and return casting must be provided in
00180 // order to seamlessly integrate with existing context.
00181 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
00182   Function *F = CI->getCalledFunction();
00183   LLVMContext &C = CI->getContext();
00184   IRBuilder<> Builder(C);
00185   Builder.SetInsertPoint(CI->getParent(), CI);
00186 
00187   assert(F && "Intrinsic call is not direct?");
00188 
00189   if (!NewFn) {
00190     // Get the Function's name.
00191     StringRef Name = F->getName();
00192 
00193     Value *Rep;
00194     // Upgrade packed integer vector compares intrinsics to compare instructions
00195     if (Name.startswith("llvm.x86.sse2.pcmpeq.") ||
00196         Name.startswith("llvm.x86.avx2.pcmpeq.")) {
00197       Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
00198                                  "pcmpeq");
00199       // need to sign extend since icmp returns vector of i1
00200       Rep = Builder.CreateSExt(Rep, CI->getType(), "");
00201     } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") ||
00202                Name.startswith("llvm.x86.avx2.pcmpgt.")) {
00203       Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
00204                                   "pcmpgt");
00205       // need to sign extend since icmp returns vector of i1
00206       Rep = Builder.CreateSExt(Rep, CI->getType(), "");
00207     } else if (Name == "llvm.x86.avx.movnt.dq.256" ||
00208                Name == "llvm.x86.avx.movnt.ps.256" ||
00209                Name == "llvm.x86.avx.movnt.pd.256") {
00210       IRBuilder<> Builder(C);
00211       Builder.SetInsertPoint(CI->getParent(), CI);
00212 
00213       Module *M = F->getParent();
00214       SmallVector<Value *, 1> Elts;
00215       Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
00216       MDNode *Node = MDNode::get(C, Elts);
00217 
00218       Value *Arg0 = CI->getArgOperand(0);
00219       Value *Arg1 = CI->getArgOperand(1);
00220 
00221       // Convert the type of the pointer to a pointer to the stored type.
00222       Value *BC = Builder.CreateBitCast(Arg0,
00223                                         PointerType::getUnqual(Arg1->getType()),
00224                                         "cast");
00225       StoreInst *SI = Builder.CreateStore(Arg1, BC);
00226       SI->setMetadata(M->getMDKindID("nontemporal"), Node);
00227       SI->setAlignment(16);
00228 
00229       // Remove intrinsic.
00230       CI->eraseFromParent();
00231       return;
00232     } else if (Name.startswith("llvm.x86.xop.vpcom")) {
00233       Intrinsic::ID intID;
00234       if (Name.endswith("ub"))
00235         intID = Intrinsic::x86_xop_vpcomub;
00236       else if (Name.endswith("uw"))
00237         intID = Intrinsic::x86_xop_vpcomuw;
00238       else if (Name.endswith("ud"))
00239         intID = Intrinsic::x86_xop_vpcomud;
00240       else if (Name.endswith("uq"))
00241         intID = Intrinsic::x86_xop_vpcomuq;
00242       else if (Name.endswith("b"))
00243         intID = Intrinsic::x86_xop_vpcomb;
00244       else if (Name.endswith("w"))
00245         intID = Intrinsic::x86_xop_vpcomw;
00246       else if (Name.endswith("d"))
00247         intID = Intrinsic::x86_xop_vpcomd;
00248       else if (Name.endswith("q"))
00249         intID = Intrinsic::x86_xop_vpcomq;
00250       else
00251         llvm_unreachable("Unknown suffix");
00252 
00253       Name = Name.substr(18); // strip off "llvm.x86.xop.vpcom"
00254       unsigned Imm;
00255       if (Name.startswith("lt"))
00256         Imm = 0;
00257       else if (Name.startswith("le"))
00258         Imm = 1;
00259       else if (Name.startswith("gt"))
00260         Imm = 2;
00261       else if (Name.startswith("ge"))
00262         Imm = 3;
00263       else if (Name.startswith("eq"))
00264         Imm = 4;
00265       else if (Name.startswith("ne"))
00266         Imm = 5;
00267       else if (Name.startswith("true"))
00268         Imm = 6;
00269       else if (Name.startswith("false"))
00270         Imm = 7;
00271       else
00272         llvm_unreachable("Unknown condition");
00273 
00274       Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
00275       Rep = Builder.CreateCall3(VPCOM, CI->getArgOperand(0),
00276                                 CI->getArgOperand(1), Builder.getInt8(Imm));
00277     } else if (Name == "llvm.x86.sse42.crc32.64.8") {
00278       Function *CRC32 = Intrinsic::getDeclaration(F->getParent(),
00279                                                Intrinsic::x86_sse42_crc32_32_8);
00280       Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C));
00281       Rep = Builder.CreateCall2(CRC32, Trunc0, CI->getArgOperand(1));
00282       Rep = Builder.CreateZExt(Rep, CI->getType(), "");
00283     } else {
00284       bool PD128 = false, PD256 = false, PS128 = false, PS256 = false;
00285       if (Name == "llvm.x86.avx.vpermil.pd.256")
00286         PD256 = true;
00287       else if (Name == "llvm.x86.avx.vpermil.pd")
00288         PD128 = true;
00289       else if (Name == "llvm.x86.avx.vpermil.ps.256")
00290         PS256 = true;
00291       else if (Name == "llvm.x86.avx.vpermil.ps")
00292         PS128 = true;
00293 
00294       if (PD256 || PD128 || PS256 || PS128) {
00295         Value *Op0 = CI->getArgOperand(0);
00296         unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
00297         SmallVector<Constant*, 8> Idxs;
00298 
00299         if (PD128)
00300           for (unsigned i = 0; i != 2; ++i)
00301             Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1));
00302         else if (PD256)
00303           for (unsigned l = 0; l != 4; l+=2)
00304             for (unsigned i = 0; i != 2; ++i)
00305               Idxs.push_back(Builder.getInt32(((Imm >> (l+i)) & 0x1) + l));
00306         else if (PS128)
00307           for (unsigned i = 0; i != 4; ++i)
00308             Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3));
00309         else if (PS256)
00310           for (unsigned l = 0; l != 8; l+=4)
00311             for (unsigned i = 0; i != 4; ++i)
00312               Idxs.push_back(Builder.getInt32(((Imm >> (2 * i)) & 0x3) + l));
00313         else
00314           llvm_unreachable("Unexpected function");
00315 
00316         Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs));
00317       } else {
00318         llvm_unreachable("Unknown function for CallInst upgrade.");
00319       }
00320     }
00321 
00322     CI->replaceAllUsesWith(Rep);
00323     CI->eraseFromParent();
00324     return;
00325   }
00326 
00327   std::string Name = CI->getName().str();
00328   CI->setName(Name + ".old");
00329 
00330   switch (NewFn->getIntrinsicID()) {
00331   default:
00332     llvm_unreachable("Unknown function for CallInst upgrade.");
00333 
00334   case Intrinsic::ctlz:
00335   case Intrinsic::cttz:
00336     assert(CI->getNumArgOperands() == 1 &&
00337            "Mismatch between function args and call args");
00338     CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
00339                                                Builder.getFalse(), Name));
00340     CI->eraseFromParent();
00341     return;
00342 
00343   case Intrinsic::objectsize:
00344     CI->replaceAllUsesWith(Builder.CreateCall2(NewFn,
00345                                                CI->getArgOperand(0),
00346                                                CI->getArgOperand(1),
00347                                                Name));
00348     CI->eraseFromParent();
00349     return;
00350 
00351   case Intrinsic::arm_neon_vclz: {
00352     // Change name from llvm.arm.neon.vclz.* to llvm.ctlz.*
00353     CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
00354                                                Builder.getFalse(),
00355                                                "llvm.ctlz." + Name.substr(14)));
00356     CI->eraseFromParent();
00357     return;
00358   }
00359   case Intrinsic::ctpop: {
00360     CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(0)));
00361     CI->eraseFromParent();
00362     return;
00363   }
00364 
00365   case Intrinsic::x86_xop_vfrcz_ss:
00366   case Intrinsic::x86_xop_vfrcz_sd:
00367     CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(1),
00368                                               Name));
00369     CI->eraseFromParent();
00370     return;
00371 
00372   case Intrinsic::x86_sse41_ptestc:
00373   case Intrinsic::x86_sse41_ptestz:
00374   case Intrinsic::x86_sse41_ptestnzc: {
00375     // The arguments for these intrinsics used to be v4f32, and changed
00376     // to v2i64. This is purely a nop, since those are bitwise intrinsics.
00377     // So, the only thing required is a bitcast for both arguments.
00378     // First, check the arguments have the old type.
00379     Value *Arg0 = CI->getArgOperand(0);
00380     if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4))
00381       return;
00382 
00383     // Old intrinsic, add bitcasts
00384     Value *Arg1 = CI->getArgOperand(1);
00385 
00386     Value *BC0 =
00387       Builder.CreateBitCast(Arg0,
00388                             VectorType::get(Type::getInt64Ty(C), 2),
00389                             "cast");
00390     Value *BC1 =
00391       Builder.CreateBitCast(Arg1,
00392                             VectorType::get(Type::getInt64Ty(C), 2),
00393                             "cast");
00394 
00395     CallInst* NewCall = Builder.CreateCall2(NewFn, BC0, BC1, Name);
00396     CI->replaceAllUsesWith(NewCall);
00397     CI->eraseFromParent();
00398     return;
00399   }
00400   }
00401 }
00402 
00403 // This tests each Function to determine if it needs upgrading. When we find
00404 // one we are interested in, we then upgrade all calls to reflect the new
00405 // function.
00406 void llvm::UpgradeCallsToIntrinsic(Function* F) {
00407   assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
00408 
00409   // Upgrade the function and check if it is a totaly new function.
00410   Function *NewFn;
00411   if (UpgradeIntrinsicFunction(F, NewFn)) {
00412     if (NewFn != F) {
00413       // Replace all uses to the old function with the new one if necessary.
00414       for (Value::user_iterator UI = F->user_begin(), UE = F->user_end();
00415            UI != UE; ) {
00416         if (CallInst *CI = dyn_cast<CallInst>(*UI++))
00417           UpgradeIntrinsicCall(CI, NewFn);
00418       }
00419       // Remove old function, no longer used, from the module.
00420       F->eraseFromParent();
00421     }
00422   }
00423 }
00424 
00425 void llvm::UpgradeInstWithTBAATag(Instruction *I) {
00426   MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa);
00427   assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
00428   // Check if the tag uses struct-path aware TBAA format.
00429   if (isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3)
00430     return;
00431 
00432   if (MD->getNumOperands() == 3) {
00433     Value *Elts[] = {
00434       MD->getOperand(0),
00435       MD->getOperand(1)
00436     };
00437     MDNode *ScalarType = MDNode::get(I->getContext(), Elts);
00438     // Create a MDNode <ScalarType, ScalarType, offset 0, const>
00439     Value *Elts2[] = {
00440       ScalarType, ScalarType,
00441       Constant::getNullValue(Type::getInt64Ty(I->getContext())),
00442       MD->getOperand(2)
00443     };
00444     I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2));
00445   } else {
00446     // Create a MDNode <MD, MD, offset 0>
00447     Value *Elts[] = {MD, MD,
00448       Constant::getNullValue(Type::getInt64Ty(I->getContext()))};
00449     I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts));
00450   }
00451 }
00452 
00453 Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,
00454                                       Instruction *&Temp) {
00455   if (Opc != Instruction::BitCast)
00456     return nullptr;
00457 
00458   Temp = nullptr;
00459   Type *SrcTy = V->getType();
00460   if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
00461       SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
00462     LLVMContext &Context = V->getContext();
00463 
00464     // We have no information about target data layout, so we assume that
00465     // the maximum pointer size is 64bit.
00466     Type *MidTy = Type::getInt64Ty(Context);
00467     Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy);
00468 
00469     return CastInst::Create(Instruction::IntToPtr, Temp, DestTy);
00470   }
00471 
00472   return nullptr;
00473 }
00474 
00475 Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) {
00476   if (Opc != Instruction::BitCast)
00477     return nullptr;
00478 
00479   Type *SrcTy = C->getType();
00480   if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
00481       SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
00482     LLVMContext &Context = C->getContext();
00483 
00484     // We have no information about target data layout, so we assume that
00485     // the maximum pointer size is 64bit.
00486     Type *MidTy = Type::getInt64Ty(Context);
00487 
00488     return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy),
00489                                      DestTy);
00490   }
00491 
00492   return nullptr;
00493 }
00494 
00495 /// Check the debug info version number, if it is out-dated, drop the debug
00496 /// info. Return true if module is modified.
00497 bool llvm::UpgradeDebugInfo(Module &M) {
00498   unsigned Version = getDebugMetadataVersionFromModule(M);
00499   if (Version == DEBUG_METADATA_VERSION)
00500     return false;
00501 
00502   bool RetCode = StripDebugInfo(M);
00503   if (RetCode) {
00504     DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version);
00505     M.getContext().diagnose(DiagVersion);
00506   }
00507   return RetCode;
00508 }