LLVM API Documentation

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