blob: c0d45812e6253d3d552613c7eccd6c2d9dc82d3a [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
Tom Stellard492ebea2013-03-08 15:37:07 +000053 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
54 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
Tom Stellard75aadc22012-12-11 21:25:42 +000055
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
Tom Stellarde8f9f282013-03-08 15:37:05 +000068 setOperationAction(ISD::SETCC, MVT::i32, Expand);
69 setOperationAction(ISD::SETCC, MVT::f32, Expand);
Tom Stellard75aadc22012-12-11 21:25:42 +000070 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
Tom Stellardb852af52013-03-08 15:37:03 +000097 setBooleanContents(ZeroOrNegativeOneBooleanContent);
Tom Stellard75aadc22012-12-11 21:25:42 +000098 setSchedulingPreference(Sched::VLIW);
99}
100
101MachineBasicBlock * R600TargetLowering::EmitInstrWithCustomInserter(
102 MachineInstr * MI, MachineBasicBlock * BB) const {
103 MachineFunction * MF = BB->getParent();
104 MachineRegisterInfo &MRI = MF->getRegInfo();
105 MachineBasicBlock::iterator I = *MI;
106
107 switch (MI->getOpcode()) {
108 default: return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Tom Stellard75aadc22012-12-11 21:25:42 +0000109 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;
Vincent Lejeune0b72f102013-03-05 15:04:55 +0000153 case AMDGPU::CONST_COPY: {
154 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, MI, AMDGPU::MOV,
155 MI->getOperand(0).getReg(), AMDGPU::ALU_CONST);
156 TII->setImmOperand(NewMI, R600Operands::SRC0_SEL,
157 MI->getOperand(1).getImm());
158 break;
159 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000160
161 case AMDGPU::RAT_WRITE_CACHELESS_32_eg:
162 case AMDGPU::RAT_WRITE_CACHELESS_128_eg: {
163 unsigned EOP = (llvm::next(I)->getOpcode() == AMDGPU::RETURN) ? 1 : 0;
164
165 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
166 .addOperand(MI->getOperand(0))
167 .addOperand(MI->getOperand(1))
168 .addImm(EOP); // Set End of program bit
169 break;
170 }
171
Tom Stellard75aadc22012-12-11 21:25:42 +0000172 case AMDGPU::TXD: {
173 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
174 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
175
176 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
177 .addOperand(MI->getOperand(3))
178 .addOperand(MI->getOperand(4))
179 .addOperand(MI->getOperand(5))
180 .addOperand(MI->getOperand(6));
181 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
182 .addOperand(MI->getOperand(2))
183 .addOperand(MI->getOperand(4))
184 .addOperand(MI->getOperand(5))
185 .addOperand(MI->getOperand(6));
186 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_G))
187 .addOperand(MI->getOperand(0))
188 .addOperand(MI->getOperand(1))
189 .addOperand(MI->getOperand(4))
190 .addOperand(MI->getOperand(5))
191 .addOperand(MI->getOperand(6))
192 .addReg(T0, RegState::Implicit)
193 .addReg(T1, RegState::Implicit);
194 break;
195 }
196
197 case AMDGPU::TXD_SHADOW: {
198 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
199 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
200
201 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
202 .addOperand(MI->getOperand(3))
203 .addOperand(MI->getOperand(4))
204 .addOperand(MI->getOperand(5))
205 .addOperand(MI->getOperand(6));
206 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
207 .addOperand(MI->getOperand(2))
208 .addOperand(MI->getOperand(4))
209 .addOperand(MI->getOperand(5))
210 .addOperand(MI->getOperand(6));
211 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_C_G))
212 .addOperand(MI->getOperand(0))
213 .addOperand(MI->getOperand(1))
214 .addOperand(MI->getOperand(4))
215 .addOperand(MI->getOperand(5))
216 .addOperand(MI->getOperand(6))
217 .addReg(T0, RegState::Implicit)
218 .addReg(T1, RegState::Implicit);
219 break;
220 }
221
222 case AMDGPU::BRANCH:
223 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
224 .addOperand(MI->getOperand(0))
225 .addReg(0);
226 break;
227
228 case AMDGPU::BRANCH_COND_f32: {
229 MachineInstr *NewMI =
230 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
231 AMDGPU::PREDICATE_BIT)
232 .addOperand(MI->getOperand(1))
233 .addImm(OPCODE_IS_NOT_ZERO)
234 .addImm(0); // Flags
235 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
236 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
237 .addOperand(MI->getOperand(0))
238 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
239 break;
240 }
241
242 case AMDGPU::BRANCH_COND_i32: {
243 MachineInstr *NewMI =
244 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
245 AMDGPU::PREDICATE_BIT)
246 .addOperand(MI->getOperand(1))
247 .addImm(OPCODE_IS_NOT_ZERO_INT)
248 .addImm(0); // Flags
249 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
250 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
251 .addOperand(MI->getOperand(0))
252 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
253 break;
254 }
255
Tom Stellard75aadc22012-12-11 21:25:42 +0000256 case AMDGPU::EG_ExportSwz:
257 case AMDGPU::R600_ExportSwz: {
Tom Stellard6f1b8652013-01-23 21:39:49 +0000258 // Instruction is left unmodified if its not the last one of its type
259 bool isLastInstructionOfItsType = true;
260 unsigned InstExportType = MI->getOperand(1).getImm();
261 for (MachineBasicBlock::iterator NextExportInst = llvm::next(I),
262 EndBlock = BB->end(); NextExportInst != EndBlock;
263 NextExportInst = llvm::next(NextExportInst)) {
264 if (NextExportInst->getOpcode() == AMDGPU::EG_ExportSwz ||
265 NextExportInst->getOpcode() == AMDGPU::R600_ExportSwz) {
266 unsigned CurrentInstExportType = NextExportInst->getOperand(1)
267 .getImm();
268 if (CurrentInstExportType == InstExportType) {
269 isLastInstructionOfItsType = false;
270 break;
271 }
272 }
273 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000274 bool EOP = (llvm::next(I)->getOpcode() == AMDGPU::RETURN)? 1 : 0;
Tom Stellard6f1b8652013-01-23 21:39:49 +0000275 if (!EOP && !isLastInstructionOfItsType)
Tom Stellard75aadc22012-12-11 21:25:42 +0000276 return BB;
277 unsigned CfInst = (MI->getOpcode() == AMDGPU::EG_ExportSwz)? 84 : 40;
278 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
279 .addOperand(MI->getOperand(0))
280 .addOperand(MI->getOperand(1))
281 .addOperand(MI->getOperand(2))
282 .addOperand(MI->getOperand(3))
283 .addOperand(MI->getOperand(4))
284 .addOperand(MI->getOperand(5))
285 .addOperand(MI->getOperand(6))
286 .addImm(CfInst)
Tom Stellard6f1b8652013-01-23 21:39:49 +0000287 .addImm(EOP);
Tom Stellard75aadc22012-12-11 21:25:42 +0000288 break;
289 }
Jakob Stoklund Olesenfdc37672013-02-05 17:53:52 +0000290 case AMDGPU::RETURN: {
291 // RETURN instructions must have the live-out registers as implicit uses,
292 // otherwise they appear dead.
293 R600MachineFunctionInfo *MFI = MF->getInfo<R600MachineFunctionInfo>();
294 MachineInstrBuilder MIB(*MF, MI);
295 for (unsigned i = 0, e = MFI->LiveOuts.size(); i != e; ++i)
296 MIB.addReg(MFI->LiveOuts[i], RegState::Implicit);
297 return BB;
298 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000299 }
300
301 MI->eraseFromParent();
302 return BB;
303}
304
305//===----------------------------------------------------------------------===//
306// Custom DAG Lowering Operations
307//===----------------------------------------------------------------------===//
308
309using namespace llvm::Intrinsic;
310using namespace llvm::AMDGPUIntrinsic;
311
Tom Stellard75aadc22012-12-11 21:25:42 +0000312SDValue R600TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
313 switch (Op.getOpcode()) {
314 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000315 case ISD::ROTL: return LowerROTL(Op, DAG);
316 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
317 case ISD::SELECT: return LowerSELECT(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000318 case ISD::STORE: return LowerSTORE(Op, DAG);
Tom Stellard365366f2013-01-23 02:09:06 +0000319 case ISD::LOAD: return LowerLOAD(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000320 case ISD::FPOW: return LowerFPOW(Op, DAG);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000321 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000322 case ISD::INTRINSIC_VOID: {
323 SDValue Chain = Op.getOperand(0);
324 unsigned IntrinsicID =
325 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
326 switch (IntrinsicID) {
327 case AMDGPUIntrinsic::AMDGPU_store_output: {
328 MachineFunction &MF = DAG.getMachineFunction();
Jakob Stoklund Olesenfdc37672013-02-05 17:53:52 +0000329 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
Tom Stellard75aadc22012-12-11 21:25:42 +0000330 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
331 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
Jakob Stoklund Olesenfdc37672013-02-05 17:53:52 +0000332 MFI->LiveOuts.push_back(Reg);
Tom Stellard75aadc22012-12-11 21:25:42 +0000333 return DAG.getCopyToReg(Chain, Op.getDebugLoc(), Reg, Op.getOperand(2));
334 }
Vincent Lejeuned80bc152013-02-14 16:55:06 +0000335 case AMDGPUIntrinsic::R600_store_swizzle: {
336 const SDValue Args[8] = {
337 Chain,
338 Op.getOperand(2), // Export Value
339 Op.getOperand(3), // ArrayBase
340 Op.getOperand(4), // Type
341 DAG.getConstant(0, MVT::i32), // SWZ_X
342 DAG.getConstant(1, MVT::i32), // SWZ_Y
343 DAG.getConstant(2, MVT::i32), // SWZ_Z
344 DAG.getConstant(3, MVT::i32) // SWZ_W
345 };
346 return DAG.getNode(AMDGPUISD::EXPORT, Op.getDebugLoc(), Op.getValueType(),
347 Args, 8);
Tom Stellard75aadc22012-12-11 21:25:42 +0000348 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000349
Tom Stellard75aadc22012-12-11 21:25:42 +0000350 // default for switch(IntrinsicID)
351 default: break;
352 }
353 // break out of case ISD::INTRINSIC_VOID in switch(Op.getOpcode())
354 break;
355 }
356 case ISD::INTRINSIC_WO_CHAIN: {
357 unsigned IntrinsicID =
358 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
359 EVT VT = Op.getValueType();
360 DebugLoc DL = Op.getDebugLoc();
361 switch(IntrinsicID) {
362 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
363 case AMDGPUIntrinsic::R600_load_input: {
364 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
365 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
366 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass, Reg, VT);
367 }
Tom Stellard41afe6a2013-02-05 17:09:14 +0000368
369 case AMDGPUIntrinsic::R600_interp_input: {
Tom Stellard75aadc22012-12-11 21:25:42 +0000370 int slot = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Tom Stellard41afe6a2013-02-05 17:09:14 +0000371 int ijb = cast<ConstantSDNode>(Op.getOperand(2))->getSExtValue();
372 MachineSDNode *interp;
373 if (ijb < 0) {
374 interp = DAG.getMachineNode(AMDGPU::INTERP_VEC_LOAD, DL,
375 MVT::v4f32, DAG.getTargetConstant(slot / 4 , MVT::i32));
376 return DAG.getTargetExtractSubreg(
377 TII->getRegisterInfo().getSubRegFromChannel(slot % 4),
378 DL, MVT::f32, SDValue(interp, 0));
379 }
380
381 if (slot % 4 < 2)
382 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_XY, DL,
383 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
384 CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
385 AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb + 1), MVT::f32),
386 CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
387 AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb), MVT::f32));
388 else
389 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_ZW, DL,
390 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
391 CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
392 AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb + 1), MVT::f32),
393 CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
394 AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb), MVT::f32));
395
396 return SDValue(interp, slot % 2);
Tom Stellard75aadc22012-12-11 21:25:42 +0000397 }
398
399 case r600_read_ngroups_x:
400 return LowerImplicitParameter(DAG, VT, DL, 0);
401 case r600_read_ngroups_y:
402 return LowerImplicitParameter(DAG, VT, DL, 1);
403 case r600_read_ngroups_z:
404 return LowerImplicitParameter(DAG, VT, DL, 2);
405 case r600_read_global_size_x:
406 return LowerImplicitParameter(DAG, VT, DL, 3);
407 case r600_read_global_size_y:
408 return LowerImplicitParameter(DAG, VT, DL, 4);
409 case r600_read_global_size_z:
410 return LowerImplicitParameter(DAG, VT, DL, 5);
411 case r600_read_local_size_x:
412 return LowerImplicitParameter(DAG, VT, DL, 6);
413 case r600_read_local_size_y:
414 return LowerImplicitParameter(DAG, VT, DL, 7);
415 case r600_read_local_size_z:
416 return LowerImplicitParameter(DAG, VT, DL, 8);
417
418 case r600_read_tgid_x:
419 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
420 AMDGPU::T1_X, VT);
421 case r600_read_tgid_y:
422 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
423 AMDGPU::T1_Y, VT);
424 case r600_read_tgid_z:
425 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
426 AMDGPU::T1_Z, VT);
427 case r600_read_tidig_x:
428 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
429 AMDGPU::T0_X, VT);
430 case r600_read_tidig_y:
431 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
432 AMDGPU::T0_Y, VT);
433 case r600_read_tidig_z:
434 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
435 AMDGPU::T0_Z, VT);
436 }
437 // break out of case ISD::INTRINSIC_WO_CHAIN in switch(Op.getOpcode())
438 break;
439 }
440 } // end switch(Op.getOpcode())
441 return SDValue();
442}
443
444void R600TargetLowering::ReplaceNodeResults(SDNode *N,
445 SmallVectorImpl<SDValue> &Results,
446 SelectionDAG &DAG) const {
447 switch (N->getOpcode()) {
448 default: return;
449 case ISD::FP_TO_UINT: Results.push_back(LowerFPTOUINT(N->getOperand(0), DAG));
Tom Stellard365366f2013-01-23 02:09:06 +0000450 return;
451 case ISD::LOAD: {
452 SDNode *Node = LowerLOAD(SDValue(N, 0), DAG).getNode();
453 Results.push_back(SDValue(Node, 0));
454 Results.push_back(SDValue(Node, 1));
455 // XXX: LLVM seems not to replace Chain Value inside CustomWidenLowerNode
456 // function
457 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), SDValue(Node, 1));
458 return;
459 }
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000460 case ISD::STORE:
461 SDNode *Node = LowerSTORE(SDValue(N, 0), DAG).getNode();
462 Results.push_back(SDValue(Node, 0));
463 return;
Tom Stellard75aadc22012-12-11 21:25:42 +0000464 }
465}
466
467SDValue R600TargetLowering::LowerFPTOUINT(SDValue Op, SelectionDAG &DAG) const {
468 return DAG.getNode(
469 ISD::SETCC,
470 Op.getDebugLoc(),
471 MVT::i1,
472 Op, DAG.getConstantFP(0.0f, MVT::f32),
473 DAG.getCondCode(ISD::SETNE)
474 );
475}
476
Tom Stellard75aadc22012-12-11 21:25:42 +0000477SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT,
478 DebugLoc DL,
479 unsigned DwordOffset) const {
480 unsigned ByteOffset = DwordOffset * 4;
481 PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
482 AMDGPUAS::PARAM_I_ADDRESS);
483
484 // We shouldn't be using an offset wider than 16-bits for implicit parameters.
485 assert(isInt<16>(ByteOffset));
486
487 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
488 DAG.getConstant(ByteOffset, MVT::i32), // PTR
489 MachinePointerInfo(ConstantPointerNull::get(PtrType)),
490 false, false, false, 0);
491}
492
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000493SDValue R600TargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
494
495 MachineFunction &MF = DAG.getMachineFunction();
496 const AMDGPUFrameLowering *TFL =
497 static_cast<const AMDGPUFrameLowering*>(getTargetMachine().getFrameLowering());
498
499 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op);
500 assert(FIN);
501
502 unsigned FrameIndex = FIN->getIndex();
503 unsigned Offset = TFL->getFrameIndexOffset(MF, FrameIndex);
504 return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF), MVT::i32);
505}
506
Tom Stellard75aadc22012-12-11 21:25:42 +0000507SDValue R600TargetLowering::LowerROTL(SDValue Op, SelectionDAG &DAG) const {
508 DebugLoc DL = Op.getDebugLoc();
509 EVT VT = Op.getValueType();
510
511 return DAG.getNode(AMDGPUISD::BITALIGN, DL, VT,
512 Op.getOperand(0),
513 Op.getOperand(0),
514 DAG.getNode(ISD::SUB, DL, VT,
515 DAG.getConstant(32, MVT::i32),
516 Op.getOperand(1)));
517}
518
519bool R600TargetLowering::isZero(SDValue Op) const {
520 if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) {
521 return Cst->isNullValue();
522 } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){
523 return CstFP->isZero();
524 } else {
525 return false;
526 }
527}
528
529SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
530 DebugLoc DL = Op.getDebugLoc();
531 EVT VT = Op.getValueType();
532
533 SDValue LHS = Op.getOperand(0);
534 SDValue RHS = Op.getOperand(1);
535 SDValue True = Op.getOperand(2);
536 SDValue False = Op.getOperand(3);
537 SDValue CC = Op.getOperand(4);
538 SDValue Temp;
539
540 // LHS and RHS are guaranteed to be the same value type
541 EVT CompareVT = LHS.getValueType();
542
543 // Check if we can lower this to a native operation.
544
545 // Try to lower to a CND* instruction:
546 // CND* instructions requires RHS to be zero. Some SELECT_CC nodes that
547 // can be lowered to CND* instructions can also be lowered to SET*
548 // instructions. CND* instructions are cheaper, because they dont't
549 // require additional instructions to convert their result to the correct
550 // value type, so this check should be first.
551 if (isZero(LHS) || isZero(RHS)) {
552 SDValue Cond = (isZero(LHS) ? RHS : LHS);
553 SDValue Zero = (isZero(LHS) ? LHS : RHS);
554 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
555 if (CompareVT != VT) {
556 // Bitcast True / False to the correct types. This will end up being
557 // a nop, but it allows us to define only a single pattern in the
558 // .TD files for each CND* instruction rather than having to have
559 // one pattern for integer True/False and one for fp True/False
560 True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
561 False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
562 }
563 if (isZero(LHS)) {
564 CCOpcode = ISD::getSetCCSwappedOperands(CCOpcode);
565 }
566
567 switch (CCOpcode) {
568 case ISD::SETONE:
569 case ISD::SETUNE:
570 case ISD::SETNE:
571 case ISD::SETULE:
572 case ISD::SETULT:
573 case ISD::SETOLE:
574 case ISD::SETOLT:
575 case ISD::SETLE:
576 case ISD::SETLT:
577 CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
578 Temp = True;
579 True = False;
580 False = Temp;
581 break;
582 default:
583 break;
584 }
585 SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
586 Cond, Zero,
587 True, False,
588 DAG.getCondCode(CCOpcode));
589 return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
590 }
591
592 // Try to lower to a SET* instruction:
Tom Stellarde06163a2013-02-07 14:02:35 +0000593 //
594 // CompareVT == MVT::f32 and VT == MVT::i32 is supported by the hardware,
595 // but for the other case where CompareVT != VT, all operands of
596 // SELECT_CC need to have the same value type, so we need to change True and
597 // False to be the same type as LHS and RHS, and then convert the result of
598 // the select_cc back to the correct type.
Tom Stellard75aadc22012-12-11 21:25:42 +0000599
600 // Move hardware True/False values to the correct operand.
601 if (isHWTrueValue(False) && isHWFalseValue(True)) {
602 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
603 std::swap(False, True);
604 CC = DAG.getCondCode(ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32));
605 }
606
607 if (isHWTrueValue(True) && isHWFalseValue(False)) {
Tom Stellarde06163a2013-02-07 14:02:35 +0000608 if (CompareVT != VT && VT == MVT::f32 && CompareVT == MVT::i32) {
609 SDValue Boolean = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
610 LHS, RHS,
611 DAG.getConstant(-1, MVT::i32),
612 DAG.getConstant(0, MVT::i32),
613 CC);
614 // Convert integer values of true (-1) and false (0) to fp values of
615 // true (1.0f) and false (0.0f).
616 SDValue LSB = DAG.getNode(ISD::AND, DL, MVT::i32, Boolean,
617 DAG.getConstant(1, MVT::i32));
618 return DAG.getNode(ISD::UINT_TO_FP, DL, VT, LSB);
Tom Stellard75aadc22012-12-11 21:25:42 +0000619 } else {
620 // This SELECT_CC is already legal.
621 return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
622 }
623 }
624
625 // Possible Min/Max pattern
626 SDValue MinMax = LowerMinMax(Op, DAG);
627 if (MinMax.getNode()) {
628 return MinMax;
629 }
630
631 // If we make it this for it means we have no native instructions to handle
632 // this SELECT_CC, so we must lower it.
633 SDValue HWTrue, HWFalse;
634
635 if (CompareVT == MVT::f32) {
636 HWTrue = DAG.getConstantFP(1.0f, CompareVT);
637 HWFalse = DAG.getConstantFP(0.0f, CompareVT);
638 } else if (CompareVT == MVT::i32) {
639 HWTrue = DAG.getConstant(-1, CompareVT);
640 HWFalse = DAG.getConstant(0, CompareVT);
641 }
642 else {
643 assert(!"Unhandled value type in LowerSELECT_CC");
644 }
645
646 // Lower this unsupported SELECT_CC into a combination of two supported
647 // SELECT_CC operations.
648 SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
649
650 return DAG.getNode(ISD::SELECT_CC, DL, VT,
651 Cond, HWFalse,
652 True, False,
653 DAG.getCondCode(ISD::SETNE));
654}
655
656SDValue R600TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
657 return DAG.getNode(ISD::SELECT_CC,
658 Op.getDebugLoc(),
659 Op.getValueType(),
660 Op.getOperand(0),
661 DAG.getConstant(0, MVT::i32),
662 Op.getOperand(1),
663 Op.getOperand(2),
664 DAG.getCondCode(ISD::SETNE));
665}
666
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000667/// LLVM generates byte-addresed pointers. For indirect addressing, we need to
668/// convert these pointers to a register index. Each register holds
669/// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
670/// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
671/// for indirect addressing.
672SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
673 unsigned StackWidth,
674 SelectionDAG &DAG) const {
675 unsigned SRLPad;
676 switch(StackWidth) {
677 case 1:
678 SRLPad = 2;
679 break;
680 case 2:
681 SRLPad = 3;
682 break;
683 case 4:
684 SRLPad = 4;
685 break;
686 default: llvm_unreachable("Invalid stack width");
687 }
688
689 return DAG.getNode(ISD::SRL, Ptr.getDebugLoc(), Ptr.getValueType(), Ptr,
690 DAG.getConstant(SRLPad, MVT::i32));
691}
692
693void R600TargetLowering::getStackAddress(unsigned StackWidth,
694 unsigned ElemIdx,
695 unsigned &Channel,
696 unsigned &PtrIncr) const {
697 switch (StackWidth) {
698 default:
699 case 1:
700 Channel = 0;
701 if (ElemIdx > 0) {
702 PtrIncr = 1;
703 } else {
704 PtrIncr = 0;
705 }
706 break;
707 case 2:
708 Channel = ElemIdx % 2;
709 if (ElemIdx == 2) {
710 PtrIncr = 1;
711 } else {
712 PtrIncr = 0;
713 }
714 break;
715 case 4:
716 Channel = ElemIdx;
717 PtrIncr = 0;
718 break;
719 }
720}
721
Tom Stellard75aadc22012-12-11 21:25:42 +0000722SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
723 DebugLoc DL = Op.getDebugLoc();
724 StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
725 SDValue Chain = Op.getOperand(0);
726 SDValue Value = Op.getOperand(1);
727 SDValue Ptr = Op.getOperand(2);
728
729 if (StoreNode->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
730 Ptr->getOpcode() != AMDGPUISD::DWORDADDR) {
731 // Convert pointer from byte address to dword address.
732 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, Ptr.getValueType(),
733 DAG.getNode(ISD::SRL, DL, Ptr.getValueType(),
734 Ptr, DAG.getConstant(2, MVT::i32)));
735
736 if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
737 assert(!"Truncated and indexed stores not supported yet");
738 } else {
739 Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
740 }
741 return Chain;
742 }
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000743
744 EVT ValueVT = Value.getValueType();
745
746 if (StoreNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
747 return SDValue();
748 }
749
750 // Lowering for indirect addressing
751
752 const MachineFunction &MF = DAG.getMachineFunction();
753 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
754 getTargetMachine().getFrameLowering());
755 unsigned StackWidth = TFL->getStackWidth(MF);
756
757 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
758
759 if (ValueVT.isVector()) {
760 unsigned NumElemVT = ValueVT.getVectorNumElements();
761 EVT ElemVT = ValueVT.getVectorElementType();
762 SDValue Stores[4];
763
764 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
765 "vector width in load");
766
767 for (unsigned i = 0; i < NumElemVT; ++i) {
768 unsigned Channel, PtrIncr;
769 getStackAddress(StackWidth, i, Channel, PtrIncr);
770 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
771 DAG.getConstant(PtrIncr, MVT::i32));
772 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT,
773 Value, DAG.getConstant(i, MVT::i32));
774
775 Stores[i] = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
776 Chain, Elem, Ptr,
777 DAG.getTargetConstant(Channel, MVT::i32));
778 }
779 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Stores, NumElemVT);
780 } else {
781 if (ValueVT == MVT::i8) {
782 Value = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Value);
783 }
784 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other, Chain, Value, Ptr,
785 DAG.getTargetConstant(0, MVT::i32)); // Channel
786 }
787
788 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +0000789}
790
Tom Stellard365366f2013-01-23 02:09:06 +0000791// return (512 + (kc_bank << 12)
792static int
793ConstantAddressBlock(unsigned AddressSpace) {
794 switch (AddressSpace) {
795 case AMDGPUAS::CONSTANT_BUFFER_0:
796 return 512;
797 case AMDGPUAS::CONSTANT_BUFFER_1:
798 return 512 + 4096;
799 case AMDGPUAS::CONSTANT_BUFFER_2:
800 return 512 + 4096 * 2;
801 case AMDGPUAS::CONSTANT_BUFFER_3:
802 return 512 + 4096 * 3;
803 case AMDGPUAS::CONSTANT_BUFFER_4:
804 return 512 + 4096 * 4;
805 case AMDGPUAS::CONSTANT_BUFFER_5:
806 return 512 + 4096 * 5;
807 case AMDGPUAS::CONSTANT_BUFFER_6:
808 return 512 + 4096 * 6;
809 case AMDGPUAS::CONSTANT_BUFFER_7:
810 return 512 + 4096 * 7;
811 case AMDGPUAS::CONSTANT_BUFFER_8:
812 return 512 + 4096 * 8;
813 case AMDGPUAS::CONSTANT_BUFFER_9:
814 return 512 + 4096 * 9;
815 case AMDGPUAS::CONSTANT_BUFFER_10:
816 return 512 + 4096 * 10;
817 case AMDGPUAS::CONSTANT_BUFFER_11:
818 return 512 + 4096 * 11;
819 case AMDGPUAS::CONSTANT_BUFFER_12:
820 return 512 + 4096 * 12;
821 case AMDGPUAS::CONSTANT_BUFFER_13:
822 return 512 + 4096 * 13;
823 case AMDGPUAS::CONSTANT_BUFFER_14:
824 return 512 + 4096 * 14;
825 case AMDGPUAS::CONSTANT_BUFFER_15:
826 return 512 + 4096 * 15;
827 default:
828 return -1;
829 }
830}
831
832SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const
833{
834 EVT VT = Op.getValueType();
835 DebugLoc DL = Op.getDebugLoc();
836 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
837 SDValue Chain = Op.getOperand(0);
838 SDValue Ptr = Op.getOperand(1);
839 SDValue LoweredLoad;
840
841 int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
842 if (ConstantBlock > -1) {
843 SDValue Result;
844 if (dyn_cast<ConstantExpr>(LoadNode->getSrcValue()) ||
Vincent Lejeune743dca02013-03-05 15:04:29 +0000845 dyn_cast<Constant>(LoadNode->getSrcValue()) ||
846 dyn_cast<ConstantSDNode>(Ptr)) {
Tom Stellard365366f2013-01-23 02:09:06 +0000847 SDValue Slots[4];
848 for (unsigned i = 0; i < 4; i++) {
849 // We want Const position encoded with the following formula :
850 // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
851 // const_index is Ptr computed by llvm using an alignment of 16.
852 // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
853 // then div by 4 at the ISel step
854 SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
855 DAG.getConstant(4 * i + ConstantBlock * 16, MVT::i32));
856 Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
857 }
858 Result = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, Slots, 4);
859 } else {
860 // non constant ptr cant be folded, keeps it as a v4f32 load
861 Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
Vincent Lejeune743dca02013-03-05 15:04:29 +0000862 DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr, DAG.getConstant(4, MVT::i32)),
Christian Konig189357c2013-03-07 09:03:59 +0000863 DAG.getConstant(LoadNode->getAddressSpace() -
864 AMDGPUAS::CONSTANT_BUFFER_0, MVT::i32)
Tom Stellard365366f2013-01-23 02:09:06 +0000865 );
866 }
867
868 if (!VT.isVector()) {
869 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
870 DAG.getConstant(0, MVT::i32));
871 }
872
873 SDValue MergedValues[2] = {
874 Result,
875 Chain
876 };
877 return DAG.getMergeValues(MergedValues, 2, DL);
878 }
879
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000880 if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
881 return SDValue();
882 }
883
884 // Lowering for indirect addressing
885 const MachineFunction &MF = DAG.getMachineFunction();
886 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
887 getTargetMachine().getFrameLowering());
888 unsigned StackWidth = TFL->getStackWidth(MF);
889
890 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
891
892 if (VT.isVector()) {
893 unsigned NumElemVT = VT.getVectorNumElements();
894 EVT ElemVT = VT.getVectorElementType();
895 SDValue Loads[4];
896
897 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
898 "vector width in load");
899
900 for (unsigned i = 0; i < NumElemVT; ++i) {
901 unsigned Channel, PtrIncr;
902 getStackAddress(StackWidth, i, Channel, PtrIncr);
903 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
904 DAG.getConstant(PtrIncr, MVT::i32));
905 Loads[i] = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, ElemVT,
906 Chain, Ptr,
907 DAG.getTargetConstant(Channel, MVT::i32),
908 Op.getOperand(2));
909 }
910 for (unsigned i = NumElemVT; i < 4; ++i) {
911 Loads[i] = DAG.getUNDEF(ElemVT);
912 }
913 EVT TargetVT = EVT::getVectorVT(*DAG.getContext(), ElemVT, 4);
914 LoweredLoad = DAG.getNode(ISD::BUILD_VECTOR, DL, TargetVT, Loads, 4);
915 } else {
916 LoweredLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, VT,
917 Chain, Ptr,
918 DAG.getTargetConstant(0, MVT::i32), // Channel
919 Op.getOperand(2));
920 }
921
922 SDValue Ops[2];
923 Ops[0] = LoweredLoad;
924 Ops[1] = Chain;
925
926 return DAG.getMergeValues(Ops, 2, DL);
Tom Stellard365366f2013-01-23 02:09:06 +0000927}
Tom Stellard75aadc22012-12-11 21:25:42 +0000928
929SDValue R600TargetLowering::LowerFPOW(SDValue Op,
930 SelectionDAG &DAG) const {
931 DebugLoc DL = Op.getDebugLoc();
932 EVT VT = Op.getValueType();
933 SDValue LogBase = DAG.getNode(ISD::FLOG2, DL, VT, Op.getOperand(0));
934 SDValue MulLogBase = DAG.getNode(ISD::FMUL, DL, VT, Op.getOperand(1), LogBase);
935 return DAG.getNode(ISD::FEXP2, DL, VT, MulLogBase);
936}
937
938/// XXX Only kernel functions are supported, so we can assume for now that
939/// every function is a kernel function, but in the future we should use
940/// separate calling conventions for kernel and non-kernel functions.
941SDValue R600TargetLowering::LowerFormalArguments(
942 SDValue Chain,
943 CallingConv::ID CallConv,
944 bool isVarArg,
945 const SmallVectorImpl<ISD::InputArg> &Ins,
946 DebugLoc DL, SelectionDAG &DAG,
947 SmallVectorImpl<SDValue> &InVals) const {
948 unsigned ParamOffsetBytes = 36;
949 Function::const_arg_iterator FuncArg =
950 DAG.getMachineFunction().getFunction()->arg_begin();
951 for (unsigned i = 0, e = Ins.size(); i < e; ++i, ++FuncArg) {
952 EVT VT = Ins[i].VT;
953 Type *ArgType = FuncArg->getType();
954 unsigned ArgSizeInBits = ArgType->isPointerTy() ?
955 32 : ArgType->getPrimitiveSizeInBits();
956 unsigned ArgBytes = ArgSizeInBits >> 3;
957 EVT ArgVT;
958 if (ArgSizeInBits < VT.getSizeInBits()) {
959 assert(!ArgType->isFloatTy() &&
960 "Extending floating point arguments not supported yet");
961 ArgVT = MVT::getIntegerVT(ArgSizeInBits);
962 } else {
963 ArgVT = VT;
964 }
965 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
966 AMDGPUAS::PARAM_I_ADDRESS);
967 SDValue Arg = DAG.getExtLoad(ISD::ZEXTLOAD, DL, VT, DAG.getRoot(),
968 DAG.getConstant(ParamOffsetBytes, MVT::i32),
Tom Stellard8d469ed2013-02-19 15:22:44 +0000969 MachinePointerInfo(UndefValue::get(PtrTy)),
Tom Stellard75aadc22012-12-11 21:25:42 +0000970 ArgVT, false, false, ArgBytes);
971 InVals.push_back(Arg);
972 ParamOffsetBytes += ArgBytes;
973 }
974 return Chain;
975}
976
977EVT R600TargetLowering::getSetCCResultType(EVT VT) const {
978 if (!VT.isVector()) return MVT::i32;
979 return VT.changeVectorElementTypeToInteger();
980}
981
982//===----------------------------------------------------------------------===//
983// Custom DAG Optimizations
984//===----------------------------------------------------------------------===//
985
986SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
987 DAGCombinerInfo &DCI) const {
988 SelectionDAG &DAG = DCI.DAG;
989
990 switch (N->getOpcode()) {
991 // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
992 case ISD::FP_ROUND: {
993 SDValue Arg = N->getOperand(0);
994 if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
995 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), N->getValueType(0),
996 Arg.getOperand(0));
997 }
998 break;
999 }
Tom Stellarde06163a2013-02-07 14:02:35 +00001000
1001 // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
1002 // (i32 select_cc f32, f32, -1, 0 cc)
1003 //
1004 // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1005 // this to one of the SET*_DX10 instructions.
1006 case ISD::FP_TO_SINT: {
1007 SDValue FNeg = N->getOperand(0);
1008 if (FNeg.getOpcode() != ISD::FNEG) {
1009 return SDValue();
1010 }
1011 SDValue SelectCC = FNeg.getOperand(0);
1012 if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1013 SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1014 SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1015 !isHWTrueValue(SelectCC.getOperand(2)) ||
1016 !isHWFalseValue(SelectCC.getOperand(3))) {
1017 return SDValue();
1018 }
1019
1020 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), N->getValueType(0),
1021 SelectCC.getOperand(0), // LHS
1022 SelectCC.getOperand(1), // RHS
1023 DAG.getConstant(-1, MVT::i32), // True
1024 DAG.getConstant(0, MVT::i32), // Flase
1025 SelectCC.getOperand(4)); // CC
1026
1027 break;
1028 }
Tom Stellard365366f2013-01-23 02:09:06 +00001029 // Extract_vec (Build_vector) generated by custom lowering
1030 // also needs to be customly combined
1031 case ISD::EXTRACT_VECTOR_ELT: {
1032 SDValue Arg = N->getOperand(0);
1033 if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1034 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1035 unsigned Element = Const->getZExtValue();
1036 return Arg->getOperand(Element);
1037 }
1038 }
Tom Stellarddd04c832013-01-31 22:11:53 +00001039 if (Arg.getOpcode() == ISD::BITCAST &&
1040 Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
1041 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1042 unsigned Element = Const->getZExtValue();
1043 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), N->getVTList(),
1044 Arg->getOperand(0).getOperand(Element));
1045 }
1046 }
Tom Stellard365366f2013-01-23 02:09:06 +00001047 }
Tom Stellarde06163a2013-02-07 14:02:35 +00001048
1049 case ISD::SELECT_CC: {
1050 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1051 // selectcc x, y, a, b, inv(cc)
1052 SDValue LHS = N->getOperand(0);
1053 if (LHS.getOpcode() != ISD::SELECT_CC) {
1054 return SDValue();
1055 }
1056
1057 SDValue RHS = N->getOperand(1);
1058 SDValue True = N->getOperand(2);
1059 SDValue False = N->getOperand(3);
1060
1061 if (LHS.getOperand(2).getNode() != True.getNode() ||
1062 LHS.getOperand(3).getNode() != False.getNode() ||
1063 RHS.getNode() != False.getNode() ||
1064 cast<CondCodeSDNode>(N->getOperand(4))->get() != ISD::SETEQ) {
1065 return SDValue();
1066 }
1067
1068 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(LHS->getOperand(4))->get();
1069 CCOpcode = ISD::getSetCCInverse(
1070 CCOpcode, LHS.getOperand(0).getValueType().isInteger());
1071 return DAG.getSelectCC(N->getDebugLoc(),
1072 LHS.getOperand(0),
1073 LHS.getOperand(1),
1074 LHS.getOperand(2),
1075 LHS.getOperand(3),
1076 CCOpcode);
Vincent Lejeuned80bc152013-02-14 16:55:06 +00001077 }
1078 case AMDGPUISD::EXPORT: {
1079 SDValue Arg = N->getOperand(1);
1080 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1081 break;
1082 SDValue NewBldVec[4] = {
1083 DAG.getUNDEF(MVT::f32),
1084 DAG.getUNDEF(MVT::f32),
1085 DAG.getUNDEF(MVT::f32),
1086 DAG.getUNDEF(MVT::f32)
1087 };
1088 SDValue NewArgs[8] = {
1089 N->getOperand(0), // Chain
1090 SDValue(),
1091 N->getOperand(2), // ArrayBase
1092 N->getOperand(3), // Type
1093 N->getOperand(4), // SWZ_X
1094 N->getOperand(5), // SWZ_Y
1095 N->getOperand(6), // SWZ_Z
1096 N->getOperand(7) // SWZ_W
1097 };
1098 for (unsigned i = 0; i < Arg.getNumOperands(); i++) {
1099 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Arg.getOperand(i))) {
1100 if (C->isZero()) {
1101 NewArgs[4 + i] = DAG.getConstant(4, MVT::i32); // SEL_0
1102 } else if (C->isExactlyValue(1.0)) {
1103 NewArgs[4 + i] = DAG.getConstant(5, MVT::i32); // SEL_0
1104 } else {
1105 NewBldVec[i] = Arg.getOperand(i);
1106 }
1107 } else {
1108 NewBldVec[i] = Arg.getOperand(i);
1109 }
1110 }
1111 DebugLoc DL = N->getDebugLoc();
1112 NewArgs[1] = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4f32, NewBldVec, 4);
1113 return DAG.getNode(AMDGPUISD::EXPORT, DL, N->getVTList(), NewArgs, 8);
Tom Stellarde06163a2013-02-07 14:02:35 +00001114 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001115 }
1116 return SDValue();
1117}