blob: 56e760cf517dc0a415e11aedc9297ac839d81067 [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 Arsenaulte9fa3b82014-07-15 20:18:31 +0000224 setOperationAction(ISD::FDIV, MVT::f32, Custom);
225
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000226 setTargetDAGCombine(ISD::SELECT_CC);
Tom Stellard75aadc22012-12-11 21:25:42 +0000227 setTargetDAGCombine(ISD::SETCC);
Michel Danzerf52a6722013-03-08 10:58:01 +0000228
Matt Arsenault364a6742014-06-11 17:50:44 +0000229 setTargetDAGCombine(ISD::UINT_TO_FP);
230
Christian Konigeecebd02013-03-26 14:04:02 +0000231 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000232}
233
Tom Stellard0125f2a2013-06-25 02:39:35 +0000234//===----------------------------------------------------------------------===//
235// TargetLowering queries
236//===----------------------------------------------------------------------===//
237
238bool SITargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
Matt Arsenault25793a32014-02-05 23:15:53 +0000239 unsigned AddrSpace,
Tom Stellard0125f2a2013-06-25 02:39:35 +0000240 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000241 if (IsFast)
242 *IsFast = false;
243
Tom Stellard0125f2a2013-06-25 02:39:35 +0000244 // XXX: This depends on the address space and also we may want to revist
245 // the alignment values we specify in the DataLayout.
Matt Arsenault1018c892014-04-24 17:08:26 +0000246
247 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
248 // which isn't a simple VT.
Tom Stellard81d871d2013-11-13 23:36:50 +0000249 if (!VT.isSimple() || VT == MVT::Other)
250 return false;
Matt Arsenault1018c892014-04-24 17:08:26 +0000251
252 // XXX - CI changes say "Support for unaligned memory accesses" but I don't
253 // see what for specifically. The wording everywhere else seems to be the
254 // same.
255
256 // 3.6.4 - Operations using pairs of VGPRs (for example: double-floats) have
257 // no alignment restrictions.
258 if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS) {
259 // Using any pair of GPRs should be the same as any other pair.
260 if (IsFast)
261 *IsFast = true;
262 return VT.bitsGE(MVT::i64);
263 }
264
265 // XXX - The only mention I see of this in the ISA manual is for LDS direct
266 // reads the "byte address and must be dword aligned". Is it also true for the
267 // normal loads and stores?
268 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS)
269 return false;
270
271 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
272 // byte-address are ignored, thus forcing Dword alignment.
273 if (IsFast)
274 *IsFast = true;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000275 return VT.bitsGT(MVT::i32);
276}
277
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000278TargetLoweringBase::LegalizeTypeAction
279SITargetLowering::getPreferredVectorAction(EVT VT) const {
280 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
281 return TypeSplitVector;
282
283 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000284}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000285
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000286bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
287 Type *Ty) const {
288 const SIInstrInfo *TII =
289 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
290 return TII->isInlineConstant(Imm);
291}
292
Tom Stellardaf775432013-10-23 00:44:32 +0000293SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
Tom Stellard94593ee2013-06-03 17:40:18 +0000294 SDLoc DL, SDValue Chain,
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000295 unsigned Offset, bool Signed) const {
Tom Stellard94593ee2013-06-03 17:40:18 +0000296 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
297 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
298 AMDGPUAS::CONSTANT_ADDRESS);
Tom Stellard94593ee2013-06-03 17:40:18 +0000299 SDValue BasePtr = DAG.getCopyFromReg(Chain, DL,
300 MRI.getLiveInVirtReg(AMDGPU::SGPR0_SGPR1), MVT::i64);
301 SDValue Ptr = DAG.getNode(ISD::ADD, DL, MVT::i64, BasePtr,
302 DAG.getConstant(Offset, MVT::i64));
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000303 return DAG.getExtLoad(Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD, DL, VT, Chain, Ptr,
Tom Stellardaf775432013-10-23 00:44:32 +0000304 MachinePointerInfo(UndefValue::get(PtrTy)), MemVT,
305 false, false, MemVT.getSizeInBits() >> 3);
Tom Stellard94593ee2013-06-03 17:40:18 +0000306
307}
308
Christian Konig2c8f6d52013-03-07 09:03:52 +0000309SDValue SITargetLowering::LowerFormalArguments(
310 SDValue Chain,
311 CallingConv::ID CallConv,
312 bool isVarArg,
313 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000314 SDLoc DL, SelectionDAG &DAG,
Christian Konig2c8f6d52013-03-07 09:03:52 +0000315 SmallVectorImpl<SDValue> &InVals) const {
316
317 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
318
319 MachineFunction &MF = DAG.getMachineFunction();
320 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000321 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000322
323 assert(CallConv == CallingConv::C);
324
325 SmallVector<ISD::InputArg, 16> Splits;
Christian Konig99ee0f42013-03-07 09:04:14 +0000326 uint32_t Skipped = 0;
327
328 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000329 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000330
331 // First check if it's a PS input addr
Matt Arsenault762af962014-07-13 03:06:39 +0000332 if (Info->getShaderType() == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
Vincent Lejeuned6236442013-10-13 17:56:16 +0000333 !Arg.Flags.isByVal()) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000334
335 assert((PSInputNum <= 15) && "Too many PS inputs!");
336
337 if (!Arg.Used) {
338 // We can savely skip PS inputs
339 Skipped |= 1 << i;
340 ++PSInputNum;
341 continue;
342 }
343
344 Info->PSInputAddr |= 1 << PSInputNum++;
345 }
346
347 // Second split vertices into their elements
Matt Arsenault762af962014-07-13 03:06:39 +0000348 if (Info->getShaderType() != ShaderType::COMPUTE && Arg.VT.isVector()) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000349 ISD::InputArg NewArg = Arg;
350 NewArg.Flags.setSplit();
351 NewArg.VT = Arg.VT.getVectorElementType();
352
353 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
354 // three or five element vertex only needs three or five registers,
355 // NOT four or eigth.
356 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
357 unsigned NumElements = ParamType->getVectorNumElements();
358
359 for (unsigned j = 0; j != NumElements; ++j) {
360 Splits.push_back(NewArg);
361 NewArg.PartOffset += NewArg.VT.getStoreSize();
362 }
363
Matt Arsenault762af962014-07-13 03:06:39 +0000364 } else if (Info->getShaderType() != ShaderType::COMPUTE) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000365 Splits.push_back(Arg);
366 }
367 }
368
369 SmallVector<CCValAssign, 16> ArgLocs;
370 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
371 getTargetMachine(), ArgLocs, *DAG.getContext());
372
Christian Konig99ee0f42013-03-07 09:04:14 +0000373 // At least one interpolation mode must be enabled or else the GPU will hang.
Matt Arsenault762af962014-07-13 03:06:39 +0000374 if (Info->getShaderType() == ShaderType::PIXEL &&
375 (Info->PSInputAddr & 0x7F) == 0) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000376 Info->PSInputAddr |= 1;
377 CCInfo.AllocateReg(AMDGPU::VGPR0);
378 CCInfo.AllocateReg(AMDGPU::VGPR1);
379 }
380
Tom Stellarded882c22013-06-03 17:40:11 +0000381 // The pointer to the list of arguments is stored in SGPR0, SGPR1
Matt Arsenault762af962014-07-13 03:06:39 +0000382 if (Info->getShaderType() == ShaderType::COMPUTE) {
Tom Stellarded882c22013-06-03 17:40:11 +0000383 CCInfo.AllocateReg(AMDGPU::SGPR0);
384 CCInfo.AllocateReg(AMDGPU::SGPR1);
Tom Stellard94593ee2013-06-03 17:40:18 +0000385 MF.addLiveIn(AMDGPU::SGPR0_SGPR1, &AMDGPU::SReg_64RegClass);
Tom Stellarded882c22013-06-03 17:40:11 +0000386 }
387
Matt Arsenault762af962014-07-13 03:06:39 +0000388 if (Info->getShaderType() == ShaderType::COMPUTE) {
Tom Stellardaf775432013-10-23 00:44:32 +0000389 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
390 Splits);
391 }
392
Christian Konig2c8f6d52013-03-07 09:03:52 +0000393 AnalyzeFormalArguments(CCInfo, Splits);
394
395 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
396
Christian Konigb7be72d2013-05-17 09:46:48 +0000397 const ISD::InputArg &Arg = Ins[i];
Christian Konig99ee0f42013-03-07 09:04:14 +0000398 if (Skipped & (1 << i)) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000399 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000400 continue;
401 }
402
Christian Konig2c8f6d52013-03-07 09:03:52 +0000403 CCValAssign &VA = ArgLocs[ArgIdx++];
Tom Stellarded882c22013-06-03 17:40:11 +0000404 EVT VT = VA.getLocVT();
405
406 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000407 VT = Ins[i].VT;
408 EVT MemVT = Splits[i].VT;
Tom Stellard94593ee2013-06-03 17:40:18 +0000409 // The first 36 bytes of the input buffer contains information about
410 // thread group and global sizes.
Tom Stellardaf775432013-10-23 00:44:32 +0000411 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, DAG.getRoot(),
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000412 36 + VA.getLocMemOffset(),
413 Ins[i].Flags.isSExt());
Tom Stellarded882c22013-06-03 17:40:11 +0000414 InVals.push_back(Arg);
415 continue;
416 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000417 assert(VA.isRegLoc() && "Parameter must be in a register!");
418
419 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000420
421 if (VT == MVT::i64) {
422 // For now assume it is a pointer
423 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
424 &AMDGPU::SReg_64RegClass);
425 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
426 InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
427 continue;
428 }
429
430 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
431
432 Reg = MF.addLiveIn(Reg, RC);
433 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
434
Christian Konig2c8f6d52013-03-07 09:03:52 +0000435 if (Arg.VT.isVector()) {
436
437 // Build a vector from the registers
438 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
439 unsigned NumElements = ParamType->getVectorNumElements();
440
441 SmallVector<SDValue, 4> Regs;
442 Regs.push_back(Val);
443 for (unsigned j = 1; j != NumElements; ++j) {
444 Reg = ArgLocs[ArgIdx++].getLocReg();
445 Reg = MF.addLiveIn(Reg, RC);
446 Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
447 }
448
449 // Fill up the missing vector elements
450 NumElements = Arg.VT.getVectorNumElements() - NumElements;
451 for (unsigned j = 0; j != NumElements; ++j)
452 Regs.push_back(DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000453
Craig Topper48d114b2014-04-26 18:35:24 +0000454 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000455 continue;
456 }
457
458 InVals.push_back(Val);
459 }
460 return Chain;
461}
462
Tom Stellard75aadc22012-12-11 21:25:42 +0000463MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
464 MachineInstr * MI, MachineBasicBlock * BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000465
Tom Stellard556d9aa2013-06-03 17:39:37 +0000466 MachineBasicBlock::iterator I = *MI;
Tom Stellard919bb6b2014-04-29 23:12:53 +0000467 const SIInstrInfo *TII =
468 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
469 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Tom Stellard556d9aa2013-06-03 17:39:37 +0000470
Tom Stellard75aadc22012-12-11 21:25:42 +0000471 switch (MI->getOpcode()) {
472 default:
473 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
474 case AMDGPU::BRANCH: return BB;
Tom Stellard556d9aa2013-06-03 17:39:37 +0000475 case AMDGPU::SI_ADDR64_RSRC: {
Tom Stellard556d9aa2013-06-03 17:39:37 +0000476 unsigned SuperReg = MI->getOperand(0).getReg();
Tom Stellarddef38c52014-03-21 15:51:53 +0000477 unsigned SubRegLo = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass);
478 unsigned SubRegHi = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass);
479 unsigned SubRegHiHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
480 unsigned SubRegHiLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
Tom Stellard556d9aa2013-06-03 17:39:37 +0000481 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B64), SubRegLo)
482 .addOperand(MI->getOperand(1));
483 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiLo)
484 .addImm(0);
485 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiHi)
Tom Stellard15834092014-03-21 15:51:57 +0000486 .addImm(AMDGPU::RSRC_DATA_FORMAT >> 32);
Tom Stellard556d9aa2013-06-03 17:39:37 +0000487 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SubRegHi)
488 .addReg(SubRegHiLo)
489 .addImm(AMDGPU::sub0)
490 .addReg(SubRegHiHi)
491 .addImm(AMDGPU::sub1);
492 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SuperReg)
493 .addReg(SubRegLo)
494 .addImm(AMDGPU::sub0_sub1)
495 .addReg(SubRegHi)
496 .addImm(AMDGPU::sub2_sub3);
497 MI->eraseFromParent();
498 break;
499 }
Matt Arsenaultdbc9aae2014-06-18 17:13:51 +0000500 case AMDGPU::V_SUB_F64: {
501 unsigned DestReg = MI->getOperand(0).getReg();
502 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F64), DestReg)
503 .addImm(0) // SRC0 modifiers
504 .addReg(MI->getOperand(1).getReg())
505 .addImm(1) // SRC1 modifiers
506 .addReg(MI->getOperand(2).getReg())
507 .addImm(0) // SRC2 modifiers
508 .addImm(0) // src2
509 .addImm(0) // CLAMP
510 .addImm(0); // OMOD
Tom Stellard2a6a61052013-07-12 18:15:08 +0000511 MI->eraseFromParent();
512 break;
Matt Arsenaultdbc9aae2014-06-18 17:13:51 +0000513 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000514 case AMDGPU::SI_RegisterStorePseudo: {
515 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Tom Stellard81d871d2013-11-13 23:36:50 +0000516 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
517 MachineInstrBuilder MIB =
518 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::SI_RegisterStore),
519 Reg);
520 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
521 MIB.addOperand(MI->getOperand(i));
522
523 MI->eraseFromParent();
Vincent Lejeune79a58342014-05-10 19:18:25 +0000524 break;
525 }
526 case AMDGPU::FABS_SI: {
527 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
528 const SIInstrInfo *TII =
529 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
530 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
531 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_MOV_B32_e32),
532 Reg)
533 .addImm(0x7fffffff);
534 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_AND_B32_e32),
535 MI->getOperand(0).getReg())
536 .addReg(MI->getOperand(1).getReg())
537 .addReg(Reg);
538 MI->eraseFromParent();
539 break;
540 }
541 case AMDGPU::FNEG_SI: {
542 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
543 const SIInstrInfo *TII =
544 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
545 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
546 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_MOV_B32_e32),
547 Reg)
548 .addImm(0x80000000);
549 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_XOR_B32_e32),
550 MI->getOperand(0).getReg())
551 .addReg(MI->getOperand(1).getReg())
552 .addReg(Reg);
553 MI->eraseFromParent();
554 break;
555 }
556 case AMDGPU::FCLAMP_SI: {
557 const SIInstrInfo *TII =
558 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
559 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F32_e64),
560 MI->getOperand(0).getReg())
Vincent Lejeune94af31f2014-05-10 19:18:33 +0000561 .addImm(0) // SRC0 modifiers
Vincent Lejeune79a58342014-05-10 19:18:25 +0000562 .addOperand(MI->getOperand(1))
Vincent Lejeune94af31f2014-05-10 19:18:33 +0000563 .addImm(0) // SRC1 modifiers
Vincent Lejeune79a58342014-05-10 19:18:25 +0000564 .addImm(0) // SRC1
Vincent Lejeune79a58342014-05-10 19:18:25 +0000565 .addImm(1) // CLAMP
Vincent Lejeune94af31f2014-05-10 19:18:33 +0000566 .addImm(0); // OMOD
Vincent Lejeune79a58342014-05-10 19:18:25 +0000567 MI->eraseFromParent();
Tom Stellard81d871d2013-11-13 23:36:50 +0000568 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000569 }
570 return BB;
571}
572
Matt Arsenault758659232013-05-18 00:21:46 +0000573EVT SITargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +0000574 if (!VT.isVector()) {
575 return MVT::i1;
576 }
577 return MVT::getVectorVT(MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +0000578}
579
Christian Konig082a14a2013-03-18 11:34:05 +0000580MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const {
581 return MVT::i32;
582}
583
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +0000584bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
585 VT = VT.getScalarType();
586
587 if (!VT.isSimple())
588 return false;
589
590 switch (VT.getSimpleVT().SimpleTy) {
591 case MVT::f32:
592 return false; /* There is V_MAD_F32 for f32 */
593 case MVT::f64:
594 return true;
595 default:
596 break;
597 }
598
599 return false;
600}
601
Tom Stellard75aadc22012-12-11 21:25:42 +0000602//===----------------------------------------------------------------------===//
603// Custom DAG Lowering Operations
604//===----------------------------------------------------------------------===//
605
606SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Michel Danzer49812b52013-07-10 16:37:07 +0000607 MachineFunction &MF = DAG.getMachineFunction();
608 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellard75aadc22012-12-11 21:25:42 +0000609 switch (Op.getOpcode()) {
610 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +0000611 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +0000612 case ISD::LOAD: {
613 LoadSDNode *Load = dyn_cast<LoadSDNode>(Op);
Tom Stellard10ae6a02014-07-02 20:53:54 +0000614 EVT VT = Op.getValueType();
615
616 // These loads are legal.
617 if (Load->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS &&
618 VT.isVector() && VT.getVectorNumElements() == 2 &&
619 VT.getVectorElementType() == MVT::i32)
620 return SDValue();
621
Tom Stellard80be9652014-02-13 23:34:10 +0000622 if (Op.getValueType().isVector() &&
623 (Load->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
624 Load->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS ||
625 (Load->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
626 Op.getValueType().getVectorNumElements() > 4))) {
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000627 return SplitVectorLoad(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +0000628 } else {
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000629 SDValue Result = LowerLOAD(Op, DAG);
630 assert((!Result.getNode() ||
631 Result.getNode()->getNumValues() == 2) &&
632 "Load should return a value and a chain");
633 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +0000634 }
635 }
Tom Stellardaf775432013-10-23 00:44:32 +0000636
Tom Stellard0ec134f2014-02-04 17:18:40 +0000637 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000638 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +0000639 case ISD::STORE: return LowerSTORE(Op, DAG);
Michel Danzer49812b52013-07-10 16:37:07 +0000640 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +0000641 case ISD::INTRINSIC_WO_CHAIN: {
642 unsigned IntrinsicID =
643 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
644 EVT VT = Op.getValueType();
645 SDLoc DL(Op);
646 //XXX: Hardcoded we only use two to store the pointer to the parameters.
647 unsigned NumUserSGPRs = 2;
648 switch (IntrinsicID) {
649 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
650 case Intrinsic::r600_read_ngroups_x:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000651 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 0, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000652 case Intrinsic::r600_read_ngroups_y:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000653 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 4, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000654 case Intrinsic::r600_read_ngroups_z:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000655 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 8, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000656 case Intrinsic::r600_read_global_size_x:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000657 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 12, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000658 case Intrinsic::r600_read_global_size_y:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000659 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 16, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000660 case Intrinsic::r600_read_global_size_z:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000661 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 20, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000662 case Intrinsic::r600_read_local_size_x:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000663 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 24, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000664 case Intrinsic::r600_read_local_size_y:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000665 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 28, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000666 case Intrinsic::r600_read_local_size_z:
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000667 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 32, false);
Tom Stellard94593ee2013-06-03 17:40:18 +0000668 case Intrinsic::r600_read_tgid_x:
669 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
670 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 0), VT);
671 case Intrinsic::r600_read_tgid_y:
672 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
673 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 1), VT);
674 case Intrinsic::r600_read_tgid_z:
675 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
676 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 2), VT);
677 case Intrinsic::r600_read_tidig_x:
678 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
679 AMDGPU::VGPR0, VT);
680 case Intrinsic::r600_read_tidig_y:
681 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
682 AMDGPU::VGPR1, VT);
683 case Intrinsic::r600_read_tidig_z:
684 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
685 AMDGPU::VGPR2, VT);
Tom Stellard9fa17912013-08-14 23:24:45 +0000686 case AMDGPUIntrinsic::SI_load_const: {
687 SDValue Ops [] = {
Tom Stellard868fd922014-04-17 21:00:11 +0000688 Op.getOperand(1),
Tom Stellard9fa17912013-08-14 23:24:45 +0000689 Op.getOperand(2)
690 };
Tom Stellard94593ee2013-06-03 17:40:18 +0000691
Benjamin Kramera8eecee2013-08-16 14:48:09 +0000692 MachineMemOperand *MMO = MF.getMachineMemOperand(
693 MachinePointerInfo(),
694 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
695 VT.getSizeInBits() / 8, 4);
Tom Stellard9fa17912013-08-14 23:24:45 +0000696 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
Craig Topper206fcd42014-04-26 19:29:41 +0000697 Op->getVTList(), Ops, VT, MMO);
Tom Stellard9fa17912013-08-14 23:24:45 +0000698 }
699 case AMDGPUIntrinsic::SI_sample:
700 return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG);
701 case AMDGPUIntrinsic::SI_sampleb:
702 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG);
703 case AMDGPUIntrinsic::SI_sampled:
704 return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG);
705 case AMDGPUIntrinsic::SI_samplel:
706 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG);
707 case AMDGPUIntrinsic::SI_vs_load_input:
708 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
Tom Stellard868fd922014-04-17 21:00:11 +0000709 Op.getOperand(1),
Tom Stellard9fa17912013-08-14 23:24:45 +0000710 Op.getOperand(2),
711 Op.getOperand(3));
Tom Stellard94593ee2013-06-03 17:40:18 +0000712 }
713 }
Tom Stellardafcf12f2013-09-12 02:55:14 +0000714
715 case ISD::INTRINSIC_VOID:
716 SDValue Chain = Op.getOperand(0);
717 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
718
719 switch (IntrinsicID) {
720 case AMDGPUIntrinsic::SI_tbuffer_store: {
721 SDLoc DL(Op);
722 SDValue Ops [] = {
723 Chain,
Tom Stellard868fd922014-04-17 21:00:11 +0000724 Op.getOperand(2),
Tom Stellardafcf12f2013-09-12 02:55:14 +0000725 Op.getOperand(3),
726 Op.getOperand(4),
727 Op.getOperand(5),
728 Op.getOperand(6),
729 Op.getOperand(7),
730 Op.getOperand(8),
731 Op.getOperand(9),
732 Op.getOperand(10),
733 Op.getOperand(11),
734 Op.getOperand(12),
735 Op.getOperand(13),
736 Op.getOperand(14)
737 };
738 EVT VT = Op.getOperand(3).getValueType();
739
740 MachineMemOperand *MMO = MF.getMachineMemOperand(
741 MachinePointerInfo(),
742 MachineMemOperand::MOStore,
743 VT.getSizeInBits() / 8, 4);
744 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
Craig Topper206fcd42014-04-26 19:29:41 +0000745 Op->getVTList(), Ops, VT, MMO);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000746 }
747 default:
748 break;
749 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000750 }
751 return SDValue();
752}
753
Tom Stellardf8794352012-12-19 22:10:31 +0000754/// \brief Helper function for LowerBRCOND
755static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000756
Tom Stellardf8794352012-12-19 22:10:31 +0000757 SDNode *Parent = Value.getNode();
758 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
759 I != E; ++I) {
760
761 if (I.getUse().get() != Value)
762 continue;
763
764 if (I->getOpcode() == Opcode)
765 return *I;
766 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000767 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000768}
769
770/// This transforms the control flow intrinsics to get the branch destination as
771/// last parameter, also switches branch target with BR if the need arise
772SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
773 SelectionDAG &DAG) const {
774
Andrew Trickef9de2a2013-05-25 02:42:55 +0000775 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +0000776
777 SDNode *Intr = BRCOND.getOperand(1).getNode();
778 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +0000779 SDNode *BR = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000780
781 if (Intr->getOpcode() == ISD::SETCC) {
782 // As long as we negate the condition everything is fine
783 SDNode *SetCC = Intr;
784 assert(SetCC->getConstantOperandVal(1) == 1);
NAKAMURA Takumi458a8272013-01-07 11:14:44 +0000785 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
786 ISD::SETNE);
Tom Stellardf8794352012-12-19 22:10:31 +0000787 Intr = SetCC->getOperand(0).getNode();
788
789 } else {
790 // Get the target from BR if we don't negate the condition
791 BR = findUser(BRCOND, ISD::BR);
792 Target = BR->getOperand(1);
793 }
794
795 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
796
797 // Build the result and
798 SmallVector<EVT, 4> Res;
799 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
800 Res.push_back(Intr->getValueType(i));
801
802 // operands of the new intrinsic call
803 SmallVector<SDValue, 4> Ops;
804 Ops.push_back(BRCOND.getOperand(0));
805 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
806 Ops.push_back(Intr->getOperand(i));
807 Ops.push_back(Target);
808
809 // build the new intrinsic call
810 SDNode *Result = DAG.getNode(
811 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +0000812 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +0000813
814 if (BR) {
815 // Give the branch instruction our target
816 SDValue Ops[] = {
817 BR->getOperand(0),
818 BRCOND.getOperand(2)
819 };
Craig Topper131de822014-04-27 19:21:16 +0000820 DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops);
Tom Stellardf8794352012-12-19 22:10:31 +0000821 }
822
823 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
824
825 // Copy the intrinsic results to registers
826 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
827 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
828 if (!CopyToReg)
829 continue;
830
831 Chain = DAG.getCopyToReg(
832 Chain, DL,
833 CopyToReg->getOperand(1),
834 SDValue(Result, i - 1),
835 SDValue());
836
837 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
838 }
839
840 // Remove the old intrinsic from the chain
841 DAG.ReplaceAllUsesOfValueWith(
842 SDValue(Intr, Intr->getNumValues() - 1),
843 Intr->getOperand(0));
844
845 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +0000846}
847
Tom Stellard81d871d2013-11-13 23:36:50 +0000848SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
849 SDLoc DL(Op);
850 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000851 SDValue Lowered = AMDGPUTargetLowering::LowerLOAD(Op, DAG);
852 if (Lowered.getNode())
853 return Lowered;
Tom Stellard81d871d2013-11-13 23:36:50 +0000854
Tom Stellarde9373602014-01-22 19:24:14 +0000855 if (Load->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
Tom Stellard81d871d2013-11-13 23:36:50 +0000856 return SDValue();
Tom Stellarde9373602014-01-22 19:24:14 +0000857 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000858
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000859 EVT MemVT = Load->getMemoryVT();
860
861 assert(!MemVT.isVector() && "Private loads should be scalarized");
862 assert(!MemVT.isFloatingPoint() && "FP loads should be promoted to int");
863
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000864 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Load->getBasePtr(),
Tom Stellard81d871d2013-11-13 23:36:50 +0000865 DAG.getConstant(2, MVT::i32));
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000866
867 // FIXME: REGISTER_LOAD should probably have a chain result.
868 SDValue Chain = Load->getChain();
869 SDValue LoLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
870 Chain, Ptr,
871 DAG.getTargetConstant(0, MVT::i32),
872 Op.getOperand(2));
873
874 SDValue Ret = LoLoad.getValue(0);
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000875 if (MemVT.getSizeInBits() == 64) {
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000876 // TODO: This needs a test to make sure the right thing is happening with
877 // the chain. That is hard without general function support.
878
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000879 SDValue IncPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
880 DAG.getConstant(1, MVT::i32));
881
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000882 SDValue HiLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
883 Chain, IncPtr,
884 DAG.getTargetConstant(0, MVT::i32),
885 Op.getOperand(2));
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000886
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000887 Ret = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, LoLoad, HiLoad);
888 // Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
889 // LoLoad.getValue(1), HiLoad.getValue(1));
Matt Arsenaultad41d7b2014-03-24 17:50:46 +0000890 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000891
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000892 SDValue Ops[] = {
893 Ret,
894 Chain
895 };
Tom Stellard81d871d2013-11-13 23:36:50 +0000896
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000897 return DAG.getMergeValues(Ops, DL);
Tom Stellard81d871d2013-11-13 23:36:50 +0000898}
899
Tom Stellard9fa17912013-08-14 23:24:45 +0000900SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode,
901 const SDValue &Op,
902 SelectionDAG &DAG) const {
903 return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1),
904 Op.getOperand(2),
Tom Stellard868fd922014-04-17 21:00:11 +0000905 Op.getOperand(3),
Tom Stellard9fa17912013-08-14 23:24:45 +0000906 Op.getOperand(4));
907}
908
Tom Stellard0ec134f2014-02-04 17:18:40 +0000909SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
910 if (Op.getValueType() != MVT::i64)
911 return SDValue();
912
913 SDLoc DL(Op);
914 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000915
916 SDValue Zero = DAG.getConstant(0, MVT::i32);
917 SDValue One = DAG.getConstant(1, MVT::i32);
918
Tom Stellard7ea3d6d2014-03-31 14:01:55 +0000919 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
920 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
921
922 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
923 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000924
925 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
926
Tom Stellard7ea3d6d2014-03-31 14:01:55 +0000927 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
928 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000929
930 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
931
Tom Stellard7ea3d6d2014-03-31 14:01:55 +0000932 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
933 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000934}
935
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000936static SDValue performUnsafeFDIV(SDValue Op, SelectionDAG &DAG) {
937 SDLoc SL(Op);
938 SDValue LHS = Op.getOperand(0);
939 SDValue RHS = Op.getOperand(1);
940 EVT VT = Op.getValueType();
941
942 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
943 if (CLHS->isExactlyValue(1.0)) {
944
945 // 1.0 / sqrt(x) -> rsq(x)
946 if (RHS.getOpcode() == ISD::FSQRT)
947 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
948
949 // 1.0 / x -> rcp(x)
950 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
951 }
952 }
953
954 // Turn into multiply by the reciprocal
955 // x / y -> x * (1.0 / y)
956 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
957 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip);
958}
959
960SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
961 if (DAG.getTarget().Options.UnsafeFPMath)
962 return performUnsafeFDIV(Op, DAG);
963
964 SDLoc SL(Op);
965 SDValue LHS = Op.getOperand(0);
966 SDValue RHS = Op.getOperand(1);
967
968 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
969
970 const APFloat K0Val(BitsToFloat(0x6f800000));
971 const SDValue K0 = DAG.getConstantFP(K0Val, MVT::f32);
972
973 const APFloat K1Val(BitsToFloat(0x2f800000));
974 const SDValue K1 = DAG.getConstantFP(K1Val, MVT::f32);
975
976 const SDValue One = DAG.getTargetConstantFP(1.0, MVT::f32);
977
978 EVT SetCCVT = getSetCCResultType(*DAG.getContext(), MVT::f32);
979
980 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
981
982 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
983
984 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
985
986 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
987
988 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
989
990 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
991}
992
993SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
994 return SDValue();
995}
996
997SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
998 EVT VT = Op.getValueType();
999
1000 if (VT == MVT::f32)
1001 return LowerFDIV32(Op, DAG);
1002
1003 if (VT == MVT::f64)
1004 return LowerFDIV64(Op, DAG);
1005
1006 llvm_unreachable("Unexpected type for fdiv");
1007}
1008
Tom Stellard81d871d2013-11-13 23:36:50 +00001009SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1010 SDLoc DL(Op);
1011 StoreSDNode *Store = cast<StoreSDNode>(Op);
1012 EVT VT = Store->getMemoryVT();
1013
Tom Stellard9b3816b2014-06-24 23:33:04 +00001014 // These stores are legal.
1015 if (Store->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS &&
1016 VT.isVector() && VT.getVectorNumElements() == 2 &&
1017 VT.getVectorElementType() == MVT::i32)
1018 return SDValue();
1019
Tom Stellard81d871d2013-11-13 23:36:50 +00001020 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1021 if (Ret.getNode())
1022 return Ret;
1023
1024 if (VT.isVector() && VT.getVectorNumElements() >= 8)
1025 return SplitVectorStore(Op, DAG);
1026
Tom Stellard1c8788e2014-03-07 20:12:33 +00001027 if (VT == MVT::i1)
1028 return DAG.getTruncStore(Store->getChain(), DL,
1029 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
1030 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
1031
Tom Stellard81d871d2013-11-13 23:36:50 +00001032 if (Store->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS)
1033 return SDValue();
1034
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +00001035 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Store->getBasePtr(),
Tom Stellard81d871d2013-11-13 23:36:50 +00001036 DAG.getConstant(2, MVT::i32));
1037 SDValue Chain = Store->getChain();
1038 SmallVector<SDValue, 8> Values;
1039
Tom Stellarde9373602014-01-22 19:24:14 +00001040 if (Store->isTruncatingStore()) {
1041 unsigned Mask = 0;
1042 if (Store->getMemoryVT() == MVT::i8) {
1043 Mask = 0xff;
1044 } else if (Store->getMemoryVT() == MVT::i16) {
1045 Mask = 0xffff;
1046 }
1047 SDValue Dst = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
1048 Chain, Store->getBasePtr(),
1049 DAG.getConstant(0, MVT::i32));
1050 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getBasePtr(),
1051 DAG.getConstant(0x3, MVT::i32));
1052 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1053 DAG.getConstant(3, MVT::i32));
1054 SDValue MaskedValue = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getValue(),
1055 DAG.getConstant(Mask, MVT::i32));
1056 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
1057 MaskedValue, ShiftAmt);
1058 SDValue RotrAmt = DAG.getNode(ISD::SUB, DL, MVT::i32,
1059 DAG.getConstant(32, MVT::i32), ShiftAmt);
1060 SDValue DstMask = DAG.getNode(ISD::ROTR, DL, MVT::i32,
1061 DAG.getConstant(Mask, MVT::i32),
1062 RotrAmt);
1063 Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
1064 Dst = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
1065
1066 Values.push_back(Dst);
1067 } else if (VT == MVT::i64) {
Tom Stellard81d871d2013-11-13 23:36:50 +00001068 for (unsigned i = 0; i < 2; ++i) {
1069 Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
1070 Store->getValue(), DAG.getConstant(i, MVT::i32)));
1071 }
1072 } else if (VT == MVT::i128) {
1073 for (unsigned i = 0; i < 2; ++i) {
1074 for (unsigned j = 0; j < 2; ++j) {
1075 Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
1076 DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64,
1077 Store->getValue(), DAG.getConstant(i, MVT::i32)),
1078 DAG.getConstant(j, MVT::i32)));
1079 }
1080 }
1081 } else {
1082 Values.push_back(Store->getValue());
1083 }
1084
1085 for (unsigned i = 0; i < Values.size(); ++i) {
1086 SDValue PartPtr = DAG.getNode(ISD::ADD, DL, MVT::i32,
1087 Ptr, DAG.getConstant(i, MVT::i32));
1088 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1089 Chain, Values[i], PartPtr,
1090 DAG.getTargetConstant(0, MVT::i32));
1091 }
1092 return Chain;
1093}
1094
Tom Stellard75aadc22012-12-11 21:25:42 +00001095//===----------------------------------------------------------------------===//
1096// Custom DAG optimizations
1097//===----------------------------------------------------------------------===//
1098
Matt Arsenault364a6742014-06-11 17:50:44 +00001099SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
1100 DAGCombinerInfo &DCI) {
1101 EVT VT = N->getValueType(0);
1102 EVT ScalarVT = VT.getScalarType();
1103 if (ScalarVT != MVT::f32)
1104 return SDValue();
1105
1106 SelectionDAG &DAG = DCI.DAG;
1107 SDLoc DL(N);
1108
1109 SDValue Src = N->getOperand(0);
1110 EVT SrcVT = Src.getValueType();
1111
1112 // TODO: We could try to match extracting the higher bytes, which would be
1113 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
1114 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
1115 // about in practice.
1116 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
1117 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
1118 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
1119 DCI.AddToWorklist(Cvt.getNode());
1120 return Cvt;
1121 }
1122 }
1123
1124 // We are primarily trying to catch operations on illegal vector types
1125 // before they are expanded.
1126 // For scalars, we can use the more flexible method of checking masked bits
1127 // after legalization.
1128 if (!DCI.isBeforeLegalize() ||
1129 !SrcVT.isVector() ||
1130 SrcVT.getVectorElementType() != MVT::i8) {
1131 return SDValue();
1132 }
1133
1134 assert(DCI.isBeforeLegalize() && "Unexpected legal type");
1135
1136 // Weird sized vectors are a pain to handle, but we know 3 is really the same
1137 // size as 4.
1138 unsigned NElts = SrcVT.getVectorNumElements();
1139 if (!SrcVT.isSimple() && NElts != 3)
1140 return SDValue();
1141
1142 // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to
1143 // prevent a mess from expanding to v4i32 and repacking.
1144 if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) {
1145 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT);
1146 EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT);
1147 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts);
1148
1149 LoadSDNode *Load = cast<LoadSDNode>(Src);
1150 SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT,
1151 Load->getChain(),
1152 Load->getBasePtr(),
1153 LoadVT,
1154 Load->getMemOperand());
1155
1156 // Make sure successors of the original load stay after it by updating
1157 // them to use the new Chain.
1158 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1));
1159
1160 SmallVector<SDValue, 4> Elts;
1161 if (RegVT.isVector())
1162 DAG.ExtractVectorElements(NewLoad, Elts);
1163 else
1164 Elts.push_back(NewLoad);
1165
1166 SmallVector<SDValue, 4> Ops;
1167
1168 unsigned EltIdx = 0;
1169 for (SDValue Elt : Elts) {
1170 unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx);
1171 for (unsigned I = 0; I < ComponentsInElt; ++I) {
1172 unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I;
1173 SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt);
1174 DCI.AddToWorklist(Cvt.getNode());
1175 Ops.push_back(Cvt);
1176 }
1177
1178 ++EltIdx;
1179 }
1180
1181 assert(Ops.size() == NElts);
1182
1183 return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops);
1184 }
1185
1186 return SDValue();
1187}
1188
Tom Stellard75aadc22012-12-11 21:25:42 +00001189SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
1190 DAGCombinerInfo &DCI) const {
1191 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001192 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00001193 EVT VT = N->getValueType(0);
1194
1195 switch (N->getOpcode()) {
Tom Stellard50122a52014-04-07 19:45:41 +00001196 default: return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001197 case ISD::SELECT_CC: {
Tom Stellard75aadc22012-12-11 21:25:42 +00001198 ConstantSDNode *True, *False;
1199 // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
1200 if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1201 && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1202 && True->isAllOnesValue()
1203 && False->isNullValue()
1204 && VT == MVT::i1) {
1205 return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
1206 N->getOperand(1), N->getOperand(4));
1207
1208 }
1209 break;
1210 }
1211 case ISD::SETCC: {
1212 SDValue Arg0 = N->getOperand(0);
1213 SDValue Arg1 = N->getOperand(1);
1214 SDValue CC = N->getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00001215 ConstantSDNode * C = nullptr;
Tom Stellard75aadc22012-12-11 21:25:42 +00001216 ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
1217
1218 // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
1219 if (VT == MVT::i1
1220 && Arg0.getOpcode() == ISD::SIGN_EXTEND
1221 && Arg0.getOperand(0).getValueType() == MVT::i1
1222 && (C = dyn_cast<ConstantSDNode>(Arg1))
1223 && C->isNullValue()
1224 && CCOp == ISD::SETNE) {
1225 return SimplifySetCC(VT, Arg0.getOperand(0),
1226 DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
1227 }
1228 break;
1229 }
Matt Arsenault364a6742014-06-11 17:50:44 +00001230
1231 case AMDGPUISD::CVT_F32_UBYTE0:
1232 case AMDGPUISD::CVT_F32_UBYTE1:
1233 case AMDGPUISD::CVT_F32_UBYTE2:
1234 case AMDGPUISD::CVT_F32_UBYTE3: {
1235 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
1236
1237 SDValue Src = N->getOperand(0);
1238 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
1239
1240 APInt KnownZero, KnownOne;
1241 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
1242 !DCI.isBeforeLegalizeOps());
1243 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1244 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
1245 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
1246 DCI.CommitTargetLoweringOpt(TLO);
1247 }
1248
1249 break;
1250 }
1251
1252 case ISD::UINT_TO_FP: {
1253 return performUCharToFloatCombine(N, DCI);
1254 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001255 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00001256
1257 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001258}
Christian Konigd910b7d2013-02-26 17:52:16 +00001259
Matt Arsenault758659232013-05-18 00:21:46 +00001260/// \brief Test if RegClass is one of the VSrc classes
Christian Konigf82901a2013-02-26 17:52:23 +00001261static bool isVSrc(unsigned RegClass) {
1262 return AMDGPU::VSrc_32RegClassID == RegClass ||
1263 AMDGPU::VSrc_64RegClassID == RegClass;
1264}
1265
Matt Arsenault758659232013-05-18 00:21:46 +00001266/// \brief Test if RegClass is one of the SSrc classes
Christian Konigf82901a2013-02-26 17:52:23 +00001267static bool isSSrc(unsigned RegClass) {
1268 return AMDGPU::SSrc_32RegClassID == RegClass ||
1269 AMDGPU::SSrc_64RegClassID == RegClass;
1270}
1271
1272/// \brief Analyze the possible immediate value Op
1273///
1274/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
1275/// and the immediate value if it's a literal immediate
1276int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
1277
1278 union {
1279 int32_t I;
1280 float F;
1281 } Imm;
1282
Tom Stellardedbf1eb2013-04-05 23:31:20 +00001283 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
1284 if (Node->getZExtValue() >> 32) {
1285 return -1;
1286 }
Christian Konigf82901a2013-02-26 17:52:23 +00001287 Imm.I = Node->getSExtValue();
Tom Stellard7ed0b522014-04-03 20:19:27 +00001288 } else if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
1289 if (N->getValueType(0) != MVT::f32)
1290 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00001291 Imm.F = Node->getValueAPF().convertToFloat();
Tom Stellard7ed0b522014-04-03 20:19:27 +00001292 } else
Christian Konigf82901a2013-02-26 17:52:23 +00001293 return -1; // It isn't an immediate
1294
1295 if ((Imm.I >= -16 && Imm.I <= 64) ||
1296 Imm.F == 0.5f || Imm.F == -0.5f ||
1297 Imm.F == 1.0f || Imm.F == -1.0f ||
1298 Imm.F == 2.0f || Imm.F == -2.0f ||
1299 Imm.F == 4.0f || Imm.F == -4.0f)
1300 return 0; // It's an inline immediate
1301
1302 return Imm.I; // It's a literal immediate
1303}
1304
1305/// \brief Try to fold an immediate directly into an instruction
1306bool SITargetLowering::foldImm(SDValue &Operand, int32_t &Immediate,
1307 bool &ScalarSlotUsed) const {
1308
1309 MachineSDNode *Mov = dyn_cast<MachineSDNode>(Operand);
Bill Wendling37e9adb2013-06-07 20:28:55 +00001310 const SIInstrInfo *TII =
1311 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
Craig Topper062a2ba2014-04-25 05:30:21 +00001312 if (!Mov || !TII->isMov(Mov->getMachineOpcode()))
Christian Konigf82901a2013-02-26 17:52:23 +00001313 return false;
1314
1315 const SDValue &Op = Mov->getOperand(0);
1316 int32_t Value = analyzeImmediate(Op.getNode());
1317 if (Value == -1) {
1318 // Not an immediate at all
1319 return false;
1320
1321 } else if (Value == 0) {
1322 // Inline immediates can always be fold
1323 Operand = Op;
1324 return true;
1325
1326 } else if (Value == Immediate) {
1327 // Already fold literal immediate
1328 Operand = Op;
1329 return true;
1330
1331 } else if (!ScalarSlotUsed && !Immediate) {
1332 // Fold this literal immediate
1333 ScalarSlotUsed = true;
1334 Immediate = Value;
1335 Operand = Op;
1336 return true;
1337
1338 }
1339
1340 return false;
1341}
1342
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001343const TargetRegisterClass *SITargetLowering::getRegClassForNode(
1344 SelectionDAG &DAG, const SDValue &Op) const {
1345 const SIInstrInfo *TII =
1346 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1347 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1348
1349 if (!Op->isMachineOpcode()) {
1350 switch(Op->getOpcode()) {
1351 case ISD::CopyFromReg: {
1352 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1353 unsigned Reg = cast<RegisterSDNode>(Op->getOperand(1))->getReg();
1354 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1355 return MRI.getRegClass(Reg);
1356 }
1357 return TRI.getPhysRegClass(Reg);
1358 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001359 default: return nullptr;
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001360 }
1361 }
1362 const MCInstrDesc &Desc = TII->get(Op->getMachineOpcode());
1363 int OpClassID = Desc.OpInfo[Op.getResNo()].RegClass;
1364 if (OpClassID != -1) {
1365 return TRI.getRegClass(OpClassID);
1366 }
1367 switch(Op.getMachineOpcode()) {
1368 case AMDGPU::COPY_TO_REGCLASS:
1369 // Operand 1 is the register class id for COPY_TO_REGCLASS instructions.
1370 OpClassID = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1371
1372 // If the COPY_TO_REGCLASS instruction is copying to a VSrc register
1373 // class, then the register class for the value could be either a
1374 // VReg or and SReg. In order to get a more accurate
1375 if (OpClassID == AMDGPU::VSrc_32RegClassID ||
1376 OpClassID == AMDGPU::VSrc_64RegClassID) {
1377 return getRegClassForNode(DAG, Op.getOperand(0));
1378 }
1379 return TRI.getRegClass(OpClassID);
1380 case AMDGPU::EXTRACT_SUBREG: {
1381 int SubIdx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1382 const TargetRegisterClass *SuperClass =
1383 getRegClassForNode(DAG, Op.getOperand(0));
1384 return TRI.getSubClassWithSubReg(SuperClass, SubIdx);
1385 }
1386 case AMDGPU::REG_SEQUENCE:
1387 // Operand 0 is the register class id for REG_SEQUENCE instructions.
1388 return TRI.getRegClass(
1389 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue());
1390 default:
1391 return getRegClassFor(Op.getSimpleValueType());
1392 }
1393}
1394
Christian Konigf82901a2013-02-26 17:52:23 +00001395/// \brief Does "Op" fit into register class "RegClass" ?
Tom Stellardb35efba2013-05-20 15:02:01 +00001396bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, const SDValue &Op,
Christian Konigf82901a2013-02-26 17:52:23 +00001397 unsigned RegClass) const {
Bill Wendling37e9adb2013-06-07 20:28:55 +00001398 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001399 const TargetRegisterClass *RC = getRegClassForNode(DAG, Op);
1400 if (!RC) {
Christian Konigf82901a2013-02-26 17:52:23 +00001401 return false;
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001402 }
1403 return TRI->getRegClass(RegClass)->hasSubClassEq(RC);
Christian Konigf82901a2013-02-26 17:52:23 +00001404}
1405
1406/// \brief Make sure that we don't exeed the number of allowed scalars
1407void SITargetLowering::ensureSRegLimit(SelectionDAG &DAG, SDValue &Operand,
1408 unsigned RegClass,
1409 bool &ScalarSlotUsed) const {
1410
1411 // First map the operands register class to a destination class
1412 if (RegClass == AMDGPU::VSrc_32RegClassID)
1413 RegClass = AMDGPU::VReg_32RegClassID;
1414 else if (RegClass == AMDGPU::VSrc_64RegClassID)
1415 RegClass = AMDGPU::VReg_64RegClassID;
1416 else
1417 return;
1418
Alp Tokercb402912014-01-24 17:20:08 +00001419 // Nothing to do if they fit naturally
Christian Konigf82901a2013-02-26 17:52:23 +00001420 if (fitsRegClass(DAG, Operand, RegClass))
1421 return;
1422
1423 // If the scalar slot isn't used yet use it now
1424 if (!ScalarSlotUsed) {
1425 ScalarSlotUsed = true;
1426 return;
1427 }
1428
Matt Arsenault1408b602013-10-10 23:05:37 +00001429 // This is a conservative aproach. It is possible that we can't determine the
1430 // correct register class and copy too often, but better safe than sorry.
Christian Konigf82901a2013-02-26 17:52:23 +00001431 SDValue RC = DAG.getTargetConstant(RegClass, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001432 SDNode *Node = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, SDLoc(),
Christian Konigf82901a2013-02-26 17:52:23 +00001433 Operand.getValueType(), Operand, RC);
1434 Operand = SDValue(Node, 0);
1435}
1436
Tom Stellardacec99c2013-06-05 23:39:50 +00001437/// \returns true if \p Node's operands are different from the SDValue list
1438/// \p Ops
1439static bool isNodeChanged(const SDNode *Node, const std::vector<SDValue> &Ops) {
1440 for (unsigned i = 0, e = Node->getNumOperands(); i < e; ++i) {
1441 if (Ops[i].getNode() != Node->getOperand(i).getNode()) {
1442 return true;
1443 }
1444 }
1445 return false;
1446}
1447
Christian Konig8e06e2a2013-04-10 08:39:08 +00001448/// \brief Try to fold the Nodes operands into the Node
1449SDNode *SITargetLowering::foldOperands(MachineSDNode *Node,
1450 SelectionDAG &DAG) const {
Christian Konigf82901a2013-02-26 17:52:23 +00001451
1452 // Original encoding (either e32 or e64)
1453 int Opcode = Node->getMachineOpcode();
Bill Wendling37e9adb2013-06-07 20:28:55 +00001454 const SIInstrInfo *TII =
1455 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00001456 const MCInstrDesc *Desc = &TII->get(Opcode);
1457
1458 unsigned NumDefs = Desc->getNumDefs();
1459 unsigned NumOps = Desc->getNumOperands();
1460
Christian Konig3c145802013-03-27 09:12:59 +00001461 // Commuted opcode if available
1462 int OpcodeRev = Desc->isCommutable() ? TII->commuteOpcode(Opcode) : -1;
Craig Topper062a2ba2014-04-25 05:30:21 +00001463 const MCInstrDesc *DescRev = OpcodeRev == -1 ? nullptr : &TII->get(OpcodeRev);
Christian Konig3c145802013-03-27 09:12:59 +00001464
1465 assert(!DescRev || DescRev->getNumDefs() == NumDefs);
1466 assert(!DescRev || DescRev->getNumOperands() == NumOps);
1467
Christian Konige500e442013-02-26 17:52:47 +00001468 // e64 version if available, -1 otherwise
1469 int OpcodeE64 = AMDGPU::getVOPe64(Opcode);
Craig Topper062a2ba2014-04-25 05:30:21 +00001470 const MCInstrDesc *DescE64 = OpcodeE64 == -1 ? nullptr : &TII->get(OpcodeE64);
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001471 int InputModifiers[3] = {0};
Christian Konige500e442013-02-26 17:52:47 +00001472
1473 assert(!DescE64 || DescE64->getNumDefs() == NumDefs);
Christian Konige500e442013-02-26 17:52:47 +00001474
Christian Konigf82901a2013-02-26 17:52:23 +00001475 int32_t Immediate = Desc->getSize() == 4 ? 0 : -1;
1476 bool HaveVSrc = false, HaveSSrc = false;
1477
Matt Arsenault08d84942014-06-03 23:06:13 +00001478 // First figure out what we already have in this instruction.
Christian Konigf82901a2013-02-26 17:52:23 +00001479 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1480 i != e && Op < NumOps; ++i, ++Op) {
1481
1482 unsigned RegClass = Desc->OpInfo[Op].RegClass;
1483 if (isVSrc(RegClass))
1484 HaveVSrc = true;
1485 else if (isSSrc(RegClass))
1486 HaveSSrc = true;
1487 else
1488 continue;
1489
1490 int32_t Imm = analyzeImmediate(Node->getOperand(i).getNode());
1491 if (Imm != -1 && Imm != 0) {
1492 // Literal immediate
1493 Immediate = Imm;
1494 }
1495 }
1496
Matt Arsenault08d84942014-06-03 23:06:13 +00001497 // If we neither have VSrc nor SSrc, it makes no sense to continue.
Christian Konigf82901a2013-02-26 17:52:23 +00001498 if (!HaveVSrc && !HaveSSrc)
1499 return Node;
1500
1501 // No scalar allowed when we have both VSrc and SSrc
1502 bool ScalarSlotUsed = HaveVSrc && HaveSSrc;
1503
1504 // Second go over the operands and try to fold them
1505 std::vector<SDValue> Ops;
Christian Konige500e442013-02-26 17:52:47 +00001506 bool Promote2e64 = false;
Christian Konigf82901a2013-02-26 17:52:23 +00001507 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1508 i != e && Op < NumOps; ++i, ++Op) {
1509
1510 const SDValue &Operand = Node->getOperand(i);
1511 Ops.push_back(Operand);
1512
Matt Arsenault08d84942014-06-03 23:06:13 +00001513 // Already folded immediate?
Christian Konigf82901a2013-02-26 17:52:23 +00001514 if (isa<ConstantSDNode>(Operand.getNode()) ||
1515 isa<ConstantFPSDNode>(Operand.getNode()))
1516 continue;
1517
Matt Arsenault08d84942014-06-03 23:06:13 +00001518 // Is this a VSrc or SSrc operand?
Christian Konigf82901a2013-02-26 17:52:23 +00001519 unsigned RegClass = Desc->OpInfo[Op].RegClass;
Christian Konig8370dbb2013-03-26 14:04:17 +00001520 if (isVSrc(RegClass) || isSSrc(RegClass)) {
1521 // Try to fold the immediates
1522 if (!foldImm(Ops[i], Immediate, ScalarSlotUsed)) {
Matt Arsenault08d84942014-06-03 23:06:13 +00001523 // Folding didn't work, make sure we don't hit the SReg limit.
Christian Konig8370dbb2013-03-26 14:04:17 +00001524 ensureSRegLimit(DAG, Ops[i], RegClass, ScalarSlotUsed);
1525 }
1526 continue;
1527 }
Christian Konig6612ac32013-02-26 17:52:36 +00001528
Christian Konig3c145802013-03-27 09:12:59 +00001529 if (i == 1 && DescRev && fitsRegClass(DAG, Ops[0], RegClass)) {
Christian Konig6612ac32013-02-26 17:52:36 +00001530
Christian Konig8370dbb2013-03-26 14:04:17 +00001531 unsigned OtherRegClass = Desc->OpInfo[NumDefs].RegClass;
1532 assert(isVSrc(OtherRegClass) || isSSrc(OtherRegClass));
1533
1534 // Test if it makes sense to swap operands
1535 if (foldImm(Ops[1], Immediate, ScalarSlotUsed) ||
1536 (!fitsRegClass(DAG, Ops[1], RegClass) &&
1537 fitsRegClass(DAG, Ops[1], OtherRegClass))) {
Christian Konig6612ac32013-02-26 17:52:36 +00001538
1539 // Swap commutable operands
Matt Arsenault4be76e92014-04-07 16:44:26 +00001540 std::swap(Ops[0], Ops[1]);
Christian Konig3c145802013-03-27 09:12:59 +00001541
1542 Desc = DescRev;
Craig Topper062a2ba2014-04-25 05:30:21 +00001543 DescRev = nullptr;
Christian Konig8370dbb2013-03-26 14:04:17 +00001544 continue;
Christian Konig6612ac32013-02-26 17:52:36 +00001545 }
Christian Konig6612ac32013-02-26 17:52:36 +00001546 }
Christian Konigf82901a2013-02-26 17:52:23 +00001547
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001548 if (Immediate)
1549 continue;
1550
1551 if (DescE64) {
Christian Konig8370dbb2013-03-26 14:04:17 +00001552 // Test if it makes sense to switch to e64 encoding
1553 unsigned OtherRegClass = DescE64->OpInfo[Op].RegClass;
1554 if (!isVSrc(OtherRegClass) && !isSSrc(OtherRegClass))
1555 continue;
1556
1557 int32_t TmpImm = -1;
1558 if (foldImm(Ops[i], TmpImm, ScalarSlotUsed) ||
1559 (!fitsRegClass(DAG, Ops[i], RegClass) &&
1560 fitsRegClass(DAG, Ops[1], OtherRegClass))) {
1561
1562 // Switch to e64 encoding
1563 Immediate = -1;
1564 Promote2e64 = true;
1565 Desc = DescE64;
Craig Topper062a2ba2014-04-25 05:30:21 +00001566 DescE64 = nullptr;
Christian Konig8370dbb2013-03-26 14:04:17 +00001567 }
Christian Konigf82901a2013-02-26 17:52:23 +00001568 }
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001569
1570 if (!DescE64 && !Promote2e64)
1571 continue;
1572 if (!Operand.isMachineOpcode())
1573 continue;
1574 if (Operand.getMachineOpcode() == AMDGPU::FNEG_SI) {
1575 Ops.pop_back();
1576 Ops.push_back(Operand.getOperand(0));
1577 InputModifiers[i] = 1;
1578 Promote2e64 = true;
1579 if (!DescE64)
1580 continue;
1581 Desc = DescE64;
Matt Arsenaultc6f338d2014-06-05 00:01:12 +00001582 DescE64 = nullptr;
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001583 }
1584 else if (Operand.getMachineOpcode() == AMDGPU::FABS_SI) {
1585 Ops.pop_back();
1586 Ops.push_back(Operand.getOperand(0));
1587 InputModifiers[i] = 2;
1588 Promote2e64 = true;
1589 if (!DescE64)
1590 continue;
1591 Desc = DescE64;
Matt Arsenaultc6f338d2014-06-05 00:01:12 +00001592 DescE64 = nullptr;
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001593 }
Christian Konigf82901a2013-02-26 17:52:23 +00001594 }
1595
Christian Konige500e442013-02-26 17:52:47 +00001596 if (Promote2e64) {
Vincent Lejeune94af31f2014-05-10 19:18:33 +00001597 std::vector<SDValue> OldOps(Ops);
1598 Ops.clear();
1599 for (unsigned i = 0; i < OldOps.size(); ++i) {
1600 // src_modifier
Vincent Lejeune29c0c212014-05-10 19:18:39 +00001601 Ops.push_back(DAG.getTargetConstant(InputModifiers[i], MVT::i32));
Vincent Lejeune94af31f2014-05-10 19:18:33 +00001602 Ops.push_back(OldOps[i]);
1603 }
Christian Konige500e442013-02-26 17:52:47 +00001604 // Add the modifier flags while promoting
Vincent Lejeune94af31f2014-05-10 19:18:33 +00001605 for (unsigned i = 0; i < 2; ++i)
Christian Konige500e442013-02-26 17:52:47 +00001606 Ops.push_back(DAG.getTargetConstant(0, MVT::i32));
1607 }
1608
Christian Konigf82901a2013-02-26 17:52:23 +00001609 // Add optional chain and glue
1610 for (unsigned i = NumOps - NumDefs, e = Node->getNumOperands(); i < e; ++i)
1611 Ops.push_back(Node->getOperand(i));
1612
Tom Stellardb5a97002013-06-03 17:39:50 +00001613 // Nodes that have a glue result are not CSE'd by getMachineNode(), so in
1614 // this case a brand new node is always be created, even if the operands
1615 // are the same as before. So, manually check if anything has been changed.
Tom Stellardacec99c2013-06-05 23:39:50 +00001616 if (Desc->Opcode == Opcode && !isNodeChanged(Node, Ops)) {
1617 return Node;
Tom Stellardb5a97002013-06-03 17:39:50 +00001618 }
1619
Christian Konig3c145802013-03-27 09:12:59 +00001620 // Create a complete new instruction
Andrew Trickef9de2a2013-05-25 02:42:55 +00001621 return DAG.getMachineNode(Desc->Opcode, SDLoc(Node), Node->getVTList(), Ops);
Christian Konigd910b7d2013-02-26 17:52:16 +00001622}
Christian Konig8e06e2a2013-04-10 08:39:08 +00001623
1624/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00001625static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00001626 switch (Idx) {
1627 default: return 0;
1628 case AMDGPU::sub0: return 0;
1629 case AMDGPU::sub1: return 1;
1630 case AMDGPU::sub2: return 2;
1631 case AMDGPU::sub3: return 3;
1632 }
1633}
1634
1635/// \brief Adjust the writemask of MIMG instructions
1636void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
1637 SelectionDAG &DAG) const {
1638 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00001639 unsigned Lane = 0;
1640 unsigned OldDmask = Node->getConstantOperandVal(0);
1641 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001642
1643 // Try to figure out the used register components
1644 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
1645 I != E; ++I) {
1646
1647 // Abort if we can't understand the usage
1648 if (!I->isMachineOpcode() ||
1649 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
1650 return;
1651
Tom Stellard54774e52013-10-23 02:53:47 +00001652 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
1653 // Note that subregs are packed, i.e. Lane==0 is the first bit set
1654 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
1655 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00001656 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00001657
Tom Stellard54774e52013-10-23 02:53:47 +00001658 // Set which texture component corresponds to the lane.
1659 unsigned Comp;
1660 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
1661 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00001662 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00001663 Dmask &= ~(1 << Comp);
1664 }
1665
Christian Konig8e06e2a2013-04-10 08:39:08 +00001666 // Abort if we have more than one user per component
1667 if (Users[Lane])
1668 return;
1669
1670 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00001671 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001672 }
1673
Tom Stellard54774e52013-10-23 02:53:47 +00001674 // Abort if there's no change
1675 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00001676 return;
1677
1678 // Adjust the writemask in the node
1679 std::vector<SDValue> Ops;
Tom Stellard54774e52013-10-23 02:53:47 +00001680 Ops.push_back(DAG.getTargetConstant(NewDmask, MVT::i32));
Christian Konig8e06e2a2013-04-10 08:39:08 +00001681 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1682 Ops.push_back(Node->getOperand(i));
Craig Topper8c0b4d02014-04-28 05:57:50 +00001683 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00001684
Christian Konig8b1ed282013-04-10 08:39:16 +00001685 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00001686 // (if NewDmask has only one bit set...)
1687 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Christian Konig8b1ed282013-04-10 08:39:16 +00001688 SDValue RC = DAG.getTargetConstant(AMDGPU::VReg_32RegClassID, MVT::i32);
1689 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001690 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00001691 SDValue(Node, 0), RC);
1692 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
1693 return;
1694 }
1695
Christian Konig8e06e2a2013-04-10 08:39:08 +00001696 // Update the users of the node with the new indices
1697 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
1698
1699 SDNode *User = Users[i];
1700 if (!User)
1701 continue;
1702
1703 SDValue Op = DAG.getTargetConstant(Idx, MVT::i32);
1704 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
1705
1706 switch (Idx) {
1707 default: break;
1708 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
1709 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
1710 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
1711 }
1712 }
1713}
1714
Matt Arsenault08d84942014-06-03 23:06:13 +00001715/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00001716SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
1717 SelectionDAG &DAG) const {
Tom Stellard16a9a202013-08-14 23:24:17 +00001718 const SIInstrInfo *TII =
1719 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
Tom Stellard0518ff82013-06-03 17:39:58 +00001720 Node = AdjustRegClass(Node, DAG);
Christian Konig8e06e2a2013-04-10 08:39:08 +00001721
Tom Stellard16a9a202013-08-14 23:24:17 +00001722 if (TII->isMIMG(Node->getMachineOpcode()))
Christian Konig8e06e2a2013-04-10 08:39:08 +00001723 adjustWritemask(Node, DAG);
1724
1725 return foldOperands(Node, DAG);
1726}
Christian Konig8b1ed282013-04-10 08:39:16 +00001727
1728/// \brief Assign the register class depending on the number of
1729/// bits set in the writemask
1730void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
1731 SDNode *Node) const {
Tom Stellard16a9a202013-08-14 23:24:17 +00001732 const SIInstrInfo *TII =
1733 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1734 if (!TII->isMIMG(MI->getOpcode()))
Christian Konig8b1ed282013-04-10 08:39:16 +00001735 return;
1736
1737 unsigned VReg = MI->getOperand(0).getReg();
1738 unsigned Writemask = MI->getOperand(1).getImm();
1739 unsigned BitsSet = 0;
1740 for (unsigned i = 0; i < 4; ++i)
1741 BitsSet += Writemask & (1 << i) ? 1 : 0;
1742
1743 const TargetRegisterClass *RC;
1744 switch (BitsSet) {
1745 default: return;
1746 case 1: RC = &AMDGPU::VReg_32RegClass; break;
1747 case 2: RC = &AMDGPU::VReg_64RegClass; break;
1748 case 3: RC = &AMDGPU::VReg_96RegClass; break;
1749 }
1750
Tom Stellard682bfbc2013-10-10 17:11:24 +00001751 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
1752 MI->setDesc(TII->get(NewOpcode));
Christian Konig8b1ed282013-04-10 08:39:16 +00001753 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
1754 MRI.setRegClass(VReg, RC);
1755}
Tom Stellard0518ff82013-06-03 17:39:58 +00001756
1757MachineSDNode *SITargetLowering::AdjustRegClass(MachineSDNode *N,
1758 SelectionDAG &DAG) const {
1759
1760 SDLoc DL(N);
1761 unsigned NewOpcode = N->getMachineOpcode();
1762
1763 switch (N->getMachineOpcode()) {
1764 default: return N;
Tom Stellard0518ff82013-06-03 17:39:58 +00001765 case AMDGPU::S_LOAD_DWORD_IMM:
1766 NewOpcode = AMDGPU::BUFFER_LOAD_DWORD_ADDR64;
1767 // Fall-through
1768 case AMDGPU::S_LOAD_DWORDX2_SGPR:
1769 if (NewOpcode == N->getMachineOpcode()) {
1770 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64;
1771 }
1772 // Fall-through
1773 case AMDGPU::S_LOAD_DWORDX4_IMM:
1774 case AMDGPU::S_LOAD_DWORDX4_SGPR: {
1775 if (NewOpcode == N->getMachineOpcode()) {
1776 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64;
1777 }
1778 if (fitsRegClass(DAG, N->getOperand(0), AMDGPU::SReg_64RegClassID)) {
1779 return N;
1780 }
1781 ConstantSDNode *Offset = cast<ConstantSDNode>(N->getOperand(1));
1782 SDValue Ops[] = {
1783 SDValue(DAG.getMachineNode(AMDGPU::SI_ADDR64_RSRC, DL, MVT::i128,
1784 DAG.getConstant(0, MVT::i64)), 0),
1785 N->getOperand(0),
1786 DAG.getConstant(Offset->getSExtValue() << 2, MVT::i32)
1787 };
1788 return DAG.getMachineNode(NewOpcode, DL, N->getVTList(), Ops);
1789 }
1790 }
1791}
Tom Stellard94593ee2013-06-03 17:40:18 +00001792
1793SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
1794 const TargetRegisterClass *RC,
1795 unsigned Reg, EVT VT) const {
1796 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
1797
1798 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
1799 cast<RegisterSDNode>(VReg)->getReg(), VT);
1800}