AArch64LegalizerInfo.cpp 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  1. //===- AArch64LegalizerInfo.cpp ----------------------------------*- C++ -*-==//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. /// \file
  9. /// This file implements the targeting of the Machinelegalizer class for
  10. /// AArch64.
  11. /// \todo This should be generated by TableGen.
  12. //===----------------------------------------------------------------------===//
  13. #include "AArch64LegalizerInfo.h"
  14. #include "AArch64RegisterBankInfo.h"
  15. #include "AArch64Subtarget.h"
  16. #include "llvm/CodeGen/GlobalISel/LegalizerHelper.h"
  17. #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
  18. #include "llvm/CodeGen/GlobalISel/MIPatternMatch.h"
  19. #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
  20. #include "llvm/CodeGen/GlobalISel/Utils.h"
  21. #include "llvm/CodeGen/MachineInstr.h"
  22. #include "llvm/CodeGen/MachineRegisterInfo.h"
  23. #include "llvm/CodeGen/TargetOpcodes.h"
  24. #include "llvm/CodeGen/ValueTypes.h"
  25. #include "llvm/IR/DerivedTypes.h"
  26. #include "llvm/IR/Intrinsics.h"
  27. #include "llvm/IR/IntrinsicsAArch64.h"
  28. #include "llvm/IR/Type.h"
  29. #include "llvm/Support/MathExtras.h"
  30. #include <initializer_list>
  31. #define DEBUG_TYPE "aarch64-legalinfo"
  32. using namespace llvm;
  33. using namespace LegalizeActions;
  34. using namespace LegalizeMutations;
  35. using namespace LegalityPredicates;
  36. using namespace MIPatternMatch;
  37. AArch64LegalizerInfo::AArch64LegalizerInfo(const AArch64Subtarget &ST)
  38. : ST(&ST) {
  39. using namespace TargetOpcode;
  40. const LLT p0 = LLT::pointer(0, 64);
  41. const LLT s1 = LLT::scalar(1);
  42. const LLT s8 = LLT::scalar(8);
  43. const LLT s16 = LLT::scalar(16);
  44. const LLT s32 = LLT::scalar(32);
  45. const LLT s64 = LLT::scalar(64);
  46. const LLT s128 = LLT::scalar(128);
  47. const LLT v16s8 = LLT::fixed_vector(16, 8);
  48. const LLT v8s8 = LLT::fixed_vector(8, 8);
  49. const LLT v4s8 = LLT::fixed_vector(4, 8);
  50. const LLT v8s16 = LLT::fixed_vector(8, 16);
  51. const LLT v4s16 = LLT::fixed_vector(4, 16);
  52. const LLT v2s16 = LLT::fixed_vector(2, 16);
  53. const LLT v2s32 = LLT::fixed_vector(2, 32);
  54. const LLT v4s32 = LLT::fixed_vector(4, 32);
  55. const LLT v2s64 = LLT::fixed_vector(2, 64);
  56. const LLT v2p0 = LLT::fixed_vector(2, p0);
  57. std::initializer_list<LLT> PackedVectorAllTypeList = {/* Begin 128bit types */
  58. v16s8, v8s16, v4s32,
  59. v2s64, v2p0,
  60. /* End 128bit types */
  61. /* Begin 64bit types */
  62. v8s8, v4s16, v2s32};
  63. const TargetMachine &TM = ST.getTargetLowering()->getTargetMachine();
  64. // FIXME: support subtargets which have neon/fp-armv8 disabled.
  65. if (!ST.hasNEON() || !ST.hasFPARMv8()) {
  66. getLegacyLegalizerInfo().computeTables();
  67. return;
  68. }
  69. // Some instructions only support s16 if the subtarget has full 16-bit FP
  70. // support.
  71. const bool HasFP16 = ST.hasFullFP16();
  72. const LLT &MinFPScalar = HasFP16 ? s16 : s32;
  73. getActionDefinitionsBuilder({G_IMPLICIT_DEF, G_FREEZE})
  74. .legalFor({p0, s1, s8, s16, s32, s64})
  75. .legalFor(PackedVectorAllTypeList)
  76. .widenScalarToNextPow2(0)
  77. .clampScalar(0, s8, s64)
  78. .fewerElementsIf(
  79. [=](const LegalityQuery &Query) {
  80. return Query.Types[0].isVector() &&
  81. (Query.Types[0].getElementType() != s64 ||
  82. Query.Types[0].getNumElements() != 2);
  83. },
  84. [=](const LegalityQuery &Query) {
  85. LLT EltTy = Query.Types[0].getElementType();
  86. if (EltTy == s64)
  87. return std::make_pair(0, LLT::fixed_vector(2, 64));
  88. return std::make_pair(0, EltTy);
  89. });
  90. getActionDefinitionsBuilder(G_PHI)
  91. .legalFor({p0, s16, s32, s64})
  92. .legalFor(PackedVectorAllTypeList)
  93. .widenScalarToNextPow2(0)
  94. .clampScalar(0, s16, s64)
  95. // Maximum: sN * k = 128
  96. .clampMaxNumElements(0, s8, 16)
  97. .clampMaxNumElements(0, s16, 8)
  98. .clampMaxNumElements(0, s32, 4)
  99. .clampMaxNumElements(0, s64, 2)
  100. .clampMaxNumElements(0, p0, 2);
  101. getActionDefinitionsBuilder(G_BSWAP)
  102. .legalFor({s32, s64, v4s32, v2s32, v2s64})
  103. .widenScalarToNextPow2(0)
  104. .clampScalar(0, s32, s64);
  105. getActionDefinitionsBuilder({G_ADD, G_SUB, G_MUL, G_AND, G_OR, G_XOR})
  106. .legalFor({s32, s64, v2s32, v4s32, v4s16, v8s16, v16s8, v8s8})
  107. .scalarizeIf(
  108. [=](const LegalityQuery &Query) {
  109. return Query.Opcode == G_MUL && Query.Types[0] == v2s64;
  110. },
  111. 0)
  112. .legalFor({v2s64})
  113. .widenScalarToNextPow2(0)
  114. .clampScalar(0, s32, s64)
  115. .clampNumElements(0, v2s32, v4s32)
  116. .clampNumElements(0, v2s64, v2s64)
  117. .moreElementsToNextPow2(0);
  118. getActionDefinitionsBuilder({G_SHL, G_ASHR, G_LSHR})
  119. .customIf([=](const LegalityQuery &Query) {
  120. const auto &SrcTy = Query.Types[0];
  121. const auto &AmtTy = Query.Types[1];
  122. return !SrcTy.isVector() && SrcTy.getSizeInBits() == 32 &&
  123. AmtTy.getSizeInBits() == 32;
  124. })
  125. .legalFor({
  126. {s32, s32},
  127. {s32, s64},
  128. {s64, s64},
  129. {v8s8, v8s8},
  130. {v16s8, v16s8},
  131. {v4s16, v4s16},
  132. {v8s16, v8s16},
  133. {v2s32, v2s32},
  134. {v4s32, v4s32},
  135. {v2s64, v2s64},
  136. })
  137. .widenScalarToNextPow2(0)
  138. .clampScalar(1, s32, s64)
  139. .clampScalar(0, s32, s64)
  140. .clampNumElements(0, v2s32, v4s32)
  141. .clampNumElements(0, v2s64, v2s64)
  142. .moreElementsToNextPow2(0)
  143. .minScalarSameAs(1, 0);
  144. getActionDefinitionsBuilder(G_PTR_ADD)
  145. .legalFor({{p0, s64}, {v2p0, v2s64}})
  146. .clampScalar(1, s64, s64);
  147. getActionDefinitionsBuilder(G_PTRMASK).legalFor({{p0, s64}});
  148. getActionDefinitionsBuilder({G_SDIV, G_UDIV})
  149. .legalFor({s32, s64})
  150. .libcallFor({s128})
  151. .clampScalar(0, s32, s64)
  152. .widenScalarToNextPow2(0)
  153. .scalarize(0);
  154. getActionDefinitionsBuilder({G_SREM, G_UREM, G_SDIVREM, G_UDIVREM})
  155. .lowerFor({s1, s8, s16, s32, s64, v2s64, v4s32, v2s32})
  156. .widenScalarOrEltToNextPow2(0)
  157. .clampScalarOrElt(0, s32, s64)
  158. .clampNumElements(0, v2s32, v4s32)
  159. .clampNumElements(0, v2s64, v2s64)
  160. .moreElementsToNextPow2(0);
  161. getActionDefinitionsBuilder({G_SMULO, G_UMULO})
  162. .widenScalarToNextPow2(0, /*Min = */ 32)
  163. .clampScalar(0, s32, s64)
  164. .lowerIf(typeIs(1, s1));
  165. getActionDefinitionsBuilder({G_SMULH, G_UMULH})
  166. .legalFor({s64, v8s16, v16s8, v4s32})
  167. .lower();
  168. getActionDefinitionsBuilder({G_SMIN, G_SMAX, G_UMIN, G_UMAX})
  169. .legalFor({v8s8, v16s8, v4s16, v8s16, v2s32, v4s32})
  170. .clampNumElements(0, v8s8, v16s8)
  171. .clampNumElements(0, v4s16, v8s16)
  172. .clampNumElements(0, v2s32, v4s32)
  173. // FIXME: This sholdn't be needed as v2s64 types are going to
  174. // be expanded anyway, but G_ICMP doesn't support splitting vectors yet
  175. .clampNumElements(0, v2s64, v2s64)
  176. .lower();
  177. getActionDefinitionsBuilder(
  178. {G_SADDE, G_SSUBE, G_UADDE, G_USUBE, G_SADDO, G_SSUBO, G_UADDO, G_USUBO})
  179. .legalFor({{s32, s1}, {s64, s1}})
  180. .clampScalar(0, s32, s64)
  181. .widenScalarToNextPow2(0);
  182. getActionDefinitionsBuilder({G_FADD, G_FSUB, G_FMUL, G_FDIV, G_FNEG})
  183. .legalFor({MinFPScalar, s32, s64, v2s64, v4s32, v2s32})
  184. .clampScalar(0, MinFPScalar, s64)
  185. .clampNumElements(0, v2s32, v4s32)
  186. .clampNumElements(0, v2s64, v2s64);
  187. getActionDefinitionsBuilder(G_FREM).libcallFor({s32, s64});
  188. getActionDefinitionsBuilder({G_FCEIL, G_FABS, G_FSQRT, G_FFLOOR, G_FRINT,
  189. G_FMA, G_INTRINSIC_TRUNC, G_INTRINSIC_ROUND,
  190. G_FNEARBYINT, G_INTRINSIC_LRINT})
  191. // If we don't have full FP16 support, then scalarize the elements of
  192. // vectors containing fp16 types.
  193. .fewerElementsIf(
  194. [=, &ST](const LegalityQuery &Query) {
  195. const auto &Ty = Query.Types[0];
  196. return Ty.isVector() && Ty.getElementType() == s16 &&
  197. !ST.hasFullFP16();
  198. },
  199. [=](const LegalityQuery &Query) { return std::make_pair(0, s16); })
  200. // If we don't have full FP16 support, then widen s16 to s32 if we
  201. // encounter it.
  202. .widenScalarIf(
  203. [=, &ST](const LegalityQuery &Query) {
  204. return Query.Types[0] == s16 && !ST.hasFullFP16();
  205. },
  206. [=](const LegalityQuery &Query) { return std::make_pair(0, s32); })
  207. .legalFor({s16, s32, s64, v2s32, v4s32, v2s64, v2s16, v4s16, v8s16});
  208. getActionDefinitionsBuilder(
  209. {G_FCOS, G_FSIN, G_FLOG10, G_FLOG, G_FLOG2, G_FEXP, G_FEXP2, G_FPOW})
  210. // We need a call for these, so we always need to scalarize.
  211. .scalarize(0)
  212. // Regardless of FP16 support, widen 16-bit elements to 32-bits.
  213. .minScalar(0, s32)
  214. .libcallFor({s32, s64, v2s32, v4s32, v2s64});
  215. getActionDefinitionsBuilder(G_INSERT)
  216. .legalIf(all(typeInSet(0, {s32, s64, p0}),
  217. typeInSet(1, {s1, s8, s16, s32}), smallerThan(1, 0)))
  218. .widenScalarToNextPow2(0)
  219. .clampScalar(0, s32, s64)
  220. .widenScalarToNextPow2(1)
  221. .minScalar(1, s8)
  222. .maxScalarIf(typeInSet(0, {s32}), 1, s16)
  223. .maxScalarIf(typeInSet(0, {s64, p0}), 1, s32);
  224. getActionDefinitionsBuilder(G_EXTRACT)
  225. .legalIf(all(typeInSet(0, {s16, s32, s64, p0}),
  226. typeInSet(1, {s32, s64, s128, p0}), smallerThan(0, 1)))
  227. .widenScalarToNextPow2(1)
  228. .clampScalar(1, s32, s128)
  229. .widenScalarToNextPow2(0)
  230. .minScalar(0, s16)
  231. .maxScalarIf(typeInSet(1, {s32}), 0, s16)
  232. .maxScalarIf(typeInSet(1, {s64, p0}), 0, s32)
  233. .maxScalarIf(typeInSet(1, {s128}), 0, s64);
  234. getActionDefinitionsBuilder({G_SEXTLOAD, G_ZEXTLOAD})
  235. .lowerIf(atomicOrderingAtLeastOrStrongerThan(0, AtomicOrdering::Unordered))
  236. .legalForTypesWithMemDesc({{s32, p0, s8, 8},
  237. {s32, p0, s16, 8},
  238. {s32, p0, s32, 8},
  239. {s64, p0, s8, 2},
  240. {s64, p0, s16, 2},
  241. {s64, p0, s32, 4},
  242. {s64, p0, s64, 8},
  243. {p0, p0, s64, 8},
  244. {v2s32, p0, s64, 8}})
  245. .widenScalarToNextPow2(0)
  246. .clampScalar(0, s32, s64)
  247. // TODO: We could support sum-of-pow2's but the lowering code doesn't know
  248. // how to do that yet.
  249. .unsupportedIfMemSizeNotPow2()
  250. // Lower anything left over into G_*EXT and G_LOAD
  251. .lower();
  252. auto IsPtrVecPred = [=](const LegalityQuery &Query) {
  253. const LLT &ValTy = Query.Types[0];
  254. if (!ValTy.isVector())
  255. return false;
  256. const LLT EltTy = ValTy.getElementType();
  257. return EltTy.isPointer() && EltTy.getAddressSpace() == 0;
  258. };
  259. getActionDefinitionsBuilder(G_LOAD)
  260. .customIf([=](const LegalityQuery &Query) {
  261. return Query.Types[0] == s128 &&
  262. Query.MMODescrs[0].Ordering != AtomicOrdering::NotAtomic;
  263. })
  264. .legalForTypesWithMemDesc({{s8, p0, s8, 8},
  265. {s16, p0, s16, 8},
  266. {s32, p0, s32, 8},
  267. {s64, p0, s64, 8},
  268. {p0, p0, s64, 8},
  269. {s128, p0, s128, 8},
  270. {v8s8, p0, s64, 8},
  271. {v16s8, p0, s128, 8},
  272. {v4s16, p0, s64, 8},
  273. {v8s16, p0, s128, 8},
  274. {v2s32, p0, s64, 8},
  275. {v4s32, p0, s128, 8},
  276. {v2s64, p0, s128, 8}})
  277. // These extends are also legal
  278. .legalForTypesWithMemDesc({{s32, p0, s8, 8}, {s32, p0, s16, 8}})
  279. .widenScalarToNextPow2(0, /* MinSize = */8)
  280. .lowerIfMemSizeNotPow2()
  281. .clampScalar(0, s8, s64)
  282. .narrowScalarIf([=](const LegalityQuery &Query) {
  283. // Clamp extending load results to 32-bits.
  284. return Query.Types[0].isScalar() &&
  285. Query.Types[0] != Query.MMODescrs[0].MemoryTy &&
  286. Query.Types[0].getSizeInBits() > 32;
  287. },
  288. changeTo(0, s32))
  289. // Lower any any-extending loads left into G_ANYEXT and G_LOAD
  290. .lowerIf([=](const LegalityQuery &Query) {
  291. return Query.Types[0] != Query.MMODescrs[0].MemoryTy;
  292. })
  293. .clampMaxNumElements(0, s8, 16)
  294. .clampMaxNumElements(0, s16, 8)
  295. .clampMaxNumElements(0, s32, 4)
  296. .clampMaxNumElements(0, s64, 2)
  297. .clampMaxNumElements(0, p0, 2)
  298. .customIf(IsPtrVecPred)
  299. .scalarizeIf(typeIs(0, v2s16), 0);
  300. getActionDefinitionsBuilder(G_STORE)
  301. .customIf([=](const LegalityQuery &Query) {
  302. return Query.Types[0] == s128 &&
  303. Query.MMODescrs[0].Ordering != AtomicOrdering::NotAtomic;
  304. })
  305. .legalForTypesWithMemDesc({{s8, p0, s8, 8},
  306. {s16, p0, s8, 8}, // truncstorei8 from s16
  307. {s32, p0, s8, 8}, // truncstorei8 from s32
  308. {s64, p0, s8, 8}, // truncstorei8 from s64
  309. {s16, p0, s16, 8},
  310. {s32, p0, s16, 8}, // truncstorei16 from s32
  311. {s64, p0, s16, 8}, // truncstorei16 from s64
  312. {s32, p0, s8, 8},
  313. {s32, p0, s16, 8},
  314. {s32, p0, s32, 8},
  315. {s64, p0, s64, 8},
  316. {s64, p0, s32, 8}, // truncstorei32 from s64
  317. {p0, p0, s64, 8},
  318. {s128, p0, s128, 8},
  319. {v16s8, p0, s128, 8},
  320. {v8s8, p0, s64, 8},
  321. {v4s16, p0, s64, 8},
  322. {v8s16, p0, s128, 8},
  323. {v2s32, p0, s64, 8},
  324. {v4s32, p0, s128, 8},
  325. {v2s64, p0, s128, 8}})
  326. .clampScalar(0, s8, s64)
  327. .lowerIf([=](const LegalityQuery &Query) {
  328. return Query.Types[0].isScalar() &&
  329. Query.Types[0] != Query.MMODescrs[0].MemoryTy;
  330. })
  331. // Maximum: sN * k = 128
  332. .clampMaxNumElements(0, s8, 16)
  333. .clampMaxNumElements(0, s16, 8)
  334. .clampMaxNumElements(0, s32, 4)
  335. .clampMaxNumElements(0, s64, 2)
  336. .clampMaxNumElements(0, p0, 2)
  337. .lowerIfMemSizeNotPow2()
  338. .customIf(IsPtrVecPred)
  339. .scalarizeIf(typeIs(0, v2s16), 0);
  340. // Constants
  341. getActionDefinitionsBuilder(G_CONSTANT)
  342. .legalFor({p0, s8, s16, s32, s64})
  343. .widenScalarToNextPow2(0)
  344. .clampScalar(0, s8, s64);
  345. getActionDefinitionsBuilder(G_FCONSTANT)
  346. .legalIf([=](const LegalityQuery &Query) {
  347. const auto &Ty = Query.Types[0];
  348. if (HasFP16 && Ty == s16)
  349. return true;
  350. return Ty == s32 || Ty == s64 || Ty == s128;
  351. })
  352. .clampScalar(0, MinFPScalar, s128);
  353. getActionDefinitionsBuilder({G_ICMP, G_FCMP})
  354. .legalFor({{s32, s32},
  355. {s32, s64},
  356. {s32, p0},
  357. {v4s32, v4s32},
  358. {v2s32, v2s32},
  359. {v2s64, v2s64},
  360. {v2s64, v2p0},
  361. {v4s16, v4s16},
  362. {v8s16, v8s16},
  363. {v8s8, v8s8},
  364. {v16s8, v16s8}})
  365. .widenScalarOrEltToNextPow2(1)
  366. .clampScalar(1, s32, s64)
  367. .clampScalar(0, s32, s32)
  368. .minScalarEltSameAsIf(
  369. [=](const LegalityQuery &Query) {
  370. const LLT &Ty = Query.Types[0];
  371. const LLT &SrcTy = Query.Types[1];
  372. return Ty.isVector() && !SrcTy.getElementType().isPointer() &&
  373. Ty.getElementType() != SrcTy.getElementType();
  374. },
  375. 0, 1)
  376. .minScalarOrEltIf(
  377. [=](const LegalityQuery &Query) { return Query.Types[1] == v2s16; },
  378. 1, s32)
  379. .minScalarOrEltIf(
  380. [=](const LegalityQuery &Query) { return Query.Types[1] == v2p0; }, 0,
  381. s64)
  382. .clampNumElements(0, v2s32, v4s32);
  383. // Extensions
  384. auto ExtLegalFunc = [=](const LegalityQuery &Query) {
  385. unsigned DstSize = Query.Types[0].getSizeInBits();
  386. if (DstSize == 128 && !Query.Types[0].isVector())
  387. return false; // Extending to a scalar s128 needs narrowing.
  388. // Make sure that we have something that will fit in a register, and
  389. // make sure it's a power of 2.
  390. if (DstSize < 8 || DstSize > 128 || !isPowerOf2_32(DstSize))
  391. return false;
  392. const LLT &SrcTy = Query.Types[1];
  393. // Special case for s1.
  394. if (SrcTy == s1)
  395. return true;
  396. // Make sure we fit in a register otherwise. Don't bother checking that
  397. // the source type is below 128 bits. We shouldn't be allowing anything
  398. // through which is wider than the destination in the first place.
  399. unsigned SrcSize = SrcTy.getSizeInBits();
  400. if (SrcSize < 8 || !isPowerOf2_32(SrcSize))
  401. return false;
  402. return true;
  403. };
  404. getActionDefinitionsBuilder({G_ZEXT, G_SEXT, G_ANYEXT})
  405. .legalIf(ExtLegalFunc)
  406. .clampScalar(0, s64, s64); // Just for s128, others are handled above.
  407. getActionDefinitionsBuilder(G_TRUNC)
  408. .minScalarOrEltIf(
  409. [=](const LegalityQuery &Query) { return Query.Types[0].isVector(); },
  410. 0, s8)
  411. .customIf([=](const LegalityQuery &Query) {
  412. LLT DstTy = Query.Types[0];
  413. LLT SrcTy = Query.Types[1];
  414. return DstTy == v8s8 && SrcTy.getSizeInBits() > 128;
  415. })
  416. .alwaysLegal();
  417. getActionDefinitionsBuilder(G_SEXT_INREG).legalFor({s32, s64}).lower();
  418. // FP conversions
  419. getActionDefinitionsBuilder(G_FPTRUNC)
  420. .legalFor(
  421. {{s16, s32}, {s16, s64}, {s32, s64}, {v4s16, v4s32}, {v2s32, v2s64}})
  422. .clampMaxNumElements(0, s32, 2);
  423. getActionDefinitionsBuilder(G_FPEXT)
  424. .legalFor(
  425. {{s32, s16}, {s64, s16}, {s64, s32}, {v4s32, v4s16}, {v2s64, v2s32}})
  426. .clampMaxNumElements(0, s64, 2);
  427. // Conversions
  428. getActionDefinitionsBuilder({G_FPTOSI, G_FPTOUI})
  429. .legalForCartesianProduct({s32, s64, v2s64, v4s32, v2s32})
  430. .widenScalarToNextPow2(0)
  431. .clampScalar(0, s32, s64)
  432. .widenScalarToNextPow2(1)
  433. .clampScalar(1, s32, s64);
  434. getActionDefinitionsBuilder({G_SITOFP, G_UITOFP})
  435. .legalForCartesianProduct({s32, s64, v2s64, v4s32, v2s32})
  436. .clampScalar(1, s32, s64)
  437. .minScalarSameAs(1, 0)
  438. .clampScalar(0, s32, s64)
  439. .widenScalarToNextPow2(0);
  440. // Control-flow
  441. getActionDefinitionsBuilder(G_BRCOND).legalFor({s1, s8, s16, s32});
  442. getActionDefinitionsBuilder(G_BRINDIRECT).legalFor({p0});
  443. getActionDefinitionsBuilder(G_SELECT)
  444. .legalFor({{s32, s1}, {s64, s1}, {p0, s1}})
  445. .widenScalarToNextPow2(0)
  446. .clampScalar(0, s32, s64)
  447. .minScalarEltSameAsIf(all(isVector(0), isVector(1)), 1, 0)
  448. .lowerIf(isVector(0));
  449. // Pointer-handling
  450. getActionDefinitionsBuilder(G_FRAME_INDEX).legalFor({p0});
  451. if (TM.getCodeModel() == CodeModel::Small)
  452. getActionDefinitionsBuilder(G_GLOBAL_VALUE).custom();
  453. else
  454. getActionDefinitionsBuilder(G_GLOBAL_VALUE).legalFor({p0});
  455. getActionDefinitionsBuilder(G_PTRTOINT)
  456. .legalForCartesianProduct({s1, s8, s16, s32, s64}, {p0})
  457. .legalFor({{v2s64, v2p0}})
  458. .maxScalar(0, s64)
  459. .widenScalarToNextPow2(0, /*Min*/ 8);
  460. getActionDefinitionsBuilder(G_INTTOPTR)
  461. .unsupportedIf([&](const LegalityQuery &Query) {
  462. return Query.Types[0].getSizeInBits() != Query.Types[1].getSizeInBits();
  463. })
  464. .legalFor({{p0, s64}, {v2p0, v2s64}});
  465. // Casts for 32 and 64-bit width type are just copies.
  466. // Same for 128-bit width type, except they are on the FPR bank.
  467. getActionDefinitionsBuilder(G_BITCAST)
  468. // FIXME: This is wrong since G_BITCAST is not allowed to change the
  469. // number of bits but it's what the previous code described and fixing
  470. // it breaks tests.
  471. .legalForCartesianProduct({s1, s8, s16, s32, s64, s128, v16s8, v8s8, v4s8,
  472. v8s16, v4s16, v2s16, v4s32, v2s32, v2s64,
  473. v2p0});
  474. getActionDefinitionsBuilder(G_VASTART).legalFor({p0});
  475. // va_list must be a pointer, but most sized types are pretty easy to handle
  476. // as the destination.
  477. getActionDefinitionsBuilder(G_VAARG)
  478. .customForCartesianProduct({s8, s16, s32, s64, p0}, {p0})
  479. .clampScalar(0, s8, s64)
  480. .widenScalarToNextPow2(0, /*Min*/ 8);
  481. getActionDefinitionsBuilder(G_ATOMIC_CMPXCHG_WITH_SUCCESS)
  482. .lowerIf(
  483. all(typeInSet(0, {s8, s16, s32, s64, s128}), typeIs(1, s1), typeIs(2, p0)));
  484. getActionDefinitionsBuilder(G_ATOMIC_CMPXCHG)
  485. .customIf([](const LegalityQuery &Query) {
  486. return Query.Types[0].getSizeInBits() == 128;
  487. })
  488. .clampScalar(0, s32, s64)
  489. .legalIf(all(typeInSet(0, {s32, s64}), typeIs(1, p0)));
  490. getActionDefinitionsBuilder(
  491. {G_ATOMICRMW_XCHG, G_ATOMICRMW_ADD, G_ATOMICRMW_SUB, G_ATOMICRMW_AND,
  492. G_ATOMICRMW_OR, G_ATOMICRMW_XOR, G_ATOMICRMW_MIN, G_ATOMICRMW_MAX,
  493. G_ATOMICRMW_UMIN, G_ATOMICRMW_UMAX})
  494. .clampScalar(0, s32, s64)
  495. .legalIf(all(typeInSet(0, {s32, s64}), typeIs(1, p0)));
  496. getActionDefinitionsBuilder(G_BLOCK_ADDR).legalFor({p0});
  497. // Merge/Unmerge
  498. for (unsigned Op : {G_MERGE_VALUES, G_UNMERGE_VALUES}) {
  499. unsigned BigTyIdx = Op == G_MERGE_VALUES ? 0 : 1;
  500. unsigned LitTyIdx = Op == G_MERGE_VALUES ? 1 : 0;
  501. getActionDefinitionsBuilder(Op)
  502. .widenScalarToNextPow2(LitTyIdx, 8)
  503. .widenScalarToNextPow2(BigTyIdx, 32)
  504. .clampScalar(LitTyIdx, s8, s64)
  505. .clampScalar(BigTyIdx, s32, s128)
  506. .legalIf([=](const LegalityQuery &Q) {
  507. switch (Q.Types[BigTyIdx].getSizeInBits()) {
  508. case 32:
  509. case 64:
  510. case 128:
  511. break;
  512. default:
  513. return false;
  514. }
  515. switch (Q.Types[LitTyIdx].getSizeInBits()) {
  516. case 8:
  517. case 16:
  518. case 32:
  519. case 64:
  520. return true;
  521. default:
  522. return false;
  523. }
  524. });
  525. }
  526. getActionDefinitionsBuilder(G_EXTRACT_VECTOR_ELT)
  527. .unsupportedIf([=](const LegalityQuery &Query) {
  528. const LLT &EltTy = Query.Types[1].getElementType();
  529. return Query.Types[0] != EltTy;
  530. })
  531. .minScalar(2, s64)
  532. .legalIf([=](const LegalityQuery &Query) {
  533. const LLT &VecTy = Query.Types[1];
  534. return VecTy == v2s16 || VecTy == v4s16 || VecTy == v8s16 ||
  535. VecTy == v4s32 || VecTy == v2s64 || VecTy == v2s32 ||
  536. VecTy == v8s8 || VecTy == v16s8 || VecTy == v2s32 ||
  537. VecTy == v2p0;
  538. })
  539. .minScalarOrEltIf(
  540. [=](const LegalityQuery &Query) {
  541. // We want to promote to <M x s1> to <M x s64> if that wouldn't
  542. // cause the total vec size to be > 128b.
  543. return Query.Types[1].getNumElements() <= 2;
  544. },
  545. 0, s64)
  546. .minScalarOrEltIf(
  547. [=](const LegalityQuery &Query) {
  548. return Query.Types[1].getNumElements() <= 4;
  549. },
  550. 0, s32)
  551. .minScalarOrEltIf(
  552. [=](const LegalityQuery &Query) {
  553. return Query.Types[1].getNumElements() <= 8;
  554. },
  555. 0, s16)
  556. .minScalarOrEltIf(
  557. [=](const LegalityQuery &Query) {
  558. return Query.Types[1].getNumElements() <= 16;
  559. },
  560. 0, s8)
  561. .minScalarOrElt(0, s8) // Worst case, we need at least s8.
  562. .clampMaxNumElements(1, s64, 2)
  563. .clampMaxNumElements(1, s32, 4)
  564. .clampMaxNumElements(1, s16, 8)
  565. .clampMaxNumElements(1, p0, 2);
  566. getActionDefinitionsBuilder(G_INSERT_VECTOR_ELT)
  567. .legalIf(typeInSet(0, {v8s16, v2s32, v4s32, v2s64}));
  568. getActionDefinitionsBuilder(G_BUILD_VECTOR)
  569. .legalFor({{v8s8, s8},
  570. {v16s8, s8},
  571. {v2s16, s16},
  572. {v4s16, s16},
  573. {v8s16, s16},
  574. {v2s32, s32},
  575. {v4s32, s32},
  576. {v2p0, p0},
  577. {v2s64, s64}})
  578. .clampNumElements(0, v4s32, v4s32)
  579. .clampNumElements(0, v2s64, v2s64)
  580. .minScalarOrElt(0, s8)
  581. .minScalarSameAs(1, 0);
  582. getActionDefinitionsBuilder(G_BUILD_VECTOR_TRUNC).lower();
  583. getActionDefinitionsBuilder(G_CTLZ)
  584. .legalForCartesianProduct(
  585. {s32, s64, v8s8, v16s8, v4s16, v8s16, v2s32, v4s32})
  586. .scalarize(1);
  587. getActionDefinitionsBuilder(G_CTLZ_ZERO_UNDEF).lower();
  588. // TODO: Custom lowering for v2s32, v4s32, v2s64.
  589. getActionDefinitionsBuilder(G_BITREVERSE)
  590. .legalFor({s32, s64, v8s8, v16s8})
  591. .widenScalarToNextPow2(0, /*Min = */ 32)
  592. .clampScalar(0, s32, s64);
  593. getActionDefinitionsBuilder(G_CTTZ_ZERO_UNDEF).lower();
  594. // TODO: Handle vector types.
  595. getActionDefinitionsBuilder(G_CTTZ)
  596. .clampScalar(0, s32, s64)
  597. .scalarSameSizeAs(1, 0)
  598. .customFor({s32, s64});
  599. getActionDefinitionsBuilder(G_SHUFFLE_VECTOR)
  600. .legalIf([=](const LegalityQuery &Query) {
  601. const LLT &DstTy = Query.Types[0];
  602. const LLT &SrcTy = Query.Types[1];
  603. // For now just support the TBL2 variant which needs the source vectors
  604. // to be the same size as the dest.
  605. if (DstTy != SrcTy)
  606. return false;
  607. for (auto &Ty : {v2s32, v4s32, v2s64, v2p0, v16s8, v8s16}) {
  608. if (DstTy == Ty)
  609. return true;
  610. }
  611. return false;
  612. })
  613. // G_SHUFFLE_VECTOR can have scalar sources (from 1 x s vectors), we
  614. // just want those lowered into G_BUILD_VECTOR
  615. .lowerIf([=](const LegalityQuery &Query) {
  616. return !Query.Types[1].isVector();
  617. })
  618. .moreElementsToNextPow2(0)
  619. .clampNumElements(0, v4s32, v4s32)
  620. .clampNumElements(0, v2s64, v2s64);
  621. getActionDefinitionsBuilder(G_CONCAT_VECTORS)
  622. .legalFor({{v4s32, v2s32}, {v8s16, v4s16}, {v16s8, v8s8}});
  623. getActionDefinitionsBuilder(G_JUMP_TABLE).legalFor({{p0}, {s64}});
  624. getActionDefinitionsBuilder(G_BRJT).legalIf([=](const LegalityQuery &Query) {
  625. return Query.Types[0] == p0 && Query.Types[1] == s64;
  626. });
  627. getActionDefinitionsBuilder(G_DYN_STACKALLOC).lower();
  628. if (ST.hasMOPS()) {
  629. // G_BZERO is not supported. Currently it is only emitted by
  630. // PreLegalizerCombiner for G_MEMSET with zero constant.
  631. getActionDefinitionsBuilder(G_BZERO).unsupported();
  632. getActionDefinitionsBuilder(G_MEMSET)
  633. .legalForCartesianProduct({p0}, {s64}, {s64})
  634. .customForCartesianProduct({p0}, {s8}, {s64})
  635. .immIdx(0); // Inform verifier imm idx 0 is handled.
  636. getActionDefinitionsBuilder({G_MEMCPY, G_MEMMOVE})
  637. .legalForCartesianProduct({p0}, {p0}, {s64})
  638. .immIdx(0); // Inform verifier imm idx 0 is handled.
  639. // G_MEMCPY_INLINE does not have a tailcall immediate
  640. getActionDefinitionsBuilder(G_MEMCPY_INLINE)
  641. .legalForCartesianProduct({p0}, {p0}, {s64});
  642. } else {
  643. getActionDefinitionsBuilder({G_BZERO, G_MEMCPY, G_MEMMOVE, G_MEMSET})
  644. .libcall();
  645. }
  646. // FIXME: Legal types are only legal with NEON.
  647. getActionDefinitionsBuilder(G_ABS)
  648. .lowerIf(isScalar(0))
  649. .legalFor(PackedVectorAllTypeList);
  650. getActionDefinitionsBuilder(G_VECREDUCE_FADD)
  651. // We only have FADDP to do reduction-like operations. Lower the rest.
  652. .legalFor({{s32, v2s32}, {s64, v2s64}})
  653. .clampMaxNumElements(1, s64, 2)
  654. .clampMaxNumElements(1, s32, 2)
  655. .lower();
  656. getActionDefinitionsBuilder(G_VECREDUCE_ADD)
  657. .legalFor(
  658. {{s8, v16s8}, {s16, v8s16}, {s32, v4s32}, {s32, v2s32}, {s64, v2s64}})
  659. .clampMaxNumElements(1, s64, 2)
  660. .clampMaxNumElements(1, s32, 4)
  661. .lower();
  662. getActionDefinitionsBuilder(
  663. {G_VECREDUCE_OR, G_VECREDUCE_AND, G_VECREDUCE_XOR})
  664. // Try to break down into smaller vectors as long as they're at least 64
  665. // bits. This lets us use vector operations for some parts of the
  666. // reduction.
  667. .fewerElementsIf(
  668. [=](const LegalityQuery &Q) {
  669. LLT SrcTy = Q.Types[1];
  670. if (SrcTy.isScalar())
  671. return false;
  672. if (!isPowerOf2_32(SrcTy.getNumElements()))
  673. return false;
  674. // We can usually perform 64b vector operations.
  675. return SrcTy.getSizeInBits() > 64;
  676. },
  677. [=](const LegalityQuery &Q) {
  678. LLT SrcTy = Q.Types[1];
  679. return std::make_pair(1, SrcTy.divide(2));
  680. })
  681. .scalarize(1)
  682. .lower();
  683. getActionDefinitionsBuilder({G_UADDSAT, G_USUBSAT})
  684. .lowerIf([=](const LegalityQuery &Q) { return Q.Types[0].isScalar(); });
  685. getActionDefinitionsBuilder({G_FSHL, G_FSHR}).lower();
  686. getActionDefinitionsBuilder(G_ROTR)
  687. .legalFor({{s32, s64}, {s64, s64}})
  688. .customIf([=](const LegalityQuery &Q) {
  689. return Q.Types[0].isScalar() && Q.Types[1].getScalarSizeInBits() < 64;
  690. })
  691. .lower();
  692. getActionDefinitionsBuilder(G_ROTL).lower();
  693. getActionDefinitionsBuilder({G_SBFX, G_UBFX})
  694. .customFor({{s32, s32}, {s64, s64}});
  695. // TODO: Use generic lowering when custom lowering is not possible.
  696. auto always = [=](const LegalityQuery &Q) { return true; };
  697. getActionDefinitionsBuilder(G_CTPOP)
  698. .legalFor({{v8s8, v8s8}, {v16s8, v16s8}})
  699. .clampScalar(0, s32, s128)
  700. .widenScalarToNextPow2(0)
  701. .minScalarEltSameAsIf(always, 1, 0)
  702. .maxScalarEltSameAsIf(always, 1, 0)
  703. .customFor({{s32, s32},
  704. {s64, s64},
  705. {s128, s128},
  706. {v2s64, v2s64},
  707. {v2s32, v2s32},
  708. {v4s32, v4s32},
  709. {v4s16, v4s16},
  710. {v8s16, v8s16}});
  711. // TODO: Vector types.
  712. getActionDefinitionsBuilder({G_SADDSAT, G_SSUBSAT}).lowerIf(isScalar(0));
  713. // TODO: Vector types.
  714. getActionDefinitionsBuilder({G_FMAXNUM, G_FMINNUM})
  715. .legalFor({MinFPScalar, s32, s64})
  716. .libcallFor({s128})
  717. .minScalar(0, MinFPScalar);
  718. // TODO: Vector types.
  719. getActionDefinitionsBuilder({G_FMAXIMUM, G_FMINIMUM})
  720. .legalFor({MinFPScalar, s32, s64})
  721. .minScalar(0, MinFPScalar);
  722. // TODO: Libcall support for s128.
  723. // TODO: s16 should be legal with full FP16 support.
  724. getActionDefinitionsBuilder({G_LROUND, G_LLROUND})
  725. .legalFor({{s64, s32}, {s64, s64}});
  726. getLegacyLegalizerInfo().computeTables();
  727. verify(*ST.getInstrInfo());
  728. }
  729. bool AArch64LegalizerInfo::legalizeCustom(LegalizerHelper &Helper,
  730. MachineInstr &MI) const {
  731. MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
  732. MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
  733. GISelChangeObserver &Observer = Helper.Observer;
  734. switch (MI.getOpcode()) {
  735. default:
  736. // No idea what to do.
  737. return false;
  738. case TargetOpcode::G_VAARG:
  739. return legalizeVaArg(MI, MRI, MIRBuilder);
  740. case TargetOpcode::G_LOAD:
  741. case TargetOpcode::G_STORE:
  742. return legalizeLoadStore(MI, MRI, MIRBuilder, Observer);
  743. case TargetOpcode::G_SHL:
  744. case TargetOpcode::G_ASHR:
  745. case TargetOpcode::G_LSHR:
  746. return legalizeShlAshrLshr(MI, MRI, MIRBuilder, Observer);
  747. case TargetOpcode::G_GLOBAL_VALUE:
  748. return legalizeSmallCMGlobalValue(MI, MRI, MIRBuilder, Observer);
  749. case TargetOpcode::G_TRUNC:
  750. return legalizeVectorTrunc(MI, Helper);
  751. case TargetOpcode::G_SBFX:
  752. case TargetOpcode::G_UBFX:
  753. return legalizeBitfieldExtract(MI, MRI, Helper);
  754. case TargetOpcode::G_ROTR:
  755. return legalizeRotate(MI, MRI, Helper);
  756. case TargetOpcode::G_CTPOP:
  757. return legalizeCTPOP(MI, MRI, Helper);
  758. case TargetOpcode::G_ATOMIC_CMPXCHG:
  759. return legalizeAtomicCmpxchg128(MI, MRI, Helper);
  760. case TargetOpcode::G_CTTZ:
  761. return legalizeCTTZ(MI, Helper);
  762. case TargetOpcode::G_BZERO:
  763. case TargetOpcode::G_MEMCPY:
  764. case TargetOpcode::G_MEMMOVE:
  765. case TargetOpcode::G_MEMSET:
  766. return legalizeMemOps(MI, Helper);
  767. }
  768. llvm_unreachable("expected switch to return");
  769. }
  770. bool AArch64LegalizerInfo::legalizeRotate(MachineInstr &MI,
  771. MachineRegisterInfo &MRI,
  772. LegalizerHelper &Helper) const {
  773. // To allow for imported patterns to match, we ensure that the rotate amount
  774. // is 64b with an extension.
  775. Register AmtReg = MI.getOperand(2).getReg();
  776. LLT AmtTy = MRI.getType(AmtReg);
  777. (void)AmtTy;
  778. assert(AmtTy.isScalar() && "Expected a scalar rotate");
  779. assert(AmtTy.getSizeInBits() < 64 && "Expected this rotate to be legal");
  780. auto NewAmt = Helper.MIRBuilder.buildSExt(LLT::scalar(64), AmtReg);
  781. Helper.Observer.changingInstr(MI);
  782. MI.getOperand(2).setReg(NewAmt.getReg(0));
  783. Helper.Observer.changedInstr(MI);
  784. return true;
  785. }
  786. static void extractParts(Register Reg, MachineRegisterInfo &MRI,
  787. MachineIRBuilder &MIRBuilder, LLT Ty, int NumParts,
  788. SmallVectorImpl<Register> &VRegs) {
  789. for (int I = 0; I < NumParts; ++I)
  790. VRegs.push_back(MRI.createGenericVirtualRegister(Ty));
  791. MIRBuilder.buildUnmerge(VRegs, Reg);
  792. }
  793. bool AArch64LegalizerInfo::legalizeVectorTrunc(
  794. MachineInstr &MI, LegalizerHelper &Helper) const {
  795. MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
  796. MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
  797. // Similar to how operand splitting is done in SelectiondDAG, we can handle
  798. // %res(v8s8) = G_TRUNC %in(v8s32) by generating:
  799. // %inlo(<4x s32>), %inhi(<4 x s32>) = G_UNMERGE %in(<8 x s32>)
  800. // %lo16(<4 x s16>) = G_TRUNC %inlo
  801. // %hi16(<4 x s16>) = G_TRUNC %inhi
  802. // %in16(<8 x s16>) = G_CONCAT_VECTORS %lo16, %hi16
  803. // %res(<8 x s8>) = G_TRUNC %in16
  804. Register DstReg = MI.getOperand(0).getReg();
  805. Register SrcReg = MI.getOperand(1).getReg();
  806. LLT DstTy = MRI.getType(DstReg);
  807. LLT SrcTy = MRI.getType(SrcReg);
  808. assert(isPowerOf2_32(DstTy.getSizeInBits()) &&
  809. isPowerOf2_32(SrcTy.getSizeInBits()));
  810. // Split input type.
  811. LLT SplitSrcTy =
  812. SrcTy.changeElementCount(SrcTy.getElementCount().divideCoefficientBy(2));
  813. // First, split the source into two smaller vectors.
  814. SmallVector<Register, 2> SplitSrcs;
  815. extractParts(SrcReg, MRI, MIRBuilder, SplitSrcTy, 2, SplitSrcs);
  816. // Truncate the splits into intermediate narrower elements.
  817. LLT InterTy = SplitSrcTy.changeElementSize(DstTy.getScalarSizeInBits() * 2);
  818. for (unsigned I = 0; I < SplitSrcs.size(); ++I)
  819. SplitSrcs[I] = MIRBuilder.buildTrunc(InterTy, SplitSrcs[I]).getReg(0);
  820. auto Concat = MIRBuilder.buildConcatVectors(
  821. DstTy.changeElementSize(DstTy.getScalarSizeInBits() * 2), SplitSrcs);
  822. Helper.Observer.changingInstr(MI);
  823. MI.getOperand(1).setReg(Concat.getReg(0));
  824. Helper.Observer.changedInstr(MI);
  825. return true;
  826. }
  827. bool AArch64LegalizerInfo::legalizeSmallCMGlobalValue(
  828. MachineInstr &MI, MachineRegisterInfo &MRI, MachineIRBuilder &MIRBuilder,
  829. GISelChangeObserver &Observer) const {
  830. assert(MI.getOpcode() == TargetOpcode::G_GLOBAL_VALUE);
  831. // We do this custom legalization to convert G_GLOBAL_VALUE into target ADRP +
  832. // G_ADD_LOW instructions.
  833. // By splitting this here, we can optimize accesses in the small code model by
  834. // folding in the G_ADD_LOW into the load/store offset.
  835. auto &GlobalOp = MI.getOperand(1);
  836. const auto* GV = GlobalOp.getGlobal();
  837. if (GV->isThreadLocal())
  838. return true; // Don't want to modify TLS vars.
  839. auto &TM = ST->getTargetLowering()->getTargetMachine();
  840. unsigned OpFlags = ST->ClassifyGlobalReference(GV, TM);
  841. if (OpFlags & AArch64II::MO_GOT)
  842. return true;
  843. auto Offset = GlobalOp.getOffset();
  844. Register DstReg = MI.getOperand(0).getReg();
  845. auto ADRP = MIRBuilder.buildInstr(AArch64::ADRP, {LLT::pointer(0, 64)}, {})
  846. .addGlobalAddress(GV, Offset, OpFlags | AArch64II::MO_PAGE);
  847. // Set the regclass on the dest reg too.
  848. MRI.setRegClass(ADRP.getReg(0), &AArch64::GPR64RegClass);
  849. // MO_TAGGED on the page indicates a tagged address. Set the tag now. We do so
  850. // by creating a MOVK that sets bits 48-63 of the register to (global address
  851. // + 0x100000000 - PC) >> 48. The additional 0x100000000 offset here is to
  852. // prevent an incorrect tag being generated during relocation when the the
  853. // global appears before the code section. Without the offset, a global at
  854. // `0x0f00'0000'0000'1000` (i.e. at `0x1000` with tag `0xf`) that's referenced
  855. // by code at `0x2000` would result in `0x0f00'0000'0000'1000 - 0x2000 =
  856. // 0x0eff'ffff'ffff'f000`, meaning the tag would be incorrectly set to `0xe`
  857. // instead of `0xf`.
  858. // This assumes that we're in the small code model so we can assume a binary
  859. // size of <= 4GB, which makes the untagged PC relative offset positive. The
  860. // binary must also be loaded into address range [0, 2^48). Both of these
  861. // properties need to be ensured at runtime when using tagged addresses.
  862. if (OpFlags & AArch64II::MO_TAGGED) {
  863. assert(!Offset &&
  864. "Should not have folded in an offset for a tagged global!");
  865. ADRP = MIRBuilder.buildInstr(AArch64::MOVKXi, {LLT::pointer(0, 64)}, {ADRP})
  866. .addGlobalAddress(GV, 0x100000000,
  867. AArch64II::MO_PREL | AArch64II::MO_G3)
  868. .addImm(48);
  869. MRI.setRegClass(ADRP.getReg(0), &AArch64::GPR64RegClass);
  870. }
  871. MIRBuilder.buildInstr(AArch64::G_ADD_LOW, {DstReg}, {ADRP})
  872. .addGlobalAddress(GV, Offset,
  873. OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
  874. MI.eraseFromParent();
  875. return true;
  876. }
  877. bool AArch64LegalizerInfo::legalizeIntrinsic(LegalizerHelper &Helper,
  878. MachineInstr &MI) const {
  879. switch (MI.getIntrinsicID()) {
  880. case Intrinsic::vacopy: {
  881. unsigned PtrSize = ST->isTargetILP32() ? 4 : 8;
  882. unsigned VaListSize =
  883. (ST->isTargetDarwin() || ST->isTargetWindows())
  884. ? PtrSize
  885. : ST->isTargetILP32() ? 20 : 32;
  886. MachineFunction &MF = *MI.getMF();
  887. auto Val = MF.getRegInfo().createGenericVirtualRegister(
  888. LLT::scalar(VaListSize * 8));
  889. MachineIRBuilder MIB(MI);
  890. MIB.buildLoad(Val, MI.getOperand(2),
  891. *MF.getMachineMemOperand(MachinePointerInfo(),
  892. MachineMemOperand::MOLoad,
  893. VaListSize, Align(PtrSize)));
  894. MIB.buildStore(Val, MI.getOperand(1),
  895. *MF.getMachineMemOperand(MachinePointerInfo(),
  896. MachineMemOperand::MOStore,
  897. VaListSize, Align(PtrSize)));
  898. MI.eraseFromParent();
  899. return true;
  900. }
  901. case Intrinsic::get_dynamic_area_offset: {
  902. MachineIRBuilder &MIB = Helper.MIRBuilder;
  903. MIB.buildConstant(MI.getOperand(0).getReg(), 0);
  904. MI.eraseFromParent();
  905. return true;
  906. }
  907. case Intrinsic::aarch64_mops_memset_tag: {
  908. assert(MI.getOpcode() == TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS);
  909. // Zext the value to 64 bit
  910. MachineIRBuilder MIB(MI);
  911. auto &Value = MI.getOperand(3);
  912. Register ZExtValueReg = MIB.buildAnyExt(LLT::scalar(64), Value).getReg(0);
  913. Value.setReg(ZExtValueReg);
  914. return true;
  915. }
  916. }
  917. return true;
  918. }
  919. bool AArch64LegalizerInfo::legalizeShlAshrLshr(
  920. MachineInstr &MI, MachineRegisterInfo &MRI, MachineIRBuilder &MIRBuilder,
  921. GISelChangeObserver &Observer) const {
  922. assert(MI.getOpcode() == TargetOpcode::G_ASHR ||
  923. MI.getOpcode() == TargetOpcode::G_LSHR ||
  924. MI.getOpcode() == TargetOpcode::G_SHL);
  925. // If the shift amount is a G_CONSTANT, promote it to a 64 bit type so the
  926. // imported patterns can select it later. Either way, it will be legal.
  927. Register AmtReg = MI.getOperand(2).getReg();
  928. auto VRegAndVal = getIConstantVRegValWithLookThrough(AmtReg, MRI);
  929. if (!VRegAndVal)
  930. return true;
  931. // Check the shift amount is in range for an immediate form.
  932. int64_t Amount = VRegAndVal->Value.getSExtValue();
  933. if (Amount > 31)
  934. return true; // This will have to remain a register variant.
  935. auto ExtCst = MIRBuilder.buildConstant(LLT::scalar(64), Amount);
  936. Observer.changingInstr(MI);
  937. MI.getOperand(2).setReg(ExtCst.getReg(0));
  938. Observer.changedInstr(MI);
  939. return true;
  940. }
  941. static void matchLDPSTPAddrMode(Register Root, Register &Base, int &Offset,
  942. MachineRegisterInfo &MRI) {
  943. Base = Root;
  944. Offset = 0;
  945. Register NewBase;
  946. int64_t NewOffset;
  947. if (mi_match(Root, MRI, m_GPtrAdd(m_Reg(NewBase), m_ICst(NewOffset))) &&
  948. isShiftedInt<7, 3>(NewOffset)) {
  949. Base = NewBase;
  950. Offset = NewOffset;
  951. }
  952. }
  953. // FIXME: This should be removed and replaced with the generic bitcast legalize
  954. // action.
  955. bool AArch64LegalizerInfo::legalizeLoadStore(
  956. MachineInstr &MI, MachineRegisterInfo &MRI, MachineIRBuilder &MIRBuilder,
  957. GISelChangeObserver &Observer) const {
  958. assert(MI.getOpcode() == TargetOpcode::G_STORE ||
  959. MI.getOpcode() == TargetOpcode::G_LOAD);
  960. // Here we just try to handle vector loads/stores where our value type might
  961. // have pointer elements, which the SelectionDAG importer can't handle. To
  962. // allow the existing patterns for s64 to fire for p0, we just try to bitcast
  963. // the value to use s64 types.
  964. // Custom legalization requires the instruction, if not deleted, must be fully
  965. // legalized. In order to allow further legalization of the inst, we create
  966. // a new instruction and erase the existing one.
  967. Register ValReg = MI.getOperand(0).getReg();
  968. const LLT ValTy = MRI.getType(ValReg);
  969. if (ValTy == LLT::scalar(128)) {
  970. assert((*MI.memoperands_begin())->getSuccessOrdering() ==
  971. AtomicOrdering::Monotonic ||
  972. (*MI.memoperands_begin())->getSuccessOrdering() ==
  973. AtomicOrdering::Unordered);
  974. assert(ST->hasLSE2() && "ldp/stp not single copy atomic without +lse2");
  975. LLT s64 = LLT::scalar(64);
  976. MachineInstrBuilder NewI;
  977. if (MI.getOpcode() == TargetOpcode::G_LOAD) {
  978. NewI = MIRBuilder.buildInstr(AArch64::LDPXi, {s64, s64}, {});
  979. MIRBuilder.buildMerge(ValReg, {NewI->getOperand(0), NewI->getOperand(1)});
  980. } else {
  981. auto Split = MIRBuilder.buildUnmerge(s64, MI.getOperand(0));
  982. NewI = MIRBuilder.buildInstr(
  983. AArch64::STPXi, {}, {Split->getOperand(0), Split->getOperand(1)});
  984. }
  985. Register Base;
  986. int Offset;
  987. matchLDPSTPAddrMode(MI.getOperand(1).getReg(), Base, Offset, MRI);
  988. NewI.addUse(Base);
  989. NewI.addImm(Offset / 8);
  990. NewI.cloneMemRefs(MI);
  991. constrainSelectedInstRegOperands(*NewI, *ST->getInstrInfo(),
  992. *MRI.getTargetRegisterInfo(),
  993. *ST->getRegBankInfo());
  994. MI.eraseFromParent();
  995. return true;
  996. }
  997. if (!ValTy.isVector() || !ValTy.getElementType().isPointer() ||
  998. ValTy.getElementType().getAddressSpace() != 0) {
  999. LLVM_DEBUG(dbgs() << "Tried to do custom legalization on wrong load/store");
  1000. return false;
  1001. }
  1002. unsigned PtrSize = ValTy.getElementType().getSizeInBits();
  1003. const LLT NewTy = LLT::vector(ValTy.getElementCount(), PtrSize);
  1004. auto &MMO = **MI.memoperands_begin();
  1005. MMO.setType(NewTy);
  1006. if (MI.getOpcode() == TargetOpcode::G_STORE) {
  1007. auto Bitcast = MIRBuilder.buildBitcast(NewTy, ValReg);
  1008. MIRBuilder.buildStore(Bitcast.getReg(0), MI.getOperand(1), MMO);
  1009. } else {
  1010. auto NewLoad = MIRBuilder.buildLoad(NewTy, MI.getOperand(1), MMO);
  1011. MIRBuilder.buildBitcast(ValReg, NewLoad);
  1012. }
  1013. MI.eraseFromParent();
  1014. return true;
  1015. }
  1016. bool AArch64LegalizerInfo::legalizeVaArg(MachineInstr &MI,
  1017. MachineRegisterInfo &MRI,
  1018. MachineIRBuilder &MIRBuilder) const {
  1019. MachineFunction &MF = MIRBuilder.getMF();
  1020. Align Alignment(MI.getOperand(2).getImm());
  1021. Register Dst = MI.getOperand(0).getReg();
  1022. Register ListPtr = MI.getOperand(1).getReg();
  1023. LLT PtrTy = MRI.getType(ListPtr);
  1024. LLT IntPtrTy = LLT::scalar(PtrTy.getSizeInBits());
  1025. const unsigned PtrSize = PtrTy.getSizeInBits() / 8;
  1026. const Align PtrAlign = Align(PtrSize);
  1027. auto List = MIRBuilder.buildLoad(
  1028. PtrTy, ListPtr,
  1029. *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOLoad,
  1030. PtrTy, PtrAlign));
  1031. MachineInstrBuilder DstPtr;
  1032. if (Alignment > PtrAlign) {
  1033. // Realign the list to the actual required alignment.
  1034. auto AlignMinus1 =
  1035. MIRBuilder.buildConstant(IntPtrTy, Alignment.value() - 1);
  1036. auto ListTmp = MIRBuilder.buildPtrAdd(PtrTy, List, AlignMinus1.getReg(0));
  1037. DstPtr = MIRBuilder.buildMaskLowPtrBits(PtrTy, ListTmp, Log2(Alignment));
  1038. } else
  1039. DstPtr = List;
  1040. LLT ValTy = MRI.getType(Dst);
  1041. uint64_t ValSize = ValTy.getSizeInBits() / 8;
  1042. MIRBuilder.buildLoad(
  1043. Dst, DstPtr,
  1044. *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOLoad,
  1045. ValTy, std::max(Alignment, PtrAlign)));
  1046. auto Size = MIRBuilder.buildConstant(IntPtrTy, alignTo(ValSize, PtrAlign));
  1047. auto NewList = MIRBuilder.buildPtrAdd(PtrTy, DstPtr, Size.getReg(0));
  1048. MIRBuilder.buildStore(NewList, ListPtr,
  1049. *MF.getMachineMemOperand(MachinePointerInfo(),
  1050. MachineMemOperand::MOStore,
  1051. PtrTy, PtrAlign));
  1052. MI.eraseFromParent();
  1053. return true;
  1054. }
  1055. bool AArch64LegalizerInfo::legalizeBitfieldExtract(
  1056. MachineInstr &MI, MachineRegisterInfo &MRI, LegalizerHelper &Helper) const {
  1057. // Only legal if we can select immediate forms.
  1058. // TODO: Lower this otherwise.
  1059. return getIConstantVRegValWithLookThrough(MI.getOperand(2).getReg(), MRI) &&
  1060. getIConstantVRegValWithLookThrough(MI.getOperand(3).getReg(), MRI);
  1061. }
  1062. bool AArch64LegalizerInfo::legalizeCTPOP(MachineInstr &MI,
  1063. MachineRegisterInfo &MRI,
  1064. LegalizerHelper &Helper) const {
  1065. // While there is no integer popcount instruction, it can
  1066. // be more efficiently lowered to the following sequence that uses
  1067. // AdvSIMD registers/instructions as long as the copies to/from
  1068. // the AdvSIMD registers are cheap.
  1069. // FMOV D0, X0 // copy 64-bit int to vector, high bits zero'd
  1070. // CNT V0.8B, V0.8B // 8xbyte pop-counts
  1071. // ADDV B0, V0.8B // sum 8xbyte pop-counts
  1072. // UMOV X0, V0.B[0] // copy byte result back to integer reg
  1073. //
  1074. // For 128 bit vector popcounts, we lower to the following sequence:
  1075. // cnt.16b v0, v0 // v8s16, v4s32, v2s64
  1076. // uaddlp.8h v0, v0 // v8s16, v4s32, v2s64
  1077. // uaddlp.4s v0, v0 // v4s32, v2s64
  1078. // uaddlp.2d v0, v0 // v2s64
  1079. //
  1080. // For 64 bit vector popcounts, we lower to the following sequence:
  1081. // cnt.8b v0, v0 // v4s16, v2s32
  1082. // uaddlp.4h v0, v0 // v4s16, v2s32
  1083. // uaddlp.2s v0, v0 // v2s32
  1084. if (!ST->hasNEON() ||
  1085. MI.getMF()->getFunction().hasFnAttribute(Attribute::NoImplicitFloat))
  1086. return false;
  1087. MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
  1088. Register Dst = MI.getOperand(0).getReg();
  1089. Register Val = MI.getOperand(1).getReg();
  1090. LLT Ty = MRI.getType(Val);
  1091. assert(Ty == MRI.getType(Dst) &&
  1092. "Expected src and dst to have the same type!");
  1093. unsigned Size = Ty.getSizeInBits();
  1094. // Pre-conditioning: widen Val up to the nearest vector type.
  1095. // s32,s64,v4s16,v2s32 -> v8i8
  1096. // v8s16,v4s32,v2s64 -> v16i8
  1097. LLT VTy = Size == 128 ? LLT::fixed_vector(16, 8) : LLT::fixed_vector(8, 8);
  1098. if (Ty.isScalar()) {
  1099. assert((Size == 32 || Size == 64 || Size == 128) && "Expected only 32, 64, or 128 bit scalars!");
  1100. if (Size == 32) {
  1101. Val = MIRBuilder.buildZExt(LLT::scalar(64), Val).getReg(0);
  1102. }
  1103. }
  1104. Val = MIRBuilder.buildBitcast(VTy, Val).getReg(0);
  1105. // Count bits in each byte-sized lane.
  1106. auto CTPOP = MIRBuilder.buildCTPOP(VTy, Val);
  1107. // Sum across lanes.
  1108. Register HSum = CTPOP.getReg(0);
  1109. unsigned Opc;
  1110. SmallVector<LLT> HAddTys;
  1111. if (Ty.isScalar()) {
  1112. Opc = Intrinsic::aarch64_neon_uaddlv;
  1113. HAddTys.push_back(LLT::scalar(32));
  1114. } else if (Ty == LLT::fixed_vector(8, 16)) {
  1115. Opc = Intrinsic::aarch64_neon_uaddlp;
  1116. HAddTys.push_back(LLT::fixed_vector(8, 16));
  1117. } else if (Ty == LLT::fixed_vector(4, 32)) {
  1118. Opc = Intrinsic::aarch64_neon_uaddlp;
  1119. HAddTys.push_back(LLT::fixed_vector(8, 16));
  1120. HAddTys.push_back(LLT::fixed_vector(4, 32));
  1121. } else if (Ty == LLT::fixed_vector(2, 64)) {
  1122. Opc = Intrinsic::aarch64_neon_uaddlp;
  1123. HAddTys.push_back(LLT::fixed_vector(8, 16));
  1124. HAddTys.push_back(LLT::fixed_vector(4, 32));
  1125. HAddTys.push_back(LLT::fixed_vector(2, 64));
  1126. } else if (Ty == LLT::fixed_vector(4, 16)) {
  1127. Opc = Intrinsic::aarch64_neon_uaddlp;
  1128. HAddTys.push_back(LLT::fixed_vector(4, 16));
  1129. } else if (Ty == LLT::fixed_vector(2, 32)) {
  1130. Opc = Intrinsic::aarch64_neon_uaddlp;
  1131. HAddTys.push_back(LLT::fixed_vector(4, 16));
  1132. HAddTys.push_back(LLT::fixed_vector(2, 32));
  1133. } else
  1134. llvm_unreachable("unexpected vector shape");
  1135. MachineInstrBuilder UADD;
  1136. for (LLT HTy : HAddTys) {
  1137. UADD = MIRBuilder.buildIntrinsic(Opc, {HTy}, /*HasSideEffects =*/false)
  1138. .addUse(HSum);
  1139. HSum = UADD.getReg(0);
  1140. }
  1141. // Post-conditioning.
  1142. if (Ty.isScalar() && (Size == 64 || Size == 128))
  1143. MIRBuilder.buildZExt(Dst, UADD);
  1144. else
  1145. UADD->getOperand(0).setReg(Dst);
  1146. MI.eraseFromParent();
  1147. return true;
  1148. }
  1149. bool AArch64LegalizerInfo::legalizeAtomicCmpxchg128(
  1150. MachineInstr &MI, MachineRegisterInfo &MRI, LegalizerHelper &Helper) const {
  1151. MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
  1152. LLT s64 = LLT::scalar(64);
  1153. auto Addr = MI.getOperand(1).getReg();
  1154. auto DesiredI = MIRBuilder.buildUnmerge({s64, s64}, MI.getOperand(2));
  1155. auto NewI = MIRBuilder.buildUnmerge({s64, s64}, MI.getOperand(3));
  1156. auto DstLo = MRI.createGenericVirtualRegister(s64);
  1157. auto DstHi = MRI.createGenericVirtualRegister(s64);
  1158. MachineInstrBuilder CAS;
  1159. if (ST->hasLSE()) {
  1160. // We have 128-bit CASP instructions taking XSeqPair registers, which are
  1161. // s128. We need the merge/unmerge to bracket the expansion and pair up with
  1162. // the rest of the MIR so we must reassemble the extracted registers into a
  1163. // 128-bit known-regclass one with code like this:
  1164. //
  1165. // %in1 = REG_SEQUENCE Lo, Hi ; One for each input
  1166. // %out = CASP %in1, ...
  1167. // %OldLo = G_EXTRACT %out, 0
  1168. // %OldHi = G_EXTRACT %out, 64
  1169. auto Ordering = (*MI.memoperands_begin())->getMergedOrdering();
  1170. unsigned Opcode;
  1171. switch (Ordering) {
  1172. case AtomicOrdering::Acquire:
  1173. Opcode = AArch64::CASPAX;
  1174. break;
  1175. case AtomicOrdering::Release:
  1176. Opcode = AArch64::CASPLX;
  1177. break;
  1178. case AtomicOrdering::AcquireRelease:
  1179. case AtomicOrdering::SequentiallyConsistent:
  1180. Opcode = AArch64::CASPALX;
  1181. break;
  1182. default:
  1183. Opcode = AArch64::CASPX;
  1184. break;
  1185. }
  1186. LLT s128 = LLT::scalar(128);
  1187. auto CASDst = MRI.createGenericVirtualRegister(s128);
  1188. auto CASDesired = MRI.createGenericVirtualRegister(s128);
  1189. auto CASNew = MRI.createGenericVirtualRegister(s128);
  1190. MIRBuilder.buildInstr(TargetOpcode::REG_SEQUENCE, {CASDesired}, {})
  1191. .addUse(DesiredI->getOperand(0).getReg())
  1192. .addImm(AArch64::sube64)
  1193. .addUse(DesiredI->getOperand(1).getReg())
  1194. .addImm(AArch64::subo64);
  1195. MIRBuilder.buildInstr(TargetOpcode::REG_SEQUENCE, {CASNew}, {})
  1196. .addUse(NewI->getOperand(0).getReg())
  1197. .addImm(AArch64::sube64)
  1198. .addUse(NewI->getOperand(1).getReg())
  1199. .addImm(AArch64::subo64);
  1200. CAS = MIRBuilder.buildInstr(Opcode, {CASDst}, {CASDesired, CASNew, Addr});
  1201. MIRBuilder.buildExtract({DstLo}, {CASDst}, 0);
  1202. MIRBuilder.buildExtract({DstHi}, {CASDst}, 64);
  1203. } else {
  1204. // The -O0 CMP_SWAP_128 is friendlier to generate code for because LDXP/STXP
  1205. // can take arbitrary registers so it just has the normal GPR64 operands the
  1206. // rest of AArch64 is expecting.
  1207. auto Ordering = (*MI.memoperands_begin())->getMergedOrdering();
  1208. unsigned Opcode;
  1209. switch (Ordering) {
  1210. case AtomicOrdering::Acquire:
  1211. Opcode = AArch64::CMP_SWAP_128_ACQUIRE;
  1212. break;
  1213. case AtomicOrdering::Release:
  1214. Opcode = AArch64::CMP_SWAP_128_RELEASE;
  1215. break;
  1216. case AtomicOrdering::AcquireRelease:
  1217. case AtomicOrdering::SequentiallyConsistent:
  1218. Opcode = AArch64::CMP_SWAP_128;
  1219. break;
  1220. default:
  1221. Opcode = AArch64::CMP_SWAP_128_MONOTONIC;
  1222. break;
  1223. }
  1224. auto Scratch = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
  1225. CAS = MIRBuilder.buildInstr(Opcode, {DstLo, DstHi, Scratch},
  1226. {Addr, DesiredI->getOperand(0),
  1227. DesiredI->getOperand(1), NewI->getOperand(0),
  1228. NewI->getOperand(1)});
  1229. }
  1230. CAS.cloneMemRefs(MI);
  1231. constrainSelectedInstRegOperands(*CAS, *ST->getInstrInfo(),
  1232. *MRI.getTargetRegisterInfo(),
  1233. *ST->getRegBankInfo());
  1234. MIRBuilder.buildMerge(MI.getOperand(0), {DstLo, DstHi});
  1235. MI.eraseFromParent();
  1236. return true;
  1237. }
  1238. bool AArch64LegalizerInfo::legalizeCTTZ(MachineInstr &MI,
  1239. LegalizerHelper &Helper) const {
  1240. MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
  1241. MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
  1242. LLT Ty = MRI.getType(MI.getOperand(1).getReg());
  1243. auto BitReverse = MIRBuilder.buildBitReverse(Ty, MI.getOperand(1));
  1244. MIRBuilder.buildCTLZ(MI.getOperand(0).getReg(), BitReverse);
  1245. MI.eraseFromParent();
  1246. return true;
  1247. }
  1248. bool AArch64LegalizerInfo::legalizeMemOps(MachineInstr &MI,
  1249. LegalizerHelper &Helper) const {
  1250. MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
  1251. // Tagged version MOPSMemorySetTagged is legalised in legalizeIntrinsic
  1252. if (MI.getOpcode() == TargetOpcode::G_MEMSET) {
  1253. // Zext the value operand to 64 bit
  1254. auto &Value = MI.getOperand(1);
  1255. Register ZExtValueReg =
  1256. MIRBuilder.buildAnyExt(LLT::scalar(64), Value).getReg(0);
  1257. Value.setReg(ZExtValueReg);
  1258. return true;
  1259. }
  1260. return false;
  1261. }