blob: 89c9266746ac4defee302c4dac909faea53fae70 [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"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000020#include "R600FrameLowering.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000021#include "R600InstrInfo.h"
22#include "R600MachineFunctionInfo.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000023#include "Utils/AMDGPUBaseInfo.h"
24#include "llvm/ADT/APFloat.h"
25#include "llvm/ADT/APInt.h"
26#include "llvm/ADT/ArrayRef.h"
27#include "llvm/ADT/DenseMap.h"
28#include "llvm/ADT/SmallVector.h"
Tom Stellardacfeebf2013-07-23 01:48:05 +000029#include "llvm/CodeGen/CallingConvLower.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000030#include "llvm/CodeGen/DAGCombine.h"
31#include "llvm/CodeGen/ISDOpcodes.h"
32#include "llvm/CodeGen/MachineBasicBlock.h"
33#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstr.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000035#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000036#include "llvm/CodeGen/MachineMemOperand.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000038#include "llvm/CodeGen/MachineValueType.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000039#include "llvm/CodeGen/SelectionDAG.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000040#include "llvm/IR/Constants.h"
41#include "llvm/IR/DerivedTypes.h"
42#include "llvm/Support/Casting.h"
43#include "llvm/Support/Compiler.h"
44#include "llvm/Support/ErrorHandling.h"
45#include <cassert>
46#include <cstdint>
47#include <iterator>
48#include <utility>
49#include <vector>
Tom Stellard75aadc22012-12-11 21:25:42 +000050
51using namespace llvm;
52
Matt Arsenault43e92fe2016-06-24 06:30:11 +000053R600TargetLowering::R600TargetLowering(const TargetMachine &TM,
54 const R600Subtarget &STI)
Eric Christopher7792e322015-01-30 23:24:40 +000055 : AMDGPUTargetLowering(TM, STI), Gen(STI.getGeneration()) {
Tom Stellard75aadc22012-12-11 21:25:42 +000056 addRegisterClass(MVT::f32, &AMDGPU::R600_Reg32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000057 addRegisterClass(MVT::i32, &AMDGPU::R600_Reg32RegClass);
Tom Stellard0344cdf2013-08-01 15:23:42 +000058 addRegisterClass(MVT::v2f32, &AMDGPU::R600_Reg64RegClass);
59 addRegisterClass(MVT::v2i32, &AMDGPU::R600_Reg64RegClass);
Matt Arsenault71e66762016-05-21 02:27:49 +000060 addRegisterClass(MVT::v4f32, &AMDGPU::R600_Reg128RegClass);
61 addRegisterClass(MVT::v4i32, &AMDGPU::R600_Reg128RegClass);
Tom Stellard0344cdf2013-08-01 15:23:42 +000062
Eric Christopher23a3a7c2015-02-26 00:00:24 +000063 computeRegisterProperties(STI.getRegisterInfo());
Tom Stellard75aadc22012-12-11 21:25:42 +000064
Matt Arsenault71e66762016-05-21 02:27:49 +000065 // Legalize loads and stores to the private address space.
66 setOperationAction(ISD::LOAD, MVT::i32, Custom);
67 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
68 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
69
70 // EXTLOAD should be the same as ZEXTLOAD. It is legal for some address
71 // spaces, so it is custom lowered to handle those where it isn't.
72 for (MVT VT : MVT::integer_valuetypes()) {
73 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
74 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Custom);
75 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Custom);
76
77 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
78 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Custom);
79 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Custom);
80
81 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
82 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Custom);
83 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Custom);
84 }
85
Matt Arsenaultd1097a32016-06-02 19:54:26 +000086 // Workaround for LegalizeDAG asserting on expansion of i1 vector loads.
87 setLoadExtAction(ISD::EXTLOAD, MVT::v2i32, MVT::v2i1, Expand);
88 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i32, MVT::v2i1, Expand);
89 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i32, MVT::v2i1, Expand);
90
91 setLoadExtAction(ISD::EXTLOAD, MVT::v4i32, MVT::v4i1, Expand);
92 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i32, MVT::v4i1, Expand);
93 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i32, MVT::v4i1, Expand);
94
Matt Arsenault71e66762016-05-21 02:27:49 +000095 setOperationAction(ISD::STORE, MVT::i8, Custom);
96 setOperationAction(ISD::STORE, MVT::i32, Custom);
97 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
98 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
99
100 setTruncStoreAction(MVT::i32, MVT::i8, Custom);
101 setTruncStoreAction(MVT::i32, MVT::i16, Custom);
102
Matt Arsenaultd1097a32016-06-02 19:54:26 +0000103 // Workaround for LegalizeDAG asserting on expansion of i1 vector stores.
104 setTruncStoreAction(MVT::v2i32, MVT::v2i1, Expand);
105 setTruncStoreAction(MVT::v4i32, MVT::v4i1, Expand);
106
Tom Stellard0351ea22013-09-28 02:50:50 +0000107 // Set condition code actions
108 setCondCodeAction(ISD::SETO, MVT::f32, Expand);
109 setCondCodeAction(ISD::SETUO, MVT::f32, Expand);
Tom Stellardcd428182013-09-28 02:50:38 +0000110 setCondCodeAction(ISD::SETLT, MVT::f32, Expand);
Tom Stellard0351ea22013-09-28 02:50:50 +0000111 setCondCodeAction(ISD::SETLE, MVT::f32, Expand);
Tom Stellardcd428182013-09-28 02:50:38 +0000112 setCondCodeAction(ISD::SETOLT, MVT::f32, Expand);
113 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
Tom Stellard0351ea22013-09-28 02:50:50 +0000114 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
115 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
116 setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
117 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
Tom Stellardcd428182013-09-28 02:50:38 +0000118 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
119 setCondCodeAction(ISD::SETULE, MVT::f32, Expand);
120
121 setCondCodeAction(ISD::SETLE, MVT::i32, Expand);
122 setCondCodeAction(ISD::SETLT, MVT::i32, Expand);
123 setCondCodeAction(ISD::SETULE, MVT::i32, Expand);
124 setCondCodeAction(ISD::SETULT, MVT::i32, Expand);
125
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000126 setOperationAction(ISD::FCOS, MVT::f32, Custom);
127 setOperationAction(ISD::FSIN, MVT::f32, Custom);
128
Tom Stellard75aadc22012-12-11 21:25:42 +0000129 setOperationAction(ISD::SETCC, MVT::v4i32, Expand);
Tom Stellard0344cdf2013-08-01 15:23:42 +0000130 setOperationAction(ISD::SETCC, MVT::v2i32, Expand);
Tom Stellard75aadc22012-12-11 21:25:42 +0000131
Tom Stellard492ebea2013-03-08 15:37:07 +0000132 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
133 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
Matt Arsenault1d555c42014-06-23 18:00:55 +0000134 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Tom Stellard75aadc22012-12-11 21:25:42 +0000135
136 setOperationAction(ISD::FSUB, MVT::f32, Expand);
137
Tom Stellard75aadc22012-12-11 21:25:42 +0000138 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
139 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
140
Tom Stellarde8f9f282013-03-08 15:37:05 +0000141 setOperationAction(ISD::SETCC, MVT::i32, Expand);
142 setOperationAction(ISD::SETCC, MVT::f32, Expand);
Tom Stellard75aadc22012-12-11 21:25:42 +0000143 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Custom);
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000144 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Custom);
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000145 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
146 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
Tom Stellard75aadc22012-12-11 21:25:42 +0000147
Tom Stellard53f2f902013-09-05 18:38:03 +0000148 setOperationAction(ISD::SELECT, MVT::i32, Expand);
149 setOperationAction(ISD::SELECT, MVT::f32, Expand);
150 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
Tom Stellard53f2f902013-09-05 18:38:03 +0000151 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
Tom Stellard75aadc22012-12-11 21:25:42 +0000152
Jan Vesely808fff52015-04-30 17:15:56 +0000153 // ADD, SUB overflow.
154 // TODO: turn these into Legal?
155 if (Subtarget->hasCARRY())
156 setOperationAction(ISD::UADDO, MVT::i32, Custom);
157
158 if (Subtarget->hasBORROW())
159 setOperationAction(ISD::USUBO, MVT::i32, Custom);
160
Matt Arsenault4e466652014-04-16 01:41:30 +0000161 // Expand sign extension of vectors
162 if (!Subtarget->hasBFE())
163 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
164
165 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Expand);
166 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Expand);
167
168 if (!Subtarget->hasBFE())
169 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
170 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Expand);
171 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Expand);
172
173 if (!Subtarget->hasBFE())
174 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
175 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Expand);
176 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Expand);
177
178 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
179 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Expand);
180 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i32, Expand);
181
182 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Expand);
183
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000184 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
185
Tom Stellard880a80a2014-06-17 16:53:14 +0000186 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i32, Custom);
187 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f32, Custom);
188 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
189 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
190
191 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i32, Custom);
192 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f32, Custom);
193 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
194 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
195
Jan Vesely25f36272014-06-18 12:27:13 +0000196 // We don't have 64-bit shifts. Thus we need either SHX i64 or SHX_PARTS i32
197 // to be Legal/Custom in order to avoid library calls.
198 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000199 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Jan Veselyecf51332014-06-18 12:27:17 +0000200 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jan Vesely25f36272014-06-18 12:27:13 +0000201
Michel Danzer49812b52013-07-10 16:37:07 +0000202 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
203
Matt Arsenaultc4d3d3a2014-06-23 18:00:49 +0000204 const MVT ScalarIntVTs[] = { MVT::i32, MVT::i64 };
205 for (MVT VT : ScalarIntVTs) {
206 setOperationAction(ISD::ADDC, VT, Expand);
207 setOperationAction(ISD::SUBC, VT, Expand);
208 setOperationAction(ISD::ADDE, VT, Expand);
209 setOperationAction(ISD::SUBE, VT, Expand);
210 }
211
Tom Stellardfc455472013-08-12 22:33:21 +0000212 setSchedulingPreference(Sched::Source);
Matt Arsenault71e66762016-05-21 02:27:49 +0000213
Matt Arsenault71e66762016-05-21 02:27:49 +0000214 setTargetDAGCombine(ISD::FP_ROUND);
215 setTargetDAGCombine(ISD::FP_TO_SINT);
216 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
217 setTargetDAGCombine(ISD::SELECT_CC);
218 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
Jan Vesely38814fa2016-08-27 19:09:43 +0000219 setTargetDAGCombine(ISD::LOAD);
Tom Stellard75aadc22012-12-11 21:25:42 +0000220}
221
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000222const R600Subtarget *R600TargetLowering::getSubtarget() const {
223 return static_cast<const R600Subtarget *>(Subtarget);
224}
225
Tom Stellardc0f0fba2015-10-01 17:51:29 +0000226static inline bool isEOP(MachineBasicBlock::iterator I) {
Hans Wennborg0dd9ed12016-08-13 01:12:49 +0000227 if (std::next(I) == I->getParent()->end())
228 return false;
Tom Stellardc0f0fba2015-10-01 17:51:29 +0000229 return std::next(I)->getOpcode() == AMDGPU::RETURN;
230}
231
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000232MachineBasicBlock *
233R600TargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
234 MachineBasicBlock *BB) const {
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000235 MachineFunction *MF = BB->getParent();
Tom Stellard75aadc22012-12-11 21:25:42 +0000236 MachineRegisterInfo &MRI = MF->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000237 MachineBasicBlock::iterator I = MI;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000238 const R600InstrInfo *TII = getSubtarget()->getInstrInfo();
Tom Stellard75aadc22012-12-11 21:25:42 +0000239
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000240 switch (MI.getOpcode()) {
Tom Stellardc6f4a292013-08-26 15:05:59 +0000241 default:
Tom Stellard8f9fc202013-11-15 00:12:45 +0000242 // Replace LDS_*_RET instruction that don't have any uses with the
243 // equivalent LDS_*_NORET instruction.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000244 if (TII->isLDSRetInstr(MI.getOpcode())) {
245 int DstIdx = TII->getOperandIdx(MI.getOpcode(), AMDGPU::OpName::dst);
Tom Stellard13c68ef2013-09-05 18:38:09 +0000246 assert(DstIdx != -1);
247 MachineInstrBuilder NewMI;
Aaron Watry1885e532014-09-11 15:02:54 +0000248 // FIXME: getLDSNoRetOp method only handles LDS_1A1D LDS ops. Add
249 // LDS_1A2D support and remove this special case.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000250 if (!MRI.use_empty(MI.getOperand(DstIdx).getReg()) ||
251 MI.getOpcode() == AMDGPU::LDS_CMPST_RET)
Tom Stellard8f9fc202013-11-15 00:12:45 +0000252 return BB;
253
254 NewMI = BuildMI(*BB, I, BB->findDebugLoc(I),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000255 TII->get(AMDGPU::getLDSNoRetOp(MI.getOpcode())));
256 for (unsigned i = 1, e = MI.getNumOperands(); i < e; ++i) {
257 NewMI.addOperand(MI.getOperand(i));
Tom Stellardc6f4a292013-08-26 15:05:59 +0000258 }
Tom Stellardc6f4a292013-08-26 15:05:59 +0000259 } else {
260 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
261 }
262 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000263 case AMDGPU::CLAMP_R600: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000264 MachineInstr *NewMI = TII->buildDefaultInstruction(
265 *BB, I, AMDGPU::MOV, MI.getOperand(0).getReg(),
266 MI.getOperand(1).getReg());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000267 TII->addFlag(*NewMI, 0, MO_FLAG_CLAMP);
Tom Stellard75aadc22012-12-11 21:25:42 +0000268 break;
269 }
270
271 case AMDGPU::FABS_R600: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000272 MachineInstr *NewMI = TII->buildDefaultInstruction(
273 *BB, I, AMDGPU::MOV, MI.getOperand(0).getReg(),
274 MI.getOperand(1).getReg());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000275 TII->addFlag(*NewMI, 0, MO_FLAG_ABS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000276 break;
277 }
278
279 case AMDGPU::FNEG_R600: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000280 MachineInstr *NewMI = TII->buildDefaultInstruction(
281 *BB, I, AMDGPU::MOV, MI.getOperand(0).getReg(),
282 MI.getOperand(1).getReg());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000283 TII->addFlag(*NewMI, 0, MO_FLAG_NEG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000284 break;
285 }
286
Tom Stellard75aadc22012-12-11 21:25:42 +0000287 case AMDGPU::MASK_WRITE: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000288 unsigned maskedRegister = MI.getOperand(0).getReg();
Tom Stellard75aadc22012-12-11 21:25:42 +0000289 assert(TargetRegisterInfo::isVirtualRegister(maskedRegister));
290 MachineInstr * defInstr = MRI.getVRegDef(maskedRegister);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000291 TII->addFlag(*defInstr, 0, MO_FLAG_MASK);
Tom Stellard75aadc22012-12-11 21:25:42 +0000292 break;
293 }
294
295 case AMDGPU::MOV_IMM_F32:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000296 TII->buildMovImm(*BB, I, MI.getOperand(0).getReg(), MI.getOperand(1)
297 .getFPImm()
298 ->getValueAPF()
299 .bitcastToAPInt()
300 .getZExtValue());
Tom Stellard75aadc22012-12-11 21:25:42 +0000301 break;
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000302
Tom Stellard75aadc22012-12-11 21:25:42 +0000303 case AMDGPU::MOV_IMM_I32:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000304 TII->buildMovImm(*BB, I, MI.getOperand(0).getReg(),
305 MI.getOperand(1).getImm());
Tom Stellard75aadc22012-12-11 21:25:42 +0000306 break;
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000307
Jan Veselyf97de002016-05-13 20:39:29 +0000308 case AMDGPU::MOV_IMM_GLOBAL_ADDR: {
309 //TODO: Perhaps combine this instruction with the next if possible
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000310 auto MIB = TII->buildDefaultInstruction(
311 *BB, MI, AMDGPU::MOV, MI.getOperand(0).getReg(), AMDGPU::ALU_LITERAL_X);
Jan Veselyf97de002016-05-13 20:39:29 +0000312 int Idx = TII->getOperandIdx(*MIB, AMDGPU::OpName::literal);
313 //TODO: Ugh this is rather ugly
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000314 MIB->getOperand(Idx) = MI.getOperand(1);
Jan Veselyf97de002016-05-13 20:39:29 +0000315 break;
316 }
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000317
Vincent Lejeune0b72f102013-03-05 15:04:55 +0000318 case AMDGPU::CONST_COPY: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000319 MachineInstr *NewMI = TII->buildDefaultInstruction(
320 *BB, MI, AMDGPU::MOV, MI.getOperand(0).getReg(), AMDGPU::ALU_CONST);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000321 TII->setImmOperand(*NewMI, AMDGPU::OpName::src0_sel,
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000322 MI.getOperand(1).getImm());
Vincent Lejeune0b72f102013-03-05 15:04:55 +0000323 break;
324 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000325
326 case AMDGPU::RAT_WRITE_CACHELESS_32_eg:
Tom Stellard0344cdf2013-08-01 15:23:42 +0000327 case AMDGPU::RAT_WRITE_CACHELESS_64_eg:
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000328 case AMDGPU::RAT_WRITE_CACHELESS_128_eg:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000329 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode()))
330 .addOperand(MI.getOperand(0))
331 .addOperand(MI.getOperand(1))
332 .addImm(isEOP(I)); // Set End of program bit
Tom Stellard75aadc22012-12-11 21:25:42 +0000333 break;
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000334
335 case AMDGPU::RAT_STORE_TYPED_eg:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000336 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode()))
337 .addOperand(MI.getOperand(0))
338 .addOperand(MI.getOperand(1))
339 .addOperand(MI.getOperand(2))
340 .addImm(isEOP(I)); // Set End of program bit
Tom Stellarde0e582c2015-10-01 17:51:34 +0000341 break;
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000342
Tom Stellard75aadc22012-12-11 21:25:42 +0000343 case AMDGPU::BRANCH:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000344 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
345 .addOperand(MI.getOperand(0));
346 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000347
348 case AMDGPU::BRANCH_COND_f32: {
349 MachineInstr *NewMI =
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000350 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
351 AMDGPU::PREDICATE_BIT)
352 .addOperand(MI.getOperand(1))
Matt Arsenault44f6d692016-08-13 01:43:46 +0000353 .addImm(AMDGPU::PRED_SETNE)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000354 .addImm(0); // Flags
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000355 TII->addFlag(*NewMI, 0, MO_FLAG_PUSH);
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000356 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000357 .addOperand(MI.getOperand(0))
358 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
Tom Stellard75aadc22012-12-11 21:25:42 +0000359 break;
360 }
361
362 case AMDGPU::BRANCH_COND_i32: {
363 MachineInstr *NewMI =
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000364 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
365 AMDGPU::PREDICATE_BIT)
366 .addOperand(MI.getOperand(1))
Matt Arsenault44f6d692016-08-13 01:43:46 +0000367 .addImm(AMDGPU::PRED_SETNE_INT)
Tom Stellard75aadc22012-12-11 21:25:42 +0000368 .addImm(0); // Flags
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000369 TII->addFlag(*NewMI, 0, MO_FLAG_PUSH);
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000370 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000371 .addOperand(MI.getOperand(0))
372 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
Tom Stellard75aadc22012-12-11 21:25:42 +0000373 break;
374 }
375
Tom Stellard75aadc22012-12-11 21:25:42 +0000376 case AMDGPU::EG_ExportSwz:
377 case AMDGPU::R600_ExportSwz: {
Tom Stellard6f1b8652013-01-23 21:39:49 +0000378 // Instruction is left unmodified if its not the last one of its type
379 bool isLastInstructionOfItsType = true;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000380 unsigned InstExportType = MI.getOperand(1).getImm();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000381 for (MachineBasicBlock::iterator NextExportInst = std::next(I),
Tom Stellard6f1b8652013-01-23 21:39:49 +0000382 EndBlock = BB->end(); NextExportInst != EndBlock;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000383 NextExportInst = std::next(NextExportInst)) {
Tom Stellard6f1b8652013-01-23 21:39:49 +0000384 if (NextExportInst->getOpcode() == AMDGPU::EG_ExportSwz ||
385 NextExportInst->getOpcode() == AMDGPU::R600_ExportSwz) {
386 unsigned CurrentInstExportType = NextExportInst->getOperand(1)
387 .getImm();
388 if (CurrentInstExportType == InstExportType) {
389 isLastInstructionOfItsType = false;
390 break;
391 }
392 }
393 }
Tom Stellardc0f0fba2015-10-01 17:51:29 +0000394 bool EOP = isEOP(I);
Tom Stellard6f1b8652013-01-23 21:39:49 +0000395 if (!EOP && !isLastInstructionOfItsType)
Tom Stellard75aadc22012-12-11 21:25:42 +0000396 return BB;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000397 unsigned CfInst = (MI.getOpcode() == AMDGPU::EG_ExportSwz) ? 84 : 40;
398 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode()))
399 .addOperand(MI.getOperand(0))
400 .addOperand(MI.getOperand(1))
401 .addOperand(MI.getOperand(2))
402 .addOperand(MI.getOperand(3))
403 .addOperand(MI.getOperand(4))
404 .addOperand(MI.getOperand(5))
405 .addOperand(MI.getOperand(6))
406 .addImm(CfInst)
407 .addImm(EOP);
Tom Stellard75aadc22012-12-11 21:25:42 +0000408 break;
409 }
Jakob Stoklund Olesenfdc37672013-02-05 17:53:52 +0000410 case AMDGPU::RETURN: {
Jakob Stoklund Olesenfdc37672013-02-05 17:53:52 +0000411 return BB;
412 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000413 }
414
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000415 MI.eraseFromParent();
Tom Stellard75aadc22012-12-11 21:25:42 +0000416 return BB;
417}
418
419//===----------------------------------------------------------------------===//
420// Custom DAG Lowering Operations
421//===----------------------------------------------------------------------===//
422
Tom Stellard75aadc22012-12-11 21:25:42 +0000423SDValue R600TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Tom Stellardc026e8b2013-06-28 15:47:08 +0000424 MachineFunction &MF = DAG.getMachineFunction();
425 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
Tom Stellard75aadc22012-12-11 21:25:42 +0000426 switch (Op.getOpcode()) {
427 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellard880a80a2014-06-17 16:53:14 +0000428 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
429 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Jan Vesely25f36272014-06-18 12:27:13 +0000430 case ISD::SHL_PARTS: return LowerSHLParts(Op, DAG);
Jan Veselyecf51332014-06-18 12:27:17 +0000431 case ISD::SRA_PARTS:
Jan Vesely900ff2e2014-06-18 12:27:15 +0000432 case ISD::SRL_PARTS: return LowerSRXParts(Op, DAG);
Jan Vesely808fff52015-04-30 17:15:56 +0000433 case ISD::UADDO: return LowerUADDSUBO(Op, DAG, ISD::ADD, AMDGPUISD::CARRY);
434 case ISD::USUBO: return LowerUADDSUBO(Op, DAG, ISD::SUB, AMDGPUISD::BORROW);
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000435 case ISD::FCOS:
436 case ISD::FSIN: return LowerTrig(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000437 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000438 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaultd2c9e082014-07-07 18:34:45 +0000439 case ISD::LOAD: {
440 SDValue Result = LowerLOAD(Op, DAG);
441 assert((!Result.getNode() ||
442 Result.getNode()->getNumValues() == 2) &&
443 "Load should return a value and a chain");
444 return Result;
445 }
446
Matt Arsenault1d555c42014-06-23 18:00:55 +0000447 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellardc026e8b2013-06-28 15:47:08 +0000448 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
Matt Arsenault81d06012016-03-07 21:10:13 +0000449 case ISD::FrameIndex: return lowerFrameIndex(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000450 case ISD::INTRINSIC_VOID: {
451 SDValue Chain = Op.getOperand(0);
452 unsigned IntrinsicID =
453 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
454 switch (IntrinsicID) {
Matt Arsenault82e5e1e2016-07-15 21:27:08 +0000455 case AMDGPUIntrinsic::r600_store_swizzle: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000456 SDLoc DL(Op);
Vincent Lejeuned80bc152013-02-14 16:55:06 +0000457 const SDValue Args[8] = {
458 Chain,
459 Op.getOperand(2), // Export Value
460 Op.getOperand(3), // ArrayBase
461 Op.getOperand(4), // Type
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000462 DAG.getConstant(0, DL, MVT::i32), // SWZ_X
463 DAG.getConstant(1, DL, MVT::i32), // SWZ_Y
464 DAG.getConstant(2, DL, MVT::i32), // SWZ_Z
465 DAG.getConstant(3, DL, MVT::i32) // SWZ_W
Vincent Lejeuned80bc152013-02-14 16:55:06 +0000466 };
Matt Arsenault7bee6ac2016-12-05 20:23:10 +0000467 return DAG.getNode(AMDGPUISD::R600_EXPORT, DL, Op.getValueType(), Args);
Tom Stellard75aadc22012-12-11 21:25:42 +0000468 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000469
Tom Stellard75aadc22012-12-11 21:25:42 +0000470 // default for switch(IntrinsicID)
471 default: break;
472 }
473 // break out of case ISD::INTRINSIC_VOID in switch(Op.getOpcode())
474 break;
475 }
476 case ISD::INTRINSIC_WO_CHAIN: {
477 unsigned IntrinsicID =
478 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
479 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000480 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000481 switch(IntrinsicID) {
482 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Matt Arsenault59bd3012016-01-22 19:00:09 +0000483 case AMDGPUIntrinsic::r600_tex:
Matt Arsenaultf9245b72016-07-22 17:01:25 +0000484 case AMDGPUIntrinsic::r600_texc: {
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000485 unsigned TextureOp;
486 switch (IntrinsicID) {
Matt Arsenault59bd3012016-01-22 19:00:09 +0000487 case AMDGPUIntrinsic::r600_tex:
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000488 TextureOp = 0;
489 break;
Matt Arsenault59bd3012016-01-22 19:00:09 +0000490 case AMDGPUIntrinsic::r600_texc:
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000491 TextureOp = 1;
492 break;
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000493 default:
Matt Arsenault60a750f2016-07-26 21:03:38 +0000494 llvm_unreachable("unhandled texture operation");
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000495 }
496
497 SDValue TexArgs[19] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000498 DAG.getConstant(TextureOp, DL, MVT::i32),
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000499 Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000500 DAG.getConstant(0, DL, MVT::i32),
501 DAG.getConstant(1, DL, MVT::i32),
502 DAG.getConstant(2, DL, MVT::i32),
503 DAG.getConstant(3, DL, MVT::i32),
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000504 Op.getOperand(2),
505 Op.getOperand(3),
506 Op.getOperand(4),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000507 DAG.getConstant(0, DL, MVT::i32),
508 DAG.getConstant(1, DL, MVT::i32),
509 DAG.getConstant(2, DL, MVT::i32),
510 DAG.getConstant(3, DL, MVT::i32),
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000511 Op.getOperand(5),
512 Op.getOperand(6),
513 Op.getOperand(7),
514 Op.getOperand(8),
515 Op.getOperand(9),
516 Op.getOperand(10)
517 };
Craig Topper48d114b2014-04-26 18:35:24 +0000518 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, MVT::v4f32, TexArgs);
Vincent Lejeuned3eed662013-05-17 16:50:20 +0000519 }
Matt Arsenaultca7f5702016-07-14 05:47:17 +0000520 case AMDGPUIntrinsic::r600_dot4: {
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000521 SDValue Args[8] = {
522 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000523 DAG.getConstant(0, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000524 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000525 DAG.getConstant(0, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000526 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000527 DAG.getConstant(1, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000528 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000529 DAG.getConstant(1, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000530 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000531 DAG.getConstant(2, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000532 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000533 DAG.getConstant(2, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000534 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000535 DAG.getConstant(3, DL, MVT::i32)),
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000536 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000537 DAG.getConstant(3, DL, MVT::i32))
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000538 };
Craig Topper48d114b2014-04-26 18:35:24 +0000539 return DAG.getNode(AMDGPUISD::DOT4, DL, MVT::f32, Args);
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000540 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000541
Jan Vesely2fa28c32016-07-10 21:20:29 +0000542 case Intrinsic::r600_implicitarg_ptr: {
543 MVT PtrVT = getPointerTy(DAG.getDataLayout(), AMDGPUAS::PARAM_I_ADDRESS);
544 uint32_t ByteOffset = getImplicitParameterOffset(MFI, FIRST_IMPLICIT);
545 return DAG.getConstant(ByteOffset, DL, PtrVT);
546 }
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000547 case Intrinsic::r600_read_ngroups_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000548 return LowerImplicitParameter(DAG, VT, DL, 0);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000549 case Intrinsic::r600_read_ngroups_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000550 return LowerImplicitParameter(DAG, VT, DL, 1);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000551 case Intrinsic::r600_read_ngroups_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000552 return LowerImplicitParameter(DAG, VT, DL, 2);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000553 case Intrinsic::r600_read_global_size_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000554 return LowerImplicitParameter(DAG, VT, DL, 3);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000555 case Intrinsic::r600_read_global_size_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000556 return LowerImplicitParameter(DAG, VT, DL, 4);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000557 case Intrinsic::r600_read_global_size_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000558 return LowerImplicitParameter(DAG, VT, DL, 5);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000559 case Intrinsic::r600_read_local_size_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000560 return LowerImplicitParameter(DAG, VT, DL, 6);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000561 case Intrinsic::r600_read_local_size_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000562 return LowerImplicitParameter(DAG, VT, DL, 7);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000563 case Intrinsic::r600_read_local_size_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000564 return LowerImplicitParameter(DAG, VT, DL, 8);
565
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000566 case Intrinsic::r600_read_tgid_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000567 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
568 AMDGPU::T1_X, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000569 case Intrinsic::r600_read_tgid_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000570 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
571 AMDGPU::T1_Y, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000572 case Intrinsic::r600_read_tgid_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000573 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
574 AMDGPU::T1_Z, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000575 case Intrinsic::r600_read_tidig_x:
Tom Stellard75aadc22012-12-11 21:25:42 +0000576 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
577 AMDGPU::T0_X, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000578 case Intrinsic::r600_read_tidig_y:
Tom Stellard75aadc22012-12-11 21:25:42 +0000579 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
580 AMDGPU::T0_Y, VT);
NAKAMURA Takumi4f328e12013-05-22 06:37:31 +0000581 case Intrinsic::r600_read_tidig_z:
Tom Stellard75aadc22012-12-11 21:25:42 +0000582 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
583 AMDGPU::T0_Z, VT);
Matt Arsenaultbef34e22016-01-22 21:30:34 +0000584
Matt Arsenault09b2c4a2016-07-15 21:26:52 +0000585 case Intrinsic::r600_recipsqrt_ieee:
586 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
Matt Arsenaultbef34e22016-01-22 21:30:34 +0000587
Matt Arsenault09b2c4a2016-07-15 21:26:52 +0000588 case Intrinsic::r600_recipsqrt_clamped:
589 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
Tom Stellard75aadc22012-12-11 21:25:42 +0000590 }
Matt Arsenault09b2c4a2016-07-15 21:26:52 +0000591
Tom Stellard75aadc22012-12-11 21:25:42 +0000592 // break out of case ISD::INTRINSIC_WO_CHAIN in switch(Op.getOpcode())
593 break;
594 }
595 } // end switch(Op.getOpcode())
596 return SDValue();
597}
598
599void R600TargetLowering::ReplaceNodeResults(SDNode *N,
600 SmallVectorImpl<SDValue> &Results,
601 SelectionDAG &DAG) const {
602 switch (N->getOpcode()) {
Matt Arsenaultd125d742014-03-27 17:23:24 +0000603 default:
604 AMDGPUTargetLowering::ReplaceNodeResults(N, Results, DAG);
605 return;
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000606 case ISD::FP_TO_UINT:
607 if (N->getValueType(0) == MVT::i1) {
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000608 Results.push_back(lowerFP_TO_UINT(N->getOperand(0), DAG));
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000609 return;
610 }
Justin Bognerb03fd122016-08-17 05:10:15 +0000611 // Since we don't care about out of bounds values we can use FP_TO_SINT for
612 // uints too. The DAGLegalizer code for uint considers some extra cases
613 // which are not necessary here.
614 LLVM_FALLTHROUGH;
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000615 case ISD::FP_TO_SINT: {
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000616 if (N->getValueType(0) == MVT::i1) {
617 Results.push_back(lowerFP_TO_SINT(N->getOperand(0), DAG));
618 return;
619 }
620
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000621 SDValue Result;
622 if (expandFP_TO_SINT(N, Result, DAG))
623 Results.push_back(Result);
Tom Stellard365366f2013-01-23 02:09:06 +0000624 return;
Jan Vesely2cb62ce2014-07-10 22:40:21 +0000625 }
Jan Vesely343cd6f02014-06-22 21:43:01 +0000626 case ISD::SDIVREM: {
627 SDValue Op = SDValue(N, 1);
628 SDValue RES = LowerSDIVREM(Op, DAG);
629 Results.push_back(RES);
630 Results.push_back(RES.getValue(1));
631 break;
632 }
633 case ISD::UDIVREM: {
634 SDValue Op = SDValue(N, 0);
Tom Stellardbf69d762014-11-15 01:07:53 +0000635 LowerUDIVREM64(Op, DAG, Results);
Jan Vesely343cd6f02014-06-22 21:43:01 +0000636 break;
637 }
638 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000639}
640
Tom Stellard880a80a2014-06-17 16:53:14 +0000641SDValue R600TargetLowering::vectorToVerticalVector(SelectionDAG &DAG,
642 SDValue Vector) const {
Tom Stellard880a80a2014-06-17 16:53:14 +0000643 SDLoc DL(Vector);
644 EVT VecVT = Vector.getValueType();
645 EVT EltVT = VecVT.getVectorElementType();
646 SmallVector<SDValue, 8> Args;
647
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000648 for (unsigned i = 0, e = VecVT.getVectorNumElements(); i != e; ++i) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000649 Args.push_back(DAG.getNode(
650 ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Vector,
651 DAG.getConstant(i, DL, getVectorIdxTy(DAG.getDataLayout()))));
Tom Stellard880a80a2014-06-17 16:53:14 +0000652 }
653
654 return DAG.getNode(AMDGPUISD::BUILD_VERTICAL_VECTOR, DL, VecVT, Args);
655}
656
657SDValue R600TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
658 SelectionDAG &DAG) const {
Tom Stellard880a80a2014-06-17 16:53:14 +0000659 SDLoc DL(Op);
660 SDValue Vector = Op.getOperand(0);
661 SDValue Index = Op.getOperand(1);
662
663 if (isa<ConstantSDNode>(Index) ||
664 Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR)
665 return Op;
666
667 Vector = vectorToVerticalVector(DAG, Vector);
668 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, Op.getValueType(),
669 Vector, Index);
670}
671
672SDValue R600TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
673 SelectionDAG &DAG) const {
674 SDLoc DL(Op);
675 SDValue Vector = Op.getOperand(0);
676 SDValue Value = Op.getOperand(1);
677 SDValue Index = Op.getOperand(2);
678
679 if (isa<ConstantSDNode>(Index) ||
680 Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR)
681 return Op;
682
683 Vector = vectorToVerticalVector(DAG, Vector);
684 SDValue Insert = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, Op.getValueType(),
685 Vector, Value, Index);
686 return vectorToVerticalVector(DAG, Insert);
687}
688
Tom Stellard27233b72016-05-02 18:05:17 +0000689SDValue R600TargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
690 SDValue Op,
691 SelectionDAG &DAG) const {
Tom Stellard27233b72016-05-02 18:05:17 +0000692 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
693 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
694 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
695
696 const DataLayout &DL = DAG.getDataLayout();
697 const GlobalValue *GV = GSD->getGlobal();
Tom Stellard27233b72016-05-02 18:05:17 +0000698 MVT ConstPtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
699
Jan Veselyf97de002016-05-13 20:39:29 +0000700 SDValue GA = DAG.getTargetGlobalAddress(GV, SDLoc(GSD), ConstPtrVT);
701 return DAG.getNode(AMDGPUISD::CONST_DATA_PTR, SDLoc(GSD), ConstPtrVT, GA);
Tom Stellard27233b72016-05-02 18:05:17 +0000702}
703
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000704SDValue R600TargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
705 // On hw >= R700, COS/SIN input must be between -1. and 1.
706 // Thus we lower them to TRIG ( FRACT ( x / 2Pi + 0.5) - 0.5)
707 EVT VT = Op.getValueType();
708 SDValue Arg = Op.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000709 SDLoc DL(Op);
Sanjay Patela2607012015-09-16 16:31:21 +0000710
711 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000712 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
713 DAG.getNode(ISD::FADD, DL, VT,
714 DAG.getNode(ISD::FMUL, DL, VT, Arg,
715 DAG.getConstantFP(0.15915494309, DL, MVT::f32)),
716 DAG.getConstantFP(0.5, DL, MVT::f32)));
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000717 unsigned TrigNode;
718 switch (Op.getOpcode()) {
719 case ISD::FCOS:
720 TrigNode = AMDGPUISD::COS_HW;
721 break;
722 case ISD::FSIN:
723 TrigNode = AMDGPUISD::SIN_HW;
724 break;
725 default:
726 llvm_unreachable("Wrong trig opcode");
727 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000728 SDValue TrigVal = DAG.getNode(TrigNode, DL, VT,
729 DAG.getNode(ISD::FADD, DL, VT, FractPart,
730 DAG.getConstantFP(-0.5, DL, MVT::f32)));
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000731 if (Gen >= R600Subtarget::R700)
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000732 return TrigVal;
733 // On R600 hw, COS/SIN input must be between -Pi and Pi.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000734 return DAG.getNode(ISD::FMUL, DL, VT, TrigVal,
735 DAG.getConstantFP(3.14159265359, DL, MVT::f32));
Vincent Lejeuneb55940c2013-07-09 15:03:11 +0000736}
737
Jan Vesely25f36272014-06-18 12:27:13 +0000738SDValue R600TargetLowering::LowerSHLParts(SDValue Op, SelectionDAG &DAG) const {
739 SDLoc DL(Op);
740 EVT VT = Op.getValueType();
741
742 SDValue Lo = Op.getOperand(0);
743 SDValue Hi = Op.getOperand(1);
744 SDValue Shift = Op.getOperand(2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000745 SDValue Zero = DAG.getConstant(0, DL, VT);
746 SDValue One = DAG.getConstant(1, DL, VT);
Jan Vesely25f36272014-06-18 12:27:13 +0000747
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000748 SDValue Width = DAG.getConstant(VT.getSizeInBits(), DL, VT);
749 SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, DL, VT);
Jan Vesely25f36272014-06-18 12:27:13 +0000750 SDValue BigShift = DAG.getNode(ISD::SUB, DL, VT, Shift, Width);
751 SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift);
752
753 // The dance around Width1 is necessary for 0 special case.
754 // Without it the CompShift might be 32, producing incorrect results in
755 // Overflow. So we do the shift in two steps, the alternative is to
756 // add a conditional to filter the special case.
757
758 SDValue Overflow = DAG.getNode(ISD::SRL, DL, VT, Lo, CompShift);
759 Overflow = DAG.getNode(ISD::SRL, DL, VT, Overflow, One);
760
761 SDValue HiSmall = DAG.getNode(ISD::SHL, DL, VT, Hi, Shift);
762 HiSmall = DAG.getNode(ISD::OR, DL, VT, HiSmall, Overflow);
763 SDValue LoSmall = DAG.getNode(ISD::SHL, DL, VT, Lo, Shift);
764
765 SDValue HiBig = DAG.getNode(ISD::SHL, DL, VT, Lo, BigShift);
766 SDValue LoBig = Zero;
767
768 Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT);
769 Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT);
770
771 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi);
772}
773
Jan Vesely900ff2e2014-06-18 12:27:15 +0000774SDValue R600TargetLowering::LowerSRXParts(SDValue Op, SelectionDAG &DAG) const {
775 SDLoc DL(Op);
776 EVT VT = Op.getValueType();
777
778 SDValue Lo = Op.getOperand(0);
779 SDValue Hi = Op.getOperand(1);
780 SDValue Shift = Op.getOperand(2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000781 SDValue Zero = DAG.getConstant(0, DL, VT);
782 SDValue One = DAG.getConstant(1, DL, VT);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000783
Jan Veselyecf51332014-06-18 12:27:17 +0000784 const bool SRA = Op.getOpcode() == ISD::SRA_PARTS;
785
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000786 SDValue Width = DAG.getConstant(VT.getSizeInBits(), DL, VT);
787 SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, DL, VT);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000788 SDValue BigShift = DAG.getNode(ISD::SUB, DL, VT, Shift, Width);
789 SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift);
790
791 // The dance around Width1 is necessary for 0 special case.
792 // Without it the CompShift might be 32, producing incorrect results in
793 // Overflow. So we do the shift in two steps, the alternative is to
794 // add a conditional to filter the special case.
795
796 SDValue Overflow = DAG.getNode(ISD::SHL, DL, VT, Hi, CompShift);
797 Overflow = DAG.getNode(ISD::SHL, DL, VT, Overflow, One);
798
Jan Veselyecf51332014-06-18 12:27:17 +0000799 SDValue HiSmall = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, Shift);
Jan Vesely900ff2e2014-06-18 12:27:15 +0000800 SDValue LoSmall = DAG.getNode(ISD::SRL, DL, VT, Lo, Shift);
801 LoSmall = DAG.getNode(ISD::OR, DL, VT, LoSmall, Overflow);
802
Jan Veselyecf51332014-06-18 12:27:17 +0000803 SDValue LoBig = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, BigShift);
804 SDValue HiBig = SRA ? DAG.getNode(ISD::SRA, DL, VT, Hi, Width1) : Zero;
Jan Vesely900ff2e2014-06-18 12:27:15 +0000805
806 Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT);
807 Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT);
808
809 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi);
810}
811
Jan Vesely808fff52015-04-30 17:15:56 +0000812SDValue R600TargetLowering::LowerUADDSUBO(SDValue Op, SelectionDAG &DAG,
813 unsigned mainop, unsigned ovf) const {
814 SDLoc DL(Op);
815 EVT VT = Op.getValueType();
816
817 SDValue Lo = Op.getOperand(0);
818 SDValue Hi = Op.getOperand(1);
819
820 SDValue OVF = DAG.getNode(ovf, DL, VT, Lo, Hi);
821 // Extend sign.
822 OVF = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, OVF,
823 DAG.getValueType(MVT::i1));
824
825 SDValue Res = DAG.getNode(mainop, DL, VT, Lo, Hi);
826
827 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT, VT), Res, OVF);
828}
829
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000830SDValue R600TargetLowering::lowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000831 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000832 return DAG.getNode(
833 ISD::SETCC,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000834 DL,
Tom Stellard75aadc22012-12-11 21:25:42 +0000835 MVT::i1,
Matt Arsenault7fb961f2016-07-22 17:01:21 +0000836 Op, DAG.getConstantFP(1.0f, DL, MVT::f32),
837 DAG.getCondCode(ISD::SETEQ));
838}
839
840SDValue R600TargetLowering::lowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const {
841 SDLoc DL(Op);
842 return DAG.getNode(
843 ISD::SETCC,
844 DL,
845 MVT::i1,
846 Op, DAG.getConstantFP(-1.0f, DL, MVT::f32),
847 DAG.getCondCode(ISD::SETEQ));
Tom Stellard75aadc22012-12-11 21:25:42 +0000848}
849
Tom Stellard75aadc22012-12-11 21:25:42 +0000850SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000851 const SDLoc &DL,
Tom Stellard75aadc22012-12-11 21:25:42 +0000852 unsigned DwordOffset) const {
853 unsigned ByteOffset = DwordOffset * 4;
854 PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
Tom Stellard1e803092013-07-23 01:48:18 +0000855 AMDGPUAS::CONSTANT_BUFFER_0);
Tom Stellard75aadc22012-12-11 21:25:42 +0000856
857 // We shouldn't be using an offset wider than 16-bits for implicit parameters.
858 assert(isInt<16>(ByteOffset));
859
860 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000861 DAG.getConstant(ByteOffset, DL, MVT::i32), // PTR
Justin Lebar9c375812016-07-15 18:27:10 +0000862 MachinePointerInfo(ConstantPointerNull::get(PtrType)));
Tom Stellard75aadc22012-12-11 21:25:42 +0000863}
864
Tom Stellard75aadc22012-12-11 21:25:42 +0000865bool R600TargetLowering::isZero(SDValue Op) const {
866 if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) {
867 return Cst->isNullValue();
868 } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){
869 return CstFP->isZero();
870 } else {
871 return false;
872 }
873}
874
Matt Arsenault6b6a2c32016-03-11 08:00:27 +0000875bool R600TargetLowering::isHWTrueValue(SDValue Op) const {
876 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
877 return CFP->isExactlyValue(1.0);
878 }
879 return isAllOnesConstant(Op);
880}
881
882bool R600TargetLowering::isHWFalseValue(SDValue Op) const {
883 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
884 return CFP->getValueAPF().isZero();
885 }
886 return isNullConstant(Op);
887}
888
Tom Stellard75aadc22012-12-11 21:25:42 +0000889SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000890 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000891 EVT VT = Op.getValueType();
892
893 SDValue LHS = Op.getOperand(0);
894 SDValue RHS = Op.getOperand(1);
895 SDValue True = Op.getOperand(2);
896 SDValue False = Op.getOperand(3);
897 SDValue CC = Op.getOperand(4);
898 SDValue Temp;
899
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +0000900 if (VT == MVT::f32) {
901 DAGCombinerInfo DCI(DAG, AfterLegalizeVectorOps, true, nullptr);
902 SDValue MinMax = CombineFMinMaxLegacy(DL, VT, LHS, RHS, True, False, CC, DCI);
903 if (MinMax)
904 return MinMax;
905 }
906
Tom Stellard75aadc22012-12-11 21:25:42 +0000907 // LHS and RHS are guaranteed to be the same value type
908 EVT CompareVT = LHS.getValueType();
909
910 // Check if we can lower this to a native operation.
911
Tom Stellard2add82d2013-03-08 15:37:09 +0000912 // Try to lower to a SET* instruction:
913 //
914 // SET* can match the following patterns:
915 //
Tom Stellardcd428182013-09-28 02:50:38 +0000916 // select_cc f32, f32, -1, 0, cc_supported
917 // select_cc f32, f32, 1.0f, 0.0f, cc_supported
918 // select_cc i32, i32, -1, 0, cc_supported
Tom Stellard2add82d2013-03-08 15:37:09 +0000919 //
920
921 // Move hardware True/False values to the correct operand.
Tom Stellardcd428182013-09-28 02:50:38 +0000922 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
923 ISD::CondCode InverseCC =
924 ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
Tom Stellard5694d302013-09-28 02:50:43 +0000925 if (isHWTrueValue(False) && isHWFalseValue(True)) {
926 if (isCondCodeLegal(InverseCC, CompareVT.getSimpleVT())) {
927 std::swap(False, True);
928 CC = DAG.getCondCode(InverseCC);
929 } else {
930 ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InverseCC);
931 if (isCondCodeLegal(SwapInvCC, CompareVT.getSimpleVT())) {
932 std::swap(False, True);
933 std::swap(LHS, RHS);
934 CC = DAG.getCondCode(SwapInvCC);
935 }
936 }
Tom Stellard2add82d2013-03-08 15:37:09 +0000937 }
938
939 if (isHWTrueValue(True) && isHWFalseValue(False) &&
940 (CompareVT == VT || VT == MVT::i32)) {
941 // This can be matched by a SET* instruction.
942 return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
943 }
944
Tom Stellard75aadc22012-12-11 21:25:42 +0000945 // Try to lower to a CND* instruction:
Tom Stellard2add82d2013-03-08 15:37:09 +0000946 //
947 // CND* can match the following patterns:
948 //
Tom Stellardcd428182013-09-28 02:50:38 +0000949 // select_cc f32, 0.0, f32, f32, cc_supported
950 // select_cc f32, 0.0, i32, i32, cc_supported
951 // select_cc i32, 0, f32, f32, cc_supported
952 // select_cc i32, 0, i32, i32, cc_supported
Tom Stellard2add82d2013-03-08 15:37:09 +0000953 //
Tom Stellardcd428182013-09-28 02:50:38 +0000954
955 // Try to move the zero value to the RHS
956 if (isZero(LHS)) {
957 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
958 // Try swapping the operands
959 ISD::CondCode CCSwapped = ISD::getSetCCSwappedOperands(CCOpcode);
960 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
961 std::swap(LHS, RHS);
962 CC = DAG.getCondCode(CCSwapped);
963 } else {
964 // Try inverting the conditon and then swapping the operands
965 ISD::CondCode CCInv = ISD::getSetCCInverse(CCOpcode, CompareVT.isInteger());
966 CCSwapped = ISD::getSetCCSwappedOperands(CCInv);
967 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
968 std::swap(True, False);
969 std::swap(LHS, RHS);
970 CC = DAG.getCondCode(CCSwapped);
971 }
972 }
973 }
974 if (isZero(RHS)) {
975 SDValue Cond = LHS;
976 SDValue Zero = RHS;
Tom Stellard75aadc22012-12-11 21:25:42 +0000977 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
978 if (CompareVT != VT) {
979 // Bitcast True / False to the correct types. This will end up being
980 // a nop, but it allows us to define only a single pattern in the
981 // .TD files for each CND* instruction rather than having to have
982 // one pattern for integer True/False and one for fp True/False
983 True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
984 False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
985 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000986
987 switch (CCOpcode) {
988 case ISD::SETONE:
989 case ISD::SETUNE:
990 case ISD::SETNE:
Tom Stellard75aadc22012-12-11 21:25:42 +0000991 CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
992 Temp = True;
993 True = False;
994 False = Temp;
995 break;
996 default:
997 break;
998 }
999 SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
1000 Cond, Zero,
1001 True, False,
1002 DAG.getCondCode(CCOpcode));
1003 return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
1004 }
1005
Tom Stellard75aadc22012-12-11 21:25:42 +00001006 // If we make it this for it means we have no native instructions to handle
1007 // this SELECT_CC, so we must lower it.
1008 SDValue HWTrue, HWFalse;
1009
1010 if (CompareVT == MVT::f32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001011 HWTrue = DAG.getConstantFP(1.0f, DL, CompareVT);
1012 HWFalse = DAG.getConstantFP(0.0f, DL, CompareVT);
Tom Stellard75aadc22012-12-11 21:25:42 +00001013 } else if (CompareVT == MVT::i32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001014 HWTrue = DAG.getConstant(-1, DL, CompareVT);
1015 HWFalse = DAG.getConstant(0, DL, CompareVT);
Tom Stellard75aadc22012-12-11 21:25:42 +00001016 }
1017 else {
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +00001018 llvm_unreachable("Unhandled value type in LowerSELECT_CC");
Tom Stellard75aadc22012-12-11 21:25:42 +00001019 }
1020
1021 // Lower this unsupported SELECT_CC into a combination of two supported
1022 // SELECT_CC operations.
1023 SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
1024
1025 return DAG.getNode(ISD::SELECT_CC, DL, VT,
1026 Cond, HWFalse,
1027 True, False,
1028 DAG.getCondCode(ISD::SETNE));
1029}
1030
Alp Tokercb402912014-01-24 17:20:08 +00001031/// LLVM generates byte-addressed pointers. For indirect addressing, we need to
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001032/// convert these pointers to a register index. Each register holds
1033/// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
1034/// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
1035/// for indirect addressing.
1036SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
1037 unsigned StackWidth,
1038 SelectionDAG &DAG) const {
1039 unsigned SRLPad;
1040 switch(StackWidth) {
1041 case 1:
1042 SRLPad = 2;
1043 break;
1044 case 2:
1045 SRLPad = 3;
1046 break;
1047 case 4:
1048 SRLPad = 4;
1049 break;
1050 default: llvm_unreachable("Invalid stack width");
1051 }
1052
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001053 SDLoc DL(Ptr);
1054 return DAG.getNode(ISD::SRL, DL, Ptr.getValueType(), Ptr,
1055 DAG.getConstant(SRLPad, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001056}
1057
1058void R600TargetLowering::getStackAddress(unsigned StackWidth,
1059 unsigned ElemIdx,
1060 unsigned &Channel,
1061 unsigned &PtrIncr) const {
1062 switch (StackWidth) {
1063 default:
1064 case 1:
1065 Channel = 0;
1066 if (ElemIdx > 0) {
1067 PtrIncr = 1;
1068 } else {
1069 PtrIncr = 0;
1070 }
1071 break;
1072 case 2:
1073 Channel = ElemIdx % 2;
1074 if (ElemIdx == 2) {
1075 PtrIncr = 1;
1076 } else {
1077 PtrIncr = 0;
1078 }
1079 break;
1080 case 4:
1081 Channel = ElemIdx;
1082 PtrIncr = 0;
1083 break;
1084 }
1085}
1086
Matt Arsenault95245662016-02-11 05:32:46 +00001087SDValue R600TargetLowering::lowerPrivateTruncStore(StoreSDNode *Store,
1088 SelectionDAG &DAG) const {
1089 SDLoc DL(Store);
Tom Stellard75aadc22012-12-11 21:25:42 +00001090
Matt Arsenault95245662016-02-11 05:32:46 +00001091 unsigned Mask = 0;
1092 if (Store->getMemoryVT() == MVT::i8) {
1093 Mask = 0xff;
1094 } else if (Store->getMemoryVT() == MVT::i16) {
1095 Mask = 0xffff;
1096 }
1097
1098 SDValue Chain = Store->getChain();
1099 SDValue BasePtr = Store->getBasePtr();
1100 EVT MemVT = Store->getMemoryVT();
1101
1102 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, BasePtr,
1103 DAG.getConstant(2, DL, MVT::i32));
1104 SDValue Dst = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
1105 Chain, Ptr,
1106 DAG.getTargetConstant(0, DL, MVT::i32));
1107
1108 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, BasePtr,
1109 DAG.getConstant(0x3, DL, MVT::i32));
1110
1111 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1112 DAG.getConstant(3, DL, MVT::i32));
1113
1114 SDValue SExtValue = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i32,
1115 Store->getValue());
1116
1117 SDValue MaskedValue = DAG.getZeroExtendInReg(SExtValue, DL, MemVT);
1118
1119 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
1120 MaskedValue, ShiftAmt);
1121
1122 SDValue DstMask = DAG.getNode(ISD::SHL, DL, MVT::i32,
1123 DAG.getConstant(Mask, DL, MVT::i32),
1124 ShiftAmt);
1125 DstMask = DAG.getNode(ISD::XOR, DL, MVT::i32, DstMask,
1126 DAG.getConstant(0xffffffff, DL, MVT::i32));
1127 Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
1128
1129 SDValue Value = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
1130 return DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1131 Chain, Value, Ptr,
1132 DAG.getTargetConstant(0, DL, MVT::i32));
1133}
1134
1135SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault95245662016-02-11 05:32:46 +00001136 StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
1137 unsigned AS = StoreNode->getAddressSpace();
1138 SDValue Value = StoreNode->getValue();
1139 EVT ValueVT = Value.getValueType();
Jan Vesely00864882016-09-02 19:07:06 +00001140 EVT MemVT = StoreNode->getMemoryVT();
1141 unsigned Align = StoreNode->getAlignment();
Matt Arsenault95245662016-02-11 05:32:46 +00001142
1143 if ((AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::PRIVATE_ADDRESS) &&
1144 ValueVT.isVector()) {
1145 return SplitVectorStore(Op, DAG);
1146 }
1147
Jan Vesely00864882016-09-02 19:07:06 +00001148 // Private AS needs special fixes
1149 if (Align < MemVT.getStoreSize() && (AS != AMDGPUAS::PRIVATE_ADDRESS) &&
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001150 !allowsMisalignedMemoryAccesses(MemVT, AS, Align, nullptr)) {
Jan Vesely00864882016-09-02 19:07:06 +00001151 return expandUnalignedStore(StoreNode, DAG);
1152 }
1153
Matt Arsenault95245662016-02-11 05:32:46 +00001154 SDLoc DL(Op);
1155 SDValue Chain = StoreNode->getChain();
1156 SDValue Ptr = StoreNode->getBasePtr();
1157
1158 if (AS == AMDGPUAS::GLOBAL_ADDRESS) {
Jan Vesely00864882016-09-02 19:07:06 +00001159 // It is beneficial to create MSKOR here instead of combiner to avoid
1160 // artificial dependencies introduced by RMW
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001161 if (StoreNode->isTruncatingStore()) {
1162 EVT VT = Value.getValueType();
Tom Stellardfbab8272013-08-16 01:12:11 +00001163 assert(VT.bitsLE(MVT::i32));
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001164 SDValue MaskConstant;
1165 if (MemVT == MVT::i8) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001166 MaskConstant = DAG.getConstant(0xFF, DL, MVT::i32);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001167 } else {
1168 assert(MemVT == MVT::i16);
Jan Vesely00864882016-09-02 19:07:06 +00001169 assert(StoreNode->getAlignment() >= 2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001170 MaskConstant = DAG.getConstant(0xFFFF, DL, MVT::i32);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001171 }
1172 SDValue DWordAddr = DAG.getNode(ISD::SRL, DL, VT, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001173 DAG.getConstant(2, DL, MVT::i32));
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001174 SDValue ByteIndex = DAG.getNode(ISD::AND, DL, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001175 DAG.getConstant(0x00000003, DL, VT));
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001176 SDValue TruncValue = DAG.getNode(ISD::AND, DL, VT, Value, MaskConstant);
1177 SDValue Shift = DAG.getNode(ISD::SHL, DL, VT, ByteIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001178 DAG.getConstant(3, DL, VT));
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001179 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, VT, TruncValue, Shift);
1180 SDValue Mask = DAG.getNode(ISD::SHL, DL, VT, MaskConstant, Shift);
1181 // XXX: If we add a 64-bit ZW register class, then we could use a 2 x i32
1182 // vector instead.
1183 SDValue Src[4] = {
1184 ShiftedValue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001185 DAG.getConstant(0, DL, MVT::i32),
1186 DAG.getConstant(0, DL, MVT::i32),
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001187 Mask
1188 };
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001189 SDValue Input = DAG.getBuildVector(MVT::v4i32, DL, Src);
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001190 SDValue Args[3] = { Chain, Input, DWordAddr };
1191 return DAG.getMemIntrinsicNode(AMDGPUISD::STORE_MSKOR, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00001192 Op->getVTList(), Args, MemVT,
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001193 StoreNode->getMemOperand());
1194 } else if (Ptr->getOpcode() != AMDGPUISD::DWORDADDR &&
Matt Arsenault95245662016-02-11 05:32:46 +00001195 ValueVT.bitsGE(MVT::i32)) {
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001196 // Convert pointer from byte address to dword address.
1197 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, Ptr.getValueType(),
1198 DAG.getNode(ISD::SRL, DL, Ptr.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001199 Ptr, DAG.getConstant(2, DL, MVT::i32)));
Tom Stellard75aadc22012-12-11 21:25:42 +00001200
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001201 if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +00001202 llvm_unreachable("Truncated and indexed stores not supported yet");
Tom Stellardd3ee8c12013-08-16 01:12:06 +00001203 } else {
1204 Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
1205 }
1206 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001207 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001208 }
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001209
Matt Arsenault95245662016-02-11 05:32:46 +00001210 if (AS != AMDGPUAS::PRIVATE_ADDRESS)
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001211 return SDValue();
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001212
Matt Arsenault95245662016-02-11 05:32:46 +00001213 if (MemVT.bitsLT(MVT::i32))
1214 return lowerPrivateTruncStore(StoreNode, DAG);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001215
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00001216 // Lowering for indirect addressing
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001217 const MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001218 const R600FrameLowering *TFL = getSubtarget()->getFrameLowering();
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001219 unsigned StackWidth = TFL->getStackWidth(MF);
1220
1221 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1222
1223 if (ValueVT.isVector()) {
1224 unsigned NumElemVT = ValueVT.getVectorNumElements();
1225 EVT ElemVT = ValueVT.getVectorElementType();
Craig Topper48d114b2014-04-26 18:35:24 +00001226 SmallVector<SDValue, 4> Stores(NumElemVT);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001227
1228 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1229 "vector width in load");
1230
1231 for (unsigned i = 0; i < NumElemVT; ++i) {
1232 unsigned Channel, PtrIncr;
1233 getStackAddress(StackWidth, i, Channel, PtrIncr);
1234 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001235 DAG.getConstant(PtrIncr, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001236 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001237 Value, DAG.getConstant(i, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001238
1239 Stores[i] = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1240 Chain, Elem, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001241 DAG.getTargetConstant(Channel, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001242 }
Craig Topper48d114b2014-04-26 18:35:24 +00001243 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Stores);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001244 } else {
1245 if (ValueVT == MVT::i8) {
1246 Value = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Value);
1247 }
1248 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other, Chain, Value, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001249 DAG.getTargetConstant(0, DL, MVT::i32)); // Channel
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001250 }
1251
1252 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001253}
1254
Tom Stellard365366f2013-01-23 02:09:06 +00001255// return (512 + (kc_bank << 12)
1256static int
1257ConstantAddressBlock(unsigned AddressSpace) {
1258 switch (AddressSpace) {
1259 case AMDGPUAS::CONSTANT_BUFFER_0:
1260 return 512;
1261 case AMDGPUAS::CONSTANT_BUFFER_1:
1262 return 512 + 4096;
1263 case AMDGPUAS::CONSTANT_BUFFER_2:
1264 return 512 + 4096 * 2;
1265 case AMDGPUAS::CONSTANT_BUFFER_3:
1266 return 512 + 4096 * 3;
1267 case AMDGPUAS::CONSTANT_BUFFER_4:
1268 return 512 + 4096 * 4;
1269 case AMDGPUAS::CONSTANT_BUFFER_5:
1270 return 512 + 4096 * 5;
1271 case AMDGPUAS::CONSTANT_BUFFER_6:
1272 return 512 + 4096 * 6;
1273 case AMDGPUAS::CONSTANT_BUFFER_7:
1274 return 512 + 4096 * 7;
1275 case AMDGPUAS::CONSTANT_BUFFER_8:
1276 return 512 + 4096 * 8;
1277 case AMDGPUAS::CONSTANT_BUFFER_9:
1278 return 512 + 4096 * 9;
1279 case AMDGPUAS::CONSTANT_BUFFER_10:
1280 return 512 + 4096 * 10;
1281 case AMDGPUAS::CONSTANT_BUFFER_11:
1282 return 512 + 4096 * 11;
1283 case AMDGPUAS::CONSTANT_BUFFER_12:
1284 return 512 + 4096 * 12;
1285 case AMDGPUAS::CONSTANT_BUFFER_13:
1286 return 512 + 4096 * 13;
1287 case AMDGPUAS::CONSTANT_BUFFER_14:
1288 return 512 + 4096 * 14;
1289 case AMDGPUAS::CONSTANT_BUFFER_15:
1290 return 512 + 4096 * 15;
1291 default:
1292 return -1;
1293 }
1294}
1295
Matt Arsenault6dfda962016-02-10 18:21:39 +00001296SDValue R600TargetLowering::lowerPrivateExtLoad(SDValue Op,
1297 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001298 SDLoc DL(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001299 LoadSDNode *Load = cast<LoadSDNode>(Op);
1300 ISD::LoadExtType ExtType = Load->getExtensionType();
1301 EVT MemVT = Load->getMemoryVT();
Tom Stellard365366f2013-01-23 02:09:06 +00001302
Matt Arsenault6dfda962016-02-10 18:21:39 +00001303 // <SI && AS=PRIVATE && EXTLOAD && size < 32bit,
1304 // register (2-)byte extract.
1305
1306 // Get Register holding the target.
1307 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Load->getBasePtr(),
1308 DAG.getConstant(2, DL, MVT::i32));
1309 // Load the Register.
1310 SDValue Ret = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, Op.getValueType(),
1311 Load->getChain(),
1312 Ptr,
1313 DAG.getTargetConstant(0, DL, MVT::i32),
1314 Op.getOperand(2));
1315
1316 // Get offset within the register.
1317 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32,
1318 Load->getBasePtr(),
1319 DAG.getConstant(0x3, DL, MVT::i32));
1320
1321 // Bit offset of target byte (byteIdx * 8).
1322 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1323 DAG.getConstant(3, DL, MVT::i32));
1324
1325 // Shift to the right.
1326 Ret = DAG.getNode(ISD::SRL, DL, MVT::i32, Ret, ShiftAmt);
1327
1328 // Eliminate the upper bits by setting them to ...
1329 EVT MemEltVT = MemVT.getScalarType();
1330
1331 // ... ones.
1332 if (ExtType == ISD::SEXTLOAD) {
1333 SDValue MemEltVTNode = DAG.getValueType(MemEltVT);
1334
1335 SDValue Ops[] = {
1336 DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, Ret, MemEltVTNode),
1337 Load->getChain()
1338 };
1339
1340 return DAG.getMergeValues(Ops, DL);
1341 }
1342
1343 // ... or zeros.
1344 SDValue Ops[] = {
1345 DAG.getZeroExtendInReg(Ret, DL, MemEltVT),
1346 Load->getChain()
1347 };
1348
1349 return DAG.getMergeValues(Ops, DL);
1350}
1351
1352SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1353 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1354 unsigned AS = LoadNode->getAddressSpace();
1355 EVT MemVT = LoadNode->getMemoryVT();
1356 ISD::LoadExtType ExtType = LoadNode->getExtensionType();
1357
1358 if (AS == AMDGPUAS::PRIVATE_ADDRESS &&
1359 ExtType != ISD::NON_EXTLOAD && MemVT.bitsLT(MVT::i32)) {
1360 return lowerPrivateExtLoad(Op, DAG);
1361 }
1362
1363 SDLoc DL(Op);
1364 EVT VT = Op.getValueType();
1365 SDValue Chain = LoadNode->getChain();
1366 SDValue Ptr = LoadNode->getBasePtr();
Tom Stellarde9373602014-01-22 19:24:14 +00001367
Tom Stellard35bb18c2013-08-26 15:06:04 +00001368 if (LoadNode->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS && VT.isVector()) {
1369 SDValue MergedValues[2] = {
Matt Arsenault9c499c32016-04-14 23:31:26 +00001370 scalarizeVectorLoad(LoadNode, DAG),
Tom Stellard35bb18c2013-08-26 15:06:04 +00001371 Chain
1372 };
Craig Topper64941d92014-04-27 19:20:57 +00001373 return DAG.getMergeValues(MergedValues, DL);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001374 }
1375
Tom Stellard365366f2013-01-23 02:09:06 +00001376 int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
Matt Arsenault00a0d6f2013-11-13 02:39:07 +00001377 if (ConstantBlock > -1 &&
1378 ((LoadNode->getExtensionType() == ISD::NON_EXTLOAD) ||
1379 (LoadNode->getExtensionType() == ISD::ZEXTLOAD))) {
Tom Stellard365366f2013-01-23 02:09:06 +00001380 SDValue Result;
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001381 if (isa<ConstantExpr>(LoadNode->getMemOperand()->getValue()) ||
1382 isa<Constant>(LoadNode->getMemOperand()->getValue()) ||
Matt Arsenaultef1a9502013-11-01 17:39:26 +00001383 isa<ConstantSDNode>(Ptr)) {
Tom Stellard365366f2013-01-23 02:09:06 +00001384 SDValue Slots[4];
1385 for (unsigned i = 0; i < 4; i++) {
1386 // We want Const position encoded with the following formula :
1387 // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
1388 // const_index is Ptr computed by llvm using an alignment of 16.
1389 // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
1390 // then div by 4 at the ISel step
1391 SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001392 DAG.getConstant(4 * i + ConstantBlock * 16, DL, MVT::i32));
Tom Stellard365366f2013-01-23 02:09:06 +00001393 Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
1394 }
Tom Stellard0344cdf2013-08-01 15:23:42 +00001395 EVT NewVT = MVT::v4i32;
1396 unsigned NumElements = 4;
1397 if (VT.isVector()) {
1398 NewVT = VT;
1399 NumElements = VT.getVectorNumElements();
1400 }
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001401 Result = DAG.getBuildVector(NewVT, DL, makeArrayRef(Slots, NumElements));
Tom Stellard365366f2013-01-23 02:09:06 +00001402 } else {
Alp Tokerf907b892013-12-05 05:44:44 +00001403 // non-constant ptr can't be folded, keeps it as a v4f32 load
Tom Stellard365366f2013-01-23 02:09:06 +00001404 Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001405 DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr,
1406 DAG.getConstant(4, DL, MVT::i32)),
1407 DAG.getConstant(LoadNode->getAddressSpace() -
1408 AMDGPUAS::CONSTANT_BUFFER_0, DL, MVT::i32)
Tom Stellard365366f2013-01-23 02:09:06 +00001409 );
1410 }
1411
1412 if (!VT.isVector()) {
1413 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001414 DAG.getConstant(0, DL, MVT::i32));
Tom Stellard365366f2013-01-23 02:09:06 +00001415 }
1416
1417 SDValue MergedValues[2] = {
Matt Arsenault7939acd2014-04-07 16:44:24 +00001418 Result,
1419 Chain
Tom Stellard365366f2013-01-23 02:09:06 +00001420 };
Craig Topper64941d92014-04-27 19:20:57 +00001421 return DAG.getMergeValues(MergedValues, DL);
Tom Stellard365366f2013-01-23 02:09:06 +00001422 }
1423
Matt Arsenault6dfda962016-02-10 18:21:39 +00001424 SDValue LoweredLoad;
1425
Matt Arsenault909d0c02013-10-30 23:43:29 +00001426 // For most operations returning SDValue() will result in the node being
1427 // expanded by the DAG Legalizer. This is not the case for ISD::LOAD, so we
1428 // need to manually expand loads that may be legal in some address spaces and
1429 // illegal in others. SEXT loads from CONSTANT_BUFFER_0 are supported for
1430 // compute shaders, since the data is sign extended when it is uploaded to the
1431 // buffer. However SEXT loads from other address spaces are not supported, so
1432 // we need to expand them here.
Tom Stellard84021442013-07-23 01:48:24 +00001433 if (LoadNode->getExtensionType() == ISD::SEXTLOAD) {
1434 EVT MemVT = LoadNode->getMemoryVT();
1435 assert(!MemVT.isVector() && (MemVT == MVT::i16 || MemVT == MVT::i8));
Justin Lebar9c375812016-07-15 18:27:10 +00001436 SDValue NewLoad = DAG.getExtLoad(
1437 ISD::EXTLOAD, DL, VT, Chain, Ptr, LoadNode->getPointerInfo(), MemVT,
1438 LoadNode->getAlignment(), LoadNode->getMemOperand()->getFlags());
Jan Veselyb670d372015-05-26 18:07:22 +00001439 SDValue Res = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, NewLoad,
1440 DAG.getValueType(MemVT));
Tom Stellard84021442013-07-23 01:48:24 +00001441
Jan Veselyb670d372015-05-26 18:07:22 +00001442 SDValue MergedValues[2] = { Res, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00001443 return DAG.getMergeValues(MergedValues, DL);
Tom Stellard84021442013-07-23 01:48:24 +00001444 }
1445
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001446 if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1447 return SDValue();
1448 }
1449
1450 // Lowering for indirect addressing
1451 const MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001452 const R600FrameLowering *TFL = getSubtarget()->getFrameLowering();
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001453 unsigned StackWidth = TFL->getStackWidth(MF);
1454
1455 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1456
1457 if (VT.isVector()) {
1458 unsigned NumElemVT = VT.getVectorNumElements();
1459 EVT ElemVT = VT.getVectorElementType();
1460 SDValue Loads[4];
1461
Jan Vesely687ca8d2016-05-16 23:56:32 +00001462 assert(NumElemVT <= 4);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001463 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1464 "vector width in load");
1465
1466 for (unsigned i = 0; i < NumElemVT; ++i) {
1467 unsigned Channel, PtrIncr;
1468 getStackAddress(StackWidth, i, Channel, PtrIncr);
1469 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001470 DAG.getConstant(PtrIncr, DL, MVT::i32));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001471 Loads[i] = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, ElemVT,
1472 Chain, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001473 DAG.getTargetConstant(Channel, DL, MVT::i32),
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001474 Op.getOperand(2));
1475 }
Jan Vesely687ca8d2016-05-16 23:56:32 +00001476 EVT TargetVT = EVT::getVectorVT(*DAG.getContext(), ElemVT, NumElemVT);
1477 LoweredLoad = DAG.getBuildVector(TargetVT, DL, makeArrayRef(Loads, NumElemVT));
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001478 } else {
1479 LoweredLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, VT,
1480 Chain, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001481 DAG.getTargetConstant(0, DL, MVT::i32), // Channel
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001482 Op.getOperand(2));
1483 }
1484
Matt Arsenault7939acd2014-04-07 16:44:24 +00001485 SDValue Ops[2] = {
1486 LoweredLoad,
1487 Chain
1488 };
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001489
Craig Topper64941d92014-04-27 19:20:57 +00001490 return DAG.getMergeValues(Ops, DL);
Tom Stellard365366f2013-01-23 02:09:06 +00001491}
Tom Stellard75aadc22012-12-11 21:25:42 +00001492
Matt Arsenault1d555c42014-06-23 18:00:55 +00001493SDValue R600TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
1494 SDValue Chain = Op.getOperand(0);
1495 SDValue Cond = Op.getOperand(1);
1496 SDValue Jump = Op.getOperand(2);
1497
1498 return DAG.getNode(AMDGPUISD::BRANCH_COND, SDLoc(Op), Op.getValueType(),
1499 Chain, Jump, Cond);
1500}
1501
Matt Arsenault81d06012016-03-07 21:10:13 +00001502SDValue R600TargetLowering::lowerFrameIndex(SDValue Op,
1503 SelectionDAG &DAG) const {
1504 MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001505 const R600FrameLowering *TFL = getSubtarget()->getFrameLowering();
Matt Arsenault81d06012016-03-07 21:10:13 +00001506
1507 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op);
1508
1509 unsigned FrameIndex = FIN->getIndex();
1510 unsigned IgnoredFrameReg;
1511 unsigned Offset =
1512 TFL->getFrameIndexReference(MF, FrameIndex, IgnoredFrameReg);
1513 return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF), SDLoc(Op),
1514 Op.getValueType());
1515}
1516
Tom Stellard75aadc22012-12-11 21:25:42 +00001517/// XXX Only kernel functions are supported, so we can assume for now that
1518/// every function is a kernel function, but in the future we should use
1519/// separate calling conventions for kernel and non-kernel functions.
1520SDValue R600TargetLowering::LowerFormalArguments(
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001521 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1522 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
1523 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Tom Stellardacfeebf2013-07-23 01:48:05 +00001524 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001525 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1526 *DAG.getContext());
Vincent Lejeunef143af32013-11-11 22:10:24 +00001527 MachineFunction &MF = DAG.getMachineFunction();
Jan Veselye5121f32014-10-14 20:05:26 +00001528 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
Tom Stellardacfeebf2013-07-23 01:48:05 +00001529
Tom Stellardaf775432013-10-23 00:44:32 +00001530 SmallVector<ISD::InputArg, 8> LocalIns;
1531
Tom Stellardbbeb45a2016-09-16 21:53:00 +00001532 if (AMDGPU::isShader(CallConv)) {
1533 AnalyzeFormalArguments(CCInfo, Ins);
1534 } else {
1535 analyzeFormalArgumentsCompute(CCInfo, Ins);
1536 }
Tom Stellardacfeebf2013-07-23 01:48:05 +00001537
Tom Stellard1e803092013-07-23 01:48:18 +00001538 for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
Tom Stellardacfeebf2013-07-23 01:48:05 +00001539 CCValAssign &VA = ArgLocs[i];
Matt Arsenault74ef2772014-08-13 18:14:11 +00001540 const ISD::InputArg &In = Ins[i];
1541 EVT VT = In.VT;
1542 EVT MemVT = VA.getLocVT();
1543 if (!VT.isVector() && MemVT.isVector()) {
1544 // Get load source type if scalarized.
1545 MemVT = MemVT.getVectorElementType();
1546 }
Tom Stellard78e01292013-07-23 01:47:58 +00001547
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001548 if (AMDGPU::isShader(CallConv)) {
Vincent Lejeunef143af32013-11-11 22:10:24 +00001549 unsigned Reg = MF.addLiveIn(VA.getLocReg(), &AMDGPU::R600_Reg128RegClass);
1550 SDValue Register = DAG.getCopyFromReg(Chain, DL, Reg, VT);
1551 InVals.push_back(Register);
1552 continue;
1553 }
1554
Tom Stellard75aadc22012-12-11 21:25:42 +00001555 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
Matt Arsenault74ef2772014-08-13 18:14:11 +00001556 AMDGPUAS::CONSTANT_BUFFER_0);
Tom Stellardacfeebf2013-07-23 01:48:05 +00001557
Matt Arsenaultfae02982014-03-17 18:58:11 +00001558 // i64 isn't a legal type, so the register type used ends up as i32, which
1559 // isn't expected here. It attempts to create this sextload, but it ends up
1560 // being invalid. Somehow this seems to work with i64 arguments, but breaks
1561 // for <1 x i64>.
1562
Tom Stellardacfeebf2013-07-23 01:48:05 +00001563 // The first 36 bytes of the input buffer contains information about
1564 // thread group and global sizes.
Matt Arsenault74ef2772014-08-13 18:14:11 +00001565 ISD::LoadExtType Ext = ISD::NON_EXTLOAD;
1566 if (MemVT.getScalarSizeInBits() != VT.getScalarSizeInBits()) {
1567 // FIXME: This should really check the extload type, but the handling of
1568 // extload vector parameters seems to be broken.
Matt Arsenaulte1f030c2014-04-11 20:59:54 +00001569
Matt Arsenault74ef2772014-08-13 18:14:11 +00001570 // Ext = In.Flags.isSExt() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
1571 Ext = ISD::SEXTLOAD;
1572 }
1573
1574 // Compute the offset from the value.
1575 // XXX - I think PartOffset should give you this, but it seems to give the
1576 // size of the register which isn't useful.
1577
Andrew Trick05938a52015-02-16 18:10:47 +00001578 unsigned ValBase = ArgLocs[In.getOrigArgIndex()].getLocMemOffset();
Matt Arsenault74ef2772014-08-13 18:14:11 +00001579 unsigned PartOffset = VA.getLocMemOffset();
Matt Arsenault52ef4012016-07-26 16:45:58 +00001580 unsigned Offset = Subtarget->getExplicitKernelArgOffset() + VA.getLocMemOffset();
Matt Arsenault74ef2772014-08-13 18:14:11 +00001581
1582 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy), PartOffset - ValBase);
Justin Lebar9c375812016-07-15 18:27:10 +00001583 SDValue Arg = DAG.getLoad(
1584 ISD::UNINDEXED, Ext, VT, DL, Chain,
1585 DAG.getConstant(Offset, DL, MVT::i32), DAG.getUNDEF(MVT::i32), PtrInfo,
Justin Lebaradbf09e2016-09-11 01:38:58 +00001586 MemVT, /* Alignment = */ 4, MachineMemOperand::MONonTemporal |
1587 MachineMemOperand::MODereferenceable |
1588 MachineMemOperand::MOInvariant);
Matt Arsenault209a7b92014-04-18 07:40:20 +00001589
1590 // 4 is the preferred alignment for the CONSTANT memory space.
Tom Stellard75aadc22012-12-11 21:25:42 +00001591 InVals.push_back(Arg);
Matt Arsenault52ef4012016-07-26 16:45:58 +00001592 MFI->setABIArgOffset(Offset + MemVT.getStoreSize());
Tom Stellard75aadc22012-12-11 21:25:42 +00001593 }
1594 return Chain;
1595}
1596
Mehdi Amini44ede332015-07-09 02:09:04 +00001597EVT R600TargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1598 EVT VT) const {
Matt Arsenault209a7b92014-04-18 07:40:20 +00001599 if (!VT.isVector())
1600 return MVT::i32;
Tom Stellard75aadc22012-12-11 21:25:42 +00001601 return VT.changeVectorElementTypeToInteger();
1602}
1603
Matt Arsenaultfa67bdb2016-02-22 21:04:16 +00001604bool R600TargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
1605 unsigned AddrSpace,
1606 unsigned Align,
1607 bool *IsFast) const {
1608 if (IsFast)
1609 *IsFast = false;
1610
1611 if (!VT.isSimple() || VT == MVT::Other)
1612 return false;
1613
1614 if (VT.bitsLT(MVT::i32))
1615 return false;
1616
1617 // TODO: This is a rough estimate.
1618 if (IsFast)
1619 *IsFast = true;
1620
1621 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
1622}
1623
Matt Arsenault209a7b92014-04-18 07:40:20 +00001624static SDValue CompactSwizzlableVector(
1625 SelectionDAG &DAG, SDValue VectorEntry,
1626 DenseMap<unsigned, unsigned> &RemapSwizzle) {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001627 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1628 assert(RemapSwizzle.empty());
1629 SDValue NewBldVec[4] = {
Matt Arsenault209a7b92014-04-18 07:40:20 +00001630 VectorEntry.getOperand(0),
1631 VectorEntry.getOperand(1),
1632 VectorEntry.getOperand(2),
1633 VectorEntry.getOperand(3)
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001634 };
1635
1636 for (unsigned i = 0; i < 4; i++) {
Sanjay Patel57195842016-03-14 17:28:46 +00001637 if (NewBldVec[i].isUndef())
Vincent Lejeunefa58a5f2013-10-13 17:56:10 +00001638 // We mask write here to teach later passes that the ith element of this
1639 // vector is undef. Thus we can use it to reduce 128 bits reg usage,
1640 // break false dependencies and additionnaly make assembly easier to read.
1641 RemapSwizzle[i] = 7; // SEL_MASK_WRITE
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001642 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(NewBldVec[i])) {
1643 if (C->isZero()) {
1644 RemapSwizzle[i] = 4; // SEL_0
1645 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1646 } else if (C->isExactlyValue(1.0)) {
1647 RemapSwizzle[i] = 5; // SEL_1
1648 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1649 }
1650 }
1651
Sanjay Patel57195842016-03-14 17:28:46 +00001652 if (NewBldVec[i].isUndef())
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001653 continue;
1654 for (unsigned j = 0; j < i; j++) {
1655 if (NewBldVec[i] == NewBldVec[j]) {
1656 NewBldVec[i] = DAG.getUNDEF(NewBldVec[i].getValueType());
1657 RemapSwizzle[i] = j;
1658 break;
1659 }
1660 }
1661 }
1662
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001663 return DAG.getBuildVector(VectorEntry.getValueType(), SDLoc(VectorEntry),
1664 NewBldVec);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001665}
1666
Benjamin Kramer193960c2013-06-11 13:32:25 +00001667static SDValue ReorganizeVector(SelectionDAG &DAG, SDValue VectorEntry,
1668 DenseMap<unsigned, unsigned> &RemapSwizzle) {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001669 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1670 assert(RemapSwizzle.empty());
1671 SDValue NewBldVec[4] = {
1672 VectorEntry.getOperand(0),
1673 VectorEntry.getOperand(1),
1674 VectorEntry.getOperand(2),
1675 VectorEntry.getOperand(3)
1676 };
1677 bool isUnmovable[4] = { false, false, false, false };
Vincent Lejeunecc0ea742013-12-10 14:43:31 +00001678 for (unsigned i = 0; i < 4; i++) {
Vincent Lejeuneb8aac8d2013-07-09 15:03:25 +00001679 RemapSwizzle[i] = i;
Vincent Lejeunecc0ea742013-12-10 14:43:31 +00001680 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1681 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1682 ->getZExtValue();
1683 if (i == Idx)
1684 isUnmovable[Idx] = true;
1685 }
1686 }
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001687
1688 for (unsigned i = 0; i < 4; i++) {
1689 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1690 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1691 ->getZExtValue();
Vincent Lejeune301beb82013-10-13 17:56:04 +00001692 if (isUnmovable[Idx])
1693 continue;
1694 // Swap i and Idx
1695 std::swap(NewBldVec[Idx], NewBldVec[i]);
1696 std::swap(RemapSwizzle[i], RemapSwizzle[Idx]);
1697 break;
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001698 }
1699 }
1700
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001701 return DAG.getBuildVector(VectorEntry.getValueType(), SDLoc(VectorEntry),
1702 NewBldVec);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001703}
1704
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001705SDValue R600TargetLowering::OptimizeSwizzle(SDValue BuildVector, SDValue Swz[4],
1706 SelectionDAG &DAG,
1707 const SDLoc &DL) const {
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001708 assert(BuildVector.getOpcode() == ISD::BUILD_VECTOR);
1709 // Old -> New swizzle values
1710 DenseMap<unsigned, unsigned> SwizzleRemap;
1711
1712 BuildVector = CompactSwizzlableVector(DAG, BuildVector, SwizzleRemap);
1713 for (unsigned i = 0; i < 4; i++) {
Benjamin Kramer619c4e52015-04-10 11:24:51 +00001714 unsigned Idx = cast<ConstantSDNode>(Swz[i])->getZExtValue();
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001715 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001716 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], DL, MVT::i32);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001717 }
1718
1719 SwizzleRemap.clear();
1720 BuildVector = ReorganizeVector(DAG, BuildVector, SwizzleRemap);
1721 for (unsigned i = 0; i < 4; i++) {
Benjamin Kramer619c4e52015-04-10 11:24:51 +00001722 unsigned Idx = cast<ConstantSDNode>(Swz[i])->getZExtValue();
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001723 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001724 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], DL, MVT::i32);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001725 }
1726
1727 return BuildVector;
1728}
1729
Tom Stellard75aadc22012-12-11 21:25:42 +00001730//===----------------------------------------------------------------------===//
1731// Custom DAG Optimizations
1732//===----------------------------------------------------------------------===//
1733
1734SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
1735 DAGCombinerInfo &DCI) const {
1736 SelectionDAG &DAG = DCI.DAG;
Jan Vesely89876672016-08-29 23:21:46 +00001737 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00001738
1739 switch (N->getOpcode()) {
1740 // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
1741 case ISD::FP_ROUND: {
1742 SDValue Arg = N->getOperand(0);
1743 if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
Jan Vesely89876672016-08-29 23:21:46 +00001744 return DAG.getNode(ISD::UINT_TO_FP, DL, N->getValueType(0),
Tom Stellard75aadc22012-12-11 21:25:42 +00001745 Arg.getOperand(0));
1746 }
1747 break;
1748 }
Tom Stellarde06163a2013-02-07 14:02:35 +00001749
1750 // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
1751 // (i32 select_cc f32, f32, -1, 0 cc)
1752 //
1753 // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1754 // this to one of the SET*_DX10 instructions.
1755 case ISD::FP_TO_SINT: {
1756 SDValue FNeg = N->getOperand(0);
1757 if (FNeg.getOpcode() != ISD::FNEG) {
1758 return SDValue();
1759 }
1760 SDValue SelectCC = FNeg.getOperand(0);
1761 if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1762 SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1763 SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1764 !isHWTrueValue(SelectCC.getOperand(2)) ||
1765 !isHWFalseValue(SelectCC.getOperand(3))) {
1766 return SDValue();
1767 }
1768
Jan Vesely89876672016-08-29 23:21:46 +00001769 return DAG.getNode(ISD::SELECT_CC, DL, N->getValueType(0),
Tom Stellarde06163a2013-02-07 14:02:35 +00001770 SelectCC.getOperand(0), // LHS
1771 SelectCC.getOperand(1), // RHS
Jan Vesely89876672016-08-29 23:21:46 +00001772 DAG.getConstant(-1, DL, MVT::i32), // True
1773 DAG.getConstant(0, DL, MVT::i32), // False
Tom Stellarde06163a2013-02-07 14:02:35 +00001774 SelectCC.getOperand(4)); // CC
1775
1776 break;
1777 }
Quentin Colombete2e05482013-07-30 00:27:16 +00001778
NAKAMURA Takumi8a046432013-10-28 04:07:38 +00001779 // insert_vector_elt (build_vector elt0, ... , eltN), NewEltIdx, idx
1780 // => build_vector elt0, ... , NewEltIdx, ... , eltN
Quentin Colombete2e05482013-07-30 00:27:16 +00001781 case ISD::INSERT_VECTOR_ELT: {
1782 SDValue InVec = N->getOperand(0);
1783 SDValue InVal = N->getOperand(1);
1784 SDValue EltNo = N->getOperand(2);
Quentin Colombete2e05482013-07-30 00:27:16 +00001785
1786 // If the inserted element is an UNDEF, just use the input vector.
Sanjay Patel57195842016-03-14 17:28:46 +00001787 if (InVal.isUndef())
Quentin Colombete2e05482013-07-30 00:27:16 +00001788 return InVec;
1789
1790 EVT VT = InVec.getValueType();
1791
1792 // If we can't generate a legal BUILD_VECTOR, exit
1793 if (!isOperationLegal(ISD::BUILD_VECTOR, VT))
1794 return SDValue();
1795
1796 // Check that we know which element is being inserted
1797 if (!isa<ConstantSDNode>(EltNo))
1798 return SDValue();
1799 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
1800
1801 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
1802 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
1803 // vector elements.
1804 SmallVector<SDValue, 8> Ops;
1805 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
1806 Ops.append(InVec.getNode()->op_begin(),
1807 InVec.getNode()->op_end());
Sanjay Patel57195842016-03-14 17:28:46 +00001808 } else if (InVec.isUndef()) {
Quentin Colombete2e05482013-07-30 00:27:16 +00001809 unsigned NElts = VT.getVectorNumElements();
1810 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
1811 } else {
1812 return SDValue();
1813 }
1814
1815 // Insert the element
1816 if (Elt < Ops.size()) {
1817 // All the operands of BUILD_VECTOR must have the same type;
1818 // we enforce that here.
1819 EVT OpVT = Ops[0].getValueType();
1820 if (InVal.getValueType() != OpVT)
1821 InVal = OpVT.bitsGT(InVal.getValueType()) ?
Jan Vesely89876672016-08-29 23:21:46 +00001822 DAG.getNode(ISD::ANY_EXTEND, DL, OpVT, InVal) :
1823 DAG.getNode(ISD::TRUNCATE, DL, OpVT, InVal);
Quentin Colombete2e05482013-07-30 00:27:16 +00001824 Ops[Elt] = InVal;
1825 }
1826
1827 // Return the new vector
Jan Vesely89876672016-08-29 23:21:46 +00001828 return DAG.getBuildVector(VT, DL, Ops);
Quentin Colombete2e05482013-07-30 00:27:16 +00001829 }
1830
Tom Stellard365366f2013-01-23 02:09:06 +00001831 // Extract_vec (Build_vector) generated by custom lowering
1832 // also needs to be customly combined
1833 case ISD::EXTRACT_VECTOR_ELT: {
1834 SDValue Arg = N->getOperand(0);
1835 if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1836 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1837 unsigned Element = Const->getZExtValue();
1838 return Arg->getOperand(Element);
1839 }
1840 }
Tom Stellarddd04c832013-01-31 22:11:53 +00001841 if (Arg.getOpcode() == ISD::BITCAST &&
Jan Veselyea457462016-09-02 20:13:19 +00001842 Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
1843 (Arg.getOperand(0).getValueType().getVectorNumElements() ==
1844 Arg.getValueType().getVectorNumElements())) {
Tom Stellarddd04c832013-01-31 22:11:53 +00001845 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1846 unsigned Element = Const->getZExtValue();
Jan Vesely89876672016-08-29 23:21:46 +00001847 return DAG.getNode(ISD::BITCAST, DL, N->getVTList(),
1848 Arg->getOperand(0).getOperand(Element));
Tom Stellarddd04c832013-01-31 22:11:53 +00001849 }
1850 }
Mehdi Aminie029eae2015-07-16 06:23:12 +00001851 break;
Tom Stellard365366f2013-01-23 02:09:06 +00001852 }
Tom Stellarde06163a2013-02-07 14:02:35 +00001853
1854 case ISD::SELECT_CC: {
Tom Stellardafa8b532014-05-09 16:42:16 +00001855 // Try common optimizations
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00001856 if (SDValue Ret = AMDGPUTargetLowering::PerformDAGCombine(N, DCI))
Tom Stellardafa8b532014-05-09 16:42:16 +00001857 return Ret;
1858
Tom Stellarde06163a2013-02-07 14:02:35 +00001859 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1860 // selectcc x, y, a, b, inv(cc)
Tom Stellard5e524892013-03-08 15:37:11 +00001861 //
1862 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, setne ->
1863 // selectcc x, y, a, b, cc
Tom Stellarde06163a2013-02-07 14:02:35 +00001864 SDValue LHS = N->getOperand(0);
1865 if (LHS.getOpcode() != ISD::SELECT_CC) {
1866 return SDValue();
1867 }
1868
1869 SDValue RHS = N->getOperand(1);
1870 SDValue True = N->getOperand(2);
1871 SDValue False = N->getOperand(3);
Tom Stellard5e524892013-03-08 15:37:11 +00001872 ISD::CondCode NCC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Tom Stellarde06163a2013-02-07 14:02:35 +00001873
1874 if (LHS.getOperand(2).getNode() != True.getNode() ||
1875 LHS.getOperand(3).getNode() != False.getNode() ||
Tom Stellard5e524892013-03-08 15:37:11 +00001876 RHS.getNode() != False.getNode()) {
Tom Stellarde06163a2013-02-07 14:02:35 +00001877 return SDValue();
1878 }
1879
Tom Stellard5e524892013-03-08 15:37:11 +00001880 switch (NCC) {
1881 default: return SDValue();
1882 case ISD::SETNE: return LHS;
1883 case ISD::SETEQ: {
1884 ISD::CondCode LHSCC = cast<CondCodeSDNode>(LHS.getOperand(4))->get();
1885 LHSCC = ISD::getSetCCInverse(LHSCC,
1886 LHS.getOperand(0).getValueType().isInteger());
Tom Stellardcd428182013-09-28 02:50:38 +00001887 if (DCI.isBeforeLegalizeOps() ||
1888 isCondCodeLegal(LHSCC, LHS.getOperand(0).getSimpleValueType()))
Jan Vesely89876672016-08-29 23:21:46 +00001889 return DAG.getSelectCC(DL,
Tom Stellardcd428182013-09-28 02:50:38 +00001890 LHS.getOperand(0),
1891 LHS.getOperand(1),
1892 LHS.getOperand(2),
1893 LHS.getOperand(3),
1894 LHSCC);
1895 break;
Vincent Lejeuned80bc152013-02-14 16:55:06 +00001896 }
Tom Stellard5e524892013-03-08 15:37:11 +00001897 }
Tom Stellardcd428182013-09-28 02:50:38 +00001898 return SDValue();
Tom Stellard5e524892013-03-08 15:37:11 +00001899 }
Tom Stellardfbab8272013-08-16 01:12:11 +00001900
Matt Arsenault7bee6ac2016-12-05 20:23:10 +00001901 case AMDGPUISD::R600_EXPORT: {
Vincent Lejeuned80bc152013-02-14 16:55:06 +00001902 SDValue Arg = N->getOperand(1);
1903 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1904 break;
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001905
Vincent Lejeuned80bc152013-02-14 16:55:06 +00001906 SDValue NewArgs[8] = {
1907 N->getOperand(0), // Chain
1908 SDValue(),
1909 N->getOperand(2), // ArrayBase
1910 N->getOperand(3), // Type
1911 N->getOperand(4), // SWZ_X
1912 N->getOperand(5), // SWZ_Y
1913 N->getOperand(6), // SWZ_Z
1914 N->getOperand(7) // SWZ_W
1915 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001916 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[4], DAG, DL);
Matt Arsenault7bee6ac2016-12-05 20:23:10 +00001917 return DAG.getNode(AMDGPUISD::R600_EXPORT, DL, N->getVTList(), NewArgs);
Tom Stellarde06163a2013-02-07 14:02:35 +00001918 }
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001919 case AMDGPUISD::TEXTURE_FETCH: {
1920 SDValue Arg = N->getOperand(1);
1921 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1922 break;
1923
1924 SDValue NewArgs[19] = {
1925 N->getOperand(0),
1926 N->getOperand(1),
1927 N->getOperand(2),
1928 N->getOperand(3),
1929 N->getOperand(4),
1930 N->getOperand(5),
1931 N->getOperand(6),
1932 N->getOperand(7),
1933 N->getOperand(8),
1934 N->getOperand(9),
1935 N->getOperand(10),
1936 N->getOperand(11),
1937 N->getOperand(12),
1938 N->getOperand(13),
1939 N->getOperand(14),
1940 N->getOperand(15),
1941 N->getOperand(16),
1942 N->getOperand(17),
1943 N->getOperand(18),
1944 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001945 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[2], DAG, DL);
1946 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, N->getVTList(), NewArgs);
Vincent Lejeune276ceb82013-06-04 15:04:53 +00001947 }
Jan Vesely89876672016-08-29 23:21:46 +00001948 default: break;
Tom Stellard75aadc22012-12-11 21:25:42 +00001949 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00001950
1951 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001952}
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001953
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001954bool R600TargetLowering::FoldOperand(SDNode *ParentNode, unsigned SrcIdx,
1955 SDValue &Src, SDValue &Neg, SDValue &Abs,
1956 SDValue &Sel, SDValue &Imm,
1957 SelectionDAG &DAG) const {
1958 const R600InstrInfo *TII = getSubtarget()->getInstrInfo();
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001959 if (!Src.isMachineOpcode())
1960 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001961
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001962 switch (Src.getMachineOpcode()) {
1963 case AMDGPU::FNEG_R600:
1964 if (!Neg.getNode())
1965 return false;
1966 Src = Src.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001967 Neg = DAG.getTargetConstant(1, SDLoc(ParentNode), MVT::i32);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001968 return true;
1969 case AMDGPU::FABS_R600:
1970 if (!Abs.getNode())
1971 return false;
1972 Src = Src.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001973 Abs = DAG.getTargetConstant(1, SDLoc(ParentNode), MVT::i32);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00001974 return true;
1975 case AMDGPU::CONST_COPY: {
1976 unsigned Opcode = ParentNode->getMachineOpcode();
1977 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
1978
1979 if (!Sel.getNode())
1980 return false;
1981
1982 SDValue CstOffset = Src.getOperand(0);
1983 if (ParentNode->getValueType(0).isVector())
1984 return false;
1985
1986 // Gather constants values
1987 int SrcIndices[] = {
1988 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
1989 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
1990 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2),
1991 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
1992 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
1993 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
1994 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
1995 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
1996 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
1997 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
1998 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
1999 };
2000 std::vector<unsigned> Consts;
Matt Arsenault4d64f962014-05-12 19:23:21 +00002001 for (int OtherSrcIdx : SrcIndices) {
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002002 int OtherSelIdx = TII->getSelIdx(Opcode, OtherSrcIdx);
2003 if (OtherSrcIdx < 0 || OtherSelIdx < 0)
2004 continue;
2005 if (HasDst) {
2006 OtherSrcIdx--;
2007 OtherSelIdx--;
2008 }
2009 if (RegisterSDNode *Reg =
2010 dyn_cast<RegisterSDNode>(ParentNode->getOperand(OtherSrcIdx))) {
2011 if (Reg->getReg() == AMDGPU::ALU_CONST) {
Matt Arsenaultb3ee3882014-05-12 19:26:38 +00002012 ConstantSDNode *Cst
2013 = cast<ConstantSDNode>(ParentNode->getOperand(OtherSelIdx));
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002014 Consts.push_back(Cst->getZExtValue());
2015 }
2016 }
2017 }
2018
Matt Arsenault37c12d72014-05-12 20:42:57 +00002019 ConstantSDNode *Cst = cast<ConstantSDNode>(CstOffset);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002020 Consts.push_back(Cst->getZExtValue());
2021 if (!TII->fitsConstReadLimitations(Consts)) {
2022 return false;
2023 }
2024
2025 Sel = CstOffset;
2026 Src = DAG.getRegister(AMDGPU::ALU_CONST, MVT::f32);
2027 return true;
2028 }
Jan Vesely16800392016-05-13 20:39:31 +00002029 case AMDGPU::MOV_IMM_GLOBAL_ADDR:
2030 // Check if the Imm slot is used. Taken from below.
2031 if (cast<ConstantSDNode>(Imm)->getZExtValue())
2032 return false;
2033 Imm = Src.getOperand(0);
2034 Src = DAG.getRegister(AMDGPU::ALU_LITERAL_X, MVT::i32);
2035 return true;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002036 case AMDGPU::MOV_IMM_I32:
2037 case AMDGPU::MOV_IMM_F32: {
2038 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
2039 uint64_t ImmValue = 0;
2040
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002041 if (Src.getMachineOpcode() == AMDGPU::MOV_IMM_F32) {
2042 ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Src.getOperand(0));
2043 float FloatValue = FPC->getValueAPF().convertToFloat();
2044 if (FloatValue == 0.0) {
2045 ImmReg = AMDGPU::ZERO;
2046 } else if (FloatValue == 0.5) {
2047 ImmReg = AMDGPU::HALF;
2048 } else if (FloatValue == 1.0) {
2049 ImmReg = AMDGPU::ONE;
2050 } else {
2051 ImmValue = FPC->getValueAPF().bitcastToAPInt().getZExtValue();
2052 }
2053 } else {
2054 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(0));
2055 uint64_t Value = C->getZExtValue();
2056 if (Value == 0) {
2057 ImmReg = AMDGPU::ZERO;
2058 } else if (Value == 1) {
2059 ImmReg = AMDGPU::ONE_INT;
2060 } else {
2061 ImmValue = Value;
2062 }
2063 }
2064
2065 // Check that we aren't already using an immediate.
2066 // XXX: It's possible for an instruction to have more than one
2067 // immediate operand, but this is not supported yet.
2068 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
2069 if (!Imm.getNode())
2070 return false;
2071 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Imm);
2072 assert(C);
2073 if (C->getZExtValue())
2074 return false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002075 Imm = DAG.getTargetConstant(ImmValue, SDLoc(ParentNode), MVT::i32);
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002076 }
2077 Src = DAG.getRegister(ImmReg, MVT::i32);
2078 return true;
2079 }
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002080 default:
2081 return false;
2082 }
2083}
2084
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002085/// \brief Fold the instructions after selecting them
2086SDNode *R600TargetLowering::PostISelFolding(MachineSDNode *Node,
2087 SelectionDAG &DAG) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002088 const R600InstrInfo *TII = getSubtarget()->getInstrInfo();
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002089 if (!Node->isMachineOpcode())
2090 return Node;
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002091
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002092 unsigned Opcode = Node->getMachineOpcode();
2093 SDValue FakeOp;
2094
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002095 std::vector<SDValue> Ops(Node->op_begin(), Node->op_end());
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002096
2097 if (Opcode == AMDGPU::DOT_4) {
2098 int OperandIdx[] = {
2099 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
2100 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
2101 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
2102 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
2103 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
2104 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
2105 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
2106 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
NAKAMURA Takumi4bb85f92013-10-28 04:07:23 +00002107 };
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002108 int NegIdx[] = {
2109 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_X),
2110 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Y),
2111 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Z),
2112 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_W),
2113 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_X),
2114 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Y),
2115 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Z),
2116 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_W)
2117 };
2118 int AbsIdx[] = {
2119 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_X),
2120 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Y),
2121 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Z),
2122 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_W),
2123 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_X),
2124 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Y),
2125 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Z),
2126 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_W)
2127 };
2128 for (unsigned i = 0; i < 8; i++) {
2129 if (OperandIdx[i] < 0)
2130 return Node;
2131 SDValue &Src = Ops[OperandIdx[i] - 1];
2132 SDValue &Neg = Ops[NegIdx[i] - 1];
2133 SDValue &Abs = Ops[AbsIdx[i] - 1];
2134 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2135 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
2136 if (HasDst)
2137 SelIdx--;
2138 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002139 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, FakeOp, DAG))
2140 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2141 }
2142 } else if (Opcode == AMDGPU::REG_SEQUENCE) {
2143 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) {
2144 SDValue &Src = Ops[i];
2145 if (FoldOperand(Node, i, Src, FakeOp, FakeOp, FakeOp, FakeOp, DAG))
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002146 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2147 }
Vincent Lejeune0167a312013-09-12 23:45:00 +00002148 } else if (Opcode == AMDGPU::CLAMP_R600) {
2149 SDValue Src = Node->getOperand(0);
2150 if (!Src.isMachineOpcode() ||
2151 !TII->hasInstrModifiers(Src.getMachineOpcode()))
2152 return Node;
2153 int ClampIdx = TII->getOperandIdx(Src.getMachineOpcode(),
2154 AMDGPU::OpName::clamp);
2155 if (ClampIdx < 0)
2156 return Node;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002157 SDLoc DL(Node);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002158 std::vector<SDValue> Ops(Src->op_begin(), Src->op_end());
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002159 Ops[ClampIdx - 1] = DAG.getTargetConstant(1, DL, MVT::i32);
2160 return DAG.getMachineNode(Src.getMachineOpcode(), DL,
2161 Node->getVTList(), Ops);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002162 } else {
2163 if (!TII->hasInstrModifiers(Opcode))
2164 return Node;
2165 int OperandIdx[] = {
2166 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
2167 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
2168 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2)
2169 };
2170 int NegIdx[] = {
2171 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg),
2172 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg),
2173 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_neg)
2174 };
2175 int AbsIdx[] = {
2176 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs),
2177 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs),
2178 -1
2179 };
2180 for (unsigned i = 0; i < 3; i++) {
2181 if (OperandIdx[i] < 0)
2182 return Node;
2183 SDValue &Src = Ops[OperandIdx[i] - 1];
2184 SDValue &Neg = Ops[NegIdx[i] - 1];
2185 SDValue FakeAbs;
2186 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
2187 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2188 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002189 int ImmIdx = TII->getOperandIdx(Opcode, AMDGPU::OpName::literal);
2190 if (HasDst) {
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002191 SelIdx--;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002192 ImmIdx--;
2193 }
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002194 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
Vincent Lejeune9a248e52013-09-12 23:44:53 +00002195 SDValue &Imm = Ops[ImmIdx];
2196 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, Imm, DAG))
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002197 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2198 }
2199 }
2200
2201 return Node;
2202}