blob: 63d3d3852178749da95784b2ea3fb6e8516daf3d [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);
Matt Arsenaultb8b51532014-06-23 18:00:38 +000080 setOperationAction(ISD::SUBC, MVT::i32, Legal);
81 setOperationAction(ISD::SUBE, MVT::i32, Legal);
Aaron Watrydaabb202013-06-25 13:55:52 +000082
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);
Tom Stellard81d871d2013-11-13 23:36:50 +000094 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
95 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellard967bf582014-02-13 23:34:15 +000096 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +000097
Tom Stellard1c8788e2014-03-07 20:12:33 +000098 setOperationAction(ISD::STORE, MVT::i1, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +000099 setOperationAction(ISD::STORE, MVT::i32, 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);
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000139 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000140
Matt Arsenault470acd82014-04-15 22:28:39 +0000141 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Tom Stellarde9373602014-01-22 19:24:14 +0000142 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Custom);
143 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Custom);
Matt Arsenault470acd82014-04-15 22:28:39 +0000144 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000145 setLoadExtAction(ISD::SEXTLOAD, MVT::v8i16, Expand);
146 setLoadExtAction(ISD::SEXTLOAD, MVT::v16i16, Expand);
Tom Stellard31209cc2013-07-15 19:00:09 +0000147
Matt Arsenault470acd82014-04-15 22:28:39 +0000148 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
149 setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Custom);
150 setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Custom);
151 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Expand);
152
153 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
Tom Stellarde9373602014-01-22 19:24:14 +0000154 setLoadExtAction(ISD::EXTLOAD, MVT::i8, Custom);
155 setLoadExtAction(ISD::EXTLOAD, MVT::i16, Custom);
156 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Expand);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000157 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000158
Tom Stellarde9373602014-01-22 19:24:14 +0000159 setTruncStoreAction(MVT::i32, MVT::i8, Custom);
160 setTruncStoreAction(MVT::i32, MVT::i16, Custom);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000161 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Matt Arsenault6f243792013-09-05 19:41:10 +0000162 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000163 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
164 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000165
Matt Arsenault470acd82014-04-15 22:28:39 +0000166 setOperationAction(ISD::LOAD, MVT::i1, Custom);
167
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000168 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Expand);
169 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
170
Tom Stellardfd155822013-08-26 15:05:36 +0000171 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Tom Stellard04c0e982014-01-22 19:24:21 +0000172 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000173 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
Michel Danzer49812b52013-07-10 16:37:07 +0000174
Tom Stellard5f337882014-04-29 23:12:43 +0000175 // These should use UDIVREM, so set them to expand
176 setOperationAction(ISD::UDIV, MVT::i64, Expand);
177 setOperationAction(ISD::UREM, MVT::i64, Expand);
178
Tom Stellard967bf582014-02-13 23:34:15 +0000179 // We only support LOAD/STORE and vector manipulation ops for vectors
180 // with > 4 elements.
181 MVT VecTypes[] = {
Tom Stellardd61a1c32014-02-28 21:36:37 +0000182 MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32
Tom Stellard967bf582014-02-13 23:34:15 +0000183 };
184
Matt Arsenaultd504a742014-05-15 21:44:05 +0000185 for (MVT VT : VecTypes) {
Tom Stellard967bf582014-02-13 23:34:15 +0000186 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
187 switch(Op) {
188 case ISD::LOAD:
189 case ISD::STORE:
190 case ISD::BUILD_VECTOR:
191 case ISD::BITCAST:
192 case ISD::EXTRACT_VECTOR_ELT:
193 case ISD::INSERT_VECTOR_ELT:
194 case ISD::CONCAT_VECTORS:
195 case ISD::INSERT_SUBVECTOR:
196 case ISD::EXTRACT_SUBVECTOR:
197 break;
198 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000199 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000200 break;
201 }
202 }
203 }
204
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000205 for (int I = MVT::v1f64; I <= MVT::v8f64; ++I) {
206 MVT::SimpleValueType VT = static_cast<MVT::SimpleValueType>(I);
Matt Arsenaulta81aee82014-02-24 21:16:50 +0000207 setOperationAction(ISD::FTRUNC, VT, Expand);
208 setOperationAction(ISD::FCEIL, VT, Expand);
209 setOperationAction(ISD::FFLOOR, VT, Expand);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000210 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000211
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000212 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
213 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
214 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
215 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
Matt Arsenaulta90d22f2014-04-17 17:06:37 +0000216 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000217 }
218
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000219 // FIXME: These should be removed and handled the same was as f32 fneg. Source
Matt Arsenault7aeb8132014-06-18 17:05:22 +0000220 // modifiers also work for the double instructions.
221 setOperationAction(ISD::FNEG, MVT::f64, Expand);
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000222 setOperationAction(ISD::FABS, MVT::f64, Expand);
Matt Arsenault7aeb8132014-06-18 17:05:22 +0000223
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000224 setTargetDAGCombine(ISD::SELECT_CC);
Tom Stellard75aadc22012-12-11 21:25:42 +0000225 setTargetDAGCombine(ISD::SETCC);
Michel Danzerf52a6722013-03-08 10:58:01 +0000226
Matt Arsenault364a6742014-06-11 17:50:44 +0000227 setTargetDAGCombine(ISD::UINT_TO_FP);
228
Christian Konigeecebd02013-03-26 14:04:02 +0000229 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000230}
231
Tom Stellard0125f2a2013-06-25 02:39:35 +0000232//===----------------------------------------------------------------------===//
233// TargetLowering queries
234//===----------------------------------------------------------------------===//
235
236bool SITargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
Matt Arsenault25793a32014-02-05 23:15:53 +0000237 unsigned AddrSpace,
Tom Stellard0125f2a2013-06-25 02:39:35 +0000238 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000239 if (IsFast)
240 *IsFast = false;
241
Tom Stellard0125f2a2013-06-25 02:39:35 +0000242 // XXX: This depends on the address space and also we may want to revist
243 // the alignment values we specify in the DataLayout.
Matt Arsenault1018c892014-04-24 17:08:26 +0000244
245 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
246 // which isn't a simple VT.
Tom Stellard81d871d2013-11-13 23:36:50 +0000247 if (!VT.isSimple() || VT == MVT::Other)
248 return false;
Matt Arsenault1018c892014-04-24 17:08:26 +0000249
250 // XXX - CI changes say "Support for unaligned memory accesses" but I don't
251 // see what for specifically. The wording everywhere else seems to be the
252 // same.
253
254 // 3.6.4 - Operations using pairs of VGPRs (for example: double-floats) have
255 // no alignment restrictions.
256 if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS) {
257 // Using any pair of GPRs should be the same as any other pair.
258 if (IsFast)
259 *IsFast = true;
260 return VT.bitsGE(MVT::i64);
261 }
262
263 // XXX - The only mention I see of this in the ISA manual is for LDS direct
264 // reads the "byte address and must be dword aligned". Is it also true for the
265 // normal loads and stores?
266 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS)
267 return false;
268
269 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
270 // byte-address are ignored, thus forcing Dword alignment.
271 if (IsFast)
272 *IsFast = true;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000273 return VT.bitsGT(MVT::i32);
274}
275
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000276TargetLoweringBase::LegalizeTypeAction
277SITargetLowering::getPreferredVectorAction(EVT VT) const {
278 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
279 return TypeSplitVector;
280
281 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000282}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000283
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000284bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
285 Type *Ty) const {
286 const SIInstrInfo *TII =
287 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
288 return TII->isInlineConstant(Imm);
289}
290
Tom Stellardaf775432013-10-23 00:44:32 +0000291SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
Tom Stellard94593ee2013-06-03 17:40:18 +0000292 SDLoc DL, SDValue Chain,
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000293 unsigned Offset, bool Signed) const {
Tom Stellard94593ee2013-06-03 17:40:18 +0000294 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
295 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
296 AMDGPUAS::CONSTANT_ADDRESS);
Tom Stellard94593ee2013-06-03 17:40:18 +0000297 SDValue BasePtr = DAG.getCopyFromReg(Chain, DL,
298 MRI.getLiveInVirtReg(AMDGPU::SGPR0_SGPR1), MVT::i64);
299 SDValue Ptr = DAG.getNode(ISD::ADD, DL, MVT::i64, BasePtr,
300 DAG.getConstant(Offset, MVT::i64));
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000301 return DAG.getExtLoad(Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD, DL, VT, Chain, Ptr,
Tom Stellardaf775432013-10-23 00:44:32 +0000302 MachinePointerInfo(UndefValue::get(PtrTy)), MemVT,
303 false, false, MemVT.getSizeInBits() >> 3);
Tom Stellard94593ee2013-06-03 17:40:18 +0000304
305}
306
Christian Konig2c8f6d52013-03-07 09:03:52 +0000307SDValue SITargetLowering::LowerFormalArguments(
308 SDValue Chain,
309 CallingConv::ID CallConv,
310 bool isVarArg,
311 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000312 SDLoc DL, SelectionDAG &DAG,
Christian Konig2c8f6d52013-03-07 09:03:52 +0000313 SmallVectorImpl<SDValue> &InVals) const {
314
315 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
316
317 MachineFunction &MF = DAG.getMachineFunction();
318 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000319 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000320
321 assert(CallConv == CallingConv::C);
322
323 SmallVector<ISD::InputArg, 16> Splits;
Christian Konig99ee0f42013-03-07 09:04:14 +0000324 uint32_t Skipped = 0;
325
326 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000327 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000328
329 // First check if it's a PS input addr
Vincent Lejeuned6236442013-10-13 17:56:16 +0000330 if (Info->ShaderType == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
331 !Arg.Flags.isByVal()) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000332
333 assert((PSInputNum <= 15) && "Too many PS inputs!");
334
335 if (!Arg.Used) {
336 // We can savely skip PS inputs
337 Skipped |= 1 << i;
338 ++PSInputNum;
339 continue;
340 }
341
342 Info->PSInputAddr |= 1 << PSInputNum++;
343 }
344
345 // Second split vertices into their elements
Tom Stellarded882c22013-06-03 17:40:11 +0000346 if (Info->ShaderType != ShaderType::COMPUTE && Arg.VT.isVector()) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000347 ISD::InputArg NewArg = Arg;
348 NewArg.Flags.setSplit();
349 NewArg.VT = Arg.VT.getVectorElementType();
350
351 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
352 // three or five element vertex only needs three or five registers,
353 // NOT four or eigth.
354 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
355 unsigned NumElements = ParamType->getVectorNumElements();
356
357 for (unsigned j = 0; j != NumElements; ++j) {
358 Splits.push_back(NewArg);
359 NewArg.PartOffset += NewArg.VT.getStoreSize();
360 }
361
Tom Stellardaf775432013-10-23 00:44:32 +0000362 } else if (Info->ShaderType != ShaderType::COMPUTE) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000363 Splits.push_back(Arg);
364 }
365 }
366
367 SmallVector<CCValAssign, 16> ArgLocs;
368 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
369 getTargetMachine(), ArgLocs, *DAG.getContext());
370
Christian Konig99ee0f42013-03-07 09:04:14 +0000371 // At least one interpolation mode must be enabled or else the GPU will hang.
372 if (Info->ShaderType == ShaderType::PIXEL && (Info->PSInputAddr & 0x7F) == 0) {
373 Info->PSInputAddr |= 1;
374 CCInfo.AllocateReg(AMDGPU::VGPR0);
375 CCInfo.AllocateReg(AMDGPU::VGPR1);
376 }
377
Tom Stellarded882c22013-06-03 17:40:11 +0000378 // The pointer to the list of arguments is stored in SGPR0, SGPR1
379 if (Info->ShaderType == ShaderType::COMPUTE) {
380 CCInfo.AllocateReg(AMDGPU::SGPR0);
381 CCInfo.AllocateReg(AMDGPU::SGPR1);
Tom Stellard94593ee2013-06-03 17:40:18 +0000382 MF.addLiveIn(AMDGPU::SGPR0_SGPR1, &AMDGPU::SReg_64RegClass);
Tom Stellarded882c22013-06-03 17:40:11 +0000383 }
384
Tom Stellardaf775432013-10-23 00:44:32 +0000385 if (Info->ShaderType == ShaderType::COMPUTE) {
386 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
387 Splits);
388 }
389
Christian Konig2c8f6d52013-03-07 09:03:52 +0000390 AnalyzeFormalArguments(CCInfo, Splits);
391
392 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
393
Christian Konigb7be72d2013-05-17 09:46:48 +0000394 const ISD::InputArg &Arg = Ins[i];
Christian Konig99ee0f42013-03-07 09:04:14 +0000395 if (Skipped & (1 << i)) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000396 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000397 continue;
398 }
399
Christian Konig2c8f6d52013-03-07 09:03:52 +0000400 CCValAssign &VA = ArgLocs[ArgIdx++];
Tom Stellarded882c22013-06-03 17:40:11 +0000401 EVT VT = VA.getLocVT();
402
403 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000404 VT = Ins[i].VT;
405 EVT MemVT = Splits[i].VT;
Tom Stellard94593ee2013-06-03 17:40:18 +0000406 // The first 36 bytes of the input buffer contains information about
407 // thread group and global sizes.
Tom Stellardaf775432013-10-23 00:44:32 +0000408 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, DAG.getRoot(),
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000409 36 + VA.getLocMemOffset(),
410 Ins[i].Flags.isSExt());
Tom Stellarded882c22013-06-03 17:40:11 +0000411 InVals.push_back(Arg);
412 continue;
413 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000414 assert(VA.isRegLoc() && "Parameter must be in a register!");
415
416 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000417
418 if (VT == MVT::i64) {
419 // For now assume it is a pointer
420 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
421 &AMDGPU::SReg_64RegClass);
422 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
423 InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
424 continue;
425 }
426
427 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
428
429 Reg = MF.addLiveIn(Reg, RC);
430 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
431
Christian Konig2c8f6d52013-03-07 09:03:52 +0000432 if (Arg.VT.isVector()) {
433
434 // Build a vector from the registers
435 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
436 unsigned NumElements = ParamType->getVectorNumElements();
437
438 SmallVector<SDValue, 4> Regs;
439 Regs.push_back(Val);
440 for (unsigned j = 1; j != NumElements; ++j) {
441 Reg = ArgLocs[ArgIdx++].getLocReg();
442 Reg = MF.addLiveIn(Reg, RC);
443 Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
444 }
445
446 // Fill up the missing vector elements
447 NumElements = Arg.VT.getVectorNumElements() - NumElements;
448 for (unsigned j = 0; j != NumElements; ++j)
449 Regs.push_back(DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000450
Craig Topper48d114b2014-04-26 18:35:24 +0000451 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000452 continue;
453 }
454
455 InVals.push_back(Val);
456 }
457 return Chain;
458}
459
Tom Stellard75aadc22012-12-11 21:25:42 +0000460MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
461 MachineInstr * MI, MachineBasicBlock * BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000462
Tom Stellard556d9aa2013-06-03 17:39:37 +0000463 MachineBasicBlock::iterator I = *MI;
Tom Stellard919bb6b2014-04-29 23:12:53 +0000464 const SIInstrInfo *TII =
465 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
466 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Tom Stellard556d9aa2013-06-03 17:39:37 +0000467
Tom Stellard75aadc22012-12-11 21:25:42 +0000468 switch (MI->getOpcode()) {
469 default:
470 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
471 case AMDGPU::BRANCH: return BB;
Tom Stellard556d9aa2013-06-03 17:39:37 +0000472 case AMDGPU::SI_ADDR64_RSRC: {
Tom Stellard556d9aa2013-06-03 17:39:37 +0000473 unsigned SuperReg = MI->getOperand(0).getReg();
Tom Stellarddef38c52014-03-21 15:51:53 +0000474 unsigned SubRegLo = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass);
475 unsigned SubRegHi = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass);
476 unsigned SubRegHiHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
477 unsigned SubRegHiLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
Tom Stellard556d9aa2013-06-03 17:39:37 +0000478 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B64), SubRegLo)
479 .addOperand(MI->getOperand(1));
480 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiLo)
481 .addImm(0);
482 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiHi)
Tom Stellard15834092014-03-21 15:51:57 +0000483 .addImm(AMDGPU::RSRC_DATA_FORMAT >> 32);
Tom Stellard556d9aa2013-06-03 17:39:37 +0000484 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SubRegHi)
485 .addReg(SubRegHiLo)
486 .addImm(AMDGPU::sub0)
487 .addReg(SubRegHiHi)
488 .addImm(AMDGPU::sub1);
489 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SuperReg)
490 .addReg(SubRegLo)
491 .addImm(AMDGPU::sub0_sub1)
492 .addReg(SubRegHi)
493 .addImm(AMDGPU::sub2_sub3);
494 MI->eraseFromParent();
495 break;
496 }
Matt Arsenaultdbc9aae2014-06-18 17:13:51 +0000497 case AMDGPU::V_SUB_F64: {
498 unsigned DestReg = MI->getOperand(0).getReg();
499 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F64), DestReg)
500 .addImm(0) // SRC0 modifiers
501 .addReg(MI->getOperand(1).getReg())
502 .addImm(1) // SRC1 modifiers
503 .addReg(MI->getOperand(2).getReg())
504 .addImm(0) // SRC2 modifiers
505 .addImm(0) // src2
506 .addImm(0) // CLAMP
507 .addImm(0); // OMOD
Tom Stellard2a6a61052013-07-12 18:15:08 +0000508 MI->eraseFromParent();
509 break;
Matt Arsenaultdbc9aae2014-06-18 17:13:51 +0000510 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000511 case AMDGPU::SI_RegisterStorePseudo: {
512 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Tom Stellard81d871d2013-11-13 23:36:50 +0000513 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
514 MachineInstrBuilder MIB =
515 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::SI_RegisterStore),
516 Reg);
517 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
518 MIB.addOperand(MI->getOperand(i));
519
520 MI->eraseFromParent();
Vincent Lejeune79a58342014-05-10 19:18:25 +0000521 break;
522 }
523 case AMDGPU::FABS_SI: {
524 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
525 const SIInstrInfo *TII =
526 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
527 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
528 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_MOV_B32_e32),
529 Reg)
530 .addImm(0x7fffffff);
531 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_AND_B32_e32),
532 MI->getOperand(0).getReg())
533 .addReg(MI->getOperand(1).getReg())
534 .addReg(Reg);
535 MI->eraseFromParent();
536 break;
537 }
538 case AMDGPU::FNEG_SI: {
539 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
540 const SIInstrInfo *TII =
541 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
542 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
543 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_MOV_B32_e32),
544 Reg)
545 .addImm(0x80000000);
546 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_XOR_B32_e32),
547 MI->getOperand(0).getReg())
548 .addReg(MI->getOperand(1).getReg())
549 .addReg(Reg);
550 MI->eraseFromParent();
551 break;
552 }
553 case AMDGPU::FCLAMP_SI: {
554 const SIInstrInfo *TII =
555 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
556 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F32_e64),
557 MI->getOperand(0).getReg())
Vincent Lejeune94af31f2014-05-10 19:18:33 +0000558 .addImm(0) // SRC0 modifiers
Vincent Lejeune79a58342014-05-10 19:18:25 +0000559 .addOperand(MI->getOperand(1))
Vincent Lejeune94af31f2014-05-10 19:18:33 +0000560 .addImm(0) // SRC1 modifiers
Vincent Lejeune79a58342014-05-10 19:18:25 +0000561 .addImm(0) // SRC1
Vincent Lejeune79a58342014-05-10 19:18:25 +0000562 .addImm(1) // CLAMP
Vincent Lejeune94af31f2014-05-10 19:18:33 +0000563 .addImm(0); // OMOD
Vincent Lejeune79a58342014-05-10 19:18:25 +0000564 MI->eraseFromParent();
Tom Stellard81d871d2013-11-13 23:36:50 +0000565 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000566 }
567 return BB;
568}
569
Matt Arsenault758659232013-05-18 00:21:46 +0000570EVT SITargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +0000571 if (!VT.isVector()) {
572 return MVT::i1;
573 }
574 return MVT::getVectorVT(MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +0000575}
576
Christian Konig082a14a2013-03-18 11:34:05 +0000577MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const {
578 return MVT::i32;
579}
580
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +0000581bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
582 VT = VT.getScalarType();
583
584 if (!VT.isSimple())
585 return false;
586
587 switch (VT.getSimpleVT().SimpleTy) {
588 case MVT::f32:
589 return false; /* There is V_MAD_F32 for f32 */
590 case MVT::f64:
591 return true;
592 default:
593 break;
594 }
595
596 return false;
597}
598
Tom Stellard75aadc22012-12-11 21:25:42 +0000599//===----------------------------------------------------------------------===//
600// Custom DAG Lowering Operations
601//===----------------------------------------------------------------------===//
602
603SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Michel Danzer49812b52013-07-10 16:37:07 +0000604 MachineFunction &MF = DAG.getMachineFunction();
605 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellard75aadc22012-12-11 21:25:42 +0000606 switch (Op.getOpcode()) {
607 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +0000608 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +0000609 case ISD::LOAD: {
610 LoadSDNode *Load = dyn_cast<LoadSDNode>(Op);
Tom Stellard10ae6a02014-07-02 20:53:54 +0000611 EVT VT = Op.getValueType();
612
613 // These loads are legal.
614 if (Load->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS &&
615 VT.isVector() && VT.getVectorNumElements() == 2 &&
616 VT.getVectorElementType() == MVT::i32)
617 return SDValue();
618
Tom Stellard80be9652014-02-13 23:34:10 +0000619 if (Op.getValueType().isVector() &&
620 (Load->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
621 Load->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS ||
622 (Load->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
623 Op.getValueType().getVectorNumElements() > 4))) {
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000624 return SplitVectorLoad(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +0000625 } else {
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000626 SDValue Result = LowerLOAD(Op, DAG);
627 assert((!Result.getNode() ||
628 Result.getNode()->getNumValues() == 2) &&
629 "Load should return a value and a chain");
630 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +0000631 }
632 }
Tom Stellardaf775432013-10-23 00:44:32 +0000633
Tom Stellard0ec134f2014-02-04 17:18:40 +0000634 case ISD::SELECT: return LowerSELECT(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +0000635 case ISD::STORE: return LowerSTORE(Op, DAG);
Michel Danzer49812b52013-07-10 16:37:07 +0000636 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +0000637 case ISD::INTRINSIC_WO_CHAIN: {
638 unsigned IntrinsicID =
639 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
640 EVT VT = Op.getValueType();
641 SDLoc DL(Op);
642 //XXX: Hardcoded we only use two to store the pointer to the parameters.
643 unsigned NumUserSGPRs = 2;
644 switch (IntrinsicID) {
645 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
646 case Intrinsic::r600_read_ngroups_x:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000647 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 0, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000648 case Intrinsic::r600_read_ngroups_y:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000649 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 4, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000650 case Intrinsic::r600_read_ngroups_z:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000651 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 8, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000652 case Intrinsic::r600_read_global_size_x:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000653 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 12, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000654 case Intrinsic::r600_read_global_size_y:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000655 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 16, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000656 case Intrinsic::r600_read_global_size_z:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000657 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 20, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000658 case Intrinsic::r600_read_local_size_x:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000659 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 24, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000660 case Intrinsic::r600_read_local_size_y:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000661 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 28, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000662 case Intrinsic::r600_read_local_size_z:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000663 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 32, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000664 case Intrinsic::r600_read_tgid_x:
665 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
666 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 0), VT);
667 case Intrinsic::r600_read_tgid_y:
668 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
669 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 1), VT);
670 case Intrinsic::r600_read_tgid_z:
671 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
672 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 2), VT);
673 case Intrinsic::r600_read_tidig_x:
674 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
675 AMDGPU::VGPR0, VT);
676 case Intrinsic::r600_read_tidig_y:
677 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
678 AMDGPU::VGPR1, VT);
679 case Intrinsic::r600_read_tidig_z:
680 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
681 AMDGPU::VGPR2, VT);
Tom Stellard9fa17912013-08-14 23:24:45 +0000682 case AMDGPUIntrinsic::SI_load_const: {
683 SDValue Ops [] = {
Tom Stellard868fd922014-04-17 21:00:11 +0000684 Op.getOperand(1),
Tom Stellard9fa17912013-08-14 23:24:45 +0000685 Op.getOperand(2)
686 };
Tom Stellard94593ee2013-06-03 17:40:18 +0000687
Benjamin Kramera8eecee2013-08-16 14:48:09 +0000688 MachineMemOperand *MMO = MF.getMachineMemOperand(
689 MachinePointerInfo(),
690 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
691 VT.getSizeInBits() / 8, 4);
Tom Stellard9fa17912013-08-14 23:24:45 +0000692 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
Craig Topper206fcd42014-04-26 19:29:41 +0000693 Op->getVTList(), Ops, VT, MMO);
Tom Stellard9fa17912013-08-14 23:24:45 +0000694 }
695 case AMDGPUIntrinsic::SI_sample:
696 return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG);
697 case AMDGPUIntrinsic::SI_sampleb:
698 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG);
699 case AMDGPUIntrinsic::SI_sampled:
700 return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG);
701 case AMDGPUIntrinsic::SI_samplel:
702 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG);
703 case AMDGPUIntrinsic::SI_vs_load_input:
704 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
Tom Stellard868fd922014-04-17 21:00:11 +0000705 Op.getOperand(1),
Tom Stellard9fa17912013-08-14 23:24:45 +0000706 Op.getOperand(2),
707 Op.getOperand(3));
Tom Stellard94593ee2013-06-03 17:40:18 +0000708 }
709 }
Tom Stellardafcf12f2013-09-12 02:55:14 +0000710
711 case ISD::INTRINSIC_VOID:
712 SDValue Chain = Op.getOperand(0);
713 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
714
715 switch (IntrinsicID) {
716 case AMDGPUIntrinsic::SI_tbuffer_store: {
717 SDLoc DL(Op);
718 SDValue Ops [] = {
719 Chain,
Tom Stellard868fd922014-04-17 21:00:11 +0000720 Op.getOperand(2),
Tom Stellardafcf12f2013-09-12 02:55:14 +0000721 Op.getOperand(3),
722 Op.getOperand(4),
723 Op.getOperand(5),
724 Op.getOperand(6),
725 Op.getOperand(7),
726 Op.getOperand(8),
727 Op.getOperand(9),
728 Op.getOperand(10),
729 Op.getOperand(11),
730 Op.getOperand(12),
731 Op.getOperand(13),
732 Op.getOperand(14)
733 };
734 EVT VT = Op.getOperand(3).getValueType();
735
736 MachineMemOperand *MMO = MF.getMachineMemOperand(
737 MachinePointerInfo(),
738 MachineMemOperand::MOStore,
739 VT.getSizeInBits() / 8, 4);
740 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
Craig Topper206fcd42014-04-26 19:29:41 +0000741 Op->getVTList(), Ops, VT, MMO);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000742 }
743 default:
744 break;
745 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000746 }
747 return SDValue();
748}
749
Tom Stellardf8794352012-12-19 22:10:31 +0000750/// \brief Helper function for LowerBRCOND
751static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000752
Tom Stellardf8794352012-12-19 22:10:31 +0000753 SDNode *Parent = Value.getNode();
754 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
755 I != E; ++I) {
756
757 if (I.getUse().get() != Value)
758 continue;
759
760 if (I->getOpcode() == Opcode)
761 return *I;
762 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000763 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000764}
765
766/// This transforms the control flow intrinsics to get the branch destination as
767/// last parameter, also switches branch target with BR if the need arise
768SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
769 SelectionDAG &DAG) const {
770
Andrew Trickef9de2a2013-05-25 02:42:55 +0000771 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +0000772
773 SDNode *Intr = BRCOND.getOperand(1).getNode();
774 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +0000775 SDNode *BR = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000776
777 if (Intr->getOpcode() == ISD::SETCC) {
778 // As long as we negate the condition everything is fine
779 SDNode *SetCC = Intr;
780 assert(SetCC->getConstantOperandVal(1) == 1);
NAKAMURA Takumi458a8272013-01-07 11:14:44 +0000781 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
782 ISD::SETNE);
Tom Stellardf8794352012-12-19 22:10:31 +0000783 Intr = SetCC->getOperand(0).getNode();
784
785 } else {
786 // Get the target from BR if we don't negate the condition
787 BR = findUser(BRCOND, ISD::BR);
788 Target = BR->getOperand(1);
789 }
790
791 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
792
793 // Build the result and
794 SmallVector<EVT, 4> Res;
795 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
796 Res.push_back(Intr->getValueType(i));
797
798 // operands of the new intrinsic call
799 SmallVector<SDValue, 4> Ops;
800 Ops.push_back(BRCOND.getOperand(0));
801 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
802 Ops.push_back(Intr->getOperand(i));
803 Ops.push_back(Target);
804
805 // build the new intrinsic call
806 SDNode *Result = DAG.getNode(
807 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +0000808 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +0000809
810 if (BR) {
811 // Give the branch instruction our target
812 SDValue Ops[] = {
813 BR->getOperand(0),
814 BRCOND.getOperand(2)
815 };
Craig Topper131de822014-04-27 19:21:16 +0000816 DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops);
Tom Stellardf8794352012-12-19 22:10:31 +0000817 }
818
819 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
820
821 // Copy the intrinsic results to registers
822 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
823 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
824 if (!CopyToReg)
825 continue;
826
827 Chain = DAG.getCopyToReg(
828 Chain, DL,
829 CopyToReg->getOperand(1),
830 SDValue(Result, i - 1),
831 SDValue());
832
833 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
834 }
835
836 // Remove the old intrinsic from the chain
837 DAG.ReplaceAllUsesOfValueWith(
838 SDValue(Intr, Intr->getNumValues() - 1),
839 Intr->getOperand(0));
840
841 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +0000842}
843
Tom Stellard81d871d2013-11-13 23:36:50 +0000844SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
845 SDLoc DL(Op);
846 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000847 SDValue Lowered = AMDGPUTargetLowering::LowerLOAD(Op, DAG);
848 if (Lowered.getNode())
849 return Lowered;
Tom Stellard81d871d2013-11-13 23:36:50 +0000850
Tom Stellarde9373602014-01-22 19:24:14 +0000851 if (Load->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
Tom Stellard81d871d2013-11-13 23:36:50 +0000852 return SDValue();
Tom Stellarde9373602014-01-22 19:24:14 +0000853 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000854
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000855 EVT MemVT = Load->getMemoryVT();
856
857 assert(!MemVT.isVector() && "Private loads should be scalarized");
858 assert(!MemVT.isFloatingPoint() && "FP loads should be promoted to int");
859
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000860 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Load->getBasePtr(),
Tom Stellard81d871d2013-11-13 23:36:50 +0000861 DAG.getConstant(2, MVT::i32));
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000862
863 // FIXME: REGISTER_LOAD should probably have a chain result.
864 SDValue Chain = Load->getChain();
865 SDValue LoLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
866 Chain, Ptr,
867 DAG.getTargetConstant(0, MVT::i32),
868 Op.getOperand(2));
869
870 SDValue Ret = LoLoad.getValue(0);
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000871 if (MemVT.getSizeInBits() == 64) {
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000872 // TODO: This needs a test to make sure the right thing is happening with
873 // the chain. That is hard without general function support.
874
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000875 SDValue IncPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
876 DAG.getConstant(1, MVT::i32));
877
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000878 SDValue HiLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
879 Chain, IncPtr,
880 DAG.getTargetConstant(0, MVT::i32),
881 Op.getOperand(2));
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000882
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000883 Ret = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, LoLoad, HiLoad);
884 // Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
885 // LoLoad.getValue(1), HiLoad.getValue(1));
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000886 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000887
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000888 SDValue Ops[] = {
889 Ret,
890 Chain
891 };
Tom Stellard81d871d2013-11-13 23:36:50 +0000892
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000893 return DAG.getMergeValues(Ops, DL);
Tom Stellard81d871d2013-11-13 23:36:50 +0000894}
895
Tom Stellard9fa17912013-08-14 23:24:45 +0000896SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode,
897 const SDValue &Op,
898 SelectionDAG &DAG) const {
899 return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1),
900 Op.getOperand(2),
Tom Stellard868fd922014-04-17 21:00:11 +0000901 Op.getOperand(3),
Tom Stellard9fa17912013-08-14 23:24:45 +0000902 Op.getOperand(4));
903}
904
Tom Stellard0ec134f2014-02-04 17:18:40 +0000905SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
906 if (Op.getValueType() != MVT::i64)
907 return SDValue();
908
909 SDLoc DL(Op);
910 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000911
912 SDValue Zero = DAG.getConstant(0, MVT::i32);
913 SDValue One = DAG.getConstant(1, MVT::i32);
914
Tom Stellard7ea3d6d2014-03-31 14:01:55 +0000915 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
916 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
917
918 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
919 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000920
921 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
922
Tom Stellard7ea3d6d2014-03-31 14:01:55 +0000923 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
924 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000925
926 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
927
Tom Stellard7ea3d6d2014-03-31 14:01:55 +0000928 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
929 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000930}
931
Tom Stellard81d871d2013-11-13 23:36:50 +0000932SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
933 SDLoc DL(Op);
934 StoreSDNode *Store = cast<StoreSDNode>(Op);
935 EVT VT = Store->getMemoryVT();
936
Tom Stellard9b3816b2014-06-24 23:33:04 +0000937 // These stores are legal.
938 if (Store->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS &&
939 VT.isVector() && VT.getVectorNumElements() == 2 &&
940 VT.getVectorElementType() == MVT::i32)
941 return SDValue();
942
Tom Stellard81d871d2013-11-13 23:36:50 +0000943 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
944 if (Ret.getNode())
945 return Ret;
946
947 if (VT.isVector() && VT.getVectorNumElements() >= 8)
948 return SplitVectorStore(Op, DAG);
949
Tom Stellard1c8788e2014-03-07 20:12:33 +0000950 if (VT == MVT::i1)
951 return DAG.getTruncStore(Store->getChain(), DL,
952 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
953 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
954
Tom Stellard81d871d2013-11-13 23:36:50 +0000955 if (Store->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS)
956 return SDValue();
957
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000958 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Store->getBasePtr(),
Tom Stellard81d871d2013-11-13 23:36:50 +0000959 DAG.getConstant(2, MVT::i32));
960 SDValue Chain = Store->getChain();
961 SmallVector<SDValue, 8> Values;
962
Tom Stellarde9373602014-01-22 19:24:14 +0000963 if (Store->isTruncatingStore()) {
964 unsigned Mask = 0;
965 if (Store->getMemoryVT() == MVT::i8) {
966 Mask = 0xff;
967 } else if (Store->getMemoryVT() == MVT::i16) {
968 Mask = 0xffff;
969 }
970 SDValue Dst = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
971 Chain, Store->getBasePtr(),
972 DAG.getConstant(0, MVT::i32));
973 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getBasePtr(),
974 DAG.getConstant(0x3, MVT::i32));
975 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
976 DAG.getConstant(3, MVT::i32));
977 SDValue MaskedValue = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getValue(),
978 DAG.getConstant(Mask, MVT::i32));
979 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
980 MaskedValue, ShiftAmt);
981 SDValue RotrAmt = DAG.getNode(ISD::SUB, DL, MVT::i32,
982 DAG.getConstant(32, MVT::i32), ShiftAmt);
983 SDValue DstMask = DAG.getNode(ISD::ROTR, DL, MVT::i32,
984 DAG.getConstant(Mask, MVT::i32),
985 RotrAmt);
986 Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
987 Dst = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
988
989 Values.push_back(Dst);
990 } else if (VT == MVT::i64) {
Tom Stellard81d871d2013-11-13 23:36:50 +0000991 for (unsigned i = 0; i < 2; ++i) {
992 Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
993 Store->getValue(), DAG.getConstant(i, MVT::i32)));
994 }
995 } else if (VT == MVT::i128) {
996 for (unsigned i = 0; i < 2; ++i) {
997 for (unsigned j = 0; j < 2; ++j) {
998 Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
999 DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64,
1000 Store->getValue(), DAG.getConstant(i, MVT::i32)),
1001 DAG.getConstant(j, MVT::i32)));
1002 }
1003 }
1004 } else {
1005 Values.push_back(Store->getValue());
1006 }
1007
1008 for (unsigned i = 0; i < Values.size(); ++i) {
1009 SDValue PartPtr = DAG.getNode(ISD::ADD, DL, MVT::i32,
1010 Ptr, DAG.getConstant(i, MVT::i32));
1011 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1012 Chain, Values[i], PartPtr,
1013 DAG.getTargetConstant(0, MVT::i32));
1014 }
1015 return Chain;
1016}
1017
Tom Stellard75aadc22012-12-11 21:25:42 +00001018//===----------------------------------------------------------------------===//
1019// Custom DAG optimizations
1020//===----------------------------------------------------------------------===//
1021
Matt Arsenault364a6742014-06-11 17:50:44 +00001022SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
1023 DAGCombinerInfo &DCI) {
1024 EVT VT = N->getValueType(0);
1025 EVT ScalarVT = VT.getScalarType();
1026 if (ScalarVT != MVT::f32)
1027 return SDValue();
1028
1029 SelectionDAG &DAG = DCI.DAG;
1030 SDLoc DL(N);
1031
1032 SDValue Src = N->getOperand(0);
1033 EVT SrcVT = Src.getValueType();
1034
1035 // TODO: We could try to match extracting the higher bytes, which would be
1036 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
1037 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
1038 // about in practice.
1039 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
1040 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
1041 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
1042 DCI.AddToWorklist(Cvt.getNode());
1043 return Cvt;
1044 }
1045 }
1046
1047 // We are primarily trying to catch operations on illegal vector types
1048 // before they are expanded.
1049 // For scalars, we can use the more flexible method of checking masked bits
1050 // after legalization.
1051 if (!DCI.isBeforeLegalize() ||
1052 !SrcVT.isVector() ||
1053 SrcVT.getVectorElementType() != MVT::i8) {
1054 return SDValue();
1055 }
1056
1057 assert(DCI.isBeforeLegalize() && "Unexpected legal type");
1058
1059 // Weird sized vectors are a pain to handle, but we know 3 is really the same
1060 // size as 4.
1061 unsigned NElts = SrcVT.getVectorNumElements();
1062 if (!SrcVT.isSimple() && NElts != 3)
1063 return SDValue();
1064
1065 // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to
1066 // prevent a mess from expanding to v4i32 and repacking.
1067 if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) {
1068 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT);
1069 EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT);
1070 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts);
1071
1072 LoadSDNode *Load = cast<LoadSDNode>(Src);
1073 SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT,
1074 Load->getChain(),
1075 Load->getBasePtr(),
1076 LoadVT,
1077 Load->getMemOperand());
1078
1079 // Make sure successors of the original load stay after it by updating
1080 // them to use the new Chain.
1081 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1));
1082
1083 SmallVector<SDValue, 4> Elts;
1084 if (RegVT.isVector())
1085 DAG.ExtractVectorElements(NewLoad, Elts);
1086 else
1087 Elts.push_back(NewLoad);
1088
1089 SmallVector<SDValue, 4> Ops;
1090
1091 unsigned EltIdx = 0;
1092 for (SDValue Elt : Elts) {
1093 unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx);
1094 for (unsigned I = 0; I < ComponentsInElt; ++I) {
1095 unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I;
1096 SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt);
1097 DCI.AddToWorklist(Cvt.getNode());
1098 Ops.push_back(Cvt);
1099 }
1100
1101 ++EltIdx;
1102 }
1103
1104 assert(Ops.size() == NElts);
1105
1106 return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops);
1107 }
1108
1109 return SDValue();
1110}
1111
Tom Stellard75aadc22012-12-11 21:25:42 +00001112SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
1113 DAGCombinerInfo &DCI) const {
1114 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001115 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00001116 EVT VT = N->getValueType(0);
1117
1118 switch (N->getOpcode()) {
Tom Stellard50122a52014-04-07 19:45:41 +00001119 default: return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001120 case ISD::SELECT_CC: {
Tom Stellard75aadc22012-12-11 21:25:42 +00001121 ConstantSDNode *True, *False;
1122 // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
1123 if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1124 && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1125 && True->isAllOnesValue()
1126 && False->isNullValue()
1127 && VT == MVT::i1) {
1128 return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
1129 N->getOperand(1), N->getOperand(4));
1130
1131 }
1132 break;
1133 }
1134 case ISD::SETCC: {
1135 SDValue Arg0 = N->getOperand(0);
1136 SDValue Arg1 = N->getOperand(1);
1137 SDValue CC = N->getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00001138 ConstantSDNode * C = nullptr;
Tom Stellard75aadc22012-12-11 21:25:42 +00001139 ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
1140
1141 // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
1142 if (VT == MVT::i1
1143 && Arg0.getOpcode() == ISD::SIGN_EXTEND
1144 && Arg0.getOperand(0).getValueType() == MVT::i1
1145 && (C = dyn_cast<ConstantSDNode>(Arg1))
1146 && C->isNullValue()
1147 && CCOp == ISD::SETNE) {
1148 return SimplifySetCC(VT, Arg0.getOperand(0),
1149 DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
1150 }
1151 break;
1152 }
Matt Arsenault364a6742014-06-11 17:50:44 +00001153
1154 case AMDGPUISD::CVT_F32_UBYTE0:
1155 case AMDGPUISD::CVT_F32_UBYTE1:
1156 case AMDGPUISD::CVT_F32_UBYTE2:
1157 case AMDGPUISD::CVT_F32_UBYTE3: {
1158 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
1159
1160 SDValue Src = N->getOperand(0);
1161 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
1162
1163 APInt KnownZero, KnownOne;
1164 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
1165 !DCI.isBeforeLegalizeOps());
1166 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1167 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
1168 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
1169 DCI.CommitTargetLoweringOpt(TLO);
1170 }
1171
1172 break;
1173 }
1174
1175 case ISD::UINT_TO_FP: {
1176 return performUCharToFloatCombine(N, DCI);
1177 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001178 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00001179
1180 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001181}
Christian Konigd910b7d2013-02-26 17:52:16 +00001182
Matt Arsenault758659232013-05-18 00:21:46 +00001183/// \brief Test if RegClass is one of the VSrc classes
Christian Konigf82901a2013-02-26 17:52:23 +00001184static bool isVSrc(unsigned RegClass) {
1185 return AMDGPU::VSrc_32RegClassID == RegClass ||
1186 AMDGPU::VSrc_64RegClassID == RegClass;
1187}
1188
Matt Arsenault758659232013-05-18 00:21:46 +00001189/// \brief Test if RegClass is one of the SSrc classes
Christian Konigf82901a2013-02-26 17:52:23 +00001190static bool isSSrc(unsigned RegClass) {
1191 return AMDGPU::SSrc_32RegClassID == RegClass ||
1192 AMDGPU::SSrc_64RegClassID == RegClass;
1193}
1194
1195/// \brief Analyze the possible immediate value Op
1196///
1197/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
1198/// and the immediate value if it's a literal immediate
1199int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
1200
1201 union {
1202 int32_t I;
1203 float F;
1204 } Imm;
1205
Tom Stellardedbf1eb2013-04-05 23:31:20 +00001206 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
1207 if (Node->getZExtValue() >> 32) {
1208 return -1;
1209 }
Christian Konigf82901a2013-02-26 17:52:23 +00001210 Imm.I = Node->getSExtValue();
Tom Stellard7ed0b522014-04-03 20:19:27 +00001211 } else if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
1212 if (N->getValueType(0) != MVT::f32)
1213 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00001214 Imm.F = Node->getValueAPF().convertToFloat();
Tom Stellard7ed0b522014-04-03 20:19:27 +00001215 } else
Christian Konigf82901a2013-02-26 17:52:23 +00001216 return -1; // It isn't an immediate
1217
1218 if ((Imm.I >= -16 && Imm.I <= 64) ||
1219 Imm.F == 0.5f || Imm.F == -0.5f ||
1220 Imm.F == 1.0f || Imm.F == -1.0f ||
1221 Imm.F == 2.0f || Imm.F == -2.0f ||
1222 Imm.F == 4.0f || Imm.F == -4.0f)
1223 return 0; // It's an inline immediate
1224
1225 return Imm.I; // It's a literal immediate
1226}
1227
1228/// \brief Try to fold an immediate directly into an instruction
1229bool SITargetLowering::foldImm(SDValue &Operand, int32_t &Immediate,
1230 bool &ScalarSlotUsed) const {
1231
1232 MachineSDNode *Mov = dyn_cast<MachineSDNode>(Operand);
Bill Wendling37e9adb2013-06-07 20:28:55 +00001233 const SIInstrInfo *TII =
1234 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
Craig Topper062a2ba2014-04-25 05:30:21 +00001235 if (!Mov || !TII->isMov(Mov->getMachineOpcode()))
Christian Konigf82901a2013-02-26 17:52:23 +00001236 return false;
1237
1238 const SDValue &Op = Mov->getOperand(0);
1239 int32_t Value = analyzeImmediate(Op.getNode());
1240 if (Value == -1) {
1241 // Not an immediate at all
1242 return false;
1243
1244 } else if (Value == 0) {
1245 // Inline immediates can always be fold
1246 Operand = Op;
1247 return true;
1248
1249 } else if (Value == Immediate) {
1250 // Already fold literal immediate
1251 Operand = Op;
1252 return true;
1253
1254 } else if (!ScalarSlotUsed && !Immediate) {
1255 // Fold this literal immediate
1256 ScalarSlotUsed = true;
1257 Immediate = Value;
1258 Operand = Op;
1259 return true;
1260
1261 }
1262
1263 return false;
1264}
1265
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001266const TargetRegisterClass *SITargetLowering::getRegClassForNode(
1267 SelectionDAG &DAG, const SDValue &Op) const {
1268 const SIInstrInfo *TII =
1269 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1270 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1271
1272 if (!Op->isMachineOpcode()) {
1273 switch(Op->getOpcode()) {
1274 case ISD::CopyFromReg: {
1275 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1276 unsigned Reg = cast<RegisterSDNode>(Op->getOperand(1))->getReg();
1277 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1278 return MRI.getRegClass(Reg);
1279 }
1280 return TRI.getPhysRegClass(Reg);
1281 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001282 default: return nullptr;
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001283 }
1284 }
1285 const MCInstrDesc &Desc = TII->get(Op->getMachineOpcode());
1286 int OpClassID = Desc.OpInfo[Op.getResNo()].RegClass;
1287 if (OpClassID != -1) {
1288 return TRI.getRegClass(OpClassID);
1289 }
1290 switch(Op.getMachineOpcode()) {
1291 case AMDGPU::COPY_TO_REGCLASS:
1292 // Operand 1 is the register class id for COPY_TO_REGCLASS instructions.
1293 OpClassID = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1294
1295 // If the COPY_TO_REGCLASS instruction is copying to a VSrc register
1296 // class, then the register class for the value could be either a
1297 // VReg or and SReg. In order to get a more accurate
1298 if (OpClassID == AMDGPU::VSrc_32RegClassID ||
1299 OpClassID == AMDGPU::VSrc_64RegClassID) {
1300 return getRegClassForNode(DAG, Op.getOperand(0));
1301 }
1302 return TRI.getRegClass(OpClassID);
1303 case AMDGPU::EXTRACT_SUBREG: {
1304 int SubIdx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1305 const TargetRegisterClass *SuperClass =
1306 getRegClassForNode(DAG, Op.getOperand(0));
1307 return TRI.getSubClassWithSubReg(SuperClass, SubIdx);
1308 }
1309 case AMDGPU::REG_SEQUENCE:
1310 // Operand 0 is the register class id for REG_SEQUENCE instructions.
1311 return TRI.getRegClass(
1312 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue());
1313 default:
1314 return getRegClassFor(Op.getSimpleValueType());
1315 }
1316}
1317
Christian Konigf82901a2013-02-26 17:52:23 +00001318/// \brief Does "Op" fit into register class "RegClass" ?
Tom Stellardb35efba2013-05-20 15:02:01 +00001319bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, const SDValue &Op,
Christian Konigf82901a2013-02-26 17:52:23 +00001320 unsigned RegClass) const {
Bill Wendling37e9adb2013-06-07 20:28:55 +00001321 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001322 const TargetRegisterClass *RC = getRegClassForNode(DAG, Op);
1323 if (!RC) {
Christian Konigf82901a2013-02-26 17:52:23 +00001324 return false;
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001325 }
1326 return TRI->getRegClass(RegClass)->hasSubClassEq(RC);
Christian Konigf82901a2013-02-26 17:52:23 +00001327}
1328
1329/// \brief Make sure that we don't exeed the number of allowed scalars
1330void SITargetLowering::ensureSRegLimit(SelectionDAG &DAG, SDValue &Operand,
1331 unsigned RegClass,
1332 bool &ScalarSlotUsed) const {
1333
1334 // First map the operands register class to a destination class
1335 if (RegClass == AMDGPU::VSrc_32RegClassID)
1336 RegClass = AMDGPU::VReg_32RegClassID;
1337 else if (RegClass == AMDGPU::VSrc_64RegClassID)
1338 RegClass = AMDGPU::VReg_64RegClassID;
1339 else
1340 return;
1341
Alp Tokercb402912014-01-24 17:20:08 +00001342 // Nothing to do if they fit naturally
Christian Konigf82901a2013-02-26 17:52:23 +00001343 if (fitsRegClass(DAG, Operand, RegClass))
1344 return;
1345
1346 // If the scalar slot isn't used yet use it now
1347 if (!ScalarSlotUsed) {
1348 ScalarSlotUsed = true;
1349 return;
1350 }
1351
Matt Arsenault1408b602013-10-10 23:05:37 +00001352 // This is a conservative aproach. It is possible that we can't determine the
1353 // correct register class and copy too often, but better safe than sorry.
Christian Konigf82901a2013-02-26 17:52:23 +00001354 SDValue RC = DAG.getTargetConstant(RegClass, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001355 SDNode *Node = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, SDLoc(),
Christian Konigf82901a2013-02-26 17:52:23 +00001356 Operand.getValueType(), Operand, RC);
1357 Operand = SDValue(Node, 0);
1358}
1359
Tom Stellardacec99c2013-06-05 23:39:50 +00001360/// \returns true if \p Node's operands are different from the SDValue list
1361/// \p Ops
1362static bool isNodeChanged(const SDNode *Node, const std::vector<SDValue> &Ops) {
1363 for (unsigned i = 0, e = Node->getNumOperands(); i < e; ++i) {
1364 if (Ops[i].getNode() != Node->getOperand(i).getNode()) {
1365 return true;
1366 }
1367 }
1368 return false;
1369}
1370
Christian Konig8e06e2a2013-04-10 08:39:08 +00001371/// \brief Try to fold the Nodes operands into the Node
1372SDNode *SITargetLowering::foldOperands(MachineSDNode *Node,
1373 SelectionDAG &DAG) const {
Christian Konigf82901a2013-02-26 17:52:23 +00001374
1375 // Original encoding (either e32 or e64)
1376 int Opcode = Node->getMachineOpcode();
Bill Wendling37e9adb2013-06-07 20:28:55 +00001377 const SIInstrInfo *TII =
1378 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00001379 const MCInstrDesc *Desc = &TII->get(Opcode);
1380
1381 unsigned NumDefs = Desc->getNumDefs();
1382 unsigned NumOps = Desc->getNumOperands();
1383
Christian Konig3c145802013-03-27 09:12:59 +00001384 // Commuted opcode if available
1385 int OpcodeRev = Desc->isCommutable() ? TII->commuteOpcode(Opcode) : -1;
Craig Topper062a2ba2014-04-25 05:30:21 +00001386 const MCInstrDesc *DescRev = OpcodeRev == -1 ? nullptr : &TII->get(OpcodeRev);
Christian Konig3c145802013-03-27 09:12:59 +00001387
1388 assert(!DescRev || DescRev->getNumDefs() == NumDefs);
1389 assert(!DescRev || DescRev->getNumOperands() == NumOps);
1390
Christian Konige500e442013-02-26 17:52:47 +00001391 // e64 version if available, -1 otherwise
1392 int OpcodeE64 = AMDGPU::getVOPe64(Opcode);
Craig Topper062a2ba2014-04-25 05:30:21 +00001393 const MCInstrDesc *DescE64 = OpcodeE64 == -1 ? nullptr : &TII->get(OpcodeE64);
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001394 int InputModifiers[3] = {0};
Christian Konige500e442013-02-26 17:52:47 +00001395
1396 assert(!DescE64 || DescE64->getNumDefs() == NumDefs);
Christian Konige500e442013-02-26 17:52:47 +00001397
Christian Konigf82901a2013-02-26 17:52:23 +00001398 int32_t Immediate = Desc->getSize() == 4 ? 0 : -1;
1399 bool HaveVSrc = false, HaveSSrc = false;
1400
Matt Arsenault08d84942014-06-03 23:06:13 +00001401 // First figure out what we already have in this instruction.
Christian Konigf82901a2013-02-26 17:52:23 +00001402 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1403 i != e && Op < NumOps; ++i, ++Op) {
1404
1405 unsigned RegClass = Desc->OpInfo[Op].RegClass;
1406 if (isVSrc(RegClass))
1407 HaveVSrc = true;
1408 else if (isSSrc(RegClass))
1409 HaveSSrc = true;
1410 else
1411 continue;
1412
1413 int32_t Imm = analyzeImmediate(Node->getOperand(i).getNode());
1414 if (Imm != -1 && Imm != 0) {
1415 // Literal immediate
1416 Immediate = Imm;
1417 }
1418 }
1419
Matt Arsenault08d84942014-06-03 23:06:13 +00001420 // If we neither have VSrc nor SSrc, it makes no sense to continue.
Christian Konigf82901a2013-02-26 17:52:23 +00001421 if (!HaveVSrc && !HaveSSrc)
1422 return Node;
1423
1424 // No scalar allowed when we have both VSrc and SSrc
1425 bool ScalarSlotUsed = HaveVSrc && HaveSSrc;
1426
1427 // Second go over the operands and try to fold them
1428 std::vector<SDValue> Ops;
Christian Konige500e442013-02-26 17:52:47 +00001429 bool Promote2e64 = false;
Christian Konigf82901a2013-02-26 17:52:23 +00001430 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1431 i != e && Op < NumOps; ++i, ++Op) {
1432
1433 const SDValue &Operand = Node->getOperand(i);
1434 Ops.push_back(Operand);
1435
Matt Arsenault08d84942014-06-03 23:06:13 +00001436 // Already folded immediate?
Christian Konigf82901a2013-02-26 17:52:23 +00001437 if (isa<ConstantSDNode>(Operand.getNode()) ||
1438 isa<ConstantFPSDNode>(Operand.getNode()))
1439 continue;
1440
Matt Arsenault08d84942014-06-03 23:06:13 +00001441 // Is this a VSrc or SSrc operand?
Christian Konigf82901a2013-02-26 17:52:23 +00001442 unsigned RegClass = Desc->OpInfo[Op].RegClass;
Christian Konig8370dbb2013-03-26 14:04:17 +00001443 if (isVSrc(RegClass) || isSSrc(RegClass)) {
1444 // Try to fold the immediates
1445 if (!foldImm(Ops[i], Immediate, ScalarSlotUsed)) {
Matt Arsenault08d84942014-06-03 23:06:13 +00001446 // Folding didn't work, make sure we don't hit the SReg limit.
Christian Konig8370dbb2013-03-26 14:04:17 +00001447 ensureSRegLimit(DAG, Ops[i], RegClass, ScalarSlotUsed);
1448 }
1449 continue;
1450 }
Christian Konig6612ac32013-02-26 17:52:36 +00001451
Christian Konig3c145802013-03-27 09:12:59 +00001452 if (i == 1 && DescRev && fitsRegClass(DAG, Ops[0], RegClass)) {
Christian Konig6612ac32013-02-26 17:52:36 +00001453
Christian Konig8370dbb2013-03-26 14:04:17 +00001454 unsigned OtherRegClass = Desc->OpInfo[NumDefs].RegClass;
1455 assert(isVSrc(OtherRegClass) || isSSrc(OtherRegClass));
1456
1457 // Test if it makes sense to swap operands
1458 if (foldImm(Ops[1], Immediate, ScalarSlotUsed) ||
1459 (!fitsRegClass(DAG, Ops[1], RegClass) &&
1460 fitsRegClass(DAG, Ops[1], OtherRegClass))) {
Christian Konig6612ac32013-02-26 17:52:36 +00001461
1462 // Swap commutable operands
Matt Arsenault4be76e92014-04-07 16:44:26 +00001463 std::swap(Ops[0], Ops[1]);
Christian Konig3c145802013-03-27 09:12:59 +00001464
1465 Desc = DescRev;
Craig Topper062a2ba2014-04-25 05:30:21 +00001466 DescRev = nullptr;
Christian Konig8370dbb2013-03-26 14:04:17 +00001467 continue;
Christian Konig6612ac32013-02-26 17:52:36 +00001468 }
Christian Konig6612ac32013-02-26 17:52:36 +00001469 }
Christian Konigf82901a2013-02-26 17:52:23 +00001470
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001471 if (Immediate)
1472 continue;
1473
1474 if (DescE64) {
Christian Konig8370dbb2013-03-26 14:04:17 +00001475 // Test if it makes sense to switch to e64 encoding
1476 unsigned OtherRegClass = DescE64->OpInfo[Op].RegClass;
1477 if (!isVSrc(OtherRegClass) && !isSSrc(OtherRegClass))
1478 continue;
1479
1480 int32_t TmpImm = -1;
1481 if (foldImm(Ops[i], TmpImm, ScalarSlotUsed) ||
1482 (!fitsRegClass(DAG, Ops[i], RegClass) &&
1483 fitsRegClass(DAG, Ops[1], OtherRegClass))) {
1484
1485 // Switch to e64 encoding
1486 Immediate = -1;
1487 Promote2e64 = true;
1488 Desc = DescE64;
Craig Topper062a2ba2014-04-25 05:30:21 +00001489 DescE64 = nullptr;
Christian Konig8370dbb2013-03-26 14:04:17 +00001490 }
Christian Konigf82901a2013-02-26 17:52:23 +00001491 }
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001492
1493 if (!DescE64 && !Promote2e64)
1494 continue;
1495 if (!Operand.isMachineOpcode())
1496 continue;
1497 if (Operand.getMachineOpcode() == AMDGPU::FNEG_SI) {
1498 Ops.pop_back();
1499 Ops.push_back(Operand.getOperand(0));
1500 InputModifiers[i] = 1;
1501 Promote2e64 = true;
1502 if (!DescE64)
1503 continue;
1504 Desc = DescE64;
Matt Arsenaultc6f338d2014-06-05 00:01:12 +00001505 DescE64 = nullptr;
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001506 }
1507 else if (Operand.getMachineOpcode() == AMDGPU::FABS_SI) {
1508 Ops.pop_back();
1509 Ops.push_back(Operand.getOperand(0));
1510 InputModifiers[i] = 2;
1511 Promote2e64 = true;
1512 if (!DescE64)
1513 continue;
1514 Desc = DescE64;
Matt Arsenaultc6f338d2014-06-05 00:01:12 +00001515 DescE64 = nullptr;
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001516 }
Christian Konigf82901a2013-02-26 17:52:23 +00001517 }
1518
Christian Konige500e442013-02-26 17:52:47 +00001519 if (Promote2e64) {
Vincent Lejeune94af31f2014-05-10 19:18:33 +00001520 std::vector<SDValue> OldOps(Ops);
1521 Ops.clear();
1522 for (unsigned i = 0; i < OldOps.size(); ++i) {
1523 // src_modifier
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001524 Ops.push_back(DAG.getTargetConstant(InputModifiers[i], MVT::i32));
Vincent Lejeune94af31f2014-05-10 19:18:33 +00001525 Ops.push_back(OldOps[i]);
1526 }
Christian Konige500e442013-02-26 17:52:47 +00001527 // Add the modifier flags while promoting
Vincent Lejeune94af31f2014-05-10 19:18:33 +00001528 for (unsigned i = 0; i < 2; ++i)
Christian Konige500e442013-02-26 17:52:47 +00001529 Ops.push_back(DAG.getTargetConstant(0, MVT::i32));
1530 }
1531
Christian Konigf82901a2013-02-26 17:52:23 +00001532 // Add optional chain and glue
1533 for (unsigned i = NumOps - NumDefs, e = Node->getNumOperands(); i < e; ++i)
1534 Ops.push_back(Node->getOperand(i));
1535
Tom Stellardb5a97002013-06-03 17:39:50 +00001536 // Nodes that have a glue result are not CSE'd by getMachineNode(), so in
1537 // this case a brand new node is always be created, even if the operands
1538 // are the same as before. So, manually check if anything has been changed.
Tom Stellardacec99c2013-06-05 23:39:50 +00001539 if (Desc->Opcode == Opcode && !isNodeChanged(Node, Ops)) {
1540 return Node;
Tom Stellardb5a97002013-06-03 17:39:50 +00001541 }
1542
Christian Konig3c145802013-03-27 09:12:59 +00001543 // Create a complete new instruction
Andrew Trickef9de2a2013-05-25 02:42:55 +00001544 return DAG.getMachineNode(Desc->Opcode, SDLoc(Node), Node->getVTList(), Ops);
Christian Konigd910b7d2013-02-26 17:52:16 +00001545}
Christian Konig8e06e2a2013-04-10 08:39:08 +00001546
1547/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00001548static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00001549 switch (Idx) {
1550 default: return 0;
1551 case AMDGPU::sub0: return 0;
1552 case AMDGPU::sub1: return 1;
1553 case AMDGPU::sub2: return 2;
1554 case AMDGPU::sub3: return 3;
1555 }
1556}
1557
1558/// \brief Adjust the writemask of MIMG instructions
1559void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
1560 SelectionDAG &DAG) const {
1561 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00001562 unsigned Lane = 0;
1563 unsigned OldDmask = Node->getConstantOperandVal(0);
1564 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001565
1566 // Try to figure out the used register components
1567 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
1568 I != E; ++I) {
1569
1570 // Abort if we can't understand the usage
1571 if (!I->isMachineOpcode() ||
1572 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
1573 return;
1574
Tom Stellard54774e52013-10-23 02:53:47 +00001575 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
1576 // Note that subregs are packed, i.e. Lane==0 is the first bit set
1577 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
1578 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00001579 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00001580
Tom Stellard54774e52013-10-23 02:53:47 +00001581 // Set which texture component corresponds to the lane.
1582 unsigned Comp;
1583 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
1584 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00001585 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00001586 Dmask &= ~(1 << Comp);
1587 }
1588
Christian Konig8e06e2a2013-04-10 08:39:08 +00001589 // Abort if we have more than one user per component
1590 if (Users[Lane])
1591 return;
1592
1593 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00001594 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001595 }
1596
Tom Stellard54774e52013-10-23 02:53:47 +00001597 // Abort if there's no change
1598 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00001599 return;
1600
1601 // Adjust the writemask in the node
1602 std::vector<SDValue> Ops;
Tom Stellard54774e52013-10-23 02:53:47 +00001603 Ops.push_back(DAG.getTargetConstant(NewDmask, MVT::i32));
Christian Konig8e06e2a2013-04-10 08:39:08 +00001604 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1605 Ops.push_back(Node->getOperand(i));
Craig Topper8c0b4d02014-04-28 05:57:50 +00001606 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00001607
Christian Konig8b1ed282013-04-10 08:39:16 +00001608 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00001609 // (if NewDmask has only one bit set...)
1610 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Christian Konig8b1ed282013-04-10 08:39:16 +00001611 SDValue RC = DAG.getTargetConstant(AMDGPU::VReg_32RegClassID, MVT::i32);
1612 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001613 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00001614 SDValue(Node, 0), RC);
1615 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
1616 return;
1617 }
1618
Christian Konig8e06e2a2013-04-10 08:39:08 +00001619 // Update the users of the node with the new indices
1620 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
1621
1622 SDNode *User = Users[i];
1623 if (!User)
1624 continue;
1625
1626 SDValue Op = DAG.getTargetConstant(Idx, MVT::i32);
1627 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
1628
1629 switch (Idx) {
1630 default: break;
1631 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
1632 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
1633 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
1634 }
1635 }
1636}
1637
Matt Arsenault08d84942014-06-03 23:06:13 +00001638/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00001639SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
1640 SelectionDAG &DAG) const {
Tom Stellard16a9a202013-08-14 23:24:17 +00001641 const SIInstrInfo *TII =
1642 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
Tom Stellard0518ff82013-06-03 17:39:58 +00001643 Node = AdjustRegClass(Node, DAG);
Christian Konig8e06e2a2013-04-10 08:39:08 +00001644
Tom Stellard16a9a202013-08-14 23:24:17 +00001645 if (TII->isMIMG(Node->getMachineOpcode()))
Christian Konig8e06e2a2013-04-10 08:39:08 +00001646 adjustWritemask(Node, DAG);
1647
1648 return foldOperands(Node, DAG);
1649}
Christian Konig8b1ed282013-04-10 08:39:16 +00001650
1651/// \brief Assign the register class depending on the number of
1652/// bits set in the writemask
1653void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
1654 SDNode *Node) const {
Tom Stellard16a9a202013-08-14 23:24:17 +00001655 const SIInstrInfo *TII =
1656 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1657 if (!TII->isMIMG(MI->getOpcode()))
Christian Konig8b1ed282013-04-10 08:39:16 +00001658 return;
1659
1660 unsigned VReg = MI->getOperand(0).getReg();
1661 unsigned Writemask = MI->getOperand(1).getImm();
1662 unsigned BitsSet = 0;
1663 for (unsigned i = 0; i < 4; ++i)
1664 BitsSet += Writemask & (1 << i) ? 1 : 0;
1665
1666 const TargetRegisterClass *RC;
1667 switch (BitsSet) {
1668 default: return;
1669 case 1: RC = &AMDGPU::VReg_32RegClass; break;
1670 case 2: RC = &AMDGPU::VReg_64RegClass; break;
1671 case 3: RC = &AMDGPU::VReg_96RegClass; break;
1672 }
1673
Tom Stellard682bfbc2013-10-10 17:11:24 +00001674 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
1675 MI->setDesc(TII->get(NewOpcode));
Christian Konig8b1ed282013-04-10 08:39:16 +00001676 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
1677 MRI.setRegClass(VReg, RC);
1678}
Tom Stellard0518ff82013-06-03 17:39:58 +00001679
1680MachineSDNode *SITargetLowering::AdjustRegClass(MachineSDNode *N,
1681 SelectionDAG &DAG) const {
1682
1683 SDLoc DL(N);
1684 unsigned NewOpcode = N->getMachineOpcode();
1685
1686 switch (N->getMachineOpcode()) {
1687 default: return N;
Tom Stellard0518ff82013-06-03 17:39:58 +00001688 case AMDGPU::S_LOAD_DWORD_IMM:
1689 NewOpcode = AMDGPU::BUFFER_LOAD_DWORD_ADDR64;
1690 // Fall-through
1691 case AMDGPU::S_LOAD_DWORDX2_SGPR:
1692 if (NewOpcode == N->getMachineOpcode()) {
1693 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64;
1694 }
1695 // Fall-through
1696 case AMDGPU::S_LOAD_DWORDX4_IMM:
1697 case AMDGPU::S_LOAD_DWORDX4_SGPR: {
1698 if (NewOpcode == N->getMachineOpcode()) {
1699 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64;
1700 }
1701 if (fitsRegClass(DAG, N->getOperand(0), AMDGPU::SReg_64RegClassID)) {
1702 return N;
1703 }
1704 ConstantSDNode *Offset = cast<ConstantSDNode>(N->getOperand(1));
1705 SDValue Ops[] = {
1706 SDValue(DAG.getMachineNode(AMDGPU::SI_ADDR64_RSRC, DL, MVT::i128,
1707 DAG.getConstant(0, MVT::i64)), 0),
1708 N->getOperand(0),
1709 DAG.getConstant(Offset->getSExtValue() << 2, MVT::i32)
1710 };
1711 return DAG.getMachineNode(NewOpcode, DL, N->getVTList(), Ops);
1712 }
1713 }
1714}
Tom Stellard94593ee2013-06-03 17:40:18 +00001715
1716SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
1717 const TargetRegisterClass *RC,
1718 unsigned Reg, EVT VT) const {
1719 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
1720
1721 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
1722 cast<RegisterSDNode>(VReg)->getReg(), VT);
1723}