blob: 40858901430ce8b42de2fed93980228d962f448c [file] [log] [blame]
Tom Stellardf98f2ce2012-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);
Christian Konige9ba1832013-02-16 11:28:30 +000034 addRegisterClass(MVT::i1, &AMDGPU::SReg_64RegClass);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000035
Tom Stellard36ba9092013-02-07 17:02:09 +000036 addRegisterClass(MVT::v1i32, &AMDGPU::VReg_32RegClass);
37 addRegisterClass(MVT::v2i32, &AMDGPU::VReg_64RegClass);
38 addRegisterClass(MVT::v4i32, &AMDGPU::VReg_128RegClass);
39 addRegisterClass(MVT::v8i32, &AMDGPU::VReg_256RegClass);
40 addRegisterClass(MVT::v16i32, &AMDGPU::VReg_512RegClass);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000041
42 computeRegisterProperties();
43
Tom Stellardf98f2ce2012-12-11 21:25:42 +000044 setOperationAction(ISD::ADD, MVT::i64, Legal);
45 setOperationAction(ISD::ADD, MVT::i32, Legal);
46
Tom Stellardf98f2ce2012-12-11 21:25:42 +000047 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
48
49 // We need to custom lower loads from the USER_SGPR address space, so we can
50 // add the SGPRs as livein registers.
51 setOperationAction(ISD::LOAD, MVT::i32, Custom);
52 setOperationAction(ISD::LOAD, MVT::i64, Custom);
53
54 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
55 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
56
57 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
58 setTargetDAGCombine(ISD::SELECT_CC);
59
60 setTargetDAGCombine(ISD::SETCC);
61}
62
63MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
64 MachineInstr * MI, MachineBasicBlock * BB) const {
65 const TargetInstrInfo * TII = getTargetMachine().getInstrInfo();
66 MachineRegisterInfo & MRI = BB->getParent()->getRegInfo();
67 MachineBasicBlock::iterator I = MI;
68
Tom Stellardf98f2ce2012-12-11 21:25:42 +000069 switch (MI->getOpcode()) {
70 default:
71 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
72 case AMDGPU::BRANCH: return BB;
73 case AMDGPU::CLAMP_SI:
Michel Danzer311ea662013-02-11 15:58:21 +000074 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::V_ADD_F32_e64))
Tom Stellardf98f2ce2012-12-11 21:25:42 +000075 .addOperand(MI->getOperand(0))
76 .addOperand(MI->getOperand(1))
Christian Konige25e4902013-02-16 11:28:22 +000077 .addImm(0x80) // SRC1
78 .addImm(0x80) // SRC2
Tom Stellardf98f2ce2012-12-11 21:25:42 +000079 .addImm(0) // ABS
80 .addImm(1) // CLAMP
81 .addImm(0) // OMOD
82 .addImm(0); // NEG
83 MI->eraseFromParent();
84 break;
85
86 case AMDGPU::FABS_SI:
Michel Danzer311ea662013-02-11 15:58:21 +000087 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::V_ADD_F32_e64))
Tom Stellardf98f2ce2012-12-11 21:25:42 +000088 .addOperand(MI->getOperand(0))
89 .addOperand(MI->getOperand(1))
Christian Konige25e4902013-02-16 11:28:22 +000090 .addImm(0x80) // SRC1
91 .addImm(0x80) // SRC2
Tom Stellardf98f2ce2012-12-11 21:25:42 +000092 .addImm(1) // ABS
93 .addImm(0) // CLAMP
94 .addImm(0) // OMOD
95 .addImm(0); // NEG
96 MI->eraseFromParent();
97 break;
98
99 case AMDGPU::FNEG_SI:
Michel Danzer311ea662013-02-11 15:58:21 +0000100 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::V_ADD_F32_e64))
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000101 .addOperand(MI->getOperand(0))
102 .addOperand(MI->getOperand(1))
Christian Konige25e4902013-02-16 11:28:22 +0000103 .addImm(0x80) // SRC1
104 .addImm(0x80) // SRC2
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000105 .addImm(0) // ABS
106 .addImm(0) // CLAMP
107 .addImm(0) // OMOD
108 .addImm(1); // NEG
109 MI->eraseFromParent();
110 break;
111 case AMDGPU::SHADER_TYPE:
112 BB->getParent()->getInfo<SIMachineFunctionInfo>()->ShaderType =
113 MI->getOperand(0).getImm();
114 MI->eraseFromParent();
115 break;
116
117 case AMDGPU::SI_INTERP:
118 LowerSI_INTERP(MI, *BB, I, MRI);
119 break;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000120 case AMDGPU::SI_WQM:
121 LowerSI_WQM(MI, *BB, I, MRI);
122 break;
123 case AMDGPU::SI_V_CNDLT:
124 LowerSI_V_CNDLT(MI, *BB, I, MRI);
125 break;
126 }
127 return BB;
128}
129
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000130void SITargetLowering::LowerSI_WQM(MachineInstr *MI, MachineBasicBlock &BB,
131 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
132 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_WQM_B64), AMDGPU::EXEC)
133 .addReg(AMDGPU::EXEC);
134
135 MI->eraseFromParent();
136}
137
138void SITargetLowering::LowerSI_INTERP(MachineInstr *MI, MachineBasicBlock &BB,
139 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
140 unsigned tmp = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
141 unsigned M0 = MRI.createVirtualRegister(&AMDGPU::M0RegRegClass);
142 MachineOperand dst = MI->getOperand(0);
143 MachineOperand iReg = MI->getOperand(1);
144 MachineOperand jReg = MI->getOperand(2);
145 MachineOperand attr_chan = MI->getOperand(3);
146 MachineOperand attr = MI->getOperand(4);
147 MachineOperand params = MI->getOperand(5);
148
149 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_MOV_B32), M0)
150 .addOperand(params);
151
152 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P1_F32), tmp)
153 .addOperand(iReg)
154 .addOperand(attr_chan)
155 .addOperand(attr)
156 .addReg(M0);
157
158 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P2_F32))
159 .addOperand(dst)
160 .addReg(tmp)
161 .addOperand(jReg)
162 .addOperand(attr_chan)
163 .addOperand(attr)
164 .addReg(M0);
165
166 MI->eraseFromParent();
167}
168
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000169void SITargetLowering::LowerSI_V_CNDLT(MachineInstr *MI, MachineBasicBlock &BB,
170 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
171 unsigned VCC = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
172
173 BuildMI(BB, I, BB.findDebugLoc(I),
174 TII->get(AMDGPU::V_CMP_GT_F32_e32),
175 VCC)
Christian Konige25e4902013-02-16 11:28:22 +0000176 .addImm(0)
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000177 .addOperand(MI->getOperand(1));
178
179 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_CNDMASK_B32_e32))
180 .addOperand(MI->getOperand(0))
181 .addOperand(MI->getOperand(3))
182 .addOperand(MI->getOperand(2))
183 .addReg(VCC);
184
185 MI->eraseFromParent();
186}
187
188EVT SITargetLowering::getSetCCResultType(EVT VT) const {
189 return MVT::i1;
190}
191
192//===----------------------------------------------------------------------===//
193// Custom DAG Lowering Operations
194//===----------------------------------------------------------------------===//
195
196SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
197 switch (Op.getOpcode()) {
198 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000199 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000200 case ISD::LOAD: return LowerLOAD(Op, DAG);
201 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000202 case ISD::INTRINSIC_WO_CHAIN: {
203 unsigned IntrinsicID =
204 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
205 EVT VT = Op.getValueType();
206 switch (IntrinsicID) {
207 case AMDGPUIntrinsic::SI_vs_load_buffer_index:
208 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
209 AMDGPU::VGPR0, VT);
210 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
211 }
212 break;
213 }
214 }
215 return SDValue();
216}
217
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000218/// \brief Helper function for LowerBRCOND
219static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000220
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000221 SDNode *Parent = Value.getNode();
222 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
223 I != E; ++I) {
224
225 if (I.getUse().get() != Value)
226 continue;
227
228 if (I->getOpcode() == Opcode)
229 return *I;
230 }
231 return 0;
232}
233
234/// This transforms the control flow intrinsics to get the branch destination as
235/// last parameter, also switches branch target with BR if the need arise
236SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
237 SelectionDAG &DAG) const {
238
239 DebugLoc DL = BRCOND.getDebugLoc();
240
241 SDNode *Intr = BRCOND.getOperand(1).getNode();
242 SDValue Target = BRCOND.getOperand(2);
243 SDNode *BR = 0;
244
245 if (Intr->getOpcode() == ISD::SETCC) {
246 // As long as we negate the condition everything is fine
247 SDNode *SetCC = Intr;
248 assert(SetCC->getConstantOperandVal(1) == 1);
NAKAMURA Takumie13a2a32013-01-07 11:14:44 +0000249 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
250 ISD::SETNE);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000251 Intr = SetCC->getOperand(0).getNode();
252
253 } else {
254 // Get the target from BR if we don't negate the condition
255 BR = findUser(BRCOND, ISD::BR);
256 Target = BR->getOperand(1);
257 }
258
259 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
260
261 // Build the result and
262 SmallVector<EVT, 4> Res;
263 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
264 Res.push_back(Intr->getValueType(i));
265
266 // operands of the new intrinsic call
267 SmallVector<SDValue, 4> Ops;
268 Ops.push_back(BRCOND.getOperand(0));
269 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
270 Ops.push_back(Intr->getOperand(i));
271 Ops.push_back(Target);
272
273 // build the new intrinsic call
274 SDNode *Result = DAG.getNode(
275 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
276 DAG.getVTList(Res.data(), Res.size()), Ops.data(), Ops.size()).getNode();
277
278 if (BR) {
279 // Give the branch instruction our target
280 SDValue Ops[] = {
281 BR->getOperand(0),
282 BRCOND.getOperand(2)
283 };
284 DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2);
285 }
286
287 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
288
289 // Copy the intrinsic results to registers
290 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
291 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
292 if (!CopyToReg)
293 continue;
294
295 Chain = DAG.getCopyToReg(
296 Chain, DL,
297 CopyToReg->getOperand(1),
298 SDValue(Result, i - 1),
299 SDValue());
300
301 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
302 }
303
304 // Remove the old intrinsic from the chain
305 DAG.ReplaceAllUsesOfValueWith(
306 SDValue(Intr, Intr->getNumValues() - 1),
307 Intr->getOperand(0));
308
309 return Chain;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000310}
311
312SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
313 EVT VT = Op.getValueType();
314 LoadSDNode *Ptr = dyn_cast<LoadSDNode>(Op);
315
316 assert(Ptr);
317
318 unsigned AddrSpace = Ptr->getPointerInfo().getAddrSpace();
319
320 // We only need to lower USER_SGPR address space loads
321 if (AddrSpace != AMDGPUAS::USER_SGPR_ADDRESS) {
322 return SDValue();
323 }
324
325 // Loads from the USER_SGPR address space can only have constant value
326 // pointers.
327 ConstantSDNode *BasePtr = dyn_cast<ConstantSDNode>(Ptr->getBasePtr());
328 assert(BasePtr);
329
330 unsigned TypeDwordWidth = VT.getSizeInBits() / 32;
331 const TargetRegisterClass * dstClass;
332 switch (TypeDwordWidth) {
333 default:
334 assert(!"USER_SGPR value size not implemented");
335 return SDValue();
336 case 1:
337 dstClass = &AMDGPU::SReg_32RegClass;
338 break;
339 case 2:
340 dstClass = &AMDGPU::SReg_64RegClass;
341 break;
342 }
343 uint64_t Index = BasePtr->getZExtValue();
344 assert(Index % TypeDwordWidth == 0 && "USER_SGPR not properly aligned");
345 unsigned SGPRIndex = Index / TypeDwordWidth;
346 unsigned Reg = dstClass->getRegister(SGPRIndex);
347
348 DAG.ReplaceAllUsesOfValueWith(Op, CreateLiveInRegister(DAG, dstClass, Reg,
349 VT));
350 return SDValue();
351}
352
353SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
354 SDValue LHS = Op.getOperand(0);
355 SDValue RHS = Op.getOperand(1);
356 SDValue True = Op.getOperand(2);
357 SDValue False = Op.getOperand(3);
358 SDValue CC = Op.getOperand(4);
359 EVT VT = Op.getValueType();
360 DebugLoc DL = Op.getDebugLoc();
361
362 // Possible Min/Max pattern
363 SDValue MinMax = LowerMinMax(Op, DAG);
364 if (MinMax.getNode()) {
365 return MinMax;
366 }
367
368 SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC);
369 return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False);
370}
371
372//===----------------------------------------------------------------------===//
373// Custom DAG optimizations
374//===----------------------------------------------------------------------===//
375
376SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
377 DAGCombinerInfo &DCI) const {
378 SelectionDAG &DAG = DCI.DAG;
379 DebugLoc DL = N->getDebugLoc();
380 EVT VT = N->getValueType(0);
381
382 switch (N->getOpcode()) {
383 default: break;
384 case ISD::SELECT_CC: {
385 N->dump();
386 ConstantSDNode *True, *False;
387 // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
388 if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
389 && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
390 && True->isAllOnesValue()
391 && False->isNullValue()
392 && VT == MVT::i1) {
393 return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
394 N->getOperand(1), N->getOperand(4));
395
396 }
397 break;
398 }
399 case ISD::SETCC: {
400 SDValue Arg0 = N->getOperand(0);
401 SDValue Arg1 = N->getOperand(1);
402 SDValue CC = N->getOperand(2);
403 ConstantSDNode * C = NULL;
404 ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
405
406 // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
407 if (VT == MVT::i1
408 && Arg0.getOpcode() == ISD::SIGN_EXTEND
409 && Arg0.getOperand(0).getValueType() == MVT::i1
410 && (C = dyn_cast<ConstantSDNode>(Arg1))
411 && C->isNullValue()
412 && CCOp == ISD::SETNE) {
413 return SimplifySetCC(VT, Arg0.getOperand(0),
414 DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
415 }
416 break;
417 }
418 }
419 return SDValue();
420}