LLVM API Documentation

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