blob: 8da15a2d62341b2f3916c43bb49926d10284a6e8 [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- R600ISelLowering.cpp - R600 DAG Lowering Implementation -----------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Tom Stellard75aadc22012-12-11 21:25:42 +00006//
7//===----------------------------------------------------------------------===//
8//
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// Custom DAG lowering for R600
Tom Stellard75aadc22012-12-11 21:25:42 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "R600ISelLowering.h"
Tom Stellard2e59a452014-06-13 01:32:00 +000015#include "AMDGPUFrameLowering.h"
16#include "AMDGPUSubtarget.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000017#include "R600Defines.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000018#include "R600FrameLowering.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000019#include "R600InstrInfo.h"
20#include "R600MachineFunctionInfo.h"
Tom Stellard44b30b42018-05-22 02:03:23 +000021#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000022#include "Utils/AMDGPUBaseInfo.h"
23#include "llvm/ADT/APFloat.h"
24#include "llvm/ADT/APInt.h"
25#include "llvm/ADT/ArrayRef.h"
26#include "llvm/ADT/DenseMap.h"
27#include "llvm/ADT/SmallVector.h"
Tom Stellardacfeebf2013-07-23 01:48:05 +000028#include "llvm/CodeGen/CallingConvLower.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000029#include "llvm/CodeGen/DAGCombine.h"
30#include "llvm/CodeGen/ISDOpcodes.h"
31#include "llvm/CodeGen/MachineBasicBlock.h"
32#include "llvm/CodeGen/MachineFunction.h"
33#include "llvm/CodeGen/MachineInstr.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000034#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000035#include "llvm/CodeGen/MachineMemOperand.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
37#include "llvm/CodeGen/SelectionDAG.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000038#include "llvm/IR/Constants.h"
39#include "llvm/IR/DerivedTypes.h"
40#include "llvm/Support/Casting.h"
41#include "llvm/Support/Compiler.h"
42#include "llvm/Support/ErrorHandling.h"
David Blaikie13e77db2018-03-23 23:58:25 +000043#include "llvm/Support/MachineValueType.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000044#include <cassert>
45#include <cstdint>
46#include <iterator>
47#include <utility>
48#include <vector>
Tom Stellard75aadc22012-12-11 21:25:42 +000049
50using namespace llvm;
51
Tom Stellardc5a154d2018-06-28 23:47:12 +000052#include "R600GenCallingConv.inc"
53
Matt Arsenault43e92fe2016-06-24 06:30:11 +000054R600TargetLowering::R600TargetLowering(const TargetMachine &TM,
55 const R600Subtarget &STI)
Tom Stellardc5a154d2018-06-28 23:47:12 +000056 : AMDGPUTargetLowering(TM, STI), Subtarget(&STI), Gen(STI.getGeneration()) {
57 addRegisterClass(MVT::f32, &R600::R600_Reg32RegClass);
58 addRegisterClass(MVT::i32, &R600::R600_Reg32RegClass);
59 addRegisterClass(MVT::v2f32, &R600::R600_Reg64RegClass);
60 addRegisterClass(MVT::v2i32, &R600::R600_Reg64RegClass);
61 addRegisterClass(MVT::v4f32, &R600::R600_Reg128RegClass);
62 addRegisterClass(MVT::v4i32, &R600::R600_Reg128RegClass);
Tom Stellard0344cdf2013-08-01 15:23:42 +000063
Tom Stellardc5a154d2018-06-28 23:47:12 +000064 computeRegisterProperties(Subtarget->getRegisterInfo());
Tom Stellard75aadc22012-12-11 21:25:42 +000065
Matt Arsenault71e66762016-05-21 02:27:49 +000066 // Legalize loads and stores to the private address space.
67 setOperationAction(ISD::LOAD, MVT::i32, Custom);
68 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
69 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
70
71 // EXTLOAD should be the same as ZEXTLOAD. It is legal for some address
72 // spaces, so it is custom lowered to handle those where it isn't.
73 for (MVT VT : MVT::integer_valuetypes()) {
74 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
75 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Custom);
76 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Custom);
77
78 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
79 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Custom);
80 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Custom);
81
82 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
83 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Custom);
84 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Custom);
85 }
86
Matt Arsenaultd1097a32016-06-02 19:54:26 +000087 // Workaround for LegalizeDAG asserting on expansion of i1 vector loads.
88 setLoadExtAction(ISD::EXTLOAD, MVT::v2i32, MVT::v2i1, Expand);
89 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i32, MVT::v2i1, Expand);
90 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i32, MVT::v2i1, Expand);
91
92 setLoadExtAction(ISD::EXTLOAD, MVT::v4i32, MVT::v4i1, Expand);
93 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i32, MVT::v4i1, Expand);
94 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i32, MVT::v4i1, Expand);
95
Matt Arsenault71e66762016-05-21 02:27:49 +000096 setOperationAction(ISD::STORE, MVT::i8, Custom);
97 setOperationAction(ISD::STORE, MVT::i32, Custom);
98 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
99 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
100
101 setTruncStoreAction(MVT::i32, MVT::i8, Custom);
102 setTruncStoreAction(MVT::i32, MVT::i16, Custom);
Jan Vesely06200bd2017-01-06 21:00:46 +0000103 // We need to include these since trunc STORES to PRIVATE need
104 // special handling to accommodate RMW
105 setTruncStoreAction(MVT::v2i32, MVT::v2i16, Custom);
106 setTruncStoreAction(MVT::v4i32, MVT::v4i16, Custom);
107 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Custom);
108 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Custom);
109 setTruncStoreAction(MVT::v32i32, MVT::v32i16, Custom);
110 setTruncStoreAction(MVT::v2i32, MVT::v2i8, Custom);
111 setTruncStoreAction(MVT::v4i32, MVT::v4i8, Custom);
112 setTruncStoreAction(MVT::v8i32, MVT::v8i8, Custom);
113 setTruncStoreAction(MVT::v16i32, MVT::v16i8, Custom);
114 setTruncStoreAction(MVT::v32i32, MVT::v32i8, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +0000115
Matt Arsenaultd1097a32016-06-02 19:54:26 +0000116 // Workaround for LegalizeDAG asserting on expansion of i1 vector stores.
117 setTruncStoreAction(MVT::v2i32, MVT::v2i1, Expand);
118 setTruncStoreAction(MVT::v4i32, MVT::v4i1, Expand);
119
Tom Stellard0351ea22013-09-28 02:50:50 +0000120 // Set condition code actions
121 setCondCodeAction(ISD::SETO, MVT::f32, Expand);
122 setCondCodeAction(ISD::SETUO, MVT::f32, Expand);
Tom Stellardcd428182013-09-28 02:50:38 +0000123 setCondCodeAction(ISD::SETLT, MVT::f32, Expand);
Tom Stellard0351ea22013-09-28 02:50:50 +0000124 setCondCodeAction(ISD::SETLE, MVT::f32, Expand);
Tom Stellardcd428182013-09-28 02:50:38 +0000125 setCondCodeAction(ISD::SETOLT, MVT::f32, Expand);
126 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
Tom Stellard0351ea22013-09-28 02:50:50 +0000127 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
128 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
129 setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
130 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
Tom Stellardcd428182013-09-28 02:50:38 +0000131 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
132 setCondCodeAction(ISD::SETULE, MVT::f32, Expand);
133
134 setCondCodeAction(ISD::SETLE, MVT::i32, Expand);
135 setCondCodeAction(ISD::SETLT, MVT::i32, Expand);
136 setCondCodeAction(ISD::SETULE, MVT::i32, Expand);
137 setCondCodeAction(ISD::SETULT, MVT::i32, Expand);
138
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000139 setOperationAction(ISD::FCOS, MVT::f32, Custom);
140 setOperationAction(ISD::FSIN, MVT::f32, Custom);
141
Tom Stellard75aadc22012-12-11 21:25:42 +0000142 setOperationAction(ISD::SETCC, MVT::v4i32, Expand);
Tom Stellard0344cdf2013-08-01 15:23:42 +0000143 setOperationAction(ISD::SETCC, MVT::v2i32, Expand);
Tom Stellard75aadc22012-12-11 21:25:42 +0000144
Tom Stellard492ebea2013-03-08 15:37:07 +0000145 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
146 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
Matt Arsenault1d555c42014-06-23 18:00:55 +0000147 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Tom Stellard75aadc22012-12-11 21:25:42 +0000148
149 setOperationAction(ISD::FSUB, MVT::f32, Expand);
150
Tom Stellardc5a154d2018-06-28 23:47:12 +0000151 setOperationAction(ISD::FCEIL, MVT::f64, Custom);
152 setOperationAction(ISD::FTRUNC, MVT::f64, Custom);
153 setOperationAction(ISD::FRINT, MVT::f64, Custom);
154 setOperationAction(ISD::FFLOOR, MVT::f64, Custom);
155
Tom Stellard75aadc22012-12-11 21:25:42 +0000156 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
157 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
158
Tom Stellarde8f9f282013-03-08 15:37:05 +0000159 setOperationAction(ISD::SETCC, MVT::i32, Expand);
160 setOperationAction(ISD::SETCC, MVT::f32, Expand);
Tom Stellard75aadc22012-12-11 21:25:42 +0000161 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Custom);
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000162 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Custom);
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000163 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
164 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
Tom Stellard75aadc22012-12-11 21:25:42 +0000165
Tom Stellard53f2f902013-09-05 18:38:03 +0000166 setOperationAction(ISD::SELECT, MVT::i32, Expand);
167 setOperationAction(ISD::SELECT, MVT::f32, Expand);
168 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
Tom Stellard53f2f902013-09-05 18:38:03 +0000169 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
Tom Stellard75aadc22012-12-11 21:25:42 +0000170
Jan Vesely808fff52015-04-30 17:15:56 +0000171 // ADD, SUB overflow.
172 // TODO: turn these into Legal?
173 if (Subtarget->hasCARRY())
174 setOperationAction(ISD::UADDO, MVT::i32, Custom);
175
176 if (Subtarget->hasBORROW())
177 setOperationAction(ISD::USUBO, MVT::i32, Custom);
178
Matt Arsenault4e466652014-04-16 01:41:30 +0000179 // Expand sign extension of vectors
180 if (!Subtarget->hasBFE())
181 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
182
183 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Expand);
184 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Expand);
185
186 if (!Subtarget->hasBFE())
187 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
188 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Expand);
189 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Expand);
190
191 if (!Subtarget->hasBFE())
192 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
193 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Expand);
194 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Expand);
195
196 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
197 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Expand);
198 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i32, Expand);
199
200 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Expand);
201
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000202 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
203
Tom Stellard880a80a2014-06-17 16:53:14 +0000204 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i32, Custom);
205 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f32, Custom);
206 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
207 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
208
209 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i32, Custom);
210 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f32, Custom);
211 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
212 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
213
Jan Vesely25f36272014-06-18 12:27:13 +0000214 // We don't have 64-bit shifts. Thus we need either SHX i64 or SHX_PARTS i32
215 // to be Legal/Custom in order to avoid library calls.
216 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000217 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Jan Veselyecf51332014-06-18 12:27:17 +0000218 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jan Vesely25f36272014-06-18 12:27:13 +0000219
Jan Vesely39aeab42017-12-04 23:07:28 +0000220 if (!Subtarget->hasFMA()) {
221 setOperationAction(ISD::FMA, MVT::f32, Expand);
222 setOperationAction(ISD::FMA, MVT::f64, Expand);
223 }
Matt Arsenault4bec7d42018-07-20 09:05:08 +0000224
Tom Stellardc5a154d2018-06-28 23:47:12 +0000225 // FIXME: This was moved from AMDGPUTargetLowering, I'm not sure if we
226 // need it for R600.
227 if (!Subtarget->hasFP32Denormals())
228 setOperationAction(ISD::FMAD, MVT::f32, Legal);
229
230 if (!Subtarget->hasBFI()) {
231 // fcopysign can be done in a single instruction with BFI.
232 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
233 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
234 }
235
236 if (!Subtarget->hasBCNT(32))
237 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
238
239 if (!Subtarget->hasBCNT(64))
240 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
241
242 if (Subtarget->hasFFBH())
243 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Custom);
244
245 if (Subtarget->hasFFBL())
246 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Custom);
247
248 // FIXME: This was moved from AMDGPUTargetLowering, I'm not sure if we
249 // need it for R600.
250 if (Subtarget->hasBFE())
251 setHasExtractBitsInsn(true);
Jan Vesely39aeab42017-12-04 23:07:28 +0000252
Michel Danzer49812b52013-07-10 16:37:07 +0000253 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
254
Matt Arsenaultc4d3d3a2014-06-23 18:00:49 +0000255 const MVT ScalarIntVTs[] = { MVT::i32, MVT::i64 };
256 for (MVT VT : ScalarIntVTs) {
257 setOperationAction(ISD::ADDC, VT, Expand);
258 setOperationAction(ISD::SUBC, VT, Expand);
259 setOperationAction(ISD::ADDE, VT, Expand);
260 setOperationAction(ISD::SUBE, VT, Expand);
261 }
262
Jan Vesely334f51a2017-01-16 21:20:13 +0000263 // LLVM will expand these to atomic_cmp_swap(0)
264 // and atomic_swap, respectively.
265 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
266 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
267
Matt Arsenault754dd3e2017-04-03 18:08:08 +0000268 // We need to custom lower some of the intrinsics
269 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
270 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
271
Tom Stellardfc455472013-08-12 22:33:21 +0000272 setSchedulingPreference(Sched::Source);
Matt Arsenault71e66762016-05-21 02:27:49 +0000273
Matt Arsenault71e66762016-05-21 02:27:49 +0000274 setTargetDAGCombine(ISD::FP_ROUND);
275 setTargetDAGCombine(ISD::FP_TO_SINT);
276 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
277 setTargetDAGCombine(ISD::SELECT_CC);
278 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
Jan Vesely38814fa2016-08-27 19:09:43 +0000279 setTargetDAGCombine(ISD::LOAD);
Tom Stellard75aadc22012-12-11 21:25:42 +0000280}
281
Tom Stellardc0f0fba2015-10-01 17:51:29 +0000282static inline bool isEOP(MachineBasicBlock::iterator I) {
Hans Wennborg0dd9ed12016-08-13 01:12:49 +0000283 if (std::next(I) == I->getParent()->end())
284 return false;
Tom Stellardc5a154d2018-06-28 23:47:12 +0000285 return std::next(I)->getOpcode() == R600::RETURN;
Tom Stellardc0f0fba2015-10-01 17:51:29 +0000286}
287
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000288MachineBasicBlock *
289R600TargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
290 MachineBasicBlock *BB) const {
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000291 MachineFunction *MF = BB->getParent();
Tom Stellard75aadc22012-12-11 21:25:42 +0000292 MachineRegisterInfo &MRI = MF->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000293 MachineBasicBlock::iterator I = MI;
Tom Stellardc5a154d2018-06-28 23:47:12 +0000294 const R600InstrInfo *TII = Subtarget->getInstrInfo();
Tom Stellard75aadc22012-12-11 21:25:42 +0000295
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000296 switch (MI.getOpcode()) {
Tom Stellardc6f4a292013-08-26 15:05:59 +0000297 default:
Tom Stellard8f9fc202013-11-15 00:12:45 +0000298 // Replace LDS_*_RET instruction that don't have any uses with the
299 // equivalent LDS_*_NORET instruction.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000300 if (TII->isLDSRetInstr(MI.getOpcode())) {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000301 int DstIdx = TII->getOperandIdx(MI.getOpcode(), R600::OpName::dst);
Tom Stellard13c68ef2013-09-05 18:38:09 +0000302 assert(DstIdx != -1);
303 MachineInstrBuilder NewMI;
Aaron Watry1885e532014-09-11 15:02:54 +0000304 // FIXME: getLDSNoRetOp method only handles LDS_1A1D LDS ops. Add
305 // LDS_1A2D support and remove this special case.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000306 if (!MRI.use_empty(MI.getOperand(DstIdx).getReg()) ||
Tom Stellardc5a154d2018-06-28 23:47:12 +0000307 MI.getOpcode() == R600::LDS_CMPST_RET)
Tom Stellard8f9fc202013-11-15 00:12:45 +0000308 return BB;
309
310 NewMI = BuildMI(*BB, I, BB->findDebugLoc(I),
Tom Stellardc5a154d2018-06-28 23:47:12 +0000311 TII->get(R600::getLDSNoRetOp(MI.getOpcode())));
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000312 for (unsigned i = 1, e = MI.getNumOperands(); i < e; ++i) {
Diana Picus116bbab2017-01-13 09:58:52 +0000313 NewMI.add(MI.getOperand(i));
Tom Stellardc6f4a292013-08-26 15:05:59 +0000314 }
Tom Stellardc6f4a292013-08-26 15:05:59 +0000315 } else {
316 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
317 }
318 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000319
Tom Stellardc5a154d2018-06-28 23:47:12 +0000320 case R600::FABS_R600: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000321 MachineInstr *NewMI = TII->buildDefaultInstruction(
Tom Stellardc5a154d2018-06-28 23:47:12 +0000322 *BB, I, R600::MOV, MI.getOperand(0).getReg(),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000323 MI.getOperand(1).getReg());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000324 TII->addFlag(*NewMI, 0, MO_FLAG_ABS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000325 break;
326 }
327
Tom Stellardc5a154d2018-06-28 23:47:12 +0000328 case R600::FNEG_R600: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000329 MachineInstr *NewMI = TII->buildDefaultInstruction(
Tom Stellardc5a154d2018-06-28 23:47:12 +0000330 *BB, I, R600::MOV, MI.getOperand(0).getReg(),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000331 MI.getOperand(1).getReg());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000332 TII->addFlag(*NewMI, 0, MO_FLAG_NEG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000333 break;
334 }
335
Tom Stellardc5a154d2018-06-28 23:47:12 +0000336 case R600::MASK_WRITE: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000337 unsigned maskedRegister = MI.getOperand(0).getReg();
Tom Stellard75aadc22012-12-11 21:25:42 +0000338 assert(TargetRegisterInfo::isVirtualRegister(maskedRegister));
339 MachineInstr * defInstr = MRI.getVRegDef(maskedRegister);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000340 TII->addFlag(*defInstr, 0, MO_FLAG_MASK);
Tom Stellard75aadc22012-12-11 21:25:42 +0000341 break;
342 }
343
Tom Stellardc5a154d2018-06-28 23:47:12 +0000344 case R600::MOV_IMM_F32:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000345 TII->buildMovImm(*BB, I, MI.getOperand(0).getReg(), MI.getOperand(1)
346 .getFPImm()
347 ->getValueAPF()
348 .bitcastToAPInt()
349 .getZExtValue());
Tom Stellard75aadc22012-12-11 21:25:42 +0000350 break;
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000351
Tom Stellardc5a154d2018-06-28 23:47:12 +0000352 case R600::MOV_IMM_I32:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000353 TII->buildMovImm(*BB, I, MI.getOperand(0).getReg(),
354 MI.getOperand(1).getImm());
Tom Stellard75aadc22012-12-11 21:25:42 +0000355 break;
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000356
Tom Stellardc5a154d2018-06-28 23:47:12 +0000357 case R600::MOV_IMM_GLOBAL_ADDR: {
Jan Veselyf97de002016-05-13 20:39:29 +0000358 //TODO: Perhaps combine this instruction with the next if possible
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000359 auto MIB = TII->buildDefaultInstruction(
Tom Stellardc5a154d2018-06-28 23:47:12 +0000360 *BB, MI, R600::MOV, MI.getOperand(0).getReg(), R600::ALU_LITERAL_X);
361 int Idx = TII->getOperandIdx(*MIB, R600::OpName::literal);
Jan Veselyf97de002016-05-13 20:39:29 +0000362 //TODO: Ugh this is rather ugly
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000363 MIB->getOperand(Idx) = MI.getOperand(1);
Jan Veselyf97de002016-05-13 20:39:29 +0000364 break;
365 }
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000366
Tom Stellardc5a154d2018-06-28 23:47:12 +0000367 case R600::CONST_COPY: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000368 MachineInstr *NewMI = TII->buildDefaultInstruction(
Tom Stellardc5a154d2018-06-28 23:47:12 +0000369 *BB, MI, R600::MOV, MI.getOperand(0).getReg(), R600::ALU_CONST);
370 TII->setImmOperand(*NewMI, R600::OpName::src0_sel,
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000371 MI.getOperand(1).getImm());
Vincent Lejeune0b72f102013-03-05 15:04:55 +0000372 break;
373 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000374
Tom Stellardc5a154d2018-06-28 23:47:12 +0000375 case R600::RAT_WRITE_CACHELESS_32_eg:
376 case R600::RAT_WRITE_CACHELESS_64_eg:
377 case R600::RAT_WRITE_CACHELESS_128_eg:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000378 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode()))
Diana Picus116bbab2017-01-13 09:58:52 +0000379 .add(MI.getOperand(0))
380 .add(MI.getOperand(1))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000381 .addImm(isEOP(I)); // Set End of program bit
Tom Stellard75aadc22012-12-11 21:25:42 +0000382 break;
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000383
Tom Stellardc5a154d2018-06-28 23:47:12 +0000384 case R600::RAT_STORE_TYPED_eg:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000385 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode()))
Diana Picus116bbab2017-01-13 09:58:52 +0000386 .add(MI.getOperand(0))
387 .add(MI.getOperand(1))
388 .add(MI.getOperand(2))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000389 .addImm(isEOP(I)); // Set End of program bit
Tom Stellarde0e582c2015-10-01 17:51:34 +0000390 break;
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000391
Tom Stellardc5a154d2018-06-28 23:47:12 +0000392 case R600::BRANCH:
393 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(R600::JUMP))
Diana Picus116bbab2017-01-13 09:58:52 +0000394 .add(MI.getOperand(0));
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000395 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000396
Tom Stellardc5a154d2018-06-28 23:47:12 +0000397 case R600::BRANCH_COND_f32: {
Tom Stellard75aadc22012-12-11 21:25:42 +0000398 MachineInstr *NewMI =
Tom Stellardc5a154d2018-06-28 23:47:12 +0000399 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(R600::PRED_X),
400 R600::PREDICATE_BIT)
Diana Picus116bbab2017-01-13 09:58:52 +0000401 .add(MI.getOperand(1))
Tom Stellardc5a154d2018-06-28 23:47:12 +0000402 .addImm(R600::PRED_SETNE)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000403 .addImm(0); // Flags
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000404 TII->addFlag(*NewMI, 0, MO_FLAG_PUSH);
Tom Stellardc5a154d2018-06-28 23:47:12 +0000405 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(R600::JUMP_COND))
Diana Picus116bbab2017-01-13 09:58:52 +0000406 .add(MI.getOperand(0))
Tom Stellardc5a154d2018-06-28 23:47:12 +0000407 .addReg(R600::PREDICATE_BIT, RegState::Kill);
Tom Stellard75aadc22012-12-11 21:25:42 +0000408 break;
409 }
410
Tom Stellardc5a154d2018-06-28 23:47:12 +0000411 case R600::BRANCH_COND_i32: {
Tom Stellard75aadc22012-12-11 21:25:42 +0000412 MachineInstr *NewMI =
Tom Stellardc5a154d2018-06-28 23:47:12 +0000413 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(R600::PRED_X),
414 R600::PREDICATE_BIT)
Diana Picus116bbab2017-01-13 09:58:52 +0000415 .add(MI.getOperand(1))
Tom Stellardc5a154d2018-06-28 23:47:12 +0000416 .addImm(R600::PRED_SETNE_INT)
Tom Stellard75aadc22012-12-11 21:25:42 +0000417 .addImm(0); // Flags
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000418 TII->addFlag(*NewMI, 0, MO_FLAG_PUSH);
Tom Stellardc5a154d2018-06-28 23:47:12 +0000419 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(R600::JUMP_COND))
Diana Picus116bbab2017-01-13 09:58:52 +0000420 .add(MI.getOperand(0))
Tom Stellardc5a154d2018-06-28 23:47:12 +0000421 .addReg(R600::PREDICATE_BIT, RegState::Kill);
Tom Stellard75aadc22012-12-11 21:25:42 +0000422 break;
423 }
424
Tom Stellardc5a154d2018-06-28 23:47:12 +0000425 case R600::EG_ExportSwz:
426 case R600::R600_ExportSwz: {
Tom Stellard6f1b8652013-01-23 21:39:49 +0000427 // Instruction is left unmodified if its not the last one of its type
428 bool isLastInstructionOfItsType = true;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000429 unsigned InstExportType = MI.getOperand(1).getImm();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000430 for (MachineBasicBlock::iterator NextExportInst = std::next(I),
Tom Stellard6f1b8652013-01-23 21:39:49 +0000431 EndBlock = BB->end(); NextExportInst != EndBlock;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000432 NextExportInst = std::next(NextExportInst)) {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000433 if (NextExportInst->getOpcode() == R600::EG_ExportSwz ||
434 NextExportInst->getOpcode() == R600::R600_ExportSwz) {
Tom Stellard6f1b8652013-01-23 21:39:49 +0000435 unsigned CurrentInstExportType = NextExportInst->getOperand(1)
436 .getImm();
437 if (CurrentInstExportType == InstExportType) {
438 isLastInstructionOfItsType = false;
439 break;
440 }
441 }
442 }
Tom Stellardc0f0fba2015-10-01 17:51:29 +0000443 bool EOP = isEOP(I);
Tom Stellard6f1b8652013-01-23 21:39:49 +0000444 if (!EOP && !isLastInstructionOfItsType)
Tom Stellard75aadc22012-12-11 21:25:42 +0000445 return BB;
Tom Stellardc5a154d2018-06-28 23:47:12 +0000446 unsigned CfInst = (MI.getOpcode() == R600::EG_ExportSwz) ? 84 : 40;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000447 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode()))
Diana Picus116bbab2017-01-13 09:58:52 +0000448 .add(MI.getOperand(0))
449 .add(MI.getOperand(1))
450 .add(MI.getOperand(2))
451 .add(MI.getOperand(3))
452 .add(MI.getOperand(4))
453 .add(MI.getOperand(5))
454 .add(MI.getOperand(6))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000455 .addImm(CfInst)
456 .addImm(EOP);
Tom Stellard75aadc22012-12-11 21:25:42 +0000457 break;
458 }
Tom Stellardc5a154d2018-06-28 23:47:12 +0000459 case R600::RETURN: {
Jakob Stoklund Olesenfdc37672013-02-05 17:53:52 +0000460 return BB;
461 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000462 }
463
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000464 MI.eraseFromParent();
Tom Stellard75aadc22012-12-11 21:25:42 +0000465 return BB;
466}
467
468//===----------------------------------------------------------------------===//
469// Custom DAG Lowering Operations
470//===----------------------------------------------------------------------===//
471
Tom Stellard75aadc22012-12-11 21:25:42 +0000472SDValue R600TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Tom Stellardc026e8b2013-06-28 15:47:08 +0000473 MachineFunction &MF = DAG.getMachineFunction();
474 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
Tom Stellard75aadc22012-12-11 21:25:42 +0000475 switch (Op.getOpcode()) {
476 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellard880a80a2014-06-17 16:53:14 +0000477 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
478 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Jan Vesely25f36272014-06-18 12:27:13 +0000479 case ISD::SHL_PARTS: return LowerSHLParts(Op, DAG);
Jan Veselyecf51332014-06-18 12:27:17 +0000480 case ISD::SRA_PARTS:
Jan Vesely900ff2e2014-06-18 12:27:15 +0000481 case ISD::SRL_PARTS: return LowerSRXParts(Op, DAG);
Jan Vesely808fff52015-04-30 17:15:56 +0000482 case ISD::UADDO: return LowerUADDSUBO(Op, DAG, ISD::ADD, AMDGPUISD::CARRY);
483 case ISD::USUBO: return LowerUADDSUBO(Op, DAG, ISD::SUB, AMDGPUISD::BORROW);
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000484 case ISD::FCOS:
485 case ISD::FSIN: return LowerTrig(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000486 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000487 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000488 case ISD::LOAD: {
489 SDValue Result = LowerLOAD(Op, DAG);
490 assert((!Result.getNode() ||
491 Result.getNode()->getNumValues() == 2) &&
492 "Load should return a value and a chain");
493 return Result;
494 }
495
Matt Arsenault1d555c42014-06-23 18:00:55 +0000496 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellardc026e8b2013-06-28 15:47:08 +0000497 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
Matt Arsenault81d06012016-03-07 21:10:13 +0000498 case ISD::FrameIndex: return lowerFrameIndex(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000499 case ISD::INTRINSIC_VOID: {
500 SDValue Chain = Op.getOperand(0);
501 unsigned IntrinsicID =
502 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
503 switch (IntrinsicID) {
Tom Stellarde4377882018-06-01 02:19:46 +0000504 case Intrinsic::r600_store_swizzle: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000505 SDLoc DL(Op);
Vincent Lejeuned80bc152013-02-14 16:55:06 +0000506 const SDValue Args[8] = {
507 Chain,
508 Op.getOperand(2), // Export Value
509 Op.getOperand(3), // ArrayBase
510 Op.getOperand(4), // Type
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000511 DAG.getConstant(0, DL, MVT::i32), // SWZ_X
512 DAG.getConstant(1, DL, MVT::i32), // SWZ_Y
513 DAG.getConstant(2, DL, MVT::i32), // SWZ_Z
514 DAG.getConstant(3, DL, MVT::i32) // SWZ_W
Vincent Lejeuned80bc152013-02-14 16:55:06 +0000515 };
Matt Arsenault7bee6ac2016-12-05 20:23:10 +0000516 return DAG.getNode(AMDGPUISD::R600_EXPORT, DL, Op.getValueType(), Args);
Tom Stellard75aadc22012-12-11 21:25:42 +0000517 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000518
Tom Stellard75aadc22012-12-11 21:25:42 +0000519 // default for switch(IntrinsicID)
520 default: break;
521 }
522 // break out of case ISD::INTRINSIC_VOID in switch(Op.getOpcode())
523 break;
524 }
525 case ISD::INTRINSIC_WO_CHAIN: {
526 unsigned IntrinsicID =
527 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
528 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000529 SDLoc DL(Op);
Matt Arsenault754dd3e2017-04-03 18:08:08 +0000530 switch (IntrinsicID) {
Tom Stellarde4377882018-06-01 02:19:46 +0000531 case Intrinsic::r600_tex:
532 case Intrinsic::r600_texc: {
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000533 unsigned TextureOp;
534 switch (IntrinsicID) {
Tom Stellarde4377882018-06-01 02:19:46 +0000535 case Intrinsic::r600_tex:
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000536 TextureOp = 0;
537 break;
Tom Stellarde4377882018-06-01 02:19:46 +0000538 case Intrinsic::r600_texc:
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000539 TextureOp = 1;
540 break;
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000541 default:
Matt Arsenault60a750f2016-07-26 21:03:38 +0000542 llvm_unreachable("unhandled texture operation");
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000543 }
544
545 SDValue TexArgs[19] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000546 DAG.getConstant(TextureOp, DL, MVT::i32),
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000547 Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000548 DAG.getConstant(0, DL, MVT::i32),
549 DAG.getConstant(1, DL, MVT::i32),
550 DAG.getConstant(2, DL, MVT::i32),
551 DAG.getConstant(3, DL, MVT::i32),
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000552 Op.getOperand(2),
553 Op.getOperand(3),
554 Op.getOperand(4),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000555 DAG.getConstant(0, DL, MVT::i32),
556 DAG.getConstant(1, DL, MVT::i32),
557 DAG.getConstant(2, DL, MVT::i32),
558 DAG.getConstant(3, DL, MVT::i32),
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000559 Op.getOperand(5),
560 Op.getOperand(6),
561 Op.getOperand(7),
562 Op.getOperand(8),
563 Op.getOperand(9),
564 Op.getOperand(10)
565 };
Craig Topper48d114b2014-04-26 18:35:24 +0000566 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, MVT::v4f32, TexArgs);
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000567 }
Tom Stellarde4377882018-06-01 02:19:46 +0000568 case Intrinsic::r600_dot4: {
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000569 SDValue Args[8] = {
570 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000571 DAG.getConstant(0, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000572 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000573 DAG.getConstant(0, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000574 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000575 DAG.getConstant(1, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000576 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000577 DAG.getConstant(1, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000578 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000579 DAG.getConstant(2, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000580 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000581 DAG.getConstant(2, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000582 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000583 DAG.getConstant(3, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000584 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000585 DAG.getConstant(3, DL, MVT::i32))
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000586 };
Craig Topper48d114b2014-04-26 18:35:24 +0000587 return DAG.getNode(AMDGPUISD::DOT4, DL, MVT::f32, Args);
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000588 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000589
Jan Vesely2fa28c32016-07-10 21:20:29 +0000590 case Intrinsic::r600_implicitarg_ptr: {
Matt Arsenault0da63502018-08-31 05:49:54 +0000591 MVT PtrVT = getPointerTy(DAG.getDataLayout(), AMDGPUAS::PARAM_I_ADDRESS);
Matt Arsenault75e71922018-06-28 10:18:55 +0000592 uint32_t ByteOffset = getImplicitParameterOffset(MF, FIRST_IMPLICIT);
Jan Vesely2fa28c32016-07-10 21:20:29 +0000593 return DAG.getConstant(ByteOffset, DL, PtrVT);
594 }
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000595 case Intrinsic::r600_read_ngroups_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000596 return LowerImplicitParameter(DAG, VT, DL, 0);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000597 case Intrinsic::r600_read_ngroups_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000598 return LowerImplicitParameter(DAG, VT, DL, 1);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000599 case Intrinsic::r600_read_ngroups_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000600 return LowerImplicitParameter(DAG, VT, DL, 2);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000601 case Intrinsic::r600_read_global_size_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000602 return LowerImplicitParameter(DAG, VT, DL, 3);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000603 case Intrinsic::r600_read_global_size_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000604 return LowerImplicitParameter(DAG, VT, DL, 4);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000605 case Intrinsic::r600_read_global_size_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000606 return LowerImplicitParameter(DAG, VT, DL, 5);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000607 case Intrinsic::r600_read_local_size_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000608 return LowerImplicitParameter(DAG, VT, DL, 6);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000609 case Intrinsic::r600_read_local_size_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000610 return LowerImplicitParameter(DAG, VT, DL, 7);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000611 case Intrinsic::r600_read_local_size_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000612 return LowerImplicitParameter(DAG, VT, DL, 8);
613
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000614 case Intrinsic::r600_read_tgid_x:
Tom Stellardc5a154d2018-06-28 23:47:12 +0000615 return CreateLiveInRegisterRaw(DAG, &R600::R600_TReg32RegClass,
616 R600::T1_X, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000617 case Intrinsic::r600_read_tgid_y:
Tom Stellardc5a154d2018-06-28 23:47:12 +0000618 return CreateLiveInRegisterRaw(DAG, &R600::R600_TReg32RegClass,
619 R600::T1_Y, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000620 case Intrinsic::r600_read_tgid_z:
Tom Stellardc5a154d2018-06-28 23:47:12 +0000621 return CreateLiveInRegisterRaw(DAG, &R600::R600_TReg32RegClass,
622 R600::T1_Z, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000623 case Intrinsic::r600_read_tidig_x:
Tom Stellardc5a154d2018-06-28 23:47:12 +0000624 return CreateLiveInRegisterRaw(DAG, &R600::R600_TReg32RegClass,
625 R600::T0_X, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000626 case Intrinsic::r600_read_tidig_y:
Tom Stellardc5a154d2018-06-28 23:47:12 +0000627 return CreateLiveInRegisterRaw(DAG, &R600::R600_TReg32RegClass,
628 R600::T0_Y, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000629 case Intrinsic::r600_read_tidig_z:
Tom Stellardc5a154d2018-06-28 23:47:12 +0000630 return CreateLiveInRegisterRaw(DAG, &R600::R600_TReg32RegClass,
631 R600::T0_Z, VT);
Matt Arsenaultbef34e22016-01-22 21:30:34 +0000632
Matt Arsenault09b2c4a2016-07-15 21:26:52 +0000633 case Intrinsic::r600_recipsqrt_ieee:
634 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
Matt Arsenaultbef34e22016-01-22 21:30:34 +0000635
Matt Arsenault09b2c4a2016-07-15 21:26:52 +0000636 case Intrinsic::r600_recipsqrt_clamped:
637 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
Matt Arsenault754dd3e2017-04-03 18:08:08 +0000638 default:
639 return Op;
Tom Stellard75aadc22012-12-11 21:25:42 +0000640 }
Matt Arsenault09b2c4a2016-07-15 21:26:52 +0000641
Tom Stellard75aadc22012-12-11 21:25:42 +0000642 // break out of case ISD::INTRINSIC_WO_CHAIN in switch(Op.getOpcode())
643 break;
644 }
645 } // end switch(Op.getOpcode())
646 return SDValue();
647}
648
649void R600TargetLowering::ReplaceNodeResults(SDNode *N,
650 SmallVectorImpl<SDValue> &Results,
651 SelectionDAG &DAG) const {
652 switch (N->getOpcode()) {
Matt Arsenaultd125d742014-03-27 17:23:24 +0000653 default:
654 AMDGPUTargetLowering::ReplaceNodeResults(N, Results, DAG);
655 return;
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000656 case ISD::FP_TO_UINT:
657 if (N->getValueType(0) == MVT::i1) {
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000658 Results.push_back(lowerFP_TO_UINT(N->getOperand(0), DAG));
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000659 return;
660 }
Justin Bognerb03fd122016-08-17 05:10:15 +0000661 // Since we don't care about out of bounds values we can use FP_TO_SINT for
662 // uints too. The DAGLegalizer code for uint considers some extra cases
663 // which are not necessary here.
664 LLVM_FALLTHROUGH;
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000665 case ISD::FP_TO_SINT: {
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000666 if (N->getValueType(0) == MVT::i1) {
667 Results.push_back(lowerFP_TO_SINT(N->getOperand(0), DAG));
668 return;
669 }
670
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000671 SDValue Result;
672 if (expandFP_TO_SINT(N, Result, DAG))
673 Results.push_back(Result);
Tom Stellard365366f2013-01-23 02:09:06 +0000674 return;
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000675 }
Jan Vesely343cd6f02014-06-22 21:43:01 +0000676 case ISD::SDIVREM: {
677 SDValue Op = SDValue(N, 1);
678 SDValue RES = LowerSDIVREM(Op, DAG);
679 Results.push_back(RES);
680 Results.push_back(RES.getValue(1));
681 break;
682 }
683 case ISD::UDIVREM: {
684 SDValue Op = SDValue(N, 0);
Tom Stellardbf69d762014-11-15 01:07:53 +0000685 LowerUDIVREM64(Op, DAG, Results);
Jan Vesely343cd6f02014-06-22 21:43:01 +0000686 break;
687 }
688 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000689}
690
Tom Stellard880a80a2014-06-17 16:53:14 +0000691SDValue R600TargetLowering::vectorToVerticalVector(SelectionDAG &DAG,
692 SDValue Vector) const {
Tom Stellard880a80a2014-06-17 16:53:14 +0000693 SDLoc DL(Vector);
694 EVT VecVT = Vector.getValueType();
695 EVT EltVT = VecVT.getVectorElementType();
696 SmallVector<SDValue, 8> Args;
697
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000698 for (unsigned i = 0, e = VecVT.getVectorNumElements(); i != e; ++i) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000699 Args.push_back(DAG.getNode(
700 ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Vector,
701 DAG.getConstant(i, DL, getVectorIdxTy(DAG.getDataLayout()))));
Tom Stellard880a80a2014-06-17 16:53:14 +0000702 }
703
704 return DAG.getNode(AMDGPUISD::BUILD_VERTICAL_VECTOR, DL, VecVT, Args);
705}
706
707SDValue R600TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
708 SelectionDAG &DAG) const {
Tom Stellard880a80a2014-06-17 16:53:14 +0000709 SDLoc DL(Op);
710 SDValue Vector = Op.getOperand(0);
711 SDValue Index = Op.getOperand(1);
712
713 if (isa<ConstantSDNode>(Index) ||
714 Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR)
715 return Op;
716
717 Vector = vectorToVerticalVector(DAG, Vector);
718 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, Op.getValueType(),
719 Vector, Index);
720}
721
722SDValue R600TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
723 SelectionDAG &DAG) const {
724 SDLoc DL(Op);
725 SDValue Vector = Op.getOperand(0);
726 SDValue Value = Op.getOperand(1);
727 SDValue Index = Op.getOperand(2);
728
729 if (isa<ConstantSDNode>(Index) ||
730 Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR)
731 return Op;
732
733 Vector = vectorToVerticalVector(DAG, Vector);
734 SDValue Insert = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, Op.getValueType(),
735 Vector, Value, Index);
736 return vectorToVerticalVector(DAG, Insert);
737}
738
Tom Stellard27233b72016-05-02 18:05:17 +0000739SDValue R600TargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
740 SDValue Op,
741 SelectionDAG &DAG) const {
Tom Stellard27233b72016-05-02 18:05:17 +0000742 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
Matt Arsenault0da63502018-08-31 05:49:54 +0000743 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
Tom Stellard27233b72016-05-02 18:05:17 +0000744 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
745
746 const DataLayout &DL = DAG.getDataLayout();
747 const GlobalValue *GV = GSD->getGlobal();
Matt Arsenault0da63502018-08-31 05:49:54 +0000748 MVT ConstPtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
Tom Stellard27233b72016-05-02 18:05:17 +0000749
Jan Veselyf97de002016-05-13 20:39:29 +0000750 SDValue GA = DAG.getTargetGlobalAddress(GV, SDLoc(GSD), ConstPtrVT);
751 return DAG.getNode(AMDGPUISD::CONST_DATA_PTR, SDLoc(GSD), ConstPtrVT, GA);
Tom Stellard27233b72016-05-02 18:05:17 +0000752}
753
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000754SDValue R600TargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
755 // On hw >= R700, COS/SIN input must be between -1. and 1.
756 // Thus we lower them to TRIG ( FRACT ( x / 2Pi + 0.5) - 0.5)
757 EVT VT = Op.getValueType();
758 SDValue Arg = Op.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000759 SDLoc DL(Op);
Sanjay Patela2607012015-09-16 16:31:21 +0000760
761 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000762 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
763 DAG.getNode(ISD::FADD, DL, VT,
764 DAG.getNode(ISD::FMUL, DL, VT, Arg,
765 DAG.getConstantFP(0.15915494309, DL, MVT::f32)),
766 DAG.getConstantFP(0.5, DL, MVT::f32)));
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000767 unsigned TrigNode;
768 switch (Op.getOpcode()) {
769 case ISD::FCOS:
770 TrigNode = AMDGPUISD::COS_HW;
771 break;
772 case ISD::FSIN:
773 TrigNode = AMDGPUISD::SIN_HW;
774 break;
775 default:
776 llvm_unreachable("Wrong trig opcode");
777 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000778 SDValue TrigVal = DAG.getNode(TrigNode, DL, VT,
779 DAG.getNode(ISD::FADD, DL, VT, FractPart,
780 DAG.getConstantFP(-0.5, DL, MVT::f32)));
Tom Stellard5bfbae52018-07-11 20:59:01 +0000781 if (Gen >= AMDGPUSubtarget::R700)
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000782 return TrigVal;
783 // On R600 hw, COS/SIN input must be between -Pi and Pi.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000784 return DAG.getNode(ISD::FMUL, DL, VT, TrigVal,
785 DAG.getConstantFP(3.14159265359, DL, MVT::f32));
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000786}
787
Jan Vesely25f36272014-06-18 12:27:13 +0000788SDValue R600TargetLowering::LowerSHLParts(SDValue Op, SelectionDAG &DAG) const {
789 SDLoc DL(Op);
790 EVT VT = Op.getValueType();
791
792 SDValue Lo = Op.getOperand(0);
793 SDValue Hi = Op.getOperand(1);
794 SDValue Shift = Op.getOperand(2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000795 SDValue Zero = DAG.getConstant(0, DL, VT);
796 SDValue One = DAG.getConstant(1, DL, VT);
Jan Vesely25f36272014-06-18 12:27:13 +0000797
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000798 SDValue Width = DAG.getConstant(VT.getSizeInBits(), DL, VT);
799 SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, DL, VT);
Jan Vesely25f36272014-06-18 12:27:13 +0000800 SDValue BigShift = DAG.getNode(ISD::SUB, DL, VT, Shift, Width);
801 SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift);
802
803 // The dance around Width1 is necessary for 0 special case.
804 // Without it the CompShift might be 32, producing incorrect results in
805 // Overflow. So we do the shift in two steps, the alternative is to
806 // add a conditional to filter the special case.
807
808 SDValue Overflow = DAG.getNode(ISD::SRL, DL, VT, Lo, CompShift);
809 Overflow = DAG.getNode(ISD::SRL, DL, VT, Overflow, One);
810
811 SDValue HiSmall = DAG.getNode(ISD::SHL, DL, VT, Hi, Shift);
812 HiSmall = DAG.getNode(ISD::OR, DL, VT, HiSmall, Overflow);
813 SDValue LoSmall = DAG.getNode(ISD::SHL, DL, VT, Lo, Shift);
814
815 SDValue HiBig = DAG.getNode(ISD::SHL, DL, VT, Lo, BigShift);
816 SDValue LoBig = Zero;
817
818 Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT);
819 Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT);
820
821 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi);
822}
823
Jan Vesely900ff2e2014-06-18 12:27:15 +0000824SDValue R600TargetLowering::LowerSRXParts(SDValue Op, SelectionDAG &DAG) const {
825 SDLoc DL(Op);
826 EVT VT = Op.getValueType();
827
828 SDValue Lo = Op.getOperand(0);
829 SDValue Hi = Op.getOperand(1);
830 SDValue Shift = Op.getOperand(2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000831 SDValue Zero = DAG.getConstant(0, DL, VT);
832 SDValue One = DAG.getConstant(1, DL, VT);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000833
Jan Veselyecf51332014-06-18 12:27:17 +0000834 const bool SRA = Op.getOpcode() == ISD::SRA_PARTS;
835
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000836 SDValue Width = DAG.getConstant(VT.getSizeInBits(), DL, VT);
837 SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, DL, VT);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000838 SDValue BigShift = DAG.getNode(ISD::SUB, DL, VT, Shift, Width);
839 SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift);
840
841 // The dance around Width1 is necessary for 0 special case.
842 // Without it the CompShift might be 32, producing incorrect results in
843 // Overflow. So we do the shift in two steps, the alternative is to
844 // add a conditional to filter the special case.
845
846 SDValue Overflow = DAG.getNode(ISD::SHL, DL, VT, Hi, CompShift);
847 Overflow = DAG.getNode(ISD::SHL, DL, VT, Overflow, One);
848
Jan Veselyecf51332014-06-18 12:27:17 +0000849 SDValue HiSmall = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, Shift);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000850 SDValue LoSmall = DAG.getNode(ISD::SRL, DL, VT, Lo, Shift);
851 LoSmall = DAG.getNode(ISD::OR, DL, VT, LoSmall, Overflow);
852
Jan Veselyecf51332014-06-18 12:27:17 +0000853 SDValue LoBig = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, BigShift);
854 SDValue HiBig = SRA ? DAG.getNode(ISD::SRA, DL, VT, Hi, Width1) : Zero;
Jan Vesely900ff2e2014-06-18 12:27:15 +0000855
856 Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT);
857 Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT);
858
859 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi);
860}
861
Jan Vesely808fff52015-04-30 17:15:56 +0000862SDValue R600TargetLowering::LowerUADDSUBO(SDValue Op, SelectionDAG &DAG,
863 unsigned mainop, unsigned ovf) const {
864 SDLoc DL(Op);
865 EVT VT = Op.getValueType();
866
867 SDValue Lo = Op.getOperand(0);
868 SDValue Hi = Op.getOperand(1);
869
870 SDValue OVF = DAG.getNode(ovf, DL, VT, Lo, Hi);
871 // Extend sign.
872 OVF = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, OVF,
873 DAG.getValueType(MVT::i1));
874
875 SDValue Res = DAG.getNode(mainop, DL, VT, Lo, Hi);
876
877 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT, VT), Res, OVF);
878}
879
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000880SDValue R600TargetLowering::lowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000881 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000882 return DAG.getNode(
883 ISD::SETCC,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000884 DL,
Tom Stellard75aadc22012-12-11 21:25:42 +0000885 MVT::i1,
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000886 Op, DAG.getConstantFP(1.0f, DL, MVT::f32),
887 DAG.getCondCode(ISD::SETEQ));
888}
889
890SDValue R600TargetLowering::lowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const {
891 SDLoc DL(Op);
892 return DAG.getNode(
893 ISD::SETCC,
894 DL,
895 MVT::i1,
896 Op, DAG.getConstantFP(-1.0f, DL, MVT::f32),
897 DAG.getCondCode(ISD::SETEQ));
Tom Stellard75aadc22012-12-11 21:25:42 +0000898}
899
Tom Stellard75aadc22012-12-11 21:25:42 +0000900SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000901 const SDLoc &DL,
Tom Stellard75aadc22012-12-11 21:25:42 +0000902 unsigned DwordOffset) const {
903 unsigned ByteOffset = DwordOffset * 4;
904 PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
Matt Arsenault0da63502018-08-31 05:49:54 +0000905 AMDGPUAS::PARAM_I_ADDRESS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000906
907 // We shouldn't be using an offset wider than 16-bits for implicit parameters.
908 assert(isInt<16>(ByteOffset));
909
910 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000911 DAG.getConstant(ByteOffset, DL, MVT::i32), // PTR
Justin Lebar9c375812016-07-15 18:27:10 +0000912 MachinePointerInfo(ConstantPointerNull::get(PtrType)));
Tom Stellard75aadc22012-12-11 21:25:42 +0000913}
914
Tom Stellard75aadc22012-12-11 21:25:42 +0000915bool R600TargetLowering::isZero(SDValue Op) const {
916 if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) {
917 return Cst->isNullValue();
918 } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){
919 return CstFP->isZero();
920 } else {
921 return false;
922 }
923}
924
Matt Arsenault6b6a2c32016-03-11 08:00:27 +0000925bool R600TargetLowering::isHWTrueValue(SDValue Op) const {
926 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
927 return CFP->isExactlyValue(1.0);
928 }
929 return isAllOnesConstant(Op);
930}
931
932bool R600TargetLowering::isHWFalseValue(SDValue Op) const {
933 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
934 return CFP->getValueAPF().isZero();
935 }
936 return isNullConstant(Op);
937}
938
Tom Stellard75aadc22012-12-11 21:25:42 +0000939SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000940 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000941 EVT VT = Op.getValueType();
942
943 SDValue LHS = Op.getOperand(0);
944 SDValue RHS = Op.getOperand(1);
945 SDValue True = Op.getOperand(2);
946 SDValue False = Op.getOperand(3);
947 SDValue CC = Op.getOperand(4);
948 SDValue Temp;
949
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +0000950 if (VT == MVT::f32) {
951 DAGCombinerInfo DCI(DAG, AfterLegalizeVectorOps, true, nullptr);
Matt Arsenaultda7a6562017-02-01 00:42:40 +0000952 SDValue MinMax = combineFMinMaxLegacy(DL, VT, LHS, RHS, True, False, CC, DCI);
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +0000953 if (MinMax)
954 return MinMax;
955 }
956
Tom Stellard75aadc22012-12-11 21:25:42 +0000957 // LHS and RHS are guaranteed to be the same value type
958 EVT CompareVT = LHS.getValueType();
959
960 // Check if we can lower this to a native operation.
961
Tom Stellard2add82d2013-03-08 15:37:09 +0000962 // Try to lower to a SET* instruction:
963 //
964 // SET* can match the following patterns:
965 //
Tom Stellardcd428182013-09-28 02:50:38 +0000966 // select_cc f32, f32, -1, 0, cc_supported
967 // select_cc f32, f32, 1.0f, 0.0f, cc_supported
968 // select_cc i32, i32, -1, 0, cc_supported
Tom Stellard2add82d2013-03-08 15:37:09 +0000969 //
970
971 // Move hardware True/False values to the correct operand.
Tom Stellardcd428182013-09-28 02:50:38 +0000972 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
973 ISD::CondCode InverseCC =
974 ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
Tom Stellard5694d302013-09-28 02:50:43 +0000975 if (isHWTrueValue(False) && isHWFalseValue(True)) {
976 if (isCondCodeLegal(InverseCC, CompareVT.getSimpleVT())) {
977 std::swap(False, True);
978 CC = DAG.getCondCode(InverseCC);
979 } else {
980 ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InverseCC);
981 if (isCondCodeLegal(SwapInvCC, CompareVT.getSimpleVT())) {
982 std::swap(False, True);
983 std::swap(LHS, RHS);
984 CC = DAG.getCondCode(SwapInvCC);
985 }
986 }
Tom Stellard2add82d2013-03-08 15:37:09 +0000987 }
988
989 if (isHWTrueValue(True) && isHWFalseValue(False) &&
990 (CompareVT == VT || VT == MVT::i32)) {
991 // This can be matched by a SET* instruction.
992 return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
993 }
994
Tom Stellard75aadc22012-12-11 21:25:42 +0000995 // Try to lower to a CND* instruction:
Tom Stellard2add82d2013-03-08 15:37:09 +0000996 //
997 // CND* can match the following patterns:
998 //
Tom Stellardcd428182013-09-28 02:50:38 +0000999 // select_cc f32, 0.0, f32, f32, cc_supported
1000 // select_cc f32, 0.0, i32, i32, cc_supported
1001 // select_cc i32, 0, f32, f32, cc_supported
1002 // select_cc i32, 0, i32, i32, cc_supported
Tom Stellard2add82d2013-03-08 15:37:09 +00001003 //
Tom Stellardcd428182013-09-28 02:50:38 +00001004
1005 // Try to move the zero value to the RHS
1006 if (isZero(LHS)) {
1007 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
1008 // Try swapping the operands
1009 ISD::CondCode CCSwapped = ISD::getSetCCSwappedOperands(CCOpcode);
1010 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
1011 std::swap(LHS, RHS);
1012 CC = DAG.getCondCode(CCSwapped);
1013 } else {
1014 // Try inverting the conditon and then swapping the operands
1015 ISD::CondCode CCInv = ISD::getSetCCInverse(CCOpcode, CompareVT.isInteger());
1016 CCSwapped = ISD::getSetCCSwappedOperands(CCInv);
1017 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
1018 std::swap(True, False);
1019 std::swap(LHS, RHS);
1020 CC = DAG.getCondCode(CCSwapped);
1021 }
1022 }
1023 }
1024 if (isZero(RHS)) {
1025 SDValue Cond = LHS;
1026 SDValue Zero = RHS;
Tom Stellard75aadc22012-12-11 21:25:42 +00001027 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
1028 if (CompareVT != VT) {
1029 // Bitcast True / False to the correct types. This will end up being
1030 // a nop, but it allows us to define only a single pattern in the
1031 // .TD files for each CND* instruction rather than having to have
1032 // one pattern for integer True/False and one for fp True/False
1033 True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
1034 False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
1035 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001036
1037 switch (CCOpcode) {
1038 case ISD::SETONE:
1039 case ISD::SETUNE:
1040 case ISD::SETNE:
Tom Stellard75aadc22012-12-11 21:25:42 +00001041 CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
1042 Temp = True;
1043 True = False;
1044 False = Temp;
1045 break;
1046 default:
1047 break;
1048 }
1049 SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
1050 Cond, Zero,
1051 True, False,
1052 DAG.getCondCode(CCOpcode));
1053 return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
1054 }
1055
Tom Stellard75aadc22012-12-11 21:25:42 +00001056 // If we make it this for it means we have no native instructions to handle
1057 // this SELECT_CC, so we must lower it.
1058 SDValue HWTrue, HWFalse;
1059
1060 if (CompareVT == MVT::f32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001061 HWTrue = DAG.getConstantFP(1.0f, DL, CompareVT);
1062 HWFalse = DAG.getConstantFP(0.0f, DL, CompareVT);
Tom Stellard75aadc22012-12-11 21:25:42 +00001063 } else if (CompareVT == MVT::i32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001064 HWTrue = DAG.getConstant(-1, DL, CompareVT);
1065 HWFalse = DAG.getConstant(0, DL, CompareVT);
Tom Stellard75aadc22012-12-11 21:25:42 +00001066 }
1067 else {
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +00001068 llvm_unreachable("Unhandled value type in LowerSELECT_CC");
Tom Stellard75aadc22012-12-11 21:25:42 +00001069 }
1070
1071 // Lower this unsupported SELECT_CC into a combination of two supported
1072 // SELECT_CC operations.
1073 SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
1074
1075 return DAG.getNode(ISD::SELECT_CC, DL, VT,
1076 Cond, HWFalse,
1077 True, False,
1078 DAG.getCondCode(ISD::SETNE));
1079}
1080
Alp Tokercb402912014-01-24 17:20:08 +00001081/// LLVM generates byte-addressed pointers. For indirect addressing, we need to
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001082/// convert these pointers to a register index. Each register holds
1083/// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
1084/// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
1085/// for indirect addressing.
1086SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
1087 unsigned StackWidth,
1088 SelectionDAG &DAG) const {
1089 unsigned SRLPad;
1090 switch(StackWidth) {
1091 case 1:
1092 SRLPad = 2;
1093 break;
1094 case 2:
1095 SRLPad = 3;
1096 break;
1097 case 4:
1098 SRLPad = 4;
1099 break;
1100 default: llvm_unreachable("Invalid stack width");
1101 }
1102
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001103 SDLoc DL(Ptr);
1104 return DAG.getNode(ISD::SRL, DL, Ptr.getValueType(), Ptr,
1105 DAG.getConstant(SRLPad, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001106}
1107
1108void R600TargetLowering::getStackAddress(unsigned StackWidth,
1109 unsigned ElemIdx,
1110 unsigned &Channel,
1111 unsigned &PtrIncr) const {
1112 switch (StackWidth) {
1113 default:
1114 case 1:
1115 Channel = 0;
1116 if (ElemIdx > 0) {
1117 PtrIncr = 1;
1118 } else {
1119 PtrIncr = 0;
1120 }
1121 break;
1122 case 2:
1123 Channel = ElemIdx % 2;
1124 if (ElemIdx == 2) {
1125 PtrIncr = 1;
1126 } else {
1127 PtrIncr = 0;
1128 }
1129 break;
1130 case 4:
1131 Channel = ElemIdx;
1132 PtrIncr = 0;
1133 break;
1134 }
1135}
1136
Matt Arsenault95245662016-02-11 05:32:46 +00001137SDValue R600TargetLowering::lowerPrivateTruncStore(StoreSDNode *Store,
1138 SelectionDAG &DAG) const {
1139 SDLoc DL(Store);
Jan Vesely06200bd2017-01-06 21:00:46 +00001140 //TODO: Who creates the i8 stores?
1141 assert(Store->isTruncatingStore()
1142 || Store->getValue().getValueType() == MVT::i8);
Matt Arsenault0da63502018-08-31 05:49:54 +00001143 assert(Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS);
Tom Stellard75aadc22012-12-11 21:25:42 +00001144
Jan Vesely06200bd2017-01-06 21:00:46 +00001145 SDValue Mask;
Matt Arsenault95245662016-02-11 05:32:46 +00001146 if (Store->getMemoryVT() == MVT::i8) {
Jan Vesely06200bd2017-01-06 21:00:46 +00001147 assert(Store->getAlignment() >= 1);
1148 Mask = DAG.getConstant(0xff, DL, MVT::i32);
Matt Arsenault95245662016-02-11 05:32:46 +00001149 } else if (Store->getMemoryVT() == MVT::i16) {
Jan Vesely06200bd2017-01-06 21:00:46 +00001150 assert(Store->getAlignment() >= 2);
Mandeep Singh Grang5e1697e2017-06-06 05:08:36 +00001151 Mask = DAG.getConstant(0xffff, DL, MVT::i32);
Jan Vesely06200bd2017-01-06 21:00:46 +00001152 } else {
1153 llvm_unreachable("Unsupported private trunc store");
Matt Arsenault95245662016-02-11 05:32:46 +00001154 }
1155
Jan Veselyf1705042017-01-20 21:24:26 +00001156 SDValue OldChain = Store->getChain();
1157 bool VectorTrunc = (OldChain.getOpcode() == AMDGPUISD::DUMMY_CHAIN);
1158 // Skip dummy
1159 SDValue Chain = VectorTrunc ? OldChain->getOperand(0) : OldChain;
Matt Arsenault95245662016-02-11 05:32:46 +00001160 SDValue BasePtr = Store->getBasePtr();
Jan Vesely06200bd2017-01-06 21:00:46 +00001161 SDValue Offset = Store->getOffset();
Matt Arsenault95245662016-02-11 05:32:46 +00001162 EVT MemVT = Store->getMemoryVT();
1163
Jan Vesely06200bd2017-01-06 21:00:46 +00001164 SDValue LoadPtr = BasePtr;
1165 if (!Offset.isUndef()) {
1166 LoadPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, Offset);
1167 }
Matt Arsenault95245662016-02-11 05:32:46 +00001168
Jan Vesely06200bd2017-01-06 21:00:46 +00001169 // Get dword location
1170 // TODO: this should be eliminated by the future SHR ptr, 2
1171 SDValue Ptr = DAG.getNode(ISD::AND, DL, MVT::i32, LoadPtr,
1172 DAG.getConstant(0xfffffffc, DL, MVT::i32));
1173
1174 // Load dword
1175 // TODO: can we be smarter about machine pointer info?
Yaxun Liu35845f02017-11-10 02:03:28 +00001176 MachinePointerInfo PtrInfo(UndefValue::get(
Matt Arsenault0da63502018-08-31 05:49:54 +00001177 Type::getInt32PtrTy(*DAG.getContext(), AMDGPUAS::PRIVATE_ADDRESS)));
Yaxun Liu35845f02017-11-10 02:03:28 +00001178 SDValue Dst = DAG.getLoad(MVT::i32, DL, Chain, Ptr, PtrInfo);
Jan Vesely06200bd2017-01-06 21:00:46 +00001179
1180 Chain = Dst.getValue(1);
1181
1182 // Get offset in dword
1183 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, LoadPtr,
Matt Arsenault95245662016-02-11 05:32:46 +00001184 DAG.getConstant(0x3, DL, MVT::i32));
1185
Jan Vesely06200bd2017-01-06 21:00:46 +00001186 // Convert byte offset to bit shift
Matt Arsenault95245662016-02-11 05:32:46 +00001187 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1188 DAG.getConstant(3, DL, MVT::i32));
1189
Jan Vesely06200bd2017-01-06 21:00:46 +00001190 // TODO: Contrary to the name of the functiom,
1191 // it also handles sub i32 non-truncating stores (like i1)
Matt Arsenault95245662016-02-11 05:32:46 +00001192 SDValue SExtValue = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i32,
1193 Store->getValue());
1194
Jan Vesely06200bd2017-01-06 21:00:46 +00001195 // Mask the value to the right type
Matt Arsenault95245662016-02-11 05:32:46 +00001196 SDValue MaskedValue = DAG.getZeroExtendInReg(SExtValue, DL, MemVT);
1197
Jan Vesely06200bd2017-01-06 21:00:46 +00001198 // Shift the value in place
Matt Arsenault95245662016-02-11 05:32:46 +00001199 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
1200 MaskedValue, ShiftAmt);
1201
Jan Vesely06200bd2017-01-06 21:00:46 +00001202 // Shift the mask in place
1203 SDValue DstMask = DAG.getNode(ISD::SHL, DL, MVT::i32, Mask, ShiftAmt);
1204
1205 // Invert the mask. NOTE: if we had native ROL instructions we could
1206 // use inverted mask
1207 DstMask = DAG.getNOT(DL, DstMask, MVT::i32);
1208
1209 // Cleanup the target bits
Matt Arsenault95245662016-02-11 05:32:46 +00001210 Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
1211
Jan Vesely06200bd2017-01-06 21:00:46 +00001212 // Add the new bits
Matt Arsenault95245662016-02-11 05:32:46 +00001213 SDValue Value = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
Jan Vesely06200bd2017-01-06 21:00:46 +00001214
1215 // Store dword
1216 // TODO: Can we be smarter about MachinePointerInfo?
Yaxun Liu35845f02017-11-10 02:03:28 +00001217 SDValue NewStore = DAG.getStore(Chain, DL, Value, Ptr, PtrInfo);
Jan Veselyf1705042017-01-20 21:24:26 +00001218
1219 // If we are part of expanded vector, make our neighbors depend on this store
1220 if (VectorTrunc) {
1221 // Make all other vector elements depend on this store
1222 Chain = DAG.getNode(AMDGPUISD::DUMMY_CHAIN, DL, MVT::Other, NewStore);
1223 DAG.ReplaceAllUsesOfValueWith(OldChain, Chain);
1224 }
1225 return NewStore;
Matt Arsenault95245662016-02-11 05:32:46 +00001226}
1227
1228SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault95245662016-02-11 05:32:46 +00001229 StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
1230 unsigned AS = StoreNode->getAddressSpace();
Matt Arsenault95245662016-02-11 05:32:46 +00001231
Jan Vesely06200bd2017-01-06 21:00:46 +00001232 SDValue Chain = StoreNode->getChain();
1233 SDValue Ptr = StoreNode->getBasePtr();
1234 SDValue Value = StoreNode->getValue();
1235
1236 EVT VT = Value.getValueType();
1237 EVT MemVT = StoreNode->getMemoryVT();
1238 EVT PtrVT = Ptr.getValueType();
1239
1240 SDLoc DL(Op);
1241
1242 // Neither LOCAL nor PRIVATE can do vectors at the moment
Matt Arsenault0da63502018-08-31 05:49:54 +00001243 if ((AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::PRIVATE_ADDRESS) &&
Jan Vesely06200bd2017-01-06 21:00:46 +00001244 VT.isVector()) {
Matt Arsenault0da63502018-08-31 05:49:54 +00001245 if ((AS == AMDGPUAS::PRIVATE_ADDRESS) &&
Yaxun Liu1a14bfa2017-03-27 14:04:01 +00001246 StoreNode->isTruncatingStore()) {
Jan Veselyf1705042017-01-20 21:24:26 +00001247 // Add an extra level of chain to isolate this vector
1248 SDValue NewChain = DAG.getNode(AMDGPUISD::DUMMY_CHAIN, DL, MVT::Other, Chain);
1249 // TODO: can the chain be replaced without creating a new store?
1250 SDValue NewStore = DAG.getTruncStore(
1251 NewChain, DL, Value, Ptr, StoreNode->getPointerInfo(),
1252 MemVT, StoreNode->getAlignment(),
1253 StoreNode->getMemOperand()->getFlags(), StoreNode->getAAInfo());
1254 StoreNode = cast<StoreSDNode>(NewStore);
1255 }
1256
Jan Vesely06200bd2017-01-06 21:00:46 +00001257 return scalarizeVectorStore(StoreNode, DAG);
Matt Arsenault95245662016-02-11 05:32:46 +00001258 }
1259
Jan Vesely06200bd2017-01-06 21:00:46 +00001260 unsigned Align = StoreNode->getAlignment();
1261 if (Align < MemVT.getStoreSize() &&
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001262 !allowsMisalignedMemoryAccesses(MemVT, AS, Align, nullptr)) {
Jan Vesely00864882016-09-02 19:07:06 +00001263 return expandUnalignedStore(StoreNode, DAG);
1264 }
1265
Jan Vesely06200bd2017-01-06 21:00:46 +00001266 SDValue DWordAddr = DAG.getNode(ISD::SRL, DL, PtrVT, Ptr,
1267 DAG.getConstant(2, DL, PtrVT));
Matt Arsenault95245662016-02-11 05:32:46 +00001268
Matt Arsenault0da63502018-08-31 05:49:54 +00001269 if (AS == AMDGPUAS::GLOBAL_ADDRESS) {
Jan Vesely00864882016-09-02 19:07:06 +00001270 // It is beneficial to create MSKOR here instead of combiner to avoid
1271 // artificial dependencies introduced by RMW
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001272 if (StoreNode->isTruncatingStore()) {
Tom Stellardfbab8272013-08-16 01:12:11 +00001273 assert(VT.bitsLE(MVT::i32));
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001274 SDValue MaskConstant;
1275 if (MemVT == MVT::i8) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001276 MaskConstant = DAG.getConstant(0xFF, DL, MVT::i32);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001277 } else {
1278 assert(MemVT == MVT::i16);
Jan Vesely00864882016-09-02 19:07:06 +00001279 assert(StoreNode->getAlignment() >= 2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001280 MaskConstant = DAG.getConstant(0xFFFF, DL, MVT::i32);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001281 }
Jan Vesely06200bd2017-01-06 21:00:46 +00001282
1283 SDValue ByteIndex = DAG.getNode(ISD::AND, DL, PtrVT, Ptr,
1284 DAG.getConstant(0x00000003, DL, PtrVT));
1285 SDValue BitShift = DAG.getNode(ISD::SHL, DL, VT, ByteIndex,
1286 DAG.getConstant(3, DL, VT));
1287
1288 // Put the mask in correct place
1289 SDValue Mask = DAG.getNode(ISD::SHL, DL, VT, MaskConstant, BitShift);
1290
Jan Veselyf1705042017-01-20 21:24:26 +00001291 // Put the value bits in correct place
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001292 SDValue TruncValue = DAG.getNode(ISD::AND, DL, VT, Value, MaskConstant);
Jan Vesely06200bd2017-01-06 21:00:46 +00001293 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, VT, TruncValue, BitShift);
1294
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001295 // XXX: If we add a 64-bit ZW register class, then we could use a 2 x i32
1296 // vector instead.
1297 SDValue Src[4] = {
1298 ShiftedValue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001299 DAG.getConstant(0, DL, MVT::i32),
1300 DAG.getConstant(0, DL, MVT::i32),
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001301 Mask
1302 };
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001303 SDValue Input = DAG.getBuildVector(MVT::v4i32, DL, Src);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001304 SDValue Args[3] = { Chain, Input, DWordAddr };
1305 return DAG.getMemIntrinsicNode(AMDGPUISD::STORE_MSKOR, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00001306 Op->getVTList(), Args, MemVT,
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001307 StoreNode->getMemOperand());
Jan Vesely06200bd2017-01-06 21:00:46 +00001308 } else if (Ptr->getOpcode() != AMDGPUISD::DWORDADDR && VT.bitsGE(MVT::i32)) {
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001309 // Convert pointer from byte address to dword address.
Jan Vesely06200bd2017-01-06 21:00:46 +00001310 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, PtrVT, DWordAddr);
Tom Stellard75aadc22012-12-11 21:25:42 +00001311
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001312 if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +00001313 llvm_unreachable("Truncated and indexed stores not supported yet");
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001314 } else {
1315 Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
1316 }
1317 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001318 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001319 }
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001320
Jan Vesely06200bd2017-01-06 21:00:46 +00001321 // GLOBAL_ADDRESS has been handled above, LOCAL_ADDRESS allows all sizes
Matt Arsenault0da63502018-08-31 05:49:54 +00001322 if (AS != AMDGPUAS::PRIVATE_ADDRESS)
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001323 return SDValue();
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001324
Matt Arsenault95245662016-02-11 05:32:46 +00001325 if (MemVT.bitsLT(MVT::i32))
1326 return lowerPrivateTruncStore(StoreNode, DAG);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001327
Jan Vesely06200bd2017-01-06 21:00:46 +00001328 // Standard i32+ store, tag it with DWORDADDR to note that the address
1329 // has been shifted
1330 if (Ptr.getOpcode() != AMDGPUISD::DWORDADDR) {
1331 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, PtrVT, DWordAddr);
1332 return DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001333 }
1334
Jan Vesely06200bd2017-01-06 21:00:46 +00001335 // Tagged i32+ stores will be matched by patterns
1336 return SDValue();
Tom Stellard75aadc22012-12-11 21:25:42 +00001337}
1338
Tom Stellard365366f2013-01-23 02:09:06 +00001339// return (512 + (kc_bank << 12)
1340static int
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001341ConstantAddressBlock(unsigned AddressSpace) {
Tom Stellard365366f2013-01-23 02:09:06 +00001342 switch (AddressSpace) {
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001343 case AMDGPUAS::CONSTANT_BUFFER_0:
Tom Stellard365366f2013-01-23 02:09:06 +00001344 return 512;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001345 case AMDGPUAS::CONSTANT_BUFFER_1:
Tom Stellard365366f2013-01-23 02:09:06 +00001346 return 512 + 4096;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001347 case AMDGPUAS::CONSTANT_BUFFER_2:
Tom Stellard365366f2013-01-23 02:09:06 +00001348 return 512 + 4096 * 2;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001349 case AMDGPUAS::CONSTANT_BUFFER_3:
Tom Stellard365366f2013-01-23 02:09:06 +00001350 return 512 + 4096 * 3;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001351 case AMDGPUAS::CONSTANT_BUFFER_4:
Tom Stellard365366f2013-01-23 02:09:06 +00001352 return 512 + 4096 * 4;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001353 case AMDGPUAS::CONSTANT_BUFFER_5:
Tom Stellard365366f2013-01-23 02:09:06 +00001354 return 512 + 4096 * 5;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001355 case AMDGPUAS::CONSTANT_BUFFER_6:
Tom Stellard365366f2013-01-23 02:09:06 +00001356 return 512 + 4096 * 6;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001357 case AMDGPUAS::CONSTANT_BUFFER_7:
Tom Stellard365366f2013-01-23 02:09:06 +00001358 return 512 + 4096 * 7;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001359 case AMDGPUAS::CONSTANT_BUFFER_8:
Tom Stellard365366f2013-01-23 02:09:06 +00001360 return 512 + 4096 * 8;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001361 case AMDGPUAS::CONSTANT_BUFFER_9:
Tom Stellard365366f2013-01-23 02:09:06 +00001362 return 512 + 4096 * 9;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001363 case AMDGPUAS::CONSTANT_BUFFER_10:
Tom Stellard365366f2013-01-23 02:09:06 +00001364 return 512 + 4096 * 10;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001365 case AMDGPUAS::CONSTANT_BUFFER_11:
Tom Stellard365366f2013-01-23 02:09:06 +00001366 return 512 + 4096 * 11;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001367 case AMDGPUAS::CONSTANT_BUFFER_12:
Tom Stellard365366f2013-01-23 02:09:06 +00001368 return 512 + 4096 * 12;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001369 case AMDGPUAS::CONSTANT_BUFFER_13:
Tom Stellard365366f2013-01-23 02:09:06 +00001370 return 512 + 4096 * 13;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001371 case AMDGPUAS::CONSTANT_BUFFER_14:
Tom Stellard365366f2013-01-23 02:09:06 +00001372 return 512 + 4096 * 14;
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001373 case AMDGPUAS::CONSTANT_BUFFER_15:
Tom Stellard365366f2013-01-23 02:09:06 +00001374 return 512 + 4096 * 15;
1375 default:
1376 return -1;
1377 }
1378}
1379
Matt Arsenault6dfda962016-02-10 18:21:39 +00001380SDValue R600TargetLowering::lowerPrivateExtLoad(SDValue Op,
1381 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001382 SDLoc DL(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001383 LoadSDNode *Load = cast<LoadSDNode>(Op);
1384 ISD::LoadExtType ExtType = Load->getExtensionType();
1385 EVT MemVT = Load->getMemoryVT();
Jan Vesely06200bd2017-01-06 21:00:46 +00001386 assert(Load->getAlignment() >= MemVT.getStoreSize());
Tom Stellard365366f2013-01-23 02:09:06 +00001387
Jan Vesely06200bd2017-01-06 21:00:46 +00001388 SDValue BasePtr = Load->getBasePtr();
1389 SDValue Chain = Load->getChain();
1390 SDValue Offset = Load->getOffset();
Matt Arsenault6dfda962016-02-10 18:21:39 +00001391
Jan Vesely06200bd2017-01-06 21:00:46 +00001392 SDValue LoadPtr = BasePtr;
1393 if (!Offset.isUndef()) {
1394 LoadPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, Offset);
1395 }
1396
1397 // Get dword location
1398 // NOTE: this should be eliminated by the future SHR ptr, 2
1399 SDValue Ptr = DAG.getNode(ISD::AND, DL, MVT::i32, LoadPtr,
1400 DAG.getConstant(0xfffffffc, DL, MVT::i32));
1401
1402 // Load dword
1403 // TODO: can we be smarter about machine pointer info?
Yaxun Liu35845f02017-11-10 02:03:28 +00001404 MachinePointerInfo PtrInfo(UndefValue::get(
Matt Arsenault0da63502018-08-31 05:49:54 +00001405 Type::getInt32PtrTy(*DAG.getContext(), AMDGPUAS::PRIVATE_ADDRESS)));
Yaxun Liu35845f02017-11-10 02:03:28 +00001406 SDValue Read = DAG.getLoad(MVT::i32, DL, Chain, Ptr, PtrInfo);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001407
1408 // Get offset within the register.
1409 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32,
Jan Vesely06200bd2017-01-06 21:00:46 +00001410 LoadPtr, DAG.getConstant(0x3, DL, MVT::i32));
Matt Arsenault6dfda962016-02-10 18:21:39 +00001411
1412 // Bit offset of target byte (byteIdx * 8).
1413 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1414 DAG.getConstant(3, DL, MVT::i32));
1415
1416 // Shift to the right.
Jan Vesely06200bd2017-01-06 21:00:46 +00001417 SDValue Ret = DAG.getNode(ISD::SRL, DL, MVT::i32, Read, ShiftAmt);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001418
1419 // Eliminate the upper bits by setting them to ...
1420 EVT MemEltVT = MemVT.getScalarType();
1421
Jan Vesely06200bd2017-01-06 21:00:46 +00001422 if (ExtType == ISD::SEXTLOAD) { // ... ones.
Matt Arsenault6dfda962016-02-10 18:21:39 +00001423 SDValue MemEltVTNode = DAG.getValueType(MemEltVT);
Jan Vesely06200bd2017-01-06 21:00:46 +00001424 Ret = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, Ret, MemEltVTNode);
1425 } else { // ... or zeros.
1426 Ret = DAG.getZeroExtendInReg(Ret, DL, MemEltVT);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001427 }
1428
Matt Arsenault6dfda962016-02-10 18:21:39 +00001429 SDValue Ops[] = {
Jan Vesely06200bd2017-01-06 21:00:46 +00001430 Ret,
1431 Read.getValue(1) // This should be our output chain
Matt Arsenault6dfda962016-02-10 18:21:39 +00001432 };
1433
1434 return DAG.getMergeValues(Ops, DL);
1435}
1436
1437SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1438 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1439 unsigned AS = LoadNode->getAddressSpace();
1440 EVT MemVT = LoadNode->getMemoryVT();
1441 ISD::LoadExtType ExtType = LoadNode->getExtensionType();
1442
Matt Arsenault0da63502018-08-31 05:49:54 +00001443 if (AS == AMDGPUAS::PRIVATE_ADDRESS &&
Matt Arsenault6dfda962016-02-10 18:21:39 +00001444 ExtType != ISD::NON_EXTLOAD && MemVT.bitsLT(MVT::i32)) {
1445 return lowerPrivateExtLoad(Op, DAG);
1446 }
1447
1448 SDLoc DL(Op);
1449 EVT VT = Op.getValueType();
1450 SDValue Chain = LoadNode->getChain();
1451 SDValue Ptr = LoadNode->getBasePtr();
Tom Stellarde9373602014-01-22 19:24:14 +00001452
Matt Arsenault0da63502018-08-31 05:49:54 +00001453 if ((LoadNode->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
1454 LoadNode->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) &&
Jan Vesely06200bd2017-01-06 21:00:46 +00001455 VT.isVector()) {
1456 return scalarizeVectorLoad(LoadNode, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001457 }
1458
Jan Vesely93b25272018-08-01 18:36:07 +00001459 // This is still used for explicit load from addrspace(8)
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +00001460 int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
Matt Arsenault00a0d6f2013-11-13 02:39:07 +00001461 if (ConstantBlock > -1 &&
1462 ((LoadNode->getExtensionType() == ISD::NON_EXTLOAD) ||
1463 (LoadNode->getExtensionType() == ISD::ZEXTLOAD))) {
Tom Stellard365366f2013-01-23 02:09:06 +00001464 SDValue Result;
Jan Vesely93b25272018-08-01 18:36:07 +00001465 if (isa<Constant>(LoadNode->getMemOperand()->getValue()) ||
Matt Arsenaultef1a9502013-11-01 17:39:26 +00001466 isa<ConstantSDNode>(Ptr)) {
Jan Vesely93b25272018-08-01 18:36:07 +00001467 return constBufferLoad(LoadNode, LoadNode->getAddressSpace(), DAG);
Tom Stellard365366f2013-01-23 02:09:06 +00001468 } else {
Jan Vesely93b25272018-08-01 18:36:07 +00001469 //TODO: Does this even work?
Alp Tokerf907b892013-12-05 05:44:44 +00001470 // non-constant ptr can't be folded, keeps it as a v4f32 load
Tom Stellard365366f2013-01-23 02:09:06 +00001471 Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001472 DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr,
1473 DAG.getConstant(4, DL, MVT::i32)),
1474 DAG.getConstant(LoadNode->getAddressSpace() -
Matt Arsenault0da63502018-08-31 05:49:54 +00001475 AMDGPUAS::CONSTANT_BUFFER_0, DL, MVT::i32)
Tom Stellard365366f2013-01-23 02:09:06 +00001476 );
1477 }
1478
1479 if (!VT.isVector()) {
1480 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001481 DAG.getConstant(0, DL, MVT::i32));
Tom Stellard365366f2013-01-23 02:09:06 +00001482 }
1483
1484 SDValue MergedValues[2] = {
Matt Arsenault7939acd2014-04-07 16:44:24 +00001485 Result,
1486 Chain
Tom Stellard365366f2013-01-23 02:09:06 +00001487 };
Craig Topper64941d92014-04-27 19:20:57 +00001488 return DAG.getMergeValues(MergedValues, DL);
Tom Stellard365366f2013-01-23 02:09:06 +00001489 }
1490
Matt Arsenault909d0c02013-10-30 23:43:29 +00001491 // For most operations returning SDValue() will result in the node being
1492 // expanded by the DAG Legalizer. This is not the case for ISD::LOAD, so we
1493 // need to manually expand loads that may be legal in some address spaces and
1494 // illegal in others. SEXT loads from CONSTANT_BUFFER_0 are supported for
1495 // compute shaders, since the data is sign extended when it is uploaded to the
1496 // buffer. However SEXT loads from other address spaces are not supported, so
1497 // we need to expand them here.
Tom Stellard84021442013-07-23 01:48:24 +00001498 if (LoadNode->getExtensionType() == ISD::SEXTLOAD) {
1499 EVT MemVT = LoadNode->getMemoryVT();
1500 assert(!MemVT.isVector() && (MemVT == MVT::i16 || MemVT == MVT::i8));
Justin Lebar9c375812016-07-15 18:27:10 +00001501 SDValue NewLoad = DAG.getExtLoad(
1502 ISD::EXTLOAD, DL, VT, Chain, Ptr, LoadNode->getPointerInfo(), MemVT,
1503 LoadNode->getAlignment(), LoadNode->getMemOperand()->getFlags());
Jan Veselyb670d372015-05-26 18:07:22 +00001504 SDValue Res = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, NewLoad,
1505 DAG.getValueType(MemVT));
Tom Stellard84021442013-07-23 01:48:24 +00001506
Jan Veselyb670d372015-05-26 18:07:22 +00001507 SDValue MergedValues[2] = { Res, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00001508 return DAG.getMergeValues(MergedValues, DL);
Tom Stellard84021442013-07-23 01:48:24 +00001509 }
1510
Matt Arsenault0da63502018-08-31 05:49:54 +00001511 if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001512 return SDValue();
1513 }
1514
Jan Vesely06200bd2017-01-06 21:00:46 +00001515 // DWORDADDR ISD marks already shifted address
1516 if (Ptr.getOpcode() != AMDGPUISD::DWORDADDR) {
1517 assert(VT == MVT::i32);
1518 Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr, DAG.getConstant(2, DL, MVT::i32));
1519 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, MVT::i32, Ptr);
1520 return DAG.getLoad(MVT::i32, DL, Chain, Ptr, LoadNode->getMemOperand());
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001521 }
Jan Vesely06200bd2017-01-06 21:00:46 +00001522 return SDValue();
Tom Stellard365366f2013-01-23 02:09:06 +00001523}
Tom Stellard75aadc22012-12-11 21:25:42 +00001524
Matt Arsenault1d555c42014-06-23 18:00:55 +00001525SDValue R600TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
1526 SDValue Chain = Op.getOperand(0);
1527 SDValue Cond = Op.getOperand(1);
1528 SDValue Jump = Op.getOperand(2);
1529
1530 return DAG.getNode(AMDGPUISD::BRANCH_COND, SDLoc(Op), Op.getValueType(),
1531 Chain, Jump, Cond);
1532}
1533
Matt Arsenault81d06012016-03-07 21:10:13 +00001534SDValue R600TargetLowering::lowerFrameIndex(SDValue Op,
1535 SelectionDAG &DAG) const {
1536 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardc5a154d2018-06-28 23:47:12 +00001537 const R600FrameLowering *TFL = Subtarget->getFrameLowering();
Matt Arsenault81d06012016-03-07 21:10:13 +00001538
1539 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op);
1540
1541 unsigned FrameIndex = FIN->getIndex();
1542 unsigned IgnoredFrameReg;
1543 unsigned Offset =
1544 TFL->getFrameIndexReference(MF, FrameIndex, IgnoredFrameReg);
1545 return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF), SDLoc(Op),
1546 Op.getValueType());
1547}
1548
Tom Stellardc5a154d2018-06-28 23:47:12 +00001549CCAssignFn *R600TargetLowering::CCAssignFnForCall(CallingConv::ID CC,
1550 bool IsVarArg) const {
1551 switch (CC) {
1552 case CallingConv::AMDGPU_KERNEL:
1553 case CallingConv::SPIR_KERNEL:
1554 case CallingConv::C:
1555 case CallingConv::Fast:
1556 case CallingConv::Cold:
Matt Arsenault4bec7d42018-07-20 09:05:08 +00001557 llvm_unreachable("kernels should not be handled here");
Tom Stellardc5a154d2018-06-28 23:47:12 +00001558 case CallingConv::AMDGPU_VS:
1559 case CallingConv::AMDGPU_GS:
1560 case CallingConv::AMDGPU_PS:
1561 case CallingConv::AMDGPU_CS:
1562 case CallingConv::AMDGPU_HS:
1563 case CallingConv::AMDGPU_ES:
1564 case CallingConv::AMDGPU_LS:
1565 return CC_R600;
1566 default:
1567 report_fatal_error("Unsupported calling convention.");
1568 }
1569}
1570
Tom Stellard75aadc22012-12-11 21:25:42 +00001571/// XXX Only kernel functions are supported, so we can assume for now that
1572/// every function is a kernel function, but in the future we should use
1573/// separate calling conventions for kernel and non-kernel functions.
1574SDValue R600TargetLowering::LowerFormalArguments(
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001575 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1576 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
1577 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Tom Stellardacfeebf2013-07-23 01:48:05 +00001578 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001579 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1580 *DAG.getContext());
Vincent Lejeunef143af32013-11-11 22:10:24 +00001581 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardaf775432013-10-23 00:44:32 +00001582 SmallVector<ISD::InputArg, 8> LocalIns;
1583
Tom Stellardbbeb45a2016-09-16 21:53:00 +00001584 if (AMDGPU::isShader(CallConv)) {
Matt Arsenaulte622dc32017-04-11 22:29:24 +00001585 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForCall(CallConv, isVarArg));
Tom Stellardbbeb45a2016-09-16 21:53:00 +00001586 } else {
1587 analyzeFormalArgumentsCompute(CCInfo, Ins);
1588 }
Tom Stellardacfeebf2013-07-23 01:48:05 +00001589
Tom Stellard1e803092013-07-23 01:48:18 +00001590 for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
Tom Stellardacfeebf2013-07-23 01:48:05 +00001591 CCValAssign &VA = ArgLocs[i];
Matt Arsenault74ef2772014-08-13 18:14:11 +00001592 const ISD::InputArg &In = Ins[i];
1593 EVT VT = In.VT;
1594 EVT MemVT = VA.getLocVT();
1595 if (!VT.isVector() && MemVT.isVector()) {
1596 // Get load source type if scalarized.
1597 MemVT = MemVT.getVectorElementType();
1598 }
Tom Stellard78e01292013-07-23 01:47:58 +00001599
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001600 if (AMDGPU::isShader(CallConv)) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00001601 unsigned Reg = MF.addLiveIn(VA.getLocReg(), &R600::R600_Reg128RegClass);
Vincent Lejeunef143af32013-11-11 22:10:24 +00001602 SDValue Register = DAG.getCopyFromReg(Chain, DL, Reg, VT);
1603 InVals.push_back(Register);
1604 continue;
1605 }
1606
Tom Stellard75aadc22012-12-11 21:25:42 +00001607 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
Matt Arsenault0da63502018-08-31 05:49:54 +00001608 AMDGPUAS::PARAM_I_ADDRESS);
Tom Stellardacfeebf2013-07-23 01:48:05 +00001609
Matt Arsenaultfae02982014-03-17 18:58:11 +00001610 // i64 isn't a legal type, so the register type used ends up as i32, which
1611 // isn't expected here. It attempts to create this sextload, but it ends up
1612 // being invalid. Somehow this seems to work with i64 arguments, but breaks
1613 // for <1 x i64>.
1614
Tom Stellardacfeebf2013-07-23 01:48:05 +00001615 // The first 36 bytes of the input buffer contains information about
1616 // thread group and global sizes.
Matt Arsenault74ef2772014-08-13 18:14:11 +00001617 ISD::LoadExtType Ext = ISD::NON_EXTLOAD;
1618 if (MemVT.getScalarSizeInBits() != VT.getScalarSizeInBits()) {
1619 // FIXME: This should really check the extload type, but the handling of
1620 // extload vector parameters seems to be broken.
Matt Arsenaulte1f030c2014-04-11 20:59:54 +00001621
Matt Arsenault74ef2772014-08-13 18:14:11 +00001622 // Ext = In.Flags.isSExt() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
1623 Ext = ISD::SEXTLOAD;
1624 }
1625
1626 // Compute the offset from the value.
1627 // XXX - I think PartOffset should give you this, but it seems to give the
1628 // size of the register which isn't useful.
1629
Andrew Trick05938a52015-02-16 18:10:47 +00001630 unsigned ValBase = ArgLocs[In.getOrigArgIndex()].getLocMemOffset();
Matt Arsenault74ef2772014-08-13 18:14:11 +00001631 unsigned PartOffset = VA.getLocMemOffset();
Jan Vesely93b25272018-08-01 18:36:07 +00001632 unsigned Alignment = MinAlign(VT.getStoreSize(), PartOffset);
Matt Arsenault74ef2772014-08-13 18:14:11 +00001633
1634 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy), PartOffset - ValBase);
Justin Lebar9c375812016-07-15 18:27:10 +00001635 SDValue Arg = DAG.getLoad(
1636 ISD::UNINDEXED, Ext, VT, DL, Chain,
Matt Arsenault4bec7d42018-07-20 09:05:08 +00001637 DAG.getConstant(PartOffset, DL, MVT::i32), DAG.getUNDEF(MVT::i32),
1638 PtrInfo,
Jan Vesely93b25272018-08-01 18:36:07 +00001639 MemVT, Alignment, MachineMemOperand::MONonTemporal |
Justin Lebaradbf09e2016-09-11 01:38:58 +00001640 MachineMemOperand::MODereferenceable |
1641 MachineMemOperand::MOInvariant);
Matt Arsenault209a7b92014-04-18 07:40:20 +00001642
Tom Stellard75aadc22012-12-11 21:25:42 +00001643 InVals.push_back(Arg);
Tom Stellard75aadc22012-12-11 21:25:42 +00001644 }
1645 return Chain;
1646}
1647
Mehdi Amini44ede332015-07-09 02:09:04 +00001648EVT R600TargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1649 EVT VT) const {
Matt Arsenault209a7b92014-04-18 07:40:20 +00001650 if (!VT.isVector())
1651 return MVT::i32;
Tom Stellard75aadc22012-12-11 21:25:42 +00001652 return VT.changeVectorElementTypeToInteger();
1653}
1654
Nirav Dave4dcad5d2017-07-10 20:25:54 +00001655bool R600TargetLowering::canMergeStoresTo(unsigned AS, EVT MemVT,
1656 const SelectionDAG &DAG) const {
Nirav Daved20066c2017-05-24 15:59:09 +00001657 // Local and Private addresses do not handle vectors. Limit to i32
Matt Arsenault0da63502018-08-31 05:49:54 +00001658 if ((AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::PRIVATE_ADDRESS)) {
Nirav Daved20066c2017-05-24 15:59:09 +00001659 return (MemVT.getSizeInBits() <= 32);
1660 }
1661 return true;
1662}
1663
Matt Arsenaultfa67bdb2016-02-22 21:04:16 +00001664bool R600TargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
1665 unsigned AddrSpace,
1666 unsigned Align,
1667 bool *IsFast) const {
1668 if (IsFast)
1669 *IsFast = false;
1670
1671 if (!VT.isSimple() || VT == MVT::Other)
1672 return false;
1673
1674 if (VT.bitsLT(MVT::i32))
1675 return false;
1676
1677 // TODO: This is a rough estimate.
1678 if (IsFast)
1679 *IsFast = true;
1680
1681 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
1682}
1683
Matt Arsenault209a7b92014-04-18 07:40:20 +00001684static SDValue CompactSwizzlableVector(
1685 SelectionDAG &DAG, SDValue VectorEntry,
1686 DenseMap<unsigned, unsigned> &RemapSwizzle) {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001687 assert(RemapSwizzle.empty());
Simon Pilgrim858303b2018-10-30 10:32:11 +00001688
1689 SDLoc DL(VectorEntry);
1690 EVT EltTy = VectorEntry.getValueType().getVectorElementType();
1691
1692 SDValue NewBldVec[4];
1693 for (unsigned i = 0; i < 4; i++)
1694 NewBldVec[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltTy, VectorEntry,
1695 DAG.getIntPtrConstant(i, DL));
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001696
1697 for (unsigned i = 0; i < 4; i++) {
Sanjay Patel57195842016-03-14 17:28:46 +00001698 if (NewBldVec[i].isUndef())
Vincent Lejeunefa58a5f2013-10-13 17:56:10 +00001699 // We mask write here to teach later passes that the ith element of this
1700 // vector is undef. Thus we can use it to reduce 128 bits reg usage,
1701 // break false dependencies and additionnaly make assembly easier to read.
1702 RemapSwizzle[i] = 7; // SEL_MASK_WRITE
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001703 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(NewBldVec[i])) {
1704 if (C->isZero()) {
1705 RemapSwizzle[i] = 4; // SEL_0
1706 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1707 } else if (C->isExactlyValue(1.0)) {
1708 RemapSwizzle[i] = 5; // SEL_1
1709 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1710 }
1711 }
1712
Sanjay Patel57195842016-03-14 17:28:46 +00001713 if (NewBldVec[i].isUndef())
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001714 continue;
1715 for (unsigned j = 0; j < i; j++) {
1716 if (NewBldVec[i] == NewBldVec[j]) {
1717 NewBldVec[i] = DAG.getUNDEF(NewBldVec[i].getValueType());
1718 RemapSwizzle[i] = j;
1719 break;
1720 }
1721 }
1722 }
1723
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001724 return DAG.getBuildVector(VectorEntry.getValueType(), SDLoc(VectorEntry),
1725 NewBldVec);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001726}
1727
Benjamin Kramer193960c2013-06-11 13:32:25 +00001728static SDValue ReorganizeVector(SelectionDAG &DAG, SDValue VectorEntry,
1729 DenseMap<unsigned, unsigned> &RemapSwizzle) {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001730 assert(RemapSwizzle.empty());
Simon Pilgrim858303b2018-10-30 10:32:11 +00001731
1732 SDLoc DL(VectorEntry);
1733 EVT EltTy = VectorEntry.getValueType().getVectorElementType();
1734
1735 SDValue NewBldVec[4];
1736 bool isUnmovable[4] = {false, false, false, false};
1737 for (unsigned i = 0; i < 4; i++)
1738 NewBldVec[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltTy, VectorEntry,
1739 DAG.getIntPtrConstant(i, DL));
1740
Vincent Lejeunecc0ea742013-12-10 14:43:31 +00001741 for (unsigned i = 0; i < 4; i++) {
Vincent Lejeuneb8aac8d2013-07-09 15:03:25 +00001742 RemapSwizzle[i] = i;
Vincent Lejeunecc0ea742013-12-10 14:43:31 +00001743 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1744 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1745 ->getZExtValue();
1746 if (i == Idx)
1747 isUnmovable[Idx] = true;
1748 }
1749 }
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001750
1751 for (unsigned i = 0; i < 4; i++) {
1752 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1753 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1754 ->getZExtValue();
Vincent Lejeune301beb82013-10-13 17:56:04 +00001755 if (isUnmovable[Idx])
1756 continue;
1757 // Swap i and Idx
1758 std::swap(NewBldVec[Idx], NewBldVec[i]);
1759 std::swap(RemapSwizzle[i], RemapSwizzle[Idx]);
1760 break;
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001761 }
1762 }
1763
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001764 return DAG.getBuildVector(VectorEntry.getValueType(), SDLoc(VectorEntry),
1765 NewBldVec);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001766}
1767
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001768SDValue R600TargetLowering::OptimizeSwizzle(SDValue BuildVector, SDValue Swz[4],
1769 SelectionDAG &DAG,
1770 const SDLoc &DL) const {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001771 // Old -> New swizzle values
1772 DenseMap<unsigned, unsigned> SwizzleRemap;
1773
1774 BuildVector = CompactSwizzlableVector(DAG, BuildVector, SwizzleRemap);
1775 for (unsigned i = 0; i < 4; i++) {
Benjamin Kramer619c4e52015-04-10 11:24:51 +00001776 unsigned Idx = cast<ConstantSDNode>(Swz[i])->getZExtValue();
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001777 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001778 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], DL, MVT::i32);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001779 }
1780
1781 SwizzleRemap.clear();
1782 BuildVector = ReorganizeVector(DAG, BuildVector, SwizzleRemap);
1783 for (unsigned i = 0; i < 4; i++) {
Benjamin Kramer619c4e52015-04-10 11:24:51 +00001784 unsigned Idx = cast<ConstantSDNode>(Swz[i])->getZExtValue();
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001785 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001786 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], DL, MVT::i32);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001787 }
1788
1789 return BuildVector;
1790}
1791
Jan Vesely93b25272018-08-01 18:36:07 +00001792SDValue R600TargetLowering::constBufferLoad(LoadSDNode *LoadNode, int Block,
1793 SelectionDAG &DAG) const {
1794 SDLoc DL(LoadNode);
1795 EVT VT = LoadNode->getValueType(0);
1796 SDValue Chain = LoadNode->getChain();
1797 SDValue Ptr = LoadNode->getBasePtr();
1798 assert (isa<ConstantSDNode>(Ptr));
1799
1800 //TODO: Support smaller loads
1801 if (LoadNode->getMemoryVT().getScalarType() != MVT::i32 || !ISD::isNON_EXTLoad(LoadNode))
1802 return SDValue();
1803
1804 if (LoadNode->getAlignment() < 4)
1805 return SDValue();
1806
1807 int ConstantBlock = ConstantAddressBlock(Block);
1808
1809 SDValue Slots[4];
1810 for (unsigned i = 0; i < 4; i++) {
1811 // We want Const position encoded with the following formula :
1812 // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
1813 // const_index is Ptr computed by llvm using an alignment of 16.
1814 // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
1815 // then div by 4 at the ISel step
1816 SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
1817 DAG.getConstant(4 * i + ConstantBlock * 16, DL, MVT::i32));
1818 Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
1819 }
1820 EVT NewVT = MVT::v4i32;
1821 unsigned NumElements = 4;
1822 if (VT.isVector()) {
1823 NewVT = VT;
1824 NumElements = VT.getVectorNumElements();
1825 }
1826 SDValue Result = DAG.getBuildVector(NewVT, DL, makeArrayRef(Slots, NumElements));
1827 if (!VT.isVector()) {
1828 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
1829 DAG.getConstant(0, DL, MVT::i32));
1830 }
1831 SDValue MergedValues[2] = {
1832 Result,
1833 Chain
1834 };
1835 return DAG.getMergeValues(MergedValues, DL);
1836}
1837
Tom Stellard75aadc22012-12-11 21:25:42 +00001838//===----------------------------------------------------------------------===//
1839// Custom DAG Optimizations
1840//===----------------------------------------------------------------------===//
1841
1842SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
1843 DAGCombinerInfo &DCI) const {
1844 SelectionDAG &DAG = DCI.DAG;
Jan Vesely89876672016-08-29 23:21:46 +00001845 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00001846
1847 switch (N->getOpcode()) {
1848 // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
1849 case ISD::FP_ROUND: {
1850 SDValue Arg = N->getOperand(0);
1851 if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
Jan Vesely89876672016-08-29 23:21:46 +00001852 return DAG.getNode(ISD::UINT_TO_FP, DL, N->getValueType(0),
Tom Stellard75aadc22012-12-11 21:25:42 +00001853 Arg.getOperand(0));
1854 }
1855 break;
1856 }
Tom Stellarde06163a2013-02-07 14:02:35 +00001857
1858 // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
1859 // (i32 select_cc f32, f32, -1, 0 cc)
1860 //
1861 // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1862 // this to one of the SET*_DX10 instructions.
1863 case ISD::FP_TO_SINT: {
1864 SDValue FNeg = N->getOperand(0);
1865 if (FNeg.getOpcode() != ISD::FNEG) {
1866 return SDValue();
1867 }
1868 SDValue SelectCC = FNeg.getOperand(0);
1869 if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1870 SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1871 SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1872 !isHWTrueValue(SelectCC.getOperand(2)) ||
1873 !isHWFalseValue(SelectCC.getOperand(3))) {
1874 return SDValue();
1875 }
1876
Jan Vesely89876672016-08-29 23:21:46 +00001877 return DAG.getNode(ISD::SELECT_CC, DL, N->getValueType(0),
Tom Stellarde06163a2013-02-07 14:02:35 +00001878 SelectCC.getOperand(0), // LHS
1879 SelectCC.getOperand(1), // RHS
Jan Vesely89876672016-08-29 23:21:46 +00001880 DAG.getConstant(-1, DL, MVT::i32), // True
1881 DAG.getConstant(0, DL, MVT::i32), // False
Tom Stellarde06163a2013-02-07 14:02:35 +00001882 SelectCC.getOperand(4)); // CC
1883
1884 break;
1885 }
Quentin Colombete2e05482013-07-30 00:27:16 +00001886
NAKAMURA Takumi8a046432013-10-28 04:07:38 +00001887 // insert_vector_elt (build_vector elt0, ... , eltN), NewEltIdx, idx
1888 // => build_vector elt0, ... , NewEltIdx, ... , eltN
Quentin Colombete2e05482013-07-30 00:27:16 +00001889 case ISD::INSERT_VECTOR_ELT: {
1890 SDValue InVec = N->getOperand(0);
1891 SDValue InVal = N->getOperand(1);
1892 SDValue EltNo = N->getOperand(2);
Quentin Colombete2e05482013-07-30 00:27:16 +00001893
1894 // If the inserted element is an UNDEF, just use the input vector.
Sanjay Patel57195842016-03-14 17:28:46 +00001895 if (InVal.isUndef())
Quentin Colombete2e05482013-07-30 00:27:16 +00001896 return InVec;
1897
1898 EVT VT = InVec.getValueType();
1899
1900 // If we can't generate a legal BUILD_VECTOR, exit
1901 if (!isOperationLegal(ISD::BUILD_VECTOR, VT))
1902 return SDValue();
1903
1904 // Check that we know which element is being inserted
1905 if (!isa<ConstantSDNode>(EltNo))
1906 return SDValue();
1907 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
1908
1909 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
1910 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
1911 // vector elements.
1912 SmallVector<SDValue, 8> Ops;
1913 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
1914 Ops.append(InVec.getNode()->op_begin(),
1915 InVec.getNode()->op_end());
Sanjay Patel57195842016-03-14 17:28:46 +00001916 } else if (InVec.isUndef()) {
Quentin Colombete2e05482013-07-30 00:27:16 +00001917 unsigned NElts = VT.getVectorNumElements();
1918 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
1919 } else {
1920 return SDValue();
1921 }
1922
1923 // Insert the element
1924 if (Elt < Ops.size()) {
1925 // All the operands of BUILD_VECTOR must have the same type;
1926 // we enforce that here.
1927 EVT OpVT = Ops[0].getValueType();
1928 if (InVal.getValueType() != OpVT)
1929 InVal = OpVT.bitsGT(InVal.getValueType()) ?
Jan Vesely89876672016-08-29 23:21:46 +00001930 DAG.getNode(ISD::ANY_EXTEND, DL, OpVT, InVal) :
1931 DAG.getNode(ISD::TRUNCATE, DL, OpVT, InVal);
Quentin Colombete2e05482013-07-30 00:27:16 +00001932 Ops[Elt] = InVal;
1933 }
1934
1935 // Return the new vector
Jan Vesely89876672016-08-29 23:21:46 +00001936 return DAG.getBuildVector(VT, DL, Ops);
Quentin Colombete2e05482013-07-30 00:27:16 +00001937 }
1938
Tom Stellard365366f2013-01-23 02:09:06 +00001939 // Extract_vec (Build_vector) generated by custom lowering
1940 // also needs to be customly combined
1941 case ISD::EXTRACT_VECTOR_ELT: {
1942 SDValue Arg = N->getOperand(0);
1943 if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1944 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1945 unsigned Element = Const->getZExtValue();
1946 return Arg->getOperand(Element);
1947 }
1948 }
Tom Stellarddd04c832013-01-31 22:11:53 +00001949 if (Arg.getOpcode() == ISD::BITCAST &&
Jan Veselyea457462016-09-02 20:13:19 +00001950 Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
1951 (Arg.getOperand(0).getValueType().getVectorNumElements() ==
1952 Arg.getValueType().getVectorNumElements())) {
Tom Stellarddd04c832013-01-31 22:11:53 +00001953 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1954 unsigned Element = Const->getZExtValue();
Jan Vesely89876672016-08-29 23:21:46 +00001955 return DAG.getNode(ISD::BITCAST, DL, N->getVTList(),
1956 Arg->getOperand(0).getOperand(Element));
Tom Stellarddd04c832013-01-31 22:11:53 +00001957 }
1958 }
Mehdi Aminie029eae2015-07-16 06:23:12 +00001959 break;
Tom Stellard365366f2013-01-23 02:09:06 +00001960 }
Tom Stellarde06163a2013-02-07 14:02:35 +00001961
1962 case ISD::SELECT_CC: {
Tom Stellardafa8b532014-05-09 16:42:16 +00001963 // Try common optimizations
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00001964 if (SDValue Ret = AMDGPUTargetLowering::PerformDAGCombine(N, DCI))
Tom Stellardafa8b532014-05-09 16:42:16 +00001965 return Ret;
1966
Tom Stellarde06163a2013-02-07 14:02:35 +00001967 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1968 // selectcc x, y, a, b, inv(cc)
Tom Stellard5e524892013-03-08 15:37:11 +00001969 //
1970 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, setne ->
1971 // selectcc x, y, a, b, cc
Tom Stellarde06163a2013-02-07 14:02:35 +00001972 SDValue LHS = N->getOperand(0);
1973 if (LHS.getOpcode() != ISD::SELECT_CC) {
1974 return SDValue();
1975 }
1976
1977 SDValue RHS = N->getOperand(1);
1978 SDValue True = N->getOperand(2);
1979 SDValue False = N->getOperand(3);
Tom Stellard5e524892013-03-08 15:37:11 +00001980 ISD::CondCode NCC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Tom Stellarde06163a2013-02-07 14:02:35 +00001981
1982 if (LHS.getOperand(2).getNode() != True.getNode() ||
1983 LHS.getOperand(3).getNode() != False.getNode() ||
Tom Stellard5e524892013-03-08 15:37:11 +00001984 RHS.getNode() != False.getNode()) {
Tom Stellarde06163a2013-02-07 14:02:35 +00001985 return SDValue();
1986 }
1987
Tom Stellard5e524892013-03-08 15:37:11 +00001988 switch (NCC) {
1989 default: return SDValue();
1990 case ISD::SETNE: return LHS;
1991 case ISD::SETEQ: {
1992 ISD::CondCode LHSCC = cast<CondCodeSDNode>(LHS.getOperand(4))->get();
1993 LHSCC = ISD::getSetCCInverse(LHSCC,
1994 LHS.getOperand(0).getValueType().isInteger());
Tom Stellardcd428182013-09-28 02:50:38 +00001995 if (DCI.isBeforeLegalizeOps() ||
1996 isCondCodeLegal(LHSCC, LHS.getOperand(0).getSimpleValueType()))
Jan Vesely89876672016-08-29 23:21:46 +00001997 return DAG.getSelectCC(DL,
Tom Stellardcd428182013-09-28 02:50:38 +00001998 LHS.getOperand(0),
1999 LHS.getOperand(1),
2000 LHS.getOperand(2),
2001 LHS.getOperand(3),
2002 LHSCC);
2003 break;
Vincent Lejeuned80bc152013-02-14 16:55:06 +00002004 }
Tom Stellard5e524892013-03-08 15:37:11 +00002005 }
Tom Stellardcd428182013-09-28 02:50:38 +00002006 return SDValue();
Tom Stellard5e524892013-03-08 15:37:11 +00002007 }
Tom Stellardfbab8272013-08-16 01:12:11 +00002008
Matt Arsenault7bee6ac2016-12-05 20:23:10 +00002009 case AMDGPUISD::R600_EXPORT: {
Vincent Lejeuned80bc152013-02-14 16:55:06 +00002010 SDValue Arg = N->getOperand(1);
2011 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
2012 break;
Vincent Lejeune276ceb82013-06-04 15:04:53 +00002013
Vincent Lejeuned80bc152013-02-14 16:55:06 +00002014 SDValue NewArgs[8] = {
2015 N->getOperand(0), // Chain
2016 SDValue(),
2017 N->getOperand(2), // ArrayBase
2018 N->getOperand(3), // Type
2019 N->getOperand(4), // SWZ_X
2020 N->getOperand(5), // SWZ_Y
2021 N->getOperand(6), // SWZ_Z
2022 N->getOperand(7) // SWZ_W
2023 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002024 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[4], DAG, DL);
Matt Arsenault7bee6ac2016-12-05 20:23:10 +00002025 return DAG.getNode(AMDGPUISD::R600_EXPORT, DL, N->getVTList(), NewArgs);
Tom Stellarde06163a2013-02-07 14:02:35 +00002026 }
Vincent Lejeune276ceb82013-06-04 15:04:53 +00002027 case AMDGPUISD::TEXTURE_FETCH: {
2028 SDValue Arg = N->getOperand(1);
2029 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
2030 break;
2031
2032 SDValue NewArgs[19] = {
2033 N->getOperand(0),
2034 N->getOperand(1),
2035 N->getOperand(2),
2036 N->getOperand(3),
2037 N->getOperand(4),
2038 N->getOperand(5),
2039 N->getOperand(6),
2040 N->getOperand(7),
2041 N->getOperand(8),
2042 N->getOperand(9),
2043 N->getOperand(10),
2044 N->getOperand(11),
2045 N->getOperand(12),
2046 N->getOperand(13),
2047 N->getOperand(14),
2048 N->getOperand(15),
2049 N->getOperand(16),
2050 N->getOperand(17),
2051 N->getOperand(18),
2052 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002053 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[2], DAG, DL);
2054 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, N->getVTList(), NewArgs);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00002055 }
Jan Vesely93b25272018-08-01 18:36:07 +00002056
2057 case ISD::LOAD: {
2058 LoadSDNode *LoadNode = cast<LoadSDNode>(N);
2059 SDValue Ptr = LoadNode->getBasePtr();
2060 if (LoadNode->getAddressSpace() == AMDGPUAS::PARAM_I_ADDRESS &&
2061 isa<ConstantSDNode>(Ptr))
2062 return constBufferLoad(LoadNode, AMDGPUAS::CONSTANT_BUFFER_0, DAG);
2063 break;
2064 }
2065
Jan Vesely89876672016-08-29 23:21:46 +00002066 default: break;
Tom Stellard75aadc22012-12-11 21:25:42 +00002067 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002068
2069 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00002070}
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002071
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002072bool R600TargetLowering::FoldOperand(SDNode *ParentNode, unsigned SrcIdx,
2073 SDValue &Src, SDValue &Neg, SDValue &Abs,
2074 SDValue &Sel, SDValue &Imm,
2075 SelectionDAG &DAG) const {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002076 const R600InstrInfo *TII = Subtarget->getInstrInfo();
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002077 if (!Src.isMachineOpcode())
2078 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002079
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002080 switch (Src.getMachineOpcode()) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002081 case R600::FNEG_R600:
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002082 if (!Neg.getNode())
2083 return false;
2084 Src = Src.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002085 Neg = DAG.getTargetConstant(1, SDLoc(ParentNode), MVT::i32);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002086 return true;
Tom Stellardc5a154d2018-06-28 23:47:12 +00002087 case R600::FABS_R600:
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002088 if (!Abs.getNode())
2089 return false;
2090 Src = Src.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002091 Abs = DAG.getTargetConstant(1, SDLoc(ParentNode), MVT::i32);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002092 return true;
Tom Stellardc5a154d2018-06-28 23:47:12 +00002093 case R600::CONST_COPY: {
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002094 unsigned Opcode = ParentNode->getMachineOpcode();
Tom Stellardc5a154d2018-06-28 23:47:12 +00002095 bool HasDst = TII->getOperandIdx(Opcode, R600::OpName::dst) > -1;
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002096
2097 if (!Sel.getNode())
2098 return false;
2099
2100 SDValue CstOffset = Src.getOperand(0);
2101 if (ParentNode->getValueType(0).isVector())
2102 return false;
2103
2104 // Gather constants values
2105 int SrcIndices[] = {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002106 TII->getOperandIdx(Opcode, R600::OpName::src0),
2107 TII->getOperandIdx(Opcode, R600::OpName::src1),
2108 TII->getOperandIdx(Opcode, R600::OpName::src2),
2109 TII->getOperandIdx(Opcode, R600::OpName::src0_X),
2110 TII->getOperandIdx(Opcode, R600::OpName::src0_Y),
2111 TII->getOperandIdx(Opcode, R600::OpName::src0_Z),
2112 TII->getOperandIdx(Opcode, R600::OpName::src0_W),
2113 TII->getOperandIdx(Opcode, R600::OpName::src1_X),
2114 TII->getOperandIdx(Opcode, R600::OpName::src1_Y),
2115 TII->getOperandIdx(Opcode, R600::OpName::src1_Z),
2116 TII->getOperandIdx(Opcode, R600::OpName::src1_W)
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002117 };
2118 std::vector<unsigned> Consts;
Matt Arsenault4d64f962014-05-12 19:23:21 +00002119 for (int OtherSrcIdx : SrcIndices) {
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002120 int OtherSelIdx = TII->getSelIdx(Opcode, OtherSrcIdx);
2121 if (OtherSrcIdx < 0 || OtherSelIdx < 0)
2122 continue;
2123 if (HasDst) {
2124 OtherSrcIdx--;
2125 OtherSelIdx--;
2126 }
2127 if (RegisterSDNode *Reg =
2128 dyn_cast<RegisterSDNode>(ParentNode->getOperand(OtherSrcIdx))) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002129 if (Reg->getReg() == R600::ALU_CONST) {
Matt Arsenaultb3ee3882014-05-12 19:26:38 +00002130 ConstantSDNode *Cst
2131 = cast<ConstantSDNode>(ParentNode->getOperand(OtherSelIdx));
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002132 Consts.push_back(Cst->getZExtValue());
2133 }
2134 }
2135 }
2136
Matt Arsenault37c12d72014-05-12 20:42:57 +00002137 ConstantSDNode *Cst = cast<ConstantSDNode>(CstOffset);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002138 Consts.push_back(Cst->getZExtValue());
2139 if (!TII->fitsConstReadLimitations(Consts)) {
2140 return false;
2141 }
2142
2143 Sel = CstOffset;
Tom Stellardc5a154d2018-06-28 23:47:12 +00002144 Src = DAG.getRegister(R600::ALU_CONST, MVT::f32);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002145 return true;
2146 }
Tom Stellardc5a154d2018-06-28 23:47:12 +00002147 case R600::MOV_IMM_GLOBAL_ADDR:
Jan Vesely16800392016-05-13 20:39:31 +00002148 // Check if the Imm slot is used. Taken from below.
2149 if (cast<ConstantSDNode>(Imm)->getZExtValue())
2150 return false;
2151 Imm = Src.getOperand(0);
Tom Stellardc5a154d2018-06-28 23:47:12 +00002152 Src = DAG.getRegister(R600::ALU_LITERAL_X, MVT::i32);
Jan Vesely16800392016-05-13 20:39:31 +00002153 return true;
Tom Stellardc5a154d2018-06-28 23:47:12 +00002154 case R600::MOV_IMM_I32:
2155 case R600::MOV_IMM_F32: {
2156 unsigned ImmReg = R600::ALU_LITERAL_X;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002157 uint64_t ImmValue = 0;
2158
Tom Stellardc5a154d2018-06-28 23:47:12 +00002159 if (Src.getMachineOpcode() == R600::MOV_IMM_F32) {
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002160 ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Src.getOperand(0));
2161 float FloatValue = FPC->getValueAPF().convertToFloat();
2162 if (FloatValue == 0.0) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002163 ImmReg = R600::ZERO;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002164 } else if (FloatValue == 0.5) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002165 ImmReg = R600::HALF;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002166 } else if (FloatValue == 1.0) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002167 ImmReg = R600::ONE;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002168 } else {
2169 ImmValue = FPC->getValueAPF().bitcastToAPInt().getZExtValue();
2170 }
2171 } else {
2172 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(0));
2173 uint64_t Value = C->getZExtValue();
2174 if (Value == 0) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002175 ImmReg = R600::ZERO;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002176 } else if (Value == 1) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002177 ImmReg = R600::ONE_INT;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002178 } else {
2179 ImmValue = Value;
2180 }
2181 }
2182
2183 // Check that we aren't already using an immediate.
2184 // XXX: It's possible for an instruction to have more than one
2185 // immediate operand, but this is not supported yet.
Tom Stellardc5a154d2018-06-28 23:47:12 +00002186 if (ImmReg == R600::ALU_LITERAL_X) {
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002187 if (!Imm.getNode())
2188 return false;
2189 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Imm);
2190 assert(C);
2191 if (C->getZExtValue())
2192 return false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002193 Imm = DAG.getTargetConstant(ImmValue, SDLoc(ParentNode), MVT::i32);
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002194 }
2195 Src = DAG.getRegister(ImmReg, MVT::i32);
2196 return true;
2197 }
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002198 default:
2199 return false;
2200 }
2201}
2202
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00002203/// Fold the instructions after selecting them
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002204SDNode *R600TargetLowering::PostISelFolding(MachineSDNode *Node,
2205 SelectionDAG &DAG) const {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002206 const R600InstrInfo *TII = Subtarget->getInstrInfo();
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002207 if (!Node->isMachineOpcode())
2208 return Node;
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002209
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002210 unsigned Opcode = Node->getMachineOpcode();
2211 SDValue FakeOp;
2212
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002213 std::vector<SDValue> Ops(Node->op_begin(), Node->op_end());
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002214
Tom Stellardc5a154d2018-06-28 23:47:12 +00002215 if (Opcode == R600::DOT_4) {
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002216 int OperandIdx[] = {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002217 TII->getOperandIdx(Opcode, R600::OpName::src0_X),
2218 TII->getOperandIdx(Opcode, R600::OpName::src0_Y),
2219 TII->getOperandIdx(Opcode, R600::OpName::src0_Z),
2220 TII->getOperandIdx(Opcode, R600::OpName::src0_W),
2221 TII->getOperandIdx(Opcode, R600::OpName::src1_X),
2222 TII->getOperandIdx(Opcode, R600::OpName::src1_Y),
2223 TII->getOperandIdx(Opcode, R600::OpName::src1_Z),
2224 TII->getOperandIdx(Opcode, R600::OpName::src1_W)
NAKAMURA Takumi4bb85f92013-10-28 04:07:23 +00002225 };
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002226 int NegIdx[] = {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002227 TII->getOperandIdx(Opcode, R600::OpName::src0_neg_X),
2228 TII->getOperandIdx(Opcode, R600::OpName::src0_neg_Y),
2229 TII->getOperandIdx(Opcode, R600::OpName::src0_neg_Z),
2230 TII->getOperandIdx(Opcode, R600::OpName::src0_neg_W),
2231 TII->getOperandIdx(Opcode, R600::OpName::src1_neg_X),
2232 TII->getOperandIdx(Opcode, R600::OpName::src1_neg_Y),
2233 TII->getOperandIdx(Opcode, R600::OpName::src1_neg_Z),
2234 TII->getOperandIdx(Opcode, R600::OpName::src1_neg_W)
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002235 };
2236 int AbsIdx[] = {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002237 TII->getOperandIdx(Opcode, R600::OpName::src0_abs_X),
2238 TII->getOperandIdx(Opcode, R600::OpName::src0_abs_Y),
2239 TII->getOperandIdx(Opcode, R600::OpName::src0_abs_Z),
2240 TII->getOperandIdx(Opcode, R600::OpName::src0_abs_W),
2241 TII->getOperandIdx(Opcode, R600::OpName::src1_abs_X),
2242 TII->getOperandIdx(Opcode, R600::OpName::src1_abs_Y),
2243 TII->getOperandIdx(Opcode, R600::OpName::src1_abs_Z),
2244 TII->getOperandIdx(Opcode, R600::OpName::src1_abs_W)
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002245 };
2246 for (unsigned i = 0; i < 8; i++) {
2247 if (OperandIdx[i] < 0)
2248 return Node;
2249 SDValue &Src = Ops[OperandIdx[i] - 1];
2250 SDValue &Neg = Ops[NegIdx[i] - 1];
2251 SDValue &Abs = Ops[AbsIdx[i] - 1];
Tom Stellardc5a154d2018-06-28 23:47:12 +00002252 bool HasDst = TII->getOperandIdx(Opcode, R600::OpName::dst) > -1;
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002253 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
2254 if (HasDst)
2255 SelIdx--;
2256 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002257 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, FakeOp, DAG))
2258 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2259 }
Tom Stellardc5a154d2018-06-28 23:47:12 +00002260 } else if (Opcode == R600::REG_SEQUENCE) {
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002261 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) {
2262 SDValue &Src = Ops[i];
2263 if (FoldOperand(Node, i, Src, FakeOp, FakeOp, FakeOp, FakeOp, DAG))
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002264 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2265 }
2266 } else {
2267 if (!TII->hasInstrModifiers(Opcode))
2268 return Node;
2269 int OperandIdx[] = {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002270 TII->getOperandIdx(Opcode, R600::OpName::src0),
2271 TII->getOperandIdx(Opcode, R600::OpName::src1),
2272 TII->getOperandIdx(Opcode, R600::OpName::src2)
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002273 };
2274 int NegIdx[] = {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002275 TII->getOperandIdx(Opcode, R600::OpName::src0_neg),
2276 TII->getOperandIdx(Opcode, R600::OpName::src1_neg),
2277 TII->getOperandIdx(Opcode, R600::OpName::src2_neg)
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002278 };
2279 int AbsIdx[] = {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002280 TII->getOperandIdx(Opcode, R600::OpName::src0_abs),
2281 TII->getOperandIdx(Opcode, R600::OpName::src1_abs),
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002282 -1
2283 };
2284 for (unsigned i = 0; i < 3; i++) {
2285 if (OperandIdx[i] < 0)
2286 return Node;
2287 SDValue &Src = Ops[OperandIdx[i] - 1];
2288 SDValue &Neg = Ops[NegIdx[i] - 1];
2289 SDValue FakeAbs;
2290 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
Tom Stellardc5a154d2018-06-28 23:47:12 +00002291 bool HasDst = TII->getOperandIdx(Opcode, R600::OpName::dst) > -1;
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002292 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
Tom Stellardc5a154d2018-06-28 23:47:12 +00002293 int ImmIdx = TII->getOperandIdx(Opcode, R600::OpName::literal);
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002294 if (HasDst) {
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002295 SelIdx--;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002296 ImmIdx--;
2297 }
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002298 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002299 SDValue &Imm = Ops[ImmIdx];
2300 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, Imm, DAG))
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002301 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2302 }
2303 }
2304
2305 return Node;
2306}