LLVM  5.0.0
SIISelLowering.cpp
Go to the documentation of this file.
1 //===-- SIISelLowering.cpp - SI DAG Lowering Implementation ---------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file
11 /// \brief Custom DAG lowering for SI
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifdef _MSC_VER
16 // Provide M_PI.
17 #define _USE_MATH_DEFINES
18 #endif
19 
20 #include "SIISelLowering.h"
21 #include "AMDGPU.h"
22 #include "AMDGPUIntrinsicInfo.h"
23 #include "AMDGPUSubtarget.h"
24 #include "AMDGPUTargetMachine.h"
25 #include "SIDefines.h"
26 #include "SIInstrInfo.h"
27 #include "SIMachineFunctionInfo.h"
28 #include "SIRegisterInfo.h"
29 #include "Utils/AMDGPUBaseInfo.h"
30 #include "llvm/ADT/APFloat.h"
31 #include "llvm/ADT/APInt.h"
32 #include "llvm/ADT/ArrayRef.h"
33 #include "llvm/ADT/BitVector.h"
34 #include "llvm/ADT/SmallVector.h"
35 #include "llvm/ADT/StringRef.h"
36 #include "llvm/ADT/StringSwitch.h"
37 #include "llvm/ADT/Twine.h"
38 #include "llvm/CodeGen/Analysis.h"
54 #include "llvm/IR/Constants.h"
55 #include "llvm/IR/DataLayout.h"
56 #include "llvm/IR/DebugLoc.h"
57 #include "llvm/IR/DerivedTypes.h"
58 #include "llvm/IR/DiagnosticInfo.h"
59 #include "llvm/IR/Function.h"
60 #include "llvm/IR/GlobalValue.h"
61 #include "llvm/IR/InstrTypes.h"
62 #include "llvm/IR/Instruction.h"
63 #include "llvm/IR/Instructions.h"
64 #include "llvm/IR/IntrinsicInst.h"
65 #include "llvm/IR/Type.h"
66 #include "llvm/Support/Casting.h"
67 #include "llvm/Support/CodeGen.h"
69 #include "llvm/Support/Compiler.h"
71 #include "llvm/Support/KnownBits.h"
76 #include <cassert>
77 #include <cmath>
78 #include <cstdint>
79 #include <iterator>
80 #include <tuple>
81 #include <utility>
82 #include <vector>
83 
84 using namespace llvm;
85 
87  "amdgpu-vgpr-index-mode",
88  cl::desc("Use GPR indexing mode instead of movrel for vector indexing"),
89  cl::init(false));
90 
91 static unsigned findFirstFreeSGPR(CCState &CCInfo) {
92  unsigned NumSGPRs = AMDGPU::SGPR_32RegClass.getNumRegs();
93  for (unsigned Reg = 0; Reg < NumSGPRs; ++Reg) {
94  if (!CCInfo.isAllocated(AMDGPU::SGPR0 + Reg)) {
95  return AMDGPU::SGPR0 + Reg;
96  }
97  }
98  llvm_unreachable("Cannot allocate sgpr");
99 }
100 
102  const SISubtarget &STI)
103  : AMDGPUTargetLowering(TM, STI) {
104  addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
105  addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
106 
107  addRegisterClass(MVT::i32, &AMDGPU::SReg_32_XM0RegClass);
108  addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass);
109 
110  addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
111  addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
112  addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
113 
114  addRegisterClass(MVT::v2i64, &AMDGPU::SReg_128RegClass);
115  addRegisterClass(MVT::v2f64, &AMDGPU::SReg_128RegClass);
116 
117  addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
118  addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
119 
120  addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass);
121  addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
122 
123  addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass);
124  addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
125 
126  if (Subtarget->has16BitInsts()) {
127  addRegisterClass(MVT::i16, &AMDGPU::SReg_32_XM0RegClass);
128  addRegisterClass(MVT::f16, &AMDGPU::SReg_32_XM0RegClass);
129  }
130 
131  if (Subtarget->hasVOP3PInsts()) {
132  addRegisterClass(MVT::v2i16, &AMDGPU::SReg_32_XM0RegClass);
133  addRegisterClass(MVT::v2f16, &AMDGPU::SReg_32_XM0RegClass);
134  }
135 
137 
138  // We need to custom lower vector stores from local memory
144 
150 
161 
165 
170 
176 
181 
184 
192 
197 
199 
203 
210 
213 
216 
217  // We only support LOAD/STORE and vector manipulation ops for vectors
218  // with > 4 elements.
221  for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
222  switch (Op) {
223  case ISD::LOAD:
224  case ISD::STORE:
225  case ISD::BUILD_VECTOR:
226  case ISD::BITCAST:
232  break;
233  case ISD::CONCAT_VECTORS:
235  break;
236  default:
238  break;
239  }
240  }
241  }
242 
243  // TODO: For dynamic 64-bit vector inserts/extracts, should emit a pseudo that
244  // is expanded to avoid having two separate loops in case the index is a VGPR.
245 
246  // Most operations are naturally 32-bit vector operations. We only support
247  // load and store of i64 vectors, so promote v2i64 vector operations to v4i32.
248  for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) {
251 
254 
257 
260  }
261 
266 
267  // Avoid stack access for these.
268  // TODO: Generalize to more vector types.
273 
274  // BUFFER/FLAT_ATOMIC_CMP_SWAP on GCN GPUs needs input marshalling,
275  // and output demarshalling
278 
279  // We can't return success/failure, only the old value,
280  // let LLVM add the comparison
283 
284  if (getSubtarget()->hasFlatAddressSpace()) {
287  }
288 
291 
292  // On SI this is s_memtime and s_memrealtime on VI.
296 
299 
304  }
305 
307 
312 
313  if (Subtarget->has16BitInsts()) {
315 
318 
321 
324 
327 
332 
335 
340 
342 
344 
346 
348 
353 
358 
359  // F16 - Constant Actions.
361 
362  // F16 - Load/Store Actions.
367 
368  // F16 - VOP1 Actions.
377 
378  // F16 - VOP2 Actions.
384 
385  // F16 - VOP3 Actions.
387  if (!Subtarget->hasFP16Denormals())
389  }
390 
391  if (Subtarget->hasVOP3PInsts()) {
392  for (MVT VT : {MVT::v2i16, MVT::v2f16}) {
393  for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
394  switch (Op) {
395  case ISD::LOAD:
396  case ISD::STORE:
397  case ISD::BUILD_VECTOR:
398  case ISD::BITCAST:
404  break;
405  case ISD::CONCAT_VECTORS:
407  break;
408  default:
410  break;
411  }
412  }
413  }
414 
415  // XXX - Do these do anything? Vector constants turn into build_vector.
418 
423 
428 
439 
450 
457 
458  // This isn't really legal, but this avoids the legalizer unrolling it (and
459  // allows matching fneg (fabs x) patterns)
461 
464 
468  } else {
471  }
472 
475  }
476 
499 
500  // All memory operations. Some folding on the pointer operand is done to help
501  // matching the constant offsets in the addressing modes.
519 
521 }
522 
524  return static_cast<const SISubtarget *>(Subtarget);
525 }
526 
527 //===----------------------------------------------------------------------===//
528 // TargetLowering queries
529 //===----------------------------------------------------------------------===//
530 
532  EVT) const {
533  // SI has some legal vector types, but no legal vector operations. Say no
534  // shuffles are legal in order to prefer scalarizing some vector operations.
535  return false;
536 }
537 
539  const CallInst &CI,
540  unsigned IntrID) const {
541  switch (IntrID) {
542  case Intrinsic::amdgcn_atomic_inc:
543  case Intrinsic::amdgcn_atomic_dec: {
545  Info.memVT = MVT::getVT(CI.getType());
546  Info.ptrVal = CI.getOperand(0);
547  Info.align = 0;
548 
549  const ConstantInt *Vol = dyn_cast<ConstantInt>(CI.getOperand(4));
550  Info.vol = !Vol || !Vol->isZero();
551  Info.readMem = true;
552  Info.writeMem = true;
553  return true;
554  }
555  default:
556  return false;
557  }
558 }
559 
562  Type *&AccessTy) const {
563  switch (II->getIntrinsicID()) {
564  case Intrinsic::amdgcn_atomic_inc:
565  case Intrinsic::amdgcn_atomic_dec: {
566  Value *Ptr = II->getArgOperand(0);
567  AccessTy = II->getType();
568  Ops.push_back(Ptr);
569  return true;
570  }
571  default:
572  return false;
573  }
574 }
575 
576 bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const {
577  if (!Subtarget->hasFlatInstOffsets()) {
578  // Flat instructions do not have offsets, and only have the register
579  // address.
580  return AM.BaseOffs == 0 && AM.Scale == 0;
581  }
582 
583  // GFX9 added a 13-bit signed offset. When using regular flat instructions,
584  // the sign bit is ignored and is treated as a 12-bit unsigned offset.
585 
586  // Just r + i
587  return isUInt<12>(AM.BaseOffs) && AM.Scale == 0;
588 }
589 
590 bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const {
591  // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
592  // additionally can do r + r + i with addr64. 32-bit has more addressing
593  // mode options. Depending on the resource constant, it can also do
594  // (i64 r0) + (i32 r1) * (i14 i).
595  //
596  // Private arrays end up using a scratch buffer most of the time, so also
597  // assume those use MUBUF instructions. Scratch loads / stores are currently
598  // implemented as mubuf instructions with offen bit set, so slightly
599  // different than the normal addr64.
600  if (!isUInt<12>(AM.BaseOffs))
601  return false;
602 
603  // FIXME: Since we can split immediate into soffset and immediate offset,
604  // would it make sense to allow any immediate?
605 
606  switch (AM.Scale) {
607  case 0: // r + i or just i, depending on HasBaseReg.
608  return true;
609  case 1:
610  return true; // We have r + r or r + i.
611  case 2:
612  if (AM.HasBaseReg) {
613  // Reject 2 * r + r.
614  return false;
615  }
616 
617  // Allow 2 * r as r + r
618  // Or 2 * r + i is allowed as r + r + i.
619  return true;
620  default: // Don't allow n * r
621  return false;
622  }
623 }
624 
626  const AddrMode &AM, Type *Ty,
627  unsigned AS) const {
628  // No global is ever allowed as a base.
629  if (AM.BaseGV)
630  return false;
631 
632  if (AS == AMDGPUASI.GLOBAL_ADDRESS) {
634  // Assume the we will use FLAT for all global memory accesses
635  // on VI.
636  // FIXME: This assumption is currently wrong. On VI we still use
637  // MUBUF instructions for the r + i addressing mode. As currently
638  // implemented, the MUBUF instructions only work on buffer < 4GB.
639  // It may be possible to support > 4GB buffers with MUBUF instructions,
640  // by setting the stride value in the resource descriptor which would
641  // increase the size limit to (stride * 4GB). However, this is risky,
642  // because it has never been validated.
643  return isLegalFlatAddressingMode(AM);
644  }
645 
646  return isLegalMUBUFAddressingMode(AM);
647  } else if (AS == AMDGPUASI.CONSTANT_ADDRESS) {
648  // If the offset isn't a multiple of 4, it probably isn't going to be
649  // correctly aligned.
650  // FIXME: Can we get the real alignment here?
651  if (AM.BaseOffs % 4 != 0)
652  return isLegalMUBUFAddressingMode(AM);
653 
654  // There are no SMRD extloads, so if we have to do a small type access we
655  // will use a MUBUF load.
656  // FIXME?: We also need to do this if unaligned, but we don't know the
657  // alignment here.
658  if (DL.getTypeStoreSize(Ty) < 4)
659  return isLegalMUBUFAddressingMode(AM);
660 
662  // SMRD instructions have an 8-bit, dword offset on SI.
663  if (!isUInt<8>(AM.BaseOffs / 4))
664  return false;
666  // On CI+, this can also be a 32-bit literal constant offset. If it fits
667  // in 8-bits, it can use a smaller encoding.
668  if (!isUInt<32>(AM.BaseOffs / 4))
669  return false;
671  // On VI, these use the SMEM format and the offset is 20-bit in bytes.
672  if (!isUInt<20>(AM.BaseOffs))
673  return false;
674  } else
675  llvm_unreachable("unhandled generation");
676 
677  if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
678  return true;
679 
680  if (AM.Scale == 1 && AM.HasBaseReg)
681  return true;
682 
683  return false;
684 
685  } else if (AS == AMDGPUASI.PRIVATE_ADDRESS) {
686  return isLegalMUBUFAddressingMode(AM);
687  } else if (AS == AMDGPUASI.LOCAL_ADDRESS ||
688  AS == AMDGPUASI.REGION_ADDRESS) {
689  // Basic, single offset DS instructions allow a 16-bit unsigned immediate
690  // field.
691  // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have
692  // an 8-bit dword offset but we don't know the alignment here.
693  if (!isUInt<16>(AM.BaseOffs))
694  return false;
695 
696  if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
697  return true;
698 
699  if (AM.Scale == 1 && AM.HasBaseReg)
700  return true;
701 
702  return false;
703  } else if (AS == AMDGPUASI.FLAT_ADDRESS ||
705  // For an unknown address space, this usually means that this is for some
706  // reason being used for pure arithmetic, and not based on some addressing
707  // computation. We don't have instructions that compute pointers with any
708  // addressing modes, so treat them as having no offset like flat
709  // instructions.
710  return isLegalFlatAddressingMode(AM);
711  } else {
712  llvm_unreachable("unhandled address space");
713  }
714 }
715 
716 bool SITargetLowering::canMergeStoresTo(unsigned AS, EVT MemVT,
717  const SelectionDAG &DAG) const {
718  if (AS == AMDGPUASI.GLOBAL_ADDRESS || AS == AMDGPUASI.FLAT_ADDRESS) {
719  return (MemVT.getSizeInBits() <= 4 * 32);
720  } else if (AS == AMDGPUASI.PRIVATE_ADDRESS) {
721  unsigned MaxPrivateBits = 8 * getSubtarget()->getMaxPrivateElementSize();
722  return (MemVT.getSizeInBits() <= MaxPrivateBits);
723  } else if (AS == AMDGPUASI.LOCAL_ADDRESS) {
724  return (MemVT.getSizeInBits() <= 2 * 32);
725  }
726  return true;
727 }
728 
730  unsigned AddrSpace,
731  unsigned Align,
732  bool *IsFast) const {
733  if (IsFast)
734  *IsFast = false;
735 
736  // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
737  // which isn't a simple VT.
738  // Until MVT is extended to handle this, simply check for the size and
739  // rely on the condition below: allow accesses if the size is a multiple of 4.
740  if (VT == MVT::Other || (VT != MVT::Other && VT.getSizeInBits() > 1024 &&
741  VT.getStoreSize() > 16)) {
742  return false;
743  }
744 
745  if (AddrSpace == AMDGPUASI.LOCAL_ADDRESS ||
746  AddrSpace == AMDGPUASI.REGION_ADDRESS) {
747  // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
748  // aligned, 8 byte access in a single operation using ds_read2/write2_b32
749  // with adjacent offsets.
750  bool AlignedBy4 = (Align % 4 == 0);
751  if (IsFast)
752  *IsFast = AlignedBy4;
753 
754  return AlignedBy4;
755  }
756 
757  // FIXME: We have to be conservative here and assume that flat operations
758  // will access scratch. If we had access to the IR function, then we
759  // could determine if any private memory was used in the function.
761  (AddrSpace == AMDGPUASI.PRIVATE_ADDRESS ||
762  AddrSpace == AMDGPUASI.FLAT_ADDRESS)) {
763  return false;
764  }
765 
767  // If we have an uniform constant load, it still requires using a slow
768  // buffer instruction if unaligned.
769  if (IsFast) {
770  *IsFast = (AddrSpace == AMDGPUASI.CONSTANT_ADDRESS) ?
771  (Align % 4 == 0) : true;
772  }
773 
774  return true;
775  }
776 
777  // Smaller than dword value must be aligned.
778  if (VT.bitsLT(MVT::i32))
779  return false;
780 
781  // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
782  // byte-address are ignored, thus forcing Dword alignment.
783  // This applies to private, global, and constant memory.
784  if (IsFast)
785  *IsFast = true;
786 
787  return VT.bitsGT(MVT::i32) && Align % 4 == 0;
788 }
789 
790 EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
791  unsigned SrcAlign, bool IsMemset,
792  bool ZeroMemset,
793  bool MemcpyStrSrc,
794  MachineFunction &MF) const {
795  // FIXME: Should account for address space here.
796 
797  // The default fallback uses the private pointer size as a guess for a type to
798  // use. Make sure we switch these to 64-bit accesses.
799 
800  if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
801  return MVT::v4i32;
802 
803  if (Size >= 8 && DstAlign >= 4)
804  return MVT::v2i32;
805 
806  // Use the default.
807  return MVT::Other;
808 }
809 
810 static bool isFlatGlobalAddrSpace(unsigned AS, AMDGPUAS AMDGPUASI) {
811  return AS == AMDGPUASI.GLOBAL_ADDRESS ||
812  AS == AMDGPUASI.FLAT_ADDRESS ||
813  AS == AMDGPUASI.CONSTANT_ADDRESS;
814 }
815 
817  unsigned DestAS) const {
818  return isFlatGlobalAddrSpace(SrcAS, AMDGPUASI) &&
820 }
821 
823  const MemSDNode *MemNode = cast<MemSDNode>(N);
824  const Value *Ptr = MemNode->getMemOperand()->getValue();
825  const Instruction *I = dyn_cast<Instruction>(Ptr);
826  return I && I->getMetadata("amdgpu.noclobber");
827 }
828 
830  unsigned DestAS) const {
831  // Flat -> private/local is a simple truncate.
832  // Flat -> global is no-op
833  if (SrcAS == AMDGPUASI.FLAT_ADDRESS)
834  return true;
835 
836  return isNoopAddrSpaceCast(SrcAS, DestAS);
837 }
838 
840  const MemSDNode *MemNode = cast<MemSDNode>(N);
841 
842  return AMDGPU::isUniformMMO(MemNode->getMemOperand());
843 }
844 
847  if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
848  return TypeSplitVector;
849 
851 }
852 
854  Type *Ty) const {
855  // FIXME: Could be smarter if called for vector constants.
856  return true;
857 }
858 
860  if (Subtarget->has16BitInsts() && VT == MVT::i16) {
861  switch (Op) {
862  case ISD::LOAD:
863  case ISD::STORE:
864 
865  // These operations are done with 32-bit instructions anyway.
866  case ISD::AND:
867  case ISD::OR:
868  case ISD::XOR:
869  case ISD::SELECT:
870  // TODO: Extensions?
871  return true;
872  default:
873  return false;
874  }
875  }
876 
877  // SimplifySetCC uses this function to determine whether or not it should
878  // create setcc with i1 operands. We don't have instructions for i1 setcc.
879  if (VT == MVT::i1 && Op == ISD::SETCC)
880  return false;
881 
883 }
884 
885 SDValue SITargetLowering::lowerKernArgParameterPtr(SelectionDAG &DAG,
886  const SDLoc &SL,
887  SDValue Chain,
888  uint64_t Offset) const {
889  const DataLayout &DL = DAG.getDataLayout();
891  const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
892  unsigned InputPtrReg = TRI->getPreloadedValue(MF,
894 
897  SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
898  MRI.getLiveInVirtReg(InputPtrReg), PtrVT);
899  return DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
900  DAG.getConstant(Offset, SL, PtrVT));
901 }
902 
903 SDValue SITargetLowering::convertArgType(SelectionDAG &DAG, EVT VT, EVT MemVT,
904  const SDLoc &SL, SDValue Val,
905  bool Signed,
906  const ISD::InputArg *Arg) const {
907  if (Arg && (Arg->Flags.isSExt() || Arg->Flags.isZExt()) &&
908  VT.bitsLT(MemVT)) {
909  unsigned Opc = Arg->Flags.isZExt() ? ISD::AssertZext : ISD::AssertSext;
910  Val = DAG.getNode(Opc, SL, MemVT, Val, DAG.getValueType(VT));
911  }
912 
913  if (MemVT.isFloatingPoint())
914  Val = getFPExtOrFPTrunc(DAG, Val, SL, VT);
915  else if (Signed)
916  Val = DAG.getSExtOrTrunc(Val, SL, VT);
917  else
918  Val = DAG.getZExtOrTrunc(Val, SL, VT);
919 
920  return Val;
921 }
922 
923 SDValue SITargetLowering::lowerKernargMemParameter(
924  SelectionDAG &DAG, EVT VT, EVT MemVT,
925  const SDLoc &SL, SDValue Chain,
926  uint64_t Offset, bool Signed,
927  const ISD::InputArg *Arg) const {
928  const DataLayout &DL = DAG.getDataLayout();
929  Type *Ty = MemVT.getTypeForEVT(*DAG.getContext());
931  MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
932 
933  unsigned Align = DL.getABITypeAlignment(Ty);
934 
935  SDValue Ptr = lowerKernArgParameterPtr(DAG, SL, Chain, Offset);
936  SDValue Load = DAG.getLoad(MemVT, SL, Chain, Ptr, PtrInfo, Align,
940 
941  SDValue Val = convertArgType(DAG, VT, MemVT, SL, Load, Signed, Arg);
942  return DAG.getMergeValues({ Val, Load.getValue(1) }, SL);
943 }
944 
945 SDValue SITargetLowering::lowerStackParameter(SelectionDAG &DAG, CCValAssign &VA,
946  const SDLoc &SL, SDValue Chain,
947  const ISD::InputArg &Arg) const {
949  MachineFrameInfo &MFI = MF.getFrameInfo();
950 
951  if (Arg.Flags.isByVal()) {
952  unsigned Size = Arg.Flags.getByValSize();
953  int FrameIdx = MFI.CreateFixedObject(Size, VA.getLocMemOffset(), false);
954  return DAG.getFrameIndex(FrameIdx, MVT::i32);
955  }
956 
957  unsigned ArgOffset = VA.getLocMemOffset();
958  unsigned ArgSize = VA.getValVT().getStoreSize();
959 
960  int FI = MFI.CreateFixedObject(ArgSize, ArgOffset, true);
961 
962  // Create load nodes to retrieve arguments from the stack.
963  SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
964  SDValue ArgValue;
965 
966  // For NON_EXTLOAD, generic code in getLoad assert(ValVT == MemVT)
968  MVT MemVT = VA.getValVT();
969 
970  switch (VA.getLocInfo()) {
971  default:
972  break;
973  case CCValAssign::BCvt:
974  MemVT = VA.getLocVT();
975  break;
976  case CCValAssign::SExt:
977  ExtType = ISD::SEXTLOAD;
978  break;
979  case CCValAssign::ZExt:
980  ExtType = ISD::ZEXTLOAD;
981  break;
982  case CCValAssign::AExt:
983  ExtType = ISD::EXTLOAD;
984  break;
985  }
986 
987  ArgValue = DAG.getExtLoad(
988  ExtType, SL, VA.getLocVT(), Chain, FIN,
990  MemVT);
991  return ArgValue;
992 }
993 
995  CallingConv::ID CallConv,
997  BitVector &Skipped,
998  FunctionType *FType,
999  SIMachineFunctionInfo *Info) {
1000  for (unsigned I = 0, E = Ins.size(), PSInputNum = 0; I != E; ++I) {
1001  const ISD::InputArg &Arg = Ins[I];
1002 
1003  // First check if it's a PS input addr.
1004  if (CallConv == CallingConv::AMDGPU_PS && !Arg.Flags.isInReg() &&
1005  !Arg.Flags.isByVal() && PSInputNum <= 15) {
1006 
1007  if (!Arg.Used && !Info->isPSInputAllocated(PSInputNum)) {
1008  // We can safely skip PS inputs.
1009  Skipped.set(I);
1010  ++PSInputNum;
1011  continue;
1012  }
1013 
1014  Info->markPSInputAllocated(PSInputNum);
1015  if (Arg.Used)
1016  Info->markPSInputEnabled(PSInputNum);
1017 
1018  ++PSInputNum;
1019  }
1020 
1021  // Second split vertices into their elements.
1022  if (Arg.VT.isVector()) {
1023  ISD::InputArg NewArg = Arg;
1024  NewArg.Flags.setSplit();
1025  NewArg.VT = Arg.VT.getVectorElementType();
1026 
1027  // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
1028  // three or five element vertex only needs three or five registers,
1029  // NOT four or eight.
1030  Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
1031  unsigned NumElements = ParamType->getVectorNumElements();
1032 
1033  for (unsigned J = 0; J != NumElements; ++J) {
1034  Splits.push_back(NewArg);
1035  NewArg.PartOffset += NewArg.VT.getStoreSize();
1036  }
1037  } else {
1038  Splits.push_back(Arg);
1039  }
1040  }
1041 }
1042 
1043 // Allocate special inputs passed in VGPRs.
1045  MachineFunction &MF,
1046  const SIRegisterInfo &TRI,
1047  SIMachineFunctionInfo &Info) {
1048  if (Info.hasWorkItemIDX()) {
1050  MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
1051  CCInfo.AllocateReg(Reg);
1052  }
1053 
1054  if (Info.hasWorkItemIDY()) {
1056  MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
1057  CCInfo.AllocateReg(Reg);
1058  }
1059 
1060  if (Info.hasWorkItemIDZ()) {
1062  MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
1063  CCInfo.AllocateReg(Reg);
1064  }
1065 }
1066 
1067 // Allocate special inputs passed in user SGPRs.
1068 static void allocateHSAUserSGPRs(CCState &CCInfo,
1069  MachineFunction &MF,
1070  const SIRegisterInfo &TRI,
1071  SIMachineFunctionInfo &Info) {
1072  if (Info.hasImplicitBufferPtr()) {
1073  unsigned ImplicitBufferPtrReg = Info.addImplicitBufferPtr(TRI);
1074  MF.addLiveIn(ImplicitBufferPtrReg, &AMDGPU::SGPR_64RegClass);
1075  CCInfo.AllocateReg(ImplicitBufferPtrReg);
1076  }
1077 
1078  // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
1079  if (Info.hasPrivateSegmentBuffer()) {
1080  unsigned PrivateSegmentBufferReg = Info.addPrivateSegmentBuffer(TRI);
1081  MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SGPR_128RegClass);
1082  CCInfo.AllocateReg(PrivateSegmentBufferReg);
1083  }
1084 
1085  if (Info.hasDispatchPtr()) {
1086  unsigned DispatchPtrReg = Info.addDispatchPtr(TRI);
1087  MF.addLiveIn(DispatchPtrReg, &AMDGPU::SGPR_64RegClass);
1088  CCInfo.AllocateReg(DispatchPtrReg);
1089  }
1090 
1091  if (Info.hasQueuePtr()) {
1092  unsigned QueuePtrReg = Info.addQueuePtr(TRI);
1093  MF.addLiveIn(QueuePtrReg, &AMDGPU::SGPR_64RegClass);
1094  CCInfo.AllocateReg(QueuePtrReg);
1095  }
1096 
1097  if (Info.hasKernargSegmentPtr()) {
1098  unsigned InputPtrReg = Info.addKernargSegmentPtr(TRI);
1099  MF.addLiveIn(InputPtrReg, &AMDGPU::SGPR_64RegClass);
1100  CCInfo.AllocateReg(InputPtrReg);
1101  }
1102 
1103  if (Info.hasDispatchID()) {
1104  unsigned DispatchIDReg = Info.addDispatchID(TRI);
1105  MF.addLiveIn(DispatchIDReg, &AMDGPU::SGPR_64RegClass);
1106  CCInfo.AllocateReg(DispatchIDReg);
1107  }
1108 
1109  if (Info.hasFlatScratchInit()) {
1110  unsigned FlatScratchInitReg = Info.addFlatScratchInit(TRI);
1111  MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SGPR_64RegClass);
1112  CCInfo.AllocateReg(FlatScratchInitReg);
1113  }
1114 
1115  // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read
1116  // these from the dispatch pointer.
1117 }
1118 
1119 // Allocate special input registers that are initialized per-wave.
1120 static void allocateSystemSGPRs(CCState &CCInfo,
1121  MachineFunction &MF,
1122  SIMachineFunctionInfo &Info,
1123  CallingConv::ID CallConv,
1124  bool IsShader) {
1125  if (Info.hasWorkGroupIDX()) {
1126  unsigned Reg = Info.addWorkGroupIDX();
1127  MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass);
1128  CCInfo.AllocateReg(Reg);
1129  }
1130 
1131  if (Info.hasWorkGroupIDY()) {
1132  unsigned Reg = Info.addWorkGroupIDY();
1133  MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass);
1134  CCInfo.AllocateReg(Reg);
1135  }
1136 
1137  if (Info.hasWorkGroupIDZ()) {
1138  unsigned Reg = Info.addWorkGroupIDZ();
1139  MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass);
1140  CCInfo.AllocateReg(Reg);
1141  }
1142 
1143  if (Info.hasWorkGroupInfo()) {
1144  unsigned Reg = Info.addWorkGroupInfo();
1145  MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass);
1146  CCInfo.AllocateReg(Reg);
1147  }
1148 
1149  if (Info.hasPrivateSegmentWaveByteOffset()) {
1150  // Scratch wave offset passed in system SGPR.
1151  unsigned PrivateSegmentWaveByteOffsetReg;
1152 
1153  if (IsShader) {
1154  PrivateSegmentWaveByteOffsetReg =
1156 
1157  // This is true if the scratch wave byte offset doesn't have a fixed
1158  // location.
1159  if (PrivateSegmentWaveByteOffsetReg == AMDGPU::NoRegister) {
1160  PrivateSegmentWaveByteOffsetReg = findFirstFreeSGPR(CCInfo);
1161  Info.setPrivateSegmentWaveByteOffset(PrivateSegmentWaveByteOffsetReg);
1162  }
1163  } else
1164  PrivateSegmentWaveByteOffsetReg = Info.addPrivateSegmentWaveByteOffset();
1165 
1166  MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass);
1167  CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg);
1168  }
1169 }
1170 
1172  MachineFunction &MF,
1173  const SIRegisterInfo &TRI,
1174  SIMachineFunctionInfo &Info) {
1175  // Now that we've figured out where the scratch register inputs are, see if
1176  // should reserve the arguments and use them directly.
1177  MachineFrameInfo &MFI = MF.getFrameInfo();
1178  bool HasStackObjects = MFI.hasStackObjects();
1179 
1180  // Record that we know we have non-spill stack objects so we don't need to
1181  // check all stack objects later.
1182  if (HasStackObjects)
1183  Info.setHasNonSpillStackObjects(true);
1184 
1185  // Everything live out of a block is spilled with fast regalloc, so it's
1186  // almost certain that spilling will be required.
1187  if (TM.getOptLevel() == CodeGenOpt::None)
1188  HasStackObjects = true;
1189 
1190  const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
1191  if (ST.isAmdCodeObjectV2(MF)) {
1192  if (HasStackObjects) {
1193  // If we have stack objects, we unquestionably need the private buffer
1194  // resource. For the Code Object V2 ABI, this will be the first 4 user
1195  // SGPR inputs. We can reserve those and use them directly.
1196 
1197  unsigned PrivateSegmentBufferReg = TRI.getPreloadedValue(
1199  Info.setScratchRSrcReg(PrivateSegmentBufferReg);
1200 
1201  unsigned PrivateSegmentWaveByteOffsetReg = TRI.getPreloadedValue(
1203  Info.setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg);
1204  } else {
1205  unsigned ReservedBufferReg
1207  unsigned ReservedOffsetReg
1209 
1210  // We tentatively reserve the last registers (skipping the last two
1211  // which may contain VCC). After register allocation, we'll replace
1212  // these with the ones immediately after those which were really
1213  // allocated. In the prologue copies will be inserted from the argument
1214  // to these reserved registers.
1215  Info.setScratchRSrcReg(ReservedBufferReg);
1216  Info.setScratchWaveOffsetReg(ReservedOffsetReg);
1217  }
1218  } else {
1219  unsigned ReservedBufferReg = TRI.reservedPrivateSegmentBufferReg(MF);
1220 
1221  // Without HSA, relocations are used for the scratch pointer and the
1222  // buffer resource setup is always inserted in the prologue. Scratch wave
1223  // offset is still in an input SGPR.
1224  Info.setScratchRSrcReg(ReservedBufferReg);
1225 
1226  if (HasStackObjects) {
1227  unsigned ScratchWaveOffsetReg = TRI.getPreloadedValue(
1229  Info.setScratchWaveOffsetReg(ScratchWaveOffsetReg);
1230  } else {
1231  unsigned ReservedOffsetReg
1233  Info.setScratchWaveOffsetReg(ReservedOffsetReg);
1234  }
1235  }
1236 }
1237 
1239  SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1240  const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
1241  SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
1242  const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
1243 
1244  MachineFunction &MF = DAG.getMachineFunction();
1245  FunctionType *FType = MF.getFunction()->getFunctionType();
1247  const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
1248 
1249  if (Subtarget->isAmdHsaOS() && AMDGPU::isShader(CallConv)) {
1250  const Function *Fn = MF.getFunction();
1251  DiagnosticInfoUnsupported NoGraphicsHSA(
1252  *Fn, "unsupported non-compute shaders with HSA", DL.getDebugLoc());
1253  DAG.getContext()->diagnose(NoGraphicsHSA);
1254  return DAG.getEntryNode();
1255  }
1256 
1257  // Create stack objects that are used for emitting debugger prologue if
1258  // "amdgpu-debugger-emit-prologue" attribute was specified.
1259  if (ST.debuggerEmitPrologue())
1260  createDebuggerPrologueStackObjects(MF);
1261 
1264  BitVector Skipped(Ins.size());
1265  CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1266  *DAG.getContext());
1267 
1268  bool IsShader = AMDGPU::isShader(CallConv);
1269  bool IsKernel = AMDGPU::isKernel(CallConv);
1270  bool IsEntryFunc = AMDGPU::isEntryFunctionCC(CallConv);
1271 
1272  if (IsShader) {
1273  processShaderInputArgs(Splits, CallConv, Ins, Skipped, FType, Info);
1274 
1275  // At least one interpolation mode must be enabled or else the GPU will
1276  // hang.
1277  //
1278  // Check PSInputAddr instead of PSInputEnable. The idea is that if the user
1279  // set PSInputAddr, the user wants to enable some bits after the compilation
1280  // based on run-time states. Since we can't know what the final PSInputEna
1281  // will look like, so we shouldn't do anything here and the user should take
1282  // responsibility for the correct programming.
1283  //
1284  // Otherwise, the following restrictions apply:
1285  // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled.
1286  // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be
1287  // enabled too.
1288  if (CallConv == CallingConv::AMDGPU_PS &&
1289  ((Info->getPSInputAddr() & 0x7F) == 0 ||
1290  ((Info->getPSInputAddr() & 0xF) == 0 &&
1291  Info->isPSInputAllocated(11)))) {
1292  CCInfo.AllocateReg(AMDGPU::VGPR0);
1293  CCInfo.AllocateReg(AMDGPU::VGPR1);
1294  Info->markPSInputAllocated(0);
1295  Info->markPSInputEnabled(0);
1296  }
1297 
1298  assert(!Info->hasDispatchPtr() &&
1299  !Info->hasKernargSegmentPtr() && !Info->hasFlatScratchInit() &&
1300  !Info->hasWorkGroupIDX() && !Info->hasWorkGroupIDY() &&
1301  !Info->hasWorkGroupIDZ() && !Info->hasWorkGroupInfo() &&
1302  !Info->hasWorkItemIDX() && !Info->hasWorkItemIDY() &&
1303  !Info->hasWorkItemIDZ());
1304  } else if (IsKernel) {
1305  assert(Info->hasWorkGroupIDX() && Info->hasWorkItemIDX());
1306  } else {
1307  Splits.append(Ins.begin(), Ins.end());
1308  }
1309 
1310  if (IsEntryFunc) {
1311  allocateSpecialInputVGPRs(CCInfo, MF, *TRI, *Info);
1312  allocateHSAUserSGPRs(CCInfo, MF, *TRI, *Info);
1313  }
1314 
1315  if (IsKernel) {
1316  analyzeFormalArgumentsCompute(CCInfo, Ins);
1317  } else {
1318  CCAssignFn *AssignFn = CCAssignFnForCall(CallConv, isVarArg);
1319  CCInfo.AnalyzeFormalArguments(Splits, AssignFn);
1320  }
1321 
1322  SmallVector<SDValue, 16> Chains;
1323 
1324  for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
1325  const ISD::InputArg &Arg = Ins[i];
1326  if (Skipped[i]) {
1327  InVals.push_back(DAG.getUNDEF(Arg.VT));
1328  continue;
1329  }
1330 
1331  CCValAssign &VA = ArgLocs[ArgIdx++];
1332  MVT VT = VA.getLocVT();
1333 
1334  if (IsEntryFunc && VA.isMemLoc()) {
1335  VT = Ins[i].VT;
1336  EVT MemVT = VA.getLocVT();
1337 
1338  const uint64_t Offset = Subtarget->getExplicitKernelArgOffset(MF) +
1339  VA.getLocMemOffset();
1340  Info->setABIArgOffset(Offset + MemVT.getStoreSize());
1341 
1342  // The first 36 bytes of the input buffer contains information about
1343  // thread group and global sizes.
1344  SDValue Arg = lowerKernargMemParameter(
1345  DAG, VT, MemVT, DL, Chain, Offset, Ins[i].Flags.isSExt(), &Ins[i]);
1346  Chains.push_back(Arg.getValue(1));
1347 
1348  auto *ParamTy =
1349  dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
1351  ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
1352  // On SI local pointers are just offsets into LDS, so they are always
1353  // less than 16-bits. On CI and newer they could potentially be
1354  // real pointers, so we can't guarantee their size.
1355  Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
1356  DAG.getValueType(MVT::i16));
1357  }
1358 
1359  InVals.push_back(Arg);
1360  continue;
1361  } else if (!IsEntryFunc && VA.isMemLoc()) {
1362  SDValue Val = lowerStackParameter(DAG, VA, DL, Chain, Arg);
1363  InVals.push_back(Val);
1364  if (!Arg.Flags.isByVal())
1365  Chains.push_back(Val.getValue(1));
1366  continue;
1367  }
1368 
1369  assert(VA.isRegLoc() && "Parameter must be in a register!");
1370 
1371  unsigned Reg = VA.getLocReg();
1372  const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
1373  EVT ValVT = VA.getValVT();
1374 
1375  Reg = MF.addLiveIn(Reg, RC);
1376  SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
1377 
1378  // If this is an 8 or 16-bit value, it is really passed promoted
1379  // to 32 bits. Insert an assert[sz]ext to capture this, then
1380  // truncate to the right size.
1381  switch (VA.getLocInfo()) {
1382  case CCValAssign::Full:
1383  break;
1384  case CCValAssign::BCvt:
1385  Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
1386  break;
1387  case CCValAssign::SExt:
1388  Val = DAG.getNode(ISD::AssertSext, DL, VT, Val,
1389  DAG.getValueType(ValVT));
1390  Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
1391  break;
1392  case CCValAssign::ZExt:
1393  Val = DAG.getNode(ISD::AssertZext, DL, VT, Val,
1394  DAG.getValueType(ValVT));
1395  Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
1396  break;
1397  case CCValAssign::AExt:
1398  Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
1399  break;
1400  default:
1401  llvm_unreachable("Unknown loc info!");
1402  }
1403 
1404  if (IsShader && Arg.VT.isVector()) {
1405  // Build a vector from the registers
1406  Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
1407  unsigned NumElements = ParamType->getVectorNumElements();
1408 
1410  Regs.push_back(Val);
1411  for (unsigned j = 1; j != NumElements; ++j) {
1412  Reg = ArgLocs[ArgIdx++].getLocReg();
1413  Reg = MF.addLiveIn(Reg, RC);
1414 
1415  SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
1416  Regs.push_back(Copy);
1417  }
1418 
1419  // Fill up the missing vector elements
1420  NumElements = Arg.VT.getVectorNumElements() - NumElements;
1421  Regs.append(NumElements, DAG.getUNDEF(VT));
1422 
1423  InVals.push_back(DAG.getBuildVector(Arg.VT, DL, Regs));
1424  continue;
1425  }
1426 
1427  InVals.push_back(Val);
1428  }
1429 
1430  // Start adding system SGPRs.
1431  if (IsEntryFunc) {
1432  allocateSystemSGPRs(CCInfo, MF, *Info, CallConv, IsShader);
1433  } else {
1434  CCInfo.AllocateReg(Info->getScratchRSrcReg());
1435  CCInfo.AllocateReg(Info->getScratchWaveOffsetReg());
1436  CCInfo.AllocateReg(Info->getFrameOffsetReg());
1437  }
1438 
1439  return Chains.empty() ? Chain :
1440  DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
1441 }
1442 
1443 // TODO: If return values can't fit in registers, we should return as many as
1444 // possible in registers before passing on stack.
1446  CallingConv::ID CallConv,
1447  MachineFunction &MF, bool IsVarArg,
1448  const SmallVectorImpl<ISD::OutputArg> &Outs,
1449  LLVMContext &Context) const {
1450  // Replacing returns with sret/stack usage doesn't make sense for shaders.
1451  // FIXME: Also sort of a workaround for custom vector splitting in LowerReturn
1452  // for shaders. Vector types should be explicitly handled by CC.
1453  if (AMDGPU::isEntryFunctionCC(CallConv))
1454  return true;
1455 
1457  CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
1458  return CCInfo.CheckReturn(Outs, CCAssignFnForReturn(CallConv, IsVarArg));
1459 }
1460 
1461 SDValue
1463  bool isVarArg,
1464  const SmallVectorImpl<ISD::OutputArg> &Outs,
1465  const SmallVectorImpl<SDValue> &OutVals,
1466  const SDLoc &DL, SelectionDAG &DAG) const {
1467  MachineFunction &MF = DAG.getMachineFunction();
1469 
1470  if (AMDGPU::isKernel(CallConv)) {
1471  return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs,
1472  OutVals, DL, DAG);
1473  }
1474 
1475  bool IsShader = AMDGPU::isShader(CallConv);
1476 
1477  Info->setIfReturnsVoid(Outs.size() == 0);
1478  bool IsWaveEnd = Info->returnsVoid() && IsShader;
1479 
1481  SmallVector<SDValue, 48> SplitVals;
1482 
1483  // Split vectors into their elements.
1484  for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
1485  const ISD::OutputArg &Out = Outs[i];
1486 
1487  if (IsShader && Out.VT.isVector()) {
1488  MVT VT = Out.VT.getVectorElementType();
1489  ISD::OutputArg NewOut = Out;
1490  NewOut.Flags.setSplit();
1491  NewOut.VT = VT;
1492 
1493  // We want the original number of vector elements here, e.g.
1494  // three or five, not four or eight.
1495  unsigned NumElements = Out.ArgVT.getVectorNumElements();
1496 
1497  for (unsigned j = 0; j != NumElements; ++j) {
1498  SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i],
1499  DAG.getConstant(j, DL, MVT::i32));
1500  SplitVals.push_back(Elem);
1501  Splits.push_back(NewOut);
1502  NewOut.PartOffset += NewOut.VT.getStoreSize();
1503  }
1504  } else {
1505  SplitVals.push_back(OutVals[i]);
1506  Splits.push_back(Out);
1507  }
1508  }
1509 
1510  // CCValAssign - represent the assignment of the return value to a location.
1512 
1513  // CCState - Info about the registers and stack slots.
1514  CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1515  *DAG.getContext());
1516 
1517  // Analyze outgoing return values.
1518  CCInfo.AnalyzeReturn(Splits, CCAssignFnForReturn(CallConv, isVarArg));
1519 
1520  SDValue Flag;
1521  SmallVector<SDValue, 48> RetOps;
1522  RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1523 
1524  // Add return address for callable functions.
1525  if (!Info->isEntryFunction()) {
1526  const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
1527  SDValue ReturnAddrReg = CreateLiveInRegister(
1528  DAG, &AMDGPU::SReg_64RegClass, TRI->getReturnAddressReg(MF), MVT::i64);
1529 
1530  // FIXME: Should be able to use a vreg here, but need a way to prevent it
1531  // from being allcoated to a CSR.
1532 
1533  SDValue PhysReturnAddrReg = DAG.getRegister(TRI->getReturnAddressReg(MF),
1534  MVT::i64);
1535 
1536  Chain = DAG.getCopyToReg(Chain, DL, PhysReturnAddrReg, ReturnAddrReg, Flag);
1537  Flag = Chain.getValue(1);
1538 
1539  RetOps.push_back(PhysReturnAddrReg);
1540  }
1541 
1542  // Copy the result values into the output registers.
1543  for (unsigned i = 0, realRVLocIdx = 0;
1544  i != RVLocs.size();
1545  ++i, ++realRVLocIdx) {
1546  CCValAssign &VA = RVLocs[i];
1547  assert(VA.isRegLoc() && "Can only return in registers!");
1548  // TODO: Partially return in registers if return values don't fit.
1549 
1550  SDValue Arg = SplitVals[realRVLocIdx];
1551 
1552  // Copied from other backends.
1553  switch (VA.getLocInfo()) {
1554  case CCValAssign::Full:
1555  break;
1556  case CCValAssign::BCvt:
1557  Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
1558  break;
1559  case CCValAssign::SExt:
1560  Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
1561  break;
1562  case CCValAssign::ZExt:
1563  Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
1564  break;
1565  case CCValAssign::AExt:
1566  Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
1567  break;
1568  default:
1569  llvm_unreachable("Unknown loc info!");
1570  }
1571 
1572  Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
1573  Flag = Chain.getValue(1);
1574  RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1575  }
1576 
1577  // FIXME: Does sret work properly?
1578 
1579  // Update chain and glue.
1580  RetOps[0] = Chain;
1581  if (Flag.getNode())
1582  RetOps.push_back(Flag);
1583 
1584  unsigned Opc = AMDGPUISD::ENDPGM;
1585  if (!IsWaveEnd)
1587  return DAG.getNode(Opc, DL, MVT::Other, RetOps);
1588 }
1589 
1590 unsigned SITargetLowering::getRegisterByName(const char* RegName, EVT VT,
1591  SelectionDAG &DAG) const {
1592  unsigned Reg = StringSwitch<unsigned>(RegName)
1593  .Case("m0", AMDGPU::M0)
1594  .Case("exec", AMDGPU::EXEC)
1595  .Case("exec_lo", AMDGPU::EXEC_LO)
1596  .Case("exec_hi", AMDGPU::EXEC_HI)
1597  .Case("flat_scratch", AMDGPU::FLAT_SCR)
1598  .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO)
1599  .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI)
1600  .Default(AMDGPU::NoRegister);
1601 
1602  if (Reg == AMDGPU::NoRegister) {
1603  report_fatal_error(Twine("invalid register name \""
1604  + StringRef(RegName) + "\"."));
1605 
1606  }
1607 
1609  Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) {
1610  report_fatal_error(Twine("invalid register \""
1611  + StringRef(RegName) + "\" for subtarget."));
1612  }
1613 
1614  switch (Reg) {
1615  case AMDGPU::M0:
1616  case AMDGPU::EXEC_LO:
1617  case AMDGPU::EXEC_HI:
1618  case AMDGPU::FLAT_SCR_LO:
1619  case AMDGPU::FLAT_SCR_HI:
1620  if (VT.getSizeInBits() == 32)
1621  return Reg;
1622  break;
1623  case AMDGPU::EXEC:
1624  case AMDGPU::FLAT_SCR:
1625  if (VT.getSizeInBits() == 64)
1626  return Reg;
1627  break;
1628  default:
1629  llvm_unreachable("missing register type checking");
1630  }
1631 
1632  report_fatal_error(Twine("invalid type for register \""
1633  + StringRef(RegName) + "\"."));
1634 }
1635 
1636 // If kill is not the last instruction, split the block so kill is always a
1637 // proper terminator.
1639  MachineBasicBlock *BB) const {
1640  const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1641 
1642  MachineBasicBlock::iterator SplitPoint(&MI);
1643  ++SplitPoint;
1644 
1645  if (SplitPoint == BB->end()) {
1646  // Don't bother with a new block.
1647  MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR));
1648  return BB;
1649  }
1650 
1651  MachineFunction *MF = BB->getParent();
1652  MachineBasicBlock *SplitBB
1654 
1655  MF->insert(++MachineFunction::iterator(BB), SplitBB);
1656  SplitBB->splice(SplitBB->begin(), BB, SplitPoint, BB->end());
1657 
1658  SplitBB->transferSuccessorsAndUpdatePHIs(BB);
1659  BB->addSuccessor(SplitBB);
1660 
1661  MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR));
1662  return SplitBB;
1663 }
1664 
1665 // Do a v_movrels_b32 or v_movreld_b32 for each unique value of \p IdxReg in the
1666 // wavefront. If the value is uniform and just happens to be in a VGPR, this
1667 // will only do one iteration. In the worst case, this will loop 64 times.
1668 //
1669 // TODO: Just use v_readlane_b32 if we know the VGPR has a uniform value.
1671  const SIInstrInfo *TII,
1672  MachineRegisterInfo &MRI,
1673  MachineBasicBlock &OrigBB,
1674  MachineBasicBlock &LoopBB,
1675  const DebugLoc &DL,
1676  const MachineOperand &IdxReg,
1677  unsigned InitReg,
1678  unsigned ResultReg,
1679  unsigned PhiReg,
1680  unsigned InitSaveExecReg,
1681  int Offset,
1682  bool UseGPRIdxMode) {
1683  MachineBasicBlock::iterator I = LoopBB.begin();
1684 
1685  unsigned PhiExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1686  unsigned NewExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1687  unsigned CurrentIdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1688  unsigned CondReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1689 
1690  BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiReg)
1691  .addReg(InitReg)
1692  .addMBB(&OrigBB)
1693  .addReg(ResultReg)
1694  .addMBB(&LoopBB);
1695 
1696  BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiExec)
1697  .addReg(InitSaveExecReg)
1698  .addMBB(&OrigBB)
1699  .addReg(NewExec)
1700  .addMBB(&LoopBB);
1701 
1702  // Read the next variant <- also loop target.
1703  BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), CurrentIdxReg)
1704  .addReg(IdxReg.getReg(), getUndefRegState(IdxReg.isUndef()));
1705 
1706  // Compare the just read M0 value to all possible Idx values.
1707  BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e64), CondReg)
1708  .addReg(CurrentIdxReg)
1709  .addReg(IdxReg.getReg(), 0, IdxReg.getSubReg());
1710 
1711  if (UseGPRIdxMode) {
1712  unsigned IdxReg;
1713  if (Offset == 0) {
1714  IdxReg = CurrentIdxReg;
1715  } else {
1716  IdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1717  BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), IdxReg)
1718  .addReg(CurrentIdxReg, RegState::Kill)
1719  .addImm(Offset);
1720  }
1721 
1722  MachineInstr *SetIdx =
1723  BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_IDX))
1724  .addReg(IdxReg, RegState::Kill);
1725  SetIdx->getOperand(2).setIsUndef();
1726  } else {
1727  // Move index from VCC into M0
1728  if (Offset == 0) {
1729  BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
1730  .addReg(CurrentIdxReg, RegState::Kill);
1731  } else {
1732  BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
1733  .addReg(CurrentIdxReg, RegState::Kill)
1734  .addImm(Offset);
1735  }
1736  }
1737 
1738  // Update EXEC, save the original EXEC value to VCC.
1739  BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), NewExec)
1740  .addReg(CondReg, RegState::Kill);
1741 
1742  MRI.setSimpleHint(NewExec, CondReg);
1743 
1744  // Update EXEC, switch all done bits to 0 and all todo bits to 1.
1745  MachineInstr *InsertPt =
1746  BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
1747  .addReg(AMDGPU::EXEC)
1748  .addReg(NewExec);
1749 
1750  // XXX - s_xor_b64 sets scc to 1 if the result is nonzero, so can we use
1751  // s_cbranch_scc0?
1752 
1753  // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover.
1754  BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
1755  .addMBB(&LoopBB);
1756 
1757  return InsertPt->getIterator();
1758 }
1759 
1760 // This has slightly sub-optimal regalloc when the source vector is killed by
1761 // the read. The register allocator does not understand that the kill is
1762 // per-workitem, so is kept alive for the whole loop so we end up not re-using a
1763 // subregister from it, using 1 more VGPR than necessary. This was saved when
1764 // this was expanded after register allocation.
1766  MachineBasicBlock &MBB,
1767  MachineInstr &MI,
1768  unsigned InitResultReg,
1769  unsigned PhiReg,
1770  int Offset,
1771  bool UseGPRIdxMode) {
1772  MachineFunction *MF = MBB.getParent();
1773  MachineRegisterInfo &MRI = MF->getRegInfo();
1774  const DebugLoc &DL = MI.getDebugLoc();
1776 
1777  unsigned DstReg = MI.getOperand(0).getReg();
1778  unsigned SaveExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1779  unsigned TmpExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1780 
1781  BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), TmpExec);
1782 
1783  // Save the EXEC mask
1784  BuildMI(MBB, I, DL, TII->get(AMDGPU::S_MOV_B64), SaveExec)
1785  .addReg(AMDGPU::EXEC);
1786 
1787  // To insert the loop we need to split the block. Move everything after this
1788  // point to a new block, and insert a new empty block between the two.
1790  MachineBasicBlock *RemainderBB = MF->CreateMachineBasicBlock();
1791  MachineFunction::iterator MBBI(MBB);
1792  ++MBBI;
1793 
1794  MF->insert(MBBI, LoopBB);
1795  MF->insert(MBBI, RemainderBB);
1796 
1797  LoopBB->addSuccessor(LoopBB);
1798  LoopBB->addSuccessor(RemainderBB);
1799 
1800  // Move the rest of the block into a new block.
1801  RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB);
1802  RemainderBB->splice(RemainderBB->begin(), &MBB, I, MBB.end());
1803 
1804  MBB.addSuccessor(LoopBB);
1805 
1806  const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1807 
1808  auto InsPt = emitLoadM0FromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, *Idx,
1809  InitResultReg, DstReg, PhiReg, TmpExec,
1810  Offset, UseGPRIdxMode);
1811 
1812  MachineBasicBlock::iterator First = RemainderBB->begin();
1813  BuildMI(*RemainderBB, First, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
1814  .addReg(SaveExec);
1815 
1816  return InsPt;
1817 }
1818 
1819 // Returns subreg index, offset
1820 static std::pair<unsigned, int>
1822  const TargetRegisterClass *SuperRC,
1823  unsigned VecReg,
1824  int Offset) {
1825  int NumElts = TRI.getRegSizeInBits(*SuperRC) / 32;
1826 
1827  // Skip out of bounds offsets, or else we would end up using an undefined
1828  // register.
1829  if (Offset >= NumElts || Offset < 0)
1830  return std::make_pair(AMDGPU::sub0, Offset);
1831 
1832  return std::make_pair(AMDGPU::sub0 + Offset, 0);
1833 }
1834 
1835 // Return true if the index is an SGPR and was set.
1837  MachineRegisterInfo &MRI,
1838  MachineInstr &MI,
1839  int Offset,
1840  bool UseGPRIdxMode,
1841  bool IsIndirectSrc) {
1842  MachineBasicBlock *MBB = MI.getParent();
1843  const DebugLoc &DL = MI.getDebugLoc();
1845 
1846  const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1847  const TargetRegisterClass *IdxRC = MRI.getRegClass(Idx->getReg());
1848 
1849  assert(Idx->getReg() != AMDGPU::NoRegister);
1850 
1851  if (!TII->getRegisterInfo().isSGPRClass(IdxRC))
1852  return false;
1853 
1854  if (UseGPRIdxMode) {
1855  unsigned IdxMode = IsIndirectSrc ?
1857  if (Offset == 0) {
1858  MachineInstr *SetOn =
1859  BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
1860  .add(*Idx)
1861  .addImm(IdxMode);
1862 
1863  SetOn->getOperand(3).setIsUndef();
1864  } else {
1865  unsigned Tmp = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
1866  BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), Tmp)
1867  .add(*Idx)
1868  .addImm(Offset);
1869  MachineInstr *SetOn =
1870  BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
1871  .addReg(Tmp, RegState::Kill)
1872  .addImm(IdxMode);
1873 
1874  SetOn->getOperand(3).setIsUndef();
1875  }
1876 
1877  return true;
1878  }
1879 
1880  if (Offset == 0) {
1881  BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
1882  .add(*Idx);
1883  } else {
1884  BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
1885  .add(*Idx)
1886  .addImm(Offset);
1887  }
1888 
1889  return true;
1890 }
1891 
1892 // Control flow needs to be inserted if indexing with a VGPR.
1894  MachineBasicBlock &MBB,
1895  const SISubtarget &ST) {
1896  const SIInstrInfo *TII = ST.getInstrInfo();
1897  const SIRegisterInfo &TRI = TII->getRegisterInfo();
1898  MachineFunction *MF = MBB.getParent();
1899  MachineRegisterInfo &MRI = MF->getRegInfo();
1900 
1901  unsigned Dst = MI.getOperand(0).getReg();
1902  unsigned SrcReg = TII->getNamedOperand(MI, AMDGPU::OpName::src)->getReg();
1903  int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
1904 
1905  const TargetRegisterClass *VecRC = MRI.getRegClass(SrcReg);
1906 
1907  unsigned SubReg;
1908  std::tie(SubReg, Offset)
1909  = computeIndirectRegAndOffset(TRI, VecRC, SrcReg, Offset);
1910 
1911  bool UseGPRIdxMode = ST.useVGPRIndexMode(EnableVGPRIndexMode);
1912 
1913  if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, true)) {
1915  const DebugLoc &DL = MI.getDebugLoc();
1916 
1917  if (UseGPRIdxMode) {
1918  // TODO: Look at the uses to avoid the copy. This may require rescheduling
1919  // to avoid interfering with other uses, so probably requires a new
1920  // optimization pass.
1921  BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst)
1922  .addReg(SrcReg, RegState::Undef, SubReg)
1923  .addReg(SrcReg, RegState::Implicit)
1924  .addReg(AMDGPU::M0, RegState::Implicit);
1925  BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
1926  } else {
1927  BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
1928  .addReg(SrcReg, RegState::Undef, SubReg)
1929  .addReg(SrcReg, RegState::Implicit);
1930  }
1931 
1932  MI.eraseFromParent();
1933 
1934  return &MBB;
1935  }
1936 
1937  const DebugLoc &DL = MI.getDebugLoc();
1939 
1940  unsigned PhiReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1941  unsigned InitReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1942 
1943  BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), InitReg);
1944 
1945  if (UseGPRIdxMode) {
1946  MachineInstr *SetOn = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
1947  .addImm(0) // Reset inside loop.
1949  SetOn->getOperand(3).setIsUndef();
1950 
1951  // Disable again after the loop.
1952  BuildMI(MBB, std::next(I), DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
1953  }
1954 
1955  auto InsPt = loadM0FromVGPR(TII, MBB, MI, InitReg, PhiReg, Offset, UseGPRIdxMode);
1956  MachineBasicBlock *LoopBB = InsPt->getParent();
1957 
1958  if (UseGPRIdxMode) {
1959  BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst)
1960  .addReg(SrcReg, RegState::Undef, SubReg)
1961  .addReg(SrcReg, RegState::Implicit)
1962  .addReg(AMDGPU::M0, RegState::Implicit);
1963  } else {
1964  BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
1965  .addReg(SrcReg, RegState::Undef, SubReg)
1966  .addReg(SrcReg, RegState::Implicit);
1967  }
1968 
1969  MI.eraseFromParent();
1970 
1971  return LoopBB;
1972 }
1973 
1974 static unsigned getMOVRELDPseudo(const SIRegisterInfo &TRI,
1975  const TargetRegisterClass *VecRC) {
1976  switch (TRI.getRegSizeInBits(*VecRC)) {
1977  case 32: // 4 bytes
1978  return AMDGPU::V_MOVRELD_B32_V1;
1979  case 64: // 8 bytes
1980  return AMDGPU::V_MOVRELD_B32_V2;
1981  case 128: // 16 bytes
1982  return AMDGPU::V_MOVRELD_B32_V4;
1983  case 256: // 32 bytes
1984  return AMDGPU::V_MOVRELD_B32_V8;
1985  case 512: // 64 bytes
1986  return AMDGPU::V_MOVRELD_B32_V16;
1987  default:
1988  llvm_unreachable("unsupported size for MOVRELD pseudos");
1989  }
1990 }
1991 
1993  MachineBasicBlock &MBB,
1994  const SISubtarget &ST) {
1995  const SIInstrInfo *TII = ST.getInstrInfo();
1996  const SIRegisterInfo &TRI = TII->getRegisterInfo();
1997  MachineFunction *MF = MBB.getParent();
1998  MachineRegisterInfo &MRI = MF->getRegInfo();
1999 
2000  unsigned Dst = MI.getOperand(0).getReg();
2001  const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src);
2002  const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
2003  const MachineOperand *Val = TII->getNamedOperand(MI, AMDGPU::OpName::val);
2004  int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
2005  const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg());
2006 
2007  // This can be an immediate, but will be folded later.
2008  assert(Val->getReg());
2009 
2010  unsigned SubReg;
2011  std::tie(SubReg, Offset) = computeIndirectRegAndOffset(TRI, VecRC,
2012  SrcVec->getReg(),
2013  Offset);
2014  bool UseGPRIdxMode = ST.useVGPRIndexMode(EnableVGPRIndexMode);
2015 
2016  if (Idx->getReg() == AMDGPU::NoRegister) {
2018  const DebugLoc &DL = MI.getDebugLoc();
2019 
2020  assert(Offset == 0);
2021 
2022  BuildMI(MBB, I, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dst)
2023  .add(*SrcVec)
2024  .add(*Val)
2025  .addImm(SubReg);
2026 
2027  MI.eraseFromParent();
2028  return &MBB;
2029  }
2030 
2031  if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, false)) {
2033  const DebugLoc &DL = MI.getDebugLoc();
2034 
2035  if (UseGPRIdxMode) {
2036  BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_indirect))
2037  .addReg(SrcVec->getReg(), RegState::Undef, SubReg) // vdst
2038  .add(*Val)
2039  .addReg(Dst, RegState::ImplicitDefine)
2040  .addReg(SrcVec->getReg(), RegState::Implicit)
2041  .addReg(AMDGPU::M0, RegState::Implicit);
2042 
2043  BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
2044  } else {
2045  const MCInstrDesc &MovRelDesc = TII->get(getMOVRELDPseudo(TRI, VecRC));
2046 
2047  BuildMI(MBB, I, DL, MovRelDesc)
2048  .addReg(Dst, RegState::Define)
2049  .addReg(SrcVec->getReg())
2050  .add(*Val)
2051  .addImm(SubReg - AMDGPU::sub0);
2052  }
2053 
2054  MI.eraseFromParent();
2055  return &MBB;
2056  }
2057 
2058  if (Val->isReg())
2059  MRI.clearKillFlags(Val->getReg());
2060 
2061  const DebugLoc &DL = MI.getDebugLoc();
2062 
2063  if (UseGPRIdxMode) {
2065 
2066  MachineInstr *SetOn = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
2067  .addImm(0) // Reset inside loop.
2069  SetOn->getOperand(3).setIsUndef();
2070 
2071  // Disable again after the loop.
2072  BuildMI(MBB, std::next(I), DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
2073  }
2074 
2075  unsigned PhiReg = MRI.createVirtualRegister(VecRC);
2076 
2077  auto InsPt = loadM0FromVGPR(TII, MBB, MI, SrcVec->getReg(), PhiReg,
2078  Offset, UseGPRIdxMode);
2079  MachineBasicBlock *LoopBB = InsPt->getParent();
2080 
2081  if (UseGPRIdxMode) {
2082  BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOV_B32_indirect))
2083  .addReg(PhiReg, RegState::Undef, SubReg) // vdst
2084  .add(*Val) // src0
2086  .addReg(PhiReg, RegState::Implicit)
2087  .addReg(AMDGPU::M0, RegState::Implicit);
2088  } else {
2089  const MCInstrDesc &MovRelDesc = TII->get(getMOVRELDPseudo(TRI, VecRC));
2090 
2091  BuildMI(*LoopBB, InsPt, DL, MovRelDesc)
2092  .addReg(Dst, RegState::Define)
2093  .addReg(PhiReg)
2094  .add(*Val)
2095  .addImm(SubReg - AMDGPU::sub0);
2096  }
2097 
2098  MI.eraseFromParent();
2099 
2100  return LoopBB;
2101 }
2102 
2104  MachineInstr &MI, MachineBasicBlock *BB) const {
2105 
2106  const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
2107  MachineFunction *MF = BB->getParent();
2109 
2110  if (TII->isMIMG(MI)) {
2111  if (!MI.memoperands_empty())
2112  return BB;
2113  // Add a memoperand for mimg instructions so that they aren't assumed to
2114  // be ordered memory instuctions.
2115 
2116  MachinePointerInfo PtrInfo(MFI->getImagePSV());
2118  if (MI.mayStore())
2119  Flags |= MachineMemOperand::MOStore;
2120 
2121  if (MI.mayLoad())
2122  Flags |= MachineMemOperand::MOLoad;
2123 
2124  auto MMO = MF->getMachineMemOperand(PtrInfo, Flags, 0, 0);
2125  MI.addMemOperand(*MF, MMO);
2126  return BB;
2127  }
2128 
2129  switch (MI.getOpcode()) {
2130  case AMDGPU::SI_INIT_M0:
2131  BuildMI(*BB, MI.getIterator(), MI.getDebugLoc(),
2132  TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
2133  .add(MI.getOperand(0));
2134  MI.eraseFromParent();
2135  return BB;
2136 
2137  case AMDGPU::SI_INIT_EXEC:
2138  // This should be before all vector instructions.
2139  BuildMI(*BB, &*BB->begin(), MI.getDebugLoc(), TII->get(AMDGPU::S_MOV_B64),
2140  AMDGPU::EXEC)
2141  .addImm(MI.getOperand(0).getImm());
2142  MI.eraseFromParent();
2143  return BB;
2144 
2145  case AMDGPU::SI_INIT_EXEC_FROM_INPUT: {
2146  // Extract the thread count from an SGPR input and set EXEC accordingly.
2147  // Since BFM can't shift by 64, handle that case with CMP + CMOV.
2148  //
2149  // S_BFE_U32 count, input, {shift, 7}
2150  // S_BFM_B64 exec, count, 0
2151  // S_CMP_EQ_U32 count, 64
2152  // S_CMOV_B64 exec, -1
2153  MachineInstr *FirstMI = &*BB->begin();
2154  MachineRegisterInfo &MRI = MF->getRegInfo();
2155  unsigned InputReg = MI.getOperand(0).getReg();
2156  unsigned CountReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
2157  bool Found = false;
2158 
2159  // Move the COPY of the input reg to the beginning, so that we can use it.
2160  for (auto I = BB->begin(); I != &MI; I++) {
2161  if (I->getOpcode() != TargetOpcode::COPY ||
2162  I->getOperand(0).getReg() != InputReg)
2163  continue;
2164 
2165  if (I == FirstMI) {
2166  FirstMI = &*++BB->begin();
2167  } else {
2168  I->removeFromParent();
2169  BB->insert(FirstMI, &*I);
2170  }
2171  Found = true;
2172  break;
2173  }
2174  assert(Found);
2175  (void)Found;
2176 
2177  // This should be before all vector instructions.
2178  BuildMI(*BB, FirstMI, DebugLoc(), TII->get(AMDGPU::S_BFE_U32), CountReg)
2179  .addReg(InputReg)
2180  .addImm((MI.getOperand(1).getImm() & 0x7f) | 0x70000);
2181  BuildMI(*BB, FirstMI, DebugLoc(), TII->get(AMDGPU::S_BFM_B64),
2182  AMDGPU::EXEC)
2183  .addReg(CountReg)
2184  .addImm(0);
2185  BuildMI(*BB, FirstMI, DebugLoc(), TII->get(AMDGPU::S_CMP_EQ_U32))
2186  .addReg(CountReg, RegState::Kill)
2187  .addImm(64);
2188  BuildMI(*BB, FirstMI, DebugLoc(), TII->get(AMDGPU::S_CMOV_B64),
2189  AMDGPU::EXEC)
2190  .addImm(-1);
2191  MI.eraseFromParent();
2192  return BB;
2193  }
2194 
2195  case AMDGPU::GET_GROUPSTATICSIZE: {
2196  DebugLoc DL = MI.getDebugLoc();
2197  BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_MOV_B32))
2198  .add(MI.getOperand(0))
2199  .addImm(MFI->getLDSSize());
2200  MI.eraseFromParent();
2201  return BB;
2202  }
2203  case AMDGPU::SI_INDIRECT_SRC_V1:
2204  case AMDGPU::SI_INDIRECT_SRC_V2:
2205  case AMDGPU::SI_INDIRECT_SRC_V4:
2206  case AMDGPU::SI_INDIRECT_SRC_V8:
2207  case AMDGPU::SI_INDIRECT_SRC_V16:
2208  return emitIndirectSrc(MI, *BB, *getSubtarget());
2209  case AMDGPU::SI_INDIRECT_DST_V1:
2210  case AMDGPU::SI_INDIRECT_DST_V2:
2211  case AMDGPU::SI_INDIRECT_DST_V4:
2212  case AMDGPU::SI_INDIRECT_DST_V8:
2213  case AMDGPU::SI_INDIRECT_DST_V16:
2214  return emitIndirectDst(MI, *BB, *getSubtarget());
2215  case AMDGPU::SI_KILL:
2216  return splitKillBlock(MI, BB);
2217  case AMDGPU::V_CNDMASK_B64_PSEUDO: {
2218  MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
2219 
2220  unsigned Dst = MI.getOperand(0).getReg();
2221  unsigned Src0 = MI.getOperand(1).getReg();
2222  unsigned Src1 = MI.getOperand(2).getReg();
2223  const DebugLoc &DL = MI.getDebugLoc();
2224  unsigned SrcCond = MI.getOperand(3).getReg();
2225 
2226  unsigned DstLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
2227  unsigned DstHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
2228 
2229  BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstLo)
2230  .addReg(Src0, 0, AMDGPU::sub0)
2231  .addReg(Src1, 0, AMDGPU::sub0)
2232  .addReg(SrcCond);
2233  BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstHi)
2234  .addReg(Src0, 0, AMDGPU::sub1)
2235  .addReg(Src1, 0, AMDGPU::sub1)
2236  .addReg(SrcCond);
2237 
2238  BuildMI(*BB, MI, DL, TII->get(AMDGPU::REG_SEQUENCE), Dst)
2239  .addReg(DstLo)
2240  .addImm(AMDGPU::sub0)
2241  .addReg(DstHi)
2242  .addImm(AMDGPU::sub1);
2243  MI.eraseFromParent();
2244  return BB;
2245  }
2246  case AMDGPU::SI_BR_UNDEF: {
2247  const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
2248  const DebugLoc &DL = MI.getDebugLoc();
2249  MachineInstr *Br = BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_CBRANCH_SCC1))
2250  .add(MI.getOperand(0));
2251  Br->getOperand(1).setIsUndef(true); // read undef SCC
2252  MI.eraseFromParent();
2253  return BB;
2254  }
2255  default:
2257  }
2258 }
2259 
2261  // This currently forces unfolding various combinations of fsub into fma with
2262  // free fneg'd operands. As long as we have fast FMA (controlled by
2263  // isFMAFasterThanFMulAndFAdd), we should perform these.
2264 
2265  // When fma is quarter rate, for f64 where add / sub are at best half rate,
2266  // most of these combines appear to be cycle neutral but save on instruction
2267  // count / code size.
2268  return true;
2269 }
2270 
2272  EVT VT) const {
2273  if (!VT.isVector()) {
2274  return MVT::i1;
2275  }
2276  return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
2277 }
2278 
2280  // TODO: Should i16 be used always if legal? For now it would force VALU
2281  // shifts.
2282  return (VT == MVT::i16) ? MVT::i16 : MVT::i32;
2283 }
2284 
2285 // Answering this is somewhat tricky and depends on the specific device which
2286 // have different rates for fma or all f64 operations.
2287 //
2288 // v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
2289 // regardless of which device (although the number of cycles differs between
2290 // devices), so it is always profitable for f64.
2291 //
2292 // v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
2293 // only on full rate devices. Normally, we should prefer selecting v_mad_f32
2294 // which we can always do even without fused FP ops since it returns the same
2295 // result as the separate operations and since it is always full
2296 // rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
2297 // however does not support denormals, so we do report fma as faster if we have
2298 // a fast fma device and require denormals.
2299 //
2301  VT = VT.getScalarType();
2302 
2303  switch (VT.getSimpleVT().SimpleTy) {
2304  case MVT::f32:
2305  // This is as fast on some subtargets. However, we always have full rate f32
2306  // mad available which returns the same result as the separate operations
2307  // which we should prefer over fma. We can't use this if we want to support
2308  // denormals, so only report this in these cases.
2310  case MVT::f64:
2311  return true;
2312  case MVT::f16:
2314  default:
2315  break;
2316  }
2317 
2318  return false;
2319 }
2320 
2321 //===----------------------------------------------------------------------===//
2322 // Custom DAG Lowering Operations
2323 //===----------------------------------------------------------------------===//
2324 
2326  switch (Op.getOpcode()) {
2327  default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
2328  case ISD::BRCOND: return LowerBRCOND(Op, DAG);
2329  case ISD::LOAD: {
2330  SDValue Result = LowerLOAD(Op, DAG);
2331  assert((!Result.getNode() ||
2332  Result.getNode()->getNumValues() == 2) &&
2333  "Load should return a value and a chain");
2334  return Result;
2335  }
2336 
2337  case ISD::FSIN:
2338  case ISD::FCOS:
2339  return LowerTrig(Op, DAG);
2340  case ISD::SELECT: return LowerSELECT(Op, DAG);
2341  case ISD::FDIV: return LowerFDIV(Op, DAG);
2342  case ISD::ATOMIC_CMP_SWAP: return LowerATOMIC_CMP_SWAP(Op, DAG);
2343  case ISD::STORE: return LowerSTORE(Op, DAG);
2344  case ISD::GlobalAddress: {
2345  MachineFunction &MF = DAG.getMachineFunction();
2347  return LowerGlobalAddress(MFI, Op, DAG);
2348  }
2349  case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
2350  case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
2351  case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
2352  case ISD::ADDRSPACECAST: return lowerADDRSPACECAST(Op, DAG);
2354  return lowerINSERT_VECTOR_ELT(Op, DAG);
2356  return lowerEXTRACT_VECTOR_ELT(Op, DAG);
2357  case ISD::FP_ROUND:
2358  return lowerFP_ROUND(Op, DAG);
2359 
2360  case ISD::TRAP:
2361  case ISD::DEBUGTRAP:
2362  return lowerTRAP(Op, DAG);
2363  }
2364  return SDValue();
2365 }
2366 
2369  SelectionDAG &DAG) const {
2370  switch (N->getOpcode()) {
2371  case ISD::INSERT_VECTOR_ELT: {
2372  if (SDValue Res = lowerINSERT_VECTOR_ELT(SDValue(N, 0), DAG))
2373  Results.push_back(Res);
2374  return;
2375  }
2376  case ISD::EXTRACT_VECTOR_ELT: {
2377  if (SDValue Res = lowerEXTRACT_VECTOR_ELT(SDValue(N, 0), DAG))
2378  Results.push_back(Res);
2379  return;
2380  }
2381  case ISD::INTRINSIC_WO_CHAIN: {
2382  unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
2383  if (IID == Intrinsic::amdgcn_cvt_pkrtz) {
2384  SDValue Src0 = N->getOperand(1);
2385  SDValue Src1 = N->getOperand(2);
2386  SDLoc SL(N);
2388  Src0, Src1);
2389  Results.push_back(DAG.getNode(ISD::BITCAST, SL, MVT::v2f16, Cvt));
2390  return;
2391  }
2392  break;
2393  }
2394  case ISD::SELECT: {
2395  SDLoc SL(N);
2396  EVT VT = N->getValueType(0);
2397  EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT);
2398  SDValue LHS = DAG.getNode(ISD::BITCAST, SL, NewVT, N->getOperand(1));
2399  SDValue RHS = DAG.getNode(ISD::BITCAST, SL, NewVT, N->getOperand(2));
2400 
2401  EVT SelectVT = NewVT;
2402  if (NewVT.bitsLT(MVT::i32)) {
2403  LHS = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, LHS);
2404  RHS = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, RHS);
2405  SelectVT = MVT::i32;
2406  }
2407 
2408  SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, SelectVT,
2409  N->getOperand(0), LHS, RHS);
2410 
2411  if (NewVT != SelectVT)
2412  NewSelect = DAG.getNode(ISD::TRUNCATE, SL, NewVT, NewSelect);
2413  Results.push_back(DAG.getNode(ISD::BITCAST, SL, VT, NewSelect));
2414  return;
2415  }
2416  default:
2417  break;
2418  }
2419 }
2420 
2421 /// \brief Helper function for LowerBRCOND
2422 static SDNode *findUser(SDValue Value, unsigned Opcode) {
2423 
2424  SDNode *Parent = Value.getNode();
2425  for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
2426  I != E; ++I) {
2427 
2428  if (I.getUse().get() != Value)
2429  continue;
2430 
2431  if (I->getOpcode() == Opcode)
2432  return *I;
2433  }
2434  return nullptr;
2435 }
2436 
2437 unsigned SITargetLowering::isCFIntrinsic(const SDNode *Intr) const {
2438  if (Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN) {
2439  switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) {
2440  case Intrinsic::amdgcn_if:
2441  return AMDGPUISD::IF;
2442  case Intrinsic::amdgcn_else:
2443  return AMDGPUISD::ELSE;
2444  case Intrinsic::amdgcn_loop:
2445  return AMDGPUISD::LOOP;
2446  case Intrinsic::amdgcn_end_cf:
2447  llvm_unreachable("should not occur");
2448  default:
2449  return 0;
2450  }
2451  }
2452 
2453  // break, if_break, else_break are all only used as inputs to loop, not
2454  // directly as branch conditions.
2455  return 0;
2456 }
2457 
2458 void SITargetLowering::createDebuggerPrologueStackObjects(
2459  MachineFunction &MF) const {
2460  // Create stack objects that are used for emitting debugger prologue.
2461  //
2462  // Debugger prologue writes work group IDs and work item IDs to scratch memory
2463  // at fixed location in the following format:
2464  // offset 0: work group ID x
2465  // offset 4: work group ID y
2466  // offset 8: work group ID z
2467  // offset 16: work item ID x
2468  // offset 20: work item ID y
2469  // offset 24: work item ID z
2471  int ObjectIdx = 0;
2472 
2473  // For each dimension:
2474  for (unsigned i = 0; i < 3; ++i) {
2475  // Create fixed stack object for work group ID.
2476  ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4, true);
2477  Info->setDebuggerWorkGroupIDStackObjectIndex(i, ObjectIdx);
2478  // Create fixed stack object for work item ID.
2479  ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4 + 16, true);
2480  Info->setDebuggerWorkItemIDStackObjectIndex(i, ObjectIdx);
2481  }
2482 }
2483 
2484 bool SITargetLowering::shouldEmitFixup(const GlobalValue *GV) const {
2485  const Triple &TT = getTargetMachine().getTargetTriple();
2486  return GV->getType()->getAddressSpace() == AMDGPUASI.CONSTANT_ADDRESS &&
2488 }
2489 
2490 bool SITargetLowering::shouldEmitGOTReloc(const GlobalValue *GV) const {
2491  return (GV->getType()->getAddressSpace() == AMDGPUASI.GLOBAL_ADDRESS ||
2493  !shouldEmitFixup(GV) &&
2495 }
2496 
2497 bool SITargetLowering::shouldEmitPCReloc(const GlobalValue *GV) const {
2498  return !shouldEmitFixup(GV) && !shouldEmitGOTReloc(GV);
2499 }
2500 
2501 /// This transforms the control flow intrinsics to get the branch destination as
2502 /// last parameter, also switches branch target with BR if the need arise
2503 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
2504  SelectionDAG &DAG) const {
2505  SDLoc DL(BRCOND);
2506 
2507  SDNode *Intr = BRCOND.getOperand(1).getNode();
2508  SDValue Target = BRCOND.getOperand(2);
2509  SDNode *BR = nullptr;
2510  SDNode *SetCC = nullptr;
2511 
2512  if (Intr->getOpcode() == ISD::SETCC) {
2513  // As long as we negate the condition everything is fine
2514  SetCC = Intr;
2515  Intr = SetCC->getOperand(0).getNode();
2516 
2517  } else {
2518  // Get the target from BR if we don't negate the condition
2519  BR = findUser(BRCOND, ISD::BR);
2520  Target = BR->getOperand(1);
2521  }
2522 
2523  // FIXME: This changes the types of the intrinsics instead of introducing new
2524  // nodes with the correct types.
2525  // e.g. llvm.amdgcn.loop
2526 
2527  // eg: i1,ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3
2528  // => t9: ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3, BasicBlock:ch<bb1 0x7fee5286d088>
2529 
2530  unsigned CFNode = isCFIntrinsic(Intr);
2531  if (CFNode == 0) {
2532  // This is a uniform branch so we don't need to legalize.
2533  return BRCOND;
2534  }
2535 
2536  bool HaveChain = Intr->getOpcode() == ISD::INTRINSIC_VOID ||
2537  Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN;
2538 
2539  assert(!SetCC ||
2540  (SetCC->getConstantOperandVal(1) == 1 &&
2541  cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
2542  ISD::SETNE));
2543 
2544  // operands of the new intrinsic call
2546  if (HaveChain)
2547  Ops.push_back(BRCOND.getOperand(0));
2548 
2549  Ops.append(Intr->op_begin() + (HaveChain ? 2 : 1), Intr->op_end());
2550  Ops.push_back(Target);
2551 
2552  ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
2553 
2554  // build the new intrinsic call
2555  SDNode *Result = DAG.getNode(CFNode, DL, DAG.getVTList(Res), Ops).getNode();
2556 
2557  if (!HaveChain) {
2558  SDValue Ops[] = {
2559  SDValue(Result, 0),
2560  BRCOND.getOperand(0)
2561  };
2562 
2563  Result = DAG.getMergeValues(Ops, DL).getNode();
2564  }
2565 
2566  if (BR) {
2567  // Give the branch instruction our target
2568  SDValue Ops[] = {
2569  BR->getOperand(0),
2570  BRCOND.getOperand(2)
2571  };
2572  SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
2573  DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
2574  BR = NewBR.getNode();
2575  }
2576 
2577  SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
2578 
2579  // Copy the intrinsic results to registers
2580  for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
2582  if (!CopyToReg)
2583  continue;
2584 
2585  Chain = DAG.getCopyToReg(
2586  Chain, DL,
2587  CopyToReg->getOperand(1),
2588  SDValue(Result, i - 1),
2589  SDValue());
2590 
2591  DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
2592  }
2593 
2594  // Remove the old intrinsic from the chain
2596  SDValue(Intr, Intr->getNumValues() - 1),
2597  Intr->getOperand(0));
2598 
2599  return Chain;
2600 }
2601 
2602 SDValue SITargetLowering::getFPExtOrFPTrunc(SelectionDAG &DAG,
2603  SDValue Op,
2604  const SDLoc &DL,
2605  EVT VT) const {
2606  return Op.getValueType().bitsLE(VT) ?
2607  DAG.getNode(ISD::FP_EXTEND, DL, VT, Op) :
2608  DAG.getNode(ISD::FTRUNC, DL, VT, Op);
2609 }
2610 
2611 SDValue SITargetLowering::lowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
2612  assert(Op.getValueType() == MVT::f16 &&
2613  "Do not know how to custom lower FP_ROUND for non-f16 type");
2614 
2615  SDValue Src = Op.getOperand(0);
2616  EVT SrcVT = Src.getValueType();
2617  if (SrcVT != MVT::f64)
2618  return Op;
2619 
2620  SDLoc DL(Op);
2621 
2622  SDValue FpToFp16 = DAG.getNode(ISD::FP_TO_FP16, DL, MVT::i32, Src);
2623  SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, FpToFp16);
2624  return DAG.getNode(ISD::BITCAST, DL, MVT::f16, Trunc);
2625 }
2626 
2627 SDValue SITargetLowering::lowerTRAP(SDValue Op, SelectionDAG &DAG) const {
2628  SDLoc SL(Op);
2629  MachineFunction &MF = DAG.getMachineFunction();
2630  SDValue Chain = Op.getOperand(0);
2631 
2632  unsigned TrapID = Op.getOpcode() == ISD::DEBUGTRAP ?
2634 
2638  unsigned UserSGPR = Info->getQueuePtrUserSGPR();
2639  assert(UserSGPR != AMDGPU::NoRegister);
2640 
2642  DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64);
2643 
2644  SDValue SGPR01 = DAG.getRegister(AMDGPU::SGPR0_SGPR1, MVT::i64);
2645 
2646  SDValue ToReg = DAG.getCopyToReg(Chain, SL, SGPR01,
2647  QueuePtr, SDValue());
2648 
2649  SDValue Ops[] = {
2650  ToReg,
2651  DAG.getTargetConstant(TrapID, SL, MVT::i16),
2652  SGPR01,
2653  ToReg.getValue(1)
2654  };
2655 
2656  return DAG.getNode(AMDGPUISD::TRAP, SL, MVT::Other, Ops);
2657  }
2658 
2659  switch (TrapID) {
2661  return DAG.getNode(AMDGPUISD::ENDPGM, SL, MVT::Other, Chain);
2664  "debugtrap handler not supported",
2665  Op.getDebugLoc(),
2666  DS_Warning);
2667  LLVMContext &Ctx = MF.getFunction()->getContext();
2668  Ctx.diagnose(NoTrap);
2669  return Chain;
2670  }
2671  default:
2672  llvm_unreachable("unsupported trap handler type!");
2673  }
2674 
2675  return Chain;
2676 }
2677 
2678 SDValue SITargetLowering::getSegmentAperture(unsigned AS, const SDLoc &DL,
2679  SelectionDAG &DAG) const {
2680  // FIXME: Use inline constants (src_{shared, private}_base) instead.
2681  if (Subtarget->hasApertureRegs()) {
2682  unsigned Offset = AS == AMDGPUASI.LOCAL_ADDRESS ?
2685  unsigned WidthM1 = AS == AMDGPUASI.LOCAL_ADDRESS ?
2688  unsigned Encoding =
2690  Offset << AMDGPU::Hwreg::OFFSET_SHIFT_ |
2691  WidthM1 << AMDGPU::Hwreg::WIDTH_M1_SHIFT_;
2692 
2693  SDValue EncodingImm = DAG.getTargetConstant(Encoding, DL, MVT::i16);
2694  SDValue ApertureReg = SDValue(
2695  DAG.getMachineNode(AMDGPU::S_GETREG_B32, DL, MVT::i32, EncodingImm), 0);
2696  SDValue ShiftAmount = DAG.getTargetConstant(WidthM1 + 1, DL, MVT::i32);
2697  return DAG.getNode(ISD::SHL, DL, MVT::i32, ApertureReg, ShiftAmount);
2698  }
2699 
2700  MachineFunction &MF = DAG.getMachineFunction();
2702  unsigned UserSGPR = Info->getQueuePtrUserSGPR();
2703  assert(UserSGPR != AMDGPU::NoRegister);
2704 
2705  SDValue QueuePtr = CreateLiveInRegister(
2706  DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64);
2707 
2708  // Offset into amd_queue_t for group_segment_aperture_base_hi /
2709  // private_segment_aperture_base_hi.
2710  uint32_t StructOffset = (AS == AMDGPUASI.LOCAL_ADDRESS) ? 0x40 : 0x44;
2711 
2712  SDValue Ptr = DAG.getNode(ISD::ADD, DL, MVT::i64, QueuePtr,
2713  DAG.getConstant(StructOffset, DL, MVT::i64));
2714 
2715  // TODO: Use custom target PseudoSourceValue.
2716  // TODO: We should use the value from the IR intrinsic call, but it might not
2717  // be available and how do we get it?
2720 
2721  MachinePointerInfo PtrInfo(V, StructOffset);
2722  return DAG.getLoad(MVT::i32, DL, QueuePtr.getValue(1), Ptr, PtrInfo,
2723  MinAlign(64, StructOffset),
2726 }
2727 
2728 SDValue SITargetLowering::lowerADDRSPACECAST(SDValue Op,
2729  SelectionDAG &DAG) const {
2730  SDLoc SL(Op);
2731  const AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(Op);
2732 
2733  SDValue Src = ASC->getOperand(0);
2734  SDValue FlatNullPtr = DAG.getConstant(0, SL, MVT::i64);
2735 
2736  const AMDGPUTargetMachine &TM =
2737  static_cast<const AMDGPUTargetMachine &>(getTargetMachine());
2738 
2739  // flat -> local/private
2740  if (ASC->getSrcAddressSpace() == AMDGPUASI.FLAT_ADDRESS) {
2741  unsigned DestAS = ASC->getDestAddressSpace();
2742 
2743  if (DestAS == AMDGPUASI.LOCAL_ADDRESS ||
2744  DestAS == AMDGPUASI.PRIVATE_ADDRESS) {
2745  unsigned NullVal = TM.getNullPointerValue(DestAS);
2746  SDValue SegmentNullPtr = DAG.getConstant(NullVal, SL, MVT::i32);
2747  SDValue NonNull = DAG.getSetCC(SL, MVT::i1, Src, FlatNullPtr, ISD::SETNE);
2748  SDValue Ptr = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src);
2749 
2750  return DAG.getNode(ISD::SELECT, SL, MVT::i32,
2751  NonNull, Ptr, SegmentNullPtr);
2752  }
2753  }
2754 
2755  // local/private -> flat
2756  if (ASC->getDestAddressSpace() == AMDGPUASI.FLAT_ADDRESS) {
2757  unsigned SrcAS = ASC->getSrcAddressSpace();
2758 
2759  if (SrcAS == AMDGPUASI.LOCAL_ADDRESS ||
2760  SrcAS == AMDGPUASI.PRIVATE_ADDRESS) {
2761  unsigned NullVal = TM.getNullPointerValue(SrcAS);
2762  SDValue SegmentNullPtr = DAG.getConstant(NullVal, SL, MVT::i32);
2763 
2764  SDValue NonNull
2765  = DAG.getSetCC(SL, MVT::i1, Src, SegmentNullPtr, ISD::SETNE);
2766 
2767  SDValue Aperture = getSegmentAperture(ASC->getSrcAddressSpace(), SL, DAG);
2768  SDValue CvtPtr
2769  = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, Aperture);
2770 
2771  return DAG.getNode(ISD::SELECT, SL, MVT::i64, NonNull,
2772  DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr),
2773  FlatNullPtr);
2774  }
2775  }
2776 
2777  // global <-> flat are no-ops and never emitted.
2778 
2779  const MachineFunction &MF = DAG.getMachineFunction();
2780  DiagnosticInfoUnsupported InvalidAddrSpaceCast(
2781  *MF.getFunction(), "invalid addrspacecast", SL.getDebugLoc());
2782  DAG.getContext()->diagnose(InvalidAddrSpaceCast);
2783 
2784  return DAG.getUNDEF(ASC->getValueType(0));
2785 }
2786 
2787 SDValue SITargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
2788  SelectionDAG &DAG) const {
2789  SDValue Idx = Op.getOperand(2);
2790  if (isa<ConstantSDNode>(Idx))
2791  return SDValue();
2792 
2793  // Avoid stack access for dynamic indexing.
2794  SDLoc SL(Op);
2795  SDValue Vec = Op.getOperand(0);
2796  SDValue Val = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Op.getOperand(1));
2797 
2798  // v_bfi_b32 (v_bfm_b32 16, (shl idx, 16)), val, vec
2799  SDValue ExtVal = DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i32, Val);
2800 
2801  // Convert vector index to bit-index.
2802  SDValue ScaledIdx = DAG.getNode(ISD::SHL, SL, MVT::i32, Idx,
2803  DAG.getConstant(16, SL, MVT::i32));
2804 
2805  SDValue BCVec = DAG.getNode(ISD::BITCAST, SL, MVT::i32, Vec);
2806 
2807  SDValue BFM = DAG.getNode(ISD::SHL, SL, MVT::i32,
2808  DAG.getConstant(0xffff, SL, MVT::i32),
2809  ScaledIdx);
2810 
2811  SDValue LHS = DAG.getNode(ISD::AND, SL, MVT::i32, BFM, ExtVal);
2812  SDValue RHS = DAG.getNode(ISD::AND, SL, MVT::i32,
2813  DAG.getNOT(SL, BFM, MVT::i32), BCVec);
2814 
2815  SDValue BFI = DAG.getNode(ISD::OR, SL, MVT::i32, LHS, RHS);
2816  return DAG.getNode(ISD::BITCAST, SL, Op.getValueType(), BFI);
2817 }
2818 
2819 SDValue SITargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
2820  SelectionDAG &DAG) const {
2821  SDLoc SL(Op);
2822 
2823  EVT ResultVT = Op.getValueType();
2824  SDValue Vec = Op.getOperand(0);
2825  SDValue Idx = Op.getOperand(1);
2826 
2827  DAGCombinerInfo DCI(DAG, AfterLegalizeVectorOps, true, nullptr);
2828 
2829  // Make sure we we do any optimizations that will make it easier to fold
2830  // source modifiers before obscuring it with bit operations.
2831 
2832  // XXX - Why doesn't this get called when vector_shuffle is expanded?
2833  if (SDValue Combined = performExtractVectorEltCombine(Op.getNode(), DCI))
2834  return Combined;
2835 
2836  if (const ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
2837  SDValue Result = DAG.getNode(ISD::BITCAST, SL, MVT::i32, Vec);
2838 
2839  if (CIdx->getZExtValue() == 1) {
2840  Result = DAG.getNode(ISD::SRL, SL, MVT::i32, Result,
2841  DAG.getConstant(16, SL, MVT::i32));
2842  } else {
2843  assert(CIdx->getZExtValue() == 0);
2844  }
2845 
2846  if (ResultVT.bitsLT(MVT::i32))
2847  Result = DAG.getNode(ISD::TRUNCATE, SL, MVT::i16, Result);
2848  return DAG.getNode(ISD::BITCAST, SL, ResultVT, Result);
2849  }
2850 
2851  SDValue Sixteen = DAG.getConstant(16, SL, MVT::i32);
2852 
2853  // Convert vector index to bit-index.
2854  SDValue ScaledIdx = DAG.getNode(ISD::SHL, SL, MVT::i32, Idx, Sixteen);
2855 
2856  SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::i32, Vec);
2857  SDValue Elt = DAG.getNode(ISD::SRL, SL, MVT::i32, BC, ScaledIdx);
2858 
2859  SDValue Result = Elt;
2860  if (ResultVT.bitsLT(MVT::i32))
2861  Result = DAG.getNode(ISD::TRUNCATE, SL, MVT::i16, Result);
2862 
2863  return DAG.getNode(ISD::BITCAST, SL, ResultVT, Result);
2864 }
2865 
2866 bool
2868  // We can fold offsets for anything that doesn't require a GOT relocation.
2869  return (GA->getAddressSpace() == AMDGPUASI.GLOBAL_ADDRESS ||
2871  !shouldEmitGOTReloc(GA->getGlobal());
2872 }
2873 
2874 static SDValue
2876  const SDLoc &DL, unsigned Offset, EVT PtrVT,
2877  unsigned GAFlags = SIInstrInfo::MO_NONE) {
2878  // In order to support pc-relative addressing, the PC_ADD_REL_OFFSET SDNode is
2879  // lowered to the following code sequence:
2880  //
2881  // For constant address space:
2882  // s_getpc_b64 s[0:1]
2883  // s_add_u32 s0, s0, $symbol
2884  // s_addc_u32 s1, s1, 0
2885  //
2886  // s_getpc_b64 returns the address of the s_add_u32 instruction and then
2887  // a fixup or relocation is emitted to replace $symbol with a literal
2888  // constant, which is a pc-relative offset from the encoding of the $symbol
2889  // operand to the global variable.
2890  //
2891  // For global address space:
2892  // s_getpc_b64 s[0:1]
2893  // s_add_u32 s0, s0, $symbol@{gotpc}rel32@lo
2894  // s_addc_u32 s1, s1, $symbol@{gotpc}rel32@hi
2895  //
2896  // s_getpc_b64 returns the address of the s_add_u32 instruction and then
2897  // fixups or relocations are emitted to replace $symbol@*@lo and
2898  // $symbol@*@hi with lower 32 bits and higher 32 bits of a literal constant,
2899  // which is a 64-bit pc-relative offset from the encoding of the $symbol
2900  // operand to the global variable.
2901  //
2902  // What we want here is an offset from the value returned by s_getpc
2903  // (which is the address of the s_add_u32 instruction) to the global
2904  // variable, but since the encoding of $symbol starts 4 bytes after the start
2905  // of the s_add_u32 instruction, we end up with an offset that is 4 bytes too
2906  // small. This requires us to add 4 to the global variable offset in order to
2907  // compute the correct address.
2908  SDValue PtrLo = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4,
2909  GAFlags);
2910  SDValue PtrHi = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4,
2911  GAFlags == SIInstrInfo::MO_NONE ?
2912  GAFlags : GAFlags + 1);
2913  return DAG.getNode(AMDGPUISD::PC_ADD_REL_OFFSET, DL, PtrVT, PtrLo, PtrHi);
2914 }
2915 
2916 SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
2917  SDValue Op,
2918  SelectionDAG &DAG) const {
2919  GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
2920 
2921  if (GSD->getAddressSpace() != AMDGPUASI.CONSTANT_ADDRESS &&
2923  return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
2924 
2925  SDLoc DL(GSD);
2926  const GlobalValue *GV = GSD->getGlobal();
2927  EVT PtrVT = Op.getValueType();
2928 
2929  if (shouldEmitFixup(GV))
2930  return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT);
2931  else if (shouldEmitPCReloc(GV))
2932  return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT,
2934 
2935  SDValue GOTAddr = buildPCRelGlobalAddress(DAG, GV, DL, 0, PtrVT,
2937 
2938  Type *Ty = PtrVT.getTypeForEVT(*DAG.getContext());
2940  const DataLayout &DataLayout = DAG.getDataLayout();
2941  unsigned Align = DataLayout.getABITypeAlignment(PtrTy);
2942  // FIXME: Use a PseudoSourceValue once those can be assigned an address space.
2943  MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
2944 
2945  return DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), GOTAddr, PtrInfo, Align,
2948 }
2949 
2951  const SDLoc &DL, SDValue V) const {
2952  // We can't use S_MOV_B32 directly, because there is no way to specify m0 as
2953  // the destination register.
2954  //
2955  // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
2956  // so we will end up with redundant moves to m0.
2957  //
2958  // We use a pseudo to ensure we emit s_mov_b32 with m0 as the direct result.
2959 
2960  // A Null SDValue creates a glue result.
2961  SDNode *M0 = DAG.getMachineNode(AMDGPU::SI_INIT_M0, DL, MVT::Other, MVT::Glue,
2962  V, Chain);
2963  return SDValue(M0, 0);
2964 }
2965 
2966 SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG,
2967  SDValue Op,
2968  MVT VT,
2969  unsigned Offset) const {
2970  SDLoc SL(Op);
2971  SDValue Param = lowerKernargMemParameter(DAG, MVT::i32, MVT::i32, SL,
2972  DAG.getEntryNode(), Offset, false);
2973  // The local size values will have the hi 16-bits as zero.
2974  return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
2975  DAG.getValueType(VT));
2976 }
2977 
2979  EVT VT) {
2981  "non-hsa intrinsic with hsa target",
2982  DL.getDebugLoc());
2983  DAG.getContext()->diagnose(BadIntrin);
2984  return DAG.getUNDEF(VT);
2985 }
2986 
2988  EVT VT) {
2990  "intrinsic not supported on subtarget",
2991  DL.getDebugLoc());
2992  DAG.getContext()->diagnose(BadIntrin);
2993  return DAG.getUNDEF(VT);
2994 }
2995 
2996 SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
2997  SelectionDAG &DAG) const {
2998  MachineFunction &MF = DAG.getMachineFunction();
2999  auto MFI = MF.getInfo<SIMachineFunctionInfo>();
3000  const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
3001 
3002  EVT VT = Op.getValueType();
3003  SDLoc DL(Op);
3004  unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3005 
3006  // TODO: Should this propagate fast-math-flags?
3007 
3008  switch (IntrinsicID) {
3009  case Intrinsic::amdgcn_implicit_buffer_ptr: {
3010  if (getSubtarget()->isAmdCodeObjectV2(MF))
3011  return emitNonHSAIntrinsicError(DAG, DL, VT);
3012 
3013  unsigned Reg = TRI->getPreloadedValue(MF,
3015  return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT);
3016  }
3017  case Intrinsic::amdgcn_dispatch_ptr:
3018  case Intrinsic::amdgcn_queue_ptr: {
3019  if (!Subtarget->isAmdCodeObjectV2(MF)) {
3020  DiagnosticInfoUnsupported BadIntrin(
3021  *MF.getFunction(), "unsupported hsa intrinsic without hsa target",
3022  DL.getDebugLoc());
3023  DAG.getContext()->diagnose(BadIntrin);
3024  return DAG.getUNDEF(VT);
3025  }
3026 
3027  auto Reg = IntrinsicID == Intrinsic::amdgcn_dispatch_ptr ?
3029  return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass,
3030  TRI->getPreloadedValue(MF, Reg), VT);
3031  }
3032  case Intrinsic::amdgcn_implicitarg_ptr: {
3033  unsigned offset = getImplicitParameterOffset(MFI, FIRST_IMPLICIT);
3034  return lowerKernArgParameterPtr(DAG, DL, DAG.getEntryNode(), offset);
3035  }
3036  case Intrinsic::amdgcn_kernarg_segment_ptr: {
3037  unsigned Reg
3038  = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
3039  return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT);
3040  }
3041  case Intrinsic::amdgcn_dispatch_id: {
3042  unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_ID);
3043  return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT);
3044  }
3045  case Intrinsic::amdgcn_rcp:
3046  return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1));
3047  case Intrinsic::amdgcn_rsq:
3048  return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
3049  case Intrinsic::amdgcn_rsq_legacy:
3051  return emitRemovedIntrinsicError(DAG, DL, VT);
3052 
3053  return DAG.getNode(AMDGPUISD::RSQ_LEGACY, DL, VT, Op.getOperand(1));
3054  case Intrinsic::amdgcn_rcp_legacy:
3056  return emitRemovedIntrinsicError(DAG, DL, VT);
3057  return DAG.getNode(AMDGPUISD::RCP_LEGACY, DL, VT, Op.getOperand(1));
3058  case Intrinsic::amdgcn_rsq_clamp: {
3060  return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
3061 
3062  Type *Type = VT.getTypeForEVT(*DAG.getContext());
3065 
3066  SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
3067  SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq,
3068  DAG.getConstantFP(Max, DL, VT));
3069  return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp,
3070  DAG.getConstantFP(Min, DL, VT));
3071  }
3072  case Intrinsic::r600_read_ngroups_x:
3073  if (Subtarget->isAmdHsaOS())
3074  return emitNonHSAIntrinsicError(DAG, DL, VT);
3075 
3076  return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
3078  case Intrinsic::r600_read_ngroups_y:
3079  if (Subtarget->isAmdHsaOS())
3080  return emitNonHSAIntrinsicError(DAG, DL, VT);
3081 
3082  return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
3084  case Intrinsic::r600_read_ngroups_z:
3085  if (Subtarget->isAmdHsaOS())
3086  return emitNonHSAIntrinsicError(DAG, DL, VT);
3087 
3088  return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
3090  case Intrinsic::r600_read_global_size_x:
3091  if (Subtarget->isAmdHsaOS())
3092  return emitNonHSAIntrinsicError(DAG, DL, VT);
3093 
3094  return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
3096  case Intrinsic::r600_read_global_size_y:
3097  if (Subtarget->isAmdHsaOS())
3098  return emitNonHSAIntrinsicError(DAG, DL, VT);
3099 
3100  return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
3102  case Intrinsic::r600_read_global_size_z:
3103  if (Subtarget->isAmdHsaOS())
3104  return emitNonHSAIntrinsicError(DAG, DL, VT);
3105 
3106  return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
3108  case Intrinsic::r600_read_local_size_x:
3109  if (Subtarget->isAmdHsaOS())
3110  return emitNonHSAIntrinsicError(DAG, DL, VT);
3111 
3112  return lowerImplicitZextParam(DAG, Op, MVT::i16,
3114  case Intrinsic::r600_read_local_size_y:
3115  if (Subtarget->isAmdHsaOS())
3116  return emitNonHSAIntrinsicError(DAG, DL, VT);
3117 
3118  return lowerImplicitZextParam(DAG, Op, MVT::i16,
3120  case Intrinsic::r600_read_local_size_z:
3121  if (Subtarget->isAmdHsaOS())
3122  return emitNonHSAIntrinsicError(DAG, DL, VT);
3123 
3124  return lowerImplicitZextParam(DAG, Op, MVT::i16,
3126  case Intrinsic::amdgcn_workgroup_id_x:
3127  case Intrinsic::r600_read_tgid_x:
3128  return CreateLiveInRegister(DAG, &AMDGPU::SReg_32_XM0RegClass,
3129  TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT);
3130  case Intrinsic::amdgcn_workgroup_id_y:
3131  case Intrinsic::r600_read_tgid_y:
3132  return CreateLiveInRegister(DAG, &AMDGPU::SReg_32_XM0RegClass,
3133  TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT);
3134  case Intrinsic::amdgcn_workgroup_id_z:
3135  case Intrinsic::r600_read_tgid_z:
3136  return CreateLiveInRegister(DAG, &AMDGPU::SReg_32_XM0RegClass,
3137  TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT);
3138  case Intrinsic::amdgcn_workitem_id_x:
3139  case Intrinsic::r600_read_tidig_x:
3140  return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
3141  TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT);
3142  case Intrinsic::amdgcn_workitem_id_y:
3143  case Intrinsic::r600_read_tidig_y:
3144  return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
3145  TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT);
3146  case Intrinsic::amdgcn_workitem_id_z:
3147  case Intrinsic::r600_read_tidig_z:
3148  return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
3149  TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT);
3150  case AMDGPUIntrinsic::SI_load_const: {
3151  SDValue Ops[] = {
3152  Op.getOperand(1),
3153  Op.getOperand(2)
3154  };
3155 
3160  VT.getStoreSize(), 4);
3162  Op->getVTList(), Ops, VT, MMO);
3163  }
3164  case Intrinsic::amdgcn_fdiv_fast:
3165  return lowerFDIV_FAST(Op, DAG);
3166  case Intrinsic::amdgcn_interp_mov: {
3167  SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4));
3168  SDValue Glue = M0.getValue(1);
3169  return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32, Op.getOperand(1),
3170  Op.getOperand(2), Op.getOperand(3), Glue);
3171  }
3172  case Intrinsic::amdgcn_interp_p1: {
3173  SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4));
3174  SDValue Glue = M0.getValue(1);
3175  return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1),
3176  Op.getOperand(2), Op.getOperand(3), Glue);
3177  }
3178  case Intrinsic::amdgcn_interp_p2: {
3179  SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5));
3180  SDValue Glue = SDValue(M0.getNode(), 1);
3181  return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1),
3182  Op.getOperand(2), Op.getOperand(3), Op.getOperand(4),
3183  Glue);
3184  }
3185  case Intrinsic::amdgcn_sin:
3186  return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1));
3187 
3188  case Intrinsic::amdgcn_cos:
3189  return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1));
3190 
3191  case Intrinsic::amdgcn_log_clamp: {
3193  return SDValue();
3194 
3195  DiagnosticInfoUnsupported BadIntrin(
3196  *MF.getFunction(), "intrinsic not supported on subtarget",
3197  DL.getDebugLoc());
3198  DAG.getContext()->diagnose(BadIntrin);
3199  return DAG.getUNDEF(VT);
3200  }
3201  case Intrinsic::amdgcn_ldexp:
3202  return DAG.getNode(AMDGPUISD::LDEXP, DL, VT,
3203  Op.getOperand(1), Op.getOperand(2));
3204 
3205  case Intrinsic::amdgcn_fract:
3206  return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
3207 
3208  case Intrinsic::amdgcn_class:
3209  return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT,
3210  Op.getOperand(1), Op.getOperand(2));
3211  case Intrinsic::amdgcn_div_fmas:
3212  return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT,
3213  Op.getOperand(1), Op.getOperand(2), Op.getOperand(3),
3214  Op.getOperand(4));
3215 
3216  case Intrinsic::amdgcn_div_fixup:
3217  return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT,
3218  Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3219 
3220  case Intrinsic::amdgcn_trig_preop:
3221  return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT,
3222  Op.getOperand(1), Op.getOperand(2));
3223  case Intrinsic::amdgcn_div_scale: {
3224  // 3rd parameter required to be a constant.
3225  const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3226  if (!Param)
3227  return DAG.getUNDEF(VT);
3228 
3229  // Translate to the operands expected by the machine instruction. The
3230  // first parameter must be the same as the first instruction.
3231  SDValue Numerator = Op.getOperand(1);
3232  SDValue Denominator = Op.getOperand(2);
3233 
3234  // Note this order is opposite of the machine instruction's operations,
3235  // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The
3236  // intrinsic has the numerator as the first operand to match a normal
3237  // division operation.
3238 
3239  SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator;
3240 
3241  return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0,
3242  Denominator, Numerator);
3243  }
3244  case Intrinsic::amdgcn_icmp: {
3245  const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3246  if (!CD)
3247  return DAG.getUNDEF(VT);
3248 
3249  int CondCode = CD->getSExtValue();
3250  if (CondCode < ICmpInst::Predicate::FIRST_ICMP_PREDICATE ||
3251  CondCode > ICmpInst::Predicate::LAST_ICMP_PREDICATE)
3252  return DAG.getUNDEF(VT);
3253 
3254  ICmpInst::Predicate IcInput = static_cast<ICmpInst::Predicate>(CondCode);
3255  ISD::CondCode CCOpcode = getICmpCondCode(IcInput);
3256  return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1),
3257  Op.getOperand(2), DAG.getCondCode(CCOpcode));
3258  }
3259  case Intrinsic::amdgcn_fcmp: {
3260  const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3261  if (!CD)
3262  return DAG.getUNDEF(VT);
3263 
3264  int CondCode = CD->getSExtValue();
3265  if (CondCode < FCmpInst::Predicate::FIRST_FCMP_PREDICATE ||
3266  CondCode > FCmpInst::Predicate::LAST_FCMP_PREDICATE)
3267  return DAG.getUNDEF(VT);
3268 
3269  FCmpInst::Predicate IcInput = static_cast<FCmpInst::Predicate>(CondCode);
3270  ISD::CondCode CCOpcode = getFCmpCondCode(IcInput);
3271  return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1),
3272  Op.getOperand(2), DAG.getCondCode(CCOpcode));
3273  }
3274  case Intrinsic::amdgcn_fmed3:
3275  return DAG.getNode(AMDGPUISD::FMED3, DL, VT,
3276  Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3277  case Intrinsic::amdgcn_fmul_legacy:
3278  return DAG.getNode(AMDGPUISD::FMUL_LEGACY, DL, VT,
3279  Op.getOperand(1), Op.getOperand(2));
3280  case Intrinsic::amdgcn_sffbh:
3281  return DAG.getNode(AMDGPUISD::FFBH_I32, DL, VT, Op.getOperand(1));
3282  case Intrinsic::amdgcn_sbfe:
3283  return DAG.getNode(AMDGPUISD::BFE_I32, DL, VT,
3284  Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3285  case Intrinsic::amdgcn_ubfe:
3286  return DAG.getNode(AMDGPUISD::BFE_U32, DL, VT,
3287  Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3288  case Intrinsic::amdgcn_cvt_pkrtz: {
3289  // FIXME: Stop adding cast if v2f16 legal.
3290  EVT VT = Op.getValueType();
3292  Op.getOperand(1), Op.getOperand(2));
3293  return DAG.getNode(ISD::BITCAST, DL, VT, Node);
3294  }
3295  default:
3296  return Op;
3297  }
3298 }
3299 
3300 SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
3301  SelectionDAG &DAG) const {
3302  unsigned IntrID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
3303  SDLoc DL(Op);
3304  MachineFunction &MF = DAG.getMachineFunction();
3305 
3306  switch (IntrID) {
3307  case Intrinsic::amdgcn_atomic_inc:
3308  case Intrinsic::amdgcn_atomic_dec: {
3309  MemSDNode *M = cast<MemSDNode>(Op);
3310  unsigned Opc = (IntrID == Intrinsic::amdgcn_atomic_inc) ?
3312  SDValue Ops[] = {
3313  M->getOperand(0), // Chain
3314  M->getOperand(2), // Ptr
3315  M->getOperand(3) // Value
3316  };
3317 
3318  return DAG.getMemIntrinsicNode(Opc, SDLoc(Op), M->getVTList(), Ops,
3319  M->getMemoryVT(), M->getMemOperand());
3320  }
3321  case Intrinsic::amdgcn_buffer_load:
3322  case Intrinsic::amdgcn_buffer_load_format: {
3323  SDValue Ops[] = {
3324  Op.getOperand(0), // Chain
3325  Op.getOperand(2), // rsrc
3326  Op.getOperand(3), // vindex
3327  Op.getOperand(4), // offset
3328  Op.getOperand(5), // glc
3329  Op.getOperand(6) // slc
3330  };
3332 
3333  unsigned Opc = (IntrID == Intrinsic::amdgcn_buffer_load) ?
3335  EVT VT = Op.getValueType();
3336  EVT IntVT = VT.changeTypeToInteger();
3337 
3341  VT.getStoreSize(), VT.getStoreSize());
3342 
3343  return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops, IntVT, MMO);
3344  }
3345  case Intrinsic::amdgcn_tbuffer_load: {
3346  SDValue Ops[] = {
3347  Op.getOperand(0), // Chain
3348  Op.getOperand(2), // rsrc
3349  Op.getOperand(3), // vindex
3350  Op.getOperand(4), // voffset
3351  Op.getOperand(5), // soffset
3352  Op.getOperand(6), // offset
3353  Op.getOperand(7), // dfmt
3354  Op.getOperand(8), // nfmt
3355  Op.getOperand(9), // glc
3356  Op.getOperand(10) // slc
3357  };
3358 
3359  EVT VT = Op.getOperand(2).getValueType();
3360 
3364  VT.getStoreSize(), VT.getStoreSize());
3366  Op->getVTList(), Ops, VT, MMO);
3367  }
3368  // Basic sample.
3369  case Intrinsic::amdgcn_image_sample:
3370  case Intrinsic::amdgcn_image_sample_cl:
3371  case Intrinsic::amdgcn_image_sample_d:
3372  case Intrinsic::amdgcn_image_sample_d_cl:
3373  case Intrinsic::amdgcn_image_sample_l:
3374  case Intrinsic::amdgcn_image_sample_b:
3375  case Intrinsic::amdgcn_image_sample_b_cl:
3376  case Intrinsic::amdgcn_image_sample_lz:
3377  case Intrinsic::amdgcn_image_sample_cd:
3378  case Intrinsic::amdgcn_image_sample_cd_cl:
3379 
3380  // Sample with comparison.
3381  case Intrinsic::amdgcn_image_sample_c:
3382  case Intrinsic::amdgcn_image_sample_c_cl:
3383  case Intrinsic::amdgcn_image_sample_c_d:
3384  case Intrinsic::amdgcn_image_sample_c_d_cl:
3385  case Intrinsic::amdgcn_image_sample_c_l:
3386  case Intrinsic::amdgcn_image_sample_c_b:
3387  case Intrinsic::amdgcn_image_sample_c_b_cl:
3388  case Intrinsic::amdgcn_image_sample_c_lz:
3389  case Intrinsic::amdgcn_image_sample_c_cd:
3390  case Intrinsic::amdgcn_image_sample_c_cd_cl:
3391 
3392  // Sample with offsets.
3393  case Intrinsic::amdgcn_image_sample_o:
3394  case Intrinsic::amdgcn_image_sample_cl_o:
3395  case Intrinsic::amdgcn_image_sample_d_o:
3396  case Intrinsic::amdgcn_image_sample_d_cl_o:
3397  case Intrinsic::amdgcn_image_sample_l_o:
3398  case Intrinsic::amdgcn_image_sample_b_o:
3399  case Intrinsic::amdgcn_image_sample_b_cl_o:
3400  case Intrinsic::amdgcn_image_sample_lz_o:
3401  case Intrinsic::amdgcn_image_sample_cd_o:
3402  case Intrinsic::amdgcn_image_sample_cd_cl_o:
3403 
3404  // Sample with comparison and offsets.
3405  case Intrinsic::amdgcn_image_sample_c_o:
3406  case Intrinsic::amdgcn_image_sample_c_cl_o:
3407  case Intrinsic::amdgcn_image_sample_c_d_o:
3408  case Intrinsic::amdgcn_image_sample_c_d_cl_o:
3409  case Intrinsic::amdgcn_image_sample_c_l_o:
3410  case Intrinsic::amdgcn_image_sample_c_b_o:
3411  case Intrinsic::amdgcn_image_sample_c_b_cl_o:
3412  case Intrinsic::amdgcn_image_sample_c_lz_o:
3413  case Intrinsic::amdgcn_image_sample_c_cd_o:
3414  case Intrinsic::amdgcn_image_sample_c_cd_cl_o:
3415 
3416  case Intrinsic::amdgcn_image_getlod: {
3417  // Replace dmask with everything disabled with undef.
3418  const ConstantSDNode *DMask = dyn_cast<ConstantSDNode>(Op.getOperand(5));
3419  if (!DMask || DMask->isNullValue()) {
3420  SDValue Undef = DAG.getUNDEF(Op.getValueType());
3421  return DAG.getMergeValues({ Undef, Op.getOperand(0) }, SDLoc(Op));
3422  }
3423 
3424  return SDValue();
3425  }
3426  default:
3427  return SDValue();
3428  }
3429 }
3430 
3431 SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
3432  SelectionDAG &DAG) const {
3433  SDLoc DL(Op);
3434  SDValue Chain = Op.getOperand(0);
3435  unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
3436  MachineFunction &MF = DAG.getMachineFunction();
3437 
3438  switch (IntrinsicID) {
3439  case Intrinsic::amdgcn_exp: {
3440  const ConstantSDNode *Tgt = cast<ConstantSDNode>(Op.getOperand(2));
3441  const ConstantSDNode *En = cast<ConstantSDNode>(Op.getOperand(3));
3442  const ConstantSDNode *Done = cast<ConstantSDNode>(Op.getOperand(8));
3443  const ConstantSDNode *VM = cast<ConstantSDNode>(Op.getOperand(9));
3444 
3445  const SDValue Ops[] = {
3446  Chain,
3447  DAG.getTargetConstant(Tgt->getZExtValue(), DL, MVT::i8), // tgt
3448  DAG.getTargetConstant(En->getZExtValue(), DL, MVT::i8), // en
3449  Op.getOperand(4), // src0
3450  Op.getOperand(5), // src1
3451  Op.getOperand(6), // src2
3452  Op.getOperand(7), // src3
3453  DAG.getTargetConstant(0, DL, MVT::i1), // compr
3454  DAG.getTargetConstant(VM->getZExtValue(), DL, MVT::i1)
3455  };
3456 
3457  unsigned Opc = Done->isNullValue() ?
3459  return DAG.getNode(Opc, DL, Op->getVTList(), Ops);
3460  }
3461  case Intrinsic::amdgcn_exp_compr: {
3462  const ConstantSDNode *Tgt = cast<ConstantSDNode>(Op.getOperand(2));
3463  const ConstantSDNode *En = cast<ConstantSDNode>(Op.getOperand(3));
3464  SDValue Src0 = Op.getOperand(4);
3465  SDValue Src1 = Op.getOperand(5);
3466  const ConstantSDNode *Done = cast<ConstantSDNode>(Op.getOperand(6));
3467  const ConstantSDNode *VM = cast<ConstantSDNode>(Op.getOperand(7));
3468 
3469  SDValue Undef = DAG.getUNDEF(MVT::f32);
3470  const SDValue Ops[] = {
3471  Chain,
3472  DAG.getTargetConstant(Tgt->getZExtValue(), DL, MVT::i8), // tgt
3473  DAG.getTargetConstant(En->getZExtValue(), DL, MVT::i8), // en
3474  DAG.getNode(ISD::BITCAST, DL, MVT::f32, Src0),
3475  DAG.getNode(ISD::BITCAST, DL, MVT::f32, Src1),
3476  Undef, // src2
3477  Undef, // src3
3478  DAG.getTargetConstant(1, DL, MVT::i1), // compr
3479  DAG.getTargetConstant(VM->getZExtValue(), DL, MVT::i1)
3480  };
3481 
3482  unsigned Opc = Done->isNullValue() ?
3484  return DAG.getNode(Opc, DL, Op->getVTList(), Ops);
3485  }
3486  case Intrinsic::amdgcn_s_sendmsg:
3487  case Intrinsic::amdgcn_s_sendmsghalt: {
3488  unsigned NodeOp = (IntrinsicID == Intrinsic::amdgcn_s_sendmsg) ?
3490  Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
3491  SDValue Glue = Chain.getValue(1);
3492  return DAG.getNode(NodeOp, DL, MVT::Other, Chain,
3493  Op.getOperand(2), Glue);
3494  }
3495  case Intrinsic::amdgcn_init_exec: {
3496  return DAG.getNode(AMDGPUISD::INIT_EXEC, DL, MVT::Other, Chain,
3497  Op.getOperand(2));
3498  }
3499  case Intrinsic::amdgcn_init_exec_from_input: {
3500  return DAG.getNode(AMDGPUISD::INIT_EXEC_FROM_INPUT, DL, MVT::Other, Chain,
3501  Op.getOperand(2), Op.getOperand(3));
3502  }
3503  case AMDGPUIntrinsic::AMDGPU_kill: {
3504  SDValue Src = Op.getOperand(2);
3505  if (const ConstantFPSDNode *K = dyn_cast<ConstantFPSDNode>(Src)) {
3506  if (!K->isNegative())
3507  return Chain;
3508 
3509  SDValue NegOne = DAG.getTargetConstant(FloatToBits(-1.0f), DL, MVT::i32);
3510  return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, NegOne);
3511  }
3512 
3513  SDValue Cast = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Src);
3514  return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, Cast);
3515  }
3516  case Intrinsic::amdgcn_s_barrier: {
3517  if (getTargetMachine().getOptLevel() > CodeGenOpt::None) {
3518  const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
3519  unsigned WGSize = ST.getFlatWorkGroupSizes(*MF.getFunction()).second;
3520  if (WGSize <= ST.getWavefrontSize())
3521  return SDValue(DAG.getMachineNode(AMDGPU::WAVE_BARRIER, DL, MVT::Other,
3522  Op.getOperand(0)), 0);
3523  }
3524  return SDValue();
3525  };
3526  case AMDGPUIntrinsic::SI_tbuffer_store: {
3527 
3528  // Extract vindex and voffset from vaddr as appropriate
3529  const ConstantSDNode *OffEn = cast<ConstantSDNode>(Op.getOperand(10));
3530  const ConstantSDNode *IdxEn = cast<ConstantSDNode>(Op.getOperand(11));
3531  SDValue VAddr = Op.getOperand(5);
3532 
3533  SDValue Zero = DAG.getTargetConstant(0, DL, MVT::i32);
3534 
3535  assert(!(OffEn->isOne() && IdxEn->isOne()) &&
3536  "Legacy intrinsic doesn't support both offset and index - use new version");
3537 
3538  SDValue VIndex = IdxEn->isOne() ? VAddr : Zero;
3539  SDValue VOffset = OffEn->isOne() ? VAddr : Zero;
3540 
3541  // Deal with the vec-3 case
3542  const ConstantSDNode *NumChannels = cast<ConstantSDNode>(Op.getOperand(4));
3543  auto Opcode = NumChannels->getZExtValue() == 3 ?
3545 
3546  SDValue Ops[] = {
3547  Chain,
3548  Op.getOperand(3), // vdata
3549  Op.getOperand(2), // rsrc
3550  VIndex,
3551  VOffset,
3552  Op.getOperand(6), // soffset
3553  Op.getOperand(7), // inst_offset
3554  Op.getOperand(8), // dfmt
3555  Op.getOperand(9), // nfmt
3556  Op.getOperand(12), // glc
3557  Op.getOperand(13), // slc
3558  };
3559 
3560  assert((cast<ConstantSDNode>(Op.getOperand(14)))->getZExtValue() == 0 &&
3561  "Value of tfe other than zero is unsupported");
3562 
3563  EVT VT = Op.getOperand(3).getValueType();
3567  VT.getStoreSize(), 4);
3568  return DAG.getMemIntrinsicNode(Opcode, DL,
3569  Op->getVTList(), Ops, VT, MMO);
3570  }
3571 
3572  case Intrinsic::amdgcn_tbuffer_store: {
3573  SDValue Ops[] = {
3574  Chain,
3575  Op.getOperand(2), // vdata
3576  Op.getOperand(3), // rsrc
3577  Op.getOperand(4), // vindex
3578  Op.getOperand(5), // voffset
3579  Op.getOperand(6), // soffset
3580  Op.getOperand(7), // offset
3581  Op.getOperand(8), // dfmt
3582  Op.getOperand(9), // nfmt
3583  Op.getOperand(10), // glc
3584  Op.getOperand(11) // slc
3585  };
3586  EVT VT = Op.getOperand(3).getValueType();
3590  VT.getStoreSize(), 4);
3592  Op->getVTList(), Ops, VT, MMO);
3593  }
3594 
3595  default:
3596  return Op;
3597  }
3598 }
3599 
3600 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
3601  SDLoc DL(Op);
3602  LoadSDNode *Load = cast<LoadSDNode>(Op);
3603  ISD::LoadExtType ExtType = Load->getExtensionType();
3604  EVT MemVT = Load->getMemoryVT();
3605 
3606  if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) {
3607  // FIXME: Copied from PPC
3608  // First, load into 32 bits, then truncate to 1 bit.
3609 
3610  SDValue Chain = Load->getChain();
3611  SDValue BasePtr = Load->getBasePtr();
3612  MachineMemOperand *MMO = Load->getMemOperand();
3613 
3614  EVT RealMemVT = (MemVT == MVT::i1) ? MVT::i8 : MVT::i16;
3615 
3616  SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
3617  BasePtr, RealMemVT, MMO);
3618 
3619  SDValue Ops[] = {
3620  DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD),
3621  NewLD.getValue(1)
3622  };
3623 
3624  return DAG.getMergeValues(Ops, DL);
3625  }
3626 
3627  if (!MemVT.isVector())
3628  return SDValue();
3629 
3631  "Custom lowering for non-i32 vectors hasn't been implemented.");
3632 
3633  unsigned AS = Load->getAddressSpace();
3634  if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), MemVT,
3635  AS, Load->getAlignment())) {
3636  SDValue Ops[2];
3637  std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(Load, DAG);
3638  return DAG.getMergeValues(Ops, DL);
3639  }
3640 
3641  MachineFunction &MF = DAG.getMachineFunction();
3643  // If there is a possibilty that flat instruction access scratch memory
3644  // then we need to use the same legalization rules we use for private.
3645  if (AS == AMDGPUASI.FLAT_ADDRESS)
3646  AS = MFI->hasFlatScratchInit() ?
3648 
3649  unsigned NumElements = MemVT.getVectorNumElements();
3650  if (AS == AMDGPUASI.CONSTANT_ADDRESS) {
3651  if (isMemOpUniform(Load))
3652  return SDValue();
3653  // Non-uniform loads will be selected to MUBUF instructions, so they
3654  // have the same legalization requirements as global and private
3655  // loads.
3656  //
3657  }
3658  if (AS == AMDGPUASI.CONSTANT_ADDRESS || AS == AMDGPUASI.GLOBAL_ADDRESS) {
3660  !Load->isVolatile() && isMemOpHasNoClobberedMemOperand(Load))
3661  return SDValue();
3662  // Non-uniform loads will be selected to MUBUF instructions, so they
3663  // have the same legalization requirements as global and private
3664  // loads.
3665  //
3666  }
3667  if (AS == AMDGPUASI.CONSTANT_ADDRESS || AS == AMDGPUASI.GLOBAL_ADDRESS ||
3668  AS == AMDGPUASI.FLAT_ADDRESS) {
3669  if (NumElements > 4)
3670  return SplitVectorLoad(Op, DAG);
3671  // v4 loads are supported for private and global memory.
3672  return SDValue();
3673  }
3674  if (AS == AMDGPUASI.PRIVATE_ADDRESS) {
3675  // Depending on the setting of the private_element_size field in the
3676  // resource descriptor, we can only make private accesses up to a certain
3677  // size.
3678  switch (Subtarget->getMaxPrivateElementSize()) {
3679  case 4:
3680  return scalarizeVectorLoad(Load, DAG);
3681  case 8:
3682  if (NumElements > 2)
3683  return SplitVectorLoad(Op, DAG);
3684  return SDValue();
3685  case 16:
3686  // Same as global/flat
3687  if (NumElements > 4)
3688  return SplitVectorLoad(Op, DAG);
3689  return SDValue();
3690  default:
3691  llvm_unreachable("unsupported private_element_size");
3692  }
3693  } else if (AS == AMDGPUASI.LOCAL_ADDRESS) {
3694  if (NumElements > 2)
3695  return SplitVectorLoad(Op, DAG);
3696 
3697  if (NumElements == 2)
3698  return SDValue();
3699 
3700  // If properly aligned, if we split we might be able to use ds_read_b64.
3701  return SplitVectorLoad(Op, DAG);
3702  }
3703  return SDValue();
3704 }
3705 
3706 SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3707  if (Op.getValueType() != MVT::i64)
3708  return SDValue();
3709 
3710  SDLoc DL(Op);
3711  SDValue Cond = Op.getOperand(0);
3712 
3713  SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
3714  SDValue One = DAG.getConstant(1, DL, MVT::i32);
3715 
3716  SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
3717  SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
3718 
3719  SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
3720  SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
3721 
3722  SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
3723 
3724  SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
3725  SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
3726 
3727  SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
3728 
3729  SDValue Res = DAG.getBuildVector(MVT::v2i32, DL, {Lo, Hi});
3730  return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
3731 }
3732 
3733 // Catch division cases where we can use shortcuts with rcp and rsq
3734 // instructions.
3735 SDValue SITargetLowering::lowerFastUnsafeFDIV(SDValue Op,
3736  SelectionDAG &DAG) const {
3737  SDLoc SL(Op);
3738  SDValue LHS = Op.getOperand(0);
3739  SDValue RHS = Op.getOperand(1);
3740  EVT VT = Op.getValueType();
3741  const SDNodeFlags Flags = Op->getFlags();
3742  bool Unsafe = DAG.getTarget().Options.UnsafeFPMath ||
3743  Flags.hasUnsafeAlgebra() || Flags.hasAllowReciprocal();
3744 
3745  if (!Unsafe && VT == MVT::f32 && Subtarget->hasFP32Denormals())
3746  return SDValue();
3747 
3748  if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
3749  if (Unsafe || VT == MVT::f32 || VT == MVT::f16) {
3750  if (CLHS->isExactlyValue(1.0)) {
3751  // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
3752  // the CI documentation has a worst case error of 1 ulp.
3753  // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
3754  // use it as long as we aren't trying to use denormals.
3755  //
3756  // v_rcp_f16 and v_rsq_f16 DO support denormals.
3757 
3758  // 1.0 / sqrt(x) -> rsq(x)
3759 
3760  // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
3761  // error seems really high at 2^29 ULP.
3762  if (RHS.getOpcode() == ISD::FSQRT)
3763  return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
3764 
3765  // 1.0 / x -> rcp(x)
3766  return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
3767  }
3768 
3769  // Same as for 1.0, but expand the sign out of the constant.
3770  if (CLHS->isExactlyValue(-1.0)) {
3771  // -1.0 / x -> rcp (fneg x)
3772  SDValue FNegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
3773  return DAG.getNode(AMDGPUISD::RCP, SL, VT, FNegRHS);
3774  }
3775  }
3776  }
3777 
3778  if (Unsafe) {
3779  // Turn into multiply by the reciprocal.
3780  // x / y -> x * (1.0 / y)
3781  SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
3782  return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, Flags);
3783  }
3784 
3785  return SDValue();
3786 }
3787 
3788 static SDValue getFPBinOp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &SL,
3789  EVT VT, SDValue A, SDValue B, SDValue GlueChain) {
3790  if (GlueChain->getNumValues() <= 1) {
3791  return DAG.getNode(Opcode, SL, VT, A, B);
3792  }
3793 
3794  assert(GlueChain->getNumValues() == 3);
3795 
3796  SDVTList VTList = DAG.getVTList(VT, MVT::Other, MVT::Glue);
3797  switch (Opcode) {
3798  default: llvm_unreachable("no chain equivalent for opcode");
3799  case ISD::FMUL:
3800  Opcode = AMDGPUISD::FMUL_W_CHAIN;
3801  break;
3802  }
3803 
3804  return DAG.getNode(Opcode, SL, VTList, GlueChain.getValue(1), A, B,
3805  GlueChain.getValue(2));
3806 }
3807 
3808 static SDValue getFPTernOp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &SL,
3809  EVT VT, SDValue A, SDValue B, SDValue C,
3810  SDValue GlueChain) {
3811  if (GlueChain->getNumValues() <= 1) {
3812  return DAG.getNode(Opcode, SL, VT, A, B, C);
3813  }
3814 
3815  assert(GlueChain->getNumValues() == 3);
3816 
3817  SDVTList VTList = DAG.getVTList(VT, MVT::Other, MVT::Glue);
3818  switch (Opcode) {
3819  default: llvm_unreachable("no chain equivalent for opcode");
3820  case ISD::FMA:
3821  Opcode = AMDGPUISD::FMA_W_CHAIN;
3822  break;
3823  }
3824 
3825  return DAG.getNode(Opcode, SL, VTList, GlueChain.getValue(1), A, B, C,
3826  GlueChain.getValue(2));
3827 }
3828 
3829 SDValue SITargetLowering::LowerFDIV16(SDValue Op, SelectionDAG &DAG) const {
3830  if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG))
3831  return FastLowered;
3832 
3833  SDLoc SL(Op);
3834  SDValue Src0 = Op.getOperand(0);
3835  SDValue Src1 = Op.getOperand(1);
3836 
3837  SDValue CvtSrc0 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src0);
3838  SDValue CvtSrc1 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src1);
3839 
3840  SDValue RcpSrc1 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, CvtSrc1);
3841  SDValue Quot = DAG.getNode(ISD::FMUL, SL, MVT::f32, CvtSrc0, RcpSrc1);
3842 
3843  SDValue FPRoundFlag = DAG.getTargetConstant(0, SL, MVT::i32);
3844  SDValue BestQuot = DAG.getNode(ISD::FP_ROUND, SL, MVT::f16, Quot, FPRoundFlag);
3845 
3846  return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f16, BestQuot, Src1, Src0);
3847 }
3848 
3849 // Faster 2.5 ULP division that does not support denormals.
3850 SDValue SITargetLowering::lowerFDIV_FAST(SDValue Op, SelectionDAG &DAG) const {
3851  SDLoc SL(Op);
3852  SDValue LHS = Op.getOperand(1);
3853  SDValue RHS = Op.getOperand(2);
3854 
3855  SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
3856 
3857  const APFloat K0Val(BitsToFloat(0x6f800000));
3858  const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
3859 
3860  const APFloat K1Val(BitsToFloat(0x2f800000));
3861  const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
3862 
3863  const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
3864 
3865  EVT SetCCVT =
3867 
3868  SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
3869 
3870  SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
3871 
3872  // TODO: Should this propagate fast-math-flags?
3873  r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
3874 
3875  // rcp does not support denormals.
3876  SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
3877 
3878  SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
3879 
3880  return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
3881 }
3882 
3883 SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
3884  if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG))
3885  return FastLowered;
3886 
3887  SDLoc SL(Op);
3888  SDValue LHS = Op.getOperand(0);
3889  SDValue RHS = Op.getOperand(1);
3890 
3891  const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
3892 
3893  SDVTList ScaleVT = DAG.getVTList(MVT::f32, MVT::i1);
3894 
3895  SDValue DenominatorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT,
3896  RHS, RHS, LHS);
3897  SDValue NumeratorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT,
3898  LHS, RHS, LHS);
3899 
3900  // Denominator is scaled to not be denormal, so using rcp is ok.
3901  SDValue ApproxRcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32,
3902  DenominatorScaled);
3903  SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f32,
3904  DenominatorScaled);
3905 
3906  const unsigned Denorm32Reg = AMDGPU::Hwreg::ID_MODE |
3908  (1 << AMDGPU::Hwreg::WIDTH_M1_SHIFT_);
3909 
3910  const SDValue BitField = DAG.getTargetConstant(Denorm32Reg, SL, MVT::i16);
3911 
3912  if (!Subtarget->hasFP32Denormals()) {
3913  SDVTList BindParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
3914  const SDValue EnableDenormValue = DAG.getConstant(FP_DENORM_FLUSH_NONE,
3915  SL, MVT::i32);
3916  SDValue EnableDenorm = DAG.getNode(AMDGPUISD::SETREG, SL, BindParamVTs,
3917  DAG.getEntryNode(),
3918  EnableDenormValue, BitField);
3919  SDValue Ops[3] = {
3920  NegDivScale0,
3921  EnableDenorm.getValue(0),
3922  EnableDenorm.getValue(1)
3923  };
3924 
3925  NegDivScale0 = DAG.getMergeValues(Ops, SL);
3926  }
3927 
3928  SDValue Fma0 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0,
3929  ApproxRcp, One, NegDivScale0);
3930 
3931  SDValue Fma1 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, Fma0, ApproxRcp,
3932  ApproxRcp, Fma0);
3933 
3934  SDValue Mul = getFPBinOp(DAG, ISD::FMUL, SL, MVT::f32, NumeratorScaled,
3935  Fma1, Fma1);
3936 
3937  SDValue Fma2 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0, Mul,
3938  NumeratorScaled, Mul);
3939 
3940  SDValue Fma3 = getFPTernOp(DAG, ISD::FMA,SL, MVT::f32, Fma2, Fma1, Mul, Fma2);
3941 
3942  SDValue Fma4 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0, Fma3,
3943  NumeratorScaled, Fma3);
3944 
3945  if (!Subtarget->hasFP32Denormals()) {
3946  const SDValue DisableDenormValue =
3948  SDValue DisableDenorm = DAG.getNode(AMDGPUISD::SETREG, SL, MVT::Other,
3949  Fma4.getValue(1),
3950  DisableDenormValue,
3951  BitField,
3952  Fma4.getValue(2));
3953 
3954  SDValue OutputChain = DAG.getNode(ISD::TokenFactor, SL, MVT::Other,
3955  DisableDenorm, DAG.getRoot());
3956  DAG.setRoot(OutputChain);
3957  }
3958 
3959  SDValue Scale = NumeratorScaled.getValue(1);
3960  SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f32,
3961  Fma4, Fma1, Fma3, Scale);
3962 
3963  return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f32, Fmas, RHS, LHS);
3964 }
3965 
3966 SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
3967  if (DAG.getTarget().Options.UnsafeFPMath)
3968  return lowerFastUnsafeFDIV(Op, DAG);
3969 
3970  SDLoc SL(Op);
3971  SDValue X = Op.getOperand(0);
3972  SDValue Y = Op.getOperand(1);
3973 
3974  const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
3975 
3976  SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
3977 
3978  SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
3979 
3980  SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
3981 
3982  SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
3983 
3984  SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
3985 
3986  SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
3987 
3988  SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
3989 
3990  SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
3991 
3992  SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
3993  SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
3994 
3995  SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
3996  NegDivScale0, Mul, DivScale1);
3997 
3998  SDValue Scale;
3999 
4001  // Workaround a hardware bug on SI where the condition output from div_scale
4002  // is not usable.
4003 
4004  const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
4005 
4006  // Figure out if the scale to use for div_fmas.
4007  SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
4008  SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
4009  SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
4010  SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
4011 
4012  SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
4013  SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
4014 
4015  SDValue Scale0Hi
4016  = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
4017  SDValue Scale1Hi
4018  = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
4019 
4020  SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
4021  SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
4022  Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
4023  } else {
4024  Scale = DivScale1.getValue(1);
4025  }
4026 
4027  SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
4028  Fma4, Fma3, Mul, Scale);
4029 
4030  return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
4031 }
4032 
4033 SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
4034  EVT VT = Op.getValueType();
4035 
4036  if (VT == MVT::f32)
4037  return LowerFDIV32(Op, DAG);
4038 
4039  if (VT == MVT::f64)
4040  return LowerFDIV64(Op, DAG);
4041 
4042  if (VT == MVT::f16)
4043  return LowerFDIV16(Op, DAG);
4044 
4045  llvm_unreachable("Unexpected type for fdiv");
4046 }
4047 
4048 SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
4049  SDLoc DL(Op);
4050  StoreSDNode *Store = cast<StoreSDNode>(Op);
4051  EVT VT = Store->getMemoryVT();
4052 
4053  if (VT == MVT::i1) {
4054  return DAG.getTruncStore(Store->getChain(), DL,
4055  DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
4056  Store->getBasePtr(), MVT::i1, Store->getMemOperand());
4057  }
4058 
4059  assert(VT.isVector() &&
4060  Store->getValue().getValueType().getScalarType() == MVT::i32);
4061 
4062  unsigned AS = Store->getAddressSpace();
4063  if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), VT,
4064  AS, Store->getAlignment())) {
4065  return expandUnalignedStore(Store, DAG);
4066  }
4067 
4068  MachineFunction &MF = DAG.getMachineFunction();
4070  // If there is a possibilty that flat instruction access scratch memory
4071  // then we need to use the same legalization rules we use for private.
4072  if (AS == AMDGPUASI.FLAT_ADDRESS)
4073  AS = MFI->hasFlatScratchInit() ?
4075 
4076  unsigned NumElements = VT.getVectorNumElements();
4077  if (AS == AMDGPUASI.GLOBAL_ADDRESS ||
4078  AS == AMDGPUASI.FLAT_ADDRESS) {
4079  if (NumElements > 4)
4080  return SplitVectorStore(Op, DAG);
4081  return SDValue();
4082  } else if (AS == AMDGPUASI.PRIVATE_ADDRESS) {
4083  switch (Subtarget->getMaxPrivateElementSize()) {
4084  case 4:
4085  return scalarizeVectorStore(Store, DAG);
4086  case 8:
4087  if (NumElements > 2)
4088  return SplitVectorStore(Op, DAG);
4089  return SDValue();
4090  case 16:
4091  if (NumElements > 4)
4092  return SplitVectorStore(Op, DAG);
4093  return SDValue();
4094  default:
4095  llvm_unreachable("unsupported private_element_size");
4096  }
4097  } else if (AS == AMDGPUASI.LOCAL_ADDRESS) {
4098  if (NumElements > 2)
4099  return SplitVectorStore(Op, DAG);
4100 
4101  if (NumElements == 2)
4102  return Op;
4103 
4104  // If properly aligned, if we split we might be able to use ds_write_b64.
4105  return SplitVectorStore(Op, DAG);
4106  } else {
4107  llvm_unreachable("unhandled address space");
4108  }
4109 }
4110 
4111 SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
4112  SDLoc DL(Op);
4113  EVT VT = Op.getValueType();
4114  SDValue Arg = Op.getOperand(0);
4115  // TODO: Should this propagate fast-math-flags?
4116  SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
4117  DAG.getNode(ISD::FMUL, DL, VT, Arg,
4118  DAG.getConstantFP(0.5/M_PI, DL,
4119  VT)));
4120 
4121  switch (Op.getOpcode()) {
4122  case ISD::FCOS:
4123  return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
4124  case ISD::FSIN:
4125  return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
4126  default:
4127  llvm_unreachable("Wrong trig opcode");
4128  }
4129 }
4130 
4131 SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
4132  AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op);
4133  assert(AtomicNode->isCompareAndSwap());
4134  unsigned AS = AtomicNode->getAddressSpace();
4135 
4136  // No custom lowering required for local address space
4137  if (!isFlatGlobalAddrSpace(AS, AMDGPUASI))
4138  return Op;
4139 
4140  // Non-local address space requires custom lowering for atomic compare
4141  // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2
4142  SDLoc DL(Op);
4143  SDValue ChainIn = Op.getOperand(0);
4144  SDValue Addr = Op.getOperand(1);
4145  SDValue Old = Op.getOperand(2);
4146  SDValue New = Op.getOperand(3);
4147  EVT VT = Op.getValueType();
4148  MVT SimpleVT = VT.getSimpleVT();
4149  MVT VecType = MVT::getVectorVT(SimpleVT, 2);
4150 
4151  SDValue NewOld = DAG.getBuildVector(VecType, DL, {New, Old});
4152  SDValue Ops[] = { ChainIn, Addr, NewOld };
4153 
4155  Ops, VT, AtomicNode->getMemOperand());
4156 }
4157 
4158 //===----------------------------------------------------------------------===//
4159 // Custom DAG optimizations
4160 //===----------------------------------------------------------------------===//
4161 
4162 SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
4163  DAGCombinerInfo &DCI) const {
4164  EVT VT = N->getValueType(0);
4165  EVT ScalarVT = VT.getScalarType();
4166  if (ScalarVT != MVT::f32)
4167  return SDValue();
4168 
4169  SelectionDAG &DAG = DCI.DAG;
4170  SDLoc DL(N);
4171 
4172  SDValue Src = N->getOperand(0);
4173  EVT SrcVT = Src.getValueType();
4174 
4175  // TODO: We could try to match extracting the higher bytes, which would be
4176  // easier if i8 vectors weren't promoted to i32 vectors, particularly after
4177  // types are legalized. v4i8 -> v4f32 is probably the only case to worry
4178  // about in practice.
4179  if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
4180  if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
4181  SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
4182  DCI.AddToWorklist(Cvt.getNode());
4183  return Cvt;
4184  }
4185  }
4186 
4187  return SDValue();
4188 }
4189 
4190 /// \brief Return true if the given offset Size in bytes can be folded into
4191 /// the immediate offsets of a memory instruction for the given address space.
4192 static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
4193  const SISubtarget &STI) {
4194  auto AMDGPUASI = STI.getAMDGPUAS();
4195  if (AS == AMDGPUASI.GLOBAL_ADDRESS) {
4196  // MUBUF instructions a 12-bit offset in bytes.
4197  return isUInt<12>(OffsetSize);
4198  }
4199  if (AS == AMDGPUASI.CONSTANT_ADDRESS) {
4200  // SMRD instructions have an 8-bit offset in dwords on SI and
4201  // a 20-bit offset in bytes on VI.
4203  return isUInt<20>(OffsetSize);
4204  else
4205  return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
4206  }
4207  if (AS == AMDGPUASI.LOCAL_ADDRESS ||
4208  AS == AMDGPUASI.REGION_ADDRESS) {
4209  // The single offset versions have a 16-bit offset in bytes.
4210  return isUInt<16>(OffsetSize);
4211  }
4212  // Indirect register addressing does not use any offsets.
4213  return false;
4214 }
4215 
4216 // (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
4217 
4218 // This is a variant of
4219 // (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
4220 //
4221 // The normal DAG combiner will do this, but only if the add has one use since
4222 // that would increase the number of instructions.
4223 //
4224 // This prevents us from seeing a constant offset that can be folded into a
4225 // memory instruction's addressing mode. If we know the resulting add offset of
4226 // a pointer can be folded into an addressing offset, we can replace the pointer
4227 // operand with the add of new constant offset. This eliminates one of the uses,
4228 // and may allow the remaining use to also be simplified.
4229 //
4230 SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
4231  unsigned AddrSpace,
4232  DAGCombinerInfo &DCI) const {
4233  SDValue N0 = N->getOperand(0);
4234  SDValue N1 = N->getOperand(1);
4235 
4236  if (N0.getOpcode() != ISD::ADD)
4237  return SDValue();
4238 
4239  const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
4240  if (!CN1)
4241  return SDValue();
4242 
4243  const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
4244  if (!CAdd)
4245  return SDValue();
4246 
4247  // If the resulting offset is too large, we can't fold it into the addressing
4248  // mode offset.
4249  APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
4250  if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *getSubtarget()))
4251  return SDValue();
4252 
4253  SelectionDAG &DAG = DCI.DAG;
4254  SDLoc SL(N);
4255  EVT VT = N->getValueType(0);
4256 
4257  SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
4258  SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
4259 
4260  return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
4261 }
4262 
4263 SDValue SITargetLowering::performMemSDNodeCombine(MemSDNode *N,
4264  DAGCombinerInfo &DCI) const {
4265  SDValue Ptr = N->getBasePtr();
4266  SelectionDAG &DAG = DCI.DAG;
4267  SDLoc SL(N);
4268 
4269  // TODO: We could also do this for multiplies.
4270  unsigned AS = N->getAddressSpace();
4271  if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUASI.PRIVATE_ADDRESS) {
4272  SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
4273  if (NewPtr) {
4274  SmallVector<SDValue, 8> NewOps(N->op_begin(), N->op_end());
4275 
4276  NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
4277  return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
4278  }
4279  }
4280 
4281  return SDValue();
4282 }
4283 
4284 static bool bitOpWithConstantIsReducible(unsigned Opc, uint32_t Val) {
4285  return (Opc == ISD::AND && (Val == 0 || Val == 0xffffffff)) ||
4286  (Opc == ISD::OR && (Val == 0xffffffff || Val == 0)) ||
4287  (Opc == ISD::XOR && Val == 0);
4288 }
4289 
4290 // Break up 64-bit bit operation of a constant into two 32-bit and/or/xor. This
4291 // will typically happen anyway for a VALU 64-bit and. This exposes other 32-bit
4292 // integer combine opportunities since most 64-bit operations are decomposed
4293 // this way. TODO: We won't want this for SALU especially if it is an inline
4294 // immediate.
4295 SDValue SITargetLowering::splitBinaryBitConstantOp(
4296  DAGCombinerInfo &DCI,
4297  const SDLoc &SL,
4298  unsigned Opc, SDValue LHS,
4299  const ConstantSDNode *CRHS) const {
4300  uint64_t Val = CRHS->getZExtValue();
4301  uint32_t ValLo = Lo_32(Val);
4302  uint32_t ValHi = Hi_32(Val);
4303  const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
4304 
4305  if ((bitOpWithConstantIsReducible(Opc, ValLo) ||
4306  bitOpWithConstantIsReducible(Opc, ValHi)) ||
4307  (CRHS->hasOneUse() && !TII->isInlineConstant(CRHS->getAPIntValue()))) {
4308  // If we need to materialize a 64-bit immediate, it will be split up later
4309  // anyway. Avoid creating the harder to understand 64-bit immediate
4310  // materialization.
4311  return splitBinaryBitConstantOpImpl(DCI, SL, Opc, LHS, ValLo, ValHi);
4312  }
4313 
4314  return SDValue();
4315 }
4316 
4317 // Returns true if argument is a boolean value which is not serialized into
4318 // memory or argument and does not require v_cmdmask_b32 to be deserialized.
4319 static bool isBoolSGPR(SDValue V) {
4320  if (V.getValueType() != MVT::i1)
4321  return false;
4322  switch (V.getOpcode()) {
4323  default: break;
4324  case ISD::SETCC:
4325  case ISD::AND:
4326  case ISD::OR:
4327  case ISD::XOR:
4328  case AMDGPUISD::FP_CLASS:
4329  return true;
4330  }
4331  return false;
4332 }
4333 
4334 SDValue SITargetLowering::performAndCombine(SDNode *N,
4335  DAGCombinerInfo &DCI) const {
4336  if (DCI.isBeforeLegalize())
4337  return SDValue();
4338 
4339  SelectionDAG &DAG = DCI.DAG;
4340  EVT VT = N->getValueType(0);
4341  SDValue LHS = N->getOperand(0);
4342  SDValue RHS = N->getOperand(1);
4343 
4344 
4345  const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS);
4346  if (VT == MVT::i64 && CRHS) {
4347  if (SDValue Split
4348  = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::AND, LHS, CRHS))
4349  return Split;
4350  }
4351 
4352  if (CRHS && VT == MVT::i32) {
4353  // and (srl x, c), mask => shl (bfe x, nb + c, mask >> nb), nb
4354  // nb = number of trailing zeroes in mask
4355  // It can be optimized out using SDWA for GFX8+ in the SDWA peephole pass,
4356  // given that we are selecting 8 or 16 bit fields starting at byte boundary.
4357  uint64_t Mask = CRHS->getZExtValue();
4358  unsigned Bits = countPopulation(Mask);
4359  if (getSubtarget()->hasSDWA() && LHS->getOpcode() == ISD::SRL &&
4360  (Bits == 8 || Bits == 16) && isShiftedMask_64(Mask) && !(Mask & 1)) {
4361  if (auto *CShift = dyn_cast<ConstantSDNode>(LHS->getOperand(1))) {
4362  unsigned Shift = CShift->getZExtValue();
4363  unsigned NB = CRHS->getAPIntValue().countTrailingZeros();
4364  unsigned Offset = NB + Shift;
4365  if ((Offset & (Bits - 1)) == 0) { // Starts at a byte or word boundary.
4366  SDLoc SL(N);
4367  SDValue BFE = DAG.getNode(AMDGPUISD::BFE_U32, SL, MVT::i32,
4368  LHS->getOperand(0),
4369  DAG.getConstant(Offset, SL, MVT::i32),
4370  DAG.getConstant(Bits, SL, MVT::i32));
4371  EVT NarrowVT = EVT::getIntegerVT(*DAG.getContext(), Bits);
4372  SDValue Ext = DAG.getNode(ISD::AssertZext, SL, VT, BFE,
4373  DAG.getValueType(NarrowVT));
4374  SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(LHS), VT, Ext,
4375  DAG.getConstant(NB, SDLoc(CRHS), MVT::i32));
4376  return Shl;
4377  }
4378  }
4379  }
4380  }
4381 
4382  // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
4383  // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
4384  if (LHS.getOpcode() == ISD::SETCC && RHS.getOpcode() == ISD::SETCC) {
4385  ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
4386  ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
4387 
4388  SDValue X = LHS.getOperand(0);
4389  SDValue Y = RHS.getOperand(0);
4390  if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
4391  return SDValue();
4392 
4393  if (LCC == ISD::SETO) {
4394  if (X != LHS.getOperand(1))
4395  return SDValue();
4396 
4397  if (RCC == ISD::SETUNE) {
4399  if (!C1 || !C1->isInfinity() || C1->isNegative())
4400  return SDValue();
4401 
4402  const uint32_t Mask = SIInstrFlags::N_NORMAL |
4408 
4409  static_assert(((~(SIInstrFlags::S_NAN |
4412  SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
4413  "mask not equal");
4414 
4415  SDLoc DL(N);
4416  return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
4417  X, DAG.getConstant(Mask, DL, MVT::i32));
4418  }
4419  }
4420  }
4421 
4422  if (VT == MVT::i32 &&
4423  (RHS.getOpcode() == ISD::SIGN_EXTEND || LHS.getOpcode() == ISD::SIGN_EXTEND)) {
4424  // and x, (sext cc from i1) => select cc, x, 0
4425  if (RHS.getOpcode() != ISD::SIGN_EXTEND)
4426  std::swap(LHS, RHS);
4427  if (isBoolSGPR(RHS.getOperand(0)))
4428  return DAG.getSelect(SDLoc(N), MVT::i32, RHS.getOperand(0),
4429  LHS, DAG.getConstant(0, SDLoc(N), MVT::i32));
4430  }
4431 
4432  return SDValue();
4433 }
4434 
4435 SDValue SITargetLowering::performOrCombine(SDNode *N,
4436  DAGCombinerInfo &DCI) const {
4437  SelectionDAG &DAG = DCI.DAG;
4438  SDValue LHS = N->getOperand(0);
4439  SDValue RHS = N->getOperand(1);
4440 
4441  EVT VT = N->getValueType(0);
4442  if (VT == MVT::i1) {
4443  // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
4444  if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
4445  RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
4446  SDValue Src = LHS.getOperand(0);
4447  if (Src != RHS.getOperand(0))
4448  return SDValue();
4449 
4450  const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
4451  const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
4452  if (!CLHS || !CRHS)
4453  return SDValue();
4454 
4455  // Only 10 bits are used.
4456  static const uint32_t MaxMask = 0x3ff;
4457 
4458  uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
4459  SDLoc DL(N);
4460  return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
4461  Src, DAG.getConstant(NewMask, DL, MVT::i32));
4462  }
4463 
4464  return SDValue();
4465  }
4466 
4467  if (VT != MVT::i64)
4468  return SDValue();
4469 
4470  // TODO: This could be a generic combine with a predicate for extracting the
4471  // high half of an integer being free.
4472 
4473  // (or i64:x, (zero_extend i32:y)) ->
4474  // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x)))
4475  if (LHS.getOpcode() == ISD::ZERO_EXTEND &&
4476  RHS.getOpcode() != ISD::ZERO_EXTEND)
4477  std::swap(LHS, RHS);
4478 
4479  if (RHS.getOpcode() == ISD::ZERO_EXTEND) {
4480  SDValue ExtSrc = RHS.getOperand(0);
4481  EVT SrcVT = ExtSrc.getValueType();
4482  if (SrcVT == MVT::i32) {
4483  SDLoc SL(N);
4484  SDValue LowLHS, HiBits;
4485  std::tie(LowLHS, HiBits) = split64BitValue(LHS, DAG);
4486  SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc);
4487 
4488  DCI.AddToWorklist(LowOr.getNode());
4489  DCI.AddToWorklist(HiBits.getNode());
4490 
4492  LowOr, HiBits);
4493  return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
4494  }
4495  }
4496 
4497  const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
4498  if (CRHS) {
4499  if (SDValue Split
4500  = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::OR, LHS, CRHS))
4501  return Split;
4502  }
4503 
4504  return SDValue();
4505 }
4506 
4507 SDValue SITargetLowering::performXorCombine(SDNode *N,
4508  DAGCombinerInfo &DCI) const {
4509  EVT VT = N->getValueType(0);
4510  if (VT != MVT::i64)
4511  return SDValue();
4512 
4513  SDValue LHS = N->getOperand(0);
4514  SDValue RHS = N->getOperand(1);
4515 
4516  const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS);
4517  if (CRHS) {
4518  if (SDValue Split
4519  = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::XOR, LHS, CRHS))
4520  return Split;
4521  }
4522 
4523  return SDValue();
4524 }
4525 
4526 // Instructions that will be lowered with a final instruction that zeros the
4527 // high result bits.
4528 // XXX - probably only need to list legal operations.
4529 static bool fp16SrcZerosHighBits(unsigned Opc) {
4530  switch (Opc) {
4531  case ISD::FADD:
4532  case ISD::FSUB:
4533  case ISD::FMUL:
4534  case ISD::FDIV:
4535  case ISD::FREM:
4536  case ISD::FMA:
4537  case ISD::FMAD:
4538  case ISD::FCANONICALIZE:
4539  case ISD::FP_ROUND:
4540  case ISD::UINT_TO_FP:
4541  case ISD::SINT_TO_FP:
4542  case ISD::FABS:
4543  // Fabs is lowered to a bit operation, but it's an and which will clear the
4544  // high bits anyway.
4545  case ISD::FSQRT:
4546  case ISD::FSIN:
4547  case ISD::FCOS:
4548  case ISD::FPOWI:
4549  case ISD::FPOW:
4550  case ISD::FLOG:
4551  case ISD::FLOG2:
4552  case ISD::FLOG10:
4553  case ISD::FEXP:
4554  case ISD::FEXP2:
4555  case ISD::FCEIL:
4556  case ISD::FTRUNC:
4557  case ISD::FRINT:
4558  case ISD::FNEARBYINT:
4559  case ISD::FROUND:
4560  case ISD::FFLOOR:
4561  case ISD::FMINNUM:
4562  case ISD::FMAXNUM:
4563  case AMDGPUISD::FRACT:
4564  case AMDGPUISD::CLAMP:
4565  case AMDGPUISD::COS_HW:
4566  case AMDGPUISD::SIN_HW:
4567  case AMDGPUISD::FMIN3:
4568  case AMDGPUISD::FMAX3:
4569  case AMDGPUISD::FMED3:
4570  case AMDGPUISD::FMAD_FTZ:
4571  case AMDGPUISD::RCP:
4572  case AMDGPUISD::RSQ:
4573  case AMDGPUISD::LDEXP:
4574  return true;
4575  default:
4576  // fcopysign, select and others may be lowered to 32-bit bit operations
4577  // which don't zero the high bits.
4578  return false;
4579  }
4580 }
4581 
4582 SDValue SITargetLowering::performZeroExtendCombine(SDNode *N,
4583  DAGCombinerInfo &DCI) const {
4584  if (!Subtarget->has16BitInsts() ||
4585  DCI.getDAGCombineLevel() < AfterLegalizeDAG)
4586  return SDValue();
4587 
4588  EVT VT = N->getValueType(0);
4589  if (VT != MVT::i32)
4590  return SDValue();
4591 
4592  SDValue Src = N->getOperand(0);
4593  if (Src.getValueType() != MVT::i16)
4594  return SDValue();
4595 
4596  // (i32 zext (i16 (bitcast f16:$src))) -> fp16_zext $src
4597  // FIXME: It is not universally true that the high bits are zeroed on gfx9.
4598  if (Src.getOpcode() == ISD::BITCAST) {
4599  SDValue BCSrc = Src.getOperand(0);
4600  if (BCSrc.getValueType() == MVT::f16 &&
4602  return DCI.DAG.getNode(AMDGPUISD::FP16_ZEXT, SDLoc(N), VT, BCSrc);
4603  }
4604 
4605  return SDValue();
4606 }
4607 
4608 SDValue SITargetLowering::performClassCombine(SDNode *N,
4609  DAGCombinerInfo &DCI) const {
4610  SelectionDAG &DAG = DCI.DAG;
4611  SDValue Mask = N->getOperand(1);
4612 
4613  // fp_class x, 0 -> false
4614  if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
4615  if (CMask->isNullValue())
4616  return DAG.getConstant(0, SDLoc(N), MVT::i1);
4617  }
4618 
4619  if (N->getOperand(0).isUndef())
4620  return DAG.getUNDEF(MVT::i1);
4621 
4622  return SDValue();
4623 }
4624 
4625 static bool isKnownNeverSNan(SelectionDAG &DAG, SDValue Op) {
4627  return true;
4628 
4629  return DAG.isKnownNeverNaN(Op);
4630 }
4631 
4632 static bool isCanonicalized(SelectionDAG &DAG, SDValue Op,
4633  const SISubtarget *ST, unsigned MaxDepth=5) {
4634  // If source is a result of another standard FP operation it is already in
4635  // canonical form.
4636 
4637  switch (Op.getOpcode()) {
4638  default:
4639  break;
4640 
4641  // These will flush denorms if required.
4642  case ISD::FADD:
4643  case ISD::FSUB:
4644  case ISD::FMUL:
4645  case ISD::FSQRT:
4646  case ISD::FCEIL:
4647  case ISD::FFLOOR:
4648  case ISD::FMA:
4649  case ISD::FMAD:
4650 
4651  case ISD::FCANONICALIZE:
4652  return true;
4653 
4654  case ISD::FP_ROUND:
4655  return Op.getValueType().getScalarType() != MVT::f16 ||
4656  ST->hasFP16Denormals();
4657 
4658  case ISD::FP_EXTEND:
4659  return Op.getOperand(0).getValueType().getScalarType() != MVT::f16 ||
4660  ST->hasFP16Denormals();
4661 
4662  case ISD::FP16_TO_FP:
4663  case ISD::FP_TO_FP16:
4664  return ST->hasFP16Denormals();
4665 
4666  // It can/will be lowered or combined as a bit operation.
4667  // Need to check their input recursively to handle.
4668  case ISD::FNEG:
4669  case ISD::FABS:
4670  return (MaxDepth > 0) &&
4671  isCanonicalized(DAG, Op.getOperand(0), ST, MaxDepth - 1);
4672 
4673  case ISD::FSIN:
4674  case ISD::FCOS:
4675  case ISD::FSINCOS:
4676  return Op.getValueType().getScalarType() != MVT::f16;
4677 
4678  // In pre-GFX9 targets V_MIN_F32 and others do not flush denorms.
4679  // For such targets need to check their input recursively.
4680  case ISD::FMINNUM:
4681  case ISD::FMAXNUM:
4682  case ISD::FMINNAN:
4683  case ISD::FMAXNAN:
4684 
4685  if (ST->supportsMinMaxDenormModes() &&
4686  DAG.isKnownNeverNaN(Op.getOperand(0)) &&
4687  DAG.isKnownNeverNaN(Op.getOperand(1)))
4688  return true;
4689 
4690  return (MaxDepth > 0) &&
4691  isCanonicalized(DAG, Op.getOperand(0), ST, MaxDepth - 1) &&
4692  isCanonicalized(DAG, Op.getOperand(1), ST, MaxDepth - 1);
4693 
4694  case ISD::ConstantFP: {
4695  auto F = cast<ConstantFPSDNode>(Op)->getValueAPF();
4696  return !F.isDenormal() && !(F.isNaN() && F.isSignaling());
4697  }
4698  }
4699  return false;
4700 }
4701 
4702 // Constant fold canonicalize.
4703 SDValue SITargetLowering::performFCanonicalizeCombine(
4704  SDNode *N,
4705  DAGCombinerInfo &DCI) const {
4706  SelectionDAG &DAG = DCI.DAG;
4708 
4709  if (!CFP) {
4710  SDValue N0 = N->getOperand(0);
4711  EVT VT = N0.getValueType().getScalarType();
4712  auto ST = getSubtarget();
4713 
4714  if (((VT == MVT::f32 && ST->hasFP32Denormals()) ||
4715  (VT == MVT::f64 && ST->hasFP64Denormals()) ||
4716  (VT == MVT::f16 && ST->hasFP16Denormals())) &&
4717  DAG.isKnownNeverNaN(N0))
4718  return N0;
4719 
4720  bool IsIEEEMode = Subtarget->enableIEEEBit(DAG.getMachineFunction());
4721 
4722  if ((IsIEEEMode || isKnownNeverSNan(DAG, N0)) &&
4723  isCanonicalized(DAG, N0, ST))
4724  return N0;
4725 
4726  return SDValue();
4727  }
4728 
4729  const APFloat &C = CFP->getValueAPF();
4730 
4731  // Flush denormals to 0 if not enabled.
4732  if (C.isDenormal()) {
4733  EVT VT = N->getValueType(0);
4734  EVT SVT = VT.getScalarType();
4735  if (SVT == MVT::f32 && !Subtarget->hasFP32Denormals())
4736  return DAG.getConstantFP(0.0, SDLoc(N), VT);
4737 
4738  if (SVT == MVT::f64 && !Subtarget->hasFP64Denormals())
4739  return DAG.getConstantFP(0.0, SDLoc(N), VT);
4740 
4741  if (SVT == MVT::f16 && !Subtarget->hasFP16Denormals())
4742  return DAG.getConstantFP(0.0, SDLoc(N), VT);
4743  }
4744 
4745  if (C.isNaN()) {
4746  EVT VT = N->getValueType(0);
4747  APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics());
4748  if (C.isSignaling()) {
4749  // Quiet a signaling NaN.
4750  return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
4751  }
4752 
4753  // Make sure it is the canonical NaN bitpattern.
4754  //
4755  // TODO: Can we use -1 as the canonical NaN value since it's an inline
4756  // immediate?
4757  if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt())
4758  return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
4759  }
4760 
4761  return N->getOperand(0);
4762 }
4763 
4764 static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
4765  switch (Opc) {
4766  case ISD::FMAXNUM:
4767  return AMDGPUISD::FMAX3;
4768  case ISD::SMAX:
4769  return AMDGPUISD::SMAX3;
4770  case ISD::UMAX:
4771  return AMDGPUISD::UMAX3;
4772  case ISD::FMINNUM:
4773  return AMDGPUISD::FMIN3;
4774  case ISD::SMIN:
4775  return AMDGPUISD::SMIN3;
4776  case ISD::UMIN:
4777  return AMDGPUISD::UMIN3;
4778  default:
4779  llvm_unreachable("Not a min/max opcode");
4780  }
4781 }
4782 
4783 SDValue SITargetLowering::performIntMed3ImmCombine(
4784  SelectionDAG &DAG, const SDLoc &SL,
4785  SDValue Op0, SDValue Op1, bool Signed) const {
4787  if (!K1)
4788  return SDValue();
4789 
4791  if (!K0)
4792  return SDValue();
4793 
4794  if (Signed) {
4795  if (K0->getAPIntValue().sge(K1->getAPIntValue()))
4796  return SDValue();
4797  } else {
4798  if (K0->getAPIntValue().uge(K1->getAPIntValue()))
4799  return SDValue();
4800  }
4801 
4802  EVT VT = K0->getValueType(0);
4803  unsigned Med3Opc = Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3;
4804  if (VT == MVT::i32 || (VT == MVT::i16 && Subtarget->hasMed3_16())) {
4805  return DAG.getNode(Med3Opc, SL, VT,
4806  Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0));
4807  }
4808 
4809  // If there isn't a 16-bit med3 operation, convert to 32-bit.
4810  MVT NVT = MVT::i32;
4811  unsigned ExtOp = Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
4812 
4813  SDValue Tmp1 = DAG.getNode(ExtOp, SL, NVT, Op0->getOperand(0));
4814  SDValue Tmp2 = DAG.getNode(ExtOp, SL, NVT, Op0->getOperand(1));
4815  SDValue Tmp3 = DAG.getNode(ExtOp, SL, NVT, Op1);
4816 
4817  SDValue Med3 = DAG.getNode(Med3Opc, SL, NVT, Tmp1, Tmp2, Tmp3);
4818  return DAG.getNode(ISD::TRUNCATE, SL, VT, Med3);
4819 }
4820 
4821 SDValue SITargetLowering::performFPMed3ImmCombine(SelectionDAG &DAG,
4822  const SDLoc &SL,
4823  SDValue Op0,
4824  SDValue Op1) const {
4826  if (!K1)
4827  return SDValue();
4828 
4830  if (!K0)
4831  return SDValue();
4832 
4833  // Ordered >= (although NaN inputs should have folded away by now).
4835  if (Cmp == APFloat::cmpGreaterThan)
4836  return SDValue();
4837 
4838  // TODO: Check IEEE bit enabled?
4839  EVT VT = K0->getValueType(0);
4840  if (Subtarget->enableDX10Clamp()) {
4841  // If dx10_clamp is enabled, NaNs clamp to 0.0. This is the same as the
4842  // hardware fmed3 behavior converting to a min.
4843  // FIXME: Should this be allowing -0.0?
4844  if (K1->isExactlyValue(1.0) && K0->isExactlyValue(0.0))
4845  return DAG.getNode(AMDGPUISD::CLAMP, SL, VT, Op0.getOperand(0));
4846  }
4847 
4848  // med3 for f16 is only available on gfx9+.
4849  if (VT == MVT::f64 || (VT == MVT::f16 && !Subtarget->hasMed3_16()))
4850  return SDValue();
4851 
4852  // This isn't safe with signaling NaNs because in IEEE mode, min/max on a
4853  // signaling NaN gives a quiet NaN. The quiet NaN input to the min would then
4854  // give the other result, which is different from med3 with a NaN input.
4855  SDValue Var = Op0.getOperand(0);
4856  if (!isKnownNeverSNan(DAG, Var))
4857  return SDValue();
4858 
4859  return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0),
4860  Var, SDValue(K0, 0), SDValue(K1, 0));
4861 }
4862 
4863 SDValue SITargetLowering::performMinMaxCombine(SDNode *N,
4864  DAGCombinerInfo &DCI) const {
4865  SelectionDAG &DAG = DCI.DAG;
4866 
4867  EVT VT = N->getValueType(0);
4868  unsigned Opc = N->getOpcode();
4869  SDValue Op0 = N->getOperand(0);
4870  SDValue Op1 = N->getOperand(1);
4871 
4872  // Only do this if the inner op has one use since this will just increases
4873  // register pressure for no benefit.
4874 
4875 
4876  if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY &&
4877  VT != MVT::f64 &&
4878  ((VT != MVT::f16 && VT != MVT::i16) || Subtarget->hasMin3Max3_16())) {
4879  // max(max(a, b), c) -> max3(a, b, c)
4880  // min(min(a, b), c) -> min3(a, b, c)
4881  if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
4882  SDLoc DL(N);
4883  return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
4884  DL,
4885  N->getValueType(0),
4886  Op0.getOperand(0),
4887  Op0.getOperand(1),
4888  Op1);
4889  }
4890 
4891  // Try commuted.
4892  // max(a, max(b, c)) -> max3(a, b, c)
4893  // min(a, min(b, c)) -> min3(a, b, c)
4894  if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
4895  SDLoc DL(N);
4896  return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
4897  DL,
4898  N->getValueType(0),
4899  Op0,
4900  Op1.getOperand(0),
4901  Op1.getOperand(1));
4902  }
4903  }
4904 
4905  // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1)
4906  if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) {
4907  if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true))
4908  return Med3;
4909  }
4910 
4911  if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) {
4912  if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false))
4913  return Med3;
4914  }
4915 
4916  // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1)
4917  if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) ||
4918  (Opc == AMDGPUISD::FMIN_LEGACY &&
4919  Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) &&
4920  (VT == MVT::f32 || VT == MVT::f64 ||
4921  (VT == MVT::f16 && Subtarget->has16BitInsts())) &&
4922  Op0.hasOneUse()) {
4923  if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1))
4924  return Res;
4925  }
4926 
4927  return SDValue();
4928 }
4929 
4931  if (ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A)) {
4932  if (ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B)) {
4933  // FIXME: Should this be allowing -0.0?
4934  return (CA->isExactlyValue(0.0) && CB->isExactlyValue(1.0)) ||
4935  (CA->isExactlyValue(1.0) && CB->isExactlyValue(0.0));
4936  }
4937  }
4938 
4939  return false;
4940 }
4941 
4942 // FIXME: Should only worry about snans for version with chain.
4943 SDValue SITargetLowering::performFMed3Combine(SDNode *N,
4944  DAGCombinerInfo &DCI) const {
4945  EVT VT = N->getValueType(0);
4946  // v_med3_f32 and v_max_f32 behave identically wrt denorms, exceptions and
4947  // NaNs. With a NaN input, the order of the operands may change the result.
4948 
4949  SelectionDAG &DAG = DCI.DAG;
4950  SDLoc SL(N);
4951 
4952  SDValue Src0 = N->getOperand(0);
4953  SDValue Src1 = N->getOperand(1);
4954  SDValue Src2 = N->getOperand(2);
4955 
4956  if (isClampZeroToOne(Src0, Src1)) {
4957  // const_a, const_b, x -> clamp is safe in all cases including signaling
4958  // nans.
4959  // FIXME: Should this be allowing -0.0?
4960  return DAG.getNode(AMDGPUISD::CLAMP, SL, VT, Src2);
4961  }
4962 
4963  // FIXME: dx10_clamp behavior assumed in instcombine. Should we really bother
4964  // handling no dx10-clamp?
4965  if (Subtarget->enableDX10Clamp()) {
4966  // If NaNs is clamped to 0, we are free to reorder the inputs.
4967 
4968  if (isa<ConstantFPSDNode>(Src0) && !isa<ConstantFPSDNode>(Src1))
4969  std::swap(Src0, Src1);
4970 
4971  if (isa<ConstantFPSDNode>(Src1) && !isa<ConstantFPSDNode>(Src2))
4972  std::swap(Src1, Src2);
4973 
4974  if (isa<ConstantFPSDNode>(Src0) && !isa<ConstantFPSDNode>(Src1))
4975  std::swap(Src0, Src1);
4976 
4977  if (isClampZeroToOne(Src1, Src2))
4978  return DAG.getNode(AMDGPUISD::CLAMP, SL, VT, Src0);
4979  }
4980 
4981  return SDValue();
4982 }
4983 
4984 SDValue SITargetLowering::performCvtPkRTZCombine(SDNode *N,
4985  DAGCombinerInfo &DCI) const {
4986  SDValue Src0 = N->getOperand(0);
4987  SDValue Src1 = N->getOperand(1);
4988  if (Src0.isUndef() && Src1.isUndef())
4989  return DCI.DAG.getUNDEF(N->getValueType(0));
4990  return SDValue();
4991 }
4992 
4993 SDValue SITargetLowering::performExtractVectorEltCombine(
4994  SDNode *N, DAGCombinerInfo &DCI) const {
4995  SDValue Vec = N->getOperand(0);
4996 
4997  SelectionDAG &DAG= DCI.DAG;
4998  if (Vec.getOpcode() == ISD::FNEG && allUsesHaveSourceMods(N)) {
4999  SDLoc SL(N);
5000  EVT EltVT = N->getValueType(0);
5001  SDValue Idx = N->getOperand(1);
5002  SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
5003  Vec.getOperand(0), Idx);
5004  return DAG.getNode(ISD::FNEG, SL, EltVT, Elt);
5005  }
5006 
5007  return SDValue();
5008 }
5009 
5010 
5011 unsigned SITargetLowering::getFusedOpcode(const SelectionDAG &DAG,
5012  const SDNode *N0,
5013  const SDNode *N1) const {
5014  EVT VT = N0->getValueType(0);
5015 
5016  // Only do this if we are not trying to support denormals. v_mad_f32 does not
5017  // support denormals ever.
5018  if ((VT == MVT::f32 && !Subtarget->hasFP32Denormals()) ||
5019  (VT == MVT::f16 && !Subtarget->hasFP16Denormals()))
5020  return ISD::FMAD;
5021 
5022  const TargetOptions &Options = DAG.getTarget().Options;
5023  if ((Options.AllowFPOpFusion == FPOpFusion::Fast || Options.UnsafeFPMath ||
5024  (N0->getFlags().hasUnsafeAlgebra() &&
5025  N1->getFlags().hasUnsafeAlgebra())) &&
5027  return ISD::FMA;
5028  }
5029 
5030  return 0;
5031 }
5032 
5033 SDValue SITargetLowering::performAddCombine(SDNode *N,
5034  DAGCombinerInfo &DCI) const {
5035  SelectionDAG &DAG = DCI.DAG;
5036  EVT VT = N->getValueType(0);
5037 
5038  if (VT != MVT::i32)
5039  return SDValue();
5040 
5041  SDLoc SL(N);
5042  SDValue LHS = N->getOperand(0);
5043  SDValue RHS = N->getOperand(1);
5044 
5045  // add x, zext (setcc) => addcarry x, 0, setcc
5046  // add x, sext (setcc) => subcarry x, 0, setcc
5047  unsigned Opc = LHS.getOpcode();
5048  if (Opc == ISD::ZERO_EXTEND || Opc == ISD::SIGN_EXTEND ||
5049  Opc == ISD::ANY_EXTEND || Opc == ISD::ADDCARRY)
5050  std::swap(RHS, LHS);
5051 
5052  Opc = RHS.getOpcode();
5053  switch (Opc) {
5054  default: break;
5055  case ISD::ZERO_EXTEND:
5056  case ISD::SIGN_EXTEND:
5057  case ISD::ANY_EXTEND: {
5058  auto Cond = RHS.getOperand(0);
5059  if (!isBoolSGPR(Cond))
5060  break;
5061  SDVTList VTList = DAG.getVTList(MVT::i32, MVT::i1);
5062  SDValue Args[] = { LHS, DAG.getConstant(0, SL, MVT::i32), Cond };
5063  Opc = (Opc == ISD::SIGN_EXTEND) ? ISD::SUBCARRY : ISD::ADDCARRY;
5064  return DAG.getNode(Opc, SL, VTList, Args);
5065  }
5066  case ISD::ADDCARRY: {
5067  // add x, (addcarry y, 0, cc) => addcarry x, y, cc
5068  auto C = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
5069  if (!C || C->getZExtValue() != 0) break;
5070  SDValue Args[] = { LHS, RHS.getOperand(0), RHS.getOperand(2) };
5071  return DAG.getNode(ISD::ADDCARRY, SDLoc(N), RHS->getVTList(), Args);
5072  }
5073  }
5074  return SDValue();
5075 }
5076 
5077 SDValue SITargetLowering::performSubCombine(SDNode *N,
5078  DAGCombinerInfo &DCI) const {
5079  SelectionDAG &DAG = DCI.DAG;
5080  EVT VT = N->getValueType(0);
5081 
5082  if (VT != MVT::i32)
5083  return SDValue();
5084 
5085  SDLoc SL(N);
5086  SDValue LHS = N->getOperand(0);
5087  SDValue RHS = N->getOperand(1);
5088 
5089  unsigned Opc = LHS.getOpcode();
5090  if (Opc != ISD::SUBCARRY)
5091  std::swap(RHS, LHS);
5092 
5093  if (LHS.getOpcode() == ISD::SUBCARRY) {
5094  // sub (subcarry x, 0, cc), y => subcarry x, y, cc
5095  auto C = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
5096  if (!C || C->getZExtValue() != 0)
5097  return SDValue();
5098  SDValue Args[] = { LHS.getOperand(0), RHS, LHS.getOperand(2) };
5099  return DAG.getNode(ISD::SUBCARRY, SDLoc(N), LHS->getVTList(), Args);
5100  }
5101  return SDValue();
5102 }
5103 
5104 SDValue SITargetLowering::performAddCarrySubCarryCombine(SDNode *N,
5105  DAGCombinerInfo &DCI) const {
5106 
5107  if (N->getValueType(0) != MVT::i32)
5108  return SDValue();
5109 
5110  auto C = dyn_cast<ConstantSDNode>(N->getOperand(1));
5111  if (!C || C->getZExtValue() != 0)
5112  return SDValue();
5113 
5114  SelectionDAG &DAG = DCI.DAG;
5115  SDValue LHS = N->getOperand(0);
5116 
5117  // addcarry (add x, y), 0, cc => addcarry x, y, cc
5118  // subcarry (sub x, y), 0, cc => subcarry x, y, cc
5119  unsigned LHSOpc = LHS.getOpcode();
5120  unsigned Opc = N->getOpcode();
5121  if ((LHSOpc == ISD::ADD && Opc == ISD::ADDCARRY) ||
5122  (LHSOpc == ISD::SUB && Opc == ISD::SUBCARRY)) {
5123  SDValue Args[] = { LHS.getOperand(0), LHS.getOperand(1), N->getOperand(2) };
5124  return DAG.getNode(Opc, SDLoc(N), N->getVTList(), Args);
5125  }
5126  return SDValue();
5127 }
5128 
5129 SDValue SITargetLowering::performFAddCombine(SDNode *N,
5130  DAGCombinerInfo &DCI) const {
5131  if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
5132  return SDValue();
5133 
5134  SelectionDAG &DAG = DCI.DAG;
5135  EVT VT = N->getValueType(0);
5136 
5137  SDLoc SL(N);
5138  SDValue LHS = N->getOperand(0);
5139  SDValue RHS = N->getOperand(1);
5140 
5141  // These should really be instruction patterns, but writing patterns with
5142  // source modiifiers is a pain.
5143 
5144  // fadd (fadd (a, a), b) -> mad 2.0, a, b
5145  if (LHS.getOpcode() == ISD::FADD) {
5146  SDValue A = LHS.getOperand(0);
5147  if (A == LHS.getOperand(1)) {
5148  unsigned FusedOp = getFusedOpcode(DAG, N, LHS.getNode());
5149  if (FusedOp != 0) {
5150  const SDValue Two = DAG.getConstantFP(2.0, SL, VT);
5151  return DAG.getNode(FusedOp, SL, VT, A, Two, RHS);
5152  }
5153  }
5154  }
5155 
5156  // fadd (b, fadd (a, a)) -> mad 2.0, a, b
5157  if (RHS.getOpcode() == ISD::FADD) {
5158  SDValue A = RHS.getOperand(0);
5159  if (A == RHS.getOperand(1)) {
5160  unsigned FusedOp = getFusedOpcode(DAG, N, RHS.getNode());
5161  if (FusedOp != 0) {
5162  const SDValue Two = DAG.getConstantFP(2.0, SL, VT);
5163  return DAG.getNode(FusedOp, SL, VT, A, Two, LHS);
5164  }
5165  }
5166  }
5167 
5168  return SDValue();
5169 }
5170 
5171 SDValue SITargetLowering::performFSubCombine(SDNode *N,
5172  DAGCombinerInfo &DCI) const {
5173  if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
5174  return SDValue();
5175 
5176  SelectionDAG &DAG = DCI.DAG;
5177  SDLoc SL(N);
5178  EVT VT = N->getValueType(0);
5179  assert(!VT.isVector());
5180 
5181  // Try to get the fneg to fold into the source modifier. This undoes generic
5182  // DAG combines and folds them into the mad.
5183  //
5184  // Only do this if we are not trying to support denormals. v_mad_f32 does
5185  // not support denormals ever.
5186  SDValue LHS = N->getOperand(0);
5187  SDValue RHS = N->getOperand(1);
5188  if (LHS.getOpcode() == ISD::FADD) {
5189  // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
5190  SDValue A = LHS.getOperand(0);
5191  if (A == LHS.getOperand(1)) {
5192  unsigned FusedOp = getFusedOpcode(DAG, N, LHS.getNode());
5193  if (FusedOp != 0){
5194  const SDValue Two = DAG.getConstantFP(2.0, SL, VT);
5195  SDValue NegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
5196 
5197  return DAG.getNode(FusedOp, SL, VT, A, Two, NegRHS);
5198  }
5199  }
5200  }
5201 
5202  if (RHS.getOpcode() == ISD::FADD) {
5203  // (fsub c, (fadd a, a)) -> mad -2.0, a, c
5204 
5205  SDValue A = RHS.getOperand(0);
5206  if (A == RHS.getOperand(1)) {
5207  unsigned FusedOp = getFusedOpcode(DAG, N, RHS.getNode());
5208  if (FusedOp != 0){
5209  const SDValue NegTwo = DAG.getConstantFP(-2.0, SL, VT);
5210  return DAG.getNode(FusedOp, SL, VT, A, NegTwo, LHS);
5211  }
5212  }
5213  }
5214 
5215  return SDValue();
5216 }
5217 
5218 SDValue SITargetLowering::performSetCCCombine(SDNode *N,
5219  DAGCombinerInfo &DCI) const {
5220  SelectionDAG &DAG = DCI.DAG;
5221  SDLoc SL(N);
5222 
5223  SDValue LHS = N->getOperand(0);
5224  SDValue RHS = N->getOperand(1);
5225  EVT VT = LHS.getValueType();
5226  ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
5227 
5228  auto CRHS = dyn_cast<ConstantSDNode>(RHS);
5229  if (!CRHS) {
5230  CRHS = dyn_cast<ConstantSDNode>(LHS);
5231  if (CRHS) {
5232  std::swap(LHS, RHS);
5233  CC = getSetCCSwappedOperands(CC);
5234  }
5235  }
5236 
5237  if (CRHS && VT == MVT::i32 && LHS.getOpcode() == ISD::SIGN_EXTEND &&
5238  isBoolSGPR(LHS.getOperand(0))) {
5239  // setcc (sext from i1 cc), -1, ne|sgt|ult) => not cc => xor cc, -1
5240  // setcc (sext from i1 cc), -1, eq|sle|uge) => cc
5241  // setcc (sext from i1 cc), 0, eq|sge|ule) => not cc => xor cc, -1
5242  // setcc (sext from i1 cc), 0, ne|ugt|slt) => cc
5243  if ((CRHS->isAllOnesValue() &&
5244  (CC == ISD::SETNE || CC == ISD::SETGT || CC == ISD::SETULT)) ||
5245  (CRHS->isNullValue() &&
5246  (CC == ISD::SETEQ || CC == ISD::SETGE || CC == ISD::SETULE)))
5247  return DAG.getNode(ISD::XOR, SL, MVT::i1, LHS.getOperand(0),
5248  DAG.getConstant(-1, SL, MVT::i1));
5249  if ((CRHS->isAllOnesValue() &&
5250  (CC == ISD::SETEQ || CC == ISD::SETLE || CC == ISD::SETUGE)) ||
5251  (CRHS->isNullValue() &&
5252  (CC == ISD::SETNE || CC == ISD::SETUGT || CC == ISD::SETLT)))
5253  return LHS.getOperand(0);
5254  }
5255 
5256  if (VT != MVT::f32 && VT != MVT::f64 && (Subtarget->has16BitInsts() &&
5257  VT != MVT::f16))
5258  return SDValue();
5259 
5260  // Match isinf pattern
5261  // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
5262  if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
5263  const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
5264  if (!CRHS)
5265  return SDValue();
5266 
5267  const APFloat &APF = CRHS->getValueAPF();
5268  if (APF.isInfinity() && !APF.isNegative()) {
5270  return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
5271  DAG.getConstant(Mask, SL, MVT::i32));
5272  }
5273  }
5274 
5275  return SDValue();
5276 }
5277 
5278 SDValue SITargetLowering::performCvtF32UByteNCombine(SDNode *N,
5279  DAGCombinerInfo &DCI) const {
5280  SelectionDAG &DAG = DCI.DAG;
5281  SDLoc SL(N);
5282  unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
5283 
5284  SDValue Src = N->getOperand(0);
5285  SDValue Srl = N->getOperand(0);
5286  if (Srl.getOpcode() == ISD::ZERO_EXTEND)
5287  Srl = Srl.getOperand(0);
5288 
5289  // TODO: Handle (or x, (srl y, 8)) pattern when known bits are zero.
5290  if (Srl.getOpcode() == ISD::SRL) {
5291  // cvt_f32_ubyte0 (srl x, 16) -> cvt_f32_ubyte2 x
5292  // cvt_f32_ubyte1 (srl x, 16) -> cvt_f32_ubyte3 x
5293  // cvt_f32_ubyte0 (srl x, 8) -> cvt_f32_ubyte1 x
5294 
5295  if (const ConstantSDNode *C =
5296  dyn_cast<ConstantSDNode>(Srl.getOperand(1))) {
5297  Srl = DAG.getZExtOrTrunc(Srl.getOperand(0), SDLoc(Srl.getOperand(0)),
5298  EVT(MVT::i32));
5299 
5300  unsigned SrcOffset = C->getZExtValue() + 8 * Offset;
5301  if (SrcOffset < 32 && SrcOffset % 8 == 0) {
5302  return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0 + SrcOffset / 8, SL,
5303  MVT::f32, Srl);
5304  }
5305  }
5306  }
5307 
5308  APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
5309 
5310  KnownBits Known;
5311  TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
5312  !DCI.isBeforeLegalizeOps());
5313  const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5314  if (TLI.ShrinkDemandedConstant(Src, Demanded, TLO) ||
5315  TLI.SimplifyDemandedBits(Src, Demanded, Known, TLO)) {
5316  DCI.CommitTargetLoweringOpt(TLO);
5317  }
5318 
5319  return SDValue();
5320 }
5321 
5323  DAGCombinerInfo &DCI) const {
5324  switch (N->getOpcode()) {
5325  default:
5327  case ISD::ADD:
5328  return performAddCombine(N, DCI);
5329  case ISD::SUB:
5330  return performSubCombine(N, DCI);
5331  case ISD::ADDCARRY:
5332  case ISD::SUBCARRY:
5333  return performAddCarrySubCarryCombine(N, DCI);
5334  case ISD::FADD:
5335  return performFAddCombine(N, DCI);
5336  case ISD::FSUB:
5337  return performFSubCombine(N, DCI);
5338  case ISD::SETCC:
5339  return performSetCCCombine(N, DCI);
5340  case ISD::FMAXNUM:
5341  case ISD::FMINNUM:
5342  case ISD::SMAX:
5343  case ISD::SMIN:
5344  case ISD::UMAX:
5345  case ISD::UMIN:
5347  case AMDGPUISD::FMAX_LEGACY: {
5348  if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
5350  return performMinMaxCombine(N, DCI);
5351  break;
5352  }
5353  case ISD::LOAD:
5354  case ISD::STORE:
5355  case ISD::ATOMIC_LOAD:
5356  case ISD::ATOMIC_STORE:
5357  case ISD::ATOMIC_CMP_SWAP:
5359  case ISD::ATOMIC_SWAP:
5360  case ISD::ATOMIC_LOAD_ADD:
5361  case ISD::ATOMIC_LOAD_SUB:
5362  case ISD::ATOMIC_LOAD_AND:
5363  case ISD::ATOMIC_LOAD_OR:
5364  case ISD::ATOMIC_LOAD_XOR:
5365  case ISD::ATOMIC_LOAD_NAND:
5366  case ISD::ATOMIC_LOAD_MIN:
5367  case ISD::ATOMIC_LOAD_MAX:
5368  case ISD::ATOMIC_LOAD_UMIN:
5369  case ISD::ATOMIC_LOAD_UMAX:
5370  case AMDGPUISD::ATOMIC_INC:
5371  case AMDGPUISD::ATOMIC_DEC: // TODO: Target mem intrinsics.
5372  if (DCI.isBeforeLegalize())
5373  break;
5374  return performMemSDNodeCombine(cast<MemSDNode>(N), DCI);
5375  case ISD::AND:
5376  return performAndCombine(N, DCI);
5377  case ISD::OR:
5378  return performOrCombine(N, DCI);
5379  case ISD::XOR:
5380  return performXorCombine(N, DCI);
5381  case ISD::ZERO_EXTEND:
5382  return performZeroExtendCombine(N, DCI);
5383  case AMDGPUISD::FP_CLASS:
5384  return performClassCombine(N, DCI);
5385  case ISD::FCANONICALIZE:
5386  return performFCanonicalizeCombine(N, DCI);
5387  case AMDGPUISD::FRACT:
5388  case AMDGPUISD::RCP:
5389  case AMDGPUISD::RSQ:
5390  case AMDGPUISD::RCP_LEGACY:
5391  case AMDGPUISD::RSQ_LEGACY:
5392  case AMDGPUISD::RSQ_CLAMP:
5393  case AMDGPUISD::LDEXP: {
5394  SDValue Src = N->getOperand(0);
5395  if (Src.isUndef())
5396  return Src;
5397  break;
5398  }
5399  case ISD::SINT_TO_FP:
5400  case ISD::UINT_TO_FP:
5401  return performUCharToFloatCombine(N, DCI);
5406  return performCvtF32UByteNCombine(N, DCI);
5407  case AMDGPUISD::FMED3:
5408  return performFMed3Combine(N, DCI);
5410  return performCvtPkRTZCombine(N, DCI);
5411  case ISD::SCALAR_TO_VECTOR: {
5412  SelectionDAG &DAG = DCI.DAG;
5413  EVT VT = N->getValueType(0);
5414 
5415  // v2i16 (scalar_to_vector i16:x) -> v2i16 (bitcast (any_extend i16:x))
5416  if (VT == MVT::v2i16 || VT == MVT::v2f16) {
5417  SDLoc SL(N);
5418  SDValue Src = N->getOperand(0);
5419  EVT EltVT = Src.getValueType();
5420  if (EltVT == MVT::f16)
5421  Src = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Src);
5422 
5423  SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, Src);
5424  return DAG.getNode(ISD::BITCAST, SL, VT, Ext);
5425  }
5426 
5427  break;
5428  }
5430  return performExtractVectorEltCombine(N, DCI);
5431  }
5433 }
5434 
5435 /// \brief Helper function for adjustWritemask
5436 static unsigned SubIdx2Lane(unsigned Idx) {
5437  switch (Idx) {
5438  default: return 0;
5439  case AMDGPU::sub0: return 0;
5440  case AMDGPU::sub1: return 1;
5441  case AMDGPU::sub2: return 2;
5442  case AMDGPU::sub3: return 3;
5443  }
5444 }
5445 
5446 /// \brief Adjust the writemask of MIMG instructions
5447 void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
5448  SelectionDAG &DAG) const {
5449  SDNode *Users[4] = { };
5450  unsigned Lane = 0;
5451  unsigned DmaskIdx = (Node->getNumOperands() - Node->getNumValues() == 9) ? 2 : 3;
5452  unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx);
5453  unsigned NewDmask = 0;
5454 
5455  // Try to figure out the used register components
5456  for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
5457  I != E; ++I) {
5458 
5459  // Don't look at users of the chain.
5460  if (I.getUse().getResNo() != 0)
5461  continue;
5462 
5463  // Abort if we can't understand the usage
5464  if (!I->isMachineOpcode() ||
5465  I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
5466  return;
5467 
5468  // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
5469  // Note that subregs are packed, i.e. Lane==0 is the first bit set
5470  // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
5471  // set, etc.
5472  Lane = SubIdx2Lane(I->getConstantOperandVal(1));
5473 
5474  // Set which texture component corresponds to the lane.
5475  unsigned Comp;
5476  for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
5477  assert(Dmask);
5478  Comp = countTrailingZeros(Dmask);
5479  Dmask &= ~(1 << Comp);
5480  }
5481 
5482  // Abort if we have more than one user per component
5483  if (Users[Lane])
5484  return;
5485 
5486  Users[Lane] = *I;
5487  NewDmask |= 1 << Comp;
5488  }
5489 
5490  // Abort if there's no change
5491  if (NewDmask == OldDmask)
5492  return;
5493 
5494  // Adjust the writemask in the node
5495  std::vector<SDValue> Ops;
5496  Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx);
5497  Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
5498  Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end());
5499  Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
5500 
5501  // If we only got one lane, replace it with a copy
5502  // (if NewDmask has only one bit set...)
5503  if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
5504  SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
5505  MVT::i32);
5506  SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
5507  SDLoc(), Users[Lane]->getValueType(0),
5508  SDValue(Node, 0), RC);
5509  DAG.ReplaceAllUsesWith(Users[Lane], Copy);
5510  return;
5511  }
5512 
5513  // Update the users of the node with the new indices
5514  for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
5515  SDNode *User = Users[i];
5516  if (!User)
5517  continue;
5518 
5519  SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
5520  DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
5521 
5522  switch (Idx) {
5523  default: break;
5524  case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
5525  case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
5526  case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
5527  }
5528  }
5529 }
5530 
5531 static bool isFrameIndexOp(SDValue Op) {
5532  if (Op.getOpcode() == ISD::AssertZext)
5533  Op = Op.getOperand(0);
5534 
5535  return isa<FrameIndexSDNode>(Op);
5536 }
5537 
5538 /// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
5539 /// with frame index operands.
5540 /// LLVM assumes that inputs are to these instructions are registers.
5542  SelectionDAG &DAG) const {
5543  if (Node->getOpcode() == ISD::CopyToReg) {
5544  RegisterSDNode *DestReg = cast<RegisterSDNode>(Node->getOperand(1));
5545  SDValue SrcVal = Node->getOperand(2);
5546 
5547  // Insert a copy to a VReg_1 virtual register so LowerI1Copies doesn't have
5548  // to try understanding copies to physical registers.
5549  if (SrcVal.getValueType() == MVT::i1 &&
5551  SDLoc SL(Node);
5553  SDValue VReg = DAG.getRegister(
5554  MRI.createVirtualRegister(&AMDGPU::VReg_1RegClass), MVT::i1);
5555 
5556  SDNode *Glued = Node->getGluedNode();
5557  SDValue ToVReg
5558  = DAG.getCopyToReg(Node->getOperand(0), SL, VReg, SrcVal,
5559  SDValue(Glued, Glued ? Glued->getNumValues() - 1 : 0));
5560  SDValue ToResultReg
5561  = DAG.getCopyToReg(ToVReg, SL, SDValue(DestReg, 0),
5562  VReg, ToVReg.getValue(1));
5563  DAG.ReplaceAllUsesWith(Node, ToResultReg.getNode());
5564  DAG.RemoveDeadNode(Node);
5565  return ToResultReg.getNode();
5566  }
5567  }
5568 
5570  for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
5571  if (!isFrameIndexOp(Node->getOperand(i))) {
5572  Ops.push_back(Node->getOperand(i));
5573  continue;
5574  }
5575 
5576  SDLoc DL(Node);
5577  Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
5578  Node->getOperand(i).getValueType(),
5579  Node->getOperand(i)), 0));
5580  }
5581 
5582  DAG.UpdateNodeOperands(Node, Ops);
5583  return Node;
5584 }
5585 
5586 /// \brief Fold the instructions after selecting them.
5588  SelectionDAG &DAG) const {
5589  const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
5590  unsigned Opcode = Node->getMachineOpcode();
5591 
5592  if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore() &&
5593  !TII->isGather4(Opcode))
5594  adjustWritemask(Node, DAG);
5595 
5596  if (Opcode == AMDGPU::INSERT_SUBREG ||
5597  Opcode == AMDGPU::REG_SEQUENCE) {
5598  legalizeTargetIndependentNode(Node, DAG);
5599  return Node;
5600  }
5601  return Node;
5602 }
5603 
5604 /// \brief Assign the register class depending on the number of
5605 /// bits set in the writemask
5607  SDNode *Node) const {
5608  const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
5609 
5611 
5612  if (TII->isVOP3(MI.getOpcode())) {
5613  // Make sure constant bus requirements are respected.
5614  TII->legalizeOperandsVOP3(MRI, MI);
5615  return;
5616  }
5617 
5618  if (TII->isMIMG(MI)) {
5619  unsigned VReg = MI.getOperand(0).getReg();
5620  const TargetRegisterClass *RC = MRI.getRegClass(VReg);
5621  // TODO: Need mapping tables to handle other cases (register classes).
5622  if (RC != &AMDGPU::VReg_128RegClass)
5623  return;
5624 
5625  unsigned DmaskIdx = MI.getNumOperands() == 12 ? 3 : 4;
5626  unsigned Writemask = MI.getOperand(DmaskIdx).getImm();
5627  unsigned BitsSet = 0;
5628  for (unsigned i = 0; i < 4; ++i)
5629  BitsSet += Writemask & (1 << i) ? 1 : 0;
5630  switch (BitsSet) {
5631  default: return;
5632  case 1: RC = &AMDGPU::VGPR_32RegClass; break;
5633  case 2: RC = &AMDGPU::VReg_64RegClass; break;
5634  case 3: RC = &AMDGPU::VReg_96RegClass; break;
5635  }
5636 
5637  unsigned NewOpcode = TII->getMaskedMIMGOp(MI.getOpcode(), BitsSet);
5638  MI.setDesc(TII->get(NewOpcode));
5639  MRI.setRegClass(VReg, RC);
5640  return;
5641  }
5642 
5643  // Replace unused atomics with the no return version.
5644  int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI.getOpcode());
5645  if (NoRetAtomicOp != -1) {
5646  if (!Node->hasAnyUseOfValue(0)) {
5647  MI.setDesc(TII->get(NoRetAtomicOp));
5648  MI.RemoveOperand(0);
5649  return;
5650  }
5651 
5652  // For mubuf_atomic_cmpswap, we need to have tablegen use an extract_subreg
5653  // instruction, because the return type of these instructions is a vec2 of
5654  // the memory type, so it can be tied to the input operand.
5655  // This means these instructions always have a use, so we need to add a
5656  // special case to check if the atomic has only one extract_subreg use,
5657  // which itself has no uses.
5658  if ((Node->hasNUsesOfValue(1, 0) &&
5659  Node->use_begin()->isMachineOpcode() &&
5660  Node->use_begin()->getMachineOpcode() == AMDGPU::EXTRACT_SUBREG &&
5661  !Node->use_begin()->hasAnyUseOfValue(0))) {
5662  unsigned Def = MI.getOperand(0).getReg();
5663 
5664  // Change this into a noret atomic.
5665  MI.setDesc(TII->get(NoRetAtomicOp));
5666  MI.RemoveOperand(0);
5667 
5668  // If we only remove the def operand from the atomic instruction, the
5669  // extract_subreg will be left with a use of a vreg without a def.
5670  // So we need to insert an implicit_def to avoid machine verifier
5671  // errors.
5672  BuildMI(*MI.getParent(), MI, MI.getDebugLoc(),
5673  TII->get(AMDGPU::IMPLICIT_DEF), Def);
5674  }
5675  return;
5676  }
5677 }
5678 
5679 static SDValue buildSMovImm32(SelectionDAG &DAG, const SDLoc &DL,
5680  uint64_t Val) {
5681  SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
5682  return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
5683 }
5684 
5686  const SDLoc &DL,
5687  SDValue Ptr) const {
5688  const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
5689 
5690  // Build the half of the subregister with the constants before building the
5691  // full 128-bit register. If we are building multiple resource descriptors,
5692  // this will allow CSEing of the 2-component register.
5693  const SDValue Ops0[] = {
5694  DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
5695  buildSMovImm32(DAG, DL, 0),
5696  DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
5697  buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
5698  DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
5699  };
5700 
5701  SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
5702  MVT::v2i32, Ops0), 0);
5703 
5704  // Combine the constants and the pointer.
5705  const SDValue Ops1[] = {
5706  DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
5707  Ptr,
5708  DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
5709  SubRegHi,
5710  DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
5711  };
5712 
5713  return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
5714 }
5715 
5716 /// \brief Return a resource descriptor with the 'Add TID' bit enabled
5717 /// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
5718 /// of the resource descriptor) to create an offset, which is added to
5719 /// the resource pointer.
5721  SDValue Ptr, uint32_t RsrcDword1,
5722  uint64_t RsrcDword2And3) const {
5723  SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
5724  SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
5725  if (RsrcDword1) {
5726  PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
5727  DAG.getConstant(RsrcDword1, DL, MVT::i32)),
5728  0);
5729  }
5730 
5731  SDValue DataLo = buildSMovImm32(DAG, DL,
5732  RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
5733  SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
5734 
5735  const SDValue Ops[] = {
5736  DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
5737  PtrLo,
5738  DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
5739  PtrHi,
5740  DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
5741  DataLo,
5742  DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
5743  DataHi,
5744  DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
5745  };
5746 
5747  return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
5748 }
5749 
5750 //===----------------------------------------------------------------------===//
5751 // SI Inline Assembly Support
5752 //===----------------------------------------------------------------------===//
5753 
5754 std::pair<unsigned, const TargetRegisterClass *>
5756  StringRef Constraint,
5757  MVT VT) const {
5758  if (!isTypeLegal(VT))
5759  return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
5760 
5761  if (Constraint.size() == 1) {
5762  switch (Constraint[0]) {
5763  case 's':
5764  case 'r':
5765  switch (VT.getSizeInBits()) {
5766  default:
5767  return std::make_pair(0U, nullptr);
5768  case 32:
5769  case 16:
5770  return std::make_pair(0U, &AMDGPU::SReg_32_XM0RegClass);
5771  case 64:
5772  return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
5773  case 128:
5774  return std::make_pair(0U, &AMDGPU::SReg_128RegClass);
5775  case 256:
5776  return std::make_pair(0U, &AMDGPU::SReg_256RegClass);
5777  case 512:
5778  return std::make_pair(0U, &AMDGPU::SReg_512RegClass);
5779  }
5780 
5781  case 'v':
5782  switch (VT.getSizeInBits()) {
5783  default:
5784  return std::make_pair(0U, nullptr);
5785  case 32:
5786  case 16:
5787  return std::make_pair(0U, &AMDGPU::VGPR_32RegClass);
5788  case 64:
5789  return std::make_pair(0U, &AMDGPU::VReg_64RegClass);
5790  case 96:
5791  return std::make_pair(0U, &AMDGPU::VReg_96RegClass);
5792  case 128:
5793  return std::make_pair(0U, &AMDGPU::VReg_128RegClass);
5794  case 256:
5795  return std::make_pair(0U, &AMDGPU::VReg_256RegClass);
5796  case 512:
5797  return std::make_pair(0U, &AMDGPU::VReg_512RegClass);
5798  }
5799  }
5800  }
5801 
5802  if (Constraint.size() > 1) {
5803  const TargetRegisterClass *RC = nullptr;
5804  if (Constraint[1] == 'v') {
5805  RC = &AMDGPU::VGPR_32RegClass;
5806  } else if (Constraint[1] == 's') {
5807  RC = &AMDGPU::SGPR_32RegClass;
5808  }
5809 
5810  if (RC) {
5811  uint32_t Idx;
5812  bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
5813  if (!Failed && Idx < RC->getNumRegs())
5814  return std::make_pair(RC->getRegister(Idx), RC);
5815  }
5816  }
5817  return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
5818 }
5819 
5822  if (Constraint.size() == 1) {
5823  switch (Constraint[0]) {
5824  default: break;
5825  case 's':
5826  case 'v':
5827  return C_RegisterClass;
5828  }
5829  }
5830  return TargetLowering::getConstraintType(Constraint);
5831 }
5832 
5833 // Figure out which registers should be reserved for stack access. Only after
5834 // the function is legalized do we know all of the non-spill stack objects or if
5835 // calls are present.
5837  MachineRegisterInfo &MRI = MF.getRegInfo();
5839  const MachineFrameInfo &MFI = MF.getFrameInfo();
5840  const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
5841  const SIRegisterInfo *TRI = ST.getRegisterInfo();
5842 
5843  if (Info->isEntryFunction()) {
5844  // Callable functions have fixed registers used for stack access.
5845  reservePrivateMemoryRegs(getTargetMachine(), MF, *TRI, *Info);
5846  }
5847 
5848  // We have to assume the SP is needed in case there are calls in the function
5849  // during lowering. Calls are only detected after the function is
5850  // lowered. We're about to reserve registers, so don't bother using it if we
5851  // aren't really going to use it.
5852  bool NeedSP = !Info->isEntryFunction() ||
5853  MFI.hasVarSizedObjects() ||
5854  MFI.hasCalls();
5855 
5856  if (NeedSP) {
5857  unsigned ReservedStackPtrOffsetReg = TRI->reservedStackPtrOffsetReg(MF);
5858  Info->setStackPtrOffsetReg(ReservedStackPtrOffsetReg);
5859 
5860  assert(Info->getStackPtrOffsetReg() != Info->getFrameOffsetReg());
5861  assert(!TRI->isSubRegister(Info->getScratchRSrcReg(),
5862  Info->getStackPtrOffsetReg()));
5863  MRI.replaceRegWith(AMDGPU::SP_REG, Info->getStackPtrOffsetReg());
5864  }
5865 
5866  MRI.replaceRegWith(AMDGPU::PRIVATE_RSRC_REG, Info->getScratchRSrcReg());
5867  MRI.replaceRegWith(AMDGPU::FP_REG, Info->getFrameOffsetReg());
5868  MRI.replaceRegWith(AMDGPU::SCRATCH_WAVE_OFFSET_REG,
5869  Info->getScratchWaveOffsetReg());
5870 
5872 }
unsigned addImplicitBufferPtr(const SIRegisterInfo &TRI)
value_iterator value_begin() const
static const unsigned UNKNOWN_ADDRESS_SPACE
Definition: AMDGPU.h:202
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:497
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition: ISDOpcodes.h:544
constexpr bool isUInt< 32 >(uint64_t x)
Definition: MathExtras.h:341
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition: ISDOpcodes.h:511
SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool isVarArg, const SmallVectorImpl< ISD::OutputArg > &Outs, const SmallVectorImpl< SDValue > &OutVals, const SDLoc &DL, SelectionDAG &DAG) const override
This hook must be implemented to lower outgoing return values, described by the Outs array...
void push_back(const T &Elt)
Definition: SmallVector.h:211
BUILTIN_OP_END - This must be the last enum value in this list.
Definition: ISDOpcodes.h:827
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
static void r3(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, int I, uint32_t *Buf)
Definition: SHA1.cpp:61
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:109
const AMDGPURegisterInfo * getRegisterInfo() const override=0
MachineSDNode * wrapAddr64Rsrc(SelectionDAG &DAG, const SDLoc &DL, SDValue Ptr) const
ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition: Analysis.cpp:199
FMINNUM/FMAXNUM - Perform floating-point minimum or maximum on two values.
Definition: ISDOpcodes.h:568
BitVector & set()
Definition: BitVector.h:398
Interface definition for SIRegisterInfo.
SDValue getValue(unsigned R) const
This represents an addressing mode of: BaseGV + BaseOffs + BaseReg + Scale*ScaleReg If BaseGV is null...
const DebugLoc & getDebugLoc() const
static void r2(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, int I, uint32_t *Buf)
Definition: SHA1.cpp:55
int getMaskedMIMGOp(uint16_t Opcode, unsigned Channels) const
Given a MIMG Opcode that writes all 4 channels, return the equivalent opcode that writes Channels Cha...
MVT getValVT() const
std::pair< unsigned, const TargetRegisterClass * > getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const override
Given a physical register constraint (e.g.
virtual ConstraintType getConstraintType(StringRef Constraint) const
Given a constraint, return the type of constraint it is for this target.
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:194
bool isFMAFasterThanFMulAndFAdd(EVT VT) const override
Return true if an FMA operation is faster than a pair of fmul and fadd instructions.
LLVMContext * getContext() const
Definition: SelectionDAG.h:381
LLVMContext & Context
Diagnostic information for unsupported feature in backend.
AMDGPU specific subclass of TargetSubtarget.
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1542
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond)
Helper function to make it easier to build SetCC's if you just have an ISD::CondCode instead of an SD...
Definition: SelectionDAG.h:884
SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override
This method will be invoked for all target nodes and for any target-independent nodes that the target...
SDValue scalarizeVectorStore(StoreSDNode *ST, SelectionDAG &DAG) const
bool isLegalAddressingMode(const DataLayout &DL, const AddrMode &AM, Type *Ty, unsigned AS) const override
Return true if the addressing mode represented by AM is legal for this target, for a load/store of th...
bool isNaN() const
Definition: APFloat.h:1130
bool isTrapHandlerEnabled() const
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR (an vector value) starting with the ...
Definition: ISDOpcodes.h:332
bool isKnownNeverNaN(SDValue Op) const
Test whether the given SDValue is known to never be NaN.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:103
BR_CC - Conditional branch.
Definition: ISDOpcodes.h:616
virtual MachineBasicBlock * EmitInstrWithCustomInserter(MachineInstr &MI, MachineBasicBlock *MBB) const
This method should be implemented by targets that mark instructions with the 'usesCustomInserter' fla...
LocInfo getLocInfo() const
unsigned getRegister(unsigned i) const
Return the specified register in the class.
InputArg - This struct carries flags and type information about a single incoming (formal) argument o...
static MVT getVectorVT(MVT VT, unsigned NumElements)
unsigned getPrivateSegmentWaveByteOffsetSystemSGPR() const
bool hasOneUse() const
Return true if there is exactly one use of this node.
unsigned getDestAddressSpace() const
bool hasNUsesOfValue(unsigned NUses, unsigned Value) const
Return true if there are exactly NUSES uses of the indicated value.
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition: ISDOpcodes.h:341
static bool isBoolSGPR(SDValue V)
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node.
static void allocateSystemSGPRs(CCState &CCInfo, MachineFunction &MF, SIMachineFunctionInfo &Info, CallingConv::ID CallConv, bool IsShader)
const TargetMachine & getTargetMachine() const
SDVTList getVTList() const
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:51
unsigned createVirtualRegister(const TargetRegisterClass *RegClass)
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
bool hasUnalignedBufferAccess() const
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
Definition: MathExtras.h:281
virtual SDValue LowerGlobalAddress(AMDGPUMachineFunction *MFI, SDValue Op, SelectionDAG &DAG) const
unsigned getReg() const
bool canMergeStoresTo(unsigned AS, EVT MemVT, const SelectionDAG &DAG) const override
Returns if it's reasonable to merge stores to MemVT size.
const TargetMachine & getTarget() const
Definition: SelectionDAG.h:377
unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC)
addLiveIn - Add the specified physical register as a live-in value and create a corresponding virtual...
void legalizeOperandsVOP3(MachineRegisterInfo &MRI, MachineInstr &MI) const
Fix operands in MI to satisfy constant bus requirements.
SDNode * getGluedNode() const
If this node has a glue operand, return the node to which the glue operand points.
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:163
bool mayStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly modify memory.
Definition: MachineInstr.h:613
bool hasFastFMAF32() const
void setIsUndef(bool Val=true)
bool useVGPRIndexMode(bool UserEnable) const
DataLayout DL
unsigned addKernargSegmentPtr(const SIRegisterInfo &TRI)
void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB)
Transfers all the successors, as in transferSuccessors, and update PHI operands in the successor bloc...
const MachineInstrBuilder & add(const MachineOperand &MO) const
This class represents a function call, abstracting a target machine's calling convention.
const SIInstrInfo * getInstrInfo() const override
unsigned getSrcAddressSpace() const
const GlobalValue * getGlobal() const
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space...
Definition: Type.cpp:617
bool CCAssignFn(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, CCState &State)
CCAssignFn - This function assigns a location for Val, updating State to reflect the change...
float BitsToFloat(uint32_t Bits)
This function takes a 32-bit integer and returns the bit equivalent float.
Definition: MathExtras.h:573
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
unsigned getScratchWaveOffsetReg() const
bool getTgtMemIntrinsic(IntrinsicInfo &, const CallInst &, unsigned IntrinsicID) const override
Given an intrinsic, checks if on the target the intrinsic will need to map to a MemIntrinsicNode (tou...
Function Alias Analysis Results
Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
Definition: ValueTypes.cpp:205
unsigned getSizeInBits() const
bool isMemOpUniform(const SDNode *N) const
bool isSignaling() const
Definition: APFloat.h:1134
unsigned getByValSize() const
bool enableIEEEBit(const MachineFunction &MF) const
uint64_t getNullPointerValue(unsigned AddrSpace) const
Get the integer value of a null pointer in the given address space.
static bool bitOpWithConstantIsReducible(unsigned Opc, uint32_t Val)
EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, EVT VT) const override
Return the ValueType of the result of SETCC operations.
Val, Success, OUTCHAIN = ATOMIC_CMP_SWAP_WITH_SUCCESS(INCHAIN, ptr, cmp, swap) N.b.
Definition: ISDOpcodes.h:762
unsigned second
unsigned getNumOperands() const
Return the number of values used by this operation.
static SDValue getFPTernOp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &SL, EVT VT, SDValue A, SDValue B, SDValue C, SDValue GlueChain)
A debug info location.
Definition: DebugLoc.h:34
const SDValue & getOperand(unsigned Num) const
bool isSGPRClass(const TargetRegisterClass *RC) const
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
void markPSInputEnabled(unsigned Index)
SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS)
Helper function to make it easier to build Select's if you just have operands and don't want to check...
Definition: SelectionDAG.h:897
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:495
SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, EVT TVT, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
[US]{MIN/MAX} - Binary minimum or maximum or signed or unsigned integers.
Definition: ISDOpcodes.h:358
SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
iv Induction Variable Users
Definition: IVUsers.cpp:51
bool CheckReturn(const SmallVectorImpl< ISD::OutputArg > &ArgsFlags, CCAssignFn Fn)
CheckReturn - Analyze the return values of a function, returning true if the return can be performed ...
const SDValue & setRoot(SDValue N)
Set the current root tag of the SelectionDAG.
Definition: SelectionDAG.h:440
SDValue scalarizeVectorLoad(LoadSDNode *LD, SelectionDAG &DAG) const
Turn load of vector type into a load of the individual elements.
LLVM_READONLY int getAtomicNoRetOp(uint16_t Opcode)
CLAMP value between 0.0 and 1.0.
const SDValue & getBasePtr() const
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1 at the ...
Definition: ISDOpcodes.h:327
std::pair< SDValue, SDValue > split64BitValue(SDValue Op, SelectionDAG &DAG) const
Return 64-bit value Op as two 32-bit integers.
void ReplaceNodeResults(SDNode *N, SmallVectorImpl< SDValue > &Results, SelectionDAG &DAG) const override
This callback is invoked when a node result type is illegal for the target, and the operation was reg...
const AMDGPUImagePseudoSourceValue * getImagePSV() const
bool isAmdCodeObjectV2(const MachineFunction &MF) const
virtual bool isTypeDesirableForOp(unsigned, EVT VT) const
Return true if the target has native support for the specified value type and it is 'desirable' to us...
SDValue splitBinaryBitConstantOpImpl(DAGCombinerInfo &DCI, const SDLoc &SL, unsigned Opc, SDValue LHS, uint32_t ValLo, uint32_t ValHi) const
Split the 64-bit value LHS into two 32-bit components, and perform the binary operation Opc to it wit...
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition: ISDOpcodes.h:403
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition: ValueTypes.h:241
unsigned getScratchRSrcReg() const
Returns the physical register reserved for use as the resource descriptor for scratch accesses...
bool isRegLoc() const
bool isAllOnesValue() const
void setTruncStoreAction(MVT ValVT, MVT MemVT, LegalizeAction Action)
Indicate that the specified truncating store does not work with the specified type and indicate what ...
static const unsigned LOCAL_ADDRESS
Address space for local memory.
Definition: AMDGPU.h:175
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition: ISDOpcodes.h:159
A convenience struct that encapsulates a DAG, and two SDValues for returning information from TargetL...
const Triple & getTargetTriple() const
static SDValue emitRemovedIntrinsicError(SelectionDAG &DAG, const SDLoc &DL, EVT VT)
SDValue expandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG) const
Expands an unaligned store to 2 half-size stores for integer values, and possibly more for vectors...
bool isVector() const
Return true if this is a vector value type.
Definition: ValueTypes.h:151
void setPrivateSegmentWaveByteOffset(unsigned Reg)
#define FP_DENORM_FLUSH_NONE
Definition: SIDefines.h:448
void setDebuggerWorkGroupIDStackObjectIndex(unsigned Dim, int ObjectIdx)
Sets stack object index for Dim's work group ID to ObjectIdx.
bool isNegative() const
Return true if the value is negative.
unsigned reservedPrivateSegmentWaveByteOffsetReg(const MachineFunction &MF) const
Return the end register initially reserved for the scratch wave offset in case spilling is needed...
static unsigned findFirstFreeSGPR(CCState &CCInfo)
const AMDGPUBufferPseudoSourceValue * getBufferPSV() const
A description of a memory reference used in the backend.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
OpenCL uses address spaces to differentiate between various memory regions on the hardware...
Definition: AMDGPU.h:164
const HexagonInstrInfo * TII
bool getAddrModeArguments(IntrinsicInst *, SmallVectorImpl< Value * > &, Type *&) const override
CodeGenPrepare sinks address calculations into the same BB as Load/Store instructions reading the add...
Shift and rotation operations.
Definition: ISDOpcodes.h:378
static bool isGather4(const MachineInstr &MI)
Definition: SIInstrInfo.h:411
void setHasNonSpillStackObjects(bool StackObject=true)
unsigned addDispatchID(const SIRegisterInfo &TRI)
static bool isMIMG(const MachineInstr &MI)
Definition: SIInstrInfo.h:403
void setDebuggerWorkItemIDStackObjectIndex(unsigned Dim, int ObjectIdx)
Sets stack object index for Dim's work item ID to ObjectIdx.
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
static void allocateHSAUserSGPRs(CCState &CCInfo, MachineFunction &MF, const SIRegisterInfo &TRI, SIMachineFunctionInfo &Info)
SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s), MachineInstr opcode, and operands.
MachineFunction & getMachineFunction() const
Definition: SelectionDAG.h:375
bool isReg() const
isReg - Tests if this is a MO_Register operand.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
static unsigned SubIdx2Lane(unsigned Idx)
Helper function for adjustWritemask.
CopyToReg - This node has three operands: a chain, a register number to set to this value...
Definition: ISDOpcodes.h:170
unsigned SubReg
bool mayLoad(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read memory.
Definition: MachineInstr.h:600
LLVM_READONLY MachineOperand * getNamedOperand(MachineInstr &MI, unsigned OperandName) const
Returns the operand named Op.
const TargetRegisterClass * getRegClass(unsigned Reg) const
Return the register class of the specified virtual register.
LLVM_ATTRIBUTE_ALWAYS_INLINE R Default(const T &Value) const
Definition: StringSwitch.h:244
unsigned getAddressSpace() const
Reg
All possible values of the reg field in the ModR/M byte.
SimpleValueType SimpleTy
APInt bitcastToAPInt() const
Definition: APFloat.h:1094
bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const override
Return true if folding a constant offset with the given GlobalAddress is legal.
The memory access is dereferenceable (i.e., doesn't trap).
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition: ValueTypes.h:260
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN, ptr, amt) For double-word atomic operations: ValLo, ValHi, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amtLo, amtHi) ValLo, ValHi, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN, ptr, amtLo, amtHi) These correspond to the atomicrmw instruction.
Definition: ISDOpcodes.h:770
bool isUndef() const
void setOperationAction(unsigned Op, MVT VT, LegalizeAction Action)
Indicate that the specified operation does not work with the specified type and indicate what to do a...
SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
unsigned getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
This is an SDNode representing atomic operations.
bool getScalarizeGlobalBehavior() const
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s, unsigned base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override
This callback is invoked for operations that are unsupported by the target, which are registered to u...
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
This file implements a class to represent arbitrary precision integral constant values and operations...
OutputArg - This struct carries flags and a value for a single outgoing (actual) argument or outgoing...
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:60
unsigned getNumOperands() const
Access to explicit operands of the instruction.
Definition: MachineInstr.h:282
SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition: ValueTypes.h:265
static void reservePrivateMemoryRegs(const TargetMachine &TM, MachineFunction &MF, const SIRegisterInfo &TRI, SIMachineFunctionInfo &Info)
const AMDGPUAS &AS return SrcAS
SDValue getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either sign-extending or trunca...
void RemoveOperand(unsigned i)
Erase an operand from an instruction, leaving it with one fewer operand than it started with...
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Case(const char(&S)[N], const T &Value)
Definition: StringSwitch.h:74
bool isShuffleMaskLegal(const SmallVectorImpl< int > &, EVT) const override
Targets can use this to indicate that they only support some VECTOR_SHUFFLE operations, those with specific masks.
unsigned getLocReg() const
static void allocateSpecialInputVGPRs(CCState &CCInfo, MachineFunction &MF, const SIRegisterInfo &TRI, SIMachineFunctionInfo &Info)
Class to represent function types.
Definition: DerivedTypes.h:103
#define F(x, y, z)
Definition: MD5.cpp:55
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition: ISDOpcodes.h:454
void computeRegisterProperties(const TargetRegisterInfo *TRI)
Once all of the register classes are added, this allows us to compute derived properties we expose...
const AMDGPUSubtarget * Subtarget
void setStackPtrOffsetReg(unsigned Reg)
bool hasStackObjects() const
Return true if there are any stack objects in this function.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
value_iterator value_end() const
const RegList & Regs
Simple integer binary arithmetic operators.
Definition: ISDOpcodes.h:200
static MachineBasicBlock * emitIndirectSrc(MachineInstr &MI, MachineBasicBlock &MBB, const SISubtarget &ST)
TrapHandlerAbi getTrapHandlerAbi() const
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
bool enableAggressiveFMAFusion(EVT VT) const override
Return true if target always beneficiates from combining into FMA for a given value type...
static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc)
ConstraintType getConstraintType(StringRef Constraint) const override
Given a constraint, return the type of constraint it is for this target.
const SDValue & getBasePtr() const
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
Definition: SelectionDAG.h:816
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition: APFloat.h:166
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out...
Definition: ISDOpcodes.h:909
const APInt & getAPIntValue() const
EVT getValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
Return the EVT corresponding to this LLVM type.
EVT getMemoryVT() const
Return the type of the in-memory value.
CodeGenOpt::Level getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
int64_t getImm() const
ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred)
getFCmpCondCode - Return the ISD condition code corresponding to the given LLVM IR floating-point con...
Definition: Analysis.cpp:162
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
Definition: SelectionDAG.h:546
READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
Definition: ISDOpcodes.h:707
void addMemOperand(MachineFunction &MF, MachineMemOperand *MO)
Add a MachineMemOperand to the machine instruction.
QueuePtr(false)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
Generation getGeneration() const
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
unsigned getUndefRegState(bool B)
void markPSInputAllocated(unsigned Index)
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition: ISDOpcodes.h:151
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
static SDValue emitNonHSAIntrinsicError(SelectionDAG &DAG, const SDLoc &DL, EVT VT)
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *bb=nullptr)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
const DataLayout & getDataLayout() const
Definition: SelectionDAG.h:376
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition: ValueTypes.h:247
const SIRegisterInfo & getRegisterInfo() const
Definition: SIInstrInfo.h:125
Class to represent pointers.
Definition: DerivedTypes.h:467
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
This class is used to represent ISD::STORE nodes.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:279
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition: ISDOpcodes.h:497
uint32_t FloatToBits(float Float)
This function takes a float and returns the bit equivalent 32-bit integer.
Definition: MathExtras.h:593
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:121
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:139
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a vector with the specified, possibly variable...
Definition: ISDOpcodes.h:301
bool isPSInputAllocated(unsigned Index) const
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
Definition: APInt.h:629
static bool isFlatGlobalAddrSpace(unsigned AS, AMDGPUAS AMDGPUASI)
bool isDenormal() const
Definition: APFloat.h:1133
SDNode * getNode() const
get the SDNode which holds the desired result
constexpr uint64_t MinAlign(uint64_t A, uint64_t B)
A and B are either alignments or offsets.
Definition: MathExtras.h:602
T Min(T a, T b)
Definition: FuzzerDefs.h:90
virtual void finalizeLowering(MachineFunction &MF) const
Execute target specific actions to finalize target lowering.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
unsigned getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition: ValueTypes.h:304
static bool fp16SrcZerosHighBits(unsigned Opc)
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:43
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:135
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:401
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition: ISDOpcodes.h:166
unsigned UnsafeFPMath
UnsafeFPMath - This flag is enabled when the -enable-unsafe-fp-math flag is specified on the command ...
FPOpFusion::FPOpFusionMode AllowFPOpFusion
AllowFPOpFusion - This flag is set by the -fuse-fp-ops=xxx option.
Control flow instructions. These all have token chains.
Definition: ISDOpcodes.h:595
const SDValue & getBasePtr() const
std::pair< SDValue, SDValue > expandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG) const
Expands an unaligned load to 2 half-size loads for an integer, and possibly more for vectors...
constexpr bool isUInt< 8 >(uint64_t x)
Definition: MathExtras.h:335
unsigned const MachineRegisterInfo * MRI
std::size_t countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the least significant bit to the most stopping at the first 1...
Definition: MathExtras.h:112
unsigned getVectorNumElements() const
bool hasFloatingPointExceptions() const
Return true if target supports floating point exceptions.
Machine Value Type.
const SDValue & getOperand(unsigned i) const
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
bool isCompareAndSwap() const
Returns true if this SDNode represents cmpxchg atomic operation, false otherwise. ...
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.h:137
void addRegisterClass(MVT VT, const TargetRegisterClass *RC)
Add the specified register class as an available regclass for the specified value type...
Simple binary floating point operators.
Definition: ISDOpcodes.h:259
void setTargetDAGCombine(ISD::NodeType NT)
Targets should invoke this method for each target independent node that they want to provide a custom...
unsigned getPreloadedValue(const MachineFunction &MF, enum PreloadedValue Value) const
Returns the physical register that Value is stored in.
SDValue getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef< SDValue > Ops, EVT MemVT, MachinePointerInfo PtrInfo, unsigned Align=0, bool Vol=false, bool ReadMem=true, bool WriteMem=true, unsigned Size=0)
Creates a MemIntrinsicNode that may produce a result and takes a list of operands.
bool sge(const APInt &RHS) const
Signed greather or equal comparison.
Definition: APInt.h:1288
MVT getLocVT() const
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
SDNode * PostISelFolding(MachineSDNode *N, SelectionDAG &DAG) const override
Fold the instructions after selecting them.
bool isVector() const
Return true if this is a vector value type.
const SDNodeFlags getFlags() const
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:115
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
Definition: ISDOpcodes.h:884
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL...
Definition: ISDOpcodes.h:306
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:284
static MachineBasicBlock * emitIndirectDst(MachineInstr &MI, MachineBasicBlock &MBB, const SISubtarget &ST)
unsigned getLiveInVirtReg(unsigned PReg) const
getLiveInVirtReg - If PReg is a live-in physical register, return the corresponding live-in physical ...
#define A
Definition: LargeTest.cpp:12
EVT getOptimalMemOpType(uint64_t Size, unsigned DstAlign, unsigned SrcAlign, bool IsMemset, bool ZeroMemset, bool MemcpyStrSrc, MachineFunction &MF) const override
Returns the target specific optimal type for load and store operations as a result of memset...
bool isEntryFunctionCC(CallingConv::ID CC)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:598
bool hasUnalignedScratchAccess() const
SI DAG Lowering interface definition.
virtual std::pair< unsigned, const TargetRegisterClass * > getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const
Given a physical register constraint (e.g.
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This class provides iterator support for SDUse operands that use a specific SDNode.
SITargetLowering(const TargetMachine &tm, const SISubtarget &STI)
bool hasAllowReciprocal() const
unsigned PartOffset
Offset in bytes of current input value relative to the beginning of original argument.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:860
unsigned addQueuePtr(const SIRegisterInfo &TRI)
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1272
unsigned getOpcode() const
static unsigned getMOVRELDPseudo(const SIRegisterInfo &TRI, const TargetRegisterClass *VecRC)
TRAP - Trapping instruction.
Definition: ISDOpcodes.h:727
bool hasFP32Denormals() const
Value * getOperand(unsigned i) const
Definition: User.h:154
uint64_t getDefaultRsrcDataFormat() const
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition: ISDOpcodes.h:57
DEBUGTRAP - Trap intended to get the attention of a debugger.
Definition: ISDOpcodes.h:730
#define FP_DENORM_FLUSH_IN_FLUSH_OUT
Definition: SIDefines.h:445
void RemoveDeadNode(SDNode *N)
Remove the specified node from the system.
self_iterator getIterator()
Definition: ilist_node.h:82
The memory access is non-temporal.
CondCode getSetCCSwappedOperands(CondCode Operation)
Return the operation corresponding to (Y op X) when given the operation for (X op Y)...
static MVT getVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
Definition: ValueTypes.cpp:283
static std::pair< unsigned, int > computeIndirectRegAndOffset(const SIRegisterInfo &TRI, const TargetRegisterClass *SuperRC, unsigned VecReg, int Offset)
uint64_t getConstantOperandVal(unsigned Num) const
Helper method returns the integer value of a ConstantSDNode operand.
int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool Immutable, bool isAliased=false)
Create a new object at a fixed location on the stack.
bool hasMed3_16() const
use_iterator use_begin() const
Provide iteration support to walk over all uses of an SDNode.
The AMDGPU TargetMachine interface definition for hw codgen targets.
bool isVolatile() const
const SDValue & getValue() const
void AddPromotedToType(unsigned Opc, MVT OrigVT, MVT DestVT)
If Opc/OrigVT is specified as being promoted, the promotion code defaults to trying a larger integer/...
bool has16BitInsts() const
static void r1(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, int I, uint32_t *Buf)
Definition: SHA1.cpp:49
Bit counting operators with an undefined result for zero inputs.
Definition: ISDOpcodes.h:384
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo...
Definition: ISDOpcodes.h:756
bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if 'Op & Mask' is known to be zero.
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:529
unsigned REGION_ADDRESS
Address space for region memory.
Definition: AMDGPU.h:168
unsigned getSubReg() const
Extended Value Type.
Definition: ValueTypes.h:34
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1320
const APFloat & getValueAPF() const
void setScratchWaveOffsetReg(unsigned Reg)
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
bool shouldAssumeDSOLocal(const Module &M, const GlobalValue *GV) const
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
Definition: APInt.h:1611
This class contains a discriminated union of information about pointers in memory operands...
bool hasFP64Denormals() const
bool hasFlatInstOffsets() const
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
Definition: SelectionDAG.h:431
unsigned addDispatchPtr(const SIRegisterInfo &TRI)
SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands...
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
static CCAssignFn * CCAssignFnForCall(CallingConv::ID CC, bool IsVarArg)
Selects the correct CCAssignFn for a given CallingConvention value.
The memory access writes data.
bool isInlineConstant(const APInt &Imm) const
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition: ValueTypes.h:229
static bool setM0ToIndexFromSGPR(const SIInstrInfo *TII, MachineRegisterInfo &MRI, MachineInstr &MI, int Offset, bool UseGPRIdxMode, bool IsIndirectSrc)
const unsigned MaxDepth
static SDValue buildPCRelGlobalAddress(SelectionDAG &DAG, const GlobalValue *GV, const SDLoc &DL, unsigned Offset, EVT PtrVT, unsigned GAFlags=SIInstrInfo::MO_NONE)
SDValue SplitVectorStore(SDValue Op, SelectionDAG &DAG) const
Split a vector store into 2 stores of half the vector.
TokenFactor - This node takes multiple tokens as input and produces a single token result...
Definition: ISDOpcodes.h:50
MVT getScalarShiftAmountTy(const DataLayout &, EVT) const override
EVT is not used in-tree, but is used by out-of-tree target.
unsigned getABITypeAlignment(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
Definition: DataLayout.cpp:680
unsigned getReturnAddressReg(const MachineFunction &MF) const
bool memoperands_empty() const
Return true if we don't have any memory operands which described the the memory access done by this i...
Definition: MachineInstr.h:368
Iterator for intrusive lists based on ilist_node.
CCState - This class holds information needed while lowering arguments and return values...
unsigned countPopulation(T Value)
Count the number of set bits in a value.
Definition: MathExtras.h:512
#define E
Definition: LargeTest.cpp:27
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
const DebugLoc & getDebugLoc() const
void setDesc(const MCInstrDesc &tid)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one...
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
TargetLoweringBase::LegalizeTypeAction getPreferredVectorAction(EVT VT) const override
Return the preferred vector type legalization action.
Returns platform specific canonical encoding of a floating point number.
Definition: ISDOpcodes.h:293
#define B
Definition: LargeTest.cpp:24
static bool isKnownNeverSNan(SelectionDAG &DAG, SDValue Op)
SDValue SplitVectorLoad(SDValue Op, SelectionDAG &DAG) const
Split a vector load into 2 loads of half the vector.
bool isInfinity() const
Return true if the value is an infinity.
bool isNegative() const
Definition: APFloat.h:1132
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition: ISDOpcodes.h:313
SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
const SIRegisterInfo * getRegisterInfo() const override
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
Definition: SelectionDAG.h:209
SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
ADDRSPACECAST - This operator converts between pointers of different address spaces.
Definition: ISDOpcodes.h:548
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Calling convention used for Mesa pixel shaders.
Definition: CallingConv.h:193
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
Definition: SelectionDAG.h:698
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:190
CCValAssign - Represent assignment of one arg/retval to a location.
AMDGPUAS getAMDGPUAS() const
SDNode * legalizeTargetIndependentNode(SDNode *Node, SelectionDAG &DAG) const
Legalize target independent instructions (e.g.
BRCOND - Conditional branch.
Definition: ISDOpcodes.h:610
An SDNode that represents everything that will be needed to construct a MachineInstr.
const SDValue & getChain() const
Byte Swap and Counting operators.
Definition: ISDOpcodes.h:381
const size_t N
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
Definition: ISDOpcodes.h:554
static SDValue getFPBinOp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &SL, EVT VT, SDValue A, SDValue B, SDValue GlueChain)
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
This is an abstract virtual class for memory operations.
LLVM_READNONE bool isKernel(CallingConv::ID CC)
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:193
Represents one node in the SelectionDAG.
const fltSemantics & getFltSemantics() const
Definition: Type.h:169
SDValue LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool isVarArg, const SmallVectorImpl< ISD::InputArg > &Ins, const SDLoc &DL, SelectionDAG &DAG, SmallVectorImpl< SDValue > &InVals) const override
This hook must be implemented to lower the incoming (formal) arguments, described by the Ins array...
bool hasApertureRegs() const
SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
static bool canFoldOffset(unsigned OffsetSize, unsigned AS, const SISubtarget &STI)
Return true if the given offset Size in bytes can be folded into the immediate offsets of a memory in...
SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, unsigned Reg, SDValue N)
Definition: SelectionDAG.h:643
static SDNode * findUser(SDValue Value, unsigned Opcode)
Helper function for LowerBRCOND.
static bool allUsesHaveSourceMods(const SDNode *N, unsigned CostThreshold=4)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:923
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
ISD::LoadExtType getExtensionType() const
Return whether this is a plain node, or one of the varieties of value-extending loads.
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT...
Definition: ValueTypes.h:73
Target - Wrapper for Target specific information.
Class for arbitrary precision integers.
Definition: APInt.h:69
const Value * getValue() const
Return the base address of the memory access.
SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
bool isShader(CallingConv::ID cc)
Select(COND, TRUEVAL, FALSEVAL).
Definition: ISDOpcodes.h:388
Interface for the AMDGPU Implementation of the Intrinsic Info class.
op_iterator op_begin() const
void finalizeLowering(MachineFunction &MF) const override
Execute target specific actions to finalize target lowering.
static use_iterator use_end()
SDValue copyToM0(SelectionDAG &DAG, SDValue Chain, const SDLoc &DL, SDValue V) const
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition: ISDOpcodes.h:444
bool CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF, bool isVarArg, const SmallVectorImpl< ISD::OutputArg > &Outs, LLVMContext &Context) const override
This hook should be implemented to check whether the return values described by the Outs array can fi...
bool isAmdHsaOS() const
AddrMode
ARM Addressing Modes.
Definition: ARMBaseInfo.h:235
bool isMemLoc() const
unsigned getVectorNumElements() const
Definition: DerivedTypes.h:462
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition: ISDOpcodes.h:447
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:257
void replaceRegWith(unsigned FromReg, unsigned ToReg)
replaceRegWith - Replace all instances of FromReg with ToReg in the machine function.
FMINNAN/FMAXNAN - Behave identically to FMINNUM/FMAXNUM, except that when a single input is NaN...
Definition: ISDOpcodes.h:571
virtual TargetLoweringBase::LegalizeTypeAction getPreferredVectorAction(EVT VT) const
Return the preferred vector type legalization action.
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:395
bool allowsMisalignedMemoryAccesses(EVT VT, unsigned AS, unsigned Align, bool *IsFast) const override
Determine if the target supports unaligned memory accesses.
void AdjustInstrPostInstrSelection(MachineInstr &MI, SDNode *Node) const override
Assign the register class depending on the number of bits set in the writemask.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
static bool isVOP3(const MachineInstr &MI)
Definition: SIInstrInfo.h:347
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition: APInt.h:600
unsigned addPrivateSegmentBuffer(const SIRegisterInfo &TRI)
Flags
Flags values. These may be or'd together.
bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const override
Returns true if a cast between SrcAS and DestAS is a noop.
llvm_unreachable("Unknown RegBank!")
unsigned getAddressSpace() const
Return the address space for the associated pointer.
unsigned addFlatScratchInit(const SIRegisterInfo &TRI)
static CCAssignFn * CCAssignFnForReturn(CallingConv::ID CC, bool IsVarArg)
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
The memory access reads data.
bool supportsMinMaxDenormModes() const
static MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
bool isCheapAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const override
Returns true if a cast from SrcAS to DestAS is "cheap", such that e.g.
static std::vector< std::string > Flags
Definition: FlagsTest.cpp:8
const SISubtarget * getSubtarget() const
Representation of each machine instruction.
Definition: MachineInstr.h:59
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
ConstantFPSDNode * isConstOrConstSplatFP(SDValue V)
Returns the SDNode if it is a constant splat BuildVector or constant float.
These are IR-level optimization flags that may be propagated to SDNodes.
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:260
static SDValue buildSMovImm32(SelectionDAG &DAG, const SDLoc &DL, uint64_t Val)
SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, const SmallVectorImpl< ISD::OutputArg > &Outs, const SmallVectorImpl< SDValue > &OutVals, const SDLoc &DL, SelectionDAG &DAG) const override
This hook must be implemented to lower outgoing return values, described by the Outs array...
unsigned PartOffset
Offset in bytes of current output value relative to the beginning of original argument.
This class keeps track of the SPI_SP_INPUT_ADDR config register, which tells the hardware which inter...
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:119
Interface definition for SIInstrInfo.
constexpr bool isShiftedMask_64(uint64_t Value)
Return true if the argument contains a non-empty sequence of ones with the remainder zero (64 bit ver...
Definition: MathExtras.h:415
static bool isCanonicalized(SelectionDAG &DAG, SDValue Op, const SISubtarget *ST, unsigned MaxDepth=5)
Bitwise operators - logical and, logical or, logical xor.
Definition: ISDOpcodes.h:361
bool hasAnyUseOfValue(unsigned Value) const
Return true if there are any use of the indicated value.
bool isUndef() const
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
SDValue getCopyFromReg(SDValue Chain, const SDLoc &dl, unsigned Reg, EVT VT)
Definition: SelectionDAG.h:669
unsigned getMaxPrivateElementSize() const
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition: ISDOpcodes.h:462
#define C
Definition: LargeTest.cpp:25
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
Definition: ISDOpcodes.h:580
static bool isClampZeroToOne(SDValue A, SDValue B)
TargetOptions Options
Definition: TargetMachine.h:95
bool isMemOpHasNoClobberedMemOperand(const SDNode *N) const
unsigned getSizeInBits() const
Return the size of the specified value type in bits.
Definition: ValueTypes.h:292
unsigned FLAT_ADDRESS
Address space for flat memory.
Definition: AMDGPU.h:167
void ReplaceAllUsesWith(SDValue From, SDValue Op)
Modify anything using 'From' to use 'To' instead.
unsigned getOrigArgIndex() const
static cl::opt< bool > EnableVGPRIndexMode("amdgpu-vgpr-index-mode", cl::desc("Use GPR indexing mode instead of movrel for vector indexing"), cl::init(false))
#define I(x, y, z)
Definition: MD5.cpp:58
void AnalyzeReturn(const SmallVectorImpl< ISD::OutputArg > &Outs, CCAssignFn Fn)
AnalyzeReturn - Analyze the returned values of a return, incorporating info about the result values i...
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
The memory access always returns the same value (or traps).
bool isAllocated(unsigned Reg) const
isAllocated - Return true if the specified register (or an alias) is allocated.
void clearKillFlags(unsigned Reg) const
clearKillFlags - Iterate over all the uses of the given register and clear the kill flag from the Mac...
bool enableDX10Clamp() const
op_iterator op_end() const
unsigned getExplicitKernelArgOffset(const MachineFunction &MF) const
Returns the offset in bytes from the start of the input buffer of the first explicit kernel argument...
bool shouldEmitConstantsToTextSection(const Triple &TT)
void analyzeFormalArgumentsCompute(CCState &State, const SmallVectorImpl< ISD::InputArg > &Ins) const
The SelectionDAGBuilder will automatically promote function arguments with illegal types...
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
static volatile int Zero
bool hasUnsafeAlgebra() const
bool isUniformMMO(const MachineMemOperand *MMO)
unsigned getWavefrontSize() const
static void r0(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, int I, uint32_t *Buf)
Definition: SHA1.cpp:43
FSINCOS - Compute both fsin and fcos as a single operation.
Definition: ISDOpcodes.h:574
static const unsigned CONSTANT_ADDRESS
Address space for constant memory (VTX2)
Definition: AMDGPU.h:174
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
uint64_t getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type...
Definition: DataLayout.h:388
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:904
constexpr bool isUInt< 16 >(uint64_t x)
Definition: MathExtras.h:338
void setSimpleHint(unsigned VReg, unsigned PrefReg)
Specify the preferred register allocation hint for the specified virtual register.
EVT getValueType() const
Return the ValueType of the referenced return value.
SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
SDValue getCondCode(ISD::CondCode Cond)
static bool isFrameIndexOp(SDValue Op)
static MachineBasicBlock::iterator loadM0FromVGPR(const SIInstrInfo *TII, MachineBasicBlock &MBB, MachineInstr &MI, unsigned InitResultReg, unsigned PhiReg, int Offset, bool UseGPRIdxMode)
static const unsigned GLOBAL_ADDRESS
Address space for global memory (RAT0, VTX0).
Definition: AMDGPU.h:173
void setABIArgOffset(unsigned NewOffset)
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition: ISDOpcodes.h:319
unsigned getReg() const
getReg - Returns the register number.
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition: ValueTypes.h:136
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void insert(iterator MBBI, MachineBasicBlock *MBB)
SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
LegalizeTypeAction
This enum indicates whether a types are legal for a target, and if not, what action should be used to...
bool allowsMemoryAccess(LLVMContext &Context, const DataLayout &DL, EVT VT, unsigned AddrSpace=0, unsigned Alignment=1, bool *Fast=nullptr) const
Return true if the target supports a memory access of this type for the given address space and align...
MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
void setSchedulingPreference(Sched::Preference Pref)
Specify the target scheduling preference.
constexpr uint32_t Hi_32(uint64_t Value)
Return the high 32 bits of a 64 bit value.
Definition: MathExtras.h:276
SDValue CreateLiveInRegister(SelectionDAG &DAG, const TargetRegisterClass *RC, unsigned Reg, EVT VT, const SDLoc &SL, bool RawReg=false) const
Helper function that adds Reg to the LiveIn list of the DAG's MachineFunction.
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:545
LLVM Value Representation.
Definition: Value.h:73
FMA - Perform a * b + c with no intermediate rounding step.
Definition: ISDOpcodes.h:276
unsigned reservedPrivateSegmentBufferReg(const MachineFunction &MF) const
Return the end register initially reserved for the scratch buffer in case spilling is needed...
SDValue getRegister(unsigned Reg, EVT VT)
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned char TargetFlags=0) const
EVT changeTypeToInteger()
Return the type converted to an equivalently sized integer or vector with integer element type...
Definition: ValueTypes.h:115
SDValue getValueType(EVT)
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:81
bool isInfinity() const
Definition: APFloat.h:1129
bool hasVOP3PInsts() const
const TargetLowering & getTargetLoweringInfo() const
Definition: SelectionDAG.h:379
SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override
This method will be invoked for all target nodes and for any target-independent nodes that the target...
void ReplaceAllUsesOfValueWith(SDValue From, SDValue To)
Replace any uses of From with To, leaving uses of other values produced by From.Val alone...
static EVT getEquivalentMemType(LLVMContext &Context, EVT VT)
cmpResult compare(const APFloat &RHS) const
Definition: APFloat.h:1102
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:56
IRTranslator LLVM IR MI
void setRegClass(unsigned Reg, const TargetRegisterClass *RC)
setRegClass - Set the register class of the specified virtual register.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
FMAD - Perform a * b + c, while getting the same result as the separately rounded operations...
Definition: ISDOpcodes.h:280
std::pair< unsigned, unsigned > getFlatWorkGroupSizes(const Function &F) const
SetCC operator - This evaluates to a true value iff the condition is true.
Definition: ISDOpcodes.h:411
uint32_t getImplicitParameterOffset(const AMDGPUMachineFunction *MFI, const ImplicitParameter Param) const
Helper function that returns the byte offset of the given type of implicit parameter.
bool hasMin3Max3_16() const
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
bool hasFP16Denormals() const
unsigned getLocMemOffset() const
MVT getVectorElementType() const
Conversion operators.
Definition: ISDOpcodes.h:441
bool isTypeDesirableForOp(unsigned Op, EVT VT) const override
Return true if the target has native support for the specified value type and it is 'desirable' to us...
int * Ptr
T Max(T a, T b)
Definition: FuzzerDefs.h:91
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
Definition: SelectionDAG.h:434
OUTCHAIN = ATOMIC_STORE(INCHAIN, ptr, val) This corresponds to "store atomic" instruction.
Definition: ISDOpcodes.h:749
TRUNCATE - Completely drop the high bits.
Definition: ISDOpcodes.h:450
static void Split(std::vector< std::string > &V, StringRef S)
Splits a string of comma separated items in to a vector of strings.
unsigned getAlignment() const
unsigned AllocateReg(unsigned Reg)
AllocateReg - Attempt to allocate one register.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation...
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
MachineBasicBlock * EmitInstrWithCustomInserter(MachineInstr &MI, MachineBasicBlock *BB) const override
This method should be implemented by targets that mark instructions with the 'usesCustomInserter' fla...
FNEG, FABS, FSQRT, FSIN, FCOS, FPOWI, FPOW, FLOG, FLOG2, FLOG10, FEXP, FEXP2, FCEIL, FTRUNC, FRINT, FNEARBYINT, FROUND, FFLOOR - Perform various unary floating point operations.
Definition: ISDOpcodes.h:560
static void processShaderInputArgs(SmallVectorImpl< ISD::InputArg > &Splits, CallingConv::ID CallConv, ArrayRef< ISD::InputArg > Ins, BitVector &Skipped, FunctionType *FType, SIMachineFunctionInfo *Info)
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition: ValueTypes.h:64
MachineSDNode * buildRSRC(SelectionDAG &DAG, const SDLoc &DL, SDValue Ptr, uint32_t RsrcDword1, uint64_t RsrcDword2And3) const
Return a resource descriptor with the 'Add TID' bit enabled The TID (Thread ID) is multiplied by the ...
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:174
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
Definition: ISDOpcodes.h:745
const fltSemantics & getSemantics() const
Definition: APFloat.h:1140
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition: ISDOpcodes.h:349
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition: APFloat.h:886
bool shouldConvertConstantLoadToIntImm(const APInt &Imm, Type *Ty) const override
Return true if it is beneficial to convert a load of a constant to just the constant itself...
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition: ValueTypes.h:253
unsigned PRIVATE_ADDRESS
Address space for private memory.
Definition: AMDGPU.h:166
Carry-using nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:241
bool isMachineOpcode() const
Test if this node has a post-isel opcode, directly corresponding to a MachineInstr opcode...
static MachineBasicBlock::iterator emitLoadM0FromVGPRLoop(const SIInstrInfo *TII, MachineRegisterInfo &MRI, MachineBasicBlock &OrigBB, MachineBasicBlock &LoopBB, const DebugLoc &DL, const MachineOperand &IdxReg, unsigned InitReg, unsigned ResultReg, unsigned PhiReg, unsigned InitSaveExecReg, int Offset, bool UseGPRIdxMode)
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:44
unsigned getRegisterByName(const char *RegName, EVT VT, SelectionDAG &DAG) const override
Return the register ID of the name passed in.
unsigned getMachineOpcode() const
This may only be called if isMachineOpcode returns true.
SDValue getTargetGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, unsigned char TargetFlags=0)
Definition: SelectionDAG.h:588
MachineBasicBlock * splitKillBlock(MachineInstr &MI, MachineBasicBlock *BB) const
uint64_t getZExtValue() const
SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override
This callback is invoked for operations that are unsupported by the target, which are registered to u...
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition: ValueTypes.h:273
This class is used to represent ISD::LOAD nodes.