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 MetadataAsValue *getExpression(Value *VarOperand, Function *F) {
00264   // Old-style DIVariables have an optional expression as the 8th element.
00265   DIExpression Expr(getNodeField(
00266       cast<MDNode>(cast<MetadataAsValue>(VarOperand)->getMetadata()), 8));
00267   if (!Expr) {
00268     DIBuilder DIB(*F->getParent(), /*AllowUnresolved*/ false);
00269     Expr = DIB.createExpression();
00270   }
00271   return MetadataAsValue::get(F->getContext(), Expr);
00272 }
00273 
00274 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
00275 // upgraded intrinsic. All argument and return casting must be provided in
00276 // order to seamlessly integrate with existing context.
00277 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
00278   Function *F = CI->getCalledFunction();
00279   LLVMContext &C = CI->getContext();
00280   IRBuilder<> Builder(C);
00281   Builder.SetInsertPoint(CI->getParent(), CI);
00282 
00283   assert(F && "Intrinsic call is not direct?");
00284 
00285   if (!NewFn) {
00286     // Get the Function's name.
00287     StringRef Name = F->getName();
00288 
00289     Value *Rep;
00290     // Upgrade packed integer vector compares intrinsics to compare instructions
00291     if (Name.startswith("llvm.x86.sse2.pcmpeq.") ||
00292         Name.startswith("llvm.x86.avx2.pcmpeq.")) {
00293       Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
00294                                  "pcmpeq");
00295       // need to sign extend since icmp returns vector of i1
00296       Rep = Builder.CreateSExt(Rep, CI->getType(), "");
00297     } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") ||
00298                Name.startswith("llvm.x86.avx2.pcmpgt.")) {
00299       Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
00300                                   "pcmpgt");
00301       // need to sign extend since icmp returns vector of i1
00302       Rep = Builder.CreateSExt(Rep, CI->getType(), "");
00303     } else if (Name == "llvm.x86.avx.movnt.dq.256" ||
00304                Name == "llvm.x86.avx.movnt.ps.256" ||
00305                Name == "llvm.x86.avx.movnt.pd.256") {
00306       IRBuilder<> Builder(C);
00307       Builder.SetInsertPoint(CI->getParent(), CI);
00308 
00309       Module *M = F->getParent();
00310       SmallVector<Metadata *, 1> Elts;
00311       Elts.push_back(
00312           ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
00313       MDNode *Node = MDNode::get(C, Elts);
00314 
00315       Value *Arg0 = CI->getArgOperand(0);
00316       Value *Arg1 = CI->getArgOperand(1);
00317 
00318       // Convert the type of the pointer to a pointer to the stored type.
00319       Value *BC = Builder.CreateBitCast(Arg0,
00320                                         PointerType::getUnqual(Arg1->getType()),
00321                                         "cast");
00322       StoreInst *SI = Builder.CreateStore(Arg1, BC);
00323       SI->setMetadata(M->getMDKindID("nontemporal"), Node);
00324       SI->setAlignment(16);
00325 
00326       // Remove intrinsic.
00327       CI->eraseFromParent();
00328       return;
00329     } else if (Name.startswith("llvm.x86.xop.vpcom")) {
00330       Intrinsic::ID intID;
00331       if (Name.endswith("ub"))
00332         intID = Intrinsic::x86_xop_vpcomub;
00333       else if (Name.endswith("uw"))
00334         intID = Intrinsic::x86_xop_vpcomuw;
00335       else if (Name.endswith("ud"))
00336         intID = Intrinsic::x86_xop_vpcomud;
00337       else if (Name.endswith("uq"))
00338         intID = Intrinsic::x86_xop_vpcomuq;
00339       else if (Name.endswith("b"))
00340         intID = Intrinsic::x86_xop_vpcomb;
00341       else if (Name.endswith("w"))
00342         intID = Intrinsic::x86_xop_vpcomw;
00343       else if (Name.endswith("d"))
00344         intID = Intrinsic::x86_xop_vpcomd;
00345       else if (Name.endswith("q"))
00346         intID = Intrinsic::x86_xop_vpcomq;
00347       else
00348         llvm_unreachable("Unknown suffix");
00349 
00350       Name = Name.substr(18); // strip off "llvm.x86.xop.vpcom"
00351       unsigned Imm;
00352       if (Name.startswith("lt"))
00353         Imm = 0;
00354       else if (Name.startswith("le"))
00355         Imm = 1;
00356       else if (Name.startswith("gt"))
00357         Imm = 2;
00358       else if (Name.startswith("ge"))
00359         Imm = 3;
00360       else if (Name.startswith("eq"))
00361         Imm = 4;
00362       else if (Name.startswith("ne"))
00363         Imm = 5;
00364       else if (Name.startswith("true"))
00365         Imm = 6;
00366       else if (Name.startswith("false"))
00367         Imm = 7;
00368       else
00369         llvm_unreachable("Unknown condition");
00370 
00371       Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
00372       Rep = Builder.CreateCall3(VPCOM, CI->getArgOperand(0),
00373                                 CI->getArgOperand(1), Builder.getInt8(Imm));
00374     } else if (Name == "llvm.x86.sse42.crc32.64.8") {
00375       Function *CRC32 = Intrinsic::getDeclaration(F->getParent(),
00376                                                Intrinsic::x86_sse42_crc32_32_8);
00377       Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C));
00378       Rep = Builder.CreateCall2(CRC32, Trunc0, CI->getArgOperand(1));
00379       Rep = Builder.CreateZExt(Rep, CI->getType(), "");
00380     } else if (Name.startswith("llvm.x86.avx.vbroadcast")) {
00381       // Replace broadcasts with a series of insertelements.
00382       Type *VecTy = CI->getType();
00383       Type *EltTy = VecTy->getVectorElementType();
00384       unsigned EltNum = VecTy->getVectorNumElements();
00385       Value *Cast = Builder.CreateBitCast(CI->getArgOperand(0),
00386                                           EltTy->getPointerTo());
00387       Value *Load = Builder.CreateLoad(Cast);
00388       Type *I32Ty = Type::getInt32Ty(C);
00389       Rep = UndefValue::get(VecTy);
00390       for (unsigned I = 0; I < EltNum; ++I)
00391         Rep = Builder.CreateInsertElement(Rep, Load,
00392                                           ConstantInt::get(I32Ty, I));
00393     } else {
00394       bool PD128 = false, PD256 = false, PS128 = false, PS256 = false;
00395       if (Name == "llvm.x86.avx.vpermil.pd.256")
00396         PD256 = true;
00397       else if (Name == "llvm.x86.avx.vpermil.pd")
00398         PD128 = true;
00399       else if (Name == "llvm.x86.avx.vpermil.ps.256")
00400         PS256 = true;
00401       else if (Name == "llvm.x86.avx.vpermil.ps")
00402         PS128 = true;
00403 
00404       if (PD256 || PD128 || PS256 || PS128) {
00405         Value *Op0 = CI->getArgOperand(0);
00406         unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
00407         SmallVector<Constant*, 8> Idxs;
00408 
00409         if (PD128)
00410           for (unsigned i = 0; i != 2; ++i)
00411             Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1));
00412         else if (PD256)
00413           for (unsigned l = 0; l != 4; l+=2)
00414             for (unsigned i = 0; i != 2; ++i)
00415               Idxs.push_back(Builder.getInt32(((Imm >> (l+i)) & 0x1) + l));
00416         else if (PS128)
00417           for (unsigned i = 0; i != 4; ++i)
00418             Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3));
00419         else if (PS256)
00420           for (unsigned l = 0; l != 8; l+=4)
00421             for (unsigned i = 0; i != 4; ++i)
00422               Idxs.push_back(Builder.getInt32(((Imm >> (2 * i)) & 0x3) + l));
00423         else
00424           llvm_unreachable("Unexpected function");
00425 
00426         Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs));
00427       } else {
00428         llvm_unreachable("Unknown function for CallInst upgrade.");
00429       }
00430     }
00431 
00432     CI->replaceAllUsesWith(Rep);
00433     CI->eraseFromParent();
00434     return;
00435   }
00436 
00437   std::string Name = CI->getName().str();
00438   if (!Name.empty())
00439     CI->setName(Name + ".old");
00440 
00441   switch (NewFn->getIntrinsicID()) {
00442   default:
00443     llvm_unreachable("Unknown function for CallInst upgrade.");
00444 
00445   // Upgrade debug intrinsics to use an additional DIExpression argument.
00446   case Intrinsic::dbg_declare: {
00447     auto NewCI =
00448         Builder.CreateCall3(NewFn, CI->getArgOperand(0), CI->getArgOperand(1),
00449                             getExpression(CI->getArgOperand(1), F), Name);
00450     NewCI->setDebugLoc(CI->getDebugLoc());
00451     CI->replaceAllUsesWith(NewCI);
00452     CI->eraseFromParent();
00453     return;
00454   }
00455   case Intrinsic::dbg_value: {
00456     auto NewCI = Builder.CreateCall4(
00457         NewFn, CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2),
00458         getExpression(CI->getArgOperand(2), F), Name);
00459     NewCI->setDebugLoc(CI->getDebugLoc());
00460     CI->replaceAllUsesWith(NewCI);
00461     CI->eraseFromParent();
00462     return;
00463   }
00464   case Intrinsic::ctlz:
00465   case Intrinsic::cttz:
00466     assert(CI->getNumArgOperands() == 1 &&
00467            "Mismatch between function args and call args");
00468     CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
00469                                                Builder.getFalse(), Name));
00470     CI->eraseFromParent();
00471     return;
00472 
00473   case Intrinsic::objectsize:
00474     CI->replaceAllUsesWith(Builder.CreateCall2(NewFn,
00475                                                CI->getArgOperand(0),
00476                                                CI->getArgOperand(1),
00477                                                Name));
00478     CI->eraseFromParent();
00479     return;
00480 
00481   case Intrinsic::ctpop: {
00482     CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(0)));
00483     CI->eraseFromParent();
00484     return;
00485   }
00486 
00487   case Intrinsic::x86_xop_vfrcz_ss:
00488   case Intrinsic::x86_xop_vfrcz_sd:
00489     CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(1),
00490                                               Name));
00491     CI->eraseFromParent();
00492     return;
00493 
00494   case Intrinsic::x86_sse41_ptestc:
00495   case Intrinsic::x86_sse41_ptestz:
00496   case Intrinsic::x86_sse41_ptestnzc: {
00497     // The arguments for these intrinsics used to be v4f32, and changed
00498     // to v2i64. This is purely a nop, since those are bitwise intrinsics.
00499     // So, the only thing required is a bitcast for both arguments.
00500     // First, check the arguments have the old type.
00501     Value *Arg0 = CI->getArgOperand(0);
00502     if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4))
00503       return;
00504 
00505     // Old intrinsic, add bitcasts
00506     Value *Arg1 = CI->getArgOperand(1);
00507 
00508     Value *BC0 =
00509       Builder.CreateBitCast(Arg0,
00510                             VectorType::get(Type::getInt64Ty(C), 2),
00511                             "cast");
00512     Value *BC1 =
00513       Builder.CreateBitCast(Arg1,
00514                             VectorType::get(Type::getInt64Ty(C), 2),
00515                             "cast");
00516 
00517     CallInst* NewCall = Builder.CreateCall2(NewFn, BC0, BC1, Name);
00518     CI->replaceAllUsesWith(NewCall);
00519     CI->eraseFromParent();
00520     return;
00521   }
00522 
00523   case Intrinsic::x86_sse41_pblendw:
00524   case Intrinsic::x86_sse41_blendpd:
00525   case Intrinsic::x86_sse41_blendps:
00526   case Intrinsic::x86_sse41_insertps:
00527   case Intrinsic::x86_sse41_dppd:
00528   case Intrinsic::x86_sse41_dpps:
00529   case Intrinsic::x86_sse41_mpsadbw:
00530   case Intrinsic::x86_avx_blend_pd_256:
00531   case Intrinsic::x86_avx_blend_ps_256:
00532   case Intrinsic::x86_avx_dp_ps_256:
00533   case Intrinsic::x86_avx2_pblendw:
00534   case Intrinsic::x86_avx2_pblendd_128:
00535   case Intrinsic::x86_avx2_pblendd_256:
00536   case Intrinsic::x86_avx2_mpsadbw: {
00537     // Need to truncate the last argument from i32 to i8 -- this argument models
00538     // an inherently 8-bit immediate operand to these x86 instructions.
00539     SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
00540                                  CI->arg_operands().end());
00541 
00542     // Replace the last argument with a trunc.
00543     Args.back() = Builder.CreateTrunc(Args.back(), Type::getInt8Ty(C), "trunc");
00544 
00545     CallInst *NewCall = Builder.CreateCall(NewFn, Args);
00546     CI->replaceAllUsesWith(NewCall);
00547     CI->eraseFromParent();
00548     return;
00549   }
00550   }
00551 }
00552 
00553 // This tests each Function to determine if it needs upgrading. When we find
00554 // one we are interested in, we then upgrade all calls to reflect the new
00555 // function.
00556 void llvm::UpgradeCallsToIntrinsic(Function* F) {
00557   assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
00558 
00559   // Upgrade the function and check if it is a totaly new function.
00560   Function *NewFn;
00561   if (UpgradeIntrinsicFunction(F, NewFn)) {
00562     if (NewFn != F) {
00563       // Replace all uses to the old function with the new one if necessary.
00564       for (Value::user_iterator UI = F->user_begin(), UE = F->user_end();
00565            UI != UE; ) {
00566         if (CallInst *CI = dyn_cast<CallInst>(*UI++))
00567           UpgradeIntrinsicCall(CI, NewFn);
00568       }
00569       // Remove old function, no longer used, from the module.
00570       F->eraseFromParent();
00571     }
00572   }
00573 }
00574 
00575 void llvm::UpgradeInstWithTBAATag(Instruction *I) {
00576   MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa);
00577   assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
00578   // Check if the tag uses struct-path aware TBAA format.
00579   if (isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3)
00580     return;
00581 
00582   if (MD->getNumOperands() == 3) {
00583     Metadata *Elts[] = {MD->getOperand(0), MD->getOperand(1)};
00584     MDNode *ScalarType = MDNode::get(I->getContext(), Elts);
00585     // Create a MDNode <ScalarType, ScalarType, offset 0, const>
00586     Metadata *Elts2[] = {ScalarType, ScalarType,
00587                          ConstantAsMetadata::get(Constant::getNullValue(
00588                              Type::getInt64Ty(I->getContext()))),
00589                          MD->getOperand(2)};
00590     I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2));
00591   } else {
00592     // Create a MDNode <MD, MD, offset 0>
00593     Metadata *Elts[] = {MD, MD, ConstantAsMetadata::get(Constant::getNullValue(
00594                                     Type::getInt64Ty(I->getContext())))};
00595     I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts));
00596   }
00597 }
00598 
00599 Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,
00600                                       Instruction *&Temp) {
00601   if (Opc != Instruction::BitCast)
00602     return nullptr;
00603 
00604   Temp = nullptr;
00605   Type *SrcTy = V->getType();
00606   if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
00607       SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
00608     LLVMContext &Context = V->getContext();
00609 
00610     // We have no information about target data layout, so we assume that
00611     // the maximum pointer size is 64bit.
00612     Type *MidTy = Type::getInt64Ty(Context);
00613     Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy);
00614 
00615     return CastInst::Create(Instruction::IntToPtr, Temp, DestTy);
00616   }
00617 
00618   return nullptr;
00619 }
00620 
00621 Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) {
00622   if (Opc != Instruction::BitCast)
00623     return nullptr;
00624 
00625   Type *SrcTy = C->getType();
00626   if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
00627       SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
00628     LLVMContext &Context = C->getContext();
00629 
00630     // We have no information about target data layout, so we assume that
00631     // the maximum pointer size is 64bit.
00632     Type *MidTy = Type::getInt64Ty(Context);
00633 
00634     return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy),
00635                                      DestTy);
00636   }
00637 
00638   return nullptr;
00639 }
00640 
00641 /// Check the debug info version number, if it is out-dated, drop the debug
00642 /// info. Return true if module is modified.
00643 bool llvm::UpgradeDebugInfo(Module &M) {
00644   unsigned Version = getDebugMetadataVersionFromModule(M);
00645   if (Version == DEBUG_METADATA_VERSION)
00646     return false;
00647 
00648   bool RetCode = StripDebugInfo(M);
00649   if (RetCode) {
00650     DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version);
00651     M.getContext().diagnose(DiagVersion);
00652   }
00653   return RetCode;
00654 }
00655 
00656 void llvm::UpgradeMDStringConstant(std::string &String) {
00657   const std::string OldPrefix = "llvm.vectorizer.";
00658   if (String == "llvm.vectorizer.unroll") {
00659     String = "llvm.loop.interleave.count";
00660   } else if (String.find(OldPrefix) == 0) {
00661     String.replace(0, OldPrefix.size(), "llvm.loop.vectorize.");
00662   }
00663 }