blob: cd23cd2a4e0a4ffacaac29a22e59ed5d355e4d26 [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 Arsenaultc791f392014-06-23 18:00:31 +000017#include "AMDGPUIntrinsicInfo.h"
Matt Arsenault41e2f2b2014-02-24 21:01:28 +000018#include "AMDGPUSubtarget.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000019#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"
Matt Arsenault364a6742014-06-11 17:50:44 +000027#include "llvm/ADT/SmallString.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000028
29using namespace llvm;
30
31SITargetLowering::SITargetLowering(TargetMachine &TM) :
Bill Wendling37e9adb2013-06-07 20:28:55 +000032 AMDGPUTargetLowering(TM) {
Tom Stellard1bd80722014-04-30 15:31:33 +000033 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
Tom Stellard436780b2014-05-15 14:41:57 +000034 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000035
Christian Konig2214f142013-03-07 09:03:38 +000036 addRegisterClass(MVT::v32i8, &AMDGPU::SReg_256RegClass);
37 addRegisterClass(MVT::v64i8, &AMDGPU::SReg_512RegClass);
38
Tom Stellard334b29c2014-04-17 21:00:09 +000039 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
Tom Stellard436780b2014-05-15 14:41:57 +000040 addRegisterClass(MVT::f32, &AMDGPU::VReg_32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000041
Tom Stellard436780b2014-05-15 14:41:57 +000042 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
43 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
44 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000045
Tom Stellard436780b2014-05-15 14:41:57 +000046 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
47 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_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 Stellard35bb18c2013-08-26 15:06:04 +000081 // We need to custom lower vector stores from local memory
82 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
83 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellardaf775432013-10-23 00:44:32 +000084 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
85 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
86
87 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
88 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
Tom Stellard35bb18c2013-08-26 15:06:04 +000089
Tom Stellard81d871d2013-11-13 23:36:50 +000090 // We need to custom lower loads/stores from private memory
91 setOperationAction(ISD::LOAD, MVT::i32, Custom);
92 setOperationAction(ISD::LOAD, MVT::i64, Custom);
93 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
94 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellard967bf582014-02-13 23:34:15 +000095 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +000096
Tom Stellard1c8788e2014-03-07 20:12:33 +000097 setOperationAction(ISD::STORE, MVT::i1, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +000098 setOperationAction(ISD::STORE, MVT::i32, Custom);
99 setOperationAction(ISD::STORE, MVT::i64, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +0000100 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
101 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
102
Tom Stellardf719ee92014-05-16 20:56:41 +0000103 setOperationAction(ISD::SELECT, MVT::f32, Promote);
104 AddPromotedToType(ISD::SELECT, MVT::f32, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000105 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Tom Stellardda99c6e2014-03-24 16:07:30 +0000106 setOperationAction(ISD::SELECT, MVT::f64, Promote);
107 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
Tom Stellard81d871d2013-11-13 23:36:50 +0000108
Tom Stellard3ca1bfc2014-06-10 16:01:22 +0000109 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
110 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
111 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
112 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Tom Stellard754f80f2013-04-05 23:31:51 +0000113
Tom Stellard83747202013-07-18 21:43:53 +0000114 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
115 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
116
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000117 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000118 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
120
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000121 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
124
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000125 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000126 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
127 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
128
129 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Custom);
130
131 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
132
Tom Stellard94593ee2013-06-03 17:40:18 +0000133 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Tom Stellard9fa17912013-08-14 23:24:45 +0000134 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
135 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom);
136 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
Tom Stellard94593ee2013-06-03 17:40:18 +0000137
Tom Stellardafcf12f2013-09-12 02:55:14 +0000138 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
139
Matt Arsenault470acd82014-04-15 22:28:39 +0000140 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Tom Stellarde9373602014-01-22 19:24:14 +0000141 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Custom);
142 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Custom);
Matt Arsenault470acd82014-04-15 22:28:39 +0000143 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000144 setLoadExtAction(ISD::SEXTLOAD, MVT::v8i16, Expand);
145 setLoadExtAction(ISD::SEXTLOAD, MVT::v16i16, Expand);
Tom Stellard31209cc2013-07-15 19:00:09 +0000146
Matt Arsenault470acd82014-04-15 22:28:39 +0000147 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
148 setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Custom);
149 setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Custom);
150 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Expand);
151
152 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
Tom Stellarde9373602014-01-22 19:24:14 +0000153 setLoadExtAction(ISD::EXTLOAD, MVT::i8, Custom);
154 setLoadExtAction(ISD::EXTLOAD, MVT::i16, Custom);
155 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Expand);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000156 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000157
Tom Stellarde9373602014-01-22 19:24:14 +0000158 setTruncStoreAction(MVT::i32, MVT::i8, Custom);
159 setTruncStoreAction(MVT::i32, MVT::i16, Custom);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000160 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Matt Arsenault6f243792013-09-05 19:41:10 +0000161 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000162 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
163 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000164
Matt Arsenault470acd82014-04-15 22:28:39 +0000165 setOperationAction(ISD::LOAD, MVT::i1, Custom);
166
Tom Stellardfd155822013-08-26 15:05:36 +0000167 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Tom Stellard04c0e982014-01-22 19:24:21 +0000168 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000169 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
Michel Danzer49812b52013-07-10 16:37:07 +0000170
Tom Stellard5f337882014-04-29 23:12:43 +0000171 // These should use UDIVREM, so set them to expand
172 setOperationAction(ISD::UDIV, MVT::i64, Expand);
173 setOperationAction(ISD::UREM, MVT::i64, Expand);
174
Tom Stellard967bf582014-02-13 23:34:15 +0000175 // We only support LOAD/STORE and vector manipulation ops for vectors
176 // with > 4 elements.
177 MVT VecTypes[] = {
Tom Stellardd61a1c32014-02-28 21:36:37 +0000178 MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32
Tom Stellard967bf582014-02-13 23:34:15 +0000179 };
180
Matt Arsenaultd504a742014-05-15 21:44:05 +0000181 for (MVT VT : VecTypes) {
Tom Stellard967bf582014-02-13 23:34:15 +0000182 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
183 switch(Op) {
184 case ISD::LOAD:
185 case ISD::STORE:
186 case ISD::BUILD_VECTOR:
187 case ISD::BITCAST:
188 case ISD::EXTRACT_VECTOR_ELT:
189 case ISD::INSERT_VECTOR_ELT:
190 case ISD::CONCAT_VECTORS:
191 case ISD::INSERT_SUBVECTOR:
192 case ISD::EXTRACT_SUBVECTOR:
193 break;
194 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000195 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000196 break;
197 }
198 }
199 }
200
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000201 for (int I = MVT::v1f64; I <= MVT::v8f64; ++I) {
202 MVT::SimpleValueType VT = static_cast<MVT::SimpleValueType>(I);
Matt Arsenaulta81aee82014-02-24 21:16:50 +0000203 setOperationAction(ISD::FTRUNC, VT, Expand);
204 setOperationAction(ISD::FCEIL, VT, Expand);
205 setOperationAction(ISD::FFLOOR, VT, Expand);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000206 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000207
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000208 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
209 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
210 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
211 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
Matt Arsenaulta90d22f2014-04-17 17:06:37 +0000212 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000213 }
214
Matt Arsenault7aeb8132014-06-18 17:05:22 +0000215 // FIXME: This should be removed and handled the same was as f32 fneg. Source
216 // modifiers also work for the double instructions.
217 setOperationAction(ISD::FNEG, MVT::f64, Expand);
218
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000219 setTargetDAGCombine(ISD::SELECT_CC);
Tom Stellard75aadc22012-12-11 21:25:42 +0000220 setTargetDAGCombine(ISD::SETCC);
Michel Danzerf52a6722013-03-08 10:58:01 +0000221
Matt Arsenault364a6742014-06-11 17:50:44 +0000222 setTargetDAGCombine(ISD::UINT_TO_FP);
223
Christian Konigeecebd02013-03-26 14:04:02 +0000224 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000225}
226
Tom Stellard0125f2a2013-06-25 02:39:35 +0000227//===----------------------------------------------------------------------===//
228// TargetLowering queries
229//===----------------------------------------------------------------------===//
230
231bool SITargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
Matt Arsenault25793a32014-02-05 23:15:53 +0000232 unsigned AddrSpace,
Tom Stellard0125f2a2013-06-25 02:39:35 +0000233 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000234 if (IsFast)
235 *IsFast = false;
236
Tom Stellard0125f2a2013-06-25 02:39:35 +0000237 // XXX: This depends on the address space and also we may want to revist
238 // the alignment values we specify in the DataLayout.
Matt Arsenault1018c892014-04-24 17:08:26 +0000239
240 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
241 // which isn't a simple VT.
Tom Stellard81d871d2013-11-13 23:36:50 +0000242 if (!VT.isSimple() || VT == MVT::Other)
243 return false;
Matt Arsenault1018c892014-04-24 17:08:26 +0000244
245 // XXX - CI changes say "Support for unaligned memory accesses" but I don't
246 // see what for specifically. The wording everywhere else seems to be the
247 // same.
248
249 // 3.6.4 - Operations using pairs of VGPRs (for example: double-floats) have
250 // no alignment restrictions.
251 if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS) {
252 // Using any pair of GPRs should be the same as any other pair.
253 if (IsFast)
254 *IsFast = true;
255 return VT.bitsGE(MVT::i64);
256 }
257
258 // XXX - The only mention I see of this in the ISA manual is for LDS direct
259 // reads the "byte address and must be dword aligned". Is it also true for the
260 // normal loads and stores?
261 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS)
262 return false;
263
264 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
265 // byte-address are ignored, thus forcing Dword alignment.
266 if (IsFast)
267 *IsFast = true;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000268 return VT.bitsGT(MVT::i32);
269}
270
Matt Arsenaultf751d622014-03-31 20:54:58 +0000271bool SITargetLowering::shouldSplitVectorType(EVT VT) const {
272 return VT.getScalarType().bitsLE(MVT::i16);
Tom Stellardd86003e2013-08-14 23:25:00 +0000273}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000274
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000275bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
276 Type *Ty) const {
277 const SIInstrInfo *TII =
278 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
279 return TII->isInlineConstant(Imm);
280}
281
Tom Stellardaf775432013-10-23 00:44:32 +0000282SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
Tom Stellard94593ee2013-06-03 17:40:18 +0000283 SDLoc DL, SDValue Chain,
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000284 unsigned Offset, bool Signed) const {
Tom Stellard94593ee2013-06-03 17:40:18 +0000285 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
286 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
287 AMDGPUAS::CONSTANT_ADDRESS);
Tom Stellard94593ee2013-06-03 17:40:18 +0000288 SDValue BasePtr = DAG.getCopyFromReg(Chain, DL,
289 MRI.getLiveInVirtReg(AMDGPU::SGPR0_SGPR1), MVT::i64);
290 SDValue Ptr = DAG.getNode(ISD::ADD, DL, MVT::i64, BasePtr,
291 DAG.getConstant(Offset, MVT::i64));
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000292 return DAG.getExtLoad(Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD, DL, VT, Chain, Ptr,
Tom Stellardaf775432013-10-23 00:44:32 +0000293 MachinePointerInfo(UndefValue::get(PtrTy)), MemVT,
294 false, false, MemVT.getSizeInBits() >> 3);
Tom Stellard94593ee2013-06-03 17:40:18 +0000295
296}
297
Christian Konig2c8f6d52013-03-07 09:03:52 +0000298SDValue SITargetLowering::LowerFormalArguments(
299 SDValue Chain,
300 CallingConv::ID CallConv,
301 bool isVarArg,
302 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000303 SDLoc DL, SelectionDAG &DAG,
Christian Konig2c8f6d52013-03-07 09:03:52 +0000304 SmallVectorImpl<SDValue> &InVals) const {
305
306 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
307
308 MachineFunction &MF = DAG.getMachineFunction();
309 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000310 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000311
312 assert(CallConv == CallingConv::C);
313
314 SmallVector<ISD::InputArg, 16> Splits;
Christian Konig99ee0f42013-03-07 09:04:14 +0000315 uint32_t Skipped = 0;
316
317 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000318 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000319
320 // First check if it's a PS input addr
Vincent Lejeuned6236442013-10-13 17:56:16 +0000321 if (Info->ShaderType == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
322 !Arg.Flags.isByVal()) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000323
324 assert((PSInputNum <= 15) && "Too many PS inputs!");
325
326 if (!Arg.Used) {
327 // We can savely skip PS inputs
328 Skipped |= 1 << i;
329 ++PSInputNum;
330 continue;
331 }
332
333 Info->PSInputAddr |= 1 << PSInputNum++;
334 }
335
336 // Second split vertices into their elements
Tom Stellarded882c22013-06-03 17:40:11 +0000337 if (Info->ShaderType != ShaderType::COMPUTE && Arg.VT.isVector()) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000338 ISD::InputArg NewArg = Arg;
339 NewArg.Flags.setSplit();
340 NewArg.VT = Arg.VT.getVectorElementType();
341
342 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
343 // three or five element vertex only needs three or five registers,
344 // NOT four or eigth.
345 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
346 unsigned NumElements = ParamType->getVectorNumElements();
347
348 for (unsigned j = 0; j != NumElements; ++j) {
349 Splits.push_back(NewArg);
350 NewArg.PartOffset += NewArg.VT.getStoreSize();
351 }
352
Tom Stellardaf775432013-10-23 00:44:32 +0000353 } else if (Info->ShaderType != ShaderType::COMPUTE) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000354 Splits.push_back(Arg);
355 }
356 }
357
358 SmallVector<CCValAssign, 16> ArgLocs;
359 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
360 getTargetMachine(), ArgLocs, *DAG.getContext());
361
Christian Konig99ee0f42013-03-07 09:04:14 +0000362 // At least one interpolation mode must be enabled or else the GPU will hang.
363 if (Info->ShaderType == ShaderType::PIXEL && (Info->PSInputAddr & 0x7F) == 0) {
364 Info->PSInputAddr |= 1;
365 CCInfo.AllocateReg(AMDGPU::VGPR0);
366 CCInfo.AllocateReg(AMDGPU::VGPR1);
367 }
368
Tom Stellarded882c22013-06-03 17:40:11 +0000369 // The pointer to the list of arguments is stored in SGPR0, SGPR1
370 if (Info->ShaderType == ShaderType::COMPUTE) {
371 CCInfo.AllocateReg(AMDGPU::SGPR0);
372 CCInfo.AllocateReg(AMDGPU::SGPR1);
Tom Stellard94593ee2013-06-03 17:40:18 +0000373 MF.addLiveIn(AMDGPU::SGPR0_SGPR1, &AMDGPU::SReg_64RegClass);
Tom Stellarded882c22013-06-03 17:40:11 +0000374 }
375
Tom Stellardaf775432013-10-23 00:44:32 +0000376 if (Info->ShaderType == ShaderType::COMPUTE) {
377 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
378 Splits);
379 }
380
Christian Konig2c8f6d52013-03-07 09:03:52 +0000381 AnalyzeFormalArguments(CCInfo, Splits);
382
383 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
384
Christian Konigb7be72d2013-05-17 09:46:48 +0000385 const ISD::InputArg &Arg = Ins[i];
Christian Konig99ee0f42013-03-07 09:04:14 +0000386 if (Skipped & (1 << i)) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000387 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000388 continue;
389 }
390
Christian Konig2c8f6d52013-03-07 09:03:52 +0000391 CCValAssign &VA = ArgLocs[ArgIdx++];
Tom Stellarded882c22013-06-03 17:40:11 +0000392 EVT VT = VA.getLocVT();
393
394 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000395 VT = Ins[i].VT;
396 EVT MemVT = Splits[i].VT;
Tom Stellard94593ee2013-06-03 17:40:18 +0000397 // The first 36 bytes of the input buffer contains information about
398 // thread group and global sizes.
Tom Stellardaf775432013-10-23 00:44:32 +0000399 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, DAG.getRoot(),
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000400 36 + VA.getLocMemOffset(),
401 Ins[i].Flags.isSExt());
Tom Stellarded882c22013-06-03 17:40:11 +0000402 InVals.push_back(Arg);
403 continue;
404 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000405 assert(VA.isRegLoc() && "Parameter must be in a register!");
406
407 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000408
409 if (VT == MVT::i64) {
410 // For now assume it is a pointer
411 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
412 &AMDGPU::SReg_64RegClass);
413 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
414 InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
415 continue;
416 }
417
418 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
419
420 Reg = MF.addLiveIn(Reg, RC);
421 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
422
Christian Konig2c8f6d52013-03-07 09:03:52 +0000423 if (Arg.VT.isVector()) {
424
425 // Build a vector from the registers
426 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
427 unsigned NumElements = ParamType->getVectorNumElements();
428
429 SmallVector<SDValue, 4> Regs;
430 Regs.push_back(Val);
431 for (unsigned j = 1; j != NumElements; ++j) {
432 Reg = ArgLocs[ArgIdx++].getLocReg();
433 Reg = MF.addLiveIn(Reg, RC);
434 Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
435 }
436
437 // Fill up the missing vector elements
438 NumElements = Arg.VT.getVectorNumElements() - NumElements;
439 for (unsigned j = 0; j != NumElements; ++j)
440 Regs.push_back(DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000441
Craig Topper48d114b2014-04-26 18:35:24 +0000442 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000443 continue;
444 }
445
446 InVals.push_back(Val);
447 }
448 return Chain;
449}
450
Tom Stellard75aadc22012-12-11 21:25:42 +0000451MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
452 MachineInstr * MI, MachineBasicBlock * BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000453
Tom Stellard556d9aa2013-06-03 17:39:37 +0000454 MachineBasicBlock::iterator I = *MI;
Tom Stellard919bb6b2014-04-29 23:12:53 +0000455 const SIInstrInfo *TII =
456 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
457 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Tom Stellard556d9aa2013-06-03 17:39:37 +0000458
Tom Stellard75aadc22012-12-11 21:25:42 +0000459 switch (MI->getOpcode()) {
460 default:
461 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
462 case AMDGPU::BRANCH: return BB;
Tom Stellard556d9aa2013-06-03 17:39:37 +0000463 case AMDGPU::SI_ADDR64_RSRC: {
Tom Stellard556d9aa2013-06-03 17:39:37 +0000464 unsigned SuperReg = MI->getOperand(0).getReg();
Tom Stellarddef38c52014-03-21 15:51:53 +0000465 unsigned SubRegLo = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass);
466 unsigned SubRegHi = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass);
467 unsigned SubRegHiHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
468 unsigned SubRegHiLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
Tom Stellard556d9aa2013-06-03 17:39:37 +0000469 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B64), SubRegLo)
470 .addOperand(MI->getOperand(1));
471 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiLo)
472 .addImm(0);
473 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiHi)
Tom Stellard15834092014-03-21 15:51:57 +0000474 .addImm(AMDGPU::RSRC_DATA_FORMAT >> 32);
Tom Stellard556d9aa2013-06-03 17:39:37 +0000475 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SubRegHi)
476 .addReg(SubRegHiLo)
477 .addImm(AMDGPU::sub0)
478 .addReg(SubRegHiHi)
479 .addImm(AMDGPU::sub1);
480 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SuperReg)
481 .addReg(SubRegLo)
482 .addImm(AMDGPU::sub0_sub1)
483 .addReg(SubRegHi)
484 .addImm(AMDGPU::sub2_sub3);
485 MI->eraseFromParent();
486 break;
487 }
Matt Arsenaultdbc9aae2014-06-18 17:13:51 +0000488 case AMDGPU::V_SUB_F64: {
489 unsigned DestReg = MI->getOperand(0).getReg();
490 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F64), DestReg)
491 .addImm(0) // SRC0 modifiers
492 .addReg(MI->getOperand(1).getReg())
493 .addImm(1) // SRC1 modifiers
494 .addReg(MI->getOperand(2).getReg())
495 .addImm(0) // SRC2 modifiers
496 .addImm(0) // src2
497 .addImm(0) // CLAMP
498 .addImm(0); // OMOD
Tom Stellard2a6a61052013-07-12 18:15:08 +0000499 MI->eraseFromParent();
500 break;
Matt Arsenaultdbc9aae2014-06-18 17:13:51 +0000501 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000502 case AMDGPU::SI_RegisterStorePseudo: {
503 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Tom Stellard81d871d2013-11-13 23:36:50 +0000504 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
505 MachineInstrBuilder MIB =
506 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::SI_RegisterStore),
507 Reg);
508 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
509 MIB.addOperand(MI->getOperand(i));
510
511 MI->eraseFromParent();
Vincent Lejeune79a58342014-05-10 19:18:25 +0000512 break;
513 }
514 case AMDGPU::FABS_SI: {
515 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
516 const SIInstrInfo *TII =
517 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
518 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
519 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_MOV_B32_e32),
520 Reg)
521 .addImm(0x7fffffff);
522 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_AND_B32_e32),
523 MI->getOperand(0).getReg())
524 .addReg(MI->getOperand(1).getReg())
525 .addReg(Reg);
526 MI->eraseFromParent();
527 break;
528 }
529 case AMDGPU::FNEG_SI: {
530 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
531 const SIInstrInfo *TII =
532 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
533 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
534 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_MOV_B32_e32),
535 Reg)
536 .addImm(0x80000000);
537 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_XOR_B32_e32),
538 MI->getOperand(0).getReg())
539 .addReg(MI->getOperand(1).getReg())
540 .addReg(Reg);
541 MI->eraseFromParent();
542 break;
543 }
544 case AMDGPU::FCLAMP_SI: {
545 const SIInstrInfo *TII =
546 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
547 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F32_e64),
548 MI->getOperand(0).getReg())
Vincent Lejeune94af31f2014-05-10 19:18:33 +0000549 .addImm(0) // SRC0 modifiers
Vincent Lejeune79a58342014-05-10 19:18:25 +0000550 .addOperand(MI->getOperand(1))
Vincent Lejeune94af31f2014-05-10 19:18:33 +0000551 .addImm(0) // SRC1 modifiers
Vincent Lejeune79a58342014-05-10 19:18:25 +0000552 .addImm(0) // SRC1
Vincent Lejeune79a58342014-05-10 19:18:25 +0000553 .addImm(1) // CLAMP
Vincent Lejeune94af31f2014-05-10 19:18:33 +0000554 .addImm(0); // OMOD
Vincent Lejeune79a58342014-05-10 19:18:25 +0000555 MI->eraseFromParent();
Tom Stellard81d871d2013-11-13 23:36:50 +0000556 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000557 }
558 return BB;
559}
560
Matt Arsenault758659232013-05-18 00:21:46 +0000561EVT SITargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +0000562 if (!VT.isVector()) {
563 return MVT::i1;
564 }
565 return MVT::getVectorVT(MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +0000566}
567
Christian Konig082a14a2013-03-18 11:34:05 +0000568MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const {
569 return MVT::i32;
570}
571
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +0000572bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
573 VT = VT.getScalarType();
574
575 if (!VT.isSimple())
576 return false;
577
578 switch (VT.getSimpleVT().SimpleTy) {
579 case MVT::f32:
580 return false; /* There is V_MAD_F32 for f32 */
581 case MVT::f64:
582 return true;
583 default:
584 break;
585 }
586
587 return false;
588}
589
Tom Stellard75aadc22012-12-11 21:25:42 +0000590//===----------------------------------------------------------------------===//
591// Custom DAG Lowering Operations
592//===----------------------------------------------------------------------===//
593
594SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Michel Danzer49812b52013-07-10 16:37:07 +0000595 MachineFunction &MF = DAG.getMachineFunction();
596 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellard75aadc22012-12-11 21:25:42 +0000597 switch (Op.getOpcode()) {
598 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +0000599 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +0000600 case ISD::LOAD: {
601 LoadSDNode *Load = dyn_cast<LoadSDNode>(Op);
Tom Stellard80be9652014-02-13 23:34:10 +0000602 if (Op.getValueType().isVector() &&
603 (Load->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
604 Load->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS ||
605 (Load->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
606 Op.getValueType().getVectorNumElements() > 4))) {
Tom Stellard35bb18c2013-08-26 15:06:04 +0000607 SDValue MergedValues[2] = {
608 SplitVectorLoad(Op, DAG),
609 Load->getChain()
610 };
Craig Topper64941d92014-04-27 19:20:57 +0000611 return DAG.getMergeValues(MergedValues, SDLoc(Op));
Tom Stellard35bb18c2013-08-26 15:06:04 +0000612 } else {
Tom Stellard81d871d2013-11-13 23:36:50 +0000613 return LowerLOAD(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +0000614 }
615 }
Tom Stellardaf775432013-10-23 00:44:32 +0000616
Tom Stellard0ec134f2014-02-04 17:18:40 +0000617 case ISD::SELECT: return LowerSELECT(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +0000618 case ISD::STORE: return LowerSTORE(Op, DAG);
Michel Danzer49812b52013-07-10 16:37:07 +0000619 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +0000620 case ISD::INTRINSIC_WO_CHAIN: {
621 unsigned IntrinsicID =
622 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
623 EVT VT = Op.getValueType();
624 SDLoc DL(Op);
625 //XXX: Hardcoded we only use two to store the pointer to the parameters.
626 unsigned NumUserSGPRs = 2;
627 switch (IntrinsicID) {
628 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
629 case Intrinsic::r600_read_ngroups_x:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000630 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 0, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000631 case Intrinsic::r600_read_ngroups_y:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000632 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 4, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000633 case Intrinsic::r600_read_ngroups_z:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000634 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 8, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000635 case Intrinsic::r600_read_global_size_x:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000636 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 12, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000637 case Intrinsic::r600_read_global_size_y:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000638 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 16, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000639 case Intrinsic::r600_read_global_size_z:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000640 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 20, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000641 case Intrinsic::r600_read_local_size_x:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000642 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 24, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000643 case Intrinsic::r600_read_local_size_y:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000644 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 28, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000645 case Intrinsic::r600_read_local_size_z:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000646 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 32, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000647 case Intrinsic::r600_read_tgid_x:
648 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
649 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 0), VT);
650 case Intrinsic::r600_read_tgid_y:
651 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
652 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 1), VT);
653 case Intrinsic::r600_read_tgid_z:
654 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
655 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 2), VT);
656 case Intrinsic::r600_read_tidig_x:
657 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
658 AMDGPU::VGPR0, VT);
659 case Intrinsic::r600_read_tidig_y:
660 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
661 AMDGPU::VGPR1, VT);
662 case Intrinsic::r600_read_tidig_z:
663 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
664 AMDGPU::VGPR2, VT);
Tom Stellard9fa17912013-08-14 23:24:45 +0000665 case AMDGPUIntrinsic::SI_load_const: {
666 SDValue Ops [] = {
Tom Stellard868fd922014-04-17 21:00:11 +0000667 Op.getOperand(1),
Tom Stellard9fa17912013-08-14 23:24:45 +0000668 Op.getOperand(2)
669 };
Tom Stellard94593ee2013-06-03 17:40:18 +0000670
Benjamin Kramera8eecee2013-08-16 14:48:09 +0000671 MachineMemOperand *MMO = MF.getMachineMemOperand(
672 MachinePointerInfo(),
673 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
674 VT.getSizeInBits() / 8, 4);
Tom Stellard9fa17912013-08-14 23:24:45 +0000675 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
Craig Topper206fcd42014-04-26 19:29:41 +0000676 Op->getVTList(), Ops, VT, MMO);
Tom Stellard9fa17912013-08-14 23:24:45 +0000677 }
678 case AMDGPUIntrinsic::SI_sample:
679 return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG);
680 case AMDGPUIntrinsic::SI_sampleb:
681 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG);
682 case AMDGPUIntrinsic::SI_sampled:
683 return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG);
684 case AMDGPUIntrinsic::SI_samplel:
685 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG);
686 case AMDGPUIntrinsic::SI_vs_load_input:
687 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
Tom Stellard868fd922014-04-17 21:00:11 +0000688 Op.getOperand(1),
Tom Stellard9fa17912013-08-14 23:24:45 +0000689 Op.getOperand(2),
690 Op.getOperand(3));
Tom Stellard94593ee2013-06-03 17:40:18 +0000691 }
692 }
Tom Stellardafcf12f2013-09-12 02:55:14 +0000693
694 case ISD::INTRINSIC_VOID:
695 SDValue Chain = Op.getOperand(0);
696 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
697
698 switch (IntrinsicID) {
699 case AMDGPUIntrinsic::SI_tbuffer_store: {
700 SDLoc DL(Op);
701 SDValue Ops [] = {
702 Chain,
Tom Stellard868fd922014-04-17 21:00:11 +0000703 Op.getOperand(2),
Tom Stellardafcf12f2013-09-12 02:55:14 +0000704 Op.getOperand(3),
705 Op.getOperand(4),
706 Op.getOperand(5),
707 Op.getOperand(6),
708 Op.getOperand(7),
709 Op.getOperand(8),
710 Op.getOperand(9),
711 Op.getOperand(10),
712 Op.getOperand(11),
713 Op.getOperand(12),
714 Op.getOperand(13),
715 Op.getOperand(14)
716 };
717 EVT VT = Op.getOperand(3).getValueType();
718
719 MachineMemOperand *MMO = MF.getMachineMemOperand(
720 MachinePointerInfo(),
721 MachineMemOperand::MOStore,
722 VT.getSizeInBits() / 8, 4);
723 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
Craig Topper206fcd42014-04-26 19:29:41 +0000724 Op->getVTList(), Ops, VT, MMO);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000725 }
726 default:
727 break;
728 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000729 }
730 return SDValue();
731}
732
Tom Stellardf8794352012-12-19 22:10:31 +0000733/// \brief Helper function for LowerBRCOND
734static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000735
Tom Stellardf8794352012-12-19 22:10:31 +0000736 SDNode *Parent = Value.getNode();
737 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
738 I != E; ++I) {
739
740 if (I.getUse().get() != Value)
741 continue;
742
743 if (I->getOpcode() == Opcode)
744 return *I;
745 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000746 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000747}
748
749/// This transforms the control flow intrinsics to get the branch destination as
750/// last parameter, also switches branch target with BR if the need arise
751SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
752 SelectionDAG &DAG) const {
753
Andrew Trickef9de2a2013-05-25 02:42:55 +0000754 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +0000755
756 SDNode *Intr = BRCOND.getOperand(1).getNode();
757 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +0000758 SDNode *BR = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000759
760 if (Intr->getOpcode() == ISD::SETCC) {
761 // As long as we negate the condition everything is fine
762 SDNode *SetCC = Intr;
763 assert(SetCC->getConstantOperandVal(1) == 1);
NAKAMURA Takumi458a8272013-01-07 11:14:44 +0000764 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
765 ISD::SETNE);
Tom Stellardf8794352012-12-19 22:10:31 +0000766 Intr = SetCC->getOperand(0).getNode();
767
768 } else {
769 // Get the target from BR if we don't negate the condition
770 BR = findUser(BRCOND, ISD::BR);
771 Target = BR->getOperand(1);
772 }
773
774 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
775
776 // Build the result and
777 SmallVector<EVT, 4> Res;
778 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
779 Res.push_back(Intr->getValueType(i));
780
781 // operands of the new intrinsic call
782 SmallVector<SDValue, 4> Ops;
783 Ops.push_back(BRCOND.getOperand(0));
784 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
785 Ops.push_back(Intr->getOperand(i));
786 Ops.push_back(Target);
787
788 // build the new intrinsic call
789 SDNode *Result = DAG.getNode(
790 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +0000791 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +0000792
793 if (BR) {
794 // Give the branch instruction our target
795 SDValue Ops[] = {
796 BR->getOperand(0),
797 BRCOND.getOperand(2)
798 };
Craig Topper131de822014-04-27 19:21:16 +0000799 DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops);
Tom Stellardf8794352012-12-19 22:10:31 +0000800 }
801
802 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
803
804 // Copy the intrinsic results to registers
805 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
806 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
807 if (!CopyToReg)
808 continue;
809
810 Chain = DAG.getCopyToReg(
811 Chain, DL,
812 CopyToReg->getOperand(1),
813 SDValue(Result, i - 1),
814 SDValue());
815
816 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
817 }
818
819 // Remove the old intrinsic from the chain
820 DAG.ReplaceAllUsesOfValueWith(
821 SDValue(Intr, Intr->getNumValues() - 1),
822 Intr->getOperand(0));
823
824 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +0000825}
826
Tom Stellard81d871d2013-11-13 23:36:50 +0000827SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
828 SDLoc DL(Op);
829 LoadSDNode *Load = cast<LoadSDNode>(Op);
Tom Stellarde9373602014-01-22 19:24:14 +0000830 SDValue Ret = AMDGPUTargetLowering::LowerLOAD(Op, DAG);
831 SDValue MergedValues[2];
832 MergedValues[1] = Load->getChain();
833 if (Ret.getNode()) {
834 MergedValues[0] = Ret;
Craig Topper64941d92014-04-27 19:20:57 +0000835 return DAG.getMergeValues(MergedValues, DL);
Tom Stellarde9373602014-01-22 19:24:14 +0000836 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000837
Tom Stellarde9373602014-01-22 19:24:14 +0000838 if (Load->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
Tom Stellard81d871d2013-11-13 23:36:50 +0000839 return SDValue();
Tom Stellarde9373602014-01-22 19:24:14 +0000840 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000841
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000842 EVT MemVT = Load->getMemoryVT();
843
844 assert(!MemVT.isVector() && "Private loads should be scalarized");
845 assert(!MemVT.isFloatingPoint() && "FP loads should be promoted to int");
846
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000847 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Load->getBasePtr(),
Tom Stellard81d871d2013-11-13 23:36:50 +0000848 DAG.getConstant(2, MVT::i32));
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000849 Ret = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
Tom Stellarde9373602014-01-22 19:24:14 +0000850 Load->getChain(), Ptr,
851 DAG.getTargetConstant(0, MVT::i32),
852 Op.getOperand(2));
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000853 if (MemVT.getSizeInBits() == 64) {
854 SDValue IncPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
855 DAG.getConstant(1, MVT::i32));
856
857 SDValue LoadUpper = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
858 Load->getChain(), IncPtr,
859 DAG.getTargetConstant(0, MVT::i32),
860 Op.getOperand(2));
861
862 Ret = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ret, LoadUpper);
863 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000864
Tom Stellarde9373602014-01-22 19:24:14 +0000865 MergedValues[0] = Ret;
Craig Topper64941d92014-04-27 19:20:57 +0000866 return DAG.getMergeValues(MergedValues, DL);
Tom Stellard81d871d2013-11-13 23:36:50 +0000867
868}
869
Tom Stellard9fa17912013-08-14 23:24:45 +0000870SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode,
871 const SDValue &Op,
872 SelectionDAG &DAG) const {
873 return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1),
874 Op.getOperand(2),
Tom Stellard868fd922014-04-17 21:00:11 +0000875 Op.getOperand(3),
Tom Stellard9fa17912013-08-14 23:24:45 +0000876 Op.getOperand(4));
877}
878
Tom Stellard0ec134f2014-02-04 17:18:40 +0000879SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
880 if (Op.getValueType() != MVT::i64)
881 return SDValue();
882
883 SDLoc DL(Op);
884 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000885
886 SDValue Zero = DAG.getConstant(0, MVT::i32);
887 SDValue One = DAG.getConstant(1, MVT::i32);
888
Tom Stellard7ea3d6d2014-03-31 14:01:55 +0000889 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
890 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
891
892 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
893 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000894
895 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
896
Tom Stellard7ea3d6d2014-03-31 14:01:55 +0000897 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
898 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000899
900 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
901
Tom Stellard7ea3d6d2014-03-31 14:01:55 +0000902 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
903 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000904}
905
Tom Stellard81d871d2013-11-13 23:36:50 +0000906SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
907 SDLoc DL(Op);
908 StoreSDNode *Store = cast<StoreSDNode>(Op);
909 EVT VT = Store->getMemoryVT();
910
911 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
912 if (Ret.getNode())
913 return Ret;
914
915 if (VT.isVector() && VT.getVectorNumElements() >= 8)
916 return SplitVectorStore(Op, DAG);
917
Tom Stellard1c8788e2014-03-07 20:12:33 +0000918 if (VT == MVT::i1)
919 return DAG.getTruncStore(Store->getChain(), DL,
920 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
921 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
922
Tom Stellard81d871d2013-11-13 23:36:50 +0000923 if (Store->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS)
924 return SDValue();
925
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000926 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Store->getBasePtr(),
Tom Stellard81d871d2013-11-13 23:36:50 +0000927 DAG.getConstant(2, MVT::i32));
928 SDValue Chain = Store->getChain();
929 SmallVector<SDValue, 8> Values;
930
Tom Stellarde9373602014-01-22 19:24:14 +0000931 if (Store->isTruncatingStore()) {
932 unsigned Mask = 0;
933 if (Store->getMemoryVT() == MVT::i8) {
934 Mask = 0xff;
935 } else if (Store->getMemoryVT() == MVT::i16) {
936 Mask = 0xffff;
937 }
938 SDValue Dst = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
939 Chain, Store->getBasePtr(),
940 DAG.getConstant(0, MVT::i32));
941 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getBasePtr(),
942 DAG.getConstant(0x3, MVT::i32));
943 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
944 DAG.getConstant(3, MVT::i32));
945 SDValue MaskedValue = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getValue(),
946 DAG.getConstant(Mask, MVT::i32));
947 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
948 MaskedValue, ShiftAmt);
949 SDValue RotrAmt = DAG.getNode(ISD::SUB, DL, MVT::i32,
950 DAG.getConstant(32, MVT::i32), ShiftAmt);
951 SDValue DstMask = DAG.getNode(ISD::ROTR, DL, MVT::i32,
952 DAG.getConstant(Mask, MVT::i32),
953 RotrAmt);
954 Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
955 Dst = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
956
957 Values.push_back(Dst);
958 } else if (VT == MVT::i64) {
Tom Stellard81d871d2013-11-13 23:36:50 +0000959 for (unsigned i = 0; i < 2; ++i) {
960 Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
961 Store->getValue(), DAG.getConstant(i, MVT::i32)));
962 }
963 } else if (VT == MVT::i128) {
964 for (unsigned i = 0; i < 2; ++i) {
965 for (unsigned j = 0; j < 2; ++j) {
966 Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
967 DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64,
968 Store->getValue(), DAG.getConstant(i, MVT::i32)),
969 DAG.getConstant(j, MVT::i32)));
970 }
971 }
972 } else {
973 Values.push_back(Store->getValue());
974 }
975
976 for (unsigned i = 0; i < Values.size(); ++i) {
977 SDValue PartPtr = DAG.getNode(ISD::ADD, DL, MVT::i32,
978 Ptr, DAG.getConstant(i, MVT::i32));
979 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
980 Chain, Values[i], PartPtr,
981 DAG.getTargetConstant(0, MVT::i32));
982 }
983 return Chain;
984}
985
Tom Stellard75aadc22012-12-11 21:25:42 +0000986//===----------------------------------------------------------------------===//
987// Custom DAG optimizations
988//===----------------------------------------------------------------------===//
989
Matt Arsenault364a6742014-06-11 17:50:44 +0000990SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
991 DAGCombinerInfo &DCI) {
992 EVT VT = N->getValueType(0);
993 EVT ScalarVT = VT.getScalarType();
994 if (ScalarVT != MVT::f32)
995 return SDValue();
996
997 SelectionDAG &DAG = DCI.DAG;
998 SDLoc DL(N);
999
1000 SDValue Src = N->getOperand(0);
1001 EVT SrcVT = Src.getValueType();
1002
1003 // TODO: We could try to match extracting the higher bytes, which would be
1004 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
1005 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
1006 // about in practice.
1007 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
1008 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
1009 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
1010 DCI.AddToWorklist(Cvt.getNode());
1011 return Cvt;
1012 }
1013 }
1014
1015 // We are primarily trying to catch operations on illegal vector types
1016 // before they are expanded.
1017 // For scalars, we can use the more flexible method of checking masked bits
1018 // after legalization.
1019 if (!DCI.isBeforeLegalize() ||
1020 !SrcVT.isVector() ||
1021 SrcVT.getVectorElementType() != MVT::i8) {
1022 return SDValue();
1023 }
1024
1025 assert(DCI.isBeforeLegalize() && "Unexpected legal type");
1026
1027 // Weird sized vectors are a pain to handle, but we know 3 is really the same
1028 // size as 4.
1029 unsigned NElts = SrcVT.getVectorNumElements();
1030 if (!SrcVT.isSimple() && NElts != 3)
1031 return SDValue();
1032
1033 // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to
1034 // prevent a mess from expanding to v4i32 and repacking.
1035 if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) {
1036 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT);
1037 EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT);
1038 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts);
1039
1040 LoadSDNode *Load = cast<LoadSDNode>(Src);
1041 SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT,
1042 Load->getChain(),
1043 Load->getBasePtr(),
1044 LoadVT,
1045 Load->getMemOperand());
1046
1047 // Make sure successors of the original load stay after it by updating
1048 // them to use the new Chain.
1049 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1));
1050
1051 SmallVector<SDValue, 4> Elts;
1052 if (RegVT.isVector())
1053 DAG.ExtractVectorElements(NewLoad, Elts);
1054 else
1055 Elts.push_back(NewLoad);
1056
1057 SmallVector<SDValue, 4> Ops;
1058
1059 unsigned EltIdx = 0;
1060 for (SDValue Elt : Elts) {
1061 unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx);
1062 for (unsigned I = 0; I < ComponentsInElt; ++I) {
1063 unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I;
1064 SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt);
1065 DCI.AddToWorklist(Cvt.getNode());
1066 Ops.push_back(Cvt);
1067 }
1068
1069 ++EltIdx;
1070 }
1071
1072 assert(Ops.size() == NElts);
1073
1074 return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops);
1075 }
1076
1077 return SDValue();
1078}
1079
Tom Stellard75aadc22012-12-11 21:25:42 +00001080SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
1081 DAGCombinerInfo &DCI) const {
1082 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001083 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00001084 EVT VT = N->getValueType(0);
1085
1086 switch (N->getOpcode()) {
Tom Stellard50122a52014-04-07 19:45:41 +00001087 default: return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001088 case ISD::SELECT_CC: {
Tom Stellard75aadc22012-12-11 21:25:42 +00001089 ConstantSDNode *True, *False;
1090 // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
1091 if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1092 && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1093 && True->isAllOnesValue()
1094 && False->isNullValue()
1095 && VT == MVT::i1) {
1096 return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
1097 N->getOperand(1), N->getOperand(4));
1098
1099 }
1100 break;
1101 }
1102 case ISD::SETCC: {
1103 SDValue Arg0 = N->getOperand(0);
1104 SDValue Arg1 = N->getOperand(1);
1105 SDValue CC = N->getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00001106 ConstantSDNode * C = nullptr;
Tom Stellard75aadc22012-12-11 21:25:42 +00001107 ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
1108
1109 // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
1110 if (VT == MVT::i1
1111 && Arg0.getOpcode() == ISD::SIGN_EXTEND
1112 && Arg0.getOperand(0).getValueType() == MVT::i1
1113 && (C = dyn_cast<ConstantSDNode>(Arg1))
1114 && C->isNullValue()
1115 && CCOp == ISD::SETNE) {
1116 return SimplifySetCC(VT, Arg0.getOperand(0),
1117 DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
1118 }
1119 break;
1120 }
Matt Arsenault364a6742014-06-11 17:50:44 +00001121
1122 case AMDGPUISD::CVT_F32_UBYTE0:
1123 case AMDGPUISD::CVT_F32_UBYTE1:
1124 case AMDGPUISD::CVT_F32_UBYTE2:
1125 case AMDGPUISD::CVT_F32_UBYTE3: {
1126 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
1127
1128 SDValue Src = N->getOperand(0);
1129 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
1130
1131 APInt KnownZero, KnownOne;
1132 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
1133 !DCI.isBeforeLegalizeOps());
1134 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1135 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
1136 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
1137 DCI.CommitTargetLoweringOpt(TLO);
1138 }
1139
1140 break;
1141 }
1142
1143 case ISD::UINT_TO_FP: {
1144 return performUCharToFloatCombine(N, DCI);
1145 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001146 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00001147
1148 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001149}
Christian Konigd910b7d2013-02-26 17:52:16 +00001150
Matt Arsenault758659232013-05-18 00:21:46 +00001151/// \brief Test if RegClass is one of the VSrc classes
Christian Konigf82901a2013-02-26 17:52:23 +00001152static bool isVSrc(unsigned RegClass) {
1153 return AMDGPU::VSrc_32RegClassID == RegClass ||
1154 AMDGPU::VSrc_64RegClassID == RegClass;
1155}
1156
Matt Arsenault758659232013-05-18 00:21:46 +00001157/// \brief Test if RegClass is one of the SSrc classes
Christian Konigf82901a2013-02-26 17:52:23 +00001158static bool isSSrc(unsigned RegClass) {
1159 return AMDGPU::SSrc_32RegClassID == RegClass ||
1160 AMDGPU::SSrc_64RegClassID == RegClass;
1161}
1162
1163/// \brief Analyze the possible immediate value Op
1164///
1165/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
1166/// and the immediate value if it's a literal immediate
1167int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
1168
1169 union {
1170 int32_t I;
1171 float F;
1172 } Imm;
1173
Tom Stellardedbf1eb2013-04-05 23:31:20 +00001174 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
1175 if (Node->getZExtValue() >> 32) {
1176 return -1;
1177 }
Christian Konigf82901a2013-02-26 17:52:23 +00001178 Imm.I = Node->getSExtValue();
Tom Stellard7ed0b522014-04-03 20:19:27 +00001179 } else if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
1180 if (N->getValueType(0) != MVT::f32)
1181 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00001182 Imm.F = Node->getValueAPF().convertToFloat();
Tom Stellard7ed0b522014-04-03 20:19:27 +00001183 } else
Christian Konigf82901a2013-02-26 17:52:23 +00001184 return -1; // It isn't an immediate
1185
1186 if ((Imm.I >= -16 && Imm.I <= 64) ||
1187 Imm.F == 0.5f || Imm.F == -0.5f ||
1188 Imm.F == 1.0f || Imm.F == -1.0f ||
1189 Imm.F == 2.0f || Imm.F == -2.0f ||
1190 Imm.F == 4.0f || Imm.F == -4.0f)
1191 return 0; // It's an inline immediate
1192
1193 return Imm.I; // It's a literal immediate
1194}
1195
1196/// \brief Try to fold an immediate directly into an instruction
1197bool SITargetLowering::foldImm(SDValue &Operand, int32_t &Immediate,
1198 bool &ScalarSlotUsed) const {
1199
1200 MachineSDNode *Mov = dyn_cast<MachineSDNode>(Operand);
Bill Wendling37e9adb2013-06-07 20:28:55 +00001201 const SIInstrInfo *TII =
1202 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
Craig Topper062a2ba2014-04-25 05:30:21 +00001203 if (!Mov || !TII->isMov(Mov->getMachineOpcode()))
Christian Konigf82901a2013-02-26 17:52:23 +00001204 return false;
1205
1206 const SDValue &Op = Mov->getOperand(0);
1207 int32_t Value = analyzeImmediate(Op.getNode());
1208 if (Value == -1) {
1209 // Not an immediate at all
1210 return false;
1211
1212 } else if (Value == 0) {
1213 // Inline immediates can always be fold
1214 Operand = Op;
1215 return true;
1216
1217 } else if (Value == Immediate) {
1218 // Already fold literal immediate
1219 Operand = Op;
1220 return true;
1221
1222 } else if (!ScalarSlotUsed && !Immediate) {
1223 // Fold this literal immediate
1224 ScalarSlotUsed = true;
1225 Immediate = Value;
1226 Operand = Op;
1227 return true;
1228
1229 }
1230
1231 return false;
1232}
1233
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001234const TargetRegisterClass *SITargetLowering::getRegClassForNode(
1235 SelectionDAG &DAG, const SDValue &Op) const {
1236 const SIInstrInfo *TII =
1237 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1238 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1239
1240 if (!Op->isMachineOpcode()) {
1241 switch(Op->getOpcode()) {
1242 case ISD::CopyFromReg: {
1243 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1244 unsigned Reg = cast<RegisterSDNode>(Op->getOperand(1))->getReg();
1245 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1246 return MRI.getRegClass(Reg);
1247 }
1248 return TRI.getPhysRegClass(Reg);
1249 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001250 default: return nullptr;
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001251 }
1252 }
1253 const MCInstrDesc &Desc = TII->get(Op->getMachineOpcode());
1254 int OpClassID = Desc.OpInfo[Op.getResNo()].RegClass;
1255 if (OpClassID != -1) {
1256 return TRI.getRegClass(OpClassID);
1257 }
1258 switch(Op.getMachineOpcode()) {
1259 case AMDGPU::COPY_TO_REGCLASS:
1260 // Operand 1 is the register class id for COPY_TO_REGCLASS instructions.
1261 OpClassID = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1262
1263 // If the COPY_TO_REGCLASS instruction is copying to a VSrc register
1264 // class, then the register class for the value could be either a
1265 // VReg or and SReg. In order to get a more accurate
1266 if (OpClassID == AMDGPU::VSrc_32RegClassID ||
1267 OpClassID == AMDGPU::VSrc_64RegClassID) {
1268 return getRegClassForNode(DAG, Op.getOperand(0));
1269 }
1270 return TRI.getRegClass(OpClassID);
1271 case AMDGPU::EXTRACT_SUBREG: {
1272 int SubIdx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1273 const TargetRegisterClass *SuperClass =
1274 getRegClassForNode(DAG, Op.getOperand(0));
1275 return TRI.getSubClassWithSubReg(SuperClass, SubIdx);
1276 }
1277 case AMDGPU::REG_SEQUENCE:
1278 // Operand 0 is the register class id for REG_SEQUENCE instructions.
1279 return TRI.getRegClass(
1280 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue());
1281 default:
1282 return getRegClassFor(Op.getSimpleValueType());
1283 }
1284}
1285
Christian Konigf82901a2013-02-26 17:52:23 +00001286/// \brief Does "Op" fit into register class "RegClass" ?
Tom Stellardb35efba2013-05-20 15:02:01 +00001287bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, const SDValue &Op,
Christian Konigf82901a2013-02-26 17:52:23 +00001288 unsigned RegClass) const {
Bill Wendling37e9adb2013-06-07 20:28:55 +00001289 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001290 const TargetRegisterClass *RC = getRegClassForNode(DAG, Op);
1291 if (!RC) {
Christian Konigf82901a2013-02-26 17:52:23 +00001292 return false;
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001293 }
1294 return TRI->getRegClass(RegClass)->hasSubClassEq(RC);
Christian Konigf82901a2013-02-26 17:52:23 +00001295}
1296
1297/// \brief Make sure that we don't exeed the number of allowed scalars
1298void SITargetLowering::ensureSRegLimit(SelectionDAG &DAG, SDValue &Operand,
1299 unsigned RegClass,
1300 bool &ScalarSlotUsed) const {
1301
1302 // First map the operands register class to a destination class
1303 if (RegClass == AMDGPU::VSrc_32RegClassID)
1304 RegClass = AMDGPU::VReg_32RegClassID;
1305 else if (RegClass == AMDGPU::VSrc_64RegClassID)
1306 RegClass = AMDGPU::VReg_64RegClassID;
1307 else
1308 return;
1309
Alp Tokercb402912014-01-24 17:20:08 +00001310 // Nothing to do if they fit naturally
Christian Konigf82901a2013-02-26 17:52:23 +00001311 if (fitsRegClass(DAG, Operand, RegClass))
1312 return;
1313
1314 // If the scalar slot isn't used yet use it now
1315 if (!ScalarSlotUsed) {
1316 ScalarSlotUsed = true;
1317 return;
1318 }
1319
Matt Arsenault1408b602013-10-10 23:05:37 +00001320 // This is a conservative aproach. It is possible that we can't determine the
1321 // correct register class and copy too often, but better safe than sorry.
Christian Konigf82901a2013-02-26 17:52:23 +00001322 SDValue RC = DAG.getTargetConstant(RegClass, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001323 SDNode *Node = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, SDLoc(),
Christian Konigf82901a2013-02-26 17:52:23 +00001324 Operand.getValueType(), Operand, RC);
1325 Operand = SDValue(Node, 0);
1326}
1327
Tom Stellardacec99c2013-06-05 23:39:50 +00001328/// \returns true if \p Node's operands are different from the SDValue list
1329/// \p Ops
1330static bool isNodeChanged(const SDNode *Node, const std::vector<SDValue> &Ops) {
1331 for (unsigned i = 0, e = Node->getNumOperands(); i < e; ++i) {
1332 if (Ops[i].getNode() != Node->getOperand(i).getNode()) {
1333 return true;
1334 }
1335 }
1336 return false;
1337}
1338
Christian Konig8e06e2a2013-04-10 08:39:08 +00001339/// \brief Try to fold the Nodes operands into the Node
1340SDNode *SITargetLowering::foldOperands(MachineSDNode *Node,
1341 SelectionDAG &DAG) const {
Christian Konigf82901a2013-02-26 17:52:23 +00001342
1343 // Original encoding (either e32 or e64)
1344 int Opcode = Node->getMachineOpcode();
Bill Wendling37e9adb2013-06-07 20:28:55 +00001345 const SIInstrInfo *TII =
1346 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00001347 const MCInstrDesc *Desc = &TII->get(Opcode);
1348
1349 unsigned NumDefs = Desc->getNumDefs();
1350 unsigned NumOps = Desc->getNumOperands();
1351
Christian Konig3c145802013-03-27 09:12:59 +00001352 // Commuted opcode if available
1353 int OpcodeRev = Desc->isCommutable() ? TII->commuteOpcode(Opcode) : -1;
Craig Topper062a2ba2014-04-25 05:30:21 +00001354 const MCInstrDesc *DescRev = OpcodeRev == -1 ? nullptr : &TII->get(OpcodeRev);
Christian Konig3c145802013-03-27 09:12:59 +00001355
1356 assert(!DescRev || DescRev->getNumDefs() == NumDefs);
1357 assert(!DescRev || DescRev->getNumOperands() == NumOps);
1358
Christian Konige500e442013-02-26 17:52:47 +00001359 // e64 version if available, -1 otherwise
1360 int OpcodeE64 = AMDGPU::getVOPe64(Opcode);
Craig Topper062a2ba2014-04-25 05:30:21 +00001361 const MCInstrDesc *DescE64 = OpcodeE64 == -1 ? nullptr : &TII->get(OpcodeE64);
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001362 int InputModifiers[3] = {0};
Christian Konige500e442013-02-26 17:52:47 +00001363
1364 assert(!DescE64 || DescE64->getNumDefs() == NumDefs);
Christian Konige500e442013-02-26 17:52:47 +00001365
Christian Konigf82901a2013-02-26 17:52:23 +00001366 int32_t Immediate = Desc->getSize() == 4 ? 0 : -1;
1367 bool HaveVSrc = false, HaveSSrc = false;
1368
Matt Arsenault08d84942014-06-03 23:06:13 +00001369 // First figure out what we already have in this instruction.
Christian Konigf82901a2013-02-26 17:52:23 +00001370 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1371 i != e && Op < NumOps; ++i, ++Op) {
1372
1373 unsigned RegClass = Desc->OpInfo[Op].RegClass;
1374 if (isVSrc(RegClass))
1375 HaveVSrc = true;
1376 else if (isSSrc(RegClass))
1377 HaveSSrc = true;
1378 else
1379 continue;
1380
1381 int32_t Imm = analyzeImmediate(Node->getOperand(i).getNode());
1382 if (Imm != -1 && Imm != 0) {
1383 // Literal immediate
1384 Immediate = Imm;
1385 }
1386 }
1387
Matt Arsenault08d84942014-06-03 23:06:13 +00001388 // If we neither have VSrc nor SSrc, it makes no sense to continue.
Christian Konigf82901a2013-02-26 17:52:23 +00001389 if (!HaveVSrc && !HaveSSrc)
1390 return Node;
1391
1392 // No scalar allowed when we have both VSrc and SSrc
1393 bool ScalarSlotUsed = HaveVSrc && HaveSSrc;
1394
1395 // Second go over the operands and try to fold them
1396 std::vector<SDValue> Ops;
Christian Konige500e442013-02-26 17:52:47 +00001397 bool Promote2e64 = false;
Christian Konigf82901a2013-02-26 17:52:23 +00001398 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1399 i != e && Op < NumOps; ++i, ++Op) {
1400
1401 const SDValue &Operand = Node->getOperand(i);
1402 Ops.push_back(Operand);
1403
Matt Arsenault08d84942014-06-03 23:06:13 +00001404 // Already folded immediate?
Christian Konigf82901a2013-02-26 17:52:23 +00001405 if (isa<ConstantSDNode>(Operand.getNode()) ||
1406 isa<ConstantFPSDNode>(Operand.getNode()))
1407 continue;
1408
Matt Arsenault08d84942014-06-03 23:06:13 +00001409 // Is this a VSrc or SSrc operand?
Christian Konigf82901a2013-02-26 17:52:23 +00001410 unsigned RegClass = Desc->OpInfo[Op].RegClass;
Christian Konig8370dbb2013-03-26 14:04:17 +00001411 if (isVSrc(RegClass) || isSSrc(RegClass)) {
1412 // Try to fold the immediates
1413 if (!foldImm(Ops[i], Immediate, ScalarSlotUsed)) {
Matt Arsenault08d84942014-06-03 23:06:13 +00001414 // Folding didn't work, make sure we don't hit the SReg limit.
Christian Konig8370dbb2013-03-26 14:04:17 +00001415 ensureSRegLimit(DAG, Ops[i], RegClass, ScalarSlotUsed);
1416 }
1417 continue;
1418 }
Christian Konig6612ac32013-02-26 17:52:36 +00001419
Christian Konig3c145802013-03-27 09:12:59 +00001420 if (i == 1 && DescRev && fitsRegClass(DAG, Ops[0], RegClass)) {
Christian Konig6612ac32013-02-26 17:52:36 +00001421
Christian Konig8370dbb2013-03-26 14:04:17 +00001422 unsigned OtherRegClass = Desc->OpInfo[NumDefs].RegClass;
1423 assert(isVSrc(OtherRegClass) || isSSrc(OtherRegClass));
1424
1425 // Test if it makes sense to swap operands
1426 if (foldImm(Ops[1], Immediate, ScalarSlotUsed) ||
1427 (!fitsRegClass(DAG, Ops[1], RegClass) &&
1428 fitsRegClass(DAG, Ops[1], OtherRegClass))) {
Christian Konig6612ac32013-02-26 17:52:36 +00001429
1430 // Swap commutable operands
Matt Arsenault4be76e92014-04-07 16:44:26 +00001431 std::swap(Ops[0], Ops[1]);
Christian Konig3c145802013-03-27 09:12:59 +00001432
1433 Desc = DescRev;
Craig Topper062a2ba2014-04-25 05:30:21 +00001434 DescRev = nullptr;
Christian Konig8370dbb2013-03-26 14:04:17 +00001435 continue;
Christian Konig6612ac32013-02-26 17:52:36 +00001436 }
Christian Konig6612ac32013-02-26 17:52:36 +00001437 }
Christian Konigf82901a2013-02-26 17:52:23 +00001438
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001439 if (Immediate)
1440 continue;
1441
1442 if (DescE64) {
Christian Konig8370dbb2013-03-26 14:04:17 +00001443 // Test if it makes sense to switch to e64 encoding
1444 unsigned OtherRegClass = DescE64->OpInfo[Op].RegClass;
1445 if (!isVSrc(OtherRegClass) && !isSSrc(OtherRegClass))
1446 continue;
1447
1448 int32_t TmpImm = -1;
1449 if (foldImm(Ops[i], TmpImm, ScalarSlotUsed) ||
1450 (!fitsRegClass(DAG, Ops[i], RegClass) &&
1451 fitsRegClass(DAG, Ops[1], OtherRegClass))) {
1452
1453 // Switch to e64 encoding
1454 Immediate = -1;
1455 Promote2e64 = true;
1456 Desc = DescE64;
Craig Topper062a2ba2014-04-25 05:30:21 +00001457 DescE64 = nullptr;
Christian Konig8370dbb2013-03-26 14:04:17 +00001458 }
Christian Konigf82901a2013-02-26 17:52:23 +00001459 }
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001460
1461 if (!DescE64 && !Promote2e64)
1462 continue;
1463 if (!Operand.isMachineOpcode())
1464 continue;
1465 if (Operand.getMachineOpcode() == AMDGPU::FNEG_SI) {
1466 Ops.pop_back();
1467 Ops.push_back(Operand.getOperand(0));
1468 InputModifiers[i] = 1;
1469 Promote2e64 = true;
1470 if (!DescE64)
1471 continue;
1472 Desc = DescE64;
Matt Arsenaultc6f338d2014-06-05 00:01:12 +00001473 DescE64 = nullptr;
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001474 }
1475 else if (Operand.getMachineOpcode() == AMDGPU::FABS_SI) {
1476 Ops.pop_back();
1477 Ops.push_back(Operand.getOperand(0));
1478 InputModifiers[i] = 2;
1479 Promote2e64 = true;
1480 if (!DescE64)
1481 continue;
1482 Desc = DescE64;
Matt Arsenaultc6f338d2014-06-05 00:01:12 +00001483 DescE64 = nullptr;
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001484 }
Christian Konigf82901a2013-02-26 17:52:23 +00001485 }
1486
Christian Konige500e442013-02-26 17:52:47 +00001487 if (Promote2e64) {
Vincent Lejeune94af31f2014-05-10 19:18:33 +00001488 std::vector<SDValue> OldOps(Ops);
1489 Ops.clear();
1490 for (unsigned i = 0; i < OldOps.size(); ++i) {
1491 // src_modifier
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001492 Ops.push_back(DAG.getTargetConstant(InputModifiers[i], MVT::i32));
Vincent Lejeune94af31f2014-05-10 19:18:33 +00001493 Ops.push_back(OldOps[i]);
1494 }
Christian Konige500e442013-02-26 17:52:47 +00001495 // Add the modifier flags while promoting
Vincent Lejeune94af31f2014-05-10 19:18:33 +00001496 for (unsigned i = 0; i < 2; ++i)
Christian Konige500e442013-02-26 17:52:47 +00001497 Ops.push_back(DAG.getTargetConstant(0, MVT::i32));
1498 }
1499
Christian Konigf82901a2013-02-26 17:52:23 +00001500 // Add optional chain and glue
1501 for (unsigned i = NumOps - NumDefs, e = Node->getNumOperands(); i < e; ++i)
1502 Ops.push_back(Node->getOperand(i));
1503
Tom Stellardb5a97002013-06-03 17:39:50 +00001504 // Nodes that have a glue result are not CSE'd by getMachineNode(), so in
1505 // this case a brand new node is always be created, even if the operands
1506 // are the same as before. So, manually check if anything has been changed.
Tom Stellardacec99c2013-06-05 23:39:50 +00001507 if (Desc->Opcode == Opcode && !isNodeChanged(Node, Ops)) {
1508 return Node;
Tom Stellardb5a97002013-06-03 17:39:50 +00001509 }
1510
Christian Konig3c145802013-03-27 09:12:59 +00001511 // Create a complete new instruction
Andrew Trickef9de2a2013-05-25 02:42:55 +00001512 return DAG.getMachineNode(Desc->Opcode, SDLoc(Node), Node->getVTList(), Ops);
Christian Konigd910b7d2013-02-26 17:52:16 +00001513}
Christian Konig8e06e2a2013-04-10 08:39:08 +00001514
1515/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00001516static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00001517 switch (Idx) {
1518 default: return 0;
1519 case AMDGPU::sub0: return 0;
1520 case AMDGPU::sub1: return 1;
1521 case AMDGPU::sub2: return 2;
1522 case AMDGPU::sub3: return 3;
1523 }
1524}
1525
1526/// \brief Adjust the writemask of MIMG instructions
1527void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
1528 SelectionDAG &DAG) const {
1529 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00001530 unsigned Lane = 0;
1531 unsigned OldDmask = Node->getConstantOperandVal(0);
1532 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001533
1534 // Try to figure out the used register components
1535 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
1536 I != E; ++I) {
1537
1538 // Abort if we can't understand the usage
1539 if (!I->isMachineOpcode() ||
1540 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
1541 return;
1542
Tom Stellard54774e52013-10-23 02:53:47 +00001543 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
1544 // Note that subregs are packed, i.e. Lane==0 is the first bit set
1545 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
1546 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00001547 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00001548
Tom Stellard54774e52013-10-23 02:53:47 +00001549 // Set which texture component corresponds to the lane.
1550 unsigned Comp;
1551 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
1552 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00001553 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00001554 Dmask &= ~(1 << Comp);
1555 }
1556
Christian Konig8e06e2a2013-04-10 08:39:08 +00001557 // Abort if we have more than one user per component
1558 if (Users[Lane])
1559 return;
1560
1561 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00001562 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001563 }
1564
Tom Stellard54774e52013-10-23 02:53:47 +00001565 // Abort if there's no change
1566 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00001567 return;
1568
1569 // Adjust the writemask in the node
1570 std::vector<SDValue> Ops;
Tom Stellard54774e52013-10-23 02:53:47 +00001571 Ops.push_back(DAG.getTargetConstant(NewDmask, MVT::i32));
Christian Konig8e06e2a2013-04-10 08:39:08 +00001572 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1573 Ops.push_back(Node->getOperand(i));
Craig Topper8c0b4d02014-04-28 05:57:50 +00001574 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00001575
Christian Konig8b1ed282013-04-10 08:39:16 +00001576 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00001577 // (if NewDmask has only one bit set...)
1578 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Christian Konig8b1ed282013-04-10 08:39:16 +00001579 SDValue RC = DAG.getTargetConstant(AMDGPU::VReg_32RegClassID, MVT::i32);
1580 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001581 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00001582 SDValue(Node, 0), RC);
1583 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
1584 return;
1585 }
1586
Christian Konig8e06e2a2013-04-10 08:39:08 +00001587 // Update the users of the node with the new indices
1588 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
1589
1590 SDNode *User = Users[i];
1591 if (!User)
1592 continue;
1593
1594 SDValue Op = DAG.getTargetConstant(Idx, MVT::i32);
1595 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
1596
1597 switch (Idx) {
1598 default: break;
1599 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
1600 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
1601 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
1602 }
1603 }
1604}
1605
Matt Arsenault08d84942014-06-03 23:06:13 +00001606/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00001607SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
1608 SelectionDAG &DAG) const {
Tom Stellard16a9a202013-08-14 23:24:17 +00001609 const SIInstrInfo *TII =
1610 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
Tom Stellard0518ff82013-06-03 17:39:58 +00001611 Node = AdjustRegClass(Node, DAG);
Christian Konig8e06e2a2013-04-10 08:39:08 +00001612
Tom Stellard16a9a202013-08-14 23:24:17 +00001613 if (TII->isMIMG(Node->getMachineOpcode()))
Christian Konig8e06e2a2013-04-10 08:39:08 +00001614 adjustWritemask(Node, DAG);
1615
1616 return foldOperands(Node, DAG);
1617}
Christian Konig8b1ed282013-04-10 08:39:16 +00001618
1619/// \brief Assign the register class depending on the number of
1620/// bits set in the writemask
1621void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
1622 SDNode *Node) const {
Tom Stellard16a9a202013-08-14 23:24:17 +00001623 const SIInstrInfo *TII =
1624 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1625 if (!TII->isMIMG(MI->getOpcode()))
Christian Konig8b1ed282013-04-10 08:39:16 +00001626 return;
1627
1628 unsigned VReg = MI->getOperand(0).getReg();
1629 unsigned Writemask = MI->getOperand(1).getImm();
1630 unsigned BitsSet = 0;
1631 for (unsigned i = 0; i < 4; ++i)
1632 BitsSet += Writemask & (1 << i) ? 1 : 0;
1633
1634 const TargetRegisterClass *RC;
1635 switch (BitsSet) {
1636 default: return;
1637 case 1: RC = &AMDGPU::VReg_32RegClass; break;
1638 case 2: RC = &AMDGPU::VReg_64RegClass; break;
1639 case 3: RC = &AMDGPU::VReg_96RegClass; break;
1640 }
1641
Tom Stellard682bfbc2013-10-10 17:11:24 +00001642 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
1643 MI->setDesc(TII->get(NewOpcode));
Christian Konig8b1ed282013-04-10 08:39:16 +00001644 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
1645 MRI.setRegClass(VReg, RC);
1646}
Tom Stellard0518ff82013-06-03 17:39:58 +00001647
1648MachineSDNode *SITargetLowering::AdjustRegClass(MachineSDNode *N,
1649 SelectionDAG &DAG) const {
1650
1651 SDLoc DL(N);
1652 unsigned NewOpcode = N->getMachineOpcode();
1653
1654 switch (N->getMachineOpcode()) {
1655 default: return N;
Tom Stellard0518ff82013-06-03 17:39:58 +00001656 case AMDGPU::S_LOAD_DWORD_IMM:
1657 NewOpcode = AMDGPU::BUFFER_LOAD_DWORD_ADDR64;
1658 // Fall-through
1659 case AMDGPU::S_LOAD_DWORDX2_SGPR:
1660 if (NewOpcode == N->getMachineOpcode()) {
1661 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64;
1662 }
1663 // Fall-through
1664 case AMDGPU::S_LOAD_DWORDX4_IMM:
1665 case AMDGPU::S_LOAD_DWORDX4_SGPR: {
1666 if (NewOpcode == N->getMachineOpcode()) {
1667 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64;
1668 }
1669 if (fitsRegClass(DAG, N->getOperand(0), AMDGPU::SReg_64RegClassID)) {
1670 return N;
1671 }
1672 ConstantSDNode *Offset = cast<ConstantSDNode>(N->getOperand(1));
1673 SDValue Ops[] = {
1674 SDValue(DAG.getMachineNode(AMDGPU::SI_ADDR64_RSRC, DL, MVT::i128,
1675 DAG.getConstant(0, MVT::i64)), 0),
1676 N->getOperand(0),
1677 DAG.getConstant(Offset->getSExtValue() << 2, MVT::i32)
1678 };
1679 return DAG.getMachineNode(NewOpcode, DL, N->getVTList(), Ops);
1680 }
1681 }
1682}
Tom Stellard94593ee2013-06-03 17:40:18 +00001683
1684SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
1685 const TargetRegisterClass *RC,
1686 unsigned Reg, EVT VT) const {
1687 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
1688
1689 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
1690 cast<RegisterSDNode>(VReg)->getReg(), VT);
1691}