1 //===-- AMDGPUISelDAGToDAG.cpp - A dag to dag inst selector for AMDGPU ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //==-----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// Defines an instruction selector for the AMDGPU target.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "AMDGPU.h"
15 #include "AMDGPUTargetMachine.h"
16 #include "SIMachineFunctionInfo.h"
17 #include "llvm/Analysis/LegacyDivergenceAnalysis.h"
18 #include "llvm/Analysis/ValueTracking.h"
19 #include "llvm/CodeGen/FunctionLoweringInfo.h"
20 #include "llvm/CodeGen/SelectionDAG.h"
21 #include "llvm/CodeGen/SelectionDAGISel.h"
22 #include "llvm/CodeGen/SelectionDAGNodes.h"
23 #include "llvm/IR/IntrinsicsAMDGPU.h"
24 #include "llvm/InitializePasses.h"
25 
26 #ifdef EXPENSIVE_CHECKS
27 #include "llvm/Analysis/LoopInfo.h"
28 #include "llvm/IR/Dominators.h"
29 #endif
30 
31 #define DEBUG_TYPE "isel"
32 
33 using namespace llvm;
34 
35 namespace llvm {
36 
37 class R600InstrInfo;
38 
39 } // end namespace llvm
40 
41 //===----------------------------------------------------------------------===//
42 // Instruction Selector Implementation
43 //===----------------------------------------------------------------------===//
44 
45 namespace {
46 
47 static bool isNullConstantOrUndef(SDValue V) {
48   if (V.isUndef())
49     return true;
50 
51   ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
52   return Const != nullptr && Const->isNullValue();
53 }
54 
55 static bool getConstantValue(SDValue N, uint32_t &Out) {
56   // This is only used for packed vectors, where ussing 0 for undef should
57   // always be good.
58   if (N.isUndef()) {
59     Out = 0;
60     return true;
61   }
62 
63   if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
64     Out = C->getAPIntValue().getSExtValue();
65     return true;
66   }
67 
68   if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N)) {
69     Out = C->getValueAPF().bitcastToAPInt().getSExtValue();
70     return true;
71   }
72 
73   return false;
74 }
75 
76 // TODO: Handle undef as zero
77 static SDNode *packConstantV2I16(const SDNode *N, SelectionDAG &DAG,
78                                  bool Negate = false) {
79   assert(N->getOpcode() == ISD::BUILD_VECTOR && N->getNumOperands() == 2);
80   uint32_t LHSVal, RHSVal;
81   if (getConstantValue(N->getOperand(0), LHSVal) &&
82       getConstantValue(N->getOperand(1), RHSVal)) {
83     SDLoc SL(N);
84     uint32_t K = Negate ?
85       (-LHSVal & 0xffff) | (-RHSVal << 16) :
86       (LHSVal & 0xffff) | (RHSVal << 16);
87     return DAG.getMachineNode(AMDGPU::S_MOV_B32, SL, N->getValueType(0),
88                               DAG.getTargetConstant(K, SL, MVT::i32));
89   }
90 
91   return nullptr;
92 }
93 
94 static SDNode *packNegConstantV2I16(const SDNode *N, SelectionDAG &DAG) {
95   return packConstantV2I16(N, DAG, true);
96 }
97 
98 /// AMDGPU specific code to select AMDGPU machine instructions for
99 /// SelectionDAG operations.
100 class AMDGPUDAGToDAGISel : public SelectionDAGISel {
101   // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can
102   // make the right decision when generating code for different targets.
103   const GCNSubtarget *Subtarget;
104 
105   // Default FP mode for the current function.
106   AMDGPU::SIModeRegisterDefaults Mode;
107 
108   bool EnableLateStructurizeCFG;
109 
110 public:
111   explicit AMDGPUDAGToDAGISel(TargetMachine *TM = nullptr,
112                               CodeGenOpt::Level OptLevel = CodeGenOpt::Default)
113     : SelectionDAGISel(*TM, OptLevel) {
114     EnableLateStructurizeCFG = AMDGPUTargetMachine::EnableLateStructurizeCFG;
115   }
116   ~AMDGPUDAGToDAGISel() override = default;
117 
118   void getAnalysisUsage(AnalysisUsage &AU) const override {
119     AU.addRequired<AMDGPUArgumentUsageInfo>();
120     AU.addRequired<LegacyDivergenceAnalysis>();
121 #ifdef EXPENSIVE_CHECKS
122     AU.addRequired<DominatorTreeWrapperPass>();
123     AU.addRequired<LoopInfoWrapperPass>();
124 #endif
125     SelectionDAGISel::getAnalysisUsage(AU);
126   }
127 
128   bool matchLoadD16FromBuildVector(SDNode *N) const;
129 
130   bool runOnMachineFunction(MachineFunction &MF) override;
131   void PreprocessISelDAG() override;
132   void Select(SDNode *N) override;
133   StringRef getPassName() const override;
134   void PostprocessISelDAG() override;
135 
136 protected:
137   void SelectBuildVector(SDNode *N, unsigned RegClassID);
138 
139 private:
140   std::pair<SDValue, SDValue> foldFrameIndex(SDValue N) const;
141   bool isNoNanSrc(SDValue N) const;
142   bool isInlineImmediate(const SDNode *N, bool Negated = false) const;
143   bool isNegInlineImmediate(const SDNode *N) const {
144     return isInlineImmediate(N, true);
145   }
146 
147   bool isInlineImmediate16(int64_t Imm) const {
148     return AMDGPU::isInlinableLiteral16(Imm, Subtarget->hasInv2PiInlineImm());
149   }
150 
151   bool isInlineImmediate32(int64_t Imm) const {
152     return AMDGPU::isInlinableLiteral32(Imm, Subtarget->hasInv2PiInlineImm());
153   }
154 
155   bool isInlineImmediate64(int64_t Imm) const {
156     return AMDGPU::isInlinableLiteral64(Imm, Subtarget->hasInv2PiInlineImm());
157   }
158 
159   bool isInlineImmediate(const APFloat &Imm) const {
160     return Subtarget->getInstrInfo()->isInlineConstant(Imm);
161   }
162 
163   bool isVGPRImm(const SDNode *N) const;
164   bool isUniformLoad(const SDNode *N) const;
165   bool isUniformBr(const SDNode *N) const;
166 
167   bool isBaseWithConstantOffset64(SDValue Addr, SDValue &LHS,
168                                   SDValue &RHS) const;
169 
170   MachineSDNode *buildSMovImm64(SDLoc &DL, uint64_t Val, EVT VT) const;
171 
172   SDNode *glueCopyToOp(SDNode *N, SDValue NewChain, SDValue Glue) const;
173   SDNode *glueCopyToM0(SDNode *N, SDValue Val) const;
174   SDNode *glueCopyToM0LDSInit(SDNode *N) const;
175 
176   const TargetRegisterClass *getOperandRegClass(SDNode *N, unsigned OpNo) const;
177   virtual bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
178   virtual bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
179   bool isDSOffsetLegal(SDValue Base, unsigned Offset) const;
180   bool isDSOffset2Legal(SDValue Base, unsigned Offset0, unsigned Offset1,
181                         unsigned Size) const;
182   bool SelectDS1Addr1Offset(SDValue Ptr, SDValue &Base, SDValue &Offset) const;
183   bool SelectDS64Bit4ByteAligned(SDValue Ptr, SDValue &Base, SDValue &Offset0,
184                                  SDValue &Offset1) const;
185   bool SelectDS128Bit8ByteAligned(SDValue Ptr, SDValue &Base, SDValue &Offset0,
186                                   SDValue &Offset1) const;
187   bool SelectDSReadWrite2(SDValue Ptr, SDValue &Base, SDValue &Offset0,
188                           SDValue &Offset1, unsigned Size) const;
189   bool SelectMUBUF(SDValue Addr, SDValue &SRsrc, SDValue &VAddr,
190                    SDValue &SOffset, SDValue &Offset, SDValue &Offen,
191                    SDValue &Idxen, SDValue &Addr64, SDValue &GLC, SDValue &SLC,
192                    SDValue &TFE, SDValue &DLC, SDValue &SWZ) const;
193   bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, SDValue &VAddr,
194                          SDValue &SOffset, SDValue &Offset, SDValue &GLC,
195                          SDValue &SLC, SDValue &TFE, SDValue &DLC,
196                          SDValue &SWZ) const;
197   bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc,
198                          SDValue &VAddr, SDValue &SOffset, SDValue &Offset,
199                          SDValue &SLC) const;
200   bool SelectMUBUFScratchOffen(SDNode *Parent,
201                                SDValue Addr, SDValue &RSrc, SDValue &VAddr,
202                                SDValue &SOffset, SDValue &ImmOffset) const;
203   bool SelectMUBUFScratchOffset(SDNode *Parent,
204                                 SDValue Addr, SDValue &SRsrc, SDValue &Soffset,
205                                 SDValue &Offset) const;
206 
207   bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &SOffset,
208                          SDValue &Offset, SDValue &GLC, SDValue &SLC,
209                          SDValue &TFE, SDValue &DLC, SDValue &SWZ) const;
210   bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &Soffset,
211                          SDValue &Offset, SDValue &SLC) const;
212   bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &Soffset,
213                          SDValue &Offset) const;
214 
215   template <bool IsSigned>
216   bool SelectFlatOffset(SDNode *N, SDValue Addr, SDValue &VAddr,
217                         SDValue &Offset) const;
218   bool SelectGlobalSAddr(SDNode *N, SDValue Addr, SDValue &SAddr,
219                          SDValue &VOffset, SDValue &Offset) const;
220   bool SelectScratchSAddr(SDNode *N, SDValue Addr, SDValue &SAddr,
221                           SDValue &Offset) const;
222 
223   bool SelectSMRDOffset(SDValue ByteOffsetNode, SDValue &Offset,
224                         bool &Imm) const;
225   SDValue Expand32BitAddress(SDValue Addr) const;
226   bool SelectSMRD(SDValue Addr, SDValue &SBase, SDValue &Offset,
227                   bool &Imm) const;
228   bool SelectSMRDImm(SDValue Addr, SDValue &SBase, SDValue &Offset) const;
229   bool SelectSMRDImm32(SDValue Addr, SDValue &SBase, SDValue &Offset) const;
230   bool SelectSMRDSgpr(SDValue Addr, SDValue &SBase, SDValue &Offset) const;
231   bool SelectSMRDBufferImm(SDValue Addr, SDValue &Offset) const;
232   bool SelectSMRDBufferImm32(SDValue Addr, SDValue &Offset) const;
233   bool SelectMOVRELOffset(SDValue Index, SDValue &Base, SDValue &Offset) const;
234 
235   bool SelectVOP3Mods_NNaN(SDValue In, SDValue &Src, SDValue &SrcMods) const;
236   bool SelectVOP3ModsImpl(SDValue In, SDValue &Src, unsigned &SrcMods,
237                           bool AllowAbs = true) const;
238   bool SelectVOP3Mods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
239   bool SelectVOP3BMods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
240   bool SelectVOP3NoMods(SDValue In, SDValue &Src) const;
241   bool SelectVOP3Mods0(SDValue In, SDValue &Src, SDValue &SrcMods,
242                        SDValue &Clamp, SDValue &Omod) const;
243   bool SelectVOP3BMods0(SDValue In, SDValue &Src, SDValue &SrcMods,
244                         SDValue &Clamp, SDValue &Omod) const;
245   bool SelectVOP3NoMods0(SDValue In, SDValue &Src, SDValue &SrcMods,
246                          SDValue &Clamp, SDValue &Omod) const;
247 
248   bool SelectVOP3OMods(SDValue In, SDValue &Src,
249                        SDValue &Clamp, SDValue &Omod) const;
250 
251   bool SelectVOP3PMods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
252 
253   bool SelectVOP3OpSel(SDValue In, SDValue &Src, SDValue &SrcMods) const;
254 
255   bool SelectVOP3OpSelMods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
256   bool SelectVOP3PMadMixModsImpl(SDValue In, SDValue &Src, unsigned &Mods) const;
257   bool SelectVOP3PMadMixMods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
258 
259   SDValue getHi16Elt(SDValue In) const;
260 
261   SDValue getMaterializedScalarImm32(int64_t Val, const SDLoc &DL) const;
262 
263   void SelectADD_SUB_I64(SDNode *N);
264   void SelectAddcSubb(SDNode *N);
265   void SelectUADDO_USUBO(SDNode *N);
266   void SelectDIV_SCALE(SDNode *N);
267   void SelectMAD_64_32(SDNode *N);
268   void SelectFMA_W_CHAIN(SDNode *N);
269   void SelectFMUL_W_CHAIN(SDNode *N);
270 
271   SDNode *getS_BFE(unsigned Opcode, const SDLoc &DL, SDValue Val,
272                    uint32_t Offset, uint32_t Width);
273   void SelectS_BFEFromShifts(SDNode *N);
274   void SelectS_BFE(SDNode *N);
275   bool isCBranchSCC(const SDNode *N) const;
276   void SelectBRCOND(SDNode *N);
277   void SelectFMAD_FMA(SDNode *N);
278   void SelectATOMIC_CMP_SWAP(SDNode *N);
279   void SelectDSAppendConsume(SDNode *N, unsigned IntrID);
280   void SelectDS_GWS(SDNode *N, unsigned IntrID);
281   void SelectInterpP1F16(SDNode *N);
282   void SelectINTRINSIC_W_CHAIN(SDNode *N);
283   void SelectINTRINSIC_WO_CHAIN(SDNode *N);
284   void SelectINTRINSIC_VOID(SDNode *N);
285 
286 protected:
287   // Include the pieces autogenerated from the target description.
288 #include "AMDGPUGenDAGISel.inc"
289 };
290 
291 class R600DAGToDAGISel : public AMDGPUDAGToDAGISel {
292   const R600Subtarget *Subtarget;
293 
294   bool isConstantLoad(const MemSDNode *N, int cbID) const;
295   bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
296   bool SelectGlobalValueVariableOffset(SDValue Addr, SDValue &BaseReg,
297                                        SDValue& Offset);
298 public:
299   explicit R600DAGToDAGISel(TargetMachine *TM, CodeGenOpt::Level OptLevel) :
300       AMDGPUDAGToDAGISel(TM, OptLevel) {}
301 
302   void Select(SDNode *N) override;
303 
304   bool SelectADDRIndirect(SDValue Addr, SDValue &Base,
305                           SDValue &Offset) override;
306   bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
307                           SDValue &Offset) override;
308 
309   bool runOnMachineFunction(MachineFunction &MF) override;
310 
311   void PreprocessISelDAG() override {}
312 
313 protected:
314   // Include the pieces autogenerated from the target description.
315 #include "R600GenDAGISel.inc"
316 };
317 
318 static SDValue stripBitcast(SDValue Val) {
319   return Val.getOpcode() == ISD::BITCAST ? Val.getOperand(0) : Val;
320 }
321 
322 // Figure out if this is really an extract of the high 16-bits of a dword.
323 static bool isExtractHiElt(SDValue In, SDValue &Out) {
324   In = stripBitcast(In);
325   if (In.getOpcode() != ISD::TRUNCATE)
326     return false;
327 
328   SDValue Srl = In.getOperand(0);
329   if (Srl.getOpcode() == ISD::SRL) {
330     if (ConstantSDNode *ShiftAmt = dyn_cast<ConstantSDNode>(Srl.getOperand(1))) {
331       if (ShiftAmt->getZExtValue() == 16) {
332         Out = stripBitcast(Srl.getOperand(0));
333         return true;
334       }
335     }
336   }
337 
338   return false;
339 }
340 
341 // Look through operations that obscure just looking at the low 16-bits of the
342 // same register.
343 static SDValue stripExtractLoElt(SDValue In) {
344   if (In.getOpcode() == ISD::TRUNCATE) {
345     SDValue Src = In.getOperand(0);
346     if (Src.getValueType().getSizeInBits() == 32)
347       return stripBitcast(Src);
348   }
349 
350   return In;
351 }
352 
353 }  // end anonymous namespace
354 
355 INITIALIZE_PASS_BEGIN(AMDGPUDAGToDAGISel, "amdgpu-isel",
356                       "AMDGPU DAG->DAG Pattern Instruction Selection", false, false)
357 INITIALIZE_PASS_DEPENDENCY(AMDGPUArgumentUsageInfo)
358 INITIALIZE_PASS_DEPENDENCY(AMDGPUPerfHintAnalysis)
359 INITIALIZE_PASS_DEPENDENCY(LegacyDivergenceAnalysis)
360 #ifdef EXPENSIVE_CHECKS
361 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
362 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
363 #endif
364 INITIALIZE_PASS_END(AMDGPUDAGToDAGISel, "amdgpu-isel",
365                     "AMDGPU DAG->DAG Pattern Instruction Selection", false, false)
366 
367 /// This pass converts a legalized DAG into a AMDGPU-specific
368 // DAG, ready for instruction scheduling.
369 FunctionPass *llvm::createAMDGPUISelDag(TargetMachine *TM,
370                                         CodeGenOpt::Level OptLevel) {
371   return new AMDGPUDAGToDAGISel(TM, OptLevel);
372 }
373 
374 /// This pass converts a legalized DAG into a R600-specific
375 // DAG, ready for instruction scheduling.
376 FunctionPass *llvm::createR600ISelDag(TargetMachine *TM,
377                                       CodeGenOpt::Level OptLevel) {
378   return new R600DAGToDAGISel(TM, OptLevel);
379 }
380 
381 bool AMDGPUDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
382 #ifdef EXPENSIVE_CHECKS
383   DominatorTree & DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
384   LoopInfo * LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
385   for (auto &L : LI->getLoopsInPreorder()) {
386     assert(L->isLCSSAForm(DT));
387   }
388 #endif
389   Subtarget = &MF.getSubtarget<GCNSubtarget>();
390   Mode = AMDGPU::SIModeRegisterDefaults(MF.getFunction());
391   return SelectionDAGISel::runOnMachineFunction(MF);
392 }
393 
394 bool AMDGPUDAGToDAGISel::matchLoadD16FromBuildVector(SDNode *N) const {
395   assert(Subtarget->d16PreservesUnusedBits());
396   MVT VT = N->getValueType(0).getSimpleVT();
397   if (VT != MVT::v2i16 && VT != MVT::v2f16)
398     return false;
399 
400   SDValue Lo = N->getOperand(0);
401   SDValue Hi = N->getOperand(1);
402 
403   LoadSDNode *LdHi = dyn_cast<LoadSDNode>(stripBitcast(Hi));
404 
405   // build_vector lo, (load ptr) -> load_d16_hi ptr, lo
406   // build_vector lo, (zextload ptr from i8) -> load_d16_hi_u8 ptr, lo
407   // build_vector lo, (sextload ptr from i8) -> load_d16_hi_i8 ptr, lo
408 
409   // Need to check for possible indirect dependencies on the other half of the
410   // vector to avoid introducing a cycle.
411   if (LdHi && Hi.hasOneUse() && !LdHi->isPredecessorOf(Lo.getNode())) {
412     SDVTList VTList = CurDAG->getVTList(VT, MVT::Other);
413 
414     SDValue TiedIn = CurDAG->getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), VT, Lo);
415     SDValue Ops[] = {
416       LdHi->getChain(), LdHi->getBasePtr(), TiedIn
417     };
418 
419     unsigned LoadOp = AMDGPUISD::LOAD_D16_HI;
420     if (LdHi->getMemoryVT() == MVT::i8) {
421       LoadOp = LdHi->getExtensionType() == ISD::SEXTLOAD ?
422         AMDGPUISD::LOAD_D16_HI_I8 : AMDGPUISD::LOAD_D16_HI_U8;
423     } else {
424       assert(LdHi->getMemoryVT() == MVT::i16);
425     }
426 
427     SDValue NewLoadHi =
428       CurDAG->getMemIntrinsicNode(LoadOp, SDLoc(LdHi), VTList,
429                                   Ops, LdHi->getMemoryVT(),
430                                   LdHi->getMemOperand());
431 
432     CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), NewLoadHi);
433     CurDAG->ReplaceAllUsesOfValueWith(SDValue(LdHi, 1), NewLoadHi.getValue(1));
434     return true;
435   }
436 
437   // build_vector (load ptr), hi -> load_d16_lo ptr, hi
438   // build_vector (zextload ptr from i8), hi -> load_d16_lo_u8 ptr, hi
439   // build_vector (sextload ptr from i8), hi -> load_d16_lo_i8 ptr, hi
440   LoadSDNode *LdLo = dyn_cast<LoadSDNode>(stripBitcast(Lo));
441   if (LdLo && Lo.hasOneUse()) {
442     SDValue TiedIn = getHi16Elt(Hi);
443     if (!TiedIn || LdLo->isPredecessorOf(TiedIn.getNode()))
444       return false;
445 
446     SDVTList VTList = CurDAG->getVTList(VT, MVT::Other);
447     unsigned LoadOp = AMDGPUISD::LOAD_D16_LO;
448     if (LdLo->getMemoryVT() == MVT::i8) {
449       LoadOp = LdLo->getExtensionType() == ISD::SEXTLOAD ?
450         AMDGPUISD::LOAD_D16_LO_I8 : AMDGPUISD::LOAD_D16_LO_U8;
451     } else {
452       assert(LdLo->getMemoryVT() == MVT::i16);
453     }
454 
455     TiedIn = CurDAG->getNode(ISD::BITCAST, SDLoc(N), VT, TiedIn);
456 
457     SDValue Ops[] = {
458       LdLo->getChain(), LdLo->getBasePtr(), TiedIn
459     };
460 
461     SDValue NewLoadLo =
462       CurDAG->getMemIntrinsicNode(LoadOp, SDLoc(LdLo), VTList,
463                                   Ops, LdLo->getMemoryVT(),
464                                   LdLo->getMemOperand());
465 
466     CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), NewLoadLo);
467     CurDAG->ReplaceAllUsesOfValueWith(SDValue(LdLo, 1), NewLoadLo.getValue(1));
468     return true;
469   }
470 
471   return false;
472 }
473 
474 void AMDGPUDAGToDAGISel::PreprocessISelDAG() {
475   if (!Subtarget->d16PreservesUnusedBits())
476     return;
477 
478   SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_end();
479 
480   bool MadeChange = false;
481   while (Position != CurDAG->allnodes_begin()) {
482     SDNode *N = &*--Position;
483     if (N->use_empty())
484       continue;
485 
486     switch (N->getOpcode()) {
487     case ISD::BUILD_VECTOR:
488       MadeChange |= matchLoadD16FromBuildVector(N);
489       break;
490     default:
491       break;
492     }
493   }
494 
495   if (MadeChange) {
496     CurDAG->RemoveDeadNodes();
497     LLVM_DEBUG(dbgs() << "After PreProcess:\n";
498                CurDAG->dump(););
499   }
500 }
501 
502 bool AMDGPUDAGToDAGISel::isNoNanSrc(SDValue N) const {
503   if (TM.Options.NoNaNsFPMath)
504     return true;
505 
506   // TODO: Move into isKnownNeverNaN
507   if (N->getFlags().hasNoNaNs())
508     return true;
509 
510   return CurDAG->isKnownNeverNaN(N);
511 }
512 
513 bool AMDGPUDAGToDAGISel::isInlineImmediate(const SDNode *N,
514                                            bool Negated) const {
515   if (N->isUndef())
516     return true;
517 
518   const SIInstrInfo *TII = Subtarget->getInstrInfo();
519   if (Negated) {
520     if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N))
521       return TII->isInlineConstant(-C->getAPIntValue());
522 
523     if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N))
524       return TII->isInlineConstant(-C->getValueAPF().bitcastToAPInt());
525 
526   } else {
527     if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N))
528       return TII->isInlineConstant(C->getAPIntValue());
529 
530     if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N))
531       return TII->isInlineConstant(C->getValueAPF().bitcastToAPInt());
532   }
533 
534   return false;
535 }
536 
537 /// Determine the register class for \p OpNo
538 /// \returns The register class of the virtual register that will be used for
539 /// the given operand number \OpNo or NULL if the register class cannot be
540 /// determined.
541 const TargetRegisterClass *AMDGPUDAGToDAGISel::getOperandRegClass(SDNode *N,
542                                                           unsigned OpNo) const {
543   if (!N->isMachineOpcode()) {
544     if (N->getOpcode() == ISD::CopyToReg) {
545       Register Reg = cast<RegisterSDNode>(N->getOperand(1))->getReg();
546       if (Reg.isVirtual()) {
547         MachineRegisterInfo &MRI = CurDAG->getMachineFunction().getRegInfo();
548         return MRI.getRegClass(Reg);
549       }
550 
551       const SIRegisterInfo *TRI
552         = static_cast<const GCNSubtarget *>(Subtarget)->getRegisterInfo();
553       return TRI->getPhysRegClass(Reg);
554     }
555 
556     return nullptr;
557   }
558 
559   switch (N->getMachineOpcode()) {
560   default: {
561     const MCInstrDesc &Desc =
562         Subtarget->getInstrInfo()->get(N->getMachineOpcode());
563     unsigned OpIdx = Desc.getNumDefs() + OpNo;
564     if (OpIdx >= Desc.getNumOperands())
565       return nullptr;
566     int RegClass = Desc.OpInfo[OpIdx].RegClass;
567     if (RegClass == -1)
568       return nullptr;
569 
570     return Subtarget->getRegisterInfo()->getRegClass(RegClass);
571   }
572   case AMDGPU::REG_SEQUENCE: {
573     unsigned RCID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
574     const TargetRegisterClass *SuperRC =
575         Subtarget->getRegisterInfo()->getRegClass(RCID);
576 
577     SDValue SubRegOp = N->getOperand(OpNo + 1);
578     unsigned SubRegIdx = cast<ConstantSDNode>(SubRegOp)->getZExtValue();
579     return Subtarget->getRegisterInfo()->getSubClassWithSubReg(SuperRC,
580                                                               SubRegIdx);
581   }
582   }
583 }
584 
585 SDNode *AMDGPUDAGToDAGISel::glueCopyToOp(SDNode *N, SDValue NewChain,
586                                          SDValue Glue) const {
587   SmallVector <SDValue, 8> Ops;
588   Ops.push_back(NewChain); // Replace the chain.
589   for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
590     Ops.push_back(N->getOperand(i));
591 
592   Ops.push_back(Glue);
593   return CurDAG->MorphNodeTo(N, N->getOpcode(), N->getVTList(), Ops);
594 }
595 
596 SDNode *AMDGPUDAGToDAGISel::glueCopyToM0(SDNode *N, SDValue Val) const {
597   const SITargetLowering& Lowering =
598     *static_cast<const SITargetLowering*>(getTargetLowering());
599 
600   assert(N->getOperand(0).getValueType() == MVT::Other && "Expected chain");
601 
602   SDValue M0 = Lowering.copyToM0(*CurDAG, N->getOperand(0), SDLoc(N), Val);
603   return glueCopyToOp(N, M0, M0.getValue(1));
604 }
605 
606 SDNode *AMDGPUDAGToDAGISel::glueCopyToM0LDSInit(SDNode *N) const {
607   unsigned AS = cast<MemSDNode>(N)->getAddressSpace();
608   if (AS == AMDGPUAS::LOCAL_ADDRESS) {
609     if (Subtarget->ldsRequiresM0Init())
610       return glueCopyToM0(N, CurDAG->getTargetConstant(-1, SDLoc(N), MVT::i32));
611   } else if (AS == AMDGPUAS::REGION_ADDRESS) {
612     MachineFunction &MF = CurDAG->getMachineFunction();
613     unsigned Value = MF.getInfo<SIMachineFunctionInfo>()->getGDSSize();
614     return
615         glueCopyToM0(N, CurDAG->getTargetConstant(Value, SDLoc(N), MVT::i32));
616   }
617   return N;
618 }
619 
620 MachineSDNode *AMDGPUDAGToDAGISel::buildSMovImm64(SDLoc &DL, uint64_t Imm,
621                                                   EVT VT) const {
622   SDNode *Lo = CurDAG->getMachineNode(
623       AMDGPU::S_MOV_B32, DL, MVT::i32,
624       CurDAG->getTargetConstant(Imm & 0xFFFFFFFF, DL, MVT::i32));
625   SDNode *Hi =
626       CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32,
627                              CurDAG->getTargetConstant(Imm >> 32, DL, MVT::i32));
628   const SDValue Ops[] = {
629       CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32),
630       SDValue(Lo, 0), CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
631       SDValue(Hi, 0), CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32)};
632 
633   return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL, VT, Ops);
634 }
635 
636 void AMDGPUDAGToDAGISel::SelectBuildVector(SDNode *N, unsigned RegClassID) {
637   EVT VT = N->getValueType(0);
638   unsigned NumVectorElts = VT.getVectorNumElements();
639   EVT EltVT = VT.getVectorElementType();
640   SDLoc DL(N);
641   SDValue RegClass = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32);
642 
643   if (NumVectorElts == 1) {
644     CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS, EltVT, N->getOperand(0),
645                          RegClass);
646     return;
647   }
648 
649   assert(NumVectorElts <= 32 && "Vectors with more than 32 elements not "
650                                   "supported yet");
651   // 32 = Max Num Vector Elements
652   // 2 = 2 REG_SEQUENCE operands per element (value, subreg index)
653   // 1 = Vector Register Class
654   SmallVector<SDValue, 32 * 2 + 1> RegSeqArgs(NumVectorElts * 2 + 1);
655 
656   bool IsGCN = CurDAG->getSubtarget().getTargetTriple().getArch() ==
657                Triple::amdgcn;
658   RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32);
659   bool IsRegSeq = true;
660   unsigned NOps = N->getNumOperands();
661   for (unsigned i = 0; i < NOps; i++) {
662     // XXX: Why is this here?
663     if (isa<RegisterSDNode>(N->getOperand(i))) {
664       IsRegSeq = false;
665       break;
666     }
667     unsigned Sub = IsGCN ? SIRegisterInfo::getSubRegFromChannel(i)
668                          : R600RegisterInfo::getSubRegFromChannel(i);
669     RegSeqArgs[1 + (2 * i)] = N->getOperand(i);
670     RegSeqArgs[1 + (2 * i) + 1] = CurDAG->getTargetConstant(Sub, DL, MVT::i32);
671   }
672   if (NOps != NumVectorElts) {
673     // Fill in the missing undef elements if this was a scalar_to_vector.
674     assert(N->getOpcode() == ISD::SCALAR_TO_VECTOR && NOps < NumVectorElts);
675     MachineSDNode *ImpDef = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
676                                                    DL, EltVT);
677     for (unsigned i = NOps; i < NumVectorElts; ++i) {
678       unsigned Sub = IsGCN ? SIRegisterInfo::getSubRegFromChannel(i)
679                            : R600RegisterInfo::getSubRegFromChannel(i);
680       RegSeqArgs[1 + (2 * i)] = SDValue(ImpDef, 0);
681       RegSeqArgs[1 + (2 * i) + 1] =
682           CurDAG->getTargetConstant(Sub, DL, MVT::i32);
683     }
684   }
685 
686   if (!IsRegSeq)
687     SelectCode(N);
688   CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(), RegSeqArgs);
689 }
690 
691 void AMDGPUDAGToDAGISel::Select(SDNode *N) {
692   unsigned int Opc = N->getOpcode();
693   if (N->isMachineOpcode()) {
694     N->setNodeId(-1);
695     return;   // Already selected.
696   }
697 
698   // isa<MemSDNode> almost works but is slightly too permissive for some DS
699   // intrinsics.
700   if (Opc == ISD::LOAD || Opc == ISD::STORE || isa<AtomicSDNode>(N) ||
701       (Opc == AMDGPUISD::ATOMIC_INC || Opc == AMDGPUISD::ATOMIC_DEC ||
702        Opc == ISD::ATOMIC_LOAD_FADD ||
703        Opc == AMDGPUISD::ATOMIC_LOAD_FMIN ||
704        Opc == AMDGPUISD::ATOMIC_LOAD_FMAX)) {
705     N = glueCopyToM0LDSInit(N);
706     SelectCode(N);
707     return;
708   }
709 
710   switch (Opc) {
711   default:
712     break;
713   // We are selecting i64 ADD here instead of custom lower it during
714   // DAG legalization, so we can fold some i64 ADDs used for address
715   // calculation into the LOAD and STORE instructions.
716   case ISD::ADDC:
717   case ISD::ADDE:
718   case ISD::SUBC:
719   case ISD::SUBE: {
720     if (N->getValueType(0) != MVT::i64)
721       break;
722 
723     SelectADD_SUB_I64(N);
724     return;
725   }
726   case ISD::ADDCARRY:
727   case ISD::SUBCARRY:
728     if (N->getValueType(0) != MVT::i32)
729       break;
730 
731     SelectAddcSubb(N);
732     return;
733   case ISD::UADDO:
734   case ISD::USUBO: {
735     SelectUADDO_USUBO(N);
736     return;
737   }
738   case AMDGPUISD::FMUL_W_CHAIN: {
739     SelectFMUL_W_CHAIN(N);
740     return;
741   }
742   case AMDGPUISD::FMA_W_CHAIN: {
743     SelectFMA_W_CHAIN(N);
744     return;
745   }
746 
747   case ISD::SCALAR_TO_VECTOR:
748   case ISD::BUILD_VECTOR: {
749     EVT VT = N->getValueType(0);
750     unsigned NumVectorElts = VT.getVectorNumElements();
751     if (VT.getScalarSizeInBits() == 16) {
752       if (Opc == ISD::BUILD_VECTOR && NumVectorElts == 2) {
753         if (SDNode *Packed = packConstantV2I16(N, *CurDAG)) {
754           ReplaceNode(N, Packed);
755           return;
756         }
757       }
758 
759       break;
760     }
761 
762     assert(VT.getVectorElementType().bitsEq(MVT::i32));
763     unsigned RegClassID =
764         SIRegisterInfo::getSGPRClassForBitWidth(NumVectorElts * 32)->getID();
765     SelectBuildVector(N, RegClassID);
766     return;
767   }
768   case ISD::BUILD_PAIR: {
769     SDValue RC, SubReg0, SubReg1;
770     SDLoc DL(N);
771     if (N->getValueType(0) == MVT::i128) {
772       RC = CurDAG->getTargetConstant(AMDGPU::SGPR_128RegClassID, DL, MVT::i32);
773       SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32);
774       SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32);
775     } else if (N->getValueType(0) == MVT::i64) {
776       RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32);
777       SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32);
778       SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32);
779     } else {
780       llvm_unreachable("Unhandled value type for BUILD_PAIR");
781     }
782     const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
783                             N->getOperand(1), SubReg1 };
784     ReplaceNode(N, CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL,
785                                           N->getValueType(0), Ops));
786     return;
787   }
788 
789   case ISD::Constant:
790   case ISD::ConstantFP: {
791     if (N->getValueType(0).getSizeInBits() != 64 || isInlineImmediate(N))
792       break;
793 
794     uint64_t Imm;
795     if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(N))
796       Imm = FP->getValueAPF().bitcastToAPInt().getZExtValue();
797     else {
798       ConstantSDNode *C = cast<ConstantSDNode>(N);
799       Imm = C->getZExtValue();
800     }
801 
802     SDLoc DL(N);
803     ReplaceNode(N, buildSMovImm64(DL, Imm, N->getValueType(0)));
804     return;
805   }
806   case AMDGPUISD::BFE_I32:
807   case AMDGPUISD::BFE_U32: {
808     // There is a scalar version available, but unlike the vector version which
809     // has a separate operand for the offset and width, the scalar version packs
810     // the width and offset into a single operand. Try to move to the scalar
811     // version if the offsets are constant, so that we can try to keep extended
812     // loads of kernel arguments in SGPRs.
813 
814     // TODO: Technically we could try to pattern match scalar bitshifts of
815     // dynamic values, but it's probably not useful.
816     ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
817     if (!Offset)
818       break;
819 
820     ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
821     if (!Width)
822       break;
823 
824     bool Signed = Opc == AMDGPUISD::BFE_I32;
825 
826     uint32_t OffsetVal = Offset->getZExtValue();
827     uint32_t WidthVal = Width->getZExtValue();
828 
829     ReplaceNode(N, getS_BFE(Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32,
830                             SDLoc(N), N->getOperand(0), OffsetVal, WidthVal));
831     return;
832   }
833   case AMDGPUISD::DIV_SCALE: {
834     SelectDIV_SCALE(N);
835     return;
836   }
837   case AMDGPUISD::MAD_I64_I32:
838   case AMDGPUISD::MAD_U64_U32: {
839     SelectMAD_64_32(N);
840     return;
841   }
842   case ISD::CopyToReg: {
843     const SITargetLowering& Lowering =
844       *static_cast<const SITargetLowering*>(getTargetLowering());
845     N = Lowering.legalizeTargetIndependentNode(N, *CurDAG);
846     break;
847   }
848   case ISD::AND:
849   case ISD::SRL:
850   case ISD::SRA:
851   case ISD::SIGN_EXTEND_INREG:
852     if (N->getValueType(0) != MVT::i32)
853       break;
854 
855     SelectS_BFE(N);
856     return;
857   case ISD::BRCOND:
858     SelectBRCOND(N);
859     return;
860   case ISD::FMAD:
861   case ISD::FMA:
862     SelectFMAD_FMA(N);
863     return;
864   case AMDGPUISD::ATOMIC_CMP_SWAP:
865     SelectATOMIC_CMP_SWAP(N);
866     return;
867   case AMDGPUISD::CVT_PKRTZ_F16_F32:
868   case AMDGPUISD::CVT_PKNORM_I16_F32:
869   case AMDGPUISD::CVT_PKNORM_U16_F32:
870   case AMDGPUISD::CVT_PK_U16_U32:
871   case AMDGPUISD::CVT_PK_I16_I32: {
872     // Hack around using a legal type if f16 is illegal.
873     if (N->getValueType(0) == MVT::i32) {
874       MVT NewVT = Opc == AMDGPUISD::CVT_PKRTZ_F16_F32 ? MVT::v2f16 : MVT::v2i16;
875       N = CurDAG->MorphNodeTo(N, N->getOpcode(), CurDAG->getVTList(NewVT),
876                               { N->getOperand(0), N->getOperand(1) });
877       SelectCode(N);
878       return;
879     }
880 
881     break;
882   }
883   case ISD::INTRINSIC_W_CHAIN: {
884     SelectINTRINSIC_W_CHAIN(N);
885     return;
886   }
887   case ISD::INTRINSIC_WO_CHAIN: {
888     SelectINTRINSIC_WO_CHAIN(N);
889     return;
890   }
891   case ISD::INTRINSIC_VOID: {
892     SelectINTRINSIC_VOID(N);
893     return;
894   }
895   }
896 
897   SelectCode(N);
898 }
899 
900 bool AMDGPUDAGToDAGISel::isUniformBr(const SDNode *N) const {
901   const BasicBlock *BB = FuncInfo->MBB->getBasicBlock();
902   const Instruction *Term = BB->getTerminator();
903   return Term->getMetadata("amdgpu.uniform") ||
904          Term->getMetadata("structurizecfg.uniform");
905 }
906 
907 static bool getBaseWithOffsetUsingSplitOR(SelectionDAG &DAG, SDValue Addr,
908                                           SDValue &N0, SDValue &N1) {
909   if (Addr.getValueType() == MVT::i64 && Addr.getOpcode() == ISD::BITCAST &&
910       Addr.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
911     // As we split 64-bit `or` earlier, it's complicated pattern to match, i.e.
912     // (i64 (bitcast (v2i32 (build_vector
913     //                        (or (extract_vector_elt V, 0), OFFSET),
914     //                        (extract_vector_elt V, 1)))))
915     SDValue Lo = Addr.getOperand(0).getOperand(0);
916     if (Lo.getOpcode() == ISD::OR && DAG.isBaseWithConstantOffset(Lo)) {
917       SDValue BaseLo = Lo.getOperand(0);
918       SDValue BaseHi = Addr.getOperand(0).getOperand(1);
919       // Check that split base (Lo and Hi) are extracted from the same one.
920       if (BaseLo.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
921           BaseHi.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
922           BaseLo.getOperand(0) == BaseHi.getOperand(0) &&
923           // Lo is statically extracted from index 0.
924           isa<ConstantSDNode>(BaseLo.getOperand(1)) &&
925           BaseLo.getConstantOperandVal(1) == 0 &&
926           // Hi is statically extracted from index 0.
927           isa<ConstantSDNode>(BaseHi.getOperand(1)) &&
928           BaseHi.getConstantOperandVal(1) == 1) {
929         N0 = BaseLo.getOperand(0).getOperand(0);
930         N1 = Lo.getOperand(1);
931         return true;
932       }
933     }
934   }
935   return false;
936 }
937 
938 bool AMDGPUDAGToDAGISel::isBaseWithConstantOffset64(SDValue Addr, SDValue &LHS,
939                                                     SDValue &RHS) const {
940   if (CurDAG->isBaseWithConstantOffset(Addr)) {
941     LHS = Addr.getOperand(0);
942     RHS = Addr.getOperand(1);
943     return true;
944   }
945 
946   if (getBaseWithOffsetUsingSplitOR(*CurDAG, Addr, LHS, RHS)) {
947     assert(LHS && RHS && isa<ConstantSDNode>(RHS));
948     return true;
949   }
950 
951   return false;
952 }
953 
954 StringRef AMDGPUDAGToDAGISel::getPassName() const {
955   return "AMDGPU DAG->DAG Pattern Instruction Selection";
956 }
957 
958 //===----------------------------------------------------------------------===//
959 // Complex Patterns
960 //===----------------------------------------------------------------------===//
961 
962 bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
963                                             SDValue &Offset) {
964   return false;
965 }
966 
967 bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
968                                             SDValue &Offset) {
969   ConstantSDNode *C;
970   SDLoc DL(Addr);
971 
972   if ((C = dyn_cast<ConstantSDNode>(Addr))) {
973     Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32);
974     Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
975   } else if ((Addr.getOpcode() == AMDGPUISD::DWORDADDR) &&
976              (C = dyn_cast<ConstantSDNode>(Addr.getOperand(0)))) {
977     Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32);
978     Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
979   } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
980             (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
981     Base = Addr.getOperand(0);
982     Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
983   } else {
984     Base = Addr;
985     Offset = CurDAG->getTargetConstant(0, DL, MVT::i32);
986   }
987 
988   return true;
989 }
990 
991 SDValue AMDGPUDAGToDAGISel::getMaterializedScalarImm32(int64_t Val,
992                                                        const SDLoc &DL) const {
993   SDNode *Mov = CurDAG->getMachineNode(
994     AMDGPU::S_MOV_B32, DL, MVT::i32,
995     CurDAG->getTargetConstant(Val, DL, MVT::i32));
996   return SDValue(Mov, 0);
997 }
998 
999 // FIXME: Should only handle addcarry/subcarry
1000 void AMDGPUDAGToDAGISel::SelectADD_SUB_I64(SDNode *N) {
1001   SDLoc DL(N);
1002   SDValue LHS = N->getOperand(0);
1003   SDValue RHS = N->getOperand(1);
1004 
1005   unsigned Opcode = N->getOpcode();
1006   bool ConsumeCarry = (Opcode == ISD::ADDE || Opcode == ISD::SUBE);
1007   bool ProduceCarry =
1008       ConsumeCarry || Opcode == ISD::ADDC || Opcode == ISD::SUBC;
1009   bool IsAdd = Opcode == ISD::ADD || Opcode == ISD::ADDC || Opcode == ISD::ADDE;
1010 
1011   SDValue Sub0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32);
1012   SDValue Sub1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32);
1013 
1014   SDNode *Lo0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
1015                                        DL, MVT::i32, LHS, Sub0);
1016   SDNode *Hi0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
1017                                        DL, MVT::i32, LHS, Sub1);
1018 
1019   SDNode *Lo1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
1020                                        DL, MVT::i32, RHS, Sub0);
1021   SDNode *Hi1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
1022                                        DL, MVT::i32, RHS, Sub1);
1023 
1024   SDVTList VTList = CurDAG->getVTList(MVT::i32, MVT::Glue);
1025 
1026   static const unsigned OpcMap[2][2][2] = {
1027       {{AMDGPU::S_SUB_U32, AMDGPU::S_ADD_U32},
1028        {AMDGPU::V_SUB_CO_U32_e32, AMDGPU::V_ADD_CO_U32_e32}},
1029       {{AMDGPU::S_SUBB_U32, AMDGPU::S_ADDC_U32},
1030        {AMDGPU::V_SUBB_U32_e32, AMDGPU::V_ADDC_U32_e32}}};
1031 
1032   unsigned Opc = OpcMap[0][N->isDivergent()][IsAdd];
1033   unsigned CarryOpc = OpcMap[1][N->isDivergent()][IsAdd];
1034 
1035   SDNode *AddLo;
1036   if (!ConsumeCarry) {
1037     SDValue Args[] = { SDValue(Lo0, 0), SDValue(Lo1, 0) };
1038     AddLo = CurDAG->getMachineNode(Opc, DL, VTList, Args);
1039   } else {
1040     SDValue Args[] = { SDValue(Lo0, 0), SDValue(Lo1, 0), N->getOperand(2) };
1041     AddLo = CurDAG->getMachineNode(CarryOpc, DL, VTList, Args);
1042   }
1043   SDValue AddHiArgs[] = {
1044     SDValue(Hi0, 0),
1045     SDValue(Hi1, 0),
1046     SDValue(AddLo, 1)
1047   };
1048   SDNode *AddHi = CurDAG->getMachineNode(CarryOpc, DL, VTList, AddHiArgs);
1049 
1050   SDValue RegSequenceArgs[] = {
1051     CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32),
1052     SDValue(AddLo,0),
1053     Sub0,
1054     SDValue(AddHi,0),
1055     Sub1,
1056   };
1057   SDNode *RegSequence = CurDAG->getMachineNode(AMDGPU::REG_SEQUENCE, DL,
1058                                                MVT::i64, RegSequenceArgs);
1059 
1060   if (ProduceCarry) {
1061     // Replace the carry-use
1062     ReplaceUses(SDValue(N, 1), SDValue(AddHi, 1));
1063   }
1064 
1065   // Replace the remaining uses.
1066   ReplaceNode(N, RegSequence);
1067 }
1068 
1069 void AMDGPUDAGToDAGISel::SelectAddcSubb(SDNode *N) {
1070   SDLoc DL(N);
1071   SDValue LHS = N->getOperand(0);
1072   SDValue RHS = N->getOperand(1);
1073   SDValue CI = N->getOperand(2);
1074 
1075   if (N->isDivergent()) {
1076     unsigned Opc = N->getOpcode() == ISD::ADDCARRY ? AMDGPU::V_ADDC_U32_e64
1077                                                    : AMDGPU::V_SUBB_U32_e64;
1078     CurDAG->SelectNodeTo(
1079         N, Opc, N->getVTList(),
1080         {LHS, RHS, CI,
1081          CurDAG->getTargetConstant(0, {}, MVT::i1) /*clamp bit*/});
1082   } else {
1083     unsigned Opc = N->getOpcode() == ISD::ADDCARRY ? AMDGPU::S_ADD_CO_PSEUDO
1084                                                    : AMDGPU::S_SUB_CO_PSEUDO;
1085     CurDAG->SelectNodeTo(N, Opc, N->getVTList(), {LHS, RHS, CI});
1086   }
1087 }
1088 
1089 void AMDGPUDAGToDAGISel::SelectUADDO_USUBO(SDNode *N) {
1090   // The name of the opcodes are misleading. v_add_i32/v_sub_i32 have unsigned
1091   // carry out despite the _i32 name. These were renamed in VI to _U32.
1092   // FIXME: We should probably rename the opcodes here.
1093   bool IsAdd = N->getOpcode() == ISD::UADDO;
1094   bool IsVALU = N->isDivergent();
1095 
1096   for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;
1097        ++UI)
1098     if (UI.getUse().getResNo() == 1) {
1099       if ((IsAdd && (UI->getOpcode() != ISD::ADDCARRY)) ||
1100           (!IsAdd && (UI->getOpcode() != ISD::SUBCARRY))) {
1101         IsVALU = true;
1102         break;
1103       }
1104     }
1105 
1106   if (IsVALU) {
1107     unsigned Opc = IsAdd ? AMDGPU::V_ADD_CO_U32_e64 : AMDGPU::V_SUB_CO_U32_e64;
1108 
1109     CurDAG->SelectNodeTo(
1110         N, Opc, N->getVTList(),
1111         {N->getOperand(0), N->getOperand(1),
1112          CurDAG->getTargetConstant(0, {}, MVT::i1) /*clamp bit*/});
1113   } else {
1114     unsigned Opc = N->getOpcode() == ISD::UADDO ? AMDGPU::S_UADDO_PSEUDO
1115                                                 : AMDGPU::S_USUBO_PSEUDO;
1116 
1117     CurDAG->SelectNodeTo(N, Opc, N->getVTList(),
1118                          {N->getOperand(0), N->getOperand(1)});
1119   }
1120 }
1121 
1122 void AMDGPUDAGToDAGISel::SelectFMA_W_CHAIN(SDNode *N) {
1123   SDLoc SL(N);
1124   //  src0_modifiers, src0,  src1_modifiers, src1, src2_modifiers, src2, clamp, omod
1125   SDValue Ops[10];
1126 
1127   SelectVOP3Mods0(N->getOperand(1), Ops[1], Ops[0], Ops[6], Ops[7]);
1128   SelectVOP3Mods(N->getOperand(2), Ops[3], Ops[2]);
1129   SelectVOP3Mods(N->getOperand(3), Ops[5], Ops[4]);
1130   Ops[8] = N->getOperand(0);
1131   Ops[9] = N->getOperand(4);
1132 
1133   CurDAG->SelectNodeTo(N, AMDGPU::V_FMA_F32_e64, N->getVTList(), Ops);
1134 }
1135 
1136 void AMDGPUDAGToDAGISel::SelectFMUL_W_CHAIN(SDNode *N) {
1137   SDLoc SL(N);
1138   //    src0_modifiers, src0,  src1_modifiers, src1, clamp, omod
1139   SDValue Ops[8];
1140 
1141   SelectVOP3Mods0(N->getOperand(1), Ops[1], Ops[0], Ops[4], Ops[5]);
1142   SelectVOP3Mods(N->getOperand(2), Ops[3], Ops[2]);
1143   Ops[6] = N->getOperand(0);
1144   Ops[7] = N->getOperand(3);
1145 
1146   CurDAG->SelectNodeTo(N, AMDGPU::V_MUL_F32_e64, N->getVTList(), Ops);
1147 }
1148 
1149 // We need to handle this here because tablegen doesn't support matching
1150 // instructions with multiple outputs.
1151 void AMDGPUDAGToDAGISel::SelectDIV_SCALE(SDNode *N) {
1152   SDLoc SL(N);
1153   EVT VT = N->getValueType(0);
1154 
1155   assert(VT == MVT::f32 || VT == MVT::f64);
1156 
1157   unsigned Opc
1158     = (VT == MVT::f64) ? AMDGPU::V_DIV_SCALE_F64_e64 : AMDGPU::V_DIV_SCALE_F32_e64;
1159 
1160   // src0_modifiers, src0, src1_modifiers, src1, src2_modifiers, src2, clamp,
1161   // omod
1162   SDValue Ops[8];
1163   SelectVOP3BMods0(N->getOperand(0), Ops[1], Ops[0], Ops[6], Ops[7]);
1164   SelectVOP3BMods(N->getOperand(1), Ops[3], Ops[2]);
1165   SelectVOP3BMods(N->getOperand(2), Ops[5], Ops[4]);
1166   CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops);
1167 }
1168 
1169 // We need to handle this here because tablegen doesn't support matching
1170 // instructions with multiple outputs.
1171 void AMDGPUDAGToDAGISel::SelectMAD_64_32(SDNode *N) {
1172   SDLoc SL(N);
1173   bool Signed = N->getOpcode() == AMDGPUISD::MAD_I64_I32;
1174   unsigned Opc = Signed ? AMDGPU::V_MAD_I64_I32_e64 : AMDGPU::V_MAD_U64_U32_e64;
1175 
1176   SDValue Clamp = CurDAG->getTargetConstant(0, SL, MVT::i1);
1177   SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
1178                     Clamp };
1179   CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops);
1180 }
1181 
1182 bool AMDGPUDAGToDAGISel::isDSOffsetLegal(SDValue Base, unsigned Offset) const {
1183   if (!isUInt<16>(Offset))
1184     return false;
1185 
1186   if (!Base || Subtarget->hasUsableDSOffset() ||
1187       Subtarget->unsafeDSOffsetFoldingEnabled())
1188     return true;
1189 
1190   // On Southern Islands instruction with a negative base value and an offset
1191   // don't seem to work.
1192   return CurDAG->SignBitIsZero(Base);
1193 }
1194 
1195 bool AMDGPUDAGToDAGISel::SelectDS1Addr1Offset(SDValue Addr, SDValue &Base,
1196                                               SDValue &Offset) const {
1197   SDLoc DL(Addr);
1198   if (CurDAG->isBaseWithConstantOffset(Addr)) {
1199     SDValue N0 = Addr.getOperand(0);
1200     SDValue N1 = Addr.getOperand(1);
1201     ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
1202     if (isDSOffsetLegal(N0, C1->getSExtValue())) {
1203       // (add n0, c0)
1204       Base = N0;
1205       Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16);
1206       return true;
1207     }
1208   } else if (Addr.getOpcode() == ISD::SUB) {
1209     // sub C, x -> add (sub 0, x), C
1210     if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr.getOperand(0))) {
1211       int64_t ByteOffset = C->getSExtValue();
1212       if (isDSOffsetLegal(SDValue(), ByteOffset)) {
1213         SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
1214 
1215         // XXX - This is kind of hacky. Create a dummy sub node so we can check
1216         // the known bits in isDSOffsetLegal. We need to emit the selected node
1217         // here, so this is thrown away.
1218         SDValue Sub = CurDAG->getNode(ISD::SUB, DL, MVT::i32,
1219                                       Zero, Addr.getOperand(1));
1220 
1221         if (isDSOffsetLegal(Sub, ByteOffset)) {
1222           SmallVector<SDValue, 3> Opnds;
1223           Opnds.push_back(Zero);
1224           Opnds.push_back(Addr.getOperand(1));
1225 
1226           // FIXME: Select to VOP3 version for with-carry.
1227           unsigned SubOp = AMDGPU::V_SUB_CO_U32_e32;
1228           if (Subtarget->hasAddNoCarry()) {
1229             SubOp = AMDGPU::V_SUB_U32_e64;
1230             Opnds.push_back(
1231                 CurDAG->getTargetConstant(0, {}, MVT::i1)); // clamp bit
1232           }
1233 
1234           MachineSDNode *MachineSub =
1235               CurDAG->getMachineNode(SubOp, DL, MVT::i32, Opnds);
1236 
1237           Base = SDValue(MachineSub, 0);
1238           Offset = CurDAG->getTargetConstant(ByteOffset, DL, MVT::i16);
1239           return true;
1240         }
1241       }
1242     }
1243   } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) {
1244     // If we have a constant address, prefer to put the constant into the
1245     // offset. This can save moves to load the constant address since multiple
1246     // operations can share the zero base address register, and enables merging
1247     // into read2 / write2 instructions.
1248 
1249     SDLoc DL(Addr);
1250 
1251     if (isDSOffsetLegal(SDValue(), CAddr->getZExtValue())) {
1252       SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
1253       MachineSDNode *MovZero = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32,
1254                                  DL, MVT::i32, Zero);
1255       Base = SDValue(MovZero, 0);
1256       Offset = CurDAG->getTargetConstant(CAddr->getZExtValue(), DL, MVT::i16);
1257       return true;
1258     }
1259   }
1260 
1261   // default case
1262   Base = Addr;
1263   Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i16);
1264   return true;
1265 }
1266 
1267 bool AMDGPUDAGToDAGISel::isDSOffset2Legal(SDValue Base, unsigned Offset0,
1268                                           unsigned Offset1,
1269                                           unsigned Size) const {
1270   if (Offset0 % Size != 0 || Offset1 % Size != 0)
1271     return false;
1272   if (!isUInt<8>(Offset0 / Size) || !isUInt<8>(Offset1 / Size))
1273     return false;
1274 
1275   if (!Base || Subtarget->hasUsableDSOffset() ||
1276       Subtarget->unsafeDSOffsetFoldingEnabled())
1277     return true;
1278 
1279   // On Southern Islands instruction with a negative base value and an offset
1280   // don't seem to work.
1281   return CurDAG->SignBitIsZero(Base);
1282 }
1283 
1284 // TODO: If offset is too big, put low 16-bit into offset.
1285 bool AMDGPUDAGToDAGISel::SelectDS64Bit4ByteAligned(SDValue Addr, SDValue &Base,
1286                                                    SDValue &Offset0,
1287                                                    SDValue &Offset1) const {
1288   return SelectDSReadWrite2(Addr, Base, Offset0, Offset1, 4);
1289 }
1290 
1291 bool AMDGPUDAGToDAGISel::SelectDS128Bit8ByteAligned(SDValue Addr, SDValue &Base,
1292                                                     SDValue &Offset0,
1293                                                     SDValue &Offset1) const {
1294   return SelectDSReadWrite2(Addr, Base, Offset0, Offset1, 8);
1295 }
1296 
1297 bool AMDGPUDAGToDAGISel::SelectDSReadWrite2(SDValue Addr, SDValue &Base,
1298                                             SDValue &Offset0, SDValue &Offset1,
1299                                             unsigned Size) const {
1300   SDLoc DL(Addr);
1301 
1302   if (CurDAG->isBaseWithConstantOffset(Addr)) {
1303     SDValue N0 = Addr.getOperand(0);
1304     SDValue N1 = Addr.getOperand(1);
1305     ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
1306     unsigned OffsetValue0 = C1->getZExtValue();
1307     unsigned OffsetValue1 = OffsetValue0 + Size;
1308 
1309     // (add n0, c0)
1310     if (isDSOffset2Legal(N0, OffsetValue0, OffsetValue1, Size)) {
1311       Base = N0;
1312       Offset0 = CurDAG->getTargetConstant(OffsetValue0 / Size, DL, MVT::i8);
1313       Offset1 = CurDAG->getTargetConstant(OffsetValue1 / Size, DL, MVT::i8);
1314       return true;
1315     }
1316   } else if (Addr.getOpcode() == ISD::SUB) {
1317     // sub C, x -> add (sub 0, x), C
1318     if (const ConstantSDNode *C =
1319             dyn_cast<ConstantSDNode>(Addr.getOperand(0))) {
1320       unsigned OffsetValue0 = C->getZExtValue();
1321       unsigned OffsetValue1 = OffsetValue0 + Size;
1322 
1323       if (isDSOffset2Legal(SDValue(), OffsetValue0, OffsetValue1, Size)) {
1324         SDLoc DL(Addr);
1325         SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
1326 
1327         // XXX - This is kind of hacky. Create a dummy sub node so we can check
1328         // the known bits in isDSOffsetLegal. We need to emit the selected node
1329         // here, so this is thrown away.
1330         SDValue Sub =
1331             CurDAG->getNode(ISD::SUB, DL, MVT::i32, Zero, Addr.getOperand(1));
1332 
1333         if (isDSOffset2Legal(Sub, OffsetValue0, OffsetValue1, Size)) {
1334           SmallVector<SDValue, 3> Opnds;
1335           Opnds.push_back(Zero);
1336           Opnds.push_back(Addr.getOperand(1));
1337           unsigned SubOp = AMDGPU::V_SUB_CO_U32_e32;
1338           if (Subtarget->hasAddNoCarry()) {
1339             SubOp = AMDGPU::V_SUB_U32_e64;
1340             Opnds.push_back(
1341                 CurDAG->getTargetConstant(0, {}, MVT::i1)); // clamp bit
1342           }
1343 
1344           MachineSDNode *MachineSub = CurDAG->getMachineNode(
1345               SubOp, DL, MVT::getIntegerVT(Size * 8), Opnds);
1346 
1347           Base = SDValue(MachineSub, 0);
1348           Offset0 = CurDAG->getTargetConstant(OffsetValue0 / Size, DL, MVT::i8);
1349           Offset1 = CurDAG->getTargetConstant(OffsetValue1 / Size, DL, MVT::i8);
1350           return true;
1351         }
1352       }
1353     }
1354   } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) {
1355     unsigned OffsetValue0 = CAddr->getZExtValue();
1356     unsigned OffsetValue1 = OffsetValue0 + Size;
1357 
1358     if (isDSOffset2Legal(SDValue(), OffsetValue0, OffsetValue1, Size)) {
1359       SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
1360       MachineSDNode *MovZero =
1361           CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32, DL, MVT::i32, Zero);
1362       Base = SDValue(MovZero, 0);
1363       Offset0 = CurDAG->getTargetConstant(OffsetValue0 / Size, DL, MVT::i8);
1364       Offset1 = CurDAG->getTargetConstant(OffsetValue1 / Size, DL, MVT::i8);
1365       return true;
1366     }
1367   }
1368 
1369   // default case
1370 
1371   Base = Addr;
1372   Offset0 = CurDAG->getTargetConstant(0, DL, MVT::i8);
1373   Offset1 = CurDAG->getTargetConstant(1, DL, MVT::i8);
1374   return true;
1375 }
1376 
1377 bool AMDGPUDAGToDAGISel::SelectMUBUF(SDValue Addr, SDValue &Ptr,
1378                                      SDValue &VAddr, SDValue &SOffset,
1379                                      SDValue &Offset, SDValue &Offen,
1380                                      SDValue &Idxen, SDValue &Addr64,
1381                                      SDValue &GLC, SDValue &SLC,
1382                                      SDValue &TFE, SDValue &DLC,
1383                                      SDValue &SWZ) const {
1384   // Subtarget prefers to use flat instruction
1385   // FIXME: This should be a pattern predicate and not reach here
1386   if (Subtarget->useFlatForGlobal())
1387     return false;
1388 
1389   SDLoc DL(Addr);
1390 
1391   if (!GLC.getNode())
1392     GLC = CurDAG->getTargetConstant(0, DL, MVT::i1);
1393   if (!SLC.getNode())
1394     SLC = CurDAG->getTargetConstant(0, DL, MVT::i1);
1395   TFE = CurDAG->getTargetConstant(0, DL, MVT::i1);
1396   DLC = CurDAG->getTargetConstant(0, DL, MVT::i1);
1397   SWZ = CurDAG->getTargetConstant(0, DL, MVT::i1);
1398 
1399   Idxen = CurDAG->getTargetConstant(0, DL, MVT::i1);
1400   Offen = CurDAG->getTargetConstant(0, DL, MVT::i1);
1401   Addr64 = CurDAG->getTargetConstant(0, DL, MVT::i1);
1402   SOffset = CurDAG->getTargetConstant(0, DL, MVT::i32);
1403 
1404   ConstantSDNode *C1 = nullptr;
1405   SDValue N0 = Addr;
1406   if (CurDAG->isBaseWithConstantOffset(Addr)) {
1407     C1 = cast<ConstantSDNode>(Addr.getOperand(1));
1408     if (isUInt<32>(C1->getZExtValue()))
1409       N0 = Addr.getOperand(0);
1410     else
1411       C1 = nullptr;
1412   }
1413 
1414   if (N0.getOpcode() == ISD::ADD) {
1415     // (add N2, N3) -> addr64, or
1416     // (add (add N2, N3), C1) -> addr64
1417     SDValue N2 = N0.getOperand(0);
1418     SDValue N3 = N0.getOperand(1);
1419     Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1);
1420 
1421     if (N2->isDivergent()) {
1422       if (N3->isDivergent()) {
1423         // Both N2 and N3 are divergent. Use N0 (the result of the add) as the
1424         // addr64, and construct the resource from a 0 address.
1425         Ptr = SDValue(buildSMovImm64(DL, 0, MVT::v2i32), 0);
1426         VAddr = N0;
1427       } else {
1428         // N2 is divergent, N3 is not.
1429         Ptr = N3;
1430         VAddr = N2;
1431       }
1432     } else {
1433       // N2 is not divergent.
1434       Ptr = N2;
1435       VAddr = N3;
1436     }
1437     Offset = CurDAG->getTargetConstant(0, DL, MVT::i16);
1438   } else if (N0->isDivergent()) {
1439     // N0 is divergent. Use it as the addr64, and construct the resource from a
1440     // 0 address.
1441     Ptr = SDValue(buildSMovImm64(DL, 0, MVT::v2i32), 0);
1442     VAddr = N0;
1443     Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1);
1444   } else {
1445     // N0 -> offset, or
1446     // (N0 + C1) -> offset
1447     VAddr = CurDAG->getTargetConstant(0, DL, MVT::i32);
1448     Ptr = N0;
1449   }
1450 
1451   if (!C1) {
1452     // No offset.
1453     Offset = CurDAG->getTargetConstant(0, DL, MVT::i16);
1454     return true;
1455   }
1456 
1457   if (SIInstrInfo::isLegalMUBUFImmOffset(C1->getZExtValue())) {
1458     // Legal offset for instruction.
1459     Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16);
1460     return true;
1461   }
1462 
1463   // Illegal offset, store it in soffset.
1464   Offset = CurDAG->getTargetConstant(0, DL, MVT::i16);
1465   SOffset =
1466       SDValue(CurDAG->getMachineNode(
1467                   AMDGPU::S_MOV_B32, DL, MVT::i32,
1468                   CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i32)),
1469               0);
1470   return true;
1471 }
1472 
1473 bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc,
1474                                            SDValue &VAddr, SDValue &SOffset,
1475                                            SDValue &Offset, SDValue &GLC,
1476                                            SDValue &SLC, SDValue &TFE,
1477                                            SDValue &DLC, SDValue &SWZ) const {
1478   SDValue Ptr, Offen, Idxen, Addr64;
1479 
1480   // addr64 bit was removed for volcanic islands.
1481   // FIXME: This should be a pattern predicate and not reach here
1482   if (!Subtarget->hasAddr64())
1483     return false;
1484 
1485   if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64,
1486               GLC, SLC, TFE, DLC, SWZ))
1487     return false;
1488 
1489   ConstantSDNode *C = cast<ConstantSDNode>(Addr64);
1490   if (C->getSExtValue()) {
1491     SDLoc DL(Addr);
1492 
1493     const SITargetLowering& Lowering =
1494       *static_cast<const SITargetLowering*>(getTargetLowering());
1495 
1496     SRsrc = SDValue(Lowering.wrapAddr64Rsrc(*CurDAG, DL, Ptr), 0);
1497     return true;
1498   }
1499 
1500   return false;
1501 }
1502 
1503 bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc,
1504                                            SDValue &VAddr, SDValue &SOffset,
1505                                            SDValue &Offset,
1506                                            SDValue &SLC) const {
1507   SLC = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i1);
1508   SDValue GLC, TFE, DLC, SWZ;
1509 
1510   return SelectMUBUFAddr64(Addr, SRsrc, VAddr, SOffset, Offset, GLC, SLC, TFE, DLC, SWZ);
1511 }
1512 
1513 static bool isStackPtrRelative(const MachinePointerInfo &PtrInfo) {
1514   auto PSV = PtrInfo.V.dyn_cast<const PseudoSourceValue *>();
1515   return PSV && PSV->isStack();
1516 }
1517 
1518 std::pair<SDValue, SDValue> AMDGPUDAGToDAGISel::foldFrameIndex(SDValue N) const {
1519   SDLoc DL(N);
1520 
1521   auto *FI = dyn_cast<FrameIndexSDNode>(N);
1522   SDValue TFI =
1523       FI ? CurDAG->getTargetFrameIndex(FI->getIndex(), FI->getValueType(0)) : N;
1524 
1525   // We rebase the base address into an absolute stack address and hence
1526   // use constant 0 for soffset.
1527   return std::make_pair(TFI, CurDAG->getTargetConstant(0, DL, MVT::i32));
1528 }
1529 
1530 bool AMDGPUDAGToDAGISel::SelectMUBUFScratchOffen(SDNode *Parent,
1531                                                  SDValue Addr, SDValue &Rsrc,
1532                                                  SDValue &VAddr, SDValue &SOffset,
1533                                                  SDValue &ImmOffset) const {
1534 
1535   SDLoc DL(Addr);
1536   MachineFunction &MF = CurDAG->getMachineFunction();
1537   const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1538 
1539   Rsrc = CurDAG->getRegister(Info->getScratchRSrcReg(), MVT::v4i32);
1540 
1541   if (ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) {
1542     int64_t Imm = CAddr->getSExtValue();
1543     const int64_t NullPtr =
1544         AMDGPUTargetMachine::getNullPointerValue(AMDGPUAS::PRIVATE_ADDRESS);
1545     // Don't fold null pointer.
1546     if (Imm != NullPtr) {
1547       SDValue HighBits = CurDAG->getTargetConstant(Imm & ~4095, DL, MVT::i32);
1548       MachineSDNode *MovHighBits = CurDAG->getMachineNode(
1549         AMDGPU::V_MOV_B32_e32, DL, MVT::i32, HighBits);
1550       VAddr = SDValue(MovHighBits, 0);
1551 
1552       // In a call sequence, stores to the argument stack area are relative to the
1553       // stack pointer.
1554       const MachinePointerInfo &PtrInfo
1555         = cast<MemSDNode>(Parent)->getPointerInfo();
1556       SOffset = isStackPtrRelative(PtrInfo)
1557         ? CurDAG->getRegister(Info->getStackPtrOffsetReg(), MVT::i32)
1558         : CurDAG->getTargetConstant(0, DL, MVT::i32);
1559       ImmOffset = CurDAG->getTargetConstant(Imm & 4095, DL, MVT::i16);
1560       return true;
1561     }
1562   }
1563 
1564   if (CurDAG->isBaseWithConstantOffset(Addr)) {
1565     // (add n0, c1)
1566 
1567     SDValue N0 = Addr.getOperand(0);
1568     SDValue N1 = Addr.getOperand(1);
1569 
1570     // Offsets in vaddr must be positive if range checking is enabled.
1571     //
1572     // The total computation of vaddr + soffset + offset must not overflow.  If
1573     // vaddr is negative, even if offset is 0 the sgpr offset add will end up
1574     // overflowing.
1575     //
1576     // Prior to gfx9, MUBUF instructions with the vaddr offset enabled would
1577     // always perform a range check. If a negative vaddr base index was used,
1578     // this would fail the range check. The overall address computation would
1579     // compute a valid address, but this doesn't happen due to the range
1580     // check. For out-of-bounds MUBUF loads, a 0 is returned.
1581     //
1582     // Therefore it should be safe to fold any VGPR offset on gfx9 into the
1583     // MUBUF vaddr, but not on older subtargets which can only do this if the
1584     // sign bit is known 0.
1585     ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
1586     if (SIInstrInfo::isLegalMUBUFImmOffset(C1->getZExtValue()) &&
1587         (!Subtarget->privateMemoryResourceIsRangeChecked() ||
1588          CurDAG->SignBitIsZero(N0))) {
1589       std::tie(VAddr, SOffset) = foldFrameIndex(N0);
1590       ImmOffset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16);
1591       return true;
1592     }
1593   }
1594 
1595   // (node)
1596   std::tie(VAddr, SOffset) = foldFrameIndex(Addr);
1597   ImmOffset = CurDAG->getTargetConstant(0, DL, MVT::i16);
1598   return true;
1599 }
1600 
1601 bool AMDGPUDAGToDAGISel::SelectMUBUFScratchOffset(SDNode *Parent,
1602                                                   SDValue Addr,
1603                                                   SDValue &SRsrc,
1604                                                   SDValue &SOffset,
1605                                                   SDValue &Offset) const {
1606   ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr);
1607   if (!CAddr || !SIInstrInfo::isLegalMUBUFImmOffset(CAddr->getZExtValue()))
1608     return false;
1609 
1610   SDLoc DL(Addr);
1611   MachineFunction &MF = CurDAG->getMachineFunction();
1612   const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1613 
1614   SRsrc = CurDAG->getRegister(Info->getScratchRSrcReg(), MVT::v4i32);
1615 
1616   const MachinePointerInfo &PtrInfo = cast<MemSDNode>(Parent)->getPointerInfo();
1617 
1618   // FIXME: Get from MachinePointerInfo? We should only be using the frame
1619   // offset if we know this is in a call sequence.
1620   SOffset = isStackPtrRelative(PtrInfo)
1621                 ? CurDAG->getRegister(Info->getStackPtrOffsetReg(), MVT::i32)
1622                 : CurDAG->getTargetConstant(0, DL, MVT::i32);
1623 
1624   Offset = CurDAG->getTargetConstant(CAddr->getZExtValue(), DL, MVT::i16);
1625   return true;
1626 }
1627 
1628 bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc,
1629                                            SDValue &SOffset, SDValue &Offset,
1630                                            SDValue &GLC, SDValue &SLC,
1631                                            SDValue &TFE, SDValue &DLC,
1632                                            SDValue &SWZ) const {
1633   SDValue Ptr, VAddr, Offen, Idxen, Addr64;
1634   const SIInstrInfo *TII =
1635     static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
1636 
1637   if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64,
1638               GLC, SLC, TFE, DLC, SWZ))
1639     return false;
1640 
1641   if (!cast<ConstantSDNode>(Offen)->getSExtValue() &&
1642       !cast<ConstantSDNode>(Idxen)->getSExtValue() &&
1643       !cast<ConstantSDNode>(Addr64)->getSExtValue()) {
1644     uint64_t Rsrc = TII->getDefaultRsrcDataFormat() |
1645                     APInt::getAllOnesValue(32).getZExtValue(); // Size
1646     SDLoc DL(Addr);
1647 
1648     const SITargetLowering& Lowering =
1649       *static_cast<const SITargetLowering*>(getTargetLowering());
1650 
1651     SRsrc = SDValue(Lowering.buildRSRC(*CurDAG, DL, Ptr, 0, Rsrc), 0);
1652     return true;
1653   }
1654   return false;
1655 }
1656 
1657 bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc,
1658                                            SDValue &Soffset, SDValue &Offset
1659                                            ) const {
1660   SDValue GLC, SLC, TFE, DLC, SWZ;
1661 
1662   return SelectMUBUFOffset(Addr, SRsrc, Soffset, Offset, GLC, SLC, TFE, DLC, SWZ);
1663 }
1664 bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc,
1665                                            SDValue &Soffset, SDValue &Offset,
1666                                            SDValue &SLC) const {
1667   SDValue GLC, TFE, DLC, SWZ;
1668 
1669   return SelectMUBUFOffset(Addr, SRsrc, Soffset, Offset, GLC, SLC, TFE, DLC, SWZ);
1670 }
1671 
1672 // Find a load or store from corresponding pattern root.
1673 // Roots may be build_vector, bitconvert or their combinations.
1674 static MemSDNode* findMemSDNode(SDNode *N) {
1675   N = AMDGPUTargetLowering::stripBitcast(SDValue(N,0)).getNode();
1676   if (MemSDNode *MN = dyn_cast<MemSDNode>(N))
1677     return MN;
1678   assert(isa<BuildVectorSDNode>(N));
1679   for (SDValue V : N->op_values())
1680     if (MemSDNode *MN =
1681           dyn_cast<MemSDNode>(AMDGPUTargetLowering::stripBitcast(V)))
1682       return MN;
1683   llvm_unreachable("cannot find MemSDNode in the pattern!");
1684 }
1685 
1686 template <bool IsSigned>
1687 bool AMDGPUDAGToDAGISel::SelectFlatOffset(SDNode *N,
1688                                           SDValue Addr,
1689                                           SDValue &VAddr,
1690                                           SDValue &Offset) const {
1691   int64_t OffsetVal = 0;
1692 
1693   unsigned AS = findMemSDNode(N)->getAddressSpace();
1694 
1695   if (Subtarget->hasFlatInstOffsets() &&
1696       (!Subtarget->hasFlatSegmentOffsetBug() ||
1697        AS != AMDGPUAS::FLAT_ADDRESS)) {
1698     SDValue N0, N1;
1699     if (isBaseWithConstantOffset64(Addr, N0, N1)) {
1700       uint64_t COffsetVal = cast<ConstantSDNode>(N1)->getSExtValue();
1701 
1702       const SIInstrInfo *TII = Subtarget->getInstrInfo();
1703       if (TII->isLegalFLATOffset(COffsetVal, AS, IsSigned)) {
1704         Addr = N0;
1705         OffsetVal = COffsetVal;
1706       } else {
1707         // If the offset doesn't fit, put the low bits into the offset field and
1708         // add the rest.
1709         //
1710         // For a FLAT instruction the hardware decides whether to access
1711         // global/scratch/shared memory based on the high bits of vaddr,
1712         // ignoring the offset field, so we have to ensure that when we add
1713         // remainder to vaddr it still points into the same underlying object.
1714         // The easiest way to do that is to make sure that we split the offset
1715         // into two pieces that are both >= 0 or both <= 0.
1716 
1717         SDLoc DL(N);
1718         uint64_t RemainderOffset;
1719 
1720         std::tie(OffsetVal, RemainderOffset)
1721           = TII->splitFlatOffset(COffsetVal, AS, IsSigned);
1722 
1723         SDValue AddOffsetLo =
1724             getMaterializedScalarImm32(Lo_32(RemainderOffset), DL);
1725         SDValue Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1);
1726 
1727         if (Addr.getValueType().getSizeInBits() == 32) {
1728           SmallVector<SDValue, 3> Opnds;
1729           Opnds.push_back(N0);
1730           Opnds.push_back(AddOffsetLo);
1731           unsigned AddOp = AMDGPU::V_ADD_CO_U32_e32;
1732           if (Subtarget->hasAddNoCarry()) {
1733             AddOp = AMDGPU::V_ADD_U32_e64;
1734             Opnds.push_back(Clamp);
1735           }
1736           Addr = SDValue(CurDAG->getMachineNode(AddOp, DL, MVT::i32, Opnds), 0);
1737         } else {
1738           // TODO: Should this try to use a scalar add pseudo if the base address
1739           // is uniform and saddr is usable?
1740           SDValue Sub0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32);
1741           SDValue Sub1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32);
1742 
1743           SDNode *N0Lo = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
1744                                                 DL, MVT::i32, N0, Sub0);
1745           SDNode *N0Hi = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
1746                                                 DL, MVT::i32, N0, Sub1);
1747 
1748           SDValue AddOffsetHi =
1749               getMaterializedScalarImm32(Hi_32(RemainderOffset), DL);
1750 
1751           SDVTList VTs = CurDAG->getVTList(MVT::i32, MVT::i1);
1752 
1753           SDNode *Add =
1754               CurDAG->getMachineNode(AMDGPU::V_ADD_CO_U32_e64, DL, VTs,
1755                                      {AddOffsetLo, SDValue(N0Lo, 0), Clamp});
1756 
1757           SDNode *Addc = CurDAG->getMachineNode(
1758               AMDGPU::V_ADDC_U32_e64, DL, VTs,
1759               {AddOffsetHi, SDValue(N0Hi, 0), SDValue(Add, 1), Clamp});
1760 
1761           SDValue RegSequenceArgs[] = {
1762               CurDAG->getTargetConstant(AMDGPU::VReg_64RegClassID, DL, MVT::i32),
1763               SDValue(Add, 0), Sub0, SDValue(Addc, 0), Sub1};
1764 
1765           Addr = SDValue(CurDAG->getMachineNode(AMDGPU::REG_SEQUENCE, DL,
1766                                                 MVT::i64, RegSequenceArgs),
1767                          0);
1768         }
1769       }
1770     }
1771   }
1772 
1773   VAddr = Addr;
1774   Offset = CurDAG->getTargetConstant(OffsetVal, SDLoc(), MVT::i16);
1775   return true;
1776 }
1777 
1778 // If this matches zero_extend i32:x, return x
1779 static SDValue matchZExtFromI32(SDValue Op) {
1780   if (Op.getOpcode() != ISD::ZERO_EXTEND)
1781     return SDValue();
1782 
1783   SDValue ExtSrc = Op.getOperand(0);
1784   return (ExtSrc.getValueType() == MVT::i32) ? ExtSrc : SDValue();
1785 }
1786 
1787 // Match (64-bit SGPR base) + (zext vgpr offset) + sext(imm offset)
1788 bool AMDGPUDAGToDAGISel::SelectGlobalSAddr(SDNode *N,
1789                                            SDValue Addr,
1790                                            SDValue &SAddr,
1791                                            SDValue &VOffset,
1792                                            SDValue &Offset) const {
1793   int64_t ImmOffset = 0;
1794 
1795   // Match the immediate offset first, which canonically is moved as low as
1796   // possible.
1797 
1798   SDValue LHS, RHS;
1799   if (isBaseWithConstantOffset64(Addr, LHS, RHS)) {
1800     int64_t COffsetVal = cast<ConstantSDNode>(RHS)->getSExtValue();
1801     const SIInstrInfo *TII = Subtarget->getInstrInfo();
1802 
1803     if (TII->isLegalFLATOffset(COffsetVal, AMDGPUAS::GLOBAL_ADDRESS, true)) {
1804       Addr = LHS;
1805       ImmOffset = COffsetVal;
1806     } else if (!LHS->isDivergent() && COffsetVal > 0) {
1807       SDLoc SL(N);
1808       // saddr + large_offset -> saddr + (voffset = large_offset & ~MaxOffset) +
1809       //                         (large_offset & MaxOffset);
1810       int64_t SplitImmOffset, RemainderOffset;
1811       std::tie(SplitImmOffset, RemainderOffset)
1812         = TII->splitFlatOffset(COffsetVal, AMDGPUAS::GLOBAL_ADDRESS, true);
1813 
1814       if (isUInt<32>(RemainderOffset)) {
1815         SDNode *VMov = CurDAG->getMachineNode(
1816           AMDGPU::V_MOV_B32_e32, SL, MVT::i32,
1817           CurDAG->getTargetConstant(RemainderOffset, SDLoc(), MVT::i32));
1818         VOffset = SDValue(VMov, 0);
1819         SAddr = LHS;
1820         Offset = CurDAG->getTargetConstant(SplitImmOffset, SDLoc(), MVT::i16);
1821         return true;
1822       }
1823     }
1824   }
1825 
1826   // Match the variable offset.
1827   if (Addr.getOpcode() != ISD::ADD) {
1828     if (Addr->isDivergent() || Addr.getOpcode() == ISD::UNDEF ||
1829         isa<ConstantSDNode>(Addr))
1830       return false;
1831 
1832     // It's cheaper to materialize a single 32-bit zero for vaddr than the two
1833     // moves required to copy a 64-bit SGPR to VGPR.
1834     SAddr = Addr;
1835     SDNode *VMov = CurDAG->getMachineNode(
1836       AMDGPU::V_MOV_B32_e32, SDLoc(Addr), MVT::i32,
1837       CurDAG->getTargetConstant(0, SDLoc(), MVT::i32));
1838     VOffset = SDValue(VMov, 0);
1839     Offset = CurDAG->getTargetConstant(ImmOffset, SDLoc(), MVT::i16);
1840     return true;
1841   }
1842 
1843   LHS = Addr.getOperand(0);
1844   RHS = Addr.getOperand(1);
1845 
1846   if (!LHS->isDivergent()) {
1847     // add (i64 sgpr), (zero_extend (i32 vgpr))
1848     if (SDValue ZextRHS = matchZExtFromI32(RHS)) {
1849       SAddr = LHS;
1850       VOffset = ZextRHS;
1851     }
1852   }
1853 
1854   if (!SAddr && !RHS->isDivergent()) {
1855     // add (zero_extend (i32 vgpr)), (i64 sgpr)
1856     if (SDValue ZextLHS = matchZExtFromI32(LHS)) {
1857       SAddr = RHS;
1858       VOffset = ZextLHS;
1859     }
1860   }
1861 
1862   if (!SAddr)
1863     return false;
1864 
1865   Offset = CurDAG->getTargetConstant(ImmOffset, SDLoc(), MVT::i16);
1866   return true;
1867 }
1868 
1869 // Match (32-bit SGPR base) + sext(imm offset)
1870 bool AMDGPUDAGToDAGISel::SelectScratchSAddr(SDNode *N,
1871                                             SDValue Addr,
1872                                             SDValue &SAddr,
1873                                             SDValue &Offset) const {
1874   if (Addr->isDivergent())
1875     return false;
1876 
1877   SAddr = Addr;
1878   int64_t COffsetVal = 0;
1879 
1880   if (CurDAG->isBaseWithConstantOffset(Addr)) {
1881     COffsetVal = cast<ConstantSDNode>(Addr.getOperand(1))->getSExtValue();
1882     SAddr = Addr.getOperand(0);
1883   }
1884 
1885   if (auto FI = dyn_cast<FrameIndexSDNode>(SAddr)) {
1886     SAddr = CurDAG->getTargetFrameIndex(FI->getIndex(), FI->getValueType(0));
1887   } else if (SAddr.getOpcode() == ISD::ADD &&
1888              isa<FrameIndexSDNode>(SAddr.getOperand(0))) {
1889     // Materialize this into a scalar move for scalar address to avoid
1890     // readfirstlane.
1891     auto FI = cast<FrameIndexSDNode>(SAddr.getOperand(0));
1892     SDValue TFI = CurDAG->getTargetFrameIndex(FI->getIndex(),
1893                                               FI->getValueType(0));
1894     SAddr = SDValue(CurDAG->getMachineNode(AMDGPU::S_ADD_U32, SDLoc(SAddr),
1895                                            MVT::i32, TFI, SAddr.getOperand(1)),
1896                     0);
1897   }
1898 
1899   const SIInstrInfo *TII = Subtarget->getInstrInfo();
1900 
1901   if (!TII->isLegalFLATOffset(COffsetVal, AMDGPUAS::PRIVATE_ADDRESS, true)) {
1902     int64_t RemainderOffset = COffsetVal;
1903     int64_t ImmField = 0;
1904     const unsigned NumBits = AMDGPU::getNumFlatOffsetBits(*Subtarget, true);
1905     // Use signed division by a power of two to truncate towards 0.
1906     int64_t D = 1LL << (NumBits - 1);
1907     RemainderOffset = (COffsetVal / D) * D;
1908     ImmField = COffsetVal - RemainderOffset;
1909 
1910     assert(TII->isLegalFLATOffset(ImmField, AMDGPUAS::PRIVATE_ADDRESS, true));
1911     assert(RemainderOffset + ImmField == COffsetVal);
1912 
1913     COffsetVal = ImmField;
1914 
1915     SDLoc DL(N);
1916     SDValue AddOffset =
1917         getMaterializedScalarImm32(Lo_32(RemainderOffset), DL);
1918     SAddr = SDValue(CurDAG->getMachineNode(AMDGPU::S_ADD_U32, DL, MVT::i32,
1919                                            SAddr, AddOffset), 0);
1920   }
1921 
1922   Offset = CurDAG->getTargetConstant(COffsetVal, SDLoc(), MVT::i16);
1923 
1924   return true;
1925 }
1926 
1927 bool AMDGPUDAGToDAGISel::SelectSMRDOffset(SDValue ByteOffsetNode,
1928                                           SDValue &Offset, bool &Imm) const {
1929   ConstantSDNode *C = dyn_cast<ConstantSDNode>(ByteOffsetNode);
1930   if (!C) {
1931     if (ByteOffsetNode.getValueType().isScalarInteger() &&
1932         ByteOffsetNode.getValueType().getSizeInBits() == 32) {
1933       Offset = ByteOffsetNode;
1934       Imm = false;
1935       return true;
1936     }
1937     if (ByteOffsetNode.getOpcode() == ISD::ZERO_EXTEND) {
1938       if (ByteOffsetNode.getOperand(0).getValueType().getSizeInBits() == 32) {
1939         Offset = ByteOffsetNode.getOperand(0);
1940         Imm = false;
1941         return true;
1942       }
1943     }
1944     return false;
1945   }
1946 
1947   SDLoc SL(ByteOffsetNode);
1948   // GFX9 and GFX10 have signed byte immediate offsets.
1949   int64_t ByteOffset = C->getSExtValue();
1950   Optional<int64_t> EncodedOffset =
1951       AMDGPU::getSMRDEncodedOffset(*Subtarget, ByteOffset, false);
1952   if (EncodedOffset) {
1953     Offset = CurDAG->getTargetConstant(*EncodedOffset, SL, MVT::i32);
1954     Imm = true;
1955     return true;
1956   }
1957 
1958   // SGPR and literal offsets are unsigned.
1959   if (ByteOffset < 0)
1960     return false;
1961 
1962   EncodedOffset = AMDGPU::getSMRDEncodedLiteralOffset32(*Subtarget, ByteOffset);
1963   if (EncodedOffset) {
1964     Offset = CurDAG->getTargetConstant(*EncodedOffset, SL, MVT::i32);
1965     return true;
1966   }
1967 
1968   if (!isUInt<32>(ByteOffset) && !isInt<32>(ByteOffset))
1969     return false;
1970 
1971   SDValue C32Bit = CurDAG->getTargetConstant(ByteOffset, SL, MVT::i32);
1972   Offset = SDValue(
1973       CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SL, MVT::i32, C32Bit), 0);
1974 
1975   return true;
1976 }
1977 
1978 SDValue AMDGPUDAGToDAGISel::Expand32BitAddress(SDValue Addr) const {
1979   if (Addr.getValueType() != MVT::i32)
1980     return Addr;
1981 
1982   // Zero-extend a 32-bit address.
1983   SDLoc SL(Addr);
1984 
1985   const MachineFunction &MF = CurDAG->getMachineFunction();
1986   const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1987   unsigned AddrHiVal = Info->get32BitAddressHighBits();
1988   SDValue AddrHi = CurDAG->getTargetConstant(AddrHiVal, SL, MVT::i32);
1989 
1990   const SDValue Ops[] = {
1991     CurDAG->getTargetConstant(AMDGPU::SReg_64_XEXECRegClassID, SL, MVT::i32),
1992     Addr,
1993     CurDAG->getTargetConstant(AMDGPU::sub0, SL, MVT::i32),
1994     SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SL, MVT::i32, AddrHi),
1995             0),
1996     CurDAG->getTargetConstant(AMDGPU::sub1, SL, MVT::i32),
1997   };
1998 
1999   return SDValue(CurDAG->getMachineNode(AMDGPU::REG_SEQUENCE, SL, MVT::i64,
2000                                         Ops), 0);
2001 }
2002 
2003 bool AMDGPUDAGToDAGISel::SelectSMRD(SDValue Addr, SDValue &SBase,
2004                                      SDValue &Offset, bool &Imm) const {
2005   SDLoc SL(Addr);
2006 
2007   // A 32-bit (address + offset) should not cause unsigned 32-bit integer
2008   // wraparound, because s_load instructions perform the addition in 64 bits.
2009   if ((Addr.getValueType() != MVT::i32 ||
2010        Addr->getFlags().hasNoUnsignedWrap())) {
2011     SDValue N0, N1;
2012     // Extract the base and offset if possible.
2013     if (CurDAG->isBaseWithConstantOffset(Addr) ||
2014         Addr.getOpcode() == ISD::ADD) {
2015       N0 = Addr.getOperand(0);
2016       N1 = Addr.getOperand(1);
2017     } else if (getBaseWithOffsetUsingSplitOR(*CurDAG, Addr, N0, N1)) {
2018       assert(N0 && N1 && isa<ConstantSDNode>(N1));
2019     }
2020     if (N0 && N1) {
2021       if (SelectSMRDOffset(N1, Offset, Imm)) {
2022         SBase = Expand32BitAddress(N0);
2023         return true;
2024       }
2025     }
2026   }
2027   SBase = Expand32BitAddress(Addr);
2028   Offset = CurDAG->getTargetConstant(0, SL, MVT::i32);
2029   Imm = true;
2030   return true;
2031 }
2032 
2033 bool AMDGPUDAGToDAGISel::SelectSMRDImm(SDValue Addr, SDValue &SBase,
2034                                        SDValue &Offset) const {
2035   bool Imm = false;
2036   return SelectSMRD(Addr, SBase, Offset, Imm) && Imm;
2037 }
2038 
2039 bool AMDGPUDAGToDAGISel::SelectSMRDImm32(SDValue Addr, SDValue &SBase,
2040                                          SDValue &Offset) const {
2041 
2042   assert(Subtarget->getGeneration() == AMDGPUSubtarget::SEA_ISLANDS);
2043 
2044   bool Imm = false;
2045   if (!SelectSMRD(Addr, SBase, Offset, Imm))
2046     return false;
2047 
2048   return !Imm && isa<ConstantSDNode>(Offset);
2049 }
2050 
2051 bool AMDGPUDAGToDAGISel::SelectSMRDSgpr(SDValue Addr, SDValue &SBase,
2052                                         SDValue &Offset) const {
2053   bool Imm = false;
2054   return SelectSMRD(Addr, SBase, Offset, Imm) && !Imm &&
2055          !isa<ConstantSDNode>(Offset);
2056 }
2057 
2058 bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm(SDValue Addr,
2059                                              SDValue &Offset) const {
2060   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr)) {
2061     // The immediate offset for S_BUFFER instructions is unsigned.
2062     if (auto Imm =
2063             AMDGPU::getSMRDEncodedOffset(*Subtarget, C->getZExtValue(), true)) {
2064       Offset = CurDAG->getTargetConstant(*Imm, SDLoc(Addr), MVT::i32);
2065       return true;
2066     }
2067   }
2068 
2069   return false;
2070 }
2071 
2072 bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm32(SDValue Addr,
2073                                                SDValue &Offset) const {
2074   assert(Subtarget->getGeneration() == AMDGPUSubtarget::SEA_ISLANDS);
2075 
2076   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr)) {
2077     if (auto Imm = AMDGPU::getSMRDEncodedLiteralOffset32(*Subtarget,
2078                                                          C->getZExtValue())) {
2079       Offset = CurDAG->getTargetConstant(*Imm, SDLoc(Addr), MVT::i32);
2080       return true;
2081     }
2082   }
2083 
2084   return false;
2085 }
2086 
2087 bool AMDGPUDAGToDAGISel::SelectMOVRELOffset(SDValue Index,
2088                                             SDValue &Base,
2089                                             SDValue &Offset) const {
2090   SDLoc DL(Index);
2091 
2092   if (CurDAG->isBaseWithConstantOffset(Index)) {
2093     SDValue N0 = Index.getOperand(0);
2094     SDValue N1 = Index.getOperand(1);
2095     ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
2096 
2097     // (add n0, c0)
2098     // Don't peel off the offset (c0) if doing so could possibly lead
2099     // the base (n0) to be negative.
2100     // (or n0, |c0|) can never change a sign given isBaseWithConstantOffset.
2101     if (C1->getSExtValue() <= 0 || CurDAG->SignBitIsZero(N0) ||
2102         (Index->getOpcode() == ISD::OR && C1->getSExtValue() >= 0)) {
2103       Base = N0;
2104       Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i32);
2105       return true;
2106     }
2107   }
2108 
2109   if (isa<ConstantSDNode>(Index))
2110     return false;
2111 
2112   Base = Index;
2113   Offset = CurDAG->getTargetConstant(0, DL, MVT::i32);
2114   return true;
2115 }
2116 
2117 SDNode *AMDGPUDAGToDAGISel::getS_BFE(unsigned Opcode, const SDLoc &DL,
2118                                      SDValue Val, uint32_t Offset,
2119                                      uint32_t Width) {
2120   // Transformation function, pack the offset and width of a BFE into
2121   // the format expected by the S_BFE_I32 / S_BFE_U32. In the second
2122   // source, bits [5:0] contain the offset and bits [22:16] the width.
2123   uint32_t PackedVal = Offset | (Width << 16);
2124   SDValue PackedConst = CurDAG->getTargetConstant(PackedVal, DL, MVT::i32);
2125 
2126   return CurDAG->getMachineNode(Opcode, DL, MVT::i32, Val, PackedConst);
2127 }
2128 
2129 void AMDGPUDAGToDAGISel::SelectS_BFEFromShifts(SDNode *N) {
2130   // "(a << b) srl c)" ---> "BFE_U32 a, (c-b), (32-c)
2131   // "(a << b) sra c)" ---> "BFE_I32 a, (c-b), (32-c)
2132   // Predicate: 0 < b <= c < 32
2133 
2134   const SDValue &Shl = N->getOperand(0);
2135   ConstantSDNode *B = dyn_cast<ConstantSDNode>(Shl->getOperand(1));
2136   ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
2137 
2138   if (B && C) {
2139     uint32_t BVal = B->getZExtValue();
2140     uint32_t CVal = C->getZExtValue();
2141 
2142     if (0 < BVal && BVal <= CVal && CVal < 32) {
2143       bool Signed = N->getOpcode() == ISD::SRA;
2144       unsigned Opcode = Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32;
2145 
2146       ReplaceNode(N, getS_BFE(Opcode, SDLoc(N), Shl.getOperand(0), CVal - BVal,
2147                               32 - CVal));
2148       return;
2149     }
2150   }
2151   SelectCode(N);
2152 }
2153 
2154 void AMDGPUDAGToDAGISel::SelectS_BFE(SDNode *N) {
2155   switch (N->getOpcode()) {
2156   case ISD::AND:
2157     if (N->getOperand(0).getOpcode() == ISD::SRL) {
2158       // "(a srl b) & mask" ---> "BFE_U32 a, b, popcount(mask)"
2159       // Predicate: isMask(mask)
2160       const SDValue &Srl = N->getOperand(0);
2161       ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(Srl.getOperand(1));
2162       ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(N->getOperand(1));
2163 
2164       if (Shift && Mask) {
2165         uint32_t ShiftVal = Shift->getZExtValue();
2166         uint32_t MaskVal = Mask->getZExtValue();
2167 
2168         if (isMask_32(MaskVal)) {
2169           uint32_t WidthVal = countPopulation(MaskVal);
2170 
2171           ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N),
2172                                   Srl.getOperand(0), ShiftVal, WidthVal));
2173           return;
2174         }
2175       }
2176     }
2177     break;
2178   case ISD::SRL:
2179     if (N->getOperand(0).getOpcode() == ISD::AND) {
2180       // "(a & mask) srl b)" ---> "BFE_U32 a, b, popcount(mask >> b)"
2181       // Predicate: isMask(mask >> b)
2182       const SDValue &And = N->getOperand(0);
2183       ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(N->getOperand(1));
2184       ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(And->getOperand(1));
2185 
2186       if (Shift && Mask) {
2187         uint32_t ShiftVal = Shift->getZExtValue();
2188         uint32_t MaskVal = Mask->getZExtValue() >> ShiftVal;
2189 
2190         if (isMask_32(MaskVal)) {
2191           uint32_t WidthVal = countPopulation(MaskVal);
2192 
2193           ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N),
2194                                   And.getOperand(0), ShiftVal, WidthVal));
2195           return;
2196         }
2197       }
2198     } else if (N->getOperand(0).getOpcode() == ISD::SHL) {
2199       SelectS_BFEFromShifts(N);
2200       return;
2201     }
2202     break;
2203   case ISD::SRA:
2204     if (N->getOperand(0).getOpcode() == ISD::SHL) {
2205       SelectS_BFEFromShifts(N);
2206       return;
2207     }
2208     break;
2209 
2210   case ISD::SIGN_EXTEND_INREG: {
2211     // sext_inreg (srl x, 16), i8 -> bfe_i32 x, 16, 8
2212     SDValue Src = N->getOperand(0);
2213     if (Src.getOpcode() != ISD::SRL)
2214       break;
2215 
2216     const ConstantSDNode *Amt = dyn_cast<ConstantSDNode>(Src.getOperand(1));
2217     if (!Amt)
2218       break;
2219 
2220     unsigned Width = cast<VTSDNode>(N->getOperand(1))->getVT().getSizeInBits();
2221     ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_I32, SDLoc(N), Src.getOperand(0),
2222                             Amt->getZExtValue(), Width));
2223     return;
2224   }
2225   }
2226 
2227   SelectCode(N);
2228 }
2229 
2230 bool AMDGPUDAGToDAGISel::isCBranchSCC(const SDNode *N) const {
2231   assert(N->getOpcode() == ISD::BRCOND);
2232   if (!N->hasOneUse())
2233     return false;
2234 
2235   SDValue Cond = N->getOperand(1);
2236   if (Cond.getOpcode() == ISD::CopyToReg)
2237     Cond = Cond.getOperand(2);
2238 
2239   if (Cond.getOpcode() != ISD::SETCC || !Cond.hasOneUse())
2240     return false;
2241 
2242   MVT VT = Cond.getOperand(0).getSimpleValueType();
2243   if (VT == MVT::i32)
2244     return true;
2245 
2246   if (VT == MVT::i64) {
2247     auto ST = static_cast<const GCNSubtarget *>(Subtarget);
2248 
2249     ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
2250     return (CC == ISD::SETEQ || CC == ISD::SETNE) && ST->hasScalarCompareEq64();
2251   }
2252 
2253   return false;
2254 }
2255 
2256 void AMDGPUDAGToDAGISel::SelectBRCOND(SDNode *N) {
2257   SDValue Cond = N->getOperand(1);
2258 
2259   if (Cond.isUndef()) {
2260     CurDAG->SelectNodeTo(N, AMDGPU::SI_BR_UNDEF, MVT::Other,
2261                          N->getOperand(2), N->getOperand(0));
2262     return;
2263   }
2264 
2265   const GCNSubtarget *ST = static_cast<const GCNSubtarget *>(Subtarget);
2266   const SIRegisterInfo *TRI = ST->getRegisterInfo();
2267 
2268   bool UseSCCBr = isCBranchSCC(N) && isUniformBr(N);
2269   unsigned BrOp = UseSCCBr ? AMDGPU::S_CBRANCH_SCC1 : AMDGPU::S_CBRANCH_VCCNZ;
2270   Register CondReg = UseSCCBr ? AMDGPU::SCC : TRI->getVCC();
2271   SDLoc SL(N);
2272 
2273   if (!UseSCCBr) {
2274     // This is the case that we are selecting to S_CBRANCH_VCCNZ.  We have not
2275     // analyzed what generates the vcc value, so we do not know whether vcc
2276     // bits for disabled lanes are 0.  Thus we need to mask out bits for
2277     // disabled lanes.
2278     //
2279     // For the case that we select S_CBRANCH_SCC1 and it gets
2280     // changed to S_CBRANCH_VCCNZ in SIFixSGPRCopies, SIFixSGPRCopies calls
2281     // SIInstrInfo::moveToVALU which inserts the S_AND).
2282     //
2283     // We could add an analysis of what generates the vcc value here and omit
2284     // the S_AND when is unnecessary. But it would be better to add a separate
2285     // pass after SIFixSGPRCopies to do the unnecessary S_AND removal, so it
2286     // catches both cases.
2287     Cond = SDValue(CurDAG->getMachineNode(ST->isWave32() ? AMDGPU::S_AND_B32
2288                                                          : AMDGPU::S_AND_B64,
2289                      SL, MVT::i1,
2290                      CurDAG->getRegister(ST->isWave32() ? AMDGPU::EXEC_LO
2291                                                         : AMDGPU::EXEC,
2292                                          MVT::i1),
2293                     Cond),
2294                    0);
2295   }
2296 
2297   SDValue VCC = CurDAG->getCopyToReg(N->getOperand(0), SL, CondReg, Cond);
2298   CurDAG->SelectNodeTo(N, BrOp, MVT::Other,
2299                        N->getOperand(2), // Basic Block
2300                        VCC.getValue(0));
2301 }
2302 
2303 void AMDGPUDAGToDAGISel::SelectFMAD_FMA(SDNode *N) {
2304   MVT VT = N->getSimpleValueType(0);
2305   bool IsFMA = N->getOpcode() == ISD::FMA;
2306   if (VT != MVT::f32 || (!Subtarget->hasMadMixInsts() &&
2307                          !Subtarget->hasFmaMixInsts()) ||
2308       ((IsFMA && Subtarget->hasMadMixInsts()) ||
2309        (!IsFMA && Subtarget->hasFmaMixInsts()))) {
2310     SelectCode(N);
2311     return;
2312   }
2313 
2314   SDValue Src0 = N->getOperand(0);
2315   SDValue Src1 = N->getOperand(1);
2316   SDValue Src2 = N->getOperand(2);
2317   unsigned Src0Mods, Src1Mods, Src2Mods;
2318 
2319   // Avoid using v_mad_mix_f32/v_fma_mix_f32 unless there is actually an operand
2320   // using the conversion from f16.
2321   bool Sel0 = SelectVOP3PMadMixModsImpl(Src0, Src0, Src0Mods);
2322   bool Sel1 = SelectVOP3PMadMixModsImpl(Src1, Src1, Src1Mods);
2323   bool Sel2 = SelectVOP3PMadMixModsImpl(Src2, Src2, Src2Mods);
2324 
2325   assert((IsFMA || !Mode.allFP32Denormals()) &&
2326          "fmad selected with denormals enabled");
2327   // TODO: We can select this with f32 denormals enabled if all the sources are
2328   // converted from f16 (in which case fmad isn't legal).
2329 
2330   if (Sel0 || Sel1 || Sel2) {
2331     // For dummy operands.
2332     SDValue Zero = CurDAG->getTargetConstant(0, SDLoc(), MVT::i32);
2333     SDValue Ops[] = {
2334       CurDAG->getTargetConstant(Src0Mods, SDLoc(), MVT::i32), Src0,
2335       CurDAG->getTargetConstant(Src1Mods, SDLoc(), MVT::i32), Src1,
2336       CurDAG->getTargetConstant(Src2Mods, SDLoc(), MVT::i32), Src2,
2337       CurDAG->getTargetConstant(0, SDLoc(), MVT::i1),
2338       Zero, Zero
2339     };
2340 
2341     CurDAG->SelectNodeTo(N,
2342                          IsFMA ? AMDGPU::V_FMA_MIX_F32 : AMDGPU::V_MAD_MIX_F32,
2343                          MVT::f32, Ops);
2344   } else {
2345     SelectCode(N);
2346   }
2347 }
2348 
2349 // This is here because there isn't a way to use the generated sub0_sub1 as the
2350 // subreg index to EXTRACT_SUBREG in tablegen.
2351 void AMDGPUDAGToDAGISel::SelectATOMIC_CMP_SWAP(SDNode *N) {
2352   MemSDNode *Mem = cast<MemSDNode>(N);
2353   unsigned AS = Mem->getAddressSpace();
2354   if (AS == AMDGPUAS::FLAT_ADDRESS) {
2355     SelectCode(N);
2356     return;
2357   }
2358 
2359   MVT VT = N->getSimpleValueType(0);
2360   bool Is32 = (VT == MVT::i32);
2361   SDLoc SL(N);
2362 
2363   MachineSDNode *CmpSwap = nullptr;
2364   if (Subtarget->hasAddr64()) {
2365     SDValue SRsrc, VAddr, SOffset, Offset, SLC;
2366 
2367     if (SelectMUBUFAddr64(Mem->getBasePtr(), SRsrc, VAddr, SOffset, Offset, SLC)) {
2368       unsigned Opcode = Is32 ? AMDGPU::BUFFER_ATOMIC_CMPSWAP_ADDR64_RTN :
2369         AMDGPU::BUFFER_ATOMIC_CMPSWAP_X2_ADDR64_RTN;
2370       SDValue CmpVal = Mem->getOperand(2);
2371       SDValue GLC = CurDAG->getTargetConstant(1, SL, MVT::i1);
2372 
2373       // XXX - Do we care about glue operands?
2374 
2375       SDValue Ops[] = {
2376         CmpVal, VAddr, SRsrc, SOffset, Offset, GLC, SLC, Mem->getChain()
2377       };
2378 
2379       CmpSwap = CurDAG->getMachineNode(Opcode, SL, Mem->getVTList(), Ops);
2380     }
2381   }
2382 
2383   if (!CmpSwap) {
2384     SDValue SRsrc, SOffset, Offset, SLC;
2385     if (SelectMUBUFOffset(Mem->getBasePtr(), SRsrc, SOffset, Offset, SLC)) {
2386       unsigned Opcode = Is32 ? AMDGPU::BUFFER_ATOMIC_CMPSWAP_OFFSET_RTN :
2387         AMDGPU::BUFFER_ATOMIC_CMPSWAP_X2_OFFSET_RTN;
2388 
2389       SDValue CmpVal = Mem->getOperand(2);
2390       SDValue GLC = CurDAG->getTargetConstant(1, SL, MVT::i1);
2391       SDValue Ops[] = {
2392         CmpVal, SRsrc, SOffset, Offset, GLC, SLC, Mem->getChain()
2393       };
2394 
2395       CmpSwap = CurDAG->getMachineNode(Opcode, SL, Mem->getVTList(), Ops);
2396     }
2397   }
2398 
2399   if (!CmpSwap) {
2400     SelectCode(N);
2401     return;
2402   }
2403 
2404   MachineMemOperand *MMO = Mem->getMemOperand();
2405   CurDAG->setNodeMemRefs(CmpSwap, {MMO});
2406 
2407   unsigned SubReg = Is32 ? AMDGPU::sub0 : AMDGPU::sub0_sub1;
2408   SDValue Extract
2409     = CurDAG->getTargetExtractSubreg(SubReg, SL, VT, SDValue(CmpSwap, 0));
2410 
2411   ReplaceUses(SDValue(N, 0), Extract);
2412   ReplaceUses(SDValue(N, 1), SDValue(CmpSwap, 1));
2413   CurDAG->RemoveDeadNode(N);
2414 }
2415 
2416 void AMDGPUDAGToDAGISel::SelectDSAppendConsume(SDNode *N, unsigned IntrID) {
2417   // The address is assumed to be uniform, so if it ends up in a VGPR, it will
2418   // be copied to an SGPR with readfirstlane.
2419   unsigned Opc = IntrID == Intrinsic::amdgcn_ds_append ?
2420     AMDGPU::DS_APPEND : AMDGPU::DS_CONSUME;
2421 
2422   SDValue Chain = N->getOperand(0);
2423   SDValue Ptr = N->getOperand(2);
2424   MemIntrinsicSDNode *M = cast<MemIntrinsicSDNode>(N);
2425   MachineMemOperand *MMO = M->getMemOperand();
2426   bool IsGDS = M->getAddressSpace() == AMDGPUAS::REGION_ADDRESS;
2427 
2428   SDValue Offset;
2429   if (CurDAG->isBaseWithConstantOffset(Ptr)) {
2430     SDValue PtrBase = Ptr.getOperand(0);
2431     SDValue PtrOffset = Ptr.getOperand(1);
2432 
2433     const APInt &OffsetVal = cast<ConstantSDNode>(PtrOffset)->getAPIntValue();
2434     if (isDSOffsetLegal(PtrBase, OffsetVal.getZExtValue())) {
2435       N = glueCopyToM0(N, PtrBase);
2436       Offset = CurDAG->getTargetConstant(OffsetVal, SDLoc(), MVT::i32);
2437     }
2438   }
2439 
2440   if (!Offset) {
2441     N = glueCopyToM0(N, Ptr);
2442     Offset = CurDAG->getTargetConstant(0, SDLoc(), MVT::i32);
2443   }
2444 
2445   SDValue Ops[] = {
2446     Offset,
2447     CurDAG->getTargetConstant(IsGDS, SDLoc(), MVT::i32),
2448     Chain,
2449     N->getOperand(N->getNumOperands() - 1) // New glue
2450   };
2451 
2452   SDNode *Selected = CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops);
2453   CurDAG->setNodeMemRefs(cast<MachineSDNode>(Selected), {MMO});
2454 }
2455 
2456 static unsigned gwsIntrinToOpcode(unsigned IntrID) {
2457   switch (IntrID) {
2458   case Intrinsic::amdgcn_ds_gws_init:
2459     return AMDGPU::DS_GWS_INIT;
2460   case Intrinsic::amdgcn_ds_gws_barrier:
2461     return AMDGPU::DS_GWS_BARRIER;
2462   case Intrinsic::amdgcn_ds_gws_sema_v:
2463     return AMDGPU::DS_GWS_SEMA_V;
2464   case Intrinsic::amdgcn_ds_gws_sema_br:
2465     return AMDGPU::DS_GWS_SEMA_BR;
2466   case Intrinsic::amdgcn_ds_gws_sema_p:
2467     return AMDGPU::DS_GWS_SEMA_P;
2468   case Intrinsic::amdgcn_ds_gws_sema_release_all:
2469     return AMDGPU::DS_GWS_SEMA_RELEASE_ALL;
2470   default:
2471     llvm_unreachable("not a gws intrinsic");
2472   }
2473 }
2474 
2475 void AMDGPUDAGToDAGISel::SelectDS_GWS(SDNode *N, unsigned IntrID) {
2476   if (IntrID == Intrinsic::amdgcn_ds_gws_sema_release_all &&
2477       !Subtarget->hasGWSSemaReleaseAll()) {
2478     // Let this error.
2479     SelectCode(N);
2480     return;
2481   }
2482 
2483   // Chain, intrinsic ID, vsrc, offset
2484   const bool HasVSrc = N->getNumOperands() == 4;
2485   assert(HasVSrc || N->getNumOperands() == 3);
2486 
2487   SDLoc SL(N);
2488   SDValue BaseOffset = N->getOperand(HasVSrc ? 3 : 2);
2489   int ImmOffset = 0;
2490   MemIntrinsicSDNode *M = cast<MemIntrinsicSDNode>(N);
2491   MachineMemOperand *MMO = M->getMemOperand();
2492 
2493   // Don't worry if the offset ends up in a VGPR. Only one lane will have
2494   // effect, so SIFixSGPRCopies will validly insert readfirstlane.
2495 
2496   // The resource id offset is computed as (<isa opaque base> + M0[21:16] +
2497   // offset field) % 64. Some versions of the programming guide omit the m0
2498   // part, or claim it's from offset 0.
2499   if (ConstantSDNode *ConstOffset = dyn_cast<ConstantSDNode>(BaseOffset)) {
2500     // If we have a constant offset, try to use the 0 in m0 as the base.
2501     // TODO: Look into changing the default m0 initialization value. If the
2502     // default -1 only set the low 16-bits, we could leave it as-is and add 1 to
2503     // the immediate offset.
2504     glueCopyToM0(N, CurDAG->getTargetConstant(0, SL, MVT::i32));
2505     ImmOffset = ConstOffset->getZExtValue();
2506   } else {
2507     if (CurDAG->isBaseWithConstantOffset(BaseOffset)) {
2508       ImmOffset = BaseOffset.getConstantOperandVal(1);
2509       BaseOffset = BaseOffset.getOperand(0);
2510     }
2511 
2512     // Prefer to do the shift in an SGPR since it should be possible to use m0
2513     // as the result directly. If it's already an SGPR, it will be eliminated
2514     // later.
2515     SDNode *SGPROffset
2516       = CurDAG->getMachineNode(AMDGPU::V_READFIRSTLANE_B32, SL, MVT::i32,
2517                                BaseOffset);
2518     // Shift to offset in m0
2519     SDNode *M0Base
2520       = CurDAG->getMachineNode(AMDGPU::S_LSHL_B32, SL, MVT::i32,
2521                                SDValue(SGPROffset, 0),
2522                                CurDAG->getTargetConstant(16, SL, MVT::i32));
2523     glueCopyToM0(N, SDValue(M0Base, 0));
2524   }
2525 
2526   SDValue Chain = N->getOperand(0);
2527   SDValue OffsetField = CurDAG->getTargetConstant(ImmOffset, SL, MVT::i32);
2528 
2529   const unsigned Opc = gwsIntrinToOpcode(IntrID);
2530   SmallVector<SDValue, 5> Ops;
2531   if (HasVSrc)
2532     Ops.push_back(N->getOperand(2));
2533   Ops.push_back(OffsetField);
2534   Ops.push_back(Chain);
2535 
2536   SDNode *Selected = CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops);
2537   CurDAG->setNodeMemRefs(cast<MachineSDNode>(Selected), {MMO});
2538 }
2539 
2540 void AMDGPUDAGToDAGISel::SelectInterpP1F16(SDNode *N) {
2541   if (Subtarget->getLDSBankCount() != 16) {
2542     // This is a single instruction with a pattern.
2543     SelectCode(N);
2544     return;
2545   }
2546 
2547   SDLoc DL(N);
2548 
2549   // This requires 2 instructions. It is possible to write a pattern to support
2550   // this, but the generated isel emitter doesn't correctly deal with multiple
2551   // output instructions using the same physical register input. The copy to m0
2552   // is incorrectly placed before the second instruction.
2553   //
2554   // TODO: Match source modifiers.
2555   //
2556   // def : Pat <
2557   //   (int_amdgcn_interp_p1_f16
2558   //    (VOP3Mods f32:$src0, i32:$src0_modifiers),
2559   //                             (i32 timm:$attrchan), (i32 timm:$attr),
2560   //                             (i1 timm:$high), M0),
2561   //   (V_INTERP_P1LV_F16 $src0_modifiers, VGPR_32:$src0, timm:$attr,
2562   //       timm:$attrchan, 0,
2563   //       (V_INTERP_MOV_F32 2, timm:$attr, timm:$attrchan), timm:$high)> {
2564   //   let Predicates = [has16BankLDS];
2565   // }
2566 
2567   // 16 bank LDS
2568   SDValue ToM0 = CurDAG->getCopyToReg(CurDAG->getEntryNode(), DL, AMDGPU::M0,
2569                                       N->getOperand(5), SDValue());
2570 
2571   SDVTList VTs = CurDAG->getVTList(MVT::f32, MVT::Other);
2572 
2573   SDNode *InterpMov =
2574     CurDAG->getMachineNode(AMDGPU::V_INTERP_MOV_F32, DL, VTs, {
2575         CurDAG->getTargetConstant(2, DL, MVT::i32), // P0
2576         N->getOperand(3),  // Attr
2577         N->getOperand(2),  // Attrchan
2578         ToM0.getValue(1) // In glue
2579   });
2580 
2581   SDNode *InterpP1LV =
2582     CurDAG->getMachineNode(AMDGPU::V_INTERP_P1LV_F16, DL, MVT::f32, {
2583         CurDAG->getTargetConstant(0, DL, MVT::i32), // $src0_modifiers
2584         N->getOperand(1), // Src0
2585         N->getOperand(3), // Attr
2586         N->getOperand(2), // Attrchan
2587         CurDAG->getTargetConstant(0, DL, MVT::i32), // $src2_modifiers
2588         SDValue(InterpMov, 0), // Src2 - holds two f16 values selected by high
2589         N->getOperand(4), // high
2590         CurDAG->getTargetConstant(0, DL, MVT::i1), // $clamp
2591         CurDAG->getTargetConstant(0, DL, MVT::i32), // $omod
2592         SDValue(InterpMov, 1)
2593   });
2594 
2595   CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), SDValue(InterpP1LV, 0));
2596 }
2597 
2598 void AMDGPUDAGToDAGISel::SelectINTRINSIC_W_CHAIN(SDNode *N) {
2599   unsigned IntrID = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
2600   switch (IntrID) {
2601   case Intrinsic::amdgcn_ds_append:
2602   case Intrinsic::amdgcn_ds_consume: {
2603     if (N->getValueType(0) != MVT::i32)
2604       break;
2605     SelectDSAppendConsume(N, IntrID);
2606     return;
2607   }
2608   }
2609 
2610   SelectCode(N);
2611 }
2612 
2613 void AMDGPUDAGToDAGISel::SelectINTRINSIC_WO_CHAIN(SDNode *N) {
2614   unsigned IntrID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
2615   unsigned Opcode;
2616   switch (IntrID) {
2617   case Intrinsic::amdgcn_wqm:
2618     Opcode = AMDGPU::WQM;
2619     break;
2620   case Intrinsic::amdgcn_softwqm:
2621     Opcode = AMDGPU::SOFT_WQM;
2622     break;
2623   case Intrinsic::amdgcn_wwm:
2624     Opcode = AMDGPU::WWM;
2625     break;
2626   case Intrinsic::amdgcn_interp_p1_f16:
2627     SelectInterpP1F16(N);
2628     return;
2629   default:
2630     SelectCode(N);
2631     return;
2632   }
2633 
2634   SDValue Src = N->getOperand(1);
2635   CurDAG->SelectNodeTo(N, Opcode, N->getVTList(), {Src});
2636 }
2637 
2638 void AMDGPUDAGToDAGISel::SelectINTRINSIC_VOID(SDNode *N) {
2639   unsigned IntrID = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
2640   switch (IntrID) {
2641   case Intrinsic::amdgcn_ds_gws_init:
2642   case Intrinsic::amdgcn_ds_gws_barrier:
2643   case Intrinsic::amdgcn_ds_gws_sema_v:
2644   case Intrinsic::amdgcn_ds_gws_sema_br:
2645   case Intrinsic::amdgcn_ds_gws_sema_p:
2646   case Intrinsic::amdgcn_ds_gws_sema_release_all:
2647     SelectDS_GWS(N, IntrID);
2648     return;
2649   default:
2650     break;
2651   }
2652 
2653   SelectCode(N);
2654 }
2655 
2656 bool AMDGPUDAGToDAGISel::SelectVOP3ModsImpl(SDValue In, SDValue &Src,
2657                                             unsigned &Mods,
2658                                             bool AllowAbs) const {
2659   Mods = 0;
2660   Src = In;
2661 
2662   if (Src.getOpcode() == ISD::FNEG) {
2663     Mods |= SISrcMods::NEG;
2664     Src = Src.getOperand(0);
2665   }
2666 
2667   if (AllowAbs && Src.getOpcode() == ISD::FABS) {
2668     Mods |= SISrcMods::ABS;
2669     Src = Src.getOperand(0);
2670   }
2671 
2672   return true;
2673 }
2674 
2675 bool AMDGPUDAGToDAGISel::SelectVOP3Mods(SDValue In, SDValue &Src,
2676                                         SDValue &SrcMods) const {
2677   unsigned Mods;
2678   if (SelectVOP3ModsImpl(In, Src, Mods)) {
2679     SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
2680     return true;
2681   }
2682 
2683   return false;
2684 }
2685 
2686 bool AMDGPUDAGToDAGISel::SelectVOP3BMods(SDValue In, SDValue &Src,
2687                                          SDValue &SrcMods) const {
2688   unsigned Mods;
2689   if (SelectVOP3ModsImpl(In, Src, Mods, /* AllowAbs */ false)) {
2690     SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
2691     return true;
2692   }
2693 
2694   return false;
2695 }
2696 
2697 bool AMDGPUDAGToDAGISel::SelectVOP3Mods_NNaN(SDValue In, SDValue &Src,
2698                                              SDValue &SrcMods) const {
2699   SelectVOP3Mods(In, Src, SrcMods);
2700   return isNoNanSrc(Src);
2701 }
2702 
2703 bool AMDGPUDAGToDAGISel::SelectVOP3NoMods(SDValue In, SDValue &Src) const {
2704   if (In.getOpcode() == ISD::FABS || In.getOpcode() == ISD::FNEG)
2705     return false;
2706 
2707   Src = In;
2708   return true;
2709 }
2710 
2711 bool AMDGPUDAGToDAGISel::SelectVOP3Mods0(SDValue In, SDValue &Src,
2712                                          SDValue &SrcMods, SDValue &Clamp,
2713                                          SDValue &Omod) const {
2714   SDLoc DL(In);
2715   Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1);
2716   Omod = CurDAG->getTargetConstant(0, DL, MVT::i1);
2717 
2718   return SelectVOP3Mods(In, Src, SrcMods);
2719 }
2720 
2721 bool AMDGPUDAGToDAGISel::SelectVOP3BMods0(SDValue In, SDValue &Src,
2722                                           SDValue &SrcMods, SDValue &Clamp,
2723                                           SDValue &Omod) const {
2724   SDLoc DL(In);
2725   Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1);
2726   Omod = CurDAG->getTargetConstant(0, DL, MVT::i1);
2727 
2728   return SelectVOP3BMods(In, Src, SrcMods);
2729 }
2730 
2731 bool AMDGPUDAGToDAGISel::SelectVOP3OMods(SDValue In, SDValue &Src,
2732                                          SDValue &Clamp, SDValue &Omod) const {
2733   Src = In;
2734 
2735   SDLoc DL(In);
2736   Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1);
2737   Omod = CurDAG->getTargetConstant(0, DL, MVT::i1);
2738 
2739   return true;
2740 }
2741 
2742 bool AMDGPUDAGToDAGISel::SelectVOP3PMods(SDValue In, SDValue &Src,
2743                                          SDValue &SrcMods) const {
2744   unsigned Mods = 0;
2745   Src = In;
2746 
2747   if (Src.getOpcode() == ISD::FNEG) {
2748     Mods ^= (SISrcMods::NEG | SISrcMods::NEG_HI);
2749     Src = Src.getOperand(0);
2750   }
2751 
2752   if (Src.getOpcode() == ISD::BUILD_VECTOR) {
2753     unsigned VecMods = Mods;
2754 
2755     SDValue Lo = stripBitcast(Src.getOperand(0));
2756     SDValue Hi = stripBitcast(Src.getOperand(1));
2757 
2758     if (Lo.getOpcode() == ISD::FNEG) {
2759       Lo = stripBitcast(Lo.getOperand(0));
2760       Mods ^= SISrcMods::NEG;
2761     }
2762 
2763     if (Hi.getOpcode() == ISD::FNEG) {
2764       Hi = stripBitcast(Hi.getOperand(0));
2765       Mods ^= SISrcMods::NEG_HI;
2766     }
2767 
2768     if (isExtractHiElt(Lo, Lo))
2769       Mods |= SISrcMods::OP_SEL_0;
2770 
2771     if (isExtractHiElt(Hi, Hi))
2772       Mods |= SISrcMods::OP_SEL_1;
2773 
2774     Lo = stripExtractLoElt(Lo);
2775     Hi = stripExtractLoElt(Hi);
2776 
2777     if (Lo == Hi && !isInlineImmediate(Lo.getNode())) {
2778       // Really a scalar input. Just select from the low half of the register to
2779       // avoid packing.
2780 
2781       Src = Lo;
2782       SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
2783       return true;
2784     }
2785 
2786     Mods = VecMods;
2787   }
2788 
2789   // Packed instructions do not have abs modifiers.
2790   Mods |= SISrcMods::OP_SEL_1;
2791 
2792   SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
2793   return true;
2794 }
2795 
2796 bool AMDGPUDAGToDAGISel::SelectVOP3OpSel(SDValue In, SDValue &Src,
2797                                          SDValue &SrcMods) const {
2798   Src = In;
2799   // FIXME: Handle op_sel
2800   SrcMods = CurDAG->getTargetConstant(0, SDLoc(In), MVT::i32);
2801   return true;
2802 }
2803 
2804 bool AMDGPUDAGToDAGISel::SelectVOP3OpSelMods(SDValue In, SDValue &Src,
2805                                              SDValue &SrcMods) const {
2806   // FIXME: Handle op_sel
2807   return SelectVOP3Mods(In, Src, SrcMods);
2808 }
2809 
2810 // The return value is not whether the match is possible (which it always is),
2811 // but whether or not it a conversion is really used.
2812 bool AMDGPUDAGToDAGISel::SelectVOP3PMadMixModsImpl(SDValue In, SDValue &Src,
2813                                                    unsigned &Mods) const {
2814   Mods = 0;
2815   SelectVOP3ModsImpl(In, Src, Mods);
2816 
2817   if (Src.getOpcode() == ISD::FP_EXTEND) {
2818     Src = Src.getOperand(0);
2819     assert(Src.getValueType() == MVT::f16);
2820     Src = stripBitcast(Src);
2821 
2822     // Be careful about folding modifiers if we already have an abs. fneg is
2823     // applied last, so we don't want to apply an earlier fneg.
2824     if ((Mods & SISrcMods::ABS) == 0) {
2825       unsigned ModsTmp;
2826       SelectVOP3ModsImpl(Src, Src, ModsTmp);
2827 
2828       if ((ModsTmp & SISrcMods::NEG) != 0)
2829         Mods ^= SISrcMods::NEG;
2830 
2831       if ((ModsTmp & SISrcMods::ABS) != 0)
2832         Mods |= SISrcMods::ABS;
2833     }
2834 
2835     // op_sel/op_sel_hi decide the source type and source.
2836     // If the source's op_sel_hi is set, it indicates to do a conversion from fp16.
2837     // If the sources's op_sel is set, it picks the high half of the source
2838     // register.
2839 
2840     Mods |= SISrcMods::OP_SEL_1;
2841     if (isExtractHiElt(Src, Src)) {
2842       Mods |= SISrcMods::OP_SEL_0;
2843 
2844       // TODO: Should we try to look for neg/abs here?
2845     }
2846 
2847     return true;
2848   }
2849 
2850   return false;
2851 }
2852 
2853 bool AMDGPUDAGToDAGISel::SelectVOP3PMadMixMods(SDValue In, SDValue &Src,
2854                                                SDValue &SrcMods) const {
2855   unsigned Mods = 0;
2856   SelectVOP3PMadMixModsImpl(In, Src, Mods);
2857   SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
2858   return true;
2859 }
2860 
2861 SDValue AMDGPUDAGToDAGISel::getHi16Elt(SDValue In) const {
2862   if (In.isUndef())
2863     return CurDAG->getUNDEF(MVT::i32);
2864 
2865   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(In)) {
2866     SDLoc SL(In);
2867     return CurDAG->getConstant(C->getZExtValue() << 16, SL, MVT::i32);
2868   }
2869 
2870   if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(In)) {
2871     SDLoc SL(In);
2872     return CurDAG->getConstant(
2873       C->getValueAPF().bitcastToAPInt().getZExtValue() << 16, SL, MVT::i32);
2874   }
2875 
2876   SDValue Src;
2877   if (isExtractHiElt(In, Src))
2878     return Src;
2879 
2880   return SDValue();
2881 }
2882 
2883 bool AMDGPUDAGToDAGISel::isVGPRImm(const SDNode * N) const {
2884   assert(CurDAG->getTarget().getTargetTriple().getArch() == Triple::amdgcn);
2885 
2886   const SIRegisterInfo *SIRI =
2887     static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
2888   const SIInstrInfo * SII =
2889     static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
2890 
2891   unsigned Limit = 0;
2892   bool AllUsesAcceptSReg = true;
2893   for (SDNode::use_iterator U = N->use_begin(), E = SDNode::use_end();
2894     Limit < 10 && U != E; ++U, ++Limit) {
2895     const TargetRegisterClass *RC = getOperandRegClass(*U, U.getOperandNo());
2896 
2897     // If the register class is unknown, it could be an unknown
2898     // register class that needs to be an SGPR, e.g. an inline asm
2899     // constraint
2900     if (!RC || SIRI->isSGPRClass(RC))
2901       return false;
2902 
2903     if (RC != &AMDGPU::VS_32RegClass) {
2904       AllUsesAcceptSReg = false;
2905       SDNode * User = *U;
2906       if (User->isMachineOpcode()) {
2907         unsigned Opc = User->getMachineOpcode();
2908         MCInstrDesc Desc = SII->get(Opc);
2909         if (Desc.isCommutable()) {
2910           unsigned OpIdx = Desc.getNumDefs() + U.getOperandNo();
2911           unsigned CommuteIdx1 = TargetInstrInfo::CommuteAnyOperandIndex;
2912           if (SII->findCommutedOpIndices(Desc, OpIdx, CommuteIdx1)) {
2913             unsigned CommutedOpNo = CommuteIdx1 - Desc.getNumDefs();
2914             const TargetRegisterClass *CommutedRC = getOperandRegClass(*U, CommutedOpNo);
2915             if (CommutedRC == &AMDGPU::VS_32RegClass)
2916               AllUsesAcceptSReg = true;
2917           }
2918         }
2919       }
2920       // If "AllUsesAcceptSReg == false" so far we haven't suceeded
2921       // commuting current user. This means have at least one use
2922       // that strictly require VGPR. Thus, we will not attempt to commute
2923       // other user instructions.
2924       if (!AllUsesAcceptSReg)
2925         break;
2926     }
2927   }
2928   return !AllUsesAcceptSReg && (Limit < 10);
2929 }
2930 
2931 bool AMDGPUDAGToDAGISel::isUniformLoad(const SDNode * N) const {
2932   auto Ld = cast<LoadSDNode>(N);
2933 
2934   return Ld->getAlignment() >= 4 &&
2935         (
2936           (
2937             (
2938               Ld->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS       ||
2939               Ld->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS_32BIT
2940             )
2941             &&
2942             !N->isDivergent()
2943           )
2944           ||
2945           (
2946             Subtarget->getScalarizeGlobalBehavior() &&
2947             Ld->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
2948             Ld->isSimple() &&
2949             !N->isDivergent() &&
2950             static_cast<const SITargetLowering *>(
2951               getTargetLowering())->isMemOpHasNoClobberedMemOperand(N)
2952           )
2953         );
2954 }
2955 
2956 void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
2957   const AMDGPUTargetLowering& Lowering =
2958     *static_cast<const AMDGPUTargetLowering*>(getTargetLowering());
2959   bool IsModified = false;
2960   do {
2961     IsModified = false;
2962 
2963     // Go over all selected nodes and try to fold them a bit more
2964     SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_begin();
2965     while (Position != CurDAG->allnodes_end()) {
2966       SDNode *Node = &*Position++;
2967       MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(Node);
2968       if (!MachineNode)
2969         continue;
2970 
2971       SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
2972       if (ResNode != Node) {
2973         if (ResNode)
2974           ReplaceUses(Node, ResNode);
2975         IsModified = true;
2976       }
2977     }
2978     CurDAG->RemoveDeadNodes();
2979   } while (IsModified);
2980 }
2981 
2982 bool R600DAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
2983   Subtarget = &MF.getSubtarget<R600Subtarget>();
2984   return SelectionDAGISel::runOnMachineFunction(MF);
2985 }
2986 
2987 bool R600DAGToDAGISel::isConstantLoad(const MemSDNode *N, int CbId) const {
2988   if (!N->readMem())
2989     return false;
2990   if (CbId == -1)
2991     return N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS ||
2992            N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS_32BIT;
2993 
2994   return N->getAddressSpace() == AMDGPUAS::CONSTANT_BUFFER_0 + CbId;
2995 }
2996 
2997 bool R600DAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
2998                                                          SDValue& IntPtr) {
2999   if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
3000     IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, SDLoc(Addr),
3001                                        true);
3002     return true;
3003   }
3004   return false;
3005 }
3006 
3007 bool R600DAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
3008     SDValue& BaseReg, SDValue &Offset) {
3009   if (!isa<ConstantSDNode>(Addr)) {
3010     BaseReg = Addr;
3011     Offset = CurDAG->getIntPtrConstant(0, SDLoc(Addr), true);
3012     return true;
3013   }
3014   return false;
3015 }
3016 
3017 void R600DAGToDAGISel::Select(SDNode *N) {
3018   unsigned int Opc = N->getOpcode();
3019   if (N->isMachineOpcode()) {
3020     N->setNodeId(-1);
3021     return;   // Already selected.
3022   }
3023 
3024   switch (Opc) {
3025   default: break;
3026   case AMDGPUISD::BUILD_VERTICAL_VECTOR:
3027   case ISD::SCALAR_TO_VECTOR:
3028   case ISD::BUILD_VECTOR: {
3029     EVT VT = N->getValueType(0);
3030     unsigned NumVectorElts = VT.getVectorNumElements();
3031     unsigned RegClassID;
3032     // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
3033     // that adds a 128 bits reg copy when going through TwoAddressInstructions
3034     // pass. We want to avoid 128 bits copies as much as possible because they
3035     // can't be bundled by our scheduler.
3036     switch(NumVectorElts) {
3037     case 2: RegClassID = R600::R600_Reg64RegClassID; break;
3038     case 4:
3039       if (Opc == AMDGPUISD::BUILD_VERTICAL_VECTOR)
3040         RegClassID = R600::R600_Reg128VerticalRegClassID;
3041       else
3042         RegClassID = R600::R600_Reg128RegClassID;
3043       break;
3044     default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
3045     }
3046     SelectBuildVector(N, RegClassID);
3047     return;
3048   }
3049   }
3050 
3051   SelectCode(N);
3052 }
3053 
3054 bool R600DAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
3055                                           SDValue &Offset) {
3056   ConstantSDNode *C;
3057   SDLoc DL(Addr);
3058 
3059   if ((C = dyn_cast<ConstantSDNode>(Addr))) {
3060     Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32);
3061     Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
3062   } else if ((Addr.getOpcode() == AMDGPUISD::DWORDADDR) &&
3063              (C = dyn_cast<ConstantSDNode>(Addr.getOperand(0)))) {
3064     Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32);
3065     Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
3066   } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
3067             (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
3068     Base = Addr.getOperand(0);
3069     Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
3070   } else {
3071     Base = Addr;
3072     Offset = CurDAG->getTargetConstant(0, DL, MVT::i32);
3073   }
3074 
3075   return true;
3076 }
3077 
3078 bool R600DAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
3079                                           SDValue &Offset) {
3080   ConstantSDNode *IMMOffset;
3081 
3082   if (Addr.getOpcode() == ISD::ADD
3083       && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
3084       && isInt<16>(IMMOffset->getZExtValue())) {
3085 
3086       Base = Addr.getOperand(0);
3087       Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr),
3088                                          MVT::i32);
3089       return true;
3090   // If the pointer address is constant, we can move it to the offset field.
3091   } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
3092              && isInt<16>(IMMOffset->getZExtValue())) {
3093     Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
3094                                   SDLoc(CurDAG->getEntryNode()),
3095                                   R600::ZERO, MVT::i32);
3096     Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr),
3097                                        MVT::i32);
3098     return true;
3099   }
3100 
3101   // Default case, no offset
3102   Base = Addr;
3103   Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i32);
3104   return true;
3105 }
3106