blob: 292ce850f7d97666a9e0c4c59b5f71ac850153f1 [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- SIISelLowering.cpp - SI 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 SI
12//
13//===----------------------------------------------------------------------===//
14
15#include "SIISelLowering.h"
16#include "AMDIL.h"
17#include "AMDILIntrinsicInfo.h"
18#include "SIInstrInfo.h"
19#include "SIMachineFunctionInfo.h"
20#include "SIRegisterInfo.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
24
25using namespace llvm;
26
27SITargetLowering::SITargetLowering(TargetMachine &TM) :
28 AMDGPUTargetLowering(TM),
29 TII(static_cast<const SIInstrInfo*>(TM.getInstrInfo())) {
30 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
31 addRegisterClass(MVT::f32, &AMDGPU::VReg_32RegClass);
32 addRegisterClass(MVT::i32, &AMDGPU::VReg_32RegClass);
33 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
34 addRegisterClass(MVT::i1, &AMDGPU::SCCRegRegClass);
35 addRegisterClass(MVT::i1, &AMDGPU::VCCRegRegClass);
36
37 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
38 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass);
39
40 computeRegisterProperties();
41
42 setOperationAction(ISD::AND, MVT::i1, Custom);
43
44 setOperationAction(ISD::ADD, MVT::i64, Legal);
45 setOperationAction(ISD::ADD, MVT::i32, Legal);
46
47 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
48
49 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
50
51 // We need to custom lower loads from the USER_SGPR address space, so we can
52 // add the SGPRs as livein registers.
53 setOperationAction(ISD::LOAD, MVT::i32, Custom);
54 setOperationAction(ISD::LOAD, MVT::i64, Custom);
55
56 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
57 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
58
59 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
60 setTargetDAGCombine(ISD::SELECT_CC);
61
62 setTargetDAGCombine(ISD::SETCC);
63}
64
65MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
66 MachineInstr * MI, MachineBasicBlock * BB) const {
67 const TargetInstrInfo * TII = getTargetMachine().getInstrInfo();
68 MachineRegisterInfo & MRI = BB->getParent()->getRegInfo();
69 MachineBasicBlock::iterator I = MI;
70
71 if (TII->get(MI->getOpcode()).TSFlags & SIInstrFlags::NEED_WAIT) {
72 AppendS_WAITCNT(MI, *BB, llvm::next(I));
73 return BB;
74 }
75
76 switch (MI->getOpcode()) {
77 default:
78 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
79 case AMDGPU::BRANCH: return BB;
80 case AMDGPU::CLAMP_SI:
81 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::V_MOV_B32_e64))
82 .addOperand(MI->getOperand(0))
83 .addOperand(MI->getOperand(1))
84 // VSRC1-2 are unused, but we still need to fill all the
85 // operand slots, so we just reuse the VSRC0 operand
86 .addOperand(MI->getOperand(1))
87 .addOperand(MI->getOperand(1))
88 .addImm(0) // ABS
89 .addImm(1) // CLAMP
90 .addImm(0) // OMOD
91 .addImm(0); // NEG
92 MI->eraseFromParent();
93 break;
94
95 case AMDGPU::FABS_SI:
96 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::V_MOV_B32_e64))
97 .addOperand(MI->getOperand(0))
98 .addOperand(MI->getOperand(1))
99 // VSRC1-2 are unused, but we still need to fill all the
100 // operand slots, so we just reuse the VSRC0 operand
101 .addOperand(MI->getOperand(1))
102 .addOperand(MI->getOperand(1))
103 .addImm(1) // ABS
104 .addImm(0) // CLAMP
105 .addImm(0) // OMOD
106 .addImm(0); // NEG
107 MI->eraseFromParent();
108 break;
109
110 case AMDGPU::FNEG_SI:
111 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::V_MOV_B32_e64))
112 .addOperand(MI->getOperand(0))
113 .addOperand(MI->getOperand(1))
114 // VSRC1-2 are unused, but we still need to fill all the
115 // operand slots, so we just reuse the VSRC0 operand
116 .addOperand(MI->getOperand(1))
117 .addOperand(MI->getOperand(1))
118 .addImm(0) // ABS
119 .addImm(0) // CLAMP
120 .addImm(0) // OMOD
121 .addImm(1); // NEG
122 MI->eraseFromParent();
123 break;
124 case AMDGPU::SHADER_TYPE:
125 BB->getParent()->getInfo<SIMachineFunctionInfo>()->ShaderType =
126 MI->getOperand(0).getImm();
127 MI->eraseFromParent();
128 break;
129
130 case AMDGPU::SI_INTERP:
131 LowerSI_INTERP(MI, *BB, I, MRI);
132 break;
133 case AMDGPU::SI_INTERP_CONST:
134 LowerSI_INTERP_CONST(MI, *BB, I, MRI);
135 break;
136 case AMDGPU::SI_KIL:
137 LowerSI_KIL(MI, *BB, I, MRI);
138 break;
139 case AMDGPU::SI_WQM:
140 LowerSI_WQM(MI, *BB, I, MRI);
141 break;
142 case AMDGPU::SI_V_CNDLT:
143 LowerSI_V_CNDLT(MI, *BB, I, MRI);
144 break;
145 }
146 return BB;
147}
148
149void SITargetLowering::AppendS_WAITCNT(MachineInstr *MI, MachineBasicBlock &BB,
150 MachineBasicBlock::iterator I) const {
151 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_WAITCNT))
152 .addImm(0);
153}
154
155
156void SITargetLowering::LowerSI_WQM(MachineInstr *MI, MachineBasicBlock &BB,
157 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
158 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_WQM_B64), AMDGPU::EXEC)
159 .addReg(AMDGPU::EXEC);
160
161 MI->eraseFromParent();
162}
163
164void SITargetLowering::LowerSI_INTERP(MachineInstr *MI, MachineBasicBlock &BB,
165 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
166 unsigned tmp = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
167 unsigned M0 = MRI.createVirtualRegister(&AMDGPU::M0RegRegClass);
168 MachineOperand dst = MI->getOperand(0);
169 MachineOperand iReg = MI->getOperand(1);
170 MachineOperand jReg = MI->getOperand(2);
171 MachineOperand attr_chan = MI->getOperand(3);
172 MachineOperand attr = MI->getOperand(4);
173 MachineOperand params = MI->getOperand(5);
174
175 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_MOV_B32), M0)
176 .addOperand(params);
177
178 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P1_F32), tmp)
179 .addOperand(iReg)
180 .addOperand(attr_chan)
181 .addOperand(attr)
182 .addReg(M0);
183
184 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P2_F32))
185 .addOperand(dst)
186 .addReg(tmp)
187 .addOperand(jReg)
188 .addOperand(attr_chan)
189 .addOperand(attr)
190 .addReg(M0);
191
192 MI->eraseFromParent();
193}
194
195void SITargetLowering::LowerSI_INTERP_CONST(MachineInstr *MI,
196 MachineBasicBlock &BB, MachineBasicBlock::iterator I,
197 MachineRegisterInfo &MRI) const {
198 MachineOperand dst = MI->getOperand(0);
199 MachineOperand attr_chan = MI->getOperand(1);
200 MachineOperand attr = MI->getOperand(2);
201 MachineOperand params = MI->getOperand(3);
202 unsigned M0 = MRI.createVirtualRegister(&AMDGPU::M0RegRegClass);
203
204 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_MOV_B32), M0)
205 .addOperand(params);
206
207 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_MOV_F32))
208 .addOperand(dst)
209 .addOperand(attr_chan)
210 .addOperand(attr)
211 .addReg(M0);
212
213 MI->eraseFromParent();
214}
215
216void SITargetLowering::LowerSI_KIL(MachineInstr *MI, MachineBasicBlock &BB,
217 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
218 // Clear this pixel from the exec mask if the operand is negative
219 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_CMPX_LE_F32_e32),
220 AMDGPU::VCC)
221 .addReg(AMDGPU::SREG_LIT_0)
222 .addOperand(MI->getOperand(0));
223
224 MI->eraseFromParent();
225}
226
227void SITargetLowering::LowerSI_V_CNDLT(MachineInstr *MI, MachineBasicBlock &BB,
228 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
229 unsigned VCC = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
230
231 BuildMI(BB, I, BB.findDebugLoc(I),
232 TII->get(AMDGPU::V_CMP_GT_F32_e32),
233 VCC)
234 .addReg(AMDGPU::SREG_LIT_0)
235 .addOperand(MI->getOperand(1));
236
237 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_CNDMASK_B32_e32))
238 .addOperand(MI->getOperand(0))
239 .addOperand(MI->getOperand(3))
240 .addOperand(MI->getOperand(2))
241 .addReg(VCC);
242
243 MI->eraseFromParent();
244}
245
246EVT SITargetLowering::getSetCCResultType(EVT VT) const {
247 return MVT::i1;
248}
249
250//===----------------------------------------------------------------------===//
251// Custom DAG Lowering Operations
252//===----------------------------------------------------------------------===//
253
254SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
255 switch (Op.getOpcode()) {
256 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
257 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
258 case ISD::LOAD: return LowerLOAD(Op, DAG);
259 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
260 case ISD::AND: return Loweri1ContextSwitch(Op, DAG, ISD::AND);
261 case ISD::INTRINSIC_WO_CHAIN: {
262 unsigned IntrinsicID =
263 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
264 EVT VT = Op.getValueType();
265 switch (IntrinsicID) {
266 case AMDGPUIntrinsic::SI_vs_load_buffer_index:
267 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
268 AMDGPU::VGPR0, VT);
269 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
270 }
271 break;
272 }
273 }
274 return SDValue();
275}
276
277/// \brief The function is for lowering i1 operations on the
278/// VCC register.
279///
280/// In the VALU context, VCC is a one bit register, but in the
281/// SALU context the VCC is a 64-bit register (1-bit per thread). Since only
282/// the SALU can perform operations on the VCC register, we need to promote
283/// the operand types from i1 to i64 in order for tablegen to be able to match
284/// this operation to the correct SALU instruction. We do this promotion by
285/// wrapping the operands in a CopyToReg node.
286///
287SDValue SITargetLowering::Loweri1ContextSwitch(SDValue Op,
288 SelectionDAG &DAG,
289 unsigned VCCNode) const {
290 DebugLoc DL = Op.getDebugLoc();
291
292 SDValue OpNode = DAG.getNode(VCCNode, DL, MVT::i64,
293 DAG.getNode(SIISD::VCC_BITCAST, DL, MVT::i64,
294 Op.getOperand(0)),
295 DAG.getNode(SIISD::VCC_BITCAST, DL, MVT::i64,
296 Op.getOperand(1)));
297
298 return DAG.getNode(SIISD::VCC_BITCAST, DL, MVT::i1, OpNode);
299}
300
301SDValue SITargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
302 SDValue Chain = Op.getOperand(0);
303 SDValue CC = Op.getOperand(1);
304 SDValue LHS = Op.getOperand(2);
305 SDValue RHS = Op.getOperand(3);
306 SDValue JumpT = Op.getOperand(4);
307 SDValue CmpValue;
308 SDValue Result;
309 CmpValue = DAG.getNode(
310 ISD::SETCC,
311 Op.getDebugLoc(),
312 MVT::i1,
313 LHS, RHS,
314 CC);
315
316 Result = DAG.getNode(
317 AMDGPUISD::BRANCH_COND,
318 CmpValue.getDebugLoc(),
319 MVT::Other, Chain,
320 JumpT, CmpValue);
321 return Result;
322}
323
324SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
325 EVT VT = Op.getValueType();
326 LoadSDNode *Ptr = dyn_cast<LoadSDNode>(Op);
327
328 assert(Ptr);
329
330 unsigned AddrSpace = Ptr->getPointerInfo().getAddrSpace();
331
332 // We only need to lower USER_SGPR address space loads
333 if (AddrSpace != AMDGPUAS::USER_SGPR_ADDRESS) {
334 return SDValue();
335 }
336
337 // Loads from the USER_SGPR address space can only have constant value
338 // pointers.
339 ConstantSDNode *BasePtr = dyn_cast<ConstantSDNode>(Ptr->getBasePtr());
340 assert(BasePtr);
341
342 unsigned TypeDwordWidth = VT.getSizeInBits() / 32;
343 const TargetRegisterClass * dstClass;
344 switch (TypeDwordWidth) {
345 default:
346 assert(!"USER_SGPR value size not implemented");
347 return SDValue();
348 case 1:
349 dstClass = &AMDGPU::SReg_32RegClass;
350 break;
351 case 2:
352 dstClass = &AMDGPU::SReg_64RegClass;
353 break;
354 }
355 uint64_t Index = BasePtr->getZExtValue();
356 assert(Index % TypeDwordWidth == 0 && "USER_SGPR not properly aligned");
357 unsigned SGPRIndex = Index / TypeDwordWidth;
358 unsigned Reg = dstClass->getRegister(SGPRIndex);
359
360 DAG.ReplaceAllUsesOfValueWith(Op, CreateLiveInRegister(DAG, dstClass, Reg,
361 VT));
362 return SDValue();
363}
364
365SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
366 SDValue LHS = Op.getOperand(0);
367 SDValue RHS = Op.getOperand(1);
368 SDValue True = Op.getOperand(2);
369 SDValue False = Op.getOperand(3);
370 SDValue CC = Op.getOperand(4);
371 EVT VT = Op.getValueType();
372 DebugLoc DL = Op.getDebugLoc();
373
374 // Possible Min/Max pattern
375 SDValue MinMax = LowerMinMax(Op, DAG);
376 if (MinMax.getNode()) {
377 return MinMax;
378 }
379
380 SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC);
381 return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False);
382}
383
384//===----------------------------------------------------------------------===//
385// Custom DAG optimizations
386//===----------------------------------------------------------------------===//
387
388SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
389 DAGCombinerInfo &DCI) const {
390 SelectionDAG &DAG = DCI.DAG;
391 DebugLoc DL = N->getDebugLoc();
392 EVT VT = N->getValueType(0);
393
394 switch (N->getOpcode()) {
395 default: break;
396 case ISD::SELECT_CC: {
397 N->dump();
398 ConstantSDNode *True, *False;
399 // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
400 if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
401 && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
402 && True->isAllOnesValue()
403 && False->isNullValue()
404 && VT == MVT::i1) {
405 return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
406 N->getOperand(1), N->getOperand(4));
407
408 }
409 break;
410 }
411 case ISD::SETCC: {
412 SDValue Arg0 = N->getOperand(0);
413 SDValue Arg1 = N->getOperand(1);
414 SDValue CC = N->getOperand(2);
415 ConstantSDNode * C = NULL;
416 ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
417
418 // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
419 if (VT == MVT::i1
420 && Arg0.getOpcode() == ISD::SIGN_EXTEND
421 && Arg0.getOperand(0).getValueType() == MVT::i1
422 && (C = dyn_cast<ConstantSDNode>(Arg1))
423 && C->isNullValue()
424 && CCOp == ISD::SETNE) {
425 return SimplifySetCC(VT, Arg0.getOperand(0),
426 DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
427 }
428 break;
429 }
430 }
431 return SDValue();
432}
433
434#define NODE_NAME_CASE(node) case SIISD::node: return #node;
435
436const char* SITargetLowering::getTargetNodeName(unsigned Opcode) const {
437 switch (Opcode) {
438 default: return AMDGPUTargetLowering::getTargetNodeName(Opcode);
439 NODE_NAME_CASE(VCC_AND)
440 NODE_NAME_CASE(VCC_BITCAST)
441 }
442}