blob: 4110e6a28d660e032252d018c343f6a95e47eb2d [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- R600ISelLowering.cpp - R600 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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// Custom DAG lowering for R600
Tom Stellard75aadc22012-12-11 21:25:42 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "R600ISelLowering.h"
Tom Stellard2e59a452014-06-13 01:32:00 +000016#include "AMDGPUFrameLowering.h"
17#include "AMDGPUSubtarget.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000018#include "R600Defines.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000019#include "R600FrameLowering.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000020#include "R600InstrInfo.h"
21#include "R600MachineFunctionInfo.h"
Tom Stellard44b30b42018-05-22 02:03:23 +000022#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000023#include "Utils/AMDGPUBaseInfo.h"
24#include "llvm/ADT/APFloat.h"
25#include "llvm/ADT/APInt.h"
26#include "llvm/ADT/ArrayRef.h"
27#include "llvm/ADT/DenseMap.h"
28#include "llvm/ADT/SmallVector.h"
Tom Stellardacfeebf2013-07-23 01:48:05 +000029#include "llvm/CodeGen/CallingConvLower.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000030#include "llvm/CodeGen/DAGCombine.h"
31#include "llvm/CodeGen/ISDOpcodes.h"
32#include "llvm/CodeGen/MachineBasicBlock.h"
33#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstr.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000035#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000036#include "llvm/CodeGen/MachineMemOperand.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
38#include "llvm/CodeGen/SelectionDAG.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000039#include "llvm/IR/Constants.h"
40#include "llvm/IR/DerivedTypes.h"
41#include "llvm/Support/Casting.h"
42#include "llvm/Support/Compiler.h"
43#include "llvm/Support/ErrorHandling.h"
David Blaikie13e77db2018-03-23 23:58:25 +000044#include "llvm/Support/MachineValueType.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000045#include <cassert>
46#include <cstdint>
47#include <iterator>
48#include <utility>
49#include <vector>
Tom Stellard75aadc22012-12-11 21:25:42 +000050
51using namespace llvm;
52
Evgeniy Stepanov1971ba02018-07-14 01:20:53 +000053static bool allocateKernArg(unsigned ValNo, MVT ValVT, MVT LocVT,
54 CCValAssign::LocInfo LocInfo,
55 ISD::ArgFlagsTy ArgFlags, CCState &State) {
56 MachineFunction &MF = State.getMachineFunction();
57 AMDGPUMachineFunction *MFI = MF.getInfo<AMDGPUMachineFunction>();
58
59 uint64_t Offset = MFI->allocateKernArg(LocVT.getStoreSize(),
60 ArgFlags.getOrigAlign());
61 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT, Offset, LocVT, LocInfo));
62 return true;
63}
64
Tom Stellardc5a154d2018-06-28 23:47:12 +000065#include "R600GenCallingConv.inc"
66
Matt Arsenault43e92fe2016-06-24 06:30:11 +000067R600TargetLowering::R600TargetLowering(const TargetMachine &TM,
68 const R600Subtarget &STI)
Tom Stellardc5a154d2018-06-28 23:47:12 +000069 : AMDGPUTargetLowering(TM, STI), Subtarget(&STI), Gen(STI.getGeneration()) {
70 addRegisterClass(MVT::f32, &R600::R600_Reg32RegClass);
71 addRegisterClass(MVT::i32, &R600::R600_Reg32RegClass);
72 addRegisterClass(MVT::v2f32, &R600::R600_Reg64RegClass);
73 addRegisterClass(MVT::v2i32, &R600::R600_Reg64RegClass);
74 addRegisterClass(MVT::v4f32, &R600::R600_Reg128RegClass);
75 addRegisterClass(MVT::v4i32, &R600::R600_Reg128RegClass);
Tom Stellard0344cdf2013-08-01 15:23:42 +000076
Tom Stellardc5a154d2018-06-28 23:47:12 +000077 computeRegisterProperties(Subtarget->getRegisterInfo());
Tom Stellard75aadc22012-12-11 21:25:42 +000078
Matt Arsenault71e66762016-05-21 02:27:49 +000079 // Legalize loads and stores to the private address space.
80 setOperationAction(ISD::LOAD, MVT::i32, Custom);
81 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
82 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
83
84 // EXTLOAD should be the same as ZEXTLOAD. It is legal for some address
85 // spaces, so it is custom lowered to handle those where it isn't.
86 for (MVT VT : MVT::integer_valuetypes()) {
87 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
88 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Custom);
89 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Custom);
90
91 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
92 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Custom);
93 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Custom);
94
95 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
96 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Custom);
97 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Custom);
98 }
99
Matt Arsenaultd1097a32016-06-02 19:54:26 +0000100 // Workaround for LegalizeDAG asserting on expansion of i1 vector loads.
101 setLoadExtAction(ISD::EXTLOAD, MVT::v2i32, MVT::v2i1, Expand);
102 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i32, MVT::v2i1, Expand);
103 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i32, MVT::v2i1, Expand);
104
105 setLoadExtAction(ISD::EXTLOAD, MVT::v4i32, MVT::v4i1, Expand);
106 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i32, MVT::v4i1, Expand);
107 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i32, MVT::v4i1, Expand);
108
Matt Arsenault71e66762016-05-21 02:27:49 +0000109 setOperationAction(ISD::STORE, MVT::i8, Custom);
110 setOperationAction(ISD::STORE, MVT::i32, Custom);
111 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
112 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
113
114 setTruncStoreAction(MVT::i32, MVT::i8, Custom);
115 setTruncStoreAction(MVT::i32, MVT::i16, Custom);
Jan Vesely06200bd2017-01-06 21:00:46 +0000116 // We need to include these since trunc STORES to PRIVATE need
117 // special handling to accommodate RMW
118 setTruncStoreAction(MVT::v2i32, MVT::v2i16, Custom);
119 setTruncStoreAction(MVT::v4i32, MVT::v4i16, Custom);
120 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Custom);
121 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Custom);
122 setTruncStoreAction(MVT::v32i32, MVT::v32i16, Custom);
123 setTruncStoreAction(MVT::v2i32, MVT::v2i8, Custom);
124 setTruncStoreAction(MVT::v4i32, MVT::v4i8, Custom);
125 setTruncStoreAction(MVT::v8i32, MVT::v8i8, Custom);
126 setTruncStoreAction(MVT::v16i32, MVT::v16i8, Custom);
127 setTruncStoreAction(MVT::v32i32, MVT::v32i8, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +0000128
Matt Arsenaultd1097a32016-06-02 19:54:26 +0000129 // Workaround for LegalizeDAG asserting on expansion of i1 vector stores.
130 setTruncStoreAction(MVT::v2i32, MVT::v2i1, Expand);
131 setTruncStoreAction(MVT::v4i32, MVT::v4i1, Expand);
132
Tom Stellard0351ea22013-09-28 02:50:50 +0000133 // Set condition code actions
134 setCondCodeAction(ISD::SETO, MVT::f32, Expand);
135 setCondCodeAction(ISD::SETUO, MVT::f32, Expand);
Tom Stellardcd428182013-09-28 02:50:38 +0000136 setCondCodeAction(ISD::SETLT, MVT::f32, Expand);
Tom Stellard0351ea22013-09-28 02:50:50 +0000137 setCondCodeAction(ISD::SETLE, MVT::f32, Expand);
Tom Stellardcd428182013-09-28 02:50:38 +0000138 setCondCodeAction(ISD::SETOLT, MVT::f32, Expand);
139 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
Tom Stellard0351ea22013-09-28 02:50:50 +0000140 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
141 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
142 setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
143 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
Tom Stellardcd428182013-09-28 02:50:38 +0000144 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
145 setCondCodeAction(ISD::SETULE, MVT::f32, Expand);
146
147 setCondCodeAction(ISD::SETLE, MVT::i32, Expand);
148 setCondCodeAction(ISD::SETLT, MVT::i32, Expand);
149 setCondCodeAction(ISD::SETULE, MVT::i32, Expand);
150 setCondCodeAction(ISD::SETULT, MVT::i32, Expand);
151
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000152 setOperationAction(ISD::FCOS, MVT::f32, Custom);
153 setOperationAction(ISD::FSIN, MVT::f32, Custom);
154
Tom Stellard75aadc22012-12-11 21:25:42 +0000155 setOperationAction(ISD::SETCC, MVT::v4i32, Expand);
Tom Stellard0344cdf2013-08-01 15:23:42 +0000156 setOperationAction(ISD::SETCC, MVT::v2i32, Expand);
Tom Stellard75aadc22012-12-11 21:25:42 +0000157
Tom Stellard492ebea2013-03-08 15:37:07 +0000158 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
159 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
Matt Arsenault1d555c42014-06-23 18:00:55 +0000160 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Tom Stellard75aadc22012-12-11 21:25:42 +0000161
162 setOperationAction(ISD::FSUB, MVT::f32, Expand);
163
Tom Stellardc5a154d2018-06-28 23:47:12 +0000164 setOperationAction(ISD::FCEIL, MVT::f64, Custom);
165 setOperationAction(ISD::FTRUNC, MVT::f64, Custom);
166 setOperationAction(ISD::FRINT, MVT::f64, Custom);
167 setOperationAction(ISD::FFLOOR, MVT::f64, Custom);
168
Tom Stellard75aadc22012-12-11 21:25:42 +0000169 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
170 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
171
Tom Stellarde8f9f282013-03-08 15:37:05 +0000172 setOperationAction(ISD::SETCC, MVT::i32, Expand);
173 setOperationAction(ISD::SETCC, MVT::f32, Expand);
Tom Stellard75aadc22012-12-11 21:25:42 +0000174 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Custom);
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000175 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Custom);
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000176 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
177 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
Tom Stellard75aadc22012-12-11 21:25:42 +0000178
Tom Stellard53f2f902013-09-05 18:38:03 +0000179 setOperationAction(ISD::SELECT, MVT::i32, Expand);
180 setOperationAction(ISD::SELECT, MVT::f32, Expand);
181 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
Tom Stellard53f2f902013-09-05 18:38:03 +0000182 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
Tom Stellard75aadc22012-12-11 21:25:42 +0000183
Jan Vesely808fff52015-04-30 17:15:56 +0000184 // ADD, SUB overflow.
185 // TODO: turn these into Legal?
186 if (Subtarget->hasCARRY())
187 setOperationAction(ISD::UADDO, MVT::i32, Custom);
188
189 if (Subtarget->hasBORROW())
190 setOperationAction(ISD::USUBO, MVT::i32, Custom);
191
Matt Arsenault4e466652014-04-16 01:41:30 +0000192 // Expand sign extension of vectors
193 if (!Subtarget->hasBFE())
194 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
195
196 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Expand);
197 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Expand);
198
199 if (!Subtarget->hasBFE())
200 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
201 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Expand);
202 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Expand);
203
204 if (!Subtarget->hasBFE())
205 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
206 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Expand);
207 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Expand);
208
209 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
210 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Expand);
211 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i32, Expand);
212
213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Expand);
214
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000215 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
216
Tom Stellard880a80a2014-06-17 16:53:14 +0000217 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i32, Custom);
218 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f32, Custom);
219 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
220 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
221
222 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i32, Custom);
223 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f32, Custom);
224 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
225 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
226
Jan Vesely25f36272014-06-18 12:27:13 +0000227 // We don't have 64-bit shifts. Thus we need either SHX i64 or SHX_PARTS i32
228 // to be Legal/Custom in order to avoid library calls.
229 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000230 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Jan Veselyecf51332014-06-18 12:27:17 +0000231 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jan Vesely25f36272014-06-18 12:27:13 +0000232
Jan Vesely39aeab42017-12-04 23:07:28 +0000233 if (!Subtarget->hasFMA()) {
234 setOperationAction(ISD::FMA, MVT::f32, Expand);
235 setOperationAction(ISD::FMA, MVT::f64, Expand);
236 }
Evgeniy Stepanov1971ba02018-07-14 01:20:53 +0000237
Tom Stellardc5a154d2018-06-28 23:47:12 +0000238 // FIXME: This was moved from AMDGPUTargetLowering, I'm not sure if we
239 // need it for R600.
240 if (!Subtarget->hasFP32Denormals())
241 setOperationAction(ISD::FMAD, MVT::f32, Legal);
242
243 if (!Subtarget->hasBFI()) {
244 // fcopysign can be done in a single instruction with BFI.
245 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
246 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
247 }
248
249 if (!Subtarget->hasBCNT(32))
250 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
251
252 if (!Subtarget->hasBCNT(64))
253 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
254
255 if (Subtarget->hasFFBH())
256 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Custom);
257
258 if (Subtarget->hasFFBL())
259 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Custom);
260
261 // FIXME: This was moved from AMDGPUTargetLowering, I'm not sure if we
262 // need it for R600.
263 if (Subtarget->hasBFE())
264 setHasExtractBitsInsn(true);
Jan Vesely39aeab42017-12-04 23:07:28 +0000265
Michel Danzer49812b52013-07-10 16:37:07 +0000266 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
267
Matt Arsenaultc4d3d3a2014-06-23 18:00:49 +0000268 const MVT ScalarIntVTs[] = { MVT::i32, MVT::i64 };
269 for (MVT VT : ScalarIntVTs) {
270 setOperationAction(ISD::ADDC, VT, Expand);
271 setOperationAction(ISD::SUBC, VT, Expand);
272 setOperationAction(ISD::ADDE, VT, Expand);
273 setOperationAction(ISD::SUBE, VT, Expand);
274 }
275
Jan Vesely334f51a2017-01-16 21:20:13 +0000276 // LLVM will expand these to atomic_cmp_swap(0)
277 // and atomic_swap, respectively.
278 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
279 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
280
Matt Arsenault754dd3e2017-04-03 18:08:08 +0000281 // We need to custom lower some of the intrinsics
282 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
283 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
284
Tom Stellardfc455472013-08-12 22:33:21 +0000285 setSchedulingPreference(Sched::Source);
Matt Arsenault71e66762016-05-21 02:27:49 +0000286
Matt Arsenault71e66762016-05-21 02:27:49 +0000287 setTargetDAGCombine(ISD::FP_ROUND);
288 setTargetDAGCombine(ISD::FP_TO_SINT);
289 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
290 setTargetDAGCombine(ISD::SELECT_CC);
291 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
Jan Vesely38814fa2016-08-27 19:09:43 +0000292 setTargetDAGCombine(ISD::LOAD);
Tom Stellard75aadc22012-12-11 21:25:42 +0000293}
294
Tom Stellardc0f0fba2015-10-01 17:51:29 +0000295static inline bool isEOP(MachineBasicBlock::iterator I) {
Hans Wennborg0dd9ed12016-08-13 01:12:49 +0000296 if (std::next(I) == I->getParent()->end())
297 return false;
Tom Stellardc5a154d2018-06-28 23:47:12 +0000298 return std::next(I)->getOpcode() == R600::RETURN;
Tom Stellardc0f0fba2015-10-01 17:51:29 +0000299}
300
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000301MachineBasicBlock *
302R600TargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
303 MachineBasicBlock *BB) const {
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000304 MachineFunction *MF = BB->getParent();
Tom Stellard75aadc22012-12-11 21:25:42 +0000305 MachineRegisterInfo &MRI = MF->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000306 MachineBasicBlock::iterator I = MI;
Tom Stellardc5a154d2018-06-28 23:47:12 +0000307 const R600InstrInfo *TII = Subtarget->getInstrInfo();
Tom Stellard75aadc22012-12-11 21:25:42 +0000308
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000309 switch (MI.getOpcode()) {
Tom Stellardc6f4a292013-08-26 15:05:59 +0000310 default:
Tom Stellard8f9fc202013-11-15 00:12:45 +0000311 // Replace LDS_*_RET instruction that don't have any uses with the
312 // equivalent LDS_*_NORET instruction.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000313 if (TII->isLDSRetInstr(MI.getOpcode())) {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000314 int DstIdx = TII->getOperandIdx(MI.getOpcode(), R600::OpName::dst);
Tom Stellard13c68ef2013-09-05 18:38:09 +0000315 assert(DstIdx != -1);
316 MachineInstrBuilder NewMI;
Aaron Watry1885e532014-09-11 15:02:54 +0000317 // FIXME: getLDSNoRetOp method only handles LDS_1A1D LDS ops. Add
318 // LDS_1A2D support and remove this special case.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000319 if (!MRI.use_empty(MI.getOperand(DstIdx).getReg()) ||
Tom Stellardc5a154d2018-06-28 23:47:12 +0000320 MI.getOpcode() == R600::LDS_CMPST_RET)
Tom Stellard8f9fc202013-11-15 00:12:45 +0000321 return BB;
322
323 NewMI = BuildMI(*BB, I, BB->findDebugLoc(I),
Tom Stellardc5a154d2018-06-28 23:47:12 +0000324 TII->get(R600::getLDSNoRetOp(MI.getOpcode())));
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000325 for (unsigned i = 1, e = MI.getNumOperands(); i < e; ++i) {
Diana Picus116bbab2017-01-13 09:58:52 +0000326 NewMI.add(MI.getOperand(i));
Tom Stellardc6f4a292013-08-26 15:05:59 +0000327 }
Tom Stellardc6f4a292013-08-26 15:05:59 +0000328 } else {
329 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
330 }
331 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000332
Tom Stellardc5a154d2018-06-28 23:47:12 +0000333 case R600::FABS_R600: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000334 MachineInstr *NewMI = TII->buildDefaultInstruction(
Tom Stellardc5a154d2018-06-28 23:47:12 +0000335 *BB, I, R600::MOV, MI.getOperand(0).getReg(),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000336 MI.getOperand(1).getReg());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000337 TII->addFlag(*NewMI, 0, MO_FLAG_ABS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000338 break;
339 }
340
Tom Stellardc5a154d2018-06-28 23:47:12 +0000341 case R600::FNEG_R600: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000342 MachineInstr *NewMI = TII->buildDefaultInstruction(
Tom Stellardc5a154d2018-06-28 23:47:12 +0000343 *BB, I, R600::MOV, MI.getOperand(0).getReg(),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000344 MI.getOperand(1).getReg());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000345 TII->addFlag(*NewMI, 0, MO_FLAG_NEG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000346 break;
347 }
348
Tom Stellardc5a154d2018-06-28 23:47:12 +0000349 case R600::MASK_WRITE: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000350 unsigned maskedRegister = MI.getOperand(0).getReg();
Tom Stellard75aadc22012-12-11 21:25:42 +0000351 assert(TargetRegisterInfo::isVirtualRegister(maskedRegister));
352 MachineInstr * defInstr = MRI.getVRegDef(maskedRegister);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000353 TII->addFlag(*defInstr, 0, MO_FLAG_MASK);
Tom Stellard75aadc22012-12-11 21:25:42 +0000354 break;
355 }
356
Tom Stellardc5a154d2018-06-28 23:47:12 +0000357 case R600::MOV_IMM_F32:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000358 TII->buildMovImm(*BB, I, MI.getOperand(0).getReg(), MI.getOperand(1)
359 .getFPImm()
360 ->getValueAPF()
361 .bitcastToAPInt()
362 .getZExtValue());
Tom Stellard75aadc22012-12-11 21:25:42 +0000363 break;
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000364
Tom Stellardc5a154d2018-06-28 23:47:12 +0000365 case R600::MOV_IMM_I32:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000366 TII->buildMovImm(*BB, I, MI.getOperand(0).getReg(),
367 MI.getOperand(1).getImm());
Tom Stellard75aadc22012-12-11 21:25:42 +0000368 break;
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000369
Tom Stellardc5a154d2018-06-28 23:47:12 +0000370 case R600::MOV_IMM_GLOBAL_ADDR: {
Jan Veselyf97de002016-05-13 20:39:29 +0000371 //TODO: Perhaps combine this instruction with the next if possible
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000372 auto MIB = TII->buildDefaultInstruction(
Tom Stellardc5a154d2018-06-28 23:47:12 +0000373 *BB, MI, R600::MOV, MI.getOperand(0).getReg(), R600::ALU_LITERAL_X);
374 int Idx = TII->getOperandIdx(*MIB, R600::OpName::literal);
Jan Veselyf97de002016-05-13 20:39:29 +0000375 //TODO: Ugh this is rather ugly
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000376 MIB->getOperand(Idx) = MI.getOperand(1);
Jan Veselyf97de002016-05-13 20:39:29 +0000377 break;
378 }
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000379
Tom Stellardc5a154d2018-06-28 23:47:12 +0000380 case R600::CONST_COPY: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000381 MachineInstr *NewMI = TII->buildDefaultInstruction(
Tom Stellardc5a154d2018-06-28 23:47:12 +0000382 *BB, MI, R600::MOV, MI.getOperand(0).getReg(), R600::ALU_CONST);
383 TII->setImmOperand(*NewMI, R600::OpName::src0_sel,
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000384 MI.getOperand(1).getImm());
Vincent Lejeune0b72f102013-03-05 15:04:55 +0000385 break;
386 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000387
Tom Stellardc5a154d2018-06-28 23:47:12 +0000388 case R600::RAT_WRITE_CACHELESS_32_eg:
389 case R600::RAT_WRITE_CACHELESS_64_eg:
390 case R600::RAT_WRITE_CACHELESS_128_eg:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000391 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode()))
Diana Picus116bbab2017-01-13 09:58:52 +0000392 .add(MI.getOperand(0))
393 .add(MI.getOperand(1))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000394 .addImm(isEOP(I)); // Set End of program bit
Tom Stellard75aadc22012-12-11 21:25:42 +0000395 break;
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000396
Tom Stellardc5a154d2018-06-28 23:47:12 +0000397 case R600::RAT_STORE_TYPED_eg:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000398 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode()))
Diana Picus116bbab2017-01-13 09:58:52 +0000399 .add(MI.getOperand(0))
400 .add(MI.getOperand(1))
401 .add(MI.getOperand(2))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000402 .addImm(isEOP(I)); // Set End of program bit
Tom Stellarde0e582c2015-10-01 17:51:34 +0000403 break;
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000404
Tom Stellardc5a154d2018-06-28 23:47:12 +0000405 case R600::BRANCH:
406 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(R600::JUMP))
Diana Picus116bbab2017-01-13 09:58:52 +0000407 .add(MI.getOperand(0));
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000408 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000409
Tom Stellardc5a154d2018-06-28 23:47:12 +0000410 case R600::BRANCH_COND_f32: {
Tom Stellard75aadc22012-12-11 21:25:42 +0000411 MachineInstr *NewMI =
Tom Stellardc5a154d2018-06-28 23:47:12 +0000412 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(R600::PRED_X),
413 R600::PREDICATE_BIT)
Diana Picus116bbab2017-01-13 09:58:52 +0000414 .add(MI.getOperand(1))
Tom Stellardc5a154d2018-06-28 23:47:12 +0000415 .addImm(R600::PRED_SETNE)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000416 .addImm(0); // Flags
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000417 TII->addFlag(*NewMI, 0, MO_FLAG_PUSH);
Tom Stellardc5a154d2018-06-28 23:47:12 +0000418 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(R600::JUMP_COND))
Diana Picus116bbab2017-01-13 09:58:52 +0000419 .add(MI.getOperand(0))
Tom Stellardc5a154d2018-06-28 23:47:12 +0000420 .addReg(R600::PREDICATE_BIT, RegState::Kill);
Tom Stellard75aadc22012-12-11 21:25:42 +0000421 break;
422 }
423
Tom Stellardc5a154d2018-06-28 23:47:12 +0000424 case R600::BRANCH_COND_i32: {
Tom Stellard75aadc22012-12-11 21:25:42 +0000425 MachineInstr *NewMI =
Tom Stellardc5a154d2018-06-28 23:47:12 +0000426 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(R600::PRED_X),
427 R600::PREDICATE_BIT)
Diana Picus116bbab2017-01-13 09:58:52 +0000428 .add(MI.getOperand(1))
Tom Stellardc5a154d2018-06-28 23:47:12 +0000429 .addImm(R600::PRED_SETNE_INT)
Tom Stellard75aadc22012-12-11 21:25:42 +0000430 .addImm(0); // Flags
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000431 TII->addFlag(*NewMI, 0, MO_FLAG_PUSH);
Tom Stellardc5a154d2018-06-28 23:47:12 +0000432 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(R600::JUMP_COND))
Diana Picus116bbab2017-01-13 09:58:52 +0000433 .add(MI.getOperand(0))
Tom Stellardc5a154d2018-06-28 23:47:12 +0000434 .addReg(R600::PREDICATE_BIT, RegState::Kill);
Tom Stellard75aadc22012-12-11 21:25:42 +0000435 break;
436 }
437
Tom Stellardc5a154d2018-06-28 23:47:12 +0000438 case R600::EG_ExportSwz:
439 case R600::R600_ExportSwz: {
Tom Stellard6f1b8652013-01-23 21:39:49 +0000440 // Instruction is left unmodified if its not the last one of its type
441 bool isLastInstructionOfItsType = true;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000442 unsigned InstExportType = MI.getOperand(1).getImm();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000443 for (MachineBasicBlock::iterator NextExportInst = std::next(I),
Tom Stellard6f1b8652013-01-23 21:39:49 +0000444 EndBlock = BB->end(); NextExportInst != EndBlock;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000445 NextExportInst = std::next(NextExportInst)) {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000446 if (NextExportInst->getOpcode() == R600::EG_ExportSwz ||
447 NextExportInst->getOpcode() == R600::R600_ExportSwz) {
Tom Stellard6f1b8652013-01-23 21:39:49 +0000448 unsigned CurrentInstExportType = NextExportInst->getOperand(1)
449 .getImm();
450 if (CurrentInstExportType == InstExportType) {
451 isLastInstructionOfItsType = false;
452 break;
453 }
454 }
455 }
Tom Stellardc0f0fba2015-10-01 17:51:29 +0000456 bool EOP = isEOP(I);
Tom Stellard6f1b8652013-01-23 21:39:49 +0000457 if (!EOP && !isLastInstructionOfItsType)
Tom Stellard75aadc22012-12-11 21:25:42 +0000458 return BB;
Tom Stellardc5a154d2018-06-28 23:47:12 +0000459 unsigned CfInst = (MI.getOpcode() == R600::EG_ExportSwz) ? 84 : 40;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000460 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode()))
Diana Picus116bbab2017-01-13 09:58:52 +0000461 .add(MI.getOperand(0))
462 .add(MI.getOperand(1))
463 .add(MI.getOperand(2))
464 .add(MI.getOperand(3))
465 .add(MI.getOperand(4))
466 .add(MI.getOperand(5))
467 .add(MI.getOperand(6))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000468 .addImm(CfInst)
469 .addImm(EOP);
Tom Stellard75aadc22012-12-11 21:25:42 +0000470 break;
471 }
Tom Stellardc5a154d2018-06-28 23:47:12 +0000472 case R600::RETURN: {
Jakob Stoklund Olesenfdc37672013-02-05 17:53:52 +0000473 return BB;
474 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000475 }
476
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000477 MI.eraseFromParent();
Tom Stellard75aadc22012-12-11 21:25:42 +0000478 return BB;
479}
480
481//===----------------------------------------------------------------------===//
482// Custom DAG Lowering Operations
483//===----------------------------------------------------------------------===//
484
Tom Stellard75aadc22012-12-11 21:25:42 +0000485SDValue R600TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Tom Stellardc026e8b2013-06-28 15:47:08 +0000486 MachineFunction &MF = DAG.getMachineFunction();
487 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
Tom Stellard75aadc22012-12-11 21:25:42 +0000488 switch (Op.getOpcode()) {
489 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellard880a80a2014-06-17 16:53:14 +0000490 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
491 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Jan Vesely25f36272014-06-18 12:27:13 +0000492 case ISD::SHL_PARTS: return LowerSHLParts(Op, DAG);
Jan Veselyecf51332014-06-18 12:27:17 +0000493 case ISD::SRA_PARTS:
Jan Vesely900ff2e2014-06-18 12:27:15 +0000494 case ISD::SRL_PARTS: return LowerSRXParts(Op, DAG);
Jan Vesely808fff52015-04-30 17:15:56 +0000495 case ISD::UADDO: return LowerUADDSUBO(Op, DAG, ISD::ADD, AMDGPUISD::CARRY);
496 case ISD::USUBO: return LowerUADDSUBO(Op, DAG, ISD::SUB, AMDGPUISD::BORROW);
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000497 case ISD::FCOS:
498 case ISD::FSIN: return LowerTrig(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000499 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000500 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000501 case ISD::LOAD: {
502 SDValue Result = LowerLOAD(Op, DAG);
503 assert((!Result.getNode() ||
504 Result.getNode()->getNumValues() == 2) &&
505 "Load should return a value and a chain");
506 return Result;
507 }
508
Matt Arsenault1d555c42014-06-23 18:00:55 +0000509 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellardc026e8b2013-06-28 15:47:08 +0000510 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
Matt Arsenault81d06012016-03-07 21:10:13 +0000511 case ISD::FrameIndex: return lowerFrameIndex(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000512 case ISD::INTRINSIC_VOID: {
513 SDValue Chain = Op.getOperand(0);
514 unsigned IntrinsicID =
515 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
516 switch (IntrinsicID) {
Tom Stellarde4377882018-06-01 02:19:46 +0000517 case Intrinsic::r600_store_swizzle: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000518 SDLoc DL(Op);
Vincent Lejeuned80bc152013-02-14 16:55:06 +0000519 const SDValue Args[8] = {
520 Chain,
521 Op.getOperand(2), // Export Value
522 Op.getOperand(3), // ArrayBase
523 Op.getOperand(4), // Type
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000524 DAG.getConstant(0, DL, MVT::i32), // SWZ_X
525 DAG.getConstant(1, DL, MVT::i32), // SWZ_Y
526 DAG.getConstant(2, DL, MVT::i32), // SWZ_Z
527 DAG.getConstant(3, DL, MVT::i32) // SWZ_W
Vincent Lejeuned80bc152013-02-14 16:55:06 +0000528 };
Matt Arsenault7bee6ac2016-12-05 20:23:10 +0000529 return DAG.getNode(AMDGPUISD::R600_EXPORT, DL, Op.getValueType(), Args);
Tom Stellard75aadc22012-12-11 21:25:42 +0000530 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000531
Tom Stellard75aadc22012-12-11 21:25:42 +0000532 // default for switch(IntrinsicID)
533 default: break;
534 }
535 // break out of case ISD::INTRINSIC_VOID in switch(Op.getOpcode())
536 break;
537 }
538 case ISD::INTRINSIC_WO_CHAIN: {
539 unsigned IntrinsicID =
540 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
541 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000542 SDLoc DL(Op);
Matt Arsenault754dd3e2017-04-03 18:08:08 +0000543 switch (IntrinsicID) {
Tom Stellarde4377882018-06-01 02:19:46 +0000544 case Intrinsic::r600_tex:
545 case Intrinsic::r600_texc: {
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000546 unsigned TextureOp;
547 switch (IntrinsicID) {
Tom Stellarde4377882018-06-01 02:19:46 +0000548 case Intrinsic::r600_tex:
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000549 TextureOp = 0;
550 break;
Tom Stellarde4377882018-06-01 02:19:46 +0000551 case Intrinsic::r600_texc:
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000552 TextureOp = 1;
553 break;
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000554 default:
Matt Arsenault60a750f2016-07-26 21:03:38 +0000555 llvm_unreachable("unhandled texture operation");
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000556 }
557
558 SDValue TexArgs[19] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000559 DAG.getConstant(TextureOp, DL, MVT::i32),
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000560 Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000561 DAG.getConstant(0, DL, MVT::i32),
562 DAG.getConstant(1, DL, MVT::i32),
563 DAG.getConstant(2, DL, MVT::i32),
564 DAG.getConstant(3, DL, MVT::i32),
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000565 Op.getOperand(2),
566 Op.getOperand(3),
567 Op.getOperand(4),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000568 DAG.getConstant(0, DL, MVT::i32),
569 DAG.getConstant(1, DL, MVT::i32),
570 DAG.getConstant(2, DL, MVT::i32),
571 DAG.getConstant(3, DL, MVT::i32),
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000572 Op.getOperand(5),
573 Op.getOperand(6),
574 Op.getOperand(7),
575 Op.getOperand(8),
576 Op.getOperand(9),
577 Op.getOperand(10)
578 };
Craig Topper48d114b2014-04-26 18:35:24 +0000579 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, MVT::v4f32, TexArgs);
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000580 }
Tom Stellarde4377882018-06-01 02:19:46 +0000581 case Intrinsic::r600_dot4: {
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000582 SDValue Args[8] = {
583 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000584 DAG.getConstant(0, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000585 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000586 DAG.getConstant(0, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000587 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000588 DAG.getConstant(1, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000589 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000590 DAG.getConstant(1, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000591 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000592 DAG.getConstant(2, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000593 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000594 DAG.getConstant(2, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000595 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000596 DAG.getConstant(3, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000597 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000598 DAG.getConstant(3, DL, MVT::i32))
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000599 };
Craig Topper48d114b2014-04-26 18:35:24 +0000600 return DAG.getNode(AMDGPUISD::DOT4, DL, MVT::f32, Args);
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000601 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000602
Jan Vesely2fa28c32016-07-10 21:20:29 +0000603 case Intrinsic::r600_implicitarg_ptr: {
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000604 MVT PtrVT = getPointerTy(DAG.getDataLayout(), AMDGPUASI.PARAM_I_ADDRESS);
Matt Arsenault75e71922018-06-28 10:18:55 +0000605 uint32_t ByteOffset = getImplicitParameterOffset(MF, FIRST_IMPLICIT);
Jan Vesely2fa28c32016-07-10 21:20:29 +0000606 return DAG.getConstant(ByteOffset, DL, PtrVT);
607 }
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000608 case Intrinsic::r600_read_ngroups_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000609 return LowerImplicitParameter(DAG, VT, DL, 0);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000610 case Intrinsic::r600_read_ngroups_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000611 return LowerImplicitParameter(DAG, VT, DL, 1);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000612 case Intrinsic::r600_read_ngroups_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000613 return LowerImplicitParameter(DAG, VT, DL, 2);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000614 case Intrinsic::r600_read_global_size_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000615 return LowerImplicitParameter(DAG, VT, DL, 3);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000616 case Intrinsic::r600_read_global_size_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000617 return LowerImplicitParameter(DAG, VT, DL, 4);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000618 case Intrinsic::r600_read_global_size_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000619 return LowerImplicitParameter(DAG, VT, DL, 5);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000620 case Intrinsic::r600_read_local_size_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000621 return LowerImplicitParameter(DAG, VT, DL, 6);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000622 case Intrinsic::r600_read_local_size_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000623 return LowerImplicitParameter(DAG, VT, DL, 7);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000624 case Intrinsic::r600_read_local_size_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000625 return LowerImplicitParameter(DAG, VT, DL, 8);
626
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000627 case Intrinsic::r600_read_tgid_x:
Tom Stellardc5a154d2018-06-28 23:47:12 +0000628 return CreateLiveInRegisterRaw(DAG, &R600::R600_TReg32RegClass,
629 R600::T1_X, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000630 case Intrinsic::r600_read_tgid_y:
Tom Stellardc5a154d2018-06-28 23:47:12 +0000631 return CreateLiveInRegisterRaw(DAG, &R600::R600_TReg32RegClass,
632 R600::T1_Y, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000633 case Intrinsic::r600_read_tgid_z:
Tom Stellardc5a154d2018-06-28 23:47:12 +0000634 return CreateLiveInRegisterRaw(DAG, &R600::R600_TReg32RegClass,
635 R600::T1_Z, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000636 case Intrinsic::r600_read_tidig_x:
Tom Stellardc5a154d2018-06-28 23:47:12 +0000637 return CreateLiveInRegisterRaw(DAG, &R600::R600_TReg32RegClass,
638 R600::T0_X, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000639 case Intrinsic::r600_read_tidig_y:
Tom Stellardc5a154d2018-06-28 23:47:12 +0000640 return CreateLiveInRegisterRaw(DAG, &R600::R600_TReg32RegClass,
641 R600::T0_Y, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000642 case Intrinsic::r600_read_tidig_z:
Tom Stellardc5a154d2018-06-28 23:47:12 +0000643 return CreateLiveInRegisterRaw(DAG, &R600::R600_TReg32RegClass,
644 R600::T0_Z, VT);
Matt Arsenaultbef34e22016-01-22 21:30:34 +0000645
Matt Arsenault09b2c4a2016-07-15 21:26:52 +0000646 case Intrinsic::r600_recipsqrt_ieee:
647 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
Matt Arsenaultbef34e22016-01-22 21:30:34 +0000648
Matt Arsenault09b2c4a2016-07-15 21:26:52 +0000649 case Intrinsic::r600_recipsqrt_clamped:
650 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
Matt Arsenault754dd3e2017-04-03 18:08:08 +0000651 default:
652 return Op;
Tom Stellard75aadc22012-12-11 21:25:42 +0000653 }
Matt Arsenault09b2c4a2016-07-15 21:26:52 +0000654
Tom Stellard75aadc22012-12-11 21:25:42 +0000655 // break out of case ISD::INTRINSIC_WO_CHAIN in switch(Op.getOpcode())
656 break;
657 }
658 } // end switch(Op.getOpcode())
659 return SDValue();
660}
661
662void R600TargetLowering::ReplaceNodeResults(SDNode *N,
663 SmallVectorImpl<SDValue> &Results,
664 SelectionDAG &DAG) const {
665 switch (N->getOpcode()) {
Matt Arsenaultd125d742014-03-27 17:23:24 +0000666 default:
667 AMDGPUTargetLowering::ReplaceNodeResults(N, Results, DAG);
668 return;
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000669 case ISD::FP_TO_UINT:
670 if (N->getValueType(0) == MVT::i1) {
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000671 Results.push_back(lowerFP_TO_UINT(N->getOperand(0), DAG));
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000672 return;
673 }
Justin Bognerb03fd122016-08-17 05:10:15 +0000674 // Since we don't care about out of bounds values we can use FP_TO_SINT for
675 // uints too. The DAGLegalizer code for uint considers some extra cases
676 // which are not necessary here.
677 LLVM_FALLTHROUGH;
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000678 case ISD::FP_TO_SINT: {
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000679 if (N->getValueType(0) == MVT::i1) {
680 Results.push_back(lowerFP_TO_SINT(N->getOperand(0), DAG));
681 return;
682 }
683
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000684 SDValue Result;
685 if (expandFP_TO_SINT(N, Result, DAG))
686 Results.push_back(Result);
Tom Stellard365366f2013-01-23 02:09:06 +0000687 return;
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000688 }
Jan Vesely343cd6f02014-06-22 21:43:01 +0000689 case ISD::SDIVREM: {
690 SDValue Op = SDValue(N, 1);
691 SDValue RES = LowerSDIVREM(Op, DAG);
692 Results.push_back(RES);
693 Results.push_back(RES.getValue(1));
694 break;
695 }
696 case ISD::UDIVREM: {
697 SDValue Op = SDValue(N, 0);
Tom Stellardbf69d762014-11-15 01:07:53 +0000698 LowerUDIVREM64(Op, DAG, Results);
Jan Vesely343cd6f02014-06-22 21:43:01 +0000699 break;
700 }
701 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000702}
703
Tom Stellard880a80a2014-06-17 16:53:14 +0000704SDValue R600TargetLowering::vectorToVerticalVector(SelectionDAG &DAG,
705 SDValue Vector) const {
Tom Stellard880a80a2014-06-17 16:53:14 +0000706 SDLoc DL(Vector);
707 EVT VecVT = Vector.getValueType();
708 EVT EltVT = VecVT.getVectorElementType();
709 SmallVector<SDValue, 8> Args;
710
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000711 for (unsigned i = 0, e = VecVT.getVectorNumElements(); i != e; ++i) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000712 Args.push_back(DAG.getNode(
713 ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Vector,
714 DAG.getConstant(i, DL, getVectorIdxTy(DAG.getDataLayout()))));
Tom Stellard880a80a2014-06-17 16:53:14 +0000715 }
716
717 return DAG.getNode(AMDGPUISD::BUILD_VERTICAL_VECTOR, DL, VecVT, Args);
718}
719
720SDValue R600TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
721 SelectionDAG &DAG) const {
Tom Stellard880a80a2014-06-17 16:53:14 +0000722 SDLoc DL(Op);
723 SDValue Vector = Op.getOperand(0);
724 SDValue Index = Op.getOperand(1);
725
726 if (isa<ConstantSDNode>(Index) ||
727 Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR)
728 return Op;
729
730 Vector = vectorToVerticalVector(DAG, Vector);
731 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, Op.getValueType(),
732 Vector, Index);
733}
734
735SDValue R600TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
736 SelectionDAG &DAG) const {
737 SDLoc DL(Op);
738 SDValue Vector = Op.getOperand(0);
739 SDValue Value = Op.getOperand(1);
740 SDValue Index = Op.getOperand(2);
741
742 if (isa<ConstantSDNode>(Index) ||
743 Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR)
744 return Op;
745
746 Vector = vectorToVerticalVector(DAG, Vector);
747 SDValue Insert = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, Op.getValueType(),
748 Vector, Value, Index);
749 return vectorToVerticalVector(DAG, Insert);
750}
751
Tom Stellard27233b72016-05-02 18:05:17 +0000752SDValue R600TargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
753 SDValue Op,
754 SelectionDAG &DAG) const {
Tom Stellard27233b72016-05-02 18:05:17 +0000755 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000756 if (GSD->getAddressSpace() != AMDGPUASI.CONSTANT_ADDRESS)
Tom Stellard27233b72016-05-02 18:05:17 +0000757 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
758
759 const DataLayout &DL = DAG.getDataLayout();
760 const GlobalValue *GV = GSD->getGlobal();
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000761 MVT ConstPtrVT = getPointerTy(DL, AMDGPUASI.CONSTANT_ADDRESS);
Tom Stellard27233b72016-05-02 18:05:17 +0000762
Jan Veselyf97de002016-05-13 20:39:29 +0000763 SDValue GA = DAG.getTargetGlobalAddress(GV, SDLoc(GSD), ConstPtrVT);
764 return DAG.getNode(AMDGPUISD::CONST_DATA_PTR, SDLoc(GSD), ConstPtrVT, GA);
Tom Stellard27233b72016-05-02 18:05:17 +0000765}
766
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000767SDValue R600TargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
768 // On hw >= R700, COS/SIN input must be between -1. and 1.
769 // Thus we lower them to TRIG ( FRACT ( x / 2Pi + 0.5) - 0.5)
770 EVT VT = Op.getValueType();
771 SDValue Arg = Op.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000772 SDLoc DL(Op);
Sanjay Patela2607012015-09-16 16:31:21 +0000773
774 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000775 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
776 DAG.getNode(ISD::FADD, DL, VT,
777 DAG.getNode(ISD::FMUL, DL, VT, Arg,
778 DAG.getConstantFP(0.15915494309, DL, MVT::f32)),
779 DAG.getConstantFP(0.5, DL, MVT::f32)));
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000780 unsigned TrigNode;
781 switch (Op.getOpcode()) {
782 case ISD::FCOS:
783 TrigNode = AMDGPUISD::COS_HW;
784 break;
785 case ISD::FSIN:
786 TrigNode = AMDGPUISD::SIN_HW;
787 break;
788 default:
789 llvm_unreachable("Wrong trig opcode");
790 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000791 SDValue TrigVal = DAG.getNode(TrigNode, DL, VT,
792 DAG.getNode(ISD::FADD, DL, VT, FractPart,
793 DAG.getConstantFP(-0.5, DL, MVT::f32)));
Tom Stellard5bfbae52018-07-11 20:59:01 +0000794 if (Gen >= AMDGPUSubtarget::R700)
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000795 return TrigVal;
796 // On R600 hw, COS/SIN input must be between -Pi and Pi.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000797 return DAG.getNode(ISD::FMUL, DL, VT, TrigVal,
798 DAG.getConstantFP(3.14159265359, DL, MVT::f32));
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000799}
800
Jan Vesely25f36272014-06-18 12:27:13 +0000801SDValue R600TargetLowering::LowerSHLParts(SDValue Op, SelectionDAG &DAG) const {
802 SDLoc DL(Op);
803 EVT VT = Op.getValueType();
804
805 SDValue Lo = Op.getOperand(0);
806 SDValue Hi = Op.getOperand(1);
807 SDValue Shift = Op.getOperand(2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000808 SDValue Zero = DAG.getConstant(0, DL, VT);
809 SDValue One = DAG.getConstant(1, DL, VT);
Jan Vesely25f36272014-06-18 12:27:13 +0000810
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000811 SDValue Width = DAG.getConstant(VT.getSizeInBits(), DL, VT);
812 SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, DL, VT);
Jan Vesely25f36272014-06-18 12:27:13 +0000813 SDValue BigShift = DAG.getNode(ISD::SUB, DL, VT, Shift, Width);
814 SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift);
815
816 // The dance around Width1 is necessary for 0 special case.
817 // Without it the CompShift might be 32, producing incorrect results in
818 // Overflow. So we do the shift in two steps, the alternative is to
819 // add a conditional to filter the special case.
820
821 SDValue Overflow = DAG.getNode(ISD::SRL, DL, VT, Lo, CompShift);
822 Overflow = DAG.getNode(ISD::SRL, DL, VT, Overflow, One);
823
824 SDValue HiSmall = DAG.getNode(ISD::SHL, DL, VT, Hi, Shift);
825 HiSmall = DAG.getNode(ISD::OR, DL, VT, HiSmall, Overflow);
826 SDValue LoSmall = DAG.getNode(ISD::SHL, DL, VT, Lo, Shift);
827
828 SDValue HiBig = DAG.getNode(ISD::SHL, DL, VT, Lo, BigShift);
829 SDValue LoBig = Zero;
830
831 Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT);
832 Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT);
833
834 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi);
835}
836
Jan Vesely900ff2e2014-06-18 12:27:15 +0000837SDValue R600TargetLowering::LowerSRXParts(SDValue Op, SelectionDAG &DAG) const {
838 SDLoc DL(Op);
839 EVT VT = Op.getValueType();
840
841 SDValue Lo = Op.getOperand(0);
842 SDValue Hi = Op.getOperand(1);
843 SDValue Shift = Op.getOperand(2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000844 SDValue Zero = DAG.getConstant(0, DL, VT);
845 SDValue One = DAG.getConstant(1, DL, VT);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000846
Jan Veselyecf51332014-06-18 12:27:17 +0000847 const bool SRA = Op.getOpcode() == ISD::SRA_PARTS;
848
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000849 SDValue Width = DAG.getConstant(VT.getSizeInBits(), DL, VT);
850 SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, DL, VT);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000851 SDValue BigShift = DAG.getNode(ISD::SUB, DL, VT, Shift, Width);
852 SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift);
853
854 // The dance around Width1 is necessary for 0 special case.
855 // Without it the CompShift might be 32, producing incorrect results in
856 // Overflow. So we do the shift in two steps, the alternative is to
857 // add a conditional to filter the special case.
858
859 SDValue Overflow = DAG.getNode(ISD::SHL, DL, VT, Hi, CompShift);
860 Overflow = DAG.getNode(ISD::SHL, DL, VT, Overflow, One);
861
Jan Veselyecf51332014-06-18 12:27:17 +0000862 SDValue HiSmall = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, Shift);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000863 SDValue LoSmall = DAG.getNode(ISD::SRL, DL, VT, Lo, Shift);
864 LoSmall = DAG.getNode(ISD::OR, DL, VT, LoSmall, Overflow);
865
Jan Veselyecf51332014-06-18 12:27:17 +0000866 SDValue LoBig = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, BigShift);
867 SDValue HiBig = SRA ? DAG.getNode(ISD::SRA, DL, VT, Hi, Width1) : Zero;
Jan Vesely900ff2e2014-06-18 12:27:15 +0000868
869 Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT);
870 Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT);
871
872 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi);
873}
874
Jan Vesely808fff52015-04-30 17:15:56 +0000875SDValue R600TargetLowering::LowerUADDSUBO(SDValue Op, SelectionDAG &DAG,
876 unsigned mainop, unsigned ovf) const {
877 SDLoc DL(Op);
878 EVT VT = Op.getValueType();
879
880 SDValue Lo = Op.getOperand(0);
881 SDValue Hi = Op.getOperand(1);
882
883 SDValue OVF = DAG.getNode(ovf, DL, VT, Lo, Hi);
884 // Extend sign.
885 OVF = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, OVF,
886 DAG.getValueType(MVT::i1));
887
888 SDValue Res = DAG.getNode(mainop, DL, VT, Lo, Hi);
889
890 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT, VT), Res, OVF);
891}
892
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000893SDValue R600TargetLowering::lowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000894 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000895 return DAG.getNode(
896 ISD::SETCC,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000897 DL,
Tom Stellard75aadc22012-12-11 21:25:42 +0000898 MVT::i1,
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000899 Op, DAG.getConstantFP(1.0f, DL, MVT::f32),
900 DAG.getCondCode(ISD::SETEQ));
901}
902
903SDValue R600TargetLowering::lowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const {
904 SDLoc DL(Op);
905 return DAG.getNode(
906 ISD::SETCC,
907 DL,
908 MVT::i1,
909 Op, DAG.getConstantFP(-1.0f, DL, MVT::f32),
910 DAG.getCondCode(ISD::SETEQ));
Tom Stellard75aadc22012-12-11 21:25:42 +0000911}
912
Tom Stellard75aadc22012-12-11 21:25:42 +0000913SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000914 const SDLoc &DL,
Tom Stellard75aadc22012-12-11 21:25:42 +0000915 unsigned DwordOffset) const {
916 unsigned ByteOffset = DwordOffset * 4;
917 PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000918 AMDGPUASI.CONSTANT_BUFFER_0);
Tom Stellard75aadc22012-12-11 21:25:42 +0000919
920 // We shouldn't be using an offset wider than 16-bits for implicit parameters.
921 assert(isInt<16>(ByteOffset));
922
923 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000924 DAG.getConstant(ByteOffset, DL, MVT::i32), // PTR
Justin Lebar9c375812016-07-15 18:27:10 +0000925 MachinePointerInfo(ConstantPointerNull::get(PtrType)));
Tom Stellard75aadc22012-12-11 21:25:42 +0000926}
927
Tom Stellard75aadc22012-12-11 21:25:42 +0000928bool R600TargetLowering::isZero(SDValue Op) const {
929 if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) {
930 return Cst->isNullValue();
931 } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){
932 return CstFP->isZero();
933 } else {
934 return false;
935 }
936}
937
Matt Arsenault6b6a2c32016-03-11 08:00:27 +0000938bool R600TargetLowering::isHWTrueValue(SDValue Op) const {
939 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
940 return CFP->isExactlyValue(1.0);
941 }
942 return isAllOnesConstant(Op);
943}
944
945bool R600TargetLowering::isHWFalseValue(SDValue Op) const {
946 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
947 return CFP->getValueAPF().isZero();
948 }
949 return isNullConstant(Op);
950}
951
Tom Stellard75aadc22012-12-11 21:25:42 +0000952SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000953 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000954 EVT VT = Op.getValueType();
955
956 SDValue LHS = Op.getOperand(0);
957 SDValue RHS = Op.getOperand(1);
958 SDValue True = Op.getOperand(2);
959 SDValue False = Op.getOperand(3);
960 SDValue CC = Op.getOperand(4);
961 SDValue Temp;
962
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +0000963 if (VT == MVT::f32) {
964 DAGCombinerInfo DCI(DAG, AfterLegalizeVectorOps, true, nullptr);
Matt Arsenaultda7a6562017-02-01 00:42:40 +0000965 SDValue MinMax = combineFMinMaxLegacy(DL, VT, LHS, RHS, True, False, CC, DCI);
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +0000966 if (MinMax)
967 return MinMax;
968 }
969
Tom Stellard75aadc22012-12-11 21:25:42 +0000970 // LHS and RHS are guaranteed to be the same value type
971 EVT CompareVT = LHS.getValueType();
972
973 // Check if we can lower this to a native operation.
974
Tom Stellard2add82d2013-03-08 15:37:09 +0000975 // Try to lower to a SET* instruction:
976 //
977 // SET* can match the following patterns:
978 //
Tom Stellardcd428182013-09-28 02:50:38 +0000979 // select_cc f32, f32, -1, 0, cc_supported
980 // select_cc f32, f32, 1.0f, 0.0f, cc_supported
981 // select_cc i32, i32, -1, 0, cc_supported
Tom Stellard2add82d2013-03-08 15:37:09 +0000982 //
983
984 // Move hardware True/False values to the correct operand.
Tom Stellardcd428182013-09-28 02:50:38 +0000985 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
986 ISD::CondCode InverseCC =
987 ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
Tom Stellard5694d302013-09-28 02:50:43 +0000988 if (isHWTrueValue(False) && isHWFalseValue(True)) {
989 if (isCondCodeLegal(InverseCC, CompareVT.getSimpleVT())) {
990 std::swap(False, True);
991 CC = DAG.getCondCode(InverseCC);
992 } else {
993 ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InverseCC);
994 if (isCondCodeLegal(SwapInvCC, CompareVT.getSimpleVT())) {
995 std::swap(False, True);
996 std::swap(LHS, RHS);
997 CC = DAG.getCondCode(SwapInvCC);
998 }
999 }
Tom Stellard2add82d2013-03-08 15:37:09 +00001000 }
1001
1002 if (isHWTrueValue(True) && isHWFalseValue(False) &&
1003 (CompareVT == VT || VT == MVT::i32)) {
1004 // This can be matched by a SET* instruction.
1005 return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
1006 }
1007
Tom Stellard75aadc22012-12-11 21:25:42 +00001008 // Try to lower to a CND* instruction:
Tom Stellard2add82d2013-03-08 15:37:09 +00001009 //
1010 // CND* can match the following patterns:
1011 //
Tom Stellardcd428182013-09-28 02:50:38 +00001012 // select_cc f32, 0.0, f32, f32, cc_supported
1013 // select_cc f32, 0.0, i32, i32, cc_supported
1014 // select_cc i32, 0, f32, f32, cc_supported
1015 // select_cc i32, 0, i32, i32, cc_supported
Tom Stellard2add82d2013-03-08 15:37:09 +00001016 //
Tom Stellardcd428182013-09-28 02:50:38 +00001017
1018 // Try to move the zero value to the RHS
1019 if (isZero(LHS)) {
1020 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
1021 // Try swapping the operands
1022 ISD::CondCode CCSwapped = ISD::getSetCCSwappedOperands(CCOpcode);
1023 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
1024 std::swap(LHS, RHS);
1025 CC = DAG.getCondCode(CCSwapped);
1026 } else {
1027 // Try inverting the conditon and then swapping the operands
1028 ISD::CondCode CCInv = ISD::getSetCCInverse(CCOpcode, CompareVT.isInteger());
1029 CCSwapped = ISD::getSetCCSwappedOperands(CCInv);
1030 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
1031 std::swap(True, False);
1032 std::swap(LHS, RHS);
1033 CC = DAG.getCondCode(CCSwapped);
1034 }
1035 }
1036 }
1037 if (isZero(RHS)) {
1038 SDValue Cond = LHS;
1039 SDValue Zero = RHS;
Tom Stellard75aadc22012-12-11 21:25:42 +00001040 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
1041 if (CompareVT != VT) {
1042 // Bitcast True / False to the correct types. This will end up being
1043 // a nop, but it allows us to define only a single pattern in the
1044 // .TD files for each CND* instruction rather than having to have
1045 // one pattern for integer True/False and one for fp True/False
1046 True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
1047 False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
1048 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001049
1050 switch (CCOpcode) {
1051 case ISD::SETONE:
1052 case ISD::SETUNE:
1053 case ISD::SETNE:
Tom Stellard75aadc22012-12-11 21:25:42 +00001054 CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
1055 Temp = True;
1056 True = False;
1057 False = Temp;
1058 break;
1059 default:
1060 break;
1061 }
1062 SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
1063 Cond, Zero,
1064 True, False,
1065 DAG.getCondCode(CCOpcode));
1066 return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
1067 }
1068
Tom Stellard75aadc22012-12-11 21:25:42 +00001069 // If we make it this for it means we have no native instructions to handle
1070 // this SELECT_CC, so we must lower it.
1071 SDValue HWTrue, HWFalse;
1072
1073 if (CompareVT == MVT::f32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001074 HWTrue = DAG.getConstantFP(1.0f, DL, CompareVT);
1075 HWFalse = DAG.getConstantFP(0.0f, DL, CompareVT);
Tom Stellard75aadc22012-12-11 21:25:42 +00001076 } else if (CompareVT == MVT::i32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001077 HWTrue = DAG.getConstant(-1, DL, CompareVT);
1078 HWFalse = DAG.getConstant(0, DL, CompareVT);
Tom Stellard75aadc22012-12-11 21:25:42 +00001079 }
1080 else {
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +00001081 llvm_unreachable("Unhandled value type in LowerSELECT_CC");
Tom Stellard75aadc22012-12-11 21:25:42 +00001082 }
1083
1084 // Lower this unsupported SELECT_CC into a combination of two supported
1085 // SELECT_CC operations.
1086 SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
1087
1088 return DAG.getNode(ISD::SELECT_CC, DL, VT,
1089 Cond, HWFalse,
1090 True, False,
1091 DAG.getCondCode(ISD::SETNE));
1092}
1093
Alp Tokercb402912014-01-24 17:20:08 +00001094/// LLVM generates byte-addressed pointers. For indirect addressing, we need to
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001095/// convert these pointers to a register index. Each register holds
1096/// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
1097/// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
1098/// for indirect addressing.
1099SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
1100 unsigned StackWidth,
1101 SelectionDAG &DAG) const {
1102 unsigned SRLPad;
1103 switch(StackWidth) {
1104 case 1:
1105 SRLPad = 2;
1106 break;
1107 case 2:
1108 SRLPad = 3;
1109 break;
1110 case 4:
1111 SRLPad = 4;
1112 break;
1113 default: llvm_unreachable("Invalid stack width");
1114 }
1115
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001116 SDLoc DL(Ptr);
1117 return DAG.getNode(ISD::SRL, DL, Ptr.getValueType(), Ptr,
1118 DAG.getConstant(SRLPad, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001119}
1120
1121void R600TargetLowering::getStackAddress(unsigned StackWidth,
1122 unsigned ElemIdx,
1123 unsigned &Channel,
1124 unsigned &PtrIncr) const {
1125 switch (StackWidth) {
1126 default:
1127 case 1:
1128 Channel = 0;
1129 if (ElemIdx > 0) {
1130 PtrIncr = 1;
1131 } else {
1132 PtrIncr = 0;
1133 }
1134 break;
1135 case 2:
1136 Channel = ElemIdx % 2;
1137 if (ElemIdx == 2) {
1138 PtrIncr = 1;
1139 } else {
1140 PtrIncr = 0;
1141 }
1142 break;
1143 case 4:
1144 Channel = ElemIdx;
1145 PtrIncr = 0;
1146 break;
1147 }
1148}
1149
Matt Arsenault95245662016-02-11 05:32:46 +00001150SDValue R600TargetLowering::lowerPrivateTruncStore(StoreSDNode *Store,
1151 SelectionDAG &DAG) const {
1152 SDLoc DL(Store);
Jan Vesely06200bd2017-01-06 21:00:46 +00001153 //TODO: Who creates the i8 stores?
1154 assert(Store->isTruncatingStore()
1155 || Store->getValue().getValueType() == MVT::i8);
Yaxun Liu1a14bfa2017-03-27 14:04:01 +00001156 assert(Store->getAddressSpace() == AMDGPUASI.PRIVATE_ADDRESS);
Tom Stellard75aadc22012-12-11 21:25:42 +00001157
Jan Vesely06200bd2017-01-06 21:00:46 +00001158 SDValue Mask;
Matt Arsenault95245662016-02-11 05:32:46 +00001159 if (Store->getMemoryVT() == MVT::i8) {
Jan Vesely06200bd2017-01-06 21:00:46 +00001160 assert(Store->getAlignment() >= 1);
1161 Mask = DAG.getConstant(0xff, DL, MVT::i32);
Matt Arsenault95245662016-02-11 05:32:46 +00001162 } else if (Store->getMemoryVT() == MVT::i16) {
Jan Vesely06200bd2017-01-06 21:00:46 +00001163 assert(Store->getAlignment() >= 2);
Mandeep Singh Grang5e1697e2017-06-06 05:08:36 +00001164 Mask = DAG.getConstant(0xffff, DL, MVT::i32);
Jan Vesely06200bd2017-01-06 21:00:46 +00001165 } else {
1166 llvm_unreachable("Unsupported private trunc store");
Matt Arsenault95245662016-02-11 05:32:46 +00001167 }
1168
Jan Veselyf1705042017-01-20 21:24:26 +00001169 SDValue OldChain = Store->getChain();
1170 bool VectorTrunc = (OldChain.getOpcode() == AMDGPUISD::DUMMY_CHAIN);
1171 // Skip dummy
1172 SDValue Chain = VectorTrunc ? OldChain->getOperand(0) : OldChain;
Matt Arsenault95245662016-02-11 05:32:46 +00001173 SDValue BasePtr = Store->getBasePtr();
Jan Vesely06200bd2017-01-06 21:00:46 +00001174 SDValue Offset = Store->getOffset();
Matt Arsenault95245662016-02-11 05:32:46 +00001175 EVT MemVT = Store->getMemoryVT();
1176
Jan Vesely06200bd2017-01-06 21:00:46 +00001177 SDValue LoadPtr = BasePtr;
1178 if (!Offset.isUndef()) {
1179 LoadPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, Offset);
1180 }
Matt Arsenault95245662016-02-11 05:32:46 +00001181
Jan Vesely06200bd2017-01-06 21:00:46 +00001182 // Get dword location
1183 // TODO: this should be eliminated by the future SHR ptr, 2
1184 SDValue Ptr = DAG.getNode(ISD::AND, DL, MVT::i32, LoadPtr,
1185 DAG.getConstant(0xfffffffc, DL, MVT::i32));
1186
1187 // Load dword
1188 // TODO: can we be smarter about machine pointer info?
Yaxun Liu35845f02017-11-10 02:03:28 +00001189 MachinePointerInfo PtrInfo(UndefValue::get(
1190 Type::getInt32PtrTy(*DAG.getContext(), AMDGPUASI.PRIVATE_ADDRESS)));
1191 SDValue Dst = DAG.getLoad(MVT::i32, DL, Chain, Ptr, PtrInfo);
Jan Vesely06200bd2017-01-06 21:00:46 +00001192
1193 Chain = Dst.getValue(1);
1194
1195 // Get offset in dword
1196 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, LoadPtr,
Matt Arsenault95245662016-02-11 05:32:46 +00001197 DAG.getConstant(0x3, DL, MVT::i32));
1198
Jan Vesely06200bd2017-01-06 21:00:46 +00001199 // Convert byte offset to bit shift
Matt Arsenault95245662016-02-11 05:32:46 +00001200 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1201 DAG.getConstant(3, DL, MVT::i32));
1202
Jan Vesely06200bd2017-01-06 21:00:46 +00001203 // TODO: Contrary to the name of the functiom,
1204 // it also handles sub i32 non-truncating stores (like i1)
Matt Arsenault95245662016-02-11 05:32:46 +00001205 SDValue SExtValue = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i32,
1206 Store->getValue());
1207
Jan Vesely06200bd2017-01-06 21:00:46 +00001208 // Mask the value to the right type
Matt Arsenault95245662016-02-11 05:32:46 +00001209 SDValue MaskedValue = DAG.getZeroExtendInReg(SExtValue, DL, MemVT);
1210
Jan Vesely06200bd2017-01-06 21:00:46 +00001211 // Shift the value in place
Matt Arsenault95245662016-02-11 05:32:46 +00001212 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
1213 MaskedValue, ShiftAmt);
1214
Jan Vesely06200bd2017-01-06 21:00:46 +00001215 // Shift the mask in place
1216 SDValue DstMask = DAG.getNode(ISD::SHL, DL, MVT::i32, Mask, ShiftAmt);
1217
1218 // Invert the mask. NOTE: if we had native ROL instructions we could
1219 // use inverted mask
1220 DstMask = DAG.getNOT(DL, DstMask, MVT::i32);
1221
1222 // Cleanup the target bits
Matt Arsenault95245662016-02-11 05:32:46 +00001223 Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
1224
Jan Vesely06200bd2017-01-06 21:00:46 +00001225 // Add the new bits
Matt Arsenault95245662016-02-11 05:32:46 +00001226 SDValue Value = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
Jan Vesely06200bd2017-01-06 21:00:46 +00001227
1228 // Store dword
1229 // TODO: Can we be smarter about MachinePointerInfo?
Yaxun Liu35845f02017-11-10 02:03:28 +00001230 SDValue NewStore = DAG.getStore(Chain, DL, Value, Ptr, PtrInfo);
Jan Veselyf1705042017-01-20 21:24:26 +00001231
1232 // If we are part of expanded vector, make our neighbors depend on this store
1233 if (VectorTrunc) {
1234 // Make all other vector elements depend on this store
1235 Chain = DAG.getNode(AMDGPUISD::DUMMY_CHAIN, DL, MVT::Other, NewStore);
1236 DAG.ReplaceAllUsesOfValueWith(OldChain, Chain);
1237 }
1238 return NewStore;
Matt Arsenault95245662016-02-11 05:32:46 +00001239}
1240
1241SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault95245662016-02-11 05:32:46 +00001242 StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
1243 unsigned AS = StoreNode->getAddressSpace();
Matt Arsenault95245662016-02-11 05:32:46 +00001244
Jan Vesely06200bd2017-01-06 21:00:46 +00001245 SDValue Chain = StoreNode->getChain();
1246 SDValue Ptr = StoreNode->getBasePtr();
1247 SDValue Value = StoreNode->getValue();
1248
1249 EVT VT = Value.getValueType();
1250 EVT MemVT = StoreNode->getMemoryVT();
1251 EVT PtrVT = Ptr.getValueType();
1252
1253 SDLoc DL(Op);
1254
1255 // Neither LOCAL nor PRIVATE can do vectors at the moment
Yaxun Liu1a14bfa2017-03-27 14:04:01 +00001256 if ((AS == AMDGPUASI.LOCAL_ADDRESS || AS == AMDGPUASI.PRIVATE_ADDRESS) &&
Jan Vesely06200bd2017-01-06 21:00:46 +00001257 VT.isVector()) {
Yaxun Liu1a14bfa2017-03-27 14:04:01 +00001258 if ((AS == AMDGPUASI.PRIVATE_ADDRESS) &&
1259 StoreNode->isTruncatingStore()) {
Jan Veselyf1705042017-01-20 21:24:26 +00001260 // Add an extra level of chain to isolate this vector
1261 SDValue NewChain = DAG.getNode(AMDGPUISD::DUMMY_CHAIN, DL, MVT::Other, Chain);
1262 // TODO: can the chain be replaced without creating a new store?
1263 SDValue NewStore = DAG.getTruncStore(
1264 NewChain, DL, Value, Ptr, StoreNode->getPointerInfo(),
1265 MemVT, StoreNode->getAlignment(),
1266 StoreNode->getMemOperand()->getFlags(), StoreNode->getAAInfo());
1267 StoreNode = cast<StoreSDNode>(NewStore);
1268 }
1269
Jan Vesely06200bd2017-01-06 21:00:46 +00001270 return scalarizeVectorStore(StoreNode, DAG);
Matt Arsenault95245662016-02-11 05:32:46 +00001271 }
1272
Jan Vesely06200bd2017-01-06 21:00:46 +00001273 unsigned Align = StoreNode->getAlignment();
1274 if (Align < MemVT.getStoreSize() &&
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001275 !allowsMisalignedMemoryAccesses(MemVT, AS, Align, nullptr)) {
Jan Vesely00864882016-09-02 19:07:06 +00001276 return expandUnalignedStore(StoreNode, DAG);
1277 }
1278
Jan Vesely06200bd2017-01-06 21:00:46 +00001279 SDValue DWordAddr = DAG.getNode(ISD::SRL, DL, PtrVT, Ptr,
1280 DAG.getConstant(2, DL, PtrVT));
Matt Arsenault95245662016-02-11 05:32:46 +00001281
Yaxun Liu1a14bfa2017-03-27 14:04:01 +00001282 if (AS == AMDGPUASI.GLOBAL_ADDRESS) {
Jan Vesely00864882016-09-02 19:07:06 +00001283 // It is beneficial to create MSKOR here instead of combiner to avoid
1284 // artificial dependencies introduced by RMW
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001285 if (StoreNode->isTruncatingStore()) {
Tom Stellardfbab8272013-08-16 01:12:11 +00001286 assert(VT.bitsLE(MVT::i32));
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001287 SDValue MaskConstant;
1288 if (MemVT == MVT::i8) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001289 MaskConstant = DAG.getConstant(0xFF, DL, MVT::i32);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001290 } else {
1291 assert(MemVT == MVT::i16);
Jan Vesely00864882016-09-02 19:07:06 +00001292 assert(StoreNode->getAlignment() >= 2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001293 MaskConstant = DAG.getConstant(0xFFFF, DL, MVT::i32);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001294 }
Jan Vesely06200bd2017-01-06 21:00:46 +00001295
1296 SDValue ByteIndex = DAG.getNode(ISD::AND, DL, PtrVT, Ptr,
1297 DAG.getConstant(0x00000003, DL, PtrVT));
1298 SDValue BitShift = DAG.getNode(ISD::SHL, DL, VT, ByteIndex,
1299 DAG.getConstant(3, DL, VT));
1300
1301 // Put the mask in correct place
1302 SDValue Mask = DAG.getNode(ISD::SHL, DL, VT, MaskConstant, BitShift);
1303
Jan Veselyf1705042017-01-20 21:24:26 +00001304 // Put the value bits in correct place
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001305 SDValue TruncValue = DAG.getNode(ISD::AND, DL, VT, Value, MaskConstant);
Jan Vesely06200bd2017-01-06 21:00:46 +00001306 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, VT, TruncValue, BitShift);
1307
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001308 // XXX: If we add a 64-bit ZW register class, then we could use a 2 x i32
1309 // vector instead.
1310 SDValue Src[4] = {
1311 ShiftedValue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001312 DAG.getConstant(0, DL, MVT::i32),
1313 DAG.getConstant(0, DL, MVT::i32),
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001314 Mask
1315 };
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001316 SDValue Input = DAG.getBuildVector(MVT::v4i32, DL, Src);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001317 SDValue Args[3] = { Chain, Input, DWordAddr };
1318 return DAG.getMemIntrinsicNode(AMDGPUISD::STORE_MSKOR, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00001319 Op->getVTList(), Args, MemVT,
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001320 StoreNode->getMemOperand());
Jan Vesely06200bd2017-01-06 21:00:46 +00001321 } else if (Ptr->getOpcode() != AMDGPUISD::DWORDADDR && VT.bitsGE(MVT::i32)) {
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001322 // Convert pointer from byte address to dword address.
Jan Vesely06200bd2017-01-06 21:00:46 +00001323 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, PtrVT, DWordAddr);
Tom Stellard75aadc22012-12-11 21:25:42 +00001324
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001325 if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +00001326 llvm_unreachable("Truncated and indexed stores not supported yet");
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001327 } else {
1328 Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
1329 }
1330 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001331 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001332 }
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001333
Jan Vesely06200bd2017-01-06 21:00:46 +00001334 // GLOBAL_ADDRESS has been handled above, LOCAL_ADDRESS allows all sizes
Yaxun Liu1a14bfa2017-03-27 14:04:01 +00001335 if (AS != AMDGPUASI.PRIVATE_ADDRESS)
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001336 return SDValue();
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001337
Matt Arsenault95245662016-02-11 05:32:46 +00001338 if (MemVT.bitsLT(MVT::i32))
1339 return lowerPrivateTruncStore(StoreNode, DAG);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001340
Jan Vesely06200bd2017-01-06 21:00:46 +00001341 // Standard i32+ store, tag it with DWORDADDR to note that the address
1342 // has been shifted
1343 if (Ptr.getOpcode() != AMDGPUISD::DWORDADDR) {
1344 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, PtrVT, DWordAddr);
1345 return DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001346 }
1347
Jan Vesely06200bd2017-01-06 21:00:46 +00001348 // Tagged i32+ stores will be matched by patterns
1349 return SDValue();
Tom Stellard75aadc22012-12-11 21:25:42 +00001350}
1351
Tom Stellard365366f2013-01-23 02:09:06 +00001352// return (512 + (kc_bank << 12)
1353static int
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001354ConstantAddressBlock(unsigned AddressSpace) {
Tom Stellard365366f2013-01-23 02:09:06 +00001355 switch (AddressSpace) {
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001356 case AMDGPUAS::CONSTANT_BUFFER_0:
Tom Stellard365366f2013-01-23 02:09:06 +00001357 return 512;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001358 case AMDGPUAS::CONSTANT_BUFFER_1:
Tom Stellard365366f2013-01-23 02:09:06 +00001359 return 512 + 4096;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001360 case AMDGPUAS::CONSTANT_BUFFER_2:
Tom Stellard365366f2013-01-23 02:09:06 +00001361 return 512 + 4096 * 2;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001362 case AMDGPUAS::CONSTANT_BUFFER_3:
Tom Stellard365366f2013-01-23 02:09:06 +00001363 return 512 + 4096 * 3;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001364 case AMDGPUAS::CONSTANT_BUFFER_4:
Tom Stellard365366f2013-01-23 02:09:06 +00001365 return 512 + 4096 * 4;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001366 case AMDGPUAS::CONSTANT_BUFFER_5:
Tom Stellard365366f2013-01-23 02:09:06 +00001367 return 512 + 4096 * 5;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001368 case AMDGPUAS::CONSTANT_BUFFER_6:
Tom Stellard365366f2013-01-23 02:09:06 +00001369 return 512 + 4096 * 6;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001370 case AMDGPUAS::CONSTANT_BUFFER_7:
Tom Stellard365366f2013-01-23 02:09:06 +00001371 return 512 + 4096 * 7;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001372 case AMDGPUAS::CONSTANT_BUFFER_8:
Tom Stellard365366f2013-01-23 02:09:06 +00001373 return 512 + 4096 * 8;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001374 case AMDGPUAS::CONSTANT_BUFFER_9:
Tom Stellard365366f2013-01-23 02:09:06 +00001375 return 512 + 4096 * 9;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001376 case AMDGPUAS::CONSTANT_BUFFER_10:
Tom Stellard365366f2013-01-23 02:09:06 +00001377 return 512 + 4096 * 10;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001378 case AMDGPUAS::CONSTANT_BUFFER_11:
Tom Stellard365366f2013-01-23 02:09:06 +00001379 return 512 + 4096 * 11;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001380 case AMDGPUAS::CONSTANT_BUFFER_12:
Tom Stellard365366f2013-01-23 02:09:06 +00001381 return 512 + 4096 * 12;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001382 case AMDGPUAS::CONSTANT_BUFFER_13:
Tom Stellard365366f2013-01-23 02:09:06 +00001383 return 512 + 4096 * 13;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001384 case AMDGPUAS::CONSTANT_BUFFER_14:
Tom Stellard365366f2013-01-23 02:09:06 +00001385 return 512 + 4096 * 14;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001386 case AMDGPUAS::CONSTANT_BUFFER_15:
Tom Stellard365366f2013-01-23 02:09:06 +00001387 return 512 + 4096 * 15;
1388 default:
1389 return -1;
1390 }
1391}
1392
Matt Arsenault6dfda962016-02-10 18:21:39 +00001393SDValue R600TargetLowering::lowerPrivateExtLoad(SDValue Op,
1394 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001395 SDLoc DL(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001396 LoadSDNode *Load = cast<LoadSDNode>(Op);
1397 ISD::LoadExtType ExtType = Load->getExtensionType();
1398 EVT MemVT = Load->getMemoryVT();
Jan Vesely06200bd2017-01-06 21:00:46 +00001399 assert(Load->getAlignment() >= MemVT.getStoreSize());
Tom Stellard365366f2013-01-23 02:09:06 +00001400
Jan Vesely06200bd2017-01-06 21:00:46 +00001401 SDValue BasePtr = Load->getBasePtr();
1402 SDValue Chain = Load->getChain();
1403 SDValue Offset = Load->getOffset();
Matt Arsenault6dfda962016-02-10 18:21:39 +00001404
Jan Vesely06200bd2017-01-06 21:00:46 +00001405 SDValue LoadPtr = BasePtr;
1406 if (!Offset.isUndef()) {
1407 LoadPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, Offset);
1408 }
1409
1410 // Get dword location
1411 // NOTE: this should be eliminated by the future SHR ptr, 2
1412 SDValue Ptr = DAG.getNode(ISD::AND, DL, MVT::i32, LoadPtr,
1413 DAG.getConstant(0xfffffffc, DL, MVT::i32));
1414
1415 // Load dword
1416 // TODO: can we be smarter about machine pointer info?
Yaxun Liu35845f02017-11-10 02:03:28 +00001417 MachinePointerInfo PtrInfo(UndefValue::get(
1418 Type::getInt32PtrTy(*DAG.getContext(), AMDGPUASI.PRIVATE_ADDRESS)));
1419 SDValue Read = DAG.getLoad(MVT::i32, DL, Chain, Ptr, PtrInfo);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001420
1421 // Get offset within the register.
1422 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32,
Jan Vesely06200bd2017-01-06 21:00:46 +00001423 LoadPtr, DAG.getConstant(0x3, DL, MVT::i32));
Matt Arsenault6dfda962016-02-10 18:21:39 +00001424
1425 // Bit offset of target byte (byteIdx * 8).
1426 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1427 DAG.getConstant(3, DL, MVT::i32));
1428
1429 // Shift to the right.
Jan Vesely06200bd2017-01-06 21:00:46 +00001430 SDValue Ret = DAG.getNode(ISD::SRL, DL, MVT::i32, Read, ShiftAmt);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001431
1432 // Eliminate the upper bits by setting them to ...
1433 EVT MemEltVT = MemVT.getScalarType();
1434
Jan Vesely06200bd2017-01-06 21:00:46 +00001435 if (ExtType == ISD::SEXTLOAD) { // ... ones.
Matt Arsenault6dfda962016-02-10 18:21:39 +00001436 SDValue MemEltVTNode = DAG.getValueType(MemEltVT);
Jan Vesely06200bd2017-01-06 21:00:46 +00001437 Ret = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, Ret, MemEltVTNode);
1438 } else { // ... or zeros.
1439 Ret = DAG.getZeroExtendInReg(Ret, DL, MemEltVT);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001440 }
1441
Matt Arsenault6dfda962016-02-10 18:21:39 +00001442 SDValue Ops[] = {
Jan Vesely06200bd2017-01-06 21:00:46 +00001443 Ret,
1444 Read.getValue(1) // This should be our output chain
Matt Arsenault6dfda962016-02-10 18:21:39 +00001445 };
1446
1447 return DAG.getMergeValues(Ops, DL);
1448}
1449
1450SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1451 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1452 unsigned AS = LoadNode->getAddressSpace();
1453 EVT MemVT = LoadNode->getMemoryVT();
1454 ISD::LoadExtType ExtType = LoadNode->getExtensionType();
1455
Yaxun Liu1a14bfa2017-03-27 14:04:01 +00001456 if (AS == AMDGPUASI.PRIVATE_ADDRESS &&
Matt Arsenault6dfda962016-02-10 18:21:39 +00001457 ExtType != ISD::NON_EXTLOAD && MemVT.bitsLT(MVT::i32)) {
1458 return lowerPrivateExtLoad(Op, DAG);
1459 }
1460
1461 SDLoc DL(Op);
1462 EVT VT = Op.getValueType();
1463 SDValue Chain = LoadNode->getChain();
1464 SDValue Ptr = LoadNode->getBasePtr();
Tom Stellarde9373602014-01-22 19:24:14 +00001465
Yaxun Liu1a14bfa2017-03-27 14:04:01 +00001466 if ((LoadNode->getAddressSpace() == AMDGPUASI.LOCAL_ADDRESS ||
1467 LoadNode->getAddressSpace() == AMDGPUASI.PRIVATE_ADDRESS) &&
Jan Vesely06200bd2017-01-06 21:00:46 +00001468 VT.isVector()) {
1469 return scalarizeVectorLoad(LoadNode, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001470 }
1471
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001472 int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
Matt Arsenault00a0d6f2013-11-13 02:39:07 +00001473 if (ConstantBlock > -1 &&
1474 ((LoadNode->getExtensionType() == ISD::NON_EXTLOAD) ||
1475 (LoadNode->getExtensionType() == ISD::ZEXTLOAD))) {
Tom Stellard365366f2013-01-23 02:09:06 +00001476 SDValue Result;
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001477 if (isa<ConstantExpr>(LoadNode->getMemOperand()->getValue()) ||
1478 isa<Constant>(LoadNode->getMemOperand()->getValue()) ||
Matt Arsenaultef1a9502013-11-01 17:39:26 +00001479 isa<ConstantSDNode>(Ptr)) {
Tom Stellard365366f2013-01-23 02:09:06 +00001480 SDValue Slots[4];
1481 for (unsigned i = 0; i < 4; i++) {
1482 // We want Const position encoded with the following formula :
1483 // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
1484 // const_index is Ptr computed by llvm using an alignment of 16.
1485 // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
1486 // then div by 4 at the ISel step
1487 SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001488 DAG.getConstant(4 * i + ConstantBlock * 16, DL, MVT::i32));
Tom Stellard365366f2013-01-23 02:09:06 +00001489 Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
1490 }
Tom Stellard0344cdf2013-08-01 15:23:42 +00001491 EVT NewVT = MVT::v4i32;
1492 unsigned NumElements = 4;
1493 if (VT.isVector()) {
1494 NewVT = VT;
1495 NumElements = VT.getVectorNumElements();
1496 }
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001497 Result = DAG.getBuildVector(NewVT, DL, makeArrayRef(Slots, NumElements));
Tom Stellard365366f2013-01-23 02:09:06 +00001498 } else {
Alp Tokerf907b892013-12-05 05:44:44 +00001499 // non-constant ptr can't be folded, keeps it as a v4f32 load
Tom Stellard365366f2013-01-23 02:09:06 +00001500 Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001501 DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr,
1502 DAG.getConstant(4, DL, MVT::i32)),
1503 DAG.getConstant(LoadNode->getAddressSpace() -
Yaxun Liu1a14bfa2017-03-27 14:04:01 +00001504 AMDGPUASI.CONSTANT_BUFFER_0, DL, MVT::i32)
Tom Stellard365366f2013-01-23 02:09:06 +00001505 );
1506 }
1507
1508 if (!VT.isVector()) {
1509 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001510 DAG.getConstant(0, DL, MVT::i32));
Tom Stellard365366f2013-01-23 02:09:06 +00001511 }
1512
1513 SDValue MergedValues[2] = {
Matt Arsenault7939acd2014-04-07 16:44:24 +00001514 Result,
1515 Chain
Tom Stellard365366f2013-01-23 02:09:06 +00001516 };
Craig Topper64941d92014-04-27 19:20:57 +00001517 return DAG.getMergeValues(MergedValues, DL);
Tom Stellard365366f2013-01-23 02:09:06 +00001518 }
1519
Matt Arsenault909d0c02013-10-30 23:43:29 +00001520 // For most operations returning SDValue() will result in the node being
1521 // expanded by the DAG Legalizer. This is not the case for ISD::LOAD, so we
1522 // need to manually expand loads that may be legal in some address spaces and
1523 // illegal in others. SEXT loads from CONSTANT_BUFFER_0 are supported for
1524 // compute shaders, since the data is sign extended when it is uploaded to the
1525 // buffer. However SEXT loads from other address spaces are not supported, so
1526 // we need to expand them here.
Tom Stellard84021442013-07-23 01:48:24 +00001527 if (LoadNode->getExtensionType() == ISD::SEXTLOAD) {
1528 EVT MemVT = LoadNode->getMemoryVT();
1529 assert(!MemVT.isVector() && (MemVT == MVT::i16 || MemVT == MVT::i8));
Justin Lebar9c375812016-07-15 18:27:10 +00001530 SDValue NewLoad = DAG.getExtLoad(
1531 ISD::EXTLOAD, DL, VT, Chain, Ptr, LoadNode->getPointerInfo(), MemVT,
1532 LoadNode->getAlignment(), LoadNode->getMemOperand()->getFlags());
Jan Veselyb670d372015-05-26 18:07:22 +00001533 SDValue Res = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, NewLoad,
1534 DAG.getValueType(MemVT));
Tom Stellard84021442013-07-23 01:48:24 +00001535
Jan Veselyb670d372015-05-26 18:07:22 +00001536 SDValue MergedValues[2] = { Res, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00001537 return DAG.getMergeValues(MergedValues, DL);
Tom Stellard84021442013-07-23 01:48:24 +00001538 }
1539
Yaxun Liu1a14bfa2017-03-27 14:04:01 +00001540 if (LoadNode->getAddressSpace() != AMDGPUASI.PRIVATE_ADDRESS) {
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001541 return SDValue();
1542 }
1543
Jan Vesely06200bd2017-01-06 21:00:46 +00001544 // DWORDADDR ISD marks already shifted address
1545 if (Ptr.getOpcode() != AMDGPUISD::DWORDADDR) {
1546 assert(VT == MVT::i32);
1547 Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr, DAG.getConstant(2, DL, MVT::i32));
1548 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, MVT::i32, Ptr);
1549 return DAG.getLoad(MVT::i32, DL, Chain, Ptr, LoadNode->getMemOperand());
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001550 }
Jan Vesely06200bd2017-01-06 21:00:46 +00001551 return SDValue();
Tom Stellard365366f2013-01-23 02:09:06 +00001552}
Tom Stellard75aadc22012-12-11 21:25:42 +00001553
Matt Arsenault1d555c42014-06-23 18:00:55 +00001554SDValue R600TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
1555 SDValue Chain = Op.getOperand(0);
1556 SDValue Cond = Op.getOperand(1);
1557 SDValue Jump = Op.getOperand(2);
1558
1559 return DAG.getNode(AMDGPUISD::BRANCH_COND, SDLoc(Op), Op.getValueType(),
1560 Chain, Jump, Cond);
1561}
1562
Matt Arsenault81d06012016-03-07 21:10:13 +00001563SDValue R600TargetLowering::lowerFrameIndex(SDValue Op,
1564 SelectionDAG &DAG) const {
1565 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardc5a154d2018-06-28 23:47:12 +00001566 const R600FrameLowering *TFL = Subtarget->getFrameLowering();
Matt Arsenault81d06012016-03-07 21:10:13 +00001567
1568 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op);
1569
1570 unsigned FrameIndex = FIN->getIndex();
1571 unsigned IgnoredFrameReg;
1572 unsigned Offset =
1573 TFL->getFrameIndexReference(MF, FrameIndex, IgnoredFrameReg);
1574 return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF), SDLoc(Op),
1575 Op.getValueType());
1576}
1577
Tom Stellardc5a154d2018-06-28 23:47:12 +00001578CCAssignFn *R600TargetLowering::CCAssignFnForCall(CallingConv::ID CC,
1579 bool IsVarArg) const {
1580 switch (CC) {
1581 case CallingConv::AMDGPU_KERNEL:
1582 case CallingConv::SPIR_KERNEL:
1583 case CallingConv::C:
1584 case CallingConv::Fast:
1585 case CallingConv::Cold:
Evgeniy Stepanov1971ba02018-07-14 01:20:53 +00001586 return CC_R600_Kernel;
Tom Stellardc5a154d2018-06-28 23:47:12 +00001587 case CallingConv::AMDGPU_VS:
1588 case CallingConv::AMDGPU_GS:
1589 case CallingConv::AMDGPU_PS:
1590 case CallingConv::AMDGPU_CS:
1591 case CallingConv::AMDGPU_HS:
1592 case CallingConv::AMDGPU_ES:
1593 case CallingConv::AMDGPU_LS:
1594 return CC_R600;
1595 default:
1596 report_fatal_error("Unsupported calling convention.");
1597 }
1598}
1599
Tom Stellard75aadc22012-12-11 21:25:42 +00001600/// XXX Only kernel functions are supported, so we can assume for now that
1601/// every function is a kernel function, but in the future we should use
1602/// separate calling conventions for kernel and non-kernel functions.
1603SDValue R600TargetLowering::LowerFormalArguments(
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001604 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1605 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
1606 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Tom Stellardacfeebf2013-07-23 01:48:05 +00001607 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001608 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1609 *DAG.getContext());
Vincent Lejeunef143af32013-11-11 22:10:24 +00001610 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardaf775432013-10-23 00:44:32 +00001611 SmallVector<ISD::InputArg, 8> LocalIns;
1612
Tom Stellardbbeb45a2016-09-16 21:53:00 +00001613 if (AMDGPU::isShader(CallConv)) {
Matt Arsenaulte622dc32017-04-11 22:29:24 +00001614 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForCall(CallConv, isVarArg));
Tom Stellardbbeb45a2016-09-16 21:53:00 +00001615 } else {
1616 analyzeFormalArgumentsCompute(CCInfo, Ins);
1617 }
Tom Stellardacfeebf2013-07-23 01:48:05 +00001618
Tom Stellard1e803092013-07-23 01:48:18 +00001619 for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
Tom Stellardacfeebf2013-07-23 01:48:05 +00001620 CCValAssign &VA = ArgLocs[i];
Matt Arsenault74ef2772014-08-13 18:14:11 +00001621 const ISD::InputArg &In = Ins[i];
1622 EVT VT = In.VT;
1623 EVT MemVT = VA.getLocVT();
1624 if (!VT.isVector() && MemVT.isVector()) {
1625 // Get load source type if scalarized.
1626 MemVT = MemVT.getVectorElementType();
1627 }
Tom Stellard78e01292013-07-23 01:47:58 +00001628
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001629 if (AMDGPU::isShader(CallConv)) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00001630 unsigned Reg = MF.addLiveIn(VA.getLocReg(), &R600::R600_Reg128RegClass);
Vincent Lejeunef143af32013-11-11 22:10:24 +00001631 SDValue Register = DAG.getCopyFromReg(Chain, DL, Reg, VT);
1632 InVals.push_back(Register);
1633 continue;
1634 }
1635
Tom Stellard75aadc22012-12-11 21:25:42 +00001636 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
Yaxun Liu1a14bfa2017-03-27 14:04:01 +00001637 AMDGPUASI.CONSTANT_BUFFER_0);
Tom Stellardacfeebf2013-07-23 01:48:05 +00001638
Matt Arsenaultfae02982014-03-17 18:58:11 +00001639 // i64 isn't a legal type, so the register type used ends up as i32, which
1640 // isn't expected here. It attempts to create this sextload, but it ends up
1641 // being invalid. Somehow this seems to work with i64 arguments, but breaks
1642 // for <1 x i64>.
1643
Tom Stellardacfeebf2013-07-23 01:48:05 +00001644 // The first 36 bytes of the input buffer contains information about
1645 // thread group and global sizes.
Matt Arsenault74ef2772014-08-13 18:14:11 +00001646 ISD::LoadExtType Ext = ISD::NON_EXTLOAD;
1647 if (MemVT.getScalarSizeInBits() != VT.getScalarSizeInBits()) {
1648 // FIXME: This should really check the extload type, but the handling of
1649 // extload vector parameters seems to be broken.
Matt Arsenaulte1f030c2014-04-11 20:59:54 +00001650
Matt Arsenault74ef2772014-08-13 18:14:11 +00001651 // Ext = In.Flags.isSExt() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
1652 Ext = ISD::SEXTLOAD;
1653 }
1654
1655 // Compute the offset from the value.
1656 // XXX - I think PartOffset should give you this, but it seems to give the
1657 // size of the register which isn't useful.
1658
Andrew Trick05938a52015-02-16 18:10:47 +00001659 unsigned ValBase = ArgLocs[In.getOrigArgIndex()].getLocMemOffset();
Matt Arsenault74ef2772014-08-13 18:14:11 +00001660 unsigned PartOffset = VA.getLocMemOffset();
Evgeniy Stepanov1971ba02018-07-14 01:20:53 +00001661 unsigned Offset = Subtarget->getExplicitKernelArgOffset(MF.getFunction()) +
1662 VA.getLocMemOffset();
Matt Arsenault74ef2772014-08-13 18:14:11 +00001663
1664 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy), PartOffset - ValBase);
Justin Lebar9c375812016-07-15 18:27:10 +00001665 SDValue Arg = DAG.getLoad(
1666 ISD::UNINDEXED, Ext, VT, DL, Chain,
Evgeniy Stepanov1971ba02018-07-14 01:20:53 +00001667 DAG.getConstant(Offset, DL, MVT::i32), DAG.getUNDEF(MVT::i32), PtrInfo,
Justin Lebaradbf09e2016-09-11 01:38:58 +00001668 MemVT, /* Alignment = */ 4, MachineMemOperand::MONonTemporal |
1669 MachineMemOperand::MODereferenceable |
1670 MachineMemOperand::MOInvariant);
Matt Arsenault209a7b92014-04-18 07:40:20 +00001671
1672 // 4 is the preferred alignment for the CONSTANT memory space.
Tom Stellard75aadc22012-12-11 21:25:42 +00001673 InVals.push_back(Arg);
Tom Stellard75aadc22012-12-11 21:25:42 +00001674 }
1675 return Chain;
1676}
1677
Mehdi Amini44ede332015-07-09 02:09:04 +00001678EVT R600TargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1679 EVT VT) const {
Matt Arsenault209a7b92014-04-18 07:40:20 +00001680 if (!VT.isVector())
1681 return MVT::i32;
Tom Stellard75aadc22012-12-11 21:25:42 +00001682 return VT.changeVectorElementTypeToInteger();
1683}
1684
Nirav Dave4dcad5d2017-07-10 20:25:54 +00001685bool R600TargetLowering::canMergeStoresTo(unsigned AS, EVT MemVT,
1686 const SelectionDAG &DAG) const {
Nirav Daved20066c2017-05-24 15:59:09 +00001687 // Local and Private addresses do not handle vectors. Limit to i32
1688 if ((AS == AMDGPUASI.LOCAL_ADDRESS || AS == AMDGPUASI.PRIVATE_ADDRESS)) {
1689 return (MemVT.getSizeInBits() <= 32);
1690 }
1691 return true;
1692}
1693
Matt Arsenaultfa67bdb2016-02-22 21:04:16 +00001694bool R600TargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
1695 unsigned AddrSpace,
1696 unsigned Align,
1697 bool *IsFast) const {
1698 if (IsFast)
1699 *IsFast = false;
1700
1701 if (!VT.isSimple() || VT == MVT::Other)
1702 return false;
1703
1704 if (VT.bitsLT(MVT::i32))
1705 return false;
1706
1707 // TODO: This is a rough estimate.
1708 if (IsFast)
1709 *IsFast = true;
1710
1711 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
1712}
1713
Matt Arsenault209a7b92014-04-18 07:40:20 +00001714static SDValue CompactSwizzlableVector(
1715 SelectionDAG &DAG, SDValue VectorEntry,
1716 DenseMap<unsigned, unsigned> &RemapSwizzle) {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001717 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1718 assert(RemapSwizzle.empty());
1719 SDValue NewBldVec[4] = {
Matt Arsenault209a7b92014-04-18 07:40:20 +00001720 VectorEntry.getOperand(0),
1721 VectorEntry.getOperand(1),
1722 VectorEntry.getOperand(2),
1723 VectorEntry.getOperand(3)
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001724 };
1725
1726 for (unsigned i = 0; i < 4; i++) {
Sanjay Patel57195842016-03-14 17:28:46 +00001727 if (NewBldVec[i].isUndef())
Vincent Lejeunefa58a5f2013-10-13 17:56:10 +00001728 // We mask write here to teach later passes that the ith element of this
1729 // vector is undef. Thus we can use it to reduce 128 bits reg usage,
1730 // break false dependencies and additionnaly make assembly easier to read.
1731 RemapSwizzle[i] = 7; // SEL_MASK_WRITE
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001732 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(NewBldVec[i])) {
1733 if (C->isZero()) {
1734 RemapSwizzle[i] = 4; // SEL_0
1735 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1736 } else if (C->isExactlyValue(1.0)) {
1737 RemapSwizzle[i] = 5; // SEL_1
1738 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1739 }
1740 }
1741
Sanjay Patel57195842016-03-14 17:28:46 +00001742 if (NewBldVec[i].isUndef())
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001743 continue;
1744 for (unsigned j = 0; j < i; j++) {
1745 if (NewBldVec[i] == NewBldVec[j]) {
1746 NewBldVec[i] = DAG.getUNDEF(NewBldVec[i].getValueType());
1747 RemapSwizzle[i] = j;
1748 break;
1749 }
1750 }
1751 }
1752
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001753 return DAG.getBuildVector(VectorEntry.getValueType(), SDLoc(VectorEntry),
1754 NewBldVec);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001755}
1756
Benjamin Kramer193960c2013-06-11 13:32:25 +00001757static SDValue ReorganizeVector(SelectionDAG &DAG, SDValue VectorEntry,
1758 DenseMap<unsigned, unsigned> &RemapSwizzle) {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001759 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1760 assert(RemapSwizzle.empty());
1761 SDValue NewBldVec[4] = {
1762 VectorEntry.getOperand(0),
1763 VectorEntry.getOperand(1),
1764 VectorEntry.getOperand(2),
1765 VectorEntry.getOperand(3)
1766 };
1767 bool isUnmovable[4] = { false, false, false, false };
Vincent Lejeunecc0ea742013-12-10 14:43:31 +00001768 for (unsigned i = 0; i < 4; i++) {
Vincent Lejeuneb8aac8d2013-07-09 15:03:25 +00001769 RemapSwizzle[i] = i;
Vincent Lejeunecc0ea742013-12-10 14:43:31 +00001770 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1771 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1772 ->getZExtValue();
1773 if (i == Idx)
1774 isUnmovable[Idx] = true;
1775 }
1776 }
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001777
1778 for (unsigned i = 0; i < 4; i++) {
1779 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1780 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1781 ->getZExtValue();
Vincent Lejeune301beb82013-10-13 17:56:04 +00001782 if (isUnmovable[Idx])
1783 continue;
1784 // Swap i and Idx
1785 std::swap(NewBldVec[Idx], NewBldVec[i]);
1786 std::swap(RemapSwizzle[i], RemapSwizzle[Idx]);
1787 break;
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001788 }
1789 }
1790
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001791 return DAG.getBuildVector(VectorEntry.getValueType(), SDLoc(VectorEntry),
1792 NewBldVec);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001793}
1794
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001795SDValue R600TargetLowering::OptimizeSwizzle(SDValue BuildVector, SDValue Swz[4],
1796 SelectionDAG &DAG,
1797 const SDLoc &DL) const {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001798 assert(BuildVector.getOpcode() == ISD::BUILD_VECTOR);
1799 // Old -> New swizzle values
1800 DenseMap<unsigned, unsigned> SwizzleRemap;
1801
1802 BuildVector = CompactSwizzlableVector(DAG, BuildVector, SwizzleRemap);
1803 for (unsigned i = 0; i < 4; i++) {
Benjamin Kramer619c4e52015-04-10 11:24:51 +00001804 unsigned Idx = cast<ConstantSDNode>(Swz[i])->getZExtValue();
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001805 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001806 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], DL, MVT::i32);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001807 }
1808
1809 SwizzleRemap.clear();
1810 BuildVector = ReorganizeVector(DAG, BuildVector, SwizzleRemap);
1811 for (unsigned i = 0; i < 4; i++) {
Benjamin Kramer619c4e52015-04-10 11:24:51 +00001812 unsigned Idx = cast<ConstantSDNode>(Swz[i])->getZExtValue();
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001813 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001814 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], DL, MVT::i32);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001815 }
1816
1817 return BuildVector;
1818}
1819
Tom Stellard75aadc22012-12-11 21:25:42 +00001820//===----------------------------------------------------------------------===//
1821// Custom DAG Optimizations
1822//===----------------------------------------------------------------------===//
1823
1824SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
1825 DAGCombinerInfo &DCI) const {
1826 SelectionDAG &DAG = DCI.DAG;
Jan Vesely89876672016-08-29 23:21:46 +00001827 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00001828
1829 switch (N->getOpcode()) {
1830 // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
1831 case ISD::FP_ROUND: {
1832 SDValue Arg = N->getOperand(0);
1833 if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
Jan Vesely89876672016-08-29 23:21:46 +00001834 return DAG.getNode(ISD::UINT_TO_FP, DL, N->getValueType(0),
Tom Stellard75aadc22012-12-11 21:25:42 +00001835 Arg.getOperand(0));
1836 }
1837 break;
1838 }
Tom Stellarde06163a2013-02-07 14:02:35 +00001839
1840 // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
1841 // (i32 select_cc f32, f32, -1, 0 cc)
1842 //
1843 // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1844 // this to one of the SET*_DX10 instructions.
1845 case ISD::FP_TO_SINT: {
1846 SDValue FNeg = N->getOperand(0);
1847 if (FNeg.getOpcode() != ISD::FNEG) {
1848 return SDValue();
1849 }
1850 SDValue SelectCC = FNeg.getOperand(0);
1851 if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1852 SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1853 SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1854 !isHWTrueValue(SelectCC.getOperand(2)) ||
1855 !isHWFalseValue(SelectCC.getOperand(3))) {
1856 return SDValue();
1857 }
1858
Jan Vesely89876672016-08-29 23:21:46 +00001859 return DAG.getNode(ISD::SELECT_CC, DL, N->getValueType(0),
Tom Stellarde06163a2013-02-07 14:02:35 +00001860 SelectCC.getOperand(0), // LHS
1861 SelectCC.getOperand(1), // RHS
Jan Vesely89876672016-08-29 23:21:46 +00001862 DAG.getConstant(-1, DL, MVT::i32), // True
1863 DAG.getConstant(0, DL, MVT::i32), // False
Tom Stellarde06163a2013-02-07 14:02:35 +00001864 SelectCC.getOperand(4)); // CC
1865
1866 break;
1867 }
Quentin Colombete2e05482013-07-30 00:27:16 +00001868
NAKAMURA Takumi8a046432013-10-28 04:07:38 +00001869 // insert_vector_elt (build_vector elt0, ... , eltN), NewEltIdx, idx
1870 // => build_vector elt0, ... , NewEltIdx, ... , eltN
Quentin Colombete2e05482013-07-30 00:27:16 +00001871 case ISD::INSERT_VECTOR_ELT: {
1872 SDValue InVec = N->getOperand(0);
1873 SDValue InVal = N->getOperand(1);
1874 SDValue EltNo = N->getOperand(2);
Quentin Colombete2e05482013-07-30 00:27:16 +00001875
1876 // If the inserted element is an UNDEF, just use the input vector.
Sanjay Patel57195842016-03-14 17:28:46 +00001877 if (InVal.isUndef())
Quentin Colombete2e05482013-07-30 00:27:16 +00001878 return InVec;
1879
1880 EVT VT = InVec.getValueType();
1881
1882 // If we can't generate a legal BUILD_VECTOR, exit
1883 if (!isOperationLegal(ISD::BUILD_VECTOR, VT))
1884 return SDValue();
1885
1886 // Check that we know which element is being inserted
1887 if (!isa<ConstantSDNode>(EltNo))
1888 return SDValue();
1889 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
1890
1891 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
1892 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
1893 // vector elements.
1894 SmallVector<SDValue, 8> Ops;
1895 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
1896 Ops.append(InVec.getNode()->op_begin(),
1897 InVec.getNode()->op_end());
Sanjay Patel57195842016-03-14 17:28:46 +00001898 } else if (InVec.isUndef()) {
Quentin Colombete2e05482013-07-30 00:27:16 +00001899 unsigned NElts = VT.getVectorNumElements();
1900 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
1901 } else {
1902 return SDValue();
1903 }
1904
1905 // Insert the element
1906 if (Elt < Ops.size()) {
1907 // All the operands of BUILD_VECTOR must have the same type;
1908 // we enforce that here.
1909 EVT OpVT = Ops[0].getValueType();
1910 if (InVal.getValueType() != OpVT)
1911 InVal = OpVT.bitsGT(InVal.getValueType()) ?
Jan Vesely89876672016-08-29 23:21:46 +00001912 DAG.getNode(ISD::ANY_EXTEND, DL, OpVT, InVal) :
1913 DAG.getNode(ISD::TRUNCATE, DL, OpVT, InVal);
Quentin Colombete2e05482013-07-30 00:27:16 +00001914 Ops[Elt] = InVal;
1915 }
1916
1917 // Return the new vector
Jan Vesely89876672016-08-29 23:21:46 +00001918 return DAG.getBuildVector(VT, DL, Ops);
Quentin Colombete2e05482013-07-30 00:27:16 +00001919 }
1920
Tom Stellard365366f2013-01-23 02:09:06 +00001921 // Extract_vec (Build_vector) generated by custom lowering
1922 // also needs to be customly combined
1923 case ISD::EXTRACT_VECTOR_ELT: {
1924 SDValue Arg = N->getOperand(0);
1925 if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1926 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1927 unsigned Element = Const->getZExtValue();
1928 return Arg->getOperand(Element);
1929 }
1930 }
Tom Stellarddd04c832013-01-31 22:11:53 +00001931 if (Arg.getOpcode() == ISD::BITCAST &&
Jan Veselyea457462016-09-02 20:13:19 +00001932 Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
1933 (Arg.getOperand(0).getValueType().getVectorNumElements() ==
1934 Arg.getValueType().getVectorNumElements())) {
Tom Stellarddd04c832013-01-31 22:11:53 +00001935 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1936 unsigned Element = Const->getZExtValue();
Jan Vesely89876672016-08-29 23:21:46 +00001937 return DAG.getNode(ISD::BITCAST, DL, N->getVTList(),
1938 Arg->getOperand(0).getOperand(Element));
Tom Stellarddd04c832013-01-31 22:11:53 +00001939 }
1940 }
Mehdi Aminie029eae2015-07-16 06:23:12 +00001941 break;
Tom Stellard365366f2013-01-23 02:09:06 +00001942 }
Tom Stellarde06163a2013-02-07 14:02:35 +00001943
1944 case ISD::SELECT_CC: {
Tom Stellardafa8b532014-05-09 16:42:16 +00001945 // Try common optimizations
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00001946 if (SDValue Ret = AMDGPUTargetLowering::PerformDAGCombine(N, DCI))
Tom Stellardafa8b532014-05-09 16:42:16 +00001947 return Ret;
1948
Tom Stellarde06163a2013-02-07 14:02:35 +00001949 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1950 // selectcc x, y, a, b, inv(cc)
Tom Stellard5e524892013-03-08 15:37:11 +00001951 //
1952 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, setne ->
1953 // selectcc x, y, a, b, cc
Tom Stellarde06163a2013-02-07 14:02:35 +00001954 SDValue LHS = N->getOperand(0);
1955 if (LHS.getOpcode() != ISD::SELECT_CC) {
1956 return SDValue();
1957 }
1958
1959 SDValue RHS = N->getOperand(1);
1960 SDValue True = N->getOperand(2);
1961 SDValue False = N->getOperand(3);
Tom Stellard5e524892013-03-08 15:37:11 +00001962 ISD::CondCode NCC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Tom Stellarde06163a2013-02-07 14:02:35 +00001963
1964 if (LHS.getOperand(2).getNode() != True.getNode() ||
1965 LHS.getOperand(3).getNode() != False.getNode() ||
Tom Stellard5e524892013-03-08 15:37:11 +00001966 RHS.getNode() != False.getNode()) {
Tom Stellarde06163a2013-02-07 14:02:35 +00001967 return SDValue();
1968 }
1969
Tom Stellard5e524892013-03-08 15:37:11 +00001970 switch (NCC) {
1971 default: return SDValue();
1972 case ISD::SETNE: return LHS;
1973 case ISD::SETEQ: {
1974 ISD::CondCode LHSCC = cast<CondCodeSDNode>(LHS.getOperand(4))->get();
1975 LHSCC = ISD::getSetCCInverse(LHSCC,
1976 LHS.getOperand(0).getValueType().isInteger());
Tom Stellardcd428182013-09-28 02:50:38 +00001977 if (DCI.isBeforeLegalizeOps() ||
1978 isCondCodeLegal(LHSCC, LHS.getOperand(0).getSimpleValueType()))
Jan Vesely89876672016-08-29 23:21:46 +00001979 return DAG.getSelectCC(DL,
Tom Stellardcd428182013-09-28 02:50:38 +00001980 LHS.getOperand(0),
1981 LHS.getOperand(1),
1982 LHS.getOperand(2),
1983 LHS.getOperand(3),
1984 LHSCC);
1985 break;
Vincent Lejeuned80bc152013-02-14 16:55:06 +00001986 }
Tom Stellard5e524892013-03-08 15:37:11 +00001987 }
Tom Stellardcd428182013-09-28 02:50:38 +00001988 return SDValue();
Tom Stellard5e524892013-03-08 15:37:11 +00001989 }
Tom Stellardfbab8272013-08-16 01:12:11 +00001990
Matt Arsenault7bee6ac2016-12-05 20:23:10 +00001991 case AMDGPUISD::R600_EXPORT: {
Vincent Lejeuned80bc152013-02-14 16:55:06 +00001992 SDValue Arg = N->getOperand(1);
1993 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1994 break;
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001995
Vincent Lejeuned80bc152013-02-14 16:55:06 +00001996 SDValue NewArgs[8] = {
1997 N->getOperand(0), // Chain
1998 SDValue(),
1999 N->getOperand(2), // ArrayBase
2000 N->getOperand(3), // Type
2001 N->getOperand(4), // SWZ_X
2002 N->getOperand(5), // SWZ_Y
2003 N->getOperand(6), // SWZ_Z
2004 N->getOperand(7) // SWZ_W
2005 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002006 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[4], DAG, DL);
Matt Arsenault7bee6ac2016-12-05 20:23:10 +00002007 return DAG.getNode(AMDGPUISD::R600_EXPORT, DL, N->getVTList(), NewArgs);
Tom Stellarde06163a2013-02-07 14:02:35 +00002008 }
Vincent Lejeune276ceb82013-06-04 15:04:53 +00002009 case AMDGPUISD::TEXTURE_FETCH: {
2010 SDValue Arg = N->getOperand(1);
2011 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
2012 break;
2013
2014 SDValue NewArgs[19] = {
2015 N->getOperand(0),
2016 N->getOperand(1),
2017 N->getOperand(2),
2018 N->getOperand(3),
2019 N->getOperand(4),
2020 N->getOperand(5),
2021 N->getOperand(6),
2022 N->getOperand(7),
2023 N->getOperand(8),
2024 N->getOperand(9),
2025 N->getOperand(10),
2026 N->getOperand(11),
2027 N->getOperand(12),
2028 N->getOperand(13),
2029 N->getOperand(14),
2030 N->getOperand(15),
2031 N->getOperand(16),
2032 N->getOperand(17),
2033 N->getOperand(18),
2034 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002035 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[2], DAG, DL);
2036 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, N->getVTList(), NewArgs);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00002037 }
Jan Vesely89876672016-08-29 23:21:46 +00002038 default: break;
Tom Stellard75aadc22012-12-11 21:25:42 +00002039 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002040
2041 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00002042}
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002043
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002044bool R600TargetLowering::FoldOperand(SDNode *ParentNode, unsigned SrcIdx,
2045 SDValue &Src, SDValue &Neg, SDValue &Abs,
2046 SDValue &Sel, SDValue &Imm,
2047 SelectionDAG &DAG) const {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002048 const R600InstrInfo *TII = Subtarget->getInstrInfo();
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002049 if (!Src.isMachineOpcode())
2050 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002051
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002052 switch (Src.getMachineOpcode()) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002053 case R600::FNEG_R600:
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002054 if (!Neg.getNode())
2055 return false;
2056 Src = Src.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002057 Neg = DAG.getTargetConstant(1, SDLoc(ParentNode), MVT::i32);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002058 return true;
Tom Stellardc5a154d2018-06-28 23:47:12 +00002059 case R600::FABS_R600:
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002060 if (!Abs.getNode())
2061 return false;
2062 Src = Src.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002063 Abs = DAG.getTargetConstant(1, SDLoc(ParentNode), MVT::i32);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002064 return true;
Tom Stellardc5a154d2018-06-28 23:47:12 +00002065 case R600::CONST_COPY: {
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002066 unsigned Opcode = ParentNode->getMachineOpcode();
Tom Stellardc5a154d2018-06-28 23:47:12 +00002067 bool HasDst = TII->getOperandIdx(Opcode, R600::OpName::dst) > -1;
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002068
2069 if (!Sel.getNode())
2070 return false;
2071
2072 SDValue CstOffset = Src.getOperand(0);
2073 if (ParentNode->getValueType(0).isVector())
2074 return false;
2075
2076 // Gather constants values
2077 int SrcIndices[] = {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002078 TII->getOperandIdx(Opcode, R600::OpName::src0),
2079 TII->getOperandIdx(Opcode, R600::OpName::src1),
2080 TII->getOperandIdx(Opcode, R600::OpName::src2),
2081 TII->getOperandIdx(Opcode, R600::OpName::src0_X),
2082 TII->getOperandIdx(Opcode, R600::OpName::src0_Y),
2083 TII->getOperandIdx(Opcode, R600::OpName::src0_Z),
2084 TII->getOperandIdx(Opcode, R600::OpName::src0_W),
2085 TII->getOperandIdx(Opcode, R600::OpName::src1_X),
2086 TII->getOperandIdx(Opcode, R600::OpName::src1_Y),
2087 TII->getOperandIdx(Opcode, R600::OpName::src1_Z),
2088 TII->getOperandIdx(Opcode, R600::OpName::src1_W)
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002089 };
2090 std::vector<unsigned> Consts;
Matt Arsenault4d64f962014-05-12 19:23:21 +00002091 for (int OtherSrcIdx : SrcIndices) {
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002092 int OtherSelIdx = TII->getSelIdx(Opcode, OtherSrcIdx);
2093 if (OtherSrcIdx < 0 || OtherSelIdx < 0)
2094 continue;
2095 if (HasDst) {
2096 OtherSrcIdx--;
2097 OtherSelIdx--;
2098 }
2099 if (RegisterSDNode *Reg =
2100 dyn_cast<RegisterSDNode>(ParentNode->getOperand(OtherSrcIdx))) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002101 if (Reg->getReg() == R600::ALU_CONST) {
Matt Arsenaultb3ee3882014-05-12 19:26:38 +00002102 ConstantSDNode *Cst
2103 = cast<ConstantSDNode>(ParentNode->getOperand(OtherSelIdx));
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002104 Consts.push_back(Cst->getZExtValue());
2105 }
2106 }
2107 }
2108
Matt Arsenault37c12d72014-05-12 20:42:57 +00002109 ConstantSDNode *Cst = cast<ConstantSDNode>(CstOffset);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002110 Consts.push_back(Cst->getZExtValue());
2111 if (!TII->fitsConstReadLimitations(Consts)) {
2112 return false;
2113 }
2114
2115 Sel = CstOffset;
Tom Stellardc5a154d2018-06-28 23:47:12 +00002116 Src = DAG.getRegister(R600::ALU_CONST, MVT::f32);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002117 return true;
2118 }
Tom Stellardc5a154d2018-06-28 23:47:12 +00002119 case R600::MOV_IMM_GLOBAL_ADDR:
Jan Vesely16800392016-05-13 20:39:31 +00002120 // Check if the Imm slot is used. Taken from below.
2121 if (cast<ConstantSDNode>(Imm)->getZExtValue())
2122 return false;
2123 Imm = Src.getOperand(0);
Tom Stellardc5a154d2018-06-28 23:47:12 +00002124 Src = DAG.getRegister(R600::ALU_LITERAL_X, MVT::i32);
Jan Vesely16800392016-05-13 20:39:31 +00002125 return true;
Tom Stellardc5a154d2018-06-28 23:47:12 +00002126 case R600::MOV_IMM_I32:
2127 case R600::MOV_IMM_F32: {
2128 unsigned ImmReg = R600::ALU_LITERAL_X;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002129 uint64_t ImmValue = 0;
2130
Tom Stellardc5a154d2018-06-28 23:47:12 +00002131 if (Src.getMachineOpcode() == R600::MOV_IMM_F32) {
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002132 ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Src.getOperand(0));
2133 float FloatValue = FPC->getValueAPF().convertToFloat();
2134 if (FloatValue == 0.0) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002135 ImmReg = R600::ZERO;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002136 } else if (FloatValue == 0.5) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002137 ImmReg = R600::HALF;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002138 } else if (FloatValue == 1.0) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002139 ImmReg = R600::ONE;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002140 } else {
2141 ImmValue = FPC->getValueAPF().bitcastToAPInt().getZExtValue();
2142 }
2143 } else {
2144 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(0));
2145 uint64_t Value = C->getZExtValue();
2146 if (Value == 0) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002147 ImmReg = R600::ZERO;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002148 } else if (Value == 1) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002149 ImmReg = R600::ONE_INT;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002150 } else {
2151 ImmValue = Value;
2152 }
2153 }
2154
2155 // Check that we aren't already using an immediate.
2156 // XXX: It's possible for an instruction to have more than one
2157 // immediate operand, but this is not supported yet.
Tom Stellardc5a154d2018-06-28 23:47:12 +00002158 if (ImmReg == R600::ALU_LITERAL_X) {
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002159 if (!Imm.getNode())
2160 return false;
2161 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Imm);
2162 assert(C);
2163 if (C->getZExtValue())
2164 return false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002165 Imm = DAG.getTargetConstant(ImmValue, SDLoc(ParentNode), MVT::i32);
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002166 }
2167 Src = DAG.getRegister(ImmReg, MVT::i32);
2168 return true;
2169 }
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002170 default:
2171 return false;
2172 }
2173}
2174
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00002175/// Fold the instructions after selecting them
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002176SDNode *R600TargetLowering::PostISelFolding(MachineSDNode *Node,
2177 SelectionDAG &DAG) const {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002178 const R600InstrInfo *TII = Subtarget->getInstrInfo();
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002179 if (!Node->isMachineOpcode())
2180 return Node;
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002181
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002182 unsigned Opcode = Node->getMachineOpcode();
2183 SDValue FakeOp;
2184
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002185 std::vector<SDValue> Ops(Node->op_begin(), Node->op_end());
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002186
Tom Stellardc5a154d2018-06-28 23:47:12 +00002187 if (Opcode == R600::DOT_4) {
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002188 int OperandIdx[] = {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002189 TII->getOperandIdx(Opcode, R600::OpName::src0_X),
2190 TII->getOperandIdx(Opcode, R600::OpName::src0_Y),
2191 TII->getOperandIdx(Opcode, R600::OpName::src0_Z),
2192 TII->getOperandIdx(Opcode, R600::OpName::src0_W),
2193 TII->getOperandIdx(Opcode, R600::OpName::src1_X),
2194 TII->getOperandIdx(Opcode, R600::OpName::src1_Y),
2195 TII->getOperandIdx(Opcode, R600::OpName::src1_Z),
2196 TII->getOperandIdx(Opcode, R600::OpName::src1_W)
NAKAMURA Takumi4bb85f92013-10-28 04:07:23 +00002197 };
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002198 int NegIdx[] = {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002199 TII->getOperandIdx(Opcode, R600::OpName::src0_neg_X),
2200 TII->getOperandIdx(Opcode, R600::OpName::src0_neg_Y),
2201 TII->getOperandIdx(Opcode, R600::OpName::src0_neg_Z),
2202 TII->getOperandIdx(Opcode, R600::OpName::src0_neg_W),
2203 TII->getOperandIdx(Opcode, R600::OpName::src1_neg_X),
2204 TII->getOperandIdx(Opcode, R600::OpName::src1_neg_Y),
2205 TII->getOperandIdx(Opcode, R600::OpName::src1_neg_Z),
2206 TII->getOperandIdx(Opcode, R600::OpName::src1_neg_W)
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002207 };
2208 int AbsIdx[] = {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002209 TII->getOperandIdx(Opcode, R600::OpName::src0_abs_X),
2210 TII->getOperandIdx(Opcode, R600::OpName::src0_abs_Y),
2211 TII->getOperandIdx(Opcode, R600::OpName::src0_abs_Z),
2212 TII->getOperandIdx(Opcode, R600::OpName::src0_abs_W),
2213 TII->getOperandIdx(Opcode, R600::OpName::src1_abs_X),
2214 TII->getOperandIdx(Opcode, R600::OpName::src1_abs_Y),
2215 TII->getOperandIdx(Opcode, R600::OpName::src1_abs_Z),
2216 TII->getOperandIdx(Opcode, R600::OpName::src1_abs_W)
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002217 };
2218 for (unsigned i = 0; i < 8; i++) {
2219 if (OperandIdx[i] < 0)
2220 return Node;
2221 SDValue &Src = Ops[OperandIdx[i] - 1];
2222 SDValue &Neg = Ops[NegIdx[i] - 1];
2223 SDValue &Abs = Ops[AbsIdx[i] - 1];
Tom Stellardc5a154d2018-06-28 23:47:12 +00002224 bool HasDst = TII->getOperandIdx(Opcode, R600::OpName::dst) > -1;
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002225 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
2226 if (HasDst)
2227 SelIdx--;
2228 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002229 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, FakeOp, DAG))
2230 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2231 }
Tom Stellardc5a154d2018-06-28 23:47:12 +00002232 } else if (Opcode == R600::REG_SEQUENCE) {
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002233 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) {
2234 SDValue &Src = Ops[i];
2235 if (FoldOperand(Node, i, Src, FakeOp, FakeOp, FakeOp, FakeOp, DAG))
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002236 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2237 }
2238 } else {
2239 if (!TII->hasInstrModifiers(Opcode))
2240 return Node;
2241 int OperandIdx[] = {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002242 TII->getOperandIdx(Opcode, R600::OpName::src0),
2243 TII->getOperandIdx(Opcode, R600::OpName::src1),
2244 TII->getOperandIdx(Opcode, R600::OpName::src2)
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002245 };
2246 int NegIdx[] = {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002247 TII->getOperandIdx(Opcode, R600::OpName::src0_neg),
2248 TII->getOperandIdx(Opcode, R600::OpName::src1_neg),
2249 TII->getOperandIdx(Opcode, R600::OpName::src2_neg)
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002250 };
2251 int AbsIdx[] = {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002252 TII->getOperandIdx(Opcode, R600::OpName::src0_abs),
2253 TII->getOperandIdx(Opcode, R600::OpName::src1_abs),
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002254 -1
2255 };
2256 for (unsigned i = 0; i < 3; i++) {
2257 if (OperandIdx[i] < 0)
2258 return Node;
2259 SDValue &Src = Ops[OperandIdx[i] - 1];
2260 SDValue &Neg = Ops[NegIdx[i] - 1];
2261 SDValue FakeAbs;
2262 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
Tom Stellardc5a154d2018-06-28 23:47:12 +00002263 bool HasDst = TII->getOperandIdx(Opcode, R600::OpName::dst) > -1;
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002264 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
Tom Stellardc5a154d2018-06-28 23:47:12 +00002265 int ImmIdx = TII->getOperandIdx(Opcode, R600::OpName::literal);
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002266 if (HasDst) {
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002267 SelIdx--;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002268 ImmIdx--;
2269 }
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002270 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002271 SDValue &Imm = Ops[ImmIdx];
2272 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, Imm, DAG))
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002273 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2274 }
2275 }
2276
2277 return Node;
2278}