blob: f75f5d42754d83ad100b0e282f38dc7822a078e9 [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"
Christian Konigcc226402013-03-07 09:04:14 +000017#include "AMDGPU.h"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000018#include "AMDILIntrinsicInfo.h"
19#include "SIInstrInfo.h"
20#include "SIMachineFunctionInfo.h"
21#include "SIRegisterInfo.h"
Christian Konig90c64cb2013-03-07 09:03:52 +000022#include "llvm/IR/Function.h"
23#include "llvm/CodeGen/CallingConvLower.h"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
26#include "llvm/CodeGen/SelectionDAG.h"
27
28using namespace llvm;
29
30SITargetLowering::SITargetLowering(TargetMachine &TM) :
31 AMDGPUTargetLowering(TM),
Christian Konigd3b55092013-02-26 17:52:23 +000032 TII(static_cast<const SIInstrInfo*>(TM.getInstrInfo())),
33 TRI(TM.getRegisterInfo()) {
Christian Konig204a2d32013-03-07 09:03:38 +000034
Christian Konige9ba1832013-02-16 11:28:30 +000035 addRegisterClass(MVT::i1, &AMDGPU::SReg_64RegClass);
Christian Konig204a2d32013-03-07 09:03:38 +000036 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
37
38 addRegisterClass(MVT::v16i8, &AMDGPU::SReg_128RegClass);
39 addRegisterClass(MVT::v32i8, &AMDGPU::SReg_256RegClass);
40 addRegisterClass(MVT::v64i8, &AMDGPU::SReg_512RegClass);
41
42 addRegisterClass(MVT::i32, &AMDGPU::VReg_32RegClass);
43 addRegisterClass(MVT::f32, &AMDGPU::VReg_32RegClass);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000044
Tom Stellard36ba9092013-02-07 17:02:09 +000045 addRegisterClass(MVT::v1i32, &AMDGPU::VReg_32RegClass);
Christian Konig204a2d32013-03-07 09:03:38 +000046
Tom Stellard36ba9092013-02-07 17:02:09 +000047 addRegisterClass(MVT::v2i32, &AMDGPU::VReg_64RegClass);
Christian Konig204a2d32013-03-07 09:03:38 +000048 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
49
Tom Stellard36ba9092013-02-07 17:02:09 +000050 addRegisterClass(MVT::v4i32, &AMDGPU::VReg_128RegClass);
Christian Konig204a2d32013-03-07 09:03:38 +000051 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
52
Tom Stellard36ba9092013-02-07 17:02:09 +000053 addRegisterClass(MVT::v8i32, &AMDGPU::VReg_256RegClass);
Christian Konig204a2d32013-03-07 09:03:38 +000054 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
55
Tom Stellard36ba9092013-02-07 17:02:09 +000056 addRegisterClass(MVT::v16i32, &AMDGPU::VReg_512RegClass);
Christian Konig204a2d32013-03-07 09:03:38 +000057 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000058
59 computeRegisterProperties();
60
Christian Konigb9e86782013-03-18 11:34:16 +000061 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
62 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
63 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
64 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
65
Tom Stellardf98f2ce2012-12-11 21:25:42 +000066 setOperationAction(ISD::ADD, MVT::i64, Legal);
67 setOperationAction(ISD::ADD, MVT::i32, Legal);
68
Tom Stellardf98f2ce2012-12-11 21:25:42 +000069 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
70 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
71
72 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
73 setTargetDAGCombine(ISD::SELECT_CC);
74
75 setTargetDAGCombine(ISD::SETCC);
Michel Danzerd787c042013-03-08 10:58:01 +000076
Christian Konigc53270f2013-03-26 14:04:02 +000077 setSchedulingPreference(Sched::RegPressure);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000078}
79
Christian Konig90c64cb2013-03-07 09:03:52 +000080SDValue SITargetLowering::LowerFormalArguments(
81 SDValue Chain,
82 CallingConv::ID CallConv,
83 bool isVarArg,
84 const SmallVectorImpl<ISD::InputArg> &Ins,
85 DebugLoc DL, SelectionDAG &DAG,
86 SmallVectorImpl<SDValue> &InVals) const {
87
88 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
89
90 MachineFunction &MF = DAG.getMachineFunction();
91 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konigcc226402013-03-07 09:04:14 +000092 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Christian Konig90c64cb2013-03-07 09:03:52 +000093
94 assert(CallConv == CallingConv::C);
95
96 SmallVector<ISD::InputArg, 16> Splits;
Christian Konigcc226402013-03-07 09:04:14 +000097 uint32_t Skipped = 0;
98
99 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig90c64cb2013-03-07 09:03:52 +0000100 const ISD::InputArg &Arg = Ins[i];
101
Christian Konigcc226402013-03-07 09:04:14 +0000102 // First check if it's a PS input addr
103 if (Info->ShaderType == ShaderType::PIXEL && !Arg.Flags.isInReg()) {
104
105 assert((PSInputNum <= 15) && "Too many PS inputs!");
106
107 if (!Arg.Used) {
108 // We can savely skip PS inputs
109 Skipped |= 1 << i;
110 ++PSInputNum;
111 continue;
112 }
113
114 Info->PSInputAddr |= 1 << PSInputNum++;
115 }
116
117 // Second split vertices into their elements
Christian Konig90c64cb2013-03-07 09:03:52 +0000118 if (Arg.VT.isVector()) {
119 ISD::InputArg NewArg = Arg;
120 NewArg.Flags.setSplit();
121 NewArg.VT = Arg.VT.getVectorElementType();
122
123 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
124 // three or five element vertex only needs three or five registers,
125 // NOT four or eigth.
126 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
127 unsigned NumElements = ParamType->getVectorNumElements();
128
129 for (unsigned j = 0; j != NumElements; ++j) {
130 Splits.push_back(NewArg);
131 NewArg.PartOffset += NewArg.VT.getStoreSize();
132 }
133
134 } else {
135 Splits.push_back(Arg);
136 }
137 }
138
139 SmallVector<CCValAssign, 16> ArgLocs;
140 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
141 getTargetMachine(), ArgLocs, *DAG.getContext());
142
Christian Konigcc226402013-03-07 09:04:14 +0000143 // At least one interpolation mode must be enabled or else the GPU will hang.
144 if (Info->ShaderType == ShaderType::PIXEL && (Info->PSInputAddr & 0x7F) == 0) {
145 Info->PSInputAddr |= 1;
146 CCInfo.AllocateReg(AMDGPU::VGPR0);
147 CCInfo.AllocateReg(AMDGPU::VGPR1);
148 }
149
Christian Konig90c64cb2013-03-07 09:03:52 +0000150 AnalyzeFormalArguments(CCInfo, Splits);
151
152 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
153
Christian Konigcc226402013-03-07 09:04:14 +0000154 if (Skipped & (1 << i)) {
155 InVals.push_back(SDValue());
156 continue;
157 }
158
Christian Konig90c64cb2013-03-07 09:03:52 +0000159 CCValAssign &VA = ArgLocs[ArgIdx++];
160 assert(VA.isRegLoc() && "Parameter must be in a register!");
161
162 unsigned Reg = VA.getLocReg();
163 MVT VT = VA.getLocVT();
164
165 if (VT == MVT::i64) {
166 // For now assume it is a pointer
167 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
168 &AMDGPU::SReg_64RegClass);
169 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
170 InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
171 continue;
172 }
173
174 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
175
176 Reg = MF.addLiveIn(Reg, RC);
177 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
178
179 const ISD::InputArg &Arg = Ins[i];
180 if (Arg.VT.isVector()) {
181
182 // Build a vector from the registers
183 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
184 unsigned NumElements = ParamType->getVectorNumElements();
185
186 SmallVector<SDValue, 4> Regs;
187 Regs.push_back(Val);
188 for (unsigned j = 1; j != NumElements; ++j) {
189 Reg = ArgLocs[ArgIdx++].getLocReg();
190 Reg = MF.addLiveIn(Reg, RC);
191 Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
192 }
193
194 // Fill up the missing vector elements
195 NumElements = Arg.VT.getVectorNumElements() - NumElements;
196 for (unsigned j = 0; j != NumElements; ++j)
197 Regs.push_back(DAG.getUNDEF(VT));
198
199 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT,
200 Regs.data(), Regs.size()));
201 continue;
202 }
203
204 InVals.push_back(Val);
205 }
206 return Chain;
207}
208
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000209MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
210 MachineInstr * MI, MachineBasicBlock * BB) const {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000211
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000212 switch (MI->getOpcode()) {
213 default:
214 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
215 case AMDGPU::BRANCH: return BB;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000216 }
217 return BB;
218}
219
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000220EVT SITargetLowering::getSetCCResultType(EVT VT) const {
221 return MVT::i1;
222}
223
Christian Konigb8708222013-03-18 11:34:05 +0000224MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const {
225 return MVT::i32;
226}
227
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000228//===----------------------------------------------------------------------===//
229// Custom DAG Lowering Operations
230//===----------------------------------------------------------------------===//
231
232SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
233 switch (Op.getOpcode()) {
234 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000235 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000236 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000237 }
238 return SDValue();
239}
240
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000241/// \brief Helper function for LowerBRCOND
242static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000243
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000244 SDNode *Parent = Value.getNode();
245 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
246 I != E; ++I) {
247
248 if (I.getUse().get() != Value)
249 continue;
250
251 if (I->getOpcode() == Opcode)
252 return *I;
253 }
254 return 0;
255}
256
257/// This transforms the control flow intrinsics to get the branch destination as
258/// last parameter, also switches branch target with BR if the need arise
259SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
260 SelectionDAG &DAG) const {
261
262 DebugLoc DL = BRCOND.getDebugLoc();
263
264 SDNode *Intr = BRCOND.getOperand(1).getNode();
265 SDValue Target = BRCOND.getOperand(2);
266 SDNode *BR = 0;
267
268 if (Intr->getOpcode() == ISD::SETCC) {
269 // As long as we negate the condition everything is fine
270 SDNode *SetCC = Intr;
271 assert(SetCC->getConstantOperandVal(1) == 1);
NAKAMURA Takumie13a2a32013-01-07 11:14:44 +0000272 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
273 ISD::SETNE);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000274 Intr = SetCC->getOperand(0).getNode();
275
276 } else {
277 // Get the target from BR if we don't negate the condition
278 BR = findUser(BRCOND, ISD::BR);
279 Target = BR->getOperand(1);
280 }
281
282 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
283
284 // Build the result and
285 SmallVector<EVT, 4> Res;
286 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
287 Res.push_back(Intr->getValueType(i));
288
289 // operands of the new intrinsic call
290 SmallVector<SDValue, 4> Ops;
291 Ops.push_back(BRCOND.getOperand(0));
292 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
293 Ops.push_back(Intr->getOperand(i));
294 Ops.push_back(Target);
295
296 // build the new intrinsic call
297 SDNode *Result = DAG.getNode(
298 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
299 DAG.getVTList(Res.data(), Res.size()), Ops.data(), Ops.size()).getNode();
300
301 if (BR) {
302 // Give the branch instruction our target
303 SDValue Ops[] = {
304 BR->getOperand(0),
305 BRCOND.getOperand(2)
306 };
307 DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2);
308 }
309
310 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
311
312 // Copy the intrinsic results to registers
313 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
314 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
315 if (!CopyToReg)
316 continue;
317
318 Chain = DAG.getCopyToReg(
319 Chain, DL,
320 CopyToReg->getOperand(1),
321 SDValue(Result, i - 1),
322 SDValue());
323
324 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
325 }
326
327 // Remove the old intrinsic from the chain
328 DAG.ReplaceAllUsesOfValueWith(
329 SDValue(Intr, Intr->getNumValues() - 1),
330 Intr->getOperand(0));
331
332 return Chain;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000333}
334
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000335SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
336 SDValue LHS = Op.getOperand(0);
337 SDValue RHS = Op.getOperand(1);
338 SDValue True = Op.getOperand(2);
339 SDValue False = Op.getOperand(3);
340 SDValue CC = Op.getOperand(4);
341 EVT VT = Op.getValueType();
342 DebugLoc DL = Op.getDebugLoc();
343
344 // Possible Min/Max pattern
345 SDValue MinMax = LowerMinMax(Op, DAG);
346 if (MinMax.getNode()) {
347 return MinMax;
348 }
349
350 SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC);
351 return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False);
352}
353
354//===----------------------------------------------------------------------===//
355// Custom DAG optimizations
356//===----------------------------------------------------------------------===//
357
358SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
359 DAGCombinerInfo &DCI) const {
360 SelectionDAG &DAG = DCI.DAG;
361 DebugLoc DL = N->getDebugLoc();
362 EVT VT = N->getValueType(0);
363
364 switch (N->getOpcode()) {
365 default: break;
366 case ISD::SELECT_CC: {
367 N->dump();
368 ConstantSDNode *True, *False;
369 // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
370 if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
371 && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
372 && True->isAllOnesValue()
373 && False->isNullValue()
374 && VT == MVT::i1) {
375 return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
376 N->getOperand(1), N->getOperand(4));
377
378 }
379 break;
380 }
381 case ISD::SETCC: {
382 SDValue Arg0 = N->getOperand(0);
383 SDValue Arg1 = N->getOperand(1);
384 SDValue CC = N->getOperand(2);
385 ConstantSDNode * C = NULL;
386 ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
387
388 // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
389 if (VT == MVT::i1
390 && Arg0.getOpcode() == ISD::SIGN_EXTEND
391 && Arg0.getOperand(0).getValueType() == MVT::i1
392 && (C = dyn_cast<ConstantSDNode>(Arg1))
393 && C->isNullValue()
394 && CCOp == ISD::SETNE) {
395 return SimplifySetCC(VT, Arg0.getOperand(0),
396 DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
397 }
398 break;
399 }
400 }
401 return SDValue();
402}
Christian Konigc018eca2013-02-26 17:52:16 +0000403
Christian Konigd3b55092013-02-26 17:52:23 +0000404/// \brief Test if RegClass is one of the VSrc classes
405static bool isVSrc(unsigned RegClass) {
406 return AMDGPU::VSrc_32RegClassID == RegClass ||
407 AMDGPU::VSrc_64RegClassID == RegClass;
408}
409
410/// \brief Test if RegClass is one of the SSrc classes
411static bool isSSrc(unsigned RegClass) {
412 return AMDGPU::SSrc_32RegClassID == RegClass ||
413 AMDGPU::SSrc_64RegClassID == RegClass;
414}
415
416/// \brief Analyze the possible immediate value Op
417///
418/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
419/// and the immediate value if it's a literal immediate
420int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
421
422 union {
423 int32_t I;
424 float F;
425 } Imm;
426
427 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N))
428 Imm.I = Node->getSExtValue();
429 else if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N))
430 Imm.F = Node->getValueAPF().convertToFloat();
431 else
432 return -1; // It isn't an immediate
433
434 if ((Imm.I >= -16 && Imm.I <= 64) ||
435 Imm.F == 0.5f || Imm.F == -0.5f ||
436 Imm.F == 1.0f || Imm.F == -1.0f ||
437 Imm.F == 2.0f || Imm.F == -2.0f ||
438 Imm.F == 4.0f || Imm.F == -4.0f)
439 return 0; // It's an inline immediate
440
441 return Imm.I; // It's a literal immediate
442}
443
444/// \brief Try to fold an immediate directly into an instruction
445bool SITargetLowering::foldImm(SDValue &Operand, int32_t &Immediate,
446 bool &ScalarSlotUsed) const {
447
448 MachineSDNode *Mov = dyn_cast<MachineSDNode>(Operand);
449 if (Mov == 0 || !TII->isMov(Mov->getMachineOpcode()))
450 return false;
451
452 const SDValue &Op = Mov->getOperand(0);
453 int32_t Value = analyzeImmediate(Op.getNode());
454 if (Value == -1) {
455 // Not an immediate at all
456 return false;
457
458 } else if (Value == 0) {
459 // Inline immediates can always be fold
460 Operand = Op;
461 return true;
462
463 } else if (Value == Immediate) {
464 // Already fold literal immediate
465 Operand = Op;
466 return true;
467
468 } else if (!ScalarSlotUsed && !Immediate) {
469 // Fold this literal immediate
470 ScalarSlotUsed = true;
471 Immediate = Value;
472 Operand = Op;
473 return true;
474
475 }
476
477 return false;
478}
479
480/// \brief Does "Op" fit into register class "RegClass" ?
481bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, SDValue &Op,
482 unsigned RegClass) const {
483
484 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
485 SDNode *Node = Op.getNode();
486
487 int OpClass;
488 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(Node)) {
489 const MCInstrDesc &Desc = TII->get(MN->getMachineOpcode());
490 OpClass = Desc.OpInfo[Op.getResNo()].RegClass;
491
492 } else if (Node->getOpcode() == ISD::CopyFromReg) {
493 RegisterSDNode *Reg = cast<RegisterSDNode>(Node->getOperand(1).getNode());
494 OpClass = MRI.getRegClass(Reg->getReg())->getID();
495
496 } else
497 return false;
498
499 if (OpClass == -1)
500 return false;
501
502 return TRI->getRegClass(RegClass)->hasSubClassEq(TRI->getRegClass(OpClass));
503}
504
505/// \brief Make sure that we don't exeed the number of allowed scalars
506void SITargetLowering::ensureSRegLimit(SelectionDAG &DAG, SDValue &Operand,
507 unsigned RegClass,
508 bool &ScalarSlotUsed) const {
509
510 // First map the operands register class to a destination class
511 if (RegClass == AMDGPU::VSrc_32RegClassID)
512 RegClass = AMDGPU::VReg_32RegClassID;
513 else if (RegClass == AMDGPU::VSrc_64RegClassID)
514 RegClass = AMDGPU::VReg_64RegClassID;
515 else
516 return;
517
518 // Nothing todo if they fit naturaly
519 if (fitsRegClass(DAG, Operand, RegClass))
520 return;
521
522 // If the scalar slot isn't used yet use it now
523 if (!ScalarSlotUsed) {
524 ScalarSlotUsed = true;
525 return;
526 }
527
528 // This is a conservative aproach, it is possible that we can't determine
529 // the correct register class and copy too often, but better save than sorry.
530 SDValue RC = DAG.getTargetConstant(RegClass, MVT::i32);
531 SDNode *Node = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, DebugLoc(),
532 Operand.getValueType(), Operand, RC);
533 Operand = SDValue(Node, 0);
534}
535
Christian Konigc018eca2013-02-26 17:52:16 +0000536SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
537 SelectionDAG &DAG) const {
Christian Konigd3b55092013-02-26 17:52:23 +0000538
539 // Original encoding (either e32 or e64)
540 int Opcode = Node->getMachineOpcode();
541 const MCInstrDesc *Desc = &TII->get(Opcode);
542
543 unsigned NumDefs = Desc->getNumDefs();
544 unsigned NumOps = Desc->getNumOperands();
545
Christian Konig3c980d12013-02-26 17:52:47 +0000546 // e64 version if available, -1 otherwise
547 int OpcodeE64 = AMDGPU::getVOPe64(Opcode);
548 const MCInstrDesc *DescE64 = OpcodeE64 == -1 ? 0 : &TII->get(OpcodeE64);
549
550 assert(!DescE64 || DescE64->getNumDefs() == NumDefs);
551 assert(!DescE64 || DescE64->getNumOperands() == (NumOps + 4));
552
Christian Konigd3b55092013-02-26 17:52:23 +0000553 int32_t Immediate = Desc->getSize() == 4 ? 0 : -1;
554 bool HaveVSrc = false, HaveSSrc = false;
555
556 // First figure out what we alread have in this instruction
557 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
558 i != e && Op < NumOps; ++i, ++Op) {
559
560 unsigned RegClass = Desc->OpInfo[Op].RegClass;
561 if (isVSrc(RegClass))
562 HaveVSrc = true;
563 else if (isSSrc(RegClass))
564 HaveSSrc = true;
565 else
566 continue;
567
568 int32_t Imm = analyzeImmediate(Node->getOperand(i).getNode());
569 if (Imm != -1 && Imm != 0) {
570 // Literal immediate
571 Immediate = Imm;
572 }
573 }
574
575 // If we neither have VSrc nor SSrc it makes no sense to continue
576 if (!HaveVSrc && !HaveSSrc)
577 return Node;
578
579 // No scalar allowed when we have both VSrc and SSrc
580 bool ScalarSlotUsed = HaveVSrc && HaveSSrc;
581
582 // Second go over the operands and try to fold them
583 std::vector<SDValue> Ops;
Christian Konig3c980d12013-02-26 17:52:47 +0000584 bool Promote2e64 = false;
Christian Konigd3b55092013-02-26 17:52:23 +0000585 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
586 i != e && Op < NumOps; ++i, ++Op) {
587
588 const SDValue &Operand = Node->getOperand(i);
589 Ops.push_back(Operand);
590
591 // Already folded immediate ?
592 if (isa<ConstantSDNode>(Operand.getNode()) ||
593 isa<ConstantFPSDNode>(Operand.getNode()))
594 continue;
595
596 // Is this a VSrc or SSrc operand ?
597 unsigned RegClass = Desc->OpInfo[Op].RegClass;
Christian Konigb37afdc2013-02-26 17:52:36 +0000598 if (!isVSrc(RegClass) && !isSSrc(RegClass)) {
599
600 if (i == 1 && Desc->isCommutable() &&
601 fitsRegClass(DAG, Ops[0], RegClass) &&
602 foldImm(Ops[1], Immediate, ScalarSlotUsed)) {
603
604 assert(isVSrc(Desc->OpInfo[NumDefs].RegClass) ||
605 isSSrc(Desc->OpInfo[NumDefs].RegClass));
606
607 // Swap commutable operands
608 SDValue Tmp = Ops[1];
609 Ops[1] = Ops[0];
610 Ops[0] = Tmp;
Christian Konig3c980d12013-02-26 17:52:47 +0000611
612 } else if (DescE64 && !Immediate) {
613 // Test if it makes sense to switch to e64 encoding
614
615 RegClass = DescE64->OpInfo[Op].RegClass;
616 int32_t TmpImm = -1;
617 if ((isVSrc(RegClass) || isSSrc(RegClass)) &&
618 foldImm(Ops[i], TmpImm, ScalarSlotUsed)) {
619
620 Immediate = -1;
621 Promote2e64 = true;
622 Desc = DescE64;
623 DescE64 = 0;
624 }
Christian Konigb37afdc2013-02-26 17:52:36 +0000625 }
Christian Konigd3b55092013-02-26 17:52:23 +0000626 continue;
Christian Konigb37afdc2013-02-26 17:52:36 +0000627 }
Christian Konigd3b55092013-02-26 17:52:23 +0000628
629 // Try to fold the immediates
630 if (!foldImm(Ops[i], Immediate, ScalarSlotUsed)) {
631 // Folding didn't worked, make sure we don't hit the SReg limit
632 ensureSRegLimit(DAG, Ops[i], RegClass, ScalarSlotUsed);
633 }
634 }
635
Christian Konig3c980d12013-02-26 17:52:47 +0000636 if (Promote2e64) {
637 // Add the modifier flags while promoting
638 for (unsigned i = 0; i < 4; ++i)
639 Ops.push_back(DAG.getTargetConstant(0, MVT::i32));
640 }
641
Christian Konigd3b55092013-02-26 17:52:23 +0000642 // Add optional chain and glue
643 for (unsigned i = NumOps - NumDefs, e = Node->getNumOperands(); i < e; ++i)
644 Ops.push_back(Node->getOperand(i));
645
Christian Konig3c980d12013-02-26 17:52:47 +0000646 // Either create a complete new or update the current instruction
647 if (Promote2e64)
648 return DAG.getMachineNode(OpcodeE64, Node->getDebugLoc(),
649 Node->getVTList(), Ops.data(), Ops.size());
650 else
651 return DAG.UpdateNodeOperands(Node, Ops.data(), Ops.size());
Christian Konigc018eca2013-02-26 17:52:16 +0000652}