blob: 825812f379609e831a9112c7f391a977ecf563b8 [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"
Christian Konig99ee0f42013-03-07 09:04:14 +000016#include "AMDGPU.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000017#include "AMDIL.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000018#include "AMDILIntrinsicInfo.h"
19#include "SIInstrInfo.h"
20#include "SIMachineFunctionInfo.h"
21#include "SIRegisterInfo.h"
Christian Konig2c8f6d52013-03-07 09:03:52 +000022#include "llvm/CodeGen/CallingConvLower.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000023#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/CodeGen/SelectionDAG.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000026#include "llvm/IR/Function.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000027
Tom Stellard556d9aa2013-06-03 17:39:37 +000028const uint64_t RSRC_DATA_FORMAT = 0xf00000000000LL;
29
Tom Stellard75aadc22012-12-11 21:25:42 +000030using namespace llvm;
31
32SITargetLowering::SITargetLowering(TargetMachine &TM) :
33 AMDGPUTargetLowering(TM),
Christian Konigf82901a2013-02-26 17:52:23 +000034 TII(static_cast<const SIInstrInfo*>(TM.getInstrInfo())),
35 TRI(TM.getRegisterInfo()) {
Christian Konig2214f142013-03-07 09:03:38 +000036
Christian Koniga8811792013-02-16 11:28:30 +000037 addRegisterClass(MVT::i1, &AMDGPU::SReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000038 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
39
40 addRegisterClass(MVT::v16i8, &AMDGPU::SReg_128RegClass);
41 addRegisterClass(MVT::v32i8, &AMDGPU::SReg_256RegClass);
42 addRegisterClass(MVT::v64i8, &AMDGPU::SReg_512RegClass);
43
44 addRegisterClass(MVT::i32, &AMDGPU::VReg_32RegClass);
45 addRegisterClass(MVT::f32, &AMDGPU::VReg_32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000046
Tom Stellard538ceeb2013-02-07 17:02:09 +000047 addRegisterClass(MVT::v1i32, &AMDGPU::VReg_32RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000048
Tom Stellard538ceeb2013-02-07 17:02:09 +000049 addRegisterClass(MVT::v2i32, &AMDGPU::VReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000050 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
51
Tom Stellard538ceeb2013-02-07 17:02:09 +000052 addRegisterClass(MVT::v4i32, &AMDGPU::VReg_128RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000053 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
Tom Stellard754f80f2013-04-05 23:31:51 +000054 addRegisterClass(MVT::i128, &AMDGPU::SReg_128RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000055
Tom Stellard538ceeb2013-02-07 17:02:09 +000056 addRegisterClass(MVT::v8i32, &AMDGPU::VReg_256RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000057 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
58
Tom Stellard538ceeb2013-02-07 17:02:09 +000059 addRegisterClass(MVT::v16i32, &AMDGPU::VReg_512RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000060 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000061
62 computeRegisterProperties();
63
Christian Konig2989ffc2013-03-18 11:34:16 +000064 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
65 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
66 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
67 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
68
Tom Stellard75aadc22012-12-11 21:25:42 +000069 setOperationAction(ISD::ADD, MVT::i64, Legal);
70 setOperationAction(ISD::ADD, MVT::i32, Legal);
71
Tom Stellard75aadc22012-12-11 21:25:42 +000072 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
73 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
74
75 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
Tom Stellard754f80f2013-04-05 23:31:51 +000076
Tom Stellard046039e2013-06-03 17:40:03 +000077 setOperationAction(ISD::SIGN_EXTEND, MVT::i64, Custom);
78
Tom Stellard75aadc22012-12-11 21:25:42 +000079 setTargetDAGCombine(ISD::SELECT_CC);
80
81 setTargetDAGCombine(ISD::SETCC);
Michel Danzerf52a6722013-03-08 10:58:01 +000082
Christian Konigeecebd02013-03-26 14:04:02 +000083 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +000084}
85
Christian Konig2c8f6d52013-03-07 09:03:52 +000086SDValue SITargetLowering::LowerFormalArguments(
87 SDValue Chain,
88 CallingConv::ID CallConv,
89 bool isVarArg,
90 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +000091 SDLoc DL, SelectionDAG &DAG,
Christian Konig2c8f6d52013-03-07 09:03:52 +000092 SmallVectorImpl<SDValue> &InVals) const {
93
94 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
95
96 MachineFunction &MF = DAG.getMachineFunction();
97 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +000098 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Christian Konig2c8f6d52013-03-07 09:03:52 +000099
100 assert(CallConv == CallingConv::C);
101
102 SmallVector<ISD::InputArg, 16> Splits;
Christian Konig99ee0f42013-03-07 09:04:14 +0000103 uint32_t Skipped = 0;
104
105 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000106 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000107
108 // First check if it's a PS input addr
Christian Konig99ee0f42013-03-07 09:04:14 +0000109 if (Info->ShaderType == ShaderType::PIXEL && !Arg.Flags.isInReg()) {
110
111 assert((PSInputNum <= 15) && "Too many PS inputs!");
112
113 if (!Arg.Used) {
114 // We can savely skip PS inputs
115 Skipped |= 1 << i;
116 ++PSInputNum;
117 continue;
118 }
119
120 Info->PSInputAddr |= 1 << PSInputNum++;
121 }
122
123 // Second split vertices into their elements
Christian Konig2c8f6d52013-03-07 09:03:52 +0000124 if (Arg.VT.isVector()) {
125 ISD::InputArg NewArg = Arg;
126 NewArg.Flags.setSplit();
127 NewArg.VT = Arg.VT.getVectorElementType();
128
129 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
130 // three or five element vertex only needs three or five registers,
131 // NOT four or eigth.
132 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
133 unsigned NumElements = ParamType->getVectorNumElements();
134
135 for (unsigned j = 0; j != NumElements; ++j) {
136 Splits.push_back(NewArg);
137 NewArg.PartOffset += NewArg.VT.getStoreSize();
138 }
139
140 } else {
141 Splits.push_back(Arg);
142 }
143 }
144
145 SmallVector<CCValAssign, 16> ArgLocs;
146 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
147 getTargetMachine(), ArgLocs, *DAG.getContext());
148
Christian Konig99ee0f42013-03-07 09:04:14 +0000149 // At least one interpolation mode must be enabled or else the GPU will hang.
150 if (Info->ShaderType == ShaderType::PIXEL && (Info->PSInputAddr & 0x7F) == 0) {
151 Info->PSInputAddr |= 1;
152 CCInfo.AllocateReg(AMDGPU::VGPR0);
153 CCInfo.AllocateReg(AMDGPU::VGPR1);
154 }
155
Christian Konig2c8f6d52013-03-07 09:03:52 +0000156 AnalyzeFormalArguments(CCInfo, Splits);
157
158 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
159
Christian Konigb7be72d2013-05-17 09:46:48 +0000160 const ISD::InputArg &Arg = Ins[i];
Christian Konig99ee0f42013-03-07 09:04:14 +0000161 if (Skipped & (1 << i)) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000162 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000163 continue;
164 }
165
Christian Konig2c8f6d52013-03-07 09:03:52 +0000166 CCValAssign &VA = ArgLocs[ArgIdx++];
167 assert(VA.isRegLoc() && "Parameter must be in a register!");
168
169 unsigned Reg = VA.getLocReg();
170 MVT VT = VA.getLocVT();
171
172 if (VT == MVT::i64) {
173 // For now assume it is a pointer
174 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
175 &AMDGPU::SReg_64RegClass);
176 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
177 InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
178 continue;
179 }
180
181 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
182
183 Reg = MF.addLiveIn(Reg, RC);
184 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
185
Christian Konig2c8f6d52013-03-07 09:03:52 +0000186 if (Arg.VT.isVector()) {
187
188 // Build a vector from the registers
189 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
190 unsigned NumElements = ParamType->getVectorNumElements();
191
192 SmallVector<SDValue, 4> Regs;
193 Regs.push_back(Val);
194 for (unsigned j = 1; j != NumElements; ++j) {
195 Reg = ArgLocs[ArgIdx++].getLocReg();
196 Reg = MF.addLiveIn(Reg, RC);
197 Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
198 }
199
200 // Fill up the missing vector elements
201 NumElements = Arg.VT.getVectorNumElements() - NumElements;
202 for (unsigned j = 0; j != NumElements; ++j)
203 Regs.push_back(DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000204
Christian Konig2c8f6d52013-03-07 09:03:52 +0000205 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT,
206 Regs.data(), Regs.size()));
207 continue;
208 }
209
210 InVals.push_back(Val);
211 }
212 return Chain;
213}
214
Tom Stellard75aadc22012-12-11 21:25:42 +0000215MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
216 MachineInstr * MI, MachineBasicBlock * BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000217
Tom Stellard556d9aa2013-06-03 17:39:37 +0000218 MachineBasicBlock::iterator I = *MI;
219
Tom Stellard75aadc22012-12-11 21:25:42 +0000220 switch (MI->getOpcode()) {
221 default:
222 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
223 case AMDGPU::BRANCH: return BB;
Tom Stellard556d9aa2013-06-03 17:39:37 +0000224 case AMDGPU::SI_ADDR64_RSRC: {
225 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
226 unsigned SuperReg = MI->getOperand(0).getReg();
227 unsigned SubRegLo = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
228 unsigned SubRegHi = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
229 unsigned SubRegHiHi = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
230 unsigned SubRegHiLo = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
231 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B64), SubRegLo)
232 .addOperand(MI->getOperand(1));
233 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiLo)
234 .addImm(0);
235 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiHi)
236 .addImm(RSRC_DATA_FORMAT >> 32);
237 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SubRegHi)
238 .addReg(SubRegHiLo)
239 .addImm(AMDGPU::sub0)
240 .addReg(SubRegHiHi)
241 .addImm(AMDGPU::sub1);
242 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SuperReg)
243 .addReg(SubRegLo)
244 .addImm(AMDGPU::sub0_sub1)
245 .addReg(SubRegHi)
246 .addImm(AMDGPU::sub2_sub3);
247 MI->eraseFromParent();
248 break;
249 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000250 }
251 return BB;
252}
253
Matt Arsenault758659232013-05-18 00:21:46 +0000254EVT SITargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000255 return MVT::i1;
256}
257
Christian Konig082a14a2013-03-18 11:34:05 +0000258MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const {
259 return MVT::i32;
260}
261
Tom Stellard75aadc22012-12-11 21:25:42 +0000262//===----------------------------------------------------------------------===//
263// Custom DAG Lowering Operations
264//===----------------------------------------------------------------------===//
265
266SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
267 switch (Op.getOpcode()) {
268 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +0000269 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000270 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Tom Stellard046039e2013-06-03 17:40:03 +0000271 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000272 }
273 return SDValue();
274}
275
Tom Stellardf8794352012-12-19 22:10:31 +0000276/// \brief Helper function for LowerBRCOND
277static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000278
Tom Stellardf8794352012-12-19 22:10:31 +0000279 SDNode *Parent = Value.getNode();
280 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
281 I != E; ++I) {
282
283 if (I.getUse().get() != Value)
284 continue;
285
286 if (I->getOpcode() == Opcode)
287 return *I;
288 }
289 return 0;
290}
291
292/// This transforms the control flow intrinsics to get the branch destination as
293/// last parameter, also switches branch target with BR if the need arise
294SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
295 SelectionDAG &DAG) const {
296
Andrew Trickef9de2a2013-05-25 02:42:55 +0000297 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +0000298
299 SDNode *Intr = BRCOND.getOperand(1).getNode();
300 SDValue Target = BRCOND.getOperand(2);
301 SDNode *BR = 0;
302
303 if (Intr->getOpcode() == ISD::SETCC) {
304 // As long as we negate the condition everything is fine
305 SDNode *SetCC = Intr;
306 assert(SetCC->getConstantOperandVal(1) == 1);
NAKAMURA Takumi458a8272013-01-07 11:14:44 +0000307 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
308 ISD::SETNE);
Tom Stellardf8794352012-12-19 22:10:31 +0000309 Intr = SetCC->getOperand(0).getNode();
310
311 } else {
312 // Get the target from BR if we don't negate the condition
313 BR = findUser(BRCOND, ISD::BR);
314 Target = BR->getOperand(1);
315 }
316
317 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
318
319 // Build the result and
320 SmallVector<EVT, 4> Res;
321 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
322 Res.push_back(Intr->getValueType(i));
323
324 // operands of the new intrinsic call
325 SmallVector<SDValue, 4> Ops;
326 Ops.push_back(BRCOND.getOperand(0));
327 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
328 Ops.push_back(Intr->getOperand(i));
329 Ops.push_back(Target);
330
331 // build the new intrinsic call
332 SDNode *Result = DAG.getNode(
333 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
334 DAG.getVTList(Res.data(), Res.size()), Ops.data(), Ops.size()).getNode();
335
336 if (BR) {
337 // Give the branch instruction our target
338 SDValue Ops[] = {
339 BR->getOperand(0),
340 BRCOND.getOperand(2)
341 };
342 DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2);
343 }
344
345 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
346
347 // Copy the intrinsic results to registers
348 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
349 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
350 if (!CopyToReg)
351 continue;
352
353 Chain = DAG.getCopyToReg(
354 Chain, DL,
355 CopyToReg->getOperand(1),
356 SDValue(Result, i - 1),
357 SDValue());
358
359 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
360 }
361
362 // Remove the old intrinsic from the chain
363 DAG.ReplaceAllUsesOfValueWith(
364 SDValue(Intr, Intr->getNumValues() - 1),
365 Intr->getOperand(0));
366
367 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +0000368}
369
Tom Stellard75aadc22012-12-11 21:25:42 +0000370SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
371 SDValue LHS = Op.getOperand(0);
372 SDValue RHS = Op.getOperand(1);
373 SDValue True = Op.getOperand(2);
374 SDValue False = Op.getOperand(3);
375 SDValue CC = Op.getOperand(4);
376 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000377 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000378
379 // Possible Min/Max pattern
380 SDValue MinMax = LowerMinMax(Op, DAG);
381 if (MinMax.getNode()) {
382 return MinMax;
383 }
384
385 SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC);
386 return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False);
387}
388
Tom Stellard046039e2013-06-03 17:40:03 +0000389SDValue SITargetLowering::LowerSIGN_EXTEND(SDValue Op,
390 SelectionDAG &DAG) const {
391 EVT VT = Op.getValueType();
392 SDLoc DL(Op);
393
394 if (VT != MVT::i64) {
395 return SDValue();
396 }
397
398 SDValue Hi = DAG.getNode(ISD::SRA, DL, MVT::i32, Op.getOperand(0),
399 DAG.getConstant(31, MVT::i32));
400
401 return DAG.getNode(ISD::BUILD_PAIR, DL, VT, Op.getOperand(0), Hi);
402}
403
Tom Stellard75aadc22012-12-11 21:25:42 +0000404//===----------------------------------------------------------------------===//
405// Custom DAG optimizations
406//===----------------------------------------------------------------------===//
407
408SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
409 DAGCombinerInfo &DCI) const {
410 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000411 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +0000412 EVT VT = N->getValueType(0);
413
414 switch (N->getOpcode()) {
415 default: break;
416 case ISD::SELECT_CC: {
417 N->dump();
418 ConstantSDNode *True, *False;
419 // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
420 if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
421 && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
422 && True->isAllOnesValue()
423 && False->isNullValue()
424 && VT == MVT::i1) {
425 return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
426 N->getOperand(1), N->getOperand(4));
427
428 }
429 break;
430 }
431 case ISD::SETCC: {
432 SDValue Arg0 = N->getOperand(0);
433 SDValue Arg1 = N->getOperand(1);
434 SDValue CC = N->getOperand(2);
435 ConstantSDNode * C = NULL;
436 ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
437
438 // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
439 if (VT == MVT::i1
440 && Arg0.getOpcode() == ISD::SIGN_EXTEND
441 && Arg0.getOperand(0).getValueType() == MVT::i1
442 && (C = dyn_cast<ConstantSDNode>(Arg1))
443 && C->isNullValue()
444 && CCOp == ISD::SETNE) {
445 return SimplifySetCC(VT, Arg0.getOperand(0),
446 DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
447 }
448 break;
449 }
450 }
451 return SDValue();
452}
Christian Konigd910b7d2013-02-26 17:52:16 +0000453
Matt Arsenault758659232013-05-18 00:21:46 +0000454/// \brief Test if RegClass is one of the VSrc classes
Christian Konigf82901a2013-02-26 17:52:23 +0000455static bool isVSrc(unsigned RegClass) {
456 return AMDGPU::VSrc_32RegClassID == RegClass ||
457 AMDGPU::VSrc_64RegClassID == RegClass;
458}
459
Matt Arsenault758659232013-05-18 00:21:46 +0000460/// \brief Test if RegClass is one of the SSrc classes
Christian Konigf82901a2013-02-26 17:52:23 +0000461static bool isSSrc(unsigned RegClass) {
462 return AMDGPU::SSrc_32RegClassID == RegClass ||
463 AMDGPU::SSrc_64RegClassID == RegClass;
464}
465
466/// \brief Analyze the possible immediate value Op
467///
468/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
469/// and the immediate value if it's a literal immediate
470int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
471
472 union {
473 int32_t I;
474 float F;
475 } Imm;
476
Tom Stellardedbf1eb2013-04-05 23:31:20 +0000477 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
478 if (Node->getZExtValue() >> 32) {
479 return -1;
480 }
Christian Konigf82901a2013-02-26 17:52:23 +0000481 Imm.I = Node->getSExtValue();
Tom Stellardedbf1eb2013-04-05 23:31:20 +0000482 } else if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N))
Christian Konigf82901a2013-02-26 17:52:23 +0000483 Imm.F = Node->getValueAPF().convertToFloat();
484 else
485 return -1; // It isn't an immediate
486
487 if ((Imm.I >= -16 && Imm.I <= 64) ||
488 Imm.F == 0.5f || Imm.F == -0.5f ||
489 Imm.F == 1.0f || Imm.F == -1.0f ||
490 Imm.F == 2.0f || Imm.F == -2.0f ||
491 Imm.F == 4.0f || Imm.F == -4.0f)
492 return 0; // It's an inline immediate
493
494 return Imm.I; // It's a literal immediate
495}
496
497/// \brief Try to fold an immediate directly into an instruction
498bool SITargetLowering::foldImm(SDValue &Operand, int32_t &Immediate,
499 bool &ScalarSlotUsed) const {
500
501 MachineSDNode *Mov = dyn_cast<MachineSDNode>(Operand);
502 if (Mov == 0 || !TII->isMov(Mov->getMachineOpcode()))
503 return false;
504
505 const SDValue &Op = Mov->getOperand(0);
506 int32_t Value = analyzeImmediate(Op.getNode());
507 if (Value == -1) {
508 // Not an immediate at all
509 return false;
510
511 } else if (Value == 0) {
512 // Inline immediates can always be fold
513 Operand = Op;
514 return true;
515
516 } else if (Value == Immediate) {
517 // Already fold literal immediate
518 Operand = Op;
519 return true;
520
521 } else if (!ScalarSlotUsed && !Immediate) {
522 // Fold this literal immediate
523 ScalarSlotUsed = true;
524 Immediate = Value;
525 Operand = Op;
526 return true;
527
528 }
529
530 return false;
531}
532
533/// \brief Does "Op" fit into register class "RegClass" ?
Tom Stellardb35efba2013-05-20 15:02:01 +0000534bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, const SDValue &Op,
Christian Konigf82901a2013-02-26 17:52:23 +0000535 unsigned RegClass) const {
536
Matt Arsenault758659232013-05-18 00:21:46 +0000537 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
Christian Konigf82901a2013-02-26 17:52:23 +0000538 SDNode *Node = Op.getNode();
539
Christian Konig8370dbb2013-03-26 14:04:17 +0000540 const TargetRegisterClass *OpClass;
Christian Konigf82901a2013-02-26 17:52:23 +0000541 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(Node)) {
542 const MCInstrDesc &Desc = TII->get(MN->getMachineOpcode());
Christian Konig8370dbb2013-03-26 14:04:17 +0000543 int OpClassID = Desc.OpInfo[Op.getResNo()].RegClass;
Tom Stellardbad1f592013-06-03 17:39:54 +0000544 if (OpClassID == -1) {
545 switch (MN->getMachineOpcode()) {
546 case AMDGPU::REG_SEQUENCE:
547 // Operand 0 is the register class id for REG_SEQUENCE instructions.
548 OpClass = TRI->getRegClass(
549 cast<ConstantSDNode>(MN->getOperand(0))->getZExtValue());
550 break;
551 default:
552 OpClass = getRegClassFor(Op.getSimpleValueType());
553 break;
554 }
555 } else {
Christian Konig8370dbb2013-03-26 14:04:17 +0000556 OpClass = TRI->getRegClass(OpClassID);
Tom Stellardbad1f592013-06-03 17:39:54 +0000557 }
Christian Konigf82901a2013-02-26 17:52:23 +0000558
559 } else if (Node->getOpcode() == ISD::CopyFromReg) {
560 RegisterSDNode *Reg = cast<RegisterSDNode>(Node->getOperand(1).getNode());
Christian Konig8370dbb2013-03-26 14:04:17 +0000561 OpClass = MRI.getRegClass(Reg->getReg());
Christian Konigf82901a2013-02-26 17:52:23 +0000562
563 } else
564 return false;
565
Christian Konig8370dbb2013-03-26 14:04:17 +0000566 return TRI->getRegClass(RegClass)->hasSubClassEq(OpClass);
Christian Konigf82901a2013-02-26 17:52:23 +0000567}
568
569/// \brief Make sure that we don't exeed the number of allowed scalars
570void SITargetLowering::ensureSRegLimit(SelectionDAG &DAG, SDValue &Operand,
571 unsigned RegClass,
572 bool &ScalarSlotUsed) const {
573
574 // First map the operands register class to a destination class
575 if (RegClass == AMDGPU::VSrc_32RegClassID)
576 RegClass = AMDGPU::VReg_32RegClassID;
577 else if (RegClass == AMDGPU::VSrc_64RegClassID)
578 RegClass = AMDGPU::VReg_64RegClassID;
579 else
580 return;
581
582 // Nothing todo if they fit naturaly
583 if (fitsRegClass(DAG, Operand, RegClass))
584 return;
585
586 // If the scalar slot isn't used yet use it now
587 if (!ScalarSlotUsed) {
588 ScalarSlotUsed = true;
589 return;
590 }
591
592 // This is a conservative aproach, it is possible that we can't determine
593 // the correct register class and copy too often, but better save than sorry.
594 SDValue RC = DAG.getTargetConstant(RegClass, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000595 SDNode *Node = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, SDLoc(),
Christian Konigf82901a2013-02-26 17:52:23 +0000596 Operand.getValueType(), Operand, RC);
597 Operand = SDValue(Node, 0);
598}
599
Christian Konig8e06e2a2013-04-10 08:39:08 +0000600/// \brief Try to fold the Nodes operands into the Node
601SDNode *SITargetLowering::foldOperands(MachineSDNode *Node,
602 SelectionDAG &DAG) const {
Christian Konigf82901a2013-02-26 17:52:23 +0000603
604 // Original encoding (either e32 or e64)
605 int Opcode = Node->getMachineOpcode();
606 const MCInstrDesc *Desc = &TII->get(Opcode);
607
608 unsigned NumDefs = Desc->getNumDefs();
609 unsigned NumOps = Desc->getNumOperands();
610
Christian Konig3c145802013-03-27 09:12:59 +0000611 // Commuted opcode if available
612 int OpcodeRev = Desc->isCommutable() ? TII->commuteOpcode(Opcode) : -1;
613 const MCInstrDesc *DescRev = OpcodeRev == -1 ? 0 : &TII->get(OpcodeRev);
614
615 assert(!DescRev || DescRev->getNumDefs() == NumDefs);
616 assert(!DescRev || DescRev->getNumOperands() == NumOps);
617
Christian Konige500e442013-02-26 17:52:47 +0000618 // e64 version if available, -1 otherwise
619 int OpcodeE64 = AMDGPU::getVOPe64(Opcode);
620 const MCInstrDesc *DescE64 = OpcodeE64 == -1 ? 0 : &TII->get(OpcodeE64);
621
622 assert(!DescE64 || DescE64->getNumDefs() == NumDefs);
623 assert(!DescE64 || DescE64->getNumOperands() == (NumOps + 4));
624
Christian Konigf82901a2013-02-26 17:52:23 +0000625 int32_t Immediate = Desc->getSize() == 4 ? 0 : -1;
626 bool HaveVSrc = false, HaveSSrc = false;
627
628 // First figure out what we alread have in this instruction
629 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
630 i != e && Op < NumOps; ++i, ++Op) {
631
632 unsigned RegClass = Desc->OpInfo[Op].RegClass;
633 if (isVSrc(RegClass))
634 HaveVSrc = true;
635 else if (isSSrc(RegClass))
636 HaveSSrc = true;
637 else
638 continue;
639
640 int32_t Imm = analyzeImmediate(Node->getOperand(i).getNode());
641 if (Imm != -1 && Imm != 0) {
642 // Literal immediate
643 Immediate = Imm;
644 }
645 }
646
647 // If we neither have VSrc nor SSrc it makes no sense to continue
648 if (!HaveVSrc && !HaveSSrc)
649 return Node;
650
651 // No scalar allowed when we have both VSrc and SSrc
652 bool ScalarSlotUsed = HaveVSrc && HaveSSrc;
653
654 // Second go over the operands and try to fold them
655 std::vector<SDValue> Ops;
Christian Konige500e442013-02-26 17:52:47 +0000656 bool Promote2e64 = false;
Christian Konigf82901a2013-02-26 17:52:23 +0000657 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
658 i != e && Op < NumOps; ++i, ++Op) {
659
660 const SDValue &Operand = Node->getOperand(i);
661 Ops.push_back(Operand);
662
663 // Already folded immediate ?
664 if (isa<ConstantSDNode>(Operand.getNode()) ||
665 isa<ConstantFPSDNode>(Operand.getNode()))
666 continue;
667
668 // Is this a VSrc or SSrc operand ?
669 unsigned RegClass = Desc->OpInfo[Op].RegClass;
Christian Konig8370dbb2013-03-26 14:04:17 +0000670 if (isVSrc(RegClass) || isSSrc(RegClass)) {
671 // Try to fold the immediates
672 if (!foldImm(Ops[i], Immediate, ScalarSlotUsed)) {
673 // Folding didn't worked, make sure we don't hit the SReg limit
674 ensureSRegLimit(DAG, Ops[i], RegClass, ScalarSlotUsed);
675 }
676 continue;
677 }
Christian Konig6612ac32013-02-26 17:52:36 +0000678
Christian Konig3c145802013-03-27 09:12:59 +0000679 if (i == 1 && DescRev && fitsRegClass(DAG, Ops[0], RegClass)) {
Christian Konig6612ac32013-02-26 17:52:36 +0000680
Christian Konig8370dbb2013-03-26 14:04:17 +0000681 unsigned OtherRegClass = Desc->OpInfo[NumDefs].RegClass;
682 assert(isVSrc(OtherRegClass) || isSSrc(OtherRegClass));
683
684 // Test if it makes sense to swap operands
685 if (foldImm(Ops[1], Immediate, ScalarSlotUsed) ||
686 (!fitsRegClass(DAG, Ops[1], RegClass) &&
687 fitsRegClass(DAG, Ops[1], OtherRegClass))) {
Christian Konig6612ac32013-02-26 17:52:36 +0000688
689 // Swap commutable operands
690 SDValue Tmp = Ops[1];
691 Ops[1] = Ops[0];
692 Ops[0] = Tmp;
Christian Konig3c145802013-03-27 09:12:59 +0000693
694 Desc = DescRev;
695 DescRev = 0;
Christian Konig8370dbb2013-03-26 14:04:17 +0000696 continue;
Christian Konig6612ac32013-02-26 17:52:36 +0000697 }
Christian Konig6612ac32013-02-26 17:52:36 +0000698 }
Christian Konigf82901a2013-02-26 17:52:23 +0000699
Christian Konig8370dbb2013-03-26 14:04:17 +0000700 if (DescE64 && !Immediate) {
701
702 // Test if it makes sense to switch to e64 encoding
703 unsigned OtherRegClass = DescE64->OpInfo[Op].RegClass;
704 if (!isVSrc(OtherRegClass) && !isSSrc(OtherRegClass))
705 continue;
706
707 int32_t TmpImm = -1;
708 if (foldImm(Ops[i], TmpImm, ScalarSlotUsed) ||
709 (!fitsRegClass(DAG, Ops[i], RegClass) &&
710 fitsRegClass(DAG, Ops[1], OtherRegClass))) {
711
712 // Switch to e64 encoding
713 Immediate = -1;
714 Promote2e64 = true;
715 Desc = DescE64;
716 DescE64 = 0;
717 }
Christian Konigf82901a2013-02-26 17:52:23 +0000718 }
719 }
720
Christian Konige500e442013-02-26 17:52:47 +0000721 if (Promote2e64) {
722 // Add the modifier flags while promoting
723 for (unsigned i = 0; i < 4; ++i)
724 Ops.push_back(DAG.getTargetConstant(0, MVT::i32));
725 }
726
Christian Konigf82901a2013-02-26 17:52:23 +0000727 // Add optional chain and glue
728 for (unsigned i = NumOps - NumDefs, e = Node->getNumOperands(); i < e; ++i)
729 Ops.push_back(Node->getOperand(i));
730
Tom Stellardb5a97002013-06-03 17:39:50 +0000731 // Nodes that have a glue result are not CSE'd by getMachineNode(), so in
732 // this case a brand new node is always be created, even if the operands
733 // are the same as before. So, manually check if anything has been changed.
734 if (Desc->Opcode == Opcode) {
735 bool Changed = false;
736 for (unsigned i = 0, e = Node->getNumOperands(); i < e; ++i) {
737 if (Ops[i].getNode() != Node->getOperand(i).getNode()) {
738 Changed = true;
739 break;
740 }
741 }
742 if (!Changed) {
743 return Node;
744 }
745 }
746
Christian Konig3c145802013-03-27 09:12:59 +0000747 // Create a complete new instruction
Andrew Trickef9de2a2013-05-25 02:42:55 +0000748 return DAG.getMachineNode(Desc->Opcode, SDLoc(Node), Node->getVTList(), Ops);
Christian Konigd910b7d2013-02-26 17:52:16 +0000749}
Christian Konig8e06e2a2013-04-10 08:39:08 +0000750
751/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +0000752static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +0000753 switch (Idx) {
754 default: return 0;
755 case AMDGPU::sub0: return 0;
756 case AMDGPU::sub1: return 1;
757 case AMDGPU::sub2: return 2;
758 case AMDGPU::sub3: return 3;
759 }
760}
761
762/// \brief Adjust the writemask of MIMG instructions
763void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
764 SelectionDAG &DAG) const {
765 SDNode *Users[4] = { };
Christian Konig8b1ed282013-04-10 08:39:16 +0000766 unsigned Writemask = 0, Lane = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +0000767
768 // Try to figure out the used register components
769 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
770 I != E; ++I) {
771
772 // Abort if we can't understand the usage
773 if (!I->isMachineOpcode() ||
774 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
775 return;
776
Christian Konig8b1ed282013-04-10 08:39:16 +0000777 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +0000778
779 // Abort if we have more than one user per component
780 if (Users[Lane])
781 return;
782
783 Users[Lane] = *I;
784 Writemask |= 1 << Lane;
785 }
786
787 // Abort if all components are used
788 if (Writemask == 0xf)
789 return;
790
791 // Adjust the writemask in the node
792 std::vector<SDValue> Ops;
793 Ops.push_back(DAG.getTargetConstant(Writemask, MVT::i32));
794 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
795 Ops.push_back(Node->getOperand(i));
796 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops.data(), Ops.size());
797
Christian Konig8b1ed282013-04-10 08:39:16 +0000798 // If we only got one lane, replace it with a copy
799 if (Writemask == (1U << Lane)) {
800 SDValue RC = DAG.getTargetConstant(AMDGPU::VReg_32RegClassID, MVT::i32);
801 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000802 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +0000803 SDValue(Node, 0), RC);
804 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
805 return;
806 }
807
Christian Konig8e06e2a2013-04-10 08:39:08 +0000808 // Update the users of the node with the new indices
809 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
810
811 SDNode *User = Users[i];
812 if (!User)
813 continue;
814
815 SDValue Op = DAG.getTargetConstant(Idx, MVT::i32);
816 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
817
818 switch (Idx) {
819 default: break;
820 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
821 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
822 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
823 }
824 }
825}
826
827/// \brief Fold the instructions after slecting them
828SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
829 SelectionDAG &DAG) const {
Tom Stellard0518ff82013-06-03 17:39:58 +0000830 Node = AdjustRegClass(Node, DAG);
Christian Konig8e06e2a2013-04-10 08:39:08 +0000831
832 if (AMDGPU::isMIMG(Node->getMachineOpcode()) != -1)
833 adjustWritemask(Node, DAG);
834
835 return foldOperands(Node, DAG);
836}
Christian Konig8b1ed282013-04-10 08:39:16 +0000837
838/// \brief Assign the register class depending on the number of
839/// bits set in the writemask
840void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
841 SDNode *Node) const {
842 if (AMDGPU::isMIMG(MI->getOpcode()) == -1)
843 return;
844
845 unsigned VReg = MI->getOperand(0).getReg();
846 unsigned Writemask = MI->getOperand(1).getImm();
847 unsigned BitsSet = 0;
848 for (unsigned i = 0; i < 4; ++i)
849 BitsSet += Writemask & (1 << i) ? 1 : 0;
850
851 const TargetRegisterClass *RC;
852 switch (BitsSet) {
853 default: return;
854 case 1: RC = &AMDGPU::VReg_32RegClass; break;
855 case 2: RC = &AMDGPU::VReg_64RegClass; break;
856 case 3: RC = &AMDGPU::VReg_96RegClass; break;
857 }
858
859 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
860 MRI.setRegClass(VReg, RC);
861}
Tom Stellard0518ff82013-06-03 17:39:58 +0000862
863MachineSDNode *SITargetLowering::AdjustRegClass(MachineSDNode *N,
864 SelectionDAG &DAG) const {
865
866 SDLoc DL(N);
867 unsigned NewOpcode = N->getMachineOpcode();
868
869 switch (N->getMachineOpcode()) {
870 default: return N;
871 case AMDGPU::REG_SEQUENCE: {
872 // MVT::i128 only use SGPRs, so i128 REG_SEQUENCEs don't need to be
873 // rewritten.
874 if (N->getValueType(0) == MVT::i128) {
875 return N;
876 }
877 const SDValue Ops[] = {
878 DAG.getTargetConstant(AMDGPU::VReg_64RegClassID, MVT::i32),
879 N->getOperand(1) , N->getOperand(2),
880 N->getOperand(3), N->getOperand(4)
881 };
882 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::i64, Ops);
883 }
884
885 case AMDGPU::S_LOAD_DWORD_IMM:
886 NewOpcode = AMDGPU::BUFFER_LOAD_DWORD_ADDR64;
887 // Fall-through
888 case AMDGPU::S_LOAD_DWORDX2_SGPR:
889 if (NewOpcode == N->getMachineOpcode()) {
890 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64;
891 }
892 // Fall-through
893 case AMDGPU::S_LOAD_DWORDX4_IMM:
894 case AMDGPU::S_LOAD_DWORDX4_SGPR: {
895 if (NewOpcode == N->getMachineOpcode()) {
896 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64;
897 }
898 if (fitsRegClass(DAG, N->getOperand(0), AMDGPU::SReg_64RegClassID)) {
899 return N;
900 }
901 ConstantSDNode *Offset = cast<ConstantSDNode>(N->getOperand(1));
902 SDValue Ops[] = {
903 SDValue(DAG.getMachineNode(AMDGPU::SI_ADDR64_RSRC, DL, MVT::i128,
904 DAG.getConstant(0, MVT::i64)), 0),
905 N->getOperand(0),
906 DAG.getConstant(Offset->getSExtValue() << 2, MVT::i32)
907 };
908 return DAG.getMachineNode(NewOpcode, DL, N->getVTList(), Ops);
909 }
910 }
911}