blob: f7027394f8031621dbef904361016f75725a6d44 [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"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
Tim Northover91366172017-02-15 23:22:50 +000017#include "llvm/CodeGen/MachineInstr.h"
18#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000019#include "llvm/CodeGen/TargetOpcodes.h"
Tim Northover33b07d62016-07-22 20:03:43 +000020#include "llvm/CodeGen/ValueTypes.h"
Tim Northover33b07d62016-07-22 20:03:43 +000021#include "llvm/IR/DerivedTypes.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000022#include "llvm/IR/Type.h"
Tim Northover33b07d62016-07-22 20:03:43 +000023
24using namespace llvm;
25
Kristof Beylsaf9814a2017-11-07 10:34:34 +000026/// FIXME: The following static functions are SizeChangeStrategy functions
27/// that are meant to temporarily mimic the behaviour of the old legalization
28/// based on doubling/halving non-legal types as closely as possible. This is
29/// not entirly possible as only legalizing the types that are exactly a power
30/// of 2 times the size of the legal types would require specifying all those
31/// sizes explicitly.
32/// In practice, not specifying those isn't a problem, and the below functions
33/// should disappear quickly as we add support for legalizing non-power-of-2
34/// sized types further.
35static void
36addAndInterleaveWithUnsupported(LegalizerInfo::SizeAndActionsVec &result,
37 const LegalizerInfo::SizeAndActionsVec &v) {
38 for (unsigned i = 0; i < v.size(); ++i) {
39 result.push_back(v[i]);
40 if (i + 1 < v[i].first && i + 1 < v.size() &&
41 v[i + 1].first != v[i].first + 1)
42 result.push_back({v[i].first + 1, LegalizerInfo::Unsupported});
43 }
44}
45
46static LegalizerInfo::SizeAndActionsVec
47widen_1_narrow_128_ToLargest(const LegalizerInfo::SizeAndActionsVec &v) {
48 assert(v.size() >= 1);
49 assert(v[0].first > 2);
50 LegalizerInfo::SizeAndActionsVec result = {{1, LegalizerInfo::WidenScalar},
51 {2, LegalizerInfo::Unsupported}};
52 addAndInterleaveWithUnsupported(result, v);
53 auto Largest = result.back().first;
54 assert(Largest + 1 < 128);
55 result.push_back({Largest + 1, LegalizerInfo::Unsupported});
56 result.push_back({128, LegalizerInfo::NarrowScalar});
57 result.push_back({129, LegalizerInfo::Unsupported});
58 return result;
59}
60
61static LegalizerInfo::SizeAndActionsVec
62widen_16(const LegalizerInfo::SizeAndActionsVec &v) {
63 assert(v.size() >= 1);
64 assert(v[0].first > 17);
65 LegalizerInfo::SizeAndActionsVec result = {{1, LegalizerInfo::Unsupported},
66 {16, LegalizerInfo::WidenScalar},
67 {17, LegalizerInfo::Unsupported}};
68 addAndInterleaveWithUnsupported(result, v);
69 auto Largest = result.back().first;
70 result.push_back({Largest + 1, LegalizerInfo::Unsupported});
71 return result;
72}
73
74static LegalizerInfo::SizeAndActionsVec
75widen_1_8(const LegalizerInfo::SizeAndActionsVec &v) {
76 assert(v.size() >= 1);
77 assert(v[0].first > 9);
78 LegalizerInfo::SizeAndActionsVec result = {
79 {1, LegalizerInfo::WidenScalar}, {2, LegalizerInfo::Unsupported},
80 {8, LegalizerInfo::WidenScalar}, {9, LegalizerInfo::Unsupported}};
81 addAndInterleaveWithUnsupported(result, v);
82 auto Largest = result.back().first;
83 result.push_back({Largest + 1, LegalizerInfo::Unsupported});
84 return result;
85}
86
87static LegalizerInfo::SizeAndActionsVec
88widen_1_8_16(const LegalizerInfo::SizeAndActionsVec &v) {
89 assert(v.size() >= 1);
90 assert(v[0].first > 17);
91 LegalizerInfo::SizeAndActionsVec result = {
92 {1, LegalizerInfo::WidenScalar}, {2, LegalizerInfo::Unsupported},
93 {8, LegalizerInfo::WidenScalar}, {9, LegalizerInfo::Unsupported},
94 {16, LegalizerInfo::WidenScalar}, {17, LegalizerInfo::Unsupported}};
95 addAndInterleaveWithUnsupported(result, v);
96 auto Largest = result.back().first;
97 result.push_back({Largest + 1, LegalizerInfo::Unsupported});
98 return result;
99}
100
101static LegalizerInfo::SizeAndActionsVec
102widen_1_8_16_narrowToLargest(const LegalizerInfo::SizeAndActionsVec &v) {
103 assert(v.size() >= 1);
104 assert(v[0].first > 17);
105 LegalizerInfo::SizeAndActionsVec result = {
106 {1, LegalizerInfo::WidenScalar}, {2, LegalizerInfo::Unsupported},
107 {8, LegalizerInfo::WidenScalar}, {9, LegalizerInfo::Unsupported},
108 {16, LegalizerInfo::WidenScalar}, {17, LegalizerInfo::Unsupported}};
109 addAndInterleaveWithUnsupported(result, v);
110 auto Largest = result.back().first;
111 result.push_back({Largest + 1, LegalizerInfo::NarrowScalar});
112 return result;
113}
114
115static LegalizerInfo::SizeAndActionsVec
116widen_1_8_16_32(const LegalizerInfo::SizeAndActionsVec &v) {
117 assert(v.size() >= 1);
118 assert(v[0].first > 33);
119 LegalizerInfo::SizeAndActionsVec result = {
120 {1, LegalizerInfo::WidenScalar}, {2, LegalizerInfo::Unsupported},
121 {8, LegalizerInfo::WidenScalar}, {9, LegalizerInfo::Unsupported},
122 {16, LegalizerInfo::WidenScalar}, {17, LegalizerInfo::Unsupported},
123 {32, LegalizerInfo::WidenScalar}, {33, LegalizerInfo::Unsupported}};
124 addAndInterleaveWithUnsupported(result, v);
125 auto Largest = result.back().first;
126 result.push_back({Largest + 1, LegalizerInfo::Unsupported});
127 return result;
128}
129
Tim Northover69fa84a2016-10-14 22:18:18 +0000130AArch64LegalizerInfo::AArch64LegalizerInfo() {
Ahmed Bougachaad30db32016-08-02 15:10:28 +0000131 using namespace TargetOpcode;
Tim Northover5ae83502016-09-15 09:20:34 +0000132 const LLT p0 = LLT::pointer(0, 64);
Tim Northoverea904f92016-08-19 22:40:00 +0000133 const LLT s1 = LLT::scalar(1);
Tim Northover9656f142016-08-04 20:54:13 +0000134 const LLT s8 = LLT::scalar(8);
135 const LLT s16 = LLT::scalar(16);
Ahmed Bougachaad30db32016-08-02 15:10:28 +0000136 const LLT s32 = LLT::scalar(32);
137 const LLT s64 = LLT::scalar(64);
Quentin Colombet7c114d32017-10-16 22:28:27 +0000138 const LLT s128 = LLT::scalar(128);
Ahmed Bougachaad30db32016-08-02 15:10:28 +0000139 const LLT v2s32 = LLT::vector(2, 32);
140 const LLT v4s32 = LLT::vector(4, 32);
141 const LLT v2s64 = LLT::vector(2, 64);
142
Tim Northoverff5e7e12017-06-30 20:27:36 +0000143 for (auto Ty : {p0, s1, s8, s16, s32, s64})
144 setAction({G_IMPLICIT_DEF, Ty}, Legal);
145
Amara Emerson1cd89ca2017-10-08 15:29:11 +0000146 for (auto Ty : {s16, s32, s64, p0})
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000147 setAction({G_PHI, Ty}, Legal);
148
Kristof Beylsaf9814a2017-11-07 10:34:34 +0000149 setLegalizeScalarToDifferentSizeStrategy(G_PHI, 0, widen_1_8);
Aditya Nandakumar892979e2017-08-25 04:57:27 +0000150
Daniel Sanders83e23d12017-09-19 14:25:15 +0000151 for (auto Ty : { s32, s64 })
152 setAction({G_BSWAP, Ty}, Legal);
153
Quentin Colombet24203cf2017-01-27 01:13:30 +0000154 for (unsigned BinOp : {G_ADD, G_SUB, G_MUL, G_AND, G_OR, G_XOR, G_SHL}) {
Tim Northoverfe880a82016-08-25 17:37:39 +0000155 // These operations naturally get the right answer when used on
156 // GPR32, even if the actual type is narrower.
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000157 for (auto Ty : {s32, s64, v2s32, v4s32, v2s64})
Quentin Colombete15e4602017-01-27 01:13:25 +0000158 setAction({BinOp, Ty}, Legal);
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000159
Kristof Beylsaf9814a2017-11-07 10:34:34 +0000160 if (BinOp != G_ADD)
161 setLegalizeScalarToDifferentSizeStrategy(BinOp, 0,
162 widen_1_8_16_narrowToLargest);
Tim Northover9656f142016-08-04 20:54:13 +0000163 }
164
Quentin Colombete15e4602017-01-27 01:13:25 +0000165 setAction({G_GEP, p0}, Legal);
166 setAction({G_GEP, 1, s64}, Legal);
Tim Northover22d82cf2016-09-15 11:02:19 +0000167
Kristof Beylsaf9814a2017-11-07 10:34:34 +0000168 setLegalizeScalarToDifferentSizeStrategy(G_GEP, 1, widen_1_8_16_32);
Tim Northover22d82cf2016-09-15 11:02:19 +0000169
Tim Northover398c5f52017-02-14 20:56:29 +0000170 setAction({G_PTR_MASK, p0}, Legal);
171
Quentin Colombet24203cf2017-01-27 01:13:30 +0000172 for (unsigned BinOp : {G_LSHR, G_ASHR, G_SDIV, G_UDIV}) {
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000173 for (auto Ty : {s32, s64})
Quentin Colombete15e4602017-01-27 01:13:25 +0000174 setAction({BinOp, Ty}, Legal);
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000175
Kristof Beylsaf9814a2017-11-07 10:34:34 +0000176 setLegalizeScalarToDifferentSizeStrategy(BinOp, 0, widen_1_8_16);
Tim Northover7a753d92016-08-26 17:46:06 +0000177 }
178
Quentin Colombet24203cf2017-01-27 01:13:30 +0000179 for (unsigned BinOp : {G_SREM, G_UREM})
Tim Northovercecee562016-08-26 17:46:13 +0000180 for (auto Ty : { s1, s8, s16, s32, s64 })
Quentin Colombete15e4602017-01-27 01:13:25 +0000181 setAction({BinOp, Ty}, Lower);
Tim Northovercecee562016-08-26 17:46:13 +0000182
Kristof Beylsaf9814a2017-11-07 10:34:34 +0000183 for (unsigned Op : {G_SMULO, G_UMULO}) {
184 setAction({Op, 0, s64}, Lower);
185 setAction({Op, 1, s1}, Legal);
186 }
Tim Northover0a9b2792017-02-08 21:22:15 +0000187
188 for (unsigned Op : {G_UADDE, G_USUBE, G_SADDO, G_SSUBO, G_SMULH, G_UMULH}) {
Tim Northover438c77c2016-08-25 17:37:32 +0000189 for (auto Ty : { s32, s64 })
Quentin Colombete15e4602017-01-27 01:13:25 +0000190 setAction({Op, Ty}, Legal);
Tim Northover438c77c2016-08-25 17:37:32 +0000191
Quentin Colombete15e4602017-01-27 01:13:25 +0000192 setAction({Op, 1, s1}, Legal);
Tim Northoverd8a6d7c2016-08-25 17:37:41 +0000193 }
194
Daniel Sanders40b66d62017-07-18 14:10:07 +0000195 for (unsigned BinOp : {G_FADD, G_FSUB, G_FMA, G_FMUL, G_FDIV})
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000196 for (auto Ty : {s32, s64})
Quentin Colombete15e4602017-01-27 01:13:25 +0000197 setAction({BinOp, Ty}, Legal);
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000198
Tim Northovere0418412017-02-08 23:23:39 +0000199 for (unsigned BinOp : {G_FREM, G_FPOW}) {
200 setAction({BinOp, s32}, Libcall);
201 setAction({BinOp, s64}, Libcall);
202 }
Tim Northoveredb3c8c2016-08-29 19:07:16 +0000203
Tim Northover3e6a7af2017-03-03 23:05:47 +0000204 for (auto Ty : {s32, s64, p0}) {
Tim Northover0e6afbd2017-02-06 21:56:47 +0000205 setAction({G_INSERT, Ty}, Legal);
206 setAction({G_INSERT, 1, Ty}, Legal);
207 }
Kristof Beylsaf9814a2017-11-07 10:34:34 +0000208 setLegalizeScalarToDifferentSizeStrategy(G_INSERT, 0,
209 widen_1_8_16_narrowToLargest);
Tim Northover0e6afbd2017-02-06 21:56:47 +0000210 for (auto Ty : {s1, s8, s16}) {
Tim Northover3e6a7af2017-03-03 23:05:47 +0000211 setAction({G_INSERT, 1, Ty}, Legal);
Tim Northover0e6afbd2017-02-06 21:56:47 +0000212 // FIXME: Can't widen the sources because that violates the constraints on
213 // G_INSERT (It seems entirely reasonable that inputs shouldn't overlap).
214 }
215
Tim Northoverc2d5e6d2017-06-26 20:34:13 +0000216 for (auto Ty : {s1, s8, s16, s32, s64, p0})
217 setAction({G_EXTRACT, Ty}, Legal);
218
219 for (auto Ty : {s32, s64})
220 setAction({G_EXTRACT, 1, Ty}, Legal);
221
Quentin Colombet24203cf2017-01-27 01:13:30 +0000222 for (unsigned MemOp : {G_LOAD, G_STORE}) {
Quentin Colombetd3126d52016-10-11 00:21:08 +0000223 for (auto Ty : {s8, s16, s32, s64, p0, v2s32})
Quentin Colombete15e4602017-01-27 01:13:25 +0000224 setAction({MemOp, Ty}, Legal);
Ahmed Bougachaad30db32016-08-02 15:10:28 +0000225
Kristof Beylsaf9814a2017-11-07 10:34:34 +0000226 setLegalizeScalarToDifferentSizeStrategy(MemOp, 0,
227 widen_1_narrow_128_ToLargest);
Tim Northovera01bece2016-08-23 19:30:42 +0000228
229 // And everything's fine in addrspace 0.
Quentin Colombete15e4602017-01-27 01:13:25 +0000230 setAction({MemOp, 1, p0}, Legal);
Tim Northover3c73e362016-08-23 18:20:09 +0000231 }
232
Tim Northoverb3a0be42016-08-23 21:01:20 +0000233 // Constants
Tim Northoverea904f92016-08-19 22:40:00 +0000234 for (auto Ty : {s32, s64}) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000235 setAction({TargetOpcode::G_CONSTANT, Ty}, Legal);
236 setAction({TargetOpcode::G_FCONSTANT, Ty}, Legal);
Tim Northoverea904f92016-08-19 22:40:00 +0000237 }
238
Quentin Colombete15e4602017-01-27 01:13:25 +0000239 setAction({G_CONSTANT, p0}, Legal);
Tim Northover7a1ec012016-08-25 17:37:35 +0000240
Kristof Beylsaf9814a2017-11-07 10:34:34 +0000241 setLegalizeScalarToDifferentSizeStrategy(G_CONSTANT, 0, widen_1_8_16);
242 setLegalizeScalarToDifferentSizeStrategy(G_FCONSTANT, 0, widen_16);
Tim Northover9656f142016-08-04 20:54:13 +0000243
Quentin Colombete15e4602017-01-27 01:13:25 +0000244 setAction({G_ICMP, 1, s32}, Legal);
245 setAction({G_ICMP, 1, s64}, Legal);
246 setAction({G_ICMP, 1, p0}, Legal);
Tim Northover6cd4b232016-08-23 21:01:26 +0000247
Kristof Beylsaf9814a2017-11-07 10:34:34 +0000248 setLegalizeScalarToDifferentSizeStrategy(G_ICMP, 0, widen_1_8_16);
249 setLegalizeScalarToDifferentSizeStrategy(G_FCMP, 0, widen_1_8_16);
250 setLegalizeScalarToDifferentSizeStrategy(G_ICMP, 1, widen_1_8_16);
Tim Northover6cd4b232016-08-23 21:01:26 +0000251
Aditya Nandakumar02c602e2017-07-31 17:00:16 +0000252 setAction({G_ICMP, s32}, Legal);
253 setAction({G_FCMP, s32}, Legal);
Quentin Colombete15e4602017-01-27 01:13:25 +0000254 setAction({G_FCMP, 1, s32}, Legal);
255 setAction({G_FCMP, 1, s64}, Legal);
Tim Northover30bd36e2016-08-26 17:46:19 +0000256
Tim Northover2c4a8382016-08-25 17:37:25 +0000257 // Extensions
258 for (auto Ty : { s1, s8, s16, s32, s64 }) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000259 setAction({G_ZEXT, Ty}, Legal);
260 setAction({G_SEXT, Ty}, Legal);
261 setAction({G_ANYEXT, Ty}, Legal);
Tim Northover2c4a8382016-08-25 17:37:25 +0000262 }
263
Ahmed Bougacha106dd032017-09-12 21:04:11 +0000264 // FP conversions
265 for (auto Ty : { s16, s32 }) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000266 setAction({G_FPTRUNC, Ty}, Legal);
Ahmed Bougacha106dd032017-09-12 21:04:11 +0000267 setAction({G_FPEXT, 1, Ty}, Legal);
268 }
Tim Northover438c77c2016-08-25 17:37:32 +0000269
Ahmed Bougacha106dd032017-09-12 21:04:11 +0000270 for (auto Ty : { s32, s64 }) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000271 setAction({G_FPTRUNC, 1, Ty}, Legal);
Ahmed Bougacha106dd032017-09-12 21:04:11 +0000272 setAction({G_FPEXT, Ty}, Legal);
273 }
Tim Northover438c77c2016-08-25 17:37:32 +0000274
Tim Northover5d0eaa42016-08-26 17:45:58 +0000275 // Conversions
Ahmed Bougachad2948232017-01-20 01:37:24 +0000276 for (auto Ty : { s32, s64 }) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000277 setAction({G_FPTOSI, 0, Ty}, Legal);
278 setAction({G_FPTOUI, 0, Ty}, Legal);
279 setAction({G_SITOFP, 1, Ty}, Legal);
280 setAction({G_UITOFP, 1, Ty}, Legal);
Tim Northover5d0eaa42016-08-26 17:45:58 +0000281 }
Kristof Beylsaf9814a2017-11-07 10:34:34 +0000282 setLegalizeScalarToDifferentSizeStrategy(G_FPTOSI, 0, widen_1_8_16);
283 setLegalizeScalarToDifferentSizeStrategy(G_FPTOUI, 0, widen_1_8_16);
284 setLegalizeScalarToDifferentSizeStrategy(G_SITOFP, 1, widen_1_8_16);
285 setLegalizeScalarToDifferentSizeStrategy(G_UITOFP, 1, widen_1_8_16);
Tim Northover5d0eaa42016-08-26 17:45:58 +0000286
287 for (auto Ty : { s32, s64 }) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000288 setAction({G_FPTOSI, 1, Ty}, Legal);
289 setAction({G_FPTOUI, 1, Ty}, Legal);
290 setAction({G_SITOFP, 0, Ty}, Legal);
291 setAction({G_UITOFP, 0, Ty}, Legal);
Tim Northover5d0eaa42016-08-26 17:45:58 +0000292 }
Tim Northover438c77c2016-08-25 17:37:32 +0000293
Tim Northoverb3a0be42016-08-23 21:01:20 +0000294 // Control-flow
Tim Northover6aacd272016-10-12 22:48:36 +0000295 for (auto Ty : {s1, s8, s16, s32})
Quentin Colombete15e4602017-01-27 01:13:25 +0000296 setAction({G_BRCOND, Ty}, Legal);
Kristof Beyls65a12c02017-01-30 09:13:18 +0000297 setAction({G_BRINDIRECT, p0}, Legal);
Ahmed Bougachaad30db32016-08-02 15:10:28 +0000298
Tim Northover1d18a992016-08-26 17:46:03 +0000299 // Select
Kristof Beylsaf9814a2017-11-07 10:34:34 +0000300 setLegalizeScalarToDifferentSizeStrategy(G_SELECT, 0, widen_1_8_16);
Tim Northover868332d2017-02-06 23:41:27 +0000301
302 for (auto Ty : {s32, s64, p0})
Quentin Colombete15e4602017-01-27 01:13:25 +0000303 setAction({G_SELECT, Ty}, Legal);
Tim Northover1d18a992016-08-26 17:46:03 +0000304
Quentin Colombete15e4602017-01-27 01:13:25 +0000305 setAction({G_SELECT, 1, s1}, Legal);
Tim Northover1d18a992016-08-26 17:46:03 +0000306
Tim Northoverb3a0be42016-08-23 21:01:20 +0000307 // Pointer-handling
Quentin Colombete15e4602017-01-27 01:13:25 +0000308 setAction({G_FRAME_INDEX, p0}, Legal);
309 setAction({G_GLOBAL_VALUE, p0}, Legal);
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000310
Tim Northover037af52c2016-10-31 18:31:09 +0000311 for (auto Ty : {s1, s8, s16, s32, s64})
Quentin Colombete15e4602017-01-27 01:13:25 +0000312 setAction({G_PTRTOINT, 0, Ty}, Legal);
Tim Northover037af52c2016-10-31 18:31:09 +0000313
Quentin Colombete15e4602017-01-27 01:13:25 +0000314 setAction({G_PTRTOINT, 1, p0}, Legal);
Tim Northovera01bece2016-08-23 19:30:42 +0000315
Quentin Colombete15e4602017-01-27 01:13:25 +0000316 setAction({G_INTTOPTR, 0, p0}, Legal);
317 setAction({G_INTTOPTR, 1, s64}, Legal);
Tim Northover456a3c02016-08-23 19:30:38 +0000318
Quentin Colombet404e4352016-10-12 03:57:43 +0000319 // Casts for 32 and 64-bit width type are just copies.
Quentin Colombet7c114d32017-10-16 22:28:27 +0000320 // Same for 128-bit width type, except they are on the FPR bank.
321 for (auto Ty : {s1, s8, s16, s32, s64, s128}) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000322 setAction({G_BITCAST, 0, Ty}, Legal);
323 setAction({G_BITCAST, 1, Ty}, Legal);
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000324 }
325
Quentin Colombetdb643d92016-10-13 00:12:01 +0000326 // For the sake of copying bits around, the type does not really
327 // matter as long as it fits a register.
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000328 for (int EltSize = 8; EltSize <= 64; EltSize *= 2) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000329 setAction({G_BITCAST, 0, LLT::vector(128/EltSize, EltSize)}, Legal);
330 setAction({G_BITCAST, 1, LLT::vector(128/EltSize, EltSize)}, Legal);
Quentin Colombetdb643d92016-10-13 00:12:01 +0000331 if (EltSize >= 64)
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000332 continue;
333
Quentin Colombete15e4602017-01-27 01:13:25 +0000334 setAction({G_BITCAST, 0, LLT::vector(64/EltSize, EltSize)}, Legal);
335 setAction({G_BITCAST, 1, LLT::vector(64/EltSize, EltSize)}, Legal);
Quentin Colombetdb643d92016-10-13 00:12:01 +0000336 if (EltSize >= 32)
337 continue;
338
Quentin Colombete15e4602017-01-27 01:13:25 +0000339 setAction({G_BITCAST, 0, LLT::vector(32/EltSize, EltSize)}, Legal);
340 setAction({G_BITCAST, 1, LLT::vector(32/EltSize, EltSize)}, Legal);
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000341 }
342
Tim Northovere9600d82017-02-08 17:57:27 +0000343 setAction({G_VASTART, p0}, Legal);
344
Tim Northover91366172017-02-15 23:22:50 +0000345 // va_list must be a pointer, but most sized types are pretty easy to handle
346 // as the destination.
347 setAction({G_VAARG, 1, p0}, Legal);
348
349 for (auto Ty : {s8, s16, s32, s64, p0})
350 setAction({G_VAARG, Ty}, Custom);
351
Tim Northover33b07d62016-07-22 20:03:43 +0000352 computeTables();
353}
Tim Northover91366172017-02-15 23:22:50 +0000354
355bool AArch64LegalizerInfo::legalizeCustom(MachineInstr &MI,
356 MachineRegisterInfo &MRI,
357 MachineIRBuilder &MIRBuilder) const {
358 switch (MI.getOpcode()) {
359 default:
360 // No idea what to do.
361 return false;
362 case TargetOpcode::G_VAARG:
363 return legalizeVaArg(MI, MRI, MIRBuilder);
364 }
365
366 llvm_unreachable("expected switch to return");
367}
368
369bool AArch64LegalizerInfo::legalizeVaArg(MachineInstr &MI,
370 MachineRegisterInfo &MRI,
371 MachineIRBuilder &MIRBuilder) const {
372 MIRBuilder.setInstr(MI);
373 MachineFunction &MF = MIRBuilder.getMF();
374 unsigned Align = MI.getOperand(2).getImm();
375 unsigned Dst = MI.getOperand(0).getReg();
376 unsigned ListPtr = MI.getOperand(1).getReg();
377
378 LLT PtrTy = MRI.getType(ListPtr);
379 LLT IntPtrTy = LLT::scalar(PtrTy.getSizeInBits());
380
381 const unsigned PtrSize = PtrTy.getSizeInBits() / 8;
382 unsigned List = MRI.createGenericVirtualRegister(PtrTy);
383 MIRBuilder.buildLoad(
384 List, ListPtr,
385 *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOLoad,
386 PtrSize, /* Align = */ PtrSize));
387
388 unsigned DstPtr;
389 if (Align > PtrSize) {
390 // Realign the list to the actual required alignment.
Aditya Nandakumar17451212017-07-06 19:40:07 +0000391 auto AlignMinus1 = MIRBuilder.buildConstant(IntPtrTy, Align - 1);
Tim Northover91366172017-02-15 23:22:50 +0000392
393 unsigned ListTmp = MRI.createGenericVirtualRegister(PtrTy);
Aditya Nandakumar17451212017-07-06 19:40:07 +0000394 MIRBuilder.buildGEP(ListTmp, List, AlignMinus1->getOperand(0).getReg());
Tim Northover91366172017-02-15 23:22:50 +0000395
396 DstPtr = MRI.createGenericVirtualRegister(PtrTy);
397 MIRBuilder.buildPtrMask(DstPtr, ListTmp, Log2_64(Align));
398 } else
399 DstPtr = List;
400
401 uint64_t ValSize = MRI.getType(Dst).getSizeInBits() / 8;
402 MIRBuilder.buildLoad(
403 Dst, DstPtr,
404 *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOLoad,
405 ValSize, std::max(Align, PtrSize)));
406
407 unsigned SizeReg = MRI.createGenericVirtualRegister(IntPtrTy);
408 MIRBuilder.buildConstant(SizeReg, alignTo(ValSize, PtrSize));
409
410 unsigned NewList = MRI.createGenericVirtualRegister(PtrTy);
411 MIRBuilder.buildGEP(NewList, DstPtr, SizeReg);
412
413 MIRBuilder.buildStore(
414 NewList, ListPtr,
415 *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOStore,
416 PtrSize, /* Align = */ PtrSize));
417
418 MI.eraseFromParent();
419 return true;
420}