blob: 0a0fbd98928e0b34018aaf03f762fef7f5946291 [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),
Christian Konigd3b55092013-02-26 17:52:23 +000029 TII(static_cast<const SIInstrInfo*>(TM.getInstrInfo())),
30 TRI(TM.getRegisterInfo()) {
Tom Stellardf98f2ce2012-12-11 21:25:42 +000031 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
32 addRegisterClass(MVT::f32, &AMDGPU::VReg_32RegClass);
33 addRegisterClass(MVT::i32, &AMDGPU::VReg_32RegClass);
34 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
Christian Konige9ba1832013-02-16 11:28:30 +000035 addRegisterClass(MVT::i1, &AMDGPU::SReg_64RegClass);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000036
Tom Stellard36ba9092013-02-07 17:02:09 +000037 addRegisterClass(MVT::v1i32, &AMDGPU::VReg_32RegClass);
38 addRegisterClass(MVT::v2i32, &AMDGPU::VReg_64RegClass);
39 addRegisterClass(MVT::v4i32, &AMDGPU::VReg_128RegClass);
40 addRegisterClass(MVT::v8i32, &AMDGPU::VReg_256RegClass);
41 addRegisterClass(MVT::v16i32, &AMDGPU::VReg_512RegClass);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000042
43 computeRegisterProperties();
44
Tom Stellardf98f2ce2012-12-11 21:25:42 +000045 setOperationAction(ISD::ADD, MVT::i64, Legal);
46 setOperationAction(ISD::ADD, MVT::i32, Legal);
47
Tom Stellardf98f2ce2012-12-11 21:25:42 +000048 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
49
50 // We need to custom lower loads from the USER_SGPR address space, so we can
51 // add the SGPRs as livein registers.
52 setOperationAction(ISD::LOAD, MVT::i32, Custom);
53 setOperationAction(ISD::LOAD, MVT::i64, Custom);
54
55 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
56 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
57
58 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
59 setTargetDAGCombine(ISD::SELECT_CC);
60
61 setTargetDAGCombine(ISD::SETCC);
62}
63
64MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
65 MachineInstr * MI, MachineBasicBlock * BB) const {
Tom Stellardf98f2ce2012-12-11 21:25:42 +000066 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;
Tom Stellardf98f2ce2012-12-11 21:25:42 +000073 case AMDGPU::SHADER_TYPE:
74 BB->getParent()->getInfo<SIMachineFunctionInfo>()->ShaderType =
75 MI->getOperand(0).getImm();
76 MI->eraseFromParent();
77 break;
78
79 case AMDGPU::SI_INTERP:
80 LowerSI_INTERP(MI, *BB, I, MRI);
81 break;
Tom Stellardf98f2ce2012-12-11 21:25:42 +000082 case AMDGPU::SI_WQM:
83 LowerSI_WQM(MI, *BB, I, MRI);
84 break;
Tom Stellardf98f2ce2012-12-11 21:25:42 +000085 }
86 return BB;
87}
88
Tom Stellardf98f2ce2012-12-11 21:25:42 +000089void SITargetLowering::LowerSI_WQM(MachineInstr *MI, MachineBasicBlock &BB,
90 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
91 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_WQM_B64), AMDGPU::EXEC)
92 .addReg(AMDGPU::EXEC);
93
94 MI->eraseFromParent();
95}
96
97void SITargetLowering::LowerSI_INTERP(MachineInstr *MI, MachineBasicBlock &BB,
98 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
99 unsigned tmp = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
100 unsigned M0 = MRI.createVirtualRegister(&AMDGPU::M0RegRegClass);
101 MachineOperand dst = MI->getOperand(0);
102 MachineOperand iReg = MI->getOperand(1);
103 MachineOperand jReg = MI->getOperand(2);
104 MachineOperand attr_chan = MI->getOperand(3);
105 MachineOperand attr = MI->getOperand(4);
106 MachineOperand params = MI->getOperand(5);
107
108 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_MOV_B32), M0)
109 .addOperand(params);
110
111 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P1_F32), tmp)
112 .addOperand(iReg)
113 .addOperand(attr_chan)
114 .addOperand(attr)
115 .addReg(M0);
116
117 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P2_F32))
118 .addOperand(dst)
119 .addReg(tmp)
120 .addOperand(jReg)
121 .addOperand(attr_chan)
122 .addOperand(attr)
123 .addReg(M0);
124
125 MI->eraseFromParent();
126}
127
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000128EVT SITargetLowering::getSetCCResultType(EVT VT) const {
129 return MVT::i1;
130}
131
132//===----------------------------------------------------------------------===//
133// Custom DAG Lowering Operations
134//===----------------------------------------------------------------------===//
135
136SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
137 switch (Op.getOpcode()) {
138 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000139 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000140 case ISD::LOAD: return LowerLOAD(Op, DAG);
141 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000142 case ISD::INTRINSIC_WO_CHAIN: {
143 unsigned IntrinsicID =
144 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
145 EVT VT = Op.getValueType();
146 switch (IntrinsicID) {
147 case AMDGPUIntrinsic::SI_vs_load_buffer_index:
148 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
149 AMDGPU::VGPR0, VT);
150 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
151 }
152 break;
153 }
154 }
155 return SDValue();
156}
157
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000158/// \brief Helper function for LowerBRCOND
159static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000160
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000161 SDNode *Parent = Value.getNode();
162 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
163 I != E; ++I) {
164
165 if (I.getUse().get() != Value)
166 continue;
167
168 if (I->getOpcode() == Opcode)
169 return *I;
170 }
171 return 0;
172}
173
174/// This transforms the control flow intrinsics to get the branch destination as
175/// last parameter, also switches branch target with BR if the need arise
176SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
177 SelectionDAG &DAG) const {
178
179 DebugLoc DL = BRCOND.getDebugLoc();
180
181 SDNode *Intr = BRCOND.getOperand(1).getNode();
182 SDValue Target = BRCOND.getOperand(2);
183 SDNode *BR = 0;
184
185 if (Intr->getOpcode() == ISD::SETCC) {
186 // As long as we negate the condition everything is fine
187 SDNode *SetCC = Intr;
188 assert(SetCC->getConstantOperandVal(1) == 1);
NAKAMURA Takumie13a2a32013-01-07 11:14:44 +0000189 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
190 ISD::SETNE);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000191 Intr = SetCC->getOperand(0).getNode();
192
193 } else {
194 // Get the target from BR if we don't negate the condition
195 BR = findUser(BRCOND, ISD::BR);
196 Target = BR->getOperand(1);
197 }
198
199 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
200
201 // Build the result and
202 SmallVector<EVT, 4> Res;
203 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
204 Res.push_back(Intr->getValueType(i));
205
206 // operands of the new intrinsic call
207 SmallVector<SDValue, 4> Ops;
208 Ops.push_back(BRCOND.getOperand(0));
209 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
210 Ops.push_back(Intr->getOperand(i));
211 Ops.push_back(Target);
212
213 // build the new intrinsic call
214 SDNode *Result = DAG.getNode(
215 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
216 DAG.getVTList(Res.data(), Res.size()), Ops.data(), Ops.size()).getNode();
217
218 if (BR) {
219 // Give the branch instruction our target
220 SDValue Ops[] = {
221 BR->getOperand(0),
222 BRCOND.getOperand(2)
223 };
224 DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2);
225 }
226
227 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
228
229 // Copy the intrinsic results to registers
230 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
231 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
232 if (!CopyToReg)
233 continue;
234
235 Chain = DAG.getCopyToReg(
236 Chain, DL,
237 CopyToReg->getOperand(1),
238 SDValue(Result, i - 1),
239 SDValue());
240
241 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
242 }
243
244 // Remove the old intrinsic from the chain
245 DAG.ReplaceAllUsesOfValueWith(
246 SDValue(Intr, Intr->getNumValues() - 1),
247 Intr->getOperand(0));
248
249 return Chain;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000250}
251
252SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
253 EVT VT = Op.getValueType();
254 LoadSDNode *Ptr = dyn_cast<LoadSDNode>(Op);
255
256 assert(Ptr);
257
258 unsigned AddrSpace = Ptr->getPointerInfo().getAddrSpace();
259
260 // We only need to lower USER_SGPR address space loads
261 if (AddrSpace != AMDGPUAS::USER_SGPR_ADDRESS) {
262 return SDValue();
263 }
264
265 // Loads from the USER_SGPR address space can only have constant value
266 // pointers.
267 ConstantSDNode *BasePtr = dyn_cast<ConstantSDNode>(Ptr->getBasePtr());
268 assert(BasePtr);
269
270 unsigned TypeDwordWidth = VT.getSizeInBits() / 32;
271 const TargetRegisterClass * dstClass;
272 switch (TypeDwordWidth) {
273 default:
274 assert(!"USER_SGPR value size not implemented");
275 return SDValue();
276 case 1:
277 dstClass = &AMDGPU::SReg_32RegClass;
278 break;
279 case 2:
280 dstClass = &AMDGPU::SReg_64RegClass;
281 break;
282 }
283 uint64_t Index = BasePtr->getZExtValue();
284 assert(Index % TypeDwordWidth == 0 && "USER_SGPR not properly aligned");
285 unsigned SGPRIndex = Index / TypeDwordWidth;
286 unsigned Reg = dstClass->getRegister(SGPRIndex);
287
288 DAG.ReplaceAllUsesOfValueWith(Op, CreateLiveInRegister(DAG, dstClass, Reg,
289 VT));
290 return SDValue();
291}
292
293SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
294 SDValue LHS = Op.getOperand(0);
295 SDValue RHS = Op.getOperand(1);
296 SDValue True = Op.getOperand(2);
297 SDValue False = Op.getOperand(3);
298 SDValue CC = Op.getOperand(4);
299 EVT VT = Op.getValueType();
300 DebugLoc DL = Op.getDebugLoc();
301
302 // Possible Min/Max pattern
303 SDValue MinMax = LowerMinMax(Op, DAG);
304 if (MinMax.getNode()) {
305 return MinMax;
306 }
307
308 SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC);
309 return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False);
310}
311
312//===----------------------------------------------------------------------===//
313// Custom DAG optimizations
314//===----------------------------------------------------------------------===//
315
316SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
317 DAGCombinerInfo &DCI) const {
318 SelectionDAG &DAG = DCI.DAG;
319 DebugLoc DL = N->getDebugLoc();
320 EVT VT = N->getValueType(0);
321
322 switch (N->getOpcode()) {
323 default: break;
324 case ISD::SELECT_CC: {
325 N->dump();
326 ConstantSDNode *True, *False;
327 // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
328 if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
329 && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
330 && True->isAllOnesValue()
331 && False->isNullValue()
332 && VT == MVT::i1) {
333 return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
334 N->getOperand(1), N->getOperand(4));
335
336 }
337 break;
338 }
339 case ISD::SETCC: {
340 SDValue Arg0 = N->getOperand(0);
341 SDValue Arg1 = N->getOperand(1);
342 SDValue CC = N->getOperand(2);
343 ConstantSDNode * C = NULL;
344 ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
345
346 // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
347 if (VT == MVT::i1
348 && Arg0.getOpcode() == ISD::SIGN_EXTEND
349 && Arg0.getOperand(0).getValueType() == MVT::i1
350 && (C = dyn_cast<ConstantSDNode>(Arg1))
351 && C->isNullValue()
352 && CCOp == ISD::SETNE) {
353 return SimplifySetCC(VT, Arg0.getOperand(0),
354 DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
355 }
356 break;
357 }
358 }
359 return SDValue();
360}
Christian Konigc018eca2013-02-26 17:52:16 +0000361
Christian Konigd3b55092013-02-26 17:52:23 +0000362/// \brief Test if RegClass is one of the VSrc classes
363static bool isVSrc(unsigned RegClass) {
364 return AMDGPU::VSrc_32RegClassID == RegClass ||
365 AMDGPU::VSrc_64RegClassID == RegClass;
366}
367
368/// \brief Test if RegClass is one of the SSrc classes
369static bool isSSrc(unsigned RegClass) {
370 return AMDGPU::SSrc_32RegClassID == RegClass ||
371 AMDGPU::SSrc_64RegClassID == RegClass;
372}
373
374/// \brief Analyze the possible immediate value Op
375///
376/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
377/// and the immediate value if it's a literal immediate
378int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
379
380 union {
381 int32_t I;
382 float F;
383 } Imm;
384
385 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N))
386 Imm.I = Node->getSExtValue();
387 else if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N))
388 Imm.F = Node->getValueAPF().convertToFloat();
389 else
390 return -1; // It isn't an immediate
391
392 if ((Imm.I >= -16 && Imm.I <= 64) ||
393 Imm.F == 0.5f || Imm.F == -0.5f ||
394 Imm.F == 1.0f || Imm.F == -1.0f ||
395 Imm.F == 2.0f || Imm.F == -2.0f ||
396 Imm.F == 4.0f || Imm.F == -4.0f)
397 return 0; // It's an inline immediate
398
399 return Imm.I; // It's a literal immediate
400}
401
402/// \brief Try to fold an immediate directly into an instruction
403bool SITargetLowering::foldImm(SDValue &Operand, int32_t &Immediate,
404 bool &ScalarSlotUsed) const {
405
406 MachineSDNode *Mov = dyn_cast<MachineSDNode>(Operand);
407 if (Mov == 0 || !TII->isMov(Mov->getMachineOpcode()))
408 return false;
409
410 const SDValue &Op = Mov->getOperand(0);
411 int32_t Value = analyzeImmediate(Op.getNode());
412 if (Value == -1) {
413 // Not an immediate at all
414 return false;
415
416 } else if (Value == 0) {
417 // Inline immediates can always be fold
418 Operand = Op;
419 return true;
420
421 } else if (Value == Immediate) {
422 // Already fold literal immediate
423 Operand = Op;
424 return true;
425
426 } else if (!ScalarSlotUsed && !Immediate) {
427 // Fold this literal immediate
428 ScalarSlotUsed = true;
429 Immediate = Value;
430 Operand = Op;
431 return true;
432
433 }
434
435 return false;
436}
437
438/// \brief Does "Op" fit into register class "RegClass" ?
439bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, SDValue &Op,
440 unsigned RegClass) const {
441
442 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
443 SDNode *Node = Op.getNode();
444
445 int OpClass;
446 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(Node)) {
447 const MCInstrDesc &Desc = TII->get(MN->getMachineOpcode());
448 OpClass = Desc.OpInfo[Op.getResNo()].RegClass;
449
450 } else if (Node->getOpcode() == ISD::CopyFromReg) {
451 RegisterSDNode *Reg = cast<RegisterSDNode>(Node->getOperand(1).getNode());
452 OpClass = MRI.getRegClass(Reg->getReg())->getID();
453
454 } else
455 return false;
456
457 if (OpClass == -1)
458 return false;
459
460 return TRI->getRegClass(RegClass)->hasSubClassEq(TRI->getRegClass(OpClass));
461}
462
463/// \brief Make sure that we don't exeed the number of allowed scalars
464void SITargetLowering::ensureSRegLimit(SelectionDAG &DAG, SDValue &Operand,
465 unsigned RegClass,
466 bool &ScalarSlotUsed) const {
467
468 // First map the operands register class to a destination class
469 if (RegClass == AMDGPU::VSrc_32RegClassID)
470 RegClass = AMDGPU::VReg_32RegClassID;
471 else if (RegClass == AMDGPU::VSrc_64RegClassID)
472 RegClass = AMDGPU::VReg_64RegClassID;
473 else
474 return;
475
476 // Nothing todo if they fit naturaly
477 if (fitsRegClass(DAG, Operand, RegClass))
478 return;
479
480 // If the scalar slot isn't used yet use it now
481 if (!ScalarSlotUsed) {
482 ScalarSlotUsed = true;
483 return;
484 }
485
486 // This is a conservative aproach, it is possible that we can't determine
487 // the correct register class and copy too often, but better save than sorry.
488 SDValue RC = DAG.getTargetConstant(RegClass, MVT::i32);
489 SDNode *Node = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, DebugLoc(),
490 Operand.getValueType(), Operand, RC);
491 Operand = SDValue(Node, 0);
492}
493
Christian Konigc018eca2013-02-26 17:52:16 +0000494SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
495 SelectionDAG &DAG) const {
Christian Konigd3b55092013-02-26 17:52:23 +0000496
497 // Original encoding (either e32 or e64)
498 int Opcode = Node->getMachineOpcode();
499 const MCInstrDesc *Desc = &TII->get(Opcode);
500
501 unsigned NumDefs = Desc->getNumDefs();
502 unsigned NumOps = Desc->getNumOperands();
503
Christian Konig3c980d12013-02-26 17:52:47 +0000504 // e64 version if available, -1 otherwise
505 int OpcodeE64 = AMDGPU::getVOPe64(Opcode);
506 const MCInstrDesc *DescE64 = OpcodeE64 == -1 ? 0 : &TII->get(OpcodeE64);
507
508 assert(!DescE64 || DescE64->getNumDefs() == NumDefs);
509 assert(!DescE64 || DescE64->getNumOperands() == (NumOps + 4));
510
Christian Konigd3b55092013-02-26 17:52:23 +0000511 int32_t Immediate = Desc->getSize() == 4 ? 0 : -1;
512 bool HaveVSrc = false, HaveSSrc = false;
513
514 // First figure out what we alread have in this instruction
515 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
516 i != e && Op < NumOps; ++i, ++Op) {
517
518 unsigned RegClass = Desc->OpInfo[Op].RegClass;
519 if (isVSrc(RegClass))
520 HaveVSrc = true;
521 else if (isSSrc(RegClass))
522 HaveSSrc = true;
523 else
524 continue;
525
526 int32_t Imm = analyzeImmediate(Node->getOperand(i).getNode());
527 if (Imm != -1 && Imm != 0) {
528 // Literal immediate
529 Immediate = Imm;
530 }
531 }
532
533 // If we neither have VSrc nor SSrc it makes no sense to continue
534 if (!HaveVSrc && !HaveSSrc)
535 return Node;
536
537 // No scalar allowed when we have both VSrc and SSrc
538 bool ScalarSlotUsed = HaveVSrc && HaveSSrc;
539
540 // Second go over the operands and try to fold them
541 std::vector<SDValue> Ops;
Christian Konig3c980d12013-02-26 17:52:47 +0000542 bool Promote2e64 = false;
Christian Konigd3b55092013-02-26 17:52:23 +0000543 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
544 i != e && Op < NumOps; ++i, ++Op) {
545
546 const SDValue &Operand = Node->getOperand(i);
547 Ops.push_back(Operand);
548
549 // Already folded immediate ?
550 if (isa<ConstantSDNode>(Operand.getNode()) ||
551 isa<ConstantFPSDNode>(Operand.getNode()))
552 continue;
553
554 // Is this a VSrc or SSrc operand ?
555 unsigned RegClass = Desc->OpInfo[Op].RegClass;
Christian Konigb37afdc2013-02-26 17:52:36 +0000556 if (!isVSrc(RegClass) && !isSSrc(RegClass)) {
557
558 if (i == 1 && Desc->isCommutable() &&
559 fitsRegClass(DAG, Ops[0], RegClass) &&
560 foldImm(Ops[1], Immediate, ScalarSlotUsed)) {
561
562 assert(isVSrc(Desc->OpInfo[NumDefs].RegClass) ||
563 isSSrc(Desc->OpInfo[NumDefs].RegClass));
564
565 // Swap commutable operands
566 SDValue Tmp = Ops[1];
567 Ops[1] = Ops[0];
568 Ops[0] = Tmp;
Christian Konig3c980d12013-02-26 17:52:47 +0000569
570 } else if (DescE64 && !Immediate) {
571 // Test if it makes sense to switch to e64 encoding
572
573 RegClass = DescE64->OpInfo[Op].RegClass;
574 int32_t TmpImm = -1;
575 if ((isVSrc(RegClass) || isSSrc(RegClass)) &&
576 foldImm(Ops[i], TmpImm, ScalarSlotUsed)) {
577
578 Immediate = -1;
579 Promote2e64 = true;
580 Desc = DescE64;
581 DescE64 = 0;
582 }
Christian Konigb37afdc2013-02-26 17:52:36 +0000583 }
Christian Konigd3b55092013-02-26 17:52:23 +0000584 continue;
Christian Konigb37afdc2013-02-26 17:52:36 +0000585 }
Christian Konigd3b55092013-02-26 17:52:23 +0000586
587 // Try to fold the immediates
588 if (!foldImm(Ops[i], Immediate, ScalarSlotUsed)) {
589 // Folding didn't worked, make sure we don't hit the SReg limit
590 ensureSRegLimit(DAG, Ops[i], RegClass, ScalarSlotUsed);
591 }
592 }
593
Christian Konig3c980d12013-02-26 17:52:47 +0000594 if (Promote2e64) {
595 // Add the modifier flags while promoting
596 for (unsigned i = 0; i < 4; ++i)
597 Ops.push_back(DAG.getTargetConstant(0, MVT::i32));
598 }
599
Christian Konigd3b55092013-02-26 17:52:23 +0000600 // Add optional chain and glue
601 for (unsigned i = NumOps - NumDefs, e = Node->getNumOperands(); i < e; ++i)
602 Ops.push_back(Node->getOperand(i));
603
Christian Konig3c980d12013-02-26 17:52:47 +0000604 // Either create a complete new or update the current instruction
605 if (Promote2e64)
606 return DAG.getMachineNode(OpcodeE64, Node->getDebugLoc(),
607 Node->getVTList(), Ops.data(), Ops.size());
608 else
609 return DAG.UpdateNodeOperands(Node, Ops.data(), Ops.size());
Christian Konigc018eca2013-02-26 17:52:16 +0000610}