blob: c7df4440749c3d863d166bd089c27c883b8d32cd [file] [log] [blame]
Tom Stellardca166212017-01-30 21:56:46 +00001//===- AMDGPULegalizerInfo.cpp -----------------------------------*- C++ -*-==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// This file implements the targeting of the Machinelegalizer class for
11/// AMDGPU.
12/// \todo This should be generated by TableGen.
13//===----------------------------------------------------------------------===//
14
David Blaikie36a0f222018-03-23 23:58:31 +000015#include "AMDGPU.h"
Craig Topper2fa14362018-03-29 17:21:10 +000016#include "AMDGPULegalizerInfo.h"
Matt Arsenault85803362018-03-17 15:17:41 +000017#include "AMDGPUTargetMachine.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000018#include "llvm/CodeGen/TargetOpcodes.h"
Craig Topper2fa14362018-03-29 17:21:10 +000019#include "llvm/CodeGen/ValueTypes.h"
Tom Stellardca166212017-01-30 21:56:46 +000020#include "llvm/IR/DerivedTypes.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000021#include "llvm/IR/Type.h"
Tom Stellardca166212017-01-30 21:56:46 +000022#include "llvm/Support/Debug.h"
23
24using namespace llvm;
Daniel Sanders9ade5592018-01-29 17:37:29 +000025using namespace LegalizeActions;
Tom Stellardca166212017-01-30 21:56:46 +000026
Tom Stellard5bfbae52018-07-11 20:59:01 +000027AMDGPULegalizerInfo::AMDGPULegalizerInfo(const GCNSubtarget &ST,
Matt Arsenaultc3fe46b2018-03-08 16:24:16 +000028 const GCNTargetMachine &TM) {
Tom Stellardca166212017-01-30 21:56:46 +000029 using namespace TargetOpcode;
30
Matt Arsenault85803362018-03-17 15:17:41 +000031 auto GetAddrSpacePtr = [&TM](unsigned AS) {
32 return LLT::pointer(AS, TM.getPointerSizeInBits(AS));
33 };
34
35 const LLT S1 = LLT::scalar(1);
Tom Stellardca166212017-01-30 21:56:46 +000036 const LLT S32 = LLT::scalar(32);
37 const LLT S64 = LLT::scalar(64);
Tom Stellardeebbfc22018-06-30 04:09:44 +000038 const LLT S512 = LLT::scalar(512);
Matt Arsenault85803362018-03-17 15:17:41 +000039
Matt Arsenaultbee2ad72018-12-21 03:03:11 +000040 const LLT V2S16 = LLT::vector(2, 16);
41
42 const LLT V2S32 = LLT::vector(2, 32);
43 const LLT V3S32 = LLT::vector(3, 32);
44 const LLT V4S32 = LLT::vector(4, 32);
45 const LLT V5S32 = LLT::vector(5, 32);
46 const LLT V6S32 = LLT::vector(6, 32);
47 const LLT V7S32 = LLT::vector(7, 32);
48 const LLT V8S32 = LLT::vector(8, 32);
49 const LLT V9S32 = LLT::vector(9, 32);
50 const LLT V10S32 = LLT::vector(10, 32);
51 const LLT V11S32 = LLT::vector(11, 32);
52 const LLT V12S32 = LLT::vector(12, 32);
53 const LLT V13S32 = LLT::vector(13, 32);
54 const LLT V14S32 = LLT::vector(14, 32);
55 const LLT V15S32 = LLT::vector(15, 32);
56 const LLT V16S32 = LLT::vector(16, 32);
57
58 const LLT V2S64 = LLT::vector(2, 64);
59 const LLT V3S64 = LLT::vector(3, 64);
60 const LLT V4S64 = LLT::vector(4, 64);
61 const LLT V5S64 = LLT::vector(5, 64);
62 const LLT V6S64 = LLT::vector(6, 64);
63 const LLT V7S64 = LLT::vector(7, 64);
64 const LLT V8S64 = LLT::vector(8, 64);
65
66 std::initializer_list<LLT> AllS32Vectors =
67 {V2S32, V3S32, V4S32, V5S32, V6S32, V7S32, V8S32,
68 V9S32, V10S32, V11S32, V12S32, V13S32, V14S32, V15S32, V16S32};
69 std::initializer_list<LLT> AllS64Vectors =
70 {V2S64, V3S64, V4S64, V5S64, V6S64, V7S64, V8S64};
71
Matt Arsenault85803362018-03-17 15:17:41 +000072 const LLT GlobalPtr = GetAddrSpacePtr(AMDGPUAS::GLOBAL_ADDRESS);
73 const LLT ConstantPtr = GetAddrSpacePtr(AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenault685d1e82018-03-17 15:17:45 +000074 const LLT LocalPtr = GetAddrSpacePtr(AMDGPUAS::LOCAL_ADDRESS);
Matt Arsenault0da63502018-08-31 05:49:54 +000075 const LLT FlatPtr = GetAddrSpacePtr(AMDGPUAS::FLAT_ADDRESS);
76 const LLT PrivatePtr = GetAddrSpacePtr(AMDGPUAS::PRIVATE_ADDRESS);
Matt Arsenault85803362018-03-17 15:17:41 +000077
Matt Arsenault934e5342018-12-13 20:34:15 +000078 const LLT CodePtr = FlatPtr;
79
Matt Arsenault685d1e82018-03-17 15:17:45 +000080 const LLT AddrSpaces[] = {
81 GlobalPtr,
82 ConstantPtr,
83 LocalPtr,
84 FlatPtr,
85 PrivatePtr
86 };
Tom Stellardca166212017-01-30 21:56:46 +000087
Tom Stellardee6e6452017-06-12 20:54:56 +000088 setAction({G_ADD, S32}, Legal);
Tom Stellard26fac0f2018-06-22 02:54:57 +000089 setAction({G_ASHR, S32}, Legal);
Matt Arsenaultfed0a452018-03-19 14:07:23 +000090 setAction({G_SUB, S32}, Legal);
Matt Arsenaultdc14ec02018-03-01 19:22:05 +000091 setAction({G_MUL, S32}, Legal);
Matt Arsenault43398832018-12-20 01:35:49 +000092
93 // FIXME: 64-bit ones only legal for scalar
94 getActionDefinitionsBuilder({G_AND, G_OR, G_XOR})
95 .legalFor({S32, S1, S64, V2S32});
Tom Stellardee6e6452017-06-12 20:54:56 +000096
Tom Stellardff63ee02017-06-19 13:15:45 +000097 setAction({G_BITCAST, V2S16}, Legal);
98 setAction({G_BITCAST, 1, S32}, Legal);
99
100 setAction({G_BITCAST, S32}, Legal);
101 setAction({G_BITCAST, 1, V2S16}, Legal);
102
Matt Arsenaultabdc4f22018-03-17 15:17:48 +0000103 getActionDefinitionsBuilder(G_FCONSTANT)
104 .legalFor({S32, S64});
Tom Stellardeebbfc22018-06-30 04:09:44 +0000105
106 // G_IMPLICIT_DEF is a no-op so we can make it legal for any value type that
107 // can fit in a register.
108 // FIXME: We need to legalize several more operations before we can add
109 // a test case for size > 512.
Matt Arsenaultb3feccd2018-06-25 15:42:12 +0000110 getActionDefinitionsBuilder(G_IMPLICIT_DEF)
Tom Stellardeebbfc22018-06-30 04:09:44 +0000111 .legalIf([=](const LegalityQuery &Query) {
112 return Query.Types[0].getSizeInBits() <= 512;
113 })
114 .clampScalar(0, S1, S512);
Matt Arsenaultb3feccd2018-06-25 15:42:12 +0000115
116 getActionDefinitionsBuilder(G_CONSTANT)
Matt Arsenaultabdc4f22018-03-17 15:17:48 +0000117 .legalFor({S1, S32, S64});
118
Tom Stellarde0424122017-06-03 01:13:33 +0000119 // FIXME: i1 operands to intrinsics should always be legal, but other i1
120 // values may not be legal. We need to figure out how to distinguish
121 // between these two scenarios.
122 setAction({G_CONSTANT, S1}, Legal);
Matt Arsenault06cbb272018-03-01 19:16:52 +0000123
Matt Arsenaultc94e26c2018-12-18 09:46:13 +0000124 setAction({G_FRAME_INDEX, PrivatePtr}, Legal);
125
Matt Arsenault577b9fc2018-12-13 08:27:48 +0000126 getActionDefinitionsBuilder(
Matt Arsenaultc0ea2212018-12-18 09:39:56 +0000127 { G_FADD, G_FMUL, G_FNEG, G_FABS, G_FMA})
Matt Arsenault577b9fc2018-12-13 08:27:48 +0000128 .legalFor({S32, S64});
Tom Stellardd0c6cf22017-10-27 23:57:41 +0000129
Matt Arsenaultdff33c32018-12-20 00:37:02 +0000130 getActionDefinitionsBuilder(G_FPTRUNC)
131 .legalFor({{S32, S64}});
132
Matt Arsenaulte01e7c82018-12-18 09:19:03 +0000133 // Use actual fsub instruction
134 setAction({G_FSUB, S32}, Legal);
135
136 // Must use fadd + fneg
137 setAction({G_FSUB, S64}, Lower);
138
Matt Arsenault8e80a5f2018-03-01 19:09:16 +0000139 setAction({G_FCMP, S1}, Legal);
140 setAction({G_FCMP, 1, S32}, Legal);
141 setAction({G_FCMP, 1, S64}, Legal);
142
Matt Arsenault0529a8e2018-03-01 20:56:21 +0000143 setAction({G_ZEXT, S64}, Legal);
144 setAction({G_ZEXT, 1, S32}, Legal);
145
Matt Arsenaultf38f4832018-12-13 08:23:51 +0000146 setAction({G_SEXT, S64}, Legal);
147 setAction({G_SEXT, 1, S32}, Legal);
148
149 setAction({G_ANYEXT, S64}, Legal);
150 setAction({G_ANYEXT, 1, S32}, Legal);
151
Matt Arsenaultdd022ce2018-03-01 19:04:25 +0000152 setAction({G_FPTOSI, S32}, Legal);
153 setAction({G_FPTOSI, 1, S32}, Legal);
154
Tom Stellard9a653572018-06-22 02:34:29 +0000155 setAction({G_SITOFP, S32}, Legal);
156 setAction({G_SITOFP, 1, S32}, Legal);
157
Matt Arsenaultdff33c32018-12-20 00:37:02 +0000158 setAction({G_UITOFP, S32}, Legal);
159 setAction({G_UITOFP, 1, S32}, Legal);
160
Tom Stellard33445762018-02-07 04:47:59 +0000161 setAction({G_FPTOUI, S32}, Legal);
162 setAction({G_FPTOUI, 1, S32}, Legal);
163
Matt Arsenault685d1e82018-03-17 15:17:45 +0000164 for (LLT PtrTy : AddrSpaces) {
165 LLT IdxTy = LLT::scalar(PtrTy.getSizeInBits());
166 setAction({G_GEP, PtrTy}, Legal);
167 setAction({G_GEP, 1, IdxTy}, Legal);
168 }
Tom Stellardca166212017-01-30 21:56:46 +0000169
Matt Arsenault934e5342018-12-13 20:34:15 +0000170 setAction({G_BLOCK_ADDR, CodePtr}, Legal);
171
Tom Stellard8cd60a52017-06-06 14:16:50 +0000172 setAction({G_ICMP, S1}, Legal);
173 setAction({G_ICMP, 1, S32}, Legal);
174
Matt Arsenaultf38f4832018-12-13 08:23:51 +0000175 setAction({G_CTLZ, S32}, Legal);
176 setAction({G_CTLZ_ZERO_UNDEF, S32}, Legal);
177 setAction({G_CTTZ, S32}, Legal);
178 setAction({G_CTTZ_ZERO_UNDEF, S32}, Legal);
179 setAction({G_BSWAP, S32}, Legal);
180 setAction({G_CTPOP, S32}, Legal);
181
Tom Stellard7c650782018-10-05 04:34:09 +0000182 getActionDefinitionsBuilder(G_INTTOPTR)
183 .legalIf([](const LegalityQuery &Query) {
184 return true;
185 });
Matt Arsenault85803362018-03-17 15:17:41 +0000186
Matt Arsenaultf38f4832018-12-13 08:23:51 +0000187 getActionDefinitionsBuilder(G_PTRTOINT)
188 .legalIf([](const LegalityQuery &Query) {
189 return true;
190 });
191
Matt Arsenault85803362018-03-17 15:17:41 +0000192 getActionDefinitionsBuilder({G_LOAD, G_STORE})
193 .legalIf([=, &ST](const LegalityQuery &Query) {
194 const LLT &Ty0 = Query.Types[0];
195
196 // TODO: Decompose private loads into 4-byte components.
197 // TODO: Illegal flat loads on SI
198 switch (Ty0.getSizeInBits()) {
199 case 32:
200 case 64:
201 case 128:
202 return true;
203
204 case 96:
205 // XXX hasLoadX3
206 return (ST.getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS);
207
208 case 256:
209 case 512:
210 // TODO: constant loads
211 default:
212 return false;
213 }
214 });
215
216
Matt Arsenault36d40922018-12-20 00:33:49 +0000217 auto &Atomics = getActionDefinitionsBuilder(
218 {G_ATOMICRMW_XCHG, G_ATOMICRMW_ADD, G_ATOMICRMW_SUB,
219 G_ATOMICRMW_AND, G_ATOMICRMW_OR, G_ATOMICRMW_XOR,
220 G_ATOMICRMW_MAX, G_ATOMICRMW_MIN, G_ATOMICRMW_UMAX,
221 G_ATOMICRMW_UMIN, G_ATOMIC_CMPXCHG})
222 .legalFor({{S32, GlobalPtr}, {S32, LocalPtr},
223 {S64, GlobalPtr}, {S64, LocalPtr}});
224 if (ST.hasFlatAddressSpace()) {
225 Atomics.legalFor({{S32, FlatPtr}, {S64, FlatPtr}});
226 }
Tom Stellardca166212017-01-30 21:56:46 +0000227
Tom Stellard2860a422017-06-07 13:54:51 +0000228 setAction({G_SELECT, S32}, Legal);
229 setAction({G_SELECT, 1, S1}, Legal);
230
Tom Stellardeb8f1e22017-06-26 15:56:52 +0000231 setAction({G_SHL, S32}, Legal);
232
Tom Stellardca166212017-01-30 21:56:46 +0000233
234 // FIXME: When RegBankSelect inserts copies, it will only create new
235 // registers with scalar types. This means we can end up with
236 // G_LOAD/G_STORE/G_GEP instruction with scalar types for their pointer
237 // operands. In assert builds, the instruction selector will assert
238 // if it sees a generic instruction which isn't legal, so we need to
239 // tell it that scalar types are legal for pointer operands
240 setAction({G_GEP, S64}, Legal);
Tom Stellardca166212017-01-30 21:56:46 +0000241
Matt Arsenault7b9ed892018-03-12 13:35:53 +0000242 for (unsigned Op : {G_EXTRACT_VECTOR_ELT, G_INSERT_VECTOR_ELT}) {
243 getActionDefinitionsBuilder(Op)
244 .legalIf([=](const LegalityQuery &Query) {
245 const LLT &VecTy = Query.Types[1];
246 const LLT &IdxTy = Query.Types[2];
247 return VecTy.getSizeInBits() % 32 == 0 &&
248 VecTy.getSizeInBits() <= 512 &&
249 IdxTy.getSizeInBits() == 32;
250 });
251 }
252
Matt Arsenault71272e62018-03-05 16:25:15 +0000253 // FIXME: Doesn't handle extract of illegal sizes.
Tom Stellardb7f19e62018-07-24 02:19:20 +0000254 getActionDefinitionsBuilder({G_EXTRACT, G_INSERT})
Matt Arsenault71272e62018-03-05 16:25:15 +0000255 .legalIf([=](const LegalityQuery &Query) {
256 const LLT &Ty0 = Query.Types[0];
257 const LLT &Ty1 = Query.Types[1];
258 return (Ty0.getSizeInBits() % 32 == 0) &&
259 (Ty1.getSizeInBits() % 32 == 0);
260 });
261
Amara Emerson5ec14602018-12-10 18:44:58 +0000262 getActionDefinitionsBuilder(G_BUILD_VECTOR)
Matt Arsenaultbee2ad72018-12-21 03:03:11 +0000263 .legalForCartesianProduct(AllS32Vectors, {S32})
264 .legalForCartesianProduct(AllS64Vectors, {S64})
265 .clampNumElements(0, V16S32, V16S32)
266 .clampNumElements(0, V2S64, V8S64)
267 .minScalarSameAs(1, 0);
268
Matt Arsenault503afda2018-03-12 13:35:43 +0000269 // Merge/Unmerge
270 for (unsigned Op : {G_MERGE_VALUES, G_UNMERGE_VALUES}) {
271 unsigned BigTyIdx = Op == G_MERGE_VALUES ? 0 : 1;
272 unsigned LitTyIdx = Op == G_MERGE_VALUES ? 1 : 0;
273
274 getActionDefinitionsBuilder(Op)
275 .legalIf([=](const LegalityQuery &Query) {
276 const LLT &BigTy = Query.Types[BigTyIdx];
277 const LLT &LitTy = Query.Types[LitTyIdx];
278 return BigTy.getSizeInBits() % 32 == 0 &&
279 LitTy.getSizeInBits() % 32 == 0 &&
280 BigTy.getSizeInBits() <= 512;
281 })
282 // Any vectors left are the wrong size. Scalarize them.
283 .fewerElementsIf([](const LegalityQuery &Query) { return true; },
284 [](const LegalityQuery &Query) {
285 return std::make_pair(
286 0, Query.Types[0].getElementType());
287 })
288 .fewerElementsIf([](const LegalityQuery &Query) { return true; },
289 [](const LegalityQuery &Query) {
290 return std::make_pair(
291 1, Query.Types[1].getElementType());
292 });
293
294 }
295
Tom Stellardca166212017-01-30 21:56:46 +0000296 computeTables();
Roman Tereshin76c29c62018-05-31 16:16:48 +0000297 verify(*ST.getInstrInfo());
Tom Stellardca166212017-01-30 21:56:46 +0000298}