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/DIBuilder.h"
00021 #include "llvm/IR/Function.h"
00022 #include "llvm/IR/IRBuilder.h"
00023 #include "llvm/IR/Instruction.h"
00024 #include "llvm/IR/IntrinsicInst.h"
00025 #include "llvm/IR/LLVMContext.h"
00026 #include "llvm/IR/Module.h"
00027 #include "llvm/Support/ErrorHandling.h"
00028 #include <cstring>
00029 using namespace llvm;
00030 
00031 // Upgrade the declarations of the SSE4.1 functions whose arguments have
00032 // changed their type from v4f32 to v2i64.
00033 static bool UpgradeSSE41Function(Function* F, Intrinsic::ID IID,
00034                                  Function *&NewFn) {
00035   // Check whether this is an old version of the function, which received
00036   // v4f32 arguments.
00037   Type *Arg0Type = F->getFunctionType()->getParamType(0);
00038   if (Arg0Type != VectorType::get(Type::getFloatTy(F->getContext()), 4))
00039     return false;
00040 
00041   // Yes, it's old, replace it with new version.
00042   F->setName(F->getName() + ".old");
00043   NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
00044   return true;
00045 }
00046 
00047 // Upgrade the declarations of intrinsic functions whose 8-bit immediate mask
00048 // arguments have changed their type from i32 to i8.
00049 static bool UpgradeX86IntrinsicsWith8BitMask(Function *F, Intrinsic::ID IID,
00050                                              Function *&NewFn) {
00051   // Check that the last argument is an i32.
00052   Type *LastArgType = F->getFunctionType()->getParamType(
00053      F->getFunctionType()->getNumParams() - 1);
00054   if (!LastArgType->isIntegerTy(32))
00055     return false;
00056 
00057   // Move this function aside and map down.
00058   F->setName(F->getName() + ".old");
00059   NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
00060   return true;
00061 }
00062 
00063 static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
00064   assert(F && "Illegal to upgrade a non-existent Function.");
00065 
00066   // Quickly eliminate it, if it's not a candidate.
00067   StringRef Name = F->getName();
00068   if (Name.size() <= 8 || !Name.startswith("llvm."))
00069     return false;
00070   Name = Name.substr(5); // Strip off "llvm."
00071 
00072   switch (Name[0]) {
00073   default: break;
00074   case 'a': {
00075     if (Name.startswith("arm.neon.vclz")) {
00076       Type* args[2] = {
00077         F->arg_begin()->getType(),
00078         Type::getInt1Ty(F->getContext())
00079       };
00080       // Can't use Intrinsic::getDeclaration here as it adds a ".i1" to
00081       // the end of the name. Change name from llvm.arm.neon.vclz.* to
00082       //  llvm.ctlz.*
00083       FunctionType* fType = FunctionType::get(F->getReturnType(), args, false);
00084       NewFn = Function::Create(fType, F->getLinkage(),
00085                                "llvm.ctlz." + Name.substr(14), F->getParent());
00086       return true;
00087     }
00088     if (Name.startswith("arm.neon.vcnt")) {
00089       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctpop,
00090                                         F->arg_begin()->getType());
00091       return true;
00092     }
00093     break;
00094   }
00095   case 'c': {
00096     if (Name.startswith("ctlz.") && F->arg_size() == 1) {
00097       F->setName(Name + ".old");
00098       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
00099                                         F->arg_begin()->getType());
00100       return true;
00101     }
00102     if (Name.startswith("cttz.") && F->arg_size() == 1) {
00103       F->setName(Name + ".old");
00104       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz,
00105                                         F->arg_begin()->getType());
00106       return true;
00107     }
00108     break;
00109   }
00110   case 'd': {
00111     if (Name.startswith("dbg.declare") && F->arg_size() == 2) {
00112       F->setName(Name + ".old");
00113       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::dbg_declare);
00114       return true;
00115     }
00116     if (Name.startswith("dbg.value") && F->arg_size() == 3) {
00117       F->setName(Name + ".old");
00118       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::dbg_value);
00119       return true;
00120     }
00121     break;
00122   }
00123 
00124   case 'o':
00125     // We only need to change the name to match the mangling including the
00126     // address space.
00127     if (F->arg_size() == 2 && Name.startswith("objectsize.")) {
00128       Type *Tys[2] = { F->getReturnType(), F->arg_begin()->getType() };
00129       if (F->getName() != Intrinsic::getName(Intrinsic::objectsize, Tys)) {
00130         F->setName(Name + ".old");
00131         NewFn = Intrinsic::getDeclaration(F->getParent(),
00132                                           Intrinsic::objectsize, Tys);
00133         return true;
00134       }
00135     }
00136     break;
00137 
00138   case 'x': {
00139     if (Name.startswith("x86.sse2.pcmpeq.") ||
00140         Name.startswith("x86.sse2.pcmpgt.") ||
00141         Name.startswith("x86.avx2.pcmpeq.") ||
00142         Name.startswith("x86.avx2.pcmpgt.") ||
00143         Name.startswith("x86.avx.vpermil.") ||
00144         Name == "x86.avx.movnt.dq.256" ||
00145         Name == "x86.avx.movnt.pd.256" ||
00146         Name == "x86.avx.movnt.ps.256" ||
00147         Name == "x86.sse42.crc32.64.8" ||
00148         Name == "x86.avx.vbroadcast.ss" ||
00149         Name == "x86.avx.vbroadcast.ss.256" ||
00150         Name == "x86.avx.vbroadcast.sd.256" ||
00151         (Name.startswith("x86.xop.vpcom") && F->arg_size() == 2)) {
00152       NewFn = nullptr;
00153       return true;
00154     }
00155     // SSE4.1 ptest functions may have an old signature.
00156     if (Name.startswith("x86.sse41.ptest")) {
00157       if (Name == "x86.sse41.ptestc")
00158         return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn);
00159       if (Name == "x86.sse41.ptestz")
00160         return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn);
00161       if (Name == "x86.sse41.ptestnzc")
00162         return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn);
00163     }
00164     // Several blend and other instructions with maskes used the wrong number of
00165     // bits.
00166     if (Name == "x86.sse41.pblendw")
00167       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_pblendw,
00168                                               NewFn);
00169     if (Name == "x86.sse41.blendpd")
00170       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_blendpd,
00171                                               NewFn);
00172     if (Name == "x86.sse41.blendps")
00173       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_blendps,
00174                                               NewFn);
00175     if (Name == "x86.sse41.insertps")
00176       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_insertps,
00177                                               NewFn);
00178     if (Name == "x86.sse41.dppd")
00179       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dppd,
00180                                               NewFn);
00181     if (Name == "x86.sse41.dpps")
00182       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dpps,
00183                                               NewFn);
00184     if (Name == "x86.sse41.mpsadbw")
00185       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_mpsadbw,
00186                                               NewFn);
00187     if (Name == "x86.avx.blend.pd.256")
00188       return UpgradeX86IntrinsicsWith8BitMask(
00189           F, Intrinsic::x86_avx_blend_pd_256, NewFn);
00190     if (Name == "x86.avx.blend.ps.256")
00191       return UpgradeX86IntrinsicsWith8BitMask(
00192           F, Intrinsic::x86_avx_blend_ps_256, NewFn);
00193     if (Name == "x86.avx.dp.ps.256")
00194       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx_dp_ps_256,
00195                                               NewFn);
00196     if (Name == "x86.avx2.pblendw")
00197       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_pblendw,
00198                                               NewFn);
00199     if (Name == "x86.avx2.pblendd.128")
00200       return UpgradeX86IntrinsicsWith8BitMask(
00201           F, Intrinsic::x86_avx2_pblendd_128, NewFn);
00202     if (Name == "x86.avx2.pblendd.256")
00203       return UpgradeX86IntrinsicsWith8BitMask(
00204           F, Intrinsic::x86_avx2_pblendd_256, NewFn);
00205     if (Name == "x86.avx2.mpsadbw")
00206       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_mpsadbw,
00207                                               NewFn);
00208 
00209     // frcz.ss/sd may need to have an argument dropped
00210     if (Name.startswith("x86.xop.vfrcz.ss") && F->arg_size() == 2) {
00211       F->setName(Name + ".old");
00212       NewFn = Intrinsic::getDeclaration(F->getParent(),
00213                                         Intrinsic::x86_xop_vfrcz_ss);
00214       return true;
00215     }
00216     if (Name.startswith("x86.xop.vfrcz.sd") && F->arg_size() == 2) {
00217       F->setName(Name + ".old");
00218       NewFn = Intrinsic::getDeclaration(F->getParent(),
00219                                         Intrinsic::x86_xop_vfrcz_sd);
00220       return true;
00221     }
00222     // Fix the FMA4 intrinsics to remove the 4
00223     if (Name.startswith("x86.fma4.")) {
00224       F->setName("llvm.x86.fma" + Name.substr(8));
00225       NewFn = F;
00226       return true;
00227     }
00228     break;
00229   }
00230   }
00231 
00232   //  This may not belong here. This function is effectively being overloaded
00233   //  to both detect an intrinsic which needs upgrading, and to provide the
00234   //  upgraded form of the intrinsic. We should perhaps have two separate
00235   //  functions for this.
00236   return false;
00237 }
00238 
00239 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
00240   NewFn = nullptr;
00241   bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
00242 
00243   // Upgrade intrinsic attributes.  This does not change the function.
00244   if (NewFn)
00245     F = NewFn;
00246   if (unsigned id = F->getIntrinsicID())
00247     F->setAttributes(Intrinsic::getAttributes(F->getContext(),
00248                                               (Intrinsic::ID)id));
00249   return Upgraded;
00250 }
00251 
00252 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
00253   // Nothing to do yet.
00254   return false;
00255 }
00256 
00257 static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
00258   if (!DbgNode || Elt >= DbgNode->getNumOperands())
00259     return nullptr;
00260   return dyn_cast_or_null<MDNode>(DbgNode->getOperand(Elt));
00261 }
00262 
00263 static DIExpression getExpression(Value *VarOperand, Function *F) {
00264   // Old-style DIVariables have an optional expression as the 8th element.
00265   DIExpression Expr(getNodeField(cast<MDNode>(VarOperand), 8));
00266   if (!Expr) {
00267     DIBuilder DIB(*F->getParent());
00268     Expr = DIB.createExpression();
00269   }
00270   return Expr;
00271 }
00272 
00273 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
00274 // upgraded intrinsic. All argument and return casting must be provided in
00275 // order to seamlessly integrate with existing context.
00276 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
00277   Function *F = CI->getCalledFunction();
00278   LLVMContext &C = CI->getContext();
00279   IRBuilder<> Builder(C);
00280   Builder.SetInsertPoint(CI->getParent(), CI);
00281 
00282   assert(F && "Intrinsic call is not direct?");
00283 
00284   if (!NewFn) {
00285     // Get the Function's name.
00286     StringRef Name = F->getName();
00287 
00288     Value *Rep;
00289     // Upgrade packed integer vector compares intrinsics to compare instructions
00290     if (Name.startswith("llvm.x86.sse2.pcmpeq.") ||
00291         Name.startswith("llvm.x86.avx2.pcmpeq.")) {
00292       Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
00293                                  "pcmpeq");
00294       // need to sign extend since icmp returns vector of i1
00295       Rep = Builder.CreateSExt(Rep, CI->getType(), "");
00296     } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") ||
00297                Name.startswith("llvm.x86.avx2.pcmpgt.")) {
00298       Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
00299                                   "pcmpgt");
00300       // need to sign extend since icmp returns vector of i1
00301       Rep = Builder.CreateSExt(Rep, CI->getType(), "");
00302     } else if (Name == "llvm.x86.avx.movnt.dq.256" ||
00303                Name == "llvm.x86.avx.movnt.ps.256" ||
00304                Name == "llvm.x86.avx.movnt.pd.256") {
00305       IRBuilder<> Builder(C);
00306       Builder.SetInsertPoint(CI->getParent(), CI);
00307 
00308       Module *M = F->getParent();
00309       SmallVector<Value *, 1> Elts;
00310       Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
00311       MDNode *Node = MDNode::get(C, Elts);
00312 
00313       Value *Arg0 = CI->getArgOperand(0);
00314       Value *Arg1 = CI->getArgOperand(1);
00315 
00316       // Convert the type of the pointer to a pointer to the stored type.
00317       Value *BC = Builder.CreateBitCast(Arg0,
00318                                         PointerType::getUnqual(Arg1->getType()),
00319                                         "cast");
00320       StoreInst *SI = Builder.CreateStore(Arg1, BC);
00321       SI->setMetadata(M->getMDKindID("nontemporal"), Node);
00322       SI->setAlignment(16);
00323 
00324       // Remove intrinsic.
00325       CI->eraseFromParent();
00326       return;
00327     } else if (Name.startswith("llvm.x86.xop.vpcom")) {
00328       Intrinsic::ID intID;
00329       if (Name.endswith("ub"))
00330         intID = Intrinsic::x86_xop_vpcomub;
00331       else if (Name.endswith("uw"))
00332         intID = Intrinsic::x86_xop_vpcomuw;
00333       else if (Name.endswith("ud"))
00334         intID = Intrinsic::x86_xop_vpcomud;
00335       else if (Name.endswith("uq"))
00336         intID = Intrinsic::x86_xop_vpcomuq;
00337       else if (Name.endswith("b"))
00338         intID = Intrinsic::x86_xop_vpcomb;
00339       else if (Name.endswith("w"))
00340         intID = Intrinsic::x86_xop_vpcomw;
00341       else if (Name.endswith("d"))
00342         intID = Intrinsic::x86_xop_vpcomd;
00343       else if (Name.endswith("q"))
00344         intID = Intrinsic::x86_xop_vpcomq;
00345       else
00346         llvm_unreachable("Unknown suffix");
00347 
00348       Name = Name.substr(18); // strip off "llvm.x86.xop.vpcom"
00349       unsigned Imm;
00350       if (Name.startswith("lt"))
00351         Imm = 0;
00352       else if (Name.startswith("le"))
00353         Imm = 1;
00354       else if (Name.startswith("gt"))
00355         Imm = 2;
00356       else if (Name.startswith("ge"))
00357         Imm = 3;
00358       else if (Name.startswith("eq"))
00359         Imm = 4;
00360       else if (Name.startswith("ne"))
00361         Imm = 5;
00362       else if (Name.startswith("true"))
00363         Imm = 6;
00364       else if (Name.startswith("false"))
00365         Imm = 7;
00366       else
00367         llvm_unreachable("Unknown condition");
00368 
00369       Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
00370       Rep = Builder.CreateCall3(VPCOM, CI->getArgOperand(0),
00371                                 CI->getArgOperand(1), Builder.getInt8(Imm));
00372     } else if (Name == "llvm.x86.sse42.crc32.64.8") {
00373       Function *CRC32 = Intrinsic::getDeclaration(F->getParent(),
00374                                                Intrinsic::x86_sse42_crc32_32_8);
00375       Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C));
00376       Rep = Builder.CreateCall2(CRC32, Trunc0, CI->getArgOperand(1));
00377       Rep = Builder.CreateZExt(Rep, CI->getType(), "");
00378     } else if (Name.startswith("llvm.x86.avx.vbroadcast")) {
00379       // Replace broadcasts with a series of insertelements.
00380       Type *VecTy = CI->getType();
00381       Type *EltTy = VecTy->getVectorElementType();
00382       unsigned EltNum = VecTy->getVectorNumElements();
00383       Value *Cast = Builder.CreateBitCast(CI->getArgOperand(0),
00384                                           EltTy->getPointerTo());
00385       Value *Load = Builder.CreateLoad(Cast);
00386       Type *I32Ty = Type::getInt32Ty(C);
00387       Rep = UndefValue::get(VecTy);
00388       for (unsigned I = 0; I < EltNum; ++I)
00389         Rep = Builder.CreateInsertElement(Rep, Load,
00390                                           ConstantInt::get(I32Ty, I));
00391     } else {
00392       bool PD128 = false, PD256 = false, PS128 = false, PS256 = false;
00393       if (Name == "llvm.x86.avx.vpermil.pd.256")
00394         PD256 = true;
00395       else if (Name == "llvm.x86.avx.vpermil.pd")
00396         PD128 = true;
00397       else if (Name == "llvm.x86.avx.vpermil.ps.256")
00398         PS256 = true;
00399       else if (Name == "llvm.x86.avx.vpermil.ps")
00400         PS128 = true;
00401 
00402       if (PD256 || PD128 || PS256 || PS128) {
00403         Value *Op0 = CI->getArgOperand(0);
00404         unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
00405         SmallVector<Constant*, 8> Idxs;
00406 
00407         if (PD128)
00408           for (unsigned i = 0; i != 2; ++i)
00409             Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1));
00410         else if (PD256)
00411           for (unsigned l = 0; l != 4; l+=2)
00412             for (unsigned i = 0; i != 2; ++i)
00413               Idxs.push_back(Builder.getInt32(((Imm >> (l+i)) & 0x1) + l));
00414         else if (PS128)
00415           for (unsigned i = 0; i != 4; ++i)
00416             Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3));
00417         else if (PS256)
00418           for (unsigned l = 0; l != 8; l+=4)
00419             for (unsigned i = 0; i != 4; ++i)
00420               Idxs.push_back(Builder.getInt32(((Imm >> (2 * i)) & 0x3) + l));
00421         else
00422           llvm_unreachable("Unexpected function");
00423 
00424         Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs));
00425       } else {
00426         llvm_unreachable("Unknown function for CallInst upgrade.");
00427       }
00428     }
00429 
00430     CI->replaceAllUsesWith(Rep);
00431     CI->eraseFromParent();
00432     return;
00433   }
00434 
00435   std::string Name = CI->getName().str();
00436   if (!Name.empty())
00437     CI->setName(Name + ".old");
00438 
00439   switch (NewFn->getIntrinsicID()) {
00440   default:
00441     llvm_unreachable("Unknown function for CallInst upgrade.");
00442 
00443   // Upgrade debug intrinsics to use an additional DIExpression argument.
00444   case Intrinsic::dbg_declare: {
00445     auto NewCI =
00446         Builder.CreateCall3(NewFn, CI->getArgOperand(0), CI->getArgOperand(1),
00447                             getExpression(CI->getArgOperand(1), F), Name);
00448     NewCI->setDebugLoc(CI->getDebugLoc());
00449     CI->replaceAllUsesWith(NewCI);
00450     CI->eraseFromParent();
00451     return;
00452   }
00453   case Intrinsic::dbg_value: {
00454     auto NewCI = Builder.CreateCall4(
00455         NewFn, CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2),
00456         getExpression(CI->getArgOperand(2), F), Name);
00457     NewCI->setDebugLoc(CI->getDebugLoc());
00458     CI->replaceAllUsesWith(NewCI);
00459     CI->eraseFromParent();
00460     return;
00461   }
00462   case Intrinsic::ctlz:
00463   case Intrinsic::cttz:
00464     assert(CI->getNumArgOperands() == 1 &&
00465            "Mismatch between function args and call args");
00466     CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
00467                                                Builder.getFalse(), Name));
00468     CI->eraseFromParent();
00469     return;
00470 
00471   case Intrinsic::objectsize:
00472     CI->replaceAllUsesWith(Builder.CreateCall2(NewFn,
00473                                                CI->getArgOperand(0),
00474                                                CI->getArgOperand(1),
00475                                                Name));
00476     CI->eraseFromParent();
00477     return;
00478 
00479   case Intrinsic::arm_neon_vclz: {
00480     // Change name from llvm.arm.neon.vclz.* to llvm.ctlz.*
00481     CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
00482                                                Builder.getFalse(),
00483                                                "llvm.ctlz." + Name.substr(14)));
00484     CI->eraseFromParent();
00485     return;
00486   }
00487   case Intrinsic::ctpop: {
00488     CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(0)));
00489     CI->eraseFromParent();
00490     return;
00491   }
00492 
00493   case Intrinsic::x86_xop_vfrcz_ss:
00494   case Intrinsic::x86_xop_vfrcz_sd:
00495     CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(1),
00496                                               Name));
00497     CI->eraseFromParent();
00498     return;
00499 
00500   case Intrinsic::x86_sse41_ptestc:
00501   case Intrinsic::x86_sse41_ptestz:
00502   case Intrinsic::x86_sse41_ptestnzc: {
00503     // The arguments for these intrinsics used to be v4f32, and changed
00504     // to v2i64. This is purely a nop, since those are bitwise intrinsics.
00505     // So, the only thing required is a bitcast for both arguments.
00506     // First, check the arguments have the old type.
00507     Value *Arg0 = CI->getArgOperand(0);
00508     if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4))
00509       return;
00510 
00511     // Old intrinsic, add bitcasts
00512     Value *Arg1 = CI->getArgOperand(1);
00513 
00514     Value *BC0 =
00515       Builder.CreateBitCast(Arg0,
00516                             VectorType::get(Type::getInt64Ty(C), 2),
00517                             "cast");
00518     Value *BC1 =
00519       Builder.CreateBitCast(Arg1,
00520                             VectorType::get(Type::getInt64Ty(C), 2),
00521                             "cast");
00522 
00523     CallInst* NewCall = Builder.CreateCall2(NewFn, BC0, BC1, Name);
00524     CI->replaceAllUsesWith(NewCall);
00525     CI->eraseFromParent();
00526     return;
00527   }
00528 
00529   case Intrinsic::x86_sse41_pblendw:
00530   case Intrinsic::x86_sse41_blendpd:
00531   case Intrinsic::x86_sse41_blendps:
00532   case Intrinsic::x86_sse41_insertps:
00533   case Intrinsic::x86_sse41_dppd:
00534   case Intrinsic::x86_sse41_dpps:
00535   case Intrinsic::x86_sse41_mpsadbw:
00536   case Intrinsic::x86_avx_blend_pd_256:
00537   case Intrinsic::x86_avx_blend_ps_256:
00538   case Intrinsic::x86_avx_dp_ps_256:
00539   case Intrinsic::x86_avx2_pblendw:
00540   case Intrinsic::x86_avx2_pblendd_128:
00541   case Intrinsic::x86_avx2_pblendd_256:
00542   case Intrinsic::x86_avx2_mpsadbw: {
00543     // Need to truncate the last argument from i32 to i8 -- this argument models
00544     // an inherently 8-bit immediate operand to these x86 instructions.
00545     SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
00546                                  CI->arg_operands().end());
00547 
00548     // Replace the last argument with a trunc.
00549     Args.back() = Builder.CreateTrunc(Args.back(), Type::getInt8Ty(C), "trunc");
00550 
00551     CallInst *NewCall = Builder.CreateCall(NewFn, Args);
00552     CI->replaceAllUsesWith(NewCall);
00553     CI->eraseFromParent();
00554     return;
00555   }
00556   }
00557 }
00558 
00559 // This tests each Function to determine if it needs upgrading. When we find
00560 // one we are interested in, we then upgrade all calls to reflect the new
00561 // function.
00562 void llvm::UpgradeCallsToIntrinsic(Function* F) {
00563   assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
00564 
00565   // Upgrade the function and check if it is a totaly new function.
00566   Function *NewFn;
00567   if (UpgradeIntrinsicFunction(F, NewFn)) {
00568     if (NewFn != F) {
00569       // Replace all uses to the old function with the new one if necessary.
00570       for (Value::user_iterator UI = F->user_begin(), UE = F->user_end();
00571            UI != UE; ) {
00572         if (CallInst *CI = dyn_cast<CallInst>(*UI++))
00573           UpgradeIntrinsicCall(CI, NewFn);
00574       }
00575       // Remove old function, no longer used, from the module.
00576       F->eraseFromParent();
00577     }
00578   }
00579 }
00580 
00581 void llvm::UpgradeInstWithTBAATag(Instruction *I) {
00582   MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa);
00583   assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
00584   // Check if the tag uses struct-path aware TBAA format.
00585   if (isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3)
00586     return;
00587 
00588   if (MD->getNumOperands() == 3) {
00589     Value *Elts[] = {
00590       MD->getOperand(0),
00591       MD->getOperand(1)
00592     };
00593     MDNode *ScalarType = MDNode::get(I->getContext(), Elts);
00594     // Create a MDNode <ScalarType, ScalarType, offset 0, const>
00595     Value *Elts2[] = {
00596       ScalarType, ScalarType,
00597       Constant::getNullValue(Type::getInt64Ty(I->getContext())),
00598       MD->getOperand(2)
00599     };
00600     I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2));
00601   } else {
00602     // Create a MDNode <MD, MD, offset 0>
00603     Value *Elts[] = {MD, MD,
00604       Constant::getNullValue(Type::getInt64Ty(I->getContext()))};
00605     I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts));
00606   }
00607 }
00608 
00609 Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,
00610                                       Instruction *&Temp) {
00611   if (Opc != Instruction::BitCast)
00612     return nullptr;
00613 
00614   Temp = nullptr;
00615   Type *SrcTy = V->getType();
00616   if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
00617       SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
00618     LLVMContext &Context = V->getContext();
00619 
00620     // We have no information about target data layout, so we assume that
00621     // the maximum pointer size is 64bit.
00622     Type *MidTy = Type::getInt64Ty(Context);
00623     Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy);
00624 
00625     return CastInst::Create(Instruction::IntToPtr, Temp, DestTy);
00626   }
00627 
00628   return nullptr;
00629 }
00630 
00631 Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) {
00632   if (Opc != Instruction::BitCast)
00633     return nullptr;
00634 
00635   Type *SrcTy = C->getType();
00636   if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
00637       SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
00638     LLVMContext &Context = C->getContext();
00639 
00640     // We have no information about target data layout, so we assume that
00641     // the maximum pointer size is 64bit.
00642     Type *MidTy = Type::getInt64Ty(Context);
00643 
00644     return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy),
00645                                      DestTy);
00646   }
00647 
00648   return nullptr;
00649 }
00650 
00651 /// Check the debug info version number, if it is out-dated, drop the debug
00652 /// info. Return true if module is modified.
00653 bool llvm::UpgradeDebugInfo(Module &M) {
00654   unsigned Version = getDebugMetadataVersionFromModule(M);
00655   if (Version == DEBUG_METADATA_VERSION)
00656     return false;
00657 
00658   bool RetCode = StripDebugInfo(M);
00659   if (RetCode) {
00660     DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version);
00661     M.getContext().diagnose(DiagVersion);
00662   }
00663   return RetCode;
00664 }
00665 
00666 void llvm::UpgradeMDStringConstant(std::string &String) {
00667   const std::string OldPrefix = "llvm.vectorizer.";
00668   if (String == "llvm.vectorizer.unroll") {
00669     String = "llvm.loop.interleave.count";
00670   } else if (String.find(OldPrefix) == 0) {
00671     String.replace(0, OldPrefix.size(), "llvm.loop.vectorize.");
00672   }
00673 }