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