NVPTXRegisterInfo.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //===- NVPTXRegisterInfo.cpp - NVPTX Register Information -----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file contains the NVPTX implementation of the TargetRegisterInfo class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "NVPTXRegisterInfo.h"
  13. #include "NVPTX.h"
  14. #include "NVPTXSubtarget.h"
  15. #include "NVPTXTargetMachine.h"
  16. #include "llvm/ADT/BitVector.h"
  17. #include "llvm/CodeGen/MachineFrameInfo.h"
  18. #include "llvm/CodeGen/MachineFunction.h"
  19. #include "llvm/CodeGen/MachineInstrBuilder.h"
  20. #include "llvm/CodeGen/TargetInstrInfo.h"
  21. #include "llvm/MC/MachineLocation.h"
  22. using namespace llvm;
  23. #define DEBUG_TYPE "nvptx-reg-info"
  24. namespace llvm {
  25. std::string getNVPTXRegClassName(TargetRegisterClass const *RC) {
  26. if (RC == &NVPTX::Float32RegsRegClass)
  27. return ".f32";
  28. if (RC == &NVPTX::Float16RegsRegClass)
  29. // Ideally fp16 registers should be .f16, but this syntax is only
  30. // supported on sm_53+. On the other hand, .b16 registers are
  31. // accepted for all supported fp16 instructions on all GPU
  32. // variants, so we can use them instead.
  33. return ".b16";
  34. if (RC == &NVPTX::Float16x2RegsRegClass)
  35. return ".b32";
  36. if (RC == &NVPTX::Float64RegsRegClass)
  37. return ".f64";
  38. if (RC == &NVPTX::Int64RegsRegClass)
  39. // We use untyped (.b) integer registers here as NVCC does.
  40. // Correctness of generated code does not depend on register type,
  41. // but using .s/.u registers runs into ptxas bug that prevents
  42. // assembly of otherwise valid PTX into SASS. Despite PTX ISA
  43. // specifying only argument size for fp16 instructions, ptxas does
  44. // not allow using .s16 or .u16 arguments for .fp16
  45. // instructions. At the same time it allows using .s32/.u32
  46. // arguments for .fp16v2 instructions:
  47. //
  48. // .reg .b16 rb16
  49. // .reg .s16 rs16
  50. // add.f16 rb16,rb16,rb16; // OK
  51. // add.f16 rs16,rs16,rs16; // Arguments mismatch for instruction 'add'
  52. // but:
  53. // .reg .b32 rb32
  54. // .reg .s32 rs32
  55. // add.f16v2 rb32,rb32,rb32; // OK
  56. // add.f16v2 rs32,rs32,rs32; // OK
  57. return ".b64";
  58. if (RC == &NVPTX::Int32RegsRegClass)
  59. return ".b32";
  60. if (RC == &NVPTX::Int16RegsRegClass)
  61. return ".b16";
  62. if (RC == &NVPTX::Int1RegsRegClass)
  63. return ".pred";
  64. if (RC == &NVPTX::SpecialRegsRegClass)
  65. return "!Special!";
  66. return "INTERNAL";
  67. }
  68. std::string getNVPTXRegClassStr(TargetRegisterClass const *RC) {
  69. if (RC == &NVPTX::Float32RegsRegClass)
  70. return "%f";
  71. if (RC == &NVPTX::Float16RegsRegClass)
  72. return "%h";
  73. if (RC == &NVPTX::Float16x2RegsRegClass)
  74. return "%hh";
  75. if (RC == &NVPTX::Float64RegsRegClass)
  76. return "%fd";
  77. if (RC == &NVPTX::Int64RegsRegClass)
  78. return "%rd";
  79. if (RC == &NVPTX::Int32RegsRegClass)
  80. return "%r";
  81. if (RC == &NVPTX::Int16RegsRegClass)
  82. return "%rs";
  83. if (RC == &NVPTX::Int1RegsRegClass)
  84. return "%p";
  85. if (RC == &NVPTX::SpecialRegsRegClass)
  86. return "!Special!";
  87. return "INTERNAL";
  88. }
  89. }
  90. NVPTXRegisterInfo::NVPTXRegisterInfo() : NVPTXGenRegisterInfo(0) {}
  91. #define GET_REGINFO_TARGET_DESC
  92. #include "NVPTXGenRegisterInfo.inc"
  93. /// NVPTX Callee Saved Registers
  94. const MCPhysReg *
  95. NVPTXRegisterInfo::getCalleeSavedRegs(const MachineFunction *) const {
  96. static const MCPhysReg CalleeSavedRegs[] = { 0 };
  97. return CalleeSavedRegs;
  98. }
  99. BitVector NVPTXRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
  100. BitVector Reserved(getNumRegs());
  101. for (unsigned Reg = NVPTX::ENVREG0; Reg <= NVPTX::ENVREG31; ++Reg) {
  102. markSuperRegs(Reserved, Reg);
  103. }
  104. markSuperRegs(Reserved, NVPTX::VRFrame32);
  105. markSuperRegs(Reserved, NVPTX::VRFrameLocal32);
  106. markSuperRegs(Reserved, NVPTX::VRFrame64);
  107. markSuperRegs(Reserved, NVPTX::VRFrameLocal64);
  108. markSuperRegs(Reserved, NVPTX::VRDepot);
  109. return Reserved;
  110. }
  111. void NVPTXRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
  112. int SPAdj, unsigned FIOperandNum,
  113. RegScavenger *RS) const {
  114. assert(SPAdj == 0 && "Unexpected");
  115. MachineInstr &MI = *II;
  116. int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
  117. MachineFunction &MF = *MI.getParent()->getParent();
  118. int Offset = MF.getFrameInfo().getObjectOffset(FrameIndex) +
  119. MI.getOperand(FIOperandNum + 1).getImm();
  120. // Using I0 as the frame pointer
  121. MI.getOperand(FIOperandNum).ChangeToRegister(getFrameRegister(MF), false);
  122. MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
  123. }
  124. Register NVPTXRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
  125. const NVPTXTargetMachine &TM =
  126. static_cast<const NVPTXTargetMachine &>(MF.getTarget());
  127. return TM.is64Bit() ? NVPTX::VRFrame64 : NVPTX::VRFrame32;
  128. }
  129. Register
  130. NVPTXRegisterInfo::getFrameLocalRegister(const MachineFunction &MF) const {
  131. const NVPTXTargetMachine &TM =
  132. static_cast<const NVPTXTargetMachine &>(MF.getTarget());
  133. return TM.is64Bit() ? NVPTX::VRFrameLocal64 : NVPTX::VRFrameLocal32;
  134. }