blob: 05df5120222985e98669b39e93d6244a6a676f56 [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;
26
Kristof Beylsaf9814a2017-11-07 10:34:34 +000027/// FIXME: The following static functions are SizeChangeStrategy functions
28/// that are meant to temporarily mimic the behaviour of the old legalization
29/// based on doubling/halving non-legal types as closely as possible. This is
30/// not entirly possible as only legalizing the types that are exactly a power
31/// of 2 times the size of the legal types would require specifying all those
32/// sizes explicitly.
33/// In practice, not specifying those isn't a problem, and the below functions
34/// should disappear quickly as we add support for legalizing non-power-of-2
35/// sized types further.
36static void
37addAndInterleaveWithUnsupported(LegalizerInfo::SizeAndActionsVec &result,
38 const LegalizerInfo::SizeAndActionsVec &v) {
39 for (unsigned i = 0; i < v.size(); ++i) {
40 result.push_back(v[i]);
41 if (i + 1 < v[i].first && i + 1 < v.size() &&
42 v[i + 1].first != v[i].first + 1)
43 result.push_back({v[i].first + 1, LegalizerInfo::Unsupported});
44 }
45}
46
47static LegalizerInfo::SizeAndActionsVec
48widen_1_narrow_128_ToLargest(const LegalizerInfo::SizeAndActionsVec &v) {
49 assert(v.size() >= 1);
50 assert(v[0].first > 2);
51 LegalizerInfo::SizeAndActionsVec result = {{1, LegalizerInfo::WidenScalar},
52 {2, LegalizerInfo::Unsupported}};
53 addAndInterleaveWithUnsupported(result, v);
54 auto Largest = result.back().first;
55 assert(Largest + 1 < 128);
56 result.push_back({Largest + 1, LegalizerInfo::Unsupported});
57 result.push_back({128, LegalizerInfo::NarrowScalar});
58 result.push_back({129, LegalizerInfo::Unsupported});
59 return result;
60}
61
62static LegalizerInfo::SizeAndActionsVec
63widen_16(const LegalizerInfo::SizeAndActionsVec &v) {
64 assert(v.size() >= 1);
65 assert(v[0].first > 17);
66 LegalizerInfo::SizeAndActionsVec result = {{1, LegalizerInfo::Unsupported},
67 {16, LegalizerInfo::WidenScalar},
68 {17, LegalizerInfo::Unsupported}};
69 addAndInterleaveWithUnsupported(result, v);
70 auto Largest = result.back().first;
71 result.push_back({Largest + 1, LegalizerInfo::Unsupported});
72 return result;
73}
74
75static LegalizerInfo::SizeAndActionsVec
76widen_1_8(const LegalizerInfo::SizeAndActionsVec &v) {
77 assert(v.size() >= 1);
78 assert(v[0].first > 9);
79 LegalizerInfo::SizeAndActionsVec result = {
80 {1, LegalizerInfo::WidenScalar}, {2, LegalizerInfo::Unsupported},
81 {8, LegalizerInfo::WidenScalar}, {9, LegalizerInfo::Unsupported}};
82 addAndInterleaveWithUnsupported(result, v);
83 auto Largest = result.back().first;
84 result.push_back({Largest + 1, LegalizerInfo::Unsupported});
85 return result;
86}
87
88static LegalizerInfo::SizeAndActionsVec
89widen_1_8_16(const LegalizerInfo::SizeAndActionsVec &v) {
90 assert(v.size() >= 1);
91 assert(v[0].first > 17);
92 LegalizerInfo::SizeAndActionsVec result = {
93 {1, LegalizerInfo::WidenScalar}, {2, LegalizerInfo::Unsupported},
94 {8, LegalizerInfo::WidenScalar}, {9, LegalizerInfo::Unsupported},
95 {16, LegalizerInfo::WidenScalar}, {17, LegalizerInfo::Unsupported}};
96 addAndInterleaveWithUnsupported(result, v);
97 auto Largest = result.back().first;
98 result.push_back({Largest + 1, LegalizerInfo::Unsupported});
99 return result;
100}
101
102static LegalizerInfo::SizeAndActionsVec
103widen_1_8_16_narrowToLargest(const LegalizerInfo::SizeAndActionsVec &v) {
104 assert(v.size() >= 1);
105 assert(v[0].first > 17);
106 LegalizerInfo::SizeAndActionsVec result = {
107 {1, LegalizerInfo::WidenScalar}, {2, LegalizerInfo::Unsupported},
108 {8, LegalizerInfo::WidenScalar}, {9, LegalizerInfo::Unsupported},
109 {16, LegalizerInfo::WidenScalar}, {17, LegalizerInfo::Unsupported}};
110 addAndInterleaveWithUnsupported(result, v);
111 auto Largest = result.back().first;
112 result.push_back({Largest + 1, LegalizerInfo::NarrowScalar});
113 return result;
114}
115
116static LegalizerInfo::SizeAndActionsVec
117widen_1_8_16_32(const LegalizerInfo::SizeAndActionsVec &v) {
118 assert(v.size() >= 1);
119 assert(v[0].first > 33);
120 LegalizerInfo::SizeAndActionsVec result = {
121 {1, LegalizerInfo::WidenScalar}, {2, LegalizerInfo::Unsupported},
122 {8, LegalizerInfo::WidenScalar}, {9, LegalizerInfo::Unsupported},
123 {16, LegalizerInfo::WidenScalar}, {17, LegalizerInfo::Unsupported},
124 {32, LegalizerInfo::WidenScalar}, {33, LegalizerInfo::Unsupported}};
125 addAndInterleaveWithUnsupported(result, v);
126 auto Largest = result.back().first;
127 result.push_back({Largest + 1, LegalizerInfo::Unsupported});
128 return result;
129}
130
Daniel Sanders7fe7acc2017-11-28 20:21:15 +0000131AArch64LegalizerInfo::AArch64LegalizerInfo(const AArch64Subtarget &ST) {
Ahmed Bougachaad30db32016-08-02 15:10:28 +0000132 using namespace TargetOpcode;
Tim Northover5ae83502016-09-15 09:20:34 +0000133 const LLT p0 = LLT::pointer(0, 64);
Tim Northoverea904f92016-08-19 22:40:00 +0000134 const LLT s1 = LLT::scalar(1);
Tim Northover9656f142016-08-04 20:54:13 +0000135 const LLT s8 = LLT::scalar(8);
136 const LLT s16 = LLT::scalar(16);
Ahmed Bougachaad30db32016-08-02 15:10:28 +0000137 const LLT s32 = LLT::scalar(32);
138 const LLT s64 = LLT::scalar(64);
Quentin Colombet7c114d32017-10-16 22:28:27 +0000139 const LLT s128 = LLT::scalar(128);
Ahmed Bougachaad30db32016-08-02 15:10:28 +0000140 const LLT v2s32 = LLT::vector(2, 32);
141 const LLT v4s32 = LLT::vector(4, 32);
142 const LLT v2s64 = LLT::vector(2, 64);
143
Tim Northoverff5e7e12017-06-30 20:27:36 +0000144 for (auto Ty : {p0, s1, s8, s16, s32, s64})
145 setAction({G_IMPLICIT_DEF, Ty}, Legal);
146
Amara Emerson1cd89ca2017-10-08 15:29:11 +0000147 for (auto Ty : {s16, s32, s64, p0})
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000148 setAction({G_PHI, Ty}, Legal);
149
Kristof Beylsaf9814a2017-11-07 10:34:34 +0000150 setLegalizeScalarToDifferentSizeStrategy(G_PHI, 0, widen_1_8);
Aditya Nandakumar892979e2017-08-25 04:57:27 +0000151
Daniel Sanders83e23d12017-09-19 14:25:15 +0000152 for (auto Ty : { s32, s64 })
153 setAction({G_BSWAP, Ty}, Legal);
154
Quentin Colombet24203cf2017-01-27 01:13:30 +0000155 for (unsigned BinOp : {G_ADD, G_SUB, G_MUL, G_AND, G_OR, G_XOR, G_SHL}) {
Tim Northoverfe880a82016-08-25 17:37:39 +0000156 // These operations naturally get the right answer when used on
157 // GPR32, even if the actual type is narrower.
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000158 for (auto Ty : {s32, s64, v2s32, v4s32, v2s64})
Quentin Colombete15e4602017-01-27 01:13:25 +0000159 setAction({BinOp, Ty}, Legal);
Ahmed Bougachacfb384d2017-01-23 21:10:05 +0000160
Kristof Beylsaf9814a2017-11-07 10:34:34 +0000161 if (BinOp != G_ADD)
162 setLegalizeScalarToDifferentSizeStrategy(BinOp, 0,
163 widen_1_8_16_narrowToLargest);
Tim Northover9656f142016-08-04 20:54:13 +0000164 }
165
Quentin Colombete15e4602017-01-27 01:13:25 +0000166 setAction({G_GEP, p0}, Legal);
167 setAction({G_GEP, 1, s64}, Legal);
Tim Northover22d82cf2016-09-15 11:02:19 +0000168
Kristof Beylsaf9814a2017-11-07 10:34:34 +0000169 setLegalizeScalarToDifferentSizeStrategy(G_GEP, 1, widen_1_8_16_32);
Tim Northover22d82cf2016-09-15 11:02:19 +0000170
Tim Northover398c5f52017-02-14 20:56:29 +0000171 setAction({G_PTR_MASK, p0}, Legal);
172
Quentin Colombet24203cf2017-01-27 01:13:30 +0000173 for (unsigned BinOp : {G_LSHR, G_ASHR, G_SDIV, G_UDIV}) {
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000174 for (auto Ty : {s32, s64})
Quentin Colombete15e4602017-01-27 01:13:25 +0000175 setAction({BinOp, Ty}, Legal);
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +0000176
Kristof Beylsaf9814a2017-11-07 10:34:34 +0000177 setLegalizeScalarToDifferentSizeStrategy(BinOp, 0, widen_1_8_16);
Tim Northover7a753d92016-08-26 17:46:06 +0000178 }
179
Quentin Colombet24203cf2017-01-27 01:13:30 +0000180 for (unsigned BinOp : {G_SREM, G_UREM})
Tim Northovercecee562016-08-26 17:46:13 +0000181 for (auto Ty : { s1, s8, s16, s32, s64 })
Quentin Colombete15e4602017-01-27 01:13:25 +0000182 setAction({BinOp, Ty}, Lower);
Tim Northovercecee562016-08-26 17:46:13 +0000183
Kristof Beylsaf9814a2017-11-07 10:34:34 +0000184 for (unsigned Op : {G_SMULO, G_UMULO}) {
185 setAction({Op, 0, s64}, Lower);
186 setAction({Op, 1, s1}, Legal);
187 }
Tim Northover0a9b2792017-02-08 21:22:15 +0000188
189 for (unsigned Op : {G_UADDE, G_USUBE, G_SADDO, G_SSUBO, G_SMULH, G_UMULH}) {
Tim Northover438c77c2016-08-25 17:37:32 +0000190 for (auto Ty : { s32, s64 })
Quentin Colombete15e4602017-01-27 01:13:25 +0000191 setAction({Op, Ty}, Legal);
Tim Northover438c77c2016-08-25 17:37:32 +0000192
Quentin Colombete15e4602017-01-27 01:13:25 +0000193 setAction({Op, 1, s1}, Legal);
Tim Northoverd8a6d7c2016-08-25 17:37:41 +0000194 }
195
Daniel Sanders40b66d62017-07-18 14:10:07 +0000196 for (unsigned BinOp : {G_FADD, G_FSUB, G_FMA, G_FMUL, G_FDIV})
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000197 for (auto Ty : {s32, s64})
Quentin Colombete15e4602017-01-27 01:13:25 +0000198 setAction({BinOp, Ty}, Legal);
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +0000199
Tim Northovere0418412017-02-08 23:23:39 +0000200 for (unsigned BinOp : {G_FREM, G_FPOW}) {
201 setAction({BinOp, s32}, Libcall);
202 setAction({BinOp, s64}, Libcall);
203 }
Tim Northoveredb3c8c2016-08-29 19:07:16 +0000204
Tim Northover3e6a7af2017-03-03 23:05:47 +0000205 for (auto Ty : {s32, s64, p0}) {
Tim Northover0e6afbd2017-02-06 21:56:47 +0000206 setAction({G_INSERT, Ty}, Legal);
207 setAction({G_INSERT, 1, Ty}, Legal);
208 }
Kristof Beylsaf9814a2017-11-07 10:34:34 +0000209 setLegalizeScalarToDifferentSizeStrategy(G_INSERT, 0,
210 widen_1_8_16_narrowToLargest);
Tim Northover0e6afbd2017-02-06 21:56:47 +0000211 for (auto Ty : {s1, s8, s16}) {
Tim Northover3e6a7af2017-03-03 23:05:47 +0000212 setAction({G_INSERT, 1, Ty}, Legal);
Tim Northover0e6afbd2017-02-06 21:56:47 +0000213 // FIXME: Can't widen the sources because that violates the constraints on
214 // G_INSERT (It seems entirely reasonable that inputs shouldn't overlap).
215 }
216
Tim Northoverc2d5e6d2017-06-26 20:34:13 +0000217 for (auto Ty : {s1, s8, s16, s32, s64, p0})
218 setAction({G_EXTRACT, Ty}, Legal);
219
220 for (auto Ty : {s32, s64})
221 setAction({G_EXTRACT, 1, Ty}, Legal);
222
Quentin Colombet24203cf2017-01-27 01:13:30 +0000223 for (unsigned MemOp : {G_LOAD, G_STORE}) {
Quentin Colombetd3126d52016-10-11 00:21:08 +0000224 for (auto Ty : {s8, s16, s32, s64, p0, v2s32})
Quentin Colombete15e4602017-01-27 01:13:25 +0000225 setAction({MemOp, Ty}, Legal);
Ahmed Bougachaad30db32016-08-02 15:10:28 +0000226
Kristof Beylsaf9814a2017-11-07 10:34:34 +0000227 setLegalizeScalarToDifferentSizeStrategy(MemOp, 0,
228 widen_1_narrow_128_ToLargest);
Tim Northovera01bece2016-08-23 19:30:42 +0000229
230 // And everything's fine in addrspace 0.
Quentin Colombete15e4602017-01-27 01:13:25 +0000231 setAction({MemOp, 1, p0}, Legal);
Tim Northover3c73e362016-08-23 18:20:09 +0000232 }
233
Tim Northoverb3a0be42016-08-23 21:01:20 +0000234 // Constants
Tim Northoverea904f92016-08-19 22:40:00 +0000235 for (auto Ty : {s32, s64}) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000236 setAction({TargetOpcode::G_CONSTANT, Ty}, Legal);
237 setAction({TargetOpcode::G_FCONSTANT, Ty}, Legal);
Tim Northoverea904f92016-08-19 22:40:00 +0000238 }
239
Quentin Colombete15e4602017-01-27 01:13:25 +0000240 setAction({G_CONSTANT, p0}, Legal);
Tim Northover7a1ec012016-08-25 17:37:35 +0000241
Kristof Beylsaf9814a2017-11-07 10:34:34 +0000242 setLegalizeScalarToDifferentSizeStrategy(G_CONSTANT, 0, widen_1_8_16);
243 setLegalizeScalarToDifferentSizeStrategy(G_FCONSTANT, 0, widen_16);
Tim Northover9656f142016-08-04 20:54:13 +0000244
Quentin Colombete15e4602017-01-27 01:13:25 +0000245 setAction({G_ICMP, 1, s32}, Legal);
246 setAction({G_ICMP, 1, s64}, Legal);
247 setAction({G_ICMP, 1, p0}, Legal);
Tim Northover6cd4b232016-08-23 21:01:26 +0000248
Kristof Beylsaf9814a2017-11-07 10:34:34 +0000249 setLegalizeScalarToDifferentSizeStrategy(G_ICMP, 0, widen_1_8_16);
250 setLegalizeScalarToDifferentSizeStrategy(G_FCMP, 0, widen_1_8_16);
251 setLegalizeScalarToDifferentSizeStrategy(G_ICMP, 1, widen_1_8_16);
Tim Northover6cd4b232016-08-23 21:01:26 +0000252
Aditya Nandakumar02c602e2017-07-31 17:00:16 +0000253 setAction({G_ICMP, s32}, Legal);
254 setAction({G_FCMP, s32}, Legal);
Quentin Colombete15e4602017-01-27 01:13:25 +0000255 setAction({G_FCMP, 1, s32}, Legal);
256 setAction({G_FCMP, 1, s64}, Legal);
Tim Northover30bd36e2016-08-26 17:46:19 +0000257
Tim Northover2c4a8382016-08-25 17:37:25 +0000258 // Extensions
259 for (auto Ty : { s1, s8, s16, s32, s64 }) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000260 setAction({G_ZEXT, Ty}, Legal);
261 setAction({G_SEXT, Ty}, Legal);
262 setAction({G_ANYEXT, Ty}, Legal);
Tim Northover2c4a8382016-08-25 17:37:25 +0000263 }
264
Ahmed Bougacha106dd032017-09-12 21:04:11 +0000265 // FP conversions
266 for (auto Ty : { s16, s32 }) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000267 setAction({G_FPTRUNC, Ty}, Legal);
Ahmed Bougacha106dd032017-09-12 21:04:11 +0000268 setAction({G_FPEXT, 1, Ty}, Legal);
269 }
Tim Northover438c77c2016-08-25 17:37:32 +0000270
Ahmed Bougacha106dd032017-09-12 21:04:11 +0000271 for (auto Ty : { s32, s64 }) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000272 setAction({G_FPTRUNC, 1, Ty}, Legal);
Ahmed Bougacha106dd032017-09-12 21:04:11 +0000273 setAction({G_FPEXT, Ty}, Legal);
274 }
Tim Northover438c77c2016-08-25 17:37:32 +0000275
Tim Northover5d0eaa42016-08-26 17:45:58 +0000276 // Conversions
Ahmed Bougachad2948232017-01-20 01:37:24 +0000277 for (auto Ty : { s32, s64 }) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000278 setAction({G_FPTOSI, 0, Ty}, Legal);
279 setAction({G_FPTOUI, 0, Ty}, Legal);
280 setAction({G_SITOFP, 1, Ty}, Legal);
281 setAction({G_UITOFP, 1, Ty}, Legal);
Tim Northover5d0eaa42016-08-26 17:45:58 +0000282 }
Kristof Beylsaf9814a2017-11-07 10:34:34 +0000283 setLegalizeScalarToDifferentSizeStrategy(G_FPTOSI, 0, widen_1_8_16);
284 setLegalizeScalarToDifferentSizeStrategy(G_FPTOUI, 0, widen_1_8_16);
285 setLegalizeScalarToDifferentSizeStrategy(G_SITOFP, 1, widen_1_8_16);
286 setLegalizeScalarToDifferentSizeStrategy(G_UITOFP, 1, widen_1_8_16);
Tim Northover5d0eaa42016-08-26 17:45:58 +0000287
288 for (auto Ty : { s32, s64 }) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000289 setAction({G_FPTOSI, 1, Ty}, Legal);
290 setAction({G_FPTOUI, 1, Ty}, Legal);
291 setAction({G_SITOFP, 0, Ty}, Legal);
292 setAction({G_UITOFP, 0, Ty}, Legal);
Tim Northover5d0eaa42016-08-26 17:45:58 +0000293 }
Tim Northover438c77c2016-08-25 17:37:32 +0000294
Tim Northoverb3a0be42016-08-23 21:01:20 +0000295 // Control-flow
Tim Northover6aacd272016-10-12 22:48:36 +0000296 for (auto Ty : {s1, s8, s16, s32})
Quentin Colombete15e4602017-01-27 01:13:25 +0000297 setAction({G_BRCOND, Ty}, Legal);
Kristof Beyls65a12c02017-01-30 09:13:18 +0000298 setAction({G_BRINDIRECT, p0}, Legal);
Ahmed Bougachaad30db32016-08-02 15:10:28 +0000299
Tim Northover1d18a992016-08-26 17:46:03 +0000300 // Select
Kristof Beylsaf9814a2017-11-07 10:34:34 +0000301 setLegalizeScalarToDifferentSizeStrategy(G_SELECT, 0, widen_1_8_16);
Tim Northover868332d2017-02-06 23:41:27 +0000302
303 for (auto Ty : {s32, s64, p0})
Quentin Colombete15e4602017-01-27 01:13:25 +0000304 setAction({G_SELECT, Ty}, Legal);
Tim Northover1d18a992016-08-26 17:46:03 +0000305
Quentin Colombete15e4602017-01-27 01:13:25 +0000306 setAction({G_SELECT, 1, s1}, Legal);
Tim Northover1d18a992016-08-26 17:46:03 +0000307
Tim Northoverb3a0be42016-08-23 21:01:20 +0000308 // Pointer-handling
Quentin Colombete15e4602017-01-27 01:13:25 +0000309 setAction({G_FRAME_INDEX, p0}, Legal);
310 setAction({G_GLOBAL_VALUE, p0}, Legal);
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000311
Tim Northover037af52c2016-10-31 18:31:09 +0000312 for (auto Ty : {s1, s8, s16, s32, s64})
Quentin Colombete15e4602017-01-27 01:13:25 +0000313 setAction({G_PTRTOINT, 0, Ty}, Legal);
Tim Northover037af52c2016-10-31 18:31:09 +0000314
Quentin Colombete15e4602017-01-27 01:13:25 +0000315 setAction({G_PTRTOINT, 1, p0}, Legal);
Tim Northovera01bece2016-08-23 19:30:42 +0000316
Quentin Colombete15e4602017-01-27 01:13:25 +0000317 setAction({G_INTTOPTR, 0, p0}, Legal);
318 setAction({G_INTTOPTR, 1, s64}, Legal);
Tim Northover456a3c02016-08-23 19:30:38 +0000319
Quentin Colombet404e4352016-10-12 03:57:43 +0000320 // Casts for 32 and 64-bit width type are just copies.
Quentin Colombet7c114d32017-10-16 22:28:27 +0000321 // Same for 128-bit width type, except they are on the FPR bank.
322 for (auto Ty : {s1, s8, s16, s32, s64, s128}) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000323 setAction({G_BITCAST, 0, Ty}, Legal);
324 setAction({G_BITCAST, 1, Ty}, Legal);
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000325 }
326
Quentin Colombetdb643d92016-10-13 00:12:01 +0000327 // For the sake of copying bits around, the type does not really
328 // matter as long as it fits a register.
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000329 for (int EltSize = 8; EltSize <= 64; EltSize *= 2) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000330 setAction({G_BITCAST, 0, LLT::vector(128/EltSize, EltSize)}, Legal);
331 setAction({G_BITCAST, 1, LLT::vector(128/EltSize, EltSize)}, Legal);
Quentin Colombetdb643d92016-10-13 00:12:01 +0000332 if (EltSize >= 64)
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000333 continue;
334
Quentin Colombete15e4602017-01-27 01:13:25 +0000335 setAction({G_BITCAST, 0, LLT::vector(64/EltSize, EltSize)}, Legal);
336 setAction({G_BITCAST, 1, LLT::vector(64/EltSize, EltSize)}, Legal);
Quentin Colombetdb643d92016-10-13 00:12:01 +0000337 if (EltSize >= 32)
338 continue;
339
Quentin Colombete15e4602017-01-27 01:13:25 +0000340 setAction({G_BITCAST, 0, LLT::vector(32/EltSize, EltSize)}, Legal);
341 setAction({G_BITCAST, 1, LLT::vector(32/EltSize, EltSize)}, Legal);
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000342 }
343
Tim Northovere9600d82017-02-08 17:57:27 +0000344 setAction({G_VASTART, p0}, Legal);
345
Tim Northover91366172017-02-15 23:22:50 +0000346 // va_list must be a pointer, but most sized types are pretty easy to handle
347 // as the destination.
348 setAction({G_VAARG, 1, p0}, Legal);
349
350 for (auto Ty : {s8, s16, s32, s64, p0})
351 setAction({G_VAARG, Ty}, Custom);
352
Daniel Sanders7fe7acc2017-11-28 20:21:15 +0000353 if (ST.hasLSE()) {
Daniel Sandersaef1dfc2017-11-30 20:11:42 +0000354 for (auto Ty : {s8, s16, s32, s64}) {
355 setAction({G_ATOMIC_CMPXCHG_WITH_SUCCESS, Ty}, Lower);
Daniel Sanders7fe7acc2017-11-28 20:21:15 +0000356 setAction({G_ATOMIC_CMPXCHG, Ty}, Legal);
Daniel Sandersaef1dfc2017-11-30 20:11:42 +0000357 }
Daniel Sanders7fe7acc2017-11-28 20:21:15 +0000358 setAction({G_ATOMIC_CMPXCHG, 1, p0}, Legal);
359
Simon Pilgrim14d3fd22017-11-28 22:32:43 +0000360 for (unsigned Op :
Daniel Sanders7fe7acc2017-11-28 20:21:15 +0000361 {G_ATOMICRMW_XCHG, G_ATOMICRMW_ADD, G_ATOMICRMW_SUB, G_ATOMICRMW_AND,
362 G_ATOMICRMW_OR, G_ATOMICRMW_XOR, G_ATOMICRMW_MIN, G_ATOMICRMW_MAX,
363 G_ATOMICRMW_UMIN, G_ATOMICRMW_UMAX}) {
364 for (auto Ty : {s8, s16, s32, s64}) {
365 setAction({Op, Ty}, Legal);
366 }
367 setAction({Op, 1, p0}, Legal);
368 }
369 }
370
Volkan Kelesa32ff002017-12-01 08:19:10 +0000371 // Merge/Unmerge
372 for (unsigned Op : {G_MERGE_VALUES, G_UNMERGE_VALUES})
373 for (int Sz : {8, 16, 32, 64, 128, 192, 256, 384, 512}) {
374 LLT ScalarTy = LLT::scalar(Sz);
375 setAction({Op, ScalarTy}, Legal);
376 setAction({Op, 1, ScalarTy}, Legal);
377 if (Sz < 32)
378 continue;
379 for (int EltSize = 8; EltSize <= 64; EltSize *= 2) {
380 if (EltSize >= Sz)
381 continue;
382 LLT VecTy = LLT::vector(Sz / EltSize, EltSize);
383 setAction({Op, VecTy}, Legal);
384 setAction({Op, 1, VecTy}, Legal);
385 }
386 }
387
Tim Northover33b07d62016-07-22 20:03:43 +0000388 computeTables();
389}
Tim Northover91366172017-02-15 23:22:50 +0000390
391bool AArch64LegalizerInfo::legalizeCustom(MachineInstr &MI,
392 MachineRegisterInfo &MRI,
393 MachineIRBuilder &MIRBuilder) const {
394 switch (MI.getOpcode()) {
395 default:
396 // No idea what to do.
397 return false;
398 case TargetOpcode::G_VAARG:
399 return legalizeVaArg(MI, MRI, MIRBuilder);
400 }
401
402 llvm_unreachable("expected switch to return");
403}
404
405bool AArch64LegalizerInfo::legalizeVaArg(MachineInstr &MI,
406 MachineRegisterInfo &MRI,
407 MachineIRBuilder &MIRBuilder) const {
408 MIRBuilder.setInstr(MI);
409 MachineFunction &MF = MIRBuilder.getMF();
410 unsigned Align = MI.getOperand(2).getImm();
411 unsigned Dst = MI.getOperand(0).getReg();
412 unsigned ListPtr = MI.getOperand(1).getReg();
413
414 LLT PtrTy = MRI.getType(ListPtr);
415 LLT IntPtrTy = LLT::scalar(PtrTy.getSizeInBits());
416
417 const unsigned PtrSize = PtrTy.getSizeInBits() / 8;
418 unsigned List = MRI.createGenericVirtualRegister(PtrTy);
419 MIRBuilder.buildLoad(
420 List, ListPtr,
421 *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOLoad,
422 PtrSize, /* Align = */ PtrSize));
423
424 unsigned DstPtr;
425 if (Align > PtrSize) {
426 // Realign the list to the actual required alignment.
Aditya Nandakumar17451212017-07-06 19:40:07 +0000427 auto AlignMinus1 = MIRBuilder.buildConstant(IntPtrTy, Align - 1);
Tim Northover91366172017-02-15 23:22:50 +0000428
429 unsigned ListTmp = MRI.createGenericVirtualRegister(PtrTy);
Aditya Nandakumar17451212017-07-06 19:40:07 +0000430 MIRBuilder.buildGEP(ListTmp, List, AlignMinus1->getOperand(0).getReg());
Tim Northover91366172017-02-15 23:22:50 +0000431
432 DstPtr = MRI.createGenericVirtualRegister(PtrTy);
433 MIRBuilder.buildPtrMask(DstPtr, ListTmp, Log2_64(Align));
434 } else
435 DstPtr = List;
436
437 uint64_t ValSize = MRI.getType(Dst).getSizeInBits() / 8;
438 MIRBuilder.buildLoad(
439 Dst, DstPtr,
440 *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOLoad,
441 ValSize, std::max(Align, PtrSize)));
442
443 unsigned SizeReg = MRI.createGenericVirtualRegister(IntPtrTy);
444 MIRBuilder.buildConstant(SizeReg, alignTo(ValSize, PtrSize));
445
446 unsigned NewList = MRI.createGenericVirtualRegister(PtrTy);
447 MIRBuilder.buildGEP(NewList, DstPtr, SizeReg);
448
449 MIRBuilder.buildStore(
450 NewList, ListPtr,
451 *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOStore,
452 PtrSize, /* Align = */ PtrSize));
453
454 MI.eraseFromParent();
455 return true;
456}