blob: 1199a833ee5042e56a78bfe001a21e44ae91027f [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
Yaxun Liu0124b542018-02-13 18:00:25 +000015#include "AMDGPU.h"
Tom Stellardca166212017-01-30 21:56:46 +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"
Tom Stellardca166212017-01-30 21:56:46 +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
Matt Arsenaultc3fe46b2018-03-08 16:24:16 +000027AMDGPULegalizerInfo::AMDGPULegalizerInfo(const SISubtarget &ST,
28 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
Matt Arsenault685d1e82018-03-17 15:17:45 +000035 auto AMDGPUAS = ST.getAMDGPUAS();
36
Matt Arsenault85803362018-03-17 15:17:41 +000037 const LLT S1 = LLT::scalar(1);
Tom Stellardff63ee02017-06-19 13:15:45 +000038 const LLT V2S16 = LLT::vector(2, 16);
Matt Arsenault85803362018-03-17 15:17:41 +000039
Tom Stellardca166212017-01-30 21:56:46 +000040 const LLT S32 = LLT::scalar(32);
41 const LLT S64 = LLT::scalar(64);
Matt Arsenault85803362018-03-17 15:17:41 +000042
43 const LLT GlobalPtr = GetAddrSpacePtr(AMDGPUAS::GLOBAL_ADDRESS);
44 const LLT ConstantPtr = GetAddrSpacePtr(AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenault685d1e82018-03-17 15:17:45 +000045 const LLT LocalPtr = GetAddrSpacePtr(AMDGPUAS::LOCAL_ADDRESS);
46 const LLT FlatPtr = GetAddrSpacePtr(AMDGPUAS.FLAT_ADDRESS);
47 const LLT PrivatePtr = GetAddrSpacePtr(AMDGPUAS.PRIVATE_ADDRESS);
Matt Arsenault85803362018-03-17 15:17:41 +000048
Matt Arsenault685d1e82018-03-17 15:17:45 +000049 const LLT AddrSpaces[] = {
50 GlobalPtr,
51 ConstantPtr,
52 LocalPtr,
53 FlatPtr,
54 PrivatePtr
55 };
Tom Stellardca166212017-01-30 21:56:46 +000056
Tom Stellardee6e6452017-06-12 20:54:56 +000057 setAction({G_ADD, S32}, Legal);
Matt Arsenaultdc14ec02018-03-01 19:22:05 +000058 setAction({G_MUL, S32}, Legal);
Tom Stellardaf552dc2017-06-23 15:17:17 +000059 setAction({G_AND, S32}, Legal);
Matt Arsenault3f6a2042018-03-01 19:09:21 +000060 setAction({G_OR, S32}, Legal);
61 setAction({G_XOR, S32}, Legal);
Tom Stellardee6e6452017-06-12 20:54:56 +000062
Tom Stellardff63ee02017-06-19 13:15:45 +000063 setAction({G_BITCAST, V2S16}, Legal);
64 setAction({G_BITCAST, 1, S32}, Legal);
65
66 setAction({G_BITCAST, S32}, Legal);
67 setAction({G_BITCAST, 1, V2S16}, Legal);
68
Matt Arsenaultabdc4f22018-03-17 15:17:48 +000069 getActionDefinitionsBuilder(G_FCONSTANT)
70 .legalFor({S32, S64});
71 getActionDefinitionsBuilder({G_IMPLICIT_DEF, G_CONSTANT})
72 .legalFor({S1, S32, S64});
73
Tom Stellarde0424122017-06-03 01:13:33 +000074 // FIXME: i1 operands to intrinsics should always be legal, but other i1
75 // values may not be legal. We need to figure out how to distinguish
76 // between these two scenarios.
77 setAction({G_CONSTANT, S1}, Legal);
Matt Arsenault06cbb272018-03-01 19:16:52 +000078
Tom Stellardd0c6cf22017-10-27 23:57:41 +000079 setAction({G_FADD, S32}, Legal);
80
Matt Arsenault8e80a5f2018-03-01 19:09:16 +000081 setAction({G_FCMP, S1}, Legal);
82 setAction({G_FCMP, 1, S32}, Legal);
83 setAction({G_FCMP, 1, S64}, Legal);
84
Tom Stellard3337d742017-08-02 22:56:30 +000085 setAction({G_FMUL, S32}, Legal);
86
Matt Arsenault0529a8e2018-03-01 20:56:21 +000087 setAction({G_ZEXT, S64}, Legal);
88 setAction({G_ZEXT, 1, S32}, Legal);
89
Matt Arsenaultdd022ce2018-03-01 19:04:25 +000090 setAction({G_FPTOSI, S32}, Legal);
91 setAction({G_FPTOSI, 1, S32}, Legal);
92
Tom Stellard33445762018-02-07 04:47:59 +000093 setAction({G_FPTOUI, S32}, Legal);
94 setAction({G_FPTOUI, 1, S32}, Legal);
95
Matt Arsenault685d1e82018-03-17 15:17:45 +000096 for (LLT PtrTy : AddrSpaces) {
97 LLT IdxTy = LLT::scalar(PtrTy.getSizeInBits());
98 setAction({G_GEP, PtrTy}, Legal);
99 setAction({G_GEP, 1, IdxTy}, Legal);
100 }
Tom Stellardca166212017-01-30 21:56:46 +0000101
Tom Stellard8cd60a52017-06-06 14:16:50 +0000102 setAction({G_ICMP, S1}, Legal);
103 setAction({G_ICMP, 1, S32}, Legal);
104
Matt Arsenault85803362018-03-17 15:17:41 +0000105
106 getActionDefinitionsBuilder({G_LOAD, G_STORE})
107 .legalIf([=, &ST](const LegalityQuery &Query) {
108 const LLT &Ty0 = Query.Types[0];
109
110 // TODO: Decompose private loads into 4-byte components.
111 // TODO: Illegal flat loads on SI
112 switch (Ty0.getSizeInBits()) {
113 case 32:
114 case 64:
115 case 128:
116 return true;
117
118 case 96:
119 // XXX hasLoadX3
120 return (ST.getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS);
121
122 case 256:
123 case 512:
124 // TODO: constant loads
125 default:
126 return false;
127 }
128 });
129
130
Tom Stellardca166212017-01-30 21:56:46 +0000131
Tom Stellard2860a422017-06-07 13:54:51 +0000132 setAction({G_SELECT, S32}, Legal);
133 setAction({G_SELECT, 1, S1}, Legal);
134
Tom Stellardeb8f1e22017-06-26 15:56:52 +0000135 setAction({G_SHL, S32}, Legal);
136
Tom Stellardca166212017-01-30 21:56:46 +0000137
138 // FIXME: When RegBankSelect inserts copies, it will only create new
139 // registers with scalar types. This means we can end up with
140 // G_LOAD/G_STORE/G_GEP instruction with scalar types for their pointer
141 // operands. In assert builds, the instruction selector will assert
142 // if it sees a generic instruction which isn't legal, so we need to
143 // tell it that scalar types are legal for pointer operands
144 setAction({G_GEP, S64}, Legal);
Tom Stellardca166212017-01-30 21:56:46 +0000145
Matt Arsenault7b9ed892018-03-12 13:35:53 +0000146 for (unsigned Op : {G_EXTRACT_VECTOR_ELT, G_INSERT_VECTOR_ELT}) {
147 getActionDefinitionsBuilder(Op)
148 .legalIf([=](const LegalityQuery &Query) {
149 const LLT &VecTy = Query.Types[1];
150 const LLT &IdxTy = Query.Types[2];
151 return VecTy.getSizeInBits() % 32 == 0 &&
152 VecTy.getSizeInBits() <= 512 &&
153 IdxTy.getSizeInBits() == 32;
154 });
155 }
156
Matt Arsenault71272e62018-03-05 16:25:15 +0000157 // FIXME: Doesn't handle extract of illegal sizes.
158 getActionDefinitionsBuilder(G_EXTRACT)
159 .unsupportedIf([=](const LegalityQuery &Query) {
160 return Query.Types[0].getSizeInBits() >= Query.Types[1].getSizeInBits();
161 })
162 .legalIf([=](const LegalityQuery &Query) {
163 const LLT &Ty0 = Query.Types[0];
164 const LLT &Ty1 = Query.Types[1];
165 return (Ty0.getSizeInBits() % 32 == 0) &&
166 (Ty1.getSizeInBits() % 32 == 0);
167 });
168
Matt Arsenault503afda2018-03-12 13:35:43 +0000169 // Merge/Unmerge
170 for (unsigned Op : {G_MERGE_VALUES, G_UNMERGE_VALUES}) {
171 unsigned BigTyIdx = Op == G_MERGE_VALUES ? 0 : 1;
172 unsigned LitTyIdx = Op == G_MERGE_VALUES ? 1 : 0;
173
174 getActionDefinitionsBuilder(Op)
175 .legalIf([=](const LegalityQuery &Query) {
176 const LLT &BigTy = Query.Types[BigTyIdx];
177 const LLT &LitTy = Query.Types[LitTyIdx];
178 return BigTy.getSizeInBits() % 32 == 0 &&
179 LitTy.getSizeInBits() % 32 == 0 &&
180 BigTy.getSizeInBits() <= 512;
181 })
182 // Any vectors left are the wrong size. Scalarize them.
183 .fewerElementsIf([](const LegalityQuery &Query) { return true; },
184 [](const LegalityQuery &Query) {
185 return std::make_pair(
186 0, Query.Types[0].getElementType());
187 })
188 .fewerElementsIf([](const LegalityQuery &Query) { return true; },
189 [](const LegalityQuery &Query) {
190 return std::make_pair(
191 1, Query.Types[1].getElementType());
192 });
193
194 }
195
Tom Stellardca166212017-01-30 21:56:46 +0000196 computeTables();
197}