LLVM  mainline
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 // This is where deprecated IR intrinsics and other IR features are updated to
00012 // current specifications.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #include "llvm/IR/AutoUpgrade.h"
00017 #include "llvm/IR/CFG.h"
00018 #include "llvm/IR/CallSite.h"
00019 #include "llvm/IR/Constants.h"
00020 #include "llvm/IR/DIBuilder.h"
00021 #include "llvm/IR/DebugInfo.h"
00022 #include "llvm/IR/DiagnosticInfo.h"
00023 #include "llvm/IR/Function.h"
00024 #include "llvm/IR/IRBuilder.h"
00025 #include "llvm/IR/Instruction.h"
00026 #include "llvm/IR/IntrinsicInst.h"
00027 #include "llvm/IR/LLVMContext.h"
00028 #include "llvm/IR/Module.h"
00029 #include "llvm/Support/ErrorHandling.h"
00030 #include <cstring>
00031 using namespace llvm;
00032 
00033 // Upgrade the declarations of the SSE4.1 functions whose arguments have
00034 // changed their type from v4f32 to v2i64.
00035 static bool UpgradeSSE41Function(Function* F, Intrinsic::ID IID,
00036                                  Function *&NewFn) {
00037   // Check whether this is an old version of the function, which received
00038   // v4f32 arguments.
00039   Type *Arg0Type = F->getFunctionType()->getParamType(0);
00040   if (Arg0Type != VectorType::get(Type::getFloatTy(F->getContext()), 4))
00041     return false;
00042 
00043   // Yes, it's old, replace it with new version.
00044   F->setName(F->getName() + ".old");
00045   NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
00046   return true;
00047 }
00048 
00049 // Upgrade the declarations of intrinsic functions whose 8-bit immediate mask
00050 // arguments have changed their type from i32 to i8.
00051 static bool UpgradeX86IntrinsicsWith8BitMask(Function *F, Intrinsic::ID IID,
00052                                              Function *&NewFn) {
00053   // Check that the last argument is an i32.
00054   Type *LastArgType = F->getFunctionType()->getParamType(
00055      F->getFunctionType()->getNumParams() - 1);
00056   if (!LastArgType->isIntegerTy(32))
00057     return false;
00058 
00059   // Move this function aside and map down.
00060   F->setName(F->getName() + ".old");
00061   NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
00062   return true;
00063 }
00064 
00065 // Upgrade the declarations of AVX-512 cmp intrinsic functions whose 8-bit
00066 // immediates have changed their type from i32 to i8.
00067 static bool UpgradeAVX512CmpIntrinsic(Function *F, Intrinsic::ID IID,
00068                                       Function *&NewFn) {
00069   // Check that the last argument is an i32.
00070   Type *LastArgType = F->getFunctionType()->getParamType(2);
00071   if (!LastArgType->isIntegerTy(32))
00072     return false;
00073 
00074   // Move this function aside and map down.
00075   F->setName(F->getName() + ".old");
00076   NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
00077   return true;
00078 }
00079 
00080 static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
00081   assert(F && "Illegal to upgrade a non-existent Function.");
00082 
00083   // Quickly eliminate it, if it's not a candidate.
00084   StringRef Name = F->getName();
00085   if (Name.size() <= 8 || !Name.startswith("llvm."))
00086     return false;
00087   Name = Name.substr(5); // Strip off "llvm."
00088 
00089   switch (Name[0]) {
00090   default: break;
00091   case 'a': {
00092     if (Name.startswith("arm.neon.vclz")) {
00093       Type* args[2] = {
00094         F->arg_begin()->getType(),
00095         Type::getInt1Ty(F->getContext())
00096       };
00097       // Can't use Intrinsic::getDeclaration here as it adds a ".i1" to
00098       // the end of the name. Change name from llvm.arm.neon.vclz.* to
00099       //  llvm.ctlz.*
00100       FunctionType* fType = FunctionType::get(F->getReturnType(), args, false);
00101       NewFn = Function::Create(fType, F->getLinkage(),
00102                                "llvm.ctlz." + Name.substr(14), F->getParent());
00103       return true;
00104     }
00105     if (Name.startswith("arm.neon.vcnt")) {
00106       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctpop,
00107                                         F->arg_begin()->getType());
00108       return true;
00109     }
00110     break;
00111   }
00112   case 'c': {
00113     if (Name.startswith("ctlz.") && F->arg_size() == 1) {
00114       F->setName(Name + ".old");
00115       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
00116                                         F->arg_begin()->getType());
00117       return true;
00118     }
00119     if (Name.startswith("cttz.") && F->arg_size() == 1) {
00120       F->setName(Name + ".old");
00121       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz,
00122                                         F->arg_begin()->getType());
00123       return true;
00124     }
00125     break;
00126   }
00127 
00128   case 'o':
00129     // We only need to change the name to match the mangling including the
00130     // address space.
00131     if (F->arg_size() == 2 && Name.startswith("objectsize.")) {
00132       Type *Tys[2] = { F->getReturnType(), F->arg_begin()->getType() };
00133       if (F->getName() != Intrinsic::getName(Intrinsic::objectsize, Tys)) {
00134         F->setName(Name + ".old");
00135         NewFn = Intrinsic::getDeclaration(F->getParent(),
00136                                           Intrinsic::objectsize, Tys);
00137         return true;
00138       }
00139     }
00140     break;
00141 
00142   case 'x': {
00143     if (Name.startswith("x86.sse2.pcmpeq.") ||
00144         Name.startswith("x86.sse2.pcmpgt.") ||
00145         Name.startswith("x86.avx2.pcmpeq.") ||
00146         Name.startswith("x86.avx2.pcmpgt.") ||
00147         Name.startswith("x86.avx.vpermil.") ||
00148         Name == "x86.avx.vinsertf128.pd.256" ||
00149         Name == "x86.avx.vinsertf128.ps.256" ||
00150         Name == "x86.avx.vinsertf128.si.256" ||
00151         Name == "x86.avx2.vinserti128" ||
00152         Name == "x86.avx.vextractf128.pd.256" ||
00153         Name == "x86.avx.vextractf128.ps.256" ||
00154         Name == "x86.avx.vextractf128.si.256" ||
00155         Name == "x86.avx2.vextracti128" ||
00156         Name == "x86.avx.movnt.dq.256" ||
00157         Name == "x86.avx.movnt.pd.256" ||
00158         Name == "x86.avx.movnt.ps.256" ||
00159         Name == "x86.sse42.crc32.64.8" ||
00160         Name == "x86.avx.vbroadcast.ss" ||
00161         Name == "x86.avx.vbroadcast.ss.256" ||
00162         Name == "x86.avx.vbroadcast.sd.256" ||
00163         Name == "x86.sse2.psll.dq" ||
00164         Name == "x86.sse2.psrl.dq" ||
00165         Name == "x86.avx2.psll.dq" ||
00166         Name == "x86.avx2.psrl.dq" ||
00167         Name == "x86.sse2.psll.dq.bs" ||
00168         Name == "x86.sse2.psrl.dq.bs" ||
00169         Name == "x86.avx2.psll.dq.bs" ||
00170         Name == "x86.avx2.psrl.dq.bs" ||
00171         Name == "x86.sse41.pblendw" ||
00172         Name == "x86.sse41.blendpd" ||
00173         Name == "x86.sse41.blendps" ||
00174         Name == "x86.avx.blend.pd.256" ||
00175         Name == "x86.avx.blend.ps.256" ||
00176         Name == "x86.avx2.pblendw" ||
00177         Name == "x86.avx2.pblendd.128" ||
00178         Name == "x86.avx2.pblendd.256" ||
00179         Name == "x86.avx2.vbroadcasti128" ||
00180         (Name.startswith("x86.xop.vpcom") && F->arg_size() == 2)) {
00181       NewFn = nullptr;
00182       return true;
00183     }
00184     // SSE4.1 ptest functions may have an old signature.
00185     if (Name.startswith("x86.sse41.ptest")) {
00186       if (Name == "x86.sse41.ptestc")
00187         return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn);
00188       if (Name == "x86.sse41.ptestz")
00189         return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn);
00190       if (Name == "x86.sse41.ptestnzc")
00191         return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn);
00192     }
00193     // Several blend and other instructions with masks used the wrong number of
00194     // bits.
00195     if (Name == "x86.sse41.insertps")
00196       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_insertps,
00197                                               NewFn);
00198     if (Name == "x86.sse41.dppd")
00199       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dppd,
00200                                               NewFn);
00201     if (Name == "x86.sse41.dpps")
00202       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dpps,
00203                                               NewFn);
00204     if (Name == "x86.sse41.mpsadbw")
00205       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_mpsadbw,
00206                                               NewFn);
00207     if (Name == "x86.avx.dp.ps.256")
00208       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx_dp_ps_256,
00209                                               NewFn);
00210     if (Name == "x86.avx2.mpsadbw")
00211       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_mpsadbw,
00212                                               NewFn);
00213 
00214     if (Name == "x86.avx512.mask.cmp.ps.512")
00215       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_ps_512,
00216                                        NewFn);
00217     if (Name == "x86.avx512.mask.cmp.pd.512")
00218       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_pd_512,
00219                                        NewFn);
00220 
00221     if (Name == "x86.avx512.mask.cmp.b.512")
00222       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_b_512,
00223                                        NewFn);
00224     if (Name == "x86.avx512.mask.cmp.w.512")
00225       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_w_512,
00226                                        NewFn);
00227     if (Name == "x86.avx512.mask.cmp.d.512")
00228       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_d_512,
00229                                        NewFn);
00230     if (Name == "x86.avx512.mask.cmp.q.512")
00231       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_q_512,
00232                                        NewFn);
00233     if (Name == "x86.avx512.mask.ucmp.b.512")
00234       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_b_512,
00235                                        NewFn);
00236     if (Name == "x86.avx512.mask.ucmp.w.512")
00237       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_w_512,
00238                                        NewFn);
00239     if (Name == "x86.avx512.mask.ucmp.d.512")
00240       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_d_512,
00241                                        NewFn);
00242     if (Name == "x86.avx512.mask.ucmp.q.512")
00243       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_q_512,
00244                                        NewFn);
00245 
00246     if (Name == "x86.avx512.mask.cmp.b.256")
00247       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_b_256,
00248                                        NewFn);
00249     if (Name == "x86.avx512.mask.cmp.w.256")
00250       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_w_256,
00251                                        NewFn);
00252     if (Name == "x86.avx512.mask.cmp.d.256")
00253       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_d_256,
00254                                        NewFn);
00255     if (Name == "x86.avx512.mask.cmp.q.256")
00256       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_q_256,
00257                                        NewFn);
00258     if (Name == "x86.avx512.mask.ucmp.b.256")
00259       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_b_256,
00260                                        NewFn);
00261     if (Name == "x86.avx512.mask.ucmp.w.256")
00262       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_w_256,
00263                                        NewFn);
00264     if (Name == "x86.avx512.mask.ucmp.d.256")
00265       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_d_256,
00266                                        NewFn);
00267     if (Name == "x86.avx512.mask.ucmp.q.256")
00268       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_q_256,
00269                                        NewFn);
00270 
00271     if (Name == "x86.avx512.mask.cmp.b.128")
00272       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_b_128,
00273                                        NewFn);
00274     if (Name == "x86.avx512.mask.cmp.w.128")
00275       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_w_128,
00276                                        NewFn);
00277     if (Name == "x86.avx512.mask.cmp.d.128")
00278       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_d_128,
00279                                        NewFn);
00280     if (Name == "x86.avx512.mask.cmp.q.128")
00281       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_q_128,
00282                                        NewFn);
00283     if (Name == "x86.avx512.mask.ucmp.b.128")
00284       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_b_128,
00285                                        NewFn);
00286     if (Name == "x86.avx512.mask.ucmp.w.128")
00287       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_w_128,
00288                                        NewFn);
00289     if (Name == "x86.avx512.mask.ucmp.d.128")
00290       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_d_128,
00291                                        NewFn);
00292     if (Name == "x86.avx512.mask.ucmp.q.128")
00293       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_q_128,
00294                                        NewFn);
00295 
00296     // frcz.ss/sd may need to have an argument dropped
00297     if (Name.startswith("x86.xop.vfrcz.ss") && F->arg_size() == 2) {
00298       F->setName(Name + ".old");
00299       NewFn = Intrinsic::getDeclaration(F->getParent(),
00300                                         Intrinsic::x86_xop_vfrcz_ss);
00301       return true;
00302     }
00303     if (Name.startswith("x86.xop.vfrcz.sd") && F->arg_size() == 2) {
00304       F->setName(Name + ".old");
00305       NewFn = Intrinsic::getDeclaration(F->getParent(),
00306                                         Intrinsic::x86_xop_vfrcz_sd);
00307       return true;
00308     }
00309     // Fix the FMA4 intrinsics to remove the 4
00310     if (Name.startswith("x86.fma4.")) {
00311       F->setName("llvm.x86.fma" + Name.substr(8));
00312       NewFn = F;
00313       return true;
00314     }
00315     break;
00316   }
00317   }
00318 
00319   //  This may not belong here. This function is effectively being overloaded
00320   //  to both detect an intrinsic which needs upgrading, and to provide the
00321   //  upgraded form of the intrinsic. We should perhaps have two separate
00322   //  functions for this.
00323   return false;
00324 }
00325 
00326 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
00327   NewFn = nullptr;
00328   bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
00329 
00330   // Upgrade intrinsic attributes.  This does not change the function.
00331   if (NewFn)
00332     F = NewFn;
00333   if (unsigned id = F->getIntrinsicID())
00334     F->setAttributes(Intrinsic::getAttributes(F->getContext(),
00335                                               (Intrinsic::ID)id));
00336   return Upgraded;
00337 }
00338 
00339 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
00340   // Nothing to do yet.
00341   return false;
00342 }
00343 
00344 // Handles upgrading SSE2 and AVX2 PSLLDQ intrinsics by converting them
00345 // to byte shuffles.
00346 static Value *UpgradeX86PSLLDQIntrinsics(IRBuilder<> &Builder, LLVMContext &C,
00347                                          Value *Op, unsigned NumLanes,
00348                                          unsigned Shift) {
00349   // Each lane is 16 bytes.
00350   unsigned NumElts = NumLanes * 16;
00351 
00352   // Bitcast from a 64-bit element type to a byte element type.
00353   Op = Builder.CreateBitCast(Op,
00354                              VectorType::get(Type::getInt8Ty(C), NumElts),
00355                              "cast");
00356   // We'll be shuffling in zeroes.
00357   Value *Res = ConstantVector::getSplat(NumElts, Builder.getInt8(0));
00358 
00359   // If shift is less than 16, emit a shuffle to move the bytes. Otherwise,
00360   // we'll just return the zero vector.
00361   if (Shift < 16) {
00362     SmallVector<Constant*, 32> Idxs;
00363     // 256-bit version is split into two 16-byte lanes.
00364     for (unsigned l = 0; l != NumElts; l += 16)
00365       for (unsigned i = 0; i != 16; ++i) {
00366         unsigned Idx = NumElts + i - Shift;
00367         if (Idx < NumElts)
00368           Idx -= NumElts - 16; // end of lane, switch operand.
00369         Idxs.push_back(Builder.getInt32(Idx + l));
00370       }
00371 
00372     Res = Builder.CreateShuffleVector(Res, Op, ConstantVector::get(Idxs));
00373   }
00374 
00375   // Bitcast back to a 64-bit element type.
00376   return Builder.CreateBitCast(Res,
00377                                VectorType::get(Type::getInt64Ty(C), 2*NumLanes),
00378                                "cast");
00379 }
00380 
00381 // Handles upgrading SSE2 and AVX2 PSRLDQ intrinsics by converting them
00382 // to byte shuffles.
00383 static Value *UpgradeX86PSRLDQIntrinsics(IRBuilder<> &Builder, LLVMContext &C,
00384                                          Value *Op, unsigned NumLanes,
00385                                          unsigned Shift) {
00386   // Each lane is 16 bytes.
00387   unsigned NumElts = NumLanes * 16;
00388 
00389   // Bitcast from a 64-bit element type to a byte element type.
00390   Op = Builder.CreateBitCast(Op,
00391                              VectorType::get(Type::getInt8Ty(C), NumElts),
00392                              "cast");
00393   // We'll be shuffling in zeroes.
00394   Value *Res = ConstantVector::getSplat(NumElts, Builder.getInt8(0));
00395 
00396   // If shift is less than 16, emit a shuffle to move the bytes. Otherwise,
00397   // we'll just return the zero vector.
00398   if (Shift < 16) {
00399     SmallVector<Constant*, 32> Idxs;
00400     // 256-bit version is split into two 16-byte lanes.
00401     for (unsigned l = 0; l != NumElts; l += 16)
00402       for (unsigned i = 0; i != 16; ++i) {
00403         unsigned Idx = i + Shift;
00404         if (Idx >= 16)
00405           Idx += NumElts - 16; // end of lane, switch operand.
00406         Idxs.push_back(Builder.getInt32(Idx + l));
00407       }
00408 
00409     Res = Builder.CreateShuffleVector(Op, Res, ConstantVector::get(Idxs));
00410   }
00411 
00412   // Bitcast back to a 64-bit element type.
00413   return Builder.CreateBitCast(Res,
00414                                VectorType::get(Type::getInt64Ty(C), 2*NumLanes),
00415                                "cast");
00416 }
00417 
00418 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
00419 // upgraded intrinsic. All argument and return casting must be provided in
00420 // order to seamlessly integrate with existing context.
00421 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
00422   Function *F = CI->getCalledFunction();
00423   LLVMContext &C = CI->getContext();
00424   IRBuilder<> Builder(C);
00425   Builder.SetInsertPoint(CI->getParent(), CI);
00426 
00427   assert(F && "Intrinsic call is not direct?");
00428 
00429   if (!NewFn) {
00430     // Get the Function's name.
00431     StringRef Name = F->getName();
00432 
00433     Value *Rep;
00434     // Upgrade packed integer vector compares intrinsics to compare instructions
00435     if (Name.startswith("llvm.x86.sse2.pcmpeq.") ||
00436         Name.startswith("llvm.x86.avx2.pcmpeq.")) {
00437       Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
00438                                  "pcmpeq");
00439       // need to sign extend since icmp returns vector of i1
00440       Rep = Builder.CreateSExt(Rep, CI->getType(), "");
00441     } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") ||
00442                Name.startswith("llvm.x86.avx2.pcmpgt.")) {
00443       Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
00444                                   "pcmpgt");
00445       // need to sign extend since icmp returns vector of i1
00446       Rep = Builder.CreateSExt(Rep, CI->getType(), "");
00447     } else if (Name == "llvm.x86.avx.movnt.dq.256" ||
00448                Name == "llvm.x86.avx.movnt.ps.256" ||
00449                Name == "llvm.x86.avx.movnt.pd.256") {
00450       IRBuilder<> Builder(C);
00451       Builder.SetInsertPoint(CI->getParent(), CI);
00452 
00453       Module *M = F->getParent();
00454       SmallVector<Metadata *, 1> Elts;
00455       Elts.push_back(
00456           ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
00457       MDNode *Node = MDNode::get(C, Elts);
00458 
00459       Value *Arg0 = CI->getArgOperand(0);
00460       Value *Arg1 = CI->getArgOperand(1);
00461 
00462       // Convert the type of the pointer to a pointer to the stored type.
00463       Value *BC = Builder.CreateBitCast(Arg0,
00464                                         PointerType::getUnqual(Arg1->getType()),
00465                                         "cast");
00466       StoreInst *SI = Builder.CreateStore(Arg1, BC);
00467       SI->setMetadata(M->getMDKindID("nontemporal"), Node);
00468       SI->setAlignment(16);
00469 
00470       // Remove intrinsic.
00471       CI->eraseFromParent();
00472       return;
00473     } else if (Name.startswith("llvm.x86.xop.vpcom")) {
00474       Intrinsic::ID intID;
00475       if (Name.endswith("ub"))
00476         intID = Intrinsic::x86_xop_vpcomub;
00477       else if (Name.endswith("uw"))
00478         intID = Intrinsic::x86_xop_vpcomuw;
00479       else if (Name.endswith("ud"))
00480         intID = Intrinsic::x86_xop_vpcomud;
00481       else if (Name.endswith("uq"))
00482         intID = Intrinsic::x86_xop_vpcomuq;
00483       else if (Name.endswith("b"))
00484         intID = Intrinsic::x86_xop_vpcomb;
00485       else if (Name.endswith("w"))
00486         intID = Intrinsic::x86_xop_vpcomw;
00487       else if (Name.endswith("d"))
00488         intID = Intrinsic::x86_xop_vpcomd;
00489       else if (Name.endswith("q"))
00490         intID = Intrinsic::x86_xop_vpcomq;
00491       else
00492         llvm_unreachable("Unknown suffix");
00493 
00494       Name = Name.substr(18); // strip off "llvm.x86.xop.vpcom"
00495       unsigned Imm;
00496       if (Name.startswith("lt"))
00497         Imm = 0;
00498       else if (Name.startswith("le"))
00499         Imm = 1;
00500       else if (Name.startswith("gt"))
00501         Imm = 2;
00502       else if (Name.startswith("ge"))
00503         Imm = 3;
00504       else if (Name.startswith("eq"))
00505         Imm = 4;
00506       else if (Name.startswith("ne"))
00507         Imm = 5;
00508       else if (Name.startswith("false"))
00509         Imm = 6;
00510       else if (Name.startswith("true"))
00511         Imm = 7;
00512       else
00513         llvm_unreachable("Unknown condition");
00514 
00515       Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
00516       Rep = Builder.CreateCall3(VPCOM, CI->getArgOperand(0),
00517                                 CI->getArgOperand(1), Builder.getInt8(Imm));
00518     } else if (Name == "llvm.x86.sse42.crc32.64.8") {
00519       Function *CRC32 = Intrinsic::getDeclaration(F->getParent(),
00520                                                Intrinsic::x86_sse42_crc32_32_8);
00521       Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C));
00522       Rep = Builder.CreateCall2(CRC32, Trunc0, CI->getArgOperand(1));
00523       Rep = Builder.CreateZExt(Rep, CI->getType(), "");
00524     } else if (Name.startswith("llvm.x86.avx.vbroadcast")) {
00525       // Replace broadcasts with a series of insertelements.
00526       Type *VecTy = CI->getType();
00527       Type *EltTy = VecTy->getVectorElementType();
00528       unsigned EltNum = VecTy->getVectorNumElements();
00529       Value *Cast = Builder.CreateBitCast(CI->getArgOperand(0),
00530                                           EltTy->getPointerTo());
00531       Value *Load = Builder.CreateLoad(Cast);
00532       Type *I32Ty = Type::getInt32Ty(C);
00533       Rep = UndefValue::get(VecTy);
00534       for (unsigned I = 0; I < EltNum; ++I)
00535         Rep = Builder.CreateInsertElement(Rep, Load,
00536                                           ConstantInt::get(I32Ty, I));
00537     } else if (Name == "llvm.x86.avx2.vbroadcasti128") {
00538       // Replace vbroadcasts with a vector shuffle.
00539       Value *Op = Builder.CreatePointerCast(
00540           CI->getArgOperand(0),
00541           PointerType::getUnqual(VectorType::get(Type::getInt64Ty(C), 2)));
00542       Value *Load = Builder.CreateLoad(Op);
00543       const int Idxs[4] = { 0, 1, 0, 1 };
00544       Rep = Builder.CreateShuffleVector(Load, UndefValue::get(Load->getType()),
00545                                         Idxs);
00546     } else if (Name == "llvm.x86.sse2.psll.dq") {
00547       // 128-bit shift left specified in bits.
00548       unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
00549       Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0), 1,
00550                                        Shift / 8); // Shift is in bits.
00551     } else if (Name == "llvm.x86.sse2.psrl.dq") {
00552       // 128-bit shift right specified in bits.
00553       unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
00554       Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0), 1,
00555                                        Shift / 8); // Shift is in bits.
00556     } else if (Name == "llvm.x86.avx2.psll.dq") {
00557       // 256-bit shift left specified in bits.
00558       unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
00559       Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0), 2,
00560                                        Shift / 8); // Shift is in bits.
00561     } else if (Name == "llvm.x86.avx2.psrl.dq") {
00562       // 256-bit shift right specified in bits.
00563       unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
00564       Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0), 2,
00565                                        Shift / 8); // Shift is in bits.
00566     } else if (Name == "llvm.x86.sse2.psll.dq.bs") {
00567       // 128-bit shift left specified in bytes.
00568       unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
00569       Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0), 1,
00570                                        Shift);
00571     } else if (Name == "llvm.x86.sse2.psrl.dq.bs") {
00572       // 128-bit shift right specified in bytes.
00573       unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
00574       Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0), 1,
00575                                        Shift);
00576     } else if (Name == "llvm.x86.avx2.psll.dq.bs") {
00577       // 256-bit shift left specified in bytes.
00578       unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
00579       Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0), 2,
00580                                        Shift);
00581     } else if (Name == "llvm.x86.avx2.psrl.dq.bs") {
00582       // 256-bit shift right specified in bytes.
00583       unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
00584       Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0), 2,
00585                                        Shift);
00586     } else if (Name == "llvm.x86.sse41.pblendw" ||
00587                Name == "llvm.x86.sse41.blendpd" ||
00588                Name == "llvm.x86.sse41.blendps" ||
00589                Name == "llvm.x86.avx.blend.pd.256" ||
00590                Name == "llvm.x86.avx.blend.ps.256" ||
00591                Name == "llvm.x86.avx2.pblendw" ||
00592                Name == "llvm.x86.avx2.pblendd.128" ||
00593                Name == "llvm.x86.avx2.pblendd.256") {
00594       Value *Op0 = CI->getArgOperand(0);
00595       Value *Op1 = CI->getArgOperand(1);
00596       unsigned Imm = cast <ConstantInt>(CI->getArgOperand(2))->getZExtValue();
00597       VectorType *VecTy = cast<VectorType>(CI->getType());
00598       unsigned NumElts = VecTy->getNumElements();
00599 
00600       SmallVector<Constant*, 16> Idxs;
00601       for (unsigned i = 0; i != NumElts; ++i) {
00602         unsigned Idx = ((Imm >> (i%8)) & 1) ? i + NumElts : i;
00603         Idxs.push_back(Builder.getInt32(Idx));
00604       }
00605 
00606       Rep = Builder.CreateShuffleVector(Op0, Op1, ConstantVector::get(Idxs));
00607     } else if (Name == "llvm.x86.avx.vinsertf128.pd.256" ||
00608                Name == "llvm.x86.avx.vinsertf128.ps.256" ||
00609                Name == "llvm.x86.avx.vinsertf128.si.256" ||
00610                Name == "llvm.x86.avx2.vinserti128") {
00611       Value *Op0 = CI->getArgOperand(0);
00612       Value *Op1 = CI->getArgOperand(1);
00613       unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
00614       VectorType *VecTy = cast<VectorType>(CI->getType());
00615       unsigned NumElts = VecTy->getNumElements();
00616       
00617       // Mask off the high bits of the immediate value; hardware ignores those.
00618       Imm = Imm & 1;
00619       
00620       // Extend the second operand into a vector that is twice as big.
00621       Value *UndefV = UndefValue::get(Op1->getType());
00622       SmallVector<Constant*, 8> Idxs;
00623       for (unsigned i = 0; i != NumElts; ++i) {
00624         Idxs.push_back(Builder.getInt32(i));
00625       }
00626       Rep = Builder.CreateShuffleVector(Op1, UndefV, ConstantVector::get(Idxs));
00627 
00628       // Insert the second operand into the first operand.
00629 
00630       // Note that there is no guarantee that instruction lowering will actually
00631       // produce a vinsertf128 instruction for the created shuffles. In
00632       // particular, the 0 immediate case involves no lane changes, so it can
00633       // be handled as a blend.
00634 
00635       // Example of shuffle mask for 32-bit elements:
00636       // Imm = 1  <i32 0, i32 1, i32 2,  i32 3,  i32 8, i32 9, i32 10, i32 11>
00637       // Imm = 0  <i32 8, i32 9, i32 10, i32 11, i32 4, i32 5, i32 6,  i32 7 >
00638 
00639       SmallVector<Constant*, 8> Idxs2;
00640       // The low half of the result is either the low half of the 1st operand
00641       // or the low half of the 2nd operand (the inserted vector).
00642       for (unsigned i = 0; i != NumElts / 2; ++i) {
00643         unsigned Idx = Imm ? i : (i + NumElts);
00644         Idxs2.push_back(Builder.getInt32(Idx));
00645       }
00646       // The high half of the result is either the low half of the 2nd operand
00647       // (the inserted vector) or the high half of the 1st operand.
00648       for (unsigned i = NumElts / 2; i != NumElts; ++i) {
00649         unsigned Idx = Imm ? (i + NumElts / 2) : i;
00650         Idxs2.push_back(Builder.getInt32(Idx));
00651       }
00652       Rep = Builder.CreateShuffleVector(Op0, Rep, ConstantVector::get(Idxs2));
00653     } else if (Name == "llvm.x86.avx.vextractf128.pd.256" ||
00654                Name == "llvm.x86.avx.vextractf128.ps.256" ||
00655                Name == "llvm.x86.avx.vextractf128.si.256" ||
00656                Name == "llvm.x86.avx2.vextracti128") {
00657       Value *Op0 = CI->getArgOperand(0);
00658       unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
00659       VectorType *VecTy = cast<VectorType>(CI->getType());
00660       unsigned NumElts = VecTy->getNumElements();
00661       
00662       // Mask off the high bits of the immediate value; hardware ignores those.
00663       Imm = Imm & 1;
00664 
00665       // Get indexes for either the high half or low half of the input vector.
00666       SmallVector<Constant*, 4> Idxs(NumElts);
00667       for (unsigned i = 0; i != NumElts; ++i) {
00668         unsigned Idx = Imm ? (i + NumElts) : i;
00669         Idxs[i] = Builder.getInt32(Idx);
00670       }
00671 
00672       Value *UndefV = UndefValue::get(Op0->getType());
00673       Rep = Builder.CreateShuffleVector(Op0, UndefV, ConstantVector::get(Idxs));
00674     } else {
00675       bool PD128 = false, PD256 = false, PS128 = false, PS256 = false;
00676       if (Name == "llvm.x86.avx.vpermil.pd.256")
00677         PD256 = true;
00678       else if (Name == "llvm.x86.avx.vpermil.pd")
00679         PD128 = true;
00680       else if (Name == "llvm.x86.avx.vpermil.ps.256")
00681         PS256 = true;
00682       else if (Name == "llvm.x86.avx.vpermil.ps")
00683         PS128 = true;
00684 
00685       if (PD256 || PD128 || PS256 || PS128) {
00686         Value *Op0 = CI->getArgOperand(0);
00687         unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
00688         SmallVector<Constant*, 8> Idxs;
00689 
00690         if (PD128)
00691           for (unsigned i = 0; i != 2; ++i)
00692             Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1));
00693         else if (PD256)
00694           for (unsigned l = 0; l != 4; l+=2)
00695             for (unsigned i = 0; i != 2; ++i)
00696               Idxs.push_back(Builder.getInt32(((Imm >> (l+i)) & 0x1) + l));
00697         else if (PS128)
00698           for (unsigned i = 0; i != 4; ++i)
00699             Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3));
00700         else if (PS256)
00701           for (unsigned l = 0; l != 8; l+=4)
00702             for (unsigned i = 0; i != 4; ++i)
00703               Idxs.push_back(Builder.getInt32(((Imm >> (2 * i)) & 0x3) + l));
00704         else
00705           llvm_unreachable("Unexpected function");
00706 
00707         Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs));
00708       } else {
00709         llvm_unreachable("Unknown function for CallInst upgrade.");
00710       }
00711     }
00712 
00713     CI->replaceAllUsesWith(Rep);
00714     CI->eraseFromParent();
00715     return;
00716   }
00717 
00718   std::string Name = CI->getName();
00719   if (!Name.empty())
00720     CI->setName(Name + ".old");
00721 
00722   switch (NewFn->getIntrinsicID()) {
00723   default:
00724     llvm_unreachable("Unknown function for CallInst upgrade.");
00725 
00726   case Intrinsic::ctlz:
00727   case Intrinsic::cttz:
00728     assert(CI->getNumArgOperands() == 1 &&
00729            "Mismatch between function args and call args");
00730     CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
00731                                                Builder.getFalse(), Name));
00732     CI->eraseFromParent();
00733     return;
00734 
00735   case Intrinsic::objectsize:
00736     CI->replaceAllUsesWith(Builder.CreateCall2(NewFn,
00737                                                CI->getArgOperand(0),
00738                                                CI->getArgOperand(1),
00739                                                Name));
00740     CI->eraseFromParent();
00741     return;
00742 
00743   case Intrinsic::ctpop: {
00744     CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(0)));
00745     CI->eraseFromParent();
00746     return;
00747   }
00748 
00749   case Intrinsic::x86_xop_vfrcz_ss:
00750   case Intrinsic::x86_xop_vfrcz_sd:
00751     CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(1),
00752                                               Name));
00753     CI->eraseFromParent();
00754     return;
00755 
00756   case Intrinsic::x86_sse41_ptestc:
00757   case Intrinsic::x86_sse41_ptestz:
00758   case Intrinsic::x86_sse41_ptestnzc: {
00759     // The arguments for these intrinsics used to be v4f32, and changed
00760     // to v2i64. This is purely a nop, since those are bitwise intrinsics.
00761     // So, the only thing required is a bitcast for both arguments.
00762     // First, check the arguments have the old type.
00763     Value *Arg0 = CI->getArgOperand(0);
00764     if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4))
00765       return;
00766 
00767     // Old intrinsic, add bitcasts
00768     Value *Arg1 = CI->getArgOperand(1);
00769 
00770     Value *BC0 =
00771       Builder.CreateBitCast(Arg0,
00772                             VectorType::get(Type::getInt64Ty(C), 2),
00773                             "cast");
00774     Value *BC1 =
00775       Builder.CreateBitCast(Arg1,
00776                             VectorType::get(Type::getInt64Ty(C), 2),
00777                             "cast");
00778 
00779     CallInst* NewCall = Builder.CreateCall2(NewFn, BC0, BC1, Name);
00780     CI->replaceAllUsesWith(NewCall);
00781     CI->eraseFromParent();
00782     return;
00783   }
00784 
00785   case Intrinsic::x86_sse41_insertps:
00786   case Intrinsic::x86_sse41_dppd:
00787   case Intrinsic::x86_sse41_dpps:
00788   case Intrinsic::x86_sse41_mpsadbw:
00789   case Intrinsic::x86_avx_dp_ps_256:
00790   case Intrinsic::x86_avx2_mpsadbw: {
00791     // Need to truncate the last argument from i32 to i8 -- this argument models
00792     // an inherently 8-bit immediate operand to these x86 instructions.
00793     SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
00794                                  CI->arg_operands().end());
00795 
00796     // Replace the last argument with a trunc.
00797     Args.back() = Builder.CreateTrunc(Args.back(), Type::getInt8Ty(C), "trunc");
00798 
00799     CallInst *NewCall = Builder.CreateCall(NewFn, Args);
00800     CI->replaceAllUsesWith(NewCall);
00801     CI->eraseFromParent();
00802     return;
00803   }
00804   case Intrinsic::x86_avx512_mask_cmp_ps_512:
00805   case Intrinsic::x86_avx512_mask_cmp_pd_512: {
00806     // Need to truncate the last argument from i32 to i8 -- this argument models
00807     // an inherently 8-bit immediate operand to these x86 instructions.
00808     SmallVector<Value *, 5> Args(CI->arg_operands().begin(),
00809                                  CI->arg_operands().end());
00810 
00811     // Replace the last argument with a trunc.
00812     Args[2] = Builder.CreateTrunc(Args[2], Type::getInt8Ty(C), "trunc");
00813 
00814     CallInst *NewCall = Builder.CreateCall(NewFn, Args);
00815     CI->replaceAllUsesWith(NewCall);
00816     CI->eraseFromParent();
00817     return;
00818   }
00819   }
00820 }
00821 
00822 // This tests each Function to determine if it needs upgrading. When we find
00823 // one we are interested in, we then upgrade all calls to reflect the new
00824 // function.
00825 void llvm::UpgradeCallsToIntrinsic(Function* F) {
00826   assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
00827 
00828   // Upgrade the function and check if it is a totaly new function.
00829   Function *NewFn;
00830   if (UpgradeIntrinsicFunction(F, NewFn)) {
00831     if (NewFn != F) {
00832       // Replace all uses to the old function with the new one if necessary.
00833       for (Value::user_iterator UI = F->user_begin(), UE = F->user_end();
00834            UI != UE; ) {
00835         if (CallInst *CI = dyn_cast<CallInst>(*UI++))
00836           UpgradeIntrinsicCall(CI, NewFn);
00837       }
00838       // Remove old function, no longer used, from the module.
00839       F->eraseFromParent();
00840     }
00841   }
00842 }
00843 
00844 void llvm::UpgradeInstWithTBAATag(Instruction *I) {
00845   MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa);
00846   assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
00847   // Check if the tag uses struct-path aware TBAA format.
00848   if (isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3)
00849     return;
00850 
00851   if (MD->getNumOperands() == 3) {
00852     Metadata *Elts[] = {MD->getOperand(0), MD->getOperand(1)};
00853     MDNode *ScalarType = MDNode::get(I->getContext(), Elts);
00854     // Create a MDNode <ScalarType, ScalarType, offset 0, const>
00855     Metadata *Elts2[] = {ScalarType, ScalarType,
00856                          ConstantAsMetadata::get(Constant::getNullValue(
00857                              Type::getInt64Ty(I->getContext()))),
00858                          MD->getOperand(2)};
00859     I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2));
00860   } else {
00861     // Create a MDNode <MD, MD, offset 0>
00862     Metadata *Elts[] = {MD, MD, ConstantAsMetadata::get(Constant::getNullValue(
00863                                     Type::getInt64Ty(I->getContext())))};
00864     I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts));
00865   }
00866 }
00867 
00868 Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,
00869                                       Instruction *&Temp) {
00870   if (Opc != Instruction::BitCast)
00871     return nullptr;
00872 
00873   Temp = nullptr;
00874   Type *SrcTy = V->getType();
00875   if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
00876       SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
00877     LLVMContext &Context = V->getContext();
00878 
00879     // We have no information about target data layout, so we assume that
00880     // the maximum pointer size is 64bit.
00881     Type *MidTy = Type::getInt64Ty(Context);
00882     Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy);
00883 
00884     return CastInst::Create(Instruction::IntToPtr, Temp, DestTy);
00885   }
00886 
00887   return nullptr;
00888 }
00889 
00890 Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) {
00891   if (Opc != Instruction::BitCast)
00892     return nullptr;
00893 
00894   Type *SrcTy = C->getType();
00895   if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
00896       SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
00897     LLVMContext &Context = C->getContext();
00898 
00899     // We have no information about target data layout, so we assume that
00900     // the maximum pointer size is 64bit.
00901     Type *MidTy = Type::getInt64Ty(Context);
00902 
00903     return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy),
00904                                      DestTy);
00905   }
00906 
00907   return nullptr;
00908 }
00909 
00910 /// Check the debug info version number, if it is out-dated, drop the debug
00911 /// info. Return true if module is modified.
00912 bool llvm::UpgradeDebugInfo(Module &M) {
00913   unsigned Version = getDebugMetadataVersionFromModule(M);
00914   if (Version == DEBUG_METADATA_VERSION)
00915     return false;
00916 
00917   bool RetCode = StripDebugInfo(M);
00918   if (RetCode) {
00919     DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version);
00920     M.getContext().diagnose(DiagVersion);
00921   }
00922   return RetCode;
00923 }
00924 
00925 void llvm::UpgradeMDStringConstant(std::string &String) {
00926   const std::string OldPrefix = "llvm.vectorizer.";
00927   if (String == "llvm.vectorizer.unroll") {
00928     String = "llvm.loop.interleave.count";
00929   } else if (String.find(OldPrefix) == 0) {
00930     String.replace(0, OldPrefix.size(), "llvm.loop.vectorize.");
00931   }
00932 }