RDFRegisters.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. //===- RDFRegisters.cpp ---------------------------------------------------===//
  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. #include "llvm/ADT/BitVector.h"
  9. #include "llvm/CodeGen/MachineFunction.h"
  10. #include "llvm/CodeGen/MachineInstr.h"
  11. #include "llvm/CodeGen/MachineOperand.h"
  12. #include "llvm/CodeGen/RDFRegisters.h"
  13. #include "llvm/CodeGen/TargetRegisterInfo.h"
  14. #include "llvm/MC/LaneBitmask.h"
  15. #include "llvm/MC/MCRegisterInfo.h"
  16. #include "llvm/Support/ErrorHandling.h"
  17. #include "llvm/Support/raw_ostream.h"
  18. #include <cassert>
  19. #include <cstdint>
  20. #include <set>
  21. #include <utility>
  22. using namespace llvm;
  23. using namespace rdf;
  24. PhysicalRegisterInfo::PhysicalRegisterInfo(const TargetRegisterInfo &tri,
  25. const MachineFunction &mf)
  26. : TRI(tri) {
  27. RegInfos.resize(TRI.getNumRegs());
  28. BitVector BadRC(TRI.getNumRegs());
  29. for (const TargetRegisterClass *RC : TRI.regclasses()) {
  30. for (MCPhysReg R : *RC) {
  31. RegInfo &RI = RegInfos[R];
  32. if (RI.RegClass != nullptr && !BadRC[R]) {
  33. if (RC->LaneMask != RI.RegClass->LaneMask) {
  34. BadRC.set(R);
  35. RI.RegClass = nullptr;
  36. }
  37. } else
  38. RI.RegClass = RC;
  39. }
  40. }
  41. UnitInfos.resize(TRI.getNumRegUnits());
  42. for (uint32_t U = 0, NU = TRI.getNumRegUnits(); U != NU; ++U) {
  43. if (UnitInfos[U].Reg != 0)
  44. continue;
  45. MCRegUnitRootIterator R(U, &TRI);
  46. assert(R.isValid());
  47. RegisterId F = *R;
  48. ++R;
  49. if (R.isValid()) {
  50. UnitInfos[U].Mask = LaneBitmask::getAll();
  51. UnitInfos[U].Reg = F;
  52. } else {
  53. for (MCRegUnitMaskIterator I(F, &TRI); I.isValid(); ++I) {
  54. std::pair<uint32_t,LaneBitmask> P = *I;
  55. UnitInfo &UI = UnitInfos[P.first];
  56. UI.Reg = F;
  57. if (P.second.any()) {
  58. UI.Mask = P.second;
  59. } else {
  60. if (const TargetRegisterClass *RC = RegInfos[F].RegClass)
  61. UI.Mask = RC->LaneMask;
  62. else
  63. UI.Mask = LaneBitmask::getAll();
  64. }
  65. }
  66. }
  67. }
  68. for (const uint32_t *RM : TRI.getRegMasks())
  69. RegMasks.insert(RM);
  70. for (const MachineBasicBlock &B : mf)
  71. for (const MachineInstr &In : B)
  72. for (const MachineOperand &Op : In.operands())
  73. if (Op.isRegMask())
  74. RegMasks.insert(Op.getRegMask());
  75. MaskInfos.resize(RegMasks.size()+1);
  76. for (uint32_t M = 1, NM = RegMasks.size(); M <= NM; ++M) {
  77. BitVector PU(TRI.getNumRegUnits());
  78. const uint32_t *MB = RegMasks.get(M);
  79. for (unsigned I = 1, E = TRI.getNumRegs(); I != E; ++I) {
  80. if (!(MB[I / 32] & (1u << (I % 32))))
  81. continue;
  82. for (MCRegUnitIterator U(MCRegister::from(I), &TRI); U.isValid(); ++U)
  83. PU.set(*U);
  84. }
  85. MaskInfos[M].Units = PU.flip();
  86. }
  87. AliasInfos.resize(TRI.getNumRegUnits());
  88. for (uint32_t U = 0, NU = TRI.getNumRegUnits(); U != NU; ++U) {
  89. BitVector AS(TRI.getNumRegs());
  90. for (MCRegUnitRootIterator R(U, &TRI); R.isValid(); ++R)
  91. for (MCSuperRegIterator S(*R, &TRI, true); S.isValid(); ++S)
  92. AS.set(*S);
  93. AliasInfos[U].Regs = AS;
  94. }
  95. }
  96. std::set<RegisterId> PhysicalRegisterInfo::getAliasSet(RegisterId Reg) const {
  97. // Do not include RR in the alias set.
  98. std::set<RegisterId> AS;
  99. assert(isRegMaskId(Reg) || Register::isPhysicalRegister(Reg));
  100. if (isRegMaskId(Reg)) {
  101. // XXX SLOW
  102. const uint32_t *MB = getRegMaskBits(Reg);
  103. for (unsigned i = 1, e = TRI.getNumRegs(); i != e; ++i) {
  104. if (MB[i/32] & (1u << (i%32)))
  105. continue;
  106. AS.insert(i);
  107. }
  108. for (const uint32_t *RM : RegMasks) {
  109. RegisterId MI = getRegMaskId(RM);
  110. if (MI != Reg && aliasMM(RegisterRef(Reg), RegisterRef(MI)))
  111. AS.insert(MI);
  112. }
  113. return AS;
  114. }
  115. for (MCRegAliasIterator AI(Reg, &TRI, false); AI.isValid(); ++AI)
  116. AS.insert(*AI);
  117. for (const uint32_t *RM : RegMasks) {
  118. RegisterId MI = getRegMaskId(RM);
  119. if (aliasRM(RegisterRef(Reg), RegisterRef(MI)))
  120. AS.insert(MI);
  121. }
  122. return AS;
  123. }
  124. bool PhysicalRegisterInfo::aliasRR(RegisterRef RA, RegisterRef RB) const {
  125. assert(Register::isPhysicalRegister(RA.Reg));
  126. assert(Register::isPhysicalRegister(RB.Reg));
  127. MCRegUnitMaskIterator UMA(RA.Reg, &TRI);
  128. MCRegUnitMaskIterator UMB(RB.Reg, &TRI);
  129. // Reg units are returned in the numerical order.
  130. while (UMA.isValid() && UMB.isValid()) {
  131. // Skip units that are masked off in RA.
  132. std::pair<RegisterId,LaneBitmask> PA = *UMA;
  133. if (PA.second.any() && (PA.second & RA.Mask).none()) {
  134. ++UMA;
  135. continue;
  136. }
  137. // Skip units that are masked off in RB.
  138. std::pair<RegisterId,LaneBitmask> PB = *UMB;
  139. if (PB.second.any() && (PB.second & RB.Mask).none()) {
  140. ++UMB;
  141. continue;
  142. }
  143. if (PA.first == PB.first)
  144. return true;
  145. if (PA.first < PB.first)
  146. ++UMA;
  147. else if (PB.first < PA.first)
  148. ++UMB;
  149. }
  150. return false;
  151. }
  152. bool PhysicalRegisterInfo::aliasRM(RegisterRef RR, RegisterRef RM) const {
  153. assert(Register::isPhysicalRegister(RR.Reg) && isRegMaskId(RM.Reg));
  154. const uint32_t *MB = getRegMaskBits(RM.Reg);
  155. bool Preserved = MB[RR.Reg/32] & (1u << (RR.Reg%32));
  156. // If the lane mask information is "full", e.g. when the given lane mask
  157. // is a superset of the lane mask from the register class, check the regmask
  158. // bit directly.
  159. if (RR.Mask == LaneBitmask::getAll())
  160. return !Preserved;
  161. const TargetRegisterClass *RC = RegInfos[RR.Reg].RegClass;
  162. if (RC != nullptr && (RR.Mask & RC->LaneMask) == RC->LaneMask)
  163. return !Preserved;
  164. // Otherwise, check all subregisters whose lane mask overlaps the given
  165. // mask. For each such register, if it is preserved by the regmask, then
  166. // clear the corresponding bits in the given mask. If at the end, all
  167. // bits have been cleared, the register does not alias the regmask (i.e.
  168. // is it preserved by it).
  169. LaneBitmask M = RR.Mask;
  170. for (MCSubRegIndexIterator SI(RR.Reg, &TRI); SI.isValid(); ++SI) {
  171. LaneBitmask SM = TRI.getSubRegIndexLaneMask(SI.getSubRegIndex());
  172. if ((SM & RR.Mask).none())
  173. continue;
  174. unsigned SR = SI.getSubReg();
  175. if (!(MB[SR/32] & (1u << (SR%32))))
  176. continue;
  177. // The subregister SR is preserved.
  178. M &= ~SM;
  179. if (M.none())
  180. return false;
  181. }
  182. return true;
  183. }
  184. bool PhysicalRegisterInfo::aliasMM(RegisterRef RM, RegisterRef RN) const {
  185. assert(isRegMaskId(RM.Reg) && isRegMaskId(RN.Reg));
  186. unsigned NumRegs = TRI.getNumRegs();
  187. const uint32_t *BM = getRegMaskBits(RM.Reg);
  188. const uint32_t *BN = getRegMaskBits(RN.Reg);
  189. for (unsigned w = 0, nw = NumRegs/32; w != nw; ++w) {
  190. // Intersect the negations of both words. Disregard reg=0,
  191. // i.e. 0th bit in the 0th word.
  192. uint32_t C = ~BM[w] & ~BN[w];
  193. if (w == 0)
  194. C &= ~1;
  195. if (C)
  196. return true;
  197. }
  198. // Check the remaining registers in the last word.
  199. unsigned TailRegs = NumRegs % 32;
  200. if (TailRegs == 0)
  201. return false;
  202. unsigned TW = NumRegs / 32;
  203. uint32_t TailMask = (1u << TailRegs) - 1;
  204. if (~BM[TW] & ~BN[TW] & TailMask)
  205. return true;
  206. return false;
  207. }
  208. RegisterRef PhysicalRegisterInfo::mapTo(RegisterRef RR, unsigned R) const {
  209. if (RR.Reg == R)
  210. return RR;
  211. if (unsigned Idx = TRI.getSubRegIndex(R, RR.Reg))
  212. return RegisterRef(R, TRI.composeSubRegIndexLaneMask(Idx, RR.Mask));
  213. if (unsigned Idx = TRI.getSubRegIndex(RR.Reg, R)) {
  214. const RegInfo &RI = RegInfos[R];
  215. LaneBitmask RCM = RI.RegClass ? RI.RegClass->LaneMask
  216. : LaneBitmask::getAll();
  217. LaneBitmask M = TRI.reverseComposeSubRegIndexLaneMask(Idx, RR.Mask);
  218. return RegisterRef(R, M & RCM);
  219. }
  220. llvm_unreachable("Invalid arguments: unrelated registers?");
  221. }
  222. bool RegisterAggr::hasAliasOf(RegisterRef RR) const {
  223. if (PhysicalRegisterInfo::isRegMaskId(RR.Reg))
  224. return Units.anyCommon(PRI.getMaskUnits(RR.Reg));
  225. for (MCRegUnitMaskIterator U(RR.Reg, &PRI.getTRI()); U.isValid(); ++U) {
  226. std::pair<uint32_t,LaneBitmask> P = *U;
  227. if (P.second.none() || (P.second & RR.Mask).any())
  228. if (Units.test(P.first))
  229. return true;
  230. }
  231. return false;
  232. }
  233. bool RegisterAggr::hasCoverOf(RegisterRef RR) const {
  234. if (PhysicalRegisterInfo::isRegMaskId(RR.Reg)) {
  235. BitVector T(PRI.getMaskUnits(RR.Reg));
  236. return T.reset(Units).none();
  237. }
  238. for (MCRegUnitMaskIterator U(RR.Reg, &PRI.getTRI()); U.isValid(); ++U) {
  239. std::pair<uint32_t,LaneBitmask> P = *U;
  240. if (P.second.none() || (P.second & RR.Mask).any())
  241. if (!Units.test(P.first))
  242. return false;
  243. }
  244. return true;
  245. }
  246. RegisterAggr &RegisterAggr::insert(RegisterRef RR) {
  247. if (PhysicalRegisterInfo::isRegMaskId(RR.Reg)) {
  248. Units |= PRI.getMaskUnits(RR.Reg);
  249. return *this;
  250. }
  251. for (MCRegUnitMaskIterator U(RR.Reg, &PRI.getTRI()); U.isValid(); ++U) {
  252. std::pair<uint32_t,LaneBitmask> P = *U;
  253. if (P.second.none() || (P.second & RR.Mask).any())
  254. Units.set(P.first);
  255. }
  256. return *this;
  257. }
  258. RegisterAggr &RegisterAggr::insert(const RegisterAggr &RG) {
  259. Units |= RG.Units;
  260. return *this;
  261. }
  262. RegisterAggr &RegisterAggr::intersect(RegisterRef RR) {
  263. return intersect(RegisterAggr(PRI).insert(RR));
  264. }
  265. RegisterAggr &RegisterAggr::intersect(const RegisterAggr &RG) {
  266. Units &= RG.Units;
  267. return *this;
  268. }
  269. RegisterAggr &RegisterAggr::clear(RegisterRef RR) {
  270. return clear(RegisterAggr(PRI).insert(RR));
  271. }
  272. RegisterAggr &RegisterAggr::clear(const RegisterAggr &RG) {
  273. Units.reset(RG.Units);
  274. return *this;
  275. }
  276. RegisterRef RegisterAggr::intersectWith(RegisterRef RR) const {
  277. RegisterAggr T(PRI);
  278. T.insert(RR).intersect(*this);
  279. if (T.empty())
  280. return RegisterRef();
  281. RegisterRef NR = T.makeRegRef();
  282. assert(NR);
  283. return NR;
  284. }
  285. RegisterRef RegisterAggr::clearIn(RegisterRef RR) const {
  286. return RegisterAggr(PRI).insert(RR).clear(*this).makeRegRef();
  287. }
  288. RegisterRef RegisterAggr::makeRegRef() const {
  289. int U = Units.find_first();
  290. if (U < 0)
  291. return RegisterRef();
  292. // Find the set of all registers that are aliased to all the units
  293. // in this aggregate.
  294. // Get all the registers aliased to the first unit in the bit vector.
  295. BitVector Regs = PRI.getUnitAliases(U);
  296. U = Units.find_next(U);
  297. // For each other unit, intersect it with the set of all registers
  298. // aliased that unit.
  299. while (U >= 0) {
  300. Regs &= PRI.getUnitAliases(U);
  301. U = Units.find_next(U);
  302. }
  303. // If there is at least one register remaining, pick the first one,
  304. // and consolidate the masks of all of its units contained in this
  305. // aggregate.
  306. int F = Regs.find_first();
  307. if (F <= 0)
  308. return RegisterRef();
  309. LaneBitmask M;
  310. for (MCRegUnitMaskIterator I(F, &PRI.getTRI()); I.isValid(); ++I) {
  311. std::pair<uint32_t,LaneBitmask> P = *I;
  312. if (Units.test(P.first))
  313. M |= P.second.none() ? LaneBitmask::getAll() : P.second;
  314. }
  315. return RegisterRef(F, M);
  316. }
  317. void RegisterAggr::print(raw_ostream &OS) const {
  318. OS << '{';
  319. for (int U = Units.find_first(); U >= 0; U = Units.find_next(U))
  320. OS << ' ' << printRegUnit(U, &PRI.getTRI());
  321. OS << " }";
  322. }
  323. RegisterAggr::rr_iterator::rr_iterator(const RegisterAggr &RG,
  324. bool End)
  325. : Owner(&RG) {
  326. for (int U = RG.Units.find_first(); U >= 0; U = RG.Units.find_next(U)) {
  327. RegisterRef R = RG.PRI.getRefForUnit(U);
  328. Masks[R.Reg] |= R.Mask;
  329. }
  330. Pos = End ? Masks.end() : Masks.begin();
  331. Index = End ? Masks.size() : 0;
  332. }
  333. raw_ostream &rdf::operator<<(raw_ostream &OS, const RegisterAggr &A) {
  334. A.print(OS);
  335. return OS;
  336. }