blob: ece0b9a5c8d53020328285f404310d0e9aa4bd1d [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"
16#include "R600Defines.h"
17#include "R600InstrInfo.h"
18#include "R600MachineFunctionInfo.h"
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Argument.h"
24#include "llvm/IR/Function.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000025
26using namespace llvm;
27
28R600TargetLowering::R600TargetLowering(TargetMachine &TM) :
29 AMDGPUTargetLowering(TM),
30 TII(static_cast<const R600InstrInfo*>(TM.getInstrInfo())) {
31 setOperationAction(ISD::MUL, MVT::i64, Expand);
32 addRegisterClass(MVT::v4f32, &AMDGPU::R600_Reg128RegClass);
33 addRegisterClass(MVT::f32, &AMDGPU::R600_Reg32RegClass);
34 addRegisterClass(MVT::v4i32, &AMDGPU::R600_Reg128RegClass);
35 addRegisterClass(MVT::i32, &AMDGPU::R600_Reg32RegClass);
36 computeRegisterProperties();
37
38 setOperationAction(ISD::FADD, MVT::v4f32, Expand);
39 setOperationAction(ISD::FMUL, MVT::v4f32, Expand);
40 setOperationAction(ISD::FDIV, MVT::v4f32, Expand);
41 setOperationAction(ISD::FSUB, MVT::v4f32, Expand);
42
43 setOperationAction(ISD::ADD, MVT::v4i32, Expand);
44 setOperationAction(ISD::AND, MVT::v4i32, Expand);
Tom Stellarda8b03512012-12-21 16:33:24 +000045 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Expand);
46 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Expand);
47 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Expand);
48 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Expand);
Tom Stellard75aadc22012-12-11 21:25:42 +000049 setOperationAction(ISD::UDIV, MVT::v4i32, Expand);
50 setOperationAction(ISD::UREM, MVT::v4i32, Expand);
51 setOperationAction(ISD::SETCC, MVT::v4i32, Expand);
52
53 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
54 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
55
56 setOperationAction(ISD::FSUB, MVT::f32, Expand);
57
58 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
59 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
60 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i1, Custom);
61 setOperationAction(ISD::FPOW, MVT::f32, Custom);
62
63 setOperationAction(ISD::ROTL, MVT::i32, Custom);
64
65 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
66 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
67
68 setOperationAction(ISD::SETCC, MVT::i32, Custom);
69 setOperationAction(ISD::SETCC, MVT::f32, Custom);
70 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Custom);
71
72 setOperationAction(ISD::SELECT, MVT::i32, Custom);
73 setOperationAction(ISD::SELECT, MVT::f32, Custom);
74
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000075 // Legalize loads and stores to the private address space.
76 setOperationAction(ISD::LOAD, MVT::i32, Custom);
77 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
78 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
79 setLoadExtAction(ISD::EXTLOAD, MVT::v4i8, Custom);
80 setLoadExtAction(ISD::EXTLOAD, MVT::i8, Custom);
81 setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Custom);
82 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i8, Custom);
83 setOperationAction(ISD::STORE, MVT::i8, Custom);
Tom Stellard75aadc22012-12-11 21:25:42 +000084 setOperationAction(ISD::STORE, MVT::i32, Custom);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000085 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
Tom Stellard75aadc22012-12-11 21:25:42 +000086 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
87
Tom Stellard365366f2013-01-23 02:09:06 +000088 setOperationAction(ISD::LOAD, MVT::i32, Custom);
89 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000090 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
91
Tom Stellard75aadc22012-12-11 21:25:42 +000092 setTargetDAGCombine(ISD::FP_ROUND);
Tom Stellarde06163a2013-02-07 14:02:35 +000093 setTargetDAGCombine(ISD::FP_TO_SINT);
Tom Stellard365366f2013-01-23 02:09:06 +000094 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Tom Stellarde06163a2013-02-07 14:02:35 +000095 setTargetDAGCombine(ISD::SELECT_CC);
Tom Stellard75aadc22012-12-11 21:25:42 +000096
97 setSchedulingPreference(Sched::VLIW);
98}
99
100MachineBasicBlock * R600TargetLowering::EmitInstrWithCustomInserter(
101 MachineInstr * MI, MachineBasicBlock * BB) const {
102 MachineFunction * MF = BB->getParent();
103 MachineRegisterInfo &MRI = MF->getRegInfo();
104 MachineBasicBlock::iterator I = *MI;
105
106 switch (MI->getOpcode()) {
107 default: return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
108 case AMDGPU::SHADER_TYPE: break;
109 case AMDGPU::CLAMP_R600: {
110 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
111 AMDGPU::MOV,
112 MI->getOperand(0).getReg(),
113 MI->getOperand(1).getReg());
114 TII->addFlag(NewMI, 0, MO_FLAG_CLAMP);
115 break;
116 }
117
118 case AMDGPU::FABS_R600: {
119 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
120 AMDGPU::MOV,
121 MI->getOperand(0).getReg(),
122 MI->getOperand(1).getReg());
123 TII->addFlag(NewMI, 0, MO_FLAG_ABS);
124 break;
125 }
126
127 case AMDGPU::FNEG_R600: {
128 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
129 AMDGPU::MOV,
130 MI->getOperand(0).getReg(),
131 MI->getOperand(1).getReg());
132 TII->addFlag(NewMI, 0, MO_FLAG_NEG);
133 break;
134 }
135
Tom Stellard75aadc22012-12-11 21:25:42 +0000136 case AMDGPU::MASK_WRITE: {
137 unsigned maskedRegister = MI->getOperand(0).getReg();
138 assert(TargetRegisterInfo::isVirtualRegister(maskedRegister));
139 MachineInstr * defInstr = MRI.getVRegDef(maskedRegister);
140 TII->addFlag(defInstr, 0, MO_FLAG_MASK);
141 break;
142 }
143
144 case AMDGPU::MOV_IMM_F32:
145 TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
146 MI->getOperand(1).getFPImm()->getValueAPF()
147 .bitcastToAPInt().getZExtValue());
148 break;
149 case AMDGPU::MOV_IMM_I32:
150 TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
151 MI->getOperand(1).getImm());
152 break;
153
154
155 case AMDGPU::RAT_WRITE_CACHELESS_32_eg:
156 case AMDGPU::RAT_WRITE_CACHELESS_128_eg: {
157 unsigned EOP = (llvm::next(I)->getOpcode() == AMDGPU::RETURN) ? 1 : 0;
158
159 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
160 .addOperand(MI->getOperand(0))
161 .addOperand(MI->getOperand(1))
162 .addImm(EOP); // Set End of program bit
163 break;
164 }
165
Tom Stellard75aadc22012-12-11 21:25:42 +0000166 case AMDGPU::TXD: {
167 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
168 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
169
170 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
171 .addOperand(MI->getOperand(3))
172 .addOperand(MI->getOperand(4))
173 .addOperand(MI->getOperand(5))
174 .addOperand(MI->getOperand(6));
175 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
176 .addOperand(MI->getOperand(2))
177 .addOperand(MI->getOperand(4))
178 .addOperand(MI->getOperand(5))
179 .addOperand(MI->getOperand(6));
180 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_G))
181 .addOperand(MI->getOperand(0))
182 .addOperand(MI->getOperand(1))
183 .addOperand(MI->getOperand(4))
184 .addOperand(MI->getOperand(5))
185 .addOperand(MI->getOperand(6))
186 .addReg(T0, RegState::Implicit)
187 .addReg(T1, RegState::Implicit);
188 break;
189 }
190
191 case AMDGPU::TXD_SHADOW: {
192 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
193 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
194
195 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
196 .addOperand(MI->getOperand(3))
197 .addOperand(MI->getOperand(4))
198 .addOperand(MI->getOperand(5))
199 .addOperand(MI->getOperand(6));
200 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
201 .addOperand(MI->getOperand(2))
202 .addOperand(MI->getOperand(4))
203 .addOperand(MI->getOperand(5))
204 .addOperand(MI->getOperand(6));
205 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_C_G))
206 .addOperand(MI->getOperand(0))
207 .addOperand(MI->getOperand(1))
208 .addOperand(MI->getOperand(4))
209 .addOperand(MI->getOperand(5))
210 .addOperand(MI->getOperand(6))
211 .addReg(T0, RegState::Implicit)
212 .addReg(T1, RegState::Implicit);
213 break;
214 }
215
216 case AMDGPU::BRANCH:
217 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
218 .addOperand(MI->getOperand(0))
219 .addReg(0);
220 break;
221
222 case AMDGPU::BRANCH_COND_f32: {
223 MachineInstr *NewMI =
224 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
225 AMDGPU::PREDICATE_BIT)
226 .addOperand(MI->getOperand(1))
227 .addImm(OPCODE_IS_NOT_ZERO)
228 .addImm(0); // Flags
229 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
230 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
231 .addOperand(MI->getOperand(0))
232 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
233 break;
234 }
235
236 case AMDGPU::BRANCH_COND_i32: {
237 MachineInstr *NewMI =
238 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
239 AMDGPU::PREDICATE_BIT)
240 .addOperand(MI->getOperand(1))
241 .addImm(OPCODE_IS_NOT_ZERO_INT)
242 .addImm(0); // Flags
243 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
244 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
245 .addOperand(MI->getOperand(0))
246 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
247 break;
248 }
249
Tom Stellard75aadc22012-12-11 21:25:42 +0000250 case AMDGPU::EG_ExportSwz:
251 case AMDGPU::R600_ExportSwz: {
Tom Stellard6f1b8652013-01-23 21:39:49 +0000252 // Instruction is left unmodified if its not the last one of its type
253 bool isLastInstructionOfItsType = true;
254 unsigned InstExportType = MI->getOperand(1).getImm();
255 for (MachineBasicBlock::iterator NextExportInst = llvm::next(I),
256 EndBlock = BB->end(); NextExportInst != EndBlock;
257 NextExportInst = llvm::next(NextExportInst)) {
258 if (NextExportInst->getOpcode() == AMDGPU::EG_ExportSwz ||
259 NextExportInst->getOpcode() == AMDGPU::R600_ExportSwz) {
260 unsigned CurrentInstExportType = NextExportInst->getOperand(1)
261 .getImm();
262 if (CurrentInstExportType == InstExportType) {
263 isLastInstructionOfItsType = false;
264 break;
265 }
266 }
267 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000268 bool EOP = (llvm::next(I)->getOpcode() == AMDGPU::RETURN)? 1 : 0;
Tom Stellard6f1b8652013-01-23 21:39:49 +0000269 if (!EOP && !isLastInstructionOfItsType)
Tom Stellard75aadc22012-12-11 21:25:42 +0000270 return BB;
271 unsigned CfInst = (MI->getOpcode() == AMDGPU::EG_ExportSwz)? 84 : 40;
272 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
273 .addOperand(MI->getOperand(0))
274 .addOperand(MI->getOperand(1))
275 .addOperand(MI->getOperand(2))
276 .addOperand(MI->getOperand(3))
277 .addOperand(MI->getOperand(4))
278 .addOperand(MI->getOperand(5))
279 .addOperand(MI->getOperand(6))
280 .addImm(CfInst)
Tom Stellard6f1b8652013-01-23 21:39:49 +0000281 .addImm(EOP);
Tom Stellard75aadc22012-12-11 21:25:42 +0000282 break;
283 }
Jakob Stoklund Olesenfdc37672013-02-05 17:53:52 +0000284 case AMDGPU::RETURN: {
285 // RETURN instructions must have the live-out registers as implicit uses,
286 // otherwise they appear dead.
287 R600MachineFunctionInfo *MFI = MF->getInfo<R600MachineFunctionInfo>();
288 MachineInstrBuilder MIB(*MF, MI);
289 for (unsigned i = 0, e = MFI->LiveOuts.size(); i != e; ++i)
290 MIB.addReg(MFI->LiveOuts[i], RegState::Implicit);
291 return BB;
292 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000293 }
294
295 MI->eraseFromParent();
296 return BB;
297}
298
299//===----------------------------------------------------------------------===//
300// Custom DAG Lowering Operations
301//===----------------------------------------------------------------------===//
302
303using namespace llvm::Intrinsic;
304using namespace llvm::AMDGPUIntrinsic;
305
Tom Stellard75aadc22012-12-11 21:25:42 +0000306SDValue R600TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
307 switch (Op.getOpcode()) {
308 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
309 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
310 case ISD::ROTL: return LowerROTL(Op, DAG);
311 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
312 case ISD::SELECT: return LowerSELECT(Op, DAG);
313 case ISD::SETCC: return LowerSETCC(Op, DAG);
314 case ISD::STORE: return LowerSTORE(Op, DAG);
Tom Stellard365366f2013-01-23 02:09:06 +0000315 case ISD::LOAD: return LowerLOAD(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000316 case ISD::FPOW: return LowerFPOW(Op, DAG);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000317 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000318 case ISD::INTRINSIC_VOID: {
319 SDValue Chain = Op.getOperand(0);
320 unsigned IntrinsicID =
321 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
322 switch (IntrinsicID) {
323 case AMDGPUIntrinsic::AMDGPU_store_output: {
324 MachineFunction &MF = DAG.getMachineFunction();
Jakob Stoklund Olesenfdc37672013-02-05 17:53:52 +0000325 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
Tom Stellard75aadc22012-12-11 21:25:42 +0000326 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
327 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
Jakob Stoklund Olesenfdc37672013-02-05 17:53:52 +0000328 MFI->LiveOuts.push_back(Reg);
Tom Stellard75aadc22012-12-11 21:25:42 +0000329 return DAG.getCopyToReg(Chain, Op.getDebugLoc(), Reg, Op.getOperand(2));
330 }
Vincent Lejeuned80bc152013-02-14 16:55:06 +0000331 case AMDGPUIntrinsic::R600_store_swizzle: {
332 const SDValue Args[8] = {
333 Chain,
334 Op.getOperand(2), // Export Value
335 Op.getOperand(3), // ArrayBase
336 Op.getOperand(4), // Type
337 DAG.getConstant(0, MVT::i32), // SWZ_X
338 DAG.getConstant(1, MVT::i32), // SWZ_Y
339 DAG.getConstant(2, MVT::i32), // SWZ_Z
340 DAG.getConstant(3, MVT::i32) // SWZ_W
341 };
342 return DAG.getNode(AMDGPUISD::EXPORT, Op.getDebugLoc(), Op.getValueType(),
343 Args, 8);
Tom Stellard75aadc22012-12-11 21:25:42 +0000344 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000345
Tom Stellard75aadc22012-12-11 21:25:42 +0000346 // default for switch(IntrinsicID)
347 default: break;
348 }
349 // break out of case ISD::INTRINSIC_VOID in switch(Op.getOpcode())
350 break;
351 }
352 case ISD::INTRINSIC_WO_CHAIN: {
353 unsigned IntrinsicID =
354 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
355 EVT VT = Op.getValueType();
356 DebugLoc DL = Op.getDebugLoc();
357 switch(IntrinsicID) {
358 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
359 case AMDGPUIntrinsic::R600_load_input: {
360 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
361 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
362 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass, Reg, VT);
363 }
Tom Stellard41afe6a2013-02-05 17:09:14 +0000364
365 case AMDGPUIntrinsic::R600_interp_input: {
Tom Stellard75aadc22012-12-11 21:25:42 +0000366 int slot = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Tom Stellard41afe6a2013-02-05 17:09:14 +0000367 int ijb = cast<ConstantSDNode>(Op.getOperand(2))->getSExtValue();
368 MachineSDNode *interp;
369 if (ijb < 0) {
370 interp = DAG.getMachineNode(AMDGPU::INTERP_VEC_LOAD, DL,
371 MVT::v4f32, DAG.getTargetConstant(slot / 4 , MVT::i32));
372 return DAG.getTargetExtractSubreg(
373 TII->getRegisterInfo().getSubRegFromChannel(slot % 4),
374 DL, MVT::f32, SDValue(interp, 0));
375 }
376
377 if (slot % 4 < 2)
378 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_XY, DL,
379 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
380 CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
381 AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb + 1), MVT::f32),
382 CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
383 AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb), MVT::f32));
384 else
385 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_ZW, DL,
386 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
387 CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
388 AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb + 1), MVT::f32),
389 CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
390 AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb), MVT::f32));
391
392 return SDValue(interp, slot % 2);
Tom Stellard75aadc22012-12-11 21:25:42 +0000393 }
394
395 case r600_read_ngroups_x:
396 return LowerImplicitParameter(DAG, VT, DL, 0);
397 case r600_read_ngroups_y:
398 return LowerImplicitParameter(DAG, VT, DL, 1);
399 case r600_read_ngroups_z:
400 return LowerImplicitParameter(DAG, VT, DL, 2);
401 case r600_read_global_size_x:
402 return LowerImplicitParameter(DAG, VT, DL, 3);
403 case r600_read_global_size_y:
404 return LowerImplicitParameter(DAG, VT, DL, 4);
405 case r600_read_global_size_z:
406 return LowerImplicitParameter(DAG, VT, DL, 5);
407 case r600_read_local_size_x:
408 return LowerImplicitParameter(DAG, VT, DL, 6);
409 case r600_read_local_size_y:
410 return LowerImplicitParameter(DAG, VT, DL, 7);
411 case r600_read_local_size_z:
412 return LowerImplicitParameter(DAG, VT, DL, 8);
413
414 case r600_read_tgid_x:
415 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
416 AMDGPU::T1_X, VT);
417 case r600_read_tgid_y:
418 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
419 AMDGPU::T1_Y, VT);
420 case r600_read_tgid_z:
421 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
422 AMDGPU::T1_Z, VT);
423 case r600_read_tidig_x:
424 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
425 AMDGPU::T0_X, VT);
426 case r600_read_tidig_y:
427 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
428 AMDGPU::T0_Y, VT);
429 case r600_read_tidig_z:
430 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
431 AMDGPU::T0_Z, VT);
432 }
433 // break out of case ISD::INTRINSIC_WO_CHAIN in switch(Op.getOpcode())
434 break;
435 }
436 } // end switch(Op.getOpcode())
437 return SDValue();
438}
439
440void R600TargetLowering::ReplaceNodeResults(SDNode *N,
441 SmallVectorImpl<SDValue> &Results,
442 SelectionDAG &DAG) const {
443 switch (N->getOpcode()) {
444 default: return;
445 case ISD::FP_TO_UINT: Results.push_back(LowerFPTOUINT(N->getOperand(0), DAG));
Tom Stellard365366f2013-01-23 02:09:06 +0000446 return;
447 case ISD::LOAD: {
448 SDNode *Node = LowerLOAD(SDValue(N, 0), DAG).getNode();
449 Results.push_back(SDValue(Node, 0));
450 Results.push_back(SDValue(Node, 1));
451 // XXX: LLVM seems not to replace Chain Value inside CustomWidenLowerNode
452 // function
453 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), SDValue(Node, 1));
454 return;
455 }
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000456 case ISD::STORE:
457 SDNode *Node = LowerSTORE(SDValue(N, 0), DAG).getNode();
458 Results.push_back(SDValue(Node, 0));
459 return;
Tom Stellard75aadc22012-12-11 21:25:42 +0000460 }
461}
462
463SDValue R600TargetLowering::LowerFPTOUINT(SDValue Op, SelectionDAG &DAG) const {
464 return DAG.getNode(
465 ISD::SETCC,
466 Op.getDebugLoc(),
467 MVT::i1,
468 Op, DAG.getConstantFP(0.0f, MVT::f32),
469 DAG.getCondCode(ISD::SETNE)
470 );
471}
472
473SDValue R600TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
474 SDValue Chain = Op.getOperand(0);
475 SDValue CC = Op.getOperand(1);
476 SDValue LHS = Op.getOperand(2);
477 SDValue RHS = Op.getOperand(3);
478 SDValue JumpT = Op.getOperand(4);
479 SDValue CmpValue;
480 SDValue Result;
481
482 if (LHS.getValueType() == MVT::i32) {
483 CmpValue = DAG.getNode(
484 ISD::SELECT_CC,
485 Op.getDebugLoc(),
486 MVT::i32,
487 LHS, RHS,
488 DAG.getConstant(-1, MVT::i32),
489 DAG.getConstant(0, MVT::i32),
490 CC);
491 } else if (LHS.getValueType() == MVT::f32) {
492 CmpValue = DAG.getNode(
493 ISD::SELECT_CC,
494 Op.getDebugLoc(),
495 MVT::f32,
496 LHS, RHS,
497 DAG.getConstantFP(1.0f, MVT::f32),
498 DAG.getConstantFP(0.0f, MVT::f32),
499 CC);
500 } else {
501 assert(0 && "Not valid type for br_cc");
502 }
503 Result = DAG.getNode(
504 AMDGPUISD::BRANCH_COND,
505 CmpValue.getDebugLoc(),
506 MVT::Other, Chain,
507 JumpT, CmpValue);
508 return Result;
509}
510
511SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT,
512 DebugLoc DL,
513 unsigned DwordOffset) const {
514 unsigned ByteOffset = DwordOffset * 4;
515 PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
516 AMDGPUAS::PARAM_I_ADDRESS);
517
518 // We shouldn't be using an offset wider than 16-bits for implicit parameters.
519 assert(isInt<16>(ByteOffset));
520
521 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
522 DAG.getConstant(ByteOffset, MVT::i32), // PTR
523 MachinePointerInfo(ConstantPointerNull::get(PtrType)),
524 false, false, false, 0);
525}
526
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000527SDValue R600TargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
528
529 MachineFunction &MF = DAG.getMachineFunction();
530 const AMDGPUFrameLowering *TFL =
531 static_cast<const AMDGPUFrameLowering*>(getTargetMachine().getFrameLowering());
532
533 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op);
534 assert(FIN);
535
536 unsigned FrameIndex = FIN->getIndex();
537 unsigned Offset = TFL->getFrameIndexOffset(MF, FrameIndex);
538 return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF), MVT::i32);
539}
540
Tom Stellard75aadc22012-12-11 21:25:42 +0000541SDValue R600TargetLowering::LowerROTL(SDValue Op, SelectionDAG &DAG) const {
542 DebugLoc DL = Op.getDebugLoc();
543 EVT VT = Op.getValueType();
544
545 return DAG.getNode(AMDGPUISD::BITALIGN, DL, VT,
546 Op.getOperand(0),
547 Op.getOperand(0),
548 DAG.getNode(ISD::SUB, DL, VT,
549 DAG.getConstant(32, MVT::i32),
550 Op.getOperand(1)));
551}
552
553bool R600TargetLowering::isZero(SDValue Op) const {
554 if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) {
555 return Cst->isNullValue();
556 } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){
557 return CstFP->isZero();
558 } else {
559 return false;
560 }
561}
562
563SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
564 DebugLoc DL = Op.getDebugLoc();
565 EVT VT = Op.getValueType();
566
567 SDValue LHS = Op.getOperand(0);
568 SDValue RHS = Op.getOperand(1);
569 SDValue True = Op.getOperand(2);
570 SDValue False = Op.getOperand(3);
571 SDValue CC = Op.getOperand(4);
572 SDValue Temp;
573
574 // LHS and RHS are guaranteed to be the same value type
575 EVT CompareVT = LHS.getValueType();
576
577 // Check if we can lower this to a native operation.
578
579 // Try to lower to a CND* instruction:
580 // CND* instructions requires RHS to be zero. Some SELECT_CC nodes that
581 // can be lowered to CND* instructions can also be lowered to SET*
582 // instructions. CND* instructions are cheaper, because they dont't
583 // require additional instructions to convert their result to the correct
584 // value type, so this check should be first.
585 if (isZero(LHS) || isZero(RHS)) {
586 SDValue Cond = (isZero(LHS) ? RHS : LHS);
587 SDValue Zero = (isZero(LHS) ? LHS : RHS);
588 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
589 if (CompareVT != VT) {
590 // Bitcast True / False to the correct types. This will end up being
591 // a nop, but it allows us to define only a single pattern in the
592 // .TD files for each CND* instruction rather than having to have
593 // one pattern for integer True/False and one for fp True/False
594 True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
595 False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
596 }
597 if (isZero(LHS)) {
598 CCOpcode = ISD::getSetCCSwappedOperands(CCOpcode);
599 }
600
601 switch (CCOpcode) {
602 case ISD::SETONE:
603 case ISD::SETUNE:
604 case ISD::SETNE:
605 case ISD::SETULE:
606 case ISD::SETULT:
607 case ISD::SETOLE:
608 case ISD::SETOLT:
609 case ISD::SETLE:
610 case ISD::SETLT:
611 CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
612 Temp = True;
613 True = False;
614 False = Temp;
615 break;
616 default:
617 break;
618 }
619 SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
620 Cond, Zero,
621 True, False,
622 DAG.getCondCode(CCOpcode));
623 return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
624 }
625
626 // Try to lower to a SET* instruction:
Tom Stellarde06163a2013-02-07 14:02:35 +0000627 //
628 // CompareVT == MVT::f32 and VT == MVT::i32 is supported by the hardware,
629 // but for the other case where CompareVT != VT, all operands of
630 // SELECT_CC need to have the same value type, so we need to change True and
631 // False to be the same type as LHS and RHS, and then convert the result of
632 // the select_cc back to the correct type.
Tom Stellard75aadc22012-12-11 21:25:42 +0000633
634 // Move hardware True/False values to the correct operand.
635 if (isHWTrueValue(False) && isHWFalseValue(True)) {
636 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
637 std::swap(False, True);
638 CC = DAG.getCondCode(ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32));
639 }
640
641 if (isHWTrueValue(True) && isHWFalseValue(False)) {
Tom Stellarde06163a2013-02-07 14:02:35 +0000642 if (CompareVT != VT && VT == MVT::f32 && CompareVT == MVT::i32) {
643 SDValue Boolean = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
644 LHS, RHS,
645 DAG.getConstant(-1, MVT::i32),
646 DAG.getConstant(0, MVT::i32),
647 CC);
648 // Convert integer values of true (-1) and false (0) to fp values of
649 // true (1.0f) and false (0.0f).
650 SDValue LSB = DAG.getNode(ISD::AND, DL, MVT::i32, Boolean,
651 DAG.getConstant(1, MVT::i32));
652 return DAG.getNode(ISD::UINT_TO_FP, DL, VT, LSB);
Tom Stellard75aadc22012-12-11 21:25:42 +0000653 } else {
654 // This SELECT_CC is already legal.
655 return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
656 }
657 }
658
659 // Possible Min/Max pattern
660 SDValue MinMax = LowerMinMax(Op, DAG);
661 if (MinMax.getNode()) {
662 return MinMax;
663 }
664
665 // If we make it this for it means we have no native instructions to handle
666 // this SELECT_CC, so we must lower it.
667 SDValue HWTrue, HWFalse;
668
669 if (CompareVT == MVT::f32) {
670 HWTrue = DAG.getConstantFP(1.0f, CompareVT);
671 HWFalse = DAG.getConstantFP(0.0f, CompareVT);
672 } else if (CompareVT == MVT::i32) {
673 HWTrue = DAG.getConstant(-1, CompareVT);
674 HWFalse = DAG.getConstant(0, CompareVT);
675 }
676 else {
677 assert(!"Unhandled value type in LowerSELECT_CC");
678 }
679
680 // Lower this unsupported SELECT_CC into a combination of two supported
681 // SELECT_CC operations.
682 SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
683
684 return DAG.getNode(ISD::SELECT_CC, DL, VT,
685 Cond, HWFalse,
686 True, False,
687 DAG.getCondCode(ISD::SETNE));
688}
689
690SDValue R600TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
691 return DAG.getNode(ISD::SELECT_CC,
692 Op.getDebugLoc(),
693 Op.getValueType(),
694 Op.getOperand(0),
695 DAG.getConstant(0, MVT::i32),
696 Op.getOperand(1),
697 Op.getOperand(2),
698 DAG.getCondCode(ISD::SETNE));
699}
700
701SDValue R600TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
702 SDValue Cond;
703 SDValue LHS = Op.getOperand(0);
704 SDValue RHS = Op.getOperand(1);
705 SDValue CC = Op.getOperand(2);
706 DebugLoc DL = Op.getDebugLoc();
707 assert(Op.getValueType() == MVT::i32);
708 if (LHS.getValueType() == MVT::i32) {
709 Cond = DAG.getNode(
710 ISD::SELECT_CC,
711 Op.getDebugLoc(),
712 MVT::i32,
713 LHS, RHS,
714 DAG.getConstant(-1, MVT::i32),
715 DAG.getConstant(0, MVT::i32),
716 CC);
717 } else if (LHS.getValueType() == MVT::f32) {
718 Cond = DAG.getNode(
719 ISD::SELECT_CC,
720 Op.getDebugLoc(),
721 MVT::f32,
722 LHS, RHS,
723 DAG.getConstantFP(1.0f, MVT::f32),
724 DAG.getConstantFP(0.0f, MVT::f32),
725 CC);
726 Cond = DAG.getNode(
727 ISD::FP_TO_SINT,
728 DL,
729 MVT::i32,
730 Cond);
731 } else {
732 assert(0 && "Not valid type for set_cc");
733 }
734 Cond = DAG.getNode(
735 ISD::AND,
736 DL,
737 MVT::i32,
738 DAG.getConstant(1, MVT::i32),
739 Cond);
740 return Cond;
741}
742
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000743/// LLVM generates byte-addresed pointers. For indirect addressing, we need to
744/// convert these pointers to a register index. Each register holds
745/// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
746/// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
747/// for indirect addressing.
748SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
749 unsigned StackWidth,
750 SelectionDAG &DAG) const {
751 unsigned SRLPad;
752 switch(StackWidth) {
753 case 1:
754 SRLPad = 2;
755 break;
756 case 2:
757 SRLPad = 3;
758 break;
759 case 4:
760 SRLPad = 4;
761 break;
762 default: llvm_unreachable("Invalid stack width");
763 }
764
765 return DAG.getNode(ISD::SRL, Ptr.getDebugLoc(), Ptr.getValueType(), Ptr,
766 DAG.getConstant(SRLPad, MVT::i32));
767}
768
769void R600TargetLowering::getStackAddress(unsigned StackWidth,
770 unsigned ElemIdx,
771 unsigned &Channel,
772 unsigned &PtrIncr) const {
773 switch (StackWidth) {
774 default:
775 case 1:
776 Channel = 0;
777 if (ElemIdx > 0) {
778 PtrIncr = 1;
779 } else {
780 PtrIncr = 0;
781 }
782 break;
783 case 2:
784 Channel = ElemIdx % 2;
785 if (ElemIdx == 2) {
786 PtrIncr = 1;
787 } else {
788 PtrIncr = 0;
789 }
790 break;
791 case 4:
792 Channel = ElemIdx;
793 PtrIncr = 0;
794 break;
795 }
796}
797
Tom Stellard75aadc22012-12-11 21:25:42 +0000798SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
799 DebugLoc DL = Op.getDebugLoc();
800 StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
801 SDValue Chain = Op.getOperand(0);
802 SDValue Value = Op.getOperand(1);
803 SDValue Ptr = Op.getOperand(2);
804
805 if (StoreNode->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
806 Ptr->getOpcode() != AMDGPUISD::DWORDADDR) {
807 // Convert pointer from byte address to dword address.
808 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, Ptr.getValueType(),
809 DAG.getNode(ISD::SRL, DL, Ptr.getValueType(),
810 Ptr, DAG.getConstant(2, MVT::i32)));
811
812 if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
813 assert(!"Truncated and indexed stores not supported yet");
814 } else {
815 Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
816 }
817 return Chain;
818 }
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000819
820 EVT ValueVT = Value.getValueType();
821
822 if (StoreNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
823 return SDValue();
824 }
825
826 // Lowering for indirect addressing
827
828 const MachineFunction &MF = DAG.getMachineFunction();
829 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
830 getTargetMachine().getFrameLowering());
831 unsigned StackWidth = TFL->getStackWidth(MF);
832
833 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
834
835 if (ValueVT.isVector()) {
836 unsigned NumElemVT = ValueVT.getVectorNumElements();
837 EVT ElemVT = ValueVT.getVectorElementType();
838 SDValue Stores[4];
839
840 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
841 "vector width in load");
842
843 for (unsigned i = 0; i < NumElemVT; ++i) {
844 unsigned Channel, PtrIncr;
845 getStackAddress(StackWidth, i, Channel, PtrIncr);
846 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
847 DAG.getConstant(PtrIncr, MVT::i32));
848 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT,
849 Value, DAG.getConstant(i, MVT::i32));
850
851 Stores[i] = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
852 Chain, Elem, Ptr,
853 DAG.getTargetConstant(Channel, MVT::i32));
854 }
855 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Stores, NumElemVT);
856 } else {
857 if (ValueVT == MVT::i8) {
858 Value = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Value);
859 }
860 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other, Chain, Value, Ptr,
861 DAG.getTargetConstant(0, MVT::i32)); // Channel
862 }
863
864 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +0000865}
866
Tom Stellard365366f2013-01-23 02:09:06 +0000867// return (512 + (kc_bank << 12)
868static int
869ConstantAddressBlock(unsigned AddressSpace) {
870 switch (AddressSpace) {
871 case AMDGPUAS::CONSTANT_BUFFER_0:
872 return 512;
873 case AMDGPUAS::CONSTANT_BUFFER_1:
874 return 512 + 4096;
875 case AMDGPUAS::CONSTANT_BUFFER_2:
876 return 512 + 4096 * 2;
877 case AMDGPUAS::CONSTANT_BUFFER_3:
878 return 512 + 4096 * 3;
879 case AMDGPUAS::CONSTANT_BUFFER_4:
880 return 512 + 4096 * 4;
881 case AMDGPUAS::CONSTANT_BUFFER_5:
882 return 512 + 4096 * 5;
883 case AMDGPUAS::CONSTANT_BUFFER_6:
884 return 512 + 4096 * 6;
885 case AMDGPUAS::CONSTANT_BUFFER_7:
886 return 512 + 4096 * 7;
887 case AMDGPUAS::CONSTANT_BUFFER_8:
888 return 512 + 4096 * 8;
889 case AMDGPUAS::CONSTANT_BUFFER_9:
890 return 512 + 4096 * 9;
891 case AMDGPUAS::CONSTANT_BUFFER_10:
892 return 512 + 4096 * 10;
893 case AMDGPUAS::CONSTANT_BUFFER_11:
894 return 512 + 4096 * 11;
895 case AMDGPUAS::CONSTANT_BUFFER_12:
896 return 512 + 4096 * 12;
897 case AMDGPUAS::CONSTANT_BUFFER_13:
898 return 512 + 4096 * 13;
899 case AMDGPUAS::CONSTANT_BUFFER_14:
900 return 512 + 4096 * 14;
901 case AMDGPUAS::CONSTANT_BUFFER_15:
902 return 512 + 4096 * 15;
903 default:
904 return -1;
905 }
906}
907
908SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const
909{
910 EVT VT = Op.getValueType();
911 DebugLoc DL = Op.getDebugLoc();
912 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
913 SDValue Chain = Op.getOperand(0);
914 SDValue Ptr = Op.getOperand(1);
915 SDValue LoweredLoad;
916
917 int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
918 if (ConstantBlock > -1) {
919 SDValue Result;
920 if (dyn_cast<ConstantExpr>(LoadNode->getSrcValue()) ||
Vincent Lejeune743dca02013-03-05 15:04:29 +0000921 dyn_cast<Constant>(LoadNode->getSrcValue()) ||
922 dyn_cast<ConstantSDNode>(Ptr)) {
Tom Stellard365366f2013-01-23 02:09:06 +0000923 SDValue Slots[4];
924 for (unsigned i = 0; i < 4; i++) {
925 // We want Const position encoded with the following formula :
926 // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
927 // const_index is Ptr computed by llvm using an alignment of 16.
928 // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
929 // then div by 4 at the ISel step
930 SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
931 DAG.getConstant(4 * i + ConstantBlock * 16, MVT::i32));
932 Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
933 }
934 Result = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, Slots, 4);
935 } else {
936 // non constant ptr cant be folded, keeps it as a v4f32 load
937 Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
Vincent Lejeune743dca02013-03-05 15:04:29 +0000938 DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr, DAG.getConstant(4, MVT::i32)),
939 DAG.getConstant(LoadNode->getAddressSpace() - 9, MVT::i32)
Tom Stellard365366f2013-01-23 02:09:06 +0000940 );
941 }
942
943 if (!VT.isVector()) {
944 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
945 DAG.getConstant(0, MVT::i32));
946 }
947
948 SDValue MergedValues[2] = {
949 Result,
950 Chain
951 };
952 return DAG.getMergeValues(MergedValues, 2, DL);
953 }
954
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000955 if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
956 return SDValue();
957 }
958
959 // Lowering for indirect addressing
960 const MachineFunction &MF = DAG.getMachineFunction();
961 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
962 getTargetMachine().getFrameLowering());
963 unsigned StackWidth = TFL->getStackWidth(MF);
964
965 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
966
967 if (VT.isVector()) {
968 unsigned NumElemVT = VT.getVectorNumElements();
969 EVT ElemVT = VT.getVectorElementType();
970 SDValue Loads[4];
971
972 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
973 "vector width in load");
974
975 for (unsigned i = 0; i < NumElemVT; ++i) {
976 unsigned Channel, PtrIncr;
977 getStackAddress(StackWidth, i, Channel, PtrIncr);
978 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
979 DAG.getConstant(PtrIncr, MVT::i32));
980 Loads[i] = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, ElemVT,
981 Chain, Ptr,
982 DAG.getTargetConstant(Channel, MVT::i32),
983 Op.getOperand(2));
984 }
985 for (unsigned i = NumElemVT; i < 4; ++i) {
986 Loads[i] = DAG.getUNDEF(ElemVT);
987 }
988 EVT TargetVT = EVT::getVectorVT(*DAG.getContext(), ElemVT, 4);
989 LoweredLoad = DAG.getNode(ISD::BUILD_VECTOR, DL, TargetVT, Loads, 4);
990 } else {
991 LoweredLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, VT,
992 Chain, Ptr,
993 DAG.getTargetConstant(0, MVT::i32), // Channel
994 Op.getOperand(2));
995 }
996
997 SDValue Ops[2];
998 Ops[0] = LoweredLoad;
999 Ops[1] = Chain;
1000
1001 return DAG.getMergeValues(Ops, 2, DL);
Tom Stellard365366f2013-01-23 02:09:06 +00001002}
Tom Stellard75aadc22012-12-11 21:25:42 +00001003
1004SDValue R600TargetLowering::LowerFPOW(SDValue Op,
1005 SelectionDAG &DAG) const {
1006 DebugLoc DL = Op.getDebugLoc();
1007 EVT VT = Op.getValueType();
1008 SDValue LogBase = DAG.getNode(ISD::FLOG2, DL, VT, Op.getOperand(0));
1009 SDValue MulLogBase = DAG.getNode(ISD::FMUL, DL, VT, Op.getOperand(1), LogBase);
1010 return DAG.getNode(ISD::FEXP2, DL, VT, MulLogBase);
1011}
1012
1013/// XXX Only kernel functions are supported, so we can assume for now that
1014/// every function is a kernel function, but in the future we should use
1015/// separate calling conventions for kernel and non-kernel functions.
1016SDValue R600TargetLowering::LowerFormalArguments(
1017 SDValue Chain,
1018 CallingConv::ID CallConv,
1019 bool isVarArg,
1020 const SmallVectorImpl<ISD::InputArg> &Ins,
1021 DebugLoc DL, SelectionDAG &DAG,
1022 SmallVectorImpl<SDValue> &InVals) const {
1023 unsigned ParamOffsetBytes = 36;
1024 Function::const_arg_iterator FuncArg =
1025 DAG.getMachineFunction().getFunction()->arg_begin();
1026 for (unsigned i = 0, e = Ins.size(); i < e; ++i, ++FuncArg) {
1027 EVT VT = Ins[i].VT;
1028 Type *ArgType = FuncArg->getType();
1029 unsigned ArgSizeInBits = ArgType->isPointerTy() ?
1030 32 : ArgType->getPrimitiveSizeInBits();
1031 unsigned ArgBytes = ArgSizeInBits >> 3;
1032 EVT ArgVT;
1033 if (ArgSizeInBits < VT.getSizeInBits()) {
1034 assert(!ArgType->isFloatTy() &&
1035 "Extending floating point arguments not supported yet");
1036 ArgVT = MVT::getIntegerVT(ArgSizeInBits);
1037 } else {
1038 ArgVT = VT;
1039 }
1040 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
1041 AMDGPUAS::PARAM_I_ADDRESS);
1042 SDValue Arg = DAG.getExtLoad(ISD::ZEXTLOAD, DL, VT, DAG.getRoot(),
1043 DAG.getConstant(ParamOffsetBytes, MVT::i32),
Tom Stellard8d469ed2013-02-19 15:22:44 +00001044 MachinePointerInfo(UndefValue::get(PtrTy)),
Tom Stellard75aadc22012-12-11 21:25:42 +00001045 ArgVT, false, false, ArgBytes);
1046 InVals.push_back(Arg);
1047 ParamOffsetBytes += ArgBytes;
1048 }
1049 return Chain;
1050}
1051
1052EVT R600TargetLowering::getSetCCResultType(EVT VT) const {
1053 if (!VT.isVector()) return MVT::i32;
1054 return VT.changeVectorElementTypeToInteger();
1055}
1056
1057//===----------------------------------------------------------------------===//
1058// Custom DAG Optimizations
1059//===----------------------------------------------------------------------===//
1060
1061SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
1062 DAGCombinerInfo &DCI) const {
1063 SelectionDAG &DAG = DCI.DAG;
1064
1065 switch (N->getOpcode()) {
1066 // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
1067 case ISD::FP_ROUND: {
1068 SDValue Arg = N->getOperand(0);
1069 if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
1070 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), N->getValueType(0),
1071 Arg.getOperand(0));
1072 }
1073 break;
1074 }
Tom Stellarde06163a2013-02-07 14:02:35 +00001075
1076 // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
1077 // (i32 select_cc f32, f32, -1, 0 cc)
1078 //
1079 // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1080 // this to one of the SET*_DX10 instructions.
1081 case ISD::FP_TO_SINT: {
1082 SDValue FNeg = N->getOperand(0);
1083 if (FNeg.getOpcode() != ISD::FNEG) {
1084 return SDValue();
1085 }
1086 SDValue SelectCC = FNeg.getOperand(0);
1087 if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1088 SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1089 SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1090 !isHWTrueValue(SelectCC.getOperand(2)) ||
1091 !isHWFalseValue(SelectCC.getOperand(3))) {
1092 return SDValue();
1093 }
1094
1095 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), N->getValueType(0),
1096 SelectCC.getOperand(0), // LHS
1097 SelectCC.getOperand(1), // RHS
1098 DAG.getConstant(-1, MVT::i32), // True
1099 DAG.getConstant(0, MVT::i32), // Flase
1100 SelectCC.getOperand(4)); // CC
1101
1102 break;
1103 }
Tom Stellard365366f2013-01-23 02:09:06 +00001104 // Extract_vec (Build_vector) generated by custom lowering
1105 // also needs to be customly combined
1106 case ISD::EXTRACT_VECTOR_ELT: {
1107 SDValue Arg = N->getOperand(0);
1108 if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1109 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1110 unsigned Element = Const->getZExtValue();
1111 return Arg->getOperand(Element);
1112 }
1113 }
Tom Stellarddd04c832013-01-31 22:11:53 +00001114 if (Arg.getOpcode() == ISD::BITCAST &&
1115 Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
1116 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1117 unsigned Element = Const->getZExtValue();
1118 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), N->getVTList(),
1119 Arg->getOperand(0).getOperand(Element));
1120 }
1121 }
Tom Stellard365366f2013-01-23 02:09:06 +00001122 }
Tom Stellarde06163a2013-02-07 14:02:35 +00001123
1124 case ISD::SELECT_CC: {
1125 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1126 // selectcc x, y, a, b, inv(cc)
1127 SDValue LHS = N->getOperand(0);
1128 if (LHS.getOpcode() != ISD::SELECT_CC) {
1129 return SDValue();
1130 }
1131
1132 SDValue RHS = N->getOperand(1);
1133 SDValue True = N->getOperand(2);
1134 SDValue False = N->getOperand(3);
1135
1136 if (LHS.getOperand(2).getNode() != True.getNode() ||
1137 LHS.getOperand(3).getNode() != False.getNode() ||
1138 RHS.getNode() != False.getNode() ||
1139 cast<CondCodeSDNode>(N->getOperand(4))->get() != ISD::SETEQ) {
1140 return SDValue();
1141 }
1142
1143 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(LHS->getOperand(4))->get();
1144 CCOpcode = ISD::getSetCCInverse(
1145 CCOpcode, LHS.getOperand(0).getValueType().isInteger());
1146 return DAG.getSelectCC(N->getDebugLoc(),
1147 LHS.getOperand(0),
1148 LHS.getOperand(1),
1149 LHS.getOperand(2),
1150 LHS.getOperand(3),
1151 CCOpcode);
Vincent Lejeuned80bc152013-02-14 16:55:06 +00001152 }
1153 case AMDGPUISD::EXPORT: {
1154 SDValue Arg = N->getOperand(1);
1155 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1156 break;
1157 SDValue NewBldVec[4] = {
1158 DAG.getUNDEF(MVT::f32),
1159 DAG.getUNDEF(MVT::f32),
1160 DAG.getUNDEF(MVT::f32),
1161 DAG.getUNDEF(MVT::f32)
1162 };
1163 SDValue NewArgs[8] = {
1164 N->getOperand(0), // Chain
1165 SDValue(),
1166 N->getOperand(2), // ArrayBase
1167 N->getOperand(3), // Type
1168 N->getOperand(4), // SWZ_X
1169 N->getOperand(5), // SWZ_Y
1170 N->getOperand(6), // SWZ_Z
1171 N->getOperand(7) // SWZ_W
1172 };
1173 for (unsigned i = 0; i < Arg.getNumOperands(); i++) {
1174 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Arg.getOperand(i))) {
1175 if (C->isZero()) {
1176 NewArgs[4 + i] = DAG.getConstant(4, MVT::i32); // SEL_0
1177 } else if (C->isExactlyValue(1.0)) {
1178 NewArgs[4 + i] = DAG.getConstant(5, MVT::i32); // SEL_0
1179 } else {
1180 NewBldVec[i] = Arg.getOperand(i);
1181 }
1182 } else {
1183 NewBldVec[i] = Arg.getOperand(i);
1184 }
1185 }
1186 DebugLoc DL = N->getDebugLoc();
1187 NewArgs[1] = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4f32, NewBldVec, 4);
1188 return DAG.getNode(AMDGPUISD::EXPORT, DL, N->getVTList(), NewArgs, 8);
Tom Stellarde06163a2013-02-07 14:02:35 +00001189 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001190 }
1191 return SDValue();
1192}