AutoUpgrade.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. //===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the auto-upgrade helper functions.
  11. // This is where deprecated IR intrinsics and other IR features are updated to
  12. // current specifications.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/IR/AutoUpgrade.h"
  16. #include "llvm/IR/CFG.h"
  17. #include "llvm/IR/CallSite.h"
  18. #include "llvm/IR/Constants.h"
  19. #include "llvm/IR/DIBuilder.h"
  20. #include "llvm/IR/DebugInfo.h"
  21. #include "llvm/IR/DiagnosticInfo.h"
  22. #include "llvm/IR/Function.h"
  23. #include "llvm/IR/IRBuilder.h"
  24. #include "llvm/IR/Instruction.h"
  25. #include "llvm/IR/IntrinsicInst.h"
  26. #include "llvm/IR/LLVMContext.h"
  27. #include "llvm/IR/Module.h"
  28. #include "llvm/Support/ErrorHandling.h"
  29. #include <cstring>
  30. using namespace llvm;
  31. // Upgrade the declarations of the SSE4.1 functions whose arguments have
  32. // changed their type from v4f32 to v2i64.
  33. static bool UpgradeSSE41Function(Function* F, Intrinsic::ID IID,
  34. Function *&NewFn) {
  35. // Check whether this is an old version of the function, which received
  36. // v4f32 arguments.
  37. Type *Arg0Type = F->getFunctionType()->getParamType(0);
  38. if (Arg0Type != VectorType::get(Type::getFloatTy(F->getContext()), 4))
  39. return false;
  40. // Yes, it's old, replace it with new version.
  41. F->setName(F->getName() + ".old");
  42. NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
  43. return true;
  44. }
  45. // Upgrade the declarations of intrinsic functions whose 8-bit immediate mask
  46. // arguments have changed their type from i32 to i8.
  47. static bool UpgradeX86IntrinsicsWith8BitMask(Function *F, Intrinsic::ID IID,
  48. Function *&NewFn) {
  49. // Check that the last argument is an i32.
  50. Type *LastArgType = F->getFunctionType()->getParamType(
  51. F->getFunctionType()->getNumParams() - 1);
  52. if (!LastArgType->isIntegerTy(32))
  53. return false;
  54. // Move this function aside and map down.
  55. F->setName(F->getName() + ".old");
  56. NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
  57. return true;
  58. }
  59. static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
  60. assert(F && "Illegal to upgrade a non-existent Function.");
  61. // Quickly eliminate it, if it's not a candidate.
  62. StringRef Name = F->getName();
  63. if (Name.size() <= 8 || !Name.startswith("llvm."))
  64. return false;
  65. Name = Name.substr(5); // Strip off "llvm."
  66. switch (Name[0]) {
  67. default: break;
  68. case 'a': {
  69. if (Name.startswith("arm.neon.vclz")) {
  70. Type* args[2] = {
  71. F->arg_begin()->getType(),
  72. Type::getInt1Ty(F->getContext())
  73. };
  74. // Can't use Intrinsic::getDeclaration here as it adds a ".i1" to
  75. // the end of the name. Change name from llvm.arm.neon.vclz.* to
  76. // llvm.ctlz.*
  77. FunctionType* fType = FunctionType::get(F->getReturnType(), args, false);
  78. NewFn = Function::Create(fType, F->getLinkage(),
  79. "llvm.ctlz." + Name.substr(14), F->getParent());
  80. return true;
  81. }
  82. if (Name.startswith("arm.neon.vcnt")) {
  83. NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctpop,
  84. F->arg_begin()->getType());
  85. return true;
  86. }
  87. break;
  88. }
  89. case 'c': {
  90. if (Name.startswith("ctlz.") && F->arg_size() == 1) {
  91. F->setName(Name + ".old");
  92. NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
  93. F->arg_begin()->getType());
  94. return true;
  95. }
  96. if (Name.startswith("cttz.") && F->arg_size() == 1) {
  97. F->setName(Name + ".old");
  98. NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz,
  99. F->arg_begin()->getType());
  100. return true;
  101. }
  102. break;
  103. }
  104. case 'o':
  105. // We only need to change the name to match the mangling including the
  106. // address space.
  107. if (F->arg_size() == 2 && Name.startswith("objectsize.")) {
  108. Type *Tys[2] = { F->getReturnType(), F->arg_begin()->getType() };
  109. if (F->getName() != Intrinsic::getName(Intrinsic::objectsize, Tys)) {
  110. F->setName(Name + ".old");
  111. NewFn = Intrinsic::getDeclaration(F->getParent(),
  112. Intrinsic::objectsize, Tys);
  113. return true;
  114. }
  115. }
  116. break;
  117. #if 0 // HLSL Change - remove platform intrinsics
  118. case 'x': {
  119. if (Name.startswith("x86.sse2.pcmpeq.") ||
  120. Name.startswith("x86.sse2.pcmpgt.") ||
  121. Name.startswith("x86.avx2.pcmpeq.") ||
  122. Name.startswith("x86.avx2.pcmpgt.") ||
  123. Name.startswith("x86.avx.vpermil.") ||
  124. Name == "x86.avx.vinsertf128.pd.256" ||
  125. Name == "x86.avx.vinsertf128.ps.256" ||
  126. Name == "x86.avx.vinsertf128.si.256" ||
  127. Name == "x86.avx2.vinserti128" ||
  128. Name == "x86.avx.vextractf128.pd.256" ||
  129. Name == "x86.avx.vextractf128.ps.256" ||
  130. Name == "x86.avx.vextractf128.si.256" ||
  131. Name == "x86.avx2.vextracti128" ||
  132. Name == "x86.avx.movnt.dq.256" ||
  133. Name == "x86.avx.movnt.pd.256" ||
  134. Name == "x86.avx.movnt.ps.256" ||
  135. Name == "x86.sse42.crc32.64.8" ||
  136. Name == "x86.avx.vbroadcast.ss" ||
  137. Name == "x86.avx.vbroadcast.ss.256" ||
  138. Name == "x86.avx.vbroadcast.sd.256" ||
  139. Name == "x86.sse2.psll.dq" ||
  140. Name == "x86.sse2.psrl.dq" ||
  141. Name == "x86.avx2.psll.dq" ||
  142. Name == "x86.avx2.psrl.dq" ||
  143. Name == "x86.sse2.psll.dq.bs" ||
  144. Name == "x86.sse2.psrl.dq.bs" ||
  145. Name == "x86.avx2.psll.dq.bs" ||
  146. Name == "x86.avx2.psrl.dq.bs" ||
  147. Name == "x86.sse41.pblendw" ||
  148. Name == "x86.sse41.blendpd" ||
  149. Name == "x86.sse41.blendps" ||
  150. Name == "x86.avx.blend.pd.256" ||
  151. Name == "x86.avx.blend.ps.256" ||
  152. Name == "x86.avx2.pblendw" ||
  153. Name == "x86.avx2.pblendd.128" ||
  154. Name == "x86.avx2.pblendd.256" ||
  155. Name == "x86.avx2.vbroadcasti128" ||
  156. (Name.startswith("x86.xop.vpcom") && F->arg_size() == 2)) {
  157. NewFn = nullptr;
  158. return true;
  159. }
  160. // SSE4.1 ptest functions may have an old signature.
  161. if (Name.startswith("x86.sse41.ptest")) {
  162. if (Name == "x86.sse41.ptestc")
  163. return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn);
  164. if (Name == "x86.sse41.ptestz")
  165. return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn);
  166. if (Name == "x86.sse41.ptestnzc")
  167. return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn);
  168. }
  169. // Several blend and other instructions with masks used the wrong number of
  170. // bits.
  171. if (Name == "x86.sse41.insertps")
  172. return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_insertps,
  173. NewFn);
  174. if (Name == "x86.sse41.dppd")
  175. return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dppd,
  176. NewFn);
  177. if (Name == "x86.sse41.dpps")
  178. return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dpps,
  179. NewFn);
  180. if (Name == "x86.sse41.mpsadbw")
  181. return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_mpsadbw,
  182. NewFn);
  183. if (Name == "x86.avx.dp.ps.256")
  184. return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx_dp_ps_256,
  185. NewFn);
  186. if (Name == "x86.avx2.mpsadbw")
  187. return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_mpsadbw,
  188. NewFn);
  189. // frcz.ss/sd may need to have an argument dropped
  190. if (Name.startswith("x86.xop.vfrcz.ss") && F->arg_size() == 2) {
  191. F->setName(Name + ".old");
  192. NewFn = Intrinsic::getDeclaration(F->getParent(),
  193. Intrinsic::x86_xop_vfrcz_ss);
  194. return true;
  195. }
  196. if (Name.startswith("x86.xop.vfrcz.sd") && F->arg_size() == 2) {
  197. F->setName(Name + ".old");
  198. NewFn = Intrinsic::getDeclaration(F->getParent(),
  199. Intrinsic::x86_xop_vfrcz_sd);
  200. return true;
  201. }
  202. // Fix the FMA4 intrinsics to remove the 4
  203. if (Name.startswith("x86.fma4.")) {
  204. F->setName("llvm.x86.fma" + Name.substr(8));
  205. NewFn = F;
  206. return true;
  207. }
  208. break;
  209. }
  210. #endif // HLSL Change - remove platform intrinsics
  211. }
  212. // This may not belong here. This function is effectively being overloaded
  213. // to both detect an intrinsic which needs upgrading, and to provide the
  214. // upgraded form of the intrinsic. We should perhaps have two separate
  215. // functions for this.
  216. return false;
  217. }
  218. bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
  219. NewFn = nullptr;
  220. bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
  221. assert(F != NewFn && "Intrinsic function upgraded to the same function");
  222. // Upgrade intrinsic attributes. This does not change the function.
  223. if (NewFn)
  224. F = NewFn;
  225. if (Intrinsic::ID id = F->getIntrinsicID())
  226. F->setAttributes(Intrinsic::getAttributes(F->getContext(), id));
  227. return Upgraded;
  228. }
  229. bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
  230. // Nothing to do yet.
  231. return false;
  232. }
  233. // Handles upgrading SSE2 and AVX2 PSLLDQ intrinsics by converting them
  234. // to byte shuffles.
  235. static Value *UpgradeX86PSLLDQIntrinsics(IRBuilder<> &Builder, LLVMContext &C,
  236. Value *Op, unsigned NumLanes,
  237. unsigned Shift) {
  238. // Each lane is 16 bytes.
  239. unsigned NumElts = NumLanes * 16;
  240. // Bitcast from a 64-bit element type to a byte element type.
  241. Op = Builder.CreateBitCast(Op,
  242. VectorType::get(Type::getInt8Ty(C), NumElts),
  243. "cast");
  244. // We'll be shuffling in zeroes.
  245. Value *Res = ConstantVector::getSplat(NumElts, Builder.getInt8(0));
  246. // If shift is less than 16, emit a shuffle to move the bytes. Otherwise,
  247. // we'll just return the zero vector.
  248. if (Shift < 16) {
  249. SmallVector<Constant*, 32> Idxs;
  250. // 256-bit version is split into two 16-byte lanes.
  251. for (unsigned l = 0; l != NumElts; l += 16)
  252. for (unsigned i = 0; i != 16; ++i) {
  253. unsigned Idx = NumElts + i - Shift;
  254. if (Idx < NumElts)
  255. Idx -= NumElts - 16; // end of lane, switch operand.
  256. Idxs.push_back(Builder.getInt32(Idx + l));
  257. }
  258. Res = Builder.CreateShuffleVector(Res, Op, ConstantVector::get(Idxs));
  259. }
  260. // Bitcast back to a 64-bit element type.
  261. return Builder.CreateBitCast(Res,
  262. VectorType::get(Type::getInt64Ty(C), 2*NumLanes),
  263. "cast");
  264. }
  265. // Handles upgrading SSE2 and AVX2 PSRLDQ intrinsics by converting them
  266. // to byte shuffles.
  267. static Value *UpgradeX86PSRLDQIntrinsics(IRBuilder<> &Builder, LLVMContext &C,
  268. Value *Op, unsigned NumLanes,
  269. unsigned Shift) {
  270. // Each lane is 16 bytes.
  271. unsigned NumElts = NumLanes * 16;
  272. // Bitcast from a 64-bit element type to a byte element type.
  273. Op = Builder.CreateBitCast(Op,
  274. VectorType::get(Type::getInt8Ty(C), NumElts),
  275. "cast");
  276. // We'll be shuffling in zeroes.
  277. Value *Res = ConstantVector::getSplat(NumElts, Builder.getInt8(0));
  278. // If shift is less than 16, emit a shuffle to move the bytes. Otherwise,
  279. // we'll just return the zero vector.
  280. if (Shift < 16) {
  281. SmallVector<Constant*, 32> Idxs;
  282. // 256-bit version is split into two 16-byte lanes.
  283. for (unsigned l = 0; l != NumElts; l += 16)
  284. for (unsigned i = 0; i != 16; ++i) {
  285. unsigned Idx = i + Shift;
  286. if (Idx >= 16)
  287. Idx += NumElts - 16; // end of lane, switch operand.
  288. Idxs.push_back(Builder.getInt32(Idx + l));
  289. }
  290. Res = Builder.CreateShuffleVector(Op, Res, ConstantVector::get(Idxs));
  291. }
  292. // Bitcast back to a 64-bit element type.
  293. return Builder.CreateBitCast(Res,
  294. VectorType::get(Type::getInt64Ty(C), 2*NumLanes),
  295. "cast");
  296. }
  297. // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
  298. // upgraded intrinsic. All argument and return casting must be provided in
  299. // order to seamlessly integrate with existing context.
  300. void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
  301. Function *F = CI->getCalledFunction();
  302. LLVMContext &C = CI->getContext();
  303. IRBuilder<> Builder(C);
  304. Builder.SetInsertPoint(CI->getParent(), CI);
  305. assert(F && "Intrinsic call is not direct?");
  306. if (!NewFn) {
  307. (void)F; // HLSL Change - unused local variable
  308. #if 0 // HLSL Change - remove platform intrinsics
  309. // Get the Function's name.
  310. StringRef Name = F->getName();
  311. Value *Rep;
  312. // Upgrade packed integer vector compares intrinsics to compare instructions
  313. if (Name.startswith("llvm.x86.sse2.pcmpeq.") ||
  314. Name.startswith("llvm.x86.avx2.pcmpeq.")) {
  315. Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
  316. "pcmpeq");
  317. // need to sign extend since icmp returns vector of i1
  318. Rep = Builder.CreateSExt(Rep, CI->getType(), "");
  319. } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") ||
  320. Name.startswith("llvm.x86.avx2.pcmpgt.")) {
  321. Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
  322. "pcmpgt");
  323. // need to sign extend since icmp returns vector of i1
  324. Rep = Builder.CreateSExt(Rep, CI->getType(), "");
  325. } else if (Name == "llvm.x86.avx.movnt.dq.256" ||
  326. Name == "llvm.x86.avx.movnt.ps.256" ||
  327. Name == "llvm.x86.avx.movnt.pd.256") {
  328. IRBuilder<> Builder(C);
  329. Builder.SetInsertPoint(CI->getParent(), CI);
  330. Module *M = F->getParent();
  331. SmallVector<Metadata *, 1> Elts;
  332. Elts.push_back(
  333. ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
  334. MDNode *Node = MDNode::get(C, Elts);
  335. Value *Arg0 = CI->getArgOperand(0);
  336. Value *Arg1 = CI->getArgOperand(1);
  337. // Convert the type of the pointer to a pointer to the stored type.
  338. Value *BC = Builder.CreateBitCast(Arg0,
  339. PointerType::getUnqual(Arg1->getType()),
  340. "cast");
  341. StoreInst *SI = Builder.CreateStore(Arg1, BC);
  342. SI->setMetadata(M->getMDKindID("nontemporal"), Node);
  343. SI->setAlignment(16);
  344. // Remove intrinsic.
  345. CI->eraseFromParent();
  346. return;
  347. } else if (Name.startswith("llvm.x86.xop.vpcom")) {
  348. Intrinsic::ID intID;
  349. if (Name.endswith("ub"))
  350. intID = Intrinsic::x86_xop_vpcomub;
  351. else if (Name.endswith("uw"))
  352. intID = Intrinsic::x86_xop_vpcomuw;
  353. else if (Name.endswith("ud"))
  354. intID = Intrinsic::x86_xop_vpcomud;
  355. else if (Name.endswith("uq"))
  356. intID = Intrinsic::x86_xop_vpcomuq;
  357. else if (Name.endswith("b"))
  358. intID = Intrinsic::x86_xop_vpcomb;
  359. else if (Name.endswith("w"))
  360. intID = Intrinsic::x86_xop_vpcomw;
  361. else if (Name.endswith("d"))
  362. intID = Intrinsic::x86_xop_vpcomd;
  363. else if (Name.endswith("q"))
  364. intID = Intrinsic::x86_xop_vpcomq;
  365. else
  366. llvm_unreachable("Unknown suffix");
  367. Name = Name.substr(18); // strip off "llvm.x86.xop.vpcom"
  368. unsigned Imm;
  369. if (Name.startswith("lt"))
  370. Imm = 0;
  371. else if (Name.startswith("le"))
  372. Imm = 1;
  373. else if (Name.startswith("gt"))
  374. Imm = 2;
  375. else if (Name.startswith("ge"))
  376. Imm = 3;
  377. else if (Name.startswith("eq"))
  378. Imm = 4;
  379. else if (Name.startswith("ne"))
  380. Imm = 5;
  381. else if (Name.startswith("false"))
  382. Imm = 6;
  383. else if (Name.startswith("true"))
  384. Imm = 7;
  385. else
  386. llvm_unreachable("Unknown condition");
  387. Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
  388. Rep =
  389. Builder.CreateCall(VPCOM, {CI->getArgOperand(0), CI->getArgOperand(1),
  390. Builder.getInt8(Imm)});
  391. } else if (Name == "llvm.x86.sse42.crc32.64.8") {
  392. Function *CRC32 = Intrinsic::getDeclaration(F->getParent(),
  393. Intrinsic::x86_sse42_crc32_32_8);
  394. Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C));
  395. Rep = Builder.CreateCall(CRC32, {Trunc0, CI->getArgOperand(1)});
  396. Rep = Builder.CreateZExt(Rep, CI->getType(), "");
  397. } else if (Name.startswith("llvm.x86.avx.vbroadcast")) {
  398. // Replace broadcasts with a series of insertelements.
  399. Type *VecTy = CI->getType();
  400. Type *EltTy = VecTy->getVectorElementType();
  401. unsigned EltNum = VecTy->getVectorNumElements();
  402. Value *Cast = Builder.CreateBitCast(CI->getArgOperand(0),
  403. EltTy->getPointerTo());
  404. Value *Load = Builder.CreateLoad(EltTy, Cast);
  405. Type *I32Ty = Type::getInt32Ty(C);
  406. Rep = UndefValue::get(VecTy);
  407. for (unsigned I = 0; I < EltNum; ++I)
  408. Rep = Builder.CreateInsertElement(Rep, Load,
  409. ConstantInt::get(I32Ty, I));
  410. } else if (Name == "llvm.x86.avx2.vbroadcasti128") {
  411. // Replace vbroadcasts with a vector shuffle.
  412. Type *VT = VectorType::get(Type::getInt64Ty(C), 2);
  413. Value *Op = Builder.CreatePointerCast(CI->getArgOperand(0),
  414. PointerType::getUnqual(VT));
  415. Value *Load = Builder.CreateLoad(VT, Op);
  416. const int Idxs[4] = { 0, 1, 0, 1 };
  417. Rep = Builder.CreateShuffleVector(Load, UndefValue::get(Load->getType()),
  418. Idxs);
  419. } else if (Name == "llvm.x86.sse2.psll.dq") {
  420. // 128-bit shift left specified in bits.
  421. unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
  422. Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0), 1,
  423. Shift / 8); // Shift is in bits.
  424. } else if (Name == "llvm.x86.sse2.psrl.dq") {
  425. // 128-bit shift right specified in bits.
  426. unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
  427. Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0), 1,
  428. Shift / 8); // Shift is in bits.
  429. } else if (Name == "llvm.x86.avx2.psll.dq") {
  430. // 256-bit shift left specified in bits.
  431. unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
  432. Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0), 2,
  433. Shift / 8); // Shift is in bits.
  434. } else if (Name == "llvm.x86.avx2.psrl.dq") {
  435. // 256-bit shift right specified in bits.
  436. unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
  437. Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0), 2,
  438. Shift / 8); // Shift is in bits.
  439. } else if (Name == "llvm.x86.sse2.psll.dq.bs") {
  440. // 128-bit shift left specified in bytes.
  441. unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
  442. Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0), 1,
  443. Shift);
  444. } else if (Name == "llvm.x86.sse2.psrl.dq.bs") {
  445. // 128-bit shift right specified in bytes.
  446. unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
  447. Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0), 1,
  448. Shift);
  449. } else if (Name == "llvm.x86.avx2.psll.dq.bs") {
  450. // 256-bit shift left specified in bytes.
  451. unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
  452. Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0), 2,
  453. Shift);
  454. } else if (Name == "llvm.x86.avx2.psrl.dq.bs") {
  455. // 256-bit shift right specified in bytes.
  456. unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
  457. Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0), 2,
  458. Shift);
  459. } else if (Name == "llvm.x86.sse41.pblendw" ||
  460. Name == "llvm.x86.sse41.blendpd" ||
  461. Name == "llvm.x86.sse41.blendps" ||
  462. Name == "llvm.x86.avx.blend.pd.256" ||
  463. Name == "llvm.x86.avx.blend.ps.256" ||
  464. Name == "llvm.x86.avx2.pblendw" ||
  465. Name == "llvm.x86.avx2.pblendd.128" ||
  466. Name == "llvm.x86.avx2.pblendd.256") {
  467. Value *Op0 = CI->getArgOperand(0);
  468. Value *Op1 = CI->getArgOperand(1);
  469. unsigned Imm = cast <ConstantInt>(CI->getArgOperand(2))->getZExtValue();
  470. VectorType *VecTy = cast<VectorType>(CI->getType());
  471. unsigned NumElts = VecTy->getNumElements();
  472. SmallVector<Constant*, 16> Idxs;
  473. for (unsigned i = 0; i != NumElts; ++i) {
  474. unsigned Idx = ((Imm >> (i%8)) & 1) ? i + NumElts : i;
  475. Idxs.push_back(Builder.getInt32(Idx));
  476. }
  477. Rep = Builder.CreateShuffleVector(Op0, Op1, ConstantVector::get(Idxs));
  478. } else if (Name == "llvm.x86.avx.vinsertf128.pd.256" ||
  479. Name == "llvm.x86.avx.vinsertf128.ps.256" ||
  480. Name == "llvm.x86.avx.vinsertf128.si.256" ||
  481. Name == "llvm.x86.avx2.vinserti128") {
  482. Value *Op0 = CI->getArgOperand(0);
  483. Value *Op1 = CI->getArgOperand(1);
  484. unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
  485. VectorType *VecTy = cast<VectorType>(CI->getType());
  486. unsigned NumElts = VecTy->getNumElements();
  487. // Mask off the high bits of the immediate value; hardware ignores those.
  488. Imm = Imm & 1;
  489. // Extend the second operand into a vector that is twice as big.
  490. Value *UndefV = UndefValue::get(Op1->getType());
  491. SmallVector<Constant*, 8> Idxs;
  492. for (unsigned i = 0; i != NumElts; ++i) {
  493. Idxs.push_back(Builder.getInt32(i));
  494. }
  495. Rep = Builder.CreateShuffleVector(Op1, UndefV, ConstantVector::get(Idxs));
  496. // Insert the second operand into the first operand.
  497. // Note that there is no guarantee that instruction lowering will actually
  498. // produce a vinsertf128 instruction for the created shuffles. In
  499. // particular, the 0 immediate case involves no lane changes, so it can
  500. // be handled as a blend.
  501. // Example of shuffle mask for 32-bit elements:
  502. // Imm = 1 <i32 0, i32 1, i32 2, i32 3, i32 8, i32 9, i32 10, i32 11>
  503. // Imm = 0 <i32 8, i32 9, i32 10, i32 11, i32 4, i32 5, i32 6, i32 7 >
  504. SmallVector<Constant*, 8> Idxs2;
  505. // The low half of the result is either the low half of the 1st operand
  506. // or the low half of the 2nd operand (the inserted vector).
  507. for (unsigned i = 0; i != NumElts / 2; ++i) {
  508. unsigned Idx = Imm ? i : (i + NumElts);
  509. Idxs2.push_back(Builder.getInt32(Idx));
  510. }
  511. // The high half of the result is either the low half of the 2nd operand
  512. // (the inserted vector) or the high half of the 1st operand.
  513. for (unsigned i = NumElts / 2; i != NumElts; ++i) {
  514. unsigned Idx = Imm ? (i + NumElts / 2) : i;
  515. Idxs2.push_back(Builder.getInt32(Idx));
  516. }
  517. Rep = Builder.CreateShuffleVector(Op0, Rep, ConstantVector::get(Idxs2));
  518. } else if (Name == "llvm.x86.avx.vextractf128.pd.256" ||
  519. Name == "llvm.x86.avx.vextractf128.ps.256" ||
  520. Name == "llvm.x86.avx.vextractf128.si.256" ||
  521. Name == "llvm.x86.avx2.vextracti128") {
  522. Value *Op0 = CI->getArgOperand(0);
  523. unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
  524. VectorType *VecTy = cast<VectorType>(CI->getType());
  525. unsigned NumElts = VecTy->getNumElements();
  526. // Mask off the high bits of the immediate value; hardware ignores those.
  527. Imm = Imm & 1;
  528. // Get indexes for either the high half or low half of the input vector.
  529. SmallVector<Constant*, 4> Idxs(NumElts);
  530. for (unsigned i = 0; i != NumElts; ++i) {
  531. unsigned Idx = Imm ? (i + NumElts) : i;
  532. Idxs[i] = Builder.getInt32(Idx);
  533. }
  534. Value *UndefV = UndefValue::get(Op0->getType());
  535. Rep = Builder.CreateShuffleVector(Op0, UndefV, ConstantVector::get(Idxs));
  536. } else {
  537. bool PD128 = false, PD256 = false, PS128 = false, PS256 = false;
  538. if (Name == "llvm.x86.avx.vpermil.pd.256")
  539. PD256 = true;
  540. else if (Name == "llvm.x86.avx.vpermil.pd")
  541. PD128 = true;
  542. else if (Name == "llvm.x86.avx.vpermil.ps.256")
  543. PS256 = true;
  544. else if (Name == "llvm.x86.avx.vpermil.ps")
  545. PS128 = true;
  546. if (PD256 || PD128 || PS256 || PS128) {
  547. Value *Op0 = CI->getArgOperand(0);
  548. unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
  549. SmallVector<Constant*, 8> Idxs;
  550. if (PD128)
  551. for (unsigned i = 0; i != 2; ++i)
  552. Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1));
  553. else if (PD256)
  554. for (unsigned l = 0; l != 4; l+=2)
  555. for (unsigned i = 0; i != 2; ++i)
  556. Idxs.push_back(Builder.getInt32(((Imm >> (l+i)) & 0x1) + l));
  557. else if (PS128)
  558. for (unsigned i = 0; i != 4; ++i)
  559. Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3));
  560. else if (PS256)
  561. for (unsigned l = 0; l != 8; l+=4)
  562. for (unsigned i = 0; i != 4; ++i)
  563. Idxs.push_back(Builder.getInt32(((Imm >> (2 * i)) & 0x3) + l));
  564. else
  565. llvm_unreachable("Unexpected function");
  566. Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs));
  567. } else {
  568. llvm_unreachable("Unknown function for CallInst upgrade.");
  569. }
  570. }
  571. CI->replaceAllUsesWith(Rep);
  572. CI->eraseFromParent();
  573. #endif // HLSL Change - remove platform intrinsics
  574. llvm_unreachable("HLSL - should not be upgrading platform intrinsics."); // HLSL Change - remove platform intrinsics
  575. return;
  576. }
  577. std::string Name = CI->getName();
  578. if (!Name.empty())
  579. CI->setName(Name + ".old");
  580. switch (NewFn->getIntrinsicID()) {
  581. default:
  582. llvm_unreachable("Unknown function for CallInst upgrade.");
  583. case Intrinsic::ctlz:
  584. case Intrinsic::cttz:
  585. assert(CI->getNumArgOperands() == 1 &&
  586. "Mismatch between function args and call args");
  587. CI->replaceAllUsesWith(Builder.CreateCall(
  588. NewFn, {CI->getArgOperand(0), Builder.getFalse()}, Name));
  589. CI->eraseFromParent();
  590. return;
  591. case Intrinsic::objectsize:
  592. CI->replaceAllUsesWith(Builder.CreateCall(
  593. NewFn, {CI->getArgOperand(0), CI->getArgOperand(1)}, Name));
  594. CI->eraseFromParent();
  595. return;
  596. case Intrinsic::ctpop: {
  597. CI->replaceAllUsesWith(Builder.CreateCall(NewFn, {CI->getArgOperand(0)}));
  598. CI->eraseFromParent();
  599. return;
  600. }
  601. #if 0 // HLSL Change - remove platform intrinsics
  602. case Intrinsic::x86_xop_vfrcz_ss:
  603. case Intrinsic::x86_xop_vfrcz_sd:
  604. CI->replaceAllUsesWith(
  605. Builder.CreateCall(NewFn, {CI->getArgOperand(1)}, Name));
  606. CI->eraseFromParent();
  607. return;
  608. case Intrinsic::x86_sse41_ptestc:
  609. case Intrinsic::x86_sse41_ptestz:
  610. case Intrinsic::x86_sse41_ptestnzc: {
  611. // The arguments for these intrinsics used to be v4f32, and changed
  612. // to v2i64. This is purely a nop, since those are bitwise intrinsics.
  613. // So, the only thing required is a bitcast for both arguments.
  614. // First, check the arguments have the old type.
  615. Value *Arg0 = CI->getArgOperand(0);
  616. if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4))
  617. return;
  618. // Old intrinsic, add bitcasts
  619. Value *Arg1 = CI->getArgOperand(1);
  620. Type *NewVecTy = VectorType::get(Type::getInt64Ty(C), 2);
  621. Value *BC0 = Builder.CreateBitCast(Arg0, NewVecTy, "cast");
  622. Value *BC1 = Builder.CreateBitCast(Arg1, NewVecTy, "cast");
  623. CallInst *NewCall = Builder.CreateCall(NewFn, {BC0, BC1}, Name);
  624. CI->replaceAllUsesWith(NewCall);
  625. CI->eraseFromParent();
  626. return;
  627. }
  628. case Intrinsic::x86_sse41_insertps:
  629. case Intrinsic::x86_sse41_dppd:
  630. case Intrinsic::x86_sse41_dpps:
  631. case Intrinsic::x86_sse41_mpsadbw:
  632. case Intrinsic::x86_avx_dp_ps_256:
  633. case Intrinsic::x86_avx2_mpsadbw: {
  634. // Need to truncate the last argument from i32 to i8 -- this argument models
  635. // an inherently 8-bit immediate operand to these x86 instructions.
  636. SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
  637. CI->arg_operands().end());
  638. // Replace the last argument with a trunc.
  639. Args.back() = Builder.CreateTrunc(Args.back(), Type::getInt8Ty(C), "trunc");
  640. CallInst *NewCall = Builder.CreateCall(NewFn, Args);
  641. CI->replaceAllUsesWith(NewCall);
  642. CI->eraseFromParent();
  643. return;
  644. }
  645. #endif // HLSL Change - remove platform intrinsics
  646. }
  647. }
  648. // This tests each Function to determine if it needs upgrading. When we find
  649. // one we are interested in, we then upgrade all calls to reflect the new
  650. // function.
  651. void llvm::UpgradeCallsToIntrinsic(Function* F) {
  652. assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
  653. // Upgrade the function and check if it is a totaly new function.
  654. Function *NewFn;
  655. if (UpgradeIntrinsicFunction(F, NewFn)) {
  656. // Replace all uses to the old function with the new one if necessary.
  657. for (Value::user_iterator UI = F->user_begin(), UE = F->user_end();
  658. UI != UE;) {
  659. if (CallInst *CI = dyn_cast<CallInst>(*UI++))
  660. UpgradeIntrinsicCall(CI, NewFn);
  661. }
  662. // Remove old function, no longer used, from the module.
  663. F->eraseFromParent();
  664. }
  665. }
  666. void llvm::UpgradeInstWithTBAATag(Instruction *I) {
  667. MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa);
  668. assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
  669. // Check if the tag uses struct-path aware TBAA format.
  670. if (isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3)
  671. return;
  672. if (MD->getNumOperands() == 3) {
  673. Metadata *Elts[] = {MD->getOperand(0), MD->getOperand(1)};
  674. MDNode *ScalarType = MDNode::get(I->getContext(), Elts);
  675. // Create a MDNode <ScalarType, ScalarType, offset 0, const>
  676. Metadata *Elts2[] = {ScalarType, ScalarType,
  677. ConstantAsMetadata::get(Constant::getNullValue(
  678. Type::getInt64Ty(I->getContext()))),
  679. MD->getOperand(2)};
  680. I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2));
  681. } else {
  682. // Create a MDNode <MD, MD, offset 0>
  683. Metadata *Elts[] = {MD, MD, ConstantAsMetadata::get(Constant::getNullValue(
  684. Type::getInt64Ty(I->getContext())))};
  685. I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts));
  686. }
  687. }
  688. Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,
  689. Instruction *&Temp) {
  690. if (Opc != Instruction::BitCast)
  691. return nullptr;
  692. Temp = nullptr;
  693. Type *SrcTy = V->getType();
  694. if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
  695. SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
  696. LLVMContext &Context = V->getContext();
  697. // We have no information about target data layout, so we assume that
  698. // the maximum pointer size is 64bit.
  699. Type *MidTy = Type::getInt64Ty(Context);
  700. Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy);
  701. return CastInst::Create(Instruction::IntToPtr, Temp, DestTy);
  702. }
  703. return nullptr;
  704. }
  705. Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) {
  706. if (Opc != Instruction::BitCast)
  707. return nullptr;
  708. Type *SrcTy = C->getType();
  709. if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
  710. SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
  711. LLVMContext &Context = C->getContext();
  712. // We have no information about target data layout, so we assume that
  713. // the maximum pointer size is 64bit.
  714. Type *MidTy = Type::getInt64Ty(Context);
  715. return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy),
  716. DestTy);
  717. }
  718. return nullptr;
  719. }
  720. /// Check the debug info version number, if it is out-dated, drop the debug
  721. /// info. Return true if module is modified.
  722. bool llvm::UpgradeDebugInfo(Module &M) {
  723. unsigned Version = getDebugMetadataVersionFromModule(M);
  724. if (Version == DEBUG_METADATA_VERSION)
  725. return false;
  726. bool RetCode = StripDebugInfo(M);
  727. if (RetCode) {
  728. DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version);
  729. M.getContext().diagnose(DiagVersion);
  730. }
  731. return RetCode;
  732. }
  733. void llvm::UpgradeMDStringConstant(std::string &String) {
  734. const std::string OldPrefix = "llvm.vectorizer.";
  735. if (String == "llvm.vectorizer.unroll") {
  736. String = "llvm.loop.interleave.count";
  737. } else if (String.find(OldPrefix) == 0) {
  738. String.replace(0, OldPrefix.size(), "llvm.loop.vectorize.");
  739. }
  740. }