blob: d7d40a83fa66b181a1b9ac05f424c2be3239b187 [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"
20#include "R600InstrInfo.h"
21#include "R600MachineFunctionInfo.h"
Tom Stellard067c8152014-07-21 14:01:14 +000022#include "llvm/Analysis/ValueTracking.h"
Tom Stellardacfeebf2013-07-23 01:48:05 +000023#include "llvm/CodeGen/CallingConvLower.h"
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000025#include "llvm/CodeGen/MachineInstrBuilder.h"
26#include "llvm/CodeGen/MachineRegisterInfo.h"
27#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/Argument.h"
29#include "llvm/IR/Function.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000030
31using namespace llvm;
32
Matt Arsenault43e92fe2016-06-24 06:30:11 +000033R600TargetLowering::R600TargetLowering(const TargetMachine &TM,
34 const R600Subtarget &STI)
Eric Christopher7792e322015-01-30 23:24:40 +000035 : AMDGPUTargetLowering(TM, STI), Gen(STI.getGeneration()) {
Tom Stellard75aadc22012-12-11 21:25:42 +000036 addRegisterClass(MVT::f32, &AMDGPU::R600_Reg32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000037 addRegisterClass(MVT::i32, &AMDGPU::R600_Reg32RegClass);
Tom Stellard0344cdf2013-08-01 15:23:42 +000038 addRegisterClass(MVT::v2f32, &AMDGPU::R600_Reg64RegClass);
39 addRegisterClass(MVT::v2i32, &AMDGPU::R600_Reg64RegClass);
Matt Arsenault71e66762016-05-21 02:27:49 +000040 addRegisterClass(MVT::v4f32, &AMDGPU::R600_Reg128RegClass);
41 addRegisterClass(MVT::v4i32, &AMDGPU::R600_Reg128RegClass);
Tom Stellard0344cdf2013-08-01 15:23:42 +000042
Eric Christopher23a3a7c2015-02-26 00:00:24 +000043 computeRegisterProperties(STI.getRegisterInfo());
Tom Stellard75aadc22012-12-11 21:25:42 +000044
Matt Arsenault71e66762016-05-21 02:27:49 +000045 // Legalize loads and stores to the private address space.
46 setOperationAction(ISD::LOAD, MVT::i32, Custom);
47 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
48 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
49
50 // EXTLOAD should be the same as ZEXTLOAD. It is legal for some address
51 // spaces, so it is custom lowered to handle those where it isn't.
52 for (MVT VT : MVT::integer_valuetypes()) {
53 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
54 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Custom);
55 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Custom);
56
57 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
58 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Custom);
59 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Custom);
60
61 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
62 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Custom);
63 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Custom);
64 }
65
Matt Arsenaultd1097a32016-06-02 19:54:26 +000066 // Workaround for LegalizeDAG asserting on expansion of i1 vector loads.
67 setLoadExtAction(ISD::EXTLOAD, MVT::v2i32, MVT::v2i1, Expand);
68 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i32, MVT::v2i1, Expand);
69 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i32, MVT::v2i1, Expand);
70
71 setLoadExtAction(ISD::EXTLOAD, MVT::v4i32, MVT::v4i1, Expand);
72 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i32, MVT::v4i1, Expand);
73 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i32, MVT::v4i1, Expand);
74
75
Matt Arsenault71e66762016-05-21 02:27:49 +000076 setOperationAction(ISD::STORE, MVT::i8, Custom);
77 setOperationAction(ISD::STORE, MVT::i32, Custom);
78 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
79 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
80
81 setTruncStoreAction(MVT::i32, MVT::i8, Custom);
82 setTruncStoreAction(MVT::i32, MVT::i16, Custom);
83
Matt Arsenaultd1097a32016-06-02 19:54:26 +000084 // Workaround for LegalizeDAG asserting on expansion of i1 vector stores.
85 setTruncStoreAction(MVT::v2i32, MVT::v2i1, Expand);
86 setTruncStoreAction(MVT::v4i32, MVT::v4i1, Expand);
87
Tom Stellard0351ea22013-09-28 02:50:50 +000088 // Set condition code actions
89 setCondCodeAction(ISD::SETO, MVT::f32, Expand);
90 setCondCodeAction(ISD::SETUO, MVT::f32, Expand);
Tom Stellardcd428182013-09-28 02:50:38 +000091 setCondCodeAction(ISD::SETLT, MVT::f32, Expand);
Tom Stellard0351ea22013-09-28 02:50:50 +000092 setCondCodeAction(ISD::SETLE, MVT::f32, Expand);
Tom Stellardcd428182013-09-28 02:50:38 +000093 setCondCodeAction(ISD::SETOLT, MVT::f32, Expand);
94 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
Tom Stellard0351ea22013-09-28 02:50:50 +000095 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
96 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
97 setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
98 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
Tom Stellardcd428182013-09-28 02:50:38 +000099 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
100 setCondCodeAction(ISD::SETULE, MVT::f32, Expand);
101
102 setCondCodeAction(ISD::SETLE, MVT::i32, Expand);
103 setCondCodeAction(ISD::SETLT, MVT::i32, Expand);
104 setCondCodeAction(ISD::SETULE, MVT::i32, Expand);
105 setCondCodeAction(ISD::SETULT, MVT::i32, Expand);
106
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000107 setOperationAction(ISD::FCOS, MVT::f32, Custom);
108 setOperationAction(ISD::FSIN, MVT::f32, Custom);
109
Tom Stellard75aadc22012-12-11 21:25:42 +0000110 setOperationAction(ISD::SETCC, MVT::v4i32, Expand);
Tom Stellard0344cdf2013-08-01 15:23:42 +0000111 setOperationAction(ISD::SETCC, MVT::v2i32, Expand);
Tom Stellard75aadc22012-12-11 21:25:42 +0000112
Tom Stellard492ebea2013-03-08 15:37:07 +0000113 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
114 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
Matt Arsenault1d555c42014-06-23 18:00:55 +0000115 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Tom Stellard75aadc22012-12-11 21:25:42 +0000116
117 setOperationAction(ISD::FSUB, MVT::f32, Expand);
118
Tom Stellard75aadc22012-12-11 21:25:42 +0000119 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
120 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
121
Tom Stellarde8f9f282013-03-08 15:37:05 +0000122 setOperationAction(ISD::SETCC, MVT::i32, Expand);
123 setOperationAction(ISD::SETCC, MVT::f32, Expand);
Tom Stellard75aadc22012-12-11 21:25:42 +0000124 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Custom);
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000125 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Custom);
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000126 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
127 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
Tom Stellard75aadc22012-12-11 21:25:42 +0000128
Tom Stellard53f2f902013-09-05 18:38:03 +0000129 setOperationAction(ISD::SELECT, MVT::i32, Expand);
130 setOperationAction(ISD::SELECT, MVT::f32, Expand);
131 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
Tom Stellard53f2f902013-09-05 18:38:03 +0000132 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
Tom Stellard75aadc22012-12-11 21:25:42 +0000133
Jan Vesely808fff52015-04-30 17:15:56 +0000134 // ADD, SUB overflow.
135 // TODO: turn these into Legal?
136 if (Subtarget->hasCARRY())
137 setOperationAction(ISD::UADDO, MVT::i32, Custom);
138
139 if (Subtarget->hasBORROW())
140 setOperationAction(ISD::USUBO, MVT::i32, Custom);
141
Matt Arsenault4e466652014-04-16 01:41:30 +0000142 // Expand sign extension of vectors
143 if (!Subtarget->hasBFE())
144 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
145
146 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Expand);
147 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Expand);
148
149 if (!Subtarget->hasBFE())
150 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
151 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Expand);
152 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Expand);
153
154 if (!Subtarget->hasBFE())
155 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
156 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Expand);
157 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Expand);
158
159 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
160 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Expand);
161 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i32, Expand);
162
163 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Expand);
164
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000165 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
166
Tom Stellard880a80a2014-06-17 16:53:14 +0000167 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i32, Custom);
168 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f32, Custom);
169 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
170 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
171
172 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i32, Custom);
173 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f32, Custom);
174 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
175 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
176
Jan Vesely25f36272014-06-18 12:27:13 +0000177 // We don't have 64-bit shifts. Thus we need either SHX i64 or SHX_PARTS i32
178 // to be Legal/Custom in order to avoid library calls.
179 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000180 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Jan Veselyecf51332014-06-18 12:27:17 +0000181 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jan Vesely25f36272014-06-18 12:27:13 +0000182
Michel Danzer49812b52013-07-10 16:37:07 +0000183 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
184
Matt Arsenaultc4d3d3a2014-06-23 18:00:49 +0000185 const MVT ScalarIntVTs[] = { MVT::i32, MVT::i64 };
186 for (MVT VT : ScalarIntVTs) {
187 setOperationAction(ISD::ADDC, VT, Expand);
188 setOperationAction(ISD::SUBC, VT, Expand);
189 setOperationAction(ISD::ADDE, VT, Expand);
190 setOperationAction(ISD::SUBE, VT, Expand);
191 }
192
Tom Stellardfc455472013-08-12 22:33:21 +0000193 setSchedulingPreference(Sched::Source);
Matt Arsenault71e66762016-05-21 02:27:49 +0000194
195
196 setTargetDAGCombine(ISD::FP_ROUND);
197 setTargetDAGCombine(ISD::FP_TO_SINT);
198 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
199 setTargetDAGCombine(ISD::SELECT_CC);
200 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
Jan Vesely38814fa2016-08-27 19:09:43 +0000201 setTargetDAGCombine(ISD::LOAD);
Tom Stellard75aadc22012-12-11 21:25:42 +0000202}
203
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000204const R600Subtarget *R600TargetLowering::getSubtarget() const {
205 return static_cast<const R600Subtarget *>(Subtarget);
206}
207
Tom Stellardc0f0fba2015-10-01 17:51:29 +0000208static inline bool isEOP(MachineBasicBlock::iterator I) {
Hans Wennborg0dd9ed12016-08-13 01:12:49 +0000209 if (std::next(I) == I->getParent()->end())
210 return false;
Tom Stellardc0f0fba2015-10-01 17:51:29 +0000211 return std::next(I)->getOpcode() == AMDGPU::RETURN;
212}
213
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000214MachineBasicBlock *
215R600TargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
216 MachineBasicBlock *BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000217 MachineFunction * MF = BB->getParent();
218 MachineRegisterInfo &MRI = MF->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000219 MachineBasicBlock::iterator I = MI;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000220 const R600InstrInfo *TII = getSubtarget()->getInstrInfo();
Tom Stellard75aadc22012-12-11 21:25:42 +0000221
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000222 switch (MI.getOpcode()) {
Tom Stellardc6f4a292013-08-26 15:05:59 +0000223 default:
Tom Stellard8f9fc202013-11-15 00:12:45 +0000224 // Replace LDS_*_RET instruction that don't have any uses with the
225 // equivalent LDS_*_NORET instruction.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000226 if (TII->isLDSRetInstr(MI.getOpcode())) {
227 int DstIdx = TII->getOperandIdx(MI.getOpcode(), AMDGPU::OpName::dst);
Tom Stellard13c68ef2013-09-05 18:38:09 +0000228 assert(DstIdx != -1);
229 MachineInstrBuilder NewMI;
Aaron Watry1885e532014-09-11 15:02:54 +0000230 // FIXME: getLDSNoRetOp method only handles LDS_1A1D LDS ops. Add
231 // LDS_1A2D support and remove this special case.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000232 if (!MRI.use_empty(MI.getOperand(DstIdx).getReg()) ||
233 MI.getOpcode() == AMDGPU::LDS_CMPST_RET)
Tom Stellard8f9fc202013-11-15 00:12:45 +0000234 return BB;
235
236 NewMI = BuildMI(*BB, I, BB->findDebugLoc(I),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000237 TII->get(AMDGPU::getLDSNoRetOp(MI.getOpcode())));
238 for (unsigned i = 1, e = MI.getNumOperands(); i < e; ++i) {
239 NewMI.addOperand(MI.getOperand(i));
Tom Stellardc6f4a292013-08-26 15:05:59 +0000240 }
Tom Stellardc6f4a292013-08-26 15:05:59 +0000241 } else {
242 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
243 }
244 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000245 case AMDGPU::CLAMP_R600: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000246 MachineInstr *NewMI = TII->buildDefaultInstruction(
247 *BB, I, AMDGPU::MOV, MI.getOperand(0).getReg(),
248 MI.getOperand(1).getReg());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000249 TII->addFlag(*NewMI, 0, MO_FLAG_CLAMP);
Tom Stellard75aadc22012-12-11 21:25:42 +0000250 break;
251 }
252
253 case AMDGPU::FABS_R600: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000254 MachineInstr *NewMI = TII->buildDefaultInstruction(
255 *BB, I, AMDGPU::MOV, MI.getOperand(0).getReg(),
256 MI.getOperand(1).getReg());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000257 TII->addFlag(*NewMI, 0, MO_FLAG_ABS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000258 break;
259 }
260
261 case AMDGPU::FNEG_R600: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000262 MachineInstr *NewMI = TII->buildDefaultInstruction(
263 *BB, I, AMDGPU::MOV, MI.getOperand(0).getReg(),
264 MI.getOperand(1).getReg());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000265 TII->addFlag(*NewMI, 0, MO_FLAG_NEG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000266 break;
267 }
268
Tom Stellard75aadc22012-12-11 21:25:42 +0000269 case AMDGPU::MASK_WRITE: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000270 unsigned maskedRegister = MI.getOperand(0).getReg();
Tom Stellard75aadc22012-12-11 21:25:42 +0000271 assert(TargetRegisterInfo::isVirtualRegister(maskedRegister));
272 MachineInstr * defInstr = MRI.getVRegDef(maskedRegister);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000273 TII->addFlag(*defInstr, 0, MO_FLAG_MASK);
Tom Stellard75aadc22012-12-11 21:25:42 +0000274 break;
275 }
276
277 case AMDGPU::MOV_IMM_F32:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000278 TII->buildMovImm(*BB, I, MI.getOperand(0).getReg(), MI.getOperand(1)
279 .getFPImm()
280 ->getValueAPF()
281 .bitcastToAPInt()
282 .getZExtValue());
Tom Stellard75aadc22012-12-11 21:25:42 +0000283 break;
284 case AMDGPU::MOV_IMM_I32:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000285 TII->buildMovImm(*BB, I, MI.getOperand(0).getReg(),
286 MI.getOperand(1).getImm());
Tom Stellard75aadc22012-12-11 21:25:42 +0000287 break;
Jan Veselyf97de002016-05-13 20:39:29 +0000288 case AMDGPU::MOV_IMM_GLOBAL_ADDR: {
289 //TODO: Perhaps combine this instruction with the next if possible
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000290 auto MIB = TII->buildDefaultInstruction(
291 *BB, MI, AMDGPU::MOV, MI.getOperand(0).getReg(), AMDGPU::ALU_LITERAL_X);
Jan Veselyf97de002016-05-13 20:39:29 +0000292 int Idx = TII->getOperandIdx(*MIB, AMDGPU::OpName::literal);
293 //TODO: Ugh this is rather ugly
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000294 MIB->getOperand(Idx) = MI.getOperand(1);
Jan Veselyf97de002016-05-13 20:39:29 +0000295 break;
296 }
Vincent Lejeune0b72f102013-03-05 15:04:55 +0000297 case AMDGPU::CONST_COPY: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000298 MachineInstr *NewMI = TII->buildDefaultInstruction(
299 *BB, MI, AMDGPU::MOV, MI.getOperand(0).getReg(), AMDGPU::ALU_CONST);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000300 TII->setImmOperand(*NewMI, AMDGPU::OpName::src0_sel,
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000301 MI.getOperand(1).getImm());
Vincent Lejeune0b72f102013-03-05 15:04:55 +0000302 break;
303 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000304
305 case AMDGPU::RAT_WRITE_CACHELESS_32_eg:
Tom Stellard0344cdf2013-08-01 15:23:42 +0000306 case AMDGPU::RAT_WRITE_CACHELESS_64_eg:
Tom Stellard75aadc22012-12-11 21:25:42 +0000307 case AMDGPU::RAT_WRITE_CACHELESS_128_eg: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000308 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode()))
309 .addOperand(MI.getOperand(0))
310 .addOperand(MI.getOperand(1))
311 .addImm(isEOP(I)); // Set End of program bit
Tom Stellard75aadc22012-12-11 21:25:42 +0000312 break;
313 }
Tom Stellarde0e582c2015-10-01 17:51:34 +0000314 case AMDGPU::RAT_STORE_TYPED_eg: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000315 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode()))
316 .addOperand(MI.getOperand(0))
317 .addOperand(MI.getOperand(1))
318 .addOperand(MI.getOperand(2))
319 .addImm(isEOP(I)); // Set End of program bit
Tom Stellarde0e582c2015-10-01 17:51:34 +0000320 break;
321 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000322 case AMDGPU::BRANCH:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000323 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
324 .addOperand(MI.getOperand(0));
325 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000326
327 case AMDGPU::BRANCH_COND_f32: {
328 MachineInstr *NewMI =
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000329 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
330 AMDGPU::PREDICATE_BIT)
331 .addOperand(MI.getOperand(1))
Matt Arsenault44f6d692016-08-13 01:43:46 +0000332 .addImm(AMDGPU::PRED_SETNE)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000333 .addImm(0); // Flags
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000334 TII->addFlag(*NewMI, 0, MO_FLAG_PUSH);
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000335 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000336 .addOperand(MI.getOperand(0))
337 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
Tom Stellard75aadc22012-12-11 21:25:42 +0000338 break;
339 }
340
341 case AMDGPU::BRANCH_COND_i32: {
342 MachineInstr *NewMI =
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000343 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
344 AMDGPU::PREDICATE_BIT)
345 .addOperand(MI.getOperand(1))
Matt Arsenault44f6d692016-08-13 01:43:46 +0000346 .addImm(AMDGPU::PRED_SETNE_INT)
Tom Stellard75aadc22012-12-11 21:25:42 +0000347 .addImm(0); // Flags
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000348 TII->addFlag(*NewMI, 0, MO_FLAG_PUSH);
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000349 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000350 .addOperand(MI.getOperand(0))
351 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
Tom Stellard75aadc22012-12-11 21:25:42 +0000352 break;
353 }
354
Tom Stellard75aadc22012-12-11 21:25:42 +0000355 case AMDGPU::EG_ExportSwz:
356 case AMDGPU::R600_ExportSwz: {
Tom Stellard6f1b8652013-01-23 21:39:49 +0000357 // Instruction is left unmodified if its not the last one of its type
358 bool isLastInstructionOfItsType = true;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000359 unsigned InstExportType = MI.getOperand(1).getImm();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000360 for (MachineBasicBlock::iterator NextExportInst = std::next(I),
Tom Stellard6f1b8652013-01-23 21:39:49 +0000361 EndBlock = BB->end(); NextExportInst != EndBlock;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000362 NextExportInst = std::next(NextExportInst)) {
Tom Stellard6f1b8652013-01-23 21:39:49 +0000363 if (NextExportInst->getOpcode() == AMDGPU::EG_ExportSwz ||
364 NextExportInst->getOpcode() == AMDGPU::R600_ExportSwz) {
365 unsigned CurrentInstExportType = NextExportInst->getOperand(1)
366 .getImm();
367 if (CurrentInstExportType == InstExportType) {
368 isLastInstructionOfItsType = false;
369 break;
370 }
371 }
372 }
Tom Stellardc0f0fba2015-10-01 17:51:29 +0000373 bool EOP = isEOP(I);
Tom Stellard6f1b8652013-01-23 21:39:49 +0000374 if (!EOP && !isLastInstructionOfItsType)
Tom Stellard75aadc22012-12-11 21:25:42 +0000375 return BB;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000376 unsigned CfInst = (MI.getOpcode() == AMDGPU::EG_ExportSwz) ? 84 : 40;
377 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode()))
378 .addOperand(MI.getOperand(0))
379 .addOperand(MI.getOperand(1))
380 .addOperand(MI.getOperand(2))
381 .addOperand(MI.getOperand(3))
382 .addOperand(MI.getOperand(4))
383 .addOperand(MI.getOperand(5))
384 .addOperand(MI.getOperand(6))
385 .addImm(CfInst)
386 .addImm(EOP);
Tom Stellard75aadc22012-12-11 21:25:42 +0000387 break;
388 }
Jakob Stoklund Olesenfdc37672013-02-05 17:53:52 +0000389 case AMDGPU::RETURN: {
Jakob Stoklund Olesenfdc37672013-02-05 17:53:52 +0000390 return BB;
391 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000392 }
393
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000394 MI.eraseFromParent();
Tom Stellard75aadc22012-12-11 21:25:42 +0000395 return BB;
396}
397
398//===----------------------------------------------------------------------===//
399// Custom DAG Lowering Operations
400//===----------------------------------------------------------------------===//
401
Tom Stellard75aadc22012-12-11 21:25:42 +0000402SDValue R600TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Tom Stellardc026e8b2013-06-28 15:47:08 +0000403 MachineFunction &MF = DAG.getMachineFunction();
404 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
Tom Stellard75aadc22012-12-11 21:25:42 +0000405 switch (Op.getOpcode()) {
406 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellard880a80a2014-06-17 16:53:14 +0000407 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
408 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Jan Vesely25f36272014-06-18 12:27:13 +0000409 case ISD::SHL_PARTS: return LowerSHLParts(Op, DAG);
Jan Veselyecf51332014-06-18 12:27:17 +0000410 case ISD::SRA_PARTS:
Jan Vesely900ff2e2014-06-18 12:27:15 +0000411 case ISD::SRL_PARTS: return LowerSRXParts(Op, DAG);
Jan Vesely808fff52015-04-30 17:15:56 +0000412 case ISD::UADDO: return LowerUADDSUBO(Op, DAG, ISD::ADD, AMDGPUISD::CARRY);
413 case ISD::USUBO: return LowerUADDSUBO(Op, DAG, ISD::SUB, AMDGPUISD::BORROW);
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000414 case ISD::FCOS:
415 case ISD::FSIN: return LowerTrig(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000416 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000417 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000418 case ISD::LOAD: {
419 SDValue Result = LowerLOAD(Op, DAG);
420 assert((!Result.getNode() ||
421 Result.getNode()->getNumValues() == 2) &&
422 "Load should return a value and a chain");
423 return Result;
424 }
425
Matt Arsenault1d555c42014-06-23 18:00:55 +0000426 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellardc026e8b2013-06-28 15:47:08 +0000427 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
Matt Arsenault81d06012016-03-07 21:10:13 +0000428 case ISD::FrameIndex: return lowerFrameIndex(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000429 case ISD::INTRINSIC_VOID: {
430 SDValue Chain = Op.getOperand(0);
431 unsigned IntrinsicID =
432 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
433 switch (IntrinsicID) {
Matt Arsenault82e5e1e2016-07-15 21:27:08 +0000434 case AMDGPUIntrinsic::r600_store_swizzle: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000435 SDLoc DL(Op);
Vincent Lejeuned80bc152013-02-14 16:55:06 +0000436 const SDValue Args[8] = {
437 Chain,
438 Op.getOperand(2), // Export Value
439 Op.getOperand(3), // ArrayBase
440 Op.getOperand(4), // Type
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000441 DAG.getConstant(0, DL, MVT::i32), // SWZ_X
442 DAG.getConstant(1, DL, MVT::i32), // SWZ_Y
443 DAG.getConstant(2, DL, MVT::i32), // SWZ_Z
444 DAG.getConstant(3, DL, MVT::i32) // SWZ_W
Vincent Lejeuned80bc152013-02-14 16:55:06 +0000445 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000446 return DAG.getNode(AMDGPUISD::EXPORT, DL, Op.getValueType(), Args);
Tom Stellard75aadc22012-12-11 21:25:42 +0000447 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000448
Tom Stellard75aadc22012-12-11 21:25:42 +0000449 // default for switch(IntrinsicID)
450 default: break;
451 }
452 // break out of case ISD::INTRINSIC_VOID in switch(Op.getOpcode())
453 break;
454 }
455 case ISD::INTRINSIC_WO_CHAIN: {
456 unsigned IntrinsicID =
457 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
458 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000459 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000460 switch(IntrinsicID) {
461 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Matt Arsenault59bd3012016-01-22 19:00:09 +0000462 case AMDGPUIntrinsic::r600_tex:
Matt Arsenaultf9245b72016-07-22 17:01:25 +0000463 case AMDGPUIntrinsic::r600_texc: {
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000464 unsigned TextureOp;
465 switch (IntrinsicID) {
Matt Arsenault59bd3012016-01-22 19:00:09 +0000466 case AMDGPUIntrinsic::r600_tex:
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000467 TextureOp = 0;
468 break;
Matt Arsenault59bd3012016-01-22 19:00:09 +0000469 case AMDGPUIntrinsic::r600_texc:
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000470 TextureOp = 1;
471 break;
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000472 default:
Matt Arsenault60a750f2016-07-26 21:03:38 +0000473 llvm_unreachable("unhandled texture operation");
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000474 }
475
476 SDValue TexArgs[19] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000477 DAG.getConstant(TextureOp, DL, MVT::i32),
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000478 Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000479 DAG.getConstant(0, DL, MVT::i32),
480 DAG.getConstant(1, DL, MVT::i32),
481 DAG.getConstant(2, DL, MVT::i32),
482 DAG.getConstant(3, DL, MVT::i32),
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000483 Op.getOperand(2),
484 Op.getOperand(3),
485 Op.getOperand(4),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000486 DAG.getConstant(0, DL, MVT::i32),
487 DAG.getConstant(1, DL, MVT::i32),
488 DAG.getConstant(2, DL, MVT::i32),
489 DAG.getConstant(3, DL, MVT::i32),
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000490 Op.getOperand(5),
491 Op.getOperand(6),
492 Op.getOperand(7),
493 Op.getOperand(8),
494 Op.getOperand(9),
495 Op.getOperand(10)
496 };
Craig Topper48d114b2014-04-26 18:35:24 +0000497 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, MVT::v4f32, TexArgs);
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000498 }
Matt Arsenaultca7f5702016-07-14 05:47:17 +0000499 case AMDGPUIntrinsic::r600_dot4: {
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000500 SDValue Args[8] = {
501 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000502 DAG.getConstant(0, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000503 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000504 DAG.getConstant(0, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000505 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000506 DAG.getConstant(1, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000507 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000508 DAG.getConstant(1, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000509 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000510 DAG.getConstant(2, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000511 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000512 DAG.getConstant(2, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000513 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000514 DAG.getConstant(3, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000515 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000516 DAG.getConstant(3, DL, MVT::i32))
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000517 };
Craig Topper48d114b2014-04-26 18:35:24 +0000518 return DAG.getNode(AMDGPUISD::DOT4, DL, MVT::f32, Args);
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000519 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000520
Jan Vesely2fa28c32016-07-10 21:20:29 +0000521 case Intrinsic::r600_implicitarg_ptr: {
522 MVT PtrVT = getPointerTy(DAG.getDataLayout(), AMDGPUAS::PARAM_I_ADDRESS);
523 uint32_t ByteOffset = getImplicitParameterOffset(MFI, FIRST_IMPLICIT);
524 return DAG.getConstant(ByteOffset, DL, PtrVT);
525 }
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000526 case Intrinsic::r600_read_ngroups_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000527 return LowerImplicitParameter(DAG, VT, DL, 0);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000528 case Intrinsic::r600_read_ngroups_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000529 return LowerImplicitParameter(DAG, VT, DL, 1);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000530 case Intrinsic::r600_read_ngroups_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000531 return LowerImplicitParameter(DAG, VT, DL, 2);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000532 case Intrinsic::r600_read_global_size_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000533 return LowerImplicitParameter(DAG, VT, DL, 3);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000534 case Intrinsic::r600_read_global_size_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000535 return LowerImplicitParameter(DAG, VT, DL, 4);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000536 case Intrinsic::r600_read_global_size_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000537 return LowerImplicitParameter(DAG, VT, DL, 5);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000538 case Intrinsic::r600_read_local_size_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000539 return LowerImplicitParameter(DAG, VT, DL, 6);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000540 case Intrinsic::r600_read_local_size_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000541 return LowerImplicitParameter(DAG, VT, DL, 7);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000542 case Intrinsic::r600_read_local_size_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000543 return LowerImplicitParameter(DAG, VT, DL, 8);
544
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000545 case Intrinsic::r600_read_tgid_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000546 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
547 AMDGPU::T1_X, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000548 case Intrinsic::r600_read_tgid_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000549 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
550 AMDGPU::T1_Y, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000551 case Intrinsic::r600_read_tgid_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000552 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
553 AMDGPU::T1_Z, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000554 case Intrinsic::r600_read_tidig_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000555 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
556 AMDGPU::T0_X, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000557 case Intrinsic::r600_read_tidig_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000558 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
559 AMDGPU::T0_Y, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000560 case Intrinsic::r600_read_tidig_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000561 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
562 AMDGPU::T0_Z, VT);
Matt Arsenaultbef34e22016-01-22 21:30:34 +0000563
Matt Arsenault09b2c4a2016-07-15 21:26:52 +0000564 case Intrinsic::r600_recipsqrt_ieee:
565 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
Matt Arsenaultbef34e22016-01-22 21:30:34 +0000566
Matt Arsenault09b2c4a2016-07-15 21:26:52 +0000567 case Intrinsic::r600_recipsqrt_clamped:
568 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
Tom Stellard75aadc22012-12-11 21:25:42 +0000569 }
Matt Arsenault09b2c4a2016-07-15 21:26:52 +0000570
Tom Stellard75aadc22012-12-11 21:25:42 +0000571 // break out of case ISD::INTRINSIC_WO_CHAIN in switch(Op.getOpcode())
572 break;
573 }
574 } // end switch(Op.getOpcode())
575 return SDValue();
576}
577
578void R600TargetLowering::ReplaceNodeResults(SDNode *N,
579 SmallVectorImpl<SDValue> &Results,
580 SelectionDAG &DAG) const {
581 switch (N->getOpcode()) {
Matt Arsenaultd125d742014-03-27 17:23:24 +0000582 default:
583 AMDGPUTargetLowering::ReplaceNodeResults(N, Results, DAG);
584 return;
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000585 case ISD::FP_TO_UINT:
586 if (N->getValueType(0) == MVT::i1) {
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000587 Results.push_back(lowerFP_TO_UINT(N->getOperand(0), DAG));
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000588 return;
589 }
Justin Bognerb03fd122016-08-17 05:10:15 +0000590 // Since we don't care about out of bounds values we can use FP_TO_SINT for
591 // uints too. The DAGLegalizer code for uint considers some extra cases
592 // which are not necessary here.
593 LLVM_FALLTHROUGH;
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000594 case ISD::FP_TO_SINT: {
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000595 if (N->getValueType(0) == MVT::i1) {
596 Results.push_back(lowerFP_TO_SINT(N->getOperand(0), DAG));
597 return;
598 }
599
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000600 SDValue Result;
601 if (expandFP_TO_SINT(N, Result, DAG))
602 Results.push_back(Result);
Tom Stellard365366f2013-01-23 02:09:06 +0000603 return;
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000604 }
Jan Vesely343cd6f02014-06-22 21:43:01 +0000605 case ISD::SDIVREM: {
606 SDValue Op = SDValue(N, 1);
607 SDValue RES = LowerSDIVREM(Op, DAG);
608 Results.push_back(RES);
609 Results.push_back(RES.getValue(1));
610 break;
611 }
612 case ISD::UDIVREM: {
613 SDValue Op = SDValue(N, 0);
Tom Stellardbf69d762014-11-15 01:07:53 +0000614 LowerUDIVREM64(Op, DAG, Results);
Jan Vesely343cd6f02014-06-22 21:43:01 +0000615 break;
616 }
617 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000618}
619
Tom Stellard880a80a2014-06-17 16:53:14 +0000620SDValue R600TargetLowering::vectorToVerticalVector(SelectionDAG &DAG,
621 SDValue Vector) const {
622
623 SDLoc DL(Vector);
624 EVT VecVT = Vector.getValueType();
625 EVT EltVT = VecVT.getVectorElementType();
626 SmallVector<SDValue, 8> Args;
627
628 for (unsigned i = 0, e = VecVT.getVectorNumElements();
629 i != e; ++i) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000630 Args.push_back(DAG.getNode(
631 ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Vector,
632 DAG.getConstant(i, DL, getVectorIdxTy(DAG.getDataLayout()))));
Tom Stellard880a80a2014-06-17 16:53:14 +0000633 }
634
635 return DAG.getNode(AMDGPUISD::BUILD_VERTICAL_VECTOR, DL, VecVT, Args);
636}
637
638SDValue R600TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
639 SelectionDAG &DAG) const {
640
641 SDLoc DL(Op);
642 SDValue Vector = Op.getOperand(0);
643 SDValue Index = Op.getOperand(1);
644
645 if (isa<ConstantSDNode>(Index) ||
646 Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR)
647 return Op;
648
649 Vector = vectorToVerticalVector(DAG, Vector);
650 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, Op.getValueType(),
651 Vector, Index);
652}
653
654SDValue R600TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
655 SelectionDAG &DAG) const {
656 SDLoc DL(Op);
657 SDValue Vector = Op.getOperand(0);
658 SDValue Value = Op.getOperand(1);
659 SDValue Index = Op.getOperand(2);
660
661 if (isa<ConstantSDNode>(Index) ||
662 Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR)
663 return Op;
664
665 Vector = vectorToVerticalVector(DAG, Vector);
666 SDValue Insert = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, Op.getValueType(),
667 Vector, Value, Index);
668 return vectorToVerticalVector(DAG, Insert);
669}
670
Tom Stellard27233b72016-05-02 18:05:17 +0000671SDValue R600TargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
672 SDValue Op,
673 SelectionDAG &DAG) const {
674
675 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
676 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
677 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
678
679 const DataLayout &DL = DAG.getDataLayout();
680 const GlobalValue *GV = GSD->getGlobal();
Tom Stellard27233b72016-05-02 18:05:17 +0000681 MVT ConstPtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
682
Jan Veselyf97de002016-05-13 20:39:29 +0000683 SDValue GA = DAG.getTargetGlobalAddress(GV, SDLoc(GSD), ConstPtrVT);
684 return DAG.getNode(AMDGPUISD::CONST_DATA_PTR, SDLoc(GSD), ConstPtrVT, GA);
Tom Stellard27233b72016-05-02 18:05:17 +0000685}
686
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000687SDValue R600TargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
688 // On hw >= R700, COS/SIN input must be between -1. and 1.
689 // Thus we lower them to TRIG ( FRACT ( x / 2Pi + 0.5) - 0.5)
690 EVT VT = Op.getValueType();
691 SDValue Arg = Op.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000692 SDLoc DL(Op);
Sanjay Patela2607012015-09-16 16:31:21 +0000693
694 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000695 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
696 DAG.getNode(ISD::FADD, DL, VT,
697 DAG.getNode(ISD::FMUL, DL, VT, Arg,
698 DAG.getConstantFP(0.15915494309, DL, MVT::f32)),
699 DAG.getConstantFP(0.5, DL, MVT::f32)));
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000700 unsigned TrigNode;
701 switch (Op.getOpcode()) {
702 case ISD::FCOS:
703 TrigNode = AMDGPUISD::COS_HW;
704 break;
705 case ISD::FSIN:
706 TrigNode = AMDGPUISD::SIN_HW;
707 break;
708 default:
709 llvm_unreachable("Wrong trig opcode");
710 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000711 SDValue TrigVal = DAG.getNode(TrigNode, DL, VT,
712 DAG.getNode(ISD::FADD, DL, VT, FractPart,
713 DAG.getConstantFP(-0.5, DL, MVT::f32)));
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000714 if (Gen >= R600Subtarget::R700)
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000715 return TrigVal;
716 // On R600 hw, COS/SIN input must be between -Pi and Pi.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000717 return DAG.getNode(ISD::FMUL, DL, VT, TrigVal,
718 DAG.getConstantFP(3.14159265359, DL, MVT::f32));
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000719}
720
Jan Vesely25f36272014-06-18 12:27:13 +0000721SDValue R600TargetLowering::LowerSHLParts(SDValue Op, SelectionDAG &DAG) const {
722 SDLoc DL(Op);
723 EVT VT = Op.getValueType();
724
725 SDValue Lo = Op.getOperand(0);
726 SDValue Hi = Op.getOperand(1);
727 SDValue Shift = Op.getOperand(2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000728 SDValue Zero = DAG.getConstant(0, DL, VT);
729 SDValue One = DAG.getConstant(1, DL, VT);
Jan Vesely25f36272014-06-18 12:27:13 +0000730
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000731 SDValue Width = DAG.getConstant(VT.getSizeInBits(), DL, VT);
732 SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, DL, VT);
Jan Vesely25f36272014-06-18 12:27:13 +0000733 SDValue BigShift = DAG.getNode(ISD::SUB, DL, VT, Shift, Width);
734 SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift);
735
736 // The dance around Width1 is necessary for 0 special case.
737 // Without it the CompShift might be 32, producing incorrect results in
738 // Overflow. So we do the shift in two steps, the alternative is to
739 // add a conditional to filter the special case.
740
741 SDValue Overflow = DAG.getNode(ISD::SRL, DL, VT, Lo, CompShift);
742 Overflow = DAG.getNode(ISD::SRL, DL, VT, Overflow, One);
743
744 SDValue HiSmall = DAG.getNode(ISD::SHL, DL, VT, Hi, Shift);
745 HiSmall = DAG.getNode(ISD::OR, DL, VT, HiSmall, Overflow);
746 SDValue LoSmall = DAG.getNode(ISD::SHL, DL, VT, Lo, Shift);
747
748 SDValue HiBig = DAG.getNode(ISD::SHL, DL, VT, Lo, BigShift);
749 SDValue LoBig = Zero;
750
751 Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT);
752 Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT);
753
754 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi);
755}
756
Jan Vesely900ff2e2014-06-18 12:27:15 +0000757SDValue R600TargetLowering::LowerSRXParts(SDValue Op, SelectionDAG &DAG) const {
758 SDLoc DL(Op);
759 EVT VT = Op.getValueType();
760
761 SDValue Lo = Op.getOperand(0);
762 SDValue Hi = Op.getOperand(1);
763 SDValue Shift = Op.getOperand(2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000764 SDValue Zero = DAG.getConstant(0, DL, VT);
765 SDValue One = DAG.getConstant(1, DL, VT);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000766
Jan Veselyecf51332014-06-18 12:27:17 +0000767 const bool SRA = Op.getOpcode() == ISD::SRA_PARTS;
768
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000769 SDValue Width = DAG.getConstant(VT.getSizeInBits(), DL, VT);
770 SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, DL, VT);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000771 SDValue BigShift = DAG.getNode(ISD::SUB, DL, VT, Shift, Width);
772 SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift);
773
774 // The dance around Width1 is necessary for 0 special case.
775 // Without it the CompShift might be 32, producing incorrect results in
776 // Overflow. So we do the shift in two steps, the alternative is to
777 // add a conditional to filter the special case.
778
779 SDValue Overflow = DAG.getNode(ISD::SHL, DL, VT, Hi, CompShift);
780 Overflow = DAG.getNode(ISD::SHL, DL, VT, Overflow, One);
781
Jan Veselyecf51332014-06-18 12:27:17 +0000782 SDValue HiSmall = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, Shift);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000783 SDValue LoSmall = DAG.getNode(ISD::SRL, DL, VT, Lo, Shift);
784 LoSmall = DAG.getNode(ISD::OR, DL, VT, LoSmall, Overflow);
785
Jan Veselyecf51332014-06-18 12:27:17 +0000786 SDValue LoBig = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, BigShift);
787 SDValue HiBig = SRA ? DAG.getNode(ISD::SRA, DL, VT, Hi, Width1) : Zero;
Jan Vesely900ff2e2014-06-18 12:27:15 +0000788
789 Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT);
790 Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT);
791
792 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi);
793}
794
Jan Vesely808fff52015-04-30 17:15:56 +0000795SDValue R600TargetLowering::LowerUADDSUBO(SDValue Op, SelectionDAG &DAG,
796 unsigned mainop, unsigned ovf) const {
797 SDLoc DL(Op);
798 EVT VT = Op.getValueType();
799
800 SDValue Lo = Op.getOperand(0);
801 SDValue Hi = Op.getOperand(1);
802
803 SDValue OVF = DAG.getNode(ovf, DL, VT, Lo, Hi);
804 // Extend sign.
805 OVF = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, OVF,
806 DAG.getValueType(MVT::i1));
807
808 SDValue Res = DAG.getNode(mainop, DL, VT, Lo, Hi);
809
810 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT, VT), Res, OVF);
811}
812
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000813SDValue R600TargetLowering::lowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000814 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000815 return DAG.getNode(
816 ISD::SETCC,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000817 DL,
Tom Stellard75aadc22012-12-11 21:25:42 +0000818 MVT::i1,
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000819 Op, DAG.getConstantFP(1.0f, DL, MVT::f32),
820 DAG.getCondCode(ISD::SETEQ));
821}
822
823SDValue R600TargetLowering::lowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const {
824 SDLoc DL(Op);
825 return DAG.getNode(
826 ISD::SETCC,
827 DL,
828 MVT::i1,
829 Op, DAG.getConstantFP(-1.0f, DL, MVT::f32),
830 DAG.getCondCode(ISD::SETEQ));
Tom Stellard75aadc22012-12-11 21:25:42 +0000831}
832
Tom Stellard75aadc22012-12-11 21:25:42 +0000833SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000834 const SDLoc &DL,
Tom Stellard75aadc22012-12-11 21:25:42 +0000835 unsigned DwordOffset) const {
836 unsigned ByteOffset = DwordOffset * 4;
837 PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
Tom Stellard1e803092013-07-23 01:48:18 +0000838 AMDGPUAS::CONSTANT_BUFFER_0);
Tom Stellard75aadc22012-12-11 21:25:42 +0000839
840 // We shouldn't be using an offset wider than 16-bits for implicit parameters.
841 assert(isInt<16>(ByteOffset));
842
843 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000844 DAG.getConstant(ByteOffset, DL, MVT::i32), // PTR
Justin Lebar9c375812016-07-15 18:27:10 +0000845 MachinePointerInfo(ConstantPointerNull::get(PtrType)));
Tom Stellard75aadc22012-12-11 21:25:42 +0000846}
847
Tom Stellard75aadc22012-12-11 21:25:42 +0000848bool R600TargetLowering::isZero(SDValue Op) const {
849 if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) {
850 return Cst->isNullValue();
851 } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){
852 return CstFP->isZero();
853 } else {
854 return false;
855 }
856}
857
Matt Arsenault6b6a2c32016-03-11 08:00:27 +0000858bool R600TargetLowering::isHWTrueValue(SDValue Op) const {
859 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
860 return CFP->isExactlyValue(1.0);
861 }
862 return isAllOnesConstant(Op);
863}
864
865bool R600TargetLowering::isHWFalseValue(SDValue Op) const {
866 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
867 return CFP->getValueAPF().isZero();
868 }
869 return isNullConstant(Op);
870}
871
Tom Stellard75aadc22012-12-11 21:25:42 +0000872SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000873 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000874 EVT VT = Op.getValueType();
875
876 SDValue LHS = Op.getOperand(0);
877 SDValue RHS = Op.getOperand(1);
878 SDValue True = Op.getOperand(2);
879 SDValue False = Op.getOperand(3);
880 SDValue CC = Op.getOperand(4);
881 SDValue Temp;
882
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +0000883 if (VT == MVT::f32) {
884 DAGCombinerInfo DCI(DAG, AfterLegalizeVectorOps, true, nullptr);
885 SDValue MinMax = CombineFMinMaxLegacy(DL, VT, LHS, RHS, True, False, CC, DCI);
886 if (MinMax)
887 return MinMax;
888 }
889
Tom Stellard75aadc22012-12-11 21:25:42 +0000890 // LHS and RHS are guaranteed to be the same value type
891 EVT CompareVT = LHS.getValueType();
892
893 // Check if we can lower this to a native operation.
894
Tom Stellard2add82d2013-03-08 15:37:09 +0000895 // Try to lower to a SET* instruction:
896 //
897 // SET* can match the following patterns:
898 //
Tom Stellardcd428182013-09-28 02:50:38 +0000899 // select_cc f32, f32, -1, 0, cc_supported
900 // select_cc f32, f32, 1.0f, 0.0f, cc_supported
901 // select_cc i32, i32, -1, 0, cc_supported
Tom Stellard2add82d2013-03-08 15:37:09 +0000902 //
903
904 // Move hardware True/False values to the correct operand.
Tom Stellardcd428182013-09-28 02:50:38 +0000905 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
906 ISD::CondCode InverseCC =
907 ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
Tom Stellard5694d302013-09-28 02:50:43 +0000908 if (isHWTrueValue(False) && isHWFalseValue(True)) {
909 if (isCondCodeLegal(InverseCC, CompareVT.getSimpleVT())) {
910 std::swap(False, True);
911 CC = DAG.getCondCode(InverseCC);
912 } else {
913 ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InverseCC);
914 if (isCondCodeLegal(SwapInvCC, CompareVT.getSimpleVT())) {
915 std::swap(False, True);
916 std::swap(LHS, RHS);
917 CC = DAG.getCondCode(SwapInvCC);
918 }
919 }
Tom Stellard2add82d2013-03-08 15:37:09 +0000920 }
921
922 if (isHWTrueValue(True) && isHWFalseValue(False) &&
923 (CompareVT == VT || VT == MVT::i32)) {
924 // This can be matched by a SET* instruction.
925 return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
926 }
927
Tom Stellard75aadc22012-12-11 21:25:42 +0000928 // Try to lower to a CND* instruction:
Tom Stellard2add82d2013-03-08 15:37:09 +0000929 //
930 // CND* can match the following patterns:
931 //
Tom Stellardcd428182013-09-28 02:50:38 +0000932 // select_cc f32, 0.0, f32, f32, cc_supported
933 // select_cc f32, 0.0, i32, i32, cc_supported
934 // select_cc i32, 0, f32, f32, cc_supported
935 // select_cc i32, 0, i32, i32, cc_supported
Tom Stellard2add82d2013-03-08 15:37:09 +0000936 //
Tom Stellardcd428182013-09-28 02:50:38 +0000937
938 // Try to move the zero value to the RHS
939 if (isZero(LHS)) {
940 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
941 // Try swapping the operands
942 ISD::CondCode CCSwapped = ISD::getSetCCSwappedOperands(CCOpcode);
943 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
944 std::swap(LHS, RHS);
945 CC = DAG.getCondCode(CCSwapped);
946 } else {
947 // Try inverting the conditon and then swapping the operands
948 ISD::CondCode CCInv = ISD::getSetCCInverse(CCOpcode, CompareVT.isInteger());
949 CCSwapped = ISD::getSetCCSwappedOperands(CCInv);
950 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
951 std::swap(True, False);
952 std::swap(LHS, RHS);
953 CC = DAG.getCondCode(CCSwapped);
954 }
955 }
956 }
957 if (isZero(RHS)) {
958 SDValue Cond = LHS;
959 SDValue Zero = RHS;
Tom Stellard75aadc22012-12-11 21:25:42 +0000960 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
961 if (CompareVT != VT) {
962 // Bitcast True / False to the correct types. This will end up being
963 // a nop, but it allows us to define only a single pattern in the
964 // .TD files for each CND* instruction rather than having to have
965 // one pattern for integer True/False and one for fp True/False
966 True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
967 False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
968 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000969
970 switch (CCOpcode) {
971 case ISD::SETONE:
972 case ISD::SETUNE:
973 case ISD::SETNE:
Tom Stellard75aadc22012-12-11 21:25:42 +0000974 CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
975 Temp = True;
976 True = False;
977 False = Temp;
978 break;
979 default:
980 break;
981 }
982 SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
983 Cond, Zero,
984 True, False,
985 DAG.getCondCode(CCOpcode));
986 return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
987 }
988
Tom Stellard75aadc22012-12-11 21:25:42 +0000989 // If we make it this for it means we have no native instructions to handle
990 // this SELECT_CC, so we must lower it.
991 SDValue HWTrue, HWFalse;
992
993 if (CompareVT == MVT::f32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000994 HWTrue = DAG.getConstantFP(1.0f, DL, CompareVT);
995 HWFalse = DAG.getConstantFP(0.0f, DL, CompareVT);
Tom Stellard75aadc22012-12-11 21:25:42 +0000996 } else if (CompareVT == MVT::i32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000997 HWTrue = DAG.getConstant(-1, DL, CompareVT);
998 HWFalse = DAG.getConstant(0, DL, CompareVT);
Tom Stellard75aadc22012-12-11 21:25:42 +0000999 }
1000 else {
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +00001001 llvm_unreachable("Unhandled value type in LowerSELECT_CC");
Tom Stellard75aadc22012-12-11 21:25:42 +00001002 }
1003
1004 // Lower this unsupported SELECT_CC into a combination of two supported
1005 // SELECT_CC operations.
1006 SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
1007
1008 return DAG.getNode(ISD::SELECT_CC, DL, VT,
1009 Cond, HWFalse,
1010 True, False,
1011 DAG.getCondCode(ISD::SETNE));
1012}
1013
Alp Tokercb402912014-01-24 17:20:08 +00001014/// LLVM generates byte-addressed pointers. For indirect addressing, we need to
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001015/// convert these pointers to a register index. Each register holds
1016/// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
1017/// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
1018/// for indirect addressing.
1019SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
1020 unsigned StackWidth,
1021 SelectionDAG &DAG) const {
1022 unsigned SRLPad;
1023 switch(StackWidth) {
1024 case 1:
1025 SRLPad = 2;
1026 break;
1027 case 2:
1028 SRLPad = 3;
1029 break;
1030 case 4:
1031 SRLPad = 4;
1032 break;
1033 default: llvm_unreachable("Invalid stack width");
1034 }
1035
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001036 SDLoc DL(Ptr);
1037 return DAG.getNode(ISD::SRL, DL, Ptr.getValueType(), Ptr,
1038 DAG.getConstant(SRLPad, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001039}
1040
1041void R600TargetLowering::getStackAddress(unsigned StackWidth,
1042 unsigned ElemIdx,
1043 unsigned &Channel,
1044 unsigned &PtrIncr) const {
1045 switch (StackWidth) {
1046 default:
1047 case 1:
1048 Channel = 0;
1049 if (ElemIdx > 0) {
1050 PtrIncr = 1;
1051 } else {
1052 PtrIncr = 0;
1053 }
1054 break;
1055 case 2:
1056 Channel = ElemIdx % 2;
1057 if (ElemIdx == 2) {
1058 PtrIncr = 1;
1059 } else {
1060 PtrIncr = 0;
1061 }
1062 break;
1063 case 4:
1064 Channel = ElemIdx;
1065 PtrIncr = 0;
1066 break;
1067 }
1068}
1069
Matt Arsenault95245662016-02-11 05:32:46 +00001070SDValue R600TargetLowering::lowerPrivateTruncStore(StoreSDNode *Store,
1071 SelectionDAG &DAG) const {
1072 SDLoc DL(Store);
Tom Stellard75aadc22012-12-11 21:25:42 +00001073
Matt Arsenault95245662016-02-11 05:32:46 +00001074 unsigned Mask = 0;
1075 if (Store->getMemoryVT() == MVT::i8) {
1076 Mask = 0xff;
1077 } else if (Store->getMemoryVT() == MVT::i16) {
1078 Mask = 0xffff;
1079 }
1080
1081 SDValue Chain = Store->getChain();
1082 SDValue BasePtr = Store->getBasePtr();
1083 EVT MemVT = Store->getMemoryVT();
1084
1085 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, BasePtr,
1086 DAG.getConstant(2, DL, MVT::i32));
1087 SDValue Dst = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
1088 Chain, Ptr,
1089 DAG.getTargetConstant(0, DL, MVT::i32));
1090
1091 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, BasePtr,
1092 DAG.getConstant(0x3, DL, MVT::i32));
1093
1094 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1095 DAG.getConstant(3, DL, MVT::i32));
1096
1097 SDValue SExtValue = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i32,
1098 Store->getValue());
1099
1100 SDValue MaskedValue = DAG.getZeroExtendInReg(SExtValue, DL, MemVT);
1101
1102 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
1103 MaskedValue, ShiftAmt);
1104
1105 SDValue DstMask = DAG.getNode(ISD::SHL, DL, MVT::i32,
1106 DAG.getConstant(Mask, DL, MVT::i32),
1107 ShiftAmt);
1108 DstMask = DAG.getNode(ISD::XOR, DL, MVT::i32, DstMask,
1109 DAG.getConstant(0xffffffff, DL, MVT::i32));
1110 Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
1111
1112 SDValue Value = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
1113 return DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1114 Chain, Value, Ptr,
1115 DAG.getTargetConstant(0, DL, MVT::i32));
1116}
1117
1118SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1119 if (SDValue Result = AMDGPUTargetLowering::MergeVectorStore(Op, DAG))
Tom Stellardfbab8272013-08-16 01:12:11 +00001120 return Result;
Tom Stellardfbab8272013-08-16 01:12:11 +00001121
Matt Arsenault95245662016-02-11 05:32:46 +00001122 StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
1123 unsigned AS = StoreNode->getAddressSpace();
1124 SDValue Value = StoreNode->getValue();
1125 EVT ValueVT = Value.getValueType();
1126
1127 if ((AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::PRIVATE_ADDRESS) &&
1128 ValueVT.isVector()) {
1129 return SplitVectorStore(Op, DAG);
1130 }
1131
1132 SDLoc DL(Op);
1133 SDValue Chain = StoreNode->getChain();
1134 SDValue Ptr = StoreNode->getBasePtr();
1135
1136 if (AS == AMDGPUAS::GLOBAL_ADDRESS) {
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001137 if (StoreNode->isTruncatingStore()) {
1138 EVT VT = Value.getValueType();
Tom Stellardfbab8272013-08-16 01:12:11 +00001139 assert(VT.bitsLE(MVT::i32));
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001140 EVT MemVT = StoreNode->getMemoryVT();
1141 SDValue MaskConstant;
1142 if (MemVT == MVT::i8) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001143 MaskConstant = DAG.getConstant(0xFF, DL, MVT::i32);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001144 } else {
1145 assert(MemVT == MVT::i16);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001146 MaskConstant = DAG.getConstant(0xFFFF, DL, MVT::i32);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001147 }
1148 SDValue DWordAddr = DAG.getNode(ISD::SRL, DL, VT, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001149 DAG.getConstant(2, DL, MVT::i32));
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001150 SDValue ByteIndex = DAG.getNode(ISD::AND, DL, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001151 DAG.getConstant(0x00000003, DL, VT));
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001152 SDValue TruncValue = DAG.getNode(ISD::AND, DL, VT, Value, MaskConstant);
1153 SDValue Shift = DAG.getNode(ISD::SHL, DL, VT, ByteIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001154 DAG.getConstant(3, DL, VT));
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001155 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, VT, TruncValue, Shift);
1156 SDValue Mask = DAG.getNode(ISD::SHL, DL, VT, MaskConstant, Shift);
1157 // XXX: If we add a 64-bit ZW register class, then we could use a 2 x i32
1158 // vector instead.
1159 SDValue Src[4] = {
1160 ShiftedValue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001161 DAG.getConstant(0, DL, MVT::i32),
1162 DAG.getConstant(0, DL, MVT::i32),
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001163 Mask
1164 };
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001165 SDValue Input = DAG.getBuildVector(MVT::v4i32, DL, Src);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001166 SDValue Args[3] = { Chain, Input, DWordAddr };
1167 return DAG.getMemIntrinsicNode(AMDGPUISD::STORE_MSKOR, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00001168 Op->getVTList(), Args, MemVT,
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001169 StoreNode->getMemOperand());
1170 } else if (Ptr->getOpcode() != AMDGPUISD::DWORDADDR &&
Matt Arsenault95245662016-02-11 05:32:46 +00001171 ValueVT.bitsGE(MVT::i32)) {
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001172 // Convert pointer from byte address to dword address.
1173 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, Ptr.getValueType(),
1174 DAG.getNode(ISD::SRL, DL, Ptr.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001175 Ptr, DAG.getConstant(2, DL, MVT::i32)));
Tom Stellard75aadc22012-12-11 21:25:42 +00001176
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001177 if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +00001178 llvm_unreachable("Truncated and indexed stores not supported yet");
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001179 } else {
1180 Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
1181 }
1182 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001183 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001184 }
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001185
Matt Arsenault95245662016-02-11 05:32:46 +00001186 if (AS != AMDGPUAS::PRIVATE_ADDRESS)
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001187 return SDValue();
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001188
Matt Arsenault95245662016-02-11 05:32:46 +00001189 EVT MemVT = StoreNode->getMemoryVT();
1190 if (MemVT.bitsLT(MVT::i32))
1191 return lowerPrivateTruncStore(StoreNode, DAG);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001192
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00001193 // Lowering for indirect addressing
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001194 const MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001195 const R600FrameLowering *TFL = getSubtarget()->getFrameLowering();
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001196 unsigned StackWidth = TFL->getStackWidth(MF);
1197
1198 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1199
1200 if (ValueVT.isVector()) {
1201 unsigned NumElemVT = ValueVT.getVectorNumElements();
1202 EVT ElemVT = ValueVT.getVectorElementType();
Craig Topper48d114b2014-04-26 18:35:24 +00001203 SmallVector<SDValue, 4> Stores(NumElemVT);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001204
1205 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1206 "vector width in load");
1207
1208 for (unsigned i = 0; i < NumElemVT; ++i) {
1209 unsigned Channel, PtrIncr;
1210 getStackAddress(StackWidth, i, Channel, PtrIncr);
1211 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001212 DAG.getConstant(PtrIncr, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001213 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001214 Value, DAG.getConstant(i, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001215
1216 Stores[i] = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1217 Chain, Elem, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001218 DAG.getTargetConstant(Channel, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001219 }
Craig Topper48d114b2014-04-26 18:35:24 +00001220 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Stores);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001221 } else {
1222 if (ValueVT == MVT::i8) {
1223 Value = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Value);
1224 }
1225 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other, Chain, Value, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001226 DAG.getTargetConstant(0, DL, MVT::i32)); // Channel
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001227 }
1228
1229 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001230}
1231
Tom Stellard365366f2013-01-23 02:09:06 +00001232// return (512 + (kc_bank << 12)
1233static int
1234ConstantAddressBlock(unsigned AddressSpace) {
1235 switch (AddressSpace) {
1236 case AMDGPUAS::CONSTANT_BUFFER_0:
1237 return 512;
1238 case AMDGPUAS::CONSTANT_BUFFER_1:
1239 return 512 + 4096;
1240 case AMDGPUAS::CONSTANT_BUFFER_2:
1241 return 512 + 4096 * 2;
1242 case AMDGPUAS::CONSTANT_BUFFER_3:
1243 return 512 + 4096 * 3;
1244 case AMDGPUAS::CONSTANT_BUFFER_4:
1245 return 512 + 4096 * 4;
1246 case AMDGPUAS::CONSTANT_BUFFER_5:
1247 return 512 + 4096 * 5;
1248 case AMDGPUAS::CONSTANT_BUFFER_6:
1249 return 512 + 4096 * 6;
1250 case AMDGPUAS::CONSTANT_BUFFER_7:
1251 return 512 + 4096 * 7;
1252 case AMDGPUAS::CONSTANT_BUFFER_8:
1253 return 512 + 4096 * 8;
1254 case AMDGPUAS::CONSTANT_BUFFER_9:
1255 return 512 + 4096 * 9;
1256 case AMDGPUAS::CONSTANT_BUFFER_10:
1257 return 512 + 4096 * 10;
1258 case AMDGPUAS::CONSTANT_BUFFER_11:
1259 return 512 + 4096 * 11;
1260 case AMDGPUAS::CONSTANT_BUFFER_12:
1261 return 512 + 4096 * 12;
1262 case AMDGPUAS::CONSTANT_BUFFER_13:
1263 return 512 + 4096 * 13;
1264 case AMDGPUAS::CONSTANT_BUFFER_14:
1265 return 512 + 4096 * 14;
1266 case AMDGPUAS::CONSTANT_BUFFER_15:
1267 return 512 + 4096 * 15;
1268 default:
1269 return -1;
1270 }
1271}
1272
Matt Arsenault6dfda962016-02-10 18:21:39 +00001273SDValue R600TargetLowering::lowerPrivateExtLoad(SDValue Op,
1274 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001275 SDLoc DL(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001276 LoadSDNode *Load = cast<LoadSDNode>(Op);
1277 ISD::LoadExtType ExtType = Load->getExtensionType();
1278 EVT MemVT = Load->getMemoryVT();
Tom Stellard365366f2013-01-23 02:09:06 +00001279
Matt Arsenault6dfda962016-02-10 18:21:39 +00001280 // <SI && AS=PRIVATE && EXTLOAD && size < 32bit,
1281 // register (2-)byte extract.
1282
1283 // Get Register holding the target.
1284 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Load->getBasePtr(),
1285 DAG.getConstant(2, DL, MVT::i32));
1286 // Load the Register.
1287 SDValue Ret = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, Op.getValueType(),
1288 Load->getChain(),
1289 Ptr,
1290 DAG.getTargetConstant(0, DL, MVT::i32),
1291 Op.getOperand(2));
1292
1293 // Get offset within the register.
1294 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32,
1295 Load->getBasePtr(),
1296 DAG.getConstant(0x3, DL, MVT::i32));
1297
1298 // Bit offset of target byte (byteIdx * 8).
1299 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1300 DAG.getConstant(3, DL, MVT::i32));
1301
1302 // Shift to the right.
1303 Ret = DAG.getNode(ISD::SRL, DL, MVT::i32, Ret, ShiftAmt);
1304
1305 // Eliminate the upper bits by setting them to ...
1306 EVT MemEltVT = MemVT.getScalarType();
1307
1308 // ... ones.
1309 if (ExtType == ISD::SEXTLOAD) {
1310 SDValue MemEltVTNode = DAG.getValueType(MemEltVT);
1311
1312 SDValue Ops[] = {
1313 DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, Ret, MemEltVTNode),
1314 Load->getChain()
1315 };
1316
1317 return DAG.getMergeValues(Ops, DL);
1318 }
1319
1320 // ... or zeros.
1321 SDValue Ops[] = {
1322 DAG.getZeroExtendInReg(Ret, DL, MemEltVT),
1323 Load->getChain()
1324 };
1325
1326 return DAG.getMergeValues(Ops, DL);
1327}
1328
1329SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1330 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1331 unsigned AS = LoadNode->getAddressSpace();
1332 EVT MemVT = LoadNode->getMemoryVT();
1333 ISD::LoadExtType ExtType = LoadNode->getExtensionType();
1334
1335 if (AS == AMDGPUAS::PRIVATE_ADDRESS &&
1336 ExtType != ISD::NON_EXTLOAD && MemVT.bitsLT(MVT::i32)) {
1337 return lowerPrivateExtLoad(Op, DAG);
1338 }
1339
1340 SDLoc DL(Op);
1341 EVT VT = Op.getValueType();
1342 SDValue Chain = LoadNode->getChain();
1343 SDValue Ptr = LoadNode->getBasePtr();
Tom Stellarde9373602014-01-22 19:24:14 +00001344
Tom Stellard35bb18c2013-08-26 15:06:04 +00001345 if (LoadNode->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS && VT.isVector()) {
1346 SDValue MergedValues[2] = {
Matt Arsenault9c499c32016-04-14 23:31:26 +00001347 scalarizeVectorLoad(LoadNode, DAG),
Tom Stellard35bb18c2013-08-26 15:06:04 +00001348 Chain
1349 };
Craig Topper64941d92014-04-27 19:20:57 +00001350 return DAG.getMergeValues(MergedValues, DL);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001351 }
1352
Tom Stellard365366f2013-01-23 02:09:06 +00001353 int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
Matt Arsenault00a0d6f2013-11-13 02:39:07 +00001354 if (ConstantBlock > -1 &&
1355 ((LoadNode->getExtensionType() == ISD::NON_EXTLOAD) ||
1356 (LoadNode->getExtensionType() == ISD::ZEXTLOAD))) {
Tom Stellard365366f2013-01-23 02:09:06 +00001357 SDValue Result;
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001358 if (isa<ConstantExpr>(LoadNode->getMemOperand()->getValue()) ||
1359 isa<Constant>(LoadNode->getMemOperand()->getValue()) ||
Matt Arsenaultef1a9502013-11-01 17:39:26 +00001360 isa<ConstantSDNode>(Ptr)) {
Tom Stellard365366f2013-01-23 02:09:06 +00001361 SDValue Slots[4];
1362 for (unsigned i = 0; i < 4; i++) {
1363 // We want Const position encoded with the following formula :
1364 // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
1365 // const_index is Ptr computed by llvm using an alignment of 16.
1366 // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
1367 // then div by 4 at the ISel step
1368 SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001369 DAG.getConstant(4 * i + ConstantBlock * 16, DL, MVT::i32));
Tom Stellard365366f2013-01-23 02:09:06 +00001370 Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
1371 }
Tom Stellard0344cdf2013-08-01 15:23:42 +00001372 EVT NewVT = MVT::v4i32;
1373 unsigned NumElements = 4;
1374 if (VT.isVector()) {
1375 NewVT = VT;
1376 NumElements = VT.getVectorNumElements();
1377 }
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001378 Result = DAG.getBuildVector(NewVT, DL, makeArrayRef(Slots, NumElements));
Tom Stellard365366f2013-01-23 02:09:06 +00001379 } else {
Alp Tokerf907b892013-12-05 05:44:44 +00001380 // non-constant ptr can't be folded, keeps it as a v4f32 load
Tom Stellard365366f2013-01-23 02:09:06 +00001381 Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001382 DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr,
1383 DAG.getConstant(4, DL, MVT::i32)),
1384 DAG.getConstant(LoadNode->getAddressSpace() -
1385 AMDGPUAS::CONSTANT_BUFFER_0, DL, MVT::i32)
Tom Stellard365366f2013-01-23 02:09:06 +00001386 );
1387 }
1388
1389 if (!VT.isVector()) {
1390 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001391 DAG.getConstant(0, DL, MVT::i32));
Tom Stellard365366f2013-01-23 02:09:06 +00001392 }
1393
1394 SDValue MergedValues[2] = {
Matt Arsenault7939acd2014-04-07 16:44:24 +00001395 Result,
1396 Chain
Tom Stellard365366f2013-01-23 02:09:06 +00001397 };
Craig Topper64941d92014-04-27 19:20:57 +00001398 return DAG.getMergeValues(MergedValues, DL);
Tom Stellard365366f2013-01-23 02:09:06 +00001399 }
1400
Matt Arsenault6dfda962016-02-10 18:21:39 +00001401 SDValue LoweredLoad;
1402
Matt Arsenault909d0c02013-10-30 23:43:29 +00001403 // For most operations returning SDValue() will result in the node being
1404 // expanded by the DAG Legalizer. This is not the case for ISD::LOAD, so we
1405 // need to manually expand loads that may be legal in some address spaces and
1406 // illegal in others. SEXT loads from CONSTANT_BUFFER_0 are supported for
1407 // compute shaders, since the data is sign extended when it is uploaded to the
1408 // buffer. However SEXT loads from other address spaces are not supported, so
1409 // we need to expand them here.
Tom Stellard84021442013-07-23 01:48:24 +00001410 if (LoadNode->getExtensionType() == ISD::SEXTLOAD) {
1411 EVT MemVT = LoadNode->getMemoryVT();
1412 assert(!MemVT.isVector() && (MemVT == MVT::i16 || MemVT == MVT::i8));
Justin Lebar9c375812016-07-15 18:27:10 +00001413 SDValue NewLoad = DAG.getExtLoad(
1414 ISD::EXTLOAD, DL, VT, Chain, Ptr, LoadNode->getPointerInfo(), MemVT,
1415 LoadNode->getAlignment(), LoadNode->getMemOperand()->getFlags());
Jan Veselyb670d372015-05-26 18:07:22 +00001416 SDValue Res = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, NewLoad,
1417 DAG.getValueType(MemVT));
Tom Stellard84021442013-07-23 01:48:24 +00001418
Jan Veselyb670d372015-05-26 18:07:22 +00001419 SDValue MergedValues[2] = { Res, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00001420 return DAG.getMergeValues(MergedValues, DL);
Tom Stellard84021442013-07-23 01:48:24 +00001421 }
1422
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001423 if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1424 return SDValue();
1425 }
1426
1427 // Lowering for indirect addressing
1428 const MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001429 const R600FrameLowering *TFL = getSubtarget()->getFrameLowering();
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001430 unsigned StackWidth = TFL->getStackWidth(MF);
1431
1432 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1433
1434 if (VT.isVector()) {
1435 unsigned NumElemVT = VT.getVectorNumElements();
1436 EVT ElemVT = VT.getVectorElementType();
1437 SDValue Loads[4];
1438
Jan Vesely687ca8d2016-05-16 23:56:32 +00001439 assert(NumElemVT <= 4);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001440 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1441 "vector width in load");
1442
1443 for (unsigned i = 0; i < NumElemVT; ++i) {
1444 unsigned Channel, PtrIncr;
1445 getStackAddress(StackWidth, i, Channel, PtrIncr);
1446 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001447 DAG.getConstant(PtrIncr, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001448 Loads[i] = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, ElemVT,
1449 Chain, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001450 DAG.getTargetConstant(Channel, DL, MVT::i32),
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001451 Op.getOperand(2));
1452 }
Jan Vesely687ca8d2016-05-16 23:56:32 +00001453 EVT TargetVT = EVT::getVectorVT(*DAG.getContext(), ElemVT, NumElemVT);
1454 LoweredLoad = DAG.getBuildVector(TargetVT, DL, makeArrayRef(Loads, NumElemVT));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001455 } else {
1456 LoweredLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, VT,
1457 Chain, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001458 DAG.getTargetConstant(0, DL, MVT::i32), // Channel
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001459 Op.getOperand(2));
1460 }
1461
Matt Arsenault7939acd2014-04-07 16:44:24 +00001462 SDValue Ops[2] = {
1463 LoweredLoad,
1464 Chain
1465 };
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001466
Craig Topper64941d92014-04-27 19:20:57 +00001467 return DAG.getMergeValues(Ops, DL);
Tom Stellard365366f2013-01-23 02:09:06 +00001468}
Tom Stellard75aadc22012-12-11 21:25:42 +00001469
Matt Arsenault1d555c42014-06-23 18:00:55 +00001470SDValue R600TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
1471 SDValue Chain = Op.getOperand(0);
1472 SDValue Cond = Op.getOperand(1);
1473 SDValue Jump = Op.getOperand(2);
1474
1475 return DAG.getNode(AMDGPUISD::BRANCH_COND, SDLoc(Op), Op.getValueType(),
1476 Chain, Jump, Cond);
1477}
1478
Matt Arsenault81d06012016-03-07 21:10:13 +00001479SDValue R600TargetLowering::lowerFrameIndex(SDValue Op,
1480 SelectionDAG &DAG) const {
1481 MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001482 const R600FrameLowering *TFL = getSubtarget()->getFrameLowering();
Matt Arsenault81d06012016-03-07 21:10:13 +00001483
1484 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op);
1485
1486 unsigned FrameIndex = FIN->getIndex();
1487 unsigned IgnoredFrameReg;
1488 unsigned Offset =
1489 TFL->getFrameIndexReference(MF, FrameIndex, IgnoredFrameReg);
1490 return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF), SDLoc(Op),
1491 Op.getValueType());
1492}
1493
Tom Stellard75aadc22012-12-11 21:25:42 +00001494/// XXX Only kernel functions are supported, so we can assume for now that
1495/// every function is a kernel function, but in the future we should use
1496/// separate calling conventions for kernel and non-kernel functions.
1497SDValue R600TargetLowering::LowerFormalArguments(
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001498 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1499 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
1500 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Tom Stellardacfeebf2013-07-23 01:48:05 +00001501 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001502 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1503 *DAG.getContext());
Vincent Lejeunef143af32013-11-11 22:10:24 +00001504 MachineFunction &MF = DAG.getMachineFunction();
Jan Veselye5121f32014-10-14 20:05:26 +00001505 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
Tom Stellardacfeebf2013-07-23 01:48:05 +00001506
Tom Stellardaf775432013-10-23 00:44:32 +00001507 SmallVector<ISD::InputArg, 8> LocalIns;
1508
Matt Arsenault209a7b92014-04-18 07:40:20 +00001509 getOriginalFunctionArgs(DAG, MF.getFunction(), Ins, LocalIns);
Tom Stellardaf775432013-10-23 00:44:32 +00001510
1511 AnalyzeFormalArguments(CCInfo, LocalIns);
Tom Stellardacfeebf2013-07-23 01:48:05 +00001512
Tom Stellard1e803092013-07-23 01:48:18 +00001513 for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
Tom Stellardacfeebf2013-07-23 01:48:05 +00001514 CCValAssign &VA = ArgLocs[i];
Matt Arsenault74ef2772014-08-13 18:14:11 +00001515 const ISD::InputArg &In = Ins[i];
1516 EVT VT = In.VT;
1517 EVT MemVT = VA.getLocVT();
1518 if (!VT.isVector() && MemVT.isVector()) {
1519 // Get load source type if scalarized.
1520 MemVT = MemVT.getVectorElementType();
1521 }
Tom Stellard78e01292013-07-23 01:47:58 +00001522
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001523 if (AMDGPU::isShader(CallConv)) {
Vincent Lejeunef143af32013-11-11 22:10:24 +00001524 unsigned Reg = MF.addLiveIn(VA.getLocReg(), &AMDGPU::R600_Reg128RegClass);
1525 SDValue Register = DAG.getCopyFromReg(Chain, DL, Reg, VT);
1526 InVals.push_back(Register);
1527 continue;
1528 }
1529
Tom Stellard75aadc22012-12-11 21:25:42 +00001530 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
Matt Arsenault74ef2772014-08-13 18:14:11 +00001531 AMDGPUAS::CONSTANT_BUFFER_0);
Tom Stellardacfeebf2013-07-23 01:48:05 +00001532
Matt Arsenaultfae02982014-03-17 18:58:11 +00001533 // i64 isn't a legal type, so the register type used ends up as i32, which
1534 // isn't expected here. It attempts to create this sextload, but it ends up
1535 // being invalid. Somehow this seems to work with i64 arguments, but breaks
1536 // for <1 x i64>.
1537
Tom Stellardacfeebf2013-07-23 01:48:05 +00001538 // The first 36 bytes of the input buffer contains information about
1539 // thread group and global sizes.
Matt Arsenault74ef2772014-08-13 18:14:11 +00001540 ISD::LoadExtType Ext = ISD::NON_EXTLOAD;
1541 if (MemVT.getScalarSizeInBits() != VT.getScalarSizeInBits()) {
1542 // FIXME: This should really check the extload type, but the handling of
1543 // extload vector parameters seems to be broken.
Matt Arsenaulte1f030c2014-04-11 20:59:54 +00001544
Matt Arsenault74ef2772014-08-13 18:14:11 +00001545 // Ext = In.Flags.isSExt() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
1546 Ext = ISD::SEXTLOAD;
1547 }
1548
1549 // Compute the offset from the value.
1550 // XXX - I think PartOffset should give you this, but it seems to give the
1551 // size of the register which isn't useful.
1552
Andrew Trick05938a52015-02-16 18:10:47 +00001553 unsigned ValBase = ArgLocs[In.getOrigArgIndex()].getLocMemOffset();
Matt Arsenault74ef2772014-08-13 18:14:11 +00001554 unsigned PartOffset = VA.getLocMemOffset();
Matt Arsenault52ef4012016-07-26 16:45:58 +00001555 unsigned Offset = Subtarget->getExplicitKernelArgOffset() + VA.getLocMemOffset();
Matt Arsenault74ef2772014-08-13 18:14:11 +00001556
1557 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy), PartOffset - ValBase);
Justin Lebar9c375812016-07-15 18:27:10 +00001558 SDValue Arg = DAG.getLoad(
1559 ISD::UNINDEXED, Ext, VT, DL, Chain,
1560 DAG.getConstant(Offset, DL, MVT::i32), DAG.getUNDEF(MVT::i32), PtrInfo,
1561 MemVT, /* Alignment = */ 4,
1562 MachineMemOperand::MONonTemporal | MachineMemOperand::MOInvariant);
Matt Arsenault209a7b92014-04-18 07:40:20 +00001563
1564 // 4 is the preferred alignment for the CONSTANT memory space.
Tom Stellard75aadc22012-12-11 21:25:42 +00001565 InVals.push_back(Arg);
Matt Arsenault52ef4012016-07-26 16:45:58 +00001566 MFI->setABIArgOffset(Offset + MemVT.getStoreSize());
Tom Stellard75aadc22012-12-11 21:25:42 +00001567 }
1568 return Chain;
1569}
1570
Mehdi Amini44ede332015-07-09 02:09:04 +00001571EVT R600TargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1572 EVT VT) const {
Matt Arsenault209a7b92014-04-18 07:40:20 +00001573 if (!VT.isVector())
1574 return MVT::i32;
Tom Stellard75aadc22012-12-11 21:25:42 +00001575 return VT.changeVectorElementTypeToInteger();
1576}
1577
Matt Arsenaultfa67bdb2016-02-22 21:04:16 +00001578bool R600TargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
1579 unsigned AddrSpace,
1580 unsigned Align,
1581 bool *IsFast) const {
1582 if (IsFast)
1583 *IsFast = false;
1584
1585 if (!VT.isSimple() || VT == MVT::Other)
1586 return false;
1587
1588 if (VT.bitsLT(MVT::i32))
1589 return false;
1590
1591 // TODO: This is a rough estimate.
1592 if (IsFast)
1593 *IsFast = true;
1594
1595 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
1596}
1597
Matt Arsenault209a7b92014-04-18 07:40:20 +00001598static SDValue CompactSwizzlableVector(
1599 SelectionDAG &DAG, SDValue VectorEntry,
1600 DenseMap<unsigned, unsigned> &RemapSwizzle) {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001601 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1602 assert(RemapSwizzle.empty());
1603 SDValue NewBldVec[4] = {
Matt Arsenault209a7b92014-04-18 07:40:20 +00001604 VectorEntry.getOperand(0),
1605 VectorEntry.getOperand(1),
1606 VectorEntry.getOperand(2),
1607 VectorEntry.getOperand(3)
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001608 };
1609
1610 for (unsigned i = 0; i < 4; i++) {
Sanjay Patel57195842016-03-14 17:28:46 +00001611 if (NewBldVec[i].isUndef())
Vincent Lejeunefa58a5f2013-10-13 17:56:10 +00001612 // We mask write here to teach later passes that the ith element of this
1613 // vector is undef. Thus we can use it to reduce 128 bits reg usage,
1614 // break false dependencies and additionnaly make assembly easier to read.
1615 RemapSwizzle[i] = 7; // SEL_MASK_WRITE
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001616 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(NewBldVec[i])) {
1617 if (C->isZero()) {
1618 RemapSwizzle[i] = 4; // SEL_0
1619 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1620 } else if (C->isExactlyValue(1.0)) {
1621 RemapSwizzle[i] = 5; // SEL_1
1622 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1623 }
1624 }
1625
Sanjay Patel57195842016-03-14 17:28:46 +00001626 if (NewBldVec[i].isUndef())
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001627 continue;
1628 for (unsigned j = 0; j < i; j++) {
1629 if (NewBldVec[i] == NewBldVec[j]) {
1630 NewBldVec[i] = DAG.getUNDEF(NewBldVec[i].getValueType());
1631 RemapSwizzle[i] = j;
1632 break;
1633 }
1634 }
1635 }
1636
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001637 return DAG.getBuildVector(VectorEntry.getValueType(), SDLoc(VectorEntry),
1638 NewBldVec);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001639}
1640
Benjamin Kramer193960c2013-06-11 13:32:25 +00001641static SDValue ReorganizeVector(SelectionDAG &DAG, SDValue VectorEntry,
1642 DenseMap<unsigned, unsigned> &RemapSwizzle) {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001643 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1644 assert(RemapSwizzle.empty());
1645 SDValue NewBldVec[4] = {
1646 VectorEntry.getOperand(0),
1647 VectorEntry.getOperand(1),
1648 VectorEntry.getOperand(2),
1649 VectorEntry.getOperand(3)
1650 };
1651 bool isUnmovable[4] = { false, false, false, false };
Vincent Lejeunecc0ea742013-12-10 14:43:31 +00001652 for (unsigned i = 0; i < 4; i++) {
Vincent Lejeuneb8aac8d2013-07-09 15:03:25 +00001653 RemapSwizzle[i] = i;
Vincent Lejeunecc0ea742013-12-10 14:43:31 +00001654 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1655 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1656 ->getZExtValue();
1657 if (i == Idx)
1658 isUnmovable[Idx] = true;
1659 }
1660 }
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001661
1662 for (unsigned i = 0; i < 4; i++) {
1663 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1664 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1665 ->getZExtValue();
Vincent Lejeune301beb82013-10-13 17:56:04 +00001666 if (isUnmovable[Idx])
1667 continue;
1668 // Swap i and Idx
1669 std::swap(NewBldVec[Idx], NewBldVec[i]);
1670 std::swap(RemapSwizzle[i], RemapSwizzle[Idx]);
1671 break;
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001672 }
1673 }
1674
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001675 return DAG.getBuildVector(VectorEntry.getValueType(), SDLoc(VectorEntry),
1676 NewBldVec);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001677}
1678
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001679SDValue R600TargetLowering::OptimizeSwizzle(SDValue BuildVector, SDValue Swz[4],
1680 SelectionDAG &DAG,
1681 const SDLoc &DL) const {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001682 assert(BuildVector.getOpcode() == ISD::BUILD_VECTOR);
1683 // Old -> New swizzle values
1684 DenseMap<unsigned, unsigned> SwizzleRemap;
1685
1686 BuildVector = CompactSwizzlableVector(DAG, BuildVector, SwizzleRemap);
1687 for (unsigned i = 0; i < 4; i++) {
Benjamin Kramer619c4e52015-04-10 11:24:51 +00001688 unsigned Idx = cast<ConstantSDNode>(Swz[i])->getZExtValue();
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001689 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001690 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], DL, MVT::i32);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001691 }
1692
1693 SwizzleRemap.clear();
1694 BuildVector = ReorganizeVector(DAG, BuildVector, SwizzleRemap);
1695 for (unsigned i = 0; i < 4; i++) {
Benjamin Kramer619c4e52015-04-10 11:24:51 +00001696 unsigned Idx = cast<ConstantSDNode>(Swz[i])->getZExtValue();
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001697 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001698 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], DL, MVT::i32);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001699 }
1700
1701 return BuildVector;
1702}
1703
1704
Tom Stellard75aadc22012-12-11 21:25:42 +00001705//===----------------------------------------------------------------------===//
1706// Custom DAG Optimizations
1707//===----------------------------------------------------------------------===//
1708
1709SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
1710 DAGCombinerInfo &DCI) const {
1711 SelectionDAG &DAG = DCI.DAG;
1712
1713 switch (N->getOpcode()) {
Tom Stellard50122a52014-04-07 19:45:41 +00001714 default: return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001715 // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
1716 case ISD::FP_ROUND: {
1717 SDValue Arg = N->getOperand(0);
1718 if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001719 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), N->getValueType(0),
Tom Stellard75aadc22012-12-11 21:25:42 +00001720 Arg.getOperand(0));
1721 }
1722 break;
1723 }
Tom Stellarde06163a2013-02-07 14:02:35 +00001724
1725 // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
1726 // (i32 select_cc f32, f32, -1, 0 cc)
1727 //
1728 // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1729 // this to one of the SET*_DX10 instructions.
1730 case ISD::FP_TO_SINT: {
1731 SDValue FNeg = N->getOperand(0);
1732 if (FNeg.getOpcode() != ISD::FNEG) {
1733 return SDValue();
1734 }
1735 SDValue SelectCC = FNeg.getOperand(0);
1736 if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1737 SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1738 SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1739 !isHWTrueValue(SelectCC.getOperand(2)) ||
1740 !isHWFalseValue(SelectCC.getOperand(3))) {
1741 return SDValue();
1742 }
1743
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001744 SDLoc dl(N);
1745 return DAG.getNode(ISD::SELECT_CC, dl, N->getValueType(0),
Tom Stellarde06163a2013-02-07 14:02:35 +00001746 SelectCC.getOperand(0), // LHS
1747 SelectCC.getOperand(1), // RHS
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001748 DAG.getConstant(-1, dl, MVT::i32), // True
1749 DAG.getConstant(0, dl, MVT::i32), // False
Tom Stellarde06163a2013-02-07 14:02:35 +00001750 SelectCC.getOperand(4)); // CC
1751
1752 break;
1753 }
Quentin Colombete2e05482013-07-30 00:27:16 +00001754
NAKAMURA Takumi8a046432013-10-28 04:07:38 +00001755 // insert_vector_elt (build_vector elt0, ... , eltN), NewEltIdx, idx
1756 // => build_vector elt0, ... , NewEltIdx, ... , eltN
Quentin Colombete2e05482013-07-30 00:27:16 +00001757 case ISD::INSERT_VECTOR_ELT: {
1758 SDValue InVec = N->getOperand(0);
1759 SDValue InVal = N->getOperand(1);
1760 SDValue EltNo = N->getOperand(2);
1761 SDLoc dl(N);
1762
1763 // If the inserted element is an UNDEF, just use the input vector.
Sanjay Patel57195842016-03-14 17:28:46 +00001764 if (InVal.isUndef())
Quentin Colombete2e05482013-07-30 00:27:16 +00001765 return InVec;
1766
1767 EVT VT = InVec.getValueType();
1768
1769 // If we can't generate a legal BUILD_VECTOR, exit
1770 if (!isOperationLegal(ISD::BUILD_VECTOR, VT))
1771 return SDValue();
1772
1773 // Check that we know which element is being inserted
1774 if (!isa<ConstantSDNode>(EltNo))
1775 return SDValue();
1776 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
1777
1778 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
1779 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
1780 // vector elements.
1781 SmallVector<SDValue, 8> Ops;
1782 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
1783 Ops.append(InVec.getNode()->op_begin(),
1784 InVec.getNode()->op_end());
Sanjay Patel57195842016-03-14 17:28:46 +00001785 } else if (InVec.isUndef()) {
Quentin Colombete2e05482013-07-30 00:27:16 +00001786 unsigned NElts = VT.getVectorNumElements();
1787 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
1788 } else {
1789 return SDValue();
1790 }
1791
1792 // Insert the element
1793 if (Elt < Ops.size()) {
1794 // All the operands of BUILD_VECTOR must have the same type;
1795 // we enforce that here.
1796 EVT OpVT = Ops[0].getValueType();
1797 if (InVal.getValueType() != OpVT)
1798 InVal = OpVT.bitsGT(InVal.getValueType()) ?
1799 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
1800 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
1801 Ops[Elt] = InVal;
1802 }
1803
1804 // Return the new vector
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001805 return DAG.getBuildVector(VT, dl, Ops);
Quentin Colombete2e05482013-07-30 00:27:16 +00001806 }
1807
Tom Stellard365366f2013-01-23 02:09:06 +00001808 // Extract_vec (Build_vector) generated by custom lowering
1809 // also needs to be customly combined
1810 case ISD::EXTRACT_VECTOR_ELT: {
1811 SDValue Arg = N->getOperand(0);
1812 if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1813 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1814 unsigned Element = Const->getZExtValue();
1815 return Arg->getOperand(Element);
1816 }
1817 }
Tom Stellarddd04c832013-01-31 22:11:53 +00001818 if (Arg.getOpcode() == ISD::BITCAST &&
1819 Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
1820 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1821 unsigned Element = Const->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001822 return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getVTList(),
Tom Stellarddd04c832013-01-31 22:11:53 +00001823 Arg->getOperand(0).getOperand(Element));
1824 }
1825 }
Mehdi Aminie029eae2015-07-16 06:23:12 +00001826 break;
Tom Stellard365366f2013-01-23 02:09:06 +00001827 }
Tom Stellarde06163a2013-02-07 14:02:35 +00001828
1829 case ISD::SELECT_CC: {
Tom Stellardafa8b532014-05-09 16:42:16 +00001830 // Try common optimizations
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00001831 if (SDValue Ret = AMDGPUTargetLowering::PerformDAGCombine(N, DCI))
Tom Stellardafa8b532014-05-09 16:42:16 +00001832 return Ret;
1833
Tom Stellarde06163a2013-02-07 14:02:35 +00001834 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1835 // selectcc x, y, a, b, inv(cc)
Tom Stellard5e524892013-03-08 15:37:11 +00001836 //
1837 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, setne ->
1838 // selectcc x, y, a, b, cc
Tom Stellarde06163a2013-02-07 14:02:35 +00001839 SDValue LHS = N->getOperand(0);
1840 if (LHS.getOpcode() != ISD::SELECT_CC) {
1841 return SDValue();
1842 }
1843
1844 SDValue RHS = N->getOperand(1);
1845 SDValue True = N->getOperand(2);
1846 SDValue False = N->getOperand(3);
Tom Stellard5e524892013-03-08 15:37:11 +00001847 ISD::CondCode NCC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Tom Stellarde06163a2013-02-07 14:02:35 +00001848
1849 if (LHS.getOperand(2).getNode() != True.getNode() ||
1850 LHS.getOperand(3).getNode() != False.getNode() ||
Tom Stellard5e524892013-03-08 15:37:11 +00001851 RHS.getNode() != False.getNode()) {
Tom Stellarde06163a2013-02-07 14:02:35 +00001852 return SDValue();
1853 }
1854
Tom Stellard5e524892013-03-08 15:37:11 +00001855 switch (NCC) {
1856 default: return SDValue();
1857 case ISD::SETNE: return LHS;
1858 case ISD::SETEQ: {
1859 ISD::CondCode LHSCC = cast<CondCodeSDNode>(LHS.getOperand(4))->get();
1860 LHSCC = ISD::getSetCCInverse(LHSCC,
1861 LHS.getOperand(0).getValueType().isInteger());
Tom Stellardcd428182013-09-28 02:50:38 +00001862 if (DCI.isBeforeLegalizeOps() ||
1863 isCondCodeLegal(LHSCC, LHS.getOperand(0).getSimpleValueType()))
1864 return DAG.getSelectCC(SDLoc(N),
1865 LHS.getOperand(0),
1866 LHS.getOperand(1),
1867 LHS.getOperand(2),
1868 LHS.getOperand(3),
1869 LHSCC);
1870 break;
Vincent Lejeuned80bc152013-02-14 16:55:06 +00001871 }
Tom Stellard5e524892013-03-08 15:37:11 +00001872 }
Tom Stellardcd428182013-09-28 02:50:38 +00001873 return SDValue();
Tom Stellard5e524892013-03-08 15:37:11 +00001874 }
Tom Stellardfbab8272013-08-16 01:12:11 +00001875
Vincent Lejeuned80bc152013-02-14 16:55:06 +00001876 case AMDGPUISD::EXPORT: {
1877 SDValue Arg = N->getOperand(1);
1878 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1879 break;
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001880
Vincent Lejeuned80bc152013-02-14 16:55:06 +00001881 SDValue NewArgs[8] = {
1882 N->getOperand(0), // Chain
1883 SDValue(),
1884 N->getOperand(2), // ArrayBase
1885 N->getOperand(3), // Type
1886 N->getOperand(4), // SWZ_X
1887 N->getOperand(5), // SWZ_Y
1888 N->getOperand(6), // SWZ_Z
1889 N->getOperand(7) // SWZ_W
1890 };
Andrew Trickef9de2a2013-05-25 02:42:55 +00001891 SDLoc DL(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001892 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[4], DAG, DL);
Craig Topper48d114b2014-04-26 18:35:24 +00001893 return DAG.getNode(AMDGPUISD::EXPORT, DL, N->getVTList(), NewArgs);
Tom Stellarde06163a2013-02-07 14:02:35 +00001894 }
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001895 case AMDGPUISD::TEXTURE_FETCH: {
1896 SDValue Arg = N->getOperand(1);
1897 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1898 break;
1899
1900 SDValue NewArgs[19] = {
1901 N->getOperand(0),
1902 N->getOperand(1),
1903 N->getOperand(2),
1904 N->getOperand(3),
1905 N->getOperand(4),
1906 N->getOperand(5),
1907 N->getOperand(6),
1908 N->getOperand(7),
1909 N->getOperand(8),
1910 N->getOperand(9),
1911 N->getOperand(10),
1912 N->getOperand(11),
1913 N->getOperand(12),
1914 N->getOperand(13),
1915 N->getOperand(14),
1916 N->getOperand(15),
1917 N->getOperand(16),
1918 N->getOperand(17),
1919 N->getOperand(18),
1920 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001921 SDLoc DL(N);
1922 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[2], DAG, DL);
1923 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, N->getVTList(), NewArgs);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001924 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001925 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00001926
1927 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001928}
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001929
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001930bool R600TargetLowering::FoldOperand(SDNode *ParentNode, unsigned SrcIdx,
1931 SDValue &Src, SDValue &Neg, SDValue &Abs,
1932 SDValue &Sel, SDValue &Imm,
1933 SelectionDAG &DAG) const {
1934 const R600InstrInfo *TII = getSubtarget()->getInstrInfo();
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001935 if (!Src.isMachineOpcode())
1936 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001937
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001938 switch (Src.getMachineOpcode()) {
1939 case AMDGPU::FNEG_R600:
1940 if (!Neg.getNode())
1941 return false;
1942 Src = Src.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001943 Neg = DAG.getTargetConstant(1, SDLoc(ParentNode), MVT::i32);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001944 return true;
1945 case AMDGPU::FABS_R600:
1946 if (!Abs.getNode())
1947 return false;
1948 Src = Src.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001949 Abs = DAG.getTargetConstant(1, SDLoc(ParentNode), MVT::i32);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001950 return true;
1951 case AMDGPU::CONST_COPY: {
1952 unsigned Opcode = ParentNode->getMachineOpcode();
1953 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
1954
1955 if (!Sel.getNode())
1956 return false;
1957
1958 SDValue CstOffset = Src.getOperand(0);
1959 if (ParentNode->getValueType(0).isVector())
1960 return false;
1961
1962 // Gather constants values
1963 int SrcIndices[] = {
1964 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
1965 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
1966 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2),
1967 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
1968 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
1969 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
1970 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
1971 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
1972 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
1973 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
1974 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
1975 };
1976 std::vector<unsigned> Consts;
Matt Arsenault4d64f962014-05-12 19:23:21 +00001977 for (int OtherSrcIdx : SrcIndices) {
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001978 int OtherSelIdx = TII->getSelIdx(Opcode, OtherSrcIdx);
1979 if (OtherSrcIdx < 0 || OtherSelIdx < 0)
1980 continue;
1981 if (HasDst) {
1982 OtherSrcIdx--;
1983 OtherSelIdx--;
1984 }
1985 if (RegisterSDNode *Reg =
1986 dyn_cast<RegisterSDNode>(ParentNode->getOperand(OtherSrcIdx))) {
1987 if (Reg->getReg() == AMDGPU::ALU_CONST) {
Matt Arsenaultb3ee3882014-05-12 19:26:38 +00001988 ConstantSDNode *Cst
1989 = cast<ConstantSDNode>(ParentNode->getOperand(OtherSelIdx));
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001990 Consts.push_back(Cst->getZExtValue());
1991 }
1992 }
1993 }
1994
Matt Arsenault37c12d72014-05-12 20:42:57 +00001995 ConstantSDNode *Cst = cast<ConstantSDNode>(CstOffset);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001996 Consts.push_back(Cst->getZExtValue());
1997 if (!TII->fitsConstReadLimitations(Consts)) {
1998 return false;
1999 }
2000
2001 Sel = CstOffset;
2002 Src = DAG.getRegister(AMDGPU::ALU_CONST, MVT::f32);
2003 return true;
2004 }
Jan Vesely16800392016-05-13 20:39:31 +00002005 case AMDGPU::MOV_IMM_GLOBAL_ADDR:
2006 // Check if the Imm slot is used. Taken from below.
2007 if (cast<ConstantSDNode>(Imm)->getZExtValue())
2008 return false;
2009 Imm = Src.getOperand(0);
2010 Src = DAG.getRegister(AMDGPU::ALU_LITERAL_X, MVT::i32);
2011 return true;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002012 case AMDGPU::MOV_IMM_I32:
2013 case AMDGPU::MOV_IMM_F32: {
2014 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
2015 uint64_t ImmValue = 0;
2016
2017
2018 if (Src.getMachineOpcode() == AMDGPU::MOV_IMM_F32) {
2019 ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Src.getOperand(0));
2020 float FloatValue = FPC->getValueAPF().convertToFloat();
2021 if (FloatValue == 0.0) {
2022 ImmReg = AMDGPU::ZERO;
2023 } else if (FloatValue == 0.5) {
2024 ImmReg = AMDGPU::HALF;
2025 } else if (FloatValue == 1.0) {
2026 ImmReg = AMDGPU::ONE;
2027 } else {
2028 ImmValue = FPC->getValueAPF().bitcastToAPInt().getZExtValue();
2029 }
2030 } else {
2031 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(0));
2032 uint64_t Value = C->getZExtValue();
2033 if (Value == 0) {
2034 ImmReg = AMDGPU::ZERO;
2035 } else if (Value == 1) {
2036 ImmReg = AMDGPU::ONE_INT;
2037 } else {
2038 ImmValue = Value;
2039 }
2040 }
2041
2042 // Check that we aren't already using an immediate.
2043 // XXX: It's possible for an instruction to have more than one
2044 // immediate operand, but this is not supported yet.
2045 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
2046 if (!Imm.getNode())
2047 return false;
2048 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Imm);
2049 assert(C);
2050 if (C->getZExtValue())
2051 return false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002052 Imm = DAG.getTargetConstant(ImmValue, SDLoc(ParentNode), MVT::i32);
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002053 }
2054 Src = DAG.getRegister(ImmReg, MVT::i32);
2055 return true;
2056 }
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002057 default:
2058 return false;
2059 }
2060}
2061
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002062/// \brief Fold the instructions after selecting them
2063SDNode *R600TargetLowering::PostISelFolding(MachineSDNode *Node,
2064 SelectionDAG &DAG) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002065 const R600InstrInfo *TII = getSubtarget()->getInstrInfo();
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002066 if (!Node->isMachineOpcode())
2067 return Node;
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002068
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002069 unsigned Opcode = Node->getMachineOpcode();
2070 SDValue FakeOp;
2071
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002072 std::vector<SDValue> Ops(Node->op_begin(), Node->op_end());
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002073
2074 if (Opcode == AMDGPU::DOT_4) {
2075 int OperandIdx[] = {
2076 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
2077 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
2078 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
2079 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
2080 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
2081 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
2082 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
2083 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
NAKAMURA Takumi4bb85f92013-10-28 04:07:23 +00002084 };
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002085 int NegIdx[] = {
2086 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_X),
2087 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Y),
2088 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Z),
2089 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_W),
2090 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_X),
2091 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Y),
2092 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Z),
2093 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_W)
2094 };
2095 int AbsIdx[] = {
2096 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_X),
2097 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Y),
2098 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Z),
2099 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_W),
2100 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_X),
2101 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Y),
2102 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Z),
2103 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_W)
2104 };
2105 for (unsigned i = 0; i < 8; i++) {
2106 if (OperandIdx[i] < 0)
2107 return Node;
2108 SDValue &Src = Ops[OperandIdx[i] - 1];
2109 SDValue &Neg = Ops[NegIdx[i] - 1];
2110 SDValue &Abs = Ops[AbsIdx[i] - 1];
2111 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2112 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
2113 if (HasDst)
2114 SelIdx--;
2115 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002116 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, FakeOp, DAG))
2117 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2118 }
2119 } else if (Opcode == AMDGPU::REG_SEQUENCE) {
2120 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) {
2121 SDValue &Src = Ops[i];
2122 if (FoldOperand(Node, i, Src, FakeOp, FakeOp, FakeOp, FakeOp, DAG))
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002123 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2124 }
Vincent Lejeune0167a312013-09-12 23:45:00 +00002125 } else if (Opcode == AMDGPU::CLAMP_R600) {
2126 SDValue Src = Node->getOperand(0);
2127 if (!Src.isMachineOpcode() ||
2128 !TII->hasInstrModifiers(Src.getMachineOpcode()))
2129 return Node;
2130 int ClampIdx = TII->getOperandIdx(Src.getMachineOpcode(),
2131 AMDGPU::OpName::clamp);
2132 if (ClampIdx < 0)
2133 return Node;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002134 SDLoc DL(Node);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002135 std::vector<SDValue> Ops(Src->op_begin(), Src->op_end());
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002136 Ops[ClampIdx - 1] = DAG.getTargetConstant(1, DL, MVT::i32);
2137 return DAG.getMachineNode(Src.getMachineOpcode(), DL,
2138 Node->getVTList(), Ops);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002139 } else {
2140 if (!TII->hasInstrModifiers(Opcode))
2141 return Node;
2142 int OperandIdx[] = {
2143 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
2144 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
2145 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2)
2146 };
2147 int NegIdx[] = {
2148 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg),
2149 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg),
2150 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_neg)
2151 };
2152 int AbsIdx[] = {
2153 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs),
2154 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs),
2155 -1
2156 };
2157 for (unsigned i = 0; i < 3; i++) {
2158 if (OperandIdx[i] < 0)
2159 return Node;
2160 SDValue &Src = Ops[OperandIdx[i] - 1];
2161 SDValue &Neg = Ops[NegIdx[i] - 1];
2162 SDValue FakeAbs;
2163 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
2164 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2165 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002166 int ImmIdx = TII->getOperandIdx(Opcode, AMDGPU::OpName::literal);
2167 if (HasDst) {
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002168 SelIdx--;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002169 ImmIdx--;
2170 }
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002171 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002172 SDValue &Imm = Ops[ImmIdx];
2173 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, Imm, DAG))
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002174 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2175 }
2176 }
2177
2178 return Node;
2179}