blob: ad3f481047eece94f7da298ad1f09a7b9c058063 [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 }
589 // Fall-through. Since we don't care about out of bounds values
590 // we can use FP_TO_SINT for uints too. The DAGLegalizer code for uint
591 // considers some extra cases which are not necessary here.
592 case ISD::FP_TO_SINT: {
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000593 if (N->getValueType(0) == MVT::i1) {
594 Results.push_back(lowerFP_TO_SINT(N->getOperand(0), DAG));
595 return;
596 }
597
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000598 SDValue Result;
599 if (expandFP_TO_SINT(N, Result, DAG))
600 Results.push_back(Result);
Tom Stellard365366f2013-01-23 02:09:06 +0000601 return;
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000602 }
Jan Vesely343cd6f02014-06-22 21:43:01 +0000603 case ISD::SDIVREM: {
604 SDValue Op = SDValue(N, 1);
605 SDValue RES = LowerSDIVREM(Op, DAG);
606 Results.push_back(RES);
607 Results.push_back(RES.getValue(1));
608 break;
609 }
610 case ISD::UDIVREM: {
611 SDValue Op = SDValue(N, 0);
Tom Stellardbf69d762014-11-15 01:07:53 +0000612 LowerUDIVREM64(Op, DAG, Results);
Jan Vesely343cd6f02014-06-22 21:43:01 +0000613 break;
614 }
615 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000616}
617
Tom Stellard880a80a2014-06-17 16:53:14 +0000618SDValue R600TargetLowering::vectorToVerticalVector(SelectionDAG &DAG,
619 SDValue Vector) const {
620
621 SDLoc DL(Vector);
622 EVT VecVT = Vector.getValueType();
623 EVT EltVT = VecVT.getVectorElementType();
624 SmallVector<SDValue, 8> Args;
625
626 for (unsigned i = 0, e = VecVT.getVectorNumElements();
627 i != e; ++i) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000628 Args.push_back(DAG.getNode(
629 ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Vector,
630 DAG.getConstant(i, DL, getVectorIdxTy(DAG.getDataLayout()))));
Tom Stellard880a80a2014-06-17 16:53:14 +0000631 }
632
633 return DAG.getNode(AMDGPUISD::BUILD_VERTICAL_VECTOR, DL, VecVT, Args);
634}
635
636SDValue R600TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
637 SelectionDAG &DAG) const {
638
639 SDLoc DL(Op);
640 SDValue Vector = Op.getOperand(0);
641 SDValue Index = Op.getOperand(1);
642
643 if (isa<ConstantSDNode>(Index) ||
644 Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR)
645 return Op;
646
647 Vector = vectorToVerticalVector(DAG, Vector);
648 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, Op.getValueType(),
649 Vector, Index);
650}
651
652SDValue R600TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
653 SelectionDAG &DAG) const {
654 SDLoc DL(Op);
655 SDValue Vector = Op.getOperand(0);
656 SDValue Value = Op.getOperand(1);
657 SDValue Index = Op.getOperand(2);
658
659 if (isa<ConstantSDNode>(Index) ||
660 Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR)
661 return Op;
662
663 Vector = vectorToVerticalVector(DAG, Vector);
664 SDValue Insert = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, Op.getValueType(),
665 Vector, Value, Index);
666 return vectorToVerticalVector(DAG, Insert);
667}
668
Tom Stellard27233b72016-05-02 18:05:17 +0000669SDValue R600TargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
670 SDValue Op,
671 SelectionDAG &DAG) const {
672
673 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
674 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
675 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
676
677 const DataLayout &DL = DAG.getDataLayout();
678 const GlobalValue *GV = GSD->getGlobal();
Tom Stellard27233b72016-05-02 18:05:17 +0000679 MVT ConstPtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
680
Jan Veselyf97de002016-05-13 20:39:29 +0000681 SDValue GA = DAG.getTargetGlobalAddress(GV, SDLoc(GSD), ConstPtrVT);
682 return DAG.getNode(AMDGPUISD::CONST_DATA_PTR, SDLoc(GSD), ConstPtrVT, GA);
Tom Stellard27233b72016-05-02 18:05:17 +0000683}
684
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000685SDValue R600TargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
686 // On hw >= R700, COS/SIN input must be between -1. and 1.
687 // Thus we lower them to TRIG ( FRACT ( x / 2Pi + 0.5) - 0.5)
688 EVT VT = Op.getValueType();
689 SDValue Arg = Op.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000690 SDLoc DL(Op);
Sanjay Patela2607012015-09-16 16:31:21 +0000691
692 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000693 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
694 DAG.getNode(ISD::FADD, DL, VT,
695 DAG.getNode(ISD::FMUL, DL, VT, Arg,
696 DAG.getConstantFP(0.15915494309, DL, MVT::f32)),
697 DAG.getConstantFP(0.5, DL, MVT::f32)));
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000698 unsigned TrigNode;
699 switch (Op.getOpcode()) {
700 case ISD::FCOS:
701 TrigNode = AMDGPUISD::COS_HW;
702 break;
703 case ISD::FSIN:
704 TrigNode = AMDGPUISD::SIN_HW;
705 break;
706 default:
707 llvm_unreachable("Wrong trig opcode");
708 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000709 SDValue TrigVal = DAG.getNode(TrigNode, DL, VT,
710 DAG.getNode(ISD::FADD, DL, VT, FractPart,
711 DAG.getConstantFP(-0.5, DL, MVT::f32)));
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000712 if (Gen >= R600Subtarget::R700)
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000713 return TrigVal;
714 // On R600 hw, COS/SIN input must be between -Pi and Pi.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000715 return DAG.getNode(ISD::FMUL, DL, VT, TrigVal,
716 DAG.getConstantFP(3.14159265359, DL, MVT::f32));
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000717}
718
Jan Vesely25f36272014-06-18 12:27:13 +0000719SDValue R600TargetLowering::LowerSHLParts(SDValue Op, SelectionDAG &DAG) const {
720 SDLoc DL(Op);
721 EVT VT = Op.getValueType();
722
723 SDValue Lo = Op.getOperand(0);
724 SDValue Hi = Op.getOperand(1);
725 SDValue Shift = Op.getOperand(2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000726 SDValue Zero = DAG.getConstant(0, DL, VT);
727 SDValue One = DAG.getConstant(1, DL, VT);
Jan Vesely25f36272014-06-18 12:27:13 +0000728
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000729 SDValue Width = DAG.getConstant(VT.getSizeInBits(), DL, VT);
730 SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, DL, VT);
Jan Vesely25f36272014-06-18 12:27:13 +0000731 SDValue BigShift = DAG.getNode(ISD::SUB, DL, VT, Shift, Width);
732 SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift);
733
734 // The dance around Width1 is necessary for 0 special case.
735 // Without it the CompShift might be 32, producing incorrect results in
736 // Overflow. So we do the shift in two steps, the alternative is to
737 // add a conditional to filter the special case.
738
739 SDValue Overflow = DAG.getNode(ISD::SRL, DL, VT, Lo, CompShift);
740 Overflow = DAG.getNode(ISD::SRL, DL, VT, Overflow, One);
741
742 SDValue HiSmall = DAG.getNode(ISD::SHL, DL, VT, Hi, Shift);
743 HiSmall = DAG.getNode(ISD::OR, DL, VT, HiSmall, Overflow);
744 SDValue LoSmall = DAG.getNode(ISD::SHL, DL, VT, Lo, Shift);
745
746 SDValue HiBig = DAG.getNode(ISD::SHL, DL, VT, Lo, BigShift);
747 SDValue LoBig = Zero;
748
749 Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT);
750 Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT);
751
752 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi);
753}
754
Jan Vesely900ff2e2014-06-18 12:27:15 +0000755SDValue R600TargetLowering::LowerSRXParts(SDValue Op, SelectionDAG &DAG) const {
756 SDLoc DL(Op);
757 EVT VT = Op.getValueType();
758
759 SDValue Lo = Op.getOperand(0);
760 SDValue Hi = Op.getOperand(1);
761 SDValue Shift = Op.getOperand(2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000762 SDValue Zero = DAG.getConstant(0, DL, VT);
763 SDValue One = DAG.getConstant(1, DL, VT);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000764
Jan Veselyecf51332014-06-18 12:27:17 +0000765 const bool SRA = Op.getOpcode() == ISD::SRA_PARTS;
766
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000767 SDValue Width = DAG.getConstant(VT.getSizeInBits(), DL, VT);
768 SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, DL, VT);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000769 SDValue BigShift = DAG.getNode(ISD::SUB, DL, VT, Shift, Width);
770 SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift);
771
772 // The dance around Width1 is necessary for 0 special case.
773 // Without it the CompShift might be 32, producing incorrect results in
774 // Overflow. So we do the shift in two steps, the alternative is to
775 // add a conditional to filter the special case.
776
777 SDValue Overflow = DAG.getNode(ISD::SHL, DL, VT, Hi, CompShift);
778 Overflow = DAG.getNode(ISD::SHL, DL, VT, Overflow, One);
779
Jan Veselyecf51332014-06-18 12:27:17 +0000780 SDValue HiSmall = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, Shift);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000781 SDValue LoSmall = DAG.getNode(ISD::SRL, DL, VT, Lo, Shift);
782 LoSmall = DAG.getNode(ISD::OR, DL, VT, LoSmall, Overflow);
783
Jan Veselyecf51332014-06-18 12:27:17 +0000784 SDValue LoBig = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, BigShift);
785 SDValue HiBig = SRA ? DAG.getNode(ISD::SRA, DL, VT, Hi, Width1) : Zero;
Jan Vesely900ff2e2014-06-18 12:27:15 +0000786
787 Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT);
788 Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT);
789
790 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi);
791}
792
Jan Vesely808fff52015-04-30 17:15:56 +0000793SDValue R600TargetLowering::LowerUADDSUBO(SDValue Op, SelectionDAG &DAG,
794 unsigned mainop, unsigned ovf) const {
795 SDLoc DL(Op);
796 EVT VT = Op.getValueType();
797
798 SDValue Lo = Op.getOperand(0);
799 SDValue Hi = Op.getOperand(1);
800
801 SDValue OVF = DAG.getNode(ovf, DL, VT, Lo, Hi);
802 // Extend sign.
803 OVF = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, OVF,
804 DAG.getValueType(MVT::i1));
805
806 SDValue Res = DAG.getNode(mainop, DL, VT, Lo, Hi);
807
808 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT, VT), Res, OVF);
809}
810
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000811SDValue R600TargetLowering::lowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000812 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000813 return DAG.getNode(
814 ISD::SETCC,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000815 DL,
Tom Stellard75aadc22012-12-11 21:25:42 +0000816 MVT::i1,
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000817 Op, DAG.getConstantFP(1.0f, DL, MVT::f32),
818 DAG.getCondCode(ISD::SETEQ));
819}
820
821SDValue R600TargetLowering::lowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const {
822 SDLoc DL(Op);
823 return DAG.getNode(
824 ISD::SETCC,
825 DL,
826 MVT::i1,
827 Op, DAG.getConstantFP(-1.0f, DL, MVT::f32),
828 DAG.getCondCode(ISD::SETEQ));
Tom Stellard75aadc22012-12-11 21:25:42 +0000829}
830
Tom Stellard75aadc22012-12-11 21:25:42 +0000831SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000832 const SDLoc &DL,
Tom Stellard75aadc22012-12-11 21:25:42 +0000833 unsigned DwordOffset) const {
834 unsigned ByteOffset = DwordOffset * 4;
835 PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
Tom Stellard1e803092013-07-23 01:48:18 +0000836 AMDGPUAS::CONSTANT_BUFFER_0);
Tom Stellard75aadc22012-12-11 21:25:42 +0000837
838 // We shouldn't be using an offset wider than 16-bits for implicit parameters.
839 assert(isInt<16>(ByteOffset));
840
841 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000842 DAG.getConstant(ByteOffset, DL, MVT::i32), // PTR
Justin Lebar9c375812016-07-15 18:27:10 +0000843 MachinePointerInfo(ConstantPointerNull::get(PtrType)));
Tom Stellard75aadc22012-12-11 21:25:42 +0000844}
845
Tom Stellard75aadc22012-12-11 21:25:42 +0000846bool R600TargetLowering::isZero(SDValue Op) const {
847 if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) {
848 return Cst->isNullValue();
849 } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){
850 return CstFP->isZero();
851 } else {
852 return false;
853 }
854}
855
Matt Arsenault6b6a2c32016-03-11 08:00:27 +0000856bool R600TargetLowering::isHWTrueValue(SDValue Op) const {
857 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
858 return CFP->isExactlyValue(1.0);
859 }
860 return isAllOnesConstant(Op);
861}
862
863bool R600TargetLowering::isHWFalseValue(SDValue Op) const {
864 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
865 return CFP->getValueAPF().isZero();
866 }
867 return isNullConstant(Op);
868}
869
Tom Stellard75aadc22012-12-11 21:25:42 +0000870SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000871 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000872 EVT VT = Op.getValueType();
873
874 SDValue LHS = Op.getOperand(0);
875 SDValue RHS = Op.getOperand(1);
876 SDValue True = Op.getOperand(2);
877 SDValue False = Op.getOperand(3);
878 SDValue CC = Op.getOperand(4);
879 SDValue Temp;
880
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +0000881 if (VT == MVT::f32) {
882 DAGCombinerInfo DCI(DAG, AfterLegalizeVectorOps, true, nullptr);
883 SDValue MinMax = CombineFMinMaxLegacy(DL, VT, LHS, RHS, True, False, CC, DCI);
884 if (MinMax)
885 return MinMax;
886 }
887
Tom Stellard75aadc22012-12-11 21:25:42 +0000888 // LHS and RHS are guaranteed to be the same value type
889 EVT CompareVT = LHS.getValueType();
890
891 // Check if we can lower this to a native operation.
892
Tom Stellard2add82d2013-03-08 15:37:09 +0000893 // Try to lower to a SET* instruction:
894 //
895 // SET* can match the following patterns:
896 //
Tom Stellardcd428182013-09-28 02:50:38 +0000897 // select_cc f32, f32, -1, 0, cc_supported
898 // select_cc f32, f32, 1.0f, 0.0f, cc_supported
899 // select_cc i32, i32, -1, 0, cc_supported
Tom Stellard2add82d2013-03-08 15:37:09 +0000900 //
901
902 // Move hardware True/False values to the correct operand.
Tom Stellardcd428182013-09-28 02:50:38 +0000903 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
904 ISD::CondCode InverseCC =
905 ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
Tom Stellard5694d302013-09-28 02:50:43 +0000906 if (isHWTrueValue(False) && isHWFalseValue(True)) {
907 if (isCondCodeLegal(InverseCC, CompareVT.getSimpleVT())) {
908 std::swap(False, True);
909 CC = DAG.getCondCode(InverseCC);
910 } else {
911 ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InverseCC);
912 if (isCondCodeLegal(SwapInvCC, CompareVT.getSimpleVT())) {
913 std::swap(False, True);
914 std::swap(LHS, RHS);
915 CC = DAG.getCondCode(SwapInvCC);
916 }
917 }
Tom Stellard2add82d2013-03-08 15:37:09 +0000918 }
919
920 if (isHWTrueValue(True) && isHWFalseValue(False) &&
921 (CompareVT == VT || VT == MVT::i32)) {
922 // This can be matched by a SET* instruction.
923 return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
924 }
925
Tom Stellard75aadc22012-12-11 21:25:42 +0000926 // Try to lower to a CND* instruction:
Tom Stellard2add82d2013-03-08 15:37:09 +0000927 //
928 // CND* can match the following patterns:
929 //
Tom Stellardcd428182013-09-28 02:50:38 +0000930 // select_cc f32, 0.0, f32, f32, cc_supported
931 // select_cc f32, 0.0, i32, i32, cc_supported
932 // select_cc i32, 0, f32, f32, cc_supported
933 // select_cc i32, 0, i32, i32, cc_supported
Tom Stellard2add82d2013-03-08 15:37:09 +0000934 //
Tom Stellardcd428182013-09-28 02:50:38 +0000935
936 // Try to move the zero value to the RHS
937 if (isZero(LHS)) {
938 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
939 // Try swapping the operands
940 ISD::CondCode CCSwapped = ISD::getSetCCSwappedOperands(CCOpcode);
941 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
942 std::swap(LHS, RHS);
943 CC = DAG.getCondCode(CCSwapped);
944 } else {
945 // Try inverting the conditon and then swapping the operands
946 ISD::CondCode CCInv = ISD::getSetCCInverse(CCOpcode, CompareVT.isInteger());
947 CCSwapped = ISD::getSetCCSwappedOperands(CCInv);
948 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
949 std::swap(True, False);
950 std::swap(LHS, RHS);
951 CC = DAG.getCondCode(CCSwapped);
952 }
953 }
954 }
955 if (isZero(RHS)) {
956 SDValue Cond = LHS;
957 SDValue Zero = RHS;
Tom Stellard75aadc22012-12-11 21:25:42 +0000958 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
959 if (CompareVT != VT) {
960 // Bitcast True / False to the correct types. This will end up being
961 // a nop, but it allows us to define only a single pattern in the
962 // .TD files for each CND* instruction rather than having to have
963 // one pattern for integer True/False and one for fp True/False
964 True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
965 False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
966 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000967
968 switch (CCOpcode) {
969 case ISD::SETONE:
970 case ISD::SETUNE:
971 case ISD::SETNE:
Tom Stellard75aadc22012-12-11 21:25:42 +0000972 CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
973 Temp = True;
974 True = False;
975 False = Temp;
976 break;
977 default:
978 break;
979 }
980 SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
981 Cond, Zero,
982 True, False,
983 DAG.getCondCode(CCOpcode));
984 return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
985 }
986
Tom Stellard75aadc22012-12-11 21:25:42 +0000987 // If we make it this for it means we have no native instructions to handle
988 // this SELECT_CC, so we must lower it.
989 SDValue HWTrue, HWFalse;
990
991 if (CompareVT == MVT::f32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000992 HWTrue = DAG.getConstantFP(1.0f, DL, CompareVT);
993 HWFalse = DAG.getConstantFP(0.0f, DL, CompareVT);
Tom Stellard75aadc22012-12-11 21:25:42 +0000994 } else if (CompareVT == MVT::i32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000995 HWTrue = DAG.getConstant(-1, DL, CompareVT);
996 HWFalse = DAG.getConstant(0, DL, CompareVT);
Tom Stellard75aadc22012-12-11 21:25:42 +0000997 }
998 else {
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +0000999 llvm_unreachable("Unhandled value type in LowerSELECT_CC");
Tom Stellard75aadc22012-12-11 21:25:42 +00001000 }
1001
1002 // Lower this unsupported SELECT_CC into a combination of two supported
1003 // SELECT_CC operations.
1004 SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
1005
1006 return DAG.getNode(ISD::SELECT_CC, DL, VT,
1007 Cond, HWFalse,
1008 True, False,
1009 DAG.getCondCode(ISD::SETNE));
1010}
1011
Alp Tokercb402912014-01-24 17:20:08 +00001012/// LLVM generates byte-addressed pointers. For indirect addressing, we need to
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001013/// convert these pointers to a register index. Each register holds
1014/// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
1015/// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
1016/// for indirect addressing.
1017SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
1018 unsigned StackWidth,
1019 SelectionDAG &DAG) const {
1020 unsigned SRLPad;
1021 switch(StackWidth) {
1022 case 1:
1023 SRLPad = 2;
1024 break;
1025 case 2:
1026 SRLPad = 3;
1027 break;
1028 case 4:
1029 SRLPad = 4;
1030 break;
1031 default: llvm_unreachable("Invalid stack width");
1032 }
1033
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001034 SDLoc DL(Ptr);
1035 return DAG.getNode(ISD::SRL, DL, Ptr.getValueType(), Ptr,
1036 DAG.getConstant(SRLPad, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001037}
1038
1039void R600TargetLowering::getStackAddress(unsigned StackWidth,
1040 unsigned ElemIdx,
1041 unsigned &Channel,
1042 unsigned &PtrIncr) const {
1043 switch (StackWidth) {
1044 default:
1045 case 1:
1046 Channel = 0;
1047 if (ElemIdx > 0) {
1048 PtrIncr = 1;
1049 } else {
1050 PtrIncr = 0;
1051 }
1052 break;
1053 case 2:
1054 Channel = ElemIdx % 2;
1055 if (ElemIdx == 2) {
1056 PtrIncr = 1;
1057 } else {
1058 PtrIncr = 0;
1059 }
1060 break;
1061 case 4:
1062 Channel = ElemIdx;
1063 PtrIncr = 0;
1064 break;
1065 }
1066}
1067
Matt Arsenault95245662016-02-11 05:32:46 +00001068SDValue R600TargetLowering::lowerPrivateTruncStore(StoreSDNode *Store,
1069 SelectionDAG &DAG) const {
1070 SDLoc DL(Store);
Tom Stellard75aadc22012-12-11 21:25:42 +00001071
Matt Arsenault95245662016-02-11 05:32:46 +00001072 unsigned Mask = 0;
1073 if (Store->getMemoryVT() == MVT::i8) {
1074 Mask = 0xff;
1075 } else if (Store->getMemoryVT() == MVT::i16) {
1076 Mask = 0xffff;
1077 }
1078
1079 SDValue Chain = Store->getChain();
1080 SDValue BasePtr = Store->getBasePtr();
1081 EVT MemVT = Store->getMemoryVT();
1082
1083 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, BasePtr,
1084 DAG.getConstant(2, DL, MVT::i32));
1085 SDValue Dst = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
1086 Chain, Ptr,
1087 DAG.getTargetConstant(0, DL, MVT::i32));
1088
1089 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, BasePtr,
1090 DAG.getConstant(0x3, DL, MVT::i32));
1091
1092 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1093 DAG.getConstant(3, DL, MVT::i32));
1094
1095 SDValue SExtValue = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i32,
1096 Store->getValue());
1097
1098 SDValue MaskedValue = DAG.getZeroExtendInReg(SExtValue, DL, MemVT);
1099
1100 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
1101 MaskedValue, ShiftAmt);
1102
1103 SDValue DstMask = DAG.getNode(ISD::SHL, DL, MVT::i32,
1104 DAG.getConstant(Mask, DL, MVT::i32),
1105 ShiftAmt);
1106 DstMask = DAG.getNode(ISD::XOR, DL, MVT::i32, DstMask,
1107 DAG.getConstant(0xffffffff, DL, MVT::i32));
1108 Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
1109
1110 SDValue Value = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
1111 return DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1112 Chain, Value, Ptr,
1113 DAG.getTargetConstant(0, DL, MVT::i32));
1114}
1115
1116SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1117 if (SDValue Result = AMDGPUTargetLowering::MergeVectorStore(Op, DAG))
Tom Stellardfbab8272013-08-16 01:12:11 +00001118 return Result;
Tom Stellardfbab8272013-08-16 01:12:11 +00001119
Matt Arsenault95245662016-02-11 05:32:46 +00001120 StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
1121 unsigned AS = StoreNode->getAddressSpace();
1122 SDValue Value = StoreNode->getValue();
1123 EVT ValueVT = Value.getValueType();
1124
1125 if ((AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::PRIVATE_ADDRESS) &&
1126 ValueVT.isVector()) {
1127 return SplitVectorStore(Op, DAG);
1128 }
1129
1130 SDLoc DL(Op);
1131 SDValue Chain = StoreNode->getChain();
1132 SDValue Ptr = StoreNode->getBasePtr();
1133
1134 if (AS == AMDGPUAS::GLOBAL_ADDRESS) {
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001135 if (StoreNode->isTruncatingStore()) {
1136 EVT VT = Value.getValueType();
Tom Stellardfbab8272013-08-16 01:12:11 +00001137 assert(VT.bitsLE(MVT::i32));
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001138 EVT MemVT = StoreNode->getMemoryVT();
1139 SDValue MaskConstant;
1140 if (MemVT == MVT::i8) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001141 MaskConstant = DAG.getConstant(0xFF, DL, MVT::i32);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001142 } else {
1143 assert(MemVT == MVT::i16);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001144 MaskConstant = DAG.getConstant(0xFFFF, DL, MVT::i32);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001145 }
1146 SDValue DWordAddr = DAG.getNode(ISD::SRL, DL, VT, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001147 DAG.getConstant(2, DL, MVT::i32));
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001148 SDValue ByteIndex = DAG.getNode(ISD::AND, DL, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001149 DAG.getConstant(0x00000003, DL, VT));
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001150 SDValue TruncValue = DAG.getNode(ISD::AND, DL, VT, Value, MaskConstant);
1151 SDValue Shift = DAG.getNode(ISD::SHL, DL, VT, ByteIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001152 DAG.getConstant(3, DL, VT));
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001153 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, VT, TruncValue, Shift);
1154 SDValue Mask = DAG.getNode(ISD::SHL, DL, VT, MaskConstant, Shift);
1155 // XXX: If we add a 64-bit ZW register class, then we could use a 2 x i32
1156 // vector instead.
1157 SDValue Src[4] = {
1158 ShiftedValue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001159 DAG.getConstant(0, DL, MVT::i32),
1160 DAG.getConstant(0, DL, MVT::i32),
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001161 Mask
1162 };
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001163 SDValue Input = DAG.getBuildVector(MVT::v4i32, DL, Src);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001164 SDValue Args[3] = { Chain, Input, DWordAddr };
1165 return DAG.getMemIntrinsicNode(AMDGPUISD::STORE_MSKOR, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00001166 Op->getVTList(), Args, MemVT,
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001167 StoreNode->getMemOperand());
1168 } else if (Ptr->getOpcode() != AMDGPUISD::DWORDADDR &&
Matt Arsenault95245662016-02-11 05:32:46 +00001169 ValueVT.bitsGE(MVT::i32)) {
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001170 // Convert pointer from byte address to dword address.
1171 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, Ptr.getValueType(),
1172 DAG.getNode(ISD::SRL, DL, Ptr.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001173 Ptr, DAG.getConstant(2, DL, MVT::i32)));
Tom Stellard75aadc22012-12-11 21:25:42 +00001174
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001175 if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +00001176 llvm_unreachable("Truncated and indexed stores not supported yet");
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001177 } else {
1178 Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
1179 }
1180 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001181 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001182 }
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001183
Matt Arsenault95245662016-02-11 05:32:46 +00001184 if (AS != AMDGPUAS::PRIVATE_ADDRESS)
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001185 return SDValue();
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001186
Matt Arsenault95245662016-02-11 05:32:46 +00001187 EVT MemVT = StoreNode->getMemoryVT();
1188 if (MemVT.bitsLT(MVT::i32))
1189 return lowerPrivateTruncStore(StoreNode, DAG);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001190
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00001191 // Lowering for indirect addressing
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001192 const MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001193 const R600FrameLowering *TFL = getSubtarget()->getFrameLowering();
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001194 unsigned StackWidth = TFL->getStackWidth(MF);
1195
1196 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1197
1198 if (ValueVT.isVector()) {
1199 unsigned NumElemVT = ValueVT.getVectorNumElements();
1200 EVT ElemVT = ValueVT.getVectorElementType();
Craig Topper48d114b2014-04-26 18:35:24 +00001201 SmallVector<SDValue, 4> Stores(NumElemVT);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001202
1203 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1204 "vector width in load");
1205
1206 for (unsigned i = 0; i < NumElemVT; ++i) {
1207 unsigned Channel, PtrIncr;
1208 getStackAddress(StackWidth, i, Channel, PtrIncr);
1209 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001210 DAG.getConstant(PtrIncr, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001211 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001212 Value, DAG.getConstant(i, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001213
1214 Stores[i] = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1215 Chain, Elem, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001216 DAG.getTargetConstant(Channel, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001217 }
Craig Topper48d114b2014-04-26 18:35:24 +00001218 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Stores);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001219 } else {
1220 if (ValueVT == MVT::i8) {
1221 Value = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Value);
1222 }
1223 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other, Chain, Value, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001224 DAG.getTargetConstant(0, DL, MVT::i32)); // Channel
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001225 }
1226
1227 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001228}
1229
Tom Stellard365366f2013-01-23 02:09:06 +00001230// return (512 + (kc_bank << 12)
1231static int
1232ConstantAddressBlock(unsigned AddressSpace) {
1233 switch (AddressSpace) {
1234 case AMDGPUAS::CONSTANT_BUFFER_0:
1235 return 512;
1236 case AMDGPUAS::CONSTANT_BUFFER_1:
1237 return 512 + 4096;
1238 case AMDGPUAS::CONSTANT_BUFFER_2:
1239 return 512 + 4096 * 2;
1240 case AMDGPUAS::CONSTANT_BUFFER_3:
1241 return 512 + 4096 * 3;
1242 case AMDGPUAS::CONSTANT_BUFFER_4:
1243 return 512 + 4096 * 4;
1244 case AMDGPUAS::CONSTANT_BUFFER_5:
1245 return 512 + 4096 * 5;
1246 case AMDGPUAS::CONSTANT_BUFFER_6:
1247 return 512 + 4096 * 6;
1248 case AMDGPUAS::CONSTANT_BUFFER_7:
1249 return 512 + 4096 * 7;
1250 case AMDGPUAS::CONSTANT_BUFFER_8:
1251 return 512 + 4096 * 8;
1252 case AMDGPUAS::CONSTANT_BUFFER_9:
1253 return 512 + 4096 * 9;
1254 case AMDGPUAS::CONSTANT_BUFFER_10:
1255 return 512 + 4096 * 10;
1256 case AMDGPUAS::CONSTANT_BUFFER_11:
1257 return 512 + 4096 * 11;
1258 case AMDGPUAS::CONSTANT_BUFFER_12:
1259 return 512 + 4096 * 12;
1260 case AMDGPUAS::CONSTANT_BUFFER_13:
1261 return 512 + 4096 * 13;
1262 case AMDGPUAS::CONSTANT_BUFFER_14:
1263 return 512 + 4096 * 14;
1264 case AMDGPUAS::CONSTANT_BUFFER_15:
1265 return 512 + 4096 * 15;
1266 default:
1267 return -1;
1268 }
1269}
1270
Matt Arsenault6dfda962016-02-10 18:21:39 +00001271SDValue R600TargetLowering::lowerPrivateExtLoad(SDValue Op,
1272 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001273 SDLoc DL(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001274 LoadSDNode *Load = cast<LoadSDNode>(Op);
1275 ISD::LoadExtType ExtType = Load->getExtensionType();
1276 EVT MemVT = Load->getMemoryVT();
Tom Stellard365366f2013-01-23 02:09:06 +00001277
Matt Arsenault6dfda962016-02-10 18:21:39 +00001278 // <SI && AS=PRIVATE && EXTLOAD && size < 32bit,
1279 // register (2-)byte extract.
1280
1281 // Get Register holding the target.
1282 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Load->getBasePtr(),
1283 DAG.getConstant(2, DL, MVT::i32));
1284 // Load the Register.
1285 SDValue Ret = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, Op.getValueType(),
1286 Load->getChain(),
1287 Ptr,
1288 DAG.getTargetConstant(0, DL, MVT::i32),
1289 Op.getOperand(2));
1290
1291 // Get offset within the register.
1292 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32,
1293 Load->getBasePtr(),
1294 DAG.getConstant(0x3, DL, MVT::i32));
1295
1296 // Bit offset of target byte (byteIdx * 8).
1297 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1298 DAG.getConstant(3, DL, MVT::i32));
1299
1300 // Shift to the right.
1301 Ret = DAG.getNode(ISD::SRL, DL, MVT::i32, Ret, ShiftAmt);
1302
1303 // Eliminate the upper bits by setting them to ...
1304 EVT MemEltVT = MemVT.getScalarType();
1305
1306 // ... ones.
1307 if (ExtType == ISD::SEXTLOAD) {
1308 SDValue MemEltVTNode = DAG.getValueType(MemEltVT);
1309
1310 SDValue Ops[] = {
1311 DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, Ret, MemEltVTNode),
1312 Load->getChain()
1313 };
1314
1315 return DAG.getMergeValues(Ops, DL);
1316 }
1317
1318 // ... or zeros.
1319 SDValue Ops[] = {
1320 DAG.getZeroExtendInReg(Ret, DL, MemEltVT),
1321 Load->getChain()
1322 };
1323
1324 return DAG.getMergeValues(Ops, DL);
1325}
1326
1327SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1328 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1329 unsigned AS = LoadNode->getAddressSpace();
1330 EVT MemVT = LoadNode->getMemoryVT();
1331 ISD::LoadExtType ExtType = LoadNode->getExtensionType();
1332
1333 if (AS == AMDGPUAS::PRIVATE_ADDRESS &&
1334 ExtType != ISD::NON_EXTLOAD && MemVT.bitsLT(MVT::i32)) {
1335 return lowerPrivateExtLoad(Op, DAG);
1336 }
1337
1338 SDLoc DL(Op);
1339 EVT VT = Op.getValueType();
1340 SDValue Chain = LoadNode->getChain();
1341 SDValue Ptr = LoadNode->getBasePtr();
Tom Stellarde9373602014-01-22 19:24:14 +00001342
Tom Stellard35bb18c2013-08-26 15:06:04 +00001343 if (LoadNode->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS && VT.isVector()) {
1344 SDValue MergedValues[2] = {
Matt Arsenault9c499c32016-04-14 23:31:26 +00001345 scalarizeVectorLoad(LoadNode, DAG),
Tom Stellard35bb18c2013-08-26 15:06:04 +00001346 Chain
1347 };
Craig Topper64941d92014-04-27 19:20:57 +00001348 return DAG.getMergeValues(MergedValues, DL);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001349 }
1350
Tom Stellard365366f2013-01-23 02:09:06 +00001351 int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
Matt Arsenault00a0d6f2013-11-13 02:39:07 +00001352 if (ConstantBlock > -1 &&
1353 ((LoadNode->getExtensionType() == ISD::NON_EXTLOAD) ||
1354 (LoadNode->getExtensionType() == ISD::ZEXTLOAD))) {
Tom Stellard365366f2013-01-23 02:09:06 +00001355 SDValue Result;
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001356 if (isa<ConstantExpr>(LoadNode->getMemOperand()->getValue()) ||
1357 isa<Constant>(LoadNode->getMemOperand()->getValue()) ||
Matt Arsenaultef1a9502013-11-01 17:39:26 +00001358 isa<ConstantSDNode>(Ptr)) {
Tom Stellard365366f2013-01-23 02:09:06 +00001359 SDValue Slots[4];
1360 for (unsigned i = 0; i < 4; i++) {
1361 // We want Const position encoded with the following formula :
1362 // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
1363 // const_index is Ptr computed by llvm using an alignment of 16.
1364 // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
1365 // then div by 4 at the ISel step
1366 SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001367 DAG.getConstant(4 * i + ConstantBlock * 16, DL, MVT::i32));
Tom Stellard365366f2013-01-23 02:09:06 +00001368 Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
1369 }
Tom Stellard0344cdf2013-08-01 15:23:42 +00001370 EVT NewVT = MVT::v4i32;
1371 unsigned NumElements = 4;
1372 if (VT.isVector()) {
1373 NewVT = VT;
1374 NumElements = VT.getVectorNumElements();
1375 }
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001376 Result = DAG.getBuildVector(NewVT, DL, makeArrayRef(Slots, NumElements));
Tom Stellard365366f2013-01-23 02:09:06 +00001377 } else {
Alp Tokerf907b892013-12-05 05:44:44 +00001378 // non-constant ptr can't be folded, keeps it as a v4f32 load
Tom Stellard365366f2013-01-23 02:09:06 +00001379 Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001380 DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr,
1381 DAG.getConstant(4, DL, MVT::i32)),
1382 DAG.getConstant(LoadNode->getAddressSpace() -
1383 AMDGPUAS::CONSTANT_BUFFER_0, DL, MVT::i32)
Tom Stellard365366f2013-01-23 02:09:06 +00001384 );
1385 }
1386
1387 if (!VT.isVector()) {
1388 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001389 DAG.getConstant(0, DL, MVT::i32));
Tom Stellard365366f2013-01-23 02:09:06 +00001390 }
1391
1392 SDValue MergedValues[2] = {
Matt Arsenault7939acd2014-04-07 16:44:24 +00001393 Result,
1394 Chain
Tom Stellard365366f2013-01-23 02:09:06 +00001395 };
Craig Topper64941d92014-04-27 19:20:57 +00001396 return DAG.getMergeValues(MergedValues, DL);
Tom Stellard365366f2013-01-23 02:09:06 +00001397 }
1398
Matt Arsenault6dfda962016-02-10 18:21:39 +00001399 SDValue LoweredLoad;
1400
Matt Arsenault909d0c02013-10-30 23:43:29 +00001401 // For most operations returning SDValue() will result in the node being
1402 // expanded by the DAG Legalizer. This is not the case for ISD::LOAD, so we
1403 // need to manually expand loads that may be legal in some address spaces and
1404 // illegal in others. SEXT loads from CONSTANT_BUFFER_0 are supported for
1405 // compute shaders, since the data is sign extended when it is uploaded to the
1406 // buffer. However SEXT loads from other address spaces are not supported, so
1407 // we need to expand them here.
Tom Stellard84021442013-07-23 01:48:24 +00001408 if (LoadNode->getExtensionType() == ISD::SEXTLOAD) {
1409 EVT MemVT = LoadNode->getMemoryVT();
1410 assert(!MemVT.isVector() && (MemVT == MVT::i16 || MemVT == MVT::i8));
Justin Lebar9c375812016-07-15 18:27:10 +00001411 SDValue NewLoad = DAG.getExtLoad(
1412 ISD::EXTLOAD, DL, VT, Chain, Ptr, LoadNode->getPointerInfo(), MemVT,
1413 LoadNode->getAlignment(), LoadNode->getMemOperand()->getFlags());
Jan Veselyb670d372015-05-26 18:07:22 +00001414 SDValue Res = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, NewLoad,
1415 DAG.getValueType(MemVT));
Tom Stellard84021442013-07-23 01:48:24 +00001416
Jan Veselyb670d372015-05-26 18:07:22 +00001417 SDValue MergedValues[2] = { Res, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00001418 return DAG.getMergeValues(MergedValues, DL);
Tom Stellard84021442013-07-23 01:48:24 +00001419 }
1420
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001421 if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1422 return SDValue();
1423 }
1424
1425 // Lowering for indirect addressing
1426 const MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001427 const R600FrameLowering *TFL = getSubtarget()->getFrameLowering();
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001428 unsigned StackWidth = TFL->getStackWidth(MF);
1429
1430 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1431
1432 if (VT.isVector()) {
1433 unsigned NumElemVT = VT.getVectorNumElements();
1434 EVT ElemVT = VT.getVectorElementType();
1435 SDValue Loads[4];
1436
Jan Vesely687ca8d2016-05-16 23:56:32 +00001437 assert(NumElemVT <= 4);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001438 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1439 "vector width in load");
1440
1441 for (unsigned i = 0; i < NumElemVT; ++i) {
1442 unsigned Channel, PtrIncr;
1443 getStackAddress(StackWidth, i, Channel, PtrIncr);
1444 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001445 DAG.getConstant(PtrIncr, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001446 Loads[i] = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, ElemVT,
1447 Chain, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001448 DAG.getTargetConstant(Channel, DL, MVT::i32),
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001449 Op.getOperand(2));
1450 }
Jan Vesely687ca8d2016-05-16 23:56:32 +00001451 EVT TargetVT = EVT::getVectorVT(*DAG.getContext(), ElemVT, NumElemVT);
1452 LoweredLoad = DAG.getBuildVector(TargetVT, DL, makeArrayRef(Loads, NumElemVT));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001453 } else {
1454 LoweredLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, VT,
1455 Chain, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001456 DAG.getTargetConstant(0, DL, MVT::i32), // Channel
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001457 Op.getOperand(2));
1458 }
1459
Matt Arsenault7939acd2014-04-07 16:44:24 +00001460 SDValue Ops[2] = {
1461 LoweredLoad,
1462 Chain
1463 };
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001464
Craig Topper64941d92014-04-27 19:20:57 +00001465 return DAG.getMergeValues(Ops, DL);
Tom Stellard365366f2013-01-23 02:09:06 +00001466}
Tom Stellard75aadc22012-12-11 21:25:42 +00001467
Matt Arsenault1d555c42014-06-23 18:00:55 +00001468SDValue R600TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
1469 SDValue Chain = Op.getOperand(0);
1470 SDValue Cond = Op.getOperand(1);
1471 SDValue Jump = Op.getOperand(2);
1472
1473 return DAG.getNode(AMDGPUISD::BRANCH_COND, SDLoc(Op), Op.getValueType(),
1474 Chain, Jump, Cond);
1475}
1476
Matt Arsenault81d06012016-03-07 21:10:13 +00001477SDValue R600TargetLowering::lowerFrameIndex(SDValue Op,
1478 SelectionDAG &DAG) const {
1479 MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001480 const R600FrameLowering *TFL = getSubtarget()->getFrameLowering();
Matt Arsenault81d06012016-03-07 21:10:13 +00001481
1482 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op);
1483
1484 unsigned FrameIndex = FIN->getIndex();
1485 unsigned IgnoredFrameReg;
1486 unsigned Offset =
1487 TFL->getFrameIndexReference(MF, FrameIndex, IgnoredFrameReg);
1488 return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF), SDLoc(Op),
1489 Op.getValueType());
1490}
1491
Tom Stellard75aadc22012-12-11 21:25:42 +00001492/// XXX Only kernel functions are supported, so we can assume for now that
1493/// every function is a kernel function, but in the future we should use
1494/// separate calling conventions for kernel and non-kernel functions.
1495SDValue R600TargetLowering::LowerFormalArguments(
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001496 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1497 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
1498 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Tom Stellardacfeebf2013-07-23 01:48:05 +00001499 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001500 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1501 *DAG.getContext());
Vincent Lejeunef143af32013-11-11 22:10:24 +00001502 MachineFunction &MF = DAG.getMachineFunction();
Jan Veselye5121f32014-10-14 20:05:26 +00001503 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
Tom Stellardacfeebf2013-07-23 01:48:05 +00001504
Tom Stellardaf775432013-10-23 00:44:32 +00001505 SmallVector<ISD::InputArg, 8> LocalIns;
1506
Matt Arsenault209a7b92014-04-18 07:40:20 +00001507 getOriginalFunctionArgs(DAG, MF.getFunction(), Ins, LocalIns);
Tom Stellardaf775432013-10-23 00:44:32 +00001508
1509 AnalyzeFormalArguments(CCInfo, LocalIns);
Tom Stellardacfeebf2013-07-23 01:48:05 +00001510
Tom Stellard1e803092013-07-23 01:48:18 +00001511 for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
Tom Stellardacfeebf2013-07-23 01:48:05 +00001512 CCValAssign &VA = ArgLocs[i];
Matt Arsenault74ef2772014-08-13 18:14:11 +00001513 const ISD::InputArg &In = Ins[i];
1514 EVT VT = In.VT;
1515 EVT MemVT = VA.getLocVT();
1516 if (!VT.isVector() && MemVT.isVector()) {
1517 // Get load source type if scalarized.
1518 MemVT = MemVT.getVectorElementType();
1519 }
Tom Stellard78e01292013-07-23 01:47:58 +00001520
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001521 if (AMDGPU::isShader(CallConv)) {
Vincent Lejeunef143af32013-11-11 22:10:24 +00001522 unsigned Reg = MF.addLiveIn(VA.getLocReg(), &AMDGPU::R600_Reg128RegClass);
1523 SDValue Register = DAG.getCopyFromReg(Chain, DL, Reg, VT);
1524 InVals.push_back(Register);
1525 continue;
1526 }
1527
Tom Stellard75aadc22012-12-11 21:25:42 +00001528 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
Matt Arsenault74ef2772014-08-13 18:14:11 +00001529 AMDGPUAS::CONSTANT_BUFFER_0);
Tom Stellardacfeebf2013-07-23 01:48:05 +00001530
Matt Arsenaultfae02982014-03-17 18:58:11 +00001531 // i64 isn't a legal type, so the register type used ends up as i32, which
1532 // isn't expected here. It attempts to create this sextload, but it ends up
1533 // being invalid. Somehow this seems to work with i64 arguments, but breaks
1534 // for <1 x i64>.
1535
Tom Stellardacfeebf2013-07-23 01:48:05 +00001536 // The first 36 bytes of the input buffer contains information about
1537 // thread group and global sizes.
Matt Arsenault74ef2772014-08-13 18:14:11 +00001538 ISD::LoadExtType Ext = ISD::NON_EXTLOAD;
1539 if (MemVT.getScalarSizeInBits() != VT.getScalarSizeInBits()) {
1540 // FIXME: This should really check the extload type, but the handling of
1541 // extload vector parameters seems to be broken.
Matt Arsenaulte1f030c2014-04-11 20:59:54 +00001542
Matt Arsenault74ef2772014-08-13 18:14:11 +00001543 // Ext = In.Flags.isSExt() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
1544 Ext = ISD::SEXTLOAD;
1545 }
1546
1547 // Compute the offset from the value.
1548 // XXX - I think PartOffset should give you this, but it seems to give the
1549 // size of the register which isn't useful.
1550
Andrew Trick05938a52015-02-16 18:10:47 +00001551 unsigned ValBase = ArgLocs[In.getOrigArgIndex()].getLocMemOffset();
Matt Arsenault74ef2772014-08-13 18:14:11 +00001552 unsigned PartOffset = VA.getLocMemOffset();
Matt Arsenault52ef4012016-07-26 16:45:58 +00001553 unsigned Offset = Subtarget->getExplicitKernelArgOffset() + VA.getLocMemOffset();
Matt Arsenault74ef2772014-08-13 18:14:11 +00001554
1555 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy), PartOffset - ValBase);
Justin Lebar9c375812016-07-15 18:27:10 +00001556 SDValue Arg = DAG.getLoad(
1557 ISD::UNINDEXED, Ext, VT, DL, Chain,
1558 DAG.getConstant(Offset, DL, MVT::i32), DAG.getUNDEF(MVT::i32), PtrInfo,
1559 MemVT, /* Alignment = */ 4,
1560 MachineMemOperand::MONonTemporal | MachineMemOperand::MOInvariant);
Matt Arsenault209a7b92014-04-18 07:40:20 +00001561
1562 // 4 is the preferred alignment for the CONSTANT memory space.
Tom Stellard75aadc22012-12-11 21:25:42 +00001563 InVals.push_back(Arg);
Matt Arsenault52ef4012016-07-26 16:45:58 +00001564 MFI->setABIArgOffset(Offset + MemVT.getStoreSize());
Tom Stellard75aadc22012-12-11 21:25:42 +00001565 }
1566 return Chain;
1567}
1568
Mehdi Amini44ede332015-07-09 02:09:04 +00001569EVT R600TargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1570 EVT VT) const {
Matt Arsenault209a7b92014-04-18 07:40:20 +00001571 if (!VT.isVector())
1572 return MVT::i32;
Tom Stellard75aadc22012-12-11 21:25:42 +00001573 return VT.changeVectorElementTypeToInteger();
1574}
1575
Matt Arsenaultfa67bdb2016-02-22 21:04:16 +00001576bool R600TargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
1577 unsigned AddrSpace,
1578 unsigned Align,
1579 bool *IsFast) const {
1580 if (IsFast)
1581 *IsFast = false;
1582
1583 if (!VT.isSimple() || VT == MVT::Other)
1584 return false;
1585
1586 if (VT.bitsLT(MVT::i32))
1587 return false;
1588
1589 // TODO: This is a rough estimate.
1590 if (IsFast)
1591 *IsFast = true;
1592
1593 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
1594}
1595
Matt Arsenault209a7b92014-04-18 07:40:20 +00001596static SDValue CompactSwizzlableVector(
1597 SelectionDAG &DAG, SDValue VectorEntry,
1598 DenseMap<unsigned, unsigned> &RemapSwizzle) {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001599 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1600 assert(RemapSwizzle.empty());
1601 SDValue NewBldVec[4] = {
Matt Arsenault209a7b92014-04-18 07:40:20 +00001602 VectorEntry.getOperand(0),
1603 VectorEntry.getOperand(1),
1604 VectorEntry.getOperand(2),
1605 VectorEntry.getOperand(3)
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001606 };
1607
1608 for (unsigned i = 0; i < 4; i++) {
Sanjay Patel57195842016-03-14 17:28:46 +00001609 if (NewBldVec[i].isUndef())
Vincent Lejeunefa58a5f2013-10-13 17:56:10 +00001610 // We mask write here to teach later passes that the ith element of this
1611 // vector is undef. Thus we can use it to reduce 128 bits reg usage,
1612 // break false dependencies and additionnaly make assembly easier to read.
1613 RemapSwizzle[i] = 7; // SEL_MASK_WRITE
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001614 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(NewBldVec[i])) {
1615 if (C->isZero()) {
1616 RemapSwizzle[i] = 4; // SEL_0
1617 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1618 } else if (C->isExactlyValue(1.0)) {
1619 RemapSwizzle[i] = 5; // SEL_1
1620 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1621 }
1622 }
1623
Sanjay Patel57195842016-03-14 17:28:46 +00001624 if (NewBldVec[i].isUndef())
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001625 continue;
1626 for (unsigned j = 0; j < i; j++) {
1627 if (NewBldVec[i] == NewBldVec[j]) {
1628 NewBldVec[i] = DAG.getUNDEF(NewBldVec[i].getValueType());
1629 RemapSwizzle[i] = j;
1630 break;
1631 }
1632 }
1633 }
1634
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001635 return DAG.getBuildVector(VectorEntry.getValueType(), SDLoc(VectorEntry),
1636 NewBldVec);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001637}
1638
Benjamin Kramer193960c2013-06-11 13:32:25 +00001639static SDValue ReorganizeVector(SelectionDAG &DAG, SDValue VectorEntry,
1640 DenseMap<unsigned, unsigned> &RemapSwizzle) {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001641 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1642 assert(RemapSwizzle.empty());
1643 SDValue NewBldVec[4] = {
1644 VectorEntry.getOperand(0),
1645 VectorEntry.getOperand(1),
1646 VectorEntry.getOperand(2),
1647 VectorEntry.getOperand(3)
1648 };
1649 bool isUnmovable[4] = { false, false, false, false };
Vincent Lejeunecc0ea742013-12-10 14:43:31 +00001650 for (unsigned i = 0; i < 4; i++) {
Vincent Lejeuneb8aac8d2013-07-09 15:03:25 +00001651 RemapSwizzle[i] = i;
Vincent Lejeunecc0ea742013-12-10 14:43:31 +00001652 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1653 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1654 ->getZExtValue();
1655 if (i == Idx)
1656 isUnmovable[Idx] = true;
1657 }
1658 }
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001659
1660 for (unsigned i = 0; i < 4; i++) {
1661 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1662 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1663 ->getZExtValue();
Vincent Lejeune301beb82013-10-13 17:56:04 +00001664 if (isUnmovable[Idx])
1665 continue;
1666 // Swap i and Idx
1667 std::swap(NewBldVec[Idx], NewBldVec[i]);
1668 std::swap(RemapSwizzle[i], RemapSwizzle[Idx]);
1669 break;
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001670 }
1671 }
1672
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001673 return DAG.getBuildVector(VectorEntry.getValueType(), SDLoc(VectorEntry),
1674 NewBldVec);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001675}
1676
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001677SDValue R600TargetLowering::OptimizeSwizzle(SDValue BuildVector, SDValue Swz[4],
1678 SelectionDAG &DAG,
1679 const SDLoc &DL) const {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001680 assert(BuildVector.getOpcode() == ISD::BUILD_VECTOR);
1681 // Old -> New swizzle values
1682 DenseMap<unsigned, unsigned> SwizzleRemap;
1683
1684 BuildVector = CompactSwizzlableVector(DAG, BuildVector, SwizzleRemap);
1685 for (unsigned i = 0; i < 4; i++) {
Benjamin Kramer619c4e52015-04-10 11:24:51 +00001686 unsigned Idx = cast<ConstantSDNode>(Swz[i])->getZExtValue();
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001687 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001688 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], DL, MVT::i32);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001689 }
1690
1691 SwizzleRemap.clear();
1692 BuildVector = ReorganizeVector(DAG, BuildVector, SwizzleRemap);
1693 for (unsigned i = 0; i < 4; i++) {
Benjamin Kramer619c4e52015-04-10 11:24:51 +00001694 unsigned Idx = cast<ConstantSDNode>(Swz[i])->getZExtValue();
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001695 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001696 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], DL, MVT::i32);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001697 }
1698
1699 return BuildVector;
1700}
1701
1702
Tom Stellard75aadc22012-12-11 21:25:42 +00001703//===----------------------------------------------------------------------===//
1704// Custom DAG Optimizations
1705//===----------------------------------------------------------------------===//
1706
1707SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
1708 DAGCombinerInfo &DCI) const {
1709 SelectionDAG &DAG = DCI.DAG;
1710
1711 switch (N->getOpcode()) {
Tom Stellard50122a52014-04-07 19:45:41 +00001712 default: return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001713 // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
1714 case ISD::FP_ROUND: {
1715 SDValue Arg = N->getOperand(0);
1716 if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001717 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), N->getValueType(0),
Tom Stellard75aadc22012-12-11 21:25:42 +00001718 Arg.getOperand(0));
1719 }
1720 break;
1721 }
Tom Stellarde06163a2013-02-07 14:02:35 +00001722
1723 // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
1724 // (i32 select_cc f32, f32, -1, 0 cc)
1725 //
1726 // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1727 // this to one of the SET*_DX10 instructions.
1728 case ISD::FP_TO_SINT: {
1729 SDValue FNeg = N->getOperand(0);
1730 if (FNeg.getOpcode() != ISD::FNEG) {
1731 return SDValue();
1732 }
1733 SDValue SelectCC = FNeg.getOperand(0);
1734 if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1735 SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1736 SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1737 !isHWTrueValue(SelectCC.getOperand(2)) ||
1738 !isHWFalseValue(SelectCC.getOperand(3))) {
1739 return SDValue();
1740 }
1741
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001742 SDLoc dl(N);
1743 return DAG.getNode(ISD::SELECT_CC, dl, N->getValueType(0),
Tom Stellarde06163a2013-02-07 14:02:35 +00001744 SelectCC.getOperand(0), // LHS
1745 SelectCC.getOperand(1), // RHS
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001746 DAG.getConstant(-1, dl, MVT::i32), // True
1747 DAG.getConstant(0, dl, MVT::i32), // False
Tom Stellarde06163a2013-02-07 14:02:35 +00001748 SelectCC.getOperand(4)); // CC
1749
1750 break;
1751 }
Quentin Colombete2e05482013-07-30 00:27:16 +00001752
NAKAMURA Takumi8a046432013-10-28 04:07:38 +00001753 // insert_vector_elt (build_vector elt0, ... , eltN), NewEltIdx, idx
1754 // => build_vector elt0, ... , NewEltIdx, ... , eltN
Quentin Colombete2e05482013-07-30 00:27:16 +00001755 case ISD::INSERT_VECTOR_ELT: {
1756 SDValue InVec = N->getOperand(0);
1757 SDValue InVal = N->getOperand(1);
1758 SDValue EltNo = N->getOperand(2);
1759 SDLoc dl(N);
1760
1761 // If the inserted element is an UNDEF, just use the input vector.
Sanjay Patel57195842016-03-14 17:28:46 +00001762 if (InVal.isUndef())
Quentin Colombete2e05482013-07-30 00:27:16 +00001763 return InVec;
1764
1765 EVT VT = InVec.getValueType();
1766
1767 // If we can't generate a legal BUILD_VECTOR, exit
1768 if (!isOperationLegal(ISD::BUILD_VECTOR, VT))
1769 return SDValue();
1770
1771 // Check that we know which element is being inserted
1772 if (!isa<ConstantSDNode>(EltNo))
1773 return SDValue();
1774 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
1775
1776 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
1777 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
1778 // vector elements.
1779 SmallVector<SDValue, 8> Ops;
1780 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
1781 Ops.append(InVec.getNode()->op_begin(),
1782 InVec.getNode()->op_end());
Sanjay Patel57195842016-03-14 17:28:46 +00001783 } else if (InVec.isUndef()) {
Quentin Colombete2e05482013-07-30 00:27:16 +00001784 unsigned NElts = VT.getVectorNumElements();
1785 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
1786 } else {
1787 return SDValue();
1788 }
1789
1790 // Insert the element
1791 if (Elt < Ops.size()) {
1792 // All the operands of BUILD_VECTOR must have the same type;
1793 // we enforce that here.
1794 EVT OpVT = Ops[0].getValueType();
1795 if (InVal.getValueType() != OpVT)
1796 InVal = OpVT.bitsGT(InVal.getValueType()) ?
1797 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
1798 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
1799 Ops[Elt] = InVal;
1800 }
1801
1802 // Return the new vector
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001803 return DAG.getBuildVector(VT, dl, Ops);
Quentin Colombete2e05482013-07-30 00:27:16 +00001804 }
1805
Tom Stellard365366f2013-01-23 02:09:06 +00001806 // Extract_vec (Build_vector) generated by custom lowering
1807 // also needs to be customly combined
1808 case ISD::EXTRACT_VECTOR_ELT: {
1809 SDValue Arg = N->getOperand(0);
1810 if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1811 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1812 unsigned Element = Const->getZExtValue();
1813 return Arg->getOperand(Element);
1814 }
1815 }
Tom Stellarddd04c832013-01-31 22:11:53 +00001816 if (Arg.getOpcode() == ISD::BITCAST &&
1817 Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
1818 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1819 unsigned Element = Const->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001820 return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getVTList(),
Tom Stellarddd04c832013-01-31 22:11:53 +00001821 Arg->getOperand(0).getOperand(Element));
1822 }
1823 }
Mehdi Aminie029eae2015-07-16 06:23:12 +00001824 break;
Tom Stellard365366f2013-01-23 02:09:06 +00001825 }
Tom Stellarde06163a2013-02-07 14:02:35 +00001826
1827 case ISD::SELECT_CC: {
Tom Stellardafa8b532014-05-09 16:42:16 +00001828 // Try common optimizations
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00001829 if (SDValue Ret = AMDGPUTargetLowering::PerformDAGCombine(N, DCI))
Tom Stellardafa8b532014-05-09 16:42:16 +00001830 return Ret;
1831
Tom Stellarde06163a2013-02-07 14:02:35 +00001832 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1833 // selectcc x, y, a, b, inv(cc)
Tom Stellard5e524892013-03-08 15:37:11 +00001834 //
1835 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, setne ->
1836 // selectcc x, y, a, b, cc
Tom Stellarde06163a2013-02-07 14:02:35 +00001837 SDValue LHS = N->getOperand(0);
1838 if (LHS.getOpcode() != ISD::SELECT_CC) {
1839 return SDValue();
1840 }
1841
1842 SDValue RHS = N->getOperand(1);
1843 SDValue True = N->getOperand(2);
1844 SDValue False = N->getOperand(3);
Tom Stellard5e524892013-03-08 15:37:11 +00001845 ISD::CondCode NCC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Tom Stellarde06163a2013-02-07 14:02:35 +00001846
1847 if (LHS.getOperand(2).getNode() != True.getNode() ||
1848 LHS.getOperand(3).getNode() != False.getNode() ||
Tom Stellard5e524892013-03-08 15:37:11 +00001849 RHS.getNode() != False.getNode()) {
Tom Stellarde06163a2013-02-07 14:02:35 +00001850 return SDValue();
1851 }
1852
Tom Stellard5e524892013-03-08 15:37:11 +00001853 switch (NCC) {
1854 default: return SDValue();
1855 case ISD::SETNE: return LHS;
1856 case ISD::SETEQ: {
1857 ISD::CondCode LHSCC = cast<CondCodeSDNode>(LHS.getOperand(4))->get();
1858 LHSCC = ISD::getSetCCInverse(LHSCC,
1859 LHS.getOperand(0).getValueType().isInteger());
Tom Stellardcd428182013-09-28 02:50:38 +00001860 if (DCI.isBeforeLegalizeOps() ||
1861 isCondCodeLegal(LHSCC, LHS.getOperand(0).getSimpleValueType()))
1862 return DAG.getSelectCC(SDLoc(N),
1863 LHS.getOperand(0),
1864 LHS.getOperand(1),
1865 LHS.getOperand(2),
1866 LHS.getOperand(3),
1867 LHSCC);
1868 break;
Vincent Lejeuned80bc152013-02-14 16:55:06 +00001869 }
Tom Stellard5e524892013-03-08 15:37:11 +00001870 }
Tom Stellardcd428182013-09-28 02:50:38 +00001871 return SDValue();
Tom Stellard5e524892013-03-08 15:37:11 +00001872 }
Tom Stellardfbab8272013-08-16 01:12:11 +00001873
Vincent Lejeuned80bc152013-02-14 16:55:06 +00001874 case AMDGPUISD::EXPORT: {
1875 SDValue Arg = N->getOperand(1);
1876 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1877 break;
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001878
Vincent Lejeuned80bc152013-02-14 16:55:06 +00001879 SDValue NewArgs[8] = {
1880 N->getOperand(0), // Chain
1881 SDValue(),
1882 N->getOperand(2), // ArrayBase
1883 N->getOperand(3), // Type
1884 N->getOperand(4), // SWZ_X
1885 N->getOperand(5), // SWZ_Y
1886 N->getOperand(6), // SWZ_Z
1887 N->getOperand(7) // SWZ_W
1888 };
Andrew Trickef9de2a2013-05-25 02:42:55 +00001889 SDLoc DL(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001890 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[4], DAG, DL);
Craig Topper48d114b2014-04-26 18:35:24 +00001891 return DAG.getNode(AMDGPUISD::EXPORT, DL, N->getVTList(), NewArgs);
Tom Stellarde06163a2013-02-07 14:02:35 +00001892 }
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001893 case AMDGPUISD::TEXTURE_FETCH: {
1894 SDValue Arg = N->getOperand(1);
1895 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1896 break;
1897
1898 SDValue NewArgs[19] = {
1899 N->getOperand(0),
1900 N->getOperand(1),
1901 N->getOperand(2),
1902 N->getOperand(3),
1903 N->getOperand(4),
1904 N->getOperand(5),
1905 N->getOperand(6),
1906 N->getOperand(7),
1907 N->getOperand(8),
1908 N->getOperand(9),
1909 N->getOperand(10),
1910 N->getOperand(11),
1911 N->getOperand(12),
1912 N->getOperand(13),
1913 N->getOperand(14),
1914 N->getOperand(15),
1915 N->getOperand(16),
1916 N->getOperand(17),
1917 N->getOperand(18),
1918 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001919 SDLoc DL(N);
1920 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[2], DAG, DL);
1921 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, N->getVTList(), NewArgs);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001922 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001923 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00001924
1925 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001926}
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001927
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001928bool R600TargetLowering::FoldOperand(SDNode *ParentNode, unsigned SrcIdx,
1929 SDValue &Src, SDValue &Neg, SDValue &Abs,
1930 SDValue &Sel, SDValue &Imm,
1931 SelectionDAG &DAG) const {
1932 const R600InstrInfo *TII = getSubtarget()->getInstrInfo();
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001933 if (!Src.isMachineOpcode())
1934 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001935
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001936 switch (Src.getMachineOpcode()) {
1937 case AMDGPU::FNEG_R600:
1938 if (!Neg.getNode())
1939 return false;
1940 Src = Src.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001941 Neg = DAG.getTargetConstant(1, SDLoc(ParentNode), MVT::i32);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001942 return true;
1943 case AMDGPU::FABS_R600:
1944 if (!Abs.getNode())
1945 return false;
1946 Src = Src.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001947 Abs = DAG.getTargetConstant(1, SDLoc(ParentNode), MVT::i32);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001948 return true;
1949 case AMDGPU::CONST_COPY: {
1950 unsigned Opcode = ParentNode->getMachineOpcode();
1951 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
1952
1953 if (!Sel.getNode())
1954 return false;
1955
1956 SDValue CstOffset = Src.getOperand(0);
1957 if (ParentNode->getValueType(0).isVector())
1958 return false;
1959
1960 // Gather constants values
1961 int SrcIndices[] = {
1962 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
1963 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
1964 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2),
1965 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
1966 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
1967 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
1968 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
1969 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
1970 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
1971 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
1972 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
1973 };
1974 std::vector<unsigned> Consts;
Matt Arsenault4d64f962014-05-12 19:23:21 +00001975 for (int OtherSrcIdx : SrcIndices) {
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001976 int OtherSelIdx = TII->getSelIdx(Opcode, OtherSrcIdx);
1977 if (OtherSrcIdx < 0 || OtherSelIdx < 0)
1978 continue;
1979 if (HasDst) {
1980 OtherSrcIdx--;
1981 OtherSelIdx--;
1982 }
1983 if (RegisterSDNode *Reg =
1984 dyn_cast<RegisterSDNode>(ParentNode->getOperand(OtherSrcIdx))) {
1985 if (Reg->getReg() == AMDGPU::ALU_CONST) {
Matt Arsenaultb3ee3882014-05-12 19:26:38 +00001986 ConstantSDNode *Cst
1987 = cast<ConstantSDNode>(ParentNode->getOperand(OtherSelIdx));
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001988 Consts.push_back(Cst->getZExtValue());
1989 }
1990 }
1991 }
1992
Matt Arsenault37c12d72014-05-12 20:42:57 +00001993 ConstantSDNode *Cst = cast<ConstantSDNode>(CstOffset);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001994 Consts.push_back(Cst->getZExtValue());
1995 if (!TII->fitsConstReadLimitations(Consts)) {
1996 return false;
1997 }
1998
1999 Sel = CstOffset;
2000 Src = DAG.getRegister(AMDGPU::ALU_CONST, MVT::f32);
2001 return true;
2002 }
Jan Vesely16800392016-05-13 20:39:31 +00002003 case AMDGPU::MOV_IMM_GLOBAL_ADDR:
2004 // Check if the Imm slot is used. Taken from below.
2005 if (cast<ConstantSDNode>(Imm)->getZExtValue())
2006 return false;
2007 Imm = Src.getOperand(0);
2008 Src = DAG.getRegister(AMDGPU::ALU_LITERAL_X, MVT::i32);
2009 return true;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002010 case AMDGPU::MOV_IMM_I32:
2011 case AMDGPU::MOV_IMM_F32: {
2012 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
2013 uint64_t ImmValue = 0;
2014
2015
2016 if (Src.getMachineOpcode() == AMDGPU::MOV_IMM_F32) {
2017 ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Src.getOperand(0));
2018 float FloatValue = FPC->getValueAPF().convertToFloat();
2019 if (FloatValue == 0.0) {
2020 ImmReg = AMDGPU::ZERO;
2021 } else if (FloatValue == 0.5) {
2022 ImmReg = AMDGPU::HALF;
2023 } else if (FloatValue == 1.0) {
2024 ImmReg = AMDGPU::ONE;
2025 } else {
2026 ImmValue = FPC->getValueAPF().bitcastToAPInt().getZExtValue();
2027 }
2028 } else {
2029 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(0));
2030 uint64_t Value = C->getZExtValue();
2031 if (Value == 0) {
2032 ImmReg = AMDGPU::ZERO;
2033 } else if (Value == 1) {
2034 ImmReg = AMDGPU::ONE_INT;
2035 } else {
2036 ImmValue = Value;
2037 }
2038 }
2039
2040 // Check that we aren't already using an immediate.
2041 // XXX: It's possible for an instruction to have more than one
2042 // immediate operand, but this is not supported yet.
2043 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
2044 if (!Imm.getNode())
2045 return false;
2046 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Imm);
2047 assert(C);
2048 if (C->getZExtValue())
2049 return false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002050 Imm = DAG.getTargetConstant(ImmValue, SDLoc(ParentNode), MVT::i32);
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002051 }
2052 Src = DAG.getRegister(ImmReg, MVT::i32);
2053 return true;
2054 }
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002055 default:
2056 return false;
2057 }
2058}
2059
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002060/// \brief Fold the instructions after selecting them
2061SDNode *R600TargetLowering::PostISelFolding(MachineSDNode *Node,
2062 SelectionDAG &DAG) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002063 const R600InstrInfo *TII = getSubtarget()->getInstrInfo();
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002064 if (!Node->isMachineOpcode())
2065 return Node;
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002066
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002067 unsigned Opcode = Node->getMachineOpcode();
2068 SDValue FakeOp;
2069
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002070 std::vector<SDValue> Ops(Node->op_begin(), Node->op_end());
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002071
2072 if (Opcode == AMDGPU::DOT_4) {
2073 int OperandIdx[] = {
2074 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
2075 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
2076 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
2077 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
2078 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
2079 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
2080 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
2081 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
NAKAMURA Takumi4bb85f92013-10-28 04:07:23 +00002082 };
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002083 int NegIdx[] = {
2084 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_X),
2085 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Y),
2086 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Z),
2087 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_W),
2088 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_X),
2089 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Y),
2090 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Z),
2091 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_W)
2092 };
2093 int AbsIdx[] = {
2094 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_X),
2095 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Y),
2096 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Z),
2097 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_W),
2098 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_X),
2099 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Y),
2100 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Z),
2101 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_W)
2102 };
2103 for (unsigned i = 0; i < 8; i++) {
2104 if (OperandIdx[i] < 0)
2105 return Node;
2106 SDValue &Src = Ops[OperandIdx[i] - 1];
2107 SDValue &Neg = Ops[NegIdx[i] - 1];
2108 SDValue &Abs = Ops[AbsIdx[i] - 1];
2109 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2110 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
2111 if (HasDst)
2112 SelIdx--;
2113 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002114 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, FakeOp, DAG))
2115 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2116 }
2117 } else if (Opcode == AMDGPU::REG_SEQUENCE) {
2118 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) {
2119 SDValue &Src = Ops[i];
2120 if (FoldOperand(Node, i, Src, FakeOp, FakeOp, FakeOp, FakeOp, DAG))
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002121 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2122 }
Vincent Lejeune0167a312013-09-12 23:45:00 +00002123 } else if (Opcode == AMDGPU::CLAMP_R600) {
2124 SDValue Src = Node->getOperand(0);
2125 if (!Src.isMachineOpcode() ||
2126 !TII->hasInstrModifiers(Src.getMachineOpcode()))
2127 return Node;
2128 int ClampIdx = TII->getOperandIdx(Src.getMachineOpcode(),
2129 AMDGPU::OpName::clamp);
2130 if (ClampIdx < 0)
2131 return Node;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002132 SDLoc DL(Node);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002133 std::vector<SDValue> Ops(Src->op_begin(), Src->op_end());
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002134 Ops[ClampIdx - 1] = DAG.getTargetConstant(1, DL, MVT::i32);
2135 return DAG.getMachineNode(Src.getMachineOpcode(), DL,
2136 Node->getVTList(), Ops);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002137 } else {
2138 if (!TII->hasInstrModifiers(Opcode))
2139 return Node;
2140 int OperandIdx[] = {
2141 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
2142 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
2143 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2)
2144 };
2145 int NegIdx[] = {
2146 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg),
2147 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg),
2148 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_neg)
2149 };
2150 int AbsIdx[] = {
2151 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs),
2152 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs),
2153 -1
2154 };
2155 for (unsigned i = 0; i < 3; i++) {
2156 if (OperandIdx[i] < 0)
2157 return Node;
2158 SDValue &Src = Ops[OperandIdx[i] - 1];
2159 SDValue &Neg = Ops[NegIdx[i] - 1];
2160 SDValue FakeAbs;
2161 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
2162 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2163 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002164 int ImmIdx = TII->getOperandIdx(Opcode, AMDGPU::OpName::literal);
2165 if (HasDst) {
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002166 SelIdx--;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002167 ImmIdx--;
2168 }
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002169 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002170 SDValue &Imm = Ops[ImmIdx];
2171 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, Imm, DAG))
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002172 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2173 }
2174 }
2175
2176 return Node;
2177}