blob: 31c08d0cd2a869b94aaab08018a9f500239dc276 [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- R600ISelLowering.cpp - R600 DAG Lowering Implementation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11/// \brief Custom DAG lowering for R600
12//
13//===----------------------------------------------------------------------===//
14
15#include "R600ISelLowering.h"
Tom Stellard2e59a452014-06-13 01:32:00 +000016#include "AMDGPUFrameLowering.h"
Matt Arsenaultc791f392014-06-23 18:00:31 +000017#include "AMDGPUIntrinsicInfo.h"
Tom Stellard2e59a452014-06-13 01:32:00 +000018#include "AMDGPUSubtarget.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000019#include "R600Defines.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000020#include "R600FrameLowering.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000021#include "R600InstrInfo.h"
22#include "R600MachineFunctionInfo.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000023#include "Utils/AMDGPUBaseInfo.h"
24#include "llvm/ADT/APFloat.h"
25#include "llvm/ADT/APInt.h"
26#include "llvm/ADT/ArrayRef.h"
27#include "llvm/ADT/DenseMap.h"
28#include "llvm/ADT/SmallVector.h"
Tom Stellardacfeebf2013-07-23 01:48:05 +000029#include "llvm/CodeGen/CallingConvLower.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000030#include "llvm/CodeGen/DAGCombine.h"
31#include "llvm/CodeGen/ISDOpcodes.h"
32#include "llvm/CodeGen/MachineBasicBlock.h"
33#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstr.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000035#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000036#include "llvm/CodeGen/MachineMemOperand.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000038#include "llvm/CodeGen/MachineValueType.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000039#include "llvm/CodeGen/SelectionDAG.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000040#include "llvm/IR/Constants.h"
41#include "llvm/IR/DerivedTypes.h"
42#include "llvm/Support/Casting.h"
43#include "llvm/Support/Compiler.h"
44#include "llvm/Support/ErrorHandling.h"
45#include <cassert>
46#include <cstdint>
47#include <iterator>
48#include <utility>
49#include <vector>
Tom Stellard75aadc22012-12-11 21:25:42 +000050
51using namespace llvm;
52
Matt Arsenault43e92fe2016-06-24 06:30:11 +000053R600TargetLowering::R600TargetLowering(const TargetMachine &TM,
54 const R600Subtarget &STI)
Eric Christopher7792e322015-01-30 23:24:40 +000055 : AMDGPUTargetLowering(TM, STI), Gen(STI.getGeneration()) {
Tom Stellard75aadc22012-12-11 21:25:42 +000056 addRegisterClass(MVT::f32, &AMDGPU::R600_Reg32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000057 addRegisterClass(MVT::i32, &AMDGPU::R600_Reg32RegClass);
Tom Stellard0344cdf2013-08-01 15:23:42 +000058 addRegisterClass(MVT::v2f32, &AMDGPU::R600_Reg64RegClass);
59 addRegisterClass(MVT::v2i32, &AMDGPU::R600_Reg64RegClass);
Matt Arsenault71e66762016-05-21 02:27:49 +000060 addRegisterClass(MVT::v4f32, &AMDGPU::R600_Reg128RegClass);
61 addRegisterClass(MVT::v4i32, &AMDGPU::R600_Reg128RegClass);
Tom Stellard0344cdf2013-08-01 15:23:42 +000062
Eric Christopher23a3a7c2015-02-26 00:00:24 +000063 computeRegisterProperties(STI.getRegisterInfo());
Tom Stellard75aadc22012-12-11 21:25:42 +000064
Matt Arsenault71e66762016-05-21 02:27:49 +000065 // Legalize loads and stores to the private address space.
66 setOperationAction(ISD::LOAD, MVT::i32, Custom);
67 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
68 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
69
70 // EXTLOAD should be the same as ZEXTLOAD. It is legal for some address
71 // spaces, so it is custom lowered to handle those where it isn't.
72 for (MVT VT : MVT::integer_valuetypes()) {
73 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
74 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Custom);
75 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Custom);
76
77 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
78 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Custom);
79 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Custom);
80
81 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
82 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Custom);
83 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Custom);
84 }
85
Matt Arsenaultd1097a32016-06-02 19:54:26 +000086 // Workaround for LegalizeDAG asserting on expansion of i1 vector loads.
87 setLoadExtAction(ISD::EXTLOAD, MVT::v2i32, MVT::v2i1, Expand);
88 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i32, MVT::v2i1, Expand);
89 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i32, MVT::v2i1, Expand);
90
91 setLoadExtAction(ISD::EXTLOAD, MVT::v4i32, MVT::v4i1, Expand);
92 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i32, MVT::v4i1, Expand);
93 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i32, MVT::v4i1, Expand);
94
Matt Arsenault71e66762016-05-21 02:27:49 +000095 setOperationAction(ISD::STORE, MVT::i8, Custom);
96 setOperationAction(ISD::STORE, MVT::i32, Custom);
97 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
98 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
99
100 setTruncStoreAction(MVT::i32, MVT::i8, Custom);
101 setTruncStoreAction(MVT::i32, MVT::i16, Custom);
Jan Vesely06200bd2017-01-06 21:00:46 +0000102 // We need to include these since trunc STORES to PRIVATE need
103 // special handling to accommodate RMW
104 setTruncStoreAction(MVT::v2i32, MVT::v2i16, Custom);
105 setTruncStoreAction(MVT::v4i32, MVT::v4i16, Custom);
106 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Custom);
107 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Custom);
108 setTruncStoreAction(MVT::v32i32, MVT::v32i16, Custom);
109 setTruncStoreAction(MVT::v2i32, MVT::v2i8, Custom);
110 setTruncStoreAction(MVT::v4i32, MVT::v4i8, Custom);
111 setTruncStoreAction(MVT::v8i32, MVT::v8i8, Custom);
112 setTruncStoreAction(MVT::v16i32, MVT::v16i8, Custom);
113 setTruncStoreAction(MVT::v32i32, MVT::v32i8, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +0000114
Matt Arsenaultd1097a32016-06-02 19:54:26 +0000115 // Workaround for LegalizeDAG asserting on expansion of i1 vector stores.
116 setTruncStoreAction(MVT::v2i32, MVT::v2i1, Expand);
117 setTruncStoreAction(MVT::v4i32, MVT::v4i1, Expand);
118
Tom Stellard0351ea22013-09-28 02:50:50 +0000119 // Set condition code actions
120 setCondCodeAction(ISD::SETO, MVT::f32, Expand);
121 setCondCodeAction(ISD::SETUO, MVT::f32, Expand);
Tom Stellardcd428182013-09-28 02:50:38 +0000122 setCondCodeAction(ISD::SETLT, MVT::f32, Expand);
Tom Stellard0351ea22013-09-28 02:50:50 +0000123 setCondCodeAction(ISD::SETLE, MVT::f32, Expand);
Tom Stellardcd428182013-09-28 02:50:38 +0000124 setCondCodeAction(ISD::SETOLT, MVT::f32, Expand);
125 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
Tom Stellard0351ea22013-09-28 02:50:50 +0000126 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
127 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
128 setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
129 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
Tom Stellardcd428182013-09-28 02:50:38 +0000130 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
131 setCondCodeAction(ISD::SETULE, MVT::f32, Expand);
132
133 setCondCodeAction(ISD::SETLE, MVT::i32, Expand);
134 setCondCodeAction(ISD::SETLT, MVT::i32, Expand);
135 setCondCodeAction(ISD::SETULE, MVT::i32, Expand);
136 setCondCodeAction(ISD::SETULT, MVT::i32, Expand);
137
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000138 setOperationAction(ISD::FCOS, MVT::f32, Custom);
139 setOperationAction(ISD::FSIN, MVT::f32, Custom);
140
Tom Stellard75aadc22012-12-11 21:25:42 +0000141 setOperationAction(ISD::SETCC, MVT::v4i32, Expand);
Tom Stellard0344cdf2013-08-01 15:23:42 +0000142 setOperationAction(ISD::SETCC, MVT::v2i32, Expand);
Tom Stellard75aadc22012-12-11 21:25:42 +0000143
Tom Stellard492ebea2013-03-08 15:37:07 +0000144 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
145 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
Matt Arsenault1d555c42014-06-23 18:00:55 +0000146 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Tom Stellard75aadc22012-12-11 21:25:42 +0000147
148 setOperationAction(ISD::FSUB, MVT::f32, Expand);
149
Tom Stellard75aadc22012-12-11 21:25:42 +0000150 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
151 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
152
Tom Stellarde8f9f282013-03-08 15:37:05 +0000153 setOperationAction(ISD::SETCC, MVT::i32, Expand);
154 setOperationAction(ISD::SETCC, MVT::f32, Expand);
Tom Stellard75aadc22012-12-11 21:25:42 +0000155 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Custom);
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000156 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Custom);
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000157 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
158 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
Tom Stellard75aadc22012-12-11 21:25:42 +0000159
Tom Stellard53f2f902013-09-05 18:38:03 +0000160 setOperationAction(ISD::SELECT, MVT::i32, Expand);
161 setOperationAction(ISD::SELECT, MVT::f32, Expand);
162 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
Tom Stellard53f2f902013-09-05 18:38:03 +0000163 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
Tom Stellard75aadc22012-12-11 21:25:42 +0000164
Jan Vesely808fff52015-04-30 17:15:56 +0000165 // ADD, SUB overflow.
166 // TODO: turn these into Legal?
167 if (Subtarget->hasCARRY())
168 setOperationAction(ISD::UADDO, MVT::i32, Custom);
169
170 if (Subtarget->hasBORROW())
171 setOperationAction(ISD::USUBO, MVT::i32, Custom);
172
Matt Arsenault4e466652014-04-16 01:41:30 +0000173 // Expand sign extension of vectors
174 if (!Subtarget->hasBFE())
175 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
176
177 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Expand);
178 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Expand);
179
180 if (!Subtarget->hasBFE())
181 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
182 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Expand);
183 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Expand);
184
185 if (!Subtarget->hasBFE())
186 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
187 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Expand);
188 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Expand);
189
190 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
191 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Expand);
192 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i32, Expand);
193
194 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Expand);
195
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000196 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
197
Tom Stellard880a80a2014-06-17 16:53:14 +0000198 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i32, Custom);
199 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f32, Custom);
200 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
201 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
202
203 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i32, Custom);
204 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f32, Custom);
205 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
206 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
207
Jan Vesely25f36272014-06-18 12:27:13 +0000208 // We don't have 64-bit shifts. Thus we need either SHX i64 or SHX_PARTS i32
209 // to be Legal/Custom in order to avoid library calls.
210 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000211 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Jan Veselyecf51332014-06-18 12:27:17 +0000212 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jan Vesely25f36272014-06-18 12:27:13 +0000213
Michel Danzer49812b52013-07-10 16:37:07 +0000214 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
215
Matt Arsenaultc4d3d3a2014-06-23 18:00:49 +0000216 const MVT ScalarIntVTs[] = { MVT::i32, MVT::i64 };
217 for (MVT VT : ScalarIntVTs) {
218 setOperationAction(ISD::ADDC, VT, Expand);
219 setOperationAction(ISD::SUBC, VT, Expand);
220 setOperationAction(ISD::ADDE, VT, Expand);
221 setOperationAction(ISD::SUBE, VT, Expand);
222 }
223
Jan Vesely334f51a2017-01-16 21:20:13 +0000224 // LLVM will expand these to atomic_cmp_swap(0)
225 // and atomic_swap, respectively.
226 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
227 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
228
Tom Stellardfc455472013-08-12 22:33:21 +0000229 setSchedulingPreference(Sched::Source);
Matt Arsenault71e66762016-05-21 02:27:49 +0000230
Matt Arsenault71e66762016-05-21 02:27:49 +0000231 setTargetDAGCombine(ISD::FP_ROUND);
232 setTargetDAGCombine(ISD::FP_TO_SINT);
233 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
234 setTargetDAGCombine(ISD::SELECT_CC);
235 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
Jan Vesely38814fa2016-08-27 19:09:43 +0000236 setTargetDAGCombine(ISD::LOAD);
Tom Stellard75aadc22012-12-11 21:25:42 +0000237}
238
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000239const R600Subtarget *R600TargetLowering::getSubtarget() const {
240 return static_cast<const R600Subtarget *>(Subtarget);
241}
242
Tom Stellardc0f0fba2015-10-01 17:51:29 +0000243static inline bool isEOP(MachineBasicBlock::iterator I) {
Hans Wennborg0dd9ed12016-08-13 01:12:49 +0000244 if (std::next(I) == I->getParent()->end())
245 return false;
Tom Stellardc0f0fba2015-10-01 17:51:29 +0000246 return std::next(I)->getOpcode() == AMDGPU::RETURN;
247}
248
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000249MachineBasicBlock *
250R600TargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
251 MachineBasicBlock *BB) const {
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000252 MachineFunction *MF = BB->getParent();
Tom Stellard75aadc22012-12-11 21:25:42 +0000253 MachineRegisterInfo &MRI = MF->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000254 MachineBasicBlock::iterator I = MI;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000255 const R600InstrInfo *TII = getSubtarget()->getInstrInfo();
Tom Stellard75aadc22012-12-11 21:25:42 +0000256
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000257 switch (MI.getOpcode()) {
Tom Stellardc6f4a292013-08-26 15:05:59 +0000258 default:
Tom Stellard8f9fc202013-11-15 00:12:45 +0000259 // Replace LDS_*_RET instruction that don't have any uses with the
260 // equivalent LDS_*_NORET instruction.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000261 if (TII->isLDSRetInstr(MI.getOpcode())) {
262 int DstIdx = TII->getOperandIdx(MI.getOpcode(), AMDGPU::OpName::dst);
Tom Stellard13c68ef2013-09-05 18:38:09 +0000263 assert(DstIdx != -1);
264 MachineInstrBuilder NewMI;
Aaron Watry1885e532014-09-11 15:02:54 +0000265 // FIXME: getLDSNoRetOp method only handles LDS_1A1D LDS ops. Add
266 // LDS_1A2D support and remove this special case.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000267 if (!MRI.use_empty(MI.getOperand(DstIdx).getReg()) ||
268 MI.getOpcode() == AMDGPU::LDS_CMPST_RET)
Tom Stellard8f9fc202013-11-15 00:12:45 +0000269 return BB;
270
271 NewMI = BuildMI(*BB, I, BB->findDebugLoc(I),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000272 TII->get(AMDGPU::getLDSNoRetOp(MI.getOpcode())));
273 for (unsigned i = 1, e = MI.getNumOperands(); i < e; ++i) {
Diana Picus116bbab2017-01-13 09:58:52 +0000274 NewMI.add(MI.getOperand(i));
Tom Stellardc6f4a292013-08-26 15:05:59 +0000275 }
Tom Stellardc6f4a292013-08-26 15:05:59 +0000276 } else {
277 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
278 }
279 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000280 case AMDGPU::CLAMP_R600: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000281 MachineInstr *NewMI = TII->buildDefaultInstruction(
282 *BB, I, AMDGPU::MOV, MI.getOperand(0).getReg(),
283 MI.getOperand(1).getReg());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000284 TII->addFlag(*NewMI, 0, MO_FLAG_CLAMP);
Tom Stellard75aadc22012-12-11 21:25:42 +0000285 break;
286 }
287
288 case AMDGPU::FABS_R600: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000289 MachineInstr *NewMI = TII->buildDefaultInstruction(
290 *BB, I, AMDGPU::MOV, MI.getOperand(0).getReg(),
291 MI.getOperand(1).getReg());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000292 TII->addFlag(*NewMI, 0, MO_FLAG_ABS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000293 break;
294 }
295
296 case AMDGPU::FNEG_R600: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000297 MachineInstr *NewMI = TII->buildDefaultInstruction(
298 *BB, I, AMDGPU::MOV, MI.getOperand(0).getReg(),
299 MI.getOperand(1).getReg());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000300 TII->addFlag(*NewMI, 0, MO_FLAG_NEG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000301 break;
302 }
303
Tom Stellard75aadc22012-12-11 21:25:42 +0000304 case AMDGPU::MASK_WRITE: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000305 unsigned maskedRegister = MI.getOperand(0).getReg();
Tom Stellard75aadc22012-12-11 21:25:42 +0000306 assert(TargetRegisterInfo::isVirtualRegister(maskedRegister));
307 MachineInstr * defInstr = MRI.getVRegDef(maskedRegister);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000308 TII->addFlag(*defInstr, 0, MO_FLAG_MASK);
Tom Stellard75aadc22012-12-11 21:25:42 +0000309 break;
310 }
311
312 case AMDGPU::MOV_IMM_F32:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000313 TII->buildMovImm(*BB, I, MI.getOperand(0).getReg(), MI.getOperand(1)
314 .getFPImm()
315 ->getValueAPF()
316 .bitcastToAPInt()
317 .getZExtValue());
Tom Stellard75aadc22012-12-11 21:25:42 +0000318 break;
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000319
Tom Stellard75aadc22012-12-11 21:25:42 +0000320 case AMDGPU::MOV_IMM_I32:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000321 TII->buildMovImm(*BB, I, MI.getOperand(0).getReg(),
322 MI.getOperand(1).getImm());
Tom Stellard75aadc22012-12-11 21:25:42 +0000323 break;
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000324
Jan Veselyf97de002016-05-13 20:39:29 +0000325 case AMDGPU::MOV_IMM_GLOBAL_ADDR: {
326 //TODO: Perhaps combine this instruction with the next if possible
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000327 auto MIB = TII->buildDefaultInstruction(
328 *BB, MI, AMDGPU::MOV, MI.getOperand(0).getReg(), AMDGPU::ALU_LITERAL_X);
Jan Veselyf97de002016-05-13 20:39:29 +0000329 int Idx = TII->getOperandIdx(*MIB, AMDGPU::OpName::literal);
330 //TODO: Ugh this is rather ugly
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000331 MIB->getOperand(Idx) = MI.getOperand(1);
Jan Veselyf97de002016-05-13 20:39:29 +0000332 break;
333 }
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000334
Vincent Lejeune0b72f102013-03-05 15:04:55 +0000335 case AMDGPU::CONST_COPY: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000336 MachineInstr *NewMI = TII->buildDefaultInstruction(
337 *BB, MI, AMDGPU::MOV, MI.getOperand(0).getReg(), AMDGPU::ALU_CONST);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000338 TII->setImmOperand(*NewMI, AMDGPU::OpName::src0_sel,
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000339 MI.getOperand(1).getImm());
Vincent Lejeune0b72f102013-03-05 15:04:55 +0000340 break;
341 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000342
343 case AMDGPU::RAT_WRITE_CACHELESS_32_eg:
Tom Stellard0344cdf2013-08-01 15:23:42 +0000344 case AMDGPU::RAT_WRITE_CACHELESS_64_eg:
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000345 case AMDGPU::RAT_WRITE_CACHELESS_128_eg:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000346 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode()))
Diana Picus116bbab2017-01-13 09:58:52 +0000347 .add(MI.getOperand(0))
348 .add(MI.getOperand(1))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000349 .addImm(isEOP(I)); // Set End of program bit
Tom Stellard75aadc22012-12-11 21:25:42 +0000350 break;
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000351
352 case AMDGPU::RAT_STORE_TYPED_eg:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000353 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode()))
Diana Picus116bbab2017-01-13 09:58:52 +0000354 .add(MI.getOperand(0))
355 .add(MI.getOperand(1))
356 .add(MI.getOperand(2))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000357 .addImm(isEOP(I)); // Set End of program bit
Tom Stellarde0e582c2015-10-01 17:51:34 +0000358 break;
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000359
Tom Stellard75aadc22012-12-11 21:25:42 +0000360 case AMDGPU::BRANCH:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000361 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
Diana Picus116bbab2017-01-13 09:58:52 +0000362 .add(MI.getOperand(0));
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000363 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000364
365 case AMDGPU::BRANCH_COND_f32: {
366 MachineInstr *NewMI =
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000367 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
368 AMDGPU::PREDICATE_BIT)
Diana Picus116bbab2017-01-13 09:58:52 +0000369 .add(MI.getOperand(1))
Matt Arsenault44f6d692016-08-13 01:43:46 +0000370 .addImm(AMDGPU::PRED_SETNE)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000371 .addImm(0); // Flags
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000372 TII->addFlag(*NewMI, 0, MO_FLAG_PUSH);
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000373 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
Diana Picus116bbab2017-01-13 09:58:52 +0000374 .add(MI.getOperand(0))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000375 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
Tom Stellard75aadc22012-12-11 21:25:42 +0000376 break;
377 }
378
379 case AMDGPU::BRANCH_COND_i32: {
380 MachineInstr *NewMI =
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000381 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
382 AMDGPU::PREDICATE_BIT)
Diana Picus116bbab2017-01-13 09:58:52 +0000383 .add(MI.getOperand(1))
Matt Arsenault44f6d692016-08-13 01:43:46 +0000384 .addImm(AMDGPU::PRED_SETNE_INT)
Tom Stellard75aadc22012-12-11 21:25:42 +0000385 .addImm(0); // Flags
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000386 TII->addFlag(*NewMI, 0, MO_FLAG_PUSH);
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000387 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
Diana Picus116bbab2017-01-13 09:58:52 +0000388 .add(MI.getOperand(0))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000389 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
Tom Stellard75aadc22012-12-11 21:25:42 +0000390 break;
391 }
392
Tom Stellard75aadc22012-12-11 21:25:42 +0000393 case AMDGPU::EG_ExportSwz:
394 case AMDGPU::R600_ExportSwz: {
Tom Stellard6f1b8652013-01-23 21:39:49 +0000395 // Instruction is left unmodified if its not the last one of its type
396 bool isLastInstructionOfItsType = true;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000397 unsigned InstExportType = MI.getOperand(1).getImm();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000398 for (MachineBasicBlock::iterator NextExportInst = std::next(I),
Tom Stellard6f1b8652013-01-23 21:39:49 +0000399 EndBlock = BB->end(); NextExportInst != EndBlock;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000400 NextExportInst = std::next(NextExportInst)) {
Tom Stellard6f1b8652013-01-23 21:39:49 +0000401 if (NextExportInst->getOpcode() == AMDGPU::EG_ExportSwz ||
402 NextExportInst->getOpcode() == AMDGPU::R600_ExportSwz) {
403 unsigned CurrentInstExportType = NextExportInst->getOperand(1)
404 .getImm();
405 if (CurrentInstExportType == InstExportType) {
406 isLastInstructionOfItsType = false;
407 break;
408 }
409 }
410 }
Tom Stellardc0f0fba2015-10-01 17:51:29 +0000411 bool EOP = isEOP(I);
Tom Stellard6f1b8652013-01-23 21:39:49 +0000412 if (!EOP && !isLastInstructionOfItsType)
Tom Stellard75aadc22012-12-11 21:25:42 +0000413 return BB;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000414 unsigned CfInst = (MI.getOpcode() == AMDGPU::EG_ExportSwz) ? 84 : 40;
415 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode()))
Diana Picus116bbab2017-01-13 09:58:52 +0000416 .add(MI.getOperand(0))
417 .add(MI.getOperand(1))
418 .add(MI.getOperand(2))
419 .add(MI.getOperand(3))
420 .add(MI.getOperand(4))
421 .add(MI.getOperand(5))
422 .add(MI.getOperand(6))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000423 .addImm(CfInst)
424 .addImm(EOP);
Tom Stellard75aadc22012-12-11 21:25:42 +0000425 break;
426 }
Jakob Stoklund Olesenfdc37672013-02-05 17:53:52 +0000427 case AMDGPU::RETURN: {
Jakob Stoklund Olesenfdc37672013-02-05 17:53:52 +0000428 return BB;
429 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000430 }
431
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000432 MI.eraseFromParent();
Tom Stellard75aadc22012-12-11 21:25:42 +0000433 return BB;
434}
435
436//===----------------------------------------------------------------------===//
437// Custom DAG Lowering Operations
438//===----------------------------------------------------------------------===//
439
Tom Stellard75aadc22012-12-11 21:25:42 +0000440SDValue R600TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Tom Stellardc026e8b2013-06-28 15:47:08 +0000441 MachineFunction &MF = DAG.getMachineFunction();
442 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
Tom Stellard75aadc22012-12-11 21:25:42 +0000443 switch (Op.getOpcode()) {
444 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellard880a80a2014-06-17 16:53:14 +0000445 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
446 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Jan Vesely25f36272014-06-18 12:27:13 +0000447 case ISD::SHL_PARTS: return LowerSHLParts(Op, DAG);
Jan Veselyecf51332014-06-18 12:27:17 +0000448 case ISD::SRA_PARTS:
Jan Vesely900ff2e2014-06-18 12:27:15 +0000449 case ISD::SRL_PARTS: return LowerSRXParts(Op, DAG);
Jan Vesely808fff52015-04-30 17:15:56 +0000450 case ISD::UADDO: return LowerUADDSUBO(Op, DAG, ISD::ADD, AMDGPUISD::CARRY);
451 case ISD::USUBO: return LowerUADDSUBO(Op, DAG, ISD::SUB, AMDGPUISD::BORROW);
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000452 case ISD::FCOS:
453 case ISD::FSIN: return LowerTrig(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000454 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000455 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000456 case ISD::LOAD: {
457 SDValue Result = LowerLOAD(Op, DAG);
458 assert((!Result.getNode() ||
459 Result.getNode()->getNumValues() == 2) &&
460 "Load should return a value and a chain");
461 return Result;
462 }
463
Matt Arsenault1d555c42014-06-23 18:00:55 +0000464 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellardc026e8b2013-06-28 15:47:08 +0000465 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
Matt Arsenault81d06012016-03-07 21:10:13 +0000466 case ISD::FrameIndex: return lowerFrameIndex(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000467 case ISD::INTRINSIC_VOID: {
468 SDValue Chain = Op.getOperand(0);
469 unsigned IntrinsicID =
470 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
471 switch (IntrinsicID) {
Matt Arsenault82e5e1e2016-07-15 21:27:08 +0000472 case AMDGPUIntrinsic::r600_store_swizzle: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000473 SDLoc DL(Op);
Vincent Lejeuned80bc152013-02-14 16:55:06 +0000474 const SDValue Args[8] = {
475 Chain,
476 Op.getOperand(2), // Export Value
477 Op.getOperand(3), // ArrayBase
478 Op.getOperand(4), // Type
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000479 DAG.getConstant(0, DL, MVT::i32), // SWZ_X
480 DAG.getConstant(1, DL, MVT::i32), // SWZ_Y
481 DAG.getConstant(2, DL, MVT::i32), // SWZ_Z
482 DAG.getConstant(3, DL, MVT::i32) // SWZ_W
Vincent Lejeuned80bc152013-02-14 16:55:06 +0000483 };
Matt Arsenault7bee6ac2016-12-05 20:23:10 +0000484 return DAG.getNode(AMDGPUISD::R600_EXPORT, DL, Op.getValueType(), Args);
Tom Stellard75aadc22012-12-11 21:25:42 +0000485 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000486
Tom Stellard75aadc22012-12-11 21:25:42 +0000487 // default for switch(IntrinsicID)
488 default: break;
489 }
490 // break out of case ISD::INTRINSIC_VOID in switch(Op.getOpcode())
491 break;
492 }
493 case ISD::INTRINSIC_WO_CHAIN: {
494 unsigned IntrinsicID =
495 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
496 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000497 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000498 switch(IntrinsicID) {
499 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Matt Arsenault59bd3012016-01-22 19:00:09 +0000500 case AMDGPUIntrinsic::r600_tex:
Matt Arsenaultf9245b72016-07-22 17:01:25 +0000501 case AMDGPUIntrinsic::r600_texc: {
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000502 unsigned TextureOp;
503 switch (IntrinsicID) {
Matt Arsenault59bd3012016-01-22 19:00:09 +0000504 case AMDGPUIntrinsic::r600_tex:
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000505 TextureOp = 0;
506 break;
Matt Arsenault59bd3012016-01-22 19:00:09 +0000507 case AMDGPUIntrinsic::r600_texc:
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000508 TextureOp = 1;
509 break;
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000510 default:
Matt Arsenault60a750f2016-07-26 21:03:38 +0000511 llvm_unreachable("unhandled texture operation");
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000512 }
513
514 SDValue TexArgs[19] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000515 DAG.getConstant(TextureOp, DL, MVT::i32),
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000516 Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000517 DAG.getConstant(0, DL, MVT::i32),
518 DAG.getConstant(1, DL, MVT::i32),
519 DAG.getConstant(2, DL, MVT::i32),
520 DAG.getConstant(3, DL, MVT::i32),
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000521 Op.getOperand(2),
522 Op.getOperand(3),
523 Op.getOperand(4),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000524 DAG.getConstant(0, DL, MVT::i32),
525 DAG.getConstant(1, DL, MVT::i32),
526 DAG.getConstant(2, DL, MVT::i32),
527 DAG.getConstant(3, DL, MVT::i32),
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000528 Op.getOperand(5),
529 Op.getOperand(6),
530 Op.getOperand(7),
531 Op.getOperand(8),
532 Op.getOperand(9),
533 Op.getOperand(10)
534 };
Craig Topper48d114b2014-04-26 18:35:24 +0000535 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, MVT::v4f32, TexArgs);
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000536 }
Matt Arsenaultca7f5702016-07-14 05:47:17 +0000537 case AMDGPUIntrinsic::r600_dot4: {
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000538 SDValue Args[8] = {
539 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000540 DAG.getConstant(0, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000541 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000542 DAG.getConstant(0, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000543 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000544 DAG.getConstant(1, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000545 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000546 DAG.getConstant(1, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000547 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000548 DAG.getConstant(2, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000549 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000550 DAG.getConstant(2, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000551 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000552 DAG.getConstant(3, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000553 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000554 DAG.getConstant(3, DL, MVT::i32))
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000555 };
Craig Topper48d114b2014-04-26 18:35:24 +0000556 return DAG.getNode(AMDGPUISD::DOT4, DL, MVT::f32, Args);
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000557 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000558
Jan Vesely2fa28c32016-07-10 21:20:29 +0000559 case Intrinsic::r600_implicitarg_ptr: {
560 MVT PtrVT = getPointerTy(DAG.getDataLayout(), AMDGPUAS::PARAM_I_ADDRESS);
561 uint32_t ByteOffset = getImplicitParameterOffset(MFI, FIRST_IMPLICIT);
562 return DAG.getConstant(ByteOffset, DL, PtrVT);
563 }
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000564 case Intrinsic::r600_read_ngroups_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000565 return LowerImplicitParameter(DAG, VT, DL, 0);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000566 case Intrinsic::r600_read_ngroups_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000567 return LowerImplicitParameter(DAG, VT, DL, 1);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000568 case Intrinsic::r600_read_ngroups_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000569 return LowerImplicitParameter(DAG, VT, DL, 2);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000570 case Intrinsic::r600_read_global_size_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000571 return LowerImplicitParameter(DAG, VT, DL, 3);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000572 case Intrinsic::r600_read_global_size_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000573 return LowerImplicitParameter(DAG, VT, DL, 4);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000574 case Intrinsic::r600_read_global_size_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000575 return LowerImplicitParameter(DAG, VT, DL, 5);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000576 case Intrinsic::r600_read_local_size_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000577 return LowerImplicitParameter(DAG, VT, DL, 6);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000578 case Intrinsic::r600_read_local_size_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000579 return LowerImplicitParameter(DAG, VT, DL, 7);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000580 case Intrinsic::r600_read_local_size_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000581 return LowerImplicitParameter(DAG, VT, DL, 8);
582
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000583 case Intrinsic::r600_read_tgid_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000584 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
585 AMDGPU::T1_X, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000586 case Intrinsic::r600_read_tgid_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000587 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
588 AMDGPU::T1_Y, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000589 case Intrinsic::r600_read_tgid_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000590 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
591 AMDGPU::T1_Z, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000592 case Intrinsic::r600_read_tidig_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000593 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
594 AMDGPU::T0_X, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000595 case Intrinsic::r600_read_tidig_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000596 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
597 AMDGPU::T0_Y, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000598 case Intrinsic::r600_read_tidig_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000599 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
600 AMDGPU::T0_Z, VT);
Matt Arsenaultbef34e22016-01-22 21:30:34 +0000601
Matt Arsenault09b2c4a2016-07-15 21:26:52 +0000602 case Intrinsic::r600_recipsqrt_ieee:
603 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
Matt Arsenaultbef34e22016-01-22 21:30:34 +0000604
Matt Arsenault09b2c4a2016-07-15 21:26:52 +0000605 case Intrinsic::r600_recipsqrt_clamped:
606 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
Tom Stellard75aadc22012-12-11 21:25:42 +0000607 }
Matt Arsenault09b2c4a2016-07-15 21:26:52 +0000608
Tom Stellard75aadc22012-12-11 21:25:42 +0000609 // break out of case ISD::INTRINSIC_WO_CHAIN in switch(Op.getOpcode())
610 break;
611 }
612 } // end switch(Op.getOpcode())
613 return SDValue();
614}
615
616void R600TargetLowering::ReplaceNodeResults(SDNode *N,
617 SmallVectorImpl<SDValue> &Results,
618 SelectionDAG &DAG) const {
619 switch (N->getOpcode()) {
Matt Arsenaultd125d742014-03-27 17:23:24 +0000620 default:
621 AMDGPUTargetLowering::ReplaceNodeResults(N, Results, DAG);
622 return;
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000623 case ISD::FP_TO_UINT:
624 if (N->getValueType(0) == MVT::i1) {
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000625 Results.push_back(lowerFP_TO_UINT(N->getOperand(0), DAG));
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000626 return;
627 }
Justin Bognerb03fd122016-08-17 05:10:15 +0000628 // Since we don't care about out of bounds values we can use FP_TO_SINT for
629 // uints too. The DAGLegalizer code for uint considers some extra cases
630 // which are not necessary here.
631 LLVM_FALLTHROUGH;
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000632 case ISD::FP_TO_SINT: {
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000633 if (N->getValueType(0) == MVT::i1) {
634 Results.push_back(lowerFP_TO_SINT(N->getOperand(0), DAG));
635 return;
636 }
637
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000638 SDValue Result;
639 if (expandFP_TO_SINT(N, Result, DAG))
640 Results.push_back(Result);
Tom Stellard365366f2013-01-23 02:09:06 +0000641 return;
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000642 }
Jan Vesely343cd6f02014-06-22 21:43:01 +0000643 case ISD::SDIVREM: {
644 SDValue Op = SDValue(N, 1);
645 SDValue RES = LowerSDIVREM(Op, DAG);
646 Results.push_back(RES);
647 Results.push_back(RES.getValue(1));
648 break;
649 }
650 case ISD::UDIVREM: {
651 SDValue Op = SDValue(N, 0);
Tom Stellardbf69d762014-11-15 01:07:53 +0000652 LowerUDIVREM64(Op, DAG, Results);
Jan Vesely343cd6f02014-06-22 21:43:01 +0000653 break;
654 }
655 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000656}
657
Tom Stellard880a80a2014-06-17 16:53:14 +0000658SDValue R600TargetLowering::vectorToVerticalVector(SelectionDAG &DAG,
659 SDValue Vector) const {
Tom Stellard880a80a2014-06-17 16:53:14 +0000660 SDLoc DL(Vector);
661 EVT VecVT = Vector.getValueType();
662 EVT EltVT = VecVT.getVectorElementType();
663 SmallVector<SDValue, 8> Args;
664
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000665 for (unsigned i = 0, e = VecVT.getVectorNumElements(); i != e; ++i) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000666 Args.push_back(DAG.getNode(
667 ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Vector,
668 DAG.getConstant(i, DL, getVectorIdxTy(DAG.getDataLayout()))));
Tom Stellard880a80a2014-06-17 16:53:14 +0000669 }
670
671 return DAG.getNode(AMDGPUISD::BUILD_VERTICAL_VECTOR, DL, VecVT, Args);
672}
673
674SDValue R600TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
675 SelectionDAG &DAG) const {
Tom Stellard880a80a2014-06-17 16:53:14 +0000676 SDLoc DL(Op);
677 SDValue Vector = Op.getOperand(0);
678 SDValue Index = Op.getOperand(1);
679
680 if (isa<ConstantSDNode>(Index) ||
681 Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR)
682 return Op;
683
684 Vector = vectorToVerticalVector(DAG, Vector);
685 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, Op.getValueType(),
686 Vector, Index);
687}
688
689SDValue R600TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
690 SelectionDAG &DAG) const {
691 SDLoc DL(Op);
692 SDValue Vector = Op.getOperand(0);
693 SDValue Value = Op.getOperand(1);
694 SDValue Index = Op.getOperand(2);
695
696 if (isa<ConstantSDNode>(Index) ||
697 Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR)
698 return Op;
699
700 Vector = vectorToVerticalVector(DAG, Vector);
701 SDValue Insert = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, Op.getValueType(),
702 Vector, Value, Index);
703 return vectorToVerticalVector(DAG, Insert);
704}
705
Tom Stellard27233b72016-05-02 18:05:17 +0000706SDValue R600TargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
707 SDValue Op,
708 SelectionDAG &DAG) const {
Tom Stellard27233b72016-05-02 18:05:17 +0000709 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
710 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
711 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
712
713 const DataLayout &DL = DAG.getDataLayout();
714 const GlobalValue *GV = GSD->getGlobal();
Tom Stellard27233b72016-05-02 18:05:17 +0000715 MVT ConstPtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
716
Jan Veselyf97de002016-05-13 20:39:29 +0000717 SDValue GA = DAG.getTargetGlobalAddress(GV, SDLoc(GSD), ConstPtrVT);
718 return DAG.getNode(AMDGPUISD::CONST_DATA_PTR, SDLoc(GSD), ConstPtrVT, GA);
Tom Stellard27233b72016-05-02 18:05:17 +0000719}
720
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000721SDValue R600TargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
722 // On hw >= R700, COS/SIN input must be between -1. and 1.
723 // Thus we lower them to TRIG ( FRACT ( x / 2Pi + 0.5) - 0.5)
724 EVT VT = Op.getValueType();
725 SDValue Arg = Op.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000726 SDLoc DL(Op);
Sanjay Patela2607012015-09-16 16:31:21 +0000727
728 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000729 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
730 DAG.getNode(ISD::FADD, DL, VT,
731 DAG.getNode(ISD::FMUL, DL, VT, Arg,
732 DAG.getConstantFP(0.15915494309, DL, MVT::f32)),
733 DAG.getConstantFP(0.5, DL, MVT::f32)));
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000734 unsigned TrigNode;
735 switch (Op.getOpcode()) {
736 case ISD::FCOS:
737 TrigNode = AMDGPUISD::COS_HW;
738 break;
739 case ISD::FSIN:
740 TrigNode = AMDGPUISD::SIN_HW;
741 break;
742 default:
743 llvm_unreachable("Wrong trig opcode");
744 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000745 SDValue TrigVal = DAG.getNode(TrigNode, DL, VT,
746 DAG.getNode(ISD::FADD, DL, VT, FractPart,
747 DAG.getConstantFP(-0.5, DL, MVT::f32)));
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000748 if (Gen >= R600Subtarget::R700)
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000749 return TrigVal;
750 // On R600 hw, COS/SIN input must be between -Pi and Pi.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000751 return DAG.getNode(ISD::FMUL, DL, VT, TrigVal,
752 DAG.getConstantFP(3.14159265359, DL, MVT::f32));
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000753}
754
Jan Vesely25f36272014-06-18 12:27:13 +0000755SDValue R600TargetLowering::LowerSHLParts(SDValue Op, SelectionDAG &DAG) const {
756 SDLoc DL(Op);
757 EVT VT = Op.getValueType();
758
759 SDValue Lo = Op.getOperand(0);
760 SDValue Hi = Op.getOperand(1);
761 SDValue Shift = Op.getOperand(2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000762 SDValue Zero = DAG.getConstant(0, DL, VT);
763 SDValue One = DAG.getConstant(1, DL, VT);
Jan Vesely25f36272014-06-18 12:27:13 +0000764
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000765 SDValue Width = DAG.getConstant(VT.getSizeInBits(), DL, VT);
766 SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, DL, VT);
Jan Vesely25f36272014-06-18 12:27:13 +0000767 SDValue BigShift = DAG.getNode(ISD::SUB, DL, VT, Shift, Width);
768 SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift);
769
770 // The dance around Width1 is necessary for 0 special case.
771 // Without it the CompShift might be 32, producing incorrect results in
772 // Overflow. So we do the shift in two steps, the alternative is to
773 // add a conditional to filter the special case.
774
775 SDValue Overflow = DAG.getNode(ISD::SRL, DL, VT, Lo, CompShift);
776 Overflow = DAG.getNode(ISD::SRL, DL, VT, Overflow, One);
777
778 SDValue HiSmall = DAG.getNode(ISD::SHL, DL, VT, Hi, Shift);
779 HiSmall = DAG.getNode(ISD::OR, DL, VT, HiSmall, Overflow);
780 SDValue LoSmall = DAG.getNode(ISD::SHL, DL, VT, Lo, Shift);
781
782 SDValue HiBig = DAG.getNode(ISD::SHL, DL, VT, Lo, BigShift);
783 SDValue LoBig = Zero;
784
785 Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT);
786 Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT);
787
788 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi);
789}
790
Jan Vesely900ff2e2014-06-18 12:27:15 +0000791SDValue R600TargetLowering::LowerSRXParts(SDValue Op, SelectionDAG &DAG) const {
792 SDLoc DL(Op);
793 EVT VT = Op.getValueType();
794
795 SDValue Lo = Op.getOperand(0);
796 SDValue Hi = Op.getOperand(1);
797 SDValue Shift = Op.getOperand(2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000798 SDValue Zero = DAG.getConstant(0, DL, VT);
799 SDValue One = DAG.getConstant(1, DL, VT);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000800
Jan Veselyecf51332014-06-18 12:27:17 +0000801 const bool SRA = Op.getOpcode() == ISD::SRA_PARTS;
802
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000803 SDValue Width = DAG.getConstant(VT.getSizeInBits(), DL, VT);
804 SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, DL, VT);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000805 SDValue BigShift = DAG.getNode(ISD::SUB, DL, VT, Shift, Width);
806 SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift);
807
808 // The dance around Width1 is necessary for 0 special case.
809 // Without it the CompShift might be 32, producing incorrect results in
810 // Overflow. So we do the shift in two steps, the alternative is to
811 // add a conditional to filter the special case.
812
813 SDValue Overflow = DAG.getNode(ISD::SHL, DL, VT, Hi, CompShift);
814 Overflow = DAG.getNode(ISD::SHL, DL, VT, Overflow, One);
815
Jan Veselyecf51332014-06-18 12:27:17 +0000816 SDValue HiSmall = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, Shift);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000817 SDValue LoSmall = DAG.getNode(ISD::SRL, DL, VT, Lo, Shift);
818 LoSmall = DAG.getNode(ISD::OR, DL, VT, LoSmall, Overflow);
819
Jan Veselyecf51332014-06-18 12:27:17 +0000820 SDValue LoBig = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, BigShift);
821 SDValue HiBig = SRA ? DAG.getNode(ISD::SRA, DL, VT, Hi, Width1) : Zero;
Jan Vesely900ff2e2014-06-18 12:27:15 +0000822
823 Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT);
824 Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT);
825
826 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi);
827}
828
Jan Vesely808fff52015-04-30 17:15:56 +0000829SDValue R600TargetLowering::LowerUADDSUBO(SDValue Op, SelectionDAG &DAG,
830 unsigned mainop, unsigned ovf) const {
831 SDLoc DL(Op);
832 EVT VT = Op.getValueType();
833
834 SDValue Lo = Op.getOperand(0);
835 SDValue Hi = Op.getOperand(1);
836
837 SDValue OVF = DAG.getNode(ovf, DL, VT, Lo, Hi);
838 // Extend sign.
839 OVF = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, OVF,
840 DAG.getValueType(MVT::i1));
841
842 SDValue Res = DAG.getNode(mainop, DL, VT, Lo, Hi);
843
844 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT, VT), Res, OVF);
845}
846
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000847SDValue R600TargetLowering::lowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000848 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000849 return DAG.getNode(
850 ISD::SETCC,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000851 DL,
Tom Stellard75aadc22012-12-11 21:25:42 +0000852 MVT::i1,
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000853 Op, DAG.getConstantFP(1.0f, DL, MVT::f32),
854 DAG.getCondCode(ISD::SETEQ));
855}
856
857SDValue R600TargetLowering::lowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const {
858 SDLoc DL(Op);
859 return DAG.getNode(
860 ISD::SETCC,
861 DL,
862 MVT::i1,
863 Op, DAG.getConstantFP(-1.0f, DL, MVT::f32),
864 DAG.getCondCode(ISD::SETEQ));
Tom Stellard75aadc22012-12-11 21:25:42 +0000865}
866
Tom Stellard75aadc22012-12-11 21:25:42 +0000867SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000868 const SDLoc &DL,
Tom Stellard75aadc22012-12-11 21:25:42 +0000869 unsigned DwordOffset) const {
870 unsigned ByteOffset = DwordOffset * 4;
871 PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
Tom Stellard1e803092013-07-23 01:48:18 +0000872 AMDGPUAS::CONSTANT_BUFFER_0);
Tom Stellard75aadc22012-12-11 21:25:42 +0000873
874 // We shouldn't be using an offset wider than 16-bits for implicit parameters.
875 assert(isInt<16>(ByteOffset));
876
877 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000878 DAG.getConstant(ByteOffset, DL, MVT::i32), // PTR
Justin Lebar9c375812016-07-15 18:27:10 +0000879 MachinePointerInfo(ConstantPointerNull::get(PtrType)));
Tom Stellard75aadc22012-12-11 21:25:42 +0000880}
881
Tom Stellard75aadc22012-12-11 21:25:42 +0000882bool R600TargetLowering::isZero(SDValue Op) const {
883 if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) {
884 return Cst->isNullValue();
885 } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){
886 return CstFP->isZero();
887 } else {
888 return false;
889 }
890}
891
Matt Arsenault6b6a2c32016-03-11 08:00:27 +0000892bool R600TargetLowering::isHWTrueValue(SDValue Op) const {
893 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
894 return CFP->isExactlyValue(1.0);
895 }
896 return isAllOnesConstant(Op);
897}
898
899bool R600TargetLowering::isHWFalseValue(SDValue Op) const {
900 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
901 return CFP->getValueAPF().isZero();
902 }
903 return isNullConstant(Op);
904}
905
Tom Stellard75aadc22012-12-11 21:25:42 +0000906SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000907 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000908 EVT VT = Op.getValueType();
909
910 SDValue LHS = Op.getOperand(0);
911 SDValue RHS = Op.getOperand(1);
912 SDValue True = Op.getOperand(2);
913 SDValue False = Op.getOperand(3);
914 SDValue CC = Op.getOperand(4);
915 SDValue Temp;
916
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +0000917 if (VT == MVT::f32) {
918 DAGCombinerInfo DCI(DAG, AfterLegalizeVectorOps, true, nullptr);
919 SDValue MinMax = CombineFMinMaxLegacy(DL, VT, LHS, RHS, True, False, CC, DCI);
920 if (MinMax)
921 return MinMax;
922 }
923
Tom Stellard75aadc22012-12-11 21:25:42 +0000924 // LHS and RHS are guaranteed to be the same value type
925 EVT CompareVT = LHS.getValueType();
926
927 // Check if we can lower this to a native operation.
928
Tom Stellard2add82d2013-03-08 15:37:09 +0000929 // Try to lower to a SET* instruction:
930 //
931 // SET* can match the following patterns:
932 //
Tom Stellardcd428182013-09-28 02:50:38 +0000933 // select_cc f32, f32, -1, 0, cc_supported
934 // select_cc f32, f32, 1.0f, 0.0f, cc_supported
935 // select_cc i32, i32, -1, 0, cc_supported
Tom Stellard2add82d2013-03-08 15:37:09 +0000936 //
937
938 // Move hardware True/False values to the correct operand.
Tom Stellardcd428182013-09-28 02:50:38 +0000939 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
940 ISD::CondCode InverseCC =
941 ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
Tom Stellard5694d302013-09-28 02:50:43 +0000942 if (isHWTrueValue(False) && isHWFalseValue(True)) {
943 if (isCondCodeLegal(InverseCC, CompareVT.getSimpleVT())) {
944 std::swap(False, True);
945 CC = DAG.getCondCode(InverseCC);
946 } else {
947 ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InverseCC);
948 if (isCondCodeLegal(SwapInvCC, CompareVT.getSimpleVT())) {
949 std::swap(False, True);
950 std::swap(LHS, RHS);
951 CC = DAG.getCondCode(SwapInvCC);
952 }
953 }
Tom Stellard2add82d2013-03-08 15:37:09 +0000954 }
955
956 if (isHWTrueValue(True) && isHWFalseValue(False) &&
957 (CompareVT == VT || VT == MVT::i32)) {
958 // This can be matched by a SET* instruction.
959 return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
960 }
961
Tom Stellard75aadc22012-12-11 21:25:42 +0000962 // Try to lower to a CND* instruction:
Tom Stellard2add82d2013-03-08 15:37:09 +0000963 //
964 // CND* can match the following patterns:
965 //
Tom Stellardcd428182013-09-28 02:50:38 +0000966 // select_cc f32, 0.0, f32, f32, cc_supported
967 // select_cc f32, 0.0, i32, i32, cc_supported
968 // select_cc i32, 0, f32, f32, cc_supported
969 // select_cc i32, 0, i32, i32, cc_supported
Tom Stellard2add82d2013-03-08 15:37:09 +0000970 //
Tom Stellardcd428182013-09-28 02:50:38 +0000971
972 // Try to move the zero value to the RHS
973 if (isZero(LHS)) {
974 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
975 // Try swapping the operands
976 ISD::CondCode CCSwapped = ISD::getSetCCSwappedOperands(CCOpcode);
977 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
978 std::swap(LHS, RHS);
979 CC = DAG.getCondCode(CCSwapped);
980 } else {
981 // Try inverting the conditon and then swapping the operands
982 ISD::CondCode CCInv = ISD::getSetCCInverse(CCOpcode, CompareVT.isInteger());
983 CCSwapped = ISD::getSetCCSwappedOperands(CCInv);
984 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
985 std::swap(True, False);
986 std::swap(LHS, RHS);
987 CC = DAG.getCondCode(CCSwapped);
988 }
989 }
990 }
991 if (isZero(RHS)) {
992 SDValue Cond = LHS;
993 SDValue Zero = RHS;
Tom Stellard75aadc22012-12-11 21:25:42 +0000994 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
995 if (CompareVT != VT) {
996 // Bitcast True / False to the correct types. This will end up being
997 // a nop, but it allows us to define only a single pattern in the
998 // .TD files for each CND* instruction rather than having to have
999 // one pattern for integer True/False and one for fp True/False
1000 True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
1001 False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
1002 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001003
1004 switch (CCOpcode) {
1005 case ISD::SETONE:
1006 case ISD::SETUNE:
1007 case ISD::SETNE:
Tom Stellard75aadc22012-12-11 21:25:42 +00001008 CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
1009 Temp = True;
1010 True = False;
1011 False = Temp;
1012 break;
1013 default:
1014 break;
1015 }
1016 SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
1017 Cond, Zero,
1018 True, False,
1019 DAG.getCondCode(CCOpcode));
1020 return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
1021 }
1022
Tom Stellard75aadc22012-12-11 21:25:42 +00001023 // If we make it this for it means we have no native instructions to handle
1024 // this SELECT_CC, so we must lower it.
1025 SDValue HWTrue, HWFalse;
1026
1027 if (CompareVT == MVT::f32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001028 HWTrue = DAG.getConstantFP(1.0f, DL, CompareVT);
1029 HWFalse = DAG.getConstantFP(0.0f, DL, CompareVT);
Tom Stellard75aadc22012-12-11 21:25:42 +00001030 } else if (CompareVT == MVT::i32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001031 HWTrue = DAG.getConstant(-1, DL, CompareVT);
1032 HWFalse = DAG.getConstant(0, DL, CompareVT);
Tom Stellard75aadc22012-12-11 21:25:42 +00001033 }
1034 else {
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +00001035 llvm_unreachable("Unhandled value type in LowerSELECT_CC");
Tom Stellard75aadc22012-12-11 21:25:42 +00001036 }
1037
1038 // Lower this unsupported SELECT_CC into a combination of two supported
1039 // SELECT_CC operations.
1040 SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
1041
1042 return DAG.getNode(ISD::SELECT_CC, DL, VT,
1043 Cond, HWFalse,
1044 True, False,
1045 DAG.getCondCode(ISD::SETNE));
1046}
1047
Alp Tokercb402912014-01-24 17:20:08 +00001048/// LLVM generates byte-addressed pointers. For indirect addressing, we need to
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001049/// convert these pointers to a register index. Each register holds
1050/// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
1051/// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
1052/// for indirect addressing.
1053SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
1054 unsigned StackWidth,
1055 SelectionDAG &DAG) const {
1056 unsigned SRLPad;
1057 switch(StackWidth) {
1058 case 1:
1059 SRLPad = 2;
1060 break;
1061 case 2:
1062 SRLPad = 3;
1063 break;
1064 case 4:
1065 SRLPad = 4;
1066 break;
1067 default: llvm_unreachable("Invalid stack width");
1068 }
1069
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001070 SDLoc DL(Ptr);
1071 return DAG.getNode(ISD::SRL, DL, Ptr.getValueType(), Ptr,
1072 DAG.getConstant(SRLPad, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001073}
1074
1075void R600TargetLowering::getStackAddress(unsigned StackWidth,
1076 unsigned ElemIdx,
1077 unsigned &Channel,
1078 unsigned &PtrIncr) const {
1079 switch (StackWidth) {
1080 default:
1081 case 1:
1082 Channel = 0;
1083 if (ElemIdx > 0) {
1084 PtrIncr = 1;
1085 } else {
1086 PtrIncr = 0;
1087 }
1088 break;
1089 case 2:
1090 Channel = ElemIdx % 2;
1091 if (ElemIdx == 2) {
1092 PtrIncr = 1;
1093 } else {
1094 PtrIncr = 0;
1095 }
1096 break;
1097 case 4:
1098 Channel = ElemIdx;
1099 PtrIncr = 0;
1100 break;
1101 }
1102}
1103
Matt Arsenault95245662016-02-11 05:32:46 +00001104SDValue R600TargetLowering::lowerPrivateTruncStore(StoreSDNode *Store,
1105 SelectionDAG &DAG) const {
1106 SDLoc DL(Store);
Jan Vesely06200bd2017-01-06 21:00:46 +00001107 //TODO: Who creates the i8 stores?
1108 assert(Store->isTruncatingStore()
1109 || Store->getValue().getValueType() == MVT::i8);
1110 assert(Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS);
Tom Stellard75aadc22012-12-11 21:25:42 +00001111
Jan Vesely06200bd2017-01-06 21:00:46 +00001112 SDValue Mask;
Matt Arsenault95245662016-02-11 05:32:46 +00001113 if (Store->getMemoryVT() == MVT::i8) {
Jan Vesely06200bd2017-01-06 21:00:46 +00001114 assert(Store->getAlignment() >= 1);
1115 Mask = DAG.getConstant(0xff, DL, MVT::i32);
Matt Arsenault95245662016-02-11 05:32:46 +00001116 } else if (Store->getMemoryVT() == MVT::i16) {
Jan Vesely06200bd2017-01-06 21:00:46 +00001117 assert(Store->getAlignment() >= 2);
1118 Mask = DAG.getConstant(0xffff, DL, MVT::i32);;
1119 } else {
1120 llvm_unreachable("Unsupported private trunc store");
Matt Arsenault95245662016-02-11 05:32:46 +00001121 }
1122
Jan Veselyf1705042017-01-20 21:24:26 +00001123 SDValue OldChain = Store->getChain();
1124 bool VectorTrunc = (OldChain.getOpcode() == AMDGPUISD::DUMMY_CHAIN);
1125 // Skip dummy
1126 SDValue Chain = VectorTrunc ? OldChain->getOperand(0) : OldChain;
Matt Arsenault95245662016-02-11 05:32:46 +00001127 SDValue BasePtr = Store->getBasePtr();
Jan Vesely06200bd2017-01-06 21:00:46 +00001128 SDValue Offset = Store->getOffset();
Matt Arsenault95245662016-02-11 05:32:46 +00001129 EVT MemVT = Store->getMemoryVT();
1130
Jan Vesely06200bd2017-01-06 21:00:46 +00001131 SDValue LoadPtr = BasePtr;
1132 if (!Offset.isUndef()) {
1133 LoadPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, Offset);
1134 }
Matt Arsenault95245662016-02-11 05:32:46 +00001135
Jan Vesely06200bd2017-01-06 21:00:46 +00001136 // Get dword location
1137 // TODO: this should be eliminated by the future SHR ptr, 2
1138 SDValue Ptr = DAG.getNode(ISD::AND, DL, MVT::i32, LoadPtr,
1139 DAG.getConstant(0xfffffffc, DL, MVT::i32));
1140
1141 // Load dword
1142 // TODO: can we be smarter about machine pointer info?
1143 SDValue Dst = DAG.getLoad(MVT::i32, DL, Chain, Ptr, MachinePointerInfo());
1144
1145 Chain = Dst.getValue(1);
1146
1147 // Get offset in dword
1148 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, LoadPtr,
Matt Arsenault95245662016-02-11 05:32:46 +00001149 DAG.getConstant(0x3, DL, MVT::i32));
1150
Jan Vesely06200bd2017-01-06 21:00:46 +00001151 // Convert byte offset to bit shift
Matt Arsenault95245662016-02-11 05:32:46 +00001152 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1153 DAG.getConstant(3, DL, MVT::i32));
1154
Jan Vesely06200bd2017-01-06 21:00:46 +00001155 // TODO: Contrary to the name of the functiom,
1156 // it also handles sub i32 non-truncating stores (like i1)
Matt Arsenault95245662016-02-11 05:32:46 +00001157 SDValue SExtValue = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i32,
1158 Store->getValue());
1159
Jan Vesely06200bd2017-01-06 21:00:46 +00001160 // Mask the value to the right type
Matt Arsenault95245662016-02-11 05:32:46 +00001161 SDValue MaskedValue = DAG.getZeroExtendInReg(SExtValue, DL, MemVT);
1162
Jan Vesely06200bd2017-01-06 21:00:46 +00001163 // Shift the value in place
Matt Arsenault95245662016-02-11 05:32:46 +00001164 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
1165 MaskedValue, ShiftAmt);
1166
Jan Vesely06200bd2017-01-06 21:00:46 +00001167 // Shift the mask in place
1168 SDValue DstMask = DAG.getNode(ISD::SHL, DL, MVT::i32, Mask, ShiftAmt);
1169
1170 // Invert the mask. NOTE: if we had native ROL instructions we could
1171 // use inverted mask
1172 DstMask = DAG.getNOT(DL, DstMask, MVT::i32);
1173
1174 // Cleanup the target bits
Matt Arsenault95245662016-02-11 05:32:46 +00001175 Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
1176
Jan Vesely06200bd2017-01-06 21:00:46 +00001177 // Add the new bits
Matt Arsenault95245662016-02-11 05:32:46 +00001178 SDValue Value = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
Jan Vesely06200bd2017-01-06 21:00:46 +00001179
1180 // Store dword
1181 // TODO: Can we be smarter about MachinePointerInfo?
Jan Veselyf1705042017-01-20 21:24:26 +00001182 SDValue NewStore = DAG.getStore(Chain, DL, Value, Ptr, MachinePointerInfo());
1183
1184 // If we are part of expanded vector, make our neighbors depend on this store
1185 if (VectorTrunc) {
1186 // Make all other vector elements depend on this store
1187 Chain = DAG.getNode(AMDGPUISD::DUMMY_CHAIN, DL, MVT::Other, NewStore);
1188 DAG.ReplaceAllUsesOfValueWith(OldChain, Chain);
1189 }
1190 return NewStore;
Matt Arsenault95245662016-02-11 05:32:46 +00001191}
1192
1193SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault95245662016-02-11 05:32:46 +00001194 StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
1195 unsigned AS = StoreNode->getAddressSpace();
Matt Arsenault95245662016-02-11 05:32:46 +00001196
Jan Vesely06200bd2017-01-06 21:00:46 +00001197 SDValue Chain = StoreNode->getChain();
1198 SDValue Ptr = StoreNode->getBasePtr();
1199 SDValue Value = StoreNode->getValue();
1200
1201 EVT VT = Value.getValueType();
1202 EVT MemVT = StoreNode->getMemoryVT();
1203 EVT PtrVT = Ptr.getValueType();
1204
1205 SDLoc DL(Op);
1206
1207 // Neither LOCAL nor PRIVATE can do vectors at the moment
Matt Arsenault95245662016-02-11 05:32:46 +00001208 if ((AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::PRIVATE_ADDRESS) &&
Jan Vesely06200bd2017-01-06 21:00:46 +00001209 VT.isVector()) {
Jan Veselyf1705042017-01-20 21:24:26 +00001210 if ((AS == AMDGPUAS::PRIVATE_ADDRESS) && StoreNode->isTruncatingStore()) {
1211 // Add an extra level of chain to isolate this vector
1212 SDValue NewChain = DAG.getNode(AMDGPUISD::DUMMY_CHAIN, DL, MVT::Other, Chain);
1213 // TODO: can the chain be replaced without creating a new store?
1214 SDValue NewStore = DAG.getTruncStore(
1215 NewChain, DL, Value, Ptr, StoreNode->getPointerInfo(),
1216 MemVT, StoreNode->getAlignment(),
1217 StoreNode->getMemOperand()->getFlags(), StoreNode->getAAInfo());
1218 StoreNode = cast<StoreSDNode>(NewStore);
1219 }
1220
Jan Vesely06200bd2017-01-06 21:00:46 +00001221 return scalarizeVectorStore(StoreNode, DAG);
Matt Arsenault95245662016-02-11 05:32:46 +00001222 }
1223
Jan Vesely06200bd2017-01-06 21:00:46 +00001224 unsigned Align = StoreNode->getAlignment();
1225 if (Align < MemVT.getStoreSize() &&
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001226 !allowsMisalignedMemoryAccesses(MemVT, AS, Align, nullptr)) {
Jan Vesely00864882016-09-02 19:07:06 +00001227 return expandUnalignedStore(StoreNode, DAG);
1228 }
1229
Jan Vesely06200bd2017-01-06 21:00:46 +00001230 SDValue DWordAddr = DAG.getNode(ISD::SRL, DL, PtrVT, Ptr,
1231 DAG.getConstant(2, DL, PtrVT));
Matt Arsenault95245662016-02-11 05:32:46 +00001232
1233 if (AS == AMDGPUAS::GLOBAL_ADDRESS) {
Jan Vesely00864882016-09-02 19:07:06 +00001234 // It is beneficial to create MSKOR here instead of combiner to avoid
1235 // artificial dependencies introduced by RMW
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001236 if (StoreNode->isTruncatingStore()) {
Tom Stellardfbab8272013-08-16 01:12:11 +00001237 assert(VT.bitsLE(MVT::i32));
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001238 SDValue MaskConstant;
1239 if (MemVT == MVT::i8) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001240 MaskConstant = DAG.getConstant(0xFF, DL, MVT::i32);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001241 } else {
1242 assert(MemVT == MVT::i16);
Jan Vesely00864882016-09-02 19:07:06 +00001243 assert(StoreNode->getAlignment() >= 2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001244 MaskConstant = DAG.getConstant(0xFFFF, DL, MVT::i32);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001245 }
Jan Vesely06200bd2017-01-06 21:00:46 +00001246
1247 SDValue ByteIndex = DAG.getNode(ISD::AND, DL, PtrVT, Ptr,
1248 DAG.getConstant(0x00000003, DL, PtrVT));
1249 SDValue BitShift = DAG.getNode(ISD::SHL, DL, VT, ByteIndex,
1250 DAG.getConstant(3, DL, VT));
1251
1252 // Put the mask in correct place
1253 SDValue Mask = DAG.getNode(ISD::SHL, DL, VT, MaskConstant, BitShift);
1254
Jan Veselyf1705042017-01-20 21:24:26 +00001255 // Put the value bits in correct place
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001256 SDValue TruncValue = DAG.getNode(ISD::AND, DL, VT, Value, MaskConstant);
Jan Vesely06200bd2017-01-06 21:00:46 +00001257 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, VT, TruncValue, BitShift);
1258
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001259 // XXX: If we add a 64-bit ZW register class, then we could use a 2 x i32
1260 // vector instead.
1261 SDValue Src[4] = {
1262 ShiftedValue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001263 DAG.getConstant(0, DL, MVT::i32),
1264 DAG.getConstant(0, DL, MVT::i32),
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001265 Mask
1266 };
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001267 SDValue Input = DAG.getBuildVector(MVT::v4i32, DL, Src);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001268 SDValue Args[3] = { Chain, Input, DWordAddr };
1269 return DAG.getMemIntrinsicNode(AMDGPUISD::STORE_MSKOR, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00001270 Op->getVTList(), Args, MemVT,
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001271 StoreNode->getMemOperand());
Jan Vesely06200bd2017-01-06 21:00:46 +00001272 } else if (Ptr->getOpcode() != AMDGPUISD::DWORDADDR && VT.bitsGE(MVT::i32)) {
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001273 // Convert pointer from byte address to dword address.
Jan Vesely06200bd2017-01-06 21:00:46 +00001274 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, PtrVT, DWordAddr);
Tom Stellard75aadc22012-12-11 21:25:42 +00001275
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001276 if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +00001277 llvm_unreachable("Truncated and indexed stores not supported yet");
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001278 } else {
1279 Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
1280 }
1281 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001282 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001283 }
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001284
Jan Vesely06200bd2017-01-06 21:00:46 +00001285 // GLOBAL_ADDRESS has been handled above, LOCAL_ADDRESS allows all sizes
Matt Arsenault95245662016-02-11 05:32:46 +00001286 if (AS != AMDGPUAS::PRIVATE_ADDRESS)
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001287 return SDValue();
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001288
Matt Arsenault95245662016-02-11 05:32:46 +00001289 if (MemVT.bitsLT(MVT::i32))
1290 return lowerPrivateTruncStore(StoreNode, DAG);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001291
Jan Vesely06200bd2017-01-06 21:00:46 +00001292 // Standard i32+ store, tag it with DWORDADDR to note that the address
1293 // has been shifted
1294 if (Ptr.getOpcode() != AMDGPUISD::DWORDADDR) {
1295 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, PtrVT, DWordAddr);
1296 return DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001297 }
1298
Jan Vesely06200bd2017-01-06 21:00:46 +00001299 // Tagged i32+ stores will be matched by patterns
1300 return SDValue();
Tom Stellard75aadc22012-12-11 21:25:42 +00001301}
1302
Tom Stellard365366f2013-01-23 02:09:06 +00001303// return (512 + (kc_bank << 12)
1304static int
1305ConstantAddressBlock(unsigned AddressSpace) {
1306 switch (AddressSpace) {
1307 case AMDGPUAS::CONSTANT_BUFFER_0:
1308 return 512;
1309 case AMDGPUAS::CONSTANT_BUFFER_1:
1310 return 512 + 4096;
1311 case AMDGPUAS::CONSTANT_BUFFER_2:
1312 return 512 + 4096 * 2;
1313 case AMDGPUAS::CONSTANT_BUFFER_3:
1314 return 512 + 4096 * 3;
1315 case AMDGPUAS::CONSTANT_BUFFER_4:
1316 return 512 + 4096 * 4;
1317 case AMDGPUAS::CONSTANT_BUFFER_5:
1318 return 512 + 4096 * 5;
1319 case AMDGPUAS::CONSTANT_BUFFER_6:
1320 return 512 + 4096 * 6;
1321 case AMDGPUAS::CONSTANT_BUFFER_7:
1322 return 512 + 4096 * 7;
1323 case AMDGPUAS::CONSTANT_BUFFER_8:
1324 return 512 + 4096 * 8;
1325 case AMDGPUAS::CONSTANT_BUFFER_9:
1326 return 512 + 4096 * 9;
1327 case AMDGPUAS::CONSTANT_BUFFER_10:
1328 return 512 + 4096 * 10;
1329 case AMDGPUAS::CONSTANT_BUFFER_11:
1330 return 512 + 4096 * 11;
1331 case AMDGPUAS::CONSTANT_BUFFER_12:
1332 return 512 + 4096 * 12;
1333 case AMDGPUAS::CONSTANT_BUFFER_13:
1334 return 512 + 4096 * 13;
1335 case AMDGPUAS::CONSTANT_BUFFER_14:
1336 return 512 + 4096 * 14;
1337 case AMDGPUAS::CONSTANT_BUFFER_15:
1338 return 512 + 4096 * 15;
1339 default:
1340 return -1;
1341 }
1342}
1343
Matt Arsenault6dfda962016-02-10 18:21:39 +00001344SDValue R600TargetLowering::lowerPrivateExtLoad(SDValue Op,
1345 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001346 SDLoc DL(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001347 LoadSDNode *Load = cast<LoadSDNode>(Op);
1348 ISD::LoadExtType ExtType = Load->getExtensionType();
1349 EVT MemVT = Load->getMemoryVT();
Jan Vesely06200bd2017-01-06 21:00:46 +00001350 assert(Load->getAlignment() >= MemVT.getStoreSize());
Tom Stellard365366f2013-01-23 02:09:06 +00001351
Jan Vesely06200bd2017-01-06 21:00:46 +00001352 SDValue BasePtr = Load->getBasePtr();
1353 SDValue Chain = Load->getChain();
1354 SDValue Offset = Load->getOffset();
Matt Arsenault6dfda962016-02-10 18:21:39 +00001355
Jan Vesely06200bd2017-01-06 21:00:46 +00001356 SDValue LoadPtr = BasePtr;
1357 if (!Offset.isUndef()) {
1358 LoadPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, Offset);
1359 }
1360
1361 // Get dword location
1362 // NOTE: this should be eliminated by the future SHR ptr, 2
1363 SDValue Ptr = DAG.getNode(ISD::AND, DL, MVT::i32, LoadPtr,
1364 DAG.getConstant(0xfffffffc, DL, MVT::i32));
1365
1366 // Load dword
1367 // TODO: can we be smarter about machine pointer info?
1368 SDValue Read = DAG.getLoad(MVT::i32, DL, Chain, Ptr, MachinePointerInfo());
Matt Arsenault6dfda962016-02-10 18:21:39 +00001369
1370 // Get offset within the register.
1371 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32,
Jan Vesely06200bd2017-01-06 21:00:46 +00001372 LoadPtr, DAG.getConstant(0x3, DL, MVT::i32));
Matt Arsenault6dfda962016-02-10 18:21:39 +00001373
1374 // Bit offset of target byte (byteIdx * 8).
1375 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1376 DAG.getConstant(3, DL, MVT::i32));
1377
1378 // Shift to the right.
Jan Vesely06200bd2017-01-06 21:00:46 +00001379 SDValue Ret = DAG.getNode(ISD::SRL, DL, MVT::i32, Read, ShiftAmt);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001380
1381 // Eliminate the upper bits by setting them to ...
1382 EVT MemEltVT = MemVT.getScalarType();
1383
Jan Vesely06200bd2017-01-06 21:00:46 +00001384 if (ExtType == ISD::SEXTLOAD) { // ... ones.
Matt Arsenault6dfda962016-02-10 18:21:39 +00001385 SDValue MemEltVTNode = DAG.getValueType(MemEltVT);
Jan Vesely06200bd2017-01-06 21:00:46 +00001386 Ret = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, Ret, MemEltVTNode);
1387 } else { // ... or zeros.
1388 Ret = DAG.getZeroExtendInReg(Ret, DL, MemEltVT);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001389 }
1390
Matt Arsenault6dfda962016-02-10 18:21:39 +00001391 SDValue Ops[] = {
Jan Vesely06200bd2017-01-06 21:00:46 +00001392 Ret,
1393 Read.getValue(1) // This should be our output chain
Matt Arsenault6dfda962016-02-10 18:21:39 +00001394 };
1395
1396 return DAG.getMergeValues(Ops, DL);
1397}
1398
1399SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1400 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1401 unsigned AS = LoadNode->getAddressSpace();
1402 EVT MemVT = LoadNode->getMemoryVT();
1403 ISD::LoadExtType ExtType = LoadNode->getExtensionType();
1404
1405 if (AS == AMDGPUAS::PRIVATE_ADDRESS &&
1406 ExtType != ISD::NON_EXTLOAD && MemVT.bitsLT(MVT::i32)) {
1407 return lowerPrivateExtLoad(Op, DAG);
1408 }
1409
1410 SDLoc DL(Op);
1411 EVT VT = Op.getValueType();
1412 SDValue Chain = LoadNode->getChain();
1413 SDValue Ptr = LoadNode->getBasePtr();
Tom Stellarde9373602014-01-22 19:24:14 +00001414
Jan Vesely06200bd2017-01-06 21:00:46 +00001415 if ((LoadNode->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
1416 LoadNode->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) &&
1417 VT.isVector()) {
1418 return scalarizeVectorLoad(LoadNode, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001419 }
1420
Tom Stellard365366f2013-01-23 02:09:06 +00001421 int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
Matt Arsenault00a0d6f2013-11-13 02:39:07 +00001422 if (ConstantBlock > -1 &&
1423 ((LoadNode->getExtensionType() == ISD::NON_EXTLOAD) ||
1424 (LoadNode->getExtensionType() == ISD::ZEXTLOAD))) {
Tom Stellard365366f2013-01-23 02:09:06 +00001425 SDValue Result;
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001426 if (isa<ConstantExpr>(LoadNode->getMemOperand()->getValue()) ||
1427 isa<Constant>(LoadNode->getMemOperand()->getValue()) ||
Matt Arsenaultef1a9502013-11-01 17:39:26 +00001428 isa<ConstantSDNode>(Ptr)) {
Tom Stellard365366f2013-01-23 02:09:06 +00001429 SDValue Slots[4];
1430 for (unsigned i = 0; i < 4; i++) {
1431 // We want Const position encoded with the following formula :
1432 // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
1433 // const_index is Ptr computed by llvm using an alignment of 16.
1434 // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
1435 // then div by 4 at the ISel step
1436 SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001437 DAG.getConstant(4 * i + ConstantBlock * 16, DL, MVT::i32));
Tom Stellard365366f2013-01-23 02:09:06 +00001438 Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
1439 }
Tom Stellard0344cdf2013-08-01 15:23:42 +00001440 EVT NewVT = MVT::v4i32;
1441 unsigned NumElements = 4;
1442 if (VT.isVector()) {
1443 NewVT = VT;
1444 NumElements = VT.getVectorNumElements();
1445 }
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001446 Result = DAG.getBuildVector(NewVT, DL, makeArrayRef(Slots, NumElements));
Tom Stellard365366f2013-01-23 02:09:06 +00001447 } else {
Alp Tokerf907b892013-12-05 05:44:44 +00001448 // non-constant ptr can't be folded, keeps it as a v4f32 load
Tom Stellard365366f2013-01-23 02:09:06 +00001449 Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001450 DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr,
1451 DAG.getConstant(4, DL, MVT::i32)),
1452 DAG.getConstant(LoadNode->getAddressSpace() -
1453 AMDGPUAS::CONSTANT_BUFFER_0, DL, MVT::i32)
Tom Stellard365366f2013-01-23 02:09:06 +00001454 );
1455 }
1456
1457 if (!VT.isVector()) {
1458 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001459 DAG.getConstant(0, DL, MVT::i32));
Tom Stellard365366f2013-01-23 02:09:06 +00001460 }
1461
1462 SDValue MergedValues[2] = {
Matt Arsenault7939acd2014-04-07 16:44:24 +00001463 Result,
1464 Chain
Tom Stellard365366f2013-01-23 02:09:06 +00001465 };
Craig Topper64941d92014-04-27 19:20:57 +00001466 return DAG.getMergeValues(MergedValues, DL);
Tom Stellard365366f2013-01-23 02:09:06 +00001467 }
1468
Matt Arsenault909d0c02013-10-30 23:43:29 +00001469 // For most operations returning SDValue() will result in the node being
1470 // expanded by the DAG Legalizer. This is not the case for ISD::LOAD, so we
1471 // need to manually expand loads that may be legal in some address spaces and
1472 // illegal in others. SEXT loads from CONSTANT_BUFFER_0 are supported for
1473 // compute shaders, since the data is sign extended when it is uploaded to the
1474 // buffer. However SEXT loads from other address spaces are not supported, so
1475 // we need to expand them here.
Tom Stellard84021442013-07-23 01:48:24 +00001476 if (LoadNode->getExtensionType() == ISD::SEXTLOAD) {
1477 EVT MemVT = LoadNode->getMemoryVT();
1478 assert(!MemVT.isVector() && (MemVT == MVT::i16 || MemVT == MVT::i8));
Justin Lebar9c375812016-07-15 18:27:10 +00001479 SDValue NewLoad = DAG.getExtLoad(
1480 ISD::EXTLOAD, DL, VT, Chain, Ptr, LoadNode->getPointerInfo(), MemVT,
1481 LoadNode->getAlignment(), LoadNode->getMemOperand()->getFlags());
Jan Veselyb670d372015-05-26 18:07:22 +00001482 SDValue Res = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, NewLoad,
1483 DAG.getValueType(MemVT));
Tom Stellard84021442013-07-23 01:48:24 +00001484
Jan Veselyb670d372015-05-26 18:07:22 +00001485 SDValue MergedValues[2] = { Res, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00001486 return DAG.getMergeValues(MergedValues, DL);
Tom Stellard84021442013-07-23 01:48:24 +00001487 }
1488
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001489 if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1490 return SDValue();
1491 }
1492
Jan Vesely06200bd2017-01-06 21:00:46 +00001493 // DWORDADDR ISD marks already shifted address
1494 if (Ptr.getOpcode() != AMDGPUISD::DWORDADDR) {
1495 assert(VT == MVT::i32);
1496 Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr, DAG.getConstant(2, DL, MVT::i32));
1497 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, MVT::i32, Ptr);
1498 return DAG.getLoad(MVT::i32, DL, Chain, Ptr, LoadNode->getMemOperand());
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001499 }
Jan Vesely06200bd2017-01-06 21:00:46 +00001500 return SDValue();
Tom Stellard365366f2013-01-23 02:09:06 +00001501}
Tom Stellard75aadc22012-12-11 21:25:42 +00001502
Matt Arsenault1d555c42014-06-23 18:00:55 +00001503SDValue R600TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
1504 SDValue Chain = Op.getOperand(0);
1505 SDValue Cond = Op.getOperand(1);
1506 SDValue Jump = Op.getOperand(2);
1507
1508 return DAG.getNode(AMDGPUISD::BRANCH_COND, SDLoc(Op), Op.getValueType(),
1509 Chain, Jump, Cond);
1510}
1511
Matt Arsenault81d06012016-03-07 21:10:13 +00001512SDValue R600TargetLowering::lowerFrameIndex(SDValue Op,
1513 SelectionDAG &DAG) const {
1514 MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001515 const R600FrameLowering *TFL = getSubtarget()->getFrameLowering();
Matt Arsenault81d06012016-03-07 21:10:13 +00001516
1517 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op);
1518
1519 unsigned FrameIndex = FIN->getIndex();
1520 unsigned IgnoredFrameReg;
1521 unsigned Offset =
1522 TFL->getFrameIndexReference(MF, FrameIndex, IgnoredFrameReg);
1523 return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF), SDLoc(Op),
1524 Op.getValueType());
1525}
1526
Tom Stellard75aadc22012-12-11 21:25:42 +00001527/// XXX Only kernel functions are supported, so we can assume for now that
1528/// every function is a kernel function, but in the future we should use
1529/// separate calling conventions for kernel and non-kernel functions.
1530SDValue R600TargetLowering::LowerFormalArguments(
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001531 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1532 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
1533 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Tom Stellardacfeebf2013-07-23 01:48:05 +00001534 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001535 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1536 *DAG.getContext());
Vincent Lejeunef143af32013-11-11 22:10:24 +00001537 MachineFunction &MF = DAG.getMachineFunction();
Jan Veselye5121f32014-10-14 20:05:26 +00001538 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
Tom Stellardacfeebf2013-07-23 01:48:05 +00001539
Tom Stellardaf775432013-10-23 00:44:32 +00001540 SmallVector<ISD::InputArg, 8> LocalIns;
1541
Tom Stellardbbeb45a2016-09-16 21:53:00 +00001542 if (AMDGPU::isShader(CallConv)) {
1543 AnalyzeFormalArguments(CCInfo, Ins);
1544 } else {
1545 analyzeFormalArgumentsCompute(CCInfo, Ins);
1546 }
Tom Stellardacfeebf2013-07-23 01:48:05 +00001547
Tom Stellard1e803092013-07-23 01:48:18 +00001548 for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
Tom Stellardacfeebf2013-07-23 01:48:05 +00001549 CCValAssign &VA = ArgLocs[i];
Matt Arsenault74ef2772014-08-13 18:14:11 +00001550 const ISD::InputArg &In = Ins[i];
1551 EVT VT = In.VT;
1552 EVT MemVT = VA.getLocVT();
1553 if (!VT.isVector() && MemVT.isVector()) {
1554 // Get load source type if scalarized.
1555 MemVT = MemVT.getVectorElementType();
1556 }
Tom Stellard78e01292013-07-23 01:47:58 +00001557
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001558 if (AMDGPU::isShader(CallConv)) {
Vincent Lejeunef143af32013-11-11 22:10:24 +00001559 unsigned Reg = MF.addLiveIn(VA.getLocReg(), &AMDGPU::R600_Reg128RegClass);
1560 SDValue Register = DAG.getCopyFromReg(Chain, DL, Reg, VT);
1561 InVals.push_back(Register);
1562 continue;
1563 }
1564
Tom Stellard75aadc22012-12-11 21:25:42 +00001565 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
Matt Arsenault74ef2772014-08-13 18:14:11 +00001566 AMDGPUAS::CONSTANT_BUFFER_0);
Tom Stellardacfeebf2013-07-23 01:48:05 +00001567
Matt Arsenaultfae02982014-03-17 18:58:11 +00001568 // i64 isn't a legal type, so the register type used ends up as i32, which
1569 // isn't expected here. It attempts to create this sextload, but it ends up
1570 // being invalid. Somehow this seems to work with i64 arguments, but breaks
1571 // for <1 x i64>.
1572
Tom Stellardacfeebf2013-07-23 01:48:05 +00001573 // The first 36 bytes of the input buffer contains information about
1574 // thread group and global sizes.
Matt Arsenault74ef2772014-08-13 18:14:11 +00001575 ISD::LoadExtType Ext = ISD::NON_EXTLOAD;
1576 if (MemVT.getScalarSizeInBits() != VT.getScalarSizeInBits()) {
1577 // FIXME: This should really check the extload type, but the handling of
1578 // extload vector parameters seems to be broken.
Matt Arsenaulte1f030c2014-04-11 20:59:54 +00001579
Matt Arsenault74ef2772014-08-13 18:14:11 +00001580 // Ext = In.Flags.isSExt() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
1581 Ext = ISD::SEXTLOAD;
1582 }
1583
1584 // Compute the offset from the value.
1585 // XXX - I think PartOffset should give you this, but it seems to give the
1586 // size of the register which isn't useful.
1587
Andrew Trick05938a52015-02-16 18:10:47 +00001588 unsigned ValBase = ArgLocs[In.getOrigArgIndex()].getLocMemOffset();
Matt Arsenault74ef2772014-08-13 18:14:11 +00001589 unsigned PartOffset = VA.getLocMemOffset();
Matt Arsenault52ef4012016-07-26 16:45:58 +00001590 unsigned Offset = Subtarget->getExplicitKernelArgOffset() + VA.getLocMemOffset();
Matt Arsenault74ef2772014-08-13 18:14:11 +00001591
1592 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy), PartOffset - ValBase);
Justin Lebar9c375812016-07-15 18:27:10 +00001593 SDValue Arg = DAG.getLoad(
1594 ISD::UNINDEXED, Ext, VT, DL, Chain,
1595 DAG.getConstant(Offset, DL, MVT::i32), DAG.getUNDEF(MVT::i32), PtrInfo,
Justin Lebaradbf09e2016-09-11 01:38:58 +00001596 MemVT, /* Alignment = */ 4, MachineMemOperand::MONonTemporal |
1597 MachineMemOperand::MODereferenceable |
1598 MachineMemOperand::MOInvariant);
Matt Arsenault209a7b92014-04-18 07:40:20 +00001599
1600 // 4 is the preferred alignment for the CONSTANT memory space.
Tom Stellard75aadc22012-12-11 21:25:42 +00001601 InVals.push_back(Arg);
Matt Arsenault52ef4012016-07-26 16:45:58 +00001602 MFI->setABIArgOffset(Offset + MemVT.getStoreSize());
Tom Stellard75aadc22012-12-11 21:25:42 +00001603 }
1604 return Chain;
1605}
1606
Mehdi Amini44ede332015-07-09 02:09:04 +00001607EVT R600TargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1608 EVT VT) const {
Matt Arsenault209a7b92014-04-18 07:40:20 +00001609 if (!VT.isVector())
1610 return MVT::i32;
Tom Stellard75aadc22012-12-11 21:25:42 +00001611 return VT.changeVectorElementTypeToInteger();
1612}
1613
Matt Arsenaultfa67bdb2016-02-22 21:04:16 +00001614bool R600TargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
1615 unsigned AddrSpace,
1616 unsigned Align,
1617 bool *IsFast) const {
1618 if (IsFast)
1619 *IsFast = false;
1620
1621 if (!VT.isSimple() || VT == MVT::Other)
1622 return false;
1623
1624 if (VT.bitsLT(MVT::i32))
1625 return false;
1626
1627 // TODO: This is a rough estimate.
1628 if (IsFast)
1629 *IsFast = true;
1630
1631 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
1632}
1633
Matt Arsenault209a7b92014-04-18 07:40:20 +00001634static SDValue CompactSwizzlableVector(
1635 SelectionDAG &DAG, SDValue VectorEntry,
1636 DenseMap<unsigned, unsigned> &RemapSwizzle) {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001637 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1638 assert(RemapSwizzle.empty());
1639 SDValue NewBldVec[4] = {
Matt Arsenault209a7b92014-04-18 07:40:20 +00001640 VectorEntry.getOperand(0),
1641 VectorEntry.getOperand(1),
1642 VectorEntry.getOperand(2),
1643 VectorEntry.getOperand(3)
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001644 };
1645
1646 for (unsigned i = 0; i < 4; i++) {
Sanjay Patel57195842016-03-14 17:28:46 +00001647 if (NewBldVec[i].isUndef())
Vincent Lejeunefa58a5f2013-10-13 17:56:10 +00001648 // We mask write here to teach later passes that the ith element of this
1649 // vector is undef. Thus we can use it to reduce 128 bits reg usage,
1650 // break false dependencies and additionnaly make assembly easier to read.
1651 RemapSwizzle[i] = 7; // SEL_MASK_WRITE
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001652 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(NewBldVec[i])) {
1653 if (C->isZero()) {
1654 RemapSwizzle[i] = 4; // SEL_0
1655 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1656 } else if (C->isExactlyValue(1.0)) {
1657 RemapSwizzle[i] = 5; // SEL_1
1658 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1659 }
1660 }
1661
Sanjay Patel57195842016-03-14 17:28:46 +00001662 if (NewBldVec[i].isUndef())
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001663 continue;
1664 for (unsigned j = 0; j < i; j++) {
1665 if (NewBldVec[i] == NewBldVec[j]) {
1666 NewBldVec[i] = DAG.getUNDEF(NewBldVec[i].getValueType());
1667 RemapSwizzle[i] = j;
1668 break;
1669 }
1670 }
1671 }
1672
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001673 return DAG.getBuildVector(VectorEntry.getValueType(), SDLoc(VectorEntry),
1674 NewBldVec);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001675}
1676
Benjamin Kramer193960c2013-06-11 13:32:25 +00001677static SDValue ReorganizeVector(SelectionDAG &DAG, SDValue VectorEntry,
1678 DenseMap<unsigned, unsigned> &RemapSwizzle) {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001679 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1680 assert(RemapSwizzle.empty());
1681 SDValue NewBldVec[4] = {
1682 VectorEntry.getOperand(0),
1683 VectorEntry.getOperand(1),
1684 VectorEntry.getOperand(2),
1685 VectorEntry.getOperand(3)
1686 };
1687 bool isUnmovable[4] = { false, false, false, false };
Vincent Lejeunecc0ea742013-12-10 14:43:31 +00001688 for (unsigned i = 0; i < 4; i++) {
Vincent Lejeuneb8aac8d2013-07-09 15:03:25 +00001689 RemapSwizzle[i] = i;
Vincent Lejeunecc0ea742013-12-10 14:43:31 +00001690 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1691 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1692 ->getZExtValue();
1693 if (i == Idx)
1694 isUnmovable[Idx] = true;
1695 }
1696 }
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001697
1698 for (unsigned i = 0; i < 4; i++) {
1699 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1700 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1701 ->getZExtValue();
Vincent Lejeune301beb82013-10-13 17:56:04 +00001702 if (isUnmovable[Idx])
1703 continue;
1704 // Swap i and Idx
1705 std::swap(NewBldVec[Idx], NewBldVec[i]);
1706 std::swap(RemapSwizzle[i], RemapSwizzle[Idx]);
1707 break;
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001708 }
1709 }
1710
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001711 return DAG.getBuildVector(VectorEntry.getValueType(), SDLoc(VectorEntry),
1712 NewBldVec);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001713}
1714
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001715SDValue R600TargetLowering::OptimizeSwizzle(SDValue BuildVector, SDValue Swz[4],
1716 SelectionDAG &DAG,
1717 const SDLoc &DL) const {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001718 assert(BuildVector.getOpcode() == ISD::BUILD_VECTOR);
1719 // Old -> New swizzle values
1720 DenseMap<unsigned, unsigned> SwizzleRemap;
1721
1722 BuildVector = CompactSwizzlableVector(DAG, BuildVector, SwizzleRemap);
1723 for (unsigned i = 0; i < 4; i++) {
Benjamin Kramer619c4e52015-04-10 11:24:51 +00001724 unsigned Idx = cast<ConstantSDNode>(Swz[i])->getZExtValue();
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001725 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001726 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], DL, MVT::i32);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001727 }
1728
1729 SwizzleRemap.clear();
1730 BuildVector = ReorganizeVector(DAG, BuildVector, SwizzleRemap);
1731 for (unsigned i = 0; i < 4; i++) {
Benjamin Kramer619c4e52015-04-10 11:24:51 +00001732 unsigned Idx = cast<ConstantSDNode>(Swz[i])->getZExtValue();
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001733 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001734 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], DL, MVT::i32);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001735 }
1736
1737 return BuildVector;
1738}
1739
Tom Stellard75aadc22012-12-11 21:25:42 +00001740//===----------------------------------------------------------------------===//
1741// Custom DAG Optimizations
1742//===----------------------------------------------------------------------===//
1743
1744SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
1745 DAGCombinerInfo &DCI) const {
1746 SelectionDAG &DAG = DCI.DAG;
Jan Vesely89876672016-08-29 23:21:46 +00001747 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00001748
1749 switch (N->getOpcode()) {
1750 // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
1751 case ISD::FP_ROUND: {
1752 SDValue Arg = N->getOperand(0);
1753 if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
Jan Vesely89876672016-08-29 23:21:46 +00001754 return DAG.getNode(ISD::UINT_TO_FP, DL, N->getValueType(0),
Tom Stellard75aadc22012-12-11 21:25:42 +00001755 Arg.getOperand(0));
1756 }
1757 break;
1758 }
Tom Stellarde06163a2013-02-07 14:02:35 +00001759
1760 // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
1761 // (i32 select_cc f32, f32, -1, 0 cc)
1762 //
1763 // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1764 // this to one of the SET*_DX10 instructions.
1765 case ISD::FP_TO_SINT: {
1766 SDValue FNeg = N->getOperand(0);
1767 if (FNeg.getOpcode() != ISD::FNEG) {
1768 return SDValue();
1769 }
1770 SDValue SelectCC = FNeg.getOperand(0);
1771 if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1772 SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1773 SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1774 !isHWTrueValue(SelectCC.getOperand(2)) ||
1775 !isHWFalseValue(SelectCC.getOperand(3))) {
1776 return SDValue();
1777 }
1778
Jan Vesely89876672016-08-29 23:21:46 +00001779 return DAG.getNode(ISD::SELECT_CC, DL, N->getValueType(0),
Tom Stellarde06163a2013-02-07 14:02:35 +00001780 SelectCC.getOperand(0), // LHS
1781 SelectCC.getOperand(1), // RHS
Jan Vesely89876672016-08-29 23:21:46 +00001782 DAG.getConstant(-1, DL, MVT::i32), // True
1783 DAG.getConstant(0, DL, MVT::i32), // False
Tom Stellarde06163a2013-02-07 14:02:35 +00001784 SelectCC.getOperand(4)); // CC
1785
1786 break;
1787 }
Quentin Colombete2e05482013-07-30 00:27:16 +00001788
NAKAMURA Takumi8a046432013-10-28 04:07:38 +00001789 // insert_vector_elt (build_vector elt0, ... , eltN), NewEltIdx, idx
1790 // => build_vector elt0, ... , NewEltIdx, ... , eltN
Quentin Colombete2e05482013-07-30 00:27:16 +00001791 case ISD::INSERT_VECTOR_ELT: {
1792 SDValue InVec = N->getOperand(0);
1793 SDValue InVal = N->getOperand(1);
1794 SDValue EltNo = N->getOperand(2);
Quentin Colombete2e05482013-07-30 00:27:16 +00001795
1796 // If the inserted element is an UNDEF, just use the input vector.
Sanjay Patel57195842016-03-14 17:28:46 +00001797 if (InVal.isUndef())
Quentin Colombete2e05482013-07-30 00:27:16 +00001798 return InVec;
1799
1800 EVT VT = InVec.getValueType();
1801
1802 // If we can't generate a legal BUILD_VECTOR, exit
1803 if (!isOperationLegal(ISD::BUILD_VECTOR, VT))
1804 return SDValue();
1805
1806 // Check that we know which element is being inserted
1807 if (!isa<ConstantSDNode>(EltNo))
1808 return SDValue();
1809 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
1810
1811 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
1812 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
1813 // vector elements.
1814 SmallVector<SDValue, 8> Ops;
1815 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
1816 Ops.append(InVec.getNode()->op_begin(),
1817 InVec.getNode()->op_end());
Sanjay Patel57195842016-03-14 17:28:46 +00001818 } else if (InVec.isUndef()) {
Quentin Colombete2e05482013-07-30 00:27:16 +00001819 unsigned NElts = VT.getVectorNumElements();
1820 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
1821 } else {
1822 return SDValue();
1823 }
1824
1825 // Insert the element
1826 if (Elt < Ops.size()) {
1827 // All the operands of BUILD_VECTOR must have the same type;
1828 // we enforce that here.
1829 EVT OpVT = Ops[0].getValueType();
1830 if (InVal.getValueType() != OpVT)
1831 InVal = OpVT.bitsGT(InVal.getValueType()) ?
Jan Vesely89876672016-08-29 23:21:46 +00001832 DAG.getNode(ISD::ANY_EXTEND, DL, OpVT, InVal) :
1833 DAG.getNode(ISD::TRUNCATE, DL, OpVT, InVal);
Quentin Colombete2e05482013-07-30 00:27:16 +00001834 Ops[Elt] = InVal;
1835 }
1836
1837 // Return the new vector
Jan Vesely89876672016-08-29 23:21:46 +00001838 return DAG.getBuildVector(VT, DL, Ops);
Quentin Colombete2e05482013-07-30 00:27:16 +00001839 }
1840
Tom Stellard365366f2013-01-23 02:09:06 +00001841 // Extract_vec (Build_vector) generated by custom lowering
1842 // also needs to be customly combined
1843 case ISD::EXTRACT_VECTOR_ELT: {
1844 SDValue Arg = N->getOperand(0);
1845 if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1846 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1847 unsigned Element = Const->getZExtValue();
1848 return Arg->getOperand(Element);
1849 }
1850 }
Tom Stellarddd04c832013-01-31 22:11:53 +00001851 if (Arg.getOpcode() == ISD::BITCAST &&
Jan Veselyea457462016-09-02 20:13:19 +00001852 Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
1853 (Arg.getOperand(0).getValueType().getVectorNumElements() ==
1854 Arg.getValueType().getVectorNumElements())) {
Tom Stellarddd04c832013-01-31 22:11:53 +00001855 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1856 unsigned Element = Const->getZExtValue();
Jan Vesely89876672016-08-29 23:21:46 +00001857 return DAG.getNode(ISD::BITCAST, DL, N->getVTList(),
1858 Arg->getOperand(0).getOperand(Element));
Tom Stellarddd04c832013-01-31 22:11:53 +00001859 }
1860 }
Mehdi Aminie029eae2015-07-16 06:23:12 +00001861 break;
Tom Stellard365366f2013-01-23 02:09:06 +00001862 }
Tom Stellarde06163a2013-02-07 14:02:35 +00001863
1864 case ISD::SELECT_CC: {
Tom Stellardafa8b532014-05-09 16:42:16 +00001865 // Try common optimizations
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00001866 if (SDValue Ret = AMDGPUTargetLowering::PerformDAGCombine(N, DCI))
Tom Stellardafa8b532014-05-09 16:42:16 +00001867 return Ret;
1868
Tom Stellarde06163a2013-02-07 14:02:35 +00001869 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1870 // selectcc x, y, a, b, inv(cc)
Tom Stellard5e524892013-03-08 15:37:11 +00001871 //
1872 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, setne ->
1873 // selectcc x, y, a, b, cc
Tom Stellarde06163a2013-02-07 14:02:35 +00001874 SDValue LHS = N->getOperand(0);
1875 if (LHS.getOpcode() != ISD::SELECT_CC) {
1876 return SDValue();
1877 }
1878
1879 SDValue RHS = N->getOperand(1);
1880 SDValue True = N->getOperand(2);
1881 SDValue False = N->getOperand(3);
Tom Stellard5e524892013-03-08 15:37:11 +00001882 ISD::CondCode NCC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Tom Stellarde06163a2013-02-07 14:02:35 +00001883
1884 if (LHS.getOperand(2).getNode() != True.getNode() ||
1885 LHS.getOperand(3).getNode() != False.getNode() ||
Tom Stellard5e524892013-03-08 15:37:11 +00001886 RHS.getNode() != False.getNode()) {
Tom Stellarde06163a2013-02-07 14:02:35 +00001887 return SDValue();
1888 }
1889
Tom Stellard5e524892013-03-08 15:37:11 +00001890 switch (NCC) {
1891 default: return SDValue();
1892 case ISD::SETNE: return LHS;
1893 case ISD::SETEQ: {
1894 ISD::CondCode LHSCC = cast<CondCodeSDNode>(LHS.getOperand(4))->get();
1895 LHSCC = ISD::getSetCCInverse(LHSCC,
1896 LHS.getOperand(0).getValueType().isInteger());
Tom Stellardcd428182013-09-28 02:50:38 +00001897 if (DCI.isBeforeLegalizeOps() ||
1898 isCondCodeLegal(LHSCC, LHS.getOperand(0).getSimpleValueType()))
Jan Vesely89876672016-08-29 23:21:46 +00001899 return DAG.getSelectCC(DL,
Tom Stellardcd428182013-09-28 02:50:38 +00001900 LHS.getOperand(0),
1901 LHS.getOperand(1),
1902 LHS.getOperand(2),
1903 LHS.getOperand(3),
1904 LHSCC);
1905 break;
Vincent Lejeuned80bc152013-02-14 16:55:06 +00001906 }
Tom Stellard5e524892013-03-08 15:37:11 +00001907 }
Tom Stellardcd428182013-09-28 02:50:38 +00001908 return SDValue();
Tom Stellard5e524892013-03-08 15:37:11 +00001909 }
Tom Stellardfbab8272013-08-16 01:12:11 +00001910
Matt Arsenault7bee6ac2016-12-05 20:23:10 +00001911 case AMDGPUISD::R600_EXPORT: {
Vincent Lejeuned80bc152013-02-14 16:55:06 +00001912 SDValue Arg = N->getOperand(1);
1913 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1914 break;
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001915
Vincent Lejeuned80bc152013-02-14 16:55:06 +00001916 SDValue NewArgs[8] = {
1917 N->getOperand(0), // Chain
1918 SDValue(),
1919 N->getOperand(2), // ArrayBase
1920 N->getOperand(3), // Type
1921 N->getOperand(4), // SWZ_X
1922 N->getOperand(5), // SWZ_Y
1923 N->getOperand(6), // SWZ_Z
1924 N->getOperand(7) // SWZ_W
1925 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001926 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[4], DAG, DL);
Matt Arsenault7bee6ac2016-12-05 20:23:10 +00001927 return DAG.getNode(AMDGPUISD::R600_EXPORT, DL, N->getVTList(), NewArgs);
Tom Stellarde06163a2013-02-07 14:02:35 +00001928 }
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001929 case AMDGPUISD::TEXTURE_FETCH: {
1930 SDValue Arg = N->getOperand(1);
1931 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1932 break;
1933
1934 SDValue NewArgs[19] = {
1935 N->getOperand(0),
1936 N->getOperand(1),
1937 N->getOperand(2),
1938 N->getOperand(3),
1939 N->getOperand(4),
1940 N->getOperand(5),
1941 N->getOperand(6),
1942 N->getOperand(7),
1943 N->getOperand(8),
1944 N->getOperand(9),
1945 N->getOperand(10),
1946 N->getOperand(11),
1947 N->getOperand(12),
1948 N->getOperand(13),
1949 N->getOperand(14),
1950 N->getOperand(15),
1951 N->getOperand(16),
1952 N->getOperand(17),
1953 N->getOperand(18),
1954 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001955 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[2], DAG, DL);
1956 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, N->getVTList(), NewArgs);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001957 }
Jan Vesely89876672016-08-29 23:21:46 +00001958 default: break;
Tom Stellard75aadc22012-12-11 21:25:42 +00001959 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00001960
1961 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001962}
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001963
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001964bool R600TargetLowering::FoldOperand(SDNode *ParentNode, unsigned SrcIdx,
1965 SDValue &Src, SDValue &Neg, SDValue &Abs,
1966 SDValue &Sel, SDValue &Imm,
1967 SelectionDAG &DAG) const {
1968 const R600InstrInfo *TII = getSubtarget()->getInstrInfo();
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001969 if (!Src.isMachineOpcode())
1970 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001971
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001972 switch (Src.getMachineOpcode()) {
1973 case AMDGPU::FNEG_R600:
1974 if (!Neg.getNode())
1975 return false;
1976 Src = Src.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001977 Neg = DAG.getTargetConstant(1, SDLoc(ParentNode), MVT::i32);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001978 return true;
1979 case AMDGPU::FABS_R600:
1980 if (!Abs.getNode())
1981 return false;
1982 Src = Src.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001983 Abs = DAG.getTargetConstant(1, SDLoc(ParentNode), MVT::i32);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001984 return true;
1985 case AMDGPU::CONST_COPY: {
1986 unsigned Opcode = ParentNode->getMachineOpcode();
1987 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
1988
1989 if (!Sel.getNode())
1990 return false;
1991
1992 SDValue CstOffset = Src.getOperand(0);
1993 if (ParentNode->getValueType(0).isVector())
1994 return false;
1995
1996 // Gather constants values
1997 int SrcIndices[] = {
1998 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
1999 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
2000 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2),
2001 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
2002 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
2003 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
2004 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
2005 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
2006 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
2007 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
2008 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
2009 };
2010 std::vector<unsigned> Consts;
Matt Arsenault4d64f962014-05-12 19:23:21 +00002011 for (int OtherSrcIdx : SrcIndices) {
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002012 int OtherSelIdx = TII->getSelIdx(Opcode, OtherSrcIdx);
2013 if (OtherSrcIdx < 0 || OtherSelIdx < 0)
2014 continue;
2015 if (HasDst) {
2016 OtherSrcIdx--;
2017 OtherSelIdx--;
2018 }
2019 if (RegisterSDNode *Reg =
2020 dyn_cast<RegisterSDNode>(ParentNode->getOperand(OtherSrcIdx))) {
2021 if (Reg->getReg() == AMDGPU::ALU_CONST) {
Matt Arsenaultb3ee3882014-05-12 19:26:38 +00002022 ConstantSDNode *Cst
2023 = cast<ConstantSDNode>(ParentNode->getOperand(OtherSelIdx));
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002024 Consts.push_back(Cst->getZExtValue());
2025 }
2026 }
2027 }
2028
Matt Arsenault37c12d72014-05-12 20:42:57 +00002029 ConstantSDNode *Cst = cast<ConstantSDNode>(CstOffset);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002030 Consts.push_back(Cst->getZExtValue());
2031 if (!TII->fitsConstReadLimitations(Consts)) {
2032 return false;
2033 }
2034
2035 Sel = CstOffset;
2036 Src = DAG.getRegister(AMDGPU::ALU_CONST, MVT::f32);
2037 return true;
2038 }
Jan Vesely16800392016-05-13 20:39:31 +00002039 case AMDGPU::MOV_IMM_GLOBAL_ADDR:
2040 // Check if the Imm slot is used. Taken from below.
2041 if (cast<ConstantSDNode>(Imm)->getZExtValue())
2042 return false;
2043 Imm = Src.getOperand(0);
2044 Src = DAG.getRegister(AMDGPU::ALU_LITERAL_X, MVT::i32);
2045 return true;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002046 case AMDGPU::MOV_IMM_I32:
2047 case AMDGPU::MOV_IMM_F32: {
2048 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
2049 uint64_t ImmValue = 0;
2050
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002051 if (Src.getMachineOpcode() == AMDGPU::MOV_IMM_F32) {
2052 ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Src.getOperand(0));
2053 float FloatValue = FPC->getValueAPF().convertToFloat();
2054 if (FloatValue == 0.0) {
2055 ImmReg = AMDGPU::ZERO;
2056 } else if (FloatValue == 0.5) {
2057 ImmReg = AMDGPU::HALF;
2058 } else if (FloatValue == 1.0) {
2059 ImmReg = AMDGPU::ONE;
2060 } else {
2061 ImmValue = FPC->getValueAPF().bitcastToAPInt().getZExtValue();
2062 }
2063 } else {
2064 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(0));
2065 uint64_t Value = C->getZExtValue();
2066 if (Value == 0) {
2067 ImmReg = AMDGPU::ZERO;
2068 } else if (Value == 1) {
2069 ImmReg = AMDGPU::ONE_INT;
2070 } else {
2071 ImmValue = Value;
2072 }
2073 }
2074
2075 // Check that we aren't already using an immediate.
2076 // XXX: It's possible for an instruction to have more than one
2077 // immediate operand, but this is not supported yet.
2078 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
2079 if (!Imm.getNode())
2080 return false;
2081 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Imm);
2082 assert(C);
2083 if (C->getZExtValue())
2084 return false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002085 Imm = DAG.getTargetConstant(ImmValue, SDLoc(ParentNode), MVT::i32);
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002086 }
2087 Src = DAG.getRegister(ImmReg, MVT::i32);
2088 return true;
2089 }
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002090 default:
2091 return false;
2092 }
2093}
2094
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002095/// \brief Fold the instructions after selecting them
2096SDNode *R600TargetLowering::PostISelFolding(MachineSDNode *Node,
2097 SelectionDAG &DAG) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002098 const R600InstrInfo *TII = getSubtarget()->getInstrInfo();
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002099 if (!Node->isMachineOpcode())
2100 return Node;
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002101
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002102 unsigned Opcode = Node->getMachineOpcode();
2103 SDValue FakeOp;
2104
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002105 std::vector<SDValue> Ops(Node->op_begin(), Node->op_end());
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002106
2107 if (Opcode == AMDGPU::DOT_4) {
2108 int OperandIdx[] = {
2109 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
2110 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
2111 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
2112 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
2113 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
2114 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
2115 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
2116 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
NAKAMURA Takumi4bb85f92013-10-28 04:07:23 +00002117 };
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002118 int NegIdx[] = {
2119 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_X),
2120 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Y),
2121 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Z),
2122 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_W),
2123 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_X),
2124 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Y),
2125 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Z),
2126 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_W)
2127 };
2128 int AbsIdx[] = {
2129 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_X),
2130 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Y),
2131 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Z),
2132 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_W),
2133 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_X),
2134 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Y),
2135 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Z),
2136 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_W)
2137 };
2138 for (unsigned i = 0; i < 8; i++) {
2139 if (OperandIdx[i] < 0)
2140 return Node;
2141 SDValue &Src = Ops[OperandIdx[i] - 1];
2142 SDValue &Neg = Ops[NegIdx[i] - 1];
2143 SDValue &Abs = Ops[AbsIdx[i] - 1];
2144 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2145 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
2146 if (HasDst)
2147 SelIdx--;
2148 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002149 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, FakeOp, DAG))
2150 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2151 }
2152 } else if (Opcode == AMDGPU::REG_SEQUENCE) {
2153 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) {
2154 SDValue &Src = Ops[i];
2155 if (FoldOperand(Node, i, Src, FakeOp, FakeOp, FakeOp, FakeOp, DAG))
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002156 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2157 }
Vincent Lejeune0167a312013-09-12 23:45:00 +00002158 } else if (Opcode == AMDGPU::CLAMP_R600) {
2159 SDValue Src = Node->getOperand(0);
2160 if (!Src.isMachineOpcode() ||
2161 !TII->hasInstrModifiers(Src.getMachineOpcode()))
2162 return Node;
2163 int ClampIdx = TII->getOperandIdx(Src.getMachineOpcode(),
2164 AMDGPU::OpName::clamp);
2165 if (ClampIdx < 0)
2166 return Node;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002167 SDLoc DL(Node);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002168 std::vector<SDValue> Ops(Src->op_begin(), Src->op_end());
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002169 Ops[ClampIdx - 1] = DAG.getTargetConstant(1, DL, MVT::i32);
2170 return DAG.getMachineNode(Src.getMachineOpcode(), DL,
2171 Node->getVTList(), Ops);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002172 } else {
2173 if (!TII->hasInstrModifiers(Opcode))
2174 return Node;
2175 int OperandIdx[] = {
2176 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
2177 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
2178 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2)
2179 };
2180 int NegIdx[] = {
2181 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg),
2182 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg),
2183 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_neg)
2184 };
2185 int AbsIdx[] = {
2186 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs),
2187 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs),
2188 -1
2189 };
2190 for (unsigned i = 0; i < 3; i++) {
2191 if (OperandIdx[i] < 0)
2192 return Node;
2193 SDValue &Src = Ops[OperandIdx[i] - 1];
2194 SDValue &Neg = Ops[NegIdx[i] - 1];
2195 SDValue FakeAbs;
2196 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
2197 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2198 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002199 int ImmIdx = TII->getOperandIdx(Opcode, AMDGPU::OpName::literal);
2200 if (HasDst) {
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002201 SelIdx--;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002202 ImmIdx--;
2203 }
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002204 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002205 SDValue &Imm = Ops[ImmIdx];
2206 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, Imm, DAG))
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002207 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2208 }
2209 }
2210
2211 return Node;
2212}