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