X86RegisterBankInfo.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //===- X86RegisterBankInfo.cpp -----------------------------------*- C++ -*-==//
  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. /// \file
  9. /// This file implements the targeting of the RegisterBankInfo class for X86.
  10. /// \todo This should be generated by TableGen.
  11. //===----------------------------------------------------------------------===//
  12. #include "X86RegisterBankInfo.h"
  13. #include "X86InstrInfo.h"
  14. #include "llvm/CodeGen/GlobalISel/RegisterBank.h"
  15. #include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
  16. #include "llvm/CodeGen/MachineRegisterInfo.h"
  17. #include "llvm/CodeGen/TargetRegisterInfo.h"
  18. #define GET_TARGET_REGBANK_IMPL
  19. #include "X86GenRegisterBank.inc"
  20. using namespace llvm;
  21. // This file will be TableGen'ed at some point.
  22. #define GET_TARGET_REGBANK_INFO_IMPL
  23. #include "X86GenRegisterBankInfo.def"
  24. X86RegisterBankInfo::X86RegisterBankInfo(const TargetRegisterInfo &TRI)
  25. : X86GenRegisterBankInfo() {
  26. // validate RegBank initialization.
  27. const RegisterBank &RBGPR = getRegBank(X86::GPRRegBankID);
  28. (void)RBGPR;
  29. assert(&X86::GPRRegBank == &RBGPR && "Incorrect RegBanks inizalization.");
  30. // The GPR register bank is fully defined by all the registers in
  31. // GR64 + its subclasses.
  32. assert(RBGPR.covers(*TRI.getRegClass(X86::GR64RegClassID)) &&
  33. "Subclass not added?");
  34. assert(RBGPR.getSize() == 64 && "GPRs should hold up to 64-bit");
  35. }
  36. const RegisterBank &
  37. X86RegisterBankInfo::getRegBankFromRegClass(const TargetRegisterClass &RC,
  38. LLT) const {
  39. if (X86::GR8RegClass.hasSubClassEq(&RC) ||
  40. X86::GR16RegClass.hasSubClassEq(&RC) ||
  41. X86::GR32RegClass.hasSubClassEq(&RC) ||
  42. X86::GR64RegClass.hasSubClassEq(&RC) ||
  43. X86::LOW32_ADDR_ACCESSRegClass.hasSubClassEq(&RC) ||
  44. X86::LOW32_ADDR_ACCESS_RBPRegClass.hasSubClassEq(&RC))
  45. return getRegBank(X86::GPRRegBankID);
  46. if (X86::FR32XRegClass.hasSubClassEq(&RC) ||
  47. X86::FR64XRegClass.hasSubClassEq(&RC) ||
  48. X86::VR128XRegClass.hasSubClassEq(&RC) ||
  49. X86::VR256XRegClass.hasSubClassEq(&RC) ||
  50. X86::VR512RegClass.hasSubClassEq(&RC))
  51. return getRegBank(X86::VECRRegBankID);
  52. llvm_unreachable("Unsupported register kind yet.");
  53. }
  54. X86GenRegisterBankInfo::PartialMappingIdx
  55. X86GenRegisterBankInfo::getPartialMappingIdx(const LLT &Ty, bool isFP) {
  56. if ((Ty.isScalar() && !isFP) || Ty.isPointer()) {
  57. switch (Ty.getSizeInBits()) {
  58. case 1:
  59. case 8:
  60. return PMI_GPR8;
  61. case 16:
  62. return PMI_GPR16;
  63. case 32:
  64. return PMI_GPR32;
  65. case 64:
  66. return PMI_GPR64;
  67. case 128:
  68. return PMI_VEC128;
  69. break;
  70. default:
  71. llvm_unreachable("Unsupported register size.");
  72. }
  73. } else if (Ty.isScalar()) {
  74. switch (Ty.getSizeInBits()) {
  75. case 32:
  76. return PMI_FP32;
  77. case 64:
  78. return PMI_FP64;
  79. case 128:
  80. return PMI_VEC128;
  81. default:
  82. llvm_unreachable("Unsupported register size.");
  83. }
  84. } else {
  85. switch (Ty.getSizeInBits()) {
  86. case 128:
  87. return PMI_VEC128;
  88. case 256:
  89. return PMI_VEC256;
  90. case 512:
  91. return PMI_VEC512;
  92. default:
  93. llvm_unreachable("Unsupported register size.");
  94. }
  95. }
  96. return PMI_None;
  97. }
  98. void X86RegisterBankInfo::getInstrPartialMappingIdxs(
  99. const MachineInstr &MI, const MachineRegisterInfo &MRI, const bool isFP,
  100. SmallVectorImpl<PartialMappingIdx> &OpRegBankIdx) {
  101. unsigned NumOperands = MI.getNumOperands();
  102. for (unsigned Idx = 0; Idx < NumOperands; ++Idx) {
  103. auto &MO = MI.getOperand(Idx);
  104. if (!MO.isReg())
  105. OpRegBankIdx[Idx] = PMI_None;
  106. else
  107. OpRegBankIdx[Idx] = getPartialMappingIdx(MRI.getType(MO.getReg()), isFP);
  108. }
  109. }
  110. bool X86RegisterBankInfo::getInstrValueMapping(
  111. const MachineInstr &MI,
  112. const SmallVectorImpl<PartialMappingIdx> &OpRegBankIdx,
  113. SmallVectorImpl<const ValueMapping *> &OpdsMapping) {
  114. unsigned NumOperands = MI.getNumOperands();
  115. for (unsigned Idx = 0; Idx < NumOperands; ++Idx) {
  116. if (!MI.getOperand(Idx).isReg())
  117. continue;
  118. auto Mapping = getValueMapping(OpRegBankIdx[Idx], 1);
  119. if (!Mapping->isValid())
  120. return false;
  121. OpdsMapping[Idx] = Mapping;
  122. }
  123. return true;
  124. }
  125. const RegisterBankInfo::InstructionMapping &
  126. X86RegisterBankInfo::getSameOperandsMapping(const MachineInstr &MI,
  127. bool isFP) const {
  128. const MachineFunction &MF = *MI.getParent()->getParent();
  129. const MachineRegisterInfo &MRI = MF.getRegInfo();
  130. unsigned NumOperands = MI.getNumOperands();
  131. LLT Ty = MRI.getType(MI.getOperand(0).getReg());
  132. if (NumOperands != 3 || (Ty != MRI.getType(MI.getOperand(1).getReg())) ||
  133. (Ty != MRI.getType(MI.getOperand(2).getReg())))
  134. llvm_unreachable("Unsupported operand mapping yet.");
  135. auto Mapping = getValueMapping(getPartialMappingIdx(Ty, isFP), 3);
  136. return getInstructionMapping(DefaultMappingID, 1, Mapping, NumOperands);
  137. }
  138. const RegisterBankInfo::InstructionMapping &
  139. X86RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
  140. const MachineFunction &MF = *MI.getParent()->getParent();
  141. const MachineRegisterInfo &MRI = MF.getRegInfo();
  142. unsigned Opc = MI.getOpcode();
  143. // Try the default logic for non-generic instructions that are either copies
  144. // or already have some operands assigned to banks.
  145. if (!isPreISelGenericOpcode(Opc) || Opc == TargetOpcode::G_PHI) {
  146. const InstructionMapping &Mapping = getInstrMappingImpl(MI);
  147. if (Mapping.isValid())
  148. return Mapping;
  149. }
  150. switch (Opc) {
  151. case TargetOpcode::G_ADD:
  152. case TargetOpcode::G_SUB:
  153. case TargetOpcode::G_MUL:
  154. return getSameOperandsMapping(MI, false);
  155. case TargetOpcode::G_FADD:
  156. case TargetOpcode::G_FSUB:
  157. case TargetOpcode::G_FMUL:
  158. case TargetOpcode::G_FDIV:
  159. return getSameOperandsMapping(MI, true);
  160. case TargetOpcode::G_SHL:
  161. case TargetOpcode::G_LSHR:
  162. case TargetOpcode::G_ASHR: {
  163. unsigned NumOperands = MI.getNumOperands();
  164. LLT Ty = MRI.getType(MI.getOperand(0).getReg());
  165. auto Mapping = getValueMapping(getPartialMappingIdx(Ty, false), 3);
  166. return getInstructionMapping(DefaultMappingID, 1, Mapping, NumOperands);
  167. }
  168. default:
  169. break;
  170. }
  171. unsigned NumOperands = MI.getNumOperands();
  172. SmallVector<PartialMappingIdx, 4> OpRegBankIdx(NumOperands);
  173. switch (Opc) {
  174. case TargetOpcode::G_FPEXT:
  175. case TargetOpcode::G_FPTRUNC:
  176. case TargetOpcode::G_FCONSTANT:
  177. // Instruction having only floating-point operands (all scalars in VECRReg)
  178. getInstrPartialMappingIdxs(MI, MRI, /* isFP */ true, OpRegBankIdx);
  179. break;
  180. case TargetOpcode::G_SITOFP:
  181. case TargetOpcode::G_FPTOSI: {
  182. // Some of the floating-point instructions have mixed GPR and FP operands:
  183. // fine-tune the computed mapping.
  184. auto &Op0 = MI.getOperand(0);
  185. auto &Op1 = MI.getOperand(1);
  186. const LLT Ty0 = MRI.getType(Op0.getReg());
  187. const LLT Ty1 = MRI.getType(Op1.getReg());
  188. bool FirstArgIsFP = Opc == TargetOpcode::G_SITOFP;
  189. bool SecondArgIsFP = Opc == TargetOpcode::G_FPTOSI;
  190. OpRegBankIdx[0] = getPartialMappingIdx(Ty0, /* isFP */ FirstArgIsFP);
  191. OpRegBankIdx[1] = getPartialMappingIdx(Ty1, /* isFP */ SecondArgIsFP);
  192. break;
  193. }
  194. case TargetOpcode::G_FCMP: {
  195. LLT Ty1 = MRI.getType(MI.getOperand(2).getReg());
  196. LLT Ty2 = MRI.getType(MI.getOperand(3).getReg());
  197. (void)Ty2;
  198. assert(Ty1.getSizeInBits() == Ty2.getSizeInBits() &&
  199. "Mismatched operand sizes for G_FCMP");
  200. unsigned Size = Ty1.getSizeInBits();
  201. (void)Size;
  202. assert((Size == 32 || Size == 64) && "Unsupported size for G_FCMP");
  203. auto FpRegBank = getPartialMappingIdx(Ty1, /* isFP */ true);
  204. OpRegBankIdx = {PMI_GPR8,
  205. /* Predicate */ PMI_None, FpRegBank, FpRegBank};
  206. break;
  207. }
  208. case TargetOpcode::G_TRUNC:
  209. case TargetOpcode::G_ANYEXT: {
  210. auto &Op0 = MI.getOperand(0);
  211. auto &Op1 = MI.getOperand(1);
  212. const LLT Ty0 = MRI.getType(Op0.getReg());
  213. const LLT Ty1 = MRI.getType(Op1.getReg());
  214. bool isFPTrunc = (Ty0.getSizeInBits() == 32 || Ty0.getSizeInBits() == 64) &&
  215. Ty1.getSizeInBits() == 128 && Opc == TargetOpcode::G_TRUNC;
  216. bool isFPAnyExt =
  217. Ty0.getSizeInBits() == 128 &&
  218. (Ty1.getSizeInBits() == 32 || Ty1.getSizeInBits() == 64) &&
  219. Opc == TargetOpcode::G_ANYEXT;
  220. getInstrPartialMappingIdxs(MI, MRI, /* isFP */ isFPTrunc || isFPAnyExt,
  221. OpRegBankIdx);
  222. } break;
  223. default:
  224. // Track the bank of each register, use NotFP mapping (all scalars in GPRs)
  225. getInstrPartialMappingIdxs(MI, MRI, /* isFP */ false, OpRegBankIdx);
  226. break;
  227. }
  228. // Finally construct the computed mapping.
  229. SmallVector<const ValueMapping *, 8> OpdsMapping(NumOperands);
  230. if (!getInstrValueMapping(MI, OpRegBankIdx, OpdsMapping))
  231. return getInvalidInstructionMapping();
  232. return getInstructionMapping(DefaultMappingID, /* Cost */ 1,
  233. getOperandsMapping(OpdsMapping), NumOperands);
  234. }
  235. void X86RegisterBankInfo::applyMappingImpl(
  236. const OperandsMapper &OpdMapper) const {
  237. return applyDefaultMapping(OpdMapper);
  238. }
  239. RegisterBankInfo::InstructionMappings
  240. X86RegisterBankInfo::getInstrAlternativeMappings(const MachineInstr &MI) const {
  241. const MachineFunction &MF = *MI.getParent()->getParent();
  242. const TargetSubtargetInfo &STI = MF.getSubtarget();
  243. const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
  244. const MachineRegisterInfo &MRI = MF.getRegInfo();
  245. switch (MI.getOpcode()) {
  246. case TargetOpcode::G_LOAD:
  247. case TargetOpcode::G_STORE:
  248. case TargetOpcode::G_IMPLICIT_DEF: {
  249. // we going to try to map 32/64 bit to PMI_FP32/PMI_FP64
  250. unsigned Size = getSizeInBits(MI.getOperand(0).getReg(), MRI, TRI);
  251. if (Size != 32 && Size != 64)
  252. break;
  253. unsigned NumOperands = MI.getNumOperands();
  254. // Track the bank of each register, use FP mapping (all scalars in VEC)
  255. SmallVector<PartialMappingIdx, 4> OpRegBankIdx(NumOperands);
  256. getInstrPartialMappingIdxs(MI, MRI, /* isFP */ true, OpRegBankIdx);
  257. // Finally construct the computed mapping.
  258. SmallVector<const ValueMapping *, 8> OpdsMapping(NumOperands);
  259. if (!getInstrValueMapping(MI, OpRegBankIdx, OpdsMapping))
  260. break;
  261. const RegisterBankInfo::InstructionMapping &Mapping = getInstructionMapping(
  262. /*ID*/ 1, /*Cost*/ 1, getOperandsMapping(OpdsMapping), NumOperands);
  263. InstructionMappings AltMappings;
  264. AltMappings.push_back(&Mapping);
  265. return AltMappings;
  266. }
  267. default:
  268. break;
  269. }
  270. return RegisterBankInfo::getInstrAlternativeMappings(MI);
  271. }