blob: 7c0a6dd9f4aeb41716de9d97edc89056b380f268 [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- SIISelLowering.cpp - SI DAG Lowering Implementation ---------------===//
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//
10/// \file
11/// \brief Custom DAG lowering for SI
12//
13//===----------------------------------------------------------------------===//
14
15#include "SIISelLowering.h"
Christian Konig99ee0f42013-03-07 09:04:14 +000016#include "AMDGPU.h"
Matt Arsenault41e2f2b2014-02-24 21:01:28 +000017#include "AMDGPUSubtarget.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000018#include "AMDILIntrinsicInfo.h"
19#include "SIInstrInfo.h"
20#include "SIMachineFunctionInfo.h"
21#include "SIRegisterInfo.h"
Christian Konig2c8f6d52013-03-07 09:03:52 +000022#include "llvm/CodeGen/CallingConvLower.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000023#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/CodeGen/SelectionDAG.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000026#include "llvm/IR/Function.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000027
28using namespace llvm;
29
30SITargetLowering::SITargetLowering(TargetMachine &TM) :
Bill Wendling37e9adb2013-06-07 20:28:55 +000031 AMDGPUTargetLowering(TM) {
Tom Stellard1bd80722014-04-30 15:31:33 +000032 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
Tom Stellard436780b2014-05-15 14:41:57 +000033 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000034
Christian Konig2214f142013-03-07 09:03:38 +000035 addRegisterClass(MVT::v32i8, &AMDGPU::SReg_256RegClass);
36 addRegisterClass(MVT::v64i8, &AMDGPU::SReg_512RegClass);
37
Tom Stellard334b29c2014-04-17 21:00:09 +000038 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
Tom Stellard436780b2014-05-15 14:41:57 +000039 addRegisterClass(MVT::f32, &AMDGPU::VReg_32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000040
Tom Stellard436780b2014-05-15 14:41:57 +000041 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
42 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
43 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000044
Tom Stellard436780b2014-05-15 14:41:57 +000045 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
46 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000047
Tom Stellard538ceeb2013-02-07 17:02:09 +000048 addRegisterClass(MVT::v8i32, &AMDGPU::VReg_256RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000049 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
50
Tom Stellard538ceeb2013-02-07 17:02:09 +000051 addRegisterClass(MVT::v16i32, &AMDGPU::VReg_512RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000052 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000053
54 computeRegisterProperties();
55
Tom Stellardc0845332013-11-22 23:07:58 +000056 // Condition Codes
57 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
58 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
59 setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
60 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
61 setCondCodeAction(ISD::SETULE, MVT::f32, Expand);
62 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
63
64 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
65 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
66 setCondCodeAction(ISD::SETUGE, MVT::f64, Expand);
67 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
68 setCondCodeAction(ISD::SETULE, MVT::f64, Expand);
69 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
70
Christian Konig2989ffc2013-03-18 11:34:16 +000071 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
72 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
73 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
74 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
75
Tom Stellard75aadc22012-12-11 21:25:42 +000076 setOperationAction(ISD::ADD, MVT::i32, Legal);
Matt Arsenaulte8d21462013-11-18 20:09:40 +000077 setOperationAction(ISD::ADDC, MVT::i32, Legal);
78 setOperationAction(ISD::ADDE, MVT::i32, Legal);
Aaron Watrydaabb202013-06-25 13:55:52 +000079
Tom Stellard35bb18c2013-08-26 15:06:04 +000080 // We need to custom lower vector stores from local memory
81 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
82 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellardaf775432013-10-23 00:44:32 +000083 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
84 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
85
86 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
87 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
Tom Stellard35bb18c2013-08-26 15:06:04 +000088
Tom Stellard81d871d2013-11-13 23:36:50 +000089 // We need to custom lower loads/stores from private memory
90 setOperationAction(ISD::LOAD, MVT::i32, Custom);
91 setOperationAction(ISD::LOAD, MVT::i64, Custom);
92 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
93 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellard967bf582014-02-13 23:34:15 +000094 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +000095
Tom Stellard1c8788e2014-03-07 20:12:33 +000096 setOperationAction(ISD::STORE, MVT::i1, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +000097 setOperationAction(ISD::STORE, MVT::i32, Custom);
98 setOperationAction(ISD::STORE, MVT::i64, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +000099 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
100 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
101
Tom Stellard0ec134f2014-02-04 17:18:40 +0000102 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Tom Stellardda99c6e2014-03-24 16:07:30 +0000103 setOperationAction(ISD::SELECT, MVT::f64, Promote);
104 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
Tom Stellard81d871d2013-11-13 23:36:50 +0000105
Tom Stellard75aadc22012-12-11 21:25:42 +0000106 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
107 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
108
109 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
Tom Stellard754f80f2013-04-05 23:31:51 +0000110
Tom Stellard83747202013-07-18 21:43:53 +0000111 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
112 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
113
Tom Stellardaf775432013-10-23 00:44:32 +0000114 setOperationAction(ISD::ANY_EXTEND, MVT::i64, Custom);
Tom Stellard046039e2013-06-03 17:40:03 +0000115 setOperationAction(ISD::SIGN_EXTEND, MVT::i64, Custom);
Tom Stellard98f675a2013-08-01 15:23:26 +0000116 setOperationAction(ISD::ZERO_EXTEND, MVT::i64, Custom);
Tom Stellard046039e2013-06-03 17:40:03 +0000117
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000118 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
120 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
121
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
125
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000126 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000127 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
128 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
129
130 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Custom);
131
132 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
133
Tom Stellard94593ee2013-06-03 17:40:18 +0000134 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Tom Stellard9fa17912013-08-14 23:24:45 +0000135 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
136 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom);
137 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
Tom Stellard94593ee2013-06-03 17:40:18 +0000138
Tom Stellardafcf12f2013-09-12 02:55:14 +0000139 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
140
Matt Arsenault470acd82014-04-15 22:28:39 +0000141 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Tom Stellarde9373602014-01-22 19:24:14 +0000142 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Custom);
143 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Custom);
Matt Arsenault470acd82014-04-15 22:28:39 +0000144 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000145 setLoadExtAction(ISD::SEXTLOAD, MVT::v8i16, Expand);
146 setLoadExtAction(ISD::SEXTLOAD, MVT::v16i16, Expand);
Tom Stellard31209cc2013-07-15 19:00:09 +0000147
Matt Arsenault470acd82014-04-15 22:28:39 +0000148 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
149 setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Custom);
150 setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Custom);
151 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Expand);
152
153 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
Tom Stellarde9373602014-01-22 19:24:14 +0000154 setLoadExtAction(ISD::EXTLOAD, MVT::i8, Custom);
155 setLoadExtAction(ISD::EXTLOAD, MVT::i16, Custom);
156 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Expand);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000157 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000158
Tom Stellarde9373602014-01-22 19:24:14 +0000159 setTruncStoreAction(MVT::i32, MVT::i8, Custom);
160 setTruncStoreAction(MVT::i32, MVT::i16, Custom);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000161 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Matt Arsenault6f243792013-09-05 19:41:10 +0000162 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000163 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
164 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000165
Matt Arsenault470acd82014-04-15 22:28:39 +0000166 setOperationAction(ISD::LOAD, MVT::i1, Custom);
167
Tom Stellardfd155822013-08-26 15:05:36 +0000168 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Tom Stellard04c0e982014-01-22 19:24:21 +0000169 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000170 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
Michel Danzer49812b52013-07-10 16:37:07 +0000171
Tom Stellard5f337882014-04-29 23:12:43 +0000172 // These should use UDIVREM, so set them to expand
173 setOperationAction(ISD::UDIV, MVT::i64, Expand);
174 setOperationAction(ISD::UREM, MVT::i64, Expand);
175
Tom Stellard967bf582014-02-13 23:34:15 +0000176 // We only support LOAD/STORE and vector manipulation ops for vectors
177 // with > 4 elements.
178 MVT VecTypes[] = {
Tom Stellardd61a1c32014-02-28 21:36:37 +0000179 MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32
Tom Stellard967bf582014-02-13 23:34:15 +0000180 };
181
Matt Arsenaultd504a742014-05-15 21:44:05 +0000182 for (MVT VT : VecTypes) {
Tom Stellard967bf582014-02-13 23:34:15 +0000183 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
184 switch(Op) {
185 case ISD::LOAD:
186 case ISD::STORE:
187 case ISD::BUILD_VECTOR:
188 case ISD::BITCAST:
189 case ISD::EXTRACT_VECTOR_ELT:
190 case ISD::INSERT_VECTOR_ELT:
191 case ISD::CONCAT_VECTORS:
192 case ISD::INSERT_SUBVECTOR:
193 case ISD::EXTRACT_SUBVECTOR:
194 break;
195 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000196 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000197 break;
198 }
199 }
200 }
201
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000202 for (int I = MVT::v1f64; I <= MVT::v8f64; ++I) {
203 MVT::SimpleValueType VT = static_cast<MVT::SimpleValueType>(I);
Matt Arsenaulta81aee82014-02-24 21:16:50 +0000204 setOperationAction(ISD::FTRUNC, VT, Expand);
205 setOperationAction(ISD::FCEIL, VT, Expand);
206 setOperationAction(ISD::FFLOOR, VT, Expand);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000207 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000208
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000209 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
210 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
211 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
212 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
Matt Arsenaulta90d22f2014-04-17 17:06:37 +0000213 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000214 }
215
216 setTargetDAGCombine(ISD::SELECT_CC);
Tom Stellard75aadc22012-12-11 21:25:42 +0000217 setTargetDAGCombine(ISD::SETCC);
Michel Danzerf52a6722013-03-08 10:58:01 +0000218
Christian Konigeecebd02013-03-26 14:04:02 +0000219 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000220}
221
Tom Stellard0125f2a2013-06-25 02:39:35 +0000222//===----------------------------------------------------------------------===//
223// TargetLowering queries
224//===----------------------------------------------------------------------===//
225
226bool SITargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
Matt Arsenault25793a32014-02-05 23:15:53 +0000227 unsigned AddrSpace,
Tom Stellard0125f2a2013-06-25 02:39:35 +0000228 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000229 if (IsFast)
230 *IsFast = false;
231
Tom Stellard0125f2a2013-06-25 02:39:35 +0000232 // XXX: This depends on the address space and also we may want to revist
233 // the alignment values we specify in the DataLayout.
Matt Arsenault1018c892014-04-24 17:08:26 +0000234
235 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
236 // which isn't a simple VT.
Tom Stellard81d871d2013-11-13 23:36:50 +0000237 if (!VT.isSimple() || VT == MVT::Other)
238 return false;
Matt Arsenault1018c892014-04-24 17:08:26 +0000239
240 // XXX - CI changes say "Support for unaligned memory accesses" but I don't
241 // see what for specifically. The wording everywhere else seems to be the
242 // same.
243
244 // 3.6.4 - Operations using pairs of VGPRs (for example: double-floats) have
245 // no alignment restrictions.
246 if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS) {
247 // Using any pair of GPRs should be the same as any other pair.
248 if (IsFast)
249 *IsFast = true;
250 return VT.bitsGE(MVT::i64);
251 }
252
253 // XXX - The only mention I see of this in the ISA manual is for LDS direct
254 // reads the "byte address and must be dword aligned". Is it also true for the
255 // normal loads and stores?
256 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS)
257 return false;
258
259 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
260 // byte-address are ignored, thus forcing Dword alignment.
261 if (IsFast)
262 *IsFast = true;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000263 return VT.bitsGT(MVT::i32);
264}
265
Matt Arsenaultf751d622014-03-31 20:54:58 +0000266bool SITargetLowering::shouldSplitVectorType(EVT VT) const {
267 return VT.getScalarType().bitsLE(MVT::i16);
Tom Stellardd86003e2013-08-14 23:25:00 +0000268}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000269
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000270bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
271 Type *Ty) const {
272 const SIInstrInfo *TII =
273 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
274 return TII->isInlineConstant(Imm);
275}
276
Tom Stellardaf775432013-10-23 00:44:32 +0000277SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
Tom Stellard94593ee2013-06-03 17:40:18 +0000278 SDLoc DL, SDValue Chain,
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000279 unsigned Offset, bool Signed) const {
Tom Stellard94593ee2013-06-03 17:40:18 +0000280 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
281 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
282 AMDGPUAS::CONSTANT_ADDRESS);
Tom Stellard94593ee2013-06-03 17:40:18 +0000283 SDValue BasePtr = DAG.getCopyFromReg(Chain, DL,
284 MRI.getLiveInVirtReg(AMDGPU::SGPR0_SGPR1), MVT::i64);
285 SDValue Ptr = DAG.getNode(ISD::ADD, DL, MVT::i64, BasePtr,
286 DAG.getConstant(Offset, MVT::i64));
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000287 return DAG.getExtLoad(Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD, DL, VT, Chain, Ptr,
Tom Stellardaf775432013-10-23 00:44:32 +0000288 MachinePointerInfo(UndefValue::get(PtrTy)), MemVT,
289 false, false, MemVT.getSizeInBits() >> 3);
Tom Stellard94593ee2013-06-03 17:40:18 +0000290
291}
292
Christian Konig2c8f6d52013-03-07 09:03:52 +0000293SDValue SITargetLowering::LowerFormalArguments(
294 SDValue Chain,
295 CallingConv::ID CallConv,
296 bool isVarArg,
297 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000298 SDLoc DL, SelectionDAG &DAG,
Christian Konig2c8f6d52013-03-07 09:03:52 +0000299 SmallVectorImpl<SDValue> &InVals) const {
300
301 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
302
303 MachineFunction &MF = DAG.getMachineFunction();
304 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000305 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000306
307 assert(CallConv == CallingConv::C);
308
309 SmallVector<ISD::InputArg, 16> Splits;
Christian Konig99ee0f42013-03-07 09:04:14 +0000310 uint32_t Skipped = 0;
311
312 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000313 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000314
315 // First check if it's a PS input addr
Vincent Lejeuned6236442013-10-13 17:56:16 +0000316 if (Info->ShaderType == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
317 !Arg.Flags.isByVal()) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000318
319 assert((PSInputNum <= 15) && "Too many PS inputs!");
320
321 if (!Arg.Used) {
322 // We can savely skip PS inputs
323 Skipped |= 1 << i;
324 ++PSInputNum;
325 continue;
326 }
327
328 Info->PSInputAddr |= 1 << PSInputNum++;
329 }
330
331 // Second split vertices into their elements
Tom Stellarded882c22013-06-03 17:40:11 +0000332 if (Info->ShaderType != ShaderType::COMPUTE && Arg.VT.isVector()) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000333 ISD::InputArg NewArg = Arg;
334 NewArg.Flags.setSplit();
335 NewArg.VT = Arg.VT.getVectorElementType();
336
337 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
338 // three or five element vertex only needs three or five registers,
339 // NOT four or eigth.
340 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
341 unsigned NumElements = ParamType->getVectorNumElements();
342
343 for (unsigned j = 0; j != NumElements; ++j) {
344 Splits.push_back(NewArg);
345 NewArg.PartOffset += NewArg.VT.getStoreSize();
346 }
347
Tom Stellardaf775432013-10-23 00:44:32 +0000348 } else if (Info->ShaderType != ShaderType::COMPUTE) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000349 Splits.push_back(Arg);
350 }
351 }
352
353 SmallVector<CCValAssign, 16> ArgLocs;
354 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
355 getTargetMachine(), ArgLocs, *DAG.getContext());
356
Christian Konig99ee0f42013-03-07 09:04:14 +0000357 // At least one interpolation mode must be enabled or else the GPU will hang.
358 if (Info->ShaderType == ShaderType::PIXEL && (Info->PSInputAddr & 0x7F) == 0) {
359 Info->PSInputAddr |= 1;
360 CCInfo.AllocateReg(AMDGPU::VGPR0);
361 CCInfo.AllocateReg(AMDGPU::VGPR1);
362 }
363
Tom Stellarded882c22013-06-03 17:40:11 +0000364 // The pointer to the list of arguments is stored in SGPR0, SGPR1
365 if (Info->ShaderType == ShaderType::COMPUTE) {
366 CCInfo.AllocateReg(AMDGPU::SGPR0);
367 CCInfo.AllocateReg(AMDGPU::SGPR1);
Tom Stellard94593ee2013-06-03 17:40:18 +0000368 MF.addLiveIn(AMDGPU::SGPR0_SGPR1, &AMDGPU::SReg_64RegClass);
Tom Stellarded882c22013-06-03 17:40:11 +0000369 }
370
Tom Stellardaf775432013-10-23 00:44:32 +0000371 if (Info->ShaderType == ShaderType::COMPUTE) {
372 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
373 Splits);
374 }
375
Christian Konig2c8f6d52013-03-07 09:03:52 +0000376 AnalyzeFormalArguments(CCInfo, Splits);
377
378 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
379
Christian Konigb7be72d2013-05-17 09:46:48 +0000380 const ISD::InputArg &Arg = Ins[i];
Christian Konig99ee0f42013-03-07 09:04:14 +0000381 if (Skipped & (1 << i)) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000382 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000383 continue;
384 }
385
Christian Konig2c8f6d52013-03-07 09:03:52 +0000386 CCValAssign &VA = ArgLocs[ArgIdx++];
Tom Stellarded882c22013-06-03 17:40:11 +0000387 EVT VT = VA.getLocVT();
388
389 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000390 VT = Ins[i].VT;
391 EVT MemVT = Splits[i].VT;
Tom Stellard94593ee2013-06-03 17:40:18 +0000392 // The first 36 bytes of the input buffer contains information about
393 // thread group and global sizes.
Tom Stellardaf775432013-10-23 00:44:32 +0000394 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, DAG.getRoot(),
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000395 36 + VA.getLocMemOffset(),
396 Ins[i].Flags.isSExt());
Tom Stellarded882c22013-06-03 17:40:11 +0000397 InVals.push_back(Arg);
398 continue;
399 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000400 assert(VA.isRegLoc() && "Parameter must be in a register!");
401
402 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000403
404 if (VT == MVT::i64) {
405 // For now assume it is a pointer
406 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
407 &AMDGPU::SReg_64RegClass);
408 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
409 InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
410 continue;
411 }
412
413 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
414
415 Reg = MF.addLiveIn(Reg, RC);
416 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
417
Christian Konig2c8f6d52013-03-07 09:03:52 +0000418 if (Arg.VT.isVector()) {
419
420 // Build a vector from the registers
421 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
422 unsigned NumElements = ParamType->getVectorNumElements();
423
424 SmallVector<SDValue, 4> Regs;
425 Regs.push_back(Val);
426 for (unsigned j = 1; j != NumElements; ++j) {
427 Reg = ArgLocs[ArgIdx++].getLocReg();
428 Reg = MF.addLiveIn(Reg, RC);
429 Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
430 }
431
432 // Fill up the missing vector elements
433 NumElements = Arg.VT.getVectorNumElements() - NumElements;
434 for (unsigned j = 0; j != NumElements; ++j)
435 Regs.push_back(DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000436
Craig Topper48d114b2014-04-26 18:35:24 +0000437 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000438 continue;
439 }
440
441 InVals.push_back(Val);
442 }
443 return Chain;
444}
445
Tom Stellard75aadc22012-12-11 21:25:42 +0000446MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
447 MachineInstr * MI, MachineBasicBlock * BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000448
Tom Stellard556d9aa2013-06-03 17:39:37 +0000449 MachineBasicBlock::iterator I = *MI;
Tom Stellard919bb6b2014-04-29 23:12:53 +0000450 const SIInstrInfo *TII =
451 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
452 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Tom Stellard556d9aa2013-06-03 17:39:37 +0000453
Tom Stellard75aadc22012-12-11 21:25:42 +0000454 switch (MI->getOpcode()) {
455 default:
456 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
457 case AMDGPU::BRANCH: return BB;
Tom Stellard556d9aa2013-06-03 17:39:37 +0000458 case AMDGPU::SI_ADDR64_RSRC: {
Tom Stellard556d9aa2013-06-03 17:39:37 +0000459 unsigned SuperReg = MI->getOperand(0).getReg();
Tom Stellarddef38c52014-03-21 15:51:53 +0000460 unsigned SubRegLo = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass);
461 unsigned SubRegHi = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass);
462 unsigned SubRegHiHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
463 unsigned SubRegHiLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
Tom Stellard556d9aa2013-06-03 17:39:37 +0000464 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B64), SubRegLo)
465 .addOperand(MI->getOperand(1));
466 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiLo)
467 .addImm(0);
468 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiHi)
Tom Stellard15834092014-03-21 15:51:57 +0000469 .addImm(AMDGPU::RSRC_DATA_FORMAT >> 32);
Tom Stellard556d9aa2013-06-03 17:39:37 +0000470 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SubRegHi)
471 .addReg(SubRegHiLo)
472 .addImm(AMDGPU::sub0)
473 .addReg(SubRegHiHi)
474 .addImm(AMDGPU::sub1);
475 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SuperReg)
476 .addReg(SubRegLo)
477 .addImm(AMDGPU::sub0_sub1)
478 .addReg(SubRegHi)
479 .addImm(AMDGPU::sub2_sub3);
480 MI->eraseFromParent();
481 break;
482 }
Tom Stellard919bb6b2014-04-29 23:12:53 +0000483 case AMDGPU::V_SUB_F64:
Tom Stellard2a6a61052013-07-12 18:15:08 +0000484 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F64),
485 MI->getOperand(0).getReg())
486 .addReg(MI->getOperand(1).getReg())
487 .addReg(MI->getOperand(2).getReg())
488 .addImm(0) /* src2 */
489 .addImm(0) /* ABS */
490 .addImm(0) /* CLAMP */
491 .addImm(0) /* OMOD */
492 .addImm(2); /* NEG */
493 MI->eraseFromParent();
494 break;
Tom Stellard919bb6b2014-04-29 23:12:53 +0000495
Tom Stellard81d871d2013-11-13 23:36:50 +0000496 case AMDGPU::SI_RegisterStorePseudo: {
497 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Tom Stellard81d871d2013-11-13 23:36:50 +0000498 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
499 MachineInstrBuilder MIB =
500 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::SI_RegisterStore),
501 Reg);
502 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
503 MIB.addOperand(MI->getOperand(i));
504
505 MI->eraseFromParent();
Vincent Lejeune79a58342014-05-10 19:18:25 +0000506 break;
507 }
508 case AMDGPU::FABS_SI: {
509 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
510 const SIInstrInfo *TII =
511 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
512 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
513 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_MOV_B32_e32),
514 Reg)
515 .addImm(0x7fffffff);
516 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_AND_B32_e32),
517 MI->getOperand(0).getReg())
518 .addReg(MI->getOperand(1).getReg())
519 .addReg(Reg);
520 MI->eraseFromParent();
521 break;
522 }
523 case AMDGPU::FNEG_SI: {
524 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
525 const SIInstrInfo *TII =
526 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
527 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
528 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_MOV_B32_e32),
529 Reg)
530 .addImm(0x80000000);
531 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_XOR_B32_e32),
532 MI->getOperand(0).getReg())
533 .addReg(MI->getOperand(1).getReg())
534 .addReg(Reg);
535 MI->eraseFromParent();
536 break;
537 }
538 case AMDGPU::FCLAMP_SI: {
539 const SIInstrInfo *TII =
540 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
541 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F32_e64),
542 MI->getOperand(0).getReg())
Vincent Lejeune94af31f2014-05-10 19:18:33 +0000543 .addImm(0) // SRC0 modifiers
Vincent Lejeune79a58342014-05-10 19:18:25 +0000544 .addOperand(MI->getOperand(1))
Vincent Lejeune94af31f2014-05-10 19:18:33 +0000545 .addImm(0) // SRC1 modifiers
Vincent Lejeune79a58342014-05-10 19:18:25 +0000546 .addImm(0) // SRC1
Vincent Lejeune79a58342014-05-10 19:18:25 +0000547 .addImm(1) // CLAMP
Vincent Lejeune94af31f2014-05-10 19:18:33 +0000548 .addImm(0); // OMOD
Vincent Lejeune79a58342014-05-10 19:18:25 +0000549 MI->eraseFromParent();
Tom Stellard81d871d2013-11-13 23:36:50 +0000550 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000551 }
552 return BB;
553}
554
Matt Arsenault758659232013-05-18 00:21:46 +0000555EVT SITargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +0000556 if (!VT.isVector()) {
557 return MVT::i1;
558 }
559 return MVT::getVectorVT(MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +0000560}
561
Christian Konig082a14a2013-03-18 11:34:05 +0000562MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const {
563 return MVT::i32;
564}
565
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +0000566bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
567 VT = VT.getScalarType();
568
569 if (!VT.isSimple())
570 return false;
571
572 switch (VT.getSimpleVT().SimpleTy) {
573 case MVT::f32:
574 return false; /* There is V_MAD_F32 for f32 */
575 case MVT::f64:
576 return true;
577 default:
578 break;
579 }
580
581 return false;
582}
583
Tom Stellard75aadc22012-12-11 21:25:42 +0000584//===----------------------------------------------------------------------===//
585// Custom DAG Lowering Operations
586//===----------------------------------------------------------------------===//
587
588SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Michel Danzer49812b52013-07-10 16:37:07 +0000589 MachineFunction &MF = DAG.getMachineFunction();
590 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellard75aadc22012-12-11 21:25:42 +0000591 switch (Op.getOpcode()) {
592 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +0000593 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +0000594 case ISD::LOAD: {
595 LoadSDNode *Load = dyn_cast<LoadSDNode>(Op);
Tom Stellard80be9652014-02-13 23:34:10 +0000596 if (Op.getValueType().isVector() &&
597 (Load->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
598 Load->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS ||
599 (Load->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
600 Op.getValueType().getVectorNumElements() > 4))) {
Tom Stellard35bb18c2013-08-26 15:06:04 +0000601 SDValue MergedValues[2] = {
602 SplitVectorLoad(Op, DAG),
603 Load->getChain()
604 };
Craig Topper64941d92014-04-27 19:20:57 +0000605 return DAG.getMergeValues(MergedValues, SDLoc(Op));
Tom Stellard35bb18c2013-08-26 15:06:04 +0000606 } else {
Tom Stellard81d871d2013-11-13 23:36:50 +0000607 return LowerLOAD(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +0000608 }
609 }
Tom Stellardaf775432013-10-23 00:44:32 +0000610
Tom Stellard0ec134f2014-02-04 17:18:40 +0000611 case ISD::SELECT: return LowerSELECT(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000612 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Tom Stellard046039e2013-06-03 17:40:03 +0000613 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +0000614 case ISD::STORE: return LowerSTORE(Op, DAG);
Tom Stellardaf775432013-10-23 00:44:32 +0000615 case ISD::ANY_EXTEND: // Fall-through
Tom Stellard98f675a2013-08-01 15:23:26 +0000616 case ISD::ZERO_EXTEND: return LowerZERO_EXTEND(Op, DAG);
Michel Danzer49812b52013-07-10 16:37:07 +0000617 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +0000618 case ISD::INTRINSIC_WO_CHAIN: {
619 unsigned IntrinsicID =
620 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
621 EVT VT = Op.getValueType();
622 SDLoc DL(Op);
623 //XXX: Hardcoded we only use two to store the pointer to the parameters.
624 unsigned NumUserSGPRs = 2;
625 switch (IntrinsicID) {
626 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
627 case Intrinsic::r600_read_ngroups_x:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000628 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 0, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000629 case Intrinsic::r600_read_ngroups_y:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000630 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 4, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000631 case Intrinsic::r600_read_ngroups_z:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000632 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 8, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000633 case Intrinsic::r600_read_global_size_x:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000634 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 12, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000635 case Intrinsic::r600_read_global_size_y:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000636 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 16, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000637 case Intrinsic::r600_read_global_size_z:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000638 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 20, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000639 case Intrinsic::r600_read_local_size_x:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000640 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 24, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000641 case Intrinsic::r600_read_local_size_y:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000642 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 28, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000643 case Intrinsic::r600_read_local_size_z:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000644 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 32, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000645 case Intrinsic::r600_read_tgid_x:
646 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
647 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 0), VT);
648 case Intrinsic::r600_read_tgid_y:
649 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
650 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 1), VT);
651 case Intrinsic::r600_read_tgid_z:
652 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
653 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 2), VT);
654 case Intrinsic::r600_read_tidig_x:
655 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
656 AMDGPU::VGPR0, VT);
657 case Intrinsic::r600_read_tidig_y:
658 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
659 AMDGPU::VGPR1, VT);
660 case Intrinsic::r600_read_tidig_z:
661 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
662 AMDGPU::VGPR2, VT);
Tom Stellard9fa17912013-08-14 23:24:45 +0000663 case AMDGPUIntrinsic::SI_load_const: {
664 SDValue Ops [] = {
Tom Stellard868fd922014-04-17 21:00:11 +0000665 Op.getOperand(1),
Tom Stellard9fa17912013-08-14 23:24:45 +0000666 Op.getOperand(2)
667 };
Tom Stellard94593ee2013-06-03 17:40:18 +0000668
Benjamin Kramera8eecee2013-08-16 14:48:09 +0000669 MachineMemOperand *MMO = MF.getMachineMemOperand(
670 MachinePointerInfo(),
671 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
672 VT.getSizeInBits() / 8, 4);
Tom Stellard9fa17912013-08-14 23:24:45 +0000673 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
Craig Topper206fcd42014-04-26 19:29:41 +0000674 Op->getVTList(), Ops, VT, MMO);
Tom Stellard9fa17912013-08-14 23:24:45 +0000675 }
676 case AMDGPUIntrinsic::SI_sample:
677 return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG);
678 case AMDGPUIntrinsic::SI_sampleb:
679 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG);
680 case AMDGPUIntrinsic::SI_sampled:
681 return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG);
682 case AMDGPUIntrinsic::SI_samplel:
683 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG);
684 case AMDGPUIntrinsic::SI_vs_load_input:
685 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
Tom Stellard868fd922014-04-17 21:00:11 +0000686 Op.getOperand(1),
Tom Stellard9fa17912013-08-14 23:24:45 +0000687 Op.getOperand(2),
688 Op.getOperand(3));
Tom Stellard94593ee2013-06-03 17:40:18 +0000689 }
690 }
Tom Stellardafcf12f2013-09-12 02:55:14 +0000691
692 case ISD::INTRINSIC_VOID:
693 SDValue Chain = Op.getOperand(0);
694 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
695
696 switch (IntrinsicID) {
697 case AMDGPUIntrinsic::SI_tbuffer_store: {
698 SDLoc DL(Op);
699 SDValue Ops [] = {
700 Chain,
Tom Stellard868fd922014-04-17 21:00:11 +0000701 Op.getOperand(2),
Tom Stellardafcf12f2013-09-12 02:55:14 +0000702 Op.getOperand(3),
703 Op.getOperand(4),
704 Op.getOperand(5),
705 Op.getOperand(6),
706 Op.getOperand(7),
707 Op.getOperand(8),
708 Op.getOperand(9),
709 Op.getOperand(10),
710 Op.getOperand(11),
711 Op.getOperand(12),
712 Op.getOperand(13),
713 Op.getOperand(14)
714 };
715 EVT VT = Op.getOperand(3).getValueType();
716
717 MachineMemOperand *MMO = MF.getMachineMemOperand(
718 MachinePointerInfo(),
719 MachineMemOperand::MOStore,
720 VT.getSizeInBits() / 8, 4);
721 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
Craig Topper206fcd42014-04-26 19:29:41 +0000722 Op->getVTList(), Ops, VT, MMO);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000723 }
724 default:
725 break;
726 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000727 }
728 return SDValue();
729}
730
Tom Stellardf8794352012-12-19 22:10:31 +0000731/// \brief Helper function for LowerBRCOND
732static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000733
Tom Stellardf8794352012-12-19 22:10:31 +0000734 SDNode *Parent = Value.getNode();
735 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
736 I != E; ++I) {
737
738 if (I.getUse().get() != Value)
739 continue;
740
741 if (I->getOpcode() == Opcode)
742 return *I;
743 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000744 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000745}
746
747/// This transforms the control flow intrinsics to get the branch destination as
748/// last parameter, also switches branch target with BR if the need arise
749SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
750 SelectionDAG &DAG) const {
751
Andrew Trickef9de2a2013-05-25 02:42:55 +0000752 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +0000753
754 SDNode *Intr = BRCOND.getOperand(1).getNode();
755 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +0000756 SDNode *BR = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000757
758 if (Intr->getOpcode() == ISD::SETCC) {
759 // As long as we negate the condition everything is fine
760 SDNode *SetCC = Intr;
761 assert(SetCC->getConstantOperandVal(1) == 1);
NAKAMURA Takumi458a8272013-01-07 11:14:44 +0000762 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
763 ISD::SETNE);
Tom Stellardf8794352012-12-19 22:10:31 +0000764 Intr = SetCC->getOperand(0).getNode();
765
766 } else {
767 // Get the target from BR if we don't negate the condition
768 BR = findUser(BRCOND, ISD::BR);
769 Target = BR->getOperand(1);
770 }
771
772 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
773
774 // Build the result and
775 SmallVector<EVT, 4> Res;
776 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
777 Res.push_back(Intr->getValueType(i));
778
779 // operands of the new intrinsic call
780 SmallVector<SDValue, 4> Ops;
781 Ops.push_back(BRCOND.getOperand(0));
782 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
783 Ops.push_back(Intr->getOperand(i));
784 Ops.push_back(Target);
785
786 // build the new intrinsic call
787 SDNode *Result = DAG.getNode(
788 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +0000789 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +0000790
791 if (BR) {
792 // Give the branch instruction our target
793 SDValue Ops[] = {
794 BR->getOperand(0),
795 BRCOND.getOperand(2)
796 };
Craig Topper131de822014-04-27 19:21:16 +0000797 DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops);
Tom Stellardf8794352012-12-19 22:10:31 +0000798 }
799
800 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
801
802 // Copy the intrinsic results to registers
803 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
804 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
805 if (!CopyToReg)
806 continue;
807
808 Chain = DAG.getCopyToReg(
809 Chain, DL,
810 CopyToReg->getOperand(1),
811 SDValue(Result, i - 1),
812 SDValue());
813
814 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
815 }
816
817 // Remove the old intrinsic from the chain
818 DAG.ReplaceAllUsesOfValueWith(
819 SDValue(Intr, Intr->getNumValues() - 1),
820 Intr->getOperand(0));
821
822 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +0000823}
824
Tom Stellard81d871d2013-11-13 23:36:50 +0000825SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
826 SDLoc DL(Op);
827 LoadSDNode *Load = cast<LoadSDNode>(Op);
Tom Stellarde9373602014-01-22 19:24:14 +0000828 SDValue Ret = AMDGPUTargetLowering::LowerLOAD(Op, DAG);
829 SDValue MergedValues[2];
830 MergedValues[1] = Load->getChain();
831 if (Ret.getNode()) {
832 MergedValues[0] = Ret;
Craig Topper64941d92014-04-27 19:20:57 +0000833 return DAG.getMergeValues(MergedValues, DL);
Tom Stellarde9373602014-01-22 19:24:14 +0000834 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000835
Tom Stellarde9373602014-01-22 19:24:14 +0000836 if (Load->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
Tom Stellard81d871d2013-11-13 23:36:50 +0000837 return SDValue();
Tom Stellarde9373602014-01-22 19:24:14 +0000838 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000839
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000840 EVT MemVT = Load->getMemoryVT();
841
842 assert(!MemVT.isVector() && "Private loads should be scalarized");
843 assert(!MemVT.isFloatingPoint() && "FP loads should be promoted to int");
844
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000845 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Load->getBasePtr(),
Tom Stellard81d871d2013-11-13 23:36:50 +0000846 DAG.getConstant(2, MVT::i32));
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000847 Ret = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
Tom Stellarde9373602014-01-22 19:24:14 +0000848 Load->getChain(), Ptr,
849 DAG.getTargetConstant(0, MVT::i32),
850 Op.getOperand(2));
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000851 if (MemVT.getSizeInBits() == 64) {
852 SDValue IncPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
853 DAG.getConstant(1, MVT::i32));
854
855 SDValue LoadUpper = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
856 Load->getChain(), IncPtr,
857 DAG.getTargetConstant(0, MVT::i32),
858 Op.getOperand(2));
859
860 Ret = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ret, LoadUpper);
861 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000862
Tom Stellarde9373602014-01-22 19:24:14 +0000863 MergedValues[0] = Ret;
Craig Topper64941d92014-04-27 19:20:57 +0000864 return DAG.getMergeValues(MergedValues, DL);
Tom Stellard81d871d2013-11-13 23:36:50 +0000865
866}
867
Tom Stellard9fa17912013-08-14 23:24:45 +0000868SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode,
869 const SDValue &Op,
870 SelectionDAG &DAG) const {
871 return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1),
872 Op.getOperand(2),
Tom Stellard868fd922014-04-17 21:00:11 +0000873 Op.getOperand(3),
Tom Stellard9fa17912013-08-14 23:24:45 +0000874 Op.getOperand(4));
875}
876
Tom Stellard0ec134f2014-02-04 17:18:40 +0000877SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
878 if (Op.getValueType() != MVT::i64)
879 return SDValue();
880
881 SDLoc DL(Op);
882 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000883
884 SDValue Zero = DAG.getConstant(0, MVT::i32);
885 SDValue One = DAG.getConstant(1, MVT::i32);
886
Tom Stellard7ea3d6d2014-03-31 14:01:55 +0000887 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
888 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
889
890 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
891 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000892
893 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
894
Tom Stellard7ea3d6d2014-03-31 14:01:55 +0000895 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
896 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000897
898 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
899
Tom Stellard7ea3d6d2014-03-31 14:01:55 +0000900 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
901 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000902}
903
Tom Stellard75aadc22012-12-11 21:25:42 +0000904SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
905 SDValue LHS = Op.getOperand(0);
906 SDValue RHS = Op.getOperand(1);
907 SDValue True = Op.getOperand(2);
908 SDValue False = Op.getOperand(3);
909 SDValue CC = Op.getOperand(4);
910 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000911 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000912
Tom Stellard75aadc22012-12-11 21:25:42 +0000913 SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC);
914 return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False);
915}
916
Tom Stellard046039e2013-06-03 17:40:03 +0000917SDValue SITargetLowering::LowerSIGN_EXTEND(SDValue Op,
918 SelectionDAG &DAG) const {
919 EVT VT = Op.getValueType();
920 SDLoc DL(Op);
921
922 if (VT != MVT::i64) {
923 return SDValue();
924 }
925
926 SDValue Hi = DAG.getNode(ISD::SRA, DL, MVT::i32, Op.getOperand(0),
927 DAG.getConstant(31, MVT::i32));
928
929 return DAG.getNode(ISD::BUILD_PAIR, DL, VT, Op.getOperand(0), Hi);
930}
931
Tom Stellard81d871d2013-11-13 23:36:50 +0000932SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
933 SDLoc DL(Op);
934 StoreSDNode *Store = cast<StoreSDNode>(Op);
935 EVT VT = Store->getMemoryVT();
936
937 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
938 if (Ret.getNode())
939 return Ret;
940
941 if (VT.isVector() && VT.getVectorNumElements() >= 8)
942 return SplitVectorStore(Op, DAG);
943
Tom Stellard1c8788e2014-03-07 20:12:33 +0000944 if (VT == MVT::i1)
945 return DAG.getTruncStore(Store->getChain(), DL,
946 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
947 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
948
Tom Stellard81d871d2013-11-13 23:36:50 +0000949 if (Store->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS)
950 return SDValue();
951
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000952 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Store->getBasePtr(),
Tom Stellard81d871d2013-11-13 23:36:50 +0000953 DAG.getConstant(2, MVT::i32));
954 SDValue Chain = Store->getChain();
955 SmallVector<SDValue, 8> Values;
956
Tom Stellarde9373602014-01-22 19:24:14 +0000957 if (Store->isTruncatingStore()) {
958 unsigned Mask = 0;
959 if (Store->getMemoryVT() == MVT::i8) {
960 Mask = 0xff;
961 } else if (Store->getMemoryVT() == MVT::i16) {
962 Mask = 0xffff;
963 }
964 SDValue Dst = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
965 Chain, Store->getBasePtr(),
966 DAG.getConstant(0, MVT::i32));
967 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getBasePtr(),
968 DAG.getConstant(0x3, MVT::i32));
969 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
970 DAG.getConstant(3, MVT::i32));
971 SDValue MaskedValue = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getValue(),
972 DAG.getConstant(Mask, MVT::i32));
973 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
974 MaskedValue, ShiftAmt);
975 SDValue RotrAmt = DAG.getNode(ISD::SUB, DL, MVT::i32,
976 DAG.getConstant(32, MVT::i32), ShiftAmt);
977 SDValue DstMask = DAG.getNode(ISD::ROTR, DL, MVT::i32,
978 DAG.getConstant(Mask, MVT::i32),
979 RotrAmt);
980 Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
981 Dst = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
982
983 Values.push_back(Dst);
984 } else if (VT == MVT::i64) {
Tom Stellard81d871d2013-11-13 23:36:50 +0000985 for (unsigned i = 0; i < 2; ++i) {
986 Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
987 Store->getValue(), DAG.getConstant(i, MVT::i32)));
988 }
989 } else if (VT == MVT::i128) {
990 for (unsigned i = 0; i < 2; ++i) {
991 for (unsigned j = 0; j < 2; ++j) {
992 Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
993 DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64,
994 Store->getValue(), DAG.getConstant(i, MVT::i32)),
995 DAG.getConstant(j, MVT::i32)));
996 }
997 }
998 } else {
999 Values.push_back(Store->getValue());
1000 }
1001
1002 for (unsigned i = 0; i < Values.size(); ++i) {
1003 SDValue PartPtr = DAG.getNode(ISD::ADD, DL, MVT::i32,
1004 Ptr, DAG.getConstant(i, MVT::i32));
1005 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1006 Chain, Values[i], PartPtr,
1007 DAG.getTargetConstant(0, MVT::i32));
1008 }
1009 return Chain;
1010}
1011
1012
Tom Stellard98f675a2013-08-01 15:23:26 +00001013SDValue SITargetLowering::LowerZERO_EXTEND(SDValue Op,
1014 SelectionDAG &DAG) const {
1015 EVT VT = Op.getValueType();
1016 SDLoc DL(Op);
1017
1018 if (VT != MVT::i64) {
1019 return SDValue();
1020 }
1021
Matt Arsenault51df0c12014-04-17 02:03:08 +00001022 SDValue Src = Op.getOperand(0);
1023 if (Src.getValueType() != MVT::i32)
1024 Src = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Src);
1025
1026 SDValue Zero = DAG.getConstant(0, MVT::i32);
1027 return DAG.getNode(ISD::BUILD_PAIR, DL, VT, Src, Zero);
Tom Stellard98f675a2013-08-01 15:23:26 +00001028}
1029
Tom Stellard75aadc22012-12-11 21:25:42 +00001030//===----------------------------------------------------------------------===//
1031// Custom DAG optimizations
1032//===----------------------------------------------------------------------===//
1033
1034SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
1035 DAGCombinerInfo &DCI) const {
1036 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001037 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00001038 EVT VT = N->getValueType(0);
1039
1040 switch (N->getOpcode()) {
Tom Stellard50122a52014-04-07 19:45:41 +00001041 default: return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001042 case ISD::SELECT_CC: {
Tom Stellard75aadc22012-12-11 21:25:42 +00001043 ConstantSDNode *True, *False;
1044 // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
1045 if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1046 && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1047 && True->isAllOnesValue()
1048 && False->isNullValue()
1049 && VT == MVT::i1) {
1050 return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
1051 N->getOperand(1), N->getOperand(4));
1052
1053 }
1054 break;
1055 }
1056 case ISD::SETCC: {
1057 SDValue Arg0 = N->getOperand(0);
1058 SDValue Arg1 = N->getOperand(1);
1059 SDValue CC = N->getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00001060 ConstantSDNode * C = nullptr;
Tom Stellard75aadc22012-12-11 21:25:42 +00001061 ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
1062
1063 // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
1064 if (VT == MVT::i1
1065 && Arg0.getOpcode() == ISD::SIGN_EXTEND
1066 && Arg0.getOperand(0).getValueType() == MVT::i1
1067 && (C = dyn_cast<ConstantSDNode>(Arg1))
1068 && C->isNullValue()
1069 && CCOp == ISD::SETNE) {
1070 return SimplifySetCC(VT, Arg0.getOperand(0),
1071 DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
1072 }
1073 break;
1074 }
1075 }
1076 return SDValue();
1077}
Christian Konigd910b7d2013-02-26 17:52:16 +00001078
Matt Arsenault758659232013-05-18 00:21:46 +00001079/// \brief Test if RegClass is one of the VSrc classes
Christian Konigf82901a2013-02-26 17:52:23 +00001080static bool isVSrc(unsigned RegClass) {
1081 return AMDGPU::VSrc_32RegClassID == RegClass ||
1082 AMDGPU::VSrc_64RegClassID == RegClass;
1083}
1084
Matt Arsenault758659232013-05-18 00:21:46 +00001085/// \brief Test if RegClass is one of the SSrc classes
Christian Konigf82901a2013-02-26 17:52:23 +00001086static bool isSSrc(unsigned RegClass) {
1087 return AMDGPU::SSrc_32RegClassID == RegClass ||
1088 AMDGPU::SSrc_64RegClassID == RegClass;
1089}
1090
1091/// \brief Analyze the possible immediate value Op
1092///
1093/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
1094/// and the immediate value if it's a literal immediate
1095int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
1096
1097 union {
1098 int32_t I;
1099 float F;
1100 } Imm;
1101
Tom Stellardedbf1eb2013-04-05 23:31:20 +00001102 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
1103 if (Node->getZExtValue() >> 32) {
1104 return -1;
1105 }
Christian Konigf82901a2013-02-26 17:52:23 +00001106 Imm.I = Node->getSExtValue();
Tom Stellard7ed0b522014-04-03 20:19:27 +00001107 } else if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
1108 if (N->getValueType(0) != MVT::f32)
1109 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00001110 Imm.F = Node->getValueAPF().convertToFloat();
Tom Stellard7ed0b522014-04-03 20:19:27 +00001111 } else
Christian Konigf82901a2013-02-26 17:52:23 +00001112 return -1; // It isn't an immediate
1113
1114 if ((Imm.I >= -16 && Imm.I <= 64) ||
1115 Imm.F == 0.5f || Imm.F == -0.5f ||
1116 Imm.F == 1.0f || Imm.F == -1.0f ||
1117 Imm.F == 2.0f || Imm.F == -2.0f ||
1118 Imm.F == 4.0f || Imm.F == -4.0f)
1119 return 0; // It's an inline immediate
1120
1121 return Imm.I; // It's a literal immediate
1122}
1123
1124/// \brief Try to fold an immediate directly into an instruction
1125bool SITargetLowering::foldImm(SDValue &Operand, int32_t &Immediate,
1126 bool &ScalarSlotUsed) const {
1127
1128 MachineSDNode *Mov = dyn_cast<MachineSDNode>(Operand);
Bill Wendling37e9adb2013-06-07 20:28:55 +00001129 const SIInstrInfo *TII =
1130 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
Craig Topper062a2ba2014-04-25 05:30:21 +00001131 if (!Mov || !TII->isMov(Mov->getMachineOpcode()))
Christian Konigf82901a2013-02-26 17:52:23 +00001132 return false;
1133
1134 const SDValue &Op = Mov->getOperand(0);
1135 int32_t Value = analyzeImmediate(Op.getNode());
1136 if (Value == -1) {
1137 // Not an immediate at all
1138 return false;
1139
1140 } else if (Value == 0) {
1141 // Inline immediates can always be fold
1142 Operand = Op;
1143 return true;
1144
1145 } else if (Value == Immediate) {
1146 // Already fold literal immediate
1147 Operand = Op;
1148 return true;
1149
1150 } else if (!ScalarSlotUsed && !Immediate) {
1151 // Fold this literal immediate
1152 ScalarSlotUsed = true;
1153 Immediate = Value;
1154 Operand = Op;
1155 return true;
1156
1157 }
1158
1159 return false;
1160}
1161
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001162const TargetRegisterClass *SITargetLowering::getRegClassForNode(
1163 SelectionDAG &DAG, const SDValue &Op) const {
1164 const SIInstrInfo *TII =
1165 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1166 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1167
1168 if (!Op->isMachineOpcode()) {
1169 switch(Op->getOpcode()) {
1170 case ISD::CopyFromReg: {
1171 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1172 unsigned Reg = cast<RegisterSDNode>(Op->getOperand(1))->getReg();
1173 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1174 return MRI.getRegClass(Reg);
1175 }
1176 return TRI.getPhysRegClass(Reg);
1177 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001178 default: return nullptr;
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001179 }
1180 }
1181 const MCInstrDesc &Desc = TII->get(Op->getMachineOpcode());
1182 int OpClassID = Desc.OpInfo[Op.getResNo()].RegClass;
1183 if (OpClassID != -1) {
1184 return TRI.getRegClass(OpClassID);
1185 }
1186 switch(Op.getMachineOpcode()) {
1187 case AMDGPU::COPY_TO_REGCLASS:
1188 // Operand 1 is the register class id for COPY_TO_REGCLASS instructions.
1189 OpClassID = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1190
1191 // If the COPY_TO_REGCLASS instruction is copying to a VSrc register
1192 // class, then the register class for the value could be either a
1193 // VReg or and SReg. In order to get a more accurate
1194 if (OpClassID == AMDGPU::VSrc_32RegClassID ||
1195 OpClassID == AMDGPU::VSrc_64RegClassID) {
1196 return getRegClassForNode(DAG, Op.getOperand(0));
1197 }
1198 return TRI.getRegClass(OpClassID);
1199 case AMDGPU::EXTRACT_SUBREG: {
1200 int SubIdx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1201 const TargetRegisterClass *SuperClass =
1202 getRegClassForNode(DAG, Op.getOperand(0));
1203 return TRI.getSubClassWithSubReg(SuperClass, SubIdx);
1204 }
1205 case AMDGPU::REG_SEQUENCE:
1206 // Operand 0 is the register class id for REG_SEQUENCE instructions.
1207 return TRI.getRegClass(
1208 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue());
1209 default:
1210 return getRegClassFor(Op.getSimpleValueType());
1211 }
1212}
1213
Christian Konigf82901a2013-02-26 17:52:23 +00001214/// \brief Does "Op" fit into register class "RegClass" ?
Tom Stellardb35efba2013-05-20 15:02:01 +00001215bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, const SDValue &Op,
Christian Konigf82901a2013-02-26 17:52:23 +00001216 unsigned RegClass) const {
Bill Wendling37e9adb2013-06-07 20:28:55 +00001217 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001218 const TargetRegisterClass *RC = getRegClassForNode(DAG, Op);
1219 if (!RC) {
Christian Konigf82901a2013-02-26 17:52:23 +00001220 return false;
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001221 }
1222 return TRI->getRegClass(RegClass)->hasSubClassEq(RC);
Christian Konigf82901a2013-02-26 17:52:23 +00001223}
1224
1225/// \brief Make sure that we don't exeed the number of allowed scalars
1226void SITargetLowering::ensureSRegLimit(SelectionDAG &DAG, SDValue &Operand,
1227 unsigned RegClass,
1228 bool &ScalarSlotUsed) const {
1229
1230 // First map the operands register class to a destination class
1231 if (RegClass == AMDGPU::VSrc_32RegClassID)
1232 RegClass = AMDGPU::VReg_32RegClassID;
1233 else if (RegClass == AMDGPU::VSrc_64RegClassID)
1234 RegClass = AMDGPU::VReg_64RegClassID;
1235 else
1236 return;
1237
Alp Tokercb402912014-01-24 17:20:08 +00001238 // Nothing to do if they fit naturally
Christian Konigf82901a2013-02-26 17:52:23 +00001239 if (fitsRegClass(DAG, Operand, RegClass))
1240 return;
1241
1242 // If the scalar slot isn't used yet use it now
1243 if (!ScalarSlotUsed) {
1244 ScalarSlotUsed = true;
1245 return;
1246 }
1247
Matt Arsenault1408b602013-10-10 23:05:37 +00001248 // This is a conservative aproach. It is possible that we can't determine the
1249 // correct register class and copy too often, but better safe than sorry.
Christian Konigf82901a2013-02-26 17:52:23 +00001250 SDValue RC = DAG.getTargetConstant(RegClass, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001251 SDNode *Node = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, SDLoc(),
Christian Konigf82901a2013-02-26 17:52:23 +00001252 Operand.getValueType(), Operand, RC);
1253 Operand = SDValue(Node, 0);
1254}
1255
Tom Stellardacec99c2013-06-05 23:39:50 +00001256/// \returns true if \p Node's operands are different from the SDValue list
1257/// \p Ops
1258static bool isNodeChanged(const SDNode *Node, const std::vector<SDValue> &Ops) {
1259 for (unsigned i = 0, e = Node->getNumOperands(); i < e; ++i) {
1260 if (Ops[i].getNode() != Node->getOperand(i).getNode()) {
1261 return true;
1262 }
1263 }
1264 return false;
1265}
1266
Christian Konig8e06e2a2013-04-10 08:39:08 +00001267/// \brief Try to fold the Nodes operands into the Node
1268SDNode *SITargetLowering::foldOperands(MachineSDNode *Node,
1269 SelectionDAG &DAG) const {
Christian Konigf82901a2013-02-26 17:52:23 +00001270
1271 // Original encoding (either e32 or e64)
1272 int Opcode = Node->getMachineOpcode();
Bill Wendling37e9adb2013-06-07 20:28:55 +00001273 const SIInstrInfo *TII =
1274 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00001275 const MCInstrDesc *Desc = &TII->get(Opcode);
1276
1277 unsigned NumDefs = Desc->getNumDefs();
1278 unsigned NumOps = Desc->getNumOperands();
1279
Christian Konig3c145802013-03-27 09:12:59 +00001280 // Commuted opcode if available
1281 int OpcodeRev = Desc->isCommutable() ? TII->commuteOpcode(Opcode) : -1;
Craig Topper062a2ba2014-04-25 05:30:21 +00001282 const MCInstrDesc *DescRev = OpcodeRev == -1 ? nullptr : &TII->get(OpcodeRev);
Christian Konig3c145802013-03-27 09:12:59 +00001283
1284 assert(!DescRev || DescRev->getNumDefs() == NumDefs);
1285 assert(!DescRev || DescRev->getNumOperands() == NumOps);
1286
Christian Konige500e442013-02-26 17:52:47 +00001287 // e64 version if available, -1 otherwise
1288 int OpcodeE64 = AMDGPU::getVOPe64(Opcode);
Craig Topper062a2ba2014-04-25 05:30:21 +00001289 const MCInstrDesc *DescE64 = OpcodeE64 == -1 ? nullptr : &TII->get(OpcodeE64);
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001290 int InputModifiers[3] = {0};
Christian Konige500e442013-02-26 17:52:47 +00001291
1292 assert(!DescE64 || DescE64->getNumDefs() == NumDefs);
Christian Konige500e442013-02-26 17:52:47 +00001293
Christian Konigf82901a2013-02-26 17:52:23 +00001294 int32_t Immediate = Desc->getSize() == 4 ? 0 : -1;
1295 bool HaveVSrc = false, HaveSSrc = false;
1296
1297 // First figure out what we alread have in this instruction
1298 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1299 i != e && Op < NumOps; ++i, ++Op) {
1300
1301 unsigned RegClass = Desc->OpInfo[Op].RegClass;
1302 if (isVSrc(RegClass))
1303 HaveVSrc = true;
1304 else if (isSSrc(RegClass))
1305 HaveSSrc = true;
1306 else
1307 continue;
1308
1309 int32_t Imm = analyzeImmediate(Node->getOperand(i).getNode());
1310 if (Imm != -1 && Imm != 0) {
1311 // Literal immediate
1312 Immediate = Imm;
1313 }
1314 }
1315
1316 // If we neither have VSrc nor SSrc it makes no sense to continue
1317 if (!HaveVSrc && !HaveSSrc)
1318 return Node;
1319
1320 // No scalar allowed when we have both VSrc and SSrc
1321 bool ScalarSlotUsed = HaveVSrc && HaveSSrc;
1322
1323 // Second go over the operands and try to fold them
1324 std::vector<SDValue> Ops;
Christian Konige500e442013-02-26 17:52:47 +00001325 bool Promote2e64 = false;
Christian Konigf82901a2013-02-26 17:52:23 +00001326 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1327 i != e && Op < NumOps; ++i, ++Op) {
1328
1329 const SDValue &Operand = Node->getOperand(i);
1330 Ops.push_back(Operand);
1331
1332 // Already folded immediate ?
1333 if (isa<ConstantSDNode>(Operand.getNode()) ||
1334 isa<ConstantFPSDNode>(Operand.getNode()))
1335 continue;
1336
1337 // Is this a VSrc or SSrc operand ?
1338 unsigned RegClass = Desc->OpInfo[Op].RegClass;
Christian Konig8370dbb2013-03-26 14:04:17 +00001339 if (isVSrc(RegClass) || isSSrc(RegClass)) {
1340 // Try to fold the immediates
1341 if (!foldImm(Ops[i], Immediate, ScalarSlotUsed)) {
1342 // Folding didn't worked, make sure we don't hit the SReg limit
1343 ensureSRegLimit(DAG, Ops[i], RegClass, ScalarSlotUsed);
1344 }
1345 continue;
1346 }
Christian Konig6612ac32013-02-26 17:52:36 +00001347
Christian Konig3c145802013-03-27 09:12:59 +00001348 if (i == 1 && DescRev && fitsRegClass(DAG, Ops[0], RegClass)) {
Christian Konig6612ac32013-02-26 17:52:36 +00001349
Christian Konig8370dbb2013-03-26 14:04:17 +00001350 unsigned OtherRegClass = Desc->OpInfo[NumDefs].RegClass;
1351 assert(isVSrc(OtherRegClass) || isSSrc(OtherRegClass));
1352
1353 // Test if it makes sense to swap operands
1354 if (foldImm(Ops[1], Immediate, ScalarSlotUsed) ||
1355 (!fitsRegClass(DAG, Ops[1], RegClass) &&
1356 fitsRegClass(DAG, Ops[1], OtherRegClass))) {
Christian Konig6612ac32013-02-26 17:52:36 +00001357
1358 // Swap commutable operands
Matt Arsenault4be76e92014-04-07 16:44:26 +00001359 std::swap(Ops[0], Ops[1]);
Christian Konig3c145802013-03-27 09:12:59 +00001360
1361 Desc = DescRev;
Craig Topper062a2ba2014-04-25 05:30:21 +00001362 DescRev = nullptr;
Christian Konig8370dbb2013-03-26 14:04:17 +00001363 continue;
Christian Konig6612ac32013-02-26 17:52:36 +00001364 }
Christian Konig6612ac32013-02-26 17:52:36 +00001365 }
Christian Konigf82901a2013-02-26 17:52:23 +00001366
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001367 if (Immediate)
1368 continue;
1369
1370 if (DescE64) {
Christian Konig8370dbb2013-03-26 14:04:17 +00001371
1372 // Test if it makes sense to switch to e64 encoding
1373 unsigned OtherRegClass = DescE64->OpInfo[Op].RegClass;
1374 if (!isVSrc(OtherRegClass) && !isSSrc(OtherRegClass))
1375 continue;
1376
1377 int32_t TmpImm = -1;
1378 if (foldImm(Ops[i], TmpImm, ScalarSlotUsed) ||
1379 (!fitsRegClass(DAG, Ops[i], RegClass) &&
1380 fitsRegClass(DAG, Ops[1], OtherRegClass))) {
1381
1382 // Switch to e64 encoding
1383 Immediate = -1;
1384 Promote2e64 = true;
1385 Desc = DescE64;
Craig Topper062a2ba2014-04-25 05:30:21 +00001386 DescE64 = nullptr;
Christian Konig8370dbb2013-03-26 14:04:17 +00001387 }
Christian Konigf82901a2013-02-26 17:52:23 +00001388 }
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001389
1390 if (!DescE64 && !Promote2e64)
1391 continue;
1392 if (!Operand.isMachineOpcode())
1393 continue;
1394 if (Operand.getMachineOpcode() == AMDGPU::FNEG_SI) {
1395 Ops.pop_back();
1396 Ops.push_back(Operand.getOperand(0));
1397 InputModifiers[i] = 1;
1398 Promote2e64 = true;
1399 if (!DescE64)
1400 continue;
1401 Desc = DescE64;
1402 DescE64 = 0;
1403 }
1404 else if (Operand.getMachineOpcode() == AMDGPU::FABS_SI) {
1405 Ops.pop_back();
1406 Ops.push_back(Operand.getOperand(0));
1407 InputModifiers[i] = 2;
1408 Promote2e64 = true;
1409 if (!DescE64)
1410 continue;
1411 Desc = DescE64;
1412 DescE64 = 0;
1413 }
Christian Konigf82901a2013-02-26 17:52:23 +00001414 }
1415
Christian Konige500e442013-02-26 17:52:47 +00001416 if (Promote2e64) {
Vincent Lejeune94af31f2014-05-10 19:18:33 +00001417 std::vector<SDValue> OldOps(Ops);
1418 Ops.clear();
1419 for (unsigned i = 0; i < OldOps.size(); ++i) {
1420 // src_modifier
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001421 Ops.push_back(DAG.getTargetConstant(InputModifiers[i], MVT::i32));
Vincent Lejeune94af31f2014-05-10 19:18:33 +00001422 Ops.push_back(OldOps[i]);
1423 }
Christian Konige500e442013-02-26 17:52:47 +00001424 // Add the modifier flags while promoting
Vincent Lejeune94af31f2014-05-10 19:18:33 +00001425 for (unsigned i = 0; i < 2; ++i)
Christian Konige500e442013-02-26 17:52:47 +00001426 Ops.push_back(DAG.getTargetConstant(0, MVT::i32));
1427 }
1428
Christian Konigf82901a2013-02-26 17:52:23 +00001429 // Add optional chain and glue
1430 for (unsigned i = NumOps - NumDefs, e = Node->getNumOperands(); i < e; ++i)
1431 Ops.push_back(Node->getOperand(i));
1432
Tom Stellardb5a97002013-06-03 17:39:50 +00001433 // Nodes that have a glue result are not CSE'd by getMachineNode(), so in
1434 // this case a brand new node is always be created, even if the operands
1435 // are the same as before. So, manually check if anything has been changed.
Tom Stellardacec99c2013-06-05 23:39:50 +00001436 if (Desc->Opcode == Opcode && !isNodeChanged(Node, Ops)) {
1437 return Node;
Tom Stellardb5a97002013-06-03 17:39:50 +00001438 }
1439
Christian Konig3c145802013-03-27 09:12:59 +00001440 // Create a complete new instruction
Andrew Trickef9de2a2013-05-25 02:42:55 +00001441 return DAG.getMachineNode(Desc->Opcode, SDLoc(Node), Node->getVTList(), Ops);
Christian Konigd910b7d2013-02-26 17:52:16 +00001442}
Christian Konig8e06e2a2013-04-10 08:39:08 +00001443
1444/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00001445static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00001446 switch (Idx) {
1447 default: return 0;
1448 case AMDGPU::sub0: return 0;
1449 case AMDGPU::sub1: return 1;
1450 case AMDGPU::sub2: return 2;
1451 case AMDGPU::sub3: return 3;
1452 }
1453}
1454
1455/// \brief Adjust the writemask of MIMG instructions
1456void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
1457 SelectionDAG &DAG) const {
1458 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00001459 unsigned Lane = 0;
1460 unsigned OldDmask = Node->getConstantOperandVal(0);
1461 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001462
1463 // Try to figure out the used register components
1464 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
1465 I != E; ++I) {
1466
1467 // Abort if we can't understand the usage
1468 if (!I->isMachineOpcode() ||
1469 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
1470 return;
1471
Tom Stellard54774e52013-10-23 02:53:47 +00001472 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
1473 // Note that subregs are packed, i.e. Lane==0 is the first bit set
1474 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
1475 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00001476 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00001477
Tom Stellard54774e52013-10-23 02:53:47 +00001478 // Set which texture component corresponds to the lane.
1479 unsigned Comp;
1480 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
1481 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00001482 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00001483 Dmask &= ~(1 << Comp);
1484 }
1485
Christian Konig8e06e2a2013-04-10 08:39:08 +00001486 // Abort if we have more than one user per component
1487 if (Users[Lane])
1488 return;
1489
1490 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00001491 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001492 }
1493
Tom Stellard54774e52013-10-23 02:53:47 +00001494 // Abort if there's no change
1495 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00001496 return;
1497
1498 // Adjust the writemask in the node
1499 std::vector<SDValue> Ops;
Tom Stellard54774e52013-10-23 02:53:47 +00001500 Ops.push_back(DAG.getTargetConstant(NewDmask, MVT::i32));
Christian Konig8e06e2a2013-04-10 08:39:08 +00001501 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1502 Ops.push_back(Node->getOperand(i));
Craig Topper8c0b4d02014-04-28 05:57:50 +00001503 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00001504
Christian Konig8b1ed282013-04-10 08:39:16 +00001505 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00001506 // (if NewDmask has only one bit set...)
1507 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Christian Konig8b1ed282013-04-10 08:39:16 +00001508 SDValue RC = DAG.getTargetConstant(AMDGPU::VReg_32RegClassID, MVT::i32);
1509 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001510 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00001511 SDValue(Node, 0), RC);
1512 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
1513 return;
1514 }
1515
Christian Konig8e06e2a2013-04-10 08:39:08 +00001516 // Update the users of the node with the new indices
1517 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
1518
1519 SDNode *User = Users[i];
1520 if (!User)
1521 continue;
1522
1523 SDValue Op = DAG.getTargetConstant(Idx, MVT::i32);
1524 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
1525
1526 switch (Idx) {
1527 default: break;
1528 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
1529 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
1530 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
1531 }
1532 }
1533}
1534
1535/// \brief Fold the instructions after slecting them
1536SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
1537 SelectionDAG &DAG) const {
Tom Stellard16a9a202013-08-14 23:24:17 +00001538 const SIInstrInfo *TII =
1539 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
Tom Stellard0518ff82013-06-03 17:39:58 +00001540 Node = AdjustRegClass(Node, DAG);
Christian Konig8e06e2a2013-04-10 08:39:08 +00001541
Tom Stellard16a9a202013-08-14 23:24:17 +00001542 if (TII->isMIMG(Node->getMachineOpcode()))
Christian Konig8e06e2a2013-04-10 08:39:08 +00001543 adjustWritemask(Node, DAG);
1544
1545 return foldOperands(Node, DAG);
1546}
Christian Konig8b1ed282013-04-10 08:39:16 +00001547
1548/// \brief Assign the register class depending on the number of
1549/// bits set in the writemask
1550void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
1551 SDNode *Node) const {
Tom Stellard16a9a202013-08-14 23:24:17 +00001552 const SIInstrInfo *TII =
1553 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1554 if (!TII->isMIMG(MI->getOpcode()))
Christian Konig8b1ed282013-04-10 08:39:16 +00001555 return;
1556
1557 unsigned VReg = MI->getOperand(0).getReg();
1558 unsigned Writemask = MI->getOperand(1).getImm();
1559 unsigned BitsSet = 0;
1560 for (unsigned i = 0; i < 4; ++i)
1561 BitsSet += Writemask & (1 << i) ? 1 : 0;
1562
1563 const TargetRegisterClass *RC;
1564 switch (BitsSet) {
1565 default: return;
1566 case 1: RC = &AMDGPU::VReg_32RegClass; break;
1567 case 2: RC = &AMDGPU::VReg_64RegClass; break;
1568 case 3: RC = &AMDGPU::VReg_96RegClass; break;
1569 }
1570
Tom Stellard682bfbc2013-10-10 17:11:24 +00001571 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
1572 MI->setDesc(TII->get(NewOpcode));
Christian Konig8b1ed282013-04-10 08:39:16 +00001573 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
1574 MRI.setRegClass(VReg, RC);
1575}
Tom Stellard0518ff82013-06-03 17:39:58 +00001576
1577MachineSDNode *SITargetLowering::AdjustRegClass(MachineSDNode *N,
1578 SelectionDAG &DAG) const {
1579
1580 SDLoc DL(N);
1581 unsigned NewOpcode = N->getMachineOpcode();
1582
1583 switch (N->getMachineOpcode()) {
1584 default: return N;
Tom Stellard0518ff82013-06-03 17:39:58 +00001585 case AMDGPU::S_LOAD_DWORD_IMM:
1586 NewOpcode = AMDGPU::BUFFER_LOAD_DWORD_ADDR64;
1587 // Fall-through
1588 case AMDGPU::S_LOAD_DWORDX2_SGPR:
1589 if (NewOpcode == N->getMachineOpcode()) {
1590 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64;
1591 }
1592 // Fall-through
1593 case AMDGPU::S_LOAD_DWORDX4_IMM:
1594 case AMDGPU::S_LOAD_DWORDX4_SGPR: {
1595 if (NewOpcode == N->getMachineOpcode()) {
1596 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64;
1597 }
1598 if (fitsRegClass(DAG, N->getOperand(0), AMDGPU::SReg_64RegClassID)) {
1599 return N;
1600 }
1601 ConstantSDNode *Offset = cast<ConstantSDNode>(N->getOperand(1));
1602 SDValue Ops[] = {
1603 SDValue(DAG.getMachineNode(AMDGPU::SI_ADDR64_RSRC, DL, MVT::i128,
1604 DAG.getConstant(0, MVT::i64)), 0),
1605 N->getOperand(0),
1606 DAG.getConstant(Offset->getSExtValue() << 2, MVT::i32)
1607 };
1608 return DAG.getMachineNode(NewOpcode, DL, N->getVTList(), Ops);
1609 }
1610 }
1611}
Tom Stellard94593ee2013-06-03 17:40:18 +00001612
1613SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
1614 const TargetRegisterClass *RC,
1615 unsigned Reg, EVT VT) const {
1616 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
1617
1618 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
1619 cast<RegisterSDNode>(VReg)->getReg(), VT);
1620}