blob: 481a6ac99e5458b667927a09d8f8353b189af7cf [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);
Tom Stellard75aadc22012-12-11 21:25:42 +0000201}
202
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000203const R600Subtarget *R600TargetLowering::getSubtarget() const {
204 return static_cast<const R600Subtarget *>(Subtarget);
205}
206
Tom Stellardc0f0fba2015-10-01 17:51:29 +0000207static inline bool isEOP(MachineBasicBlock::iterator I) {
Hans Wennborg0dd9ed12016-08-13 01:12:49 +0000208 if (std::next(I) == I->getParent()->end())
209 return false;
Tom Stellardc0f0fba2015-10-01 17:51:29 +0000210 return std::next(I)->getOpcode() == AMDGPU::RETURN;
211}
212
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000213MachineBasicBlock *
214R600TargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
215 MachineBasicBlock *BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000216 MachineFunction * MF = BB->getParent();
217 MachineRegisterInfo &MRI = MF->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000218 MachineBasicBlock::iterator I = MI;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000219 const R600InstrInfo *TII = getSubtarget()->getInstrInfo();
Tom Stellard75aadc22012-12-11 21:25:42 +0000220
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000221 switch (MI.getOpcode()) {
Tom Stellardc6f4a292013-08-26 15:05:59 +0000222 default:
Tom Stellard8f9fc202013-11-15 00:12:45 +0000223 // Replace LDS_*_RET instruction that don't have any uses with the
224 // equivalent LDS_*_NORET instruction.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000225 if (TII->isLDSRetInstr(MI.getOpcode())) {
226 int DstIdx = TII->getOperandIdx(MI.getOpcode(), AMDGPU::OpName::dst);
Tom Stellard13c68ef2013-09-05 18:38:09 +0000227 assert(DstIdx != -1);
228 MachineInstrBuilder NewMI;
Aaron Watry1885e532014-09-11 15:02:54 +0000229 // FIXME: getLDSNoRetOp method only handles LDS_1A1D LDS ops. Add
230 // LDS_1A2D support and remove this special case.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000231 if (!MRI.use_empty(MI.getOperand(DstIdx).getReg()) ||
232 MI.getOpcode() == AMDGPU::LDS_CMPST_RET)
Tom Stellard8f9fc202013-11-15 00:12:45 +0000233 return BB;
234
235 NewMI = BuildMI(*BB, I, BB->findDebugLoc(I),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000236 TII->get(AMDGPU::getLDSNoRetOp(MI.getOpcode())));
237 for (unsigned i = 1, e = MI.getNumOperands(); i < e; ++i) {
238 NewMI.addOperand(MI.getOperand(i));
Tom Stellardc6f4a292013-08-26 15:05:59 +0000239 }
Tom Stellardc6f4a292013-08-26 15:05:59 +0000240 } else {
241 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
242 }
243 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000244 case AMDGPU::CLAMP_R600: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000245 MachineInstr *NewMI = TII->buildDefaultInstruction(
246 *BB, I, AMDGPU::MOV, MI.getOperand(0).getReg(),
247 MI.getOperand(1).getReg());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000248 TII->addFlag(*NewMI, 0, MO_FLAG_CLAMP);
Tom Stellard75aadc22012-12-11 21:25:42 +0000249 break;
250 }
251
252 case AMDGPU::FABS_R600: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000253 MachineInstr *NewMI = TII->buildDefaultInstruction(
254 *BB, I, AMDGPU::MOV, MI.getOperand(0).getReg(),
255 MI.getOperand(1).getReg());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000256 TII->addFlag(*NewMI, 0, MO_FLAG_ABS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000257 break;
258 }
259
260 case AMDGPU::FNEG_R600: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000261 MachineInstr *NewMI = TII->buildDefaultInstruction(
262 *BB, I, AMDGPU::MOV, MI.getOperand(0).getReg(),
263 MI.getOperand(1).getReg());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000264 TII->addFlag(*NewMI, 0, MO_FLAG_NEG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000265 break;
266 }
267
Tom Stellard75aadc22012-12-11 21:25:42 +0000268 case AMDGPU::MASK_WRITE: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000269 unsigned maskedRegister = MI.getOperand(0).getReg();
Tom Stellard75aadc22012-12-11 21:25:42 +0000270 assert(TargetRegisterInfo::isVirtualRegister(maskedRegister));
271 MachineInstr * defInstr = MRI.getVRegDef(maskedRegister);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000272 TII->addFlag(*defInstr, 0, MO_FLAG_MASK);
Tom Stellard75aadc22012-12-11 21:25:42 +0000273 break;
274 }
275
276 case AMDGPU::MOV_IMM_F32:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000277 TII->buildMovImm(*BB, I, MI.getOperand(0).getReg(), MI.getOperand(1)
278 .getFPImm()
279 ->getValueAPF()
280 .bitcastToAPInt()
281 .getZExtValue());
Tom Stellard75aadc22012-12-11 21:25:42 +0000282 break;
283 case AMDGPU::MOV_IMM_I32:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000284 TII->buildMovImm(*BB, I, MI.getOperand(0).getReg(),
285 MI.getOperand(1).getImm());
Tom Stellard75aadc22012-12-11 21:25:42 +0000286 break;
Jan Veselyf97de002016-05-13 20:39:29 +0000287 case AMDGPU::MOV_IMM_GLOBAL_ADDR: {
288 //TODO: Perhaps combine this instruction with the next if possible
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000289 auto MIB = TII->buildDefaultInstruction(
290 *BB, MI, AMDGPU::MOV, MI.getOperand(0).getReg(), AMDGPU::ALU_LITERAL_X);
Jan Veselyf97de002016-05-13 20:39:29 +0000291 int Idx = TII->getOperandIdx(*MIB, AMDGPU::OpName::literal);
292 //TODO: Ugh this is rather ugly
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000293 MIB->getOperand(Idx) = MI.getOperand(1);
Jan Veselyf97de002016-05-13 20:39:29 +0000294 break;
295 }
Vincent Lejeune0b72f102013-03-05 15:04:55 +0000296 case AMDGPU::CONST_COPY: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000297 MachineInstr *NewMI = TII->buildDefaultInstruction(
298 *BB, MI, AMDGPU::MOV, MI.getOperand(0).getReg(), AMDGPU::ALU_CONST);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000299 TII->setImmOperand(*NewMI, AMDGPU::OpName::src0_sel,
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000300 MI.getOperand(1).getImm());
Vincent Lejeune0b72f102013-03-05 15:04:55 +0000301 break;
302 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000303
304 case AMDGPU::RAT_WRITE_CACHELESS_32_eg:
Tom Stellard0344cdf2013-08-01 15:23:42 +0000305 case AMDGPU::RAT_WRITE_CACHELESS_64_eg:
Tom Stellard75aadc22012-12-11 21:25:42 +0000306 case AMDGPU::RAT_WRITE_CACHELESS_128_eg: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000307 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode()))
308 .addOperand(MI.getOperand(0))
309 .addOperand(MI.getOperand(1))
310 .addImm(isEOP(I)); // Set End of program bit
Tom Stellard75aadc22012-12-11 21:25:42 +0000311 break;
312 }
Tom Stellarde0e582c2015-10-01 17:51:34 +0000313 case AMDGPU::RAT_STORE_TYPED_eg: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000314 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode()))
315 .addOperand(MI.getOperand(0))
316 .addOperand(MI.getOperand(1))
317 .addOperand(MI.getOperand(2))
318 .addImm(isEOP(I)); // Set End of program bit
Tom Stellarde0e582c2015-10-01 17:51:34 +0000319 break;
320 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000321 case AMDGPU::BRANCH:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000322 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
323 .addOperand(MI.getOperand(0));
324 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000325
326 case AMDGPU::BRANCH_COND_f32: {
327 MachineInstr *NewMI =
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000328 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
329 AMDGPU::PREDICATE_BIT)
330 .addOperand(MI.getOperand(1))
Matt Arsenault44f6d692016-08-13 01:43:46 +0000331 .addImm(AMDGPU::PRED_SETNE)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000332 .addImm(0); // Flags
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000333 TII->addFlag(*NewMI, 0, MO_FLAG_PUSH);
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000334 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000335 .addOperand(MI.getOperand(0))
336 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
Tom Stellard75aadc22012-12-11 21:25:42 +0000337 break;
338 }
339
340 case AMDGPU::BRANCH_COND_i32: {
341 MachineInstr *NewMI =
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000342 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
343 AMDGPU::PREDICATE_BIT)
344 .addOperand(MI.getOperand(1))
Matt Arsenault44f6d692016-08-13 01:43:46 +0000345 .addImm(AMDGPU::PRED_SETNE_INT)
Tom Stellard75aadc22012-12-11 21:25:42 +0000346 .addImm(0); // Flags
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000347 TII->addFlag(*NewMI, 0, MO_FLAG_PUSH);
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000348 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000349 .addOperand(MI.getOperand(0))
350 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
Tom Stellard75aadc22012-12-11 21:25:42 +0000351 break;
352 }
353
Tom Stellard75aadc22012-12-11 21:25:42 +0000354 case AMDGPU::EG_ExportSwz:
355 case AMDGPU::R600_ExportSwz: {
Tom Stellard6f1b8652013-01-23 21:39:49 +0000356 // Instruction is left unmodified if its not the last one of its type
357 bool isLastInstructionOfItsType = true;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000358 unsigned InstExportType = MI.getOperand(1).getImm();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000359 for (MachineBasicBlock::iterator NextExportInst = std::next(I),
Tom Stellard6f1b8652013-01-23 21:39:49 +0000360 EndBlock = BB->end(); NextExportInst != EndBlock;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000361 NextExportInst = std::next(NextExportInst)) {
Tom Stellard6f1b8652013-01-23 21:39:49 +0000362 if (NextExportInst->getOpcode() == AMDGPU::EG_ExportSwz ||
363 NextExportInst->getOpcode() == AMDGPU::R600_ExportSwz) {
364 unsigned CurrentInstExportType = NextExportInst->getOperand(1)
365 .getImm();
366 if (CurrentInstExportType == InstExportType) {
367 isLastInstructionOfItsType = false;
368 break;
369 }
370 }
371 }
Tom Stellardc0f0fba2015-10-01 17:51:29 +0000372 bool EOP = isEOP(I);
Tom Stellard6f1b8652013-01-23 21:39:49 +0000373 if (!EOP && !isLastInstructionOfItsType)
Tom Stellard75aadc22012-12-11 21:25:42 +0000374 return BB;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000375 unsigned CfInst = (MI.getOpcode() == AMDGPU::EG_ExportSwz) ? 84 : 40;
376 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode()))
377 .addOperand(MI.getOperand(0))
378 .addOperand(MI.getOperand(1))
379 .addOperand(MI.getOperand(2))
380 .addOperand(MI.getOperand(3))
381 .addOperand(MI.getOperand(4))
382 .addOperand(MI.getOperand(5))
383 .addOperand(MI.getOperand(6))
384 .addImm(CfInst)
385 .addImm(EOP);
Tom Stellard75aadc22012-12-11 21:25:42 +0000386 break;
387 }
Jakob Stoklund Olesenfdc37672013-02-05 17:53:52 +0000388 case AMDGPU::RETURN: {
Jakob Stoklund Olesenfdc37672013-02-05 17:53:52 +0000389 return BB;
390 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000391 }
392
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000393 MI.eraseFromParent();
Tom Stellard75aadc22012-12-11 21:25:42 +0000394 return BB;
395}
396
397//===----------------------------------------------------------------------===//
398// Custom DAG Lowering Operations
399//===----------------------------------------------------------------------===//
400
Tom Stellard75aadc22012-12-11 21:25:42 +0000401SDValue R600TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Tom Stellardc026e8b2013-06-28 15:47:08 +0000402 MachineFunction &MF = DAG.getMachineFunction();
403 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
Tom Stellard75aadc22012-12-11 21:25:42 +0000404 switch (Op.getOpcode()) {
405 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellard880a80a2014-06-17 16:53:14 +0000406 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
407 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Jan Vesely25f36272014-06-18 12:27:13 +0000408 case ISD::SHL_PARTS: return LowerSHLParts(Op, DAG);
Jan Veselyecf51332014-06-18 12:27:17 +0000409 case ISD::SRA_PARTS:
Jan Vesely900ff2e2014-06-18 12:27:15 +0000410 case ISD::SRL_PARTS: return LowerSRXParts(Op, DAG);
Jan Vesely808fff52015-04-30 17:15:56 +0000411 case ISD::UADDO: return LowerUADDSUBO(Op, DAG, ISD::ADD, AMDGPUISD::CARRY);
412 case ISD::USUBO: return LowerUADDSUBO(Op, DAG, ISD::SUB, AMDGPUISD::BORROW);
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000413 case ISD::FCOS:
414 case ISD::FSIN: return LowerTrig(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000415 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000416 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000417 case ISD::LOAD: {
418 SDValue Result = LowerLOAD(Op, DAG);
419 assert((!Result.getNode() ||
420 Result.getNode()->getNumValues() == 2) &&
421 "Load should return a value and a chain");
422 return Result;
423 }
424
Matt Arsenault1d555c42014-06-23 18:00:55 +0000425 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellardc026e8b2013-06-28 15:47:08 +0000426 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
Matt Arsenault81d06012016-03-07 21:10:13 +0000427 case ISD::FrameIndex: return lowerFrameIndex(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000428 case ISD::INTRINSIC_VOID: {
429 SDValue Chain = Op.getOperand(0);
430 unsigned IntrinsicID =
431 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
432 switch (IntrinsicID) {
Matt Arsenault82e5e1e2016-07-15 21:27:08 +0000433 case AMDGPUIntrinsic::r600_store_swizzle: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000434 SDLoc DL(Op);
Vincent Lejeuned80bc152013-02-14 16:55:06 +0000435 const SDValue Args[8] = {
436 Chain,
437 Op.getOperand(2), // Export Value
438 Op.getOperand(3), // ArrayBase
439 Op.getOperand(4), // Type
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000440 DAG.getConstant(0, DL, MVT::i32), // SWZ_X
441 DAG.getConstant(1, DL, MVT::i32), // SWZ_Y
442 DAG.getConstant(2, DL, MVT::i32), // SWZ_Z
443 DAG.getConstant(3, DL, MVT::i32) // SWZ_W
Vincent Lejeuned80bc152013-02-14 16:55:06 +0000444 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000445 return DAG.getNode(AMDGPUISD::EXPORT, DL, Op.getValueType(), Args);
Tom Stellard75aadc22012-12-11 21:25:42 +0000446 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000447
Tom Stellard75aadc22012-12-11 21:25:42 +0000448 // default for switch(IntrinsicID)
449 default: break;
450 }
451 // break out of case ISD::INTRINSIC_VOID in switch(Op.getOpcode())
452 break;
453 }
454 case ISD::INTRINSIC_WO_CHAIN: {
455 unsigned IntrinsicID =
456 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
457 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000458 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000459 switch(IntrinsicID) {
460 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Matt Arsenault59bd3012016-01-22 19:00:09 +0000461 case AMDGPUIntrinsic::r600_tex:
Matt Arsenaultf9245b72016-07-22 17:01:25 +0000462 case AMDGPUIntrinsic::r600_texc: {
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000463 unsigned TextureOp;
464 switch (IntrinsicID) {
Matt Arsenault59bd3012016-01-22 19:00:09 +0000465 case AMDGPUIntrinsic::r600_tex:
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000466 TextureOp = 0;
467 break;
Matt Arsenault59bd3012016-01-22 19:00:09 +0000468 case AMDGPUIntrinsic::r600_texc:
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000469 TextureOp = 1;
470 break;
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000471 default:
Matt Arsenault60a750f2016-07-26 21:03:38 +0000472 llvm_unreachable("unhandled texture operation");
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000473 }
474
475 SDValue TexArgs[19] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000476 DAG.getConstant(TextureOp, DL, MVT::i32),
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000477 Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000478 DAG.getConstant(0, DL, MVT::i32),
479 DAG.getConstant(1, DL, MVT::i32),
480 DAG.getConstant(2, DL, MVT::i32),
481 DAG.getConstant(3, DL, MVT::i32),
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000482 Op.getOperand(2),
483 Op.getOperand(3),
484 Op.getOperand(4),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000485 DAG.getConstant(0, DL, MVT::i32),
486 DAG.getConstant(1, DL, MVT::i32),
487 DAG.getConstant(2, DL, MVT::i32),
488 DAG.getConstant(3, DL, MVT::i32),
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000489 Op.getOperand(5),
490 Op.getOperand(6),
491 Op.getOperand(7),
492 Op.getOperand(8),
493 Op.getOperand(9),
494 Op.getOperand(10)
495 };
Craig Topper48d114b2014-04-26 18:35:24 +0000496 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, MVT::v4f32, TexArgs);
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000497 }
Matt Arsenaultca7f5702016-07-14 05:47:17 +0000498 case AMDGPUIntrinsic::r600_dot4: {
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000499 SDValue Args[8] = {
500 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000501 DAG.getConstant(0, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000502 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000503 DAG.getConstant(0, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000504 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000505 DAG.getConstant(1, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000506 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000507 DAG.getConstant(1, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000508 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000509 DAG.getConstant(2, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000510 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000511 DAG.getConstant(2, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000512 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000513 DAG.getConstant(3, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000514 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000515 DAG.getConstant(3, DL, MVT::i32))
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000516 };
Craig Topper48d114b2014-04-26 18:35:24 +0000517 return DAG.getNode(AMDGPUISD::DOT4, DL, MVT::f32, Args);
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000518 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000519
Jan Vesely2fa28c32016-07-10 21:20:29 +0000520 case Intrinsic::r600_implicitarg_ptr: {
521 MVT PtrVT = getPointerTy(DAG.getDataLayout(), AMDGPUAS::PARAM_I_ADDRESS);
522 uint32_t ByteOffset = getImplicitParameterOffset(MFI, FIRST_IMPLICIT);
523 return DAG.getConstant(ByteOffset, DL, PtrVT);
524 }
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000525 case Intrinsic::r600_read_ngroups_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000526 return LowerImplicitParameter(DAG, VT, DL, 0);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000527 case Intrinsic::r600_read_ngroups_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000528 return LowerImplicitParameter(DAG, VT, DL, 1);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000529 case Intrinsic::r600_read_ngroups_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000530 return LowerImplicitParameter(DAG, VT, DL, 2);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000531 case Intrinsic::r600_read_global_size_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000532 return LowerImplicitParameter(DAG, VT, DL, 3);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000533 case Intrinsic::r600_read_global_size_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000534 return LowerImplicitParameter(DAG, VT, DL, 4);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000535 case Intrinsic::r600_read_global_size_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000536 return LowerImplicitParameter(DAG, VT, DL, 5);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000537 case Intrinsic::r600_read_local_size_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000538 return LowerImplicitParameter(DAG, VT, DL, 6);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000539 case Intrinsic::r600_read_local_size_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000540 return LowerImplicitParameter(DAG, VT, DL, 7);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000541 case Intrinsic::r600_read_local_size_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000542 return LowerImplicitParameter(DAG, VT, DL, 8);
543
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000544 case Intrinsic::r600_read_tgid_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000545 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
546 AMDGPU::T1_X, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000547 case Intrinsic::r600_read_tgid_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000548 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
549 AMDGPU::T1_Y, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000550 case Intrinsic::r600_read_tgid_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000551 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
552 AMDGPU::T1_Z, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000553 case Intrinsic::r600_read_tidig_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000554 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
555 AMDGPU::T0_X, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000556 case Intrinsic::r600_read_tidig_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000557 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
558 AMDGPU::T0_Y, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000559 case Intrinsic::r600_read_tidig_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000560 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
561 AMDGPU::T0_Z, VT);
Matt Arsenaultbef34e22016-01-22 21:30:34 +0000562
Matt Arsenault09b2c4a2016-07-15 21:26:52 +0000563 case Intrinsic::r600_recipsqrt_ieee:
564 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
Matt Arsenaultbef34e22016-01-22 21:30:34 +0000565
Matt Arsenault09b2c4a2016-07-15 21:26:52 +0000566 case Intrinsic::r600_recipsqrt_clamped:
567 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
Tom Stellard75aadc22012-12-11 21:25:42 +0000568 }
Matt Arsenault09b2c4a2016-07-15 21:26:52 +0000569
Tom Stellard75aadc22012-12-11 21:25:42 +0000570 // break out of case ISD::INTRINSIC_WO_CHAIN in switch(Op.getOpcode())
571 break;
572 }
573 } // end switch(Op.getOpcode())
574 return SDValue();
575}
576
577void R600TargetLowering::ReplaceNodeResults(SDNode *N,
578 SmallVectorImpl<SDValue> &Results,
579 SelectionDAG &DAG) const {
580 switch (N->getOpcode()) {
Matt Arsenaultd125d742014-03-27 17:23:24 +0000581 default:
582 AMDGPUTargetLowering::ReplaceNodeResults(N, Results, DAG);
583 return;
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000584 case ISD::FP_TO_UINT:
585 if (N->getValueType(0) == MVT::i1) {
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000586 Results.push_back(lowerFP_TO_UINT(N->getOperand(0), DAG));
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000587 return;
588 }
Justin Bognerb03fd122016-08-17 05:10:15 +0000589 // Since we don't care about out of bounds values we can use FP_TO_SINT for
590 // uints too. The DAGLegalizer code for uint considers some extra cases
591 // which are not necessary here.
592 LLVM_FALLTHROUGH;
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000593 case ISD::FP_TO_SINT: {
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000594 if (N->getValueType(0) == MVT::i1) {
595 Results.push_back(lowerFP_TO_SINT(N->getOperand(0), DAG));
596 return;
597 }
598
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000599 SDValue Result;
600 if (expandFP_TO_SINT(N, Result, DAG))
601 Results.push_back(Result);
Tom Stellard365366f2013-01-23 02:09:06 +0000602 return;
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000603 }
Jan Vesely343cd6f02014-06-22 21:43:01 +0000604 case ISD::SDIVREM: {
605 SDValue Op = SDValue(N, 1);
606 SDValue RES = LowerSDIVREM(Op, DAG);
607 Results.push_back(RES);
608 Results.push_back(RES.getValue(1));
609 break;
610 }
611 case ISD::UDIVREM: {
612 SDValue Op = SDValue(N, 0);
Tom Stellardbf69d762014-11-15 01:07:53 +0000613 LowerUDIVREM64(Op, DAG, Results);
Jan Vesely343cd6f02014-06-22 21:43:01 +0000614 break;
615 }
616 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000617}
618
Tom Stellard880a80a2014-06-17 16:53:14 +0000619SDValue R600TargetLowering::vectorToVerticalVector(SelectionDAG &DAG,
620 SDValue Vector) const {
621
622 SDLoc DL(Vector);
623 EVT VecVT = Vector.getValueType();
624 EVT EltVT = VecVT.getVectorElementType();
625 SmallVector<SDValue, 8> Args;
626
627 for (unsigned i = 0, e = VecVT.getVectorNumElements();
628 i != e; ++i) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000629 Args.push_back(DAG.getNode(
630 ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Vector,
631 DAG.getConstant(i, DL, getVectorIdxTy(DAG.getDataLayout()))));
Tom Stellard880a80a2014-06-17 16:53:14 +0000632 }
633
634 return DAG.getNode(AMDGPUISD::BUILD_VERTICAL_VECTOR, DL, VecVT, Args);
635}
636
637SDValue R600TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
638 SelectionDAG &DAG) const {
639
640 SDLoc DL(Op);
641 SDValue Vector = Op.getOperand(0);
642 SDValue Index = Op.getOperand(1);
643
644 if (isa<ConstantSDNode>(Index) ||
645 Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR)
646 return Op;
647
648 Vector = vectorToVerticalVector(DAG, Vector);
649 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, Op.getValueType(),
650 Vector, Index);
651}
652
653SDValue R600TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
654 SelectionDAG &DAG) const {
655 SDLoc DL(Op);
656 SDValue Vector = Op.getOperand(0);
657 SDValue Value = Op.getOperand(1);
658 SDValue Index = Op.getOperand(2);
659
660 if (isa<ConstantSDNode>(Index) ||
661 Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR)
662 return Op;
663
664 Vector = vectorToVerticalVector(DAG, Vector);
665 SDValue Insert = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, Op.getValueType(),
666 Vector, Value, Index);
667 return vectorToVerticalVector(DAG, Insert);
668}
669
Tom Stellard27233b72016-05-02 18:05:17 +0000670SDValue R600TargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
671 SDValue Op,
672 SelectionDAG &DAG) const {
673
674 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
675 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
676 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
677
678 const DataLayout &DL = DAG.getDataLayout();
679 const GlobalValue *GV = GSD->getGlobal();
Tom Stellard27233b72016-05-02 18:05:17 +0000680 MVT ConstPtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
681
Jan Veselyf97de002016-05-13 20:39:29 +0000682 SDValue GA = DAG.getTargetGlobalAddress(GV, SDLoc(GSD), ConstPtrVT);
683 return DAG.getNode(AMDGPUISD::CONST_DATA_PTR, SDLoc(GSD), ConstPtrVT, GA);
Tom Stellard27233b72016-05-02 18:05:17 +0000684}
685
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000686SDValue R600TargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
687 // On hw >= R700, COS/SIN input must be between -1. and 1.
688 // Thus we lower them to TRIG ( FRACT ( x / 2Pi + 0.5) - 0.5)
689 EVT VT = Op.getValueType();
690 SDValue Arg = Op.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000691 SDLoc DL(Op);
Sanjay Patela2607012015-09-16 16:31:21 +0000692
693 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000694 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
695 DAG.getNode(ISD::FADD, DL, VT,
696 DAG.getNode(ISD::FMUL, DL, VT, Arg,
697 DAG.getConstantFP(0.15915494309, DL, MVT::f32)),
698 DAG.getConstantFP(0.5, DL, MVT::f32)));
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000699 unsigned TrigNode;
700 switch (Op.getOpcode()) {
701 case ISD::FCOS:
702 TrigNode = AMDGPUISD::COS_HW;
703 break;
704 case ISD::FSIN:
705 TrigNode = AMDGPUISD::SIN_HW;
706 break;
707 default:
708 llvm_unreachable("Wrong trig opcode");
709 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000710 SDValue TrigVal = DAG.getNode(TrigNode, DL, VT,
711 DAG.getNode(ISD::FADD, DL, VT, FractPart,
712 DAG.getConstantFP(-0.5, DL, MVT::f32)));
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000713 if (Gen >= R600Subtarget::R700)
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000714 return TrigVal;
715 // On R600 hw, COS/SIN input must be between -Pi and Pi.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000716 return DAG.getNode(ISD::FMUL, DL, VT, TrigVal,
717 DAG.getConstantFP(3.14159265359, DL, MVT::f32));
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000718}
719
Jan Vesely25f36272014-06-18 12:27:13 +0000720SDValue R600TargetLowering::LowerSHLParts(SDValue Op, SelectionDAG &DAG) const {
721 SDLoc DL(Op);
722 EVT VT = Op.getValueType();
723
724 SDValue Lo = Op.getOperand(0);
725 SDValue Hi = Op.getOperand(1);
726 SDValue Shift = Op.getOperand(2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000727 SDValue Zero = DAG.getConstant(0, DL, VT);
728 SDValue One = DAG.getConstant(1, DL, VT);
Jan Vesely25f36272014-06-18 12:27:13 +0000729
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000730 SDValue Width = DAG.getConstant(VT.getSizeInBits(), DL, VT);
731 SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, DL, VT);
Jan Vesely25f36272014-06-18 12:27:13 +0000732 SDValue BigShift = DAG.getNode(ISD::SUB, DL, VT, Shift, Width);
733 SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift);
734
735 // The dance around Width1 is necessary for 0 special case.
736 // Without it the CompShift might be 32, producing incorrect results in
737 // Overflow. So we do the shift in two steps, the alternative is to
738 // add a conditional to filter the special case.
739
740 SDValue Overflow = DAG.getNode(ISD::SRL, DL, VT, Lo, CompShift);
741 Overflow = DAG.getNode(ISD::SRL, DL, VT, Overflow, One);
742
743 SDValue HiSmall = DAG.getNode(ISD::SHL, DL, VT, Hi, Shift);
744 HiSmall = DAG.getNode(ISD::OR, DL, VT, HiSmall, Overflow);
745 SDValue LoSmall = DAG.getNode(ISD::SHL, DL, VT, Lo, Shift);
746
747 SDValue HiBig = DAG.getNode(ISD::SHL, DL, VT, Lo, BigShift);
748 SDValue LoBig = Zero;
749
750 Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT);
751 Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT);
752
753 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi);
754}
755
Jan Vesely900ff2e2014-06-18 12:27:15 +0000756SDValue R600TargetLowering::LowerSRXParts(SDValue Op, SelectionDAG &DAG) const {
757 SDLoc DL(Op);
758 EVT VT = Op.getValueType();
759
760 SDValue Lo = Op.getOperand(0);
761 SDValue Hi = Op.getOperand(1);
762 SDValue Shift = Op.getOperand(2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000763 SDValue Zero = DAG.getConstant(0, DL, VT);
764 SDValue One = DAG.getConstant(1, DL, VT);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000765
Jan Veselyecf51332014-06-18 12:27:17 +0000766 const bool SRA = Op.getOpcode() == ISD::SRA_PARTS;
767
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000768 SDValue Width = DAG.getConstant(VT.getSizeInBits(), DL, VT);
769 SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, DL, VT);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000770 SDValue BigShift = DAG.getNode(ISD::SUB, DL, VT, Shift, Width);
771 SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift);
772
773 // The dance around Width1 is necessary for 0 special case.
774 // Without it the CompShift might be 32, producing incorrect results in
775 // Overflow. So we do the shift in two steps, the alternative is to
776 // add a conditional to filter the special case.
777
778 SDValue Overflow = DAG.getNode(ISD::SHL, DL, VT, Hi, CompShift);
779 Overflow = DAG.getNode(ISD::SHL, DL, VT, Overflow, One);
780
Jan Veselyecf51332014-06-18 12:27:17 +0000781 SDValue HiSmall = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, Shift);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000782 SDValue LoSmall = DAG.getNode(ISD::SRL, DL, VT, Lo, Shift);
783 LoSmall = DAG.getNode(ISD::OR, DL, VT, LoSmall, Overflow);
784
Jan Veselyecf51332014-06-18 12:27:17 +0000785 SDValue LoBig = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, BigShift);
786 SDValue HiBig = SRA ? DAG.getNode(ISD::SRA, DL, VT, Hi, Width1) : Zero;
Jan Vesely900ff2e2014-06-18 12:27:15 +0000787
788 Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT);
789 Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT);
790
791 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi);
792}
793
Jan Vesely808fff52015-04-30 17:15:56 +0000794SDValue R600TargetLowering::LowerUADDSUBO(SDValue Op, SelectionDAG &DAG,
795 unsigned mainop, unsigned ovf) const {
796 SDLoc DL(Op);
797 EVT VT = Op.getValueType();
798
799 SDValue Lo = Op.getOperand(0);
800 SDValue Hi = Op.getOperand(1);
801
802 SDValue OVF = DAG.getNode(ovf, DL, VT, Lo, Hi);
803 // Extend sign.
804 OVF = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, OVF,
805 DAG.getValueType(MVT::i1));
806
807 SDValue Res = DAG.getNode(mainop, DL, VT, Lo, Hi);
808
809 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT, VT), Res, OVF);
810}
811
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000812SDValue R600TargetLowering::lowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000813 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000814 return DAG.getNode(
815 ISD::SETCC,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000816 DL,
Tom Stellard75aadc22012-12-11 21:25:42 +0000817 MVT::i1,
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000818 Op, DAG.getConstantFP(1.0f, DL, MVT::f32),
819 DAG.getCondCode(ISD::SETEQ));
820}
821
822SDValue R600TargetLowering::lowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const {
823 SDLoc DL(Op);
824 return DAG.getNode(
825 ISD::SETCC,
826 DL,
827 MVT::i1,
828 Op, DAG.getConstantFP(-1.0f, DL, MVT::f32),
829 DAG.getCondCode(ISD::SETEQ));
Tom Stellard75aadc22012-12-11 21:25:42 +0000830}
831
Tom Stellard75aadc22012-12-11 21:25:42 +0000832SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000833 const SDLoc &DL,
Tom Stellard75aadc22012-12-11 21:25:42 +0000834 unsigned DwordOffset) const {
835 unsigned ByteOffset = DwordOffset * 4;
836 PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
Tom Stellard1e803092013-07-23 01:48:18 +0000837 AMDGPUAS::CONSTANT_BUFFER_0);
Tom Stellard75aadc22012-12-11 21:25:42 +0000838
839 // We shouldn't be using an offset wider than 16-bits for implicit parameters.
840 assert(isInt<16>(ByteOffset));
841
842 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000843 DAG.getConstant(ByteOffset, DL, MVT::i32), // PTR
Justin Lebar9c375812016-07-15 18:27:10 +0000844 MachinePointerInfo(ConstantPointerNull::get(PtrType)));
Tom Stellard75aadc22012-12-11 21:25:42 +0000845}
846
Tom Stellard75aadc22012-12-11 21:25:42 +0000847bool R600TargetLowering::isZero(SDValue Op) const {
848 if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) {
849 return Cst->isNullValue();
850 } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){
851 return CstFP->isZero();
852 } else {
853 return false;
854 }
855}
856
Matt Arsenault6b6a2c32016-03-11 08:00:27 +0000857bool R600TargetLowering::isHWTrueValue(SDValue Op) const {
858 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
859 return CFP->isExactlyValue(1.0);
860 }
861 return isAllOnesConstant(Op);
862}
863
864bool R600TargetLowering::isHWFalseValue(SDValue Op) const {
865 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
866 return CFP->getValueAPF().isZero();
867 }
868 return isNullConstant(Op);
869}
870
Tom Stellard75aadc22012-12-11 21:25:42 +0000871SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000872 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000873 EVT VT = Op.getValueType();
874
875 SDValue LHS = Op.getOperand(0);
876 SDValue RHS = Op.getOperand(1);
877 SDValue True = Op.getOperand(2);
878 SDValue False = Op.getOperand(3);
879 SDValue CC = Op.getOperand(4);
880 SDValue Temp;
881
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +0000882 if (VT == MVT::f32) {
883 DAGCombinerInfo DCI(DAG, AfterLegalizeVectorOps, true, nullptr);
884 SDValue MinMax = CombineFMinMaxLegacy(DL, VT, LHS, RHS, True, False, CC, DCI);
885 if (MinMax)
886 return MinMax;
887 }
888
Tom Stellard75aadc22012-12-11 21:25:42 +0000889 // LHS and RHS are guaranteed to be the same value type
890 EVT CompareVT = LHS.getValueType();
891
892 // Check if we can lower this to a native operation.
893
Tom Stellard2add82d2013-03-08 15:37:09 +0000894 // Try to lower to a SET* instruction:
895 //
896 // SET* can match the following patterns:
897 //
Tom Stellardcd428182013-09-28 02:50:38 +0000898 // select_cc f32, f32, -1, 0, cc_supported
899 // select_cc f32, f32, 1.0f, 0.0f, cc_supported
900 // select_cc i32, i32, -1, 0, cc_supported
Tom Stellard2add82d2013-03-08 15:37:09 +0000901 //
902
903 // Move hardware True/False values to the correct operand.
Tom Stellardcd428182013-09-28 02:50:38 +0000904 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
905 ISD::CondCode InverseCC =
906 ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
Tom Stellard5694d302013-09-28 02:50:43 +0000907 if (isHWTrueValue(False) && isHWFalseValue(True)) {
908 if (isCondCodeLegal(InverseCC, CompareVT.getSimpleVT())) {
909 std::swap(False, True);
910 CC = DAG.getCondCode(InverseCC);
911 } else {
912 ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InverseCC);
913 if (isCondCodeLegal(SwapInvCC, CompareVT.getSimpleVT())) {
914 std::swap(False, True);
915 std::swap(LHS, RHS);
916 CC = DAG.getCondCode(SwapInvCC);
917 }
918 }
Tom Stellard2add82d2013-03-08 15:37:09 +0000919 }
920
921 if (isHWTrueValue(True) && isHWFalseValue(False) &&
922 (CompareVT == VT || VT == MVT::i32)) {
923 // This can be matched by a SET* instruction.
924 return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
925 }
926
Tom Stellard75aadc22012-12-11 21:25:42 +0000927 // Try to lower to a CND* instruction:
Tom Stellard2add82d2013-03-08 15:37:09 +0000928 //
929 // CND* can match the following patterns:
930 //
Tom Stellardcd428182013-09-28 02:50:38 +0000931 // select_cc f32, 0.0, f32, f32, cc_supported
932 // select_cc f32, 0.0, i32, i32, cc_supported
933 // select_cc i32, 0, f32, f32, cc_supported
934 // select_cc i32, 0, i32, i32, cc_supported
Tom Stellard2add82d2013-03-08 15:37:09 +0000935 //
Tom Stellardcd428182013-09-28 02:50:38 +0000936
937 // Try to move the zero value to the RHS
938 if (isZero(LHS)) {
939 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
940 // Try swapping the operands
941 ISD::CondCode CCSwapped = ISD::getSetCCSwappedOperands(CCOpcode);
942 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
943 std::swap(LHS, RHS);
944 CC = DAG.getCondCode(CCSwapped);
945 } else {
946 // Try inverting the conditon and then swapping the operands
947 ISD::CondCode CCInv = ISD::getSetCCInverse(CCOpcode, CompareVT.isInteger());
948 CCSwapped = ISD::getSetCCSwappedOperands(CCInv);
949 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
950 std::swap(True, False);
951 std::swap(LHS, RHS);
952 CC = DAG.getCondCode(CCSwapped);
953 }
954 }
955 }
956 if (isZero(RHS)) {
957 SDValue Cond = LHS;
958 SDValue Zero = RHS;
Tom Stellard75aadc22012-12-11 21:25:42 +0000959 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
960 if (CompareVT != VT) {
961 // Bitcast True / False to the correct types. This will end up being
962 // a nop, but it allows us to define only a single pattern in the
963 // .TD files for each CND* instruction rather than having to have
964 // one pattern for integer True/False and one for fp True/False
965 True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
966 False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
967 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000968
969 switch (CCOpcode) {
970 case ISD::SETONE:
971 case ISD::SETUNE:
972 case ISD::SETNE:
Tom Stellard75aadc22012-12-11 21:25:42 +0000973 CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
974 Temp = True;
975 True = False;
976 False = Temp;
977 break;
978 default:
979 break;
980 }
981 SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
982 Cond, Zero,
983 True, False,
984 DAG.getCondCode(CCOpcode));
985 return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
986 }
987
Tom Stellard75aadc22012-12-11 21:25:42 +0000988 // If we make it this for it means we have no native instructions to handle
989 // this SELECT_CC, so we must lower it.
990 SDValue HWTrue, HWFalse;
991
992 if (CompareVT == MVT::f32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000993 HWTrue = DAG.getConstantFP(1.0f, DL, CompareVT);
994 HWFalse = DAG.getConstantFP(0.0f, DL, CompareVT);
Tom Stellard75aadc22012-12-11 21:25:42 +0000995 } else if (CompareVT == MVT::i32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000996 HWTrue = DAG.getConstant(-1, DL, CompareVT);
997 HWFalse = DAG.getConstant(0, DL, CompareVT);
Tom Stellard75aadc22012-12-11 21:25:42 +0000998 }
999 else {
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +00001000 llvm_unreachable("Unhandled value type in LowerSELECT_CC");
Tom Stellard75aadc22012-12-11 21:25:42 +00001001 }
1002
1003 // Lower this unsupported SELECT_CC into a combination of two supported
1004 // SELECT_CC operations.
1005 SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
1006
1007 return DAG.getNode(ISD::SELECT_CC, DL, VT,
1008 Cond, HWFalse,
1009 True, False,
1010 DAG.getCondCode(ISD::SETNE));
1011}
1012
Alp Tokercb402912014-01-24 17:20:08 +00001013/// LLVM generates byte-addressed pointers. For indirect addressing, we need to
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001014/// convert these pointers to a register index. Each register holds
1015/// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
1016/// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
1017/// for indirect addressing.
1018SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
1019 unsigned StackWidth,
1020 SelectionDAG &DAG) const {
1021 unsigned SRLPad;
1022 switch(StackWidth) {
1023 case 1:
1024 SRLPad = 2;
1025 break;
1026 case 2:
1027 SRLPad = 3;
1028 break;
1029 case 4:
1030 SRLPad = 4;
1031 break;
1032 default: llvm_unreachable("Invalid stack width");
1033 }
1034
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001035 SDLoc DL(Ptr);
1036 return DAG.getNode(ISD::SRL, DL, Ptr.getValueType(), Ptr,
1037 DAG.getConstant(SRLPad, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001038}
1039
1040void R600TargetLowering::getStackAddress(unsigned StackWidth,
1041 unsigned ElemIdx,
1042 unsigned &Channel,
1043 unsigned &PtrIncr) const {
1044 switch (StackWidth) {
1045 default:
1046 case 1:
1047 Channel = 0;
1048 if (ElemIdx > 0) {
1049 PtrIncr = 1;
1050 } else {
1051 PtrIncr = 0;
1052 }
1053 break;
1054 case 2:
1055 Channel = ElemIdx % 2;
1056 if (ElemIdx == 2) {
1057 PtrIncr = 1;
1058 } else {
1059 PtrIncr = 0;
1060 }
1061 break;
1062 case 4:
1063 Channel = ElemIdx;
1064 PtrIncr = 0;
1065 break;
1066 }
1067}
1068
Matt Arsenault95245662016-02-11 05:32:46 +00001069SDValue R600TargetLowering::lowerPrivateTruncStore(StoreSDNode *Store,
1070 SelectionDAG &DAG) const {
1071 SDLoc DL(Store);
Tom Stellard75aadc22012-12-11 21:25:42 +00001072
Matt Arsenault95245662016-02-11 05:32:46 +00001073 unsigned Mask = 0;
1074 if (Store->getMemoryVT() == MVT::i8) {
1075 Mask = 0xff;
1076 } else if (Store->getMemoryVT() == MVT::i16) {
1077 Mask = 0xffff;
1078 }
1079
1080 SDValue Chain = Store->getChain();
1081 SDValue BasePtr = Store->getBasePtr();
1082 EVT MemVT = Store->getMemoryVT();
1083
1084 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, BasePtr,
1085 DAG.getConstant(2, DL, MVT::i32));
1086 SDValue Dst = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
1087 Chain, Ptr,
1088 DAG.getTargetConstant(0, DL, MVT::i32));
1089
1090 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, BasePtr,
1091 DAG.getConstant(0x3, DL, MVT::i32));
1092
1093 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1094 DAG.getConstant(3, DL, MVT::i32));
1095
1096 SDValue SExtValue = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i32,
1097 Store->getValue());
1098
1099 SDValue MaskedValue = DAG.getZeroExtendInReg(SExtValue, DL, MemVT);
1100
1101 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
1102 MaskedValue, ShiftAmt);
1103
1104 SDValue DstMask = DAG.getNode(ISD::SHL, DL, MVT::i32,
1105 DAG.getConstant(Mask, DL, MVT::i32),
1106 ShiftAmt);
1107 DstMask = DAG.getNode(ISD::XOR, DL, MVT::i32, DstMask,
1108 DAG.getConstant(0xffffffff, DL, MVT::i32));
1109 Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
1110
1111 SDValue Value = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
1112 return DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1113 Chain, Value, Ptr,
1114 DAG.getTargetConstant(0, DL, MVT::i32));
1115}
1116
1117SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1118 if (SDValue Result = AMDGPUTargetLowering::MergeVectorStore(Op, DAG))
Tom Stellardfbab8272013-08-16 01:12:11 +00001119 return Result;
Tom Stellardfbab8272013-08-16 01:12:11 +00001120
Matt Arsenault95245662016-02-11 05:32:46 +00001121 StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
1122 unsigned AS = StoreNode->getAddressSpace();
1123 SDValue Value = StoreNode->getValue();
1124 EVT ValueVT = Value.getValueType();
1125
1126 if ((AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::PRIVATE_ADDRESS) &&
1127 ValueVT.isVector()) {
1128 return SplitVectorStore(Op, DAG);
1129 }
1130
1131 SDLoc DL(Op);
1132 SDValue Chain = StoreNode->getChain();
1133 SDValue Ptr = StoreNode->getBasePtr();
1134
1135 if (AS == AMDGPUAS::GLOBAL_ADDRESS) {
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001136 if (StoreNode->isTruncatingStore()) {
1137 EVT VT = Value.getValueType();
Tom Stellardfbab8272013-08-16 01:12:11 +00001138 assert(VT.bitsLE(MVT::i32));
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001139 EVT MemVT = StoreNode->getMemoryVT();
1140 SDValue MaskConstant;
1141 if (MemVT == MVT::i8) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001142 MaskConstant = DAG.getConstant(0xFF, DL, MVT::i32);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001143 } else {
1144 assert(MemVT == MVT::i16);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001145 MaskConstant = DAG.getConstant(0xFFFF, DL, MVT::i32);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001146 }
1147 SDValue DWordAddr = DAG.getNode(ISD::SRL, DL, VT, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001148 DAG.getConstant(2, DL, MVT::i32));
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001149 SDValue ByteIndex = DAG.getNode(ISD::AND, DL, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001150 DAG.getConstant(0x00000003, DL, VT));
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001151 SDValue TruncValue = DAG.getNode(ISD::AND, DL, VT, Value, MaskConstant);
1152 SDValue Shift = DAG.getNode(ISD::SHL, DL, VT, ByteIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001153 DAG.getConstant(3, DL, VT));
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001154 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, VT, TruncValue, Shift);
1155 SDValue Mask = DAG.getNode(ISD::SHL, DL, VT, MaskConstant, Shift);
1156 // XXX: If we add a 64-bit ZW register class, then we could use a 2 x i32
1157 // vector instead.
1158 SDValue Src[4] = {
1159 ShiftedValue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001160 DAG.getConstant(0, DL, MVT::i32),
1161 DAG.getConstant(0, DL, MVT::i32),
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001162 Mask
1163 };
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001164 SDValue Input = DAG.getBuildVector(MVT::v4i32, DL, Src);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001165 SDValue Args[3] = { Chain, Input, DWordAddr };
1166 return DAG.getMemIntrinsicNode(AMDGPUISD::STORE_MSKOR, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00001167 Op->getVTList(), Args, MemVT,
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001168 StoreNode->getMemOperand());
1169 } else if (Ptr->getOpcode() != AMDGPUISD::DWORDADDR &&
Matt Arsenault95245662016-02-11 05:32:46 +00001170 ValueVT.bitsGE(MVT::i32)) {
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001171 // Convert pointer from byte address to dword address.
1172 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, Ptr.getValueType(),
1173 DAG.getNode(ISD::SRL, DL, Ptr.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001174 Ptr, DAG.getConstant(2, DL, MVT::i32)));
Tom Stellard75aadc22012-12-11 21:25:42 +00001175
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001176 if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +00001177 llvm_unreachable("Truncated and indexed stores not supported yet");
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001178 } else {
1179 Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
1180 }
1181 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001182 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001183 }
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001184
Matt Arsenault95245662016-02-11 05:32:46 +00001185 if (AS != AMDGPUAS::PRIVATE_ADDRESS)
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001186 return SDValue();
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001187
Matt Arsenault95245662016-02-11 05:32:46 +00001188 EVT MemVT = StoreNode->getMemoryVT();
1189 if (MemVT.bitsLT(MVT::i32))
1190 return lowerPrivateTruncStore(StoreNode, DAG);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001191
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00001192 // Lowering for indirect addressing
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001193 const MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001194 const R600FrameLowering *TFL = getSubtarget()->getFrameLowering();
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001195 unsigned StackWidth = TFL->getStackWidth(MF);
1196
1197 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1198
1199 if (ValueVT.isVector()) {
1200 unsigned NumElemVT = ValueVT.getVectorNumElements();
1201 EVT ElemVT = ValueVT.getVectorElementType();
Craig Topper48d114b2014-04-26 18:35:24 +00001202 SmallVector<SDValue, 4> Stores(NumElemVT);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001203
1204 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1205 "vector width in load");
1206
1207 for (unsigned i = 0; i < NumElemVT; ++i) {
1208 unsigned Channel, PtrIncr;
1209 getStackAddress(StackWidth, i, Channel, PtrIncr);
1210 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001211 DAG.getConstant(PtrIncr, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001212 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001213 Value, DAG.getConstant(i, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001214
1215 Stores[i] = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1216 Chain, Elem, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001217 DAG.getTargetConstant(Channel, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001218 }
Craig Topper48d114b2014-04-26 18:35:24 +00001219 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Stores);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001220 } else {
1221 if (ValueVT == MVT::i8) {
1222 Value = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Value);
1223 }
1224 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other, Chain, Value, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001225 DAG.getTargetConstant(0, DL, MVT::i32)); // Channel
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001226 }
1227
1228 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001229}
1230
Tom Stellard365366f2013-01-23 02:09:06 +00001231// return (512 + (kc_bank << 12)
1232static int
1233ConstantAddressBlock(unsigned AddressSpace) {
1234 switch (AddressSpace) {
1235 case AMDGPUAS::CONSTANT_BUFFER_0:
1236 return 512;
1237 case AMDGPUAS::CONSTANT_BUFFER_1:
1238 return 512 + 4096;
1239 case AMDGPUAS::CONSTANT_BUFFER_2:
1240 return 512 + 4096 * 2;
1241 case AMDGPUAS::CONSTANT_BUFFER_3:
1242 return 512 + 4096 * 3;
1243 case AMDGPUAS::CONSTANT_BUFFER_4:
1244 return 512 + 4096 * 4;
1245 case AMDGPUAS::CONSTANT_BUFFER_5:
1246 return 512 + 4096 * 5;
1247 case AMDGPUAS::CONSTANT_BUFFER_6:
1248 return 512 + 4096 * 6;
1249 case AMDGPUAS::CONSTANT_BUFFER_7:
1250 return 512 + 4096 * 7;
1251 case AMDGPUAS::CONSTANT_BUFFER_8:
1252 return 512 + 4096 * 8;
1253 case AMDGPUAS::CONSTANT_BUFFER_9:
1254 return 512 + 4096 * 9;
1255 case AMDGPUAS::CONSTANT_BUFFER_10:
1256 return 512 + 4096 * 10;
1257 case AMDGPUAS::CONSTANT_BUFFER_11:
1258 return 512 + 4096 * 11;
1259 case AMDGPUAS::CONSTANT_BUFFER_12:
1260 return 512 + 4096 * 12;
1261 case AMDGPUAS::CONSTANT_BUFFER_13:
1262 return 512 + 4096 * 13;
1263 case AMDGPUAS::CONSTANT_BUFFER_14:
1264 return 512 + 4096 * 14;
1265 case AMDGPUAS::CONSTANT_BUFFER_15:
1266 return 512 + 4096 * 15;
1267 default:
1268 return -1;
1269 }
1270}
1271
Matt Arsenault6dfda962016-02-10 18:21:39 +00001272SDValue R600TargetLowering::lowerPrivateExtLoad(SDValue Op,
1273 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001274 SDLoc DL(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001275 LoadSDNode *Load = cast<LoadSDNode>(Op);
1276 ISD::LoadExtType ExtType = Load->getExtensionType();
1277 EVT MemVT = Load->getMemoryVT();
Tom Stellard365366f2013-01-23 02:09:06 +00001278
Matt Arsenault6dfda962016-02-10 18:21:39 +00001279 // <SI && AS=PRIVATE && EXTLOAD && size < 32bit,
1280 // register (2-)byte extract.
1281
1282 // Get Register holding the target.
1283 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Load->getBasePtr(),
1284 DAG.getConstant(2, DL, MVT::i32));
1285 // Load the Register.
1286 SDValue Ret = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, Op.getValueType(),
1287 Load->getChain(),
1288 Ptr,
1289 DAG.getTargetConstant(0, DL, MVT::i32),
1290 Op.getOperand(2));
1291
1292 // Get offset within the register.
1293 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32,
1294 Load->getBasePtr(),
1295 DAG.getConstant(0x3, DL, MVT::i32));
1296
1297 // Bit offset of target byte (byteIdx * 8).
1298 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1299 DAG.getConstant(3, DL, MVT::i32));
1300
1301 // Shift to the right.
1302 Ret = DAG.getNode(ISD::SRL, DL, MVT::i32, Ret, ShiftAmt);
1303
1304 // Eliminate the upper bits by setting them to ...
1305 EVT MemEltVT = MemVT.getScalarType();
1306
1307 // ... ones.
1308 if (ExtType == ISD::SEXTLOAD) {
1309 SDValue MemEltVTNode = DAG.getValueType(MemEltVT);
1310
1311 SDValue Ops[] = {
1312 DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, Ret, MemEltVTNode),
1313 Load->getChain()
1314 };
1315
1316 return DAG.getMergeValues(Ops, DL);
1317 }
1318
1319 // ... or zeros.
1320 SDValue Ops[] = {
1321 DAG.getZeroExtendInReg(Ret, DL, MemEltVT),
1322 Load->getChain()
1323 };
1324
1325 return DAG.getMergeValues(Ops, DL);
1326}
1327
1328SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1329 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1330 unsigned AS = LoadNode->getAddressSpace();
1331 EVT MemVT = LoadNode->getMemoryVT();
1332 ISD::LoadExtType ExtType = LoadNode->getExtensionType();
1333
1334 if (AS == AMDGPUAS::PRIVATE_ADDRESS &&
1335 ExtType != ISD::NON_EXTLOAD && MemVT.bitsLT(MVT::i32)) {
1336 return lowerPrivateExtLoad(Op, DAG);
1337 }
1338
1339 SDLoc DL(Op);
1340 EVT VT = Op.getValueType();
1341 SDValue Chain = LoadNode->getChain();
1342 SDValue Ptr = LoadNode->getBasePtr();
Tom Stellarde9373602014-01-22 19:24:14 +00001343
Tom Stellard35bb18c2013-08-26 15:06:04 +00001344 if (LoadNode->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS && VT.isVector()) {
1345 SDValue MergedValues[2] = {
Matt Arsenault9c499c32016-04-14 23:31:26 +00001346 scalarizeVectorLoad(LoadNode, DAG),
Tom Stellard35bb18c2013-08-26 15:06:04 +00001347 Chain
1348 };
Craig Topper64941d92014-04-27 19:20:57 +00001349 return DAG.getMergeValues(MergedValues, DL);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001350 }
1351
Tom Stellard365366f2013-01-23 02:09:06 +00001352 int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
Matt Arsenault00a0d6f2013-11-13 02:39:07 +00001353 if (ConstantBlock > -1 &&
1354 ((LoadNode->getExtensionType() == ISD::NON_EXTLOAD) ||
1355 (LoadNode->getExtensionType() == ISD::ZEXTLOAD))) {
Tom Stellard365366f2013-01-23 02:09:06 +00001356 SDValue Result;
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001357 if (isa<ConstantExpr>(LoadNode->getMemOperand()->getValue()) ||
1358 isa<Constant>(LoadNode->getMemOperand()->getValue()) ||
Matt Arsenaultef1a9502013-11-01 17:39:26 +00001359 isa<ConstantSDNode>(Ptr)) {
Tom Stellard365366f2013-01-23 02:09:06 +00001360 SDValue Slots[4];
1361 for (unsigned i = 0; i < 4; i++) {
1362 // We want Const position encoded with the following formula :
1363 // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
1364 // const_index is Ptr computed by llvm using an alignment of 16.
1365 // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
1366 // then div by 4 at the ISel step
1367 SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001368 DAG.getConstant(4 * i + ConstantBlock * 16, DL, MVT::i32));
Tom Stellard365366f2013-01-23 02:09:06 +00001369 Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
1370 }
Tom Stellard0344cdf2013-08-01 15:23:42 +00001371 EVT NewVT = MVT::v4i32;
1372 unsigned NumElements = 4;
1373 if (VT.isVector()) {
1374 NewVT = VT;
1375 NumElements = VT.getVectorNumElements();
1376 }
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001377 Result = DAG.getBuildVector(NewVT, DL, makeArrayRef(Slots, NumElements));
Tom Stellard365366f2013-01-23 02:09:06 +00001378 } else {
Alp Tokerf907b892013-12-05 05:44:44 +00001379 // non-constant ptr can't be folded, keeps it as a v4f32 load
Tom Stellard365366f2013-01-23 02:09:06 +00001380 Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001381 DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr,
1382 DAG.getConstant(4, DL, MVT::i32)),
1383 DAG.getConstant(LoadNode->getAddressSpace() -
1384 AMDGPUAS::CONSTANT_BUFFER_0, DL, MVT::i32)
Tom Stellard365366f2013-01-23 02:09:06 +00001385 );
1386 }
1387
1388 if (!VT.isVector()) {
1389 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001390 DAG.getConstant(0, DL, MVT::i32));
Tom Stellard365366f2013-01-23 02:09:06 +00001391 }
1392
1393 SDValue MergedValues[2] = {
Matt Arsenault7939acd2014-04-07 16:44:24 +00001394 Result,
1395 Chain
Tom Stellard365366f2013-01-23 02:09:06 +00001396 };
Craig Topper64941d92014-04-27 19:20:57 +00001397 return DAG.getMergeValues(MergedValues, DL);
Tom Stellard365366f2013-01-23 02:09:06 +00001398 }
1399
Matt Arsenault6dfda962016-02-10 18:21:39 +00001400 SDValue LoweredLoad;
1401
Matt Arsenault909d0c02013-10-30 23:43:29 +00001402 // For most operations returning SDValue() will result in the node being
1403 // expanded by the DAG Legalizer. This is not the case for ISD::LOAD, so we
1404 // need to manually expand loads that may be legal in some address spaces and
1405 // illegal in others. SEXT loads from CONSTANT_BUFFER_0 are supported for
1406 // compute shaders, since the data is sign extended when it is uploaded to the
1407 // buffer. However SEXT loads from other address spaces are not supported, so
1408 // we need to expand them here.
Tom Stellard84021442013-07-23 01:48:24 +00001409 if (LoadNode->getExtensionType() == ISD::SEXTLOAD) {
1410 EVT MemVT = LoadNode->getMemoryVT();
1411 assert(!MemVT.isVector() && (MemVT == MVT::i16 || MemVT == MVT::i8));
Justin Lebar9c375812016-07-15 18:27:10 +00001412 SDValue NewLoad = DAG.getExtLoad(
1413 ISD::EXTLOAD, DL, VT, Chain, Ptr, LoadNode->getPointerInfo(), MemVT,
1414 LoadNode->getAlignment(), LoadNode->getMemOperand()->getFlags());
Jan Veselyb670d372015-05-26 18:07:22 +00001415 SDValue Res = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, NewLoad,
1416 DAG.getValueType(MemVT));
Tom Stellard84021442013-07-23 01:48:24 +00001417
Jan Veselyb670d372015-05-26 18:07:22 +00001418 SDValue MergedValues[2] = { Res, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00001419 return DAG.getMergeValues(MergedValues, DL);
Tom Stellard84021442013-07-23 01:48:24 +00001420 }
1421
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001422 if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1423 return SDValue();
1424 }
1425
1426 // Lowering for indirect addressing
1427 const MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001428 const R600FrameLowering *TFL = getSubtarget()->getFrameLowering();
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001429 unsigned StackWidth = TFL->getStackWidth(MF);
1430
1431 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1432
1433 if (VT.isVector()) {
1434 unsigned NumElemVT = VT.getVectorNumElements();
1435 EVT ElemVT = VT.getVectorElementType();
1436 SDValue Loads[4];
1437
Jan Vesely687ca8d2016-05-16 23:56:32 +00001438 assert(NumElemVT <= 4);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001439 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1440 "vector width in load");
1441
1442 for (unsigned i = 0; i < NumElemVT; ++i) {
1443 unsigned Channel, PtrIncr;
1444 getStackAddress(StackWidth, i, Channel, PtrIncr);
1445 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001446 DAG.getConstant(PtrIncr, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001447 Loads[i] = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, ElemVT,
1448 Chain, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001449 DAG.getTargetConstant(Channel, DL, MVT::i32),
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001450 Op.getOperand(2));
1451 }
Jan Vesely687ca8d2016-05-16 23:56:32 +00001452 EVT TargetVT = EVT::getVectorVT(*DAG.getContext(), ElemVT, NumElemVT);
1453 LoweredLoad = DAG.getBuildVector(TargetVT, DL, makeArrayRef(Loads, NumElemVT));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001454 } else {
1455 LoweredLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, VT,
1456 Chain, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001457 DAG.getTargetConstant(0, DL, MVT::i32), // Channel
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001458 Op.getOperand(2));
1459 }
1460
Matt Arsenault7939acd2014-04-07 16:44:24 +00001461 SDValue Ops[2] = {
1462 LoweredLoad,
1463 Chain
1464 };
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001465
Craig Topper64941d92014-04-27 19:20:57 +00001466 return DAG.getMergeValues(Ops, DL);
Tom Stellard365366f2013-01-23 02:09:06 +00001467}
Tom Stellard75aadc22012-12-11 21:25:42 +00001468
Matt Arsenault1d555c42014-06-23 18:00:55 +00001469SDValue R600TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
1470 SDValue Chain = Op.getOperand(0);
1471 SDValue Cond = Op.getOperand(1);
1472 SDValue Jump = Op.getOperand(2);
1473
1474 return DAG.getNode(AMDGPUISD::BRANCH_COND, SDLoc(Op), Op.getValueType(),
1475 Chain, Jump, Cond);
1476}
1477
Matt Arsenault81d06012016-03-07 21:10:13 +00001478SDValue R600TargetLowering::lowerFrameIndex(SDValue Op,
1479 SelectionDAG &DAG) const {
1480 MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001481 const R600FrameLowering *TFL = getSubtarget()->getFrameLowering();
Matt Arsenault81d06012016-03-07 21:10:13 +00001482
1483 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op);
1484
1485 unsigned FrameIndex = FIN->getIndex();
1486 unsigned IgnoredFrameReg;
1487 unsigned Offset =
1488 TFL->getFrameIndexReference(MF, FrameIndex, IgnoredFrameReg);
1489 return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF), SDLoc(Op),
1490 Op.getValueType());
1491}
1492
Tom Stellard75aadc22012-12-11 21:25:42 +00001493/// XXX Only kernel functions are supported, so we can assume for now that
1494/// every function is a kernel function, but in the future we should use
1495/// separate calling conventions for kernel and non-kernel functions.
1496SDValue R600TargetLowering::LowerFormalArguments(
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001497 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1498 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
1499 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Tom Stellardacfeebf2013-07-23 01:48:05 +00001500 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001501 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1502 *DAG.getContext());
Vincent Lejeunef143af32013-11-11 22:10:24 +00001503 MachineFunction &MF = DAG.getMachineFunction();
Jan Veselye5121f32014-10-14 20:05:26 +00001504 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
Tom Stellardacfeebf2013-07-23 01:48:05 +00001505
Tom Stellardaf775432013-10-23 00:44:32 +00001506 SmallVector<ISD::InputArg, 8> LocalIns;
1507
Matt Arsenault209a7b92014-04-18 07:40:20 +00001508 getOriginalFunctionArgs(DAG, MF.getFunction(), Ins, LocalIns);
Tom Stellardaf775432013-10-23 00:44:32 +00001509
1510 AnalyzeFormalArguments(CCInfo, LocalIns);
Tom Stellardacfeebf2013-07-23 01:48:05 +00001511
Tom Stellard1e803092013-07-23 01:48:18 +00001512 for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
Tom Stellardacfeebf2013-07-23 01:48:05 +00001513 CCValAssign &VA = ArgLocs[i];
Matt Arsenault74ef2772014-08-13 18:14:11 +00001514 const ISD::InputArg &In = Ins[i];
1515 EVT VT = In.VT;
1516 EVT MemVT = VA.getLocVT();
1517 if (!VT.isVector() && MemVT.isVector()) {
1518 // Get load source type if scalarized.
1519 MemVT = MemVT.getVectorElementType();
1520 }
Tom Stellard78e01292013-07-23 01:47:58 +00001521
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001522 if (AMDGPU::isShader(CallConv)) {
Vincent Lejeunef143af32013-11-11 22:10:24 +00001523 unsigned Reg = MF.addLiveIn(VA.getLocReg(), &AMDGPU::R600_Reg128RegClass);
1524 SDValue Register = DAG.getCopyFromReg(Chain, DL, Reg, VT);
1525 InVals.push_back(Register);
1526 continue;
1527 }
1528
Tom Stellard75aadc22012-12-11 21:25:42 +00001529 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
Matt Arsenault74ef2772014-08-13 18:14:11 +00001530 AMDGPUAS::CONSTANT_BUFFER_0);
Tom Stellardacfeebf2013-07-23 01:48:05 +00001531
Matt Arsenaultfae02982014-03-17 18:58:11 +00001532 // i64 isn't a legal type, so the register type used ends up as i32, which
1533 // isn't expected here. It attempts to create this sextload, but it ends up
1534 // being invalid. Somehow this seems to work with i64 arguments, but breaks
1535 // for <1 x i64>.
1536
Tom Stellardacfeebf2013-07-23 01:48:05 +00001537 // The first 36 bytes of the input buffer contains information about
1538 // thread group and global sizes.
Matt Arsenault74ef2772014-08-13 18:14:11 +00001539 ISD::LoadExtType Ext = ISD::NON_EXTLOAD;
1540 if (MemVT.getScalarSizeInBits() != VT.getScalarSizeInBits()) {
1541 // FIXME: This should really check the extload type, but the handling of
1542 // extload vector parameters seems to be broken.
Matt Arsenaulte1f030c2014-04-11 20:59:54 +00001543
Matt Arsenault74ef2772014-08-13 18:14:11 +00001544 // Ext = In.Flags.isSExt() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
1545 Ext = ISD::SEXTLOAD;
1546 }
1547
1548 // Compute the offset from the value.
1549 // XXX - I think PartOffset should give you this, but it seems to give the
1550 // size of the register which isn't useful.
1551
Andrew Trick05938a52015-02-16 18:10:47 +00001552 unsigned ValBase = ArgLocs[In.getOrigArgIndex()].getLocMemOffset();
Matt Arsenault74ef2772014-08-13 18:14:11 +00001553 unsigned PartOffset = VA.getLocMemOffset();
Matt Arsenault52ef4012016-07-26 16:45:58 +00001554 unsigned Offset = Subtarget->getExplicitKernelArgOffset() + VA.getLocMemOffset();
Matt Arsenault74ef2772014-08-13 18:14:11 +00001555
1556 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy), PartOffset - ValBase);
Justin Lebar9c375812016-07-15 18:27:10 +00001557 SDValue Arg = DAG.getLoad(
1558 ISD::UNINDEXED, Ext, VT, DL, Chain,
1559 DAG.getConstant(Offset, DL, MVT::i32), DAG.getUNDEF(MVT::i32), PtrInfo,
1560 MemVT, /* Alignment = */ 4,
1561 MachineMemOperand::MONonTemporal | MachineMemOperand::MOInvariant);
Matt Arsenault209a7b92014-04-18 07:40:20 +00001562
1563 // 4 is the preferred alignment for the CONSTANT memory space.
Tom Stellard75aadc22012-12-11 21:25:42 +00001564 InVals.push_back(Arg);
Matt Arsenault52ef4012016-07-26 16:45:58 +00001565 MFI->setABIArgOffset(Offset + MemVT.getStoreSize());
Tom Stellard75aadc22012-12-11 21:25:42 +00001566 }
1567 return Chain;
1568}
1569
Mehdi Amini44ede332015-07-09 02:09:04 +00001570EVT R600TargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1571 EVT VT) const {
Matt Arsenault209a7b92014-04-18 07:40:20 +00001572 if (!VT.isVector())
1573 return MVT::i32;
Tom Stellard75aadc22012-12-11 21:25:42 +00001574 return VT.changeVectorElementTypeToInteger();
1575}
1576
Matt Arsenaultfa67bdb2016-02-22 21:04:16 +00001577bool R600TargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
1578 unsigned AddrSpace,
1579 unsigned Align,
1580 bool *IsFast) const {
1581 if (IsFast)
1582 *IsFast = false;
1583
1584 if (!VT.isSimple() || VT == MVT::Other)
1585 return false;
1586
1587 if (VT.bitsLT(MVT::i32))
1588 return false;
1589
1590 // TODO: This is a rough estimate.
1591 if (IsFast)
1592 *IsFast = true;
1593
1594 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
1595}
1596
Matt Arsenault209a7b92014-04-18 07:40:20 +00001597static SDValue CompactSwizzlableVector(
1598 SelectionDAG &DAG, SDValue VectorEntry,
1599 DenseMap<unsigned, unsigned> &RemapSwizzle) {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001600 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1601 assert(RemapSwizzle.empty());
1602 SDValue NewBldVec[4] = {
Matt Arsenault209a7b92014-04-18 07:40:20 +00001603 VectorEntry.getOperand(0),
1604 VectorEntry.getOperand(1),
1605 VectorEntry.getOperand(2),
1606 VectorEntry.getOperand(3)
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001607 };
1608
1609 for (unsigned i = 0; i < 4; i++) {
Sanjay Patel57195842016-03-14 17:28:46 +00001610 if (NewBldVec[i].isUndef())
Vincent Lejeunefa58a5f2013-10-13 17:56:10 +00001611 // We mask write here to teach later passes that the ith element of this
1612 // vector is undef. Thus we can use it to reduce 128 bits reg usage,
1613 // break false dependencies and additionnaly make assembly easier to read.
1614 RemapSwizzle[i] = 7; // SEL_MASK_WRITE
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001615 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(NewBldVec[i])) {
1616 if (C->isZero()) {
1617 RemapSwizzle[i] = 4; // SEL_0
1618 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1619 } else if (C->isExactlyValue(1.0)) {
1620 RemapSwizzle[i] = 5; // SEL_1
1621 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1622 }
1623 }
1624
Sanjay Patel57195842016-03-14 17:28:46 +00001625 if (NewBldVec[i].isUndef())
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001626 continue;
1627 for (unsigned j = 0; j < i; j++) {
1628 if (NewBldVec[i] == NewBldVec[j]) {
1629 NewBldVec[i] = DAG.getUNDEF(NewBldVec[i].getValueType());
1630 RemapSwizzle[i] = j;
1631 break;
1632 }
1633 }
1634 }
1635
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001636 return DAG.getBuildVector(VectorEntry.getValueType(), SDLoc(VectorEntry),
1637 NewBldVec);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001638}
1639
Benjamin Kramer193960c2013-06-11 13:32:25 +00001640static SDValue ReorganizeVector(SelectionDAG &DAG, SDValue VectorEntry,
1641 DenseMap<unsigned, unsigned> &RemapSwizzle) {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001642 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1643 assert(RemapSwizzle.empty());
1644 SDValue NewBldVec[4] = {
1645 VectorEntry.getOperand(0),
1646 VectorEntry.getOperand(1),
1647 VectorEntry.getOperand(2),
1648 VectorEntry.getOperand(3)
1649 };
1650 bool isUnmovable[4] = { false, false, false, false };
Vincent Lejeunecc0ea742013-12-10 14:43:31 +00001651 for (unsigned i = 0; i < 4; i++) {
Vincent Lejeuneb8aac8d2013-07-09 15:03:25 +00001652 RemapSwizzle[i] = i;
Vincent Lejeunecc0ea742013-12-10 14:43:31 +00001653 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1654 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1655 ->getZExtValue();
1656 if (i == Idx)
1657 isUnmovable[Idx] = true;
1658 }
1659 }
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001660
1661 for (unsigned i = 0; i < 4; i++) {
1662 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1663 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1664 ->getZExtValue();
Vincent Lejeune301beb82013-10-13 17:56:04 +00001665 if (isUnmovable[Idx])
1666 continue;
1667 // Swap i and Idx
1668 std::swap(NewBldVec[Idx], NewBldVec[i]);
1669 std::swap(RemapSwizzle[i], RemapSwizzle[Idx]);
1670 break;
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001671 }
1672 }
1673
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001674 return DAG.getBuildVector(VectorEntry.getValueType(), SDLoc(VectorEntry),
1675 NewBldVec);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001676}
1677
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001678SDValue R600TargetLowering::OptimizeSwizzle(SDValue BuildVector, SDValue Swz[4],
1679 SelectionDAG &DAG,
1680 const SDLoc &DL) const {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001681 assert(BuildVector.getOpcode() == ISD::BUILD_VECTOR);
1682 // Old -> New swizzle values
1683 DenseMap<unsigned, unsigned> SwizzleRemap;
1684
1685 BuildVector = CompactSwizzlableVector(DAG, BuildVector, SwizzleRemap);
1686 for (unsigned i = 0; i < 4; i++) {
Benjamin Kramer619c4e52015-04-10 11:24:51 +00001687 unsigned Idx = cast<ConstantSDNode>(Swz[i])->getZExtValue();
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001688 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001689 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], DL, MVT::i32);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001690 }
1691
1692 SwizzleRemap.clear();
1693 BuildVector = ReorganizeVector(DAG, BuildVector, SwizzleRemap);
1694 for (unsigned i = 0; i < 4; i++) {
Benjamin Kramer619c4e52015-04-10 11:24:51 +00001695 unsigned Idx = cast<ConstantSDNode>(Swz[i])->getZExtValue();
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001696 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001697 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], DL, MVT::i32);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001698 }
1699
1700 return BuildVector;
1701}
1702
1703
Tom Stellard75aadc22012-12-11 21:25:42 +00001704//===----------------------------------------------------------------------===//
1705// Custom DAG Optimizations
1706//===----------------------------------------------------------------------===//
1707
1708SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
1709 DAGCombinerInfo &DCI) const {
1710 SelectionDAG &DAG = DCI.DAG;
1711
1712 switch (N->getOpcode()) {
Tom Stellard50122a52014-04-07 19:45:41 +00001713 default: return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001714 // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
1715 case ISD::FP_ROUND: {
1716 SDValue Arg = N->getOperand(0);
1717 if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001718 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), N->getValueType(0),
Tom Stellard75aadc22012-12-11 21:25:42 +00001719 Arg.getOperand(0));
1720 }
1721 break;
1722 }
Tom Stellarde06163a2013-02-07 14:02:35 +00001723
1724 // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
1725 // (i32 select_cc f32, f32, -1, 0 cc)
1726 //
1727 // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1728 // this to one of the SET*_DX10 instructions.
1729 case ISD::FP_TO_SINT: {
1730 SDValue FNeg = N->getOperand(0);
1731 if (FNeg.getOpcode() != ISD::FNEG) {
1732 return SDValue();
1733 }
1734 SDValue SelectCC = FNeg.getOperand(0);
1735 if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1736 SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1737 SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1738 !isHWTrueValue(SelectCC.getOperand(2)) ||
1739 !isHWFalseValue(SelectCC.getOperand(3))) {
1740 return SDValue();
1741 }
1742
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001743 SDLoc dl(N);
1744 return DAG.getNode(ISD::SELECT_CC, dl, N->getValueType(0),
Tom Stellarde06163a2013-02-07 14:02:35 +00001745 SelectCC.getOperand(0), // LHS
1746 SelectCC.getOperand(1), // RHS
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001747 DAG.getConstant(-1, dl, MVT::i32), // True
1748 DAG.getConstant(0, dl, MVT::i32), // False
Tom Stellarde06163a2013-02-07 14:02:35 +00001749 SelectCC.getOperand(4)); // CC
1750
1751 break;
1752 }
Quentin Colombete2e05482013-07-30 00:27:16 +00001753
NAKAMURA Takumi8a046432013-10-28 04:07:38 +00001754 // insert_vector_elt (build_vector elt0, ... , eltN), NewEltIdx, idx
1755 // => build_vector elt0, ... , NewEltIdx, ... , eltN
Quentin Colombete2e05482013-07-30 00:27:16 +00001756 case ISD::INSERT_VECTOR_ELT: {
1757 SDValue InVec = N->getOperand(0);
1758 SDValue InVal = N->getOperand(1);
1759 SDValue EltNo = N->getOperand(2);
1760 SDLoc dl(N);
1761
1762 // If the inserted element is an UNDEF, just use the input vector.
Sanjay Patel57195842016-03-14 17:28:46 +00001763 if (InVal.isUndef())
Quentin Colombete2e05482013-07-30 00:27:16 +00001764 return InVec;
1765
1766 EVT VT = InVec.getValueType();
1767
1768 // If we can't generate a legal BUILD_VECTOR, exit
1769 if (!isOperationLegal(ISD::BUILD_VECTOR, VT))
1770 return SDValue();
1771
1772 // Check that we know which element is being inserted
1773 if (!isa<ConstantSDNode>(EltNo))
1774 return SDValue();
1775 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
1776
1777 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
1778 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
1779 // vector elements.
1780 SmallVector<SDValue, 8> Ops;
1781 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
1782 Ops.append(InVec.getNode()->op_begin(),
1783 InVec.getNode()->op_end());
Sanjay Patel57195842016-03-14 17:28:46 +00001784 } else if (InVec.isUndef()) {
Quentin Colombete2e05482013-07-30 00:27:16 +00001785 unsigned NElts = VT.getVectorNumElements();
1786 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
1787 } else {
1788 return SDValue();
1789 }
1790
1791 // Insert the element
1792 if (Elt < Ops.size()) {
1793 // All the operands of BUILD_VECTOR must have the same type;
1794 // we enforce that here.
1795 EVT OpVT = Ops[0].getValueType();
1796 if (InVal.getValueType() != OpVT)
1797 InVal = OpVT.bitsGT(InVal.getValueType()) ?
1798 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
1799 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
1800 Ops[Elt] = InVal;
1801 }
1802
1803 // Return the new vector
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001804 return DAG.getBuildVector(VT, dl, Ops);
Quentin Colombete2e05482013-07-30 00:27:16 +00001805 }
1806
Tom Stellard365366f2013-01-23 02:09:06 +00001807 // Extract_vec (Build_vector) generated by custom lowering
1808 // also needs to be customly combined
1809 case ISD::EXTRACT_VECTOR_ELT: {
1810 SDValue Arg = N->getOperand(0);
1811 if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1812 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1813 unsigned Element = Const->getZExtValue();
1814 return Arg->getOperand(Element);
1815 }
1816 }
Tom Stellarddd04c832013-01-31 22:11:53 +00001817 if (Arg.getOpcode() == ISD::BITCAST &&
1818 Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
1819 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1820 unsigned Element = Const->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001821 return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getVTList(),
Tom Stellarddd04c832013-01-31 22:11:53 +00001822 Arg->getOperand(0).getOperand(Element));
1823 }
1824 }
Mehdi Aminie029eae2015-07-16 06:23:12 +00001825 break;
Tom Stellard365366f2013-01-23 02:09:06 +00001826 }
Tom Stellarde06163a2013-02-07 14:02:35 +00001827
1828 case ISD::SELECT_CC: {
Tom Stellardafa8b532014-05-09 16:42:16 +00001829 // Try common optimizations
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00001830 if (SDValue Ret = AMDGPUTargetLowering::PerformDAGCombine(N, DCI))
Tom Stellardafa8b532014-05-09 16:42:16 +00001831 return Ret;
1832
Tom Stellarde06163a2013-02-07 14:02:35 +00001833 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1834 // selectcc x, y, a, b, inv(cc)
Tom Stellard5e524892013-03-08 15:37:11 +00001835 //
1836 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, setne ->
1837 // selectcc x, y, a, b, cc
Tom Stellarde06163a2013-02-07 14:02:35 +00001838 SDValue LHS = N->getOperand(0);
1839 if (LHS.getOpcode() != ISD::SELECT_CC) {
1840 return SDValue();
1841 }
1842
1843 SDValue RHS = N->getOperand(1);
1844 SDValue True = N->getOperand(2);
1845 SDValue False = N->getOperand(3);
Tom Stellard5e524892013-03-08 15:37:11 +00001846 ISD::CondCode NCC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Tom Stellarde06163a2013-02-07 14:02:35 +00001847
1848 if (LHS.getOperand(2).getNode() != True.getNode() ||
1849 LHS.getOperand(3).getNode() != False.getNode() ||
Tom Stellard5e524892013-03-08 15:37:11 +00001850 RHS.getNode() != False.getNode()) {
Tom Stellarde06163a2013-02-07 14:02:35 +00001851 return SDValue();
1852 }
1853
Tom Stellard5e524892013-03-08 15:37:11 +00001854 switch (NCC) {
1855 default: return SDValue();
1856 case ISD::SETNE: return LHS;
1857 case ISD::SETEQ: {
1858 ISD::CondCode LHSCC = cast<CondCodeSDNode>(LHS.getOperand(4))->get();
1859 LHSCC = ISD::getSetCCInverse(LHSCC,
1860 LHS.getOperand(0).getValueType().isInteger());
Tom Stellardcd428182013-09-28 02:50:38 +00001861 if (DCI.isBeforeLegalizeOps() ||
1862 isCondCodeLegal(LHSCC, LHS.getOperand(0).getSimpleValueType()))
1863 return DAG.getSelectCC(SDLoc(N),
1864 LHS.getOperand(0),
1865 LHS.getOperand(1),
1866 LHS.getOperand(2),
1867 LHS.getOperand(3),
1868 LHSCC);
1869 break;
Vincent Lejeuned80bc152013-02-14 16:55:06 +00001870 }
Tom Stellard5e524892013-03-08 15:37:11 +00001871 }
Tom Stellardcd428182013-09-28 02:50:38 +00001872 return SDValue();
Tom Stellard5e524892013-03-08 15:37:11 +00001873 }
Tom Stellardfbab8272013-08-16 01:12:11 +00001874
Vincent Lejeuned80bc152013-02-14 16:55:06 +00001875 case AMDGPUISD::EXPORT: {
1876 SDValue Arg = N->getOperand(1);
1877 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1878 break;
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001879
Vincent Lejeuned80bc152013-02-14 16:55:06 +00001880 SDValue NewArgs[8] = {
1881 N->getOperand(0), // Chain
1882 SDValue(),
1883 N->getOperand(2), // ArrayBase
1884 N->getOperand(3), // Type
1885 N->getOperand(4), // SWZ_X
1886 N->getOperand(5), // SWZ_Y
1887 N->getOperand(6), // SWZ_Z
1888 N->getOperand(7) // SWZ_W
1889 };
Andrew Trickef9de2a2013-05-25 02:42:55 +00001890 SDLoc DL(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001891 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[4], DAG, DL);
Craig Topper48d114b2014-04-26 18:35:24 +00001892 return DAG.getNode(AMDGPUISD::EXPORT, DL, N->getVTList(), NewArgs);
Tom Stellarde06163a2013-02-07 14:02:35 +00001893 }
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001894 case AMDGPUISD::TEXTURE_FETCH: {
1895 SDValue Arg = N->getOperand(1);
1896 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1897 break;
1898
1899 SDValue NewArgs[19] = {
1900 N->getOperand(0),
1901 N->getOperand(1),
1902 N->getOperand(2),
1903 N->getOperand(3),
1904 N->getOperand(4),
1905 N->getOperand(5),
1906 N->getOperand(6),
1907 N->getOperand(7),
1908 N->getOperand(8),
1909 N->getOperand(9),
1910 N->getOperand(10),
1911 N->getOperand(11),
1912 N->getOperand(12),
1913 N->getOperand(13),
1914 N->getOperand(14),
1915 N->getOperand(15),
1916 N->getOperand(16),
1917 N->getOperand(17),
1918 N->getOperand(18),
1919 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001920 SDLoc DL(N);
1921 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[2], DAG, DL);
1922 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, N->getVTList(), NewArgs);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001923 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001924 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00001925
1926 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001927}
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001928
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001929bool R600TargetLowering::FoldOperand(SDNode *ParentNode, unsigned SrcIdx,
1930 SDValue &Src, SDValue &Neg, SDValue &Abs,
1931 SDValue &Sel, SDValue &Imm,
1932 SelectionDAG &DAG) const {
1933 const R600InstrInfo *TII = getSubtarget()->getInstrInfo();
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001934 if (!Src.isMachineOpcode())
1935 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001936
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001937 switch (Src.getMachineOpcode()) {
1938 case AMDGPU::FNEG_R600:
1939 if (!Neg.getNode())
1940 return false;
1941 Src = Src.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001942 Neg = DAG.getTargetConstant(1, SDLoc(ParentNode), MVT::i32);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001943 return true;
1944 case AMDGPU::FABS_R600:
1945 if (!Abs.getNode())
1946 return false;
1947 Src = Src.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001948 Abs = DAG.getTargetConstant(1, SDLoc(ParentNode), MVT::i32);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001949 return true;
1950 case AMDGPU::CONST_COPY: {
1951 unsigned Opcode = ParentNode->getMachineOpcode();
1952 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
1953
1954 if (!Sel.getNode())
1955 return false;
1956
1957 SDValue CstOffset = Src.getOperand(0);
1958 if (ParentNode->getValueType(0).isVector())
1959 return false;
1960
1961 // Gather constants values
1962 int SrcIndices[] = {
1963 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
1964 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
1965 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2),
1966 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
1967 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
1968 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
1969 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
1970 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
1971 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
1972 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
1973 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
1974 };
1975 std::vector<unsigned> Consts;
Matt Arsenault4d64f962014-05-12 19:23:21 +00001976 for (int OtherSrcIdx : SrcIndices) {
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001977 int OtherSelIdx = TII->getSelIdx(Opcode, OtherSrcIdx);
1978 if (OtherSrcIdx < 0 || OtherSelIdx < 0)
1979 continue;
1980 if (HasDst) {
1981 OtherSrcIdx--;
1982 OtherSelIdx--;
1983 }
1984 if (RegisterSDNode *Reg =
1985 dyn_cast<RegisterSDNode>(ParentNode->getOperand(OtherSrcIdx))) {
1986 if (Reg->getReg() == AMDGPU::ALU_CONST) {
Matt Arsenaultb3ee3882014-05-12 19:26:38 +00001987 ConstantSDNode *Cst
1988 = cast<ConstantSDNode>(ParentNode->getOperand(OtherSelIdx));
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001989 Consts.push_back(Cst->getZExtValue());
1990 }
1991 }
1992 }
1993
Matt Arsenault37c12d72014-05-12 20:42:57 +00001994 ConstantSDNode *Cst = cast<ConstantSDNode>(CstOffset);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001995 Consts.push_back(Cst->getZExtValue());
1996 if (!TII->fitsConstReadLimitations(Consts)) {
1997 return false;
1998 }
1999
2000 Sel = CstOffset;
2001 Src = DAG.getRegister(AMDGPU::ALU_CONST, MVT::f32);
2002 return true;
2003 }
Jan Vesely16800392016-05-13 20:39:31 +00002004 case AMDGPU::MOV_IMM_GLOBAL_ADDR:
2005 // Check if the Imm slot is used. Taken from below.
2006 if (cast<ConstantSDNode>(Imm)->getZExtValue())
2007 return false;
2008 Imm = Src.getOperand(0);
2009 Src = DAG.getRegister(AMDGPU::ALU_LITERAL_X, MVT::i32);
2010 return true;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002011 case AMDGPU::MOV_IMM_I32:
2012 case AMDGPU::MOV_IMM_F32: {
2013 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
2014 uint64_t ImmValue = 0;
2015
2016
2017 if (Src.getMachineOpcode() == AMDGPU::MOV_IMM_F32) {
2018 ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Src.getOperand(0));
2019 float FloatValue = FPC->getValueAPF().convertToFloat();
2020 if (FloatValue == 0.0) {
2021 ImmReg = AMDGPU::ZERO;
2022 } else if (FloatValue == 0.5) {
2023 ImmReg = AMDGPU::HALF;
2024 } else if (FloatValue == 1.0) {
2025 ImmReg = AMDGPU::ONE;
2026 } else {
2027 ImmValue = FPC->getValueAPF().bitcastToAPInt().getZExtValue();
2028 }
2029 } else {
2030 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(0));
2031 uint64_t Value = C->getZExtValue();
2032 if (Value == 0) {
2033 ImmReg = AMDGPU::ZERO;
2034 } else if (Value == 1) {
2035 ImmReg = AMDGPU::ONE_INT;
2036 } else {
2037 ImmValue = Value;
2038 }
2039 }
2040
2041 // Check that we aren't already using an immediate.
2042 // XXX: It's possible for an instruction to have more than one
2043 // immediate operand, but this is not supported yet.
2044 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
2045 if (!Imm.getNode())
2046 return false;
2047 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Imm);
2048 assert(C);
2049 if (C->getZExtValue())
2050 return false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002051 Imm = DAG.getTargetConstant(ImmValue, SDLoc(ParentNode), MVT::i32);
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002052 }
2053 Src = DAG.getRegister(ImmReg, MVT::i32);
2054 return true;
2055 }
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002056 default:
2057 return false;
2058 }
2059}
2060
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002061/// \brief Fold the instructions after selecting them
2062SDNode *R600TargetLowering::PostISelFolding(MachineSDNode *Node,
2063 SelectionDAG &DAG) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002064 const R600InstrInfo *TII = getSubtarget()->getInstrInfo();
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002065 if (!Node->isMachineOpcode())
2066 return Node;
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002067
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002068 unsigned Opcode = Node->getMachineOpcode();
2069 SDValue FakeOp;
2070
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002071 std::vector<SDValue> Ops(Node->op_begin(), Node->op_end());
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002072
2073 if (Opcode == AMDGPU::DOT_4) {
2074 int OperandIdx[] = {
2075 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
2076 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
2077 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
2078 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
2079 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
2080 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
2081 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
2082 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
NAKAMURA Takumi4bb85f92013-10-28 04:07:23 +00002083 };
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002084 int NegIdx[] = {
2085 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_X),
2086 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Y),
2087 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Z),
2088 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_W),
2089 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_X),
2090 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Y),
2091 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Z),
2092 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_W)
2093 };
2094 int AbsIdx[] = {
2095 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_X),
2096 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Y),
2097 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Z),
2098 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_W),
2099 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_X),
2100 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Y),
2101 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Z),
2102 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_W)
2103 };
2104 for (unsigned i = 0; i < 8; i++) {
2105 if (OperandIdx[i] < 0)
2106 return Node;
2107 SDValue &Src = Ops[OperandIdx[i] - 1];
2108 SDValue &Neg = Ops[NegIdx[i] - 1];
2109 SDValue &Abs = Ops[AbsIdx[i] - 1];
2110 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2111 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
2112 if (HasDst)
2113 SelIdx--;
2114 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002115 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, FakeOp, DAG))
2116 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2117 }
2118 } else if (Opcode == AMDGPU::REG_SEQUENCE) {
2119 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) {
2120 SDValue &Src = Ops[i];
2121 if (FoldOperand(Node, i, Src, FakeOp, FakeOp, FakeOp, FakeOp, DAG))
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002122 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2123 }
Vincent Lejeune0167a312013-09-12 23:45:00 +00002124 } else if (Opcode == AMDGPU::CLAMP_R600) {
2125 SDValue Src = Node->getOperand(0);
2126 if (!Src.isMachineOpcode() ||
2127 !TII->hasInstrModifiers(Src.getMachineOpcode()))
2128 return Node;
2129 int ClampIdx = TII->getOperandIdx(Src.getMachineOpcode(),
2130 AMDGPU::OpName::clamp);
2131 if (ClampIdx < 0)
2132 return Node;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002133 SDLoc DL(Node);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002134 std::vector<SDValue> Ops(Src->op_begin(), Src->op_end());
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002135 Ops[ClampIdx - 1] = DAG.getTargetConstant(1, DL, MVT::i32);
2136 return DAG.getMachineNode(Src.getMachineOpcode(), DL,
2137 Node->getVTList(), Ops);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002138 } else {
2139 if (!TII->hasInstrModifiers(Opcode))
2140 return Node;
2141 int OperandIdx[] = {
2142 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
2143 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
2144 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2)
2145 };
2146 int NegIdx[] = {
2147 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg),
2148 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg),
2149 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_neg)
2150 };
2151 int AbsIdx[] = {
2152 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs),
2153 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs),
2154 -1
2155 };
2156 for (unsigned i = 0; i < 3; i++) {
2157 if (OperandIdx[i] < 0)
2158 return Node;
2159 SDValue &Src = Ops[OperandIdx[i] - 1];
2160 SDValue &Neg = Ops[NegIdx[i] - 1];
2161 SDValue FakeAbs;
2162 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
2163 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2164 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002165 int ImmIdx = TII->getOperandIdx(Opcode, AMDGPU::OpName::literal);
2166 if (HasDst) {
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002167 SelIdx--;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002168 ImmIdx--;
2169 }
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002170 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002171 SDValue &Imm = Ops[ImmIdx];
2172 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, Imm, DAG))
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002173 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2174 }
2175 }
2176
2177 return Node;
2178}