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