blob: b3429b9748c2c85de012989f93fb9dcee207f382 [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 Arsenault0d89e842014-07-15 21:44:37 +0000185 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
186 setOperationAction(ISD::SELECT, MVT::i1, Promote);
187
Matt Arsenaultd504a742014-05-15 21:44:05 +0000188 for (MVT VT : VecTypes) {
Tom Stellard967bf582014-02-13 23:34:15 +0000189 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
190 switch(Op) {
191 case ISD::LOAD:
192 case ISD::STORE:
193 case ISD::BUILD_VECTOR:
194 case ISD::BITCAST:
195 case ISD::EXTRACT_VECTOR_ELT:
196 case ISD::INSERT_VECTOR_ELT:
197 case ISD::CONCAT_VECTORS:
198 case ISD::INSERT_SUBVECTOR:
199 case ISD::EXTRACT_SUBVECTOR:
200 break;
201 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000202 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000203 break;
204 }
205 }
206 }
207
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000208 for (int I = MVT::v1f64; I <= MVT::v8f64; ++I) {
209 MVT::SimpleValueType VT = static_cast<MVT::SimpleValueType>(I);
Matt Arsenaulta81aee82014-02-24 21:16:50 +0000210 setOperationAction(ISD::FTRUNC, VT, Expand);
211 setOperationAction(ISD::FCEIL, VT, Expand);
212 setOperationAction(ISD::FFLOOR, VT, Expand);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000213 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000214
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000215 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
216 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
217 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
218 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
Matt Arsenaulta90d22f2014-04-17 17:06:37 +0000219 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000220 }
221
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000222 // FIXME: These should be removed and handled the same was as f32 fneg. Source
Matt Arsenault7aeb8132014-06-18 17:05:22 +0000223 // modifiers also work for the double instructions.
224 setOperationAction(ISD::FNEG, MVT::f64, Expand);
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000225 setOperationAction(ISD::FABS, MVT::f64, Expand);
Matt Arsenault7aeb8132014-06-18 17:05:22 +0000226
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000227 setOperationAction(ISD::FDIV, MVT::f32, Custom);
228
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000229 setTargetDAGCombine(ISD::SELECT_CC);
Tom Stellard75aadc22012-12-11 21:25:42 +0000230 setTargetDAGCombine(ISD::SETCC);
Michel Danzerf52a6722013-03-08 10:58:01 +0000231
Matt Arsenault364a6742014-06-11 17:50:44 +0000232 setTargetDAGCombine(ISD::UINT_TO_FP);
233
Christian Konigeecebd02013-03-26 14:04:02 +0000234 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000235}
236
Tom Stellard0125f2a2013-06-25 02:39:35 +0000237//===----------------------------------------------------------------------===//
238// TargetLowering queries
239//===----------------------------------------------------------------------===//
240
241bool SITargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
Matt Arsenault25793a32014-02-05 23:15:53 +0000242 unsigned AddrSpace,
Tom Stellard0125f2a2013-06-25 02:39:35 +0000243 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000244 if (IsFast)
245 *IsFast = false;
246
Tom Stellard0125f2a2013-06-25 02:39:35 +0000247 // XXX: This depends on the address space and also we may want to revist
248 // the alignment values we specify in the DataLayout.
Matt Arsenault1018c892014-04-24 17:08:26 +0000249
250 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
251 // which isn't a simple VT.
Tom Stellard81d871d2013-11-13 23:36:50 +0000252 if (!VT.isSimple() || VT == MVT::Other)
253 return false;
Matt Arsenault1018c892014-04-24 17:08:26 +0000254
255 // XXX - CI changes say "Support for unaligned memory accesses" but I don't
256 // see what for specifically. The wording everywhere else seems to be the
257 // same.
258
259 // 3.6.4 - Operations using pairs of VGPRs (for example: double-floats) have
260 // no alignment restrictions.
261 if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS) {
262 // Using any pair of GPRs should be the same as any other pair.
263 if (IsFast)
264 *IsFast = true;
265 return VT.bitsGE(MVT::i64);
266 }
267
268 // XXX - The only mention I see of this in the ISA manual is for LDS direct
269 // reads the "byte address and must be dword aligned". Is it also true for the
270 // normal loads and stores?
271 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS)
272 return false;
273
274 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
275 // byte-address are ignored, thus forcing Dword alignment.
276 if (IsFast)
277 *IsFast = true;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000278 return VT.bitsGT(MVT::i32);
279}
280
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000281TargetLoweringBase::LegalizeTypeAction
282SITargetLowering::getPreferredVectorAction(EVT VT) const {
283 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
284 return TypeSplitVector;
285
286 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000287}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000288
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000289bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
290 Type *Ty) const {
291 const SIInstrInfo *TII =
292 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
293 return TII->isInlineConstant(Imm);
294}
295
Tom Stellardaf775432013-10-23 00:44:32 +0000296SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
Tom Stellard94593ee2013-06-03 17:40:18 +0000297 SDLoc DL, SDValue Chain,
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000298 unsigned Offset, bool Signed) const {
Tom Stellard94593ee2013-06-03 17:40:18 +0000299 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
300 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
301 AMDGPUAS::CONSTANT_ADDRESS);
Tom Stellard94593ee2013-06-03 17:40:18 +0000302 SDValue BasePtr = DAG.getCopyFromReg(Chain, DL,
303 MRI.getLiveInVirtReg(AMDGPU::SGPR0_SGPR1), MVT::i64);
304 SDValue Ptr = DAG.getNode(ISD::ADD, DL, MVT::i64, BasePtr,
305 DAG.getConstant(Offset, MVT::i64));
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000306 return DAG.getExtLoad(Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD, DL, VT, Chain, Ptr,
Tom Stellardaf775432013-10-23 00:44:32 +0000307 MachinePointerInfo(UndefValue::get(PtrTy)), MemVT,
308 false, false, MemVT.getSizeInBits() >> 3);
Tom Stellard94593ee2013-06-03 17:40:18 +0000309
310}
311
Christian Konig2c8f6d52013-03-07 09:03:52 +0000312SDValue SITargetLowering::LowerFormalArguments(
313 SDValue Chain,
314 CallingConv::ID CallConv,
315 bool isVarArg,
316 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000317 SDLoc DL, SelectionDAG &DAG,
Christian Konig2c8f6d52013-03-07 09:03:52 +0000318 SmallVectorImpl<SDValue> &InVals) const {
319
320 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
321
322 MachineFunction &MF = DAG.getMachineFunction();
323 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000324 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000325
326 assert(CallConv == CallingConv::C);
327
328 SmallVector<ISD::InputArg, 16> Splits;
Christian Konig99ee0f42013-03-07 09:04:14 +0000329 uint32_t Skipped = 0;
330
331 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000332 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000333
334 // First check if it's a PS input addr
Matt Arsenault762af962014-07-13 03:06:39 +0000335 if (Info->getShaderType() == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
Vincent Lejeuned6236442013-10-13 17:56:16 +0000336 !Arg.Flags.isByVal()) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000337
338 assert((PSInputNum <= 15) && "Too many PS inputs!");
339
340 if (!Arg.Used) {
341 // We can savely skip PS inputs
342 Skipped |= 1 << i;
343 ++PSInputNum;
344 continue;
345 }
346
347 Info->PSInputAddr |= 1 << PSInputNum++;
348 }
349
350 // Second split vertices into their elements
Matt Arsenault762af962014-07-13 03:06:39 +0000351 if (Info->getShaderType() != ShaderType::COMPUTE && Arg.VT.isVector()) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000352 ISD::InputArg NewArg = Arg;
353 NewArg.Flags.setSplit();
354 NewArg.VT = Arg.VT.getVectorElementType();
355
356 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
357 // three or five element vertex only needs three or five registers,
358 // NOT four or eigth.
359 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
360 unsigned NumElements = ParamType->getVectorNumElements();
361
362 for (unsigned j = 0; j != NumElements; ++j) {
363 Splits.push_back(NewArg);
364 NewArg.PartOffset += NewArg.VT.getStoreSize();
365 }
366
Matt Arsenault762af962014-07-13 03:06:39 +0000367 } else if (Info->getShaderType() != ShaderType::COMPUTE) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000368 Splits.push_back(Arg);
369 }
370 }
371
372 SmallVector<CCValAssign, 16> ArgLocs;
373 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
374 getTargetMachine(), ArgLocs, *DAG.getContext());
375
Christian Konig99ee0f42013-03-07 09:04:14 +0000376 // At least one interpolation mode must be enabled or else the GPU will hang.
Matt Arsenault762af962014-07-13 03:06:39 +0000377 if (Info->getShaderType() == ShaderType::PIXEL &&
378 (Info->PSInputAddr & 0x7F) == 0) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000379 Info->PSInputAddr |= 1;
380 CCInfo.AllocateReg(AMDGPU::VGPR0);
381 CCInfo.AllocateReg(AMDGPU::VGPR1);
382 }
383
Tom Stellarded882c22013-06-03 17:40:11 +0000384 // The pointer to the list of arguments is stored in SGPR0, SGPR1
Matt Arsenault762af962014-07-13 03:06:39 +0000385 if (Info->getShaderType() == ShaderType::COMPUTE) {
Tom Stellarded882c22013-06-03 17:40:11 +0000386 CCInfo.AllocateReg(AMDGPU::SGPR0);
387 CCInfo.AllocateReg(AMDGPU::SGPR1);
Tom Stellard94593ee2013-06-03 17:40:18 +0000388 MF.addLiveIn(AMDGPU::SGPR0_SGPR1, &AMDGPU::SReg_64RegClass);
Tom Stellarded882c22013-06-03 17:40:11 +0000389 }
390
Matt Arsenault762af962014-07-13 03:06:39 +0000391 if (Info->getShaderType() == ShaderType::COMPUTE) {
Tom Stellardaf775432013-10-23 00:44:32 +0000392 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
393 Splits);
394 }
395
Christian Konig2c8f6d52013-03-07 09:03:52 +0000396 AnalyzeFormalArguments(CCInfo, Splits);
397
398 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
399
Christian Konigb7be72d2013-05-17 09:46:48 +0000400 const ISD::InputArg &Arg = Ins[i];
Christian Konig99ee0f42013-03-07 09:04:14 +0000401 if (Skipped & (1 << i)) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000402 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000403 continue;
404 }
405
Christian Konig2c8f6d52013-03-07 09:03:52 +0000406 CCValAssign &VA = ArgLocs[ArgIdx++];
Tom Stellarded882c22013-06-03 17:40:11 +0000407 EVT VT = VA.getLocVT();
408
409 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000410 VT = Ins[i].VT;
411 EVT MemVT = Splits[i].VT;
Tom Stellard94593ee2013-06-03 17:40:18 +0000412 // The first 36 bytes of the input buffer contains information about
413 // thread group and global sizes.
Tom Stellardaf775432013-10-23 00:44:32 +0000414 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, DAG.getRoot(),
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000415 36 + VA.getLocMemOffset(),
416 Ins[i].Flags.isSExt());
Tom Stellarded882c22013-06-03 17:40:11 +0000417 InVals.push_back(Arg);
418 continue;
419 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000420 assert(VA.isRegLoc() && "Parameter must be in a register!");
421
422 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000423
424 if (VT == MVT::i64) {
425 // For now assume it is a pointer
426 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
427 &AMDGPU::SReg_64RegClass);
428 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
429 InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
430 continue;
431 }
432
433 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
434
435 Reg = MF.addLiveIn(Reg, RC);
436 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
437
Christian Konig2c8f6d52013-03-07 09:03:52 +0000438 if (Arg.VT.isVector()) {
439
440 // Build a vector from the registers
441 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
442 unsigned NumElements = ParamType->getVectorNumElements();
443
444 SmallVector<SDValue, 4> Regs;
445 Regs.push_back(Val);
446 for (unsigned j = 1; j != NumElements; ++j) {
447 Reg = ArgLocs[ArgIdx++].getLocReg();
448 Reg = MF.addLiveIn(Reg, RC);
449 Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
450 }
451
452 // Fill up the missing vector elements
453 NumElements = Arg.VT.getVectorNumElements() - NumElements;
454 for (unsigned j = 0; j != NumElements; ++j)
455 Regs.push_back(DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000456
Craig Topper48d114b2014-04-26 18:35:24 +0000457 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000458 continue;
459 }
460
461 InVals.push_back(Val);
462 }
463 return Chain;
464}
465
Tom Stellard75aadc22012-12-11 21:25:42 +0000466MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
467 MachineInstr * MI, MachineBasicBlock * BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000468
Tom Stellard556d9aa2013-06-03 17:39:37 +0000469 MachineBasicBlock::iterator I = *MI;
Tom Stellard919bb6b2014-04-29 23:12:53 +0000470 const SIInstrInfo *TII =
471 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
472 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Tom Stellard556d9aa2013-06-03 17:39:37 +0000473
Tom Stellard75aadc22012-12-11 21:25:42 +0000474 switch (MI->getOpcode()) {
475 default:
476 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
477 case AMDGPU::BRANCH: return BB;
Tom Stellard556d9aa2013-06-03 17:39:37 +0000478 case AMDGPU::SI_ADDR64_RSRC: {
Tom Stellard556d9aa2013-06-03 17:39:37 +0000479 unsigned SuperReg = MI->getOperand(0).getReg();
Tom Stellarddef38c52014-03-21 15:51:53 +0000480 unsigned SubRegLo = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass);
481 unsigned SubRegHi = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass);
482 unsigned SubRegHiHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
483 unsigned SubRegHiLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
Tom Stellard556d9aa2013-06-03 17:39:37 +0000484 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B64), SubRegLo)
485 .addOperand(MI->getOperand(1));
486 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiLo)
487 .addImm(0);
488 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiHi)
Tom Stellard15834092014-03-21 15:51:57 +0000489 .addImm(AMDGPU::RSRC_DATA_FORMAT >> 32);
Tom Stellard556d9aa2013-06-03 17:39:37 +0000490 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SubRegHi)
491 .addReg(SubRegHiLo)
492 .addImm(AMDGPU::sub0)
493 .addReg(SubRegHiHi)
494 .addImm(AMDGPU::sub1);
495 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SuperReg)
496 .addReg(SubRegLo)
497 .addImm(AMDGPU::sub0_sub1)
498 .addReg(SubRegHi)
499 .addImm(AMDGPU::sub2_sub3);
500 MI->eraseFromParent();
501 break;
502 }
Matt Arsenaultdbc9aae2014-06-18 17:13:51 +0000503 case AMDGPU::V_SUB_F64: {
504 unsigned DestReg = MI->getOperand(0).getReg();
505 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F64), DestReg)
506 .addImm(0) // SRC0 modifiers
507 .addReg(MI->getOperand(1).getReg())
508 .addImm(1) // SRC1 modifiers
509 .addReg(MI->getOperand(2).getReg())
510 .addImm(0) // SRC2 modifiers
511 .addImm(0) // src2
512 .addImm(0) // CLAMP
513 .addImm(0); // OMOD
Tom Stellard2a6a61052013-07-12 18:15:08 +0000514 MI->eraseFromParent();
515 break;
Matt Arsenaultdbc9aae2014-06-18 17:13:51 +0000516 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000517 case AMDGPU::SI_RegisterStorePseudo: {
518 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Tom Stellard81d871d2013-11-13 23:36:50 +0000519 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
520 MachineInstrBuilder MIB =
521 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::SI_RegisterStore),
522 Reg);
523 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
524 MIB.addOperand(MI->getOperand(i));
525
526 MI->eraseFromParent();
Vincent Lejeune79a58342014-05-10 19:18:25 +0000527 break;
528 }
529 case AMDGPU::FABS_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(0x7fffffff);
537 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_AND_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::FNEG_SI: {
545 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
546 const SIInstrInfo *TII =
547 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
548 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
549 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_MOV_B32_e32),
550 Reg)
551 .addImm(0x80000000);
552 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_XOR_B32_e32),
553 MI->getOperand(0).getReg())
554 .addReg(MI->getOperand(1).getReg())
555 .addReg(Reg);
556 MI->eraseFromParent();
557 break;
558 }
559 case AMDGPU::FCLAMP_SI: {
560 const SIInstrInfo *TII =
561 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
562 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F32_e64),
563 MI->getOperand(0).getReg())
Vincent Lejeune94af31f2014-05-10 19:18:33 +0000564 .addImm(0) // SRC0 modifiers
Vincent Lejeune79a58342014-05-10 19:18:25 +0000565 .addOperand(MI->getOperand(1))
Vincent Lejeune94af31f2014-05-10 19:18:33 +0000566 .addImm(0) // SRC1 modifiers
Vincent Lejeune79a58342014-05-10 19:18:25 +0000567 .addImm(0) // SRC1
Vincent Lejeune79a58342014-05-10 19:18:25 +0000568 .addImm(1) // CLAMP
Vincent Lejeune94af31f2014-05-10 19:18:33 +0000569 .addImm(0); // OMOD
Vincent Lejeune79a58342014-05-10 19:18:25 +0000570 MI->eraseFromParent();
Tom Stellard81d871d2013-11-13 23:36:50 +0000571 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000572 }
573 return BB;
574}
575
Matt Arsenault758659232013-05-18 00:21:46 +0000576EVT SITargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +0000577 if (!VT.isVector()) {
578 return MVT::i1;
579 }
580 return MVT::getVectorVT(MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +0000581}
582
Christian Konig082a14a2013-03-18 11:34:05 +0000583MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const {
584 return MVT::i32;
585}
586
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +0000587bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
588 VT = VT.getScalarType();
589
590 if (!VT.isSimple())
591 return false;
592
593 switch (VT.getSimpleVT().SimpleTy) {
594 case MVT::f32:
595 return false; /* There is V_MAD_F32 for f32 */
596 case MVT::f64:
597 return true;
598 default:
599 break;
600 }
601
602 return false;
603}
604
Tom Stellard75aadc22012-12-11 21:25:42 +0000605//===----------------------------------------------------------------------===//
606// Custom DAG Lowering Operations
607//===----------------------------------------------------------------------===//
608
609SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Michel Danzer49812b52013-07-10 16:37:07 +0000610 MachineFunction &MF = DAG.getMachineFunction();
611 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellard75aadc22012-12-11 21:25:42 +0000612 switch (Op.getOpcode()) {
613 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +0000614 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +0000615 case ISD::LOAD: {
616 LoadSDNode *Load = dyn_cast<LoadSDNode>(Op);
Tom Stellard10ae6a02014-07-02 20:53:54 +0000617 EVT VT = Op.getValueType();
618
619 // These loads are legal.
620 if (Load->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS &&
621 VT.isVector() && VT.getVectorNumElements() == 2 &&
622 VT.getVectorElementType() == MVT::i32)
623 return SDValue();
624
Tom Stellard80be9652014-02-13 23:34:10 +0000625 if (Op.getValueType().isVector() &&
626 (Load->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
627 Load->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS ||
628 (Load->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
629 Op.getValueType().getVectorNumElements() > 4))) {
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000630 return SplitVectorLoad(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +0000631 } else {
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000632 SDValue Result = LowerLOAD(Op, DAG);
633 assert((!Result.getNode() ||
634 Result.getNode()->getNumValues() == 2) &&
635 "Load should return a value and a chain");
636 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +0000637 }
638 }
Tom Stellardaf775432013-10-23 00:44:32 +0000639
Tom Stellard0ec134f2014-02-04 17:18:40 +0000640 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000641 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +0000642 case ISD::STORE: return LowerSTORE(Op, DAG);
Michel Danzer49812b52013-07-10 16:37:07 +0000643 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +0000644 case ISD::INTRINSIC_WO_CHAIN: {
645 unsigned IntrinsicID =
646 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
647 EVT VT = Op.getValueType();
648 SDLoc DL(Op);
649 //XXX: Hardcoded we only use two to store the pointer to the parameters.
650 unsigned NumUserSGPRs = 2;
651 switch (IntrinsicID) {
652 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
653 case Intrinsic::r600_read_ngroups_x:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000654 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 0, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000655 case Intrinsic::r600_read_ngroups_y:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000656 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 4, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000657 case Intrinsic::r600_read_ngroups_z:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000658 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 8, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000659 case Intrinsic::r600_read_global_size_x:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000660 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 12, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000661 case Intrinsic::r600_read_global_size_y:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000662 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 16, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000663 case Intrinsic::r600_read_global_size_z:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000664 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 20, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000665 case Intrinsic::r600_read_local_size_x:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000666 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 24, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000667 case Intrinsic::r600_read_local_size_y:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000668 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 28, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000669 case Intrinsic::r600_read_local_size_z:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000670 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 32, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000671 case Intrinsic::r600_read_tgid_x:
672 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
673 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 0), VT);
674 case Intrinsic::r600_read_tgid_y:
675 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
676 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 1), VT);
677 case Intrinsic::r600_read_tgid_z:
678 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
679 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 2), VT);
680 case Intrinsic::r600_read_tidig_x:
681 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
682 AMDGPU::VGPR0, VT);
683 case Intrinsic::r600_read_tidig_y:
684 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
685 AMDGPU::VGPR1, VT);
686 case Intrinsic::r600_read_tidig_z:
687 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
688 AMDGPU::VGPR2, VT);
Tom Stellard9fa17912013-08-14 23:24:45 +0000689 case AMDGPUIntrinsic::SI_load_const: {
690 SDValue Ops [] = {
Tom Stellard868fd922014-04-17 21:00:11 +0000691 Op.getOperand(1),
Tom Stellard9fa17912013-08-14 23:24:45 +0000692 Op.getOperand(2)
693 };
Tom Stellard94593ee2013-06-03 17:40:18 +0000694
Benjamin Kramera8eecee2013-08-16 14:48:09 +0000695 MachineMemOperand *MMO = MF.getMachineMemOperand(
696 MachinePointerInfo(),
697 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
698 VT.getSizeInBits() / 8, 4);
Tom Stellard9fa17912013-08-14 23:24:45 +0000699 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
Craig Topper206fcd42014-04-26 19:29:41 +0000700 Op->getVTList(), Ops, VT, MMO);
Tom Stellard9fa17912013-08-14 23:24:45 +0000701 }
702 case AMDGPUIntrinsic::SI_sample:
703 return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG);
704 case AMDGPUIntrinsic::SI_sampleb:
705 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG);
706 case AMDGPUIntrinsic::SI_sampled:
707 return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG);
708 case AMDGPUIntrinsic::SI_samplel:
709 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG);
710 case AMDGPUIntrinsic::SI_vs_load_input:
711 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
Tom Stellard868fd922014-04-17 21:00:11 +0000712 Op.getOperand(1),
Tom Stellard9fa17912013-08-14 23:24:45 +0000713 Op.getOperand(2),
714 Op.getOperand(3));
Tom Stellard94593ee2013-06-03 17:40:18 +0000715 }
716 }
Tom Stellardafcf12f2013-09-12 02:55:14 +0000717
718 case ISD::INTRINSIC_VOID:
719 SDValue Chain = Op.getOperand(0);
720 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
721
722 switch (IntrinsicID) {
723 case AMDGPUIntrinsic::SI_tbuffer_store: {
724 SDLoc DL(Op);
725 SDValue Ops [] = {
726 Chain,
Tom Stellard868fd922014-04-17 21:00:11 +0000727 Op.getOperand(2),
Tom Stellardafcf12f2013-09-12 02:55:14 +0000728 Op.getOperand(3),
729 Op.getOperand(4),
730 Op.getOperand(5),
731 Op.getOperand(6),
732 Op.getOperand(7),
733 Op.getOperand(8),
734 Op.getOperand(9),
735 Op.getOperand(10),
736 Op.getOperand(11),
737 Op.getOperand(12),
738 Op.getOperand(13),
739 Op.getOperand(14)
740 };
741 EVT VT = Op.getOperand(3).getValueType();
742
743 MachineMemOperand *MMO = MF.getMachineMemOperand(
744 MachinePointerInfo(),
745 MachineMemOperand::MOStore,
746 VT.getSizeInBits() / 8, 4);
747 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
Craig Topper206fcd42014-04-26 19:29:41 +0000748 Op->getVTList(), Ops, VT, MMO);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000749 }
750 default:
751 break;
752 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000753 }
754 return SDValue();
755}
756
Tom Stellardf8794352012-12-19 22:10:31 +0000757/// \brief Helper function for LowerBRCOND
758static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000759
Tom Stellardf8794352012-12-19 22:10:31 +0000760 SDNode *Parent = Value.getNode();
761 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
762 I != E; ++I) {
763
764 if (I.getUse().get() != Value)
765 continue;
766
767 if (I->getOpcode() == Opcode)
768 return *I;
769 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000770 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000771}
772
773/// This transforms the control flow intrinsics to get the branch destination as
774/// last parameter, also switches branch target with BR if the need arise
775SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
776 SelectionDAG &DAG) const {
777
Andrew Trickef9de2a2013-05-25 02:42:55 +0000778 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +0000779
780 SDNode *Intr = BRCOND.getOperand(1).getNode();
781 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +0000782 SDNode *BR = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000783
784 if (Intr->getOpcode() == ISD::SETCC) {
785 // As long as we negate the condition everything is fine
786 SDNode *SetCC = Intr;
787 assert(SetCC->getConstantOperandVal(1) == 1);
NAKAMURA Takumi458a8272013-01-07 11:14:44 +0000788 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
789 ISD::SETNE);
Tom Stellardf8794352012-12-19 22:10:31 +0000790 Intr = SetCC->getOperand(0).getNode();
791
792 } else {
793 // Get the target from BR if we don't negate the condition
794 BR = findUser(BRCOND, ISD::BR);
795 Target = BR->getOperand(1);
796 }
797
798 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
799
800 // Build the result and
801 SmallVector<EVT, 4> Res;
802 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
803 Res.push_back(Intr->getValueType(i));
804
805 // operands of the new intrinsic call
806 SmallVector<SDValue, 4> Ops;
807 Ops.push_back(BRCOND.getOperand(0));
808 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
809 Ops.push_back(Intr->getOperand(i));
810 Ops.push_back(Target);
811
812 // build the new intrinsic call
813 SDNode *Result = DAG.getNode(
814 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +0000815 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +0000816
817 if (BR) {
818 // Give the branch instruction our target
819 SDValue Ops[] = {
820 BR->getOperand(0),
821 BRCOND.getOperand(2)
822 };
Craig Topper131de822014-04-27 19:21:16 +0000823 DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops);
Tom Stellardf8794352012-12-19 22:10:31 +0000824 }
825
826 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
827
828 // Copy the intrinsic results to registers
829 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
830 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
831 if (!CopyToReg)
832 continue;
833
834 Chain = DAG.getCopyToReg(
835 Chain, DL,
836 CopyToReg->getOperand(1),
837 SDValue(Result, i - 1),
838 SDValue());
839
840 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
841 }
842
843 // Remove the old intrinsic from the chain
844 DAG.ReplaceAllUsesOfValueWith(
845 SDValue(Intr, Intr->getNumValues() - 1),
846 Intr->getOperand(0));
847
848 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +0000849}
850
Tom Stellard81d871d2013-11-13 23:36:50 +0000851SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
852 SDLoc DL(Op);
853 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000854 SDValue Lowered = AMDGPUTargetLowering::LowerLOAD(Op, DAG);
855 if (Lowered.getNode())
856 return Lowered;
Tom Stellard81d871d2013-11-13 23:36:50 +0000857
Tom Stellarde9373602014-01-22 19:24:14 +0000858 if (Load->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
Tom Stellard81d871d2013-11-13 23:36:50 +0000859 return SDValue();
Tom Stellarde9373602014-01-22 19:24:14 +0000860 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000861
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000862 EVT MemVT = Load->getMemoryVT();
863
864 assert(!MemVT.isVector() && "Private loads should be scalarized");
865 assert(!MemVT.isFloatingPoint() && "FP loads should be promoted to int");
866
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000867 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Load->getBasePtr(),
Tom Stellard81d871d2013-11-13 23:36:50 +0000868 DAG.getConstant(2, MVT::i32));
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000869
870 // FIXME: REGISTER_LOAD should probably have a chain result.
871 SDValue Chain = Load->getChain();
872 SDValue LoLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
873 Chain, Ptr,
874 DAG.getTargetConstant(0, MVT::i32),
875 Op.getOperand(2));
876
877 SDValue Ret = LoLoad.getValue(0);
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000878 if (MemVT.getSizeInBits() == 64) {
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000879 // TODO: This needs a test to make sure the right thing is happening with
880 // the chain. That is hard without general function support.
881
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000882 SDValue IncPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
883 DAG.getConstant(1, MVT::i32));
884
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000885 SDValue HiLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
886 Chain, IncPtr,
887 DAG.getTargetConstant(0, MVT::i32),
888 Op.getOperand(2));
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000889
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000890 Ret = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, LoLoad, HiLoad);
891 // Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
892 // LoLoad.getValue(1), HiLoad.getValue(1));
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000893 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000894
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000895 SDValue Ops[] = {
896 Ret,
897 Chain
898 };
Tom Stellard81d871d2013-11-13 23:36:50 +0000899
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000900 return DAG.getMergeValues(Ops, DL);
Tom Stellard81d871d2013-11-13 23:36:50 +0000901}
902
Tom Stellard9fa17912013-08-14 23:24:45 +0000903SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode,
904 const SDValue &Op,
905 SelectionDAG &DAG) const {
906 return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1),
907 Op.getOperand(2),
Tom Stellard868fd922014-04-17 21:00:11 +0000908 Op.getOperand(3),
Tom Stellard9fa17912013-08-14 23:24:45 +0000909 Op.getOperand(4));
910}
911
Tom Stellard0ec134f2014-02-04 17:18:40 +0000912SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
913 if (Op.getValueType() != MVT::i64)
914 return SDValue();
915
916 SDLoc DL(Op);
917 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000918
919 SDValue Zero = DAG.getConstant(0, MVT::i32);
920 SDValue One = DAG.getConstant(1, MVT::i32);
921
Tom Stellard7ea3d6d2014-03-31 14:01:55 +0000922 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
923 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
924
925 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
926 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000927
928 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
929
Tom Stellard7ea3d6d2014-03-31 14:01:55 +0000930 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
931 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000932
933 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
934
Tom Stellard7ea3d6d2014-03-31 14:01:55 +0000935 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
936 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000937}
938
Matt Arsenault22ca3f82014-07-15 23:50:10 +0000939// Catch division cases where we can use shortcuts with rcp and rsq
940// instructions.
941SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000942 SDLoc SL(Op);
943 SDValue LHS = Op.getOperand(0);
944 SDValue RHS = Op.getOperand(1);
945 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +0000946 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000947
948 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +0000949 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) &&
950 CLHS->isExactlyValue(1.0)) {
951 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
952 // the CI documentation has a worst case error of 1 ulp.
953 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
954 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000955
956 // 1.0 / sqrt(x) -> rsq(x)
Matt Arsenault22ca3f82014-07-15 23:50:10 +0000957 //
958 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
959 // error seems really high at 2^29 ULP.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000960 if (RHS.getOpcode() == ISD::FSQRT)
961 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
962
963 // 1.0 / x -> rcp(x)
964 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
965 }
966 }
967
Matt Arsenault22ca3f82014-07-15 23:50:10 +0000968 if (Unsafe) {
969 // Turn into multiply by the reciprocal.
970 // x / y -> x * (1.0 / y)
971 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
972 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip);
973 }
974
975 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000976}
977
978SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault22ca3f82014-07-15 23:50:10 +0000979 SDValue FastLowered = LowerFastFDIV(Op, DAG);
980 if (FastLowered.getNode())
981 return FastLowered;
982
983 // This uses v_rcp_f32 which does not handle denormals. Let this hit a
984 // selection error for now rather than do something incorrect.
985 if (Subtarget->hasFP32Denormals())
986 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000987
988 SDLoc SL(Op);
989 SDValue LHS = Op.getOperand(0);
990 SDValue RHS = Op.getOperand(1);
991
992 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
993
994 const APFloat K0Val(BitsToFloat(0x6f800000));
995 const SDValue K0 = DAG.getConstantFP(K0Val, MVT::f32);
996
997 const APFloat K1Val(BitsToFloat(0x2f800000));
998 const SDValue K1 = DAG.getConstantFP(K1Val, MVT::f32);
999
1000 const SDValue One = DAG.getTargetConstantFP(1.0, MVT::f32);
1001
1002 EVT SetCCVT = getSetCCResultType(*DAG.getContext(), MVT::f32);
1003
1004 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
1005
1006 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
1007
1008 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
1009
1010 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
1011
1012 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
1013
1014 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
1015}
1016
1017SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
1018 return SDValue();
1019}
1020
1021SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
1022 EVT VT = Op.getValueType();
1023
1024 if (VT == MVT::f32)
1025 return LowerFDIV32(Op, DAG);
1026
1027 if (VT == MVT::f64)
1028 return LowerFDIV64(Op, DAG);
1029
1030 llvm_unreachable("Unexpected type for fdiv");
1031}
1032
Tom Stellard81d871d2013-11-13 23:36:50 +00001033SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1034 SDLoc DL(Op);
1035 StoreSDNode *Store = cast<StoreSDNode>(Op);
1036 EVT VT = Store->getMemoryVT();
1037
Tom Stellard9b3816b2014-06-24 23:33:04 +00001038 // These stores are legal.
1039 if (Store->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS &&
1040 VT.isVector() && VT.getVectorNumElements() == 2 &&
1041 VT.getVectorElementType() == MVT::i32)
1042 return SDValue();
1043
Tom Stellard81d871d2013-11-13 23:36:50 +00001044 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1045 if (Ret.getNode())
1046 return Ret;
1047
1048 if (VT.isVector() && VT.getVectorNumElements() >= 8)
1049 return SplitVectorStore(Op, DAG);
1050
Tom Stellard1c8788e2014-03-07 20:12:33 +00001051 if (VT == MVT::i1)
1052 return DAG.getTruncStore(Store->getChain(), DL,
1053 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
1054 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
1055
Tom Stellard81d871d2013-11-13 23:36:50 +00001056 if (Store->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS)
1057 return SDValue();
1058
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +00001059 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Store->getBasePtr(),
Tom Stellard81d871d2013-11-13 23:36:50 +00001060 DAG.getConstant(2, MVT::i32));
1061 SDValue Chain = Store->getChain();
1062 SmallVector<SDValue, 8> Values;
1063
Tom Stellarde9373602014-01-22 19:24:14 +00001064 if (Store->isTruncatingStore()) {
1065 unsigned Mask = 0;
1066 if (Store->getMemoryVT() == MVT::i8) {
1067 Mask = 0xff;
1068 } else if (Store->getMemoryVT() == MVT::i16) {
1069 Mask = 0xffff;
1070 }
1071 SDValue Dst = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
1072 Chain, Store->getBasePtr(),
1073 DAG.getConstant(0, MVT::i32));
1074 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getBasePtr(),
1075 DAG.getConstant(0x3, MVT::i32));
1076 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1077 DAG.getConstant(3, MVT::i32));
1078 SDValue MaskedValue = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getValue(),
1079 DAG.getConstant(Mask, MVT::i32));
1080 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
1081 MaskedValue, ShiftAmt);
1082 SDValue RotrAmt = DAG.getNode(ISD::SUB, DL, MVT::i32,
1083 DAG.getConstant(32, MVT::i32), ShiftAmt);
1084 SDValue DstMask = DAG.getNode(ISD::ROTR, DL, MVT::i32,
1085 DAG.getConstant(Mask, MVT::i32),
1086 RotrAmt);
1087 Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
1088 Dst = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
1089
1090 Values.push_back(Dst);
1091 } else if (VT == MVT::i64) {
Tom Stellard81d871d2013-11-13 23:36:50 +00001092 for (unsigned i = 0; i < 2; ++i) {
1093 Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
1094 Store->getValue(), DAG.getConstant(i, MVT::i32)));
1095 }
1096 } else if (VT == MVT::i128) {
1097 for (unsigned i = 0; i < 2; ++i) {
1098 for (unsigned j = 0; j < 2; ++j) {
1099 Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
1100 DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64,
1101 Store->getValue(), DAG.getConstant(i, MVT::i32)),
1102 DAG.getConstant(j, MVT::i32)));
1103 }
1104 }
1105 } else {
1106 Values.push_back(Store->getValue());
1107 }
1108
1109 for (unsigned i = 0; i < Values.size(); ++i) {
1110 SDValue PartPtr = DAG.getNode(ISD::ADD, DL, MVT::i32,
1111 Ptr, DAG.getConstant(i, MVT::i32));
1112 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1113 Chain, Values[i], PartPtr,
1114 DAG.getTargetConstant(0, MVT::i32));
1115 }
1116 return Chain;
1117}
1118
Tom Stellard75aadc22012-12-11 21:25:42 +00001119//===----------------------------------------------------------------------===//
1120// Custom DAG optimizations
1121//===----------------------------------------------------------------------===//
1122
Matt Arsenault364a6742014-06-11 17:50:44 +00001123SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
1124 DAGCombinerInfo &DCI) {
1125 EVT VT = N->getValueType(0);
1126 EVT ScalarVT = VT.getScalarType();
1127 if (ScalarVT != MVT::f32)
1128 return SDValue();
1129
1130 SelectionDAG &DAG = DCI.DAG;
1131 SDLoc DL(N);
1132
1133 SDValue Src = N->getOperand(0);
1134 EVT SrcVT = Src.getValueType();
1135
1136 // TODO: We could try to match extracting the higher bytes, which would be
1137 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
1138 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
1139 // about in practice.
1140 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
1141 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
1142 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
1143 DCI.AddToWorklist(Cvt.getNode());
1144 return Cvt;
1145 }
1146 }
1147
1148 // We are primarily trying to catch operations on illegal vector types
1149 // before they are expanded.
1150 // For scalars, we can use the more flexible method of checking masked bits
1151 // after legalization.
1152 if (!DCI.isBeforeLegalize() ||
1153 !SrcVT.isVector() ||
1154 SrcVT.getVectorElementType() != MVT::i8) {
1155 return SDValue();
1156 }
1157
1158 assert(DCI.isBeforeLegalize() && "Unexpected legal type");
1159
1160 // Weird sized vectors are a pain to handle, but we know 3 is really the same
1161 // size as 4.
1162 unsigned NElts = SrcVT.getVectorNumElements();
1163 if (!SrcVT.isSimple() && NElts != 3)
1164 return SDValue();
1165
1166 // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to
1167 // prevent a mess from expanding to v4i32 and repacking.
1168 if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) {
1169 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT);
1170 EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT);
1171 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts);
1172
1173 LoadSDNode *Load = cast<LoadSDNode>(Src);
1174 SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT,
1175 Load->getChain(),
1176 Load->getBasePtr(),
1177 LoadVT,
1178 Load->getMemOperand());
1179
1180 // Make sure successors of the original load stay after it by updating
1181 // them to use the new Chain.
1182 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1));
1183
1184 SmallVector<SDValue, 4> Elts;
1185 if (RegVT.isVector())
1186 DAG.ExtractVectorElements(NewLoad, Elts);
1187 else
1188 Elts.push_back(NewLoad);
1189
1190 SmallVector<SDValue, 4> Ops;
1191
1192 unsigned EltIdx = 0;
1193 for (SDValue Elt : Elts) {
1194 unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx);
1195 for (unsigned I = 0; I < ComponentsInElt; ++I) {
1196 unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I;
1197 SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt);
1198 DCI.AddToWorklist(Cvt.getNode());
1199 Ops.push_back(Cvt);
1200 }
1201
1202 ++EltIdx;
1203 }
1204
1205 assert(Ops.size() == NElts);
1206
1207 return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops);
1208 }
1209
1210 return SDValue();
1211}
1212
Tom Stellard75aadc22012-12-11 21:25:42 +00001213SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
1214 DAGCombinerInfo &DCI) const {
1215 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001216 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00001217 EVT VT = N->getValueType(0);
1218
1219 switch (N->getOpcode()) {
Tom Stellard50122a52014-04-07 19:45:41 +00001220 default: return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001221 case ISD::SELECT_CC: {
Tom Stellard75aadc22012-12-11 21:25:42 +00001222 ConstantSDNode *True, *False;
1223 // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
1224 if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1225 && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1226 && True->isAllOnesValue()
1227 && False->isNullValue()
1228 && VT == MVT::i1) {
1229 return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
1230 N->getOperand(1), N->getOperand(4));
1231
1232 }
1233 break;
1234 }
1235 case ISD::SETCC: {
1236 SDValue Arg0 = N->getOperand(0);
1237 SDValue Arg1 = N->getOperand(1);
1238 SDValue CC = N->getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00001239 ConstantSDNode * C = nullptr;
Tom Stellard75aadc22012-12-11 21:25:42 +00001240 ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
1241
1242 // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
1243 if (VT == MVT::i1
1244 && Arg0.getOpcode() == ISD::SIGN_EXTEND
1245 && Arg0.getOperand(0).getValueType() == MVT::i1
1246 && (C = dyn_cast<ConstantSDNode>(Arg1))
1247 && C->isNullValue()
1248 && CCOp == ISD::SETNE) {
1249 return SimplifySetCC(VT, Arg0.getOperand(0),
1250 DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
1251 }
1252 break;
1253 }
Matt Arsenault364a6742014-06-11 17:50:44 +00001254
1255 case AMDGPUISD::CVT_F32_UBYTE0:
1256 case AMDGPUISD::CVT_F32_UBYTE1:
1257 case AMDGPUISD::CVT_F32_UBYTE2:
1258 case AMDGPUISD::CVT_F32_UBYTE3: {
1259 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
1260
1261 SDValue Src = N->getOperand(0);
1262 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
1263
1264 APInt KnownZero, KnownOne;
1265 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
1266 !DCI.isBeforeLegalizeOps());
1267 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1268 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
1269 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
1270 DCI.CommitTargetLoweringOpt(TLO);
1271 }
1272
1273 break;
1274 }
1275
1276 case ISD::UINT_TO_FP: {
1277 return performUCharToFloatCombine(N, DCI);
1278 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001279 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00001280
1281 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001282}
Christian Konigd910b7d2013-02-26 17:52:16 +00001283
Matt Arsenault758659232013-05-18 00:21:46 +00001284/// \brief Test if RegClass is one of the VSrc classes
Christian Konigf82901a2013-02-26 17:52:23 +00001285static bool isVSrc(unsigned RegClass) {
1286 return AMDGPU::VSrc_32RegClassID == RegClass ||
1287 AMDGPU::VSrc_64RegClassID == RegClass;
1288}
1289
Matt Arsenault758659232013-05-18 00:21:46 +00001290/// \brief Test if RegClass is one of the SSrc classes
Christian Konigf82901a2013-02-26 17:52:23 +00001291static bool isSSrc(unsigned RegClass) {
1292 return AMDGPU::SSrc_32RegClassID == RegClass ||
1293 AMDGPU::SSrc_64RegClassID == RegClass;
1294}
1295
1296/// \brief Analyze the possible immediate value Op
1297///
1298/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
1299/// and the immediate value if it's a literal immediate
1300int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
1301
1302 union {
1303 int32_t I;
1304 float F;
1305 } Imm;
1306
Tom Stellardedbf1eb2013-04-05 23:31:20 +00001307 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
1308 if (Node->getZExtValue() >> 32) {
1309 return -1;
1310 }
Christian Konigf82901a2013-02-26 17:52:23 +00001311 Imm.I = Node->getSExtValue();
Tom Stellard7ed0b522014-04-03 20:19:27 +00001312 } else if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
1313 if (N->getValueType(0) != MVT::f32)
1314 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00001315 Imm.F = Node->getValueAPF().convertToFloat();
Tom Stellard7ed0b522014-04-03 20:19:27 +00001316 } else
Christian Konigf82901a2013-02-26 17:52:23 +00001317 return -1; // It isn't an immediate
1318
1319 if ((Imm.I >= -16 && Imm.I <= 64) ||
1320 Imm.F == 0.5f || Imm.F == -0.5f ||
1321 Imm.F == 1.0f || Imm.F == -1.0f ||
1322 Imm.F == 2.0f || Imm.F == -2.0f ||
1323 Imm.F == 4.0f || Imm.F == -4.0f)
1324 return 0; // It's an inline immediate
1325
1326 return Imm.I; // It's a literal immediate
1327}
1328
1329/// \brief Try to fold an immediate directly into an instruction
1330bool SITargetLowering::foldImm(SDValue &Operand, int32_t &Immediate,
1331 bool &ScalarSlotUsed) const {
1332
1333 MachineSDNode *Mov = dyn_cast<MachineSDNode>(Operand);
Bill Wendling37e9adb2013-06-07 20:28:55 +00001334 const SIInstrInfo *TII =
1335 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
Craig Topper062a2ba2014-04-25 05:30:21 +00001336 if (!Mov || !TII->isMov(Mov->getMachineOpcode()))
Christian Konigf82901a2013-02-26 17:52:23 +00001337 return false;
1338
1339 const SDValue &Op = Mov->getOperand(0);
1340 int32_t Value = analyzeImmediate(Op.getNode());
1341 if (Value == -1) {
1342 // Not an immediate at all
1343 return false;
1344
1345 } else if (Value == 0) {
1346 // Inline immediates can always be fold
1347 Operand = Op;
1348 return true;
1349
1350 } else if (Value == Immediate) {
1351 // Already fold literal immediate
1352 Operand = Op;
1353 return true;
1354
1355 } else if (!ScalarSlotUsed && !Immediate) {
1356 // Fold this literal immediate
1357 ScalarSlotUsed = true;
1358 Immediate = Value;
1359 Operand = Op;
1360 return true;
1361
1362 }
1363
1364 return false;
1365}
1366
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001367const TargetRegisterClass *SITargetLowering::getRegClassForNode(
1368 SelectionDAG &DAG, const SDValue &Op) const {
1369 const SIInstrInfo *TII =
1370 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1371 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1372
1373 if (!Op->isMachineOpcode()) {
1374 switch(Op->getOpcode()) {
1375 case ISD::CopyFromReg: {
1376 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1377 unsigned Reg = cast<RegisterSDNode>(Op->getOperand(1))->getReg();
1378 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1379 return MRI.getRegClass(Reg);
1380 }
1381 return TRI.getPhysRegClass(Reg);
1382 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001383 default: return nullptr;
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001384 }
1385 }
1386 const MCInstrDesc &Desc = TII->get(Op->getMachineOpcode());
1387 int OpClassID = Desc.OpInfo[Op.getResNo()].RegClass;
1388 if (OpClassID != -1) {
1389 return TRI.getRegClass(OpClassID);
1390 }
1391 switch(Op.getMachineOpcode()) {
1392 case AMDGPU::COPY_TO_REGCLASS:
1393 // Operand 1 is the register class id for COPY_TO_REGCLASS instructions.
1394 OpClassID = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1395
1396 // If the COPY_TO_REGCLASS instruction is copying to a VSrc register
1397 // class, then the register class for the value could be either a
1398 // VReg or and SReg. In order to get a more accurate
1399 if (OpClassID == AMDGPU::VSrc_32RegClassID ||
1400 OpClassID == AMDGPU::VSrc_64RegClassID) {
1401 return getRegClassForNode(DAG, Op.getOperand(0));
1402 }
1403 return TRI.getRegClass(OpClassID);
1404 case AMDGPU::EXTRACT_SUBREG: {
1405 int SubIdx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1406 const TargetRegisterClass *SuperClass =
1407 getRegClassForNode(DAG, Op.getOperand(0));
1408 return TRI.getSubClassWithSubReg(SuperClass, SubIdx);
1409 }
1410 case AMDGPU::REG_SEQUENCE:
1411 // Operand 0 is the register class id for REG_SEQUENCE instructions.
1412 return TRI.getRegClass(
1413 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue());
1414 default:
1415 return getRegClassFor(Op.getSimpleValueType());
1416 }
1417}
1418
Christian Konigf82901a2013-02-26 17:52:23 +00001419/// \brief Does "Op" fit into register class "RegClass" ?
Tom Stellardb35efba2013-05-20 15:02:01 +00001420bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, const SDValue &Op,
Christian Konigf82901a2013-02-26 17:52:23 +00001421 unsigned RegClass) const {
Bill Wendling37e9adb2013-06-07 20:28:55 +00001422 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001423 const TargetRegisterClass *RC = getRegClassForNode(DAG, Op);
1424 if (!RC) {
Christian Konigf82901a2013-02-26 17:52:23 +00001425 return false;
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001426 }
1427 return TRI->getRegClass(RegClass)->hasSubClassEq(RC);
Christian Konigf82901a2013-02-26 17:52:23 +00001428}
1429
1430/// \brief Make sure that we don't exeed the number of allowed scalars
1431void SITargetLowering::ensureSRegLimit(SelectionDAG &DAG, SDValue &Operand,
1432 unsigned RegClass,
1433 bool &ScalarSlotUsed) const {
1434
1435 // First map the operands register class to a destination class
1436 if (RegClass == AMDGPU::VSrc_32RegClassID)
1437 RegClass = AMDGPU::VReg_32RegClassID;
1438 else if (RegClass == AMDGPU::VSrc_64RegClassID)
1439 RegClass = AMDGPU::VReg_64RegClassID;
1440 else
1441 return;
1442
Alp Tokercb402912014-01-24 17:20:08 +00001443 // Nothing to do if they fit naturally
Christian Konigf82901a2013-02-26 17:52:23 +00001444 if (fitsRegClass(DAG, Operand, RegClass))
1445 return;
1446
1447 // If the scalar slot isn't used yet use it now
1448 if (!ScalarSlotUsed) {
1449 ScalarSlotUsed = true;
1450 return;
1451 }
1452
Matt Arsenault1408b602013-10-10 23:05:37 +00001453 // This is a conservative aproach. It is possible that we can't determine the
1454 // correct register class and copy too often, but better safe than sorry.
Christian Konigf82901a2013-02-26 17:52:23 +00001455 SDValue RC = DAG.getTargetConstant(RegClass, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001456 SDNode *Node = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, SDLoc(),
Christian Konigf82901a2013-02-26 17:52:23 +00001457 Operand.getValueType(), Operand, RC);
1458 Operand = SDValue(Node, 0);
1459}
1460
Tom Stellardacec99c2013-06-05 23:39:50 +00001461/// \returns true if \p Node's operands are different from the SDValue list
1462/// \p Ops
1463static bool isNodeChanged(const SDNode *Node, const std::vector<SDValue> &Ops) {
1464 for (unsigned i = 0, e = Node->getNumOperands(); i < e; ++i) {
1465 if (Ops[i].getNode() != Node->getOperand(i).getNode()) {
1466 return true;
1467 }
1468 }
1469 return false;
1470}
1471
Christian Konig8e06e2a2013-04-10 08:39:08 +00001472/// \brief Try to fold the Nodes operands into the Node
1473SDNode *SITargetLowering::foldOperands(MachineSDNode *Node,
1474 SelectionDAG &DAG) const {
Christian Konigf82901a2013-02-26 17:52:23 +00001475
1476 // Original encoding (either e32 or e64)
1477 int Opcode = Node->getMachineOpcode();
Bill Wendling37e9adb2013-06-07 20:28:55 +00001478 const SIInstrInfo *TII =
1479 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00001480 const MCInstrDesc *Desc = &TII->get(Opcode);
1481
1482 unsigned NumDefs = Desc->getNumDefs();
1483 unsigned NumOps = Desc->getNumOperands();
1484
Christian Konig3c145802013-03-27 09:12:59 +00001485 // Commuted opcode if available
1486 int OpcodeRev = Desc->isCommutable() ? TII->commuteOpcode(Opcode) : -1;
Craig Topper062a2ba2014-04-25 05:30:21 +00001487 const MCInstrDesc *DescRev = OpcodeRev == -1 ? nullptr : &TII->get(OpcodeRev);
Christian Konig3c145802013-03-27 09:12:59 +00001488
1489 assert(!DescRev || DescRev->getNumDefs() == NumDefs);
1490 assert(!DescRev || DescRev->getNumOperands() == NumOps);
1491
Christian Konige500e442013-02-26 17:52:47 +00001492 // e64 version if available, -1 otherwise
1493 int OpcodeE64 = AMDGPU::getVOPe64(Opcode);
Craig Topper062a2ba2014-04-25 05:30:21 +00001494 const MCInstrDesc *DescE64 = OpcodeE64 == -1 ? nullptr : &TII->get(OpcodeE64);
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001495 int InputModifiers[3] = {0};
Christian Konige500e442013-02-26 17:52:47 +00001496
1497 assert(!DescE64 || DescE64->getNumDefs() == NumDefs);
Christian Konige500e442013-02-26 17:52:47 +00001498
Christian Konigf82901a2013-02-26 17:52:23 +00001499 int32_t Immediate = Desc->getSize() == 4 ? 0 : -1;
1500 bool HaveVSrc = false, HaveSSrc = false;
1501
Matt Arsenault08d84942014-06-03 23:06:13 +00001502 // First figure out what we already have in this instruction.
Christian Konigf82901a2013-02-26 17:52:23 +00001503 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1504 i != e && Op < NumOps; ++i, ++Op) {
1505
1506 unsigned RegClass = Desc->OpInfo[Op].RegClass;
1507 if (isVSrc(RegClass))
1508 HaveVSrc = true;
1509 else if (isSSrc(RegClass))
1510 HaveSSrc = true;
1511 else
1512 continue;
1513
1514 int32_t Imm = analyzeImmediate(Node->getOperand(i).getNode());
1515 if (Imm != -1 && Imm != 0) {
1516 // Literal immediate
1517 Immediate = Imm;
1518 }
1519 }
1520
Matt Arsenault08d84942014-06-03 23:06:13 +00001521 // If we neither have VSrc nor SSrc, it makes no sense to continue.
Christian Konigf82901a2013-02-26 17:52:23 +00001522 if (!HaveVSrc && !HaveSSrc)
1523 return Node;
1524
1525 // No scalar allowed when we have both VSrc and SSrc
1526 bool ScalarSlotUsed = HaveVSrc && HaveSSrc;
1527
1528 // Second go over the operands and try to fold them
1529 std::vector<SDValue> Ops;
Christian Konige500e442013-02-26 17:52:47 +00001530 bool Promote2e64 = false;
Christian Konigf82901a2013-02-26 17:52:23 +00001531 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1532 i != e && Op < NumOps; ++i, ++Op) {
1533
1534 const SDValue &Operand = Node->getOperand(i);
1535 Ops.push_back(Operand);
1536
Matt Arsenault08d84942014-06-03 23:06:13 +00001537 // Already folded immediate?
Christian Konigf82901a2013-02-26 17:52:23 +00001538 if (isa<ConstantSDNode>(Operand.getNode()) ||
1539 isa<ConstantFPSDNode>(Operand.getNode()))
1540 continue;
1541
Matt Arsenault08d84942014-06-03 23:06:13 +00001542 // Is this a VSrc or SSrc operand?
Christian Konigf82901a2013-02-26 17:52:23 +00001543 unsigned RegClass = Desc->OpInfo[Op].RegClass;
Christian Konig8370dbb2013-03-26 14:04:17 +00001544 if (isVSrc(RegClass) || isSSrc(RegClass)) {
1545 // Try to fold the immediates
1546 if (!foldImm(Ops[i], Immediate, ScalarSlotUsed)) {
Matt Arsenault08d84942014-06-03 23:06:13 +00001547 // Folding didn't work, make sure we don't hit the SReg limit.
Christian Konig8370dbb2013-03-26 14:04:17 +00001548 ensureSRegLimit(DAG, Ops[i], RegClass, ScalarSlotUsed);
1549 }
1550 continue;
1551 }
Christian Konig6612ac32013-02-26 17:52:36 +00001552
Christian Konig3c145802013-03-27 09:12:59 +00001553 if (i == 1 && DescRev && fitsRegClass(DAG, Ops[0], RegClass)) {
Christian Konig6612ac32013-02-26 17:52:36 +00001554
Christian Konig8370dbb2013-03-26 14:04:17 +00001555 unsigned OtherRegClass = Desc->OpInfo[NumDefs].RegClass;
1556 assert(isVSrc(OtherRegClass) || isSSrc(OtherRegClass));
1557
1558 // Test if it makes sense to swap operands
1559 if (foldImm(Ops[1], Immediate, ScalarSlotUsed) ||
1560 (!fitsRegClass(DAG, Ops[1], RegClass) &&
1561 fitsRegClass(DAG, Ops[1], OtherRegClass))) {
Christian Konig6612ac32013-02-26 17:52:36 +00001562
1563 // Swap commutable operands
Matt Arsenault4be76e92014-04-07 16:44:26 +00001564 std::swap(Ops[0], Ops[1]);
Christian Konig3c145802013-03-27 09:12:59 +00001565
1566 Desc = DescRev;
Craig Topper062a2ba2014-04-25 05:30:21 +00001567 DescRev = nullptr;
Christian Konig8370dbb2013-03-26 14:04:17 +00001568 continue;
Christian Konig6612ac32013-02-26 17:52:36 +00001569 }
Christian Konig6612ac32013-02-26 17:52:36 +00001570 }
Christian Konigf82901a2013-02-26 17:52:23 +00001571
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001572 if (Immediate)
1573 continue;
1574
1575 if (DescE64) {
Christian Konig8370dbb2013-03-26 14:04:17 +00001576 // Test if it makes sense to switch to e64 encoding
1577 unsigned OtherRegClass = DescE64->OpInfo[Op].RegClass;
1578 if (!isVSrc(OtherRegClass) && !isSSrc(OtherRegClass))
1579 continue;
1580
1581 int32_t TmpImm = -1;
1582 if (foldImm(Ops[i], TmpImm, ScalarSlotUsed) ||
1583 (!fitsRegClass(DAG, Ops[i], RegClass) &&
1584 fitsRegClass(DAG, Ops[1], OtherRegClass))) {
1585
1586 // Switch to e64 encoding
1587 Immediate = -1;
1588 Promote2e64 = true;
1589 Desc = DescE64;
Craig Topper062a2ba2014-04-25 05:30:21 +00001590 DescE64 = nullptr;
Christian Konig8370dbb2013-03-26 14:04:17 +00001591 }
Christian Konigf82901a2013-02-26 17:52:23 +00001592 }
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001593
1594 if (!DescE64 && !Promote2e64)
1595 continue;
1596 if (!Operand.isMachineOpcode())
1597 continue;
1598 if (Operand.getMachineOpcode() == AMDGPU::FNEG_SI) {
1599 Ops.pop_back();
1600 Ops.push_back(Operand.getOperand(0));
1601 InputModifiers[i] = 1;
1602 Promote2e64 = true;
1603 if (!DescE64)
1604 continue;
1605 Desc = DescE64;
Matt Arsenaultc6f338d2014-06-05 00:01:12 +00001606 DescE64 = nullptr;
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001607 }
1608 else if (Operand.getMachineOpcode() == AMDGPU::FABS_SI) {
1609 Ops.pop_back();
1610 Ops.push_back(Operand.getOperand(0));
1611 InputModifiers[i] = 2;
1612 Promote2e64 = true;
1613 if (!DescE64)
1614 continue;
1615 Desc = DescE64;
Matt Arsenaultc6f338d2014-06-05 00:01:12 +00001616 DescE64 = nullptr;
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001617 }
Christian Konigf82901a2013-02-26 17:52:23 +00001618 }
1619
Christian Konige500e442013-02-26 17:52:47 +00001620 if (Promote2e64) {
Vincent Lejeune94af31f2014-05-10 19:18:33 +00001621 std::vector<SDValue> OldOps(Ops);
1622 Ops.clear();
1623 for (unsigned i = 0; i < OldOps.size(); ++i) {
1624 // src_modifier
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001625 Ops.push_back(DAG.getTargetConstant(InputModifiers[i], MVT::i32));
Vincent Lejeune94af31f2014-05-10 19:18:33 +00001626 Ops.push_back(OldOps[i]);
1627 }
Christian Konige500e442013-02-26 17:52:47 +00001628 // Add the modifier flags while promoting
Vincent Lejeune94af31f2014-05-10 19:18:33 +00001629 for (unsigned i = 0; i < 2; ++i)
Christian Konige500e442013-02-26 17:52:47 +00001630 Ops.push_back(DAG.getTargetConstant(0, MVT::i32));
1631 }
1632
Christian Konigf82901a2013-02-26 17:52:23 +00001633 // Add optional chain and glue
1634 for (unsigned i = NumOps - NumDefs, e = Node->getNumOperands(); i < e; ++i)
1635 Ops.push_back(Node->getOperand(i));
1636
Tom Stellardb5a97002013-06-03 17:39:50 +00001637 // Nodes that have a glue result are not CSE'd by getMachineNode(), so in
1638 // this case a brand new node is always be created, even if the operands
1639 // are the same as before. So, manually check if anything has been changed.
Tom Stellardacec99c2013-06-05 23:39:50 +00001640 if (Desc->Opcode == Opcode && !isNodeChanged(Node, Ops)) {
1641 return Node;
Tom Stellardb5a97002013-06-03 17:39:50 +00001642 }
1643
Christian Konig3c145802013-03-27 09:12:59 +00001644 // Create a complete new instruction
Andrew Trickef9de2a2013-05-25 02:42:55 +00001645 return DAG.getMachineNode(Desc->Opcode, SDLoc(Node), Node->getVTList(), Ops);
Christian Konigd910b7d2013-02-26 17:52:16 +00001646}
Christian Konig8e06e2a2013-04-10 08:39:08 +00001647
1648/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00001649static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00001650 switch (Idx) {
1651 default: return 0;
1652 case AMDGPU::sub0: return 0;
1653 case AMDGPU::sub1: return 1;
1654 case AMDGPU::sub2: return 2;
1655 case AMDGPU::sub3: return 3;
1656 }
1657}
1658
1659/// \brief Adjust the writemask of MIMG instructions
1660void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
1661 SelectionDAG &DAG) const {
1662 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00001663 unsigned Lane = 0;
1664 unsigned OldDmask = Node->getConstantOperandVal(0);
1665 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001666
1667 // Try to figure out the used register components
1668 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
1669 I != E; ++I) {
1670
1671 // Abort if we can't understand the usage
1672 if (!I->isMachineOpcode() ||
1673 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
1674 return;
1675
Tom Stellard54774e52013-10-23 02:53:47 +00001676 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
1677 // Note that subregs are packed, i.e. Lane==0 is the first bit set
1678 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
1679 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00001680 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00001681
Tom Stellard54774e52013-10-23 02:53:47 +00001682 // Set which texture component corresponds to the lane.
1683 unsigned Comp;
1684 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
1685 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00001686 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00001687 Dmask &= ~(1 << Comp);
1688 }
1689
Christian Konig8e06e2a2013-04-10 08:39:08 +00001690 // Abort if we have more than one user per component
1691 if (Users[Lane])
1692 return;
1693
1694 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00001695 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001696 }
1697
Tom Stellard54774e52013-10-23 02:53:47 +00001698 // Abort if there's no change
1699 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00001700 return;
1701
1702 // Adjust the writemask in the node
1703 std::vector<SDValue> Ops;
Tom Stellard54774e52013-10-23 02:53:47 +00001704 Ops.push_back(DAG.getTargetConstant(NewDmask, MVT::i32));
Christian Konig8e06e2a2013-04-10 08:39:08 +00001705 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1706 Ops.push_back(Node->getOperand(i));
Craig Topper8c0b4d02014-04-28 05:57:50 +00001707 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00001708
Christian Konig8b1ed282013-04-10 08:39:16 +00001709 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00001710 // (if NewDmask has only one bit set...)
1711 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Christian Konig8b1ed282013-04-10 08:39:16 +00001712 SDValue RC = DAG.getTargetConstant(AMDGPU::VReg_32RegClassID, MVT::i32);
1713 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001714 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00001715 SDValue(Node, 0), RC);
1716 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
1717 return;
1718 }
1719
Christian Konig8e06e2a2013-04-10 08:39:08 +00001720 // Update the users of the node with the new indices
1721 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
1722
1723 SDNode *User = Users[i];
1724 if (!User)
1725 continue;
1726
1727 SDValue Op = DAG.getTargetConstant(Idx, MVT::i32);
1728 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
1729
1730 switch (Idx) {
1731 default: break;
1732 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
1733 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
1734 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
1735 }
1736 }
1737}
1738
Matt Arsenault08d84942014-06-03 23:06:13 +00001739/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00001740SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
1741 SelectionDAG &DAG) const {
Tom Stellard16a9a202013-08-14 23:24:17 +00001742 const SIInstrInfo *TII =
1743 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
Tom Stellard0518ff82013-06-03 17:39:58 +00001744 Node = AdjustRegClass(Node, DAG);
Christian Konig8e06e2a2013-04-10 08:39:08 +00001745
Tom Stellard16a9a202013-08-14 23:24:17 +00001746 if (TII->isMIMG(Node->getMachineOpcode()))
Christian Konig8e06e2a2013-04-10 08:39:08 +00001747 adjustWritemask(Node, DAG);
1748
1749 return foldOperands(Node, DAG);
1750}
Christian Konig8b1ed282013-04-10 08:39:16 +00001751
1752/// \brief Assign the register class depending on the number of
1753/// bits set in the writemask
1754void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
1755 SDNode *Node) const {
Tom Stellard16a9a202013-08-14 23:24:17 +00001756 const SIInstrInfo *TII =
1757 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1758 if (!TII->isMIMG(MI->getOpcode()))
Christian Konig8b1ed282013-04-10 08:39:16 +00001759 return;
1760
1761 unsigned VReg = MI->getOperand(0).getReg();
1762 unsigned Writemask = MI->getOperand(1).getImm();
1763 unsigned BitsSet = 0;
1764 for (unsigned i = 0; i < 4; ++i)
1765 BitsSet += Writemask & (1 << i) ? 1 : 0;
1766
1767 const TargetRegisterClass *RC;
1768 switch (BitsSet) {
1769 default: return;
1770 case 1: RC = &AMDGPU::VReg_32RegClass; break;
1771 case 2: RC = &AMDGPU::VReg_64RegClass; break;
1772 case 3: RC = &AMDGPU::VReg_96RegClass; break;
1773 }
1774
Tom Stellard682bfbc2013-10-10 17:11:24 +00001775 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
1776 MI->setDesc(TII->get(NewOpcode));
Christian Konig8b1ed282013-04-10 08:39:16 +00001777 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
1778 MRI.setRegClass(VReg, RC);
1779}
Tom Stellard0518ff82013-06-03 17:39:58 +00001780
1781MachineSDNode *SITargetLowering::AdjustRegClass(MachineSDNode *N,
1782 SelectionDAG &DAG) const {
1783
1784 SDLoc DL(N);
1785 unsigned NewOpcode = N->getMachineOpcode();
1786
1787 switch (N->getMachineOpcode()) {
1788 default: return N;
Tom Stellard0518ff82013-06-03 17:39:58 +00001789 case AMDGPU::S_LOAD_DWORD_IMM:
1790 NewOpcode = AMDGPU::BUFFER_LOAD_DWORD_ADDR64;
1791 // Fall-through
1792 case AMDGPU::S_LOAD_DWORDX2_SGPR:
1793 if (NewOpcode == N->getMachineOpcode()) {
1794 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64;
1795 }
1796 // Fall-through
1797 case AMDGPU::S_LOAD_DWORDX4_IMM:
1798 case AMDGPU::S_LOAD_DWORDX4_SGPR: {
1799 if (NewOpcode == N->getMachineOpcode()) {
1800 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64;
1801 }
1802 if (fitsRegClass(DAG, N->getOperand(0), AMDGPU::SReg_64RegClassID)) {
1803 return N;
1804 }
1805 ConstantSDNode *Offset = cast<ConstantSDNode>(N->getOperand(1));
1806 SDValue Ops[] = {
1807 SDValue(DAG.getMachineNode(AMDGPU::SI_ADDR64_RSRC, DL, MVT::i128,
1808 DAG.getConstant(0, MVT::i64)), 0),
1809 N->getOperand(0),
1810 DAG.getConstant(Offset->getSExtValue() << 2, MVT::i32)
1811 };
1812 return DAG.getMachineNode(NewOpcode, DL, N->getVTList(), Ops);
1813 }
1814 }
1815}
Tom Stellard94593ee2013-06-03 17:40:18 +00001816
1817SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
1818 const TargetRegisterClass *RC,
1819 unsigned Reg, EVT VT) const {
1820 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
1821
1822 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
1823 cast<RegisterSDNode>(VReg)->getReg(), VT);
1824}