blob: 235665ab89e006e81a27946f835fc6babc0a8668 [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) {
Christian Koniga8811792013-02-16 11:28:30 +000032 addRegisterClass(MVT::i1, &AMDGPU::SReg_64RegClass);
Tom Stellard2f7cdda2013-08-06 23:08:28 +000033 addRegisterClass(MVT::i64, &AMDGPU::VSrc_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 Stellard2f7cdda2013-08-06 23:08:28 +000038 addRegisterClass(MVT::i32, &AMDGPU::VSrc_32RegClass);
39 addRegisterClass(MVT::f32, &AMDGPU::VSrc_32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000040
Tom Stellard2f7cdda2013-08-06 23:08:28 +000041 addRegisterClass(MVT::f64, &AMDGPU::VSrc_64RegClass);
42 addRegisterClass(MVT::v2i32, &AMDGPU::VSrc_64RegClass);
43 addRegisterClass(MVT::v2f32, &AMDGPU::VSrc_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000044
Tom Stellard538ceeb2013-02-07 17:02:09 +000045 addRegisterClass(MVT::v4i32, &AMDGPU::VReg_128RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000046 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
Tom Stellard754f80f2013-04-05 23:31:51 +000047 addRegisterClass(MVT::i128, &AMDGPU::SReg_128RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000048
Tom Stellard538ceeb2013-02-07 17:02:09 +000049 addRegisterClass(MVT::v8i32, &AMDGPU::VReg_256RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000050 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
51
Tom Stellard538ceeb2013-02-07 17:02:09 +000052 addRegisterClass(MVT::v16i32, &AMDGPU::VReg_512RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000053 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000054
55 computeRegisterProperties();
56
Tom Stellardc0845332013-11-22 23:07:58 +000057 // Condition Codes
58 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
59 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
60 setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
61 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
62 setCondCodeAction(ISD::SETULE, MVT::f32, Expand);
63 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
64
65 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
66 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
67 setCondCodeAction(ISD::SETUGE, MVT::f64, Expand);
68 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
69 setCondCodeAction(ISD::SETULE, MVT::f64, Expand);
70 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
71
Christian Konig2989ffc2013-03-18 11:34:16 +000072 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
73 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
74 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
75 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
76
Tom Stellard75aadc22012-12-11 21:25:42 +000077 setOperationAction(ISD::ADD, MVT::i32, Legal);
Matt Arsenaulte8d21462013-11-18 20:09:40 +000078 setOperationAction(ISD::ADDC, MVT::i32, Legal);
79 setOperationAction(ISD::ADDE, MVT::i32, Legal);
Aaron Watrydaabb202013-06-25 13:55:52 +000080
Tom Stellard9fa17912013-08-14 23:24:45 +000081 setOperationAction(ISD::BITCAST, MVT::i128, Legal);
82
Tom Stellard35bb18c2013-08-26 15:06:04 +000083 // We need to custom lower vector stores from local memory
84 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
85 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellardaf775432013-10-23 00:44:32 +000086 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
87 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
88
89 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
90 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
Tom Stellard35bb18c2013-08-26 15:06:04 +000091
Tom Stellard81d871d2013-11-13 23:36:50 +000092 // We need to custom lower loads/stores from private memory
93 setOperationAction(ISD::LOAD, MVT::i32, Custom);
94 setOperationAction(ISD::LOAD, MVT::i64, Custom);
95 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
96 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellard967bf582014-02-13 23:34:15 +000097 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +000098
Tom Stellard1c8788e2014-03-07 20:12:33 +000099 setOperationAction(ISD::STORE, MVT::i1, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +0000100 setOperationAction(ISD::STORE, MVT::i32, Custom);
101 setOperationAction(ISD::STORE, MVT::i64, Custom);
102 setOperationAction(ISD::STORE, MVT::i128, Custom);
103 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
104 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
105
Tom Stellard0ec134f2014-02-04 17:18:40 +0000106 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Tom Stellardda99c6e2014-03-24 16:07:30 +0000107 setOperationAction(ISD::SELECT, MVT::f64, Promote);
108 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
Tom Stellard81d871d2013-11-13 23:36:50 +0000109
Tom Stellard75aadc22012-12-11 21:25:42 +0000110 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
111 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
112
113 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
Tom Stellard754f80f2013-04-05 23:31:51 +0000114
Tom Stellard83747202013-07-18 21:43:53 +0000115 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
116 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
117
Tom Stellardaf775432013-10-23 00:44:32 +0000118 setOperationAction(ISD::ANY_EXTEND, MVT::i64, Custom);
Tom Stellard046039e2013-06-03 17:40:03 +0000119 setOperationAction(ISD::SIGN_EXTEND, MVT::i64, Custom);
Tom Stellard98f675a2013-08-01 15:23:26 +0000120 setOperationAction(ISD::ZERO_EXTEND, MVT::i64, Custom);
Tom Stellard046039e2013-06-03 17:40:03 +0000121
Matt Arsenault4e466652014-04-16 01:41:30 +0000122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Custom);
123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
125
126 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Custom);
127 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
128 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
129
130 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Custom);
131 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
132 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
133
134 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Custom);
135
136 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
137
Tom Stellard94593ee2013-06-03 17:40:18 +0000138 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Tom Stellard9fa17912013-08-14 23:24:45 +0000139 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
140 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom);
141 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
Tom Stellard94593ee2013-06-03 17:40:18 +0000142
Tom Stellardafcf12f2013-09-12 02:55:14 +0000143 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
144
Matt Arsenault470acd82014-04-15 22:28:39 +0000145 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Tom Stellarde9373602014-01-22 19:24:14 +0000146 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Custom);
147 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Custom);
Matt Arsenault470acd82014-04-15 22:28:39 +0000148 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000149 setLoadExtAction(ISD::SEXTLOAD, MVT::v8i16, Expand);
150 setLoadExtAction(ISD::SEXTLOAD, MVT::v16i16, Expand);
Tom Stellard31209cc2013-07-15 19:00:09 +0000151
Matt Arsenault470acd82014-04-15 22:28:39 +0000152 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
153 setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Custom);
154 setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Custom);
155 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Expand);
156
157 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
Tom Stellarde9373602014-01-22 19:24:14 +0000158 setLoadExtAction(ISD::EXTLOAD, MVT::i8, Custom);
159 setLoadExtAction(ISD::EXTLOAD, MVT::i16, Custom);
160 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Expand);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000161 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000162
Tom Stellarde9373602014-01-22 19:24:14 +0000163 setTruncStoreAction(MVT::i32, MVT::i8, Custom);
164 setTruncStoreAction(MVT::i32, MVT::i16, Custom);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000165 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Matt Arsenault6f243792013-09-05 19:41:10 +0000166 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000167 setTruncStoreAction(MVT::i128, MVT::i64, Expand);
168 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
169 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000170
Matt Arsenault470acd82014-04-15 22:28:39 +0000171 setOperationAction(ISD::LOAD, MVT::i1, Custom);
172
Tom Stellardfd155822013-08-26 15:05:36 +0000173 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Tom Stellard04c0e982014-01-22 19:24:21 +0000174 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000175 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
Michel Danzer49812b52013-07-10 16:37:07 +0000176
Tom Stellard967bf582014-02-13 23:34:15 +0000177 // We only support LOAD/STORE and vector manipulation ops for vectors
178 // with > 4 elements.
179 MVT VecTypes[] = {
Tom Stellardd61a1c32014-02-28 21:36:37 +0000180 MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32
Tom Stellard967bf582014-02-13 23:34:15 +0000181 };
182
183 const size_t NumVecTypes = array_lengthof(VecTypes);
184 for (unsigned Type = 0; Type < NumVecTypes; ++Type) {
185 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
186 switch(Op) {
187 case ISD::LOAD:
188 case ISD::STORE:
189 case ISD::BUILD_VECTOR:
190 case ISD::BITCAST:
191 case ISD::EXTRACT_VECTOR_ELT:
192 case ISD::INSERT_VECTOR_ELT:
193 case ISD::CONCAT_VECTORS:
194 case ISD::INSERT_SUBVECTOR:
195 case ISD::EXTRACT_SUBVECTOR:
196 break;
197 default:
198 setOperationAction(Op, VecTypes[Type], Expand);
199 break;
200 }
201 }
202 }
203
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000204 for (int I = MVT::v1f64; I <= MVT::v8f64; ++I) {
205 MVT::SimpleValueType VT = static_cast<MVT::SimpleValueType>(I);
Matt Arsenaulta81aee82014-02-24 21:16:50 +0000206 setOperationAction(ISD::FTRUNC, VT, Expand);
207 setOperationAction(ISD::FCEIL, VT, Expand);
208 setOperationAction(ISD::FFLOOR, VT, Expand);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000209 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000210
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000211 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
212 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
213 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
214 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
215 }
216
217 setTargetDAGCombine(ISD::SELECT_CC);
Tom Stellard75aadc22012-12-11 21:25:42 +0000218 setTargetDAGCombine(ISD::SETCC);
Michel Danzerf52a6722013-03-08 10:58:01 +0000219
Christian Konigeecebd02013-03-26 14:04:02 +0000220 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000221}
222
Tom Stellard0125f2a2013-06-25 02:39:35 +0000223//===----------------------------------------------------------------------===//
224// TargetLowering queries
225//===----------------------------------------------------------------------===//
226
227bool SITargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
Matt Arsenault25793a32014-02-05 23:15:53 +0000228 unsigned AddrSpace,
Tom Stellard0125f2a2013-06-25 02:39:35 +0000229 bool *IsFast) const {
230 // XXX: This depends on the address space and also we may want to revist
231 // the alignment values we specify in the DataLayout.
Tom Stellard81d871d2013-11-13 23:36:50 +0000232 if (!VT.isSimple() || VT == MVT::Other)
233 return false;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000234 return VT.bitsGT(MVT::i32);
235}
236
Matt Arsenaultf751d622014-03-31 20:54:58 +0000237bool SITargetLowering::shouldSplitVectorType(EVT VT) const {
238 return VT.getScalarType().bitsLE(MVT::i16);
Tom Stellardd86003e2013-08-14 23:25:00 +0000239}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000240
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000241bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
242 Type *Ty) const {
243 const SIInstrInfo *TII =
244 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
245 return TII->isInlineConstant(Imm);
246}
247
Tom Stellardaf775432013-10-23 00:44:32 +0000248SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
Tom Stellard94593ee2013-06-03 17:40:18 +0000249 SDLoc DL, SDValue Chain,
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000250 unsigned Offset, bool Signed) const {
Tom Stellard94593ee2013-06-03 17:40:18 +0000251 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
252 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
253 AMDGPUAS::CONSTANT_ADDRESS);
Tom Stellard94593ee2013-06-03 17:40:18 +0000254 SDValue BasePtr = DAG.getCopyFromReg(Chain, DL,
255 MRI.getLiveInVirtReg(AMDGPU::SGPR0_SGPR1), MVT::i64);
256 SDValue Ptr = DAG.getNode(ISD::ADD, DL, MVT::i64, BasePtr,
257 DAG.getConstant(Offset, MVT::i64));
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000258 return DAG.getExtLoad(Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD, DL, VT, Chain, Ptr,
Tom Stellardaf775432013-10-23 00:44:32 +0000259 MachinePointerInfo(UndefValue::get(PtrTy)), MemVT,
260 false, false, MemVT.getSizeInBits() >> 3);
Tom Stellard94593ee2013-06-03 17:40:18 +0000261
262}
263
Christian Konig2c8f6d52013-03-07 09:03:52 +0000264SDValue SITargetLowering::LowerFormalArguments(
265 SDValue Chain,
266 CallingConv::ID CallConv,
267 bool isVarArg,
268 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000269 SDLoc DL, SelectionDAG &DAG,
Christian Konig2c8f6d52013-03-07 09:03:52 +0000270 SmallVectorImpl<SDValue> &InVals) const {
271
272 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
273
274 MachineFunction &MF = DAG.getMachineFunction();
275 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000276 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000277
278 assert(CallConv == CallingConv::C);
279
280 SmallVector<ISD::InputArg, 16> Splits;
Christian Konig99ee0f42013-03-07 09:04:14 +0000281 uint32_t Skipped = 0;
282
283 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000284 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000285
286 // First check if it's a PS input addr
Vincent Lejeuned6236442013-10-13 17:56:16 +0000287 if (Info->ShaderType == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
288 !Arg.Flags.isByVal()) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000289
290 assert((PSInputNum <= 15) && "Too many PS inputs!");
291
292 if (!Arg.Used) {
293 // We can savely skip PS inputs
294 Skipped |= 1 << i;
295 ++PSInputNum;
296 continue;
297 }
298
299 Info->PSInputAddr |= 1 << PSInputNum++;
300 }
301
302 // Second split vertices into their elements
Tom Stellarded882c22013-06-03 17:40:11 +0000303 if (Info->ShaderType != ShaderType::COMPUTE && Arg.VT.isVector()) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000304 ISD::InputArg NewArg = Arg;
305 NewArg.Flags.setSplit();
306 NewArg.VT = Arg.VT.getVectorElementType();
307
308 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
309 // three or five element vertex only needs three or five registers,
310 // NOT four or eigth.
311 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
312 unsigned NumElements = ParamType->getVectorNumElements();
313
314 for (unsigned j = 0; j != NumElements; ++j) {
315 Splits.push_back(NewArg);
316 NewArg.PartOffset += NewArg.VT.getStoreSize();
317 }
318
Tom Stellardaf775432013-10-23 00:44:32 +0000319 } else if (Info->ShaderType != ShaderType::COMPUTE) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000320 Splits.push_back(Arg);
321 }
322 }
323
324 SmallVector<CCValAssign, 16> ArgLocs;
325 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
326 getTargetMachine(), ArgLocs, *DAG.getContext());
327
Christian Konig99ee0f42013-03-07 09:04:14 +0000328 // At least one interpolation mode must be enabled or else the GPU will hang.
329 if (Info->ShaderType == ShaderType::PIXEL && (Info->PSInputAddr & 0x7F) == 0) {
330 Info->PSInputAddr |= 1;
331 CCInfo.AllocateReg(AMDGPU::VGPR0);
332 CCInfo.AllocateReg(AMDGPU::VGPR1);
333 }
334
Tom Stellarded882c22013-06-03 17:40:11 +0000335 // The pointer to the list of arguments is stored in SGPR0, SGPR1
336 if (Info->ShaderType == ShaderType::COMPUTE) {
337 CCInfo.AllocateReg(AMDGPU::SGPR0);
338 CCInfo.AllocateReg(AMDGPU::SGPR1);
Tom Stellard94593ee2013-06-03 17:40:18 +0000339 MF.addLiveIn(AMDGPU::SGPR0_SGPR1, &AMDGPU::SReg_64RegClass);
Tom Stellarded882c22013-06-03 17:40:11 +0000340 }
341
Tom Stellardaf775432013-10-23 00:44:32 +0000342 if (Info->ShaderType == ShaderType::COMPUTE) {
343 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
344 Splits);
345 }
346
Christian Konig2c8f6d52013-03-07 09:03:52 +0000347 AnalyzeFormalArguments(CCInfo, Splits);
348
349 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
350
Christian Konigb7be72d2013-05-17 09:46:48 +0000351 const ISD::InputArg &Arg = Ins[i];
Christian Konig99ee0f42013-03-07 09:04:14 +0000352 if (Skipped & (1 << i)) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000353 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000354 continue;
355 }
356
Christian Konig2c8f6d52013-03-07 09:03:52 +0000357 CCValAssign &VA = ArgLocs[ArgIdx++];
Tom Stellarded882c22013-06-03 17:40:11 +0000358 EVT VT = VA.getLocVT();
359
360 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000361 VT = Ins[i].VT;
362 EVT MemVT = Splits[i].VT;
Tom Stellard94593ee2013-06-03 17:40:18 +0000363 // The first 36 bytes of the input buffer contains information about
364 // thread group and global sizes.
Tom Stellardaf775432013-10-23 00:44:32 +0000365 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, DAG.getRoot(),
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000366 36 + VA.getLocMemOffset(),
367 Ins[i].Flags.isSExt());
Tom Stellarded882c22013-06-03 17:40:11 +0000368 InVals.push_back(Arg);
369 continue;
370 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000371 assert(VA.isRegLoc() && "Parameter must be in a register!");
372
373 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000374
375 if (VT == MVT::i64) {
376 // For now assume it is a pointer
377 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
378 &AMDGPU::SReg_64RegClass);
379 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
380 InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
381 continue;
382 }
383
384 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
385
386 Reg = MF.addLiveIn(Reg, RC);
387 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
388
Christian Konig2c8f6d52013-03-07 09:03:52 +0000389 if (Arg.VT.isVector()) {
390
391 // Build a vector from the registers
392 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
393 unsigned NumElements = ParamType->getVectorNumElements();
394
395 SmallVector<SDValue, 4> Regs;
396 Regs.push_back(Val);
397 for (unsigned j = 1; j != NumElements; ++j) {
398 Reg = ArgLocs[ArgIdx++].getLocReg();
399 Reg = MF.addLiveIn(Reg, RC);
400 Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
401 }
402
403 // Fill up the missing vector elements
404 NumElements = Arg.VT.getVectorNumElements() - NumElements;
405 for (unsigned j = 0; j != NumElements; ++j)
406 Regs.push_back(DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000407
Christian Konig2c8f6d52013-03-07 09:03:52 +0000408 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT,
409 Regs.data(), Regs.size()));
410 continue;
411 }
412
413 InVals.push_back(Val);
414 }
415 return Chain;
416}
417
Tom Stellard75aadc22012-12-11 21:25:42 +0000418MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
419 MachineInstr * MI, MachineBasicBlock * BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000420
Tom Stellard556d9aa2013-06-03 17:39:37 +0000421 MachineBasicBlock::iterator I = *MI;
422
Tom Stellard75aadc22012-12-11 21:25:42 +0000423 switch (MI->getOpcode()) {
424 default:
425 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
426 case AMDGPU::BRANCH: return BB;
Tom Stellard556d9aa2013-06-03 17:39:37 +0000427 case AMDGPU::SI_ADDR64_RSRC: {
Bill Wendling37e9adb2013-06-07 20:28:55 +0000428 const SIInstrInfo *TII =
429 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
Tom Stellard556d9aa2013-06-03 17:39:37 +0000430 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
431 unsigned SuperReg = MI->getOperand(0).getReg();
Tom Stellarddef38c52014-03-21 15:51:53 +0000432 unsigned SubRegLo = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass);
433 unsigned SubRegHi = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass);
434 unsigned SubRegHiHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
435 unsigned SubRegHiLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
Tom Stellard556d9aa2013-06-03 17:39:37 +0000436 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B64), SubRegLo)
437 .addOperand(MI->getOperand(1));
438 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiLo)
439 .addImm(0);
440 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiHi)
Tom Stellard15834092014-03-21 15:51:57 +0000441 .addImm(AMDGPU::RSRC_DATA_FORMAT >> 32);
Tom Stellard556d9aa2013-06-03 17:39:37 +0000442 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SubRegHi)
443 .addReg(SubRegHiLo)
444 .addImm(AMDGPU::sub0)
445 .addReg(SubRegHiHi)
446 .addImm(AMDGPU::sub1);
447 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SuperReg)
448 .addReg(SubRegLo)
449 .addImm(AMDGPU::sub0_sub1)
450 .addReg(SubRegHi)
451 .addImm(AMDGPU::sub2_sub3);
452 MI->eraseFromParent();
453 break;
454 }
Tom Stellard2a6a61052013-07-12 18:15:08 +0000455 case AMDGPU::V_SUB_F64: {
456 const SIInstrInfo *TII =
457 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
458 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F64),
459 MI->getOperand(0).getReg())
460 .addReg(MI->getOperand(1).getReg())
461 .addReg(MI->getOperand(2).getReg())
462 .addImm(0) /* src2 */
463 .addImm(0) /* ABS */
464 .addImm(0) /* CLAMP */
465 .addImm(0) /* OMOD */
466 .addImm(2); /* NEG */
467 MI->eraseFromParent();
468 break;
469 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000470 case AMDGPU::SI_RegisterStorePseudo: {
471 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
472 const SIInstrInfo *TII =
473 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
474 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
475 MachineInstrBuilder MIB =
476 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::SI_RegisterStore),
477 Reg);
478 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
479 MIB.addOperand(MI->getOperand(i));
480
481 MI->eraseFromParent();
482 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000483 }
484 return BB;
485}
486
Matt Arsenault758659232013-05-18 00:21:46 +0000487EVT SITargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +0000488 if (!VT.isVector()) {
489 return MVT::i1;
490 }
491 return MVT::getVectorVT(MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +0000492}
493
Christian Konig082a14a2013-03-18 11:34:05 +0000494MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const {
495 return MVT::i32;
496}
497
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +0000498bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
499 VT = VT.getScalarType();
500
501 if (!VT.isSimple())
502 return false;
503
504 switch (VT.getSimpleVT().SimpleTy) {
505 case MVT::f32:
506 return false; /* There is V_MAD_F32 for f32 */
507 case MVT::f64:
508 return true;
509 default:
510 break;
511 }
512
513 return false;
514}
515
Tom Stellard75aadc22012-12-11 21:25:42 +0000516//===----------------------------------------------------------------------===//
517// Custom DAG Lowering Operations
518//===----------------------------------------------------------------------===//
519
520SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Michel Danzer49812b52013-07-10 16:37:07 +0000521 MachineFunction &MF = DAG.getMachineFunction();
522 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellard75aadc22012-12-11 21:25:42 +0000523 switch (Op.getOpcode()) {
524 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +0000525 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +0000526 case ISD::LOAD: {
527 LoadSDNode *Load = dyn_cast<LoadSDNode>(Op);
Tom Stellard80be9652014-02-13 23:34:10 +0000528 if (Op.getValueType().isVector() &&
529 (Load->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
530 Load->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS ||
531 (Load->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
532 Op.getValueType().getVectorNumElements() > 4))) {
Tom Stellard35bb18c2013-08-26 15:06:04 +0000533 SDValue MergedValues[2] = {
534 SplitVectorLoad(Op, DAG),
535 Load->getChain()
536 };
537 return DAG.getMergeValues(MergedValues, 2, SDLoc(Op));
538 } else {
Tom Stellard81d871d2013-11-13 23:36:50 +0000539 return LowerLOAD(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +0000540 }
541 }
Tom Stellardaf775432013-10-23 00:44:32 +0000542
Tom Stellard0ec134f2014-02-04 17:18:40 +0000543 case ISD::SELECT: return LowerSELECT(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000544 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Tom Stellard046039e2013-06-03 17:40:03 +0000545 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +0000546 case ISD::STORE: return LowerSTORE(Op, DAG);
Tom Stellardaf775432013-10-23 00:44:32 +0000547 case ISD::ANY_EXTEND: // Fall-through
Tom Stellard98f675a2013-08-01 15:23:26 +0000548 case ISD::ZERO_EXTEND: return LowerZERO_EXTEND(Op, DAG);
Michel Danzer49812b52013-07-10 16:37:07 +0000549 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +0000550 case ISD::INTRINSIC_WO_CHAIN: {
551 unsigned IntrinsicID =
552 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
553 EVT VT = Op.getValueType();
554 SDLoc DL(Op);
555 //XXX: Hardcoded we only use two to store the pointer to the parameters.
556 unsigned NumUserSGPRs = 2;
557 switch (IntrinsicID) {
558 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
559 case Intrinsic::r600_read_ngroups_x:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000560 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 0, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000561 case Intrinsic::r600_read_ngroups_y:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000562 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 4, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000563 case Intrinsic::r600_read_ngroups_z:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000564 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 8, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000565 case Intrinsic::r600_read_global_size_x:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000566 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 12, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000567 case Intrinsic::r600_read_global_size_y:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000568 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 16, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000569 case Intrinsic::r600_read_global_size_z:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000570 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 20, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000571 case Intrinsic::r600_read_local_size_x:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000572 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 24, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000573 case Intrinsic::r600_read_local_size_y:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000574 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 28, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000575 case Intrinsic::r600_read_local_size_z:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000576 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 32, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000577 case Intrinsic::r600_read_tgid_x:
578 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
579 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 0), VT);
580 case Intrinsic::r600_read_tgid_y:
581 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
582 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 1), VT);
583 case Intrinsic::r600_read_tgid_z:
584 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
585 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 2), VT);
586 case Intrinsic::r600_read_tidig_x:
587 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
588 AMDGPU::VGPR0, VT);
589 case Intrinsic::r600_read_tidig_y:
590 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
591 AMDGPU::VGPR1, VT);
592 case Intrinsic::r600_read_tidig_z:
593 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
594 AMDGPU::VGPR2, VT);
Tom Stellard9fa17912013-08-14 23:24:45 +0000595 case AMDGPUIntrinsic::SI_load_const: {
596 SDValue Ops [] = {
597 ResourceDescriptorToi128(Op.getOperand(1), DAG),
598 Op.getOperand(2)
599 };
Tom Stellard94593ee2013-06-03 17:40:18 +0000600
Benjamin Kramera8eecee2013-08-16 14:48:09 +0000601 MachineMemOperand *MMO = MF.getMachineMemOperand(
602 MachinePointerInfo(),
603 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
604 VT.getSizeInBits() / 8, 4);
Tom Stellard9fa17912013-08-14 23:24:45 +0000605 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
606 Op->getVTList(), Ops, 2, VT, MMO);
607 }
608 case AMDGPUIntrinsic::SI_sample:
609 return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG);
610 case AMDGPUIntrinsic::SI_sampleb:
611 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG);
612 case AMDGPUIntrinsic::SI_sampled:
613 return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG);
614 case AMDGPUIntrinsic::SI_samplel:
615 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG);
616 case AMDGPUIntrinsic::SI_vs_load_input:
617 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
618 ResourceDescriptorToi128(Op.getOperand(1), DAG),
619 Op.getOperand(2),
620 Op.getOperand(3));
Tom Stellard94593ee2013-06-03 17:40:18 +0000621 }
622 }
Tom Stellardafcf12f2013-09-12 02:55:14 +0000623
624 case ISD::INTRINSIC_VOID:
625 SDValue Chain = Op.getOperand(0);
626 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
627
628 switch (IntrinsicID) {
629 case AMDGPUIntrinsic::SI_tbuffer_store: {
630 SDLoc DL(Op);
631 SDValue Ops [] = {
632 Chain,
633 ResourceDescriptorToi128(Op.getOperand(2), DAG),
634 Op.getOperand(3),
635 Op.getOperand(4),
636 Op.getOperand(5),
637 Op.getOperand(6),
638 Op.getOperand(7),
639 Op.getOperand(8),
640 Op.getOperand(9),
641 Op.getOperand(10),
642 Op.getOperand(11),
643 Op.getOperand(12),
644 Op.getOperand(13),
645 Op.getOperand(14)
646 };
647 EVT VT = Op.getOperand(3).getValueType();
648
649 MachineMemOperand *MMO = MF.getMachineMemOperand(
650 MachinePointerInfo(),
651 MachineMemOperand::MOStore,
652 VT.getSizeInBits() / 8, 4);
653 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
654 Op->getVTList(), Ops,
655 sizeof(Ops)/sizeof(Ops[0]), VT, MMO);
656 }
657 default:
658 break;
659 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000660 }
661 return SDValue();
662}
663
Tom Stellardf8794352012-12-19 22:10:31 +0000664/// \brief Helper function for LowerBRCOND
665static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000666
Tom Stellardf8794352012-12-19 22:10:31 +0000667 SDNode *Parent = Value.getNode();
668 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
669 I != E; ++I) {
670
671 if (I.getUse().get() != Value)
672 continue;
673
674 if (I->getOpcode() == Opcode)
675 return *I;
676 }
677 return 0;
678}
679
680/// This transforms the control flow intrinsics to get the branch destination as
681/// last parameter, also switches branch target with BR if the need arise
682SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
683 SelectionDAG &DAG) const {
684
Andrew Trickef9de2a2013-05-25 02:42:55 +0000685 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +0000686
687 SDNode *Intr = BRCOND.getOperand(1).getNode();
688 SDValue Target = BRCOND.getOperand(2);
689 SDNode *BR = 0;
690
691 if (Intr->getOpcode() == ISD::SETCC) {
692 // As long as we negate the condition everything is fine
693 SDNode *SetCC = Intr;
694 assert(SetCC->getConstantOperandVal(1) == 1);
NAKAMURA Takumi458a8272013-01-07 11:14:44 +0000695 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
696 ISD::SETNE);
Tom Stellardf8794352012-12-19 22:10:31 +0000697 Intr = SetCC->getOperand(0).getNode();
698
699 } else {
700 // Get the target from BR if we don't negate the condition
701 BR = findUser(BRCOND, ISD::BR);
702 Target = BR->getOperand(1);
703 }
704
705 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
706
707 // Build the result and
708 SmallVector<EVT, 4> Res;
709 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
710 Res.push_back(Intr->getValueType(i));
711
712 // operands of the new intrinsic call
713 SmallVector<SDValue, 4> Ops;
714 Ops.push_back(BRCOND.getOperand(0));
715 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
716 Ops.push_back(Intr->getOperand(i));
717 Ops.push_back(Target);
718
719 // build the new intrinsic call
720 SDNode *Result = DAG.getNode(
721 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topperabb4ac72014-04-16 06:10:51 +0000722 DAG.getVTList(Res), Ops.data(), Ops.size()).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +0000723
724 if (BR) {
725 // Give the branch instruction our target
726 SDValue Ops[] = {
727 BR->getOperand(0),
728 BRCOND.getOperand(2)
729 };
730 DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2);
731 }
732
733 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
734
735 // Copy the intrinsic results to registers
736 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
737 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
738 if (!CopyToReg)
739 continue;
740
741 Chain = DAG.getCopyToReg(
742 Chain, DL,
743 CopyToReg->getOperand(1),
744 SDValue(Result, i - 1),
745 SDValue());
746
747 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
748 }
749
750 // Remove the old intrinsic from the chain
751 DAG.ReplaceAllUsesOfValueWith(
752 SDValue(Intr, Intr->getNumValues() - 1),
753 Intr->getOperand(0));
754
755 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +0000756}
757
Tom Stellard81d871d2013-11-13 23:36:50 +0000758SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
759 SDLoc DL(Op);
760 LoadSDNode *Load = cast<LoadSDNode>(Op);
Tom Stellarde9373602014-01-22 19:24:14 +0000761 SDValue Ret = AMDGPUTargetLowering::LowerLOAD(Op, DAG);
762 SDValue MergedValues[2];
763 MergedValues[1] = Load->getChain();
764 if (Ret.getNode()) {
765 MergedValues[0] = Ret;
766 return DAG.getMergeValues(MergedValues, 2, DL);
767 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000768
Tom Stellarde9373602014-01-22 19:24:14 +0000769 if (Load->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
Tom Stellard81d871d2013-11-13 23:36:50 +0000770 return SDValue();
Tom Stellarde9373602014-01-22 19:24:14 +0000771 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000772
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000773 EVT MemVT = Load->getMemoryVT();
774
775 assert(!MemVT.isVector() && "Private loads should be scalarized");
776 assert(!MemVT.isFloatingPoint() && "FP loads should be promoted to int");
777
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000778 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Load->getBasePtr(),
Tom Stellard81d871d2013-11-13 23:36:50 +0000779 DAG.getConstant(2, MVT::i32));
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000780 Ret = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
Tom Stellarde9373602014-01-22 19:24:14 +0000781 Load->getChain(), Ptr,
782 DAG.getTargetConstant(0, MVT::i32),
783 Op.getOperand(2));
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000784 if (MemVT.getSizeInBits() == 64) {
785 SDValue IncPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
786 DAG.getConstant(1, MVT::i32));
787
788 SDValue LoadUpper = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
789 Load->getChain(), IncPtr,
790 DAG.getTargetConstant(0, MVT::i32),
791 Op.getOperand(2));
792
793 Ret = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ret, LoadUpper);
794 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000795
Tom Stellarde9373602014-01-22 19:24:14 +0000796 MergedValues[0] = Ret;
Tom Stellard81d871d2013-11-13 23:36:50 +0000797 return DAG.getMergeValues(MergedValues, 2, DL);
798
799}
800
Tom Stellard9fa17912013-08-14 23:24:45 +0000801SDValue SITargetLowering::ResourceDescriptorToi128(SDValue Op,
802 SelectionDAG &DAG) const {
803
804 if (Op.getValueType() == MVT::i128) {
805 return Op;
806 }
807
808 assert(Op.getOpcode() == ISD::UNDEF);
809
810 return DAG.getNode(ISD::BUILD_PAIR, SDLoc(Op), MVT::i128,
811 DAG.getConstant(0, MVT::i64),
812 DAG.getConstant(0, MVT::i64));
813}
814
815SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode,
816 const SDValue &Op,
817 SelectionDAG &DAG) const {
818 return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1),
819 Op.getOperand(2),
820 ResourceDescriptorToi128(Op.getOperand(3), DAG),
821 Op.getOperand(4));
822}
823
Tom Stellard0ec134f2014-02-04 17:18:40 +0000824SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
825 if (Op.getValueType() != MVT::i64)
826 return SDValue();
827
828 SDLoc DL(Op);
829 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000830
831 SDValue Zero = DAG.getConstant(0, MVT::i32);
832 SDValue One = DAG.getConstant(1, MVT::i32);
833
Tom Stellard7ea3d6d2014-03-31 14:01:55 +0000834 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
835 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
836
837 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
838 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000839
840 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
841
Tom Stellard7ea3d6d2014-03-31 14:01:55 +0000842 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
843 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000844
845 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
846
Tom Stellard7ea3d6d2014-03-31 14:01:55 +0000847 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
848 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000849}
850
Tom Stellard75aadc22012-12-11 21:25:42 +0000851SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
852 SDValue LHS = Op.getOperand(0);
853 SDValue RHS = Op.getOperand(1);
854 SDValue True = Op.getOperand(2);
855 SDValue False = Op.getOperand(3);
856 SDValue CC = Op.getOperand(4);
857 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000858 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000859
860 // Possible Min/Max pattern
861 SDValue MinMax = LowerMinMax(Op, DAG);
862 if (MinMax.getNode()) {
863 return MinMax;
864 }
865
866 SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC);
867 return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False);
868}
869
Tom Stellard046039e2013-06-03 17:40:03 +0000870SDValue SITargetLowering::LowerSIGN_EXTEND(SDValue Op,
871 SelectionDAG &DAG) const {
872 EVT VT = Op.getValueType();
873 SDLoc DL(Op);
874
875 if (VT != MVT::i64) {
876 return SDValue();
877 }
878
879 SDValue Hi = DAG.getNode(ISD::SRA, DL, MVT::i32, Op.getOperand(0),
880 DAG.getConstant(31, MVT::i32));
881
882 return DAG.getNode(ISD::BUILD_PAIR, DL, VT, Op.getOperand(0), Hi);
883}
884
Tom Stellard81d871d2013-11-13 23:36:50 +0000885SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
886 SDLoc DL(Op);
887 StoreSDNode *Store = cast<StoreSDNode>(Op);
888 EVT VT = Store->getMemoryVT();
889
890 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
891 if (Ret.getNode())
892 return Ret;
893
894 if (VT.isVector() && VT.getVectorNumElements() >= 8)
895 return SplitVectorStore(Op, DAG);
896
Tom Stellard1c8788e2014-03-07 20:12:33 +0000897 if (VT == MVT::i1)
898 return DAG.getTruncStore(Store->getChain(), DL,
899 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
900 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
901
Tom Stellard81d871d2013-11-13 23:36:50 +0000902 if (Store->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS)
903 return SDValue();
904
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000905 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Store->getBasePtr(),
Tom Stellard81d871d2013-11-13 23:36:50 +0000906 DAG.getConstant(2, MVT::i32));
907 SDValue Chain = Store->getChain();
908 SmallVector<SDValue, 8> Values;
909
Tom Stellarde9373602014-01-22 19:24:14 +0000910 if (Store->isTruncatingStore()) {
911 unsigned Mask = 0;
912 if (Store->getMemoryVT() == MVT::i8) {
913 Mask = 0xff;
914 } else if (Store->getMemoryVT() == MVT::i16) {
915 Mask = 0xffff;
916 }
917 SDValue Dst = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
918 Chain, Store->getBasePtr(),
919 DAG.getConstant(0, MVT::i32));
920 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getBasePtr(),
921 DAG.getConstant(0x3, MVT::i32));
922 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
923 DAG.getConstant(3, MVT::i32));
924 SDValue MaskedValue = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getValue(),
925 DAG.getConstant(Mask, MVT::i32));
926 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
927 MaskedValue, ShiftAmt);
928 SDValue RotrAmt = DAG.getNode(ISD::SUB, DL, MVT::i32,
929 DAG.getConstant(32, MVT::i32), ShiftAmt);
930 SDValue DstMask = DAG.getNode(ISD::ROTR, DL, MVT::i32,
931 DAG.getConstant(Mask, MVT::i32),
932 RotrAmt);
933 Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
934 Dst = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
935
936 Values.push_back(Dst);
937 } else if (VT == MVT::i64) {
Tom Stellard81d871d2013-11-13 23:36:50 +0000938 for (unsigned i = 0; i < 2; ++i) {
939 Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
940 Store->getValue(), DAG.getConstant(i, MVT::i32)));
941 }
942 } else if (VT == MVT::i128) {
943 for (unsigned i = 0; i < 2; ++i) {
944 for (unsigned j = 0; j < 2; ++j) {
945 Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
946 DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64,
947 Store->getValue(), DAG.getConstant(i, MVT::i32)),
948 DAG.getConstant(j, MVT::i32)));
949 }
950 }
951 } else {
952 Values.push_back(Store->getValue());
953 }
954
955 for (unsigned i = 0; i < Values.size(); ++i) {
956 SDValue PartPtr = DAG.getNode(ISD::ADD, DL, MVT::i32,
957 Ptr, DAG.getConstant(i, MVT::i32));
958 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
959 Chain, Values[i], PartPtr,
960 DAG.getTargetConstant(0, MVT::i32));
961 }
962 return Chain;
963}
964
965
Tom Stellard98f675a2013-08-01 15:23:26 +0000966SDValue SITargetLowering::LowerZERO_EXTEND(SDValue Op,
967 SelectionDAG &DAG) const {
968 EVT VT = Op.getValueType();
969 SDLoc DL(Op);
970
971 if (VT != MVT::i64) {
972 return SDValue();
973 }
974
Matt Arsenault51df0c12014-04-17 02:03:08 +0000975 SDValue Src = Op.getOperand(0);
976 if (Src.getValueType() != MVT::i32)
977 Src = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Src);
978
979 SDValue Zero = DAG.getConstant(0, MVT::i32);
980 return DAG.getNode(ISD::BUILD_PAIR, DL, VT, Src, Zero);
Tom Stellard98f675a2013-08-01 15:23:26 +0000981}
982
Tom Stellard75aadc22012-12-11 21:25:42 +0000983//===----------------------------------------------------------------------===//
984// Custom DAG optimizations
985//===----------------------------------------------------------------------===//
986
987SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
988 DAGCombinerInfo &DCI) const {
989 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000990 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +0000991 EVT VT = N->getValueType(0);
992
993 switch (N->getOpcode()) {
Tom Stellard50122a52014-04-07 19:45:41 +0000994 default: return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000995 case ISD::SELECT_CC: {
Tom Stellard75aadc22012-12-11 21:25:42 +0000996 ConstantSDNode *True, *False;
997 // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
998 if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
999 && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1000 && True->isAllOnesValue()
1001 && False->isNullValue()
1002 && VT == MVT::i1) {
1003 return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
1004 N->getOperand(1), N->getOperand(4));
1005
1006 }
1007 break;
1008 }
1009 case ISD::SETCC: {
1010 SDValue Arg0 = N->getOperand(0);
1011 SDValue Arg1 = N->getOperand(1);
1012 SDValue CC = N->getOperand(2);
1013 ConstantSDNode * C = NULL;
1014 ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
1015
1016 // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
1017 if (VT == MVT::i1
1018 && Arg0.getOpcode() == ISD::SIGN_EXTEND
1019 && Arg0.getOperand(0).getValueType() == MVT::i1
1020 && (C = dyn_cast<ConstantSDNode>(Arg1))
1021 && C->isNullValue()
1022 && CCOp == ISD::SETNE) {
1023 return SimplifySetCC(VT, Arg0.getOperand(0),
1024 DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
1025 }
1026 break;
1027 }
1028 }
1029 return SDValue();
1030}
Christian Konigd910b7d2013-02-26 17:52:16 +00001031
Matt Arsenault758659232013-05-18 00:21:46 +00001032/// \brief Test if RegClass is one of the VSrc classes
Christian Konigf82901a2013-02-26 17:52:23 +00001033static bool isVSrc(unsigned RegClass) {
1034 return AMDGPU::VSrc_32RegClassID == RegClass ||
1035 AMDGPU::VSrc_64RegClassID == RegClass;
1036}
1037
Matt Arsenault758659232013-05-18 00:21:46 +00001038/// \brief Test if RegClass is one of the SSrc classes
Christian Konigf82901a2013-02-26 17:52:23 +00001039static bool isSSrc(unsigned RegClass) {
1040 return AMDGPU::SSrc_32RegClassID == RegClass ||
1041 AMDGPU::SSrc_64RegClassID == RegClass;
1042}
1043
1044/// \brief Analyze the possible immediate value Op
1045///
1046/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
1047/// and the immediate value if it's a literal immediate
1048int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
1049
1050 union {
1051 int32_t I;
1052 float F;
1053 } Imm;
1054
Tom Stellardedbf1eb2013-04-05 23:31:20 +00001055 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
1056 if (Node->getZExtValue() >> 32) {
1057 return -1;
1058 }
Christian Konigf82901a2013-02-26 17:52:23 +00001059 Imm.I = Node->getSExtValue();
Tom Stellard7ed0b522014-04-03 20:19:27 +00001060 } else if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
1061 if (N->getValueType(0) != MVT::f32)
1062 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00001063 Imm.F = Node->getValueAPF().convertToFloat();
Tom Stellard7ed0b522014-04-03 20:19:27 +00001064 } else
Christian Konigf82901a2013-02-26 17:52:23 +00001065 return -1; // It isn't an immediate
1066
1067 if ((Imm.I >= -16 && Imm.I <= 64) ||
1068 Imm.F == 0.5f || Imm.F == -0.5f ||
1069 Imm.F == 1.0f || Imm.F == -1.0f ||
1070 Imm.F == 2.0f || Imm.F == -2.0f ||
1071 Imm.F == 4.0f || Imm.F == -4.0f)
1072 return 0; // It's an inline immediate
1073
1074 return Imm.I; // It's a literal immediate
1075}
1076
1077/// \brief Try to fold an immediate directly into an instruction
1078bool SITargetLowering::foldImm(SDValue &Operand, int32_t &Immediate,
1079 bool &ScalarSlotUsed) const {
1080
1081 MachineSDNode *Mov = dyn_cast<MachineSDNode>(Operand);
Bill Wendling37e9adb2013-06-07 20:28:55 +00001082 const SIInstrInfo *TII =
1083 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00001084 if (Mov == 0 || !TII->isMov(Mov->getMachineOpcode()))
1085 return false;
1086
1087 const SDValue &Op = Mov->getOperand(0);
1088 int32_t Value = analyzeImmediate(Op.getNode());
1089 if (Value == -1) {
1090 // Not an immediate at all
1091 return false;
1092
1093 } else if (Value == 0) {
1094 // Inline immediates can always be fold
1095 Operand = Op;
1096 return true;
1097
1098 } else if (Value == Immediate) {
1099 // Already fold literal immediate
1100 Operand = Op;
1101 return true;
1102
1103 } else if (!ScalarSlotUsed && !Immediate) {
1104 // Fold this literal immediate
1105 ScalarSlotUsed = true;
1106 Immediate = Value;
1107 Operand = Op;
1108 return true;
1109
1110 }
1111
1112 return false;
1113}
1114
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001115const TargetRegisterClass *SITargetLowering::getRegClassForNode(
1116 SelectionDAG &DAG, const SDValue &Op) const {
1117 const SIInstrInfo *TII =
1118 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1119 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1120
1121 if (!Op->isMachineOpcode()) {
1122 switch(Op->getOpcode()) {
1123 case ISD::CopyFromReg: {
1124 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1125 unsigned Reg = cast<RegisterSDNode>(Op->getOperand(1))->getReg();
1126 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1127 return MRI.getRegClass(Reg);
1128 }
1129 return TRI.getPhysRegClass(Reg);
1130 }
1131 default: return NULL;
1132 }
1133 }
1134 const MCInstrDesc &Desc = TII->get(Op->getMachineOpcode());
1135 int OpClassID = Desc.OpInfo[Op.getResNo()].RegClass;
1136 if (OpClassID != -1) {
1137 return TRI.getRegClass(OpClassID);
1138 }
1139 switch(Op.getMachineOpcode()) {
1140 case AMDGPU::COPY_TO_REGCLASS:
1141 // Operand 1 is the register class id for COPY_TO_REGCLASS instructions.
1142 OpClassID = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1143
1144 // If the COPY_TO_REGCLASS instruction is copying to a VSrc register
1145 // class, then the register class for the value could be either a
1146 // VReg or and SReg. In order to get a more accurate
1147 if (OpClassID == AMDGPU::VSrc_32RegClassID ||
1148 OpClassID == AMDGPU::VSrc_64RegClassID) {
1149 return getRegClassForNode(DAG, Op.getOperand(0));
1150 }
1151 return TRI.getRegClass(OpClassID);
1152 case AMDGPU::EXTRACT_SUBREG: {
1153 int SubIdx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1154 const TargetRegisterClass *SuperClass =
1155 getRegClassForNode(DAG, Op.getOperand(0));
1156 return TRI.getSubClassWithSubReg(SuperClass, SubIdx);
1157 }
1158 case AMDGPU::REG_SEQUENCE:
1159 // Operand 0 is the register class id for REG_SEQUENCE instructions.
1160 return TRI.getRegClass(
1161 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue());
1162 default:
1163 return getRegClassFor(Op.getSimpleValueType());
1164 }
1165}
1166
Christian Konigf82901a2013-02-26 17:52:23 +00001167/// \brief Does "Op" fit into register class "RegClass" ?
Tom Stellardb35efba2013-05-20 15:02:01 +00001168bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, const SDValue &Op,
Christian Konigf82901a2013-02-26 17:52:23 +00001169 unsigned RegClass) const {
Bill Wendling37e9adb2013-06-07 20:28:55 +00001170 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001171 const TargetRegisterClass *RC = getRegClassForNode(DAG, Op);
1172 if (!RC) {
Christian Konigf82901a2013-02-26 17:52:23 +00001173 return false;
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001174 }
1175 return TRI->getRegClass(RegClass)->hasSubClassEq(RC);
Christian Konigf82901a2013-02-26 17:52:23 +00001176}
1177
1178/// \brief Make sure that we don't exeed the number of allowed scalars
1179void SITargetLowering::ensureSRegLimit(SelectionDAG &DAG, SDValue &Operand,
1180 unsigned RegClass,
1181 bool &ScalarSlotUsed) const {
1182
1183 // First map the operands register class to a destination class
1184 if (RegClass == AMDGPU::VSrc_32RegClassID)
1185 RegClass = AMDGPU::VReg_32RegClassID;
1186 else if (RegClass == AMDGPU::VSrc_64RegClassID)
1187 RegClass = AMDGPU::VReg_64RegClassID;
1188 else
1189 return;
1190
Alp Tokercb402912014-01-24 17:20:08 +00001191 // Nothing to do if they fit naturally
Christian Konigf82901a2013-02-26 17:52:23 +00001192 if (fitsRegClass(DAG, Operand, RegClass))
1193 return;
1194
1195 // If the scalar slot isn't used yet use it now
1196 if (!ScalarSlotUsed) {
1197 ScalarSlotUsed = true;
1198 return;
1199 }
1200
Matt Arsenault1408b602013-10-10 23:05:37 +00001201 // This is a conservative aproach. It is possible that we can't determine the
1202 // correct register class and copy too often, but better safe than sorry.
Christian Konigf82901a2013-02-26 17:52:23 +00001203 SDValue RC = DAG.getTargetConstant(RegClass, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001204 SDNode *Node = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, SDLoc(),
Christian Konigf82901a2013-02-26 17:52:23 +00001205 Operand.getValueType(), Operand, RC);
1206 Operand = SDValue(Node, 0);
1207}
1208
Tom Stellardacec99c2013-06-05 23:39:50 +00001209/// \returns true if \p Node's operands are different from the SDValue list
1210/// \p Ops
1211static bool isNodeChanged(const SDNode *Node, const std::vector<SDValue> &Ops) {
1212 for (unsigned i = 0, e = Node->getNumOperands(); i < e; ++i) {
1213 if (Ops[i].getNode() != Node->getOperand(i).getNode()) {
1214 return true;
1215 }
1216 }
1217 return false;
1218}
1219
Christian Konig8e06e2a2013-04-10 08:39:08 +00001220/// \brief Try to fold the Nodes operands into the Node
1221SDNode *SITargetLowering::foldOperands(MachineSDNode *Node,
1222 SelectionDAG &DAG) const {
Christian Konigf82901a2013-02-26 17:52:23 +00001223
1224 // Original encoding (either e32 or e64)
1225 int Opcode = Node->getMachineOpcode();
Bill Wendling37e9adb2013-06-07 20:28:55 +00001226 const SIInstrInfo *TII =
1227 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00001228 const MCInstrDesc *Desc = &TII->get(Opcode);
1229
1230 unsigned NumDefs = Desc->getNumDefs();
1231 unsigned NumOps = Desc->getNumOperands();
1232
Christian Konig3c145802013-03-27 09:12:59 +00001233 // Commuted opcode if available
1234 int OpcodeRev = Desc->isCommutable() ? TII->commuteOpcode(Opcode) : -1;
1235 const MCInstrDesc *DescRev = OpcodeRev == -1 ? 0 : &TII->get(OpcodeRev);
1236
1237 assert(!DescRev || DescRev->getNumDefs() == NumDefs);
1238 assert(!DescRev || DescRev->getNumOperands() == NumOps);
1239
Christian Konige500e442013-02-26 17:52:47 +00001240 // e64 version if available, -1 otherwise
1241 int OpcodeE64 = AMDGPU::getVOPe64(Opcode);
1242 const MCInstrDesc *DescE64 = OpcodeE64 == -1 ? 0 : &TII->get(OpcodeE64);
1243
1244 assert(!DescE64 || DescE64->getNumDefs() == NumDefs);
1245 assert(!DescE64 || DescE64->getNumOperands() == (NumOps + 4));
1246
Christian Konigf82901a2013-02-26 17:52:23 +00001247 int32_t Immediate = Desc->getSize() == 4 ? 0 : -1;
1248 bool HaveVSrc = false, HaveSSrc = false;
1249
1250 // First figure out what we alread have in this instruction
1251 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1252 i != e && Op < NumOps; ++i, ++Op) {
1253
1254 unsigned RegClass = Desc->OpInfo[Op].RegClass;
1255 if (isVSrc(RegClass))
1256 HaveVSrc = true;
1257 else if (isSSrc(RegClass))
1258 HaveSSrc = true;
1259 else
1260 continue;
1261
1262 int32_t Imm = analyzeImmediate(Node->getOperand(i).getNode());
1263 if (Imm != -1 && Imm != 0) {
1264 // Literal immediate
1265 Immediate = Imm;
1266 }
1267 }
1268
1269 // If we neither have VSrc nor SSrc it makes no sense to continue
1270 if (!HaveVSrc && !HaveSSrc)
1271 return Node;
1272
1273 // No scalar allowed when we have both VSrc and SSrc
1274 bool ScalarSlotUsed = HaveVSrc && HaveSSrc;
1275
1276 // Second go over the operands and try to fold them
1277 std::vector<SDValue> Ops;
Christian Konige500e442013-02-26 17:52:47 +00001278 bool Promote2e64 = false;
Christian Konigf82901a2013-02-26 17:52:23 +00001279 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1280 i != e && Op < NumOps; ++i, ++Op) {
1281
1282 const SDValue &Operand = Node->getOperand(i);
1283 Ops.push_back(Operand);
1284
1285 // Already folded immediate ?
1286 if (isa<ConstantSDNode>(Operand.getNode()) ||
1287 isa<ConstantFPSDNode>(Operand.getNode()))
1288 continue;
1289
1290 // Is this a VSrc or SSrc operand ?
1291 unsigned RegClass = Desc->OpInfo[Op].RegClass;
Christian Konig8370dbb2013-03-26 14:04:17 +00001292 if (isVSrc(RegClass) || isSSrc(RegClass)) {
1293 // Try to fold the immediates
1294 if (!foldImm(Ops[i], Immediate, ScalarSlotUsed)) {
1295 // Folding didn't worked, make sure we don't hit the SReg limit
1296 ensureSRegLimit(DAG, Ops[i], RegClass, ScalarSlotUsed);
1297 }
1298 continue;
1299 }
Christian Konig6612ac32013-02-26 17:52:36 +00001300
Christian Konig3c145802013-03-27 09:12:59 +00001301 if (i == 1 && DescRev && fitsRegClass(DAG, Ops[0], RegClass)) {
Christian Konig6612ac32013-02-26 17:52:36 +00001302
Christian Konig8370dbb2013-03-26 14:04:17 +00001303 unsigned OtherRegClass = Desc->OpInfo[NumDefs].RegClass;
1304 assert(isVSrc(OtherRegClass) || isSSrc(OtherRegClass));
1305
1306 // Test if it makes sense to swap operands
1307 if (foldImm(Ops[1], Immediate, ScalarSlotUsed) ||
1308 (!fitsRegClass(DAG, Ops[1], RegClass) &&
1309 fitsRegClass(DAG, Ops[1], OtherRegClass))) {
Christian Konig6612ac32013-02-26 17:52:36 +00001310
1311 // Swap commutable operands
Matt Arsenault4be76e92014-04-07 16:44:26 +00001312 std::swap(Ops[0], Ops[1]);
Christian Konig3c145802013-03-27 09:12:59 +00001313
1314 Desc = DescRev;
1315 DescRev = 0;
Christian Konig8370dbb2013-03-26 14:04:17 +00001316 continue;
Christian Konig6612ac32013-02-26 17:52:36 +00001317 }
Christian Konig6612ac32013-02-26 17:52:36 +00001318 }
Christian Konigf82901a2013-02-26 17:52:23 +00001319
Christian Konig8370dbb2013-03-26 14:04:17 +00001320 if (DescE64 && !Immediate) {
1321
1322 // Test if it makes sense to switch to e64 encoding
1323 unsigned OtherRegClass = DescE64->OpInfo[Op].RegClass;
1324 if (!isVSrc(OtherRegClass) && !isSSrc(OtherRegClass))
1325 continue;
1326
1327 int32_t TmpImm = -1;
1328 if (foldImm(Ops[i], TmpImm, ScalarSlotUsed) ||
1329 (!fitsRegClass(DAG, Ops[i], RegClass) &&
1330 fitsRegClass(DAG, Ops[1], OtherRegClass))) {
1331
1332 // Switch to e64 encoding
1333 Immediate = -1;
1334 Promote2e64 = true;
1335 Desc = DescE64;
1336 DescE64 = 0;
1337 }
Christian Konigf82901a2013-02-26 17:52:23 +00001338 }
1339 }
1340
Christian Konige500e442013-02-26 17:52:47 +00001341 if (Promote2e64) {
1342 // Add the modifier flags while promoting
1343 for (unsigned i = 0; i < 4; ++i)
1344 Ops.push_back(DAG.getTargetConstant(0, MVT::i32));
1345 }
1346
Christian Konigf82901a2013-02-26 17:52:23 +00001347 // Add optional chain and glue
1348 for (unsigned i = NumOps - NumDefs, e = Node->getNumOperands(); i < e; ++i)
1349 Ops.push_back(Node->getOperand(i));
1350
Tom Stellardb5a97002013-06-03 17:39:50 +00001351 // Nodes that have a glue result are not CSE'd by getMachineNode(), so in
1352 // this case a brand new node is always be created, even if the operands
1353 // are the same as before. So, manually check if anything has been changed.
Tom Stellardacec99c2013-06-05 23:39:50 +00001354 if (Desc->Opcode == Opcode && !isNodeChanged(Node, Ops)) {
1355 return Node;
Tom Stellardb5a97002013-06-03 17:39:50 +00001356 }
1357
Christian Konig3c145802013-03-27 09:12:59 +00001358 // Create a complete new instruction
Andrew Trickef9de2a2013-05-25 02:42:55 +00001359 return DAG.getMachineNode(Desc->Opcode, SDLoc(Node), Node->getVTList(), Ops);
Christian Konigd910b7d2013-02-26 17:52:16 +00001360}
Christian Konig8e06e2a2013-04-10 08:39:08 +00001361
1362/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00001363static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00001364 switch (Idx) {
1365 default: return 0;
1366 case AMDGPU::sub0: return 0;
1367 case AMDGPU::sub1: return 1;
1368 case AMDGPU::sub2: return 2;
1369 case AMDGPU::sub3: return 3;
1370 }
1371}
1372
1373/// \brief Adjust the writemask of MIMG instructions
1374void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
1375 SelectionDAG &DAG) const {
1376 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00001377 unsigned Lane = 0;
1378 unsigned OldDmask = Node->getConstantOperandVal(0);
1379 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001380
1381 // Try to figure out the used register components
1382 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
1383 I != E; ++I) {
1384
1385 // Abort if we can't understand the usage
1386 if (!I->isMachineOpcode() ||
1387 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
1388 return;
1389
Tom Stellard54774e52013-10-23 02:53:47 +00001390 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
1391 // Note that subregs are packed, i.e. Lane==0 is the first bit set
1392 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
1393 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00001394 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00001395
Tom Stellard54774e52013-10-23 02:53:47 +00001396 // Set which texture component corresponds to the lane.
1397 unsigned Comp;
1398 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
1399 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00001400 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00001401 Dmask &= ~(1 << Comp);
1402 }
1403
Christian Konig8e06e2a2013-04-10 08:39:08 +00001404 // Abort if we have more than one user per component
1405 if (Users[Lane])
1406 return;
1407
1408 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00001409 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001410 }
1411
Tom Stellard54774e52013-10-23 02:53:47 +00001412 // Abort if there's no change
1413 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00001414 return;
1415
1416 // Adjust the writemask in the node
1417 std::vector<SDValue> Ops;
Tom Stellard54774e52013-10-23 02:53:47 +00001418 Ops.push_back(DAG.getTargetConstant(NewDmask, MVT::i32));
Christian Konig8e06e2a2013-04-10 08:39:08 +00001419 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1420 Ops.push_back(Node->getOperand(i));
1421 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops.data(), Ops.size());
1422
Christian Konig8b1ed282013-04-10 08:39:16 +00001423 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00001424 // (if NewDmask has only one bit set...)
1425 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Christian Konig8b1ed282013-04-10 08:39:16 +00001426 SDValue RC = DAG.getTargetConstant(AMDGPU::VReg_32RegClassID, MVT::i32);
1427 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001428 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00001429 SDValue(Node, 0), RC);
1430 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
1431 return;
1432 }
1433
Christian Konig8e06e2a2013-04-10 08:39:08 +00001434 // Update the users of the node with the new indices
1435 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
1436
1437 SDNode *User = Users[i];
1438 if (!User)
1439 continue;
1440
1441 SDValue Op = DAG.getTargetConstant(Idx, MVT::i32);
1442 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
1443
1444 switch (Idx) {
1445 default: break;
1446 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
1447 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
1448 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
1449 }
1450 }
1451}
1452
1453/// \brief Fold the instructions after slecting them
1454SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
1455 SelectionDAG &DAG) const {
Tom Stellard16a9a202013-08-14 23:24:17 +00001456 const SIInstrInfo *TII =
1457 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
Tom Stellard0518ff82013-06-03 17:39:58 +00001458 Node = AdjustRegClass(Node, DAG);
Christian Konig8e06e2a2013-04-10 08:39:08 +00001459
Tom Stellard16a9a202013-08-14 23:24:17 +00001460 if (TII->isMIMG(Node->getMachineOpcode()))
Christian Konig8e06e2a2013-04-10 08:39:08 +00001461 adjustWritemask(Node, DAG);
1462
1463 return foldOperands(Node, DAG);
1464}
Christian Konig8b1ed282013-04-10 08:39:16 +00001465
1466/// \brief Assign the register class depending on the number of
1467/// bits set in the writemask
1468void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
1469 SDNode *Node) const {
Tom Stellard16a9a202013-08-14 23:24:17 +00001470 const SIInstrInfo *TII =
1471 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1472 if (!TII->isMIMG(MI->getOpcode()))
Christian Konig8b1ed282013-04-10 08:39:16 +00001473 return;
1474
1475 unsigned VReg = MI->getOperand(0).getReg();
1476 unsigned Writemask = MI->getOperand(1).getImm();
1477 unsigned BitsSet = 0;
1478 for (unsigned i = 0; i < 4; ++i)
1479 BitsSet += Writemask & (1 << i) ? 1 : 0;
1480
1481 const TargetRegisterClass *RC;
1482 switch (BitsSet) {
1483 default: return;
1484 case 1: RC = &AMDGPU::VReg_32RegClass; break;
1485 case 2: RC = &AMDGPU::VReg_64RegClass; break;
1486 case 3: RC = &AMDGPU::VReg_96RegClass; break;
1487 }
1488
Tom Stellard682bfbc2013-10-10 17:11:24 +00001489 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
1490 MI->setDesc(TII->get(NewOpcode));
Christian Konig8b1ed282013-04-10 08:39:16 +00001491 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
1492 MRI.setRegClass(VReg, RC);
1493}
Tom Stellard0518ff82013-06-03 17:39:58 +00001494
1495MachineSDNode *SITargetLowering::AdjustRegClass(MachineSDNode *N,
1496 SelectionDAG &DAG) const {
1497
1498 SDLoc DL(N);
1499 unsigned NewOpcode = N->getMachineOpcode();
1500
1501 switch (N->getMachineOpcode()) {
1502 default: return N;
Tom Stellard0518ff82013-06-03 17:39:58 +00001503 case AMDGPU::S_LOAD_DWORD_IMM:
1504 NewOpcode = AMDGPU::BUFFER_LOAD_DWORD_ADDR64;
1505 // Fall-through
1506 case AMDGPU::S_LOAD_DWORDX2_SGPR:
1507 if (NewOpcode == N->getMachineOpcode()) {
1508 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64;
1509 }
1510 // Fall-through
1511 case AMDGPU::S_LOAD_DWORDX4_IMM:
1512 case AMDGPU::S_LOAD_DWORDX4_SGPR: {
1513 if (NewOpcode == N->getMachineOpcode()) {
1514 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64;
1515 }
1516 if (fitsRegClass(DAG, N->getOperand(0), AMDGPU::SReg_64RegClassID)) {
1517 return N;
1518 }
1519 ConstantSDNode *Offset = cast<ConstantSDNode>(N->getOperand(1));
1520 SDValue Ops[] = {
1521 SDValue(DAG.getMachineNode(AMDGPU::SI_ADDR64_RSRC, DL, MVT::i128,
1522 DAG.getConstant(0, MVT::i64)), 0),
1523 N->getOperand(0),
1524 DAG.getConstant(Offset->getSExtValue() << 2, MVT::i32)
1525 };
1526 return DAG.getMachineNode(NewOpcode, DL, N->getVTList(), Ops);
1527 }
1528 }
1529}
Tom Stellard94593ee2013-06-03 17:40:18 +00001530
1531SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
1532 const TargetRegisterClass *RC,
1533 unsigned Reg, EVT VT) const {
1534 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
1535
1536 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
1537 cast<RegisterSDNode>(VReg)->getReg(), VT);
1538}