blob: 21854affe5aafaf901b6331b89b6967cc5b259c0 [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) {
208 return std::next(I)->getOpcode() == AMDGPU::RETURN;
209}
210
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000211MachineBasicBlock *
212R600TargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
213 MachineBasicBlock *BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000214 MachineFunction * MF = BB->getParent();
215 MachineRegisterInfo &MRI = MF->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000216 MachineBasicBlock::iterator I = MI;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000217 const R600InstrInfo *TII = getSubtarget()->getInstrInfo();
Tom Stellard75aadc22012-12-11 21:25:42 +0000218
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000219 switch (MI.getOpcode()) {
Tom Stellardc6f4a292013-08-26 15:05:59 +0000220 default:
Tom Stellard8f9fc202013-11-15 00:12:45 +0000221 // Replace LDS_*_RET instruction that don't have any uses with the
222 // equivalent LDS_*_NORET instruction.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000223 if (TII->isLDSRetInstr(MI.getOpcode())) {
224 int DstIdx = TII->getOperandIdx(MI.getOpcode(), AMDGPU::OpName::dst);
Tom Stellard13c68ef2013-09-05 18:38:09 +0000225 assert(DstIdx != -1);
226 MachineInstrBuilder NewMI;
Aaron Watry1885e532014-09-11 15:02:54 +0000227 // FIXME: getLDSNoRetOp method only handles LDS_1A1D LDS ops. Add
228 // LDS_1A2D support and remove this special case.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000229 if (!MRI.use_empty(MI.getOperand(DstIdx).getReg()) ||
230 MI.getOpcode() == AMDGPU::LDS_CMPST_RET)
Tom Stellard8f9fc202013-11-15 00:12:45 +0000231 return BB;
232
233 NewMI = BuildMI(*BB, I, BB->findDebugLoc(I),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000234 TII->get(AMDGPU::getLDSNoRetOp(MI.getOpcode())));
235 for (unsigned i = 1, e = MI.getNumOperands(); i < e; ++i) {
236 NewMI.addOperand(MI.getOperand(i));
Tom Stellardc6f4a292013-08-26 15:05:59 +0000237 }
Tom Stellardc6f4a292013-08-26 15:05:59 +0000238 } else {
239 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
240 }
241 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000242 case AMDGPU::CLAMP_R600: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000243 MachineInstr *NewMI = TII->buildDefaultInstruction(
244 *BB, I, AMDGPU::MOV, MI.getOperand(0).getReg(),
245 MI.getOperand(1).getReg());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000246 TII->addFlag(*NewMI, 0, MO_FLAG_CLAMP);
Tom Stellard75aadc22012-12-11 21:25:42 +0000247 break;
248 }
249
250 case AMDGPU::FABS_R600: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000251 MachineInstr *NewMI = TII->buildDefaultInstruction(
252 *BB, I, AMDGPU::MOV, MI.getOperand(0).getReg(),
253 MI.getOperand(1).getReg());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000254 TII->addFlag(*NewMI, 0, MO_FLAG_ABS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000255 break;
256 }
257
258 case AMDGPU::FNEG_R600: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000259 MachineInstr *NewMI = TII->buildDefaultInstruction(
260 *BB, I, AMDGPU::MOV, MI.getOperand(0).getReg(),
261 MI.getOperand(1).getReg());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000262 TII->addFlag(*NewMI, 0, MO_FLAG_NEG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000263 break;
264 }
265
Tom Stellard75aadc22012-12-11 21:25:42 +0000266 case AMDGPU::MASK_WRITE: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000267 unsigned maskedRegister = MI.getOperand(0).getReg();
Tom Stellard75aadc22012-12-11 21:25:42 +0000268 assert(TargetRegisterInfo::isVirtualRegister(maskedRegister));
269 MachineInstr * defInstr = MRI.getVRegDef(maskedRegister);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000270 TII->addFlag(*defInstr, 0, MO_FLAG_MASK);
Tom Stellard75aadc22012-12-11 21:25:42 +0000271 break;
272 }
273
274 case AMDGPU::MOV_IMM_F32:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000275 TII->buildMovImm(*BB, I, MI.getOperand(0).getReg(), MI.getOperand(1)
276 .getFPImm()
277 ->getValueAPF()
278 .bitcastToAPInt()
279 .getZExtValue());
Tom Stellard75aadc22012-12-11 21:25:42 +0000280 break;
281 case AMDGPU::MOV_IMM_I32:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000282 TII->buildMovImm(*BB, I, MI.getOperand(0).getReg(),
283 MI.getOperand(1).getImm());
Tom Stellard75aadc22012-12-11 21:25:42 +0000284 break;
Jan Veselyf97de002016-05-13 20:39:29 +0000285 case AMDGPU::MOV_IMM_GLOBAL_ADDR: {
286 //TODO: Perhaps combine this instruction with the next if possible
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000287 auto MIB = TII->buildDefaultInstruction(
288 *BB, MI, AMDGPU::MOV, MI.getOperand(0).getReg(), AMDGPU::ALU_LITERAL_X);
Jan Veselyf97de002016-05-13 20:39:29 +0000289 int Idx = TII->getOperandIdx(*MIB, AMDGPU::OpName::literal);
290 //TODO: Ugh this is rather ugly
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000291 MIB->getOperand(Idx) = MI.getOperand(1);
Jan Veselyf97de002016-05-13 20:39:29 +0000292 break;
293 }
Vincent Lejeune0b72f102013-03-05 15:04:55 +0000294 case AMDGPU::CONST_COPY: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000295 MachineInstr *NewMI = TII->buildDefaultInstruction(
296 *BB, MI, AMDGPU::MOV, MI.getOperand(0).getReg(), AMDGPU::ALU_CONST);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000297 TII->setImmOperand(*NewMI, AMDGPU::OpName::src0_sel,
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000298 MI.getOperand(1).getImm());
Vincent Lejeune0b72f102013-03-05 15:04:55 +0000299 break;
300 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000301
302 case AMDGPU::RAT_WRITE_CACHELESS_32_eg:
Tom Stellard0344cdf2013-08-01 15:23:42 +0000303 case AMDGPU::RAT_WRITE_CACHELESS_64_eg:
Tom Stellard75aadc22012-12-11 21:25:42 +0000304 case AMDGPU::RAT_WRITE_CACHELESS_128_eg: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000305 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode()))
306 .addOperand(MI.getOperand(0))
307 .addOperand(MI.getOperand(1))
308 .addImm(isEOP(I)); // Set End of program bit
Tom Stellard75aadc22012-12-11 21:25:42 +0000309 break;
310 }
Tom Stellarde0e582c2015-10-01 17:51:34 +0000311 case AMDGPU::RAT_STORE_TYPED_eg: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000312 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode()))
313 .addOperand(MI.getOperand(0))
314 .addOperand(MI.getOperand(1))
315 .addOperand(MI.getOperand(2))
316 .addImm(isEOP(I)); // Set End of program bit
Tom Stellarde0e582c2015-10-01 17:51:34 +0000317 break;
318 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000319 case AMDGPU::BRANCH:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000320 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
321 .addOperand(MI.getOperand(0));
322 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000323
324 case AMDGPU::BRANCH_COND_f32: {
325 MachineInstr *NewMI =
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000326 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
327 AMDGPU::PREDICATE_BIT)
328 .addOperand(MI.getOperand(1))
329 .addImm(OPCODE_IS_NOT_ZERO)
330 .addImm(0); // Flags
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000331 TII->addFlag(*NewMI, 0, MO_FLAG_PUSH);
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000332 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000333 .addOperand(MI.getOperand(0))
334 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
Tom Stellard75aadc22012-12-11 21:25:42 +0000335 break;
336 }
337
338 case AMDGPU::BRANCH_COND_i32: {
339 MachineInstr *NewMI =
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000340 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
341 AMDGPU::PREDICATE_BIT)
342 .addOperand(MI.getOperand(1))
Tom Stellard75aadc22012-12-11 21:25:42 +0000343 .addImm(OPCODE_IS_NOT_ZERO_INT)
344 .addImm(0); // Flags
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000345 TII->addFlag(*NewMI, 0, MO_FLAG_PUSH);
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000346 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000347 .addOperand(MI.getOperand(0))
348 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
Tom Stellard75aadc22012-12-11 21:25:42 +0000349 break;
350 }
351
Tom Stellard75aadc22012-12-11 21:25:42 +0000352 case AMDGPU::EG_ExportSwz:
353 case AMDGPU::R600_ExportSwz: {
Tom Stellard6f1b8652013-01-23 21:39:49 +0000354 // Instruction is left unmodified if its not the last one of its type
355 bool isLastInstructionOfItsType = true;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000356 unsigned InstExportType = MI.getOperand(1).getImm();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000357 for (MachineBasicBlock::iterator NextExportInst = std::next(I),
Tom Stellard6f1b8652013-01-23 21:39:49 +0000358 EndBlock = BB->end(); NextExportInst != EndBlock;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000359 NextExportInst = std::next(NextExportInst)) {
Tom Stellard6f1b8652013-01-23 21:39:49 +0000360 if (NextExportInst->getOpcode() == AMDGPU::EG_ExportSwz ||
361 NextExportInst->getOpcode() == AMDGPU::R600_ExportSwz) {
362 unsigned CurrentInstExportType = NextExportInst->getOperand(1)
363 .getImm();
364 if (CurrentInstExportType == InstExportType) {
365 isLastInstructionOfItsType = false;
366 break;
367 }
368 }
369 }
Tom Stellardc0f0fba2015-10-01 17:51:29 +0000370 bool EOP = isEOP(I);
Tom Stellard6f1b8652013-01-23 21:39:49 +0000371 if (!EOP && !isLastInstructionOfItsType)
Tom Stellard75aadc22012-12-11 21:25:42 +0000372 return BB;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000373 unsigned CfInst = (MI.getOpcode() == AMDGPU::EG_ExportSwz) ? 84 : 40;
374 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode()))
375 .addOperand(MI.getOperand(0))
376 .addOperand(MI.getOperand(1))
377 .addOperand(MI.getOperand(2))
378 .addOperand(MI.getOperand(3))
379 .addOperand(MI.getOperand(4))
380 .addOperand(MI.getOperand(5))
381 .addOperand(MI.getOperand(6))
382 .addImm(CfInst)
383 .addImm(EOP);
Tom Stellard75aadc22012-12-11 21:25:42 +0000384 break;
385 }
Jakob Stoklund Olesenfdc37672013-02-05 17:53:52 +0000386 case AMDGPU::RETURN: {
Jakob Stoklund Olesenfdc37672013-02-05 17:53:52 +0000387 return BB;
388 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000389 }
390
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000391 MI.eraseFromParent();
Tom Stellard75aadc22012-12-11 21:25:42 +0000392 return BB;
393}
394
395//===----------------------------------------------------------------------===//
396// Custom DAG Lowering Operations
397//===----------------------------------------------------------------------===//
398
Tom Stellard75aadc22012-12-11 21:25:42 +0000399SDValue R600TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Tom Stellardc026e8b2013-06-28 15:47:08 +0000400 MachineFunction &MF = DAG.getMachineFunction();
401 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
Tom Stellard75aadc22012-12-11 21:25:42 +0000402 switch (Op.getOpcode()) {
403 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellard880a80a2014-06-17 16:53:14 +0000404 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
405 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Jan Vesely25f36272014-06-18 12:27:13 +0000406 case ISD::SHL_PARTS: return LowerSHLParts(Op, DAG);
Jan Veselyecf51332014-06-18 12:27:17 +0000407 case ISD::SRA_PARTS:
Jan Vesely900ff2e2014-06-18 12:27:15 +0000408 case ISD::SRL_PARTS: return LowerSRXParts(Op, DAG);
Jan Vesely808fff52015-04-30 17:15:56 +0000409 case ISD::UADDO: return LowerUADDSUBO(Op, DAG, ISD::ADD, AMDGPUISD::CARRY);
410 case ISD::USUBO: return LowerUADDSUBO(Op, DAG, ISD::SUB, AMDGPUISD::BORROW);
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000411 case ISD::FCOS:
412 case ISD::FSIN: return LowerTrig(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000413 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000414 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000415 case ISD::LOAD: {
416 SDValue Result = LowerLOAD(Op, DAG);
417 assert((!Result.getNode() ||
418 Result.getNode()->getNumValues() == 2) &&
419 "Load should return a value and a chain");
420 return Result;
421 }
422
Matt Arsenault1d555c42014-06-23 18:00:55 +0000423 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellardc026e8b2013-06-28 15:47:08 +0000424 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
Matt Arsenault81d06012016-03-07 21:10:13 +0000425 case ISD::FrameIndex: return lowerFrameIndex(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000426 case ISD::INTRINSIC_VOID: {
427 SDValue Chain = Op.getOperand(0);
428 unsigned IntrinsicID =
429 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
430 switch (IntrinsicID) {
Matt Arsenault82e5e1e2016-07-15 21:27:08 +0000431 case AMDGPUIntrinsic::r600_store_swizzle: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000432 SDLoc DL(Op);
Vincent Lejeuned80bc152013-02-14 16:55:06 +0000433 const SDValue Args[8] = {
434 Chain,
435 Op.getOperand(2), // Export Value
436 Op.getOperand(3), // ArrayBase
437 Op.getOperand(4), // Type
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000438 DAG.getConstant(0, DL, MVT::i32), // SWZ_X
439 DAG.getConstant(1, DL, MVT::i32), // SWZ_Y
440 DAG.getConstant(2, DL, MVT::i32), // SWZ_Z
441 DAG.getConstant(3, DL, MVT::i32) // SWZ_W
Vincent Lejeuned80bc152013-02-14 16:55:06 +0000442 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000443 return DAG.getNode(AMDGPUISD::EXPORT, DL, Op.getValueType(), Args);
Tom Stellard75aadc22012-12-11 21:25:42 +0000444 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000445
Tom Stellard75aadc22012-12-11 21:25:42 +0000446 // default for switch(IntrinsicID)
447 default: break;
448 }
449 // break out of case ISD::INTRINSIC_VOID in switch(Op.getOpcode())
450 break;
451 }
452 case ISD::INTRINSIC_WO_CHAIN: {
453 unsigned IntrinsicID =
454 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
455 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000456 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000457 switch(IntrinsicID) {
458 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Matt Arsenault59bd3012016-01-22 19:00:09 +0000459 case AMDGPUIntrinsic::r600_tex:
Matt Arsenaultf9245b72016-07-22 17:01:25 +0000460 case AMDGPUIntrinsic::r600_texc: {
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000461 unsigned TextureOp;
462 switch (IntrinsicID) {
Matt Arsenault59bd3012016-01-22 19:00:09 +0000463 case AMDGPUIntrinsic::r600_tex:
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000464 TextureOp = 0;
465 break;
Matt Arsenault59bd3012016-01-22 19:00:09 +0000466 case AMDGPUIntrinsic::r600_texc:
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000467 TextureOp = 1;
468 break;
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000469 default:
Matt Arsenault60a750f2016-07-26 21:03:38 +0000470 llvm_unreachable("unhandled texture operation");
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000471 }
472
473 SDValue TexArgs[19] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000474 DAG.getConstant(TextureOp, DL, MVT::i32),
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000475 Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000476 DAG.getConstant(0, DL, MVT::i32),
477 DAG.getConstant(1, DL, MVT::i32),
478 DAG.getConstant(2, DL, MVT::i32),
479 DAG.getConstant(3, DL, MVT::i32),
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000480 Op.getOperand(2),
481 Op.getOperand(3),
482 Op.getOperand(4),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000483 DAG.getConstant(0, DL, MVT::i32),
484 DAG.getConstant(1, DL, MVT::i32),
485 DAG.getConstant(2, DL, MVT::i32),
486 DAG.getConstant(3, DL, MVT::i32),
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000487 Op.getOperand(5),
488 Op.getOperand(6),
489 Op.getOperand(7),
490 Op.getOperand(8),
491 Op.getOperand(9),
492 Op.getOperand(10)
493 };
Craig Topper48d114b2014-04-26 18:35:24 +0000494 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, MVT::v4f32, TexArgs);
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000495 }
Matt Arsenaultca7f5702016-07-14 05:47:17 +0000496 case AMDGPUIntrinsic::r600_dot4: {
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000497 SDValue Args[8] = {
498 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000499 DAG.getConstant(0, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000500 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
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(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000503 DAG.getConstant(1, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000504 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
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(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000507 DAG.getConstant(2, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000508 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
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(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000511 DAG.getConstant(3, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000512 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000513 DAG.getConstant(3, DL, MVT::i32))
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000514 };
Craig Topper48d114b2014-04-26 18:35:24 +0000515 return DAG.getNode(AMDGPUISD::DOT4, DL, MVT::f32, Args);
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000516 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000517
Jan Vesely2fa28c32016-07-10 21:20:29 +0000518 case Intrinsic::r600_implicitarg_ptr: {
519 MVT PtrVT = getPointerTy(DAG.getDataLayout(), AMDGPUAS::PARAM_I_ADDRESS);
520 uint32_t ByteOffset = getImplicitParameterOffset(MFI, FIRST_IMPLICIT);
521 return DAG.getConstant(ByteOffset, DL, PtrVT);
522 }
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000523 case Intrinsic::r600_read_ngroups_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000524 return LowerImplicitParameter(DAG, VT, DL, 0);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000525 case Intrinsic::r600_read_ngroups_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000526 return LowerImplicitParameter(DAG, VT, DL, 1);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000527 case Intrinsic::r600_read_ngroups_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000528 return LowerImplicitParameter(DAG, VT, DL, 2);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000529 case Intrinsic::r600_read_global_size_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000530 return LowerImplicitParameter(DAG, VT, DL, 3);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000531 case Intrinsic::r600_read_global_size_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000532 return LowerImplicitParameter(DAG, VT, DL, 4);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000533 case Intrinsic::r600_read_global_size_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000534 return LowerImplicitParameter(DAG, VT, DL, 5);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000535 case Intrinsic::r600_read_local_size_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000536 return LowerImplicitParameter(DAG, VT, DL, 6);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000537 case Intrinsic::r600_read_local_size_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000538 return LowerImplicitParameter(DAG, VT, DL, 7);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000539 case Intrinsic::r600_read_local_size_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000540 return LowerImplicitParameter(DAG, VT, DL, 8);
541
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000542 case Intrinsic::r600_read_tgid_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000543 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
544 AMDGPU::T1_X, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000545 case Intrinsic::r600_read_tgid_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000546 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
547 AMDGPU::T1_Y, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000548 case Intrinsic::r600_read_tgid_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000549 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
550 AMDGPU::T1_Z, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000551 case Intrinsic::r600_read_tidig_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000552 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
553 AMDGPU::T0_X, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000554 case Intrinsic::r600_read_tidig_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000555 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
556 AMDGPU::T0_Y, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000557 case Intrinsic::r600_read_tidig_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000558 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
559 AMDGPU::T0_Z, VT);
Matt Arsenaultbef34e22016-01-22 21:30:34 +0000560
Matt Arsenault09b2c4a2016-07-15 21:26:52 +0000561 case Intrinsic::r600_recipsqrt_ieee:
562 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
Matt Arsenaultbef34e22016-01-22 21:30:34 +0000563
Matt Arsenault09b2c4a2016-07-15 21:26:52 +0000564 case Intrinsic::r600_recipsqrt_clamped:
565 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
Tom Stellard75aadc22012-12-11 21:25:42 +0000566 }
Matt Arsenault09b2c4a2016-07-15 21:26:52 +0000567
Tom Stellard75aadc22012-12-11 21:25:42 +0000568 // break out of case ISD::INTRINSIC_WO_CHAIN in switch(Op.getOpcode())
569 break;
570 }
571 } // end switch(Op.getOpcode())
572 return SDValue();
573}
574
575void R600TargetLowering::ReplaceNodeResults(SDNode *N,
576 SmallVectorImpl<SDValue> &Results,
577 SelectionDAG &DAG) const {
578 switch (N->getOpcode()) {
Matt Arsenaultd125d742014-03-27 17:23:24 +0000579 default:
580 AMDGPUTargetLowering::ReplaceNodeResults(N, Results, DAG);
581 return;
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000582 case ISD::FP_TO_UINT:
583 if (N->getValueType(0) == MVT::i1) {
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000584 Results.push_back(lowerFP_TO_UINT(N->getOperand(0), DAG));
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000585 return;
586 }
587 // Fall-through. Since we don't care about out of bounds values
588 // we can use FP_TO_SINT for uints too. The DAGLegalizer code for uint
589 // considers some extra cases which are not necessary here.
590 case ISD::FP_TO_SINT: {
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000591 if (N->getValueType(0) == MVT::i1) {
592 Results.push_back(lowerFP_TO_SINT(N->getOperand(0), DAG));
593 return;
594 }
595
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000596 SDValue Result;
597 if (expandFP_TO_SINT(N, Result, DAG))
598 Results.push_back(Result);
Tom Stellard365366f2013-01-23 02:09:06 +0000599 return;
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000600 }
Jan Vesely343cd6f02014-06-22 21:43:01 +0000601 case ISD::SDIVREM: {
602 SDValue Op = SDValue(N, 1);
603 SDValue RES = LowerSDIVREM(Op, DAG);
604 Results.push_back(RES);
605 Results.push_back(RES.getValue(1));
606 break;
607 }
608 case ISD::UDIVREM: {
609 SDValue Op = SDValue(N, 0);
Tom Stellardbf69d762014-11-15 01:07:53 +0000610 LowerUDIVREM64(Op, DAG, Results);
Jan Vesely343cd6f02014-06-22 21:43:01 +0000611 break;
612 }
613 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000614}
615
Tom Stellard880a80a2014-06-17 16:53:14 +0000616SDValue R600TargetLowering::vectorToVerticalVector(SelectionDAG &DAG,
617 SDValue Vector) const {
618
619 SDLoc DL(Vector);
620 EVT VecVT = Vector.getValueType();
621 EVT EltVT = VecVT.getVectorElementType();
622 SmallVector<SDValue, 8> Args;
623
624 for (unsigned i = 0, e = VecVT.getVectorNumElements();
625 i != e; ++i) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000626 Args.push_back(DAG.getNode(
627 ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Vector,
628 DAG.getConstant(i, DL, getVectorIdxTy(DAG.getDataLayout()))));
Tom Stellard880a80a2014-06-17 16:53:14 +0000629 }
630
631 return DAG.getNode(AMDGPUISD::BUILD_VERTICAL_VECTOR, DL, VecVT, Args);
632}
633
634SDValue R600TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
635 SelectionDAG &DAG) const {
636
637 SDLoc DL(Op);
638 SDValue Vector = Op.getOperand(0);
639 SDValue Index = Op.getOperand(1);
640
641 if (isa<ConstantSDNode>(Index) ||
642 Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR)
643 return Op;
644
645 Vector = vectorToVerticalVector(DAG, Vector);
646 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, Op.getValueType(),
647 Vector, Index);
648}
649
650SDValue R600TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
651 SelectionDAG &DAG) const {
652 SDLoc DL(Op);
653 SDValue Vector = Op.getOperand(0);
654 SDValue Value = Op.getOperand(1);
655 SDValue Index = Op.getOperand(2);
656
657 if (isa<ConstantSDNode>(Index) ||
658 Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR)
659 return Op;
660
661 Vector = vectorToVerticalVector(DAG, Vector);
662 SDValue Insert = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, Op.getValueType(),
663 Vector, Value, Index);
664 return vectorToVerticalVector(DAG, Insert);
665}
666
Tom Stellard27233b72016-05-02 18:05:17 +0000667SDValue R600TargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
668 SDValue Op,
669 SelectionDAG &DAG) const {
670
671 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
672 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
673 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
674
675 const DataLayout &DL = DAG.getDataLayout();
676 const GlobalValue *GV = GSD->getGlobal();
Tom Stellard27233b72016-05-02 18:05:17 +0000677 MVT ConstPtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
678
Jan Veselyf97de002016-05-13 20:39:29 +0000679 SDValue GA = DAG.getTargetGlobalAddress(GV, SDLoc(GSD), ConstPtrVT);
680 return DAG.getNode(AMDGPUISD::CONST_DATA_PTR, SDLoc(GSD), ConstPtrVT, GA);
Tom Stellard27233b72016-05-02 18:05:17 +0000681}
682
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000683SDValue R600TargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
684 // On hw >= R700, COS/SIN input must be between -1. and 1.
685 // Thus we lower them to TRIG ( FRACT ( x / 2Pi + 0.5) - 0.5)
686 EVT VT = Op.getValueType();
687 SDValue Arg = Op.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000688 SDLoc DL(Op);
Sanjay Patela2607012015-09-16 16:31:21 +0000689
690 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000691 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
692 DAG.getNode(ISD::FADD, DL, VT,
693 DAG.getNode(ISD::FMUL, DL, VT, Arg,
694 DAG.getConstantFP(0.15915494309, DL, MVT::f32)),
695 DAG.getConstantFP(0.5, DL, MVT::f32)));
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000696 unsigned TrigNode;
697 switch (Op.getOpcode()) {
698 case ISD::FCOS:
699 TrigNode = AMDGPUISD::COS_HW;
700 break;
701 case ISD::FSIN:
702 TrigNode = AMDGPUISD::SIN_HW;
703 break;
704 default:
705 llvm_unreachable("Wrong trig opcode");
706 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000707 SDValue TrigVal = DAG.getNode(TrigNode, DL, VT,
708 DAG.getNode(ISD::FADD, DL, VT, FractPart,
709 DAG.getConstantFP(-0.5, DL, MVT::f32)));
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000710 if (Gen >= R600Subtarget::R700)
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000711 return TrigVal;
712 // On R600 hw, COS/SIN input must be between -Pi and Pi.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000713 return DAG.getNode(ISD::FMUL, DL, VT, TrigVal,
714 DAG.getConstantFP(3.14159265359, DL, MVT::f32));
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000715}
716
Jan Vesely25f36272014-06-18 12:27:13 +0000717SDValue R600TargetLowering::LowerSHLParts(SDValue Op, SelectionDAG &DAG) const {
718 SDLoc DL(Op);
719 EVT VT = Op.getValueType();
720
721 SDValue Lo = Op.getOperand(0);
722 SDValue Hi = Op.getOperand(1);
723 SDValue Shift = Op.getOperand(2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000724 SDValue Zero = DAG.getConstant(0, DL, VT);
725 SDValue One = DAG.getConstant(1, DL, VT);
Jan Vesely25f36272014-06-18 12:27:13 +0000726
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000727 SDValue Width = DAG.getConstant(VT.getSizeInBits(), DL, VT);
728 SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, DL, VT);
Jan Vesely25f36272014-06-18 12:27:13 +0000729 SDValue BigShift = DAG.getNode(ISD::SUB, DL, VT, Shift, Width);
730 SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift);
731
732 // The dance around Width1 is necessary for 0 special case.
733 // Without it the CompShift might be 32, producing incorrect results in
734 // Overflow. So we do the shift in two steps, the alternative is to
735 // add a conditional to filter the special case.
736
737 SDValue Overflow = DAG.getNode(ISD::SRL, DL, VT, Lo, CompShift);
738 Overflow = DAG.getNode(ISD::SRL, DL, VT, Overflow, One);
739
740 SDValue HiSmall = DAG.getNode(ISD::SHL, DL, VT, Hi, Shift);
741 HiSmall = DAG.getNode(ISD::OR, DL, VT, HiSmall, Overflow);
742 SDValue LoSmall = DAG.getNode(ISD::SHL, DL, VT, Lo, Shift);
743
744 SDValue HiBig = DAG.getNode(ISD::SHL, DL, VT, Lo, BigShift);
745 SDValue LoBig = Zero;
746
747 Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT);
748 Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT);
749
750 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi);
751}
752
Jan Vesely900ff2e2014-06-18 12:27:15 +0000753SDValue R600TargetLowering::LowerSRXParts(SDValue Op, SelectionDAG &DAG) const {
754 SDLoc DL(Op);
755 EVT VT = Op.getValueType();
756
757 SDValue Lo = Op.getOperand(0);
758 SDValue Hi = Op.getOperand(1);
759 SDValue Shift = Op.getOperand(2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000760 SDValue Zero = DAG.getConstant(0, DL, VT);
761 SDValue One = DAG.getConstant(1, DL, VT);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000762
Jan Veselyecf51332014-06-18 12:27:17 +0000763 const bool SRA = Op.getOpcode() == ISD::SRA_PARTS;
764
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000765 SDValue Width = DAG.getConstant(VT.getSizeInBits(), DL, VT);
766 SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, DL, VT);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000767 SDValue BigShift = DAG.getNode(ISD::SUB, DL, VT, Shift, Width);
768 SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift);
769
770 // The dance around Width1 is necessary for 0 special case.
771 // Without it the CompShift might be 32, producing incorrect results in
772 // Overflow. So we do the shift in two steps, the alternative is to
773 // add a conditional to filter the special case.
774
775 SDValue Overflow = DAG.getNode(ISD::SHL, DL, VT, Hi, CompShift);
776 Overflow = DAG.getNode(ISD::SHL, DL, VT, Overflow, One);
777
Jan Veselyecf51332014-06-18 12:27:17 +0000778 SDValue HiSmall = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, Shift);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000779 SDValue LoSmall = DAG.getNode(ISD::SRL, DL, VT, Lo, Shift);
780 LoSmall = DAG.getNode(ISD::OR, DL, VT, LoSmall, Overflow);
781
Jan Veselyecf51332014-06-18 12:27:17 +0000782 SDValue LoBig = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, BigShift);
783 SDValue HiBig = SRA ? DAG.getNode(ISD::SRA, DL, VT, Hi, Width1) : Zero;
Jan Vesely900ff2e2014-06-18 12:27:15 +0000784
785 Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT);
786 Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT);
787
788 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi);
789}
790
Jan Vesely808fff52015-04-30 17:15:56 +0000791SDValue R600TargetLowering::LowerUADDSUBO(SDValue Op, SelectionDAG &DAG,
792 unsigned mainop, unsigned ovf) const {
793 SDLoc DL(Op);
794 EVT VT = Op.getValueType();
795
796 SDValue Lo = Op.getOperand(0);
797 SDValue Hi = Op.getOperand(1);
798
799 SDValue OVF = DAG.getNode(ovf, DL, VT, Lo, Hi);
800 // Extend sign.
801 OVF = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, OVF,
802 DAG.getValueType(MVT::i1));
803
804 SDValue Res = DAG.getNode(mainop, DL, VT, Lo, Hi);
805
806 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT, VT), Res, OVF);
807}
808
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000809SDValue R600TargetLowering::lowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000810 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000811 return DAG.getNode(
812 ISD::SETCC,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000813 DL,
Tom Stellard75aadc22012-12-11 21:25:42 +0000814 MVT::i1,
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000815 Op, DAG.getConstantFP(1.0f, DL, MVT::f32),
816 DAG.getCondCode(ISD::SETEQ));
817}
818
819SDValue R600TargetLowering::lowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const {
820 SDLoc DL(Op);
821 return DAG.getNode(
822 ISD::SETCC,
823 DL,
824 MVT::i1,
825 Op, DAG.getConstantFP(-1.0f, DL, MVT::f32),
826 DAG.getCondCode(ISD::SETEQ));
Tom Stellard75aadc22012-12-11 21:25:42 +0000827}
828
Tom Stellard75aadc22012-12-11 21:25:42 +0000829SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000830 const SDLoc &DL,
Tom Stellard75aadc22012-12-11 21:25:42 +0000831 unsigned DwordOffset) const {
832 unsigned ByteOffset = DwordOffset * 4;
833 PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
Tom Stellard1e803092013-07-23 01:48:18 +0000834 AMDGPUAS::CONSTANT_BUFFER_0);
Tom Stellard75aadc22012-12-11 21:25:42 +0000835
836 // We shouldn't be using an offset wider than 16-bits for implicit parameters.
837 assert(isInt<16>(ByteOffset));
838
839 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000840 DAG.getConstant(ByteOffset, DL, MVT::i32), // PTR
Justin Lebar9c375812016-07-15 18:27:10 +0000841 MachinePointerInfo(ConstantPointerNull::get(PtrType)));
Tom Stellard75aadc22012-12-11 21:25:42 +0000842}
843
Tom Stellard75aadc22012-12-11 21:25:42 +0000844bool R600TargetLowering::isZero(SDValue Op) const {
845 if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) {
846 return Cst->isNullValue();
847 } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){
848 return CstFP->isZero();
849 } else {
850 return false;
851 }
852}
853
Matt Arsenault6b6a2c32016-03-11 08:00:27 +0000854bool R600TargetLowering::isHWTrueValue(SDValue Op) const {
855 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
856 return CFP->isExactlyValue(1.0);
857 }
858 return isAllOnesConstant(Op);
859}
860
861bool R600TargetLowering::isHWFalseValue(SDValue Op) const {
862 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
863 return CFP->getValueAPF().isZero();
864 }
865 return isNullConstant(Op);
866}
867
Tom Stellard75aadc22012-12-11 21:25:42 +0000868SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000869 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000870 EVT VT = Op.getValueType();
871
872 SDValue LHS = Op.getOperand(0);
873 SDValue RHS = Op.getOperand(1);
874 SDValue True = Op.getOperand(2);
875 SDValue False = Op.getOperand(3);
876 SDValue CC = Op.getOperand(4);
877 SDValue Temp;
878
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +0000879 if (VT == MVT::f32) {
880 DAGCombinerInfo DCI(DAG, AfterLegalizeVectorOps, true, nullptr);
881 SDValue MinMax = CombineFMinMaxLegacy(DL, VT, LHS, RHS, True, False, CC, DCI);
882 if (MinMax)
883 return MinMax;
884 }
885
Tom Stellard75aadc22012-12-11 21:25:42 +0000886 // LHS and RHS are guaranteed to be the same value type
887 EVT CompareVT = LHS.getValueType();
888
889 // Check if we can lower this to a native operation.
890
Tom Stellard2add82d2013-03-08 15:37:09 +0000891 // Try to lower to a SET* instruction:
892 //
893 // SET* can match the following patterns:
894 //
Tom Stellardcd428182013-09-28 02:50:38 +0000895 // select_cc f32, f32, -1, 0, cc_supported
896 // select_cc f32, f32, 1.0f, 0.0f, cc_supported
897 // select_cc i32, i32, -1, 0, cc_supported
Tom Stellard2add82d2013-03-08 15:37:09 +0000898 //
899
900 // Move hardware True/False values to the correct operand.
Tom Stellardcd428182013-09-28 02:50:38 +0000901 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
902 ISD::CondCode InverseCC =
903 ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
Tom Stellard5694d302013-09-28 02:50:43 +0000904 if (isHWTrueValue(False) && isHWFalseValue(True)) {
905 if (isCondCodeLegal(InverseCC, CompareVT.getSimpleVT())) {
906 std::swap(False, True);
907 CC = DAG.getCondCode(InverseCC);
908 } else {
909 ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InverseCC);
910 if (isCondCodeLegal(SwapInvCC, CompareVT.getSimpleVT())) {
911 std::swap(False, True);
912 std::swap(LHS, RHS);
913 CC = DAG.getCondCode(SwapInvCC);
914 }
915 }
Tom Stellard2add82d2013-03-08 15:37:09 +0000916 }
917
918 if (isHWTrueValue(True) && isHWFalseValue(False) &&
919 (CompareVT == VT || VT == MVT::i32)) {
920 // This can be matched by a SET* instruction.
921 return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
922 }
923
Tom Stellard75aadc22012-12-11 21:25:42 +0000924 // Try to lower to a CND* instruction:
Tom Stellard2add82d2013-03-08 15:37:09 +0000925 //
926 // CND* can match the following patterns:
927 //
Tom Stellardcd428182013-09-28 02:50:38 +0000928 // select_cc f32, 0.0, f32, f32, cc_supported
929 // select_cc f32, 0.0, i32, i32, cc_supported
930 // select_cc i32, 0, f32, f32, cc_supported
931 // select_cc i32, 0, i32, i32, cc_supported
Tom Stellard2add82d2013-03-08 15:37:09 +0000932 //
Tom Stellardcd428182013-09-28 02:50:38 +0000933
934 // Try to move the zero value to the RHS
935 if (isZero(LHS)) {
936 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
937 // Try swapping the operands
938 ISD::CondCode CCSwapped = ISD::getSetCCSwappedOperands(CCOpcode);
939 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
940 std::swap(LHS, RHS);
941 CC = DAG.getCondCode(CCSwapped);
942 } else {
943 // Try inverting the conditon and then swapping the operands
944 ISD::CondCode CCInv = ISD::getSetCCInverse(CCOpcode, CompareVT.isInteger());
945 CCSwapped = ISD::getSetCCSwappedOperands(CCInv);
946 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
947 std::swap(True, False);
948 std::swap(LHS, RHS);
949 CC = DAG.getCondCode(CCSwapped);
950 }
951 }
952 }
953 if (isZero(RHS)) {
954 SDValue Cond = LHS;
955 SDValue Zero = RHS;
Tom Stellard75aadc22012-12-11 21:25:42 +0000956 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
957 if (CompareVT != VT) {
958 // Bitcast True / False to the correct types. This will end up being
959 // a nop, but it allows us to define only a single pattern in the
960 // .TD files for each CND* instruction rather than having to have
961 // one pattern for integer True/False and one for fp True/False
962 True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
963 False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
964 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000965
966 switch (CCOpcode) {
967 case ISD::SETONE:
968 case ISD::SETUNE:
969 case ISD::SETNE:
Tom Stellard75aadc22012-12-11 21:25:42 +0000970 CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
971 Temp = True;
972 True = False;
973 False = Temp;
974 break;
975 default:
976 break;
977 }
978 SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
979 Cond, Zero,
980 True, False,
981 DAG.getCondCode(CCOpcode));
982 return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
983 }
984
Tom Stellard75aadc22012-12-11 21:25:42 +0000985 // If we make it this for it means we have no native instructions to handle
986 // this SELECT_CC, so we must lower it.
987 SDValue HWTrue, HWFalse;
988
989 if (CompareVT == MVT::f32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000990 HWTrue = DAG.getConstantFP(1.0f, DL, CompareVT);
991 HWFalse = DAG.getConstantFP(0.0f, DL, CompareVT);
Tom Stellard75aadc22012-12-11 21:25:42 +0000992 } else if (CompareVT == MVT::i32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000993 HWTrue = DAG.getConstant(-1, DL, CompareVT);
994 HWFalse = DAG.getConstant(0, DL, CompareVT);
Tom Stellard75aadc22012-12-11 21:25:42 +0000995 }
996 else {
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +0000997 llvm_unreachable("Unhandled value type in LowerSELECT_CC");
Tom Stellard75aadc22012-12-11 21:25:42 +0000998 }
999
1000 // Lower this unsupported SELECT_CC into a combination of two supported
1001 // SELECT_CC operations.
1002 SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
1003
1004 return DAG.getNode(ISD::SELECT_CC, DL, VT,
1005 Cond, HWFalse,
1006 True, False,
1007 DAG.getCondCode(ISD::SETNE));
1008}
1009
Alp Tokercb402912014-01-24 17:20:08 +00001010/// LLVM generates byte-addressed pointers. For indirect addressing, we need to
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001011/// convert these pointers to a register index. Each register holds
1012/// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
1013/// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
1014/// for indirect addressing.
1015SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
1016 unsigned StackWidth,
1017 SelectionDAG &DAG) const {
1018 unsigned SRLPad;
1019 switch(StackWidth) {
1020 case 1:
1021 SRLPad = 2;
1022 break;
1023 case 2:
1024 SRLPad = 3;
1025 break;
1026 case 4:
1027 SRLPad = 4;
1028 break;
1029 default: llvm_unreachable("Invalid stack width");
1030 }
1031
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001032 SDLoc DL(Ptr);
1033 return DAG.getNode(ISD::SRL, DL, Ptr.getValueType(), Ptr,
1034 DAG.getConstant(SRLPad, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001035}
1036
1037void R600TargetLowering::getStackAddress(unsigned StackWidth,
1038 unsigned ElemIdx,
1039 unsigned &Channel,
1040 unsigned &PtrIncr) const {
1041 switch (StackWidth) {
1042 default:
1043 case 1:
1044 Channel = 0;
1045 if (ElemIdx > 0) {
1046 PtrIncr = 1;
1047 } else {
1048 PtrIncr = 0;
1049 }
1050 break;
1051 case 2:
1052 Channel = ElemIdx % 2;
1053 if (ElemIdx == 2) {
1054 PtrIncr = 1;
1055 } else {
1056 PtrIncr = 0;
1057 }
1058 break;
1059 case 4:
1060 Channel = ElemIdx;
1061 PtrIncr = 0;
1062 break;
1063 }
1064}
1065
Matt Arsenault95245662016-02-11 05:32:46 +00001066SDValue R600TargetLowering::lowerPrivateTruncStore(StoreSDNode *Store,
1067 SelectionDAG &DAG) const {
1068 SDLoc DL(Store);
Tom Stellard75aadc22012-12-11 21:25:42 +00001069
Matt Arsenault95245662016-02-11 05:32:46 +00001070 unsigned Mask = 0;
1071 if (Store->getMemoryVT() == MVT::i8) {
1072 Mask = 0xff;
1073 } else if (Store->getMemoryVT() == MVT::i16) {
1074 Mask = 0xffff;
1075 }
1076
1077 SDValue Chain = Store->getChain();
1078 SDValue BasePtr = Store->getBasePtr();
1079 EVT MemVT = Store->getMemoryVT();
1080
1081 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, BasePtr,
1082 DAG.getConstant(2, DL, MVT::i32));
1083 SDValue Dst = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
1084 Chain, Ptr,
1085 DAG.getTargetConstant(0, DL, MVT::i32));
1086
1087 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, BasePtr,
1088 DAG.getConstant(0x3, DL, MVT::i32));
1089
1090 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1091 DAG.getConstant(3, DL, MVT::i32));
1092
1093 SDValue SExtValue = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i32,
1094 Store->getValue());
1095
1096 SDValue MaskedValue = DAG.getZeroExtendInReg(SExtValue, DL, MemVT);
1097
1098 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
1099 MaskedValue, ShiftAmt);
1100
1101 SDValue DstMask = DAG.getNode(ISD::SHL, DL, MVT::i32,
1102 DAG.getConstant(Mask, DL, MVT::i32),
1103 ShiftAmt);
1104 DstMask = DAG.getNode(ISD::XOR, DL, MVT::i32, DstMask,
1105 DAG.getConstant(0xffffffff, DL, MVT::i32));
1106 Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
1107
1108 SDValue Value = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
1109 return DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1110 Chain, Value, Ptr,
1111 DAG.getTargetConstant(0, DL, MVT::i32));
1112}
1113
1114SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1115 if (SDValue Result = AMDGPUTargetLowering::MergeVectorStore(Op, DAG))
Tom Stellardfbab8272013-08-16 01:12:11 +00001116 return Result;
Tom Stellardfbab8272013-08-16 01:12:11 +00001117
Matt Arsenault95245662016-02-11 05:32:46 +00001118 StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
1119 unsigned AS = StoreNode->getAddressSpace();
1120 SDValue Value = StoreNode->getValue();
1121 EVT ValueVT = Value.getValueType();
1122
1123 if ((AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::PRIVATE_ADDRESS) &&
1124 ValueVT.isVector()) {
1125 return SplitVectorStore(Op, DAG);
1126 }
1127
1128 SDLoc DL(Op);
1129 SDValue Chain = StoreNode->getChain();
1130 SDValue Ptr = StoreNode->getBasePtr();
1131
1132 if (AS == AMDGPUAS::GLOBAL_ADDRESS) {
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001133 if (StoreNode->isTruncatingStore()) {
1134 EVT VT = Value.getValueType();
Tom Stellardfbab8272013-08-16 01:12:11 +00001135 assert(VT.bitsLE(MVT::i32));
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001136 EVT MemVT = StoreNode->getMemoryVT();
1137 SDValue MaskConstant;
1138 if (MemVT == MVT::i8) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001139 MaskConstant = DAG.getConstant(0xFF, DL, MVT::i32);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001140 } else {
1141 assert(MemVT == MVT::i16);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001142 MaskConstant = DAG.getConstant(0xFFFF, DL, MVT::i32);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001143 }
1144 SDValue DWordAddr = DAG.getNode(ISD::SRL, DL, VT, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001145 DAG.getConstant(2, DL, MVT::i32));
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001146 SDValue ByteIndex = DAG.getNode(ISD::AND, DL, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001147 DAG.getConstant(0x00000003, DL, VT));
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001148 SDValue TruncValue = DAG.getNode(ISD::AND, DL, VT, Value, MaskConstant);
1149 SDValue Shift = DAG.getNode(ISD::SHL, DL, VT, ByteIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001150 DAG.getConstant(3, DL, VT));
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001151 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, VT, TruncValue, Shift);
1152 SDValue Mask = DAG.getNode(ISD::SHL, DL, VT, MaskConstant, Shift);
1153 // XXX: If we add a 64-bit ZW register class, then we could use a 2 x i32
1154 // vector instead.
1155 SDValue Src[4] = {
1156 ShiftedValue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001157 DAG.getConstant(0, DL, MVT::i32),
1158 DAG.getConstant(0, DL, MVT::i32),
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001159 Mask
1160 };
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001161 SDValue Input = DAG.getBuildVector(MVT::v4i32, DL, Src);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001162 SDValue Args[3] = { Chain, Input, DWordAddr };
1163 return DAG.getMemIntrinsicNode(AMDGPUISD::STORE_MSKOR, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00001164 Op->getVTList(), Args, MemVT,
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001165 StoreNode->getMemOperand());
1166 } else if (Ptr->getOpcode() != AMDGPUISD::DWORDADDR &&
Matt Arsenault95245662016-02-11 05:32:46 +00001167 ValueVT.bitsGE(MVT::i32)) {
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001168 // Convert pointer from byte address to dword address.
1169 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, Ptr.getValueType(),
1170 DAG.getNode(ISD::SRL, DL, Ptr.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001171 Ptr, DAG.getConstant(2, DL, MVT::i32)));
Tom Stellard75aadc22012-12-11 21:25:42 +00001172
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001173 if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +00001174 llvm_unreachable("Truncated and indexed stores not supported yet");
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001175 } else {
1176 Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
1177 }
1178 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001179 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001180 }
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001181
Matt Arsenault95245662016-02-11 05:32:46 +00001182 if (AS != AMDGPUAS::PRIVATE_ADDRESS)
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001183 return SDValue();
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001184
Matt Arsenault95245662016-02-11 05:32:46 +00001185 EVT MemVT = StoreNode->getMemoryVT();
1186 if (MemVT.bitsLT(MVT::i32))
1187 return lowerPrivateTruncStore(StoreNode, DAG);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001188
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00001189 // Lowering for indirect addressing
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001190 const MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001191 const R600FrameLowering *TFL = getSubtarget()->getFrameLowering();
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001192 unsigned StackWidth = TFL->getStackWidth(MF);
1193
1194 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1195
1196 if (ValueVT.isVector()) {
1197 unsigned NumElemVT = ValueVT.getVectorNumElements();
1198 EVT ElemVT = ValueVT.getVectorElementType();
Craig Topper48d114b2014-04-26 18:35:24 +00001199 SmallVector<SDValue, 4> Stores(NumElemVT);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001200
1201 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1202 "vector width in load");
1203
1204 for (unsigned i = 0; i < NumElemVT; ++i) {
1205 unsigned Channel, PtrIncr;
1206 getStackAddress(StackWidth, i, Channel, PtrIncr);
1207 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001208 DAG.getConstant(PtrIncr, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001209 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001210 Value, DAG.getConstant(i, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001211
1212 Stores[i] = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1213 Chain, Elem, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001214 DAG.getTargetConstant(Channel, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001215 }
Craig Topper48d114b2014-04-26 18:35:24 +00001216 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Stores);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001217 } else {
1218 if (ValueVT == MVT::i8) {
1219 Value = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Value);
1220 }
1221 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other, Chain, Value, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001222 DAG.getTargetConstant(0, DL, MVT::i32)); // Channel
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001223 }
1224
1225 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001226}
1227
Tom Stellard365366f2013-01-23 02:09:06 +00001228// return (512 + (kc_bank << 12)
1229static int
1230ConstantAddressBlock(unsigned AddressSpace) {
1231 switch (AddressSpace) {
1232 case AMDGPUAS::CONSTANT_BUFFER_0:
1233 return 512;
1234 case AMDGPUAS::CONSTANT_BUFFER_1:
1235 return 512 + 4096;
1236 case AMDGPUAS::CONSTANT_BUFFER_2:
1237 return 512 + 4096 * 2;
1238 case AMDGPUAS::CONSTANT_BUFFER_3:
1239 return 512 + 4096 * 3;
1240 case AMDGPUAS::CONSTANT_BUFFER_4:
1241 return 512 + 4096 * 4;
1242 case AMDGPUAS::CONSTANT_BUFFER_5:
1243 return 512 + 4096 * 5;
1244 case AMDGPUAS::CONSTANT_BUFFER_6:
1245 return 512 + 4096 * 6;
1246 case AMDGPUAS::CONSTANT_BUFFER_7:
1247 return 512 + 4096 * 7;
1248 case AMDGPUAS::CONSTANT_BUFFER_8:
1249 return 512 + 4096 * 8;
1250 case AMDGPUAS::CONSTANT_BUFFER_9:
1251 return 512 + 4096 * 9;
1252 case AMDGPUAS::CONSTANT_BUFFER_10:
1253 return 512 + 4096 * 10;
1254 case AMDGPUAS::CONSTANT_BUFFER_11:
1255 return 512 + 4096 * 11;
1256 case AMDGPUAS::CONSTANT_BUFFER_12:
1257 return 512 + 4096 * 12;
1258 case AMDGPUAS::CONSTANT_BUFFER_13:
1259 return 512 + 4096 * 13;
1260 case AMDGPUAS::CONSTANT_BUFFER_14:
1261 return 512 + 4096 * 14;
1262 case AMDGPUAS::CONSTANT_BUFFER_15:
1263 return 512 + 4096 * 15;
1264 default:
1265 return -1;
1266 }
1267}
1268
Matt Arsenault6dfda962016-02-10 18:21:39 +00001269SDValue R600TargetLowering::lowerPrivateExtLoad(SDValue Op,
1270 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001271 SDLoc DL(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001272 LoadSDNode *Load = cast<LoadSDNode>(Op);
1273 ISD::LoadExtType ExtType = Load->getExtensionType();
1274 EVT MemVT = Load->getMemoryVT();
Tom Stellard365366f2013-01-23 02:09:06 +00001275
Matt Arsenault6dfda962016-02-10 18:21:39 +00001276 // <SI && AS=PRIVATE && EXTLOAD && size < 32bit,
1277 // register (2-)byte extract.
1278
1279 // Get Register holding the target.
1280 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Load->getBasePtr(),
1281 DAG.getConstant(2, DL, MVT::i32));
1282 // Load the Register.
1283 SDValue Ret = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, Op.getValueType(),
1284 Load->getChain(),
1285 Ptr,
1286 DAG.getTargetConstant(0, DL, MVT::i32),
1287 Op.getOperand(2));
1288
1289 // Get offset within the register.
1290 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32,
1291 Load->getBasePtr(),
1292 DAG.getConstant(0x3, DL, MVT::i32));
1293
1294 // Bit offset of target byte (byteIdx * 8).
1295 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1296 DAG.getConstant(3, DL, MVT::i32));
1297
1298 // Shift to the right.
1299 Ret = DAG.getNode(ISD::SRL, DL, MVT::i32, Ret, ShiftAmt);
1300
1301 // Eliminate the upper bits by setting them to ...
1302 EVT MemEltVT = MemVT.getScalarType();
1303
1304 // ... ones.
1305 if (ExtType == ISD::SEXTLOAD) {
1306 SDValue MemEltVTNode = DAG.getValueType(MemEltVT);
1307
1308 SDValue Ops[] = {
1309 DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, Ret, MemEltVTNode),
1310 Load->getChain()
1311 };
1312
1313 return DAG.getMergeValues(Ops, DL);
1314 }
1315
1316 // ... or zeros.
1317 SDValue Ops[] = {
1318 DAG.getZeroExtendInReg(Ret, DL, MemEltVT),
1319 Load->getChain()
1320 };
1321
1322 return DAG.getMergeValues(Ops, DL);
1323}
1324
1325SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1326 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1327 unsigned AS = LoadNode->getAddressSpace();
1328 EVT MemVT = LoadNode->getMemoryVT();
1329 ISD::LoadExtType ExtType = LoadNode->getExtensionType();
1330
1331 if (AS == AMDGPUAS::PRIVATE_ADDRESS &&
1332 ExtType != ISD::NON_EXTLOAD && MemVT.bitsLT(MVT::i32)) {
1333 return lowerPrivateExtLoad(Op, DAG);
1334 }
1335
1336 SDLoc DL(Op);
1337 EVT VT = Op.getValueType();
1338 SDValue Chain = LoadNode->getChain();
1339 SDValue Ptr = LoadNode->getBasePtr();
Tom Stellarde9373602014-01-22 19:24:14 +00001340
Tom Stellard35bb18c2013-08-26 15:06:04 +00001341 if (LoadNode->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS && VT.isVector()) {
1342 SDValue MergedValues[2] = {
Matt Arsenault9c499c32016-04-14 23:31:26 +00001343 scalarizeVectorLoad(LoadNode, DAG),
Tom Stellard35bb18c2013-08-26 15:06:04 +00001344 Chain
1345 };
Craig Topper64941d92014-04-27 19:20:57 +00001346 return DAG.getMergeValues(MergedValues, DL);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001347 }
1348
Tom Stellard365366f2013-01-23 02:09:06 +00001349 int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
Matt Arsenault00a0d6f2013-11-13 02:39:07 +00001350 if (ConstantBlock > -1 &&
1351 ((LoadNode->getExtensionType() == ISD::NON_EXTLOAD) ||
1352 (LoadNode->getExtensionType() == ISD::ZEXTLOAD))) {
Tom Stellard365366f2013-01-23 02:09:06 +00001353 SDValue Result;
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001354 if (isa<ConstantExpr>(LoadNode->getMemOperand()->getValue()) ||
1355 isa<Constant>(LoadNode->getMemOperand()->getValue()) ||
Matt Arsenaultef1a9502013-11-01 17:39:26 +00001356 isa<ConstantSDNode>(Ptr)) {
Tom Stellard365366f2013-01-23 02:09:06 +00001357 SDValue Slots[4];
1358 for (unsigned i = 0; i < 4; i++) {
1359 // We want Const position encoded with the following formula :
1360 // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
1361 // const_index is Ptr computed by llvm using an alignment of 16.
1362 // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
1363 // then div by 4 at the ISel step
1364 SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001365 DAG.getConstant(4 * i + ConstantBlock * 16, DL, MVT::i32));
Tom Stellard365366f2013-01-23 02:09:06 +00001366 Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
1367 }
Tom Stellard0344cdf2013-08-01 15:23:42 +00001368 EVT NewVT = MVT::v4i32;
1369 unsigned NumElements = 4;
1370 if (VT.isVector()) {
1371 NewVT = VT;
1372 NumElements = VT.getVectorNumElements();
1373 }
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001374 Result = DAG.getBuildVector(NewVT, DL, makeArrayRef(Slots, NumElements));
Tom Stellard365366f2013-01-23 02:09:06 +00001375 } else {
Alp Tokerf907b892013-12-05 05:44:44 +00001376 // non-constant ptr can't be folded, keeps it as a v4f32 load
Tom Stellard365366f2013-01-23 02:09:06 +00001377 Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001378 DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr,
1379 DAG.getConstant(4, DL, MVT::i32)),
1380 DAG.getConstant(LoadNode->getAddressSpace() -
1381 AMDGPUAS::CONSTANT_BUFFER_0, DL, MVT::i32)
Tom Stellard365366f2013-01-23 02:09:06 +00001382 );
1383 }
1384
1385 if (!VT.isVector()) {
1386 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001387 DAG.getConstant(0, DL, MVT::i32));
Tom Stellard365366f2013-01-23 02:09:06 +00001388 }
1389
1390 SDValue MergedValues[2] = {
Matt Arsenault7939acd2014-04-07 16:44:24 +00001391 Result,
1392 Chain
Tom Stellard365366f2013-01-23 02:09:06 +00001393 };
Craig Topper64941d92014-04-27 19:20:57 +00001394 return DAG.getMergeValues(MergedValues, DL);
Tom Stellard365366f2013-01-23 02:09:06 +00001395 }
1396
Matt Arsenault6dfda962016-02-10 18:21:39 +00001397 SDValue LoweredLoad;
1398
Matt Arsenault909d0c02013-10-30 23:43:29 +00001399 // For most operations returning SDValue() will result in the node being
1400 // expanded by the DAG Legalizer. This is not the case for ISD::LOAD, so we
1401 // need to manually expand loads that may be legal in some address spaces and
1402 // illegal in others. SEXT loads from CONSTANT_BUFFER_0 are supported for
1403 // compute shaders, since the data is sign extended when it is uploaded to the
1404 // buffer. However SEXT loads from other address spaces are not supported, so
1405 // we need to expand them here.
Tom Stellard84021442013-07-23 01:48:24 +00001406 if (LoadNode->getExtensionType() == ISD::SEXTLOAD) {
1407 EVT MemVT = LoadNode->getMemoryVT();
1408 assert(!MemVT.isVector() && (MemVT == MVT::i16 || MemVT == MVT::i8));
Justin Lebar9c375812016-07-15 18:27:10 +00001409 SDValue NewLoad = DAG.getExtLoad(
1410 ISD::EXTLOAD, DL, VT, Chain, Ptr, LoadNode->getPointerInfo(), MemVT,
1411 LoadNode->getAlignment(), LoadNode->getMemOperand()->getFlags());
Jan Veselyb670d372015-05-26 18:07:22 +00001412 SDValue Res = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, NewLoad,
1413 DAG.getValueType(MemVT));
Tom Stellard84021442013-07-23 01:48:24 +00001414
Jan Veselyb670d372015-05-26 18:07:22 +00001415 SDValue MergedValues[2] = { Res, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00001416 return DAG.getMergeValues(MergedValues, DL);
Tom Stellard84021442013-07-23 01:48:24 +00001417 }
1418
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001419 if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1420 return SDValue();
1421 }
1422
1423 // Lowering for indirect addressing
1424 const MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001425 const R600FrameLowering *TFL = getSubtarget()->getFrameLowering();
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001426 unsigned StackWidth = TFL->getStackWidth(MF);
1427
1428 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1429
1430 if (VT.isVector()) {
1431 unsigned NumElemVT = VT.getVectorNumElements();
1432 EVT ElemVT = VT.getVectorElementType();
1433 SDValue Loads[4];
1434
Jan Vesely687ca8d2016-05-16 23:56:32 +00001435 assert(NumElemVT <= 4);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001436 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1437 "vector width in load");
1438
1439 for (unsigned i = 0; i < NumElemVT; ++i) {
1440 unsigned Channel, PtrIncr;
1441 getStackAddress(StackWidth, i, Channel, PtrIncr);
1442 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001443 DAG.getConstant(PtrIncr, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001444 Loads[i] = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, ElemVT,
1445 Chain, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001446 DAG.getTargetConstant(Channel, DL, MVT::i32),
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001447 Op.getOperand(2));
1448 }
Jan Vesely687ca8d2016-05-16 23:56:32 +00001449 EVT TargetVT = EVT::getVectorVT(*DAG.getContext(), ElemVT, NumElemVT);
1450 LoweredLoad = DAG.getBuildVector(TargetVT, DL, makeArrayRef(Loads, NumElemVT));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001451 } else {
1452 LoweredLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, VT,
1453 Chain, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001454 DAG.getTargetConstant(0, DL, MVT::i32), // Channel
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001455 Op.getOperand(2));
1456 }
1457
Matt Arsenault7939acd2014-04-07 16:44:24 +00001458 SDValue Ops[2] = {
1459 LoweredLoad,
1460 Chain
1461 };
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001462
Craig Topper64941d92014-04-27 19:20:57 +00001463 return DAG.getMergeValues(Ops, DL);
Tom Stellard365366f2013-01-23 02:09:06 +00001464}
Tom Stellard75aadc22012-12-11 21:25:42 +00001465
Matt Arsenault1d555c42014-06-23 18:00:55 +00001466SDValue R600TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
1467 SDValue Chain = Op.getOperand(0);
1468 SDValue Cond = Op.getOperand(1);
1469 SDValue Jump = Op.getOperand(2);
1470
1471 return DAG.getNode(AMDGPUISD::BRANCH_COND, SDLoc(Op), Op.getValueType(),
1472 Chain, Jump, Cond);
1473}
1474
Matt Arsenault81d06012016-03-07 21:10:13 +00001475SDValue R600TargetLowering::lowerFrameIndex(SDValue Op,
1476 SelectionDAG &DAG) const {
1477 MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001478 const R600FrameLowering *TFL = getSubtarget()->getFrameLowering();
Matt Arsenault81d06012016-03-07 21:10:13 +00001479
1480 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op);
1481
1482 unsigned FrameIndex = FIN->getIndex();
1483 unsigned IgnoredFrameReg;
1484 unsigned Offset =
1485 TFL->getFrameIndexReference(MF, FrameIndex, IgnoredFrameReg);
1486 return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF), SDLoc(Op),
1487 Op.getValueType());
1488}
1489
Tom Stellard75aadc22012-12-11 21:25:42 +00001490/// XXX Only kernel functions are supported, so we can assume for now that
1491/// every function is a kernel function, but in the future we should use
1492/// separate calling conventions for kernel and non-kernel functions.
1493SDValue R600TargetLowering::LowerFormalArguments(
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001494 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1495 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
1496 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Tom Stellardacfeebf2013-07-23 01:48:05 +00001497 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001498 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1499 *DAG.getContext());
Vincent Lejeunef143af32013-11-11 22:10:24 +00001500 MachineFunction &MF = DAG.getMachineFunction();
Jan Veselye5121f32014-10-14 20:05:26 +00001501 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
Tom Stellardacfeebf2013-07-23 01:48:05 +00001502
Tom Stellardaf775432013-10-23 00:44:32 +00001503 SmallVector<ISD::InputArg, 8> LocalIns;
1504
Matt Arsenault209a7b92014-04-18 07:40:20 +00001505 getOriginalFunctionArgs(DAG, MF.getFunction(), Ins, LocalIns);
Tom Stellardaf775432013-10-23 00:44:32 +00001506
1507 AnalyzeFormalArguments(CCInfo, LocalIns);
Tom Stellardacfeebf2013-07-23 01:48:05 +00001508
Tom Stellard1e803092013-07-23 01:48:18 +00001509 for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
Tom Stellardacfeebf2013-07-23 01:48:05 +00001510 CCValAssign &VA = ArgLocs[i];
Matt Arsenault74ef2772014-08-13 18:14:11 +00001511 const ISD::InputArg &In = Ins[i];
1512 EVT VT = In.VT;
1513 EVT MemVT = VA.getLocVT();
1514 if (!VT.isVector() && MemVT.isVector()) {
1515 // Get load source type if scalarized.
1516 MemVT = MemVT.getVectorElementType();
1517 }
Tom Stellard78e01292013-07-23 01:47:58 +00001518
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001519 if (AMDGPU::isShader(CallConv)) {
Vincent Lejeunef143af32013-11-11 22:10:24 +00001520 unsigned Reg = MF.addLiveIn(VA.getLocReg(), &AMDGPU::R600_Reg128RegClass);
1521 SDValue Register = DAG.getCopyFromReg(Chain, DL, Reg, VT);
1522 InVals.push_back(Register);
1523 continue;
1524 }
1525
Tom Stellard75aadc22012-12-11 21:25:42 +00001526 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
Matt Arsenault74ef2772014-08-13 18:14:11 +00001527 AMDGPUAS::CONSTANT_BUFFER_0);
Tom Stellardacfeebf2013-07-23 01:48:05 +00001528
Matt Arsenaultfae02982014-03-17 18:58:11 +00001529 // i64 isn't a legal type, so the register type used ends up as i32, which
1530 // isn't expected here. It attempts to create this sextload, but it ends up
1531 // being invalid. Somehow this seems to work with i64 arguments, but breaks
1532 // for <1 x i64>.
1533
Tom Stellardacfeebf2013-07-23 01:48:05 +00001534 // The first 36 bytes of the input buffer contains information about
1535 // thread group and global sizes.
Matt Arsenault74ef2772014-08-13 18:14:11 +00001536 ISD::LoadExtType Ext = ISD::NON_EXTLOAD;
1537 if (MemVT.getScalarSizeInBits() != VT.getScalarSizeInBits()) {
1538 // FIXME: This should really check the extload type, but the handling of
1539 // extload vector parameters seems to be broken.
Matt Arsenaulte1f030c2014-04-11 20:59:54 +00001540
Matt Arsenault74ef2772014-08-13 18:14:11 +00001541 // Ext = In.Flags.isSExt() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
1542 Ext = ISD::SEXTLOAD;
1543 }
1544
1545 // Compute the offset from the value.
1546 // XXX - I think PartOffset should give you this, but it seems to give the
1547 // size of the register which isn't useful.
1548
Andrew Trick05938a52015-02-16 18:10:47 +00001549 unsigned ValBase = ArgLocs[In.getOrigArgIndex()].getLocMemOffset();
Matt Arsenault74ef2772014-08-13 18:14:11 +00001550 unsigned PartOffset = VA.getLocMemOffset();
Matt Arsenault52ef4012016-07-26 16:45:58 +00001551 unsigned Offset = Subtarget->getExplicitKernelArgOffset() + VA.getLocMemOffset();
Matt Arsenault74ef2772014-08-13 18:14:11 +00001552
1553 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy), PartOffset - ValBase);
Justin Lebar9c375812016-07-15 18:27:10 +00001554 SDValue Arg = DAG.getLoad(
1555 ISD::UNINDEXED, Ext, VT, DL, Chain,
1556 DAG.getConstant(Offset, DL, MVT::i32), DAG.getUNDEF(MVT::i32), PtrInfo,
1557 MemVT, /* Alignment = */ 4,
1558 MachineMemOperand::MONonTemporal | MachineMemOperand::MOInvariant);
Matt Arsenault209a7b92014-04-18 07:40:20 +00001559
1560 // 4 is the preferred alignment for the CONSTANT memory space.
Tom Stellard75aadc22012-12-11 21:25:42 +00001561 InVals.push_back(Arg);
Matt Arsenault52ef4012016-07-26 16:45:58 +00001562 MFI->setABIArgOffset(Offset + MemVT.getStoreSize());
Tom Stellard75aadc22012-12-11 21:25:42 +00001563 }
1564 return Chain;
1565}
1566
Mehdi Amini44ede332015-07-09 02:09:04 +00001567EVT R600TargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1568 EVT VT) const {
Matt Arsenault209a7b92014-04-18 07:40:20 +00001569 if (!VT.isVector())
1570 return MVT::i32;
Tom Stellard75aadc22012-12-11 21:25:42 +00001571 return VT.changeVectorElementTypeToInteger();
1572}
1573
Matt Arsenaultfa67bdb2016-02-22 21:04:16 +00001574bool R600TargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
1575 unsigned AddrSpace,
1576 unsigned Align,
1577 bool *IsFast) const {
1578 if (IsFast)
1579 *IsFast = false;
1580
1581 if (!VT.isSimple() || VT == MVT::Other)
1582 return false;
1583
1584 if (VT.bitsLT(MVT::i32))
1585 return false;
1586
1587 // TODO: This is a rough estimate.
1588 if (IsFast)
1589 *IsFast = true;
1590
1591 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
1592}
1593
Matt Arsenault209a7b92014-04-18 07:40:20 +00001594static SDValue CompactSwizzlableVector(
1595 SelectionDAG &DAG, SDValue VectorEntry,
1596 DenseMap<unsigned, unsigned> &RemapSwizzle) {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001597 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1598 assert(RemapSwizzle.empty());
1599 SDValue NewBldVec[4] = {
Matt Arsenault209a7b92014-04-18 07:40:20 +00001600 VectorEntry.getOperand(0),
1601 VectorEntry.getOperand(1),
1602 VectorEntry.getOperand(2),
1603 VectorEntry.getOperand(3)
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001604 };
1605
1606 for (unsigned i = 0; i < 4; i++) {
Sanjay Patel57195842016-03-14 17:28:46 +00001607 if (NewBldVec[i].isUndef())
Vincent Lejeunefa58a5f2013-10-13 17:56:10 +00001608 // We mask write here to teach later passes that the ith element of this
1609 // vector is undef. Thus we can use it to reduce 128 bits reg usage,
1610 // break false dependencies and additionnaly make assembly easier to read.
1611 RemapSwizzle[i] = 7; // SEL_MASK_WRITE
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001612 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(NewBldVec[i])) {
1613 if (C->isZero()) {
1614 RemapSwizzle[i] = 4; // SEL_0
1615 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1616 } else if (C->isExactlyValue(1.0)) {
1617 RemapSwizzle[i] = 5; // SEL_1
1618 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1619 }
1620 }
1621
Sanjay Patel57195842016-03-14 17:28:46 +00001622 if (NewBldVec[i].isUndef())
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001623 continue;
1624 for (unsigned j = 0; j < i; j++) {
1625 if (NewBldVec[i] == NewBldVec[j]) {
1626 NewBldVec[i] = DAG.getUNDEF(NewBldVec[i].getValueType());
1627 RemapSwizzle[i] = j;
1628 break;
1629 }
1630 }
1631 }
1632
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001633 return DAG.getBuildVector(VectorEntry.getValueType(), SDLoc(VectorEntry),
1634 NewBldVec);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001635}
1636
Benjamin Kramer193960c2013-06-11 13:32:25 +00001637static SDValue ReorganizeVector(SelectionDAG &DAG, SDValue VectorEntry,
1638 DenseMap<unsigned, unsigned> &RemapSwizzle) {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001639 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1640 assert(RemapSwizzle.empty());
1641 SDValue NewBldVec[4] = {
1642 VectorEntry.getOperand(0),
1643 VectorEntry.getOperand(1),
1644 VectorEntry.getOperand(2),
1645 VectorEntry.getOperand(3)
1646 };
1647 bool isUnmovable[4] = { false, false, false, false };
Vincent Lejeunecc0ea742013-12-10 14:43:31 +00001648 for (unsigned i = 0; i < 4; i++) {
Vincent Lejeuneb8aac8d2013-07-09 15:03:25 +00001649 RemapSwizzle[i] = i;
Vincent Lejeunecc0ea742013-12-10 14:43:31 +00001650 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1651 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1652 ->getZExtValue();
1653 if (i == Idx)
1654 isUnmovable[Idx] = true;
1655 }
1656 }
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001657
1658 for (unsigned i = 0; i < 4; i++) {
1659 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1660 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1661 ->getZExtValue();
Vincent Lejeune301beb82013-10-13 17:56:04 +00001662 if (isUnmovable[Idx])
1663 continue;
1664 // Swap i and Idx
1665 std::swap(NewBldVec[Idx], NewBldVec[i]);
1666 std::swap(RemapSwizzle[i], RemapSwizzle[Idx]);
1667 break;
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001668 }
1669 }
1670
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001671 return DAG.getBuildVector(VectorEntry.getValueType(), SDLoc(VectorEntry),
1672 NewBldVec);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001673}
1674
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001675SDValue R600TargetLowering::OptimizeSwizzle(SDValue BuildVector, SDValue Swz[4],
1676 SelectionDAG &DAG,
1677 const SDLoc &DL) const {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001678 assert(BuildVector.getOpcode() == ISD::BUILD_VECTOR);
1679 // Old -> New swizzle values
1680 DenseMap<unsigned, unsigned> SwizzleRemap;
1681
1682 BuildVector = CompactSwizzlableVector(DAG, BuildVector, SwizzleRemap);
1683 for (unsigned i = 0; i < 4; i++) {
Benjamin Kramer619c4e52015-04-10 11:24:51 +00001684 unsigned Idx = cast<ConstantSDNode>(Swz[i])->getZExtValue();
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001685 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001686 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], DL, MVT::i32);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001687 }
1688
1689 SwizzleRemap.clear();
1690 BuildVector = ReorganizeVector(DAG, BuildVector, SwizzleRemap);
1691 for (unsigned i = 0; i < 4; i++) {
Benjamin Kramer619c4e52015-04-10 11:24:51 +00001692 unsigned Idx = cast<ConstantSDNode>(Swz[i])->getZExtValue();
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001693 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001694 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], DL, MVT::i32);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001695 }
1696
1697 return BuildVector;
1698}
1699
1700
Tom Stellard75aadc22012-12-11 21:25:42 +00001701//===----------------------------------------------------------------------===//
1702// Custom DAG Optimizations
1703//===----------------------------------------------------------------------===//
1704
1705SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
1706 DAGCombinerInfo &DCI) const {
1707 SelectionDAG &DAG = DCI.DAG;
1708
1709 switch (N->getOpcode()) {
Tom Stellard50122a52014-04-07 19:45:41 +00001710 default: return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001711 // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
1712 case ISD::FP_ROUND: {
1713 SDValue Arg = N->getOperand(0);
1714 if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001715 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), N->getValueType(0),
Tom Stellard75aadc22012-12-11 21:25:42 +00001716 Arg.getOperand(0));
1717 }
1718 break;
1719 }
Tom Stellarde06163a2013-02-07 14:02:35 +00001720
1721 // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
1722 // (i32 select_cc f32, f32, -1, 0 cc)
1723 //
1724 // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1725 // this to one of the SET*_DX10 instructions.
1726 case ISD::FP_TO_SINT: {
1727 SDValue FNeg = N->getOperand(0);
1728 if (FNeg.getOpcode() != ISD::FNEG) {
1729 return SDValue();
1730 }
1731 SDValue SelectCC = FNeg.getOperand(0);
1732 if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1733 SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1734 SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1735 !isHWTrueValue(SelectCC.getOperand(2)) ||
1736 !isHWFalseValue(SelectCC.getOperand(3))) {
1737 return SDValue();
1738 }
1739
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001740 SDLoc dl(N);
1741 return DAG.getNode(ISD::SELECT_CC, dl, N->getValueType(0),
Tom Stellarde06163a2013-02-07 14:02:35 +00001742 SelectCC.getOperand(0), // LHS
1743 SelectCC.getOperand(1), // RHS
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001744 DAG.getConstant(-1, dl, MVT::i32), // True
1745 DAG.getConstant(0, dl, MVT::i32), // False
Tom Stellarde06163a2013-02-07 14:02:35 +00001746 SelectCC.getOperand(4)); // CC
1747
1748 break;
1749 }
Quentin Colombete2e05482013-07-30 00:27:16 +00001750
NAKAMURA Takumi8a046432013-10-28 04:07:38 +00001751 // insert_vector_elt (build_vector elt0, ... , eltN), NewEltIdx, idx
1752 // => build_vector elt0, ... , NewEltIdx, ... , eltN
Quentin Colombete2e05482013-07-30 00:27:16 +00001753 case ISD::INSERT_VECTOR_ELT: {
1754 SDValue InVec = N->getOperand(0);
1755 SDValue InVal = N->getOperand(1);
1756 SDValue EltNo = N->getOperand(2);
1757 SDLoc dl(N);
1758
1759 // If the inserted element is an UNDEF, just use the input vector.
Sanjay Patel57195842016-03-14 17:28:46 +00001760 if (InVal.isUndef())
Quentin Colombete2e05482013-07-30 00:27:16 +00001761 return InVec;
1762
1763 EVT VT = InVec.getValueType();
1764
1765 // If we can't generate a legal BUILD_VECTOR, exit
1766 if (!isOperationLegal(ISD::BUILD_VECTOR, VT))
1767 return SDValue();
1768
1769 // Check that we know which element is being inserted
1770 if (!isa<ConstantSDNode>(EltNo))
1771 return SDValue();
1772 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
1773
1774 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
1775 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
1776 // vector elements.
1777 SmallVector<SDValue, 8> Ops;
1778 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
1779 Ops.append(InVec.getNode()->op_begin(),
1780 InVec.getNode()->op_end());
Sanjay Patel57195842016-03-14 17:28:46 +00001781 } else if (InVec.isUndef()) {
Quentin Colombete2e05482013-07-30 00:27:16 +00001782 unsigned NElts = VT.getVectorNumElements();
1783 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
1784 } else {
1785 return SDValue();
1786 }
1787
1788 // Insert the element
1789 if (Elt < Ops.size()) {
1790 // All the operands of BUILD_VECTOR must have the same type;
1791 // we enforce that here.
1792 EVT OpVT = Ops[0].getValueType();
1793 if (InVal.getValueType() != OpVT)
1794 InVal = OpVT.bitsGT(InVal.getValueType()) ?
1795 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
1796 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
1797 Ops[Elt] = InVal;
1798 }
1799
1800 // Return the new vector
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001801 return DAG.getBuildVector(VT, dl, Ops);
Quentin Colombete2e05482013-07-30 00:27:16 +00001802 }
1803
Tom Stellard365366f2013-01-23 02:09:06 +00001804 // Extract_vec (Build_vector) generated by custom lowering
1805 // also needs to be customly combined
1806 case ISD::EXTRACT_VECTOR_ELT: {
1807 SDValue Arg = N->getOperand(0);
1808 if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1809 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1810 unsigned Element = Const->getZExtValue();
1811 return Arg->getOperand(Element);
1812 }
1813 }
Tom Stellarddd04c832013-01-31 22:11:53 +00001814 if (Arg.getOpcode() == ISD::BITCAST &&
1815 Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
1816 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1817 unsigned Element = Const->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001818 return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getVTList(),
Tom Stellarddd04c832013-01-31 22:11:53 +00001819 Arg->getOperand(0).getOperand(Element));
1820 }
1821 }
Mehdi Aminie029eae2015-07-16 06:23:12 +00001822 break;
Tom Stellard365366f2013-01-23 02:09:06 +00001823 }
Tom Stellarde06163a2013-02-07 14:02:35 +00001824
1825 case ISD::SELECT_CC: {
Tom Stellardafa8b532014-05-09 16:42:16 +00001826 // Try common optimizations
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00001827 if (SDValue Ret = AMDGPUTargetLowering::PerformDAGCombine(N, DCI))
Tom Stellardafa8b532014-05-09 16:42:16 +00001828 return Ret;
1829
Tom Stellarde06163a2013-02-07 14:02:35 +00001830 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1831 // selectcc x, y, a, b, inv(cc)
Tom Stellard5e524892013-03-08 15:37:11 +00001832 //
1833 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, setne ->
1834 // selectcc x, y, a, b, cc
Tom Stellarde06163a2013-02-07 14:02:35 +00001835 SDValue LHS = N->getOperand(0);
1836 if (LHS.getOpcode() != ISD::SELECT_CC) {
1837 return SDValue();
1838 }
1839
1840 SDValue RHS = N->getOperand(1);
1841 SDValue True = N->getOperand(2);
1842 SDValue False = N->getOperand(3);
Tom Stellard5e524892013-03-08 15:37:11 +00001843 ISD::CondCode NCC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Tom Stellarde06163a2013-02-07 14:02:35 +00001844
1845 if (LHS.getOperand(2).getNode() != True.getNode() ||
1846 LHS.getOperand(3).getNode() != False.getNode() ||
Tom Stellard5e524892013-03-08 15:37:11 +00001847 RHS.getNode() != False.getNode()) {
Tom Stellarde06163a2013-02-07 14:02:35 +00001848 return SDValue();
1849 }
1850
Tom Stellard5e524892013-03-08 15:37:11 +00001851 switch (NCC) {
1852 default: return SDValue();
1853 case ISD::SETNE: return LHS;
1854 case ISD::SETEQ: {
1855 ISD::CondCode LHSCC = cast<CondCodeSDNode>(LHS.getOperand(4))->get();
1856 LHSCC = ISD::getSetCCInverse(LHSCC,
1857 LHS.getOperand(0).getValueType().isInteger());
Tom Stellardcd428182013-09-28 02:50:38 +00001858 if (DCI.isBeforeLegalizeOps() ||
1859 isCondCodeLegal(LHSCC, LHS.getOperand(0).getSimpleValueType()))
1860 return DAG.getSelectCC(SDLoc(N),
1861 LHS.getOperand(0),
1862 LHS.getOperand(1),
1863 LHS.getOperand(2),
1864 LHS.getOperand(3),
1865 LHSCC);
1866 break;
Vincent Lejeuned80bc152013-02-14 16:55:06 +00001867 }
Tom Stellard5e524892013-03-08 15:37:11 +00001868 }
Tom Stellardcd428182013-09-28 02:50:38 +00001869 return SDValue();
Tom Stellard5e524892013-03-08 15:37:11 +00001870 }
Tom Stellardfbab8272013-08-16 01:12:11 +00001871
Vincent Lejeuned80bc152013-02-14 16:55:06 +00001872 case AMDGPUISD::EXPORT: {
1873 SDValue Arg = N->getOperand(1);
1874 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1875 break;
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001876
Vincent Lejeuned80bc152013-02-14 16:55:06 +00001877 SDValue NewArgs[8] = {
1878 N->getOperand(0), // Chain
1879 SDValue(),
1880 N->getOperand(2), // ArrayBase
1881 N->getOperand(3), // Type
1882 N->getOperand(4), // SWZ_X
1883 N->getOperand(5), // SWZ_Y
1884 N->getOperand(6), // SWZ_Z
1885 N->getOperand(7) // SWZ_W
1886 };
Andrew Trickef9de2a2013-05-25 02:42:55 +00001887 SDLoc DL(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001888 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[4], DAG, DL);
Craig Topper48d114b2014-04-26 18:35:24 +00001889 return DAG.getNode(AMDGPUISD::EXPORT, DL, N->getVTList(), NewArgs);
Tom Stellarde06163a2013-02-07 14:02:35 +00001890 }
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001891 case AMDGPUISD::TEXTURE_FETCH: {
1892 SDValue Arg = N->getOperand(1);
1893 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1894 break;
1895
1896 SDValue NewArgs[19] = {
1897 N->getOperand(0),
1898 N->getOperand(1),
1899 N->getOperand(2),
1900 N->getOperand(3),
1901 N->getOperand(4),
1902 N->getOperand(5),
1903 N->getOperand(6),
1904 N->getOperand(7),
1905 N->getOperand(8),
1906 N->getOperand(9),
1907 N->getOperand(10),
1908 N->getOperand(11),
1909 N->getOperand(12),
1910 N->getOperand(13),
1911 N->getOperand(14),
1912 N->getOperand(15),
1913 N->getOperand(16),
1914 N->getOperand(17),
1915 N->getOperand(18),
1916 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001917 SDLoc DL(N);
1918 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[2], DAG, DL);
1919 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, N->getVTList(), NewArgs);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001920 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001921 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00001922
1923 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001924}
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001925
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001926bool R600TargetLowering::FoldOperand(SDNode *ParentNode, unsigned SrcIdx,
1927 SDValue &Src, SDValue &Neg, SDValue &Abs,
1928 SDValue &Sel, SDValue &Imm,
1929 SelectionDAG &DAG) const {
1930 const R600InstrInfo *TII = getSubtarget()->getInstrInfo();
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001931 if (!Src.isMachineOpcode())
1932 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001933
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001934 switch (Src.getMachineOpcode()) {
1935 case AMDGPU::FNEG_R600:
1936 if (!Neg.getNode())
1937 return false;
1938 Src = Src.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001939 Neg = DAG.getTargetConstant(1, SDLoc(ParentNode), MVT::i32);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001940 return true;
1941 case AMDGPU::FABS_R600:
1942 if (!Abs.getNode())
1943 return false;
1944 Src = Src.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001945 Abs = DAG.getTargetConstant(1, SDLoc(ParentNode), MVT::i32);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001946 return true;
1947 case AMDGPU::CONST_COPY: {
1948 unsigned Opcode = ParentNode->getMachineOpcode();
1949 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
1950
1951 if (!Sel.getNode())
1952 return false;
1953
1954 SDValue CstOffset = Src.getOperand(0);
1955 if (ParentNode->getValueType(0).isVector())
1956 return false;
1957
1958 // Gather constants values
1959 int SrcIndices[] = {
1960 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
1961 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
1962 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2),
1963 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
1964 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
1965 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
1966 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
1967 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
1968 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
1969 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
1970 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
1971 };
1972 std::vector<unsigned> Consts;
Matt Arsenault4d64f962014-05-12 19:23:21 +00001973 for (int OtherSrcIdx : SrcIndices) {
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001974 int OtherSelIdx = TII->getSelIdx(Opcode, OtherSrcIdx);
1975 if (OtherSrcIdx < 0 || OtherSelIdx < 0)
1976 continue;
1977 if (HasDst) {
1978 OtherSrcIdx--;
1979 OtherSelIdx--;
1980 }
1981 if (RegisterSDNode *Reg =
1982 dyn_cast<RegisterSDNode>(ParentNode->getOperand(OtherSrcIdx))) {
1983 if (Reg->getReg() == AMDGPU::ALU_CONST) {
Matt Arsenaultb3ee3882014-05-12 19:26:38 +00001984 ConstantSDNode *Cst
1985 = cast<ConstantSDNode>(ParentNode->getOperand(OtherSelIdx));
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001986 Consts.push_back(Cst->getZExtValue());
1987 }
1988 }
1989 }
1990
Matt Arsenault37c12d72014-05-12 20:42:57 +00001991 ConstantSDNode *Cst = cast<ConstantSDNode>(CstOffset);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001992 Consts.push_back(Cst->getZExtValue());
1993 if (!TII->fitsConstReadLimitations(Consts)) {
1994 return false;
1995 }
1996
1997 Sel = CstOffset;
1998 Src = DAG.getRegister(AMDGPU::ALU_CONST, MVT::f32);
1999 return true;
2000 }
Jan Vesely16800392016-05-13 20:39:31 +00002001 case AMDGPU::MOV_IMM_GLOBAL_ADDR:
2002 // Check if the Imm slot is used. Taken from below.
2003 if (cast<ConstantSDNode>(Imm)->getZExtValue())
2004 return false;
2005 Imm = Src.getOperand(0);
2006 Src = DAG.getRegister(AMDGPU::ALU_LITERAL_X, MVT::i32);
2007 return true;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002008 case AMDGPU::MOV_IMM_I32:
2009 case AMDGPU::MOV_IMM_F32: {
2010 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
2011 uint64_t ImmValue = 0;
2012
2013
2014 if (Src.getMachineOpcode() == AMDGPU::MOV_IMM_F32) {
2015 ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Src.getOperand(0));
2016 float FloatValue = FPC->getValueAPF().convertToFloat();
2017 if (FloatValue == 0.0) {
2018 ImmReg = AMDGPU::ZERO;
2019 } else if (FloatValue == 0.5) {
2020 ImmReg = AMDGPU::HALF;
2021 } else if (FloatValue == 1.0) {
2022 ImmReg = AMDGPU::ONE;
2023 } else {
2024 ImmValue = FPC->getValueAPF().bitcastToAPInt().getZExtValue();
2025 }
2026 } else {
2027 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(0));
2028 uint64_t Value = C->getZExtValue();
2029 if (Value == 0) {
2030 ImmReg = AMDGPU::ZERO;
2031 } else if (Value == 1) {
2032 ImmReg = AMDGPU::ONE_INT;
2033 } else {
2034 ImmValue = Value;
2035 }
2036 }
2037
2038 // Check that we aren't already using an immediate.
2039 // XXX: It's possible for an instruction to have more than one
2040 // immediate operand, but this is not supported yet.
2041 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
2042 if (!Imm.getNode())
2043 return false;
2044 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Imm);
2045 assert(C);
2046 if (C->getZExtValue())
2047 return false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002048 Imm = DAG.getTargetConstant(ImmValue, SDLoc(ParentNode), MVT::i32);
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002049 }
2050 Src = DAG.getRegister(ImmReg, MVT::i32);
2051 return true;
2052 }
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002053 default:
2054 return false;
2055 }
2056}
2057
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002058/// \brief Fold the instructions after selecting them
2059SDNode *R600TargetLowering::PostISelFolding(MachineSDNode *Node,
2060 SelectionDAG &DAG) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002061 const R600InstrInfo *TII = getSubtarget()->getInstrInfo();
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002062 if (!Node->isMachineOpcode())
2063 return Node;
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002064
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002065 unsigned Opcode = Node->getMachineOpcode();
2066 SDValue FakeOp;
2067
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002068 std::vector<SDValue> Ops(Node->op_begin(), Node->op_end());
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002069
2070 if (Opcode == AMDGPU::DOT_4) {
2071 int OperandIdx[] = {
2072 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
2073 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
2074 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
2075 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
2076 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
2077 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
2078 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
2079 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
NAKAMURA Takumi4bb85f92013-10-28 04:07:23 +00002080 };
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002081 int NegIdx[] = {
2082 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_X),
2083 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Y),
2084 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Z),
2085 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_W),
2086 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_X),
2087 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Y),
2088 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Z),
2089 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_W)
2090 };
2091 int AbsIdx[] = {
2092 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_X),
2093 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Y),
2094 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Z),
2095 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_W),
2096 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_X),
2097 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Y),
2098 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Z),
2099 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_W)
2100 };
2101 for (unsigned i = 0; i < 8; i++) {
2102 if (OperandIdx[i] < 0)
2103 return Node;
2104 SDValue &Src = Ops[OperandIdx[i] - 1];
2105 SDValue &Neg = Ops[NegIdx[i] - 1];
2106 SDValue &Abs = Ops[AbsIdx[i] - 1];
2107 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2108 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
2109 if (HasDst)
2110 SelIdx--;
2111 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002112 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, FakeOp, DAG))
2113 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2114 }
2115 } else if (Opcode == AMDGPU::REG_SEQUENCE) {
2116 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) {
2117 SDValue &Src = Ops[i];
2118 if (FoldOperand(Node, i, Src, FakeOp, FakeOp, FakeOp, FakeOp, DAG))
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002119 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2120 }
Vincent Lejeune0167a312013-09-12 23:45:00 +00002121 } else if (Opcode == AMDGPU::CLAMP_R600) {
2122 SDValue Src = Node->getOperand(0);
2123 if (!Src.isMachineOpcode() ||
2124 !TII->hasInstrModifiers(Src.getMachineOpcode()))
2125 return Node;
2126 int ClampIdx = TII->getOperandIdx(Src.getMachineOpcode(),
2127 AMDGPU::OpName::clamp);
2128 if (ClampIdx < 0)
2129 return Node;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002130 SDLoc DL(Node);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002131 std::vector<SDValue> Ops(Src->op_begin(), Src->op_end());
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002132 Ops[ClampIdx - 1] = DAG.getTargetConstant(1, DL, MVT::i32);
2133 return DAG.getMachineNode(Src.getMachineOpcode(), DL,
2134 Node->getVTList(), Ops);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002135 } else {
2136 if (!TII->hasInstrModifiers(Opcode))
2137 return Node;
2138 int OperandIdx[] = {
2139 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
2140 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
2141 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2)
2142 };
2143 int NegIdx[] = {
2144 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg),
2145 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg),
2146 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_neg)
2147 };
2148 int AbsIdx[] = {
2149 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs),
2150 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs),
2151 -1
2152 };
2153 for (unsigned i = 0; i < 3; i++) {
2154 if (OperandIdx[i] < 0)
2155 return Node;
2156 SDValue &Src = Ops[OperandIdx[i] - 1];
2157 SDValue &Neg = Ops[NegIdx[i] - 1];
2158 SDValue FakeAbs;
2159 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
2160 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2161 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002162 int ImmIdx = TII->getOperandIdx(Opcode, AMDGPU::OpName::literal);
2163 if (HasDst) {
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002164 SelIdx--;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002165 ImmIdx--;
2166 }
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002167 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002168 SDValue &Imm = Ops[ImmIdx];
2169 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, Imm, DAG))
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002170 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2171 }
2172 }
2173
2174 return Node;
2175}