R600/SI: Use a ComplexPattern for DS loads and stores
[oota-llvm.git] / lib / Target / R600 / AMDGPUISelDAGToDAG.cpp
1 //===-- AMDILISelDAGToDAG.cpp - A dag to dag inst selector for AMDIL ------===//
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 Defines an instruction selector for the AMDGPU target.
12 //
13 //===----------------------------------------------------------------------===//
14 #include "AMDGPUInstrInfo.h"
15 #include "AMDGPUISelLowering.h" // For AMDGPUISD
16 #include "AMDGPURegisterInfo.h"
17 #include "AMDGPUSubtarget.h"
18 #include "R600InstrInfo.h"
19 #include "SIDefines.h"
20 #include "SIISelLowering.h"
21 #include "SIMachineFunctionInfo.h"
22 #include "llvm/CodeGen/FunctionLoweringInfo.h"
23 #include "llvm/CodeGen/PseudoSourceValue.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/CodeGen/SelectionDAG.h"
27 #include "llvm/CodeGen/SelectionDAGISel.h"
28 #include "llvm/IR/Function.h"
29
30 using namespace llvm;
31
32 //===----------------------------------------------------------------------===//
33 // Instruction Selector Implementation
34 //===----------------------------------------------------------------------===//
35
36 namespace {
37 /// AMDGPU specific code to select AMDGPU machine instructions for
38 /// SelectionDAG operations.
39 class AMDGPUDAGToDAGISel : public SelectionDAGISel {
40   // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can
41   // make the right decision when generating code for different targets.
42   const AMDGPUSubtarget &Subtarget;
43 public:
44   AMDGPUDAGToDAGISel(TargetMachine &TM);
45   virtual ~AMDGPUDAGToDAGISel();
46
47   SDNode *Select(SDNode *N) override;
48   const char *getPassName() const override;
49   void PostprocessISelDAG() override;
50
51 private:
52   bool isInlineImmediate(SDNode *N) const;
53   inline SDValue getSmallIPtrImm(unsigned Imm);
54   bool FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg, SDValue &Abs,
55                    const R600InstrInfo *TII);
56   bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
57   bool FoldDotOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
58
59   // Complex pattern selectors
60   bool SelectADDRParam(SDValue Addr, SDValue& R1, SDValue& R2);
61   bool SelectADDR(SDValue N, SDValue &R1, SDValue &R2);
62   bool SelectADDR64(SDValue N, SDValue &R1, SDValue &R2);
63
64   static bool checkType(const Value *ptr, unsigned int addrspace);
65   static bool checkPrivateAddress(const MachineMemOperand *Op);
66
67   static bool isGlobalStore(const StoreSDNode *N);
68   static bool isPrivateStore(const StoreSDNode *N);
69   static bool isLocalStore(const StoreSDNode *N);
70   static bool isRegionStore(const StoreSDNode *N);
71
72   bool isCPLoad(const LoadSDNode *N) const;
73   bool isConstantLoad(const LoadSDNode *N, int cbID) const;
74   bool isGlobalLoad(const LoadSDNode *N) const;
75   bool isParamLoad(const LoadSDNode *N) const;
76   bool isPrivateLoad(const LoadSDNode *N) const;
77   bool isLocalLoad(const LoadSDNode *N) const;
78   bool isRegionLoad(const LoadSDNode *N) const;
79
80   /// \returns True if the current basic block being selected is at control
81   ///          flow depth 0.  Meaning that the current block dominates the
82   //           exit block.
83   bool isCFDepth0() const;
84
85   const TargetRegisterClass *getOperandRegClass(SDNode *N, unsigned OpNo) const;
86   bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
87   bool SelectGlobalValueVariableOffset(SDValue Addr, SDValue &BaseReg,
88                                        SDValue& Offset);
89   bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
90   bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
91   bool isDSOffsetLegal(const SDValue &Base, unsigned Offset,
92                        unsigned OffsetBits) const;
93   bool SelectDS1Addr1Offset(SDValue Ptr, SDValue &Base, SDValue &Offset) const;
94   void SelectMUBUF(SDValue Addr, SDValue &SRsrc, SDValue &VAddr,
95                    SDValue &SOffset, SDValue &Offset, SDValue &Offen,
96                    SDValue &Idxen, SDValue &Addr64, SDValue &GLC, SDValue &SLC,
97                    SDValue &TFE) const;
98   bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, SDValue &VAddr,
99                          SDValue &Offset) const;
100   bool SelectMUBUFScratch(SDValue Addr, SDValue &RSrc, SDValue &VAddr,
101                           SDValue &SOffset, SDValue &ImmOffset) const;
102   bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &SOffset,
103                          SDValue &Offset, SDValue &GLC, SDValue &SLC,
104                          SDValue &TFE) const;
105   bool SelectVOP3Mods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
106   bool SelectVOP3Mods0(SDValue In, SDValue &Src, SDValue &SrcMods,
107                        SDValue &Clamp, SDValue &Omod) const;
108
109   SDNode *SelectADD_SUB_I64(SDNode *N);
110   SDNode *SelectDIV_SCALE(SDNode *N);
111
112   // Include the pieces autogenerated from the target description.
113 #include "AMDGPUGenDAGISel.inc"
114 };
115 }  // end anonymous namespace
116
117 /// \brief This pass converts a legalized DAG into a AMDGPU-specific
118 // DAG, ready for instruction scheduling.
119 FunctionPass *llvm::createAMDGPUISelDag(TargetMachine &TM) {
120   return new AMDGPUDAGToDAGISel(TM);
121 }
122
123 AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM)
124   : SelectionDAGISel(TM), Subtarget(TM.getSubtarget<AMDGPUSubtarget>()) {
125 }
126
127 AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() {
128 }
129
130 bool AMDGPUDAGToDAGISel::isInlineImmediate(SDNode *N) const {
131   const SITargetLowering *TL
132       = static_cast<const SITargetLowering *>(getTargetLowering());
133   return TL->analyzeImmediate(N) == 0;
134 }
135
136 /// \brief Determine the register class for \p OpNo
137 /// \returns The register class of the virtual register that will be used for
138 /// the given operand number \OpNo or NULL if the register class cannot be
139 /// determined.
140 const TargetRegisterClass *AMDGPUDAGToDAGISel::getOperandRegClass(SDNode *N,
141                                                           unsigned OpNo) const {
142   if (!N->isMachineOpcode())
143     return nullptr;
144
145   switch (N->getMachineOpcode()) {
146   default: {
147     const MCInstrDesc &Desc =
148         TM.getSubtargetImpl()->getInstrInfo()->get(N->getMachineOpcode());
149     unsigned OpIdx = Desc.getNumDefs() + OpNo;
150     if (OpIdx >= Desc.getNumOperands())
151       return nullptr;
152     int RegClass = Desc.OpInfo[OpIdx].RegClass;
153     if (RegClass == -1)
154       return nullptr;
155
156     return TM.getSubtargetImpl()->getRegisterInfo()->getRegClass(RegClass);
157   }
158   case AMDGPU::REG_SEQUENCE: {
159     unsigned RCID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
160     const TargetRegisterClass *SuperRC =
161         TM.getSubtargetImpl()->getRegisterInfo()->getRegClass(RCID);
162
163     SDValue SubRegOp = N->getOperand(OpNo + 1);
164     unsigned SubRegIdx = cast<ConstantSDNode>(SubRegOp)->getZExtValue();
165     return TM.getSubtargetImpl()->getRegisterInfo()->getSubClassWithSubReg(
166         SuperRC, SubRegIdx);
167   }
168   }
169 }
170
171 SDValue AMDGPUDAGToDAGISel::getSmallIPtrImm(unsigned int Imm) {
172   return CurDAG->getTargetConstant(Imm, MVT::i32);
173 }
174
175 bool AMDGPUDAGToDAGISel::SelectADDRParam(
176   SDValue Addr, SDValue& R1, SDValue& R2) {
177
178   if (Addr.getOpcode() == ISD::FrameIndex) {
179     if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
180       R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
181       R2 = CurDAG->getTargetConstant(0, MVT::i32);
182     } else {
183       R1 = Addr;
184       R2 = CurDAG->getTargetConstant(0, MVT::i32);
185     }
186   } else if (Addr.getOpcode() == ISD::ADD) {
187     R1 = Addr.getOperand(0);
188     R2 = Addr.getOperand(1);
189   } else {
190     R1 = Addr;
191     R2 = CurDAG->getTargetConstant(0, MVT::i32);
192   }
193   return true;
194 }
195
196 bool AMDGPUDAGToDAGISel::SelectADDR(SDValue Addr, SDValue& R1, SDValue& R2) {
197   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
198       Addr.getOpcode() == ISD::TargetGlobalAddress) {
199     return false;
200   }
201   return SelectADDRParam(Addr, R1, R2);
202 }
203
204
205 bool AMDGPUDAGToDAGISel::SelectADDR64(SDValue Addr, SDValue& R1, SDValue& R2) {
206   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
207       Addr.getOpcode() == ISD::TargetGlobalAddress) {
208     return false;
209   }
210
211   if (Addr.getOpcode() == ISD::FrameIndex) {
212     if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
213       R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
214       R2 = CurDAG->getTargetConstant(0, MVT::i64);
215     } else {
216       R1 = Addr;
217       R2 = CurDAG->getTargetConstant(0, MVT::i64);
218     }
219   } else if (Addr.getOpcode() == ISD::ADD) {
220     R1 = Addr.getOperand(0);
221     R2 = Addr.getOperand(1);
222   } else {
223     R1 = Addr;
224     R2 = CurDAG->getTargetConstant(0, MVT::i64);
225   }
226   return true;
227 }
228
229 SDNode *AMDGPUDAGToDAGISel::Select(SDNode *N) {
230   unsigned int Opc = N->getOpcode();
231   if (N->isMachineOpcode()) {
232     N->setNodeId(-1);
233     return nullptr;   // Already selected.
234   }
235
236   const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
237   switch (Opc) {
238   default: break;
239   // We are selecting i64 ADD here instead of custom lower it during
240   // DAG legalization, so we can fold some i64 ADDs used for address
241   // calculation into the LOAD and STORE instructions.
242   case ISD::ADD:
243   case ISD::SUB: {
244     if (N->getValueType(0) != MVT::i64 ||
245         ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS)
246       break;
247
248     return SelectADD_SUB_I64(N);
249   }
250   case ISD::SCALAR_TO_VECTOR:
251   case AMDGPUISD::BUILD_VERTICAL_VECTOR:
252   case ISD::BUILD_VECTOR: {
253     unsigned RegClassID;
254     const AMDGPURegisterInfo *TRI = static_cast<const AMDGPURegisterInfo *>(
255         TM.getSubtargetImpl()->getRegisterInfo());
256     const SIRegisterInfo *SIRI = static_cast<const SIRegisterInfo *>(
257         TM.getSubtargetImpl()->getRegisterInfo());
258     EVT VT = N->getValueType(0);
259     unsigned NumVectorElts = VT.getVectorNumElements();
260     EVT EltVT = VT.getVectorElementType();
261     assert(EltVT.bitsEq(MVT::i32));
262     if (ST.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
263       bool UseVReg = true;
264       for (SDNode::use_iterator U = N->use_begin(), E = SDNode::use_end();
265                                                     U != E; ++U) {
266         if (!U->isMachineOpcode()) {
267           continue;
268         }
269         const TargetRegisterClass *RC = getOperandRegClass(*U, U.getOperandNo());
270         if (!RC) {
271           continue;
272         }
273         if (SIRI->isSGPRClass(RC)) {
274           UseVReg = false;
275         }
276       }
277       switch(NumVectorElts) {
278       case 1: RegClassID = UseVReg ? AMDGPU::VReg_32RegClassID :
279                                      AMDGPU::SReg_32RegClassID;
280         break;
281       case 2: RegClassID = UseVReg ? AMDGPU::VReg_64RegClassID :
282                                      AMDGPU::SReg_64RegClassID;
283         break;
284       case 4: RegClassID = UseVReg ? AMDGPU::VReg_128RegClassID :
285                                      AMDGPU::SReg_128RegClassID;
286         break;
287       case 8: RegClassID = UseVReg ? AMDGPU::VReg_256RegClassID :
288                                      AMDGPU::SReg_256RegClassID;
289         break;
290       case 16: RegClassID = UseVReg ? AMDGPU::VReg_512RegClassID :
291                                       AMDGPU::SReg_512RegClassID;
292         break;
293       default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
294       }
295     } else {
296       // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
297       // that adds a 128 bits reg copy when going through TwoAddressInstructions
298       // pass. We want to avoid 128 bits copies as much as possible because they
299       // can't be bundled by our scheduler.
300       switch(NumVectorElts) {
301       case 2: RegClassID = AMDGPU::R600_Reg64RegClassID; break;
302       case 4:
303         if (Opc == AMDGPUISD::BUILD_VERTICAL_VECTOR)
304           RegClassID = AMDGPU::R600_Reg128VerticalRegClassID;
305         else
306           RegClassID = AMDGPU::R600_Reg128RegClassID;
307         break;
308       default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
309       }
310     }
311
312     SDValue RegClass = CurDAG->getTargetConstant(RegClassID, MVT::i32);
313
314     if (NumVectorElts == 1) {
315       return CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS, EltVT,
316                                   N->getOperand(0), RegClass);
317     }
318
319     assert(NumVectorElts <= 16 && "Vectors with more than 16 elements not "
320                                   "supported yet");
321     // 16 = Max Num Vector Elements
322     // 2 = 2 REG_SEQUENCE operands per element (value, subreg index)
323     // 1 = Vector Register Class
324     SmallVector<SDValue, 16 * 2 + 1> RegSeqArgs(NumVectorElts * 2 + 1);
325
326     RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, MVT::i32);
327     bool IsRegSeq = true;
328     unsigned NOps = N->getNumOperands();
329     for (unsigned i = 0; i < NOps; i++) {
330       // XXX: Why is this here?
331       if (dyn_cast<RegisterSDNode>(N->getOperand(i))) {
332         IsRegSeq = false;
333         break;
334       }
335       RegSeqArgs[1 + (2 * i)] = N->getOperand(i);
336       RegSeqArgs[1 + (2 * i) + 1] =
337               CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), MVT::i32);
338     }
339
340     if (NOps != NumVectorElts) {
341       // Fill in the missing undef elements if this was a scalar_to_vector.
342       assert(Opc == ISD::SCALAR_TO_VECTOR && NOps < NumVectorElts);
343
344       MachineSDNode *ImpDef = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
345                                                      SDLoc(N), EltVT);
346       for (unsigned i = NOps; i < NumVectorElts; ++i) {
347         RegSeqArgs[1 + (2 * i)] = SDValue(ImpDef, 0);
348         RegSeqArgs[1 + (2 * i) + 1] =
349           CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), MVT::i32);
350       }
351     }
352
353     if (!IsRegSeq)
354       break;
355     return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(),
356                                 RegSeqArgs);
357   }
358   case ISD::BUILD_PAIR: {
359     SDValue RC, SubReg0, SubReg1;
360     if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
361       break;
362     }
363     if (N->getValueType(0) == MVT::i128) {
364       RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32);
365       SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, MVT::i32);
366       SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, MVT::i32);
367     } else if (N->getValueType(0) == MVT::i64) {
368       RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32);
369       SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
370       SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
371     } else {
372       llvm_unreachable("Unhandled value type for BUILD_PAIR");
373     }
374     const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
375                             N->getOperand(1), SubReg1 };
376     return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
377                                   SDLoc(N), N->getValueType(0), Ops);
378   }
379
380   case ISD::Constant:
381   case ISD::ConstantFP: {
382     const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
383     if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
384         N->getValueType(0).getSizeInBits() != 64 || isInlineImmediate(N))
385       break;
386
387     uint64_t Imm;
388     if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(N))
389       Imm = FP->getValueAPF().bitcastToAPInt().getZExtValue();
390     else {
391       ConstantSDNode *C = cast<ConstantSDNode>(N);
392       Imm = C->getZExtValue();
393     }
394
395     SDNode *Lo = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SDLoc(N), MVT::i32,
396                                 CurDAG->getConstant(Imm & 0xFFFFFFFF, MVT::i32));
397     SDNode *Hi = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SDLoc(N), MVT::i32,
398                                 CurDAG->getConstant(Imm >> 32, MVT::i32));
399     const SDValue Ops[] = {
400       CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32),
401       SDValue(Lo, 0), CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32),
402       SDValue(Hi, 0), CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32)
403     };
404
405     return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, SDLoc(N),
406                                   N->getValueType(0), Ops);
407   }
408
409   case AMDGPUISD::REGISTER_LOAD: {
410     if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
411       break;
412     SDValue Addr, Offset;
413
414     SelectADDRIndirect(N->getOperand(1), Addr, Offset);
415     const SDValue Ops[] = {
416       Addr,
417       Offset,
418       CurDAG->getTargetConstant(0, MVT::i32),
419       N->getOperand(0),
420     };
421     return CurDAG->getMachineNode(AMDGPU::SI_RegisterLoad, SDLoc(N),
422                                   CurDAG->getVTList(MVT::i32, MVT::i64, MVT::Other),
423                                   Ops);
424   }
425   case AMDGPUISD::REGISTER_STORE: {
426     if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
427       break;
428     SDValue Addr, Offset;
429     SelectADDRIndirect(N->getOperand(2), Addr, Offset);
430     const SDValue Ops[] = {
431       N->getOperand(1),
432       Addr,
433       Offset,
434       CurDAG->getTargetConstant(0, MVT::i32),
435       N->getOperand(0),
436     };
437     return CurDAG->getMachineNode(AMDGPU::SI_RegisterStorePseudo, SDLoc(N),
438                                         CurDAG->getVTList(MVT::Other),
439                                         Ops);
440   }
441
442   case AMDGPUISD::BFE_I32:
443   case AMDGPUISD::BFE_U32: {
444     if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS)
445       break;
446
447     // There is a scalar version available, but unlike the vector version which
448     // has a separate operand for the offset and width, the scalar version packs
449     // the width and offset into a single operand. Try to move to the scalar
450     // version if the offsets are constant, so that we can try to keep extended
451     // loads of kernel arguments in SGPRs.
452
453     // TODO: Technically we could try to pattern match scalar bitshifts of
454     // dynamic values, but it's probably not useful.
455     ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
456     if (!Offset)
457       break;
458
459     ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
460     if (!Width)
461       break;
462
463     bool Signed = Opc == AMDGPUISD::BFE_I32;
464
465     // Transformation function, pack the offset and width of a BFE into
466     // the format expected by the S_BFE_I32 / S_BFE_U32. In the second
467     // source, bits [5:0] contain the offset and bits [22:16] the width.
468
469     uint32_t OffsetVal = Offset->getZExtValue();
470     uint32_t WidthVal = Width->getZExtValue();
471
472     uint32_t PackedVal = OffsetVal | WidthVal << 16;
473
474     SDValue PackedOffsetWidth = CurDAG->getTargetConstant(PackedVal, MVT::i32);
475     return CurDAG->getMachineNode(Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32,
476                                   SDLoc(N),
477                                   MVT::i32,
478                                   N->getOperand(0),
479                                   PackedOffsetWidth);
480
481   }
482   case AMDGPUISD::DIV_SCALE: {
483     return SelectDIV_SCALE(N);
484   }
485   }
486   return SelectCode(N);
487 }
488
489
490 bool AMDGPUDAGToDAGISel::checkType(const Value *Ptr, unsigned AS) {
491   assert(AS != 0 && "Use checkPrivateAddress instead.");
492   if (!Ptr)
493     return false;
494
495   return Ptr->getType()->getPointerAddressSpace() == AS;
496 }
497
498 bool AMDGPUDAGToDAGISel::checkPrivateAddress(const MachineMemOperand *Op) {
499   if (Op->getPseudoValue())
500     return true;
501
502   if (PointerType *PT = dyn_cast<PointerType>(Op->getValue()->getType()))
503     return PT->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS;
504
505   return false;
506 }
507
508 bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
509   return checkType(N->getMemOperand()->getValue(), AMDGPUAS::GLOBAL_ADDRESS);
510 }
511
512 bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
513   const Value *MemVal = N->getMemOperand()->getValue();
514   return (!checkType(MemVal, AMDGPUAS::LOCAL_ADDRESS) &&
515           !checkType(MemVal, AMDGPUAS::GLOBAL_ADDRESS) &&
516           !checkType(MemVal, AMDGPUAS::REGION_ADDRESS));
517 }
518
519 bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
520   return checkType(N->getMemOperand()->getValue(), AMDGPUAS::LOCAL_ADDRESS);
521 }
522
523 bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
524   return checkType(N->getMemOperand()->getValue(), AMDGPUAS::REGION_ADDRESS);
525 }
526
527 bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int CbId) const {
528   const Value *MemVal = N->getMemOperand()->getValue();
529   if (CbId == -1)
530     return checkType(MemVal, AMDGPUAS::CONSTANT_ADDRESS);
531
532   return checkType(MemVal, AMDGPUAS::CONSTANT_BUFFER_0 + CbId);
533 }
534
535 bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) const {
536   if (N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) {
537     const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
538     if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
539         N->getMemoryVT().bitsLT(MVT::i32)) {
540       return true;
541     }
542   }
543   return checkType(N->getMemOperand()->getValue(), AMDGPUAS::GLOBAL_ADDRESS);
544 }
545
546 bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) const {
547   return checkType(N->getMemOperand()->getValue(), AMDGPUAS::PARAM_I_ADDRESS);
548 }
549
550 bool AMDGPUDAGToDAGISel::isLocalLoad(const  LoadSDNode *N) const {
551   return checkType(N->getMemOperand()->getValue(), AMDGPUAS::LOCAL_ADDRESS);
552 }
553
554 bool AMDGPUDAGToDAGISel::isRegionLoad(const  LoadSDNode *N) const {
555   return checkType(N->getMemOperand()->getValue(), AMDGPUAS::REGION_ADDRESS);
556 }
557
558 bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) const {
559   MachineMemOperand *MMO = N->getMemOperand();
560   if (checkPrivateAddress(N->getMemOperand())) {
561     if (MMO) {
562       const PseudoSourceValue *PSV = MMO->getPseudoValue();
563       if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
564         return true;
565       }
566     }
567   }
568   return false;
569 }
570
571 bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) const {
572   if (checkPrivateAddress(N->getMemOperand())) {
573     // Check to make sure we are not a constant pool load or a constant load
574     // that is marked as a private load
575     if (isCPLoad(N) || isConstantLoad(N, -1)) {
576       return false;
577     }
578   }
579
580   const Value *MemVal = N->getMemOperand()->getValue();
581   if (!checkType(MemVal, AMDGPUAS::LOCAL_ADDRESS) &&
582       !checkType(MemVal, AMDGPUAS::GLOBAL_ADDRESS) &&
583       !checkType(MemVal, AMDGPUAS::REGION_ADDRESS) &&
584       !checkType(MemVal, AMDGPUAS::CONSTANT_ADDRESS) &&
585       !checkType(MemVal, AMDGPUAS::PARAM_D_ADDRESS) &&
586       !checkType(MemVal, AMDGPUAS::PARAM_I_ADDRESS)){
587     return true;
588   }
589   return false;
590 }
591
592 bool AMDGPUDAGToDAGISel::isCFDepth0() const {
593   // FIXME: Figure out a way to use DominatorTree analysis here.
594   const BasicBlock *CurBlock = FuncInfo->MBB->getBasicBlock();
595   const Function *Fn = FuncInfo->Fn;
596   return &Fn->front() == CurBlock || &Fn->back() == CurBlock;
597 }
598
599
600 const char *AMDGPUDAGToDAGISel::getPassName() const {
601   return "AMDGPU DAG->DAG Pattern Instruction Selection";
602 }
603
604 #ifdef DEBUGTMP
605 #undef INT64_C
606 #endif
607 #undef DEBUGTMP
608
609 //===----------------------------------------------------------------------===//
610 // Complex Patterns
611 //===----------------------------------------------------------------------===//
612
613 bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
614                                                          SDValue& IntPtr) {
615   if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
616     IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
617     return true;
618   }
619   return false;
620 }
621
622 bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
623     SDValue& BaseReg, SDValue &Offset) {
624   if (!isa<ConstantSDNode>(Addr)) {
625     BaseReg = Addr;
626     Offset = CurDAG->getIntPtrConstant(0, true);
627     return true;
628   }
629   return false;
630 }
631
632 bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
633                                            SDValue &Offset) {
634   ConstantSDNode *IMMOffset;
635
636   if (Addr.getOpcode() == ISD::ADD
637       && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
638       && isInt<16>(IMMOffset->getZExtValue())) {
639
640       Base = Addr.getOperand(0);
641       Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
642       return true;
643   // If the pointer address is constant, we can move it to the offset field.
644   } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
645              && isInt<16>(IMMOffset->getZExtValue())) {
646     Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
647                                   SDLoc(CurDAG->getEntryNode()),
648                                   AMDGPU::ZERO, MVT::i32);
649     Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
650     return true;
651   }
652
653   // Default case, no offset
654   Base = Addr;
655   Offset = CurDAG->getTargetConstant(0, MVT::i32);
656   return true;
657 }
658
659 bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
660                                             SDValue &Offset) {
661   ConstantSDNode *C;
662
663   if ((C = dyn_cast<ConstantSDNode>(Addr))) {
664     Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
665     Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
666   } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
667             (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
668     Base = Addr.getOperand(0);
669     Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
670   } else {
671     Base = Addr;
672     Offset = CurDAG->getTargetConstant(0, MVT::i32);
673   }
674
675   return true;
676 }
677
678 SDNode *AMDGPUDAGToDAGISel::SelectADD_SUB_I64(SDNode *N) {
679   SDLoc DL(N);
680   SDValue LHS = N->getOperand(0);
681   SDValue RHS = N->getOperand(1);
682
683   bool IsAdd = (N->getOpcode() == ISD::ADD);
684
685   SDValue Sub0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
686   SDValue Sub1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
687
688   SDNode *Lo0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
689                                        DL, MVT::i32, LHS, Sub0);
690   SDNode *Hi0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
691                                        DL, MVT::i32, LHS, Sub1);
692
693   SDNode *Lo1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
694                                        DL, MVT::i32, RHS, Sub0);
695   SDNode *Hi1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
696                                        DL, MVT::i32, RHS, Sub1);
697
698   SDVTList VTList = CurDAG->getVTList(MVT::i32, MVT::Glue);
699   SDValue AddLoArgs[] = { SDValue(Lo0, 0), SDValue(Lo1, 0) };
700
701
702   unsigned Opc = IsAdd ? AMDGPU::S_ADD_I32 : AMDGPU::S_SUB_I32;
703   unsigned CarryOpc = IsAdd ? AMDGPU::S_ADDC_U32 : AMDGPU::S_SUBB_U32;
704
705   if (!isCFDepth0()) {
706     Opc = IsAdd ? AMDGPU::V_ADD_I32_e32 : AMDGPU::V_SUB_I32_e32;
707     CarryOpc = IsAdd ? AMDGPU::V_ADDC_U32_e32 : AMDGPU::V_SUBB_U32_e32;
708   }
709
710   SDNode *AddLo = CurDAG->getMachineNode( Opc, DL, VTList, AddLoArgs);
711   SDValue Carry(AddLo, 1);
712   SDNode *AddHi
713     = CurDAG->getMachineNode(CarryOpc, DL, MVT::i32,
714                              SDValue(Hi0, 0), SDValue(Hi1, 0), Carry);
715
716   SDValue Args[5] = {
717     CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32),
718     SDValue(AddLo,0),
719     Sub0,
720     SDValue(AddHi,0),
721     Sub1,
722   };
723   return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, MVT::i64, Args);
724 }
725
726 SDNode *AMDGPUDAGToDAGISel::SelectDIV_SCALE(SDNode *N) {
727   SDLoc SL(N);
728   EVT VT = N->getValueType(0);
729
730   assert(VT == MVT::f32 || VT == MVT::f64);
731
732   unsigned Opc
733     = (VT == MVT::f64) ? AMDGPU::V_DIV_SCALE_F64 : AMDGPU::V_DIV_SCALE_F32;
734
735   const SDValue Zero = CurDAG->getTargetConstant(0, MVT::i32);
736
737   SDValue Ops[] = {
738     N->getOperand(0),
739     N->getOperand(1),
740     N->getOperand(2),
741     Zero,
742     Zero,
743     Zero,
744     Zero
745   };
746
747   return CurDAG->SelectNodeTo(N, Opc, VT, MVT::i1, Ops);
748 }
749
750 bool AMDGPUDAGToDAGISel::isDSOffsetLegal(const SDValue &Base, unsigned Offset,
751                                          unsigned OffsetBits) const {
752   const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
753   if ((OffsetBits == 16 && !isUInt<16>(Offset)) ||
754       (OffsetBits == 8 && !isUInt<8>(Offset)))
755     return false;
756
757   if (ST.getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS)
758     return true;
759
760   // On Southern Islands instruction with a negative base value and an offset
761   // don't seem to work.
762   return CurDAG->SignBitIsZero(Base);
763 }
764
765 bool AMDGPUDAGToDAGISel::SelectDS1Addr1Offset(SDValue Addr, SDValue &Base,
766                                               SDValue &Offset) const {
767   if (CurDAG->isBaseWithConstantOffset(Addr)) {
768     SDValue N0 = Addr.getOperand(0);
769     SDValue N1 = Addr.getOperand(1);
770     ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
771     if (isDSOffsetLegal(N0, C1->getSExtValue(), 16)) {
772       // (add n0, c0)
773       Base = N0;
774       Offset = N1;
775       return true;
776     }
777   }
778
779   // default case
780   Base = Addr;
781   Offset = CurDAG->getTargetConstant(0, MVT::i16);
782   return true;
783 }
784
785 static SDValue wrapAddr64Rsrc(SelectionDAG *DAG, SDLoc DL, SDValue Ptr) {
786   return SDValue(DAG->getMachineNode(AMDGPU::SI_ADDR64_RSRC, DL, MVT::v4i32,
787                                      Ptr), 0);
788 }
789
790 static bool isLegalMUBUFImmOffset(const ConstantSDNode *Imm) {
791   return isUInt<12>(Imm->getZExtValue());
792 }
793
794 void AMDGPUDAGToDAGISel::SelectMUBUF(SDValue Addr, SDValue &Ptr,
795                                      SDValue &VAddr, SDValue &SOffset,
796                                      SDValue &Offset, SDValue &Offen,
797                                      SDValue &Idxen, SDValue &Addr64,
798                                      SDValue &GLC, SDValue &SLC,
799                                      SDValue &TFE) const {
800   SDLoc DL(Addr);
801
802   GLC = CurDAG->getTargetConstant(0, MVT::i1);
803   SLC = CurDAG->getTargetConstant(0, MVT::i1);
804   TFE = CurDAG->getTargetConstant(0, MVT::i1);
805
806   Idxen = CurDAG->getTargetConstant(0, MVT::i1);
807   Offen = CurDAG->getTargetConstant(0, MVT::i1);
808   Addr64 = CurDAG->getTargetConstant(0, MVT::i1);
809   SOffset = CurDAG->getTargetConstant(0, MVT::i32);
810
811   if (CurDAG->isBaseWithConstantOffset(Addr)) {
812     SDValue N0 = Addr.getOperand(0);
813     SDValue N1 = Addr.getOperand(1);
814     ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
815
816     if (isLegalMUBUFImmOffset(C1)) {
817
818       if (N0.getOpcode() == ISD::ADD) {
819         // (add (add N2, N3), C1) -> addr64
820         SDValue N2 = N0.getOperand(0);
821         SDValue N3 = N0.getOperand(1);
822         Addr64 = CurDAG->getTargetConstant(1, MVT::i1);
823         Ptr = N2;
824         VAddr = N3;
825         Offset = CurDAG->getTargetConstant(C1->getZExtValue(), MVT::i16);
826         return;
827       }
828
829       // (add N0, C1) -> offset
830       VAddr = CurDAG->getTargetConstant(0, MVT::i32);
831       Ptr = N0;
832       Offset = CurDAG->getTargetConstant(C1->getZExtValue(), MVT::i16);
833       return;
834     }
835   }
836   if (Addr.getOpcode() == ISD::ADD) {
837     // (add N0, N1) -> addr64
838     SDValue N0 = Addr.getOperand(0);
839     SDValue N1 = Addr.getOperand(1);
840     Addr64 = CurDAG->getTargetConstant(1, MVT::i1);
841     Ptr = N0;
842     VAddr = N1;
843     Offset = CurDAG->getTargetConstant(0, MVT::i16);
844     return;
845   }
846
847   // default case -> offset
848   VAddr = CurDAG->getTargetConstant(0, MVT::i32);
849   Ptr = Addr;
850   Offset = CurDAG->getTargetConstant(0, MVT::i16);
851
852 }
853
854 bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc,
855                                            SDValue &VAddr,
856                                            SDValue &Offset) const {
857   SDValue Ptr, SOffset, Offen, Idxen, Addr64, GLC, SLC, TFE;
858
859   SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64,
860               GLC, SLC, TFE);
861
862   ConstantSDNode *C = cast<ConstantSDNode>(Addr64);
863   if (C->getSExtValue()) {
864     SDLoc DL(Addr);
865     SRsrc = wrapAddr64Rsrc(CurDAG, DL, Ptr);
866     return true;
867   }
868   return false;
869 }
870
871 static SDValue buildRSRC(SelectionDAG *DAG, SDLoc DL, SDValue Ptr,
872                          uint32_t RsrcDword1, uint64_t RsrcDword2And3) {
873
874   SDValue PtrLo = DAG->getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
875   SDValue PtrHi = DAG->getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
876   if (RsrcDword1)
877     PtrHi = SDValue(DAG->getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
878                                     DAG->getConstant(RsrcDword1, MVT::i32)), 0);
879
880   SDValue DataLo = DAG->getTargetConstant(
881       RsrcDword2And3 & APInt::getAllOnesValue(32).getZExtValue(), MVT::i32);
882   SDValue DataHi = DAG->getTargetConstant(RsrcDword2And3 >> 32, MVT::i32);
883
884   const SDValue Ops[] = { PtrLo, PtrHi, DataLo, DataHi };
885   return SDValue(DAG->getMachineNode(AMDGPU::SI_BUFFER_RSRC, DL,
886                                      MVT::v4i32, Ops), 0);
887 }
888
889 /// \brief Return a resource descriptor with the 'Add TID' bit enabled
890 ///        The TID (Thread ID) is multipled by the stride value (bits [61:48]
891 ///        of the resource descriptor) to create an offset, which is added to the
892 ///        resource ponter.
893 static SDValue buildScratchRSRC(SelectionDAG *DAG, SDLoc DL, SDValue Ptr) {
894
895   uint64_t Rsrc = AMDGPU::RSRC_DATA_FORMAT | AMDGPU::RSRC_TID_ENABLE |
896                   0xffffffff; // Size
897
898   return buildRSRC(DAG, DL, Ptr, 0, Rsrc);
899 }
900
901 bool AMDGPUDAGToDAGISel::SelectMUBUFScratch(SDValue Addr, SDValue &Rsrc,
902                                             SDValue &VAddr, SDValue &SOffset,
903                                             SDValue &ImmOffset) const {
904
905   SDLoc DL(Addr);
906   MachineFunction &MF = CurDAG->getMachineFunction();
907   const SIRegisterInfo *TRI =
908       static_cast<const SIRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
909   MachineRegisterInfo &MRI = MF.getRegInfo();
910   const SITargetLowering& Lowering =
911     *static_cast<const SITargetLowering*>(getTargetLowering());
912
913   unsigned ScratchPtrReg =
914       TRI->getPreloadedValue(MF, SIRegisterInfo::SCRATCH_PTR);
915   unsigned ScratchOffsetReg =
916       TRI->getPreloadedValue(MF, SIRegisterInfo::SCRATCH_WAVE_OFFSET);
917   Lowering.CreateLiveInRegister(*CurDAG, &AMDGPU::SReg_32RegClass,
918                                 ScratchOffsetReg, MVT::i32);
919
920   Rsrc = buildScratchRSRC(CurDAG, DL,
921       CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
922                              MRI.getLiveInVirtReg(ScratchPtrReg), MVT::i64));
923   SOffset = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
924       MRI.getLiveInVirtReg(ScratchOffsetReg), MVT::i32);
925
926   // (add n0, c1)
927   if (CurDAG->isBaseWithConstantOffset(Addr)) {
928     SDValue N1 = Addr.getOperand(1);
929     ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
930
931     if (isLegalMUBUFImmOffset(C1)) {
932       VAddr = Addr.getOperand(0);
933       ImmOffset = CurDAG->getTargetConstant(C1->getZExtValue(), MVT::i16);
934       return true;
935     }
936   }
937
938   // (add FI, n0)
939   if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
940        isa<FrameIndexSDNode>(Addr.getOperand(0))) {
941     VAddr = Addr.getOperand(1);
942     ImmOffset = Addr.getOperand(0);
943     return true;
944   }
945
946   // (FI)
947   if (isa<FrameIndexSDNode>(Addr)) {
948     VAddr = SDValue(CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32, DL, MVT::i32,
949                                           CurDAG->getConstant(0, MVT::i32)), 0);
950     ImmOffset = Addr;
951     return true;
952   }
953
954   // (node)
955   VAddr = Addr;
956   ImmOffset = CurDAG->getTargetConstant(0, MVT::i16);
957   return true;
958 }
959
960 bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc,
961                                            SDValue &SOffset, SDValue &Offset,
962                                            SDValue &GLC, SDValue &SLC,
963                                            SDValue &TFE) const {
964   SDValue Ptr, VAddr, Offen, Idxen, Addr64;
965
966   SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64,
967               GLC, SLC, TFE);
968
969   if (!cast<ConstantSDNode>(Offen)->getSExtValue() &&
970       !cast<ConstantSDNode>(Idxen)->getSExtValue() &&
971       !cast<ConstantSDNode>(Addr64)->getSExtValue()) {
972     uint64_t Rsrc = AMDGPU::RSRC_DATA_FORMAT |
973                     APInt::getAllOnesValue(32).getZExtValue(); // Size
974     SDLoc DL(Addr);
975     SRsrc = buildRSRC(CurDAG, DL, Ptr, 0, Rsrc);
976     return true;
977   }
978   return false;
979 }
980
981 bool AMDGPUDAGToDAGISel::SelectVOP3Mods(SDValue In, SDValue &Src,
982                                         SDValue &SrcMods) const {
983
984   unsigned Mods = 0;
985
986   Src = In;
987
988   if (Src.getOpcode() == ISD::FNEG) {
989     Mods |= SISrcMods::NEG;
990     Src = Src.getOperand(0);
991   }
992
993   if (Src.getOpcode() == ISD::FABS) {
994     Mods |= SISrcMods::ABS;
995     Src = Src.getOperand(0);
996   }
997
998   SrcMods = CurDAG->getTargetConstant(Mods, MVT::i32);
999
1000   return true;
1001 }
1002
1003 bool AMDGPUDAGToDAGISel::SelectVOP3Mods0(SDValue In, SDValue &Src,
1004                                          SDValue &SrcMods, SDValue &Clamp,
1005                                          SDValue &Omod) const {
1006   // FIXME: Handle Clamp and Omod
1007   Clamp = CurDAG->getTargetConstant(0, MVT::i32);
1008   Omod = CurDAG->getTargetConstant(0, MVT::i32);
1009
1010   return SelectVOP3Mods(In, Src, SrcMods);
1011 }
1012
1013 void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
1014   const AMDGPUTargetLowering& Lowering =
1015     *static_cast<const AMDGPUTargetLowering*>(getTargetLowering());
1016   bool IsModified = false;
1017   do {
1018     IsModified = false;
1019     // Go over all selected nodes and try to fold them a bit more
1020     for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
1021          E = CurDAG->allnodes_end(); I != E; ++I) {
1022
1023       SDNode *Node = I;
1024
1025       MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
1026       if (!MachineNode)
1027         continue;
1028
1029       SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
1030       if (ResNode != Node) {
1031         ReplaceUses(Node, ResNode);
1032         IsModified = true;
1033       }
1034     }
1035     CurDAG->RemoveDeadNodes();
1036   } while (IsModified);
1037 }