blob: 16821fbe3d0602c7b7253b0a8aee8fd507380bb9 [file] [log] [blame]
Tim Northover69fa84a2016-10-14 22:18:18 +00001//===- AArch64LegalizerInfo.cpp ----------------------------------*- C++ -*-==//
Tim Northover33b07d62016-07-22 20:03:43 +00002//
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/// \file
10/// This file implements the targeting of the Machinelegalizer class for
11/// AArch64.
12/// \todo This should be generated by TableGen.
13//===----------------------------------------------------------------------===//
14
Tim Northover69fa84a2016-10-14 22:18:18 +000015#include "AArch64LegalizerInfo.h"
Daniel Sanders7fe7acc2017-11-28 20:21:15 +000016#include "AArch64Subtarget.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000017#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
Tim Northover91366172017-02-15 23:22:50 +000018#include "llvm/CodeGen/MachineInstr.h"
19#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000020#include "llvm/CodeGen/TargetOpcodes.h"
Tim Northover33b07d62016-07-22 20:03:43 +000021#include "llvm/CodeGen/ValueTypes.h"
Tim Northover33b07d62016-07-22 20:03:43 +000022#include "llvm/IR/DerivedTypes.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000023#include "llvm/IR/Type.h"
Tim Northover33b07d62016-07-22 20:03:43 +000024
25using namespace llvm;
Daniel Sanders9ade5592018-01-29 17:37:29 +000026using namespace LegalizeActions;
Daniel Sanders79cb8392018-01-29 19:54:49 +000027using namespace LegalityPredicates;
Kristof Beylsaf9814a2017-11-07 10:34:34 +000028
Daniel Sanders7fe7acc2017-11-28 20:21:15 +000029AArch64LegalizerInfo::AArch64LegalizerInfo(const AArch64Subtarget &ST) {
Ahmed Bougachaad30db32016-08-02 15:10:28 +000030 using namespace TargetOpcode;
Tim Northover5ae83502016-09-15 09:20:34 +000031 const LLT p0 = LLT::pointer(0, 64);
Tim Northoverea904f92016-08-19 22:40:00 +000032 const LLT s1 = LLT::scalar(1);
Tim Northover9656f142016-08-04 20:54:13 +000033 const LLT s8 = LLT::scalar(8);
34 const LLT s16 = LLT::scalar(16);
Ahmed Bougachaad30db32016-08-02 15:10:28 +000035 const LLT s32 = LLT::scalar(32);
36 const LLT s64 = LLT::scalar(64);
Quentin Colombet7c114d32017-10-16 22:28:27 +000037 const LLT s128 = LLT::scalar(128);
Daniel Sanders79cb8392018-01-29 19:54:49 +000038 const LLT s256 = LLT::scalar(256);
39 const LLT s512 = LLT::scalar(512);
40 const LLT v16s8 = LLT::vector(16, 8);
41 const LLT v8s8 = LLT::vector(8, 8);
42 const LLT v4s8 = LLT::vector(4, 8);
43 const LLT v8s16 = LLT::vector(8, 16);
44 const LLT v4s16 = LLT::vector(4, 16);
45 const LLT v2s16 = LLT::vector(2, 16);
Ahmed Bougachaad30db32016-08-02 15:10:28 +000046 const LLT v2s32 = LLT::vector(2, 32);
47 const LLT v4s32 = LLT::vector(4, 32);
48 const LLT v2s64 = LLT::vector(2, 64);
49
Daniel Sanders79cb8392018-01-29 19:54:49 +000050 getActionDefinitionsBuilder(G_IMPLICIT_DEF)
51 .legalFor({p0, s1, s8, s16, s32, s64})
52 .clampScalar(0, s1, s64)
53 .widenScalarToNextPow2(0, 8);
Tim Northoverff5e7e12017-06-30 20:27:36 +000054
Daniel Sanders79cb8392018-01-29 19:54:49 +000055 getActionDefinitionsBuilder(G_PHI)
56 .legalFor({p0, s16, s32, s64})
57 .clampScalar(0, s16, s64)
58 .widenScalarToNextPow2(0);
Aditya Nandakumarefd8a842017-08-23 20:45:48 +000059
Daniel Sanders79cb8392018-01-29 19:54:49 +000060 getActionDefinitionsBuilder(G_BSWAP)
61 .legalFor({s32, s64})
62 .clampScalar(0, s16, s64)
63 .widenScalarToNextPow2(0);
Aditya Nandakumar892979e2017-08-25 04:57:27 +000064
Daniel Sanders79cb8392018-01-29 19:54:49 +000065 getActionDefinitionsBuilder({G_ADD, G_SUB, G_MUL, G_AND, G_OR, G_XOR, G_SHL})
66 .legalFor({s32, s64, v2s32, v4s32, v2s64})
67 .clampScalar(0, s32, s64)
68 .widenScalarToNextPow2(0)
69 .clampNumElements(0, v2s32, v4s32)
70 .clampNumElements(0, v2s64, v2s64)
71 .moreElementsToNextPow2(0);
Daniel Sanders83e23d12017-09-19 14:25:15 +000072
Daniel Sanders79cb8392018-01-29 19:54:49 +000073 getActionDefinitionsBuilder(G_GEP)
74 .legalFor({{p0, s64}})
75 .clampScalar(1, s64, s64);
Ahmed Bougachacfb384d2017-01-23 21:10:05 +000076
Daniel Sanders79cb8392018-01-29 19:54:49 +000077 getActionDefinitionsBuilder(G_PTR_MASK).legalFor({p0});
Tim Northover9656f142016-08-04 20:54:13 +000078
Daniel Sanders79cb8392018-01-29 19:54:49 +000079 getActionDefinitionsBuilder({G_LSHR, G_ASHR, G_SDIV, G_UDIV})
80 .legalFor({s32, s64})
81 .clampScalar(0, s32, s64)
82 .widenScalarToNextPow2(0);
Tim Northover7a753d92016-08-26 17:46:06 +000083
Quentin Colombet24203cf2017-01-27 01:13:30 +000084 for (unsigned BinOp : {G_SREM, G_UREM})
Tim Northovercecee562016-08-26 17:46:13 +000085 for (auto Ty : { s1, s8, s16, s32, s64 })
Quentin Colombete15e4602017-01-27 01:13:25 +000086 setAction({BinOp, Ty}, Lower);
Tim Northovercecee562016-08-26 17:46:13 +000087
Kristof Beylsaf9814a2017-11-07 10:34:34 +000088 for (unsigned Op : {G_SMULO, G_UMULO}) {
89 setAction({Op, 0, s64}, Lower);
90 setAction({Op, 1, s1}, Legal);
91 }
Tim Northover0a9b2792017-02-08 21:22:15 +000092
Daniel Sanders79cb8392018-01-29 19:54:49 +000093 getActionDefinitionsBuilder({G_SMULH, G_UMULH}).legalFor({s32, s64});
Tim Northover438c77c2016-08-25 17:37:32 +000094
Daniel Sanders79cb8392018-01-29 19:54:49 +000095 getActionDefinitionsBuilder({G_UADDE, G_USUBE, G_SADDO, G_SSUBO})
96 .legalFor({{s32, s1}, {s64, s1}});
Tim Northoverd8a6d7c2016-08-25 17:37:41 +000097
Daniel Sanders79cb8392018-01-29 19:54:49 +000098 getActionDefinitionsBuilder({G_FADD, G_FSUB, G_FMA, G_FMUL, G_FDIV})
99 .legalFor({s32, s64});
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000100
Daniel Sanders79cb8392018-01-29 19:54:49 +0000101 getActionDefinitionsBuilder({G_FREM, G_FPOW}).libcallFor({s32, s64});
Tim Northoveredb3c8c2016-08-29 19:07:16 +0000102
Daniel Sanders79cb8392018-01-29 19:54:49 +0000103 getActionDefinitionsBuilder(G_INSERT)
104 .unsupportedIf([=](const LegalityQuery &Query) {
105 return Query.Types[0].getSizeInBits() <= Query.Types[1].getSizeInBits();
106 })
107 .legalIf([=](const LegalityQuery &Query) {
108 const LLT &Ty0 = Query.Types[0];
109 const LLT &Ty1 = Query.Types[1];
110 if (Ty0 != s32 && Ty0 != s64 && Ty0 != p0)
111 return false;
112 return isPowerOf2_32(Ty1.getSizeInBits()) &&
113 (Ty1.getSizeInBits() == 1 || Ty1.getSizeInBits() >= 8);
114 })
115 .clampScalar(0, s32, s64)
116 .widenScalarToNextPow2(0)
117 .maxScalarIf(typeInSet(0, {s32}), 1, s16)
118 .maxScalarIf(typeInSet(0, {s64}), 1, s32)
119 .widenScalarToNextPow2(1);
Tim Northover0e6afbd2017-02-06 21:56:47 +0000120
Daniel Sanders79cb8392018-01-29 19:54:49 +0000121 getActionDefinitionsBuilder(G_EXTRACT)
122 .unsupportedIf([=](const LegalityQuery &Query) {
123 return Query.Types[0].getSizeInBits() >= Query.Types[1].getSizeInBits();
124 })
125 .legalIf([=](const LegalityQuery &Query) {
126 const LLT &Ty0 = Query.Types[0];
127 const LLT &Ty1 = Query.Types[1];
128 if (Ty1 != s32 && Ty1 != s64)
129 return false;
130 if (Ty1 == p0)
131 return true;
132 return isPowerOf2_32(Ty0.getSizeInBits()) &&
133 (Ty0.getSizeInBits() == 1 || Ty0.getSizeInBits() >= 8);
134 })
135 .clampScalar(1, s32, s64)
136 .widenScalarToNextPow2(1)
137 .maxScalarIf(typeInSet(1, {s32}), 0, s16)
138 .maxScalarIf(typeInSet(1, {s64}), 0, s32)
139 .widenScalarToNextPow2(0);
Tim Northoverc2d5e6d2017-06-26 20:34:13 +0000140
Daniel Sanders79cb8392018-01-29 19:54:49 +0000141 getActionDefinitionsBuilder({G_LOAD, G_STORE})
142 .legalFor(
143 {{s8, p0}, {s16, p0}, {s32, p0}, {s64, p0}, {p0, p0}, {v2s32, p0}})
144 .clampScalar(0, s8, s64)
145 .widenScalarToNextPow2(0)
146 .clampNumElements(0, v2s32, v2s32);
Tim Northover3c73e362016-08-23 18:20:09 +0000147
Tim Northoverb3a0be42016-08-23 21:01:20 +0000148 // Constants
Daniel Sanders79cb8392018-01-29 19:54:49 +0000149 getActionDefinitionsBuilder(G_CONSTANT)
150 .legalFor({p0, s32, s64})
151 .clampScalar(0, s32, s64)
152 .widenScalarToNextPow2(0);
153 getActionDefinitionsBuilder(G_FCONSTANT)
154 .legalFor({s32, s64})
155 .clampScalar(0, s32, s64);
Tim Northoverea904f92016-08-19 22:40:00 +0000156
Daniel Sanders79cb8392018-01-29 19:54:49 +0000157 getActionDefinitionsBuilder(G_ICMP)
158 .legalFor({{s32, s32}, {s32, s64}, {s32, p0}})
159 .clampScalar(0, s32, s32)
160 .clampScalar(1, s32, s64)
161 .widenScalarToNextPow2(1);
Tim Northover7a1ec012016-08-25 17:37:35 +0000162
Daniel Sanders79cb8392018-01-29 19:54:49 +0000163 getActionDefinitionsBuilder(G_FCMP)
164 .legalFor({{s32, s32}, {s32, s64}})
165 .clampScalar(0, s32, s32)
166 .clampScalar(1, s32, s64)
167 .widenScalarToNextPow2(1);
Tim Northover30bd36e2016-08-26 17:46:19 +0000168
Tim Northover2c4a8382016-08-25 17:37:25 +0000169 // Extensions
Daniel Sanders79cb8392018-01-29 19:54:49 +0000170 getActionDefinitionsBuilder({G_ZEXT, G_SEXT, G_ANYEXT})
171 .legalFor({s1, s8, s16, s32, s64})
172 .maxScalar(0, s64)
173 .widenScalarToNextPow2(0);
Tim Northover2c4a8382016-08-25 17:37:25 +0000174
Ahmed Bougacha106dd032017-09-12 21:04:11 +0000175 // FP conversions
Daniel Sanders79cb8392018-01-29 19:54:49 +0000176 getActionDefinitionsBuilder(G_FPTRUNC).legalFor(
177 {{s16, s32}, {s16, s64}, {s32, s64}});
178 getActionDefinitionsBuilder(G_FPEXT).legalFor(
179 {{s32, s16}, {s64, s16}, {s64, s32}});
Tim Northover438c77c2016-08-25 17:37:32 +0000180
Tim Northover5d0eaa42016-08-26 17:45:58 +0000181 // Conversions
Daniel Sanders79cb8392018-01-29 19:54:49 +0000182 getActionDefinitionsBuilder({G_FPTOSI, G_FPTOUI})
183 .legalForCartesianProduct({s32, s64})
184 .clampScalar(0, s32, s64)
185 .widenScalarToNextPow2(0)
186 .clampScalar(1, s32, s64)
187 .widenScalarToNextPow2(1);
Tim Northover5d0eaa42016-08-26 17:45:58 +0000188
Daniel Sanders79cb8392018-01-29 19:54:49 +0000189 getActionDefinitionsBuilder({G_SITOFP, G_UITOFP})
190 .legalForCartesianProduct({s32, s64})
191 .clampScalar(1, s32, s64)
192 .widenScalarToNextPow2(1)
193 .clampScalar(0, s32, s64)
194 .widenScalarToNextPow2(0);
Tim Northover438c77c2016-08-25 17:37:32 +0000195
Tim Northoverb3a0be42016-08-23 21:01:20 +0000196 // Control-flow
Daniel Sanders79cb8392018-01-29 19:54:49 +0000197 getActionDefinitionsBuilder(G_BRCOND).legalFor({s1, s8, s16, s32});
198 getActionDefinitionsBuilder(G_BRINDIRECT).legalFor({p0});
Ahmed Bougachaad30db32016-08-02 15:10:28 +0000199
Tim Northover1d18a992016-08-26 17:46:03 +0000200 // Select
Daniel Sanders79cb8392018-01-29 19:54:49 +0000201 getActionDefinitionsBuilder(G_SELECT)
202 .legalFor({{s32, s1}, {s64, s1}, {p0, s1}})
203 .clampScalar(0, s32, s64)
204 .widenScalarToNextPow2(0);
Tim Northover1d18a992016-08-26 17:46:03 +0000205
Tim Northoverb3a0be42016-08-23 21:01:20 +0000206 // Pointer-handling
Daniel Sanders79cb8392018-01-29 19:54:49 +0000207 getActionDefinitionsBuilder(G_FRAME_INDEX).legalFor({p0});
208 getActionDefinitionsBuilder(G_GLOBAL_VALUE).legalFor({p0});
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000209
Daniel Sanders79cb8392018-01-29 19:54:49 +0000210 getActionDefinitionsBuilder(G_PTRTOINT)
211 .legalForCartesianProduct({s1, s8, s16, s32, s64}, {p0})
212 .maxScalar(0, s64)
213 .widenScalarToNextPow2(0, /*Min*/ 8);
Tim Northover037af52c2016-10-31 18:31:09 +0000214
Daniel Sanders79cb8392018-01-29 19:54:49 +0000215 getActionDefinitionsBuilder(G_INTTOPTR)
216 .unsupportedIf([&](const LegalityQuery &Query) {
217 return Query.Types[0].getSizeInBits() != Query.Types[1].getSizeInBits();
218 })
219 .legalFor({s64, p0});
Tim Northover456a3c02016-08-23 19:30:38 +0000220
Quentin Colombet404e4352016-10-12 03:57:43 +0000221 // Casts for 32 and 64-bit width type are just copies.
Quentin Colombet7c114d32017-10-16 22:28:27 +0000222 // Same for 128-bit width type, except they are on the FPR bank.
Daniel Sanders79cb8392018-01-29 19:54:49 +0000223 getActionDefinitionsBuilder(G_BITCAST)
224 // FIXME: This is wrong since G_BITCAST is not allowed to change the
225 // number of bits but it's what the previous code described and fixing
226 // it breaks tests.
227 .legalForCartesianProduct({s1, s8, s16, s32, s64, s128, v16s8, v8s8, v4s8,
228 v8s16, v4s16, v2s16, v4s32, v2s32, v2s64});
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000229
Daniel Sanders79cb8392018-01-29 19:54:49 +0000230 getActionDefinitionsBuilder(G_VASTART).legalFor({p0});
Tim Northovere9600d82017-02-08 17:57:27 +0000231
Tim Northover91366172017-02-15 23:22:50 +0000232 // va_list must be a pointer, but most sized types are pretty easy to handle
233 // as the destination.
Daniel Sanders79cb8392018-01-29 19:54:49 +0000234 getActionDefinitionsBuilder(G_VAARG)
235 .customForCartesianProduct({s8, s16, s32, s64, p0}, {p0})
236 .clampScalar(0, s8, s64)
237 .widenScalarToNextPow2(0, /*Min*/ 8);
Tim Northover91366172017-02-15 23:22:50 +0000238
Daniel Sanders79cb8392018-01-29 19:54:49 +0000239 if (ST.hasLSE()) {
240 getActionDefinitionsBuilder(G_ATOMIC_CMPXCHG)
241 .legalForCartesianProduct({s8, s16, s32, s64}, {p0});
242 }
243 getActionDefinitionsBuilder(G_ATOMIC_CMPXCHG);
Tim Northover91366172017-02-15 23:22:50 +0000244
Daniel Sanders7fe7acc2017-11-28 20:21:15 +0000245 if (ST.hasLSE()) {
Daniel Sandersaef1dfc2017-11-30 20:11:42 +0000246 for (auto Ty : {s8, s16, s32, s64}) {
247 setAction({G_ATOMIC_CMPXCHG_WITH_SUCCESS, Ty}, Lower);
Daniel Sandersaef1dfc2017-11-30 20:11:42 +0000248 }
Daniel Sanders7fe7acc2017-11-28 20:21:15 +0000249
Daniel Sanders79cb8392018-01-29 19:54:49 +0000250 getActionDefinitionsBuilder(
251 {G_ATOMICRMW_XCHG, G_ATOMICRMW_ADD, G_ATOMICRMW_SUB, G_ATOMICRMW_AND,
252 G_ATOMICRMW_OR, G_ATOMICRMW_XOR, G_ATOMICRMW_MIN, G_ATOMICRMW_MAX,
253 G_ATOMICRMW_UMIN, G_ATOMICRMW_UMAX})
254 .legalForCartesianProduct({s8, s16, s32, s64}, {p0});
Daniel Sanders7fe7acc2017-11-28 20:21:15 +0000255 }
256
Volkan Kelesa32ff002017-12-01 08:19:10 +0000257 // Merge/Unmerge
Daniel Sanders79cb8392018-01-29 19:54:49 +0000258 for (unsigned Op : {G_MERGE_VALUES, G_UNMERGE_VALUES}) {
259 unsigned BigTyIdx = Op == G_MERGE_VALUES ? 0 : 1;
260 unsigned LitTyIdx = Op == G_MERGE_VALUES ? 1 : 0;
261
262 auto notValidElt = [](const LegalityQuery &Query, unsigned TypeIdx) {
263 const LLT &Ty = Query.Types[TypeIdx];
264 if (Ty.isVector()) {
265 const LLT &EltTy = Ty.getElementType();
266 if (EltTy.getSizeInBits() < 8 || EltTy.getSizeInBits() > 64)
267 return true;
268 if (!isPowerOf2_32(EltTy.getSizeInBits()))
269 return true;
Volkan Kelesa32ff002017-12-01 08:19:10 +0000270 }
Daniel Sanders79cb8392018-01-29 19:54:49 +0000271 return false;
272 };
273 auto scalarize =
274 [](const LegalityQuery &Query, unsigned TypeIdx) {
275 const LLT &Ty = Query.Types[TypeIdx];
276 return std::make_pair(TypeIdx, Ty.getElementType());
277 };
278
279 // FIXME: This rule is horrible, but specifies the same as what we had
280 // before with the particularly strange definitions removed (e.g.
281 // s8 = G_MERGE_VALUES s32, s32).
282 // Part of the complexity comes from these ops being extremely flexible. For
283 // example, you can build/decompose vectors with it, concatenate vectors,
284 // etc. and in addition to this you can also bitcast with it at the same
285 // time. We've been considering breaking it up into multiple ops to make it
286 // more manageable throughout the backend.
287 getActionDefinitionsBuilder(Op)
288 // Break up vectors with weird elements into scalars
289 .fewerElementsIf(
290 [=](const LegalityQuery &Query) { return notValidElt(Query, 0); },
291 [=](const LegalityQuery &Query) { return scalarize(Query, 0); })
292 .fewerElementsIf(
293 [=](const LegalityQuery &Query) { return notValidElt(Query, 1); },
294 [=](const LegalityQuery &Query) { return scalarize(Query, 1); })
295 // Clamp the big scalar to s8-s512 and make it either a power of 2, 192,
296 // or 384.
297 .clampScalar(BigTyIdx, s8, s512)
298 .widenScalarIf(
299 [=](const LegalityQuery &Query) {
300 const LLT &Ty = Query.Types[BigTyIdx];
301 return !isPowerOf2_32(Ty.getSizeInBits()) &&
302 Ty.getSizeInBits() % 64 != 0;
303 },
304 [=](const LegalityQuery &Query) {
305 // Pick the next power of 2, or a multiple of 64 over 128.
306 // Whichever is smaller.
307 const LLT &Ty = Query.Types[BigTyIdx];
308 unsigned NewSizeInBits = 1
309 << Log2_32_Ceil(Ty.getSizeInBits() + 1);
310 if (NewSizeInBits >= 256) {
311 unsigned RoundedTo = alignTo<64>(Ty.getSizeInBits() + 1);
312 if (RoundedTo < NewSizeInBits)
313 NewSizeInBits = RoundedTo;
314 }
315 return std::make_pair(BigTyIdx, LLT::scalar(NewSizeInBits));
316 })
317 // Clamp the little scalar to s8-s256 and make it a power of 2. It's not
318 // worth considering the multiples of 64 since 2*192 and 2*384 are not
319 // valid.
320 .clampScalar(LitTyIdx, s8, s256)
321 .widenScalarToNextPow2(LitTyIdx, /*Min*/ 8)
322 // So at this point, we have s8, s16, s32, s64, s128, s192, s256, s384,
323 // s512, <X x s8>, <X x s16>, <X x s32>, or <X x s64>.
324 // At this point it's simple enough to accept the legal types.
325 .legalIf([=](const LegalityQuery &Query) {
326 const LLT &BigTy = Query.Types[BigTyIdx];
327 const LLT &LitTy = Query.Types[LitTyIdx];
328 if (BigTy.isVector() && BigTy.getSizeInBits() < 32)
329 return false;
330 if (LitTy.isVector() && LitTy.getSizeInBits() < 32)
331 return false;
332 return BigTy.getSizeInBits() % LitTy.getSizeInBits() == 0;
333 })
334 // Any vectors left are the wrong size. Scalarize them.
335 .fewerElementsIf([](const LegalityQuery &Query) { return true; },
336 [](const LegalityQuery &Query) {
337 return std::make_pair(
338 0, Query.Types[0].getElementType());
339 })
340 .fewerElementsIf([](const LegalityQuery &Query) { return true; },
341 [](const LegalityQuery &Query) {
342 return std::make_pair(
343 1, Query.Types[1].getElementType());
344 });
345 }
Volkan Kelesa32ff002017-12-01 08:19:10 +0000346
Tim Northover33b07d62016-07-22 20:03:43 +0000347 computeTables();
348}
Tim Northover91366172017-02-15 23:22:50 +0000349
350bool AArch64LegalizerInfo::legalizeCustom(MachineInstr &MI,
351 MachineRegisterInfo &MRI,
352 MachineIRBuilder &MIRBuilder) const {
353 switch (MI.getOpcode()) {
354 default:
355 // No idea what to do.
356 return false;
357 case TargetOpcode::G_VAARG:
358 return legalizeVaArg(MI, MRI, MIRBuilder);
359 }
360
361 llvm_unreachable("expected switch to return");
362}
363
364bool AArch64LegalizerInfo::legalizeVaArg(MachineInstr &MI,
365 MachineRegisterInfo &MRI,
366 MachineIRBuilder &MIRBuilder) const {
367 MIRBuilder.setInstr(MI);
368 MachineFunction &MF = MIRBuilder.getMF();
369 unsigned Align = MI.getOperand(2).getImm();
370 unsigned Dst = MI.getOperand(0).getReg();
371 unsigned ListPtr = MI.getOperand(1).getReg();
372
373 LLT PtrTy = MRI.getType(ListPtr);
374 LLT IntPtrTy = LLT::scalar(PtrTy.getSizeInBits());
375
376 const unsigned PtrSize = PtrTy.getSizeInBits() / 8;
377 unsigned List = MRI.createGenericVirtualRegister(PtrTy);
378 MIRBuilder.buildLoad(
379 List, ListPtr,
380 *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOLoad,
381 PtrSize, /* Align = */ PtrSize));
382
383 unsigned DstPtr;
384 if (Align > PtrSize) {
385 // Realign the list to the actual required alignment.
Aditya Nandakumar17451212017-07-06 19:40:07 +0000386 auto AlignMinus1 = MIRBuilder.buildConstant(IntPtrTy, Align - 1);
Tim Northover91366172017-02-15 23:22:50 +0000387
388 unsigned ListTmp = MRI.createGenericVirtualRegister(PtrTy);
Aditya Nandakumar17451212017-07-06 19:40:07 +0000389 MIRBuilder.buildGEP(ListTmp, List, AlignMinus1->getOperand(0).getReg());
Tim Northover91366172017-02-15 23:22:50 +0000390
391 DstPtr = MRI.createGenericVirtualRegister(PtrTy);
392 MIRBuilder.buildPtrMask(DstPtr, ListTmp, Log2_64(Align));
393 } else
394 DstPtr = List;
395
396 uint64_t ValSize = MRI.getType(Dst).getSizeInBits() / 8;
397 MIRBuilder.buildLoad(
398 Dst, DstPtr,
399 *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOLoad,
400 ValSize, std::max(Align, PtrSize)));
401
402 unsigned SizeReg = MRI.createGenericVirtualRegister(IntPtrTy);
403 MIRBuilder.buildConstant(SizeReg, alignTo(ValSize, PtrSize));
404
405 unsigned NewList = MRI.createGenericVirtualRegister(PtrTy);
406 MIRBuilder.buildGEP(NewList, DstPtr, SizeReg);
407
408 MIRBuilder.buildStore(
409 NewList, ListPtr,
410 *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOStore,
411 PtrSize, /* Align = */ PtrSize));
412
413 MI.eraseFromParent();
414 return true;
415}