blob: cac53d9cc8756e1dfad95bb940a01fae83730d91 [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 Stellardff63ee02017-06-19 13:15:45 +000036 const LLT V2S16 = LLT::vector(2, 16);
Matt Arsenault85803362018-03-17 15:17:41 +000037
Tom Stellardca166212017-01-30 21:56:46 +000038 const LLT S32 = LLT::scalar(32);
39 const LLT S64 = LLT::scalar(64);
Tom Stellardeebbfc22018-06-30 04:09:44 +000040 const LLT S512 = LLT::scalar(512);
Matt Arsenault85803362018-03-17 15:17:41 +000041
42 const LLT GlobalPtr = GetAddrSpacePtr(AMDGPUAS::GLOBAL_ADDRESS);
43 const LLT ConstantPtr = GetAddrSpacePtr(AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenault685d1e82018-03-17 15:17:45 +000044 const LLT LocalPtr = GetAddrSpacePtr(AMDGPUAS::LOCAL_ADDRESS);
Matt Arsenault0da63502018-08-31 05:49:54 +000045 const LLT FlatPtr = GetAddrSpacePtr(AMDGPUAS::FLAT_ADDRESS);
46 const LLT PrivatePtr = GetAddrSpacePtr(AMDGPUAS::PRIVATE_ADDRESS);
Matt Arsenault85803362018-03-17 15:17:41 +000047
Matt Arsenault934e5342018-12-13 20:34:15 +000048 const LLT CodePtr = FlatPtr;
49
Matt Arsenault685d1e82018-03-17 15:17:45 +000050 const LLT AddrSpaces[] = {
51 GlobalPtr,
52 ConstantPtr,
53 LocalPtr,
54 FlatPtr,
55 PrivatePtr
56 };
Tom Stellardca166212017-01-30 21:56:46 +000057
Tom Stellardee6e6452017-06-12 20:54:56 +000058 setAction({G_ADD, S32}, Legal);
Tom Stellard26fac0f2018-06-22 02:54:57 +000059 setAction({G_ASHR, S32}, Legal);
Matt Arsenaultfed0a452018-03-19 14:07:23 +000060 setAction({G_SUB, S32}, Legal);
Matt Arsenaultdc14ec02018-03-01 19:22:05 +000061 setAction({G_MUL, S32}, Legal);
Tom Stellardaf552dc2017-06-23 15:17:17 +000062 setAction({G_AND, S32}, Legal);
Matt Arsenault3f6a2042018-03-01 19:09:21 +000063 setAction({G_OR, S32}, Legal);
64 setAction({G_XOR, S32}, Legal);
Tom Stellardee6e6452017-06-12 20:54:56 +000065
Tom Stellardff63ee02017-06-19 13:15:45 +000066 setAction({G_BITCAST, V2S16}, Legal);
67 setAction({G_BITCAST, 1, S32}, Legal);
68
69 setAction({G_BITCAST, S32}, Legal);
70 setAction({G_BITCAST, 1, V2S16}, Legal);
71
Matt Arsenaultabdc4f22018-03-17 15:17:48 +000072 getActionDefinitionsBuilder(G_FCONSTANT)
73 .legalFor({S32, S64});
Tom Stellardeebbfc22018-06-30 04:09:44 +000074
75 // G_IMPLICIT_DEF is a no-op so we can make it legal for any value type that
76 // can fit in a register.
77 // FIXME: We need to legalize several more operations before we can add
78 // a test case for size > 512.
Matt Arsenaultb3feccd2018-06-25 15:42:12 +000079 getActionDefinitionsBuilder(G_IMPLICIT_DEF)
Tom Stellardeebbfc22018-06-30 04:09:44 +000080 .legalIf([=](const LegalityQuery &Query) {
81 return Query.Types[0].getSizeInBits() <= 512;
82 })
83 .clampScalar(0, S1, S512);
Matt Arsenaultb3feccd2018-06-25 15:42:12 +000084
85 getActionDefinitionsBuilder(G_CONSTANT)
Matt Arsenaultabdc4f22018-03-17 15:17:48 +000086 .legalFor({S1, S32, S64});
87
Tom Stellarde0424122017-06-03 01:13:33 +000088 // FIXME: i1 operands to intrinsics should always be legal, but other i1
89 // values may not be legal. We need to figure out how to distinguish
90 // between these two scenarios.
91 setAction({G_CONSTANT, S1}, Legal);
Matt Arsenault06cbb272018-03-01 19:16:52 +000092
Matt Arsenaultc94e26c2018-12-18 09:46:13 +000093 setAction({G_FRAME_INDEX, PrivatePtr}, Legal);
94
Matt Arsenault577b9fc2018-12-13 08:27:48 +000095 getActionDefinitionsBuilder(
Matt Arsenaultc0ea2212018-12-18 09:39:56 +000096 { G_FADD, G_FMUL, G_FNEG, G_FABS, G_FMA})
Matt Arsenault577b9fc2018-12-13 08:27:48 +000097 .legalFor({S32, S64});
Tom Stellardd0c6cf22017-10-27 23:57:41 +000098
Matt Arsenaultdff33c32018-12-20 00:37:02 +000099 getActionDefinitionsBuilder(G_FPTRUNC)
100 .legalFor({{S32, S64}});
101
Matt Arsenaulte01e7c82018-12-18 09:19:03 +0000102 // Use actual fsub instruction
103 setAction({G_FSUB, S32}, Legal);
104
105 // Must use fadd + fneg
106 setAction({G_FSUB, S64}, Lower);
107
Matt Arsenault8e80a5f2018-03-01 19:09:16 +0000108 setAction({G_FCMP, S1}, Legal);
109 setAction({G_FCMP, 1, S32}, Legal);
110 setAction({G_FCMP, 1, S64}, Legal);
111
Matt Arsenault0529a8e2018-03-01 20:56:21 +0000112 setAction({G_ZEXT, S64}, Legal);
113 setAction({G_ZEXT, 1, S32}, Legal);
114
Matt Arsenaultf38f4832018-12-13 08:23:51 +0000115 setAction({G_SEXT, S64}, Legal);
116 setAction({G_SEXT, 1, S32}, Legal);
117
118 setAction({G_ANYEXT, S64}, Legal);
119 setAction({G_ANYEXT, 1, S32}, Legal);
120
Matt Arsenaultdd022ce2018-03-01 19:04:25 +0000121 setAction({G_FPTOSI, S32}, Legal);
122 setAction({G_FPTOSI, 1, S32}, Legal);
123
Tom Stellard9a653572018-06-22 02:34:29 +0000124 setAction({G_SITOFP, S32}, Legal);
125 setAction({G_SITOFP, 1, S32}, Legal);
126
Matt Arsenaultdff33c32018-12-20 00:37:02 +0000127 setAction({G_UITOFP, S32}, Legal);
128 setAction({G_UITOFP, 1, S32}, Legal);
129
Tom Stellard33445762018-02-07 04:47:59 +0000130 setAction({G_FPTOUI, S32}, Legal);
131 setAction({G_FPTOUI, 1, S32}, Legal);
132
Matt Arsenault685d1e82018-03-17 15:17:45 +0000133 for (LLT PtrTy : AddrSpaces) {
134 LLT IdxTy = LLT::scalar(PtrTy.getSizeInBits());
135 setAction({G_GEP, PtrTy}, Legal);
136 setAction({G_GEP, 1, IdxTy}, Legal);
137 }
Tom Stellardca166212017-01-30 21:56:46 +0000138
Matt Arsenault934e5342018-12-13 20:34:15 +0000139 setAction({G_BLOCK_ADDR, CodePtr}, Legal);
140
Tom Stellard8cd60a52017-06-06 14:16:50 +0000141 setAction({G_ICMP, S1}, Legal);
142 setAction({G_ICMP, 1, S32}, Legal);
143
Matt Arsenaultf38f4832018-12-13 08:23:51 +0000144 setAction({G_CTLZ, S32}, Legal);
145 setAction({G_CTLZ_ZERO_UNDEF, S32}, Legal);
146 setAction({G_CTTZ, S32}, Legal);
147 setAction({G_CTTZ_ZERO_UNDEF, S32}, Legal);
148 setAction({G_BSWAP, S32}, Legal);
149 setAction({G_CTPOP, S32}, Legal);
150
Tom Stellard7c650782018-10-05 04:34:09 +0000151 getActionDefinitionsBuilder(G_INTTOPTR)
152 .legalIf([](const LegalityQuery &Query) {
153 return true;
154 });
Matt Arsenault85803362018-03-17 15:17:41 +0000155
Matt Arsenaultf38f4832018-12-13 08:23:51 +0000156 getActionDefinitionsBuilder(G_PTRTOINT)
157 .legalIf([](const LegalityQuery &Query) {
158 return true;
159 });
160
Matt Arsenault85803362018-03-17 15:17:41 +0000161 getActionDefinitionsBuilder({G_LOAD, G_STORE})
162 .legalIf([=, &ST](const LegalityQuery &Query) {
163 const LLT &Ty0 = Query.Types[0];
164
165 // TODO: Decompose private loads into 4-byte components.
166 // TODO: Illegal flat loads on SI
167 switch (Ty0.getSizeInBits()) {
168 case 32:
169 case 64:
170 case 128:
171 return true;
172
173 case 96:
174 // XXX hasLoadX3
175 return (ST.getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS);
176
177 case 256:
178 case 512:
179 // TODO: constant loads
180 default:
181 return false;
182 }
183 });
184
185
Matt Arsenault36d40922018-12-20 00:33:49 +0000186 auto &Atomics = getActionDefinitionsBuilder(
187 {G_ATOMICRMW_XCHG, G_ATOMICRMW_ADD, G_ATOMICRMW_SUB,
188 G_ATOMICRMW_AND, G_ATOMICRMW_OR, G_ATOMICRMW_XOR,
189 G_ATOMICRMW_MAX, G_ATOMICRMW_MIN, G_ATOMICRMW_UMAX,
190 G_ATOMICRMW_UMIN, G_ATOMIC_CMPXCHG})
191 .legalFor({{S32, GlobalPtr}, {S32, LocalPtr},
192 {S64, GlobalPtr}, {S64, LocalPtr}});
193 if (ST.hasFlatAddressSpace()) {
194 Atomics.legalFor({{S32, FlatPtr}, {S64, FlatPtr}});
195 }
Tom Stellardca166212017-01-30 21:56:46 +0000196
Tom Stellard2860a422017-06-07 13:54:51 +0000197 setAction({G_SELECT, S32}, Legal);
198 setAction({G_SELECT, 1, S1}, Legal);
199
Tom Stellardeb8f1e22017-06-26 15:56:52 +0000200 setAction({G_SHL, S32}, Legal);
201
Tom Stellardca166212017-01-30 21:56:46 +0000202
203 // FIXME: When RegBankSelect inserts copies, it will only create new
204 // registers with scalar types. This means we can end up with
205 // G_LOAD/G_STORE/G_GEP instruction with scalar types for their pointer
206 // operands. In assert builds, the instruction selector will assert
207 // if it sees a generic instruction which isn't legal, so we need to
208 // tell it that scalar types are legal for pointer operands
209 setAction({G_GEP, S64}, Legal);
Tom Stellardca166212017-01-30 21:56:46 +0000210
Matt Arsenault7b9ed892018-03-12 13:35:53 +0000211 for (unsigned Op : {G_EXTRACT_VECTOR_ELT, G_INSERT_VECTOR_ELT}) {
212 getActionDefinitionsBuilder(Op)
213 .legalIf([=](const LegalityQuery &Query) {
214 const LLT &VecTy = Query.Types[1];
215 const LLT &IdxTy = Query.Types[2];
216 return VecTy.getSizeInBits() % 32 == 0 &&
217 VecTy.getSizeInBits() <= 512 &&
218 IdxTy.getSizeInBits() == 32;
219 });
220 }
221
Matt Arsenault71272e62018-03-05 16:25:15 +0000222 // FIXME: Doesn't handle extract of illegal sizes.
Tom Stellardb7f19e62018-07-24 02:19:20 +0000223 getActionDefinitionsBuilder({G_EXTRACT, G_INSERT})
Matt Arsenault71272e62018-03-05 16:25:15 +0000224 .legalIf([=](const LegalityQuery &Query) {
225 const LLT &Ty0 = Query.Types[0];
226 const LLT &Ty1 = Query.Types[1];
227 return (Ty0.getSizeInBits() % 32 == 0) &&
228 (Ty1.getSizeInBits() % 32 == 0);
229 });
230
Amara Emerson5ec14602018-12-10 18:44:58 +0000231 getActionDefinitionsBuilder(G_BUILD_VECTOR)
232 .legalIf([=](const LegalityQuery &Query) {
233 const LLT &VecTy = Query.Types[0];
234 const LLT &ScalarTy = Query.Types[1];
235 return VecTy.getSizeInBits() % 32 == 0 &&
236 ScalarTy.getSizeInBits() % 32 == 0 &&
237 VecTy.getSizeInBits() <= 512;
238 });
Matt Arsenault503afda2018-03-12 13:35:43 +0000239 // Merge/Unmerge
240 for (unsigned Op : {G_MERGE_VALUES, G_UNMERGE_VALUES}) {
241 unsigned BigTyIdx = Op == G_MERGE_VALUES ? 0 : 1;
242 unsigned LitTyIdx = Op == G_MERGE_VALUES ? 1 : 0;
243
244 getActionDefinitionsBuilder(Op)
245 .legalIf([=](const LegalityQuery &Query) {
246 const LLT &BigTy = Query.Types[BigTyIdx];
247 const LLT &LitTy = Query.Types[LitTyIdx];
248 return BigTy.getSizeInBits() % 32 == 0 &&
249 LitTy.getSizeInBits() % 32 == 0 &&
250 BigTy.getSizeInBits() <= 512;
251 })
252 // Any vectors left are the wrong size. Scalarize them.
253 .fewerElementsIf([](const LegalityQuery &Query) { return true; },
254 [](const LegalityQuery &Query) {
255 return std::make_pair(
256 0, Query.Types[0].getElementType());
257 })
258 .fewerElementsIf([](const LegalityQuery &Query) { return true; },
259 [](const LegalityQuery &Query) {
260 return std::make_pair(
261 1, Query.Types[1].getElementType());
262 });
263
264 }
265
Tom Stellardca166212017-01-30 21:56:46 +0000266 computeTables();
Roman Tereshin76c29c62018-05-31 16:16:48 +0000267 verify(*ST.getInstrInfo());
Tom Stellardca166212017-01-30 21:56:46 +0000268}